| 1 | /* New version of run front end support for simulators. |
| 2 | Copyright (C) 1997, 2004 Free Software Foundation, Inc. |
| 3 | |
| 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) |
| 7 | any later version. |
| 8 | |
| 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. |
| 13 | |
| 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. */ |
| 17 | |
| 18 | #include <signal.h> |
| 19 | #include "sim-main.h" |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | |
| 23 | #ifdef HAVE_ENVIRON |
| 24 | extern char **environ; |
| 25 | #endif |
| 26 | |
| 27 | #ifdef HAVE_UNISTD_H |
| 28 | /* For chdir. */ |
| 29 | #include <unistd.h> |
| 30 | #endif |
| 31 | |
| 32 | static void usage (void); |
| 33 | |
| 34 | extern host_callback default_callback; |
| 35 | |
| 36 | static char *myname; |
| 37 | |
| 38 | static SIM_DESC sd; |
| 39 | |
| 40 | static RETSIGTYPE |
| 41 | cntrl_c (int sig) |
| 42 | { |
| 43 | if (! sim_stop (sd)) |
| 44 | { |
| 45 | fprintf (stderr, "Quit!\n"); |
| 46 | exit (1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | main (int argc, char **argv) |
| 52 | { |
| 53 | char *name; |
| 54 | char **prog_argv = NULL; |
| 55 | struct bfd *prog_bfd; |
| 56 | enum sim_stop reason; |
| 57 | int sigrc = 0; |
| 58 | int single_step = 0; |
| 59 | RETSIGTYPE (*prev_sigint) (); |
| 60 | |
| 61 | myname = argv[0] + strlen (argv[0]); |
| 62 | while (myname > argv[0] && myname[-1] != '/') |
| 63 | --myname; |
| 64 | |
| 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) |
| 70 | { |
| 71 | single_step = 1; |
| 72 | } |
| 73 | |
| 74 | /* Create an instance of the simulator. */ |
| 75 | default_callback.init (&default_callback); |
| 76 | sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv); |
| 77 | if (sd == 0) |
| 78 | exit (1); |
| 79 | if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER) |
| 80 | { |
| 81 | fprintf (stderr, "Internal error - bad magic number in simulator struct\n"); |
| 82 | abort (); |
| 83 | } |
| 84 | |
| 85 | /* Was there a program to run? */ |
| 86 | prog_argv = STATE_PROG_ARGV (sd); |
| 87 | prog_bfd = STATE_PROG_BFD (sd); |
| 88 | if (prog_argv == NULL || *prog_argv == NULL) |
| 89 | usage (); |
| 90 | |
| 91 | name = *prog_argv; |
| 92 | |
| 93 | /* For simulators that don't open prog during sim_open() */ |
| 94 | if (prog_bfd == NULL) |
| 95 | { |
| 96 | prog_bfd = bfd_openr (name, 0); |
| 97 | if (prog_bfd == NULL) |
| 98 | { |
| 99 | fprintf (stderr, "%s: can't open \"%s\": %s\n", |
| 100 | myname, name, bfd_errmsg (bfd_get_error ())); |
| 101 | exit (1); |
| 102 | } |
| 103 | if (!bfd_check_format (prog_bfd, bfd_object)) |
| 104 | { |
| 105 | fprintf (stderr, "%s: \"%s\" is not an object file: %s\n", |
| 106 | myname, name, bfd_errmsg (bfd_get_error ())); |
| 107 | exit (1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (STATE_VERBOSE_P (sd)) |
| 112 | printf ("%s %s\n", myname, name); |
| 113 | |
| 114 | /* Load the program into the simulator. */ |
| 115 | if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL) |
| 116 | exit (1); |
| 117 | |
| 118 | /* Prepare the program for execution. */ |
| 119 | #ifdef HAVE_ENVIRON |
| 120 | sim_create_inferior (sd, prog_bfd, prog_argv, environ); |
| 121 | #else |
| 122 | sim_create_inferior (sd, prog_bfd, prog_argv, NULL); |
| 123 | #endif |
| 124 | |
| 125 | /* To accommodate relative file paths, chdir to sysroot now. We |
| 126 | mustn't do this until BFD has opened the program, else we wouldn't |
| 127 | find the executable if it has a relative file path. */ |
| 128 | if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0) |
| 129 | { |
| 130 | fprintf (stderr, "%s: can't change directory to \"%s\"\n", |
| 131 | myname, simulator_sysroot); |
| 132 | exit (1); |
| 133 | } |
| 134 | |
| 135 | /* Run/Step the program. */ |
| 136 | if (single_step) |
| 137 | { |
| 138 | do |
| 139 | { |
| 140 | prev_sigint = signal (SIGINT, cntrl_c); |
| 141 | sim_resume (sd, 1/*step*/, 0); |
| 142 | signal (SIGINT, prev_sigint); |
| 143 | sim_stop_reason (sd, &reason, &sigrc); |
| 144 | |
| 145 | if ((reason == sim_stopped) && |
| 146 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) |
| 147 | break; /* exit on control-C */ |
| 148 | } |
| 149 | /* remain on breakpoint or signals in oe mode*/ |
| 150 | while (((reason == sim_signalled) && |
| 151 | (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) || |
| 152 | ((reason == sim_stopped) && |
| 153 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT))); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | do |
| 158 | { |
| 159 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) |
| 160 | struct sigaction sa, osa; |
| 161 | sa.sa_handler = cntrl_c; |
| 162 | sigemptyset (&sa.sa_mask); |
| 163 | sa.sa_flags = 0; |
| 164 | sigaction (SIGINT, &sa, &osa); |
| 165 | prev_sigint = osa.sa_handler; |
| 166 | #else |
| 167 | prev_sigint = signal (SIGINT, cntrl_c); |
| 168 | #endif |
| 169 | sim_resume (sd, 0, sigrc); |
| 170 | signal (SIGINT, prev_sigint); |
| 171 | sim_stop_reason (sd, &reason, &sigrc); |
| 172 | |
| 173 | if ((reason == sim_stopped) && |
| 174 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) |
| 175 | break; /* exit on control-C */ |
| 176 | |
| 177 | /* remain on signals in oe mode */ |
| 178 | } while ((reason == sim_stopped) && |
| 179 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)); |
| 180 | |
| 181 | } |
| 182 | /* Print any stats the simulator collected. */ |
| 183 | if (STATE_VERBOSE_P (sd)) |
| 184 | sim_info (sd, 0); |
| 185 | |
| 186 | /* Shutdown the simulator. */ |
| 187 | sim_close (sd, 0); |
| 188 | |
| 189 | /* If reason is sim_exited, then sigrc holds the exit code which we want |
| 190 | to return. If reason is sim_stopped or sim_signalled, then sigrc holds |
| 191 | the signal that the simulator received; we want to return that to |
| 192 | indicate failure. */ |
| 193 | |
| 194 | /* Why did we stop? */ |
| 195 | switch (reason) |
| 196 | { |
| 197 | case sim_signalled: |
| 198 | case sim_stopped: |
| 199 | if (sigrc != 0) |
| 200 | fprintf (stderr, "program stopped with signal %d.\n", sigrc); |
| 201 | break; |
| 202 | |
| 203 | case sim_exited: |
| 204 | break; |
| 205 | |
| 206 | default: |
| 207 | fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc); |
| 208 | break; |
| 209 | |
| 210 | } |
| 211 | |
| 212 | return sigrc; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | usage () |
| 217 | { |
| 218 | fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); |
| 219 | fprintf (stderr, "Run `%s --help' for full list of options.\n", myname); |
| 220 | exit (1); |
| 221 | } |