blob: 8866187b56aad346548eed31a11c1cc0ca742349 [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);
Caroline Ticef8da8632010-12-03 18:46:09 +000050 pid_t LaunchForDebug (const char *path, char const *argv[], char const *envp[], const char *stdio_path, bool no_stdio, nub_launch_flavor_t launch_flavor, int disable_aslr, DNBError &err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 static pid_t ForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], MachProcess* process, DNBError &err);
Caroline Ticef8da8632010-12-03 18:46:09 +000052 static pid_t PosixSpawnChildForPTraceDebugging (const char *path, cpu_type_t cpu_type, char const *argv[], char const *envp[], const char *stdio_path, bool no_stdio, MachProcess* process, int disable_aslr, DNBError& err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 nub_addr_t GetDYLDAllImageInfosAddress ();
54 static const void * PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str);
55 static void CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str);
56 static nub_process_t CheckForProcess (const void *attach_token);
57#if defined (__arm__)
Caroline Ticef8da8632010-12-03 18:46:09 +000058 pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err);
59 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 +000060#endif
61 nub_addr_t LookupSymbol (const char *name, const char *shlib);
62 void SetNameToAddressCallback (DNBCallbackNameToAddress callback, void *baton)
63 {
64 m_name_to_addr_callback = callback;
65 m_name_to_addr_baton = baton;
66 }
67 void SetSharedLibraryInfoCallback (DNBCallbackCopyExecutableImageInfos callback, void *baton)
68 {
69 m_image_infos_callback = callback;
70 m_image_infos_baton = baton;
71 }
72
73 bool Resume (const DNBThreadResumeActions& thread_actions);
74 bool Signal (int signal, const struct timespec *timeout_abstime = NULL);
75 bool Kill (const struct timespec *timeout_abstime = NULL);
76 bool Detach ();
77 nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
78 nub_size_t WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
79
80 //----------------------------------------------------------------------
81 // Path and arg accessors
82 //----------------------------------------------------------------------
83 const char * Path () const { return m_path.c_str(); }
84 size_t ArgumentCount () const { return m_args.size(); }
85 const char * ArgumentAtIndex (size_t arg_idx) const
86 {
87 if (arg_idx < m_args.size())
88 return m_args[arg_idx].c_str();
89 return NULL;
90 }
91
92 //----------------------------------------------------------------------
93 // Breakpoint functions
94 //----------------------------------------------------------------------
95 nub_break_t CreateBreakpoint (nub_addr_t addr, nub_size_t length, bool hardware, thread_t thread);
96 bool DisableBreakpoint (nub_break_t breakID, bool remove);
97 nub_size_t DisableAllBreakpoints (bool remove);
98 bool EnableBreakpoint (nub_break_t breakID);
99 void DumpBreakpoint(nub_break_t breakID) const;
100 DNBBreakpointList& Breakpoints() { return m_breakpoints; }
101 const DNBBreakpointList& Breakpoints() const { return m_breakpoints; }
102
103 //----------------------------------------------------------------------
104 // Watchpoint functions
105 //----------------------------------------------------------------------
106 nub_watch_t CreateWatchpoint (nub_addr_t addr, nub_size_t length, uint32_t watch_type, bool hardware, thread_t thread);
107 bool DisableWatchpoint (nub_watch_t watchID, bool remove);
108 nub_size_t DisableAllWatchpoints (bool remove);
109 bool EnableWatchpoint (nub_watch_t watchID);
110 void DumpWatchpoint(nub_watch_t watchID) const;
111 DNBBreakpointList& Watchpoints() { return m_watchpoints; }
112 const DNBBreakpointList& Watchpoints() const { return m_watchpoints; }
113
114 //----------------------------------------------------------------------
115 // Exception thread functions
116 //----------------------------------------------------------------------
117 bool StartSTDIOThread ();
118 static void * STDIOThread (void *arg);
119 void ExceptionMessageReceived (const MachException::Message& exceptionMessage);
120 void ExceptionMessageBundleComplete ();
121 void SharedLibrariesUpdated ();
122 nub_size_t CopyImageInfos (struct DNBExecutableImageInfo **image_infos, bool only_changed);
123
124 //----------------------------------------------------------------------
125 // Accessors
126 //----------------------------------------------------------------------
127 pid_t ProcessID () const { return m_pid; }
128 bool ProcessIDIsValid () const { return m_pid > 0; }
129 pid_t SetProcessID (pid_t pid);
130 MachTask& Task() { return m_task; }
131 const MachTask& Task() const { return m_task; }
132
133 PThreadEvent& Events() { return m_events; }
134 const DNBRegisterSetInfo *
135 GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const;
136 bool GetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *reg_value) const;
137 bool SetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value) const;
138 const char * ThreadGetName (nub_thread_t tid);
139 nub_state_t ThreadGetState (nub_thread_t tid);
140 nub_size_t GetNumThreads () const;
141 nub_thread_t GetThreadAtIndex (nub_size_t thread_idx) const;
142 uint32_t GetThreadIndexFromThreadID (nub_thread_t tid);
143 nub_thread_t GetCurrentThread ();
144 nub_thread_t SetCurrentThread (nub_thread_t tid);
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000145 MachThreadList & GetThreadList() { return m_thread_list; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 bool GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info) const;
147 void DumpThreadStoppedReason(nub_thread_t tid) const;
148 const char * GetThreadInfo (nub_thread_t tid) const;
149
150 nub_state_t GetState ();
151 void SetState (nub_state_t state);
152 bool IsRunning (nub_state_t state)
153 {
154 return state == eStateRunning || IsStepping(state);
155 }
156 bool IsStepping (nub_state_t state)
157 {
158 return state == eStateStepping;
159 }
160 bool CanResume (nub_state_t state)
161 {
162 return state == eStateStopped;
163 }
164
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 bool GetExitStatus(int* status)
166 {
167 if (GetState() == eStateExited)
168 {
169 if (status)
170 *status = m_exit_status;
171 return true;
172 }
173 return false;
174 }
175 void SetExitStatus(int status)
176 {
177 m_exit_status = status;
178 SetState(eStateExited);
179 }
180
181 uint32_t StopCount() const { return m_stop_count; }
182 void SetChildFileDescriptors (int stdin_fileno, int stdout_fileno, int stderr_fileno)
183 {
184 m_child_stdin = stdin_fileno;
185 m_child_stdout = stdout_fileno;
186 m_child_stderr = stderr_fileno;
187 }
188
189 int GetStdinFileDescriptor () const { return m_child_stdin; }
190 int GetStdoutFileDescriptor () const { return m_child_stdout; }
191 int GetStderrFileDescriptor () const { return m_child_stderr; }
192 void AppendSTDOUT (char* s, size_t len);
193 size_t GetAvailableSTDOUT (char *buf, size_t buf_size);
194 size_t GetAvailableSTDERR (char *buf, size_t buf_size);
195 void CloseChildFileDescriptors ()
196 {
197 if (m_child_stdin >= 0)
198 {
199 ::close (m_child_stdin);
200 m_child_stdin = -1;
201 }
202 if (m_child_stdout >= 0)
203 {
204 ::close (m_child_stdout);
205 m_child_stdout = -1;
206 }
207 if (m_child_stderr >= 0)
208 {
209 ::close (m_child_stderr);
210 m_child_stderr = -1;
211 }
212 }
213
214 bool ProcessUsingSpringBoard() const { return (m_flags & eMachProcessFlagsUsingSBS) != 0; }
215private:
216 enum
217 {
218 eMachProcessFlagsNone = 0,
219 eMachProcessFlagsAttached = (1 << 0),
220 eMachProcessFlagsUsingSBS = (1 << 1)
221 };
222 void Clear ();
Greg Claytonc4e411f2011-01-18 19:36:39 +0000223 void ReplyToAllExceptions ();
224 void PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 nub_size_t RemoveTrapsFromBuffer (nub_addr_t addr, nub_size_t size, uint8_t *buf) const;
226
227 uint32_t Flags () const { return m_flags; }
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000228 nub_state_t DoSIGSTOP (bool clear_bps_and_wps, uint32_t *thread_idx_ptr = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229
230 pid_t m_pid; // Process ID of child process
231 int m_child_stdin;
232 int m_child_stdout;
233 int m_child_stderr;
234 std::string m_path; // A path to the executable if we have one
235 std::vector<std::string> m_args; // The arguments with which the process was lauched
236 int m_exit_status; // The exit status for the process
237 MachTask m_task; // The mach task for this process
238 uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums)
239 uint32_t m_stop_count; // A count of many times have we stopped
240 pthread_t m_stdio_thread; // Thread ID for the thread that watches for child process stdio
241 PThreadMutex m_stdio_mutex; // Multithreaded protection for stdio
242 std::string m_stdout_data;
Greg Claytonc4e411f2011-01-18 19:36:39 +0000243 DNBThreadResumeActions m_thread_actions; // The thread actions for the current MachProcess::Resume() call
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 MachException::Message::collection
245 m_exception_messages; // A collection of exception messages caught when listening to the exception port
246 PThreadMutex m_exception_messages_mutex; // Multithreaded protection for m_exception_messages
247
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000248 MachThreadList m_thread_list; // A list of threads that is maintained/updated after each stop
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 nub_state_t m_state; // The state of our process
250 PThreadMutex m_state_mutex; // Multithreaded protection for m_state
251 PThreadEvent m_events; // Process related events in the child processes lifetime can be waited upon
252 DNBBreakpointList m_breakpoints; // Breakpoint list for this process
253 DNBBreakpointList m_watchpoints; // Watchpoint list for this process
254 DNBCallbackNameToAddress m_name_to_addr_callback;
255 void * m_name_to_addr_baton;
256 DNBCallbackCopyExecutableImageInfos
257 m_image_infos_callback;
258 void * m_image_infos_baton;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259};
260
261
262#endif // __MachProcess_h__