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