1 /* New version of run front end support for simulators.
2 Copyright (C) 1997, 2004, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 extern char **environ;
32 static void usage (void);
34 extern host_callback default_callback;
45 fprintf (stderr, "Quit!\n");
51 main (int argc, char **argv)
54 char **prog_argv = NULL;
59 RETSIGTYPE (*prev_sigint) ();
61 myname = argv[0] + strlen (argv[0]);
62 while (myname > argv[0] && myname[-1] != '/')
65 /* INTERNAL: When MYNAME is `step', single step the simulator
66 instead of allowing it to run free. The sole purpose of this
67 HACK is to allow the sim_resume interface's step argument to be
68 tested without having to build/run gdb. */
69 if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
74 /* Create an instance of the simulator. */
75 default_callback.init (&default_callback);
76 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
79 if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
81 fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
85 /* We can't set the endianness in the callback structure until
86 sim_config is called, which happens in sim_open. */
87 default_callback.target_endian
88 = (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN
89 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
91 /* Was there a program to run? */
92 prog_argv = STATE_PROG_ARGV (sd);
93 prog_bfd = STATE_PROG_BFD (sd);
94 if (prog_argv == NULL || *prog_argv == NULL)
99 /* For simulators that don't open prog during sim_open() */
100 if (prog_bfd == NULL)
102 prog_bfd = bfd_openr (name, 0);
103 if (prog_bfd == NULL)
105 fprintf (stderr, "%s: can't open \"%s\": %s\n",
106 myname, name, bfd_errmsg (bfd_get_error ()));
109 if (!bfd_check_format (prog_bfd, bfd_object))
111 fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
112 myname, name, bfd_errmsg (bfd_get_error ()));
117 if (STATE_VERBOSE_P (sd))
118 printf ("%s %s\n", myname, name);
120 /* Load the program into the simulator. */
121 if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
124 /* Prepare the program for execution. */
126 sim_create_inferior (sd, prog_bfd, prog_argv, environ);
128 sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
131 /* To accommodate relative file paths, chdir to sysroot now. We
132 mustn't do this until BFD has opened the program, else we wouldn't
133 find the executable if it has a relative file path. */
134 if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
136 fprintf (stderr, "%s: can't change directory to \"%s\"\n",
137 myname, simulator_sysroot);
141 /* Run/Step the program. */
146 prev_sigint = signal (SIGINT, cntrl_c);
147 sim_resume (sd, 1/*step*/, 0);
148 signal (SIGINT, prev_sigint);
149 sim_stop_reason (sd, &reason, &sigrc);
151 if ((reason == sim_stopped) &&
152 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
153 break; /* exit on control-C */
155 /* remain on breakpoint or signals in oe mode*/
156 while (((reason == sim_signalled) &&
157 (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
158 ((reason == sim_stopped) &&
159 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
165 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
166 struct sigaction sa, osa;
167 sa.sa_handler = cntrl_c;
168 sigemptyset (&sa.sa_mask);
170 sigaction (SIGINT, &sa, &osa);
171 prev_sigint = osa.sa_handler;
173 prev_sigint = signal (SIGINT, cntrl_c);
175 sim_resume (sd, 0, sigrc);
176 signal (SIGINT, prev_sigint);
177 sim_stop_reason (sd, &reason, &sigrc);
179 if ((reason == sim_stopped) &&
180 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
181 break; /* exit on control-C */
183 /* remain on signals in oe mode */
184 } while ((reason == sim_stopped) &&
185 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
188 /* Print any stats the simulator collected. */
189 if (STATE_VERBOSE_P (sd))
192 /* Shutdown the simulator. */
195 /* If reason is sim_exited, then sigrc holds the exit code which we want
196 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
197 the signal that the simulator received; we want to return that to
200 /* Why did we stop? */
206 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
213 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
224 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
225 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);