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