1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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)
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.
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. */
22 #include "sim-assert.h"
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h> /* needed by sys/resource.h */
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
48 #include "libiberty.h"
50 #include "sim-utils.h"
52 /* Global pointer to all state data.
54 struct sim_state *current_state;
56 /* Allocate zero filled memory with xmalloc. */
59 zalloc (unsigned long size)
61 void *memory = (void *) xmalloc (size);
62 memset (memory, 0, size);
72 /* Allocate a sim_state struct. */
75 sim_state_alloc (void)
77 SIM_DESC sd = zalloc (sizeof (struct sim_state));
78 sd->base.magic = SIM_MAGIC_NUMBER;
82 /* Free a sim_state struct. */
85 sim_state_free (SIM_DESC sd)
87 ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
91 /* Turn VALUE into a string with commas. */
94 sim_add_commas (char *buf, int sizeof_buf, unsigned long value)
97 char *endbuf = buf + sizeof_buf - 1;
107 *--endbuf = (value % 10) + '0';
108 } while ((value /= 10) != 0);
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
131 for (argc = 0; argv[argc] != NULL; argc++);
132 copy = (char **) malloc ((argc + 1) * sizeof (char *));
137 for (argc = 0; argv[argc] != NULL; argc++)
139 int len = strlen (argv[argc]);
140 copy[argc] = malloc (sizeof (char *) * (len + 1));
141 if (copy[argc] == NULL)
146 strcpy (copy[argc], argv[argc]);
152 /* Analyze a bfd and set various fields in the state struct. */
155 sim_analyze_program (sd, prog_bfd)
161 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
162 STATE_PROG_BFD (sd) = prog_bfd;
163 STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
165 for (s = prog_bfd->sections; s; s = s->next)
166 if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
168 STATE_TEXT_SECTION (sd) = s;
169 STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
170 STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
175 /* Simulator timing support. */
177 /* Called before sim_elapsed_time_since to get a reference point. */
180 sim_elapsed_time_get ()
182 #ifdef HAVE_GETRUSAGE
183 struct rusage mytime;
184 if (getrusage (RUSAGE_SELF, &mytime) == 0)
185 return (SIM_ELAPSED_TIME) (((double) mytime.ru_utime.tv_sec * 1000) + (((double) mytime.ru_utime.tv_usec + 500) / 1000));
189 return (SIM_ELAPSED_TIME) time ((time_t) 0);
196 /* Return the elapsed time in milliseconds since START.
197 The actual time may be cpu usage (prefered) or wall clock. */
200 sim_elapsed_time_since (start)
201 SIM_ELAPSED_TIME start;
203 #ifdef HAVE_GETRUSAGE
204 return sim_elapsed_time_get () - start;
207 return (sim_elapsed_time_get () - start) * 1000;