1//===-- Process.h -----------------------------------------------*- 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#ifndef liblldb_Process_h_ 11#define liblldb_Process_h_ 12 13#include "lldb/Host/Config.h" 14 15// C Includes 16#include <limits.h> 17 18// C++ Includes 19#include <list> 20#include <iosfwd> 21#include <vector> 22 23// Other libraries and framework includes 24// Project includes 25#include "lldb/lldb-private.h" 26#include "lldb/Core/ArchSpec.h" 27#include "lldb/Core/Broadcaster.h" 28#include "lldb/Core/Communication.h" 29#include "lldb/Core/Error.h" 30#include "lldb/Core/Event.h" 31#include "lldb/Core/RangeMap.h" 32#include "lldb/Core/StringList.h" 33#include "lldb/Core/ThreadSafeValue.h" 34#include "lldb/Core/PluginInterface.h" 35#include "lldb/Core/UserSettingsController.h" 36#include "lldb/Breakpoint/BreakpointSiteList.h" 37#include "lldb/Expression/ClangPersistentVariables.h" 38#include "lldb/Expression/IRDynamicChecks.h" 39#include "lldb/Host/FileSpec.h" 40#include "lldb/Host/Host.h" 41#include "lldb/Host/ProcessRunLock.h" 42#include "lldb/Interpreter/Args.h" 43#include "lldb/Interpreter/Options.h" 44#include "lldb/Target/ExecutionContextScope.h" 45#include "lldb/Target/Memory.h" 46#include "lldb/Target/QueueList.h" 47#include "lldb/Target/ThreadList.h" 48#include "lldb/Target/UnixSignals.h" 49#include "lldb/Utility/PseudoTerminal.h" 50 51namespace lldb_private { 52 53//---------------------------------------------------------------------- 54// ProcessProperties 55//---------------------------------------------------------------------- 56class ProcessProperties : public Properties 57{ 58public: 59 ProcessProperties(bool is_global); 60 61 virtual 62 ~ProcessProperties(); 63 64 bool 65 GetDisableMemoryCache() const; 66 67 Args 68 GetExtraStartupCommands () const; 69 70 void 71 SetExtraStartupCommands (const Args &args); 72 73 FileSpec 74 GetPythonOSPluginPath () const; 75 76 void 77 SetPythonOSPluginPath (const FileSpec &file); 78 79 bool 80 GetIgnoreBreakpointsInExpressions () const; 81 82 void 83 SetIgnoreBreakpointsInExpressions (bool ignore); 84 85 bool 86 GetUnwindOnErrorInExpressions () const; 87 88 void 89 SetUnwindOnErrorInExpressions (bool ignore); 90 91 bool 92 GetStopOnSharedLibraryEvents () const; 93 94 void 95 SetStopOnSharedLibraryEvents (bool stop); 96 97 bool 98 GetDetachKeepsStopped () const; 99 100 void 101 SetDetachKeepsStopped (bool keep_stopped); 102}; 103 104typedef std::shared_ptr<ProcessProperties> ProcessPropertiesSP; 105 106//---------------------------------------------------------------------- 107// ProcessInfo 108// 109// A base class for information for a process. This can be used to fill 110// out information for a process prior to launching it, or it can be 111// used for an instance of a process and can be filled in with the 112// existing values for that process. 113//---------------------------------------------------------------------- 114class ProcessInfo 115{ 116public: 117 ProcessInfo () : 118 m_executable (), 119 m_arguments (), 120 m_environment (), 121 m_uid (UINT32_MAX), 122 m_gid (UINT32_MAX), 123 m_arch(), 124 m_pid (LLDB_INVALID_PROCESS_ID) 125 { 126 } 127 128 ProcessInfo (const char *name, 129 const ArchSpec &arch, 130 lldb::pid_t pid) : 131 m_executable (name, false), 132 m_arguments (), 133 m_environment(), 134 m_uid (UINT32_MAX), 135 m_gid (UINT32_MAX), 136 m_arch (arch), 137 m_pid (pid) 138 { 139 } 140 141 void 142 Clear () 143 { 144 m_executable.Clear(); 145 m_arguments.Clear(); 146 m_environment.Clear(); 147 m_uid = UINT32_MAX; 148 m_gid = UINT32_MAX; 149 m_arch.Clear(); 150 m_pid = LLDB_INVALID_PROCESS_ID; 151 } 152 153 const char * 154 GetName() const 155 { 156 return m_executable.GetFilename().GetCString(); 157 } 158 159 size_t 160 GetNameLength() const 161 { 162 return m_executable.GetFilename().GetLength(); 163 } 164 165 FileSpec & 166 GetExecutableFile () 167 { 168 return m_executable; 169 } 170 171 void 172 SetExecutableFile (const FileSpec &exe_file, bool add_exe_file_as_first_arg) 173 { 174 if (exe_file) 175 { 176 m_executable = exe_file; 177 if (add_exe_file_as_first_arg) 178 { 179 char filename[PATH_MAX]; 180 if (exe_file.GetPath(filename, sizeof(filename))) 181 m_arguments.InsertArgumentAtIndex (0, filename); 182 } 183 } 184 else 185 { 186 m_executable.Clear(); 187 } 188 } 189 190 const FileSpec & 191 GetExecutableFile () const 192 { 193 return m_executable; 194 } 195 196 uint32_t 197 GetUserID() const 198 { 199 return m_uid; 200 } 201 202 uint32_t 203 GetGroupID() const 204 { 205 return m_gid; 206 } 207 208 bool 209 UserIDIsValid () const 210 { 211 return m_uid != UINT32_MAX; 212 } 213 214 bool 215 GroupIDIsValid () const 216 { 217 return m_gid != UINT32_MAX; 218 } 219 220 void 221 SetUserID (uint32_t uid) 222 { 223 m_uid = uid; 224 } 225 226 void 227 SetGroupID (uint32_t gid) 228 { 229 m_gid = gid; 230 } 231 232 ArchSpec & 233 GetArchitecture () 234 { 235 return m_arch; 236 } 237 238 const ArchSpec & 239 GetArchitecture () const 240 { 241 return m_arch; 242 } 243 244 void 245 SetArchitecture (ArchSpec arch) 246 { 247 m_arch = arch; 248 } 249 250 lldb::pid_t 251 GetProcessID () const 252 { 253 return m_pid; 254 } 255 256 void 257 SetProcessID (lldb::pid_t pid) 258 { 259 m_pid = pid; 260 } 261 262 bool 263 ProcessIDIsValid() const 264 { 265 return m_pid != LLDB_INVALID_PROCESS_ID; 266 } 267 268 void 269 Dump (Stream &s, Platform *platform) const; 270 271 Args & 272 GetArguments () 273 { 274 return m_arguments; 275 } 276 277 const Args & 278 GetArguments () const 279 { 280 return m_arguments; 281 } 282 283 const char * 284 GetArg0 () const 285 { 286 if (m_arg0.empty()) 287 return NULL; 288 return m_arg0.c_str(); 289 } 290 291 void 292 SetArg0 (const char *arg) 293 { 294 if (arg && arg[0]) 295 m_arg0 = arg; 296 else 297 m_arg0.clear(); 298 } 299 300 void 301 SetArguments (const Args& args, bool first_arg_is_executable); 302 303 void 304 SetArguments (char const **argv, bool first_arg_is_executable); 305 306 Args & 307 GetEnvironmentEntries () 308 { 309 return m_environment; 310 } 311 312 const Args & 313 GetEnvironmentEntries () const 314 { 315 return m_environment; 316 } 317 318protected: 319 FileSpec m_executable; 320 std::string m_arg0; // argv[0] if supported. If empty, then use m_executable. 321 // Not all process plug-ins support specifying an argv[0] 322 // that differs from the resolved platform executable 323 // (which is in m_executable) 324 Args m_arguments; // All program arguments except argv[0] 325 Args m_environment; 326 uint32_t m_uid; 327 uint32_t m_gid; 328 ArchSpec m_arch; 329 lldb::pid_t m_pid; 330}; 331 332//---------------------------------------------------------------------- 333// ProcessInstanceInfo 334// 335// Describes an existing process and any discoverable information that 336// pertains to that process. 337//---------------------------------------------------------------------- 338class ProcessInstanceInfo : public ProcessInfo 339{ 340public: 341 ProcessInstanceInfo () : 342 ProcessInfo (), 343 m_euid (UINT32_MAX), 344 m_egid (UINT32_MAX), 345 m_parent_pid (LLDB_INVALID_PROCESS_ID) 346 { 347 } 348 349 ProcessInstanceInfo (const char *name, 350 const ArchSpec &arch, 351 lldb::pid_t pid) : 352 ProcessInfo (name, arch, pid), 353 m_euid (UINT32_MAX), 354 m_egid (UINT32_MAX), 355 m_parent_pid (LLDB_INVALID_PROCESS_ID) 356 { 357 } 358 359 void 360 Clear () 361 { 362 ProcessInfo::Clear(); 363 m_euid = UINT32_MAX; 364 m_egid = UINT32_MAX; 365 m_parent_pid = LLDB_INVALID_PROCESS_ID; 366 } 367 368 uint32_t 369 GetEffectiveUserID() const 370 { 371 return m_euid; 372 } 373 374 uint32_t 375 GetEffectiveGroupID() const 376 { 377 return m_egid; 378 } 379 380 bool 381 EffectiveUserIDIsValid () const 382 { 383 return m_euid != UINT32_MAX; 384 } 385 386 bool 387 EffectiveGroupIDIsValid () const 388 { 389 return m_egid != UINT32_MAX; 390 } 391 392 void 393 SetEffectiveUserID (uint32_t uid) 394 { 395 m_euid = uid; 396 } 397 398 void 399 SetEffectiveGroupID (uint32_t gid) 400 { 401 m_egid = gid; 402 } 403 404 lldb::pid_t 405 GetParentProcessID () const 406 { 407 return m_parent_pid; 408 } 409 410 void 411 SetParentProcessID (lldb::pid_t pid) 412 { 413 m_parent_pid = pid; 414 } 415 416 bool 417 ParentProcessIDIsValid() const 418 { 419 return m_parent_pid != LLDB_INVALID_PROCESS_ID; 420 } 421 422 void 423 Dump (Stream &s, Platform *platform) const; 424 425 static void 426 DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose); 427 428 void 429 DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const; 430 431protected: 432 uint32_t m_euid; 433 uint32_t m_egid; 434 lldb::pid_t m_parent_pid; 435}; 436 437 438//---------------------------------------------------------------------- 439// ProcessLaunchInfo 440// 441// Describes any information that is required to launch a process. 442//---------------------------------------------------------------------- 443 444class ProcessLaunchInfo : public ProcessInfo 445{ 446public: 447 448 class FileAction 449 { 450 public: 451 enum Action 452 { 453 eFileActionNone, 454 eFileActionClose, 455 eFileActionDuplicate, 456 eFileActionOpen 457 }; 458 459 460 FileAction () : 461 m_action (eFileActionNone), 462 m_fd (-1), 463 m_arg (-1), 464 m_path () 465 { 466 } 467 468 void 469 Clear() 470 { 471 m_action = eFileActionNone; 472 m_fd = -1; 473 m_arg = -1; 474 m_path.clear(); 475 } 476 477 bool 478 Close (int fd); 479 480 bool 481 Duplicate (int fd, int dup_fd); 482 483 bool 484 Open (int fd, const char *path, bool read, bool write); 485 486#ifndef LLDB_DISABLE_POSIX 487 static bool 488 AddPosixSpawnFileAction (void *file_actions, 489 const FileAction *info, 490 Log *log, 491 Error& error); 492#endif 493 494 int 495 GetFD () const 496 { 497 return m_fd; 498 } 499 500 Action 501 GetAction () const 502 { 503 return m_action; 504 } 505 506 int 507 GetActionArgument () const 508 { 509 return m_arg; 510 } 511 512 const char * 513 GetPath () const 514 { 515 if (m_path.empty()) 516 return NULL; 517 return m_path.c_str(); 518 } 519 520 protected: 521 Action m_action; // The action for this file 522 int m_fd; // An existing file descriptor 523 int m_arg; // oflag for eFileActionOpen*, dup_fd for eFileActionDuplicate 524 std::string m_path; // A file path to use for opening after fork or posix_spawn 525 }; 526 527 ProcessLaunchInfo () : 528 ProcessInfo(), 529 m_working_dir (), 530 m_plugin_name (), 531 m_shell (), 532 m_flags (0), 533 m_file_actions (), 534 m_pty (), 535 m_resume_count (0), 536 m_monitor_callback (NULL), 537 m_monitor_callback_baton (NULL), 538 m_monitor_signals (false), 539 m_hijack_listener_sp () 540 { 541 } 542 543 ProcessLaunchInfo (const char *stdin_path, 544 const char *stdout_path, 545 const char *stderr_path, 546 const char *working_directory, 547 uint32_t launch_flags) : 548 ProcessInfo(), 549 m_working_dir (), 550 m_plugin_name (), 551 m_shell (), 552 m_flags (launch_flags), 553 m_file_actions (), 554 m_pty (), 555 m_resume_count (0), 556 m_monitor_callback (NULL), 557 m_monitor_callback_baton (NULL), 558 m_monitor_signals (false), 559 m_hijack_listener_sp () 560 { 561 if (stdin_path) 562 { 563 ProcessLaunchInfo::FileAction file_action; 564 const bool read = true; 565 const bool write = false; 566 if (file_action.Open(STDIN_FILENO, stdin_path, read, write)) 567 AppendFileAction (file_action); 568 } 569 if (stdout_path) 570 { 571 ProcessLaunchInfo::FileAction file_action; 572 const bool read = false; 573 const bool write = true; 574 if (file_action.Open(STDOUT_FILENO, stdout_path, read, write)) 575 AppendFileAction (file_action); 576 } 577 if (stderr_path) 578 { 579 ProcessLaunchInfo::FileAction file_action; 580 const bool read = false; 581 const bool write = true; 582 if (file_action.Open(STDERR_FILENO, stderr_path, read, write)) 583 AppendFileAction (file_action); 584 } 585 if (working_directory) 586 SetWorkingDirectory(working_directory); 587 } 588 589 void 590 AppendFileAction (const FileAction &info) 591 { 592 m_file_actions.push_back(info); 593 } 594 595 bool 596 AppendCloseFileAction (int fd) 597 { 598 FileAction file_action; 599 if (file_action.Close (fd)) 600 { 601 AppendFileAction (file_action); 602 return true; 603 } 604 return false; 605 } 606 607 bool 608 AppendDuplicateFileAction (int fd, int dup_fd) 609 { 610 FileAction file_action; 611 if (file_action.Duplicate (fd, dup_fd)) 612 { 613 AppendFileAction (file_action); 614 return true; 615 } 616 return false; 617 } 618 619 bool 620 AppendOpenFileAction (int fd, const char *path, bool read, bool write) 621 { 622 FileAction file_action; 623 if (file_action.Open (fd, path, read, write)) 624 { 625 AppendFileAction (file_action); 626 return true; 627 } 628 return false; 629 } 630 631 bool 632 AppendSuppressFileAction (int fd, bool read, bool write) 633 { 634 FileAction file_action; 635 if (file_action.Open (fd, "/dev/null", read, write)) 636 { 637 AppendFileAction (file_action); 638 return true; 639 } 640 return false; 641 } 642 643 void 644 FinalizeFileActions (Target *target, 645 bool default_to_use_pty); 646 647 size_t 648 GetNumFileActions () const 649 { 650 return m_file_actions.size(); 651 } 652 653 const FileAction * 654 GetFileActionAtIndex (size_t idx) const 655 { 656 if (idx < m_file_actions.size()) 657 return &m_file_actions[idx]; 658 return NULL; 659 } 660 661 const FileAction * 662 GetFileActionForFD (int fd) const 663 { 664 for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx) 665 { 666 if (m_file_actions[idx].GetFD () == fd) 667 return &m_file_actions[idx]; 668 } 669 return NULL; 670 } 671 672 Flags & 673 GetFlags () 674 { 675 return m_flags; 676 } 677 678 const Flags & 679 GetFlags () const 680 { 681 return m_flags; 682 } 683 684 const char * 685 GetWorkingDirectory () const 686 { 687 if (m_working_dir.empty()) 688 return NULL; 689 return m_working_dir.c_str(); 690 } 691 692 void 693 SetWorkingDirectory (const char *working_dir) 694 { 695 if (working_dir && working_dir[0]) 696 m_working_dir.assign (working_dir); 697 else 698 m_working_dir.clear(); 699 } 700 701 void 702 SwapWorkingDirectory (std::string &working_dir) 703 { 704 m_working_dir.swap (working_dir); 705 } 706 707 708 const char * 709 GetProcessPluginName () const 710 { 711 if (m_plugin_name.empty()) 712 return NULL; 713 return m_plugin_name.c_str(); 714 } 715 716 void 717 SetProcessPluginName (const char *plugin) 718 { 719 if (plugin && plugin[0]) 720 m_plugin_name.assign (plugin); 721 else 722 m_plugin_name.clear(); 723 } 724 725 const char * 726 GetShell () const 727 { 728 if (m_shell.empty()) 729 return NULL; 730 return m_shell.c_str(); 731 } 732 733 void 734 SetShell (const char * path) 735 { 736 if (path && path[0]) 737 { 738 m_shell.assign (path); 739 m_flags.Set (lldb::eLaunchFlagLaunchInShell); 740 } 741 else 742 { 743 m_shell.clear(); 744 m_flags.Clear (lldb::eLaunchFlagLaunchInShell); 745 } 746 } 747 748 uint32_t 749 GetResumeCount () const 750 { 751 return m_resume_count; 752 } 753 754 void 755 SetResumeCount (uint32_t c) 756 { 757 m_resume_count = c; 758 } 759 760 bool 761 GetLaunchInSeparateProcessGroup () 762 { 763 return m_flags.Test(lldb::eLaunchFlagLaunchInSeparateProcessGroup); 764 } 765 766 void 767 SetLaunchInSeparateProcessGroup (bool separate) 768 { 769 if (separate) 770 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); 771 else 772 m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup); 773 774 } 775 776 void 777 Clear () 778 { 779 ProcessInfo::Clear(); 780 m_working_dir.clear(); 781 m_plugin_name.clear(); 782 m_shell.clear(); 783 m_flags.Clear(); 784 m_file_actions.clear(); 785 m_resume_count = 0; 786 m_hijack_listener_sp.reset(); 787 } 788 789 bool 790 ConvertArgumentsForLaunchingInShell (Error &error, 791 bool localhost, 792 bool will_debug, 793 bool first_arg_is_full_shell_command, 794 int32_t num_resumes); 795 796 void 797 SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback, 798 void *baton, 799 bool monitor_signals) 800 { 801 m_monitor_callback = callback; 802 m_monitor_callback_baton = baton; 803 m_monitor_signals = monitor_signals; 804 } 805 806 Host::MonitorChildProcessCallback 807 GetMonitorProcessCallback () 808 { 809 return m_monitor_callback; 810 } 811 812 const void* 813 GetMonitorProcessBaton () const 814 { 815 return m_monitor_callback_baton; 816 } 817 818 // If the LaunchInfo has a monitor callback, then arrange to monitor the process. 819 // Return true if the LaunchInfo has taken care of monitoring the process, and false if the 820 // caller might want to monitor the process themselves. 821 822 bool 823 MonitorProcess () const 824 { 825 if (GetFlags().Test(lldb::eLaunchFlagsDontMonitorProcess)) 826 return true; 827 828 if (m_monitor_callback && ProcessIDIsValid()) 829 { 830 Host::StartMonitoringChildProcess (m_monitor_callback, 831 m_monitor_callback_baton, 832 GetProcessID(), 833 m_monitor_signals); 834 return true; 835 } 836 return false; 837 } 838 839 lldb_utility::PseudoTerminal & 840 GetPTY () 841 { 842 return m_pty; 843 } 844 845 lldb::ListenerSP 846 GetHijackListener () const 847 { 848 return m_hijack_listener_sp; 849 } 850 851 void 852 SetHijackListener (const lldb::ListenerSP &listener_sp) 853 { 854 m_hijack_listener_sp = listener_sp; 855 } 856 857 858protected: 859 std::string m_working_dir; 860 std::string m_plugin_name; 861 std::string m_shell; 862 Flags m_flags; // Bitwise OR of bits from lldb::LaunchFlags 863 std::vector<FileAction> m_file_actions; // File actions for any other files 864 lldb_utility::PseudoTerminal m_pty; 865 uint32_t m_resume_count; // How many times do we resume after launching 866 Host::MonitorChildProcessCallback m_monitor_callback; 867 void *m_monitor_callback_baton; 868 bool m_monitor_signals; 869 lldb::ListenerSP m_hijack_listener_sp; 870}; 871 872//---------------------------------------------------------------------- 873// ProcessLaunchInfo 874// 875// Describes any information that is required to launch a process. 876//---------------------------------------------------------------------- 877 878class ProcessAttachInfo : public ProcessInstanceInfo 879{ 880public: 881 ProcessAttachInfo() : 882 ProcessInstanceInfo(), 883 m_plugin_name (), 884 m_resume_count (0), 885 m_wait_for_launch (false), 886 m_ignore_existing (true), 887 m_continue_once_attached (false) 888 { 889 } 890 891 ProcessAttachInfo (const ProcessLaunchInfo &launch_info) : 892 ProcessInstanceInfo(), 893 m_plugin_name (), 894 m_resume_count (0), 895 m_wait_for_launch (false), 896 m_ignore_existing (true), 897 m_continue_once_attached (false) 898 { 899 ProcessInfo::operator= (launch_info); 900 SetProcessPluginName (launch_info.GetProcessPluginName()); 901 SetResumeCount (launch_info.GetResumeCount()); 902 SetHijackListener(launch_info.GetHijackListener()); 903 } 904 905 bool 906 GetWaitForLaunch () const 907 { 908 return m_wait_for_launch; 909 } 910 911 void 912 SetWaitForLaunch (bool b) 913 { 914 m_wait_for_launch = b; 915 } 916 917 bool 918 GetIgnoreExisting () const 919 { 920 return m_ignore_existing; 921 } 922 923 void 924 SetIgnoreExisting (bool b) 925 { 926 m_ignore_existing = b; 927 } 928 929 bool 930 GetContinueOnceAttached () const 931 { 932 return m_continue_once_attached; 933 } 934 935 void 936 SetContinueOnceAttached (bool b) 937 { 938 m_continue_once_attached = b; 939 } 940 941 uint32_t 942 GetResumeCount () const 943 { 944 return m_resume_count; 945 } 946 947 void 948 SetResumeCount (uint32_t c) 949 { 950 m_resume_count = c; 951 } 952 953 const char * 954 GetProcessPluginName () const 955 { 956 if (m_plugin_name.empty()) 957 return NULL; 958 return m_plugin_name.c_str(); 959 } 960 961 void 962 SetProcessPluginName (const char *plugin) 963 { 964 if (plugin && plugin[0]) 965 m_plugin_name.assign (plugin); 966 else 967 m_plugin_name.clear(); 968 } 969 970 void 971 Clear () 972 { 973 ProcessInstanceInfo::Clear(); 974 m_plugin_name.clear(); 975 m_resume_count = 0; 976 m_wait_for_launch = false; 977 m_ignore_existing = true; 978 m_continue_once_attached = false; 979 } 980 981 bool 982 ProcessInfoSpecified () const 983 { 984 if (GetExecutableFile()) 985 return true; 986 if (GetProcessID() != LLDB_INVALID_PROCESS_ID) 987 return true; 988 if (GetParentProcessID() != LLDB_INVALID_PROCESS_ID) 989 return true; 990 return false; 991 } 992 993 lldb::ListenerSP 994 GetHijackListener () const 995 { 996 return m_hijack_listener_sp; 997 } 998 999 void 1000 SetHijackListener (const lldb::ListenerSP &listener_sp) 1001 { 1002 m_hijack_listener_sp = listener_sp; 1003 } 1004 1005 1006protected: 1007 lldb::ListenerSP m_hijack_listener_sp; 1008 std::string m_plugin_name; 1009 uint32_t m_resume_count; // How many times do we resume after launching 1010 bool m_wait_for_launch; 1011 bool m_ignore_existing; 1012 bool m_continue_once_attached; // Supports the use-case scenario of immediately continuing the process once attached. 1013}; 1014 1015class ProcessLaunchCommandOptions : public Options 1016{ 1017public: 1018 1019 ProcessLaunchCommandOptions (CommandInterpreter &interpreter) : 1020 Options(interpreter) 1021 { 1022 // Keep default values of all options in one place: OptionParsingStarting () 1023 OptionParsingStarting (); 1024 } 1025 1026 ~ProcessLaunchCommandOptions () 1027 { 1028 } 1029 1030 Error 1031 SetOptionValue (uint32_t option_idx, const char *option_arg); 1032 1033 void 1034 OptionParsingStarting () 1035 { 1036 launch_info.Clear(); 1037 } 1038 1039 const OptionDefinition* 1040 GetDefinitions () 1041 { 1042 return g_option_table; 1043 } 1044 1045 // Options table: Required for subclasses of Options. 1046 1047 static OptionDefinition g_option_table[]; 1048 1049 // Instance variables to hold the values for command options. 1050 1051 ProcessLaunchInfo launch_info; 1052}; 1053 1054//---------------------------------------------------------------------- 1055// ProcessInstanceInfoMatch 1056// 1057// A class to help matching one ProcessInstanceInfo to another. 1058//---------------------------------------------------------------------- 1059 1060class ProcessInstanceInfoMatch 1061{ 1062public: 1063 ProcessInstanceInfoMatch () : 1064 m_match_info (), 1065 m_name_match_type (eNameMatchIgnore), 1066 m_match_all_users (false) 1067 { 1068 } 1069 1070 ProcessInstanceInfoMatch (const char *process_name, 1071 NameMatchType process_name_match_type) : 1072 m_match_info (), 1073 m_name_match_type (process_name_match_type), 1074 m_match_all_users (false) 1075 { 1076 m_match_info.GetExecutableFile().SetFile(process_name, false); 1077 } 1078 1079 ProcessInstanceInfo & 1080 GetProcessInfo () 1081 { 1082 return m_match_info; 1083 } 1084 1085 const ProcessInstanceInfo & 1086 GetProcessInfo () const 1087 { 1088 return m_match_info; 1089 } 1090 1091 bool 1092 GetMatchAllUsers () const 1093 { 1094 return m_match_all_users; 1095 } 1096 1097 void 1098 SetMatchAllUsers (bool b) 1099 { 1100 m_match_all_users = b; 1101 } 1102 1103 NameMatchType 1104 GetNameMatchType () const 1105 { 1106 return m_name_match_type; 1107 } 1108 1109 void 1110 SetNameMatchType (NameMatchType name_match_type) 1111 { 1112 m_name_match_type = name_match_type; 1113 } 1114 1115 bool 1116 NameMatches (const char *process_name) const; 1117 1118 bool 1119 Matches (const ProcessInstanceInfo &proc_info) const; 1120 1121 bool 1122 MatchAllProcesses () const; 1123 void 1124 Clear (); 1125 1126protected: 1127 ProcessInstanceInfo m_match_info; 1128 NameMatchType m_name_match_type; 1129 bool m_match_all_users; 1130}; 1131 1132class ProcessInstanceInfoList 1133{ 1134public: 1135 ProcessInstanceInfoList () : 1136 m_infos() 1137 { 1138 } 1139 1140 void 1141 Clear() 1142 { 1143 m_infos.clear(); 1144 } 1145 1146 size_t 1147 GetSize() 1148 { 1149 return m_infos.size(); 1150 } 1151 1152 void 1153 Append (const ProcessInstanceInfo &info) 1154 { 1155 m_infos.push_back (info); 1156 } 1157 1158 const char * 1159 GetProcessNameAtIndex (size_t idx) 1160 { 1161 if (idx < m_infos.size()) 1162 return m_infos[idx].GetName(); 1163 return NULL; 1164 } 1165 1166 size_t 1167 GetProcessNameLengthAtIndex (size_t idx) 1168 { 1169 if (idx < m_infos.size()) 1170 return m_infos[idx].GetNameLength(); 1171 return 0; 1172 } 1173 1174 lldb::pid_t 1175 GetProcessIDAtIndex (size_t idx) 1176 { 1177 if (idx < m_infos.size()) 1178 return m_infos[idx].GetProcessID(); 1179 return 0; 1180 } 1181 1182 bool 1183 GetInfoAtIndex (size_t idx, ProcessInstanceInfo &info) 1184 { 1185 if (idx < m_infos.size()) 1186 { 1187 info = m_infos[idx]; 1188 return true; 1189 } 1190 return false; 1191 } 1192 1193 // You must ensure "idx" is valid before calling this function 1194 const ProcessInstanceInfo & 1195 GetProcessInfoAtIndex (size_t idx) const 1196 { 1197 assert (idx < m_infos.size()); 1198 return m_infos[idx]; 1199 } 1200 1201protected: 1202 typedef std::vector<ProcessInstanceInfo> collection; 1203 collection m_infos; 1204}; 1205 1206 1207// This class tracks the Modification state of the process. Things that can currently modify 1208// the program are running the program (which will up the StopID) and writing memory (which 1209// will up the MemoryID.) 1210// FIXME: Should we also include modification of register states? 1211 1212class ProcessModID 1213{ 1214friend bool operator== (const ProcessModID &lhs, const ProcessModID &rhs); 1215public: 1216 ProcessModID () : 1217 m_stop_id (0), 1218 m_last_natural_stop_id(0), 1219 m_resume_id (0), 1220 m_memory_id (0), 1221 m_last_user_expression_resume (0), 1222 m_running_user_expression (false) 1223 {} 1224 1225 ProcessModID (const ProcessModID &rhs) : 1226 m_stop_id (rhs.m_stop_id), 1227 m_memory_id (rhs.m_memory_id) 1228 {} 1229 1230 const ProcessModID & operator= (const ProcessModID &rhs) 1231 { 1232 if (this != &rhs) 1233 { 1234 m_stop_id = rhs.m_stop_id; 1235 m_memory_id = rhs.m_memory_id; 1236 } 1237 return *this; 1238 } 1239 1240 ~ProcessModID () {} 1241 1242 void BumpStopID () { 1243 m_stop_id++; 1244 if (!IsLastResumeForUserExpression()) 1245 m_last_natural_stop_id++; 1246 } 1247 1248 void BumpMemoryID () { m_memory_id++; } 1249 1250 void BumpResumeID () { 1251 m_resume_id++; 1252 if (m_running_user_expression > 0) 1253 m_last_user_expression_resume = m_resume_id; 1254 } 1255 1256 uint32_t GetStopID() const { return m_stop_id; } 1257 uint32_t GetLastNaturalStopID() const { return m_last_natural_stop_id; } 1258 uint32_t GetMemoryID () const { return m_memory_id; } 1259 uint32_t GetResumeID () const { return m_resume_id; } 1260 uint32_t GetLastUserExpressionResumeID () const { return m_last_user_expression_resume; } 1261 1262 bool MemoryIDEqual (const ProcessModID &compare) const 1263 { 1264 return m_memory_id == compare.m_memory_id; 1265 } 1266 1267 bool StopIDEqual (const ProcessModID &compare) const 1268 { 1269 return m_stop_id == compare.m_stop_id; 1270 } 1271 1272 void SetInvalid () 1273 { 1274 m_stop_id = UINT32_MAX; 1275 } 1276 1277 bool IsValid () const 1278 { 1279 return m_stop_id != UINT32_MAX; 1280 } 1281 1282 bool 1283 IsLastResumeForUserExpression () const 1284 { 1285 return m_resume_id == m_last_user_expression_resume; 1286 } 1287 1288 void 1289 SetRunningUserExpression (bool on) 1290 { 1291 // REMOVEME printf ("Setting running user expression %s at resume id %d - value: %d.\n", on ? "on" : "off", m_resume_id, m_running_user_expression); 1292 if (on) 1293 m_running_user_expression++; 1294 else 1295 m_running_user_expression--; 1296 } 1297 1298private: 1299 uint32_t m_stop_id; 1300 uint32_t m_last_natural_stop_id; 1301 uint32_t m_resume_id; 1302 uint32_t m_memory_id; 1303 uint32_t m_last_user_expression_resume; 1304 uint32_t m_running_user_expression; 1305}; 1306inline bool operator== (const ProcessModID &lhs, const ProcessModID &rhs) 1307{ 1308 if (lhs.StopIDEqual (rhs) 1309 && lhs.MemoryIDEqual (rhs)) 1310 return true; 1311 else 1312 return false; 1313} 1314 1315inline bool operator!= (const ProcessModID &lhs, const ProcessModID &rhs) 1316{ 1317 if (!lhs.StopIDEqual (rhs) 1318 || !lhs.MemoryIDEqual (rhs)) 1319 return true; 1320 else 1321 return false; 1322} 1323 1324class MemoryRegionInfo 1325{ 1326public: 1327 typedef Range<lldb::addr_t, lldb::addr_t> RangeType; 1328 1329 enum OptionalBool { 1330 eDontKnow = -1, 1331 eNo = 0, 1332 eYes = 1 1333 }; 1334 1335 MemoryRegionInfo () : 1336 m_range (), 1337 m_read (eDontKnow), 1338 m_write (eDontKnow), 1339 m_execute (eDontKnow) 1340 { 1341 } 1342 1343 ~MemoryRegionInfo () 1344 { 1345 } 1346 1347 RangeType & 1348 GetRange() 1349 { 1350 return m_range; 1351 } 1352 1353 void 1354 Clear() 1355 { 1356 m_range.Clear(); 1357 m_read = m_write = m_execute = eDontKnow; 1358 } 1359 1360 const RangeType & 1361 GetRange() const 1362 { 1363 return m_range; 1364 } 1365 1366 OptionalBool 1367 GetReadable () const 1368 { 1369 return m_read; 1370 } 1371 1372 OptionalBool 1373 GetWritable () const 1374 { 1375 return m_write; 1376 } 1377 1378 OptionalBool 1379 GetExecutable () const 1380 { 1381 return m_execute; 1382 } 1383 1384 void 1385 SetReadable (OptionalBool val) 1386 { 1387 m_read = val; 1388 } 1389 1390 void 1391 SetWritable (OptionalBool val) 1392 { 1393 m_write = val; 1394 } 1395 1396 void 1397 SetExecutable (OptionalBool val) 1398 { 1399 m_execute = val; 1400 } 1401 1402protected: 1403 RangeType m_range; 1404 OptionalBool m_read; 1405 OptionalBool m_write; 1406 OptionalBool m_execute; 1407}; 1408 1409//---------------------------------------------------------------------- 1410/// @class Process Process.h "lldb/Target/Process.h" 1411/// @brief A plug-in interface definition class for debugging a process. 1412//---------------------------------------------------------------------- 1413class Process : 1414 public std::enable_shared_from_this<Process>, 1415 public ProcessProperties, 1416 public UserID, 1417 public Broadcaster, 1418 public ExecutionContextScope, 1419 public PluginInterface 1420{ 1421 friend class ClangFunction; // For WaitForStateChangeEventsPrivate 1422 friend class ProcessEventData; 1423 friend class StopInfo; 1424 friend class Target; 1425 friend class ThreadList; 1426 1427public: 1428 1429 //------------------------------------------------------------------ 1430 /// Broadcaster event bits definitions. 1431 //------------------------------------------------------------------ 1432 enum 1433 { 1434 eBroadcastBitStateChanged = (1 << 0), 1435 eBroadcastBitInterrupt = (1 << 1), 1436 eBroadcastBitSTDOUT = (1 << 2), 1437 eBroadcastBitSTDERR = (1 << 3), 1438 eBroadcastBitProfileData = (1 << 4) 1439 }; 1440 1441 enum 1442 { 1443 eBroadcastInternalStateControlStop = (1<<0), 1444 eBroadcastInternalStateControlPause = (1<<1), 1445 eBroadcastInternalStateControlResume = (1<<2) 1446 }; 1447 1448 typedef Range<lldb::addr_t, lldb::addr_t> LoadRange; 1449 // We use a read/write lock to allow on or more clients to 1450 // access the process state while the process is stopped (reader). 1451 // We lock the write lock to control access to the process 1452 // while it is running (readers, or clients that want the process 1453 // stopped can block waiting for the process to stop, or just 1454 // try to lock it to see if they can immediately access the stopped 1455 // process. If the try read lock fails, then the process is running. 1456 typedef ProcessRunLock::ProcessRunLocker StopLocker; 1457 1458 // These two functions fill out the Broadcaster interface: 1459 1460 static ConstString &GetStaticBroadcasterClass (); 1461 1462 virtual ConstString &GetBroadcasterClass() const 1463 { 1464 return GetStaticBroadcasterClass(); 1465 } 1466 1467 1468 //------------------------------------------------------------------ 1469 /// A notification structure that can be used by clients to listen 1470 /// for changes in a process's lifetime. 1471 /// 1472 /// @see RegisterNotificationCallbacks (const Notifications&) 1473 /// @see UnregisterNotificationCallbacks (const Notifications&) 1474 //------------------------------------------------------------------ 1475#ifndef SWIG 1476 typedef struct 1477 { 1478 void *baton; 1479 void (*initialize)(void *baton, Process *process); 1480 void (*process_state_changed) (void *baton, Process *process, lldb::StateType state); 1481 } Notifications; 1482 1483 class ProcessEventData : 1484 public EventData 1485 { 1486 friend class Process; 1487 1488 public: 1489 ProcessEventData (); 1490 ProcessEventData (const lldb::ProcessSP &process, lldb::StateType state); 1491 1492 virtual ~ProcessEventData(); 1493 1494 static const ConstString & 1495 GetFlavorString (); 1496 1497 virtual const ConstString & 1498 GetFlavor () const; 1499 1500 const lldb::ProcessSP & 1501 GetProcessSP() const 1502 { 1503 return m_process_sp; 1504 } 1505 lldb::StateType 1506 GetState() const 1507 { 1508 return m_state; 1509 } 1510 bool 1511 GetRestarted () const 1512 { 1513 return m_restarted; 1514 } 1515 1516 size_t 1517 GetNumRestartedReasons () 1518 { 1519 return m_restarted_reasons.size(); 1520 } 1521 1522 const char * 1523 GetRestartedReasonAtIndex(size_t idx) 1524 { 1525 if (idx > m_restarted_reasons.size()) 1526 return NULL; 1527 else 1528 return m_restarted_reasons[idx].c_str(); 1529 } 1530 1531 bool 1532 GetInterrupted () const 1533 { 1534 return m_interrupted; 1535 } 1536 1537 virtual void 1538 Dump (Stream *s) const; 1539 1540 virtual void 1541 DoOnRemoval (Event *event_ptr); 1542 1543 static const Process::ProcessEventData * 1544 GetEventDataFromEvent (const Event *event_ptr); 1545 1546 static lldb::ProcessSP 1547 GetProcessFromEvent (const Event *event_ptr); 1548 1549 static lldb::StateType 1550 GetStateFromEvent (const Event *event_ptr); 1551 1552 static bool 1553 GetRestartedFromEvent (const Event *event_ptr); 1554 1555 static size_t 1556 GetNumRestartedReasons(const Event *event_ptr); 1557 1558 static const char * 1559 GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx); 1560 1561 static void 1562 AddRestartedReason (Event *event_ptr, const char *reason); 1563 1564 static void 1565 SetRestartedInEvent (Event *event_ptr, bool new_value); 1566 1567 static bool 1568 GetInterruptedFromEvent (const Event *event_ptr); 1569 1570 static void 1571 SetInterruptedInEvent (Event *event_ptr, bool new_value); 1572 1573 static bool 1574 SetUpdateStateOnRemoval (Event *event_ptr); 1575 1576 private: 1577 1578 void 1579 SetUpdateStateOnRemoval() 1580 { 1581 m_update_state++; 1582 } 1583 void 1584 SetRestarted (bool new_value) 1585 { 1586 m_restarted = new_value; 1587 } 1588 void 1589 SetInterrupted (bool new_value) 1590 { 1591 m_interrupted = new_value; 1592 } 1593 void 1594 AddRestartedReason (const char *reason) 1595 { 1596 m_restarted_reasons.push_back(reason); 1597 } 1598 1599 lldb::ProcessSP m_process_sp; 1600 lldb::StateType m_state; 1601 std::vector<std::string> m_restarted_reasons; 1602 bool m_restarted; // For "eStateStopped" events, this is true if the target was automatically restarted. 1603 int m_update_state; 1604 bool m_interrupted; 1605 DISALLOW_COPY_AND_ASSIGN (ProcessEventData); 1606 1607 }; 1608 1609#endif 1610 1611 static void 1612 SettingsInitialize (); 1613 1614 static void 1615 SettingsTerminate (); 1616 1617 static const ProcessPropertiesSP & 1618 GetGlobalProperties(); 1619 1620 //------------------------------------------------------------------ 1621 /// Construct with a shared pointer to a target, and the Process listener. 1622 //------------------------------------------------------------------ 1623 Process(Target &target, Listener &listener); 1624 1625 //------------------------------------------------------------------ 1626 /// Destructor. 1627 /// 1628 /// The destructor is virtual since this class is designed to be 1629 /// inherited from by the plug-in instance. 1630 //------------------------------------------------------------------ 1631 virtual 1632 ~Process(); 1633 1634 //------------------------------------------------------------------ 1635 /// Find a Process plug-in that can debug \a module using the 1636 /// currently selected architecture. 1637 /// 1638 /// Scans all loaded plug-in interfaces that implement versions of 1639 /// the Process plug-in interface and returns the first instance 1640 /// that can debug the file. 1641 /// 1642 /// @param[in] module_sp 1643 /// The module shared pointer that this process will debug. 1644 /// 1645 /// @param[in] plugin_name 1646 /// If NULL, select the best plug-in for the binary. If non-NULL 1647 /// then look for a plugin whose PluginInfo's name matches 1648 /// this string. 1649 /// 1650 /// @see Process::CanDebug () 1651 //------------------------------------------------------------------ 1652 static lldb::ProcessSP 1653 FindPlugin (Target &target, 1654 const char *plugin_name, 1655 Listener &listener, 1656 const FileSpec *crash_file_path); 1657 1658 1659 1660 //------------------------------------------------------------------ 1661 /// Static function that can be used with the \b host function 1662 /// Host::StartMonitoringChildProcess (). 1663 /// 1664 /// This function can be used by lldb_private::Process subclasses 1665 /// when they want to watch for a local process and have its exit 1666 /// status automatically set when the host child process exits. 1667 /// Subclasses should call Host::StartMonitoringChildProcess () 1668 /// with: 1669 /// callback = Process::SetHostProcessExitStatus 1670 /// callback_baton = NULL 1671 /// pid = Process::GetID() 1672 /// monitor_signals = false 1673 //------------------------------------------------------------------ 1674 static bool 1675 SetProcessExitStatus (void *callback_baton, // The callback baton which should be set to NULL 1676 lldb::pid_t pid, // The process ID we want to monitor 1677 bool exited, 1678 int signo, // Zero for no signal 1679 int status); // Exit value of process if signal is zero 1680 1681 lldb::ByteOrder 1682 GetByteOrder () const; 1683 1684 uint32_t 1685 GetAddressByteSize () const; 1686 1687 uint32_t 1688 GetUniqueID() const 1689 { 1690 return m_process_unique_id; 1691 } 1692 //------------------------------------------------------------------ 1693 /// Check if a plug-in instance can debug the file in \a module. 1694 /// 1695 /// Each plug-in is given a chance to say whether it can debug 1696 /// the file in \a module. If the Process plug-in instance can 1697 /// debug a file on the current system, it should return \b true. 1698 /// 1699 /// @return 1700 /// Returns \b true if this Process plug-in instance can 1701 /// debug the executable, \b false otherwise. 1702 //------------------------------------------------------------------ 1703 virtual bool 1704 CanDebug (Target &target, 1705 bool plugin_specified_by_name) = 0; 1706 1707 1708 //------------------------------------------------------------------ 1709 /// This object is about to be destroyed, do any necessary cleanup. 1710 /// 1711 /// Subclasses that override this method should always call this 1712 /// superclass method. 1713 //------------------------------------------------------------------ 1714 virtual void 1715 Finalize(); 1716 1717 1718 //------------------------------------------------------------------ 1719 /// Return whether this object is valid (i.e. has not been finalized.) 1720 /// 1721 /// @return 1722 /// Returns \b true if this Process has not been finalized 1723 /// and \b false otherwise. 1724 //------------------------------------------------------------------ 1725 bool 1726 IsValid() const 1727 { 1728 return !m_finalize_called; 1729 } 1730 1731 //------------------------------------------------------------------ 1732 /// Return a multi-word command object that can be used to expose 1733 /// plug-in specific commands. 1734 /// 1735 /// This object will be used to resolve plug-in commands and can be 1736 /// triggered by a call to: 1737 /// 1738 /// (lldb) process commmand <args> 1739 /// 1740 /// @return 1741 /// A CommandObject which can be one of the concrete subclasses 1742 /// of CommandObject like CommandObjectRaw, CommandObjectParsed, 1743 /// or CommandObjectMultiword. 1744 //------------------------------------------------------------------ 1745 virtual CommandObject * 1746 GetPluginCommandObject() 1747 { 1748 return NULL; 1749 } 1750 1751 //------------------------------------------------------------------ 1752 /// Launch a new process. 1753 /// 1754 /// Launch a new process by spawning a new process using the 1755 /// target object's executable module's file as the file to launch. 1756 /// Arguments are given in \a argv, and the environment variables 1757 /// are in \a envp. Standard input and output files can be 1758 /// optionally re-directed to \a stdin_path, \a stdout_path, and 1759 /// \a stderr_path. 1760 /// 1761 /// This function is not meant to be overridden by Process 1762 /// subclasses. It will first call Process::WillLaunch (Module *) 1763 /// and if that returns \b true, Process::DoLaunch (Module*, 1764 /// char const *[],char const *[],const char *,const char *, 1765 /// const char *) will be called to actually do the launching. If 1766 /// DoLaunch returns \b true, then Process::DidLaunch() will be 1767 /// called. 1768 /// 1769 /// @param[in] argv 1770 /// The argument array. 1771 /// 1772 /// @param[in] envp 1773 /// The environment array. 1774 /// 1775 /// @param[in] launch_flags 1776 /// Flags to modify the launch (@see lldb::LaunchFlags) 1777 /// 1778 /// @param[in] stdin_path 1779 /// The path to use when re-directing the STDIN of the new 1780 /// process. If all stdXX_path arguments are NULL, a pseudo 1781 /// terminal will be used. 1782 /// 1783 /// @param[in] stdout_path 1784 /// The path to use when re-directing the STDOUT of the new 1785 /// process. If all stdXX_path arguments are NULL, a pseudo 1786 /// terminal will be used. 1787 /// 1788 /// @param[in] stderr_path 1789 /// The path to use when re-directing the STDERR of the new 1790 /// process. If all stdXX_path arguments are NULL, a pseudo 1791 /// terminal will be used. 1792 /// 1793 /// @param[in] working_directory 1794 /// The working directory to have the child process run in 1795 /// 1796 /// @return 1797 /// An error object. Call GetID() to get the process ID if 1798 /// the error object is success. 1799 //------------------------------------------------------------------ 1800 virtual Error 1801 Launch (ProcessLaunchInfo &launch_info); 1802 1803 virtual Error 1804 LoadCore (); 1805 1806 virtual Error 1807 DoLoadCore () 1808 { 1809 Error error; 1810 error.SetErrorStringWithFormat("error: %s does not support loading core files.", GetPluginName().GetCString()); 1811 return error; 1812 } 1813 1814 //------------------------------------------------------------------ 1815 /// Get the dynamic loader plug-in for this process. 1816 /// 1817 /// The default action is to let the DynamicLoader plug-ins check 1818 /// the main executable and the DynamicLoader will select itself 1819 /// automatically. Subclasses can override this if inspecting the 1820 /// executable is not desired, or if Process subclasses can only 1821 /// use a specific DynamicLoader plug-in. 1822 //------------------------------------------------------------------ 1823 virtual DynamicLoader * 1824 GetDynamicLoader (); 1825 1826 //------------------------------------------------------------------ 1827 /// Get the system runtime plug-in for this process. 1828 /// 1829 /// @return 1830 /// Returns a pointer to the SystemRuntime plugin for this Process 1831 /// if one is available. Else returns NULL. 1832 //------------------------------------------------------------------ 1833 virtual SystemRuntime * 1834 GetSystemRuntime (); 1835 1836 //------------------------------------------------------------------ 1837 /// Attach to an existing process using the process attach info. 1838 /// 1839 /// This function is not meant to be overridden by Process 1840 /// subclasses. It will first call WillAttach (lldb::pid_t) 1841 /// or WillAttach (const char *), and if that returns \b 1842 /// true, DoAttach (lldb::pid_t) or DoAttach (const char *) will 1843 /// be called to actually do the attach. If DoAttach returns \b 1844 /// true, then Process::DidAttach() will be called. 1845 /// 1846 /// @param[in] pid 1847 /// The process ID that we should attempt to attach to. 1848 /// 1849 /// @return 1850 /// Returns \a pid if attaching was successful, or 1851 /// LLDB_INVALID_PROCESS_ID if attaching fails. 1852 //------------------------------------------------------------------ 1853 virtual Error 1854 Attach (ProcessAttachInfo &attach_info); 1855 1856 //------------------------------------------------------------------ 1857 /// Attach to a remote system via a URL 1858 /// 1859 /// @param[in] strm 1860 /// A stream where output intended for the user 1861 /// (if the driver has a way to display that) generated during 1862 /// the connection. This may be NULL if no output is needed.A 1863 /// 1864 /// @param[in] remote_url 1865 /// The URL format that we are connecting to. 1866 /// 1867 /// @return 1868 /// Returns an error object. 1869 //------------------------------------------------------------------ 1870 virtual Error 1871 ConnectRemote (Stream *strm, const char *remote_url); 1872 1873 bool 1874 GetShouldDetach () const 1875 { 1876 return m_should_detach; 1877 } 1878 1879 void 1880 SetShouldDetach (bool b) 1881 { 1882 m_should_detach = b; 1883 } 1884 1885 //------------------------------------------------------------------ 1886 /// Get the image information address for the current process. 1887 /// 1888 /// Some runtimes have system functions that can help dynamic 1889 /// loaders locate the dynamic loader information needed to observe 1890 /// shared libraries being loaded or unloaded. This function is 1891 /// in the Process interface (as opposed to the DynamicLoader 1892 /// interface) to ensure that remote debugging can take advantage of 1893 /// this functionality. 1894 /// 1895 /// @return 1896 /// The address of the dynamic loader information, or 1897 /// LLDB_INVALID_ADDRESS if this is not supported by this 1898 /// interface. 1899 //------------------------------------------------------------------ 1900 virtual lldb::addr_t 1901 GetImageInfoAddress (); 1902 1903 //------------------------------------------------------------------ 1904 /// Load a shared library into this process. 1905 /// 1906 /// Try and load a shared library into the current process. This 1907 /// call might fail in the dynamic loader plug-in says it isn't safe 1908 /// to try and load shared libraries at the moment. 1909 /// 1910 /// @param[in] image_spec 1911 /// The image file spec that points to the shared library that 1912 /// you want to load. 1913 /// 1914 /// @param[out] error 1915 /// An error object that gets filled in with any errors that 1916 /// might occur when trying to load the shared library. 1917 /// 1918 /// @return 1919 /// A token that represents the shared library that can be 1920 /// later used to unload the shared library. A value of 1921 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 1922 /// library can't be opened. 1923 //------------------------------------------------------------------ 1924 virtual uint32_t 1925 LoadImage (const FileSpec &image_spec, Error &error); 1926 1927 virtual Error 1928 UnloadImage (uint32_t image_token); 1929 1930 //------------------------------------------------------------------ 1931 /// Register for process and thread notifications. 1932 /// 1933 /// Clients can register nofication callbacks by filling out a 1934 /// Process::Notifications structure and calling this function. 1935 /// 1936 /// @param[in] callbacks 1937 /// A structure that contains the notification baton and 1938 /// callback functions. 1939 /// 1940 /// @see Process::Notifications 1941 //------------------------------------------------------------------ 1942#ifndef SWIG 1943 void 1944 RegisterNotificationCallbacks (const Process::Notifications& callbacks); 1945#endif 1946 //------------------------------------------------------------------ 1947 /// Unregister for process and thread notifications. 1948 /// 1949 /// Clients can unregister nofication callbacks by passing a copy of 1950 /// the original baton and callbacks in \a callbacks. 1951 /// 1952 /// @param[in] callbacks 1953 /// A structure that contains the notification baton and 1954 /// callback functions. 1955 /// 1956 /// @return 1957 /// Returns \b true if the notification callbacks were 1958 /// successfully removed from the process, \b false otherwise. 1959 /// 1960 /// @see Process::Notifications 1961 //------------------------------------------------------------------ 1962#ifndef SWIG 1963 bool 1964 UnregisterNotificationCallbacks (const Process::Notifications& callbacks); 1965#endif 1966 //================================================================== 1967 // Built in Process Control functions 1968 //================================================================== 1969 //------------------------------------------------------------------ 1970 /// Resumes all of a process's threads as configured using the 1971 /// Thread run control functions. 1972 /// 1973 /// Threads for a process should be updated with one of the run 1974 /// control actions (resume, step, or suspend) that they should take 1975 /// when the process is resumed. If no run control action is given 1976 /// to a thread it will be resumed by default. 1977 /// 1978 /// This function is not meant to be overridden by Process 1979 /// subclasses. This function will take care of disabling any 1980 /// breakpoints that threads may be stopped at, single stepping, and 1981 /// re-enabling breakpoints, and enabling the basic flow control 1982 /// that the plug-in instances need not worry about. 1983 /// 1984 /// N.B. This function also sets the Write side of the Run Lock, 1985 /// which is unset when the corresponding stop event is pulled off 1986 /// the Public Event Queue. If you need to resume the process without 1987 /// setting the Run Lock, use PrivateResume (though you should only do 1988 /// that from inside the Process class. 1989 /// 1990 /// @return 1991 /// Returns an error object. 1992 /// 1993 /// @see Thread:Resume() 1994 /// @see Thread:Step() 1995 /// @see Thread:Suspend() 1996 //------------------------------------------------------------------ 1997 Error 1998 Resume(); 1999 2000 //------------------------------------------------------------------ 2001 /// Halts a running process. 2002 /// 2003 /// This function is not meant to be overridden by Process 2004 /// subclasses. 2005 /// If the process is successfully halted, a eStateStopped 2006 /// process event with GetInterrupted will be broadcast. If false, we will 2007 /// halt the process with no events generated by the halt. 2008 /// 2009 /// @param[in] clear_thread_plans 2010 /// If true, when the process stops, clear all thread plans. 2011 /// 2012 /// @return 2013 /// Returns an error object. If the error is empty, the process is halted. 2014 /// otherwise the halt has failed. 2015 //------------------------------------------------------------------ 2016 Error 2017 Halt (bool clear_thread_plans = false); 2018 2019 //------------------------------------------------------------------ 2020 /// Detaches from a running or stopped process. 2021 /// 2022 /// This function is not meant to be overridden by Process 2023 /// subclasses. 2024 /// 2025 /// @param[in] keep_stopped 2026 /// If true, don't resume the process on detach. 2027 /// 2028 /// @return 2029 /// Returns an error object. 2030 //------------------------------------------------------------------ 2031 Error 2032 Detach (bool keep_stopped); 2033 2034 //------------------------------------------------------------------ 2035 /// Kills the process and shuts down all threads that were spawned 2036 /// to track and monitor the process. 2037 /// 2038 /// This function is not meant to be overridden by Process 2039 /// subclasses. 2040 /// 2041 /// @return 2042 /// Returns an error object. 2043 //------------------------------------------------------------------ 2044 Error 2045 Destroy(); 2046 2047 //------------------------------------------------------------------ 2048 /// Sends a process a UNIX signal \a signal. 2049 /// 2050 /// This function is not meant to be overridden by Process 2051 /// subclasses. 2052 /// 2053 /// @return 2054 /// Returns an error object. 2055 //------------------------------------------------------------------ 2056 Error 2057 Signal (int signal); 2058 2059 virtual UnixSignals & 2060 GetUnixSignals () 2061 { 2062 return m_unix_signals; 2063 } 2064 2065 //================================================================== 2066 // Plug-in Process Control Overrides 2067 //================================================================== 2068 2069 //------------------------------------------------------------------ 2070 /// Called before attaching to a process. 2071 /// 2072 /// Allow Process plug-ins to execute some code before attaching a 2073 /// process. 2074 /// 2075 /// @return 2076 /// Returns an error object. 2077 //------------------------------------------------------------------ 2078 virtual Error 2079 WillAttachToProcessWithID (lldb::pid_t pid) 2080 { 2081 return Error(); 2082 } 2083 2084 //------------------------------------------------------------------ 2085 /// Called before attaching to a process. 2086 /// 2087 /// Allow Process plug-ins to execute some code before attaching a 2088 /// process. 2089 /// 2090 /// @return 2091 /// Returns an error object. 2092 //------------------------------------------------------------------ 2093 virtual Error 2094 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) 2095 { 2096 return Error(); 2097 } 2098 2099 //------------------------------------------------------------------ 2100 /// Attach to a remote system via a URL 2101 /// 2102 /// @param[in] strm 2103 /// A stream where output intended for the user 2104 /// (if the driver has a way to display that) generated during 2105 /// the connection. This may be NULL if no output is needed.A 2106 /// 2107 /// @param[in] remote_url 2108 /// The URL format that we are connecting to. 2109 /// 2110 /// @return 2111 /// Returns an error object. 2112 //------------------------------------------------------------------ 2113 virtual Error 2114 DoConnectRemote (Stream *strm, const char *remote_url) 2115 { 2116 Error error; 2117 error.SetErrorString ("remote connections are not supported"); 2118 return error; 2119 } 2120 2121 //------------------------------------------------------------------ 2122 /// Attach to an existing process using a process ID. 2123 /// 2124 /// @param[in] pid 2125 /// The process ID that we should attempt to attach to. 2126 /// 2127 /// @return 2128 /// Returns \a pid if attaching was successful, or 2129 /// LLDB_INVALID_PROCESS_ID if attaching fails. 2130 //------------------------------------------------------------------ 2131 virtual Error 2132 DoAttachToProcessWithID (lldb::pid_t pid) 2133 { 2134 Error error; 2135 error.SetErrorStringWithFormat("error: %s does not support attaching to a process by pid", GetPluginName().GetCString()); 2136 return error; 2137 } 2138 2139 //------------------------------------------------------------------ 2140 /// Attach to an existing process using a process ID. 2141 /// 2142 /// @param[in] pid 2143 /// The process ID that we should attempt to attach to. 2144 /// 2145 /// @param[in] attach_info 2146 /// Information on how to do the attach. For example, GetUserID() 2147 /// will return the uid to attach as. 2148 /// 2149 /// @return 2150 /// Returns \a pid if attaching was successful, or 2151 /// LLDB_INVALID_PROCESS_ID if attaching fails. 2152 /// hanming : need flag 2153 //------------------------------------------------------------------ 2154 virtual Error 2155 DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info) 2156 { 2157 Error error; 2158 error.SetErrorStringWithFormat("error: %s does not support attaching to a process by pid", GetPluginName().GetCString()); 2159 return error; 2160 } 2161 2162 //------------------------------------------------------------------ 2163 /// Attach to an existing process using a partial process name. 2164 /// 2165 /// @param[in] process_name 2166 /// The name of the process to attach to. 2167 /// 2168 /// @param[in] attach_info 2169 /// Information on how to do the attach. For example, GetUserID() 2170 /// will return the uid to attach as. 2171 /// 2172 /// @return 2173 /// Returns an error object. 2174 //------------------------------------------------------------------ 2175 virtual Error 2176 DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) 2177 { 2178 Error error; 2179 error.SetErrorString("attach by name is not supported"); 2180 return error; 2181 } 2182 2183 //------------------------------------------------------------------ 2184 /// Called after attaching a process. 2185 /// 2186 /// Allow Process plug-ins to execute some code after attaching to 2187 /// a process. 2188 //------------------------------------------------------------------ 2189 virtual void 2190 DidAttach () {} 2191 2192 2193 //------------------------------------------------------------------ 2194 /// Called after a process re-execs itself. 2195 /// 2196 /// Allow Process plug-ins to execute some code after a process has 2197 /// exec'ed itself. Subclasses typically should override DoDidExec() 2198 /// as the lldb_private::Process class needs to remove its dynamic 2199 /// loader, runtime, ABI and other plug-ins, as well as unload all 2200 /// shared libraries. 2201 //------------------------------------------------------------------ 2202 virtual void 2203 DidExec (); 2204 2205 //------------------------------------------------------------------ 2206 /// Subclasses of Process should implement this function if they 2207 /// need to do anything after a process exec's itself. 2208 //------------------------------------------------------------------ 2209 virtual void 2210 DoDidExec () 2211 { 2212 } 2213 2214 //------------------------------------------------------------------ 2215 /// Called before launching to a process. 2216 /// 2217 /// Allow Process plug-ins to execute some code before launching a 2218 /// process. 2219 /// 2220 /// @return 2221 /// Returns an error object. 2222 //------------------------------------------------------------------ 2223 virtual Error 2224 WillLaunch (Module* module) 2225 { 2226 return Error(); 2227 } 2228 2229 //------------------------------------------------------------------ 2230 /// Launch a new process. 2231 /// 2232 /// Launch a new process by spawning a new process using \a module's 2233 /// file as the file to launch. Arguments are given in \a argv, 2234 /// and the environment variables are in \a envp. Standard input 2235 /// and output files can be optionally re-directed to \a stdin_path, 2236 /// \a stdout_path, and \a stderr_path. 2237 /// 2238 /// @param[in] module 2239 /// The module from which to extract the file specification and 2240 /// launch. 2241 /// 2242 /// @param[in] argv 2243 /// The argument array. 2244 /// 2245 /// @param[in] envp 2246 /// The environment array. 2247 /// 2248 /// @param[in] launch_flags 2249 /// Flags to modify the launch (@see lldb::LaunchFlags) 2250 /// 2251 /// @param[in] stdin_path 2252 /// The path to use when re-directing the STDIN of the new 2253 /// process. If all stdXX_path arguments are NULL, a pseudo 2254 /// terminal will be used. 2255 /// 2256 /// @param[in] stdout_path 2257 /// The path to use when re-directing the STDOUT of the new 2258 /// process. If all stdXX_path arguments are NULL, a pseudo 2259 /// terminal will be used. 2260 /// 2261 /// @param[in] stderr_path 2262 /// The path to use when re-directing the STDERR of the new 2263 /// process. If all stdXX_path arguments are NULL, a pseudo 2264 /// terminal will be used. 2265 /// 2266 /// @param[in] working_directory 2267 /// The working directory to have the child process run in 2268 /// 2269 /// @return 2270 /// A new valid process ID, or LLDB_INVALID_PROCESS_ID if 2271 /// launching fails. 2272 //------------------------------------------------------------------ 2273 virtual Error 2274 DoLaunch (Module *exe_module, 2275 ProcessLaunchInfo &launch_info) 2276 { 2277 Error error; 2278 error.SetErrorStringWithFormat("error: %s does not support launching processes", GetPluginName().GetCString()); 2279 return error; 2280 } 2281 2282 2283 //------------------------------------------------------------------ 2284 /// Called after launching a process. 2285 /// 2286 /// Allow Process plug-ins to execute some code after launching 2287 /// a process. 2288 //------------------------------------------------------------------ 2289 virtual void 2290 DidLaunch () {} 2291 2292 2293 2294 //------------------------------------------------------------------ 2295 /// Called before resuming to a process. 2296 /// 2297 /// Allow Process plug-ins to execute some code before resuming a 2298 /// process. 2299 /// 2300 /// @return 2301 /// Returns an error object. 2302 //------------------------------------------------------------------ 2303 virtual Error 2304 WillResume () { return Error(); } 2305 2306 //------------------------------------------------------------------ 2307 /// Resumes all of a process's threads as configured using the 2308 /// Thread run control functions. 2309 /// 2310 /// Threads for a process should be updated with one of the run 2311 /// control actions (resume, step, or suspend) that they should take 2312 /// when the process is resumed. If no run control action is given 2313 /// to a thread it will be resumed by default. 2314 /// 2315 /// @return 2316 /// Returns \b true if the process successfully resumes using 2317 /// the thread run control actions, \b false otherwise. 2318 /// 2319 /// @see Thread:Resume() 2320 /// @see Thread:Step() 2321 /// @see Thread:Suspend() 2322 //------------------------------------------------------------------ 2323 virtual Error 2324 DoResume () 2325 { 2326 Error error; 2327 error.SetErrorStringWithFormat("error: %s does not support resuming processes", GetPluginName().GetCString()); 2328 return error; 2329 } 2330 2331 2332 //------------------------------------------------------------------ 2333 /// Called after resuming a process. 2334 /// 2335 /// Allow Process plug-ins to execute some code after resuming 2336 /// a process. 2337 //------------------------------------------------------------------ 2338 virtual void 2339 DidResume () {} 2340 2341 2342 //------------------------------------------------------------------ 2343 /// Called before halting to a process. 2344 /// 2345 /// Allow Process plug-ins to execute some code before halting a 2346 /// process. 2347 /// 2348 /// @return 2349 /// Returns an error object. 2350 //------------------------------------------------------------------ 2351 virtual Error 2352 WillHalt () { return Error(); } 2353 2354 //------------------------------------------------------------------ 2355 /// Halts a running process. 2356 /// 2357 /// DoHalt must produce one and only one stop StateChanged event if it actually 2358 /// stops the process. If the stop happens through some natural event (for 2359 /// instance a SIGSTOP), then forwarding that event will do. Otherwise, you must 2360 /// generate the event manually. Note also, the private event thread is stopped when 2361 /// DoHalt is run to prevent the events generated while halting to trigger 2362 /// other state changes before the halt is complete. 2363 /// 2364 /// @param[out] caused_stop 2365 /// If true, then this Halt caused the stop, otherwise, the 2366 /// process was already stopped. 2367 /// 2368 /// @return 2369 /// Returns \b true if the process successfully halts, \b false 2370 /// otherwise. 2371 //------------------------------------------------------------------ 2372 virtual Error 2373 DoHalt (bool &caused_stop) 2374 { 2375 Error error; 2376 error.SetErrorStringWithFormat("error: %s does not support halting processes", GetPluginName().GetCString()); 2377 return error; 2378 } 2379 2380 2381 //------------------------------------------------------------------ 2382 /// Called after halting a process. 2383 /// 2384 /// Allow Process plug-ins to execute some code after halting 2385 /// a process. 2386 //------------------------------------------------------------------ 2387 virtual void 2388 DidHalt () {} 2389 2390 //------------------------------------------------------------------ 2391 /// Called before detaching from a process. 2392 /// 2393 /// Allow Process plug-ins to execute some code before detaching 2394 /// from a process. 2395 /// 2396 /// @return 2397 /// Returns an error object. 2398 //------------------------------------------------------------------ 2399 virtual Error 2400 WillDetach () 2401 { 2402 return Error(); 2403 } 2404 2405 //------------------------------------------------------------------ 2406 /// Detaches from a running or stopped process. 2407 /// 2408 /// @return 2409 /// Returns \b true if the process successfully detaches, \b 2410 /// false otherwise. 2411 //------------------------------------------------------------------ 2412 virtual Error 2413 DoDetach (bool keep_stopped) 2414 { 2415 Error error; 2416 error.SetErrorStringWithFormat("error: %s does not support detaching from processes", GetPluginName().GetCString()); 2417 return error; 2418 } 2419 2420 2421 //------------------------------------------------------------------ 2422 /// Called after detaching from a process. 2423 /// 2424 /// Allow Process plug-ins to execute some code after detaching 2425 /// from a process. 2426 //------------------------------------------------------------------ 2427 virtual void 2428 DidDetach () {} 2429 2430 virtual bool 2431 DetachRequiresHalt() { return false; } 2432 2433 //------------------------------------------------------------------ 2434 /// Called before sending a signal to a process. 2435 /// 2436 /// Allow Process plug-ins to execute some code before sending a 2437 /// signal to a process. 2438 /// 2439 /// @return 2440 /// Returns no error if it is safe to proceed with a call to 2441 /// Process::DoSignal(int), otherwise an error describing what 2442 /// prevents the signal from being sent. 2443 //------------------------------------------------------------------ 2444 virtual Error 2445 WillSignal () { return Error(); } 2446 2447 //------------------------------------------------------------------ 2448 /// Sends a process a UNIX signal \a signal. 2449 /// 2450 /// @return 2451 /// Returns an error object. 2452 //------------------------------------------------------------------ 2453 virtual Error 2454 DoSignal (int signal) 2455 { 2456 Error error; 2457 error.SetErrorStringWithFormat("error: %s does not support senging signals to processes", GetPluginName().GetCString()); 2458 return error; 2459 } 2460 2461 virtual Error 2462 WillDestroy () { return Error(); } 2463 2464 virtual Error 2465 DoDestroy () = 0; 2466 2467 virtual void 2468 DidDestroy () { } 2469 2470 virtual bool 2471 DestroyRequiresHalt() { return true; } 2472 2473 2474 //------------------------------------------------------------------ 2475 /// Called after sending a signal to a process. 2476 /// 2477 /// Allow Process plug-ins to execute some code after sending a 2478 /// signal to a process. 2479 //------------------------------------------------------------------ 2480 virtual void 2481 DidSignal () {} 2482 2483 //------------------------------------------------------------------ 2484 /// Currently called as part of ShouldStop. 2485 /// FIXME: Should really happen when the target stops before the 2486 /// event is taken from the queue... 2487 /// 2488 /// This callback is called as the event 2489 /// is about to be queued up to allow Process plug-ins to execute 2490 /// some code prior to clients being notified that a process was 2491 /// stopped. Common operations include updating the thread list, 2492 /// invalidating any thread state (registers, stack, etc) prior to 2493 /// letting the notification go out. 2494 /// 2495 //------------------------------------------------------------------ 2496 virtual void 2497 RefreshStateAfterStop () = 0; 2498 2499 //------------------------------------------------------------------ 2500 /// Get the target object pointer for this module. 2501 /// 2502 /// @return 2503 /// A Target object pointer to the target that owns this 2504 /// module. 2505 //------------------------------------------------------------------ 2506 Target & 2507 GetTarget () 2508 { 2509 return m_target; 2510 } 2511 2512 //------------------------------------------------------------------ 2513 /// Get the const target object pointer for this module. 2514 /// 2515 /// @return 2516 /// A const Target object pointer to the target that owns this 2517 /// module. 2518 //------------------------------------------------------------------ 2519 const Target & 2520 GetTarget () const 2521 { 2522 return m_target; 2523 } 2524 2525 //------------------------------------------------------------------ 2526 /// Flush all data in the process. 2527 /// 2528 /// Flush the memory caches, all threads, and any other cached data 2529 /// in the process. 2530 /// 2531 /// This function can be called after a world changing event like 2532 /// adding a new symbol file, or after the process makes a large 2533 /// context switch (from boot ROM to booted into an OS). 2534 //------------------------------------------------------------------ 2535 void 2536 Flush (); 2537 2538 //------------------------------------------------------------------ 2539 /// Get accessor for the current process state. 2540 /// 2541 /// @return 2542 /// The current state of the process. 2543 /// 2544 /// @see lldb::StateType 2545 //------------------------------------------------------------------ 2546 lldb::StateType 2547 GetState (); 2548 2549 ExecutionResults 2550 RunThreadPlan (ExecutionContext &exe_ctx, 2551 lldb::ThreadPlanSP &thread_plan_sp, 2552 const EvaluateExpressionOptions &options, 2553 Stream &errors); 2554 2555 static const char * 2556 ExecutionResultAsCString (ExecutionResults result); 2557 2558 void 2559 GetStatus (Stream &ostrm); 2560 2561 size_t 2562 GetThreadStatus (Stream &ostrm, 2563 bool only_threads_with_stop_reason, 2564 uint32_t start_frame, 2565 uint32_t num_frames, 2566 uint32_t num_frames_with_source); 2567 2568 void 2569 SendAsyncInterrupt (); 2570 2571protected: 2572 2573 void 2574 SetState (lldb::EventSP &event_sp); 2575 2576 lldb::StateType 2577 GetPrivateState (); 2578 2579 //------------------------------------------------------------------ 2580 /// The "private" side of resuming a process. This doesn't alter the 2581 /// state of m_run_lock, but just causes the process to resume. 2582 /// 2583 /// @return 2584 /// An Error object describing the success or failure of the resume. 2585 //------------------------------------------------------------------ 2586 Error 2587 PrivateResume (); 2588 2589 //------------------------------------------------------------------ 2590 // Called internally 2591 //------------------------------------------------------------------ 2592 void 2593 CompleteAttach (); 2594 2595public: 2596 //------------------------------------------------------------------ 2597 /// Get the exit status for a process. 2598 /// 2599 /// @return 2600 /// The process's return code, or -1 if the current process 2601 /// state is not eStateExited. 2602 //------------------------------------------------------------------ 2603 int 2604 GetExitStatus (); 2605 2606 //------------------------------------------------------------------ 2607 /// Get a textual description of what the process exited. 2608 /// 2609 /// @return 2610 /// The textual description of why the process exited, or NULL 2611 /// if there is no description available. 2612 //------------------------------------------------------------------ 2613 const char * 2614 GetExitDescription (); 2615 2616 2617 virtual void 2618 DidExit () 2619 { 2620 } 2621 2622 //------------------------------------------------------------------ 2623 /// Get the Modification ID of the process. 2624 /// 2625 /// @return 2626 /// The modification ID of the process. 2627 //------------------------------------------------------------------ 2628 ProcessModID 2629 GetModID () const 2630 { 2631 return m_mod_id; 2632 } 2633 2634 const ProcessModID & 2635 GetModIDRef () const 2636 { 2637 return m_mod_id; 2638 } 2639 2640 uint32_t 2641 GetStopID () const 2642 { 2643 return m_mod_id.GetStopID(); 2644 } 2645 2646 uint32_t 2647 GetResumeID () const 2648 { 2649 return m_mod_id.GetResumeID(); 2650 } 2651 2652 uint32_t 2653 GetLastUserExpressionResumeID () const 2654 { 2655 return m_mod_id.GetLastUserExpressionResumeID(); 2656 } 2657 2658 uint32_t 2659 GetLastNaturalStopID() 2660 { 2661 return m_mod_id.GetLastNaturalStopID(); 2662 } 2663 2664 //------------------------------------------------------------------ 2665 /// Set accessor for the process exit status (return code). 2666 /// 2667 /// Sometimes a child exits and the exit can be detected by global 2668 /// functions (signal handler for SIGCHLD for example). This 2669 /// accessor allows the exit status to be set from an external 2670 /// source. 2671 /// 2672 /// Setting this will cause a eStateExited event to be posted to 2673 /// the process event queue. 2674 /// 2675 /// @param[in] exit_status 2676 /// The value for the process's return code. 2677 /// 2678 /// @see lldb::StateType 2679 //------------------------------------------------------------------ 2680 virtual bool 2681 SetExitStatus (int exit_status, const char *cstr); 2682 2683 //------------------------------------------------------------------ 2684 /// Check if a process is still alive. 2685 /// 2686 /// @return 2687 /// Returns \b true if the process is still valid, \b false 2688 /// otherwise. 2689 //------------------------------------------------------------------ 2690 virtual bool 2691 IsAlive () = 0; 2692 2693 //------------------------------------------------------------------ 2694 /// Before lldb detaches from a process, it warns the user that they are about to lose their debug session. 2695 /// In some cases, this warning doesn't need to be emitted -- for instance, with core file debugging where 2696 /// the user can reconstruct the "state" by simply re-running the debugger on the core file. 2697 /// 2698 /// @return 2699 // true if the user should be warned about detaching from this process. 2700 //------------------------------------------------------------------ 2701 virtual bool 2702 WarnBeforeDetach () const 2703 { 2704 return true; 2705 } 2706 2707 //------------------------------------------------------------------ 2708 /// Actually do the reading of memory from a process. 2709 /// 2710 /// Subclasses must override this function and can return fewer 2711 /// bytes than requested when memory requests are too large. This 2712 /// class will break up the memory requests and keep advancing the 2713 /// arguments along as needed. 2714 /// 2715 /// @param[in] vm_addr 2716 /// A virtual load address that indicates where to start reading 2717 /// memory from. 2718 /// 2719 /// @param[in] size 2720 /// The number of bytes to read. 2721 /// 2722 /// @param[out] buf 2723 /// A byte buffer that is at least \a size bytes long that 2724 /// will receive the memory bytes. 2725 /// 2726 /// @return 2727 /// The number of bytes that were actually read into \a buf. 2728 //------------------------------------------------------------------ 2729 virtual size_t 2730 DoReadMemory (lldb::addr_t vm_addr, 2731 void *buf, 2732 size_t size, 2733 Error &error) = 0; 2734 2735 //------------------------------------------------------------------ 2736 /// Read of memory from a process. 2737 /// 2738 /// This function will read memory from the current process's 2739 /// address space and remove any traps that may have been inserted 2740 /// into the memory. 2741 /// 2742 /// This function is not meant to be overridden by Process 2743 /// subclasses, the subclasses should implement 2744 /// Process::DoReadMemory (lldb::addr_t, size_t, void *). 2745 /// 2746 /// @param[in] vm_addr 2747 /// A virtual load address that indicates where to start reading 2748 /// memory from. 2749 /// 2750 /// @param[out] buf 2751 /// A byte buffer that is at least \a size bytes long that 2752 /// will receive the memory bytes. 2753 /// 2754 /// @param[in] size 2755 /// The number of bytes to read. 2756 /// 2757 /// @return 2758 /// The number of bytes that were actually read into \a buf. If 2759 /// the returned number is greater than zero, yet less than \a 2760 /// size, then this function will get called again with \a 2761 /// vm_addr, \a buf, and \a size updated appropriately. Zero is 2762 /// returned to indicate an error. 2763 //------------------------------------------------------------------ 2764 virtual size_t 2765 ReadMemory (lldb::addr_t vm_addr, 2766 void *buf, 2767 size_t size, 2768 Error &error); 2769 2770 //------------------------------------------------------------------ 2771 /// Read a NULL terminated string from memory 2772 /// 2773 /// This function will read a cache page at a time until a NULL 2774 /// string terminator is found. It will stop reading if an aligned 2775 /// sequence of NULL termination \a type_width bytes is not found 2776 /// before reading \a cstr_max_len bytes. The results are always 2777 /// guaranteed to be NULL terminated, and that no more than 2778 /// (max_bytes - type_width) bytes will be read. 2779 /// 2780 /// @param[in] vm_addr 2781 /// The virtual load address to start the memory read. 2782 /// 2783 /// @param[in] str 2784 /// A character buffer containing at least max_bytes. 2785 /// 2786 /// @param[in] max_bytes 2787 /// The maximum number of bytes to read. 2788 /// 2789 /// @param[in] error 2790 /// The error status of the read operation. 2791 /// 2792 /// @param[in] type_width 2793 /// The size of the null terminator (1 to 4 bytes per 2794 /// character). Defaults to 1. 2795 /// 2796 /// @return 2797 /// The error status or the number of bytes prior to the null terminator. 2798 //------------------------------------------------------------------ 2799 size_t 2800 ReadStringFromMemory (lldb::addr_t vm_addr, 2801 char *str, 2802 size_t max_bytes, 2803 Error &error, 2804 size_t type_width = 1); 2805 2806 //------------------------------------------------------------------ 2807 /// Read a NULL terminated C string from memory 2808 /// 2809 /// This function will read a cache page at a time until the NULL 2810 /// C string terminator is found. It will stop reading if the NULL 2811 /// termination byte isn't found before reading \a cstr_max_len 2812 /// bytes, and the results are always guaranteed to be NULL 2813 /// terminated (at most cstr_max_len - 1 bytes will be read). 2814 //------------------------------------------------------------------ 2815 size_t 2816 ReadCStringFromMemory (lldb::addr_t vm_addr, 2817 char *cstr, 2818 size_t cstr_max_len, 2819 Error &error); 2820 2821 size_t 2822 ReadCStringFromMemory (lldb::addr_t vm_addr, 2823 std::string &out_str, 2824 Error &error); 2825 2826 size_t 2827 ReadMemoryFromInferior (lldb::addr_t vm_addr, 2828 void *buf, 2829 size_t size, 2830 Error &error); 2831 2832 //------------------------------------------------------------------ 2833 /// Reads an unsigned integer of the specified byte size from 2834 /// process memory. 2835 /// 2836 /// @param[in] load_addr 2837 /// A load address of the integer to read. 2838 /// 2839 /// @param[in] byte_size 2840 /// The size in byte of the integer to read. 2841 /// 2842 /// @param[in] fail_value 2843 /// The value to return if we fail to read an integer. 2844 /// 2845 /// @param[out] error 2846 /// An error that indicates the success or failure of this 2847 /// operation. If error indicates success (error.Success()), 2848 /// then the value returned can be trusted, otherwise zero 2849 /// will be returned. 2850 /// 2851 /// @return 2852 /// The unsigned integer that was read from the process memory 2853 /// space. If the integer was smaller than a uint64_t, any 2854 /// unused upper bytes will be zero filled. If the process 2855 /// byte order differs from the host byte order, the integer 2856 /// value will be appropriately byte swapped into host byte 2857 /// order. 2858 //------------------------------------------------------------------ 2859 uint64_t 2860 ReadUnsignedIntegerFromMemory (lldb::addr_t load_addr, 2861 size_t byte_size, 2862 uint64_t fail_value, 2863 Error &error); 2864 2865 lldb::addr_t 2866 ReadPointerFromMemory (lldb::addr_t vm_addr, 2867 Error &error); 2868 2869 bool 2870 WritePointerToMemory (lldb::addr_t vm_addr, 2871 lldb::addr_t ptr_value, 2872 Error &error); 2873 2874 //------------------------------------------------------------------ 2875 /// Actually do the writing of memory to a process. 2876 /// 2877 /// @param[in] vm_addr 2878 /// A virtual load address that indicates where to start writing 2879 /// memory to. 2880 /// 2881 /// @param[in] buf 2882 /// A byte buffer that is at least \a size bytes long that 2883 /// contains the data to write. 2884 /// 2885 /// @param[in] size 2886 /// The number of bytes to write. 2887 /// 2888 /// @param[out] error 2889 /// An error value in case the memory write fails. 2890 /// 2891 /// @return 2892 /// The number of bytes that were actually written. 2893 //------------------------------------------------------------------ 2894 virtual size_t 2895 DoWriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) 2896 { 2897 error.SetErrorStringWithFormat("error: %s does not support writing to processes", GetPluginName().GetCString()); 2898 return 0; 2899 } 2900 2901 2902 //------------------------------------------------------------------ 2903 /// Write all or part of a scalar value to memory. 2904 /// 2905 /// The value contained in \a scalar will be swapped to match the 2906 /// byte order of the process that is being debugged. If \a size is 2907 /// less than the size of scalar, the least significate \a size bytes 2908 /// from scalar will be written. If \a size is larger than the byte 2909 /// size of scalar, then the extra space will be padded with zeros 2910 /// and the scalar value will be placed in the least significant 2911 /// bytes in memory. 2912 /// 2913 /// @param[in] vm_addr 2914 /// A virtual load address that indicates where to start writing 2915 /// memory to. 2916 /// 2917 /// @param[in] scalar 2918 /// The scalar to write to the debugged process. 2919 /// 2920 /// @param[in] size 2921 /// This value can be smaller or larger than the scalar value 2922 /// itself. If \a size is smaller than the size of \a scalar, 2923 /// the least significant bytes in \a scalar will be used. If 2924 /// \a size is larger than the byte size of \a scalar, then 2925 /// the extra space will be padded with zeros. If \a size is 2926 /// set to UINT32_MAX, then the size of \a scalar will be used. 2927 /// 2928 /// @param[out] error 2929 /// An error value in case the memory write fails. 2930 /// 2931 /// @return 2932 /// The number of bytes that were actually written. 2933 //------------------------------------------------------------------ 2934 size_t 2935 WriteScalarToMemory (lldb::addr_t vm_addr, 2936 const Scalar &scalar, 2937 size_t size, 2938 Error &error); 2939 2940 size_t 2941 ReadScalarIntegerFromMemory (lldb::addr_t addr, 2942 uint32_t byte_size, 2943 bool is_signed, 2944 Scalar &scalar, 2945 Error &error); 2946 2947 //------------------------------------------------------------------ 2948 /// Write memory to a process. 2949 /// 2950 /// This function will write memory to the current process's 2951 /// address space and maintain any traps that might be present due 2952 /// to software breakpoints. 2953 /// 2954 /// This function is not meant to be overridden by Process 2955 /// subclasses, the subclasses should implement 2956 /// Process::DoWriteMemory (lldb::addr_t, size_t, void *). 2957 /// 2958 /// @param[in] vm_addr 2959 /// A virtual load address that indicates where to start writing 2960 /// memory to. 2961 /// 2962 /// @param[in] buf 2963 /// A byte buffer that is at least \a size bytes long that 2964 /// contains the data to write. 2965 /// 2966 /// @param[in] size 2967 /// The number of bytes to write. 2968 /// 2969 /// @return 2970 /// The number of bytes that were actually written. 2971 //------------------------------------------------------------------ 2972 size_t 2973 WriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error); 2974 2975 2976 //------------------------------------------------------------------ 2977 /// Actually allocate memory in the process. 2978 /// 2979 /// This function will allocate memory in the process's address 2980 /// space. This can't rely on the generic function calling mechanism, 2981 /// since that requires this function. 2982 /// 2983 /// @param[in] size 2984 /// The size of the allocation requested. 2985 /// 2986 /// @return 2987 /// The address of the allocated buffer in the process, or 2988 /// LLDB_INVALID_ADDRESS if the allocation failed. 2989 //------------------------------------------------------------------ 2990 2991 virtual lldb::addr_t 2992 DoAllocateMemory (size_t size, uint32_t permissions, Error &error) 2993 { 2994 error.SetErrorStringWithFormat("error: %s does not support allocating in the debug process", GetPluginName().GetCString()); 2995 return LLDB_INVALID_ADDRESS; 2996 } 2997 2998 2999 //------------------------------------------------------------------ 3000 /// The public interface to allocating memory in the process. 3001 /// 3002 /// This function will allocate memory in the process's address 3003 /// space. This can't rely on the generic function calling mechanism, 3004 /// since that requires this function. 3005 /// 3006 /// @param[in] size 3007 /// The size of the allocation requested. 3008 /// 3009 /// @param[in] permissions 3010 /// Or together any of the lldb::Permissions bits. The permissions on 3011 /// a given memory allocation can't be changed after allocation. Note 3012 /// that a block that isn't set writable can still be written on from lldb, 3013 /// just not by the process itself. 3014 /// 3015 /// @param[in/out] error 3016 /// An error object to fill in if things go wrong. 3017 /// @return 3018 /// The address of the allocated buffer in the process, or 3019 /// LLDB_INVALID_ADDRESS if the allocation failed. 3020 //------------------------------------------------------------------ 3021 3022 lldb::addr_t 3023 AllocateMemory (size_t size, uint32_t permissions, Error &error); 3024 3025 3026 //------------------------------------------------------------------ 3027 /// Resolve dynamically loaded indirect functions. 3028 /// 3029 /// @param[in] address 3030 /// The load address of the indirect function to resolve. 3031 /// 3032 /// @param[out] error 3033 /// An error value in case the resolve fails. 3034 /// 3035 /// @return 3036 /// The address of the resolved function. 3037 /// LLDB_INVALID_ADDRESS if the resolution failed. 3038 //------------------------------------------------------------------ 3039 3040 virtual lldb::addr_t 3041 ResolveIndirectFunction(const Address *address, Error &error); 3042 3043 virtual Error 3044 GetMemoryRegionInfo (lldb::addr_t load_addr, 3045 MemoryRegionInfo &range_info) 3046 { 3047 Error error; 3048 error.SetErrorString ("Process::GetMemoryRegionInfo() not supported"); 3049 return error; 3050 } 3051 3052 virtual Error 3053 GetWatchpointSupportInfo (uint32_t &num) 3054 { 3055 Error error; 3056 num = 0; 3057 error.SetErrorString ("Process::GetWatchpointSupportInfo() not supported"); 3058 return error; 3059 } 3060 3061 virtual Error 3062 GetWatchpointSupportInfo (uint32_t &num, bool& after) 3063 { 3064 Error error; 3065 num = 0; 3066 after = true; 3067 error.SetErrorString ("Process::GetWatchpointSupportInfo() not supported"); 3068 return error; 3069 } 3070 3071 lldb::ModuleSP 3072 ReadModuleFromMemory (const FileSpec& file_spec, 3073 lldb::addr_t header_addr); 3074 3075 //------------------------------------------------------------------ 3076 /// Attempt to get the attributes for a region of memory in the process. 3077 /// 3078 /// It may be possible for the remote debug server to inspect attributes 3079 /// for a region of memory in the process, such as whether there is a 3080 /// valid page of memory at a given address or whether that page is 3081 /// readable/writable/executable by the process. 3082 /// 3083 /// @param[in] load_addr 3084 /// The address of interest in the process. 3085 /// 3086 /// @param[out] permissions 3087 /// If this call returns successfully, this bitmask will have 3088 /// its Permissions bits set to indicate whether the region is 3089 /// readable/writable/executable. If this call fails, the 3090 /// bitmask values are undefined. 3091 /// 3092 /// @return 3093 /// Returns true if it was able to determine the attributes of the 3094 /// memory region. False if not. 3095 //------------------------------------------------------------------ 3096 3097 virtual bool 3098 GetLoadAddressPermissions (lldb::addr_t load_addr, uint32_t &permissions) 3099 { 3100 MemoryRegionInfo range_info; 3101 permissions = 0; 3102 Error error (GetMemoryRegionInfo (load_addr, range_info)); 3103 if (!error.Success()) 3104 return false; 3105 if (range_info.GetReadable() == MemoryRegionInfo::eDontKnow 3106 || range_info.GetWritable() == MemoryRegionInfo::eDontKnow 3107 || range_info.GetExecutable() == MemoryRegionInfo::eDontKnow) 3108 { 3109 return false; 3110 } 3111 3112 if (range_info.GetReadable() == MemoryRegionInfo::eYes) 3113 permissions |= lldb::ePermissionsReadable; 3114 3115 if (range_info.GetWritable() == MemoryRegionInfo::eYes) 3116 permissions |= lldb::ePermissionsWritable; 3117 3118 if (range_info.GetExecutable() == MemoryRegionInfo::eYes) 3119 permissions |= lldb::ePermissionsExecutable; 3120 3121 return true; 3122 } 3123 3124 //------------------------------------------------------------------ 3125 /// Determines whether executing JIT-compiled code in this process 3126 /// is possible. 3127 /// 3128 /// @return 3129 /// True if execution of JIT code is possible; false otherwise. 3130 //------------------------------------------------------------------ 3131 bool CanJIT (); 3132 3133 //------------------------------------------------------------------ 3134 /// Sets whether executing JIT-compiled code in this process 3135 /// is possible. 3136 /// 3137 /// @param[in] can_jit 3138 /// True if execution of JIT code is possible; false otherwise. 3139 //------------------------------------------------------------------ 3140 void SetCanJIT (bool can_jit); 3141 3142 //------------------------------------------------------------------ 3143 /// Actually deallocate memory in the process. 3144 /// 3145 /// This function will deallocate memory in the process's address 3146 /// space that was allocated with AllocateMemory. 3147 /// 3148 /// @param[in] ptr 3149 /// A return value from AllocateMemory, pointing to the memory you 3150 /// want to deallocate. 3151 /// 3152 /// @return 3153 /// \btrue if the memory was deallocated, \bfalse otherwise. 3154 //------------------------------------------------------------------ 3155 3156 virtual Error 3157 DoDeallocateMemory (lldb::addr_t ptr) 3158 { 3159 Error error; 3160 error.SetErrorStringWithFormat("error: %s does not support deallocating in the debug process", GetPluginName().GetCString()); 3161 return error; 3162 } 3163 3164 3165 //------------------------------------------------------------------ 3166 /// The public interface to deallocating memory in the process. 3167 /// 3168 /// This function will deallocate memory in the process's address 3169 /// space that was allocated with AllocateMemory. 3170 /// 3171 /// @param[in] ptr 3172 /// A return value from AllocateMemory, pointing to the memory you 3173 /// want to deallocate. 3174 /// 3175 /// @return 3176 /// \btrue if the memory was deallocated, \bfalse otherwise. 3177 //------------------------------------------------------------------ 3178 3179 Error 3180 DeallocateMemory (lldb::addr_t ptr); 3181 3182 //------------------------------------------------------------------ 3183 /// Get any available STDOUT. 3184 /// 3185 /// If the process was launched without supplying valid file paths 3186 /// for stdin, stdout, and stderr, then the Process class might 3187 /// try to cache the STDOUT for the process if it is able. Events 3188 /// will be queued indicating that there is STDOUT available that 3189 /// can be retrieved using this function. 3190 /// 3191 /// @param[out] buf 3192 /// A buffer that will receive any STDOUT bytes that are 3193 /// currently available. 3194 /// 3195 /// @param[out] buf_size 3196 /// The size in bytes for the buffer \a buf. 3197 /// 3198 /// @return 3199 /// The number of bytes written into \a buf. If this value is 3200 /// equal to \a buf_size, another call to this function should 3201 /// be made to retrieve more STDOUT data. 3202 //------------------------------------------------------------------ 3203 virtual size_t 3204 GetSTDOUT (char *buf, size_t buf_size, Error &error); 3205 3206 //------------------------------------------------------------------ 3207 /// Get any available STDERR. 3208 /// 3209 /// If the process was launched without supplying valid file paths 3210 /// for stdin, stdout, and stderr, then the Process class might 3211 /// try to cache the STDERR for the process if it is able. Events 3212 /// will be queued indicating that there is STDERR available that 3213 /// can be retrieved using this function. 3214 /// 3215 /// @param[out] buf 3216 /// A buffer that will receive any STDERR bytes that are 3217 /// currently available. 3218 /// 3219 /// @param[out] buf_size 3220 /// The size in bytes for the buffer \a buf. 3221 /// 3222 /// @return 3223 /// The number of bytes written into \a buf. If this value is 3224 /// equal to \a buf_size, another call to this function should 3225 /// be made to retrieve more STDERR data. 3226 //------------------------------------------------------------------ 3227 virtual size_t 3228 GetSTDERR (char *buf, size_t buf_size, Error &error); 3229 3230 virtual size_t 3231 PutSTDIN (const char *buf, size_t buf_size, Error &error) 3232 { 3233 error.SetErrorString("stdin unsupported"); 3234 return 0; 3235 } 3236 3237 //------------------------------------------------------------------ 3238 /// Get any available profile data. 3239 /// 3240 /// @param[out] buf 3241 /// A buffer that will receive any profile data bytes that are 3242 /// currently available. 3243 /// 3244 /// @param[out] buf_size 3245 /// The size in bytes for the buffer \a buf. 3246 /// 3247 /// @return 3248 /// The number of bytes written into \a buf. If this value is 3249 /// equal to \a buf_size, another call to this function should 3250 /// be made to retrieve more profile data. 3251 //------------------------------------------------------------------ 3252 virtual size_t 3253 GetAsyncProfileData (char *buf, size_t buf_size, Error &error); 3254 3255 //---------------------------------------------------------------------- 3256 // Process Breakpoints 3257 //---------------------------------------------------------------------- 3258 size_t 3259 GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site); 3260 3261 virtual Error 3262 EnableBreakpointSite (BreakpointSite *bp_site) 3263 { 3264 Error error; 3265 error.SetErrorStringWithFormat("error: %s does not support enabling breakpoints", GetPluginName().GetCString()); 3266 return error; 3267 } 3268 3269 3270 virtual Error 3271 DisableBreakpointSite (BreakpointSite *bp_site) 3272 { 3273 Error error; 3274 error.SetErrorStringWithFormat("error: %s does not support disabling breakpoints", GetPluginName().GetCString()); 3275 return error; 3276 } 3277 3278 3279 // This is implemented completely using the lldb::Process API. Subclasses 3280 // don't need to implement this function unless the standard flow of 3281 // read existing opcode, write breakpoint opcode, verify breakpoint opcode 3282 // doesn't work for a specific process plug-in. 3283 virtual Error 3284 EnableSoftwareBreakpoint (BreakpointSite *bp_site); 3285 3286 // This is implemented completely using the lldb::Process API. Subclasses 3287 // don't need to implement this function unless the standard flow of 3288 // restoring original opcode in memory and verifying the restored opcode 3289 // doesn't work for a specific process plug-in. 3290 virtual Error 3291 DisableSoftwareBreakpoint (BreakpointSite *bp_site); 3292 3293 BreakpointSiteList & 3294 GetBreakpointSiteList(); 3295 3296 const BreakpointSiteList & 3297 GetBreakpointSiteList() const; 3298 3299 void 3300 DisableAllBreakpointSites (); 3301 3302 Error 3303 ClearBreakpointSiteByID (lldb::user_id_t break_id); 3304 3305 lldb::break_id_t 3306 CreateBreakpointSite (const lldb::BreakpointLocationSP &owner, 3307 bool use_hardware); 3308 3309 Error 3310 DisableBreakpointSiteByID (lldb::user_id_t break_id); 3311 3312 Error 3313 EnableBreakpointSiteByID (lldb::user_id_t break_id); 3314 3315 3316 // BreakpointLocations use RemoveOwnerFromBreakpointSite to remove 3317 // themselves from the owner's list of this breakpoint sites. 3318 void 3319 RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, 3320 lldb::user_id_t owner_loc_id, 3321 lldb::BreakpointSiteSP &bp_site_sp); 3322 3323 //---------------------------------------------------------------------- 3324 // Process Watchpoints (optional) 3325 //---------------------------------------------------------------------- 3326 virtual Error 3327 EnableWatchpoint (Watchpoint *wp, bool notify = true); 3328 3329 virtual Error 3330 DisableWatchpoint (Watchpoint *wp, bool notify = true); 3331 3332 //------------------------------------------------------------------ 3333 // Thread Queries 3334 //------------------------------------------------------------------ 3335 virtual bool 3336 UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) = 0; 3337 3338 void 3339 UpdateThreadListIfNeeded (); 3340 3341 ThreadList & 3342 GetThreadList () 3343 { 3344 return m_thread_list; 3345 } 3346 3347 // When ExtendedBacktraces are requested, the HistoryThreads that are 3348 // created need an owner -- they're saved here in the Process. The 3349 // threads in this list are not iterated over - driver programs need to 3350 // request the extended backtrace calls starting from a root concrete 3351 // thread one by one. 3352 ThreadList & 3353 GetExtendedThreadList () 3354 { 3355 return m_extended_thread_list; 3356 } 3357 3358 ThreadList::ThreadIterable 3359 Threads () 3360 { 3361 return m_thread_list.Threads(); 3362 } 3363 3364 uint32_t 3365 GetNextThreadIndexID (uint64_t thread_id); 3366 3367 lldb::ThreadSP 3368 CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context); 3369 3370 // Returns true if an index id has been assigned to a thread. 3371 bool 3372 HasAssignedIndexIDToThread(uint64_t sb_thread_id); 3373 3374 // Given a thread_id, it will assign a more reasonable index id for display to the user. 3375 // If the thread_id has previously been assigned, the same index id will be used. 3376 uint32_t 3377 AssignIndexIDToThread(uint64_t thread_id); 3378 3379 //------------------------------------------------------------------ 3380 // Queue Queries 3381 //------------------------------------------------------------------ 3382 3383 void 3384 UpdateQueueListIfNeeded (); 3385 3386 QueueList & 3387 GetQueueList () 3388 { 3389 UpdateQueueListIfNeeded(); 3390 return m_queue_list; 3391 } 3392 3393 QueueList::QueueIterable 3394 Queues () 3395 { 3396 UpdateQueueListIfNeeded(); 3397 return m_queue_list.Queues(); 3398 } 3399 3400 //------------------------------------------------------------------ 3401 // Event Handling 3402 //------------------------------------------------------------------ 3403 lldb::StateType 3404 GetNextEvent (lldb::EventSP &event_sp); 3405 3406 // Returns the process state when it is stopped. If specified, event_sp_ptr 3407 // is set to the event which triggered the stop. If wait_always = false, 3408 // and the process is already stopped, this function returns immediately. 3409 lldb::StateType 3410 WaitForProcessToStop (const TimeValue *timeout, 3411 lldb::EventSP *event_sp_ptr = NULL, 3412 bool wait_always = true, 3413 Listener *hijack_listener = NULL); 3414 3415 lldb::StateType 3416 WaitForStateChangedEvents (const TimeValue *timeout, 3417 lldb::EventSP &event_sp, 3418 Listener *hijack_listener); // Pass NULL to use builtin listener 3419 3420 Event * 3421 PeekAtStateChangedEvents (); 3422 3423 3424 class 3425 ProcessEventHijacker 3426 { 3427 public: 3428 ProcessEventHijacker (Process &process, Listener *listener) : 3429 m_process (process) 3430 { 3431 m_process.HijackProcessEvents (listener); 3432 } 3433 ~ProcessEventHijacker () 3434 { 3435 m_process.RestoreProcessEvents(); 3436 } 3437 3438 private: 3439 Process &m_process; 3440 }; 3441 friend class ProcessEventHijacker; 3442 //------------------------------------------------------------------ 3443 /// If you need to ensure that you and only you will hear about some public 3444 /// event, then make a new listener, set to listen to process events, and 3445 /// then call this with that listener. Then you will have to wait on that 3446 /// listener explicitly for events (rather than using the GetNextEvent & WaitFor* 3447 /// calls above. Be sure to call RestoreProcessEvents when you are done. 3448 /// 3449 /// @param[in] listener 3450 /// This is the new listener to whom all process events will be delivered. 3451 /// 3452 /// @return 3453 /// Returns \b true if the new listener could be installed, 3454 /// \b false otherwise. 3455 //------------------------------------------------------------------ 3456 bool 3457 HijackProcessEvents (Listener *listener); 3458 3459 //------------------------------------------------------------------ 3460 /// Restores the process event broadcasting to its normal state. 3461 /// 3462 //------------------------------------------------------------------ 3463 void 3464 RestoreProcessEvents (); 3465 3466private: 3467 //------------------------------------------------------------------ 3468 /// This is the part of the event handling that for a process event. 3469 /// It decides what to do with the event and returns true if the 3470 /// event needs to be propagated to the user, and false otherwise. 3471 /// If the event is not propagated, this call will most likely set 3472 /// the target to executing again. 3473 /// There is only one place where this call should be called, HandlePrivateEvent. 3474 /// Don't call it from anywhere else... 3475 /// 3476 /// @param[in] event_ptr 3477 /// This is the event we are handling. 3478 /// 3479 /// @return 3480 /// Returns \b true if the event should be reported to the 3481 /// user, \b false otherwise. 3482 //------------------------------------------------------------------ 3483 bool 3484 ShouldBroadcastEvent (Event *event_ptr); 3485 3486public: 3487 const lldb::ABISP & 3488 GetABI (); 3489 3490 OperatingSystem * 3491 GetOperatingSystem () 3492 { 3493 return m_os_ap.get(); 3494 } 3495 3496 3497 virtual LanguageRuntime * 3498 GetLanguageRuntime (lldb::LanguageType language, bool retry_if_null = true); 3499 3500 virtual CPPLanguageRuntime * 3501 GetCPPLanguageRuntime (bool retry_if_null = true); 3502 3503 virtual ObjCLanguageRuntime * 3504 GetObjCLanguageRuntime (bool retry_if_null = true); 3505 3506 bool 3507 IsPossibleDynamicValue (ValueObject& in_value); 3508 3509 bool 3510 IsRunning () const; 3511 3512 DynamicCheckerFunctions *GetDynamicCheckers() 3513 { 3514 return m_dynamic_checkers_ap.get(); 3515 } 3516 3517 void SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) 3518 { 3519 m_dynamic_checkers_ap.reset(dynamic_checkers); 3520 } 3521 3522 //------------------------------------------------------------------ 3523 /// Call this to set the lldb in the mode where it breaks on new thread 3524 /// creations, and then auto-restarts. This is useful when you are trying 3525 /// to run only one thread, but either that thread or the kernel is creating 3526 /// new threads in the process. If you stop when the thread is created, you 3527 /// can immediately suspend it, and keep executing only the one thread you intend. 3528 /// 3529 /// @return 3530 /// Returns \b true if we were able to start up the notification 3531 /// \b false otherwise. 3532 //------------------------------------------------------------------ 3533 virtual bool 3534 StartNoticingNewThreads() 3535 { 3536 return true; 3537 } 3538 3539 //------------------------------------------------------------------ 3540 /// Call this to turn off the stop & notice new threads mode. 3541 /// 3542 /// @return 3543 /// Returns \b true if we were able to start up the notification 3544 /// \b false otherwise. 3545 //------------------------------------------------------------------ 3546 virtual bool 3547 StopNoticingNewThreads() 3548 { 3549 return true; 3550 } 3551 3552 void 3553 SetRunningUserExpression (bool on); 3554 3555 //------------------------------------------------------------------ 3556 // lldb::ExecutionContextScope pure virtual functions 3557 //------------------------------------------------------------------ 3558 virtual lldb::TargetSP 3559 CalculateTarget (); 3560 3561 virtual lldb::ProcessSP 3562 CalculateProcess () 3563 { 3564 return shared_from_this(); 3565 } 3566 3567 virtual lldb::ThreadSP 3568 CalculateThread () 3569 { 3570 return lldb::ThreadSP(); 3571 } 3572 3573 virtual lldb::StackFrameSP 3574 CalculateStackFrame () 3575 { 3576 return lldb::StackFrameSP(); 3577 } 3578 3579 virtual void 3580 CalculateExecutionContext (ExecutionContext &exe_ctx); 3581 3582 void 3583 SetSTDIOFileDescriptor (int file_descriptor); 3584 3585 void 3586 WatchForSTDIN (IOHandler &io_handler); 3587 3588 void 3589 CancelWatchForSTDIN (bool exited); 3590 3591 //------------------------------------------------------------------ 3592 // Add a permanent region of memory that should never be read or 3593 // written to. This can be used to ensure that memory reads or writes 3594 // to certain areas of memory never end up being sent to the 3595 // DoReadMemory or DoWriteMemory functions which can improve 3596 // performance. 3597 //------------------------------------------------------------------ 3598 void 3599 AddInvalidMemoryRegion (const LoadRange ®ion); 3600 3601 //------------------------------------------------------------------ 3602 // Remove a permanent region of memory that should never be read or 3603 // written to that was previously added with AddInvalidMemoryRegion. 3604 //------------------------------------------------------------------ 3605 bool 3606 RemoveInvalidMemoryRange (const LoadRange ®ion); 3607 3608 //------------------------------------------------------------------ 3609 // If the setup code of a thread plan needs to do work that might involve 3610 // calling a function in the target, it should not do that work directly 3611 // in one of the thread plan functions (DidPush/WillResume) because 3612 // such work needs to be handled carefully. Instead, put that work in 3613 // a PreResumeAction callback, and register it with the process. It will 3614 // get done before the actual "DoResume" gets called. 3615 //------------------------------------------------------------------ 3616 3617 typedef bool (PreResumeActionCallback)(void *); 3618 3619 void 3620 AddPreResumeAction (PreResumeActionCallback callback, void *baton); 3621 3622 bool 3623 RunPreResumeActions (); 3624 3625 void 3626 ClearPreResumeActions (); 3627 3628 ProcessRunLock & 3629 GetRunLock () 3630 { 3631 if (Host::GetCurrentThread() == m_private_state_thread) 3632 return m_private_run_lock; 3633 else 3634 return m_public_run_lock; 3635 } 3636 3637protected: 3638 //------------------------------------------------------------------ 3639 // NextEventAction provides a way to register an action on the next 3640 // event that is delivered to this process. There is currently only 3641 // one next event action allowed in the process at one time. If a 3642 // new "NextEventAction" is added while one is already present, the 3643 // old action will be discarded (with HandleBeingUnshipped called 3644 // after it is discarded.) 3645 // 3646 // If you want to resume the process as a result of a resume action, 3647 // call RequestResume, don't call Resume directly. 3648 //------------------------------------------------------------------ 3649 class NextEventAction 3650 { 3651 public: 3652 typedef enum EventActionResult 3653 { 3654 eEventActionSuccess, 3655 eEventActionRetry, 3656 eEventActionExit 3657 } EventActionResult; 3658 3659 NextEventAction (Process *process) : 3660 m_process(process) 3661 { 3662 } 3663 3664 virtual 3665 ~NextEventAction() 3666 { 3667 } 3668 3669 virtual EventActionResult PerformAction (lldb::EventSP &event_sp) = 0; 3670 virtual void HandleBeingUnshipped () {} 3671 virtual EventActionResult HandleBeingInterrupted () = 0; 3672 virtual const char *GetExitString() = 0; 3673 void RequestResume() 3674 { 3675 m_process->m_resume_requested = true; 3676 } 3677 protected: 3678 Process *m_process; 3679 }; 3680 3681 void SetNextEventAction (Process::NextEventAction *next_event_action) 3682 { 3683 if (m_next_event_action_ap.get()) 3684 m_next_event_action_ap->HandleBeingUnshipped(); 3685 3686 m_next_event_action_ap.reset(next_event_action); 3687 } 3688 3689 // This is the completer for Attaching: 3690 class AttachCompletionHandler : public NextEventAction 3691 { 3692 public: 3693 AttachCompletionHandler (Process *process, uint32_t exec_count) : 3694 NextEventAction (process), 3695 m_exec_count (exec_count) 3696 { 3697 } 3698 3699 virtual 3700 ~AttachCompletionHandler() 3701 { 3702 } 3703 3704 virtual EventActionResult PerformAction (lldb::EventSP &event_sp); 3705 virtual EventActionResult HandleBeingInterrupted (); 3706 virtual const char *GetExitString(); 3707 private: 3708 uint32_t m_exec_count; 3709 std::string m_exit_string; 3710 }; 3711 3712 bool 3713 HijackPrivateProcessEvents (Listener *listener); 3714 3715 void 3716 RestorePrivateProcessEvents (); 3717 3718 bool 3719 PrivateStateThreadIsValid () const 3720 { 3721 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); 3722 } 3723 3724 void 3725 ForceNextEventDelivery() 3726 { 3727 m_force_next_event_delivery = true; 3728 } 3729 3730 //------------------------------------------------------------------ 3731 // Type definitions 3732 //------------------------------------------------------------------ 3733 typedef std::map<lldb::LanguageType, lldb::LanguageRuntimeSP> LanguageRuntimeCollection; 3734 3735 struct PreResumeCallbackAndBaton 3736 { 3737 bool (*callback) (void *); 3738 void *baton; 3739 PreResumeCallbackAndBaton (PreResumeActionCallback in_callback, void *in_baton) : 3740 callback (in_callback), 3741 baton (in_baton) 3742 { 3743 } 3744 }; 3745 3746 //------------------------------------------------------------------ 3747 // Member variables 3748 //------------------------------------------------------------------ 3749 Target & m_target; ///< The target that owns this process. 3750 ThreadSafeValue<lldb::StateType> m_public_state; 3751 ThreadSafeValue<lldb::StateType> m_private_state; // The actual state of our process 3752 Broadcaster m_private_state_broadcaster; // This broadcaster feeds state changed events into the private state thread's listener. 3753 Broadcaster m_private_state_control_broadcaster; // This is the control broadcaster, used to pause, resume & stop the private state thread. 3754 Listener m_private_state_listener; // This is the listener for the private state thread. 3755 Predicate<bool> m_private_state_control_wait; /// This Predicate is used to signal that a control operation is complete. 3756 lldb::thread_t m_private_state_thread; // Thread ID for the thread that watches interal state events 3757 ProcessModID m_mod_id; ///< Tracks the state of the process over stops and other alterations. 3758 uint32_t m_process_unique_id; ///< Each lldb_private::Process class that is created gets a unique integer ID that increments with each new instance 3759 uint32_t m_thread_index_id; ///< Each thread is created with a 1 based index that won't get re-used. 3760 std::map<uint64_t, uint32_t> m_thread_id_to_index_id_map; 3761 int m_exit_status; ///< The exit status of the process, or -1 if not set. 3762 std::string m_exit_string; ///< A textual description of why a process exited. 3763 Mutex m_thread_mutex; 3764 ThreadList m_thread_list_real; ///< The threads for this process as are known to the protocol we are debugging with 3765 ThreadList m_thread_list; ///< The threads for this process as the user will see them. This is usually the same as 3766 ///< m_thread_list_real, but might be different if there is an OS plug-in creating memory threads 3767 ThreadList m_extended_thread_list; ///< Owner for extended threads that may be generated, cleared on natural stops 3768 uint32_t m_extended_thread_stop_id; ///< The natural stop id when extended_thread_list was last updated 3769 QueueList m_queue_list; ///< The list of libdispatch queues at a given stop point 3770 uint32_t m_queue_list_stop_id; ///< The natural stop id when queue list was last fetched 3771 std::vector<Notifications> m_notifications; ///< The list of notifications that this process can deliver. 3772 std::vector<lldb::addr_t> m_image_tokens; 3773 Listener &m_listener; 3774 BreakpointSiteList m_breakpoint_site_list; ///< This is the list of breakpoint locations we intend to insert in the target. 3775 std::unique_ptr<DynamicLoader> m_dyld_ap; 3776 std::unique_ptr<DynamicCheckerFunctions> m_dynamic_checkers_ap; ///< The functions used by the expression parser to validate data that expressions use. 3777 std::unique_ptr<OperatingSystem> m_os_ap; 3778 std::unique_ptr<SystemRuntime> m_system_runtime_ap; 3779 UnixSignals m_unix_signals; /// This is the current signal set for this process. 3780 lldb::ABISP m_abi_sp; 3781 lldb::IOHandlerSP m_process_input_reader; 3782 Communication m_stdio_communication; 3783 Mutex m_stdio_communication_mutex; 3784 std::string m_stdout_data; 3785 std::string m_stderr_data; 3786 Mutex m_profile_data_comm_mutex; 3787 std::vector<std::string> m_profile_data; 3788 MemoryCache m_memory_cache; 3789 AllocatedMemoryCache m_allocated_memory_cache; 3790 bool m_should_detach; /// Should we detach if the process object goes away with an explicit call to Kill or Detach? 3791 LanguageRuntimeCollection m_language_runtimes; 3792 std::unique_ptr<NextEventAction> m_next_event_action_ap; 3793 std::vector<PreResumeCallbackAndBaton> m_pre_resume_actions; 3794 ProcessRunLock m_public_run_lock; 3795 ProcessRunLock m_private_run_lock; 3796 Predicate<bool> m_currently_handling_event; // This predicate is set in HandlePrivateEvent while all its business is being done. 3797 bool m_currently_handling_do_on_removals; 3798 bool m_resume_requested; // If m_currently_handling_event or m_currently_handling_do_on_removals are true, Resume will only request a resume, using this flag to check. 3799 bool m_finalize_called; 3800 bool m_clear_thread_plans_on_stop; 3801 bool m_force_next_event_delivery; 3802 lldb::StateType m_last_broadcast_state; /// This helps with the Public event coalescing in ShouldBroadcastEvent. 3803 std::map<lldb::addr_t,lldb::addr_t> m_resolved_indirect_addresses; 3804 bool m_destroy_in_process; 3805 3806 enum { 3807 eCanJITDontKnow= 0, 3808 eCanJITYes, 3809 eCanJITNo 3810 } m_can_jit; 3811 3812 size_t 3813 RemoveBreakpointOpcodesFromBuffer (lldb::addr_t addr, size_t size, uint8_t *buf) const; 3814 3815 void 3816 SynchronouslyNotifyStateChanged (lldb::StateType state); 3817 3818 void 3819 SetPublicState (lldb::StateType new_state, bool restarted); 3820 3821 void 3822 SetPrivateState (lldb::StateType state); 3823 3824 bool 3825 StartPrivateStateThread (bool force = false); 3826 3827 void 3828 StopPrivateStateThread (); 3829 3830 void 3831 PausePrivateStateThread (); 3832 3833 void 3834 ResumePrivateStateThread (); 3835 3836 static lldb::thread_result_t 3837 PrivateStateThread (void *arg); 3838 3839 lldb::thread_result_t 3840 RunPrivateStateThread (); 3841 3842 void 3843 HandlePrivateEvent (lldb::EventSP &event_sp); 3844 3845 lldb::StateType 3846 WaitForProcessStopPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 3847 3848 // This waits for both the state change broadcaster, and the control broadcaster. 3849 // If control_only, it only waits for the control broadcaster. 3850 3851 bool 3852 WaitForEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp, bool control_only); 3853 3854 lldb::StateType 3855 WaitForStateChangedEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 3856 3857 lldb::StateType 3858 WaitForState (const TimeValue *timeout, 3859 const lldb::StateType *match_states, 3860 const uint32_t num_match_states); 3861 3862 size_t 3863 WriteMemoryPrivate (lldb::addr_t addr, const void *buf, size_t size, Error &error); 3864 3865 void 3866 AppendSTDOUT (const char *s, size_t len); 3867 3868 void 3869 AppendSTDERR (const char *s, size_t len); 3870 3871 void 3872 BroadcastAsyncProfileData(const std::string &one_profile_data); 3873 3874 static void 3875 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); 3876 3877 void 3878 PushProcessIOHandler (); 3879 3880 void 3881 PopProcessIOHandler (); 3882 3883 void 3884 ResetProcessIOHandler (); 3885 3886 Error 3887 HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp); 3888 3889private: 3890 //------------------------------------------------------------------ 3891 // For Process only 3892 //------------------------------------------------------------------ 3893 void ControlPrivateStateThread (uint32_t signal); 3894 3895 DISALLOW_COPY_AND_ASSIGN (Process); 3896 3897}; 3898 3899} // namespace lldb_private 3900 3901#endif // liblldb_Process_h_ 3902