sim: constify prog_name
[external/binutils.git] / sim / ppc / sim_calls.c
1 /*  This file is part of the program psim.
2
3     Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, see <http://www.gnu.org/licenses/>.
17  
18     */
19
20
21 #include <signal.h> /* FIXME - should be machine dependant version */
22 #include <stdarg.h>
23 #include <ctype.h>
24
25 #include "psim.h"
26 #include "options.h"
27
28 #undef printf_filtered /* blow away the mapping */
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #else
37 #ifdef HAVE_STRINGS_H
38 #include <strings.h>
39 #endif
40 #endif
41
42 #include "libiberty.h"
43 #include "bfd.h"
44 #include "gdb/callback.h"
45 #include "gdb/remote-sim.h"
46 #include "gdb/signals.h"
47
48 /* Define the rate at which the simulator should poll the host
49    for a quit. */
50 #ifndef POLL_QUIT_INTERVAL
51 #define POLL_QUIT_INTERVAL 0x20
52 #endif
53
54 static int poll_quit_count = POLL_QUIT_INTERVAL;
55
56 /* Structures used by the simulator, for gdb just have static structures */
57
58 psim *simulator;
59 static device *root_device;
60 static host_callback *callbacks;
61
62 SIM_DESC
63 sim_open (SIM_OPEN_KIND kind,
64           host_callback *callback,
65           struct bfd *abfd,
66           char **argv)
67 {
68   callbacks = callback;
69
70   /* Note: The simulation is not created by sim_open() because
71      complete information is not yet available */
72   /* trace the call */
73   TRACE(trace_gdb, ("sim_open called\n"));
74
75   if (root_device != NULL)
76     sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
77   root_device = psim_tree();
78   simulator = NULL;
79
80   psim_options(root_device, argv + 1);
81
82   if (ppc_trace[trace_opts])
83     print_options ();
84
85   /* fudge our descriptor for now */
86   return (SIM_DESC) 1;
87 }
88
89
90 void
91 sim_close (SIM_DESC sd, int quitting)
92 {
93   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
94   if (ppc_trace[trace_print_info] && simulator != NULL)
95     psim_print_info (simulator, ppc_trace[trace_print_info]);
96 }
97
98
99 SIM_RC
100 sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
101 {
102   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
103                     prog, from_tty));
104   ASSERT(prog != NULL);
105
106   /* create the simulator */
107   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
108   simulator = psim_create(prog, root_device);
109
110   /* bring in all the data section */
111   psim_init(simulator);
112
113   /* get the start address */
114   if (abfd == NULL)
115     {
116       abfd = bfd_openr (prog, 0);
117       if (abfd == NULL)
118         error ("psim: can't open \"%s\": %s\n", 
119                prog, bfd_errmsg (bfd_get_error ()));
120       if (!bfd_check_format (abfd, bfd_object)) 
121         {
122           const char *errmsg = bfd_errmsg (bfd_get_error ());
123           bfd_close (abfd);
124           error ("psim: \"%s\" is not an object file: %s\n",
125                  prog, errmsg);
126         }
127       bfd_close (abfd);
128     }
129
130   return SIM_RC_OK;
131 }
132
133
134 int
135 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
136 {
137   int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
138                                 buf, mem, length);
139   TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
140                     (long)mem, (long)buf, length, result));
141   return result;
142 }
143
144
145 int
146 sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
147 {
148   int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
149                                  buf, mem, length,
150                                  1/*violate_ro*/);
151   TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
152                     (long)mem, (long)buf, length, result));
153   return result;
154 }
155
156 void
157 sim_info (SIM_DESC sd, int verbose)
158 {
159   TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
160   psim_print_info (simulator, verbose);
161 }
162
163
164 SIM_RC
165 sim_create_inferior (SIM_DESC sd,
166                      struct bfd *abfd,
167                      char **argv,
168                      char **envp)
169 {
170   unsigned_word entry_point;
171   TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
172                     entry_point));
173
174   if (simulator == NULL)
175     error ("No program loaded");
176
177   if (abfd != NULL)
178     entry_point = bfd_get_start_address (abfd);
179   else
180     entry_point = 0xfff00000; /* ??? */
181
182   psim_init(simulator);
183   psim_stack(simulator, argv, envp);
184
185   ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
186                               &entry_point, "pc", cooked_transfer) > 0);
187   return SIM_RC_OK;
188 }
189
190
191 void
192 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
193 {
194   psim_status status = psim_get_status(simulator);
195
196   switch (status.reason) {
197   case was_continuing:
198     *reason = sim_stopped;
199     if (status.signal == 0)
200       *sigrc = GDB_SIGNAL_TRAP;
201     else
202       *sigrc = status.signal;
203     break;
204   case was_trap:
205     *reason = sim_stopped;
206     *sigrc = GDB_SIGNAL_TRAP;
207     break;
208   case was_exited:
209     *reason = sim_exited;
210     *sigrc = status.signal;
211     break;
212   case was_signalled:
213     *reason = sim_signalled;
214     *sigrc = status.signal;
215     break;
216   }
217
218   TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
219                     (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
220 }
221
222
223
224 /* Run (or resume) the program.  */
225
226 int
227 sim_stop (SIM_DESC sd)
228 {
229   psim_stop (simulator);
230   return 1;
231 }
232
233 void
234 sim_resume (SIM_DESC sd, int step, int siggnal)
235 {
236   TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
237                     step, siggnal));
238
239   if (step)
240     {
241       psim_step (simulator);
242     }
243   else
244     {
245       psim_run (simulator);
246     }
247 }
248
249 void
250 sim_do_command (SIM_DESC sd, char *cmd)
251 {
252   TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
253                     cmd ? cmd : "(null)"));
254   if (cmd != NULL) {
255     char **argv = buildargv(cmd);
256     psim_command(root_device, argv);
257     freeargv(argv);
258   }
259 }
260
261 char **
262 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
263 {
264   return NULL;
265 }
266
267 /* Polling, if required */
268
269 void
270 sim_io_poll_quit (void)
271 {
272   if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
273     {
274       poll_quit_count = POLL_QUIT_INTERVAL;
275       if (callbacks->poll_quit (callbacks))
276         psim_stop (simulator);
277     }
278 }
279
280
281
282 /* Map simulator IO operations onto the corresponding GDB I/O
283    functions.
284    
285    NB: Only a limited subset of operations are mapped across.  More
286    advanced operations (such as dup or write) must either be mapped to
287    one of the below calls or handled internally */
288
289 int
290 sim_io_read_stdin(char *buf,
291                   int sizeof_buf)
292 {
293   switch (CURRENT_STDIO) {
294   case DO_USE_STDIO:
295     return callbacks->read_stdin(callbacks, buf, sizeof_buf);
296     break;
297   case DONT_USE_STDIO:
298     return callbacks->read(callbacks, 0, buf, sizeof_buf);
299     break;
300   default:
301     error("sim_io_read_stdin: unaccounted switch\n");
302     break;
303   }
304   return 0;
305 }
306
307 int
308 sim_io_write_stdout(const char *buf,
309                     int sizeof_buf)
310 {
311   switch (CURRENT_STDIO) {
312   case DO_USE_STDIO:
313     return callbacks->write_stdout(callbacks, buf, sizeof_buf);
314     break;
315   case DONT_USE_STDIO:
316     return callbacks->write(callbacks, 1, buf, sizeof_buf);
317     break;
318   default:
319     error("sim_io_write_stdout: unaccounted switch\n");
320     break;
321   }
322   return 0;
323 }
324
325 int
326 sim_io_write_stderr(const char *buf,
327                     int sizeof_buf)
328 {
329   switch (CURRENT_STDIO) {
330   case DO_USE_STDIO:
331     /* NB: I think there should be an explicit write_stderr callback */
332     return callbacks->write(callbacks, 3, buf, sizeof_buf);
333     break;
334   case DONT_USE_STDIO:
335     return callbacks->write(callbacks, 3, buf, sizeof_buf);
336     break;
337   default:
338     error("sim_io_write_stderr: unaccounted switch\n");
339     break;
340   }
341   return 0;
342 }
343
344
345 void
346 sim_io_printf_filtered(const char *fmt,
347                        ...)
348 {
349   char message[1024];
350   va_list ap;
351   /* format the message */
352   va_start(ap, fmt);
353   vsprintf(message, fmt, ap);
354   va_end(ap);
355   /* sanity check */
356   if (strlen(message) >= sizeof(message))
357     error("sim_io_printf_filtered: buffer overflow\n");
358   callbacks->printf_filtered(callbacks, "%s", message);
359 }
360
361 void
362 sim_io_flush_stdoutput(void)
363 {
364   switch (CURRENT_STDIO) {
365   case DO_USE_STDIO:
366     callbacks->flush_stdout (callbacks);
367     break;
368   case DONT_USE_STDIO:
369     break;
370   default:
371     error("sim_io_read_stdin: unaccounted switch\n");
372     break;
373   }
374 }
375
376 void
377 sim_io_error (SIM_DESC sd, const char *fmt, ...)
378 {
379   va_list ap;
380   va_start(ap, fmt);
381   callbacks->evprintf_filtered (callbacks, fmt, ap);
382   va_end(ap);
383   callbacks->error (callbacks, "");
384 }
385
386 /****/
387
388 void *
389 zalloc(long size)
390 {
391   void *memory = (void*)xmalloc(size);
392   if (memory == NULL)
393     error("xmalloc failed\n");
394   memset(memory, 0, size);
395   return memory;
396 }