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