]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inftarg.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / inftarg.c
1 /* Target-vector operations for controlling Unix child processes, for GDB.
2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 Contributed by Cygnus Support.
7
8 ## Contains temporary hacks..
9
10 This file is part of GDB.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA. */
26
27 #include "defs.h"
28 #include "frame.h" /* required by inferior.h */
29 #include "inferior.h"
30 #include "target.h"
31 #include "gdbcore.h"
32 #include "command.h"
33 #include "gdb_stat.h"
34 #include <signal.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include "gdb_wait.h"
38 #include "inflow.h"
39
40 extern struct symtab_and_line *child_enable_exception_callback (enum
41 exception_event_kind,
42 int);
43
44 extern struct exception_event_record
45 *child_get_current_exception_event (void);
46
47 extern void _initialize_inftarg (void);
48
49 static void child_prepare_to_store (void);
50
51 #ifndef CHILD_WAIT
52 static ptid_t child_wait (ptid_t, struct target_waitstatus *);
53 #endif /* CHILD_WAIT */
54
55 static void child_open (char *, int);
56
57 static void child_files_info (struct target_ops *);
58
59 static void child_detach (char *, int);
60
61 static void child_attach (char *, int);
62
63 #if !defined(CHILD_POST_ATTACH)
64 extern void child_post_attach (int);
65 #endif
66
67 static void ptrace_me (void);
68
69 static void ptrace_him (int);
70
71 static void child_create_inferior (char *, char *, char **, int);
72
73 static void child_mourn_inferior (void);
74
75 static int child_can_run (void);
76
77 static void child_stop (void);
78
79 #ifndef CHILD_THREAD_ALIVE
80 int child_thread_alive (ptid_t);
81 #endif
82
83 static void init_child_ops (void);
84
85 extern char **environ;
86
87 int child_suppress_run = 0; /* Non-zero if inftarg should pretend not to
88 be a runnable target. Used by targets
89 that can sit atop inftarg, such as HPUX
90 thread support. */
91
92 #ifndef CHILD_WAIT
93
94 /* Wait for child to do something. Return pid of child, or -1 in case
95 of error; store status through argument pointer OURSTATUS. */
96
97 static ptid_t
98 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
99 {
100 int save_errno;
101 int status;
102 char *execd_pathname = NULL;
103 int exit_status;
104 int syscall_id;
105 enum target_waitkind kind;
106 int pid;
107
108 do
109 {
110 set_sigint_trap (); /* Causes SIGINT to be passed on to the
111 attached process. */
112 set_sigio_trap ();
113
114 pid = wait (&status);
115
116 save_errno = errno;
117
118 clear_sigio_trap ();
119
120 clear_sigint_trap ();
121
122 if (pid == -1)
123 {
124 if (save_errno == EINTR)
125 continue;
126
127 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
128 safe_strerror (save_errno));
129
130 /* Claim it exited with unknown signal. */
131 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
132 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
133 return pid_to_ptid (-1);
134 }
135
136 /* Did it exit?
137 */
138 if (target_has_exited (pid, status, &exit_status))
139 {
140 /* ??rehrauer: For now, ignore this. */
141 continue;
142 }
143
144 if (!target_thread_alive (pid_to_ptid (pid)))
145 {
146 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
147 return pid_to_ptid (pid);
148 }
149 } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
150
151 store_waitstatus (ourstatus, status);
152 return pid_to_ptid (pid);
153 }
154 #endif /* CHILD_WAIT */
155
156 #ifndef CHILD_THREAD_ALIVE
157
158 /* Check to see if the given thread is alive.
159
160 FIXME: Is kill() ever the right way to do this? I doubt it, but
161 for now we're going to try and be compatable with the old thread
162 code. */
163 int
164 child_thread_alive (ptid_t ptid)
165 {
166 pid_t pid = PIDGET (ptid);
167
168 return (kill (pid, 0) != -1);
169 }
170
171 #endif
172
173 /* Attach to process PID, then initialize for debugging it. */
174
175 static void
176 child_attach (char *args, int from_tty)
177 {
178 char *exec_file;
179 int pid;
180 char *dummy;
181
182 if (!args)
183 error_no_arg (_("process-id to attach"));
184
185 dummy = args;
186 pid = strtol (args, &dummy, 0);
187 /* Some targets don't set errno on errors, grrr! */
188 if ((pid == 0) && (args == dummy))
189 error (_("Illegal process-id: %s."), args);
190
191 if (pid == getpid ()) /* Trying to masturbate? */
192 error (_("I refuse to debug myself!"));
193
194 if (from_tty)
195 {
196 exec_file = (char *) get_exec_file (0);
197
198 if (exec_file)
199 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
200 target_pid_to_str (pid_to_ptid (pid)));
201 else
202 printf_unfiltered (_("Attaching to %s\n"),
203 target_pid_to_str (pid_to_ptid (pid)));
204
205 gdb_flush (gdb_stdout);
206 }
207
208 attach (pid);
209
210 inferior_ptid = pid_to_ptid (pid);
211 push_target (&deprecated_child_ops);
212 }
213
214 #if !defined(CHILD_POST_ATTACH)
215 void
216 child_post_attach (int pid)
217 {
218 /* This version of Unix doesn't require a meaningful "post attach"
219 operation by a debugger. */
220 }
221 #endif
222
223 /* Take a program previously attached to and detaches it.
224 The program resumes execution and will no longer stop
225 on signals, etc. We'd better not have left any breakpoints
226 in the program or it'll die when it hits one. For this
227 to work, it may be necessary for the process to have been
228 previously attached. It *might* work if the program was
229 started via the normal ptrace (PTRACE_TRACEME). */
230
231 static void
232 child_detach (char *args, int from_tty)
233 {
234 int siggnal = 0;
235 int pid = PIDGET (inferior_ptid);
236
237 if (from_tty)
238 {
239 char *exec_file = get_exec_file (0);
240 if (exec_file == 0)
241 exec_file = "";
242 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
243 target_pid_to_str (pid_to_ptid (pid)));
244 gdb_flush (gdb_stdout);
245 }
246 if (args)
247 siggnal = atoi (args);
248
249 detach (siggnal);
250
251 inferior_ptid = null_ptid;
252 unpush_target (&deprecated_child_ops);
253 }
254
255 /* Get ready to modify the registers array. On machines which store
256 individual registers, this doesn't need to do anything. On machines
257 which store all the registers in one fell swoop, this makes sure
258 that registers contains all the registers from the program being
259 debugged. */
260
261 static void
262 child_prepare_to_store (void)
263 {
264 #ifdef CHILD_PREPARE_TO_STORE
265 CHILD_PREPARE_TO_STORE ();
266 #endif
267 }
268
269 /* Print status information about what we're accessing. */
270
271 static void
272 child_files_info (struct target_ops *ignore)
273 {
274 printf_unfiltered (_("\tUsing the running image of %s %s.\n"),
275 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
276 }
277
278 static void
279 child_open (char *arg, int from_tty)
280 {
281 error (_("Use the \"run\" command to start a Unix child process."));
282 }
283
284 /* Stub function which causes the inferior that runs it, to be ptrace-able
285 by its parent process. */
286
287 static void
288 ptrace_me (void)
289 {
290 /* "Trace me, Dr. Memory!" */
291 call_ptrace (0, 0, (PTRACE_TYPE_ARG3) 0, 0);
292 }
293
294 /* Stub function which causes the GDB that runs it, to start ptrace-ing
295 the child process. */
296
297 static void
298 ptrace_him (int pid)
299 {
300 push_target (&deprecated_child_ops);
301
302 /* On some targets, there must be some explicit synchronization
303 between the parent and child processes after the debugger
304 forks, and before the child execs the debuggee program. This
305 call basically gives permission for the child to exec.
306 */
307
308 target_acknowledge_created_inferior (pid);
309
310 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
311 * and will be 1 or 2 depending on whether we're starting
312 * without or with a shell.
313 */
314 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
315
316 /* On some targets, there must be some explicit actions taken after
317 the inferior has been started up.
318 */
319 target_post_startup_inferior (pid_to_ptid (pid));
320 }
321
322 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
323 EXEC_FILE is the file to run.
324 ALLARGS is a string containing the arguments to the program.
325 ENV is the environment vector to pass. Errors reported with error(). */
326
327 static void
328 child_create_inferior (char *exec_file, char *allargs, char **env,
329 int from_tty)
330 {
331 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
332 }
333
334 #if !defined(CHILD_POST_STARTUP_INFERIOR)
335 void
336 child_post_startup_inferior (ptid_t ptid)
337 {
338 /* This version of Unix doesn't require a meaningful "post startup inferior"
339 operation by a debugger.
340 */
341 }
342 #endif
343
344 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
345 void
346 child_acknowledge_created_inferior (int pid)
347 {
348 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
349 operation by a debugger.
350 */
351 }
352 #endif
353
354
355 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
356 void
357 child_insert_fork_catchpoint (int pid)
358 {
359 /* This version of Unix doesn't support notification of fork
360 events. */
361 }
362 #endif
363
364 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
365 int
366 child_remove_fork_catchpoint (int pid)
367 {
368 /* This version of Unix doesn't support notification of fork events. */
369 return 0;
370 }
371 #endif
372
373 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
374 void
375 child_insert_vfork_catchpoint (int pid)
376 {
377 /* This version of Unix doesn't support notification of vfork
378 events. */
379 }
380 #endif
381
382 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
383 int
384 child_remove_vfork_catchpoint (int pid)
385 {
386 /* This version of Unix doesn't support notification of vfork events. */
387 return 0;
388 }
389 #endif
390
391 #if !defined(CHILD_FOLLOW_FORK)
392 int
393 child_follow_fork (struct target_ops *ops, int follow_child)
394 {
395 /* This version of Unix doesn't support following fork or vfork events. */
396 return 0;
397 }
398 #endif
399
400 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
401 void
402 child_insert_exec_catchpoint (int pid)
403 {
404 /* This version of Unix doesn't support notification of exec
405 events. */
406 }
407 #endif
408
409 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
410 int
411 child_remove_exec_catchpoint (int pid)
412 {
413 /* This version of Unix doesn't support notification of exec events. */
414 return 0;
415 }
416 #endif
417
418 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
419 int
420 child_reported_exec_events_per_exec_call (void)
421 {
422 /* This version of Unix doesn't support notification of exec events.
423 */
424 return 1;
425 }
426 #endif
427
428 #if !defined(CHILD_HAS_EXITED)
429 int
430 child_has_exited (int pid, int wait_status, int *exit_status)
431 {
432 if (WIFEXITED (wait_status))
433 {
434 *exit_status = WEXITSTATUS (wait_status);
435 return 1;
436 }
437
438 if (WIFSIGNALED (wait_status))
439 {
440 *exit_status = 0; /* ?? Don't know what else to say here. */
441 return 1;
442 }
443
444 /* ?? Do we really need to consult the event state, too? Assume the
445 wait_state alone suffices.
446 */
447 return 0;
448 }
449 #endif
450
451
452 static void
453 child_mourn_inferior (void)
454 {
455 unpush_target (&deprecated_child_ops);
456 generic_mourn_inferior ();
457 }
458
459 static int
460 child_can_run (void)
461 {
462 /* This variable is controlled by modules that sit atop inftarg that may layer
463 their own process structure atop that provided here. hpux-thread.c does
464 this because of the Hpux user-mode level thread model. */
465
466 return !child_suppress_run;
467 }
468
469 /* Send a SIGINT to the process group. This acts just like the user typed a
470 ^C on the controlling terminal.
471
472 XXX - This may not be correct for all systems. Some may want to use
473 killpg() instead of kill (-pgrp). */
474
475 static void
476 child_stop (void)
477 {
478 kill (-inferior_process_group, SIGINT);
479 }
480
481 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
482 struct symtab_and_line *
483 child_enable_exception_callback (enum exception_event_kind kind, int enable)
484 {
485 return (struct symtab_and_line *) NULL;
486 }
487 #endif
488
489 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
490 struct exception_event_record *
491 child_get_current_exception_event (void)
492 {
493 return (struct exception_event_record *) NULL;
494 }
495 #endif
496
497
498 #if !defined(CHILD_PID_TO_EXEC_FILE)
499 char *
500 child_pid_to_exec_file (int pid)
501 {
502 /* This version of Unix doesn't support translation of a process ID
503 to the filename of the executable file.
504 */
505 return NULL;
506 }
507 #endif
508
509 char *
510 child_core_file_to_sym_file (char *core)
511 {
512 /* The target stratum for a running executable need not support
513 this operation.
514 */
515 return NULL;
516 }
517
518 /* Perform a partial transfer to/from the specified object. For
519 memory transfers, fall back to the old memory xfer functions. */
520
521 static LONGEST
522 child_xfer_partial (struct target_ops *ops, enum target_object object,
523 const char *annex, gdb_byte *readbuf,
524 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
525 {
526 switch (object)
527 {
528 case TARGET_OBJECT_MEMORY:
529 if (readbuf)
530 return child_xfer_memory (offset, readbuf, len, 0/*write*/,
531 NULL, ops);
532 if (writebuf)
533 return child_xfer_memory (offset, (void *) writebuf, len, 1/*write*/,
534 NULL, ops);
535 return -1;
536
537 case TARGET_OBJECT_UNWIND_TABLE:
538 #ifndef NATIVE_XFER_UNWIND_TABLE
539 #define NATIVE_XFER_UNWIND_TABLE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
540 #endif
541 return NATIVE_XFER_UNWIND_TABLE (ops, object, annex, readbuf, writebuf,
542 offset, len);
543
544 case TARGET_OBJECT_AUXV:
545 #ifndef NATIVE_XFER_AUXV
546 #define NATIVE_XFER_AUXV(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
547 #endif
548 return NATIVE_XFER_AUXV (ops, object, annex, readbuf, writebuf,
549 offset, len);
550
551 default:
552 return -1;
553 }
554 }
555
556 #if !defined(CHILD_PID_TO_STR)
557 char *
558 child_pid_to_str (ptid_t ptid)
559 {
560 return normal_pid_to_str (ptid);
561 }
562 #endif
563
564 static void
565 init_child_ops (void)
566 {
567 deprecated_child_ops.to_shortname = "child";
568 deprecated_child_ops.to_longname = "Unix child process";
569 deprecated_child_ops.to_doc = "Unix child process (started by the \"run\" command).";
570 deprecated_child_ops.to_open = child_open;
571 deprecated_child_ops.to_attach = child_attach;
572 deprecated_child_ops.to_post_attach = child_post_attach;
573 deprecated_child_ops.to_detach = child_detach;
574 deprecated_child_ops.to_resume = child_resume;
575 deprecated_child_ops.to_wait = child_wait;
576 deprecated_child_ops.to_fetch_registers = fetch_inferior_registers;
577 deprecated_child_ops.to_store_registers = store_inferior_registers;
578 deprecated_child_ops.to_prepare_to_store = child_prepare_to_store;
579 deprecated_child_ops.deprecated_xfer_memory = child_xfer_memory;
580 deprecated_child_ops.to_xfer_partial = child_xfer_partial;
581 deprecated_child_ops.to_files_info = child_files_info;
582 deprecated_child_ops.to_insert_breakpoint = memory_insert_breakpoint;
583 deprecated_child_ops.to_remove_breakpoint = memory_remove_breakpoint;
584 deprecated_child_ops.to_terminal_init = terminal_init_inferior;
585 deprecated_child_ops.to_terminal_inferior = terminal_inferior;
586 deprecated_child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
587 deprecated_child_ops.to_terminal_save_ours = terminal_save_ours;
588 deprecated_child_ops.to_terminal_ours = terminal_ours;
589 deprecated_child_ops.to_terminal_info = child_terminal_info;
590 deprecated_child_ops.to_kill = kill_inferior;
591 deprecated_child_ops.to_create_inferior = child_create_inferior;
592 deprecated_child_ops.to_post_startup_inferior = child_post_startup_inferior;
593 deprecated_child_ops.to_acknowledge_created_inferior = child_acknowledge_created_inferior;
594 deprecated_child_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
595 deprecated_child_ops.to_remove_fork_catchpoint = child_remove_fork_catchpoint;
596 deprecated_child_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
597 deprecated_child_ops.to_remove_vfork_catchpoint = child_remove_vfork_catchpoint;
598 deprecated_child_ops.to_follow_fork = child_follow_fork;
599 deprecated_child_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
600 deprecated_child_ops.to_remove_exec_catchpoint = child_remove_exec_catchpoint;
601 deprecated_child_ops.to_reported_exec_events_per_exec_call = child_reported_exec_events_per_exec_call;
602 deprecated_child_ops.to_has_exited = child_has_exited;
603 deprecated_child_ops.to_mourn_inferior = child_mourn_inferior;
604 deprecated_child_ops.to_can_run = child_can_run;
605 deprecated_child_ops.to_thread_alive = child_thread_alive;
606 deprecated_child_ops.to_pid_to_str = child_pid_to_str;
607 deprecated_child_ops.to_stop = child_stop;
608 deprecated_child_ops.to_enable_exception_callback = child_enable_exception_callback;
609 deprecated_child_ops.to_get_current_exception_event = child_get_current_exception_event;
610 deprecated_child_ops.to_pid_to_exec_file = child_pid_to_exec_file;
611 deprecated_child_ops.to_stratum = process_stratum;
612 deprecated_child_ops.to_has_all_memory = 1;
613 deprecated_child_ops.to_has_memory = 1;
614 deprecated_child_ops.to_has_stack = 1;
615 deprecated_child_ops.to_has_registers = 1;
616 deprecated_child_ops.to_has_execution = 1;
617 deprecated_child_ops.to_magic = OPS_MAGIC;
618 }
619
620 void
621 _initialize_inftarg (void)
622 {
623 #ifdef HAVE_OPTIONAL_PROC_FS
624 char procname[32];
625 int fd;
626
627 /* If we have an optional /proc filesystem (e.g. under OSF/1),
628 don't add ptrace support if we can access the running GDB via /proc. */
629 #ifndef PROC_NAME_FMT
630 #define PROC_NAME_FMT "/proc/%05d"
631 #endif
632 sprintf (procname, PROC_NAME_FMT, getpid ());
633 fd = open (procname, O_RDONLY);
634 if (fd >= 0)
635 {
636 close (fd);
637 return;
638 }
639 #endif
640
641 init_child_ops ();
642 add_target (&deprecated_child_ops);
643 }