Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "CommandObjectProcess.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
| 17 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/State.h" |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 19 | #include "lldb/Host/Host.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Platform.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/Target.h" |
| 25 | #include "lldb/Target/Thread.h" |
| 26 | |
| 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
| 30 | //------------------------------------------------------------------------- |
| 31 | // CommandObjectProcessLaunch |
| 32 | //------------------------------------------------------------------------- |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 33 | #pragma mark CommandObjectProcessLaunch |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 34 | class CommandObjectProcessLaunch : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 38 | CommandObjectProcessLaunch (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 39 | CommandObjectParsed (interpreter, |
| 40 | "process launch", |
| 41 | "Launch the executable in the debugger.", |
| 42 | NULL), |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 43 | m_options (interpreter) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 45 | CommandArgumentEntry arg; |
| 46 | CommandArgumentData run_args_arg; |
| 47 | |
| 48 | // Define the first (and only) variant of this arg. |
| 49 | run_args_arg.arg_type = eArgTypeRunArgs; |
| 50 | run_args_arg.arg_repetition = eArgRepeatOptional; |
| 51 | |
| 52 | // There is only one variant this argument could be; put it into the argument entry. |
| 53 | arg.push_back (run_args_arg); |
| 54 | |
| 55 | // Push the data for the first argument into the m_arguments vector. |
| 56 | m_arguments.push_back (arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | ~CommandObjectProcessLaunch () |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | Options * |
| 65 | GetOptions () |
| 66 | { |
| 67 | return &m_options; |
| 68 | } |
| 69 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 70 | virtual const char *GetRepeatCommand (Args ¤t_command_args, uint32_t index) |
| 71 | { |
| 72 | // No repeat for "process launch"... |
| 73 | return ""; |
| 74 | } |
| 75 | |
| 76 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 78 | DoExecute (Args& launch_args, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 80 | Debugger &debugger = m_interpreter.GetDebugger(); |
| 81 | Target *target = debugger.GetSelectedTarget().get(); |
| 82 | Error error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | |
| 84 | if (target == NULL) |
| 85 | { |
Greg Clayton | e1f50b9 | 2011-05-03 22:09:39 +0000 | [diff] [blame] | 86 | result.AppendError ("invalid target, create a debug target using the 'target create' command"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | result.SetStatus (eReturnStatusFailed); |
| 88 | return false; |
| 89 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | // If our listener is NULL, users aren't allows to launch |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | char filename[PATH_MAX]; |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 92 | const Module *exe_module = target->GetExecutableModulePointer(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 93 | |
| 94 | if (exe_module == NULL) |
| 95 | { |
Greg Clayton | e1f50b9 | 2011-05-03 22:09:39 +0000 | [diff] [blame] | 96 | result.AppendError ("no file in target, create a debug target using the 'target create' command"); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 97 | result.SetStatus (eReturnStatusFailed); |
| 98 | return false; |
| 99 | } |
| 100 | |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 101 | exe_module->GetFileSpec().GetPath (filename, sizeof(filename)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 103 | const bool add_exe_file_as_first_arg = true; |
Greg Clayton | 1d1f39e | 2011-11-29 04:03:30 +0000 | [diff] [blame] | 104 | m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg); |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 105 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 106 | StateType state = eStateInvalid; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 107 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 108 | if (process) |
| 109 | { |
| 110 | state = process->GetState(); |
| 111 | |
| 112 | if (process->IsAlive() && state != eStateConnected) |
| 113 | { |
| 114 | char message[1024]; |
| 115 | if (process->GetState() == eStateAttaching) |
| 116 | ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message)); |
| 117 | else |
| 118 | ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message)); |
| 119 | |
| 120 | if (!m_interpreter.Confirm (message, true)) |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 121 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 122 | result.SetStatus (eReturnStatusFailed); |
| 123 | return false; |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 124 | } |
| 125 | else |
| 126 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 127 | Error destroy_error (process->Destroy()); |
| 128 | if (destroy_error.Success()) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 129 | { |
| 130 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 131 | } |
| 132 | else |
| 133 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 134 | result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 135 | result.SetStatus (eReturnStatusFailed); |
| 136 | } |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 140 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 141 | if (launch_args.GetArgumentCount() == 0) |
| 142 | { |
| 143 | const Args &process_args = target->GetRunArguments(); |
| 144 | if (process_args.GetArgumentCount() > 0) |
| 145 | m_options.launch_info.GetArguments().AppendArguments (process_args); |
| 146 | } |
| 147 | else |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 148 | { |
Greg Clayton | 3e6f2cc | 2011-11-21 21:51:18 +0000 | [diff] [blame] | 149 | // Save the arguments for subsequent runs in the current target. |
| 150 | target->SetRunArguments (launch_args); |
| 151 | |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 152 | m_options.launch_info.GetArguments().AppendArguments (launch_args); |
| 153 | } |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 154 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 155 | if (target->GetDisableASLR()) |
| 156 | m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 157 | |
| 158 | if (target->GetDisableSTDIO()) |
| 159 | m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO); |
| 160 | |
| 161 | m_options.launch_info.GetFlags().Set (eLaunchFlagDebug); |
| 162 | |
| 163 | Args environment; |
| 164 | target->GetEnvironmentAsArgs (environment); |
| 165 | if (environment.GetArgumentCount() > 0) |
| 166 | m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment); |
| 167 | |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 168 | // Finalize the file actions, and if none were given, default to opening |
| 169 | // up a pseudo terminal |
| 170 | const bool default_to_use_pty = true; |
| 171 | m_options.launch_info.FinalizeFileActions (target, default_to_use_pty); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 172 | |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 173 | if (state == eStateConnected) |
| 174 | { |
| 175 | if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY)) |
| 176 | { |
| 177 | result.AppendWarning("can't launch in tty when launching through a remote connection"); |
| 178 | m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY); |
| 179 | } |
| 180 | } |
| 181 | else |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 182 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 183 | if (!m_options.launch_info.GetArchitecture().IsValid()) |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 184 | m_options.launch_info.GetArchitecture() = target->GetArchitecture(); |
| 185 | |
Greg Clayton | 75d8c25 | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 186 | PlatformSP platform_sp (target->GetPlatform()); |
| 187 | |
| 188 | if (platform_sp && platform_sp->CanDebugProcess ()) |
| 189 | { |
| 190 | process = target->GetPlatform()->DebugProcess (m_options.launch_info, |
| 191 | debugger, |
| 192 | target, |
| 193 | debugger.GetListener(), |
| 194 | error).get(); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | const char *plugin_name = m_options.launch_info.GetProcessPluginName(); |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 199 | process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get(); |
Greg Clayton | 75d8c25 | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 200 | if (process) |
| 201 | error = process->Launch (m_options.launch_info); |
| 202 | } |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 203 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 204 | if (process == NULL) |
| 205 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 206 | result.SetError (error, "failed to launch or debug process"); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 207 | return false; |
| 208 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | } |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 210 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 211 | if (error.Success()) |
| 212 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 213 | const char *archname = exe_module->GetArchitecture().GetArchitectureName(); |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 214 | |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 215 | result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname); |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 216 | result.SetDidChangeProcessState (true); |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 217 | if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false) |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 218 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 219 | result.SetStatus (eReturnStatusSuccessContinuingNoResult); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 220 | StateType state = process->WaitForProcessToStop (NULL); |
| 221 | |
| 222 | if (state == eStateStopped) |
| 223 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 224 | error = process->Resume(); |
| 225 | if (error.Success()) |
| 226 | { |
| 227 | bool synchronous_execution = m_interpreter.GetSynchronous (); |
| 228 | if (synchronous_execution) |
| 229 | { |
| 230 | state = process->WaitForProcessToStop (NULL); |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 231 | const bool must_be_alive = true; |
| 232 | if (!StateIsStoppedState(state, must_be_alive)) |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 233 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 234 | result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state)); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 235 | } |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 236 | result.SetDidChangeProcessState (true); |
| 237 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | result.SetStatus (eReturnStatusSuccessContinuingNoResult); |
| 242 | } |
| 243 | } |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 244 | else |
| 245 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 246 | result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString()); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 247 | result.SetStatus (eReturnStatusFailed); |
| 248 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 249 | } |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 250 | else |
| 251 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 252 | result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state)); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 253 | result.SetStatus (eReturnStatusFailed); |
| 254 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 257 | else |
| 258 | { |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 259 | result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString()); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 260 | result.SetStatus (eReturnStatusFailed); |
| 261 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | return result.Succeeded(); |
| 264 | } |
| 265 | |
| 266 | protected: |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 267 | ProcessLaunchCommandOptions m_options; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 271 | //#define SET1 LLDB_OPT_SET_1 |
| 272 | //#define SET2 LLDB_OPT_SET_2 |
| 273 | //#define SET3 LLDB_OPT_SET_3 |
| 274 | // |
| 275 | //OptionDefinition |
| 276 | //CommandObjectProcessLaunch::CommandOptions::g_option_table[] = |
| 277 | //{ |
| 278 | //{ SET1 | SET2 | SET3, false, "stop-at-entry", 's', no_argument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."}, |
| 279 | //{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."}, |
| 280 | //{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."}, |
| 281 | //{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."}, |
| 282 | //{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 283 | //{ SET2 , false, "tty", 't', optional_argument, NULL, 0, eArgTypePath, "Start the process in a terminal. If <path> is specified, look for a terminal whose name contains <path>, else start the process in a new terminal."}, |
| 284 | //{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."}, |
| 285 | //{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."}, |
| 286 | //{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 287 | //}; |
| 288 | // |
| 289 | //#undef SET1 |
| 290 | //#undef SET2 |
| 291 | //#undef SET3 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | |
| 293 | //------------------------------------------------------------------------- |
| 294 | // CommandObjectProcessAttach |
| 295 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 296 | #pragma mark CommandObjectProcessAttach |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 297 | class CommandObjectProcessAttach : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | { |
| 299 | public: |
| 300 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | class CommandOptions : public Options |
| 302 | { |
| 303 | public: |
| 304 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 305 | CommandOptions (CommandInterpreter &interpreter) : |
| 306 | Options(interpreter) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 308 | // Keep default values of all options in one place: OptionParsingStarting () |
| 309 | OptionParsingStarting (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | ~CommandOptions () |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 317 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | { |
| 319 | Error error; |
| 320 | char short_option = (char) m_getopt_table[option_idx].val; |
| 321 | bool success = false; |
| 322 | switch (short_option) |
| 323 | { |
Johnny Chen | 7c09997 | 2012-05-24 00:43:00 +0000 | [diff] [blame] | 324 | case 'c': |
| 325 | attach_info.SetContinueOnceAttached(true); |
| 326 | break; |
| 327 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | case 'p': |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 330 | lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success); |
| 331 | if (!success || pid == LLDB_INVALID_PROCESS_ID) |
| 332 | { |
| 333 | error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | attach_info.SetProcessID (pid); |
| 338 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 339 | } |
| 340 | break; |
| 341 | |
| 342 | case 'P': |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 343 | attach_info.SetProcessPluginName (option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 344 | break; |
| 345 | |
| 346 | case 'n': |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 347 | attach_info.GetExecutableFile().SetFile(option_arg, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | break; |
| 349 | |
| 350 | case 'w': |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 351 | attach_info.SetWaitForLaunch(true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | break; |
Jim Ingham | 3a458eb | 2012-07-20 21:37:13 +0000 | [diff] [blame^] | 353 | |
| 354 | case 'i': |
| 355 | attach_info.SetIgnoreExisting(false); |
| 356 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | |
| 358 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 359 | error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | return error; |
| 363 | } |
| 364 | |
| 365 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 366 | OptionParsingStarting () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 368 | attach_info.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 371 | const OptionDefinition* |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | GetDefinitions () |
| 373 | { |
| 374 | return g_option_table; |
| 375 | } |
| 376 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 377 | virtual bool |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 378 | HandleOptionArgumentCompletion (Args &input, |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 379 | int cursor_index, |
| 380 | int char_pos, |
| 381 | OptionElementVector &opt_element_vector, |
| 382 | int opt_element_index, |
| 383 | int match_start_point, |
| 384 | int max_return_elements, |
| 385 | bool &word_complete, |
| 386 | StringList &matches) |
| 387 | { |
| 388 | int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; |
| 389 | int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; |
| 390 | |
| 391 | // We are only completing the name option for now... |
| 392 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 393 | const OptionDefinition *opt_defs = GetDefinitions(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 394 | if (opt_defs[opt_defs_index].short_option == 'n') |
| 395 | { |
| 396 | // Are we in the name? |
| 397 | |
| 398 | // Look to see if there is a -P argument provided, and if so use that plugin, otherwise |
| 399 | // use the default plugin. |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 400 | |
| 401 | const char *partial_name = NULL; |
| 402 | partial_name = input.GetArgumentAtIndex(opt_arg_pos); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 403 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 404 | PlatformSP platform_sp (m_interpreter.GetPlatform (true)); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 405 | if (platform_sp) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 406 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 407 | ProcessInstanceInfoList process_infos; |
| 408 | ProcessInstanceInfoMatch match_info; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 409 | if (partial_name) |
| 410 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 411 | match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 412 | match_info.SetNameMatchType(eNameMatchStartsWith); |
| 413 | } |
| 414 | platform_sp->FindProcesses (match_info, process_infos); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 415 | const uint32_t num_matches = process_infos.GetSize(); |
| 416 | if (num_matches > 0) |
| 417 | { |
| 418 | for (uint32_t i=0; i<num_matches; ++i) |
| 419 | { |
| 420 | matches.AppendString (process_infos.GetProcessNameAtIndex(i), |
| 421 | process_infos.GetProcessNameLengthAtIndex(i)); |
| 422 | } |
| 423 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | return false; |
| 428 | } |
| 429 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | // Options table: Required for subclasses of Options. |
| 431 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 432 | static OptionDefinition g_option_table[]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 433 | |
| 434 | // Instance variables to hold the values for command options. |
| 435 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 436 | ProcessAttachInfo attach_info; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 439 | CommandObjectProcessAttach (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 440 | CommandObjectParsed (interpreter, |
| 441 | "process attach", |
| 442 | "Attach to a process.", |
| 443 | "process attach <cmd-options>"), |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 444 | m_options (interpreter) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 445 | { |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | ~CommandObjectProcessAttach () |
| 449 | { |
| 450 | } |
| 451 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 452 | Options * |
| 453 | GetOptions () |
| 454 | { |
| 455 | return &m_options; |
| 456 | } |
| 457 | |
| 458 | protected: |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 459 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 460 | DoExecute (Args& command, |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 461 | CommandReturnObject &result) |
| 462 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 463 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
Jim Ingham | ee940e2 | 2011-09-15 01:08:57 +0000 | [diff] [blame] | 464 | // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach |
| 465 | // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop |
| 466 | // ourselves here. |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 467 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 468 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 469 | StateType state = eStateInvalid; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 470 | if (process) |
| 471 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 472 | state = process->GetState(); |
| 473 | if (process->IsAlive() && state != eStateConnected) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 474 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 475 | result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n", |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 476 | process->GetID()); |
| 477 | result.SetStatus (eReturnStatusFailed); |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (target == NULL) |
| 483 | { |
| 484 | // If there isn't a current target create one. |
| 485 | TargetSP new_target_sp; |
| 486 | FileSpec emptyFileSpec; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 487 | Error error; |
| 488 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 489 | error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(), |
| 490 | emptyFileSpec, |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 491 | NULL, |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 492 | false, |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 493 | NULL, // No platform options |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 494 | new_target_sp); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 495 | target = new_target_sp.get(); |
| 496 | if (target == NULL || error.Fail()) |
| 497 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 498 | result.AppendError(error.AsCString("Error creating target")); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 499 | return false; |
| 500 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 501 | m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Record the old executable module, we want to issue a warning if the process of attaching changed the |
| 505 | // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.) |
| 506 | |
| 507 | ModuleSP old_exec_module_sp = target->GetExecutableModule(); |
| 508 | ArchSpec old_arch_spec = target->GetArchitecture(); |
| 509 | |
| 510 | if (command.GetArgumentCount()) |
| 511 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 512 | result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 513 | result.SetStatus (eReturnStatusFailed); |
| 514 | } |
| 515 | else |
| 516 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 517 | if (state != eStateConnected) |
| 518 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 519 | const char *plugin_name = m_options.attach_info.GetProcessPluginName(); |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 520 | process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 521 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 522 | |
| 523 | if (process) |
| 524 | { |
| 525 | Error error; |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 526 | // If no process info was specified, then use the target executable |
| 527 | // name as the process to attach to by default |
| 528 | if (!m_options.attach_info.ProcessInfoSpecified ()) |
Jim Ingham | 4805a1c | 2010-09-15 01:34:14 +0000 | [diff] [blame] | 529 | { |
| 530 | if (old_exec_module_sp) |
Greg Clayton | 1d1f39e | 2011-11-29 04:03:30 +0000 | [diff] [blame] | 531 | m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename(); |
Jim Ingham | 4805a1c | 2010-09-15 01:34:14 +0000 | [diff] [blame] | 532 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 533 | if (!m_options.attach_info.ProcessInfoSpecified ()) |
| 534 | { |
| 535 | error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option"); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (error.Success()) |
| 540 | { |
| 541 | error = process->Attach (m_options.attach_info); |
| 542 | |
Jim Ingham | 4805a1c | 2010-09-15 01:34:14 +0000 | [diff] [blame] | 543 | if (error.Success()) |
| 544 | { |
| 545 | result.SetStatus (eReturnStatusSuccessContinuingNoResult); |
| 546 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 547 | else |
| 548 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 549 | result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString()); |
Jim Ingham | 4805a1c | 2010-09-15 01:34:14 +0000 | [diff] [blame] | 550 | result.SetStatus (eReturnStatusFailed); |
| 551 | return false; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 552 | } |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 553 | // If we're synchronous, wait for the stopped event and report that. |
| 554 | // Otherwise just return. |
| 555 | // FIXME: in the async case it will now be possible to get to the command |
| 556 | // interpreter with a state eStateAttaching. Make sure we handle that correctly. |
Jim Ingham | ee940e2 | 2011-09-15 01:08:57 +0000 | [diff] [blame] | 557 | StateType state = process->WaitForProcessToStop (NULL); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 558 | |
Jim Ingham | ee940e2 | 2011-09-15 01:08:57 +0000 | [diff] [blame] | 559 | result.SetDidChangeProcessState (true); |
Johnny Chen | 9986a3b | 2012-05-18 00:51:36 +0000 | [diff] [blame] | 560 | |
| 561 | if (state == eStateStopped) |
| 562 | { |
| 563 | result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state)); |
| 564 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | result.AppendError ("attach failed: process did not stop (no such process or permission problem?)"); |
| 569 | result.SetStatus (eReturnStatusFailed); |
| 570 | return false; |
| 571 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 572 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | if (result.Succeeded()) |
| 577 | { |
| 578 | // Okay, we're done. Last step is to warn if the executable module has changed: |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 579 | char new_path[PATH_MAX]; |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 580 | ModuleSP new_exec_module_sp (target->GetExecutableModule()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 581 | if (!old_exec_module_sp) |
| 582 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 583 | // We might not have a module if we attached to a raw pid... |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 584 | if (new_exec_module_sp) |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 585 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 586 | new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX); |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 587 | result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path); |
| 588 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 589 | } |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 590 | else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec()) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 591 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 592 | char old_path[PATH_MAX]; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 593 | |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 594 | old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX); |
| 595 | new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 596 | |
| 597 | result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n", |
| 598 | old_path, new_path); |
| 599 | } |
| 600 | |
| 601 | if (!old_arch_spec.IsValid()) |
| 602 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 603 | result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 604 | } |
| 605 | else if (old_arch_spec != target->GetArchitecture()) |
| 606 | { |
| 607 | result.AppendWarningWithFormat("Architecture changed from %s to %s.\n", |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 608 | old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 609 | } |
Johnny Chen | 7c09997 | 2012-05-24 00:43:00 +0000 | [diff] [blame] | 610 | |
| 611 | // This supports the use-case scenario of immediately continuing the process once attached. |
| 612 | if (m_options.attach_info.GetContinueOnceAttached()) |
Sean Callanan | 4336d93 | 2012-05-31 01:30:08 +0000 | [diff] [blame] | 613 | m_interpreter.HandleCommand("process continue", eLazyBoolNo, result); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 614 | } |
| 615 | return result.Succeeded(); |
| 616 | } |
| 617 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | CommandOptions m_options; |
| 619 | }; |
| 620 | |
| 621 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 622 | OptionDefinition |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | CommandObjectProcessAttach::CommandOptions::g_option_table[] = |
| 624 | { |
Jim Ingham | 3a458eb | 2012-07-20 21:37:13 +0000 | [diff] [blame^] | 625 | { LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."}, |
| 626 | { LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 627 | { LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."}, |
| 628 | { LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."}, |
| 629 | { LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."}, |
| 630 | { LLDB_OPT_SET_2, false, "waitfor", 'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the process with <process-name> to launch."}, |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 631 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | //------------------------------------------------------------------------- |
| 635 | // CommandObjectProcessContinue |
| 636 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 637 | #pragma mark CommandObjectProcessContinue |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 638 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 639 | class CommandObjectProcessContinue : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 640 | { |
| 641 | public: |
| 642 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 643 | CommandObjectProcessContinue (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 644 | CommandObjectParsed (interpreter, |
| 645 | "process continue", |
| 646 | "Continue execution of all threads in the current process.", |
| 647 | "process continue", |
| 648 | eFlagProcessMustBeLaunched | eFlagProcessMustBePaused) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | { |
| 650 | } |
| 651 | |
| 652 | |
| 653 | ~CommandObjectProcessContinue () |
| 654 | { |
| 655 | } |
| 656 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 657 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 658 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 659 | DoExecute (Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 660 | CommandReturnObject &result) |
| 661 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 662 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 663 | bool synchronous_execution = m_interpreter.GetSynchronous (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | |
| 665 | if (process == NULL) |
| 666 | { |
| 667 | result.AppendError ("no process to continue"); |
| 668 | result.SetStatus (eReturnStatusFailed); |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | StateType state = process->GetState(); |
| 673 | if (state == eStateStopped) |
| 674 | { |
| 675 | if (command.GetArgumentCount() != 0) |
| 676 | { |
| 677 | result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str()); |
| 678 | result.SetStatus (eReturnStatusFailed); |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 683 | |
| 684 | // Set the actions that the threads should each take when resuming |
| 685 | for (uint32_t idx=0; idx<num_threads; ++idx) |
| 686 | { |
| 687 | process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning); |
| 688 | } |
| 689 | |
| 690 | Error error(process->Resume()); |
| 691 | if (error.Success()) |
| 692 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 693 | result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 694 | if (synchronous_execution) |
| 695 | { |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 696 | state = process->WaitForProcessToStop (NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 697 | |
| 698 | result.SetDidChangeProcessState (true); |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 699 | result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 700 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | result.SetStatus (eReturnStatusSuccessContinuingNoResult); |
| 705 | } |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString()); |
| 710 | result.SetStatus (eReturnStatusFailed); |
| 711 | } |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n", |
| 716 | StateAsCString(state)); |
| 717 | result.SetStatus (eReturnStatusFailed); |
| 718 | } |
| 719 | return result.Succeeded(); |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | //------------------------------------------------------------------------- |
| 724 | // CommandObjectProcessDetach |
| 725 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 726 | #pragma mark CommandObjectProcessDetach |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 727 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 728 | class CommandObjectProcessDetach : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 729 | { |
| 730 | public: |
| 731 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 732 | CommandObjectProcessDetach (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 733 | CommandObjectParsed (interpreter, |
| 734 | "process detach", |
| 735 | "Detach from the current process being debugged.", |
| 736 | "process detach", |
| 737 | eFlagProcessMustBeLaunched) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | { |
| 739 | } |
| 740 | |
| 741 | ~CommandObjectProcessDetach () |
| 742 | { |
| 743 | } |
| 744 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 745 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 747 | DoExecute (Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | CommandReturnObject &result) |
| 749 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 750 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | if (process == NULL) |
| 752 | { |
| 753 | result.AppendError ("must have a valid process in order to detach"); |
| 754 | result.SetStatus (eReturnStatusFailed); |
| 755 | return false; |
| 756 | } |
| 757 | |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 758 | result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | Error error (process->Detach()); |
| 760 | if (error.Success()) |
| 761 | { |
| 762 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString()); |
| 767 | result.SetStatus (eReturnStatusFailed); |
| 768 | return false; |
| 769 | } |
| 770 | return result.Succeeded(); |
| 771 | } |
| 772 | }; |
| 773 | |
| 774 | //------------------------------------------------------------------------- |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 775 | // CommandObjectProcessConnect |
| 776 | //------------------------------------------------------------------------- |
| 777 | #pragma mark CommandObjectProcessConnect |
| 778 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 779 | class CommandObjectProcessConnect : public CommandObjectParsed |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 780 | { |
| 781 | public: |
| 782 | |
| 783 | class CommandOptions : public Options |
| 784 | { |
| 785 | public: |
| 786 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 787 | CommandOptions (CommandInterpreter &interpreter) : |
| 788 | Options(interpreter) |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 789 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 790 | // Keep default values of all options in one place: OptionParsingStarting () |
| 791 | OptionParsingStarting (); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | ~CommandOptions () |
| 795 | { |
| 796 | } |
| 797 | |
| 798 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 799 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 800 | { |
| 801 | Error error; |
| 802 | char short_option = (char) m_getopt_table[option_idx].val; |
| 803 | |
| 804 | switch (short_option) |
| 805 | { |
| 806 | case 'p': |
| 807 | plugin_name.assign (option_arg); |
| 808 | break; |
| 809 | |
| 810 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 811 | error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | return error; |
| 815 | } |
| 816 | |
| 817 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 818 | OptionParsingStarting () |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 819 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 820 | plugin_name.clear(); |
| 821 | } |
| 822 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 823 | const OptionDefinition* |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 824 | GetDefinitions () |
| 825 | { |
| 826 | return g_option_table; |
| 827 | } |
| 828 | |
| 829 | // Options table: Required for subclasses of Options. |
| 830 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 831 | static OptionDefinition g_option_table[]; |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 832 | |
| 833 | // Instance variables to hold the values for command options. |
| 834 | |
| 835 | std::string plugin_name; |
| 836 | }; |
| 837 | |
| 838 | CommandObjectProcessConnect (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 839 | CommandObjectParsed (interpreter, |
| 840 | "process connect", |
| 841 | "Connect to a remote debug service.", |
| 842 | "process connect <remote-url>", |
| 843 | 0), |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 844 | m_options (interpreter) |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 845 | { |
| 846 | } |
| 847 | |
| 848 | ~CommandObjectProcessConnect () |
| 849 | { |
| 850 | } |
| 851 | |
| 852 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 853 | Options * |
| 854 | GetOptions () |
| 855 | { |
| 856 | return &m_options; |
| 857 | } |
| 858 | |
| 859 | protected: |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 860 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 861 | DoExecute (Args& command, |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 862 | CommandReturnObject &result) |
| 863 | { |
| 864 | |
| 865 | TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget()); |
| 866 | Error error; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 867 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 868 | if (process) |
| 869 | { |
| 870 | if (process->IsAlive()) |
| 871 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 872 | result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n", |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 873 | process->GetID()); |
| 874 | result.SetStatus (eReturnStatusFailed); |
| 875 | return false; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | if (!target_sp) |
| 880 | { |
| 881 | // If there isn't a current target create one. |
| 882 | FileSpec emptyFileSpec; |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 883 | |
| 884 | error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(), |
| 885 | emptyFileSpec, |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 886 | NULL, |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 887 | false, |
Greg Clayton | 3e8c25f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 888 | NULL, // No platform options |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 889 | target_sp); |
| 890 | if (!target_sp || error.Fail()) |
| 891 | { |
| 892 | result.AppendError(error.AsCString("Error creating target")); |
| 893 | result.SetStatus (eReturnStatusFailed); |
| 894 | return false; |
| 895 | } |
| 896 | m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get()); |
| 897 | } |
| 898 | |
| 899 | if (command.GetArgumentCount() == 1) |
| 900 | { |
| 901 | const char *plugin_name = NULL; |
| 902 | if (!m_options.plugin_name.empty()) |
| 903 | plugin_name = m_options.plugin_name.c_str(); |
| 904 | |
| 905 | const char *remote_url = command.GetArgumentAtIndex(0); |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 906 | process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get(); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 907 | |
| 908 | if (process) |
| 909 | { |
| 910 | error = process->ConnectRemote (remote_url); |
| 911 | |
| 912 | if (error.Fail()) |
| 913 | { |
| 914 | result.AppendError(error.AsCString("Remote connect failed")); |
| 915 | result.SetStatus (eReturnStatusFailed); |
Greg Clayton | 0cbb93b | 2012-03-31 00:10:30 +0000 | [diff] [blame] | 916 | target_sp->DeleteCurrentProcess(); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 917 | return false; |
| 918 | } |
| 919 | } |
| 920 | else |
| 921 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 922 | 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", |
| 923 | m_cmd_name.c_str()); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 924 | result.SetStatus (eReturnStatusFailed); |
| 925 | } |
| 926 | } |
| 927 | else |
| 928 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 929 | result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n", |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 930 | m_cmd_name.c_str(), |
| 931 | m_cmd_syntax.c_str()); |
| 932 | result.SetStatus (eReturnStatusFailed); |
| 933 | } |
| 934 | return result.Succeeded(); |
| 935 | } |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 936 | |
| 937 | CommandOptions m_options; |
| 938 | }; |
| 939 | |
| 940 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 941 | OptionDefinition |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 942 | CommandObjectProcessConnect::CommandOptions::g_option_table[] = |
| 943 | { |
| 944 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 945 | { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL } |
| 946 | }; |
| 947 | |
| 948 | //------------------------------------------------------------------------- |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 949 | // CommandObjectProcessLoad |
| 950 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 951 | #pragma mark CommandObjectProcessLoad |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 952 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 953 | class CommandObjectProcessLoad : public CommandObjectParsed |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 954 | { |
| 955 | public: |
| 956 | |
| 957 | CommandObjectProcessLoad (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 958 | CommandObjectParsed (interpreter, |
| 959 | "process load", |
| 960 | "Load a shared library into the current process.", |
| 961 | "process load <filename> [<filename> ...]", |
| 962 | eFlagProcessMustBeLaunched | eFlagProcessMustBePaused) |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 963 | { |
| 964 | } |
| 965 | |
| 966 | ~CommandObjectProcessLoad () |
| 967 | { |
| 968 | } |
| 969 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 970 | protected: |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 971 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 972 | DoExecute (Args& command, |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 973 | CommandReturnObject &result) |
| 974 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 975 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 976 | if (process == NULL) |
| 977 | { |
| 978 | result.AppendError ("must have a valid process in order to load a shared library"); |
| 979 | result.SetStatus (eReturnStatusFailed); |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | const uint32_t argc = command.GetArgumentCount(); |
| 984 | |
| 985 | for (uint32_t i=0; i<argc; ++i) |
| 986 | { |
| 987 | Error error; |
| 988 | const char *image_path = command.GetArgumentAtIndex(i); |
| 989 | FileSpec image_spec (image_path, false); |
Greg Clayton | f2bf870 | 2011-08-11 16:25:18 +0000 | [diff] [blame] | 990 | process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 991 | uint32_t image_token = process->LoadImage(image_spec, error); |
| 992 | if (image_token != LLDB_INVALID_IMAGE_TOKEN) |
| 993 | { |
| 994 | result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token); |
| 995 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 996 | } |
| 997 | else |
| 998 | { |
| 999 | result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString()); |
| 1000 | result.SetStatus (eReturnStatusFailed); |
| 1001 | } |
| 1002 | } |
| 1003 | return result.Succeeded(); |
| 1004 | } |
| 1005 | }; |
| 1006 | |
| 1007 | |
| 1008 | //------------------------------------------------------------------------- |
| 1009 | // CommandObjectProcessUnload |
| 1010 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1011 | #pragma mark CommandObjectProcessUnload |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1012 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1013 | class CommandObjectProcessUnload : public CommandObjectParsed |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1014 | { |
| 1015 | public: |
| 1016 | |
| 1017 | CommandObjectProcessUnload (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1018 | CommandObjectParsed (interpreter, |
| 1019 | "process unload", |
| 1020 | "Unload a shared library from the current process using the index returned by a previous call to \"process load\".", |
| 1021 | "process unload <index>", |
| 1022 | eFlagProcessMustBeLaunched | eFlagProcessMustBePaused) |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1023 | { |
| 1024 | } |
| 1025 | |
| 1026 | ~CommandObjectProcessUnload () |
| 1027 | { |
| 1028 | } |
| 1029 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1030 | protected: |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1031 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1032 | DoExecute (Args& command, |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1033 | CommandReturnObject &result) |
| 1034 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1035 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1036 | if (process == NULL) |
| 1037 | { |
| 1038 | result.AppendError ("must have a valid process in order to load a shared library"); |
| 1039 | result.SetStatus (eReturnStatusFailed); |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | const uint32_t argc = command.GetArgumentCount(); |
| 1044 | |
| 1045 | for (uint32_t i=0; i<argc; ++i) |
| 1046 | { |
| 1047 | const char *image_token_cstr = command.GetArgumentAtIndex(i); |
| 1048 | uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0); |
| 1049 | if (image_token == LLDB_INVALID_IMAGE_TOKEN) |
| 1050 | { |
| 1051 | result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr); |
| 1052 | result.SetStatus (eReturnStatusFailed); |
| 1053 | break; |
| 1054 | } |
| 1055 | else |
| 1056 | { |
| 1057 | Error error (process->UnloadImage(image_token)); |
| 1058 | if (error.Success()) |
| 1059 | { |
| 1060 | result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token); |
| 1061 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString()); |
| 1066 | result.SetStatus (eReturnStatusFailed); |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | return result.Succeeded(); |
| 1072 | } |
| 1073 | }; |
| 1074 | |
| 1075 | //------------------------------------------------------------------------- |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | // CommandObjectProcessSignal |
| 1077 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1078 | #pragma mark CommandObjectProcessSignal |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1079 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1080 | class CommandObjectProcessSignal : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1081 | { |
| 1082 | public: |
| 1083 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1084 | CommandObjectProcessSignal (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1085 | CommandObjectParsed (interpreter, |
| 1086 | "process signal", |
| 1087 | "Send a UNIX signal to the current process being debugged.", |
| 1088 | NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 1090 | CommandArgumentEntry arg; |
| 1091 | CommandArgumentData signal_arg; |
| 1092 | |
| 1093 | // Define the first (and only) variant of this arg. |
Caroline Tice | 3a62e6d | 2010-10-18 22:56:57 +0000 | [diff] [blame] | 1094 | signal_arg.arg_type = eArgTypeUnixSignal; |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 1095 | signal_arg.arg_repetition = eArgRepeatPlain; |
| 1096 | |
| 1097 | // There is only one variant this argument could be; put it into the argument entry. |
| 1098 | arg.push_back (signal_arg); |
| 1099 | |
| 1100 | // Push the data for the first argument into the m_arguments vector. |
| 1101 | m_arguments.push_back (arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | ~CommandObjectProcessSignal () |
| 1105 | { |
| 1106 | } |
| 1107 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1108 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1109 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1110 | DoExecute (Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1111 | CommandReturnObject &result) |
| 1112 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1113 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1114 | if (process == NULL) |
| 1115 | { |
| 1116 | result.AppendError ("no process to signal"); |
| 1117 | result.SetStatus (eReturnStatusFailed); |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
| 1121 | if (command.GetArgumentCount() == 1) |
| 1122 | { |
Greg Clayton | 8f6be2a | 2010-10-09 01:40:57 +0000 | [diff] [blame] | 1123 | int signo = LLDB_INVALID_SIGNAL_NUMBER; |
| 1124 | |
| 1125 | const char *signal_name = command.GetArgumentAtIndex(0); |
| 1126 | if (::isxdigit (signal_name[0])) |
| 1127 | signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0); |
| 1128 | else |
| 1129 | signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name); |
| 1130 | |
| 1131 | if (signo == LLDB_INVALID_SIGNAL_NUMBER) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1132 | { |
| 1133 | result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0)); |
| 1134 | result.SetStatus (eReturnStatusFailed); |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | Error error (process->Signal (signo)); |
| 1139 | if (error.Success()) |
| 1140 | { |
| 1141 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString()); |
| 1146 | result.SetStatus (eReturnStatusFailed); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | else |
| 1151 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1152 | result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | m_cmd_syntax.c_str()); |
| 1154 | result.SetStatus (eReturnStatusFailed); |
| 1155 | } |
| 1156 | return result.Succeeded(); |
| 1157 | } |
| 1158 | }; |
| 1159 | |
| 1160 | |
| 1161 | //------------------------------------------------------------------------- |
| 1162 | // CommandObjectProcessInterrupt |
| 1163 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1164 | #pragma mark CommandObjectProcessInterrupt |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1165 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1166 | class CommandObjectProcessInterrupt : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1167 | { |
| 1168 | public: |
| 1169 | |
| 1170 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1171 | CommandObjectProcessInterrupt (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1172 | CommandObjectParsed (interpreter, |
| 1173 | "process interrupt", |
| 1174 | "Interrupt the current process being debugged.", |
| 1175 | "process interrupt", |
| 1176 | eFlagProcessMustBeLaunched) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1177 | { |
| 1178 | } |
| 1179 | |
| 1180 | ~CommandObjectProcessInterrupt () |
| 1181 | { |
| 1182 | } |
| 1183 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1184 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1185 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1186 | DoExecute (Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1187 | CommandReturnObject &result) |
| 1188 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1189 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1190 | if (process == NULL) |
| 1191 | { |
| 1192 | result.AppendError ("no process to halt"); |
| 1193 | result.SetStatus (eReturnStatusFailed); |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | if (command.GetArgumentCount() == 0) |
| 1198 | { |
| 1199 | Error error(process->Halt ()); |
| 1200 | if (error.Success()) |
| 1201 | { |
| 1202 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 1203 | |
| 1204 | // Maybe we should add a "SuspendThreadPlans so we |
| 1205 | // can halt, and keep in place all the current thread plans. |
| 1206 | process->GetThreadList().DiscardThreadPlans(); |
| 1207 | } |
| 1208 | else |
| 1209 | { |
| 1210 | result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString()); |
| 1211 | result.SetStatus (eReturnStatusFailed); |
| 1212 | } |
| 1213 | } |
| 1214 | else |
| 1215 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1216 | result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1217 | m_cmd_name.c_str(), |
| 1218 | m_cmd_syntax.c_str()); |
| 1219 | result.SetStatus (eReturnStatusFailed); |
| 1220 | } |
| 1221 | return result.Succeeded(); |
| 1222 | } |
| 1223 | }; |
| 1224 | |
| 1225 | //------------------------------------------------------------------------- |
| 1226 | // CommandObjectProcessKill |
| 1227 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1228 | #pragma mark CommandObjectProcessKill |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1229 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1230 | class CommandObjectProcessKill : public CommandObjectParsed |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1231 | { |
| 1232 | public: |
| 1233 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1234 | CommandObjectProcessKill (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1235 | CommandObjectParsed (interpreter, |
| 1236 | "process kill", |
| 1237 | "Terminate the current process being debugged.", |
| 1238 | "process kill", |
| 1239 | eFlagProcessMustBeLaunched) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1240 | { |
| 1241 | } |
| 1242 | |
| 1243 | ~CommandObjectProcessKill () |
| 1244 | { |
| 1245 | } |
| 1246 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1247 | protected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1248 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1249 | DoExecute (Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1250 | CommandReturnObject &result) |
| 1251 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1252 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1253 | if (process == NULL) |
| 1254 | { |
| 1255 | result.AppendError ("no process to kill"); |
| 1256 | result.SetStatus (eReturnStatusFailed); |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | if (command.GetArgumentCount() == 0) |
| 1261 | { |
| 1262 | Error error (process->Destroy()); |
| 1263 | if (error.Success()) |
| 1264 | { |
| 1265 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString()); |
| 1270 | result.SetStatus (eReturnStatusFailed); |
| 1271 | } |
| 1272 | } |
| 1273 | else |
| 1274 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1275 | result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1276 | m_cmd_name.c_str(), |
| 1277 | m_cmd_syntax.c_str()); |
| 1278 | result.SetStatus (eReturnStatusFailed); |
| 1279 | } |
| 1280 | return result.Succeeded(); |
| 1281 | } |
| 1282 | }; |
| 1283 | |
| 1284 | //------------------------------------------------------------------------- |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1285 | // CommandObjectProcessStatus |
| 1286 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1287 | #pragma mark CommandObjectProcessStatus |
| 1288 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1289 | class CommandObjectProcessStatus : public CommandObjectParsed |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1290 | { |
| 1291 | public: |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1292 | CommandObjectProcessStatus (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1293 | CommandObjectParsed (interpreter, |
| 1294 | "process status", |
| 1295 | "Show the current status and location of executing process.", |
| 1296 | "process status", |
| 1297 | 0) |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1298 | { |
| 1299 | } |
| 1300 | |
| 1301 | ~CommandObjectProcessStatus() |
| 1302 | { |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1307 | DoExecute (Args& command, CommandReturnObject &result) |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1308 | { |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1309 | Stream &strm = result.GetOutputStream(); |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1310 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1311 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1312 | Process *process = exe_ctx.GetProcessPtr(); |
| 1313 | if (process) |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1314 | { |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1315 | const bool only_threads_with_stop_reason = true; |
| 1316 | const uint32_t start_frame = 0; |
| 1317 | const uint32_t num_frames = 1; |
| 1318 | const uint32_t num_frames_with_source = 1; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1319 | process->GetStatus(strm); |
| 1320 | process->GetThreadStatus (strm, |
| 1321 | only_threads_with_stop_reason, |
| 1322 | start_frame, |
| 1323 | num_frames, |
| 1324 | num_frames_with_source); |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1325 | |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1326 | } |
| 1327 | else |
| 1328 | { |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1329 | result.AppendError ("No process."); |
Jim Ingham | 41313fc | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1330 | result.SetStatus (eReturnStatusFailed); |
| 1331 | } |
| 1332 | return result.Succeeded(); |
| 1333 | } |
| 1334 | }; |
| 1335 | |
| 1336 | //------------------------------------------------------------------------- |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1337 | // CommandObjectProcessHandle |
| 1338 | //------------------------------------------------------------------------- |
Jim Ingham | 22dc972 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1339 | #pragma mark CommandObjectProcessHandle |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1340 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1341 | class CommandObjectProcessHandle : public CommandObjectParsed |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1342 | { |
| 1343 | public: |
| 1344 | |
| 1345 | class CommandOptions : public Options |
| 1346 | { |
| 1347 | public: |
| 1348 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 1349 | CommandOptions (CommandInterpreter &interpreter) : |
| 1350 | Options (interpreter) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1351 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1352 | OptionParsingStarting (); |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | ~CommandOptions () |
| 1356 | { |
| 1357 | } |
| 1358 | |
| 1359 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1360 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1361 | { |
| 1362 | Error error; |
| 1363 | char short_option = (char) m_getopt_table[option_idx].val; |
| 1364 | |
| 1365 | switch (short_option) |
| 1366 | { |
| 1367 | case 's': |
| 1368 | stop = option_arg; |
| 1369 | break; |
| 1370 | case 'n': |
| 1371 | notify = option_arg; |
| 1372 | break; |
| 1373 | case 'p': |
| 1374 | pass = option_arg; |
| 1375 | break; |
| 1376 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1377 | error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | return error; |
| 1381 | } |
| 1382 | |
| 1383 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1384 | OptionParsingStarting () |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1385 | { |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1386 | stop.clear(); |
| 1387 | notify.clear(); |
| 1388 | pass.clear(); |
| 1389 | } |
| 1390 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1391 | const OptionDefinition* |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1392 | GetDefinitions () |
| 1393 | { |
| 1394 | return g_option_table; |
| 1395 | } |
| 1396 | |
| 1397 | // Options table: Required for subclasses of Options. |
| 1398 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1399 | static OptionDefinition g_option_table[]; |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Instance variables to hold the values for command options. |
| 1402 | |
| 1403 | std::string stop; |
| 1404 | std::string notify; |
| 1405 | std::string pass; |
| 1406 | }; |
| 1407 | |
| 1408 | |
| 1409 | CommandObjectProcessHandle (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1410 | CommandObjectParsed (interpreter, |
| 1411 | "process handle", |
| 1412 | "Show or update what the process and debugger should do with various signals received from the OS.", |
| 1413 | NULL), |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 1414 | m_options (interpreter) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1415 | { |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1416 | SetHelpLong ("If no signals are specified, update them all. If no update option is specified, list the current values.\n"); |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1417 | CommandArgumentEntry arg; |
Caroline Tice | 3a62e6d | 2010-10-18 22:56:57 +0000 | [diff] [blame] | 1418 | CommandArgumentData signal_arg; |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1419 | |
Caroline Tice | 3a62e6d | 2010-10-18 22:56:57 +0000 | [diff] [blame] | 1420 | signal_arg.arg_type = eArgTypeUnixSignal; |
| 1421 | signal_arg.arg_repetition = eArgRepeatStar; |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1422 | |
Caroline Tice | 3a62e6d | 2010-10-18 22:56:57 +0000 | [diff] [blame] | 1423 | arg.push_back (signal_arg); |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1424 | |
| 1425 | m_arguments.push_back (arg); |
| 1426 | } |
| 1427 | |
| 1428 | ~CommandObjectProcessHandle () |
| 1429 | { |
| 1430 | } |
| 1431 | |
| 1432 | Options * |
| 1433 | GetOptions () |
| 1434 | { |
| 1435 | return &m_options; |
| 1436 | } |
| 1437 | |
| 1438 | bool |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1439 | VerifyCommandOptionValue (const std::string &option, int &real_value) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1440 | { |
| 1441 | bool okay = true; |
| 1442 | |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1443 | bool success = false; |
| 1444 | bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success); |
| 1445 | |
| 1446 | if (success && tmp_value) |
| 1447 | real_value = 1; |
| 1448 | else if (success && !tmp_value) |
| 1449 | real_value = 0; |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1450 | else |
| 1451 | { |
| 1452 | // If the value isn't 'true' or 'false', it had better be 0 or 1. |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1453 | real_value = Args::StringToUInt32 (option.c_str(), 3); |
| 1454 | if (real_value != 0 && real_value != 1) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1455 | okay = false; |
| 1456 | } |
| 1457 | |
| 1458 | return okay; |
| 1459 | } |
| 1460 | |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1461 | void |
| 1462 | PrintSignalHeader (Stream &str) |
| 1463 | { |
| 1464 | str.Printf ("NAME PASS STOP NOTIFY\n"); |
| 1465 | str.Printf ("========== ===== ===== ======\n"); |
| 1466 | } |
| 1467 | |
| 1468 | void |
| 1469 | PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals) |
| 1470 | { |
| 1471 | bool stop; |
| 1472 | bool suppress; |
| 1473 | bool notify; |
| 1474 | |
| 1475 | str.Printf ("%-10s ", sig_name); |
| 1476 | if (signals.GetSignalInfo (signo, suppress, stop, notify)) |
| 1477 | { |
| 1478 | bool pass = !suppress; |
| 1479 | str.Printf ("%s %s %s", |
| 1480 | (pass ? "true " : "false"), |
| 1481 | (stop ? "true " : "false"), |
| 1482 | (notify ? "true " : "false")); |
| 1483 | } |
| 1484 | str.Printf ("\n"); |
| 1485 | } |
| 1486 | |
| 1487 | void |
| 1488 | PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals) |
| 1489 | { |
| 1490 | PrintSignalHeader (str); |
| 1491 | |
| 1492 | if (num_valid_signals > 0) |
| 1493 | { |
| 1494 | size_t num_args = signal_args.GetArgumentCount(); |
| 1495 | for (size_t i = 0; i < num_args; ++i) |
| 1496 | { |
| 1497 | int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i)); |
| 1498 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 1499 | PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals); |
| 1500 | } |
| 1501 | } |
| 1502 | else // Print info for ALL signals |
| 1503 | { |
| 1504 | int32_t signo = signals.GetFirstSignalNumber(); |
| 1505 | while (signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 1506 | { |
| 1507 | PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals); |
| 1508 | signo = signals.GetNextSignalNumber (signo); |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1513 | protected: |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1514 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1515 | DoExecute (Args &signal_args, CommandReturnObject &result) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1516 | { |
| 1517 | TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget(); |
| 1518 | |
| 1519 | if (!target_sp) |
| 1520 | { |
| 1521 | result.AppendError ("No current target;" |
| 1522 | " cannot handle signals until you have a valid target and process.\n"); |
| 1523 | result.SetStatus (eReturnStatusFailed); |
| 1524 | return false; |
| 1525 | } |
| 1526 | |
| 1527 | ProcessSP process_sp = target_sp->GetProcessSP(); |
| 1528 | |
| 1529 | if (!process_sp) |
| 1530 | { |
| 1531 | result.AppendError ("No current process; cannot handle signals until you have a valid process.\n"); |
| 1532 | result.SetStatus (eReturnStatusFailed); |
| 1533 | return false; |
| 1534 | } |
| 1535 | |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1536 | int stop_action = -1; // -1 means leave the current setting alone |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1537 | int pass_action = -1; // -1 means leave the current setting alone |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1538 | int notify_action = -1; // -1 means leave the current setting alone |
| 1539 | |
| 1540 | if (! m_options.stop.empty() |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1541 | && ! VerifyCommandOptionValue (m_options.stop, stop_action)) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1542 | { |
| 1543 | result.AppendError ("Invalid argument for command option --stop; must be true or false.\n"); |
| 1544 | result.SetStatus (eReturnStatusFailed); |
| 1545 | return false; |
| 1546 | } |
| 1547 | |
| 1548 | if (! m_options.notify.empty() |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1549 | && ! VerifyCommandOptionValue (m_options.notify, notify_action)) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1550 | { |
| 1551 | result.AppendError ("Invalid argument for command option --notify; must be true or false.\n"); |
| 1552 | result.SetStatus (eReturnStatusFailed); |
| 1553 | return false; |
| 1554 | } |
| 1555 | |
| 1556 | if (! m_options.pass.empty() |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1557 | && ! VerifyCommandOptionValue (m_options.pass, pass_action)) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1558 | { |
| 1559 | result.AppendError ("Invalid argument for command option --pass; must be true or false.\n"); |
| 1560 | result.SetStatus (eReturnStatusFailed); |
| 1561 | return false; |
| 1562 | } |
| 1563 | |
| 1564 | size_t num_args = signal_args.GetArgumentCount(); |
| 1565 | UnixSignals &signals = process_sp->GetUnixSignals(); |
| 1566 | int num_signals_set = 0; |
| 1567 | |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1568 | if (num_args > 0) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1569 | { |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1570 | for (size_t i = 0; i < num_args; ++i) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1571 | { |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1572 | int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i)); |
| 1573 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1574 | { |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1575 | // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees |
| 1576 | // the value is either 0 or 1. |
| 1577 | if (stop_action != -1) |
| 1578 | signals.SetShouldStop (signo, (bool) stop_action); |
| 1579 | if (pass_action != -1) |
| 1580 | { |
| 1581 | bool suppress = ! ((bool) pass_action); |
| 1582 | signals.SetShouldSuppress (signo, suppress); |
| 1583 | } |
| 1584 | if (notify_action != -1) |
| 1585 | signals.SetShouldNotify (signo, (bool) notify_action); |
| 1586 | ++num_signals_set; |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1587 | } |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1588 | else |
| 1589 | { |
| 1590 | result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i)); |
| 1591 | } |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
Caroline Tice | e747198 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1594 | else |
| 1595 | { |
| 1596 | // No signal specified, if any command options were specified, update ALL signals. |
| 1597 | if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) |
| 1598 | { |
| 1599 | if (m_interpreter.Confirm ("Do you really want to update all the signals?", false)) |
| 1600 | { |
| 1601 | int32_t signo = signals.GetFirstSignalNumber(); |
| 1602 | while (signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 1603 | { |
| 1604 | if (notify_action != -1) |
| 1605 | signals.SetShouldNotify (signo, (bool) notify_action); |
| 1606 | if (stop_action != -1) |
| 1607 | signals.SetShouldStop (signo, (bool) stop_action); |
| 1608 | if (pass_action != -1) |
| 1609 | { |
| 1610 | bool suppress = ! ((bool) pass_action); |
| 1611 | signals.SetShouldSuppress (signo, suppress); |
| 1612 | } |
| 1613 | signo = signals.GetNextSignalNumber (signo); |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals); |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1620 | |
| 1621 | if (num_signals_set > 0) |
| 1622 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1623 | else |
| 1624 | result.SetStatus (eReturnStatusFailed); |
| 1625 | |
| 1626 | return result.Succeeded(); |
| 1627 | } |
| 1628 | |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1629 | CommandOptions m_options; |
| 1630 | }; |
| 1631 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1632 | OptionDefinition |
Caroline Tice | 23d6f27 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1633 | CommandObjectProcessHandle::CommandOptions::g_option_table[] = |
| 1634 | { |
| 1635 | { LLDB_OPT_SET_1, false, "stop", 's', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." }, |
| 1636 | { LLDB_OPT_SET_1, false, "notify", 'n', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." }, |
| 1637 | { LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }, |
| 1638 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 1639 | }; |
| 1640 | |
| 1641 | //------------------------------------------------------------------------- |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1642 | // CommandObjectMultiwordProcess |
| 1643 | //------------------------------------------------------------------------- |
| 1644 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1645 | CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1646 | CommandObjectMultiword (interpreter, |
| 1647 | "process", |
| 1648 | "A set of commands for operating on a process.", |
| 1649 | "process <subcommand> [<subcommand-options>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1650 | { |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 1651 | LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter))); |
| 1652 | LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter))); |
| 1653 | LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter))); |
| 1654 | LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter))); |
| 1655 | LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter))); |
| 1656 | LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter))); |
| 1657 | LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter))); |
| 1658 | LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter))); |
| 1659 | LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter))); |
| 1660 | LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter))); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1661 | LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter))); |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 1662 | LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess () |
| 1666 | { |
| 1667 | } |
| 1668 | |