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