1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997, 1998 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 2, or (at your option)
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 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. */
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 xmalloc - xmalloc aborts of the
60 zalloc (unsigned long size
)
62 void *memory
= (void *) xmalloc (size
);
63 memset (memory
, 0, size
);
73 /* Allocate a sim_state struct. */
76 sim_state_alloc (SIM_OPEN_KIND kind
,
77 host_callback
*callback
)
79 SIM_DESC sd
= ZALLOC (struct sim_state
);
81 STATE_MAGIC (sd
) = SIM_MAGIC_NUMBER
;
82 STATE_CALLBACK (sd
) = callback
;
83 STATE_OPEN_KIND (sd
) = kind
;
89 /* Initialize the back link from the cpu struct to the state struct. */
90 /* ??? I can envision a design where the state struct contains an array
91 of pointers to cpu structs, rather than an array of structs themselves.
92 Implementing this is trickier as one may not know what to allocate until
93 one has parsed the args. Parsing the args twice wouldn't be unreasonable,
94 IMHO. If the state struct ever does contain an array of pointers then we
95 can't do this here. */
96 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
97 CPU_STATE (STATE_CPU (sd
, cpu_nr
)) = sd
;
101 #ifdef SIM_STATE_INIT
108 /* Free a sim_state struct. */
111 sim_state_free (SIM_DESC sd
)
113 ASSERT (sd
->base
.magic
== SIM_MAGIC_NUMBER
);
115 #ifdef SIM_STATE_FREE
122 /* Return a pointer to the cpu data for CPU_NAME, or NULL if not found. */
125 sim_cpu_lookup (SIM_DESC sd
, const char *cpu_name
)
129 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
130 if (strcmp (cpu_name
, CPU_NAME (STATE_CPU (sd
, i
))) == 0)
131 return STATE_CPU (sd
, i
);
135 /* Turn VALUE into a string with commas. */
138 sim_add_commas (char *buf
, int sizeof_buf
, unsigned long value
)
141 char *endbuf
= buf
+ sizeof_buf
- 1;
151 *--endbuf
= (value
% 10) + '0';
152 } while ((value
/= 10) != 0);
157 /* Analyze a prog_name/prog_bfd and set various fields in the state
161 sim_analyze_program (sd
, prog_name
, prog_bfd
)
167 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
169 if (prog_bfd
!= NULL
)
171 if (prog_bfd
== STATE_PROG_BFD (sd
))
172 /* already analyzed */
175 /* duplicate needed, save the name of the file to be re-opened */
176 prog_name
= bfd_get_filename (prog_bfd
);
179 /* do we need to duplicate anything? */
180 if (prog_name
== NULL
)
183 /* open a new copy of the prog_bfd */
184 prog_bfd
= bfd_openr (prog_name
, STATE_TARGET (sd
));
185 if (prog_bfd
== NULL
)
187 sim_io_eprintf (sd
, "%s: can't open \"%s\": %s\n",
190 bfd_errmsg (bfd_get_error ()));
193 if (!bfd_check_format (prog_bfd
, bfd_object
))
195 sim_io_eprintf (sd
, "%s: \"%s\" is not an object file: %s\n",
198 bfd_errmsg (bfd_get_error ()));
199 bfd_close (prog_bfd
);
202 if (STATE_ARCHITECTURE (sd
) != NULL
)
203 bfd_set_arch_info (prog_bfd
, STATE_ARCHITECTURE (sd
));
206 if (bfd_get_arch (prog_bfd
) != bfd_arch_unknown
207 && bfd_get_arch (prog_bfd
) != bfd_arch_obscure
)
209 STATE_ARCHITECTURE (sd
) = bfd_get_arch_info (prog_bfd
);
213 /* update the sim structure */
214 if (STATE_PROG_BFD (sd
) != NULL
)
215 bfd_close (STATE_PROG_BFD (sd
));
216 STATE_PROG_BFD (sd
) = prog_bfd
;
217 STATE_START_ADDR (sd
) = bfd_get_start_address (prog_bfd
);
219 for (s
= prog_bfd
->sections
; s
; s
= s
->next
)
220 if (strcmp (bfd_get_section_name (prog_bfd
, s
), ".text") == 0)
222 STATE_TEXT_SECTION (sd
) = s
;
223 STATE_TEXT_START (sd
) = bfd_get_section_vma (prog_bfd
, s
);
224 STATE_TEXT_END (sd
) = STATE_TEXT_START (sd
) + bfd_section_size (prog_bfd
, s
);
231 /* Simulator timing support. */
233 /* Called before sim_elapsed_time_since to get a reference point. */
236 sim_elapsed_time_get ()
238 #ifdef HAVE_GETRUSAGE
239 struct rusage mytime
;
240 if (getrusage (RUSAGE_SELF
, &mytime
) == 0)
241 return 1 + (SIM_ELAPSED_TIME
) (((double) mytime
.ru_utime
.tv_sec
* 1000) + (((double) mytime
.ru_utime
.tv_usec
+ 500) / 1000));
245 return 1 + (SIM_ELAPSED_TIME
) time ((time_t) 0);
252 /* Return the elapsed time in milliseconds since START.
253 The actual time may be cpu usage (prefered) or wall clock. */
256 sim_elapsed_time_since (start
)
257 SIM_ELAPSED_TIME start
;
259 #ifdef HAVE_GETRUSAGE
260 return sim_elapsed_time_get () - start
;
263 return (sim_elapsed_time_get () - start
) * 1000;
272 /* do_command but with printf style formatting of the arguments */
274 sim_do_commandf (SIM_DESC sd
,
281 vasprintf (&buf
, fmt
, ap
);
282 sim_do_command (sd
, buf
);