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