fb1e57e6f27e58e9599b373809afaa18bc170b59
[binutils-gdb.git] / gdb / remote-sim.c
1 /* Generic remote debugging interface for simulators.
2 Copyright 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Steve Chamberlain (sac@cygnus.com) and Doug Evans (dje@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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "wait.h"
25 #include "value.h"
26 #include <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
37 /* Naming convention:
38
39 sim_* are the interface to the simulator (see remote-sim.h).
40
41 gdbsim_* are stuff which is internal to gdb. */
42
43 /* Forward data declarations */
44 extern struct target_ops gdbsim_ops;
45
46 static int program_loaded = 0;
47
48 static void
49 dump_mem (buf, len)
50 char *buf;
51 int len;
52 {
53 if (len <= 8)
54 {
55 if (len == 8 || len == 4)
56 {
57 long l[2];
58 memcpy (l, buf, len);
59 printf_filtered ("\t0x%x", l[0]);
60 printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]);
61 }
62 else
63 {
64 int i;
65 printf_filtered ("\t");
66 for (i = 0; i < len; i++)
67 printf_filtered ("0x%x ", buf[i]);
68 printf_filtered ("\n");
69 }
70 }
71 }
72
73 static void
74 gdbsim_fetch_register (regno)
75 int regno;
76 {
77 if (regno == -1)
78 {
79 for (regno = 0; regno < NUM_REGS; regno++)
80 gdbsim_fetch_register (regno);
81 }
82 else
83 {
84 char buf[MAX_REGISTER_RAW_SIZE];
85
86 sim_fetch_register (regno, buf);
87 supply_register (regno, buf);
88 if (sr_get_debug ())
89 {
90 printf_filtered ("gdbsim_fetch_register: %d", regno);
91 /* FIXME: We could print something more intelligible. */
92 dump_mem (buf, REGISTER_RAW_SIZE (regno));
93 }
94 }
95 }
96
97 static void
98 gdbsim_store_register (regno)
99 int regno;
100 {
101 if (regno == -1)
102 {
103 for (regno = 0; regno < NUM_REGS; regno++)
104 gdbsim_store_register (regno);
105 }
106 else
107 {
108 /* FIXME: Until read_register() returns LONGEST, we have this. */
109 char value[MAX_REGISTER_RAW_SIZE];
110 read_register_gen (regno, value);
111 sim_store_register (regno, value);
112 if (sr_get_debug ())
113 {
114 printf_filtered ("gdbsim_store_register: %d", regno);
115 /* FIXME: We could print something more intelligible. */
116 dump_mem (value, REGISTER_RAW_SIZE (regno));
117 }
118 }
119 }
120
121 static void
122 gdbsim_kill ()
123 {
124 if (sr_get_debug ())
125 printf_filtered ("gdbsim_kill\n");
126
127 sim_kill (); /* close fd's, remove mappings */
128 inferior_pid = 0;
129 }
130
131 /* Load an executable file into the target process. This is expected to
132 not only bring new code into the target process, but also to update
133 GDB's symbol tables to match. */
134
135 static void
136 gdbsim_load (prog, fromtty)
137 char *prog;
138 int fromtty;
139 {
140 bfd *abfd;
141
142 if (sr_get_debug ())
143 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
144
145 inferior_pid = 0;
146 program_loaded = 0;
147 abfd = bfd_openr (prog, gnutarget);
148
149 if (!abfd)
150 error ("Unable to open file %s.", prog);
151
152 if (bfd_check_format (abfd, bfd_object) == 0)
153 error ("File is not an object file.");
154
155 if (sim_load (abfd, prog) != 0)
156 return;
157
158 program_loaded = 1;
159
160 sim_set_pc (abfd->start_address);
161 }
162
163 /*
164 * This is a utility routine that sim_load() can call to do the work.
165 * The result is 0 for success, non-zero for failure.
166 *
167 * Eg: int sim_load (bfd *bfd, char *prog) { return sim_load_standard (bfd); }
168 */
169
170 sim_load_standard (abfd)
171 bfd *abfd;
172 {
173 asection *s;
174
175 s = abfd->sections;
176 while (s != (asection *)NULL)
177 {
178 if (s->flags & SEC_LOAD)
179 {
180 int i;
181 int delta = 4096;
182 char *buffer = xmalloc (delta);
183 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
184 s->name, s->vma, s->vma + s->_raw_size);
185 for (i = 0; i < s->_raw_size; i+= delta)
186 {
187 int sub_delta = delta;
188 if (sub_delta > s->_raw_size - i)
189 sub_delta = s->_raw_size - i ;
190
191 bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
192 sim_write (s->vma + i, buffer, sub_delta);
193 printf_filtered ("*");
194 fflush (stdout);
195 }
196 printf_filtered ("\n");
197 free (buffer);
198 }
199 s = s->next;
200 }
201
202 return 0;
203 }
204
205 /* Start an inferior process and set inferior_pid to its pid.
206 EXEC_FILE is the file to run.
207 ALLARGS is a string containing the arguments to the program.
208 ENV is the environment vector to pass. Errors reported with error().
209 On VxWorks and various standalone systems, we ignore exec_file. */
210 /* This is called not only when we first attach, but also when the
211 user types "run" after having attached. */
212
213 static void
214 gdbsim_create_inferior (exec_file, args, env)
215 char *exec_file;
216 char *args;
217 char **env;
218 {
219 int len,entry_pt;
220 char *arg_buf,**argv;
221
222 if (! program_loaded)
223 error ("No program loaded.");
224
225 if (sr_get_debug ())
226 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
227 exec_file, args);
228
229 if (exec_file == 0 || exec_bfd == 0)
230 error ("No exec file specified.");
231
232 entry_pt = (int) bfd_get_start_address (exec_bfd);
233
234 gdbsim_kill (NULL, NULL);
235 remove_breakpoints ();
236 init_wait_for_inferior ();
237
238 len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
239 arg_buf = (char *) alloca (len);
240 arg_buf[0] = '\0';
241 strcat (arg_buf, exec_file);
242 strcat (arg_buf, " ");
243 strcat (arg_buf, args);
244 argv = buildargv (arg_buf);
245 make_cleanup (freeargv, (char *) argv);
246 /* FIXME: remote-sim.h says targets that don't support this return
247 non-zero. Perhaps distinguish between "not supported" and other errors?
248 Or maybe that can be the only error. */
249 if (sim_set_args (argv, env) != 0)
250 return;
251
252 inferior_pid = 42;
253 insert_breakpoints (); /* Needed to get correct instruction in cache */
254 proceed (entry_pt, -1, 0);
255 }
256
257 /* The open routine takes the rest of the parameters from the command,
258 and (if successful) pushes a new target onto the stack.
259 Targets should supply this routine, if only to provide an error message. */
260 /* Called when selecting the simulator. EG: (gdb) target sim name. */
261
262 static void
263 gdbsim_open (args, from_tty)
264 char *args;
265 int from_tty;
266 {
267 if (sr_get_debug ())
268 printf_filtered ("gdbsim_open: args \"%s\"\n", args);
269
270 if (sim_open (args) != 0)
271 {
272 /* FIXME: This is totally bogus. sim_open should have a way to
273 tell us what the error was, so we can tell the user. */
274 error ("Unable to initialize simulator (insufficient memory?).");
275 return;
276 }
277
278 push_target (&gdbsim_ops);
279 target_fetch_registers (-1);
280
281 printf_filtered ("Connected to the simulator.\n");
282 }
283
284 /* Does whatever cleanup is required for a target that we are no longer
285 going to be calling. Argument says whether we are quitting gdb and
286 should not get hung in case of errors, or whether we want a clean
287 termination even if it takes a while. This routine is automatically
288 always called just before a routine is popped off the target stack.
289 Closing file descriptors and freeing memory are typical things it should
290 do. */
291 /* Close out all files and local state before this target loses control. */
292
293 static void
294 gdbsim_close (quitting)
295 int quitting;
296 {
297 if (sr_get_debug ())
298 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
299
300 program_loaded = 0;
301
302 /* FIXME: Need to call sim_close() to close all files and
303 delete all mappings. */
304 }
305
306 /* Takes a program previously attached to and detaches it.
307 The program may resume execution (some targets do, some don't) and will
308 no longer stop on signals, etc. We better not have left any breakpoints
309 in the program or it'll die when it hits one. ARGS is arguments
310 typed by the user (e.g. a signal to send the process). FROM_TTY
311 says whether to be verbose or not. */
312 /* Terminate the open connection to the remote debugger.
313 Use this when you want to detach and do something else with your gdb. */
314
315 static void
316 gdbsim_detach (args,from_tty)
317 char *args;
318 int from_tty;
319 {
320 if (sr_get_debug ())
321 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
322
323 pop_target (); /* calls gdbsim_close to do the real work */
324 if (from_tty)
325 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
326 }
327
328 /* Resume execution of the target process. STEP says whether to single-step
329 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
330 to the target, or zero for no signal. */
331
332 static void
333 gdbsim_resume (pid, step, siggnal)
334 int pid, step, siggnal;
335 {
336 if (sr_get_debug ())
337 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
338
339 sim_resume (step, siggnal);
340 }
341
342 /* Wait for inferior process to do something. Return pid of child,
343 or -1 in case of error; store status through argument pointer STATUS,
344 just as `wait' would. */
345
346 static int
347 gdbsim_wait (status)
348 WAITTYPE *status;
349 {
350 if (sr_get_debug ())
351 printf_filtered ("gdbsim_wait: ");
352 WSETSTOP (*status, sim_stop_signal ());
353 if (sr_get_debug ())
354 printf_filtered ("status %d\n", *status);
355 return inferior_pid;
356 }
357
358 /* Get ready to modify the registers array. On machines which store
359 individual registers, this doesn't need to do anything. On machines
360 which store all the registers in one fell swoop, this makes sure
361 that registers contains all the registers from the program being
362 debugged. */
363
364 static void
365 gdbsim_prepare_to_store ()
366 {
367 /* Do nothing, since we can store individual regs */
368 }
369
370 static int
371 gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target)
372 CORE_ADDR memaddr;
373 char *myaddr;
374 int len;
375 int write;
376 struct target_ops *target; /* ignored */
377 {
378 if (! program_loaded)
379 error ("No program loaded.");
380
381 if (sr_get_debug ())
382 {
383 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n",
384 myaddr, memaddr, len, write);
385 if (sr_get_debug () && write)
386 dump_mem(myaddr, len);
387 }
388
389 if (write)
390 {
391 len = sim_write (memaddr, myaddr, len);
392 }
393 else
394 {
395 len = sim_read (memaddr, myaddr, len);
396 if (sr_get_debug () && len > 0)
397 dump_mem(myaddr, len);
398 }
399 return len;
400 }
401
402 static void
403 gdbsim_files_info (target)
404 struct target_ops *target;
405 {
406 char *file = "nothing";
407
408 if (exec_bfd)
409 file = bfd_get_filename (exec_bfd);
410
411 if (sr_get_debug ())
412 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
413
414 if (exec_bfd)
415 {
416 printf_filtered ("\tAttached to %s running program %s\n",
417 target_shortname, file);
418 sim_info ();
419 }
420 }
421
422 /* Clear the sims notion of what the break points are. */
423
424 static void
425 gdbsim_mourn_inferior ()
426 {
427 if (sr_get_debug ())
428 printf_filtered ("gdbsim_mourn_inferior:\n");
429
430 remove_breakpoints ();
431 generic_mourn_inferior ();
432 }
433
434 /* Define the target subroutine names */
435
436 struct target_ops gdbsim_ops =
437 {
438 "sim", "simulator",
439 "Use the simulator",
440 gdbsim_open, gdbsim_close,
441 0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
442 gdbsim_fetch_register, gdbsim_store_register,
443 gdbsim_prepare_to_store,
444 gdbsim_xfer_inferior_memory,
445 gdbsim_files_info,
446 0, 0, /* Breakpoints */
447 0, 0, 0, 0, 0, /* Terminal handling */
448 gdbsim_kill, /* kill */
449 gdbsim_load,
450 0, /* lookup_symbol */
451 gdbsim_create_inferior, /* create_inferior */
452 gdbsim_mourn_inferior, /* mourn_inferior */
453 0, /* can_run */
454 0, /* notice_signals */
455 process_stratum, 0, /* next */
456 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
457 0, 0, /* Section pointers */
458 OPS_MAGIC, /* Always the last thing */
459 };
460
461 void
462 _initialize_remote_sim ()
463 {
464 add_target (&gdbsim_ops);
465 }