Skip to content

[lldb][Target] RunThreadPlan to save/restore the ExecutionContext's frame if one exists #134097

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 4 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5080,7 +5080,13 @@ Process::RunThreadPlan(ExecutionContext &exe_ctx,
return eExpressionSetupError;
}

StackID ctx_frame_id = selected_frame_sp->GetStackID();
// If the ExecutionContext has a frame, we want to make sure to save/restore
// that frame into exe_ctx. This can happen when we run expressions from a
// non-selected SBFrame, in which case we don't want some thread-plan
// to overwrite the ExecutionContext frame.
StackID ctx_frame_id = exe_ctx.HasFrameScope()
? exe_ctx.GetFrameRef().GetStackID()
: selected_frame_sp->GetStackID();

// N.B. Running the target may unset the currently selected thread and frame.
// We don't want to do that either, so we should arrange to reset them as
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
C_SOURCES := main.c

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class ExprFromNonZeroFrame(TestBase):
NO_DEBUG_INFO_TESTCASE = True

def test(self):
"""
Tests that we can use SBFrame::EvaluateExpression on a frame
that we're not stopped in, even if thread-plans run as part of
parsing the expression (e.g., when running static initializers).
"""
self.build()

(_, _, thread, _) = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec("main.c")
)
frame = thread.GetFrameAtIndex(1)

# Using a function pointer inside the expression ensures we
# emit a ptrauth static initializer on arm64e into the JITted
# expression. The thread-plan that runs for this static
# initializer should save/restore the current execution context
# frame (which in this test is frame #1).
result = frame.EvaluateExpression("int (*fptr)() = &func; fptr()")
self.assertTrue(result.GetError().Success())
self.assertEqual(result.GetValueAsSigned(), 5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int func(void) {
__builtin_printf("Break here");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This fails to compile on Windows:
https://lab.llvm.org/buildbot/#/builders/141/builds/7573

lld-link: error: undefined symbol: printf

>>> referenced by main.o:(func)

clang: error: linker command failed with exit code 1 (use -v to see invocation)

No idea why it would complain, perhaps on some platforms the builtin use here is compiled to puts and on windows it isn't?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I fixed this in a follow-up commit. Is the test still failing?

Yea not exactly sure about why the builtin doesnt link. Your theory sounds plausible

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry yes it is building now but failing:

FAIL: test (TestExprFromNonZeroFrame.ExprFromNonZeroFrame.test)

   Tests that we can use SBFrame::EvaluateExpression on a frame

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\API\commands\expression\expr-from-non-zero-frame\TestExprFromNonZeroFrame.py", line 29, in test

    self.assertTrue(result.GetError().Success())

AssertionError: False is not true

False is indeed not true, thank you test framework /s

Copy link
Collaborator

Choose a reason for hiding this comment

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

Skipped for now: d4002b4

It could be another case where we link with link.exe which can't handle DWARF information, and that's needed for the test. We will try to confirm or deny that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

return 5;
}

int main(int argc, const char *argv[]) { return func(); }