* A few more improvements to gx jit prototype.
[deliverable/binutils-gdb.git] / sim / common / nrun.c
CommitLineData
c95d08a8
DE
1/* New version of run front end support for simulators.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2, or (at your option)
7any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along
15with this program; if not, write to the Free Software Foundation, Inc.,
1659 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
247fccde 18#include <signal.h>
c95d08a8
DE
19#include "sim-main.h"
20
897a1d78
DE
21#include "bfd.h"
22
c95d08a8
DE
23#ifdef HAVE_ENVIRON
24extern char **environ;
25#endif
26
247fccde 27static void usage (void);
c95d08a8
DE
28
29extern host_callback default_callback;
30
31static char *myname;
32
247fccde
AC
33static SIM_DESC sd;
34
35static RETSIGTYPE
36cntrl_c (int sig)
37{
38 if (! sim_stop (sd))
39 {
40 fprintf (stderr, "Quit!\n");
41 exit (1);
42 }
43}
44
c95d08a8 45int
247fccde 46main (int argc, char **argv)
c95d08a8
DE
47{
48 char *name;
49 char **prog_argv = NULL;
897a1d78 50 struct _bfd *prog_bfd;
c95d08a8
DE
51 enum sim_stop reason;
52 int sigrc;
247fccde 53 RETSIGTYPE (*prev_sigint) ();
c95d08a8
DE
54
55 myname = argv[0] + strlen (argv[0]);
56 while (myname > argv[0] && myname[-1] != '/')
57 --myname;
58
c95d08a8 59 /* Create an instance of the simulator. */
247fccde
AC
60 default_callback.init (&default_callback);
61 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
c95d08a8
DE
62 if (sd == 0)
63 exit (1);
247fccde
AC
64 if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
65 {
66 fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
67 abort ();
68 }
c95d08a8
DE
69
70 /* Was there a program to run? */
71 prog_argv = STATE_PROG_ARGV (sd);
897a1d78 72 prog_bfd = STATE_PROG_BFD (sd);
c95d08a8
DE
73 if (prog_argv == NULL || *prog_argv == NULL)
74 usage ();
75
76 name = *prog_argv;
77
897a1d78
DE
78 /* For simulators that don't open prog during sim_open() */
79 if (prog_bfd == NULL)
80 {
81 prog_bfd = bfd_openr (name, 0);
82 if (prog_bfd == NULL)
83 fprintf (stderr, "%s: can't open \"%s\": %s\n",
84 myname, name, bfd_errmsg (bfd_get_error ()));
85 }
86
c95d08a8
DE
87 if (STATE_VERBOSE_P (sd))
88 printf ("%s %s\n", myname, name);
89
90 /* Load the program into the simulator. */
897a1d78 91 if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
c95d08a8
DE
92 exit (1);
93
94 /* Prepare the program for execution. */
95#ifdef HAVE_ENVIRON
897a1d78 96 sim_create_inferior (sd, prog_bfd, prog_argv, environ);
c95d08a8 97#else
897a1d78 98 sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
c95d08a8
DE
99#endif
100
101 /* Run the program. */
247fccde 102 prev_sigint = signal (SIGINT, cntrl_c);
c95d08a8 103 sim_resume (sd, 0, 0);
247fccde 104 signal (SIGINT, prev_sigint);
c95d08a8
DE
105
106 /* Print any stats the simulator collected. */
107 sim_info (sd, 0);
108
109 /* Find out why the program exited. */
110 sim_stop_reason (sd, &reason, &sigrc);
111
112 /* Shutdown the simulator. */
113 sim_close (sd, 0);
114
115 /* If reason is sim_exited, then sigrc holds the exit code which we want
116 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
117 the signal that the simulator received; we want to return that to
118 indicate failure. */
119
120#ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */
121 if (sigrc == SIGILL)
122 abort ();
123 sigrc = 0;
124#else
125 /* Why did we stop? */
126 switch (reason)
127 {
128 case sim_signalled:
129 case sim_stopped:
130 if (sigrc != 0)
131 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
132 break;
133
134 case sim_exited:
135 break;
247fccde
AC
136
137 default:
138 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
139 break;
140
c95d08a8
DE
141 }
142#endif
143
144 return sigrc;
145}
146
147static void
148usage ()
149{
150 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
151 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
152 exit (1);
153}
This page took 0.075647 seconds and 4 git commands to generate.