home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / remote-udi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-06  |  45.5 KB  |  1,676 lines

  1. /* Remote debugging interface for AMD 29k interfaced via UDI, for GDB.
  2.    Copyright 1990, 1992 Free Software Foundation, Inc.
  3.    Written by Daniel Mann.  Contributed by AMD.
  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. /* This is like remote.c but uses the Universal Debug Interface (UDI) to 
  22.    talk to the target hardware (or simulator).  UDI is a TCP/IP based
  23.    protocol; for hardware that doesn't run TCP, an interface adapter 
  24.    daemon talks UDI on one side, and talks to the hardware (typically
  25.    over a serial port) on the other side.
  26.  
  27.  - Originally written by Daniel Mann at AMD for MiniMON and gdb 3.91.6.
  28.  - David Wood (wood@lab.ultra.nyu.edu) at New York University adapted this
  29.     file to gdb 3.95.  I was unable to get this working on sun3os4
  30.     with termio, only with sgtty.
  31.  - Daniel Mann at AMD took the 3.95 adaptions above and replaced
  32.        MiniMON interface with UDI-p interface.      */
  33.  
  34. #include "defs.h"
  35. #include "inferior.h"
  36. #include "wait.h"
  37. #include "value.h"
  38. #include <ctype.h>
  39. #include <fcntl.h>
  40. #include <signal.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include "terminal.h"
  44. #include "target.h"
  45. #include "29k-share/udi/udiproc.h"
  46. #include "gdbcmd.h"
  47. #include "bfd.h"
  48. #include "gdbcore.h" /* For download function */
  49.  
  50. /* access the register store directly, without going through
  51.    the normal handler functions. This avoids an extra data copy.  */
  52.  
  53. extern int stop_soon_quietly;           /* for wait_for_inferior */
  54. extern struct value *call_function_by_hand();
  55. static void udi_resume PARAMS ((int pid, int step, enum target_signal sig));
  56. static void udi_fetch_registers PARAMS ((int regno));
  57. static void udi_load PARAMS ((char *args, int from_tty));
  58. static void fetch_register PARAMS ((int regno));
  59. static void udi_store_registers PARAMS ((int regno));
  60. static int store_register PARAMS ((int regno));
  61. static int regnum_to_srnum PARAMS ((int regno));
  62. static void udi_close PARAMS ((int quitting));
  63. static CPUSpace udi_memory_space PARAMS ((CORE_ADDR addr));
  64. static int udi_write_inferior_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
  65.                           int len));
  66. static int udi_read_inferior_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
  67.                          int len));
  68. static void download PARAMS ((char *load_arg_string, int from_tty));
  69. char   CoffFileName[100] = "";
  70.  
  71. #define FREEZE_MODE     (read_register(CPS_REGNUM) & 0x400)
  72. #define USE_SHADOW_PC    ((processor_type == a29k_freeze_mode) && FREEZE_MODE)
  73.  
  74. static int timeout = 5;
  75. extern struct target_ops udi_ops;             /* Forward declaration */
  76.  
  77. /* Special register enumeration.
  78. */
  79.  
  80. /******************************************************************* UDI DATA*/
  81. #define    MAXDATA        2*1024        /* max UDI[read/write] byte size */
  82. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  83.    udi_open knows that we don't have a file open when the program
  84.    starts.  */
  85.  
  86. UDISessionId udi_session_id = -1;
  87. static char *udi_config_id;
  88.  
  89. CPUOffset IMemStart = 0;
  90. CPUSizeT IMemSize = 0;
  91. CPUOffset DMemStart = 0;
  92. CPUSizeT DMemSize = 0;
  93. CPUOffset RMemStart = 0;
  94. CPUSizeT RMemSize = 0;
  95. UDIUInt32 CPUPRL;
  96. UDIUInt32 CoProcPRL;
  97.  
  98. UDIMemoryRange address_ranges[2]; /* Text and data */
  99. UDIResource entry = {0, 0};    /* Entry point */
  100. CPUSizeT stack_sizes[2];    /* Regular and memory stacks */
  101.  
  102. #define    SBUF_MAX    1024    /* maximum size of string handling buffer */
  103. char sbuf[SBUF_MAX];
  104.  
  105. typedef    struct     bkpt_entry_str
  106. {
  107.     UDIResource     Addr;
  108.     UDIUInt32     PassCount;
  109.     UDIBreakType Type;
  110.     unsigned int BreakId;
  111. } bkpt_entry_t;
  112. #define        BKPT_TABLE_SIZE 40
  113. static bkpt_entry_t    bkpt_table[BKPT_TABLE_SIZE];
  114. extern    char    dfe_errmsg[];        /* error string */
  115.  
  116. /* malloc'd name of the program on the remote system.  */
  117. static char *prog_name = NULL;
  118.  
  119. /* This is called not only when we first attach, but also when the
  120.    user types "run" after having attached.  */
  121.  
  122. static void
  123. udi_create_inferior (execfile, args, env)
  124.      char *execfile;
  125.      char *args;
  126.      char **env;
  127. {
  128.   char *args1;
  129.  
  130.   if (execfile)
  131.     {
  132.       if (prog_name != NULL)
  133.     free (prog_name);
  134.       prog_name = savestring (execfile, strlen (execfile));
  135.     }
  136.   else if (entry.Offset)
  137.     execfile = "";
  138.   else
  139.     error ("No image loaded into target.");
  140.  
  141.   if (udi_session_id < 0)
  142.     {
  143.       /* If the TIP is not open, open it.  */
  144.       if (UDIConnect (udi_config_id, &udi_session_id))
  145.     error("UDIConnect() failed: %s\n", dfe_errmsg);
  146.       /* We will need to download the program.  */
  147.       entry.Offset = 0;
  148.     }
  149.  
  150.   inferior_pid = 40000;
  151.  
  152.   if (!entry.Offset)
  153.     download(execfile, 0);
  154.  
  155.   args1 = alloca (strlen(execfile) + strlen(args) + 2);
  156.  
  157.   if (execfile[0] == '\0')
  158.  
  159.     /* It is empty.  We need to quote it somehow, or else the target
  160.        will think there is no argument being passed here.  According
  161.        to the UDI spec it is quoted "according to TIP OS rules" which
  162.        I guess means quoting it like the Unix shell should work
  163.        (sounds pretty bogus to me...).  In fact it doesn't work (with
  164.        isstip anyway), but passing in two quotes as the argument seems
  165.        like a reasonable enough behavior anyway (I guess).  */
  166.  
  167.     strcpy (args1, "''");
  168.   else
  169.     strcpy (args1, execfile);
  170.   strcat (args1, " ");
  171.   strcat (args1, args);
  172.  
  173.   UDIInitializeProcess (address_ranges,        /* ProcessMemory[] */
  174.             (UDIInt)2,        /* NumberOfRanges */
  175.             entry,            /* EntryPoint */
  176.             stack_sizes,        /* *StackSizes */
  177.             (UDIInt)2,        /* NumberOfStacks */
  178.             args1);            /* ArgString */
  179.  
  180.   init_wait_for_inferior ();
  181.   clear_proceed_status ();
  182.   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
  183. }
  184.  
  185. static void
  186. udi_mourn()
  187. {
  188. #if 0
  189.   /* Requiring "target udi" each time you run is a major pain.  I suspect
  190.      this was just blindy copied from remote.c, in which "target" and
  191.      "run" are combined.  Having a udi target without an inferior seems
  192.      to work between "target udi" and "run", so why not now?  */
  193.   pop_target ();                /* Pop back to no-child state */
  194. #endif
  195.   /* But if we're going to want to run it again, we better remove the
  196.      breakpoints...  */
  197.   remove_breakpoints ();
  198.   generic_mourn_inferior ();
  199. }
  200.  
  201. /******************************************************************** UDI_OPEN
  202. ** Open a connection to remote TIP.
  203.    NAME is the socket domain used for communication with the TIP,
  204.    then a space and the socket name or TIP-host name.
  205.    '<udi_udi_config_id>' for example.
  206.  */
  207.  
  208. /* XXX - need cleanups for udiconnect for various failures!!! */
  209.  
  210. static void
  211. udi_open (name, from_tty)
  212.      char *name;
  213.      int from_tty;
  214. {
  215.   unsigned int prl;
  216.   char *p;
  217.   int cnt;
  218.   UDIMemoryRange KnownMemory[10];
  219.   UDIUInt32 ChipVersions[10];
  220.   UDIInt NumberOfRanges = 10;
  221.   UDIInt NumberOfChips = 10;
  222.   UDIPId PId;
  223.   UDIUInt32 TIPId, TargetId, DFEId, DFE, TIP, DFEIPCId, TIPIPCId;
  224.  
  225.   target_preopen(from_tty);
  226.  
  227.   entry.Offset = 0;
  228.  
  229.   for (cnt = 0; cnt < BKPT_TABLE_SIZE; cnt++)
  230.     bkpt_table[cnt].Type = 0;
  231.  
  232.   if (udi_config_id)
  233.     free (udi_config_id);
  234.  
  235.   if (!name)
  236.     error("Usage: target udi config_id, where config_id appears in udi_soc file");
  237.  
  238.   udi_config_id = strdup (strtok (name, " \t"));
  239.  
  240.   if (UDIConnect (udi_config_id, &udi_session_id))
  241.     /* FIXME: Should set udi_session_id to -1 here.  */
  242.     error("UDIConnect() failed: %s\n", dfe_errmsg);
  243.  
  244.   push_target (&udi_ops);
  245.  
  246.   /*
  247.   ** Initialize target configuration structure (global)
  248.   */
  249.   if (UDIGetTargetConfig (KnownMemory, &NumberOfRanges,
  250.               ChipVersions, &NumberOfChips))
  251.     error ("UDIGetTargetConfig() failed");
  252.   if (NumberOfChips > 2)
  253.     fprintf_unfiltered(gdb_stderr,"Target has more than one processor\n");
  254.   for (cnt=0; cnt < NumberOfRanges; cnt++)
  255.     {
  256.       switch(KnownMemory[cnt].Space)
  257.     {
  258.     default:
  259.       fprintf_unfiltered(gdb_stderr, "UDIGetTargetConfig() unknown memory space\n");
  260.       break;
  261.     case UDI29KCP_S:
  262.       break;
  263.     case UDI29KIROMSpace:
  264.       RMemStart = KnownMemory[cnt].Offset;
  265.       RMemSize = KnownMemory[cnt].Size;
  266.       break;
  267.     case UDI29KIRAMSpace:
  268.       IMemStart = KnownMemory[cnt].Offset;
  269.       IMemSize = KnownMemory[cnt].Size;
  270.       break;
  271.     case UDI29KDRAMSpace:
  272.       DMemStart = KnownMemory[cnt].Offset;
  273.       DMemSize = KnownMemory[cnt].Size;
  274.       break;
  275.     }
  276.     }
  277.  
  278.   a29k_get_processor_type ();
  279.  
  280.   if (UDICreateProcess (&PId))
  281.      fprintf_unfiltered(gdb_stderr, "UDICreateProcess() failed\n");
  282.  
  283.   /* Print out some stuff, letting the user now what's going on */
  284.   if (UDICapabilities (&TIPId, &TargetId, DFEId, DFE, &TIP, &DFEIPCId,
  285.                &TIPIPCId, sbuf))
  286.     error ("UDICapabilities() failed");
  287.   if (from_tty)
  288.     {
  289.       printf_filtered ("Connected via UDI socket,\n\
  290.  DFE-IPC version %x.%x.%x  TIP-IPC version %x.%x.%x  TIP version %x.%x.%x\n %s\n",
  291.                (DFEIPCId>>8)&0xf, (DFEIPCId>>4)&0xf, DFEIPCId&0xf,
  292.                (TIPIPCId>>8)&0xf, (TIPIPCId>>4)&0xf, TIPIPCId&0xf,
  293.                (TargetId>>8)&0xf, (TargetId>>4)&0xf, TargetId&0xf,
  294.                sbuf);
  295.     }
  296. }
  297.  
  298. /******************************************************************* UDI_CLOSE
  299.    Close the open connection to the TIP process.
  300.    Use this when you want to detach and do something else
  301.    with your gdb.  */
  302. static void
  303. udi_close (quitting)    /*FIXME: how is quitting used */
  304.      int quitting;
  305. {
  306.   if (udi_session_id < 0)
  307.     return;
  308.  
  309.   /* We should never get here if there isn't something valid in
  310.      udi_session_id.  */
  311.  
  312.   if (UDIDisconnect (udi_session_id, UDITerminateSession))
  313.     {
  314.       if (quitting)
  315.     warning ("UDIDisconnect() failed in udi_close");
  316.       else
  317.     error ("UDIDisconnect() failed in udi_close");
  318.     }
  319.  
  320.   /* Do not try to close udi_session_id again, later in the program.  */
  321.   udi_session_id = -1;
  322.   inferior_pid = 0;
  323.  
  324.   printf_filtered ("  Ending remote debugging\n");
  325.  
  326. /**************************************************************** UDI_ATACH */
  327. /* Attach to a program that is already loaded and running 
  328.  * Upon exiting the process's execution is stopped.
  329.  */
  330. static void
  331. udi_attach (args, from_tty)
  332.      char *args;
  333.      int from_tty;
  334. {
  335.   UDIResource    From;
  336.   UDIInt32    PC_adds;
  337.   UDICount    Count = 1;
  338.   UDISizeT    Size = 4;
  339.   UDICount    CountDone;
  340.   UDIBool    HostEndian = 0;
  341.   UDIError    err;
  342.  
  343.   if (args == NULL)
  344.     error_no_arg ("program to attach");
  345.  
  346.   if (udi_session_id < 0)
  347.       error ("UDI connection not opened yet, use the 'target udi' command.\n");
  348.     
  349.   if (from_tty)
  350.       printf_unfiltered ("Attaching to remote program %s...\n", prog_name);
  351.  
  352.   UDIStop();
  353.   From.Space = UDI29KSpecialRegs;
  354.   From.Offset = 11;
  355.   if (err = UDIRead(From, &PC_adds, Count, Size, &CountDone, HostEndian))
  356.     error ("UDIRead failed in udi_attach");
  357.   printf_unfiltered ("Remote process is now halted, pc1 = 0x%x.\n", PC_adds);
  358. }
  359. /************************************************************* UDI_DETACH */
  360. /* Terminate the open connection to the TIP process.
  361.    Use this when you want to detach and do something else
  362.    with your gdb.  Leave remote process running (with no breakpoints set). */
  363. static void
  364. udi_detach (args,from_tty)
  365.      char *args;
  366.      int from_tty;
  367. {
  368.  
  369.   remove_breakpoints();        /* Just in case there were any left in */
  370.  
  371.   if (UDIDisconnect (udi_session_id, UDIContinueSession))
  372.     error ("UDIDisconnect() failed in udi_detach");
  373.  
  374.   /* Don't try to UDIDisconnect it again in udi_close, which is called from
  375.      pop_target.  */
  376.   udi_session_id = -1;
  377.   inferior_pid = 0;
  378.  
  379.   pop_target();
  380.  
  381.   if (from_tty)
  382.     printf_unfiltered ("Detaching from TIP\n");
  383. }
  384.  
  385.  
  386. /****************************************************************** UDI_RESUME
  387. ** Tell the remote machine to resume.  */
  388.  
  389. static void
  390. udi_resume (pid, step, sig)
  391.      int pid, step;
  392.      enum target_signal sig;
  393. {
  394.   UDIError tip_error;
  395.   UDIUInt32 Steps = 1;
  396.   UDIStepType StepType = UDIStepNatural;
  397.   UDIRange Range;
  398.  
  399.   if (step)             /* step 1 instruction */
  400.     {
  401.       tip_error = UDIStep (Steps, StepType, Range);
  402.       if (!tip_error)
  403.     return;
  404.  
  405.       fprintf_unfiltered (gdb_stderr,  "UDIStep() error = %d\n", tip_error);
  406.       error ("failed in udi_resume");
  407.     }
  408.  
  409.   if (UDIExecute())
  410.     error ("UDIExecute() failed in udi_resume");
  411. }
  412.  
  413. /******************************************************************** UDI_WAIT
  414. ** Wait until the remote machine stops, then return,
  415.    storing status in STATUS just as `wait' would.  */
  416.  
  417. static int
  418. udi_wait (pid, status)
  419.      int pid;
  420.      struct target_waitstatus *status;
  421. {
  422.   UDIInt32    MaxTime;
  423.   UDIPId    PId;
  424.   UDIInt32    StopReason;
  425.   UDISizeT    CountDone;
  426.   int         old_timeout = timeout;
  427.   int         old_immediate_quit = immediate_quit;
  428.   int        i;
  429.  
  430.   status->kind = TARGET_WAITKIND_EXITED;
  431.   status->value.integer = 0;
  432.  
  433. /* wait for message to arrive. It should be:
  434.   If the target stops executing, udi_wait() should return.
  435. */
  436.   timeout = 0;            /* Wait indefinetly for a message */
  437.   immediate_quit = 1;       /* Helps ability to QUIT */
  438.  
  439.   while(1)
  440.     {
  441.       i = 0;
  442.       MaxTime = UDIWaitForever;
  443.       UDIWait(MaxTime, &PId, &StopReason);
  444.       QUIT;            /* Let user quit if they want */
  445.  
  446.       switch (StopReason & UDIGrossState)
  447.     {
  448.     case UDIStdoutReady:
  449.       if (UDIGetStdout (sbuf, (UDISizeT)SBUF_MAX, &CountDone))
  450.         /* This is said to happen if the program tries to output
  451.            a whole bunch of output (more than SBUF_MAX, I would
  452.            guess).  It doesn't seem to happen with the simulator.  */
  453.         warning ("UDIGetStdout() failed in udi_wait");
  454.       fwrite (sbuf, 1, CountDone, gdb_stdout);
  455.       gdb_flush(gdb_stdout);
  456.       continue;
  457.  
  458.     case UDIStderrReady:
  459.       UDIGetStderr (sbuf, (UDISizeT)SBUF_MAX, &CountDone);
  460.       fwrite (sbuf, 1, CountDone, gdb_stderr);
  461.       gdb_flush(gdb_stderr);
  462.       continue;
  463.  
  464.     case UDIStdinNeeded:
  465.       {
  466.         int ch;
  467.         i = 0;
  468.         do
  469.           {
  470.         ch = getchar ();
  471.         if (ch == EOF)
  472.           break;
  473.         sbuf[i++] = ch;
  474.           } while (i < SBUF_MAX && ch != '\n');
  475.         UDIPutStdin (sbuf, (UDISizeT)i, &CountDone);
  476.         continue;
  477.       }
  478.  
  479.     case UDIRunning:
  480.       /* In spite of the fact that we told UDIWait to wait forever, it will
  481.          return spuriously sometimes.  */
  482.     case UDIStdinModeX:
  483.       continue;
  484.     default:
  485.       break;
  486.     }
  487.       break;
  488.     }
  489.  
  490.   switch (StopReason & UDIGrossState)
  491.     {
  492.     case UDITrapped:
  493.       printf_unfiltered("Am290*0 received vector number %d\n", StopReason >> 24);
  494.       
  495.       switch (StopReason >> 8)
  496.     {
  497.     case 0:            /* Illegal opcode */
  498.       printf_unfiltered("    (break point)\n");
  499.       status->kind = TARGET_WAITKIND_STOPPED;
  500.       status->value.sig = TARGET_SIGNAL_TRAP;
  501.       break;
  502.     case 1:            /* Unaligned Access */
  503.       status->kind = TARGET_WAITKIND_STOPPED;
  504.       status->value.sig = TARGET_SIGNAL_BUS;
  505.       break;
  506.     case 3:
  507.     case 4:
  508.       status->kind = TARGET_WAITKIND_STOPPED;
  509.       status->value.sig = TARGET_SIGNAL_FPE;
  510.       break;
  511.     case 5:            /* Protection Violation */
  512.       status->kind = TARGET_WAITKIND_STOPPED;
  513.       /* Why not SEGV?  What is a Protection Violation?  */
  514.       status->value.sig = TARGET_SIGNAL_ILL;
  515.       break;
  516.     case 6:
  517.     case 7:
  518.     case 8:            /* User Instruction Mapping Miss */
  519.     case 9:            /* User Data Mapping Miss */
  520.     case 10:        /* Supervisor Instruction Mapping Miss */
  521.     case 11:        /* Supervisor Data Mapping Miss */
  522.       status->kind = TARGET_WAITKIND_STOPPED;
  523.       status->value.sig = TARGET_SIGNAL_SEGV;
  524.       break;
  525.     case 12:
  526.     case 13:
  527.       status->kind = TARGET_WAITKIND_STOPPED;
  528.       status->value.sig = TARGET_SIGNAL_ILL;
  529.       break;
  530.     case 14:        /* Timer */
  531.       status->kind = TARGET_WAITKIND_STOPPED;
  532.       status->value.sig = TARGET_SIGNAL_ALRM;
  533.       break;
  534.     case 15:        /* Trace */
  535.       status->kind = TARGET_WAITKIND_STOPPED;
  536.       status->value.sig = TARGET_SIGNAL_TRAP;
  537.       break;
  538.     case 16:        /* INTR0 */
  539.     case 17:        /* INTR1 */
  540.     case 18:        /* INTR2 */
  541.     case 19:        /* INTR3/Internal */
  542.     case 20:        /* TRAP0 */
  543.     case 21:        /* TRAP1 */
  544.       status->kind = TARGET_WAITKIND_STOPPED;
  545.       status->value.sig = TARGET_SIGNAL_INT;
  546.       break;
  547.     case 22:        /* Floating-Point Exception */
  548.       status->kind = TARGET_WAITKIND_STOPPED;
  549.       /* Why not FPE?  */
  550.       status->value.sig = TARGET_SIGNAL_ILL;
  551.       break;
  552.     case 77:        /* assert 77 */
  553.       status->kind = TARGET_WAITKIND_STOPPED;
  554.       status->value.sig = TARGET_SIGNAL_TRAP;
  555.       break;
  556.     default:
  557.       status->kind = TARGET_WAITKIND_EXITED;
  558.       status->value.integer = 0;
  559.     }
  560.       break;
  561.     case UDINotExecuting:
  562.       status->kind = TARGET_WAITKIND_STOPPED;
  563.       status->value.sig = TARGET_SIGNAL_TERM;
  564.       break;
  565.     case UDIStopped:
  566.       status->kind = TARGET_WAITKIND_STOPPED;
  567.       status->value.sig = TARGET_SIGNAL_TSTP;
  568.       break;
  569.     case UDIWarned:
  570.       status->kind = TARGET_WAITKIND_STOPPED;
  571.       status->value.sig = TARGET_SIGNAL_URG;
  572.       break;
  573.     case UDIStepped:
  574.     case UDIBreak:
  575.       status->kind = TARGET_WAITKIND_STOPPED;
  576.       status->value.sig = TARGET_SIGNAL_TRAP;
  577.       break;
  578.     case UDIWaiting:
  579.       status->kind = TARGET_WAITKIND_STOPPED;
  580.       status->value.sig = TARGET_SIGNAL_STOP;
  581.       break;
  582.     case UDIHalted:
  583.       status->kind = TARGET_WAITKIND_STOPPED;
  584.       status->value.sig = TARGET_SIGNAL_KILL;
  585.       break;
  586.     case UDIExited:
  587.     default:
  588.       status->kind = TARGET_WAITKIND_EXITED;
  589.       status->value.integer = 0;
  590.     }
  591.  
  592.   timeout = old_timeout;    /* Restore original timeout value */
  593.   immediate_quit = old_immediate_quit;
  594.   return inferior_pid;
  595. }
  596.  
  597. #if 0
  598. /* Handy for debugging */
  599. udi_pc()
  600. {
  601.   UDIResource    From;
  602.   UDIUInt32    *To;
  603.   UDICount    Count;
  604.   UDISizeT    Size = 4;
  605.   UDICount    CountDone;
  606.   UDIBool    HostEndian = 0;
  607.   UDIError    err;
  608.   int pc[2];
  609.   unsigned long myregs[256];
  610.   int i;
  611.  
  612.   From.Space = UDI29KPC;
  613.   From.Offset = 0;
  614.   To = (UDIUInt32 *)pc;
  615.   Count = 2;
  616.  
  617.   err = UDIRead(From, To, Count, Size, &CountDone, HostEndian);
  618.  
  619.   printf_unfiltered ("err = %d, CountDone = %d, pc[0] = 0x%x, pc[1] = 0x%x\n",
  620.       err, CountDone, pc[0], pc[1]);
  621.  
  622.   udi_fetch_registers(-1);
  623.  
  624.   printf_unfiltered("other pc1 = 0x%x, pc0 = 0x%x\n", *(int *)®isters[4 * PC_REGNUM],
  625.       *(int *)®isters[4 * NPC_REGNUM]);
  626.  
  627.   /* Now, read all the registers globally */
  628.  
  629.   From.Space = UDI29KGlobalRegs;
  630.   From.Offset = 0;
  631.   err = UDIRead(From, myregs, 256, 4, &CountDone, HostEndian);
  632.  
  633.   printf ("err = %d, CountDone = %d\n", err, CountDone);
  634.  
  635.   printf("\n");
  636.  
  637.   for (i = 0; i < 256; i += 2)
  638.     printf("%d:\t%#10x\t%11d\t%#10x\t%11d\n", i, myregs[i], myregs[i],
  639.        myregs[i+1], myregs[i+1]);
  640.   printf("\n");
  641.  
  642.   return pc[0];
  643. }
  644. #endif
  645.  
  646. /********************************************************** UDI_FETCH_REGISTERS
  647.  * Read a remote register 'regno'. 
  648.  * If regno==-1 then read all the registers.
  649.  */
  650. static void 
  651. udi_fetch_registers (regno)
  652. int    regno;
  653. {
  654.   UDIResource    From;
  655.   UDIUInt32    *To;
  656.   UDICount    Count;
  657.   UDISizeT    Size = 4;
  658.   UDICount    CountDone;
  659.   UDIBool    HostEndian = 0;
  660.   UDIError    err;
  661.   int        i;
  662.  
  663.   if (regno >= 0)  {
  664.     fetch_register(regno);
  665.     return;
  666.   }
  667.  
  668. /* Gr1/rsp */
  669.  
  670.   From.Space = UDI29KGlobalRegs;
  671.   From.Offset = 1;
  672.   To = (UDIUInt32 *)®isters[4 * GR1_REGNUM];
  673.   Count = 1;
  674.   if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  675.     error("UDIRead() failed in udi_fetch_registers");
  676.  
  677.   register_valid[GR1_REGNUM] = 1;
  678.  
  679. #if defined(GR64_REGNUM)    /* Read gr64-127 */
  680.  
  681. /* Global Registers gr64-gr95 */ 
  682.  
  683.   From.Space = UDI29KGlobalRegs;
  684.   From.Offset = 64;
  685.   To = (UDIUInt32 *)®isters[4 * GR64_REGNUM];
  686.   Count = 32;
  687.   if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  688.     error("UDIRead() failed in udi_fetch_registers");
  689.  
  690.   for (i = GR64_REGNUM; i < GR64_REGNUM + 32; i++)
  691.     register_valid[i] = 1;
  692.  
  693. #endif    /*  GR64_REGNUM */
  694.  
  695. /* Global Registers gr96-gr127 */ 
  696.  
  697.   From.Space = UDI29KGlobalRegs;
  698.   From.Offset = 96;
  699.   To = (UDIUInt32 *)®isters[4 * GR96_REGNUM];
  700.   Count = 32;
  701.   if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  702.     error("UDIRead() failed in udi_fetch_registers");
  703.  
  704.   for (i = GR96_REGNUM; i < GR96_REGNUM + 32; i++)
  705.     register_valid[i] = 1;
  706.  
  707. /* Local Registers */
  708.  
  709.   From.Space = UDI29KLocalRegs;
  710.   From.Offset = 0;
  711.   To = (UDIUInt32 *)®isters[4 * LR0_REGNUM];
  712.   Count = 128;
  713.   if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  714.     error("UDIRead() failed in udi_fetch_registers");
  715.  
  716.   for (i = LR0_REGNUM; i < LR0_REGNUM + 128; i++)
  717.     register_valid[i] = 1;
  718.  
  719. /* Protected Special Registers */
  720.  
  721.   From.Space = UDI29KSpecialRegs;
  722.   From.Offset = 0;
  723.   To = (UDIUInt32 *)®isters[4 * SR_REGNUM(0)];
  724.   Count = 15;
  725.   if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  726.     error("UDIRead() failed in udi_fetch_registers");
  727.  
  728.   for (i = SR_REGNUM(0); i < SR_REGNUM(0) + 15; i++)
  729.     register_valid[i] = 1;
  730.  
  731.   if (USE_SHADOW_PC) {    /* Let regno_to_srnum() handle the register number */
  732.     fetch_register(NPC_REGNUM);
  733.     fetch_register(PC_REGNUM);
  734.     fetch_register(PC2_REGNUM);
  735.  
  736. /* Unprotected Special Registers sr128-sr135 */
  737.  
  738.     From.Space = UDI29KSpecialRegs;
  739.     From.Offset = 128;
  740.     To = (UDIUInt32 *)®isters[4 * SR_REGNUM(128)];
  741.     Count = 135-128 + 1;
  742.     if (err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  743.       error("UDIRead() failed in udi_fetch_registers");
  744.  
  745.     for (i = SR_REGNUM(128); i < SR_REGNUM(128) + 135-128+1; i++)
  746.       register_valid[i] = 1;
  747.   }
  748.  
  749.   if (remote_debug)
  750.     {
  751.       printf_unfiltered("Fetching all registers\n");
  752.       printf_unfiltered("Fetching PC0 = 0x%x, PC1 = 0x%x, PC2 = 0x%x\n",
  753.          read_register(NPC_REGNUM), read_register(PC_REGNUM),
  754.          read_register(PC2_REGNUM));
  755.     }
  756.  
  757.   /* There doesn't seem to be any way to get these.  */
  758.   {
  759.     int val = -1;
  760.     supply_register (FPE_REGNUM, (char *) &val);
  761.     supply_register (INTE_REGNUM, (char *) &val);
  762.     supply_register (FPS_REGNUM, (char *) &val);
  763.     supply_register (EXO_REGNUM, (char *) &val);
  764.   }
  765. }
  766.  
  767.  
  768. /********************************************************* UDI_STORE_REGISTERS
  769. ** Store register regno into the target.  
  770.  * If regno==-1 then store all the registers.
  771.  */
  772.  
  773. static void
  774. udi_store_registers (regno)
  775. int regno;
  776. {
  777.   UDIUInt32    *From;
  778.   UDIResource    To;
  779.   UDICount    Count;
  780.   UDISizeT    Size = 4;
  781.   UDICount    CountDone;
  782.   UDIBool    HostEndian = 0;
  783.   
  784.   if (regno >= 0)
  785.     {
  786.       store_register(regno);
  787.       return;
  788.     }
  789.  
  790.   if (remote_debug)
  791.     {
  792.       printf_unfiltered("Storing all registers\n");
  793.       printf_unfiltered("PC0 = 0x%x, PC1 = 0x%x, PC2 = 0x%x\n", read_register(NPC_REGNUM),
  794.          read_register(PC_REGNUM), read_register(PC2_REGNUM));
  795.     }
  796.  
  797. /* Gr1/rsp */
  798.  
  799.   From = (UDIUInt32 *)®isters[4 * GR1_REGNUM];
  800.   To.Space = UDI29KGlobalRegs;
  801.   To.Offset = 1;
  802.   Count = 1;
  803.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  804.     error("UDIWrite() failed in udi_store_regisetrs");
  805.  
  806. #if defined(GR64_REGNUM)
  807.  
  808. /* Global registers gr64-gr95 */
  809.  
  810.   From = (UDIUInt32 *)®isters[4 * GR64_REGNUM];
  811.   To.Space = UDI29KGlobalRegs;
  812.   To.Offset = 64;
  813.   Count = 32;
  814.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  815.     error("UDIWrite() failed in udi_store_regisetrs");
  816.  
  817. #endif    /* GR64_REGNUM */
  818.  
  819. /* Global registers gr96-gr127 */
  820.  
  821.   From = (UDIUInt32 *)®isters[4 * GR96_REGNUM];
  822.   To.Space = UDI29KGlobalRegs;
  823.   To.Offset = 96;
  824.   Count = 32;
  825.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  826.     error("UDIWrite() failed in udi_store_regisetrs");
  827.  
  828. /* Local Registers */
  829.  
  830.   From = (UDIUInt32 *)®isters[4 * LR0_REGNUM];
  831.   To.Space = UDI29KLocalRegs;
  832.   To.Offset = 0;
  833.   Count = 128;
  834.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  835.     error("UDIWrite() failed in udi_store_regisetrs");
  836.  
  837.  
  838. /* Protected Special Registers */ /* VAB through TMR */
  839.  
  840.   From = (UDIUInt32 *)®isters[4 * SR_REGNUM(0)];
  841.   To.Space = UDI29KSpecialRegs;
  842.   To.Offset = 0;
  843.   Count = 10;
  844.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  845.     error("UDIWrite() failed in udi_store_regisetrs");
  846.  
  847. /* PC0, PC1, PC2 possibly as shadow registers */
  848.  
  849.   From = (UDIUInt32 *)®isters[4 * SR_REGNUM(10)];
  850.   To.Space = UDI29KSpecialRegs;
  851.   Count = 3;
  852.   if (USE_SHADOW_PC) 
  853.     To.Offset = 20;                /* SPC0 */
  854.   else 
  855.     To.Offset = 10;                /* PC0 */
  856.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  857.     error("UDIWrite() failed in udi_store_regisetrs");
  858.  
  859. /* PC1 via UDI29KPC */
  860.  
  861.   From = (UDIUInt32 *)®isters[4 * PC_REGNUM];
  862.   To.Space = UDI29KPC;
  863.   To.Offset = 0;                /* PC1 */
  864.   Count = 1;
  865.   if (UDIWrite (From, To, Count, Size, &CountDone, HostEndian))
  866.     error ("UDIWrite() failed in udi_store_regisetrs");
  867.  
  868.   /* LRU and MMU */
  869.  
  870.   From = (UDIUInt32 *)®isters[4 * SR_REGNUM(13)];
  871.   To.Space = UDI29KSpecialRegs;
  872.   To.Offset = 13;
  873.   Count = 2;
  874.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  875.     error("UDIWrite() failed in udi_store_regisetrs");
  876.  
  877. /* Unprotected Special Registers */ 
  878.  
  879.   From = (UDIUInt32 *)®isters[4 * SR_REGNUM(128)];
  880.   To.Space = UDI29KSpecialRegs;
  881.   To.Offset = 128;
  882.   Count = 135-128 +1;
  883.   if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  884.     error("UDIWrite() failed in udi_store_regisetrs");
  885.  
  886.   registers_changed ();
  887. }
  888.  
  889. /****************************************************** UDI_PREPARE_TO_STORE */
  890. /* Get ready to modify the registers array.  On machines which store
  891.    individual registers, this doesn't need to do anything.  On machines
  892.    which store all the registers in one fell swoop, this makes sure
  893.    that registers contains all the registers from the program being
  894.    debugged.  */
  895.  
  896. static void
  897. udi_prepare_to_store ()
  898. {
  899.   /* Do nothing, since we can store individual regs */
  900. }
  901.  
  902. /********************************************************** TRANSLATE_ADDR */
  903. static CORE_ADDR
  904. translate_addr(addr)
  905. CORE_ADDR addr;
  906. {
  907. #if defined(ULTRA3) && defined(KERNEL_DEBUGGING)
  908.         /* Check for a virtual address in the kernel */
  909.         /* Assume physical address of ublock is in  paddr_u register */
  910.     /* FIXME: doesn't work for user virtual addresses */
  911.         if (addr >= UVADDR) {
  912.                 /* PADDR_U register holds the physical address of the ublock */
  913.                 CORE_ADDR i = (CORE_ADDR)read_register(PADDR_U_REGNUM);
  914.                 return(i + addr - (CORE_ADDR)UVADDR);
  915.         } else {
  916.             return(addr);
  917.         }
  918. #else
  919.         return(addr);
  920. #endif
  921. }
  922. /************************************************* UDI_XFER_INFERIOR_MEMORY */
  923. /* FIXME!  Merge these two.  */
  924. static int
  925. udi_xfer_inferior_memory (memaddr, myaddr, len, write)
  926.      CORE_ADDR memaddr;
  927.      char *myaddr;
  928.      int len;
  929.      int write;
  930. {
  931.  
  932.   memaddr = translate_addr(memaddr);
  933.  
  934.   if (write)
  935.     return udi_write_inferior_memory (memaddr, myaddr, len);
  936.   else
  937.     return udi_read_inferior_memory (memaddr, myaddr, len);
  938. }
  939.  
  940. /********************************************************** UDI_FILES_INFO */
  941. static void
  942. udi_files_info ()
  943. {
  944.   printf_unfiltered ("\tAttached to UDI socket to %s", udi_config_id);
  945.   if (prog_name != NULL)
  946.     printf_unfiltered ("and running program %s", prog_name);
  947.   printf_unfiltered (".\n");
  948. }
  949.  
  950. /**************************************************** UDI_INSERT_BREAKPOINT */
  951. static int
  952. udi_insert_breakpoint (addr, contents_cache)
  953.      CORE_ADDR addr;
  954.      char *contents_cache;
  955. {
  956.   int cnt;
  957.   UDIError err;
  958.  
  959.   for (cnt = 0; cnt < BKPT_TABLE_SIZE; cnt++)
  960.     if (bkpt_table[cnt].Type == 0) /* Find first free slot */
  961.       break;
  962.  
  963.   if(cnt >= BKPT_TABLE_SIZE)
  964.     error("Too many breakpoints set");
  965.  
  966.   bkpt_table[cnt].Addr.Offset = addr;
  967.   bkpt_table[cnt].Addr.Space  = UDI29KIRAMSpace;
  968.   bkpt_table[cnt].PassCount = 1;
  969.   bkpt_table[cnt].Type = UDIBreakFlagExecute;
  970.   
  971.   err = UDISetBreakpoint(bkpt_table[cnt].Addr,
  972.              bkpt_table[cnt].PassCount,
  973.              bkpt_table[cnt].Type,
  974.              &bkpt_table[cnt].BreakId);
  975.  
  976.   if (err == 0) return 0;        /* Success */
  977.  
  978.   bkpt_table[cnt].Type = 0;
  979.   error("UDISetBreakpoint returned error code %d\n", err);
  980. }
  981.  
  982. /**************************************************** UDI_REMOVE_BREAKPOINT */
  983. static int
  984. udi_remove_breakpoint (addr, contents_cache)
  985.      CORE_ADDR addr;
  986.      char *contents_cache;
  987. {
  988.   int cnt;
  989.   UDIError err;
  990.  
  991.   for (cnt = 0; cnt < BKPT_TABLE_SIZE; cnt++)
  992.     if (bkpt_table[cnt].Addr.Offset == addr) /* Find matching breakpoint */
  993.       break;
  994.  
  995.   if(cnt >= BKPT_TABLE_SIZE)
  996.     error("Can't find breakpoint in table");
  997.  
  998.   bkpt_table[cnt].Type = 0;
  999.  
  1000.   err = UDIClearBreakpoint(bkpt_table[cnt].BreakId);
  1001.   if (err == 0) return 0;    /* Success */
  1002.  
  1003.   error("UDIClearBreakpoint returned error code %d\n", err);
  1004. }
  1005.  
  1006. static void
  1007. udi_kill(arg,from_tty)
  1008.      char *arg;
  1009.      int from_tty;
  1010. {
  1011.  
  1012. #if 0
  1013. /*
  1014. UDIStop does not really work as advertised.  It causes the TIP to close it's
  1015. connection, which usually results in GDB dying with a SIGPIPE.  For now, we
  1016. just invoke udi_close, which seems to get things right.
  1017. */
  1018.   UDIStop();
  1019.  
  1020.   udi_session_id = -1;
  1021.   inferior_pid = 0;
  1022.  
  1023.   if (from_tty)
  1024.     printf_unfiltered("Target has been stopped.");
  1025. #endif /* 0 */
  1026. #if 0
  1027.   udi_close(0);
  1028.   pop_target();
  1029. #endif /* 0 */
  1030.  
  1031.   /* Keep the target around, e.g. so "run" can do the right thing when
  1032.      we are already debugging something.  */
  1033.  
  1034.   if (UDIDisconnect (udi_session_id, UDITerminateSession))
  1035.     {
  1036.       warning ("UDIDisconnect() failed");
  1037.     }
  1038.  
  1039.   /* Do not try to close udi_session_id again, later in the program.  */
  1040.   udi_session_id = -1;
  1041.   inferior_pid = 0;
  1042. }
  1043.  
  1044. /* 
  1045.    Load a program into the target.  Args are: `program {options}'.  The options
  1046.    are used to control loading of the program, and are NOT passed onto the
  1047.    loaded code as arguments.  (You need to use the `run' command to do that.)
  1048.  
  1049.    The options are:
  1050.          -ms %d    Set mem stack size to %d
  1051.         -rs %d    Set regular stack size to %d
  1052.         -i    send init info (default)
  1053.         -noi    don't send init info
  1054.         -[tT]      Load Text section
  1055.         -[dD]    Load Data section
  1056.         -[bB]    Load BSS section
  1057.         -[lL]    Load Lit section
  1058.   */
  1059.  
  1060. static void
  1061. download(load_arg_string, from_tty)
  1062.      char *load_arg_string;
  1063.      int from_tty;
  1064. {
  1065. #define DEFAULT_MEM_STACK_SIZE         0x6000
  1066. #define DEFAULT_REG_STACK_SIZE         0x2000
  1067.  
  1068.   char *token;
  1069.   char *filename;
  1070.   asection *section;
  1071.   bfd *pbfd;
  1072.   UDIError err;
  1073.   int load_text = 1, load_data = 1, load_bss = 1, load_lit = 1;
  1074.  
  1075.   address_ranges[0].Space = UDI29KIRAMSpace;
  1076.   address_ranges[0].Offset = 0xffffffff;
  1077.   address_ranges[0].Size = 0;
  1078.  
  1079.   address_ranges[1].Space = UDI29KDRAMSpace;
  1080.   address_ranges[1].Offset = 0xffffffff;
  1081.   address_ranges[1].Size = 0;
  1082.  
  1083.   stack_sizes[0] = DEFAULT_REG_STACK_SIZE;
  1084.   stack_sizes[1] = DEFAULT_MEM_STACK_SIZE;
  1085.  
  1086.   dont_repeat ();
  1087.  
  1088.   filename = strtok(load_arg_string, " \t");
  1089.   if (!filename)
  1090.     error ("Must specify at least a file name with the load command");
  1091.  
  1092.   filename = tilde_expand (filename);
  1093.   make_cleanup (free, filename);
  1094.  
  1095.   while (token = strtok (NULL, " \t"))
  1096.     {
  1097.       if (token[0] == '-')
  1098.     {
  1099.       token++;
  1100.  
  1101.       if (STREQ (token, "ms"))
  1102.         stack_sizes[1] = atol (strtok (NULL, " \t"));
  1103.       else if (STREQ (token, "rs"))
  1104.         stack_sizes[0] = atol (strtok (NULL, " \t"));
  1105.       else
  1106.         {
  1107.           load_text = load_data = load_bss = load_lit = 0;
  1108.  
  1109.           while (*token)
  1110.         {
  1111.           switch (*token++)
  1112.             {
  1113.             case 't':
  1114.             case 'T':
  1115.               load_text = 1;
  1116.               break;
  1117.             case 'd':
  1118.             case 'D':
  1119.               load_data = 1;
  1120.               break;
  1121.             case 'b':
  1122.             case 'B':
  1123.               load_bss = 1;
  1124.               break;
  1125.             case 'l':
  1126.             case 'L':
  1127.               load_lit = 1;
  1128.               break;
  1129.             default:
  1130.               error ("Unknown UDI load option -%s", token-1);
  1131.             }
  1132.         }
  1133.         }
  1134.     }
  1135.     }
  1136.  
  1137.   pbfd = bfd_openr (filename, gnutarget);
  1138.  
  1139.   if (!pbfd) 
  1140.     perror_with_name (filename);
  1141.   
  1142.   make_cleanup (bfd_close, pbfd);
  1143.  
  1144.   QUIT;
  1145.   immediate_quit++;
  1146.  
  1147.   if (!bfd_check_format (pbfd, bfd_object)) 
  1148.     error ("It doesn't seem to be an object file");
  1149.   
  1150.   for (section = pbfd->sections; section; section = section->next) 
  1151.     {
  1152.       if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
  1153.     {
  1154.       UDIResource To;
  1155.       UDICount Count;
  1156.       unsigned long section_size, section_end;
  1157.       const char *section_name;
  1158.  
  1159.       section_name = bfd_get_section_name (pbfd, section);
  1160.       if (STREQ (section_name, ".text") && !load_text)
  1161.         continue;
  1162.       else if (STREQ (section_name, ".data") && !load_data)
  1163.         continue;
  1164.       else if (STREQ (section_name, ".bss") && !load_bss)
  1165.         continue;
  1166.       else if (STREQ (section_name, ".lit") && !load_lit)
  1167.         continue;
  1168.  
  1169.       To.Offset = bfd_get_section_vma (pbfd, section);
  1170.       section_size = bfd_section_size (pbfd, section);
  1171.       section_end = To.Offset + section_size;
  1172.  
  1173.       if (section_size == 0)
  1174.         /* This is needed at least in the BSS case, where the code
  1175.            below starts writing before it even checks the size.  */
  1176.         continue;
  1177.  
  1178.       printf_unfiltered("[Loading section %s at %x (%d bytes)]\n",
  1179.          section_name,
  1180.          To.Offset,
  1181.          section_size);
  1182.  
  1183.       if (bfd_get_section_flags (pbfd, section) & SEC_CODE)
  1184.         {
  1185.           To.Space = UDI29KIRAMSpace;
  1186.  
  1187.           address_ranges[0].Offset = min (address_ranges[0].Offset,
  1188.                           To.Offset);
  1189.           address_ranges[0].Size = max (address_ranges[0].Size,
  1190.                         section_end
  1191.                         - address_ranges[0].Offset);
  1192.         }
  1193.       else
  1194.         {
  1195.           To.Space = UDI29KDRAMSpace;
  1196.  
  1197.           address_ranges[1].Offset = min (address_ranges[1].Offset,
  1198.                           To.Offset);
  1199.           address_ranges[1].Size = max (address_ranges[1].Size,
  1200.                         section_end
  1201.                         - address_ranges[1].Offset);
  1202.         }
  1203.  
  1204.       if (bfd_get_section_flags (pbfd, section) & SEC_LOAD) /* Text, data or lit */
  1205.         {
  1206.           file_ptr fptr;
  1207.  
  1208.           fptr = 0;
  1209.  
  1210.           while (section_size > 0)
  1211.         {
  1212.           char buffer[1024];
  1213.  
  1214.           Count = min (section_size, 1024);
  1215.  
  1216.           bfd_get_section_contents (pbfd, section, buffer, fptr,
  1217.                         Count);
  1218.  
  1219.           err = UDIWrite ((UDIHostMemPtr)buffer, /* From */
  1220.                   To,            /* To */
  1221.                   Count,        /* Count */
  1222.                   (UDISizeT)1,        /* Size */
  1223.                   &Count,        /* CountDone */
  1224.                   (UDIBool)0);        /* HostEndian */
  1225.           if (err)
  1226.             error ("UDIWrite failed, error = %d", err);
  1227.  
  1228.           To.Offset += Count;
  1229.           fptr += Count;
  1230.           section_size -= Count;
  1231.         }
  1232.         }
  1233.       else            /* BSS */
  1234.         {
  1235.           UDIResource From;
  1236.           unsigned long zero = 0;
  1237.  
  1238.           /* Write a zero byte at the vma */
  1239.           /* FIXME: Broken for sections of 1-3 bytes (we test for
  1240.          zero above).  */
  1241.           err = UDIWrite ((UDIHostMemPtr)&zero,    /* From */
  1242.                   To,            /* To */
  1243.                   (UDICount)1,        /* Count */
  1244.                   (UDISizeT)4,        /* Size */
  1245.                   &Count,            /* CountDone */
  1246.                   (UDIBool)0);        /* HostEndian */
  1247.           if (err)
  1248.         error ("UDIWrite failed, error = %d", err);
  1249.  
  1250.           From = To;
  1251.           To.Offset+=4;
  1252.  
  1253.           /* Now, duplicate it for the length of the BSS */
  1254.           err = UDICopy (From,            /* From */
  1255.                  To,            /* To */
  1256.                  (UDICount)(section_size/4 - 1), /* Count */
  1257.                  (UDISizeT)4,        /* Size */
  1258.                  &Count,            /* CountDone */
  1259.                  (UDIBool)1);        /* Direction */
  1260.           if (err)
  1261.         {
  1262.           char message[100];
  1263.           int xerr;
  1264.  
  1265.           xerr = UDIGetErrorMsg(err, 100, message, &Count);
  1266.           if (!xerr)
  1267.             fprintf_unfiltered (gdb_stderr, "Error is %s\n", message);
  1268.           else
  1269.             fprintf_unfiltered (gdb_stderr, "xerr is %d\n", xerr);
  1270.           error ("UDICopy failed, error = %d", err);
  1271.         }
  1272.         }
  1273.  
  1274.     }
  1275.     }
  1276.  
  1277.   entry.Space = UDI29KIRAMSpace;
  1278.   entry.Offset = bfd_get_start_address (pbfd);
  1279.   
  1280.   immediate_quit--;
  1281. }
  1282.  
  1283. /* User interface to download an image into the remote target.  See download()
  1284.  * for details on args.
  1285.  */
  1286.  
  1287. static void
  1288. udi_load(args, from_tty)
  1289.      char *args;
  1290.      int from_tty;
  1291. {
  1292.   download (args, from_tty);
  1293.  
  1294.   symbol_file_add (strtok (args, " \t"), from_tty, 0, 0, 0, 0);
  1295. }
  1296.  
  1297. /*************************************************** UDI_WRITE_INFERIOR_MEMORY
  1298. ** Copy LEN bytes of data from debugger memory at MYADDR
  1299.    to inferior's memory at MEMADDR.  Returns number of bytes written.  */
  1300. static int
  1301. udi_write_inferior_memory (memaddr, myaddr, len)
  1302.      CORE_ADDR memaddr;
  1303.      char *myaddr;
  1304.      int len;
  1305. {
  1306.   int        nwritten = 0;
  1307.   UDIUInt32    *From;
  1308.   UDIResource    To;
  1309.   UDICount    Count;
  1310.   UDISizeT    Size = 1;
  1311.   UDICount    CountDone = 0;
  1312.   UDIBool    HostEndian = 0;
  1313.   
  1314.   To.Space = udi_memory_space(memaddr);
  1315.   From = (UDIUInt32*)myaddr;
  1316.  
  1317.   while (nwritten < len)
  1318.   {    Count = len - nwritten;
  1319.     if (Count > MAXDATA) Count = MAXDATA;
  1320.       To.Offset = memaddr + nwritten;
  1321.         if(UDIWrite(From, To, Count, Size, &CountDone, HostEndian))
  1322.         {  error("UDIWrite() failed in udi_write_inferrior_memory");
  1323.        break;    
  1324.     }
  1325.     else
  1326.       {  nwritten += CountDone;
  1327.        From += CountDone;
  1328.     }
  1329.   }
  1330.   return(nwritten);
  1331. }
  1332.  
  1333. /**************************************************** UDI_READ_INFERIOR_MEMORY
  1334. ** Read LEN bytes from inferior memory at MEMADDR.  Put the result
  1335.    at debugger address MYADDR.  Returns number of bytes read.  */
  1336. static int
  1337. udi_read_inferior_memory(memaddr, myaddr, len)
  1338.      CORE_ADDR memaddr;
  1339.      char *myaddr;
  1340.      int len;
  1341. {
  1342.   int        nread = 0;
  1343.   UDIResource    From;
  1344.   UDIUInt32    *To;
  1345.   UDICount    Count;
  1346.   UDISizeT    Size = 1;
  1347.   UDICount    CountDone = 0;
  1348.   UDIBool    HostEndian = 0;
  1349.   UDIError    err;
  1350.   
  1351.   From.Space = udi_memory_space(memaddr);    
  1352.   To = (UDIUInt32*)myaddr;
  1353.  
  1354.   while (nread < len)
  1355.   {    Count = len - nread;
  1356.     if (Count > MAXDATA) Count = MAXDATA;
  1357.       From.Offset = memaddr + nread;
  1358.         if(err = UDIRead(From, To, Count, Size, &CountDone, HostEndian))
  1359.         {  error("UDIRead() failed in udi_read_inferrior_memory");
  1360.        break;    
  1361.     }
  1362.     else
  1363.       {  nread += CountDone;
  1364.        To += CountDone;
  1365.     }
  1366.   }
  1367.   return(nread);
  1368. }
  1369.  
  1370. /********************************************************************* WARNING
  1371. */
  1372. udi_warning(num)
  1373. int    num;
  1374. {
  1375.     error ("ERROR while loading program into remote TIP: $d\n", num);
  1376. }
  1377.  
  1378.  
  1379. /*****************************************************************************/ 
  1380. /* Fetch a single register indicatated by 'regno'. 
  1381.  * Returns 0/-1 on success/failure.  
  1382.  */
  1383. static void
  1384. fetch_register (regno)
  1385.      int regno;
  1386. {
  1387.   UDIResource    From;
  1388.   UDIUInt32    To;
  1389.   UDICount    Count = 1;
  1390.   UDISizeT    Size = 4;
  1391.   UDICount    CountDone;
  1392.   UDIBool    HostEndian = 0;
  1393.   UDIError    err;
  1394.   int          result;
  1395.  
  1396.   if (regno == GR1_REGNUM)
  1397.     {
  1398.       From.Space = UDI29KGlobalRegs;
  1399.       From.Offset = 1;
  1400.     }
  1401.   else if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
  1402.     {
  1403.       From.Space = UDI29KGlobalRegs;
  1404.       From.Offset = (regno - GR96_REGNUM) + 96;;
  1405.     }
  1406.  
  1407. #if defined(GR64_REGNUM)
  1408.  
  1409.   else if (regno >= GR64_REGNUM && regno < GR64_REGNUM + 32 )
  1410.     {
  1411.       From.Space = UDI29KGlobalRegs;
  1412.       From.Offset = (regno - GR64_REGNUM) + 64;
  1413.     }
  1414.  
  1415. #endif    /* GR64_REGNUM */
  1416.  
  1417.   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
  1418.     {
  1419.       From.Space = UDI29KLocalRegs;
  1420.       From.Offset = (regno - LR0_REGNUM);
  1421.     }
  1422.   else if (regno>=FPE_REGNUM && regno<=EXO_REGNUM)  
  1423.     {
  1424.       int val = -1;
  1425.       supply_register(160 + (regno - FPE_REGNUM),(char *) &val);
  1426.       return;        /* Pretend Success */
  1427.     }
  1428.   else 
  1429.     {
  1430.       From.Space = UDI29KSpecialRegs;
  1431.       From.Offset = regnum_to_srnum(regno); 
  1432.     }
  1433.  
  1434.   if (err = UDIRead(From, &To, Count, Size, &CountDone, HostEndian))
  1435.     error("UDIRead() failed in udi_fetch_registers");
  1436.  
  1437.   supply_register(regno, (char *) &To);
  1438.  
  1439.   if (remote_debug)
  1440.     printf_unfiltered("Fetching register %s = 0x%x\n", reg_names[regno], To);
  1441. }
  1442. /*****************************************************************************/ 
  1443. /* Store a single register indicated by 'regno'. 
  1444.  * Returns 0/-1 on success/failure.  
  1445.  */
  1446. static int
  1447. store_register (regno)
  1448.      int regno;
  1449. {
  1450.   int          result;
  1451.   UDIUInt32    From;
  1452.   UDIResource    To;
  1453.   UDICount    Count = 1;
  1454.   UDISizeT    Size = 4;
  1455.   UDICount    CountDone;
  1456.   UDIBool    HostEndian = 0;
  1457.  
  1458.   From =  read_register (regno);    /* get data value */
  1459.  
  1460.   if (remote_debug)
  1461.     printf_unfiltered("Storing register %s = 0x%x\n", reg_names[regno], From);
  1462.  
  1463.   if (regno == GR1_REGNUM)
  1464.     {
  1465.       To.Space = UDI29KGlobalRegs;
  1466.       To.Offset = 1;
  1467.       result = UDIWrite(&From, To, Count, Size, &CountDone, HostEndian);
  1468.       /* Setting GR1 changes the numbers of all the locals, so invalidate the 
  1469.        * register cache.  Do this *after* calling read_register, because we want 
  1470.        * read_register to return the value that write_register has just stuffed 
  1471.        * into the registers array, not the value of the register fetched from 
  1472.        * the inferior.  
  1473.        */
  1474.       registers_changed ();
  1475.     }
  1476. #if defined(GR64_REGNUM)
  1477.   else if (regno >= GR64_REGNUM && regno < GR64_REGNUM + 32 )
  1478.     {
  1479.       To.Space = UDI29KGlobalRegs;
  1480.       To.Offset = (regno - GR64_REGNUM) + 64;
  1481.       result = UDIWrite(&From, To, Count, Size, &CountDone, HostEndian);
  1482.     }
  1483. #endif    /* GR64_REGNUM */
  1484.   else if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
  1485.     {
  1486.       To.Space = UDI29KGlobalRegs;
  1487.       To.Offset = (regno - GR96_REGNUM) + 96;
  1488.       result = UDIWrite(&From, To, Count, Size, &CountDone, HostEndian);
  1489.     }
  1490.   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
  1491.     {
  1492.       To.Space = UDI29KLocalRegs;
  1493.       To.Offset = (regno - LR0_REGNUM);
  1494.       result = UDIWrite(&From, To, Count, Size, &CountDone, HostEndian);
  1495.     }
  1496.   else if (regno >= FPE_REGNUM && regno <= EXO_REGNUM)  
  1497.     return 0;        /* Pretend Success */
  1498.   else if (regno == PC_REGNUM)
  1499.     {    
  1500.       /* PC1 via UDI29KPC */
  1501.  
  1502.       To.Space = UDI29KPC;
  1503.       To.Offset = 0;        /* PC1 */
  1504.       result = UDIWrite (&From, To, Count, Size, &CountDone, HostEndian);
  1505.  
  1506.       /* Writing to this loc actually changes the values of pc0 & pc1 */
  1507.  
  1508.       register_valid[PC_REGNUM] = 0; /* pc1 */
  1509.       register_valid[NPC_REGNUM] = 0; /* pc0 */
  1510.     }
  1511.   else     /* An unprotected or protected special register */
  1512.     {
  1513.       To.Space = UDI29KSpecialRegs;
  1514.       To.Offset = regnum_to_srnum(regno); 
  1515.       result = UDIWrite(&From, To, Count, Size, &CountDone, HostEndian);
  1516.     }
  1517.  
  1518.   if (result != 0)
  1519.     error("UDIWrite() failed in store_registers");
  1520.  
  1521.   return 0;
  1522. }
  1523. /********************************************************** REGNUM_TO_SRNUM */
  1524. /* 
  1525.  * Convert a gdb special register number to a 29000 special register number.
  1526.  */
  1527. static int
  1528. regnum_to_srnum(regno)
  1529. int    regno;
  1530. {
  1531.     switch(regno) {
  1532.         case VAB_REGNUM: return(0); 
  1533.         case OPS_REGNUM: return(1); 
  1534.         case CPS_REGNUM: return(2); 
  1535.         case CFG_REGNUM: return(3); 
  1536.         case CHA_REGNUM: return(4); 
  1537.         case CHD_REGNUM: return(5); 
  1538.         case CHC_REGNUM: return(6); 
  1539.         case RBP_REGNUM: return(7); 
  1540.         case TMC_REGNUM: return(8); 
  1541.         case TMR_REGNUM: return(9); 
  1542.         case NPC_REGNUM: return(USE_SHADOW_PC ? (20) : (10));
  1543.         case PC_REGNUM:  return(USE_SHADOW_PC ? (21) : (11));
  1544.         case PC2_REGNUM: return(USE_SHADOW_PC ? (22) : (12));
  1545.         case MMU_REGNUM: return(13); 
  1546.         case LRU_REGNUM: return(14); 
  1547.         case IPC_REGNUM: return(128); 
  1548.         case IPA_REGNUM: return(129); 
  1549.         case IPB_REGNUM: return(130); 
  1550.         case Q_REGNUM:   return(131); 
  1551.         case ALU_REGNUM: return(132); 
  1552.         case BP_REGNUM:  return(133); 
  1553.         case FC_REGNUM:  return(134); 
  1554.         case CR_REGNUM:  return(135); 
  1555.         case FPE_REGNUM: return(160); 
  1556.         case INTE_REGNUM: return(161); 
  1557.         case FPS_REGNUM: return(162); 
  1558.         case EXO_REGNUM:return(164); 
  1559.         default:
  1560.             return(255);    /* Failure ? */
  1561.     }
  1562. }
  1563. /****************************************************************************/
  1564. /*
  1565.  * Determine the Target memory space qualifier based on the addr. 
  1566.  * FIXME: Can't distinguis I_ROM/D_ROM.  
  1567.  * FIXME: Doesn't know anything about I_CACHE/D_CACHE.
  1568.  */
  1569. static CPUSpace
  1570. udi_memory_space(addr)
  1571. CORE_ADDR    addr;
  1572. {
  1573.     UDIUInt32 tstart = IMemStart;
  1574.     UDIUInt32 tend   = tstart + IMemSize;  
  1575.     UDIUInt32 dstart = DMemStart;
  1576.     UDIUInt32 dend   = tstart + DMemSize;  
  1577.     UDIUInt32 rstart = RMemStart;
  1578.     UDIUInt32 rend   = tstart + RMemSize;  
  1579.  
  1580.     if (((UDIUInt32)addr >= tstart) && ((UDIUInt32)addr < tend)) { 
  1581.         return UDI29KIRAMSpace;
  1582.     } else if (((UDIUInt32)addr >= dstart) && ((UDIUInt32)addr < dend)) { 
  1583.         return UDI29KDRAMSpace;
  1584.     } else if (((UDIUInt32)addr >= rstart) && ((UDIUInt32)addr < rend)) {
  1585.         /* FIXME: how do we determine between D_ROM and I_ROM */
  1586.         return UDI29KIROMSpace;
  1587.     } else    /* FIXME: what do me do now? */
  1588.         return UDI29KDRAMSpace;    /* Hmmm! */
  1589. }
  1590. /*********************************************************************** STUBS
  1591. */
  1592.  
  1593. void  convert16() {;}
  1594. void  convert32() {;}
  1595. GDB_FILE * EchoFile = 0;        /* used for debugging */
  1596. int   QuietMode = 0;        /* used for debugging */
  1597.  
  1598. #ifdef NO_HIF_SUPPORT
  1599. service_HIF(msg)
  1600.      union msg_t *msg;
  1601. {
  1602.   return(0);            /* Emulate a failure */
  1603. }
  1604. #endif
  1605.  
  1606. /* Target_ops vector.  Not static because there does not seem to be
  1607.    any portable way to do a forward declaration of a static variable.
  1608.    The RS/6000 doesn't like "extern" followed by "static"; SunOS
  1609.    /bin/cc doesn't like "static" twice.  */
  1610.  
  1611. struct target_ops udi_ops = {
  1612.         "udi",
  1613.     "Remote UDI connected TIP",
  1614.     "Remote debug an AMD 29k using UDI socket connection to TIP process.\n\
  1615. Arguments are\n\
  1616. `configuration-id AF_INET hostname port-number'\n\
  1617.     To connect via the network, where hostname and port-number specify the\n\
  1618.     host and port where you can connect via UDI.\n\
  1619.     configuration-id is unused.\n\
  1620. \n\
  1621. `configuration-id AF_UNIX socket-name tip-program'\n\
  1622.     To connect using a local connection to the \"tip.exe\" program which is\n\
  1623.     supplied by AMD.  If socket-name specifies an AF_UNIX socket then the\n\
  1624.     tip program must already be started; connect to it using that socket.\n\
  1625.     If not, start up tip-program, which should be the name of the tip\n\
  1626.     program.  If appropriate, the PATH environment variable is searched.\n\
  1627.     configuration-id is unused.\n\
  1628. \n\
  1629. `configuration-id'\n\
  1630.     Look up the configuration in ./udi_soc or /etc/udi_soc, which\n\
  1631.     are files containing lines in the above formats.  configuration-id is\n\
  1632.     used to pick which line of the file to use.",
  1633.         udi_open,
  1634.     udi_close,
  1635.         udi_attach,
  1636.     udi_detach,
  1637.     udi_resume,
  1638.     udi_wait,
  1639.         udi_fetch_registers,
  1640.     udi_store_registers,
  1641.         udi_prepare_to_store,
  1642.         udi_xfer_inferior_memory,
  1643.         udi_files_info,
  1644.         udi_insert_breakpoint,
  1645.     udi_remove_breakpoint,
  1646.         0,            /* termial_init */
  1647.     0,            /* terminal_inferior */
  1648.     0,            /* terminal_ours_for_output */
  1649.     0,            /* terminal_ours */
  1650.     0,            /* terminal_info */
  1651.         udi_kill,                 /* FIXME, kill */
  1652.         udi_load,
  1653.         0,                      /* lookup_symbol */
  1654.         udi_create_inferior,
  1655.         udi_mourn,        /* mourn_inferior FIXME */
  1656.     0,            /* can_run */
  1657.     0,            /* notice_signals */
  1658.         process_stratum,
  1659.     0,            /* next */
  1660.         1,            /* has_all_memory */
  1661.     1,            /* has_memory */
  1662.     1,            /* has_stack */
  1663.     1,            /* has_registers */
  1664.     1,            /* has_execution */
  1665.     0,            /* sections */
  1666.     0,            /* sections_end */
  1667.     OPS_MAGIC,        /* Always the last thing */
  1668. };
  1669.  
  1670. void
  1671. _initialize_remote_udi ()
  1672. {
  1673.   add_target (&udi_ops);
  1674. }
  1675.