1130812Smarcel/* Generic remote debugging interface for simulators. 2130812Smarcel 3130812Smarcel Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 4130812Smarcel 2002, 2004 Free Software Foundation, Inc. 5130812Smarcel 6130812Smarcel Contributed by Cygnus Support. 7130812Smarcel Steve Chamberlain (sac@cygnus.com). 8130812Smarcel 9130812Smarcel This file is part of GDB. 10130812Smarcel 11130812Smarcel This program is free software; you can redistribute it and/or modify 12130812Smarcel it under the terms of the GNU General Public License as published by 13130812Smarcel the Free Software Foundation; either version 2 of the License, or 14130812Smarcel (at your option) any later version. 15130812Smarcel 16130812Smarcel This program is distributed in the hope that it will be useful, 17130812Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 18130812Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19130812Smarcel GNU General Public License for more details. 20130812Smarcel 21130812Smarcel You should have received a copy of the GNU General Public License 22130812Smarcel along with this program; if not, write to the Free Software 23130812Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 24130812Smarcel Boston, MA 02111-1307, USA. */ 25130812Smarcel 26130812Smarcel#include "defs.h" 27130812Smarcel#include "inferior.h" 28130812Smarcel#include "value.h" 29130812Smarcel#include "gdb_string.h" 30130812Smarcel#include <ctype.h> 31130812Smarcel#include <fcntl.h> 32130812Smarcel#include <signal.h> 33130812Smarcel#include <setjmp.h> 34130812Smarcel#include <errno.h> 35130812Smarcel#include "terminal.h" 36130812Smarcel#include "target.h" 37130812Smarcel#include "gdbcore.h" 38130812Smarcel#include "gdb/callback.h" 39130812Smarcel#include "gdb/remote-sim.h" 40130812Smarcel#include "remote-utils.h" 41130812Smarcel#include "command.h" 42130812Smarcel#include "regcache.h" 43130812Smarcel#include "gdb_assert.h" 44130812Smarcel#include "sim-regno.h" 45130812Smarcel#include "arch-utils.h" 46130812Smarcel 47130812Smarcel/* Prototypes */ 48130812Smarcel 49130812Smarcelextern void _initialize_remote_sim (void); 50130812Smarcel 51130812Smarcelextern int (*ui_loop_hook) (int signo); 52130812Smarcel 53130812Smarcelstatic void dump_mem (char *buf, int len); 54130812Smarcel 55130812Smarcelstatic void init_callbacks (void); 56130812Smarcel 57130812Smarcelstatic void end_callbacks (void); 58130812Smarcel 59130812Smarcelstatic int gdb_os_write_stdout (host_callback *, const char *, int); 60130812Smarcel 61130812Smarcelstatic void gdb_os_flush_stdout (host_callback *); 62130812Smarcel 63130812Smarcelstatic int gdb_os_write_stderr (host_callback *, const char *, int); 64130812Smarcel 65130812Smarcelstatic void gdb_os_flush_stderr (host_callback *); 66130812Smarcel 67130812Smarcelstatic int gdb_os_poll_quit (host_callback *); 68130812Smarcel 69130812Smarcel/* printf_filtered is depreciated */ 70130812Smarcelstatic void gdb_os_printf_filtered (host_callback *, const char *, ...); 71130812Smarcel 72130812Smarcelstatic void gdb_os_vprintf_filtered (host_callback *, const char *, va_list); 73130812Smarcel 74130812Smarcelstatic void gdb_os_evprintf_filtered (host_callback *, const char *, va_list); 75130812Smarcel 76130812Smarcelstatic void gdb_os_error (host_callback *, const char *, ...); 77130812Smarcel 78130812Smarcelstatic void gdbsim_fetch_register (int regno); 79130812Smarcel 80130812Smarcelstatic void gdbsim_store_register (int regno); 81130812Smarcel 82130812Smarcelstatic void gdbsim_kill (void); 83130812Smarcel 84130812Smarcelstatic void gdbsim_load (char *prog, int fromtty); 85130812Smarcel 86130812Smarcelstatic void gdbsim_create_inferior (char *exec_file, char *args, char **env); 87130812Smarcel 88130812Smarcelstatic void gdbsim_open (char *args, int from_tty); 89130812Smarcel 90130812Smarcelstatic void gdbsim_close (int quitting); 91130812Smarcel 92130812Smarcelstatic void gdbsim_detach (char *args, int from_tty); 93130812Smarcel 94130812Smarcelstatic void gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal); 95130812Smarcel 96130812Smarcelstatic ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status); 97130812Smarcel 98130812Smarcelstatic void gdbsim_prepare_to_store (void); 99130812Smarcel 100130812Smarcelstatic int gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, 101130812Smarcel int len, int write, 102130812Smarcel struct mem_attrib *attrib, 103130812Smarcel struct target_ops *target); 104130812Smarcel 105130812Smarcelstatic void gdbsim_files_info (struct target_ops *target); 106130812Smarcel 107130812Smarcelstatic void gdbsim_mourn_inferior (void); 108130812Smarcel 109130812Smarcelstatic void gdbsim_stop (void); 110130812Smarcel 111130812Smarcelvoid simulator_command (char *args, int from_tty); 112130812Smarcel 113130812Smarcel/* Naming convention: 114130812Smarcel 115130812Smarcel sim_* are the interface to the simulator (see remote-sim.h). 116130812Smarcel gdbsim_* are stuff which is internal to gdb. */ 117130812Smarcel 118130812Smarcel/* Forward data declarations */ 119130812Smarcelextern struct target_ops gdbsim_ops; 120130812Smarcel 121130812Smarcelstatic int program_loaded = 0; 122130812Smarcel 123130812Smarcel/* We must keep track of whether the simulator has been opened or not because 124130812Smarcel GDB can call a target's close routine twice, but sim_close doesn't allow 125130812Smarcel this. We also need to record the result of sim_open so we can pass it 126130812Smarcel back to the other sim_foo routines. */ 127130812Smarcelstatic SIM_DESC gdbsim_desc = 0; 128130812Smarcel 129130812Smarcelstatic void 130130812Smarceldump_mem (char *buf, int len) 131130812Smarcel{ 132130812Smarcel if (len <= 8) 133130812Smarcel { 134130812Smarcel if (len == 8 || len == 4) 135130812Smarcel { 136130812Smarcel long l[2]; 137130812Smarcel memcpy (l, buf, len); 138130812Smarcel printf_filtered ("\t0x%lx", l[0]); 139130812Smarcel if (len == 8) 140130812Smarcel printf_filtered (" 0x%lx", l[1]); 141130812Smarcel printf_filtered ("\n"); 142130812Smarcel } 143130812Smarcel else 144130812Smarcel { 145130812Smarcel int i; 146130812Smarcel printf_filtered ("\t"); 147130812Smarcel for (i = 0; i < len; i++) 148130812Smarcel printf_filtered ("0x%x ", buf[i]); 149130812Smarcel printf_filtered ("\n"); 150130812Smarcel } 151130812Smarcel } 152130812Smarcel} 153130812Smarcel 154130812Smarcelstatic host_callback gdb_callback; 155130812Smarcelstatic int callbacks_initialized = 0; 156130812Smarcel 157130812Smarcel/* Initialize gdb_callback. */ 158130812Smarcel 159130812Smarcelstatic void 160130812Smarcelinit_callbacks (void) 161130812Smarcel{ 162130812Smarcel if (!callbacks_initialized) 163130812Smarcel { 164130812Smarcel gdb_callback = default_callback; 165130812Smarcel gdb_callback.init (&gdb_callback); 166130812Smarcel gdb_callback.write_stdout = gdb_os_write_stdout; 167130812Smarcel gdb_callback.flush_stdout = gdb_os_flush_stdout; 168130812Smarcel gdb_callback.write_stderr = gdb_os_write_stderr; 169130812Smarcel gdb_callback.flush_stderr = gdb_os_flush_stderr; 170130812Smarcel gdb_callback.printf_filtered = gdb_os_printf_filtered; 171130812Smarcel gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered; 172130812Smarcel gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered; 173130812Smarcel gdb_callback.error = gdb_os_error; 174130812Smarcel gdb_callback.poll_quit = gdb_os_poll_quit; 175130812Smarcel gdb_callback.magic = HOST_CALLBACK_MAGIC; 176130812Smarcel callbacks_initialized = 1; 177130812Smarcel } 178130812Smarcel} 179130812Smarcel 180130812Smarcel/* Release callbacks (free resources used by them). */ 181130812Smarcel 182130812Smarcelstatic void 183130812Smarcelend_callbacks (void) 184130812Smarcel{ 185130812Smarcel if (callbacks_initialized) 186130812Smarcel { 187130812Smarcel gdb_callback.shutdown (&gdb_callback); 188130812Smarcel callbacks_initialized = 0; 189130812Smarcel } 190130812Smarcel} 191130812Smarcel 192130812Smarcel/* GDB version of os_write_stdout callback. */ 193130812Smarcel 194130812Smarcelstatic int 195130812Smarcelgdb_os_write_stdout (host_callback *p, const char *buf, int len) 196130812Smarcel{ 197130812Smarcel int i; 198130812Smarcel char b[2]; 199130812Smarcel 200130812Smarcel ui_file_write (gdb_stdtarg, buf, len); 201130812Smarcel return len; 202130812Smarcel} 203130812Smarcel 204130812Smarcel/* GDB version of os_flush_stdout callback. */ 205130812Smarcel 206130812Smarcelstatic void 207130812Smarcelgdb_os_flush_stdout (host_callback *p) 208130812Smarcel{ 209130812Smarcel gdb_flush (gdb_stdtarg); 210130812Smarcel} 211130812Smarcel 212130812Smarcel/* GDB version of os_write_stderr callback. */ 213130812Smarcel 214130812Smarcelstatic int 215130812Smarcelgdb_os_write_stderr (host_callback *p, const char *buf, int len) 216130812Smarcel{ 217130812Smarcel int i; 218130812Smarcel char b[2]; 219130812Smarcel 220130812Smarcel for (i = 0; i < len; i++) 221130812Smarcel { 222130812Smarcel b[0] = buf[i]; 223130812Smarcel b[1] = 0; 224130812Smarcel fputs_unfiltered (b, gdb_stdtargerr); 225130812Smarcel } 226130812Smarcel return len; 227130812Smarcel} 228130812Smarcel 229130812Smarcel/* GDB version of os_flush_stderr callback. */ 230130812Smarcel 231130812Smarcelstatic void 232130812Smarcelgdb_os_flush_stderr (host_callback *p) 233130812Smarcel{ 234130812Smarcel gdb_flush (gdb_stdtargerr); 235130812Smarcel} 236130812Smarcel 237130812Smarcel/* GDB version of printf_filtered callback. */ 238130812Smarcel 239130812Smarcelstatic void 240130812Smarcelgdb_os_printf_filtered (host_callback * p, const char *format,...) 241130812Smarcel{ 242130812Smarcel va_list args; 243130812Smarcel va_start (args, format); 244130812Smarcel 245130812Smarcel vfprintf_filtered (gdb_stdout, format, args); 246130812Smarcel 247130812Smarcel va_end (args); 248130812Smarcel} 249130812Smarcel 250130812Smarcel/* GDB version of error vprintf_filtered. */ 251130812Smarcel 252130812Smarcelstatic void 253130812Smarcelgdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap) 254130812Smarcel{ 255130812Smarcel vfprintf_filtered (gdb_stdout, format, ap); 256130812Smarcel} 257130812Smarcel 258130812Smarcel/* GDB version of error evprintf_filtered. */ 259130812Smarcel 260130812Smarcelstatic void 261130812Smarcelgdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap) 262130812Smarcel{ 263130812Smarcel vfprintf_filtered (gdb_stderr, format, ap); 264130812Smarcel} 265130812Smarcel 266130812Smarcel/* GDB version of error callback. */ 267130812Smarcel 268130812Smarcelstatic void 269130812Smarcelgdb_os_error (host_callback * p, const char *format,...) 270130812Smarcel{ 271130812Smarcel if (error_hook) 272130812Smarcel (*error_hook) (); 273130812Smarcel else 274130812Smarcel { 275130812Smarcel va_list args; 276130812Smarcel va_start (args, format); 277130812Smarcel verror (format, args); 278130812Smarcel va_end (args); 279130812Smarcel } 280130812Smarcel} 281130812Smarcel 282130812Smarcelint 283130812Smarcelone2one_register_sim_regno (int regnum) 284130812Smarcel{ 285130812Smarcel /* Only makes sense to supply raw registers. */ 286130812Smarcel gdb_assert (regnum >= 0 && regnum < NUM_REGS); 287130812Smarcel return regnum; 288130812Smarcel} 289130812Smarcel 290130812Smarcelstatic void 291130812Smarcelgdbsim_fetch_register (int regno) 292130812Smarcel{ 293130812Smarcel if (regno == -1) 294130812Smarcel { 295130812Smarcel for (regno = 0; regno < NUM_REGS; regno++) 296130812Smarcel gdbsim_fetch_register (regno); 297130812Smarcel return; 298130812Smarcel } 299130812Smarcel 300130812Smarcel switch (REGISTER_SIM_REGNO (regno)) 301130812Smarcel { 302130812Smarcel case LEGACY_SIM_REGNO_IGNORE: 303130812Smarcel break; 304130812Smarcel case SIM_REGNO_DOES_NOT_EXIST: 305130812Smarcel { 306130812Smarcel /* For moment treat a `does not exist' register the same way 307130812Smarcel as an ``unavailable'' register. */ 308130812Smarcel char buf[MAX_REGISTER_SIZE]; 309130812Smarcel int nr_bytes; 310130812Smarcel memset (buf, 0, MAX_REGISTER_SIZE); 311130812Smarcel supply_register (regno, buf); 312130812Smarcel set_register_cached (regno, -1); 313130812Smarcel break; 314130812Smarcel } 315130812Smarcel default: 316130812Smarcel { 317130812Smarcel static int warn_user = 1; 318130812Smarcel char buf[MAX_REGISTER_SIZE]; 319130812Smarcel int nr_bytes; 320130812Smarcel gdb_assert (regno >= 0 && regno < NUM_REGS); 321130812Smarcel memset (buf, 0, MAX_REGISTER_SIZE); 322130812Smarcel nr_bytes = sim_fetch_register (gdbsim_desc, 323130812Smarcel REGISTER_SIM_REGNO (regno), 324130812Smarcel buf, DEPRECATED_REGISTER_RAW_SIZE (regno)); 325130812Smarcel if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno) && warn_user) 326130812Smarcel { 327130812Smarcel fprintf_unfiltered (gdb_stderr, 328130812Smarcel "Size of register %s (%d/%d) incorrect (%d instead of %d))", 329130812Smarcel REGISTER_NAME (regno), 330130812Smarcel regno, REGISTER_SIM_REGNO (regno), 331130812Smarcel nr_bytes, DEPRECATED_REGISTER_RAW_SIZE (regno)); 332130812Smarcel warn_user = 0; 333130812Smarcel } 334130812Smarcel /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' 335130812Smarcel indicating that GDB and the SIM have different ideas about 336130812Smarcel which registers are fetchable. */ 337130812Smarcel /* Else if (nr_bytes < 0): an old simulator, that doesn't 338130812Smarcel think to return the register size. Just assume all is ok. */ 339130812Smarcel supply_register (regno, buf); 340130812Smarcel if (sr_get_debug ()) 341130812Smarcel { 342130812Smarcel printf_filtered ("gdbsim_fetch_register: %d", regno); 343130812Smarcel /* FIXME: We could print something more intelligible. */ 344130812Smarcel dump_mem (buf, DEPRECATED_REGISTER_RAW_SIZE (regno)); 345130812Smarcel } 346130812Smarcel break; 347130812Smarcel } 348130812Smarcel } 349130812Smarcel} 350130812Smarcel 351130812Smarcel 352130812Smarcelstatic void 353130812Smarcelgdbsim_store_register (int regno) 354130812Smarcel{ 355130812Smarcel if (regno == -1) 356130812Smarcel { 357130812Smarcel for (regno = 0; regno < NUM_REGS; regno++) 358130812Smarcel gdbsim_store_register (regno); 359130812Smarcel return; 360130812Smarcel } 361130812Smarcel else if (REGISTER_SIM_REGNO (regno) >= 0) 362130812Smarcel { 363130812Smarcel char tmp[MAX_REGISTER_SIZE]; 364130812Smarcel int nr_bytes; 365130812Smarcel deprecated_read_register_gen (regno, tmp); 366130812Smarcel nr_bytes = sim_store_register (gdbsim_desc, 367130812Smarcel REGISTER_SIM_REGNO (regno), 368130812Smarcel tmp, DEPRECATED_REGISTER_RAW_SIZE (regno)); 369130812Smarcel if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno)) 370130812Smarcel internal_error (__FILE__, __LINE__, 371130812Smarcel "Register size different to expected"); 372130812Smarcel /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' 373130812Smarcel indicating that GDB and the SIM have different ideas about 374130812Smarcel which registers are fetchable. */ 375130812Smarcel if (sr_get_debug ()) 376130812Smarcel { 377130812Smarcel printf_filtered ("gdbsim_store_register: %d", regno); 378130812Smarcel /* FIXME: We could print something more intelligible. */ 379130812Smarcel dump_mem (tmp, DEPRECATED_REGISTER_RAW_SIZE (regno)); 380130812Smarcel } 381130812Smarcel } 382130812Smarcel} 383130812Smarcel 384130812Smarcel/* Kill the running program. This may involve closing any open files 385130812Smarcel and releasing other resources acquired by the simulated program. */ 386130812Smarcel 387130812Smarcelstatic void 388130812Smarcelgdbsim_kill (void) 389130812Smarcel{ 390130812Smarcel if (sr_get_debug ()) 391130812Smarcel printf_filtered ("gdbsim_kill\n"); 392130812Smarcel 393130812Smarcel /* There is no need to `kill' running simulator - the simulator is 394130812Smarcel not running */ 395130812Smarcel inferior_ptid = null_ptid; 396130812Smarcel} 397130812Smarcel 398130812Smarcel/* Load an executable file into the target process. This is expected to 399130812Smarcel not only bring new code into the target process, but also to update 400130812Smarcel GDB's symbol tables to match. */ 401130812Smarcel 402130812Smarcelstatic void 403130812Smarcelgdbsim_load (char *prog, int fromtty) 404130812Smarcel{ 405130812Smarcel if (sr_get_debug ()) 406130812Smarcel printf_filtered ("gdbsim_load: prog \"%s\"\n", prog); 407130812Smarcel 408130812Smarcel inferior_ptid = null_ptid; 409130812Smarcel 410130812Smarcel /* FIXME: We will print two messages on error. 411130812Smarcel Need error to either not print anything if passed NULL or need 412130812Smarcel another routine that doesn't take any arguments. */ 413130812Smarcel if (sim_load (gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL) 414130812Smarcel error ("unable to load program"); 415130812Smarcel 416130812Smarcel /* FIXME: If a load command should reset the targets registers then 417130812Smarcel a call to sim_create_inferior() should go here. */ 418130812Smarcel 419130812Smarcel program_loaded = 1; 420130812Smarcel} 421130812Smarcel 422130812Smarcel 423130812Smarcel/* Start an inferior process and set inferior_ptid to its pid. 424130812Smarcel EXEC_FILE is the file to run. 425130812Smarcel ARGS is a string containing the arguments to the program. 426130812Smarcel ENV is the environment vector to pass. Errors reported with error(). 427130812Smarcel On VxWorks and various standalone systems, we ignore exec_file. */ 428130812Smarcel/* This is called not only when we first attach, but also when the 429130812Smarcel user types "run" after having attached. */ 430130812Smarcel 431130812Smarcelstatic void 432130812Smarcelgdbsim_create_inferior (char *exec_file, char *args, char **env) 433130812Smarcel{ 434130812Smarcel int len; 435130812Smarcel char *arg_buf, **argv; 436130812Smarcel 437130812Smarcel if (exec_file == 0 || exec_bfd == 0) 438130812Smarcel warning ("No executable file specified."); 439130812Smarcel if (!program_loaded) 440130812Smarcel warning ("No program loaded."); 441130812Smarcel 442130812Smarcel if (sr_get_debug ()) 443130812Smarcel printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", 444130812Smarcel (exec_file ? exec_file : "(NULL)"), 445130812Smarcel args); 446130812Smarcel 447130812Smarcel gdbsim_kill (); 448130812Smarcel remove_breakpoints (); 449130812Smarcel init_wait_for_inferior (); 450130812Smarcel 451130812Smarcel if (exec_file != NULL) 452130812Smarcel { 453130812Smarcel len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10; 454130812Smarcel arg_buf = (char *) alloca (len); 455130812Smarcel arg_buf[0] = '\0'; 456130812Smarcel strcat (arg_buf, exec_file); 457130812Smarcel strcat (arg_buf, " "); 458130812Smarcel strcat (arg_buf, args); 459130812Smarcel argv = buildargv (arg_buf); 460130812Smarcel make_cleanup_freeargv (argv); 461130812Smarcel } 462130812Smarcel else 463130812Smarcel argv = NULL; 464130812Smarcel sim_create_inferior (gdbsim_desc, exec_bfd, argv, env); 465130812Smarcel 466130812Smarcel inferior_ptid = pid_to_ptid (42); 467130812Smarcel insert_breakpoints (); /* Needed to get correct instruction in cache */ 468130812Smarcel 469130812Smarcel clear_proceed_status (); 470130812Smarcel 471130812Smarcel /* NB: Entry point already set by sim_create_inferior. */ 472130812Smarcel proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); 473130812Smarcel} 474130812Smarcel 475130812Smarcel/* The open routine takes the rest of the parameters from the command, 476130812Smarcel and (if successful) pushes a new target onto the stack. 477130812Smarcel Targets should supply this routine, if only to provide an error message. */ 478130812Smarcel/* Called when selecting the simulator. EG: (gdb) target sim name. */ 479130812Smarcel 480130812Smarcelstatic void 481130812Smarcelgdbsim_open (char *args, int from_tty) 482130812Smarcel{ 483130812Smarcel int len; 484130812Smarcel char *arg_buf; 485130812Smarcel char **argv; 486130812Smarcel 487130812Smarcel if (sr_get_debug ()) 488130812Smarcel printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)"); 489130812Smarcel 490130812Smarcel /* Remove current simulator if one exists. Only do this if the simulator 491130812Smarcel has been opened because sim_close requires it. 492130812Smarcel This is important because the call to push_target below will cause 493130812Smarcel sim_close to be called if the simulator is already open, but push_target 494130812Smarcel is called after sim_open! We can't move the call to push_target before 495130812Smarcel the call to sim_open because sim_open may invoke `error'. */ 496130812Smarcel if (gdbsim_desc != NULL) 497130812Smarcel unpush_target (&gdbsim_ops); 498130812Smarcel 499130812Smarcel len = (7 + 1 /* gdbsim */ 500130812Smarcel + strlen (" -E little") 501130812Smarcel + strlen (" --architecture=xxxxxxxxxx") 502130812Smarcel + (args ? strlen (args) : 0) 503130812Smarcel + 50) /* slack */ ; 504130812Smarcel arg_buf = (char *) alloca (len); 505130812Smarcel strcpy (arg_buf, "gdbsim"); /* 7 */ 506130812Smarcel /* Specify the byte order for the target when it is both selectable 507130812Smarcel and explicitly specified by the user (not auto detected). */ 508130812Smarcel switch (selected_byte_order ()) 509130812Smarcel { 510130812Smarcel case BFD_ENDIAN_BIG: 511130812Smarcel strcat (arg_buf, " -E big"); 512130812Smarcel break; 513130812Smarcel case BFD_ENDIAN_LITTLE: 514130812Smarcel strcat (arg_buf, " -E little"); 515130812Smarcel break; 516130812Smarcel case BFD_ENDIAN_UNKNOWN: 517130812Smarcel break; 518130812Smarcel } 519130812Smarcel /* Specify the architecture of the target when it has been 520130812Smarcel explicitly specified */ 521130812Smarcel if (selected_architecture_name () != NULL) 522130812Smarcel { 523130812Smarcel strcat (arg_buf, " --architecture="); 524130812Smarcel strcat (arg_buf, selected_architecture_name ()); 525130812Smarcel } 526130812Smarcel /* finally, any explicit args */ 527130812Smarcel if (args) 528130812Smarcel { 529130812Smarcel strcat (arg_buf, " "); /* 1 */ 530130812Smarcel strcat (arg_buf, args); 531130812Smarcel } 532130812Smarcel argv = buildargv (arg_buf); 533130812Smarcel if (argv == NULL) 534130812Smarcel error ("Insufficient memory available to allocate simulator arg list."); 535130812Smarcel make_cleanup_freeargv (argv); 536130812Smarcel 537130812Smarcel init_callbacks (); 538130812Smarcel gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, argv); 539130812Smarcel 540130812Smarcel if (gdbsim_desc == 0) 541130812Smarcel error ("unable to create simulator instance"); 542130812Smarcel 543130812Smarcel push_target (&gdbsim_ops); 544130812Smarcel target_fetch_registers (-1); 545130812Smarcel printf_filtered ("Connected to the simulator.\n"); 546130812Smarcel} 547130812Smarcel 548130812Smarcel/* Does whatever cleanup is required for a target that we are no longer 549130812Smarcel going to be calling. Argument says whether we are quitting gdb and 550130812Smarcel should not get hung in case of errors, or whether we want a clean 551130812Smarcel termination even if it takes a while. This routine is automatically 552130812Smarcel always called just before a routine is popped off the target stack. 553130812Smarcel Closing file descriptors and freeing memory are typical things it should 554130812Smarcel do. */ 555130812Smarcel/* Close out all files and local state before this target loses control. */ 556130812Smarcel 557130812Smarcelstatic void 558130812Smarcelgdbsim_close (int quitting) 559130812Smarcel{ 560130812Smarcel if (sr_get_debug ()) 561130812Smarcel printf_filtered ("gdbsim_close: quitting %d\n", quitting); 562130812Smarcel 563130812Smarcel program_loaded = 0; 564130812Smarcel 565130812Smarcel if (gdbsim_desc != NULL) 566130812Smarcel { 567130812Smarcel sim_close (gdbsim_desc, quitting); 568130812Smarcel gdbsim_desc = NULL; 569130812Smarcel } 570130812Smarcel 571130812Smarcel end_callbacks (); 572130812Smarcel generic_mourn_inferior (); 573130812Smarcel} 574130812Smarcel 575130812Smarcel/* Takes a program previously attached to and detaches it. 576130812Smarcel The program may resume execution (some targets do, some don't) and will 577130812Smarcel no longer stop on signals, etc. We better not have left any breakpoints 578130812Smarcel in the program or it'll die when it hits one. ARGS is arguments 579130812Smarcel typed by the user (e.g. a signal to send the process). FROM_TTY 580130812Smarcel says whether to be verbose or not. */ 581130812Smarcel/* Terminate the open connection to the remote debugger. 582130812Smarcel Use this when you want to detach and do something else with your gdb. */ 583130812Smarcel 584130812Smarcelstatic void 585130812Smarcelgdbsim_detach (char *args, int from_tty) 586130812Smarcel{ 587130812Smarcel if (sr_get_debug ()) 588130812Smarcel printf_filtered ("gdbsim_detach: args \"%s\"\n", args); 589130812Smarcel 590130812Smarcel pop_target (); /* calls gdbsim_close to do the real work */ 591130812Smarcel if (from_tty) 592130812Smarcel printf_filtered ("Ending simulator %s debugging\n", target_shortname); 593130812Smarcel} 594130812Smarcel 595130812Smarcel/* Resume execution of the target process. STEP says whether to single-step 596130812Smarcel or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given 597130812Smarcel to the target, or zero for no signal. */ 598130812Smarcel 599130812Smarcelstatic enum target_signal resume_siggnal; 600130812Smarcelstatic int resume_step; 601130812Smarcel 602130812Smarcelstatic void 603130812Smarcelgdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal) 604130812Smarcel{ 605130812Smarcel if (PIDGET (inferior_ptid) != 42) 606130812Smarcel error ("The program is not being run."); 607130812Smarcel 608130812Smarcel if (sr_get_debug ()) 609130812Smarcel printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); 610130812Smarcel 611130812Smarcel resume_siggnal = siggnal; 612130812Smarcel resume_step = step; 613130812Smarcel} 614130812Smarcel 615130812Smarcel/* Notify the simulator of an asynchronous request to stop. 616130812Smarcel 617130812Smarcel The simulator shall ensure that the stop request is eventually 618130812Smarcel delivered to the simulator. If the call is made while the 619130812Smarcel simulator is not running then the stop request is processed when 620130812Smarcel the simulator is next resumed. 621130812Smarcel 622130812Smarcel For simulators that do not support this operation, just abort */ 623130812Smarcel 624130812Smarcelstatic void 625130812Smarcelgdbsim_stop (void) 626130812Smarcel{ 627130812Smarcel if (!sim_stop (gdbsim_desc)) 628130812Smarcel { 629130812Smarcel quit (); 630130812Smarcel } 631130812Smarcel} 632130812Smarcel 633130812Smarcel/* GDB version of os_poll_quit callback. 634130812Smarcel Taken from gdb/util.c - should be in a library */ 635130812Smarcel 636130812Smarcelstatic int 637130812Smarcelgdb_os_poll_quit (host_callback *p) 638130812Smarcel{ 639130812Smarcel if (ui_loop_hook != NULL) 640130812Smarcel ui_loop_hook (0); 641130812Smarcel 642130812Smarcel if (quit_flag) /* gdb's idea of quit */ 643130812Smarcel { 644130812Smarcel quit_flag = 0; /* we've stolen it */ 645130812Smarcel return 1; 646130812Smarcel } 647130812Smarcel else if (immediate_quit) 648130812Smarcel { 649130812Smarcel return 1; 650130812Smarcel } 651130812Smarcel return 0; 652130812Smarcel} 653130812Smarcel 654130812Smarcel/* Wait for inferior process to do something. Return pid of child, 655130812Smarcel or -1 in case of error; store status through argument pointer STATUS, 656130812Smarcel just as `wait' would. */ 657130812Smarcel 658130812Smarcelstatic void 659130812Smarcelgdbsim_cntrl_c (int signo) 660130812Smarcel{ 661130812Smarcel gdbsim_stop (); 662130812Smarcel} 663130812Smarcel 664130812Smarcelstatic ptid_t 665130812Smarcelgdbsim_wait (ptid_t ptid, struct target_waitstatus *status) 666130812Smarcel{ 667130812Smarcel static RETSIGTYPE (*prev_sigint) (); 668130812Smarcel int sigrc = 0; 669130812Smarcel enum sim_stop reason = sim_running; 670130812Smarcel 671130812Smarcel if (sr_get_debug ()) 672130812Smarcel printf_filtered ("gdbsim_wait\n"); 673130812Smarcel 674130812Smarcel#if defined (HAVE_SIGACTION) && defined (SA_RESTART) 675130812Smarcel { 676130812Smarcel struct sigaction sa, osa; 677130812Smarcel sa.sa_handler = gdbsim_cntrl_c; 678130812Smarcel sigemptyset (&sa.sa_mask); 679130812Smarcel sa.sa_flags = 0; 680130812Smarcel sigaction (SIGINT, &sa, &osa); 681130812Smarcel prev_sigint = osa.sa_handler; 682130812Smarcel } 683130812Smarcel#else 684130812Smarcel prev_sigint = signal (SIGINT, gdbsim_cntrl_c); 685130812Smarcel#endif 686130812Smarcel sim_resume (gdbsim_desc, resume_step, 687130812Smarcel target_signal_to_host (resume_siggnal)); 688130812Smarcel signal (SIGINT, prev_sigint); 689130812Smarcel resume_step = 0; 690130812Smarcel 691130812Smarcel sim_stop_reason (gdbsim_desc, &reason, &sigrc); 692130812Smarcel 693130812Smarcel switch (reason) 694130812Smarcel { 695130812Smarcel case sim_exited: 696130812Smarcel status->kind = TARGET_WAITKIND_EXITED; 697130812Smarcel status->value.integer = sigrc; 698130812Smarcel break; 699130812Smarcel case sim_stopped: 700130812Smarcel switch (sigrc) 701130812Smarcel { 702130812Smarcel case SIGABRT: 703130812Smarcel quit (); 704130812Smarcel break; 705130812Smarcel case SIGINT: 706130812Smarcel case SIGTRAP: 707130812Smarcel default: 708130812Smarcel status->kind = TARGET_WAITKIND_STOPPED; 709130812Smarcel /* The signal in sigrc is a host signal. That probably 710130812Smarcel should be fixed. */ 711130812Smarcel status->value.sig = target_signal_from_host (sigrc); 712130812Smarcel break; 713130812Smarcel } 714130812Smarcel break; 715130812Smarcel case sim_signalled: 716130812Smarcel status->kind = TARGET_WAITKIND_SIGNALLED; 717130812Smarcel /* The signal in sigrc is a host signal. That probably 718130812Smarcel should be fixed. */ 719130812Smarcel status->value.sig = target_signal_from_host (sigrc); 720130812Smarcel break; 721130812Smarcel case sim_running: 722130812Smarcel case sim_polling: 723130812Smarcel /* FIXME: Is this correct? */ 724130812Smarcel break; 725130812Smarcel } 726130812Smarcel 727130812Smarcel return inferior_ptid; 728130812Smarcel} 729130812Smarcel 730130812Smarcel/* Get ready to modify the registers array. On machines which store 731130812Smarcel individual registers, this doesn't need to do anything. On machines 732130812Smarcel which store all the registers in one fell swoop, this makes sure 733130812Smarcel that registers contains all the registers from the program being 734130812Smarcel debugged. */ 735130812Smarcel 736130812Smarcelstatic void 737130812Smarcelgdbsim_prepare_to_store (void) 738130812Smarcel{ 739130812Smarcel /* Do nothing, since we can store individual regs */ 740130812Smarcel} 741130812Smarcel 742130812Smarcel/* Transfer LEN bytes between GDB address MYADDR and target address 743130812Smarcel MEMADDR. If WRITE is non-zero, transfer them to the target, 744130812Smarcel otherwise transfer them from the target. TARGET is unused. 745130812Smarcel 746130812Smarcel Returns the number of bytes transferred. */ 747130812Smarcel 748130812Smarcelstatic int 749130812Smarcelgdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len, 750130812Smarcel int write, struct mem_attrib *attrib, 751130812Smarcel struct target_ops *target) 752130812Smarcel{ 753130812Smarcel if (!program_loaded) 754130812Smarcel error ("No program loaded."); 755130812Smarcel 756130812Smarcel if (sr_get_debug ()) 757130812Smarcel { 758130812Smarcel /* FIXME: Send to something other than STDOUT? */ 759130812Smarcel printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x"); 760130812Smarcel gdb_print_host_address (myaddr, gdb_stdout); 761130812Smarcel printf_filtered (", memaddr 0x%s, len %d, write %d\n", 762130812Smarcel paddr_nz (memaddr), len, write); 763130812Smarcel if (sr_get_debug () && write) 764130812Smarcel dump_mem (myaddr, len); 765130812Smarcel } 766130812Smarcel 767130812Smarcel if (write) 768130812Smarcel { 769130812Smarcel len = sim_write (gdbsim_desc, memaddr, myaddr, len); 770130812Smarcel } 771130812Smarcel else 772130812Smarcel { 773130812Smarcel len = sim_read (gdbsim_desc, memaddr, myaddr, len); 774130812Smarcel if (sr_get_debug () && len > 0) 775130812Smarcel dump_mem (myaddr, len); 776130812Smarcel } 777130812Smarcel return len; 778130812Smarcel} 779130812Smarcel 780130812Smarcelstatic void 781130812Smarcelgdbsim_files_info (struct target_ops *target) 782130812Smarcel{ 783130812Smarcel char *file = "nothing"; 784130812Smarcel 785130812Smarcel if (exec_bfd) 786130812Smarcel file = bfd_get_filename (exec_bfd); 787130812Smarcel 788130812Smarcel if (sr_get_debug ()) 789130812Smarcel printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); 790130812Smarcel 791130812Smarcel if (exec_bfd) 792130812Smarcel { 793130812Smarcel printf_filtered ("\tAttached to %s running program %s\n", 794130812Smarcel target_shortname, file); 795130812Smarcel sim_info (gdbsim_desc, 0); 796130812Smarcel } 797130812Smarcel} 798130812Smarcel 799130812Smarcel/* Clear the simulator's notion of what the break points are. */ 800130812Smarcel 801130812Smarcelstatic void 802130812Smarcelgdbsim_mourn_inferior (void) 803130812Smarcel{ 804130812Smarcel if (sr_get_debug ()) 805130812Smarcel printf_filtered ("gdbsim_mourn_inferior:\n"); 806130812Smarcel 807130812Smarcel remove_breakpoints (); 808130812Smarcel generic_mourn_inferior (); 809130812Smarcel} 810130812Smarcel 811130812Smarcelstatic int 812130812Smarcelgdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache) 813130812Smarcel{ 814130812Smarcel return memory_insert_breakpoint (addr, contents_cache); 815130812Smarcel} 816130812Smarcel 817130812Smarcelstatic int 818130812Smarcelgdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache) 819130812Smarcel{ 820130812Smarcel return memory_remove_breakpoint (addr, contents_cache); 821130812Smarcel} 822130812Smarcel 823130812Smarcel/* Pass the command argument through to the simulator verbatim. The 824130812Smarcel simulator must do any command interpretation work. */ 825130812Smarcel 826130812Smarcelvoid 827130812Smarcelsimulator_command (char *args, int from_tty) 828130812Smarcel{ 829130812Smarcel if (gdbsim_desc == NULL) 830130812Smarcel { 831130812Smarcel 832130812Smarcel /* PREVIOUSLY: The user may give a command before the simulator 833130812Smarcel is opened. [...] (??? assuming of course one wishes to 834130812Smarcel continue to allow commands to be sent to unopened simulators, 835130812Smarcel which isn't entirely unreasonable). */ 836130812Smarcel 837130812Smarcel /* The simulator is a builtin abstraction of a remote target. 838130812Smarcel Consistent with that model, access to the simulator, via sim 839130812Smarcel commands, is restricted to the period when the channel to the 840130812Smarcel simulator is open. */ 841130812Smarcel 842130812Smarcel error ("Not connected to the simulator target"); 843130812Smarcel } 844130812Smarcel 845130812Smarcel sim_do_command (gdbsim_desc, args); 846130812Smarcel 847130812Smarcel /* Invalidate the register cache, in case the simulator command does 848130812Smarcel something funny. */ 849130812Smarcel registers_changed (); 850130812Smarcel} 851130812Smarcel 852130812Smarcel/* Define the target subroutine names */ 853130812Smarcel 854130812Smarcelstruct target_ops gdbsim_ops; 855130812Smarcel 856130812Smarcelstatic void 857130812Smarcelinit_gdbsim_ops (void) 858130812Smarcel{ 859130812Smarcel gdbsim_ops.to_shortname = "sim"; 860130812Smarcel gdbsim_ops.to_longname = "simulator"; 861130812Smarcel gdbsim_ops.to_doc = "Use the compiled-in simulator."; 862130812Smarcel gdbsim_ops.to_open = gdbsim_open; 863130812Smarcel gdbsim_ops.to_close = gdbsim_close; 864130812Smarcel gdbsim_ops.to_detach = gdbsim_detach; 865130812Smarcel gdbsim_ops.to_resume = gdbsim_resume; 866130812Smarcel gdbsim_ops.to_wait = gdbsim_wait; 867130812Smarcel gdbsim_ops.to_fetch_registers = gdbsim_fetch_register; 868130812Smarcel gdbsim_ops.to_store_registers = gdbsim_store_register; 869130812Smarcel gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store; 870130812Smarcel gdbsim_ops.to_xfer_memory = gdbsim_xfer_inferior_memory; 871130812Smarcel gdbsim_ops.to_files_info = gdbsim_files_info; 872130812Smarcel gdbsim_ops.to_insert_breakpoint = gdbsim_insert_breakpoint; 873130812Smarcel gdbsim_ops.to_remove_breakpoint = gdbsim_remove_breakpoint; 874130812Smarcel gdbsim_ops.to_kill = gdbsim_kill; 875130812Smarcel gdbsim_ops.to_load = gdbsim_load; 876130812Smarcel gdbsim_ops.to_create_inferior = gdbsim_create_inferior; 877130812Smarcel gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior; 878130812Smarcel gdbsim_ops.to_stop = gdbsim_stop; 879130812Smarcel gdbsim_ops.to_stratum = process_stratum; 880130812Smarcel gdbsim_ops.to_has_all_memory = 1; 881130812Smarcel gdbsim_ops.to_has_memory = 1; 882130812Smarcel gdbsim_ops.to_has_stack = 1; 883130812Smarcel gdbsim_ops.to_has_registers = 1; 884130812Smarcel gdbsim_ops.to_has_execution = 1; 885130812Smarcel gdbsim_ops.to_magic = OPS_MAGIC; 886130812Smarcel 887130812Smarcel#ifdef TARGET_REDEFINE_DEFAULT_OPS 888130812Smarcel TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops); 889130812Smarcel#endif 890130812Smarcel} 891130812Smarcel 892130812Smarcelvoid 893130812Smarcel_initialize_remote_sim (void) 894130812Smarcel{ 895130812Smarcel init_gdbsim_ops (); 896130812Smarcel add_target (&gdbsim_ops); 897130812Smarcel 898130812Smarcel add_com ("sim <command>", class_obscure, simulator_command, 899130812Smarcel "Send a command to the simulator."); 900130812Smarcel} 901