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