* remote-sim.c (gdb_os_vprintf_filtered): Fix to work with non-ANSI
[deliverable/binutils-gdb.git] / sim / common / run.c
CommitLineData
6834d493
JW
1/* run front end support for all the simulators.
2 Copyright (C) 1992, 1993 1994, 1995 Free Software Foundation, Inc.
3
6834d493
JW
4GNU CC 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
9GNU CC 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
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18
19/* Steve Chamberlain
20 sac@cygnus.com */
21
22#include <signal.h>
23#include <stdio.h>
24#include <varargs.h>
25#include "bfd.h"
26#include "remote-sim.h"
27#include "callback.h"
28#ifndef SIGQUIT
29#define SIGQUIT SIGTERM
30#endif
31
32void usage();
33extern int optind;
34extern char *optarg;
35
57bc1a72 36bfd *exec_bfd;
9b280a86 37
6834d493
JW
38int target_byte_order;
39
40extern host_callback default_callback;
41int
42main (ac, av)
43 int ac;
44 char **av;
45{
46 bfd *abfd;
47 bfd_vma start_address;
48 asection *s;
49 int i;
50 int verbose = 0;
51 int trace = 0;
52 char *name = "";
53 enum sim_stop reason;
54 int sigrc;
55
56 while ((i = getopt (ac, av, "m:p:s:tv")) != EOF)
57 switch (i)
58 {
59 case 'm':
60 sim_size (atoi (optarg));
61 break;
62 case 'p':
63 sim_set_profile (atoi (optarg));
64 break;
65 case 's':
66 sim_set_profile_size (atoi (optarg));
67 break;
68 case 't':
69 trace = 1;
70 break;
71 case 'v':
72 verbose = 1;
73 break;
74 default:
75 usage();
76 }
77 ac -= optind;
78 av += optind;
79
80 if (ac != 1)
81 usage();
82
83 name = *av;
84
85 if (verbose)
86 {
87 printf ("run %s\n", name);
88 }
3be50301 89
57bc1a72 90 exec_bfd = abfd = bfd_openr (name, 0);
3be50301
JW
91 if (!abfd)
92 {
93 fprintf (stderr, "run: can't open %s: %s\n",
94 name, bfd_errmsg(bfd_get_error()));
95 exit (1);
96 }
97
98 if (!bfd_check_format (abfd, bfd_object))
99 {
100 fprintf (stderr, "run: can't load %s: %s\n",
101 name, bfd_errmsg(bfd_get_error()));
102 exit (1);
103 }
104
6834d493
JW
105 sim_set_callbacks (&default_callback);
106 default_callback.init (&default_callback);
3be50301 107
9b280a86
MM
108 /* Ensure that any run-time initialisation that needs to be
109 performed by the simulator can occur. */
110 sim_open(NULL);
111
3be50301 112 for (s = abfd->sections; s; s = s->next)
9b280a86 113 if (abfd && (s->flags & SEC_LOAD))
6834d493 114 {
3733d109
JSC
115 unsigned char *buffer = (unsigned char *)malloc ((size_t)(bfd_section_size (abfd, s)));
116 if (buffer != NULL)
117 {
118 bfd_get_section_contents (abfd,
119 s,
120 buffer,
121 0,
122 bfd_section_size (abfd, s));
123 sim_write (s->vma, buffer, bfd_section_size (abfd, s));
124 }
125 else
126 {
127 fprintf (stderr, "run: failed to allocate section buffer: %s\n",
128 bfd_errmsg(bfd_get_error()));
129 exit (1);
130 }
3be50301
JW
131 }
132
133 start_address = bfd_get_start_address (abfd);
134 sim_create_inferior (start_address, NULL, NULL);
135
9b280a86 136 target_byte_order = bfd_big_endian (abfd) ? 4321 : 1234;
6834d493 137
3be50301
JW
138 if (trace)
139 {
140 int done = 0;
141 while (!done)
142 {
143 done = sim_trace ();
6834d493
JW
144 }
145 }
3be50301
JW
146 else
147 {
148 sim_resume (0, 0);
149 }
150 if (verbose)
151 sim_info (0);
152
153 sim_stop_reason (&reason, &sigrc);
6834d493 154
9b280a86
MM
155 sim_close(0);
156
57bc1a72
MM
157 /* Why did we stop? */
158 switch (reason)
159 {
160 case sim_signalled:
161 case sim_stopped:
162 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
163 break;
164
165 case sim_exited:
166 break;
167 }
168
3be50301
JW
169 /* If reason is sim_exited, then sigrc holds the exit code which we want
170 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
171 the signal that the simulator received; we want to return that to
172 indicate failure. */
173 return sigrc;
6834d493
JW
174}
175
176void
177usage()
178{
179 fprintf (stderr, "usage: run [-tv][-m size] program\n");
180 exit (1);
181}
182
183
This page took 0.071023 seconds and 4 git commands to generate.