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