Skip to content

Commit 34045b9

Browse files
committed
Code review changes -- update thread-id argument name, separate into separate group, and format
1 parent 97a6e23 commit 34045b9

File tree

3 files changed

+58
-34
lines changed

3 files changed

+58
-34
lines changed

lldb/source/Commands/CommandObjectThread.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,22 +1134,25 @@ class CommandObjectThreadUntil : public CommandObjectParsed {
11341134

11351135
class CommandObjectThreadSelect : public CommandObjectParsed {
11361136
public:
1137-
class CommandOptions : public Options {
1137+
class OptionGroupThreadSelect : public OptionGroup {
11381138
public:
1139-
CommandOptions() { OptionParsingStarting(nullptr); }
1139+
OptionGroupThreadSelect() { OptionParsingStarting(nullptr); }
11401140

1141-
~CommandOptions() override = default;
1141+
~OptionGroupThreadSelect() override = default;
11421142

11431143
void OptionParsingStarting(ExecutionContext *execution_context) override {
1144-
m_thread_id = false;
1144+
m_thread_id = LLDB_INVALID_THREAD_ID;
11451145
}
11461146

11471147
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
11481148
ExecutionContext *execution_context) override {
1149-
const int short_option = m_getopt_table[option_idx].val;
1149+
const int short_option = g_thread_select_options[option_idx].short_option;
11501150
switch (short_option) {
11511151
case 't': {
1152-
m_thread_id = true;
1152+
if (option_arg.getAsInteger(0, m_thread_id)) {
1153+
m_thread_id = LLDB_INVALID_THREAD_ID;
1154+
return Status("Invalid thread ID: '%s'.", option_arg.str().c_str());
1155+
}
11531156
break;
11541157
}
11551158

@@ -1164,7 +1167,7 @@ class CommandObjectThreadSelect : public CommandObjectParsed {
11641167
return llvm::ArrayRef(g_thread_select_options);
11651168
}
11661169

1167-
bool m_thread_id;
1170+
lldb::tid_t m_thread_id;
11681171
};
11691172

11701173
CommandObjectThreadSelect(CommandInterpreter &interpreter)
@@ -1179,13 +1182,17 @@ class CommandObjectThreadSelect : public CommandObjectParsed {
11791182
// Define the first (and only) variant of this arg.
11801183
thread_idx_arg.arg_type = eArgTypeThreadIndex;
11811184
thread_idx_arg.arg_repetition = eArgRepeatPlain;
1185+
thread_idx_arg.arg_opt_set_association = LLDB_OPT_SET_1;
11821186

11831187
// There is only one variant this argument could be; put it into the
11841188
// argument entry.
11851189
arg.push_back(thread_idx_arg);
11861190

11871191
// Push the data for the first argument into the m_arguments vector.
11881192
m_arguments.push_back(arg);
1193+
1194+
m_option_group.Append(&m_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_2);
1195+
m_option_group.Finalize();
11891196
}
11901197

11911198
~CommandObjectThreadSelect() override = default;
@@ -1201,48 +1208,55 @@ class CommandObjectThreadSelect : public CommandObjectParsed {
12011208
nullptr);
12021209
}
12031210

1204-
Options *GetOptions() override { return &m_options; }
1211+
Options *GetOptions() override { return &m_option_group; }
12051212

12061213
protected:
12071214
void DoExecute(Args &command, CommandReturnObject &result) override {
12081215
Process *process = m_exe_ctx.GetProcessPtr();
12091216
if (process == nullptr) {
12101217
result.AppendError("no process");
12111218
return;
1212-
} else if (command.GetArgumentCount() != 1) {
1219+
} else if (m_options.m_thread_id == LLDB_INVALID_THREAD_ID && command.GetArgumentCount() != 1) {
12131220
result.AppendErrorWithFormat(
1214-
"'%s' takes exactly one thread %s argument:\nUsage: %s\n",
1215-
m_cmd_name.c_str(), m_options.m_thread_id ? "ID" : "index",
1216-
m_cmd_syntax.c_str());
1221+
"'%s' takes exactly one thread index argument, or a thread ID option:\nUsage: %s\n",
1222+
m_cmd_name.c_str(), m_cmd_syntax.c_str());
12171223
return;
1218-
}
1219-
1220-
uint32_t index_id;
1221-
if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) {
1222-
result.AppendErrorWithFormat("Invalid thread %s '%s'",
1223-
m_options.m_thread_id ? "ID" : "index",
1224-
command.GetArgumentAtIndex(0));
1224+
} else if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID && command.GetArgumentCount() != 0) {
1225+
result.AppendErrorWithFormat(
1226+
"'%s' cannot take both a thread ID option and a thread index argument:\nUsage: %s\n",
1227+
m_cmd_name.c_str(), m_cmd_syntax.c_str());
12251228
return;
12261229
}
12271230

12281231
Thread *new_thread = nullptr;
1229-
if (m_options.m_thread_id) {
1230-
new_thread = process->GetThreadList().FindThreadByID(index_id).get();
1232+
if (command.GetArgumentCount() == 1) {
1233+
uint32_t index_id;
1234+
if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) {
1235+
result.AppendErrorWithFormat("Invalid thread index '%s'",
1236+
command.GetArgumentAtIndex(0));
1237+
return;
1238+
}
1239+
new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1240+
if (new_thread == nullptr) {
1241+
result.AppendErrorWithFormat("Invalid thread #%s.\n",
1242+
command.GetArgumentAtIndex(0));
1243+
return;
1244+
}
12311245
} else {
1232-
new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1233-
}
1234-
if (new_thread == nullptr) {
1235-
result.AppendErrorWithFormat("invalid thread %s%s.\n",
1236-
m_options.m_thread_id ? "ID " : "#",
1237-
command.GetArgumentAtIndex(0));
1238-
return;
1246+
new_thread = process->GetThreadList().FindThreadByID(m_options.m_thread_id).get();
1247+
if (new_thread == nullptr) {
1248+
result.AppendErrorWithFormat("Invalid thread ID %lu.\n",
1249+
m_options.m_thread_id);
1250+
return;
1251+
}
12391252
}
12401253

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

1245-
CommandOptions m_options;
1258+
OptionGroupThreadSelect m_options;
1259+
OptionGroupOptions m_option_group;
12461260
};
12471261

12481262
// CommandObjectThreadList

lldb/source/Commands/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,8 @@ let Command = "thread plan list" in {
11181118
}
11191119

11201120
let Command = "thread select" in {
1121-
def thread_thread_id : Option<"thread_id", "t">,
1122-
Desc<"Provide a thread ID instead of a thread index.">;
1121+
def thread_select_thread_id : Option<"thread-id", "t">, Group<2>,
1122+
Arg<"ThreadID">, Desc<"Provide a thread ID instead of a thread index.">;
11231123
}
11241124

11251125
let Command = "thread trace dump function calls" in {

lldb/test/API/commands/thread/select/TestThreadSelect.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@ def test_invalid_arg(self):
2020
self.expect(
2121
"thread select -t 0x1ffffffff",
2222
error=True,
23-
startstr="error: Invalid thread ID '0x1ffffffff'",
23+
startstr="error: Invalid thread ID",
24+
)
25+
self.expect(
26+
"thread select 1 2 3",
27+
error=True,
28+
startstr="error: 'thread select' takes exactly one thread index argument, or a thread ID option:",
29+
)
30+
self.expect(
31+
"thread select -t 1234 1",
32+
error=True,
33+
startstr="error: 'thread select' cannot take both a thread ID option and a thread index argument:",
2434
)
2535
# Parses but not a valid thread id.
2636
self.expect(
2737
"thread select 0xffffffff",
2838
error=True,
29-
startstr="error: invalid thread #0xffffffff.",
39+
startstr="error: Invalid thread #0xffffffff.",
3040
)
3141
self.expect(
3242
"thread select -t 0xffffffff",
3343
error=True,
34-
startstr="error: invalid thread ID 0xffffffff.",
44+
startstr="error: Invalid thread ID",
3545
)
3646

3747
def test_thread_select_tid(self):

0 commit comments

Comments
 (0)