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