* stabs.c (_bfd_link_section_stabs): If the output_section field
[deliverable/binutils-gdb.git] / gdb / remote-sim.c
1 /* Generic remote debugging interface for simulators.
2 Copyright 1993, 1994 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 "remote-sim.h"
36 #include "remote-utils.h"
37 #include "callback.h"
38
39 /* Prototypes */
40
41 static void dump_mem PARAMS ((char *buf, int len));
42
43 static void gdbsim_fetch_register PARAMS ((int regno));
44
45 static void gdbsim_store_register PARAMS ((int regno));
46
47 static void gdbsim_kill PARAMS ((void));
48
49 static void gdbsim_load PARAMS ((char *prog, int fromtty));
50
51 static void gdbsim_create_inferior PARAMS ((char *exec_file, char *args, char **env));
52
53 static void gdbsim_open PARAMS ((char *args, int from_tty));
54
55 static void gdbsim_close PARAMS ((int quitting));
56
57 static void gdbsim_detach PARAMS ((char *args, int from_tty));
58
59 static void gdbsim_resume PARAMS ((int pid, int step, enum target_signal siggnal));
60
61 static int gdbsim_wait PARAMS ((int pid, struct target_waitstatus *status));
62
63 static void gdbsim_prepare_to_store PARAMS ((void));
64
65 static int gdbsim_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
66 char *myaddr, int len,
67 int write,
68 struct target_ops *target));
69
70 static void gdbsim_files_info PARAMS ((struct target_ops *target));
71
72 static void gdbsim_mourn_inferior PARAMS ((void));
73
74 static void simulator_command PARAMS ((char *args, int from_tty));
75
76 /* Naming convention:
77
78 sim_* are the interface to the simulator (see remote-sim.h).
79 sim_callback_* are the stuff which the simulator can see inside GDB.
80 gdbsim_* are stuff which is internal to gdb. */
81
82 /* Forward data declarations */
83 extern struct target_ops gdbsim_ops;
84
85 static int program_loaded = 0;
86
87 static void
88 dump_mem (buf, len)
89 char *buf;
90 int len;
91 {
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
112 static void
113 gdbsim_fetch_register (regno)
114 int 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 }
134 }
135
136
137 static void
138 gdbsim_store_register (regno)
139 int regno;
140 {
141 if (regno == -1)
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. */
149 char tmp[MAX_REGISTER_RAW_SIZE];
150 read_register_gen (regno, tmp);
151 sim_store_register (regno, tmp);
152 if (sr_get_debug ())
153 {
154 printf_filtered ("gdbsim_store_register: %d", regno);
155 /* FIXME: We could print something more intelligible. */
156 dump_mem (tmp, REGISTER_RAW_SIZE (regno));
157 }
158 }
159 }
160
161 /* Kill the running program. This may involve closing any open files
162 and releasing other resources acquired by the simulated program. */
163
164 static void
165 gdbsim_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. */
177
178 static void
179 gdbsim_load (prog, fromtty)
180 char *prog;
181 int fromtty;
182 {
183 if (sr_get_debug ())
184 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
185
186 inferior_pid = 0;
187
188 /* This must be done before calling gr_load_image. */
189 program_loaded = 1;
190
191 if (sim_load (prog, fromtty) != 0)
192 generic_load (prog, fromtty);
193 }
194
195
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. */
201 /* This is called not only when we first attach, but also when the
202 user types "run" after having attached. */
203
204 static void
205 gdbsim_create_inferior (exec_file, args, env)
206 char *exec_file;
207 char *args;
208 char **env;
209 {
210 int len;
211 char *arg_buf,**argv;
212 CORE_ADDR entry_pt;
213
214 if (! program_loaded)
215 error ("No program loaded.");
216
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.");
223
224 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
225
226 gdbsim_kill ();
227 remove_breakpoints ();
228 init_wait_for_inferior ();
229
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);
238 sim_create_inferior (entry_pt, argv, env);
239
240 inferior_pid = 42;
241 insert_breakpoints (); /* Needed to get correct instruction in cache */
242 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
243 }
244
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. */
249
250 static void
251 gdbsim_open (args, from_tty)
252 char *args;
253 int from_tty;
254 {
255 if (sr_get_debug ())
256 printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
257
258 sim_set_callbacks (&default_callback);
259 default_callback.init (&default_callback);
260
261 sim_open (args);
262
263 push_target (&gdbsim_ops);
264 target_fetch_registers (-1);
265 printf_filtered ("Connected to the simulator.\n");
266 }
267
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. */
275 /* Close out all files and local state before this target loses control. */
276
277 static void
278 gdbsim_close (quitting)
279 int quitting;
280 {
281 if (sr_get_debug ())
282 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
283
284 program_loaded = 0;
285
286 sim_close (quitting);
287 }
288
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. */
295 /* Terminate the open connection to the remote debugger.
296 Use this when you want to detach and do something else with your gdb. */
297
298 static void
299 gdbsim_detach (args,from_tty)
300 char *args;
301 int from_tty;
302 {
303 if (sr_get_debug ())
304 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
305
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);
309 }
310
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
315 static void
316 gdbsim_resume (pid, step, siggnal)
317 int pid, step;
318 enum target_signal siggnal;
319 {
320 if (inferior_pid != 42)
321 error ("The program is not being run.");
322
323 if (sr_get_debug ())
324 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
325
326 sim_resume (step, target_signal_to_host (siggnal));
327 }
328
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. */
332
333 static int
334 gdbsim_wait (pid, status)
335 int pid;
336 struct target_waitstatus *status;
337 {
338 int sigrc;
339 enum sim_stop reason;
340
341 if (sr_get_debug ())
342 printf_filtered ("gdbsim_wait\n");
343
344 sim_stop_reason (&reason, &sigrc);
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
365 return inferior_pid;
366 }
367
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
374 static void
375 gdbsim_prepare_to_store ()
376 {
377 /* Do nothing, since we can store individual regs */
378 }
379
380 static int
381 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
382 CORE_ADDR memaddr;
383 char *myaddr;
384 int len;
385 int write;
386 struct target_ops *target; /* ignored */
387 {
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
399 if (write)
400 {
401 len = sim_write (memaddr, myaddr, len);
402 }
403 else
404 {
405 len = sim_read (memaddr, myaddr, len);
406 if (sr_get_debug () && len > 0)
407 dump_mem(myaddr, len);
408 }
409 return len;
410 }
411
412 static void
413 gdbsim_files_info (target)
414 struct target_ops *target;
415 {
416 char *file = "nothing";
417
418 if (exec_bfd)
419 file = bfd_get_filename (exec_bfd);
420
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);
428 sim_info (0);
429 }
430 }
431
432 /* Clear the simulator's notion of what the break points are. */
433
434 static void
435 gdbsim_mourn_inferior ()
436 {
437 if (sr_get_debug ())
438 printf_filtered ("gdbsim_mourn_inferior:\n");
439
440 remove_breakpoints ();
441 generic_mourn_inferior ();
442 }
443
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. */
447
448 static void
449 simulator_command (args, from_tty)
450 char *args;
451 int from_tty;
452 {
453 sim_do_command (args);
454 }
455
456 /* Define the target subroutine names */
457
458 struct 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 */
487 0, /* to_thread_alive */
488 0, /* to_stop */
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 */
499 };
500
501 void
502 _initialize_remote_sim ()
503 {
504 add_target (&gdbsim_ops);
505
506 add_com ("sim <command>", class_obscure, simulator_command,
507 "Send a command to the simulator.");
508 }
This page took 0.053361 seconds and 4 git commands to generate.