diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -526,6 +526,18 @@ } }; + auto DumpStrSection = [&](StringRef Section) { + DataExtractor StrData(Section, isLittleEndian(), 0); + uint64_t Offset = 0; + uint64_t StrOffset = 0; + while (const char *CStr = StrData.getCStr(&Offset)) { + OS << format("0x%8.8" PRIx64 ": \"", StrOffset); + OS.write_escaped(CStr); + OS << "\"\n"; + StrOffset = Offset; + } + }; + if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine, DObj->getLineSection().Data)) { DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(), @@ -556,37 +568,16 @@ } if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr, - DObj->getStrSection())) { - DataExtractor strData(DObj->getStrSection(), isLittleEndian(), 0); - uint64_t offset = 0; - uint64_t strOffset = 0; - while (const char *s = strData.getCStr(&offset)) { - OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strOffset, s); - strOffset = offset; - } - } + DObj->getStrSection())) + DumpStrSection(DObj->getStrSection()); + if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr, - DObj->getStrDWOSection())) { - DataExtractor strDWOData(DObj->getStrDWOSection(), isLittleEndian(), 0); - uint64_t offset = 0; - uint64_t strDWOOffset = 0; - while (const char *s = strDWOData.getCStr(&offset)) { - OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strDWOOffset, s); - strDWOOffset = offset; - } - } + DObj->getStrDWOSection())) + DumpStrSection(DObj->getStrDWOSection()); + if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr, - DObj->getLineStrSection())) { - DataExtractor strData(DObj->getLineStrSection(), isLittleEndian(), 0); - uint64_t offset = 0; - uint64_t strOffset = 0; - while (const char *s = strData.getCStr(&offset)) { - OS << format("0x%8.8" PRIx64 ": \"", strOffset); - OS.write_escaped(s); - OS << "\"\n"; - strOffset = offset; - } - } + DObj->getLineStrSection())) + DumpStrSection(DObj->getLineStrSection()); if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr, DObj->getAddrSection().Data)) { diff --git a/llvm/test/tools/llvm-dwarfdump/debug-str.yaml b/llvm/test/tools/llvm-dwarfdump/debug-str.yaml --- a/llvm/test/tools/llvm-dwarfdump/debug-str.yaml +++ b/llvm/test/tools/llvm-dwarfdump/debug-str.yaml @@ -1,14 +1,19 @@ -## Show that llvm-dwarfdump dumps the .debug_str section when --debug-str is -## specified. +## a) Show that llvm-dwarfdump dumps the .debug_str and +## .debug_str.dwo sections when --debug-str is specified. -# RUN: yaml2obj %s -o %t.o +## "some string\0foo\0\0" +# RUN: yaml2obj -DCONTENT="736f6d6520737472696e6700666f6f0000" %s -o %t.o # RUN: llvm-dwarfdump %t.o --debug-str | FileCheck %s -# CHECK: .debug_str contents: -# CHECK-NEXT: 0x00000000: "some string" -# CHECK-NEXT: 0x0000000c: "foo" -# CHECK-NEXT: 0x00000010: "" -# CHECK-NOT: {{.}} +# CHECK: .debug_str contents: +# CHECK-NEXT: 0x00000000: "some string" +# CHECK-NEXT: 0x0000000c: "foo" +# CHECK-NEXT: 0x00000010: "" +# CHECK: .debug_str.dwo contents: +# CHECK-NEXT: 0x00000000: "some string" +# CHECK-NEXT: 0x0000000c: "foo" +# CHECK-NEXT: 0x00000010: "" +# CHECK-EMPTY: --- !ELF FileHeader: @@ -17,6 +22,25 @@ Type: ET_REL Machine: EM_X86_64 Sections: - - Name: .debug_str - Type: SHT_PROGBITS - Content: '736f6d6520737472696e6700666f6f0000' ## "some string\0foo\0\0" + - Name: .debug_str + Type: SHT_PROGBITS + Content: [[CONTENT]] + - Name: .debug_str.dwo + Type: SHT_PROGBITS + Content: [[CONTENT]] + +## b) Test how we dump unprintable chars. + +## ['\t', '\0', '\001', '\0', '\\', '0', '0', '1', '\0'] +# RUN: yaml2obj -DCONTENT="090001005C30303100" %s -o %t2.o +# RUN: llvm-dwarfdump --debug-str %t2.o | FileCheck %s --check-prefix=ESCAPED + +# ESCAPED: .debug_str contents: +# ESCAPED-NEXT: 0x00000000: "\t" +# ESCAPED-NEXT: 0x00000002: "\001" +# ESCAPED-NEXT: 0x00000004: "\\001" +# ESCAPED: .debug_str.dwo contents: +# ESCAPED-NEXT: 0x00000000: "\t" +# ESCAPED-NEXT: 0x00000002: "\001" +# ESCAPED-NEXT: 0x00000004: "\\001" +# ESCAPED-EMPTY: