Skip to content
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

Add option to pass thread ID to thread select command #73596

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 53 additions & 6 deletions lldb/source/Commands/CommandObjectThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,44 @@ class CommandObjectThreadUntil : public CommandObjectParsed {

// CommandObjectThreadSelect

#define LLDB_OPTIONS_thread_select
#include "CommandOptions.inc"

class CommandObjectThreadSelect : public CommandObjectParsed {
public:
class CommandOptions : public Options {
public:
CommandOptions() { OptionParsingStarting(nullptr); }

~CommandOptions() override = default;

void OptionParsingStarting(ExecutionContext *execution_context) override {
m_thread_id = false;
}

Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 't': {
m_thread_id = true;
break;
}

default:
llvm_unreachable("Unimplemented option");
}

return {};
}

llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_thread_select_options);
}

bool m_thread_id;
};

CommandObjectThreadSelect(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "thread select",
"Change the currently selected thread.", nullptr,
Expand Down Expand Up @@ -1165,6 +1201,8 @@ class CommandObjectThreadSelect : public CommandObjectParsed {
nullptr);
}

Options *GetOptions() override { return &m_options; }

protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
Process *process = m_exe_ctx.GetProcessPtr();
Expand All @@ -1173,29 +1211,38 @@ class CommandObjectThreadSelect : public CommandObjectParsed {
return;
} else if (command.GetArgumentCount() != 1) {
result.AppendErrorWithFormat(
"'%s' takes exactly one thread index argument:\nUsage: %s\n",
m_cmd_name.c_str(), m_cmd_syntax.c_str());
"'%s' takes exactly one thread %s argument:\nUsage: %s\n",
m_cmd_name.c_str(), m_options.m_thread_id ? "ID" : "index",
m_cmd_syntax.c_str());
return;
}

uint32_t index_id;
if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) {
result.AppendErrorWithFormat("Invalid thread index '%s'",
result.AppendErrorWithFormat("Invalid thread %s '%s'",
m_options.m_thread_id ? "ID" : "index",
command.GetArgumentAtIndex(0));
return;
}

Thread *new_thread =
process->GetThreadList().FindThreadByIndexID(index_id).get();
Thread *new_thread = nullptr;
if (m_options.m_thread_id) {
new_thread = process->GetThreadList().FindThreadByID(index_id).get();
} else {
new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
}
if (new_thread == nullptr) {
result.AppendErrorWithFormat("invalid thread #%s.\n",
result.AppendErrorWithFormat("invalid thread %s%s.\n",
m_options.m_thread_id ? "ID " : "#",
command.GetArgumentAtIndex(0));
return;
}

process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}

CommandOptions m_options;
};

// CommandObjectThreadList
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Commands/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ let Command = "thread plan list" in {
Desc<"Display thread plans for unreported threads">;
}

let Command = "thread select" in {
def thread_thread_id : Option<"thread_id", "t">,
mdko marked this conversation as resolved.
Show resolved Hide resolved
Desc<"Provide a thread ID instead of a thread index.">;
}

let Command = "thread trace dump function calls" in {
def thread_trace_dump_function_calls_file : Option<"file", "F">, Group<1>,
Arg<"Filename">,
Expand Down
23 changes: 20 additions & 3 deletions lldb/test/API/commands/thread/select/TestThreadSelect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,34 @@ def test_invalid_arg(self):
self, "// break here", lldb.SBFileSpec("main.cpp")
)

self.expect(
"thread select -1", error=True, startstr="error: Invalid thread index '-1'"
)
self.expect(
"thread select 0x1ffffffff",
error=True,
startstr="error: Invalid thread index '0x1ffffffff'",
)
self.expect(
"thread select -t 0x1ffffffff",
error=True,
startstr="error: Invalid thread ID '0x1ffffffff'",
)
# Parses but not a valid thread id.
self.expect(
"thread select 0xffffffff",
error=True,
startstr="error: invalid thread #0xffffffff.",
)
self.expect(
"thread select -t 0xffffffff",
error=True,
startstr="error: invalid thread ID 0xffffffff.",
)

def test_thread_select_tid(self):
self.build()

lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp")
)
self.runCmd(
"thread select -t %d" % self.thread().GetThreadID(),
)
Loading