From 71d94f805514f28730bf39143ee227648d521d09 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Thu, 19 Jul 2012 07:03:58 +0000 Subject: [PATCH] DebugInfo library: add support for fetching absolute paths to source files (instead of basenames) from DWARF. Use this behavior in llvm-dwarfdump tool. Reviewed by Benjamin Kramer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160496 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/DebugInfo/DIContext.h | 20 ++++++------ lib/DebugInfo/DWARFCompileUnit.cpp | 7 ++++ lib/DebugInfo/DWARFCompileUnit.h | 2 ++ lib/DebugInfo/DWARFContext.cpp | 30 ++++++++++++++++-- lib/DebugInfo/DWARFDebugLine.h | 7 ++-- .../Inputs/dwarfdump-test.elf-x86-64 | Bin 10246 -> 10174 bytes .../Inputs/dwarfdump-test2.elf-x86-64 | Bin 7774 -> 7702 bytes .../Inputs/dwarfdump-test3.elf-x86-64 | Bin 6794 -> 7339 bytes test/DebugInfo/dwarfdump-test.test | 24 ++++++++++---- tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 3 +- 10 files changed, 69 insertions(+), 24 deletions(-) diff --git a/include/llvm/DebugInfo/DIContext.h b/include/llvm/DebugInfo/DIContext.h index 6377acb634b..cfdeb46889e 100644 --- a/include/llvm/DebugInfo/DIContext.h +++ b/include/llvm/DebugInfo/DIContext.h @@ -15,9 +15,9 @@ #ifndef LLVM_DEBUGINFO_DICONTEXT_H #define LLVM_DEBUGINFO_DICONTEXT_H +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" -#include namespace llvm { @@ -25,28 +25,29 @@ class raw_ostream; /// DILineInfo - a format-neutral container for source line information. class DILineInfo { - const char *FileName; - const char *FunctionName; + SmallString<16> FileName; + SmallString<16> FunctionName; uint32_t Line; uint32_t Column; public: DILineInfo() : FileName(""), FunctionName(""), Line(0), Column(0) {} - DILineInfo(const char *fileName, const char *functionName, + DILineInfo(const SmallString<16> &fileName, + const SmallString<16> &functionName, uint32_t line, uint32_t column) : FileName(fileName), FunctionName(functionName), Line(line), Column(column) {} - const char *getFileName() const { return FileName; } - const char *getFunctionName() const { return FunctionName; } + const char *getFileName() { return FileName.c_str(); } + const char *getFunctionName() { return FunctionName.c_str(); } uint32_t getLine() const { return Line; } uint32_t getColumn() const { return Column; } bool operator==(const DILineInfo &RHS) const { return Line == RHS.Line && Column == RHS.Column && - std::strcmp(FileName, RHS.FileName) == 0 && - std::strcmp(FunctionName, RHS.FunctionName) == 0; + FileName.equals(RHS.FileName) && + FunctionName.equals(RHS.FunctionName); } bool operator!=(const DILineInfo &RHS) const { return !(*this == RHS); @@ -60,7 +61,8 @@ class DILineInfoSpecifier { public: enum Specification { FileLineInfo = 1 << 0, - FunctionName = 1 << 1 + AbsoluteFilePath = 1 << 1, + FunctionName = 1 << 2 }; // Use file/line info by default. DILineInfoSpecifier(uint32_t flags = FileLineInfo) : Flags(flags) {} diff --git a/lib/DebugInfo/DWARFCompileUnit.cpp b/lib/DebugInfo/DWARFCompileUnit.cpp index 6d4c7df8dcd..b27d57bef1c 100644 --- a/lib/DebugInfo/DWARFCompileUnit.cpp +++ b/lib/DebugInfo/DWARFCompileUnit.cpp @@ -97,6 +97,13 @@ void DWARFCompileUnit::dump(raw_ostream &OS) { getCompileUnitDIE(false)->dump(OS, this, -1U); } +const char *DWARFCompileUnit::getCompilationDir() { + extractDIEsIfNeeded(true); + if (DieArray.empty()) + return 0; + return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, 0); +} + void DWARFCompileUnit::setDIERelations() { if (DieArray.empty()) return; diff --git a/lib/DebugInfo/DWARFCompileUnit.h b/lib/DebugInfo/DWARFCompileUnit.h index e8a97ec716b..b34a5965af8 100644 --- a/lib/DebugInfo/DWARFCompileUnit.h +++ b/lib/DebugInfo/DWARFCompileUnit.h @@ -78,6 +78,8 @@ public: return &DieArray[0]; } + const char *getCompilationDir(); + /// setDIERelations - We read in all of the DIE entries into our flat list /// of DIE entries and now we need to go back through all of them and set the /// parent, sibling and child pointers for quick DIE navigation. diff --git a/lib/DebugInfo/DWARFContext.cpp b/lib/DebugInfo/DWARFContext.cpp index 08e5db22067..a4e0d8eae43 100644 --- a/lib/DebugInfo/DWARFContext.cpp +++ b/lib/DebugInfo/DWARFContext.cpp @@ -8,8 +8,10 @@ //===----------------------------------------------------------------------===// #include "DWARFContext.h" +#include "llvm/ADT/SmallString.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/Format.h" +#include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include using namespace llvm; @@ -148,8 +150,8 @@ DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address, DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset); if (!cu) return DILineInfo(); - const char *fileName = ""; - const char *functionName = ""; + SmallString<16> fileName(""); + SmallString<16> functionName(""); uint32_t line = 0; uint32_t column = 0; if (specifier.needs(DILineInfoSpecifier::FunctionName)) { @@ -171,7 +173,29 @@ DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address, if (rowIndex != -1U) { const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex]; // Take file/line info from the line table. - fileName = lineTable->Prologue.FileNames[row.File - 1].Name.c_str(); + const DWARFDebugLine::FileNameEntry &fileNameEntry = + lineTable->Prologue.FileNames[row.File - 1]; + fileName = fileNameEntry.Name; + if (specifier.needs(DILineInfoSpecifier::AbsoluteFilePath) && + sys::path::is_relative(fileName.str())) { + // Append include directory of file (if it is present in line table) + // and compilation directory of compile unit to make path absolute. + const char *includeDir = 0; + if (uint64_t includeDirIndex = fileNameEntry.DirIdx) { + includeDir = lineTable->Prologue + .IncludeDirectories[includeDirIndex - 1]; + } + SmallString<16> absFileName; + if (includeDir == 0 || sys::path::is_relative(includeDir)) { + if (const char *compilationDir = cu->getCompilationDir()) + sys::path::append(absFileName, compilationDir); + } + if (includeDir) { + sys::path::append(absFileName, includeDir); + } + sys::path::append(absFileName, fileName.str()); + fileName = absFileName; + } line = row.Line; column = row.Column; } diff --git a/lib/DebugInfo/DWARFDebugLine.h b/lib/DebugInfo/DWARFDebugLine.h index bc6a70b1112..a8c0669b738 100644 --- a/lib/DebugInfo/DWARFDebugLine.h +++ b/lib/DebugInfo/DWARFDebugLine.h @@ -12,7 +12,6 @@ #include "llvm/Support/DataExtractor.h" #include -#include #include namespace llvm { @@ -22,9 +21,9 @@ class raw_ostream; class DWARFDebugLine { public: struct FileNameEntry { - FileNameEntry() : DirIdx(0), ModTime(0), Length(0) {} + FileNameEntry() : Name(0), DirIdx(0), ModTime(0), Length(0) {} - std::string Name; + const char *Name; uint64_t DirIdx; uint64_t ModTime; uint64_t Length; @@ -56,7 +55,7 @@ public: // The number assigned to the first special opcode. uint8_t OpcodeBase; std::vector StandardOpcodeLengths; - std::vector IncludeDirectories; + std::vector IncludeDirectories; std::vector FileNames; // Length of the prologue in bytes. diff --git a/test/DebugInfo/Inputs/dwarfdump-test.elf-x86-64 b/test/DebugInfo/Inputs/dwarfdump-test.elf-x86-64 index 7cee96807261e372daa22f0ea30046f16de4b537..fe20c8e596672cb9bbd6972d567904521c7690ec 100755 GIT binary patch delta 448 zcmZn**yle%gK3B8My)6&k?mhZg?<-q&);^)JpcNA_ogq!@4sxG!StS&)q#P5!F_U% zfGqzaAd8W!6-Y8otYKa4;@u`4x;HvnEzBFM%s(npnah zSOw%B1L6f>M=~%Rgwl**0$`0SaE)N6vp}3)!oc4*`GTM{hMLX41w|Mcr%V~mcD#XR1$jbGUmEk!n7Z*n}6E_1RBTxy&$L5s@+>(!#tEBm%IR_f05y+R9smFU delta 546 zcmdnz-xe@IgNZ?GqgE7?NG)&G6OMyng>765oG1Kq?^gK6{butFruV$8^$ZLQZIgQh zWceQgS&Uq*K$2-<4fEs4I|TGNI)FS@7WSUW?*x>5rvN!DjEr0pfiy@NBNLM{12Z>B zm=P$>$g>K>;sr@D@G;H@l3PIntYEb$vJCw5C&vm(M?3|~uV4h3HL-&EFo?DK#gPOWMma$!1kP#3&?3^;$~oE1Sx~rF3Ktg6xUb0$Byljl$rNVt54v%GpomXJ(xoDre1j KWAj8gT}}W_q_DlY~If)!pL2cTcDqkl%AQFmcLn@+mUH=I&UI7 vqweH|Z1R&|h)8h0;bH)T13(e+$qJ$hjB6%4irO~kJIb|6OH}B^ZVGPO0&rQ`YPRuRN&&w~<&&esv)h#H> z&q_@$0dtJ>p}b=KX}eURu6>N_k>YT1shd x!RC8hj!c^scoWze%O*F9m@}DjPu?YB$M|*fPZ4{@7n3bT?HMaJSBlQ%004DVHqZb7 diff --git a/test/DebugInfo/Inputs/dwarfdump-test3.elf-x86-64 b/test/DebugInfo/Inputs/dwarfdump-test3.elf-x86-64 index fb3ca032f0dd19b4ca2bb09f419c9d5a4441b5d6..7c1730462055fd231056d2d465f995ce6eb4e556 100755 GIT binary patch literal 7339 zcmcIpYitzP6}~g$wbx#IZ7@(CK}->}5Sk%2A*D$mHjD9s45mC<8iL0A7<)I`2i+Z0 zgVNL$p#>2nr>Y_vrH!SkDrx#stNv+K$q`Z1_7_tB&|gJrk*E@>5akC^q-?))=brWK z^=?vC>Rs)ebH4Mqk2&`-Gv68++ix0%;9`kAf~5Jj02xw>*&fA!42fMLf~zgoOWVkz zY6kZM-xXWMN^BnNc0DMhhGQT8K|T&^p>>&l+#uPHDJ(`X=yBX|5P zb?KIBtWGy6JEp4p;>++;RVj;4CS0EdvpD`VY%%;h@z)F4P~kpN(>m~{x+zW<{@wV0 zTCrfA;|43^r$Lt^LP7PH`#{5EE1NLc)&gGzd^vjXC+#Lb1bkySfa1rceV3R~_A3yECVtMLhY|;?SBI}*dUzm_7#vBb z6+1v|A)l`L+03?WPPObjPGYgnO=U7}m81c}GXd^;*_59`1Jc!M&@|zCrK}hoJFqw5 z4myL*PV^_3J#;nk5B~@=#7nTCep8uVn@8%I;+KIa=G3@xp0a#W;glzwm*g@L!ow++ z3*nqwWPeHFl*eH_dEve2-R%JH{)H|pk%!3(Z%1#`cD`@fqPF=1TzfY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L z0&jPoTu^xQJh|K<-jnsW$xB(MR0H64M@T|N2v3kq#I3JtKZTWuyOGlxB diff --git a/test/DebugInfo/dwarfdump-test.test b/test/DebugInfo/dwarfdump-test.test index 7b01e382688..beac51f124e 100644 --- a/test/DebugInfo/dwarfdump-test.test +++ b/test/DebugInfo/dwarfdump-test.test @@ -9,22 +9,32 @@ RUN: --address=0x4004b8 --functions | FileCheck %s -check-prefix MANY_CU_1 RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test2.elf-x86-64 \ RUN: --address=0x4004c4 --functions | FileCheck %s -check-prefix MANY_CU_2 RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test3.elf-x86-64 \ -RUN: --address=0x538 --functions | FileCheck %s -check-prefix ABS_ORIGIN_1 +RUN: --address=0x580 --functions | FileCheck %s -check-prefix ABS_ORIGIN_1 +RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test3.elf-x86-64 \ +RUN: --address=0x573 --functions | FileCheck %s -check-prefix INCLUDE_TEST_1 +RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test3.elf-x86-64 \ +RUN: --address=0x56d --functions | FileCheck %s -check-prefix INCLUDE_TEST_2 MAIN: main -MAIN-NEXT: dwarfdump-test.cc:16:10 +MAIN-NEXT: /tmp/dbginfo/dwarfdump-test.cc:16:10 FUNCTION: _Z1fii -FUNCTION-NEXT: dwarfdump-test.cc:11:18 +FUNCTION-NEXT: /tmp/dbginfo/dwarfdump-test.cc:11:18 CTOR_WITH_SPEC: _ZN10DummyClassC1Ei -CTOR_WITH_SPEC-NEXT: dwarfdump-test.cc:4:30 +CTOR_WITH_SPEC-NEXT: /tmp/dbginfo/dwarfdump-test.cc:4:30 MANY_CU_1: a -MANY_CU_1-NEXT: a.cc:2:0 +MANY_CU_1-NEXT: /tmp/dbginfo/a.cc:2:0 MANY_CU_2: main -MANY_CU_2-NEXT: main.cc:4:0 +MANY_CU_2-NEXT: /tmp/dbginfo/main.cc:4:0 ABS_ORIGIN_1: C -ABS_ORIGIN_1-NEXT: def.cc:3:0 +ABS_ORIGIN_1-NEXT: /tmp/dbginfo/def2.cc:4:0 + +INCLUDE_TEST_1: _Z3do2v +INCLUDE_TEST_1-NEXT: /tmp/dbginfo/include/decl2.h:1:0 + +INCLUDE_TEST_2: _Z3do1v +INCLUDE_TEST_2-NEXT: /tmp/include/decl.h:5:0 diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index b6536fa1d71..ec0b4aeb63c 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -97,7 +97,8 @@ static void DumpInput(const StringRef &Filename) { dictx->dump(outs()); } else { // Print line info for the specified address. - int spec_flags = DILineInfoSpecifier::FileLineInfo; + int spec_flags = DILineInfoSpecifier::FileLineInfo | + DILineInfoSpecifier::AbsoluteFilePath; if (PrintFunctions) spec_flags |= DILineInfoSpecifier::FunctionName; DILineInfo dli = dictx->getLineInfoForAddress(Address, spec_flags);