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