Modify "platform connect" to connect to processes as well
The standard remote debugging workflow with gdb is to start the
application on the remote host under gdbserver (e.g.: gdbserver :5039
a.out) and then connect to it with gdb.
The same workflow is supported by debugserver/lldb-gdbserver with a very
similar syntax but to access all features of lldb we need to be
connected also to an lldb-platform instance running on the target.
Before this change this had to be done manually with starting a separate
lldb-platform on the target machine and then connecting to it with lldb
before connecting to the process.
This change modifies the behavior of "platform connect" with
automatically connecting to the process instance if it was started by
the remote platform. With this command replacing gdbserver in a gdb
based worflow is usually as simple as replacing the command to execute
gdbserver with executing lldb-platform.
Differential revision: http://reviews.llvm.org/D14952
llvm-svn: 255016
diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp
index 293b762..b7f894f 100644
--- a/lldb/source/Commands/CommandObjectProcess.cpp
+++ b/lldb/source/Commands/CommandObjectProcess.cpp
@@ -1051,78 +1051,46 @@
bool
DoExecute (Args& command, CommandReturnObject &result) override
{
-
- TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
- Error error;
- Process *process = m_exe_ctx.GetProcessPtr();
- if (process)
+ if (command.GetArgumentCount() != 1)
{
- if (process->IsAlive())
- {
- result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
- process->GetID());
- result.SetStatus (eReturnStatusFailed);
- return false;
- }
- }
-
- if (!target_sp)
- {
- // If there isn't a current target create one.
-
- error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
- NULL,
- NULL,
- false,
- NULL, // No platform options
- target_sp);
- if (!target_sp || error.Fail())
- {
- result.AppendError(error.AsCString("Error creating target"));
- result.SetStatus (eReturnStatusFailed);
- return false;
- }
- m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
- }
-
- if (command.GetArgumentCount() == 1)
- {
- const char *plugin_name = NULL;
- if (!m_options.plugin_name.empty())
- plugin_name = m_options.plugin_name.c_str();
-
- const char *remote_url = command.GetArgumentAtIndex(0);
- process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
-
- if (process)
- {
- error = process->ConnectRemote (process->GetTarget().GetDebugger().GetOutputFile().get(), remote_url);
-
- if (error.Fail())
- {
- result.AppendError(error.AsCString("Remote connect failed"));
- result.SetStatus (eReturnStatusFailed);
- target_sp->DeleteCurrentProcess();
- return false;
- }
- }
- else
- {
- result.AppendErrorWithFormat ("Unable to find process plug-in for remote URL '%s'.\nPlease specify a process plug-in name with the --plugin option, or specify an object file using the \"file\" command.\n",
- remote_url);
- result.SetStatus (eReturnStatusFailed);
- }
- }
- else
- {
- result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
+ result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
m_cmd_name.c_str(),
m_cmd_syntax.c_str());
result.SetStatus (eReturnStatusFailed);
+ return false;
}
- return result.Succeeded();
+
+
+ Process *process = m_exe_ctx.GetProcessPtr();
+ if (process && process->IsAlive())
+ {
+ result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
+ process->GetID());
+ result.SetStatus (eReturnStatusFailed);
+ return false;
+ }
+
+ const char *plugin_name = nullptr;
+ if (!m_options.plugin_name.empty())
+ plugin_name = m_options.plugin_name.c_str();
+
+ Error error;
+ Debugger& debugger = m_interpreter.GetDebugger();
+ PlatformSP platform_sp = m_interpreter.GetPlatform(true);
+ ProcessSP process_sp = platform_sp->ConnectProcess(command.GetArgumentAtIndex(0),
+ plugin_name,
+ debugger,
+ debugger.GetSelectedTarget().get(),
+ error);
+ if (error.Fail() || process_sp == nullptr)
+ {
+ result.AppendError(error.AsCString("Error connecting to the process"));
+ result.SetStatus (eReturnStatusFailed);
+ return false;
+ }
+ return true;
}
-
+
CommandOptions m_options;
};