* target.c (target_xfer_memory): Handle requests with zero
[external/binutils.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2    Copyright 1990, 1992, 1993, 1994, 1995, 1998 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 maybe_kill_then_attach PARAMS ((char *, int));
48
49 static void
50 kill_or_be_killed PARAMS ((int));
51
52 static void
53 default_terminal_info PARAMS ((char *, int));
54
55 static int
56 nosymbol PARAMS ((char *, CORE_ADDR *));
57
58 static void
59 tcomplain PARAMS ((void));
60
61 static int
62 nomemory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
63
64 static int
65 return_zero PARAMS ((void));
66
67 static void
68 ignore PARAMS ((void));
69
70 static void
71 target_command PARAMS ((char *, int));
72
73 static struct target_ops *
74 find_default_run_target PARAMS ((char *));
75
76 static void
77 update_current_target PARAMS ((void));
78
79 /* Transfer LEN bytes between target address MEMADDR and GDB address MYADDR.
80    Returns 0 for success, errno code for failure (which includes partial
81    transfers--if you want a more useful response to partial transfers, try
82    target_read_memory_partial).  */
83
84 static int
85 target_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
86                             int write, asection *bfd_section));
87
88 static void
89 debug_to_open PARAMS ((char *, int));
90
91 static void
92 debug_to_close PARAMS ((int));
93
94 static void
95 debug_to_attach PARAMS ((char *, int));
96
97 static void
98 debug_to_detach PARAMS ((char *, int));
99
100 static void
101 debug_to_resume PARAMS ((int, int, enum target_signal));
102
103 static int
104 debug_to_wait PARAMS ((int, struct target_waitstatus *));
105
106 static void
107 debug_to_fetch_registers PARAMS ((int));
108
109 static void
110 debug_to_store_registers PARAMS ((int));
111
112 static void
113 debug_to_prepare_to_store PARAMS ((void));
114
115 static int
116 debug_to_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
117
118 static void
119 debug_to_files_info PARAMS ((struct target_ops *));
120
121 static int
122 debug_to_insert_breakpoint PARAMS ((CORE_ADDR, char *));
123
124 static int
125 debug_to_remove_breakpoint PARAMS ((CORE_ADDR, char *));
126
127 static void
128 debug_to_terminal_init PARAMS ((void));
129
130 static void
131 debug_to_terminal_inferior PARAMS ((void));
132
133 static void
134 debug_to_terminal_ours_for_output PARAMS ((void));
135
136 static void
137 debug_to_terminal_ours PARAMS ((void));
138
139 static void
140 debug_to_terminal_info PARAMS ((char *, int));
141
142 static void
143 debug_to_kill PARAMS ((void));
144
145 static void
146 debug_to_load PARAMS ((char *, int));
147
148 static int
149 debug_to_lookup_symbol PARAMS ((char *, CORE_ADDR *));
150
151 static void
152 debug_to_create_inferior PARAMS ((char *, char *, char **));
153
154 static void
155 debug_to_mourn_inferior PARAMS ((void));
156
157 static int
158 debug_to_can_run PARAMS ((void));
159
160 static void
161 debug_to_notice_signals PARAMS ((int));
162
163 static int
164 debug_to_thread_alive PARAMS ((int));
165
166 static void
167 debug_to_stop PARAMS ((void));
168
169 /* Pointer to array of target architecture structures; the size of the
170    array; the current index into the array; the allocated size of the 
171    array.  */
172 struct target_ops **target_structs;
173 unsigned target_struct_size;
174 unsigned target_struct_index;
175 unsigned target_struct_allocsize;
176 #define DEFAULT_ALLOCSIZE       10
177
178 /* The initial current target, so that there is always a semi-valid
179    current target.  */
180
181 struct target_ops dummy_target = {
182   "None",                       /* to_shortname */
183   "None",                       /* to_longname */
184   "",                           /* to_doc */
185   0,                            /* to_open */
186   0,                            /* to_close */
187   find_default_attach,          /* to_attach */
188   0,                            /* to_detach */
189   0,                            /* to_resume */
190   0,                            /* to_wait */
191   0,                            /* to_fetch_registers */
192   0,                            /* to_store_registers */
193   0,                            /* to_prepare_to_store */
194   0,                            /* to_xfer_memory */
195   0,                            /* to_files_info */
196   0,                            /* to_insert_breakpoint */
197   0,                            /* to_remove_breakpoint */
198   0,                            /* to_terminal_init */
199   0,                            /* to_terminal_inferior */
200   0,                            /* to_terminal_ours_for_output */
201   0,                            /* to_terminal_ours */
202   0,                            /* to_terminal_info */
203   0,                            /* to_kill */
204   0,                            /* to_load */
205   0,                            /* to_lookup_symbol */
206   find_default_create_inferior, /* to_create_inferior */
207   0,                            /* to_mourn_inferior */
208   0,                            /* to_can_run */
209   0,                            /* to_notice_signals */
210   0,                            /* to_thread_alive */
211   0,                            /* to_stop */
212   dummy_stratum,                /* to_stratum */
213   0,                            /* to_next */
214   0,                            /* to_has_all_memory */
215   0,                            /* to_has_memory */
216   0,                            /* to_has_stack */
217   0,                            /* to_has_registers */
218   0,                            /* to_has_execution */
219   0,                            /* to_sections */
220   0,                            /* to_sections_end */
221   OPS_MAGIC,                    /* to_magic */
222 };
223
224 /* Top of target stack.  */
225
226 struct target_stack_item *target_stack;
227
228 /* The target structure we are currently using to talk to a process
229    or file or whatever "inferior" we have.  */
230
231 struct target_ops current_target;
232
233 /* Command list for target.  */
234
235 static struct cmd_list_element *targetlist = NULL;
236
237 /* Nonzero if we are debugging an attached outside process
238    rather than an inferior.  */
239
240 int attach_flag;
241
242 #ifdef MAINTENANCE_CMDS
243 /* Non-zero if we want to see trace of target level stuff.  */
244
245 static int targetdebug = 0;
246
247 static void setup_target_debug PARAMS ((void));
248
249 #endif
250
251 /* The user just typed 'target' without the name of a target.  */
252
253 /* ARGSUSED */
254 static void
255 target_command (arg, from_tty)
256      char *arg;
257      int from_tty;
258 {
259   fputs_filtered ("Argument required (target name).  Try `help target'\n",
260                   gdb_stdout);
261 }
262
263 /* Add a possible target architecture to the list.  */
264
265 void
266 add_target (t)
267      struct target_ops *t;
268 {
269   if (!target_structs)
270     {
271       target_struct_allocsize = DEFAULT_ALLOCSIZE;
272       target_structs = (struct target_ops **) xmalloc
273         (target_struct_allocsize * sizeof (*target_structs));
274     }
275   if (target_struct_size >= target_struct_allocsize)
276     {
277       target_struct_allocsize *= 2;
278       target_structs = (struct target_ops **)
279           xrealloc ((char *) target_structs, 
280                     target_struct_allocsize * sizeof (*target_structs));
281     }
282   target_structs[target_struct_size++] = t;
283 /*  cleanup_target (t);*/
284
285   if (targetlist == NULL)
286     add_prefix_cmd ("target", class_run, target_command,
287                     "Connect to a target machine or process.\n\
288 The first argument is the type or protocol of the target machine.\n\
289 Remaining arguments are interpreted by the target protocol.  For more\n\
290 information on the arguments for a particular protocol, type\n\
291 `help target ' followed by the protocol name.",
292                     &targetlist, "target ", 0, &cmdlist);
293   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
294 }
295
296 /* Stub functions */
297
298 static void
299 ignore ()
300 {
301 }
302
303 /* ARGSUSED */
304 static int
305 nomemory (memaddr, myaddr, len, write, t)
306      CORE_ADDR memaddr;
307      char *myaddr;
308      int len;
309      int write;
310      struct target_ops *t;
311 {
312   errno = EIO;          /* Can't read/write this location */
313   return 0;             /* No bytes handled */
314 }
315
316 static void
317 tcomplain ()
318 {
319   error ("You can't do that when your target is `%s'",
320          current_target.to_shortname);
321 }
322
323 void
324 noprocess ()
325 {
326   error ("You can't do that without a process to debug");
327 }
328
329 /* ARGSUSED */
330 static int
331 nosymbol (name, addrp)
332      char *name;
333      CORE_ADDR *addrp;
334 {
335   return 1;             /* Symbol does not exist in target env */
336 }
337
338 /* ARGSUSED */
339 static void
340 default_terminal_info (args, from_tty)
341      char *args;
342      int from_tty;
343 {
344   printf_unfiltered("No saved terminal information.\n");
345 }
346
347 /* This is the default target_create_inferior and target_attach function.
348    If the current target is executing, it asks whether to kill it off.
349    If this function returns without calling error(), it has killed off
350    the target, and the operation should be attempted.  */
351
352 static void
353 kill_or_be_killed (from_tty)
354      int from_tty;
355 {
356   if (target_has_execution)
357     {
358       printf_unfiltered ("You are already running a program:\n");
359       target_files_info ();
360       if (query ("Kill it? ")) {
361         target_kill ();
362         if (target_has_execution)
363           error ("Killing the program did not help.");
364         return;
365       } else {
366         error ("Program not killed.");
367       }
368     }
369   tcomplain();
370 }
371
372 static void
373 maybe_kill_then_attach (args, from_tty)
374      char *args;
375      int from_tty;
376 {
377   kill_or_be_killed (from_tty);
378   target_attach (args, from_tty);
379 }
380
381 static void
382 maybe_kill_then_create_inferior (exec, args, env)
383      char *exec;
384      char *args;
385      char **env;
386 {
387   kill_or_be_killed (0);
388   target_create_inferior (exec, args, env);
389 }
390
391 /* Clean up a target struct so it no longer has any zero pointers in it.
392    We default entries, at least to stubs that print error messages.  */
393
394 static void
395 cleanup_target (t)
396      struct target_ops *t;
397 {
398
399 #define de_fault(field, value) \
400   if (!t->field)        t->field = value
401
402   /*        FIELD                       DEFAULT VALUE        */
403
404   de_fault (to_open,                    (void (*) PARAMS((char *, int))) tcomplain);
405   de_fault (to_close,                   (void (*) PARAMS((int))) ignore);
406   de_fault (to_attach,                  maybe_kill_then_attach);
407   de_fault (to_detach,                  (void (*) PARAMS((char *, int))) ignore);
408   de_fault (to_resume,                  (void (*) PARAMS((int, int, enum target_signal))) noprocess);
409   de_fault (to_wait,                    (int (*) PARAMS((int, struct target_waitstatus *))) noprocess);
410   de_fault (to_fetch_registers,         (void (*) PARAMS((int))) ignore);
411   de_fault (to_store_registers,         (void (*) PARAMS((int))) noprocess);
412   de_fault (to_prepare_to_store,        (void (*) PARAMS((void))) noprocess);
413   de_fault (to_xfer_memory,             (int (*) PARAMS((CORE_ADDR, char *, int, int, struct target_ops *))) nomemory);
414   de_fault (to_files_info,              (void (*) PARAMS((struct target_ops *))) ignore);
415   de_fault (to_insert_breakpoint,       memory_insert_breakpoint);
416   de_fault (to_remove_breakpoint,       memory_remove_breakpoint);
417   de_fault (to_terminal_init,           (void (*) PARAMS((void))) ignore);
418   de_fault (to_terminal_inferior,       (void (*) PARAMS ((void))) ignore);
419   de_fault (to_terminal_ours_for_output,(void (*) PARAMS ((void))) ignore);
420   de_fault (to_terminal_ours,           (void (*) PARAMS ((void))) ignore);
421   de_fault (to_terminal_info,           default_terminal_info);
422   de_fault (to_kill,                    (void (*) PARAMS((void))) noprocess);
423   de_fault (to_load,                    (void (*) PARAMS((char *, int))) tcomplain);
424   de_fault (to_lookup_symbol,           (int (*) PARAMS ((char *, CORE_ADDR *))) nosymbol);
425   de_fault (to_create_inferior,         maybe_kill_then_create_inferior);
426   de_fault (to_mourn_inferior,          (void (*) PARAMS((void))) noprocess);
427   de_fault (to_can_run,                 return_zero);
428   de_fault (to_notice_signals,          (void (*) PARAMS((int))) ignore);
429   de_fault (to_thread_alive,            (int (*) PARAMS((int))) ignore);
430   de_fault (to_stop,                    (void (*) PARAMS((void))) ignore);
431
432 #undef de_fault
433 }
434
435 /* Go through the target stack from top to bottom, copying over zero entries in
436    current_target.  In effect, we are doing class inheritance through the
437    pushed target vectors.  */
438
439 static void
440 update_current_target ()
441 {
442   struct target_stack_item *item;
443   struct target_ops *t;
444
445   /* First, reset current_target */
446   memset (&current_target, 0, sizeof current_target);
447
448   for (item = target_stack; item; item = item->next)
449     {
450       t = item->target_ops;
451
452 #define INHERIT(FIELD, TARGET) \
453       if (!current_target.FIELD) \
454         current_target.FIELD = TARGET->FIELD
455
456       INHERIT (to_shortname, t);
457       INHERIT (to_longname, t);
458       INHERIT (to_doc, t);
459       INHERIT (to_open, t);
460       INHERIT (to_close, t);
461       INHERIT (to_attach, t);
462       INHERIT (to_detach, t);
463       INHERIT (to_resume, t);
464       INHERIT (to_wait, t);
465       INHERIT (to_fetch_registers, t);
466       INHERIT (to_store_registers, t);
467       INHERIT (to_prepare_to_store, t);
468       INHERIT (to_xfer_memory, t);
469       INHERIT (to_files_info, t);
470       INHERIT (to_insert_breakpoint, t);
471       INHERIT (to_remove_breakpoint, t);
472       INHERIT (to_terminal_init, t);
473       INHERIT (to_terminal_inferior, t);
474       INHERIT (to_terminal_ours_for_output, t);
475       INHERIT (to_terminal_ours, t);
476       INHERIT (to_terminal_info, t);
477       INHERIT (to_kill, t);
478       INHERIT (to_load, t);
479       INHERIT (to_lookup_symbol, t);
480       INHERIT (to_create_inferior, t);
481       INHERIT (to_mourn_inferior, t);
482       INHERIT (to_can_run, t);
483       INHERIT (to_notice_signals, t);
484       INHERIT (to_thread_alive, t);
485       INHERIT (to_stop, t);
486       INHERIT (to_stratum, t);
487       INHERIT (DONT_USE, t);
488       INHERIT (to_has_all_memory, t);
489       INHERIT (to_has_memory, t);
490       INHERIT (to_has_stack, t);
491       INHERIT (to_has_registers, t);
492       INHERIT (to_has_execution, t);
493       INHERIT (to_sections, t);
494       INHERIT (to_sections_end, t);
495       INHERIT (to_magic, t);
496
497 #undef INHERIT
498     }
499 }
500
501 /* Push a new target type into the stack of the existing target accessors,
502    possibly superseding some of the existing accessors.
503
504    Result is zero if the pushed target ended up on top of the stack,
505    nonzero if at least one target is on top of it.
506
507    Rather than allow an empty stack, we always have the dummy target at
508    the bottom stratum, so we can call the function vectors without
509    checking them.  */
510
511 int
512 push_target (t)
513      struct target_ops *t;
514 {
515   struct target_stack_item *cur, *prev, *tmp;
516
517   /* Check magic number.  If wrong, it probably means someone changed
518      the struct definition, but not all the places that initialize one.  */
519   if (t->to_magic != OPS_MAGIC)
520     {
521       fprintf_unfiltered(gdb_stderr,
522                          "Magic number of %s target struct wrong\n", 
523                          t->to_shortname);
524       abort();
525     }
526
527   /* Find the proper stratum to install this target in. */
528
529   for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
530     {
531       if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
532         break;
533     }
534
535   /* If there's already targets at this stratum, remove them. */
536
537   if (cur)
538     while (t->to_stratum == cur->target_ops->to_stratum)
539       {
540         /* There's already something on this stratum.  Close it off.  */
541         if (cur->target_ops->to_close)
542           (cur->target_ops->to_close) (0);
543         if (prev)
544           prev->next = cur->next; /* Unchain old target_ops */
545         else
546           target_stack = cur->next; /* Unchain first on list */
547         tmp = cur->next;
548         free (cur);
549         cur = tmp;
550       }
551
552   /* We have removed all targets in our stratum, now add the new one.  */
553
554   tmp = (struct target_stack_item *)
555     xmalloc (sizeof (struct target_stack_item));
556   tmp->next = cur;
557   tmp->target_ops = t;
558
559   if (prev)
560     prev->next = tmp;
561   else
562     target_stack = tmp;
563
564   update_current_target ();
565
566   cleanup_target (&current_target); /* Fill in the gaps */
567
568 #ifdef MAINTENANCE_CMDS
569   if (targetdebug)
570     setup_target_debug ();
571 #endif
572
573   return prev != 0;
574 }
575
576 /* Remove a target_ops vector from the stack, wherever it may be. 
577    Return how many times it was removed (0 or 1).  */
578
579 int
580 unpush_target (t)
581      struct target_ops *t;
582 {
583   struct target_stack_item *cur, *prev;
584
585   if (t->to_close)
586     t->to_close (0);            /* Let it clean up */
587
588   /* Look for the specified target.  Note that we assume that a target
589      can only occur once in the target stack. */
590
591   for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
592     if (cur->target_ops == t)
593       break;
594
595   if (!cur)
596     return 0;                   /* Didn't find target_ops, quit now */
597
598   /* Unchain the target */
599
600   if (!prev)
601     target_stack = cur->next;
602   else
603     prev->next = cur->next;
604
605   free (cur);                   /* Release the target_stack_item */
606
607   update_current_target ();
608   cleanup_target (&current_target);
609
610   return 1;
611 }
612
613 void
614 pop_target ()
615 {
616   (current_target.to_close)(0); /* Let it clean up */
617   if (unpush_target (target_stack->target_ops) == 1)
618     return;
619
620   fprintf_unfiltered(gdb_stderr,
621                      "pop_target couldn't find target %s\n", 
622                      current_target.to_shortname);
623   abort();
624 }
625
626 #undef  MIN
627 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
628
629 /* target_read_string -- read a null terminated string, up to LEN bytes,
630    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
631    Set *STRING to a pointer to malloc'd memory containing the data; the caller
632    is responsible for freeing it.  Return the number of bytes successfully
633    read.  */
634
635 int
636 target_read_string (memaddr, string, len, errnop)
637      CORE_ADDR memaddr;
638      char **string;
639      int len;
640      int *errnop;
641 {
642   int tlen, origlen, offset, i;
643   char buf[4];
644   int errcode = 0;
645   char *buffer;
646   int buffer_allocated;
647   char *bufptr;
648   unsigned int nbytes_read = 0;
649
650   /* Small for testing.  */
651   buffer_allocated = 4;
652   buffer = xmalloc (buffer_allocated);
653   bufptr = buffer;
654
655   origlen = len;
656
657   while (len > 0)
658     {
659       tlen = MIN (len, 4 - (memaddr & 3));
660       offset = memaddr & 3;
661
662       errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0, NULL);
663       if (errcode != 0)
664         {
665           /* The transfer request might have crossed the boundary to an
666              unallocated region of memory. Retry the transfer, requesting
667              a single byte.  */
668           tlen = 1;
669           offset = 0;
670           errcode = target_xfer_memory (memaddr, buf, 1, 0, NULL);
671           if (errcode != 0)
672             goto done;
673         }
674
675       if (bufptr - buffer + tlen > buffer_allocated)
676         {
677           unsigned int bytes;
678           bytes = bufptr - buffer;
679           buffer_allocated *= 2;
680           buffer = xrealloc (buffer, buffer_allocated);
681           bufptr = buffer + bytes;
682         }
683
684       for (i = 0; i < tlen; i++)
685         {
686           *bufptr++ = buf[i + offset];
687           if (buf[i + offset] == '\000')
688             {
689               nbytes_read += i + 1;
690               goto done;
691             }
692         }
693
694       memaddr += tlen;
695       len -= tlen;
696       nbytes_read += tlen;
697     }
698  done:
699   if (errnop != NULL)
700     *errnop = errcode;
701   if (string != NULL)
702     *string = buffer;
703   return nbytes_read;
704 }
705
706 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
707    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
708    if any error occurs.
709
710    If an error occurs, no guarantee is made about the contents of the data at
711    MYADDR.  In particular, the caller should not depend upon partial reads
712    filling the buffer with good data.  There is no way for the caller to know
713    how much good data might have been transfered anyway.  Callers that can
714    deal with partial reads should call target_read_memory_partial. */
715
716 int
717 target_read_memory (memaddr, myaddr, len)
718      CORE_ADDR memaddr;
719      char *myaddr;
720      int len;
721 {
722   return target_xfer_memory (memaddr, myaddr, len, 0, NULL);
723 }
724
725 int
726 target_read_memory_section (memaddr, myaddr, len, bfd_section)
727      CORE_ADDR memaddr;
728      char *myaddr;
729      int len;
730      asection *bfd_section;
731 {
732   return target_xfer_memory (memaddr, myaddr, len, 0, bfd_section);
733 }
734
735 /* Read LEN bytes of target memory at address MEMADDR, placing the results
736    in GDB's memory at MYADDR.  Returns a count of the bytes actually read,
737    and optionally an errno value in the location pointed to by ERRNOPTR
738    if ERRNOPTR is non-null. */
739
740 int
741 target_read_memory_partial (memaddr, myaddr, len, errnoptr)
742      CORE_ADDR memaddr;
743      char *myaddr;
744      int len;
745      int *errnoptr;
746 {
747   int nread;    /* Number of bytes actually read. */
748   int errcode;  /* Error from last read. */
749
750   /* First try a complete read. */
751   errcode = target_xfer_memory (memaddr, myaddr, len, 0, NULL);
752   if (errcode == 0)
753     {
754       /* Got it all. */
755       nread = len;
756     }
757   else
758     {
759       /* Loop, reading one byte at a time until we get as much as we can. */
760       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
761         {
762           errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0, NULL);
763         }
764       /* If an error, the last read was unsuccessful, so adjust count. */
765       if (errcode != 0)
766         {
767           nread--;
768         }
769     }
770   if (errnoptr != NULL)
771     {
772       *errnoptr = errcode;
773     }
774   return (nread);
775 }
776
777 int
778 target_write_memory (memaddr, myaddr, len)
779      CORE_ADDR memaddr;
780      char *myaddr;
781      int len;
782 {
783   return target_xfer_memory (memaddr, myaddr, len, 1, NULL);
784 }
785  
786 /* This variable is used to pass section information down to targets.  This
787    *should* be done by adding an argument to the target_xfer_memory function
788    of all the targets, but I didn't feel like changing 50+ files.  */
789
790 asection *target_memory_bfd_section = NULL;
791
792 /* Move memory to or from the targets.  Iterate until all of it has
793    been moved, if necessary.  The top target gets priority; anything
794    it doesn't want, is offered to the next one down, etc.  Note the
795    business with curlen:  if an early target says "no, but I have a
796    boundary overlapping this xfer" then we shorten what we offer to
797    the subsequent targets so the early guy will get a chance at the
798    tail before the subsequent ones do. 
799
800    Result is 0 or errno value.  */
801
802 static int
803 target_xfer_memory (memaddr, myaddr, len, write, bfd_section)
804      CORE_ADDR memaddr;
805      char *myaddr;
806      int len;
807      int write;
808      asection *bfd_section;
809 {
810   int curlen;
811   int res;
812   struct target_ops *t;
813   struct target_stack_item *item;
814
815   /* Zero length requests are ok and require no work.  */
816   if (len == 0)
817     return 0;
818
819   target_memory_bfd_section = bfd_section;
820
821   /* to_xfer_memory is not guaranteed to set errno, even when it returns
822      0.  */
823   errno = 0;
824
825   /* The quick case is that the top target does it all.  */
826   res = current_target.to_xfer_memory
827                         (memaddr, myaddr, len, write, &current_target);
828   if (res == len)
829     return 0;
830
831   if (res > 0)
832     goto bump;
833   /* If res <= 0 then we call it again in the loop.  Ah well.  */
834
835   for (; len > 0;)
836     {
837       curlen = len;             /* Want to do it all */
838       for (item = target_stack; item; item = item->next)
839         {
840           t = item->target_ops;
841           if (!t->to_has_memory)
842             continue;
843
844           res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
845           if (res > 0)
846             break;              /* Handled all or part of xfer */
847           if (t->to_has_all_memory)
848             break;
849         }
850
851       if (res <= 0)
852         {
853           /* If this address is for nonexistent memory,
854              read zeros if reading, or do nothing if writing.  Return error. */
855           if (!write)
856             memset (myaddr, 0, len);
857           if (errno == 0)
858             return EIO;
859           else
860             return errno;
861         }
862 bump:
863       memaddr += res;
864       myaddr  += res;
865       len     -= res;
866     }
867   return 0;                     /* We managed to cover it all somehow. */
868 }
869
870
871 /* ARGSUSED */
872 static void
873 target_info (args, from_tty)
874      char *args;
875      int from_tty;
876 {
877   struct target_ops *t;
878   struct target_stack_item *item;
879   int has_all_mem = 0;
880   
881   if (symfile_objfile != NULL)
882     printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
883
884 #ifdef FILES_INFO_HOOK
885   if (FILES_INFO_HOOK ())
886     return;
887 #endif
888
889   for (item = target_stack; item; item = item->next)
890     {
891       t = item->target_ops;
892
893       if (!t->to_has_memory)
894         continue;
895
896       if ((int)(t->to_stratum) <= (int)dummy_stratum)
897         continue;
898       if (has_all_mem)
899         printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
900       printf_unfiltered("%s:\n", t->to_longname);
901       (t->to_files_info)(t);
902       has_all_mem = t->to_has_all_memory;
903     }
904 }
905
906 /* This is to be called by the open routine before it does
907    anything.  */
908
909 void
910 target_preopen (from_tty)
911      int from_tty;
912 {
913   dont_repeat();
914
915   if (target_has_execution)
916     {   
917       if (query ("A program is being debugged already.  Kill it? "))
918         target_kill ();
919       else
920         error ("Program not killed.");
921     }
922
923   /* Calling target_kill may remove the target from the stack.  But if
924      it doesn't (which seems like a win for UDI), remove it now.  */
925
926   if (target_has_execution)
927     pop_target ();
928 }
929
930 /* Detach a target after doing deferred register stores.  */
931
932 void
933 target_detach (args, from_tty)
934      char *args;
935      int from_tty;
936 {
937   /* Handle any optimized stores to the inferior.  */
938 #ifdef DO_DEFERRED_STORES
939   DO_DEFERRED_STORES;
940 #endif
941   (current_target.to_detach) (args, from_tty);
942 }
943
944 void
945 target_link (modname, t_reloc)
946      char *modname;
947      CORE_ADDR *t_reloc;
948 {
949   if (STREQ(current_target.to_shortname, "rombug"))
950     {
951       (current_target.to_lookup_symbol) (modname, t_reloc);
952       if (*t_reloc == 0)
953       error("Unable to link to %s and get relocation in rombug", modname);
954     }
955   else
956     *t_reloc = (CORE_ADDR)-1;
957 }
958
959 /* Look through the list of possible targets for a target that can
960    execute a run or attach command without any other data.  This is
961    used to locate the default process stratum.
962
963    Result is always valid (error() is called for errors).  */
964
965 static struct target_ops *
966 find_default_run_target (do_mesg)
967      char *do_mesg;
968 {
969   struct target_ops **t;
970   struct target_ops *runable = NULL;
971   int count;
972
973   count = 0;
974
975   for (t = target_structs; t < target_structs + target_struct_size;
976        ++t)
977     {
978       if ((*t)->to_can_run && target_can_run(*t))
979         {
980           runable = *t;
981           ++count;
982         }
983     }
984
985   if (count != 1)
986     error ("Don't know how to %s.  Try \"help target\".", do_mesg);
987
988   return runable;
989 }
990
991 void
992 find_default_attach (args, from_tty)
993      char *args;
994      int from_tty;
995 {
996   struct target_ops *t;
997
998   t = find_default_run_target("attach");
999   (t->to_attach) (args, from_tty);
1000   return;
1001 }
1002
1003 void
1004 find_default_create_inferior (exec_file, allargs, env)
1005      char *exec_file;
1006      char *allargs;
1007      char **env;
1008 {
1009   struct target_ops *t;
1010
1011   t = find_default_run_target("run");
1012   (t->to_create_inferior) (exec_file, allargs, env);
1013   return;
1014 }
1015
1016 static int
1017 return_zero ()
1018 {
1019   return 0;
1020 }
1021
1022 struct target_ops *
1023 find_core_target ()
1024 {
1025   struct target_ops **t;
1026   struct target_ops *runable = NULL;
1027   int count;
1028   
1029   count = 0;
1030   
1031   for (t = target_structs; t < target_structs + target_struct_size;
1032        ++t)
1033     {
1034       if ((*t)->to_stratum == core_stratum)
1035         {
1036           runable = *t;
1037           ++count;
1038         }
1039     }
1040   
1041   return(count == 1 ? runable : NULL);
1042 }
1043 \f
1044 /* The inferior process has died.  Long live the inferior!  */
1045
1046 void
1047 generic_mourn_inferior ()
1048 {
1049   extern int show_breakpoint_hit_counts;
1050
1051   inferior_pid = 0;
1052   attach_flag = 0;
1053   breakpoint_init_inferior ();
1054   registers_changed ();
1055
1056 #ifdef CLEAR_DEFERRED_STORES
1057   /* Delete any pending stores to the inferior... */
1058   CLEAR_DEFERRED_STORES;
1059 #endif
1060
1061   reopen_exec_file ();
1062   reinit_frame_cache ();
1063
1064   /* It is confusing to the user for ignore counts to stick around
1065      from previous runs of the inferior.  So clear them.  */
1066   /* However, it is more confusing for the ignore counts to disappear when
1067      using hit counts.  So don't clear them if we're counting hits.  */
1068   if (!show_breakpoint_hit_counts)
1069     breakpoint_clear_ignore_counts ();
1070 }
1071 \f
1072 /* This table must match in order and size the signals in enum target_signal
1073    in target.h.  */
1074 static struct {
1075   char *name;
1076   char *string;
1077   } signals [] =
1078 {
1079   {"0", "Signal 0"},
1080   {"SIGHUP", "Hangup"},
1081   {"SIGINT", "Interrupt"},
1082   {"SIGQUIT", "Quit"},
1083   {"SIGILL", "Illegal instruction"},
1084   {"SIGTRAP", "Trace/breakpoint trap"},
1085   {"SIGABRT", "Aborted"},
1086   {"SIGEMT", "Emulation trap"},
1087   {"SIGFPE", "Arithmetic exception"},
1088   {"SIGKILL", "Killed"},
1089   {"SIGBUS", "Bus error"},
1090   {"SIGSEGV", "Segmentation fault"},
1091   {"SIGSYS", "Bad system call"},
1092   {"SIGPIPE", "Broken pipe"},
1093   {"SIGALRM", "Alarm clock"},
1094   {"SIGTERM", "Terminated"},
1095   {"SIGURG", "Urgent I/O condition"},
1096   {"SIGSTOP", "Stopped (signal)"},
1097   {"SIGTSTP", "Stopped (user)"},
1098   {"SIGCONT", "Continued"},
1099   {"SIGCHLD", "Child status changed"},
1100   {"SIGTTIN", "Stopped (tty input)"},
1101   {"SIGTTOU", "Stopped (tty output)"},
1102   {"SIGIO", "I/O possible"},
1103   {"SIGXCPU", "CPU time limit exceeded"},
1104   {"SIGXFSZ", "File size limit exceeded"},
1105   {"SIGVTALRM", "Virtual timer expired"},
1106   {"SIGPROF", "Profiling timer expired"},
1107   {"SIGWINCH", "Window size changed"},
1108   {"SIGLOST", "Resource lost"},
1109   {"SIGUSR1", "User defined signal 1"},
1110   {"SIGUSR2", "User defined signal 2"},
1111   {"SIGPWR", "Power fail/restart"},
1112   {"SIGPOLL", "Pollable event occurred"},
1113   {"SIGWIND", "SIGWIND"},
1114   {"SIGPHONE", "SIGPHONE"},
1115   {"SIGWAITING", "Process's LWPs are blocked"},
1116   {"SIGLWP", "Signal LWP"},
1117   {"SIGDANGER", "Swap space dangerously low"},
1118   {"SIGGRANT", "Monitor mode granted"},
1119   {"SIGRETRACT", "Need to relinguish monitor mode"},
1120   {"SIGMSG", "Monitor mode data available"},
1121   {"SIGSOUND", "Sound completed"},
1122   {"SIGSAK", "Secure attention"},
1123   {"SIGPRIO", "SIGPRIO"},
1124   {"SIG33", "Real-time event 33"},
1125   {"SIG34", "Real-time event 34"},
1126   {"SIG35", "Real-time event 35"},
1127   {"SIG36", "Real-time event 36"},
1128   {"SIG37", "Real-time event 37"},
1129   {"SIG38", "Real-time event 38"},
1130   {"SIG39", "Real-time event 39"},
1131   {"SIG40", "Real-time event 40"},
1132   {"SIG41", "Real-time event 41"},
1133   {"SIG42", "Real-time event 42"},
1134   {"SIG43", "Real-time event 43"},
1135   {"SIG44", "Real-time event 44"},
1136   {"SIG45", "Real-time event 45"},
1137   {"SIG46", "Real-time event 46"},
1138   {"SIG47", "Real-time event 47"},
1139   {"SIG48", "Real-time event 48"},
1140   {"SIG49", "Real-time event 49"},
1141   {"SIG50", "Real-time event 50"},
1142   {"SIG51", "Real-time event 51"},
1143   {"SIG52", "Real-time event 52"},
1144   {"SIG53", "Real-time event 53"},
1145   {"SIG54", "Real-time event 54"},
1146   {"SIG55", "Real-time event 55"},
1147   {"SIG56", "Real-time event 56"},
1148   {"SIG57", "Real-time event 57"},
1149   {"SIG58", "Real-time event 58"},
1150   {"SIG59", "Real-time event 59"},
1151   {"SIG60", "Real-time event 60"},
1152   {"SIG61", "Real-time event 61"},
1153   {"SIG62", "Real-time event 62"},
1154   {"SIG63", "Real-time event 63"},
1155
1156 #if defined(MACH) || defined(__MACH__)
1157   /* Mach exceptions */
1158   {"EXC_BAD_ACCESS", "Could not access memory"},
1159   {"EXC_BAD_INSTRUCTION", "Illegal instruction/operand"},
1160   {"EXC_ARITHMETIC", "Arithmetic exception"},
1161   {"EXC_EMULATION", "Emulation instruction"},
1162   {"EXC_SOFTWARE", "Software generated exception"},
1163   {"EXC_BREAKPOINT", "Breakpoint"},
1164 #endif
1165   {NULL, "Unknown signal"},
1166   {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1167
1168   /* Last entry, used to check whether the table is the right size.  */
1169   {NULL, "TARGET_SIGNAL_MAGIC"}
1170 };
1171
1172 /* Return the string for a signal.  */
1173 char *
1174 target_signal_to_string (sig)
1175      enum target_signal sig;
1176 {
1177   return signals[sig].string;
1178 }
1179
1180 /* Return the name for a signal.  */
1181 char *
1182 target_signal_to_name (sig)
1183      enum target_signal sig;
1184 {
1185   if (sig == TARGET_SIGNAL_UNKNOWN)
1186     /* I think the code which prints this will always print it along with
1187        the string, so no need to be verbose.  */
1188     return "?";
1189   return signals[sig].name;
1190 }
1191
1192 /* Given a name, return its signal.  */
1193 enum target_signal
1194 target_signal_from_name (name)
1195      char *name;
1196 {
1197   enum target_signal sig;
1198
1199   /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1200      for TARGET_SIGNAL_SIGCHLD.  SIGIOT, on the other hand, is more
1201      questionable; seems like by now people should call it SIGABRT
1202      instead.  */
1203
1204   /* This ugly cast brought to you by the native VAX compiler.  */
1205   for (sig = TARGET_SIGNAL_HUP;
1206        signals[sig].name != NULL;
1207        sig = (enum target_signal)((int)sig + 1))
1208     if (STREQ (name, signals[sig].name))
1209       return sig;
1210   return TARGET_SIGNAL_UNKNOWN;
1211 }
1212 \f
1213 /* The following functions are to help certain targets deal
1214    with the signal/waitstatus stuff.  They could just as well be in
1215    a file called native-utils.c or unixwaitstatus-utils.c or whatever.  */
1216
1217 /* Convert host signal to our signals.  */
1218 enum target_signal
1219 target_signal_from_host (hostsig)
1220      int hostsig;
1221 {
1222   /* A switch statement would make sense but would require special kludges
1223      to deal with the cases where more than one signal has the same number.  */
1224
1225   if (hostsig == 0) return TARGET_SIGNAL_0;
1226
1227 #if defined (SIGHUP)
1228   if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1229 #endif
1230 #if defined (SIGINT)
1231   if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1232 #endif
1233 #if defined (SIGQUIT)
1234   if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1235 #endif
1236 #if defined (SIGILL)
1237   if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1238 #endif
1239 #if defined (SIGTRAP)
1240   if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1241 #endif
1242 #if defined (SIGABRT)
1243   if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1244 #endif
1245 #if defined (SIGEMT)
1246   if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1247 #endif
1248 #if defined (SIGFPE)
1249   if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1250 #endif
1251 #if defined (SIGKILL)
1252   if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1253 #endif
1254 #if defined (SIGBUS)
1255   if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1256 #endif
1257 #if defined (SIGSEGV)
1258   if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1259 #endif
1260 #if defined (SIGSYS)
1261   if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1262 #endif
1263 #if defined (SIGPIPE)
1264   if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1265 #endif
1266 #if defined (SIGALRM)
1267   if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1268 #endif
1269 #if defined (SIGTERM)
1270   if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1271 #endif
1272 #if defined (SIGUSR1)
1273   if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1274 #endif
1275 #if defined (SIGUSR2)
1276   if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1277 #endif
1278 #if defined (SIGCLD)
1279   if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1280 #endif
1281 #if defined (SIGCHLD)
1282   if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1283 #endif
1284 #if defined (SIGPWR)
1285   if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1286 #endif
1287 #if defined (SIGWINCH)
1288   if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1289 #endif
1290 #if defined (SIGURG)
1291   if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1292 #endif
1293 #if defined (SIGIO)
1294   if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1295 #endif
1296 #if defined (SIGPOLL)
1297   if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1298 #endif
1299 #if defined (SIGSTOP)
1300   if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1301 #endif
1302 #if defined (SIGTSTP)
1303   if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1304 #endif
1305 #if defined (SIGCONT)
1306   if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1307 #endif
1308 #if defined (SIGTTIN)
1309   if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1310 #endif
1311 #if defined (SIGTTOU)
1312   if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1313 #endif
1314 #if defined (SIGVTALRM)
1315   if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1316 #endif
1317 #if defined (SIGPROF)
1318   if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1319 #endif
1320 #if defined (SIGXCPU)
1321   if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1322 #endif
1323 #if defined (SIGXFSZ)
1324   if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1325 #endif
1326 #if defined (SIGWIND)
1327   if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1328 #endif
1329 #if defined (SIGPHONE)
1330   if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1331 #endif
1332 #if defined (SIGLOST)
1333   if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1334 #endif
1335 #if defined (SIGWAITING)
1336   if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1337 #endif
1338 #if defined (SIGLWP)
1339   if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1340 #endif
1341 #if defined (SIGDANGER)
1342   if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1343 #endif
1344 #if defined (SIGGRANT)
1345   if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1346 #endif
1347 #if defined (SIGRETRACT)
1348   if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1349 #endif
1350 #if defined (SIGMSG)
1351   if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1352 #endif
1353 #if defined (SIGSOUND)
1354   if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1355 #endif
1356 #if defined (SIGSAK)
1357   if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1358 #endif
1359 #if defined (SIGPRIO)
1360   if (hostsig == SIGPRIO) return TARGET_SIGNAL_PRIO;
1361 #endif
1362
1363   /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1364 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1365   if (hostsig == _NSIG + EXC_BAD_ACCESS) return TARGET_EXC_BAD_ACCESS;
1366 #endif
1367 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1368   if (hostsig == _NSIG + EXC_BAD_INSTRUCTION) return TARGET_EXC_BAD_INSTRUCTION;
1369 #endif
1370 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1371   if (hostsig == _NSIG + EXC_ARITHMETIC) return TARGET_EXC_ARITHMETIC;
1372 #endif
1373 #if defined (EXC_EMULATION) && defined (_NSIG)
1374   if (hostsig == _NSIG + EXC_EMULATION) return TARGET_EXC_EMULATION;
1375 #endif
1376 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1377   if (hostsig == _NSIG + EXC_SOFTWARE) return TARGET_EXC_SOFTWARE;
1378 #endif
1379 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1380   if (hostsig == _NSIG + EXC_BREAKPOINT) return TARGET_EXC_BREAKPOINT;
1381 #endif
1382
1383 #if defined (REALTIME_LO)
1384   if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
1385     return (enum target_signal)
1386       (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1387 #endif
1388   return TARGET_SIGNAL_UNKNOWN;
1389 }
1390
1391 int
1392 target_signal_to_host (oursig)
1393      enum target_signal oursig;
1394 {
1395   switch (oursig)
1396     {
1397     case TARGET_SIGNAL_0: return 0;
1398
1399 #if defined (SIGHUP)
1400     case TARGET_SIGNAL_HUP: return SIGHUP;
1401 #endif
1402 #if defined (SIGINT)
1403     case TARGET_SIGNAL_INT: return SIGINT;
1404 #endif
1405 #if defined (SIGQUIT)
1406     case TARGET_SIGNAL_QUIT: return SIGQUIT;
1407 #endif
1408 #if defined (SIGILL)
1409     case TARGET_SIGNAL_ILL: return SIGILL;
1410 #endif
1411 #if defined (SIGTRAP)
1412     case TARGET_SIGNAL_TRAP: return SIGTRAP;
1413 #endif
1414 #if defined (SIGABRT)
1415     case TARGET_SIGNAL_ABRT: return SIGABRT;
1416 #endif
1417 #if defined (SIGEMT)
1418     case TARGET_SIGNAL_EMT: return SIGEMT;
1419 #endif
1420 #if defined (SIGFPE)
1421     case TARGET_SIGNAL_FPE: return SIGFPE;
1422 #endif
1423 #if defined (SIGKILL)
1424     case TARGET_SIGNAL_KILL: return SIGKILL;
1425 #endif
1426 #if defined (SIGBUS)
1427     case TARGET_SIGNAL_BUS: return SIGBUS;
1428 #endif
1429 #if defined (SIGSEGV)
1430     case TARGET_SIGNAL_SEGV: return SIGSEGV;
1431 #endif
1432 #if defined (SIGSYS)
1433     case TARGET_SIGNAL_SYS: return SIGSYS;
1434 #endif
1435 #if defined (SIGPIPE)
1436     case TARGET_SIGNAL_PIPE: return SIGPIPE;
1437 #endif
1438 #if defined (SIGALRM)
1439     case TARGET_SIGNAL_ALRM: return SIGALRM;
1440 #endif
1441 #if defined (SIGTERM)
1442     case TARGET_SIGNAL_TERM: return SIGTERM;
1443 #endif
1444 #if defined (SIGUSR1)
1445     case TARGET_SIGNAL_USR1: return SIGUSR1;
1446 #endif
1447 #if defined (SIGUSR2)
1448     case TARGET_SIGNAL_USR2: return SIGUSR2;
1449 #endif
1450 #if defined (SIGCHLD) || defined (SIGCLD)
1451     case TARGET_SIGNAL_CHLD: 
1452 #if defined (SIGCHLD)
1453       return SIGCHLD;
1454 #else
1455       return SIGCLD;
1456 #endif
1457 #endif /* SIGCLD or SIGCHLD */
1458 #if defined (SIGPWR)
1459     case TARGET_SIGNAL_PWR: return SIGPWR;
1460 #endif
1461 #if defined (SIGWINCH)
1462     case TARGET_SIGNAL_WINCH: return SIGWINCH;
1463 #endif
1464 #if defined (SIGURG)
1465     case TARGET_SIGNAL_URG: return SIGURG;
1466 #endif
1467 #if defined (SIGIO)
1468     case TARGET_SIGNAL_IO: return SIGIO;
1469 #endif
1470 #if defined (SIGPOLL)
1471     case TARGET_SIGNAL_POLL: return SIGPOLL;
1472 #endif
1473 #if defined (SIGSTOP)
1474     case TARGET_SIGNAL_STOP: return SIGSTOP;
1475 #endif
1476 #if defined (SIGTSTP)
1477     case TARGET_SIGNAL_TSTP: return SIGTSTP;
1478 #endif
1479 #if defined (SIGCONT)
1480     case TARGET_SIGNAL_CONT: return SIGCONT;
1481 #endif
1482 #if defined (SIGTTIN)
1483     case TARGET_SIGNAL_TTIN: return SIGTTIN;
1484 #endif
1485 #if defined (SIGTTOU)
1486     case TARGET_SIGNAL_TTOU: return SIGTTOU;
1487 #endif
1488 #if defined (SIGVTALRM)
1489     case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1490 #endif
1491 #if defined (SIGPROF)
1492     case TARGET_SIGNAL_PROF: return SIGPROF;
1493 #endif
1494 #if defined (SIGXCPU)
1495     case TARGET_SIGNAL_XCPU: return SIGXCPU;
1496 #endif
1497 #if defined (SIGXFSZ)
1498     case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1499 #endif
1500 #if defined (SIGWIND)
1501     case TARGET_SIGNAL_WIND: return SIGWIND;
1502 #endif
1503 #if defined (SIGPHONE)
1504     case TARGET_SIGNAL_PHONE: return SIGPHONE;
1505 #endif
1506 #if defined (SIGLOST)
1507     case TARGET_SIGNAL_LOST: return SIGLOST;
1508 #endif
1509 #if defined (SIGWAITING)
1510     case TARGET_SIGNAL_WAITING: return SIGWAITING;
1511 #endif
1512 #if defined (SIGLWP)
1513     case TARGET_SIGNAL_LWP: return SIGLWP;
1514 #endif
1515 #if defined (SIGDANGER)
1516     case TARGET_SIGNAL_DANGER: return SIGDANGER;
1517 #endif
1518 #if defined (SIGGRANT)
1519     case TARGET_SIGNAL_GRANT: return SIGGRANT;
1520 #endif
1521 #if defined (SIGRETRACT)
1522     case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1523 #endif
1524 #if defined (SIGMSG)
1525     case TARGET_SIGNAL_MSG: return SIGMSG;
1526 #endif
1527 #if defined (SIGSOUND)
1528     case TARGET_SIGNAL_SOUND: return SIGSOUND;
1529 #endif
1530 #if defined (SIGSAK)
1531     case TARGET_SIGNAL_SAK: return SIGSAK;
1532 #endif
1533 #if defined (SIGPRIO)
1534     case TARGET_SIGNAL_PRIO: return SIGPRIO;
1535 #endif
1536
1537       /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1538 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1539     case TARGET_EXC_BAD_ACCESS: return _NSIG + EXC_BAD_ACCESS;
1540 #endif
1541 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1542     case TARGET_EXC_BAD_INSTRUCTION: return _NSIG + EXC_BAD_INSTRUCTION;
1543 #endif
1544 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1545     case TARGET_EXC_ARITHMETIC: return _NSIG + EXC_ARITHMETIC;
1546 #endif
1547 #if defined (EXC_EMULATION) && defined (_NSIG)
1548     case TARGET_EXC_EMULATION: return _NSIG + EXC_EMULATION;
1549 #endif
1550 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1551     case TARGET_EXC_SOFTWARE: return _NSIG + EXC_SOFTWARE;
1552 #endif
1553 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1554     case TARGET_EXC_BREAKPOINT: return _NSIG + EXC_BREAKPOINT;
1555 #endif
1556
1557     default:
1558 #if defined (REALTIME_LO)
1559       if (oursig >= TARGET_SIGNAL_REALTIME_33
1560           && oursig <= TARGET_SIGNAL_REALTIME_63)
1561         {
1562           int retsig =
1563             (int)oursig - (int)TARGET_SIGNAL_REALTIME_33 + REALTIME_LO;
1564           if (retsig < REALTIME_HI)
1565             return retsig;
1566         }
1567 #endif
1568       /* The user might be trying to do "signal SIGSAK" where this system
1569          doesn't have SIGSAK.  */
1570       warning ("Signal %s does not exist on this system.\n",
1571                target_signal_to_name (oursig));
1572       return 0;
1573     }
1574 }
1575
1576 /* Helper function for child_wait and the Lynx derivatives of child_wait.
1577    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1578    translation of that in OURSTATUS.  */
1579 void
1580 store_waitstatus (ourstatus, hoststatus)
1581      struct target_waitstatus *ourstatus;
1582      int hoststatus;
1583 {
1584 #ifdef CHILD_SPECIAL_WAITSTATUS
1585   /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1586      if it wants to deal with hoststatus.  */
1587   if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1588     return;
1589 #endif
1590
1591   if (WIFEXITED (hoststatus))
1592     {
1593       ourstatus->kind = TARGET_WAITKIND_EXITED;
1594       ourstatus->value.integer = WEXITSTATUS (hoststatus);
1595     }
1596   else if (!WIFSTOPPED (hoststatus))
1597     {
1598       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1599       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1600     }
1601   else
1602     {
1603       ourstatus->kind = TARGET_WAITKIND_STOPPED;
1604       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1605     }
1606 }
1607 \f
1608 /* In some circumstances we allow a command to specify a numeric
1609    signal.  The idea is to keep these circumstances limited so that
1610    users (and scripts) develop portable habits.  For comparison,
1611    POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
1612    numeric signal at all is obscelescent.  We are slightly more
1613    lenient and allow 1-15 which should match host signal numbers on
1614    most systems.  Use of symbolic signal names is strongly encouraged.  */
1615
1616 enum target_signal
1617 target_signal_from_command (num)
1618      int num;
1619 {
1620   if (num >= 1 && num <= 15)
1621     return (enum target_signal)num;
1622   error ("Only signals 1-15 are valid as numeric signals.\n\
1623 Use \"info signals\" for a list of symbolic signals.");
1624 }
1625 \f
1626 /* Returns zero to leave the inferior alone, one to interrupt it.  */
1627 int (*target_activity_function) PARAMS ((void));
1628 int target_activity_fd;
1629 \f
1630 /* Convert a normal process ID to a string.  Returns the string in a static
1631    buffer.  */
1632
1633 char *
1634 normal_pid_to_str (pid)
1635      int pid;
1636 {
1637   static char buf[30];
1638
1639   if (STREQ (current_target.to_shortname, "remote"))
1640     sprintf (buf, "thread %d", pid);
1641   else
1642     sprintf (buf, "process %d", pid);
1643
1644   return buf;
1645 }
1646 \f
1647 #ifdef MAINTENANCE_CMDS
1648 static struct target_ops debug_target;
1649
1650 static void
1651 debug_to_open (args, from_tty)
1652      char *args;
1653      int from_tty;
1654 {
1655   debug_target.to_open (args, from_tty);
1656
1657   fprintf_unfiltered (stderr, "target_open (%s, %d)\n", args, from_tty);
1658 }
1659
1660 static void
1661 debug_to_close (quitting)
1662      int quitting;
1663 {
1664   debug_target.to_close (quitting);
1665
1666   fprintf_unfiltered (stderr, "target_close (%d)\n", quitting);
1667 }
1668
1669 static void
1670 debug_to_attach (args, from_tty)
1671      char *args;
1672      int from_tty;
1673 {
1674   debug_target.to_attach (args, from_tty);
1675
1676   fprintf_unfiltered (stderr, "target_attach (%s, %d)\n", args, from_tty);
1677 }
1678
1679 static void
1680 debug_to_detach (args, from_tty)
1681      char *args;
1682      int from_tty;
1683 {
1684   debug_target.to_detach (args, from_tty);
1685
1686   fprintf_unfiltered (stderr, "target_detach (%s, %d)\n", args, from_tty);
1687 }
1688
1689 static void
1690 debug_to_resume (pid, step, siggnal)
1691      int pid;
1692      int step;
1693      enum target_signal siggnal;
1694 {
1695   debug_target.to_resume (pid, step, siggnal);
1696
1697   fprintf_unfiltered (stderr, "target_resume (%d, %s, %s)\n", pid,
1698                       step ? "step" : "continue",
1699                       target_signal_to_name (siggnal));
1700 }
1701
1702 static int
1703 debug_to_wait (pid, status)
1704      int pid;
1705      struct target_waitstatus *status;
1706 {
1707   int retval;
1708
1709   retval = debug_target.to_wait (pid, status);
1710
1711   fprintf_unfiltered (stderr, "target_wait (%d, status) = %d,   ", pid, retval);
1712   fprintf_unfiltered (stderr, "status->kind = ");
1713   switch (status->kind)
1714     {
1715     case TARGET_WAITKIND_EXITED:
1716       fprintf_unfiltered (stderr, "exited, status = %d\n", status->value.integer);
1717       break;
1718     case TARGET_WAITKIND_STOPPED:
1719       fprintf_unfiltered (stderr, "stopped, signal = %s\n",
1720                           target_signal_to_name (status->value.sig));
1721       break;
1722     case TARGET_WAITKIND_SIGNALLED:
1723       fprintf_unfiltered (stderr, "signalled, signal = %s\n",
1724                           target_signal_to_name (status->value.sig));
1725       break;
1726     case TARGET_WAITKIND_LOADED:
1727       fprintf_unfiltered (stderr, "loaded\n");
1728       break;
1729     case TARGET_WAITKIND_SPURIOUS:
1730       fprintf_unfiltered (stderr, "spurious\n");
1731       break;
1732     default:
1733       fprintf_unfiltered (stderr, "unknown???\n");
1734       break;
1735     }
1736
1737   return retval;
1738 }
1739
1740 static void
1741 debug_to_fetch_registers (regno)
1742      int regno;
1743 {
1744   debug_target.to_fetch_registers (regno);
1745
1746   fprintf_unfiltered (stderr, "target_fetch_registers (%s)",
1747                       regno != -1 ? reg_names[regno] : "-1");
1748   if (regno != -1)
1749     fprintf_unfiltered (stderr, " = 0x%x %d",
1750                         (unsigned long) read_register (regno),
1751                         read_register (regno));
1752   fprintf_unfiltered (stderr, "\n");
1753 }
1754
1755 static void
1756 debug_to_store_registers (regno)
1757      int regno;
1758 {
1759   debug_target.to_store_registers (regno);
1760
1761   if (regno >= 0 && regno < NUM_REGS)
1762     fprintf_unfiltered (stderr, "target_store_registers (%s) = 0x%x %d\n",
1763                         reg_names[regno],
1764                         (unsigned long) read_register (regno),
1765                         (unsigned long) read_register (regno));
1766   else
1767     fprintf_unfiltered (stderr, "target_store_registers (%d)\n", regno);
1768 }
1769
1770 static void
1771 debug_to_prepare_to_store ()
1772 {
1773   debug_target.to_prepare_to_store ();
1774
1775   fprintf_unfiltered (stderr, "target_prepare_to_store ()\n");
1776 }
1777
1778 static int
1779 debug_to_xfer_memory (memaddr, myaddr, len, write, target)
1780      CORE_ADDR memaddr;
1781      char *myaddr;
1782      int len;
1783      int write;
1784      struct target_ops *target;
1785 {
1786   int retval;
1787
1788   retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write, target);
1789
1790   fprintf_unfiltered (stderr,
1791                       "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
1792                       (unsigned int) memaddr, /* possable truncate long long */
1793                       len, write ? "write" : "read", retval);
1794
1795   
1796
1797   if (retval > 0)
1798     {
1799       int i;
1800
1801       fputs_unfiltered (", bytes =", gdb_stderr);
1802       for (i = 0; i < retval; i++)
1803         {
1804           if ((((long) &(myaddr[i])) & 0xf) == 0)
1805             fprintf_unfiltered (stderr, "\n");
1806           fprintf_unfiltered (stderr, " %02x", myaddr[i] & 0xff);
1807         }
1808     }
1809
1810   fputc_unfiltered ('\n', gdb_stderr);
1811
1812   return retval;
1813 }
1814
1815 static void
1816 debug_to_files_info (target)
1817      struct target_ops *target;
1818 {
1819   debug_target.to_files_info (target);
1820
1821   fprintf_unfiltered (stderr, "target_files_info (xxx)\n");
1822 }
1823
1824 static int
1825 debug_to_insert_breakpoint (addr, save)
1826      CORE_ADDR addr;
1827      char *save;
1828 {
1829   int retval;
1830
1831   retval = debug_target.to_insert_breakpoint (addr, save);
1832
1833   fprintf_unfiltered (stderr, "target_insert_breakpoint (0x%x, xxx) = %d\n",
1834                       (unsigned long) addr, retval);
1835   return retval;
1836 }
1837
1838 static int
1839 debug_to_remove_breakpoint (addr, save)
1840      CORE_ADDR addr;
1841      char *save;
1842 {
1843   int retval;
1844
1845   retval = debug_target.to_remove_breakpoint (addr, save);
1846
1847   fprintf_unfiltered (stderr, "target_remove_breakpoint (0x%x, xxx) = %d\n",
1848                       (unsigned long)addr, retval);
1849   return retval;
1850 }
1851
1852 static void
1853 debug_to_terminal_init ()
1854 {
1855   debug_target.to_terminal_init ();
1856
1857   fprintf_unfiltered (stderr, "target_terminal_init ()\n");
1858 }
1859
1860 static void
1861 debug_to_terminal_inferior ()
1862 {
1863   debug_target.to_terminal_inferior ();
1864
1865   fprintf_unfiltered (stderr, "target_terminal_inferior ()\n");
1866 }
1867
1868 static void
1869 debug_to_terminal_ours_for_output ()
1870 {
1871   debug_target.to_terminal_ours_for_output ();
1872
1873   fprintf_unfiltered (stderr, "target_terminal_ours_for_output ()\n");
1874 }
1875
1876 static void
1877 debug_to_terminal_ours ()
1878 {
1879   debug_target.to_terminal_ours ();
1880
1881   fprintf_unfiltered (stderr, "target_terminal_ours ()\n");
1882 }
1883
1884 static void
1885 debug_to_terminal_info (arg, from_tty)
1886      char *arg;
1887      int from_tty;
1888 {
1889   debug_target.to_terminal_info (arg, from_tty);
1890
1891   fprintf_unfiltered (stderr, "target_terminal_info (%s, %d)\n", arg,
1892                       from_tty);
1893 }
1894
1895 static void
1896 debug_to_kill ()
1897 {
1898   debug_target.to_kill ();
1899
1900   fprintf_unfiltered (stderr, "target_kill ()\n");
1901 }
1902
1903 static void
1904 debug_to_load (args, from_tty)
1905      char *args;
1906      int from_tty;
1907 {
1908   debug_target.to_load (args, from_tty);
1909
1910   fprintf_unfiltered (stderr, "target_load (%s, %d)\n", args, from_tty);
1911 }
1912
1913 static int
1914 debug_to_lookup_symbol (name, addrp)
1915      char *name;
1916      CORE_ADDR *addrp;
1917 {
1918   int retval;
1919
1920   retval = debug_target.to_lookup_symbol (name, addrp);
1921
1922   fprintf_unfiltered (stderr, "target_lookup_symbol (%s, xxx)\n", name);
1923
1924   return retval;
1925 }
1926
1927 static void
1928 debug_to_create_inferior (exec_file, args, env)
1929      char *exec_file;
1930      char *args;
1931      char **env;
1932 {
1933   debug_target.to_create_inferior (exec_file, args, env);
1934
1935   fprintf_unfiltered (stderr, "target_create_inferior (%s, %s, xxx)\n",
1936                       exec_file, args);
1937 }
1938
1939 static void
1940 debug_to_mourn_inferior ()
1941 {
1942   debug_target.to_mourn_inferior ();
1943
1944   fprintf_unfiltered (stderr, "target_mourn_inferior ()\n");
1945 }
1946
1947 static int
1948 debug_to_can_run ()
1949 {
1950   int retval;
1951
1952   retval = debug_target.to_can_run ();
1953
1954   fprintf_unfiltered (stderr, "target_can_run () = %d\n", retval);
1955
1956   return retval;
1957 }
1958
1959 static void
1960 debug_to_notice_signals (pid)
1961      int pid;
1962 {
1963   debug_target.to_notice_signals (pid);
1964
1965   fprintf_unfiltered (stderr, "target_notice_signals (%d)\n", pid);
1966 }
1967
1968 static int
1969 debug_to_thread_alive (pid)
1970      int pid;
1971 {
1972   int retval;
1973
1974   retval = debug_target.to_thread_alive (pid);
1975
1976   fprintf_unfiltered (stderr, "target_thread_alive (%d) = %d\n", pid, retval);
1977
1978   return retval;
1979 }
1980
1981 static void
1982 debug_to_stop ()
1983 {
1984   debug_target.to_stop ();
1985
1986   fprintf_unfiltered (stderr, "target_stop ()\n");
1987 }
1988
1989 static void
1990 setup_target_debug ()
1991 {
1992   memcpy (&debug_target, &current_target, sizeof debug_target);
1993
1994   current_target.to_open = debug_to_open;
1995   current_target.to_close = debug_to_close;
1996   current_target.to_attach = debug_to_attach;
1997   current_target.to_detach = debug_to_detach;
1998   current_target.to_resume = debug_to_resume;
1999   current_target.to_wait = debug_to_wait;
2000   current_target.to_fetch_registers = debug_to_fetch_registers;
2001   current_target.to_store_registers = debug_to_store_registers;
2002   current_target.to_prepare_to_store = debug_to_prepare_to_store;
2003   current_target.to_xfer_memory = debug_to_xfer_memory;
2004   current_target.to_files_info = debug_to_files_info;
2005   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2006   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2007   current_target.to_terminal_init = debug_to_terminal_init;
2008   current_target.to_terminal_inferior = debug_to_terminal_inferior;
2009   current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2010   current_target.to_terminal_ours = debug_to_terminal_ours;
2011   current_target.to_terminal_info = debug_to_terminal_info;
2012   current_target.to_kill = debug_to_kill;
2013   current_target.to_load = debug_to_load;
2014   current_target.to_lookup_symbol = debug_to_lookup_symbol;
2015   current_target.to_create_inferior = debug_to_create_inferior;
2016   current_target.to_mourn_inferior = debug_to_mourn_inferior;
2017   current_target.to_can_run = debug_to_can_run;
2018   current_target.to_notice_signals = debug_to_notice_signals;
2019   current_target.to_thread_alive = debug_to_thread_alive;
2020   current_target.to_stop = debug_to_stop;
2021 }
2022 #endif /* MAINTENANCE_CMDS */
2023 \f
2024 static char targ_desc[] = 
2025     "Names of targets and files being debugged.\n\
2026 Shows the entire stack of targets currently in use (including the exec-file,\n\
2027 core-file, and process, if any), as well as the symbol file name.";
2028
2029 void
2030 initialize_targets ()
2031 {
2032   push_target (&dummy_target);
2033
2034   add_info ("target", target_info, targ_desc);
2035   add_info ("files", target_info, targ_desc);
2036
2037 #ifdef MAINTENANCE_CMDS
2038   add_show_from_set (
2039      add_set_cmd ("targetdebug", class_maintenance, var_zinteger,
2040                   (char *)&targetdebug,
2041                  "Set target debugging.\n\
2042 When non-zero, target debugging is enabled.", &setlist),
2043                      &showlist);
2044 #endif
2045
2046   if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
2047     abort ();
2048 }