* mh-hp300: Don't define CFLAGS to empty. Why should hp300 be
[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
28 #include <signal.h>
29
30 static void
31 child_prepare_to_store PARAMS ((void));
32
33 #ifndef CHILD_WAIT
34 static int
35 child_wait PARAMS ((int *));
36 #endif /* CHILD_WAIT */
37
38 static void
39 child_open PARAMS ((char *, int));
40
41 static void
42 child_files_info PARAMS ((struct target_ops *));
43
44 static void
45 child_detach PARAMS ((char *, int));
46
47 static void
48 child_attach PARAMS ((char *, int));
49
50 static void
51 ptrace_me PARAMS ((void));
52
53 static void
54 ptrace_him PARAMS ((int));
55
56 static void
57 child_create_inferior PARAMS ((char *, char *, char **));
58
59 static void
60 child_mourn_inferior PARAMS ((void));
61
62 static int
63 child_can_run PARAMS ((void));
64
65 extern char **environ;
66
67 /* Forward declaration */
68 extern struct target_ops child_ops;
69
70 #ifndef CHILD_WAIT
71
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
75 static int
76 child_wait (pid, status)
77 int pid;
78 int *status;
79 {
80 int save_errno;
81
82 do {
83 if (attach_flag)
84 set_sigint_trap(); /* Causes SIGINT to be passed on to the
85 attached process. */
86 pid = wait (status);
87 save_errno = errno;
88
89 if (attach_flag)
90 clear_sigint_trap();
91
92 if (pid == -1)
93 {
94 if (save_errno == EINTR)
95 continue;
96 fprintf (stderr, "Child process unexpectedly missing: %s.\n",
97 safe_strerror (save_errno));
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 }
104 #endif /* CHILD_WAIT */
105
106 /* Attach to process PID, then initialize for debugging it. */
107
108 static void
109 child_attach (args, from_tty)
110 char *args;
111 int from_tty;
112 {
113 char *exec_file;
114 int pid;
115
116 if (!args)
117 error_no_arg ("process-id to attach");
118
119 #ifndef ATTACH_DETACH
120 error ("Can't attach to a process on this machine.");
121 #else
122 pid = atoi (args);
123
124 if (pid == getpid()) /* Trying to masturbate? */
125 error ("I refuse to debug myself!");
126
127 if (from_tty)
128 {
129 exec_file = (char *) get_exec_file (0);
130
131 if (exec_file)
132 printf ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
133 else
134 printf ("Attaching to %s\n", target_pid_to_str (pid));
135
136 fflush (stdout);
137 }
138
139 attach (pid);
140 inferior_pid = pid;
141 push_target (&child_ops);
142 #endif /* ATTACH_DETACH */
143 }
144
145
146 /* Take a program previously attached to and detaches it.
147 The program resumes execution and will no longer stop
148 on signals, etc. We'd better not have left any breakpoints
149 in the program or it'll die when it hits one. For this
150 to work, it may be necessary for the process to have been
151 previously attached. It *might* work if the program was
152 started via the normal ptrace (PTRACE_TRACEME). */
153
154 static void
155 child_detach (args, from_tty)
156 char *args;
157 int from_tty;
158 {
159 int siggnal = 0;
160
161 #ifdef ATTACH_DETACH
162 if (from_tty)
163 {
164 char *exec_file = get_exec_file (0);
165 if (exec_file == 0)
166 exec_file = "";
167 printf ("Detaching from program: %s %s\n", exec_file,
168 target_pid_to_str (inferior_pid));
169 fflush (stdout);
170 }
171 if (args)
172 siggnal = atoi (args);
173
174 detach (siggnal);
175 inferior_pid = 0;
176 unpush_target (&child_ops); /* Pop out of handling an inferior */
177 #else
178 error ("This version of Unix does not support detaching a process.");
179 #endif
180 }
181
182 /* Get ready to modify the registers array. On machines which store
183 individual registers, this doesn't need to do anything. On machines
184 which store all the registers in one fell swoop, this makes sure
185 that registers contains all the registers from the program being
186 debugged. */
187
188 static void
189 child_prepare_to_store ()
190 {
191 #ifdef CHILD_PREPARE_TO_STORE
192 CHILD_PREPARE_TO_STORE ();
193 #endif
194 }
195
196 /* Print status information about what we're accessing. */
197
198 static void
199 child_files_info (ignore)
200 struct target_ops *ignore;
201 {
202 printf ("\tUsing the running image of %s %s.\n",
203 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
204 }
205
206 /* ARGSUSED */
207 static void
208 child_open (arg, from_tty)
209 char *arg;
210 int from_tty;
211 {
212 error ("Use the \"run\" command to start a Unix child process.");
213 }
214
215 /* Stub function which causes the inferior that runs it, to be ptrace-able
216 by its parent process. */
217
218 static void
219 ptrace_me ()
220 {
221 /* "Trace me, Dr. Memory!" */
222 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
223 }
224
225 /* Stub function which causes the GDB that runs it, to start ptrace-ing
226 the child process. */
227
228 static void
229 ptrace_him (pid)
230 int pid;
231 {
232 push_target (&child_ops);
233 }
234
235 /* Start an inferior Unix child process and sets inferior_pid to its pid.
236 EXEC_FILE is the file to run.
237 ALLARGS is a string containing the arguments to the program.
238 ENV is the environment vector to pass. Errors reported with error(). */
239
240 static void
241 child_create_inferior (exec_file, allargs, env)
242 char *exec_file;
243 char *allargs;
244 char **env;
245 {
246 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him);
247 /* We are at the first instruction we care about. */
248 /* Pedal to the metal... */
249 proceed ((CORE_ADDR) -1, 0, 0);
250 }
251
252 static void
253 child_mourn_inferior ()
254 {
255 unpush_target (&child_ops);
256 generic_mourn_inferior ();
257 }
258
259 static int
260 child_can_run ()
261 {
262 return(1);
263 }
264 \f
265 struct target_ops child_ops = {
266 "child", /* to_shortname */
267 "Unix child process", /* to_longname */
268 "Unix child process (started by the \"run\" command).", /* to_doc */
269 child_open, /* to_open */
270 0, /* to_close */
271 child_attach, /* to_attach */
272 child_detach, /* to_detach */
273 child_resume, /* to_resume */
274 child_wait, /* to_wait */
275 fetch_inferior_registers, /* to_fetch_registers */
276 store_inferior_registers, /* to_store_registers */
277 child_prepare_to_store, /* to_prepare_to_store */
278 child_xfer_memory, /* to_xfer_memory */
279 child_files_info, /* to_files_info */
280 memory_insert_breakpoint, /* to_insert_breakpoint */
281 memory_remove_breakpoint, /* to_remove_breakpoint */
282 terminal_init_inferior, /* to_terminal_init */
283 terminal_inferior, /* to_terminal_inferior */
284 terminal_ours_for_output, /* to_terminal_ours_for_output */
285 terminal_ours, /* to_terminal_ours */
286 child_terminal_info, /* to_terminal_info */
287 kill_inferior, /* to_kill */
288 0, /* to_load */
289 0, /* to_lookup_symbol */
290 child_create_inferior, /* to_create_inferior */
291 child_mourn_inferior, /* to_mourn_inferior */
292 child_can_run, /* to_can_run */
293 0, /* to_notice_signals */
294 process_stratum, /* to_stratum */
295 0, /* to_next */
296 1, /* to_has_all_memory */
297 1, /* to_has_memory */
298 1, /* to_has_stack */
299 1, /* to_has_registers */
300 1, /* to_has_execution */
301 0, /* sections */
302 0, /* sections_end */
303 OPS_MAGIC /* to_magic */
304 };
305
306 void
307 _initialize_inftarg ()
308 {
309 add_target (&child_ops);
310 }