1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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.
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.
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/>. */
21 #include "sim-assert.h"
25 #include <sys/time.h> /* needed by sys/resource.h */
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
32 #include "libiberty.h"
34 #include "sim-utils.h"
36 /* Allocate zero filled memory with xcalloc - xcalloc aborts if the
40 zalloc (unsigned long size
)
42 return xcalloc (1, size
);
45 /* Allocate a sim_state struct. */
48 sim_state_alloc (SIM_OPEN_KIND kind
,
49 host_callback
*callback
)
51 SIM_DESC sd
= ZALLOC (struct sim_state
);
53 STATE_MAGIC (sd
) = SIM_MAGIC_NUMBER
;
54 STATE_CALLBACK (sd
) = callback
;
55 STATE_OPEN_KIND (sd
) = kind
;
61 /* Initialize the back link from the cpu struct to the state struct. */
62 /* ??? I can envision a design where the state struct contains an array
63 of pointers to cpu structs, rather than an array of structs themselves.
64 Implementing this is trickier as one may not know what to allocate until
65 one has parsed the args. Parsing the args twice wouldn't be unreasonable,
66 IMHO. If the state struct ever does contain an array of pointers then we
68 ??? See also sim_post_argv_init*/
69 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
71 CPU_STATE (STATE_CPU (sd
, cpu_nr
)) = sd
;
72 CPU_INDEX (STATE_CPU (sd
, cpu_nr
)) = cpu_nr
;
84 /* Free a sim_state struct. */
87 sim_state_free (SIM_DESC sd
)
89 ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
98 /* Return a pointer to the cpu data for CPU_NAME, or NULL if not found. */
101 sim_cpu_lookup (SIM_DESC sd
, const char *cpu_name
)
105 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
106 if (strcmp (cpu_name
, CPU_NAME (STATE_CPU (sd
, i
))) == 0)
107 return STATE_CPU (sd
, i
);
111 /* Return the prefix to use for a CPU specific message (typically an
115 sim_cpu_msg_prefix (sim_cpu
*cpu
)
117 #if MAX_NR_PROCESSORS == 1
125 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
127 int len
= strlen (CPU_NAME (STATE_CPU (sd
, i
)));
131 prefix
= (char *) xmalloc (maxlen
+ 5);
133 sprintf (prefix
, "%s: ", CPU_NAME (cpu
));
138 /* Cover fn to sim_io_eprintf. */
141 sim_io_eprintf_cpu (sim_cpu
*cpu
, const char *fmt
, ...)
143 SIM_DESC sd
= CPU_STATE (cpu
);
147 sim_io_eprintf (sd
, "%s", sim_cpu_msg_prefix (cpu
));
148 sim_io_evprintf (sd
, fmt
, ap
);
152 /* Turn VALUE into a string with commas. */
155 sim_add_commas (char *buf
, int sizeof_buf
, unsigned long value
)
158 char *endbuf
= buf
+ sizeof_buf
- 1;
168 *--endbuf
= (value
% 10) + '0';
169 } while ((value
/= 10) != 0);
174 /* Analyze PROG_NAME/PROG_BFD and set these fields in the state struct:
175 STATE_ARCHITECTURE, if not set already and can be determined from the bfd
182 PROG_NAME is the file name of the executable or NULL.
183 PROG_BFD is its bfd or NULL.
185 If both PROG_NAME and PROG_BFD are NULL, this function returns immediately.
186 If PROG_BFD is not NULL, PROG_NAME is ignored.
188 Implicit inputs: STATE_MY_NAME(sd), STATE_TARGET(sd),
189 STATE_ARCHITECTURE(sd).
191 A new bfd is created so the app isn't required to keep its copy of the
195 sim_analyze_program (SIM_DESC sd
, const char *prog_name
, bfd
*prog_bfd
)
198 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
200 if (prog_bfd
!= NULL
)
202 if (prog_bfd
== STATE_PROG_BFD (sd
))
203 /* already analyzed */
206 /* duplicate needed, save the name of the file to be re-opened */
207 prog_name
= bfd_get_filename (prog_bfd
);
210 /* do we need to duplicate anything? */
211 if (prog_name
== NULL
)
214 /* open a new copy of the prog_bfd */
215 prog_bfd
= bfd_openr (prog_name
, STATE_TARGET (sd
));
216 if (prog_bfd
== NULL
)
218 sim_io_eprintf (sd
, "%s: can't open \"%s\": %s\n",
221 bfd_errmsg (bfd_get_error ()));
224 if (!bfd_check_format (prog_bfd
, bfd_object
))
226 sim_io_eprintf (sd
, "%s: \"%s\" is not an object file: %s\n",
229 bfd_errmsg (bfd_get_error ()));
230 bfd_close (prog_bfd
);
233 if (STATE_ARCHITECTURE (sd
) != NULL
)
234 bfd_set_arch_info (prog_bfd
, STATE_ARCHITECTURE (sd
));
237 if (bfd_get_arch (prog_bfd
) != bfd_arch_unknown
238 && bfd_get_arch (prog_bfd
) != bfd_arch_obscure
)
240 STATE_ARCHITECTURE (sd
) = bfd_get_arch_info (prog_bfd
);
244 /* update the sim structure */
245 if (STATE_PROG_BFD (sd
) != NULL
)
246 bfd_close (STATE_PROG_BFD (sd
));
247 STATE_PROG_BFD (sd
) = prog_bfd
;
248 STATE_START_ADDR (sd
) = bfd_get_start_address (prog_bfd
);
250 for (s
= prog_bfd
->sections
; s
; s
= s
->next
)
251 if (strcmp (bfd_section_name (s
), ".text") == 0)
253 STATE_TEXT_SECTION (sd
) = s
;
254 STATE_TEXT_START (sd
) = bfd_section_vma (s
);
255 STATE_TEXT_END (sd
) = STATE_TEXT_START (sd
) + bfd_section_size (s
);
259 bfd_cache_close (prog_bfd
);
264 /* Simulator timing support. */
266 /* Called before sim_elapsed_time_since to get a reference point. */
269 sim_elapsed_time_get (void)
271 #ifdef HAVE_GETRUSAGE
272 struct rusage mytime
;
273 if (getrusage (RUSAGE_SELF
, &mytime
) == 0)
274 return 1 + (SIM_ELAPSED_TIME
) (((double) mytime
.ru_utime
.tv_sec
* 1000) + (((double) mytime
.ru_utime
.tv_usec
+ 500) / 1000));
278 return 1 + (SIM_ELAPSED_TIME
) time ((time_t) 0);
285 /* Return the elapsed time in milliseconds since START.
286 The actual time may be cpu usage (preferred) or wall clock. */
289 sim_elapsed_time_since (SIM_ELAPSED_TIME start
)
291 #ifdef HAVE_GETRUSAGE
292 return sim_elapsed_time_get () - start
;
295 return (sim_elapsed_time_get () - start
) * 1000;
304 /* do_command but with printf style formatting of the arguments */
306 sim_do_commandf (SIM_DESC sd
,
315 ret
= vasprintf (&buf
, fmt
, ap
);
320 sim_io_eprintf (sd
, "%s: asprintf failed for `%s'\n",
321 STATE_MY_NAME (sd
), fmt
);
325 sim_do_command (sd
, buf
);
330 /* sim-basics.h defines a number of enumerations, convert each of them
331 to a string representation */
333 map_to_str (unsigned map
)
337 case read_map
: return "read";
338 case write_map
: return "write";
339 case exec_map
: return "exec";
340 case io_map
: return "io";
344 snprintf (str
, sizeof(str
), "(%ld)", (long) map
);
351 access_to_str (unsigned access
)
355 case access_invalid
: return "invalid";
356 case access_read
: return "read";
357 case access_write
: return "write";
358 case access_exec
: return "exec";
359 case access_io
: return "io";
360 case access_read_write
: return "read_write";
361 case access_read_exec
: return "read_exec";
362 case access_write_exec
: return "write_exec";
363 case access_read_write_exec
: return "read_write_exec";
364 case access_read_io
: return "read_io";
365 case access_write_io
: return "write_io";
366 case access_read_write_io
: return "read_write_io";
367 case access_exec_io
: return "exec_io";
368 case access_read_exec_io
: return "read_exec_io";
369 case access_write_exec_io
: return "write_exec_io";
370 case access_read_write_exec_io
: return "read_write_exec_io";
374 snprintf (str
, sizeof(str
), "(%ld)", (long) access
);
381 transfer_to_str (unsigned transfer
)
385 case read_transfer
: return "read";
386 case write_transfer
: return "write";
387 default: return "(error)";