Split non-target-dependent code out of target_attach routines.
[deliverable/binutils-gdb.git] / gdb / inftarg.c
1 /* Target-vector operations for controlling Unix child processes, 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 "ieee-float.h" /* Required by REGISTER_CONVERT_TO_XXX */
28 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
29
30 #include <signal.h>
31
32 #ifdef SET_STACK_LIMIT_HUGE
33 #include <sys/time.h>
34 #include <sys/resource.h>
35
36 extern int original_stack_limit;
37 #endif /* SET_STACK_LIMIT_HUGE */
38
39 static void
40 child_prepare_to_store PARAMS ((void));
41
42 static int
43 child_wait PARAMS ((int *));
44
45 static void
46 child_open PARAMS ((char *, int));
47
48 static void
49 child_files_info PARAMS ((struct target_ops *));
50
51 static void
52 child_detach PARAMS ((char *, int));
53
54 static void
55 child_attach PARAMS ((char *, int));
56
57 static void
58 child_create_inferior PARAMS ((char *, char *, char **));
59
60 static void
61 child_mourn_inferior PARAMS ((void));
62
63 static int
64 child_can_run PARAMS ((void));
65
66 extern char **environ;
67
68 /* Forward declaration */
69 extern struct target_ops child_ops;
70
71 /* Wait for child to do something. Return pid of child, or -1 in case
72 of error; store status through argument pointer STATUS. */
73
74 static int
75 child_wait (status)
76 int *status;
77 {
78 int pid;
79
80 do {
81 #ifdef USE_PROC_FS
82 pid = proc_wait (status);
83 #else
84 pid = wait (status);
85 #endif
86 if (pid == -1) /* No more children to wait for */
87 {
88 fprintf (stderr, "Child process unexpectedly missing.\n");
89 *status = 42; /* Claim it exited with signal 42 */
90 return -1;
91 }
92 } while (pid != inferior_pid); /* Some other child died or stopped */
93 return pid;
94 }
95
96
97 /* Attach to process PID, then initialize for debugging it. */
98
99 static void
100 child_attach (args, from_tty)
101 char *args;
102 int from_tty;
103 {
104 char *exec_file;
105 int pid;
106
107 if (!args)
108 error_no_arg ("process-id to attach");
109
110 #ifndef ATTACH_DETACH
111 error ("Can't attach to a process on this machine.");
112 #else
113 pid = atoi (args);
114
115 if (pid == getpid()) /* Trying to masturbate? */
116 error ("I refuse to debug myself!");
117
118 if (from_tty)
119 {
120 exec_file = (char *) get_exec_file (0);
121
122 if (exec_file)
123 printf ("Attaching program `%s', pid %d\n", exec_file, pid);
124 else
125 printf ("Attaching pid %d\n", pid);
126
127 fflush (stdout);
128 }
129
130 attach (pid);
131 inferior_pid = pid;
132 push_target (&child_ops);
133 #endif /* ATTACH_DETACH */
134 }
135
136
137 /* Take a program previously attached to and detaches it.
138 The program resumes execution and will no longer stop
139 on signals, etc. We'd better not have left any breakpoints
140 in the program or it'll die when it hits one. For this
141 to work, it may be necessary for the process to have been
142 previously attached. It *might* work if the program was
143 started via the normal ptrace (PTRACE_TRACEME). */
144
145 static void
146 child_detach (args, from_tty)
147 char *args;
148 int from_tty;
149 {
150 int siggnal = 0;
151
152 #ifdef ATTACH_DETACH
153 if (from_tty)
154 {
155 char *exec_file = get_exec_file (0);
156 if (exec_file == 0)
157 exec_file = "";
158 printf ("Detaching program: %s pid %d\n",
159 exec_file, inferior_pid);
160 fflush (stdout);
161 }
162 if (args)
163 siggnal = atoi (args);
164
165 detach (siggnal);
166 inferior_pid = 0;
167 unpush_target (&child_ops); /* Pop out of handling an inferior */
168 #else
169 error ("This version of Unix does not support detaching a process.");
170 #endif
171 }
172
173 /* Get ready to modify the registers array. On machines which store
174 individual registers, this doesn't need to do anything. On machines
175 which store all the registers in one fell swoop, this makes sure
176 that registers contains all the registers from the program being
177 debugged. */
178
179 static void
180 child_prepare_to_store ()
181 {
182 #ifdef CHILD_PREPARE_TO_STORE
183 CHILD_PREPARE_TO_STORE ();
184 #endif
185 }
186
187 /* Print status information about what we're accessing. */
188
189 static void
190 child_files_info (ignore)
191 struct target_ops *ignore;
192 {
193 printf ("\tUsing the running image of %s process %d.\n",
194 attach_flag? "attached": "child", inferior_pid);
195 }
196
197 /* ARGSUSED */
198 static void
199 child_open (arg, from_tty)
200 char *arg;
201 int from_tty;
202 {
203 error ("Use the \"run\" command to start a Unix child process.");
204 }
205
206 /* Start an inferior Unix child process and sets inferior_pid to its pid.
207 EXEC_FILE is the file to run.
208 ALLARGS is a string containing the arguments to the program.
209 ENV is the environment vector to pass. Errors reported with error(). */
210
211 #ifndef SHELL_FILE
212 #define SHELL_FILE "/bin/sh"
213 #endif
214
215 static void
216 child_create_inferior (exec_file, allargs, env)
217 char *exec_file;
218 char *allargs;
219 char **env;
220 {
221 int pid;
222 char *shell_command;
223 char *shell_file;
224 static char default_shell_file[] = SHELL_FILE;
225 int len;
226 int pending_execs;
227 /* Set debug_fork then attach to the child while it sleeps, to debug. */
228 static int debug_fork = 0;
229 /* This is set to the result of setpgrp, which if vforked, will be visible
230 to you in the parent process. It's only used by humans for debugging. */
231 static int debug_setpgrp = 657473;
232 char **save_our_env;
233
234 /* If no exec file handed to us, get it from the exec-file command -- with
235 a good, common error message if none is specified. */
236 if (exec_file == 0)
237 exec_file = get_exec_file(1);
238
239 /* The user might want tilde-expansion, and in general probably wants
240 the program to behave the same way as if run from
241 his/her favorite shell. So we let the shell run it for us.
242 FIXME, this should probably search the local environment (as
243 modified by the setenv command), not the env gdb inherited. */
244 shell_file = getenv ("SHELL");
245 if (shell_file == NULL)
246 shell_file = default_shell_file;
247
248 len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
249 /* If desired, concat something onto the front of ALLARGS.
250 SHELL_COMMAND is the result. */
251 #ifdef SHELL_COMMAND_CONCAT
252 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
253 strcpy (shell_command, SHELL_COMMAND_CONCAT);
254 #else
255 shell_command = (char *) alloca (len);
256 shell_command[0] = '\0';
257 #endif
258 strcat (shell_command, "exec ");
259 strcat (shell_command, exec_file);
260 strcat (shell_command, " ");
261 strcat (shell_command, allargs);
262
263 /* exec is said to fail if the executable is open. */
264 close_exec_file ();
265
266 /* Retain a copy of our environment variables, since the child will
267 replace the value of environ and if we're vforked, we have to
268 restore it. */
269 save_our_env = environ;
270
271 /* Tell the terminal handling subsystem what tty we plan to run on;
272 it will just record the information for later. */
273
274 new_tty_prefork (inferior_io_terminal);
275
276 /* It is generally good practice to flush any possible pending stdio
277 output prior to doing a fork, to avoid the possibility of both the
278 parent and child flushing the same data after the fork. */
279
280 fflush (stdout);
281 fflush (stderr);
282
283 #if defined(USG) && !defined(HAVE_VFORK)
284 pid = fork ();
285 #else
286 if (debug_fork)
287 pid = fork ();
288 else
289 pid = vfork ();
290 #endif
291
292 if (pid < 0)
293 perror_with_name ("vfork");
294
295 if (pid == 0)
296 {
297 if (debug_fork)
298 sleep (debug_fork);
299
300 #ifdef TIOCGPGRP
301 /* Run inferior in a separate process group. */
302 #ifdef NEED_POSIX_SETPGID
303 debug_setpgrp = setpgid (0, 0);
304 #else
305 #if defined(USG) && !defined(SETPGRP_ARGS)
306 debug_setpgrp = setpgrp ();
307 #else
308 debug_setpgrp = setpgrp (getpid (), getpid ());
309 #endif /* USG */
310 #endif /* NEED_POSIX_SETPGID */
311 if (debug_setpgrp == -1)
312 perror("setpgrp failed in child");
313 #endif /* TIOCGPGRP */
314
315 #ifdef SET_STACK_LIMIT_HUGE
316 /* Reset the stack limit back to what it was. */
317 {
318 struct rlimit rlim;
319
320 getrlimit (RLIMIT_STACK, &rlim);
321 rlim.rlim_cur = original_stack_limit;
322 setrlimit (RLIMIT_STACK, &rlim);
323 }
324 #endif /* SET_STACK_LIMIT_HUGE */
325
326 /* Ask the tty subsystem to switch to the one we specified earlier
327 (or to share the current terminal, if none was specified). */
328
329 new_tty ();
330
331 /* Changing the signal handlers for the inferior after
332 a vfork can also change them for the superior, so we don't mess
333 with signals here. See comments in
334 initialize_signals for how we get the right signal handlers
335 for the inferior. */
336
337 #ifdef USE_PROC_FS
338 /* Use SVR4 /proc interface */
339 proc_set_exec_trap ();
340 #else
341 /* "Trace me, Dr. Memory!" */
342 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
343 #endif
344
345 /* There is no execlpe call, so we have to set the environment
346 for our child in the global variable. If we've vforked, this
347 clobbers the parent, but environ is restored a few lines down
348 in the parent. By the way, yes we do need to look down the
349 path to find $SHELL. Rich Pixley says so, and I agree. */
350 environ = env;
351 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
352
353 fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
354 safe_strerror (errno));
355 fflush (stderr);
356 _exit (0177);
357 }
358
359 /* Restore our environment in case a vforked child clob'd it. */
360 environ = save_our_env;
361
362 /* Now that we have a child process, make it our target. */
363 push_target (&child_ops);
364
365 #ifdef CREATE_INFERIOR_HOOK
366 CREATE_INFERIOR_HOOK (pid);
367 #endif
368
369 /* The process was started by the fork that created it,
370 but it will have stopped one instruction after execing the shell.
371 Here we must get it up to actual execution of the real program. */
372
373 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
374
375 clear_proceed_status ();
376
377 /* We will get a trace trap after one instruction.
378 Continue it automatically. Eventually (after shell does an exec)
379 it will get another trace trap. Then insert breakpoints and continue. */
380
381 #ifdef START_INFERIOR_TRAPS_EXPECTED
382 pending_execs = START_INFERIOR_TRAPS_EXPECTED;
383 #else
384 pending_execs = 2;
385 #endif
386
387 init_wait_for_inferior ();
388
389 /* Set up the "saved terminal modes" of the inferior
390 based on what modes we are starting it with. */
391 target_terminal_init ();
392
393 /* Install inferior's terminal modes. */
394 target_terminal_inferior ();
395
396 while (1)
397 {
398 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
399 wait_for_inferior ();
400 if (stop_signal != SIGTRAP)
401 {
402 /* Let shell child handle its own signals in its own way */
403 /* FIXME, what if child has exit()ed? Must exit loop somehow */
404 resume (0, stop_signal);
405 }
406 else
407 {
408 /* We handle SIGTRAP, however; it means child did an exec. */
409 if (0 == --pending_execs)
410 break;
411 resume (0, 0); /* Just make it go on */
412 }
413 }
414 stop_soon_quietly = 0;
415
416 /* We are now in the child process of interest, having exec'd the
417 correct program, and are poised at the first instruction of the
418 new program. */
419 #ifdef SOLIB_CREATE_INFERIOR_HOOK
420 SOLIB_CREATE_INFERIOR_HOOK (pid);
421 #endif
422
423 /* Pedal to the metal. Away we go. */
424 proceed ((CORE_ADDR) -1, 0, 0);
425 }
426
427 static void
428 child_mourn_inferior ()
429 {
430 unpush_target (&child_ops);
431 generic_mourn_inferior ();
432 }
433
434 static int
435 child_can_run ()
436 {
437 return(1);
438 }
439 \f
440 struct target_ops child_ops = {
441 "child", /* to_shortname */
442 "Unix child process", /* to_longname */
443 "Unix child process (started by the \"run\" command).", /* to_doc */
444 child_open, /* to_open */
445 0, /* to_close */
446 child_attach, /* to_attach */
447 child_detach, /* to_detach */
448 child_resume, /* to_resume */
449 child_wait, /* to_wait */
450 fetch_inferior_registers, /* to_fetch_registers */
451 store_inferior_registers, /* to_store_registers */
452 child_prepare_to_store, /* to_prepare_to_store */
453 child_xfer_memory, /* to_xfer_memory */
454 child_files_info, /* to_files_info */
455 memory_insert_breakpoint, /* to_insert_breakpoint */
456 memory_remove_breakpoint, /* to_remove_breakpoint */
457 terminal_init_inferior, /* to_terminal_init */
458 terminal_inferior, /* to_terminal_inferior */
459 terminal_ours_for_output, /* to_terminal_ours_for_output */
460 terminal_ours, /* to_terminal_ours */
461 child_terminal_info, /* to_terminal_info */
462 kill_inferior, /* to_kill */
463 0, /* to_load */
464 0, /* to_lookup_symbol */
465 child_create_inferior, /* to_create_inferior */
466 child_mourn_inferior, /* to_mourn_inferior */
467 child_can_run, /* to_can_run */
468 process_stratum, /* to_stratum */
469 0, /* to_next */
470 1, /* to_has_all_memory */
471 1, /* to_has_memory */
472 1, /* to_has_stack */
473 1, /* to_has_registers */
474 1, /* to_has_execution */
475 0, /* sections */
476 0, /* sections_end */
477 OPS_MAGIC /* to_magic */
478 };
479
480 void
481 _initialize_inftarg ()
482 {
483 add_target (&child_ops);
484 }
This page took 0.039633 seconds and 5 git commands to generate.