119370Spst/* Fork a Unix child process, and set up to debug it, for GDB.
298944Sobrien   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
398944Sobrien   2001 Free Software Foundation, Inc.
419370Spst   Contributed by Cygnus Support.
519370Spst
698944Sobrien   This file is part of GDB.
719370Spst
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1219370Spst
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1719370Spst
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2219370Spst
2319370Spst#include "defs.h"
2419370Spst#include "gdb_string.h"
2546283Sdfr#include "frame.h"		/* required by inferior.h */
2619370Spst#include "inferior.h"
2719370Spst#include "target.h"
2898944Sobrien#include "gdb_wait.h"
2998944Sobrien#include "gdb_vfork.h"
3019370Spst#include "gdbcore.h"
3119370Spst#include "terminal.h"
3246283Sdfr#include "gdbthread.h"
3398944Sobrien#include "command.h" /* for dont_repeat () */
3419370Spst
3519370Spst#include <signal.h>
3619370Spst
3746283Sdfr/* This just gets used as a default if we can't find SHELL */
3819370Spst#ifndef SHELL_FILE
3919370Spst#define SHELL_FILE "/bin/sh"
4019370Spst#endif
4119370Spst
4246283Sdfrextern char **environ;
4346283Sdfr
4446283Sdfr/* This function breaks up an argument string into an argument
4546283Sdfr * vector suitable for passing to execvp().
4646283Sdfr * E.g., on "run a b c d" this routine would get as input
4746283Sdfr * the string "a b c d", and as output it would fill in argv with
4846283Sdfr * the four arguments "a", "b", "c", "d".
4946283Sdfr */
5046283Sdfrstatic void
5198944Sobrienbreakup_args (char *scratch, char **argv)
5246283Sdfr{
5346283Sdfr  char *cp = scratch;
5446283Sdfr
5546283Sdfr  for (;;)
5646283Sdfr    {
5746283Sdfr
5846283Sdfr      /* Scan past leading separators */
5946283Sdfr      while (*cp == ' ' || *cp == '\t' || *cp == '\n')
6046283Sdfr	{
6146283Sdfr	  cp++;
6246283Sdfr	}
6346283Sdfr
6446283Sdfr      /* Break if at end of string */
6546283Sdfr      if (*cp == '\0')
6646283Sdfr	break;
6746283Sdfr
6846283Sdfr      /* Take an arg */
6946283Sdfr      *argv++ = cp;
7046283Sdfr
7146283Sdfr      /* Scan for next arg separator */
7246283Sdfr      cp = strchr (cp, ' ');
7346283Sdfr      if (cp == NULL)
7446283Sdfr	cp = strchr (cp, '\t');
7546283Sdfr      if (cp == NULL)
7646283Sdfr	cp = strchr (cp, '\n');
7746283Sdfr
7846283Sdfr      /* No separators => end of string => break */
7946283Sdfr      if (cp == NULL)
8046283Sdfr	break;
8146283Sdfr
8246283Sdfr      /* Replace the separator with a terminator */
8346283Sdfr      *cp++ = '\0';
8446283Sdfr    }
8546283Sdfr
8646283Sdfr  /* execv requires a null-terminated arg vector */
8746283Sdfr  *argv = NULL;
8846283Sdfr
8946283Sdfr}
9046283Sdfr
91130803Smarcel/* When executing a command under the given shell, return non-zero
92130803Smarcel   if the '!' character should be escaped when embedded in a quoted
93130803Smarcel   command-line argument.  */
9446283Sdfr
95130803Smarcelstatic int
96130803Smarcelescape_bang_in_quoted_argument (const char *shell_file)
97130803Smarcel{
98130803Smarcel  const int shell_file_len = strlen (shell_file);
99130803Smarcel
100130803Smarcel  /* Bang should be escaped only in C Shells.  For now, simply check
101130803Smarcel     that the shell name ends with 'csh', which covers at least csh
102130803Smarcel     and tcsh.  This should be good enough for now.  */
103130803Smarcel
104130803Smarcel  if (shell_file_len < 3)
105130803Smarcel    return 0;
106130803Smarcel
107130803Smarcel  if (shell_file[shell_file_len - 3] == 'c'
108130803Smarcel      && shell_file[shell_file_len - 2] == 's'
109130803Smarcel      && shell_file[shell_file_len - 1] == 'h')
110130803Smarcel    return 1;
111130803Smarcel
112130803Smarcel  return 0;
113130803Smarcel}
114130803Smarcel
11598944Sobrien/* Start an inferior Unix child process and sets inferior_ptid to its pid.
11619370Spst   EXEC_FILE is the file to run.
11719370Spst   ALLARGS is a string containing the arguments to the program.
11819370Spst   ENV is the environment vector to pass.  SHELL_FILE is the shell file,
11919370Spst   or NULL if we should pick one.  Errors reported with error().  */
12019370Spst
12198944Sobrien/* This function is NOT-REENTRANT.  Some of the variables have been
12298944Sobrien   made static to ensure that they survive the vfork() call.  */
12398944Sobrien
12419370Spstvoid
12598944Sobrienfork_inferior (char *exec_file_arg, char *allargs, char **env,
12698944Sobrien	       void (*traceme_fun) (void), void (*init_trace_fun) (int),
12798944Sobrien	       void (*pre_trace_fun) (void), char *shell_file_arg)
12819370Spst{
12919370Spst  int pid;
13019370Spst  char *shell_command;
13119370Spst  static char default_shell_file[] = SHELL_FILE;
13219370Spst  int len;
13319370Spst  /* Set debug_fork then attach to the child while it sleeps, to debug. */
13419370Spst  static int debug_fork = 0;
13519370Spst  /* This is set to the result of setpgrp, which if vforked, will be visible
13619370Spst     to you in the parent process.  It's only used by humans for debugging.  */
13719370Spst  static int debug_setpgrp = 657473;
13898944Sobrien  static char *shell_file;
13998944Sobrien  static char *exec_file;
14019370Spst  char **save_our_env;
14146283Sdfr  int shell = 0;
14298944Sobrien  static char **argv;
14319370Spst
14419370Spst  /* If no exec file handed to us, get it from the exec-file command -- with
14519370Spst     a good, common error message if none is specified.  */
14698944Sobrien  exec_file = exec_file_arg;
14719370Spst  if (exec_file == 0)
14846283Sdfr    exec_file = get_exec_file (1);
14919370Spst
15046283Sdfr  /* STARTUP_WITH_SHELL is defined in inferior.h.
15146283Sdfr   * If 0, we'll just do a fork/exec, no shell, so don't
15246283Sdfr   * bother figuring out what shell.
15346283Sdfr   */
15498944Sobrien  shell_file = shell_file_arg;
15546283Sdfr  if (STARTUP_WITH_SHELL)
15646283Sdfr    {
15746283Sdfr      /* Figure out what shell to start up the user program under. */
15846283Sdfr      if (shell_file == NULL)
15946283Sdfr	shell_file = getenv ("SHELL");
16046283Sdfr      if (shell_file == NULL)
16146283Sdfr	shell_file = default_shell_file;
16246283Sdfr      shell = 1;
16346283Sdfr    }
16419370Spst
16519370Spst  /* Multiplying the length of exec_file by 4 is to account for the fact
16619370Spst     that it may expand when quoted; it is a worst-case number based on
16719370Spst     every character being '.  */
16898944Sobrien  len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
16919370Spst  /* If desired, concat something onto the front of ALLARGS.
17019370Spst     SHELL_COMMAND is the result.  */
17119370Spst#ifdef SHELL_COMMAND_CONCAT
17219370Spst  shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
17319370Spst  strcpy (shell_command, SHELL_COMMAND_CONCAT);
17419370Spst#else
17519370Spst  shell_command = (char *) alloca (len);
17619370Spst  shell_command[0] = '\0';
17719370Spst#endif
17819370Spst
17946283Sdfr  if (!shell)
18046283Sdfr    {
18146283Sdfr      /* We're going to call execvp. Create argv */
18246283Sdfr      /* Largest case: every other character is a separate arg */
18346283Sdfr      argv = (char **) xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) * sizeof (*argv));
18446283Sdfr      argv[0] = exec_file;
18546283Sdfr      breakup_args (allargs, &argv[1]);
18619370Spst
18746283Sdfr    }
18846283Sdfr  else
18946283Sdfr    {
19046283Sdfr
19146283Sdfr      /* We're going to call a shell */
19246283Sdfr
19346283Sdfr      /* Now add exec_file, quoting as necessary.  */
19446283Sdfr
19546283Sdfr      char *p;
19646283Sdfr      int need_to_quote;
197130803Smarcel      const int escape_bang = escape_bang_in_quoted_argument (shell_file);
19846283Sdfr
19946283Sdfr      strcat (shell_command, "exec ");
20046283Sdfr
20146283Sdfr      /* Quoting in this style is said to work with all shells.  But csh
20298944Sobrien         on IRIX 4.0.1 can't deal with it.  So we only quote it if we need
20398944Sobrien         to.  */
20446283Sdfr      p = exec_file;
20546283Sdfr      while (1)
20646283Sdfr	{
20746283Sdfr	  switch (*p)
20846283Sdfr	    {
20946283Sdfr	    case '\'':
21098944Sobrien	    case '!':
21146283Sdfr	    case '"':
21246283Sdfr	    case '(':
21346283Sdfr	    case ')':
21446283Sdfr	    case '$':
21546283Sdfr	    case '&':
21646283Sdfr	    case ';':
21746283Sdfr	    case '<':
21846283Sdfr	    case '>':
21946283Sdfr	    case ' ':
22046283Sdfr	    case '\n':
22146283Sdfr	    case '\t':
22246283Sdfr	      need_to_quote = 1;
22346283Sdfr	      goto end_scan;
22419370Spst
22546283Sdfr	    case '\0':
22646283Sdfr	      need_to_quote = 0;
22746283Sdfr	      goto end_scan;
22819370Spst
22946283Sdfr	    default:
23046283Sdfr	      break;
23146283Sdfr	    }
23246283Sdfr	  ++p;
23346283Sdfr	}
23446283Sdfr    end_scan:
23546283Sdfr      if (need_to_quote)
23646283Sdfr	{
23746283Sdfr	  strcat (shell_command, "'");
23846283Sdfr	  for (p = exec_file; *p != '\0'; ++p)
23946283Sdfr	    {
24046283Sdfr	      if (*p == '\'')
24146283Sdfr		strcat (shell_command, "'\\''");
242130803Smarcel	      else if (*p == '!' && escape_bang)
24398944Sobrien		strcat (shell_command, "\\!");
24446283Sdfr	      else
24546283Sdfr		strncat (shell_command, p, 1);
24646283Sdfr	    }
24746283Sdfr	  strcat (shell_command, "'");
24846283Sdfr	}
24946283Sdfr      else
25046283Sdfr	strcat (shell_command, exec_file);
25119370Spst
25246283Sdfr      strcat (shell_command, " ");
25346283Sdfr      strcat (shell_command, allargs);
25419370Spst
25546283Sdfr    }
25646283Sdfr
25719370Spst  /* exec is said to fail if the executable is open.  */
25819370Spst  close_exec_file ();
25919370Spst
26019370Spst  /* Retain a copy of our environment variables, since the child will
26146283Sdfr     replace the value of  environ  and if we're vforked, we have to
26219370Spst     restore it.  */
26319370Spst  save_our_env = environ;
26419370Spst
26519370Spst  /* Tell the terminal handling subsystem what tty we plan to run on;
26619370Spst     it will just record the information for later.  */
26719370Spst
26819370Spst  new_tty_prefork (inferior_io_terminal);
26919370Spst
27019370Spst  /* It is generally good practice to flush any possible pending stdio
27119370Spst     output prior to doing a fork, to avoid the possibility of both the
27219370Spst     parent and child flushing the same data after the fork. */
27319370Spst
27419370Spst  gdb_flush (gdb_stdout);
27519370Spst  gdb_flush (gdb_stderr);
27619370Spst
27746283Sdfr  /* If there's any initialization of the target layers that must happen
27898944Sobrien     to prepare to handle the child we're about fork, do it now...
27946283Sdfr   */
28046283Sdfr  if (pre_trace_fun != NULL)
28146283Sdfr    (*pre_trace_fun) ();
28246283Sdfr
28398944Sobrien  /* Create the child process.  Note that the apparent call to vfork()
28498944Sobrien     below *might* actually be a call to fork() due to the fact that
28598944Sobrien     autoconf will ``#define vfork fork'' on certain platforms.  */
28619370Spst  if (debug_fork)
28719370Spst    pid = fork ();
28819370Spst  else
28919370Spst    pid = vfork ();
29019370Spst
29119370Spst  if (pid < 0)
29219370Spst    perror_with_name ("vfork");
29319370Spst
29419370Spst  if (pid == 0)
29519370Spst    {
29646283Sdfr      if (debug_fork)
29719370Spst	sleep (debug_fork);
29819370Spst
29919370Spst      /* Run inferior in a separate process group.  */
30019370Spst      debug_setpgrp = gdb_setpgid ();
30119370Spst      if (debug_setpgrp == -1)
30246283Sdfr	perror ("setpgrp failed in child");
30319370Spst
30419370Spst      /* Ask the tty subsystem to switch to the one we specified earlier
30598944Sobrien         (or to share the current terminal, if none was specified).  */
30619370Spst
30719370Spst      new_tty ();
30819370Spst
30919370Spst      /* Changing the signal handlers for the inferior after
31098944Sobrien         a vfork can also change them for the superior, so we don't mess
31198944Sobrien         with signals here.  See comments in
31298944Sobrien         initialize_signals for how we get the right signal handlers
31398944Sobrien         for the inferior.  */
31419370Spst
31519370Spst      /* "Trace me, Dr. Memory!" */
31619370Spst      (*traceme_fun) ();
31746283Sdfr      /* The call above set this process (the "child") as debuggable
31846283Sdfr       * by the original gdb process (the "parent").  Since processes
31946283Sdfr       * (unlike people) can have only one parent, if you are
32046283Sdfr       * debugging gdb itself (and your debugger is thus _already_ the
32146283Sdfr       * controller/parent for this child),  code from here on out
32246283Sdfr       * is undebuggable.  Indeed, you probably got an error message
32346283Sdfr       * saying "not parent".  Sorry--you'll have to use print statements!
32446283Sdfr       */
32519370Spst
32619370Spst      /* There is no execlpe call, so we have to set the environment
32798944Sobrien         for our child in the global variable.  If we've vforked, this
32898944Sobrien         clobbers the parent, but environ is restored a few lines down
32998944Sobrien         in the parent.  By the way, yes we do need to look down the
33098944Sobrien         path to find $SHELL.  Rich Pixley says so, and I agree.  */
33119370Spst      environ = env;
33219370Spst
33346283Sdfr      /* If we decided above to start up with a shell,
33446283Sdfr       * we exec the shell,
33546283Sdfr       * "-c" says to interpret the next arg as a shell command
33646283Sdfr       * to execute, and this command is "exec <target-program> <args>".
33746283Sdfr       * "-f" means "fast startup" to the c-shell, which means
33846283Sdfr       * don't do .cshrc file. Doing .cshrc may cause fork/exec
33946283Sdfr       * events which will confuse debugger start-up code.
34046283Sdfr       */
34146283Sdfr      if (shell)
34246283Sdfr	{
34346283Sdfr	  execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
34446283Sdfr
34546283Sdfr	  /* If we get here, it's an error */
34646283Sdfr	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
34746283Sdfr			      safe_strerror (errno));
34846283Sdfr	  gdb_flush (gdb_stderr);
34946283Sdfr	  _exit (0177);
35046283Sdfr	}
35146283Sdfr      else
35246283Sdfr	{
35346283Sdfr	  /* Otherwise, we directly exec the target program with execvp. */
35446283Sdfr	  int i;
35546283Sdfr	  char *errstring;
35698944Sobrien
35746283Sdfr	  execvp (exec_file, argv);
35846283Sdfr
35946283Sdfr	  /* If we get here, it's an error */
36046283Sdfr	  errstring = safe_strerror (errno);
36146283Sdfr	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
36246283Sdfr
36346283Sdfr	  i = 1;
36446283Sdfr	  while (argv[i] != NULL)
36546283Sdfr	    {
36646283Sdfr	      if (i != 1)
36746283Sdfr		fprintf_unfiltered (gdb_stderr, " ");
36846283Sdfr	      fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
36946283Sdfr	      i++;
37046283Sdfr	    }
37146283Sdfr	  fprintf_unfiltered (gdb_stderr, ".\n");
37246283Sdfr	  /* This extra info seems to be useless
37398944Sobrien	     fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
37498944Sobrien	   */
37546283Sdfr	  gdb_flush (gdb_stderr);
37646283Sdfr	  _exit (0177);
37746283Sdfr	}
37819370Spst    }
37919370Spst
38019370Spst  /* Restore our environment in case a vforked child clob'd it.  */
38119370Spst  environ = save_our_env;
38219370Spst
38346283Sdfr  init_thread_list ();
38419370Spst
38598944Sobrien  inferior_ptid = pid_to_ptid (pid);	/* Needed for wait_for_inferior stuff below */
38619370Spst
38719370Spst  /* Now that we have a child process, make it our target, and
38819370Spst     initialize anything target-vector-specific that needs initializing.  */
38919370Spst
39046283Sdfr  (*init_trace_fun) (pid);
39146283Sdfr
39219370Spst  /* We are now in the child process of interest, having exec'd the
39319370Spst     correct program, and are poised at the first instruction of the
39419370Spst     new program.  */
39519370Spst
39698944Sobrien  /* Allow target dependent code to play with the new process.  This might be
39719370Spst     used to have target-specific code initialize a variable in the new process
39819370Spst     prior to executing the first instruction.  */
39919370Spst  TARGET_CREATE_INFERIOR_HOOK (pid);
40019370Spst
40119370Spst#ifdef SOLIB_CREATE_INFERIOR_HOOK
40219370Spst  SOLIB_CREATE_INFERIOR_HOOK (pid);
40319370Spst#endif
40419370Spst}
40519370Spst
40619370Spst/* Accept NTRAPS traps from the inferior.  */
40719370Spst
40819370Spstvoid
40998944Sobrienstartup_inferior (int ntraps)
41019370Spst{
41119370Spst  int pending_execs = ntraps;
41219370Spst  int terminal_initted;
41319370Spst
41419370Spst  /* The process was started by the fork that created it,
41519370Spst     but it will have stopped one instruction after execing the shell.
41619370Spst     Here we must get it up to actual execution of the real program.  */
41719370Spst
41819370Spst  clear_proceed_status ();
41919370Spst
42019370Spst  init_wait_for_inferior ();
42119370Spst
42219370Spst  terminal_initted = 0;
42319370Spst
42446283Sdfr  if (STARTUP_WITH_SHELL)
42546283Sdfr    inferior_ignoring_startup_exec_events = ntraps;
42646283Sdfr  else
42746283Sdfr    inferior_ignoring_startup_exec_events = 0;
42846283Sdfr  inferior_ignoring_leading_exec_events =
42946283Sdfr    target_reported_exec_events_per_exec_call () - 1;
43046283Sdfr
43119370Spst  while (1)
43219370Spst    {
433130803Smarcel      /* Make wait_for_inferior be quiet */
434130803Smarcel      stop_soon = STOP_QUIETLY;
43519370Spst      wait_for_inferior ();
43619370Spst      if (stop_signal != TARGET_SIGNAL_TRAP)
43719370Spst	{
43819370Spst	  /* Let shell child handle its own signals in its own way */
43919370Spst	  /* FIXME, what if child has exit()ed?  Must exit loop somehow */
44019370Spst	  resume (0, stop_signal);
44119370Spst	}
44219370Spst      else
44319370Spst	{
44419370Spst	  /* We handle SIGTRAP, however; it means child did an exec.  */
44519370Spst	  if (!terminal_initted)
44619370Spst	    {
44719370Spst	      /* Now that the child has exec'd we know it has already set its
44898944Sobrien	         process group.  On POSIX systems, tcsetpgrp will fail with
44998944Sobrien	         EPERM if we try it before the child's setpgid.  */
45019370Spst
45119370Spst	      /* Set up the "saved terminal modes" of the inferior
45298944Sobrien	         based on what modes we are starting it with.  */
45319370Spst	      target_terminal_init ();
45419370Spst
45519370Spst	      /* Install inferior's terminal modes.  */
45619370Spst	      target_terminal_inferior ();
45719370Spst
45819370Spst	      terminal_initted = 1;
45919370Spst	    }
46046283Sdfr
46146283Sdfr	  pending_execs = pending_execs - 1;
46246283Sdfr	  if (0 == pending_execs)
46319370Spst	    break;
46446283Sdfr
46546283Sdfr	  resume (0, TARGET_SIGNAL_0);	/* Just make it go on */
46619370Spst	}
46719370Spst    }
468130803Smarcel  stop_soon = NO_STOP_QUIETLY;
46919370Spst}
470