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