e12e8bd529ac70da0edbf9d1d766b12a5dc94632
[platform/upstream/binutils.git] / gdb / inftarg.c
1 /* Target-vector operations for controlling Unix child processes, for GDB.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 
3    2000, 2002
4    Free Software Foundation, Inc.
5    Contributed by Cygnus Support.
6
7    ## Contains temporary hacks..
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25
26 #include "defs.h"
27 #include "frame.h"              /* required by inferior.h */
28 #include "inferior.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "command.h"
32 #include "gdb_stat.h"
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36
37 #include "gdb_wait.h"
38
39 extern struct symtab_and_line *child_enable_exception_callback (enum
40                                                                 exception_event_kind,
41                                                                 int);
42
43 extern struct exception_event_record
44   *child_get_current_exception_event (void);
45
46 extern void _initialize_inftarg (void);
47
48 static void child_prepare_to_store (void);
49
50 #ifndef CHILD_WAIT
51 static ptid_t child_wait (ptid_t, struct target_waitstatus *);
52 #endif /* CHILD_WAIT */
53
54 #if !defined(CHILD_POST_WAIT)
55 void child_post_wait (ptid_t, int);
56 #endif
57
58 static void child_open (char *, int);
59
60 static void child_files_info (struct target_ops *);
61
62 static void child_detach (char *, int);
63
64 static void child_attach (char *, int);
65
66 #if !defined(CHILD_POST_ATTACH)
67 extern void child_post_attach (int);
68 #endif
69
70 static void ptrace_me (void);
71
72 static void ptrace_him (int);
73
74 static void child_create_inferior (char *, char *, char **);
75
76 static void child_mourn_inferior (void);
77
78 static int child_can_run (void);
79
80 static void child_stop (void);
81
82 #ifndef CHILD_THREAD_ALIVE
83 int child_thread_alive (ptid_t);
84 #endif
85
86 static void init_child_ops (void);
87
88 extern char **environ;
89
90 struct target_ops child_ops;
91
92 int child_suppress_run = 0;     /* Non-zero if inftarg should pretend not to
93                                    be a runnable target.  Used by targets
94                                    that can sit atop inftarg, such as HPUX
95                                    thread support.  */
96
97 #ifndef CHILD_WAIT
98
99 /* Wait for child to do something.  Return pid of child, or -1 in case
100    of error; store status through argument pointer OURSTATUS.  */
101
102 static ptid_t
103 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
104 {
105   int save_errno;
106   int status;
107   char *execd_pathname = NULL;
108   int exit_status;
109   int related_pid;
110   int syscall_id;
111   enum target_waitkind kind;
112   int pid;
113
114   do
115     {
116       set_sigint_trap ();       /* Causes SIGINT to be passed on to the
117                                    attached process. */
118       set_sigio_trap ();
119
120       pid = ptrace_wait (inferior_ptid, &status);
121
122       save_errno = errno;
123
124       clear_sigio_trap ();
125
126       clear_sigint_trap ();
127
128       if (pid == -1)
129         {
130           if (save_errno == EINTR)
131             continue;
132
133           fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
134                               safe_strerror (save_errno));
135
136           /* Claim it exited with unknown signal.  */
137           ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
138           ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
139           return pid_to_ptid (-1);
140         }
141
142       /* Did it exit?
143        */
144       if (target_has_exited (pid, status, &exit_status))
145         {
146           /* ??rehrauer: For now, ignore this. */
147           continue;
148         }
149
150       if (!target_thread_alive (pid_to_ptid (pid)))
151         {
152           ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
153           return pid_to_ptid (pid);
154         }
155       } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
156
157   store_waitstatus (ourstatus, status);
158   return pid_to_ptid (pid);
159 }
160 #endif /* CHILD_WAIT */
161
162 #if !defined(CHILD_POST_WAIT)
163 void
164 child_post_wait (ptid_t ptid, int wait_status)
165 {
166   /* This version of Unix doesn't require a meaningful "post wait"
167      operation.
168    */
169 }
170 #endif
171
172
173 #ifndef CHILD_THREAD_ALIVE
174
175 /* Check to see if the given thread is alive.
176
177    FIXME: Is kill() ever the right way to do this?  I doubt it, but
178    for now we're going to try and be compatable with the old thread
179    code.  */
180 int
181 child_thread_alive (ptid_t ptid)
182 {
183   pid_t pid = PIDGET (ptid);
184
185   return (kill (pid, 0) != -1);
186 }
187
188 #endif
189
190 /* Attach to process PID, then initialize for debugging it.  */
191
192 static void
193 child_attach (char *args, int from_tty)
194 {
195   if (!args)
196     error_no_arg ("process-id to attach");
197
198 #ifndef ATTACH_DETACH
199   error ("Can't attach to a process on this machine.");
200 #else
201   {
202     char *exec_file;
203     int pid;
204     char *dummy;
205
206     dummy = args;
207     pid = strtol (args, &dummy, 0);
208     /* Some targets don't set errno on errors, grrr! */
209     if ((pid == 0) && (args == dummy))
210       error ("Illegal process-id: %s\n", args);
211
212     if (pid == getpid ())       /* Trying to masturbate? */
213       error ("I refuse to debug myself!");
214
215     if (from_tty)
216       {
217         exec_file = (char *) get_exec_file (0);
218
219         if (exec_file)
220           printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
221                              target_pid_to_str (pid_to_ptid (pid)));
222         else
223           printf_unfiltered ("Attaching to %s\n", 
224                              target_pid_to_str (pid_to_ptid (pid)));
225
226         gdb_flush (gdb_stdout);
227       }
228
229     attach (pid);
230
231     inferior_ptid = pid_to_ptid (pid);
232     push_target (&child_ops);
233   }
234 #endif /* ATTACH_DETACH */
235 }
236
237 #if !defined(CHILD_POST_ATTACH)
238 void
239 child_post_attach (int pid)
240 {
241   /* This version of Unix doesn't require a meaningful "post attach"
242      operation by a debugger.  */
243 }
244 #endif
245
246 /* Take a program previously attached to and detaches it.
247    The program resumes execution and will no longer stop
248    on signals, etc.  We'd better not have left any breakpoints
249    in the program or it'll die when it hits one.  For this
250    to work, it may be necessary for the process to have been
251    previously attached.  It *might* work if the program was
252    started via the normal ptrace (PTRACE_TRACEME).  */
253
254 static void
255 child_detach (char *args, int from_tty)
256 {
257 #ifdef ATTACH_DETACH
258   {
259     int siggnal = 0;
260     int pid = PIDGET (inferior_ptid);
261
262     if (from_tty)
263       {
264         char *exec_file = get_exec_file (0);
265         if (exec_file == 0)
266           exec_file = "";
267         printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
268                            target_pid_to_str (pid_to_ptid (pid)));
269         gdb_flush (gdb_stdout);
270       }
271     if (args)
272       siggnal = atoi (args);
273
274     detach (siggnal);
275
276     inferior_ptid = null_ptid;
277     unpush_target (&child_ops);
278   }
279 #else
280   error ("This version of Unix does not support detaching a process.");
281 #endif
282 }
283
284 /* Get ready to modify the registers array.  On machines which store
285    individual registers, this doesn't need to do anything.  On machines
286    which store all the registers in one fell swoop, this makes sure
287    that registers contains all the registers from the program being
288    debugged.  */
289
290 static void
291 child_prepare_to_store (void)
292 {
293 #ifdef CHILD_PREPARE_TO_STORE
294   CHILD_PREPARE_TO_STORE ();
295 #endif
296 }
297
298 /* Print status information about what we're accessing.  */
299
300 static void
301 child_files_info (struct target_ops *ignore)
302 {
303   printf_unfiltered ("\tUsing the running image of %s %s.\n",
304       attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
305 }
306
307 /* ARGSUSED */
308 static void
309 child_open (char *arg, int from_tty)
310 {
311   error ("Use the \"run\" command to start a Unix child process.");
312 }
313
314 /* Stub function which causes the inferior that runs it, to be ptrace-able
315    by its parent process.  */
316
317 static void
318 ptrace_me (void)
319 {
320   /* "Trace me, Dr. Memory!" */
321   call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
322 }
323
324 /* Stub function which causes the GDB that runs it, to start ptrace-ing
325    the child process.  */
326
327 static void
328 ptrace_him (int pid)
329 {
330   push_target (&child_ops);
331
332   /* On some targets, there must be some explicit synchronization
333      between the parent and child processes after the debugger
334      forks, and before the child execs the debuggee program.  This
335      call basically gives permission for the child to exec.
336    */
337
338   target_acknowledge_created_inferior (pid);
339
340   /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
341    * and will be 1 or 2 depending on whether we're starting
342    * without or with a shell.
343    */
344   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
345
346   /* On some targets, there must be some explicit actions taken after
347      the inferior has been started up.
348    */
349   target_post_startup_inferior (pid_to_ptid (pid));
350 }
351
352 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
353    EXEC_FILE is the file to run.
354    ALLARGS is a string containing the arguments to the program.
355    ENV is the environment vector to pass.  Errors reported with error().  */
356
357 static void
358 child_create_inferior (char *exec_file, char *allargs, char **env)
359 {
360 #ifdef HPUXHPPA
361   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, pre_fork_inferior, NULL);
362 #else
363   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
364 #endif
365   /* We are at the first instruction we care about.  */
366   /* Pedal to the metal... */
367   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
368 }
369
370 #if !defined(CHILD_POST_STARTUP_INFERIOR)
371 void
372 child_post_startup_inferior (ptid_t ptid)
373 {
374   /* This version of Unix doesn't require a meaningful "post startup inferior"
375      operation by a debugger.
376    */
377 }
378 #endif
379
380 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
381 void
382 child_acknowledge_created_inferior (int pid)
383 {
384   /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
385      operation by a debugger.
386    */
387 }
388 #endif
389
390
391 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
392 int
393 child_insert_fork_catchpoint (int pid)
394 {
395   /* This version of Unix doesn't support notification of fork events.  */
396   return 0;
397 }
398 #endif
399
400 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
401 int
402 child_remove_fork_catchpoint (int pid)
403 {
404   /* This version of Unix doesn't support notification of fork events.  */
405   return 0;
406 }
407 #endif
408
409 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
410 int
411 child_insert_vfork_catchpoint (int pid)
412 {
413   /* This version of Unix doesn't support notification of vfork events.  */
414   return 0;
415 }
416 #endif
417
418 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
419 int
420 child_remove_vfork_catchpoint (int pid)
421 {
422   /* This version of Unix doesn't support notification of vfork events.  */
423   return 0;
424 }
425 #endif
426
427 #if !defined(CHILD_FOLLOW_FORK)
428 int
429 child_follow_fork (int follow_child)
430 {
431   /* This version of Unix doesn't support following fork or vfork events.  */
432   return 0;
433 }
434 #endif
435
436 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
437 int
438 child_insert_exec_catchpoint (int pid)
439 {
440   /* This version of Unix doesn't support notification of exec events.  */
441   return 0;
442 }
443 #endif
444
445 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
446 int
447 child_remove_exec_catchpoint (int pid)
448 {
449   /* This version of Unix doesn't support notification of exec events.  */
450   return 0;
451 }
452 #endif
453
454 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
455 int
456 child_reported_exec_events_per_exec_call (void)
457 {
458   /* This version of Unix doesn't support notification of exec events.
459    */
460   return 1;
461 }
462 #endif
463
464 #if !defined(CHILD_HAS_EXITED)
465 int
466 child_has_exited (int pid, int wait_status, int *exit_status)
467 {
468   if (WIFEXITED (wait_status))
469     {
470       *exit_status = WEXITSTATUS (wait_status);
471       return 1;
472     }
473
474   if (WIFSIGNALED (wait_status))
475     {
476       *exit_status = 0;         /* ?? Don't know what else to say here. */
477       return 1;
478     }
479
480   /* ?? Do we really need to consult the event state, too?  Assume the
481      wait_state alone suffices.
482    */
483   return 0;
484 }
485 #endif
486
487
488 static void
489 child_mourn_inferior (void)
490 {
491   unpush_target (&child_ops);
492   generic_mourn_inferior ();
493 }
494
495 static int
496 child_can_run (void)
497 {
498   /* This variable is controlled by modules that sit atop inftarg that may layer
499      their own process structure atop that provided here.  hpux-thread.c does
500      this because of the Hpux user-mode level thread model.  */
501
502   return !child_suppress_run;
503 }
504
505 /* Send a SIGINT to the process group.  This acts just like the user typed a
506    ^C on the controlling terminal.
507
508    XXX - This may not be correct for all systems.  Some may want to use
509    killpg() instead of kill (-pgrp). */
510
511 static void
512 child_stop (void)
513 {
514   extern pid_t inferior_process_group;
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 }