1 /* Generic remote debugging interface for simulators.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
6 This file is part of GDB.
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.
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.
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. */
26 #include "gdb_string.h"
35 #include "remote-sim.h"
36 #include "remote-utils.h"
41 static void dump_mem PARAMS ((char *buf, int len));
43 static void gdbsim_fetch_register PARAMS ((int regno));
45 static void gdbsim_store_register PARAMS ((int regno));
47 static void gdbsim_kill PARAMS ((void));
49 static void gdbsim_load PARAMS ((char *prog, int fromtty));
51 static void gdbsim_create_inferior PARAMS ((char *exec_file, char *args, char **env));
53 static void gdbsim_open PARAMS ((char *args, int from_tty));
55 static void gdbsim_close PARAMS ((int quitting));
57 static void gdbsim_detach PARAMS ((char *args, int from_tty));
59 static void gdbsim_resume PARAMS ((int pid, int step, enum target_signal siggnal));
61 static int gdbsim_wait PARAMS ((int pid, struct target_waitstatus *status));
63 static void gdbsim_prepare_to_store PARAMS ((void));
65 static int gdbsim_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
66 char *myaddr, int len,
68 struct target_ops *target));
70 static void gdbsim_files_info PARAMS ((struct target_ops *target));
72 static void gdbsim_mourn_inferior PARAMS ((void));
74 static void simulator_command PARAMS ((char *args, int from_tty));
78 sim_* are the interface to the simulator (see remote-sim.h).
79 sim_callback_* are the stuff which the simulator can see inside GDB.
80 gdbsim_* are stuff which is internal to gdb. */
82 /* Forward data declarations */
83 extern struct target_ops gdbsim_ops;
85 static int program_loaded = 0;
94 if (len == 8 || len == 4)
98 printf_filtered ("\t0x%x", l[0]);
99 printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
104 printf_filtered ("\t");
105 for (i = 0; i < len; i++)
106 printf_filtered ("0x%x ", buf[i]);
107 printf_filtered ("\n");
113 gdbsim_fetch_register (regno)
118 for (regno = 0; regno < NUM_REGS; regno++)
119 gdbsim_fetch_register (regno);
123 char buf[MAX_REGISTER_RAW_SIZE];
125 sim_fetch_register (regno, buf);
126 supply_register (regno, buf);
129 printf_filtered ("gdbsim_fetch_register: %d", regno);
130 /* FIXME: We could print something more intelligible. */
131 dump_mem (buf, REGISTER_RAW_SIZE (regno));
138 gdbsim_store_register (regno)
143 for (regno = 0; regno < NUM_REGS; regno++)
144 gdbsim_store_register (regno);
148 /* FIXME: Until read_register() returns LONGEST, we have this. */
149 char tmp[MAX_REGISTER_RAW_SIZE];
150 read_register_gen (regno, tmp);
151 sim_store_register (regno, tmp);
154 printf_filtered ("gdbsim_store_register: %d", regno);
155 /* FIXME: We could print something more intelligible. */
156 dump_mem (tmp, REGISTER_RAW_SIZE (regno));
161 /* Kill the running program. This may involve closing any open files
162 and releasing other resources acquired by the simulated program. */
168 printf_filtered ("gdbsim_kill\n");
170 sim_kill (); /* close fd's, remove mappings */
174 /* Load an executable file into the target process. This is expected to
175 not only bring new code into the target process, but also to update
176 GDB's symbol tables to match. */
179 gdbsim_load (prog, fromtty)
184 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
188 /* This must be done before calling gr_load_image. */
191 if (sim_load (prog, fromtty) != 0)
192 generic_load (prog, fromtty);
196 /* Start an inferior process and set inferior_pid to its pid.
197 EXEC_FILE is the file to run.
198 ALLARGS is a string containing the arguments to the program.
199 ENV is the environment vector to pass. Errors reported with error().
200 On VxWorks and various standalone systems, we ignore exec_file. */
201 /* This is called not only when we first attach, but also when the
202 user types "run" after having attached. */
205 gdbsim_create_inferior (exec_file, args, env)
211 char *arg_buf,**argv;
214 if (! program_loaded)
215 error ("No program loaded.");
218 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
221 if (exec_file == 0 || exec_bfd == 0)
222 error ("No exec file specified.");
224 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
227 remove_breakpoints ();
228 init_wait_for_inferior ();
230 len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
231 arg_buf = (char *) alloca (len);
233 strcat (arg_buf, exec_file);
234 strcat (arg_buf, " ");
235 strcat (arg_buf, args);
236 argv = buildargv (arg_buf);
237 make_cleanup (freeargv, (char *) argv);
238 sim_create_inferior (entry_pt, argv, env);
241 insert_breakpoints (); /* Needed to get correct instruction in cache */
242 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
245 /* The open routine takes the rest of the parameters from the command,
246 and (if successful) pushes a new target onto the stack.
247 Targets should supply this routine, if only to provide an error message. */
248 /* Called when selecting the simulator. EG: (gdb) target sim name. */
251 gdbsim_open (args, from_tty)
256 printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
258 sim_set_callbacks (&default_callback);
259 default_callback.init (&default_callback);
263 push_target (&gdbsim_ops);
264 target_fetch_registers (-1);
265 printf_filtered ("Connected to the simulator.\n");
268 /* Does whatever cleanup is required for a target that we are no longer
269 going to be calling. Argument says whether we are quitting gdb and
270 should not get hung in case of errors, or whether we want a clean
271 termination even if it takes a while. This routine is automatically
272 always called just before a routine is popped off the target stack.
273 Closing file descriptors and freeing memory are typical things it should
275 /* Close out all files and local state before this target loses control. */
278 gdbsim_close (quitting)
282 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
286 sim_close (quitting);
289 /* Takes a program previously attached to and detaches it.
290 The program may resume execution (some targets do, some don't) and will
291 no longer stop on signals, etc. We better not have left any breakpoints
292 in the program or it'll die when it hits one. ARGS is arguments
293 typed by the user (e.g. a signal to send the process). FROM_TTY
294 says whether to be verbose or not. */
295 /* Terminate the open connection to the remote debugger.
296 Use this when you want to detach and do something else with your gdb. */
299 gdbsim_detach (args,from_tty)
304 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
306 pop_target (); /* calls gdbsim_close to do the real work */
308 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
311 /* Resume execution of the target process. STEP says whether to single-step
312 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
313 to the target, or zero for no signal. */
316 gdbsim_resume (pid, step, siggnal)
318 enum target_signal siggnal;
320 if (inferior_pid != 42)
321 error ("The program is not being run.");
324 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
326 sim_resume (step, target_signal_to_host (siggnal));
329 /* Wait for inferior process to do something. Return pid of child,
330 or -1 in case of error; store status through argument pointer STATUS,
331 just as `wait' would. */
334 gdbsim_wait (pid, status)
336 struct target_waitstatus *status;
339 enum sim_stop reason;
342 printf_filtered ("gdbsim_wait\n");
344 sim_stop_reason (&reason, &sigrc);
348 status->kind = TARGET_WAITKIND_EXITED;
349 status->value.integer = sigrc;
352 status->kind = TARGET_WAITKIND_STOPPED;
353 /* The signal in sigrc is a host signal. That probably
355 status->value.sig = target_signal_from_host (sigrc);
358 status->kind = TARGET_WAITKIND_SIGNALLED;
359 /* The signal in sigrc is a host signal. That probably
361 status->value.sig = target_signal_from_host (sigrc);
368 /* Get ready to modify the registers array. On machines which store
369 individual registers, this doesn't need to do anything. On machines
370 which store all the registers in one fell swoop, this makes sure
371 that registers contains all the registers from the program being
375 gdbsim_prepare_to_store ()
377 /* Do nothing, since we can store individual regs */
381 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
386 struct target_ops *target; /* ignored */
388 if (! program_loaded)
389 error ("No program loaded.");
393 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
394 myaddr, memaddr, len, write);
395 if (sr_get_debug () && write)
396 dump_mem(myaddr, len);
401 len = sim_write (memaddr, myaddr, len);
405 len = sim_read (memaddr, myaddr, len);
406 if (sr_get_debug () && len > 0)
407 dump_mem(myaddr, len);
413 gdbsim_files_info (target)
414 struct target_ops *target;
416 char *file = "nothing";
419 file = bfd_get_filename (exec_bfd);
422 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
426 printf_filtered ("\tAttached to %s running program %s\n",
427 target_shortname, file);
432 /* Clear the simulator's notion of what the break points are. */
435 gdbsim_mourn_inferior ()
438 printf_filtered ("gdbsim_mourn_inferior:\n");
440 remove_breakpoints ();
441 generic_mourn_inferior ();
444 /* Put a command string, in args, out to MONITOR. Output from MONITOR
445 is placed on the users terminal until the prompt is seen. FIXME: We
446 read the characters ourseleves here cause of a nasty echo. */
449 simulator_command (args, from_tty)
453 sim_do_command (args);
456 /* Define the target subroutine names */
458 struct target_ops gdbsim_ops = {
459 "sim", /* to_shortname */
460 "simulator", /* to_longname */
461 "Use the compiled-in simulator.", /* to_doc */
462 gdbsim_open, /* to_open */
463 gdbsim_close, /* to_close */
464 NULL, /* to_attach */
465 gdbsim_detach, /* to_detach */
466 gdbsim_resume, /* to_resume */
467 gdbsim_wait, /* to_wait */
468 gdbsim_fetch_register, /* to_fetch_registers */
469 gdbsim_store_register, /* to_store_registers */
470 gdbsim_prepare_to_store, /* to_prepare_to_store */
471 gdbsim_xfer_inferior_memory, /* to_xfer_memory */
472 gdbsim_files_info, /* to_files_info */
473 memory_insert_breakpoint, /* to_insert_breakpoint */
474 memory_remove_breakpoint, /* to_remove_breakpoint */
475 NULL, /* to_terminal_init */
476 NULL, /* to_terminal_inferior */
477 NULL, /* to_terminal_ours_for_output */
478 NULL, /* to_terminal_ours */
479 NULL, /* to_terminal_info */
480 gdbsim_kill, /* to_kill */
481 gdbsim_load, /* to_load */
482 NULL, /* to_lookup_symbol */
483 gdbsim_create_inferior, /* to_create_inferior */
484 gdbsim_mourn_inferior, /* to_mourn_inferior */
486 0, /* to_notice_signals */
487 0, /* to_thread_alive */
489 process_stratum, /* to_stratum */
491 1, /* to_has_all_memory */
492 1, /* to_has_memory */
493 1, /* to_has_stack */
494 1, /* to_has_registers */
495 1, /* to_has_execution */
497 NULL, /* sections_end */
498 OPS_MAGIC, /* to_magic */
502 _initialize_remote_sim ()
504 add_target (&gdbsim_ops);
506 add_com ("sim <command>", class_obscure, simulator_command,
507 "Send a command to the simulator.");