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