Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* New version of run front end support for simulators. |
2 | Copyright (C) 1997 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 | static void usage (void); | |
28 | ||
29 | extern host_callback default_callback; | |
30 | ||
31 | static char *myname; | |
32 | ||
33 | static SIM_DESC sd; | |
34 | ||
35 | static RETSIGTYPE | |
36 | cntrl_c (int sig) | |
37 | { | |
38 | if (! sim_stop (sd)) | |
39 | { | |
40 | fprintf (stderr, "Quit!\n"); | |
41 | exit (1); | |
42 | } | |
43 | } | |
44 | ||
45 | int | |
46 | main (int argc, char **argv) | |
47 | { | |
48 | char *name; | |
49 | char **prog_argv = NULL; | |
50 | struct _bfd *prog_bfd; | |
51 | enum sim_stop reason; | |
52 | int sigrc = 0; | |
53 | int single_step = 0; | |
54 | RETSIGTYPE (*prev_sigint) (); | |
55 | ||
56 | myname = argv[0] + strlen (argv[0]); | |
57 | while (myname > argv[0] && myname[-1] != '/') | |
58 | --myname; | |
59 | ||
60 | /* INTERNAL: When MYNAME is `step', single step the simulator | |
61 | instead of allowing it to run free. The sole purpose of this | |
62 | HACK is to allow the sim_resume interface's step argument to be | |
63 | tested without having to build/run gdb. */ | |
64 | if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0) | |
65 | { | |
66 | single_step = 1; | |
67 | } | |
68 | ||
69 | /* Create an instance of the simulator. */ | |
70 | default_callback.init (&default_callback); | |
71 | sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv); | |
72 | if (sd == 0) | |
73 | exit (1); | |
74 | if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER) | |
75 | { | |
76 | fprintf (stderr, "Internal error - bad magic number in simulator struct\n"); | |
77 | abort (); | |
78 | } | |
79 | ||
80 | /* Was there a program to run? */ | |
81 | prog_argv = STATE_PROG_ARGV (sd); | |
82 | prog_bfd = STATE_PROG_BFD (sd); | |
83 | if (prog_argv == NULL || *prog_argv == NULL) | |
84 | usage (); | |
85 | ||
86 | name = *prog_argv; | |
87 | ||
88 | /* For simulators that don't open prog during sim_open() */ | |
89 | if (prog_bfd == NULL) | |
90 | { | |
91 | prog_bfd = bfd_openr (name, 0); | |
92 | if (prog_bfd == NULL) | |
93 | { | |
94 | fprintf (stderr, "%s: can't open \"%s\": %s\n", | |
95 | myname, name, bfd_errmsg (bfd_get_error ())); | |
96 | exit (1); | |
97 | } | |
98 | if (!bfd_check_format (prog_bfd, bfd_object)) | |
99 | { | |
100 | fprintf (stderr, "%s: \"%s\" is not an object file: %s\n", | |
101 | myname, name, bfd_errmsg (bfd_get_error ())); | |
102 | exit (1); | |
103 | } | |
104 | } | |
105 | ||
106 | if (STATE_VERBOSE_P (sd)) | |
107 | printf ("%s %s\n", myname, name); | |
108 | ||
109 | /* Load the program into the simulator. */ | |
110 | if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL) | |
111 | exit (1); | |
112 | ||
113 | /* Prepare the program for execution. */ | |
114 | #ifdef HAVE_ENVIRON | |
115 | sim_create_inferior (sd, prog_bfd, prog_argv, environ); | |
116 | #else | |
117 | sim_create_inferior (sd, prog_bfd, prog_argv, NULL); | |
118 | #endif | |
119 | ||
120 | /* Run/Step the program. */ | |
121 | if (single_step) | |
122 | { | |
123 | do | |
124 | { | |
125 | prev_sigint = signal (SIGINT, cntrl_c); | |
126 | sim_resume (sd, 1/*step*/, 0); | |
127 | signal (SIGINT, prev_sigint); | |
128 | sim_stop_reason (sd, &reason, &sigrc); | |
129 | ||
130 | if ((reason == sim_stopped) && | |
131 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) | |
132 | break; /* exit on control-C */ | |
133 | } | |
134 | /* remain on breakpoint or signals in oe mode*/ | |
135 | while (((reason == sim_signalled) && | |
136 | (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) || | |
137 | ((reason == sim_stopped) && | |
138 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT))); | |
139 | } | |
43e526b9 | 140 | else |
c906108c | 141 | { |
43e526b9 JM |
142 | do |
143 | { | |
c906108c | 144 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) |
43e526b9 JM |
145 | struct sigaction sa, osa; |
146 | sa.sa_handler = cntrl_c; | |
147 | sigemptyset (&sa.sa_mask); | |
148 | sa.sa_flags = 0; | |
149 | sigaction (SIGINT, &sa, &osa); | |
150 | prev_sigint = osa.sa_handler; | |
c906108c | 151 | #else |
43e526b9 | 152 | prev_sigint = signal (SIGINT, cntrl_c); |
c906108c | 153 | #endif |
43e526b9 JM |
154 | sim_resume (sd, 0, sigrc); |
155 | signal (SIGINT, prev_sigint); | |
156 | sim_stop_reason (sd, &reason, &sigrc); | |
157 | ||
158 | if ((reason == sim_stopped) && | |
159 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) | |
160 | break; /* exit on control-C */ | |
161 | ||
162 | /* remain on signals in oe mode */ | |
163 | } while ((reason == sim_stopped) && | |
164 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)); | |
165 | ||
166 | } | |
c906108c | 167 | /* Print any stats the simulator collected. */ |
f9cbceb6 AC |
168 | if (STATE_VERBOSE_P (sd)) |
169 | sim_info (sd, 0); | |
43e526b9 | 170 | |
c906108c SS |
171 | /* Shutdown the simulator. */ |
172 | sim_close (sd, 0); | |
43e526b9 | 173 | |
c906108c SS |
174 | /* If reason is sim_exited, then sigrc holds the exit code which we want |
175 | to return. If reason is sim_stopped or sim_signalled, then sigrc holds | |
176 | the signal that the simulator received; we want to return that to | |
177 | indicate failure. */ | |
43e526b9 | 178 | |
c906108c SS |
179 | #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */ |
180 | if (sigrc == SIGILL) | |
181 | abort (); | |
182 | sigrc = 0; | |
183 | #else | |
184 | /* Why did we stop? */ | |
185 | switch (reason) | |
186 | { | |
187 | case sim_signalled: | |
188 | case sim_stopped: | |
189 | if (sigrc != 0) | |
190 | fprintf (stderr, "program stopped with signal %d.\n", sigrc); | |
191 | break; | |
192 | ||
193 | case sim_exited: | |
194 | break; | |
195 | ||
196 | default: | |
197 | fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc); | |
198 | break; | |
199 | ||
200 | } | |
201 | #endif | |
202 | ||
203 | return sigrc; | |
204 | } | |
205 | ||
206 | static void | |
207 | usage () | |
208 | { | |
209 | fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); | |
210 | fprintf (stderr, "Run `%s --help' for full list of options.\n", myname); | |
211 | exit (1); | |
212 | } |