[Lldb-commits] [lldb] 3a16829 - [lldb] Switch Section-dumping code to raw_ostream

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu May 14 03:03:15 PDT 2020


Author: Pavel Labath
Date: 2020-05-14T11:59:18+02:00
New Revision: 3a16829748a1ed208a37c719d3a3bcca50dcbd0f

URL: https://github.com/llvm/llvm-project/commit/3a16829748a1ed208a37c719d3a3bcca50dcbd0f
DIFF: https://github.com/llvm/llvm-project/commit/3a16829748a1ed208a37c719d3a3bcca50dcbd0f.diff

LOG: [lldb] Switch Section-dumping code to raw_ostream

Also, add a basic test for dumping sections.

Added: 
    lldb/test/Shell/Commands/command-target-modules-dump-sections.yaml

Modified: 
    lldb/include/lldb/Core/Section.h
    lldb/source/API/SBSection.cpp
    lldb/source/Commands/CommandObjectTarget.cpp
    lldb/source/Core/Address.cpp
    lldb/source/Core/Section.cpp
    lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
    lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
    lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
    lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
    lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
    lldb/source/Target/SectionLoadList.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Section.h b/lldb/include/lldb/Core/Section.h
index 8e3adcfc0ece..3be9bc770be5 100644
--- a/lldb/include/lldb/Core/Section.h
+++ b/lldb/include/lldb/Core/Section.h
@@ -29,7 +29,6 @@ class Address;
 class DataExtractor;
 class ObjectFile;
 class Section;
-class Stream;
 class Target;
 
 class SectionList {
@@ -56,7 +55,8 @@ class SectionList {
 
   bool ContainsSection(lldb::user_id_t sect_id) const;
 
-  void Dump(Stream *s, Target *target, bool show_header, uint32_t depth) const;
+  void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
+            bool show_header, uint32_t depth) const;
 
   lldb::SectionSP FindSectionByName(ConstString section_dstr) const;
 
@@ -127,9 +127,10 @@ class Section : public std::enable_shared_from_this<Section>,
 
   const SectionList &GetChildren() const { return m_children; }
 
-  void Dump(Stream *s, Target *target, uint32_t depth) const;
+  void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
+            uint32_t depth) const;
 
-  void DumpName(Stream *s) const;
+  void DumpName(llvm::raw_ostream &s) const;
 
   lldb::addr_t GetLoadBaseAddress(Target *target) const;
 

diff  --git a/lldb/source/API/SBSection.cpp b/lldb/source/API/SBSection.cpp
index be53d33fb7a3..bb56fa18d9ca 100644
--- a/lldb/source/API/SBSection.cpp
+++ b/lldb/source/API/SBSection.cpp
@@ -281,7 +281,7 @@ bool SBSection::GetDescription(SBStream &description) {
     const addr_t file_addr = section_sp->GetFileAddress();
     strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
                 file_addr + section_sp->GetByteSize());
-    section_sp->DumpName(&strm);
+    section_sp->DumpName(strm.AsRawOstream());
   } else {
     strm.PutCString("No value");
   }

diff  --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index 8db0eef31163..41a6135f340c 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -1442,11 +1442,9 @@ static void DumpModuleSections(CommandInterpreter &interpreter, Stream &strm,
       strm.Printf("Sections for '%s' (%s):\n",
                   module->GetSpecificationDescription().c_str(),
                   module->GetArchitecture().GetArchitectureName());
-      strm.IndentMore();
-      section_list->Dump(&strm,
+      section_list->Dump(strm.AsRawOstream(), strm.GetIndentLevel() + 2,
                          interpreter.GetExecutionContext().GetTargetPtr(), true,
                          UINT32_MAX);
-      strm.IndentLess();
     }
   }
 }

diff  --git a/lldb/source/Core/Address.cpp b/lldb/source/Core/Address.cpp
index 257b2b64792e..9d52f1db8918 100644
--- a/lldb/source/Core/Address.cpp
+++ b/lldb/source/Core/Address.cpp
@@ -415,7 +415,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
 
   case DumpStyleSectionNameOffset:
     if (section_sp) {
-      section_sp->DumpName(s);
+      section_sp->DumpName(s->AsRawOstream());
       s->Printf(" + %" PRIu64, m_offset);
     } else {
       DumpAddress(s->AsRawOstream(), m_offset, addr_size);

diff  --git a/lldb/source/Core/Section.cpp b/lldb/source/Core/Section.cpp
index 3809604566e9..ce4715721ee7 100644
--- a/lldb/source/Core/Section.cpp
+++ b/lldb/source/Core/Section.cpp
@@ -13,7 +13,6 @@
 #include "lldb/Target/SectionLoadList.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/FileSpec.h"
-#include "lldb/Utility/Stream.h"
 #include "lldb/Utility/VMRange.h"
 
 #include <inttypes.h>
@@ -283,15 +282,15 @@ bool Section::ContainsFileAddress(addr_t vm_addr) const {
   return false;
 }
 
-void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
-  //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
-  s->Indent();
-  s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
+void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
+                   uint32_t depth) const {
+  s.indent(indent);
+  s << llvm::format("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
   bool resolved = true;
   addr_t addr = LLDB_INVALID_ADDRESS;
 
   if (GetByteSize() == 0)
-    s->Printf("%39s", "");
+    s.indent(39);
   else {
     if (target)
       addr = GetLoadBaseAddress(target);
@@ -303,27 +302,27 @@ void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
     }
 
     VMRange range(addr, addr + m_byte_size);
-    range.Dump(s->AsRawOstream(), 0);
+    range.Dump(s, 0);
   }
 
-  s->Printf("%c %c%c%c  0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
-            resolved ? ' ' : '*', m_readable ? 'r' : '-',
-            m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
-            m_file_size, Get());
+  s << llvm::format("%c %c%c%c  0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
+                    resolved ? ' ' : '*', m_readable ? 'r' : '-',
+                    m_writable ? 'w' : '-', m_executable ? 'x' : '-',
+                    m_file_offset, m_file_size, Get());
 
   DumpName(s);
 
-  s->EOL();
+  s << "\n";
 
   if (depth > 0)
-    m_children.Dump(s, target, false, depth - 1);
+    m_children.Dump(s, indent, target, false, depth - 1);
 }
 
-void Section::DumpName(Stream *s) const {
+void Section::DumpName(llvm::raw_ostream &s) const {
   SectionSP parent_sp(GetParent());
   if (parent_sp) {
     parent_sp->DumpName(s);
-    s->PutChar('.');
+    s << '.';
   } else {
     // The top most section prints the module basename
     const char *name = nullptr;
@@ -336,9 +335,9 @@ void Section::DumpName(Stream *s) const {
     if ((!name || !name[0]) && module_sp)
       name = module_sp->GetFileSpec().GetFilename().AsCString();
     if (name && name[0])
-      s->Printf("%s.", name);
+      s << name << '.';
   }
-  m_name.Dump(s);
+  s << m_name.GetStringRef();
 }
 
 bool Section::IsDescendant(const Section *section) {
@@ -568,31 +567,27 @@ bool SectionList::ContainsSection(user_id_t sect_id) const {
   return FindSectionByID(sect_id).get() != nullptr;
 }
 
-void SectionList::Dump(Stream *s, Target *target, bool show_header,
-                       uint32_t depth) const {
+void SectionList::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
+                       bool show_header, uint32_t depth) const {
   bool target_has_loaded_sections =
       target && !target->GetSectionLoadList().IsEmpty();
   if (show_header && !m_sections.empty()) {
-    s->Indent();
-    s->Printf("SectID     Type             %s Address                          "
-              "   Perm File Off.  File Size  Flags "
-              "     Section Name\n",
-              target_has_loaded_sections ? "Load" : "File");
-    s->Indent();
-    s->PutCString("---------- ---------------- "
-                  "---------------------------------------  ---- ---------- "
-                  "---------- "
-                  "---------- ----------------------------\n");
+    s.indent(indent);
+    s << llvm::formatv(
+        "SectID     Type             {0} Address                          "
+        "   Perm File Off.  File Size  Flags "
+        "     Section Name\n",
+        target_has_loaded_sections ? "Load" : "File");
+    s.indent(indent);
+    s << "---------- ---------------- "
+         "---------------------------------------  ---- ---------- "
+         "---------- "
+         "---------- ----------------------------\n";
   }
 
-  const_iterator sect_iter;
-  const_iterator end = m_sections.end();
-  for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
-    (*sect_iter)->Dump(s, target_has_loaded_sections ? target : nullptr, depth);
-  }
-
-  if (show_header && !m_sections.empty())
-    s->IndentLess();
+  for (const auto &section_sp : m_sections)
+    section_sp->Dump(s, indent, target_has_loaded_sections ? target : nullptr,
+                     depth);
 }
 
 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {

diff  --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index e7b6d56e885d..6b8a39021cc9 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -2957,7 +2957,8 @@ void ObjectFileELF::Dump(Stream *s) {
   s->EOL();
   SectionList *section_list = GetSectionList();
   if (section_list)
-    section_list->Dump(s, nullptr, true, UINT32_MAX);
+    section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
+                       UINT32_MAX);
   Symtab *symtab = GetSymtab();
   if (symtab)
     symtab->Dump(s, nullptr, eSortOrderNone);

diff  --git a/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp b/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
index 371bbb5ff84d..93cef12fef49 100644
--- a/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
+++ b/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
@@ -163,7 +163,8 @@ void ObjectFileJIT::Dump(Stream *s) {
 
     SectionList *sections = GetSectionList();
     if (sections)
-      sections->Dump(s, nullptr, true, UINT32_MAX);
+      sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
+                     UINT32_MAX);
 
     if (m_symtab_up)
       m_symtab_up->Dump(s, nullptr, eSortOrderNone);

diff  --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
index 006ba468d6f2..bc4017b86f07 100644
--- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -4853,7 +4853,8 @@ void ObjectFileMachO::Dump(Stream *s) {
     *s << "\n";
     SectionList *sections = GetSectionList();
     if (sections)
-      sections->Dump(s, nullptr, true, UINT32_MAX);
+      sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
+                     UINT32_MAX);
 
     if (m_symtab_up)
       m_symtab_up->Dump(s, nullptr, eSortOrderNone);

diff  --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
index 385b291df709..fe567187e2b4 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -1047,7 +1047,8 @@ void ObjectFilePECOFF::Dump(Stream *s) {
 
     SectionList *sections = GetSectionList();
     if (sections)
-      sections->Dump(s, nullptr, true, UINT32_MAX);
+      sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
+                     UINT32_MAX);
 
     if (m_symtab_up)
       m_symtab_up->Dump(s, nullptr, eSortOrderNone);

diff  --git a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
index e18a84bda4a8..91150fa02ebc 100644
--- a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
+++ b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
@@ -436,7 +436,8 @@ void ObjectFileWasm::Dump(Stream *s) {
 
   SectionList *sections = GetSectionList();
   if (sections) {
-    sections->Dump(s, nullptr, true, UINT32_MAX);
+    sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
+                   UINT32_MAX);
   }
   ostream << "\n";
   DumpSectionHeaders(ostream);

diff  --git a/lldb/source/Target/SectionLoadList.cpp b/lldb/source/Target/SectionLoadList.cpp
index 9af267db3a03..f1a626b04802 100644
--- a/lldb/source/Target/SectionLoadList.cpp
+++ b/lldb/source/Target/SectionLoadList.cpp
@@ -253,6 +253,6 @@ void SectionLoadList::Dump(Stream &s, Target *target) {
        ++pos) {
     s.Printf("addr = 0x%16.16" PRIx64 ", section = %p: ", pos->first,
              static_cast<void *>(pos->second.get()));
-    pos->second->Dump(&s, target, 0);
+    pos->second->Dump(s.AsRawOstream(), s.GetIndentLevel(), target, 0);
   }
 }

diff  --git a/lldb/test/Shell/Commands/command-target-modules-dump-sections.yaml b/lldb/test/Shell/Commands/command-target-modules-dump-sections.yaml
new file mode 100644
index 000000000000..57dea7e4b0ab
--- /dev/null
+++ b/lldb/test/Shell/Commands/command-target-modules-dump-sections.yaml
@@ -0,0 +1,36 @@
+# RUN: yaml2obj %s > %t
+# RUN: %lldb %t -o "target modules dump sections" -o exit \
+# RUN:   | FileCheck --match-full-lines --strict-whitespace %s
+
+#      CHECK:Sections for '{{.*}}command-target-modules-dump-sections.yaml.tmp' (x86_64):
+# CHECK-NEXT:  SectID     Type             File Address                             Perm File Off.  File Size  Flags      Section Name
+# CHECK-NEXT:  ---------- ---------------- ---------------------------------------  ---- ---------- ---------- ---------- ----------------------------
+# CHECK-NEXT:  0x00000001 code             [0x0000000000004000-0x0000000000005000)  r-x  0x00001000 0x00001000 0x00000006 command-target-modules-dump-sections.yaml.tmp..text
+# CHECK-NEXT:  0x00000002 regular          [0x0000000000005000-0x0000000000005100)  r--  0x00002000 0x00000100 0x00000002 command-target-modules-dump-sections.yaml.tmp..rodata
+# CHECK-NEXT:  0x00000003 eh-frame         [0x0000000000006000-0x0000000000006040)  r--  0x00002100 0x00000040 0x00000002 command-target-modules-dump-sections.yaml.tmp..eh_frame
+--- !ELF
+FileHeader:      
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:        
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x0000000000004000
+    AddressAlign:    0x0000000000001000
+    Size:            0x1000
+  - Name:            .rodata
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    Address:         0x0000000000005000
+    AddressAlign:    0x0000000000000020
+    Size:            0x100
+  - Name:            .eh_frame
+    Type:            SHT_X86_64_UNWIND
+    Flags:           [ SHF_ALLOC ]
+    Address:         0x0000000000006000
+    AddressAlign:    0x0000000000000008
+    Size:            0x40
+...


        


More information about the lldb-commits mailing list