blob: 05a3774c94ffbf2b0640581236c034b29d9dafac [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- MachProcess.cpp -----------------------------------------*- 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#include "DNB.h"
15#include <mach/mach.h>
Greg Clayton708c1ab2011-10-28 01:24:12 +000016#include <signal.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include <spawn.h>
18#include <sys/fcntl.h>
19#include <sys/types.h>
20#include <sys/ptrace.h>
21#include <sys/stat.h>
Greg Clayton71337622011-02-24 22:24:29 +000022#include <sys/sysctl.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include <unistd.h>
24#include "MacOSX/CFUtils.h"
25#include "SysSignal.h"
26
27#include <algorithm>
28#include <map>
29
30#include "DNBDataRef.h"
31#include "DNBLog.h"
32#include "DNBThreadResumeActions.h"
33#include "DNBTimer.h"
34#include "MachProcess.h"
35#include "PseudoTerminal.h"
36
37#include "CFBundle.h"
38#include "CFData.h"
39#include "CFString.h"
40
41static CFStringRef CopyBundleIDForPath (const char *app_buncle_path, DNBError &err_str);
42
Jason Molenda42999a42012-02-22 02:18:59 +000043#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
45#include <CoreFoundation/CoreFoundation.h>
46#include <SpringBoardServices/SpringBoardServer.h>
47#include <SpringBoardServices/SBSWatchdogAssertion.h>
48
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049static bool
50IsSBProcess (nub_process_t pid)
51{
Jim Ingham1bfe4f62012-07-24 01:23:53 +000052 CFReleaser<CFArrayRef> appIdsForPID (::SBSCopyDisplayIdentifiersForProcessID(pid));
53 return appIdsForPID.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#endif
57
58#if 0
59#define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
60#else
61#define DEBUG_LOG(fmt, ...)
62#endif
63
64#ifndef MACH_PROCESS_USE_POSIX_SPAWN
65#define MACH_PROCESS_USE_POSIX_SPAWN 1
66#endif
67
Greg Claytonf681b942010-08-31 18:35:14 +000068#ifndef _POSIX_SPAWN_DISABLE_ASLR
69#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
70#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
72MachProcess::MachProcess() :
73 m_pid (0),
Greg Clayton71337622011-02-24 22:24:29 +000074 m_cpu_type (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 m_child_stdin (-1),
76 m_child_stdout (-1),
77 m_child_stderr (-1),
78 m_path (),
79 m_args (),
80 m_task (this),
81 m_flags (eMachProcessFlagsNone),
82 m_stdio_thread (0),
83 m_stdio_mutex (PTHREAD_MUTEX_RECURSIVE),
84 m_stdout_data (),
Greg Claytonc4e411f2011-01-18 19:36:39 +000085 m_thread_actions (),
Greg Clayton58d1c9a2010-10-18 04:14:23 +000086 m_thread_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_exception_messages (),
88 m_exception_messages_mutex (PTHREAD_MUTEX_RECURSIVE),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 m_state (eStateUnloaded),
90 m_state_mutex (PTHREAD_MUTEX_RECURSIVE),
91 m_events (0, kAllEventsMask),
92 m_breakpoints (),
93 m_watchpoints (),
94 m_name_to_addr_callback(NULL),
95 m_name_to_addr_baton(NULL),
96 m_image_infos_callback(NULL),
97 m_image_infos_baton(NULL)
98{
99 DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
100}
101
102MachProcess::~MachProcess()
103{
104 DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
105 Clear();
106}
107
108pid_t
109MachProcess::SetProcessID(pid_t pid)
110{
111 // Free any previous process specific data or resources
112 Clear();
113 // Set the current PID appropriately
114 if (pid == 0)
Johnny Chenc0cd18d2010-09-28 16:34:56 +0000115 m_pid = ::getpid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 else
117 m_pid = pid;
118 return m_pid; // Return actualy PID in case a zero pid was passed in
119}
120
121nub_state_t
122MachProcess::GetState()
123{
124 // If any other threads access this we will need a mutex for it
125 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
126 return m_state;
127}
128
129const char *
130MachProcess::ThreadGetName(nub_thread_t tid)
131{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000132 return m_thread_list.GetName(tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133}
134
135nub_state_t
136MachProcess::ThreadGetState(nub_thread_t tid)
137{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000138 return m_thread_list.GetState(tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
141
142nub_size_t
143MachProcess::GetNumThreads () const
144{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000145 return m_thread_list.NumThreads();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146}
147
148nub_thread_t
149MachProcess::GetThreadAtIndex (nub_size_t thread_idx) const
150{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000151 return m_thread_list.ThreadIDAtIndex(thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
Jim Ingham279ceec2012-07-25 21:12:43 +0000154nub_bool_t
155MachProcess::SyncThreadState (nub_thread_t tid)
156{
157 MachThreadSP thread_sp(m_thread_list.GetThreadByID(tid));
158 if (!thread_sp)
159 return false;
160 kern_return_t kret = ::thread_abort_safely(thread_sp->ThreadID());
161 DNBLogThreadedIf (LOG_THREAD, "thread = 0x%4.4x calling thread_abort_safely (tid) => %u (GetGPRState() for stop_count = %u)", thread_sp->ThreadID(), kret, thread_sp->Process()->StopCount());
162
163 if (kret == KERN_SUCCESS)
164 return true;
165 else
166 return false;
167
168}
169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170nub_thread_t
171MachProcess::GetCurrentThread ()
172{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000173 return m_thread_list.CurrentThreadID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174}
175
176nub_thread_t
177MachProcess::SetCurrentThread(nub_thread_t tid)
178{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000179 return m_thread_list.SetCurrentThread(tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180}
181
182bool
183MachProcess::GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info) const
184{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000185 return m_thread_list.GetThreadStoppedReason(tid, stop_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
188void
189MachProcess::DumpThreadStoppedReason(nub_thread_t tid) const
190{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000191 return m_thread_list.DumpThreadStoppedReason(tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
194const char *
195MachProcess::GetThreadInfo(nub_thread_t tid) const
196{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000197 return m_thread_list.GetThreadInfo(tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
Greg Clayton71337622011-02-24 22:24:29 +0000200uint32_t
201MachProcess::GetCPUType ()
202{
203 if (m_cpu_type == 0 && m_pid != 0)
204 m_cpu_type = MachProcess::GetCPUTypeForLocalProcess (m_pid);
205 return m_cpu_type;
206}
207
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208const DNBRegisterSetInfo *
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000209MachProcess::GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210{
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000211 MachThreadSP thread_sp (m_thread_list.GetThreadByID (tid));
212 if (thread_sp)
Greg Clayton3af9ea52010-11-18 05:57:03 +0000213 {
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000214 DNBArchProtocol *arch = thread_sp->GetArchProtocol();
Greg Clayton3af9ea52010-11-18 05:57:03 +0000215 if (arch)
216 return arch->GetRegisterSetInfo (num_reg_sets);
217 }
218 *num_reg_sets = 0;
219 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220}
221
222bool
223MachProcess::GetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *value ) const
224{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000225 return m_thread_list.GetRegisterValue(tid, set, reg, value);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226}
227
228bool
229MachProcess::SetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value ) const
230{
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000231 return m_thread_list.SetRegisterValue(tid, set, reg, value);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
234void
235MachProcess::SetState(nub_state_t new_state)
236{
237 // If any other threads access this we will need a mutex for it
238 uint32_t event_mask = 0;
239
240 // Scope for mutex locker
241 {
242 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
243 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SetState ( %s )", DNBStateAsString(new_state));
244
245 const nub_state_t old_state = m_state;
246
247 if (old_state != new_state)
248 {
249 if (NUB_STATE_IS_STOPPED(new_state))
250 event_mask = eEventProcessStoppedStateChanged;
251 else
252 event_mask = eEventProcessRunningStateChanged;
253
254 m_state = new_state;
255 if (new_state == eStateStopped)
256 m_stop_count++;
257 }
258 }
259
260 if (event_mask != 0)
261 {
262 m_events.SetEvents (event_mask);
263
264 // Wait for the event bit to reset if a reset ACK is requested
265 m_events.WaitForResetAck(event_mask);
266 }
267
268}
269
270void
271MachProcess::Clear()
272{
273 // Clear any cached thread list while the pid and task are still valid
274
275 m_task.Clear();
276 // Now clear out all member variables
277 m_pid = INVALID_NUB_PROCESS;
278 CloseChildFileDescriptors();
279 m_path.clear();
280 m_args.clear();
281 SetState(eStateUnloaded);
282 m_flags = eMachProcessFlagsNone;
283 m_stop_count = 0;
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000284 m_thread_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 {
286 PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
287 m_exception_messages.clear();
288 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289}
290
291
292bool
293MachProcess::StartSTDIOThread()
294{
295 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( )", __FUNCTION__);
296 // Create the thread that watches for the child STDIO
Greg Clayton3382c2c2010-07-30 23:14:42 +0000297 return ::pthread_create (&m_stdio_thread, NULL, MachProcess::STDIOThread, this) == 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298}
299
300
301nub_addr_t
302MachProcess::LookupSymbol(const char *name, const char *shlib)
303{
304 if (m_name_to_addr_callback != NULL && name && name[0])
305 return m_name_to_addr_callback(ProcessID(), name, shlib, m_name_to_addr_baton);
306 return INVALID_NUB_ADDRESS;
307}
308
309bool
310MachProcess::Resume (const DNBThreadResumeActions& thread_actions)
311{
312 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Resume ()");
313 nub_state_t state = GetState();
314
315 if (CanResume(state))
316 {
Greg Claytonc4e411f2011-01-18 19:36:39 +0000317 m_thread_actions = thread_actions;
318 PrivateResume();
Greg Clayton3382c2c2010-07-30 23:14:42 +0000319 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 }
321 else if (state == eStateRunning)
322 {
323 DNBLogThreadedIf(LOG_PROCESS, "Resume() - task 0x%x is running, ignoring...", m_task.TaskPort());
Greg Clayton3382c2c2010-07-30 23:14:42 +0000324 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 }
Greg Clayton3382c2c2010-07-30 23:14:42 +0000326 DNBLogThreadedIf(LOG_PROCESS, "Resume() - task 0x%x can't continue, ignoring...", m_task.TaskPort());
327 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328}
329
330bool
331MachProcess::Kill (const struct timespec *timeout_abstime)
332{
333 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill ()");
Jim Ingham5d2735e2012-04-25 17:45:26 +0000334 nub_state_t state = DoSIGSTOP(true, false, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill() DoSIGSTOP() state = %s", DNBStateAsString(state));
336 errno = 0;
337 ::ptrace (PT_KILL, m_pid, 0, 0);
Greg Clayton3382c2c2010-07-30 23:14:42 +0000338 DNBError err;
339 err.SetErrorToErrno();
Greg Clayton490fbbe2011-10-28 22:59:14 +0000340 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill() DoSIGSTOP() ::ptrace (PT_KILL, pid=%u, 0, 0) => 0x%8.8x (%s)", m_pid, err.Error(), err.AsString());
Greg Claytonc4e411f2011-01-18 19:36:39 +0000341 m_thread_actions = DNBThreadResumeActions (eStateRunning, 0);
342 PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 return true;
344}
345
346bool
347MachProcess::Signal (int signal, const struct timespec *timeout_abstime)
348{
349 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p)", signal, timeout_abstime);
350 nub_state_t state = GetState();
351 if (::kill (ProcessID(), signal) == 0)
352 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 // If we were running and we have a timeout, wait for the signal to stop
354 if (IsRunning(state) && timeout_abstime)
355 {
356 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p) waiting for signal to stop process...", signal, timeout_abstime);
357 m_events.WaitForSetEvents(eEventProcessStoppedStateChanged, timeout_abstime);
358 state = GetState();
359 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p) state = %s", signal, timeout_abstime, DNBStateAsString(state));
360 return !IsRunning (state);
361 }
362 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p) not waiting...", signal, timeout_abstime);
Greg Clayton3382c2c2010-07-30 23:14:42 +0000363 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 }
Greg Clayton3382c2c2010-07-30 23:14:42 +0000365 DNBError err(errno, DNBError::POSIX);
366 err.LogThreadedIfError("kill (pid = %d, signo = %i)", ProcessID(), signal);
367 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
369}
370
371nub_state_t
Jim Ingham5d2735e2012-04-25 17:45:26 +0000372MachProcess::DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373{
374 nub_state_t state = GetState();
375 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s", DNBStateAsString (state));
376
377 if (!IsRunning(state))
378 {
379 if (clear_bps_and_wps)
380 {
381 DisableAllBreakpoints (true);
382 DisableAllWatchpoints (true);
383 clear_bps_and_wps = false;
384 }
385
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000386 // If we already have a thread stopped due to a SIGSTOP, we don't have
387 // to do anything...
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000388 uint32_t thread_idx = m_thread_list.GetThreadIndexForThreadStoppedWithSignal (SIGSTOP);
389 if (thread_idx_ptr)
390 *thread_idx_ptr = thread_idx;
391 if (thread_idx != UINT32_MAX)
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000392 return GetState();
393
394 // No threads were stopped with a SIGSTOP, we need to run and halt the
395 // process with a signal
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s -- resuming process", DNBStateAsString (state));
Jim Ingham5d2735e2012-04-25 17:45:26 +0000397 if (allow_running)
398 m_thread_actions = DNBThreadResumeActions (eStateRunning, 0);
399 else
400 m_thread_actions = DNBThreadResumeActions (eStateSuspended, 0);
401
Greg Claytonc4e411f2011-01-18 19:36:39 +0000402 PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403
404 // Reset the event that says we were indeed running
405 m_events.ResetEvents(eEventProcessRunningStateChanged);
406 state = GetState();
407 }
408
409 // We need to be stopped in order to be able to detach, so we need
410 // to send ourselves a SIGSTOP
411
412 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s -- sending SIGSTOP", DNBStateAsString (state));
413 struct timespec sigstop_timeout;
414 DNBTimer::OffsetTimeOfDay(&sigstop_timeout, 2, 0);
415 Signal (SIGSTOP, &sigstop_timeout);
416 if (clear_bps_and_wps)
417 {
418 DisableAllBreakpoints (true);
419 DisableAllWatchpoints (true);
Greg Clayton23f59502012-07-17 03:23:13 +0000420 //clear_bps_and_wps = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 }
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000422 uint32_t thread_idx = m_thread_list.GetThreadIndexForThreadStoppedWithSignal (SIGSTOP);
423 if (thread_idx_ptr)
424 *thread_idx_ptr = thread_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 return GetState();
426}
427
428bool
429MachProcess::Detach()
430{
431 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach()");
432
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000433 uint32_t thread_idx = UINT32_MAX;
Jim Ingham5d2735e2012-04-25 17:45:26 +0000434 nub_state_t state = DoSIGSTOP(true, true, &thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach() DoSIGSTOP() returned %s", DNBStateAsString(state));
436
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000437 {
Greg Claytonc4e411f2011-01-18 19:36:39 +0000438 m_thread_actions.Clear();
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000439 DNBThreadResumeAction thread_action;
440 thread_action.tid = m_thread_list.ThreadIDAtIndex (thread_idx);
441 thread_action.state = eStateRunning;
442 thread_action.signal = -1;
443 thread_action.addr = INVALID_NUB_ADDRESS;
444
Greg Claytonc4e411f2011-01-18 19:36:39 +0000445 m_thread_actions.Append (thread_action);
446 m_thread_actions.SetDefaultThreadActionIfNeeded (eStateRunning, 0);
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000447
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000448 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449
Greg Claytonc4e411f2011-01-18 19:36:39 +0000450 ReplyToAllExceptions ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000452 }
453
454 m_task.ShutDownExcecptionThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455
456 // Detach from our process
457 errno = 0;
458 nub_process_t pid = m_pid;
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000459 int ret = ::ptrace (PT_DETACH, pid, (caddr_t)1, 0);
Greg Clayton3382c2c2010-07-30 23:14:42 +0000460 DNBError err(errno, DNBError::POSIX);
Caroline Tice5d7be2e2010-11-02 16:16:53 +0000461 if (DNBLogCheckLogBit(LOG_PROCESS) || err.Fail() || (ret != 0))
Greg Clayton3382c2c2010-07-30 23:14:42 +0000462 err.LogThreaded("::ptrace (PT_DETACH, %u, (caddr_t)1, 0)", pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
464 // Resume our task
465 m_task.Resume();
466
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 // NULL our task out as we have already retored all exception ports
468 m_task.Clear();
469
470 // Clear out any notion of the process we once were
471 Clear();
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000472
473 SetState(eStateDetached);
474
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 return true;
476}
477
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478nub_size_t
479MachProcess::RemoveTrapsFromBuffer (nub_addr_t addr, nub_size_t size, uint8_t *buf) const
480{
481 nub_size_t bytes_removed = 0;
482 const DNBBreakpoint *bp;
483 nub_addr_t intersect_addr;
484 nub_size_t intersect_size;
485 nub_size_t opcode_offset;
486 nub_size_t idx;
487 for (idx = 0; (bp = m_breakpoints.GetByIndex(idx)) != NULL; ++idx)
488 {
489 if (bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset))
490 {
491 assert(addr <= intersect_addr && intersect_addr < addr + size);
492 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
493 assert(opcode_offset + intersect_size <= bp->ByteSize());
494 nub_size_t buf_offset = intersect_addr - addr;
495 ::memcpy(buf + buf_offset, bp->SavedOpcodeBytes() + opcode_offset, intersect_size);
496 }
497 }
498 return bytes_removed;
499}
500
501//----------------------------------------------------------------------
502// ReadMemory from the MachProcess level will always remove any software
503// breakpoints from the memory buffer before returning. If you wish to
504// read memory and see those traps, read from the MachTask
505// (m_task.ReadMemory()) as that version will give you what is actually
506// in inferior memory.
507//----------------------------------------------------------------------
508nub_size_t
509MachProcess::ReadMemory (nub_addr_t addr, nub_size_t size, void *buf)
510{
511 // We need to remove any current software traps (enabled software
512 // breakpoints) that we may have placed in our tasks memory.
513
514 // First just read the memory as is
515 nub_size_t bytes_read = m_task.ReadMemory(addr, size, buf);
516
517 // Then place any opcodes that fall into this range back into the buffer
518 // before we return this to callers.
519 if (bytes_read > 0)
520 RemoveTrapsFromBuffer (addr, size, (uint8_t *)buf);
521 return bytes_read;
522}
523
524//----------------------------------------------------------------------
525// WriteMemory from the MachProcess level will always write memory around
526// any software breakpoints. Any software breakpoints will have their
527// opcodes modified if they are enabled. Any memory that doesn't overlap
528// with software breakpoints will be written to. If you wish to write to
529// inferior memory without this interference, then write to the MachTask
530// (m_task.WriteMemory()) as that version will always modify inferior
531// memory.
532//----------------------------------------------------------------------
533nub_size_t
534MachProcess::WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf)
535{
536 // We need to write any data that would go where any current software traps
537 // (enabled software breakpoints) any software traps (breakpoints) that we
538 // may have placed in our tasks memory.
539
540 std::map<nub_addr_t, DNBBreakpoint *> addr_to_bp_map;
541 DNBBreakpoint *bp;
542 nub_size_t idx;
543 for (idx = 0; (bp = m_breakpoints.GetByIndex(idx)) != NULL; ++idx)
544 {
545 if (bp->IntersectsRange(addr, size, NULL, NULL, NULL))
546 addr_to_bp_map[bp->Address()] = bp;
547 }
548
549 // If we don't have any software breakpoints that are in this buffer, then
550 // we can just write memory and be done with it.
551 if (addr_to_bp_map.empty())
552 return m_task.WriteMemory(addr, size, buf);
553
554 // If we make it here, we have some breakpoints that overlap and we need
555 // to work around them.
556
557 nub_size_t bytes_written = 0;
558 nub_addr_t intersect_addr;
559 nub_size_t intersect_size;
560 nub_size_t opcode_offset;
561 const uint8_t *ubuf = (const uint8_t *)buf;
562 std::map<nub_addr_t, DNBBreakpoint *>::iterator pos, end = addr_to_bp_map.end();
563 for (pos = addr_to_bp_map.begin(); pos != end; ++pos)
564 {
565 bp = pos->second;
566
567 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
568 assert(addr <= intersect_addr && intersect_addr < addr + size);
569 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
570 assert(opcode_offset + intersect_size <= bp->ByteSize());
571
572 // Check for bytes before this breakpoint
573 const nub_addr_t curr_addr = addr + bytes_written;
574 if (intersect_addr > curr_addr)
575 {
576 // There are some bytes before this breakpoint that we need to
577 // just write to memory
578 nub_size_t curr_size = intersect_addr - curr_addr;
579 nub_size_t curr_bytes_written = m_task.WriteMemory(curr_addr, curr_size, ubuf + bytes_written);
580 bytes_written += curr_bytes_written;
581 if (curr_bytes_written != curr_size)
582 {
583 // We weren't able to write all of the requested bytes, we
584 // are done looping and will return the number of bytes that
585 // we have written so far.
586 break;
587 }
588 }
589
590 // Now write any bytes that would cover up any software breakpoints
591 // directly into the breakpoint opcode buffer
592 ::memcpy(bp->SavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
593 bytes_written += intersect_size;
594 }
595
596 // Write any remaining bytes after the last breakpoint if we have any left
597 if (bytes_written < size)
598 bytes_written += m_task.WriteMemory(addr + bytes_written, size - bytes_written, ubuf + bytes_written);
599
600 return bytes_written;
601}
602
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603void
Greg Claytonc4e411f2011-01-18 19:36:39 +0000604MachProcess::ReplyToAllExceptions ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605{
606 PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
607 if (m_exception_messages.empty() == false)
608 {
609 MachException::Message::iterator pos;
610 MachException::Message::iterator begin = m_exception_messages.begin();
611 MachException::Message::iterator end = m_exception_messages.end();
612 for (pos = begin; pos != end; ++pos)
613 {
Greg Clayton490fbbe2011-10-28 22:59:14 +0000614 DNBLogThreadedIf(LOG_EXCEPTIONS, "Replying to exception %u...", (uint32_t)std::distance(begin, pos));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 int thread_reply_signal = 0;
616
Greg Claytonc4e411f2011-01-18 19:36:39 +0000617 const DNBThreadResumeAction *action = m_thread_actions.GetActionForThread (pos->state.thread_port, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618
619 if (action)
620 {
621 thread_reply_signal = action->signal;
622 if (thread_reply_signal)
Greg Claytonc4e411f2011-01-18 19:36:39 +0000623 m_thread_actions.SetSignalHandledForThread (pos->state.thread_port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 }
625
Greg Clayton3382c2c2010-07-30 23:14:42 +0000626 DNBError err (pos->Reply(this, thread_reply_signal));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 if (DNBLogCheckLogBit(LOG_EXCEPTIONS))
Greg Clayton3382c2c2010-07-30 23:14:42 +0000628 err.LogThreadedIfError("Error replying to exception");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
630
631 // Erase all exception message as we should have used and replied
632 // to them all already.
633 m_exception_messages.clear();
634 }
635}
636void
Greg Claytonc4e411f2011-01-18 19:36:39 +0000637MachProcess::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638{
639 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
640
Greg Claytonc4e411f2011-01-18 19:36:39 +0000641 ReplyToAllExceptions ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642// bool stepOverBreakInstruction = step;
643
644 // Let the thread prepare to resume and see if any threads want us to
645 // step over a breakpoint instruction (ProcessWillResume will modify
646 // the value of stepOverBreakInstruction).
Greg Claytonc4e411f2011-01-18 19:36:39 +0000647 m_thread_list.ProcessWillResume (this, m_thread_actions);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
649 // Set our state accordingly
Greg Claytonc4e411f2011-01-18 19:36:39 +0000650 if (m_thread_actions.NumActionsWithState(eStateStepping))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 SetState (eStateStepping);
652 else
653 SetState (eStateRunning);
654
655 // Now resume our task.
Greg Clayton3382c2c2010-07-30 23:14:42 +0000656 m_task.Resume();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657}
658
659nub_break_t
660MachProcess::CreateBreakpoint(nub_addr_t addr, nub_size_t length, bool hardware, thread_t tid)
661{
Greg Clayton490fbbe2011-10-28 22:59:14 +0000662 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %zu, hardware = %i, tid = 0x%4.4x )", (uint64_t)addr, length, hardware, tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 if (hardware && tid == INVALID_NUB_THREAD)
664 tid = GetCurrentThread();
665
666 DNBBreakpoint bp(addr, length, tid, hardware);
667 nub_break_t breakID = m_breakpoints.Add(bp);
668 if (EnableBreakpoint(breakID))
669 {
Greg Clayton490fbbe2011-10-28 22:59:14 +0000670 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%4.4x ) => %u", (uint64_t)addr, length, tid, breakID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 return breakID;
672 }
673 else
674 {
675 m_breakpoints.Remove(breakID);
676 }
677 // We failed to enable the breakpoint
678 return INVALID_NUB_BREAK_ID;
679}
680
681nub_watch_t
682MachProcess::CreateWatchpoint(nub_addr_t addr, nub_size_t length, uint32_t watch_flags, bool hardware, thread_t tid)
683{
Greg Clayton490fbbe2011-10-28 22:59:14 +0000684 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %zu, flags = 0x%8.8x, hardware = %i, tid = 0x%4.4x )", (uint64_t)addr, length, watch_flags, hardware, tid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 if (hardware && tid == INVALID_NUB_THREAD)
686 tid = GetCurrentThread();
687
688 DNBBreakpoint watch(addr, length, tid, hardware);
689 watch.SetIsWatchpoint(watch_flags);
690
691 nub_watch_t watchID = m_watchpoints.Add(watch);
692 if (EnableWatchpoint(watchID))
693 {
Greg Clayton490fbbe2011-10-28 22:59:14 +0000694 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%x) => %u", (uint64_t)addr, length, tid, watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 return watchID;
696 }
697 else
698 {
Greg Clayton490fbbe2011-10-28 22:59:14 +0000699 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%x) => FAILED (%u)", (uint64_t)addr, length, tid, watchID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 m_watchpoints.Remove(watchID);
701 }
702 // We failed to enable the watchpoint
703 return INVALID_NUB_BREAK_ID;
704}
705
706nub_size_t
707MachProcess::DisableAllBreakpoints(bool remove)
708{
709 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::%s (remove = %d )", __FUNCTION__, remove);
710 DNBBreakpoint *bp;
711 nub_size_t disabled_count = 0;
712 nub_size_t idx = 0;
713 while ((bp = m_breakpoints.GetByIndex(idx)) != NULL)
714 {
715 bool success = DisableBreakpoint(bp->GetID(), remove);
716
717 if (success)
718 disabled_count++;
719 // If we failed to disable the breakpoint or we aren't removing the breakpoint
720 // increment the breakpoint index. Otherwise DisableBreakpoint will have removed
721 // the breakpoint at this index and we don't need to change it.
722 if ((success == false) || (remove == false))
723 idx++;
724 }
725 return disabled_count;
726}
727
728nub_size_t
729MachProcess::DisableAllWatchpoints(bool remove)
730{
731 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::%s (remove = %d )", __FUNCTION__, remove);
732 DNBBreakpoint *wp;
733 nub_size_t disabled_count = 0;
734 nub_size_t idx = 0;
735 while ((wp = m_watchpoints.GetByIndex(idx)) != NULL)
736 {
737 bool success = DisableWatchpoint(wp->GetID(), remove);
738
739 if (success)
740 disabled_count++;
741 // If we failed to disable the watchpoint or we aren't removing the watchpoint
742 // increment the watchpoint index. Otherwise DisableWatchpoint will have removed
743 // the watchpoint at this index and we don't need to change it.
744 if ((success == false) || (remove == false))
745 idx++;
746 }
747 return disabled_count;
748}
749
750bool
751MachProcess::DisableBreakpoint(nub_break_t breakID, bool remove)
752{
753 DNBBreakpoint *bp = m_breakpoints.FindByID (breakID);
754 if (bp)
755 {
756 nub_addr_t addr = bp->Address();
757 DNBLogThreadedIf(LOG_BREAKPOINTS | LOG_VERBOSE, "MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx", breakID, remove, (uint64_t)addr);
758
759 if (bp->IsHardware())
760 {
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000761 bool hw_disable_result = m_thread_list.DisableHardwareBreakpoint (bp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762
763 if (hw_disable_result == true)
764 {
765 bp->SetEnabled(false);
766 // Let the thread list know that a breakpoint has been modified
767 if (remove)
768 {
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000769 m_thread_list.NotifyBreakpointChanged(bp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 m_breakpoints.Remove(breakID);
771 }
772 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx (hardware) => success", breakID, remove, (uint64_t)addr);
773 return true;
774 }
775
776 return false;
777 }
778
779 const nub_size_t break_op_size = bp->ByteSize();
780 assert (break_op_size > 0);
Greg Clayton3af9ea52010-11-18 05:57:03 +0000781 const uint8_t * const break_op = DNBArchProtocol::GetBreakpointOpcode (bp->ByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 if (break_op_size > 0)
783 {
784 // Clear a software breakoint instruction
785 uint8_t curr_break_op[break_op_size];
786 bool break_op_found = false;
787
788 // Read the breakpoint opcode
789 if (m_task.ReadMemory(addr, break_op_size, curr_break_op) == break_op_size)
790 {
791 bool verify = false;
792 if (bp->IsEnabled())
793 {
794 // Make sure we have the a breakpoint opcode exists at this address
795 if (memcmp(curr_break_op, break_op, break_op_size) == 0)
796 {
797 break_op_found = true;
798 // We found a valid breakpoint opcode at this address, now restore
799 // the saved opcode.
800 if (m_task.WriteMemory(addr, break_op_size, bp->SavedOpcodeBytes()) == break_op_size)
801 {
802 verify = true;
803 }
804 else
805 {
806 DNBLogError("MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx memory write failed when restoring original opcode", breakID, remove, (uint64_t)addr);
807 }
808 }
809 else
810 {
811 DNBLogWarning("MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx expected a breakpoint opcode but didn't find one.", breakID, remove, (uint64_t)addr);
812 // Set verify to true and so we can check if the original opcode has already been restored
813 verify = true;
814 }
815 }
816 else
817 {
Greg Claytondce502e2011-11-04 03:34:56 +0000818 DNBLogThreadedIf(LOG_BREAKPOINTS | LOG_VERBOSE, "MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx is not enabled", breakID, remove, (uint64_t)addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 // Set verify to true and so we can check if the original opcode is there
820 verify = true;
821 }
822
823 if (verify)
824 {
825 uint8_t verify_opcode[break_op_size];
826 // Verify that our original opcode made it back to the inferior
827 if (m_task.ReadMemory(addr, break_op_size, verify_opcode) == break_op_size)
828 {
829 // compare the memory we just read with the original opcode
830 if (memcmp(bp->SavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
831 {
832 // SUCCESS
833 bp->SetEnabled(false);
834 // Let the thread list know that a breakpoint has been modified
835 if (remove)
836 {
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000837 m_thread_list.NotifyBreakpointChanged(bp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 m_breakpoints.Remove(breakID);
839 }
840 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx => success", breakID, remove, (uint64_t)addr);
841 return true;
842 }
843 else
844 {
845 if (break_op_found)
846 DNBLogError("MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx: failed to restore original opcode", breakID, remove, (uint64_t)addr);
847 else
848 DNBLogError("MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx: opcode changed", breakID, remove, (uint64_t)addr);
849 }
850 }
851 else
852 {
853 DNBLogWarning("MachProcess::DisableBreakpoint: unable to disable breakpoint 0x%8.8llx", (uint64_t)addr);
854 }
855 }
856 }
857 else
858 {
859 DNBLogWarning("MachProcess::DisableBreakpoint: unable to read memory at 0x%8.8llx", (uint64_t)addr);
860 }
861 }
862 }
863 else
864 {
865 DNBLogError("MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) invalid breakpoint ID", breakID, remove);
866 }
867 return false;
868}
869
870bool
871MachProcess::DisableWatchpoint(nub_watch_t watchID, bool remove)
872{
873 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::%s(watchID = %d, remove = %d)", __FUNCTION__, watchID, remove);
874 DNBBreakpoint *wp = m_watchpoints.FindByID (watchID);
875 if (wp)
876 {
877 nub_addr_t addr = wp->Address();
878 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::DisableWatchpoint ( watchID = %d, remove = %d ) addr = 0x%8.8llx", watchID, remove, (uint64_t)addr);
879
880 if (wp->IsHardware())
881 {
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000882 bool hw_disable_result = m_thread_list.DisableHardwareWatchpoint (wp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883
884 if (hw_disable_result == true)
885 {
886 wp->SetEnabled(false);
887 if (remove)
888 m_watchpoints.Remove(watchID);
889 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::Disablewatchpoint ( watchID = %d, remove = %d ) addr = 0x%8.8llx (hardware) => success", watchID, remove, (uint64_t)addr);
890 return true;
891 }
892 }
893
894 // TODO: clear software watchpoints if we implement them
895 }
896 else
897 {
898 DNBLogError("MachProcess::DisableWatchpoint ( watchID = %d, remove = %d ) invalid watchpoint ID", watchID, remove);
899 }
900 return false;
901}
902
903
904void
905MachProcess::DumpBreakpoint(nub_break_t breakID) const
906{
907 DNBLogThreaded("MachProcess::DumpBreakpoint(breakID = %d)", breakID);
908
909 if (NUB_BREAK_ID_IS_VALID(breakID))
910 {
911 const DNBBreakpoint *bp = m_breakpoints.FindByID(breakID);
912 if (bp)
913 bp->Dump();
914 else
915 DNBLog("MachProcess::DumpBreakpoint(breakID = %d): invalid breakID", breakID);
916 }
917 else
918 {
919 m_breakpoints.Dump();
920 }
921}
922
923void
924MachProcess::DumpWatchpoint(nub_watch_t watchID) const
925{
926 DNBLogThreaded("MachProcess::DumpWatchpoint(watchID = %d)", watchID);
927
928 if (NUB_BREAK_ID_IS_VALID(watchID))
929 {
930 const DNBBreakpoint *wp = m_watchpoints.FindByID(watchID);
931 if (wp)
932 wp->Dump();
933 else
934 DNBLog("MachProcess::DumpWatchpoint(watchID = %d): invalid watchID", watchID);
935 }
936 else
937 {
938 m_watchpoints.Dump();
939 }
940}
941
Johnny Chen64637202012-05-23 21:09:52 +0000942uint32_t
943MachProcess::GetNumSupportedHardwareWatchpoints () const
944{
945 return m_thread_list.NumSupportedHardwareWatchpoints();
946}
947
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948bool
949MachProcess::EnableBreakpoint(nub_break_t breakID)
950{
951 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::EnableBreakpoint ( breakID = %d )", breakID);
952 DNBBreakpoint *bp = m_breakpoints.FindByID (breakID);
953 if (bp)
954 {
955 nub_addr_t addr = bp->Address();
956 if (bp->IsEnabled())
957 {
958 DNBLogWarning("MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: breakpoint already enabled.", breakID, (uint64_t)addr);
959 return true;
960 }
961 else
962 {
963 if (bp->HardwarePreferred())
964 {
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000965 bp->SetHardwareIndex(m_thread_list.EnableHardwareBreakpoint(bp));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 if (bp->IsHardware())
967 {
968 bp->SetEnabled(true);
969 return true;
970 }
971 }
972
973 const nub_size_t break_op_size = bp->ByteSize();
974 assert (break_op_size != 0);
Greg Clayton3af9ea52010-11-18 05:57:03 +0000975 const uint8_t * const break_op = DNBArchProtocol::GetBreakpointOpcode (break_op_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976 if (break_op_size > 0)
977 {
978 // Save the original opcode by reading it
979 if (m_task.ReadMemory(addr, break_op_size, bp->SavedOpcodeBytes()) == break_op_size)
980 {
981 // Write a software breakpoint in place of the original opcode
982 if (m_task.WriteMemory(addr, break_op_size, break_op) == break_op_size)
983 {
984 uint8_t verify_break_op[4];
985 if (m_task.ReadMemory(addr, break_op_size, verify_break_op) == break_op_size)
986 {
987 if (memcmp(break_op, verify_break_op, break_op_size) == 0)
988 {
989 bp->SetEnabled(true);
990 // Let the thread list know that a breakpoint has been modified
Greg Clayton58d1c9a2010-10-18 04:14:23 +0000991 m_thread_list.NotifyBreakpointChanged(bp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: SUCCESS.", breakID, (uint64_t)addr);
993 return true;
994 }
995 else
996 {
997 DNBLogError("MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: breakpoint opcode verification failed.", breakID, (uint64_t)addr);
998 }
999 }
1000 else
1001 {
1002 DNBLogError("MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: unable to read memory to verify breakpoint opcode.", breakID, (uint64_t)addr);
1003 }
1004 }
1005 else
1006 {
1007 DNBLogError("MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: unable to write breakpoint opcode to memory.", breakID, (uint64_t)addr);
1008 }
1009 }
1010 else
1011 {
1012 DNBLogError("MachProcess::EnableBreakpoint ( breakID = %d ) addr = 0x%8.8llx: unable to read memory at breakpoint address.", breakID, (uint64_t)addr);
1013 }
1014 }
1015 else
1016 {
1017 DNBLogError("MachProcess::EnableBreakpoint ( breakID = %d ) no software breakpoint opcode for current architecture.", breakID);
1018 }
1019 }
1020 }
1021 return false;
1022}
1023
1024bool
1025MachProcess::EnableWatchpoint(nub_watch_t watchID)
1026{
1027 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::EnableWatchpoint(watchID = %d)", watchID);
1028 DNBBreakpoint *wp = m_watchpoints.FindByID (watchID);
1029 if (wp)
1030 {
1031 nub_addr_t addr = wp->Address();
1032 if (wp->IsEnabled())
1033 {
1034 DNBLogWarning("MachProcess::EnableWatchpoint(watchID = %d) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr);
1035 return true;
1036 }
1037 else
1038 {
1039 // Currently only try and set hardware watchpoints.
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001040 wp->SetHardwareIndex(m_thread_list.EnableHardwareWatchpoint(wp));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 if (wp->IsHardware())
1042 {
1043 wp->SetEnabled(true);
1044 return true;
1045 }
1046 // TODO: Add software watchpoints by doing page protection tricks.
1047 }
1048 }
1049 return false;
1050}
1051
1052// Called by the exception thread when an exception has been received from
1053// our process. The exception message is completely filled and the exception
1054// data has already been copied.
1055void
1056MachProcess::ExceptionMessageReceived (const MachException::Message& exceptionMessage)
1057{
1058 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
1059
1060 if (m_exception_messages.empty())
1061 m_task.Suspend();
1062
1063 DNBLogThreadedIf(LOG_EXCEPTIONS, "MachProcess::ExceptionMessageReceived ( )");
1064
1065 // Use a locker to automatically unlock our mutex in case of exceptions
1066 // Add the exception to our internal exception stack
1067 m_exception_messages.push_back(exceptionMessage);
1068}
1069
1070void
1071MachProcess::ExceptionMessageBundleComplete()
1072{
1073 // We have a complete bundle of exceptions for our child process.
1074 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
Greg Clayton490fbbe2011-10-28 22:59:14 +00001075 DNBLogThreadedIf(LOG_EXCEPTIONS, "%s: %zu exception messages.", __PRETTY_FUNCTION__, m_exception_messages.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 if (!m_exception_messages.empty())
1077 {
1078 // Let all threads recover from stopping and do any clean up based
1079 // on the previous thread state (if any).
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001080 m_thread_list.ProcessDidStop(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
1082 // Let each thread know of any exceptions
1083 task_t task = m_task.TaskPort();
1084 size_t i;
1085 for (i=0; i<m_exception_messages.size(); ++i)
1086 {
1087 // Let the thread list figure use the MachProcess to forward all exceptions
1088 // on down to each thread.
1089 if (m_exception_messages[i].state.task_port == task)
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001090 m_thread_list.NotifyException(m_exception_messages[i].state);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091 if (DNBLogCheckLogBit(LOG_EXCEPTIONS))
1092 m_exception_messages[i].Dump();
1093 }
1094
1095 if (DNBLogCheckLogBit(LOG_THREAD))
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001096 m_thread_list.Dump();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097
1098 bool step_more = false;
Greg Clayton58d1c9a2010-10-18 04:14:23 +00001099 if (m_thread_list.ShouldStop(step_more))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 {
1101 // Wait for the eEventProcessRunningStateChanged event to be reset
1102 // before changing state to stopped to avoid race condition with
1103 // very fast start/stops
1104 struct timespec timeout;
1105 //DNBTimer::OffsetTimeOfDay(&timeout, 0, 250 * 1000); // Wait for 250 ms
1106 DNBTimer::OffsetTimeOfDay(&timeout, 1, 0); // Wait for 250 ms
1107 m_events.WaitForEventsToReset(eEventProcessRunningStateChanged, &timeout);
1108 SetState(eStateStopped);
1109 }
1110 else
1111 {
1112 // Resume without checking our current state.
Greg Claytonc4e411f2011-01-18 19:36:39 +00001113 PrivateResume ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 }
1115 }
1116 else
1117 {
Greg Claytondce502e2011-11-04 03:34:56 +00001118 DNBLogThreadedIf(LOG_EXCEPTIONS, "%s empty exception messages bundle (%zu exceptions).", __PRETTY_FUNCTION__, m_exception_messages.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 }
1120}
1121
1122nub_size_t
1123MachProcess::CopyImageInfos ( struct DNBExecutableImageInfo **image_infos, bool only_changed)
1124{
1125 if (m_image_infos_callback != NULL)
1126 return m_image_infos_callback(ProcessID(), image_infos, only_changed, m_image_infos_baton);
1127 return 0;
1128}
1129
1130void
1131MachProcess::SharedLibrariesUpdated ( )
1132{
1133 uint32_t event_bits = eEventSharedLibsStateChange;
1134 // Set the shared library event bit to let clients know of shared library
1135 // changes
1136 m_events.SetEvents(event_bits);
1137 // Wait for the event bit to reset if a reset ACK is requested
1138 m_events.WaitForResetAck(event_bits);
1139}
1140
1141void
1142MachProcess::AppendSTDOUT (char* s, size_t len)
1143{
Greg Clayton490fbbe2011-10-28 22:59:14 +00001144 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (<%zu> %s) ...", __FUNCTION__, len, s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 PTHREAD_MUTEX_LOCKER (locker, m_stdio_mutex);
1146 m_stdout_data.append(s, len);
1147 m_events.SetEvents(eEventStdioAvailable);
1148
1149 // Wait for the event bit to reset if a reset ACK is requested
1150 m_events.WaitForResetAck(eEventStdioAvailable);
1151}
1152
1153size_t
1154MachProcess::GetAvailableSTDOUT (char *buf, size_t buf_size)
1155{
Greg Clayton490fbbe2011-10-28 22:59:14 +00001156 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (&%p[%zu]) ...", __FUNCTION__, buf, buf_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 PTHREAD_MUTEX_LOCKER (locker, m_stdio_mutex);
1158 size_t bytes_available = m_stdout_data.size();
1159 if (bytes_available > 0)
1160 {
1161 if (bytes_available > buf_size)
1162 {
1163 memcpy(buf, m_stdout_data.data(), buf_size);
1164 m_stdout_data.erase(0, buf_size);
1165 bytes_available = buf_size;
1166 }
1167 else
1168 {
1169 memcpy(buf, m_stdout_data.data(), bytes_available);
1170 m_stdout_data.clear();
1171 }
1172 }
1173 return bytes_available;
1174}
1175
1176nub_addr_t
1177MachProcess::GetDYLDAllImageInfosAddress ()
1178{
Greg Clayton3382c2c2010-07-30 23:14:42 +00001179 DNBError err;
1180 return m_task.GetDYLDAllImageInfosAddress(err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181}
1182
1183size_t
1184MachProcess::GetAvailableSTDERR (char *buf, size_t buf_size)
1185{
1186 return 0;
1187}
1188
1189void *
1190MachProcess::STDIOThread(void *arg)
1191{
1192 MachProcess *proc = (MachProcess*) arg;
1193 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( arg = %p ) thread starting...", __FUNCTION__, arg);
1194
1195 // We start use a base and more options so we can control if we
1196 // are currently using a timeout on the mach_msg. We do this to get a
1197 // bunch of related exceptions on our exception port so we can process
1198 // then together. When we have multiple threads, we can get an exception
1199 // per thread and they will come in consecutively. The main thread loop
1200 // will start by calling mach_msg to without having the MACH_RCV_TIMEOUT
1201 // flag set in the options, so we will wait forever for an exception on
1202 // our exception port. After we get one exception, we then will use the
1203 // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current
1204 // exceptions for our process. After we have received the last pending
1205 // exception, we will get a timeout which enables us to then notify
1206 // our main thread that we have an exception bundle avaiable. We then wait
1207 // for the main thread to tell this exception thread to start trying to get
1208 // exceptions messages again and we start again with a mach_msg read with
1209 // infinite timeout.
1210 DNBError err;
1211 int stdout_fd = proc->GetStdoutFileDescriptor();
1212 int stderr_fd = proc->GetStderrFileDescriptor();
1213 if (stdout_fd == stderr_fd)
1214 stderr_fd = -1;
1215
1216 while (stdout_fd >= 0 || stderr_fd >= 0)
1217 {
1218 ::pthread_testcancel ();
1219
1220 fd_set read_fds;
1221 FD_ZERO (&read_fds);
1222 if (stdout_fd >= 0)
1223 FD_SET (stdout_fd, &read_fds);
1224 if (stderr_fd >= 0)
1225 FD_SET (stderr_fd, &read_fds);
1226 int nfds = std::max<int>(stdout_fd, stderr_fd) + 1;
1227
1228 int num_set_fds = select (nfds, &read_fds, NULL, NULL, NULL);
1229 DNBLogThreadedIf(LOG_PROCESS, "select (nfds, &read_fds, NULL, NULL, NULL) => %d", num_set_fds);
1230
1231 if (num_set_fds < 0)
1232 {
1233 int select_errno = errno;
1234 if (DNBLogCheckLogBit(LOG_PROCESS))
1235 {
1236 err.SetError (select_errno, DNBError::POSIX);
1237 err.LogThreadedIfError("select (nfds, &read_fds, NULL, NULL, NULL) => %d", num_set_fds);
1238 }
1239
1240 switch (select_errno)
1241 {
1242 case EAGAIN: // The kernel was (perhaps temporarily) unable to allocate the requested number of file descriptors, or we have non-blocking IO
1243 break;
1244 case EBADF: // One of the descriptor sets specified an invalid descriptor.
1245 return NULL;
1246 break;
1247 case EINTR: // A signal was delivered before the time limit expired and before any of the selected events occurred.
1248 case EINVAL: // The specified time limit is invalid. One of its components is negative or too large.
1249 default: // Other unknown error
1250 break;
1251 }
1252 }
1253 else if (num_set_fds == 0)
1254 {
1255 }
1256 else
1257 {
1258 char s[1024];
1259 s[sizeof(s)-1] = '\0'; // Ensure we have NULL termination
1260 int bytes_read = 0;
1261 if (stdout_fd >= 0 && FD_ISSET (stdout_fd, &read_fds))
1262 {
1263 do
1264 {
1265 bytes_read = ::read (stdout_fd, s, sizeof(s)-1);
1266 if (bytes_read < 0)
1267 {
1268 int read_errno = errno;
1269 DNBLogThreadedIf(LOG_PROCESS, "read (stdout_fd, ) => %d errno: %d (%s)", bytes_read, read_errno, strerror(read_errno));
1270 }
1271 else if (bytes_read == 0)
1272 {
1273 // EOF...
1274 DNBLogThreadedIf(LOG_PROCESS, "read (stdout_fd, ) => %d (reached EOF for child STDOUT)", bytes_read);
1275 stdout_fd = -1;
1276 }
1277 else if (bytes_read > 0)
1278 {
1279 proc->AppendSTDOUT(s, bytes_read);
1280 }
1281
1282 } while (bytes_read > 0);
1283 }
1284
1285 if (stderr_fd >= 0 && FD_ISSET (stderr_fd, &read_fds))
1286 {
1287 do
1288 {
1289 bytes_read = ::read (stderr_fd, s, sizeof(s)-1);
1290 if (bytes_read < 0)
1291 {
1292 int read_errno = errno;
1293 DNBLogThreadedIf(LOG_PROCESS, "read (stderr_fd, ) => %d errno: %d (%s)", bytes_read, read_errno, strerror(read_errno));
1294 }
1295 else if (bytes_read == 0)
1296 {
1297 // EOF...
1298 DNBLogThreadedIf(LOG_PROCESS, "read (stderr_fd, ) => %d (reached EOF for child STDERR)", bytes_read);
1299 stderr_fd = -1;
1300 }
1301 else if (bytes_read > 0)
1302 {
1303 proc->AppendSTDOUT(s, bytes_read);
1304 }
1305
1306 } while (bytes_read > 0);
1307 }
1308 }
1309 }
1310 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (%p): thread exiting...", __FUNCTION__, arg);
1311 return NULL;
1312}
1313
1314pid_t
1315MachProcess::AttachForDebug (pid_t pid, char *err_str, size_t err_len)
1316{
1317 // Clear out and clean up from any current state
1318 Clear();
1319 if (pid != 0)
1320 {
Greg Clayton3382c2c2010-07-30 23:14:42 +00001321 DNBError err;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 // Make sure the process exists...
1323 if (::getpgid (pid) < 0)
1324 {
Greg Clayton3382c2c2010-07-30 23:14:42 +00001325 err.SetErrorToErrno();
1326 const char *err_cstr = err.AsString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 ::snprintf (err_str, err_len, "%s", err_cstr ? err_cstr : "No such process");
1328 return INVALID_NUB_PROCESS;
1329 }
1330
1331 SetState(eStateAttaching);
1332 m_pid = pid;
1333 // Let ourselves know we are going to be using SBS if the correct flag bit is set...
Jason Molenda42999a42012-02-22 02:18:59 +00001334#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335 if (IsSBProcess(pid))
1336 m_flags |= eMachProcessFlagsUsingSBS;
1337#endif
Greg Clayton3382c2c2010-07-30 23:14:42 +00001338 if (!m_task.StartExceptionThread(err))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 {
Greg Clayton3382c2c2010-07-30 23:14:42 +00001340 const char *err_cstr = err.AsString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 ::snprintf (err_str, err_len, "%s", err_cstr ? err_cstr : "unable to start the exception thread");
1342 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", pid);
1343 m_pid = INVALID_NUB_PROCESS;
1344 return INVALID_NUB_PROCESS;
1345 }
1346
1347 errno = 0;
Greg Clayton3382c2c2010-07-30 23:14:42 +00001348 if (::ptrace (PT_ATTACHEXC, pid, 0, 0))
1349 err.SetError(errno);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350 else
Greg Clayton3382c2c2010-07-30 23:14:42 +00001351 err.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352
Greg Clayton3382c2c2010-07-30 23:14:42 +00001353 if (err.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 {
1355 m_flags |= eMachProcessFlagsAttached;
1356 // Sleep a bit to let the exception get received and set our process status
1357 // to stopped.
1358 ::usleep(250000);
1359 DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", pid);
1360 return m_pid;
1361 }
1362 else
1363 {
Greg Clayton3382c2c2010-07-30 23:14:42 +00001364 ::snprintf (err_str, err_len, "%s", err.AsString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", pid);
1366 }
1367 }
1368 return INVALID_NUB_PROCESS;
1369}
1370
1371// Do the process specific setup for attach. If this returns NULL, then there's no
1372// platform specific stuff to be done to wait for the attach. If you get non-null,
1373// pass that token to the CheckForProcess method, and then to CleanupAfterAttach.
1374
1375// Call PrepareForAttach before attaching to a process that has not yet launched
1376// This returns a token that can be passed to CheckForProcess, and to CleanupAfterAttach.
1377// You should call CleanupAfterAttach to free the token, and do whatever other
1378// cleanup seems good.
1379
1380const void *
1381MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str)
1382{
Jason Molenda42999a42012-02-22 02:18:59 +00001383#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 // Tell SpringBoard to halt the next launch of this application on startup.
1385
1386 if (!waitfor)
1387 return NULL;
1388
1389 const char *app_ext = strstr(path, ".app");
1390 if (app_ext == NULL)
1391 {
Greg Claytondce502e2011-11-04 03:34:56 +00001392 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, we can't tell springboard to wait for launch...", path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 return NULL;
1394 }
1395
1396 if (launch_flavor != eLaunchFlavorSpringBoard
1397 && launch_flavor != eLaunchFlavorDefault)
1398 return NULL;
1399
1400 std::string app_bundle_path(path, app_ext + strlen(".app"));
1401
1402 CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path.c_str (), err_str);
1403 std::string bundleIDStr;
1404 CFString::UTF8(bundleIDCFStr, bundleIDStr);
1405 DNBLogThreadedIf(LOG_PROCESS, "CopyBundleIDForPath (%s, err_str) returned @\"%s\"", app_bundle_path.c_str (), bundleIDStr.c_str());
1406
1407 if (bundleIDCFStr == NULL)
1408 {
1409 return NULL;
1410 }
1411
1412 SBSApplicationLaunchError sbs_error = 0;
1413
1414 const char *stdout_err = "/dev/null";
1415 CFString stdio_path;
1416 stdio_path.SetFileSystemRepresentation (stdout_err);
1417
1418 DNBLogThreadedIf(LOG_PROCESS, "SBSLaunchApplicationForDebugging ( @\"%s\" , NULL, NULL, NULL, @\"%s\", @\"%s\", SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger )", bundleIDStr.c_str(), stdout_err, stdout_err);
1419 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
1420 (CFURLRef)NULL, // openURL
1421 NULL, // launch_argv.get(),
1422 NULL, // launch_envp.get(), // CFDictionaryRef environment
1423 stdio_path.get(),
1424 stdio_path.get(),
1425 SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger);
1426
1427 if (sbs_error != SBSApplicationLaunchErrorSuccess)
1428 {
1429 err_str.SetError(sbs_error, DNBError::SpringBoard);
1430 return NULL;
1431 }
1432
1433 DNBLogThreadedIf(LOG_PROCESS, "Successfully set DebugOnNextLaunch.");
1434 return bundleIDCFStr;
1435# else
1436 return NULL;
1437#endif
1438}
1439
1440// Pass in the token you got from PrepareForAttach. If there is a process
1441// for that token, then the pid will be returned, otherwise INVALID_NUB_PROCESS
1442// will be returned.
1443
1444nub_process_t
1445MachProcess::CheckForProcess (const void *attach_token)
1446{
1447 if (attach_token == NULL)
1448 return INVALID_NUB_PROCESS;
1449
Jason Molenda42999a42012-02-22 02:18:59 +00001450#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 CFStringRef bundleIDCFStr = (CFStringRef) attach_token;
1452 Boolean got_it;
1453 nub_process_t attach_pid;
1454 got_it = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &attach_pid);
1455 if (got_it)
1456 return attach_pid;
1457 else
1458 return INVALID_NUB_PROCESS;
1459#endif
1460 return INVALID_NUB_PROCESS;
1461}
1462
1463// Call this to clean up after you have either attached or given up on the attach.
1464// Pass true for success if you have attached, false if you have not.
1465// The token will also be freed at this point, so you can't use it after calling
1466// this method.
1467
1468void
1469MachProcess::CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str)
1470{
Jason Molenda42999a42012-02-22 02:18:59 +00001471#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 if (attach_token == NULL)
1473 return;
1474
1475 // Tell SpringBoard to cancel the debug on next launch of this application
1476 // if we failed to attach
1477 if (!success)
1478 {
1479 SBSApplicationLaunchError sbs_error = 0;
1480 CFStringRef bundleIDCFStr = (CFStringRef) attach_token;
1481
1482 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
1483 (CFURLRef)NULL,
1484 NULL,
1485 NULL,
1486 NULL,
1487 NULL,
1488 SBSApplicationCancelDebugOnNextLaunch);
1489
1490 if (sbs_error != SBSApplicationLaunchErrorSuccess)
1491 {
1492 err_str.SetError(sbs_error, DNBError::SpringBoard);
1493 return;
1494 }
1495 }
1496
1497 CFRelease((CFStringRef) attach_token);
1498#endif
1499}
1500
1501pid_t
1502MachProcess::LaunchForDebug
1503(
1504 const char *path,
1505 char const *argv[],
1506 char const *envp[],
Greg Clayton6779606a2011-01-22 23:43:18 +00001507 const char *working_directory, // NULL => dont' change, non-NULL => set working directory for inferior to this
1508 const char *stdin_path,
1509 const char *stdout_path,
1510 const char *stderr_path,
Caroline Ticef8da8632010-12-03 18:46:09 +00001511 bool no_stdio,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 nub_launch_flavor_t launch_flavor,
Greg Claytonf681b942010-08-31 18:35:14 +00001513 int disable_aslr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 DNBError &launch_err
1515)
1516{
1517 // Clear out and clean up from any current state
1518 Clear();
1519
Greg Claytonf681b942010-08-31 18:35:14 +00001520 DNBLogThreadedIf(LOG_PROCESS, "%s( path = '%s', argv = %p, envp = %p, launch_flavor = %u, disable_aslr = %d )", __FUNCTION__, path, argv, envp, launch_flavor, disable_aslr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521
1522 // Fork a child process for debugging
1523 SetState(eStateLaunching);
1524
1525 switch (launch_flavor)
1526 {
1527 case eLaunchFlavorForkExec:
1528 m_pid = MachProcess::ForkChildForPTraceDebugging (path, argv, envp, this, launch_err);
1529 break;
1530
Johnny Chenbe4e2082012-06-11 21:05:26 +00001531#ifdef WITH_SPRINGBOARD
1532
1533 case eLaunchFlavorSpringBoard:
1534 {
1535 const char *app_ext = strstr(path, ".app");
1536 if (app_ext != NULL)
1537 {
1538 std::string app_bundle_path(path, app_ext + strlen(".app"));
1539 if (SBLaunchForDebug (app_bundle_path.c_str(), argv, envp, no_stdio, launch_err) != 0)
1540 return m_pid; // A successful SBLaunchForDebug() returns and assigns a non-zero m_pid.
1541 }
1542 }
1543 // In case the executable name has a ".app" fragment which confuses our debugserver,
1544 // let's do an intentional fallthrough here...
1545 launch_flavor = eLaunchFlavorPosixSpawn;
1546
1547#endif
1548
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549 case eLaunchFlavorPosixSpawn:
Greg Clayton3af9ea52010-11-18 05:57:03 +00001550 m_pid = MachProcess::PosixSpawnChildForPTraceDebugging (path,
Greg Clayton3c144382010-12-01 22:45:40 +00001551 DNBArchProtocol::GetArchitecture (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001552 argv,
1553 envp,
Greg Clayton6779606a2011-01-22 23:43:18 +00001554 working_directory,
1555 stdin_path,
1556 stdout_path,
1557 stderr_path,
Caroline Ticef8da8632010-12-03 18:46:09 +00001558 no_stdio,
Greg Clayton3af9ea52010-11-18 05:57:03 +00001559 this,
1560 disable_aslr,
1561 launch_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562 break;
1563
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564 default:
1565 // Invalid launch
1566 launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
1567 return INVALID_NUB_PROCESS;
1568 }
1569
1570 if (m_pid == INVALID_NUB_PROCESS)
1571 {
1572 // If we don't have a valid process ID and no one has set the error,
1573 // then return a generic error
1574 if (launch_err.Success())
1575 launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
1576 }
1577 else
1578 {
1579 m_path = path;
1580 size_t i;
1581 char const *arg;
1582 for (i=0; (arg = argv[i]) != NULL; i++)
1583 m_args.push_back(arg);
1584
Greg Clayton3382c2c2010-07-30 23:14:42 +00001585 m_task.StartExceptionThread(launch_err);
1586 if (launch_err.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587 {
Greg Clayton3382c2c2010-07-30 23:14:42 +00001588 if (launch_err.AsString() == NULL)
1589 launch_err.SetErrorString("unable to start the exception thread");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001590 ::ptrace (PT_KILL, m_pid, 0, 0);
1591 m_pid = INVALID_NUB_PROCESS;
1592 return INVALID_NUB_PROCESS;
1593 }
1594
1595 StartSTDIOThread();
1596
1597 if (launch_flavor == eLaunchFlavorPosixSpawn)
1598 {
1599
1600 SetState (eStateAttaching);
1601 errno = 0;
Caroline Tice5d7be2e2010-11-02 16:16:53 +00001602 int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001603 if (err == 0)
1604 {
1605 m_flags |= eMachProcessFlagsAttached;
1606 DNBLogThreadedIf(LOG_PROCESS, "successfully spawned pid %d", m_pid);
1607 launch_err.Clear();
1608 }
1609 else
1610 {
1611 SetState (eStateExited);
1612 DNBError ptrace_err(errno, DNBError::POSIX);
1613 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to spawned pid %d (err = %i, errno = %i (%s))", m_pid, err, ptrace_err.Error(), ptrace_err.AsString());
1614 launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
1615 }
1616 }
1617 else
1618 {
1619 launch_err.Clear();
1620 }
1621 }
1622 return m_pid;
1623}
1624
1625pid_t
1626MachProcess::PosixSpawnChildForPTraceDebugging
1627(
1628 const char *path,
Greg Clayton3af9ea52010-11-18 05:57:03 +00001629 cpu_type_t cpu_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630 char const *argv[],
1631 char const *envp[],
Greg Clayton6779606a2011-01-22 23:43:18 +00001632 const char *working_directory,
1633 const char *stdin_path,
1634 const char *stdout_path,
1635 const char *stderr_path,
Caroline Ticef8da8632010-12-03 18:46:09 +00001636 bool no_stdio,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637 MachProcess* process,
Greg Claytonf681b942010-08-31 18:35:14 +00001638 int disable_aslr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 DNBError& err
1640)
1641{
1642 posix_spawnattr_t attr;
Greg Claytonf681b942010-08-31 18:35:14 +00001643 short flags;
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001644 DNBLogThreadedIf(LOG_PROCESS, "%s ( path='%s', argv=%p, envp=%p, working_dir=%s, stdin=%s, stdout=%s stderr=%s, no-stdio=%i)",
1645 __FUNCTION__,
1646 path,
1647 argv,
1648 envp,
1649 working_directory,
1650 stdin_path,
1651 stdout_path,
1652 stderr_path,
1653 no_stdio);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654
1655 err.SetError( ::posix_spawnattr_init (&attr), DNBError::POSIX);
1656 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
1657 err.LogThreaded("::posix_spawnattr_init ( &attr )");
1658 if (err.Fail())
1659 return INVALID_NUB_PROCESS;
1660
Greg Clayton708c1ab2011-10-28 01:24:12 +00001661 flags = POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
Greg Claytonf681b942010-08-31 18:35:14 +00001662 if (disable_aslr)
1663 flags |= _POSIX_SPAWN_DISABLE_ASLR;
Greg Clayton708c1ab2011-10-28 01:24:12 +00001664
1665 sigset_t no_signals;
1666 sigset_t all_signals;
1667 sigemptyset (&no_signals);
1668 sigfillset (&all_signals);
1669 ::posix_spawnattr_setsigmask(&attr, &no_signals);
1670 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
1671
Greg Claytonf681b942010-08-31 18:35:14 +00001672 err.SetError( ::posix_spawnattr_setflags (&attr, flags), DNBError::POSIX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
Greg Claytonf681b942010-08-31 18:35:14 +00001674 err.LogThreaded("::posix_spawnattr_setflags ( &attr, POSIX_SPAWN_START_SUSPENDED%s )", flags & _POSIX_SPAWN_DISABLE_ASLR ? " | _POSIX_SPAWN_DISABLE_ASLR" : "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675 if (err.Fail())
1676 return INVALID_NUB_PROCESS;
1677
1678 // Don't do this on SnowLeopard, _sometimes_ the TASK_BASIC_INFO will fail
1679 // and we will fail to continue with our process...
1680
1681 // On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
1682
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683#if !defined(__arm__)
1684
1685 // We don't need to do this for ARM, and we really shouldn't now that we
1686 // have multiple CPU subtypes and no posix_spawnattr call that allows us
1687 // to set which CPU subtype to launch...
Greg Clayton71337622011-02-24 22:24:29 +00001688 if (cpu_type != 0)
1689 {
1690 size_t ocount = 0;
1691 err.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu_type, &ocount), DNBError::POSIX);
1692 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
1693 err.LogThreaded("::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %zu )", cpu_type, ocount);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001694
Greg Clayton71337622011-02-24 22:24:29 +00001695 if (err.Fail() != 0 || ocount != 1)
1696 return INVALID_NUB_PROCESS;
1697 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698#endif
1699
1700 PseudoTerminal pty;
1701
1702 posix_spawn_file_actions_t file_actions;
1703 err.SetError( ::posix_spawn_file_actions_init (&file_actions), DNBError::POSIX);
1704 int file_actions_valid = err.Success();
1705 if (!file_actions_valid || DNBLogCheckLogBit(LOG_PROCESS))
1706 err.LogThreaded("::posix_spawn_file_actions_init ( &file_actions )");
1707 int pty_error = -1;
1708 pid_t pid = INVALID_NUB_PROCESS;
1709 if (file_actions_valid)
1710 {
Greg Clayton6779606a2011-01-22 23:43:18 +00001711 if (stdin_path == NULL && stdout_path == NULL && stderr_path == NULL && !no_stdio)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 {
1713 pty_error = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY);
1714 if (pty_error == PseudoTerminal::success)
Greg Clayton6779606a2011-01-22 23:43:18 +00001715 {
1716 stdin_path = stdout_path = stderr_path = pty.SlaveName();
1717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 }
1719
Greg Clayton701a6b42012-03-12 19:02:41 +00001720 // if no_stdio or std paths not supplied, then route to "/dev/null".
1721 if (no_stdio || stdin_path == NULL || stdin_path[0] == '\0')
1722 stdin_path = "/dev/null";
1723 if (no_stdio || stdout_path == NULL || stdout_path[0] == '\0')
1724 stdout_path = "/dev/null";
1725 if (no_stdio || stderr_path == NULL || stderr_path[0] == '\0')
1726 stderr_path = "/dev/null";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727
Greg Clayton701a6b42012-03-12 19:02:41 +00001728 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
1729 STDIN_FILENO,
1730 stdin_path,
1731 O_RDONLY | O_NOCTTY,
1732 0),
1733 DNBError::POSIX);
1734 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
1735 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDIN_FILENO, path='%s')", stdin_path);
1736
1737 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
1738 STDOUT_FILENO,
1739 stdout_path,
1740 O_WRONLY | O_NOCTTY | O_CREAT,
1741 0640),
1742 DNBError::POSIX);
1743 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
1744 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDOUT_FILENO, path='%s')", stdout_path);
1745
1746 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
1747 STDERR_FILENO,
1748 stderr_path,
1749 O_WRONLY | O_NOCTTY | O_CREAT,
1750 0640),
1751 DNBError::POSIX);
1752 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
1753 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDERR_FILENO, path='%s')", stderr_path);
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001754
1755 // TODO: Verify if we can set the working directory back immediately
1756 // after the posix_spawnp call without creating a race condition???
1757 if (working_directory)
1758 ::chdir (working_directory);
1759
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001760 err.SetError( ::posix_spawnp (&pid, path, &file_actions, &attr, (char * const*)argv, (char * const*)envp), DNBError::POSIX);
1761 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
1762 err.LogThreaded("::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", pid, path, &file_actions, &attr, argv, envp);
1763 }
1764 else
1765 {
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001766 // TODO: Verify if we can set the working directory back immediately
1767 // after the posix_spawnp call without creating a race condition???
1768 if (working_directory)
1769 ::chdir (working_directory);
1770
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771 err.SetError( ::posix_spawnp (&pid, path, NULL, &attr, (char * const*)argv, (char * const*)envp), DNBError::POSIX);
1772 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
1773 err.LogThreaded("::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", pid, path, NULL, &attr, argv, envp);
1774 }
1775
1776 // We have seen some cases where posix_spawnp was returning a valid
1777 // looking pid even when an error was returned, so clear it out
1778 if (err.Fail())
1779 pid = INVALID_NUB_PROCESS;
1780
1781 if (pty_error == 0)
1782 {
1783 if (process != NULL)
1784 {
1785 int master_fd = pty.ReleaseMasterFD();
1786 process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
1787 }
1788 }
Greg Clayton0b42ac32010-07-02 01:29:13 +00001789 ::posix_spawnattr_destroy (&attr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001790
Greg Clayton71337622011-02-24 22:24:29 +00001791 if (pid != INVALID_NUB_PROCESS)
1792 {
1793 cpu_type_t pid_cpu_type = MachProcess::GetCPUTypeForLocalProcess (pid);
1794 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( ) pid=%i, cpu_type=0x%8.8x", __FUNCTION__, pid, pid_cpu_type);
1795 if (pid_cpu_type)
1796 DNBArchProtocol::SetArchitecture (pid_cpu_type);
1797 }
1798
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799 if (file_actions_valid)
1800 {
1801 DNBError err2;
1802 err2.SetError( ::posix_spawn_file_actions_destroy (&file_actions), DNBError::POSIX);
1803 if (err2.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
1804 err2.LogThreaded("::posix_spawn_file_actions_destroy ( &file_actions )");
1805 }
1806
1807 return pid;
1808}
1809
Greg Clayton71337622011-02-24 22:24:29 +00001810uint32_t
1811MachProcess::GetCPUTypeForLocalProcess (pid_t pid)
1812{
1813 int mib[CTL_MAXNAME]={0,};
1814 size_t len = CTL_MAXNAME;
1815 if (::sysctlnametomib("sysctl.proc_cputype", mib, &len))
1816 return 0;
1817
1818 mib[len] = pid;
1819 len++;
1820
1821 cpu_type_t cpu;
1822 size_t cpu_len = sizeof(cpu);
1823 if (::sysctl (mib, len, &cpu, &cpu_len, 0, 0))
1824 cpu = 0;
1825 return cpu;
1826}
1827
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828pid_t
1829MachProcess::ForkChildForPTraceDebugging
1830(
1831 const char *path,
1832 char const *argv[],
1833 char const *envp[],
1834 MachProcess* process,
1835 DNBError& launch_err
1836)
1837{
1838 PseudoTerminal::Error pty_error = PseudoTerminal::success;
1839
1840 // Use a fork that ties the child process's stdin/out/err to a pseudo
1841 // terminal so we can read it in our MachProcess::STDIOThread
1842 // as unbuffered io.
1843 PseudoTerminal pty;
1844 pid_t pid = pty.Fork(pty_error);
1845
1846 if (pid < 0)
1847 {
1848 //--------------------------------------------------------------
1849 // Error during fork.
1850 //--------------------------------------------------------------
1851 return pid;
1852 }
1853 else if (pid == 0)
1854 {
1855 //--------------------------------------------------------------
1856 // Child process
1857 //--------------------------------------------------------------
1858 ::ptrace (PT_TRACE_ME, 0, 0, 0); // Debug this process
1859 ::ptrace (PT_SIGEXC, 0, 0, 0); // Get BSD signals as mach exceptions
1860
1861 // If our parent is setgid, lets make sure we don't inherit those
1862 // extra powers due to nepotism.
Greg Clayton23f59502012-07-17 03:23:13 +00001863 if (::setgid (getgid ()) == 0)
1864 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865
Greg Clayton23f59502012-07-17 03:23:13 +00001866 // Let the child have its own process group. We need to execute
1867 // this call in both the child and parent to avoid a race condition
1868 // between the two processes.
1869 ::setpgid (0, 0); // Set the child process group to match its pid
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001870
Greg Clayton23f59502012-07-17 03:23:13 +00001871 // Sleep a bit to before the exec call
1872 ::sleep (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873
Greg Clayton23f59502012-07-17 03:23:13 +00001874 // Turn this process into
1875 ::execv (path, (char * const *)argv);
1876 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 // Exit with error code. Child process should have taken
1878 // over in above exec call and if the exec fails it will
1879 // exit the child process below.
1880 ::exit (127);
1881 }
1882 else
1883 {
1884 //--------------------------------------------------------------
1885 // Parent process
1886 //--------------------------------------------------------------
1887 // Let the child have its own process group. We need to execute
1888 // this call in both the child and parent to avoid a race condition
1889 // between the two processes.
1890 ::setpgid (pid, pid); // Set the child process group to match its pid
1891
1892 if (process != NULL)
1893 {
1894 // Release our master pty file descriptor so the pty class doesn't
1895 // close it and so we can continue to use it in our STDIO thread
1896 int master_fd = pty.ReleaseMasterFD();
1897 process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
1898 }
1899 }
1900 return pid;
1901}
1902
Jason Molenda42999a42012-02-22 02:18:59 +00001903#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904
1905pid_t
Caroline Ticef8da8632010-12-03 18:46:09 +00001906MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001907{
1908 // Clear out and clean up from any current state
1909 Clear();
1910
1911 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path);
1912
1913 // Fork a child process for debugging
1914 SetState(eStateLaunching);
Caroline Ticef8da8632010-12-03 18:46:09 +00001915 m_pid = MachProcess::SBForkChildForPTraceDebugging(path, argv, envp, no_stdio, this, launch_err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916 if (m_pid != 0)
1917 {
1918 m_flags |= eMachProcessFlagsUsingSBS;
1919 m_path = path;
1920 size_t i;
1921 char const *arg;
1922 for (i=0; (arg = argv[i]) != NULL; i++)
1923 m_args.push_back(arg);
Greg Clayton6f35f5c2010-09-09 06:32:46 +00001924 m_task.StartExceptionThread(launch_err);
1925
1926 if (launch_err.Fail())
1927 {
1928 if (launch_err.AsString() == NULL)
1929 launch_err.SetErrorString("unable to start the exception thread");
1930 ::ptrace (PT_KILL, m_pid, 0, 0);
1931 m_pid = INVALID_NUB_PROCESS;
1932 return INVALID_NUB_PROCESS;
1933 }
1934
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935 StartSTDIOThread();
1936 SetState (eStateAttaching);
Caroline Tice5d7be2e2010-11-02 16:16:53 +00001937 int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001938 if (err == 0)
1939 {
1940 m_flags |= eMachProcessFlagsAttached;
1941 DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
1942 }
1943 else
1944 {
1945 SetState (eStateExited);
1946 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
1947 }
1948 }
1949 return m_pid;
1950}
1951
1952#include <servers/bootstrap.h>
1953
1954// This returns a CFRetained pointer to the Bundle ID for app_bundle_path,
1955// or NULL if there was some problem getting the bundle id.
1956static CFStringRef
1957CopyBundleIDForPath (const char *app_bundle_path, DNBError &err_str)
1958{
1959 CFBundle bundle(app_bundle_path);
1960 CFStringRef bundleIDCFStr = bundle.GetIdentifier();
1961 std::string bundleID;
1962 if (CFString::UTF8(bundleIDCFStr, bundleID) == NULL)
1963 {
1964 struct stat app_bundle_stat;
1965 char err_msg[PATH_MAX];
1966
1967 if (::stat (app_bundle_path, &app_bundle_stat) < 0)
1968 {
1969 err_str.SetError(errno, DNBError::POSIX);
1970 snprintf(err_msg, sizeof(err_msg), "%s: \"%s\"", err_str.AsString(), app_bundle_path);
1971 err_str.SetErrorString(err_msg);
1972 DNBLogThreadedIf(LOG_PROCESS, "%s() error: %s", __FUNCTION__, err_msg);
1973 }
1974 else
1975 {
1976 err_str.SetError(-1, DNBError::Generic);
1977 snprintf(err_msg, sizeof(err_msg), "failed to extract CFBundleIdentifier from %s", app_bundle_path);
1978 err_str.SetErrorString(err_msg);
1979 DNBLogThreadedIf(LOG_PROCESS, "%s() error: failed to extract CFBundleIdentifier from '%s'", __FUNCTION__, app_bundle_path);
1980 }
1981 return NULL;
1982 }
1983
1984 DNBLogThreadedIf(LOG_PROCESS, "%s() extracted CFBundleIdentifier: %s", __FUNCTION__, bundleID.c_str());
1985 CFRetain (bundleIDCFStr);
1986
1987 return bundleIDCFStr;
1988}
1989
1990pid_t
Caroline Ticef8da8632010-12-03 18:46:09 +00001991MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992{
1993 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__, app_bundle_path, process);
1994 CFAllocatorRef alloc = kCFAllocatorDefault;
1995
1996 if (argv[0] == NULL)
1997 return INVALID_NUB_PROCESS;
1998
1999 size_t argc = 0;
2000 // Count the number of arguments
2001 while (argv[argc] != NULL)
2002 argc++;
2003
2004 // Enumerate the arguments
2005 size_t first_launch_arg_idx = 1;
2006 CFReleaser<CFMutableArrayRef> launch_argv;
2007
2008 if (argv[first_launch_arg_idx])
2009 {
2010 size_t launch_argc = argc > 0 ? argc - 1 : 0;
2011 launch_argv.reset (::CFArrayCreateMutable (alloc, launch_argc, &kCFTypeArrayCallBacks));
2012 size_t i;
2013 char const *arg;
2014 CFString launch_arg;
2015 for (i=first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL); i++)
2016 {
2017 launch_arg.reset(::CFStringCreateWithCString (alloc, arg, kCFStringEncodingUTF8));
2018 if (launch_arg.get() != NULL)
2019 CFArrayAppendValue(launch_argv.get(), launch_arg.get());
2020 else
2021 break;
2022 }
2023 }
2024
2025 // Next fill in the arguments dictionary. Note, the envp array is of the form
2026 // Variable=value but SpringBoard wants a CF dictionary. So we have to convert
2027 // this here.
2028
2029 CFReleaser<CFMutableDictionaryRef> launch_envp;
2030
2031 if (envp[0])
2032 {
2033 launch_envp.reset(::CFDictionaryCreateMutable(alloc, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
2034 const char *value;
2035 int name_len;
2036 CFString name_string, value_string;
2037
2038 for (int i = 0; envp[i] != NULL; i++)
2039 {
2040 value = strstr (envp[i], "=");
2041
2042 // If the name field is empty or there's no =, skip it. Somebody's messing with us.
2043 if (value == NULL || value == envp[i])
2044 continue;
2045
2046 name_len = value - envp[i];
2047
2048 // Now move value over the "="
2049 value++;
2050
2051 name_string.reset(::CFStringCreateWithBytes(alloc, (const UInt8 *) envp[i], name_len, kCFStringEncodingUTF8, false));
2052 value_string.reset(::CFStringCreateWithCString(alloc, value, kCFStringEncodingUTF8));
2053 CFDictionarySetValue (launch_envp.get(), name_string.get(), value_string.get());
2054 }
2055 }
2056
2057 CFString stdio_path;
2058
2059 PseudoTerminal pty;
Caroline Ticef8da8632010-12-03 18:46:09 +00002060 if (!no_stdio)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061 {
Caroline Ticef8da8632010-12-03 18:46:09 +00002062 PseudoTerminal::Error pty_err = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY);
2063 if (pty_err == PseudoTerminal::success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064 {
Caroline Ticef8da8632010-12-03 18:46:09 +00002065 const char* slave_name = pty.SlaveName();
2066 DNBLogThreadedIf(LOG_PROCESS, "%s() successfully opened master pty, slave is %s", __FUNCTION__, slave_name);
2067 if (slave_name && slave_name[0])
2068 {
2069 ::chmod (slave_name, S_IRWXU | S_IRWXG | S_IRWXO);
2070 stdio_path.SetFileSystemRepresentation (slave_name);
2071 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002072 }
2073 }
Caroline Ticef8da8632010-12-03 18:46:09 +00002074
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075 if (stdio_path.get() == NULL)
2076 {
2077 stdio_path.SetFileSystemRepresentation ("/dev/null");
2078 }
2079
2080 CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path, launch_err);
2081 if (bundleIDCFStr == NULL)
2082 return INVALID_NUB_PROCESS;
2083
2084 std::string bundleID;
2085 CFString::UTF8(bundleIDCFStr, bundleID);
2086
2087 CFData argv_data(NULL);
2088
2089 if (launch_argv.get())
2090 {
2091 if (argv_data.Serialize(launch_argv.get(), kCFPropertyListBinaryFormat_v1_0) == NULL)
2092 {
2093 DNBLogThreadedIf(LOG_PROCESS, "%s() error: failed to serialize launch arg array...", __FUNCTION__);
2094 return INVALID_NUB_PROCESS;
2095 }
2096 }
2097
2098 DNBLogThreadedIf(LOG_PROCESS, "%s() serialized launch arg array", __FUNCTION__);
2099
2100 // Find SpringBoard
2101 SBSApplicationLaunchError sbs_error = 0;
2102 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
2103 (CFURLRef)NULL, // openURL
2104 launch_argv.get(),
2105 launch_envp.get(), // CFDictionaryRef environment
2106 stdio_path.get(),
2107 stdio_path.get(),
2108 SBSApplicationLaunchWaitForDebugger | SBSApplicationLaunchUnlockDevice);
2109
2110
2111 launch_err.SetError(sbs_error, DNBError::SpringBoard);
2112
2113 if (sbs_error == SBSApplicationLaunchErrorSuccess)
2114 {
2115 static const useconds_t pid_poll_interval = 200000;
2116 static const useconds_t pid_poll_timeout = 30000000;
2117
2118 useconds_t pid_poll_total = 0;
2119
2120 nub_process_t pid = INVALID_NUB_PROCESS;
2121 Boolean pid_found = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &pid);
2122 // Poll until the process is running, as long as we are getting valid responses and the timeout hasn't expired
2123 // A return PID of 0 means the process is not running, which may be because it hasn't been (asynchronously) started
2124 // yet, or that it died very quickly (if you weren't using waitForDebugger).
2125 while (!pid_found && pid_poll_total < pid_poll_timeout)
2126 {
2127 usleep (pid_poll_interval);
2128 pid_poll_total += pid_poll_interval;
2129 DNBLogThreadedIf(LOG_PROCESS, "%s() polling Springboard for pid for %s...", __FUNCTION__, bundleID.c_str());
2130 pid_found = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &pid);
2131 }
2132
2133 CFRelease (bundleIDCFStr);
2134 if (pid_found)
2135 {
2136 if (process != NULL)
2137 {
2138 // Release our master pty file descriptor so the pty class doesn't
2139 // close it and so we can continue to use it in our STDIO thread
2140 int master_fd = pty.ReleaseMasterFD();
2141 process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
2142 }
2143 DNBLogThreadedIf(LOG_PROCESS, "%s() => pid = %4.4x", __FUNCTION__, pid);
2144 }
2145 else
2146 {
2147 DNBLogError("failed to lookup the process ID for CFBundleIdentifier %s.", bundleID.c_str());
2148 }
2149 return pid;
2150 }
2151
2152 DNBLogError("unable to launch the application with CFBundleIdentifier '%s' sbs_error = %u", bundleID.c_str(), sbs_error);
2153 return INVALID_NUB_PROCESS;
2154}
2155
Jason Molenda42999a42012-02-22 02:18:59 +00002156#endif // #ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157
2158