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.
5 This file is part of GDB.
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.
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.
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. */
22 #include "frame.h" /* required by inferior.h */
31 child_prepare_to_store
PARAMS ((void));
35 child_wait
PARAMS ((int, struct target_waitstatus
*));
36 #endif /* CHILD_WAIT */
39 child_open
PARAMS ((char *, int));
42 child_files_info
PARAMS ((struct target_ops
*));
45 child_detach
PARAMS ((char *, int));
48 child_attach
PARAMS ((char *, int));
51 ptrace_me
PARAMS ((void));
54 ptrace_him
PARAMS ((int));
57 child_create_inferior
PARAMS ((char *, char *, char **));
60 child_mourn_inferior
PARAMS ((void));
63 child_can_run
PARAMS ((void));
65 extern char **environ
;
67 /* Forward declaration */
68 extern struct target_ops child_ops
;
70 /* Convert host signal to our signals. */
72 target_signal_from_host (hostsig
)
75 /* A switch statement would make sense but would require special kludges
76 to deal with the cases where more than one signal has the same number. */
78 if (hostsig
== 0) return TARGET_SIGNAL_0
;
81 if (hostsig
== SIGHUP
) return TARGET_SIGNAL_HUP
;
84 if (hostsig
== SIGINT
) return TARGET_SIGNAL_INT
;
87 if (hostsig
== SIGQUIT
) return TARGET_SIGNAL_QUIT
;
90 if (hostsig
== SIGILL
) return TARGET_SIGNAL_ILL
;
93 if (hostsig
== SIGTRAP
) return TARGET_SIGNAL_TRAP
;
96 if (hostsig
== SIGABRT
) return TARGET_SIGNAL_ABRT
;
99 if (hostsig
== SIGEMT
) return TARGET_SIGNAL_EMT
;
102 if (hostsig
== SIGFPE
) return TARGET_SIGNAL_FPE
;
104 #if defined (SIGKILL)
105 if (hostsig
== SIGKILL
) return TARGET_SIGNAL_KILL
;
108 if (hostsig
== SIGBUS
) return TARGET_SIGNAL_BUS
;
110 #if defined (SIGSEGV)
111 if (hostsig
== SIGSEGV
) return TARGET_SIGNAL_SEGV
;
114 if (hostsig
== SIGSYS
) return TARGET_SIGNAL_SYS
;
116 #if defined (SIGPIPE)
117 if (hostsig
== SIGPIPE
) return TARGET_SIGNAL_PIPE
;
119 #if defined (SIGALRM)
120 if (hostsig
== SIGALRM
) return TARGET_SIGNAL_ALRM
;
122 #if defined (SIGTERM)
123 if (hostsig
== SIGTERM
) return TARGET_SIGNAL_TERM
;
125 #if defined (SIGUSR1)
126 if (hostsig
== SIGUSR1
) return TARGET_SIGNAL_USR1
;
128 #if defined (SIGUSR2)
129 if (hostsig
== SIGUSR2
) return TARGET_SIGNAL_USR2
;
132 if (hostsig
== SIGCLD
) return TARGET_SIGNAL_CHLD
;
134 #if defined (SIGCHLD)
135 if (hostsig
== SIGCHLD
) return TARGET_SIGNAL_CHLD
;
138 if (hostsig
== SIGPWR
) return TARGET_SIGNAL_PWR
;
140 #if defined (SIGWINCH)
141 if (hostsig
== SIGWINCH
) return TARGET_SIGNAL_WINCH
;
144 if (hostsig
== SIGURG
) return TARGET_SIGNAL_URG
;
147 if (hostsig
== SIGIO
) return TARGET_SIGNAL_IO
;
149 #if defined (SIGPOLL)
150 if (hostsig
== SIGPOLL
) return TARGET_SIGNAL_POLL
;
152 #if defined (SIGSTOP)
153 if (hostsig
== SIGSTOP
) return TARGET_SIGNAL_STOP
;
155 #if defined (SIGTSTP)
156 if (hostsig
== SIGTSTP
) return TARGET_SIGNAL_TSTP
;
158 #if defined (SIGCONT)
159 if (hostsig
== SIGCONT
) return TARGET_SIGNAL_CONT
;
161 #if defined (SIGTTIN)
162 if (hostsig
== SIGTTIN
) return TARGET_SIGNAL_TTIN
;
164 #if defined (SIGTTOU)
165 if (hostsig
== SIGTTOU
) return TARGET_SIGNAL_TTOU
;
167 #if defined (SIGVTALRM)
168 if (hostsig
== SIGVTALRM
) return TARGET_SIGNAL_VTALRM
;
170 #if defined (SIGPROF)
171 if (hostsig
== SIGPROF
) return TARGET_SIGNAL_PROF
;
173 #if defined (SIGXCPU)
174 if (hostsig
== SIGXCPU
) return TARGET_SIGNAL_XCPU
;
176 #if defined (SIGXFSZ)
177 if (hostsig
== SIGXFSZ
) return TARGET_SIGNAL_XFSZ
;
179 #if defined (SIGWIND)
180 if (hostsig
== SIGWIND
) return TARGET_SIGNAL_WIND
;
182 #if defined (SIGPHONE)
183 if (hostsig
== SIGPHONE
) return TARGET_SIGNAL_PHONE
;
185 #if defined (SIGLOST)
186 if (hostsig
== SIGLOST
) return TARGET_SIGNAL_LOST
;
188 #if defined (SIGWAITING)
189 if (hostsig
== SIGWAITING
) return TARGET_SIGNAL_WAITING
;
192 if (hostsig
== SIGLWP
) return TARGET_SIGNAL_LWP
;
194 #if defined (SIGDANGER)
195 if (hostsig
== SIGDANGER
) return TARGET_SIGNAL_DANGER
;
197 #if defined (SIGGRANT)
198 if (hostsig
== SIGGRANT
) return TARGET_SIGNAL_GRANT
;
200 #if defined (SIGRETRACT)
201 if (hostsig
== SIGRETRACT
) return TARGET_SIGNAL_RETRACT
;
204 if (hostsig
== SIGMSG
) return TARGET_SIGNAL_MSG
;
206 #if defined (SIGSOUND)
207 if (hostsig
== SIGSOUND
) return TARGET_SIGNAL_SOUND
;
210 if (hostsig
== SIGSAK
) return TARGET_SIGNAL_SAK
;
212 return TARGET_SIGNAL_UNKNOWN
;
216 target_signal_to_host (oursig
)
217 enum target_signal oursig
;
221 case TARGET_SIGNAL_0
: return 0;
224 case TARGET_SIGNAL_HUP
: return SIGHUP
;
227 case TARGET_SIGNAL_INT
: return SIGINT
;
229 #if defined (SIGQUIT)
230 case TARGET_SIGNAL_QUIT
: return SIGQUIT
;
233 case TARGET_SIGNAL_ILL
: return SIGILL
;
235 #if defined (SIGTRAP)
236 case TARGET_SIGNAL_TRAP
: return SIGTRAP
;
238 #if defined (SIGABRT)
239 case TARGET_SIGNAL_ABRT
: return SIGABRT
;
242 case TARGET_SIGNAL_EMT
: return SIGEMT
;
245 case TARGET_SIGNAL_FPE
: return SIGFPE
;
247 #if defined (SIGKILL)
248 case TARGET_SIGNAL_KILL
: return SIGKILL
;
251 case TARGET_SIGNAL_BUS
: return SIGBUS
;
253 #if defined (SIGSEGV)
254 case TARGET_SIGNAL_SEGV
: return SIGSEGV
;
257 case TARGET_SIGNAL_SYS
: return SIGSYS
;
259 #if defined (SIGPIPE)
260 case TARGET_SIGNAL_PIPE
: return SIGPIPE
;
262 #if defined (SIGALRM)
263 case TARGET_SIGNAL_ALRM
: return SIGALRM
;
265 #if defined (SIGTERM)
266 case TARGET_SIGNAL_TERM
: return SIGTERM
;
268 #if defined (SIGUSR1)
269 case TARGET_SIGNAL_USR1
: return SIGUSR1
;
271 #if defined (SIGUSR2)
272 case TARGET_SIGNAL_USR2
: return SIGUSR2
;
274 #if defined (SIGCHLD) || defined (SIGCLD)
275 case TARGET_SIGNAL_CHLD
:
276 #if defined (SIGCHLD)
281 #endif /* SIGCLD or SIGCHLD */
283 case TARGET_SIGNAL_PWR
: return SIGPWR
;
285 #if defined (SIGWINCH)
286 case TARGET_SIGNAL_WINCH
: return SIGWINCH
;
289 case TARGET_SIGNAL_URG
: return SIGURG
;
292 case TARGET_SIGNAL_IO
: return SIGIO
;
294 #if defined (SIGPOLL)
295 case TARGET_SIGNAL_POLL
: return SIGPOLL
;
297 #if defined (SIGSTOP)
298 case TARGET_SIGNAL_STOP
: return SIGSTOP
;
300 #if defined (SIGTSTP)
301 case TARGET_SIGNAL_TSTP
: return SIGTSTP
;
303 #if defined (SIGCONT)
304 case TARGET_SIGNAL_CONT
: return SIGCONT
;
306 #if defined (SIGTTIN)
307 case TARGET_SIGNAL_TTIN
: return SIGTTIN
;
309 #if defined (SIGTTOU)
310 case TARGET_SIGNAL_TTOU
: return SIGTTOU
;
312 #if defined (SIGVTALRM)
313 case TARGET_SIGNAL_VTALRM
: return SIGVTALRM
;
315 #if defined (SIGPROF)
316 case TARGET_SIGNAL_PROF
: return SIGPROF
;
318 #if defined (SIGXCPU)
319 case TARGET_SIGNAL_XCPU
: return SIGXCPU
;
321 #if defined (SIGXFSZ)
322 case TARGET_SIGNAL_XFSZ
: return SIGXFSZ
;
324 #if defined (SIGWIND)
325 case TARGET_SIGNAL_WIND
: return SIGWIND
;
327 #if defined (SIGPHONE)
328 case TARGET_SIGNAL_PHONE
: return SIGPHONE
;
330 #if defined (SIGLOST)
331 case TARGET_SIGNAL_LOST
: return SIGLOST
;
333 #if defined (SIGWAITING)
334 case TARGET_SIGNAL_WAITING
: return SIGWAITING
;
337 case TARGET_SIGNAL_LWP
: return SIGLWP
;
339 #if defined (SIGDANGER)
340 case TARGET_SIGNAL_DANGER
: return SIGDANGER
;
342 #if defined (SIGGRANT)
343 case TARGET_SIGNAL_GRANT
: return SIGGRANT
;
345 #if defined (SIGRETRACT)
346 case TARGET_SIGNAL_RETRACT
: return SIGRETRACT
;
349 case TARGET_SIGNAL_MSG
: return SIGMSG
;
351 #if defined (SIGSOUND)
352 case TARGET_SIGNAL_SOUND
: return SIGSOUND
;
355 case TARGET_SIGNAL_SAK
: return SIGSAK
;
358 /* The user might be trying to do "signal SIGSAK" where this system
359 doesn't have SIGSAK. */
360 warning ("Signal %s does not exist on this system.\n",
361 target_signal_to_name (oursig
));
366 /* Helper function for child_wait and the Lynx derivatives of child_wait.
367 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
368 translation of that in OURSTATUS. */
370 store_waitstatus (ourstatus
, hoststatus
)
371 struct target_waitstatus
*ourstatus
;
374 if (WIFEXITED (hoststatus
))
376 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
377 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
379 else if (!WIFSTOPPED (hoststatus
))
381 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
382 ourstatus
->value
.sig
= target_signal_from_host (WTERMSIG (hoststatus
));
386 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
387 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (hoststatus
));
393 /* Wait for child to do something. Return pid of child, or -1 in case
394 of error; store status through argument pointer OURSTATUS. */
397 child_wait (pid
, ourstatus
)
399 struct target_waitstatus
*ourstatus
;
406 set_sigint_trap(); /* Causes SIGINT to be passed on to the
408 pid
= wait (&status
);
416 if (save_errno
== EINTR
)
418 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
419 safe_strerror (save_errno
));
420 /* Claim it exited with unknown signal. */
421 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
422 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
425 } while (pid
!= inferior_pid
); /* Some other child died or stopped */
426 store_waitstatus (ourstatus
, status
);
429 #endif /* CHILD_WAIT */
431 /* Attach to process PID, then initialize for debugging it. */
434 child_attach (args
, from_tty
)
439 error_no_arg ("process-id to attach");
441 #ifndef ATTACH_DETACH
442 error ("Can't attach to a process on this machine.");
450 if (pid
== getpid()) /* Trying to masturbate? */
451 error ("I refuse to debug myself!");
455 exec_file
= (char *) get_exec_file (0);
458 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
459 target_pid_to_str (pid
));
461 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
463 gdb_flush (gdb_stdout
);
468 push_target (&child_ops
);
470 #endif /* ATTACH_DETACH */
474 /* Take a program previously attached to and detaches it.
475 The program resumes execution and will no longer stop
476 on signals, etc. We'd better not have left any breakpoints
477 in the program or it'll die when it hits one. For this
478 to work, it may be necessary for the process to have been
479 previously attached. It *might* work if the program was
480 started via the normal ptrace (PTRACE_TRACEME). */
483 child_detach (args
, from_tty
)
493 char *exec_file
= get_exec_file (0);
496 printf_unfiltered ("Detaching from program: %s %s\n", exec_file
,
497 target_pid_to_str (inferior_pid
));
498 gdb_flush (gdb_stdout
);
501 siggnal
= atoi (args
);
505 unpush_target (&child_ops
);
508 error ("This version of Unix does not support detaching a process.");
512 /* Get ready to modify the registers array. On machines which store
513 individual registers, this doesn't need to do anything. On machines
514 which store all the registers in one fell swoop, this makes sure
515 that registers contains all the registers from the program being
519 child_prepare_to_store ()
521 #ifdef CHILD_PREPARE_TO_STORE
522 CHILD_PREPARE_TO_STORE ();
526 /* Print status information about what we're accessing. */
529 child_files_info (ignore
)
530 struct target_ops
*ignore
;
532 printf_unfiltered ("\tUsing the running image of %s %s.\n",
533 attach_flag
? "attached": "child", target_pid_to_str (inferior_pid
));
538 child_open (arg
, from_tty
)
542 error ("Use the \"run\" command to start a Unix child process.");
545 /* Stub function which causes the inferior that runs it, to be ptrace-able
546 by its parent process. */
551 /* "Trace me, Dr. Memory!" */
552 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
555 /* Stub function which causes the GDB that runs it, to start ptrace-ing
556 the child process. */
562 push_target (&child_ops
);
564 #ifdef START_INFERIOR_TRAPS_EXPECTED
565 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
567 /* One trap to exec the shell, one to exec the program being debugged. */
568 startup_inferior (2);
572 /* Start an inferior Unix child process and sets inferior_pid to its pid.
573 EXEC_FILE is the file to run.
574 ALLARGS is a string containing the arguments to the program.
575 ENV is the environment vector to pass. Errors reported with error(). */
578 child_create_inferior (exec_file
, allargs
, env
)
583 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
);
584 /* We are at the first instruction we care about. */
585 /* Pedal to the metal... */
586 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
590 child_mourn_inferior ()
592 unpush_target (&child_ops
);
593 generic_mourn_inferior ();
602 struct target_ops child_ops
= {
603 "child", /* to_shortname */
604 "Unix child process", /* to_longname */
605 "Unix child process (started by the \"run\" command).", /* to_doc */
606 child_open
, /* to_open */
608 child_attach
, /* to_attach */
609 child_detach
, /* to_detach */
610 child_resume
, /* to_resume */
611 child_wait
, /* to_wait */
612 fetch_inferior_registers
, /* to_fetch_registers */
613 store_inferior_registers
, /* to_store_registers */
614 child_prepare_to_store
, /* to_prepare_to_store */
615 child_xfer_memory
, /* to_xfer_memory */
616 child_files_info
, /* to_files_info */
617 memory_insert_breakpoint
, /* to_insert_breakpoint */
618 memory_remove_breakpoint
, /* to_remove_breakpoint */
619 terminal_init_inferior
, /* to_terminal_init */
620 terminal_inferior
, /* to_terminal_inferior */
621 terminal_ours_for_output
, /* to_terminal_ours_for_output */
622 terminal_ours
, /* to_terminal_ours */
623 child_terminal_info
, /* to_terminal_info */
624 kill_inferior
, /* to_kill */
626 0, /* to_lookup_symbol */
627 child_create_inferior
, /* to_create_inferior */
628 child_mourn_inferior
, /* to_mourn_inferior */
629 child_can_run
, /* to_can_run */
630 0, /* to_notice_signals */
631 process_stratum
, /* to_stratum */
633 1, /* to_has_all_memory */
634 1, /* to_has_memory */
635 1, /* to_has_stack */
636 1, /* to_has_registers */
637 1, /* to_has_execution */
639 0, /* sections_end */
640 OPS_MAGIC
/* to_magic */
644 _initialize_inftarg ()
646 add_target (&child_ops
);