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