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