Skip to content

[lldb] Unify the way we get the Target in CommandObject #101208

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

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions lldb/include/lldb/Interpreter/CommandObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,14 @@ 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, the target from the interpreter's execution context, the
// selected target or the dummy target.
Target &GetTarget();

// 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
// a thread in it. If it does, you should use that by default, if not, then
Expand Down
27 changes: 14 additions & 13 deletions lldb/source/Commands/CommandObjectBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {

protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
Target &target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
Target &target =
m_dummy_options.m_use_dummy ? GetDummyTarget() : GetTarget();

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

protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
Target &target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
Target &target = m_dummy_opts.m_use_dummy ? GetDummyTarget() : GetTarget();

std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
Expand Down Expand Up @@ -903,7 +904,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);
Expand Down Expand Up @@ -1010,7 +1011,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);

Expand Down Expand Up @@ -1148,7 +1149,7 @@ class CommandObjectBreakpointList : public CommandObjectParsed {

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

const BreakpointList &breakpoints =
target.GetBreakpointList(m_options.m_internal);
Expand Down Expand Up @@ -1267,7 +1268,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:
Expand Down Expand Up @@ -1416,7 +1417,7 @@ class CommandObjectBreakpointDelete : public CommandObjectParsed {

protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
Target &target = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();
result.Clear();

std::unique_lock<std::recursive_mutex> lock;
Expand Down Expand Up @@ -1676,7 +1677,7 @@ class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
return;
}

Target &target = GetSelectedOrDummyTarget(false);
Target &target = GetTarget();

std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
Expand Down Expand Up @@ -1764,7 +1765,7 @@ class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
}

Target &target =
GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
m_name_options.m_use_dummy ? GetDummyTarget() : GetTarget();

std::unique_lock<std::recursive_mutex> lock;
target.GetBreakpointList().GetListMutex(lock);
Expand Down Expand Up @@ -1838,7 +1839,7 @@ class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
}

Target &target =
GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
m_name_options.m_use_dummy ? GetDummyTarget() : GetTarget();

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

std::vector<std::string> name_list;
if (command.empty()) {
Expand Down Expand Up @@ -2209,7 +2210,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);
Expand Down Expand Up @@ -2319,7 +2320,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);
Expand Down
6 changes: 3 additions & 3 deletions lldb/source/Commands/CommandObjectBreakpointCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();

const BreakpointList &breakpoints = target.GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();
Expand Down Expand Up @@ -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 = m_options.m_use_dummy ? GetDummyTarget() : GetTarget();

const BreakpointList &breakpoints = target.GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions lldb/source/Commands/CommandObjectDisassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -252,7 +252,7 @@ CommandObjectDisassemble::GetContainingAddressRanges() {
}
};

Target &target = GetSelectedTarget();
Target &target = GetTarget();
if (!target.GetSectionLoadList().IsEmpty()) {
Address symbol_containing_address;
if (target.GetSectionLoadList().ResolveLoadAddress(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Commands/CommandObjectExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -665,7 +665,7 @@ void CommandObjectExpression::DoExecute(llvm::StringRef command,
}
}

Target &target = GetSelectedOrDummyTarget();
Target &target = GetTarget();
if (EvaluateExpression(expr, result.GetOutputStream(),
result.GetErrorStream(), result)) {

Expand Down
28 changes: 11 additions & 17 deletions lldb/source/Commands/CommandObjectFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
};
Expand All @@ -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) {
Expand Down Expand Up @@ -953,9 +951,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
return;
}

GetSelectedOrDummyTarget()
.GetFrameRecognizerManager()
.RemoveAllRecognizers();
GetTarget().GetFrameRecognizerManager().RemoveAllRecognizers();
result.SetStatus(eReturnStatusSuccessFinishResult);
return;
}
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandObjectProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading
Loading