//===-- Launcher.cpp --------------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// //---------------------------------------------------------------------- // Darwin launch helper // // This program was written to allow programs to be launched in a new // Terminal.app window and have the application be stopped for debugging // at the program entry point. // // Although it uses posix_spawn(), it uses Darwin specific posix spawn // attribute flags to accomplish its task. It uses an "exec only" flag // which avoids forking this process, and it uses a "stop at entry" // flag to stop the program at the entry point. // // Since it uses darwin specific flags this code should not be compiled // on other systems. //---------------------------------------------------------------------- #if defined (__APPLE__) #include // for _NSGetEnviron() #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef _POSIX_SPAWN_DISABLE_ASLR #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 #endif #define streq(a,b) strcmp(a,b) == 0 static struct option g_long_options[] = { { "arch", required_argument, NULL, 'a' }, { "disable-aslr", no_argument, NULL, 'd' }, { "no-env", no_argument, NULL, 'e' }, { "help", no_argument, NULL, 'h' }, { "setsid", no_argument, NULL, 's' }, { "unix-socket", required_argument, NULL, 'u' }, { "working-dir", required_argument, NULL, 'w' }, { "env", required_argument, NULL, 'E' }, { NULL, 0, NULL, 0 } }; static void usage() { puts ( "NAME\n" " darwin-debug -- posix spawn a process that is stopped at the entry point\n" " for debugging.\n" "\n" "SYNOPSIS\n" " darwin-debug --unix-socket= [--arch=] [--working-dir=] [--disable-aslr] [--no-env] [--setsid] [--help] -- [ ....]\n" "\n" "DESCRIPTION\n" " darwin-debug will exec itself into a child process that is\n" " halted for debugging. It does this by using posix_spawn() along with\n" " darwin specific posix_spawn flags that allows exec only (no fork), and\n" " stop at the program entry point. Any program arguments are\n" " passed on to the exec as the arguments for the new process. The current\n" " environment will be passed to the new process unless the \"--no-env\"\n" " option is used. A unix socket must be supplied using the\n" " --unix-socket= option so the calling program can handshake with\n" " this process and get its process id.\n" "\n" "EXAMPLE\n" " darwin-debug --arch=i386 -- /bin/ls -al /tmp\n" ); exit (1); } static void exit_with_errno (int err, const char *prefix) { if (err) { fprintf (stderr, "%s%s", prefix ? prefix : "", strerror(err)); exit (err); } } pid_t posix_spawn_for_debug ( char *const *argv, char *const *envp, const char *working_dir, cpu_type_t cpu_type, int disable_aslr) { pid_t pid = 0; const char *path = argv[0]; posix_spawnattr_t attr; exit_with_errno (::posix_spawnattr_init (&attr), "::posix_spawnattr_init (&attr) error: "); // Here we are using a darwin specific feature that allows us to exec only // since we want this program to turn into the program we want to debug, // and also have the new program start suspended (right at __dyld_start) // so we can debug it short flags = POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETEXEC | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK; // Disable ASLR if we were asked to if (disable_aslr) flags |= _POSIX_SPAWN_DISABLE_ASLR; sigset_t no_signals; sigset_t all_signals; sigemptyset (&no_signals); sigfillset (&all_signals); ::posix_spawnattr_setsigmask(&attr, &no_signals); ::posix_spawnattr_setsigdefault(&attr, &all_signals); // Set the flags we just made into our posix spawn attributes exit_with_errno (::posix_spawnattr_setflags (&attr, flags), "::posix_spawnattr_setflags (&attr, flags) error: "); // Another darwin specific thing here where we can select the architecture // of the binary we want to re-exec as. if (cpu_type != 0) { size_t ocount = 0; exit_with_errno (::posix_spawnattr_setbinpref_np (&attr, 1, &cpu_type, &ocount), "posix_spawnattr_setbinpref_np () error: "); } // I wish there was a posix_spawn flag to change the working directory of // the inferior process we will spawn, but there currently isn't. If there // ever is a better way to do this, we should use it. I would rather not // manually fork, chdir in the child process, and then posix_spawn with exec // as the whole reason for doing posix_spawn is to not hose anything up // after the fork and prior to the exec... if (working_dir) ::chdir (working_dir); exit_with_errno (::posix_spawnp (&pid, path, NULL, &attr, (char * const*)argv, (char * const*)envp), "posix_spawn() error: "); // This code will only be reached if the posix_spawn exec failed... ::posix_spawnattr_destroy (&attr); return pid; } int main (int argc, char *const *argv, char *const *envp, const char **apple) { #if defined (DEBUG_LLDB_LAUNCHER) const char *program_name = strrchr(apple[0], '/'); if (program_name) program_name++; // Skip the last slash.. else program_name = apple[0]; printf("%s called with:\n", program_name); for (int i=0; i