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