Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 708c1ab | 2011-10-28 01:24:12 +0000 | [diff] [blame] | 16 | #include <signal.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include <spawn.h> |
| 18 | #include <sys/fcntl.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/ptrace.h> |
| 21 | #include <sys/stat.h> |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 22 | #include <sys/sysctl.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #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 | |
| 41 | static CFStringRef CopyBundleIDForPath (const char *app_buncle_path, DNBError &err_str); |
| 42 | |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 43 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
| 45 | #include <CoreFoundation/CoreFoundation.h> |
| 46 | #include <SpringBoardServices/SpringBoardServer.h> |
| 47 | #include <SpringBoardServices/SBSWatchdogAssertion.h> |
| 48 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | static bool |
| 50 | IsSBProcess (nub_process_t pid) |
| 51 | { |
Jim Ingham | 1bfe4f6 | 2012-07-24 01:23:53 +0000 | [diff] [blame] | 52 | CFReleaser<CFArrayRef> appIdsForPID (::SBSCopyDisplayIdentifiersForProcessID(pid)); |
| 53 | return appIdsForPID.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | #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 Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 68 | #ifndef _POSIX_SPAWN_DISABLE_ASLR |
| 69 | #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 |
| 70 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | |
| 72 | MachProcess::MachProcess() : |
| 73 | m_pid (0), |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 74 | m_cpu_type (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | 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 Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 85 | m_thread_actions (), |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 86 | m_thread_list (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | m_exception_messages (), |
| 88 | m_exception_messages_mutex (PTHREAD_MUTEX_RECURSIVE), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | 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 | |
| 102 | MachProcess::~MachProcess() |
| 103 | { |
| 104 | DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__); |
| 105 | Clear(); |
| 106 | } |
| 107 | |
| 108 | pid_t |
| 109 | MachProcess::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 Chen | c0cd18d | 2010-09-28 16:34:56 +0000 | [diff] [blame] | 115 | m_pid = ::getpid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | else |
| 117 | m_pid = pid; |
| 118 | return m_pid; // Return actualy PID in case a zero pid was passed in |
| 119 | } |
| 120 | |
| 121 | nub_state_t |
| 122 | MachProcess::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 | |
| 129 | const char * |
| 130 | MachProcess::ThreadGetName(nub_thread_t tid) |
| 131 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 132 | return m_thread_list.GetName(tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | nub_state_t |
| 136 | MachProcess::ThreadGetState(nub_thread_t tid) |
| 137 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 138 | return m_thread_list.GetState(tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | nub_size_t |
| 143 | MachProcess::GetNumThreads () const |
| 144 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 145 | return m_thread_list.NumThreads(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | nub_thread_t |
| 149 | MachProcess::GetThreadAtIndex (nub_size_t thread_idx) const |
| 150 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 151 | return m_thread_list.ThreadIDAtIndex(thread_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Jim Ingham | 279ceec | 2012-07-25 21:12:43 +0000 | [diff] [blame] | 154 | nub_bool_t |
| 155 | MachProcess::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | nub_thread_t |
| 171 | MachProcess::GetCurrentThread () |
| 172 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 173 | return m_thread_list.CurrentThreadID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | nub_thread_t |
| 177 | MachProcess::SetCurrentThread(nub_thread_t tid) |
| 178 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 179 | return m_thread_list.SetCurrentThread(tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | bool |
| 183 | MachProcess::GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info) const |
| 184 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 185 | return m_thread_list.GetThreadStoppedReason(tid, stop_info); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
| 189 | MachProcess::DumpThreadStoppedReason(nub_thread_t tid) const |
| 190 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 191 | return m_thread_list.DumpThreadStoppedReason(tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | const char * |
| 195 | MachProcess::GetThreadInfo(nub_thread_t tid) const |
| 196 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 197 | return m_thread_list.GetThreadInfo(tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 200 | uint32_t |
| 201 | MachProcess::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | const DNBRegisterSetInfo * |
Greg Clayton | e2d4f0d | 2011-01-19 07:54:15 +0000 | [diff] [blame] | 209 | MachProcess::GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | { |
Greg Clayton | e2d4f0d | 2011-01-19 07:54:15 +0000 | [diff] [blame] | 211 | MachThreadSP thread_sp (m_thread_list.GetThreadByID (tid)); |
| 212 | if (thread_sp) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 213 | { |
Greg Clayton | e2d4f0d | 2011-01-19 07:54:15 +0000 | [diff] [blame] | 214 | DNBArchProtocol *arch = thread_sp->GetArchProtocol(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 215 | if (arch) |
| 216 | return arch->GetRegisterSetInfo (num_reg_sets); |
| 217 | } |
| 218 | *num_reg_sets = 0; |
| 219 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool |
| 223 | MachProcess::GetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *value ) const |
| 224 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 225 | return m_thread_list.GetRegisterValue(tid, set, reg, value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool |
| 229 | MachProcess::SetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value ) const |
| 230 | { |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 231 | return m_thread_list.SetRegisterValue(tid, set, reg, value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void |
| 235 | MachProcess::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 | |
| 270 | void |
| 271 | MachProcess::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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 284 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | { |
| 286 | PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex); |
| 287 | m_exception_messages.clear(); |
| 288 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
| 292 | bool |
| 293 | MachProcess::StartSTDIOThread() |
| 294 | { |
| 295 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( )", __FUNCTION__); |
| 296 | // Create the thread that watches for the child STDIO |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 297 | return ::pthread_create (&m_stdio_thread, NULL, MachProcess::STDIOThread, this) == 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
| 301 | nub_addr_t |
| 302 | MachProcess::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 | |
| 309 | bool |
| 310 | MachProcess::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 Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 317 | m_thread_actions = thread_actions; |
| 318 | PrivateResume(); |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 319 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 320 | } |
| 321 | else if (state == eStateRunning) |
| 322 | { |
| 323 | DNBLogThreadedIf(LOG_PROCESS, "Resume() - task 0x%x is running, ignoring...", m_task.TaskPort()); |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 324 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | } |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 326 | DNBLogThreadedIf(LOG_PROCESS, "Resume() - task 0x%x can't continue, ignoring...", m_task.TaskPort()); |
| 327 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | bool |
| 331 | MachProcess::Kill (const struct timespec *timeout_abstime) |
| 332 | { |
| 333 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill ()"); |
Jim Ingham | 5d2735e | 2012-04-25 17:45:26 +0000 | [diff] [blame] | 334 | nub_state_t state = DoSIGSTOP(true, false, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 335 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill() DoSIGSTOP() state = %s", DNBStateAsString(state)); |
| 336 | errno = 0; |
| 337 | ::ptrace (PT_KILL, m_pid, 0, 0); |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 338 | DNBError err; |
| 339 | err.SetErrorToErrno(); |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 340 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill() DoSIGSTOP() ::ptrace (PT_KILL, pid=%u, 0, 0) => 0x%8.8x (%s)", m_pid, err.Error(), err.AsString()); |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 341 | m_thread_actions = DNBThreadResumeActions (eStateRunning, 0); |
| 342 | PrivateResume (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool |
| 347 | MachProcess::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | // 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 Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 363 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 364 | } |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 365 | DNBError err(errno, DNBError::POSIX); |
| 366 | err.LogThreadedIfError("kill (pid = %d, signo = %i)", ProcessID(), signal); |
| 367 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | |
| 369 | } |
| 370 | |
| 371 | nub_state_t |
Jim Ingham | 5d2735e | 2012-04-25 17:45:26 +0000 | [diff] [blame] | 372 | MachProcess::DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | { |
| 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 386 | // If we already have a thread stopped due to a SIGSTOP, we don't have |
| 387 | // to do anything... |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 388 | 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 392 | return GetState(); |
| 393 | |
| 394 | // No threads were stopped with a SIGSTOP, we need to run and halt the |
| 395 | // process with a signal |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s -- resuming process", DNBStateAsString (state)); |
Jim Ingham | 5d2735e | 2012-04-25 17:45:26 +0000 | [diff] [blame] | 397 | if (allow_running) |
| 398 | m_thread_actions = DNBThreadResumeActions (eStateRunning, 0); |
| 399 | else |
| 400 | m_thread_actions = DNBThreadResumeActions (eStateSuspended, 0); |
| 401 | |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 402 | PrivateResume (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 403 | |
| 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 Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 420 | //clear_bps_and_wps = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | } |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 422 | uint32_t thread_idx = m_thread_list.GetThreadIndexForThreadStoppedWithSignal (SIGSTOP); |
| 423 | if (thread_idx_ptr) |
| 424 | *thread_idx_ptr = thread_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | return GetState(); |
| 426 | } |
| 427 | |
| 428 | bool |
| 429 | MachProcess::Detach() |
| 430 | { |
| 431 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach()"); |
| 432 | |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 433 | uint32_t thread_idx = UINT32_MAX; |
Jim Ingham | 5d2735e | 2012-04-25 17:45:26 +0000 | [diff] [blame] | 434 | nub_state_t state = DoSIGSTOP(true, true, &thread_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach() DoSIGSTOP() returned %s", DNBStateAsString(state)); |
| 436 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 437 | { |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 438 | m_thread_actions.Clear(); |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 439 | 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 Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 445 | m_thread_actions.Append (thread_action); |
| 446 | m_thread_actions.SetDefaultThreadActionIfNeeded (eStateRunning, 0); |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 447 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 448 | PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 450 | ReplyToAllExceptions (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 451 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | m_task.ShutDownExcecptionThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | |
| 456 | // Detach from our process |
| 457 | errno = 0; |
| 458 | nub_process_t pid = m_pid; |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 459 | int ret = ::ptrace (PT_DETACH, pid, (caddr_t)1, 0); |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 460 | DNBError err(errno, DNBError::POSIX); |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 461 | if (DNBLogCheckLogBit(LOG_PROCESS) || err.Fail() || (ret != 0)) |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 462 | err.LogThreaded("::ptrace (PT_DETACH, %u, (caddr_t)1, 0)", pid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | |
| 464 | // Resume our task |
| 465 | m_task.Resume(); |
| 466 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 467 | // 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 472 | |
| 473 | SetState(eStateDetached); |
| 474 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 478 | nub_size_t |
| 479 | MachProcess::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 | //---------------------------------------------------------------------- |
| 508 | nub_size_t |
| 509 | MachProcess::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 | //---------------------------------------------------------------------- |
| 533 | nub_size_t |
| 534 | MachProcess::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | void |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 604 | MachProcess::ReplyToAllExceptions () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 605 | { |
| 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 Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 614 | DNBLogThreadedIf(LOG_EXCEPTIONS, "Replying to exception %u...", (uint32_t)std::distance(begin, pos)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 615 | int thread_reply_signal = 0; |
| 616 | |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 617 | const DNBThreadResumeAction *action = m_thread_actions.GetActionForThread (pos->state.thread_port, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | |
| 619 | if (action) |
| 620 | { |
| 621 | thread_reply_signal = action->signal; |
| 622 | if (thread_reply_signal) |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 623 | m_thread_actions.SetSignalHandledForThread (pos->state.thread_port); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 626 | DNBError err (pos->Reply(this, thread_reply_signal)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | if (DNBLogCheckLogBit(LOG_EXCEPTIONS)) |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 628 | err.LogThreadedIfError("Error replying to exception"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | } |
| 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 | } |
| 636 | void |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 637 | MachProcess::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 638 | { |
| 639 | PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex); |
| 640 | |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 641 | ReplyToAllExceptions (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 642 | // 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 Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 647 | m_thread_list.ProcessWillResume (this, m_thread_actions); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | |
| 649 | // Set our state accordingly |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 650 | if (m_thread_actions.NumActionsWithState(eStateStepping)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 651 | SetState (eStateStepping); |
| 652 | else |
| 653 | SetState (eStateRunning); |
| 654 | |
| 655 | // Now resume our task. |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 656 | m_task.Resume(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | nub_break_t |
| 660 | MachProcess::CreateBreakpoint(nub_addr_t addr, nub_size_t length, bool hardware, thread_t tid) |
| 661 | { |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 662 | DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %zu, hardware = %i, tid = 0x%4.4x )", (uint64_t)addr, length, hardware, tid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | 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 Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 670 | DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%4.4x ) => %u", (uint64_t)addr, length, tid, breakID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 671 | 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 | |
| 681 | nub_watch_t |
| 682 | MachProcess::CreateWatchpoint(nub_addr_t addr, nub_size_t length, uint32_t watch_flags, bool hardware, thread_t tid) |
| 683 | { |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 684 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | 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 Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 694 | DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%x) => %u", (uint64_t)addr, length, tid, watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 695 | return watchID; |
| 696 | } |
| 697 | else |
| 698 | { |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 699 | DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %zu, tid = 0x%x) => FAILED (%u)", (uint64_t)addr, length, tid, watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 700 | m_watchpoints.Remove(watchID); |
| 701 | } |
| 702 | // We failed to enable the watchpoint |
| 703 | return INVALID_NUB_BREAK_ID; |
| 704 | } |
| 705 | |
| 706 | nub_size_t |
| 707 | MachProcess::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 | |
| 728 | nub_size_t |
| 729 | MachProcess::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 | |
| 750 | bool |
| 751 | MachProcess::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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 761 | bool hw_disable_result = m_thread_list.DisableHardwareBreakpoint (bp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | |
| 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 769 | m_thread_list.NotifyBreakpointChanged(bp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 770 | 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 Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 781 | const uint8_t * const break_op = DNBArchProtocol::GetBreakpointOpcode (bp->ByteSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 782 | 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 Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 818 | DNBLogThreadedIf(LOG_BREAKPOINTS | LOG_VERBOSE, "MachProcess::DisableBreakpoint ( breakID = %d, remove = %d ) addr = 0x%8.8llx is not enabled", breakID, remove, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | // 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 837 | m_thread_list.NotifyBreakpointChanged(bp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | 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 | |
| 870 | bool |
| 871 | MachProcess::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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 882 | bool hw_disable_result = m_thread_list.DisableHardwareWatchpoint (wp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 883 | |
| 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 | |
| 904 | void |
| 905 | MachProcess::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 | |
| 923 | void |
| 924 | MachProcess::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 Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 942 | uint32_t |
| 943 | MachProcess::GetNumSupportedHardwareWatchpoints () const |
| 944 | { |
| 945 | return m_thread_list.NumSupportedHardwareWatchpoints(); |
| 946 | } |
| 947 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | bool |
| 949 | MachProcess::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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 965 | bp->SetHardwareIndex(m_thread_list.EnableHardwareBreakpoint(bp)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 966 | 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 Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 975 | const uint8_t * const break_op = DNBArchProtocol::GetBreakpointOpcode (break_op_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 976 | 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 991 | m_thread_list.NotifyBreakpointChanged(bp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 992 | 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 | |
| 1024 | bool |
| 1025 | MachProcess::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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1040 | wp->SetHardwareIndex(m_thread_list.EnableHardwareWatchpoint(wp)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1041 | 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. |
| 1055 | void |
| 1056 | MachProcess::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 | |
| 1070 | void |
| 1071 | MachProcess::ExceptionMessageBundleComplete() |
| 1072 | { |
| 1073 | // We have a complete bundle of exceptions for our child process. |
| 1074 | PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex); |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 1075 | DNBLogThreadedIf(LOG_EXCEPTIONS, "%s: %zu exception messages.", __PRETTY_FUNCTION__, m_exception_messages.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1080 | m_thread_list.ProcessDidStop(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1081 | |
| 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 Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1090 | m_thread_list.NotifyException(m_exception_messages[i].state); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | if (DNBLogCheckLogBit(LOG_EXCEPTIONS)) |
| 1092 | m_exception_messages[i].Dump(); |
| 1093 | } |
| 1094 | |
| 1095 | if (DNBLogCheckLogBit(LOG_THREAD)) |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1096 | m_thread_list.Dump(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1097 | |
| 1098 | bool step_more = false; |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1099 | if (m_thread_list.ShouldStop(step_more)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1100 | { |
| 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 Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 1113 | PrivateResume (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | else |
| 1117 | { |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 1118 | DNBLogThreadedIf(LOG_EXCEPTIONS, "%s empty exception messages bundle (%zu exceptions).", __PRETTY_FUNCTION__, m_exception_messages.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | nub_size_t |
| 1123 | MachProcess::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 | |
| 1130 | void |
| 1131 | MachProcess::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 | |
| 1141 | void |
| 1142 | MachProcess::AppendSTDOUT (char* s, size_t len) |
| 1143 | { |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 1144 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (<%zu> %s) ...", __FUNCTION__, len, s); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1145 | 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 | |
| 1153 | size_t |
| 1154 | MachProcess::GetAvailableSTDOUT (char *buf, size_t buf_size) |
| 1155 | { |
Greg Clayton | 490fbbe | 2011-10-28 22:59:14 +0000 | [diff] [blame] | 1156 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (&%p[%zu]) ...", __FUNCTION__, buf, buf_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1157 | 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 | |
| 1176 | nub_addr_t |
| 1177 | MachProcess::GetDYLDAllImageInfosAddress () |
| 1178 | { |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1179 | DNBError err; |
| 1180 | return m_task.GetDYLDAllImageInfosAddress(err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | size_t |
| 1184 | MachProcess::GetAvailableSTDERR (char *buf, size_t buf_size) |
| 1185 | { |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | void * |
| 1190 | MachProcess::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 | |
| 1314 | pid_t |
| 1315 | MachProcess::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 Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1321 | DNBError err; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1322 | // Make sure the process exists... |
| 1323 | if (::getpgid (pid) < 0) |
| 1324 | { |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1325 | err.SetErrorToErrno(); |
| 1326 | const char *err_cstr = err.AsString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1327 | ::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 Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1334 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1335 | if (IsSBProcess(pid)) |
| 1336 | m_flags |= eMachProcessFlagsUsingSBS; |
| 1337 | #endif |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1338 | if (!m_task.StartExceptionThread(err)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1339 | { |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1340 | const char *err_cstr = err.AsString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1341 | ::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 Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1348 | if (::ptrace (PT_ATTACHEXC, pid, 0, 0)) |
| 1349 | err.SetError(errno); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1350 | else |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1351 | err.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1352 | |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1353 | if (err.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1354 | { |
| 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 Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1364 | ::snprintf (err_str, err_len, "%s", err.AsString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1365 | 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 | |
| 1380 | const void * |
| 1381 | MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str) |
| 1382 | { |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1383 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1384 | // 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 Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 1392 | DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, we can't tell springboard to wait for launch...", path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1393 | 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 | |
| 1444 | nub_process_t |
| 1445 | MachProcess::CheckForProcess (const void *attach_token) |
| 1446 | { |
| 1447 | if (attach_token == NULL) |
| 1448 | return INVALID_NUB_PROCESS; |
| 1449 | |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1450 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1451 | 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 | |
| 1468 | void |
| 1469 | MachProcess::CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str) |
| 1470 | { |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1471 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1472 | 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 | |
| 1501 | pid_t |
| 1502 | MachProcess::LaunchForDebug |
| 1503 | ( |
| 1504 | const char *path, |
| 1505 | char const *argv[], |
| 1506 | char const *envp[], |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1507 | 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 Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1511 | bool no_stdio, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1512 | nub_launch_flavor_t launch_flavor, |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1513 | int disable_aslr, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1514 | DNBError &launch_err |
| 1515 | ) |
| 1516 | { |
| 1517 | // Clear out and clean up from any current state |
| 1518 | Clear(); |
| 1519 | |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1520 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1521 | |
| 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 Chen | be4e208 | 2012-06-11 21:05:26 +0000 | [diff] [blame] | 1531 | #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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1549 | case eLaunchFlavorPosixSpawn: |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1550 | m_pid = MachProcess::PosixSpawnChildForPTraceDebugging (path, |
Greg Clayton | 3c14438 | 2010-12-01 22:45:40 +0000 | [diff] [blame] | 1551 | DNBArchProtocol::GetArchitecture (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1552 | argv, |
| 1553 | envp, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1554 | working_directory, |
| 1555 | stdin_path, |
| 1556 | stdout_path, |
| 1557 | stderr_path, |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1558 | no_stdio, |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1559 | this, |
| 1560 | disable_aslr, |
| 1561 | launch_err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1564 | 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 Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1585 | m_task.StartExceptionThread(launch_err); |
| 1586 | if (launch_err.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1587 | { |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 1588 | if (launch_err.AsString() == NULL) |
| 1589 | launch_err.SetErrorString("unable to start the exception thread"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1590 | ::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 Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 1602 | int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1603 | 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 | |
| 1625 | pid_t |
| 1626 | MachProcess::PosixSpawnChildForPTraceDebugging |
| 1627 | ( |
| 1628 | const char *path, |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1629 | cpu_type_t cpu_type, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1630 | char const *argv[], |
| 1631 | char const *envp[], |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1632 | const char *working_directory, |
| 1633 | const char *stdin_path, |
| 1634 | const char *stdout_path, |
| 1635 | const char *stderr_path, |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1636 | bool no_stdio, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1637 | MachProcess* process, |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1638 | int disable_aslr, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1639 | DNBError& err |
| 1640 | ) |
| 1641 | { |
| 1642 | posix_spawnattr_t attr; |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1643 | short flags; |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1644 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1654 | |
| 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 Clayton | 708c1ab | 2011-10-28 01:24:12 +0000 | [diff] [blame] | 1661 | flags = POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK; |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1662 | if (disable_aslr) |
| 1663 | flags |= _POSIX_SPAWN_DISABLE_ASLR; |
Greg Clayton | 708c1ab | 2011-10-28 01:24:12 +0000 | [diff] [blame] | 1664 | |
| 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 Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1672 | err.SetError( ::posix_spawnattr_setflags (&attr, flags), DNBError::POSIX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1673 | if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS)) |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1674 | err.LogThreaded("::posix_spawnattr_setflags ( &attr, POSIX_SPAWN_START_SUSPENDED%s )", flags & _POSIX_SPAWN_DISABLE_ASLR ? " | _POSIX_SPAWN_DISABLE_ASLR" : ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1675 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1683 | #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 Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1688 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1694 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1695 | if (err.Fail() != 0 || ocount != 1) |
| 1696 | return INVALID_NUB_PROCESS; |
| 1697 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1698 | #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 Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1711 | if (stdin_path == NULL && stdout_path == NULL && stderr_path == NULL && !no_stdio) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1712 | { |
| 1713 | pty_error = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY); |
| 1714 | if (pty_error == PseudoTerminal::success) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1715 | { |
| 1716 | stdin_path = stdout_path = stderr_path = pty.SlaveName(); |
| 1717 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Greg Clayton | 701a6b4 | 2012-03-12 19:02:41 +0000 | [diff] [blame] | 1720 | // 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1727 | |
Greg Clayton | 701a6b4 | 2012-03-12 19:02:41 +0000 | [diff] [blame] | 1728 | 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 Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1754 | |
| 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1760 | 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 Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1766 | // 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1771 | 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 Clayton | 0b42ac3 | 2010-07-02 01:29:13 +0000 | [diff] [blame] | 1789 | ::posix_spawnattr_destroy (&attr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1790 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1791 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1799 | 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 Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1810 | uint32_t |
| 1811 | MachProcess::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1828 | pid_t |
| 1829 | MachProcess::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 Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 1863 | if (::setgid (getgid ()) == 0) |
| 1864 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1865 | |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 1866 | // 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1870 | |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 1871 | // Sleep a bit to before the exec call |
| 1872 | ::sleep (1); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1873 | |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 1874 | // Turn this process into |
| 1875 | ::execv (path, (char * const *)argv); |
| 1876 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1877 | // 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 Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1903 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1904 | |
| 1905 | pid_t |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1906 | MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1907 | { |
| 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 Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1915 | m_pid = MachProcess::SBForkChildForPTraceDebugging(path, argv, envp, no_stdio, this, launch_err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1916 | 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 Clayton | 6f35f5c | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 1924 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1935 | StartSTDIOThread(); |
| 1936 | SetState (eStateAttaching); |
Caroline Tice | 5d7be2e | 2010-11-02 16:16:53 +0000 | [diff] [blame] | 1937 | int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1938 | 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. |
| 1956 | static CFStringRef |
| 1957 | CopyBundleIDForPath (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 | |
| 1990 | pid_t |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1991 | MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1992 | { |
| 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 Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 2060 | if (!no_stdio) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2061 | { |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 2062 | PseudoTerminal::Error pty_err = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY); |
| 2063 | if (pty_err == PseudoTerminal::success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2064 | { |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 2065 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2075 | 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 Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 2156 | #endif // #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2157 | |
| 2158 | |