diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp --- a/lldb/source/Commands/CommandObjectMemory.cpp +++ b/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 #include @@ -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(); + } } } diff --git a/lldb/test/API/functionalities/memory-region/TestMemoryRegion.py b/lldb/test/API/functionalities/memory-region/TestMemoryRegion.py --- a/lldb/test/API/functionalities/memory-region/TestMemoryRegion.py +++ b/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")