]>
Commit | Line | Data |
---|---|---|
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 | ||
5 | This file is part of GDB. | |
6 | ||
dcc8abce | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
dcc8abce JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
dcc8abce | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
dcc8abce JG |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, 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 |
30 | static void |
31 | child_prepare_to_store PARAMS ((void)); | |
32 | ||
de43d7d0 | 33 | #ifndef CHILD_WAIT |
dcc8abce | 34 | static int |
100f92e2 | 35 | child_wait PARAMS ((int, int *)); |
de43d7d0 | 36 | #endif /* CHILD_WAIT */ |
dcc8abce JG |
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)); | |
bd5635a1 | 46 | |
310cc570 RP |
47 | static void |
48 | child_attach PARAMS ((char *, int)); | |
49 | ||
de43d7d0 SG |
50 | static void |
51 | ptrace_me PARAMS ((void)); | |
52 | ||
53 | static void | |
54 | ptrace_him PARAMS ((int)); | |
55 | ||
310cc570 RP |
56 | static void |
57 | child_create_inferior PARAMS ((char *, char *, char **)); | |
58 | ||
59 | static void | |
60 | child_mourn_inferior PARAMS ((void)); | |
61 | ||
3aa6856a JG |
62 | static int |
63 | child_can_run PARAMS ((void)); | |
64 | ||
310cc570 RP |
65 | extern char **environ; |
66 | ||
bd5635a1 RP |
67 | /* Forward declaration */ |
68 | extern 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 | 75 | static int |
de43d7d0 SG |
76 | child_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 | |
108 | static void | |
109 | child_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 | |
157 | static void | |
158 | child_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 | 193 | static void |
bd5635a1 RP |
194 | child_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 | ||
203 | static void | |
dcc8abce JG |
204 | child_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 |
212 | static void |
213 | child_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 | ||
223 | static void | |
224 | ptrace_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 | ||
233 | static void | |
234 | ptrace_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 |
245 | static void |
246 | child_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 | ||
257 | static void | |
258 | child_mourn_inferior () | |
259 | { | |
260 | unpush_target (&child_ops); | |
261 | generic_mourn_inferior (); | |
262 | } | |
263 | ||
264 | static int | |
265 | child_can_run () | |
266 | { | |
267 | return(1); | |
268 | } | |
3aa6856a | 269 | \f |
bd5635a1 | 270 | struct 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 | ||
311 | void | |
312 | _initialize_inftarg () | |
313 | { | |
314 | add_target (&child_ops); | |
315 | } |