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