Bring cross-compiling to amd64 into shape, i.e. make the infrastructure
[dragonfly.git] / contrib / gdb-6.2.1 / 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 #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 **, int);
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   char *exec_file;
198   int pid;
199   char *dummy;
200
201   if (!args)
202     error_no_arg ("process-id to attach");
203
204   dummy = args;
205   pid = strtol (args, &dummy, 0);
206   /* Some targets don't set errno on errors, grrr! */
207   if ((pid == 0) && (args == dummy))
208       error ("Illegal process-id: %s\n", args);
209   
210   if (pid == getpid ()) /* Trying to masturbate? */
211     error ("I refuse to debug myself!");
212   
213   if (from_tty)
214     {
215       exec_file = (char *) get_exec_file (0);
216       
217       if (exec_file)
218         printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
219                            target_pid_to_str (pid_to_ptid (pid)));
220       else
221         printf_unfiltered ("Attaching to %s\n",
222                            target_pid_to_str (pid_to_ptid (pid)));
223       
224       gdb_flush (gdb_stdout);
225     }
226
227   attach (pid);
228   
229   inferior_ptid = pid_to_ptid (pid);
230   push_target (&child_ops);
231 }
232
233 #if !defined(CHILD_POST_ATTACH)
234 void
235 child_post_attach (int pid)
236 {
237   /* This version of Unix doesn't require a meaningful "post attach"
238      operation by a debugger.  */
239 }
240 #endif
241
242 /* Take a program previously attached to and detaches it.
243    The program resumes execution and will no longer stop
244    on signals, etc.  We'd better not have left any breakpoints
245    in the program or it'll die when it hits one.  For this
246    to work, it may be necessary for the process to have been
247    previously attached.  It *might* work if the program was
248    started via the normal ptrace (PTRACE_TRACEME).  */
249
250 static void
251 child_detach (char *args, int from_tty)
252 {
253   int siggnal = 0;
254   int pid = PIDGET (inferior_ptid);
255   
256   if (from_tty)
257     {
258       char *exec_file = get_exec_file (0);
259       if (exec_file == 0)
260         exec_file = "";
261       printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
262                          target_pid_to_str (pid_to_ptid (pid)));
263       gdb_flush (gdb_stdout);
264     }
265   if (args)
266     siggnal = atoi (args);
267   
268   detach (siggnal);
269   
270   inferior_ptid = null_ptid;
271   unpush_target (&child_ops);
272 }
273
274 /* Get ready to modify the registers array.  On machines which store
275    individual registers, this doesn't need to do anything.  On machines
276    which store all the registers in one fell swoop, this makes sure
277    that registers contains all the registers from the program being
278    debugged.  */
279
280 static void
281 child_prepare_to_store (void)
282 {
283 #ifdef CHILD_PREPARE_TO_STORE
284   CHILD_PREPARE_TO_STORE ();
285 #endif
286 }
287
288 /* Print status information about what we're accessing.  */
289
290 static void
291 child_files_info (struct target_ops *ignore)
292 {
293   printf_unfiltered ("\tUsing the running image of %s %s.\n",
294       attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
295 }
296
297 static void
298 child_open (char *arg, int from_tty)
299 {
300   error ("Use the \"run\" command to start a Unix child process.");
301 }
302
303 /* Stub function which causes the inferior that runs it, to be ptrace-able
304    by its parent process.  */
305
306 static void
307 ptrace_me (void)
308 {
309   /* "Trace me, Dr. Memory!" */
310   call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
311 }
312
313 /* Stub function which causes the GDB that runs it, to start ptrace-ing
314    the child process.  */
315
316 static void
317 ptrace_him (int pid)
318 {
319   push_target (&child_ops);
320
321   /* On some targets, there must be some explicit synchronization
322      between the parent and child processes after the debugger
323      forks, and before the child execs the debuggee program.  This
324      call basically gives permission for the child to exec.
325    */
326
327   target_acknowledge_created_inferior (pid);
328
329   /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
330    * and will be 1 or 2 depending on whether we're starting
331    * without or with a shell.
332    */
333   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
334
335   /* On some targets, there must be some explicit actions taken after
336      the inferior has been started up.
337    */
338   target_post_startup_inferior (pid_to_ptid (pid));
339 }
340
341 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
342    EXEC_FILE is the file to run.
343    ALLARGS is a string containing the arguments to the program.
344    ENV is the environment vector to pass.  Errors reported with error().  */
345
346 static void
347 child_create_inferior (char *exec_file, char *allargs, char **env,
348                        int from_tty)
349 {
350 #ifdef HPUXHPPA
351   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, pre_fork_inferior, NULL);
352 #else
353   fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
354 #endif
355   /* We are at the first instruction we care about.  */
356   observer_notify_inferior_created (&current_target, from_tty);
357   /* Pedal to the metal... */
358   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
359 }
360
361 #if !defined(CHILD_POST_STARTUP_INFERIOR)
362 void
363 child_post_startup_inferior (ptid_t ptid)
364 {
365   /* This version of Unix doesn't require a meaningful "post startup inferior"
366      operation by a debugger.
367    */
368 }
369 #endif
370
371 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
372 void
373 child_acknowledge_created_inferior (int pid)
374 {
375   /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
376      operation by a debugger.
377    */
378 }
379 #endif
380
381
382 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
383 int
384 child_insert_fork_catchpoint (int pid)
385 {
386   /* This version of Unix doesn't support notification of fork events.  */
387   return 0;
388 }
389 #endif
390
391 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
392 int
393 child_remove_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_INSERT_VFORK_CATCHPOINT)
401 int
402 child_insert_vfork_catchpoint (int pid)
403 {
404   /* This version of Unix doesn't support notification of vfork events.  */
405   return 0;
406 }
407 #endif
408
409 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
410 int
411 child_remove_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_FOLLOW_FORK)
419 int
420 child_follow_fork (int follow_child)
421 {
422   /* This version of Unix doesn't support following fork or vfork events.  */
423   return 0;
424 }
425 #endif
426
427 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
428 int
429 child_insert_exec_catchpoint (int pid)
430 {
431   /* This version of Unix doesn't support notification of exec events.  */
432   return 0;
433 }
434 #endif
435
436 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
437 int
438 child_remove_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_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
446 int
447 child_reported_exec_events_per_exec_call (void)
448 {
449   /* This version of Unix doesn't support notification of exec events.
450    */
451   return 1;
452 }
453 #endif
454
455 #if !defined(CHILD_HAS_EXITED)
456 int
457 child_has_exited (int pid, int wait_status, int *exit_status)
458 {
459   if (WIFEXITED (wait_status))
460     {
461       *exit_status = WEXITSTATUS (wait_status);
462       return 1;
463     }
464
465   if (WIFSIGNALED (wait_status))
466     {
467       *exit_status = 0;         /* ?? Don't know what else to say here. */
468       return 1;
469     }
470
471   /* ?? Do we really need to consult the event state, too?  Assume the
472      wait_state alone suffices.
473    */
474   return 0;
475 }
476 #endif
477
478
479 static void
480 child_mourn_inferior (void)
481 {
482   unpush_target (&child_ops);
483   generic_mourn_inferior ();
484 }
485
486 static int
487 child_can_run (void)
488 {
489   /* This variable is controlled by modules that sit atop inftarg that may layer
490      their own process structure atop that provided here.  hpux-thread.c does
491      this because of the Hpux user-mode level thread model.  */
492
493   return !child_suppress_run;
494 }
495
496 /* Send a SIGINT to the process group.  This acts just like the user typed a
497    ^C on the controlling terminal.
498
499    XXX - This may not be correct for all systems.  Some may want to use
500    killpg() instead of kill (-pgrp). */
501
502 static void
503 child_stop (void)
504 {
505   kill (-inferior_process_group, SIGINT);
506 }
507
508 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
509 struct symtab_and_line *
510 child_enable_exception_callback (enum exception_event_kind kind, int enable)
511 {
512   return (struct symtab_and_line *) NULL;
513 }
514 #endif
515
516 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
517 struct exception_event_record *
518 child_get_current_exception_event (void)
519 {
520   return (struct exception_event_record *) NULL;
521 }
522 #endif
523
524
525 #if !defined(CHILD_PID_TO_EXEC_FILE)
526 char *
527 child_pid_to_exec_file (int pid)
528 {
529   /* This version of Unix doesn't support translation of a process ID
530      to the filename of the executable file.
531    */
532   return NULL;
533 }
534 #endif
535
536 char *
537 child_core_file_to_sym_file (char *core)
538 {
539   /* The target stratum for a running executable need not support
540      this operation.
541    */
542   return NULL;
543 }
544
545 /* Perform a partial transfer to/from the specified object.  For
546    memory transfers, fall back to the old memory xfer functions.  */
547
548 static LONGEST
549 child_xfer_partial (struct target_ops *ops, enum target_object object,
550                     const char *annex, void *readbuf,
551                     const void *writebuf, ULONGEST offset, LONGEST len)
552 {
553   switch (object)
554     {
555     case TARGET_OBJECT_MEMORY:
556       if (readbuf)
557         return child_xfer_memory (offset, readbuf, len, 0/*write*/,
558                                   NULL, ops);
559       if (writebuf)
560         return child_xfer_memory (offset, readbuf, len, 1/*write*/,
561                                   NULL, ops);
562       return -1;
563
564     case TARGET_OBJECT_UNWIND_TABLE:
565 #ifndef NATIVE_XFER_UNWIND_TABLE
566 #define NATIVE_XFER_UNWIND_TABLE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
567 #endif
568       return NATIVE_XFER_UNWIND_TABLE (ops, object, annex, readbuf, writebuf,
569                                        offset, len);
570
571     case TARGET_OBJECT_AUXV:
572 #ifndef NATIVE_XFER_AUXV
573 #define NATIVE_XFER_AUXV(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
574 #endif
575       return NATIVE_XFER_AUXV (ops, object, annex, readbuf, writebuf,
576                                offset, len);
577
578     case TARGET_OBJECT_WCOOKIE:
579 #ifndef NATIVE_XFER_WCOOKIE
580 #define NATIVE_XFER_WCOOKIE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
581 #endif
582       return NATIVE_XFER_WCOOKIE (ops, object, annex, readbuf, writebuf,
583                                   offset, len);
584
585     default:
586       return -1;
587     }
588 }
589
590 #if !defined(CHILD_PID_TO_STR)
591 char *
592 child_pid_to_str (ptid_t ptid)
593 {
594   return normal_pid_to_str (ptid);
595 }
596 #endif
597
598 static void
599 init_child_ops (void)
600 {
601   child_ops.to_shortname = "child";
602   child_ops.to_longname = "Unix child process";
603   child_ops.to_doc = "Unix child process (started by the \"run\" command).";
604   child_ops.to_open = child_open;
605   child_ops.to_attach = child_attach;
606   child_ops.to_post_attach = child_post_attach;
607   child_ops.to_detach = child_detach;
608   child_ops.to_resume = child_resume;
609   child_ops.to_wait = child_wait;
610   child_ops.to_post_wait = child_post_wait;
611   child_ops.to_fetch_registers = fetch_inferior_registers;
612   child_ops.to_store_registers = store_inferior_registers;
613   child_ops.to_prepare_to_store = child_prepare_to_store;
614   child_ops.to_xfer_memory = child_xfer_memory;
615   child_ops.to_xfer_partial = child_xfer_partial;
616   child_ops.to_files_info = child_files_info;
617   child_ops.to_insert_breakpoint = memory_insert_breakpoint;
618   child_ops.to_remove_breakpoint = memory_remove_breakpoint;
619   child_ops.to_terminal_init = terminal_init_inferior;
620   child_ops.to_terminal_inferior = terminal_inferior;
621   child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
622   child_ops.to_terminal_save_ours = terminal_save_ours;
623   child_ops.to_terminal_ours = terminal_ours;
624   child_ops.to_terminal_info = child_terminal_info;
625   child_ops.to_kill = kill_inferior;
626   child_ops.to_create_inferior = child_create_inferior;
627   child_ops.to_post_startup_inferior = child_post_startup_inferior;
628   child_ops.to_acknowledge_created_inferior = child_acknowledge_created_inferior;
629   child_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
630   child_ops.to_remove_fork_catchpoint = child_remove_fork_catchpoint;
631   child_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
632   child_ops.to_remove_vfork_catchpoint = child_remove_vfork_catchpoint;
633   child_ops.to_follow_fork = child_follow_fork;
634   child_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
635   child_ops.to_remove_exec_catchpoint = child_remove_exec_catchpoint;
636   child_ops.to_reported_exec_events_per_exec_call = child_reported_exec_events_per_exec_call;
637   child_ops.to_has_exited = child_has_exited;
638   child_ops.to_mourn_inferior = child_mourn_inferior;
639   child_ops.to_can_run = child_can_run;
640   child_ops.to_thread_alive = child_thread_alive;
641   child_ops.to_pid_to_str = child_pid_to_str;
642   child_ops.to_stop = child_stop;
643   child_ops.to_enable_exception_callback = child_enable_exception_callback;
644   child_ops.to_get_current_exception_event = child_get_current_exception_event;
645   child_ops.to_pid_to_exec_file = child_pid_to_exec_file;
646   child_ops.to_stratum = process_stratum;
647   child_ops.to_has_all_memory = 1;
648   child_ops.to_has_memory = 1;
649   child_ops.to_has_stack = 1;
650   child_ops.to_has_registers = 1;
651   child_ops.to_has_execution = 1;
652   child_ops.to_magic = OPS_MAGIC;
653 }
654
655 /* Take over the 'find_mapped_memory' vector from inftarg.c. */
656 extern void
657 inftarg_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
658                                                        unsigned long,
659                                                        int, int, int,
660                                                        void *),
661                                               void *))
662 {
663   child_ops.to_find_memory_regions = func;
664 }
665
666 /* Take over the 'make_corefile_notes' vector from inftarg.c. */
667 extern void
668 inftarg_set_make_corefile_notes (char * (*func) (bfd *, int *))
669 {
670   child_ops.to_make_corefile_notes = func;
671 }
672
673 void
674 _initialize_inftarg (void)
675 {
676 #ifdef HAVE_OPTIONAL_PROC_FS
677   char procname[32];
678   int fd;
679
680   /* If we have an optional /proc filesystem (e.g. under OSF/1),
681      don't add ptrace support if we can access the running GDB via /proc.  */
682 #ifndef PROC_NAME_FMT
683 #define PROC_NAME_FMT "/proc/%05d"
684 #endif
685   sprintf (procname, PROC_NAME_FMT, getpid ());
686   fd = open (procname, O_RDONLY);
687   if (fd >= 0)
688     {
689       close (fd);
690       return;
691     }
692 #endif
693
694   init_child_ops ();
695   add_target (&child_ops);
696 }