Skip to content

Commit

Permalink
[lldb] Unify target checking in CommandObject
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Teemperor committed Aug 31, 2019
1 parent d4df363 commit 04a4c09
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 800 deletions.
1 change: 1 addition & 0 deletions lldb/include/lldb/Interpreter/CommandObject.h
Expand Up @@ -331,6 +331,7 @@ class CommandObject {
// 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
Expand Down
17 changes: 5 additions & 12 deletions lldb/source/Commands/CommandObjectBreakpointCommand.cpp
Expand Up @@ -589,10 +589,10 @@ class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
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;

Expand All @@ -612,14 +612,7 @@ class CommandObjectBreakpointCommandList : public CommandObjectParsed {

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();
Expand Down
11 changes: 3 additions & 8 deletions lldb/source/Commands/CommandObjectDisassemble.cpp
Expand Up @@ -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();

Expand Down
12 changes: 2 additions & 10 deletions lldb/source/Commands/CommandObjectProcess.cpp
Expand Up @@ -1290,7 +1290,7 @@ class CommandObjectProcessHandle : public CommandObjectParsed {
"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.");
Expand Down Expand Up @@ -1375,15 +1375,7 @@ class CommandObjectProcessHandle : public CommandObjectParsed {

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();

Expand Down

0 comments on commit 04a4c09

Please sign in to comment.