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