blob: f58c9d4f832174e693e7336242b5d5158fd57f49 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Greg Claytonc4e411f2011-01-18 19:36:39 +000020#include "DNBThreadResumeActions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021//#include "MachDYLD.h"
22#include "MachException.h"
23#include "MachVMMemory.h"
24#include "MachTask.h"
25#include "MachThreadList.h"
26#include "PThreadCondition.h"
27#include "PThreadEvent.h"
28#include "PThreadMutex.h"
29
30#include <mach/mach.h>
31#include <sys/signal.h>
32#include <pthread.h>
33#include <vector>
34
35class DNBThreadResumeActions;
36
37class MachProcess
38{
39public:
40 //----------------------------------------------------------------------
41 // Constructors and Destructors
42 //----------------------------------------------------------------------
43 MachProcess ();
44 ~MachProcess ();
45
46 //----------------------------------------------------------------------
47 // Child process control
48 //----------------------------------------------------------------------
49 pid_t AttachForDebug (pid_t pid, char *err_str, size_t err_len);
Greg Clayton6779606a2011-01-22 23:43:18 +000050 pid_t LaunchForDebug (const char *path,
51 char const *argv[],
52 char const *envp[],
53 const char *working_directory,
54 const char *stdin_path,
55 const char *stdout_path,
56 const char *stderr_path,
57 bool no_stdio,
58 nub_launch_flavor_t launch_flavor,
59 int disable_aslr,
60 DNBError &err);
Greg Clayton71337622011-02-24 22:24:29 +000061
62 static uint32_t GetCPUTypeForLocalProcess (pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 static pid_t ForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], MachProcess* process, DNBError &err);
Greg Clayton6779606a2011-01-22 23:43:18 +000064 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076 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);
Jason Molenda42999a42012-02-22 02:18:59 +000080#ifdef WITH_SPRINGBOARD
Caroline Ticef8da8632010-12-03 18:46:09 +000081 pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err);
82 static pid_t SBForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083#endif
84 nub_addr_t LookupSymbol (const char *name, const char *shlib);
85 void SetNameToAddressCallback (DNBCallbackNameToAddress callback, void *baton)
86 {
87 m_name_to_addr_callback = callback;
88 m_name_to_addr_baton = baton;
89 }
90 void SetSharedLibraryInfoCallback (DNBCallbackCopyExecutableImageInfos callback, void *baton)
91 {
92 m_image_infos_callback = callback;
93 m_image_infos_baton = baton;
94 }
95
96 bool Resume (const DNBThreadResumeActions& thread_actions);
97 bool Signal (int signal, const struct timespec *timeout_abstime = NULL);
98 bool Kill (const struct timespec *timeout_abstime = NULL);
99 bool Detach ();
100 nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
101 nub_size_t WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
102
103 //----------------------------------------------------------------------
104 // Path and arg accessors
105 //----------------------------------------------------------------------
106 const char * Path () const { return m_path.c_str(); }
107 size_t ArgumentCount () const { return m_args.size(); }
108 const char * ArgumentAtIndex (size_t arg_idx) const
109 {
110 if (arg_idx < m_args.size())
111 return m_args[arg_idx].c_str();
112 return NULL;
113 }
114
115 //----------------------------------------------------------------------
116 // Breakpoint functions
117 //----------------------------------------------------------------------
118 nub_break_t CreateBreakpoint (nub_addr_t addr, nub_size_t length, bool hardware, thread_t thread);
119 bool DisableBreakpoint (nub_break_t breakID, bool remove);
120 nub_size_t DisableAllBreakpoints (bool remove);
121 bool EnableBreakpoint (nub_break_t breakID);
122 void DumpBreakpoint(nub_break_t breakID) const;
123 DNBBreakpointList& Breakpoints() { return m_breakpoints; }
124 const DNBBreakpointList& Breakpoints() const { return m_breakpoints; }
125
126 //----------------------------------------------------------------------
127 // Watchpoint functions
128 //----------------------------------------------------------------------
129 nub_watch_t CreateWatchpoint (nub_addr_t addr, nub_size_t length, uint32_t watch_type, bool hardware, thread_t thread);
130 bool DisableWatchpoint (nub_watch_t watchID, bool remove);
131 nub_size_t DisableAllWatchpoints (bool remove);
132 bool EnableWatchpoint (nub_watch_t watchID);
133 void DumpWatchpoint(nub_watch_t watchID) const;
Johnny Chen64637202012-05-23 21:09:52 +0000134 uint32_t GetNumSupportedHardwareWatchpoints () const;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 DNBBreakpointList& Watchpoints() { return m_watchpoints; }
136 const DNBBreakpointList& Watchpoints() const { return m_watchpoints; }
137
138 //----------------------------------------------------------------------
139 // Exception thread functions
140 //----------------------------------------------------------------------
141 bool StartSTDIOThread ();
142 static void * STDIOThread (void *arg);
143 void ExceptionMessageReceived (const MachException::Message& exceptionMessage);
144 void ExceptionMessageBundleComplete ();
145 void SharedLibrariesUpdated ();
146 nub_size_t CopyImageInfos (struct DNBExecutableImageInfo **image_infos, bool only_changed);
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000147
148 //----------------------------------------------------------------------
149 // Profile functions
150 //----------------------------------------------------------------------
Han Ming Ong8764fe72013-03-04 21:25:51 +0000151 void SetEnableAsyncProfiling (bool enable, uint64_t internal_usec, DNBProfileDataScanType scan_type);
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000152 bool IsProfilingEnabled () { return m_profile_enabled; }
153 uint64_t ProfileInterval () { return m_profile_interval_usec; }
154 bool StartProfileThread ();
155 static void * ProfileThread (void *arg);
156 void SignalAsyncProfileData (const char *info);
157 size_t GetAsyncProfileData (char *buf, size_t buf_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158
159 //----------------------------------------------------------------------
160 // Accessors
161 //----------------------------------------------------------------------
162 pid_t ProcessID () const { return m_pid; }
163 bool ProcessIDIsValid () const { return m_pid > 0; }
164 pid_t SetProcessID (pid_t pid);
165 MachTask& Task() { return m_task; }
166 const MachTask& Task() const { return m_task; }
167
168 PThreadEvent& Events() { return m_events; }
169 const DNBRegisterSetInfo *
170 GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const;
171 bool GetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *reg_value) const;
172 bool SetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value) const;
Jim Ingham279ceec2012-07-25 21:12:43 +0000173 nub_bool_t SyncThreadState (nub_thread_t tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 const char * ThreadGetName (nub_thread_t tid);
175 nub_state_t ThreadGetState (nub_thread_t tid);
176 nub_size_t GetNumThreads () const;
177 nub_thread_t GetThreadAtIndex (nub_size_t thread_idx) const;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 nub_thread_t GetCurrentThread ();
Jason Molendad5318c02013-04-03 04:18:47 +0000179 nub_thread_t GetCurrentThreadMachPort ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 nub_thread_t SetCurrentThread (nub_thread_t tid);
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000181 MachThreadList & GetThreadList() { return m_thread_list; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 bool GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info) const;
183 void DumpThreadStoppedReason(nub_thread_t tid) const;
184 const char * GetThreadInfo (nub_thread_t tid) const;
185
Jason Molenda1c739112013-02-22 07:27:08 +0000186 nub_thread_t GetThreadIDForMachPortNumber (thread_t mach_port_number) const;
187
Greg Clayton71337622011-02-24 22:24:29 +0000188 uint32_t GetCPUType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 nub_state_t GetState ();
190 void SetState (nub_state_t state);
191 bool IsRunning (nub_state_t state)
192 {
193 return state == eStateRunning || IsStepping(state);
194 }
195 bool IsStepping (nub_state_t state)
196 {
197 return state == eStateStepping;
198 }
199 bool CanResume (nub_state_t state)
200 {
201 return state == eStateStopped;
202 }
203
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 bool GetExitStatus(int* status)
205 {
206 if (GetState() == eStateExited)
207 {
208 if (status)
209 *status = m_exit_status;
210 return true;
211 }
212 return false;
213 }
214 void SetExitStatus(int status)
215 {
216 m_exit_status = status;
217 SetState(eStateExited);
218 }
219
220 uint32_t StopCount() const { return m_stop_count; }
221 void SetChildFileDescriptors (int stdin_fileno, int stdout_fileno, int stderr_fileno)
222 {
223 m_child_stdin = stdin_fileno;
224 m_child_stdout = stdout_fileno;
225 m_child_stderr = stderr_fileno;
226 }
227
228 int GetStdinFileDescriptor () const { return m_child_stdin; }
229 int GetStdoutFileDescriptor () const { return m_child_stdout; }
230 int GetStderrFileDescriptor () const { return m_child_stderr; }
231 void AppendSTDOUT (char* s, size_t len);
232 size_t GetAvailableSTDOUT (char *buf, size_t buf_size);
233 size_t GetAvailableSTDERR (char *buf, size_t buf_size);
234 void CloseChildFileDescriptors ()
235 {
236 if (m_child_stdin >= 0)
237 {
238 ::close (m_child_stdin);
239 m_child_stdin = -1;
240 }
241 if (m_child_stdout >= 0)
242 {
243 ::close (m_child_stdout);
244 m_child_stdout = -1;
245 }
246 if (m_child_stderr >= 0)
247 {
248 ::close (m_child_stderr);
249 m_child_stderr = -1;
250 }
251 }
252
253 bool ProcessUsingSpringBoard() const { return (m_flags & eMachProcessFlagsUsingSBS) != 0; }
Han Ming Ong8764fe72013-03-04 21:25:51 +0000254
255 DNBProfileDataScanType GetProfileScanType () { return m_profile_scan_type; }
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257private:
258 enum
259 {
260 eMachProcessFlagsNone = 0,
261 eMachProcessFlagsAttached = (1 << 0),
262 eMachProcessFlagsUsingSBS = (1 << 1)
263 };
264 void Clear ();
Greg Claytonc4e411f2011-01-18 19:36:39 +0000265 void ReplyToAllExceptions ();
266 void PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 nub_size_t RemoveTrapsFromBuffer (nub_addr_t addr, nub_size_t size, uint8_t *buf) const;
268
269 uint32_t Flags () const { return m_flags; }
Jim Ingham5d2735e2012-04-25 17:45:26 +0000270 nub_state_t DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
272 pid_t m_pid; // Process ID of child process
Greg Clayton71337622011-02-24 22:24:29 +0000273 cpu_type_t m_cpu_type; // The CPU type of this process
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 int m_child_stdin;
275 int m_child_stdout;
276 int m_child_stderr;
277 std::string m_path; // A path to the executable if we have one
278 std::vector<std::string> m_args; // The arguments with which the process was lauched
279 int m_exit_status; // The exit status for the process
280 MachTask m_task; // The mach task for this process
281 uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums)
282 uint32_t m_stop_count; // A count of many times have we stopped
283 pthread_t m_stdio_thread; // Thread ID for the thread that watches for child process stdio
284 PThreadMutex m_stdio_mutex; // Multithreaded protection for stdio
285 std::string m_stdout_data;
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000286
287 bool m_profile_enabled; // A flag to indicate if profiling is enabled
288 uint64_t m_profile_interval_usec; // If enable, the profiling interval in microseconds
Han Ming Ong8764fe72013-03-04 21:25:51 +0000289 DNBProfileDataScanType m_profile_scan_type; // Indicates what needs to be profiled
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000290 pthread_t m_profile_thread; // Thread ID for the thread that profiles the inferior
291 PThreadMutex m_profile_data_mutex; // Multithreaded protection for profile info data
Han Ming Ong929a94f2012-11-29 22:14:45 +0000292 std::vector<std::string> m_profile_data; // Profile data, must be protected by m_profile_data_mutex
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000293
Greg Claytonc4e411f2011-01-18 19:36:39 +0000294 DNBThreadResumeActions m_thread_actions; // The thread actions for the current MachProcess::Resume() call
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 MachException::Message::collection
296 m_exception_messages; // A collection of exception messages caught when listening to the exception port
297 PThreadMutex m_exception_messages_mutex; // Multithreaded protection for m_exception_messages
298
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000299 MachThreadList m_thread_list; // A list of threads that is maintained/updated after each stop
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 nub_state_t m_state; // The state of our process
301 PThreadMutex m_state_mutex; // Multithreaded protection for m_state
302 PThreadEvent m_events; // Process related events in the child processes lifetime can be waited upon
Greg Clayton8b2a1d62013-05-16 16:56:02 +0000303 PThreadEvent m_private_events; // Used to coordinate running and stopping the process without affecting m_events
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 DNBBreakpointList m_breakpoints; // Breakpoint list for this process
305 DNBBreakpointList m_watchpoints; // Watchpoint list for this process
306 DNBCallbackNameToAddress m_name_to_addr_callback;
307 void * m_name_to_addr_baton;
308 DNBCallbackCopyExecutableImageInfos
309 m_image_infos_callback;
310 void * m_image_infos_baton;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311};
312
313
314#endif // __MachProcess_h__