1 //===-- MachProcess.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 // Created by Greg Clayton on 6/15/07. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef __MachProcess_h__ 15 #define __MachProcess_h__ 16 17 #include "DNBDefs.h" 18 #include "DNBBreakpoint.h" 19 #include "DNBError.h" 20 #include "DNBThreadResumeActions.h" 21 #include "MachException.h" 22 #include "MachVMMemory.h" 23 #include "MachTask.h" 24 #include "MachThreadList.h" 25 #include "PThreadCondition.h" 26 #include "PThreadEvent.h" 27 #include "PThreadMutex.h" 28 29 #include <mach/mach.h> 30 #include <sys/signal.h> 31 #include <pthread.h> 32 #include <vector> 33 34 class DNBThreadResumeActions; 35 36 class MachProcess 37 { 38 public: 39 //---------------------------------------------------------------------- 40 // Constructors and Destructors 41 //---------------------------------------------------------------------- 42 MachProcess (); 43 ~MachProcess (); 44 45 //---------------------------------------------------------------------- 46 // Child process control 47 //---------------------------------------------------------------------- 48 pid_t AttachForDebug (pid_t pid, char *err_str, size_t err_len); 49 pid_t LaunchForDebug (const char *path, 50 char const *argv[], 51 char const *envp[], 52 const char *working_directory, 53 const char *stdin_path, 54 const char *stdout_path, 55 const char *stderr_path, 56 bool no_stdio, 57 nub_launch_flavor_t launch_flavor, 58 int disable_aslr, 59 const char *event_data, 60 DNBError &err); 61 62 static uint32_t GetCPUTypeForLocalProcess (pid_t pid); 63 static pid_t ForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], MachProcess* process, DNBError &err); 64 static pid_t PosixSpawnChildForPTraceDebugging (const char *path, 65 cpu_type_t cpu_type, 66 char const *argv[], 67 char const *envp[], 68 const char *working_directory, 69 const char *stdin_path, 70 const char *stdout_path, 71 const char *stderr_path, 72 bool no_stdio, 73 MachProcess* process, 74 int disable_aslr, 75 DNBError& err); 76 nub_addr_t GetDYLDAllImageInfosAddress (); 77 static const void * PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str); 78 static void CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str); 79 static nub_process_t CheckForProcess (const void *attach_token); 80 #ifdef WITH_BKS 81 pid_t BKSLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err); 82 pid_t BKSForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err); 83 bool BKSSendEvent (const char *event, DNBError &error); 84 static void BKSCleanupAfterAttach (const void *attach_token, DNBError &err_str); 85 #endif 86 #ifdef WITH_SPRINGBOARD 87 pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, DNBError &launch_err); 88 static pid_t SBForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err); 89 #endif 90 nub_addr_t LookupSymbol (const char *name, const char *shlib); 91 void SetNameToAddressCallback (DNBCallbackNameToAddress callback, void *baton) 92 { 93 m_name_to_addr_callback = callback; 94 m_name_to_addr_baton = baton; 95 } 96 void SetSharedLibraryInfoCallback (DNBCallbackCopyExecutableImageInfos callback, void *baton) 97 { 98 m_image_infos_callback = callback; 99 m_image_infos_baton = baton; 100 } 101 102 bool Resume (const DNBThreadResumeActions& thread_actions); 103 bool Signal (int signal, const struct timespec *timeout_abstime = NULL); 104 bool Interrupt(); 105 bool SendEvent (const char *event, DNBError &send_err); 106 bool Kill (const struct timespec *timeout_abstime = NULL); 107 bool Detach (); 108 nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf); 109 nub_size_t WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf); 110 111 //---------------------------------------------------------------------- 112 // Path and arg accessors 113 //---------------------------------------------------------------------- 114 const char * Path () const { return m_path.c_str(); } 115 size_t ArgumentCount () const { return m_args.size(); } 116 const char * ArgumentAtIndex (size_t arg_idx) const 117 { 118 if (arg_idx < m_args.size()) 119 return m_args[arg_idx].c_str(); 120 return NULL; 121 } 122 123 //---------------------------------------------------------------------- 124 // Breakpoint functions 125 //---------------------------------------------------------------------- 126 DNBBreakpoint * CreateBreakpoint (nub_addr_t addr, nub_size_t length, bool hardware); 127 bool DisableBreakpoint (nub_addr_t addr, bool remove); 128 void DisableAllBreakpoints (bool remove); 129 bool EnableBreakpoint (nub_addr_t addr); 130 DNBBreakpointList& Breakpoints() { return m_breakpoints; } 131 const DNBBreakpointList& Breakpoints() const { return m_breakpoints; } 132 133 //---------------------------------------------------------------------- 134 // Watchpoint functions 135 //---------------------------------------------------------------------- 136 DNBBreakpoint * CreateWatchpoint (nub_addr_t addr, nub_size_t length, uint32_t watch_type, bool hardware); 137 bool DisableWatchpoint (nub_addr_t addr, bool remove); 138 void DisableAllWatchpoints (bool remove); 139 bool EnableWatchpoint (nub_addr_t addr); 140 uint32_t GetNumSupportedHardwareWatchpoints () const; 141 DNBBreakpointList& Watchpoints() { return m_watchpoints; } 142 const DNBBreakpointList& Watchpoints() const { return m_watchpoints; } 143 144 //---------------------------------------------------------------------- 145 // Exception thread functions 146 //---------------------------------------------------------------------- 147 bool StartSTDIOThread (); 148 static void * STDIOThread (void *arg); 149 void ExceptionMessageReceived (const MachException::Message& exceptionMessage); 150 task_t ExceptionMessageBundleComplete (); 151 void SharedLibrariesUpdated (); 152 nub_size_t CopyImageInfos (struct DNBExecutableImageInfo **image_infos, bool only_changed); 153 154 //---------------------------------------------------------------------- 155 // Profile functions 156 //---------------------------------------------------------------------- 157 void SetEnableAsyncProfiling (bool enable, uint64_t internal_usec, DNBProfileDataScanType scan_type); 158 bool IsProfilingEnabled () { return m_profile_enabled; } 159 uint64_t ProfileInterval () { return m_profile_interval_usec; } 160 bool StartProfileThread (); 161 static void * ProfileThread (void *arg); 162 void SignalAsyncProfileData (const char *info); 163 size_t GetAsyncProfileData (char *buf, size_t buf_size); 164 165 //---------------------------------------------------------------------- 166 // Accessors 167 //---------------------------------------------------------------------- 168 pid_t ProcessID () const { return m_pid; } 169 bool ProcessIDIsValid () const { return m_pid > 0; } 170 pid_t SetProcessID (pid_t pid); 171 MachTask& Task() { return m_task; } 172 const MachTask& Task() const { return m_task; } 173 174 PThreadEvent& Events() { return m_events; } 175 const DNBRegisterSetInfo * 176 GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const; 177 bool GetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *reg_value) const; 178 bool SetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value) const; 179 nub_bool_t SyncThreadState (nub_thread_t tid); 180 const char * ThreadGetName (nub_thread_t tid); 181 nub_state_t ThreadGetState (nub_thread_t tid); 182 nub_size_t GetNumThreads () const; 183 nub_thread_t GetThreadAtIndex (nub_size_t thread_idx) const; 184 nub_thread_t GetCurrentThread (); 185 nub_thread_t GetCurrentThreadMachPort (); 186 nub_thread_t SetCurrentThread (nub_thread_t tid); 187 MachThreadList & GetThreadList() { return m_thread_list; } 188 bool GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info); 189 void DumpThreadStoppedReason(nub_thread_t tid) const; 190 const char * GetThreadInfo (nub_thread_t tid) const; 191 192 nub_thread_t GetThreadIDForMachPortNumber (thread_t mach_port_number) const; 193 194 uint32_t GetCPUType (); 195 nub_state_t GetState (); 196 void SetState (nub_state_t state); 197 bool IsRunning (nub_state_t state) 198 { 199 return state == eStateRunning || IsStepping(state); 200 } 201 bool IsStepping (nub_state_t state) 202 { 203 return state == eStateStepping; 204 } 205 bool CanResume (nub_state_t state) 206 { 207 return state == eStateStopped; 208 } 209 210 bool GetExitStatus(int* status) 211 { 212 if (GetState() == eStateExited) 213 { 214 if (status) 215 *status = m_exit_status; 216 return true; 217 } 218 return false; 219 } 220 void SetExitStatus(int status) 221 { 222 m_exit_status = status; 223 SetState(eStateExited); 224 } 225 const char * GetExitInfo () 226 { 227 return m_exit_info.c_str(); 228 } 229 230 void SetExitInfo (const char *info); 231 232 uint32_t StopCount() const { return m_stop_count; } 233 void SetChildFileDescriptors (int stdin_fileno, int stdout_fileno, int stderr_fileno) 234 { 235 m_child_stdin = stdin_fileno; 236 m_child_stdout = stdout_fileno; 237 m_child_stderr = stderr_fileno; 238 } 239 240 int GetStdinFileDescriptor () const { return m_child_stdin; } 241 int GetStdoutFileDescriptor () const { return m_child_stdout; } 242 int GetStderrFileDescriptor () const { return m_child_stderr; } 243 void AppendSTDOUT (char* s, size_t len); 244 size_t GetAvailableSTDOUT (char *buf, size_t buf_size); 245 size_t GetAvailableSTDERR (char *buf, size_t buf_size); 246 void CloseChildFileDescriptors () 247 { 248 if (m_child_stdin >= 0) 249 { 250 ::close (m_child_stdin); 251 m_child_stdin = -1; 252 } 253 if (m_child_stdout >= 0) 254 { 255 ::close (m_child_stdout); 256 m_child_stdout = -1; 257 } 258 if (m_child_stderr >= 0) 259 { 260 ::close (m_child_stderr); 261 m_child_stderr = -1; 262 } 263 } 264 265 bool ProcessUsingSpringBoard() const { return (m_flags & eMachProcessFlagsUsingSBS) != 0; } 266 bool ProcessUsingBackBoard() const { return (m_flags & eMachProcessFlagsUsingBKS) != 0; } 267 268 DNBProfileDataScanType GetProfileScanType () { return m_profile_scan_type; } 269 270 private: 271 enum 272 { 273 eMachProcessFlagsNone = 0, 274 eMachProcessFlagsAttached = (1 << 0), 275 eMachProcessFlagsUsingSBS = (1 << 1), 276 eMachProcessFlagsUsingBKS = (1 << 2) 277 }; 278 void Clear (bool detaching = false); 279 void ReplyToAllExceptions (); 280 void PrivateResume (); 281 282 uint32_t Flags () const { return m_flags; } 283 nub_state_t DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr); 284 285 pid_t m_pid; // Process ID of child process 286 cpu_type_t m_cpu_type; // The CPU type of this process 287 int m_child_stdin; 288 int m_child_stdout; 289 int m_child_stderr; 290 std::string m_path; // A path to the executable if we have one 291 std::vector<std::string> m_args; // The arguments with which the process was lauched 292 int m_exit_status; // The exit status for the process 293 std::string m_exit_info; // Any extra info that we may have about the exit 294 MachTask m_task; // The mach task for this process 295 uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums) 296 uint32_t m_stop_count; // A count of many times have we stopped 297 pthread_t m_stdio_thread; // Thread ID for the thread that watches for child process stdio 298 PThreadMutex m_stdio_mutex; // Multithreaded protection for stdio 299 std::string m_stdout_data; 300 301 bool m_profile_enabled; // A flag to indicate if profiling is enabled 302 uint64_t m_profile_interval_usec; // If enable, the profiling interval in microseconds 303 DNBProfileDataScanType m_profile_scan_type; // Indicates what needs to be profiled 304 pthread_t m_profile_thread; // Thread ID for the thread that profiles the inferior 305 PThreadMutex m_profile_data_mutex; // Multithreaded protection for profile info data 306 std::vector<std::string> m_profile_data; // Profile data, must be protected by m_profile_data_mutex 307 308 DNBThreadResumeActions m_thread_actions; // The thread actions for the current MachProcess::Resume() call 309 MachException::Message::collection 310 m_exception_messages; // A collection of exception messages caught when listening to the exception port 311 PThreadMutex m_exception_messages_mutex; // Multithreaded protection for m_exception_messages 312 313 MachThreadList m_thread_list; // A list of threads that is maintained/updated after each stop 314 nub_state_t m_state; // The state of our process 315 PThreadMutex m_state_mutex; // Multithreaded protection for m_state 316 PThreadEvent m_events; // Process related events in the child processes lifetime can be waited upon 317 PThreadEvent m_private_events; // Used to coordinate running and stopping the process without affecting m_events 318 DNBBreakpointList m_breakpoints; // Breakpoint list for this process 319 DNBBreakpointList m_watchpoints; // Watchpoint list for this process 320 DNBCallbackNameToAddress m_name_to_addr_callback; 321 void * m_name_to_addr_baton; 322 DNBCallbackCopyExecutableImageInfos 323 m_image_infos_callback; 324 void * m_image_infos_baton; 325 std::string m_bundle_id; // If we are a SB or BKS process, this will be our bundle ID. 326 int m_sent_interrupt_signo; // When we call MachProcess::Interrupt(), we want to send a single signal 327 // to the inferior and only send the signal if we aren't already stopped. 328 // If we end up sending a signal to stop the process we store it until we 329 // receive an exception with this signal. This helps us to verify we got 330 // the signal that interrupted the process. We might stop due to another 331 // reason after an interrupt signal is sent, so this helps us ensure that 332 // we don't report a spurious stop on the next resume. 333 int m_auto_resume_signo; // If we resume the process and still haven't received our interrupt signal 334 // acknownledgement, we will shortly after the next resume. We store the 335 // interrupt signal in this variable so when we get the interrupt signal 336 // as the sole reason for the process being stopped, we can auto resume 337 // the process. 338 bool m_did_exec; 339 }; 340 341 342 #endif // __MachProcess_h__ 343