This commit was manufactured by cvs2svn to create branch 'gdb_7_0-branch'.
[external/binutils.git] / sim / common / sim-utils.c
1 /* Miscellaneous simulator utilities.
2    Copyright (C) 1997, 1998, 2007, 2008, 2009 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 3 of the License, or
10 (at your option) 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
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "sim-main.h"
21 #include "sim-assert.h"
22
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26
27 #ifdef HAVE_TIME_H
28 #include <time.h>
29 #endif
30
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h> /* needed by sys/resource.h */
33 #endif
34
35 #ifdef HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
37 #endif
38
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46
47 #include "libiberty.h"
48 #include "bfd.h"
49 #include "sim-utils.h"
50
51 /* Global pointer to all state data.
52    Set by sim_resume.  */
53 struct sim_state *current_state;
54
55 /* Allocate zero filled memory with xcalloc - xcalloc aborts if the
56    allocation fails.  */
57
58 void *
59 zalloc (unsigned long size)
60 {
61   return xcalloc (1, size);
62 }
63
64 void
65 zfree (void *data)
66 {
67   free (data);
68 }
69
70 /* Allocate a sim_state struct.  */
71
72 SIM_DESC
73 sim_state_alloc (SIM_OPEN_KIND kind,
74                  host_callback *callback)
75 {
76   SIM_DESC sd = ZALLOC (struct sim_state);
77
78   STATE_MAGIC (sd) = SIM_MAGIC_NUMBER;
79   STATE_CALLBACK (sd) = callback;
80   STATE_OPEN_KIND (sd) = kind;
81
82 #if 0
83   {
84     int cpu_nr;
85
86     /* Initialize the back link from the cpu struct to the state struct.  */
87     /* ??? I can envision a design where the state struct contains an array
88        of pointers to cpu structs, rather than an array of structs themselves.
89        Implementing this is trickier as one may not know what to allocate until
90        one has parsed the args.  Parsing the args twice wouldn't be unreasonable,
91        IMHO.  If the state struct ever does contain an array of pointers then we
92        can't do this here.
93        ??? See also sim_post_argv_init*/
94     for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
95       {
96         CPU_STATE (STATE_CPU (sd, cpu_nr)) = sd;
97         CPU_INDEX (STATE_CPU (sd, cpu_nr)) = cpu_nr;
98       }
99   }
100 #endif
101
102 #ifdef SIM_STATE_INIT
103   SIM_STATE_INIT (sd);
104 #endif
105
106   return sd;
107 }
108
109 /* Free a sim_state struct.  */
110
111 void
112 sim_state_free (SIM_DESC sd)
113 {
114   ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
115
116 #ifdef SIM_STATE_FREE
117   SIM_STATE_FREE (sd);
118 #endif
119
120   zfree (sd);
121 }
122
123 /* Return a pointer to the cpu data for CPU_NAME, or NULL if not found.  */
124
125 sim_cpu *
126 sim_cpu_lookup (SIM_DESC sd, const char *cpu_name)
127 {
128   int i;
129
130   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
131     if (strcmp (cpu_name, CPU_NAME (STATE_CPU (sd, i))) == 0)
132       return STATE_CPU (sd, i);
133   return NULL;
134 }
135
136 /* Return the prefix to use for a CPU specific message (typically an
137    error message).  */
138
139 const char *
140 sim_cpu_msg_prefix (sim_cpu *cpu)
141 {
142 #if MAX_NR_PROCESSORS == 1
143   return "";
144 #else
145   static char *prefix;
146
147   if (prefix == NULL)
148     {
149       int maxlen = 0;
150       for (i = 0; i < MAX_NR_PROCESSORS; ++i)
151         {
152           int len = strlen (CPU_NAME (STATE_CPU (sd, i)));
153           if (len > maxlen)
154             maxlen = len;
155         }
156       prefix = (char *) xmalloc (maxlen + 5);
157     }
158   sprintf (prefix, "%s: ", CPU_NAME (cpu));
159   return prefix;
160 #endif
161 }
162
163 /* Cover fn to sim_io_eprintf.  */
164
165 void
166 sim_io_eprintf_cpu (sim_cpu *cpu, const char *fmt, ...)
167 {
168   SIM_DESC sd = CPU_STATE (cpu);
169   va_list ap;
170
171   va_start (ap, fmt);
172   sim_io_eprintf (sd, sim_cpu_msg_prefix (cpu));
173   sim_io_evprintf (sd, fmt, ap);
174   va_end (ap);
175 }
176
177 /* Turn VALUE into a string with commas.  */
178
179 char *
180 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
181 {
182   int comma = 3;
183   char *endbuf = buf + sizeof_buf - 1;
184
185   *--endbuf = '\0';
186   do {
187     if (comma-- == 0)
188       {
189         *--endbuf = ',';
190         comma = 2;
191       }
192
193     *--endbuf = (value % 10) + '0';
194   } while ((value /= 10) != 0);
195
196   return endbuf;
197 }
198
199 /* Analyze PROG_NAME/PROG_BFD and set these fields in the state struct:
200    STATE_ARCHITECTURE, if not set already and can be determined from the bfd
201    STATE_PROG_BFD
202    STATE_START_ADDR
203    STATE_TEXT_SECTION
204    STATE_TEXT_START
205    STATE_TEXT_END
206
207    PROG_NAME is the file name of the executable or NULL.
208    PROG_BFD is its bfd or NULL.
209
210    If both PROG_NAME and PROG_BFD are NULL, this function returns immediately.
211    If PROG_BFD is not NULL, PROG_NAME is ignored.
212
213    Implicit inputs: STATE_MY_NAME(sd), STATE_TARGET(sd),
214                     STATE_ARCHITECTURE(sd).
215
216    A new bfd is created so the app isn't required to keep its copy of the
217    bfd open.  */
218
219 SIM_RC
220 sim_analyze_program (sd, prog_name, prog_bfd)
221      SIM_DESC sd;
222      char *prog_name;
223      bfd *prog_bfd;
224 {
225   asection *s;
226   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
227
228   if (prog_bfd != NULL)
229     {
230       if (prog_bfd == STATE_PROG_BFD (sd))
231         /* already analyzed */
232         return SIM_RC_OK;
233       else
234         /* duplicate needed, save the name of the file to be re-opened */
235         prog_name = bfd_get_filename (prog_bfd);
236     }
237
238   /* do we need to duplicate anything? */
239   if (prog_name == NULL)
240     return SIM_RC_OK;
241
242   /* open a new copy of the prog_bfd */
243   prog_bfd = bfd_openr (prog_name, STATE_TARGET (sd));
244   if (prog_bfd == NULL)
245     {
246       sim_io_eprintf (sd, "%s: can't open \"%s\": %s\n", 
247                       STATE_MY_NAME (sd),
248                       prog_name,
249                       bfd_errmsg (bfd_get_error ()));
250       return SIM_RC_FAIL;
251     }
252   if (!bfd_check_format (prog_bfd, bfd_object)) 
253     {
254       sim_io_eprintf (sd, "%s: \"%s\" is not an object file: %s\n",
255                       STATE_MY_NAME (sd),
256                       prog_name,
257                       bfd_errmsg (bfd_get_error ()));
258       bfd_close (prog_bfd);
259       return SIM_RC_FAIL;
260     }
261   if (STATE_ARCHITECTURE (sd) != NULL)
262     bfd_set_arch_info (prog_bfd, STATE_ARCHITECTURE (sd));
263   else
264     {
265       if (bfd_get_arch (prog_bfd) != bfd_arch_unknown
266           && bfd_get_arch (prog_bfd) != bfd_arch_obscure)
267         {
268           STATE_ARCHITECTURE (sd) = bfd_get_arch_info (prog_bfd);
269         }
270     }
271
272   /* update the sim structure */
273   if (STATE_PROG_BFD (sd) != NULL)
274     bfd_close (STATE_PROG_BFD (sd));
275   STATE_PROG_BFD (sd) = prog_bfd;
276   STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
277
278   for (s = prog_bfd->sections; s; s = s->next)
279     if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
280       {
281         STATE_TEXT_SECTION (sd) = s;
282         STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
283         STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
284         break;
285       }
286
287   bfd_cache_close (prog_bfd);
288
289   return SIM_RC_OK;
290 }
291 \f
292 /* Simulator timing support.  */
293
294 /* Called before sim_elapsed_time_since to get a reference point.  */
295
296 SIM_ELAPSED_TIME
297 sim_elapsed_time_get ()
298 {
299 #ifdef HAVE_GETRUSAGE
300   struct rusage mytime;
301   if (getrusage (RUSAGE_SELF, &mytime) == 0)
302     return 1 + (SIM_ELAPSED_TIME) (((double) mytime.ru_utime.tv_sec * 1000) + (((double) mytime.ru_utime.tv_usec + 500) / 1000));
303   return 1;
304 #else
305 #ifdef HAVE_TIME
306   return 1 + (SIM_ELAPSED_TIME) time ((time_t) 0);
307 #else
308   return 1;
309 #endif
310 #endif
311 }
312
313 /* Return the elapsed time in milliseconds since START.
314    The actual time may be cpu usage (preferred) or wall clock.  */
315
316 unsigned long
317 sim_elapsed_time_since (start)
318      SIM_ELAPSED_TIME start;
319 {
320 #ifdef HAVE_GETRUSAGE
321   return sim_elapsed_time_get () - start;
322 #else
323 #ifdef HAVE_TIME
324   return (sim_elapsed_time_get () - start) * 1000;
325 #else
326   return 0;
327 #endif
328 #endif
329 }
330
331
332
333 /* do_command but with printf style formatting of the arguments */
334 void
335 sim_do_commandf (SIM_DESC sd,
336                  const char *fmt,
337                  ...)
338 {
339   va_list ap;
340   char *buf;
341   va_start (ap, fmt);
342   vasprintf (&buf, fmt, ap);
343   sim_do_command (sd, buf);
344   va_end (ap);
345   free (buf);
346 }
347
348
349 /* sim-basics.h defines a number of enumerations, convert each of them
350    to a string representation */
351 const char *
352 map_to_str (unsigned map)
353 {
354   switch (map)
355     {
356     case read_map: return "read";
357     case write_map: return "write";
358     case exec_map: return "exec";
359     case io_map: return "io";
360     default:
361       {
362         static char str[10];
363         sprintf (str, "(%ld)", (long) map);
364         return str;
365       }
366     }
367 }
368
369 const char *
370 access_to_str (unsigned access)
371 {
372   switch (access)
373     {
374     case access_invalid: return "invalid";
375     case access_read: return "read";
376     case access_write: return "write";
377     case access_exec: return "exec";
378     case access_io: return "io";
379     case access_read_write: return "read_write";
380     case access_read_exec: return "read_exec";
381     case access_write_exec: return "write_exec";
382     case access_read_write_exec: return "read_write_exec";
383     case access_read_io: return "read_io";
384     case access_write_io: return "write_io";
385     case access_read_write_io: return "read_write_io";
386     case access_exec_io: return "exec_io";
387     case access_read_exec_io: return "read_exec_io";
388     case access_write_exec_io: return "write_exec_io";
389     case access_read_write_exec_io: return "read_write_exec_io";
390     default:
391       {
392         static char str[10];
393         sprintf (str, "(%ld)", (long) access);
394         return str;
395       }
396     }
397 }
398
399 const char *
400 transfer_to_str (unsigned transfer)
401 {
402   switch (transfer)
403     {
404     case read_transfer: return "read";
405     case write_transfer: return "write";
406     default: return "(error)";
407     }
408 }
409
410