[llvm] 369f916 - [DebugInfo] Simplify string table dumpers.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 17:41:55 PDT 2020


Author: Xing GUO
Date: 2020-09-02T08:41:10+08:00
New Revision: 369f9169a52350365c9dcdcab55a7a5fe2fe6dbf

URL: https://github.com/llvm/llvm-project/commit/369f9169a52350365c9dcdcab55a7a5fe2fe6dbf
DIFF: https://github.com/llvm/llvm-project/commit/369f9169a52350365c9dcdcab55a7a5fe2fe6dbf.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
    llvm/test/tools/llvm-dwarfdump/debug-str.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
index 96ba57946833..88f118bb05e3 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -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,
                                    DObj->getLineSection().Data)) {
     DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
@@ -556,37 +568,16 @@ void DWARFContext::dump(
   }
 
   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
index 6925ed684f1a..36729c182310 100644
--- 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 @@ FileHeader:
   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:


        


More information about the llvm-commits mailing list