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