Thu Nov 7 15:19:08 1996 Martin M. Hunt <hunt@pizza.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / remote-sim.c
CommitLineData
40b92220 1/* Generic remote debugging interface for simulators.
47424e79 2 Copyright 1993, 1994 Free Software Foundation, Inc.
40b92220 3 Contributed by Cygnus Support.
47424e79 4 Steve Chamberlain (sac@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
6c9638b4 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
ec25d19b
SC
21
22#include "defs.h"
23#include "inferior.h"
24#include "wait.h"
25#include "value.h"
2b576293 26#include "gdb_string.h"
ec25d19b
SC
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"
abf6a9dc 37#include "callback.h"
40b92220 38
8501c742
SG
39/* Prototypes */
40
41static void dump_mem PARAMS ((char *buf, int len));
42
43static void gdbsim_fetch_register PARAMS ((int regno));
44
45static void gdbsim_store_register PARAMS ((int regno));
46
47static void gdbsim_kill PARAMS ((void));
48
49static void gdbsim_load PARAMS ((char *prog, int fromtty));
50
51static void gdbsim_create_inferior PARAMS ((char *exec_file, char *args, char **env));
52
53static void gdbsim_open PARAMS ((char *args, int from_tty));
54
55static void gdbsim_close PARAMS ((int quitting));
56
57static void gdbsim_detach PARAMS ((char *args, int from_tty));
58
59static void gdbsim_resume PARAMS ((int pid, int step, enum target_signal siggnal));
60
61static int gdbsim_wait PARAMS ((int pid, struct target_waitstatus *status));
62
63static void gdbsim_prepare_to_store PARAMS ((void));
64
65static int gdbsim_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
66 char *myaddr, int len,
67 int write,
68 struct target_ops *target));
69
70static void gdbsim_files_info PARAMS ((struct target_ops *target));
71
72static void gdbsim_mourn_inferior PARAMS ((void));
73
74static void simulator_command PARAMS ((char *args, int from_tty));
75
40b92220
JK
76/* Naming convention:
77
78 sim_* are the interface to the simulator (see remote-sim.h).
cc274a2e 79 sim_callback_* are the stuff which the simulator can see inside GDB.
40b92220 80 gdbsim_* are stuff which is internal to gdb. */
ec25d19b
SC
81
82/* Forward data declarations */
40b92220 83extern struct target_ops gdbsim_ops;
ec25d19b 84
40b92220
JK
85static int program_loaded = 0;
86
87static void
88dump_mem (buf, len)
89 char *buf;
ec25d19b
SC
90 int len;
91{
40b92220
JK
92 if (len <= 8)
93 {
94 if (len == 8 || len == 4)
95 {
96 long l[2];
97 memcpy (l, buf, len);
98 printf_filtered ("\t0x%x", l[0]);
99 printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
100 }
101 else
102 {
103 int i;
104 printf_filtered ("\t");
105 for (i = 0; i < len; i++)
106 printf_filtered ("0x%x ", buf[i]);
107 printf_filtered ("\n");
108 }
109 }
110}
111
112static void
113gdbsim_fetch_register (regno)
114int regno;
115{
116 if (regno == -1)
117 {
118 for (regno = 0; regno < NUM_REGS; regno++)
119 gdbsim_fetch_register (regno);
120 }
121 else
122 {
123 char buf[MAX_REGISTER_RAW_SIZE];
124
125 sim_fetch_register (regno, buf);
126 supply_register (regno, buf);
127 if (sr_get_debug ())
128 {
129 printf_filtered ("gdbsim_fetch_register: %d", regno);
130 /* FIXME: We could print something more intelligible. */
131 dump_mem (buf, REGISTER_RAW_SIZE (regno));
132 }
133 }
ec25d19b
SC
134}
135
6b009ef6 136
a944e79a 137static void
40b92220 138gdbsim_store_register (regno)
ec25d19b
SC
139int regno;
140{
141 if (regno == -1)
40b92220
JK
142 {
143 for (regno = 0; regno < NUM_REGS; regno++)
144 gdbsim_store_register (regno);
145 }
146 else
147 {
148 /* FIXME: Until read_register() returns LONGEST, we have this. */
3beff94e
SC
149 char tmp[MAX_REGISTER_RAW_SIZE];
150 read_register_gen (regno, tmp);
151 sim_store_register (regno, tmp);
40b92220
JK
152 if (sr_get_debug ())
153 {
154 printf_filtered ("gdbsim_store_register: %d", regno);
155 /* FIXME: We could print something more intelligible. */
3beff94e 156 dump_mem (tmp, REGISTER_RAW_SIZE (regno));
40b92220
JK
157 }
158 }
ec25d19b
SC
159}
160
47424e79
DE
161/* Kill the running program. This may involve closing any open files
162 and releasing other resources acquired by the simulated program. */
163
40b92220
JK
164static void
165gdbsim_kill ()
166{
167 if (sr_get_debug ())
168 printf_filtered ("gdbsim_kill\n");
169
170 sim_kill (); /* close fd's, remove mappings */
171 inferior_pid = 0;
172}
173
174/* Load an executable file into the target process. This is expected to
175 not only bring new code into the target process, but also to update
176 GDB's symbol tables to match. */
ec25d19b 177
ec25d19b 178static void
40b92220
JK
179gdbsim_load (prog, fromtty)
180 char *prog;
181 int fromtty;
ec25d19b 182{
40b92220
JK
183 if (sr_get_debug ())
184 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
ec25d19b 185
47424e79
DE
186 inferior_pid = 0;
187
188 /* This must be done before calling gr_load_image. */
40b92220 189 program_loaded = 1;
47424e79
DE
190
191 if (sim_load (prog, fromtty) != 0)
dedcc91d 192 generic_load (prog, fromtty);
40b92220
JK
193}
194
ec25d19b 195
40b92220
JK
196/* Start an inferior process and set inferior_pid to its pid.
197 EXEC_FILE is the file to run.
198 ALLARGS is a string containing the arguments to the program.
199 ENV is the environment vector to pass. Errors reported with error().
200 On VxWorks and various standalone systems, we ignore exec_file. */
ec25d19b
SC
201/* This is called not only when we first attach, but also when the
202 user types "run" after having attached. */
40b92220
JK
203
204static void
205gdbsim_create_inferior (exec_file, args, env)
206 char *exec_file;
ec25d19b
SC
207 char *args;
208 char **env;
209{
47424e79 210 int len;
40b92220 211 char *arg_buf,**argv;
47424e79 212 CORE_ADDR entry_pt;
ec25d19b 213
40b92220
JK
214 if (! program_loaded)
215 error ("No program loaded.");
ec25d19b 216
40b92220
JK
217 if (sr_get_debug ())
218 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
219 exec_file, args);
220
221 if (exec_file == 0 || exec_bfd == 0)
222 error ("No exec file specified.");
ec25d19b 223
47424e79 224 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
40b92220 225
8501c742 226 gdbsim_kill ();
40b92220 227 remove_breakpoints ();
ec25d19b 228 init_wait_for_inferior ();
ec25d19b 229
40b92220
JK
230 len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
231 arg_buf = (char *) alloca (len);
232 arg_buf[0] = '\0';
233 strcat (arg_buf, exec_file);
234 strcat (arg_buf, " ");
235 strcat (arg_buf, args);
236 argv = buildargv (arg_buf);
237 make_cleanup (freeargv, (char *) argv);
47424e79 238 sim_create_inferior (entry_pt, argv, env);
40b92220
JK
239
240 inferior_pid = 42;
241 insert_breakpoints (); /* Needed to get correct instruction in cache */
45dc9be3 242 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
40b92220 243}
ec25d19b 244
40b92220
JK
245/* The open routine takes the rest of the parameters from the command,
246 and (if successful) pushes a new target onto the stack.
247 Targets should supply this routine, if only to provide an error message. */
248/* Called when selecting the simulator. EG: (gdb) target sim name. */
ec25d19b 249
a944e79a 250static void
40b92220
JK
251gdbsim_open (args, from_tty)
252 char *args;
ec25d19b
SC
253 int from_tty;
254{
40b92220 255 if (sr_get_debug ())
47424e79 256 printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
7a29d686
JW
257
258 sim_set_callbacks (&default_callback);
259 default_callback.init (&default_callback);
260
3e38efa0
JSC
261 sim_open (args);
262
40b92220
JK
263 push_target (&gdbsim_ops);
264 target_fetch_registers (-1);
40b92220 265 printf_filtered ("Connected to the simulator.\n");
ec25d19b
SC
266}
267
40b92220
JK
268/* Does whatever cleanup is required for a target that we are no longer
269 going to be calling. Argument says whether we are quitting gdb and
270 should not get hung in case of errors, or whether we want a clean
271 termination even if it takes a while. This routine is automatically
272 always called just before a routine is popped off the target stack.
273 Closing file descriptors and freeing memory are typical things it should
274 do. */
ec25d19b
SC
275/* Close out all files and local state before this target loses control. */
276
a944e79a 277static void
40b92220 278gdbsim_close (quitting)
ec25d19b
SC
279 int quitting;
280{
40b92220
JK
281 if (sr_get_debug ())
282 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
283
284 program_loaded = 0;
285
47424e79 286 sim_close (quitting);
ec25d19b
SC
287}
288
40b92220
JK
289/* Takes a program previously attached to and detaches it.
290 The program may resume execution (some targets do, some don't) and will
291 no longer stop on signals, etc. We better not have left any breakpoints
292 in the program or it'll die when it hits one. ARGS is arguments
293 typed by the user (e.g. a signal to send the process). FROM_TTY
294 says whether to be verbose or not. */
ec25d19b 295/* Terminate the open connection to the remote debugger.
40b92220
JK
296 Use this when you want to detach and do something else with your gdb. */
297
298static void
299gdbsim_detach (args,from_tty)
ec25d19b
SC
300 char *args;
301 int from_tty;
302{
40b92220
JK
303 if (sr_get_debug ())
304 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
a944e79a 305
40b92220
JK
306 pop_target (); /* calls gdbsim_close to do the real work */
307 if (from_tty)
308 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
ec25d19b
SC
309}
310
40b92220
JK
311/* Resume execution of the target process. STEP says whether to single-step
312 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
313 to the target, or zero for no signal. */
314
315static void
316gdbsim_resume (pid, step, siggnal)
67ac9759
JK
317 int pid, step;
318 enum target_signal siggnal;
40b92220 319{
6bafbdfb
JL
320 if (inferior_pid != 42)
321 error ("The program is not being run.");
322
40b92220
JK
323 if (sr_get_debug ())
324 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
ec25d19b 325
67ac9759 326 sim_resume (step, target_signal_to_host (siggnal));
40b92220 327}
ec25d19b 328
40b92220
JK
329/* Wait for inferior process to do something. Return pid of child,
330 or -1 in case of error; store status through argument pointer STATUS,
331 just as `wait' would. */
ec25d19b 332
40b92220 333static int
de43d7d0
SG
334gdbsim_wait (pid, status)
335 int pid;
67ac9759 336 struct target_waitstatus *status;
ec25d19b 337{
592f517a 338 int sigrc;
c7efaa16 339 enum sim_stop reason;
592f517a 340
40b92220 341 if (sr_get_debug ())
67ac9759
JK
342 printf_filtered ("gdbsim_wait\n");
343
c7efaa16 344 sim_stop_reason (&reason, &sigrc);
67ac9759
JK
345 switch (reason)
346 {
347 case sim_exited:
348 status->kind = TARGET_WAITKIND_EXITED;
349 status->value.integer = sigrc;
350 break;
351 case sim_stopped:
352 status->kind = TARGET_WAITKIND_STOPPED;
353 /* The signal in sigrc is a host signal. That probably
354 should be fixed. */
355 status->value.sig = target_signal_from_host (sigrc);
356 break;
357 case sim_signalled:
358 status->kind = TARGET_WAITKIND_SIGNALLED;
359 /* The signal in sigrc is a host signal. That probably
360 should be fixed. */
361 status->value.sig = target_signal_from_host (sigrc);
362 break;
363 }
364
3f0184ac 365 return inferior_pid;
ec25d19b
SC
366}
367
40b92220
JK
368/* Get ready to modify the registers array. On machines which store
369 individual registers, this doesn't need to do anything. On machines
370 which store all the registers in one fell swoop, this makes sure
371 that registers contains all the registers from the program being
372 debugged. */
373
ec25d19b 374static void
40b92220 375gdbsim_prepare_to_store ()
ec25d19b 376{
40b92220 377 /* Do nothing, since we can store individual regs */
ec25d19b
SC
378}
379
40b92220
JK
380static int
381gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
ec25d19b
SC
382 CORE_ADDR memaddr;
383 char *myaddr;
384 int len;
385 int write;
386 struct target_ops *target; /* ignored */
387{
40b92220
JK
388 if (! program_loaded)
389 error ("No program loaded.");
390
391 if (sr_get_debug ())
392 {
393 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
394 myaddr, memaddr, len, write);
395 if (sr_get_debug () && write)
396 dump_mem(myaddr, len);
397 }
398
ec25d19b 399 if (write)
40b92220
JK
400 {
401 len = sim_write (memaddr, myaddr, len);
402 }
ec25d19b 403 else
40b92220
JK
404 {
405 len = sim_read (memaddr, myaddr, len);
406 if (sr_get_debug () && len > 0)
407 dump_mem(myaddr, len);
408 }
ec25d19b
SC
409 return len;
410}
411
40b92220
JK
412static void
413gdbsim_files_info (target)
414 struct target_ops *target;
415{
416 char *file = "nothing";
ec25d19b 417
40b92220
JK
418 if (exec_bfd)
419 file = bfd_get_filename (exec_bfd);
ec25d19b 420
40b92220
JK
421 if (sr_get_debug ())
422 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
423
424 if (exec_bfd)
425 {
426 printf_filtered ("\tAttached to %s running program %s\n",
427 target_shortname, file);
47424e79 428 sim_info (0);
40b92220 429 }
ec25d19b
SC
430}
431
fb506180 432/* Clear the simulator's notion of what the break points are. */
ec25d19b 433
40b92220
JK
434static void
435gdbsim_mourn_inferior ()
436{
437 if (sr_get_debug ())
438 printf_filtered ("gdbsim_mourn_inferior:\n");
ec25d19b 439
40b92220
JK
440 remove_breakpoints ();
441 generic_mourn_inferior ();
ec25d19b 442}
40b92220 443
fb506180
SS
444/* Put a command string, in args, out to MONITOR. Output from MONITOR
445 is placed on the users terminal until the prompt is seen. FIXME: We
446 read the characters ourseleves here cause of a nasty echo. */
ec25d19b 447
fb506180
SS
448static void
449simulator_command (args, from_tty)
450 char *args;
451 int from_tty;
ec25d19b 452{
fb506180
SS
453 sim_do_command (args);
454}
455
456/* Define the target subroutine names */
457
458struct target_ops gdbsim_ops = {
459 "sim", /* to_shortname */
460 "simulator", /* to_longname */
461 "Use the compiled-in simulator.", /* to_doc */
462 gdbsim_open, /* to_open */
463 gdbsim_close, /* to_close */
464 NULL, /* to_attach */
465 gdbsim_detach, /* to_detach */
466 gdbsim_resume, /* to_resume */
467 gdbsim_wait, /* to_wait */
468 gdbsim_fetch_register, /* to_fetch_registers */
469 gdbsim_store_register, /* to_store_registers */
470 gdbsim_prepare_to_store, /* to_prepare_to_store */
471 gdbsim_xfer_inferior_memory, /* to_xfer_memory */
472 gdbsim_files_info, /* to_files_info */
473 memory_insert_breakpoint, /* to_insert_breakpoint */
474 memory_remove_breakpoint, /* to_remove_breakpoint */
475 NULL, /* to_terminal_init */
476 NULL, /* to_terminal_inferior */
477 NULL, /* to_terminal_ours_for_output */
478 NULL, /* to_terminal_ours */
479 NULL, /* to_terminal_info */
480 gdbsim_kill, /* to_kill */
481 gdbsim_load, /* to_load */
482 NULL, /* to_lookup_symbol */
483 gdbsim_create_inferior, /* to_create_inferior */
484 gdbsim_mourn_inferior, /* to_mourn_inferior */
485 0, /* to_can_run */
486 0, /* to_notice_signals */
43fc25c8 487 0, /* to_thread_alive */
78b459a7 488 0, /* to_stop */
fb506180
SS
489 process_stratum, /* to_stratum */
490 NULL, /* to_next */
491 1, /* to_has_all_memory */
492 1, /* to_has_memory */
493 1, /* to_has_stack */
494 1, /* to_has_registers */
495 1, /* to_has_execution */
496 NULL, /* sections */
497 NULL, /* sections_end */
498 OPS_MAGIC, /* to_magic */
ec25d19b
SC
499};
500
ec25d19b
SC
501void
502_initialize_remote_sim ()
503{
40b92220 504 add_target (&gdbsim_ops);
fb506180
SS
505
506 add_com ("sim <command>", class_obscure, simulator_command,
507 "Send a command to the simulator.");
ec25d19b 508}
This page took 0.249379 seconds and 4 git commands to generate.