home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / remote-sim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  11.2 KB  |  434 lines

  1. /* Generic remote debugging interface for simulators.
  2.    Copyright 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.    Steve Chamberlain (sac@cygnus.com).
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "inferior.h"
  24. #include "wait.h"
  25. #include "value.h"
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <fcntl.h>
  29. #include <signal.h>
  30. #include <setjmp.h>
  31. #include <errno.h>
  32. #include "terminal.h"
  33. #include "target.h"
  34. #include "gdbcore.h"
  35. #include "remote-sim.h"
  36. #include "remote-utils.h"
  37.  
  38. /* Naming convention:
  39.  
  40.    sim_* are the interface to the simulator (see remote-sim.h).
  41.  
  42.    gdbsim_* are stuff which is internal to gdb.  */
  43.  
  44. /* Forward data declarations */
  45. extern struct target_ops gdbsim_ops;
  46.  
  47. static int program_loaded = 0;
  48.  
  49. static void
  50. dump_mem (buf, len)
  51.      char *buf;
  52.      int len;
  53. {
  54.   if (len <= 8)
  55.     {
  56.       if (len == 8 || len == 4)
  57.     {
  58.       long l[2];
  59.       memcpy (l, buf, len);
  60.       printf_filtered ("\t0x%x", l[0]);
  61.       printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
  62.     }
  63.       else
  64.     {
  65.       int i;
  66.       printf_filtered ("\t");
  67.       for (i = 0; i < len; i++)
  68.         printf_filtered ("0x%x ", buf[i]);
  69.       printf_filtered ("\n");
  70.     }
  71.     }
  72. }
  73.  
  74. static void
  75. gdbsim_fetch_register (regno)
  76. int regno;
  77. {
  78.   if (regno == -1) 
  79.     {
  80.       for (regno = 0; regno < NUM_REGS; regno++)
  81.     gdbsim_fetch_register (regno);
  82.     }
  83.   else
  84.     {
  85.       char buf[MAX_REGISTER_RAW_SIZE];
  86.  
  87.       sim_fetch_register (regno, buf);
  88.       supply_register (regno, buf);
  89.       if (sr_get_debug ())
  90.     {
  91.       printf_filtered ("gdbsim_fetch_register: %d", regno);
  92.       /* FIXME: We could print something more intelligible.  */
  93.       dump_mem (buf, REGISTER_RAW_SIZE (regno));
  94.     }
  95.     }
  96. }
  97.  
  98. static void
  99. gdbsim_store_register (regno)
  100. int regno;
  101. {
  102.   if (regno  == -1) 
  103.     {
  104.       for (regno = 0; regno < NUM_REGS; regno++)
  105.     gdbsim_store_register (regno);
  106.     }
  107.   else
  108.     {
  109.       /* FIXME: Until read_register() returns LONGEST, we have this.  */
  110.       char tmp[MAX_REGISTER_RAW_SIZE];
  111.       read_register_gen (regno, tmp);
  112.       sim_store_register (regno, tmp);
  113.       if (sr_get_debug ())
  114.     {
  115.       printf_filtered ("gdbsim_store_register: %d", regno);
  116.       /* FIXME: We could print something more intelligible.  */
  117.       dump_mem (tmp, REGISTER_RAW_SIZE (regno));
  118.     }
  119.     }
  120. }
  121.  
  122. /* Kill the running program.  This may involve closing any open files
  123.    and releasing other resources acquired by the simulated program.  */
  124.  
  125. static void
  126. gdbsim_kill ()
  127. {
  128.   if (sr_get_debug ())
  129.     printf_filtered ("gdbsim_kill\n");
  130.  
  131.   sim_kill ();    /* close fd's, remove mappings */
  132.   inferior_pid = 0;
  133. }
  134.  
  135. /* Load an executable file into the target process.  This is expected to
  136.    not only bring new code into the target process, but also to update
  137.    GDB's symbol tables to match.  */
  138.  
  139. static void
  140. gdbsim_load (prog, fromtty)
  141.      char *prog;
  142.      int fromtty;
  143. {
  144.   if (sr_get_debug ())
  145.     printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
  146.  
  147.   inferior_pid = 0;
  148.  
  149.   /* This must be done before calling gr_load_image.  */
  150.   program_loaded = 1;
  151.  
  152.   if (sim_load (prog, fromtty) != 0)
  153.     gr_load_image (prog, fromtty);
  154. }
  155.  
  156.  
  157. /* Start an inferior process and set inferior_pid to its pid.
  158.    EXEC_FILE is the file to run.
  159.    ALLARGS is a string containing the arguments to the program.
  160.    ENV is the environment vector to pass.  Errors reported with error().
  161.    On VxWorks and various standalone systems, we ignore exec_file.  */
  162. /* This is called not only when we first attach, but also when the
  163.    user types "run" after having attached.  */
  164.  
  165. static void
  166. gdbsim_create_inferior (exec_file, args, env)
  167.      char *exec_file;
  168.      char *args;
  169.      char **env;
  170. {
  171.   int len;
  172.   char *arg_buf,**argv;
  173.   CORE_ADDR entry_pt;
  174.  
  175.   if (! program_loaded)
  176.     error ("No program loaded.");
  177.  
  178.   if (sr_get_debug ())
  179.     printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
  180.       exec_file, args);
  181.  
  182.   if (exec_file == 0 || exec_bfd == 0)
  183.    error ("No exec file specified.");
  184.  
  185.   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
  186.  
  187.   gdbsim_kill (NULL, NULL);     
  188.   remove_breakpoints ();
  189.   init_wait_for_inferior ();
  190.  
  191.   len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
  192.   arg_buf = (char *) alloca (len);
  193.   arg_buf[0] = '\0';
  194.   strcat (arg_buf, exec_file);
  195.   strcat (arg_buf, " ");
  196.   strcat (arg_buf, args);
  197.   argv = buildargv (arg_buf);
  198.   make_cleanup (freeargv, (char *) argv);
  199.   sim_create_inferior (entry_pt, argv, env);
  200.  
  201.   inferior_pid = 42;
  202.   insert_breakpoints ();    /* Needed to get correct instruction in cache */
  203.   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
  204. }
  205.  
  206. /* The open routine takes the rest of the parameters from the command,
  207.    and (if successful) pushes a new target onto the stack.
  208.    Targets should supply this routine, if only to provide an error message.  */
  209. /* Called when selecting the simulator. EG: (gdb) target sim name.  */
  210.  
  211. static void
  212. gdbsim_open (args, from_tty)
  213.      char *args;
  214.      int from_tty;
  215. {
  216.   if (sr_get_debug ())
  217.     printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
  218.  
  219.   sim_open (args);
  220.  
  221.   push_target (&gdbsim_ops);
  222.   target_fetch_registers (-1);
  223.  
  224.   printf_filtered ("Connected to the simulator.\n");
  225. }
  226.  
  227. /* Does whatever cleanup is required for a target that we are no longer
  228.    going to be calling.  Argument says whether we are quitting gdb and
  229.    should not get hung in case of errors, or whether we want a clean
  230.    termination even if it takes a while.  This routine is automatically
  231.    always called just before a routine is popped off the target stack.
  232.    Closing file descriptors and freeing memory are typical things it should
  233.    do.  */
  234. /* Close out all files and local state before this target loses control. */
  235.  
  236. static void
  237. gdbsim_close (quitting)
  238.      int quitting;
  239. {
  240.   if (sr_get_debug ())
  241.     printf_filtered ("gdbsim_close: quitting %d\n", quitting);
  242.  
  243.   program_loaded = 0;
  244.  
  245.   sim_close (quitting);
  246. }
  247.  
  248. /* Takes a program previously attached to and detaches it.
  249.    The program may resume execution (some targets do, some don't) and will
  250.    no longer stop on signals, etc.  We better not have left any breakpoints
  251.    in the program or it'll die when it hits one.  ARGS is arguments
  252.    typed by the user (e.g. a signal to send the process).  FROM_TTY
  253.    says whether to be verbose or not.  */
  254. /* Terminate the open connection to the remote debugger.
  255.    Use this when you want to detach and do something else with your gdb.  */
  256.  
  257. static void
  258. gdbsim_detach (args,from_tty)
  259.      char *args;
  260.      int from_tty;
  261. {
  262.   if (sr_get_debug ())
  263.     printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
  264.  
  265.   pop_target ();        /* calls gdbsim_close to do the real work */
  266.   if (from_tty)
  267.     printf_filtered ("Ending simulator %s debugging\n", target_shortname);
  268. }
  269.  
  270. /* Resume execution of the target process.  STEP says whether to single-step
  271.    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
  272.    to the target, or zero for no signal.  */
  273.  
  274. static void
  275. gdbsim_resume (pid, step, siggnal)
  276.      int pid, step;
  277.      enum target_signal siggnal;
  278. {
  279.   if (sr_get_debug ())
  280.     printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
  281.  
  282.   sim_resume (step, target_signal_to_host (siggnal));
  283. }
  284.  
  285. /* Wait for inferior process to do something.  Return pid of child,
  286.    or -1 in case of error; store status through argument pointer STATUS,
  287.    just as `wait' would.  */
  288.  
  289. static int
  290. gdbsim_wait (pid, status)
  291.      int pid;
  292.      struct target_waitstatus *status;
  293. {
  294.   int sigrc;
  295.   enum sim_stop reason;
  296.  
  297.   if (sr_get_debug ())
  298.     printf_filtered ("gdbsim_wait\n");
  299.  
  300.   sim_stop_reason (&reason, &sigrc);
  301.   switch (reason)
  302.     {
  303.     case sim_exited:
  304.       status->kind = TARGET_WAITKIND_EXITED;
  305.       status->value.integer = sigrc;
  306.       break;
  307.     case sim_stopped:
  308.       status->kind = TARGET_WAITKIND_STOPPED;
  309.       /* The signal in sigrc is a host signal.  That probably
  310.      should be fixed.  */
  311.       status->value.sig = target_signal_from_host (sigrc);
  312.       break;
  313.     case sim_signalled:
  314.       status->kind = TARGET_WAITKIND_SIGNALLED;
  315.       /* The signal in sigrc is a host signal.  That probably
  316.      should be fixed.  */
  317.       status->value.sig = target_signal_from_host (sigrc);
  318.       break;
  319.     }
  320.  
  321.   return inferior_pid;
  322. }
  323.  
  324. /* Get ready to modify the registers array.  On machines which store
  325.    individual registers, this doesn't need to do anything.  On machines
  326.    which store all the registers in one fell swoop, this makes sure
  327.    that registers contains all the registers from the program being
  328.    debugged.  */
  329.  
  330. static void
  331. gdbsim_prepare_to_store ()
  332. {
  333.   /* Do nothing, since we can store individual regs */
  334. }
  335.  
  336. static int
  337. gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  338.      CORE_ADDR memaddr;
  339.      char *myaddr;
  340.      int len;
  341.      int write;
  342.      struct target_ops *target;            /* ignored */
  343. {
  344.   if (! program_loaded)
  345.     error ("No program loaded.");
  346.  
  347.   if (sr_get_debug ())
  348.     {
  349.       printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
  350.                myaddr, memaddr, len, write);
  351.       if (sr_get_debug () && write)
  352.     dump_mem(myaddr, len);
  353.     }
  354.  
  355.   if (write)
  356.     {
  357.       len = sim_write (memaddr, myaddr, len);
  358.     }
  359.   else 
  360.     {
  361.       len = sim_read (memaddr, myaddr, len);
  362.       if (sr_get_debug () && len > 0)
  363.     dump_mem(myaddr, len);
  364.     } 
  365.   return len;
  366. }
  367.  
  368. static void
  369. gdbsim_files_info (target)
  370.      struct target_ops *target;
  371. {
  372.   char *file = "nothing";
  373.  
  374.   if (exec_bfd)
  375.     file = bfd_get_filename (exec_bfd);
  376.  
  377.   if (sr_get_debug ())
  378.     printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
  379.  
  380.   if (exec_bfd)
  381.     {
  382.       printf_filtered ("\tAttached to %s running program %s\n",
  383.                target_shortname, file);
  384.       sim_info (0);
  385.     }
  386. }
  387.  
  388. /* Clear the sims notion of what the break points are.  */
  389.  
  390. static void
  391. gdbsim_mourn_inferior () 
  392.   if (sr_get_debug ())
  393.     printf_filtered ("gdbsim_mourn_inferior:\n");
  394.  
  395.   remove_breakpoints ();
  396.   generic_mourn_inferior ();
  397. }
  398.  
  399. /* Define the target subroutine names */
  400.  
  401. struct target_ops gdbsim_ops = 
  402. {
  403.   "sim", "simulator",
  404.   "Use the simulator",
  405.   gdbsim_open, gdbsim_close, 
  406.   0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
  407.   gdbsim_fetch_register, gdbsim_store_register,
  408.   gdbsim_prepare_to_store,
  409.   gdbsim_xfer_inferior_memory, 
  410.   gdbsim_files_info,
  411.   memory_insert_breakpoint,
  412.   memory_remove_breakpoint,
  413.   0, 0, 0, 0, 0,        /* Terminal handling */
  414.   gdbsim_kill,            /* kill */
  415.   gdbsim_load,            /* load */
  416.   0,                /* lookup_symbol */
  417.   gdbsim_create_inferior,    /* create_inferior */ 
  418.   gdbsim_mourn_inferior,    /* mourn_inferior */
  419.   0,                /* can_run */
  420.   0,                /* notice_signals */
  421.   0,                /* to_stop */
  422.   process_stratum, 0,        /* next */
  423.   1, 1, 1, 1, 1,        /* all mem, mem, stack, regs, exec */
  424.   0, 0,                /* Section pointers */
  425.   OPS_MAGIC,            /* Always the last thing */
  426. };
  427.  
  428. void
  429. _initialize_remote_sim ()
  430. {
  431.   add_target (&gdbsim_ops);
  432. }
  433.