Skip to content

Commit 8398ad9

Browse files
authored
[lldb] Unify the way we get the Target in CommandObject (#101208)
Currently, CommandObjects are obtaining a target in a variety of ways. Often the command incorrectly operates on the selected target. As an example, when a breakpoint command is running, the current target is passed into the command but the target that hit the breakpoint is not the selected target. In other places we use the CommandObject's execution context, which is frozen during the execution of the command, and comes with its own limitations. Finally, we often want to fall back to the dummy target if no real target is available. Instead of having to guess how to get the target, this patch introduces one helper function in CommandObject to get the most relevant target. In order of priority, that's the target from the command object's execution context, from the interpreter's execution context, the selected target or the dummy target. rdar://110846511
1 parent faf3333 commit 8398ad9

13 files changed

+98
-96
lines changed

lldb/include/lldb/Interpreter/CommandObject.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,14 @@ class CommandObject : public std::enable_shared_from_this<CommandObject> {
369369
"currently stopped.";
370370
}
371371

372-
// This is for use in the command interpreter, when you either want the
373-
// selected target, or if no target is present you want to prime the dummy
374-
// target with entities that will be copied over to new targets.
375-
Target &GetSelectedOrDummyTarget(bool prefer_dummy = false);
376-
Target &GetSelectedTarget();
377372
Target &GetDummyTarget();
378373

374+
// This is for use in the command interpreter, and returns the most relevant
375+
// target. In order of priority, that's the target from the command object's
376+
// execution context, the target from the interpreter's execution context, the
377+
// selected target or the dummy target.
378+
Target &GetTarget();
379+
379380
// If a command needs to use the "current" thread, use this call. Command
380381
// objects will have an ExecutionContext to use, and that may or may not have
381382
// a thread in it. If it does, you should use that by default, if not, then

lldb/source/Commands/CommandObjectBreakpoint.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
539539

540540
protected:
541541
void DoExecute(Args &command, CommandReturnObject &result) override {
542-
Target &target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
542+
Target &target =
543+
m_dummy_options.m_use_dummy ? GetDummyTarget() : GetTarget();
543544

544545
// The following are the various types of breakpoints that could be set:
545546
// 1). -f -l -p [-s -g] (setting breakpoint by source location)
@@ -839,7 +840,7 @@ class CommandObjectBreakpointModify : public CommandObjectParsed {
839840

840841
protected:
841842
void DoExecute(Args &command, CommandReturnObject &result) override {
842-
Target &target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
843+
Target &target = m_dummy_opts.m_use_dummy ? GetDummyTarget() : GetTarget();
843844

844845
std::unique_lock<std::recursive_mutex> lock;
845846
target.GetBreakpointList().GetListMutex(lock);
@@ -903,7 +904,7 @@ class CommandObjectBreakpointEnable : public CommandObjectParsed {
903904

904905
protected:
905906
void DoExecute(Args &command, CommandReturnObject &result) override {
906-
Target &target = GetSelectedOrDummyTarget();
907+
Target &target = GetTarget();
907908

908909
std::unique_lock<std::recursive_mutex> lock;
909910
target.GetBreakpointList().GetListMutex(lock);
@@ -1010,7 +1011,7 @@ the second re-enables the first location.");
10101011

10111012
protected:
10121013
void DoExecute(Args &command, CommandReturnObject &result) override {
1013-
Target &target = GetSelectedOrDummyTarget();
1014+
Target &target = GetTarget();
10141015
std::unique_lock<std::recursive_mutex> lock;
10151016
target.GetBreakpointList().GetListMutex(lock);
10161017

@@ -1148,7 +1149,7 @@ class CommandObjectBreakpointList : public CommandObjectParsed {
11481149

11491150
protected:
11501151
void DoExecute(Args &command, CommandReturnObject &result) override {
1151-
Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
1152+
Target &target = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();
11521153

11531154
const BreakpointList &breakpoints =
11541155
target.GetBreakpointList(m_options.m_internal);
@@ -1267,7 +1268,7 @@ class CommandObjectBreakpointClear : public CommandObjectParsed {
12671268

12681269
protected:
12691270
void DoExecute(Args &command, CommandReturnObject &result) override {
1270-
Target &target = GetSelectedOrDummyTarget();
1271+
Target &target = GetTarget();
12711272

12721273
// The following are the various types of breakpoints that could be
12731274
// cleared:
@@ -1416,7 +1417,7 @@ class CommandObjectBreakpointDelete : public CommandObjectParsed {
14161417

14171418
protected:
14181419
void DoExecute(Args &command, CommandReturnObject &result) override {
1419-
Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
1420+
Target &target = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();
14201421
result.Clear();
14211422

14221423
std::unique_lock<std::recursive_mutex> lock;
@@ -1676,7 +1677,7 @@ class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
16761677
return;
16771678
}
16781679

1679-
Target &target = GetSelectedOrDummyTarget(false);
1680+
Target &target = GetTarget();
16801681

16811682
std::unique_lock<std::recursive_mutex> lock;
16821683
target.GetBreakpointList().GetListMutex(lock);
@@ -1764,7 +1765,7 @@ class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
17641765
}
17651766

17661767
Target &target =
1767-
GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
1768+
m_name_options.m_use_dummy ? GetDummyTarget() : GetTarget();
17681769

17691770
std::unique_lock<std::recursive_mutex> lock;
17701771
target.GetBreakpointList().GetListMutex(lock);
@@ -1838,7 +1839,7 @@ class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
18381839
}
18391840

18401841
Target &target =
1841-
GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
1842+
m_name_options.m_use_dummy ? GetDummyTarget() : GetTarget();
18421843

18431844
std::unique_lock<std::recursive_mutex> lock;
18441845
target.GetBreakpointList().GetListMutex(lock);
@@ -1897,7 +1898,7 @@ class CommandObjectBreakpointNameList : public CommandObjectParsed {
18971898
protected:
18981899
void DoExecute(Args &command, CommandReturnObject &result) override {
18991900
Target &target =
1900-
GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
1901+
m_name_options.m_use_dummy ? GetDummyTarget() : GetTarget();
19011902

19021903
std::vector<std::string> name_list;
19031904
if (command.empty()) {
@@ -2209,7 +2210,7 @@ class CommandObjectBreakpointRead : public CommandObjectParsed {
22092210

22102211
protected:
22112212
void DoExecute(Args &command, CommandReturnObject &result) override {
2212-
Target &target = GetSelectedOrDummyTarget();
2213+
Target &target = GetTarget();
22132214

22142215
std::unique_lock<std::recursive_mutex> lock;
22152216
target.GetBreakpointList().GetListMutex(lock);
@@ -2319,7 +2320,7 @@ class CommandObjectBreakpointWrite : public CommandObjectParsed {
23192320

23202321
protected:
23212322
void DoExecute(Args &command, CommandReturnObject &result) override {
2322-
Target &target = GetSelectedOrDummyTarget();
2323+
Target &target = GetTarget();
23232324

23242325
std::unique_lock<std::recursive_mutex> lock;
23252326
target.GetBreakpointList().GetListMutex(lock);

lldb/source/Commands/CommandObjectBreakpointCommand.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ are no syntax errors may indicate that a function was declared but never called.
323323

324324
protected:
325325
void DoExecute(Args &command, CommandReturnObject &result) override {
326-
Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
326+
Target &target = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();
327327

328328
const BreakpointList &breakpoints = target.GetBreakpointList();
329329
size_t num_breakpoints = breakpoints.GetSize();
@@ -481,7 +481,7 @@ class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
481481

482482
protected:
483483
void DoExecute(Args &command, CommandReturnObject &result) override {
484-
Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
484+
Target &target = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();
485485

486486
const BreakpointList &breakpoints = target.GetBreakpointList();
487487
size_t num_breakpoints = breakpoints.GetSize();
@@ -548,7 +548,7 @@ class CommandObjectBreakpointCommandList : public CommandObjectParsed {
548548

549549
protected:
550550
void DoExecute(Args &command, CommandReturnObject &result) override {
551-
Target *target = &GetSelectedTarget();
551+
Target *target = &GetTarget();
552552

553553
const BreakpointList &breakpoints = target->GetBreakpointList();
554554
size_t num_breakpoints = breakpoints.GetSize();

lldb/source/Commands/CommandObjectDisassemble.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ llvm::Error CommandObjectDisassemble::CheckRangeSize(const AddressRange &range,
227227
return llvm::Error::success();
228228
StreamString msg;
229229
msg << "Not disassembling " << what << " because it is very large ";
230-
range.Dump(&msg, &GetSelectedTarget(), Address::DumpStyleLoadAddress,
230+
range.Dump(&msg, &GetTarget(), Address::DumpStyleLoadAddress,
231231
Address::DumpStyleFileAddress);
232232
msg << ". To disassemble specify an instruction count limit, start/stop "
233233
"addresses or use the --force option.";
@@ -252,7 +252,7 @@ CommandObjectDisassemble::GetContainingAddressRanges() {
252252
}
253253
};
254254

255-
Target &target = GetSelectedTarget();
255+
Target &target = GetTarget();
256256
if (!target.GetSectionLoadList().IsEmpty()) {
257257
Address symbol_containing_address;
258258
if (target.GetSectionLoadList().ResolveLoadAddress(
@@ -351,8 +351,8 @@ CommandObjectDisassemble::GetNameRanges(CommandReturnObject &result) {
351351

352352
// Find functions matching the given name.
353353
SymbolContextList sc_list;
354-
GetSelectedTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
355-
function_options, sc_list);
354+
GetTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
355+
function_options, sc_list);
356356

357357
std::vector<AddressRange> ranges;
358358
llvm::Error range_errs = llvm::Error::success();
@@ -439,7 +439,7 @@ CommandObjectDisassemble::GetRangesForSelectedMode(
439439

440440
void CommandObjectDisassemble::DoExecute(Args &command,
441441
CommandReturnObject &result) {
442-
Target *target = &GetSelectedTarget();
442+
Target *target = &GetTarget();
443443

444444
if (!m_options.arch.IsValid())
445445
m_options.arch = target->GetArchitecture();

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ void CommandObjectExpression::DoExecute(llvm::StringRef command,
605605
return;
606606

607607
if (m_repl_option.GetOptionValue().GetCurrentValue()) {
608-
Target &target = GetSelectedOrDummyTarget();
608+
Target &target = GetTarget();
609609
// Drop into REPL
610610
m_expr_lines.clear();
611611
m_expr_line_count = 0;
@@ -665,7 +665,7 @@ void CommandObjectExpression::DoExecute(llvm::StringRef command,
665665
}
666666
}
667667

668-
Target &target = GetSelectedOrDummyTarget();
668+
Target &target = GetTarget();
669669
if (EvaluateExpression(expr, result.GetOutputStream(),
670670
result.GetErrorStream(), result)) {
671671

lldb/source/Commands/CommandObjectFrame.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ may even involve JITing and running code in the target program.)");
687687
m_cmd_name);
688688

689689
// Increment statistics.
690-
TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
690+
TargetStats &target_stats = GetTarget().GetStatistics();
691691
if (result.Succeeded())
692692
target_stats.GetFrameVariableStats().NotifySuccess();
693693
else
@@ -874,13 +874,13 @@ void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
874874
RegularExpressionSP(new RegularExpression(m_options.m_module));
875875
auto func =
876876
RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
877-
GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
877+
GetTarget().GetFrameRecognizerManager().AddRecognizer(
878878
recognizer_sp, module, func, m_options.m_first_instruction_only);
879879
} else {
880880
auto module = ConstString(m_options.m_module);
881881
std::vector<ConstString> symbols(m_options.m_symbols.begin(),
882882
m_options.m_symbols.end());
883-
GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
883+
GetTarget().GetFrameRecognizerManager().AddRecognizer(
884884
recognizer_sp, module, symbols, m_options.m_first_instruction_only);
885885
}
886886
#endif
@@ -898,9 +898,7 @@ class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
898898

899899
protected:
900900
void DoExecute(Args &command, CommandReturnObject &result) override {
901-
GetSelectedOrDummyTarget()
902-
.GetFrameRecognizerManager()
903-
.RemoveAllRecognizers();
901+
GetTarget().GetFrameRecognizerManager().RemoveAllRecognizers();
904902
result.SetStatus(eReturnStatusSuccessFinishResult);
905903
}
906904
};
@@ -922,7 +920,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
922920
if (request.GetCursorIndex() != 0)
923921
return;
924922

925-
GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
923+
GetTarget().GetFrameRecognizerManager().ForEach(
926924
[&request](uint32_t rid, std::string rname, std::string module,
927925
llvm::ArrayRef<lldb_private::ConstString> symbols,
928926
bool regexp) {
@@ -953,9 +951,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
953951
return;
954952
}
955953

956-
GetSelectedOrDummyTarget()
957-
.GetFrameRecognizerManager()
958-
.RemoveAllRecognizers();
954+
GetTarget().GetFrameRecognizerManager().RemoveAllRecognizers();
959955
result.SetStatus(eReturnStatusSuccessFinishResult);
960956
return;
961957
}
@@ -973,9 +969,8 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
973969
return;
974970
}
975971

976-
if (!GetSelectedOrDummyTarget()
977-
.GetFrameRecognizerManager()
978-
.RemoveRecognizerWithID(recognizer_id)) {
972+
if (!GetTarget().GetFrameRecognizerManager().RemoveRecognizerWithID(
973+
recognizer_id)) {
979974
result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
980975
command.GetArgumentAtIndex(0));
981976
return;
@@ -996,7 +991,7 @@ class CommandObjectFrameRecognizerList : public CommandObjectParsed {
996991
protected:
997992
void DoExecute(Args &command, CommandReturnObject &result) override {
998993
bool any_printed = false;
999-
GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
994+
GetTarget().GetFrameRecognizerManager().ForEach(
1000995
[&result, &any_printed](
1001996
uint32_t recognizer_id, std::string name, std::string module,
1002997
llvm::ArrayRef<ConstString> symbols, bool regexp) {
@@ -1073,9 +1068,8 @@ class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
10731068
return;
10741069
}
10751070

1076-
auto recognizer = GetSelectedOrDummyTarget()
1077-
.GetFrameRecognizerManager()
1078-
.GetRecognizerForFrame(frame_sp);
1071+
auto recognizer =
1072+
GetTarget().GetFrameRecognizerManager().GetRecognizerForFrame(frame_sp);
10791073

10801074
Stream &output_stream = result.GetOutputStream();
10811075
output_stream.Printf("frame %d ", frame_index);

lldb/source/Commands/CommandObjectProcess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ class CommandObjectProcessHandle : public CommandObjectParsed {
15841584

15851585
protected:
15861586
void DoExecute(Args &signal_args, CommandReturnObject &result) override {
1587-
Target &target = GetSelectedOrDummyTarget();
1587+
Target &target = GetTarget();
15881588

15891589
// Any signals that are being set should be added to the Target's
15901590
// DummySignals so they will get applied on rerun, etc.

0 commit comments

Comments
 (0)