15796c8dcSSimon Schubert /* Fork a Unix child process, and set up to debug it, for GDB. 25796c8dcSSimon Schubert 3*a45ae5f8SJohn Marino Copyright (C) 1990-1996, 1998-2001, 2004-2012 Free Software 4*a45ae5f8SJohn Marino Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert Contributed by Cygnus Support. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This file is part of GDB. 95796c8dcSSimon Schubert 105796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 115796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 125796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 135796c8dcSSimon Schubert (at your option) any later version. 145796c8dcSSimon Schubert 155796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 165796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 175796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 185796c8dcSSimon Schubert GNU General Public License for more details. 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 215796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 225796c8dcSSimon Schubert 235796c8dcSSimon Schubert #include "defs.h" 245796c8dcSSimon Schubert #include "gdb_string.h" 255796c8dcSSimon Schubert #include "inferior.h" 265796c8dcSSimon Schubert #include "terminal.h" 275796c8dcSSimon Schubert #include "target.h" 285796c8dcSSimon Schubert #include "gdb_wait.h" 295796c8dcSSimon Schubert #include "gdb_vfork.h" 305796c8dcSSimon Schubert #include "gdbcore.h" 315796c8dcSSimon Schubert #include "terminal.h" 325796c8dcSSimon Schubert #include "gdbthread.h" 335796c8dcSSimon Schubert #include "command.h" /* for dont_repeat () */ 345796c8dcSSimon Schubert #include "gdbcmd.h" 355796c8dcSSimon Schubert #include "solib.h" 365796c8dcSSimon Schubert 375796c8dcSSimon Schubert #include <signal.h> 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert /* This just gets used as a default if we can't find SHELL. */ 405796c8dcSSimon Schubert #define SHELL_FILE "/bin/sh" 415796c8dcSSimon Schubert 425796c8dcSSimon Schubert extern char **environ; 435796c8dcSSimon Schubert 445796c8dcSSimon Schubert static char *exec_wrapper; 455796c8dcSSimon Schubert 465796c8dcSSimon Schubert /* Break up SCRATCH into an argument vector suitable for passing to 475796c8dcSSimon Schubert execvp and store it in ARGV. E.g., on "run a b c d" this routine 485796c8dcSSimon Schubert would get as input the string "a b c d", and as output it would 495796c8dcSSimon Schubert fill in ARGV with the four arguments "a", "b", "c", "d". */ 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert static void 525796c8dcSSimon Schubert breakup_args (char *scratch, char **argv) 535796c8dcSSimon Schubert { 54c50c785cSJohn Marino char *cp = scratch, *tmp; 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert for (;;) 575796c8dcSSimon Schubert { 585796c8dcSSimon Schubert /* Scan past leading separators */ 595796c8dcSSimon Schubert while (*cp == ' ' || *cp == '\t' || *cp == '\n') 605796c8dcSSimon Schubert cp++; 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert /* Break if at end of string. */ 635796c8dcSSimon Schubert if (*cp == '\0') 645796c8dcSSimon Schubert break; 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert /* Take an arg. */ 675796c8dcSSimon Schubert *argv++ = cp; 685796c8dcSSimon Schubert 695796c8dcSSimon Schubert /* Scan for next arg separator. */ 70c50c785cSJohn Marino tmp = strchr (cp, ' '); 71c50c785cSJohn Marino if (tmp == NULL) 72c50c785cSJohn Marino tmp = strchr (cp, '\t'); 73c50c785cSJohn Marino if (tmp == NULL) 74c50c785cSJohn Marino tmp = strchr (cp, '\n'); 755796c8dcSSimon Schubert 765796c8dcSSimon Schubert /* No separators => end of string => break. */ 77c50c785cSJohn Marino if (tmp == NULL) 785796c8dcSSimon Schubert break; 79c50c785cSJohn Marino cp = tmp; 805796c8dcSSimon Schubert 815796c8dcSSimon Schubert /* Replace the separator with a terminator. */ 825796c8dcSSimon Schubert *cp++ = '\0'; 835796c8dcSSimon Schubert } 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* Null-terminate the vector. */ 865796c8dcSSimon Schubert *argv = NULL; 875796c8dcSSimon Schubert } 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert /* When executing a command under the given shell, return non-zero if 905796c8dcSSimon Schubert the '!' character should be escaped when embedded in a quoted 915796c8dcSSimon Schubert command-line argument. */ 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert static int 945796c8dcSSimon Schubert escape_bang_in_quoted_argument (const char *shell_file) 955796c8dcSSimon Schubert { 965796c8dcSSimon Schubert const int shell_file_len = strlen (shell_file); 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert /* Bang should be escaped only in C Shells. For now, simply check 995796c8dcSSimon Schubert that the shell name ends with 'csh', which covers at least csh 1005796c8dcSSimon Schubert and tcsh. This should be good enough for now. */ 1015796c8dcSSimon Schubert 1025796c8dcSSimon Schubert if (shell_file_len < 3) 1035796c8dcSSimon Schubert return 0; 1045796c8dcSSimon Schubert 1055796c8dcSSimon Schubert if (shell_file[shell_file_len - 3] == 'c' 1065796c8dcSSimon Schubert && shell_file[shell_file_len - 2] == 's' 1075796c8dcSSimon Schubert && shell_file[shell_file_len - 1] == 'h') 1085796c8dcSSimon Schubert return 1; 1095796c8dcSSimon Schubert 1105796c8dcSSimon Schubert return 0; 1115796c8dcSSimon Schubert } 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert /* Start an inferior Unix child process and sets inferior_ptid to its 1145796c8dcSSimon Schubert pid. EXEC_FILE is the file to run. ALLARGS is a string containing 1155796c8dcSSimon Schubert the arguments to the program. ENV is the environment vector to 1165796c8dcSSimon Schubert pass. SHELL_FILE is the shell file, or NULL if we should pick 117*a45ae5f8SJohn Marino one. EXEC_FUN is the exec(2) function to use, or NULL for the default 1185796c8dcSSimon Schubert one. */ 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert /* This function is NOT reentrant. Some of the variables have been 1215796c8dcSSimon Schubert made static to ensure that they survive the vfork call. */ 1225796c8dcSSimon Schubert 1235796c8dcSSimon Schubert int 1245796c8dcSSimon Schubert fork_inferior (char *exec_file_arg, char *allargs, char **env, 1255796c8dcSSimon Schubert void (*traceme_fun) (void), void (*init_trace_fun) (int), 126*a45ae5f8SJohn Marino void (*pre_trace_fun) (void), char *shell_file_arg, 127*a45ae5f8SJohn Marino void (*exec_fun)(const char *file, char * const *argv, 128*a45ae5f8SJohn Marino char * const *env)) 1295796c8dcSSimon Schubert { 1305796c8dcSSimon Schubert int pid; 1315796c8dcSSimon Schubert static char default_shell_file[] = SHELL_FILE; 1325796c8dcSSimon Schubert /* Set debug_fork then attach to the child while it sleeps, to debug. */ 1335796c8dcSSimon Schubert static int debug_fork = 0; 1345796c8dcSSimon Schubert /* This is set to the result of setpgrp, which if vforked, will be visible 1355796c8dcSSimon Schubert to you in the parent process. It's only used by humans for debugging. */ 1365796c8dcSSimon Schubert static int debug_setpgrp = 657473; 1375796c8dcSSimon Schubert static char *shell_file; 1385796c8dcSSimon Schubert static char *exec_file; 1395796c8dcSSimon Schubert char **save_our_env; 1405796c8dcSSimon Schubert int shell = 0; 1415796c8dcSSimon Schubert static char **argv; 1425796c8dcSSimon Schubert const char *inferior_io_terminal = get_inferior_io_terminal (); 143cf7f2e2dSJohn Marino struct inferior *inf; 144*a45ae5f8SJohn Marino int i; 145*a45ae5f8SJohn Marino int save_errno; 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert /* If no exec file handed to us, get it from the exec-file command 1485796c8dcSSimon Schubert -- with a good, common error message if none is specified. */ 1495796c8dcSSimon Schubert exec_file = exec_file_arg; 1505796c8dcSSimon Schubert if (exec_file == 0) 1515796c8dcSSimon Schubert exec_file = get_exec_file (1); 1525796c8dcSSimon Schubert 1535796c8dcSSimon Schubert /* STARTUP_WITH_SHELL is defined in inferior.h. If 0,e we'll just 1545796c8dcSSimon Schubert do a fork/exec, no shell, so don't bother figuring out what 1555796c8dcSSimon Schubert shell. */ 1565796c8dcSSimon Schubert shell_file = shell_file_arg; 1575796c8dcSSimon Schubert if (STARTUP_WITH_SHELL) 1585796c8dcSSimon Schubert { 1595796c8dcSSimon Schubert /* Figure out what shell to start up the user program under. */ 1605796c8dcSSimon Schubert if (shell_file == NULL) 1615796c8dcSSimon Schubert shell_file = getenv ("SHELL"); 1625796c8dcSSimon Schubert if (shell_file == NULL) 1635796c8dcSSimon Schubert shell_file = default_shell_file; 1645796c8dcSSimon Schubert shell = 1; 1655796c8dcSSimon Schubert } 1665796c8dcSSimon Schubert 167*a45ae5f8SJohn Marino if (!shell) 168*a45ae5f8SJohn Marino { 169*a45ae5f8SJohn Marino /* We're going to call execvp. Create argument vector. 170*a45ae5f8SJohn Marino Calculate an upper bound on the length of the vector by 171*a45ae5f8SJohn Marino assuming that every other character is a separate 172*a45ae5f8SJohn Marino argument. */ 173*a45ae5f8SJohn Marino int argc = (strlen (allargs) + 1) / 2 + 2; 174*a45ae5f8SJohn Marino 175*a45ae5f8SJohn Marino argv = (char **) alloca (argc * sizeof (*argv)); 176*a45ae5f8SJohn Marino argv[0] = exec_file; 177*a45ae5f8SJohn Marino breakup_args (allargs, &argv[1]); 178*a45ae5f8SJohn Marino } 179*a45ae5f8SJohn Marino else 180*a45ae5f8SJohn Marino { 181*a45ae5f8SJohn Marino /* We're going to call a shell. */ 182*a45ae5f8SJohn Marino char *shell_command; 183*a45ae5f8SJohn Marino int len; 184*a45ae5f8SJohn Marino char *p; 185*a45ae5f8SJohn Marino int need_to_quote; 186*a45ae5f8SJohn Marino const int escape_bang = escape_bang_in_quoted_argument (shell_file); 187*a45ae5f8SJohn Marino 1885796c8dcSSimon Schubert /* Multiplying the length of exec_file by 4 is to account for the 1895796c8dcSSimon Schubert fact that it may expand when quoted; it is a worst-case number 1905796c8dcSSimon Schubert based on every character being '. */ 1915796c8dcSSimon Schubert len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12; 1925796c8dcSSimon Schubert if (exec_wrapper) 1935796c8dcSSimon Schubert len += strlen (exec_wrapper) + 1; 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert shell_command = (char *) alloca (len); 1965796c8dcSSimon Schubert shell_command[0] = '\0'; 1975796c8dcSSimon Schubert 1985796c8dcSSimon Schubert strcat (shell_command, "exec "); 1995796c8dcSSimon Schubert 2005796c8dcSSimon Schubert /* Add any exec wrapper. That may be a program name with arguments, so 2015796c8dcSSimon Schubert the user must handle quoting. */ 2025796c8dcSSimon Schubert if (exec_wrapper) 2035796c8dcSSimon Schubert { 2045796c8dcSSimon Schubert strcat (shell_command, exec_wrapper); 2055796c8dcSSimon Schubert strcat (shell_command, " "); 2065796c8dcSSimon Schubert } 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert /* Now add exec_file, quoting as necessary. */ 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert /* Quoting in this style is said to work with all shells. But 2115796c8dcSSimon Schubert csh on IRIX 4.0.1 can't deal with it. So we only quote it if 2125796c8dcSSimon Schubert we need to. */ 2135796c8dcSSimon Schubert p = exec_file; 2145796c8dcSSimon Schubert while (1) 2155796c8dcSSimon Schubert { 2165796c8dcSSimon Schubert switch (*p) 2175796c8dcSSimon Schubert { 2185796c8dcSSimon Schubert case '\'': 2195796c8dcSSimon Schubert case '!': 2205796c8dcSSimon Schubert case '"': 2215796c8dcSSimon Schubert case '(': 2225796c8dcSSimon Schubert case ')': 2235796c8dcSSimon Schubert case '$': 2245796c8dcSSimon Schubert case '&': 2255796c8dcSSimon Schubert case ';': 2265796c8dcSSimon Schubert case '<': 2275796c8dcSSimon Schubert case '>': 2285796c8dcSSimon Schubert case ' ': 2295796c8dcSSimon Schubert case '\n': 2305796c8dcSSimon Schubert case '\t': 2315796c8dcSSimon Schubert need_to_quote = 1; 2325796c8dcSSimon Schubert goto end_scan; 2335796c8dcSSimon Schubert 2345796c8dcSSimon Schubert case '\0': 2355796c8dcSSimon Schubert need_to_quote = 0; 2365796c8dcSSimon Schubert goto end_scan; 2375796c8dcSSimon Schubert 2385796c8dcSSimon Schubert default: 2395796c8dcSSimon Schubert break; 2405796c8dcSSimon Schubert } 2415796c8dcSSimon Schubert ++p; 2425796c8dcSSimon Schubert } 2435796c8dcSSimon Schubert end_scan: 2445796c8dcSSimon Schubert if (need_to_quote) 2455796c8dcSSimon Schubert { 2465796c8dcSSimon Schubert strcat (shell_command, "'"); 2475796c8dcSSimon Schubert for (p = exec_file; *p != '\0'; ++p) 2485796c8dcSSimon Schubert { 2495796c8dcSSimon Schubert if (*p == '\'') 2505796c8dcSSimon Schubert strcat (shell_command, "'\\''"); 2515796c8dcSSimon Schubert else if (*p == '!' && escape_bang) 2525796c8dcSSimon Schubert strcat (shell_command, "\\!"); 2535796c8dcSSimon Schubert else 2545796c8dcSSimon Schubert strncat (shell_command, p, 1); 2555796c8dcSSimon Schubert } 2565796c8dcSSimon Schubert strcat (shell_command, "'"); 2575796c8dcSSimon Schubert } 2585796c8dcSSimon Schubert else 2595796c8dcSSimon Schubert strcat (shell_command, exec_file); 2605796c8dcSSimon Schubert 2615796c8dcSSimon Schubert strcat (shell_command, " "); 2625796c8dcSSimon Schubert strcat (shell_command, allargs); 263*a45ae5f8SJohn Marino 264*a45ae5f8SJohn Marino /* If we decided above to start up with a shell, we exec the 265*a45ae5f8SJohn Marino shell, "-c" says to interpret the next arg as a shell command 266*a45ae5f8SJohn Marino to execute, and this command is "exec <target-program> 267*a45ae5f8SJohn Marino <args>". */ 268*a45ae5f8SJohn Marino argv = (char **) alloca (4 * sizeof (char *)); 269*a45ae5f8SJohn Marino argv[0] = shell_file; 270*a45ae5f8SJohn Marino argv[1] = "-c"; 271*a45ae5f8SJohn Marino argv[2] = shell_command; 272*a45ae5f8SJohn Marino argv[3] = (char *) 0; 2735796c8dcSSimon Schubert } 2745796c8dcSSimon Schubert 2755796c8dcSSimon Schubert /* On some systems an exec will fail if the executable is open. */ 2765796c8dcSSimon Schubert close_exec_file (); 2775796c8dcSSimon Schubert 2785796c8dcSSimon Schubert /* Retain a copy of our environment variables, since the child will 2795796c8dcSSimon Schubert replace the value of environ and if we're vforked, we have to 2805796c8dcSSimon Schubert restore it. */ 2815796c8dcSSimon Schubert save_our_env = environ; 2825796c8dcSSimon Schubert 2835796c8dcSSimon Schubert /* Tell the terminal handling subsystem what tty we plan to run on; 2845796c8dcSSimon Schubert it will just record the information for later. */ 2855796c8dcSSimon Schubert new_tty_prefork (inferior_io_terminal); 2865796c8dcSSimon Schubert 2875796c8dcSSimon Schubert /* It is generally good practice to flush any possible pending stdio 2885796c8dcSSimon Schubert output prior to doing a fork, to avoid the possibility of both 2895796c8dcSSimon Schubert the parent and child flushing the same data after the fork. */ 2905796c8dcSSimon Schubert gdb_flush (gdb_stdout); 2915796c8dcSSimon Schubert gdb_flush (gdb_stderr); 2925796c8dcSSimon Schubert 2935796c8dcSSimon Schubert /* If there's any initialization of the target layers that must 2945796c8dcSSimon Schubert happen to prepare to handle the child we're about fork, do it 2955796c8dcSSimon Schubert now... */ 2965796c8dcSSimon Schubert if (pre_trace_fun != NULL) 2975796c8dcSSimon Schubert (*pre_trace_fun) (); 2985796c8dcSSimon Schubert 2995796c8dcSSimon Schubert /* Create the child process. Since the child process is going to 3005796c8dcSSimon Schubert exec(3) shortly afterwards, try to reduce the overhead by 3015796c8dcSSimon Schubert calling vfork(2). However, if PRE_TRACE_FUN is non-null, it's 3025796c8dcSSimon Schubert likely that this optimization won't work since there's too much 3035796c8dcSSimon Schubert work to do between the vfork(2) and the exec(3). This is known 3045796c8dcSSimon Schubert to be the case on ttrace(2)-based HP-UX, where some handshaking 3055796c8dcSSimon Schubert between parent and child needs to happen between fork(2) and 3065796c8dcSSimon Schubert exec(2). However, since the parent is suspended in the vforked 3075796c8dcSSimon Schubert state, this doesn't work. Also note that the vfork(2) call might 3085796c8dcSSimon Schubert actually be a call to fork(2) due to the fact that autoconf will 3095796c8dcSSimon Schubert ``#define vfork fork'' on certain platforms. */ 3105796c8dcSSimon Schubert if (pre_trace_fun || debug_fork) 3115796c8dcSSimon Schubert pid = fork (); 3125796c8dcSSimon Schubert else 3135796c8dcSSimon Schubert pid = vfork (); 3145796c8dcSSimon Schubert 3155796c8dcSSimon Schubert if (pid < 0) 3165796c8dcSSimon Schubert perror_with_name (("vfork")); 3175796c8dcSSimon Schubert 3185796c8dcSSimon Schubert if (pid == 0) 3195796c8dcSSimon Schubert { 3205796c8dcSSimon Schubert if (debug_fork) 3215796c8dcSSimon Schubert sleep (debug_fork); 3225796c8dcSSimon Schubert 3235796c8dcSSimon Schubert /* Create a new session for the inferior process, if necessary. 3245796c8dcSSimon Schubert It will also place the inferior in a separate process group. */ 3255796c8dcSSimon Schubert if (create_tty_session () <= 0) 3265796c8dcSSimon Schubert { 3275796c8dcSSimon Schubert /* No session was created, but we still want to run the inferior 3285796c8dcSSimon Schubert in a separate process group. */ 3295796c8dcSSimon Schubert debug_setpgrp = gdb_setpgid (); 3305796c8dcSSimon Schubert if (debug_setpgrp == -1) 331c50c785cSJohn Marino perror (_("setpgrp failed in child")); 3325796c8dcSSimon Schubert } 3335796c8dcSSimon Schubert 3345796c8dcSSimon Schubert /* Ask the tty subsystem to switch to the one we specified 3355796c8dcSSimon Schubert earlier (or to share the current terminal, if none was 3365796c8dcSSimon Schubert specified). */ 3375796c8dcSSimon Schubert new_tty (); 3385796c8dcSSimon Schubert 3395796c8dcSSimon Schubert /* Changing the signal handlers for the inferior after 3405796c8dcSSimon Schubert a vfork can also change them for the superior, so we don't mess 3415796c8dcSSimon Schubert with signals here. See comments in 3425796c8dcSSimon Schubert initialize_signals for how we get the right signal handlers 3435796c8dcSSimon Schubert for the inferior. */ 3445796c8dcSSimon Schubert 3455796c8dcSSimon Schubert /* "Trace me, Dr. Memory!" */ 3465796c8dcSSimon Schubert (*traceme_fun) (); 3475796c8dcSSimon Schubert 3485796c8dcSSimon Schubert /* The call above set this process (the "child") as debuggable 3495796c8dcSSimon Schubert by the original gdb process (the "parent"). Since processes 3505796c8dcSSimon Schubert (unlike people) can have only one parent, if you are debugging 3515796c8dcSSimon Schubert gdb itself (and your debugger is thus _already_ the 3525796c8dcSSimon Schubert controller/parent for this child), code from here on out is 3535796c8dcSSimon Schubert undebuggable. Indeed, you probably got an error message 3545796c8dcSSimon Schubert saying "not parent". Sorry; you'll have to use print 3555796c8dcSSimon Schubert statements! */ 3565796c8dcSSimon Schubert 3575796c8dcSSimon Schubert /* There is no execlpe call, so we have to set the environment 3585796c8dcSSimon Schubert for our child in the global variable. If we've vforked, this 3595796c8dcSSimon Schubert clobbers the parent, but environ is restored a few lines down 3605796c8dcSSimon Schubert in the parent. By the way, yes we do need to look down the 3615796c8dcSSimon Schubert path to find $SHELL. Rich Pixley says so, and I agree. */ 3625796c8dcSSimon Schubert environ = env; 3635796c8dcSSimon Schubert 364*a45ae5f8SJohn Marino if (exec_fun != NULL) 365*a45ae5f8SJohn Marino (*exec_fun) (argv[0], argv, env); 3665796c8dcSSimon Schubert else 367*a45ae5f8SJohn Marino execvp (argv[0], argv); 3685796c8dcSSimon Schubert 3695796c8dcSSimon Schubert /* If we get here, it's an error. */ 370*a45ae5f8SJohn Marino save_errno = errno; 3715796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "Cannot exec %s", exec_file); 372*a45ae5f8SJohn Marino for (i = 1; argv[i] != NULL; i++) 3735796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, " %s", argv[i]); 3745796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, ".\n"); 375*a45ae5f8SJohn Marino fprintf_unfiltered (gdb_stderr, "Error: %s\n", 376*a45ae5f8SJohn Marino safe_strerror (save_errno)); 3775796c8dcSSimon Schubert gdb_flush (gdb_stderr); 3785796c8dcSSimon Schubert _exit (0177); 3795796c8dcSSimon Schubert } 3805796c8dcSSimon Schubert 3815796c8dcSSimon Schubert /* Restore our environment in case a vforked child clob'd it. */ 3825796c8dcSSimon Schubert environ = save_our_env; 3835796c8dcSSimon Schubert 3845796c8dcSSimon Schubert if (!have_inferiors ()) 3855796c8dcSSimon Schubert init_thread_list (); 3865796c8dcSSimon Schubert 387cf7f2e2dSJohn Marino inf = current_inferior (); 388cf7f2e2dSJohn Marino 389cf7f2e2dSJohn Marino inferior_appeared (inf, pid); 3905796c8dcSSimon Schubert 3915796c8dcSSimon Schubert /* Needed for wait_for_inferior stuff below. */ 3925796c8dcSSimon Schubert inferior_ptid = pid_to_ptid (pid); 3935796c8dcSSimon Schubert 3945796c8dcSSimon Schubert new_tty_postfork (); 3955796c8dcSSimon Schubert 3965796c8dcSSimon Schubert /* We have something that executes now. We'll be running through 3975796c8dcSSimon Schubert the shell at this point, but the pid shouldn't change. Targets 3985796c8dcSSimon Schubert supporting MT should fill this task's ptid with more data as soon 3995796c8dcSSimon Schubert as they can. */ 4005796c8dcSSimon Schubert add_thread_silent (inferior_ptid); 4015796c8dcSSimon Schubert 4025796c8dcSSimon Schubert /* Now that we have a child process, make it our target, and 4035796c8dcSSimon Schubert initialize anything target-vector-specific that needs 4045796c8dcSSimon Schubert initializing. */ 4055796c8dcSSimon Schubert if (init_trace_fun) 4065796c8dcSSimon Schubert (*init_trace_fun) (pid); 4075796c8dcSSimon Schubert 4085796c8dcSSimon Schubert /* We are now in the child process of interest, having exec'd the 4095796c8dcSSimon Schubert correct program, and are poised at the first instruction of the 4105796c8dcSSimon Schubert new program. */ 4115796c8dcSSimon Schubert return pid; 4125796c8dcSSimon Schubert } 4135796c8dcSSimon Schubert 4145796c8dcSSimon Schubert /* Accept NTRAPS traps from the inferior. */ 4155796c8dcSSimon Schubert 4165796c8dcSSimon Schubert void 4175796c8dcSSimon Schubert startup_inferior (int ntraps) 4185796c8dcSSimon Schubert { 4195796c8dcSSimon Schubert int pending_execs = ntraps; 4205796c8dcSSimon Schubert int terminal_initted = 0; 4215796c8dcSSimon Schubert ptid_t resume_ptid; 4225796c8dcSSimon Schubert 4235796c8dcSSimon Schubert if (target_supports_multi_process ()) 4245796c8dcSSimon Schubert resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid)); 4255796c8dcSSimon Schubert else 4265796c8dcSSimon Schubert resume_ptid = minus_one_ptid; 4275796c8dcSSimon Schubert 4285796c8dcSSimon Schubert /* The process was started by the fork that created it, but it will 4295796c8dcSSimon Schubert have stopped one instruction after execing the shell. Here we 4305796c8dcSSimon Schubert must get it up to actual execution of the real program. */ 4315796c8dcSSimon Schubert 4325796c8dcSSimon Schubert if (exec_wrapper) 4335796c8dcSSimon Schubert pending_execs++; 4345796c8dcSSimon Schubert 4355796c8dcSSimon Schubert while (1) 4365796c8dcSSimon Schubert { 437c50c785cSJohn Marino enum target_signal resume_signal = TARGET_SIGNAL_0; 4385796c8dcSSimon Schubert ptid_t event_ptid; 4395796c8dcSSimon Schubert 4405796c8dcSSimon Schubert struct target_waitstatus ws; 4415796c8dcSSimon Schubert memset (&ws, 0, sizeof (ws)); 4425796c8dcSSimon Schubert event_ptid = target_wait (resume_ptid, &ws, 0); 4435796c8dcSSimon Schubert 4445796c8dcSSimon Schubert if (ws.kind == TARGET_WAITKIND_IGNORE) 4455796c8dcSSimon Schubert /* The inferior didn't really stop, keep waiting. */ 4465796c8dcSSimon Schubert continue; 4475796c8dcSSimon Schubert 4485796c8dcSSimon Schubert switch (ws.kind) 4495796c8dcSSimon Schubert { 4505796c8dcSSimon Schubert case TARGET_WAITKIND_SPURIOUS: 4515796c8dcSSimon Schubert case TARGET_WAITKIND_LOADED: 4525796c8dcSSimon Schubert case TARGET_WAITKIND_FORKED: 4535796c8dcSSimon Schubert case TARGET_WAITKIND_VFORKED: 4545796c8dcSSimon Schubert case TARGET_WAITKIND_SYSCALL_ENTRY: 4555796c8dcSSimon Schubert case TARGET_WAITKIND_SYSCALL_RETURN: 4565796c8dcSSimon Schubert /* Ignore gracefully during startup of the inferior. */ 4575796c8dcSSimon Schubert switch_to_thread (event_ptid); 4585796c8dcSSimon Schubert break; 4595796c8dcSSimon Schubert 4605796c8dcSSimon Schubert case TARGET_WAITKIND_SIGNALLED: 4615796c8dcSSimon Schubert target_terminal_ours (); 4625796c8dcSSimon Schubert target_mourn_inferior (); 4635796c8dcSSimon Schubert error (_("During startup program terminated with signal %s, %s."), 4645796c8dcSSimon Schubert target_signal_to_name (ws.value.sig), 4655796c8dcSSimon Schubert target_signal_to_string (ws.value.sig)); 4665796c8dcSSimon Schubert return; 4675796c8dcSSimon Schubert 4685796c8dcSSimon Schubert case TARGET_WAITKIND_EXITED: 4695796c8dcSSimon Schubert target_terminal_ours (); 4705796c8dcSSimon Schubert target_mourn_inferior (); 4715796c8dcSSimon Schubert if (ws.value.integer) 4725796c8dcSSimon Schubert error (_("During startup program exited with code %d."), 4735796c8dcSSimon Schubert ws.value.integer); 4745796c8dcSSimon Schubert else 4755796c8dcSSimon Schubert error (_("During startup program exited normally.")); 4765796c8dcSSimon Schubert return; 4775796c8dcSSimon Schubert 4785796c8dcSSimon Schubert case TARGET_WAITKIND_EXECD: 4795796c8dcSSimon Schubert /* Handle EXEC signals as if they were SIGTRAP signals. */ 4805796c8dcSSimon Schubert xfree (ws.value.execd_pathname); 4815796c8dcSSimon Schubert resume_signal = TARGET_SIGNAL_TRAP; 4825796c8dcSSimon Schubert switch_to_thread (event_ptid); 4835796c8dcSSimon Schubert break; 4845796c8dcSSimon Schubert 4855796c8dcSSimon Schubert case TARGET_WAITKIND_STOPPED: 4865796c8dcSSimon Schubert resume_signal = ws.value.sig; 4875796c8dcSSimon Schubert switch_to_thread (event_ptid); 4885796c8dcSSimon Schubert break; 4895796c8dcSSimon Schubert } 4905796c8dcSSimon Schubert 4915796c8dcSSimon Schubert if (resume_signal != TARGET_SIGNAL_TRAP) 4925796c8dcSSimon Schubert { 4935796c8dcSSimon Schubert /* Let shell child handle its own signals in its own way. */ 4945796c8dcSSimon Schubert target_resume (resume_ptid, 0, resume_signal); 4955796c8dcSSimon Schubert } 4965796c8dcSSimon Schubert else 4975796c8dcSSimon Schubert { 4985796c8dcSSimon Schubert /* We handle SIGTRAP, however; it means child did an exec. */ 4995796c8dcSSimon Schubert if (!terminal_initted) 5005796c8dcSSimon Schubert { 5015796c8dcSSimon Schubert /* Now that the child has exec'd we know it has already 5025796c8dcSSimon Schubert set its process group. On POSIX systems, tcsetpgrp 5035796c8dcSSimon Schubert will fail with EPERM if we try it before the child's 5045796c8dcSSimon Schubert setpgid. */ 5055796c8dcSSimon Schubert 5065796c8dcSSimon Schubert /* Set up the "saved terminal modes" of the inferior 5075796c8dcSSimon Schubert based on what modes we are starting it with. */ 5085796c8dcSSimon Schubert target_terminal_init (); 5095796c8dcSSimon Schubert 5105796c8dcSSimon Schubert /* Install inferior's terminal modes. */ 5115796c8dcSSimon Schubert target_terminal_inferior (); 5125796c8dcSSimon Schubert 5135796c8dcSSimon Schubert terminal_initted = 1; 5145796c8dcSSimon Schubert } 5155796c8dcSSimon Schubert 5165796c8dcSSimon Schubert if (--pending_execs == 0) 5175796c8dcSSimon Schubert break; 5185796c8dcSSimon Schubert 5195796c8dcSSimon Schubert /* Just make it go on. */ 5205796c8dcSSimon Schubert target_resume (resume_ptid, 0, TARGET_SIGNAL_0); 5215796c8dcSSimon Schubert } 5225796c8dcSSimon Schubert } 5235796c8dcSSimon Schubert 5245796c8dcSSimon Schubert /* Mark all threads non-executing. */ 5255796c8dcSSimon Schubert set_executing (resume_ptid, 0); 5265796c8dcSSimon Schubert } 5275796c8dcSSimon Schubert 5285796c8dcSSimon Schubert /* Implement the "unset exec-wrapper" command. */ 5295796c8dcSSimon Schubert 5305796c8dcSSimon Schubert static void 5315796c8dcSSimon Schubert unset_exec_wrapper_command (char *args, int from_tty) 5325796c8dcSSimon Schubert { 5335796c8dcSSimon Schubert xfree (exec_wrapper); 5345796c8dcSSimon Schubert exec_wrapper = NULL; 5355796c8dcSSimon Schubert } 5365796c8dcSSimon Schubert 5375796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes. */ 5385796c8dcSSimon Schubert extern initialize_file_ftype _initialize_fork_child; 5395796c8dcSSimon Schubert 5405796c8dcSSimon Schubert void 5415796c8dcSSimon Schubert _initialize_fork_child (void) 5425796c8dcSSimon Schubert { 5435796c8dcSSimon Schubert add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\ 5445796c8dcSSimon Schubert Set a wrapper for running programs.\n\ 5455796c8dcSSimon Schubert The wrapper prepares the system and environment for the new program."), 5465796c8dcSSimon Schubert _("\ 5475796c8dcSSimon Schubert Show the wrapper for running programs."), NULL, 5485796c8dcSSimon Schubert NULL, NULL, 5495796c8dcSSimon Schubert &setlist, &showlist); 5505796c8dcSSimon Schubert 5515796c8dcSSimon Schubert add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command, 5525796c8dcSSimon Schubert _("Disable use of an execution wrapper."), 5535796c8dcSSimon Schubert &unsetlist); 5545796c8dcSSimon Schubert } 555