Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 9 | #include "CommandObjectProcess.h" |
Jim Ingham | 0e41084 | 2012-08-11 01:27:55 +0000 | [diff] [blame] | 10 | #include "lldb/Breakpoint/Breakpoint.h" |
| 11 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 12 | #include "lldb/Breakpoint/BreakpointSite.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 14 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 15 | #include "lldb/Host/Host.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 16 | #include "lldb/Host/OptionParser.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 17 | #include "lldb/Host/StringConvert.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 19 | #include "lldb/Interpreter/CommandReturnObject.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/OptionArgParser.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/Options.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
Jim Ingham | 0e41084 | 2012-08-11 01:27:55 +0000 | [diff] [blame] | 24 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Target.h" |
| 26 | #include "lldb/Target/Thread.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 27 | #include "lldb/Target/UnixSignals.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/Args.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed { |
Jim Ingham | dcb1d85 | 2013-03-29 00:56:30 +0000 | [diff] [blame] | 35 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter, |
| 37 | const char *name, const char *help, |
| 38 | const char *syntax, uint32_t flags, |
| 39 | const char *new_process_action) |
| 40 | : CommandObjectParsed(interpreter, name, help, syntax, flags), |
| 41 | m_new_process_action(new_process_action) {} |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | ~CommandObjectProcessLaunchOrAttach() override = default; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 44 | |
Jim Ingham | dcb1d85 | 2013-03-29 00:56:30 +0000 | [diff] [blame] | 45 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | bool StopProcessIfNecessary(Process *process, StateType &state, |
| 47 | CommandReturnObject &result) { |
| 48 | state = eStateInvalid; |
| 49 | if (process) { |
| 50 | state = process->GetState(); |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | if (process->IsAlive() && state != eStateConnected) { |
| 53 | char message[1024]; |
| 54 | if (process->GetState() == eStateAttaching) |
| 55 | ::snprintf(message, sizeof(message), |
| 56 | "There is a pending attach, abort it and %s?", |
| 57 | m_new_process_action.c_str()); |
| 58 | else if (process->GetShouldDetach()) |
| 59 | ::snprintf(message, sizeof(message), |
| 60 | "There is a running process, detach from it and %s?", |
| 61 | m_new_process_action.c_str()); |
| 62 | else |
| 63 | ::snprintf(message, sizeof(message), |
| 64 | "There is a running process, kill it and %s?", |
| 65 | m_new_process_action.c_str()); |
| 66 | |
| 67 | if (!m_interpreter.Confirm(message, true)) { |
| 68 | result.SetStatus(eReturnStatusFailed); |
| 69 | return false; |
| 70 | } else { |
| 71 | if (process->GetShouldDetach()) { |
| 72 | bool keep_stopped = false; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 73 | Status detach_error(process->Detach(keep_stopped)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | if (detach_error.Success()) { |
| 75 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 76 | process = nullptr; |
| 77 | } else { |
| 78 | result.AppendErrorWithFormat( |
| 79 | "Failed to detach from process: %s\n", |
| 80 | detach_error.AsCString()); |
| 81 | result.SetStatus(eReturnStatusFailed); |
| 82 | } |
| 83 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 84 | Status destroy_error(process->Destroy(false)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | if (destroy_error.Success()) { |
| 86 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 87 | process = nullptr; |
| 88 | } else { |
| 89 | result.AppendErrorWithFormat("Failed to kill process: %s\n", |
| 90 | destroy_error.AsCString()); |
| 91 | result.SetStatus(eReturnStatusFailed); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return result.Succeeded(); |
| 98 | } |
| 99 | |
| 100 | std::string m_new_process_action; |
Jim Ingham | dcb1d85 | 2013-03-29 00:56:30 +0000 | [diff] [blame] | 101 | }; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | // CommandObjectProcessLaunch |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 104 | #pragma mark CommandObjectProcessLaunch |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | CommandObjectProcessLaunch(CommandInterpreter &interpreter) |
| 108 | : CommandObjectProcessLaunchOrAttach( |
| 109 | interpreter, "process launch", |
| 110 | "Launch the executable in the debugger.", nullptr, |
| 111 | eCommandRequiresTarget, "restart"), |
| 112 | m_options() { |
| 113 | CommandArgumentEntry arg; |
| 114 | CommandArgumentData run_args_arg; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | // Define the first (and only) variant of this arg. |
| 117 | run_args_arg.arg_type = eArgTypeRunArgs; |
| 118 | run_args_arg.arg_repetition = eArgRepeatOptional; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | // There is only one variant this argument could be; put it into the |
| 121 | // argument entry. |
| 122 | arg.push_back(run_args_arg); |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | // Push the data for the first argument into the m_arguments vector. |
| 125 | m_arguments.push_back(arg); |
| 126 | } |
Jim Ingham | e9ce62b | 2012-08-10 21:48:41 +0000 | [diff] [blame] | 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | ~CommandObjectProcessLaunch() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 130 | int HandleArgumentCompletion( |
| 131 | CompletionRequest &request, |
| 132 | OptionElementVector &opt_element_vector) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | |
| 134 | CommandCompletions::InvokeCommonCompletionCallbacks( |
| 135 | GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 136 | request, nullptr); |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 137 | return request.GetNumberOfMatches(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Options *GetOptions() override { return &m_options; } |
| 141 | |
| 142 | const char *GetRepeatCommand(Args ¤t_command_args, |
| 143 | uint32_t index) override { |
| 144 | // No repeat for "process launch"... |
| 145 | return ""; |
| 146 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 147 | |
| 148 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | bool DoExecute(Args &launch_args, CommandReturnObject &result) override { |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 150 | Debugger &debugger = GetDebugger(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | Target *target = debugger.GetSelectedTarget().get(); |
| 152 | // If our listener is nullptr, users aren't allows to launch |
| 153 | ModuleSP exe_module_sp = target->GetExecutableModule(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | if (exe_module_sp == nullptr) { |
| 156 | result.AppendError("no file in target, create a debug target using the " |
| 157 | "'target create' command"); |
| 158 | result.SetStatus(eReturnStatusFailed); |
| 159 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | StateType state = eStateInvalid; |
| 163 | |
| 164 | if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result)) |
| 165 | return false; |
| 166 | |
Zachary Turner | 31d97a5 | 2016-11-17 18:08:12 +0000 | [diff] [blame] | 167 | llvm::StringRef target_settings_argv0 = target->GetArg0(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | |
| 169 | // Determine whether we will disable ASLR or leave it in the default state |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 170 | // (i.e. enabled if the platform supports it). First check if the process |
| 171 | // launch options explicitly turn on/off |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | // disabling ASLR. If so, use that setting; |
| 173 | // otherwise, use the 'settings target.disable-aslr' setting. |
| 174 | bool disable_aslr = false; |
| 175 | if (m_options.disable_aslr != eLazyBoolCalculate) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 176 | // The user specified an explicit setting on the process launch line. |
| 177 | // Use it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | disable_aslr = (m_options.disable_aslr == eLazyBoolYes); |
| 179 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 180 | // The user did not explicitly specify whether to disable ASLR. Fall |
| 181 | // back to the target.disable-aslr setting. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | disable_aslr = target->GetDisableASLR(); |
| 183 | } |
| 184 | |
| 185 | if (disable_aslr) |
| 186 | m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR); |
| 187 | else |
| 188 | m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR); |
| 189 | |
| 190 | if (target->GetDetachOnError()) |
| 191 | m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError); |
| 192 | |
| 193 | if (target->GetDisableSTDIO()) |
| 194 | m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO); |
| 195 | |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 196 | m_options.launch_info.GetEnvironment() = target->GetEnvironment(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | |
Zachary Turner | 31d97a5 | 2016-11-17 18:08:12 +0000 | [diff] [blame] | 198 | if (!target_settings_argv0.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | m_options.launch_info.GetArguments().AppendArgument( |
Zachary Turner | 31d97a5 | 2016-11-17 18:08:12 +0000 | [diff] [blame] | 200 | target_settings_argv0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | m_options.launch_info.SetExecutableFile( |
| 202 | exe_module_sp->GetPlatformFileSpec(), false); |
| 203 | } else { |
| 204 | m_options.launch_info.SetExecutableFile( |
| 205 | exe_module_sp->GetPlatformFileSpec(), true); |
| 206 | } |
| 207 | |
| 208 | if (launch_args.GetArgumentCount() == 0) { |
| 209 | m_options.launch_info.GetArguments().AppendArguments( |
| 210 | target->GetProcessLaunchInfo().GetArguments()); |
| 211 | } else { |
| 212 | m_options.launch_info.GetArguments().AppendArguments(launch_args); |
| 213 | // Save the arguments for subsequent runs in the current target. |
| 214 | target->SetRunArguments(launch_args); |
| 215 | } |
| 216 | |
| 217 | StreamString stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 218 | Status error = target->Launch(m_options.launch_info, &stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | |
| 220 | if (error.Success()) { |
| 221 | ProcessSP process_sp(target->GetProcessSP()); |
| 222 | if (process_sp) { |
| 223 | // There is a race condition where this thread will return up the call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 224 | // stack to the main command handler and show an (lldb) prompt before |
| 225 | // HandlePrivateEvent (from PrivateStateThread) has a chance to call |
| 226 | // PushProcessIOHandler(). |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 227 | process_sp->SyncIOHandler(0, std::chrono::seconds(2)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 229 | llvm::StringRef data = stream.GetString(); |
| 230 | if (!data.empty()) |
| 231 | result.AppendMessage(data); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | const char *archname = |
| 233 | exe_module_sp->GetArchitecture().GetArchitectureName(); |
| 234 | result.AppendMessageWithFormat( |
| 235 | "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(), |
| 236 | exe_module_sp->GetFileSpec().GetPath().c_str(), archname); |
| 237 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 238 | result.SetDidChangeProcessState(true); |
| 239 | } else { |
| 240 | result.AppendError( |
| 241 | "no error returned from Target::Launch, and target has no process"); |
| 242 | result.SetStatus(eReturnStatusFailed); |
| 243 | } |
| 244 | } else { |
| 245 | result.AppendError(error.AsCString()); |
| 246 | result.SetStatus(eReturnStatusFailed); |
| 247 | } |
| 248 | return result.Succeeded(); |
| 249 | } |
| 250 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | ProcessLaunchCommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 255 | //#define SET1 LLDB_OPT_SET_1 |
| 256 | //#define SET2 LLDB_OPT_SET_2 |
| 257 | //#define SET3 LLDB_OPT_SET_3 |
| 258 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | // OptionDefinition |
| 260 | // CommandObjectProcessLaunch::CommandOptions::g_option_table[] = |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 261 | //{ |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame] | 262 | // // clang-format off |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | // {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument, |
| 264 | // nullptr, 0, eArgTypeNone, "Stop at the entry point of the program |
| 265 | // when launching a process."}, |
| 266 | // {SET1, false, "stdin", 'i', |
| 267 | // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, |
| 268 | // "Redirect stdin for the process to <path>."}, |
| 269 | // {SET1, false, "stdout", 'o', |
| 270 | // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, |
| 271 | // "Redirect stdout for the process to <path>."}, |
| 272 | // {SET1, false, "stderr", 'e', |
| 273 | // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, |
| 274 | // "Redirect stderr for the process to <path>."}, |
| 275 | // {SET1 | SET2 | SET3, false, "plugin", 'p', |
| 276 | // OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of |
| 277 | // the process plugin you want to use."}, |
| 278 | // { SET2, false, "tty", 't', |
| 279 | // OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start |
| 280 | // the process in a terminal. If <path> is specified, look for a terminal whose |
| 281 | // name contains <path>, else start the process in a new terminal."}, |
| 282 | // { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument, |
| 283 | // nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to |
| 284 | // running process."}, |
| 285 | // {SET1 | SET2 | SET3, false, "working-dir", 'w', |
| 286 | // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the |
| 287 | // current working directory to <path> when running the inferior."}, |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame] | 288 | // {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr} |
| 289 | // // clang-format on |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 290 | //}; |
| 291 | // |
| 292 | //#undef SET1 |
| 293 | //#undef SET2 |
| 294 | //#undef SET3 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | // CommandObjectProcessAttach |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 297 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 298 | static constexpr OptionDefinition g_process_attach_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 299 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 300 | { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Immediately continue the process once attached." }, |
| 301 | { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePlugin, "Name of the process plugin you want to use." }, |
| 302 | { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePid, "The process ID of an existing process to attach to." }, |
| 303 | { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "The name of the process to attach to." }, |
| 304 | { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Include existing processes when doing attach -w." }, |
| 305 | { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 306 | // clang-format on |
| 307 | }; |
| 308 | |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 309 | #pragma mark CommandObjectProcessAttach |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | class CommandOptions : public Options { |
| 313 | public: |
| 314 | CommandOptions() : Options() { |
| 315 | // Keep default values of all options in one place: OptionParsingStarting |
| 316 | // () |
| 317 | OptionParsingStarting(nullptr); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | ~CommandOptions() override = default; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 321 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 322 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 323 | ExecutionContext *execution_context) override { |
| 324 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | const int short_option = m_getopt_table[option_idx].val; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 326 | switch (short_option) { |
| 327 | case 'c': |
| 328 | attach_info.SetContinueOnceAttached(true); |
| 329 | break; |
| 330 | |
| 331 | case 'p': { |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 332 | lldb::pid_t pid; |
| 333 | if (option_arg.getAsInteger(0, pid)) { |
| 334 | error.SetErrorStringWithFormat("invalid process ID '%s'", |
| 335 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | } else { |
| 337 | attach_info.SetProcessID(pid); |
| 338 | } |
| 339 | } break; |
| 340 | |
| 341 | case 'P': |
| 342 | attach_info.SetProcessPluginName(option_arg); |
| 343 | break; |
| 344 | |
| 345 | case 'n': |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 346 | attach_info.GetExecutableFile().SetFile(option_arg, |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 347 | FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | break; |
| 349 | |
| 350 | case 'w': |
| 351 | attach_info.SetWaitForLaunch(true); |
| 352 | break; |
| 353 | |
| 354 | case 'i': |
| 355 | attach_info.SetIgnoreExisting(false); |
| 356 | break; |
| 357 | |
| 358 | default: |
| 359 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 360 | short_option); |
| 361 | break; |
| 362 | } |
| 363 | return error; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 367 | attach_info.Clear(); |
| 368 | } |
| 369 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 370 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 371 | return llvm::makeArrayRef(g_process_attach_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 372 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | |
| 374 | bool HandleOptionArgumentCompletion( |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 375 | CompletionRequest &request, OptionElementVector &opt_element_vector, |
| 376 | int opt_element_index, CommandInterpreter &interpreter) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; |
| 378 | int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; |
| 379 | |
| 380 | // We are only completing the name option for now... |
| 381 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 382 | if (GetDefinitions()[opt_defs_index].short_option == 'n') { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | // Are we in the name? |
| 384 | |
| 385 | // Look to see if there is a -P argument provided, and if so use that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 386 | // plugin, otherwise use the default plugin. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | |
| 388 | const char *partial_name = nullptr; |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 389 | partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | |
| 391 | PlatformSP platform_sp(interpreter.GetPlatform(true)); |
| 392 | if (platform_sp) { |
| 393 | ProcessInstanceInfoList process_infos; |
| 394 | ProcessInstanceInfoMatch match_info; |
| 395 | if (partial_name) { |
| 396 | match_info.GetProcessInfo().GetExecutableFile().SetFile( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 397 | partial_name, FileSpec::Style::native); |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 398 | match_info.SetNameMatchType(NameMatch::StartsWith); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | } |
| 400 | platform_sp->FindProcesses(match_info, process_infos); |
| 401 | const size_t num_matches = process_infos.GetSize(); |
| 402 | if (num_matches > 0) { |
| 403 | for (size_t i = 0; i < num_matches; ++i) { |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 404 | request.AddCompletion(llvm::StringRef( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | process_infos.GetProcessNameAtIndex(i), |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 406 | process_infos.GetProcessNameLengthAtIndex(i))); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | // Instance variables to hold the values for command options. |
| 416 | |
| 417 | ProcessAttachInfo attach_info; |
| 418 | }; |
| 419 | |
| 420 | CommandObjectProcessAttach(CommandInterpreter &interpreter) |
| 421 | : CommandObjectProcessLaunchOrAttach( |
| 422 | interpreter, "process attach", "Attach to a process.", |
| 423 | "process attach <cmd-options>", 0, "attach"), |
| 424 | m_options() {} |
| 425 | |
| 426 | ~CommandObjectProcessAttach() override = default; |
| 427 | |
| 428 | Options *GetOptions() override { return &m_options; } |
| 429 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 430 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 431 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 432 | PlatformSP platform_sp( |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 433 | GetDebugger().GetPlatformList().GetSelectedPlatform()); |
Oleksiy Vyalov | 926af0c | 2015-02-03 00:04:35 +0000 | [diff] [blame] | 434 | |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 435 | Target *target = GetDebugger().GetSelectedTarget().get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | // N.B. The attach should be synchronous. It doesn't help much to get the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 437 | // prompt back between initiating the attach and the target actually |
| 438 | // stopping. So even if the interpreter is set to be asynchronous, we wait |
| 439 | // for the stop ourselves here. |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | StateType state = eStateInvalid; |
| 442 | Process *process = m_exe_ctx.GetProcessPtr(); |
Oleksiy Vyalov | 926af0c | 2015-02-03 00:04:35 +0000 | [diff] [blame] | 443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | if (!StopProcessIfNecessary(process, state, result)) |
| 445 | return false; |
Oleksiy Vyalov | 926af0c | 2015-02-03 00:04:35 +0000 | [diff] [blame] | 446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 447 | if (target == nullptr) { |
| 448 | // If there isn't a current target create one. |
| 449 | TargetSP new_target_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 450 | Status error; |
Oleksiy Vyalov | 926af0c | 2015-02-03 00:04:35 +0000 | [diff] [blame] | 451 | |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 452 | error = GetDebugger().GetTargetList().CreateTarget( |
| 453 | GetDebugger(), "", "", eLoadDependentsNo, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | nullptr, // No platform options |
| 455 | new_target_sp); |
| 456 | target = new_target_sp.get(); |
| 457 | if (target == nullptr || error.Fail()) { |
| 458 | result.AppendError(error.AsCString("Error creating target")); |
| 459 | return false; |
| 460 | } |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 461 | GetDebugger().GetTargetList().SetSelectedTarget(target); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 462 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | |
| 464 | // Record the old executable module, we want to issue a warning if the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 465 | // process of attaching changed the current executable (like somebody said |
| 466 | // "file foo" then attached to a PID whose executable was bar.) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | |
| 468 | ModuleSP old_exec_module_sp = target->GetExecutableModule(); |
| 469 | ArchSpec old_arch_spec = target->GetArchitecture(); |
| 470 | |
| 471 | if (command.GetArgumentCount()) { |
| 472 | result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", |
| 473 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 474 | result.SetStatus(eReturnStatusFailed); |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | m_interpreter.UpdateExecutionContext(nullptr); |
| 479 | StreamString stream; |
| 480 | const auto error = target->Attach(m_options.attach_info, &stream); |
| 481 | if (error.Success()) { |
| 482 | ProcessSP process_sp(target->GetProcessSP()); |
| 483 | if (process_sp) { |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 484 | result.AppendMessage(stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 486 | result.SetDidChangeProcessState(true); |
| 487 | result.SetAbnormalStopWasExpected(true); |
| 488 | } else { |
| 489 | result.AppendError( |
| 490 | "no error returned from Target::Attach, and target has no process"); |
| 491 | result.SetStatus(eReturnStatusFailed); |
| 492 | } |
| 493 | } else { |
| 494 | result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString()); |
| 495 | result.SetStatus(eReturnStatusFailed); |
| 496 | } |
| 497 | |
| 498 | if (!result.Succeeded()) |
| 499 | return false; |
| 500 | |
| 501 | // Okay, we're done. Last step is to warn if the executable module has |
| 502 | // changed: |
| 503 | char new_path[PATH_MAX]; |
| 504 | ModuleSP new_exec_module_sp(target->GetExecutableModule()); |
| 505 | if (!old_exec_module_sp) { |
| 506 | // We might not have a module if we attached to a raw pid... |
| 507 | if (new_exec_module_sp) { |
| 508 | new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX); |
| 509 | result.AppendMessageWithFormat("Executable module set to \"%s\".\n", |
| 510 | new_path); |
| 511 | } |
| 512 | } else if (old_exec_module_sp->GetFileSpec() != |
| 513 | new_exec_module_sp->GetFileSpec()) { |
| 514 | char old_path[PATH_MAX]; |
| 515 | |
| 516 | old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX); |
| 517 | new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX); |
| 518 | |
| 519 | result.AppendWarningWithFormat( |
| 520 | "Executable module changed from \"%s\" to \"%s\".\n", old_path, |
| 521 | new_path); |
| 522 | } |
| 523 | |
| 524 | if (!old_arch_spec.IsValid()) { |
| 525 | result.AppendMessageWithFormat( |
| 526 | "Architecture set to: %s.\n", |
| 527 | target->GetArchitecture().GetTriple().getTriple().c_str()); |
| 528 | } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) { |
| 529 | result.AppendWarningWithFormat( |
| 530 | "Architecture changed from %s to %s.\n", |
| 531 | old_arch_spec.GetTriple().getTriple().c_str(), |
| 532 | target->GetArchitecture().GetTriple().getTriple().c_str()); |
| 533 | } |
| 534 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 535 | // This supports the use-case scenario of immediately continuing the |
| 536 | // process once attached. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | if (m_options.attach_info.GetContinueOnceAttached()) |
| 538 | m_interpreter.HandleCommand("process continue", eLazyBoolNo, result); |
| 539 | |
| 540 | return result.Succeeded(); |
| 541 | } |
| 542 | |
| 543 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 546 | // CommandObjectProcessContinue |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 547 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 548 | static constexpr OptionDefinition g_process_continue_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 549 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 550 | { LLDB_OPT_SET_ALL, false, "ignore-count",'i', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread." } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 551 | // clang-format on |
| 552 | }; |
| 553 | |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 554 | #pragma mark CommandObjectProcessContinue |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | class CommandObjectProcessContinue : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 557 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | CommandObjectProcessContinue(CommandInterpreter &interpreter) |
| 559 | : CommandObjectParsed( |
| 560 | interpreter, "process continue", |
| 561 | "Continue execution of all threads in the current process.", |
| 562 | "process continue", |
| 563 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 564 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), |
| 565 | m_options() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 567 | ~CommandObjectProcessContinue() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 568 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 569 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | class CommandOptions : public Options { |
| 571 | public: |
| 572 | CommandOptions() : Options() { |
| 573 | // Keep default values of all options in one place: OptionParsingStarting |
| 574 | // () |
| 575 | OptionParsingStarting(nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 576 | } |
Jim Ingham | 0e41084 | 2012-08-11 01:27:55 +0000 | [diff] [blame] | 577 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | ~CommandOptions() override = default; |
| 579 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 580 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 581 | ExecutionContext *execution_context) override { |
| 582 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | const int short_option = m_getopt_table[option_idx].val; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | switch (short_option) { |
| 585 | case 'i': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 586 | if (option_arg.getAsInteger(0, m_ignore)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 587 | error.SetErrorStringWithFormat( |
| 588 | "invalid value for ignore option: \"%s\", should be a number.", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 589 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | break; |
| 591 | |
| 592 | default: |
| 593 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 594 | short_option); |
| 595 | break; |
| 596 | } |
| 597 | return error; |
Jim Ingham | 0e41084 | 2012-08-11 01:27:55 +0000 | [diff] [blame] | 598 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 599 | |
| 600 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 601 | m_ignore = 0; |
| 602 | } |
| 603 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 604 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 605 | return llvm::makeArrayRef(g_process_continue_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 606 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | |
| 608 | uint32_t m_ignore; |
| 609 | }; |
| 610 | |
| 611 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 612 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 613 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
| 614 | StateType state = process->GetState(); |
| 615 | if (state == eStateStopped) { |
| 616 | if (command.GetArgumentCount() != 0) { |
| 617 | result.AppendErrorWithFormat( |
| 618 | "The '%s' command does not take any arguments.\n", |
| 619 | m_cmd_name.c_str()); |
| 620 | result.SetStatus(eReturnStatusFailed); |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | if (m_options.m_ignore > 0) { |
| 625 | ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this()); |
| 626 | if (sel_thread_sp) { |
| 627 | StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo(); |
| 628 | if (stop_info_sp && |
| 629 | stop_info_sp->GetStopReason() == eStopReasonBreakpoint) { |
| 630 | lldb::break_id_t bp_site_id = |
| 631 | (lldb::break_id_t)stop_info_sp->GetValue(); |
| 632 | BreakpointSiteSP bp_site_sp( |
| 633 | process->GetBreakpointSiteList().FindByID(bp_site_id)); |
| 634 | if (bp_site_sp) { |
| 635 | const size_t num_owners = bp_site_sp->GetNumberOfOwners(); |
| 636 | for (size_t i = 0; i < num_owners; i++) { |
| 637 | Breakpoint &bp_ref = |
| 638 | bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); |
| 639 | if (!bp_ref.IsInternal()) { |
| 640 | bp_ref.SetIgnoreCount(m_options.m_ignore); |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | { // Scope for thread list mutex: |
| 649 | std::lock_guard<std::recursive_mutex> guard( |
| 650 | process->GetThreadList().GetMutex()); |
| 651 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 652 | |
| 653 | // Set the actions that the threads should each take when resuming |
| 654 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 655 | const bool override_suspend = false; |
| 656 | process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState( |
| 657 | eStateRunning, override_suspend); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | const uint32_t iohandler_id = process->GetIOHandlerID(); |
| 662 | |
| 663 | StreamString stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 664 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 665 | if (synchronous_execution) |
| 666 | error = process->ResumeSynchronous(&stream); |
| 667 | else |
| 668 | error = process->Resume(); |
| 669 | |
| 670 | if (error.Success()) { |
| 671 | // There is a race condition where this thread will return up the call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 672 | // stack to the main command handler and show an (lldb) prompt before |
| 673 | // HandlePrivateEvent (from PrivateStateThread) has a chance to call |
| 674 | // PushProcessIOHandler(). |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 675 | process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 676 | |
| 677 | result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", |
| 678 | process->GetID()); |
| 679 | if (synchronous_execution) { |
| 680 | // If any state changed events had anything to say, add that to the |
| 681 | // result |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 682 | result.AppendMessage(stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 683 | |
| 684 | result.SetDidChangeProcessState(true); |
| 685 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 686 | } else { |
| 687 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
| 688 | } |
| 689 | } else { |
| 690 | result.AppendErrorWithFormat("Failed to resume process: %s.\n", |
| 691 | error.AsCString()); |
| 692 | result.SetStatus(eReturnStatusFailed); |
| 693 | } |
| 694 | } else { |
| 695 | result.AppendErrorWithFormat( |
| 696 | "Process cannot be continued from its current state (%s).\n", |
| 697 | StateAsCString(state)); |
| 698 | result.SetStatus(eReturnStatusFailed); |
| 699 | } |
| 700 | return result.Succeeded(); |
| 701 | } |
| 702 | |
| 703 | Options *GetOptions() override { return &m_options; } |
| 704 | |
| 705 | CommandOptions m_options; |
Jim Ingham | 0e41084 | 2012-08-11 01:27:55 +0000 | [diff] [blame] | 706 | }; |
| 707 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | // CommandObjectProcessDetach |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 709 | static constexpr OptionDefinition g_process_detach_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 710 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 711 | { LLDB_OPT_SET_1, false, "keep-stopped", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the process should be kept stopped on detach (if possible)." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 712 | // clang-format on |
| 713 | }; |
| 714 | |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 715 | #pragma mark CommandObjectProcessDetach |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 717 | class CommandObjectProcessDetach : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 719 | class CommandOptions : public Options { |
| 720 | public: |
| 721 | CommandOptions() : Options() { OptionParsingStarting(nullptr); } |
| 722 | |
| 723 | ~CommandOptions() override = default; |
| 724 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 725 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 726 | ExecutionContext *execution_context) override { |
| 727 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 728 | const int short_option = m_getopt_table[option_idx].val; |
| 729 | |
| 730 | switch (short_option) { |
| 731 | case 's': |
| 732 | bool tmp_result; |
| 733 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 734 | tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | if (!success) |
| 736 | error.SetErrorStringWithFormat("invalid boolean option: \"%s\"", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 737 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | else { |
| 739 | if (tmp_result) |
| 740 | m_keep_stopped = eLazyBoolYes; |
| 741 | else |
| 742 | m_keep_stopped = eLazyBoolNo; |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 743 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | break; |
| 745 | default: |
| 746 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 747 | short_option); |
| 748 | break; |
| 749 | } |
| 750 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 754 | m_keep_stopped = eLazyBoolCalculate; |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 757 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 758 | return llvm::makeArrayRef(g_process_detach_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 759 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | |
| 761 | // Instance variables to hold the values for command options. |
| 762 | LazyBool m_keep_stopped; |
| 763 | }; |
| 764 | |
| 765 | CommandObjectProcessDetach(CommandInterpreter &interpreter) |
| 766 | : CommandObjectParsed(interpreter, "process detach", |
| 767 | "Detach from the current target process.", |
| 768 | "process detach", |
| 769 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 770 | eCommandProcessMustBeLaunched), |
| 771 | m_options() {} |
| 772 | |
| 773 | ~CommandObjectProcessDetach() override = default; |
| 774 | |
| 775 | Options *GetOptions() override { return &m_options; } |
| 776 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 777 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 778 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 779 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 780 | // FIXME: This will be a Command Option: |
| 781 | bool keep_stopped; |
| 782 | if (m_options.m_keep_stopped == eLazyBoolCalculate) { |
| 783 | // Check the process default: |
| 784 | keep_stopped = process->GetDetachKeepsStopped(); |
| 785 | } else if (m_options.m_keep_stopped == eLazyBoolYes) |
| 786 | keep_stopped = true; |
| 787 | else |
| 788 | keep_stopped = false; |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 789 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 790 | Status error(process->Detach(keep_stopped)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | if (error.Success()) { |
| 792 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 793 | } else { |
| 794 | result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString()); |
| 795 | result.SetStatus(eReturnStatusFailed); |
| 796 | return false; |
| 797 | } |
| 798 | return result.Succeeded(); |
| 799 | } |
| 800 | |
| 801 | CommandOptions m_options; |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 804 | // CommandObjectProcessConnect |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 805 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 806 | static constexpr OptionDefinition g_process_connect_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 807 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 808 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePlugin, "Name of the process plugin you want to use." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 809 | // clang-format on |
| 810 | }; |
| 811 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 812 | #pragma mark CommandObjectProcessConnect |
| 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | class CommandObjectProcessConnect : public CommandObjectParsed { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 815 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 816 | class CommandOptions : public Options { |
| 817 | public: |
| 818 | CommandOptions() : Options() { |
| 819 | // Keep default values of all options in one place: OptionParsingStarting |
| 820 | // () |
| 821 | OptionParsingStarting(nullptr); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 822 | } |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 823 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 824 | ~CommandOptions() override = default; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 825 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 826 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 827 | ExecutionContext *execution_context) override { |
| 828 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 829 | const int short_option = m_getopt_table[option_idx].val; |
| 830 | |
| 831 | switch (short_option) { |
| 832 | case 'p': |
| 833 | plugin_name.assign(option_arg); |
| 834 | break; |
| 835 | |
| 836 | default: |
| 837 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 838 | short_option); |
| 839 | break; |
| 840 | } |
| 841 | return error; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 842 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | |
| 844 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 845 | plugin_name.clear(); |
| 846 | } |
| 847 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 848 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 849 | return llvm::makeArrayRef(g_process_connect_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 850 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | |
| 852 | // Instance variables to hold the values for command options. |
| 853 | |
| 854 | std::string plugin_name; |
| 855 | }; |
| 856 | |
| 857 | CommandObjectProcessConnect(CommandInterpreter &interpreter) |
| 858 | : CommandObjectParsed(interpreter, "process connect", |
| 859 | "Connect to a remote debug service.", |
| 860 | "process connect <remote-url>", 0), |
| 861 | m_options() {} |
| 862 | |
| 863 | ~CommandObjectProcessConnect() override = default; |
| 864 | |
| 865 | Options *GetOptions() override { return &m_options; } |
| 866 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 867 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 869 | if (command.GetArgumentCount() != 1) { |
| 870 | result.AppendErrorWithFormat( |
| 871 | "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(), |
| 872 | m_cmd_syntax.c_str()); |
| 873 | result.SetStatus(eReturnStatusFailed); |
| 874 | return false; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 875 | } |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 878 | if (process && process->IsAlive()) { |
| 879 | result.AppendErrorWithFormat( |
| 880 | "Process %" PRIu64 |
| 881 | " is currently being debugged, kill the process before connecting.\n", |
| 882 | process->GetID()); |
| 883 | result.SetStatus(eReturnStatusFailed); |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | const char *plugin_name = nullptr; |
| 888 | if (!m_options.plugin_name.empty()) |
| 889 | plugin_name = m_options.plugin_name.c_str(); |
| 890 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 891 | Status error; |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 892 | Debugger &debugger = GetDebugger(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 893 | PlatformSP platform_sp = m_interpreter.GetPlatform(true); |
| 894 | ProcessSP process_sp = platform_sp->ConnectProcess( |
| 895 | command.GetArgumentAtIndex(0), plugin_name, debugger, |
| 896 | debugger.GetSelectedTarget().get(), error); |
| 897 | if (error.Fail() || process_sp == nullptr) { |
| 898 | result.AppendError(error.AsCString("Error connecting to the process")); |
| 899 | result.SetStatus(eReturnStatusFailed); |
| 900 | return false; |
| 901 | } |
| 902 | return true; |
| 903 | } |
| 904 | |
| 905 | CommandOptions m_options; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 906 | }; |
| 907 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 908 | // CommandObjectProcessPlugin |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 909 | #pragma mark CommandObjectProcessPlugin |
| 910 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 911 | class CommandObjectProcessPlugin : public CommandObjectProxy { |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 912 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 913 | CommandObjectProcessPlugin(CommandInterpreter &interpreter) |
| 914 | : CommandObjectProxy( |
| 915 | interpreter, "process plugin", |
| 916 | "Send a custom command to the current target process plug-in.", |
| 917 | "process plugin <args>", 0) {} |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 918 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 919 | ~CommandObjectProcessPlugin() override = default; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 920 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 921 | CommandObject *GetProxyCommandObject() override { |
| 922 | Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 923 | if (process) |
| 924 | return process->GetPluginCommandObject(); |
| 925 | return nullptr; |
| 926 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 927 | }; |
| 928 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 929 | // CommandObjectProcessLoad |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 930 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 931 | static constexpr OptionDefinition g_process_load_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 932 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 933 | { LLDB_OPT_SET_ALL, false, "install", 'i', OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypePath, "Install the shared library to the target. If specified without an argument then the library will installed in the current working directory." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 934 | // clang-format on |
| 935 | }; |
| 936 | |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 937 | #pragma mark CommandObjectProcessLoad |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 938 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 939 | class CommandObjectProcessLoad : public CommandObjectParsed { |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 940 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 941 | class CommandOptions : public Options { |
| 942 | public: |
| 943 | CommandOptions() : Options() { |
| 944 | // Keep default values of all options in one place: OptionParsingStarting |
| 945 | // () |
| 946 | OptionParsingStarting(nullptr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | ~CommandOptions() override = default; |
Tamas Berghammer | 4fbd67a | 2015-12-08 13:43:59 +0000 | [diff] [blame] | 950 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 951 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 952 | ExecutionContext *execution_context) override { |
| 953 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 954 | const int short_option = m_getopt_table[option_idx].val; |
| 955 | switch (short_option) { |
| 956 | case 'i': |
| 957 | do_install = true; |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 958 | if (!option_arg.empty()) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 959 | install_path.SetFile(option_arg, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 960 | break; |
| 961 | default: |
| 962 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 963 | short_option); |
| 964 | break; |
| 965 | } |
| 966 | return error; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 970 | do_install = false; |
| 971 | install_path.Clear(); |
| 972 | } |
| 973 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 974 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 975 | return llvm::makeArrayRef(g_process_load_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 976 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | |
| 978 | // Instance variables to hold the values for command options. |
| 979 | bool do_install; |
| 980 | FileSpec install_path; |
| 981 | }; |
| 982 | |
| 983 | CommandObjectProcessLoad(CommandInterpreter &interpreter) |
| 984 | : CommandObjectParsed(interpreter, "process load", |
| 985 | "Load a shared library into the current process.", |
| 986 | "process load <filename> [<filename> ...]", |
| 987 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 988 | eCommandProcessMustBeLaunched | |
| 989 | eCommandProcessMustBePaused), |
| 990 | m_options() {} |
| 991 | |
| 992 | ~CommandObjectProcessLoad() override = default; |
| 993 | |
| 994 | Options *GetOptions() override { return &m_options; } |
| 995 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 996 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 997 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 998 | Process *process = m_exe_ctx.GetProcessPtr(); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 999 | |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1000 | for (auto &entry : command.entries()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1001 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | PlatformSP platform = process->GetTarget().GetPlatform(); |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1003 | llvm::StringRef image_path = entry.ref; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1004 | uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN; |
Tamas Berghammer | 4fbd67a | 2015-12-08 13:43:59 +0000 | [diff] [blame] | 1005 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | if (!m_options.do_install) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1007 | FileSpec image_spec(image_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | platform->ResolveRemotePath(image_spec, image_spec); |
| 1009 | image_token = |
| 1010 | platform->LoadImage(process, FileSpec(), image_spec, error); |
| 1011 | } else if (m_options.install_path) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1012 | FileSpec image_spec(image_path); |
| 1013 | FileSystem::Instance().Resolve(image_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | platform->ResolveRemotePath(m_options.install_path, |
| 1015 | m_options.install_path); |
| 1016 | image_token = platform->LoadImage(process, image_spec, |
| 1017 | m_options.install_path, error); |
| 1018 | } else { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1019 | FileSpec image_spec(image_path); |
| 1020 | FileSystem::Instance().Resolve(image_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1021 | image_token = |
| 1022 | platform->LoadImage(process, image_spec, FileSpec(), error); |
| 1023 | } |
Tamas Berghammer | 4fbd67a | 2015-12-08 13:43:59 +0000 | [diff] [blame] | 1024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | if (image_token != LLDB_INVALID_IMAGE_TOKEN) { |
| 1026 | result.AppendMessageWithFormat( |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1027 | "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(), |
| 1028 | image_token); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1029 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1030 | } else { |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1031 | result.AppendErrorWithFormat("failed to load '%s': %s", |
| 1032 | image_path.str().c_str(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | error.AsCString()); |
| 1034 | result.SetStatus(eReturnStatusFailed); |
| 1035 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1036 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1037 | return result.Succeeded(); |
| 1038 | } |
| 1039 | |
| 1040 | CommandOptions m_options; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1041 | }; |
| 1042 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1043 | // CommandObjectProcessUnload |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1044 | #pragma mark CommandObjectProcessUnload |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1046 | class CommandObjectProcessUnload : public CommandObjectParsed { |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1047 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1048 | CommandObjectProcessUnload(CommandInterpreter &interpreter) |
| 1049 | : CommandObjectParsed( |
| 1050 | interpreter, "process unload", |
| 1051 | "Unload a shared library from the current process using the index " |
| 1052 | "returned by a previous call to \"process load\".", |
| 1053 | "process unload <index>", |
| 1054 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1055 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1056 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | ~CommandObjectProcessUnload() override = default; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1058 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1059 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1060 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1061 | Process *process = m_exe_ctx.GetProcessPtr(); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1062 | |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1063 | for (auto &entry : command.entries()) { |
| 1064 | uint32_t image_token; |
| 1065 | if (entry.ref.getAsInteger(0, image_token)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1066 | result.AppendErrorWithFormat("invalid image index argument '%s'", |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 1067 | entry.ref.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1068 | result.SetStatus(eReturnStatusFailed); |
| 1069 | break; |
| 1070 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1071 | Status error(process->GetTarget().GetPlatform()->UnloadImage( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1072 | process, image_token)); |
| 1073 | if (error.Success()) { |
| 1074 | result.AppendMessageWithFormat( |
| 1075 | "Unloading shared library with index %u...ok\n", image_token); |
| 1076 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1077 | } else { |
| 1078 | result.AppendErrorWithFormat("failed to unload image: %s", |
| 1079 | error.AsCString()); |
| 1080 | result.SetStatus(eReturnStatusFailed); |
| 1081 | break; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1082 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1084 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1085 | return result.Succeeded(); |
| 1086 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1087 | }; |
| 1088 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | // CommandObjectProcessSignal |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1090 | #pragma mark CommandObjectProcessSignal |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | class CommandObjectProcessSignal : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1093 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1094 | CommandObjectProcessSignal(CommandInterpreter &interpreter) |
| 1095 | : CommandObjectParsed(interpreter, "process signal", |
| 1096 | "Send a UNIX signal to the current target process.", |
| 1097 | nullptr, eCommandRequiresProcess | |
| 1098 | eCommandTryTargetAPILock) { |
| 1099 | CommandArgumentEntry arg; |
| 1100 | CommandArgumentData signal_arg; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | // Define the first (and only) variant of this arg. |
| 1103 | signal_arg.arg_type = eArgTypeUnixSignal; |
| 1104 | signal_arg.arg_repetition = eArgRepeatPlain; |
| 1105 | |
| 1106 | // There is only one variant this argument could be; put it into the |
| 1107 | // argument entry. |
| 1108 | arg.push_back(signal_arg); |
| 1109 | |
| 1110 | // Push the data for the first argument into the m_arguments vector. |
| 1111 | m_arguments.push_back(arg); |
| 1112 | } |
| 1113 | |
| 1114 | ~CommandObjectProcessSignal() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1115 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1116 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1117 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1118 | Process *process = m_exe_ctx.GetProcessPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | if (command.GetArgumentCount() == 1) { |
| 1121 | int signo = LLDB_INVALID_SIGNAL_NUMBER; |
| 1122 | |
| 1123 | const char *signal_name = command.GetArgumentAtIndex(0); |
| 1124 | if (::isxdigit(signal_name[0])) |
| 1125 | signo = |
| 1126 | StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0); |
| 1127 | else |
| 1128 | signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name); |
| 1129 | |
| 1130 | if (signo == LLDB_INVALID_SIGNAL_NUMBER) { |
| 1131 | result.AppendErrorWithFormat("Invalid signal argument '%s'.\n", |
| 1132 | command.GetArgumentAtIndex(0)); |
| 1133 | result.SetStatus(eReturnStatusFailed); |
| 1134 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1135 | Status error(process->Signal(signo)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1136 | if (error.Success()) { |
| 1137 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1138 | } else { |
| 1139 | result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo, |
| 1140 | error.AsCString()); |
| 1141 | result.SetStatus(eReturnStatusFailed); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1142 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } else { |
| 1145 | result.AppendErrorWithFormat( |
| 1146 | "'%s' takes exactly one signal number argument:\nUsage: %s\n", |
| 1147 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 1148 | result.SetStatus(eReturnStatusFailed); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1149 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | return result.Succeeded(); |
| 1151 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1152 | }; |
| 1153 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1154 | // CommandObjectProcessInterrupt |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1155 | #pragma mark CommandObjectProcessInterrupt |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1156 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1157 | class CommandObjectProcessInterrupt : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1158 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1159 | CommandObjectProcessInterrupt(CommandInterpreter &interpreter) |
| 1160 | : CommandObjectParsed(interpreter, "process interrupt", |
| 1161 | "Interrupt the current target process.", |
| 1162 | "process interrupt", |
| 1163 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1164 | eCommandProcessMustBeLaunched) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1166 | ~CommandObjectProcessInterrupt() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1167 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1168 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1169 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1170 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1171 | if (process == nullptr) { |
| 1172 | result.AppendError("no process to halt"); |
| 1173 | result.SetStatus(eReturnStatusFailed); |
| 1174 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1175 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1176 | |
| 1177 | if (command.GetArgumentCount() == 0) { |
| 1178 | bool clear_thread_plans = true; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1179 | Status error(process->Halt(clear_thread_plans)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1180 | if (error.Success()) { |
| 1181 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1182 | } else { |
| 1183 | result.AppendErrorWithFormat("Failed to halt process: %s\n", |
| 1184 | error.AsCString()); |
| 1185 | result.SetStatus(eReturnStatusFailed); |
| 1186 | } |
| 1187 | } else { |
| 1188 | result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", |
| 1189 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 1190 | result.SetStatus(eReturnStatusFailed); |
| 1191 | } |
| 1192 | return result.Succeeded(); |
| 1193 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1194 | }; |
| 1195 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1196 | // CommandObjectProcessKill |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1197 | #pragma mark CommandObjectProcessKill |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1199 | class CommandObjectProcessKill : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1200 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1201 | CommandObjectProcessKill(CommandInterpreter &interpreter) |
| 1202 | : CommandObjectParsed(interpreter, "process kill", |
| 1203 | "Terminate the current target process.", |
| 1204 | "process kill", |
| 1205 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1206 | eCommandProcessMustBeLaunched) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1208 | ~CommandObjectProcessKill() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1209 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1210 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1211 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1212 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1213 | if (process == nullptr) { |
| 1214 | result.AppendError("no process to kill"); |
| 1215 | result.SetStatus(eReturnStatusFailed); |
| 1216 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1217 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (command.GetArgumentCount() == 0) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1220 | Status error(process->Destroy(true)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1221 | if (error.Success()) { |
| 1222 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1223 | } else { |
| 1224 | result.AppendErrorWithFormat("Failed to kill process: %s\n", |
| 1225 | error.AsCString()); |
| 1226 | result.SetStatus(eReturnStatusFailed); |
| 1227 | } |
| 1228 | } else { |
| 1229 | result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", |
| 1230 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 1231 | result.SetStatus(eReturnStatusFailed); |
| 1232 | } |
| 1233 | return result.Succeeded(); |
| 1234 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1235 | }; |
| 1236 | |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1237 | // CommandObjectProcessSaveCore |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1238 | #pragma mark CommandObjectProcessSaveCore |
| 1239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1240 | class CommandObjectProcessSaveCore : public CommandObjectParsed { |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1241 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1242 | CommandObjectProcessSaveCore(CommandInterpreter &interpreter) |
| 1243 | : CommandObjectParsed(interpreter, "process save-core", |
| 1244 | "Save the current process as a core file using an " |
| 1245 | "appropriate file type.", |
| 1246 | "process save-core FILE", |
| 1247 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1248 | eCommandProcessMustBeLaunched) {} |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 1249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1250 | ~CommandObjectProcessSaveCore() override = default; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 1251 | |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1252 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1253 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1254 | ProcessSP process_sp = m_exe_ctx.GetProcessSP(); |
| 1255 | if (process_sp) { |
| 1256 | if (command.GetArgumentCount() == 1) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1257 | FileSpec output_file(command.GetArgumentAtIndex(0)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1258 | Status error = PluginManager::SaveCore(process_sp, output_file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1259 | if (error.Success()) { |
| 1260 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1261 | } else { |
| 1262 | result.AppendErrorWithFormat( |
| 1263 | "Failed to save core file for process: %s\n", error.AsCString()); |
| 1264 | result.SetStatus(eReturnStatusFailed); |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1265 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | } else { |
| 1267 | result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n", |
| 1268 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 1269 | result.SetStatus(eReturnStatusFailed); |
| 1270 | } |
| 1271 | } else { |
| 1272 | result.AppendError("invalid process"); |
| 1273 | result.SetStatus(eReturnStatusFailed); |
| 1274 | return false; |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1275 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1276 | |
| 1277 | return result.Succeeded(); |
| 1278 | } |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 1279 | }; |
| 1280 | |
Jim Ingham | 4b9bea8 | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1281 | // CommandObjectProcessStatus |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1282 | #pragma mark CommandObjectProcessStatus |
| 1283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1284 | class CommandObjectProcessStatus : public CommandObjectParsed { |
Jim Ingham | 4b9bea8 | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1285 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1286 | CommandObjectProcessStatus(CommandInterpreter &interpreter) |
| 1287 | : CommandObjectParsed( |
| 1288 | interpreter, "process status", |
| 1289 | "Show status and stop location for the current target process.", |
| 1290 | "process status", |
| 1291 | eCommandRequiresProcess | eCommandTryTargetAPILock) {} |
Jim Ingham | 4b9bea8 | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1292 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1293 | ~CommandObjectProcessStatus() override = default; |
Jim Ingham | 4b9bea8 | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1295 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1296 | Stream &strm = result.GetOutputStream(); |
| 1297 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1298 | // No need to check "process" for validity as eCommandRequiresProcess |
| 1299 | // ensures it is valid |
| 1300 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1301 | const bool only_threads_with_stop_reason = true; |
| 1302 | const uint32_t start_frame = 0; |
| 1303 | const uint32_t num_frames = 1; |
| 1304 | const uint32_t num_frames_with_source = 1; |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1305 | const bool stop_format = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1306 | process->GetStatus(strm); |
| 1307 | process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1308 | num_frames, num_frames_with_source, stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1309 | return result.Succeeded(); |
| 1310 | } |
Jim Ingham | 4b9bea8 | 2010-06-18 01:23:09 +0000 | [diff] [blame] | 1311 | }; |
| 1312 | |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1313 | // CommandObjectProcessHandle |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1314 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1315 | static constexpr OptionDefinition g_process_handle_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1316 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1317 | { LLDB_OPT_SET_1, false, "stop", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." }, |
| 1318 | { LLDB_OPT_SET_1, false, "notify", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." }, |
| 1319 | { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1320 | // clang-format on |
| 1321 | }; |
| 1322 | |
Jim Ingham | bb9caf7 | 2010-12-09 18:58:16 +0000 | [diff] [blame] | 1323 | #pragma mark CommandObjectProcessHandle |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1324 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1325 | class CommandObjectProcessHandle : public CommandObjectParsed { |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1326 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1327 | class CommandOptions : public Options { |
| 1328 | public: |
| 1329 | CommandOptions() : Options() { OptionParsingStarting(nullptr); } |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1331 | ~CommandOptions() override = default; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1332 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1333 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1334 | ExecutionContext *execution_context) override { |
| 1335 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1336 | const int short_option = m_getopt_table[option_idx].val; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1337 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1338 | switch (short_option) { |
| 1339 | case 's': |
| 1340 | stop = option_arg; |
| 1341 | break; |
| 1342 | case 'n': |
| 1343 | notify = option_arg; |
| 1344 | break; |
| 1345 | case 'p': |
| 1346 | pass = option_arg; |
| 1347 | break; |
| 1348 | default: |
| 1349 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 1350 | short_option); |
| 1351 | break; |
| 1352 | } |
| 1353 | return error; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1356 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1357 | stop.clear(); |
| 1358 | notify.clear(); |
| 1359 | pass.clear(); |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1362 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1363 | return llvm::makeArrayRef(g_process_handle_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1364 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Instance variables to hold the values for command options. |
| 1367 | |
| 1368 | std::string stop; |
| 1369 | std::string notify; |
| 1370 | std::string pass; |
| 1371 | }; |
| 1372 | |
| 1373 | CommandObjectProcessHandle(CommandInterpreter &interpreter) |
| 1374 | : CommandObjectParsed(interpreter, "process handle", |
| 1375 | "Manage LLDB handling of OS signals for the " |
| 1376 | "current target process. Defaults to showing " |
| 1377 | "current policy.", |
| 1378 | nullptr), |
| 1379 | m_options() { |
| 1380 | SetHelpLong("\nIf no signals are specified, update them all. If no update " |
| 1381 | "option is specified, list the current values."); |
| 1382 | CommandArgumentEntry arg; |
| 1383 | CommandArgumentData signal_arg; |
| 1384 | |
| 1385 | signal_arg.arg_type = eArgTypeUnixSignal; |
| 1386 | signal_arg.arg_repetition = eArgRepeatStar; |
| 1387 | |
| 1388 | arg.push_back(signal_arg); |
| 1389 | |
| 1390 | m_arguments.push_back(arg); |
| 1391 | } |
| 1392 | |
| 1393 | ~CommandObjectProcessHandle() override = default; |
| 1394 | |
| 1395 | Options *GetOptions() override { return &m_options; } |
| 1396 | |
| 1397 | bool VerifyCommandOptionValue(const std::string &option, int &real_value) { |
| 1398 | bool okay = true; |
| 1399 | bool success = false; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 1400 | bool tmp_value = OptionArgParser::ToBoolean(option, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (success && tmp_value) |
| 1403 | real_value = 1; |
| 1404 | else if (success && !tmp_value) |
| 1405 | real_value = 0; |
| 1406 | else { |
| 1407 | // If the value isn't 'true' or 'false', it had better be 0 or 1. |
| 1408 | real_value = StringConvert::ToUInt32(option.c_str(), 3); |
| 1409 | if (real_value != 0 && real_value != 1) |
| 1410 | okay = false; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1413 | return okay; |
| 1414 | } |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | void PrintSignalHeader(Stream &str) { |
| 1417 | str.Printf("NAME PASS STOP NOTIFY\n"); |
| 1418 | str.Printf("=========== ===== ===== ======\n"); |
| 1419 | } |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1421 | void PrintSignal(Stream &str, int32_t signo, const char *sig_name, |
| 1422 | const UnixSignalsSP &signals_sp) { |
| 1423 | bool stop; |
| 1424 | bool suppress; |
| 1425 | bool notify; |
| 1426 | |
| 1427 | str.Printf("%-11s ", sig_name); |
| 1428 | if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) { |
| 1429 | bool pass = !suppress; |
| 1430 | str.Printf("%s %s %s", (pass ? "true " : "false"), |
| 1431 | (stop ? "true " : "false"), (notify ? "true " : "false")); |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1432 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1433 | str.Printf("\n"); |
| 1434 | } |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1436 | void PrintSignalInformation(Stream &str, Args &signal_args, |
| 1437 | int num_valid_signals, |
| 1438 | const UnixSignalsSP &signals_sp) { |
| 1439 | PrintSignalHeader(str); |
| 1440 | |
| 1441 | if (num_valid_signals > 0) { |
| 1442 | size_t num_args = signal_args.GetArgumentCount(); |
| 1443 | for (size_t i = 0; i < num_args; ++i) { |
| 1444 | int32_t signo = signals_sp->GetSignalNumberFromName( |
| 1445 | signal_args.GetArgumentAtIndex(i)); |
| 1446 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 1447 | PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i), |
| 1448 | signals_sp); |
| 1449 | } |
| 1450 | } else // Print info for ALL signals |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1451 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1452 | int32_t signo = signals_sp->GetFirstSignalNumber(); |
| 1453 | while (signo != LLDB_INVALID_SIGNAL_NUMBER) { |
| 1454 | PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo), |
| 1455 | signals_sp); |
| 1456 | signo = signals_sp->GetNextSignalNumber(signo); |
| 1457 | } |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1458 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1459 | } |
Caroline Tice | 10ad799 | 2010-10-14 21:31:13 +0000 | [diff] [blame] | 1460 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1461 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | bool DoExecute(Args &signal_args, CommandReturnObject &result) override { |
Jonas Devlieghere | 5717986 | 2019-04-27 06:19:42 +0000 | [diff] [blame^] | 1463 | TargetSP target_sp = GetDebugger().GetSelectedTarget(); |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | if (!target_sp) { |
| 1466 | result.AppendError("No current target;" |
| 1467 | " cannot handle signals until you have a valid target " |
| 1468 | "and process.\n"); |
| 1469 | result.SetStatus(eReturnStatusFailed); |
| 1470 | return false; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | ProcessSP process_sp = target_sp->GetProcessSP(); |
| 1474 | |
| 1475 | if (!process_sp) { |
| 1476 | result.AppendError("No current process; cannot handle signals until you " |
| 1477 | "have a valid process.\n"); |
| 1478 | result.SetStatus(eReturnStatusFailed); |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | int stop_action = -1; // -1 means leave the current setting alone |
| 1483 | int pass_action = -1; // -1 means leave the current setting alone |
| 1484 | int notify_action = -1; // -1 means leave the current setting alone |
| 1485 | |
| 1486 | if (!m_options.stop.empty() && |
| 1487 | !VerifyCommandOptionValue(m_options.stop, stop_action)) { |
| 1488 | result.AppendError("Invalid argument for command option --stop; must be " |
| 1489 | "true or false.\n"); |
| 1490 | result.SetStatus(eReturnStatusFailed); |
| 1491 | return false; |
| 1492 | } |
| 1493 | |
| 1494 | if (!m_options.notify.empty() && |
| 1495 | !VerifyCommandOptionValue(m_options.notify, notify_action)) { |
| 1496 | result.AppendError("Invalid argument for command option --notify; must " |
| 1497 | "be true or false.\n"); |
| 1498 | result.SetStatus(eReturnStatusFailed); |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
| 1502 | if (!m_options.pass.empty() && |
| 1503 | !VerifyCommandOptionValue(m_options.pass, pass_action)) { |
| 1504 | result.AppendError("Invalid argument for command option --pass; must be " |
| 1505 | "true or false.\n"); |
| 1506 | result.SetStatus(eReturnStatusFailed); |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
| 1510 | size_t num_args = signal_args.GetArgumentCount(); |
| 1511 | UnixSignalsSP signals_sp = process_sp->GetUnixSignals(); |
| 1512 | int num_signals_set = 0; |
| 1513 | |
| 1514 | if (num_args > 0) { |
Zachary Turner | d6a2475 | 2016-11-22 17:10:15 +0000 | [diff] [blame] | 1515 | for (const auto &arg : signal_args) { |
| 1516 | int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1517 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) { |
| 1518 | // Casting the actions as bools here should be okay, because |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1519 | // VerifyCommandOptionValue guarantees the value is either 0 or 1. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1520 | if (stop_action != -1) |
| 1521 | signals_sp->SetShouldStop(signo, stop_action); |
| 1522 | if (pass_action != -1) { |
| 1523 | bool suppress = !pass_action; |
| 1524 | signals_sp->SetShouldSuppress(signo, suppress); |
| 1525 | } |
| 1526 | if (notify_action != -1) |
| 1527 | signals_sp->SetShouldNotify(signo, notify_action); |
| 1528 | ++num_signals_set; |
| 1529 | } else { |
| 1530 | result.AppendErrorWithFormat("Invalid signal name '%s'\n", |
Zachary Turner | d6a2475 | 2016-11-22 17:10:15 +0000 | [diff] [blame] | 1531 | arg.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | } else { |
| 1535 | // No signal specified, if any command options were specified, update ALL |
| 1536 | // signals. |
| 1537 | if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) { |
| 1538 | if (m_interpreter.Confirm( |
| 1539 | "Do you really want to update all the signals?", false)) { |
| 1540 | int32_t signo = signals_sp->GetFirstSignalNumber(); |
| 1541 | while (signo != LLDB_INVALID_SIGNAL_NUMBER) { |
| 1542 | if (notify_action != -1) |
| 1543 | signals_sp->SetShouldNotify(signo, notify_action); |
| 1544 | if (stop_action != -1) |
| 1545 | signals_sp->SetShouldStop(signo, stop_action); |
| 1546 | if (pass_action != -1) { |
| 1547 | bool suppress = !pass_action; |
| 1548 | signals_sp->SetShouldSuppress(signo, suppress); |
| 1549 | } |
| 1550 | signo = signals_sp->GetNextSignalNumber(signo); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | PrintSignalInformation(result.GetOutputStream(), signal_args, |
| 1557 | num_signals_set, signals_sp); |
| 1558 | |
| 1559 | if (num_signals_set > 0) |
| 1560 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1561 | else |
| 1562 | result.SetStatus(eReturnStatusFailed); |
| 1563 | |
| 1564 | return result.Succeeded(); |
| 1565 | } |
| 1566 | |
| 1567 | CommandOptions m_options; |
Caroline Tice | 3573135 | 2010-10-13 20:44:39 +0000 | [diff] [blame] | 1568 | }; |
| 1569 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1570 | // CommandObjectMultiwordProcess |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1571 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1572 | CommandObjectMultiwordProcess::CommandObjectMultiwordProcess( |
| 1573 | CommandInterpreter &interpreter) |
| 1574 | : CommandObjectMultiword( |
| 1575 | interpreter, "process", |
| 1576 | "Commands for interacting with processes on the current platform.", |
| 1577 | "process <subcommand> [<subcommand-options>]") { |
| 1578 | LoadSubCommand("attach", |
| 1579 | CommandObjectSP(new CommandObjectProcessAttach(interpreter))); |
| 1580 | LoadSubCommand("launch", |
| 1581 | CommandObjectSP(new CommandObjectProcessLaunch(interpreter))); |
| 1582 | LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue( |
| 1583 | interpreter))); |
| 1584 | LoadSubCommand("connect", |
| 1585 | CommandObjectSP(new CommandObjectProcessConnect(interpreter))); |
| 1586 | LoadSubCommand("detach", |
| 1587 | CommandObjectSP(new CommandObjectProcessDetach(interpreter))); |
| 1588 | LoadSubCommand("load", |
| 1589 | CommandObjectSP(new CommandObjectProcessLoad(interpreter))); |
| 1590 | LoadSubCommand("unload", |
| 1591 | CommandObjectSP(new CommandObjectProcessUnload(interpreter))); |
| 1592 | LoadSubCommand("signal", |
| 1593 | CommandObjectSP(new CommandObjectProcessSignal(interpreter))); |
| 1594 | LoadSubCommand("handle", |
| 1595 | CommandObjectSP(new CommandObjectProcessHandle(interpreter))); |
| 1596 | LoadSubCommand("status", |
| 1597 | CommandObjectSP(new CommandObjectProcessStatus(interpreter))); |
| 1598 | LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt( |
| 1599 | interpreter))); |
| 1600 | LoadSubCommand("kill", |
| 1601 | CommandObjectSP(new CommandObjectProcessKill(interpreter))); |
| 1602 | LoadSubCommand("plugin", |
| 1603 | CommandObjectSP(new CommandObjectProcessPlugin(interpreter))); |
| 1604 | LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore( |
| 1605 | interpreter))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 1608 | CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default; |