Skip to content

[lldb] Add SBThread.selected_frame property #123981

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
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
9 changes: 9 additions & 0 deletions lldb/bindings/interface/SBThreadExtensions.i
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ STRING_EXTENSION_OUTSIDE(SBThread)
for idx in range(self.GetStopReasonDataCount())
]

def set_selected_frame(self, frame):
if isinstance(frame, SBFrame):
if frame.thread != self:
raise ValueError("cannot select frame from different thread")
self.SetSelectedFrame(frame.idx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it should check that the frame really belongs to this thread before selecting a completely unrelated frame?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Callers shouldn't have to do that; StackFrames know their Threads, so SetSelectedFrame should really reject this (though in that case it ought to also return an error.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

SetSelectedFrame returns the index of the selected frame. It should check the thread and if it doesn't match, return LLDB_INVALID_FRAME_ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call, done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh my reply was to @labath, before I saw @jimingham's comments.

Copy link
Contributor Author

@kastiglione kastiglione Jan 23, 2025

Choose a reason for hiding this comment

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

I've updated this helper method to check the SBFrame argument belongs to the target instance of SBThread. If it doesn't, a ValueError is raised. Does that work for you @jimingham?

else:
self.SetSelectedFrame(frame)

id = property(GetThreadID, None, doc='''A read only property that returns the thread ID as an integer.''')
idx = property(GetIndexID, None, doc='''A read only property that returns the thread index ID as an integer. Thread index ID values start at 1 and increment as threads come and go and can be used to uniquely identify threads.''')
return_value = property(GetStopReturnValue, None, doc='''A read only property that returns an lldb object that represents the return value from the last stop (lldb.SBValue) if we just stopped due to stepping out of a function.''')
Expand All @@ -65,6 +73,7 @@ STRING_EXTENSION_OUTSIDE(SBThread)
stop_reason_data = property(get_stop_reason_data, None, doc='''A read only property that returns the stop reason data as a list.''')
is_suspended = property(IsSuspended, None, doc='''A read only property that returns a boolean value that indicates if this thread is suspended.''')
is_stopped = property(IsStopped, None, doc='''A read only property that returns a boolean value that indicates if this thread is stopped but not exited.''')
selected_frame = property(GetSelectedFrame, set_selected_frame, doc='''A read/write property that gets and sets the selected frame of this SBThread.''')
%}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_frame_recognizer_1(self):
target, process, thread, _ = lldbutil.run_to_name_breakpoint(
self, "foo", exe_name=exe
)
frame = thread.GetSelectedFrame()
frame = thread.selected_frame

# Clear internal & plugins recognizers that get initialized at launch
self.runCmd("frame recognizer clear")
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_frame_recognizer_hiding(self):
self.build()

target, process, thread, _ = lldbutil.run_to_name_breakpoint(self, "nested")
frame = thread.GetSelectedFrame()
frame = thread.selected_frame

# Sanity check.
self.expect(
Expand Down Expand Up @@ -229,7 +229,6 @@ def test_frame_recognizer_multi_symbol(self):
target, process, thread, _ = lldbutil.run_to_name_breakpoint(
self, "foo", exe_name=exe
)
frame = thread.GetSelectedFrame()

self.expect(
"frame recognizer info 0",
Expand All @@ -239,7 +238,6 @@ def test_frame_recognizer_multi_symbol(self):
target, process, thread, _ = lldbutil.run_to_name_breakpoint(
self, "bar", exe_name=exe
)
frame = thread.GetSelectedFrame()

self.expect(
"frame recognizer info 0",
Expand Down Expand Up @@ -374,7 +372,7 @@ def test_frame_recognizer_not_only_first_instruction(self):

opts = lldb.SBVariablesOptions()
opts.SetIncludeRecognizedArguments(True)
frame = thread.GetSelectedFrame()
frame = thread.selected_frame
variables = frame.GetVariables(opts)

self.assertEqual(variables.GetSize(), 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def check_local_vars(self, process: lldb.SBProcess, check_expr: bool):
# Find `bar` on the stack, then
# make sure we can read out the local
# variables (with both `frame var` and `expr`)
for f in process.GetSelectedThread().frames:
for f in process.selected_thread.frames:
frame_name = f.GetDisplayFunctionName()
if frame_name is not None and frame_name.startswith("Foo::bar"):
argv = f.GetValueForVariablePath("argv").GetChildAtIndex(0)
Expand All @@ -34,7 +34,7 @@ def check_local_vars(self, process: lldb.SBProcess, check_expr: bool):
self.assertNotEqual(strm.GetData().find("a.out"), -1)

if check_expr:
process.GetSelectedThread().SetSelectedFrame(f.idx)
process.selected_thread.selected_frame = f
self.expect_expr("this", result_type="Foo *")

@skipIf(oslist=["linux"], archs=["arm"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def test_up_down(self):
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp")
)
frame = thread.GetSelectedFrame()
frame = thread.selected_frame
# up
self.assertIn("foo", frame.GetFunctionName())
start_idx = frame.GetFrameID()
i = 0
while i < thread.GetNumFrames():
self.expect("up")
frame = thread.GetSelectedFrame()
frame = thread.selected_frame
if frame.GetFunctionName() == "main":
break
end_idx = frame.GetFrameID()
Expand All @@ -86,7 +86,7 @@ def test_up_down(self):
start_idx = frame.GetFrameID()
for i in range(1, thread.GetNumFrames()):
self.expect("down")
frame = thread.GetSelectedFrame()
frame = thread.selected_frame
if "foo" in frame.GetFunctionName():
break
end_idx = frame.GetFrameID()
Expand All @@ -99,11 +99,8 @@ def test_api(self):
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp")
)
frame = thread.GetSelectedFrame()
num_hidden = 0
for i in range(1, thread.GetNumFrames()):
thread.SetSelectedFrame(i)
frame = thread.GetSelectedFrame()
Comment on lines -104 to -106
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code didn't need to set the selected frame, it only needs to inspect whether a frame is hidden (which can be done without selecting it).

for frame in thread.frames:
if frame.IsHidden():
num_hidden += 1

Expand Down
9 changes: 3 additions & 6 deletions lldb/test/API/lang/objc/print-obj/TestPrintObj.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ def test_print_obj(self):
# We want to traverse the frame to the one corresponding to blocked.m to
# issue our 'po lock_me' command.

depth = other_thread.GetNumFrames()
for i in range(depth):
frame = other_thread.GetFrameAtIndex(i)
name = frame.GetFunctionName()
if name == "main":
other_thread.SetSelectedFrame(i)
for frame in other_thread.frames:
if frame.name == "main":
other_thread.selected_frame = frame
if self.TraceOn():
print("selected frame:" + lldbutil.get_description(frame))
break
Expand Down
Loading