[Lldb-commits] [lldb] [lldb] Unify the way we get the Target in CommandObject (PR #101208)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Jul 30 10:31:12 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Jonas Devlieghere (JDevlieghere)
<details>
<summary>Changes</summary>
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, which comes with its own limitations. And of course there's all the places where we need to use, or to fallback on the dummy target
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. This function always return the dummy target if explicitly requested.
rdar://110846511
---
Patch is 36.00 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/101208.diff
14 Files Affected:
- (modified) lldb/include/lldb/Interpreter/CommandObject.h (+6-6)
- (modified) lldb/source/Commands/CommandObjectBreakpoint.cpp (+14-17)
- (modified) lldb/source/Commands/CommandObjectBreakpointCommand.cpp (+3-3)
- (modified) lldb/source/Commands/CommandObjectDWIMPrint.cpp (+1-1)
- (modified) lldb/source/Commands/CommandObjectDisassemble.cpp (+5-5)
- (modified) lldb/source/Commands/CommandObjectExpression.cpp (+4-4)
- (modified) lldb/source/Commands/CommandObjectFrame.cpp (+11-17)
- (modified) lldb/source/Commands/CommandObjectProcess.cpp (+2-2)
- (modified) lldb/source/Commands/CommandObjectTarget.cpp (+25-25)
- (modified) lldb/source/Commands/CommandObjectThread.cpp (+1-1)
- (modified) lldb/source/Commands/CommandObjectWatchpoint.cpp (+8-8)
- (modified) lldb/source/Commands/CommandObjectWatchpointCommand.cpp (+3-3)
- (modified) lldb/source/Interpreter/CommandObject.cpp (+20-14)
- (modified) lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp (+2-2)
``````````diff
diff --git a/lldb/include/lldb/Interpreter/CommandObject.h b/lldb/include/lldb/Interpreter/CommandObject.h
index d48dbcdd5a5da..16d89f1e5db87 100644
--- a/lldb/include/lldb/Interpreter/CommandObject.h
+++ b/lldb/include/lldb/Interpreter/CommandObject.h
@@ -369,12 +369,12 @@ class CommandObject : public std::enable_shared_from_this<CommandObject> {
"currently stopped.";
}
- // This is for use in the command interpreter, when you either want the
- // selected target, or if no target is present you want to prime the dummy
- // target with entities that will be copied over to new targets.
- Target &GetSelectedOrDummyTarget(bool prefer_dummy = false);
- Target &GetSelectedTarget();
- Target &GetDummyTarget();
+ // This is for use in the command interpreter and returns 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. This function always return the dummy target if
+ // explicitly requested.
+ Target &GetTarget(bool dummy = false);
// If a command needs to use the "current" thread, use this call. Command
// objects will have an ExecutionContext to use, and that may or may not have
diff --git a/lldb/source/Commands/CommandObjectBreakpoint.cpp b/lldb/source/Commands/CommandObjectBreakpoint.cpp
index 773f8ed2fa8af..40be0cf02cd35 100644
--- a/lldb/source/Commands/CommandObjectBreakpoint.cpp
+++ b/lldb/source/Commands/CommandObjectBreakpoint.cpp
@@ -539,7 +539,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
+ Target &target = GetTarget(m_dummy_options.m_use_dummy);
// The following are the various types of breakpoints that could be set:
// 1). -f -l -p [-s -g] (setting breakpoint by source location)
@@ -747,7 +747,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
const bool show_locations = false;
bp_sp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
show_locations);
- if (&target == &GetDummyTarget())
+ if (&target == &GetTarget(/*dummy=*/true))
output_stream.Printf("Breakpoint set in dummy target, will get copied "
"into future targets.\n");
else {
@@ -839,7 +839,7 @@ class CommandObjectBreakpointModify : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
+ Target &target = GetTarget(m_dummy_opts.m_use_dummy);
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -903,7 +903,7 @@ class CommandObjectBreakpointEnable : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -1010,7 +1010,7 @@ the second re-enables the first location.");
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -1148,7 +1148,7 @@ class CommandObjectBreakpointList : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
+ Target &target = GetTarget(m_options.m_use_dummy);
const BreakpointList &breakpoints =
target.GetBreakpointList(m_options.m_internal);
@@ -1267,7 +1267,7 @@ class CommandObjectBreakpointClear : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
// The following are the various types of breakpoints that could be
// cleared:
@@ -1416,7 +1416,7 @@ class CommandObjectBreakpointDelete : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
+ Target &target = GetTarget(m_options.m_use_dummy);
result.Clear();
std::unique_lock<std::recursive_mutex> lock;
@@ -1676,7 +1676,7 @@ class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
return;
}
- Target &target = GetSelectedOrDummyTarget(false);
+ Target &target = GetTarget(false);
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -1763,8 +1763,7 @@ class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
return;
}
- Target &target =
- GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
+ Target &target = GetTarget(m_name_options.m_use_dummy.GetCurrentValue());
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -1837,8 +1836,7 @@ class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
return;
}
- Target &target =
- GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
+ Target &target = GetTarget(m_name_options.m_use_dummy.GetCurrentValue());
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -1896,8 +1894,7 @@ class CommandObjectBreakpointNameList : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target =
- GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
+ Target &target = GetTarget(m_name_options.m_use_dummy.GetCurrentValue());
std::vector<std::string> name_list;
if (command.empty()) {
@@ -2209,7 +2206,7 @@ class CommandObjectBreakpointRead : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
@@ -2319,7 +2316,7 @@ class CommandObjectBreakpointWrite : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
diff --git a/lldb/source/Commands/CommandObjectBreakpointCommand.cpp b/lldb/source/Commands/CommandObjectBreakpointCommand.cpp
index 6ebe6e8a35570..fb352e70c2355 100644
--- a/lldb/source/Commands/CommandObjectBreakpointCommand.cpp
+++ b/lldb/source/Commands/CommandObjectBreakpointCommand.cpp
@@ -323,7 +323,7 @@ are no syntax errors may indicate that a function was declared but never called.
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
+ Target &target = GetTarget(m_options.m_use_dummy);
const BreakpointList &breakpoints = target.GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();
@@ -481,7 +481,7 @@ class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
+ Target &target = GetTarget(m_options.m_use_dummy);
const BreakpointList &breakpoints = target.GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();
@@ -548,7 +548,7 @@ class CommandObjectBreakpointCommandList : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
const BreakpointList &breakpoints = target->GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index b7cd955e00203..35db422121a20 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -77,7 +77,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
Target *target_ptr = m_exe_ctx.GetTargetPtr();
// Fallback to the dummy target, which can allow for expression evaluation.
- Target &target = target_ptr ? *target_ptr : GetDummyTarget();
+ Target &target = target_ptr ? *target_ptr : GetTarget(/*dummy=*/true);
EvaluateExpressionOptions eval_options =
m_expr_options.GetEvaluateExpressionOptions(target, m_varobj_options);
diff --git a/lldb/source/Commands/CommandObjectDisassemble.cpp b/lldb/source/Commands/CommandObjectDisassemble.cpp
index d975e39801317..8ec55cc120725 100644
--- a/lldb/source/Commands/CommandObjectDisassemble.cpp
+++ b/lldb/source/Commands/CommandObjectDisassemble.cpp
@@ -227,7 +227,7 @@ llvm::Error CommandObjectDisassemble::CheckRangeSize(const AddressRange &range,
return llvm::Error::success();
StreamString msg;
msg << "Not disassembling " << what << " because it is very large ";
- range.Dump(&msg, &GetSelectedTarget(), Address::DumpStyleLoadAddress,
+ range.Dump(&msg, &GetTarget(), Address::DumpStyleLoadAddress,
Address::DumpStyleFileAddress);
msg << ". To disassemble specify an instruction count limit, start/stop "
"addresses or use the --force option.";
@@ -252,7 +252,7 @@ CommandObjectDisassemble::GetContainingAddressRanges() {
}
};
- Target &target = GetSelectedTarget();
+ Target &target = GetTarget();
if (!target.GetSectionLoadList().IsEmpty()) {
Address symbol_containing_address;
if (target.GetSectionLoadList().ResolveLoadAddress(
@@ -351,8 +351,8 @@ CommandObjectDisassemble::GetNameRanges(CommandReturnObject &result) {
// Find functions matching the given name.
SymbolContextList sc_list;
- GetSelectedTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
- function_options, sc_list);
+ GetTarget().GetImages().FindFunctions(name, eFunctionNameTypeAuto,
+ function_options, sc_list);
std::vector<AddressRange> ranges;
llvm::Error range_errs = llvm::Error::success();
@@ -439,7 +439,7 @@ CommandObjectDisassemble::GetRangesForSelectedMode(
void CommandObjectDisassemble::DoExecute(Args &command,
CommandReturnObject &result) {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
if (!m_options.arch.IsValid())
m_options.arch = target->GetArchitecture();
diff --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index eb76753d98efc..a91e6a08d9933 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -344,7 +344,7 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
return;
Target *exe_target = exe_ctx.GetTargetPtr();
- Target &target = exe_target ? *exe_target : GetDummyTarget();
+ Target &target = exe_target ? *exe_target : GetTarget(/*dummy=*/true);
unsigned cursor_pos = request.GetRawCursorPos();
// Get the full user input including the suffix. The suffix is necessary
@@ -407,7 +407,7 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
// that use an input reader...
ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Target *exe_target = exe_ctx.GetTargetPtr();
- Target &target = exe_target ? *exe_target : GetDummyTarget();
+ Target &target = exe_target ? *exe_target : GetTarget(/*dummy=*/true);
lldb::ValueObjectSP result_valobj_sp;
StackFrame *frame = exe_ctx.GetFramePtr();
@@ -605,7 +605,7 @@ void CommandObjectExpression::DoExecute(llvm::StringRef command,
return;
if (m_repl_option.GetOptionValue().GetCurrentValue()) {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
// Drop into REPL
m_expr_lines.clear();
m_expr_line_count = 0;
@@ -665,7 +665,7 @@ void CommandObjectExpression::DoExecute(llvm::StringRef command,
}
}
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
if (EvaluateExpression(expr, result.GetOutputStream(),
result.GetErrorStream(), result)) {
diff --git a/lldb/source/Commands/CommandObjectFrame.cpp b/lldb/source/Commands/CommandObjectFrame.cpp
index 3f4178c1a9595..29e460fe3885f 100644
--- a/lldb/source/Commands/CommandObjectFrame.cpp
+++ b/lldb/source/Commands/CommandObjectFrame.cpp
@@ -687,7 +687,7 @@ may even involve JITing and running code in the target program.)");
m_cmd_name);
// Increment statistics.
- TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
+ TargetStats &target_stats = GetTarget().GetStatistics();
if (result.Succeeded())
target_stats.GetFrameVariableStats().NotifySuccess();
else
@@ -874,13 +874,13 @@ void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
RegularExpressionSP(new RegularExpression(m_options.m_module));
auto func =
RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
- GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
+ GetTarget().GetFrameRecognizerManager().AddRecognizer(
recognizer_sp, module, func, m_options.m_first_instruction_only);
} else {
auto module = ConstString(m_options.m_module);
std::vector<ConstString> symbols(m_options.m_symbols.begin(),
m_options.m_symbols.end());
- GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
+ GetTarget().GetFrameRecognizerManager().AddRecognizer(
recognizer_sp, module, symbols, m_options.m_first_instruction_only);
}
#endif
@@ -898,9 +898,7 @@ class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- GetSelectedOrDummyTarget()
- .GetFrameRecognizerManager()
- .RemoveAllRecognizers();
+ GetTarget().GetFrameRecognizerManager().RemoveAllRecognizers();
result.SetStatus(eReturnStatusSuccessFinishResult);
}
};
@@ -922,7 +920,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
if (request.GetCursorIndex() != 0)
return;
- GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
+ GetTarget().GetFrameRecognizerManager().ForEach(
[&request](uint32_t rid, std::string rname, std::string module,
llvm::ArrayRef<lldb_private::ConstString> symbols,
bool regexp) {
@@ -953,9 +951,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
return;
}
- GetSelectedOrDummyTarget()
- .GetFrameRecognizerManager()
- .RemoveAllRecognizers();
+ GetTarget().GetFrameRecognizerManager().RemoveAllRecognizers();
result.SetStatus(eReturnStatusSuccessFinishResult);
return;
}
@@ -973,9 +969,8 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
return;
}
- if (!GetSelectedOrDummyTarget()
- .GetFrameRecognizerManager()
- .RemoveRecognizerWithID(recognizer_id)) {
+ if (!GetTarget().GetFrameRecognizerManager().RemoveRecognizerWithID(
+ recognizer_id)) {
result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
command.GetArgumentAtIndex(0));
return;
@@ -996,7 +991,7 @@ class CommandObjectFrameRecognizerList : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
bool any_printed = false;
- GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
+ GetTarget().GetFrameRecognizerManager().ForEach(
[&result, &any_printed](
uint32_t recognizer_id, std::string name, std::string module,
llvm::ArrayRef<ConstString> symbols, bool regexp) {
@@ -1073,9 +1068,8 @@ class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
return;
}
- auto recognizer = GetSelectedOrDummyTarget()
- .GetFrameRecognizerManager()
- .GetRecognizerForFrame(frame_sp);
+ auto recognizer =
+ GetTarget().GetFrameRecognizerManager().GetRecognizerForFrame(frame_sp);
Stream &output_stream = result.GetOutputStream();
output_stream.Printf("frame %d ", frame_index);
diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp
index e605abdb3c771..fe5c5041af2d1 100644
--- a/lldb/source/Commands/CommandObjectProcess.cpp
+++ b/lldb/source/Commands/CommandObjectProcess.cpp
@@ -1584,7 +1584,7 @@ class CommandObjectProcessHandle : public CommandObjectParsed {
protected:
void DoExecute(Args &signal_args, CommandReturnObject &result) override {
- Target &target = GetSelectedOrDummyTarget();
+ Target &target = GetTarget();
// Any signals that are being set should be added to the Target's
// DummySignals so they will get applied on rerun, etc.
@@ -1662,7 +1662,7 @@ class CommandObjectProcessHandle : public CommandObjectParsed {
if (m_options.do_clear) {
target.ClearDummySignals(signal_args);
if (m_options.dummy)
- GetDummyTarget().ClearDummySignals(signal_args);
+ GetTarget(/*dummy=*/true).ClearDummySignals(signal_args);
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return;
}
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index cc381a2ecee1d..3e077f00e59d2 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -1027,7 +1027,7 @@ class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
const size_t argc = command.GetArgumentCount();
if (argc & 1) {
result.AppendError("add requires an even number of arguments\n");
@@ -1074,7 +1074,7 @@ class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
bool notify = true;
target->GetImageSearchPathList().Clear(notify);
result.SetStatus(eReturnStatusSuccessFinishNoResult);
@@ -1148,7 +1148,7 @@ class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
size_t argc = command.GetArgumentCount();
// check for at least 3 arguments and an odd number of parameters
if (argc >= 3 && argc & 1) {
@@ -1203,7 +1203,7 @@ class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed {
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
- Target *target = &GetSelectedTarget();
+ Target *target = &GetTarget();
target->GetImageSearchPathList().Dump(&result.GetOutputStream());
result.SetStatus(eReturnStatusSuccessFinishResult);
@@ -1226,7 +1226,7 @@ class CommandObjectTargetModulesSearchPathsQu...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/101208
More information about the lldb-commits
mailing list