* procfs.c (procfs_wait): Fix argument name to match 4 Jan changes.
[external/binutils.git] / gdb / remote-sim.c
1 /* Generic remote debugging interface for simulators.
2    Copyright 1993 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4    Steve Chamberlain (sac@cygnus.com) and Doug Evans (dje@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 static void
123 gdbsim_kill ()
124 {
125   if (sr_get_debug ())
126     printf_filtered ("gdbsim_kill\n");
127
128   sim_kill ();  /* close fd's, remove mappings */
129   inferior_pid = 0;
130 }
131
132 /* Load an executable file into the target process.  This is expected to
133    not only bring new code into the target process, but also to update
134    GDB's symbol tables to match.  */
135
136 static void
137 gdbsim_load (prog, fromtty)
138      char *prog;
139      int fromtty;
140 {
141   if (sr_get_debug ())
142     printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
143
144   inferior_pid = 0;  
145   program_loaded = 1;
146   gr_load_image (prog, fromtty);
147 }
148
149
150 /* Start an inferior process and set inferior_pid to its pid.
151    EXEC_FILE is the file to run.
152    ALLARGS is a string containing the arguments to the program.
153    ENV is the environment vector to pass.  Errors reported with error().
154    On VxWorks and various standalone systems, we ignore exec_file.  */
155 /* This is called not only when we first attach, but also when the
156    user types "run" after having attached.  */
157
158 static void
159 gdbsim_create_inferior (exec_file, args, env)
160      char *exec_file;
161      char *args;
162      char **env;
163 {
164   int len,entry_pt;
165   char *arg_buf,**argv;
166
167   if (! program_loaded)
168     error ("No program loaded.");
169
170   if (sr_get_debug ())
171     printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
172       exec_file, args);
173
174   if (exec_file == 0 || exec_bfd == 0)
175    error ("No exec file specified.");
176
177   entry_pt = (int) bfd_get_start_address (exec_bfd);
178
179   gdbsim_kill (NULL, NULL);      
180   remove_breakpoints ();
181   init_wait_for_inferior ();
182
183   len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
184   arg_buf = (char *) alloca (len);
185   arg_buf[0] = '\0';
186   strcat (arg_buf, exec_file);
187   strcat (arg_buf, " ");
188   strcat (arg_buf, args);
189   argv = buildargv (arg_buf);
190   make_cleanup (freeargv, (char *) argv);
191   /* FIXME: remote-sim.h says targets that don't support this return
192      non-zero.  Perhaps distinguish between "not supported" and other errors?
193      Or maybe that can be the only error.  */
194   if (sim_set_args (argv, env) != 0)
195     return;
196
197   inferior_pid = 42;
198   insert_breakpoints ();        /* Needed to get correct instruction in cache */
199   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
200 }
201
202 /* The open routine takes the rest of the parameters from the command,
203    and (if successful) pushes a new target onto the stack.
204    Targets should supply this routine, if only to provide an error message.  */
205 /* Called when selecting the simulator. EG: (gdb) target sim name.  */
206
207 static void
208 gdbsim_open (args, from_tty)
209      char *args;
210      int from_tty;
211 {
212   if (sr_get_debug ())
213     printf_filtered ("gdbsim_open: args \"%s\"\n", args);
214
215   if (sim_open (args) != 0)
216     {
217       /* FIXME: This is totally bogus.  sim_open should have a way to
218          tell us what the error was, so we can tell the user.  */
219       error ("Unable to initialize simulator (insufficient memory?).");
220       return;
221     }
222
223   push_target (&gdbsim_ops);
224   target_fetch_registers (-1);
225
226   printf_filtered ("Connected to the simulator.\n");
227 }
228
229 /* Does whatever cleanup is required for a target that we are no longer
230    going to be calling.  Argument says whether we are quitting gdb and
231    should not get hung in case of errors, or whether we want a clean
232    termination even if it takes a while.  This routine is automatically
233    always called just before a routine is popped off the target stack.
234    Closing file descriptors and freeing memory are typical things it should
235    do.  */
236 /* Close out all files and local state before this target loses control. */
237
238 static void
239 gdbsim_close (quitting)
240      int quitting;
241 {
242   if (sr_get_debug ())
243     printf_filtered ("gdbsim_close: quitting %d\n", quitting);
244
245   program_loaded = 0;
246
247   /* FIXME: Need to call sim_close() to close all files and
248      delete all mappings. */
249 }
250
251 /* Takes a program previously attached to and detaches it.
252    The program may resume execution (some targets do, some don't) and will
253    no longer stop on signals, etc.  We better not have left any breakpoints
254    in the program or it'll die when it hits one.  ARGS is arguments
255    typed by the user (e.g. a signal to send the process).  FROM_TTY
256    says whether to be verbose or not.  */
257 /* Terminate the open connection to the remote debugger.
258    Use this when you want to detach and do something else with your gdb.  */
259
260 static void
261 gdbsim_detach (args,from_tty)
262      char *args;
263      int from_tty;
264 {
265   if (sr_get_debug ())
266     printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
267
268   pop_target ();                /* calls gdbsim_close to do the real work */
269   if (from_tty)
270     printf_filtered ("Ending simulator %s debugging\n", target_shortname);
271 }
272  
273 /* Resume execution of the target process.  STEP says whether to single-step
274    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
275    to the target, or zero for no signal.  */
276
277 static void
278 gdbsim_resume (pid, step, siggnal)
279      int pid, step;
280      enum target_signal siggnal;
281 {
282   if (sr_get_debug ())
283     printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
284
285   sim_resume (step, target_signal_to_host (siggnal));
286 }
287
288 /* Wait for inferior process to do something.  Return pid of child,
289    or -1 in case of error; store status through argument pointer STATUS,
290    just as `wait' would.  */
291
292 static int
293 gdbsim_wait (pid, status)
294      int pid;
295      struct target_waitstatus *status;
296 {
297   int sigrc;
298   enum sim_stop reason;
299
300   if (sr_get_debug ())
301     printf_filtered ("gdbsim_wait\n");
302
303   sim_stop_reason (&reason, &sigrc);
304   switch (reason)
305     {
306     case sim_exited:
307       status->kind = TARGET_WAITKIND_EXITED;
308       status->value.integer = sigrc;
309       break;
310     case sim_stopped:
311       status->kind = TARGET_WAITKIND_STOPPED;
312       /* The signal in sigrc is a host signal.  That probably
313          should be fixed.  */
314       status->value.sig = target_signal_from_host (sigrc);
315       break;
316     case sim_signalled:
317       status->kind = TARGET_WAITKIND_SIGNALLED;
318       /* The signal in sigrc is a host signal.  That probably
319          should be fixed.  */
320       status->value.sig = target_signal_from_host (sigrc);
321       break;
322     }
323
324   return inferior_pid;
325 }
326
327 /* Get ready to modify the registers array.  On machines which store
328    individual registers, this doesn't need to do anything.  On machines
329    which store all the registers in one fell swoop, this makes sure
330    that registers contains all the registers from the program being
331    debugged.  */
332
333 static void
334 gdbsim_prepare_to_store ()
335 {
336   /* Do nothing, since we can store individual regs */
337 }
338
339 static int
340 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
341      CORE_ADDR memaddr;
342      char *myaddr;
343      int len;
344      int write;
345      struct target_ops *target;                 /* ignored */
346 {
347   if (! program_loaded)
348     error ("No program loaded.");
349
350   if (sr_get_debug ())
351     {
352       printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
353                        myaddr, memaddr, len, write);
354       if (sr_get_debug () && write)
355         dump_mem(myaddr, len);
356     }
357
358   if (write)
359     {
360       len = sim_write (memaddr, myaddr, len);
361     }
362   else 
363     {
364       len = sim_read (memaddr, myaddr, len);
365       if (sr_get_debug () && len > 0)
366         dump_mem(myaddr, len);
367     } 
368   return len;
369 }
370
371 static void
372 gdbsim_files_info (target)
373      struct target_ops *target;
374 {
375   char *file = "nothing";
376
377   if (exec_bfd)
378     file = bfd_get_filename (exec_bfd);
379
380   if (sr_get_debug ())
381     printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
382
383   if (exec_bfd)
384     {
385       printf_filtered ("\tAttached to %s running program %s\n",
386                        target_shortname, file);
387       sim_info (printf_filtered, 0);
388     }
389 }
390
391 /* Clear the sims notion of what the break points are.  */
392
393 static void
394 gdbsim_mourn_inferior () 
395
396   if (sr_get_debug ())
397     printf_filtered ("gdbsim_mourn_inferior:\n");
398
399   remove_breakpoints ();
400   generic_mourn_inferior ();
401 }
402
403 /* Define the target subroutine names */
404
405 struct target_ops gdbsim_ops = 
406 {
407   "sim", "simulator",
408   "Use the simulator",
409   gdbsim_open, gdbsim_close, 
410   0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
411   gdbsim_fetch_register, gdbsim_store_register,
412   gdbsim_prepare_to_store,
413   gdbsim_xfer_inferior_memory, 
414   gdbsim_files_info,
415   0, 0,                         /* Breakpoints */
416   0, 0, 0, 0, 0,                /* Terminal handling */
417   gdbsim_kill,                  /* kill */
418   gdbsim_load, 
419   0,                            /* lookup_symbol */
420   gdbsim_create_inferior,               /* create_inferior */ 
421   gdbsim_mourn_inferior,                /* mourn_inferior */
422   0,                            /* can_run */
423   0,                            /* notice_signals */
424   process_stratum, 0,           /* next */
425   1, 1, 1, 1, 1,                /* all mem, mem, stack, regs, exec */
426   0, 0,                         /* Section pointers */
427   OPS_MAGIC,                    /* Always the last thing */
428 };
429
430 void
431 _initialize_remote_sim ()
432 {
433   add_target (&gdbsim_ops);
434 }