How many times do I have to do this...
[deliverable/binutils-gdb.git] / gdb / inftarg.c
CommitLineData
3aa6856a 1/* Target-vector operations for controlling Unix child processes, for GDB.
310cc570 2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3 Contributed by Cygnus Support.
4
5This file is part of GDB.
6
dcc8abce 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
dcc8abce
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
dcc8abce 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
dcc8abce
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 20
bd5635a1 21#include "defs.h"
bd5635a1
RP
22#include "frame.h" /* required by inferior.h */
23#include "inferior.h"
24#include "target.h"
25#include "wait.h"
26#include "gdbcore.h"
100f92e2 27#include "command.h"
310cc570
RP
28#include <signal.h>
29
dcc8abce
JG
30static void
31child_prepare_to_store PARAMS ((void));
32
de43d7d0 33#ifndef CHILD_WAIT
dcc8abce 34static int
100f92e2 35child_wait PARAMS ((int, int *));
de43d7d0 36#endif /* CHILD_WAIT */
dcc8abce
JG
37
38static void
39child_open PARAMS ((char *, int));
40
41static void
42child_files_info PARAMS ((struct target_ops *));
43
44static void
45child_detach PARAMS ((char *, int));
bd5635a1 46
310cc570
RP
47static void
48child_attach PARAMS ((char *, int));
49
de43d7d0
SG
50static void
51ptrace_me PARAMS ((void));
52
53static void
54ptrace_him PARAMS ((int));
55
310cc570
RP
56static void
57child_create_inferior PARAMS ((char *, char *, char **));
58
59static void
60child_mourn_inferior PARAMS ((void));
61
3aa6856a
JG
62static int
63child_can_run PARAMS ((void));
64
310cc570
RP
65extern char **environ;
66
bd5635a1
RP
67/* Forward declaration */
68extern struct target_ops child_ops;
69
de43d7d0
SG
70#ifndef CHILD_WAIT
71
bd5635a1
RP
72/* Wait for child to do something. Return pid of child, or -1 in case
73 of error; store status through argument pointer STATUS. */
74
dcc8abce 75static int
de43d7d0
SG
76child_wait (pid, status)
77 int pid;
bd5635a1
RP
78 int *status;
79{
de43d7d0 80 int save_errno;
bd5635a1
RP
81
82 do {
de43d7d0
SG
83 if (attach_flag)
84 set_sigint_trap(); /* Causes SIGINT to be passed on to the
85 attached process. */
bd5635a1 86 pid = wait (status);
de43d7d0
SG
87 save_errno = errno;
88
89 if (attach_flag)
90 clear_sigint_trap();
91
92 if (pid == -1)
bd5635a1 93 {
de43d7d0
SG
94 if (save_errno == EINTR)
95 continue;
96 fprintf (stderr, "Child process unexpectedly missing: %s.\n",
97 safe_strerror (save_errno));
bd5635a1
RP
98 *status = 42; /* Claim it exited with signal 42 */
99 return -1;
100 }
101 } while (pid != inferior_pid); /* Some other child died or stopped */
102 return pid;
103}
de43d7d0 104#endif /* CHILD_WAIT */
bd5635a1 105
836e343b 106/* Attach to process PID, then initialize for debugging it. */
310cc570
RP
107
108static void
109child_attach (args, from_tty)
110 char *args;
111 int from_tty;
112{
310cc570
RP
113 if (!args)
114 error_no_arg ("process-id to attach");
115
116#ifndef ATTACH_DETACH
117 error ("Can't attach to a process on this machine.");
118#else
100f92e2
JK
119 {
120 char *exec_file;
121 int pid;
310cc570 122
100f92e2 123 pid = atoi (args);
310cc570 124
100f92e2
JK
125 if (pid == getpid()) /* Trying to masturbate? */
126 error ("I refuse to debug myself!");
310cc570 127
100f92e2
JK
128 if (from_tty)
129 {
130 exec_file = (char *) get_exec_file (0);
310cc570 131
100f92e2
JK
132 if (exec_file)
133 printf ("Attaching to program `%s', %s\n", exec_file,
134 target_pid_to_str (pid));
135 else
136 printf ("Attaching to %s\n", target_pid_to_str (pid));
137
138 fflush (stdout);
139 }
310cc570 140
100f92e2
JK
141 attach (pid);
142 inferior_pid = pid;
143 push_target (&child_ops);
144 }
310cc570
RP
145#endif /* ATTACH_DETACH */
146}
147
3aa6856a
JG
148
149/* Take a program previously attached to and detaches it.
150 The program resumes execution and will no longer stop
151 on signals, etc. We'd better not have left any breakpoints
152 in the program or it'll die when it hits one. For this
153 to work, it may be necessary for the process to have been
154 previously attached. It *might* work if the program was
155 started via the normal ptrace (PTRACE_TRACEME). */
bd5635a1
RP
156
157static void
158child_detach (args, from_tty)
159 char *args;
160 int from_tty;
161{
bd5635a1 162#ifdef ATTACH_DETACH
100f92e2
JK
163 {
164 int siggnal = 0;
165
166 if (from_tty)
167 {
168 char *exec_file = get_exec_file (0);
169 if (exec_file == 0)
170 exec_file = "";
171 printf ("Detaching from program: %s %s\n", exec_file,
172 target_pid_to_str (inferior_pid));
173 fflush (stdout);
174 }
175 if (args)
176 siggnal = atoi (args);
177
178 detach (siggnal);
179 inferior_pid = 0;
180 unpush_target (&child_ops);
181 }
bd5635a1 182#else
100f92e2 183 error ("This version of Unix does not support detaching a process.");
bd5635a1
RP
184#endif
185}
186
187/* Get ready to modify the registers array. On machines which store
188 individual registers, this doesn't need to do anything. On machines
189 which store all the registers in one fell swoop, this makes sure
190 that registers contains all the registers from the program being
191 debugged. */
192
dcc8abce 193static void
bd5635a1
RP
194child_prepare_to_store ()
195{
196#ifdef CHILD_PREPARE_TO_STORE
197 CHILD_PREPARE_TO_STORE ();
198#endif
199}
200
bd5635a1
RP
201/* Print status information about what we're accessing. */
202
203static void
dcc8abce
JG
204child_files_info (ignore)
205 struct target_ops *ignore;
bd5635a1 206{
de43d7d0
SG
207 printf ("\tUsing the running image of %s %s.\n",
208 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
bd5635a1
RP
209}
210
e1ce8aa5 211/* ARGSUSED */
70dcc196
JK
212static void
213child_open (arg, from_tty)
214 char *arg;
215 int from_tty;
216{
217 error ("Use the \"run\" command to start a Unix child process.");
218}
219
de43d7d0
SG
220/* Stub function which causes the inferior that runs it, to be ptrace-able
221 by its parent process. */
222
223static void
224ptrace_me ()
225{
226 /* "Trace me, Dr. Memory!" */
227 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
228}
229
230/* Stub function which causes the GDB that runs it, to start ptrace-ing
231 the child process. */
232
233static void
234ptrace_him (pid)
235 int pid;
236{
237 push_target (&child_ops);
238}
239
310cc570
RP
240/* Start an inferior Unix child process and sets inferior_pid to its pid.
241 EXEC_FILE is the file to run.
242 ALLARGS is a string containing the arguments to the program.
243 ENV is the environment vector to pass. Errors reported with error(). */
244
310cc570
RP
245static void
246child_create_inferior (exec_file, allargs, env)
247 char *exec_file;
248 char *allargs;
249 char **env;
250{
de43d7d0
SG
251 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him);
252 /* We are at the first instruction we care about. */
253 /* Pedal to the metal... */
310cc570
RP
254 proceed ((CORE_ADDR) -1, 0, 0);
255}
256
257static void
258child_mourn_inferior ()
259{
260 unpush_target (&child_ops);
261 generic_mourn_inferior ();
262}
263
264static int
265child_can_run ()
266{
267 return(1);
268}
3aa6856a 269\f
bd5635a1 270struct target_ops child_ops = {
dcc8abce
JG
271 "child", /* to_shortname */
272 "Unix child process", /* to_longname */
273 "Unix child process (started by the \"run\" command).", /* to_doc */
274 child_open, /* to_open */
275 0, /* to_close */
276 child_attach, /* to_attach */
277 child_detach, /* to_detach */
278 child_resume, /* to_resume */
279 child_wait, /* to_wait */
280 fetch_inferior_registers, /* to_fetch_registers */
281 store_inferior_registers, /* to_store_registers */
282 child_prepare_to_store, /* to_prepare_to_store */
283 child_xfer_memory, /* to_xfer_memory */
284 child_files_info, /* to_files_info */
285 memory_insert_breakpoint, /* to_insert_breakpoint */
286 memory_remove_breakpoint, /* to_remove_breakpoint */
287 terminal_init_inferior, /* to_terminal_init */
288 terminal_inferior, /* to_terminal_inferior */
289 terminal_ours_for_output, /* to_terminal_ours_for_output */
290 terminal_ours, /* to_terminal_ours */
291 child_terminal_info, /* to_terminal_info */
292 kill_inferior, /* to_kill */
293 0, /* to_load */
294 0, /* to_lookup_symbol */
295 child_create_inferior, /* to_create_inferior */
296 child_mourn_inferior, /* to_mourn_inferior */
310cc570 297 child_can_run, /* to_can_run */
de43d7d0 298 0, /* to_notice_signals */
dcc8abce
JG
299 process_stratum, /* to_stratum */
300 0, /* to_next */
301 1, /* to_has_all_memory */
302 1, /* to_has_memory */
303 1, /* to_has_stack */
304 1, /* to_has_registers */
305 1, /* to_has_execution */
306 0, /* sections */
307 0, /* sections_end */
308 OPS_MAGIC /* to_magic */
bd5635a1
RP
309};
310
311void
312_initialize_inftarg ()
313{
314 add_target (&child_ops);
315}
This page took 0.145082 seconds and 4 git commands to generate.