Skip to content

[lldb-dap] fix wrong assembly line number x64 #136486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025

Conversation

eronnen
Copy link
Contributor

@eronnen eronnen commented Apr 20, 2025

Fix wrong assembly line number calculation that assumes the instruction size is GetAddressByteSize() / 2

Fixes #136495

@eronnen eronnen requested a review from JDevlieghere as a code owner April 20, 2025 12:31
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 20, 2025

@llvm/pr-subscribers-lldb

Author: Ely Ronnen (eronnen)

Changes

Fix wrong assembly line number calculation that assumes the instruction size is GetAddressByteSize() / 2


Full diff: https://github.com/llvm/llvm-project/pull/136486.diff

3 Files Affected:

  • (modified) lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp (+2-1)
  • (modified) lldb/tools/lldb-dap/JSONUtils.cpp (+16-6)
  • (modified) lldb/tools/lldb-dap/JSONUtils.h (+5-2)
diff --git a/lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp
index a58e3325af100..5d1d2835b4392 100644
--- a/lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/StackTraceRequestHandler.cpp
@@ -67,7 +67,8 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
       break;
     }
 
-    stack_frames.emplace_back(CreateStackFrame(frame, dap.frame_format));
+    stack_frames.emplace_back(
+        CreateStackFrame(frame, dap.frame_format, dap.target));
   }
 
   if (dap.configuration.displayExtendedBacktrace && reached_end_of_stack) {
diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp
index 33f10c93d2ada..552fdc9439845 100644
--- a/lldb/tools/lldb-dap/JSONUtils.cpp
+++ b/lldb/tools/lldb-dap/JSONUtils.cpp
@@ -19,6 +19,8 @@
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBFrame.h"
 #include "lldb/API/SBFunction.h"
+#include "lldb/API/SBInstruction.h"
+#include "lldb/API/SBInstructionList.h"
 #include "lldb/API/SBLineEntry.h"
 #include "lldb/API/SBModule.h"
 #include "lldb/API/SBQueue.h"
@@ -719,8 +721,8 @@ llvm::json::Value CreateSource(llvm::StringRef source_path) {
 //   },
 //   "required": [ "id", "name", "line", "column" ]
 // }
-llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
-                                   lldb::SBFormat &format) {
+llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
+                                   lldb::SBTarget &target) {
   llvm::json::Object object;
   int64_t frame_id = MakeDAPFrameID(frame);
   object.try_emplace("id", frame_id);
@@ -776,10 +778,18 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
 
     // Calculate the line of the current PC from the start of the current
     // symbol.
-    lldb::addr_t inst_offset = frame.GetPCAddress().GetOffset() -
-                               frame.GetSymbol().GetStartAddress().GetOffset();
-    lldb::addr_t inst_line =
-        inst_offset / (frame.GetThread().GetProcess().GetAddressByteSize() / 2);
+    lldb::SBAddress current_address = frame.GetPCAddress();
+    lldb::SBInstructionList inst_list =
+        frame.GetSymbol().GetInstructions(target);
+    size_t inst_line = 0;
+    for (size_t i = 0; i < inst_list.GetSize(); ++i) {
+      lldb::SBInstruction inst = inst_list.GetInstructionAtIndex(i);
+      if (inst.GetAddress() == current_address) {
+        inst_line = i;
+        break;
+      }
+    }
+
     // Line numbers are 1-based.
     object.try_emplace("line", inst_line + 1);
     object.try_emplace("column", 1);
diff --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h
index b8c53353bf42d..ffa10743501b8 100644
--- a/lldb/tools/lldb-dap/JSONUtils.h
+++ b/lldb/tools/lldb-dap/JSONUtils.h
@@ -363,11 +363,14 @@ llvm::json::Value CreateSource(llvm::StringRef source_path);
 ///     The LLDB format to use when populating out the "StackFrame"
 ///     object.
 ///
+/// \param[in] target
+///     The LLDB target to use when populating out the "StackFrame"
+///     object.
 /// \return
 ///     A "StackFrame" JSON object with that follows the formal JSON
 ///     definition outlined by Microsoft.
-llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
-                                   lldb::SBFormat &format);
+llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
+                                   lldb::SBTarget &target);
 
 /// Create a "StackFrame" label object for a LLDB thread.
 ///

@eronnen eronnen changed the title [lldb-dap] fix wrong assembly line number [lldb-dap] fix wrong assembly line number x64 Apr 21, 2025
@eronnen eronnen force-pushed the lldb-dap-fix-wrong-assembly-line-x64 branch from 0efcbcf to 9d9ffee Compare April 22, 2025 19:24
@eronnen
Copy link
Contributor Author

eronnen commented Apr 22, 2025

Edit: improved the patch by using target.ReadInstructions in order to calculate the line number of the current instruction

Copy link

github-actions bot commented Apr 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@eronnen eronnen force-pushed the lldb-dap-fix-wrong-assembly-line-x64 branch 3 times, most recently from 3748df8 to 3a0a962 Compare April 22, 2025 19:29
Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I'd like to have a (x86) test that would have failed without this case.

@eronnen eronnen force-pushed the lldb-dap-fix-wrong-assembly-line-x64 branch from 3a0a962 to c002260 Compare April 23, 2025 23:25
@eronnen
Copy link
Contributor Author

eronnen commented Apr 23, 2025

@JDevlieghere added a test that checks that line increments by exactly 1 each step

@eronnen eronnen force-pushed the lldb-dap-fix-wrong-assembly-line-x64 branch from c002260 to 9f285f8 Compare April 23, 2025 23:28
Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


class TestDAP_stacktrace_x86(lldbdap_testcase.DAPTestCaseBase):
@skipUnlessArch("x86_64")
@skipUnlessPlatform(["linux"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really limited to linux? It seems like this should work on at least Darwin too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like it should work, changing

@JDevlieghere JDevlieghere requested a review from ashgti April 24, 2025 03:19
)

for i in range(2, minimum_assembly_lines):
self.stepIn()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitly step with granularity='instruction'?

Copy link
Contributor Author

@eronnen eronnen Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same because it's a function without debug symbols so each step should execute one assembly instruction

@JDevlieghere JDevlieghere merged commit 480f1a4 into llvm:main Apr 25, 2025
10 checks passed
Copy link

@eronnen Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

jyli0116 pushed a commit to jyli0116/llvm-project that referenced this pull request Apr 28, 2025
Fix wrong assembly line number calculation that assumes the instruction
size is `GetAddressByteSize() / 2`

Fixes llvm#136495
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Fix wrong assembly line number calculation that assumes the instruction
size is `GetAddressByteSize() / 2`

Fixes llvm#136495
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Fix wrong assembly line number calculation that assumes the instruction
size is `GetAddressByteSize() / 2`

Fixes llvm#136495
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Fix wrong assembly line number calculation that assumes the instruction
size is `GetAddressByteSize() / 2`

Fixes llvm#136495
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Fix wrong assembly line number calculation that assumes the instruction
size is `GetAddressByteSize() / 2`

Fixes llvm#136495
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[lldb-dap] X64 assembly shows wrong lines
4 participants