[lldb] Unify target checking in CommandObject

Summary:
We currently have several CommandObjects that manually reimplement the checking for a selected target
or a target in the execution context (which is the selected target when they are invoked). This patch removes
all these checks and replaces them by setting the eCommandRequiresTarget flag that Pavel suggested. With
this flag we are doing the same check but without having to duplicate this code in all these CommandObjects.

I also added a `GetSelectedTarget()` variant of the `GetSelectedOrDummyTarget()` function to the
CommandObject that checks that the flag is set and then returns a reference to the target. I didn't rewrite
all the `target` variables from `Target *` to `Target &` in this patch as last time this change caused a lot of merge
conflicts in Swift and I would prefer having that in a separate NFC commit.

Reviewers: labath, clayborg

Reviewed By: labath, clayborg

Subscribers: clayborg, JDevlieghere, jingham, amccarth, abidh, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D66863

llvm-svn: 370571
This commit is contained in:
Raphael Isemann 2019-08-31 09:41:25 +00:00
parent d4df363b14
commit 04a4c0910b
9 changed files with 676 additions and 807 deletions

View File

@ -331,6 +331,7 @@ protected:
// 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();
// If a command needs to use the "current" thread, use this call. Command

View File

@ -589,10 +589,10 @@ private:
class CommandObjectBreakpointCommandList : public CommandObjectParsed {
public:
CommandObjectBreakpointCommandList(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "list", "List the script or set of "
"commands to be executed when "
"the breakpoint is hit.",
nullptr) {
: CommandObjectParsed(interpreter, "list",
"List the script or set of commands to be "
"executed when the breakpoint is hit.",
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandArgumentData bp_id_arg;
@ -612,14 +612,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("There is not a current executable; there are no "
"breakpoints for which to list commands");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
const BreakpointList &breakpoints = target->GetBreakpointList();
size_t num_breakpoints = breakpoints.GetSize();

View File

@ -212,20 +212,15 @@ CommandObjectDisassemble::CommandObjectDisassemble(
"Disassemble specified instructions in the current target. "
"Defaults to the current function for the current thread and "
"stack frame.",
"disassemble [<cmd-options>]"),
"disassemble [<cmd-options>]", eCommandRequiresTarget),
m_options() {}
CommandObjectDisassemble::~CommandObjectDisassemble() = default;
bool CommandObjectDisassemble::DoExecute(Args &command,
CommandReturnObject &result) {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("invalid target, create a debug target using the "
"'target create' command");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
if (!m_options.arch.IsValid())
m_options.arch = target->GetArchitecture();

View File

@ -1290,7 +1290,7 @@ public:
"Manage LLDB handling of OS signals for the "
"current target process. Defaults to showing "
"current policy.",
nullptr),
nullptr, eCommandRequiresTarget),
m_options() {
SetHelpLong("\nIf no signals are specified, update them all. If no update "
"option is specified, list the current values.");
@ -1375,15 +1375,7 @@ public:
protected:
bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
TargetSP target_sp = GetDebugger().GetSelectedTarget();
if (!target_sp) {
result.AppendError("No current target;"
" cannot handle signals until you have a valid target "
"and process.\n");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target_sp = &GetSelectedTarget();
ProcessSP process_sp = target_sp->GetProcessSP();

File diff suppressed because it is too large Load Diff

View File

@ -819,13 +819,6 @@ public:
bool DoExecute(Args &command, CommandReturnObject &result) override {
bool synchronous_execution = m_interpreter.GetSynchronous();
if (!GetDebugger().GetSelectedTarget()) {
result.AppendError("invalid target, create a debug target using the "
"'target create' command");
result.SetStatus(eReturnStatusFailed);
return false;
}
Process *process = m_exe_ctx.GetProcessPtr();
if (process == nullptr) {
result.AppendError("no process exists. Cannot continue");
@ -1091,13 +1084,7 @@ protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
bool synchronous_execution = m_interpreter.GetSynchronous();
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("invalid target, create a debug target using the "
"'target create' command");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
Process *process = m_exe_ctx.GetProcessPtr();
if (process == nullptr) {

View File

@ -40,11 +40,6 @@ static void AddWatchpointDescription(Stream *s, Watchpoint *wp,
static bool CheckTargetForWatchpointOperations(Target *target,
CommandReturnObject &result) {
if (target == nullptr) {
result.AppendError("Invalid target. No existing target or watchpoints.");
result.SetStatus(eReturnStatusFailed);
return false;
}
bool process_is_valid =
target->GetProcessSP() && target->GetProcessSP()->IsAlive();
if (!process_is_valid) {
@ -156,7 +151,8 @@ public:
CommandObjectWatchpointList(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "watchpoint list",
"List all watchpoints at configurable levels of detail.", nullptr),
"List all watchpoints at configurable levels of detail.", nullptr,
eCommandRequiresTarget),
m_options() {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
@ -217,12 +213,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("Invalid target. No current target or watchpoints.");
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return true;
}
Target *target = &GetSelectedTarget();
if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) {
uint32_t num_supported_hardware_watchpoints;
@ -292,7 +283,7 @@ public:
: CommandObjectParsed(interpreter, "enable",
"Enable the specified disabled watchpoint(s). If "
"no watchpoints are specified, enable all of them.",
nullptr) {
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
eArgTypeWatchpointIDRange);
@ -305,7 +296,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
Target *target = &GetSelectedTarget();
if (!CheckTargetForWatchpointOperations(target, result))
return false;
@ -362,7 +353,7 @@ public:
"Disable the specified watchpoint(s) without "
"removing it/them. If no watchpoints are "
"specified, disable them all.",
nullptr) {
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
eArgTypeWatchpointIDRange);
@ -375,7 +366,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
Target *target = &GetSelectedTarget();
if (!CheckTargetForWatchpointOperations(target, result))
return false;
@ -434,7 +425,7 @@ public:
: CommandObjectParsed(interpreter, "watchpoint delete",
"Delete the specified watchpoint(s). If no "
"watchpoints are specified, delete them all.",
nullptr) {
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
eArgTypeWatchpointIDRange);
@ -447,7 +438,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
Target *target = &GetSelectedTarget();
if (!CheckTargetForWatchpointOperations(target, result))
return false;
@ -511,7 +502,7 @@ public:
: CommandObjectParsed(interpreter, "watchpoint ignore",
"Set ignore count on the specified watchpoint(s). "
"If no watchpoints are specified, set them all.",
nullptr),
nullptr, eCommandRequiresTarget),
m_options() {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
@ -564,7 +555,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
Target *target = &GetSelectedTarget();
if (!CheckTargetForWatchpointOperations(target, result))
return false;
@ -631,7 +622,7 @@ public:
"If no watchpoint is specified, act on the last created "
"watchpoint. "
"Passing an empty argument clears the modification.",
nullptr),
nullptr, eCommandRequiresTarget),
m_options() {
CommandArgumentEntry arg;
CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
@ -685,7 +676,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
Target *target = &GetSelectedTarget();
if (!CheckTargetForWatchpointOperations(target, result))
return false;

View File

@ -59,7 +59,7 @@ public:
: CommandObjectParsed(interpreter, "add",
"Add a set of LLDB commands to a watchpoint, to be "
"executed whenever the watchpoint is hit.",
nullptr),
nullptr, eCommandRequiresTarget),
IOHandlerDelegateMultiline("DONE",
IOHandlerDelegate::Completion::LLDBCommand),
m_options() {
@ -389,14 +389,7 @@ are no syntax errors may indicate that a function was declared but never called.
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("There is not a current executable; there are no "
"watchpoints to which to add commands");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
const WatchpointList &watchpoints = target->GetWatchpointList();
size_t num_watchpoints = watchpoints.GetSize();
@ -486,7 +479,7 @@ public:
CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "delete",
"Delete the set of commands from a watchpoint.",
nullptr) {
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandArgumentData wp_id_arg;
@ -506,14 +499,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("There is not a current executable; there are no "
"watchpoints from which to delete commands");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
const WatchpointList &watchpoints = target->GetWatchpointList();
size_t num_watchpoints = watchpoints.GetSize();
@ -562,10 +548,10 @@ protected:
class CommandObjectWatchpointCommandList : public CommandObjectParsed {
public:
CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "list", "List the script or set of "
"commands to be executed when "
"the watchpoint is hit.",
nullptr) {
: CommandObjectParsed(interpreter, "list",
"List the script or set of commands to be executed "
"when the watchpoint is hit.",
nullptr, eCommandRequiresTarget) {
CommandArgumentEntry arg;
CommandArgumentData wp_id_arg;
@ -585,14 +571,7 @@ public:
protected:
bool DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = GetDebugger().GetSelectedTarget().get();
if (target == nullptr) {
result.AppendError("There is not a current executable; there are no "
"watchpoints for which to list commands");
result.SetStatus(eReturnStatusFailed);
return false;
}
Target *target = &GetSelectedTarget();
const WatchpointList &watchpoints = target->GetWatchpointList();
size_t num_watchpoints = watchpoints.GetSize();

View File

@ -925,6 +925,15 @@ Target &CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy) {
return *m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy);
}
Target &CommandObject::GetSelectedTarget() {
assert(m_flags.AnySet(eCommandRequiresTarget | eCommandProcessMustBePaused |
eCommandProcessMustBeLaunched | eCommandRequiresFrame |
eCommandRequiresThread | eCommandRequiresProcess |
eCommandRequiresRegContext) &&
"GetSelectedTarget called from object that may have no target");
return *m_interpreter.GetDebugger().GetSelectedTarget();
}
Thread *CommandObject::GetDefaultThread() {
Thread *thread_to_use = m_exe_ctx.GetThreadPtr();
if (thread_to_use)