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