1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997, 1998, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
6 This file is part of GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "sim-assert.h"
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h> /* needed by sys/resource.h */
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
48 #include "libiberty.h"
50 #include "sim-utils.h"
52 /* Global pointer to all state data.
54 struct sim_state *current_state;
56 /* Allocate zero filled memory with xcalloc - xcalloc aborts if the
60 zalloc (unsigned long size)
62 return xcalloc (1, size);
65 /* Allocate a sim_state struct. */
68 sim_state_alloc (SIM_OPEN_KIND kind,
69 host_callback *callback)
71 SIM_DESC sd = ZALLOC (struct sim_state);
73 STATE_MAGIC (sd) = SIM_MAGIC_NUMBER;
74 STATE_CALLBACK (sd) = callback;
75 STATE_OPEN_KIND (sd) = kind;
81 /* Initialize the back link from the cpu struct to the state struct. */
82 /* ??? I can envision a design where the state struct contains an array
83 of pointers to cpu structs, rather than an array of structs themselves.
84 Implementing this is trickier as one may not know what to allocate until
85 one has parsed the args. Parsing the args twice wouldn't be unreasonable,
86 IMHO. If the state struct ever does contain an array of pointers then we
88 ??? See also sim_post_argv_init*/
89 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
91 CPU_STATE (STATE_CPU (sd, cpu_nr)) = sd;
92 CPU_INDEX (STATE_CPU (sd, cpu_nr)) = cpu_nr;
104 /* Free a sim_state struct. */
107 sim_state_free (SIM_DESC sd)
109 ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
111 #ifdef SIM_STATE_FREE
118 /* Return a pointer to the cpu data for CPU_NAME, or NULL if not found. */
121 sim_cpu_lookup (SIM_DESC sd, const char *cpu_name)
125 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
126 if (strcmp (cpu_name, CPU_NAME (STATE_CPU (sd, i))) == 0)
127 return STATE_CPU (sd, i);
131 /* Return the prefix to use for a CPU specific message (typically an
135 sim_cpu_msg_prefix (sim_cpu *cpu)
137 #if MAX_NR_PROCESSORS == 1
145 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
147 int len = strlen (CPU_NAME (STATE_CPU (sd, i)));
151 prefix = (char *) xmalloc (maxlen + 5);
153 sprintf (prefix, "%s: ", CPU_NAME (cpu));
158 /* Cover fn to sim_io_eprintf. */
161 sim_io_eprintf_cpu (sim_cpu *cpu, const char *fmt, ...)
163 SIM_DESC sd = CPU_STATE (cpu);
167 sim_io_eprintf (sd, "%s", sim_cpu_msg_prefix (cpu));
168 sim_io_evprintf (sd, fmt, ap);
172 /* Turn VALUE into a string with commas. */
175 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
178 char *endbuf = buf + sizeof_buf - 1;
188 *--endbuf = (value % 10) + '0';
189 } while ((value /= 10) != 0);
194 /* Analyze PROG_NAME/PROG_BFD and set these fields in the state struct:
195 STATE_ARCHITECTURE, if not set already and can be determined from the bfd
202 PROG_NAME is the file name of the executable or NULL.
203 PROG_BFD is its bfd or NULL.
205 If both PROG_NAME and PROG_BFD are NULL, this function returns immediately.
206 If PROG_BFD is not NULL, PROG_NAME is ignored.
208 Implicit inputs: STATE_MY_NAME(sd), STATE_TARGET(sd),
209 STATE_ARCHITECTURE(sd).
211 A new bfd is created so the app isn't required to keep its copy of the
215 sim_analyze_program (SIM_DESC sd, char *prog_name, bfd *prog_bfd)
218 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
220 if (prog_bfd != NULL)
222 if (prog_bfd == STATE_PROG_BFD (sd))
223 /* already analyzed */
226 /* duplicate needed, save the name of the file to be re-opened */
227 prog_name = bfd_get_filename (prog_bfd);
230 /* do we need to duplicate anything? */
231 if (prog_name == NULL)
234 /* open a new copy of the prog_bfd */
235 prog_bfd = bfd_openr (prog_name, STATE_TARGET (sd));
236 if (prog_bfd == NULL)
238 sim_io_eprintf (sd, "%s: can't open \"%s\": %s\n",
241 bfd_errmsg (bfd_get_error ()));
244 if (!bfd_check_format (prog_bfd, bfd_object))
246 sim_io_eprintf (sd, "%s: \"%s\" is not an object file: %s\n",
249 bfd_errmsg (bfd_get_error ()));
250 bfd_close (prog_bfd);
253 if (STATE_ARCHITECTURE (sd) != NULL)
254 bfd_set_arch_info (prog_bfd, STATE_ARCHITECTURE (sd));
257 if (bfd_get_arch (prog_bfd) != bfd_arch_unknown
258 && bfd_get_arch (prog_bfd) != bfd_arch_obscure)
260 STATE_ARCHITECTURE (sd) = bfd_get_arch_info (prog_bfd);
264 /* update the sim structure */
265 if (STATE_PROG_BFD (sd) != NULL)
266 bfd_close (STATE_PROG_BFD (sd));
267 STATE_PROG_BFD (sd) = prog_bfd;
268 STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
270 for (s = prog_bfd->sections; s; s = s->next)
271 if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
273 STATE_TEXT_SECTION (sd) = s;
274 STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
275 STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
279 bfd_cache_close (prog_bfd);
284 /* Simulator timing support. */
286 /* Called before sim_elapsed_time_since to get a reference point. */
289 sim_elapsed_time_get (void)
291 #ifdef HAVE_GETRUSAGE
292 struct rusage mytime;
293 if (getrusage (RUSAGE_SELF, &mytime) == 0)
294 return 1 + (SIM_ELAPSED_TIME) (((double) mytime.ru_utime.tv_sec * 1000) + (((double) mytime.ru_utime.tv_usec + 500) / 1000));
298 return 1 + (SIM_ELAPSED_TIME) time ((time_t) 0);
305 /* Return the elapsed time in milliseconds since START.
306 The actual time may be cpu usage (preferred) or wall clock. */
309 sim_elapsed_time_since (SIM_ELAPSED_TIME start)
311 #ifdef HAVE_GETRUSAGE
312 return sim_elapsed_time_get () - start;
315 return (sim_elapsed_time_get () - start) * 1000;
324 /* do_command but with printf style formatting of the arguments */
326 sim_do_commandf (SIM_DESC sd,
333 if (vasprintf (&buf, fmt, ap) < 0)
335 sim_io_eprintf (sd, "%s: asprintf failed for `%s'\n",
336 STATE_MY_NAME (sd), fmt);
339 sim_do_command (sd, buf);
345 /* sim-basics.h defines a number of enumerations, convert each of them
346 to a string representation */
348 map_to_str (unsigned map)
352 case read_map: return "read";
353 case write_map: return "write";
354 case exec_map: return "exec";
355 case io_map: return "io";
359 sprintf (str, "(%ld)", (long) map);
366 access_to_str (unsigned access)
370 case access_invalid: return "invalid";
371 case access_read: return "read";
372 case access_write: return "write";
373 case access_exec: return "exec";
374 case access_io: return "io";
375 case access_read_write: return "read_write";
376 case access_read_exec: return "read_exec";
377 case access_write_exec: return "write_exec";
378 case access_read_write_exec: return "read_write_exec";
379 case access_read_io: return "read_io";
380 case access_write_io: return "write_io";
381 case access_read_write_io: return "read_write_io";
382 case access_exec_io: return "exec_io";
383 case access_read_exec_io: return "read_exec_io";
384 case access_write_exec_io: return "write_exec_io";
385 case access_read_write_exec_io: return "read_write_exec_io";
389 sprintf (str, "(%ld)", (long) access);
396 transfer_to_str (unsigned transfer)
400 case read_transfer: return "read";
401 case write_transfer: return "write";
402 default: return "(error)";