[DebugInfo] Simplify string table dumpers.

This patch adds a helper function DumpStrSection to simplify codes.
Besides, nonprintable chars in debug_str and debug_str.dwo sections
are printed as escaped chars.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D86918
This commit is contained in:
Xing GUO 2020-09-02 08:41:04 +08:00
parent 1ccfb52a61
commit 369f9169a5
2 changed files with 55 additions and 40 deletions

View File

@ -526,6 +526,18 @@ void DWARFContext::dump(
} }
}; };
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, if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
DObj->getLineSection().Data)) { DObj->getLineSection().Data)) {
DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(), DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
@ -556,37 +568,16 @@ void DWARFContext::dump(
} }
if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr, if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
DObj->getStrSection())) { DObj->getStrSection()))
DataExtractor strData(DObj->getStrSection(), isLittleEndian(), 0); DumpStrSection(DObj->getStrSection());
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;
}
}
if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr, if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
DObj->getStrDWOSection())) { DObj->getStrDWOSection()))
DataExtractor strDWOData(DObj->getStrDWOSection(), isLittleEndian(), 0); DumpStrSection(DObj->getStrDWOSection());
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;
}
}
if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr, if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
DObj->getLineStrSection())) { DObj->getLineStrSection()))
DataExtractor strData(DObj->getLineStrSection(), isLittleEndian(), 0); DumpStrSection(DObj->getLineStrSection());
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;
}
}
if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr, if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr,
DObj->getAddrSection().Data)) { DObj->getAddrSection().Data)) {

View File

@ -1,14 +1,19 @@
## Show that llvm-dwarfdump dumps the .debug_str section when --debug-str is ## a) Show that llvm-dwarfdump dumps the .debug_str and
## specified. ## .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 # RUN: llvm-dwarfdump %t.o --debug-str | FileCheck %s
# CHECK: .debug_str contents: # CHECK: .debug_str contents:
# CHECK-NEXT: 0x00000000: "some string" # CHECK-NEXT: 0x00000000: "some string"
# CHECK-NEXT: 0x0000000c: "foo" # CHECK-NEXT: 0x0000000c: "foo"
# CHECK-NEXT: 0x00000010: "" # CHECK-NEXT: 0x00000010: ""
# CHECK-NOT: {{.}} # CHECK: .debug_str.dwo contents:
# CHECK-NEXT: 0x00000000: "some string"
# CHECK-NEXT: 0x0000000c: "foo"
# CHECK-NEXT: 0x00000010: ""
# CHECK-EMPTY:
--- !ELF --- !ELF
FileHeader: FileHeader:
@ -17,6 +22,25 @@ FileHeader:
Type: ET_REL Type: ET_REL
Machine: EM_X86_64 Machine: EM_X86_64
Sections: Sections:
- Name: .debug_str - Name: .debug_str
Type: SHT_PROGBITS Type: SHT_PROGBITS
Content: '736f6d6520737472696e6700666f6f0000' ## "some string\0foo\0\0" 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: