xref: /openbsd-src/gnu/usr.bin/binutils/gdb/target.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
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   {"SIGLOST", "Resource lost"},
1068   {"SIGUSR1", "User defined signal 1"},
1069   {"SIGUSR2", "User defined signal 2"},
1070   {"SIGPWR", "Power fail/restart"},
1071   {"SIGPOLL", "Pollable event occurred"},
1072   {"SIGWIND", "SIGWIND"},
1073   {"SIGPHONE", "SIGPHONE"},
1074   {"SIGWAITING", "Process's LWPs are blocked"},
1075   {"SIGLWP", "Signal LWP"},
1076   {"SIGDANGER", "Swap space dangerously low"},
1077   {"SIGGRANT", "Monitor mode granted"},
1078   {"SIGRETRACT", "Need to relinguish monitor mode"},
1079   {"SIGMSG", "Monitor mode data available"},
1080   {"SIGSOUND", "Sound completed"},
1081   {"SIGSAK", "Secure attention"},
1082   {"SIGPRIO", "SIGPRIO"},
1083   {"SIG33", "Real-time event 33"},
1084   {"SIG34", "Real-time event 34"},
1085   {"SIG35", "Real-time event 35"},
1086   {"SIG36", "Real-time event 36"},
1087   {"SIG37", "Real-time event 37"},
1088   {"SIG38", "Real-time event 38"},
1089   {"SIG39", "Real-time event 39"},
1090   {"SIG40", "Real-time event 40"},
1091   {"SIG41", "Real-time event 41"},
1092   {"SIG42", "Real-time event 42"},
1093   {"SIG43", "Real-time event 43"},
1094   {"SIG44", "Real-time event 44"},
1095   {"SIG45", "Real-time event 45"},
1096   {"SIG46", "Real-time event 46"},
1097   {"SIG47", "Real-time event 47"},
1098   {"SIG48", "Real-time event 48"},
1099   {"SIG49", "Real-time event 49"},
1100   {"SIG50", "Real-time event 50"},
1101   {"SIG51", "Real-time event 51"},
1102   {"SIG52", "Real-time event 52"},
1103   {"SIG53", "Real-time event 53"},
1104   {"SIG54", "Real-time event 54"},
1105   {"SIG55", "Real-time event 55"},
1106   {"SIG56", "Real-time event 56"},
1107   {"SIG57", "Real-time event 57"},
1108   {"SIG58", "Real-time event 58"},
1109   {"SIG59", "Real-time event 59"},
1110   {"SIG60", "Real-time event 60"},
1111   {"SIG61", "Real-time event 61"},
1112   {"SIG62", "Real-time event 62"},
1113   {"SIG63", "Real-time event 63"},
1114 
1115   /* Mach exceptions */
1116   {"EXC_BAD_ACCESS", "Could not access memory"},
1117   {"EXC_BAD_INSTRUCTION", "Illegal instruction/operand"},
1118   {"EXC_ARITHMETIC", "Arithmetic exception"},
1119   {"EXC_EMULATION", "Emulation instruction"},
1120   {"EXC_SOFTWARE", "Software generated exception"},
1121   {"EXC_BREAKPOINT", "Breakpoint"},
1122 
1123   {NULL, "Unknown signal"},
1124   {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1125 
1126   /* Last entry, used to check whether the table is the right size.  */
1127   {NULL, "TARGET_SIGNAL_MAGIC"}
1128 };
1129 
1130 /* Return the string for a signal.  */
1131 char *
1132 target_signal_to_string (sig)
1133      enum target_signal sig;
1134 {
1135   return signals[sig].string;
1136 }
1137 
1138 /* Return the name for a signal.  */
1139 char *
1140 target_signal_to_name (sig)
1141      enum target_signal sig;
1142 {
1143   if (sig == TARGET_SIGNAL_UNKNOWN)
1144     /* I think the code which prints this will always print it along with
1145        the string, so no need to be verbose.  */
1146     return "?";
1147   return signals[sig].name;
1148 }
1149 
1150 /* Given a name, return its signal.  */
1151 enum target_signal
1152 target_signal_from_name (name)
1153      char *name;
1154 {
1155   enum target_signal sig;
1156 
1157   /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1158      for TARGET_SIGNAL_SIGCHLD.  SIGIOT, on the other hand, is more
1159      questionable; seems like by now people should call it SIGABRT
1160      instead.  */
1161 
1162   /* This ugly cast brought to you by the native VAX compiler.  */
1163   for (sig = TARGET_SIGNAL_HUP;
1164        signals[sig].name != NULL;
1165        sig = (enum target_signal)((int)sig + 1))
1166     if (STREQ (name, signals[sig].name))
1167       return sig;
1168   return TARGET_SIGNAL_UNKNOWN;
1169 }
1170 
1171 /* The following functions are to help certain targets deal
1172    with the signal/waitstatus stuff.  They could just as well be in
1173    a file called native-utils.c or unixwaitstatus-utils.c or whatever.  */
1174 
1175 /* Convert host signal to our signals.  */
1176 enum target_signal
1177 target_signal_from_host (hostsig)
1178      int hostsig;
1179 {
1180   /* A switch statement would make sense but would require special kludges
1181      to deal with the cases where more than one signal has the same number.  */
1182 
1183   if (hostsig == 0) return TARGET_SIGNAL_0;
1184 
1185 #if defined (SIGHUP)
1186   if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1187 #endif
1188 #if defined (SIGINT)
1189   if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1190 #endif
1191 #if defined (SIGQUIT)
1192   if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1193 #endif
1194 #if defined (SIGILL)
1195   if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1196 #endif
1197 #if defined (SIGTRAP)
1198   if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1199 #endif
1200 #if defined (SIGABRT)
1201   if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1202 #endif
1203 #if defined (SIGEMT)
1204   if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1205 #endif
1206 #if defined (SIGFPE)
1207   if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1208 #endif
1209 #if defined (SIGKILL)
1210   if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1211 #endif
1212 #if defined (SIGBUS)
1213   if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1214 #endif
1215 #if defined (SIGSEGV)
1216   if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1217 #endif
1218 #if defined (SIGSYS)
1219   if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1220 #endif
1221 #if defined (SIGPIPE)
1222   if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1223 #endif
1224 #if defined (SIGALRM)
1225   if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1226 #endif
1227 #if defined (SIGTERM)
1228   if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1229 #endif
1230 #if defined (SIGUSR1)
1231   if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1232 #endif
1233 #if defined (SIGUSR2)
1234   if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1235 #endif
1236 #if defined (SIGCLD)
1237   if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1238 #endif
1239 #if defined (SIGCHLD)
1240   if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1241 #endif
1242 #if defined (SIGPWR)
1243   if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1244 #endif
1245 #if defined (SIGWINCH)
1246   if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1247 #endif
1248 #if defined (SIGURG)
1249   if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1250 #endif
1251 #if defined (SIGIO)
1252   if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1253 #endif
1254 #if defined (SIGPOLL)
1255   if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1256 #endif
1257 #if defined (SIGSTOP)
1258   if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1259 #endif
1260 #if defined (SIGTSTP)
1261   if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1262 #endif
1263 #if defined (SIGCONT)
1264   if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1265 #endif
1266 #if defined (SIGTTIN)
1267   if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1268 #endif
1269 #if defined (SIGTTOU)
1270   if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1271 #endif
1272 #if defined (SIGVTALRM)
1273   if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1274 #endif
1275 #if defined (SIGPROF)
1276   if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1277 #endif
1278 #if defined (SIGXCPU)
1279   if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1280 #endif
1281 #if defined (SIGXFSZ)
1282   if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1283 #endif
1284 #if defined (SIGWIND)
1285   if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1286 #endif
1287 #if defined (SIGPHONE)
1288   if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1289 #endif
1290 #if defined (SIGLOST)
1291   if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1292 #endif
1293 #if defined (SIGWAITING)
1294   if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1295 #endif
1296 #if defined (SIGLWP)
1297   if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1298 #endif
1299 #if defined (SIGDANGER)
1300   if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1301 #endif
1302 #if defined (SIGGRANT)
1303   if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1304 #endif
1305 #if defined (SIGRETRACT)
1306   if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1307 #endif
1308 #if defined (SIGMSG)
1309   if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1310 #endif
1311 #if defined (SIGSOUND)
1312   if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1313 #endif
1314 #if defined (SIGSAK)
1315   if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1316 #endif
1317 #if defined (SIGPRIO)
1318   if (hostsig == SIGPRIO) return TARGET_SIGNAL_PRIO;
1319 #endif
1320 
1321   /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1322 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1323   if (hostsig == _NSIG + EXC_BAD_ACCESS) return TARGET_EXC_BAD_ACCESS;
1324 #endif
1325 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1326   if (hostsig == _NSIG + EXC_BAD_INSTRUCTION) return TARGET_EXC_BAD_INSTRUCTION;
1327 #endif
1328 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1329   if (hostsig == _NSIG + EXC_ARITHMETIC) return TARGET_EXC_ARITHMETIC;
1330 #endif
1331 #if defined (EXC_EMULATION) && defined (_NSIG)
1332   if (hostsig == _NSIG + EXC_EMULATION) return TARGET_EXC_EMULATION;
1333 #endif
1334 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1335   if (hostsig == _NSIG + EXC_SOFTWARE) return TARGET_EXC_SOFTWARE;
1336 #endif
1337 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1338   if (hostsig == _NSIG + EXC_BREAKPOINT) return TARGET_EXC_BREAKPOINT;
1339 #endif
1340 
1341 #if defined (REALTIME_LO)
1342   if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
1343     return (enum target_signal)
1344       (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1345 #endif
1346   return TARGET_SIGNAL_UNKNOWN;
1347 }
1348 
1349 int
1350 target_signal_to_host (oursig)
1351      enum target_signal oursig;
1352 {
1353   switch (oursig)
1354     {
1355     case TARGET_SIGNAL_0: return 0;
1356 
1357 #if defined (SIGHUP)
1358     case TARGET_SIGNAL_HUP: return SIGHUP;
1359 #endif
1360 #if defined (SIGINT)
1361     case TARGET_SIGNAL_INT: return SIGINT;
1362 #endif
1363 #if defined (SIGQUIT)
1364     case TARGET_SIGNAL_QUIT: return SIGQUIT;
1365 #endif
1366 #if defined (SIGILL)
1367     case TARGET_SIGNAL_ILL: return SIGILL;
1368 #endif
1369 #if defined (SIGTRAP)
1370     case TARGET_SIGNAL_TRAP: return SIGTRAP;
1371 #endif
1372 #if defined (SIGABRT)
1373     case TARGET_SIGNAL_ABRT: return SIGABRT;
1374 #endif
1375 #if defined (SIGEMT)
1376     case TARGET_SIGNAL_EMT: return SIGEMT;
1377 #endif
1378 #if defined (SIGFPE)
1379     case TARGET_SIGNAL_FPE: return SIGFPE;
1380 #endif
1381 #if defined (SIGKILL)
1382     case TARGET_SIGNAL_KILL: return SIGKILL;
1383 #endif
1384 #if defined (SIGBUS)
1385     case TARGET_SIGNAL_BUS: return SIGBUS;
1386 #endif
1387 #if defined (SIGSEGV)
1388     case TARGET_SIGNAL_SEGV: return SIGSEGV;
1389 #endif
1390 #if defined (SIGSYS)
1391     case TARGET_SIGNAL_SYS: return SIGSYS;
1392 #endif
1393 #if defined (SIGPIPE)
1394     case TARGET_SIGNAL_PIPE: return SIGPIPE;
1395 #endif
1396 #if defined (SIGALRM)
1397     case TARGET_SIGNAL_ALRM: return SIGALRM;
1398 #endif
1399 #if defined (SIGTERM)
1400     case TARGET_SIGNAL_TERM: return SIGTERM;
1401 #endif
1402 #if defined (SIGUSR1)
1403     case TARGET_SIGNAL_USR1: return SIGUSR1;
1404 #endif
1405 #if defined (SIGUSR2)
1406     case TARGET_SIGNAL_USR2: return SIGUSR2;
1407 #endif
1408 #if defined (SIGCHLD) || defined (SIGCLD)
1409     case TARGET_SIGNAL_CHLD:
1410 #if defined (SIGCHLD)
1411       return SIGCHLD;
1412 #else
1413       return SIGCLD;
1414 #endif
1415 #endif /* SIGCLD or SIGCHLD */
1416 #if defined (SIGPWR)
1417     case TARGET_SIGNAL_PWR: return SIGPWR;
1418 #endif
1419 #if defined (SIGWINCH)
1420     case TARGET_SIGNAL_WINCH: return SIGWINCH;
1421 #endif
1422 #if defined (SIGURG)
1423     case TARGET_SIGNAL_URG: return SIGURG;
1424 #endif
1425 #if defined (SIGIO)
1426     case TARGET_SIGNAL_IO: return SIGIO;
1427 #endif
1428 #if defined (SIGPOLL)
1429     case TARGET_SIGNAL_POLL: return SIGPOLL;
1430 #endif
1431 #if defined (SIGSTOP)
1432     case TARGET_SIGNAL_STOP: return SIGSTOP;
1433 #endif
1434 #if defined (SIGTSTP)
1435     case TARGET_SIGNAL_TSTP: return SIGTSTP;
1436 #endif
1437 #if defined (SIGCONT)
1438     case TARGET_SIGNAL_CONT: return SIGCONT;
1439 #endif
1440 #if defined (SIGTTIN)
1441     case TARGET_SIGNAL_TTIN: return SIGTTIN;
1442 #endif
1443 #if defined (SIGTTOU)
1444     case TARGET_SIGNAL_TTOU: return SIGTTOU;
1445 #endif
1446 #if defined (SIGVTALRM)
1447     case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1448 #endif
1449 #if defined (SIGPROF)
1450     case TARGET_SIGNAL_PROF: return SIGPROF;
1451 #endif
1452 #if defined (SIGXCPU)
1453     case TARGET_SIGNAL_XCPU: return SIGXCPU;
1454 #endif
1455 #if defined (SIGXFSZ)
1456     case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1457 #endif
1458 #if defined (SIGWIND)
1459     case TARGET_SIGNAL_WIND: return SIGWIND;
1460 #endif
1461 #if defined (SIGPHONE)
1462     case TARGET_SIGNAL_PHONE: return SIGPHONE;
1463 #endif
1464 #if defined (SIGLOST)
1465     case TARGET_SIGNAL_LOST: return SIGLOST;
1466 #endif
1467 #if defined (SIGWAITING)
1468     case TARGET_SIGNAL_WAITING: return SIGWAITING;
1469 #endif
1470 #if defined (SIGLWP)
1471     case TARGET_SIGNAL_LWP: return SIGLWP;
1472 #endif
1473 #if defined (SIGDANGER)
1474     case TARGET_SIGNAL_DANGER: return SIGDANGER;
1475 #endif
1476 #if defined (SIGGRANT)
1477     case TARGET_SIGNAL_GRANT: return SIGGRANT;
1478 #endif
1479 #if defined (SIGRETRACT)
1480     case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1481 #endif
1482 #if defined (SIGMSG)
1483     case TARGET_SIGNAL_MSG: return SIGMSG;
1484 #endif
1485 #if defined (SIGSOUND)
1486     case TARGET_SIGNAL_SOUND: return SIGSOUND;
1487 #endif
1488 #if defined (SIGSAK)
1489     case TARGET_SIGNAL_SAK: return SIGSAK;
1490 #endif
1491 #if defined (SIGPRIO)
1492     case TARGET_SIGNAL_PRIO: return SIGPRIO;
1493 #endif
1494 
1495       /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
1496 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
1497     case TARGET_EXC_BAD_ACCESS: return _NSIG + EXC_BAD_ACCESS;
1498 #endif
1499 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
1500     case TARGET_EXC_BAD_INSTRUCTION: return _NSIG + EXC_BAD_INSTRUCTION;
1501 #endif
1502 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
1503     case TARGET_EXC_ARITHMETIC: return _NSIG + EXC_ARITHMETIC;
1504 #endif
1505 #if defined (EXC_EMULATION) && defined (_NSIG)
1506     case TARGET_EXC_EMULATION: return _NSIG + EXC_EMULATION;
1507 #endif
1508 #if defined (EXC_SOFTWARE) && defined (_NSIG)
1509     case TARGET_EXC_SOFTWARE: return _NSIG + EXC_SOFTWARE;
1510 #endif
1511 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
1512     case TARGET_EXC_BREAKPOINT: return _NSIG + EXC_BREAKPOINT;
1513 #endif
1514 
1515     default:
1516 #if defined (REALTIME_LO)
1517       if (oursig >= TARGET_SIGNAL_REALTIME_33
1518 	  && oursig <= TARGET_SIGNAL_REALTIME_63)
1519 	{
1520 	  int retsig =
1521 	    (int)oursig - (int)TARGET_SIGNAL_REALTIME_33 + REALTIME_LO;
1522 	  if (retsig < REALTIME_HI)
1523 	    return retsig;
1524 	}
1525 #endif
1526       /* The user might be trying to do "signal SIGSAK" where this system
1527 	 doesn't have SIGSAK.  */
1528       warning ("Signal %s does not exist on this system.\n",
1529 	       target_signal_to_name (oursig));
1530       return 0;
1531     }
1532 }
1533 
1534 /* Helper function for child_wait and the Lynx derivatives of child_wait.
1535    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1536    translation of that in OURSTATUS.  */
1537 void
1538 store_waitstatus (ourstatus, hoststatus)
1539      struct target_waitstatus *ourstatus;
1540      int hoststatus;
1541 {
1542 #ifdef CHILD_SPECIAL_WAITSTATUS
1543   /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1544      if it wants to deal with hoststatus.  */
1545   if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1546     return;
1547 #endif
1548 
1549   if (WIFEXITED (hoststatus))
1550     {
1551       ourstatus->kind = TARGET_WAITKIND_EXITED;
1552       ourstatus->value.integer = WEXITSTATUS (hoststatus);
1553     }
1554   else if (!WIFSTOPPED (hoststatus))
1555     {
1556       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1557       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1558     }
1559   else
1560     {
1561       ourstatus->kind = TARGET_WAITKIND_STOPPED;
1562       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1563     }
1564 }
1565 
1566 /* In some circumstances we allow a command to specify a numeric
1567    signal.  The idea is to keep these circumstances limited so that
1568    users (and scripts) develop portable habits.  For comparison,
1569    POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
1570    numeric signal at all is obscelescent.  We are slightly more
1571    lenient and allow 1-15 which should match host signal numbers on
1572    most systems.  Use of symbolic signal names is strongly encouraged.  */
1573 
1574 enum target_signal
1575 target_signal_from_command (num)
1576      int num;
1577 {
1578   if (num >= 1 && num <= 15)
1579     return (enum target_signal)num;
1580   error ("Only signals 1-15 are valid as numeric signals.\n\
1581 Use \"info signals\" for a list of symbolic signals.");
1582 }
1583 
1584 /* Returns zero to leave the inferior alone, one to interrupt it.  */
1585 int (*target_activity_function) PARAMS ((void));
1586 int target_activity_fd;
1587 
1588 /* Convert a normal process ID to a string.  Returns the string in a static
1589    buffer.  */
1590 
1591 char *
1592 normal_pid_to_str (pid)
1593      int pid;
1594 {
1595   static char buf[30];
1596 
1597   if (STREQ (current_target.to_shortname, "remote"))
1598     sprintf (buf, "thread %d", pid);
1599   else
1600     sprintf (buf, "process %d", pid);
1601 
1602   return buf;
1603 }
1604 
1605 #ifdef MAINTENANCE_CMDS
1606 static struct target_ops debug_target;
1607 
1608 static void
1609 debug_to_open (args, from_tty)
1610      char *args;
1611      int from_tty;
1612 {
1613   debug_target.to_open (args, from_tty);
1614 
1615   fprintf_unfiltered (stderr, "target_open (%s, %d)\n", args, from_tty);
1616 }
1617 
1618 static void
1619 debug_to_close (quitting)
1620      int quitting;
1621 {
1622   debug_target.to_close (quitting);
1623 
1624   fprintf_unfiltered (stderr, "target_close (%d)\n", quitting);
1625 }
1626 
1627 static void
1628 debug_to_attach (args, from_tty)
1629      char *args;
1630      int from_tty;
1631 {
1632   debug_target.to_attach (args, from_tty);
1633 
1634   fprintf_unfiltered (stderr, "target_attach (%s, %d)\n", args, from_tty);
1635 }
1636 
1637 static void
1638 debug_to_detach (args, from_tty)
1639      char *args;
1640      int from_tty;
1641 {
1642   debug_target.to_detach (args, from_tty);
1643 
1644   fprintf_unfiltered (stderr, "target_detach (%s, %d)\n", args, from_tty);
1645 }
1646 
1647 static void
1648 debug_to_resume (pid, step, siggnal)
1649      int pid;
1650      int step;
1651      enum target_signal siggnal;
1652 {
1653   debug_target.to_resume (pid, step, siggnal);
1654 
1655   fprintf_unfiltered (stderr, "target_resume (%d, %s, %s)\n", pid,
1656 		      step ? "step" : "continue",
1657 		      target_signal_to_name (siggnal));
1658 }
1659 
1660 static int
1661 debug_to_wait (pid, status)
1662      int pid;
1663      struct target_waitstatus *status;
1664 {
1665   int retval;
1666 
1667   retval = debug_target.to_wait (pid, status);
1668 
1669   fprintf_unfiltered (stderr, "target_wait (%d, status) = %d,   ", pid, retval);
1670   fprintf_unfiltered (stderr, "status->kind = ");
1671   switch (status->kind)
1672     {
1673     case TARGET_WAITKIND_EXITED:
1674       fprintf_unfiltered (stderr, "exited, status = %d\n", status->value.integer);
1675       break;
1676     case TARGET_WAITKIND_STOPPED:
1677       fprintf_unfiltered (stderr, "stopped, signal = %s\n",
1678 			  target_signal_to_name (status->value.sig));
1679       break;
1680     case TARGET_WAITKIND_SIGNALLED:
1681       fprintf_unfiltered (stderr, "signalled, signal = %s\n",
1682 			  target_signal_to_name (status->value.sig));
1683       break;
1684     case TARGET_WAITKIND_LOADED:
1685       fprintf_unfiltered (stderr, "loaded\n");
1686       break;
1687     case TARGET_WAITKIND_SPURIOUS:
1688       fprintf_unfiltered (stderr, "spurious\n");
1689       break;
1690     default:
1691       fprintf_unfiltered (stderr, "unknown???\n");
1692       break;
1693     }
1694 
1695   return retval;
1696 }
1697 
1698 static void
1699 debug_to_fetch_registers (regno)
1700      int regno;
1701 {
1702   debug_target.to_fetch_registers (regno);
1703 
1704   fprintf_unfiltered (stderr, "target_fetch_registers (%s)",
1705 		      regno != -1 ? reg_names[regno] : "-1");
1706   if (regno != -1)
1707     fprintf_unfiltered (stderr, " = 0x%x %d", read_register (regno),
1708 			read_register (regno));
1709   fprintf_unfiltered (stderr, "\n");
1710 }
1711 
1712 static void
1713 debug_to_store_registers (regno)
1714      int regno;
1715 {
1716   debug_target.to_store_registers (regno);
1717 
1718   if (regno >= 0 && regno < NUM_REGS)
1719     fprintf_unfiltered (stderr, "target_store_registers (%s) = 0x%x %d\n",
1720 			reg_names[regno], read_register (regno),
1721 			read_register (regno));
1722   else
1723     fprintf_unfiltered (stderr, "target_store_registers (%d)\n", regno);
1724 }
1725 
1726 static void
1727 debug_to_prepare_to_store ()
1728 {
1729   debug_target.to_prepare_to_store ();
1730 
1731   fprintf_unfiltered (stderr, "target_prepare_to_store ()\n");
1732 }
1733 
1734 static int
1735 debug_to_xfer_memory (memaddr, myaddr, len, write, target)
1736      CORE_ADDR memaddr;
1737      char *myaddr;
1738      int len;
1739      int write;
1740      struct target_ops *target;
1741 {
1742   int retval;
1743 
1744   retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write, target);
1745 
1746   fprintf_unfiltered (stderr,
1747 		      "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
1748 		      memaddr, len, write ? "write" : "read", retval);
1749 
1750   if (retval > 0)
1751     {
1752       int i;
1753 
1754       fputs_unfiltered (", bytes =", gdb_stderr);
1755       for (i = 0; i < retval; i++)
1756 	{
1757 	  if ((((long) &(myaddr[i])) & 0xf) == 0)
1758 	    fprintf_unfiltered (stderr, "\n");
1759 	  fprintf_unfiltered (stderr, " %02x", myaddr[i] & 0xff);
1760 	}
1761     }
1762 
1763   fputc_unfiltered ('\n', gdb_stderr);
1764 
1765   return retval;
1766 }
1767 
1768 static void
1769 debug_to_files_info (target)
1770      struct target_ops *target;
1771 {
1772   debug_target.to_files_info (target);
1773 
1774   fprintf_unfiltered (stderr, "target_files_info (xxx)\n");
1775 }
1776 
1777 static int
1778 debug_to_insert_breakpoint (addr, save)
1779      CORE_ADDR addr;
1780      char *save;
1781 {
1782   int retval;
1783 
1784   retval = debug_target.to_insert_breakpoint (addr, save);
1785 
1786   fprintf_unfiltered (stderr, "target_insert_breakpoint (0x%x, xxx) = %d\n",
1787 		      addr, retval);
1788   return retval;
1789 }
1790 
1791 static int
1792 debug_to_remove_breakpoint (addr, save)
1793      CORE_ADDR addr;
1794      char *save;
1795 {
1796   int retval;
1797 
1798   retval = debug_target.to_remove_breakpoint (addr, save);
1799 
1800   fprintf_unfiltered (stderr, "target_remove_breakpoint (0x%x, xxx) = %d\n",
1801 		      addr, retval);
1802   return retval;
1803 }
1804 
1805 static void
1806 debug_to_terminal_init ()
1807 {
1808   debug_target.to_terminal_init ();
1809 
1810   fprintf_unfiltered (stderr, "target_terminal_init ()\n");
1811 }
1812 
1813 static void
1814 debug_to_terminal_inferior ()
1815 {
1816   debug_target.to_terminal_inferior ();
1817 
1818   fprintf_unfiltered (stderr, "target_terminal_inferior ()\n");
1819 }
1820 
1821 static void
1822 debug_to_terminal_ours_for_output ()
1823 {
1824   debug_target.to_terminal_ours_for_output ();
1825 
1826   fprintf_unfiltered (stderr, "target_terminal_ours_for_output ()\n");
1827 }
1828 
1829 static void
1830 debug_to_terminal_ours ()
1831 {
1832   debug_target.to_terminal_ours ();
1833 
1834   fprintf_unfiltered (stderr, "target_terminal_ours ()\n");
1835 }
1836 
1837 static void
1838 debug_to_terminal_info (arg, from_tty)
1839      char *arg;
1840      int from_tty;
1841 {
1842   debug_target.to_terminal_info (arg, from_tty);
1843 
1844   fprintf_unfiltered (stderr, "target_terminal_info (%s, %d)\n", arg,
1845 		      from_tty);
1846 }
1847 
1848 static void
1849 debug_to_kill ()
1850 {
1851   debug_target.to_kill ();
1852 
1853   fprintf_unfiltered (stderr, "target_kill ()\n");
1854 }
1855 
1856 static void
1857 debug_to_load (args, from_tty)
1858      char *args;
1859      int from_tty;
1860 {
1861   debug_target.to_load (args, from_tty);
1862 
1863   fprintf_unfiltered (stderr, "target_load (%s, %d)\n", args, from_tty);
1864 }
1865 
1866 static int
1867 debug_to_lookup_symbol (name, addrp)
1868      char *name;
1869      CORE_ADDR *addrp;
1870 {
1871   int retval;
1872 
1873   retval = debug_target.to_lookup_symbol (name, addrp);
1874 
1875   fprintf_unfiltered (stderr, "target_lookup_symbol (%s, xxx)\n", name);
1876 
1877   return retval;
1878 }
1879 
1880 static void
1881 debug_to_create_inferior (exec_file, args, env)
1882      char *exec_file;
1883      char *args;
1884      char **env;
1885 {
1886   debug_target.to_create_inferior (exec_file, args, env);
1887 
1888   fprintf_unfiltered (stderr, "target_create_inferior (%s, %s, xxx)\n",
1889 		      exec_file, args);
1890 }
1891 
1892 static void
1893 debug_to_mourn_inferior ()
1894 {
1895   debug_target.to_mourn_inferior ();
1896 
1897   fprintf_unfiltered (stderr, "target_mourn_inferior ()\n");
1898 }
1899 
1900 static int
1901 debug_to_can_run ()
1902 {
1903   int retval;
1904 
1905   retval = debug_target.to_can_run ();
1906 
1907   fprintf_unfiltered (stderr, "target_can_run () = %d\n", retval);
1908 
1909   return retval;
1910 }
1911 
1912 static void
1913 debug_to_notice_signals (pid)
1914      int pid;
1915 {
1916   debug_target.to_notice_signals (pid);
1917 
1918   fprintf_unfiltered (stderr, "target_notice_signals (%d)\n", pid);
1919 }
1920 
1921 static int
1922 debug_to_thread_alive (pid)
1923      int pid;
1924 {
1925   int retval;
1926 
1927   retval = debug_target.to_thread_alive (pid);
1928 
1929   fprintf_unfiltered (stderr, "target_thread_alive (%d) = %d\n", pid, retval);
1930 
1931   return retval;
1932 }
1933 
1934 static void
1935 debug_to_stop ()
1936 {
1937   debug_target.to_stop ();
1938 
1939   fprintf_unfiltered (stderr, "target_stop ()\n");
1940 }
1941 
1942 static void
1943 setup_target_debug ()
1944 {
1945   memcpy (&debug_target, &current_target, sizeof debug_target);
1946 
1947   current_target.to_open = debug_to_open;
1948   current_target.to_close = debug_to_close;
1949   current_target.to_attach = debug_to_attach;
1950   current_target.to_detach = debug_to_detach;
1951   current_target.to_resume = debug_to_resume;
1952   current_target.to_wait = debug_to_wait;
1953   current_target.to_fetch_registers = debug_to_fetch_registers;
1954   current_target.to_store_registers = debug_to_store_registers;
1955   current_target.to_prepare_to_store = debug_to_prepare_to_store;
1956   current_target.to_xfer_memory = debug_to_xfer_memory;
1957   current_target.to_files_info = debug_to_files_info;
1958   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
1959   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
1960   current_target.to_terminal_init = debug_to_terminal_init;
1961   current_target.to_terminal_inferior = debug_to_terminal_inferior;
1962   current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
1963   current_target.to_terminal_ours = debug_to_terminal_ours;
1964   current_target.to_terminal_info = debug_to_terminal_info;
1965   current_target.to_kill = debug_to_kill;
1966   current_target.to_load = debug_to_load;
1967   current_target.to_lookup_symbol = debug_to_lookup_symbol;
1968   current_target.to_create_inferior = debug_to_create_inferior;
1969   current_target.to_mourn_inferior = debug_to_mourn_inferior;
1970   current_target.to_can_run = debug_to_can_run;
1971   current_target.to_notice_signals = debug_to_notice_signals;
1972   current_target.to_thread_alive = debug_to_thread_alive;
1973   current_target.to_stop = debug_to_stop;
1974 }
1975 #endif /* MAINTENANCE_CMDS */
1976 
1977 static char targ_desc[] =
1978     "Names of targets and files being debugged.\n\
1979 Shows the entire stack of targets currently in use (including the exec-file,\n\
1980 core-file, and process, if any), as well as the symbol file name.";
1981 
1982 void
1983 initialize_targets ()
1984 {
1985   push_target (&dummy_target);
1986 
1987   add_info ("target", target_info, targ_desc);
1988   add_info ("files", target_info, targ_desc);
1989 
1990 #ifdef MAINTENANCE_CMDS
1991   add_show_from_set (
1992      add_set_cmd ("targetdebug", class_maintenance, var_zinteger,
1993 		  (char *)&targetdebug,
1994 		 "Set target debugging.\n\
1995 When non-zero, target debugging is enabled.", &setlist),
1996 		     &showlist);
1997 #endif
1998 
1999   if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
2000     abort ();
2001 }
2002