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