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