Process.h revision 72e1c782ba1e4226da37af4722af608de9f39408
1//===-- Process.h -----------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_Process_h_ 11#define liblldb_Process_h_ 12 13// C Includes 14// C++ Includes 15#include <list> 16 17// Other libraries and framework includes 18// Project includes 19#include "lldb/lldb-private.h" 20#include "lldb/Interpreter/Args.h" 21#include "lldb/Interpreter/Options.h" 22#include "lldb/Core/Broadcaster.h" 23#include "lldb/Core/Communication.h" 24#include "lldb/Core/Error.h" 25#include "lldb/Core/Event.h" 26#include "lldb/Core/StringList.h" 27#include "lldb/Core/ThreadSafeValue.h" 28#include "lldb/Core/PluginInterface.h" 29#include "lldb/Core/UserSettingsController.h" 30#include "lldb/Breakpoint/BreakpointSiteList.h" 31#include "lldb/Expression/ClangPersistentVariables.h" 32#include "lldb/Expression/IRDynamicChecks.h" 33#include "lldb/Target/ExecutionContextScope.h" 34#include "lldb/Target/ThreadList.h" 35#include "lldb/Target/UnixSignals.h" 36 37namespace lldb_private { 38 39 40typedef enum ProcessPlugins 41{ 42 eMacosx, 43 eRemoteDebugger 44} ProcessPlugins; 45 46 47class ProcessInstanceSettings : public InstanceSettings 48{ 49public: 50 51 ProcessInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL); 52 53 ProcessInstanceSettings (const ProcessInstanceSettings &rhs); 54 55 virtual 56 ~ProcessInstanceSettings (); 57 58 ProcessInstanceSettings& 59 operator= (const ProcessInstanceSettings &rhs); 60 61 62 void 63 UpdateInstanceSettingsVariable (const ConstString &var_name, 64 const char *index_value, 65 const char *value, 66 const ConstString &instance_name, 67 const SettingEntry &entry, 68 lldb::VarSetOperationType op, 69 Error &err, 70 bool pending); 71 72 bool 73 GetInstanceSettingsValue (const SettingEntry &entry, 74 const ConstString &var_name, 75 StringList &value, 76 Error *err); 77 78 79 const Args & 80 GetRunArguments () const 81 { 82 return m_run_args; 83 } 84 85 void 86 SetRunArguments (const Args &args) 87 { 88 m_run_args = args; 89 } 90 91 void 92 GetHostEnvironmentIfNeeded (); 93 94 size_t 95 GetEnvironmentAsArgs (Args &env); 96 97 const char * 98 GetStandardInputPath () const 99 { 100 if (m_input_path.empty()) 101 return NULL; 102 return m_input_path.c_str(); 103 } 104 105 void 106 SetStandardInputPath (const char *path) 107 { 108 if (path && path[0]) 109 m_input_path.assign (path); 110 else 111 { 112 // Make sure we deallocate memory in string... 113 std::string tmp; 114 tmp.swap (m_input_path); 115 } 116 } 117 118 const char * 119 GetStandardOutputPath () const 120 { 121 if (m_output_path.empty()) 122 return NULL; 123 return m_output_path.c_str(); 124 } 125 126 void 127 SetStandardOutputPath (const char *path) 128 { 129 if (path && path[0]) 130 m_output_path.assign (path); 131 else 132 { 133 // Make sure we deallocate memory in string... 134 std::string tmp; 135 tmp.swap (m_output_path); 136 } 137 } 138 139 const char * 140 GetStandardErrorPath () const 141 { 142 if (m_error_path.empty()) 143 return NULL; 144 return m_error_path.c_str(); 145 } 146 147 void 148 SetStandardErrorPath (const char *path) 149 { 150 if (path && path[0]) 151 m_error_path.assign (path); 152 else 153 { 154 // Make sure we deallocate memory in string... 155 std::string tmp; 156 tmp.swap (m_error_path); 157 } 158 } 159 160 bool 161 GetDisableASLR () const 162 { 163 return m_disable_aslr; 164 } 165 166 void 167 SetDisableASLR (bool b) 168 { 169 m_disable_aslr = b; 170 } 171 172 bool 173 GetDisableSTDIO () const 174 { 175 return m_disable_stdio; 176 } 177 178 void 179 SetDisableSTDIO (bool b) 180 { 181 m_disable_stdio = b; 182 } 183 184protected: 185 186 void 187 CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 188 bool pending); 189 190 const ConstString 191 CreateInstanceName (); 192 193 static const ConstString & 194 RunArgsVarName (); 195 196 static const ConstString & 197 EnvVarsVarName (); 198 199 static const ConstString & 200 InheritHostEnvVarName (); 201 202 static const ConstString & 203 InputPathVarName (); 204 205 static const ConstString & 206 OutputPathVarName (); 207 208 static const ConstString & 209 ErrorPathVarName (); 210 211 static const ConstString & 212 PluginVarName (); 213 214 static const ConstString & 215 DisableASLRVarName(); 216 217 static const ConstString & 218 DisableSTDIOVarName (); 219 220private: 221 222 typedef std::map<std::string, std::string> dictionary; 223 Args m_run_args; 224 dictionary m_env_vars; 225 std::string m_input_path; 226 std::string m_output_path; 227 std::string m_error_path; 228 ProcessPlugins m_plugin; 229 bool m_disable_aslr; 230 bool m_disable_stdio; 231 bool m_inherit_host_env; 232 bool m_got_host_env; 233}; 234 235 236//---------------------------------------------------------------------- 237/// @class Process Process.h "lldb/Target/Process.h" 238/// @brief A plug-in interface definition class for debugging a process. 239//---------------------------------------------------------------------- 240class Process : 241 public UserID, 242 public Broadcaster, 243 public ExecutionContextScope, 244 public PluginInterface, 245 public ProcessInstanceSettings 246{ 247friend class ThreadList; 248friend class ClangFunction; // For WaitForStateChangeEventsPrivate 249 250public: 251 252 //------------------------------------------------------------------ 253 /// Broadcaster event bits definitions. 254 //------------------------------------------------------------------ 255 enum 256 { 257 eBroadcastBitStateChanged = (1 << 0), 258 eBroadcastBitInterrupt = (1 << 1), 259 eBroadcastBitSTDOUT = (1 << 2), 260 eBroadcastBitSTDERR = (1 << 3) 261 }; 262 263 enum 264 { 265 eBroadcastInternalStateControlStop = (1<<0), 266 eBroadcastInternalStateControlPause = (1<<1), 267 eBroadcastInternalStateControlResume = (1<<2) 268 }; 269 270 //------------------------------------------------------------------ 271 /// A notification structure that can be used by clients to listen 272 /// for changes in a process's lifetime. 273 /// 274 /// @see RegisterNotificationCallbacks (const Notifications&) 275 /// @see UnregisterNotificationCallbacks (const Notifications&) 276 //------------------------------------------------------------------ 277#ifndef SWIG 278 typedef struct 279 { 280 void *baton; 281 void (*initialize)(void *baton, Process *process); 282 void (*process_state_changed) (void *baton, Process *process, lldb::StateType state); 283 } Notifications; 284 285 class ProcessEventData : 286 public EventData 287 { 288 friend class Process; 289 290 public: 291 ProcessEventData (); 292 ProcessEventData (const lldb::ProcessSP &process, lldb::StateType state); 293 294 virtual ~ProcessEventData(); 295 296 static const ConstString & 297 GetFlavorString (); 298 299 virtual const ConstString & 300 GetFlavor () const; 301 302 const lldb::ProcessSP & 303 GetProcessSP() const 304 { 305 return m_process_sp; 306 } 307 lldb::StateType 308 GetState() const 309 { 310 return m_state; 311 } 312 bool 313 GetRestarted () const 314 { 315 return m_restarted; 316 } 317 bool 318 GetInterrupted () const 319 { 320 return m_interrupted; 321 } 322 323 virtual void 324 Dump (Stream *s) const; 325 326 virtual void 327 DoOnRemoval (Event *event_ptr); 328 329 static const Process::ProcessEventData * 330 GetEventDataFromEvent (const Event *event_ptr); 331 332 static lldb::ProcessSP 333 GetProcessFromEvent (const Event *event_ptr); 334 335 static lldb::StateType 336 GetStateFromEvent (const Event *event_ptr); 337 338 static bool 339 GetRestartedFromEvent (const Event *event_ptr); 340 341 static void 342 SetRestartedInEvent (Event *event_ptr, bool new_value); 343 344 static bool 345 GetInterruptedFromEvent (const Event *event_ptr); 346 347 static void 348 SetInterruptedInEvent (Event *event_ptr, bool new_value); 349 350 static bool 351 SetUpdateStateOnRemoval (Event *event_ptr); 352 353 private: 354 355 void 356 SetUpdateStateOnRemoval() 357 { 358 m_update_state = true; 359 } 360 void 361 SetRestarted (bool new_value) 362 { 363 m_restarted = new_value; 364 } 365 void 366 SetInterrupted (bool new_value) 367 { 368 m_interrupted = new_value; 369 } 370 371 lldb::ProcessSP m_process_sp; 372 lldb::StateType m_state; 373 bool m_restarted; // For "eStateStopped" events, this is true if the target was automatically restarted. 374 bool m_update_state; 375 bool m_interrupted; 376 DISALLOW_COPY_AND_ASSIGN (ProcessEventData); 377 378 }; 379 380 class SettingsController : public UserSettingsController 381 { 382 public: 383 384 SettingsController (); 385 386 virtual 387 ~SettingsController (); 388 389 static SettingEntry global_settings_table[]; 390 static SettingEntry instance_settings_table[]; 391 392 protected: 393 394 lldb::InstanceSettingsSP 395 CreateInstanceSettings (const char *instance_name); 396 397 static lldb::OptionEnumValueElement g_plugins[]; 398 399 private: 400 401 // Class-wide settings. 402 403 DISALLOW_COPY_AND_ASSIGN (SettingsController); 404 }; 405 406#endif 407 408 static void 409 Initialize (); 410 411 static void 412 Terminate (); 413 414 static lldb::UserSettingsControllerSP & 415 GetSettingsController (); 416 417 void 418 UpdateInstanceName (); 419 420 //------------------------------------------------------------------ 421 /// Construct with a shared pointer to a target, and the Process listener. 422 //------------------------------------------------------------------ 423 Process(Target &target, Listener &listener); 424 425 //------------------------------------------------------------------ 426 /// Destructor. 427 /// 428 /// The destructor is virtual since this class is designed to be 429 /// inherited from by the plug-in instance. 430 //------------------------------------------------------------------ 431 virtual 432 ~Process(); 433 434 //------------------------------------------------------------------ 435 /// Find a Process plug-in that can debug \a module using the 436 /// currently selected architecture. 437 /// 438 /// Scans all loaded plug-in interfaces that implement versions of 439 /// the Process plug-in interface and returns the first instance 440 /// that can debug the file. 441 /// 442 /// @param[in] module_sp 443 /// The module shared pointer that this process will debug. 444 /// 445 /// @param[in] plugin_name 446 /// If NULL, select the best plug-in for the binary. If non-NULL 447 /// then look for a plugin whose PluginInfo's name matches 448 /// this string. 449 /// 450 /// @see Process::CanDebug () 451 //------------------------------------------------------------------ 452 static Process* 453 FindPlugin (Target &target, const char *plugin_name, Listener &listener); 454 455 456 457 //------------------------------------------------------------------ 458 /// Static function that can be used with the \b host function 459 /// Host::StartMonitoringChildProcess (). 460 /// 461 /// This function can be used by lldb_private::Process subclasses 462 /// when they want to watch for a local process and have its exit 463 /// status automatically set when the host child process exits. 464 /// Subclasses should call Host::StartMonitoringChildProcess () 465 /// with: 466 /// callback = Process::SetHostProcessExitStatus 467 /// callback_baton = NULL 468 /// pid = Process::GetID() 469 /// monitor_signals = false 470 //------------------------------------------------------------------ 471 static bool 472 SetProcessExitStatus (void *callback_baton, // The callback baton which should be set to NULL 473 lldb::pid_t pid, // The process ID we want to monitor 474 int signo, // Zero for no signal 475 int status); // Exit value of process if signal is zero 476 477 //------------------------------------------------------------------ 478 /// Check if a plug-in instance can debug the file in \a module. 479 /// 480 /// Each plug-in is given a chance to say whether it can debug 481 /// the file in \a module. If the Process plug-in instance can 482 /// debug a file on the current system, it should return \b true. 483 /// 484 /// @return 485 /// Returns \b true if this Process plug-in instance can 486 /// debug the executable, \b false otherwise. 487 //------------------------------------------------------------------ 488 virtual bool 489 CanDebug (Target &target) = 0; 490 491 492 //------------------------------------------------------------------ 493 /// This object is about to be destroyed, do any necessary cleanup. 494 /// 495 /// Subclasses that override this method should always call this 496 /// superclass method. 497 //------------------------------------------------------------------ 498 virtual void 499 Finalize(); 500 501 //------------------------------------------------------------------ 502 /// Launch a new process. 503 /// 504 /// Launch a new process by spawning a new process using the 505 /// target object's executable module's file as the file to launch. 506 /// Arguments are given in \a argv, and the environment variables 507 /// are in \a envp. Standard input and output files can be 508 /// optionally re-directed to \a stdin_path, \a stdout_path, and 509 /// \a stderr_path. 510 /// 511 /// This function is not meant to be overridden by Process 512 /// subclasses. It will first call Process::WillLaunch (Module *) 513 /// and if that returns \b true, Process::DoLaunch (Module*, 514 /// char const *[],char const *[],const char *,const char *, 515 /// const char *) will be called to actually do the launching. If 516 /// DoLaunch returns \b true, then Process::DidLaunch() will be 517 /// called. 518 /// 519 /// @param[in] argv 520 /// The argument array. 521 /// 522 /// @param[in] envp 523 /// The environment array. 524 /// 525 /// @param[in] launch_flags 526 /// Flags to modify the launch (@see lldb::LaunchFlags) 527 /// 528 /// @param[in] stdin_path 529 /// The path to use when re-directing the STDIN of the new 530 /// process. If all stdXX_path arguments are NULL, a pseudo 531 /// terminal will be used. 532 /// 533 /// @param[in] stdout_path 534 /// The path to use when re-directing the STDOUT of the new 535 /// process. If all stdXX_path arguments are NULL, a pseudo 536 /// terminal will be used. 537 /// 538 /// @param[in] stderr_path 539 /// The path to use when re-directing the STDERR of the new 540 /// process. If all stdXX_path arguments are NULL, a pseudo 541 /// terminal will be used. 542 /// 543 /// @return 544 /// An error object. Call GetID() to get the process ID if 545 /// the error object is success. 546 //------------------------------------------------------------------ 547 virtual Error 548 Launch (char const *argv[], 549 char const *envp[], 550 uint32_t launch_flags, 551 const char *stdin_path, 552 const char *stdout_path, 553 const char *stderr_path); 554 555 //------------------------------------------------------------------ 556 /// Attach to an existing process using a process ID. 557 /// 558 /// This function is not meant to be overridden by Process 559 /// subclasses. It will first call Process::WillAttach (lldb::pid_t) 560 /// and if that returns \b true, Process::DoAttach (lldb::pid_t) will 561 /// be called to actually do the attach. If DoAttach returns \b 562 /// true, then Process::DidAttach() will be called. 563 /// 564 /// @param[in] pid 565 /// The process ID that we should attempt to attach to. 566 /// 567 /// @return 568 /// Returns \a pid if attaching was successful, or 569 /// LLDB_INVALID_PROCESS_ID if attaching fails. 570 //------------------------------------------------------------------ 571 virtual Error 572 Attach (lldb::pid_t pid); 573 574 //------------------------------------------------------------------ 575 /// Attach to an existing process by process name. 576 /// 577 /// This function is not meant to be overridden by Process 578 /// subclasses. It will first call 579 /// Process::WillAttach (const char *) and if that returns \b 580 /// true, Process::DoAttach (const char *) will be called to 581 /// actually do the attach. If DoAttach returns \b true, then 582 /// Process::DidAttach() will be called. 583 /// 584 /// @param[in] process_name 585 /// A process name to match against the current process list. 586 /// 587 /// @return 588 /// Returns \a pid if attaching was successful, or 589 /// LLDB_INVALID_PROCESS_ID if attaching fails. 590 //------------------------------------------------------------------ 591 virtual Error 592 Attach (const char *process_name, bool wait_for_launch); 593 594 //------------------------------------------------------------------ 595 /// List the processes matching the given partial name. 596 /// 597 /// FIXME: Is it too heavyweight to create an entire process object to do this? 598 /// The problem is for remote processes we're going to have to set up the same transport 599 /// to get this data as to actually attach. So we need to factor out transport 600 /// and process before we can do this separately from the process. 601 /// 602 /// @param[in] name 603 /// A partial name to match against the current process list. 604 /// 605 /// @param[out] matches 606 /// The list of process names matching \a name. 607 /// 608 /// @param[in] pids 609 /// A vector filled with the pids that correspond to the names in \a matches. 610 /// 611 /// @return 612 /// Returns the number of matching processes. 613 //------------------------------------------------------------------ 614 615 virtual uint32_t 616 ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids); 617 618 //------------------------------------------------------------------ 619 /// Find the architecture of a process by pid. 620 /// 621 /// FIXME: See comment for ListProcessesMatchingName. 622 /// 623 /// @param[in] pid 624 /// A pid to inspect. 625 /// 626 /// @return 627 /// Returns the architecture of the process or an invalid architecture if the process can't be found. 628 //------------------------------------------------------------------ 629 virtual ArchSpec 630 GetArchSpecForExistingProcess (lldb::pid_t pid); 631 632 //------------------------------------------------------------------ 633 /// Find the architecture of a process by name. 634 /// 635 /// FIXME: See comment for ListProcessesMatchingName. 636 /// 637 /// @param[in] process_name 638 /// The process name to inspect. 639 /// 640 /// @return 641 /// Returns the architecture of the process or an invalid architecture if the process can't be found. 642 //------------------------------------------------------------------ 643 virtual ArchSpec 644 GetArchSpecForExistingProcess (const char *process_name); 645 646 uint32_t 647 GetAddressByteSize(); 648 649 void 650 SetAddressByteSize (uint32_t addr_byte_size) 651 { 652 m_addr_byte_size = addr_byte_size; 653 } 654 655 //------------------------------------------------------------------ 656 /// Get the image information address for the current process. 657 /// 658 /// Some runtimes have system functions that can help dynamic 659 /// loaders locate the dynamic loader information needed to observe 660 /// shared libraries being loaded or unloaded. This function is 661 /// in the Process interface (as opposed to the DynamicLoader 662 /// interface) to ensure that remote debugging can take advantage of 663 /// this functionality. 664 /// 665 /// @return 666 /// The address of the dynamic loader information, or 667 /// LLDB_INVALID_ADDRESS if this is not supported by this 668 /// interface. 669 //------------------------------------------------------------------ 670 virtual lldb::addr_t 671 GetImageInfoAddress (); 672 673 //------------------------------------------------------------------ 674 /// Load a shared library into this process. 675 /// 676 /// Try and load a shared library into the current process. This 677 /// call might fail in the dynamic loader plug-in says it isn't safe 678 /// to try and load shared libraries at the moment. 679 /// 680 /// @param[in] image_spec 681 /// The image file spec that points to the shared library that 682 /// you want to load. 683 /// 684 /// @param[out] error 685 /// An error object that gets filled in with any errors that 686 /// might occur when trying to load the shared library. 687 /// 688 /// @return 689 /// A token that represents the shared library that can be 690 /// later used to unload the shared library. A value of 691 /// LLDB_INVALID_IMAGE_TOKEN will be returned if the shared 692 /// library can't be opened. 693 //------------------------------------------------------------------ 694 virtual uint32_t 695 LoadImage (const FileSpec &image_spec, Error &error); 696 697 virtual Error 698 UnloadImage (uint32_t image_token); 699 700 //------------------------------------------------------------------ 701 /// Register for process and thread notifications. 702 /// 703 /// Clients can register nofication callbacks by filling out a 704 /// Process::Notifications structure and calling this function. 705 /// 706 /// @param[in] callbacks 707 /// A structure that contains the notification baton and 708 /// callback functions. 709 /// 710 /// @see Process::Notifications 711 //------------------------------------------------------------------ 712#ifndef SWIG 713 void 714 RegisterNotificationCallbacks (const Process::Notifications& callbacks); 715#endif 716 //------------------------------------------------------------------ 717 /// Unregister for process and thread notifications. 718 /// 719 /// Clients can unregister nofication callbacks by passing a copy of 720 /// the original baton and callbacks in \a callbacks. 721 /// 722 /// @param[in] callbacks 723 /// A structure that contains the notification baton and 724 /// callback functions. 725 /// 726 /// @return 727 /// Returns \b true if the notification callbacks were 728 /// successfully removed from the process, \b false otherwise. 729 /// 730 /// @see Process::Notifications 731 //------------------------------------------------------------------ 732#ifndef SWIG 733 bool 734 UnregisterNotificationCallbacks (const Process::Notifications& callbacks); 735#endif 736 //================================================================== 737 // Built in Process Control functions 738 //================================================================== 739 //------------------------------------------------------------------ 740 /// Resumes all of a process's threads as configured using the 741 /// Thread run control functions. 742 /// 743 /// Threads for a process should be updated with one of the run 744 /// control actions (resume, step, or suspend) that they should take 745 /// when the process is resumed. If no run control action is given 746 /// to a thread it will be resumed by default. 747 /// 748 /// This function is not meant to be overridden by Process 749 /// subclasses. This function will take care of disabling any 750 /// breakpoints that threads may be stopped at, single stepping, and 751 /// re-enabling breakpoints, and enabling the basic flow control 752 /// that the plug-in instances need not worry about. 753 /// 754 /// @return 755 /// Returns an error object. 756 /// 757 /// @see Thread:Resume() 758 /// @see Thread:Step() 759 /// @see Thread:Suspend() 760 //------------------------------------------------------------------ 761 Error 762 Resume (); 763 764 //------------------------------------------------------------------ 765 /// Halts a running process. 766 /// 767 /// This function is not meant to be overridden by Process 768 /// subclasses. 769 /// If the process is successfully halted, a eStateStopped 770 /// process event with GetInterrupted will be broadcast. If false, we will 771 /// halt the process with no events generated by the halt. 772 /// 773 /// @return 774 /// Returns an error object. If the error is empty, the process is halted. 775 /// otherwise the halt has failed. 776 //------------------------------------------------------------------ 777 Error 778 Halt (); 779 780 //------------------------------------------------------------------ 781 /// Detaches from a running or stopped process. 782 /// 783 /// This function is not meant to be overridden by Process 784 /// subclasses. 785 /// 786 /// @return 787 /// Returns an error object. 788 //------------------------------------------------------------------ 789 Error 790 Detach (); 791 792 //------------------------------------------------------------------ 793 /// Kills the process and shuts down all threads that were spawned 794 /// to track and monitor the process. 795 /// 796 /// This function is not meant to be overridden by Process 797 /// subclasses. 798 /// 799 /// @return 800 /// Returns an error object. 801 //------------------------------------------------------------------ 802 Error 803 Destroy(); 804 805 //------------------------------------------------------------------ 806 /// Sends a process a UNIX signal \a signal. 807 /// 808 /// This function is not meant to be overridden by Process 809 /// subclasses. 810 /// 811 /// @return 812 /// Returns an error object. 813 //------------------------------------------------------------------ 814 Error 815 Signal (int signal); 816 817 virtual UnixSignals & 818 GetUnixSignals (); 819 820 821 //================================================================== 822 // Plug-in Process Control Overrides 823 //================================================================== 824 825 //------------------------------------------------------------------ 826 /// Called before attaching to a process. 827 /// 828 /// Allow Process plug-ins to execute some code before attaching a 829 /// process. 830 /// 831 /// @return 832 /// Returns an error object. 833 //------------------------------------------------------------------ 834 virtual Error 835 WillAttachToProcessWithID (lldb::pid_t pid) 836 { 837 return Error(); 838 } 839 840 //------------------------------------------------------------------ 841 /// Called before attaching to a process. 842 /// 843 /// Allow Process plug-ins to execute some code before attaching a 844 /// process. 845 /// 846 /// @return 847 /// Returns an error object. 848 //------------------------------------------------------------------ 849 virtual Error 850 WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) 851 { 852 return Error(); 853 } 854 855 //------------------------------------------------------------------ 856 /// Attach to an existing process using a process ID. 857 /// 858 /// @param[in] pid 859 /// The process ID that we should attempt to attach to. 860 /// 861 /// @return 862 /// Returns \a pid if attaching was successful, or 863 /// LLDB_INVALID_PROCESS_ID if attaching fails. 864 //------------------------------------------------------------------ 865 virtual Error 866 DoAttachToProcessWithID (lldb::pid_t pid) = 0; 867 868 //------------------------------------------------------------------ 869 /// Attach to an existing process using a partial process name. 870 /// 871 /// @param[in] process_name 872 /// The name of the process to attach to. 873 /// 874 /// @param[in] wait_for_launch 875 /// If \b true, wait for the process to be launched and attach 876 /// as soon as possible after it does launch. If \b false, then 877 /// search for a matching process the currently exists. 878 /// 879 /// @return 880 /// Returns \a pid if attaching was successful, or 881 /// LLDB_INVALID_PROCESS_ID if attaching fails. 882 //------------------------------------------------------------------ 883 virtual Error 884 DoAttachToProcessWithName (const char *process_name, bool wait_for_launch) 885 { 886 Error error; 887 error.SetErrorString("attach by name is not supported"); 888 return error; 889 } 890 891 //------------------------------------------------------------------ 892 /// Called after attaching a process. 893 /// 894 /// Allow Process plug-ins to execute some code after attaching to 895 /// a process. 896 //------------------------------------------------------------------ 897 virtual void 898 DidAttach () {} 899 900 901 //------------------------------------------------------------------ 902 /// Called before launching to a process. 903 /// 904 /// Allow Process plug-ins to execute some code before launching a 905 /// process. 906 /// 907 /// @return 908 /// Returns an error object. 909 //------------------------------------------------------------------ 910 virtual Error 911 WillLaunch (Module* module) 912 { 913 return Error(); 914 } 915 916 //------------------------------------------------------------------ 917 /// Launch a new process. 918 /// 919 /// Launch a new process by spawning a new process using \a module's 920 /// file as the file to launch. Arguments are given in \a argv, 921 /// and the environment variables are in \a envp. Standard input 922 /// and output files can be optionally re-directed to \a stdin_path, 923 /// \a stdout_path, and \a stderr_path. 924 /// 925 /// @param[in] module 926 /// The module from which to extract the file specification and 927 /// launch. 928 /// 929 /// @param[in] argv 930 /// The argument array. 931 /// 932 /// @param[in] envp 933 /// The environment array. 934 /// 935 /// @param[in] launch_flags 936 /// Flags to modify the launch (@see lldb::LaunchFlags) 937 /// 938 /// @param[in] stdin_path 939 /// The path to use when re-directing the STDIN of the new 940 /// process. If all stdXX_path arguments are NULL, a pseudo 941 /// terminal will be used. 942 /// 943 /// @param[in] stdout_path 944 /// The path to use when re-directing the STDOUT of the new 945 /// process. If all stdXX_path arguments are NULL, a pseudo 946 /// terminal will be used. 947 /// 948 /// @param[in] stderr_path 949 /// The path to use when re-directing the STDERR of the new 950 /// process. If all stdXX_path arguments are NULL, a pseudo 951 /// terminal will be used. 952 /// 953 /// @return 954 /// A new valid process ID, or LLDB_INVALID_PROCESS_ID if 955 /// launching fails. 956 //------------------------------------------------------------------ 957 virtual Error 958 DoLaunch (Module* module, 959 char const *argv[], 960 char const *envp[], 961 uint32_t launch_flags, 962 const char *stdin_path, 963 const char *stdout_path, 964 const char *stderr_path) = 0; 965 966 //------------------------------------------------------------------ 967 /// Called after launching a process. 968 /// 969 /// Allow Process plug-ins to execute some code after launching 970 /// a process. 971 //------------------------------------------------------------------ 972 virtual void 973 DidLaunch () {} 974 975 976 977 //------------------------------------------------------------------ 978 /// Called before resuming to a process. 979 /// 980 /// Allow Process plug-ins to execute some code before resuming a 981 /// process. 982 /// 983 /// @return 984 /// Returns an error object. 985 //------------------------------------------------------------------ 986 virtual Error 987 WillResume () { return Error(); } 988 989 //------------------------------------------------------------------ 990 /// Resumes all of a process's threads as configured using the 991 /// Thread run control functions. 992 /// 993 /// Threads for a process should be updated with one of the run 994 /// control actions (resume, step, or suspend) that they should take 995 /// when the process is resumed. If no run control action is given 996 /// to a thread it will be resumed by default. 997 /// 998 /// @return 999 /// Returns \b true if the process successfully resumes using 1000 /// the thread run control actions, \b false otherwise. 1001 /// 1002 /// @see Thread:Resume() 1003 /// @see Thread:Step() 1004 /// @see Thread:Suspend() 1005 //------------------------------------------------------------------ 1006 virtual Error 1007 DoResume () = 0; 1008 1009 //------------------------------------------------------------------ 1010 /// Called after resuming a process. 1011 /// 1012 /// Allow Process plug-ins to execute some code after resuming 1013 /// a process. 1014 //------------------------------------------------------------------ 1015 virtual void 1016 DidResume () {} 1017 1018 1019 //------------------------------------------------------------------ 1020 /// Called before halting to a process. 1021 /// 1022 /// Allow Process plug-ins to execute some code before halting a 1023 /// process. 1024 /// 1025 /// @return 1026 /// Returns an error object. 1027 //------------------------------------------------------------------ 1028 virtual Error 1029 WillHalt () { return Error(); } 1030 1031 //------------------------------------------------------------------ 1032 /// Halts a running process. 1033 /// 1034 /// DoHalt must produce one and only one stop StateChanged event if it actually 1035 /// stops the process. If the stop happens through some natural event (for 1036 /// instance a SIGSTOP), then forwarding that event will do. Otherwise, you must 1037 /// generate the event manually. Note also, the private event thread is stopped when 1038 /// DoHalt is run to prevent the events generated while halting to trigger 1039 /// other state changes before the halt is complete. 1040 /// 1041 /// @param[out] caused_stop 1042 /// If true, then this Halt caused the stop, otherwise, the 1043 /// process was already stopped. 1044 /// 1045 /// @return 1046 /// Returns \b true if the process successfully halts, \b false 1047 /// otherwise. 1048 //------------------------------------------------------------------ 1049 virtual Error 1050 DoHalt (bool &caused_stop) = 0; 1051 1052 //------------------------------------------------------------------ 1053 /// Called after halting a process. 1054 /// 1055 /// Allow Process plug-ins to execute some code after halting 1056 /// a process. 1057 //------------------------------------------------------------------ 1058 virtual void 1059 DidHalt () {} 1060 1061 //------------------------------------------------------------------ 1062 /// Called before detaching from a process. 1063 /// 1064 /// Allow Process plug-ins to execute some code before detaching 1065 /// from a process. 1066 /// 1067 /// @return 1068 /// Returns an error object. 1069 //------------------------------------------------------------------ 1070 virtual Error 1071 WillDetach () 1072 { 1073 return Error(); 1074 } 1075 1076 //------------------------------------------------------------------ 1077 /// Detaches from a running or stopped process. 1078 /// 1079 /// @return 1080 /// Returns \b true if the process successfully detaches, \b 1081 /// false otherwise. 1082 //------------------------------------------------------------------ 1083 virtual Error 1084 DoDetach () = 0; 1085 1086 //------------------------------------------------------------------ 1087 /// Called after detaching from a process. 1088 /// 1089 /// Allow Process plug-ins to execute some code after detaching 1090 /// from a process. 1091 //------------------------------------------------------------------ 1092 virtual void 1093 DidDetach () {} 1094 1095 //------------------------------------------------------------------ 1096 /// Called before sending a signal to a process. 1097 /// 1098 /// Allow Process plug-ins to execute some code before sending a 1099 /// signal to a process. 1100 /// 1101 /// @return 1102 /// Returns no error if it is safe to proceed with a call to 1103 /// Process::DoSignal(int), otherwise an error describing what 1104 /// prevents the signal from being sent. 1105 //------------------------------------------------------------------ 1106 virtual Error 1107 WillSignal () { return Error(); } 1108 1109 //------------------------------------------------------------------ 1110 /// Sends a process a UNIX signal \a signal. 1111 /// 1112 /// @return 1113 /// Returns an error object. 1114 //------------------------------------------------------------------ 1115 virtual Error 1116 DoSignal (int signal) = 0; 1117 1118 1119 1120 virtual Error 1121 WillDestroy () { return Error(); } 1122 1123 virtual Error 1124 DoDestroy () = 0; 1125 1126 virtual void 1127 DidDestroy () { } 1128 1129 1130 //------------------------------------------------------------------ 1131 /// Called after sending a signal to a process. 1132 /// 1133 /// Allow Process plug-ins to execute some code after sending a 1134 /// signal to a process. 1135 //------------------------------------------------------------------ 1136 virtual void 1137 DidSignal () {} 1138 1139 1140 //------------------------------------------------------------------ 1141 /// Currently called as part of ShouldStop. 1142 /// FIXME: Should really happen when the target stops before the 1143 /// event is taken from the queue... 1144 /// 1145 /// This callback is called as the event 1146 /// is about to be queued up to allow Process plug-ins to execute 1147 /// some code prior to clients being notified that a process was 1148 /// stopped. Common operations include updating the thread list, 1149 /// invalidating any thread state (registers, stack, etc) prior to 1150 /// letting the notification go out. 1151 /// 1152 //------------------------------------------------------------------ 1153 virtual void 1154 RefreshStateAfterStop () = 0; 1155 1156 //------------------------------------------------------------------ 1157 /// Get the target object pointer for this module. 1158 /// 1159 /// @return 1160 /// A Target object pointer to the target that owns this 1161 /// module. 1162 //------------------------------------------------------------------ 1163 Target & 1164 GetTarget (); 1165 1166 //------------------------------------------------------------------ 1167 /// Get the const target object pointer for this module. 1168 /// 1169 /// @return 1170 /// A const Target object pointer to the target that owns this 1171 /// module. 1172 //------------------------------------------------------------------ 1173 const Target & 1174 GetTarget () const; 1175 1176 //------------------------------------------------------------------ 1177 /// Get accessor for the current process state. 1178 /// 1179 /// @return 1180 /// The current state of the process. 1181 /// 1182 /// @see lldb::StateType 1183 //------------------------------------------------------------------ 1184 lldb::StateType 1185 GetState (); 1186 1187 lldb::ExecutionResults 1188 RunThreadPlan (ExecutionContext &exe_ctx, 1189 lldb::ThreadPlanSP &thread_plan_sp, 1190 bool stop_others, 1191 bool try_all_threads, 1192 bool discard_on_error, 1193 uint32_t single_thread_timeout_usec, 1194 Stream &errors); 1195 1196 static const char * 1197 ExecutionResultAsCString (lldb::ExecutionResults result); 1198 1199protected: 1200 friend class CommandObjectProcessLaunch; 1201 friend class ProcessEventData; 1202 friend class CommandObjectBreakpointCommand; 1203 1204 void 1205 SetState (lldb::EventSP &event_sp); 1206 1207 lldb::StateType 1208 GetPrivateState (); 1209 1210public: 1211 //------------------------------------------------------------------ 1212 /// Get the exit status for a process. 1213 /// 1214 /// @return 1215 /// The process's return code, or -1 if the current process 1216 /// state is not eStateExited. 1217 //------------------------------------------------------------------ 1218 int 1219 GetExitStatus (); 1220 1221 //------------------------------------------------------------------ 1222 /// Get a textual description of what the process exited. 1223 /// 1224 /// @return 1225 /// The textual description of why the process exited, or NULL 1226 /// if there is no description available. 1227 //------------------------------------------------------------------ 1228 const char * 1229 GetExitDescription (); 1230 1231 1232 virtual void 1233 DidExit () 1234 { 1235 } 1236 1237 //------------------------------------------------------------------ 1238 /// Get the number of times this process has posted a stop event. 1239 /// 1240 /// @return 1241 /// The number of times this process has stopped while being 1242 /// debugged. 1243 //------------------------------------------------------------------ 1244 uint32_t 1245 GetStopID () const; 1246 1247 //------------------------------------------------------------------ 1248 /// Set accessor for the process exit status (return code). 1249 /// 1250 /// Sometimes a child exits and the exit can be detected by global 1251 /// functions (signal handler for SIGCHLD for example). This 1252 /// accessor allows the exit status to be set from an external 1253 /// source. 1254 /// 1255 /// Setting this will cause a eStateExited event to be posted to 1256 /// the process event queue. 1257 /// 1258 /// @param[in] exit_status 1259 /// The value for the process's return code. 1260 /// 1261 /// @see lldb::StateType 1262 //------------------------------------------------------------------ 1263 virtual bool 1264 SetExitStatus (int exit_status, const char *cstr); 1265 1266 //------------------------------------------------------------------ 1267 /// Check if a process is still alive. 1268 /// 1269 /// @return 1270 /// Returns \b true if the process is still valid, \b false 1271 /// otherwise. 1272 //------------------------------------------------------------------ 1273 virtual bool 1274 IsAlive () = 0; 1275 1276 //------------------------------------------------------------------ 1277 /// Actually do the reading of memory from a process. 1278 /// 1279 /// Subclasses must override this function and can return fewer 1280 /// bytes than requested when memory requests are too large. This 1281 /// class will break up the memory requests and keep advancing the 1282 /// arguments along as needed. 1283 /// 1284 /// @param[in] vm_addr 1285 /// A virtual load address that indicates where to start reading 1286 /// memory from. 1287 /// 1288 /// @param[in] size 1289 /// The number of bytes to read. 1290 /// 1291 /// @param[out] buf 1292 /// A byte buffer that is at least \a size bytes long that 1293 /// will receive the memory bytes. 1294 /// 1295 /// @return 1296 /// The number of bytes that were actually read into \a buf. 1297 //------------------------------------------------------------------ 1298 virtual size_t 1299 DoReadMemory (lldb::addr_t vm_addr, 1300 void *buf, 1301 size_t size, 1302 Error &error) = 0; 1303 1304 //------------------------------------------------------------------ 1305 /// Read of memory from a process. 1306 /// 1307 /// This function will read memory from the current process's 1308 /// address space and remove any traps that may have been inserted 1309 /// into the memory. 1310 /// 1311 /// This function is not meant to be overridden by Process 1312 /// subclasses, the subclasses should implement 1313 /// Process::DoReadMemory (lldb::addr_t, size_t, void *). 1314 /// 1315 /// @param[in] vm_addr 1316 /// A virtual load address that indicates where to start reading 1317 /// memory from. 1318 /// 1319 /// @param[out] buf 1320 /// A byte buffer that is at least \a size bytes long that 1321 /// will receive the memory bytes. 1322 /// 1323 /// @param[in] size 1324 /// The number of bytes to read. 1325 /// 1326 /// @return 1327 /// The number of bytes that were actually read into \a buf. If 1328 /// the returned number is greater than zero, yet less than \a 1329 /// size, then this function will get called again with \a 1330 /// vm_addr, \a buf, and \a size updated appropriately. Zero is 1331 /// returned to indicate an error. 1332 //------------------------------------------------------------------ 1333 size_t 1334 ReadMemory (lldb::addr_t vm_addr, 1335 void *buf, 1336 size_t size, 1337 Error &error); 1338 1339 size_t 1340 ReadMemoryFromInferior (lldb::addr_t vm_addr, 1341 void *buf, 1342 size_t size, 1343 Error &error); 1344 1345 //------------------------------------------------------------------ 1346 /// Reads an unsigned integer of the specified byte size from 1347 /// process memory. 1348 /// 1349 /// @param[in] load_addr 1350 /// A load address of the integer to read. 1351 /// 1352 /// @param[in] byte_size 1353 /// The size in byte of the integer to read. 1354 /// 1355 /// @param[out] error 1356 /// An error that indicates the success or failure of this 1357 /// operation. If error indicates success (error.Success()), 1358 /// then the value returned can be trusted, otherwise zero 1359 /// will be returned. 1360 /// 1361 /// @return 1362 /// The unsigned integer that was read from the process memory 1363 /// space. If the integer was smaller than a uint64_t, any 1364 /// unused upper bytes will be zero filled. If the process 1365 /// byte order differs from the host byte order, the integer 1366 /// value will be appropriately byte swapped into host byte 1367 /// order. 1368 //------------------------------------------------------------------ 1369 uint64_t 1370 ReadUnsignedInteger (lldb::addr_t load_addr, 1371 size_t byte_size, 1372 Error &error); 1373 //------------------------------------------------------------------ 1374 /// Actually do the writing of memory to a process. 1375 /// 1376 /// @param[in] vm_addr 1377 /// A virtual load address that indicates where to start writing 1378 /// memory to. 1379 /// 1380 /// @param[in] buf 1381 /// A byte buffer that is at least \a size bytes long that 1382 /// contains the data to write. 1383 /// 1384 /// @param[in] size 1385 /// The number of bytes to write. 1386 /// 1387 /// @return 1388 /// The number of bytes that were actually written. 1389 //------------------------------------------------------------------ 1390 virtual size_t 1391 DoWriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) = 0; 1392 1393 //------------------------------------------------------------------ 1394 /// Write memory to a process. 1395 /// 1396 /// This function will write memory to the current process's 1397 /// address space and maintain any traps that might be present due 1398 /// to software breakpoints. 1399 /// 1400 /// This function is not meant to be overridden by Process 1401 /// subclasses, the subclasses should implement 1402 /// Process::DoWriteMemory (lldb::addr_t, size_t, void *). 1403 /// 1404 /// @param[in] vm_addr 1405 /// A virtual load address that indicates where to start writing 1406 /// memory to. 1407 /// 1408 /// @param[in] buf 1409 /// A byte buffer that is at least \a size bytes long that 1410 /// contains the data to write. 1411 /// 1412 /// @param[in] size 1413 /// The number of bytes to write. 1414 /// 1415 /// @return 1416 /// The number of bytes that were actually written. 1417 //------------------------------------------------------------------ 1418 size_t 1419 WriteMemory (lldb::addr_t vm_addr, const void *buf, size_t size, Error &error); 1420 1421 1422 //------------------------------------------------------------------ 1423 /// Actually allocate memory in the process. 1424 /// 1425 /// This function will allocate memory in the process's address 1426 /// space. This can't rely on the generic function calling mechanism, 1427 /// since that requires this function. 1428 /// 1429 /// @param[in] size 1430 /// The size of the allocation requested. 1431 /// 1432 /// @return 1433 /// The address of the allocated buffer in the process, or 1434 /// LLDB_INVALID_ADDRESS if the allocation failed. 1435 //------------------------------------------------------------------ 1436 1437 virtual lldb::addr_t 1438 DoAllocateMemory (size_t size, uint32_t permissions, Error &error) = 0; 1439 1440 //------------------------------------------------------------------ 1441 /// The public interface to allocating memory in the process. 1442 /// 1443 /// This function will allocate memory in the process's address 1444 /// space. This can't rely on the generic function calling mechanism, 1445 /// since that requires this function. 1446 /// 1447 /// @param[in] size 1448 /// The size of the allocation requested. 1449 /// 1450 /// @param[in] permissions 1451 /// Or together any of the lldb::Permissions bits. The permissions on 1452 /// a given memory allocation can't be changed after allocation. Note 1453 /// that a block that isn't set writable can still be written on from lldb, 1454 /// just not by the process itself. 1455 /// 1456 /// @return 1457 /// The address of the allocated buffer in the process, or 1458 /// LLDB_INVALID_ADDRESS if the allocation failed. 1459 //------------------------------------------------------------------ 1460 1461 lldb::addr_t 1462 AllocateMemory (size_t size, uint32_t permissions, Error &error); 1463 1464 //------------------------------------------------------------------ 1465 /// Actually deallocate memory in the process. 1466 /// 1467 /// This function will deallocate memory in the process's address 1468 /// space that was allocated with AllocateMemory. 1469 /// 1470 /// @param[in] ptr 1471 /// A return value from AllocateMemory, pointing to the memory you 1472 /// want to deallocate. 1473 /// 1474 /// @return 1475 /// \btrue if the memory was deallocated, \bfalse otherwise. 1476 //------------------------------------------------------------------ 1477 1478 virtual Error 1479 DoDeallocateMemory (lldb::addr_t ptr) = 0; 1480 1481 //------------------------------------------------------------------ 1482 /// The public interface to deallocating memory in the process. 1483 /// 1484 /// This function will deallocate memory in the process's address 1485 /// space that was allocated with AllocateMemory. 1486 /// 1487 /// @param[in] ptr 1488 /// A return value from AllocateMemory, pointing to the memory you 1489 /// want to deallocate. 1490 /// 1491 /// @return 1492 /// \btrue if the memory was deallocated, \bfalse otherwise. 1493 //------------------------------------------------------------------ 1494 1495 Error 1496 DeallocateMemory (lldb::addr_t ptr); 1497 1498 //------------------------------------------------------------------ 1499 /// Get any available STDOUT. 1500 /// 1501 /// If the process was launched without supplying valid file paths 1502 /// for stdin, stdout, and stderr, then the Process class might 1503 /// try to cache the STDOUT for the process if it is able. Events 1504 /// will be queued indicating that there is STDOUT available that 1505 /// can be retrieved using this function. 1506 /// 1507 /// @param[out] buf 1508 /// A buffer that will receive any STDOUT bytes that are 1509 /// currently available. 1510 /// 1511 /// @param[out] buf_size 1512 /// The size in bytes for the buffer \a buf. 1513 /// 1514 /// @return 1515 /// The number of bytes written into \a buf. If this value is 1516 /// equal to \a buf_size, another call to this function should 1517 /// be made to retrieve more STDOUT data. 1518 //------------------------------------------------------------------ 1519 virtual size_t 1520 GetSTDOUT (char *buf, size_t buf_size, Error &error) 1521 { 1522 error.SetErrorString("stdout unsupported"); 1523 return 0; 1524 } 1525 1526 1527 //------------------------------------------------------------------ 1528 /// Get any available STDERR. 1529 /// 1530 /// If the process was launched without supplying valid file paths 1531 /// for stdin, stdout, and stderr, then the Process class might 1532 /// try to cache the STDERR for the process if it is able. Events 1533 /// will be queued indicating that there is STDERR available that 1534 /// can be retrieved using this function. 1535 /// 1536 /// @param[out] buf 1537 /// A buffer that will receive any STDERR bytes that are 1538 /// currently available. 1539 /// 1540 /// @param[out] buf_size 1541 /// The size in bytes for the buffer \a buf. 1542 /// 1543 /// @return 1544 /// The number of bytes written into \a buf. If this value is 1545 /// equal to \a buf_size, another call to this function should 1546 /// be made to retrieve more STDERR data. 1547 //------------------------------------------------------------------ 1548 virtual size_t 1549 GetSTDERR (char *buf, size_t buf_size, Error &error) 1550 { 1551 error.SetErrorString("stderr unsupported"); 1552 return 0; 1553 } 1554 1555 virtual size_t 1556 PutSTDIN (const char *buf, size_t buf_size, Error &error) 1557 { 1558 error.SetErrorString("stdin unsupported"); 1559 return 0; 1560 } 1561 1562 //---------------------------------------------------------------------- 1563 // Process Breakpoints 1564 //---------------------------------------------------------------------- 1565 virtual size_t 1566 GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) = 0; 1567 1568 virtual Error 1569 EnableBreakpoint (BreakpointSite *bp_site) = 0; 1570 1571 virtual Error 1572 DisableBreakpoint (BreakpointSite *bp_site) = 0; 1573 1574 // This is implemented completely using the lldb::Process API. Subclasses 1575 // don't need to implement this function unless the standard flow of 1576 // read existing opcode, write breakpoint opcode, verify breakpoint opcode 1577 // doesn't work for a specific process plug-in. 1578 virtual Error 1579 EnableSoftwareBreakpoint (BreakpointSite *bp_site); 1580 1581 // This is implemented completely using the lldb::Process API. Subclasses 1582 // don't need to implement this function unless the standard flow of 1583 // restoring original opcode in memory and verifying the restored opcode 1584 // doesn't work for a specific process plug-in. 1585 virtual Error 1586 DisableSoftwareBreakpoint (BreakpointSite *bp_site); 1587 1588 BreakpointSiteList & 1589 GetBreakpointSiteList(); 1590 1591 const BreakpointSiteList & 1592 GetBreakpointSiteList() const; 1593 1594 void 1595 DisableAllBreakpointSites (); 1596 1597 Error 1598 ClearBreakpointSiteByID (lldb::user_id_t break_id); 1599 1600 lldb::break_id_t 1601 CreateBreakpointSite (lldb::BreakpointLocationSP &owner, 1602 bool use_hardware); 1603 1604 Error 1605 DisableBreakpointSiteByID (lldb::user_id_t break_id); 1606 1607 Error 1608 EnableBreakpointSiteByID (lldb::user_id_t break_id); 1609 1610 1611 // BreakpointLocations use RemoveOwnerFromBreakpointSite to remove 1612 // themselves from the owner's list of this breakpoint sites. This has to 1613 // be a static function because you can't be sure that removing the 1614 // breakpoint from it's containing map won't delete the breakpoint site, 1615 // and doing that in an instance method isn't copasetic. 1616 void 1617 RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, 1618 lldb::user_id_t owner_loc_id, 1619 lldb::BreakpointSiteSP &bp_site_sp); 1620 1621 //---------------------------------------------------------------------- 1622 // Process Watchpoints (optional) 1623 //---------------------------------------------------------------------- 1624 virtual Error 1625 EnableWatchpoint (WatchpointLocation *bp_loc); 1626 1627 virtual Error 1628 DisableWatchpoint (WatchpointLocation *bp_loc); 1629 1630 //------------------------------------------------------------------ 1631 // Thread Queries 1632 //------------------------------------------------------------------ 1633 virtual uint32_t 1634 UpdateThreadListIfNeeded () = 0; 1635 1636 ThreadList & 1637 GetThreadList (); 1638 1639 const ThreadList & 1640 GetThreadList () const; 1641 1642 uint32_t 1643 GetNextThreadIndexID (); 1644 1645 //------------------------------------------------------------------ 1646 // Event Handling 1647 //------------------------------------------------------------------ 1648 lldb::StateType 1649 GetNextEvent (lldb::EventSP &event_sp); 1650 1651 lldb::StateType 1652 WaitForProcessToStop (const TimeValue *timeout); 1653 1654 lldb::StateType 1655 WaitForStateChangedEvents (const TimeValue *timeout, lldb::EventSP &event_sp); 1656 1657 Event * 1658 PeekAtStateChangedEvents (); 1659 1660 1661 //------------------------------------------------------------------ 1662 /// If you need to ensure that you and only you will hear about some public 1663 /// event, then make a new listener, set to listen to process events, and 1664 /// then call this with that listener. Then you will have to wait on that 1665 /// listener explicitly for events (rather than using the GetNextEvent & WaitFor* 1666 /// calls above. Be sure to call RestoreProcessEvents when you are done. 1667 /// 1668 /// @param[in] listener 1669 /// This is the new listener to whom all process events will be delivered. 1670 /// 1671 /// @return 1672 /// Returns \b true if the new listener could be installed, 1673 /// \b false otherwise. 1674 //------------------------------------------------------------------ 1675 bool 1676 HijackProcessEvents (Listener *listener); 1677 1678 //------------------------------------------------------------------ 1679 /// Restores the process event broadcasting to its normal state. 1680 /// 1681 //------------------------------------------------------------------ 1682 void 1683 RestoreProcessEvents (); 1684 1685protected: 1686 //------------------------------------------------------------------ 1687 /// This is the part of the event handling that for a process event. 1688 /// It decides what to do with the event and returns true if the 1689 /// event needs to be propagated to the user, and false otherwise. 1690 /// If the event is not propagated, this call will most likely set 1691 /// the target to executing again. 1692 /// 1693 /// @param[in] event_ptr 1694 /// This is the event we are handling. 1695 /// 1696 /// @return 1697 /// Returns \b true if the event should be reported to the 1698 /// user, \b false otherwise. 1699 //------------------------------------------------------------------ 1700 bool 1701 ShouldBroadcastEvent (Event *event_ptr); 1702 1703public: 1704 //------------------------------------------------------------------ 1705 /// Gets the byte order for this process. 1706 /// 1707 /// @return 1708 /// A valid ByteOrder enumeration, or eByteOrderInvalid. 1709 //------------------------------------------------------------------ 1710 lldb::ByteOrder 1711 GetByteOrder () const 1712 { 1713 return m_byte_order; 1714 } 1715 1716 void 1717 SetByteOrder (lldb::ByteOrder byte_order) 1718 { 1719 m_byte_order = byte_order; 1720 } 1721 1722 const ConstString & 1723 GetTargetTriple () 1724 { 1725 return m_target_triple; 1726 } 1727 1728 const ABI * 1729 GetABI (); 1730 1731 virtual DynamicLoader * 1732 GetDynamicLoader (); 1733 1734 virtual LanguageRuntime * 1735 GetLanguageRuntime (lldb::LanguageType language); 1736 1737 virtual CPPLanguageRuntime * 1738 GetCPPLanguageRuntime (); 1739 1740 virtual ObjCLanguageRuntime * 1741 GetObjCLanguageRuntime (); 1742 1743 bool 1744 IsRunning () const; 1745 1746 DynamicCheckerFunctions *GetDynamicCheckers() 1747 { 1748 return m_dynamic_checkers_ap.get(); 1749 } 1750 1751 void SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) 1752 { 1753 m_dynamic_checkers_ap.reset(dynamic_checkers); 1754 } 1755 1756 virtual bool 1757 StartNoticingNewThreads() 1758 { 1759 return true; 1760 } 1761 1762 virtual bool 1763 StopNoticingNewThreads() 1764 { 1765 return true; 1766 } 1767 1768 //------------------------------------------------------------------ 1769 // lldb::ExecutionContextScope pure virtual functions 1770 //------------------------------------------------------------------ 1771 virtual Target * 1772 CalculateTarget (); 1773 1774 virtual Process * 1775 CalculateProcess (); 1776 1777 virtual Thread * 1778 CalculateThread (); 1779 1780 virtual StackFrame * 1781 CalculateStackFrame (); 1782 1783 virtual void 1784 CalculateExecutionContext (ExecutionContext &exe_ctx); 1785 1786 lldb::ProcessSP 1787 GetSP (); 1788 1789protected: 1790 class MemoryCache 1791 { 1792 public: 1793 //------------------------------------------------------------------ 1794 // Constructors and Destructors 1795 //------------------------------------------------------------------ 1796 MemoryCache (); 1797 1798 ~MemoryCache (); 1799 1800 void 1801 Clear(); 1802 1803 void 1804 Flush (lldb::addr_t addr, size_t size); 1805 1806 size_t 1807 Read (Process *process, 1808 lldb::addr_t addr, 1809 void *dst, 1810 size_t dst_len, 1811 Error &error); 1812 1813 protected: 1814 typedef std::map<lldb::addr_t, lldb::DataBufferSP> collection; 1815 //------------------------------------------------------------------ 1816 // Classes that inherit from MemoryCache can see and modify these 1817 //------------------------------------------------------------------ 1818 uint32_t m_cache_line_byte_size; 1819 Mutex m_cache_mutex; 1820 collection m_cache; 1821 1822 private: 1823 DISALLOW_COPY_AND_ASSIGN (MemoryCache); 1824 }; 1825 1826 1827 //------------------------------------------------------------------ 1828 // Member variables 1829 //------------------------------------------------------------------ 1830 Target & m_target; ///< The target that owns this process. 1831 ThreadSafeValue<lldb::StateType> m_public_state; 1832 ThreadSafeValue<lldb::StateType> m_private_state; // The actual state of our process 1833 Broadcaster m_private_state_broadcaster; // This broadcaster feeds state changed events into the private state thread's listener. 1834 Broadcaster m_private_state_control_broadcaster; // This is the control broadcaster, used to pause, resume & stop the private state thread. 1835 Listener m_private_state_listener; // This is the listener for the private state thread. 1836 Predicate<bool> m_private_state_control_wait; /// This Predicate is used to signal that a control operation is complete. 1837 lldb::thread_t m_private_state_thread; // Thread ID for the thread that watches interal state events 1838 uint32_t m_stop_id; ///< A count of many times the process has stopped. 1839 uint32_t m_thread_index_id; ///< Each thread is created with a 1 based index that won't get re-used. 1840 int m_exit_status; ///< The exit status of the process, or -1 if not set. 1841 std::string m_exit_string; ///< A textual description of why a process exited. 1842 ThreadList m_thread_list; ///< The threads for this process. 1843 std::vector<Notifications> m_notifications; ///< The list of notifications that this process can deliver. 1844 std::vector<lldb::addr_t> m_image_tokens; 1845 Listener &m_listener; 1846 BreakpointSiteList m_breakpoint_site_list; ///< This is the list of breakpoint locations we intend 1847 ///< to insert in the target. 1848 std::auto_ptr<DynamicCheckerFunctions> m_dynamic_checkers_ap; ///< The functions used by the expression parser to validate data that expressions use. 1849 UnixSignals m_unix_signals; /// This is the current signal set for this process. 1850 ConstString m_target_triple; 1851 lldb::ByteOrder m_byte_order; /// The byte order of the process. Should be set in DidLaunch/DidAttach. 1852 uint32_t m_addr_byte_size; /// The size in bytes of an address/pointer for the inferior process. Should be set in DidLaunch/DidAttach. 1853 lldb::ABISP m_abi_sp; 1854 lldb::InputReaderSP m_process_input_reader; 1855 lldb_private::Communication m_stdio_communication; 1856 lldb_private::Mutex m_stdio_communication_mutex; 1857 std::string m_stdout_data; 1858 MemoryCache m_memory_cache; 1859 1860 typedef std::map<lldb::LanguageType, lldb::LanguageRuntimeSP> LanguageRuntimeCollection; 1861 LanguageRuntimeCollection m_language_runtimes; 1862 1863 size_t 1864 RemoveBreakpointOpcodesFromBuffer (lldb::addr_t addr, size_t size, uint8_t *buf) const; 1865 1866 void 1867 SynchronouslyNotifyStateChanged (lldb::StateType state); 1868 1869 void 1870 SetPublicState (lldb::StateType new_state); 1871 1872 void 1873 SetPrivateState (lldb::StateType state); 1874 1875 bool 1876 StartPrivateStateThread (); 1877 1878 void 1879 StopPrivateStateThread (); 1880 1881 void 1882 PausePrivateStateThread (); 1883 1884 void 1885 ResumePrivateStateThread (); 1886 1887 static void * 1888 PrivateStateThread (void *arg); 1889 1890 void * 1891 RunPrivateStateThread (); 1892 1893 void 1894 HandlePrivateEvent (lldb::EventSP &event_sp); 1895 1896 lldb::StateType 1897 WaitForProcessStopPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 1898 1899 Error 1900 CompleteAttach (); 1901 1902 1903 // This waits for both the state change broadcaster, and the control broadcaster. 1904 // If control_only, it only waits for the control broadcaster. 1905 1906 bool 1907 WaitForEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp, bool control_only); 1908 1909 lldb::StateType 1910 WaitForStateChangedEventsPrivate (const TimeValue *timeout, lldb::EventSP &event_sp); 1911 1912 lldb::StateType 1913 WaitForState (const TimeValue *timeout, 1914 const lldb::StateType *match_states, 1915 const uint32_t num_match_states); 1916 1917 size_t 1918 WriteMemoryPrivate (lldb::addr_t addr, const void *buf, size_t size, Error &error); 1919 1920 void 1921 AppendSTDOUT (const char *s, size_t len); 1922 1923 static void 1924 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); 1925 1926 void 1927 PushProcessInputReader (); 1928 1929 void 1930 PopProcessInputReader (); 1931 1932 void 1933 ResetProcessInputReader (); 1934 1935 void 1936 SetUpProcessInputReader (int file_descriptor); 1937 1938 static size_t 1939 ProcessInputReaderCallback (void *baton, 1940 InputReader &reader, 1941 lldb::InputReaderAction notification, 1942 const char *bytes, 1943 size_t bytes_len); 1944 1945 1946private: 1947 //------------------------------------------------------------------ 1948 // For Process only 1949 //------------------------------------------------------------------ 1950 void ControlPrivateStateThread (uint32_t signal); 1951 1952 DISALLOW_COPY_AND_ASSIGN (Process); 1953 1954}; 1955 1956} // namespace lldb_private 1957 1958#endif // liblldb_Process_h_ 1959