Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- debugserver.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 | #include <sys/socket.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <errno.h> |
| 13 | #include <getopt.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <sys/select.h> |
| 16 | #include <sys/sysctl.h> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | #include <asl.h> |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 20 | #include <arpa/inet.h> |
| 21 | #include <netdb.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <netinet/tcp.h> |
| 24 | #include <sys/un.h> |
| 25 | #include <sys/types.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
| 27 | #include "CFString.h" |
| 28 | #include "DNB.h" |
| 29 | #include "DNBLog.h" |
| 30 | #include "DNBTimer.h" |
| 31 | #include "PseudoTerminal.h" |
| 32 | #include "RNBContext.h" |
| 33 | #include "RNBServices.h" |
| 34 | #include "RNBSocket.h" |
| 35 | #include "RNBRemote.h" |
| 36 | #include "SysSignal.h" |
| 37 | |
| 38 | // Global PID in case we get a signal and need to stop the process... |
| 39 | nub_process_t g_pid = INVALID_NUB_PROCESS; |
| 40 | |
| 41 | //---------------------------------------------------------------------- |
| 42 | // Run loop modes which determine which run loop function will be called |
| 43 | //---------------------------------------------------------------------- |
| 44 | typedef enum |
| 45 | { |
| 46 | eRNBRunLoopModeInvalid = 0, |
| 47 | eRNBRunLoopModeGetStartModeFromRemoteProtocol, |
| 48 | eRNBRunLoopModeInferiorAttaching, |
| 49 | eRNBRunLoopModeInferiorLaunching, |
| 50 | eRNBRunLoopModeInferiorExecuting, |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 51 | eRNBRunLoopModePlatformMode, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | eRNBRunLoopModeExit |
| 53 | } RNBRunLoopMode; |
| 54 | |
| 55 | |
| 56 | //---------------------------------------------------------------------- |
| 57 | // Global Variables |
| 58 | //---------------------------------------------------------------------- |
| 59 | RNBRemoteSP g_remoteSP; |
| 60 | static int g_lockdown_opt = 0; |
| 61 | static int g_applist_opt = 0; |
| 62 | static nub_launch_flavor_t g_launch_flavor = eLaunchFlavorDefault; |
Greg Clayton | 6f35f5c | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 63 | int g_disable_aslr = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | |
| 65 | int g_isatty = 0; |
| 66 | |
| 67 | #define RNBLogSTDOUT(fmt, ...) do { if (g_isatty) { fprintf(stdout, fmt, ## __VA_ARGS__); } else { _DNBLog(0, fmt, ## __VA_ARGS__); } } while (0) |
| 68 | #define RNBLogSTDERR(fmt, ...) do { if (g_isatty) { fprintf(stderr, fmt, ## __VA_ARGS__); } else { _DNBLog(0, fmt, ## __VA_ARGS__); } } while (0) |
| 69 | |
| 70 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | // Get our program path and arguments from the remote connection. |
| 72 | // We will need to start up the remote connection without a PID, get the |
| 73 | // arguments, wait for the new process to finish launching and hit its |
| 74 | // entry point, and then return the run loop mode that should come next. |
| 75 | //---------------------------------------------------------------------- |
| 76 | RNBRunLoopMode |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 77 | RNBRunLoopGetStartModeFromRemote (RNBRemote* remote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | { |
| 79 | std::string packet; |
| 80 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 81 | if (remote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | RNBContext& ctx = remote->Context(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 84 | uint32_t event_mask = RNBContext::event_read_packet_available | |
| 85 | RNBContext::event_read_thread_exiting; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | |
| 87 | // Spin waiting to get the A packet. |
| 88 | while (1) |
| 89 | { |
| 90 | DNBLogThreadedIf (LOG_RNB_MAX, "%s ctx.Events().WaitForSetEvents( 0x%08x ) ...",__FUNCTION__, event_mask); |
| 91 | nub_event_t set_events = ctx.Events().WaitForSetEvents(event_mask); |
| 92 | DNBLogThreadedIf (LOG_RNB_MAX, "%s ctx.Events().WaitForSetEvents( 0x%08x ) => 0x%08x", __FUNCTION__, event_mask, set_events); |
| 93 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 94 | if (set_events & RNBContext::event_read_thread_exiting) |
| 95 | { |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 96 | RNBLogSTDERR ("error: packet read thread exited.\n"); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 97 | return eRNBRunLoopModeExit; |
| 98 | } |
| 99 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | if (set_events & RNBContext::event_read_packet_available) |
| 101 | { |
| 102 | rnb_err_t err = rnb_err; |
| 103 | RNBRemote::PacketEnum type; |
| 104 | |
| 105 | err = remote->HandleReceivedPacket (&type); |
| 106 | |
| 107 | // check if we tried to attach to a process |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 108 | if (type == RNBRemote::vattach || type == RNBRemote::vattachwait || type == RNBRemote::vattachorwait) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | { |
| 110 | if (err == rnb_success) |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 111 | { |
| 112 | RNBLogSTDOUT ("Attach succeeded, ready to debug.\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | return eRNBRunLoopModeInferiorExecuting; |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | else |
| 116 | { |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 117 | RNBLogSTDERR ("error: attach failed.\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | return eRNBRunLoopModeExit; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (err == rnb_success) |
| 123 | { |
| 124 | // If we got our arguments we are ready to launch using the arguments |
| 125 | // and any environment variables we received. |
| 126 | if (type == RNBRemote::set_argv) |
| 127 | { |
| 128 | return eRNBRunLoopModeInferiorLaunching; |
| 129 | } |
| 130 | } |
| 131 | else if (err == rnb_not_connected) |
| 132 | { |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 133 | RNBLogSTDERR ("error: connection lost.\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | return eRNBRunLoopModeExit; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | // a catch all for any other gdb remote packets that failed |
| 139 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Error getting packet.",__FUNCTION__); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "#### %s", __FUNCTION__); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Connection closed before getting \"A\" packet.", __FUNCTION__); |
| 148 | return eRNBRunLoopModeExit; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return eRNBRunLoopModeExit; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | //---------------------------------------------------------------------- |
| 157 | // This run loop mode will wait for the process to launch and hit its |
| 158 | // entry point. It will currently ignore all events except for the |
| 159 | // process state changed event, where it watches for the process stopped |
| 160 | // or crash process state. |
| 161 | //---------------------------------------------------------------------- |
| 162 | RNBRunLoopMode |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 163 | RNBRunLoopLaunchInferior (RNBRemote *remote, const char *stdin_path, const char *stdout_path, const char *stderr_path, bool no_stdio) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | { |
| 165 | RNBContext& ctx = remote->Context(); |
| 166 | |
| 167 | // The Process stuff takes a c array, the RNBContext has a vector... |
| 168 | // So make up a c array. |
| 169 | |
| 170 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Launching '%s'...", __FUNCTION__, ctx.ArgumentAtIndex(0)); |
| 171 | |
| 172 | size_t inferior_argc = ctx.ArgumentCount(); |
| 173 | // Initialize inferior_argv with inferior_argc + 1 NULLs |
| 174 | std::vector<const char *> inferior_argv(inferior_argc + 1, NULL); |
| 175 | |
| 176 | size_t i; |
| 177 | for (i = 0; i < inferior_argc; i++) |
| 178 | inferior_argv[i] = ctx.ArgumentAtIndex(i); |
| 179 | |
| 180 | // Pass the environment array the same way: |
| 181 | |
| 182 | size_t inferior_envc = ctx.EnvironmentCount(); |
| 183 | // Initialize inferior_argv with inferior_argc + 1 NULLs |
| 184 | std::vector<const char *> inferior_envp(inferior_envc + 1, NULL); |
| 185 | |
| 186 | for (i = 0; i < inferior_envc; i++) |
| 187 | inferior_envp[i] = ctx.EnvironmentAtIndex(i); |
| 188 | |
| 189 | // Our launch type hasn't been set to anything concrete, so we need to |
| 190 | // figure our how we are going to launch automatically. |
| 191 | |
| 192 | nub_launch_flavor_t launch_flavor = g_launch_flavor; |
| 193 | if (launch_flavor == eLaunchFlavorDefault) |
| 194 | { |
| 195 | // Our default launch method is posix spawn |
| 196 | launch_flavor = eLaunchFlavorPosixSpawn; |
| 197 | |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 198 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | // Check if we have an app bundle, if so launch using SpringBoard. |
| 200 | if (strstr(inferior_argv[0], ".app")) |
| 201 | { |
| 202 | launch_flavor = eLaunchFlavorSpringBoard; |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | ctx.SetLaunchFlavor(launch_flavor); |
| 208 | char resolved_path[PATH_MAX]; |
| 209 | |
| 210 | // If we fail to resolve the path to our executable, then just use what we |
| 211 | // were given and hope for the best |
| 212 | if ( !DNBResolveExecutablePath (inferior_argv[0], resolved_path, sizeof(resolved_path)) ) |
| 213 | ::strncpy(resolved_path, inferior_argv[0], sizeof(resolved_path)); |
| 214 | |
| 215 | char launch_err_str[PATH_MAX]; |
| 216 | launch_err_str[0] = '\0'; |
Johnny Chen | 725269a | 2011-02-26 01:36:13 +0000 | [diff] [blame] | 217 | const char * cwd = (ctx.GetWorkingDirPath() != NULL ? ctx.GetWorkingDirPath() |
| 218 | : ctx.GetWorkingDirectory()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | nub_process_t pid = DNBProcessLaunch (resolved_path, |
| 220 | &inferior_argv[0], |
| 221 | &inferior_envp[0], |
Johnny Chen | 725269a | 2011-02-26 01:36:13 +0000 | [diff] [blame] | 222 | cwd, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 223 | stdin_path, |
| 224 | stdout_path, |
| 225 | stderr_path, |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 226 | no_stdio, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | launch_flavor, |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 228 | g_disable_aslr, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | launch_err_str, |
| 230 | sizeof(launch_err_str)); |
| 231 | |
| 232 | g_pid = pid; |
| 233 | |
Jason Molenda | 4e0c94b | 2011-07-08 00:00:32 +0000 | [diff] [blame] | 234 | if (pid == INVALID_NUB_PROCESS && strlen (launch_err_str) > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | { |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 236 | DNBLogThreaded ("%s DNBProcessLaunch() returned error: '%s'", __FUNCTION__, launch_err_str); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | ctx.LaunchStatus().SetError(-1, DNBError::Generic); |
| 238 | ctx.LaunchStatus().SetErrorString(launch_err_str); |
| 239 | } |
Jason Molenda | 4e0c94b | 2011-07-08 00:00:32 +0000 | [diff] [blame] | 240 | else if (pid == INVALID_NUB_PROCESS) |
| 241 | { |
| 242 | DNBLogThreaded ("%s DNBProcessLaunch() failed to launch process, unknown failure", __FUNCTION__); |
| 243 | ctx.LaunchStatus().SetError(-1, DNBError::Generic); |
| 244 | ctx.LaunchStatus().SetErrorString(launch_err_str); |
| 245 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | else |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 247 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | ctx.LaunchStatus().Clear(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | |
| 251 | if (remote->Comm().IsConnected()) |
| 252 | { |
| 253 | // It we are connected already, the next thing gdb will do is ask |
| 254 | // whether the launch succeeded, and if not, whether there is an |
| 255 | // error code. So we need to fetch one packet from gdb before we wait |
| 256 | // on the stop from the target. |
| 257 | |
| 258 | uint32_t event_mask = RNBContext::event_read_packet_available; |
| 259 | nub_event_t set_events = ctx.Events().WaitForSetEvents(event_mask); |
| 260 | |
| 261 | if (set_events & RNBContext::event_read_packet_available) |
| 262 | { |
| 263 | rnb_err_t err = rnb_err; |
| 264 | RNBRemote::PacketEnum type; |
| 265 | |
| 266 | err = remote->HandleReceivedPacket (&type); |
| 267 | |
| 268 | if (err != rnb_success) |
| 269 | { |
| 270 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Error getting packet.", __FUNCTION__); |
| 271 | return eRNBRunLoopModeExit; |
| 272 | } |
| 273 | if (type != RNBRemote::query_launch_success) |
| 274 | { |
| 275 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Didn't get the expected qLaunchSuccess packet.", __FUNCTION__); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | while (pid != INVALID_NUB_PROCESS) |
| 281 | { |
| 282 | // Wait for process to start up and hit entry point |
| 283 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s DNBProcessWaitForEvent (%4.4x, eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, true, INFINITE)...", __FUNCTION__, pid); |
| 284 | nub_event_t set_events = DNBProcessWaitForEvents (pid, eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, true, NULL); |
| 285 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s DNBProcessWaitForEvent (%4.4x, eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, true, INFINITE) => 0x%8.8x", __FUNCTION__, pid, set_events); |
| 286 | |
| 287 | if (set_events == 0) |
| 288 | { |
| 289 | pid = INVALID_NUB_PROCESS; |
| 290 | g_pid = pid; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | if (set_events & (eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged)) |
| 295 | { |
| 296 | nub_state_t pid_state = DNBProcessGetState (pid); |
| 297 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s process %4.4x state changed (eEventProcessStateChanged): %s", __FUNCTION__, pid, DNBStateAsString(pid_state)); |
| 298 | |
| 299 | switch (pid_state) |
| 300 | { |
| 301 | default: |
| 302 | case eStateInvalid: |
| 303 | case eStateUnloaded: |
| 304 | case eStateAttaching: |
| 305 | case eStateLaunching: |
| 306 | case eStateSuspended: |
| 307 | break; // Ignore |
| 308 | |
| 309 | case eStateRunning: |
| 310 | case eStateStepping: |
| 311 | // Still waiting to stop at entry point... |
| 312 | break; |
| 313 | |
| 314 | case eStateStopped: |
| 315 | case eStateCrashed: |
| 316 | ctx.SetProcessID(pid); |
| 317 | return eRNBRunLoopModeInferiorExecuting; |
| 318 | |
| 319 | case eStateDetached: |
| 320 | case eStateExited: |
| 321 | pid = INVALID_NUB_PROCESS; |
| 322 | g_pid = pid; |
| 323 | return eRNBRunLoopModeExit; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | DNBProcessResetEvents(pid, set_events); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return eRNBRunLoopModeExit; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | //---------------------------------------------------------------------- |
| 336 | // This run loop mode will wait for the process to launch and hit its |
| 337 | // entry point. It will currently ignore all events except for the |
| 338 | // process state changed event, where it watches for the process stopped |
| 339 | // or crash process state. |
| 340 | //---------------------------------------------------------------------- |
| 341 | RNBRunLoopMode |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 342 | RNBRunLoopLaunchAttaching (RNBRemote *remote, nub_process_t attach_pid, nub_process_t& pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | { |
| 344 | RNBContext& ctx = remote->Context(); |
| 345 | |
| 346 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s Attaching to pid %i...", __FUNCTION__, attach_pid); |
| 347 | char err_str[1024]; |
| 348 | pid = DNBProcessAttach (attach_pid, NULL, err_str, sizeof(err_str)); |
| 349 | g_pid = pid; |
| 350 | |
| 351 | if (pid == INVALID_NUB_PROCESS) |
| 352 | { |
| 353 | ctx.LaunchStatus().SetError(-1, DNBError::Generic); |
| 354 | if (err_str[0]) |
| 355 | ctx.LaunchStatus().SetErrorString(err_str); |
| 356 | return eRNBRunLoopModeExit; |
| 357 | } |
| 358 | else |
| 359 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | ctx.SetProcessID(pid); |
| 361 | return eRNBRunLoopModeInferiorExecuting; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | //---------------------------------------------------------------------- |
| 366 | // Watch for signals: |
| 367 | // SIGINT: so we can halt our inferior. (disabled for now) |
| 368 | // SIGPIPE: in case our child process dies |
| 369 | //---------------------------------------------------------------------- |
| 370 | int g_sigint_received = 0; |
| 371 | int g_sigpipe_received = 0; |
| 372 | void |
| 373 | signal_handler(int signo) |
| 374 | { |
| 375 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s (%s)", __FUNCTION__, SysSignal::Name(signo)); |
| 376 | |
| 377 | switch (signo) |
| 378 | { |
| 379 | case SIGINT: |
| 380 | g_sigint_received++; |
| 381 | if (g_pid != INVALID_NUB_PROCESS) |
| 382 | { |
| 383 | // Only send a SIGINT once... |
| 384 | if (g_sigint_received == 1) |
| 385 | { |
| 386 | switch (DNBProcessGetState (g_pid)) |
| 387 | { |
| 388 | case eStateRunning: |
| 389 | case eStateStepping: |
| 390 | DNBProcessSignal (g_pid, SIGSTOP); |
| 391 | return; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 392 | default: |
| 393 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | exit (SIGINT); |
| 398 | break; |
| 399 | |
| 400 | case SIGPIPE: |
| 401 | g_sigpipe_received = 1; |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Return the new run loop mode based off of the current process state |
| 407 | RNBRunLoopMode |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 408 | HandleProcessStateChange (RNBRemote *remote, bool initialize) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | { |
| 410 | RNBContext& ctx = remote->Context(); |
| 411 | nub_process_t pid = ctx.ProcessID(); |
| 412 | |
| 413 | if (pid == INVALID_NUB_PROCESS) |
| 414 | { |
| 415 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "#### %s error: pid invalid, exiting...", __FUNCTION__); |
| 416 | return eRNBRunLoopModeExit; |
| 417 | } |
| 418 | nub_state_t pid_state = DNBProcessGetState (pid); |
| 419 | |
| 420 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s (&remote, initialize=%i) pid_state = %s", __FUNCTION__, (int)initialize, DNBStateAsString (pid_state)); |
| 421 | |
| 422 | switch (pid_state) |
| 423 | { |
| 424 | case eStateInvalid: |
| 425 | case eStateUnloaded: |
| 426 | // Something bad happened |
| 427 | return eRNBRunLoopModeExit; |
| 428 | break; |
| 429 | |
| 430 | case eStateAttaching: |
| 431 | case eStateLaunching: |
| 432 | return eRNBRunLoopModeInferiorExecuting; |
| 433 | |
| 434 | case eStateSuspended: |
| 435 | case eStateCrashed: |
| 436 | case eStateStopped: |
| 437 | // If we stop due to a signal, so clear the fact that we got a SIGINT |
| 438 | // so we can stop ourselves again (but only while our inferior |
| 439 | // process is running..) |
| 440 | g_sigint_received = 0; |
| 441 | if (initialize == false) |
| 442 | { |
| 443 | // Compare the last stop count to our current notion of a stop count |
| 444 | // to make sure we don't notify more than once for a given stop. |
| 445 | nub_size_t prev_pid_stop_count = ctx.GetProcessStopCount(); |
| 446 | bool pid_stop_count_changed = ctx.SetProcessStopCount(DNBProcessGetStopCount(pid)); |
| 447 | if (pid_stop_count_changed) |
| 448 | { |
| 449 | remote->FlushSTDIO(); |
| 450 | |
| 451 | if (ctx.GetProcessStopCount() == 1) |
| 452 | { |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 453 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s (&remote, initialize=%i) pid_state = %s pid_stop_count %llu (old %llu)) Notify??? no, first stop...", __FUNCTION__, (int)initialize, DNBStateAsString (pid_state), (uint64_t)ctx.GetProcessStopCount(), (uint64_t)prev_pid_stop_count); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 454 | } |
| 455 | else |
| 456 | { |
| 457 | |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 458 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s (&remote, initialize=%i) pid_state = %s pid_stop_count %llu (old %llu)) Notify??? YES!!!", __FUNCTION__, (int)initialize, DNBStateAsString (pid_state), (uint64_t)ctx.GetProcessStopCount(), (uint64_t)prev_pid_stop_count); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | remote->NotifyThatProcessStopped (); |
| 460 | } |
| 461 | } |
| 462 | else |
| 463 | { |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 464 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "%s (&remote, initialize=%i) pid_state = %s pid_stop_count %llu (old %llu)) Notify??? skipping...", __FUNCTION__, (int)initialize, DNBStateAsString (pid_state), (uint64_t)ctx.GetProcessStopCount(), (uint64_t)prev_pid_stop_count); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | return eRNBRunLoopModeInferiorExecuting; |
| 468 | |
| 469 | case eStateStepping: |
| 470 | case eStateRunning: |
| 471 | return eRNBRunLoopModeInferiorExecuting; |
| 472 | |
| 473 | case eStateExited: |
| 474 | remote->HandlePacket_last_signal(NULL); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 475 | case eStateDetached: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 476 | return eRNBRunLoopModeExit; |
| 477 | |
| 478 | } |
| 479 | |
| 480 | // Catch all... |
| 481 | return eRNBRunLoopModeExit; |
| 482 | } |
| 483 | // This function handles the case where our inferior program is stopped and |
| 484 | // we are waiting for gdb remote protocol packets. When a packet occurs that |
| 485 | // makes the inferior run, we need to leave this function with a new state |
| 486 | // as the return code. |
| 487 | RNBRunLoopMode |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 488 | RNBRunLoopInferiorExecuting (RNBRemote *remote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | { |
| 490 | DNBLogThreadedIf (LOG_RNB_MINIMAL, "#### %s", __FUNCTION__); |
| 491 | RNBContext& ctx = remote->Context(); |
| 492 | |
| 493 | // Init our mode and set 'is_running' based on the current process state |
| 494 | RNBRunLoopMode mode = HandleProcessStateChange (remote, true); |
| 495 | |
| 496 | while (ctx.ProcessID() != INVALID_NUB_PROCESS) |
| 497 | { |
| 498 | |
| 499 | std::string set_events_str; |
| 500 | uint32_t event_mask = ctx.NormalEventBits(); |
| 501 | |
| 502 | if (!ctx.ProcessStateRunning()) |
| 503 | { |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 504 | // Clear some bits if we are not running so we don't send any async packets |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | event_mask &= ~RNBContext::event_proc_stdio_available; |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 506 | event_mask &= ~RNBContext::event_proc_profile_data; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // We want to make sure we consume all process state changes and have |
| 510 | // whomever is notifying us to wait for us to reset the event bit before |
| 511 | // continuing. |
| 512 | //ctx.Events().SetResetAckMask (RNBContext::event_proc_state_changed); |
| 513 | |
| 514 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s ctx.Events().WaitForSetEvents(0x%08x) ...",__FUNCTION__, event_mask); |
| 515 | nub_event_t set_events = ctx.Events().WaitForSetEvents(event_mask); |
| 516 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s ctx.Events().WaitForSetEvents(0x%08x) => 0x%08x (%s)",__FUNCTION__, event_mask, set_events, ctx.EventsAsString(set_events, set_events_str)); |
| 517 | |
| 518 | if (set_events) |
| 519 | { |
| 520 | if ((set_events & RNBContext::event_proc_thread_exiting) || |
| 521 | (set_events & RNBContext::event_proc_stdio_available)) |
| 522 | { |
| 523 | remote->FlushSTDIO(); |
| 524 | } |
| 525 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 526 | if (set_events & RNBContext::event_proc_profile_data) |
| 527 | { |
| 528 | remote->SendAsyncProfileData(); |
| 529 | } |
| 530 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | if (set_events & RNBContext::event_read_packet_available) |
| 532 | { |
| 533 | // handleReceivedPacket will take care of resetting the |
| 534 | // event_read_packet_available events when there are no more... |
| 535 | set_events ^= RNBContext::event_read_packet_available; |
| 536 | |
| 537 | if (ctx.ProcessStateRunning()) |
| 538 | { |
| 539 | if (remote->HandleAsyncPacket() == rnb_not_connected) |
| 540 | { |
| 541 | // TODO: connect again? Exit? |
| 542 | } |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | if (remote->HandleReceivedPacket() == rnb_not_connected) |
| 547 | { |
| 548 | // TODO: connect again? Exit? |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (set_events & RNBContext::event_proc_state_changed) |
| 554 | { |
| 555 | mode = HandleProcessStateChange (remote, false); |
| 556 | ctx.Events().ResetEvents(RNBContext::event_proc_state_changed); |
| 557 | set_events ^= RNBContext::event_proc_state_changed; |
| 558 | } |
| 559 | |
| 560 | if (set_events & RNBContext::event_proc_thread_exiting) |
| 561 | { |
| 562 | mode = eRNBRunLoopModeExit; |
| 563 | } |
| 564 | |
| 565 | if (set_events & RNBContext::event_read_thread_exiting) |
| 566 | { |
| 567 | // Out remote packet receiving thread exited, exit for now. |
| 568 | if (ctx.HasValidProcessID()) |
| 569 | { |
| 570 | // TODO: We should add code that will leave the current process |
| 571 | // in its current state and listen for another connection... |
| 572 | if (ctx.ProcessStateRunning()) |
| 573 | { |
Jason Molenda | 6566d43 | 2013-03-23 05:35:57 +0000 | [diff] [blame] | 574 | DNBLog ("debugserver's event read thread is exiting, killing the inferior process."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | DNBProcessKill (ctx.ProcessID()); |
| 576 | } |
| 577 | } |
| 578 | mode = eRNBRunLoopModeExit; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Reset all event bits that weren't reset for now... |
| 583 | if (set_events != 0) |
| 584 | ctx.Events().ResetEvents(set_events); |
| 585 | |
| 586 | if (mode != eRNBRunLoopModeInferiorExecuting) |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | return mode; |
| 591 | } |
| 592 | |
| 593 | |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 594 | RNBRunLoopMode |
| 595 | RNBRunLoopPlatform (RNBRemote *remote) |
| 596 | { |
| 597 | RNBRunLoopMode mode = eRNBRunLoopModePlatformMode; |
| 598 | RNBContext& ctx = remote->Context(); |
| 599 | |
| 600 | while (mode == eRNBRunLoopModePlatformMode) |
| 601 | { |
| 602 | std::string set_events_str; |
| 603 | const uint32_t event_mask = RNBContext::event_read_packet_available | |
| 604 | RNBContext::event_read_thread_exiting; |
| 605 | |
| 606 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s ctx.Events().WaitForSetEvents(0x%08x) ...",__FUNCTION__, event_mask); |
| 607 | nub_event_t set_events = ctx.Events().WaitForSetEvents(event_mask); |
| 608 | DNBLogThreadedIf (LOG_RNB_EVENTS, "%s ctx.Events().WaitForSetEvents(0x%08x) => 0x%08x (%s)",__FUNCTION__, event_mask, set_events, ctx.EventsAsString(set_events, set_events_str)); |
| 609 | |
| 610 | if (set_events) |
| 611 | { |
| 612 | if (set_events & RNBContext::event_read_packet_available) |
| 613 | { |
| 614 | if (remote->HandleReceivedPacket() == rnb_not_connected) |
| 615 | mode = eRNBRunLoopModeExit; |
| 616 | } |
| 617 | |
| 618 | if (set_events & RNBContext::event_read_thread_exiting) |
| 619 | { |
| 620 | mode = eRNBRunLoopModeExit; |
| 621 | } |
| 622 | ctx.Events().ResetEvents(set_events); |
| 623 | } |
| 624 | } |
| 625 | return eRNBRunLoopModeExit; |
| 626 | } |
| 627 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 628 | static void |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 629 | PortWasBoundCallbackNamedPipe (const void *baton, in_port_t port) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 630 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 631 | const char *named_pipe = (const char *)baton; |
| 632 | if (named_pipe && named_pipe[0]) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 633 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 634 | int fd = ::open(named_pipe, O_WRONLY); |
| 635 | if (fd > -1) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 636 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 637 | char port_str[64]; |
| 638 | const ssize_t port_str_len = ::snprintf (port_str, sizeof(port_str), "%u", port); |
| 639 | // Write the port number as a C string with the NULL terminator |
| 640 | ::write (fd, port_str, port_str_len + 1); |
| 641 | close (fd); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 642 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | static int |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 647 | StartListening (RNBRemote *remote, const char *listen_host, int listen_port, const char *named_pipe_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 649 | if (!remote->Comm().IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 650 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 651 | if (listen_port != 0) |
Greg Clayton | fd23889 | 2013-06-06 22:44:19 +0000 | [diff] [blame] | 652 | RNBLogSTDOUT ("Listening to port %i for a connection from %s...\n", listen_port, listen_host ? listen_host : "localhost"); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 653 | if (remote->Comm().Listen(listen_host, listen_port, PortWasBoundCallbackNamedPipe, named_pipe_path) != rnb_success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 654 | { |
| 655 | RNBLogSTDERR ("Failed to get connection from a remote gdb process.\n"); |
| 656 | return 0; |
| 657 | } |
| 658 | else |
| 659 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 660 | remote->StartReadRemoteDataThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | //---------------------------------------------------------------------- |
| 667 | // ASL Logging callback that can be registered with DNBLogSetLogCallback |
| 668 | //---------------------------------------------------------------------- |
| 669 | void |
| 670 | ASLLogCallback(void *baton, uint32_t flags, const char *format, va_list args) |
| 671 | { |
| 672 | if (format == NULL) |
| 673 | return; |
| 674 | static aslmsg g_aslmsg = NULL; |
| 675 | if (g_aslmsg == NULL) |
| 676 | { |
| 677 | g_aslmsg = ::asl_new (ASL_TYPE_MSG); |
| 678 | char asl_key_sender[PATH_MAX]; |
| 679 | snprintf(asl_key_sender, sizeof(asl_key_sender), "com.apple.%s-%g", DEBUGSERVER_PROGRAM_NAME, DEBUGSERVER_VERSION_NUM); |
| 680 | ::asl_set (g_aslmsg, ASL_KEY_SENDER, asl_key_sender); |
| 681 | } |
| 682 | |
| 683 | int asl_level; |
| 684 | if (flags & DNBLOG_FLAG_FATAL) asl_level = ASL_LEVEL_CRIT; |
| 685 | else if (flags & DNBLOG_FLAG_ERROR) asl_level = ASL_LEVEL_ERR; |
| 686 | else if (flags & DNBLOG_FLAG_WARNING) asl_level = ASL_LEVEL_WARNING; |
| 687 | else if (flags & DNBLOG_FLAG_VERBOSE) asl_level = ASL_LEVEL_WARNING; //ASL_LEVEL_INFO; |
| 688 | else asl_level = ASL_LEVEL_WARNING; //ASL_LEVEL_DEBUG; |
| 689 | |
| 690 | ::asl_vlog (NULL, g_aslmsg, asl_level, format, args); |
| 691 | } |
| 692 | |
| 693 | //---------------------------------------------------------------------- |
| 694 | // FILE based Logging callback that can be registered with |
| 695 | // DNBLogSetLogCallback |
| 696 | //---------------------------------------------------------------------- |
| 697 | void |
| 698 | FileLogCallback(void *baton, uint32_t flags, const char *format, va_list args) |
| 699 | { |
| 700 | if (baton == NULL || format == NULL) |
| 701 | return; |
| 702 | |
| 703 | ::vfprintf ((FILE *)baton, format, args); |
| 704 | ::fprintf ((FILE *)baton, "\n"); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | void |
| 709 | show_usage_and_exit (int exit_code) |
| 710 | { |
| 711 | RNBLogSTDERR ("Usage:\n %s host:port [program-name program-arg1 program-arg2 ...]\n", DEBUGSERVER_PROGRAM_NAME); |
| 712 | RNBLogSTDERR (" %s /path/file [program-name program-arg1 program-arg2 ...]\n", DEBUGSERVER_PROGRAM_NAME); |
| 713 | RNBLogSTDERR (" %s host:port --attach=<pid>\n", DEBUGSERVER_PROGRAM_NAME); |
| 714 | RNBLogSTDERR (" %s /path/file --attach=<pid>\n", DEBUGSERVER_PROGRAM_NAME); |
| 715 | RNBLogSTDERR (" %s host:port --attach=<process_name>\n", DEBUGSERVER_PROGRAM_NAME); |
| 716 | RNBLogSTDERR (" %s /path/file --attach=<process_name>\n", DEBUGSERVER_PROGRAM_NAME); |
| 717 | exit (exit_code); |
| 718 | } |
| 719 | |
| 720 | |
| 721 | //---------------------------------------------------------------------- |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 722 | // option descriptors for getopt_long_only() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 723 | //---------------------------------------------------------------------- |
| 724 | static struct option g_long_options[] = |
| 725 | { |
| 726 | { "attach", required_argument, NULL, 'a' }, |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 727 | { "arch", required_argument, NULL, 'A' }, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 728 | { "debug", no_argument, NULL, 'g' }, |
| 729 | { "verbose", no_argument, NULL, 'v' }, |
| 730 | { "lockdown", no_argument, &g_lockdown_opt, 1 }, // short option "-k" |
| 731 | { "applist", no_argument, &g_applist_opt, 1 }, // short option "-t" |
| 732 | { "log-file", required_argument, NULL, 'l' }, |
| 733 | { "log-flags", required_argument, NULL, 'f' }, |
| 734 | { "launch", required_argument, NULL, 'x' }, // Valid values are "auto", "posix-spawn", "fork-exec", "springboard" (arm only) |
| 735 | { "waitfor", required_argument, NULL, 'w' }, // Wait for a process whose name starts with ARG |
| 736 | { "waitfor-interval", required_argument, NULL, 'i' }, // Time in usecs to wait between sampling the pid list when waiting for a process by name |
| 737 | { "waitfor-duration", required_argument, NULL, 'd' }, // The time in seconds to wait for a process to show up by name |
| 738 | { "native-regs", no_argument, NULL, 'r' }, // Specify to use the native registers instead of the gdb defaults for the architecture. |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 739 | { "stdio-path", required_argument, NULL, 's' }, // Set the STDIO path to be used when launching applications (STDIN, STDOUT and STDERR) (only if debugserver launches the process) |
| 740 | { "stdin-path", required_argument, NULL, 'I' }, // Set the STDIN path to be used when launching applications (only if debugserver launches the process) |
Johnny Chen | a343545 | 2011-01-25 16:56:01 +0000 | [diff] [blame] | 741 | { "stdout-path", required_argument, NULL, 'O' }, // Set the STDOUT path to be used when launching applications (only if debugserver launches the process) |
| 742 | { "stderr-path", required_argument, NULL, 'E' }, // Set the STDERR path to be used when launching applications (only if debugserver launches the process) |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 743 | { "no-stdio", no_argument, NULL, 'n' }, // Do not set up any stdio (perhaps the program is a GUI program) (only if debugserver launches the process) |
| 744 | { "setsid", no_argument, NULL, 'S' }, // call setsid() to make debugserver run in its own session |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 745 | { "disable-aslr", no_argument, NULL, 'D' }, // Use _POSIX_SPAWN_DISABLE_ASLR to avoid shared library randomization |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 746 | { "working-dir", required_argument, NULL, 'W' }, // The working directory that the inferior process should have (only if debugserver launches the process) |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 747 | { "platform", required_argument, NULL, 'p' }, // Put this executable into a remote platform mode |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 748 | { "named-pipe", required_argument, NULL, 'P' }, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | { NULL, 0, NULL, 0 } |
| 750 | }; |
| 751 | |
| 752 | |
| 753 | //---------------------------------------------------------------------- |
| 754 | // main |
| 755 | //---------------------------------------------------------------------- |
| 756 | int |
| 757 | main (int argc, char *argv[]) |
| 758 | { |
Jason Molenda | 0b2dbe0 | 2012-11-01 02:02:59 +0000 | [diff] [blame] | 759 | const char *argv_sub_zero = argv[0]; // save a copy of argv[0] for error reporting post-launch |
| 760 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | g_isatty = ::isatty (STDIN_FILENO); |
| 762 | |
| 763 | // ::printf ("uid=%u euid=%u gid=%u egid=%u\n", |
| 764 | // getuid(), |
| 765 | // geteuid(), |
| 766 | // getgid(), |
| 767 | // getegid()); |
| 768 | |
| 769 | |
| 770 | // signal (SIGINT, signal_handler); |
| 771 | signal (SIGPIPE, signal_handler); |
Greg Clayton | 3382c2c | 2010-07-30 23:14:42 +0000 | [diff] [blame] | 772 | signal (SIGHUP, signal_handler); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 774 | g_remoteSP.reset (new RNBRemote ()); |
| 775 | |
| 776 | |
| 777 | RNBRemote *remote = g_remoteSP.get(); |
| 778 | if (remote == NULL) |
| 779 | { |
| 780 | RNBLogSTDERR ("error: failed to create a remote connection class\n"); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | RNBContext& ctx = remote->Context(); |
| 785 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 786 | int i; |
| 787 | int attach_pid = INVALID_NUB_PROCESS; |
| 788 | |
| 789 | FILE* log_file = NULL; |
| 790 | uint32_t log_flags = 0; |
| 791 | // Parse our options |
| 792 | int ch; |
| 793 | int long_option_index = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | int debug = 0; |
| 795 | std::string compile_options; |
| 796 | std::string waitfor_pid_name; // Wait for a process that starts with this name |
| 797 | std::string attach_pid_name; |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 798 | std::string arch_name; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 799 | std::string working_dir; // The new working directory to use for the inferior |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 800 | std::string named_pipe_path; // If we need to handshake with our parent process, an option will be passed down that specifies a named pipe to use |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | useconds_t waitfor_interval = 1000; // Time in usecs between process lists polls when waiting for a process by name, default 1 msec. |
| 802 | useconds_t waitfor_duration = 0; // Time in seconds to wait for a process by name, 0 means wait forever. |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 803 | bool no_stdio = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 804 | |
| 805 | #if !defined (DNBLOG_ENABLED) |
| 806 | compile_options += "(no-logging) "; |
| 807 | #endif |
| 808 | |
| 809 | RNBRunLoopMode start_mode = eRNBRunLoopModeExit; |
| 810 | |
Greg Clayton | 8d400e17 | 2011-05-23 18:04:09 +0000 | [diff] [blame] | 811 | char short_options[512]; |
| 812 | uint32_t short_options_idx = 0; |
| 813 | |
| 814 | // Handle the two case that don't have short options in g_long_options |
| 815 | short_options[short_options_idx++] = 'k'; |
| 816 | short_options[short_options_idx++] = 't'; |
| 817 | |
| 818 | for (i=0; g_long_options[i].name != NULL; ++i) |
| 819 | { |
| 820 | if (isalpha(g_long_options[i].val)) |
| 821 | { |
| 822 | short_options[short_options_idx++] = g_long_options[i].val; |
| 823 | switch (g_long_options[i].has_arg) |
| 824 | { |
| 825 | default: |
| 826 | case no_argument: |
| 827 | break; |
| 828 | |
| 829 | case optional_argument: |
| 830 | short_options[short_options_idx++] = ':'; |
| 831 | // Fall through to required_argument case below... |
| 832 | case required_argument: |
| 833 | short_options[short_options_idx++] = ':'; |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | // NULL terminate the short option string. |
| 839 | short_options[short_options_idx++] = '\0'; |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 840 | |
| 841 | #if __GLIBC__ |
| 842 | optind = 0; |
| 843 | #else |
| 844 | optreset = 1; |
| 845 | optind = 1; |
| 846 | #endif |
| 847 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 848 | while ((ch = getopt_long_only(argc, argv, short_options, g_long_options, &long_option_index)) != -1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | { |
| 850 | DNBLogDebug("option: ch == %c (0x%2.2x) --%s%c%s\n", |
| 851 | ch, (uint8_t)ch, |
| 852 | g_long_options[long_option_index].name, |
| 853 | g_long_options[long_option_index].has_arg ? '=' : ' ', |
| 854 | optarg ? optarg : ""); |
| 855 | switch (ch) |
| 856 | { |
| 857 | case 0: // Any optional that auto set themselves will return 0 |
| 858 | break; |
| 859 | |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 860 | case 'A': |
| 861 | if (optarg && optarg[0]) |
| 862 | arch_name.assign(optarg); |
| 863 | break; |
| 864 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | case 'a': |
| 866 | if (optarg && optarg[0]) |
| 867 | { |
| 868 | if (isdigit(optarg[0])) |
| 869 | { |
| 870 | char *end = NULL; |
| 871 | attach_pid = strtoul(optarg, &end, 0); |
| 872 | if (end == NULL || *end != '\0') |
| 873 | { |
| 874 | RNBLogSTDERR ("error: invalid pid option '%s'\n", optarg); |
| 875 | exit (4); |
| 876 | } |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | attach_pid_name = optarg; |
| 881 | } |
| 882 | start_mode = eRNBRunLoopModeInferiorAttaching; |
| 883 | } |
| 884 | break; |
| 885 | |
| 886 | // --waitfor=NAME |
| 887 | case 'w': |
| 888 | if (optarg && optarg[0]) |
| 889 | { |
| 890 | waitfor_pid_name = optarg; |
| 891 | start_mode = eRNBRunLoopModeInferiorAttaching; |
| 892 | } |
| 893 | break; |
| 894 | |
| 895 | // --waitfor-interval=USEC |
| 896 | case 'i': |
| 897 | if (optarg && optarg[0]) |
| 898 | { |
| 899 | char *end = NULL; |
| 900 | waitfor_interval = strtoul(optarg, &end, 0); |
| 901 | if (end == NULL || *end != '\0') |
| 902 | { |
| 903 | RNBLogSTDERR ("error: invalid waitfor-interval option value '%s'.\n", optarg); |
| 904 | exit (6); |
| 905 | } |
| 906 | } |
| 907 | break; |
| 908 | |
| 909 | // --waitfor-duration=SEC |
| 910 | case 'd': |
| 911 | if (optarg && optarg[0]) |
| 912 | { |
| 913 | char *end = NULL; |
| 914 | waitfor_duration = strtoul(optarg, &end, 0); |
| 915 | if (end == NULL || *end != '\0') |
| 916 | { |
| 917 | RNBLogSTDERR ("error: invalid waitfor-duration option value '%s'.\n", optarg); |
| 918 | exit (7); |
| 919 | } |
| 920 | } |
| 921 | break; |
| 922 | |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 923 | case 'W': |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 924 | if (optarg && optarg[0]) |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 925 | working_dir.assign(optarg); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 926 | break; |
| 927 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 928 | case 'x': |
| 929 | if (optarg && optarg[0]) |
| 930 | { |
| 931 | if (strcasecmp(optarg, "auto") == 0) |
| 932 | g_launch_flavor = eLaunchFlavorDefault; |
| 933 | else if (strcasestr(optarg, "posix") == optarg) |
| 934 | g_launch_flavor = eLaunchFlavorPosixSpawn; |
| 935 | else if (strcasestr(optarg, "fork") == optarg) |
| 936 | g_launch_flavor = eLaunchFlavorForkExec; |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 937 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 938 | else if (strcasestr(optarg, "spring") == optarg) |
| 939 | g_launch_flavor = eLaunchFlavorSpringBoard; |
| 940 | #endif |
| 941 | else |
| 942 | { |
| 943 | RNBLogSTDERR ("error: invalid TYPE for the --launch=TYPE (-x TYPE) option: '%s'\n", optarg); |
| 944 | RNBLogSTDERR ("Valid values TYPE are:\n"); |
| 945 | RNBLogSTDERR (" auto Auto-detect the best launch method to use.\n"); |
| 946 | RNBLogSTDERR (" posix Launch the executable using posix_spawn.\n"); |
| 947 | RNBLogSTDERR (" fork Launch the executable using fork and exec.\n"); |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 948 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 949 | RNBLogSTDERR (" spring Launch the executable through Springboard.\n"); |
| 950 | #endif |
| 951 | exit (5); |
| 952 | } |
| 953 | } |
| 954 | break; |
| 955 | |
| 956 | case 'l': // Set Log File |
| 957 | if (optarg && optarg[0]) |
| 958 | { |
| 959 | if (strcasecmp(optarg, "stdout") == 0) |
| 960 | log_file = stdout; |
| 961 | else if (strcasecmp(optarg, "stderr") == 0) |
| 962 | log_file = stderr; |
| 963 | else |
Jim Ingham | c530be6 | 2011-01-24 03:46:59 +0000 | [diff] [blame] | 964 | { |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 965 | log_file = fopen(optarg, "w"); |
Jim Ingham | c530be6 | 2011-01-24 03:46:59 +0000 | [diff] [blame] | 966 | if (log_file != NULL) |
| 967 | setlinebuf(log_file); |
| 968 | } |
| 969 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 970 | if (log_file == NULL) |
| 971 | { |
| 972 | const char *errno_str = strerror(errno); |
| 973 | RNBLogSTDERR ("Failed to open log file '%s' for writing: errno = %i (%s)", optarg, errno, errno_str ? errno_str : "unknown error"); |
| 974 | } |
| 975 | } |
| 976 | break; |
| 977 | |
| 978 | case 'f': // Log Flags |
| 979 | if (optarg && optarg[0]) |
| 980 | log_flags = strtoul(optarg, NULL, 0); |
| 981 | break; |
| 982 | |
| 983 | case 'g': |
| 984 | debug = 1; |
Johnny Chen | 2fe7dd4 | 2011-08-10 23:01:39 +0000 | [diff] [blame] | 985 | DNBLogSetDebug(debug); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 986 | break; |
| 987 | |
| 988 | case 't': |
| 989 | g_applist_opt = 1; |
| 990 | break; |
| 991 | |
| 992 | case 'k': |
| 993 | g_lockdown_opt = 1; |
| 994 | break; |
| 995 | |
| 996 | case 'r': |
Greg Clayton | 8548002 | 2013-11-09 00:33:46 +0000 | [diff] [blame] | 997 | // Do nothing, native regs is the default these days |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 998 | break; |
| 999 | |
| 1000 | case 'v': |
| 1001 | DNBLogSetVerbose(1); |
| 1002 | break; |
| 1003 | |
| 1004 | case 's': |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1005 | ctx.GetSTDIN().assign(optarg); |
| 1006 | ctx.GetSTDOUT().assign(optarg); |
| 1007 | ctx.GetSTDERR().assign(optarg); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1008 | break; |
| 1009 | |
| 1010 | case 'I': |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1011 | ctx.GetSTDIN().assign(optarg); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | |
| 1014 | case 'O': |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1015 | ctx.GetSTDOUT().assign(optarg); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | |
| 1018 | case 'E': |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1019 | ctx.GetSTDERR().assign(optarg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | |
Caroline Tice | f8da863 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 1022 | case 'n': |
| 1023 | no_stdio = true; |
| 1024 | break; |
| 1025 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1026 | case 'S': |
| 1027 | // Put debugserver into a new session. Terminals group processes |
| 1028 | // into sessions and when a special terminal key sequences |
| 1029 | // (like control+c) are typed they can cause signals to go out to |
| 1030 | // all processes in a session. Using this --setsid (-S) option |
| 1031 | // will cause debugserver to run in its own sessions and be free |
| 1032 | // from such issues. |
| 1033 | // |
| 1034 | // This is useful when debugserver is spawned from a command |
| 1035 | // line application that uses debugserver to do the debugging, |
| 1036 | // yet that application doesn't want debugserver receiving the |
| 1037 | // signals sent to the session (i.e. dying when anyone hits ^C). |
| 1038 | setsid(); |
| 1039 | break; |
Greg Clayton | f681b94 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1040 | case 'D': |
| 1041 | g_disable_aslr = 1; |
| 1042 | break; |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1043 | |
| 1044 | case 'p': |
| 1045 | start_mode = eRNBRunLoopModePlatformMode; |
| 1046 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1047 | |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 1048 | case 'P': |
| 1049 | named_pipe_path.assign (optarg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1050 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (arch_name.empty()) |
| 1055 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1056 | #if defined (__arm__) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1057 | arch_name.assign ("arm"); |
| 1058 | #endif |
| 1059 | } |
Greg Clayton | 3c14438 | 2010-12-01 22:45:40 +0000 | [diff] [blame] | 1060 | else |
| 1061 | { |
| 1062 | DNBSetArchitecture (arch_name.c_str()); |
| 1063 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1064 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1065 | // if (arch_name.empty()) |
| 1066 | // { |
| 1067 | // fprintf(stderr, "error: no architecture was specified\n"); |
| 1068 | // exit (8); |
| 1069 | // } |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 1070 | // Skip any options we consumed with getopt_long_only |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1071 | argc -= optind; |
| 1072 | argv += optind; |
| 1073 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1074 | |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1075 | if (!working_dir.empty()) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1076 | { |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1077 | if (remote->Context().SetWorkingDirectory (working_dir.c_str()) == false) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1078 | { |
Greg Clayton | bd82a5d | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1079 | RNBLogSTDERR ("error: working directory doesn't exist '%s'.\n", working_dir.c_str()); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1080 | exit (8); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | remote->Initialize(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1086 | // It is ok for us to set NULL as the logfile (this will disable any logging) |
| 1087 | |
| 1088 | if (log_file != NULL) |
| 1089 | { |
| 1090 | DNBLogSetLogCallback(FileLogCallback, log_file); |
| 1091 | // If our log file was set, yet we have no log flags, log everything! |
| 1092 | if (log_flags == 0) |
| 1093 | log_flags = LOG_ALL | LOG_RNB_ALL; |
| 1094 | |
| 1095 | DNBLogSetLogMask (log_flags); |
| 1096 | } |
| 1097 | else |
| 1098 | { |
| 1099 | // Enable DNB logging |
| 1100 | DNBLogSetLogCallback(ASLLogCallback, NULL); |
| 1101 | DNBLogSetLogMask (log_flags); |
| 1102 | |
| 1103 | } |
| 1104 | |
| 1105 | if (DNBLogEnabled()) |
| 1106 | { |
| 1107 | for (i=0; i<argc; i++) |
| 1108 | DNBLogDebug("argv[%i] = %s", i, argv[i]); |
| 1109 | } |
| 1110 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1111 | // as long as we're dropping remotenub in as a replacement for gdbserver, |
| 1112 | // explicitly note that this is not gdbserver. |
| 1113 | |
| 1114 | RNBLogSTDOUT ("%s-%g %sfor %s.\n", |
| 1115 | DEBUGSERVER_PROGRAM_NAME, |
| 1116 | DEBUGSERVER_VERSION_NUM, |
| 1117 | compile_options.c_str(), |
| 1118 | RNB_ARCH); |
| 1119 | |
Greg Clayton | fd23889 | 2013-06-06 22:44:19 +0000 | [diff] [blame] | 1120 | std::string listen_host; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1121 | int listen_port = INT32_MAX; |
| 1122 | char str[PATH_MAX]; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 1123 | str[0] = '\0'; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (g_lockdown_opt == 0 && g_applist_opt == 0) |
| 1126 | { |
| 1127 | // Make sure we at least have port |
| 1128 | if (argc < 1) |
| 1129 | { |
| 1130 | show_usage_and_exit (1); |
| 1131 | } |
| 1132 | // accept 'localhost:' prefix on port number |
| 1133 | |
| 1134 | int items_scanned = ::sscanf (argv[0], "%[^:]:%i", str, &listen_port); |
| 1135 | if (items_scanned == 2) |
| 1136 | { |
Greg Clayton | fd23889 | 2013-06-06 22:44:19 +0000 | [diff] [blame] | 1137 | listen_host = str; |
| 1138 | DNBLogDebug("host = '%s' port = %i", listen_host.c_str(), listen_port); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | } |
| 1140 | else |
| 1141 | { |
Greg Clayton | fd23889 | 2013-06-06 22:44:19 +0000 | [diff] [blame] | 1142 | // No hostname means "localhost" |
| 1143 | int items_scanned = ::sscanf (argv[0], "%i", &listen_port); |
| 1144 | if (items_scanned == 1) |
| 1145 | { |
| 1146 | listen_host = "localhost"; |
| 1147 | DNBLogDebug("host = '%s' port = %i", listen_host.c_str(), listen_port); |
| 1148 | } |
| 1149 | else if (argv[0][0] == '/') |
| 1150 | { |
| 1151 | listen_port = INT32_MAX; |
| 1152 | strncpy(str, argv[0], sizeof(str)); |
| 1153 | } |
| 1154 | else |
| 1155 | { |
| 1156 | show_usage_and_exit (2); |
| 1157 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | // We just used the 'host:port' or the '/path/file' arg... |
| 1161 | argc--; |
| 1162 | argv++; |
| 1163 | |
| 1164 | } |
| 1165 | |
| 1166 | // If we know we're waiting to attach, we don't need any of this other info. |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1167 | if (start_mode != eRNBRunLoopModeInferiorAttaching && |
| 1168 | start_mode != eRNBRunLoopModePlatformMode) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1169 | { |
| 1170 | if (argc == 0 || g_lockdown_opt) |
| 1171 | { |
| 1172 | if (g_lockdown_opt != 0) |
| 1173 | { |
| 1174 | // Work around for SIGPIPE crashes due to posix_spawn issue. |
| 1175 | // We have to close STDOUT and STDERR, else the first time we |
| 1176 | // try and do any, we get SIGPIPE and die as posix_spawn is |
| 1177 | // doing bad things with our file descriptors at the moment. |
| 1178 | int null = open("/dev/null", O_RDWR); |
| 1179 | dup2(null, STDOUT_FILENO); |
| 1180 | dup2(null, STDERR_FILENO); |
| 1181 | } |
| 1182 | else if (g_applist_opt != 0) |
| 1183 | { |
| 1184 | // List all applications we are able to see |
| 1185 | std::string applist_plist; |
| 1186 | int err = ListApplications(applist_plist, false, false); |
| 1187 | if (err == 0) |
| 1188 | { |
| 1189 | fputs (applist_plist.c_str(), stdout); |
| 1190 | } |
| 1191 | else |
| 1192 | { |
| 1193 | RNBLogSTDERR ("error: ListApplications returned error %i\n", err); |
| 1194 | } |
| 1195 | // Exit with appropriate error if we were asked to list the applications |
| 1196 | // with no other args were given (and we weren't trying to do this over |
| 1197 | // lockdown) |
| 1198 | return err; |
| 1199 | } |
| 1200 | |
| 1201 | DNBLogDebug("Get args from remote protocol..."); |
| 1202 | start_mode = eRNBRunLoopModeGetStartModeFromRemoteProtocol; |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | start_mode = eRNBRunLoopModeInferiorLaunching; |
| 1207 | // Fill in the argv array in the context from the rest of our args. |
| 1208 | // Skip the name of this executable and the port number |
| 1209 | for (int i = 0; i < argc; i++) |
| 1210 | { |
| 1211 | DNBLogDebug("inferior_argv[%i] = '%s'", i, argv[i]); |
| 1212 | ctx.PushArgument (argv[i]); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | if (start_mode == eRNBRunLoopModeExit) |
| 1218 | return -1; |
| 1219 | |
| 1220 | RNBRunLoopMode mode = start_mode; |
| 1221 | char err_str[1024] = {'\0'}; |
| 1222 | |
| 1223 | while (mode != eRNBRunLoopModeExit) |
| 1224 | { |
| 1225 | switch (mode) |
| 1226 | { |
| 1227 | case eRNBRunLoopModeGetStartModeFromRemoteProtocol: |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1228 | #ifdef WITH_LOCKDOWN |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1229 | if (g_lockdown_opt) |
| 1230 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1231 | if (!remote->Comm().IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1232 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1233 | if (remote->Comm().ConnectToService () != rnb_success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1234 | { |
| 1235 | RNBLogSTDERR ("Failed to get connection from a remote gdb process.\n"); |
| 1236 | mode = eRNBRunLoopModeExit; |
| 1237 | } |
| 1238 | else if (g_applist_opt != 0) |
| 1239 | { |
| 1240 | // List all applications we are able to see |
| 1241 | std::string applist_plist; |
| 1242 | if (ListApplications(applist_plist, false, false) == 0) |
| 1243 | { |
| 1244 | DNBLogDebug("Task list: %s", applist_plist.c_str()); |
| 1245 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1246 | remote->Comm().Write(applist_plist.c_str(), applist_plist.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1247 | // Issue a read that will never yield any data until the other side |
| 1248 | // closes the socket so this process doesn't just exit and cause the |
| 1249 | // socket to close prematurely on the other end and cause data loss. |
| 1250 | std::string buf; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1251 | remote->Comm().Read(buf); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1252 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1253 | remote->Comm().Disconnect(false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1254 | mode = eRNBRunLoopModeExit; |
| 1255 | break; |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | // Start watching for remote packets |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1260 | remote->StartReadRemoteDataThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | else |
| 1265 | #endif |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1266 | if (listen_port != INT32_MAX) |
| 1267 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 1268 | if (!StartListening (remote, listen_host.c_str(), listen_port, named_pipe_path.c_str())) |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1269 | mode = eRNBRunLoopModeExit; |
| 1270 | } |
| 1271 | else if (str[0] == '/') |
| 1272 | { |
| 1273 | if (remote->Comm().OpenFile (str)) |
| 1274 | mode = eRNBRunLoopModeExit; |
| 1275 | } |
| 1276 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1277 | if (mode != eRNBRunLoopModeExit) |
| 1278 | { |
| 1279 | RNBLogSTDOUT ("Got a connection, waiting for process information for launching or attaching.\n"); |
| 1280 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1281 | mode = RNBRunLoopGetStartModeFromRemote (remote); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1282 | } |
| 1283 | break; |
| 1284 | |
| 1285 | case eRNBRunLoopModeInferiorAttaching: |
| 1286 | if (!waitfor_pid_name.empty()) |
| 1287 | { |
| 1288 | // Set our end wait time if we are using a waitfor-duration |
| 1289 | // option that may have been specified |
| 1290 | struct timespec attach_timeout_abstime, *timeout_ptr = NULL; |
| 1291 | if (waitfor_duration != 0) |
| 1292 | { |
| 1293 | DNBTimer::OffsetTimeOfDay(&attach_timeout_abstime, waitfor_duration, 0); |
| 1294 | timeout_ptr = &attach_timeout_abstime; |
| 1295 | } |
| 1296 | nub_launch_flavor_t launch_flavor = g_launch_flavor; |
| 1297 | if (launch_flavor == eLaunchFlavorDefault) |
| 1298 | { |
| 1299 | // Our default launch method is posix spawn |
| 1300 | launch_flavor = eLaunchFlavorPosixSpawn; |
| 1301 | |
Jason Molenda | 42999a4 | 2012-02-22 02:18:59 +0000 | [diff] [blame] | 1302 | #ifdef WITH_SPRINGBOARD |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1303 | // Check if we have an app bundle, if so launch using SpringBoard. |
| 1304 | if (waitfor_pid_name.find (".app") != std::string::npos) |
| 1305 | { |
| 1306 | launch_flavor = eLaunchFlavorSpringBoard; |
| 1307 | } |
| 1308 | #endif |
| 1309 | } |
| 1310 | |
| 1311 | ctx.SetLaunchFlavor(launch_flavor); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1312 | bool ignore_existing = false; |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1313 | RNBLogSTDOUT ("Waiting to attach to process %s...\n", waitfor_pid_name.c_str()); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1314 | nub_process_t pid = DNBProcessAttachWait (waitfor_pid_name.c_str(), launch_flavor, ignore_existing, timeout_ptr, waitfor_interval, err_str, sizeof(err_str)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1315 | g_pid = pid; |
| 1316 | |
| 1317 | if (pid == INVALID_NUB_PROCESS) |
| 1318 | { |
| 1319 | ctx.LaunchStatus().SetError(-1, DNBError::Generic); |
| 1320 | if (err_str[0]) |
| 1321 | ctx.LaunchStatus().SetErrorString(err_str); |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1322 | RNBLogSTDERR ("error: failed to attach to process named: \"%s\" %s\n", waitfor_pid_name.c_str(), err_str); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1323 | mode = eRNBRunLoopModeExit; |
| 1324 | } |
| 1325 | else |
| 1326 | { |
| 1327 | ctx.SetProcessID(pid); |
| 1328 | mode = eRNBRunLoopModeInferiorExecuting; |
| 1329 | } |
| 1330 | } |
| 1331 | else if (attach_pid != INVALID_NUB_PROCESS) |
| 1332 | { |
| 1333 | |
| 1334 | RNBLogSTDOUT ("Attaching to process %i...\n", attach_pid); |
| 1335 | nub_process_t attached_pid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1336 | mode = RNBRunLoopLaunchAttaching (remote, attach_pid, attached_pid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1337 | if (mode != eRNBRunLoopModeInferiorExecuting) |
| 1338 | { |
| 1339 | const char *error_str = remote->Context().LaunchStatus().AsString(); |
| 1340 | RNBLogSTDERR ("error: failed to attach process %i: %s\n", attach_pid, error_str ? error_str : "unknown error."); |
| 1341 | mode = eRNBRunLoopModeExit; |
| 1342 | } |
| 1343 | } |
| 1344 | else if (!attach_pid_name.empty ()) |
| 1345 | { |
| 1346 | struct timespec attach_timeout_abstime, *timeout_ptr = NULL; |
| 1347 | if (waitfor_duration != 0) |
| 1348 | { |
| 1349 | DNBTimer::OffsetTimeOfDay(&attach_timeout_abstime, waitfor_duration, 0); |
| 1350 | timeout_ptr = &attach_timeout_abstime; |
| 1351 | } |
| 1352 | |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1353 | RNBLogSTDOUT ("Attaching to process %s...\n", attach_pid_name.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1354 | nub_process_t pid = DNBProcessAttachByName (attach_pid_name.c_str(), timeout_ptr, err_str, sizeof(err_str)); |
| 1355 | g_pid = pid; |
| 1356 | if (pid == INVALID_NUB_PROCESS) |
| 1357 | { |
| 1358 | ctx.LaunchStatus().SetError(-1, DNBError::Generic); |
| 1359 | if (err_str[0]) |
| 1360 | ctx.LaunchStatus().SetErrorString(err_str); |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1361 | RNBLogSTDERR ("error: failed to attach to process named: \"%s\" %s\n", waitfor_pid_name.c_str(), err_str); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1362 | mode = eRNBRunLoopModeExit; |
| 1363 | } |
| 1364 | else |
| 1365 | { |
| 1366 | ctx.SetProcessID(pid); |
| 1367 | mode = eRNBRunLoopModeInferiorExecuting; |
| 1368 | } |
| 1369 | |
| 1370 | } |
| 1371 | else |
| 1372 | { |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1373 | RNBLogSTDERR ("error: asked to attach with empty name and invalid PID.\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1374 | mode = eRNBRunLoopModeExit; |
| 1375 | } |
| 1376 | |
| 1377 | if (mode != eRNBRunLoopModeExit) |
| 1378 | { |
| 1379 | if (listen_port != INT32_MAX) |
| 1380 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 1381 | if (!StartListening (remote, listen_host.c_str(), listen_port, named_pipe_path.c_str())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1382 | mode = eRNBRunLoopModeExit; |
| 1383 | } |
| 1384 | else if (str[0] == '/') |
| 1385 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1386 | if (remote->Comm().OpenFile (str)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1387 | mode = eRNBRunLoopModeExit; |
| 1388 | } |
| 1389 | if (mode != eRNBRunLoopModeExit) |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1390 | RNBLogSTDOUT ("Waiting for debugger instructions for process %d.\n", attach_pid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1391 | } |
| 1392 | break; |
| 1393 | |
| 1394 | case eRNBRunLoopModeInferiorLaunching: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1395 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1396 | mode = RNBRunLoopLaunchInferior (remote, |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1397 | ctx.GetSTDINPath(), |
| 1398 | ctx.GetSTDOUTPath(), |
| 1399 | ctx.GetSTDERRPath(), |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1400 | no_stdio); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1401 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1402 | if (mode == eRNBRunLoopModeInferiorExecuting) |
| 1403 | { |
| 1404 | if (listen_port != INT32_MAX) |
| 1405 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 1406 | if (!StartListening (remote, listen_host.c_str(), listen_port, named_pipe_path.c_str())) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1407 | mode = eRNBRunLoopModeExit; |
| 1408 | } |
| 1409 | else if (str[0] == '/') |
| 1410 | { |
| 1411 | if (remote->Comm().OpenFile (str)) |
| 1412 | mode = eRNBRunLoopModeExit; |
| 1413 | } |
| 1414 | |
| 1415 | if (mode != eRNBRunLoopModeExit) |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1416 | RNBLogSTDOUT ("Got a connection, launched process %s.\n", argv_sub_zero); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1417 | } |
| 1418 | else |
| 1419 | { |
| 1420 | const char *error_str = remote->Context().LaunchStatus().AsString(); |
Jason Molenda | 0b2dbe0 | 2012-11-01 02:02:59 +0000 | [diff] [blame] | 1421 | RNBLogSTDERR ("error: failed to launch process %s: %s\n", argv_sub_zero, error_str ? error_str : "unknown error."); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1422 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1423 | } |
| 1424 | break; |
| 1425 | |
| 1426 | case eRNBRunLoopModeInferiorExecuting: |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1427 | mode = RNBRunLoopInferiorExecuting(remote); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1430 | case eRNBRunLoopModePlatformMode: |
| 1431 | if (listen_port != INT32_MAX) |
| 1432 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame^] | 1433 | if (!StartListening (remote, listen_host.c_str(), listen_port, named_pipe_path.c_str())) |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 1434 | mode = eRNBRunLoopModeExit; |
| 1435 | } |
| 1436 | else if (str[0] == '/') |
| 1437 | { |
| 1438 | if (remote->Comm().OpenFile (str)) |
| 1439 | mode = eRNBRunLoopModeExit; |
| 1440 | } |
| 1441 | |
| 1442 | if (mode != eRNBRunLoopModeExit) |
| 1443 | mode = RNBRunLoopPlatform (remote); |
| 1444 | break; |
| 1445 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1446 | default: |
| 1447 | mode = eRNBRunLoopModeExit; |
| 1448 | case eRNBRunLoopModeExit: |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1453 | remote->StopReadRemoteDataThread (); |
| 1454 | remote->Context().SetProcessID(INVALID_NUB_PROCESS); |
Jim Ingham | e2ff0ba | 2013-02-25 19:31:37 +0000 | [diff] [blame] | 1455 | RNBLogSTDOUT ("Exiting.\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1456 | |
| 1457 | return 0; |
| 1458 | } |