Add ABFD argument to sim_create_inferior. Document.
[external/binutils.git] / sim / common / sim-utils.c
1 /* Miscellaneous simulator utilities.
2    Copyright (C) 1997 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 2, or (at your option)
10 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 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.  */
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 xmalloc.  */
57
58 void *
59 zalloc (unsigned long size)
60 {
61   void *memory = (void *) xmalloc (size);
62   memset (memory, 0, size);
63   return memory;
64 }
65
66 void
67 zfree (void *data)
68 {
69   free (data);
70 }
71
72 /* Allocate a sim_state struct.  */
73
74 SIM_DESC
75 sim_state_alloc (void)
76 {
77   SIM_DESC sd = zalloc (sizeof (struct sim_state));
78   sd->base.magic = SIM_MAGIC_NUMBER;
79   return sd;
80 }
81
82 /* Free a sim_state struct.  */
83
84 void
85 sim_state_free (SIM_DESC sd)
86 {
87   ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
88   zfree (sd);
89 }
90
91 /* Turn VALUE into a string with commas.  */
92
93 char *
94 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
95 {
96   int comma = 3;
97   char *endbuf = buf + sizeof_buf - 1;
98
99   *--endbuf = '\0';
100   do {
101     if (comma-- == 0)
102       {
103         *--endbuf = ',';
104         comma = 2;
105       }
106
107     *--endbuf = (value % 10) + '0';
108   } while ((value /= 10) != 0);
109
110   return endbuf;
111 }
112
113 /* Make a copy of ARGV.
114    This can also be used to copy the environment vector.
115    The result is a pointer to the malloc'd copy or NULL if insufficient
116    memory available.  */
117
118 char **
119 sim_copy_argv (argv)
120      char **argv;
121 {
122   int argc;
123   char **copy;
124
125   if (argv == NULL)
126     return NULL;
127
128   /* the vector */
129   for (argc = 0; argv[argc] != NULL; argc++);
130   copy = (char **) malloc ((argc + 1) * sizeof (char *));
131   if (copy == NULL)
132     return NULL;
133
134   /* the strings */
135   for (argc = 0; argv[argc] != NULL; argc++)
136     {
137       int len = strlen (argv[argc]);
138       copy[argc] = malloc (sizeof (char *) * (len + 1));
139       if (copy[argc] == NULL)
140         {
141           freeargv (copy);
142           return NULL;
143         }
144       strcpy (copy[argc], argv[argc]);
145     }
146   copy[argc] = NULL;
147   return copy;
148 }
149
150 /* Analyze a prog_name/prog_bfd and set various fields in the state
151    struct.  */
152
153 SIM_RC
154 sim_analyze_program (sd, prog_name, prog_bfd)
155      SIM_DESC sd;
156      char *prog_name;
157      bfd *prog_bfd;
158 {
159   asection *s;
160   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
161
162   if (prog_bfd != NULL)
163     {
164       if (prog_bfd == STATE_PROG_BFD (sd))
165         /* already analyzed */
166         return SIM_RC_OK;
167       else
168         /* duplicate needed, save the name of the file to be re-opened */
169         prog_name = bfd_get_filename (prog_bfd);
170     }
171
172   /* do we need to duplicate anything? */
173   if (prog_name == NULL)
174     return SIM_RC_OK;
175
176   /* open a new copy of the prog_bfd */
177   prog_bfd = bfd_openr (prog_name, 0);
178   if (prog_bfd == NULL)
179     {
180       sim_io_eprintf (sd, "%s: can't open \"%s\": %s\n", 
181                       STATE_MY_NAME (sd),
182                       prog_name,
183                       bfd_errmsg (bfd_get_error ()));
184       return SIM_RC_FAIL;
185     }
186   if (!bfd_check_format (prog_bfd, bfd_object)) 
187     {
188       sim_io_eprintf (sd, "%s: \"%s\" is not an object file: %s\n",
189                       STATE_MY_NAME (sd),
190                       prog_name,
191                       bfd_errmsg (bfd_get_error ()));
192       bfd_close (prog_bfd);
193       return SIM_RC_FAIL;
194     }
195
196
197   /* update the sim structure */
198   if (STATE_PROG_BFD (sd) != NULL)
199     bfd_close (STATE_PROG_BFD (sd));
200   STATE_PROG_BFD (sd) = prog_bfd;
201   STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
202
203   for (s = prog_bfd->sections; s; s = s->next)
204     if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
205       {
206         STATE_TEXT_SECTION (sd) = s;
207         STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
208         STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
209         break;
210       }
211
212   return SIM_RC_OK;
213 }
214 \f
215 /* Simulator timing support.  */
216
217 /* Called before sim_elapsed_time_since to get a reference point.  */
218
219 SIM_ELAPSED_TIME
220 sim_elapsed_time_get ()
221 {
222 #ifdef HAVE_GETRUSAGE
223   struct rusage mytime;
224   if (getrusage (RUSAGE_SELF, &mytime) == 0)
225     return (SIM_ELAPSED_TIME) (((double) mytime.ru_utime.tv_sec * 1000) + (((double) mytime.ru_utime.tv_usec + 500) / 1000));
226   return 0;
227 #else
228 #ifdef HAVE_TIME
229   return (SIM_ELAPSED_TIME) time ((time_t) 0);
230 #else
231   return 0;
232 #endif
233 #endif
234 }
235
236 /* Return the elapsed time in milliseconds since START.
237    The actual time may be cpu usage (prefered) or wall clock.  */
238
239 unsigned long
240 sim_elapsed_time_since (start)
241      SIM_ELAPSED_TIME start;
242 {
243 #ifdef HAVE_GETRUSAGE
244   return sim_elapsed_time_get () - start;
245 #else
246 #ifdef HAVE_TIME
247   return (sim_elapsed_time_get () - start) * 1000;
248 #else
249   return 0;
250 #endif
251 #endif
252 }