home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / target.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-09  |  37.5 KB  |  1,467 lines

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