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