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