Skip to content

Commit 0efcbcf

Browse files
committed
fix wrong assembly line number when debugging assembly with different sized instructions
1 parent bb21a68 commit 0efcbcf

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
6767
break;
6868
}
6969

70-
stack_frames.emplace_back(CreateStackFrame(frame, dap.frame_format));
70+
stack_frames.emplace_back(
71+
CreateStackFrame(frame, dap.frame_format, dap.target));
7172
}
7273

7374
if (dap.configuration.displayExtendedBacktrace && reached_end_of_stack) {

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "lldb/API/SBFileSpec.h"
2020
#include "lldb/API/SBFrame.h"
2121
#include "lldb/API/SBFunction.h"
22+
#include "lldb/API/SBInstruction.h"
23+
#include "lldb/API/SBInstructionList.h"
2224
#include "lldb/API/SBLineEntry.h"
2325
#include "lldb/API/SBModule.h"
2426
#include "lldb/API/SBQueue.h"
@@ -719,8 +721,8 @@ llvm::json::Value CreateSource(llvm::StringRef source_path) {
719721
// },
720722
// "required": [ "id", "name", "line", "column" ]
721723
// }
722-
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
723-
lldb::SBFormat &format) {
724+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
725+
lldb::SBTarget &target) {
724726
llvm::json::Object object;
725727
int64_t frame_id = MakeDAPFrameID(frame);
726728
object.try_emplace("id", frame_id);
@@ -776,10 +778,18 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
776778

777779
// Calculate the line of the current PC from the start of the current
778780
// symbol.
779-
lldb::addr_t inst_offset = frame.GetPCAddress().GetOffset() -
780-
frame.GetSymbol().GetStartAddress().GetOffset();
781-
lldb::addr_t inst_line =
782-
inst_offset / (frame.GetThread().GetProcess().GetAddressByteSize() / 2);
781+
lldb::SBAddress current_address = frame.GetPCAddress();
782+
lldb::SBInstructionList inst_list =
783+
frame.GetSymbol().GetInstructions(target);
784+
size_t inst_line = 0;
785+
for (size_t i = 0; i < inst_list.GetSize(); ++i) {
786+
lldb::SBInstruction inst = inst_list.GetInstructionAtIndex(i);
787+
if (inst.GetAddress() == current_address) {
788+
inst_line = i;
789+
break;
790+
}
791+
}
792+
783793
// Line numbers are 1-based.
784794
object.try_emplace("line", inst_line + 1);
785795
object.try_emplace("column", 1);

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,14 @@ llvm::json::Value CreateSource(llvm::StringRef source_path);
363363
/// The LLDB format to use when populating out the "StackFrame"
364364
/// object.
365365
///
366+
/// \param[in] target
367+
/// The LLDB target to use when populating out the "StackFrame"
368+
/// object.
366369
/// \return
367370
/// A "StackFrame" JSON object with that follows the formal JSON
368371
/// definition outlined by Microsoft.
369-
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
370-
lldb::SBFormat &format);
372+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
373+
lldb::SBTarget &target);
371374

372375
/// Create a "StackFrame" label object for a LLDB thread.
373376
///

0 commit comments

Comments
 (0)