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