* sim-utils.c (sim_state_alloc): Delete setting of cpu backlink here.
[external/binutils.git] / sim / common / sim-utils.c
1 /* Miscellaneous simulator utilities.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "sim-main.h"
22 #include "sim-assert.h"
23
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27
28 #ifdef HAVE_TIME_H
29 #include <time.h>
30 #endif
31
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h> /* needed by sys/resource.h */
34 #endif
35
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
38 #endif
39
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #else
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>
45 #endif
46 #endif
47
48 #include "libiberty.h"
49 #include "bfd.h"
50 #include "sim-utils.h"
51
52 /* Global pointer to all state data.
53    Set by sim_resume.  */
54 struct sim_state *current_state;
55
56 /* Allocate zero filled memory with xmalloc - xmalloc aborts of the
57    allocation fails.  */
58
59 void *
60 zalloc (unsigned long size)
61 {
62   void *memory = (void *) xmalloc (size);
63   memset (memory, 0, size);
64   return memory;
65 }
66
67 void
68 zfree (void *data)
69 {
70   free (data);
71 }
72
73 /* Allocate a sim_state struct.  */
74
75 SIM_DESC
76 sim_state_alloc (SIM_OPEN_KIND kind,
77                  host_callback *callback)
78 {
79   int cpu_nr;
80   SIM_DESC sd = ZALLOC (struct sim_state);
81
82   STATE_MAGIC (sd) = SIM_MAGIC_NUMBER;
83   STATE_CALLBACK (sd) = callback;
84   STATE_OPEN_KIND (sd) = kind;
85
86 #if 0
87   /* Initialize the back link from the cpu struct to the state struct.  */
88   /* ??? I can envision a design where the state struct contains an array
89      of pointers to cpu structs, rather than an array of structs themselves.
90      Implementing this is trickier as one may not know what to allocate until
91      one has parsed the args.  Parsing the args twice wouldn't be unreasonable,
92      IMHO.  If the state struct ever does contain an array of pointers then we
93      can't do this here.  */
94   for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
95     CPU_STATE (STATE_CPU (sd, cpu_nr)) = sd;
96 #endif
97
98 #ifdef SIM_STATE_INIT
99   SIM_STATE_INIT (sd);
100 #endif
101
102   return sd;
103 }
104
105 /* Free a sim_state struct.  */
106
107 void
108 sim_state_free (SIM_DESC sd)
109 {
110   ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
111
112 #ifdef SIM_STATE_FREE
113   SIM_STATE_FREE (sd);
114 #endif
115
116   zfree (sd);
117 }
118
119 /* Turn VALUE into a string with commas.  */
120
121 char *
122 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
123 {
124   int comma = 3;
125   char *endbuf = buf + sizeof_buf - 1;
126
127   *--endbuf = '\0';
128   do {
129     if (comma-- == 0)
130       {
131         *--endbuf = ',';
132         comma = 2;
133       }
134
135     *--endbuf = (value % 10) + '0';
136   } while ((value /= 10) != 0);
137
138   return endbuf;
139 }
140
141 /* Analyze a prog_name/prog_bfd and set various fields in the state
142    struct.  */
143
144 SIM_RC
145 sim_analyze_program (sd, prog_name, prog_bfd)
146      SIM_DESC sd;
147      char *prog_name;
148      bfd *prog_bfd;
149 {
150   asection *s;
151   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
152
153   if (prog_bfd != NULL)
154     {
155       if (prog_bfd == STATE_PROG_BFD (sd))
156         /* already analyzed */
157         return SIM_RC_OK;
158       else
159         /* duplicate needed, save the name of the file to be re-opened */
160         prog_name = bfd_get_filename (prog_bfd);
161     }
162
163   /* do we need to duplicate anything? */
164   if (prog_name == NULL)
165     return SIM_RC_OK;
166
167   /* open a new copy of the prog_bfd */
168   prog_bfd = bfd_openr (prog_name, STATE_TARGET (sd));
169   if (prog_bfd == NULL)
170     {
171       sim_io_eprintf (sd, "%s: can't open \"%s\": %s\n", 
172                       STATE_MY_NAME (sd),
173                       prog_name,
174                       bfd_errmsg (bfd_get_error ()));
175       return SIM_RC_FAIL;
176     }
177   if (!bfd_check_format (prog_bfd, bfd_object)) 
178     {
179       sim_io_eprintf (sd, "%s: \"%s\" is not an object file: %s\n",
180                       STATE_MY_NAME (sd),
181                       prog_name,
182                       bfd_errmsg (bfd_get_error ()));
183       bfd_close (prog_bfd);
184       return SIM_RC_FAIL;
185     }
186   if (STATE_ARCHITECTURE (sd) != NULL)
187     bfd_set_arch_info (prog_bfd, STATE_ARCHITECTURE (sd));
188   else
189     {
190       if (bfd_get_arch (prog_bfd) != bfd_arch_unknown
191           && bfd_get_arch (prog_bfd) != bfd_arch_obscure)
192         {
193           STATE_ARCHITECTURE (sd) = bfd_get_arch_info (prog_bfd);
194         }
195     }
196
197   /* update the sim structure */
198   if (STATE_PROG_BFD (sd) != NULL)
199     bfd_close (STATE_PROG_BFD (sd));
200   STATE_PROG_BFD (sd) = prog_bfd;
201   STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
202
203   for (s = prog_bfd->sections; s; s = s->next)
204     if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
205       {
206         STATE_TEXT_SECTION (sd) = s;
207         STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
208         STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
209         break;
210       }
211
212   return SIM_RC_OK;
213 }
214 \f
215 /* Simulator timing support.  */
216
217 /* Called before sim_elapsed_time_since to get a reference point.  */
218
219 SIM_ELAPSED_TIME
220 sim_elapsed_time_get ()
221 {
222 #ifdef HAVE_GETRUSAGE
223   struct rusage mytime;
224   if (getrusage (RUSAGE_SELF, &mytime) == 0)
225     return 1 + (SIM_ELAPSED_TIME) (((double) mytime.ru_utime.tv_sec * 1000) + (((double) mytime.ru_utime.tv_usec + 500) / 1000));
226   return 1;
227 #else
228 #ifdef HAVE_TIME
229   return 1 + (SIM_ELAPSED_TIME) time ((time_t) 0);
230 #else
231   return 1;
232 #endif
233 #endif
234 }
235
236 /* Return the elapsed time in milliseconds since START.
237    The actual time may be cpu usage (prefered) or wall clock.  */
238
239 unsigned long
240 sim_elapsed_time_since (start)
241      SIM_ELAPSED_TIME start;
242 {
243 #ifdef HAVE_GETRUSAGE
244   return sim_elapsed_time_get () - start;
245 #else
246 #ifdef HAVE_TIME
247   return (sim_elapsed_time_get () - start) * 1000;
248 #else
249   return 0;
250 #endif
251 #endif
252 }
253
254
255
256 /* do_command but with printf style formatting of the arguments */
257 void
258 sim_do_commandf (SIM_DESC sd,
259                  const char *fmt,
260                  ...)
261 {
262   va_list ap;
263   char *buf;
264   va_start (ap, fmt);
265   vasprintf (&buf, fmt, ap);
266   sim_do_command (sd, buf);
267   va_end (ap);
268   free (buf);
269 }