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