Wed Sep 20 08:16:03 1995 steve chamberlain <sac@slash.cygnus.com>
[external/binutils.git] / gdb / remote-sim.c
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "wait.h"
25 #include "value.h"
26 #include "gdb_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    sim_callback_* are the stuff which the simulator can see inside GDB.
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
99 int 
100 sim_callback_write_stdout (arg, len)
101 char *arg;
102 int len;
103 {
104   int i;
105   char b[2];
106   for (i = 0; i< len; i++) {
107     b[0] = arg[i];
108     b[1] = 0;
109     if (target_output_hook)
110       target_output_hook (b);
111     else
112       fputs_filtered (b, gdb_stdout);
113   }
114   return len;
115 }
116
117 static void
118 gdbsim_store_register (regno)
119 int regno;
120 {
121   if (regno  == -1) 
122     {
123       for (regno = 0; regno < NUM_REGS; regno++)
124         gdbsim_store_register (regno);
125     }
126   else
127     {
128       /* FIXME: Until read_register() returns LONGEST, we have this.  */
129       char tmp[MAX_REGISTER_RAW_SIZE];
130       read_register_gen (regno, tmp);
131       sim_store_register (regno, tmp);
132       if (sr_get_debug ())
133         {
134           printf_filtered ("gdbsim_store_register: %d", regno);
135           /* FIXME: We could print something more intelligible.  */
136           dump_mem (tmp, REGISTER_RAW_SIZE (regno));
137         }
138     }
139 }
140
141 /* Kill the running program.  This may involve closing any open files
142    and releasing other resources acquired by the simulated program.  */
143
144 static void
145 gdbsim_kill ()
146 {
147   if (sr_get_debug ())
148     printf_filtered ("gdbsim_kill\n");
149
150   sim_kill ();  /* close fd's, remove mappings */
151   inferior_pid = 0;
152 }
153
154 /* Load an executable file into the target process.  This is expected to
155    not only bring new code into the target process, but also to update
156    GDB's symbol tables to match.  */
157
158 static void
159 gdbsim_load (prog, fromtty)
160      char *prog;
161      int fromtty;
162 {
163   if (sr_get_debug ())
164     printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
165
166   inferior_pid = 0;
167
168   /* This must be done before calling gr_load_image.  */
169   program_loaded = 1;
170
171   if (sim_load (prog, fromtty) != 0)
172     gr_load_image (prog, fromtty);
173 }
174
175
176 /* Start an inferior process and set inferior_pid to its pid.
177    EXEC_FILE is the file to run.
178    ALLARGS is a string containing the arguments to the program.
179    ENV is the environment vector to pass.  Errors reported with error().
180    On VxWorks and various standalone systems, we ignore exec_file.  */
181 /* This is called not only when we first attach, but also when the
182    user types "run" after having attached.  */
183
184 static void
185 gdbsim_create_inferior (exec_file, args, env)
186      char *exec_file;
187      char *args;
188      char **env;
189 {
190   int len;
191   char *arg_buf,**argv;
192   CORE_ADDR entry_pt;
193
194   if (! program_loaded)
195     error ("No program loaded.");
196
197   if (sr_get_debug ())
198     printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
199       exec_file, args);
200
201   if (exec_file == 0 || exec_bfd == 0)
202    error ("No exec file specified.");
203
204   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
205
206   gdbsim_kill (NULL, NULL);      
207   remove_breakpoints ();
208   init_wait_for_inferior ();
209
210   len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
211   arg_buf = (char *) alloca (len);
212   arg_buf[0] = '\0';
213   strcat (arg_buf, exec_file);
214   strcat (arg_buf, " ");
215   strcat (arg_buf, args);
216   argv = buildargv (arg_buf);
217   make_cleanup (freeargv, (char *) argv);
218   sim_create_inferior (entry_pt, argv, env);
219
220   inferior_pid = 42;
221   insert_breakpoints ();        /* Needed to get correct instruction in cache */
222   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
223 }
224
225 /* The open routine takes the rest of the parameters from the command,
226    and (if successful) pushes a new target onto the stack.
227    Targets should supply this routine, if only to provide an error message.  */
228 /* Called when selecting the simulator. EG: (gdb) target sim name.  */
229
230 static void
231 gdbsim_open (args, from_tty)
232      char *args;
233      int from_tty;
234 {
235   if (sr_get_debug ())
236     printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
237
238   sim_open (args);
239
240   push_target (&gdbsim_ops);
241   target_fetch_registers (-1);
242
243   printf_filtered ("Connected to the simulator.\n");
244 }
245
246 /* Does whatever cleanup is required for a target that we are no longer
247    going to be calling.  Argument says whether we are quitting gdb and
248    should not get hung in case of errors, or whether we want a clean
249    termination even if it takes a while.  This routine is automatically
250    always called just before a routine is popped off the target stack.
251    Closing file descriptors and freeing memory are typical things it should
252    do.  */
253 /* Close out all files and local state before this target loses control. */
254
255 static void
256 gdbsim_close (quitting)
257      int quitting;
258 {
259   if (sr_get_debug ())
260     printf_filtered ("gdbsim_close: quitting %d\n", quitting);
261
262   program_loaded = 0;
263
264   sim_close (quitting);
265 }
266
267 /* Takes a program previously attached to and detaches it.
268    The program may resume execution (some targets do, some don't) and will
269    no longer stop on signals, etc.  We better not have left any breakpoints
270    in the program or it'll die when it hits one.  ARGS is arguments
271    typed by the user (e.g. a signal to send the process).  FROM_TTY
272    says whether to be verbose or not.  */
273 /* Terminate the open connection to the remote debugger.
274    Use this when you want to detach and do something else with your gdb.  */
275
276 static void
277 gdbsim_detach (args,from_tty)
278      char *args;
279      int from_tty;
280 {
281   if (sr_get_debug ())
282     printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
283
284   pop_target ();                /* calls gdbsim_close to do the real work */
285   if (from_tty)
286     printf_filtered ("Ending simulator %s debugging\n", target_shortname);
287 }
288  
289 /* Resume execution of the target process.  STEP says whether to single-step
290    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
291    to the target, or zero for no signal.  */
292
293 static void
294 gdbsim_resume (pid, step, siggnal)
295      int pid, step;
296      enum target_signal siggnal;
297 {
298   if (sr_get_debug ())
299     printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
300
301   sim_resume (step, target_signal_to_host (siggnal));
302 }
303
304 /* Wait for inferior process to do something.  Return pid of child,
305    or -1 in case of error; store status through argument pointer STATUS,
306    just as `wait' would.  */
307
308 static int
309 gdbsim_wait (pid, status)
310      int pid;
311      struct target_waitstatus *status;
312 {
313   int sigrc;
314   enum sim_stop reason;
315
316   if (sr_get_debug ())
317     printf_filtered ("gdbsim_wait\n");
318
319   sim_stop_reason (&reason, &sigrc);
320   switch (reason)
321     {
322     case sim_exited:
323       status->kind = TARGET_WAITKIND_EXITED;
324       status->value.integer = sigrc;
325       break;
326     case sim_stopped:
327       status->kind = TARGET_WAITKIND_STOPPED;
328       /* The signal in sigrc is a host signal.  That probably
329          should be fixed.  */
330       status->value.sig = target_signal_from_host (sigrc);
331       break;
332     case sim_signalled:
333       status->kind = TARGET_WAITKIND_SIGNALLED;
334       /* The signal in sigrc is a host signal.  That probably
335          should be fixed.  */
336       status->value.sig = target_signal_from_host (sigrc);
337       break;
338     }
339
340   return inferior_pid;
341 }
342
343 /* Get ready to modify the registers array.  On machines which store
344    individual registers, this doesn't need to do anything.  On machines
345    which store all the registers in one fell swoop, this makes sure
346    that registers contains all the registers from the program being
347    debugged.  */
348
349 static void
350 gdbsim_prepare_to_store ()
351 {
352   /* Do nothing, since we can store individual regs */
353 }
354
355 static int
356 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
357      CORE_ADDR memaddr;
358      char *myaddr;
359      int len;
360      int write;
361      struct target_ops *target;                 /* ignored */
362 {
363   if (! program_loaded)
364     error ("No program loaded.");
365
366   if (sr_get_debug ())
367     {
368       printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
369                        myaddr, memaddr, len, write);
370       if (sr_get_debug () && write)
371         dump_mem(myaddr, len);
372     }
373
374   if (write)
375     {
376       len = sim_write (memaddr, myaddr, len);
377     }
378   else 
379     {
380       len = sim_read (memaddr, myaddr, len);
381       if (sr_get_debug () && len > 0)
382         dump_mem(myaddr, len);
383     } 
384   return len;
385 }
386
387 static void
388 gdbsim_files_info (target)
389      struct target_ops *target;
390 {
391   char *file = "nothing";
392
393   if (exec_bfd)
394     file = bfd_get_filename (exec_bfd);
395
396   if (sr_get_debug ())
397     printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
398
399   if (exec_bfd)
400     {
401       printf_filtered ("\tAttached to %s running program %s\n",
402                        target_shortname, file);
403       sim_info (0);
404     }
405 }
406
407 /* Clear the simulator's notion of what the break points are.  */
408
409 static void
410 gdbsim_mourn_inferior () 
411
412   if (sr_get_debug ())
413     printf_filtered ("gdbsim_mourn_inferior:\n");
414
415   remove_breakpoints ();
416   generic_mourn_inferior ();
417 }
418
419 /* Put a command string, in args, out to MONITOR.  Output from MONITOR
420    is placed on the users terminal until the prompt is seen. FIXME: We
421    read the characters ourseleves here cause of a nasty echo.  */
422
423 static void
424 simulator_command (args, from_tty)
425      char *args;
426      int from_tty;
427 {
428   sim_do_command (args);
429 }
430
431 /* Define the target subroutine names */
432
433 struct target_ops gdbsim_ops = {
434   "sim",                        /* to_shortname */
435   "simulator",                  /* to_longname */
436   "Use the compiled-in simulator.",  /* to_doc */
437   gdbsim_open,                  /* to_open */
438   gdbsim_close,                 /* to_close */
439   NULL,                         /* to_attach */
440   gdbsim_detach,                /* to_detach */
441   gdbsim_resume,                /* to_resume */
442   gdbsim_wait,                  /* to_wait */
443   gdbsim_fetch_register,        /* to_fetch_registers */
444   gdbsim_store_register,        /* to_store_registers */
445   gdbsim_prepare_to_store,      /* to_prepare_to_store */
446   gdbsim_xfer_inferior_memory,  /* to_xfer_memory */
447   gdbsim_files_info,            /* to_files_info */
448   memory_insert_breakpoint,     /* to_insert_breakpoint */
449   memory_remove_breakpoint,     /* to_remove_breakpoint */
450   NULL,                         /* to_terminal_init */
451   NULL,                         /* to_terminal_inferior */
452   NULL,                         /* to_terminal_ours_for_output */
453   NULL,                         /* to_terminal_ours */
454   NULL,                         /* to_terminal_info */
455   gdbsim_kill,                  /* to_kill */
456   gdbsim_load,                  /* to_load */
457   NULL,                         /* to_lookup_symbol */
458   gdbsim_create_inferior,       /* to_create_inferior */ 
459   gdbsim_mourn_inferior,        /* to_mourn_inferior */
460   0,                            /* to_can_run */
461   0,                            /* to_notice_signals */
462   0,                            /* to_thread_alive */
463   0,                            /* to_stop */
464   process_stratum,              /* to_stratum */
465   NULL,                         /* to_next */
466   1,                            /* to_has_all_memory */
467   1,                            /* to_has_memory */
468   1,                            /* to_has_stack */
469   1,                            /* to_has_registers */
470   1,                            /* to_has_execution */
471   NULL,                         /* sections */
472   NULL,                         /* sections_end */
473   OPS_MAGIC,                    /* to_magic */
474 };
475
476 void
477 _initialize_remote_sim ()
478 {
479   add_target (&gdbsim_ops);
480
481   add_com ("sim <command>", class_obscure, simulator_command,
482            "Send a command to the simulator."); 
483 }