blob: 3be0b2dbabb3ca4dac9635acead9557e8b5ca81d [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
Jason Molendac611a742015-10-23 02:49:51 +000017#include <mach/mach.h>
18#include <sys/signal.h>
19#include <pthread.h>
20#include <vector>
21#include <CoreFoundation/CoreFoundation.h>
22
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "DNBDefs.h"
24#include "DNBBreakpoint.h"
25#include "DNBError.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000026#include "DNBThreadResumeActions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "MachException.h"
28#include "MachVMMemory.h"
29#include "MachTask.h"
30#include "MachThreadList.h"
31#include "PThreadCondition.h"
32#include "PThreadEvent.h"
33#include "PThreadMutex.h"
Jason Molenda705b1802014-06-13 02:37:02 +000034#include "Genealogy.h"
35#include "ThreadInfo.h"
Jason Molenda20ee21b2015-07-10 23:15:22 +000036#include "JSONGenerator.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038class DNBThreadResumeActions;
39
40class MachProcess
41{
42public:
43 //----------------------------------------------------------------------
44 // Constructors and Destructors
45 //----------------------------------------------------------------------
46 MachProcess ();
47 ~MachProcess ();
48
49 //----------------------------------------------------------------------
50 // Child process control
51 //----------------------------------------------------------------------
52 pid_t AttachForDebug (pid_t pid, char *err_str, size_t err_len);
Greg Clayton6779606a2011-01-22 23:43:18 +000053 pid_t LaunchForDebug (const char *path,
54 char const *argv[],
55 char const *envp[],
56 const char *working_directory,
57 const char *stdin_path,
58 const char *stdout_path,
59 const char *stderr_path,
60 bool no_stdio,
61 nub_launch_flavor_t launch_flavor,
Jason Molendaa3329782014-03-29 18:54:20 +000062 int disable_aslr,
63 const char *event_data,
Greg Clayton6779606a2011-01-22 23:43:18 +000064 DNBError &err);
Greg Clayton71337622011-02-24 22:24:29 +000065
66 static uint32_t GetCPUTypeForLocalProcess (pid_t pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067 static pid_t ForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], MachProcess* process, DNBError &err);
Greg Clayton6779606a2011-01-22 23:43:18 +000068 static pid_t PosixSpawnChildForPTraceDebugging (const char *path,
69 cpu_type_t cpu_type,
70 char const *argv[],
71 char const *envp[],
72 const char *working_directory,
73 const char *stdin_path,
74 const char *stdout_path,
75 const char *stderr_path,
76 bool no_stdio,
77 MachProcess* process,
78 int disable_aslr,
79 DNBError& err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 nub_addr_t GetDYLDAllImageInfosAddress ();
81 static const void * PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str);
Jason Molendac611a742015-10-23 02:49:51 +000082 static void CleanupAfterAttach (const void *attach_token, nub_launch_flavor_t launch_flavor, bool success, DNBError &err_str);
83 static nub_process_t CheckForProcess (const void *attach_token, nub_launch_flavor_t launch_flavor);
84#if defined(WITH_BKS) || defined(WITH_FBS)
85 pid_t BoardServiceLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err);
86 pid_t BoardServiceForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err);
87 bool BoardServiceSendEvent (const char *event, DNBError &error);
88#endif
Jason Molenda6acc86c2015-08-12 03:27:33 +000089 static bool GetOSVersionNumbers (uint64_t *major, uint64_t *minor, uint64_t *patch);
Jason Molendaa3329782014-03-29 18:54:20 +000090#ifdef WITH_BKS
Jason Molendaa3329782014-03-29 18:54:20 +000091 static void BKSCleanupAfterAttach (const void *attach_token, DNBError &err_str);
Jason Molendac611a742015-10-23 02:49:51 +000092#endif // WITH_BKS
93#ifdef WITH_FBS
94 static void FBSCleanupAfterAttach (const void *attach_token, DNBError &err_str);
95#endif // WITH_FBS
Jason Molenda42999a42012-02-22 02:18:59 +000096#ifdef WITH_SPRINGBOARD
Jason Molendaa3329782014-03-29 18:54:20 +000097 pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, DNBError &launch_err);
Caroline Ticef8da8632010-12-03 18:46:09 +000098 static pid_t SBForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err);
Jason Molendac611a742015-10-23 02:49:51 +000099#endif // WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 nub_addr_t LookupSymbol (const char *name, const char *shlib);
101 void SetNameToAddressCallback (DNBCallbackNameToAddress callback, void *baton)
102 {
103 m_name_to_addr_callback = callback;
104 m_name_to_addr_baton = baton;
105 }
106 void SetSharedLibraryInfoCallback (DNBCallbackCopyExecutableImageInfos callback, void *baton)
107 {
108 m_image_infos_callback = callback;
109 m_image_infos_baton = baton;
110 }
111
112 bool Resume (const DNBThreadResumeActions& thread_actions);
113 bool Signal (int signal, const struct timespec *timeout_abstime = NULL);
Greg Clayton4296c222014-04-24 19:54:32 +0000114 bool Interrupt();
Jason Molendaa3329782014-03-29 18:54:20 +0000115 bool SendEvent (const char *event, DNBError &send_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 bool Kill (const struct timespec *timeout_abstime = NULL);
117 bool Detach ();
118 nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf);
119 nub_size_t WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf);
120
121 //----------------------------------------------------------------------
122 // Path and arg accessors
123 //----------------------------------------------------------------------
124 const char * Path () const { return m_path.c_str(); }
125 size_t ArgumentCount () const { return m_args.size(); }
126 const char * ArgumentAtIndex (size_t arg_idx) const
127 {
128 if (arg_idx < m_args.size())
129 return m_args[arg_idx].c_str();
130 return NULL;
131 }
132
133 //----------------------------------------------------------------------
134 // Breakpoint functions
135 //----------------------------------------------------------------------
Greg Claytond8cf1a12013-06-12 00:46:38 +0000136 DNBBreakpoint * CreateBreakpoint (nub_addr_t addr, nub_size_t length, bool hardware);
137 bool DisableBreakpoint (nub_addr_t addr, bool remove);
138 void DisableAllBreakpoints (bool remove);
139 bool EnableBreakpoint (nub_addr_t addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 DNBBreakpointList& Breakpoints() { return m_breakpoints; }
141 const DNBBreakpointList& Breakpoints() const { return m_breakpoints; }
142
143 //----------------------------------------------------------------------
144 // Watchpoint functions
145 //----------------------------------------------------------------------
Greg Claytond8cf1a12013-06-12 00:46:38 +0000146 DNBBreakpoint * CreateWatchpoint (nub_addr_t addr, nub_size_t length, uint32_t watch_type, bool hardware);
147 bool DisableWatchpoint (nub_addr_t addr, bool remove);
148 void DisableAllWatchpoints (bool remove);
149 bool EnableWatchpoint (nub_addr_t addr);
Johnny Chen64637202012-05-23 21:09:52 +0000150 uint32_t GetNumSupportedHardwareWatchpoints () const;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 DNBBreakpointList& Watchpoints() { return m_watchpoints; }
152 const DNBBreakpointList& Watchpoints() const { return m_watchpoints; }
153
154 //----------------------------------------------------------------------
155 // Exception thread functions
156 //----------------------------------------------------------------------
157 bool StartSTDIOThread ();
158 static void * STDIOThread (void *arg);
159 void ExceptionMessageReceived (const MachException::Message& exceptionMessage);
Greg Claytonbb492892014-04-30 20:27:01 +0000160 task_t ExceptionMessageBundleComplete ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 void SharedLibrariesUpdated ();
162 nub_size_t CopyImageInfos (struct DNBExecutableImageInfo **image_infos, bool only_changed);
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000163
164 //----------------------------------------------------------------------
165 // Profile functions
166 //----------------------------------------------------------------------
Han Ming Ong8764fe72013-03-04 21:25:51 +0000167 void SetEnableAsyncProfiling (bool enable, uint64_t internal_usec, DNBProfileDataScanType scan_type);
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000168 bool IsProfilingEnabled () { return m_profile_enabled; }
Greg Claytonee2ed522015-03-09 19:45:23 +0000169 useconds_t ProfileInterval () { return m_profile_interval_usec; }
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000170 bool StartProfileThread ();
171 static void * ProfileThread (void *arg);
172 void SignalAsyncProfileData (const char *info);
173 size_t GetAsyncProfileData (char *buf, size_t buf_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
175 //----------------------------------------------------------------------
176 // Accessors
177 //----------------------------------------------------------------------
178 pid_t ProcessID () const { return m_pid; }
179 bool ProcessIDIsValid () const { return m_pid > 0; }
180 pid_t SetProcessID (pid_t pid);
181 MachTask& Task() { return m_task; }
182 const MachTask& Task() const { return m_task; }
183
184 PThreadEvent& Events() { return m_events; }
185 const DNBRegisterSetInfo *
186 GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const;
187 bool GetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *reg_value) const;
188 bool SetRegisterValue (nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value) const;
Jim Ingham279ceec2012-07-25 21:12:43 +0000189 nub_bool_t SyncThreadState (nub_thread_t tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 const char * ThreadGetName (nub_thread_t tid);
191 nub_state_t ThreadGetState (nub_thread_t tid);
Jason Molenda705b1802014-06-13 02:37:02 +0000192 ThreadInfo::QoS GetRequestedQoS (nub_thread_t tid, nub_addr_t tsd, uint64_t dti_qos_class_index);
193 nub_addr_t GetPThreadT (nub_thread_t tid);
194 nub_addr_t GetDispatchQueueT (nub_thread_t tid);
195 nub_addr_t GetTSDAddressForThread (nub_thread_t tid, uint64_t plo_pthread_tsd_base_address_offset, uint64_t plo_pthread_tsd_base_offset, uint64_t plo_pthread_tsd_entry_size);
Jason Molenda20ee21b2015-07-10 23:15:22 +0000196 JSONGenerator::ObjectSP GetLoadedDynamicLibrariesInfos (nub_process_t pid, nub_addr_t image_list_address, nub_addr_t image_count);
Jason Molenda705b1802014-06-13 02:37:02 +0000197
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 nub_size_t GetNumThreads () const;
199 nub_thread_t GetThreadAtIndex (nub_size_t thread_idx) const;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 nub_thread_t GetCurrentThread ();
Jason Molendad5318c02013-04-03 04:18:47 +0000201 nub_thread_t GetCurrentThreadMachPort ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 nub_thread_t SetCurrentThread (nub_thread_t tid);
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000203 MachThreadList & GetThreadList() { return m_thread_list; }
Greg Clayton15fc2be2013-05-21 01:00:52 +0000204 bool GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 void DumpThreadStoppedReason(nub_thread_t tid) const;
206 const char * GetThreadInfo (nub_thread_t tid) const;
207
Jason Molenda1c739112013-02-22 07:27:08 +0000208 nub_thread_t GetThreadIDForMachPortNumber (thread_t mach_port_number) const;
209
Greg Clayton71337622011-02-24 22:24:29 +0000210 uint32_t GetCPUType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 nub_state_t GetState ();
212 void SetState (nub_state_t state);
213 bool IsRunning (nub_state_t state)
214 {
215 return state == eStateRunning || IsStepping(state);
216 }
217 bool IsStepping (nub_state_t state)
218 {
219 return state == eStateStepping;
220 }
221 bool CanResume (nub_state_t state)
222 {
223 return state == eStateStopped;
224 }
225
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 bool GetExitStatus(int* status)
227 {
228 if (GetState() == eStateExited)
229 {
230 if (status)
231 *status = m_exit_status;
232 return true;
233 }
234 return false;
235 }
236 void SetExitStatus(int status)
237 {
238 m_exit_status = status;
239 SetState(eStateExited);
240 }
Jason Molendaa3329782014-03-29 18:54:20 +0000241 const char * GetExitInfo ()
242 {
243 return m_exit_info.c_str();
244 }
245
Greg Claytonbb492892014-04-30 20:27:01 +0000246 void SetExitInfo (const char *info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247
248 uint32_t StopCount() const { return m_stop_count; }
249 void SetChildFileDescriptors (int stdin_fileno, int stdout_fileno, int stderr_fileno)
250 {
251 m_child_stdin = stdin_fileno;
252 m_child_stdout = stdout_fileno;
253 m_child_stderr = stderr_fileno;
254 }
255
256 int GetStdinFileDescriptor () const { return m_child_stdin; }
257 int GetStdoutFileDescriptor () const { return m_child_stdout; }
258 int GetStderrFileDescriptor () const { return m_child_stderr; }
259 void AppendSTDOUT (char* s, size_t len);
260 size_t GetAvailableSTDOUT (char *buf, size_t buf_size);
261 size_t GetAvailableSTDERR (char *buf, size_t buf_size);
262 void CloseChildFileDescriptors ()
263 {
264 if (m_child_stdin >= 0)
265 {
266 ::close (m_child_stdin);
267 m_child_stdin = -1;
268 }
269 if (m_child_stdout >= 0)
270 {
271 ::close (m_child_stdout);
272 m_child_stdout = -1;
273 }
274 if (m_child_stderr >= 0)
275 {
276 ::close (m_child_stderr);
277 m_child_stderr = -1;
278 }
279 }
280
281 bool ProcessUsingSpringBoard() const { return (m_flags & eMachProcessFlagsUsingSBS) != 0; }
Jason Molendaa3329782014-03-29 18:54:20 +0000282 bool ProcessUsingBackBoard() const { return (m_flags & eMachProcessFlagsUsingBKS) != 0; }
Jason Molenda567b1542014-06-13 02:44:21 +0000283
Jason Molenda705b1802014-06-13 02:37:02 +0000284 Genealogy::ThreadActivitySP GetGenealogyInfoForThread (nub_thread_t tid, bool &timed_out);
285
286 Genealogy::ProcessExecutableInfoSP GetGenealogyImageInfo (size_t idx);
Jason Molenda567b1542014-06-13 02:44:21 +0000287
Han Ming Ong8764fe72013-03-04 21:25:51 +0000288 DNBProfileDataScanType GetProfileScanType () { return m_profile_scan_type; }
Jason Molenda567b1542014-06-13 02:44:21 +0000289
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290private:
291 enum
292 {
293 eMachProcessFlagsNone = 0,
294 eMachProcessFlagsAttached = (1 << 0),
Jason Molendaa3329782014-03-29 18:54:20 +0000295 eMachProcessFlagsUsingSBS = (1 << 1),
Jason Molendac611a742015-10-23 02:49:51 +0000296 eMachProcessFlagsUsingBKS = (1 << 2),
297 eMachProcessFlagsUsingFBS = (1 << 3)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 };
Jim Ingham58813182014-02-25 04:53:13 +0000299 void Clear (bool detaching = false);
Greg Claytonc4e411f2011-01-18 19:36:39 +0000300 void ReplyToAllExceptions ();
301 void PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302
303 uint32_t Flags () const { return m_flags; }
Jim Ingham5d2735e2012-04-25 17:45:26 +0000304 nub_state_t DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305
306 pid_t m_pid; // Process ID of child process
Greg Clayton71337622011-02-24 22:24:29 +0000307 cpu_type_t m_cpu_type; // The CPU type of this process
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 int m_child_stdin;
309 int m_child_stdout;
310 int m_child_stderr;
311 std::string m_path; // A path to the executable if we have one
312 std::vector<std::string> m_args; // The arguments with which the process was lauched
313 int m_exit_status; // The exit status for the process
Jason Molendaa3329782014-03-29 18:54:20 +0000314 std::string m_exit_info; // Any extra info that we may have about the exit
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 MachTask m_task; // The mach task for this process
316 uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums)
317 uint32_t m_stop_count; // A count of many times have we stopped
318 pthread_t m_stdio_thread; // Thread ID for the thread that watches for child process stdio
319 PThreadMutex m_stdio_mutex; // Multithreaded protection for stdio
320 std::string m_stdout_data;
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000321
322 bool m_profile_enabled; // A flag to indicate if profiling is enabled
Greg Claytonee2ed522015-03-09 19:45:23 +0000323 useconds_t m_profile_interval_usec; // If enable, the profiling interval in microseconds
Han Ming Ong8764fe72013-03-04 21:25:51 +0000324 DNBProfileDataScanType m_profile_scan_type; // Indicates what needs to be profiled
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000325 pthread_t m_profile_thread; // Thread ID for the thread that profiles the inferior
326 PThreadMutex m_profile_data_mutex; // Multithreaded protection for profile info data
Han Ming Ong929a94f2012-11-29 22:14:45 +0000327 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 +0000328
Greg Claytonc4e411f2011-01-18 19:36:39 +0000329 DNBThreadResumeActions m_thread_actions; // The thread actions for the current MachProcess::Resume() call
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 MachException::Message::collection
331 m_exception_messages; // A collection of exception messages caught when listening to the exception port
332 PThreadMutex m_exception_messages_mutex; // Multithreaded protection for m_exception_messages
333
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000334 MachThreadList m_thread_list; // A list of threads that is maintained/updated after each stop
Jason Molenda705b1802014-06-13 02:37:02 +0000335 Genealogy m_activities; // A list of activities that is updated after every stop lazily
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 nub_state_t m_state; // The state of our process
337 PThreadMutex m_state_mutex; // Multithreaded protection for m_state
338 PThreadEvent m_events; // Process related events in the child processes lifetime can be waited upon
Greg Clayton8b2a1d62013-05-16 16:56:02 +0000339 PThreadEvent m_private_events; // Used to coordinate running and stopping the process without affecting m_events
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 DNBBreakpointList m_breakpoints; // Breakpoint list for this process
341 DNBBreakpointList m_watchpoints; // Watchpoint list for this process
342 DNBCallbackNameToAddress m_name_to_addr_callback;
343 void * m_name_to_addr_baton;
344 DNBCallbackCopyExecutableImageInfos
345 m_image_infos_callback;
346 void * m_image_infos_baton;
Jason Molendaa3329782014-03-29 18:54:20 +0000347 std::string m_bundle_id; // If we are a SB or BKS process, this will be our bundle ID.
Greg Clayton4296c222014-04-24 19:54:32 +0000348 int m_sent_interrupt_signo; // When we call MachProcess::Interrupt(), we want to send a single signal
349 // to the inferior and only send the signal if we aren't already stopped.
350 // If we end up sending a signal to stop the process we store it until we
351 // receive an exception with this signal. This helps us to verify we got
352 // the signal that interrupted the process. We might stop due to another
353 // reason after an interrupt signal is sent, so this helps us ensure that
354 // we don't report a spurious stop on the next resume.
355 int m_auto_resume_signo; // If we resume the process and still haven't received our interrupt signal
356 // acknownledgement, we will shortly after the next resume. We store the
357 // interrupt signal in this variable so when we get the interrupt signal
358 // as the sole reason for the process being stopped, we can auto resume
359 // the process.
Greg Clayton15fc2be2013-05-21 01:00:52 +0000360 bool m_did_exec;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361};
362
363
364#endif // __MachProcess_h__