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