gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / sim / ppc / sim_calls.c
CommitLineData
c906108c
SS
1/* This file is part of the program psim.
2
601cecf0 3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
c906108c
SS
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
3fd725ef 7 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
51b318de 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
c906108c
SS
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
2b3cc94f 42#include "libiberty.h"
c906108c 43#include "bfd.h"
3c25f8c7
AC
44#include "gdb/callback.h"
45#include "gdb/remote-sim.h"
aba6488e 46#include "gdb/signals.h"
c906108c 47
7a292a7a
SS
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
54static int poll_quit_count = POLL_QUIT_INTERVAL;
c906108c
SS
55
56/* Structures used by the simulator, for gdb just have static structures */
57
91cd1042 58psim *simulator;
c906108c
SS
59static device *root_device;
60static host_callback *callbacks;
61
c906108c
SS
62SIM_DESC
63sim_open (SIM_OPEN_KIND kind,
64 host_callback *callback,
6b4a8935 65 struct bfd *abfd,
2e3d4f4d 66 char * const *argv)
c906108c
SS
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
dea827fc
MF
80 if (psim_options (root_device, argv + 1, kind) == NULL)
81 return NULL;
c906108c
SS
82
83 if (ppc_trace[trace_opts])
84 print_options ();
85
86 /* fudge our descriptor for now */
87 return (SIM_DESC) 1;
88}
89
90
91void
92sim_close (SIM_DESC sd, int quitting)
93{
94 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
95 if (ppc_trace[trace_print_info] && simulator != NULL)
96 psim_print_info (simulator, ppc_trace[trace_print_info]);
97}
98
99
100SIM_RC
b2b255bd 101sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
c906108c
SS
102{
103 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
104 prog, from_tty));
105 ASSERT(prog != NULL);
106
107 /* create the simulator */
108 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
109 simulator = psim_create(prog, root_device);
110
111 /* bring in all the data section */
112 psim_init(simulator);
113
114 /* get the start address */
115 if (abfd == NULL)
116 {
117 abfd = bfd_openr (prog, 0);
118 if (abfd == NULL)
119 error ("psim: can't open \"%s\": %s\n",
120 prog, bfd_errmsg (bfd_get_error ()));
121 if (!bfd_check_format (abfd, bfd_object))
122 {
123 const char *errmsg = bfd_errmsg (bfd_get_error ());
124 bfd_close (abfd);
125 error ("psim: \"%s\" is not an object file: %s\n",
126 prog, errmsg);
127 }
128 bfd_close (abfd);
129 }
130
131 return SIM_RC_OK;
132}
133
134
135int
136sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
137{
138 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
139 buf, mem, length);
140 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
141 (long)mem, (long)buf, length, result));
142 return result;
143}
144
145
146int
5558e7e6 147sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
c906108c
SS
148{
149 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
150 buf, mem, length,
151 1/*violate_ro*/);
152 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
153 (long)mem, (long)buf, length, result));
154 return result;
155}
156
c906108c
SS
157void
158sim_info (SIM_DESC sd, int verbose)
159{
160 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
161 psim_print_info (simulator, verbose);
162}
163
164
165SIM_RC
166sim_create_inferior (SIM_DESC sd,
6b4a8935 167 struct bfd *abfd,
2e3d4f4d
MF
168 char * const *argv,
169 char * const *envp)
c906108c
SS
170{
171 unsigned_word entry_point;
172 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
173 entry_point));
174
175 if (simulator == NULL)
176 error ("No program loaded");
177
178 if (abfd != NULL)
179 entry_point = bfd_get_start_address (abfd);
180 else
181 entry_point = 0xfff00000; /* ??? */
182
183 psim_init(simulator);
184 psim_stack(simulator, argv, envp);
185
601cecf0
AC
186 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
187 &entry_point, "pc", cooked_transfer) > 0);
c906108c
SS
188 return SIM_RC_OK;
189}
190
191
192void
193sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
194{
195 psim_status status = psim_get_status(simulator);
196
197 switch (status.reason) {
198 case was_continuing:
199 *reason = sim_stopped;
200 if (status.signal == 0)
a493e3e2 201 *sigrc = GDB_SIGNAL_TRAP;
c906108c
SS
202 else
203 *sigrc = status.signal;
204 break;
205 case was_trap:
206 *reason = sim_stopped;
a493e3e2 207 *sigrc = GDB_SIGNAL_TRAP;
c906108c
SS
208 break;
209 case was_exited:
210 *reason = sim_exited;
211 *sigrc = status.signal;
212 break;
213 case was_signalled:
214 *reason = sim_signalled;
215 *sigrc = status.signal;
216 break;
217 }
218
219 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
220 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
221}
222
223
224
225/* Run (or resume) the program. */
226
227int
228sim_stop (SIM_DESC sd)
229{
230 psim_stop (simulator);
231 return 1;
232}
233
234void
235sim_resume (SIM_DESC sd, int step, int siggnal)
236{
237 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
238 step, siggnal));
239
240 if (step)
241 {
242 psim_step (simulator);
243 }
244 else
245 {
246 psim_run (simulator);
247 }
248}
249
250void
60d847df 251sim_do_command (SIM_DESC sd, const char *cmd)
c906108c
SS
252{
253 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
254 cmd ? cmd : "(null)"));
255 if (cmd != NULL) {
256 char **argv = buildargv(cmd);
257 psim_command(root_device, argv);
258 freeargv(argv);
259 }
260}
261
af9f7da7 262char **
3cb2ab1a 263sim_complete_command (SIM_DESC sd, const char *text, const char *word)
af9f7da7
MF
264{
265 return NULL;
266}
c906108c
SS
267
268/* Polling, if required */
269
270void
271sim_io_poll_quit (void)
272{
7a292a7a 273 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
c906108c 274 {
7a292a7a 275 poll_quit_count = POLL_QUIT_INTERVAL;
c906108c
SS
276 if (callbacks->poll_quit (callbacks))
277 psim_stop (simulator);
278 }
279}
280
281
282
283/* Map simulator IO operations onto the corresponding GDB I/O
284 functions.
285
286 NB: Only a limited subset of operations are mapped across. More
287 advanced operations (such as dup or write) must either be mapped to
288 one of the below calls or handled internally */
289
290int
291sim_io_read_stdin(char *buf,
292 int sizeof_buf)
293{
294 switch (CURRENT_STDIO) {
295 case DO_USE_STDIO:
296 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
297 break;
298 case DONT_USE_STDIO:
299 return callbacks->read(callbacks, 0, buf, sizeof_buf);
300 break;
301 default:
302 error("sim_io_read_stdin: unaccounted switch\n");
303 break;
304 }
305 return 0;
306}
307
308int
309sim_io_write_stdout(const char *buf,
310 int sizeof_buf)
311{
312 switch (CURRENT_STDIO) {
313 case DO_USE_STDIO:
314 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
315 break;
316 case DONT_USE_STDIO:
317 return callbacks->write(callbacks, 1, buf, sizeof_buf);
318 break;
319 default:
320 error("sim_io_write_stdout: unaccounted switch\n");
321 break;
322 }
323 return 0;
324}
325
326int
327sim_io_write_stderr(const char *buf,
328 int sizeof_buf)
329{
330 switch (CURRENT_STDIO) {
331 case DO_USE_STDIO:
332 /* NB: I think there should be an explicit write_stderr callback */
333 return callbacks->write(callbacks, 3, buf, sizeof_buf);
334 break;
335 case DONT_USE_STDIO:
336 return callbacks->write(callbacks, 3, buf, sizeof_buf);
337 break;
338 default:
339 error("sim_io_write_stderr: unaccounted switch\n");
340 break;
341 }
342 return 0;
343}
344
345
346void
347sim_io_printf_filtered(const char *fmt,
348 ...)
349{
350 char message[1024];
351 va_list ap;
352 /* format the message */
353 va_start(ap, fmt);
354 vsprintf(message, fmt, ap);
355 va_end(ap);
356 /* sanity check */
357 if (strlen(message) >= sizeof(message))
358 error("sim_io_printf_filtered: buffer overflow\n");
359 callbacks->printf_filtered(callbacks, "%s", message);
360}
361
362void
363sim_io_flush_stdoutput(void)
364{
365 switch (CURRENT_STDIO) {
366 case DO_USE_STDIO:
367 callbacks->flush_stdout (callbacks);
368 break;
369 case DONT_USE_STDIO:
370 break;
371 default:
372 error("sim_io_read_stdin: unaccounted switch\n");
373 break;
374 }
375}
376
de46f45f
MG
377void
378sim_io_error (SIM_DESC sd, const char *fmt, ...)
379{
380 va_list ap;
381 va_start(ap, fmt);
382 callbacks->evprintf_filtered (callbacks, fmt, ap);
383 va_end(ap);
384 callbacks->error (callbacks, "");
385}
386
c906108c
SS
387/****/
388
625ce09c
JB
389void NORETURN
390error (const char *msg, ...)
391{
392 va_list ap;
393 va_start(ap, msg);
394 callbacks->evprintf_filtered (callbacks, msg, ap);
395 va_end(ap);
396 callbacks->error (callbacks, "");
397}
398
c906108c
SS
399void *
400zalloc(long size)
401{
402 void *memory = (void*)xmalloc(size);
403 if (memory == NULL)
404 error("xmalloc failed\n");
405 memset(memory, 0, size);
406 return memory;
407}
This page took 0.944398 seconds and 4 git commands to generate.