1/* run front end support for all the simulators. 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 3 2002, 2003, 2004, 2007 Free Software Foundation, Inc. 4 5This program is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License as published by 7the Free Software Foundation; either version 3 of the License, or 8(at your option) any later version. 9 10This program is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18/* Steve Chamberlain sac@cygnus.com, 19 and others at Cygnus. */ 20 21#ifdef HAVE_CONFIG_H 22#include "cconfig.h" 23#include "tconfig.h" 24#endif 25 26#include <signal.h> 27#include <stdio.h> 28#ifdef __STDC__ 29#include <stdarg.h> 30#else 31#include <varargs.h> 32#endif 33 34#ifdef HAVE_STDLIB_H 35#include <stdlib.h> 36#endif 37 38#ifdef HAVE_STRING_H 39#include <string.h> 40#else 41#ifdef HAVE_STRINGS_H 42#include <strings.h> 43#endif 44#endif 45 46#include "libiberty.h" 47#include "bfd.h" 48#include "gdb/callback.h" 49#include "gdb/remote-sim.h" 50#include "ansidecl.h" 51#include "run-sim.h" 52 53static void usage PARAMS ((void)); 54extern int optind; 55extern char *optarg; 56 57extern host_callback default_callback; 58 59static char *myname; 60 61extern int getopt (); 62 63#ifdef NEED_UI_LOOP_HOOK 64/* Gdb foolery. This is only needed for gdb using a gui. */ 65int (*deprecated_ui_loop_hook) PARAMS ((int signo)); 66#endif 67 68static SIM_DESC sd; 69 70static RETSIGTYPE 71cntrl_c (int sig ATTRIBUTE_UNUSED) 72{ 73 if (! sim_stop (sd)) 74 { 75 fprintf (stderr, "Quit!\n"); 76 exit (1); 77 } 78} 79 80int 81main (ac, av) 82 int ac; 83 char **av; 84{ 85 RETSIGTYPE (*prev_sigint) (); 86 bfd *abfd; 87 int i; 88 int verbose = 0; 89 int trace = 0; 90#ifdef SIM_HAVE_ENVIRONMENT 91 int operating_p = 0; 92#endif 93 char *name; 94 static char *no_args[4]; 95 char **sim_argv = &no_args[0]; 96 char **prog_args; 97 enum sim_stop reason; 98 int sigrc; 99 100 myname = av[0] + strlen (av[0]); 101 while (myname > av[0] && myname[-1] != '/') 102 --myname; 103 104 /* The first element of sim_open's argv is the program name. */ 105 no_args[0] = av[0]; 106#ifdef SIM_HAVE_BIENDIAN 107 no_args[1] = "-E"; 108 no_args[2] = "set-later"; 109#endif 110 111 /* FIXME: This is currently being migrated into sim_open. 112 Simulators that use functions such as sim_size() still require 113 this. */ 114 default_callback.init (&default_callback); 115 sim_set_callbacks (&default_callback); 116 117#ifdef SIM_TARGET_SWITCHES 118 ac = sim_target_parse_command_line (ac, av); 119#endif 120 121 /* FIXME: This is currently being rewritten to have each simulator 122 do all argv processing. */ 123 124 while ((i = getopt (ac, av, "a:c:m:op:s:tv")) != EOF) 125 switch (i) 126 { 127 case 'a': 128 /* FIXME: Temporary hack. */ 129 { 130 int len = strlen (av[0]) + strlen (optarg); 131 char *argbuf = (char *) alloca (len + 2 + 50); 132 sprintf (argbuf, "%s %s", av[0], optarg); 133#ifdef SIM_HAVE_BIENDIAN 134 /* The desired endianness must be passed to sim_open. 135 The value for "set-later" is set when we know what it is. 136 -E support isn't yet part of the published interface. */ 137 strcat (argbuf, " -E set-later"); 138#endif 139 sim_argv = buildargv (argbuf); 140 } 141 break; 142#ifdef SIM_HAVE_SIMCACHE 143 case 'c': 144 sim_set_simcache_size (atoi (optarg)); 145 break; 146#endif 147 case 'm': 148 /* FIXME: Rename to sim_set_mem_size. */ 149 sim_size (atoi (optarg)); 150 break; 151#ifdef SIM_HAVE_ENVIRONMENT 152 case 'o': 153 /* Operating enironment where any signals are delivered to the 154 target. */ 155 operating_p = 1; 156 break; 157#endif 158#ifdef SIM_HAVE_PROFILE 159 case 'p': 160 sim_set_profile (atoi (optarg)); 161 break; 162 case 's': 163 sim_set_profile_size (atoi (optarg)); 164 break; 165#endif 166 case 't': 167 trace = 1; 168 break; 169 case 'v': 170 /* Things that are printed with -v are the kinds of things that 171 gcc -v prints. This is not meant to include detailed tracing 172 or debugging information, just summaries. */ 173 verbose = 1; 174 /* sim_set_verbose (1); */ 175 break; 176 /* FIXME: Quick hack, to be replaced by more general facility. */ 177 default: 178 usage (); 179 } 180 181 ac -= optind; 182 av += optind; 183 if (ac <= 0) 184 usage (); 185 186 name = *av; 187 prog_args = av; 188 189 if (verbose) 190 { 191 printf ("%s %s\n", myname, name); 192 } 193 194 abfd = bfd_openr (name, 0); 195 if (!abfd) 196 { 197 fprintf (stderr, "%s: can't open %s: %s\n", 198 myname, name, bfd_errmsg (bfd_get_error ())); 199 exit (1); 200 } 201 202 if (!bfd_check_format (abfd, bfd_object)) 203 { 204 fprintf (stderr, "%s: can't load %s: %s\n", 205 myname, name, bfd_errmsg (bfd_get_error ())); 206 exit (1); 207 } 208 209#ifdef SIM_HAVE_BIENDIAN 210 /* The endianness must be passed to sim_open because one may wish to 211 examine/set registers before calling sim_load [which is the other 212 place where one can determine endianness]. We previously passed the 213 endianness via global `target_byte_order' but that's not a clean 214 interface. */ 215 for (i = 1; sim_argv[i + 1] != NULL; ++i) 216 continue; 217 if (bfd_big_endian (abfd)) 218 sim_argv[i] = "big"; 219 else 220 sim_argv[i] = "little"; 221#endif 222 223 /* Ensure that any run-time initialisation that needs to be 224 performed by the simulator can occur. */ 225 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv); 226 if (sd == 0) 227 exit (1); 228 229 if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL) 230 exit (1); 231 232 if (sim_create_inferior (sd, abfd, prog_args, NULL) == SIM_RC_FAIL) 233 exit (1); 234 235#ifdef SIM_HAVE_ENVIRONMENT 236 /* NOTE: An old simulator supporting the operating environment MUST 237 provide sim_set_trace() and not sim_trace(). That way 238 sim_stop_reason() can be used to determine any stop reason. */ 239 if (trace) 240 sim_set_trace (); 241 sigrc = 0; 242 do 243 { 244 prev_sigint = signal (SIGINT, cntrl_c); 245 sim_resume (sd, 0, sigrc); 246 signal (SIGINT, prev_sigint); 247 sim_stop_reason (sd, &reason, &sigrc); 248 } 249 while (operating_p && reason == sim_stopped && sigrc != SIGINT); 250#else 251 if (trace) 252 { 253 int done = 0; 254 prev_sigint = signal (SIGINT, cntrl_c); 255 while (!done) 256 { 257 done = sim_trace (sd); 258 } 259 signal (SIGINT, prev_sigint); 260 sim_stop_reason (sd, &reason, &sigrc); 261 } 262 else 263 { 264 prev_sigint = signal (SIGINT, cntrl_c); 265 sigrc = 0; 266 sim_resume (sd, 0, sigrc); 267 signal (SIGINT, prev_sigint); 268 sim_stop_reason (sd, &reason, &sigrc); 269 } 270#endif 271 272 if (verbose) 273 sim_info (sd, 0); 274 sim_close (sd, 0); 275 276 /* If reason is sim_exited, then sigrc holds the exit code which we want 277 to return. If reason is sim_stopped or sim_signalled, then sigrc holds 278 the signal that the simulator received; we want to return that to 279 indicate failure. */ 280 281 /* Why did we stop? */ 282 switch (reason) 283 { 284 case sim_signalled: 285 case sim_stopped: 286 if (sigrc != 0) 287 fprintf (stderr, "program stopped with signal %d.\n", sigrc); 288 break; 289 290 case sim_exited: 291 break; 292 293 case sim_running: 294 case sim_polling: /* These indicate a serious problem. */ 295 abort (); 296 break; 297 298 } 299 300 return sigrc; 301} 302 303static void 304usage () 305{ 306 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); 307 fprintf (stderr, "Options:\n"); 308 fprintf (stderr, "-a args Pass `args' to simulator.\n"); 309#ifdef SIM_HAVE_SIMCACHE 310 fprintf (stderr, "-c size Set simulator cache size to `size'.\n"); 311#endif 312 fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n"); 313#ifdef SIM_HAVE_ENVIRONMENT 314 fprintf (stderr, "-o Select operating (kernel) environment.\n"); 315#endif 316#ifdef SIM_HAVE_PROFILE 317 fprintf (stderr, "-p freq Set profiling frequency.\n"); 318 fprintf (stderr, "-s size Set profiling size.\n"); 319#endif 320 fprintf (stderr, "-t Perform instruction tracing.\n"); 321 fprintf (stderr, " Note: Very few simulators support tracing.\n"); 322 fprintf (stderr, "-v Verbose output.\n"); 323 fprintf (stderr, "\n"); 324 fprintf (stderr, "program args Arguments to pass to simulated program.\n"); 325 fprintf (stderr, " Note: Very few simulators support this.\n"); 326#ifdef SIM_TARGET_SWITCHES 327 fprintf (stderr, "\nTarget specific options:\n"); 328 sim_target_display_usage (); 329#endif 330 exit (1); 331} 332