* target.c (nomemory): Fix prototype and routine to take correct
[external/binutils.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2    Copyright 1990, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "defs.h"
22 #include <errno.h>
23 #include <ctype.h>
24 #include "target.h"
25 #include "gdbcmd.h"
26 #include "symtab.h"
27 #include "inferior.h"
28 #include "bfd.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "wait.h"
32 #include <signal.h>
33
34 extern int errno;
35
36 static void
37 target_info PARAMS ((char *, int));
38
39 static void
40 cleanup_target PARAMS ((struct target_ops *));
41
42 static void
43 maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
44
45 static void
46 maybe_kill_then_attach PARAMS ((char *, int));
47
48 static void
49 kill_or_be_killed PARAMS ((int));
50
51 static void
52 default_terminal_info PARAMS ((char *, int));
53
54 static int
55 nosymbol PARAMS ((char *, CORE_ADDR *));
56
57 static void
58 tcomplain PARAMS ((void));
59
60 static int
61 nomemory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
62
63 static int
64 return_zero PARAMS ((void));
65
66 static void
67 ignore PARAMS ((void));
68
69 static void
70 target_command PARAMS ((char *, int));
71
72 static struct target_ops *
73 find_default_run_target PARAMS ((char *));
74
75 /* Pointer to array of target architecture structures; the size of the
76    array; the current index into the array; the allocated size of the 
77    array.  */
78 struct target_ops **target_structs;
79 unsigned target_struct_size;
80 unsigned target_struct_index;
81 unsigned target_struct_allocsize;
82 #define DEFAULT_ALLOCSIZE       10
83
84 /* The initial current target, so that there is always a semi-valid
85    current target.  */
86
87 struct target_ops dummy_target = {"None", "None", "",
88     0, 0,               /* open, close */
89     find_default_attach, 0,  /* attach, detach */
90     0, 0,               /* resume, wait */
91     0, 0, 0,            /* registers */
92     0, 0,               /* memory */
93     0, 0,               /* bkpts */
94     0, 0, 0, 0, 0,      /* terminal */
95     0, 0,               /* kill, load */
96     0,                  /* lookup_symbol */
97     find_default_create_inferior, /* create_inferior */
98     0,                  /* mourn_inferior */
99     0,                  /* can_run */
100     0,                  /* notice_signals */
101     dummy_stratum, 0,   /* stratum, next */
102     0, 0, 0, 0, 0,      /* all mem, mem, stack, regs, exec */
103     0, 0,               /* section pointers */
104     OPS_MAGIC,
105 };
106
107 /* Top of target stack.  */
108
109 struct target_stack_item *target_stack;
110
111 /* The target structure we are currently using to talk to a process
112    or file or whatever "inferior" we have.  */
113
114 struct target_ops current_target;
115
116 /* Command list for target.  */
117
118 static struct cmd_list_element *targetlist = NULL;
119
120 /* Nonzero if we are debugging an attached outside process
121    rather than an inferior.  */
122
123 int attach_flag;
124
125 /* The user just typed 'target' without the name of a target.  */
126
127 /* ARGSUSED */
128 static void
129 target_command (arg, from_tty)
130      char *arg;
131      int from_tty;
132 {
133   fputs_filtered ("Argument required (target name).  Try `help target'\n",
134                   gdb_stdout);
135 }
136
137 /* Add a possible target architecture to the list.  */
138
139 void
140 add_target (t)
141      struct target_ops *t;
142 {
143   if (!target_structs)
144     {
145       target_struct_allocsize = DEFAULT_ALLOCSIZE;
146       target_structs = (struct target_ops **) xmalloc
147         (target_struct_allocsize * sizeof (*target_structs));
148     }
149   if (target_struct_size >= target_struct_allocsize)
150     {
151       target_struct_allocsize *= 2;
152       target_structs = (struct target_ops **)
153           xrealloc ((char *) target_structs, 
154                     target_struct_allocsize * sizeof (*target_structs));
155     }
156   target_structs[target_struct_size++] = t;
157   cleanup_target (t);
158
159   if (targetlist == NULL)
160     add_prefix_cmd ("target", class_run, target_command,
161                     "Connect to a target machine or process.\n\
162 The first argument is the type or protocol of the target machine.\n\
163 Remaining arguments are interpreted by the target protocol.  For more\n\
164 information on the arguments for a particular protocol, type\n\
165 `help target ' followed by the protocol name.",
166                     &targetlist, "target ", 0, &cmdlist);
167   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
168 }
169
170 /* Stub functions */
171
172 static void
173 ignore ()
174 {
175 }
176
177 /* ARGSUSED */
178 static int
179 nomemory (memaddr, myaddr, len, write, t)
180      CORE_ADDR memaddr;
181      char *myaddr;
182      int len;
183      int write;
184      struct target_ops *t;
185 {
186   errno = EIO;          /* Can't read/write this location */
187   return 0;             /* No bytes handled */
188 }
189
190 static void
191 tcomplain ()
192 {
193   error ("You can't do that when your target is `%s'",
194          current_target.to_shortname);
195 }
196
197 void
198 noprocess ()
199 {
200   error ("You can't do that without a process to debug");
201 }
202
203 /* ARGSUSED */
204 static int
205 nosymbol (name, addrp)
206      char *name;
207      CORE_ADDR *addrp;
208 {
209   return 1;             /* Symbol does not exist in target env */
210 }
211
212 /* ARGSUSED */
213 static void
214 default_terminal_info (args, from_tty)
215      char *args;
216      int from_tty;
217 {
218   printf_unfiltered("No saved terminal information.\n");
219 }
220
221 /* This is the default target_create_inferior and target_attach function.
222    If the current target is executing, it asks whether to kill it off.
223    If this function returns without calling error(), it has killed off
224    the target, and the operation should be attempted.  */
225
226 static void
227 kill_or_be_killed (from_tty)
228      int from_tty;
229 {
230   if (target_has_execution)
231     {
232       printf_unfiltered ("You are already running a program:\n");
233       target_files_info ();
234       if (query ("Kill it? ")) {
235         target_kill ();
236         if (target_has_execution)
237           error ("Killing the program did not help.");
238         return;
239       } else {
240         error ("Program not killed.");
241       }
242     }
243   tcomplain();
244 }
245
246 static void
247 maybe_kill_then_attach (args, from_tty)
248      char *args;
249      int from_tty;
250 {
251   kill_or_be_killed (from_tty);
252   target_attach (args, from_tty);
253 }
254
255 static void
256 maybe_kill_then_create_inferior (exec, args, env)
257      char *exec;
258      char *args;
259      char **env;
260 {
261   kill_or_be_killed (0);
262   target_create_inferior (exec, args, env);
263 }
264
265 /* Clean up a target struct so it no longer has any zero pointers in it.
266    We default entries, at least to stubs that print error messages.  */
267
268 static void
269 cleanup_target (t)
270      struct target_ops *t;
271 {
272
273 #define de_fault(field, value) \
274   if (!t->field)        t->field = value
275
276   /*        FIELD                       DEFAULT VALUE        */
277
278   de_fault (to_open,                    (void (*)())tcomplain);
279   de_fault (to_close,                   (void (*)())ignore);
280   de_fault (to_attach,                  maybe_kill_then_attach);
281   de_fault (to_detach,                  (void (*)())ignore);
282   de_fault (to_resume,                  (void (*)())noprocess);
283   de_fault (to_wait,                    (int (*)())noprocess);
284   de_fault (to_fetch_registers,         (void (*)())ignore);
285   de_fault (to_store_registers,         (void (*)())noprocess);
286   de_fault (to_prepare_to_store,        (void (*)())noprocess);
287   de_fault (to_xfer_memory,             (int (*)())nomemory);
288   de_fault (to_files_info,              (void (*)())ignore);
289   de_fault (to_insert_breakpoint,       memory_insert_breakpoint);
290   de_fault (to_remove_breakpoint,       memory_remove_breakpoint);
291   de_fault (to_terminal_init,           ignore);
292   de_fault (to_terminal_inferior,       ignore);
293   de_fault (to_terminal_ours_for_output,ignore);
294   de_fault (to_terminal_ours,           ignore);
295   de_fault (to_terminal_info,           default_terminal_info);
296   de_fault (to_kill,                    (void (*)())noprocess);
297   de_fault (to_load,                    (void (*)())tcomplain);
298   de_fault (to_lookup_symbol,           nosymbol);
299   de_fault (to_create_inferior,         maybe_kill_then_create_inferior);
300   de_fault (to_mourn_inferior,          (void (*)())noprocess);
301   de_fault (to_can_run,                 return_zero);
302   de_fault (to_notice_signals,          (void (*)())ignore);
303
304 #undef de_fault
305 }
306
307 /* Go through the target stack from top to bottom, copying over zero entries in
308    current_target.  In effect, we are doing class inheritance through the
309    pushed target vectors.  */
310
311 static void
312 update_current_target ()
313 {
314   struct target_stack_item *item;
315   struct target_ops *t;
316
317   /* First, reset current_target */
318   memset (&current_target, 0, sizeof current_target);
319
320   for (item = target_stack; item; item = item->next)
321     {
322       t = item->target_ops;
323
324 #define INHERIT(FIELD, TARGET) \
325       if (!current_target.FIELD) \
326         current_target.FIELD = TARGET->FIELD
327
328       INHERIT (to_shortname, t);
329       INHERIT (to_longname, t);
330       INHERIT (to_doc, t);
331       INHERIT (to_open, t);
332       INHERIT (to_close, t);
333       INHERIT (to_attach, t);
334       INHERIT (to_detach, t);
335       INHERIT (to_resume, t);
336       INHERIT (to_wait, t);
337       INHERIT (to_fetch_registers, t);
338       INHERIT (to_store_registers, t);
339       INHERIT (to_prepare_to_store, t);
340       INHERIT (to_xfer_memory, t);
341       INHERIT (to_files_info, t);
342       INHERIT (to_insert_breakpoint, t);
343       INHERIT (to_remove_breakpoint, t);
344       INHERIT (to_terminal_init, t);
345       INHERIT (to_terminal_inferior, t);
346       INHERIT (to_terminal_ours_for_output, t);
347       INHERIT (to_terminal_ours, t);
348       INHERIT (to_terminal_info, t);
349       INHERIT (to_kill, t);
350       INHERIT (to_load, t);
351       INHERIT (to_lookup_symbol, t);
352       INHERIT (to_create_inferior, t);
353       INHERIT (to_mourn_inferior, t);
354       INHERIT (to_can_run, t);
355       INHERIT (to_notice_signals, t);
356       INHERIT (to_stratum, t);
357       INHERIT (DONT_USE, t);
358       INHERIT (to_has_all_memory, t);
359       INHERIT (to_has_memory, t);
360       INHERIT (to_has_stack, t);
361       INHERIT (to_has_registers, t);
362       INHERIT (to_has_execution, t);
363       INHERIT (to_sections, t);
364       INHERIT (to_sections_end, t);
365       INHERIT (to_magic, t);
366
367 #undef INHERIT
368     }
369 }
370
371 /* Push a new target type into the stack of the existing target accessors,
372    possibly superseding some of the existing accessors.
373
374    Result is zero if the pushed target ended up on top of the stack,
375    nonzero if at least one target is on top of it.
376
377    Rather than allow an empty stack, we always have the dummy target at
378    the bottom stratum, so we can call the function vectors without
379    checking them.  */
380
381 int
382 push_target (t)
383      struct target_ops *t;
384 {
385   struct target_stack_item *cur, *prev, *tmp;
386
387   /* Check magic number.  If wrong, it probably means someone changed
388      the struct definition, but not all the places that initialize one.  */
389   if (t->to_magic != OPS_MAGIC)
390     {
391       fprintf_unfiltered(gdb_stderr,
392                          "Magic number of %s target struct wrong\n", 
393                          t->to_shortname);
394       abort();
395     }
396
397   /* Find the proper stratum to install this target in. */
398
399   for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
400     {
401       if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
402         break;
403     }
404
405   /* If there's already targets at this stratum, remove them. */
406
407   if (cur)
408     while (t->to_stratum == cur->target_ops->to_stratum)
409       {
410         /* There's already something on this stratum.  Close it off.  */
411         (cur->target_ops->to_close) (0);
412         if (prev)
413           prev->next = cur->next; /* Unchain old target_ops */
414         else
415           target_stack = cur->next; /* Unchain first on list */
416         tmp = cur->next;
417         free (cur);
418         cur = tmp;
419       }
420
421   /* We have removed all targets in our stratum, now add the new one.  */
422
423   tmp = xmalloc (sizeof (struct target_stack_item));
424   tmp->next = cur;
425   tmp->target_ops = t;
426
427   if (prev)
428     prev->next = tmp;
429   else
430     target_stack = tmp;
431
432   update_current_target ();
433
434   cleanup_target (&current_target); /* Fill in the gaps */
435   return prev != 0;
436 }
437
438 /* Remove a target_ops vector from the stack, wherever it may be. 
439    Return how many times it was removed (0 or 1).  */
440
441 int
442 unpush_target (t)
443      struct target_ops *t;
444 {
445   struct target_stack_item *cur, *prev;
446
447   t->to_close (0);              /* Let it clean up */
448
449   /* Look for the specified target.  Note that we assume that a target
450      can only occur once in the target stack. */
451
452   for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
453     if (cur->target_ops == t)
454       break;
455
456   if (!cur)
457     return 0;                   /* Didn't find target_ops, quit now */
458
459   /* Unchain the target */
460
461   if (!prev)
462     target_stack = cur->next;
463   else
464     prev->next = cur->next;
465
466   free (cur);                   /* Release the target_stack_item */
467
468   update_current_target ();
469   cleanup_target (&current_target);
470
471   return 1;
472 }
473
474 void
475 pop_target ()
476 {
477   (current_target.to_close)(0); /* Let it clean up */
478   if (unpush_target (target_stack->target_ops) == 1)
479     return;
480
481   fprintf_unfiltered(gdb_stderr,
482                      "pop_target couldn't find target %s\n", 
483                      current_target.to_shortname);
484   abort();
485 }
486
487 #undef  MIN
488 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
489
490 /* target_read_string -- read a null terminated string, up to LEN bytes,
491    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
492    Set *STRING to a pointer to malloc'd memory containing the data; the caller
493    is responsible for freeing it.  Return the number of bytes successfully
494    read.  */
495
496 int
497 target_read_string (memaddr, string, len, errnop)
498      CORE_ADDR memaddr;
499      char **string;
500      int len;
501      int *errnop;
502 {
503   int tlen, origlen, offset, i;
504   char buf[4];
505   int errcode = 0;
506   char *buffer;
507   int buffer_allocated;
508   char *bufptr;
509   unsigned int nbytes_read = 0;
510
511   /* Small for testing.  */
512   buffer_allocated = 4;
513   buffer = xmalloc (buffer_allocated);
514   bufptr = buffer;
515
516   origlen = len;
517
518   while (len > 0)
519     {
520       tlen = MIN (len, 4 - (memaddr & 3));
521       offset = memaddr & 3;
522
523       errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
524       if (errcode != 0)
525         goto done;
526
527       if (bufptr - buffer + tlen > buffer_allocated)
528         {
529           unsigned int bytes;
530           bytes = bufptr - buffer;
531           buffer_allocated *= 2;
532           buffer = xrealloc (buffer, buffer_allocated);
533           bufptr = buffer + bytes;
534         }
535
536       for (i = 0; i < tlen; i++)
537         {
538           *bufptr++ = buf[i + offset];
539           if (buf[i + offset] == '\000')
540             {
541               nbytes_read += i + 1;
542               goto done;
543             }
544         }
545
546       memaddr += tlen;
547       len -= tlen;
548       nbytes_read += tlen;
549     }
550  done:
551   if (errnop != NULL)
552     *errnop = errcode;
553   if (string != NULL)
554     *string = buffer;
555   return nbytes_read;
556 }
557
558 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
559    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
560    if any error occurs.
561
562    If an error occurs, no guarantee is made about the contents of the data at
563    MYADDR.  In particular, the caller should not depend upon partial reads
564    filling the buffer with good data.  There is no way for the caller to know
565    how much good data might have been transfered anyway.  Callers that can
566    deal with partial reads should call target_read_memory_partial. */
567
568 int
569 target_read_memory (memaddr, myaddr, len)
570      CORE_ADDR memaddr;
571      char *myaddr;
572      int len;
573 {
574   return target_xfer_memory (memaddr, myaddr, len, 0);
575 }
576
577 /* Read LEN bytes of target memory at address MEMADDR, placing the results
578    in GDB's memory at MYADDR.  Returns a count of the bytes actually read,
579    and optionally an errno value in the location pointed to by ERRNOPTR
580    if ERRNOPTR is non-null. */
581
582 int
583 target_read_memory_partial (memaddr, myaddr, len, errnoptr)
584      CORE_ADDR memaddr;
585      char *myaddr;
586      int len;
587      int *errnoptr;
588 {
589   int nread;    /* Number of bytes actually read. */
590   int errcode;  /* Error from last read. */
591
592   /* First try a complete read. */
593   errcode = target_xfer_memory (memaddr, myaddr, len, 0);
594   if (errcode == 0)
595     {
596       /* Got it all. */
597       nread = len;
598     }
599   else
600     {
601       /* Loop, reading one byte at a time until we get as much as we can. */
602       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
603         {
604           errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0);
605         }
606       /* If an error, the last read was unsuccessful, so adjust count. */
607       if (errcode != 0)
608         {
609           nread--;
610         }
611     }
612   if (errnoptr != NULL)
613     {
614       *errnoptr = errcode;
615     }
616   return (nread);
617 }
618
619 int
620 target_write_memory (memaddr, myaddr, len)
621      CORE_ADDR memaddr;
622      char *myaddr;
623      int len;
624 {
625   return target_xfer_memory (memaddr, myaddr, len, 1);
626 }
627  
628 /* Move memory to or from the targets.  Iterate until all of it has
629    been moved, if necessary.  The top target gets priority; anything
630    it doesn't want, is offered to the next one down, etc.  Note the
631    business with curlen:  if an early target says "no, but I have a
632    boundary overlapping this xfer" then we shorten what we offer to
633    the subsequent targets so the early guy will get a chance at the
634    tail before the subsequent ones do. 
635
636    Result is 0 or errno value.  */
637
638 int
639 target_xfer_memory (memaddr, myaddr, len, write)
640      CORE_ADDR memaddr;
641      char *myaddr;
642      int len;
643      int write;
644 {
645   int curlen;
646   int res;
647   struct target_ops *t;
648   struct target_stack_item *item;
649
650   /* to_xfer_memory is not guaranteed to set errno, even when it returns
651      0.  */
652   errno = 0;
653
654   /* The quick case is that the top target does it all.  */
655   res = current_target.to_xfer_memory
656                         (memaddr, myaddr, len, write, &current_target);
657   if (res == len)
658     return 0;
659
660   if (res > 0)
661     goto bump;
662   /* If res <= 0 then we call it again in the loop.  Ah well.  */
663
664   for (; len > 0;)
665     {
666       curlen = len;             /* Want to do it all */
667       for (item = target_stack; item; item = item->next)
668         {
669           t = item->target_ops;
670
671           res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
672           if (res > 0)
673             break;              /* Handled all or part of xfer */
674           if (t->to_has_all_memory)
675             break;
676         }
677
678       if (res <= 0)
679         {
680           /* If this address is for nonexistent memory,
681              read zeros if reading, or do nothing if writing.  Return error. */
682           if (!write)
683             memset (myaddr, 0, len);
684           if (errno == 0)
685             return EIO;
686           else
687             return errno;
688         }
689 bump:
690       memaddr += res;
691       myaddr  += res;
692       len     -= res;
693     }
694   return 0;                     /* We managed to cover it all somehow. */
695 }
696
697
698 /* ARGSUSED */
699 static void
700 target_info (args, from_tty)
701      char *args;
702      int from_tty;
703 {
704   struct target_ops *t;
705   struct target_stack_item *item;
706   int has_all_mem = 0;
707   
708   if (symfile_objfile != NULL)
709     printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
710
711 #ifdef FILES_INFO_HOOK
712   if (FILES_INFO_HOOK ())
713     return;
714 #endif
715
716   for (item = target_stack; item; item = item->next)
717     {
718       t = item->target_ops;
719
720       if ((int)(t->to_stratum) <= (int)dummy_stratum)
721         continue;
722       if (has_all_mem)
723         printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
724       printf_unfiltered("%s:\n", t->to_longname);
725       (t->to_files_info)(t);
726       has_all_mem = t->to_has_all_memory;
727     }
728 }
729
730 /* This is to be called by the open routine before it does
731    anything.  */
732
733 void
734 target_preopen (from_tty)
735      int from_tty;
736 {
737   dont_repeat();
738
739   if (target_has_execution)
740     {   
741       if (query ("A program is being debugged already.  Kill it? "))
742         target_kill ();
743       else
744         error ("Program not killed.");
745     }
746
747   /* Calling target_kill may remove the target from the stack.  But if
748      it doesn't (which seems like a win for UDI), remove it now.  */
749
750   if (target_has_execution)
751     pop_target ();
752 }
753
754 /* Detach a target after doing deferred register stores.  */
755
756 void
757 target_detach (args, from_tty)
758      char *args;
759      int from_tty;
760 {
761   /* Handle any optimized stores to the inferior.  */
762 #ifdef DO_DEFERRED_STORES
763   DO_DEFERRED_STORES;
764 #endif
765   (current_target.to_detach) (args, from_tty);
766 }
767
768 void
769 target_link (modname, t_reloc)
770      char *modname;
771      CORE_ADDR *t_reloc;
772 {
773   if (STREQ(current_target.to_shortname, "rombug"))
774     {
775       (current_target.to_lookup_symbol) (modname, t_reloc);
776       if (*t_reloc == 0)
777       error("Unable to link to %s and get relocation in rombug", modname);
778     }
779   else
780     *t_reloc = (CORE_ADDR)-1;
781 }
782
783 /* Look through the list of possible targets for a target that can
784    execute a run or attach command without any other data.  This is
785    used to locate the default process stratum.
786
787    Result is always valid (error() is called for errors).  */
788
789 static struct target_ops *
790 find_default_run_target (do_mesg)
791      char *do_mesg;
792 {
793   struct target_ops **t;
794   struct target_ops *runable = NULL;
795   int count;
796
797   count = 0;
798
799   for (t = target_structs; t < target_structs + target_struct_size;
800        ++t)
801     {
802       if (target_can_run(*t))
803         {
804           runable = *t;
805           ++count;
806         }
807     }
808
809   if (count != 1)
810     error ("Don't know how to %s.  Try \"help target\".", do_mesg);
811
812   return runable;
813 }
814
815 void
816 find_default_attach (args, from_tty)
817      char *args;
818      int from_tty;
819 {
820   struct target_ops *t;
821
822   t = find_default_run_target("attach");
823   (t->to_attach) (args, from_tty);
824   return;
825 }
826
827 void
828 find_default_create_inferior (exec_file, allargs, env)
829      char *exec_file;
830      char *allargs;
831      char **env;
832 {
833   struct target_ops *t;
834
835   t = find_default_run_target("run");
836   (t->to_create_inferior) (exec_file, allargs, env);
837   return;
838 }
839
840 static int
841 return_zero ()
842 {
843   return 0;
844 }
845
846 struct target_ops *
847 find_core_target ()
848 {
849   struct target_ops **t;
850   struct target_ops *runable = NULL;
851   int count;
852   
853   count = 0;
854   
855   for (t = target_structs; t < target_structs + target_struct_size;
856        ++t)
857     {
858       if ((*t)->to_stratum == core_stratum)
859         {
860           runable = *t;
861           ++count;
862         }
863     }
864   
865   return(count == 1 ? runable : NULL);
866 }
867 \f
868 /* The inferior process has died.  Long live the inferior!  */
869
870 void
871 generic_mourn_inferior ()
872 {
873   extern int show_breakpoint_hit_counts;
874
875   inferior_pid = 0;
876   attach_flag = 0;
877   breakpoint_init_inferior ();
878   registers_changed ();
879
880 #ifdef CLEAR_DEFERRED_STORES
881   /* Delete any pending stores to the inferior... */
882   CLEAR_DEFERRED_STORES;
883 #endif
884
885   reopen_exec_file ();
886   reinit_frame_cache ();
887
888   /* It is confusing to the user for ignore counts to stick around
889      from previous runs of the inferior.  So clear them.  */
890   /* However, it is more confusing for the ignore counts to disappear when
891      using hit counts.  So don't clear them if we're counting hits.  */
892   if (!show_breakpoint_hit_counts)
893     breakpoint_clear_ignore_counts ();
894 }
895 \f
896 /* This table must match in order and size the signals in enum target_signal
897    in target.h.  */
898 static struct {
899   char *name;
900   char *string;
901   } signals [] =
902 {
903   {"0", "Signal 0"},
904   {"SIGHUP", "Hangup"},
905   {"SIGINT", "Interrupt"},
906   {"SIGQUIT", "Quit"},
907   {"SIGILL", "Illegal instruction"},
908   {"SIGTRAP", "Trace/breakpoint trap"},
909   {"SIGABRT", "Aborted"},
910   {"SIGEMT", "Emulation trap"},
911   {"SIGFPE", "Arithmetic exception"},
912   {"SIGKILL", "Killed"},
913   {"SIGBUS", "Bus error"},
914   {"SIGSEGV", "Segmentation fault"},
915   {"SIGSYS", "Bad system call"},
916   {"SIGPIPE", "Broken pipe"},
917   {"SIGALRM", "Alarm clock"},
918   {"SIGTERM", "Terminated"},
919   {"SIGURG", "Urgent I/O condition"},
920   {"SIGSTOP", "Stopped (signal)"},
921   {"SIGTSTP", "Stopped (user)"},
922   {"SIGCONT", "Continued"},
923   {"SIGCHLD", "Child status changed"},
924   {"SIGTTIN", "Stopped (tty input)"},
925   {"SIGTTOU", "Stopped (tty output)"},
926   {"SIGIO", "I/O possible"},
927   {"SIGXCPU", "CPU time limit exceeded"},
928   {"SIGXFSZ", "File size limit exceeded"},
929   {"SIGVTALRM", "Virtual timer expired"},
930   {"SIGPROF", "Profiling timer expired"},
931   {"SIGWINCH", "Window size changed"},
932   {"SIGLOST", "Resource lost"},
933   {"SIGUSR1", "User defined signal 1"},
934   {"SIGUSR2", "User defined signal 2"},
935   {"SIGPWR", "Power fail/restart"},
936   {"SIGPOLL", "Pollable event occurred"},
937   {"SIGWIND", "SIGWIND"},
938   {"SIGPHONE", "SIGPHONE"},
939   {"SIGWAITING", "Process's LWPs are blocked"},
940   {"SIGLWP", "Signal LWP"},
941   {"SIGDANGER", "Swap space dangerously low"},
942   {"SIGGRANT", "Monitor mode granted"},
943   {"SIGRETRACT", "Need to relinguish monitor mode"},
944   {"SIGMSG", "Monitor mode data available"},
945   {"SIGSOUND", "Sound completed"},
946   {"SIGSAK", "Secure attention"},
947   {NULL, "Unknown signal"},
948   {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
949
950   /* Last entry, used to check whether the table is the right size.  */
951   {NULL, "TARGET_SIGNAL_MAGIC"}
952 };
953
954 /* Return the string for a signal.  */
955 char *
956 target_signal_to_string (sig)
957      enum target_signal sig;
958 {
959   return signals[sig].string;
960 }
961
962 /* Return the name for a signal.  */
963 char *
964 target_signal_to_name (sig)
965      enum target_signal sig;
966 {
967   if (sig == TARGET_SIGNAL_UNKNOWN)
968     /* I think the code which prints this will always print it along with
969        the string, so no need to be verbose.  */
970     return "?";
971   return signals[sig].name;
972 }
973
974 /* Given a name, return its signal.  */
975 enum target_signal
976 target_signal_from_name (name)
977      char *name;
978 {
979   enum target_signal sig;
980
981   /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
982      for TARGET_SIGNAL_SIGCHLD.  SIGIOT, on the other hand, is more
983      questionable; seems like by now people should call it SIGABRT
984      instead.  */
985
986   /* This ugly cast brought to you by the native VAX compiler.  */
987   for (sig = TARGET_SIGNAL_HUP;
988        signals[sig].name != NULL;
989        sig = (enum target_signal)((int)sig + 1))
990     if (STREQ (name, signals[sig].name))
991       return sig;
992   return TARGET_SIGNAL_UNKNOWN;
993 }
994 \f
995 /* The following functions are to help certain targets deal
996    with the signal/waitstatus stuff.  They could just as well be in
997    a file called native-utils.c or unixwaitstatus-utils.c or whatever.  */
998
999 /* Convert host signal to our signals.  */
1000 enum target_signal
1001 target_signal_from_host (hostsig)
1002      int hostsig;
1003 {
1004   /* A switch statement would make sense but would require special kludges
1005      to deal with the cases where more than one signal has the same number.  */
1006
1007   if (hostsig == 0) return TARGET_SIGNAL_0;
1008
1009 #if defined (SIGHUP)
1010   if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1011 #endif
1012 #if defined (SIGINT)
1013   if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1014 #endif
1015 #if defined (SIGQUIT)
1016   if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1017 #endif
1018 #if defined (SIGILL)
1019   if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1020 #endif
1021 #if defined (SIGTRAP)
1022   if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1023 #endif
1024 #if defined (SIGABRT)
1025   if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1026 #endif
1027 #if defined (SIGEMT)
1028   if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1029 #endif
1030 #if defined (SIGFPE)
1031   if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1032 #endif
1033 #if defined (SIGKILL)
1034   if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1035 #endif
1036 #if defined (SIGBUS)
1037   if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1038 #endif
1039 #if defined (SIGSEGV)
1040   if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1041 #endif
1042 #if defined (SIGSYS)
1043   if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1044 #endif
1045 #if defined (SIGPIPE)
1046   if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1047 #endif
1048 #if defined (SIGALRM)
1049   if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1050 #endif
1051 #if defined (SIGTERM)
1052   if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1053 #endif
1054 #if defined (SIGUSR1)
1055   if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1056 #endif
1057 #if defined (SIGUSR2)
1058   if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1059 #endif
1060 #if defined (SIGCLD)
1061   if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1062 #endif
1063 #if defined (SIGCHLD)
1064   if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1065 #endif
1066 #if defined (SIGPWR)
1067   if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1068 #endif
1069 #if defined (SIGWINCH)
1070   if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1071 #endif
1072 #if defined (SIGURG)
1073   if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1074 #endif
1075 #if defined (SIGIO)
1076   if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1077 #endif
1078 #if defined (SIGPOLL)
1079   if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1080 #endif
1081 #if defined (SIGSTOP)
1082   if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1083 #endif
1084 #if defined (SIGTSTP)
1085   if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1086 #endif
1087 #if defined (SIGCONT)
1088   if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1089 #endif
1090 #if defined (SIGTTIN)
1091   if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1092 #endif
1093 #if defined (SIGTTOU)
1094   if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1095 #endif
1096 #if defined (SIGVTALRM)
1097   if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1098 #endif
1099 #if defined (SIGPROF)
1100   if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1101 #endif
1102 #if defined (SIGXCPU)
1103   if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1104 #endif
1105 #if defined (SIGXFSZ)
1106   if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1107 #endif
1108 #if defined (SIGWIND)
1109   if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1110 #endif
1111 #if defined (SIGPHONE)
1112   if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1113 #endif
1114 #if defined (SIGLOST)
1115   if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1116 #endif
1117 #if defined (SIGWAITING)
1118   if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1119 #endif
1120 #if defined (SIGLWP)
1121   if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1122 #endif
1123 #if defined (SIGDANGER)
1124   if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1125 #endif
1126 #if defined (SIGGRANT)
1127   if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1128 #endif
1129 #if defined (SIGRETRACT)
1130   if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1131 #endif
1132 #if defined (SIGMSG)
1133   if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1134 #endif
1135 #if defined (SIGSOUND)
1136   if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1137 #endif
1138 #if defined (SIGSAK)
1139   if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1140 #endif
1141   return TARGET_SIGNAL_UNKNOWN;
1142 }
1143
1144 int
1145 target_signal_to_host (oursig)
1146      enum target_signal oursig;
1147 {
1148   switch (oursig)
1149     {
1150     case TARGET_SIGNAL_0: return 0;
1151
1152 #if defined (SIGHUP)
1153     case TARGET_SIGNAL_HUP: return SIGHUP;
1154 #endif
1155 #if defined (SIGINT)
1156     case TARGET_SIGNAL_INT: return SIGINT;
1157 #endif
1158 #if defined (SIGQUIT)
1159     case TARGET_SIGNAL_QUIT: return SIGQUIT;
1160 #endif
1161 #if defined (SIGILL)
1162     case TARGET_SIGNAL_ILL: return SIGILL;
1163 #endif
1164 #if defined (SIGTRAP)
1165     case TARGET_SIGNAL_TRAP: return SIGTRAP;
1166 #endif
1167 #if defined (SIGABRT)
1168     case TARGET_SIGNAL_ABRT: return SIGABRT;
1169 #endif
1170 #if defined (SIGEMT)
1171     case TARGET_SIGNAL_EMT: return SIGEMT;
1172 #endif
1173 #if defined (SIGFPE)
1174     case TARGET_SIGNAL_FPE: return SIGFPE;
1175 #endif
1176 #if defined (SIGKILL)
1177     case TARGET_SIGNAL_KILL: return SIGKILL;
1178 #endif
1179 #if defined (SIGBUS)
1180     case TARGET_SIGNAL_BUS: return SIGBUS;
1181 #endif
1182 #if defined (SIGSEGV)
1183     case TARGET_SIGNAL_SEGV: return SIGSEGV;
1184 #endif
1185 #if defined (SIGSYS)
1186     case TARGET_SIGNAL_SYS: return SIGSYS;
1187 #endif
1188 #if defined (SIGPIPE)
1189     case TARGET_SIGNAL_PIPE: return SIGPIPE;
1190 #endif
1191 #if defined (SIGALRM)
1192     case TARGET_SIGNAL_ALRM: return SIGALRM;
1193 #endif
1194 #if defined (SIGTERM)
1195     case TARGET_SIGNAL_TERM: return SIGTERM;
1196 #endif
1197 #if defined (SIGUSR1)
1198     case TARGET_SIGNAL_USR1: return SIGUSR1;
1199 #endif
1200 #if defined (SIGUSR2)
1201     case TARGET_SIGNAL_USR2: return SIGUSR2;
1202 #endif
1203 #if defined (SIGCHLD) || defined (SIGCLD)
1204     case TARGET_SIGNAL_CHLD: 
1205 #if defined (SIGCHLD)
1206       return SIGCHLD;
1207 #else
1208       return SIGCLD;
1209 #endif
1210 #endif /* SIGCLD or SIGCHLD */
1211 #if defined (SIGPWR)
1212     case TARGET_SIGNAL_PWR: return SIGPWR;
1213 #endif
1214 #if defined (SIGWINCH)
1215     case TARGET_SIGNAL_WINCH: return SIGWINCH;
1216 #endif
1217 #if defined (SIGURG)
1218     case TARGET_SIGNAL_URG: return SIGURG;
1219 #endif
1220 #if defined (SIGIO)
1221     case TARGET_SIGNAL_IO: return SIGIO;
1222 #endif
1223 #if defined (SIGPOLL)
1224     case TARGET_SIGNAL_POLL: return SIGPOLL;
1225 #endif
1226 #if defined (SIGSTOP)
1227     case TARGET_SIGNAL_STOP: return SIGSTOP;
1228 #endif
1229 #if defined (SIGTSTP)
1230     case TARGET_SIGNAL_TSTP: return SIGTSTP;
1231 #endif
1232 #if defined (SIGCONT)
1233     case TARGET_SIGNAL_CONT: return SIGCONT;
1234 #endif
1235 #if defined (SIGTTIN)
1236     case TARGET_SIGNAL_TTIN: return SIGTTIN;
1237 #endif
1238 #if defined (SIGTTOU)
1239     case TARGET_SIGNAL_TTOU: return SIGTTOU;
1240 #endif
1241 #if defined (SIGVTALRM)
1242     case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1243 #endif
1244 #if defined (SIGPROF)
1245     case TARGET_SIGNAL_PROF: return SIGPROF;
1246 #endif
1247 #if defined (SIGXCPU)
1248     case TARGET_SIGNAL_XCPU: return SIGXCPU;
1249 #endif
1250 #if defined (SIGXFSZ)
1251     case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1252 #endif
1253 #if defined (SIGWIND)
1254     case TARGET_SIGNAL_WIND: return SIGWIND;
1255 #endif
1256 #if defined (SIGPHONE)
1257     case TARGET_SIGNAL_PHONE: return SIGPHONE;
1258 #endif
1259 #if defined (SIGLOST)
1260     case TARGET_SIGNAL_LOST: return SIGLOST;
1261 #endif
1262 #if defined (SIGWAITING)
1263     case TARGET_SIGNAL_WAITING: return SIGWAITING;
1264 #endif
1265 #if defined (SIGLWP)
1266     case TARGET_SIGNAL_LWP: return SIGLWP;
1267 #endif
1268 #if defined (SIGDANGER)
1269     case TARGET_SIGNAL_DANGER: return SIGDANGER;
1270 #endif
1271 #if defined (SIGGRANT)
1272     case TARGET_SIGNAL_GRANT: return SIGGRANT;
1273 #endif
1274 #if defined (SIGRETRACT)
1275     case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1276 #endif
1277 #if defined (SIGMSG)
1278     case TARGET_SIGNAL_MSG: return SIGMSG;
1279 #endif
1280 #if defined (SIGSOUND)
1281     case TARGET_SIGNAL_SOUND: return SIGSOUND;
1282 #endif
1283 #if defined (SIGSAK)
1284     case TARGET_SIGNAL_SAK: return SIGSAK;
1285 #endif
1286     default:
1287       /* The user might be trying to do "signal SIGSAK" where this system
1288          doesn't have SIGSAK.  */
1289       warning ("Signal %s does not exist on this system.\n",
1290                target_signal_to_name (oursig));
1291       return 0;
1292     }
1293 }
1294
1295 /* Helper function for child_wait and the Lynx derivatives of child_wait.
1296    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1297    translation of that in OURSTATUS.  */
1298 void
1299 store_waitstatus (ourstatus, hoststatus)
1300      struct target_waitstatus *ourstatus;
1301      int hoststatus;
1302 {
1303 #ifdef CHILD_SPECIAL_WAITSTATUS
1304   /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1305      if it wants to deal with hoststatus.  */
1306   if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1307     return;
1308 #endif
1309
1310   if (WIFEXITED (hoststatus))
1311     {
1312       ourstatus->kind = TARGET_WAITKIND_EXITED;
1313       ourstatus->value.integer = WEXITSTATUS (hoststatus);
1314     }
1315   else if (!WIFSTOPPED (hoststatus))
1316     {
1317       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1318       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1319     }
1320   else
1321     {
1322       ourstatus->kind = TARGET_WAITKIND_STOPPED;
1323       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1324     }
1325 }
1326
1327 \f
1328 /* Returns zero to leave the inferior alone, one to interrupt it.  */
1329 int (*target_activity_function) PARAMS ((void));
1330 int target_activity_fd;
1331 \f
1332 /* Convert a normal process ID to a string.  Returns the string in a static
1333    buffer.  */
1334
1335 char *
1336 normal_pid_to_str (pid)
1337      int pid;
1338 {
1339   static char buf[30];
1340
1341   sprintf (buf, "process %d", pid);
1342
1343   return buf;
1344 }
1345 \f
1346 static char targ_desc[] = 
1347     "Names of targets and files being debugged.\n\
1348 Shows the entire stack of targets currently in use (including the exec-file,\n\
1349 core-file, and process, if any), as well as the symbol file name.";
1350
1351 void
1352 initialize_targets ()
1353 {
1354   push_target (&dummy_target);
1355
1356   add_info ("target", target_info, targ_desc);
1357   add_info ("files", target_info, targ_desc);
1358
1359   if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
1360     abort ();
1361 }