kastiglione created this revision. kastiglione added reviewers: JDevlieghere, aprantl. kastiglione requested review of this revision. Herald added a project: LLDB. Herald added a subscriber: lldb-commits.
`memory region` displays the top-level section name, which for MachO is the segment name. The segment name alone is not much use, it's very course grained. This changes `memory region` to display the segment and section names for MachO images. For example: `__DATA,__const`. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D116419 Files: lldb/source/Commands/CommandObjectMemory.cpp lldb/test/API/functionalities/memory-region/TestMemoryRegion.py Index: lldb/test/API/functionalities/memory-region/TestMemoryRegion.py =================================================================== --- lldb/test/API/functionalities/memory-region/TestMemoryRegion.py +++ lldb/test/API/functionalities/memory-region/TestMemoryRegion.py @@ -62,3 +62,21 @@ interp.HandleCommand("memory region", result) self.assertFalse(result.Succeeded()) self.assertRegexpMatches(result.GetError(), "Usage: memory region ADDR") + + @skipUnlessDarwin + def test_macho(self): + self.build() + + # Set breakpoint in main and run + self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) + lldbutil.run_break_set_by_symbol( + self, "main", sym_exact=True, num_expected_locations=1 + ) + + self.runCmd("run", RUN_SUCCEEDED) + + interp = self.dbg.GetCommandInterpreter() + result = lldb.SBCommandReturnObject() + + interp.HandleCommand("memory region $pc", result) + self.assertRegexpMatches(result.GetOutput(), r"\b__TEXT,__text\b") Index: lldb/source/Commands/CommandObjectMemory.cpp =================================================================== --- lldb/source/Commands/CommandObjectMemory.cpp +++ lldb/source/Commands/CommandObjectMemory.cpp @@ -34,6 +34,7 @@ #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/StreamString.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MathExtras.h" #include <cinttypes> #include <memory> @@ -1673,13 +1674,26 @@ lldb_private::Address addr; ConstString name = range_info.GetName(); ConstString section_name; - if (process_sp->GetTarget().ResolveLoadAddress(load_addr, addr)) { + auto &target = process_sp->GetTarget(); + if (target.ResolveLoadAddress(load_addr, addr)) { SectionSP section_sp(addr.GetSection()); if (section_sp) { - // Got the top most section, not the deepest section - while (section_sp->GetParent()) - section_sp = section_sp->GetParent(); - section_name = section_sp->GetName(); + if (target.GetArchitecture().GetTriple().isOSBinFormatMachO()) { + // Display the conventional Mach-O format: __SEG,__SECT + if (auto segment_sp = section_sp->GetParent()) { + auto segment_name = segment_sp->GetName(); + auto section_name_ = section_sp->GetName(); + section_name = ConstString( + llvm::formatv("{0},{1}", segment_name, section_name_).str()); + } + } + + if (section_name.IsEmpty()) { + // Got the top most section, not the deepest section + while (section_sp->GetParent()) + section_sp = section_sp->GetParent(); + section_name = section_sp->GetName(); + } } }
Index: lldb/test/API/functionalities/memory-region/TestMemoryRegion.py =================================================================== --- lldb/test/API/functionalities/memory-region/TestMemoryRegion.py +++ lldb/test/API/functionalities/memory-region/TestMemoryRegion.py @@ -62,3 +62,21 @@ interp.HandleCommand("memory region", result) self.assertFalse(result.Succeeded()) self.assertRegexpMatches(result.GetError(), "Usage: memory region ADDR") + + @skipUnlessDarwin + def test_macho(self): + self.build() + + # Set breakpoint in main and run + self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) + lldbutil.run_break_set_by_symbol( + self, "main", sym_exact=True, num_expected_locations=1 + ) + + self.runCmd("run", RUN_SUCCEEDED) + + interp = self.dbg.GetCommandInterpreter() + result = lldb.SBCommandReturnObject() + + interp.HandleCommand("memory region $pc", result) + self.assertRegexpMatches(result.GetOutput(), r"\b__TEXT,__text\b") Index: lldb/source/Commands/CommandObjectMemory.cpp =================================================================== --- lldb/source/Commands/CommandObjectMemory.cpp +++ lldb/source/Commands/CommandObjectMemory.cpp @@ -34,6 +34,7 @@ #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/StreamString.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MathExtras.h" #include <cinttypes> #include <memory> @@ -1673,13 +1674,26 @@ lldb_private::Address addr; ConstString name = range_info.GetName(); ConstString section_name; - if (process_sp->GetTarget().ResolveLoadAddress(load_addr, addr)) { + auto &target = process_sp->GetTarget(); + if (target.ResolveLoadAddress(load_addr, addr)) { SectionSP section_sp(addr.GetSection()); if (section_sp) { - // Got the top most section, not the deepest section - while (section_sp->GetParent()) - section_sp = section_sp->GetParent(); - section_name = section_sp->GetName(); + if (target.GetArchitecture().GetTriple().isOSBinFormatMachO()) { + // Display the conventional Mach-O format: __SEG,__SECT + if (auto segment_sp = section_sp->GetParent()) { + auto segment_name = segment_sp->GetName(); + auto section_name_ = section_sp->GetName(); + section_name = ConstString( + llvm::formatv("{0},{1}", segment_name, section_name_).str()); + } + } + + if (section_name.IsEmpty()) { + // Got the top most section, not the deepest section + while (section_sp->GetParent()) + section_sp = section_sp->GetParent(); + section_name = section_sp->GetName(); + } } }
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits