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