• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DILexicalBlock F)42 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
43 }
44 
DIDescriptor(const DIVariable F)45 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
46 }
47 
DIDescriptor(const DIType F)48 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
49 }
50 
51 StringRef
getStringField(unsigned Elt) const52 DIDescriptor::getStringField(unsigned Elt) const {
53   if (DbgNode == 0)
54     return StringRef();
55 
56   if (Elt < DbgNode->getNumOperands())
57     if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
58       return MDS->getString();
59 
60   return StringRef();
61 }
62 
getUInt64Field(unsigned Elt) const63 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
64   if (DbgNode == 0)
65     return 0;
66 
67   if (Elt < DbgNode->getNumOperands())
68     if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
69       return CI->getZExtValue();
70 
71   return 0;
72 }
73 
getDescriptorField(unsigned Elt) const74 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
75   if (DbgNode == 0)
76     return DIDescriptor();
77 
78   if (Elt < DbgNode->getNumOperands())
79     return
80       DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
81   return DIDescriptor();
82 }
83 
getGlobalVariableField(unsigned Elt) const84 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
85   if (DbgNode == 0)
86     return 0;
87 
88   if (Elt < DbgNode->getNumOperands())
89       return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
90   return 0;
91 }
92 
getConstantField(unsigned Elt) const93 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
94   if (DbgNode == 0)
95     return 0;
96 
97   if (Elt < DbgNode->getNumOperands())
98       return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
99   return 0;
100 }
101 
getFunctionField(unsigned Elt) const102 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
103   if (DbgNode == 0)
104     return 0;
105 
106   if (Elt < DbgNode->getNumOperands())
107       return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
108   return 0;
109 }
110 
getNumAddrElements() const111 unsigned DIVariable::getNumAddrElements() const {
112   if (getVersion() <= llvm::LLVMDebugVersion8)
113     return DbgNode->getNumOperands()-6;
114   if (getVersion() == llvm::LLVMDebugVersion9)
115     return DbgNode->getNumOperands()-7;
116   return DbgNode->getNumOperands()-8;
117 }
118 
119 
120 //===----------------------------------------------------------------------===//
121 // Predicates
122 //===----------------------------------------------------------------------===//
123 
124 /// isBasicType - Return true if the specified tag is legal for
125 /// DIBasicType.
isBasicType() const126 bool DIDescriptor::isBasicType() const {
127   return DbgNode && getTag() == dwarf::DW_TAG_base_type;
128 }
129 
130 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
isDerivedType() const131 bool DIDescriptor::isDerivedType() const {
132   if (!DbgNode) return false;
133   switch (getTag()) {
134   case dwarf::DW_TAG_typedef:
135   case dwarf::DW_TAG_pointer_type:
136   case dwarf::DW_TAG_reference_type:
137   case dwarf::DW_TAG_const_type:
138   case dwarf::DW_TAG_volatile_type:
139   case dwarf::DW_TAG_restrict_type:
140   case dwarf::DW_TAG_member:
141   case dwarf::DW_TAG_inheritance:
142   case dwarf::DW_TAG_friend:
143     return true;
144   default:
145     // CompositeTypes are currently modelled as DerivedTypes.
146     return isCompositeType();
147   }
148 }
149 
150 /// isCompositeType - Return true if the specified tag is legal for
151 /// DICompositeType.
isCompositeType() const152 bool DIDescriptor::isCompositeType() const {
153   if (!DbgNode) return false;
154   switch (getTag()) {
155   case dwarf::DW_TAG_array_type:
156   case dwarf::DW_TAG_structure_type:
157   case dwarf::DW_TAG_union_type:
158   case dwarf::DW_TAG_enumeration_type:
159   case dwarf::DW_TAG_vector_type:
160   case dwarf::DW_TAG_subroutine_type:
161   case dwarf::DW_TAG_class_type:
162     return true;
163   default:
164     return false;
165   }
166 }
167 
168 /// isVariable - Return true if the specified tag is legal for DIVariable.
isVariable() const169 bool DIDescriptor::isVariable() const {
170   if (!DbgNode) return false;
171   switch (getTag()) {
172   case dwarf::DW_TAG_auto_variable:
173   case dwarf::DW_TAG_arg_variable:
174   case dwarf::DW_TAG_return_variable:
175     return true;
176   default:
177     return false;
178   }
179 }
180 
181 /// isType - Return true if the specified tag is legal for DIType.
isType() const182 bool DIDescriptor::isType() const {
183   return isBasicType() || isCompositeType() || isDerivedType();
184 }
185 
186 /// isSubprogram - Return true if the specified tag is legal for
187 /// DISubprogram.
isSubprogram() const188 bool DIDescriptor::isSubprogram() const {
189   return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
190 }
191 
192 /// isGlobalVariable - Return true if the specified tag is legal for
193 /// DIGlobalVariable.
isGlobalVariable() const194 bool DIDescriptor::isGlobalVariable() const {
195   return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
196                      getTag() == dwarf::DW_TAG_constant);
197 }
198 
199 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
isGlobal() const200 bool DIDescriptor::isGlobal() const {
201   return isGlobalVariable();
202 }
203 
204 /// isUnspecifiedParmeter - Return true if the specified tag is
205 /// DW_TAG_unspecified_parameters.
isUnspecifiedParameter() const206 bool DIDescriptor::isUnspecifiedParameter() const {
207   return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
208 }
209 
210 /// isScope - Return true if the specified tag is one of the scope
211 /// related tag.
isScope() const212 bool DIDescriptor::isScope() const {
213   if (!DbgNode) return false;
214   switch (getTag()) {
215   case dwarf::DW_TAG_compile_unit:
216   case dwarf::DW_TAG_lexical_block:
217   case dwarf::DW_TAG_subprogram:
218   case dwarf::DW_TAG_namespace:
219     return true;
220   default:
221     break;
222   }
223   return false;
224 }
225 
226 /// isTemplateTypeParameter - Return true if the specified tag is
227 /// DW_TAG_template_type_parameter.
isTemplateTypeParameter() const228 bool DIDescriptor::isTemplateTypeParameter() const {
229   return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
230 }
231 
232 /// isTemplateValueParameter - Return true if the specified tag is
233 /// DW_TAG_template_value_parameter.
isTemplateValueParameter() const234 bool DIDescriptor::isTemplateValueParameter() const {
235   return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
236 }
237 
238 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
isCompileUnit() const239 bool DIDescriptor::isCompileUnit() const {
240   return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
241 }
242 
243 /// isFile - Return true if the specified tag is DW_TAG_file_type.
isFile() const244 bool DIDescriptor::isFile() const {
245   return DbgNode && getTag() == dwarf::DW_TAG_file_type;
246 }
247 
248 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
isNameSpace() const249 bool DIDescriptor::isNameSpace() const {
250   return DbgNode && getTag() == dwarf::DW_TAG_namespace;
251 }
252 
253 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
isLexicalBlock() const254 bool DIDescriptor::isLexicalBlock() const {
255   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
256 }
257 
258 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
isSubrange() const259 bool DIDescriptor::isSubrange() const {
260   return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
261 }
262 
263 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
isEnumerator() const264 bool DIDescriptor::isEnumerator() const {
265   return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
266 }
267 
268 //===----------------------------------------------------------------------===//
269 // Simple Descriptor Constructors and other Methods
270 //===----------------------------------------------------------------------===//
271 
DIType(const MDNode * N)272 DIType::DIType(const MDNode *N) : DIScope(N) {
273   if (!N) return;
274   if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
275     DbgNode = 0;
276   }
277 }
278 
getNumElements() const279 unsigned DIArray::getNumElements() const {
280   if (!DbgNode)
281     return 0;
282   return DbgNode->getNumOperands();
283 }
284 
285 /// replaceAllUsesWith - Replace all uses of debug info referenced by
286 /// this descriptor.
replaceAllUsesWith(DIDescriptor & D)287 void DIType::replaceAllUsesWith(DIDescriptor &D) {
288   if (!DbgNode)
289     return;
290 
291   // Since we use a TrackingVH for the node, its easy for clients to manufacture
292   // legitimate situations where they want to replaceAllUsesWith() on something
293   // which, due to uniquing, has merged with the source. We shield clients from
294   // this detail by allowing a value to be replaced with replaceAllUsesWith()
295   // itself.
296   if (DbgNode != D) {
297     MDNode *Node = const_cast<MDNode*>(DbgNode);
298     const MDNode *DN = D;
299     const Value *V = cast_or_null<Value>(DN);
300     Node->replaceAllUsesWith(const_cast<Value*>(V));
301     MDNode::deleteTemporary(Node);
302   }
303 }
304 
305 /// replaceAllUsesWith - Replace all uses of debug info referenced by
306 /// this descriptor.
replaceAllUsesWith(MDNode * D)307 void DIType::replaceAllUsesWith(MDNode *D) {
308   if (!DbgNode)
309     return;
310 
311   // Since we use a TrackingVH for the node, its easy for clients to manufacture
312   // legitimate situations where they want to replaceAllUsesWith() on something
313   // which, due to uniquing, has merged with the source. We shield clients from
314   // this detail by allowing a value to be replaced with replaceAllUsesWith()
315   // itself.
316   if (DbgNode != D) {
317     MDNode *Node = const_cast<MDNode*>(DbgNode);
318     const MDNode *DN = D;
319     const Value *V = cast_or_null<Value>(DN);
320     Node->replaceAllUsesWith(const_cast<Value*>(V));
321     MDNode::deleteTemporary(Node);
322   }
323 }
324 
325 /// Verify - Verify that a compile unit is well formed.
Verify() const326 bool DICompileUnit::Verify() const {
327   if (!DbgNode)
328     return false;
329   StringRef N = getFilename();
330   if (N.empty())
331     return false;
332   // It is possible that directory and produce string is empty.
333   return true;
334 }
335 
336 /// Verify - Verify that a type descriptor is well formed.
Verify() const337 bool DIType::Verify() const {
338   if (!DbgNode)
339     return false;
340   if (!getContext().Verify())
341     return false;
342   unsigned Tag = getTag();
343   if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
344       Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
345       Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
346       && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
347       && Tag != dwarf::DW_TAG_enumeration_type
348       && getFilename().empty())
349     return false;
350   return true;
351 }
352 
353 /// Verify - Verify that a basic type descriptor is well formed.
Verify() const354 bool DIBasicType::Verify() const {
355   return isBasicType();
356 }
357 
358 /// Verify - Verify that a derived type descriptor is well formed.
Verify() const359 bool DIDerivedType::Verify() const {
360   return isDerivedType();
361 }
362 
363 /// Verify - Verify that a composite type descriptor is well formed.
Verify() const364 bool DICompositeType::Verify() const {
365   if (!DbgNode)
366     return false;
367   if (!getContext().Verify())
368     return false;
369 
370   DICompileUnit CU = getCompileUnit();
371   if (!CU.Verify())
372     return false;
373   return true;
374 }
375 
376 /// Verify - Verify that a subprogram descriptor is well formed.
Verify() const377 bool DISubprogram::Verify() const {
378   if (!DbgNode)
379     return false;
380 
381   if (!getContext().Verify())
382     return false;
383 
384   DICompileUnit CU = getCompileUnit();
385   if (!CU.Verify())
386     return false;
387 
388   DICompositeType Ty = getType();
389   if (!Ty.Verify())
390     return false;
391   return true;
392 }
393 
394 /// Verify - Verify that a global variable descriptor is well formed.
Verify() const395 bool DIGlobalVariable::Verify() const {
396   if (!DbgNode)
397     return false;
398 
399   if (getDisplayName().empty())
400     return false;
401 
402   if (!getContext().Verify())
403     return false;
404 
405   DICompileUnit CU = getCompileUnit();
406   if (!CU.Verify())
407     return false;
408 
409   DIType Ty = getType();
410   if (!Ty.Verify())
411     return false;
412 
413   if (!getGlobal() && !getConstant())
414     return false;
415 
416   return true;
417 }
418 
419 /// Verify - Verify that a variable descriptor is well formed.
Verify() const420 bool DIVariable::Verify() const {
421   if (!DbgNode)
422     return false;
423 
424   if (!getContext().Verify())
425     return false;
426 
427   if (!getCompileUnit().Verify())
428     return false;
429 
430   DIType Ty = getType();
431   if (!Ty.Verify())
432     return false;
433 
434   return true;
435 }
436 
437 /// Verify - Verify that a location descriptor is well formed.
Verify() const438 bool DILocation::Verify() const {
439   if (!DbgNode)
440     return false;
441 
442   return DbgNode->getNumOperands() == 4;
443 }
444 
445 /// Verify - Verify that a namespace descriptor is well formed.
Verify() const446 bool DINameSpace::Verify() const {
447   if (!DbgNode)
448     return false;
449   if (getName().empty())
450     return false;
451   if (!getCompileUnit().Verify())
452     return false;
453   return true;
454 }
455 
456 /// getOriginalTypeSize - If this type is derived from a base type then
457 /// return base type size.
getOriginalTypeSize() const458 uint64_t DIDerivedType::getOriginalTypeSize() const {
459   unsigned Tag = getTag();
460   if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
461       Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
462       Tag == dwarf::DW_TAG_restrict_type) {
463     DIType BaseType = getTypeDerivedFrom();
464     // If this type is not derived from any type then take conservative
465     // approach.
466     if (!BaseType.isValid())
467       return getSizeInBits();
468     if (BaseType.isDerivedType())
469       return DIDerivedType(BaseType).getOriginalTypeSize();
470     else
471       return BaseType.getSizeInBits();
472   }
473 
474   return getSizeInBits();
475 }
476 
477 /// isInlinedFnArgument - Return true if this variable provides debugging
478 /// information for an inlined function arguments.
isInlinedFnArgument(const Function * CurFn)479 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
480   assert(CurFn && "Invalid function");
481   if (!getContext().isSubprogram())
482     return false;
483   // This variable is not inlined function argument if its scope
484   // does not describe current function.
485   return !(DISubprogram(getContext()).describes(CurFn));
486 }
487 
488 /// describes - Return true if this subprogram provides debugging
489 /// information for the function F.
describes(const Function * F)490 bool DISubprogram::describes(const Function *F) {
491   assert(F && "Invalid function");
492   if (F == getFunction())
493     return true;
494   StringRef Name = getLinkageName();
495   if (Name.empty())
496     Name = getName();
497   if (F->getName() == Name)
498     return true;
499   return false;
500 }
501 
isOptimized() const502 unsigned DISubprogram::isOptimized() const {
503   assert (DbgNode && "Invalid subprogram descriptor!");
504   if (DbgNode->getNumOperands() == 16)
505     return getUnsignedField(15);
506   return 0;
507 }
508 
getFilename() const509 StringRef DIScope::getFilename() const {
510   if (!DbgNode)
511     return StringRef();
512   if (isLexicalBlock())
513     return DILexicalBlock(DbgNode).getFilename();
514   if (isSubprogram())
515     return DISubprogram(DbgNode).getFilename();
516   if (isCompileUnit())
517     return DICompileUnit(DbgNode).getFilename();
518   if (isNameSpace())
519     return DINameSpace(DbgNode).getFilename();
520   if (isType())
521     return DIType(DbgNode).getFilename();
522   if (isFile())
523     return DIFile(DbgNode).getFilename();
524   assert(0 && "Invalid DIScope!");
525   return StringRef();
526 }
527 
getDirectory() const528 StringRef DIScope::getDirectory() const {
529   if (!DbgNode)
530     return StringRef();
531   if (isLexicalBlock())
532     return DILexicalBlock(DbgNode).getDirectory();
533   if (isSubprogram())
534     return DISubprogram(DbgNode).getDirectory();
535   if (isCompileUnit())
536     return DICompileUnit(DbgNode).getDirectory();
537   if (isNameSpace())
538     return DINameSpace(DbgNode).getDirectory();
539   if (isType())
540     return DIType(DbgNode).getDirectory();
541   if (isFile())
542     return DIFile(DbgNode).getDirectory();
543   assert(0 && "Invalid DIScope!");
544   return StringRef();
545 }
546 
547 //===----------------------------------------------------------------------===//
548 // DIDescriptor: dump routines for all descriptors.
549 //===----------------------------------------------------------------------===//
550 
551 
552 /// print - Print descriptor.
print(raw_ostream & OS) const553 void DIDescriptor::print(raw_ostream &OS) const {
554   OS << "[" << dwarf::TagString(getTag()) << "] ";
555   OS.write_hex((intptr_t) &*DbgNode) << ']';
556 }
557 
558 /// print - Print compile unit.
print(raw_ostream & OS) const559 void DICompileUnit::print(raw_ostream &OS) const {
560   if (getLanguage())
561     OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
562 
563   OS << " [" << getDirectory() << "/" << getFilename() << "]";
564 }
565 
566 /// print - Print type.
print(raw_ostream & OS) const567 void DIType::print(raw_ostream &OS) const {
568   if (!DbgNode) return;
569 
570   StringRef Res = getName();
571   if (!Res.empty())
572     OS << " [" << Res << "] ";
573 
574   unsigned Tag = getTag();
575   OS << " [" << dwarf::TagString(Tag) << "] ";
576 
577   // TODO : Print context
578   getCompileUnit().print(OS);
579   OS << " ["
580          << "line " << getLineNumber() << ", "
581          << getSizeInBits() << " bits, "
582          << getAlignInBits() << " bit alignment, "
583          << getOffsetInBits() << " bit offset"
584          << "] ";
585 
586   if (isPrivate())
587     OS << " [private] ";
588   else if (isProtected())
589     OS << " [protected] ";
590 
591   if (isForwardDecl())
592     OS << " [fwd] ";
593 
594   if (isBasicType())
595     DIBasicType(DbgNode).print(OS);
596   else if (isDerivedType())
597     DIDerivedType(DbgNode).print(OS);
598   else if (isCompositeType())
599     DICompositeType(DbgNode).print(OS);
600   else {
601     OS << "Invalid DIType\n";
602     return;
603   }
604 
605   OS << "\n";
606 }
607 
608 /// print - Print basic type.
print(raw_ostream & OS) const609 void DIBasicType::print(raw_ostream &OS) const {
610   OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
611 }
612 
613 /// print - Print derived type.
print(raw_ostream & OS) const614 void DIDerivedType::print(raw_ostream &OS) const {
615   OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
616 }
617 
618 /// print - Print composite type.
print(raw_ostream & OS) const619 void DICompositeType::print(raw_ostream &OS) const {
620   DIArray A = getTypeArray();
621   OS << " [" << A.getNumElements() << " elements]";
622 }
623 
624 /// print - Print subprogram.
print(raw_ostream & OS) const625 void DISubprogram::print(raw_ostream &OS) const {
626   StringRef Res = getName();
627   if (!Res.empty())
628     OS << " [" << Res << "] ";
629 
630   unsigned Tag = getTag();
631   OS << " [" << dwarf::TagString(Tag) << "] ";
632 
633   // TODO : Print context
634   getCompileUnit().print(OS);
635   OS << " [" << getLineNumber() << "] ";
636 
637   if (isLocalToUnit())
638     OS << " [local] ";
639 
640   if (isDefinition())
641     OS << " [def] ";
642 
643   OS << "\n";
644 }
645 
646 /// print - Print global variable.
print(raw_ostream & OS) const647 void DIGlobalVariable::print(raw_ostream &OS) const {
648   OS << " [";
649   StringRef Res = getName();
650   if (!Res.empty())
651     OS << " [" << Res << "] ";
652 
653   unsigned Tag = getTag();
654   OS << " [" << dwarf::TagString(Tag) << "] ";
655 
656   // TODO : Print context
657   getCompileUnit().print(OS);
658   OS << " [" << getLineNumber() << "] ";
659 
660   if (isLocalToUnit())
661     OS << " [local] ";
662 
663   if (isDefinition())
664     OS << " [def] ";
665 
666   if (isGlobalVariable())
667     DIGlobalVariable(DbgNode).print(OS);
668   OS << "]\n";
669 }
670 
671 /// print - Print variable.
print(raw_ostream & OS) const672 void DIVariable::print(raw_ostream &OS) const {
673   StringRef Res = getName();
674   if (!Res.empty())
675     OS << " [" << Res << "] ";
676 
677   getCompileUnit().print(OS);
678   OS << " [" << getLineNumber() << "] ";
679   getType().print(OS);
680   OS << "\n";
681 
682   // FIXME: Dump complex addresses
683 }
684 
685 /// dump - Print descriptor to dbgs() with a newline.
dump() const686 void DIDescriptor::dump() const {
687   print(dbgs()); dbgs() << '\n';
688 }
689 
690 /// dump - Print compile unit to dbgs() with a newline.
dump() const691 void DICompileUnit::dump() const {
692   print(dbgs()); dbgs() << '\n';
693 }
694 
695 /// dump - Print type to dbgs() with a newline.
dump() const696 void DIType::dump() const {
697   print(dbgs()); dbgs() << '\n';
698 }
699 
700 /// dump - Print basic type to dbgs() with a newline.
dump() const701 void DIBasicType::dump() const {
702   print(dbgs()); dbgs() << '\n';
703 }
704 
705 /// dump - Print derived type to dbgs() with a newline.
dump() const706 void DIDerivedType::dump() const {
707   print(dbgs()); dbgs() << '\n';
708 }
709 
710 /// dump - Print composite type to dbgs() with a newline.
dump() const711 void DICompositeType::dump() const {
712   print(dbgs()); dbgs() << '\n';
713 }
714 
715 /// dump - Print subprogram to dbgs() with a newline.
dump() const716 void DISubprogram::dump() const {
717   print(dbgs()); dbgs() << '\n';
718 }
719 
720 /// dump - Print global variable.
dump() const721 void DIGlobalVariable::dump() const {
722   print(dbgs()); dbgs() << '\n';
723 }
724 
725 /// dump - Print variable.
dump() const726 void DIVariable::dump() const {
727   print(dbgs()); dbgs() << '\n';
728 }
729 
730 /// fixupObjcLikeName - Replace contains special characters used
731 /// in a typical Objective-C names with '.' in a given string.
fixupObjcLikeName(StringRef Str,SmallVectorImpl<char> & Out)732 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
733   bool isObjCLike = false;
734   for (size_t i = 0, e = Str.size(); i < e; ++i) {
735     char C = Str[i];
736     if (C == '[')
737       isObjCLike = true;
738 
739     if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
740                        C == '+' || C == '(' || C == ')'))
741       Out.push_back('.');
742     else
743       Out.push_back(C);
744   }
745 }
746 
747 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
748 /// suitable to hold function specific information.
getFnSpecificMDNode(const Module & M,StringRef FuncName)749 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
750   SmallString<32> Name = StringRef("llvm.dbg.lv.");
751   fixupObjcLikeName(FuncName, Name);
752 
753   return M.getNamedMetadata(Name.str());
754 }
755 
756 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
757 /// to hold function specific information.
getOrInsertFnSpecificMDNode(Module & M,StringRef FuncName)758 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
759   SmallString<32> Name = StringRef("llvm.dbg.lv.");
760   fixupObjcLikeName(FuncName, Name);
761 
762   return M.getOrInsertNamedMetadata(Name.str());
763 }
764 
765 /// createInlinedVariable - Create a new inlined variable based on current
766 /// variable.
767 /// @param DV            Current Variable.
768 /// @param InlinedScope  Location at current variable is inlined.
createInlinedVariable(MDNode * DV,MDNode * InlinedScope,LLVMContext & VMContext)769 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
770                                        LLVMContext &VMContext) {
771   SmallVector<Value *, 16> Elts;
772   // Insert inlined scope as 7th element.
773   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
774     i == 7 ? Elts.push_back(InlinedScope) :
775              Elts.push_back(DV->getOperand(i));
776   return DIVariable(MDNode::get(VMContext, Elts));
777 }
778 
779 /// cleanseInlinedVariable - Remove inlined scope from the variable.
cleanseInlinedVariable(MDNode * DV,LLVMContext & VMContext)780 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
781   SmallVector<Value *, 16> Elts;
782   // Insert inlined scope as 7th element.
783   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
784     i == 7 ?
785       Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
786       Elts.push_back(DV->getOperand(i));
787   return DIVariable(MDNode::get(VMContext, Elts));
788 }
789 
790 //===----------------------------------------------------------------------===//
791 // DebugInfoFinder implementations.
792 //===----------------------------------------------------------------------===//
793 
794 /// processModule - Process entire module and collect debug info.
processModule(Module & M)795 void DebugInfoFinder::processModule(Module &M) {
796   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
797     for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
798       for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
799            ++BI) {
800         if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
801           processDeclare(DDI);
802 
803         DebugLoc Loc = BI->getDebugLoc();
804         if (Loc.isUnknown())
805           continue;
806 
807         LLVMContext &Ctx = BI->getContext();
808         DIDescriptor Scope(Loc.getScope(Ctx));
809 
810         if (Scope.isCompileUnit())
811           addCompileUnit(DICompileUnit(Scope));
812         else if (Scope.isSubprogram())
813           processSubprogram(DISubprogram(Scope));
814         else if (Scope.isLexicalBlock())
815           processLexicalBlock(DILexicalBlock(Scope));
816 
817         if (MDNode *IA = Loc.getInlinedAt(Ctx))
818           processLocation(DILocation(IA));
819       }
820 
821   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
822     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
823       DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
824       if (addGlobalVariable(DIG)) {
825         addCompileUnit(DIG.getCompileUnit());
826         processType(DIG.getType());
827       }
828     }
829   }
830 
831   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
832     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
833       processSubprogram(DISubprogram(NMD->getOperand(i)));
834 }
835 
836 /// processLocation - Process DILocation.
processLocation(DILocation Loc)837 void DebugInfoFinder::processLocation(DILocation Loc) {
838   if (!Loc.Verify()) return;
839   DIDescriptor S(Loc.getScope());
840   if (S.isCompileUnit())
841     addCompileUnit(DICompileUnit(S));
842   else if (S.isSubprogram())
843     processSubprogram(DISubprogram(S));
844   else if (S.isLexicalBlock())
845     processLexicalBlock(DILexicalBlock(S));
846   processLocation(Loc.getOrigLocation());
847 }
848 
849 /// processType - Process DIType.
processType(DIType DT)850 void DebugInfoFinder::processType(DIType DT) {
851   if (!addType(DT))
852     return;
853 
854   addCompileUnit(DT.getCompileUnit());
855   if (DT.isCompositeType()) {
856     DICompositeType DCT(DT);
857     processType(DCT.getTypeDerivedFrom());
858     DIArray DA = DCT.getTypeArray();
859     for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
860       DIDescriptor D = DA.getElement(i);
861       if (D.isType())
862         processType(DIType(D));
863       else if (D.isSubprogram())
864         processSubprogram(DISubprogram(D));
865     }
866   } else if (DT.isDerivedType()) {
867     DIDerivedType DDT(DT);
868     processType(DDT.getTypeDerivedFrom());
869   }
870 }
871 
872 /// processLexicalBlock
processLexicalBlock(DILexicalBlock LB)873 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
874   DIScope Context = LB.getContext();
875   if (Context.isLexicalBlock())
876     return processLexicalBlock(DILexicalBlock(Context));
877   else
878     return processSubprogram(DISubprogram(Context));
879 }
880 
881 /// processSubprogram - Process DISubprogram.
processSubprogram(DISubprogram SP)882 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
883   if (!addSubprogram(SP))
884     return;
885   addCompileUnit(SP.getCompileUnit());
886   processType(SP.getType());
887 }
888 
889 /// processDeclare - Process DbgDeclareInst.
processDeclare(DbgDeclareInst * DDI)890 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
891   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
892   if (!N) return;
893 
894   DIDescriptor DV(N);
895   if (!DV.isVariable())
896     return;
897 
898   if (!NodesSeen.insert(DV))
899     return;
900 
901   addCompileUnit(DIVariable(N).getCompileUnit());
902   processType(DIVariable(N).getType());
903 }
904 
905 /// addType - Add type into Tys.
addType(DIType DT)906 bool DebugInfoFinder::addType(DIType DT) {
907   if (!DT.isValid())
908     return false;
909 
910   if (!NodesSeen.insert(DT))
911     return false;
912 
913   TYs.push_back(DT);
914   return true;
915 }
916 
917 /// addCompileUnit - Add compile unit into CUs.
addCompileUnit(DICompileUnit CU)918 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
919   if (!CU.Verify())
920     return false;
921 
922   if (!NodesSeen.insert(CU))
923     return false;
924 
925   CUs.push_back(CU);
926   return true;
927 }
928 
929 /// addGlobalVariable - Add global variable into GVs.
addGlobalVariable(DIGlobalVariable DIG)930 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
931   if (!DIDescriptor(DIG).isGlobalVariable())
932     return false;
933 
934   if (!NodesSeen.insert(DIG))
935     return false;
936 
937   GVs.push_back(DIG);
938   return true;
939 }
940 
941 // addSubprogram - Add subprgoram into SPs.
addSubprogram(DISubprogram SP)942 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
943   if (!DIDescriptor(SP).isSubprogram())
944     return false;
945 
946   if (!NodesSeen.insert(SP))
947     return false;
948 
949   SPs.push_back(SP);
950   return true;
951 }
952 
953 /// getDISubprogram - Find subprogram that is enclosing this scope.
getDISubprogram(const MDNode * Scope)954 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
955   DIDescriptor D(Scope);
956   if (D.isSubprogram())
957     return DISubprogram(Scope);
958 
959   if (D.isLexicalBlock())
960     return getDISubprogram(DILexicalBlock(Scope).getContext());
961 
962   return DISubprogram();
963 }
964 
965 /// getDICompositeType - Find underlying composite type.
getDICompositeType(DIType T)966 DICompositeType llvm::getDICompositeType(DIType T) {
967   if (T.isCompositeType())
968     return DICompositeType(T);
969 
970   if (T.isDerivedType())
971     return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
972 
973   return DICompositeType();
974 }
975