This is an archive of the discontinued LLVM Phabricator instance.

[lldb/Plugins] Add lldb.ScriptedProcess.get_most_relevant_thread_index interpreter method
AbandonedPublic

Authored by mib on Jan 28 2022, 8:42 AM.

Details

Summary

This patch adds a new interpreter scripted process method to return the
most relevant thread index, which can be chosen aritrarily by the user.

This can be very helpful to ensure lldb will stop on the thread the
user judged the most relevant, in his script.

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>

Diff Detail

Event Timeline

mib created this revision.Jan 28 2022, 8:42 AM
mib requested review of this revision.Jan 28 2022, 8:42 AM

This seems wrong to me. Normal Processes don't get to choose which thread the generic "which stop event is most important" logic selects, they just report the stop events and then the higher level logic decides which one to select. It seems more symmetric to have the ScriptedThreads work the same way - after all they are free to assign whatever StopInfo they want to each stop. Why do you need to force the result in the ScriptedProcess case?

mib added a comment.Jan 28 2022, 9:50 AM

This seems wrong to me. Normal Processes don't get to choose which thread the generic "which stop event is most important" logic selects, they just report the stop events and then the higher level logic decides which one to select. It seems more symmetric to have the ScriptedThreads work the same way - after all they are free to assign whatever StopInfo they want to each stop. Why do you need to force the result in the ScriptedProcess case?

This is completely optional. The user doesn't really have to override the base method, in which case lldb won't force the ScriptedProcess to select a specific thread. It will use its current stopping logic to look at the ScriptedThreads stop reasons and determine which one it should select.

However, in some cases (i.e. crashlogs), we only have very sparse metadata to construct the ScriptedThreads, so having the ability to select the most relevant thread to the user can be quite helpful.

Also, I'm not sure a Scripted Process should share the exact same behavior as a Normal Process as they will eventually be able to report fully synthetic threads / frames, but I might be wrong on that.

My preference for things like crash reports would be that you make up a stop reason that's significant for the thread(s) you think are interesting and then let lldb figure out how to display them. After all, if you set a selected thread w/o having a StopInfo that would warrant selecting that thread, it's going to look weird. We selected thread 100, but it hadn't stopped for any particular reason is an odd experience.

And I don't think the default behavior should be to select thread 1, as that forces the upper layers hand.

mib updated this revision to Diff 404068.Jan 28 2022, 10:01 AM
mib added a reviewer: jingham.

Address @jingham concerns by making this fully optional:

  • Change python default value to 0. Thread indices in lldb are 1-based, so 0 sounds like a good error_value.
  • Check for python returned integer and replace it by LLDB_INVALID_INDEX32 to prevent ScriptedProcess::RefreshStateAfterStop to select a thread with it.
shafik added a subscriber: shafik.Jan 28 2022, 10:15 AM

Just nitpicks

lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
362 ↗(On Diff #404068)

const

lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
123 ↗(On Diff #404068)

const

mib updated this revision to Diff 404107.Jan 28 2022, 11:27 AM
mib marked 2 inline comments as done.

Address @shafik feedback

mib updated this revision to Diff 404707.Jan 31 2022, 1:47 PM
mib retitled this revision from [lldb/Plugins] Add ScriptedProcessInterface::GetSelectedThreadIndex method to [lldb/Plugins] Add lldb.ScriptedProcess.get_most_relevant_thread_index interpreter method.
mib edited the summary of this revision. (Show Details)

Following an offline discussion with @jingham, we agreed that we should leave the internal lldb machinery deal with selecting the right thread to be stopped at.

However, the interpreter ScriptedProcess method to let the user provide the most relevant thread index can still be very useful, for instance when creating the appropriate stop reason from the ScriptedThread instances to fetch the most_relevant_thread_index from ScriptedProcess.

This is why this update removes all the thread selecting logic from the ScriptedProcess plugin and makes it so get_most_relevant_thread_index cannot be accessed from the debugger.

As an implementation detail for stack_core_scripted_process this seems fine. I'm not sure whether it belongs in the generic scripted_process.py example however. If you do have reasons for keeping it there as well, I think you need to say what it means for a thread to be the "most relevant" from a scripted process's point of view.

mib abandoned this revision.Feb 9 2022, 1:16 PM

@jingham I just got rid of all of this, and added a is_stopped variable to the stack_core_scripted_process.StackCoreScriptedThread class in D118484.