Wed Jan 13 14:59:02 1999 Michael Snyder <msnyder@cleaver.cygnus.com>
[platform/upstream/binutils.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2    Copyright 1990, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include <errno.h>
23 #include <ctype.h>
24 #include "gdb_string.h"
25 #include "target.h"
26 #include "gdbcmd.h"
27 #include "symtab.h"
28 #include "inferior.h"
29 #include "bfd.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "wait.h"
33 #include <signal.h>
34
35 extern int errno;
36
37 static void
38 target_info PARAMS ((char *, int));
39
40 static void
41 cleanup_target PARAMS ((struct target_ops *));
42
43 static void
44 maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
45
46 static void
47 default_clone_and_follow_inferior PARAMS ((int, int *));
48
49 static void
50 maybe_kill_then_attach PARAMS ((char *, int));
51
52 static void
53 kill_or_be_killed PARAMS ((int));
54
55 static void
56 default_terminal_info PARAMS ((char *, int));
57
58 static int
59 nosymbol PARAMS ((char *, CORE_ADDR *));
60
61 static void
62 tcomplain PARAMS ((void));
63
64 static int
65 nomemory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
66
67 static int
68 return_zero PARAMS ((void));
69
70 static int
71 return_one PARAMS ((void));
72
73 void
74 target_ignore PARAMS ((void));
75
76 static void
77 target_command PARAMS ((char *, int));
78
79 static struct target_ops *
80 find_default_run_target PARAMS ((char *));
81
82 static void
83 update_current_target PARAMS ((void));
84
85 /* Transfer LEN bytes between target address MEMADDR and GDB address MYADDR.
86    Returns 0 for success, errno code for failure (which includes partial
87    transfers--if you want a more useful response to partial transfers, try
88    target_read_memory_partial).  */
89
90 static int
91 target_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
92                             int write, asection *bfd_section));
93
94 static void
95 debug_to_open PARAMS ((char *, int));
96
97 static void
98 debug_to_close PARAMS ((int));
99
100 static void
101 debug_to_attach PARAMS ((char *, int));
102
103 static void
104 debug_to_detach PARAMS ((char *, int));
105
106 static void
107 debug_to_resume PARAMS ((int, int, enum target_signal));
108
109 static int
110 debug_to_wait PARAMS ((int, struct target_waitstatus *));
111
112 static void
113 debug_to_fetch_registers PARAMS ((int));
114
115 static void
116 debug_to_store_registers PARAMS ((int));
117
118 static void
119 debug_to_prepare_to_store PARAMS ((void));
120
121 static int
122 debug_to_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
123
124 static void
125 debug_to_files_info PARAMS ((struct target_ops *));
126
127 static int
128 debug_to_insert_breakpoint PARAMS ((CORE_ADDR, char *));
129
130 static int
131 debug_to_remove_breakpoint PARAMS ((CORE_ADDR, char *));
132
133 static void
134 debug_to_terminal_init PARAMS ((void));
135
136 static void
137 debug_to_terminal_inferior PARAMS ((void));
138
139 static void
140 debug_to_terminal_ours_for_output PARAMS ((void));
141
142 static void
143 debug_to_terminal_ours PARAMS ((void));
144
145 static void
146 debug_to_terminal_info PARAMS ((char *, int));
147
148 static void
149 debug_to_kill PARAMS ((void));
150
151 static void
152 debug_to_load PARAMS ((char *, int));
153
154 static int
155 debug_to_lookup_symbol PARAMS ((char *, CORE_ADDR *));
156
157 static void
158 debug_to_create_inferior PARAMS ((char *, char *, char **));
159
160 static void
161 debug_to_mourn_inferior PARAMS ((void));
162
163 static int
164 debug_to_can_run PARAMS ((void));
165
166 static void
167 debug_to_notice_signals PARAMS ((int));
168
169 static int
170 debug_to_thread_alive PARAMS ((int));
171
172 static void
173 debug_to_stop PARAMS ((void));
174
175 /* Pointer to array of target architecture structures; the size of the
176    array; the current index into the array; the allocated size of the 
177    array.  */
178 struct target_ops **target_structs;
179 unsigned target_struct_size;
180 unsigned target_struct_index;
181 unsigned target_struct_allocsize;
182 #define DEFAULT_ALLOCSIZE       10
183
184 /* The initial current target, so that there is always a semi-valid
185    current target.  */
186
187 struct target_ops dummy_target = {
188   "None",                       /* to_shortname */
189   "None",                       /* to_longname */
190   "",                           /* to_doc */
191   0,                            /* to_open */
192   0,                            /* to_close */
193   find_default_attach,          /* to_attach */
194   NULL,                         /* to_post_attach */
195   find_default_require_attach,  /* to_require_attach */
196   0,                            /* to_detach */
197   find_default_require_detach,  /* to_require_detach */
198   0,                            /* to_resume */
199   0,                            /* to_wait */
200   NULL,                         /* to_post_wait */
201   0,                            /* to_fetch_registers */
202   0,                            /* to_store_registers */
203   0,                            /* to_prepare_to_store */
204   0,                            /* to_xfer_memory */
205   0,                            /* to_files_info */
206   0,                            /* to_insert_breakpoint */
207   0,                            /* to_remove_breakpoint */
208   0,                            /* to_terminal_init */
209   0,                            /* to_terminal_inferior */
210   0,                            /* to_terminal_ours_for_output */
211   0,                            /* to_terminal_ours */
212   0,                            /* to_terminal_info */
213   0,                            /* to_kill */
214   0,                            /* to_load */
215   0,                            /* to_lookup_symbol */
216   find_default_create_inferior, /* to_create_inferior */
217   NULL,                         /* to_post_startup_inferior */
218   NULL,                         /* to_acknowledge_created_inferior */
219   find_default_clone_and_follow_inferior,       /* to_clone_and_follow_inferior */
220   NULL,                         /* to_post_follow_inferior_by_clone */
221   NULL,                         /* to_insert_fork_catchpoint */
222   NULL,                         /* to_remove_fork_catchpoint */
223   NULL,                         /* to_insert_vfork_catchpoint */
224   NULL,                         /* to_remove_vfork_catchpoint */
225   NULL,                         /* to_has_forked */
226   NULL,                         /* to_has_vforked */
227   NULL,                         /* to_can_follow_vfork_prior_to_exec */
228   NULL,                         /* to_post_follow_vfork */
229   NULL,                         /* to_insert_exec_catchpoint */
230   NULL,                         /* to_remove_exec_catchpoint */
231   NULL,                         /* to_has_execd */
232   NULL,                         /* to_reported_exec_events_per_exec_call */
233   NULL,                         /* to_has_syscall_event */
234   NULL,                         /* to_has_exited */
235   0,                            /* to_mourn_inferior */
236   0,                            /* to_can_run */
237   0,                            /* to_notice_signals */
238   0,                            /* to_thread_alive */
239   0,                            /* to_stop */
240   NULL,                         /* to_enable_exception_callback */
241   NULL,                         /* to_get_current_exception_event */
242   NULL,                         /* to_pid_to_exec_file */
243   NULL,                         /* to_core_file_to_sym_file */
244   dummy_stratum,                /* to_stratum */
245   0,                            /* to_next */
246   0,                            /* to_has_all_memory */
247   0,                            /* to_has_memory */
248   0,                            /* to_has_stack */
249   0,                            /* to_has_registers */
250   0,                            /* to_has_execution */
251   tc_none,                      /* to_has_thread_control */
252   0,                            /* to_sections */
253   0,                            /* to_sections_end */
254   OPS_MAGIC,                    /* to_magic */
255 };
256
257 /* Top of target stack.  */
258
259 struct target_stack_item *target_stack;
260
261 /* The target structure we are currently using to talk to a process
262    or file or whatever "inferior" we have.  */
263
264 struct target_ops current_target;
265
266 /* Command list for target.  */
267
268 static struct cmd_list_element *targetlist = NULL;
269
270 /* Nonzero if we are debugging an attached outside process
271    rather than an inferior.  */
272
273 int attach_flag;
274
275 #ifdef MAINTENANCE_CMDS
276 /* Non-zero if we want to see trace of target level stuff.  */
277
278 static int targetdebug = 0;
279
280 static void setup_target_debug PARAMS ((void));
281
282 #endif
283
284 /* The user just typed 'target' without the name of a target.  */
285
286 /* ARGSUSED */
287 static void
288 target_command (arg, from_tty)
289      char *arg;
290      int from_tty;
291 {
292   fputs_filtered ("Argument required (target name).  Try `help target'\n",
293                   gdb_stdout);
294 }
295
296 /* Add a possible target architecture to the list.  */
297
298 void
299 add_target (t)
300      struct target_ops *t;
301 {
302   if (!target_structs)
303     {
304       target_struct_allocsize = DEFAULT_ALLOCSIZE;
305       target_structs = (struct target_ops **) xmalloc
306         (target_struct_allocsize * sizeof (*target_structs));
307     }
308   if (target_struct_size >= target_struct_allocsize)
309     {
310       target_struct_allocsize *= 2;
311       target_structs = (struct target_ops **)
312           xrealloc ((char *) target_structs, 
313                     target_struct_allocsize * sizeof (*target_structs));
314     }
315   target_structs[target_struct_size++] = t;
316 /*  cleanup_target (t);*/
317
318   if (targetlist == NULL)
319     add_prefix_cmd ("target", class_run, target_command,
320                     "Connect to a target machine or process.\n\
321 The first argument is the type or protocol of the target machine.\n\
322 Remaining arguments are interpreted by the target protocol.  For more\n\
323 information on the arguments for a particular protocol, type\n\
324 `help target ' followed by the protocol name.",
325                     &targetlist, "target ", 0, &cmdlist);
326   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
327 }
328
329 /* Stub functions */
330
331 void
332 target_ignore ()
333 {
334 }
335
336 /* ARGSUSED */
337 static int
338 nomemory (memaddr, myaddr, len, write, t)
339      CORE_ADDR memaddr;
340      char *myaddr;
341      int len;
342      int write;
343      struct target_ops *t;
344 {
345   errno = EIO;          /* Can't read/write this location */
346   return 0;             /* No bytes handled */
347 }
348
349 static void
350 tcomplain ()
351 {
352   error ("You can't do that when your target is `%s'",
353          current_target.to_shortname);
354 }
355
356 void
357 noprocess ()
358 {
359   error ("You can't do that without a process to debug.");
360 }
361
362 /* ARGSUSED */
363 static int
364 nosymbol (name, addrp)
365      char *name;
366      CORE_ADDR *addrp;
367 {
368   return 1;             /* Symbol does not exist in target env */
369 }
370
371 /* ARGSUSED */
372 void
373 nosupport_runtime ()
374 {
375   if (!inferior_pid)
376     noprocess ();
377   else
378     error ("No run-time support for this");
379 }
380
381
382 /* ARGSUSED */
383 static void
384 default_terminal_info (args, from_tty)
385      char *args;
386      int from_tty;
387 {
388   printf_unfiltered("No saved terminal information.\n");
389 }
390
391 /* This is the default target_create_inferior and target_attach function.
392    If the current target is executing, it asks whether to kill it off.
393    If this function returns without calling error(), it has killed off
394    the target, and the operation should be attempted.  */
395
396 static void
397 kill_or_be_killed (from_tty)
398      int from_tty;
399 {
400   if (target_has_execution)
401     {
402       printf_unfiltered ("You are already running a program:\n");
403       target_files_info ();
404       if (query ("Kill it? ")) {
405         target_kill ();
406         if (target_has_execution)
407           error ("Killing the program did not help.");
408         return;
409       } else {
410         error ("Program not killed.");
411       }
412     }
413   tcomplain();
414 }
415
416 static void
417 maybe_kill_then_attach (args, from_tty)
418      char *args;
419      int from_tty;
420 {
421   kill_or_be_killed (from_tty);
422   target_attach (args, from_tty);
423 }
424
425 static void
426 maybe_kill_then_create_inferior (exec, args, env)
427      char *exec;
428      char *args;
429      char **env;
430 {
431   kill_or_be_killed (0);
432   target_create_inferior (exec, args, env);
433 }
434
435 static void
436 default_clone_and_follow_inferior (child_pid, followed_child)
437   int  child_pid;
438   int  *followed_child;
439 {
440   target_clone_and_follow_inferior (child_pid, followed_child);
441 }
442
443 /* Clean up a target struct so it no longer has any zero pointers in it.
444    We default entries, at least to stubs that print error messages.  */
445
446 static void
447 cleanup_target (t)
448      struct target_ops *t;
449 {
450
451 #define de_fault(field, value) \
452   if (!t->field)        t->field = value
453
454   /*        FIELD                       DEFAULT VALUE        */
455
456   de_fault (to_open,                    (void (*) PARAMS((char *, int))) tcomplain);
457   de_fault (to_close,                   (void (*) PARAMS((int))) target_ignore);
458   de_fault (to_attach,                  maybe_kill_then_attach);
459   de_fault (to_post_attach,             (void (*) PARAMS ((int))) target_ignore);
460   de_fault (to_require_attach,          maybe_kill_then_attach);
461   de_fault (to_detach,                  (void (*) PARAMS((char *, int))) target_ignore);
462   de_fault (to_require_detach,          (void (*) PARAMS((int, char *, int))) target_ignore);
463   de_fault (to_resume,                  (void (*) PARAMS((int, int, enum target_signal))) noprocess);
464   de_fault (to_wait,                    (int (*) PARAMS((int, struct target_waitstatus *))) noprocess);
465   de_fault (to_post_wait,               (void (*) PARAMS ((int, int))) target_ignore);
466   de_fault (to_fetch_registers,         (void (*) PARAMS((int))) target_ignore);
467   de_fault (to_store_registers,         (void (*) PARAMS((int))) noprocess);
468   de_fault (to_prepare_to_store,        (void (*) PARAMS((void))) noprocess);
469   de_fault (to_xfer_memory,             (int (*) PARAMS((CORE_ADDR, char *, int, int, struct target_ops *))) nomemory);
470   de_fault (to_files_info,              (void (*) PARAMS((struct target_ops *))) target_ignore);
471   de_fault (to_insert_breakpoint,       memory_insert_breakpoint);
472   de_fault (to_remove_breakpoint,       memory_remove_breakpoint);
473   de_fault (to_terminal_init,           (void (*) PARAMS((void))) target_ignore);
474   de_fault (to_terminal_inferior,       (void (*) PARAMS ((void))) target_ignore);
475   de_fault (to_terminal_ours_for_output,(void (*) PARAMS ((void))) target_ignore);
476   de_fault (to_terminal_ours,           (void (*) PARAMS ((void))) target_ignore);
477   de_fault (to_terminal_info,           default_terminal_info);
478   de_fault (to_kill,                    (void (*) PARAMS((void))) noprocess);
479   de_fault (to_load,                    (void (*) PARAMS((char *, int))) tcomplain);
480   de_fault (to_lookup_symbol,           (int (*) PARAMS ((char *, CORE_ADDR *))) nosymbol);
481   de_fault (to_create_inferior,         maybe_kill_then_create_inferior);
482   de_fault (to_post_startup_inferior,   (void (*) PARAMS ((int))) target_ignore);
483   de_fault (to_acknowledge_created_inferior,            (void (*) PARAMS((int))) target_ignore);
484   de_fault (to_clone_and_follow_inferior,               default_clone_and_follow_inferior);
485   de_fault (to_post_follow_inferior_by_clone,           (void (*) PARAMS ((void))) target_ignore);
486   de_fault (to_insert_fork_catchpoint,  (int (*) PARAMS ((int))) tcomplain);
487   de_fault (to_remove_fork_catchpoint,  (int (*) PARAMS ((int))) tcomplain);
488   de_fault (to_insert_vfork_catchpoint, (int (*) PARAMS ((int))) tcomplain);
489   de_fault (to_remove_vfork_catchpoint, (int (*) PARAMS ((int))) tcomplain);
490   de_fault (to_has_forked,              (int (*) PARAMS ((int, int *))) return_zero);
491   de_fault (to_has_vforked,             (int (*) PARAMS ((int, int *))) return_zero);
492   de_fault (to_can_follow_vfork_prior_to_exec, (int (*) PARAMS ((void ))) return_zero);
493   de_fault (to_post_follow_vfork,       (void (*) PARAMS ((int, int, int, int))) target_ignore);
494   de_fault (to_insert_exec_catchpoint,  (int (*) PARAMS ((int))) tcomplain);
495   de_fault (to_remove_exec_catchpoint,  (int (*) PARAMS ((int))) tcomplain);
496   de_fault (to_has_execd,               (int (*) PARAMS ((int, char **))) return_zero);
497   de_fault (to_reported_exec_events_per_exec_call, (int (*) PARAMS ((void))) return_one);
498   de_fault (to_has_syscall_event,       (int (*) PARAMS ((int, enum target_waitkind *, int *))) return_zero);
499   de_fault (to_has_exited,              (int (*) PARAMS ((int, int, int *))) return_zero);
500   de_fault (to_mourn_inferior,          (void (*) PARAMS ((void))) noprocess);
501   de_fault (to_can_run,                 return_zero);
502   de_fault (to_notice_signals,          (void (*) PARAMS((int))) target_ignore);
503   de_fault (to_thread_alive,            (int (*) PARAMS((int))) target_ignore);
504   de_fault (to_stop,                    (void (*) PARAMS((void))) target_ignore);
505   de_fault (to_enable_exception_callback,       (struct symtab_and_line * (*) PARAMS((enum exception_event_kind, int))) nosupport_runtime);
506   de_fault (to_get_current_exception_event,     (struct exception_event_record * (*) PARAMS((void))) nosupport_runtime);
507
508   de_fault (to_pid_to_exec_file,        (char* (*) PARAMS((int))) return_zero);
509   de_fault (to_core_file_to_sym_file,   (char* (*) PARAMS ((char *))) return_zero);
510 #undef de_fault
511 }
512
513 /* Go through the target stack from top to bottom, copying over zero entries in
514    current_target.  In effect, we are doing class inheritance through the
515    pushed target vectors.  */
516
517 static void
518 update_current_target ()
519 {
520   struct target_stack_item *item;
521   struct target_ops *t;
522
523   /* First, reset current_target */
524   memset (&current_target, 0, sizeof current_target);
525
526   for (item = target_stack; item; item = item->next)
527     {
528       t = item->target_ops;
529
530 #define INHERIT(FIELD, TARGET) \
531       if (!current_target.FIELD) \
532         current_target.FIELD = TARGET->FIELD
533
534       INHERIT (to_shortname, t);
535       INHERIT (to_longname, t);
536       INHERIT (to_doc, t);
537       INHERIT (to_open, t);
538       INHERIT (to_close, t);
539       INHERIT (to_attach, t);
540       INHERIT (to_post_attach, t);
541       INHERIT (to_require_attach, t);
542       INHERIT (to_detach, t);
543       INHERIT (to_require_detach, t);
544       INHERIT (to_resume, t);
545       INHERIT (to_wait, t);
546       INHERIT (to_post_wait, t);
547       INHERIT (to_fetch_registers, t);
548       INHERIT (to_store_registers, t);
549       INHERIT (to_prepare_to_store, t);
550       INHERIT (to_xfer_memory, t);
551       INHERIT (to_files_info, t);
552       INHERIT (to_insert_breakpoint, t);
553       INHERIT (to_remove_breakpoint, t);
554       INHERIT (to_terminal_init, t);
555       INHERIT (to_terminal_inferior, t);
556       INHERIT (to_terminal_ours_for_output, t);
557       INHERIT (to_terminal_ours, t);
558       INHERIT (to_terminal_info, t);
559       INHERIT (to_kill, t);
560       INHERIT (to_load, t);
561       INHERIT (to_lookup_symbol, t);
562       INHERIT (to_create_inferior, t);
563       INHERIT (to_post_startup_inferior, t);
564       INHERIT (to_acknowledge_created_inferior, t);
565       INHERIT (to_clone_and_follow_inferior, t);
566       INHERIT (to_post_follow_inferior_by_clone, t);
567       INHERIT (to_insert_fork_catchpoint, t);
568       INHERIT (to_remove_fork_catchpoint, t);
569       INHERIT (to_insert_vfork_catchpoint, t);
570       INHERIT (to_remove_vfork_catchpoint, t);
571       INHERIT (to_has_forked, t);
572       INHERIT (to_has_vforked, t);
573       INHERIT (to_can_follow_vfork_prior_to_exec, t);
574       INHERIT (to_post_follow_vfork, t);
575       INHERIT (to_insert_exec_catchpoint, t);
576       INHERIT (to_remove_exec_catchpoint, t);
577       INHERIT (to_has_execd, t);
578       INHERIT (to_reported_exec_events_per_exec_call, t);
579       INHERIT (to_has_syscall_event, t);
580       INHERIT (to_has_exited, t);
581       INHERIT (to_mourn_inferior, t);
582       INHERIT (to_can_run, t);
583       INHERIT (to_notice_signals, t);
584       INHERIT (to_thread_alive, t);
585       INHERIT (to_stop, t);
586       INHERIT (to_enable_exception_callback, t);
587       INHERIT (to_get_current_exception_event, t);
588       INHERIT (to_pid_to_exec_file, t);
589       INHERIT (to_core_file_to_sym_file, t);
590       INHERIT (to_stratum, t);
591       INHERIT (DONT_USE, t);
592       INHERIT (to_has_all_memory, t);
593       INHERIT (to_has_memory, t);
594       INHERIT (to_has_stack, t);
595       INHERIT (to_has_registers, t);
596       INHERIT (to_has_execution, t);
597       INHERIT (to_has_thread_control, t);
598       INHERIT (to_sections, t);
599       INHERIT (to_sections_end, t);
600       INHERIT (to_magic, t);
601
602 #undef INHERIT
603     }
604 }
605
606 /* Push a new target type into the stack of the existing target accessors,
607    possibly superseding some of the existing accessors.
608
609    Result is zero if the pushed target ended up on top of the stack,
610    nonzero if at least one target is on top of it.
611
612    Rather than allow an empty stack, we always have the dummy target at
613    the bottom stratum, so we can call the function vectors without
614    checking them.  */
615
616 int
617 push_target (t)
618      struct target_ops *t;
619 {
620   struct target_stack_item *cur, *prev, *tmp;
621
622   /* Check magic number.  If wrong, it probably means someone changed
623      the struct definition, but not all the places that initialize one.  */
624   if (t->to_magic != OPS_MAGIC)
625     {
626       fprintf_unfiltered(gdb_stderr,
627                          "Magic number of %s target struct wrong\n", 
628                          t->to_shortname);
629       abort();
630     }
631
632   /* Find the proper stratum to install this target in. */
633
634   for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
635     {
636       if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
637         break;
638     }
639
640   /* If there's already targets at this stratum, remove them. */
641
642   if (cur)
643     while (t->to_stratum == cur->target_ops->to_stratum)
644       {
645         /* There's already something on this stratum.  Close it off.  */
646         if (cur->target_ops->to_close)
647           (cur->target_ops->to_close) (0);
648         if (prev)
649           prev->next = cur->next; /* Unchain old target_ops */
650         else
651           target_stack = cur->next; /* Unchain first on list */
652         tmp = cur->next;
653         free (cur);
654         cur = tmp;
655       }
656
657   /* We have removed all targets in our stratum, now add the new one.  */
658
659   tmp = (struct target_stack_item *)
660     xmalloc (sizeof (struct target_stack_item));
661   tmp->next = cur;
662   tmp->target_ops = t;
663
664   if (prev)
665     prev->next = tmp;
666   else
667     target_stack = tmp;
668
669   update_current_target ();
670
671   cleanup_target (&current_target); /* Fill in the gaps */
672
673 #ifdef MAINTENANCE_CMDS
674   if (targetdebug)
675     setup_target_debug ();
676 #endif
677
678   return prev != 0;
679 }
680
681 /* Remove a target_ops vector from the stack, wherever it may be. 
682    Return how many times it was removed (0 or 1).  */
683
684 int
685 unpush_target (t)
686      struct target_ops *t;
687 {
688   struct target_stack_item *cur, *prev;
689
690   if (t->to_close)
691     t->to_close (0);            /* Let it clean up */
692
693   /* Look for the specified target.  Note that we assume that a target
694      can only occur once in the target stack. */
695
696   for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
697     if (cur->target_ops == t)
698       break;
699
700   if (!cur)
701     return 0;                   /* Didn't find target_ops, quit now */
702
703   /* Unchain the target */
704
705   if (!prev)
706     target_stack = cur->next;
707   else
708     prev->next = cur->next;
709
710   free (cur);                   /* Release the target_stack_item */
711
712   update_current_target ();
713   cleanup_target (&current_target);
714
715   return 1;
716 }
717
718 void
719 pop_target ()
720 {
721   (current_target.to_close)(0); /* Let it clean up */
722   if (unpush_target (target_stack->target_ops) == 1)
723     return;
724
725   fprintf_unfiltered(gdb_stderr,
726                      "pop_target couldn't find target %s\n", 
727                      current_target.to_shortname);
728   abort();
729 }
730
731 #undef  MIN
732 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
733
734 /* target_read_string -- read a null terminated string, up to LEN bytes,
735    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
736    Set *STRING to a pointer to malloc'd memory containing the data; the caller
737    is responsible for freeing it.  Return the number of bytes successfully
738    read.  */
739
740 int
741 target_read_string (memaddr, string, len, errnop)
742      CORE_ADDR memaddr;
743      char **string;
744      int len;
745      int *errnop;
746 {
747   int tlen, origlen, offset, i;
748   char buf[4];
749   int errcode = 0;
750   char *buffer;
751   int buffer_allocated;
752   char *bufptr;
753   unsigned int nbytes_read = 0;
754
755   /* Small for testing.  */
756   buffer_allocated = 4;
757   buffer = xmalloc (buffer_allocated);
758   bufptr = buffer;
759
760   origlen = len;
761
762   while (len > 0)
763     {
764       tlen = MIN (len, 4 - (memaddr & 3));
765       offset = memaddr & 3;
766
767       errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0, NULL);
768       if (errcode != 0)
769         {
770           /* The transfer request might have crossed the boundary to an
771              unallocated region of memory. Retry the transfer, requesting
772              a single byte.  */
773           tlen = 1;
774           offset = 0;
775           errcode = target_xfer_memory (memaddr, buf, 1, 0, NULL);
776           if (errcode != 0)
777             goto done;
778         }
779
780       if (bufptr - buffer + tlen > buffer_allocated)
781         {
782           unsigned int bytes;
783           bytes = bufptr - buffer;
784           buffer_allocated *= 2;
785           buffer = xrealloc (buffer, buffer_allocated);
786           bufptr = buffer + bytes;
787         }
788
789       for (i = 0; i < tlen; i++)
790         {
791           *bufptr++ = buf[i + offset];
792           if (buf[i + offset] == '\000')
793             {
794               nbytes_read += i + 1;
795               goto done;
796             }
797         }
798
799       memaddr += tlen;
800       len -= tlen;
801       nbytes_read += tlen;
802     }
803  done:
804   if (errnop != NULL)
805     *errnop = errcode;
806   if (string != NULL)
807     *string = buffer;
808   return nbytes_read;
809 }
810
811 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
812    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
813    if any error occurs.
814
815    If an error occurs, no guarantee is made about the contents of the data at
816    MYADDR.  In particular, the caller should not depend upon partial reads
817    filling the buffer with good data.  There is no way for the caller to know
818    how much good data might have been transfered anyway.  Callers that can
819    deal with partial reads should call target_read_memory_partial. */
820
821 int
822 target_read_memory (memaddr, myaddr, len)
823      CORE_ADDR memaddr;
824      char *myaddr;
825      int len;
826 {
827   return target_xfer_memory (memaddr, myaddr, len, 0, NULL);
828 }
829
830 int
831 target_read_memory_section (memaddr, myaddr, len, bfd_section)
832      CORE_ADDR memaddr;
833      char *myaddr;
834      int len;
835      asection *bfd_section;
836 {
837   return target_xfer_memory (memaddr, myaddr, len, 0, bfd_section);
838 }
839
840 /* Read LEN bytes of target memory at address MEMADDR, placing the results
841    in GDB's memory at MYADDR.  Returns a count of the bytes actually read,
842    and optionally an errno value in the location pointed to by ERRNOPTR
843    if ERRNOPTR is non-null. */
844
845 int
846 target_read_memory_partial (memaddr, myaddr, len, errnoptr)
847      CORE_ADDR memaddr;
848      char *myaddr;
849      int len;
850      int *errnoptr;
851 {
852   int nread;    /* Number of bytes actually read. */
853   int errcode;  /* Error from last read. */
854
855   /* First try a complete read. */
856   errcode = target_xfer_memory (memaddr, myaddr, len, 0, NULL);
857   if (errcode == 0)
858     {
859       /* Got it all. */
860       nread = len;
861     }
862   else
863     {
864       /* Loop, reading one byte at a time until we get as much as we can. */
865       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
866         {
867           errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0, NULL);
868         }
869       /* If an error, the last read was unsuccessful, so adjust count. */
870       if (errcode != 0)
871         {
872           nread--;
873         }
874     }
875   if (errnoptr != NULL)
876     {
877       *errnoptr = errcode;
878     }
879   return (nread);
880 }
881
882 int
883 target_write_memory (memaddr, myaddr, len)
884      CORE_ADDR memaddr;
885      char *myaddr;
886      int len;
887 {
888   return target_xfer_memory (memaddr, myaddr, len, 1, NULL);
889 }
890  
891 /* This variable is used to pass section information down to targets.  This
892    *should* be done by adding an argument to the target_xfer_memory function
893    of all the targets, but I didn't feel like changing 50+ files.  */
894
895 asection *target_memory_bfd_section = NULL;
896
897 /* Move memory to or from the targets.  Iterate until all of it has
898    been moved, if necessary.  The top target gets priority; anything
899    it doesn't want, is offered to the next one down, etc.  Note the
900    business with curlen:  if an early target says "no, but I have a
901    boundary overlapping this xfer" then we shorten what we offer to
902    the subsequent targets so the early guy will get a chance at the
903    tail before the subsequent ones do. 
904
905    Result is 0 or errno value.  */
906
907 static int
908 target_xfer_memory (memaddr, myaddr, len, write, bfd_section)
909      CORE_ADDR memaddr;
910      char *myaddr;
911      int len;
912      int write;
913      asection *bfd_section;
914 {
915   int curlen;
916   int res;
917   struct target_ops *t;
918   struct target_stack_item *item;
919
920   /* Zero length requests are ok and require no work.  */
921   if (len == 0)
922     return 0;
923
924   target_memory_bfd_section = bfd_section;
925
926   /* to_xfer_memory is not guaranteed to set errno, even when it returns
927      0.  */
928   errno = 0;
929
930   /* The quick case is that the top target does it all.  */
931   res = current_target.to_xfer_memory
932                         (memaddr, myaddr, len, write, &current_target);
933   if (res == len)
934     return 0;
935
936   if (res > 0)
937     goto bump;
938   /* If res <= 0 then we call it again in the loop.  Ah well.  */
939
940   for (; len > 0;)
941     {
942       curlen = len;             /* Want to do it all */
943       for (item = target_stack; item; item = item->next)
944         {
945           t = item->target_ops;
946           if (!t->to_has_memory)
947             continue;
948
949           res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
950           if (res > 0)
951             break;              /* Handled all or part of xfer */
952           if (t->to_has_all_memory)
953             break;
954         }
955
956       if (res <= 0)
957         {
958           /* If this address is for nonexistent memory,
959              read zeros if reading, or do nothing if writing.  Return error. */
960           if (!write)
961             memset (myaddr, 0, len);
962           if (errno == 0)
963             return EIO;
964           else
965             return errno;
966         }
967 bump:
968       memaddr += res;
969       myaddr  += res;
970       len     -= res;
971     }
972   return 0;                     /* We managed to cover it all somehow. */
973 }
974
975
976 /* ARGSUSED */
977 static void
978 target_info (args, from_tty)
979      char *args;
980      int from_tty;
981 {
982   struct target_ops *t;
983   struct target_stack_item *item;
984   int has_all_mem = 0;
985   
986   if (symfile_objfile != NULL)
987     printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
988
989 #ifdef FILES_INFO_HOOK
990   if (FILES_INFO_HOOK ())
991     return;
992 #endif
993
994   for (item = target_stack; item; item = item->next)
995     {
996       t = item->target_ops;
997
998       if (!t->to_has_memory)
999         continue;
1000
1001       if ((int)(t->to_stratum) <= (int)dummy_stratum)
1002         continue;
1003       if (has_all_mem)
1004         printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
1005       printf_unfiltered("%s:\n", t->to_longname);
1006       (t->to_files_info)(t);
1007       has_all_mem = t->to_has_all_memory;
1008     }
1009 }
1010
1011 /* This is to be called by the open routine before it does
1012    anything.  */
1013
1014 void
1015 target_preopen (from_tty)
1016      int from_tty;
1017 {
1018   dont_repeat();
1019
1020   if (target_has_execution)
1021     {   
1022       if (query ("A program is being debugged already.  Kill it? "))
1023         target_kill ();
1024       else
1025         error ("Program not killed.");
1026     }
1027
1028   /* Calling target_kill may remove the target from the stack.  But if
1029      it doesn't (which seems like a win for UDI), remove it now.  */
1030
1031   if (target_has_execution)
1032     pop_target ();
1033 }
1034
1035 /* Detach a target after doing deferred register stores.  */
1036
1037 void
1038 target_detach (args, from_tty)
1039      char *args;
1040      int from_tty;
1041 {
1042   /* Handle any optimized stores to the inferior.  */
1043 #ifdef DO_DEFERRED_STORES
1044   DO_DEFERRED_STORES;
1045 #endif
1046   (current_target.to_detach) (args, from_tty);
1047 }
1048
1049 void
1050 target_link (modname, t_reloc)
1051      char *modname;
1052      CORE_ADDR *t_reloc;
1053 {
1054   if (STREQ(current_target.to_shortname, "rombug"))
1055     {
1056       (current_target.to_lookup_symbol) (modname, t_reloc);
1057       if (*t_reloc == 0)
1058       error("Unable to link to %s and get relocation in rombug", modname);
1059     }
1060   else
1061     *t_reloc = (CORE_ADDR)-1;
1062 }
1063
1064 /* Look through the list of possible targets for a target that can
1065    execute a run or attach command without any other data.  This is
1066    used to locate the default process stratum.
1067
1068    Result is always valid (error() is called for errors).  */
1069
1070 static struct target_ops *
1071 find_default_run_target (do_mesg)
1072      char *do_mesg;
1073 {
1074   struct target_ops **t;
1075   struct target_ops *runable = NULL;
1076   int count;
1077
1078   count = 0;
1079
1080   for (t = target_structs; t < target_structs + target_struct_size;
1081        ++t)
1082     {
1083       if ((*t)->to_can_run && target_can_run(*t))
1084         {
1085           runable = *t;
1086           ++count;
1087         }
1088     }
1089
1090   if (count != 1)
1091     error ("Don't know how to %s.  Try \"help target\".", do_mesg);
1092
1093   return runable;
1094 }
1095
1096 void
1097 find_default_attach (args, from_tty)
1098      char *args;
1099      int from_tty;
1100 {
1101   struct target_ops *t;
1102
1103   t = find_default_run_target("attach");
1104   (t->to_attach) (args, from_tty);
1105   return;
1106 }
1107
1108 void
1109 find_default_require_attach (args, from_tty)
1110      char *args;
1111      int from_tty;
1112 {
1113   struct target_ops *t;
1114
1115   t = find_default_run_target("require_attach");
1116   (t->to_require_attach) (args, from_tty);
1117   return;
1118 }
1119
1120 void
1121 find_default_require_detach (pid, args, from_tty)
1122   int  pid;
1123   char *  args;
1124   int  from_tty;
1125 {
1126   struct target_ops *t;
1127
1128   t = find_default_run_target("require_detach");
1129   (t->to_require_detach) (pid, args, from_tty);
1130   return;
1131 }
1132
1133 void
1134 find_default_create_inferior (exec_file, allargs, env)
1135      char *exec_file;
1136      char *allargs;
1137      char **env;
1138 {
1139   struct target_ops *t;
1140
1141   t = find_default_run_target("run");
1142   (t->to_create_inferior) (exec_file, allargs, env);
1143   return;
1144 }
1145
1146 void
1147 find_default_clone_and_follow_inferior (child_pid, followed_child)
1148   int  child_pid;
1149   int  *followed_child;
1150 {
1151   struct target_ops *t;
1152
1153   t = find_default_run_target("run");
1154   (t->to_clone_and_follow_inferior) (child_pid, followed_child);
1155   return;
1156 }
1157
1158 static int
1159 return_zero ()
1160 {
1161   return 0;
1162 }
1163
1164 static int
1165 return_one ()
1166 {
1167   return 1;
1168 }
1169
1170 struct target_ops *
1171 find_core_target ()
1172 {
1173   struct target_ops **t;
1174   struct target_ops *runable = NULL;
1175   int count;
1176   
1177   count = 0;
1178   
1179   for (t = target_structs; t < target_structs + target_struct_size;
1180        ++t)
1181     {
1182       if ((*t)->to_stratum == core_stratum)
1183         {
1184           runable = *t;
1185           ++count;
1186         }
1187     }
1188   
1189   return(count == 1 ? runable : NULL);
1190 }
1191 \f
1192 /* The inferior process has died.  Long live the inferior!  */
1193
1194 void
1195 generic_mourn_inferior ()
1196 {
1197   extern int show_breakpoint_hit_counts;
1198
1199   inferior_pid = 0;
1200   attach_flag = 0;
1201   breakpoint_init_inferior (inf_exited);
1202   registers_changed ();
1203
1204 #ifdef CLEAR_DEFERRED_STORES
1205   /* Delete any pending stores to the inferior... */
1206   CLEAR_DEFERRED_STORES;
1207 #endif
1208
1209   reopen_exec_file ();
1210   reinit_frame_cache ();
1211
1212   /* It is confusing to the user for ignore counts to stick around
1213      from previous runs of the inferior.  So clear them.  */
1214   /* However, it is more confusing for the ignore counts to disappear when
1215      using hit counts.  So don't clear them if we're counting hits.  */
1216   if (!show_breakpoint_hit_counts)
1217     breakpoint_clear_ignore_counts ();
1218 }
1219 \f
1220 /* This table must match in order and size the signals in enum target_signal
1221    in target.h.  */
1222 static struct {
1223   char *name;
1224   char *string;
1225   } signals [] =
1226 {
1227   {"0", "Signal 0"},
1228   {"SIGHUP", "Hangup"},
1229   {"SIGINT", "Interrupt"},
1230   {"SIGQUIT", "Quit"},
1231   {"SIGILL", "Illegal instruction"},
1232   {"SIGTRAP", "Trace/breakpoint trap"},
1233   {"SIGABRT", "Aborted"},
1234   {"SIGEMT", "Emulation trap"},
1235   {"SIGFPE", "Arithmetic exception"},
1236   {"SIGKILL", "Killed"},
1237   {"SIGBUS", "Bus error"},
1238   {"SIGSEGV", "Segmentation fault"},
1239   {"SIGSYS", "Bad system call"},
1240   {"SIGPIPE", "Broken pipe"},
1241   {"SIGALRM", "Alarm clock"},
1242   {"SIGTERM", "Terminated"},
1243   {"SIGURG", "Urgent I/O condition"},
1244   {"SIGSTOP", "Stopped (signal)"},
1245   {"SIGTSTP", "Stopped (user)"},
1246   {"SIGCONT", "Continued"},
1247   {"SIGCHLD", "Child status changed"},
1248   {"SIGTTIN", "Stopped (tty input)"},
1249   {"SIGTTOU", "Stopped (tty output)"},
1250   {"SIGIO", "I/O possible"},
1251   {"SIGXCPU", "CPU time limit exceeded"},
1252   {"SIGXFSZ", "File size limit exceeded"},
1253   {"SIGVTALRM", "Virtual timer expired"},
1254   {"SIGPROF", "Profiling timer expired"},
1255   {"SIGWINCH", "Window size changed"},
1256   {"SIGLOST", "Resource lost"},
1257   {"SIGUSR1", "User defined signal 1"},
1258   {"SIGUSR2", "User defined signal 2"},
1259   {"SIGPWR", "Power fail/restart"},
1260   {"SIGPOLL", "Pollable event occurred"},
1261   {"SIGWIND", "SIGWIND"},
1262   {"SIGPHONE", "SIGPHONE"},
1263   {"SIGWAITING", "Process's LWPs are blocked"},
1264   {"SIGLWP", "Signal LWP"},
1265   {"SIGDANGER", "Swap space dangerously low"},
1266   {"SIGGRANT", "Monitor mode granted"},
1267   {"SIGRETRACT", "Need to relinquish monitor mode"},
1268   {"SIGMSG", "Monitor mode data available"},
1269   {"SIGSOUND", "Sound completed"},
1270   {"SIGSAK", "Secure attention"},
1271   {"SIGPRIO", "SIGPRIO"},
1272   {"SIG33", "Real-time event 33"},
1273   {"SIG34", "Real-time event 34"},
1274   {"SIG35", "Real-time event 35"},
1275   {"SIG36", "Real-time event 36"},
1276   {"SIG37", "Real-time event 37"},
1277   {"SIG38", "Real-time event 38"},
1278   {"SIG39", "Real-time event 39"},
1279   {"SIG40", "Real-time event 40"},
1280   {"SIG41", "Real-time event 41"},
1281   {"SIG42", "Real-time event 42"},
1282   {"SIG43", "Real-time event 43"},
1283   {"SIG44", "Real-time event 44"},
1284   {"SIG45", "Real-time event 45"},
1285   {"SIG46", "Real-time event 46"},
1286   {"SIG47", "Real-time event 47"},
1287   {"SIG48", "Real-time event 48"},
1288   {"SIG49", "Real-time event 49"},
1289   {"SIG50", "Real-time event 50"},
1290   {"SIG51", "Real-time event 51"},
1291   {"SIG52", "Real-time event 52"},
1292   {"SIG53", "Real-time event 53"},
1293   {"SIG54", "Real-time event 54"},
1294   {"SIG55", "Real-time event 55"},
1295   {"SIG56", "Real-time event 56"},
1296   {"SIG57", "Real-time event 57"},
1297   {"SIG58", "Real-time event 58"},
1298   {"SIG59", "Real-time event 59"},
1299   {"SIG60", "Real-time event 60"},
1300   {"SIG61", "Real-time event 61"},
1301   {"SIG62", "Real-time event 62"},
1302   {"SIG63", "Real-time event 63"},
1303
1304 #if defined(MACH) || defined(__MACH__)
1305   /* Mach exceptions */
1306   {"EXC_BAD_ACCESS", "Could not access memory"},
1307   {"EXC_BAD_INSTRUCTION", "Illegal instruction/operand"},
1308   {"EXC_ARITHMETIC", "Arithmetic exception"},
1309   {"EXC_EMULATION", "Emulation instruction"},
1310   {"EXC_SOFTWARE", "Software generated exception"},
1311   {"EXC_BREAKPOINT", "Breakpoint"},
1312 #endif
1313   {NULL, "Unknown signal"},
1314   {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1315
1316   /* Last entry, used to check whether the table is the right size.  */
1317   {NULL, "TARGET_SIGNAL_MAGIC"}
1318 };
1319
1320 /* Return the string for a signal.  */
1321 char *
1322 target_signal_to_string (sig)
1323      enum target_signal sig;
1324 {
1325   return signals[sig].string;
1326 }
1327
1328 /* Return the name for a signal.  */
1329 char *
1330 target_signal_to_name (sig)
1331      enum target_signal sig;
1332 {
1333   if (sig == TARGET_SIGNAL_UNKNOWN)
1334     /* I think the code which prints this will always print it along with
1335        the string, so no need to be verbose.  */
1336     return "?";
1337   return signals[sig].name;
1338 }
1339
1340 /* Given a name, return its signal.  */
1341 enum target_signal
1342 target_signal_from_name (name)
1343      char *name;
1344 {
1345   enum target_signal sig;
1346
1347   /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1348      for TARGET_SIGNAL_SIGCHLD.  SIGIOT, on the other hand, is more
1349      questionable; seems like by now people should call it SIGABRT
1350      instead.  */
1351
1352   /* This ugly cast brought to you by the native VAX compiler.  */
1353   for (sig = TARGET_SIGNAL_HUP;
1354        signals[sig].name != NULL;
1355        sig = (enum target_signal)((int)sig + 1))
1356     if (STREQ (name, signals[sig].name))
1357       return sig;
1358   return TARGET_SIGNAL_UNKNOWN;
1359 }
1360 \f
1361 /* The following functions are to help certain targets deal
1362    with the signal/waitstatus stuff.  They could just as well be in
1363    a file called native-utils.c or unixwaitstatus-utils.c or whatever.  */
1364
1365 /* Convert host signal to our signals.  */
1366 enum target_signal
1367 target_signal_from_host (hostsig)
1368      int hostsig;
1369 {
1370   /* A switch statement would make sense but would require special kludges
1371      to deal with the cases where more than one signal has the same number.  */
1372
1373   if (hostsig == 0) return TARGET_SIGNAL_0;
1374
1375 #if defined (SIGHUP)
1376   if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1377 #endif
1378 #if defined (SIGINT)
1379   if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1380 #endif
1381 #if defined (SIGQUIT)
1382   if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1383 #endif
1384 #if defined (SIGILL)
1385   if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1386 #endif
1387 #if defined (SIGTRAP)
1388   if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1389 #endif
1390 #if defined (SIGABRT)
1391   if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1392 #endif
1393 #if defined (SIGEMT)
1394   if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1395 #endif
1396 #if defined (SIGFPE)
1397   if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1398 #endif
1399 #if defined (SIGKILL)
1400   if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1401 #endif
1402 #if defined (SIGBUS)
1403   if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1404 #endif
1405 #if defined (SIGSEGV)
1406   if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1407 #endif
1408 #if defined (SIGSYS)
1409   if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1410 #endif
1411 #if defined (SIGPIPE)
1412   if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1413 #endif
1414 #if defined (SIGALRM)
1415   if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1416 #endif
1417 #if defined (SIGTERM)
1418   if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1419 #endif
1420 #if defined (SIGUSR1)
1421   if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1422 #endif
1423 #if defined (SIGUSR2)
1424   if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1425 #endif
1426 #if defined (SIGCLD)
1427   if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1428 #endif
1429 #if defined (SIGCHLD)
1430   if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1431 #endif
1432 #if defined (SIGPWR)
1433   if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1434 #endif
1435 #if defined (SIGWINCH)
1436   if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1437 #endif
1438 #if defined (SIGURG)
1439   if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1440 #endif
1441 #if defined (SIGIO)
1442   if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1443 #endif
1444 #if defined (SIGPOLL)
1445   if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1446 #endif
1447 #if defined (SIGSTOP)
1448   if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1449 #endif
1450 #if defined (SIGTSTP)
1451   if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1452 #endif
1453 #if defined (SIGCONT)
1454   if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1455 #endif
1456 #if defined (SIGTTIN)
1457   if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1458 #endif
1459 #if defined (SIGTTOU)
1460   if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1461 #endif
1462 #if defined (SIGVTALRM)
1463   if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1464 #endif
1465 #if defined (SIGPROF)
1466   if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1467 #endif
1468 #if defined (SIGXCPU)
1469   if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1470 #endif
1471 #if defined (SIGXFSZ)
1472   if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1473 #endif
1474 #if defined (SIGWIND)
1475   if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1476 #endif
1477 #if defined (SIGPHONE)
1478   if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1479 #endif
1480 #if defined (SIGLOST)
1481   if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1482 #endif
1483 #if defined (SIGWAITING)
1484   if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1485 #endif
1486 #if defined (SIGLWP)
1487   if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1488 #endif
1489 #if defined (SIGDANGER)
1490   if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1491 #endif
1492 #if defined (SIGGRANT)
1493   if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1494 #endif
1495 #if defined (SIGRETRACT)
1496   if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1497 #endif
1498 #if defined (SIGMSG)
1499   if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1500 #endif
1501 #if defined (SIGSOUND)
1502   if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1503 #endif
1504 #if defined (SIGSAK)
1505   if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1506 #endif
1507 #if defined (SIGPRIO)
1508   if (hostsig == SIGPRIO) return TARGET_SIGNAL_PRIO;
1509 #endif
1510
1511   /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1512 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1513   if (hostsig == _NSIG + EXC_BAD_ACCESS) return TARGET_EXC_BAD_ACCESS;
1514 #endif
1515 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1516   if (hostsig == _NSIG + EXC_BAD_INSTRUCTION) return TARGET_EXC_BAD_INSTRUCTION;
1517 #endif
1518 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1519   if (hostsig == _NSIG + EXC_ARITHMETIC) return TARGET_EXC_ARITHMETIC;
1520 #endif
1521 #if defined (EXC_EMULATION) && defined (_NSIG)
1522   if (hostsig == _NSIG + EXC_EMULATION) return TARGET_EXC_EMULATION;
1523 #endif
1524 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1525   if (hostsig == _NSIG + EXC_SOFTWARE) return TARGET_EXC_SOFTWARE;
1526 #endif
1527 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1528   if (hostsig == _NSIG + EXC_BREAKPOINT) return TARGET_EXC_BREAKPOINT;
1529 #endif
1530
1531 #if defined (REALTIME_LO)
1532   if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
1533     return (enum target_signal)
1534       (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1535 #endif
1536   return TARGET_SIGNAL_UNKNOWN;
1537 }
1538
1539 int
1540 target_signal_to_host (oursig)
1541      enum target_signal oursig;
1542 {
1543   switch (oursig)
1544     {
1545     case TARGET_SIGNAL_0: return 0;
1546
1547 #if defined (SIGHUP)
1548     case TARGET_SIGNAL_HUP: return SIGHUP;
1549 #endif
1550 #if defined (SIGINT)
1551     case TARGET_SIGNAL_INT: return SIGINT;
1552 #endif
1553 #if defined (SIGQUIT)
1554     case TARGET_SIGNAL_QUIT: return SIGQUIT;
1555 #endif
1556 #if defined (SIGILL)
1557     case TARGET_SIGNAL_ILL: return SIGILL;
1558 #endif
1559 #if defined (SIGTRAP)
1560     case TARGET_SIGNAL_TRAP: return SIGTRAP;
1561 #endif
1562 #if defined (SIGABRT)
1563     case TARGET_SIGNAL_ABRT: return SIGABRT;
1564 #endif
1565 #if defined (SIGEMT)
1566     case TARGET_SIGNAL_EMT: return SIGEMT;
1567 #endif
1568 #if defined (SIGFPE)
1569     case TARGET_SIGNAL_FPE: return SIGFPE;
1570 #endif
1571 #if defined (SIGKILL)
1572     case TARGET_SIGNAL_KILL: return SIGKILL;
1573 #endif
1574 #if defined (SIGBUS)
1575     case TARGET_SIGNAL_BUS: return SIGBUS;
1576 #endif
1577 #if defined (SIGSEGV)
1578     case TARGET_SIGNAL_SEGV: return SIGSEGV;
1579 #endif
1580 #if defined (SIGSYS)
1581     case TARGET_SIGNAL_SYS: return SIGSYS;
1582 #endif
1583 #if defined (SIGPIPE)
1584     case TARGET_SIGNAL_PIPE: return SIGPIPE;
1585 #endif
1586 #if defined (SIGALRM)
1587     case TARGET_SIGNAL_ALRM: return SIGALRM;
1588 #endif
1589 #if defined (SIGTERM)
1590     case TARGET_SIGNAL_TERM: return SIGTERM;
1591 #endif
1592 #if defined (SIGUSR1)
1593     case TARGET_SIGNAL_USR1: return SIGUSR1;
1594 #endif
1595 #if defined (SIGUSR2)
1596     case TARGET_SIGNAL_USR2: return SIGUSR2;
1597 #endif
1598 #if defined (SIGCHLD) || defined (SIGCLD)
1599     case TARGET_SIGNAL_CHLD: 
1600 #if defined (SIGCHLD)
1601       return SIGCHLD;
1602 #else
1603       return SIGCLD;
1604 #endif
1605 #endif /* SIGCLD or SIGCHLD */
1606 #if defined (SIGPWR)
1607     case TARGET_SIGNAL_PWR: return SIGPWR;
1608 #endif
1609 #if defined (SIGWINCH)
1610     case TARGET_SIGNAL_WINCH: return SIGWINCH;
1611 #endif
1612 #if defined (SIGURG)
1613     case TARGET_SIGNAL_URG: return SIGURG;
1614 #endif
1615 #if defined (SIGIO)
1616     case TARGET_SIGNAL_IO: return SIGIO;
1617 #endif
1618 #if defined (SIGPOLL)
1619     case TARGET_SIGNAL_POLL: return SIGPOLL;
1620 #endif
1621 #if defined (SIGSTOP)
1622     case TARGET_SIGNAL_STOP: return SIGSTOP;
1623 #endif
1624 #if defined (SIGTSTP)
1625     case TARGET_SIGNAL_TSTP: return SIGTSTP;
1626 #endif
1627 #if defined (SIGCONT)
1628     case TARGET_SIGNAL_CONT: return SIGCONT;
1629 #endif
1630 #if defined (SIGTTIN)
1631     case TARGET_SIGNAL_TTIN: return SIGTTIN;
1632 #endif
1633 #if defined (SIGTTOU)
1634     case TARGET_SIGNAL_TTOU: return SIGTTOU;
1635 #endif
1636 #if defined (SIGVTALRM)
1637     case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1638 #endif
1639 #if defined (SIGPROF)
1640     case TARGET_SIGNAL_PROF: return SIGPROF;
1641 #endif
1642 #if defined (SIGXCPU)
1643     case TARGET_SIGNAL_XCPU: return SIGXCPU;
1644 #endif
1645 #if defined (SIGXFSZ)
1646     case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1647 #endif
1648 #if defined (SIGWIND)
1649     case TARGET_SIGNAL_WIND: return SIGWIND;
1650 #endif
1651 #if defined (SIGPHONE)
1652     case TARGET_SIGNAL_PHONE: return SIGPHONE;
1653 #endif
1654 #if defined (SIGLOST)
1655     case TARGET_SIGNAL_LOST: return SIGLOST;
1656 #endif
1657 #if defined (SIGWAITING)
1658     case TARGET_SIGNAL_WAITING: return SIGWAITING;
1659 #endif
1660 #if defined (SIGLWP)
1661     case TARGET_SIGNAL_LWP: return SIGLWP;
1662 #endif
1663 #if defined (SIGDANGER)
1664     case TARGET_SIGNAL_DANGER: return SIGDANGER;
1665 #endif
1666 #if defined (SIGGRANT)
1667     case TARGET_SIGNAL_GRANT: return SIGGRANT;
1668 #endif
1669 #if defined (SIGRETRACT)
1670     case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1671 #endif
1672 #if defined (SIGMSG)
1673     case TARGET_SIGNAL_MSG: return SIGMSG;
1674 #endif
1675 #if defined (SIGSOUND)
1676     case TARGET_SIGNAL_SOUND: return SIGSOUND;
1677 #endif
1678 #if defined (SIGSAK)
1679     case TARGET_SIGNAL_SAK: return SIGSAK;
1680 #endif
1681 #if defined (SIGPRIO)
1682     case TARGET_SIGNAL_PRIO: return SIGPRIO;
1683 #endif
1684
1685       /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1686 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1687     case TARGET_EXC_BAD_ACCESS: return _NSIG + EXC_BAD_ACCESS;
1688 #endif
1689 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1690     case TARGET_EXC_BAD_INSTRUCTION: return _NSIG + EXC_BAD_INSTRUCTION;
1691 #endif
1692 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1693     case TARGET_EXC_ARITHMETIC: return _NSIG + EXC_ARITHMETIC;
1694 #endif
1695 #if defined (EXC_EMULATION) && defined (_NSIG)
1696     case TARGET_EXC_EMULATION: return _NSIG + EXC_EMULATION;
1697 #endif
1698 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1699     case TARGET_EXC_SOFTWARE: return _NSIG + EXC_SOFTWARE;
1700 #endif
1701 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1702     case TARGET_EXC_BREAKPOINT: return _NSIG + EXC_BREAKPOINT;
1703 #endif
1704
1705     default:
1706 #if defined (REALTIME_LO)
1707       if (oursig >= TARGET_SIGNAL_REALTIME_33
1708           && oursig <= TARGET_SIGNAL_REALTIME_63)
1709         {
1710           int retsig =
1711             (int)oursig - (int)TARGET_SIGNAL_REALTIME_33 + REALTIME_LO;
1712           if (retsig < REALTIME_HI)
1713             return retsig;
1714         }
1715 #endif
1716       /* The user might be trying to do "signal SIGSAK" where this system
1717          doesn't have SIGSAK.  */
1718       warning ("Signal %s does not exist on this system.\n",
1719                target_signal_to_name (oursig));
1720       return 0;
1721     }
1722 }
1723
1724 /* Helper function for child_wait and the Lynx derivatives of child_wait.
1725    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1726    translation of that in OURSTATUS.  */
1727 void
1728 store_waitstatus (ourstatus, hoststatus)
1729      struct target_waitstatus *ourstatus;
1730      int hoststatus;
1731 {
1732 #ifdef CHILD_SPECIAL_WAITSTATUS
1733   /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1734      if it wants to deal with hoststatus.  */
1735   if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1736     return;
1737 #endif
1738
1739   if (WIFEXITED (hoststatus))
1740     {
1741       ourstatus->kind = TARGET_WAITKIND_EXITED;
1742       ourstatus->value.integer = WEXITSTATUS (hoststatus);
1743     }
1744   else if (!WIFSTOPPED (hoststatus))
1745     {
1746       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1747       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1748     }
1749   else
1750     {
1751       ourstatus->kind = TARGET_WAITKIND_STOPPED;
1752       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1753     }
1754 }
1755 \f
1756 /* In some circumstances we allow a command to specify a numeric
1757    signal.  The idea is to keep these circumstances limited so that
1758    users (and scripts) develop portable habits.  For comparison,
1759    POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
1760    numeric signal at all is obscelescent.  We are slightly more
1761    lenient and allow 1-15 which should match host signal numbers on
1762    most systems.  Use of symbolic signal names is strongly encouraged.  */
1763
1764 enum target_signal
1765 target_signal_from_command (num)
1766      int num;
1767 {
1768   if (num >= 1 && num <= 15)
1769     return (enum target_signal)num;
1770   error ("Only signals 1-15 are valid as numeric signals.\n\
1771 Use \"info signals\" for a list of symbolic signals.");
1772 }
1773 \f
1774 /* Returns zero to leave the inferior alone, one to interrupt it.  */
1775 int (*target_activity_function) PARAMS ((void));
1776 int target_activity_fd;
1777 \f
1778 /* Convert a normal process ID to a string.  Returns the string in a static
1779    buffer.  */
1780
1781 char *
1782 normal_pid_to_str (pid)
1783      int pid;
1784 {
1785   static char buf[30];
1786
1787   if (STREQ (current_target.to_shortname, "remote"))
1788     sprintf (buf, "thread %d\0", pid);
1789   else
1790     sprintf (buf, "process %d\0", pid);
1791
1792   return buf;
1793 }
1794
1795 /* Some targets (such as ttrace-based HPUX) don't allow us to request
1796    notification of inferior events such as fork and vork immediately
1797    after the inferior is created.  (This because of how gdb gets an
1798    inferior created via invoking a shell to do it.  In such a scenario,
1799    if the shell init file has commands in it, the shell will fork and
1800    exec for each of those commands, and we will see each such fork
1801    event.  Very bad.)
1802    
1803    This function is used by all targets that allow us to request
1804    notification of forks, etc at inferior creation time; e.g., in
1805    target_acknowledge_forked_child.
1806    */
1807 void
1808 normal_target_post_startup_inferior (pid)
1809   int  pid;
1810 {
1811   /* This space intentionally left blank. */
1812 }
1813
1814 \f
1815 #ifdef MAINTENANCE_CMDS
1816 static struct target_ops debug_target;
1817
1818 static void
1819 debug_to_open (args, from_tty)
1820      char *args;
1821      int from_tty;
1822 {
1823   debug_target.to_open (args, from_tty);
1824
1825   fprintf_unfiltered (gdb_stderr, "target_open (%s, %d)\n", args, from_tty);
1826 }
1827
1828 static void
1829 debug_to_close (quitting)
1830      int quitting;
1831 {
1832   debug_target.to_close (quitting);
1833
1834   fprintf_unfiltered (gdb_stderr, "target_close (%d)\n", quitting);
1835 }
1836
1837 static void
1838 debug_to_attach (args, from_tty)
1839      char *args;
1840      int from_tty;
1841 {
1842   debug_target.to_attach (args, from_tty);
1843
1844   fprintf_unfiltered (gdb_stderr, "target_attach (%s, %d)\n", args, from_tty);
1845 }
1846
1847
1848 static void
1849 debug_to_post_attach (pid)
1850   int  pid;
1851 {
1852   debug_target.to_post_attach (pid);
1853
1854   fprintf_unfiltered (gdb_stderr, "target_post_attach (%d)\n", pid);
1855 }
1856
1857 static void
1858 debug_to_require_attach (args, from_tty)
1859      char *args;
1860      int from_tty;
1861 {
1862   debug_target.to_require_attach (args, from_tty);
1863
1864   fprintf_unfiltered (gdb_stderr,
1865                       "target_require_attach (%s, %d)\n", args, from_tty);
1866 }
1867
1868 static void
1869 debug_to_detach (args, from_tty)
1870      char *args;
1871      int from_tty;
1872 {
1873   debug_target.to_detach (args, from_tty);
1874
1875   fprintf_unfiltered (gdb_stderr, "target_detach (%s, %d)\n", args, from_tty);
1876 }
1877
1878 static void
1879 debug_to_require_detach (pid, args, from_tty)
1880   int  pid;
1881   char *  args;
1882   int  from_tty;
1883 {
1884   debug_target.to_require_detach (pid, args, from_tty);
1885
1886   fprintf_unfiltered (gdb_stderr,
1887                       "target_require_detach (%d, %s, %d)\n", pid, args, from_tty);
1888 }
1889
1890 static void
1891 debug_to_resume (pid, step, siggnal)
1892      int pid;
1893      int step;
1894      enum target_signal siggnal;
1895 {
1896   debug_target.to_resume (pid, step, siggnal);
1897
1898   fprintf_unfiltered (gdb_stderr, "target_resume (%d, %s, %s)\n", pid,
1899                       step ? "step" : "continue",
1900                       target_signal_to_name (siggnal));
1901 }
1902
1903 static int
1904 debug_to_wait (pid, status)
1905      int pid;
1906      struct target_waitstatus *status;
1907 {
1908   int retval;
1909
1910   retval = debug_target.to_wait (pid, status);
1911
1912   fprintf_unfiltered (gdb_stderr,
1913                       "target_wait (%d, status) = %d,   ", pid, retval);
1914   fprintf_unfiltered (gdb_stderr, "status->kind = ");
1915   switch (status->kind)
1916     {
1917     case TARGET_WAITKIND_EXITED:
1918       fprintf_unfiltered (gdb_stderr, "exited, status = %d\n",
1919                           status->value.integer);
1920       break;
1921     case TARGET_WAITKIND_STOPPED:
1922       fprintf_unfiltered (gdb_stderr, "stopped, signal = %s\n",
1923                           target_signal_to_name (status->value.sig));
1924       break;
1925     case TARGET_WAITKIND_SIGNALLED:
1926       fprintf_unfiltered (gdb_stderr, "signalled, signal = %s\n",
1927                           target_signal_to_name (status->value.sig));
1928       break;
1929     case TARGET_WAITKIND_LOADED:
1930       fprintf_unfiltered (gdb_stderr, "loaded\n");
1931       break;
1932     case TARGET_WAITKIND_FORKED:
1933       fprintf_unfiltered (gdb_stderr, "forked\n");
1934       break;
1935     case TARGET_WAITKIND_VFORKED:
1936       fprintf_unfiltered (gdb_stderr, "vforked\n");
1937       break;
1938     case TARGET_WAITKIND_EXECD:
1939       fprintf_unfiltered (gdb_stderr, "execd\n");
1940       break;
1941     case TARGET_WAITKIND_SPURIOUS:
1942       fprintf_unfiltered (gdb_stderr, "spurious\n");
1943       break;
1944     default:
1945       fprintf_unfiltered (gdb_stderr, "unknown???\n");
1946       break;
1947     }
1948
1949   return retval;
1950 }
1951
1952 static void
1953 debug_to_post_wait (pid, status)
1954   int  pid;
1955   int  status;
1956 {
1957   debug_target.to_post_wait (pid, status);
1958
1959   fprintf_unfiltered (gdb_stderr, "target_post_wait (%d, %d)\n",
1960                       pid, status);
1961 }
1962
1963 static void
1964 debug_to_fetch_registers (regno)
1965      int regno;
1966 {
1967   debug_target.to_fetch_registers (regno);
1968
1969   fprintf_unfiltered (gdb_stderr, "target_fetch_registers (%s)",
1970                       regno != -1 ? REGISTER_NAME (regno) : "-1");
1971   if (regno != -1)
1972     fprintf_unfiltered (gdb_stderr, " = 0x%x %d",
1973                         (unsigned long) read_register (regno),
1974                         read_register (regno));
1975   fprintf_unfiltered (gdb_stderr, "\n");
1976 }
1977
1978 static void
1979 debug_to_store_registers (regno)
1980      int regno;
1981 {
1982   debug_target.to_store_registers (regno);
1983
1984   if (regno >= 0 && regno < NUM_REGS)
1985     fprintf_unfiltered (gdb_stderr, "target_store_registers (%s) = 0x%x %d\n",
1986                         REGISTER_NAME (regno),
1987                         (unsigned long) read_register (regno),
1988                         (unsigned long) read_register (regno));
1989   else
1990     fprintf_unfiltered (gdb_stderr, "target_store_registers (%d)\n", regno);
1991 }
1992
1993 static void
1994 debug_to_prepare_to_store ()
1995 {
1996   debug_target.to_prepare_to_store ();
1997
1998   fprintf_unfiltered (gdb_stderr, "target_prepare_to_store ()\n");
1999 }
2000
2001 static int
2002 debug_to_xfer_memory (memaddr, myaddr, len, write, target)
2003      CORE_ADDR memaddr;
2004      char *myaddr;
2005      int len;
2006      int write;
2007      struct target_ops *target;
2008 {
2009   int retval;
2010
2011   retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write, target);
2012
2013   fprintf_unfiltered (gdb_stderr,
2014                       "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
2015                       (unsigned int) memaddr, /* possable truncate long long */
2016                       len, write ? "write" : "read", retval);
2017
2018   
2019
2020   if (retval > 0)
2021     {
2022       int i;
2023
2024       fputs_unfiltered (", bytes =", gdb_stderr);
2025       for (i = 0; i < retval; i++)
2026         {
2027           if ((((long) &(myaddr[i])) & 0xf) == 0)
2028             fprintf_unfiltered (gdb_stderr, "\n");
2029           fprintf_unfiltered (gdb_stderr, " %02x", myaddr[i] & 0xff);
2030         }
2031     }
2032
2033   fputc_unfiltered ('\n', gdb_stderr);
2034
2035   return retval;
2036 }
2037
2038 static void
2039 debug_to_files_info (target)
2040      struct target_ops *target;
2041 {
2042   debug_target.to_files_info (target);
2043
2044   fprintf_unfiltered (gdb_stderr, "target_files_info (xxx)\n");
2045 }
2046
2047 static int
2048 debug_to_insert_breakpoint (addr, save)
2049      CORE_ADDR addr;
2050      char *save;
2051 {
2052   int retval;
2053
2054   retval = debug_target.to_insert_breakpoint (addr, save);
2055
2056   fprintf_unfiltered (gdb_stderr,
2057                       "target_insert_breakpoint (0x%x, xxx) = %d\n",
2058                       (unsigned long) addr, retval);
2059   return retval;
2060 }
2061
2062 static int
2063 debug_to_remove_breakpoint (addr, save)
2064      CORE_ADDR addr;
2065      char *save;
2066 {
2067   int retval;
2068
2069   retval = debug_target.to_remove_breakpoint (addr, save);
2070
2071   fprintf_unfiltered (gdb_stderr,
2072                       "target_remove_breakpoint (0x%x, xxx) = %d\n",
2073                       (unsigned long)addr, retval);
2074   return retval;
2075 }
2076
2077 static void
2078 debug_to_terminal_init ()
2079 {
2080   debug_target.to_terminal_init ();
2081
2082   fprintf_unfiltered (gdb_stderr, "target_terminal_init ()\n");
2083 }
2084
2085 static void
2086 debug_to_terminal_inferior ()
2087 {
2088   debug_target.to_terminal_inferior ();
2089
2090   fprintf_unfiltered (gdb_stderr, "target_terminal_inferior ()\n");
2091 }
2092
2093 static void
2094 debug_to_terminal_ours_for_output ()
2095 {
2096   debug_target.to_terminal_ours_for_output ();
2097
2098   fprintf_unfiltered (gdb_stderr, "target_terminal_ours_for_output ()\n");
2099 }
2100
2101 static void
2102 debug_to_terminal_ours ()
2103 {
2104   debug_target.to_terminal_ours ();
2105
2106   fprintf_unfiltered (gdb_stderr, "target_terminal_ours ()\n");
2107 }
2108
2109 static void
2110 debug_to_terminal_info (arg, from_tty)
2111      char *arg;
2112      int from_tty;
2113 {
2114   debug_target.to_terminal_info (arg, from_tty);
2115
2116   fprintf_unfiltered (gdb_stderr, "target_terminal_info (%s, %d)\n", arg,
2117                       from_tty);
2118 }
2119
2120 static void
2121 debug_to_kill ()
2122 {
2123   debug_target.to_kill ();
2124
2125   fprintf_unfiltered (gdb_stderr, "target_kill ()\n");
2126 }
2127
2128 static void
2129 debug_to_load (args, from_tty)
2130      char *args;
2131      int from_tty;
2132 {
2133   debug_target.to_load (args, from_tty);
2134
2135   fprintf_unfiltered (gdb_stderr, "target_load (%s, %d)\n", args, from_tty);
2136 }
2137
2138 static int
2139 debug_to_lookup_symbol (name, addrp)
2140      char *name;
2141      CORE_ADDR *addrp;
2142 {
2143   int retval;
2144
2145   retval = debug_target.to_lookup_symbol (name, addrp);
2146
2147   fprintf_unfiltered (gdb_stderr, "target_lookup_symbol (%s, xxx)\n", name);
2148
2149   return retval;
2150 }
2151
2152 static void
2153 debug_to_create_inferior (exec_file, args, env)
2154      char *exec_file;
2155      char *args;
2156      char **env;
2157 {
2158   debug_target.to_create_inferior (exec_file, args, env);
2159
2160   fprintf_unfiltered (gdb_stderr, "target_create_inferior (%s, %s, xxx)\n",
2161                       exec_file, args);
2162 }
2163
2164 static void
2165 debug_to_post_startup_inferior (pid)
2166   int  pid;
2167 {
2168   debug_target.to_post_startup_inferior (pid);
2169
2170   fprintf_unfiltered (gdb_stderr, "target_post_startup_inferior (%d)\n",
2171                       pid);
2172 }
2173
2174 static void
2175 debug_to_acknowledge_created_inferior (pid)
2176   int  pid;
2177 {
2178   debug_target.to_acknowledge_created_inferior (pid);
2179
2180   fprintf_unfiltered (gdb_stderr, "target_acknowledge_created_inferior (%d)\n",
2181                       pid);
2182 }
2183
2184 static void
2185 debug_to_clone_and_follow_inferior (child_pid, followed_child)
2186   int  child_pid;
2187   int  *followed_child;
2188 {
2189   debug_target.to_clone_and_follow_inferior (child_pid, followed_child);
2190
2191   fprintf_unfiltered (gdb_stderr,
2192                       "target_clone_and_follow_inferior (%d, %d)\n",
2193                       child_pid, *followed_child);
2194 }
2195
2196 static void
2197 debug_to_post_follow_inferior_by_clone ()
2198 {
2199   debug_target.to_post_follow_inferior_by_clone ();
2200
2201   fprintf_unfiltered (gdb_stderr, "target_post_follow_inferior_by_clone ()\n");
2202 }
2203
2204 static int
2205 debug_to_insert_fork_catchpoint (pid)
2206   int  pid;
2207 {
2208   int  retval;
2209
2210   retval = debug_target.to_insert_fork_catchpoint (pid);
2211
2212   fprintf_unfiltered (gdb_stderr, "target_insert_fork_catchpoint (%d) = %d\n",
2213                       pid, retval);
2214
2215   return retval;
2216 }
2217
2218 static int
2219 debug_to_remove_fork_catchpoint (pid)
2220   int  pid;
2221 {
2222   int  retval;
2223
2224   retval = debug_target.to_remove_fork_catchpoint (pid);
2225
2226   fprintf_unfiltered (gdb_stderr, "target_remove_fork_catchpoint (%d) = %d\n",
2227                       pid, retval);
2228
2229   return retval;
2230 }
2231
2232 static int
2233 debug_to_insert_vfork_catchpoint (pid)
2234   int  pid;
2235 {
2236   int  retval;
2237
2238   retval = debug_target.to_insert_vfork_catchpoint (pid);
2239
2240   fprintf_unfiltered (gdb_stderr, "target_insert_vfork_catchpoint (%d)= %d\n",
2241                       pid, retval);
2242
2243   return retval;
2244 }
2245
2246 static int
2247 debug_to_remove_vfork_catchpoint (pid)
2248   int  pid;
2249 {
2250   int  retval;
2251
2252   retval = debug_target.to_remove_vfork_catchpoint (pid);
2253
2254   fprintf_unfiltered (gdb_stderr, "target_remove_vfork_catchpoint (%d) = %d\n",
2255                       pid, retval);
2256
2257   return retval;
2258 }
2259
2260 static int
2261 debug_to_has_forked (pid, child_pid)
2262   int  pid;
2263   int *  child_pid;
2264 {
2265   int  has_forked;
2266
2267   has_forked = debug_target.to_has_forked (pid, child_pid);
2268
2269   fprintf_unfiltered (gdb_stderr, "target_has_forked (%d, %d) = %d\n",
2270                       pid, *child_pid, has_forked);
2271
2272   return has_forked;
2273 }
2274
2275 static int
2276 debug_to_has_vforked (pid, child_pid)
2277   int  pid;
2278   int *  child_pid;
2279 {
2280   int  has_vforked;
2281
2282   has_vforked = debug_target.to_has_vforked (pid, child_pid);
2283
2284   fprintf_unfiltered (gdb_stderr, "target_has_vforked (%d, %d) = %d\n",
2285                       pid, *child_pid, has_vforked);
2286
2287   return has_vforked;
2288 }
2289
2290 static int
2291 debug_to_can_follow_vfork_prior_to_exec ()
2292 {
2293   int  can_immediately_follow_vfork;
2294
2295   can_immediately_follow_vfork = debug_target.to_can_follow_vfork_prior_to_exec ();
2296
2297   fprintf_unfiltered (gdb_stderr, "target_can_follow_vfork_prior_to_exec () = %d\n",
2298                       can_immediately_follow_vfork);
2299
2300   return can_immediately_follow_vfork;
2301 }
2302
2303 static void
2304 debug_to_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child)
2305   int  parent_pid;
2306   int  followed_parent;
2307   int  child_pid;
2308   int  followed_child;
2309 {
2310   debug_target.to_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child);
2311
2312   fprintf_unfiltered (gdb_stderr,
2313                       "target_post_follow_vfork (%d, %d, %d, %d)\n",
2314                       parent_pid, followed_parent, child_pid, followed_child);
2315 }
2316
2317 static int
2318 debug_to_insert_exec_catchpoint (pid)
2319   int  pid;
2320 {
2321   int  retval;
2322
2323   retval = debug_target.to_insert_exec_catchpoint (pid);
2324
2325   fprintf_unfiltered (gdb_stderr, "target_insert_exec_catchpoint (%d) = %d\n",
2326                       pid, retval);
2327
2328   return retval;
2329 }
2330
2331 static int
2332 debug_to_remove_exec_catchpoint (pid)
2333   int  pid;
2334 {
2335   int  retval;
2336
2337   retval = debug_target.to_remove_exec_catchpoint (pid);
2338
2339   fprintf_unfiltered (gdb_stderr, "target_remove_exec_catchpoint (%d) = %d\n",
2340                       pid, retval);
2341
2342   return retval;
2343 }
2344
2345 static int
2346 debug_to_has_execd (pid, execd_pathname)
2347   int  pid;
2348   char **  execd_pathname;
2349 {
2350   int  has_execd;
2351
2352   has_execd = debug_target.to_has_execd (pid, execd_pathname);
2353
2354   fprintf_unfiltered (gdb_stderr, "target_has_execd (%d, %s) = %d\n",
2355                       pid, *execd_pathname, has_execd);
2356
2357   return has_execd;
2358 }
2359
2360 static int
2361 debug_to_reported_exec_events_per_exec_call ()
2362 {
2363   int  reported_exec_events;
2364
2365   reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
2366
2367   fprintf_unfiltered (gdb_stderr,
2368                       "target_reported_exec_events_per_exec_call () = %d\n",
2369                       reported_exec_events);
2370
2371   return reported_exec_events;
2372 }
2373
2374 static int
2375 debug_to_has_syscall_event (pid, kind, syscall_id)
2376   int  pid;
2377   enum target_waitkind *  kind;
2378   int *  syscall_id;
2379 {
2380   int  has_syscall_event;
2381   char *  kind_spelling = "??";
2382
2383   has_syscall_event = debug_target.to_has_syscall_event (pid, kind, syscall_id);
2384   if (has_syscall_event)
2385     {
2386       switch (*kind)
2387         {
2388           case TARGET_WAITKIND_SYSCALL_ENTRY:
2389             kind_spelling = "SYSCALL_ENTRY";
2390             break;
2391           case TARGET_WAITKIND_SYSCALL_RETURN:
2392             kind_spelling = "SYSCALL_RETURN";
2393             break;
2394           default:
2395             break;
2396         }
2397     }
2398
2399   fprintf_unfiltered (gdb_stderr,
2400                       "target_has_syscall_event (%d, %s, %d) = %d\n",
2401                       pid, kind_spelling, *syscall_id, has_syscall_event);
2402
2403   return has_syscall_event;
2404 }
2405
2406 static int
2407 debug_to_has_exited (pid, wait_status, exit_status)
2408   int  pid;
2409   int  wait_status;
2410   int *  exit_status;
2411 {
2412   int  has_exited;
2413
2414   has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2415
2416   fprintf_unfiltered (gdb_stderr, "target_has_exited (%d, %d, %d) = %d\n",
2417                       pid, wait_status, *exit_status, has_exited);
2418
2419   return has_exited;
2420 }
2421
2422 static void
2423 debug_to_mourn_inferior ()
2424 {
2425   debug_target.to_mourn_inferior ();
2426
2427   fprintf_unfiltered (gdb_stderr, "target_mourn_inferior ()\n");
2428 }
2429
2430 static int
2431 debug_to_can_run ()
2432 {
2433   int retval;
2434
2435   retval = debug_target.to_can_run ();
2436
2437   fprintf_unfiltered (gdb_stderr, "target_can_run () = %d\n", retval);
2438
2439   return retval;
2440 }
2441
2442 static void
2443 debug_to_notice_signals (pid)
2444      int pid;
2445 {
2446   debug_target.to_notice_signals (pid);
2447
2448   fprintf_unfiltered (gdb_stderr, "target_notice_signals (%d)\n", pid);
2449 }
2450
2451 static int
2452 debug_to_thread_alive (pid)
2453      int pid;
2454 {
2455   int retval;
2456
2457   retval = debug_target.to_thread_alive (pid);
2458
2459   fprintf_unfiltered (gdb_stderr, "target_thread_alive (%d) = %d\n",
2460                       pid, retval);
2461
2462   return retval;
2463 }
2464
2465 static void
2466 debug_to_stop ()
2467 {
2468   debug_target.to_stop ();
2469
2470   fprintf_unfiltered (gdb_stderr, "target_stop ()\n");
2471 }
2472
2473 static struct symtab_and_line *
2474 debug_to_enable_exception_callback (kind, enable)
2475   enum exception_event_kind kind;
2476   int enable;
2477 {
2478   debug_target.to_enable_exception_callback (kind, enable);
2479
2480   fprintf_unfiltered (gdb_stderr,
2481                       "target get_exception_callback_sal (%d, %d)\n",
2482                       kind, enable);
2483 }
2484
2485 static struct exception_event_record *
2486 debug_to_get_current_exception_event ()
2487 {
2488   debug_target.to_get_current_exception_event();
2489
2490   fprintf_unfiltered (gdb_stderr, "target get_current_exception_event ()\n");
2491 }
2492
2493 static char *
2494 debug_to_pid_to_exec_file (pid)
2495   int  pid;
2496 {
2497   char *  exec_file;
2498
2499   exec_file = debug_target.to_pid_to_exec_file (pid);
2500
2501   fprintf_unfiltered (gdb_stderr, "target_pid_to_exec_file (%d) = %s\n",
2502                       pid, exec_file);
2503
2504   return exec_file;
2505 }
2506
2507 static char *
2508 debug_to_core_file_to_sym_file (core)
2509   char *  core;
2510 {
2511   char *  sym_file;
2512
2513   sym_file = debug_target.to_core_file_to_sym_file (core);
2514
2515   fprintf_unfiltered (gdb_stderr, "target_core_file_to_sym_file (%s) = %s\n",
2516                       core, sym_file);
2517
2518   return sym_file;
2519 }
2520
2521 static void
2522 setup_target_debug ()
2523 {
2524   memcpy (&debug_target, &current_target, sizeof debug_target);
2525
2526   current_target.to_open = debug_to_open;
2527   current_target.to_close = debug_to_close;
2528   current_target.to_attach = debug_to_attach;
2529   current_target.to_post_attach = debug_to_post_attach;
2530   current_target.to_require_attach = debug_to_require_attach;
2531   current_target.to_detach = debug_to_detach;
2532   current_target.to_require_detach = debug_to_require_detach;
2533   current_target.to_resume = debug_to_resume;
2534   current_target.to_wait = debug_to_wait;
2535   current_target.to_post_wait = debug_to_post_wait;
2536   current_target.to_fetch_registers = debug_to_fetch_registers;
2537   current_target.to_store_registers = debug_to_store_registers;
2538   current_target.to_prepare_to_store = debug_to_prepare_to_store;
2539   current_target.to_xfer_memory = debug_to_xfer_memory;
2540   current_target.to_files_info = debug_to_files_info;
2541   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2542   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2543   current_target.to_terminal_init = debug_to_terminal_init;
2544   current_target.to_terminal_inferior = debug_to_terminal_inferior;
2545   current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2546   current_target.to_terminal_ours = debug_to_terminal_ours;
2547   current_target.to_terminal_info = debug_to_terminal_info;
2548   current_target.to_kill = debug_to_kill;
2549   current_target.to_load = debug_to_load;
2550   current_target.to_lookup_symbol = debug_to_lookup_symbol;
2551   current_target.to_create_inferior = debug_to_create_inferior;
2552   current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2553   current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
2554   current_target.to_clone_and_follow_inferior = debug_to_clone_and_follow_inferior;
2555   current_target.to_post_follow_inferior_by_clone = debug_to_post_follow_inferior_by_clone;
2556   current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2557   current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2558   current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2559   current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2560   current_target.to_has_forked = debug_to_has_forked;
2561   current_target.to_has_vforked = debug_to_has_vforked;
2562   current_target.to_can_follow_vfork_prior_to_exec = debug_to_can_follow_vfork_prior_to_exec;
2563   current_target.to_post_follow_vfork = debug_to_post_follow_vfork;
2564   current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2565   current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
2566   current_target.to_has_execd = debug_to_has_execd;
2567   current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
2568   current_target.to_has_syscall_event = debug_to_has_syscall_event;
2569   current_target.to_has_exited = debug_to_has_exited;
2570   current_target.to_mourn_inferior = debug_to_mourn_inferior;
2571   current_target.to_can_run = debug_to_can_run;
2572   current_target.to_notice_signals = debug_to_notice_signals;
2573   current_target.to_thread_alive = debug_to_thread_alive;
2574   current_target.to_stop = debug_to_stop;
2575   current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
2576   current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
2577   current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
2578   current_target.to_core_file_to_sym_file = debug_to_core_file_to_sym_file;
2579
2580 }
2581 #endif /* MAINTENANCE_CMDS */
2582 \f
2583 static char targ_desc[] = 
2584     "Names of targets and files being debugged.\n\
2585 Shows the entire stack of targets currently in use (including the exec-file,\n\
2586 core-file, and process, if any), as well as the symbol file name.";
2587
2588 void
2589 initialize_targets ()
2590 {
2591   push_target (&dummy_target);
2592
2593   add_info ("target", target_info, targ_desc);
2594   add_info ("files", target_info, targ_desc);
2595
2596 #ifdef MAINTENANCE_CMDS
2597   add_show_from_set (
2598      add_set_cmd ("targetdebug", class_maintenance, var_zinteger,
2599                   (char *)&targetdebug,
2600                  "Set target debugging.\n\
2601 When non-zero, target debugging is enabled.", &setlist),
2602                      &showlist);
2603 #endif
2604
2605   if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
2606     abort ();
2607 }