]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-sim.c
* target.h: Add enum target_waitkind, enum target_signal, and
[thirdparty/binutils-gdb.git] / gdb / remote-sim.c
CommitLineData
40b92220
JK
1/* Generic remote debugging interface for simulators.
2 Copyright 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Steve Chamberlain (sac@cygnus.com) and Doug Evans (dje@cygnus.com).
ec25d19b
SC
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22#include "defs.h"
23#include "inferior.h"
24#include "wait.h"
25#include "value.h"
26#include <string.h>
27#include <ctype.h>
28#include <fcntl.h>
29#include <signal.h>
30#include <setjmp.h>
31#include <errno.h>
32#include "terminal.h"
33#include "target.h"
34#include "gdbcore.h"
59c2be48 35#include "remote-sim.h"
f1e7bafc 36#include "remote-utils.h"
40b92220
JK
37
38/* Naming convention:
39
40 sim_* are the interface to the simulator (see remote-sim.h).
41
42 gdbsim_* are stuff which is internal to gdb. */
ec25d19b
SC
43
44/* Forward data declarations */
40b92220 45extern struct target_ops gdbsim_ops;
ec25d19b 46
40b92220
JK
47static int program_loaded = 0;
48
49static void
50dump_mem (buf, len)
51 char *buf;
ec25d19b
SC
52 int len;
53{
40b92220
JK
54 if (len <= 8)
55 {
56 if (len == 8 || len == 4)
57 {
58 long l[2];
59 memcpy (l, buf, len);
60 printf_filtered ("\t0x%x", l[0]);
61 printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
62 }
63 else
64 {
65 int i;
66 printf_filtered ("\t");
67 for (i = 0; i < len; i++)
68 printf_filtered ("0x%x ", buf[i]);
69 printf_filtered ("\n");
70 }
71 }
72}
73
74static void
75gdbsim_fetch_register (regno)
76int regno;
77{
78 if (regno == -1)
79 {
80 for (regno = 0; regno < NUM_REGS; regno++)
81 gdbsim_fetch_register (regno);
82 }
83 else
84 {
85 char buf[MAX_REGISTER_RAW_SIZE];
86
87 sim_fetch_register (regno, buf);
88 supply_register (regno, buf);
89 if (sr_get_debug ())
90 {
91 printf_filtered ("gdbsim_fetch_register: %d", regno);
92 /* FIXME: We could print something more intelligible. */
93 dump_mem (buf, REGISTER_RAW_SIZE (regno));
94 }
95 }
ec25d19b
SC
96}
97
a944e79a 98static void
40b92220 99gdbsim_store_register (regno)
ec25d19b
SC
100int regno;
101{
102 if (regno == -1)
40b92220
JK
103 {
104 for (regno = 0; regno < NUM_REGS; regno++)
105 gdbsim_store_register (regno);
106 }
107 else
108 {
109 /* FIXME: Until read_register() returns LONGEST, we have this. */
3beff94e
SC
110 char tmp[MAX_REGISTER_RAW_SIZE];
111 read_register_gen (regno, tmp);
112 sim_store_register (regno, tmp);
40b92220
JK
113 if (sr_get_debug ())
114 {
115 printf_filtered ("gdbsim_store_register: %d", regno);
116 /* FIXME: We could print something more intelligible. */
3beff94e 117 dump_mem (tmp, REGISTER_RAW_SIZE (regno));
40b92220
JK
118 }
119 }
ec25d19b
SC
120}
121
40b92220
JK
122static void
123gdbsim_kill ()
124{
125 if (sr_get_debug ())
126 printf_filtered ("gdbsim_kill\n");
127
128 sim_kill (); /* close fd's, remove mappings */
129 inferior_pid = 0;
130}
131
132/* Load an executable file into the target process. This is expected to
133 not only bring new code into the target process, but also to update
134 GDB's symbol tables to match. */
ec25d19b 135
ec25d19b 136static void
40b92220
JK
137gdbsim_load (prog, fromtty)
138 char *prog;
139 int fromtty;
ec25d19b 140{
40b92220
JK
141 if (sr_get_debug ())
142 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
ec25d19b
SC
143
144 inferior_pid = 0;
40b92220 145 program_loaded = 1;
1b68cb4f 146 gr_load_image (prog, fromtty);
40b92220
JK
147}
148
ec25d19b 149
40b92220
JK
150/* Start an inferior process and set inferior_pid to its pid.
151 EXEC_FILE is the file to run.
152 ALLARGS is a string containing the arguments to the program.
153 ENV is the environment vector to pass. Errors reported with error().
154 On VxWorks and various standalone systems, we ignore exec_file. */
ec25d19b
SC
155/* This is called not only when we first attach, but also when the
156 user types "run" after having attached. */
40b92220
JK
157
158static void
159gdbsim_create_inferior (exec_file, args, env)
160 char *exec_file;
ec25d19b
SC
161 char *args;
162 char **env;
163{
40b92220
JK
164 int len,entry_pt;
165 char *arg_buf,**argv;
ec25d19b 166
40b92220
JK
167 if (! program_loaded)
168 error ("No program loaded.");
ec25d19b 169
40b92220
JK
170 if (sr_get_debug ())
171 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
172 exec_file, args);
173
174 if (exec_file == 0 || exec_bfd == 0)
175 error ("No exec file specified.");
ec25d19b
SC
176
177 entry_pt = (int) bfd_get_start_address (exec_bfd);
40b92220
JK
178
179 gdbsim_kill (NULL, NULL);
180 remove_breakpoints ();
ec25d19b 181 init_wait_for_inferior ();
ec25d19b 182
40b92220
JK
183 len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
184 arg_buf = (char *) alloca (len);
185 arg_buf[0] = '\0';
186 strcat (arg_buf, exec_file);
187 strcat (arg_buf, " ");
188 strcat (arg_buf, args);
189 argv = buildargv (arg_buf);
190 make_cleanup (freeargv, (char *) argv);
191 /* FIXME: remote-sim.h says targets that don't support this return
192 non-zero. Perhaps distinguish between "not supported" and other errors?
193 Or maybe that can be the only error. */
194 if (sim_set_args (argv, env) != 0)
195 return;
196
197 inferior_pid = 42;
198 insert_breakpoints (); /* Needed to get correct instruction in cache */
199 proceed (entry_pt, -1, 0);
200}
ec25d19b 201
40b92220
JK
202/* The open routine takes the rest of the parameters from the command,
203 and (if successful) pushes a new target onto the stack.
204 Targets should supply this routine, if only to provide an error message. */
205/* Called when selecting the simulator. EG: (gdb) target sim name. */
ec25d19b 206
a944e79a 207static void
40b92220
JK
208gdbsim_open (args, from_tty)
209 char *args;
ec25d19b
SC
210 int from_tty;
211{
40b92220
JK
212 if (sr_get_debug ())
213 printf_filtered ("gdbsim_open: args \"%s\"\n", args);
214
215 if (sim_open (args) != 0)
216 {
217 /* FIXME: This is totally bogus. sim_open should have a way to
218 tell us what the error was, so we can tell the user. */
219 error ("Unable to initialize simulator (insufficient memory?).");
220 return;
221 }
222
223 push_target (&gdbsim_ops);
224 target_fetch_registers (-1);
225
226 printf_filtered ("Connected to the simulator.\n");
ec25d19b
SC
227}
228
40b92220
JK
229/* Does whatever cleanup is required for a target that we are no longer
230 going to be calling. Argument says whether we are quitting gdb and
231 should not get hung in case of errors, or whether we want a clean
232 termination even if it takes a while. This routine is automatically
233 always called just before a routine is popped off the target stack.
234 Closing file descriptors and freeing memory are typical things it should
235 do. */
ec25d19b
SC
236/* Close out all files and local state before this target loses control. */
237
a944e79a 238static void
40b92220 239gdbsim_close (quitting)
ec25d19b
SC
240 int quitting;
241{
40b92220
JK
242 if (sr_get_debug ())
243 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
244
245 program_loaded = 0;
246
247 /* FIXME: Need to call sim_close() to close all files and
248 delete all mappings. */
ec25d19b
SC
249}
250
40b92220
JK
251/* Takes a program previously attached to and detaches it.
252 The program may resume execution (some targets do, some don't) and will
253 no longer stop on signals, etc. We better not have left any breakpoints
254 in the program or it'll die when it hits one. ARGS is arguments
255 typed by the user (e.g. a signal to send the process). FROM_TTY
256 says whether to be verbose or not. */
ec25d19b 257/* Terminate the open connection to the remote debugger.
40b92220
JK
258 Use this when you want to detach and do something else with your gdb. */
259
260static void
261gdbsim_detach (args,from_tty)
ec25d19b
SC
262 char *args;
263 int from_tty;
264{
40b92220
JK
265 if (sr_get_debug ())
266 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
a944e79a 267
40b92220
JK
268 pop_target (); /* calls gdbsim_close to do the real work */
269 if (from_tty)
270 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
ec25d19b
SC
271}
272
40b92220
JK
273/* Resume execution of the target process. STEP says whether to single-step
274 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
275 to the target, or zero for no signal. */
276
277static void
278gdbsim_resume (pid, step, siggnal)
67ac9759
JK
279 int pid, step;
280 enum target_signal siggnal;
40b92220
JK
281{
282 if (sr_get_debug ())
283 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
ec25d19b 284
67ac9759 285 sim_resume (step, target_signal_to_host (siggnal));
40b92220 286}
ec25d19b 287
40b92220
JK
288/* Wait for inferior process to do something. Return pid of child,
289 or -1 in case of error; store status through argument pointer STATUS,
290 just as `wait' would. */
ec25d19b 291
40b92220 292static int
de43d7d0
SG
293gdbsim_wait (pid, status)
294 int pid;
67ac9759 295 struct target_waitstatus *status;
ec25d19b 296{
592f517a 297 int sigrc;
c7efaa16 298 enum sim_stop reason;
592f517a 299
40b92220 300 if (sr_get_debug ())
67ac9759
JK
301 printf_filtered ("gdbsim_wait\n");
302
c7efaa16 303 sim_stop_reason (&reason, &sigrc);
67ac9759
JK
304 switch (reason)
305 {
306 case sim_exited:
307 status->kind = TARGET_WAITKIND_EXITED;
308 status->value.integer = sigrc;
309 break;
310 case sim_stopped:
311 status->kind = TARGET_WAITKIND_STOPPED;
312 /* The signal in sigrc is a host signal. That probably
313 should be fixed. */
314 status->value.sig = target_signal_from_host (sigrc);
315 break;
316 case sim_signalled:
317 status->kind = TARGET_WAITKIND_SIGNALLED;
318 /* The signal in sigrc is a host signal. That probably
319 should be fixed. */
320 status->value.sig = target_signal_from_host (sigrc);
321 break;
322 }
323
3f0184ac 324 return inferior_pid;
ec25d19b
SC
325}
326
40b92220
JK
327/* Get ready to modify the registers array. On machines which store
328 individual registers, this doesn't need to do anything. On machines
329 which store all the registers in one fell swoop, this makes sure
330 that registers contains all the registers from the program being
331 debugged. */
332
ec25d19b 333static void
40b92220 334gdbsim_prepare_to_store ()
ec25d19b 335{
40b92220 336 /* Do nothing, since we can store individual regs */
ec25d19b
SC
337}
338
40b92220
JK
339static int
340gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
ec25d19b
SC
341 CORE_ADDR memaddr;
342 char *myaddr;
343 int len;
344 int write;
345 struct target_ops *target; /* ignored */
346{
40b92220
JK
347 if (! program_loaded)
348 error ("No program loaded.");
349
350 if (sr_get_debug ())
351 {
352 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
353 myaddr, memaddr, len, write);
354 if (sr_get_debug () && write)
355 dump_mem(myaddr, len);
356 }
357
ec25d19b 358 if (write)
40b92220
JK
359 {
360 len = sim_write (memaddr, myaddr, len);
361 }
ec25d19b 362 else
40b92220
JK
363 {
364 len = sim_read (memaddr, myaddr, len);
365 if (sr_get_debug () && len > 0)
366 dump_mem(myaddr, len);
367 }
ec25d19b
SC
368 return len;
369}
370
40b92220
JK
371static void
372gdbsim_files_info (target)
373 struct target_ops *target;
374{
375 char *file = "nothing";
ec25d19b 376
40b92220
JK
377 if (exec_bfd)
378 file = bfd_get_filename (exec_bfd);
ec25d19b 379
40b92220
JK
380 if (sr_get_debug ())
381 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
382
383 if (exec_bfd)
384 {
385 printf_filtered ("\tAttached to %s running program %s\n",
386 target_shortname, file);
c7efaa16 387 sim_info (printf_filtered, 0);
40b92220 388 }
ec25d19b
SC
389}
390
40b92220 391/* Clear the sims notion of what the break points are. */
ec25d19b 392
40b92220
JK
393static void
394gdbsim_mourn_inferior ()
395{
396 if (sr_get_debug ())
397 printf_filtered ("gdbsim_mourn_inferior:\n");
ec25d19b 398
40b92220
JK
399 remove_breakpoints ();
400 generic_mourn_inferior ();
ec25d19b 401}
40b92220 402
ec25d19b
SC
403/* Define the target subroutine names */
404
40b92220 405struct target_ops gdbsim_ops =
ec25d19b
SC
406{
407 "sim", "simulator",
408 "Use the simulator",
40b92220
JK
409 gdbsim_open, gdbsim_close,
410 0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
411 gdbsim_fetch_register, gdbsim_store_register,
412 gdbsim_prepare_to_store,
413 gdbsim_xfer_inferior_memory,
414 gdbsim_files_info,
415 0, 0, /* Breakpoints */
ec25d19b 416 0, 0, 0, 0, 0, /* Terminal handling */
40b92220
JK
417 gdbsim_kill, /* kill */
418 gdbsim_load,
ec25d19b 419 0, /* lookup_symbol */
40b92220
JK
420 gdbsim_create_inferior, /* create_inferior */
421 gdbsim_mourn_inferior, /* mourn_inferior */
ec25d19b
SC
422 0, /* can_run */
423 0, /* notice_signals */
424 process_stratum, 0, /* next */
425 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
40b92220 426 0, 0, /* Section pointers */
ec25d19b
SC
427 OPS_MAGIC, /* Always the last thing */
428};
429
ec25d19b
SC
430void
431_initialize_remote_sim ()
432{
40b92220 433 add_target (&gdbsim_ops);
ec25d19b 434}