Process.h revision 07c51d09d4b61891cdc37f44bb332dc286b4de2f
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// C Includes 14#include <limits.h> 15#include <spawn.h> 16 17// C++ Includes 18#include <list> 19#include <iosfwd> 20#include <vector> 21 22// Other libraries and framework includes 23// Project includes 24#include "lldb/lldb-private.h" 25#include "lldb/Core/ArchSpec.h" 26#include "lldb/Core/Broadcaster.h" 27#include "lldb/Core/Communication.h" 28#include "lldb/Core/Error.h" 29#include "lldb/Core/Event.h" 30#include "lldb/Core/StringList.h" 31#include "lldb/Core/ThreadSafeValue.h" 32#include "lldb/Core/PluginInterface.h" 33#include "lldb/Core/UserSettingsController.h" 34#include "lldb/Breakpoint/BreakpointSiteList.h" 35#include "lldb/Expression/ClangPersistentVariables.h" 36#include "lldb/Expression/IRDynamicChecks.h" 37#include "lldb/Host/FileSpec.h" 38#include "lldb/Interpreter/Args.h" 39#include "lldb/Interpreter/Options.h" 40#include "lldb/Target/ExecutionContextScope.h" 41#include "lldb/Target/Memory.h" 42#include "lldb/Target/ThreadList.h" 43#include "lldb/Target/UnixSignals.h" 44 45namespace lldb_private { 46 47//---------------------------------------------------------------------- 48// ProcessInstanceSettings 49//---------------------------------------------------------------------- 50class ProcessInstanceSettings : public InstanceSettings 51{ 52public: 53 54 ProcessInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL); 55 56 ProcessInstanceSettings (const ProcessInstanceSettings &rhs); 57 58 virtual 59 ~ProcessInstanceSettings (); 60 61 ProcessInstanceSettings& 62 operator= (const ProcessInstanceSettings &rhs); 63 64 65 void 66 UpdateInstanceSettingsVariable (const ConstString &var_name, 67 const char *index_value, 68 const char *value, 69 const ConstString &instance_name, 70 const SettingEntry &entry, 71 VarSetOperationType op, 72 Error &err, 73 bool pending); 74 75 bool 76 GetInstanceSettingsValue (const SettingEntry &entry, 77 const ConstString &var_name, 78 StringList &value, 79 Error *err); 80 81 82 const Args & 83 GetRunArguments () const 84 { 85 return m_run_args; 86 } 87 88 void 89 SetRunArguments (const Args &args) 90 { 91 m_run_args = args; 92 } 93 94 void 95 GetHostEnvironmentIfNeeded (); 96 97 size_t 98 GetEnvironmentAsArgs (Args &env); 99 100 const char * 101 GetStandardInputPath () const 102 { 103 if (m_input_path.empty()) 104 return NULL; 105 return m_input_path.c_str(); 106 } 107 108 void 109 SetStandardInputPath (const char *path) 110 { 111 if (path && path[0]) 112 m_input_path.assign (path); 113 else 114 { 115 // Make sure we deallocate memory in string... 116 std::string tmp; 117 tmp.swap (m_input_path); 118 } 119 } 120 121 const char * 122 GetStandardOutputPath () const 123 { 124 if (m_output_path.empty()) 125 return NULL; 126 return m_output_path.c_str(); 127 } 128 129 void 130 SetStandardOutputPath (const char *path) 131 { 132 if (path && path[0]) 133 m_output_path.assign (path); 134 else 135 { 136 // Make sure we deallocate memory in string... 137 std::string tmp; 138 tmp.swap (m_output_path); 139 } 140 } 141 142 const char * 143 GetStandardErrorPath () const 144 { 145 if (m_error_path.empty()) 146 return NULL; 147 return m_error_path.c_str(); 148 } 149 150 void 151 SetStandardErrorPath (const char *path) 152 { 153 if (path && path[0]) 154 m_error_path.assign (path); 155 else 156 { 157 // Make sure we deallocate memory in string... 158 std::string tmp; 159 tmp.swap (m_error_path); 160 } 161 } 162 163 bool 164 GetDisableASLR () const 165 { 166 return m_disable_aslr; 167 } 168 169 void 170 SetDisableASLR (bool b) 171 { 172 m_disable_aslr = b; 173 } 174 175 bool 176 GetDisableSTDIO () const 177 { 178 return m_disable_stdio; 179 } 180 181 void 182 SetDisableSTDIO (bool b) 183 { 184 m_disable_stdio = b; 185 } 186 187protected: 188 189 void 190 CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 191 bool pending); 192 193 const ConstString 194 CreateInstanceName (); 195 196 static const ConstString & 197 RunArgsVarName (); 198 199 static const ConstString & 200 EnvVarsVarName (); 201 202 static const ConstString & 203 InheritHostEnvVarName (); 204 205 static const ConstString & 206 InputPathVarName (); 207 208 static const ConstString & 209 OutputPathVarName (); 210 211 static const ConstString & 212 ErrorPathVarName (); 213 214 static const ConstString & 215 DisableASLRVarName(); 216 217 static const ConstString & 218 DisableSTDIOVarName (); 219 220private: 221 222 typedef std::map<std::string, std::string> dictionary; 223 Args m_run_args; 224 dictionary m_env_vars; 225 std::string m_input_path; 226 std::string m_output_path; 227 std::string m_error_path; 228 bool m_disable_aslr; 229 bool m_disable_stdio; 230 bool m_inherit_host_env; 231 bool m_got_host_env; 232}; 233 234//---------------------------------------------------------------------- 235// ProcessInfo 236// 237// A base class for information for a process. This can be used to fill 238// out information for a process prior to launching it, or it can be 239// used for an instance of a process and can be filled in with the 240// existing values for that process. 241//---------------------------------------------------------------------- 242class ProcessInfo 243{ 244public: 245 ProcessInfo () : 246 m_executable (), 247 m_arguments (), 248 m_environment (), 249 m_uid (UINT32_MAX), 250 m_gid (UINT32_MAX), 251 m_arch(), 252 m_pid (LLDB_INVALID_PROCESS_ID) 253 { 254 } 255 256 ProcessInfo (const char *name, 257 const ArchSpec &arch, 258 lldb::pid_t pid) : 259 m_executable (name, false), 260 m_arguments (), 261 m_environment(), 262 m_uid (UINT32_MAX), 263 m_gid (UINT32_MAX), 264 m_arch (arch), 265 m_pid (pid) 266 { 267 } 268 269 void 270 Clear () 271 { 272 m_executable.Clear(); 273 m_arguments.Clear(); 274 m_environment.Clear(); 275 m_uid = UINT32_MAX; 276 m_gid = UINT32_MAX; 277 m_arch.Clear(); 278 m_pid = LLDB_INVALID_PROCESS_ID; 279 } 280 281 const char * 282 GetName() const 283 { 284 return m_executable.GetFilename().GetCString(); 285 } 286 287 size_t 288 GetNameLength() const 289 { 290 return m_executable.GetFilename().GetLength(); 291 } 292 293 void 294 SetName (const char *name) 295 { 296 m_executable.GetFilename().SetCString (name); 297 } 298 299 FileSpec & 300 GetExecutableFile () 301 { 302 return m_executable; 303 } 304 305 void 306 SetExecutableFile (const FileSpec &exe_file, bool add_exe_file_as_first_arg) 307 { 308 if (exe_file) 309 { 310 m_executable = exe_file; 311 if (add_exe_file_as_first_arg) 312 { 313 m_arguments.Clear(); 314 char filename[PATH_MAX]; 315 if (exe_file.GetPath(filename, sizeof(filename))) 316 m_arguments.AppendArgument (filename); 317 } 318 } 319 else 320 { 321 m_executable.Clear(); 322 if (add_exe_file_as_first_arg) 323 m_arguments.Clear(); 324 } 325 } 326 327 const FileSpec & 328 GetExecutableFile () const 329 { 330 return m_executable; 331 } 332 333 uint32_t 334 GetUserID() const 335 { 336 return m_uid; 337 } 338 339 uint32_t 340 GetGroupID() const 341 { 342 return m_gid; 343 } 344 345 bool 346 UserIDIsValid () const 347 { 348 return m_uid != UINT32_MAX; 349 } 350 351 bool 352 GroupIDIsValid () const 353 { 354 return m_gid != UINT32_MAX; 355 } 356 357 void 358 SetUserID (uint32_t uid) 359 { 360 m_uid = uid; 361 } 362 363 void 364 SetGroupID (uint32_t gid) 365 { 366 m_gid = gid; 367 } 368 369 ArchSpec & 370 GetArchitecture () 371 { 372 return m_arch; 373 } 374 375 const ArchSpec & 376 GetArchitecture () const 377 { 378 return m_arch; 379 } 380 381 lldb::pid_t 382 GetProcessID () const 383 { 384 return m_pid; 385 } 386 387 void 388 SetProcessID (lldb::pid_t pid) 389 { 390 m_pid = pid; 391 } 392 393 bool 394 ProcessIDIsValid() const 395 { 396 return m_pid != LLDB_INVALID_PROCESS_ID; 397 } 398 399 void 400 Dump (Stream &s, Platform *platform) const; 401 402 Args & 403 GetArguments () 404 { 405 return m_arguments; 406 } 407 408 const Args & 409 GetArguments () const 410 { 411 return m_arguments; 412 } 413 414 void 415 SetArguments (const Args& args, 416 bool first_arg_is_executable, 417 bool first_arg_is_executable_and_argument); 418 419 void 420 SetArguments (char const **argv, 421 bool first_arg_is_executable, 422 bool first_arg_is_executable_and_argument); 423 424 Args & 425 GetEnvironmentEntries () 426 { 427 return m_environment; 428 } 429 430 const Args & 431 GetEnvironmentEntries () const 432 { 433 return m_environment; 434 } 435 436protected: 437 FileSpec m_executable; 438 Args m_arguments; 439 Args m_environment; 440 uint32_t m_uid; 441 uint32_t m_gid; 442 ArchSpec m_arch; 443 lldb::pid_t m_pid; 444}; 445 446//---------------------------------------------------------------------- 447// ProcessInstanceInfo 448// 449// Describes an existing process and any discoverable information that 450// pertains to that process. 451//---------------------------------------------------------------------- 452class ProcessInstanceInfo : public ProcessInfo 453{ 454public: 455 ProcessInstanceInfo () : 456 ProcessInfo (), 457 m_euid (UINT32_MAX), 458 m_egid (UINT32_MAX), 459 m_parent_pid (LLDB_INVALID_PROCESS_ID) 460 { 461 } 462 463 ProcessInstanceInfo (const char *name, 464 const ArchSpec &arch, 465 lldb::pid_t pid) : 466 ProcessInfo (name, arch, pid), 467 m_euid (UINT32_MAX), 468 m_egid (UINT32_MAX), 469 m_parent_pid (LLDB_INVALID_PROCESS_ID) 470 { 471 } 472 473 void 474 Clear () 475 { 476 ProcessInfo::Clear(); 477 m_euid = UINT32_MAX; 478 m_egid = UINT32_MAX; 479 m_parent_pid = LLDB_INVALID_PROCESS_ID; 480 } 481 482 uint32_t 483 GetEffectiveUserID() const 484 { 485 return m_euid; 486 } 487 488 uint32_t 489 GetEffectiveGroupID() const 490 { 491 return m_egid; 492 } 493 494 bool 495 EffectiveUserIDIsValid () const 496 { 497 return m_euid != UINT32_MAX; 498 } 499 500 bool 501 EffectiveGroupIDIsValid () const 502 { 503 return m_egid != UINT32_MAX; 504 } 505 506 void 507 SetEffectiveUserID (uint32_t uid) 508 { 509 m_euid = uid; 510 } 511 512 void 513 SetEffectiveGroupID (uint32_t gid) 514 { 515 m_egid = gid; 516 } 517 518 lldb::pid_t 519 GetParentProcessID () const 520 { 521 return m_parent_pid; 522 } 523 524 void 525 SetParentProcessID (lldb::pid_t pid) 526 { 527 m_parent_pid = pid; 528 } 529 530 bool 531 ParentProcessIDIsValid() const 532 { 533 return m_parent_pid != LLDB_INVALID_PROCESS_ID; 534 } 535 536 void 537 Dump (Stream &s, Platform *platform) const; 538 539 static void 540 DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose); 541 542 void 543 DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const; 544 545protected: 546 uint32_t m_euid; 547 uint32_t m_egid; 548 lldb::pid_t m_parent_pid; 549}; 550 551 552//---------------------------------------------------------------------- 553// ProcessLaunchInfo 554// 555// Describes any information that is required to launch a process. 556//---------------------------------------------------------------------- 557 558class ProcessLaunchInfo : public ProcessInfo 559{ 560public: 561 562 class FileAction 563 { 564 public: 565 enum Action 566 { 567 eFileActionNone, 568 eFileActionClose, 569 eFileActionDuplicate, 570 eFileActionOpen 571 }; 572 573 574 FileAction () : 575 m_action (eFileActionNone), 576 m_fd (-1), 577 m_arg (-1), 578 m_path () 579 { 580 } 581 582 void 583 Clear() 584 { 585 m_action = eFileActionNone; 586 m_fd = -1; 587 m_arg = -1; 588 m_path.clear(); 589 } 590 591 bool 592 Close (int fd); 593 594 bool 595 Duplicate (int fd, int dup_fd); 596 597 bool 598 Open (int fd, const char *path, bool read, bool write); 599 600 static bool 601 AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions, 602 const FileAction *info, 603 Log *log, 604 Error& error); 605 606 int 607 GetFD () const 608 { 609 return m_fd; 610 } 611 612 Action 613 GetAction () const 614 { 615 return m_action; 616 } 617 618 int 619 GetActionArgument () const 620 { 621 return m_arg; 622 } 623 624 const char * 625 GetPath () const 626 { 627 if (m_path.empty()) 628 return NULL; 629 return m_path.c_str(); 630 } 631 632 protected: 633 Action m_action; // The action for this file 634 int m_fd; // An existing file descriptor 635 int m_arg; // oflag for eFileActionOpen*, dup_fd for eFileActionDuplicate 636 std::string m_path; // A file path to use for opening after fork or posix_spawn 637 }; 638 639 ProcessLaunchInfo () : 640 ProcessInfo(), 641 m_flags () 642 { 643 } 644 645 ProcessLaunchInfo (const char *stdin_path, 646 const char *stdout_path, 647 const char *stderr_path, 648 const char *working_directory, 649 uint32_t launch_flags) : 650 ProcessInfo(), 651 m_flags (launch_flags) 652 { 653 if (stderr_path) 654 { 655 ProcessLaunchInfo::FileAction file_action; 656 const bool read = true; 657 const bool write = true; 658 if (file_action.Open(STDERR_FILENO, stderr_path, read, write)) 659 AppendFileAction (file_action); 660 } 661 if (stdout_path) 662 { 663 ProcessLaunchInfo::FileAction file_action; 664 const bool read = false; 665 const bool write = true; 666 if (file_action.Open(STDOUT_FILENO, stdout_path, read, write)) 667 AppendFileAction (file_action); 668 } 669 if (stdin_path) 670 { 671 ProcessLaunchInfo::FileAction file_action; 672 const bool read = true; 673 const bool write = false; 674 if (file_action.Open(STDIN_FILENO, stdin_path, read, write)) 675 AppendFileAction (file_action); 676 } 677 if (working_directory) 678 SetWorkingDirectory(working_directory); 679 } 680 void 681 AppendFileAction (const FileAction &info) 682 { 683 m_file_actions.push_back(info); 684 } 685 686 void 687 AppendCloseFileAction (int fd) 688 { 689 FileAction file_action; 690 file_action.Close (fd); 691 AppendFileAction (file_action); 692 } 693 694 void 695 AppendDuplciateFileAction (int fd, int dup_fd) 696 { 697 FileAction file_action; 698 file_action.Duplicate (fd, dup_fd); 699 AppendFileAction (file_action); 700 } 701 702 void 703 AppendOpenFileAction (int fd, const char *path, bool read, bool write) 704 { 705 FileAction file_action; 706 file_action.Open (fd, path, read, write); 707 AppendFileAction (file_action); 708 } 709 710 void 711 AppendSuppressFileAction (int fd, bool read, bool write) 712 { 713 FileAction file_action; 714 file_action.Open (fd, "/dev/null", read, write); 715 AppendFileAction (file_action); 716 } 717 718 size_t 719 GetNumFileActions () const 720 { 721 return m_file_actions.size(); 722 } 723 724 const FileAction * 725 GetFileActionAtIndex (size_t idx) const 726 { 727 if (idx < m_file_actions.size()) 728 return &m_file_actions[idx]; 729 return NULL; 730 } 731 732 const FileAction * 733 GetFileActionForFD (int fd) const 734 { 735 for (uint32_t idx=0, count=m_file_actions.size(); idx < count; ++idx) 736 { 737 if (m_file_actions[idx].GetFD () == fd) 738 return &m_file_actions[idx]; 739 } 740 return NULL; 741 } 742 743 Flags & 744 GetFlags () 745 { 746 return m_flags; 747 } 748 749 const Flags & 750 GetFlags () const 751 { 752 return m_flags; 753 } 754 755 const char * 756 GetWorkingDirectory () const 757 { 758 if (m_working_dir.empty()) 759 return NULL; 760 return m_working_dir.c_str(); 761 } 762 763 void 764 SetWorkingDirectory (const char *working_dir) 765 { 766 if (working_dir && working_dir[0]) 767 m_working_dir.assign (working_dir); 768 else 769 m_working_dir.clear(); 770 } 771 772 void 773 SwapWorkingDirectory (std::string &working_dir) 774 { 775 m_working_dir.swap (working_dir); 776 } 777 778 779 const char * 780 GetProcessPluginName () const 781 { 782 if (m_plugin_name.empty()) 783 return NULL; 784 return m_plugin_name.c_str(); 785 } 786 787 void 788 SetProcessPluginName (const char *plugin) 789 { 790 if (plugin && plugin[0]) 791 m_plugin_name.assign (plugin); 792 else 793 m_plugin_name.clear(); 794 } 795 796 void 797 Clear () 798 { 799 ProcessInfo::Clear(); 800 m_working_dir.clear(); 801 m_plugin_name.clear(); 802 m_flags.Clear(); 803 m_file_actions.clear(); 804 } 805 806protected: 807 std::string m_working_dir; 808 std::string m_plugin_name; 809 Flags m_flags; // Bitwise OR of bits from lldb::LaunchFlags 810 std::vector<FileAction> m_file_actions; // File actions for any other files 811}; 812 813class ProcessLaunchCommandOptions : public Options 814{ 815public: 816 817 ProcessLaunchCommandOptions (CommandInterpreter &interpreter) : 818 Options(interpreter) 819 { 820 // Keep default values of all options in one place: OptionParsingStarting () 821 OptionParsingStarting (); 822 } 823 824 ~ProcessLaunchCommandOptions () 825 { 826 } 827 828 Error 829 SetOptionValue (uint32_t option_idx, const char *option_arg); 830 831 void 832 OptionParsingStarting () 833 { 834 launch_info.Clear(); 835 } 836 837 const OptionDefinition* 838 GetDefinitions () 839 { 840 return g_option_table; 841 } 842 843 // Options table: Required for subclasses of Options. 844 845 static OptionDefinition g_option_table[]; 846 847 // Instance variables to hold the values for command options. 848 849 ProcessLaunchInfo launch_info; 850}; 851 852//---------------------------------------------------------------------- 853// ProcessInstanceInfoMatch 854// 855// A class to help matching one ProcessInstanceInfo to another. 856//---------------------------------------------------------------------- 857 858class ProcessInstanceInfoMatch 859{ 860public: 861 ProcessInstanceInfoMatch () : 862 m_match_info (), 863 m_name_match_type (lldb_private::eNameMatchIgnore), 864 m_match_all_users (false) 865 { 866 } 867 868 ProcessInstanceInfoMatch (const char *process_name, 869 lldb_private::NameMatchType process_name_match_type) : 870 m_match_info (), 871 m_name_match_type (process_name_match_type), 872 m_match_all_users (false) 873 { 874 m_match_info.SetName (process_name); 875 } 876 877 ProcessInstanceInfo & 878 GetProcessInfo () 879 { 880 return m_match_info; 881 } 882 883 const ProcessInstanceInfo & 884 GetProcessInfo () const 885 { 886 return m_match_info; 887 } 888 889 bool 890 GetMatchAllUsers () const 891 { 892 return m_match_all_users; 893 } 894 895 void 896 SetMatchAllUsers (bool b) 897 { 898 m_match_all_users = b; 899 } 900 901 lldb_private::NameMatchType 902 GetNameMatchType () const 903 { 904 return m_name_match_type; 905 } 906 907 void 908 SetNameMatchType (lldb_private::NameMatchType name_match_type) 909 { 910 m_name_match_type = name_match_type; 911 } 912 913 bool 914 NameMatches (const char *process_name) const; 915 916 bool 917 Matches (const ProcessInstanceInfo &proc_info) const; 918 919 bool 920 MatchAllProcesses () const; 921 void 922 Clear (); 923 924protected: 925 ProcessInstanceInfo m_match_info; 926 lldb_private::NameMatchType m_name_match_type; 927 bool m_match_all_users; 928}; 929 930class ProcessInstanceInfoList 931{ 932public: 933 ProcessInstanceInfoList () : 934 m_infos() 935 { 936 } 937 938 void 939 Clear() 940 { 941 m_infos.clear(); 942 } 943 944 uint32_t 945 GetSize() 946 { 947 return m_infos.size(); 948 } 949 950 void 951 Append (const ProcessInstanceInfo &info) 952 { 953 m_infos.push_back (info); 954 } 955 956 const char * 957 GetProcessNameAtIndex (uint32_t idx) 958 { 959 if (idx < m_infos.size()) 960 return m_infos[idx].GetName(); 961 return NULL; 962 } 963 964 size_t 965 GetProcessNameLengthAtIndex (uint32_t idx) 966 { 967 if (idx < m_infos.size()) 968 return m_infos[idx].GetNameLength(); 969 return 0; 970 } 971 972 lldb::pid_t 973 GetProcessIDAtIndex (uint32_t idx) 974 { 975 if (idx < m_infos.size()) 976 return m_infos[idx].GetProcessID(); 977 return 0; 978 } 979 980 bool 981 GetInfoAtIndex (uint32_t idx, ProcessInstanceInfo &info) 982 { 983 if (idx < m_infos.size()) 984 { 985 info = m_infos[idx]; 986 return true; 987 } 988 return false; 989 } 990 991 // You must ensure "idx" is valid before calling this function 992 const ProcessInstanceInfo & 993 GetProcessInfoAtIndex (uint32_t idx) const 994 { 995 assert (idx < m_infos.size()); 996 return m_infos[idx]; 997 } 998 999protected: 1000 typedef std::vector<ProcessInstanceInfo> collection; 1001 collection m_infos; 1002}; 1003 1004 1005// This class tracks the Modification state of the process. Things that can currently modify 1006// the program are running the program (which will up the StopID) and writing memory (which 1007// will up the MemoryID.) 1008// FIXME: Should we also include modification of register states? 1009 1010class ProcessModID 1011{ 1012friend bool operator== (const ProcessModID &lhs, const ProcessModID &rhs); 1013public: 1014 ProcessModID () : 1015 m_stop_id (0), 1016 m_memory_id (0) 1017 {} 1018 1019 ProcessModID (const ProcessModID &rhs) : 1020 m_stop_id (rhs.m_stop_id), 1021 m_memory_id (rhs.m_memory_id) 1022 {} 1023 1024 const ProcessModID & operator= (const ProcessModID &rhs) 1025 { 1026 if (this != &rhs) 1027 { 1028 m_stop_id = rhs.m_stop_id; 1029 m_memory_id = rhs.m_memory_id; 1030 } 1031 return *this; 1032 } 1033 1034 ~ProcessModID () {} 1035 1036 void BumpStopID () { m_stop_id++; } 1037 void BumpMemoryID () { m_memory_id++; } 1038 1039 uint32_t GetStopID() const { return m_stop_id; } 1040 uint32_t GetMemoryID () const { return m_memory_id; } 1041 1042 bool MemoryIDEqual (const ProcessModID &compare) const 1043 { 1044 return m_memory_id == compare.m_memory_id; 1045 } 1046 1047 bool StopIDEqual (const ProcessModID &compare) const 1048 { 1049 return m_stop_id == compare.m_stop_id; 1050 } 1051 1052 void SetInvalid () 1053 { 1054 m_stop_id = UINT32_MAX; 1055 } 1056 1057 bool IsValid () const 1058 { 1059 return m_stop_id != UINT32_MAX; 1060 } 1061private: 1062 uint32_t m_stop_id; 1063 uint32_t m_memory_id; 1064}; 1065inline bool operator== (const ProcessModID &lhs, const ProcessModID &rhs) 1066{ 1067 if (lhs.StopIDEqual (rhs) 1068 && lhs.MemoryIDEqual (rhs)) 1069 return true; 1070 else 1071 return false; 1072} 1073 1074inline bool operator!= (const ProcessModID &lhs, const ProcessModID &rhs) 1075{ 1076 if (!lhs.StopIDEqual (rhs) 1077 || !lhs.MemoryIDEqual (rhs)) 1078 return true; 1079 else 1080 return false; 1081} 1082 1083//---------------------------------------------------------------------- 1084/// @class Process Process.h "lldb/Target/Process.h" 1085/// @brief A plug-in interface definition class for debugging a process. 1086//---------------------------------------------------------------------- 1087class Process : 1088 public ReferenceCountedBaseVirtual<Process>, 1089 public UserID, 1090 public Broadcaster, 1091 public ExecutionContextScope, 1092 public PluginInterface, 1093 public ProcessInstanceSettings 1094{ 1095friend class ThreadList; 1096friend class ClangFunction; // For WaitForStateChangeEventsPrivate 1097friend class CommandObjectProcessLaunch; 1098friend class ProcessEventData; 1099friend class CommandObjectBreakpointCommand; 1100friend class StopInfo; 1101 1102public: 1103 1104 //------------------------------------------------------------------ 1105 /// Broadcaster event bits definitions. 1106 //------------------------------------------------------------------ 1107 enum 1108 { 1109 eBroadcastBitStateChanged = (1 << 0), 1110 eBroadcastBitInterrupt = (1 << 1), 1111 eBroadcastBitSTDOUT = (1 << 2), 1112 eBroadcastBitSTDERR = (1 << 3) 1113 }; 1114 1115 enum 1116 { 1117 eBroadcastInternalStateControlStop = (1<<0), 1118 eBroadcastInternalStateControlPause = (1<<1), 1119 eBroadcastInternalStateControlResume = (1<<2) 1120 }; 1121 1122 //------------------------------------------------------------------ 1123 /// A notification structure that can be used by clients to listen 1124 /// for changes in a process's lifetime. 1125 /// 1126 /// @see RegisterNotificationCallbacks (const Notifications&) 1127 /// @see UnregisterNotificationCallbacks (const Notifications&) 1128 //------------------------------------------------------------------ 1129#ifndef SWIG 1130 typedef struct 1131 { 1132 void *baton; 1133 void (*initialize)(void *baton, Process *process); 1134 void (*process_state_changed) (void *baton, Process *process, lldb::StateType state); 1135 } Notifications; 1136 1137 class ProcessEventData : 1138 public EventData 1139 { 1140 friend class Process; 1141 1142 public: 1143 ProcessEventData (); 1144 ProcessEventData (const lldb::ProcessSP &process, lldb::StateType state); 1145 1146 virtual ~ProcessEventData(); 1147 1148 static const ConstString & 1149 GetFlavorString (); 1150 1151 virtual const ConstString & 1152 GetFlavor () const; 1153 1154 const lldb::ProcessSP & 1155 GetProcessSP() const 1156 { 1157 return m_process_sp; 1158 } 1159 lldb::StateType 1160 GetState() const 1161 { 1162 return m_state; 1163 } 1164 bool 1165 GetRestarted () const 1166 { 1167 return m_restarted; 1168 } 1169 bool 1170 GetInterrupted () const 1171 { 1172 return m_interrupted; 1173 } 1174 1175 virtual void 1176 Dump (Stream *s) const; 1177 1178 virtual void 1179 DoOnRemoval (Event *event_ptr); 1180 1181 static const Process::ProcessEventData * 1182 GetEventDataFromEvent (const Event *event_ptr); 1183 1184 static lldb::ProcessSP 1185 GetProcessFromEvent (const Event *event_ptr); 1186 1187 static lldb::StateType 1188 GetStateFromEvent (const Event *event_ptr); 1189 1190 static bool 1191 GetRestartedFromEvent (const Event *event_ptr); 1192 1193 static void 1194 SetRestartedInEvent (Event *event_ptr, bool new_value); 1195 1196 static bool 1197 GetInterruptedFromEvent (const Event *event_ptr); 1198 1199 static void 1200 SetInterruptedInEvent (Event *event_ptr, bool new_value); 1201 1202 static bool 1203 SetUpdateStateOnRemoval (Event *event_ptr); 1204 1205 private: 1206 1207 void 1208 SetUpdateStateOnRemoval() 1209 { 1210 m_update_state++; 1211 } 1212 void 1213 SetRestarted (bool new_value) 1214 { 1215 m_restarted = new_value; 1216 } 1217 void 1218 SetInterrupted (bool new_value) 1219 { 1220 m_interrupted = new_value; 1221 } 1222 1223 lldb::ProcessSP m_process_sp; 1224 lldb::StateType m_state; 1225 bool m_restarted; // For "eStateStopped" events, this is true if the target was automatically restarted. 1226 int m_update_state; 1227 bool m_interrupted; 1228 DISALLOW_COPY_AND_ASSIGN (ProcessEventData); 1229 1230 }; 1231 1232 class SettingsController : public UserSettingsController 1233 { 1234 public: 1235 1236 SettingsController (); 1237 1238 virtual 1239 ~SettingsController (); 1240 1241 static SettingEntry global_settings_table[]; 1242 static SettingEntry instance_settings_table[]; 1243 1244 protected: 1245 1246 lldb::InstanceSettingsSP 1247 CreateInstanceSettings (const char *instance_name); 1248 1249 private: 1250 1251 // Class-wide settings. 1252 1253 DISALLOW_COPY_AND_ASSIGN (SettingsController); 1254 }; 1255 1256 1257#endif 1258 1259 static void 1260 SettingsInitialize (); 1261 1262 static void 1263 SettingsTerminate (); 1264 1265 static lldb::UserSettingsControllerSP & 1266 GetSettingsController (); 1267 1268 void 1269 UpdateInstanceName (); 1270 1271 1272 //------------------------------------------------------------------ 1273 /// Construct with a shared pointer to a target, and the Process listener. 1274 //------------------------------------------------------------------ 1275 Process(Target &target, Listener &listener); 1276 1277 //------------------------------------------------------------------ 1278 /// Destructor. 1279 /// 1280 /// The destructor is virtual since this class is designed to be 1281 /// inherited from by the plug-in instance. 1282 //------------------------------------------------------------------ 1283 virtual 1284 ~Process(); 1285 1286 //------------------------------------------------------------------ 1287 /// Find a Process plug-in that can debug \a module using the 1288 /// currently selected architecture. 1289 /// 1290 /// Scans all loaded plug-in interfaces that implement versions of 1291 /// the Process plug-in interface and returns the first instance 1292 /// that can debug the file. 1293 /// 1294 /// @param[in] module_sp 1295 /// The module shared pointer that this process will debug. 1296 /// 1297 /// @param[in] plugin_name 1298 /// If NULL, select the best plug-in for the binary. If non-NULL 1299 /// then look for a plugin whose PluginInfo's name matches 1300 /// this string. 1301 /// 1302 /// @see Process::CanDebug () 1303 //------------------------------------------------------------------ 1304 static Process* 1305 FindPlugin (Target &target, const char *plugin_name, Listener &listener); 1306 1307 1308 1309 //------------------------------------------------------------------ 1310 /// Static function that can be used with the \b host function 1311 /// Host::StartMonitoringChildProcess (). 1312 /// 1313 /// This function can be used by lldb_private::Process subclasses 1314 /// when they want to watch for a local process and have its exit 1315 /// status automatically set when the host child process exits. 1316 /// Subclasses should call Host::StartMonitoringChildProcess () 1317 /// with: 1318 /// callback = Process::SetHostProcessExitStatus 1319 /// callback_baton = NULL 1320 /// pid = Process::GetID() 1321 /// monitor_signals = false 1322 //------------------------------------------------------------------ 1323 static bool 1324 SetProcessExitStatus (void *callback_baton, // The callback baton which should be set to NULL 1325 lldb::pid_t pid, // The process ID we want to monitor 1326 int signo, // Zero for no signal 1327 int status); // Exit value of process if signal is zero 1328 1329 lldb::ByteOrder 1330 GetByteOrder () const; 1331 1332 uint32_t 1333 GetAddressByteSize () const; 1334 1335 //------------------------------------------------------------------ 1336 /// Check if a plug-in instance can debug the file in \a module. 1337 /// 1338 /// Each plug-in is given a chance to say whether it can debug 1339 /// the file in \a module. If the Process plug-in instance can 1340 /// debug a file on the current system, it should return \b true. 1341 /// 1342 /// @return 1343 /// Returns \b true if this Process plug-in instance can 1344 /// debug the executable, \b false otherwise. 1345 //------------------------------------------------------------------ 1346 virtual bool 1347 CanDebug (Target &target, 1348 bool plugin_specified_by_name) = 0; 1349 1350 1351 //------------------------------------------------------------------ 1352 /// This object is about to be destroyed, do any necessary cleanup. 1353 /// 1354 /// Subclasses that override this method should always call this 1355 /// superclass method. 1356 //------------------------------------------------------------------ 1357 virtual void 1358 Finalize(); 1359 1360 //------------------------------------------------------------------ 1361 /// Launch a new process. 1362 /// 1363 /// Launch a new process by spawning a new process using the 1364 /// target object's executable module's file as the file to launch. 1365 /// Arguments are given in \a argv, and the environment variables 1366 /// are in \a envp. Standard input and output files can be 1367 /// optionally re-directed to \a stdin_path, \a stdout_path, and 1368 /// \a stderr_path. 1369 /// 1370 /// This function is not meant to be overridden by Process 1371 /// subclasses. It will first call Process::WillLaunch (Module *) 1372 /// and if that returns \b true, Process::DoLaunch (Module*, 1373 /// char const *[],char const *[],const char *,const char *, 1374 /// const char *) will be called to actually do the launching. If 1375 /// DoLaunch returns \b true, then Process::DidLaunch() will be 1376 /// called. 1377 /// 1378 /// @param[in] argv 1379 /// The argument array. 1380 /// 1381 /// @param[in] envp 1382 /// The environment array. 1383 /// 1384 /// @param[in] launch_flags 1385 /// Flags to modify the launch (@see lldb::LaunchFlags) 1386 /// 1387 /// @param[in] stdin_path 1388 /// The path to use when re-directing the STDIN of the new 1389 /// process. If all stdXX_path arguments are NULL, a pseudo 1390 /// terminal will be used. 1391 /// 1392 /// @param[in] stdout_path 1393 /// The path to use when re-directing the STDOUT of the new 1394 /// process. If all stdXX_path arguments are NULL, a pseudo 1395 /// terminal will be used. 1396 /// 1397 /// @param[in] stderr_path 1398 /// The path to use when re-directing the STDERR of the new 1399 /// process. If all stdXX_path arguments are NULL, a pseudo 1400 /// terminal will be used. 1401 /// 1402 /// @param[in] working_directory 1403 /// The working directory to have the child process run in 1404 /// 1405 /// @return 1406 /// An error object. Call GetID() to get the process ID if 1407 /// the error object is success. 1408 //------------------------------------------------------------------ 1409 virtual Error 1410 Launch (const ProcessLaunchInfo &launch_info); 1411 1412 //------------------------------------------------------------------ 1413 /// Attach to an existing process using a process ID. 1414 /// 1415 /// This function is not meant to be overridden by Process 1416 /// subclasses. It will first call Process::WillAttach (lldb::pid_t) 1417 /// and if that returns \b true, Process::DoAttach (lldb::pid_t) will 1418 /// be called to actually do the attach. If DoAttach returns \b 1419 /// true, then Process::DidAttach() will be called. 1420 /// 1421 /// @param[in] pid 1422 /// The process ID that we should attempt to attach to. 1423 /// 1424 /// @return 1425 /// Returns \a pid if attaching was successful, or 1426 /// LLDB_INVALID_PROCESS_ID if attaching fails. 1427 //------------------------------------------------------------------ 1428 virtual Error 1429 Attach (lldb::pid_t pid); 1430 1431 //------------------------------------------------------------------ 1432 /// Attach to an existing process by process name. 1433 /// 1434 /// This function is not meant to be overridden by Process 1435 /// subclasses. It will first call 1436 /// Process::WillAttach (const char *) and if that returns \b 1437 /// true, Process::DoAttach (const char *) will be called to 1438 /// actually do the attach. If DoAttach returns \b true, then 1439 /// Process::DidAttach() will be called. 1440 /// 1441 /// @param[in] process_name 1442 /// A process name to match against the current process list. 1443 /// 1444 /// @return 1445 /// Returns \a pid if attaching was successful, or 1446 /// LLDB_INVALID_PROCESS_ID if attaching fails. 1447 //------------------------------------------------------------------ 1448 virtual Error 1449 Attach (const char *process_name, bool wait_for_launch); 1450 1451 virtual Error 1452 ConnectRemote (const char *remote_url); 1453 1454 bool 1455 AttachedToProcess() const 1456 { 1457 return m_attached_to_process; 1458 } 1459 1460 //------------------------------------------------------------------ 1461 /// Get the image information address for the current process. 1462 /// 1463 /// Some runtimes have system functions that can help dynamic 1464 /// loaders locate the dynamic loader information needed to observe 1465 /// shared libraries being loaded or unloaded. This function is 1466 /// in the Process interface (as opposed to the DynamicLoader 1467 /// interface) to ensure that remote debugging can take advantage of 1468 /// this functionality. 1469 /// 1470 /// @return 1471 /// The address of the dynamic loader information, or 1472 /// LLDB_INVALID_ADDRESS if this is not supported by this 1473 /// interface. 1474 //------------------------------------------------------------------ 1475 virtual lldb::addr_t 1476 GetImageInfoAddress (); 1477 1478 //------------------------------------------------------------------ 1479 /// Load a shared library into this process. 1480 /// 1481 /// Try and load a shared library into the current process. This 1482 /// call might fail in the dynamic loader plug-in says it isn't safe 1483 /// to try and load shared libraries at the moment. 1484 /// 1485 /// @param[in] image_spec 1486 /// The image file spec that points to the shared library that 1487 /// you want to load. 1488 /// 1489 /// @param[out] error 1490 /// An error object that gets filled in with any errors that 1491 /// might occur when trying to load the shared library. 1492 /// 1493 /// @return 1494 /// A token that represents the shared library that can be 1495 /// later used to unload the shared library. A value of 1496 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 1497 /// library can't be opened. 1498 //------------------------------------------------------------------ 1499 virtual uint32_t 1500 LoadImage (const FileSpec &image_spec, Error &error); 1501 1502 virtual Error 1503 UnloadImage (uint32_t image_token); 1504 1505 //------------------------------------------------------------------ 1506 /// Register for process and thread notifications. 1507 /// 1508 /// Clients can register nofication callbacks by filling out a 1509 /// Process::Notifications structure and calling this function. 1510 /// 1511 /// @param[in] callbacks 1512 /// A structure that contains the notification baton and 1513 /// callback functions. 1514 /// 1515 /// @see Process::Notifications 1516 //------------------------------------------------------------------ 1517#ifndef SWIG 1518 void 1519 RegisterNotificationCallbacks (const Process::Notifications& callbacks); 1520#endif 1521 //------------------------------------------------------------------ 1522 /// Unregister for process and thread notifications. 1523 /// 1524 /// Clients can unregister nofication callbacks by passing a copy of 1525 /// the original baton and callbacks in \a callbacks. 1526 /// 1527 /// @param[in] callbacks 1528 /// A structure that contains the notification baton and 1529 /// callback functions. 1530 /// 1531 /// @return 1532 /// Returns \b true if the notification callbacks were 1533 /// successfully removed from the process, \b false otherwise. 1534 /// 1535 /// @see Process::Notifications 1536 //------------------------------------------------------------------ 1537#ifndef SWIG 1538 bool 1539 UnregisterNotificationCallbacks (const Process::Notifications& callbacks); 1540#endif 1541 //================================================================== 1542 // Built in Process Control functions 1543 //================================================================== 1544 //------------------------------------------------------------------ 1545 /// Resumes all of a process's threads as configured using the 1546 /// Thread run control functions. 1547 /// 1548 /// Threads for a process should be updated with one of the run 1549 /// control actions (resume, step, or suspend) that they should take 1550 /// when the process is resumed. If no run control action is given 1551 /// to a thread it will be resumed by default. 1552 /// 1553 /// This function is not meant to be overridden by Process 1554 /// subclasses. This function will take care of disabling any 1555 /// breakpoints that threads may be stopped at, single stepping, and 1556 /// re-enabling breakpoints, and enabling the basic flow control 1557 /// that the plug-in instances need not worry about. 1558 /// 1559 /// @return 1560 /// Returns an error object. 1561 /// 1562 /// @see Thread:Resume() 1563 /// @see Thread:Step() 1564 /// @see Thread:Suspend() 1565 //------------------------------------------------------------------ 1566 Error 1567 Resume (); 1568 1569 //------------------------------------------------------------------ 1570 /// Halts a running process. 1571 /// 1572 /// This function is not meant to be overridden by Process 1573 /// subclasses. 1574 /// If the process is successfully halted, a eStateStopped 1575 /// process event with GetInterrupted will be broadcast. If false, we will 1576 /// halt the process with no events generated by the halt. 1577 /// 1578 /// @return 1579 /// Returns an error object. If the error is empty, the process is halted. 1580 /// otherwise the halt has failed. 1581 //------------------------------------------------------------------ 1582 Error 1583 Halt (); 1584 1585 //------------------------------------------------------------------ 1586 /// Detaches from a running or stopped process. 1587 /// 1588 /// This function is not meant to be overridden by Process 1589 /// subclasses. 1590 /// 1591 /// @return 1592 /// Returns an error object. 1593 //------------------------------------------------------------------ 1594 Error 1595 Detach (); 1596 1597 //------------------------------------------------------------------ 1598 /// Kills the process and shuts down all threads that were spawned 1599 /// to track and monitor the process. 1600 /// 1601 /// This function is not meant to be overridden by Process 1602 /// subclasses. 1603 /// 1604 /// @return 1605 /// Returns an error object. 1606 //------------------------------------------------------------------ 1607 Error 1608 Destroy(); 1609 1610 //------------------------------------------------------------------ 1611 /// Sends a process a UNIX signal \a signal. 1612 /// 1613 /// This function is not meant to be overridden by Process 1614 /// subclasses. 1615 /// 1616 /// @return 1617 /// Returns an error object. 1618 //------------------------------------------------------------------ 1619 Error 1620 Signal (int signal); 1621 1622 virtual UnixSignals & 1623 GetUnixSignals () 1624 { 1625 return m_unix_signals; 1626 } 1627 1628 //================================================================== 1629 // Plug-in Process Control Overrides 1630 //================================================================== 1631 1632 //------------------------------------------------------------------ 1633 /// Called before attaching to a process. 1634 /// 1635 /// Allow Process plug-ins to execute some code before attaching a 1636 /// process. 1637 /// 1638 /// @return 1639 /// Returns an error object. 1640 //------------------------------------------------------------------ 1641 virtual Error 1642 WillAttachToProcessWithID (lldb::pid_t pid) 1643 { 1644 return Error(); 1645 } 1646 1647 //------------------------------------------------------------------ 1648 /// Called before attaching to a process. 1649 /// 1650 /// Allow Process plug-ins to execute some code before attaching a 1651 /// process. 1652 /// 1653 /// @return 1654 /// Returns an error object. 1655 //------------------------------------------------------------------ 1656 virtual Error 1657 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) 1658 { 1659 return Error(); 1660 } 1661 1662 virtual Error 1663 DoConnectRemote (const char *remote_url) 1664 { 1665 Error error; 1666 error.SetErrorString ("remote connections are not supported"); 1667 return error; 1668 } 1669 1670 //------------------------------------------------------------------ 1671 /// Attach to an existing process using a process ID. 1672 /// 1673 /// @param[in] pid 1674 /// The process ID that we should attempt to attach to. 1675 /// 1676 /// @return 1677 /// Returns \a pid if attaching was successful, or 1678 /// LLDB_INVALID_PROCESS_ID if attaching fails. 1679 //------------------------------------------------------------------ 1680 virtual Error 1681 DoAttachToProcessWithID (lldb::pid_t pid) = 0; 1682 1683 //------------------------------------------------------------------ 1684 /// Attach to an existing process using a partial process name. 1685 /// 1686 /// @param[in] process_name 1687 /// The name of the process to attach to. 1688 /// 1689 /// @param[in] wait_for_launch 1690 /// If \b true, wait for the process to be launched and attach 1691 /// as soon as possible after it does launch. If \b false, then 1692 /// search for a matching process the currently exists. 1693 /// 1694 /// @return 1695 /// Returns \a pid if attaching was successful, or 1696 /// LLDB_INVALID_PROCESS_ID if attaching fails. 1697 //------------------------------------------------------------------ 1698 virtual Error 1699 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch) 1700 { 1701 Error error; 1702 error.SetErrorString("attach by name is not supported"); 1703 return error; 1704 } 1705 1706 //------------------------------------------------------------------ 1707 /// Called after attaching a process. 1708 /// 1709 /// Allow Process plug-ins to execute some code after attaching to 1710 /// a process. 1711 //------------------------------------------------------------------ 1712 virtual void 1713 DidAttach () {} 1714 1715 1716 //------------------------------------------------------------------ 1717 /// Called before launching to a process. 1718 /// 1719 /// Allow Process plug-ins to execute some code before launching a 1720 /// process. 1721 /// 1722 /// @return 1723 /// Returns an error object. 1724 //------------------------------------------------------------------ 1725 virtual Error 1726 WillLaunch (Module* module) 1727 { 1728 return Error(); 1729 } 1730 1731 //------------------------------------------------------------------ 1732 /// Launch a new process. 1733 /// 1734 /// Launch a new process by spawning a new process using \a module's 1735 /// file as the file to launch. Arguments are given in \a argv, 1736 /// and the environment variables are in \a envp. Standard input 1737 /// and output files can be optionally re-directed to \a stdin_path, 1738 /// \a stdout_path, and \a stderr_path. 1739 /// 1740 /// @param[in] module 1741 /// The module from which to extract the file specification and 1742 /// launch. 1743 /// 1744 /// @param[in] argv 1745 /// The argument array. 1746 /// 1747 /// @param[in] envp 1748 /// The environment array. 1749 /// 1750 /// @param[in] launch_flags 1751 /// Flags to modify the launch (@see lldb::LaunchFlags) 1752 /// 1753 /// @param[in] stdin_path 1754 /// The path to use when re-directing the STDIN of the new 1755 /// process. If all stdXX_path arguments are NULL, a pseudo 1756 /// terminal will be used. 1757 /// 1758 /// @param[in] stdout_path 1759 /// The path to use when re-directing the STDOUT of the new 1760 /// process. If all stdXX_path arguments are NULL, a pseudo 1761 /// terminal will be used. 1762 /// 1763 /// @param[in] stderr_path 1764 /// The path to use when re-directing the STDERR of the new 1765 /// process. If all stdXX_path arguments are NULL, a pseudo 1766 /// terminal will be used. 1767 /// 1768 /// @param[in] working_directory 1769 /// The working directory to have the child process run in 1770 /// 1771 /// @return 1772 /// A new valid process ID, or LLDB_INVALID_PROCESS_ID if 1773 /// launching fails. 1774 //------------------------------------------------------------------ 1775 virtual Error 1776 DoLaunch (Module *exe_module, 1777 const ProcessLaunchInfo &launch_info) = 0; 1778 1779 //------------------------------------------------------------------ 1780 /// Called after launching a process. 1781 /// 1782 /// Allow Process plug-ins to execute some code after launching 1783 /// a process. 1784 //------------------------------------------------------------------ 1785 virtual void 1786 DidLaunch () {} 1787 1788 1789 1790 //------------------------------------------------------------------ 1791 /// Called before resuming to a process. 1792 /// 1793 /// Allow Process plug-ins to execute some code before resuming a 1794 /// process. 1795 /// 1796 /// @return 1797 /// Returns an error object. 1798 //------------------------------------------------------------------ 1799 virtual Error 1800 WillResume () { return Error(); } 1801 1802 //------------------------------------------------------------------ 1803 /// Resumes all of a process's threads as configured using the 1804 /// Thread run control functions. 1805 /// 1806 /// Threads for a process should be updated with one of the run 1807 /// control actions (resume, step, or suspend) that they should take 1808 /// when the process is resumed. If no run control action is given 1809 /// to a thread it will be resumed by default. 1810 /// 1811 /// @return 1812 /// Returns \b true if the process successfully resumes using 1813 /// the thread run control actions, \b false otherwise. 1814 /// 1815 /// @see Thread:Resume() 1816 /// @see Thread:Step() 1817 /// @see Thread:Suspend() 1818 //------------------------------------------------------------------ 1819 virtual Error 1820 DoResume () = 0; 1821 1822 //------------------------------------------------------------------ 1823 /// Called after resuming a process. 1824 /// 1825 /// Allow Process plug-ins to execute some code after resuming 1826 /// a process. 1827 //------------------------------------------------------------------ 1828 virtual void 1829 DidResume () {} 1830 1831 1832 //------------------------------------------------------------------ 1833 /// Called before halting to a process. 1834 /// 1835 /// Allow Process plug-ins to execute some code before halting a 1836 /// process. 1837 /// 1838 /// @return 1839 /// Returns an error object. 1840 //------------------------------------------------------------------ 1841 virtual Error 1842 WillHalt () { return Error(); } 1843 1844 //------------------------------------------------------------------ 1845 /// Halts a running process. 1846 /// 1847 /// DoHalt must produce one and only one stop StateChanged event if it actually 1848 /// stops the process. If the stop happens through some natural event (for 1849 /// instance a SIGSTOP), then forwarding that event will do. Otherwise, you must 1850 /// generate the event manually. Note also, the private event thread is stopped when 1851 /// DoHalt is run to prevent the events generated while halting to trigger 1852 /// other state changes before the halt is complete. 1853 /// 1854 /// @param[out] caused_stop 1855 /// If true, then this Halt caused the stop, otherwise, the 1856 /// process was already stopped. 1857 /// 1858 /// @return 1859 /// Returns \b true if the process successfully halts, \b false 1860 /// otherwise. 1861 //------------------------------------------------------------------ 1862 virtual Error 1863 DoHalt (bool &caused_stop) = 0; 1864 1865 //------------------------------------------------------------------ 1866 /// Called after halting a process. 1867 /// 1868 /// Allow Process plug-ins to execute some code after halting 1869 /// a process. 1870 //------------------------------------------------------------------ 1871 virtual void 1872 DidHalt () {} 1873 1874 //------------------------------------------------------------------ 1875 /// Called before detaching from a process. 1876 /// 1877 /// Allow Process plug-ins to execute some code before detaching 1878 /// from a process. 1879 /// 1880 /// @return 1881 /// Returns an error object. 1882 //------------------------------------------------------------------ 1883 virtual Error 1884 WillDetach () 1885 { 1886 return Error(); 1887 } 1888 1889 //------------------------------------------------------------------ 1890 /// Detaches from a running or stopped process. 1891 /// 1892 /// @return 1893 /// Returns \b true if the process successfully detaches, \b 1894 /// false otherwise. 1895 //------------------------------------------------------------------ 1896 virtual Error 1897 DoDetach () = 0; 1898 1899 //------------------------------------------------------------------ 1900 /// Called after detaching from a process. 1901 /// 1902 /// Allow Process plug-ins to execute some code after detaching 1903 /// from a process. 1904 //------------------------------------------------------------------ 1905 virtual void 1906 DidDetach () {} 1907 1908 //------------------------------------------------------------------ 1909 /// Called before sending a signal to a process. 1910 /// 1911 /// Allow Process plug-ins to execute some code before sending a 1912 /// signal to a process. 1913 /// 1914 /// @return 1915 /// Returns no error if it is safe to proceed with a call to 1916 /// Process::DoSignal(int), otherwise an error describing what 1917 /// prevents the signal from being sent. 1918 //------------------------------------------------------------------ 1919 virtual Error 1920 WillSignal () { return Error(); } 1921 1922 //------------------------------------------------------------------ 1923 /// Sends a process a UNIX signal \a signal. 1924 /// 1925 /// @return 1926 /// Returns an error object. 1927 //------------------------------------------------------------------ 1928 virtual Error 1929 DoSignal (int signal) = 0; 1930 1931 1932 1933 virtual Error 1934 WillDestroy () { return Error(); } 1935 1936 virtual Error 1937 DoDestroy () = 0; 1938 1939 virtual void 1940 DidDestroy () { } 1941 1942 1943 //------------------------------------------------------------------ 1944 /// Called after sending a signal to a process. 1945 /// 1946 /// Allow Process plug-ins to execute some code after sending a 1947 /// signal to a process. 1948 //------------------------------------------------------------------ 1949 virtual void 1950 DidSignal () {} 1951 1952 1953 //------------------------------------------------------------------ 1954 /// Currently called as part of ShouldStop. 1955 /// FIXME: Should really happen when the target stops before the 1956 /// event is taken from the queue... 1957 /// 1958 /// This callback is called as the event 1959 /// is about to be queued up to allow Process plug-ins to execute 1960 /// some code prior to clients being notified that a process was 1961 /// stopped. Common operations include updating the thread list, 1962 /// invalidating any thread state (registers, stack, etc) prior to 1963 /// letting the notification go out. 1964 /// 1965 //------------------------------------------------------------------ 1966 virtual void 1967 RefreshStateAfterStop () = 0; 1968 1969 //------------------------------------------------------------------ 1970 /// Get the target object pointer for this module. 1971 /// 1972 /// @return 1973 /// A Target object pointer to the target that owns this 1974 /// module. 1975 //------------------------------------------------------------------ 1976 Target & 1977 GetTarget () 1978 { 1979 return m_target; 1980 } 1981 1982 //------------------------------------------------------------------ 1983 /// Get the const target object pointer for this module. 1984 /// 1985 /// @return 1986 /// A const Target object pointer to the target that owns this 1987 /// module. 1988 //------------------------------------------------------------------ 1989 const Target & 1990 GetTarget () const 1991 { 1992 return m_target; 1993 } 1994 1995 1996 //------------------------------------------------------------------ 1997 /// Get accessor for the current process state. 1998 /// 1999 /// @return 2000 /// The current state of the process. 2001 /// 2002 /// @see lldb::StateType 2003 //------------------------------------------------------------------ 2004 lldb::StateType 2005 GetState (); 2006 2007 ExecutionResults 2008 RunThreadPlan (ExecutionContext &exe_ctx, 2009 lldb::ThreadPlanSP &thread_plan_sp, 2010 bool stop_others, 2011 bool try_all_threads, 2012 bool discard_on_error, 2013 uint32_t single_thread_timeout_usec, 2014 Stream &errors); 2015 2016 static const char * 2017 ExecutionResultAsCString (ExecutionResults result); 2018 2019 void 2020 GetStatus (Stream &ostrm); 2021 2022 size_t 2023 GetThreadStatus (Stream &ostrm, 2024 bool only_threads_with_stop_reason, 2025 uint32_t start_frame, 2026 uint32_t num_frames, 2027 uint32_t num_frames_with_source); 2028 2029protected: 2030 2031 void 2032 SetState (lldb::EventSP &event_sp); 2033 2034 lldb::StateType 2035 GetPrivateState (); 2036 2037 //------------------------------------------------------------------ 2038 // Called internally 2039 //------------------------------------------------------------------ 2040 void 2041 CompleteAttach (); 2042 2043public: 2044 //------------------------------------------------------------------ 2045 /// Get the exit status for a process. 2046 /// 2047 /// @return 2048 /// The process's return code, or -1 if the current process 2049 /// state is not eStateExited. 2050 //------------------------------------------------------------------ 2051 int 2052 GetExitStatus (); 2053 2054 //------------------------------------------------------------------ 2055 /// Get a textual description of what the process exited. 2056 /// 2057 /// @return 2058 /// The textual description of why the process exited, or NULL 2059 /// if there is no description available. 2060 //------------------------------------------------------------------ 2061 const char * 2062 GetExitDescription (); 2063 2064 2065 virtual void 2066 DidExit () 2067 { 2068 } 2069 2070 //------------------------------------------------------------------ 2071 /// Get the Modification ID of the process. 2072 /// 2073 /// @return 2074 /// The modification ID of the process. 2075 //------------------------------------------------------------------ 2076 ProcessModID 2077 GetModID () const 2078 { 2079 return m_mod_id; 2080 } 2081 2082 uint32_t 2083 GetStopID () const 2084 { 2085 return m_mod_id.GetStopID(); 2086 } 2087 2088 //------------------------------------------------------------------ 2089 /// Set accessor for the process exit status (return code). 2090 /// 2091 /// Sometimes a child exits and the exit can be detected by global 2092 /// functions (signal handler for SIGCHLD for example). This 2093 /// accessor allows the exit status to be set from an external 2094 /// source. 2095 /// 2096 /// Setting this will cause a eStateExited event to be posted to 2097 /// the process event queue. 2098 /// 2099 /// @param[in] exit_status 2100 /// The value for the process's return code. 2101 /// 2102 /// @see lldb::StateType 2103 //------------------------------------------------------------------ 2104 virtual bool 2105 SetExitStatus (int exit_status, const char *cstr); 2106 2107 //------------------------------------------------------------------ 2108 /// Check if a process is still alive. 2109 /// 2110 /// @return 2111 /// Returns \b true if the process is still valid, \b false 2112 /// otherwise. 2113 //------------------------------------------------------------------ 2114 virtual bool 2115 IsAlive () = 0; 2116 2117 //------------------------------------------------------------------ 2118 /// Actually do the reading of memory from a process. 2119 /// 2120 /// Subclasses must override this function and can return fewer 2121 /// bytes than requested when memory requests are too large. This 2122 /// class will break up the memory requests and keep advancing the 2123 /// arguments along as needed. 2124 /// 2125 /// @param[in] vm_addr 2126 /// A virtual load address that indicates where to start reading 2127 /// memory from. 2128 /// 2129 /// @param[in] size 2130 /// The number of bytes to read. 2131 /// 2132 /// @param[out] buf 2133 /// A byte buffer that is at least \a size bytes long that 2134 /// will receive the memory bytes. 2135 /// 2136 /// @return 2137 /// The number of bytes that were actually read into \a buf. 2138 //------------------------------------------------------------------ 2139 virtual size_t 2140 DoReadMemory (lldb::addr_t vm_addr, 2141 void *buf, 2142 size_t size, 2143 Error &error) = 0; 2144 2145 //------------------------------------------------------------------ 2146 /// Read of memory from a process. 2147 /// 2148 /// This function will read memory from the current process's 2149 /// address space and remove any traps that may have been inserted 2150 /// into the memory. 2151 /// 2152 /// This function is not meant to be overridden by Process 2153 /// subclasses, the subclasses should implement 2154 /// Process::DoReadMemory (lldb::addr_t, size_t, void *). 2155 /// 2156 /// @param[in] vm_addr 2157 /// A virtual load address that indicates where to start reading 2158 /// memory from. 2159 /// 2160 /// @param[out] buf 2161 /// A byte buffer that is at least \a size bytes long that 2162 /// will receive the memory bytes. 2163 /// 2164 /// @param[in] size 2165 /// The number of bytes to read. 2166 /// 2167 /// @return 2168 /// The number of bytes that were actually read into \a buf. If 2169 /// the returned number is greater than zero, yet less than \a 2170 /// size, then this function will get called again with \a 2171 /// vm_addr, \a buf, and \a size updated appropriately. Zero is 2172 /// returned to indicate an error. 2173 //------------------------------------------------------------------ 2174 size_t 2175 ReadMemory (lldb::addr_t vm_addr, 2176 void *buf, 2177 size_t size, 2178 Error &error); 2179 2180 //------------------------------------------------------------------ 2181 /// Read a NULL terminated C string from memory 2182 /// 2183 /// This function will read a cache page at a time until the NULL 2184 /// C stirng terminator is found. It will stop reading if the NULL 2185 /// termination byte isn't found before reading \a cstr_max_len 2186 /// bytes, and the results are always guaranteed to be NULL 2187 /// terminated (at most cstr_max_len - 1 bytes will be read). 2188 //------------------------------------------------------------------ 2189 size_t 2190 ReadCStringFromMemory (lldb::addr_t vm_addr, 2191 char *cstr, 2192 size_t cstr_max_len); 2193 2194 size_t 2195 ReadMemoryFromInferior (lldb::addr_t vm_addr, 2196 void *buf, 2197 size_t size, 2198 Error &error); 2199 2200 //------------------------------------------------------------------ 2201 /// Reads an unsigned integer of the specified byte size from 2202 /// process memory. 2203 /// 2204 /// @param[in] load_addr 2205 /// A load address of the integer to read. 2206 /// 2207 /// @param[in] byte_size 2208 /// The size in byte of the integer to read. 2209 /// 2210 /// @param[in] fail_value 2211 /// The value to return if we fail to read an integer. 2212 /// 2213 /// @param[out] error 2214 /// An error that indicates the success or failure of this 2215 /// operation. If error indicates success (error.Success()), 2216 /// then the value returned can be trusted, otherwise zero 2217 /// will be returned. 2218 /// 2219 /// @return 2220 /// The unsigned integer that was read from the process memory 2221 /// space. If the integer was smaller than a uint64_t, any 2222 /// unused upper bytes will be zero filled. If the process 2223 /// byte order differs from the host byte order, the integer 2224 /// value will be appropriately byte swapped into host byte 2225 /// order. 2226 //------------------------------------------------------------------ 2227 uint64_t 2228 ReadUnsignedIntegerFromMemory (lldb::addr_t load_addr, 2229 size_t byte_size, 2230 uint64_t fail_value, 2231 Error &error); 2232 2233 lldb::addr_t 2234 ReadPointerFromMemory (lldb::addr_t vm_addr, 2235 Error &error); 2236 2237 bool 2238 WritePointerToMemory (lldb::addr_t vm_addr, 2239 lldb::addr_t ptr_value, 2240 Error &error); 2241 2242 //------------------------------------------------------------------ 2243 /// Actually do the writing of memory to a process. 2244 /// 2245 /// @param[in] vm_addr 2246 /// A virtual load address that indicates where to start writing 2247 /// memory to. 2248 /// 2249 /// @param[in] buf 2250 /// A byte buffer that is at least \a size bytes long that 2251 /// contains the data to write. 2252 /// 2253 /// @param[in] size 2254 /// The number of bytes to write. 2255 /// 2256 /// @param[out] error 2257 /// An error value in case the memory write fails. 2258 /// 2259 /// @return 2260 /// The number of bytes that were actually written. 2261 //------------------------------------------------------------------ 2262 virtual size_t 2263 DoWriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) = 0; 2264 2265 //------------------------------------------------------------------ 2266 /// Write all or part of a scalar value to memory. 2267 /// 2268 /// The value contained in \a scalar will be swapped to match the 2269 /// byte order of the process that is being debugged. If \a size is 2270 /// less than the size of scalar, the least significate \a size bytes 2271 /// from scalar will be written. If \a size is larger than the byte 2272 /// size of scalar, then the extra space will be padded with zeros 2273 /// and the scalar value will be placed in the least significant 2274 /// bytes in memory. 2275 /// 2276 /// @param[in] vm_addr 2277 /// A virtual load address that indicates where to start writing 2278 /// memory to. 2279 /// 2280 /// @param[in] scalar 2281 /// The scalar to write to the debugged process. 2282 /// 2283 /// @param[in] size 2284 /// This value can be smaller or larger than the scalar value 2285 /// itself. If \a size is smaller than the size of \a scalar, 2286 /// the least significant bytes in \a scalar will be used. If 2287 /// \a size is larger than the byte size of \a scalar, then 2288 /// the extra space will be padded with zeros. If \a size is 2289 /// set to UINT32_MAX, then the size of \a scalar will be used. 2290 /// 2291 /// @param[out] error 2292 /// An error value in case the memory write fails. 2293 /// 2294 /// @return 2295 /// The number of bytes that were actually written. 2296 //------------------------------------------------------------------ 2297 size_t 2298 WriteScalarToMemory (lldb::addr_t vm_addr, 2299 const Scalar &scalar, 2300 uint32_t size, 2301 Error &error); 2302 2303 size_t 2304 ReadScalarIntegerFromMemory (lldb::addr_t addr, 2305 uint32_t byte_size, 2306 bool is_signed, 2307 Scalar &scalar, 2308 Error &error); 2309 2310 //------------------------------------------------------------------ 2311 /// Write memory to a process. 2312 /// 2313 /// This function will write memory to the current process's 2314 /// address space and maintain any traps that might be present due 2315 /// to software breakpoints. 2316 /// 2317 /// This function is not meant to be overridden by Process 2318 /// subclasses, the subclasses should implement 2319 /// Process::DoWriteMemory (lldb::addr_t, size_t, void *). 2320 /// 2321 /// @param[in] vm_addr 2322 /// A virtual load address that indicates where to start writing 2323 /// memory to. 2324 /// 2325 /// @param[in] buf 2326 /// A byte buffer that is at least \a size bytes long that 2327 /// contains the data to write. 2328 /// 2329 /// @param[in] size 2330 /// The number of bytes to write. 2331 /// 2332 /// @return 2333 /// The number of bytes that were actually written. 2334 //------------------------------------------------------------------ 2335 size_t 2336 WriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error); 2337 2338 2339 //------------------------------------------------------------------ 2340 /// Actually allocate memory in the process. 2341 /// 2342 /// This function will allocate memory in the process's address 2343 /// space. This can't rely on the generic function calling mechanism, 2344 /// since that requires this function. 2345 /// 2346 /// @param[in] size 2347 /// The size of the allocation requested. 2348 /// 2349 /// @return 2350 /// The address of the allocated buffer in the process, or 2351 /// LLDB_INVALID_ADDRESS if the allocation failed. 2352 //------------------------------------------------------------------ 2353 2354 virtual lldb::addr_t 2355 DoAllocateMemory (size_t size, uint32_t permissions, Error &error) = 0; 2356 2357 //------------------------------------------------------------------ 2358 /// The public interface to allocating memory in the process. 2359 /// 2360 /// This function will allocate memory in the process's address 2361 /// space. This can't rely on the generic function calling mechanism, 2362 /// since that requires this function. 2363 /// 2364 /// @param[in] size 2365 /// The size of the allocation requested. 2366 /// 2367 /// @param[in] permissions 2368 /// Or together any of the lldb::Permissions bits. The permissions on 2369 /// a given memory allocation can't be changed after allocation. Note 2370 /// that a block that isn't set writable can still be written on from lldb, 2371 /// just not by the process itself. 2372 /// 2373 /// @param[in/out] error 2374 /// An error object to fill in if things go wrong. 2375 /// @return 2376 /// The address of the allocated buffer in the process, or 2377 /// LLDB_INVALID_ADDRESS if the allocation failed. 2378 //------------------------------------------------------------------ 2379 2380 lldb::addr_t 2381 AllocateMemory (size_t size, uint32_t permissions, Error &error); 2382 2383 //------------------------------------------------------------------ 2384 /// Determines whether executing JIT-compiled code in this process 2385 /// is possible. 2386 /// 2387 /// @return 2388 /// True if execution of JIT code is possible; false otherwise. 2389 //------------------------------------------------------------------ 2390 bool CanJIT (); 2391 2392 //------------------------------------------------------------------ 2393 /// Sets whether executing JIT-compiled code in this process 2394 /// is possible. 2395 /// 2396 /// @param[in] can_jit 2397 /// True if execution of JIT code is possible; false otherwise. 2398 //------------------------------------------------------------------ 2399 void SetCanJIT (bool can_jit); 2400 2401 //------------------------------------------------------------------ 2402 /// Actually deallocate memory in the process. 2403 /// 2404 /// This function will deallocate memory in the process's address 2405 /// space that was allocated with AllocateMemory. 2406 /// 2407 /// @param[in] ptr 2408 /// A return value from AllocateMemory, pointing to the memory you 2409 /// want to deallocate. 2410 /// 2411 /// @return 2412 /// \btrue if the memory was deallocated, \bfalse otherwise. 2413 //------------------------------------------------------------------ 2414 2415 virtual Error 2416 DoDeallocateMemory (lldb::addr_t ptr) = 0; 2417 2418 //------------------------------------------------------------------ 2419 /// The public interface to deallocating memory in the process. 2420 /// 2421 /// This function will deallocate memory in the process's address 2422 /// space that was allocated with AllocateMemory. 2423 /// 2424 /// @param[in] ptr 2425 /// A return value from AllocateMemory, pointing to the memory you 2426 /// want to deallocate. 2427 /// 2428 /// @return 2429 /// \btrue if the memory was deallocated, \bfalse otherwise. 2430 //------------------------------------------------------------------ 2431 2432 Error 2433 DeallocateMemory (lldb::addr_t ptr); 2434 2435 //------------------------------------------------------------------ 2436 /// Get any available STDOUT. 2437 /// 2438 /// If the process was launched without supplying valid file paths 2439 /// for stdin, stdout, and stderr, then the Process class might 2440 /// try to cache the STDOUT for the process if it is able. Events 2441 /// will be queued indicating that there is STDOUT available that 2442 /// can be retrieved using this function. 2443 /// 2444 /// @param[out] buf 2445 /// A buffer that will receive any STDOUT bytes that are 2446 /// currently available. 2447 /// 2448 /// @param[out] buf_size 2449 /// The size in bytes for the buffer \a buf. 2450 /// 2451 /// @return 2452 /// The number of bytes written into \a buf. If this value is 2453 /// equal to \a buf_size, another call to this function should 2454 /// be made to retrieve more STDOUT data. 2455 //------------------------------------------------------------------ 2456 virtual size_t 2457 GetSTDOUT (char *buf, size_t buf_size, Error &error) 2458 { 2459 error.SetErrorString("stdout unsupported"); 2460 return 0; 2461 } 2462 2463 2464 //------------------------------------------------------------------ 2465 /// Get any available STDERR. 2466 /// 2467 /// If the process was launched without supplying valid file paths 2468 /// for stdin, stdout, and stderr, then the Process class might 2469 /// try to cache the STDERR for the process if it is able. Events 2470 /// will be queued indicating that there is STDERR available that 2471 /// can be retrieved using this function. 2472 /// 2473 /// @param[out] buf 2474 /// A buffer that will receive any STDERR bytes that are 2475 /// currently available. 2476 /// 2477 /// @param[out] buf_size 2478 /// The size in bytes for the buffer \a buf. 2479 /// 2480 /// @return 2481 /// The number of bytes written into \a buf. If this value is 2482 /// equal to \a buf_size, another call to this function should 2483 /// be made to retrieve more STDERR data. 2484 //------------------------------------------------------------------ 2485 virtual size_t 2486 GetSTDERR (char *buf, size_t buf_size, Error &error) 2487 { 2488 error.SetErrorString("stderr unsupported"); 2489 return 0; 2490 } 2491 2492 virtual size_t 2493 PutSTDIN (const char *buf, size_t buf_size, Error &error) 2494 { 2495 error.SetErrorString("stdin unsupported"); 2496 return 0; 2497 } 2498 2499 //---------------------------------------------------------------------- 2500 // Process Breakpoints 2501 //---------------------------------------------------------------------- 2502 size_t 2503 GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site); 2504 2505 virtual Error 2506 EnableBreakpoint (BreakpointSite *bp_site) = 0; 2507 2508 virtual Error 2509 DisableBreakpoint (BreakpointSite *bp_site) = 0; 2510 2511 // This is implemented completely using the lldb::Process API. Subclasses 2512 // don't need to implement this function unless the standard flow of 2513 // read existing opcode, write breakpoint opcode, verify breakpoint opcode 2514 // doesn't work for a specific process plug-in. 2515 virtual Error 2516 EnableSoftwareBreakpoint (BreakpointSite *bp_site); 2517 2518 // This is implemented completely using the lldb::Process API. Subclasses 2519 // don't need to implement this function unless the standard flow of 2520 // restoring original opcode in memory and verifying the restored opcode 2521 // doesn't work for a specific process plug-in. 2522 virtual Error 2523 DisableSoftwareBreakpoint (BreakpointSite *bp_site); 2524 2525 BreakpointSiteList & 2526 GetBreakpointSiteList(); 2527 2528 const BreakpointSiteList & 2529 GetBreakpointSiteList() const; 2530 2531 void 2532 DisableAllBreakpointSites (); 2533 2534 Error 2535 ClearBreakpointSiteByID (lldb::user_id_t break_id); 2536 2537 lldb::break_id_t 2538 CreateBreakpointSite (lldb::BreakpointLocationSP &owner, 2539 bool use_hardware); 2540 2541 Error 2542 DisableBreakpointSiteByID (lldb::user_id_t break_id); 2543 2544 Error 2545 EnableBreakpointSiteByID (lldb::user_id_t break_id); 2546 2547 2548 // BreakpointLocations use RemoveOwnerFromBreakpointSite to remove 2549 // themselves from the owner's list of this breakpoint sites. This has to 2550 // be a static function because you can't be sure that removing the 2551 // breakpoint from it's containing map won't delete the breakpoint site, 2552 // and doing that in an instance method isn't copasetic. 2553 void 2554 RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, 2555 lldb::user_id_t owner_loc_id, 2556 lldb::BreakpointSiteSP &bp_site_sp); 2557 2558 //---------------------------------------------------------------------- 2559 // Process Watchpoints (optional) 2560 //---------------------------------------------------------------------- 2561 virtual Error 2562 EnableWatchpoint (Watchpoint *wp); 2563 2564 virtual Error 2565 DisableWatchpoint (Watchpoint *wp); 2566 2567 //------------------------------------------------------------------ 2568 // Thread Queries 2569 //------------------------------------------------------------------ 2570 virtual uint32_t 2571 UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) = 0; 2572 2573 void 2574 UpdateThreadListIfNeeded (); 2575 2576 ThreadList & 2577 GetThreadList () 2578 { 2579 return m_thread_list; 2580 } 2581 2582 2583 uint32_t 2584 GetNextThreadIndexID (); 2585 2586 //------------------------------------------------------------------ 2587 // Event Handling 2588 //------------------------------------------------------------------ 2589 lldb::StateType 2590 GetNextEvent (lldb::EventSP &event_sp); 2591 2592 lldb::StateType 2593 WaitForProcessToStop (const TimeValue *timeout); 2594 2595 lldb::StateType 2596 WaitForStateChangedEvents (const TimeValue *timeout, lldb::EventSP &event_sp); 2597 2598 Event * 2599 PeekAtStateChangedEvents (); 2600 2601 2602 class 2603 ProcessEventHijacker 2604 { 2605 public: 2606 ProcessEventHijacker (Process &process, Listener *listener) : 2607 m_process (process), 2608 m_listener (listener) 2609 { 2610 m_process.HijackProcessEvents (listener); 2611 } 2612 ~ProcessEventHijacker () 2613 { 2614 m_process.RestoreProcessEvents(); 2615 } 2616 2617 private: 2618 Process &m_process; 2619 Listener *m_listener; 2620 }; 2621 friend class ProcessEventHijacker; 2622 //------------------------------------------------------------------ 2623 /// If you need to ensure that you and only you will hear about some public 2624 /// event, then make a new listener, set to listen to process events, and 2625 /// then call this with that listener. Then you will have to wait on that 2626 /// listener explicitly for events (rather than using the GetNextEvent & WaitFor* 2627 /// calls above. Be sure to call RestoreProcessEvents when you are done. 2628 /// 2629 /// @param[in] listener 2630 /// This is the new listener to whom all process events will be delivered. 2631 /// 2632 /// @return 2633 /// Returns \b true if the new listener could be installed, 2634 /// \b false otherwise. 2635 //------------------------------------------------------------------ 2636 bool 2637 HijackProcessEvents (Listener *listener); 2638 2639 //------------------------------------------------------------------ 2640 /// Restores the process event broadcasting to its normal state. 2641 /// 2642 //------------------------------------------------------------------ 2643 void 2644 RestoreProcessEvents (); 2645 2646protected: 2647 //------------------------------------------------------------------ 2648 /// This is the part of the event handling that for a process event. 2649 /// It decides what to do with the event and returns true if the 2650 /// event needs to be propagated to the user, and false otherwise. 2651 /// If the event is not propagated, this call will most likely set 2652 /// the target to executing again. 2653 /// 2654 /// @param[in] event_ptr 2655 /// This is the event we are handling. 2656 /// 2657 /// @return 2658 /// Returns \b true if the event should be reported to the 2659 /// user, \b false otherwise. 2660 //------------------------------------------------------------------ 2661 bool 2662 ShouldBroadcastEvent (Event *event_ptr); 2663 2664public: 2665 const lldb::ABISP & 2666 GetABI (); 2667 2668 DynamicLoader * 2669 GetDynamicLoader () 2670 { 2671 return m_dyld_ap.get(); 2672 } 2673 2674 OperatingSystem * 2675 GetOperatingSystem () 2676 { 2677 return m_os_ap.get(); 2678 } 2679 2680 2681 virtual LanguageRuntime * 2682 GetLanguageRuntime (lldb::LanguageType language); 2683 2684 virtual CPPLanguageRuntime * 2685 GetCPPLanguageRuntime (); 2686 2687 virtual ObjCLanguageRuntime * 2688 GetObjCLanguageRuntime (); 2689 2690 bool 2691 IsRunning () const; 2692 2693 DynamicCheckerFunctions *GetDynamicCheckers() 2694 { 2695 return m_dynamic_checkers_ap.get(); 2696 } 2697 2698 void SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) 2699 { 2700 m_dynamic_checkers_ap.reset(dynamic_checkers); 2701 } 2702 2703 //------------------------------------------------------------------ 2704 /// Call this to set the lldb in the mode where it breaks on new thread 2705 /// creations, and then auto-restarts. This is useful when you are trying 2706 /// to run only one thread, but either that thread or the kernel is creating 2707 /// new threads in the process. If you stop when the thread is created, you 2708 /// can immediately suspend it, and keep executing only the one thread you intend. 2709 /// 2710 /// @return 2711 /// Returns \b true if we were able to start up the notification 2712 /// \b false otherwise. 2713 //------------------------------------------------------------------ 2714 virtual bool 2715 StartNoticingNewThreads() 2716 { 2717 return true; 2718 } 2719 2720 //------------------------------------------------------------------ 2721 /// Call this to turn off the stop & notice new threads mode. 2722 /// 2723 /// @return 2724 /// Returns \b true if we were able to start up the notification 2725 /// \b false otherwise. 2726 //------------------------------------------------------------------ 2727 virtual bool 2728 StopNoticingNewThreads() 2729 { 2730 return true; 2731 } 2732 2733 //------------------------------------------------------------------ 2734 // lldb::ExecutionContextScope pure virtual functions 2735 //------------------------------------------------------------------ 2736 virtual Target * 2737 CalculateTarget () 2738 { 2739 return &m_target; 2740 } 2741 2742 virtual Process * 2743 CalculateProcess () 2744 { 2745 return this; 2746 } 2747 2748 virtual Thread * 2749 CalculateThread () 2750 { 2751 return NULL; 2752 } 2753 2754 virtual StackFrame * 2755 CalculateStackFrame () 2756 { 2757 return NULL; 2758 } 2759 2760 virtual void 2761 CalculateExecutionContext (ExecutionContext &exe_ctx); 2762 2763 lldb::ProcessSP 2764 GetSP (); 2765 2766protected: 2767 //------------------------------------------------------------------ 2768 // NextEventAction provides a way to register an action on the next 2769 // event that is delivered to this process. There is currently only 2770 // one next event action allowed in the process at one time. If a 2771 // new "NextEventAction" is added while one is already present, the 2772 // old action will be discarded (with HandleBeingUnshipped called 2773 // after it is discarded.) 2774 //------------------------------------------------------------------ 2775 class NextEventAction 2776 { 2777 public: 2778 typedef enum EventActionResult 2779 { 2780 eEventActionSuccess, 2781 eEventActionRetry, 2782 eEventActionExit 2783 } EventActionResult; 2784 2785 NextEventAction (Process *process) : 2786 m_process(process) 2787 {} 2788 virtual ~NextEventAction() {} 2789 2790 virtual EventActionResult PerformAction (lldb::EventSP &event_sp) = 0; 2791 virtual void HandleBeingUnshipped () {}; 2792 virtual EventActionResult HandleBeingInterrupted () = 0; 2793 virtual const char *GetExitString() = 0; 2794 protected: 2795 Process *m_process; 2796 }; 2797 2798 void SetNextEventAction (Process::NextEventAction *next_event_action) 2799 { 2800 if (m_next_event_action_ap.get()) 2801 m_next_event_action_ap->HandleBeingUnshipped(); 2802 2803 m_next_event_action_ap.reset(next_event_action); 2804 } 2805 2806 // This is the completer for Attaching: 2807 class AttachCompletionHandler : public NextEventAction 2808 { 2809 public: 2810 AttachCompletionHandler (Process *process) : 2811 NextEventAction(process) 2812 {} 2813 virtual ~AttachCompletionHandler() {} 2814 2815 virtual EventActionResult PerformAction (lldb::EventSP &event_sp); 2816 virtual EventActionResult HandleBeingInterrupted (); 2817 virtual const char *GetExitString(); 2818 private: 2819 std::string m_exit_string; 2820 }; 2821 2822 bool 2823 HijackPrivateProcessEvents (Listener *listener); 2824 2825 void 2826 RestorePrivateProcessEvents (); 2827 2828 bool 2829 PrivateStateThreadIsValid () const 2830 { 2831 return m_private_state_thread != LLDB_INVALID_HOST_THREAD; 2832 } 2833 2834 //------------------------------------------------------------------ 2835 // Member variables 2836 //------------------------------------------------------------------ 2837 Target & m_target; ///< The target that owns this process. 2838 ThreadSafeValue<lldb::StateType> m_public_state; 2839 ThreadSafeValue<lldb::StateType> m_private_state; // The actual state of our process 2840 Broadcaster m_private_state_broadcaster; // This broadcaster feeds state changed events into the private state thread's listener. 2841 Broadcaster m_private_state_control_broadcaster; // This is the control broadcaster, used to pause, resume & stop the private state thread. 2842 Listener m_private_state_listener; // This is the listener for the private state thread. 2843 Predicate<bool> m_private_state_control_wait; /// This Predicate is used to signal that a control operation is complete. 2844 lldb::thread_t m_private_state_thread; // Thread ID for the thread that watches interal state events 2845 ProcessModID m_mod_id; ///< Tracks the state of the process over stops and other alterations. 2846 uint32_t m_thread_index_id; ///< Each thread is created with a 1 based index that won't get re-used. 2847 int m_exit_status; ///< The exit status of the process, or -1 if not set. 2848 std::string m_exit_string; ///< A textual description of why a process exited. 2849 ThreadList m_thread_list; ///< The threads for this process. 2850 std::vector<Notifications> m_notifications; ///< The list of notifications that this process can deliver. 2851 std::vector<lldb::addr_t> m_image_tokens; 2852 Listener &m_listener; 2853 BreakpointSiteList m_breakpoint_site_list; ///< This is the list of breakpoint locations we intend 2854 ///< to insert in the target. 2855 std::auto_ptr<DynamicLoader> m_dyld_ap; 2856 std::auto_ptr<DynamicCheckerFunctions> m_dynamic_checkers_ap; ///< The functions used by the expression parser to validate data that expressions use. 2857 std::auto_ptr<OperatingSystem> m_os_ap; 2858 UnixSignals m_unix_signals; /// This is the current signal set for this process. 2859 lldb::ABISP m_abi_sp; 2860 lldb::InputReaderSP m_process_input_reader; 2861 lldb_private::Communication m_stdio_communication; 2862 lldb_private::Mutex m_stdio_communication_mutex; 2863 std::string m_stdout_data; 2864 MemoryCache m_memory_cache; 2865 AllocatedMemoryCache m_allocated_memory_cache; 2866 bool m_attached_to_process; /// Did we launch the process or attach to it? 2867 2868 typedef std::map<lldb::LanguageType, lldb::LanguageRuntimeSP> LanguageRuntimeCollection; 2869 LanguageRuntimeCollection m_language_runtimes; 2870 std::auto_ptr<NextEventAction> m_next_event_action_ap; 2871 2872 enum { 2873 eCanJITDontKnow= 0, 2874 eCanJITYes, 2875 eCanJITNo 2876 } m_can_jit; 2877 2878 size_t 2879 RemoveBreakpointOpcodesFromBuffer (lldb::addr_t addr, size_t size, uint8_t *buf) const; 2880 2881 void 2882 SynchronouslyNotifyStateChanged (lldb::StateType state); 2883 2884 void 2885 SetPublicState (lldb::StateType new_state); 2886 2887 void 2888 SetPrivateState (lldb::StateType state); 2889 2890 bool 2891 StartPrivateStateThread (); 2892 2893 void 2894 StopPrivateStateThread (); 2895 2896 void 2897 PausePrivateStateThread (); 2898 2899 void 2900 ResumePrivateStateThread (); 2901 2902 static void * 2903 PrivateStateThread (void *arg); 2904 2905 void * 2906 RunPrivateStateThread (); 2907 2908 void 2909 HandlePrivateEvent (lldb::EventSP &event_sp); 2910 2911 lldb::StateType 2912 WaitForProcessStopPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 2913 2914 // This waits for both the state change broadcaster, and the control broadcaster. 2915 // If control_only, it only waits for the control broadcaster. 2916 2917 bool 2918 WaitForEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp, bool control_only); 2919 2920 lldb::StateType 2921 WaitForStateChangedEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 2922 2923 lldb::StateType 2924 WaitForState (const TimeValue *timeout, 2925 const lldb::StateType *match_states, 2926 const uint32_t num_match_states); 2927 2928 size_t 2929 WriteMemoryPrivate (lldb::addr_t addr, const void *buf, size_t size, Error &error); 2930 2931 void 2932 AppendSTDOUT (const char *s, size_t len); 2933 2934 static void 2935 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); 2936 2937 void 2938 PushProcessInputReader (); 2939 2940 void 2941 PopProcessInputReader (); 2942 2943 void 2944 ResetProcessInputReader (); 2945 2946 void 2947 SetUpProcessInputReader (int file_descriptor); 2948 2949 static size_t 2950 ProcessInputReaderCallback (void *baton, 2951 InputReader &reader, 2952 lldb::InputReaderAction notification, 2953 const char *bytes, 2954 size_t bytes_len); 2955 2956 2957private: 2958 //------------------------------------------------------------------ 2959 // For Process only 2960 //------------------------------------------------------------------ 2961 void ControlPrivateStateThread (uint32_t signal); 2962 2963 DISALLOW_COPY_AND_ASSIGN (Process); 2964 2965}; 2966 2967} // namespace lldb_private 2968 2969#endif // liblldb_Process_h_ 2970