[Lldb-commits] [lldb] [lldb-dap] Show load addresses in disassembly (PR #136755)
Ely Ronnen via lldb-commits
lldb-commits at lists.llvm.org
Tue Apr 22 16:42:59 PDT 2025
https://github.com/eronnen updated https://github.com/llvm/llvm-project/pull/136755
>From c1c072875b9a7ad3d11a03d3c1e4a8b6452749f9 Mon Sep 17 00:00:00 2001
From: Ely Ronnen <elyronnen at gmail.com>
Date: Tue, 22 Apr 2025 21:58:19 +0200
Subject: [PATCH 1/3] [lldb-dap]: show load addresses in disassembly
---
lldb/include/lldb/API/SBFrame.h | 1 +
lldb/include/lldb/API/SBInstructionList.h | 7 +++--
lldb/source/API/SBInstructionList.cpp | 29 ++++++++++++++-----
.../lldb-dap/Handler/SourceRequestHandler.cpp | 2 +-
4 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/lldb/include/lldb/API/SBFrame.h b/lldb/include/lldb/API/SBFrame.h
index 3635ee5a537ad..66db059d531a8 100644
--- a/lldb/include/lldb/API/SBFrame.h
+++ b/lldb/include/lldb/API/SBFrame.h
@@ -221,6 +221,7 @@ class LLDB_API SBFrame {
friend class SBBlock;
friend class SBExecutionContext;
friend class SBInstruction;
+ friend class SBInstructionList;
friend class SBThread;
friend class SBValue;
diff --git a/lldb/include/lldb/API/SBInstructionList.h b/lldb/include/lldb/API/SBInstructionList.h
index 4c26ec9a294e0..b337c91721653 100644
--- a/lldb/include/lldb/API/SBInstructionList.h
+++ b/lldb/include/lldb/API/SBInstructionList.h
@@ -54,6 +54,10 @@ class LLDB_API SBInstructionList {
bool GetDescription(lldb::SBStream &description);
+ // Writes assembly instructions to `description` with load addresses using
+ // `frame`.
+ bool GetDescription(lldb::SBStream &description, lldb::SBFrame &frame);
+
bool DumpEmulationForAllInstructions(const char *triple);
protected:
@@ -62,8 +66,7 @@ class LLDB_API SBInstructionList {
friend class SBTarget;
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp);
- bool GetDescription(lldb_private::Stream &description);
-
+ bool GetDescription(lldb_private::Stream &description, lldb::SBFrame *frame);
private:
lldb::DisassemblerSP m_opaque_sp;
diff --git a/lldb/source/API/SBInstructionList.cpp b/lldb/source/API/SBInstructionList.cpp
index c18204375dff1..79432e8c6c91a 100644
--- a/lldb/source/API/SBInstructionList.cpp
+++ b/lldb/source/API/SBInstructionList.cpp
@@ -15,8 +15,10 @@
#include "lldb/Core/Module.h"
#include "lldb/Host/StreamFile.h"
#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/ExecutionContext.h"
#include "lldb/Utility/Instrumentation.h"
#include "lldb/Utility/Stream.h"
+#include <memory>
using namespace lldb;
using namespace lldb_private;
@@ -114,7 +116,7 @@ void SBInstructionList::Print(FILE *out) {
if (out == nullptr)
return;
StreamFile stream(out, false);
- GetDescription(stream);
+ GetDescription(stream, nullptr);
}
void SBInstructionList::Print(SBFile out) {
@@ -122,7 +124,7 @@ void SBInstructionList::Print(SBFile out) {
if (!out.IsValid())
return;
StreamFile stream(out.m_opaque_sp);
- GetDescription(stream);
+ GetDescription(stream, nullptr);
}
void SBInstructionList::Print(FileSP out_sp) {
@@ -130,15 +132,21 @@ void SBInstructionList::Print(FileSP out_sp) {
if (!out_sp || !out_sp->IsValid())
return;
StreamFile stream(out_sp);
- GetDescription(stream);
+ GetDescription(stream, nullptr);
}
bool SBInstructionList::GetDescription(lldb::SBStream &stream) {
LLDB_INSTRUMENT_VA(this, stream);
- return GetDescription(stream.ref());
+ return GetDescription(stream.ref(), nullptr);
}
-bool SBInstructionList::GetDescription(Stream &sref) {
+bool SBInstructionList::GetDescription(lldb::SBStream &stream,
+ lldb::SBFrame &frame) {
+ LLDB_INSTRUMENT_VA(this, stream);
+ return GetDescription(stream.ref(), &frame);
+}
+
+bool SBInstructionList::GetDescription(Stream &sref, lldb::SBFrame *frame) {
if (m_opaque_sp) {
size_t num_instructions = GetSize();
@@ -148,10 +156,15 @@ bool SBInstructionList::GetDescription(Stream &sref) {
const uint32_t max_opcode_byte_size =
m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
FormatEntity::Entry format;
- FormatEntity::Parse("${addr}: ", format);
+ FormatEntity::Parse("${addr-file-or-load}: ", format);
SymbolContext sc;
SymbolContext prev_sc;
+ std::shared_ptr<ExecutionContext> exec_ctx;
+ if (nullptr != frame) {
+ exec_ctx = std::make_shared<ExecutionContext>(frame->GetFrameSP());
+ }
+
// Expected address of the next instruction. Used to print an empty line
// for non-contiguous blocks of insns.
std::optional<Address> next_addr;
@@ -172,8 +185,8 @@ bool SBInstructionList::GetDescription(Stream &sref) {
if (next_addr && *next_addr != addr)
sref.EOL();
inst->Dump(&sref, max_opcode_byte_size, true, false,
- /*show_control_flow_kind=*/false, nullptr, &sc, &prev_sc,
- &format, 0);
+ /*show_control_flow_kind=*/false, exec_ctx.get(), &sc,
+ &prev_sc, &format, 0);
sref.EOL();
next_addr = addr;
next_addr->Slide(inst->GetOpcode().GetByteSize());
diff --git a/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
index 1a7a13d9f267a..bd4fc981475f4 100644
--- a/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
@@ -43,7 +43,7 @@ SourceRequestHandler::Run(const protocol::SourceArguments &args) const {
lldb::SBInstructionList insts = frame.GetSymbol().GetInstructions(dap.target);
lldb::SBStream stream;
- insts.GetDescription(stream);
+ insts.GetDescription(stream, frame);
return protocol::SourceResponseBody{/*content=*/stream.GetData(),
/*mimeType=*/"text/x-lldb.disassembly"};
>From cb037880698f8df5ac37eba9cc9f90275f2a500e Mon Sep 17 00:00:00 2001
From: Ely Ronnen <elyronnen at gmail.com>
Date: Wed, 23 Apr 2025 01:06:43 +0200
Subject: [PATCH 2/3] using SBExecutionContext parameter instead of SBFrame
---
lldb/include/lldb/API/SBExecutionContext.h | 1 +
lldb/include/lldb/API/SBFrame.h | 1 -
lldb/include/lldb/API/SBInstructionList.h | 8 ++++---
lldb/source/API/SBInstructionList.cpp | 24 ++++++++++---------
.../lldb-dap/Handler/SourceRequestHandler.cpp | 4 +++-
5 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/lldb/include/lldb/API/SBExecutionContext.h b/lldb/include/lldb/API/SBExecutionContext.h
index e1e08fe3f4aae..20584271ff36c 100644
--- a/lldb/include/lldb/API/SBExecutionContext.h
+++ b/lldb/include/lldb/API/SBExecutionContext.h
@@ -55,6 +55,7 @@ class LLDB_API SBExecutionContext {
SBFrame GetFrame() const;
protected:
+ friend class SBInstructionList;
friend class lldb_private::python::SWIGBridge;
friend class lldb_private::ScriptInterpreter;
diff --git a/lldb/include/lldb/API/SBFrame.h b/lldb/include/lldb/API/SBFrame.h
index 66db059d531a8..3635ee5a537ad 100644
--- a/lldb/include/lldb/API/SBFrame.h
+++ b/lldb/include/lldb/API/SBFrame.h
@@ -221,7 +221,6 @@ class LLDB_API SBFrame {
friend class SBBlock;
friend class SBExecutionContext;
friend class SBInstruction;
- friend class SBInstructionList;
friend class SBThread;
friend class SBValue;
diff --git a/lldb/include/lldb/API/SBInstructionList.h b/lldb/include/lldb/API/SBInstructionList.h
index b337c91721653..aafbe80a80cba 100644
--- a/lldb/include/lldb/API/SBInstructionList.h
+++ b/lldb/include/lldb/API/SBInstructionList.h
@@ -55,8 +55,9 @@ class LLDB_API SBInstructionList {
bool GetDescription(lldb::SBStream &description);
// Writes assembly instructions to `description` with load addresses using
- // `frame`.
- bool GetDescription(lldb::SBStream &description, lldb::SBFrame &frame);
+ // `exe_ctx`.
+ bool GetDescription(lldb::SBStream &description,
+ lldb::SBExecutionContext &exe_ctx);
bool DumpEmulationForAllInstructions(const char *triple);
@@ -66,7 +67,8 @@ class LLDB_API SBInstructionList {
friend class SBTarget;
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp);
- bool GetDescription(lldb_private::Stream &description, lldb::SBFrame *frame);
+ bool GetDescription(lldb_private::Stream &description,
+ lldb::SBExecutionContext *exe_ctx = nullptr);
private:
lldb::DisassemblerSP m_opaque_sp;
diff --git a/lldb/source/API/SBInstructionList.cpp b/lldb/source/API/SBInstructionList.cpp
index 79432e8c6c91a..8169392fcd1a4 100644
--- a/lldb/source/API/SBInstructionList.cpp
+++ b/lldb/source/API/SBInstructionList.cpp
@@ -8,6 +8,7 @@
#include "lldb/API/SBInstructionList.h"
#include "lldb/API/SBAddress.h"
+#include "lldb/API/SBExecutionContext.h"
#include "lldb/API/SBFile.h"
#include "lldb/API/SBInstruction.h"
#include "lldb/API/SBStream.h"
@@ -116,7 +117,7 @@ void SBInstructionList::Print(FILE *out) {
if (out == nullptr)
return;
StreamFile stream(out, false);
- GetDescription(stream, nullptr);
+ GetDescription(stream);
}
void SBInstructionList::Print(SBFile out) {
@@ -124,7 +125,7 @@ void SBInstructionList::Print(SBFile out) {
if (!out.IsValid())
return;
StreamFile stream(out.m_opaque_sp);
- GetDescription(stream, nullptr);
+ GetDescription(stream);
}
void SBInstructionList::Print(FileSP out_sp) {
@@ -132,21 +133,22 @@ void SBInstructionList::Print(FileSP out_sp) {
if (!out_sp || !out_sp->IsValid())
return;
StreamFile stream(out_sp);
- GetDescription(stream, nullptr);
+ GetDescription(stream);
}
bool SBInstructionList::GetDescription(lldb::SBStream &stream) {
LLDB_INSTRUMENT_VA(this, stream);
- return GetDescription(stream.ref(), nullptr);
+ return GetDescription(stream.ref());
}
bool SBInstructionList::GetDescription(lldb::SBStream &stream,
- lldb::SBFrame &frame) {
+ lldb::SBExecutionContext &exe_ctx) {
LLDB_INSTRUMENT_VA(this, stream);
- return GetDescription(stream.ref(), &frame);
+ return GetDescription(stream.ref(), &exe_ctx);
}
-bool SBInstructionList::GetDescription(Stream &sref, lldb::SBFrame *frame) {
+bool SBInstructionList::GetDescription(Stream &sref,
+ lldb::SBExecutionContext *exe_ctx) {
if (m_opaque_sp) {
size_t num_instructions = GetSize();
@@ -160,9 +162,9 @@ bool SBInstructionList::GetDescription(Stream &sref, lldb::SBFrame *frame) {
SymbolContext sc;
SymbolContext prev_sc;
- std::shared_ptr<ExecutionContext> exec_ctx;
- if (nullptr != frame) {
- exec_ctx = std::make_shared<ExecutionContext>(frame->GetFrameSP());
+ std::shared_ptr<ExecutionContext> exe_ctx_ptr;
+ if (nullptr != exe_ctx) {
+ exe_ctx_ptr = std::make_shared<ExecutionContext>(exe_ctx->get());
}
// Expected address of the next instruction. Used to print an empty line
@@ -185,7 +187,7 @@ bool SBInstructionList::GetDescription(Stream &sref, lldb::SBFrame *frame) {
if (next_addr && *next_addr != addr)
sref.EOL();
inst->Dump(&sref, max_opcode_byte_size, true, false,
- /*show_control_flow_kind=*/false, exec_ctx.get(), &sc,
+ /*show_control_flow_kind=*/false, exe_ctx_ptr.get(), &sc,
&prev_sc, &format, 0);
sref.EOL();
next_addr = addr;
diff --git a/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
index bd4fc981475f4..327198bab0395 100644
--- a/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp
@@ -11,6 +11,7 @@
#include "LLDBUtils.h"
#include "Protocol/ProtocolRequests.h"
#include "Protocol/ProtocolTypes.h"
+#include "lldb/API/SBExecutionContext.h"
#include "lldb/API/SBFrame.h"
#include "lldb/API/SBInstructionList.h"
#include "lldb/API/SBProcess.h"
@@ -43,7 +44,8 @@ SourceRequestHandler::Run(const protocol::SourceArguments &args) const {
lldb::SBInstructionList insts = frame.GetSymbol().GetInstructions(dap.target);
lldb::SBStream stream;
- insts.GetDescription(stream, frame);
+ lldb::SBExecutionContext exe_ctx(frame);
+ insts.GetDescription(stream, exe_ctx);
return protocol::SourceResponseBody{/*content=*/stream.GetData(),
/*mimeType=*/"text/x-lldb.disassembly"};
>From 70d9ce366d76f4ab6e6a3be10166f957d394cbcf Mon Sep 17 00:00:00 2001
From: Ely Ronnen <elyronnen at gmail.com>
Date: Wed, 23 Apr 2025 01:41:16 +0200
Subject: [PATCH 3/3] cleaner SBInstructionList::GetDescription
---
lldb/include/lldb/API/SBInstructionList.h | 2 +-
lldb/source/API/SBInstructionList.cpp | 17 ++++++-----------
2 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/lldb/include/lldb/API/SBInstructionList.h b/lldb/include/lldb/API/SBInstructionList.h
index aafbe80a80cba..4c6bab9c8ccc7 100644
--- a/lldb/include/lldb/API/SBInstructionList.h
+++ b/lldb/include/lldb/API/SBInstructionList.h
@@ -68,7 +68,7 @@ class LLDB_API SBInstructionList {
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp);
bool GetDescription(lldb_private::Stream &description,
- lldb::SBExecutionContext *exe_ctx = nullptr);
+ lldb_private::ExecutionContext *exe_ctx = nullptr);
private:
lldb::DisassemblerSP m_opaque_sp;
diff --git a/lldb/source/API/SBInstructionList.cpp b/lldb/source/API/SBInstructionList.cpp
index 8169392fcd1a4..0d958c6ae93ef 100644
--- a/lldb/source/API/SBInstructionList.cpp
+++ b/lldb/source/API/SBInstructionList.cpp
@@ -19,7 +19,6 @@
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Utility/Instrumentation.h"
#include "lldb/Utility/Stream.h"
-#include <memory>
using namespace lldb;
using namespace lldb_private;
@@ -144,11 +143,12 @@ bool SBInstructionList::GetDescription(lldb::SBStream &stream) {
bool SBInstructionList::GetDescription(lldb::SBStream &stream,
lldb::SBExecutionContext &exe_ctx) {
LLDB_INSTRUMENT_VA(this, stream);
- return GetDescription(stream.ref(), &exe_ctx);
+ ExecutionContext exe_ctx_wrapper(exe_ctx.get());
+ return GetDescription(stream.ref(), &exe_ctx_wrapper);
}
-bool SBInstructionList::GetDescription(Stream &sref,
- lldb::SBExecutionContext *exe_ctx) {
+bool SBInstructionList::GetDescription(
+ Stream &sref, lldb_private::ExecutionContext *exe_ctx) {
if (m_opaque_sp) {
size_t num_instructions = GetSize();
@@ -162,11 +162,6 @@ bool SBInstructionList::GetDescription(Stream &sref,
SymbolContext sc;
SymbolContext prev_sc;
- std::shared_ptr<ExecutionContext> exe_ctx_ptr;
- if (nullptr != exe_ctx) {
- exe_ctx_ptr = std::make_shared<ExecutionContext>(exe_ctx->get());
- }
-
// Expected address of the next instruction. Used to print an empty line
// for non-contiguous blocks of insns.
std::optional<Address> next_addr;
@@ -187,8 +182,8 @@ bool SBInstructionList::GetDescription(Stream &sref,
if (next_addr && *next_addr != addr)
sref.EOL();
inst->Dump(&sref, max_opcode_byte_size, true, false,
- /*show_control_flow_kind=*/false, exe_ctx_ptr.get(), &sc,
- &prev_sc, &format, 0);
+ /*show_control_flow_kind=*/false, exe_ctx, &sc, &prev_sc,
+ &format, 0);
sref.EOL();
next_addr = addr;
next_addr->Slide(inst->GetOpcode().GetByteSize());
More information about the lldb-commits
mailing list