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