]> Git Repo - binutils.git/blob - gdb/fork-child.c
Make the /proc support a target-struct in its own right.
[binutils.git] / gdb / fork-child.c
1 /* Fork a Unix child process, and set up to debug it, for GDB.
2    Copyright 1990, 1991, 1992 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 "frame.h"  /* required by inferior.h */
23 #include "inferior.h"
24 #include "target.h"
25 #include "wait.h"
26 #include "gdbcore.h"
27 #include "terminal.h"           /* For #ifdef TIOCGPGRP and new_tty */
28
29 #include <signal.h>
30
31 #ifdef SET_STACK_LIMIT_HUGE
32 #include <sys/time.h>
33 #include <sys/resource.h>
34
35 extern int original_stack_limit;
36 #endif /* SET_STACK_LIMIT_HUGE */
37
38 extern char **environ;
39
40 /* Start an inferior Unix child process and sets inferior_pid to its pid.
41    EXEC_FILE is the file to run.
42    ALLARGS is a string containing the arguments to the program.
43    ENV is the environment vector to pass.  Errors reported with error().  */
44
45 #ifndef SHELL_FILE
46 #define SHELL_FILE "/bin/sh"
47 #endif
48
49 void
50 fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun)
51      char *exec_file;
52      char *allargs;
53      char **env;
54      void (*traceme_fun) PARAMS ((void));
55      void (*init_trace_fun) PARAMS ((int));
56 {
57   int pid;
58   char *shell_command;
59   char *shell_file;
60   static char default_shell_file[] = SHELL_FILE;
61   int len;
62   int pending_execs;
63   /* Set debug_fork then attach to the child while it sleeps, to debug. */
64   static int debug_fork = 0;
65   /* This is set to the result of setpgrp, which if vforked, will be visible
66      to you in the parent process.  It's only used by humans for debugging.  */
67   static int debug_setpgrp = 657473;
68   char **save_our_env;
69
70   /* If no exec file handed to us, get it from the exec-file command -- with
71      a good, common error message if none is specified.  */
72   if (exec_file == 0)
73     exec_file = get_exec_file(1);
74
75   /* The user might want tilde-expansion, and in general probably wants
76      the program to behave the same way as if run from
77      his/her favorite shell.  So we let the shell run it for us.
78      FIXME, this should probably search the local environment (as
79      modified by the setenv command), not the env gdb inherited.  */
80   shell_file = getenv ("SHELL");
81   if (shell_file == NULL)
82     shell_file = default_shell_file;
83   
84   len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
85   /* If desired, concat something onto the front of ALLARGS.
86      SHELL_COMMAND is the result.  */
87 #ifdef SHELL_COMMAND_CONCAT
88   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
89   strcpy (shell_command, SHELL_COMMAND_CONCAT);
90 #else
91   shell_command = (char *) alloca (len);
92   shell_command[0] = '\0';
93 #endif
94   strcat (shell_command, "exec ");
95   strcat (shell_command, exec_file);
96   strcat (shell_command, " ");
97   strcat (shell_command, allargs);
98
99   /* exec is said to fail if the executable is open.  */
100   close_exec_file ();
101
102   /* Retain a copy of our environment variables, since the child will
103      replace the value of  environ  and if we're vforked, we have to 
104      restore it.  */
105   save_our_env = environ;
106
107   /* Tell the terminal handling subsystem what tty we plan to run on;
108      it will just record the information for later.  */
109
110   new_tty_prefork (inferior_io_terminal);
111
112   /* It is generally good practice to flush any possible pending stdio
113      output prior to doing a fork, to avoid the possibility of both the
114      parent and child flushing the same data after the fork. */
115
116   fflush (stdout);
117   fflush (stderr);
118
119 #if defined(USG) && !defined(HAVE_VFORK)
120   pid = fork ();
121 #else
122   if (debug_fork)
123     pid = fork ();
124   else
125     pid = vfork ();
126 #endif
127
128   if (pid < 0)
129     perror_with_name ("vfork");
130
131   if (pid == 0)
132     {
133       if (debug_fork) 
134         sleep (debug_fork);
135
136 #ifdef TIOCGPGRP
137       /* Run inferior in a separate process group.  */
138 #ifdef NEED_POSIX_SETPGID
139       debug_setpgrp = setpgid (0, 0);
140 #else
141 #if defined(USG) && !defined(SETPGRP_ARGS)
142       debug_setpgrp = setpgrp ();
143 #else
144       debug_setpgrp = setpgrp (getpid (), getpid ());
145 #endif /* USG */
146 #endif /* NEED_POSIX_SETPGID */
147       if (debug_setpgrp == -1)
148          perror("setpgrp failed in child");
149 #endif /* TIOCGPGRP */
150
151 #ifdef SET_STACK_LIMIT_HUGE
152       /* Reset the stack limit back to what it was.  */
153       {
154         struct rlimit rlim;
155
156         getrlimit (RLIMIT_STACK, &rlim);
157         rlim.rlim_cur = original_stack_limit;
158         setrlimit (RLIMIT_STACK, &rlim);
159       }
160 #endif /* SET_STACK_LIMIT_HUGE */
161
162       /* Ask the tty subsystem to switch to the one we specified earlier
163          (or to share the current terminal, if none was specified).  */
164
165       new_tty ();
166
167       /* Changing the signal handlers for the inferior after
168          a vfork can also change them for the superior, so we don't mess
169          with signals here.  See comments in
170          initialize_signals for how we get the right signal handlers
171          for the inferior.  */
172
173       /* "Trace me, Dr. Memory!" */
174       (*traceme_fun) ();
175
176       /* There is no execlpe call, so we have to set the environment
177          for our child in the global variable.  If we've vforked, this
178          clobbers the parent, but environ is restored a few lines down
179          in the parent.  By the way, yes we do need to look down the
180          path to find $SHELL.  Rich Pixley says so, and I agree.  */
181       environ = env;
182       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
183
184       fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
185                safe_strerror (errno));
186       fflush (stderr);
187       _exit (0177);
188     }
189
190   /* Restore our environment in case a vforked child clob'd it.  */
191   environ = save_our_env;
192
193   /* Now that we have a child process, make it our target, and
194      initialize anything target-vector-specific that needs initializing.  */
195   (*init_trace_fun)(pid);
196
197 #ifdef CREATE_INFERIOR_HOOK
198   CREATE_INFERIOR_HOOK (pid);
199 #endif  
200
201 /* The process was started by the fork that created it,
202    but it will have stopped one instruction after execing the shell.
203    Here we must get it up to actual execution of the real program.  */
204
205   inferior_pid = pid;           /* Needed for wait_for_inferior stuff below */
206
207   clear_proceed_status ();
208
209   /* We will get a trace trap after one instruction.
210      Continue it automatically.  Eventually (after shell does an exec)
211      it will get another trace trap.  Then insert breakpoints and continue.  */
212
213 #ifdef START_INFERIOR_TRAPS_EXPECTED
214   pending_execs = START_INFERIOR_TRAPS_EXPECTED;
215 #else
216   pending_execs = 2;
217 #endif
218
219   init_wait_for_inferior ();
220
221   /* Set up the "saved terminal modes" of the inferior
222      based on what modes we are starting it with.  */
223   target_terminal_init ();
224
225   /* Install inferior's terminal modes.  */
226   target_terminal_inferior ();
227
228   while (1)
229     {
230       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
231       wait_for_inferior ();
232       if (stop_signal != SIGTRAP)
233         {
234           /* Let shell child handle its own signals in its own way */
235           /* FIXME, what if child has exit()ed?  Must exit loop somehow */
236           resume (0, stop_signal);
237         }
238       else
239         {
240           /* We handle SIGTRAP, however; it means child did an exec.  */
241           if (0 == --pending_execs)
242             break;
243           resume (0, 0);                /* Just make it go on */
244         }
245     }
246   stop_soon_quietly = 0;
247
248   /* We are now in the child process of interest, having exec'd the
249      correct program, and are poised at the first instruction of the
250      new program.  */
251 #ifdef SOLIB_CREATE_INFERIOR_HOOK
252   SOLIB_CREATE_INFERIOR_HOOK (pid);
253 #endif
254 }
This page took 0.037806 seconds and 4 git commands to generate.