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