| 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 | |
| 37 | /* Naming convention: |
| 38 | |
| 39 | sim_* are the interface to the simulator (see remote-sim.h). |
| 40 | |
| 41 | gdbsim_* are stuff which is internal to gdb. */ |
| 42 | |
| 43 | /* Forward data declarations */ |
| 44 | extern struct target_ops gdbsim_ops; |
| 45 | |
| 46 | static int program_loaded = 0; |
| 47 | |
| 48 | static void |
| 49 | dump_mem (buf, len) |
| 50 | char *buf; |
| 51 | int len; |
| 52 | { |
| 53 | if (len <= 8) |
| 54 | { |
| 55 | if (len == 8 || len == 4) |
| 56 | { |
| 57 | long l[2]; |
| 58 | memcpy (l, buf, len); |
| 59 | printf_filtered ("\t0x%x", l[0]); |
| 60 | printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | int i; |
| 65 | printf_filtered ("\t"); |
| 66 | for (i = 0; i < len; i++) |
| 67 | printf_filtered ("0x%x ", buf[i]); |
| 68 | printf_filtered ("\n"); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | gdbsim_fetch_register (regno) |
| 75 | int regno; |
| 76 | { |
| 77 | if (regno == -1) |
| 78 | { |
| 79 | for (regno = 0; regno < NUM_REGS; regno++) |
| 80 | gdbsim_fetch_register (regno); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 85 | |
| 86 | sim_fetch_register (regno, buf); |
| 87 | supply_register (regno, buf); |
| 88 | if (sr_get_debug ()) |
| 89 | { |
| 90 | printf_filtered ("gdbsim_fetch_register: %d", regno); |
| 91 | /* FIXME: We could print something more intelligible. */ |
| 92 | dump_mem (buf, REGISTER_RAW_SIZE (regno)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | gdbsim_store_register (regno) |
| 99 | int regno; |
| 100 | { |
| 101 | if (regno == -1) |
| 102 | { |
| 103 | for (regno = 0; regno < NUM_REGS; regno++) |
| 104 | gdbsim_store_register (regno); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | /* FIXME: Until read_register() returns LONGEST, we have this. */ |
| 109 | char value[MAX_REGISTER_RAW_SIZE]; |
| 110 | |
| 111 | read_register_gen (regno, value); |
| 112 | SWAP_TARGET_AND_HOST (value, REGISTER_RAW_SIZE (regno)); |
| 113 | sim_store_register (regno, value); |
| 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 (value, REGISTER_RAW_SIZE (regno)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | gdbsim_kill () |
| 125 | { |
| 126 | if (sr_get_debug ()) |
| 127 | printf_filtered ("gdbsim_kill\n"); |
| 128 | |
| 129 | sim_kill (); /* close fd's, remove mappings */ |
| 130 | inferior_pid = 0; |
| 131 | } |
| 132 | |
| 133 | /* Load an executable file into the target process. This is expected to |
| 134 | not only bring new code into the target process, but also to update |
| 135 | GDB's symbol tables to match. */ |
| 136 | |
| 137 | static void |
| 138 | gdbsim_load (prog, fromtty) |
| 139 | char *prog; |
| 140 | int fromtty; |
| 141 | { |
| 142 | bfd *abfd; |
| 143 | |
| 144 | if (sr_get_debug ()) |
| 145 | printf_filtered ("gdbsim_load: prog \"%s\"\n", prog); |
| 146 | |
| 147 | inferior_pid = 0; |
| 148 | program_loaded = 0; |
| 149 | abfd = bfd_openr (prog, gnutarget); |
| 150 | |
| 151 | if (!abfd) |
| 152 | error ("Unable to open file %s.", prog); |
| 153 | |
| 154 | if (bfd_check_format (abfd, bfd_object) == 0) |
| 155 | error ("File is not an object file."); |
| 156 | |
| 157 | if (sim_load (abfd, prog) != 0) |
| 158 | return; |
| 159 | |
| 160 | program_loaded = 1; |
| 161 | |
| 162 | sim_set_pc (abfd->start_address); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * This is a utility routine that sim_load() can call to do the work. |
| 167 | * The result is 0 for success, non-zero for failure. |
| 168 | * |
| 169 | * Eg: int sim_load (bfd *bfd, char *prog) { return sim_load_standard (bfd); } |
| 170 | */ |
| 171 | |
| 172 | sim_load_standard (abfd) |
| 173 | bfd *abfd; |
| 174 | { |
| 175 | asection *s; |
| 176 | |
| 177 | s = abfd->sections; |
| 178 | while (s != (asection *)NULL) |
| 179 | { |
| 180 | if (s->flags & SEC_LOAD) |
| 181 | { |
| 182 | int i; |
| 183 | int delta = 4096; |
| 184 | char *buffer = xmalloc (delta); |
| 185 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", |
| 186 | s->name, s->vma, s->vma + s->_raw_size); |
| 187 | for (i = 0; i < s->_raw_size; i+= delta) |
| 188 | { |
| 189 | int sub_delta = delta; |
| 190 | if (sub_delta > s->_raw_size - i) |
| 191 | sub_delta = s->_raw_size - i ; |
| 192 | |
| 193 | bfd_get_section_contents (abfd, s, buffer, i, sub_delta); |
| 194 | sim_write (s->vma + i, buffer, sub_delta); |
| 195 | printf_filtered ("*"); |
| 196 | fflush (stdout); |
| 197 | } |
| 198 | printf_filtered ("\n"); |
| 199 | free (buffer); |
| 200 | } |
| 201 | s = s->next; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* Start an inferior process and set inferior_pid to its pid. |
| 208 | EXEC_FILE is the file to run. |
| 209 | ALLARGS is a string containing the arguments to the program. |
| 210 | ENV is the environment vector to pass. Errors reported with error(). |
| 211 | On VxWorks and various standalone systems, we ignore exec_file. */ |
| 212 | /* This is called not only when we first attach, but also when the |
| 213 | user types "run" after having attached. */ |
| 214 | |
| 215 | static void |
| 216 | gdbsim_create_inferior (exec_file, args, env) |
| 217 | char *exec_file; |
| 218 | char *args; |
| 219 | char **env; |
| 220 | { |
| 221 | int len,entry_pt; |
| 222 | char *arg_buf,**argv; |
| 223 | |
| 224 | if (! program_loaded) |
| 225 | error ("No program loaded."); |
| 226 | |
| 227 | if (sr_get_debug ()) |
| 228 | printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", |
| 229 | exec_file, args); |
| 230 | |
| 231 | if (exec_file == 0 || exec_bfd == 0) |
| 232 | error ("No exec file specified."); |
| 233 | |
| 234 | entry_pt = (int) bfd_get_start_address (exec_bfd); |
| 235 | |
| 236 | gdbsim_kill (NULL, NULL); |
| 237 | remove_breakpoints (); |
| 238 | init_wait_for_inferior (); |
| 239 | |
| 240 | len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10; |
| 241 | arg_buf = (char *) alloca (len); |
| 242 | arg_buf[0] = '\0'; |
| 243 | strcat (arg_buf, exec_file); |
| 244 | strcat (arg_buf, " "); |
| 245 | strcat (arg_buf, args); |
| 246 | argv = buildargv (arg_buf); |
| 247 | make_cleanup (freeargv, (char *) argv); |
| 248 | /* FIXME: remote-sim.h says targets that don't support this return |
| 249 | non-zero. Perhaps distinguish between "not supported" and other errors? |
| 250 | Or maybe that can be the only error. */ |
| 251 | if (sim_set_args (argv, env) != 0) |
| 252 | return; |
| 253 | |
| 254 | inferior_pid = 42; |
| 255 | insert_breakpoints (); /* Needed to get correct instruction in cache */ |
| 256 | proceed (entry_pt, -1, 0); |
| 257 | } |
| 258 | |
| 259 | /* The open routine takes the rest of the parameters from the command, |
| 260 | and (if successful) pushes a new target onto the stack. |
| 261 | Targets should supply this routine, if only to provide an error message. */ |
| 262 | /* Called when selecting the simulator. EG: (gdb) target sim name. */ |
| 263 | |
| 264 | static void |
| 265 | gdbsim_open (args, from_tty) |
| 266 | char *args; |
| 267 | int from_tty; |
| 268 | { |
| 269 | if (sr_get_debug ()) |
| 270 | printf_filtered ("gdbsim_open: args \"%s\"\n", args); |
| 271 | |
| 272 | if (sim_open (args) != 0) |
| 273 | { |
| 274 | /* FIXME: This is totally bogus. sim_open should have a way to |
| 275 | tell us what the error was, so we can tell the user. */ |
| 276 | error ("Unable to initialize simulator (insufficient memory?)."); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | push_target (&gdbsim_ops); |
| 281 | target_fetch_registers (-1); |
| 282 | |
| 283 | printf_filtered ("Connected to the simulator.\n"); |
| 284 | } |
| 285 | |
| 286 | /* Does whatever cleanup is required for a target that we are no longer |
| 287 | going to be calling. Argument says whether we are quitting gdb and |
| 288 | should not get hung in case of errors, or whether we want a clean |
| 289 | termination even if it takes a while. This routine is automatically |
| 290 | always called just before a routine is popped off the target stack. |
| 291 | Closing file descriptors and freeing memory are typical things it should |
| 292 | do. */ |
| 293 | /* Close out all files and local state before this target loses control. */ |
| 294 | |
| 295 | static void |
| 296 | gdbsim_close (quitting) |
| 297 | int quitting; |
| 298 | { |
| 299 | if (sr_get_debug ()) |
| 300 | printf_filtered ("gdbsim_close: quitting %d\n", quitting); |
| 301 | |
| 302 | program_loaded = 0; |
| 303 | |
| 304 | /* FIXME: Need to call sim_close() to close all files and |
| 305 | delete all mappings. */ |
| 306 | } |
| 307 | |
| 308 | /* Takes a program previously attached to and detaches it. |
| 309 | The program may resume execution (some targets do, some don't) and will |
| 310 | no longer stop on signals, etc. We better not have left any breakpoints |
| 311 | in the program or it'll die when it hits one. ARGS is arguments |
| 312 | typed by the user (e.g. a signal to send the process). FROM_TTY |
| 313 | says whether to be verbose or not. */ |
| 314 | /* Terminate the open connection to the remote debugger. |
| 315 | Use this when you want to detach and do something else with your gdb. */ |
| 316 | |
| 317 | static void |
| 318 | gdbsim_detach (args,from_tty) |
| 319 | char *args; |
| 320 | int from_tty; |
| 321 | { |
| 322 | if (sr_get_debug ()) |
| 323 | printf_filtered ("gdbsim_detach: args \"%s\"\n", args); |
| 324 | |
| 325 | pop_target (); /* calls gdbsim_close to do the real work */ |
| 326 | if (from_tty) |
| 327 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); |
| 328 | } |
| 329 | |
| 330 | /* Resume execution of the target process. STEP says whether to single-step |
| 331 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given |
| 332 | to the target, or zero for no signal. */ |
| 333 | |
| 334 | static void |
| 335 | gdbsim_resume (pid, step, siggnal) |
| 336 | int pid, step, siggnal; |
| 337 | { |
| 338 | if (sr_get_debug ()) |
| 339 | printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); |
| 340 | |
| 341 | sim_resume (step, siggnal); |
| 342 | } |
| 343 | |
| 344 | /* Wait for inferior process to do something. Return pid of child, |
| 345 | or -1 in case of error; store status through argument pointer STATUS, |
| 346 | just as `wait' would. */ |
| 347 | |
| 348 | static int |
| 349 | gdbsim_wait (status) |
| 350 | WAITTYPE *status; |
| 351 | { |
| 352 | if (sr_get_debug ()) |
| 353 | printf_filtered ("gdbsim_wait: "); |
| 354 | #if 1 |
| 355 | *status = sim_stop_signal (); |
| 356 | #else |
| 357 | WSETSTOP (*status, sim_stop_signal ()); |
| 358 | #endif |
| 359 | if (sr_get_debug ()) |
| 360 | printf_filtered ("status %d\n", *status); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* Get ready to modify the registers array. On machines which store |
| 365 | individual registers, this doesn't need to do anything. On machines |
| 366 | which store all the registers in one fell swoop, this makes sure |
| 367 | that registers contains all the registers from the program being |
| 368 | debugged. */ |
| 369 | |
| 370 | static void |
| 371 | gdbsim_prepare_to_store () |
| 372 | { |
| 373 | /* Do nothing, since we can store individual regs */ |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target) |
| 378 | CORE_ADDR memaddr; |
| 379 | char *myaddr; |
| 380 | int len; |
| 381 | int write; |
| 382 | struct target_ops *target; /* ignored */ |
| 383 | { |
| 384 | if (! program_loaded) |
| 385 | error ("No program loaded."); |
| 386 | |
| 387 | if (sr_get_debug ()) |
| 388 | { |
| 389 | printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n", |
| 390 | myaddr, memaddr, len, write); |
| 391 | if (sr_get_debug () && write) |
| 392 | dump_mem(myaddr, len); |
| 393 | } |
| 394 | |
| 395 | if (write) |
| 396 | { |
| 397 | len = sim_write (memaddr, myaddr, len); |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | len = sim_read (memaddr, myaddr, len); |
| 402 | if (sr_get_debug () && len > 0) |
| 403 | dump_mem(myaddr, len); |
| 404 | } |
| 405 | return len; |
| 406 | } |
| 407 | |
| 408 | static void |
| 409 | gdbsim_files_info (target) |
| 410 | struct target_ops *target; |
| 411 | { |
| 412 | char *file = "nothing"; |
| 413 | |
| 414 | if (exec_bfd) |
| 415 | file = bfd_get_filename (exec_bfd); |
| 416 | |
| 417 | if (sr_get_debug ()) |
| 418 | printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); |
| 419 | |
| 420 | if (exec_bfd) |
| 421 | { |
| 422 | printf_filtered ("\tAttached to %s running program %s\n", |
| 423 | target_shortname, file); |
| 424 | sim_info (); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /* Clear the sims notion of what the break points are. */ |
| 429 | |
| 430 | static void |
| 431 | gdbsim_mourn_inferior () |
| 432 | { |
| 433 | if (sr_get_debug ()) |
| 434 | printf_filtered ("gdbsim_mourn_inferior:\n"); |
| 435 | |
| 436 | remove_breakpoints (); |
| 437 | generic_mourn_inferior (); |
| 438 | } |
| 439 | |
| 440 | /* Define the target subroutine names */ |
| 441 | |
| 442 | struct target_ops gdbsim_ops = |
| 443 | { |
| 444 | "sim", "simulator", |
| 445 | "Use the simulator", |
| 446 | gdbsim_open, gdbsim_close, |
| 447 | 0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */ |
| 448 | gdbsim_fetch_register, gdbsim_store_register, |
| 449 | gdbsim_prepare_to_store, |
| 450 | gdbsim_xfer_inferior_memory, |
| 451 | gdbsim_files_info, |
| 452 | 0, 0, /* Breakpoints */ |
| 453 | 0, 0, 0, 0, 0, /* Terminal handling */ |
| 454 | gdbsim_kill, /* kill */ |
| 455 | gdbsim_load, |
| 456 | 0, /* lookup_symbol */ |
| 457 | gdbsim_create_inferior, /* create_inferior */ |
| 458 | gdbsim_mourn_inferior, /* mourn_inferior */ |
| 459 | 0, /* can_run */ |
| 460 | 0, /* notice_signals */ |
| 461 | process_stratum, 0, /* next */ |
| 462 | 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */ |
| 463 | 0, 0, /* Section pointers */ |
| 464 | OPS_MAGIC, /* Always the last thing */ |
| 465 | }; |
| 466 | |
| 467 | void |
| 468 | _initialize_remote_sim () |
| 469 | { |
| 470 | add_target (&gdbsim_ops); |
| 471 | } |