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