sim: common: trim trailing whitespace
[deliverable/binutils-gdb.git] / sim / common / sim-utils.c
1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997, 1998, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
5
6 This file is part of GDB, the GNU debugger.
7
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.
12
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.
17
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/>. */
20
21 #include "sim-main.h"
22 #include "sim-assert.h"
23
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27
28 #ifdef HAVE_TIME_H
29 #include <time.h>
30 #endif
31
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h> /* needed by sys/resource.h */
34 #endif
35
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
38 #endif
39
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #else
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>
45 #endif
46 #endif
47
48 #include "libiberty.h"
49 #include "bfd.h"
50 #include "sim-utils.h"
51
52 /* Global pointer to all state data.
53 Set by sim_resume. */
54 struct sim_state *current_state;
55
56 /* Allocate zero filled memory with xcalloc - xcalloc aborts if the
57 allocation fails. */
58
59 void *
60 zalloc (unsigned long size)
61 {
62 return xcalloc (1, size);
63 }
64
65 /* Allocate a sim_state struct. */
66
67 SIM_DESC
68 sim_state_alloc (SIM_OPEN_KIND kind,
69 host_callback *callback)
70 {
71 SIM_DESC sd = ZALLOC (struct sim_state);
72
73 STATE_MAGIC (sd) = SIM_MAGIC_NUMBER;
74 STATE_CALLBACK (sd) = callback;
75 STATE_OPEN_KIND (sd) = kind;
76
77 #if 0
78 {
79 int cpu_nr;
80
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
87 can't do this here.
88 ??? See also sim_post_argv_init*/
89 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
90 {
91 CPU_STATE (STATE_CPU (sd, cpu_nr)) = sd;
92 CPU_INDEX (STATE_CPU (sd, cpu_nr)) = cpu_nr;
93 }
94 }
95 #endif
96
97 #ifdef SIM_STATE_INIT
98 SIM_STATE_INIT (sd);
99 #endif
100
101 return sd;
102 }
103
104 /* Free a sim_state struct. */
105
106 void
107 sim_state_free (SIM_DESC sd)
108 {
109 ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
110
111 #ifdef SIM_STATE_FREE
112 SIM_STATE_FREE (sd);
113 #endif
114
115 free (sd);
116 }
117
118 /* Return a pointer to the cpu data for CPU_NAME, or NULL if not found. */
119
120 sim_cpu *
121 sim_cpu_lookup (SIM_DESC sd, const char *cpu_name)
122 {
123 int i;
124
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);
128 return NULL;
129 }
130
131 /* Return the prefix to use for a CPU specific message (typically an
132 error message). */
133
134 const char *
135 sim_cpu_msg_prefix (sim_cpu *cpu)
136 {
137 #if MAX_NR_PROCESSORS == 1
138 return "";
139 #else
140 static char *prefix;
141
142 if (prefix == NULL)
143 {
144 int maxlen = 0;
145 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
146 {
147 int len = strlen (CPU_NAME (STATE_CPU (sd, i)));
148 if (len > maxlen)
149 maxlen = len;
150 }
151 prefix = (char *) xmalloc (maxlen + 5);
152 }
153 sprintf (prefix, "%s: ", CPU_NAME (cpu));
154 return prefix;
155 #endif
156 }
157
158 /* Cover fn to sim_io_eprintf. */
159
160 void
161 sim_io_eprintf_cpu (sim_cpu *cpu, const char *fmt, ...)
162 {
163 SIM_DESC sd = CPU_STATE (cpu);
164 va_list ap;
165
166 va_start (ap, fmt);
167 sim_io_eprintf (sd, "%s", sim_cpu_msg_prefix (cpu));
168 sim_io_evprintf (sd, fmt, ap);
169 va_end (ap);
170 }
171
172 /* Turn VALUE into a string with commas. */
173
174 char *
175 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
176 {
177 int comma = 3;
178 char *endbuf = buf + sizeof_buf - 1;
179
180 *--endbuf = '\0';
181 do {
182 if (comma-- == 0)
183 {
184 *--endbuf = ',';
185 comma = 2;
186 }
187
188 *--endbuf = (value % 10) + '0';
189 } while ((value /= 10) != 0);
190
191 return endbuf;
192 }
193
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
196 STATE_PROG_BFD
197 STATE_START_ADDR
198 STATE_TEXT_SECTION
199 STATE_TEXT_START
200 STATE_TEXT_END
201
202 PROG_NAME is the file name of the executable or NULL.
203 PROG_BFD is its bfd or NULL.
204
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.
207
208 Implicit inputs: STATE_MY_NAME(sd), STATE_TARGET(sd),
209 STATE_ARCHITECTURE(sd).
210
211 A new bfd is created so the app isn't required to keep its copy of the
212 bfd open. */
213
214 SIM_RC
215 sim_analyze_program (SIM_DESC sd, char *prog_name, bfd *prog_bfd)
216 {
217 asection *s;
218 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
219
220 if (prog_bfd != NULL)
221 {
222 if (prog_bfd == STATE_PROG_BFD (sd))
223 /* already analyzed */
224 return SIM_RC_OK;
225 else
226 /* duplicate needed, save the name of the file to be re-opened */
227 prog_name = bfd_get_filename (prog_bfd);
228 }
229
230 /* do we need to duplicate anything? */
231 if (prog_name == NULL)
232 return SIM_RC_OK;
233
234 /* open a new copy of the prog_bfd */
235 prog_bfd = bfd_openr (prog_name, STATE_TARGET (sd));
236 if (prog_bfd == NULL)
237 {
238 sim_io_eprintf (sd, "%s: can't open \"%s\": %s\n",
239 STATE_MY_NAME (sd),
240 prog_name,
241 bfd_errmsg (bfd_get_error ()));
242 return SIM_RC_FAIL;
243 }
244 if (!bfd_check_format (prog_bfd, bfd_object))
245 {
246 sim_io_eprintf (sd, "%s: \"%s\" is not an object file: %s\n",
247 STATE_MY_NAME (sd),
248 prog_name,
249 bfd_errmsg (bfd_get_error ()));
250 bfd_close (prog_bfd);
251 return SIM_RC_FAIL;
252 }
253 if (STATE_ARCHITECTURE (sd) != NULL)
254 bfd_set_arch_info (prog_bfd, STATE_ARCHITECTURE (sd));
255 else
256 {
257 if (bfd_get_arch (prog_bfd) != bfd_arch_unknown
258 && bfd_get_arch (prog_bfd) != bfd_arch_obscure)
259 {
260 STATE_ARCHITECTURE (sd) = bfd_get_arch_info (prog_bfd);
261 }
262 }
263
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);
269
270 for (s = prog_bfd->sections; s; s = s->next)
271 if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
272 {
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);
276 break;
277 }
278
279 bfd_cache_close (prog_bfd);
280
281 return SIM_RC_OK;
282 }
283 \f
284 /* Simulator timing support. */
285
286 /* Called before sim_elapsed_time_since to get a reference point. */
287
288 SIM_ELAPSED_TIME
289 sim_elapsed_time_get (void)
290 {
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));
295 return 1;
296 #else
297 #ifdef HAVE_TIME
298 return 1 + (SIM_ELAPSED_TIME) time ((time_t) 0);
299 #else
300 return 1;
301 #endif
302 #endif
303 }
304
305 /* Return the elapsed time in milliseconds since START.
306 The actual time may be cpu usage (preferred) or wall clock. */
307
308 unsigned long
309 sim_elapsed_time_since (SIM_ELAPSED_TIME start)
310 {
311 #ifdef HAVE_GETRUSAGE
312 return sim_elapsed_time_get () - start;
313 #else
314 #ifdef HAVE_TIME
315 return (sim_elapsed_time_get () - start) * 1000;
316 #else
317 return 0;
318 #endif
319 #endif
320 }
321
322
323
324 /* do_command but with printf style formatting of the arguments */
325 void
326 sim_do_commandf (SIM_DESC sd,
327 const char *fmt,
328 ...)
329 {
330 va_list ap;
331 char *buf;
332 va_start (ap, fmt);
333 if (vasprintf (&buf, fmt, ap) < 0)
334 {
335 sim_io_eprintf (sd, "%s: asprintf failed for `%s'\n",
336 STATE_MY_NAME (sd), fmt);
337 return;
338 }
339 sim_do_command (sd, buf);
340 va_end (ap);
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 }
This page took 0.03915 seconds and 5 git commands to generate.