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