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