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