/3rd_party/llvm/lib/Analysis/DebugInfo.cpp

https://code.google.com/p/softart/ · C++ · 1143 lines · 820 code · 179 blank · 144 comment · 311 complexity · d7c1847bec8e9b70312841480dfa88bf MD5 · raw file

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