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