home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / fork-child.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  8.6 KB  |  301 lines

  1. /* Fork a Unix child process, and set up to debug it, for GDB.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include <string.h>
  23. #include "frame.h"  /* required by inferior.h */
  24. #include "inferior.h"
  25. #include "target.h"
  26. #include "wait.h"
  27. #include "gdbcore.h"
  28. #include "terminal.h"
  29. #include "thread.h"
  30.  
  31. #include <signal.h>
  32.  
  33. extern char **environ;
  34.  
  35. #ifndef SHELL_FILE
  36. #define SHELL_FILE "/bin/sh"
  37. #endif
  38.  
  39. /* Start an inferior Unix child process and sets inferior_pid to its pid.
  40.    EXEC_FILE is the file to run.
  41.    ALLARGS is a string containing the arguments to the program.
  42.    ENV is the environment vector to pass.  SHELL_FILE is the shell file,
  43.    or NULL if we should pick one.  Errors reported with error().  */
  44.  
  45. void
  46. fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
  47.            shell_file)
  48.      char *exec_file;
  49.      char *allargs;
  50.      char **env;
  51.      void (*traceme_fun) PARAMS ((void));
  52.      void (*init_trace_fun) PARAMS ((int));
  53.      char *shell_file;
  54. {
  55.   int pid;
  56.   char *shell_command;
  57.   static char default_shell_file[] = SHELL_FILE;
  58.   int len;
  59.   /* Set debug_fork then attach to the child while it sleeps, to debug. */
  60.   static int debug_fork = 0;
  61.   /* This is set to the result of setpgrp, which if vforked, will be visible
  62.      to you in the parent process.  It's only used by humans for debugging.  */
  63.   static int debug_setpgrp = 657473;
  64.   char **save_our_env;
  65.  
  66.   /* If no exec file handed to us, get it from the exec-file command -- with
  67.      a good, common error message if none is specified.  */
  68.   if (exec_file == 0)
  69.     exec_file = get_exec_file(1);
  70.  
  71.   /* The user might want tilde-expansion, and in general probably wants
  72.      the program to behave the same way as if run from
  73.      his/her favorite shell.  So we let the shell run it for us.
  74.      FIXME-maybe, we might want a "set shell" command so the user can change
  75.      the shell from within GDB (if so, change callers which pass in a non-NULL
  76.      shell_file too).  */
  77.   if (shell_file == NULL)
  78.     shell_file = getenv ("SHELL");
  79.   if (shell_file == NULL)
  80.     shell_file = default_shell_file;
  81.  
  82.   /* Multiplying the length of exec_file by 4 is to account for the fact
  83.      that it may expand when quoted; it is a worst-case number based on
  84.      every character being '.  */
  85.   len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
  86.   /* If desired, concat something onto the front of ALLARGS.
  87.      SHELL_COMMAND is the result.  */
  88. #ifdef SHELL_COMMAND_CONCAT
  89.   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
  90.   strcpy (shell_command, SHELL_COMMAND_CONCAT);
  91. #else
  92.   shell_command = (char *) alloca (len);
  93.   shell_command[0] = '\0';
  94. #endif
  95.   strcat (shell_command, "exec ");
  96.  
  97.   /* Now add exec_file, quoting as necessary.  */
  98.   {
  99.     char *p;
  100.     int need_to_quote;
  101.  
  102.     /* Quoting in this style is said to work with all shells.  But csh
  103.        on IRIX 4.0.1 can't deal with it.  So we only quote it if we need
  104.        to.  */
  105.     p = exec_file;
  106.     while (1)
  107.       {
  108.     switch (*p)
  109.       {
  110.       case '\'':
  111.       case '"':
  112.       case '(':
  113.       case ')':
  114.       case '$':
  115.       case '&':
  116.       case ';':
  117.       case '<':
  118.       case '>':
  119.       case ' ':
  120.       case '\n':
  121.       case '\t':
  122.         need_to_quote = 1;
  123.         goto end_scan;
  124.  
  125.       case '\0':
  126.         need_to_quote = 0;
  127.         goto end_scan;
  128.  
  129.       default:
  130.         break;
  131.       }
  132.     ++p;
  133.       }
  134.   end_scan:
  135.     if (need_to_quote)
  136.       {
  137.     strcat (shell_command, "'");
  138.     for (p = exec_file; *p != '\0'; ++p)
  139.       {
  140.         if (*p == '\'')
  141.           strcat (shell_command, "'\\''");
  142.         else
  143.           strncat (shell_command, p, 1);
  144.       }
  145.     strcat (shell_command, "'");
  146.       }
  147.     else
  148.       strcat (shell_command, exec_file);
  149.   }
  150.  
  151.   strcat (shell_command, " ");
  152.   strcat (shell_command, allargs);
  153.  
  154.   /* exec is said to fail if the executable is open.  */
  155.   close_exec_file ();
  156.  
  157.   /* Retain a copy of our environment variables, since the child will
  158.      replace the value of  environ  and if we're vforked, we have to 
  159.      restore it.  */
  160.   save_our_env = environ;
  161.  
  162.   /* Tell the terminal handling subsystem what tty we plan to run on;
  163.      it will just record the information for later.  */
  164.  
  165.   new_tty_prefork (inferior_io_terminal);
  166.  
  167.   /* It is generally good practice to flush any possible pending stdio
  168.      output prior to doing a fork, to avoid the possibility of both the
  169.      parent and child flushing the same data after the fork. */
  170.  
  171.   gdb_flush (gdb_stdout);
  172.   gdb_flush (gdb_stderr);
  173.  
  174. #if defined(USG) && !defined(HAVE_VFORK)
  175.   pid = fork ();
  176. #else
  177.   if (debug_fork)
  178.     pid = fork ();
  179.   else
  180.     pid = vfork ();
  181. #endif
  182.  
  183.   if (pid < 0)
  184.     perror_with_name ("vfork");
  185.  
  186.   if (pid == 0)
  187.     {
  188.       if (debug_fork) 
  189.     sleep (debug_fork);
  190.  
  191.       /* Run inferior in a separate process group.  */
  192.       debug_setpgrp = gdb_setpgid ();
  193.       if (debug_setpgrp == -1)
  194.      perror("setpgrp failed in child");
  195.  
  196.       /* Ask the tty subsystem to switch to the one we specified earlier
  197.      (or to share the current terminal, if none was specified).  */
  198.  
  199.       new_tty ();
  200.  
  201.       /* Changing the signal handlers for the inferior after
  202.      a vfork can also change them for the superior, so we don't mess
  203.      with signals here.  See comments in
  204.      initialize_signals for how we get the right signal handlers
  205.      for the inferior.  */
  206.  
  207.       /* "Trace me, Dr. Memory!" */
  208.       (*traceme_fun) ();
  209.  
  210.       /* There is no execlpe call, so we have to set the environment
  211.      for our child in the global variable.  If we've vforked, this
  212.      clobbers the parent, but environ is restored a few lines down
  213.      in the parent.  By the way, yes we do need to look down the
  214.      path to find $SHELL.  Rich Pixley says so, and I agree.  */
  215.       environ = env;
  216.       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
  217.  
  218.       fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
  219.            safe_strerror (errno));
  220.       gdb_flush (gdb_stderr);
  221.       _exit (0177);
  222.     }
  223.  
  224.   /* Restore our environment in case a vforked child clob'd it.  */
  225.   environ = save_our_env;
  226.  
  227.   init_thread_list();
  228.  
  229.   inferior_pid = pid;        /* Needed for wait_for_inferior stuff below */
  230.  
  231.   /* Now that we have a child process, make it our target, and
  232.      initialize anything target-vector-specific that needs initializing.  */
  233.   (*init_trace_fun)(pid);
  234.  
  235.   /* We are now in the child process of interest, having exec'd the
  236.      correct program, and are poised at the first instruction of the
  237.      new program.  */
  238. #ifdef SOLIB_CREATE_INFERIOR_HOOK
  239.   SOLIB_CREATE_INFERIOR_HOOK (pid);
  240. #endif
  241. }
  242.  
  243. /* Accept NTRAPS traps from the inferior.  */
  244.  
  245. void
  246. startup_inferior (ntraps)
  247.      int ntraps;
  248. {
  249.   int pending_execs = ntraps;
  250.   int terminal_initted;
  251.  
  252.   /* The process was started by the fork that created it,
  253.      but it will have stopped one instruction after execing the shell.
  254.      Here we must get it up to actual execution of the real program.  */
  255.  
  256.   clear_proceed_status ();
  257.  
  258.   init_wait_for_inferior ();
  259.  
  260.   terminal_initted = 0;
  261.  
  262. #ifdef STARTUP_INFERIOR
  263.   STARTUP_INFERIOR (pending_execs);
  264. #else
  265.   while (1)
  266.     {
  267.       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
  268.       wait_for_inferior ();
  269.       if (stop_signal != TARGET_SIGNAL_TRAP)
  270.     {
  271.       /* Let shell child handle its own signals in its own way */
  272.       /* FIXME, what if child has exit()ed?  Must exit loop somehow */
  273.       resume (0, stop_signal);
  274.     }
  275.       else
  276.     {
  277.       /* We handle SIGTRAP, however; it means child did an exec.  */
  278.       if (!terminal_initted)
  279.         {
  280.           /* Now that the child has exec'd we know it has already set its
  281.          process group.  On POSIX systems, tcsetpgrp will fail with
  282.          EPERM if we try it before the child's setpgid.  */
  283.  
  284.           /* Set up the "saved terminal modes" of the inferior
  285.          based on what modes we are starting it with.  */
  286.           target_terminal_init ();
  287.  
  288.           /* Install inferior's terminal modes.  */
  289.           target_terminal_inferior ();
  290.  
  291.           terminal_initted = 1;
  292.         }
  293.       if (0 == --pending_execs)
  294.         break;
  295.       resume (0, TARGET_SIGNAL_0);        /* Just make it go on */
  296.     }
  297.     }
  298. #endif /* STARTUP_INFERIOR */
  299.   stop_soon_quietly = 0;
  300. }
  301.