OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [sim/] [common/] [nrun.c] - Blame information for rev 840

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.