1 /* Generic remote debugging interface for simulators.
2 Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
35 #include "remote-sim.h"
36 #include "remote-utils.h"
40 sim_* are the interface to the simulator (see remote-sim.h).
42 gdbsim_* are stuff which is internal to gdb. */
44 /* Forward data declarations */
45 extern struct target_ops gdbsim_ops;
47 static int program_loaded = 0;
56 if (len == 8 || len == 4)
60 printf_filtered ("\t0x%x", l[0]);
61 printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
66 printf_filtered ("\t");
67 for (i = 0; i < len; i++)
68 printf_filtered ("0x%x ", buf[i]);
69 printf_filtered ("\n");
75 gdbsim_fetch_register (regno)
80 for (regno = 0; regno < NUM_REGS; regno++)
81 gdbsim_fetch_register (regno);
85 char buf[MAX_REGISTER_RAW_SIZE];
87 sim_fetch_register (regno, buf);
88 supply_register (regno, buf);
91 printf_filtered ("gdbsim_fetch_register: %d", regno);
92 /* FIXME: We could print something more intelligible. */
93 dump_mem (buf, REGISTER_RAW_SIZE (regno));
99 gdbsim_store_register (regno)
104 for (regno = 0; regno < NUM_REGS; regno++)
105 gdbsim_store_register (regno);
109 /* FIXME: Until read_register() returns LONGEST, we have this. */
110 char value[MAX_REGISTER_RAW_SIZE];
111 read_register_gen (regno, value);
112 sim_store_register (regno, value);
115 printf_filtered ("gdbsim_store_register: %d", regno);
116 /* FIXME: We could print something more intelligible. */
117 dump_mem (value, REGISTER_RAW_SIZE (regno));
126 printf_filtered ("gdbsim_kill\n");
128 sim_kill (); /* close fd's, remove mappings */
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. */
137 gdbsim_load (prog, fromtty)
144 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
148 abfd = bfd_openr (prog, gnutarget);
151 error ("Unable to open file %s.", prog);
153 if (bfd_check_format (abfd, bfd_object) == 0)
154 error ("File is not an object file.");
156 if (sim_load (abfd, prog) != 0)
161 sim_set_pc (abfd->start_address);
165 * This is a utility routine that sim_load() can call to do the work.
166 * The result is 0 for success, non-zero for failure.
168 * Eg: int sim_load (bfd *abfd, char *prog) { return sim_load_standard (abfd); }
171 sim_load_standard (abfd)
177 while (s != (asection *)NULL)
179 if (s->flags & SEC_LOAD)
183 char *buffer = xmalloc (delta);
184 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
185 s->name, s->vma, s->vma + s->_raw_size);
186 for (i = 0; i < s->_raw_size; i+= delta)
188 int sub_delta = delta;
189 if (sub_delta > s->_raw_size - i)
190 sub_delta = s->_raw_size - i ;
192 bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
193 sim_write (s->vma + i, buffer, sub_delta);
194 printf_filtered ("*");
197 printf_filtered ("\n");
206 /* Start an inferior process and set inferior_pid to its pid.
207 EXEC_FILE is the file to run.
208 ALLARGS is a string containing the arguments to the program.
209 ENV is the environment vector to pass. Errors reported with error().
210 On VxWorks and various standalone systems, we ignore exec_file. */
211 /* This is called not only when we first attach, but also when the
212 user types "run" after having attached. */
215 gdbsim_create_inferior (exec_file, args, env)
221 char *arg_buf,**argv;
223 if (! program_loaded)
224 error ("No program loaded.");
227 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
230 if (exec_file == 0 || exec_bfd == 0)
231 error ("No exec file specified.");
233 entry_pt = (int) bfd_get_start_address (exec_bfd);
235 gdbsim_kill (NULL, NULL);
236 remove_breakpoints ();
237 init_wait_for_inferior ();
239 len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
240 arg_buf = (char *) alloca (len);
242 strcat (arg_buf, exec_file);
243 strcat (arg_buf, " ");
244 strcat (arg_buf, args);
245 argv = buildargv (arg_buf);
246 make_cleanup (freeargv, (char *) argv);
247 /* FIXME: remote-sim.h says targets that don't support this return
248 non-zero. Perhaps distinguish between "not supported" and other errors?
249 Or maybe that can be the only error. */
250 if (sim_set_args (argv, env) != 0)
254 insert_breakpoints (); /* Needed to get correct instruction in cache */
255 proceed (entry_pt, -1, 0);
258 /* The open routine takes the rest of the parameters from the command,
259 and (if successful) pushes a new target onto the stack.
260 Targets should supply this routine, if only to provide an error message. */
261 /* Called when selecting the simulator. EG: (gdb) target sim name. */
264 gdbsim_open (args, from_tty)
269 printf_filtered ("gdbsim_open: args \"%s\"\n", args);
271 if (sim_open (args) != 0)
273 /* FIXME: This is totally bogus. sim_open should have a way to
274 tell us what the error was, so we can tell the user. */
275 error ("Unable to initialize simulator (insufficient memory?).");
279 push_target (&gdbsim_ops);
280 target_fetch_registers (-1);
282 printf_filtered ("Connected to the simulator.\n");
285 /* Does whatever cleanup is required for a target that we are no longer
286 going to be calling. Argument says whether we are quitting gdb and
287 should not get hung in case of errors, or whether we want a clean
288 termination even if it takes a while. This routine is automatically
289 always called just before a routine is popped off the target stack.
290 Closing file descriptors and freeing memory are typical things it should
292 /* Close out all files and local state before this target loses control. */
295 gdbsim_close (quitting)
299 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
303 /* FIXME: Need to call sim_close() to close all files and
304 delete all mappings. */
307 /* Takes a program previously attached to and detaches it.
308 The program may resume execution (some targets do, some don't) and will
309 no longer stop on signals, etc. We better not have left any breakpoints
310 in the program or it'll die when it hits one. ARGS is arguments
311 typed by the user (e.g. a signal to send the process). FROM_TTY
312 says whether to be verbose or not. */
313 /* Terminate the open connection to the remote debugger.
314 Use this when you want to detach and do something else with your gdb. */
317 gdbsim_detach (args,from_tty)
322 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
324 pop_target (); /* calls gdbsim_close to do the real work */
326 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
329 /* Resume execution of the target process. STEP says whether to single-step
330 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
331 to the target, or zero for no signal. */
334 gdbsim_resume (pid, step, siggnal)
335 int pid, step, siggnal;
338 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
340 sim_resume (step, siggnal);
343 /* Wait for inferior process to do something. Return pid of child,
344 or -1 in case of error; store status through argument pointer STATUS,
345 just as `wait' would. */
348 gdbsim_wait (pid, status)
353 printf_filtered ("gdbsim_wait: ");
354 WSETSTOP (*status, sim_stop_signal ());
356 printf_filtered ("status %d\n", *status);
360 /* Get ready to modify the registers array. On machines which store
361 individual registers, this doesn't need to do anything. On machines
362 which store all the registers in one fell swoop, this makes sure
363 that registers contains all the registers from the program being
367 gdbsim_prepare_to_store ()
369 /* Do nothing, since we can store individual regs */
373 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
378 struct target_ops *target; /* ignored */
380 if (! program_loaded)
381 error ("No program loaded.");
385 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
386 myaddr, memaddr, len, write);
387 if (sr_get_debug () && write)
388 dump_mem(myaddr, len);
393 len = sim_write (memaddr, myaddr, len);
397 len = sim_read (memaddr, myaddr, len);
398 if (sr_get_debug () && len > 0)
399 dump_mem(myaddr, len);
405 gdbsim_files_info (target)
406 struct target_ops *target;
408 char *file = "nothing";
411 file = bfd_get_filename (exec_bfd);
414 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
418 printf_filtered ("\tAttached to %s running program %s\n",
419 target_shortname, file);
424 /* Clear the sims notion of what the break points are. */
427 gdbsim_mourn_inferior ()
430 printf_filtered ("gdbsim_mourn_inferior:\n");
432 remove_breakpoints ();
433 generic_mourn_inferior ();
436 /* Define the target subroutine names */
438 struct target_ops gdbsim_ops =
442 gdbsim_open, gdbsim_close,
443 0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
444 gdbsim_fetch_register, gdbsim_store_register,
445 gdbsim_prepare_to_store,
446 gdbsim_xfer_inferior_memory,
448 0, 0, /* Breakpoints */
449 0, 0, 0, 0, 0, /* Terminal handling */
450 gdbsim_kill, /* kill */
452 0, /* lookup_symbol */
453 gdbsim_create_inferior, /* create_inferior */
454 gdbsim_mourn_inferior, /* mourn_inferior */
456 0, /* notice_signals */
457 process_stratum, 0, /* next */
458 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
459 0, 0, /* Section pointers */
460 OPS_MAGIC, /* Always the last thing */
464 _initialize_remote_sim ()
466 add_target (&gdbsim_ops);