1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30 using namespace llvm::dwarf;
31
32 //===----------------------------------------------------------------------===//
33 // DIDescriptor
34 //===----------------------------------------------------------------------===//
35
DIDescriptor(const DIFile F)36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37 }
38
DIDescriptor(const DISubprogram F)39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40 }
41
DIDescriptor(const DILexicalBlockFile F)42 DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43 }
44
DIDescriptor(const DILexicalBlock F)45 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46 }
47
DIDescriptor(const DIVariable F)48 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49 }
50
DIDescriptor(const DIType F)51 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52 }
53
54 StringRef
getStringField(unsigned Elt) const55 DIDescriptor::getStringField(unsigned Elt) const {
56 if (DbgNode == 0)
57 return StringRef();
58
59 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
61 return MDS->getString();
62
63 return StringRef();
64 }
65
getUInt64Field(unsigned Elt) const66 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
67 if (DbgNode == 0)
68 return 0;
69
70 if (Elt < DbgNode->getNumOperands())
71 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
72 return CI->getZExtValue();
73
74 return 0;
75 }
76
getDescriptorField(unsigned Elt) const77 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
78 if (DbgNode == 0)
79 return DIDescriptor();
80
81 if (Elt < DbgNode->getNumOperands())
82 return
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
84 return DIDescriptor();
85 }
86
getGlobalVariableField(unsigned Elt) const87 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
88 if (DbgNode == 0)
89 return 0;
90
91 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
93 return 0;
94 }
95
getConstantField(unsigned Elt) const96 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
97 if (DbgNode == 0)
98 return 0;
99
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
102 return 0;
103 }
104
getFunctionField(unsigned Elt) const105 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
106 if (DbgNode == 0)
107 return 0;
108
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
111 return 0;
112 }
113
getNumAddrElements() const114 unsigned DIVariable::getNumAddrElements() const {
115 if (getVersion() <= llvm::LLVMDebugVersion8)
116 return DbgNode->getNumOperands()-6;
117 if (getVersion() == llvm::LLVMDebugVersion9)
118 return DbgNode->getNumOperands()-7;
119 return DbgNode->getNumOperands()-8;
120 }
121
122 /// getInlinedAt - If this variable is inlined then return inline location.
getInlinedAt() const123 MDNode *DIVariable::getInlinedAt() const {
124 if (getVersion() <= llvm::LLVMDebugVersion9)
125 return NULL;
126 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
127 }
128
129 //===----------------------------------------------------------------------===//
130 // Predicates
131 //===----------------------------------------------------------------------===//
132
133 /// isBasicType - Return true if the specified tag is legal for
134 /// DIBasicType.
isBasicType() const135 bool DIDescriptor::isBasicType() const {
136 if (!DbgNode) return false;
137 switch (getTag()) {
138 case dwarf::DW_TAG_base_type:
139 case dwarf::DW_TAG_unspecified_type:
140 return true;
141 default:
142 return false;
143 }
144 }
145
146 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
isDerivedType() const147 bool DIDescriptor::isDerivedType() const {
148 if (!DbgNode) return false;
149 switch (getTag()) {
150 case dwarf::DW_TAG_typedef:
151 case dwarf::DW_TAG_pointer_type:
152 case dwarf::DW_TAG_reference_type:
153 case dwarf::DW_TAG_const_type:
154 case dwarf::DW_TAG_volatile_type:
155 case dwarf::DW_TAG_restrict_type:
156 case dwarf::DW_TAG_member:
157 case dwarf::DW_TAG_inheritance:
158 case dwarf::DW_TAG_friend:
159 return true;
160 default:
161 // CompositeTypes are currently modelled as DerivedTypes.
162 return isCompositeType();
163 }
164 }
165
166 /// isCompositeType - Return true if the specified tag is legal for
167 /// DICompositeType.
isCompositeType() const168 bool DIDescriptor::isCompositeType() const {
169 if (!DbgNode) return false;
170 switch (getTag()) {
171 case dwarf::DW_TAG_array_type:
172 case dwarf::DW_TAG_structure_type:
173 case dwarf::DW_TAG_union_type:
174 case dwarf::DW_TAG_enumeration_type:
175 case dwarf::DW_TAG_vector_type:
176 case dwarf::DW_TAG_subroutine_type:
177 case dwarf::DW_TAG_class_type:
178 return true;
179 default:
180 return false;
181 }
182 }
183
184 /// isVariable - Return true if the specified tag is legal for DIVariable.
isVariable() const185 bool DIDescriptor::isVariable() const {
186 if (!DbgNode) return false;
187 switch (getTag()) {
188 case dwarf::DW_TAG_auto_variable:
189 case dwarf::DW_TAG_arg_variable:
190 case dwarf::DW_TAG_return_variable:
191 return true;
192 default:
193 return false;
194 }
195 }
196
197 /// isType - Return true if the specified tag is legal for DIType.
isType() const198 bool DIDescriptor::isType() const {
199 return isBasicType() || isCompositeType() || isDerivedType();
200 }
201
202 /// isSubprogram - Return true if the specified tag is legal for
203 /// DISubprogram.
isSubprogram() const204 bool DIDescriptor::isSubprogram() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
206 }
207
208 /// isGlobalVariable - Return true if the specified tag is legal for
209 /// DIGlobalVariable.
isGlobalVariable() const210 bool DIDescriptor::isGlobalVariable() const {
211 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
212 getTag() == dwarf::DW_TAG_constant);
213 }
214
215 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
isGlobal() const216 bool DIDescriptor::isGlobal() const {
217 return isGlobalVariable();
218 }
219
220 /// isUnspecifiedParmeter - Return true if the specified tag is
221 /// DW_TAG_unspecified_parameters.
isUnspecifiedParameter() const222 bool DIDescriptor::isUnspecifiedParameter() const {
223 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
224 }
225
226 /// isScope - Return true if the specified tag is one of the scope
227 /// related tag.
isScope() const228 bool DIDescriptor::isScope() const {
229 if (!DbgNode) return false;
230 switch (getTag()) {
231 case dwarf::DW_TAG_compile_unit:
232 case dwarf::DW_TAG_lexical_block:
233 case dwarf::DW_TAG_subprogram:
234 case dwarf::DW_TAG_namespace:
235 return true;
236 default:
237 break;
238 }
239 return false;
240 }
241
242 /// isTemplateTypeParameter - Return true if the specified tag is
243 /// DW_TAG_template_type_parameter.
isTemplateTypeParameter() const244 bool DIDescriptor::isTemplateTypeParameter() const {
245 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
246 }
247
248 /// isTemplateValueParameter - Return true if the specified tag is
249 /// DW_TAG_template_value_parameter.
isTemplateValueParameter() const250 bool DIDescriptor::isTemplateValueParameter() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
252 }
253
254 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
isCompileUnit() const255 bool DIDescriptor::isCompileUnit() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
257 }
258
259 /// isFile - Return true if the specified tag is DW_TAG_file_type.
isFile() const260 bool DIDescriptor::isFile() const {
261 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
262 }
263
264 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
isNameSpace() const265 bool DIDescriptor::isNameSpace() const {
266 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
267 }
268
269 /// isLexicalBlockFile - Return true if the specified descriptor is a
270 /// lexical block with an extra file.
isLexicalBlockFile() const271 bool DIDescriptor::isLexicalBlockFile() const {
272 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
273 (DbgNode->getNumOperands() == 3);
274 }
275
276 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
isLexicalBlock() const277 bool DIDescriptor::isLexicalBlock() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
279 (DbgNode->getNumOperands() > 3);
280 }
281
282 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
isSubrange() const283 bool DIDescriptor::isSubrange() const {
284 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
285 }
286
287 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
isEnumerator() const288 bool DIDescriptor::isEnumerator() const {
289 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
290 }
291
292 //===----------------------------------------------------------------------===//
293 // Simple Descriptor Constructors and other Methods
294 //===----------------------------------------------------------------------===//
295
DIType(const MDNode * N)296 DIType::DIType(const MDNode *N) : DIScope(N) {
297 if (!N) return;
298 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
299 DbgNode = 0;
300 }
301 }
302
getNumElements() const303 unsigned DIArray::getNumElements() const {
304 if (!DbgNode)
305 return 0;
306 return DbgNode->getNumOperands();
307 }
308
309 /// replaceAllUsesWith - Replace all uses of debug info referenced by
310 /// this descriptor.
replaceAllUsesWith(DIDescriptor & D)311 void DIType::replaceAllUsesWith(DIDescriptor &D) {
312 if (!DbgNode)
313 return;
314
315 // Since we use a TrackingVH for the node, its easy for clients to manufacture
316 // legitimate situations where they want to replaceAllUsesWith() on something
317 // which, due to uniquing, has merged with the source. We shield clients from
318 // this detail by allowing a value to be replaced with replaceAllUsesWith()
319 // itself.
320 if (DbgNode != D) {
321 MDNode *Node = const_cast<MDNode*>(DbgNode);
322 const MDNode *DN = D;
323 const Value *V = cast_or_null<Value>(DN);
324 Node->replaceAllUsesWith(const_cast<Value*>(V));
325 MDNode::deleteTemporary(Node);
326 }
327 }
328
329 /// replaceAllUsesWith - Replace all uses of debug info referenced by
330 /// this descriptor.
replaceAllUsesWith(MDNode * D)331 void DIType::replaceAllUsesWith(MDNode *D) {
332 if (!DbgNode)
333 return;
334
335 // Since we use a TrackingVH for the node, its easy for clients to manufacture
336 // legitimate situations where they want to replaceAllUsesWith() on something
337 // which, due to uniquing, has merged with the source. We shield clients from
338 // this detail by allowing a value to be replaced with replaceAllUsesWith()
339 // itself.
340 if (DbgNode != D) {
341 MDNode *Node = const_cast<MDNode*>(DbgNode);
342 const MDNode *DN = D;
343 const Value *V = cast_or_null<Value>(DN);
344 Node->replaceAllUsesWith(const_cast<Value*>(V));
345 MDNode::deleteTemporary(Node);
346 }
347 }
348
349 /// isUnsignedDIType - Return true if type encoding is unsigned.
isUnsignedDIType()350 bool DIType::isUnsignedDIType() {
351 DIDerivedType DTy(DbgNode);
352 if (DTy.Verify())
353 return DTy.getTypeDerivedFrom().isUnsignedDIType();
354
355 DIBasicType BTy(DbgNode);
356 if (BTy.Verify()) {
357 unsigned Encoding = BTy.getEncoding();
358 if (Encoding == dwarf::DW_ATE_unsigned ||
359 Encoding == dwarf::DW_ATE_unsigned_char)
360 return true;
361 }
362 return false;
363 }
364
365 /// Verify - Verify that a compile unit is well formed.
Verify() const366 bool DICompileUnit::Verify() const {
367 if (!DbgNode)
368 return false;
369 StringRef N = getFilename();
370 if (N.empty())
371 return false;
372 // It is possible that directory and produce string is empty.
373 return true;
374 }
375
376 /// Verify - Verify that a type descriptor is well formed.
Verify() const377 bool DIType::Verify() const {
378 if (!DbgNode)
379 return false;
380 if (getContext() && !getContext().Verify())
381 return false;
382 unsigned Tag = getTag();
383 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
384 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
385 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
386 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
387 && Tag != dwarf::DW_TAG_enumeration_type
388 && Tag != dwarf::DW_TAG_subroutine_type
389 && getFilename().empty())
390 return false;
391 return true;
392 }
393
394 /// Verify - Verify that a basic type descriptor is well formed.
Verify() const395 bool DIBasicType::Verify() const {
396 return isBasicType();
397 }
398
399 /// Verify - Verify that a derived type descriptor is well formed.
Verify() const400 bool DIDerivedType::Verify() const {
401 return isDerivedType();
402 }
403
404 /// Verify - Verify that a composite type descriptor is well formed.
Verify() const405 bool DICompositeType::Verify() const {
406 if (!DbgNode)
407 return false;
408 if (getContext() && !getContext().Verify())
409 return false;
410
411 return true;
412 }
413
414 /// Verify - Verify that a subprogram descriptor is well formed.
Verify() const415 bool DISubprogram::Verify() const {
416 if (!DbgNode)
417 return false;
418
419 if (getContext() && !getContext().Verify())
420 return false;
421
422 DICompositeType Ty = getType();
423 if (!Ty.Verify())
424 return false;
425 return true;
426 }
427
428 /// Verify - Verify that a global variable descriptor is well formed.
Verify() const429 bool DIGlobalVariable::Verify() const {
430 if (!DbgNode)
431 return false;
432
433 if (getDisplayName().empty())
434 return false;
435
436 if (getContext() && !getContext().Verify())
437 return false;
438
439 DIType Ty = getType();
440 if (!Ty.Verify())
441 return false;
442
443 if (!getGlobal() && !getConstant())
444 return false;
445
446 return true;
447 }
448
449 /// Verify - Verify that a variable descriptor is well formed.
Verify() const450 bool DIVariable::Verify() const {
451 if (!DbgNode)
452 return false;
453
454 if (getContext() && !getContext().Verify())
455 return false;
456
457 DIType Ty = getType();
458 if (!Ty.Verify())
459 return false;
460
461 return true;
462 }
463
464 /// Verify - Verify that a location descriptor is well formed.
Verify() const465 bool DILocation::Verify() const {
466 if (!DbgNode)
467 return false;
468
469 return DbgNode->getNumOperands() == 4;
470 }
471
472 /// Verify - Verify that a namespace descriptor is well formed.
Verify() const473 bool DINameSpace::Verify() const {
474 if (!DbgNode)
475 return false;
476 if (getName().empty())
477 return false;
478 return true;
479 }
480
481 /// getOriginalTypeSize - If this type is derived from a base type then
482 /// return base type size.
getOriginalTypeSize() const483 uint64_t DIDerivedType::getOriginalTypeSize() const {
484 unsigned Tag = getTag();
485 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
486 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
487 Tag == dwarf::DW_TAG_restrict_type) {
488 DIType BaseType = getTypeDerivedFrom();
489 // If this type is not derived from any type then take conservative
490 // approach.
491 if (!BaseType.isValid())
492 return getSizeInBits();
493 if (BaseType.isDerivedType())
494 return DIDerivedType(BaseType).getOriginalTypeSize();
495 else
496 return BaseType.getSizeInBits();
497 }
498
499 return getSizeInBits();
500 }
501
502 /// isInlinedFnArgument - Return true if this variable provides debugging
503 /// information for an inlined function arguments.
isInlinedFnArgument(const Function * CurFn)504 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
505 assert(CurFn && "Invalid function");
506 if (!getContext().isSubprogram())
507 return false;
508 // This variable is not inlined function argument if its scope
509 // does not describe current function.
510 return !(DISubprogram(getContext()).describes(CurFn));
511 }
512
513 /// describes - Return true if this subprogram provides debugging
514 /// information for the function F.
describes(const Function * F)515 bool DISubprogram::describes(const Function *F) {
516 assert(F && "Invalid function");
517 if (F == getFunction())
518 return true;
519 StringRef Name = getLinkageName();
520 if (Name.empty())
521 Name = getName();
522 if (F->getName() == Name)
523 return true;
524 return false;
525 }
526
isOptimized() const527 unsigned DISubprogram::isOptimized() const {
528 assert (DbgNode && "Invalid subprogram descriptor!");
529 if (DbgNode->getNumOperands() == 16)
530 return getUnsignedField(15);
531 return 0;
532 }
533
getVariablesNodes() const534 MDNode *DISubprogram::getVariablesNodes() const {
535 if (!DbgNode || DbgNode->getNumOperands() <= 19)
536 return NULL;
537 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
538 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
539 return NULL;
540 }
541
getVariables() const542 DIArray DISubprogram::getVariables() const {
543 if (!DbgNode || DbgNode->getNumOperands() <= 19)
544 return DIArray();
545 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
546 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
547 return DIArray(A);
548 return DIArray();
549 }
550
getFilename() const551 StringRef DIScope::getFilename() const {
552 if (!DbgNode)
553 return StringRef();
554 if (isLexicalBlockFile())
555 return DILexicalBlockFile(DbgNode).getFilename();
556 if (isLexicalBlock())
557 return DILexicalBlock(DbgNode).getFilename();
558 if (isSubprogram())
559 return DISubprogram(DbgNode).getFilename();
560 if (isCompileUnit())
561 return DICompileUnit(DbgNode).getFilename();
562 if (isNameSpace())
563 return DINameSpace(DbgNode).getFilename();
564 if (isType())
565 return DIType(DbgNode).getFilename();
566 if (isFile())
567 return DIFile(DbgNode).getFilename();
568 assert(0 && "Invalid DIScope!");
569 return StringRef();
570 }
571
getDirectory() const572 StringRef DIScope::getDirectory() const {
573 if (!DbgNode)
574 return StringRef();
575 if (isLexicalBlockFile())
576 return DILexicalBlockFile(DbgNode).getDirectory();
577 if (isLexicalBlock())
578 return DILexicalBlock(DbgNode).getDirectory();
579 if (isSubprogram())
580 return DISubprogram(DbgNode).getDirectory();
581 if (isCompileUnit())
582 return DICompileUnit(DbgNode).getDirectory();
583 if (isNameSpace())
584 return DINameSpace(DbgNode).getDirectory();
585 if (isType())
586 return DIType(DbgNode).getDirectory();
587 if (isFile())
588 return DIFile(DbgNode).getDirectory();
589 assert(0 && "Invalid DIScope!");
590 return StringRef();
591 }
592
getEnumTypes() const593 DIArray DICompileUnit::getEnumTypes() const {
594 if (!DbgNode || DbgNode->getNumOperands() < 14)
595 return DIArray();
596
597 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
598 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
599 return DIArray(A);
600 return DIArray();
601 }
602
getRetainedTypes() const603 DIArray DICompileUnit::getRetainedTypes() const {
604 if (!DbgNode || DbgNode->getNumOperands() < 14)
605 return DIArray();
606
607 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
608 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
609 return DIArray(A);
610 return DIArray();
611 }
612
getSubprograms() const613 DIArray DICompileUnit::getSubprograms() const {
614 if (!DbgNode || DbgNode->getNumOperands() < 14)
615 return DIArray();
616
617 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
618 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
619 return DIArray(A);
620 return DIArray();
621 }
622
623
getGlobalVariables() const624 DIArray DICompileUnit::getGlobalVariables() const {
625 if (!DbgNode || DbgNode->getNumOperands() < 14)
626 return DIArray();
627
628 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
629 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
630 return DIArray(A);
631 return DIArray();
632 }
633
634 //===----------------------------------------------------------------------===//
635 // DIDescriptor: dump routines for all descriptors.
636 //===----------------------------------------------------------------------===//
637
638
639 /// print - Print descriptor.
print(raw_ostream & OS) const640 void DIDescriptor::print(raw_ostream &OS) const {
641 OS << "[" << dwarf::TagString(getTag()) << "] ";
642 OS.write_hex((intptr_t) &*DbgNode) << ']';
643 }
644
645 /// print - Print compile unit.
print(raw_ostream & OS) const646 void DICompileUnit::print(raw_ostream &OS) const {
647 if (getLanguage())
648 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
649
650 OS << " [" << getDirectory() << "/" << getFilename() << "]";
651 }
652
653 /// print - Print type.
print(raw_ostream & OS) const654 void DIType::print(raw_ostream &OS) const {
655 if (!DbgNode) return;
656
657 StringRef Res = getName();
658 if (!Res.empty())
659 OS << " [" << Res << "] ";
660
661 unsigned Tag = getTag();
662 OS << " [" << dwarf::TagString(Tag) << "] ";
663
664 // TODO : Print context
665 OS << " ["
666 << "line " << getLineNumber() << ", "
667 << getSizeInBits() << " bits, "
668 << getAlignInBits() << " bit alignment, "
669 << getOffsetInBits() << " bit offset"
670 << "] ";
671
672 if (isPrivate())
673 OS << " [private] ";
674 else if (isProtected())
675 OS << " [protected] ";
676
677 if (isForwardDecl())
678 OS << " [fwd] ";
679
680 if (isBasicType())
681 DIBasicType(DbgNode).print(OS);
682 else if (isDerivedType())
683 DIDerivedType(DbgNode).print(OS);
684 else if (isCompositeType())
685 DICompositeType(DbgNode).print(OS);
686 else {
687 OS << "Invalid DIType\n";
688 return;
689 }
690
691 OS << "\n";
692 }
693
694 /// print - Print basic type.
print(raw_ostream & OS) const695 void DIBasicType::print(raw_ostream &OS) const {
696 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
697 }
698
699 /// print - Print derived type.
print(raw_ostream & OS) const700 void DIDerivedType::print(raw_ostream &OS) const {
701 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
702 }
703
704 /// print - Print composite type.
print(raw_ostream & OS) const705 void DICompositeType::print(raw_ostream &OS) const {
706 DIArray A = getTypeArray();
707 OS << " [" << A.getNumElements() << " elements]";
708 }
709
710 /// print - Print subprogram.
print(raw_ostream & OS) const711 void DISubprogram::print(raw_ostream &OS) const {
712 StringRef Res = getName();
713 if (!Res.empty())
714 OS << " [" << Res << "] ";
715
716 unsigned Tag = getTag();
717 OS << " [" << dwarf::TagString(Tag) << "] ";
718
719 // TODO : Print context
720 OS << " [" << getLineNumber() << "] ";
721
722 if (isLocalToUnit())
723 OS << " [local] ";
724
725 if (isDefinition())
726 OS << " [def] ";
727
728 OS << "\n";
729 }
730
731 /// print - Print global variable.
print(raw_ostream & OS) const732 void DIGlobalVariable::print(raw_ostream &OS) const {
733 OS << " [";
734 StringRef Res = getName();
735 if (!Res.empty())
736 OS << " [" << Res << "] ";
737
738 unsigned Tag = getTag();
739 OS << " [" << dwarf::TagString(Tag) << "] ";
740
741 // TODO : Print context
742 OS << " [" << getLineNumber() << "] ";
743
744 if (isLocalToUnit())
745 OS << " [local] ";
746
747 if (isDefinition())
748 OS << " [def] ";
749
750 if (isGlobalVariable())
751 DIGlobalVariable(DbgNode).print(OS);
752 OS << "]\n";
753 }
754
printDebugLoc(DebugLoc DL,raw_ostream & CommentOS,const LLVMContext & Ctx)755 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
756 const LLVMContext &Ctx) {
757 if (!DL.isUnknown()) { // Print source line info.
758 DIScope Scope(DL.getScope(Ctx));
759 // Omit the directory, because it's likely to be long and uninteresting.
760 if (Scope.Verify())
761 CommentOS << Scope.getFilename();
762 else
763 CommentOS << "<unknown>";
764 CommentOS << ':' << DL.getLine();
765 if (DL.getCol() != 0)
766 CommentOS << ':' << DL.getCol();
767 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
768 if (!InlinedAtDL.isUnknown()) {
769 CommentOS << " @[ ";
770 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
771 CommentOS << " ]";
772 }
773 }
774 }
775
printExtendedName(raw_ostream & OS) const776 void DIVariable::printExtendedName(raw_ostream &OS) const {
777 const LLVMContext &Ctx = DbgNode->getContext();
778 StringRef Res = getName();
779 if (!Res.empty())
780 OS << Res << "," << getLineNumber();
781 if (MDNode *InlinedAt = getInlinedAt()) {
782 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
783 if (!InlinedAtDL.isUnknown()) {
784 OS << " @[";
785 printDebugLoc(InlinedAtDL, OS, Ctx);
786 OS << "]";
787 }
788 }
789 }
790
791 /// print - Print variable.
print(raw_ostream & OS) const792 void DIVariable::print(raw_ostream &OS) const {
793 StringRef Res = getName();
794 if (!Res.empty())
795 OS << " [" << Res << "] ";
796
797 OS << " [" << getLineNumber() << "] ";
798 getType().print(OS);
799 OS << "\n";
800
801 // FIXME: Dump complex addresses
802 }
803
804 /// dump - Print descriptor to dbgs() with a newline.
dump() const805 void DIDescriptor::dump() const {
806 print(dbgs()); dbgs() << '\n';
807 }
808
809 /// dump - Print compile unit to dbgs() with a newline.
dump() const810 void DICompileUnit::dump() const {
811 print(dbgs()); dbgs() << '\n';
812 }
813
814 /// dump - Print type to dbgs() with a newline.
dump() const815 void DIType::dump() const {
816 print(dbgs()); dbgs() << '\n';
817 }
818
819 /// dump - Print basic type to dbgs() with a newline.
dump() const820 void DIBasicType::dump() const {
821 print(dbgs()); dbgs() << '\n';
822 }
823
824 /// dump - Print derived type to dbgs() with a newline.
dump() const825 void DIDerivedType::dump() const {
826 print(dbgs()); dbgs() << '\n';
827 }
828
829 /// dump - Print composite type to dbgs() with a newline.
dump() const830 void DICompositeType::dump() const {
831 print(dbgs()); dbgs() << '\n';
832 }
833
834 /// dump - Print subprogram to dbgs() with a newline.
dump() const835 void DISubprogram::dump() const {
836 print(dbgs()); dbgs() << '\n';
837 }
838
839 /// dump - Print global variable.
dump() const840 void DIGlobalVariable::dump() const {
841 print(dbgs()); dbgs() << '\n';
842 }
843
844 /// dump - Print variable.
dump() const845 void DIVariable::dump() const {
846 print(dbgs()); dbgs() << '\n';
847 }
848
849 /// fixupObjcLikeName - Replace contains special characters used
850 /// in a typical Objective-C names with '.' in a given string.
fixupObjcLikeName(StringRef Str,SmallVectorImpl<char> & Out)851 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
852 bool isObjCLike = false;
853 for (size_t i = 0, e = Str.size(); i < e; ++i) {
854 char C = Str[i];
855 if (C == '[')
856 isObjCLike = true;
857
858 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
859 C == '+' || C == '(' || C == ')'))
860 Out.push_back('.');
861 else
862 Out.push_back(C);
863 }
864 }
865
866 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
867 /// suitable to hold function specific information.
getFnSpecificMDNode(const Module & M,DISubprogram Fn)868 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
869 SmallString<32> Name = StringRef("llvm.dbg.lv.");
870 StringRef FName = "fn";
871 if (Fn.getFunction())
872 FName = Fn.getFunction()->getName();
873 else
874 FName = Fn.getName();
875 char One = '\1';
876 if (FName.startswith(StringRef(&One, 1)))
877 FName = FName.substr(1);
878 fixupObjcLikeName(FName, Name);
879 return M.getNamedMetadata(Name.str());
880 }
881
882 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
883 /// to hold function specific information.
getOrInsertFnSpecificMDNode(Module & M,DISubprogram Fn)884 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
885 SmallString<32> Name = StringRef("llvm.dbg.lv.");
886 StringRef FName = "fn";
887 if (Fn.getFunction())
888 FName = Fn.getFunction()->getName();
889 else
890 FName = Fn.getName();
891 char One = '\1';
892 if (FName.startswith(StringRef(&One, 1)))
893 FName = FName.substr(1);
894 fixupObjcLikeName(FName, Name);
895
896 return M.getOrInsertNamedMetadata(Name.str());
897 }
898
899 /// createInlinedVariable - Create a new inlined variable based on current
900 /// variable.
901 /// @param DV Current Variable.
902 /// @param InlinedScope Location at current variable is inlined.
createInlinedVariable(MDNode * DV,MDNode * InlinedScope,LLVMContext & VMContext)903 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
904 LLVMContext &VMContext) {
905 SmallVector<Value *, 16> Elts;
906 // Insert inlined scope as 7th element.
907 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
908 i == 7 ? Elts.push_back(InlinedScope) :
909 Elts.push_back(DV->getOperand(i));
910 return DIVariable(MDNode::get(VMContext, Elts));
911 }
912
913 /// cleanseInlinedVariable - Remove inlined scope from the variable.
cleanseInlinedVariable(MDNode * DV,LLVMContext & VMContext)914 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
915 SmallVector<Value *, 16> Elts;
916 // Insert inlined scope as 7th element.
917 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
918 i == 7 ?
919 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
920 Elts.push_back(DV->getOperand(i));
921 return DIVariable(MDNode::get(VMContext, Elts));
922 }
923
924 //===----------------------------------------------------------------------===//
925 // DebugInfoFinder implementations.
926 //===----------------------------------------------------------------------===//
927
928 /// processModule - Process entire module and collect debug info.
processModule(Module & M)929 void DebugInfoFinder::processModule(Module &M) {
930 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"))
931 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
932 addCompileUnit(DICompileUnit(CU_Nodes->getOperand(i)));
933
934 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
935 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
936 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
937 ++BI) {
938 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
939 processDeclare(DDI);
940
941 DebugLoc Loc = BI->getDebugLoc();
942 if (Loc.isUnknown())
943 continue;
944
945 LLVMContext &Ctx = BI->getContext();
946 DIDescriptor Scope(Loc.getScope(Ctx));
947
948 if (Scope.isCompileUnit())
949 addCompileUnit(DICompileUnit(Scope));
950 else if (Scope.isSubprogram())
951 processSubprogram(DISubprogram(Scope));
952 else if (Scope.isLexicalBlockFile()) {
953 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
954 processLexicalBlock(DILexicalBlock(DBF.getScope()));
955 }
956 else if (Scope.isLexicalBlock())
957 processLexicalBlock(DILexicalBlock(Scope));
958
959 if (MDNode *IA = Loc.getInlinedAt(Ctx))
960 processLocation(DILocation(IA));
961 }
962
963 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
964 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
965 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
966 if (addGlobalVariable(DIG)) {
967 if (DIG.getVersion() <= LLVMDebugVersion10)
968 addCompileUnit(DIG.getCompileUnit());
969 processType(DIG.getType());
970 }
971 }
972 }
973
974 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
975 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
976 processSubprogram(DISubprogram(NMD->getOperand(i)));
977 }
978
979 /// processLocation - Process DILocation.
processLocation(DILocation Loc)980 void DebugInfoFinder::processLocation(DILocation Loc) {
981 if (!Loc.Verify()) return;
982 DIDescriptor S(Loc.getScope());
983 if (S.isCompileUnit())
984 addCompileUnit(DICompileUnit(S));
985 else if (S.isSubprogram())
986 processSubprogram(DISubprogram(S));
987 else if (S.isLexicalBlock())
988 processLexicalBlock(DILexicalBlock(S));
989 else if (S.isLexicalBlockFile()) {
990 DILexicalBlockFile DBF = DILexicalBlockFile(S);
991 processLexicalBlock(DILexicalBlock(DBF.getScope()));
992 }
993 processLocation(Loc.getOrigLocation());
994 }
995
996 /// processType - Process DIType.
processType(DIType DT)997 void DebugInfoFinder::processType(DIType DT) {
998 if (!addType(DT))
999 return;
1000 if (DT.getVersion() <= LLVMDebugVersion10)
1001 addCompileUnit(DT.getCompileUnit());
1002 if (DT.isCompositeType()) {
1003 DICompositeType DCT(DT);
1004 processType(DCT.getTypeDerivedFrom());
1005 DIArray DA = DCT.getTypeArray();
1006 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1007 DIDescriptor D = DA.getElement(i);
1008 if (D.isType())
1009 processType(DIType(D));
1010 else if (D.isSubprogram())
1011 processSubprogram(DISubprogram(D));
1012 }
1013 } else if (DT.isDerivedType()) {
1014 DIDerivedType DDT(DT);
1015 processType(DDT.getTypeDerivedFrom());
1016 }
1017 }
1018
1019 /// processLexicalBlock
processLexicalBlock(DILexicalBlock LB)1020 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1021 DIScope Context = LB.getContext();
1022 if (Context.isLexicalBlock())
1023 return processLexicalBlock(DILexicalBlock(Context));
1024 else if (Context.isLexicalBlockFile()) {
1025 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1026 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1027 }
1028 else
1029 return processSubprogram(DISubprogram(Context));
1030 }
1031
1032 /// processSubprogram - Process DISubprogram.
processSubprogram(DISubprogram SP)1033 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1034 if (!addSubprogram(SP))
1035 return;
1036 if (SP.getVersion() <= LLVMDebugVersion10)
1037 addCompileUnit(SP.getCompileUnit());
1038 processType(SP.getType());
1039 }
1040
1041 /// processDeclare - Process DbgDeclareInst.
processDeclare(DbgDeclareInst * DDI)1042 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1043 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1044 if (!N) return;
1045
1046 DIDescriptor DV(N);
1047 if (!DV.isVariable())
1048 return;
1049
1050 if (!NodesSeen.insert(DV))
1051 return;
1052 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1053 addCompileUnit(DIVariable(N).getCompileUnit());
1054 processType(DIVariable(N).getType());
1055 }
1056
1057 /// addType - Add type into Tys.
addType(DIType DT)1058 bool DebugInfoFinder::addType(DIType DT) {
1059 if (!DT.isValid())
1060 return false;
1061
1062 if (!NodesSeen.insert(DT))
1063 return false;
1064
1065 TYs.push_back(DT);
1066 return true;
1067 }
1068
1069 /// addCompileUnit - Add compile unit into CUs.
addCompileUnit(DICompileUnit CU)1070 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1071 if (!CU.Verify())
1072 return false;
1073
1074 if (!NodesSeen.insert(CU))
1075 return false;
1076
1077 CUs.push_back(CU);
1078 return true;
1079 }
1080
1081 /// addGlobalVariable - Add global variable into GVs.
addGlobalVariable(DIGlobalVariable DIG)1082 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1083 if (!DIDescriptor(DIG).isGlobalVariable())
1084 return false;
1085
1086 if (!NodesSeen.insert(DIG))
1087 return false;
1088
1089 GVs.push_back(DIG);
1090 return true;
1091 }
1092
1093 // addSubprogram - Add subprgoram into SPs.
addSubprogram(DISubprogram SP)1094 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1095 if (!DIDescriptor(SP).isSubprogram())
1096 return false;
1097
1098 if (!NodesSeen.insert(SP))
1099 return false;
1100
1101 SPs.push_back(SP);
1102 return true;
1103 }
1104
1105 /// getDISubprogram - Find subprogram that is enclosing this scope.
getDISubprogram(const MDNode * Scope)1106 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1107 DIDescriptor D(Scope);
1108 if (D.isSubprogram())
1109 return DISubprogram(Scope);
1110
1111 if (D.isLexicalBlockFile())
1112 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
1113
1114 if (D.isLexicalBlock())
1115 return getDISubprogram(DILexicalBlock(Scope).getContext());
1116
1117 return DISubprogram();
1118 }
1119
1120 /// getDICompositeType - Find underlying composite type.
getDICompositeType(DIType T)1121 DICompositeType llvm::getDICompositeType(DIType T) {
1122 if (T.isCompositeType())
1123 return DICompositeType(T);
1124
1125 if (T.isDerivedType())
1126 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1127
1128 return DICompositeType();
1129 }
1130
1131 /// isSubprogramContext - Return true if Context is either a subprogram
1132 /// or another context nested inside a subprogram.
isSubprogramContext(const MDNode * Context)1133 bool llvm::isSubprogramContext(const MDNode *Context) {
1134 if (!Context)
1135 return false;
1136 DIDescriptor D(Context);
1137 if (D.isSubprogram())
1138 return true;
1139 if (D.isType())
1140 return isSubprogramContext(DIType(Context).getContext());
1141 return false;
1142 }
1143
1144