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/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_or_null<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() <= LLVMDebugVersion8)
116 return DbgNode->getNumOperands()-6;
117 if (getVersion() == 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() <= 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_rvalue_reference_type:
154 case dwarf::DW_TAG_const_type:
155 case dwarf::DW_TAG_volatile_type:
156 case dwarf::DW_TAG_restrict_type:
157 case dwarf::DW_TAG_member:
158 case dwarf::DW_TAG_inheritance:
159 case dwarf::DW_TAG_friend:
160 return true;
161 default:
162 // CompositeTypes are currently modelled as DerivedTypes.
163 return isCompositeType();
164 }
165 }
166
167 /// isCompositeType - Return true if the specified tag is legal for
168 /// DICompositeType.
isCompositeType() const169 bool DIDescriptor::isCompositeType() const {
170 if (!DbgNode) return false;
171 switch (getTag()) {
172 case dwarf::DW_TAG_array_type:
173 case dwarf::DW_TAG_structure_type:
174 case dwarf::DW_TAG_union_type:
175 case dwarf::DW_TAG_enumeration_type:
176 case dwarf::DW_TAG_vector_type:
177 case dwarf::DW_TAG_subroutine_type:
178 case dwarf::DW_TAG_class_type:
179 return true;
180 default:
181 return false;
182 }
183 }
184
185 /// isVariable - Return true if the specified tag is legal for DIVariable.
isVariable() const186 bool DIDescriptor::isVariable() const {
187 if (!DbgNode) return false;
188 switch (getTag()) {
189 case dwarf::DW_TAG_auto_variable:
190 case dwarf::DW_TAG_arg_variable:
191 case dwarf::DW_TAG_return_variable:
192 return true;
193 default:
194 return false;
195 }
196 }
197
198 /// isType - Return true if the specified tag is legal for DIType.
isType() const199 bool DIDescriptor::isType() const {
200 return isBasicType() || isCompositeType() || isDerivedType();
201 }
202
203 /// isSubprogram - Return true if the specified tag is legal for
204 /// DISubprogram.
isSubprogram() const205 bool DIDescriptor::isSubprogram() const {
206 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
207 }
208
209 /// isGlobalVariable - Return true if the specified tag is legal for
210 /// DIGlobalVariable.
isGlobalVariable() const211 bool DIDescriptor::isGlobalVariable() const {
212 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
213 getTag() == dwarf::DW_TAG_constant);
214 }
215
216 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
isGlobal() const217 bool DIDescriptor::isGlobal() const {
218 return isGlobalVariable();
219 }
220
221 /// isUnspecifiedParmeter - Return true if the specified tag is
222 /// DW_TAG_unspecified_parameters.
isUnspecifiedParameter() const223 bool DIDescriptor::isUnspecifiedParameter() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
225 }
226
227 /// isScope - Return true if the specified tag is one of the scope
228 /// related tag.
isScope() const229 bool DIDescriptor::isScope() const {
230 if (!DbgNode) return false;
231 switch (getTag()) {
232 case dwarf::DW_TAG_compile_unit:
233 case dwarf::DW_TAG_lexical_block:
234 case dwarf::DW_TAG_subprogram:
235 case dwarf::DW_TAG_namespace:
236 return true;
237 default:
238 break;
239 }
240 return false;
241 }
242
243 /// isTemplateTypeParameter - Return true if the specified tag is
244 /// DW_TAG_template_type_parameter.
isTemplateTypeParameter() const245 bool DIDescriptor::isTemplateTypeParameter() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
247 }
248
249 /// isTemplateValueParameter - Return true if the specified tag is
250 /// DW_TAG_template_value_parameter.
isTemplateValueParameter() const251 bool DIDescriptor::isTemplateValueParameter() const {
252 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
253 }
254
255 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
isCompileUnit() const256 bool DIDescriptor::isCompileUnit() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
258 }
259
260 /// isFile - Return true if the specified tag is DW_TAG_file_type.
isFile() const261 bool DIDescriptor::isFile() const {
262 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
263 }
264
265 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
isNameSpace() const266 bool DIDescriptor::isNameSpace() const {
267 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
268 }
269
270 /// isLexicalBlockFile - Return true if the specified descriptor is a
271 /// lexical block with an extra file.
isLexicalBlockFile() const272 bool DIDescriptor::isLexicalBlockFile() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
274 (DbgNode->getNumOperands() == 3);
275 }
276
277 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
isLexicalBlock() const278 bool DIDescriptor::isLexicalBlock() const {
279 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
280 (DbgNode->getNumOperands() > 3);
281 }
282
283 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
isSubrange() const284 bool DIDescriptor::isSubrange() const {
285 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
286 }
287
288 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
isEnumerator() const289 bool DIDescriptor::isEnumerator() const {
290 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
291 }
292
293 /// isObjCProperty - Return true if the specified tag is DW_TAG
isObjCProperty() const294 bool DIDescriptor::isObjCProperty() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
296 }
297 //===----------------------------------------------------------------------===//
298 // Simple Descriptor Constructors and other Methods
299 //===----------------------------------------------------------------------===//
300
DIType(const MDNode * N)301 DIType::DIType(const MDNode *N) : DIScope(N) {
302 if (!N) return;
303 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
304 DbgNode = 0;
305 }
306 }
307
getNumElements() const308 unsigned DIArray::getNumElements() const {
309 if (!DbgNode)
310 return 0;
311 return DbgNode->getNumOperands();
312 }
313
314 /// replaceAllUsesWith - Replace all uses of debug info referenced by
315 /// this descriptor.
replaceAllUsesWith(DIDescriptor & D)316 void DIType::replaceAllUsesWith(DIDescriptor &D) {
317 if (!DbgNode)
318 return;
319
320 // Since we use a TrackingVH for the node, its easy for clients to manufacture
321 // legitimate situations where they want to replaceAllUsesWith() on something
322 // which, due to uniquing, has merged with the source. We shield clients from
323 // this detail by allowing a value to be replaced with replaceAllUsesWith()
324 // itself.
325 if (DbgNode != D) {
326 MDNode *Node = const_cast<MDNode*>(DbgNode);
327 const MDNode *DN = D;
328 const Value *V = cast_or_null<Value>(DN);
329 Node->replaceAllUsesWith(const_cast<Value*>(V));
330 MDNode::deleteTemporary(Node);
331 }
332 }
333
334 /// replaceAllUsesWith - Replace all uses of debug info referenced by
335 /// this descriptor.
replaceAllUsesWith(MDNode * D)336 void DIType::replaceAllUsesWith(MDNode *D) {
337 if (!DbgNode)
338 return;
339
340 // Since we use a TrackingVH for the node, its easy for clients to manufacture
341 // legitimate situations where they want to replaceAllUsesWith() on something
342 // which, due to uniquing, has merged with the source. We shield clients from
343 // this detail by allowing a value to be replaced with replaceAllUsesWith()
344 // itself.
345 if (DbgNode != D) {
346 MDNode *Node = const_cast<MDNode*>(DbgNode);
347 const MDNode *DN = D;
348 const Value *V = cast_or_null<Value>(DN);
349 Node->replaceAllUsesWith(const_cast<Value*>(V));
350 MDNode::deleteTemporary(Node);
351 }
352 }
353
354 /// isUnsignedDIType - Return true if type encoding is unsigned.
isUnsignedDIType()355 bool DIType::isUnsignedDIType() {
356 DIDerivedType DTy(DbgNode);
357 if (DTy.Verify())
358 return DTy.getTypeDerivedFrom().isUnsignedDIType();
359
360 DIBasicType BTy(DbgNode);
361 if (BTy.Verify()) {
362 unsigned Encoding = BTy.getEncoding();
363 if (Encoding == dwarf::DW_ATE_unsigned ||
364 Encoding == dwarf::DW_ATE_unsigned_char)
365 return true;
366 }
367 return false;
368 }
369
370 /// Verify - Verify that a compile unit is well formed.
Verify() const371 bool DICompileUnit::Verify() const {
372 if (!DbgNode)
373 return false;
374 StringRef N = getFilename();
375 if (N.empty())
376 return false;
377 // It is possible that directory and produce string is empty.
378 return true;
379 }
380
381 /// Verify - Verify that an ObjC property is well formed.
Verify() const382 bool DIObjCProperty::Verify() const {
383 if (!DbgNode)
384 return false;
385 unsigned Tag = getTag();
386 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
387 DIType Ty = getType();
388 if (!Ty.Verify()) return false;
389
390 // Don't worry about the rest of the strings for now.
391 return true;
392 }
393
394 /// Verify - Verify that a type descriptor is well formed.
Verify() const395 bool DIType::Verify() const {
396 if (!DbgNode)
397 return false;
398 if (getContext() && !getContext().Verify())
399 return false;
400 unsigned Tag = getTag();
401 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
402 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
403 Tag != dwarf::DW_TAG_reference_type &&
404 Tag != dwarf::DW_TAG_rvalue_reference_type &&
405 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_vector_type &&
406 Tag != dwarf::DW_TAG_array_type &&
407 Tag != dwarf::DW_TAG_enumeration_type &&
408 Tag != dwarf::DW_TAG_subroutine_type &&
409 getFilename().empty())
410 return false;
411 return true;
412 }
413
414 /// Verify - Verify that a basic type descriptor is well formed.
Verify() const415 bool DIBasicType::Verify() const {
416 return isBasicType();
417 }
418
419 /// Verify - Verify that a derived type descriptor is well formed.
Verify() const420 bool DIDerivedType::Verify() const {
421 return isDerivedType();
422 }
423
424 /// Verify - Verify that a composite type descriptor is well formed.
Verify() const425 bool DICompositeType::Verify() const {
426 if (!DbgNode)
427 return false;
428 if (getContext() && !getContext().Verify())
429 return false;
430
431 return true;
432 }
433
434 /// Verify - Verify that a subprogram descriptor is well formed.
Verify() const435 bool DISubprogram::Verify() const {
436 if (!DbgNode)
437 return false;
438
439 if (getContext() && !getContext().Verify())
440 return false;
441
442 DICompositeType Ty = getType();
443 if (!Ty.Verify())
444 return false;
445 return true;
446 }
447
448 /// Verify - Verify that a global variable descriptor is well formed.
Verify() const449 bool DIGlobalVariable::Verify() const {
450 if (!DbgNode)
451 return false;
452
453 if (getDisplayName().empty())
454 return false;
455
456 if (getContext() && !getContext().Verify())
457 return false;
458
459 DIType Ty = getType();
460 if (!Ty.Verify())
461 return false;
462
463 if (!getGlobal() && !getConstant())
464 return false;
465
466 return true;
467 }
468
469 /// Verify - Verify that a variable descriptor is well formed.
Verify() const470 bool DIVariable::Verify() const {
471 if (!DbgNode)
472 return false;
473
474 if (getContext() && !getContext().Verify())
475 return false;
476
477 DIType Ty = getType();
478 if (!Ty.Verify())
479 return false;
480
481 return true;
482 }
483
484 /// Verify - Verify that a location descriptor is well formed.
Verify() const485 bool DILocation::Verify() const {
486 if (!DbgNode)
487 return false;
488
489 return DbgNode->getNumOperands() == 4;
490 }
491
492 /// Verify - Verify that a namespace descriptor is well formed.
Verify() const493 bool DINameSpace::Verify() const {
494 if (!DbgNode)
495 return false;
496 if (getName().empty())
497 return false;
498 return true;
499 }
500
501 /// getOriginalTypeSize - If this type is derived from a base type then
502 /// return base type size.
getOriginalTypeSize() const503 uint64_t DIDerivedType::getOriginalTypeSize() const {
504 unsigned Tag = getTag();
505
506 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
507 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
508 Tag != dwarf::DW_TAG_restrict_type)
509 return getSizeInBits();
510
511 DIType BaseType = getTypeDerivedFrom();
512
513 // If this type is not derived from any type then take conservative approach.
514 if (!BaseType.isValid())
515 return getSizeInBits();
516
517 // If this is a derived type, go ahead and get the base type, unless it's a
518 // reference then it's just the size of the field. Pointer types have no need
519 // of this since they're a different type of qualification on the type.
520 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
521 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
522 return getSizeInBits();
523
524 if (BaseType.isDerivedType())
525 return DIDerivedType(BaseType).getOriginalTypeSize();
526
527 return BaseType.getSizeInBits();
528 }
529
530 /// getObjCProperty - Return property node, if this ivar is associated with one.
getObjCProperty() const531 MDNode *DIDerivedType::getObjCProperty() const {
532 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
533 return NULL;
534 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
535 }
536
537 /// isInlinedFnArgument - Return true if this variable provides debugging
538 /// information for an inlined function arguments.
isInlinedFnArgument(const Function * CurFn)539 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
540 assert(CurFn && "Invalid function");
541 if (!getContext().isSubprogram())
542 return false;
543 // This variable is not inlined function argument if its scope
544 // does not describe current function.
545 return !DISubprogram(getContext()).describes(CurFn);
546 }
547
548 /// describes - Return true if this subprogram provides debugging
549 /// information for the function F.
describes(const Function * F)550 bool DISubprogram::describes(const Function *F) {
551 assert(F && "Invalid function");
552 if (F == getFunction())
553 return true;
554 StringRef Name = getLinkageName();
555 if (Name.empty())
556 Name = getName();
557 if (F->getName() == Name)
558 return true;
559 return false;
560 }
561
isOptimized() const562 unsigned DISubprogram::isOptimized() const {
563 assert (DbgNode && "Invalid subprogram descriptor!");
564 if (DbgNode->getNumOperands() == 16)
565 return getUnsignedField(15);
566 return 0;
567 }
568
getVariablesNodes() const569 MDNode *DISubprogram::getVariablesNodes() const {
570 if (!DbgNode || DbgNode->getNumOperands() <= 19)
571 return NULL;
572 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
573 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
574 return NULL;
575 }
576
getVariables() const577 DIArray DISubprogram::getVariables() const {
578 if (!DbgNode || DbgNode->getNumOperands() <= 19)
579 return DIArray();
580 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
581 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
582 return DIArray(A);
583 return DIArray();
584 }
585
getFilename() const586 StringRef DIScope::getFilename() const {
587 if (!DbgNode)
588 return StringRef();
589 if (isLexicalBlockFile())
590 return DILexicalBlockFile(DbgNode).getFilename();
591 if (isLexicalBlock())
592 return DILexicalBlock(DbgNode).getFilename();
593 if (isSubprogram())
594 return DISubprogram(DbgNode).getFilename();
595 if (isCompileUnit())
596 return DICompileUnit(DbgNode).getFilename();
597 if (isNameSpace())
598 return DINameSpace(DbgNode).getFilename();
599 if (isType())
600 return DIType(DbgNode).getFilename();
601 if (isFile())
602 return DIFile(DbgNode).getFilename();
603 llvm_unreachable("Invalid DIScope!");
604 }
605
getDirectory() const606 StringRef DIScope::getDirectory() const {
607 if (!DbgNode)
608 return StringRef();
609 if (isLexicalBlockFile())
610 return DILexicalBlockFile(DbgNode).getDirectory();
611 if (isLexicalBlock())
612 return DILexicalBlock(DbgNode).getDirectory();
613 if (isSubprogram())
614 return DISubprogram(DbgNode).getDirectory();
615 if (isCompileUnit())
616 return DICompileUnit(DbgNode).getDirectory();
617 if (isNameSpace())
618 return DINameSpace(DbgNode).getDirectory();
619 if (isType())
620 return DIType(DbgNode).getDirectory();
621 if (isFile())
622 return DIFile(DbgNode).getDirectory();
623 llvm_unreachable("Invalid DIScope!");
624 }
625
getEnumTypes() const626 DIArray DICompileUnit::getEnumTypes() const {
627 if (!DbgNode || DbgNode->getNumOperands() < 14)
628 return DIArray();
629
630 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
631 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
632 return DIArray(A);
633 return DIArray();
634 }
635
getRetainedTypes() const636 DIArray DICompileUnit::getRetainedTypes() const {
637 if (!DbgNode || DbgNode->getNumOperands() < 14)
638 return DIArray();
639
640 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
641 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
642 return DIArray(A);
643 return DIArray();
644 }
645
getSubprograms() const646 DIArray DICompileUnit::getSubprograms() const {
647 if (!DbgNode || DbgNode->getNumOperands() < 14)
648 return DIArray();
649
650 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
651 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
652 return DIArray(A);
653 return DIArray();
654 }
655
656
getGlobalVariables() const657 DIArray DICompileUnit::getGlobalVariables() const {
658 if (!DbgNode || DbgNode->getNumOperands() < 14)
659 return DIArray();
660
661 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
662 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
663 return DIArray(A);
664 return DIArray();
665 }
666
667 /// fixupObjcLikeName - Replace contains special characters used
668 /// in a typical Objective-C names with '.' in a given string.
fixupObjcLikeName(StringRef Str,SmallVectorImpl<char> & Out)669 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
670 bool isObjCLike = false;
671 for (size_t i = 0, e = Str.size(); i < e; ++i) {
672 char C = Str[i];
673 if (C == '[')
674 isObjCLike = true;
675
676 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
677 C == '+' || C == '(' || C == ')'))
678 Out.push_back('.');
679 else
680 Out.push_back(C);
681 }
682 }
683
684 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
685 /// suitable to hold function specific information.
getFnSpecificMDNode(const Module & M,DISubprogram Fn)686 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
687 SmallString<32> Name = StringRef("llvm.dbg.lv.");
688 StringRef FName = "fn";
689 if (Fn.getFunction())
690 FName = Fn.getFunction()->getName();
691 else
692 FName = Fn.getName();
693 char One = '\1';
694 if (FName.startswith(StringRef(&One, 1)))
695 FName = FName.substr(1);
696 fixupObjcLikeName(FName, Name);
697 return M.getNamedMetadata(Name.str());
698 }
699
700 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
701 /// to hold function specific information.
getOrInsertFnSpecificMDNode(Module & M,DISubprogram Fn)702 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
703 SmallString<32> Name = StringRef("llvm.dbg.lv.");
704 StringRef FName = "fn";
705 if (Fn.getFunction())
706 FName = Fn.getFunction()->getName();
707 else
708 FName = Fn.getName();
709 char One = '\1';
710 if (FName.startswith(StringRef(&One, 1)))
711 FName = FName.substr(1);
712 fixupObjcLikeName(FName, Name);
713
714 return M.getOrInsertNamedMetadata(Name.str());
715 }
716
717 /// createInlinedVariable - Create a new inlined variable based on current
718 /// variable.
719 /// @param DV Current Variable.
720 /// @param InlinedScope Location at current variable is inlined.
createInlinedVariable(MDNode * DV,MDNode * InlinedScope,LLVMContext & VMContext)721 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
722 LLVMContext &VMContext) {
723 SmallVector<Value *, 16> Elts;
724 // Insert inlined scope as 7th element.
725 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
726 i == 7 ? Elts.push_back(InlinedScope) :
727 Elts.push_back(DV->getOperand(i));
728 return DIVariable(MDNode::get(VMContext, Elts));
729 }
730
731 /// cleanseInlinedVariable - Remove inlined scope from the variable.
cleanseInlinedVariable(MDNode * DV,LLVMContext & VMContext)732 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
733 SmallVector<Value *, 16> Elts;
734 // Insert inlined scope as 7th element.
735 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
736 i == 7 ?
737 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
738 Elts.push_back(DV->getOperand(i));
739 return DIVariable(MDNode::get(VMContext, Elts));
740 }
741
742 /// getDISubprogram - Find subprogram that is enclosing this scope.
getDISubprogram(const MDNode * Scope)743 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
744 DIDescriptor D(Scope);
745 if (D.isSubprogram())
746 return DISubprogram(Scope);
747
748 if (D.isLexicalBlockFile())
749 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
750
751 if (D.isLexicalBlock())
752 return getDISubprogram(DILexicalBlock(Scope).getContext());
753
754 return DISubprogram();
755 }
756
757 /// getDICompositeType - Find underlying composite type.
getDICompositeType(DIType T)758 DICompositeType llvm::getDICompositeType(DIType T) {
759 if (T.isCompositeType())
760 return DICompositeType(T);
761
762 if (T.isDerivedType())
763 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
764
765 return DICompositeType();
766 }
767
768 /// isSubprogramContext - Return true if Context is either a subprogram
769 /// or another context nested inside a subprogram.
isSubprogramContext(const MDNode * Context)770 bool llvm::isSubprogramContext(const MDNode *Context) {
771 if (!Context)
772 return false;
773 DIDescriptor D(Context);
774 if (D.isSubprogram())
775 return true;
776 if (D.isType())
777 return isSubprogramContext(DIType(Context).getContext());
778 return false;
779 }
780
781 //===----------------------------------------------------------------------===//
782 // DebugInfoFinder implementations.
783 //===----------------------------------------------------------------------===//
784
785 /// processModule - Process entire module and collect debug info.
processModule(Module & M)786 void DebugInfoFinder::processModule(Module &M) {
787 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
788 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
789 DICompileUnit CU(CU_Nodes->getOperand(i));
790 addCompileUnit(CU);
791 if (CU.getVersion() > LLVMDebugVersion10) {
792 DIArray GVs = CU.getGlobalVariables();
793 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
794 DIGlobalVariable DIG(GVs.getElement(i));
795 if (addGlobalVariable(DIG))
796 processType(DIG.getType());
797 }
798 DIArray SPs = CU.getSubprograms();
799 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
800 processSubprogram(DISubprogram(SPs.getElement(i)));
801 DIArray EnumTypes = CU.getEnumTypes();
802 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
803 processType(DIType(EnumTypes.getElement(i)));
804 DIArray RetainedTypes = CU.getRetainedTypes();
805 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
806 processType(DIType(RetainedTypes.getElement(i)));
807 return;
808 }
809 }
810 }
811
812 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
813 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
814 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
815 ++BI) {
816 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
817 processDeclare(DDI);
818
819 DebugLoc Loc = BI->getDebugLoc();
820 if (Loc.isUnknown())
821 continue;
822
823 LLVMContext &Ctx = BI->getContext();
824 DIDescriptor Scope(Loc.getScope(Ctx));
825
826 if (Scope.isCompileUnit())
827 addCompileUnit(DICompileUnit(Scope));
828 else if (Scope.isSubprogram())
829 processSubprogram(DISubprogram(Scope));
830 else if (Scope.isLexicalBlockFile()) {
831 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
832 processLexicalBlock(DILexicalBlock(DBF.getScope()));
833 }
834 else if (Scope.isLexicalBlock())
835 processLexicalBlock(DILexicalBlock(Scope));
836
837 if (MDNode *IA = Loc.getInlinedAt(Ctx))
838 processLocation(DILocation(IA));
839 }
840
841 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
842 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
843 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
844 if (addGlobalVariable(DIG)) {
845 if (DIG.getVersion() <= LLVMDebugVersion10)
846 addCompileUnit(DIG.getCompileUnit());
847 processType(DIG.getType());
848 }
849 }
850 }
851
852 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
853 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
854 processSubprogram(DISubprogram(NMD->getOperand(i)));
855 }
856
857 /// processLocation - Process DILocation.
processLocation(DILocation Loc)858 void DebugInfoFinder::processLocation(DILocation Loc) {
859 if (!Loc.Verify()) return;
860 DIDescriptor S(Loc.getScope());
861 if (S.isCompileUnit())
862 addCompileUnit(DICompileUnit(S));
863 else if (S.isSubprogram())
864 processSubprogram(DISubprogram(S));
865 else if (S.isLexicalBlock())
866 processLexicalBlock(DILexicalBlock(S));
867 else if (S.isLexicalBlockFile()) {
868 DILexicalBlockFile DBF = DILexicalBlockFile(S);
869 processLexicalBlock(DILexicalBlock(DBF.getScope()));
870 }
871 processLocation(Loc.getOrigLocation());
872 }
873
874 /// processType - Process DIType.
processType(DIType DT)875 void DebugInfoFinder::processType(DIType DT) {
876 if (!addType(DT))
877 return;
878 if (DT.getVersion() <= LLVMDebugVersion10)
879 addCompileUnit(DT.getCompileUnit());
880 if (DT.isCompositeType()) {
881 DICompositeType DCT(DT);
882 processType(DCT.getTypeDerivedFrom());
883 DIArray DA = DCT.getTypeArray();
884 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
885 DIDescriptor D = DA.getElement(i);
886 if (D.isType())
887 processType(DIType(D));
888 else if (D.isSubprogram())
889 processSubprogram(DISubprogram(D));
890 }
891 } else if (DT.isDerivedType()) {
892 DIDerivedType DDT(DT);
893 processType(DDT.getTypeDerivedFrom());
894 }
895 }
896
897 /// processLexicalBlock
processLexicalBlock(DILexicalBlock LB)898 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
899 DIScope Context = LB.getContext();
900 if (Context.isLexicalBlock())
901 return processLexicalBlock(DILexicalBlock(Context));
902 else if (Context.isLexicalBlockFile()) {
903 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
904 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
905 }
906 else
907 return processSubprogram(DISubprogram(Context));
908 }
909
910 /// processSubprogram - Process DISubprogram.
processSubprogram(DISubprogram SP)911 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
912 if (!addSubprogram(SP))
913 return;
914 if (SP.getVersion() <= LLVMDebugVersion10)
915 addCompileUnit(SP.getCompileUnit());
916 processType(SP.getType());
917 }
918
919 /// processDeclare - Process DbgDeclareInst.
processDeclare(DbgDeclareInst * DDI)920 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
921 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
922 if (!N) return;
923
924 DIDescriptor DV(N);
925 if (!DV.isVariable())
926 return;
927
928 if (!NodesSeen.insert(DV))
929 return;
930 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
931 addCompileUnit(DIVariable(N).getCompileUnit());
932 processType(DIVariable(N).getType());
933 }
934
935 /// addType - Add type into Tys.
addType(DIType DT)936 bool DebugInfoFinder::addType(DIType DT) {
937 if (!DT.isValid())
938 return false;
939
940 if (!NodesSeen.insert(DT))
941 return false;
942
943 TYs.push_back(DT);
944 return true;
945 }
946
947 /// addCompileUnit - Add compile unit into CUs.
addCompileUnit(DICompileUnit CU)948 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
949 if (!CU.Verify())
950 return false;
951
952 if (!NodesSeen.insert(CU))
953 return false;
954
955 CUs.push_back(CU);
956 return true;
957 }
958
959 /// addGlobalVariable - Add global variable into GVs.
addGlobalVariable(DIGlobalVariable DIG)960 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
961 if (!DIDescriptor(DIG).isGlobalVariable())
962 return false;
963
964 if (!NodesSeen.insert(DIG))
965 return false;
966
967 GVs.push_back(DIG);
968 return true;
969 }
970
971 // addSubprogram - Add subprgoram into SPs.
addSubprogram(DISubprogram SP)972 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
973 if (!DIDescriptor(SP).isSubprogram())
974 return false;
975
976 if (!NodesSeen.insert(SP))
977 return false;
978
979 SPs.push_back(SP);
980 return true;
981 }
982
983 //===----------------------------------------------------------------------===//
984 // DIDescriptor: dump routines for all descriptors.
985 //===----------------------------------------------------------------------===//
986
987 /// dump - Print descriptor to dbgs() with a newline.
dump() const988 void DIDescriptor::dump() const {
989 print(dbgs()); dbgs() << '\n';
990 }
991
992 /// print - Print descriptor.
print(raw_ostream & OS) const993 void DIDescriptor::print(raw_ostream &OS) const {
994 if (!DbgNode) return;
995
996 if (const char *Tag = dwarf::TagString(getTag()))
997 OS << "[ " << Tag << " ]";
998
999 if (this->isSubrange()) {
1000 DISubrange(DbgNode).printInternal(OS);
1001 } else if (this->isCompileUnit()) {
1002 DICompileUnit(DbgNode).printInternal(OS);
1003 } else if (this->isFile()) {
1004 DIFile(DbgNode).printInternal(OS);
1005 } else if (this->isEnumerator()) {
1006 DIEnumerator(DbgNode).printInternal(OS);
1007 } else if (this->isBasicType()) {
1008 DIType(DbgNode).printInternal(OS);
1009 } else if (this->isDerivedType()) {
1010 DIDerivedType(DbgNode).printInternal(OS);
1011 } else if (this->isCompositeType()) {
1012 DICompositeType(DbgNode).printInternal(OS);
1013 } else if (this->isSubprogram()) {
1014 DISubprogram(DbgNode).printInternal(OS);
1015 } else if (this->isGlobalVariable()) {
1016 DIGlobalVariable(DbgNode).printInternal(OS);
1017 } else if (this->isVariable()) {
1018 DIVariable(DbgNode).printInternal(OS);
1019 } else if (this->isObjCProperty()) {
1020 DIObjCProperty(DbgNode).printInternal(OS);
1021 } else if (this->isScope()) {
1022 DIScope(DbgNode).printInternal(OS);
1023 }
1024 }
1025
printInternal(raw_ostream & OS) const1026 void DISubrange::printInternal(raw_ostream &OS) const {
1027 OS << " [" << getLo() << ", " << getHi() << ']';
1028 }
1029
printInternal(raw_ostream & OS) const1030 void DIScope::printInternal(raw_ostream &OS) const {
1031 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1032 }
1033
printInternal(raw_ostream & OS) const1034 void DICompileUnit::printInternal(raw_ostream &OS) const {
1035 DIScope::printInternal(OS);
1036 if (unsigned Lang = getLanguage())
1037 OS << " [" << dwarf::LanguageString(Lang) << ']';
1038 }
1039
printInternal(raw_ostream & OS) const1040 void DIEnumerator::printInternal(raw_ostream &OS) const {
1041 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1042 }
1043
printInternal(raw_ostream & OS) const1044 void DIType::printInternal(raw_ostream &OS) const {
1045 if (!DbgNode) return;
1046
1047 StringRef Res = getName();
1048 if (!Res.empty())
1049 OS << " [" << Res << "]";
1050
1051 // TODO: Print context?
1052
1053 OS << " [line " << getLineNumber()
1054 << ", size " << getSizeInBits()
1055 << ", align " << getAlignInBits()
1056 << ", offset " << getOffsetInBits();
1057 if (isBasicType())
1058 if (const char *Enc =
1059 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1060 OS << ", enc " << Enc;
1061 OS << "]";
1062
1063 if (isPrivate())
1064 OS << " [private]";
1065 else if (isProtected())
1066 OS << " [protected]";
1067
1068 if (isForwardDecl())
1069 OS << " [fwd]";
1070 }
1071
printInternal(raw_ostream & OS) const1072 void DIDerivedType::printInternal(raw_ostream &OS) const {
1073 DIType::printInternal(OS);
1074 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1075 }
1076
printInternal(raw_ostream & OS) const1077 void DICompositeType::printInternal(raw_ostream &OS) const {
1078 DIType::printInternal(OS);
1079 DIArray A = getTypeArray();
1080 OS << " [" << A.getNumElements() << " elements]";
1081 }
1082
printInternal(raw_ostream & OS) const1083 void DISubprogram::printInternal(raw_ostream &OS) const {
1084 // TODO : Print context
1085 OS << " [line " << getLineNumber() << ']';
1086
1087 if (isLocalToUnit())
1088 OS << " [local]";
1089
1090 if (isDefinition())
1091 OS << " [def]";
1092
1093 if (getScopeLineNumber() != getLineNumber())
1094 OS << " [scope " << getScopeLineNumber() << "]";
1095
1096 StringRef Res = getName();
1097 if (!Res.empty())
1098 OS << " [" << Res << ']';
1099 }
1100
printInternal(raw_ostream & OS) const1101 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1102 StringRef Res = getName();
1103 if (!Res.empty())
1104 OS << " [" << Res << ']';
1105
1106 OS << " [line " << getLineNumber() << ']';
1107
1108 // TODO : Print context
1109
1110 if (isLocalToUnit())
1111 OS << " [local]";
1112
1113 if (isDefinition())
1114 OS << " [def]";
1115 }
1116
printInternal(raw_ostream & OS) const1117 void DIVariable::printInternal(raw_ostream &OS) const {
1118 StringRef Res = getName();
1119 if (!Res.empty())
1120 OS << " [" << Res << ']';
1121
1122 OS << " [line " << getLineNumber() << ']';
1123 }
1124
printInternal(raw_ostream & OS) const1125 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1126 StringRef Name = getObjCPropertyName();
1127 if (!Name.empty())
1128 OS << " [" << Name << ']';
1129
1130 OS << " [line " << getLineNumber()
1131 << ", properties " << getUnsignedField(6) << ']';
1132 }
1133
printDebugLoc(DebugLoc DL,raw_ostream & CommentOS,const LLVMContext & Ctx)1134 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1135 const LLVMContext &Ctx) {
1136 if (!DL.isUnknown()) { // Print source line info.
1137 DIScope Scope(DL.getScope(Ctx));
1138 // Omit the directory, because it's likely to be long and uninteresting.
1139 if (Scope.Verify())
1140 CommentOS << Scope.getFilename();
1141 else
1142 CommentOS << "<unknown>";
1143 CommentOS << ':' << DL.getLine();
1144 if (DL.getCol() != 0)
1145 CommentOS << ':' << DL.getCol();
1146 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1147 if (!InlinedAtDL.isUnknown()) {
1148 CommentOS << " @[ ";
1149 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1150 CommentOS << " ]";
1151 }
1152 }
1153 }
1154
printExtendedName(raw_ostream & OS) const1155 void DIVariable::printExtendedName(raw_ostream &OS) const {
1156 const LLVMContext &Ctx = DbgNode->getContext();
1157 StringRef Res = getName();
1158 if (!Res.empty())
1159 OS << Res << "," << getLineNumber();
1160 if (MDNode *InlinedAt = getInlinedAt()) {
1161 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1162 if (!InlinedAtDL.isUnknown()) {
1163 OS << " @[";
1164 printDebugLoc(InlinedAtDL, OS, Ctx);
1165 OS << "]";
1166 }
1167 }
1168 }
1169