15ffd83dbSDimitry Andric //===-- CommandObjectProcess.cpp ------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "CommandObjectProcess.h" 10e8d8bef9SDimitry Andric #include "CommandOptionsProcessLaunch.h" 11*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h" 12*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h" 13*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h" 14*0b57cec5SDimitry Andric #include "lldb/Core/Module.h" 15*0b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h" 16*0b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h" 17*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 18*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 19*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 20*0b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h" 21*0b57cec5SDimitry Andric #include "lldb/Target/Platform.h" 22*0b57cec5SDimitry Andric #include "lldb/Target/Process.h" 23*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h" 24*0b57cec5SDimitry Andric #include "lldb/Target/Target.h" 25*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 26*0b57cec5SDimitry Andric #include "lldb/Target/UnixSignals.h" 27*0b57cec5SDimitry Andric #include "lldb/Utility/Args.h" 28*0b57cec5SDimitry Andric #include "lldb/Utility/State.h" 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric using namespace lldb; 31*0b57cec5SDimitry Andric using namespace lldb_private; 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed { 34*0b57cec5SDimitry Andric public: 35*0b57cec5SDimitry Andric CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter, 36*0b57cec5SDimitry Andric const char *name, const char *help, 37*0b57cec5SDimitry Andric const char *syntax, uint32_t flags, 38*0b57cec5SDimitry Andric const char *new_process_action) 39*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, flags), 40*0b57cec5SDimitry Andric m_new_process_action(new_process_action) {} 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric ~CommandObjectProcessLaunchOrAttach() override = default; 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric protected: 45*0b57cec5SDimitry Andric bool StopProcessIfNecessary(Process *process, StateType &state, 46*0b57cec5SDimitry Andric CommandReturnObject &result) { 47*0b57cec5SDimitry Andric state = eStateInvalid; 48*0b57cec5SDimitry Andric if (process) { 49*0b57cec5SDimitry Andric state = process->GetState(); 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric if (process->IsAlive() && state != eStateConnected) { 52e8d8bef9SDimitry Andric std::string message; 53*0b57cec5SDimitry Andric if (process->GetState() == eStateAttaching) 54e8d8bef9SDimitry Andric message = 55e8d8bef9SDimitry Andric llvm::formatv("There is a pending attach, abort it and {0}?", 56e8d8bef9SDimitry Andric m_new_process_action); 57*0b57cec5SDimitry Andric else if (process->GetShouldDetach()) 58e8d8bef9SDimitry Andric message = llvm::formatv( 59e8d8bef9SDimitry Andric "There is a running process, detach from it and {0}?", 60e8d8bef9SDimitry Andric m_new_process_action); 61*0b57cec5SDimitry Andric else 62e8d8bef9SDimitry Andric message = 63e8d8bef9SDimitry Andric llvm::formatv("There is a running process, kill it and {0}?", 64e8d8bef9SDimitry Andric m_new_process_action); 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric if (!m_interpreter.Confirm(message, true)) { 67*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 68*0b57cec5SDimitry Andric return false; 69*0b57cec5SDimitry Andric } else { 70*0b57cec5SDimitry Andric if (process->GetShouldDetach()) { 71*0b57cec5SDimitry Andric bool keep_stopped = false; 72*0b57cec5SDimitry Andric Status detach_error(process->Detach(keep_stopped)); 73*0b57cec5SDimitry Andric if (detach_error.Success()) { 74*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 75*0b57cec5SDimitry Andric process = nullptr; 76*0b57cec5SDimitry Andric } else { 77*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 78*0b57cec5SDimitry Andric "Failed to detach from process: %s\n", 79*0b57cec5SDimitry Andric detach_error.AsCString()); 80*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 81*0b57cec5SDimitry Andric } 82*0b57cec5SDimitry Andric } else { 83*0b57cec5SDimitry Andric Status destroy_error(process->Destroy(false)); 84*0b57cec5SDimitry Andric if (destroy_error.Success()) { 85*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 86*0b57cec5SDimitry Andric process = nullptr; 87*0b57cec5SDimitry Andric } else { 88*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to kill process: %s\n", 89*0b57cec5SDimitry Andric destroy_error.AsCString()); 90*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 91*0b57cec5SDimitry Andric } 92*0b57cec5SDimitry Andric } 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric } 95*0b57cec5SDimitry Andric } 96*0b57cec5SDimitry Andric return result.Succeeded(); 97*0b57cec5SDimitry Andric } 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric std::string m_new_process_action; 100*0b57cec5SDimitry Andric }; 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric // CommandObjectProcessLaunch 103*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessLaunch 104*0b57cec5SDimitry Andric class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach { 105*0b57cec5SDimitry Andric public: 106*0b57cec5SDimitry Andric CommandObjectProcessLaunch(CommandInterpreter &interpreter) 107*0b57cec5SDimitry Andric : CommandObjectProcessLaunchOrAttach( 108*0b57cec5SDimitry Andric interpreter, "process launch", 109*0b57cec5SDimitry Andric "Launch the executable in the debugger.", nullptr, 110*0b57cec5SDimitry Andric eCommandRequiresTarget, "restart"), 111*0b57cec5SDimitry Andric m_options() { 112*0b57cec5SDimitry Andric CommandArgumentEntry arg; 113*0b57cec5SDimitry Andric CommandArgumentData run_args_arg; 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 116*0b57cec5SDimitry Andric run_args_arg.arg_type = eArgTypeRunArgs; 117*0b57cec5SDimitry Andric run_args_arg.arg_repetition = eArgRepeatOptional; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 120*0b57cec5SDimitry Andric // argument entry. 121*0b57cec5SDimitry Andric arg.push_back(run_args_arg); 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 124*0b57cec5SDimitry Andric m_arguments.push_back(arg); 125*0b57cec5SDimitry Andric } 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric ~CommandObjectProcessLaunch() override = default; 128*0b57cec5SDimitry Andric 1299dba64beSDimitry Andric void 1309dba64beSDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 131*0b57cec5SDimitry Andric OptionElementVector &opt_element_vector) override { 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 134*0b57cec5SDimitry Andric GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 135*0b57cec5SDimitry Andric request, nullptr); 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric const char *GetRepeatCommand(Args ¤t_command_args, 141*0b57cec5SDimitry Andric uint32_t index) override { 142*0b57cec5SDimitry Andric // No repeat for "process launch"... 143*0b57cec5SDimitry Andric return ""; 144*0b57cec5SDimitry Andric } 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric protected: 147*0b57cec5SDimitry Andric bool DoExecute(Args &launch_args, CommandReturnObject &result) override { 148*0b57cec5SDimitry Andric Debugger &debugger = GetDebugger(); 149*0b57cec5SDimitry Andric Target *target = debugger.GetSelectedTarget().get(); 150*0b57cec5SDimitry Andric // If our listener is nullptr, users aren't allows to launch 151*0b57cec5SDimitry Andric ModuleSP exe_module_sp = target->GetExecutableModule(); 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric if (exe_module_sp == nullptr) { 154*0b57cec5SDimitry Andric result.AppendError("no file in target, create a debug target using the " 155*0b57cec5SDimitry Andric "'target create' command"); 156*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 157*0b57cec5SDimitry Andric return false; 158*0b57cec5SDimitry Andric } 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric StateType state = eStateInvalid; 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result)) 163*0b57cec5SDimitry Andric return false; 164*0b57cec5SDimitry Andric 165*0b57cec5SDimitry Andric llvm::StringRef target_settings_argv0 = target->GetArg0(); 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric // Determine whether we will disable ASLR or leave it in the default state 168*0b57cec5SDimitry Andric // (i.e. enabled if the platform supports it). First check if the process 169*0b57cec5SDimitry Andric // launch options explicitly turn on/off 170*0b57cec5SDimitry Andric // disabling ASLR. If so, use that setting; 171*0b57cec5SDimitry Andric // otherwise, use the 'settings target.disable-aslr' setting. 172*0b57cec5SDimitry Andric bool disable_aslr = false; 173*0b57cec5SDimitry Andric if (m_options.disable_aslr != eLazyBoolCalculate) { 174*0b57cec5SDimitry Andric // The user specified an explicit setting on the process launch line. 175*0b57cec5SDimitry Andric // Use it. 176*0b57cec5SDimitry Andric disable_aslr = (m_options.disable_aslr == eLazyBoolYes); 177*0b57cec5SDimitry Andric } else { 178*0b57cec5SDimitry Andric // The user did not explicitly specify whether to disable ASLR. Fall 179*0b57cec5SDimitry Andric // back to the target.disable-aslr setting. 180*0b57cec5SDimitry Andric disable_aslr = target->GetDisableASLR(); 181*0b57cec5SDimitry Andric } 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric if (disable_aslr) 184*0b57cec5SDimitry Andric m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR); 185*0b57cec5SDimitry Andric else 186*0b57cec5SDimitry Andric m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR); 187*0b57cec5SDimitry Andric 188e8d8bef9SDimitry Andric if (target->GetInheritTCC()) 189e8d8bef9SDimitry Andric m_options.launch_info.GetFlags().Set(eLaunchFlagInheritTCCFromParent); 190e8d8bef9SDimitry Andric 191*0b57cec5SDimitry Andric if (target->GetDetachOnError()) 192*0b57cec5SDimitry Andric m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError); 193*0b57cec5SDimitry Andric 194*0b57cec5SDimitry Andric if (target->GetDisableSTDIO()) 195*0b57cec5SDimitry Andric m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO); 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric // Merge the launch info environment with the target environment. 198*0b57cec5SDimitry Andric Environment target_env = target->GetEnvironment(); 199*0b57cec5SDimitry Andric m_options.launch_info.GetEnvironment().insert(target_env.begin(), 200*0b57cec5SDimitry Andric target_env.end()); 201*0b57cec5SDimitry Andric 202*0b57cec5SDimitry Andric if (!target_settings_argv0.empty()) { 203*0b57cec5SDimitry Andric m_options.launch_info.GetArguments().AppendArgument( 204*0b57cec5SDimitry Andric target_settings_argv0); 205*0b57cec5SDimitry Andric m_options.launch_info.SetExecutableFile( 206*0b57cec5SDimitry Andric exe_module_sp->GetPlatformFileSpec(), false); 207*0b57cec5SDimitry Andric } else { 208*0b57cec5SDimitry Andric m_options.launch_info.SetExecutableFile( 209*0b57cec5SDimitry Andric exe_module_sp->GetPlatformFileSpec(), true); 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric if (launch_args.GetArgumentCount() == 0) { 213*0b57cec5SDimitry Andric m_options.launch_info.GetArguments().AppendArguments( 214*0b57cec5SDimitry Andric target->GetProcessLaunchInfo().GetArguments()); 215*0b57cec5SDimitry Andric } else { 216*0b57cec5SDimitry Andric m_options.launch_info.GetArguments().AppendArguments(launch_args); 217*0b57cec5SDimitry Andric // Save the arguments for subsequent runs in the current target. 218*0b57cec5SDimitry Andric target->SetRunArguments(launch_args); 219*0b57cec5SDimitry Andric } 220*0b57cec5SDimitry Andric 221*0b57cec5SDimitry Andric StreamString stream; 222*0b57cec5SDimitry Andric Status error = target->Launch(m_options.launch_info, &stream); 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric if (error.Success()) { 225*0b57cec5SDimitry Andric ProcessSP process_sp(target->GetProcessSP()); 226*0b57cec5SDimitry Andric if (process_sp) { 227*0b57cec5SDimitry Andric // There is a race condition where this thread will return up the call 228*0b57cec5SDimitry Andric // stack to the main command handler and show an (lldb) prompt before 229*0b57cec5SDimitry Andric // HandlePrivateEvent (from PrivateStateThread) has a chance to call 230*0b57cec5SDimitry Andric // PushProcessIOHandler(). 231*0b57cec5SDimitry Andric process_sp->SyncIOHandler(0, std::chrono::seconds(2)); 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric llvm::StringRef data = stream.GetString(); 234*0b57cec5SDimitry Andric if (!data.empty()) 235*0b57cec5SDimitry Andric result.AppendMessage(data); 236*0b57cec5SDimitry Andric const char *archname = 237*0b57cec5SDimitry Andric exe_module_sp->GetArchitecture().GetArchitectureName(); 238*0b57cec5SDimitry Andric result.AppendMessageWithFormat( 239*0b57cec5SDimitry Andric "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(), 240*0b57cec5SDimitry Andric exe_module_sp->GetFileSpec().GetPath().c_str(), archname); 241*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 242*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 243*0b57cec5SDimitry Andric } else { 244*0b57cec5SDimitry Andric result.AppendError( 245*0b57cec5SDimitry Andric "no error returned from Target::Launch, and target has no process"); 246*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 247*0b57cec5SDimitry Andric } 248*0b57cec5SDimitry Andric } else { 249*0b57cec5SDimitry Andric result.AppendError(error.AsCString()); 250*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 251*0b57cec5SDimitry Andric } 252*0b57cec5SDimitry Andric return result.Succeeded(); 253*0b57cec5SDimitry Andric } 254*0b57cec5SDimitry Andric 255e8d8bef9SDimitry Andric CommandOptionsProcessLaunch m_options; 256*0b57cec5SDimitry Andric }; 257*0b57cec5SDimitry Andric 2589dba64beSDimitry Andric #define LLDB_OPTIONS_process_attach 2599dba64beSDimitry Andric #include "CommandOptions.inc" 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessAttach 262*0b57cec5SDimitry Andric class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach { 263*0b57cec5SDimitry Andric public: 264*0b57cec5SDimitry Andric class CommandOptions : public Options { 265*0b57cec5SDimitry Andric public: 266*0b57cec5SDimitry Andric CommandOptions() : Options() { 267*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 268*0b57cec5SDimitry Andric // () 269*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric ~CommandOptions() override = default; 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 275*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 276*0b57cec5SDimitry Andric Status error; 277*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 278*0b57cec5SDimitry Andric switch (short_option) { 279*0b57cec5SDimitry Andric case 'c': 280*0b57cec5SDimitry Andric attach_info.SetContinueOnceAttached(true); 281*0b57cec5SDimitry Andric break; 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric case 'p': { 284*0b57cec5SDimitry Andric lldb::pid_t pid; 285*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, pid)) { 286*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid process ID '%s'", 287*0b57cec5SDimitry Andric option_arg.str().c_str()); 288*0b57cec5SDimitry Andric } else { 289*0b57cec5SDimitry Andric attach_info.SetProcessID(pid); 290*0b57cec5SDimitry Andric } 291*0b57cec5SDimitry Andric } break; 292*0b57cec5SDimitry Andric 293*0b57cec5SDimitry Andric case 'P': 294*0b57cec5SDimitry Andric attach_info.SetProcessPluginName(option_arg); 295*0b57cec5SDimitry Andric break; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric case 'n': 298*0b57cec5SDimitry Andric attach_info.GetExecutableFile().SetFile(option_arg, 299*0b57cec5SDimitry Andric FileSpec::Style::native); 300*0b57cec5SDimitry Andric break; 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric case 'w': 303*0b57cec5SDimitry Andric attach_info.SetWaitForLaunch(true); 304*0b57cec5SDimitry Andric break; 305*0b57cec5SDimitry Andric 306*0b57cec5SDimitry Andric case 'i': 307*0b57cec5SDimitry Andric attach_info.SetIgnoreExisting(false); 308*0b57cec5SDimitry Andric break; 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric default: 3119dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 312*0b57cec5SDimitry Andric } 313*0b57cec5SDimitry Andric return error; 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 317*0b57cec5SDimitry Andric attach_info.Clear(); 318*0b57cec5SDimitry Andric } 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 321*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_attach_options); 322*0b57cec5SDimitry Andric } 323*0b57cec5SDimitry Andric 324*0b57cec5SDimitry Andric ProcessAttachInfo attach_info; 325*0b57cec5SDimitry Andric }; 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andric CommandObjectProcessAttach(CommandInterpreter &interpreter) 328*0b57cec5SDimitry Andric : CommandObjectProcessLaunchOrAttach( 329*0b57cec5SDimitry Andric interpreter, "process attach", "Attach to a process.", 330*0b57cec5SDimitry Andric "process attach <cmd-options>", 0, "attach"), 331*0b57cec5SDimitry Andric m_options() {} 332*0b57cec5SDimitry Andric 333*0b57cec5SDimitry Andric ~CommandObjectProcessAttach() override = default; 334*0b57cec5SDimitry Andric 335*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric protected: 338*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 339*0b57cec5SDimitry Andric PlatformSP platform_sp( 340*0b57cec5SDimitry Andric GetDebugger().GetPlatformList().GetSelectedPlatform()); 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric Target *target = GetDebugger().GetSelectedTarget().get(); 343*0b57cec5SDimitry Andric // N.B. The attach should be synchronous. It doesn't help much to get the 344*0b57cec5SDimitry Andric // prompt back between initiating the attach and the target actually 345*0b57cec5SDimitry Andric // stopping. So even if the interpreter is set to be asynchronous, we wait 346*0b57cec5SDimitry Andric // for the stop ourselves here. 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric StateType state = eStateInvalid; 349*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric if (!StopProcessIfNecessary(process, state, result)) 352*0b57cec5SDimitry Andric return false; 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric if (target == nullptr) { 355*0b57cec5SDimitry Andric // If there isn't a current target create one. 356*0b57cec5SDimitry Andric TargetSP new_target_sp; 357*0b57cec5SDimitry Andric Status error; 358*0b57cec5SDimitry Andric 359*0b57cec5SDimitry Andric error = GetDebugger().GetTargetList().CreateTarget( 360*0b57cec5SDimitry Andric GetDebugger(), "", "", eLoadDependentsNo, 361*0b57cec5SDimitry Andric nullptr, // No platform options 362*0b57cec5SDimitry Andric new_target_sp); 363*0b57cec5SDimitry Andric target = new_target_sp.get(); 364*0b57cec5SDimitry Andric if (target == nullptr || error.Fail()) { 365*0b57cec5SDimitry Andric result.AppendError(error.AsCString("Error creating target")); 366*0b57cec5SDimitry Andric return false; 367*0b57cec5SDimitry Andric } 368*0b57cec5SDimitry Andric } 369*0b57cec5SDimitry Andric 370*0b57cec5SDimitry Andric // Record the old executable module, we want to issue a warning if the 371*0b57cec5SDimitry Andric // process of attaching changed the current executable (like somebody said 372*0b57cec5SDimitry Andric // "file foo" then attached to a PID whose executable was bar.) 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric ModuleSP old_exec_module_sp = target->GetExecutableModule(); 375*0b57cec5SDimitry Andric ArchSpec old_arch_spec = target->GetArchitecture(); 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric if (command.GetArgumentCount()) { 378*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", 379*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 380*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 381*0b57cec5SDimitry Andric return false; 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric m_interpreter.UpdateExecutionContext(nullptr); 385*0b57cec5SDimitry Andric StreamString stream; 386*0b57cec5SDimitry Andric const auto error = target->Attach(m_options.attach_info, &stream); 387*0b57cec5SDimitry Andric if (error.Success()) { 388*0b57cec5SDimitry Andric ProcessSP process_sp(target->GetProcessSP()); 389*0b57cec5SDimitry Andric if (process_sp) { 390*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 391*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 392*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 393*0b57cec5SDimitry Andric } else { 394*0b57cec5SDimitry Andric result.AppendError( 395*0b57cec5SDimitry Andric "no error returned from Target::Attach, and target has no process"); 396*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 397*0b57cec5SDimitry Andric } 398*0b57cec5SDimitry Andric } else { 399*0b57cec5SDimitry Andric result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString()); 400*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 401*0b57cec5SDimitry Andric } 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric if (!result.Succeeded()) 404*0b57cec5SDimitry Andric return false; 405*0b57cec5SDimitry Andric 406*0b57cec5SDimitry Andric // Okay, we're done. Last step is to warn if the executable module has 407*0b57cec5SDimitry Andric // changed: 408*0b57cec5SDimitry Andric char new_path[PATH_MAX]; 409*0b57cec5SDimitry Andric ModuleSP new_exec_module_sp(target->GetExecutableModule()); 410*0b57cec5SDimitry Andric if (!old_exec_module_sp) { 411*0b57cec5SDimitry Andric // We might not have a module if we attached to a raw pid... 412*0b57cec5SDimitry Andric if (new_exec_module_sp) { 413*0b57cec5SDimitry Andric new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX); 414*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Executable module set to \"%s\".\n", 415*0b57cec5SDimitry Andric new_path); 416*0b57cec5SDimitry Andric } 417*0b57cec5SDimitry Andric } else if (old_exec_module_sp->GetFileSpec() != 418*0b57cec5SDimitry Andric new_exec_module_sp->GetFileSpec()) { 419*0b57cec5SDimitry Andric char old_path[PATH_MAX]; 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX); 422*0b57cec5SDimitry Andric new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX); 423*0b57cec5SDimitry Andric 424*0b57cec5SDimitry Andric result.AppendWarningWithFormat( 425*0b57cec5SDimitry Andric "Executable module changed from \"%s\" to \"%s\".\n", old_path, 426*0b57cec5SDimitry Andric new_path); 427*0b57cec5SDimitry Andric } 428*0b57cec5SDimitry Andric 429*0b57cec5SDimitry Andric if (!old_arch_spec.IsValid()) { 430*0b57cec5SDimitry Andric result.AppendMessageWithFormat( 431*0b57cec5SDimitry Andric "Architecture set to: %s.\n", 432*0b57cec5SDimitry Andric target->GetArchitecture().GetTriple().getTriple().c_str()); 433*0b57cec5SDimitry Andric } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) { 434*0b57cec5SDimitry Andric result.AppendWarningWithFormat( 435*0b57cec5SDimitry Andric "Architecture changed from %s to %s.\n", 436*0b57cec5SDimitry Andric old_arch_spec.GetTriple().getTriple().c_str(), 437*0b57cec5SDimitry Andric target->GetArchitecture().GetTriple().getTriple().c_str()); 438*0b57cec5SDimitry Andric } 439*0b57cec5SDimitry Andric 440*0b57cec5SDimitry Andric // This supports the use-case scenario of immediately continuing the 441*0b57cec5SDimitry Andric // process once attached. 442*0b57cec5SDimitry Andric if (m_options.attach_info.GetContinueOnceAttached()) 443*0b57cec5SDimitry Andric m_interpreter.HandleCommand("process continue", eLazyBoolNo, result); 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric return result.Succeeded(); 446*0b57cec5SDimitry Andric } 447*0b57cec5SDimitry Andric 448*0b57cec5SDimitry Andric CommandOptions m_options; 449*0b57cec5SDimitry Andric }; 450*0b57cec5SDimitry Andric 451*0b57cec5SDimitry Andric // CommandObjectProcessContinue 452*0b57cec5SDimitry Andric 4539dba64beSDimitry Andric #define LLDB_OPTIONS_process_continue 4549dba64beSDimitry Andric #include "CommandOptions.inc" 455*0b57cec5SDimitry Andric 456*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessContinue 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric class CommandObjectProcessContinue : public CommandObjectParsed { 459*0b57cec5SDimitry Andric public: 460*0b57cec5SDimitry Andric CommandObjectProcessContinue(CommandInterpreter &interpreter) 461*0b57cec5SDimitry Andric : CommandObjectParsed( 462*0b57cec5SDimitry Andric interpreter, "process continue", 463*0b57cec5SDimitry Andric "Continue execution of all threads in the current process.", 464*0b57cec5SDimitry Andric "process continue", 465*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 466*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 467*0b57cec5SDimitry Andric m_options() {} 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric ~CommandObjectProcessContinue() override = default; 470*0b57cec5SDimitry Andric 471*0b57cec5SDimitry Andric protected: 472*0b57cec5SDimitry Andric class CommandOptions : public Options { 473*0b57cec5SDimitry Andric public: 474*0b57cec5SDimitry Andric CommandOptions() : Options() { 475*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 476*0b57cec5SDimitry Andric // () 477*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 478*0b57cec5SDimitry Andric } 479*0b57cec5SDimitry Andric 480*0b57cec5SDimitry Andric ~CommandOptions() override = default; 481*0b57cec5SDimitry Andric 482*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 483*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 484*0b57cec5SDimitry Andric Status error; 485*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 486*0b57cec5SDimitry Andric switch (short_option) { 487*0b57cec5SDimitry Andric case 'i': 488*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_ignore)) 489*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 490*0b57cec5SDimitry Andric "invalid value for ignore option: \"%s\", should be a number.", 491*0b57cec5SDimitry Andric option_arg.str().c_str()); 492*0b57cec5SDimitry Andric break; 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric default: 4959dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 496*0b57cec5SDimitry Andric } 497*0b57cec5SDimitry Andric return error; 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 501*0b57cec5SDimitry Andric m_ignore = 0; 502*0b57cec5SDimitry Andric } 503*0b57cec5SDimitry Andric 504*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 505*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_continue_options); 506*0b57cec5SDimitry Andric } 507*0b57cec5SDimitry Andric 508*0b57cec5SDimitry Andric uint32_t m_ignore; 509*0b57cec5SDimitry Andric }; 510*0b57cec5SDimitry Andric 511*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 512*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 513*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 514*0b57cec5SDimitry Andric StateType state = process->GetState(); 515*0b57cec5SDimitry Andric if (state == eStateStopped) { 516*0b57cec5SDimitry Andric if (command.GetArgumentCount() != 0) { 517*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 518*0b57cec5SDimitry Andric "The '%s' command does not take any arguments.\n", 519*0b57cec5SDimitry Andric m_cmd_name.c_str()); 520*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 521*0b57cec5SDimitry Andric return false; 522*0b57cec5SDimitry Andric } 523*0b57cec5SDimitry Andric 524*0b57cec5SDimitry Andric if (m_options.m_ignore > 0) { 525*0b57cec5SDimitry Andric ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this()); 526*0b57cec5SDimitry Andric if (sel_thread_sp) { 527*0b57cec5SDimitry Andric StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo(); 528*0b57cec5SDimitry Andric if (stop_info_sp && 529*0b57cec5SDimitry Andric stop_info_sp->GetStopReason() == eStopReasonBreakpoint) { 530*0b57cec5SDimitry Andric lldb::break_id_t bp_site_id = 531*0b57cec5SDimitry Andric (lldb::break_id_t)stop_info_sp->GetValue(); 532*0b57cec5SDimitry Andric BreakpointSiteSP bp_site_sp( 533*0b57cec5SDimitry Andric process->GetBreakpointSiteList().FindByID(bp_site_id)); 534*0b57cec5SDimitry Andric if (bp_site_sp) { 535*0b57cec5SDimitry Andric const size_t num_owners = bp_site_sp->GetNumberOfOwners(); 536*0b57cec5SDimitry Andric for (size_t i = 0; i < num_owners; i++) { 537*0b57cec5SDimitry Andric Breakpoint &bp_ref = 538*0b57cec5SDimitry Andric bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 539*0b57cec5SDimitry Andric if (!bp_ref.IsInternal()) { 540*0b57cec5SDimitry Andric bp_ref.SetIgnoreCount(m_options.m_ignore); 541*0b57cec5SDimitry Andric } 542*0b57cec5SDimitry Andric } 543*0b57cec5SDimitry Andric } 544*0b57cec5SDimitry Andric } 545*0b57cec5SDimitry Andric } 546*0b57cec5SDimitry Andric } 547*0b57cec5SDimitry Andric 548*0b57cec5SDimitry Andric { // Scope for thread list mutex: 549*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 550*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 551*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 552*0b57cec5SDimitry Andric 553*0b57cec5SDimitry Andric // Set the actions that the threads should each take when resuming 554*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 555*0b57cec5SDimitry Andric const bool override_suspend = false; 556*0b57cec5SDimitry Andric process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState( 557*0b57cec5SDimitry Andric eStateRunning, override_suspend); 558*0b57cec5SDimitry Andric } 559*0b57cec5SDimitry Andric } 560*0b57cec5SDimitry Andric 561*0b57cec5SDimitry Andric const uint32_t iohandler_id = process->GetIOHandlerID(); 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric StreamString stream; 564*0b57cec5SDimitry Andric Status error; 565*0b57cec5SDimitry Andric if (synchronous_execution) 566*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 567*0b57cec5SDimitry Andric else 568*0b57cec5SDimitry Andric error = process->Resume(); 569*0b57cec5SDimitry Andric 570*0b57cec5SDimitry Andric if (error.Success()) { 571*0b57cec5SDimitry Andric // There is a race condition where this thread will return up the call 572*0b57cec5SDimitry Andric // stack to the main command handler and show an (lldb) prompt before 573*0b57cec5SDimitry Andric // HandlePrivateEvent (from PrivateStateThread) has a chance to call 574*0b57cec5SDimitry Andric // PushProcessIOHandler(). 575*0b57cec5SDimitry Andric process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); 576*0b57cec5SDimitry Andric 577*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 578*0b57cec5SDimitry Andric process->GetID()); 579*0b57cec5SDimitry Andric if (synchronous_execution) { 580*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 581*0b57cec5SDimitry Andric // result 582*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 585*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 586*0b57cec5SDimitry Andric } else { 587*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 588*0b57cec5SDimitry Andric } 589*0b57cec5SDimitry Andric } else { 590*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s.\n", 591*0b57cec5SDimitry Andric error.AsCString()); 592*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 593*0b57cec5SDimitry Andric } 594*0b57cec5SDimitry Andric } else { 595*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 596*0b57cec5SDimitry Andric "Process cannot be continued from its current state (%s).\n", 597*0b57cec5SDimitry Andric StateAsCString(state)); 598*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 599*0b57cec5SDimitry Andric } 600*0b57cec5SDimitry Andric return result.Succeeded(); 601*0b57cec5SDimitry Andric } 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 604*0b57cec5SDimitry Andric 605*0b57cec5SDimitry Andric CommandOptions m_options; 606*0b57cec5SDimitry Andric }; 607*0b57cec5SDimitry Andric 608*0b57cec5SDimitry Andric // CommandObjectProcessDetach 6099dba64beSDimitry Andric #define LLDB_OPTIONS_process_detach 6109dba64beSDimitry Andric #include "CommandOptions.inc" 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessDetach 613*0b57cec5SDimitry Andric 614*0b57cec5SDimitry Andric class CommandObjectProcessDetach : public CommandObjectParsed { 615*0b57cec5SDimitry Andric public: 616*0b57cec5SDimitry Andric class CommandOptions : public Options { 617*0b57cec5SDimitry Andric public: 618*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 619*0b57cec5SDimitry Andric 620*0b57cec5SDimitry Andric ~CommandOptions() override = default; 621*0b57cec5SDimitry Andric 622*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 623*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 624*0b57cec5SDimitry Andric Status error; 625*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric switch (short_option) { 628*0b57cec5SDimitry Andric case 's': 629*0b57cec5SDimitry Andric bool tmp_result; 630*0b57cec5SDimitry Andric bool success; 631*0b57cec5SDimitry Andric tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success); 632*0b57cec5SDimitry Andric if (!success) 633*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid boolean option: \"%s\"", 634*0b57cec5SDimitry Andric option_arg.str().c_str()); 635*0b57cec5SDimitry Andric else { 636*0b57cec5SDimitry Andric if (tmp_result) 637*0b57cec5SDimitry Andric m_keep_stopped = eLazyBoolYes; 638*0b57cec5SDimitry Andric else 639*0b57cec5SDimitry Andric m_keep_stopped = eLazyBoolNo; 640*0b57cec5SDimitry Andric } 641*0b57cec5SDimitry Andric break; 642*0b57cec5SDimitry Andric default: 6439dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 644*0b57cec5SDimitry Andric } 645*0b57cec5SDimitry Andric return error; 646*0b57cec5SDimitry Andric } 647*0b57cec5SDimitry Andric 648*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 649*0b57cec5SDimitry Andric m_keep_stopped = eLazyBoolCalculate; 650*0b57cec5SDimitry Andric } 651*0b57cec5SDimitry Andric 652*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 653*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_detach_options); 654*0b57cec5SDimitry Andric } 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 657*0b57cec5SDimitry Andric LazyBool m_keep_stopped; 658*0b57cec5SDimitry Andric }; 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric CommandObjectProcessDetach(CommandInterpreter &interpreter) 661*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process detach", 662*0b57cec5SDimitry Andric "Detach from the current target process.", 663*0b57cec5SDimitry Andric "process detach", 664*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 665*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched), 666*0b57cec5SDimitry Andric m_options() {} 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric ~CommandObjectProcessDetach() override = default; 669*0b57cec5SDimitry Andric 670*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 671*0b57cec5SDimitry Andric 672*0b57cec5SDimitry Andric protected: 673*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 674*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 675*0b57cec5SDimitry Andric // FIXME: This will be a Command Option: 676*0b57cec5SDimitry Andric bool keep_stopped; 677*0b57cec5SDimitry Andric if (m_options.m_keep_stopped == eLazyBoolCalculate) { 678*0b57cec5SDimitry Andric // Check the process default: 679*0b57cec5SDimitry Andric keep_stopped = process->GetDetachKeepsStopped(); 680*0b57cec5SDimitry Andric } else if (m_options.m_keep_stopped == eLazyBoolYes) 681*0b57cec5SDimitry Andric keep_stopped = true; 682*0b57cec5SDimitry Andric else 683*0b57cec5SDimitry Andric keep_stopped = false; 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric Status error(process->Detach(keep_stopped)); 686*0b57cec5SDimitry Andric if (error.Success()) { 687*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 688*0b57cec5SDimitry Andric } else { 689*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString()); 690*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 691*0b57cec5SDimitry Andric return false; 692*0b57cec5SDimitry Andric } 693*0b57cec5SDimitry Andric return result.Succeeded(); 694*0b57cec5SDimitry Andric } 695*0b57cec5SDimitry Andric 696*0b57cec5SDimitry Andric CommandOptions m_options; 697*0b57cec5SDimitry Andric }; 698*0b57cec5SDimitry Andric 699*0b57cec5SDimitry Andric // CommandObjectProcessConnect 7009dba64beSDimitry Andric #define LLDB_OPTIONS_process_connect 7019dba64beSDimitry Andric #include "CommandOptions.inc" 702*0b57cec5SDimitry Andric 703*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessConnect 704*0b57cec5SDimitry Andric 705*0b57cec5SDimitry Andric class CommandObjectProcessConnect : public CommandObjectParsed { 706*0b57cec5SDimitry Andric public: 707*0b57cec5SDimitry Andric class CommandOptions : public Options { 708*0b57cec5SDimitry Andric public: 709*0b57cec5SDimitry Andric CommandOptions() : Options() { 710*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 711*0b57cec5SDimitry Andric // () 712*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 713*0b57cec5SDimitry Andric } 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric ~CommandOptions() override = default; 716*0b57cec5SDimitry Andric 717*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 718*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 719*0b57cec5SDimitry Andric Status error; 720*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 721*0b57cec5SDimitry Andric 722*0b57cec5SDimitry Andric switch (short_option) { 723*0b57cec5SDimitry Andric case 'p': 7245ffd83dbSDimitry Andric plugin_name.assign(std::string(option_arg)); 725*0b57cec5SDimitry Andric break; 726*0b57cec5SDimitry Andric 727*0b57cec5SDimitry Andric default: 7289dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 729*0b57cec5SDimitry Andric } 730*0b57cec5SDimitry Andric return error; 731*0b57cec5SDimitry Andric } 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 734*0b57cec5SDimitry Andric plugin_name.clear(); 735*0b57cec5SDimitry Andric } 736*0b57cec5SDimitry Andric 737*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 738*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_connect_options); 739*0b57cec5SDimitry Andric } 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric std::string plugin_name; 744*0b57cec5SDimitry Andric }; 745*0b57cec5SDimitry Andric 746*0b57cec5SDimitry Andric CommandObjectProcessConnect(CommandInterpreter &interpreter) 747*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process connect", 748*0b57cec5SDimitry Andric "Connect to a remote debug service.", 749*0b57cec5SDimitry Andric "process connect <remote-url>", 0), 750*0b57cec5SDimitry Andric m_options() {} 751*0b57cec5SDimitry Andric 752*0b57cec5SDimitry Andric ~CommandObjectProcessConnect() override = default; 753*0b57cec5SDimitry Andric 754*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 755*0b57cec5SDimitry Andric 756*0b57cec5SDimitry Andric protected: 757*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 758*0b57cec5SDimitry Andric if (command.GetArgumentCount() != 1) { 759*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 760*0b57cec5SDimitry Andric "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(), 761*0b57cec5SDimitry Andric m_cmd_syntax.c_str()); 762*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 763*0b57cec5SDimitry Andric return false; 764*0b57cec5SDimitry Andric } 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 767*0b57cec5SDimitry Andric if (process && process->IsAlive()) { 768*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 769*0b57cec5SDimitry Andric "Process %" PRIu64 770*0b57cec5SDimitry Andric " is currently being debugged, kill the process before connecting.\n", 771*0b57cec5SDimitry Andric process->GetID()); 772*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 773*0b57cec5SDimitry Andric return false; 774*0b57cec5SDimitry Andric } 775*0b57cec5SDimitry Andric 776*0b57cec5SDimitry Andric const char *plugin_name = nullptr; 777*0b57cec5SDimitry Andric if (!m_options.plugin_name.empty()) 778*0b57cec5SDimitry Andric plugin_name = m_options.plugin_name.c_str(); 779*0b57cec5SDimitry Andric 780*0b57cec5SDimitry Andric Status error; 781*0b57cec5SDimitry Andric Debugger &debugger = GetDebugger(); 782*0b57cec5SDimitry Andric PlatformSP platform_sp = m_interpreter.GetPlatform(true); 7835ffd83dbSDimitry Andric ProcessSP process_sp = 7845ffd83dbSDimitry Andric debugger.GetAsyncExecution() 7855ffd83dbSDimitry Andric ? platform_sp->ConnectProcess( 786*0b57cec5SDimitry Andric command.GetArgumentAtIndex(0), plugin_name, debugger, 7875ffd83dbSDimitry Andric debugger.GetSelectedTarget().get(), error) 7885ffd83dbSDimitry Andric : platform_sp->ConnectProcessSynchronous( 7895ffd83dbSDimitry Andric command.GetArgumentAtIndex(0), plugin_name, debugger, 7905ffd83dbSDimitry Andric result.GetOutputStream(), debugger.GetSelectedTarget().get(), 7915ffd83dbSDimitry Andric error); 792*0b57cec5SDimitry Andric if (error.Fail() || process_sp == nullptr) { 793*0b57cec5SDimitry Andric result.AppendError(error.AsCString("Error connecting to the process")); 794*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 795*0b57cec5SDimitry Andric return false; 796*0b57cec5SDimitry Andric } 797*0b57cec5SDimitry Andric return true; 798*0b57cec5SDimitry Andric } 799*0b57cec5SDimitry Andric 800*0b57cec5SDimitry Andric CommandOptions m_options; 801*0b57cec5SDimitry Andric }; 802*0b57cec5SDimitry Andric 803*0b57cec5SDimitry Andric // CommandObjectProcessPlugin 804*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessPlugin 805*0b57cec5SDimitry Andric 806*0b57cec5SDimitry Andric class CommandObjectProcessPlugin : public CommandObjectProxy { 807*0b57cec5SDimitry Andric public: 808*0b57cec5SDimitry Andric CommandObjectProcessPlugin(CommandInterpreter &interpreter) 809*0b57cec5SDimitry Andric : CommandObjectProxy( 810*0b57cec5SDimitry Andric interpreter, "process plugin", 811*0b57cec5SDimitry Andric "Send a custom command to the current target process plug-in.", 812*0b57cec5SDimitry Andric "process plugin <args>", 0) {} 813*0b57cec5SDimitry Andric 814*0b57cec5SDimitry Andric ~CommandObjectProcessPlugin() override = default; 815*0b57cec5SDimitry Andric 816*0b57cec5SDimitry Andric CommandObject *GetProxyCommandObject() override { 817*0b57cec5SDimitry Andric Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); 818*0b57cec5SDimitry Andric if (process) 819*0b57cec5SDimitry Andric return process->GetPluginCommandObject(); 820*0b57cec5SDimitry Andric return nullptr; 821*0b57cec5SDimitry Andric } 822*0b57cec5SDimitry Andric }; 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric // CommandObjectProcessLoad 8259dba64beSDimitry Andric #define LLDB_OPTIONS_process_load 8269dba64beSDimitry Andric #include "CommandOptions.inc" 827*0b57cec5SDimitry Andric 828*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessLoad 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric class CommandObjectProcessLoad : public CommandObjectParsed { 831*0b57cec5SDimitry Andric public: 832*0b57cec5SDimitry Andric class CommandOptions : public Options { 833*0b57cec5SDimitry Andric public: 834*0b57cec5SDimitry Andric CommandOptions() : Options() { 835*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 836*0b57cec5SDimitry Andric // () 837*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 838*0b57cec5SDimitry Andric } 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric ~CommandOptions() override = default; 841*0b57cec5SDimitry Andric 842*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 843*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 844*0b57cec5SDimitry Andric Status error; 845*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 846*0b57cec5SDimitry Andric switch (short_option) { 847*0b57cec5SDimitry Andric case 'i': 848*0b57cec5SDimitry Andric do_install = true; 849*0b57cec5SDimitry Andric if (!option_arg.empty()) 850*0b57cec5SDimitry Andric install_path.SetFile(option_arg, FileSpec::Style::native); 851*0b57cec5SDimitry Andric break; 852*0b57cec5SDimitry Andric default: 8539dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 854*0b57cec5SDimitry Andric } 855*0b57cec5SDimitry Andric return error; 856*0b57cec5SDimitry Andric } 857*0b57cec5SDimitry Andric 858*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 859*0b57cec5SDimitry Andric do_install = false; 860*0b57cec5SDimitry Andric install_path.Clear(); 861*0b57cec5SDimitry Andric } 862*0b57cec5SDimitry Andric 863*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 864*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_load_options); 865*0b57cec5SDimitry Andric } 866*0b57cec5SDimitry Andric 867*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 868*0b57cec5SDimitry Andric bool do_install; 869*0b57cec5SDimitry Andric FileSpec install_path; 870*0b57cec5SDimitry Andric }; 871*0b57cec5SDimitry Andric 872*0b57cec5SDimitry Andric CommandObjectProcessLoad(CommandInterpreter &interpreter) 873*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process load", 874*0b57cec5SDimitry Andric "Load a shared library into the current process.", 875*0b57cec5SDimitry Andric "process load <filename> [<filename> ...]", 876*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 877*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 878*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 879*0b57cec5SDimitry Andric m_options() {} 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric ~CommandObjectProcessLoad() override = default; 882*0b57cec5SDimitry Andric 883e8d8bef9SDimitry Andric void 884e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 885e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 886e8d8bef9SDimitry Andric if (!m_exe_ctx.HasProcessScope()) 887e8d8bef9SDimitry Andric return; 888e8d8bef9SDimitry Andric 889e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 890e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 891e8d8bef9SDimitry Andric request, nullptr); 892e8d8bef9SDimitry Andric } 893e8d8bef9SDimitry Andric 894*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 895*0b57cec5SDimitry Andric 896*0b57cec5SDimitry Andric protected: 897*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 898*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 899*0b57cec5SDimitry Andric 900*0b57cec5SDimitry Andric for (auto &entry : command.entries()) { 901*0b57cec5SDimitry Andric Status error; 902*0b57cec5SDimitry Andric PlatformSP platform = process->GetTarget().GetPlatform(); 9039dba64beSDimitry Andric llvm::StringRef image_path = entry.ref(); 904*0b57cec5SDimitry Andric uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN; 905*0b57cec5SDimitry Andric 906*0b57cec5SDimitry Andric if (!m_options.do_install) { 907*0b57cec5SDimitry Andric FileSpec image_spec(image_path); 908*0b57cec5SDimitry Andric platform->ResolveRemotePath(image_spec, image_spec); 909*0b57cec5SDimitry Andric image_token = 910*0b57cec5SDimitry Andric platform->LoadImage(process, FileSpec(), image_spec, error); 911*0b57cec5SDimitry Andric } else if (m_options.install_path) { 912*0b57cec5SDimitry Andric FileSpec image_spec(image_path); 913*0b57cec5SDimitry Andric FileSystem::Instance().Resolve(image_spec); 914*0b57cec5SDimitry Andric platform->ResolveRemotePath(m_options.install_path, 915*0b57cec5SDimitry Andric m_options.install_path); 916*0b57cec5SDimitry Andric image_token = platform->LoadImage(process, image_spec, 917*0b57cec5SDimitry Andric m_options.install_path, error); 918*0b57cec5SDimitry Andric } else { 919*0b57cec5SDimitry Andric FileSpec image_spec(image_path); 920*0b57cec5SDimitry Andric FileSystem::Instance().Resolve(image_spec); 921*0b57cec5SDimitry Andric image_token = 922*0b57cec5SDimitry Andric platform->LoadImage(process, image_spec, FileSpec(), error); 923*0b57cec5SDimitry Andric } 924*0b57cec5SDimitry Andric 925*0b57cec5SDimitry Andric if (image_token != LLDB_INVALID_IMAGE_TOKEN) { 926*0b57cec5SDimitry Andric result.AppendMessageWithFormat( 927*0b57cec5SDimitry Andric "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(), 928*0b57cec5SDimitry Andric image_token); 929*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 930*0b57cec5SDimitry Andric } else { 931*0b57cec5SDimitry Andric result.AppendErrorWithFormat("failed to load '%s': %s", 932*0b57cec5SDimitry Andric image_path.str().c_str(), 933*0b57cec5SDimitry Andric error.AsCString()); 934*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 935*0b57cec5SDimitry Andric } 936*0b57cec5SDimitry Andric } 937*0b57cec5SDimitry Andric return result.Succeeded(); 938*0b57cec5SDimitry Andric } 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric CommandOptions m_options; 941*0b57cec5SDimitry Andric }; 942*0b57cec5SDimitry Andric 943*0b57cec5SDimitry Andric // CommandObjectProcessUnload 944*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessUnload 945*0b57cec5SDimitry Andric 946*0b57cec5SDimitry Andric class CommandObjectProcessUnload : public CommandObjectParsed { 947*0b57cec5SDimitry Andric public: 948*0b57cec5SDimitry Andric CommandObjectProcessUnload(CommandInterpreter &interpreter) 949*0b57cec5SDimitry Andric : CommandObjectParsed( 950*0b57cec5SDimitry Andric interpreter, "process unload", 951*0b57cec5SDimitry Andric "Unload a shared library from the current process using the index " 952*0b57cec5SDimitry Andric "returned by a previous call to \"process load\".", 953*0b57cec5SDimitry Andric "process unload <index>", 954*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 955*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric ~CommandObjectProcessUnload() override = default; 958*0b57cec5SDimitry Andric 959e8d8bef9SDimitry Andric void 960e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 961e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 962e8d8bef9SDimitry Andric 963e8d8bef9SDimitry Andric if (request.GetCursorIndex() || !m_exe_ctx.HasProcessScope()) 964e8d8bef9SDimitry Andric return; 965e8d8bef9SDimitry Andric 966e8d8bef9SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 967e8d8bef9SDimitry Andric 968e8d8bef9SDimitry Andric const std::vector<lldb::addr_t> &tokens = process->GetImageTokens(); 969e8d8bef9SDimitry Andric const size_t token_num = tokens.size(); 970e8d8bef9SDimitry Andric for (size_t i = 0; i < token_num; ++i) { 971e8d8bef9SDimitry Andric if (tokens[i] == LLDB_INVALID_IMAGE_TOKEN) 972e8d8bef9SDimitry Andric continue; 973e8d8bef9SDimitry Andric request.TryCompleteCurrentArg(std::to_string(i)); 974e8d8bef9SDimitry Andric } 975e8d8bef9SDimitry Andric } 976e8d8bef9SDimitry Andric 977*0b57cec5SDimitry Andric protected: 978*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 979*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 980*0b57cec5SDimitry Andric 981*0b57cec5SDimitry Andric for (auto &entry : command.entries()) { 982*0b57cec5SDimitry Andric uint32_t image_token; 9839dba64beSDimitry Andric if (entry.ref().getAsInteger(0, image_token)) { 984*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid image index argument '%s'", 9859dba64beSDimitry Andric entry.ref().str().c_str()); 986*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 987*0b57cec5SDimitry Andric break; 988*0b57cec5SDimitry Andric } else { 989*0b57cec5SDimitry Andric Status error(process->GetTarget().GetPlatform()->UnloadImage( 990*0b57cec5SDimitry Andric process, image_token)); 991*0b57cec5SDimitry Andric if (error.Success()) { 992*0b57cec5SDimitry Andric result.AppendMessageWithFormat( 993*0b57cec5SDimitry Andric "Unloading shared library with index %u...ok\n", image_token); 994*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 995*0b57cec5SDimitry Andric } else { 996*0b57cec5SDimitry Andric result.AppendErrorWithFormat("failed to unload image: %s", 997*0b57cec5SDimitry Andric error.AsCString()); 998*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 999*0b57cec5SDimitry Andric break; 1000*0b57cec5SDimitry Andric } 1001*0b57cec5SDimitry Andric } 1002*0b57cec5SDimitry Andric } 1003*0b57cec5SDimitry Andric return result.Succeeded(); 1004*0b57cec5SDimitry Andric } 1005*0b57cec5SDimitry Andric }; 1006*0b57cec5SDimitry Andric 1007*0b57cec5SDimitry Andric // CommandObjectProcessSignal 1008*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessSignal 1009*0b57cec5SDimitry Andric 1010*0b57cec5SDimitry Andric class CommandObjectProcessSignal : public CommandObjectParsed { 1011*0b57cec5SDimitry Andric public: 1012*0b57cec5SDimitry Andric CommandObjectProcessSignal(CommandInterpreter &interpreter) 1013480093f4SDimitry Andric : CommandObjectParsed( 1014480093f4SDimitry Andric interpreter, "process signal", 1015480093f4SDimitry Andric "Send a UNIX signal to the current target process.", nullptr, 1016480093f4SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock) { 1017*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1018*0b57cec5SDimitry Andric CommandArgumentData signal_arg; 1019*0b57cec5SDimitry Andric 1020*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1021*0b57cec5SDimitry Andric signal_arg.arg_type = eArgTypeUnixSignal; 1022*0b57cec5SDimitry Andric signal_arg.arg_repetition = eArgRepeatPlain; 1023*0b57cec5SDimitry Andric 1024*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1025*0b57cec5SDimitry Andric // argument entry. 1026*0b57cec5SDimitry Andric arg.push_back(signal_arg); 1027*0b57cec5SDimitry Andric 1028*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1029*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1030*0b57cec5SDimitry Andric } 1031*0b57cec5SDimitry Andric 1032*0b57cec5SDimitry Andric ~CommandObjectProcessSignal() override = default; 1033*0b57cec5SDimitry Andric 10345ffd83dbSDimitry Andric void 10355ffd83dbSDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 10365ffd83dbSDimitry Andric OptionElementVector &opt_element_vector) override { 10375ffd83dbSDimitry Andric if (!m_exe_ctx.HasProcessScope() || request.GetCursorIndex() != 0) 10385ffd83dbSDimitry Andric return; 10395ffd83dbSDimitry Andric 10405ffd83dbSDimitry Andric UnixSignalsSP signals = m_exe_ctx.GetProcessPtr()->GetUnixSignals(); 10415ffd83dbSDimitry Andric int signo = signals->GetFirstSignalNumber(); 10425ffd83dbSDimitry Andric while (signo != LLDB_INVALID_SIGNAL_NUMBER) { 10435ffd83dbSDimitry Andric request.AddCompletion(signals->GetSignalAsCString(signo), ""); 10445ffd83dbSDimitry Andric signo = signals->GetNextSignalNumber(signo); 10455ffd83dbSDimitry Andric } 10465ffd83dbSDimitry Andric } 10475ffd83dbSDimitry Andric 1048*0b57cec5SDimitry Andric protected: 1049*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1050*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1051*0b57cec5SDimitry Andric 1052*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 1) { 1053*0b57cec5SDimitry Andric int signo = LLDB_INVALID_SIGNAL_NUMBER; 1054*0b57cec5SDimitry Andric 1055*0b57cec5SDimitry Andric const char *signal_name = command.GetArgumentAtIndex(0); 10565ffd83dbSDimitry Andric if (::isxdigit(signal_name[0])) { 10575ffd83dbSDimitry Andric if (!llvm::to_integer(signal_name, signo)) 10585ffd83dbSDimitry Andric signo = LLDB_INVALID_SIGNAL_NUMBER; 10595ffd83dbSDimitry Andric } else 1060*0b57cec5SDimitry Andric signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name); 1061*0b57cec5SDimitry Andric 1062*0b57cec5SDimitry Andric if (signo == LLDB_INVALID_SIGNAL_NUMBER) { 1063*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid signal argument '%s'.\n", 1064*0b57cec5SDimitry Andric command.GetArgumentAtIndex(0)); 1065*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1066*0b57cec5SDimitry Andric } else { 1067*0b57cec5SDimitry Andric Status error(process->Signal(signo)); 1068*0b57cec5SDimitry Andric if (error.Success()) { 1069*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1070*0b57cec5SDimitry Andric } else { 1071*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo, 1072*0b57cec5SDimitry Andric error.AsCString()); 1073*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1074*0b57cec5SDimitry Andric } 1075*0b57cec5SDimitry Andric } 1076*0b57cec5SDimitry Andric } else { 1077*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1078*0b57cec5SDimitry Andric "'%s' takes exactly one signal number argument:\nUsage: %s\n", 1079*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1080*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1081*0b57cec5SDimitry Andric } 1082*0b57cec5SDimitry Andric return result.Succeeded(); 1083*0b57cec5SDimitry Andric } 1084*0b57cec5SDimitry Andric }; 1085*0b57cec5SDimitry Andric 1086*0b57cec5SDimitry Andric // CommandObjectProcessInterrupt 1087*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessInterrupt 1088*0b57cec5SDimitry Andric 1089*0b57cec5SDimitry Andric class CommandObjectProcessInterrupt : public CommandObjectParsed { 1090*0b57cec5SDimitry Andric public: 1091*0b57cec5SDimitry Andric CommandObjectProcessInterrupt(CommandInterpreter &interpreter) 1092*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process interrupt", 1093*0b57cec5SDimitry Andric "Interrupt the current target process.", 1094*0b57cec5SDimitry Andric "process interrupt", 1095*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1096*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched) {} 1097*0b57cec5SDimitry Andric 1098*0b57cec5SDimitry Andric ~CommandObjectProcessInterrupt() override = default; 1099*0b57cec5SDimitry Andric 1100*0b57cec5SDimitry Andric protected: 1101*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1102*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1103*0b57cec5SDimitry Andric if (process == nullptr) { 1104*0b57cec5SDimitry Andric result.AppendError("no process to halt"); 1105*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1106*0b57cec5SDimitry Andric return false; 1107*0b57cec5SDimitry Andric } 1108*0b57cec5SDimitry Andric 1109*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 1110*0b57cec5SDimitry Andric bool clear_thread_plans = true; 1111*0b57cec5SDimitry Andric Status error(process->Halt(clear_thread_plans)); 1112*0b57cec5SDimitry Andric if (error.Success()) { 1113*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1114*0b57cec5SDimitry Andric } else { 1115*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to halt process: %s\n", 1116*0b57cec5SDimitry Andric error.AsCString()); 1117*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1118*0b57cec5SDimitry Andric } 1119*0b57cec5SDimitry Andric } else { 1120*0b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", 1121*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1122*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1123*0b57cec5SDimitry Andric } 1124*0b57cec5SDimitry Andric return result.Succeeded(); 1125*0b57cec5SDimitry Andric } 1126*0b57cec5SDimitry Andric }; 1127*0b57cec5SDimitry Andric 1128*0b57cec5SDimitry Andric // CommandObjectProcessKill 1129*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessKill 1130*0b57cec5SDimitry Andric 1131*0b57cec5SDimitry Andric class CommandObjectProcessKill : public CommandObjectParsed { 1132*0b57cec5SDimitry Andric public: 1133*0b57cec5SDimitry Andric CommandObjectProcessKill(CommandInterpreter &interpreter) 1134*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process kill", 1135*0b57cec5SDimitry Andric "Terminate the current target process.", 1136*0b57cec5SDimitry Andric "process kill", 1137*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1138*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched) {} 1139*0b57cec5SDimitry Andric 1140*0b57cec5SDimitry Andric ~CommandObjectProcessKill() override = default; 1141*0b57cec5SDimitry Andric 1142*0b57cec5SDimitry Andric protected: 1143*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1144*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1145*0b57cec5SDimitry Andric if (process == nullptr) { 1146*0b57cec5SDimitry Andric result.AppendError("no process to kill"); 1147*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1148*0b57cec5SDimitry Andric return false; 1149*0b57cec5SDimitry Andric } 1150*0b57cec5SDimitry Andric 1151*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 1152*0b57cec5SDimitry Andric Status error(process->Destroy(true)); 1153*0b57cec5SDimitry Andric if (error.Success()) { 1154*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1155*0b57cec5SDimitry Andric } else { 1156*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to kill process: %s\n", 1157*0b57cec5SDimitry Andric error.AsCString()); 1158*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1159*0b57cec5SDimitry Andric } 1160*0b57cec5SDimitry Andric } else { 1161*0b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n", 1162*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1163*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1164*0b57cec5SDimitry Andric } 1165*0b57cec5SDimitry Andric return result.Succeeded(); 1166*0b57cec5SDimitry Andric } 1167*0b57cec5SDimitry Andric }; 1168*0b57cec5SDimitry Andric 1169*0b57cec5SDimitry Andric // CommandObjectProcessSaveCore 1170*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessSaveCore 1171*0b57cec5SDimitry Andric 1172*0b57cec5SDimitry Andric class CommandObjectProcessSaveCore : public CommandObjectParsed { 1173*0b57cec5SDimitry Andric public: 1174*0b57cec5SDimitry Andric CommandObjectProcessSaveCore(CommandInterpreter &interpreter) 1175*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process save-core", 1176*0b57cec5SDimitry Andric "Save the current process as a core file using an " 1177*0b57cec5SDimitry Andric "appropriate file type.", 1178*0b57cec5SDimitry Andric "process save-core FILE", 1179*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1180*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched) {} 1181*0b57cec5SDimitry Andric 1182*0b57cec5SDimitry Andric ~CommandObjectProcessSaveCore() override = default; 1183*0b57cec5SDimitry Andric 1184*0b57cec5SDimitry Andric protected: 1185*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1186*0b57cec5SDimitry Andric ProcessSP process_sp = m_exe_ctx.GetProcessSP(); 1187*0b57cec5SDimitry Andric if (process_sp) { 1188*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 1) { 1189*0b57cec5SDimitry Andric FileSpec output_file(command.GetArgumentAtIndex(0)); 1190*0b57cec5SDimitry Andric Status error = PluginManager::SaveCore(process_sp, output_file); 1191*0b57cec5SDimitry Andric if (error.Success()) { 1192*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1193*0b57cec5SDimitry Andric } else { 1194*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1195*0b57cec5SDimitry Andric "Failed to save core file for process: %s\n", error.AsCString()); 1196*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1197*0b57cec5SDimitry Andric } 1198*0b57cec5SDimitry Andric } else { 1199*0b57cec5SDimitry Andric result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n", 1200*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1201*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1202*0b57cec5SDimitry Andric } 1203*0b57cec5SDimitry Andric } else { 1204*0b57cec5SDimitry Andric result.AppendError("invalid process"); 1205*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1206*0b57cec5SDimitry Andric return false; 1207*0b57cec5SDimitry Andric } 1208*0b57cec5SDimitry Andric 1209*0b57cec5SDimitry Andric return result.Succeeded(); 1210*0b57cec5SDimitry Andric } 1211*0b57cec5SDimitry Andric }; 1212*0b57cec5SDimitry Andric 1213*0b57cec5SDimitry Andric // CommandObjectProcessStatus 1214*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessStatus 12155ffd83dbSDimitry Andric #define LLDB_OPTIONS_process_status 12165ffd83dbSDimitry Andric #include "CommandOptions.inc" 1217*0b57cec5SDimitry Andric 1218*0b57cec5SDimitry Andric class CommandObjectProcessStatus : public CommandObjectParsed { 1219*0b57cec5SDimitry Andric public: 1220*0b57cec5SDimitry Andric CommandObjectProcessStatus(CommandInterpreter &interpreter) 1221*0b57cec5SDimitry Andric : CommandObjectParsed( 1222*0b57cec5SDimitry Andric interpreter, "process status", 1223*0b57cec5SDimitry Andric "Show status and stop location for the current target process.", 1224*0b57cec5SDimitry Andric "process status", 12255ffd83dbSDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock), 12265ffd83dbSDimitry Andric m_options() {} 1227*0b57cec5SDimitry Andric 1228*0b57cec5SDimitry Andric ~CommandObjectProcessStatus() override = default; 1229*0b57cec5SDimitry Andric 12305ffd83dbSDimitry Andric Options *GetOptions() override { return &m_options; } 12315ffd83dbSDimitry Andric 12325ffd83dbSDimitry Andric class CommandOptions : public Options { 12335ffd83dbSDimitry Andric public: 12345ffd83dbSDimitry Andric CommandOptions() : Options(), m_verbose(false) {} 12355ffd83dbSDimitry Andric 12365ffd83dbSDimitry Andric ~CommandOptions() override = default; 12375ffd83dbSDimitry Andric 12385ffd83dbSDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 12395ffd83dbSDimitry Andric ExecutionContext *execution_context) override { 12405ffd83dbSDimitry Andric const int short_option = m_getopt_table[option_idx].val; 12415ffd83dbSDimitry Andric 12425ffd83dbSDimitry Andric switch (short_option) { 12435ffd83dbSDimitry Andric case 'v': 12445ffd83dbSDimitry Andric m_verbose = true; 12455ffd83dbSDimitry Andric break; 12465ffd83dbSDimitry Andric default: 12475ffd83dbSDimitry Andric llvm_unreachable("Unimplemented option"); 12485ffd83dbSDimitry Andric } 12495ffd83dbSDimitry Andric 12505ffd83dbSDimitry Andric return {}; 12515ffd83dbSDimitry Andric } 12525ffd83dbSDimitry Andric 12535ffd83dbSDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 12545ffd83dbSDimitry Andric m_verbose = false; 12555ffd83dbSDimitry Andric } 12565ffd83dbSDimitry Andric 12575ffd83dbSDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 12585ffd83dbSDimitry Andric return llvm::makeArrayRef(g_process_status_options); 12595ffd83dbSDimitry Andric } 12605ffd83dbSDimitry Andric 12615ffd83dbSDimitry Andric // Instance variables to hold the values for command options. 12625ffd83dbSDimitry Andric bool m_verbose; 12635ffd83dbSDimitry Andric }; 12645ffd83dbSDimitry Andric 12655ffd83dbSDimitry Andric protected: 1266*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1267*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1268*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 12695ffd83dbSDimitry Andric 12705ffd83dbSDimitry Andric if (command.GetArgumentCount()) { 12715ffd83dbSDimitry Andric result.AppendError("'process status' takes no arguments"); 12725ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 12735ffd83dbSDimitry Andric return result.Succeeded(); 12745ffd83dbSDimitry Andric } 12755ffd83dbSDimitry Andric 1276*0b57cec5SDimitry Andric // No need to check "process" for validity as eCommandRequiresProcess 1277*0b57cec5SDimitry Andric // ensures it is valid 1278*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1279*0b57cec5SDimitry Andric const bool only_threads_with_stop_reason = true; 1280*0b57cec5SDimitry Andric const uint32_t start_frame = 0; 1281*0b57cec5SDimitry Andric const uint32_t num_frames = 1; 1282*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 1; 1283*0b57cec5SDimitry Andric const bool stop_format = true; 1284*0b57cec5SDimitry Andric process->GetStatus(strm); 1285*0b57cec5SDimitry Andric process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, 1286*0b57cec5SDimitry Andric num_frames, num_frames_with_source, stop_format); 12875ffd83dbSDimitry Andric 12885ffd83dbSDimitry Andric if (m_options.m_verbose) { 12895ffd83dbSDimitry Andric PlatformSP platform_sp = process->GetTarget().GetPlatform(); 12905ffd83dbSDimitry Andric if (!platform_sp) { 12915ffd83dbSDimitry Andric result.AppendError("Couldn'retrieve the target's platform"); 12925ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 1293*0b57cec5SDimitry Andric return result.Succeeded(); 1294*0b57cec5SDimitry Andric } 12955ffd83dbSDimitry Andric 12965ffd83dbSDimitry Andric auto expected_crash_info = 12975ffd83dbSDimitry Andric platform_sp->FetchExtendedCrashInformation(*process); 12985ffd83dbSDimitry Andric 12995ffd83dbSDimitry Andric if (!expected_crash_info) { 13005ffd83dbSDimitry Andric result.AppendError(llvm::toString(expected_crash_info.takeError())); 13015ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 13025ffd83dbSDimitry Andric return result.Succeeded(); 13035ffd83dbSDimitry Andric } 13045ffd83dbSDimitry Andric 13055ffd83dbSDimitry Andric StructuredData::DictionarySP crash_info_sp = *expected_crash_info; 13065ffd83dbSDimitry Andric 13075ffd83dbSDimitry Andric if (crash_info_sp) { 13085ffd83dbSDimitry Andric strm.PutCString("Extended Crash Information:\n"); 13095ffd83dbSDimitry Andric crash_info_sp->Dump(strm); 13105ffd83dbSDimitry Andric } 13115ffd83dbSDimitry Andric } 13125ffd83dbSDimitry Andric 13135ffd83dbSDimitry Andric return result.Succeeded(); 13145ffd83dbSDimitry Andric } 13155ffd83dbSDimitry Andric 13165ffd83dbSDimitry Andric private: 13175ffd83dbSDimitry Andric CommandOptions m_options; 1318*0b57cec5SDimitry Andric }; 1319*0b57cec5SDimitry Andric 1320*0b57cec5SDimitry Andric // CommandObjectProcessHandle 13219dba64beSDimitry Andric #define LLDB_OPTIONS_process_handle 13229dba64beSDimitry Andric #include "CommandOptions.inc" 1323*0b57cec5SDimitry Andric 1324*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessHandle 1325*0b57cec5SDimitry Andric 1326*0b57cec5SDimitry Andric class CommandObjectProcessHandle : public CommandObjectParsed { 1327*0b57cec5SDimitry Andric public: 1328*0b57cec5SDimitry Andric class CommandOptions : public Options { 1329*0b57cec5SDimitry Andric public: 1330*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1331*0b57cec5SDimitry Andric 1332*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1333*0b57cec5SDimitry Andric 1334*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1335*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1336*0b57cec5SDimitry Andric Status error; 1337*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1338*0b57cec5SDimitry Andric 1339*0b57cec5SDimitry Andric switch (short_option) { 1340*0b57cec5SDimitry Andric case 's': 13415ffd83dbSDimitry Andric stop = std::string(option_arg); 1342*0b57cec5SDimitry Andric break; 1343*0b57cec5SDimitry Andric case 'n': 13445ffd83dbSDimitry Andric notify = std::string(option_arg); 1345*0b57cec5SDimitry Andric break; 1346*0b57cec5SDimitry Andric case 'p': 13475ffd83dbSDimitry Andric pass = std::string(option_arg); 1348*0b57cec5SDimitry Andric break; 1349*0b57cec5SDimitry Andric default: 13509dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1351*0b57cec5SDimitry Andric } 1352*0b57cec5SDimitry Andric return error; 1353*0b57cec5SDimitry Andric } 1354*0b57cec5SDimitry Andric 1355*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1356*0b57cec5SDimitry Andric stop.clear(); 1357*0b57cec5SDimitry Andric notify.clear(); 1358*0b57cec5SDimitry Andric pass.clear(); 1359*0b57cec5SDimitry Andric } 1360*0b57cec5SDimitry Andric 1361*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1362*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_process_handle_options); 1363*0b57cec5SDimitry Andric } 1364*0b57cec5SDimitry Andric 1365*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1366*0b57cec5SDimitry Andric 1367*0b57cec5SDimitry Andric std::string stop; 1368*0b57cec5SDimitry Andric std::string notify; 1369*0b57cec5SDimitry Andric std::string pass; 1370*0b57cec5SDimitry Andric }; 1371*0b57cec5SDimitry Andric 1372*0b57cec5SDimitry Andric CommandObjectProcessHandle(CommandInterpreter &interpreter) 1373*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "process handle", 1374*0b57cec5SDimitry Andric "Manage LLDB handling of OS signals for the " 1375*0b57cec5SDimitry Andric "current target process. Defaults to showing " 1376*0b57cec5SDimitry Andric "current policy.", 13779dba64beSDimitry Andric nullptr, eCommandRequiresTarget), 1378*0b57cec5SDimitry Andric m_options() { 1379*0b57cec5SDimitry Andric SetHelpLong("\nIf no signals are specified, update them all. If no update " 1380*0b57cec5SDimitry Andric "option is specified, list the current values."); 1381*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1382*0b57cec5SDimitry Andric CommandArgumentData signal_arg; 1383*0b57cec5SDimitry Andric 1384*0b57cec5SDimitry Andric signal_arg.arg_type = eArgTypeUnixSignal; 1385*0b57cec5SDimitry Andric signal_arg.arg_repetition = eArgRepeatStar; 1386*0b57cec5SDimitry Andric 1387*0b57cec5SDimitry Andric arg.push_back(signal_arg); 1388*0b57cec5SDimitry Andric 1389*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1390*0b57cec5SDimitry Andric } 1391*0b57cec5SDimitry Andric 1392*0b57cec5SDimitry Andric ~CommandObjectProcessHandle() override = default; 1393*0b57cec5SDimitry Andric 1394*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1395*0b57cec5SDimitry Andric 1396*0b57cec5SDimitry Andric bool VerifyCommandOptionValue(const std::string &option, int &real_value) { 1397*0b57cec5SDimitry Andric bool okay = true; 1398*0b57cec5SDimitry Andric bool success = false; 1399*0b57cec5SDimitry Andric bool tmp_value = OptionArgParser::ToBoolean(option, false, &success); 1400*0b57cec5SDimitry Andric 1401*0b57cec5SDimitry Andric if (success && tmp_value) 1402*0b57cec5SDimitry Andric real_value = 1; 1403*0b57cec5SDimitry Andric else if (success && !tmp_value) 1404*0b57cec5SDimitry Andric real_value = 0; 1405*0b57cec5SDimitry Andric else { 1406*0b57cec5SDimitry Andric // If the value isn't 'true' or 'false', it had better be 0 or 1. 14075ffd83dbSDimitry Andric if (!llvm::to_integer(option, real_value)) 14085ffd83dbSDimitry Andric real_value = 3; 1409*0b57cec5SDimitry Andric if (real_value != 0 && real_value != 1) 1410*0b57cec5SDimitry Andric okay = false; 1411*0b57cec5SDimitry Andric } 1412*0b57cec5SDimitry Andric 1413*0b57cec5SDimitry Andric return okay; 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric 1416*0b57cec5SDimitry Andric void PrintSignalHeader(Stream &str) { 1417*0b57cec5SDimitry Andric str.Printf("NAME PASS STOP NOTIFY\n"); 1418*0b57cec5SDimitry Andric str.Printf("=========== ===== ===== ======\n"); 1419*0b57cec5SDimitry Andric } 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric void PrintSignal(Stream &str, int32_t signo, const char *sig_name, 1422*0b57cec5SDimitry Andric const UnixSignalsSP &signals_sp) { 1423*0b57cec5SDimitry Andric bool stop; 1424*0b57cec5SDimitry Andric bool suppress; 1425*0b57cec5SDimitry Andric bool notify; 1426*0b57cec5SDimitry Andric 1427*0b57cec5SDimitry Andric str.Printf("%-11s ", sig_name); 1428*0b57cec5SDimitry Andric if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) { 1429*0b57cec5SDimitry Andric bool pass = !suppress; 1430*0b57cec5SDimitry Andric str.Printf("%s %s %s", (pass ? "true " : "false"), 1431*0b57cec5SDimitry Andric (stop ? "true " : "false"), (notify ? "true " : "false")); 1432*0b57cec5SDimitry Andric } 1433*0b57cec5SDimitry Andric str.Printf("\n"); 1434*0b57cec5SDimitry Andric } 1435*0b57cec5SDimitry Andric 1436*0b57cec5SDimitry Andric void PrintSignalInformation(Stream &str, Args &signal_args, 1437*0b57cec5SDimitry Andric int num_valid_signals, 1438*0b57cec5SDimitry Andric const UnixSignalsSP &signals_sp) { 1439*0b57cec5SDimitry Andric PrintSignalHeader(str); 1440*0b57cec5SDimitry Andric 1441*0b57cec5SDimitry Andric if (num_valid_signals > 0) { 1442*0b57cec5SDimitry Andric size_t num_args = signal_args.GetArgumentCount(); 1443*0b57cec5SDimitry Andric for (size_t i = 0; i < num_args; ++i) { 1444*0b57cec5SDimitry Andric int32_t signo = signals_sp->GetSignalNumberFromName( 1445*0b57cec5SDimitry Andric signal_args.GetArgumentAtIndex(i)); 1446*0b57cec5SDimitry Andric if (signo != LLDB_INVALID_SIGNAL_NUMBER) 1447*0b57cec5SDimitry Andric PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i), 1448*0b57cec5SDimitry Andric signals_sp); 1449*0b57cec5SDimitry Andric } 1450*0b57cec5SDimitry Andric } else // Print info for ALL signals 1451*0b57cec5SDimitry Andric { 1452*0b57cec5SDimitry Andric int32_t signo = signals_sp->GetFirstSignalNumber(); 1453*0b57cec5SDimitry Andric while (signo != LLDB_INVALID_SIGNAL_NUMBER) { 1454*0b57cec5SDimitry Andric PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo), 1455*0b57cec5SDimitry Andric signals_sp); 1456*0b57cec5SDimitry Andric signo = signals_sp->GetNextSignalNumber(signo); 1457*0b57cec5SDimitry Andric } 1458*0b57cec5SDimitry Andric } 1459*0b57cec5SDimitry Andric } 1460*0b57cec5SDimitry Andric 1461*0b57cec5SDimitry Andric protected: 1462*0b57cec5SDimitry Andric bool DoExecute(Args &signal_args, CommandReturnObject &result) override { 14639dba64beSDimitry Andric Target *target_sp = &GetSelectedTarget(); 1464*0b57cec5SDimitry Andric 1465*0b57cec5SDimitry Andric ProcessSP process_sp = target_sp->GetProcessSP(); 1466*0b57cec5SDimitry Andric 1467*0b57cec5SDimitry Andric if (!process_sp) { 1468*0b57cec5SDimitry Andric result.AppendError("No current process; cannot handle signals until you " 1469*0b57cec5SDimitry Andric "have a valid process.\n"); 1470*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1471*0b57cec5SDimitry Andric return false; 1472*0b57cec5SDimitry Andric } 1473*0b57cec5SDimitry Andric 1474*0b57cec5SDimitry Andric int stop_action = -1; // -1 means leave the current setting alone 1475*0b57cec5SDimitry Andric int pass_action = -1; // -1 means leave the current setting alone 1476*0b57cec5SDimitry Andric int notify_action = -1; // -1 means leave the current setting alone 1477*0b57cec5SDimitry Andric 1478*0b57cec5SDimitry Andric if (!m_options.stop.empty() && 1479*0b57cec5SDimitry Andric !VerifyCommandOptionValue(m_options.stop, stop_action)) { 1480*0b57cec5SDimitry Andric result.AppendError("Invalid argument for command option --stop; must be " 1481*0b57cec5SDimitry Andric "true or false.\n"); 1482*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1483*0b57cec5SDimitry Andric return false; 1484*0b57cec5SDimitry Andric } 1485*0b57cec5SDimitry Andric 1486*0b57cec5SDimitry Andric if (!m_options.notify.empty() && 1487*0b57cec5SDimitry Andric !VerifyCommandOptionValue(m_options.notify, notify_action)) { 1488*0b57cec5SDimitry Andric result.AppendError("Invalid argument for command option --notify; must " 1489*0b57cec5SDimitry Andric "be true or false.\n"); 1490*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1491*0b57cec5SDimitry Andric return false; 1492*0b57cec5SDimitry Andric } 1493*0b57cec5SDimitry Andric 1494*0b57cec5SDimitry Andric if (!m_options.pass.empty() && 1495*0b57cec5SDimitry Andric !VerifyCommandOptionValue(m_options.pass, pass_action)) { 1496*0b57cec5SDimitry Andric result.AppendError("Invalid argument for command option --pass; must be " 1497*0b57cec5SDimitry Andric "true or false.\n"); 1498*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1499*0b57cec5SDimitry Andric return false; 1500*0b57cec5SDimitry Andric } 1501*0b57cec5SDimitry Andric 1502*0b57cec5SDimitry Andric size_t num_args = signal_args.GetArgumentCount(); 1503*0b57cec5SDimitry Andric UnixSignalsSP signals_sp = process_sp->GetUnixSignals(); 1504*0b57cec5SDimitry Andric int num_signals_set = 0; 1505*0b57cec5SDimitry Andric 1506*0b57cec5SDimitry Andric if (num_args > 0) { 1507*0b57cec5SDimitry Andric for (const auto &arg : signal_args) { 1508*0b57cec5SDimitry Andric int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str()); 1509*0b57cec5SDimitry Andric if (signo != LLDB_INVALID_SIGNAL_NUMBER) { 1510*0b57cec5SDimitry Andric // Casting the actions as bools here should be okay, because 1511*0b57cec5SDimitry Andric // VerifyCommandOptionValue guarantees the value is either 0 or 1. 1512*0b57cec5SDimitry Andric if (stop_action != -1) 1513*0b57cec5SDimitry Andric signals_sp->SetShouldStop(signo, stop_action); 1514*0b57cec5SDimitry Andric if (pass_action != -1) { 1515*0b57cec5SDimitry Andric bool suppress = !pass_action; 1516*0b57cec5SDimitry Andric signals_sp->SetShouldSuppress(signo, suppress); 1517*0b57cec5SDimitry Andric } 1518*0b57cec5SDimitry Andric if (notify_action != -1) 1519*0b57cec5SDimitry Andric signals_sp->SetShouldNotify(signo, notify_action); 1520*0b57cec5SDimitry Andric ++num_signals_set; 1521*0b57cec5SDimitry Andric } else { 1522*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid signal name '%s'\n", 1523*0b57cec5SDimitry Andric arg.c_str()); 1524*0b57cec5SDimitry Andric } 1525*0b57cec5SDimitry Andric } 1526*0b57cec5SDimitry Andric } else { 1527*0b57cec5SDimitry Andric // No signal specified, if any command options were specified, update ALL 1528*0b57cec5SDimitry Andric // signals. 1529*0b57cec5SDimitry Andric if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) { 1530*0b57cec5SDimitry Andric if (m_interpreter.Confirm( 1531*0b57cec5SDimitry Andric "Do you really want to update all the signals?", false)) { 1532*0b57cec5SDimitry Andric int32_t signo = signals_sp->GetFirstSignalNumber(); 1533*0b57cec5SDimitry Andric while (signo != LLDB_INVALID_SIGNAL_NUMBER) { 1534*0b57cec5SDimitry Andric if (notify_action != -1) 1535*0b57cec5SDimitry Andric signals_sp->SetShouldNotify(signo, notify_action); 1536*0b57cec5SDimitry Andric if (stop_action != -1) 1537*0b57cec5SDimitry Andric signals_sp->SetShouldStop(signo, stop_action); 1538*0b57cec5SDimitry Andric if (pass_action != -1) { 1539*0b57cec5SDimitry Andric bool suppress = !pass_action; 1540*0b57cec5SDimitry Andric signals_sp->SetShouldSuppress(signo, suppress); 1541*0b57cec5SDimitry Andric } 1542*0b57cec5SDimitry Andric signo = signals_sp->GetNextSignalNumber(signo); 1543*0b57cec5SDimitry Andric } 1544*0b57cec5SDimitry Andric } 1545*0b57cec5SDimitry Andric } 1546*0b57cec5SDimitry Andric } 1547*0b57cec5SDimitry Andric 1548*0b57cec5SDimitry Andric PrintSignalInformation(result.GetOutputStream(), signal_args, 1549*0b57cec5SDimitry Andric num_signals_set, signals_sp); 1550*0b57cec5SDimitry Andric 1551*0b57cec5SDimitry Andric if (num_signals_set > 0) 1552*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1553*0b57cec5SDimitry Andric else 1554*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1555*0b57cec5SDimitry Andric 1556*0b57cec5SDimitry Andric return result.Succeeded(); 1557*0b57cec5SDimitry Andric } 1558*0b57cec5SDimitry Andric 1559*0b57cec5SDimitry Andric CommandOptions m_options; 1560*0b57cec5SDimitry Andric }; 1561*0b57cec5SDimitry Andric 1562*0b57cec5SDimitry Andric // CommandObjectMultiwordProcess 1563*0b57cec5SDimitry Andric 1564*0b57cec5SDimitry Andric CommandObjectMultiwordProcess::CommandObjectMultiwordProcess( 1565*0b57cec5SDimitry Andric CommandInterpreter &interpreter) 1566*0b57cec5SDimitry Andric : CommandObjectMultiword( 1567*0b57cec5SDimitry Andric interpreter, "process", 1568*0b57cec5SDimitry Andric "Commands for interacting with processes on the current platform.", 1569*0b57cec5SDimitry Andric "process <subcommand> [<subcommand-options>]") { 1570*0b57cec5SDimitry Andric LoadSubCommand("attach", 1571*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessAttach(interpreter))); 1572*0b57cec5SDimitry Andric LoadSubCommand("launch", 1573*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessLaunch(interpreter))); 1574*0b57cec5SDimitry Andric LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue( 1575*0b57cec5SDimitry Andric interpreter))); 1576*0b57cec5SDimitry Andric LoadSubCommand("connect", 1577*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessConnect(interpreter))); 1578*0b57cec5SDimitry Andric LoadSubCommand("detach", 1579*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessDetach(interpreter))); 1580*0b57cec5SDimitry Andric LoadSubCommand("load", 1581*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessLoad(interpreter))); 1582*0b57cec5SDimitry Andric LoadSubCommand("unload", 1583*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessUnload(interpreter))); 1584*0b57cec5SDimitry Andric LoadSubCommand("signal", 1585*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessSignal(interpreter))); 1586*0b57cec5SDimitry Andric LoadSubCommand("handle", 1587*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessHandle(interpreter))); 1588*0b57cec5SDimitry Andric LoadSubCommand("status", 1589*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessStatus(interpreter))); 1590*0b57cec5SDimitry Andric LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt( 1591*0b57cec5SDimitry Andric interpreter))); 1592*0b57cec5SDimitry Andric LoadSubCommand("kill", 1593*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessKill(interpreter))); 1594*0b57cec5SDimitry Andric LoadSubCommand("plugin", 1595*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectProcessPlugin(interpreter))); 1596*0b57cec5SDimitry Andric LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore( 1597*0b57cec5SDimitry Andric interpreter))); 1598*0b57cec5SDimitry Andric } 1599*0b57cec5SDimitry Andric 1600*0b57cec5SDimitry Andric CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default; 1601