Commit | Line | Data |
---|---|---|
40b92220 | 1 | /* Generic remote debugging interface for simulators. |
47424e79 | 2 | Copyright 1993, 1994 Free Software Foundation, Inc. |
40b92220 | 3 | Contributed by Cygnus Support. |
47424e79 | 4 | Steve Chamberlain (sac@cygnus.com). |
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 | |
6c9638b4 | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
ec25d19b SC |
21 | |
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
24 | #include "wait.h" | |
25 | #include "value.h" | |
2b576293 | 26 | #include "gdb_string.h" |
ec25d19b SC |
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). | |
cc274a2e | 41 | sim_callback_* are the stuff which the simulator can see inside GDB. |
40b92220 | 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 | ||
6b009ef6 | 98 | |
a944e79a | 99 | static void |
40b92220 | 100 | gdbsim_store_register (regno) |
ec25d19b SC |
101 | int regno; |
102 | { | |
103 | if (regno == -1) | |
40b92220 JK |
104 | { |
105 | for (regno = 0; regno < NUM_REGS; regno++) | |
106 | gdbsim_store_register (regno); | |
107 | } | |
108 | else | |
109 | { | |
110 | /* FIXME: Until read_register() returns LONGEST, we have this. */ | |
3beff94e SC |
111 | char tmp[MAX_REGISTER_RAW_SIZE]; |
112 | read_register_gen (regno, tmp); | |
113 | sim_store_register (regno, tmp); | |
40b92220 JK |
114 | if (sr_get_debug ()) |
115 | { | |
116 | printf_filtered ("gdbsim_store_register: %d", regno); | |
117 | /* FIXME: We could print something more intelligible. */ | |
3beff94e | 118 | dump_mem (tmp, REGISTER_RAW_SIZE (regno)); |
40b92220 JK |
119 | } |
120 | } | |
ec25d19b SC |
121 | } |
122 | ||
47424e79 DE |
123 | /* Kill the running program. This may involve closing any open files |
124 | and releasing other resources acquired by the simulated program. */ | |
125 | ||
40b92220 JK |
126 | static void |
127 | gdbsim_kill () | |
128 | { | |
129 | if (sr_get_debug ()) | |
130 | printf_filtered ("gdbsim_kill\n"); | |
131 | ||
132 | sim_kill (); /* close fd's, remove mappings */ | |
133 | inferior_pid = 0; | |
134 | } | |
135 | ||
136 | /* Load an executable file into the target process. This is expected to | |
137 | not only bring new code into the target process, but also to update | |
138 | GDB's symbol tables to match. */ | |
ec25d19b | 139 | |
ec25d19b | 140 | static void |
40b92220 JK |
141 | gdbsim_load (prog, fromtty) |
142 | char *prog; | |
143 | int fromtty; | |
ec25d19b | 144 | { |
40b92220 JK |
145 | if (sr_get_debug ()) |
146 | printf_filtered ("gdbsim_load: prog \"%s\"\n", prog); | |
ec25d19b | 147 | |
47424e79 DE |
148 | inferior_pid = 0; |
149 | ||
150 | /* This must be done before calling gr_load_image. */ | |
40b92220 | 151 | program_loaded = 1; |
47424e79 DE |
152 | |
153 | if (sim_load (prog, fromtty) != 0) | |
dedcc91d | 154 | generic_load (prog, fromtty); |
40b92220 JK |
155 | } |
156 | ||
ec25d19b | 157 | |
40b92220 JK |
158 | /* Start an inferior process and set inferior_pid to its pid. |
159 | EXEC_FILE is the file to run. | |
160 | ALLARGS is a string containing the arguments to the program. | |
161 | ENV is the environment vector to pass. Errors reported with error(). | |
162 | On VxWorks and various standalone systems, we ignore exec_file. */ | |
ec25d19b SC |
163 | /* This is called not only when we first attach, but also when the |
164 | user types "run" after having attached. */ | |
40b92220 JK |
165 | |
166 | static void | |
167 | gdbsim_create_inferior (exec_file, args, env) | |
168 | char *exec_file; | |
ec25d19b SC |
169 | char *args; |
170 | char **env; | |
171 | { | |
47424e79 | 172 | int len; |
40b92220 | 173 | char *arg_buf,**argv; |
47424e79 | 174 | CORE_ADDR entry_pt; |
ec25d19b | 175 | |
40b92220 JK |
176 | if (! program_loaded) |
177 | error ("No program loaded."); | |
ec25d19b | 178 | |
40b92220 JK |
179 | if (sr_get_debug ()) |
180 | printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", | |
181 | exec_file, args); | |
182 | ||
183 | if (exec_file == 0 || exec_bfd == 0) | |
184 | error ("No exec file specified."); | |
ec25d19b | 185 | |
47424e79 | 186 | entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd); |
40b92220 JK |
187 | |
188 | gdbsim_kill (NULL, NULL); | |
189 | remove_breakpoints (); | |
ec25d19b | 190 | init_wait_for_inferior (); |
ec25d19b | 191 | |
40b92220 JK |
192 | len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10; |
193 | arg_buf = (char *) alloca (len); | |
194 | arg_buf[0] = '\0'; | |
195 | strcat (arg_buf, exec_file); | |
196 | strcat (arg_buf, " "); | |
197 | strcat (arg_buf, args); | |
198 | argv = buildargv (arg_buf); | |
199 | make_cleanup (freeargv, (char *) argv); | |
47424e79 | 200 | sim_create_inferior (entry_pt, argv, env); |
40b92220 JK |
201 | |
202 | inferior_pid = 42; | |
203 | insert_breakpoints (); /* Needed to get correct instruction in cache */ | |
45dc9be3 | 204 | proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0); |
40b92220 | 205 | } |
ec25d19b | 206 | |
40b92220 JK |
207 | /* The open routine takes the rest of the parameters from the command, |
208 | and (if successful) pushes a new target onto the stack. | |
209 | Targets should supply this routine, if only to provide an error message. */ | |
210 | /* Called when selecting the simulator. EG: (gdb) target sim name. */ | |
ec25d19b | 211 | |
a944e79a | 212 | static void |
40b92220 JK |
213 | gdbsim_open (args, from_tty) |
214 | char *args; | |
ec25d19b SC |
215 | int from_tty; |
216 | { | |
40b92220 | 217 | if (sr_get_debug ()) |
47424e79 | 218 | printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)"); |
47424e79 | 219 | sim_open (args); |
40b92220 JK |
220 | push_target (&gdbsim_ops); |
221 | target_fetch_registers (-1); | |
40b92220 | 222 | printf_filtered ("Connected to the simulator.\n"); |
ec25d19b SC |
223 | } |
224 | ||
40b92220 JK |
225 | /* Does whatever cleanup is required for a target that we are no longer |
226 | going to be calling. Argument says whether we are quitting gdb and | |
227 | should not get hung in case of errors, or whether we want a clean | |
228 | termination even if it takes a while. This routine is automatically | |
229 | always called just before a routine is popped off the target stack. | |
230 | Closing file descriptors and freeing memory are typical things it should | |
231 | do. */ | |
ec25d19b SC |
232 | /* Close out all files and local state before this target loses control. */ |
233 | ||
a944e79a | 234 | static void |
40b92220 | 235 | gdbsim_close (quitting) |
ec25d19b SC |
236 | int quitting; |
237 | { | |
40b92220 JK |
238 | if (sr_get_debug ()) |
239 | printf_filtered ("gdbsim_close: quitting %d\n", quitting); | |
240 | ||
241 | program_loaded = 0; | |
242 | ||
47424e79 | 243 | sim_close (quitting); |
ec25d19b SC |
244 | } |
245 | ||
40b92220 JK |
246 | /* Takes a program previously attached to and detaches it. |
247 | The program may resume execution (some targets do, some don't) and will | |
248 | no longer stop on signals, etc. We better not have left any breakpoints | |
249 | in the program or it'll die when it hits one. ARGS is arguments | |
250 | typed by the user (e.g. a signal to send the process). FROM_TTY | |
251 | says whether to be verbose or not. */ | |
ec25d19b | 252 | /* Terminate the open connection to the remote debugger. |
40b92220 JK |
253 | Use this when you want to detach and do something else with your gdb. */ |
254 | ||
255 | static void | |
256 | gdbsim_detach (args,from_tty) | |
ec25d19b SC |
257 | char *args; |
258 | int from_tty; | |
259 | { | |
40b92220 JK |
260 | if (sr_get_debug ()) |
261 | printf_filtered ("gdbsim_detach: args \"%s\"\n", args); | |
a944e79a | 262 | |
40b92220 JK |
263 | pop_target (); /* calls gdbsim_close to do the real work */ |
264 | if (from_tty) | |
265 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); | |
ec25d19b SC |
266 | } |
267 | ||
40b92220 JK |
268 | /* Resume execution of the target process. STEP says whether to single-step |
269 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given | |
270 | to the target, or zero for no signal. */ | |
271 | ||
272 | static void | |
273 | gdbsim_resume (pid, step, siggnal) | |
67ac9759 JK |
274 | int pid, step; |
275 | enum target_signal siggnal; | |
40b92220 JK |
276 | { |
277 | if (sr_get_debug ()) | |
278 | printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); | |
ec25d19b | 279 | |
67ac9759 | 280 | sim_resume (step, target_signal_to_host (siggnal)); |
40b92220 | 281 | } |
ec25d19b | 282 | |
40b92220 JK |
283 | /* Wait for inferior process to do something. Return pid of child, |
284 | or -1 in case of error; store status through argument pointer STATUS, | |
285 | just as `wait' would. */ | |
ec25d19b | 286 | |
40b92220 | 287 | static int |
de43d7d0 SG |
288 | gdbsim_wait (pid, status) |
289 | int pid; | |
67ac9759 | 290 | struct target_waitstatus *status; |
ec25d19b | 291 | { |
592f517a | 292 | int sigrc; |
c7efaa16 | 293 | enum sim_stop reason; |
592f517a | 294 | |
40b92220 | 295 | if (sr_get_debug ()) |
67ac9759 JK |
296 | printf_filtered ("gdbsim_wait\n"); |
297 | ||
c7efaa16 | 298 | sim_stop_reason (&reason, &sigrc); |
67ac9759 JK |
299 | switch (reason) |
300 | { | |
301 | case sim_exited: | |
302 | status->kind = TARGET_WAITKIND_EXITED; | |
303 | status->value.integer = sigrc; | |
304 | break; | |
305 | case sim_stopped: | |
306 | status->kind = TARGET_WAITKIND_STOPPED; | |
307 | /* The signal in sigrc is a host signal. That probably | |
308 | should be fixed. */ | |
309 | status->value.sig = target_signal_from_host (sigrc); | |
310 | break; | |
311 | case sim_signalled: | |
312 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
313 | /* The signal in sigrc is a host signal. That probably | |
314 | should be fixed. */ | |
315 | status->value.sig = target_signal_from_host (sigrc); | |
316 | break; | |
317 | } | |
318 | ||
3f0184ac | 319 | return inferior_pid; |
ec25d19b SC |
320 | } |
321 | ||
40b92220 JK |
322 | /* Get ready to modify the registers array. On machines which store |
323 | individual registers, this doesn't need to do anything. On machines | |
324 | which store all the registers in one fell swoop, this makes sure | |
325 | that registers contains all the registers from the program being | |
326 | debugged. */ | |
327 | ||
ec25d19b | 328 | static void |
40b92220 | 329 | gdbsim_prepare_to_store () |
ec25d19b | 330 | { |
40b92220 | 331 | /* Do nothing, since we can store individual regs */ |
ec25d19b SC |
332 | } |
333 | ||
40b92220 JK |
334 | static int |
335 | gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target) | |
ec25d19b SC |
336 | CORE_ADDR memaddr; |
337 | char *myaddr; | |
338 | int len; | |
339 | int write; | |
340 | struct target_ops *target; /* ignored */ | |
341 | { | |
40b92220 JK |
342 | if (! program_loaded) |
343 | error ("No program loaded."); | |
344 | ||
345 | if (sr_get_debug ()) | |
346 | { | |
347 | printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n", | |
348 | myaddr, memaddr, len, write); | |
349 | if (sr_get_debug () && write) | |
350 | dump_mem(myaddr, len); | |
351 | } | |
352 | ||
ec25d19b | 353 | if (write) |
40b92220 JK |
354 | { |
355 | len = sim_write (memaddr, myaddr, len); | |
356 | } | |
ec25d19b | 357 | else |
40b92220 JK |
358 | { |
359 | len = sim_read (memaddr, myaddr, len); | |
360 | if (sr_get_debug () && len > 0) | |
361 | dump_mem(myaddr, len); | |
362 | } | |
ec25d19b SC |
363 | return len; |
364 | } | |
365 | ||
40b92220 JK |
366 | static void |
367 | gdbsim_files_info (target) | |
368 | struct target_ops *target; | |
369 | { | |
370 | char *file = "nothing"; | |
ec25d19b | 371 | |
40b92220 JK |
372 | if (exec_bfd) |
373 | file = bfd_get_filename (exec_bfd); | |
ec25d19b | 374 | |
40b92220 JK |
375 | if (sr_get_debug ()) |
376 | printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); | |
377 | ||
378 | if (exec_bfd) | |
379 | { | |
380 | printf_filtered ("\tAttached to %s running program %s\n", | |
381 | target_shortname, file); | |
47424e79 | 382 | sim_info (0); |
40b92220 | 383 | } |
ec25d19b SC |
384 | } |
385 | ||
fb506180 | 386 | /* Clear the simulator's notion of what the break points are. */ |
ec25d19b | 387 | |
40b92220 JK |
388 | static void |
389 | gdbsim_mourn_inferior () | |
390 | { | |
391 | if (sr_get_debug ()) | |
392 | printf_filtered ("gdbsim_mourn_inferior:\n"); | |
ec25d19b | 393 | |
40b92220 JK |
394 | remove_breakpoints (); |
395 | generic_mourn_inferior (); | |
ec25d19b | 396 | } |
40b92220 | 397 | |
fb506180 SS |
398 | /* Put a command string, in args, out to MONITOR. Output from MONITOR |
399 | is placed on the users terminal until the prompt is seen. FIXME: We | |
400 | read the characters ourseleves here cause of a nasty echo. */ | |
ec25d19b | 401 | |
fb506180 SS |
402 | static void |
403 | simulator_command (args, from_tty) | |
404 | char *args; | |
405 | int from_tty; | |
ec25d19b | 406 | { |
fb506180 SS |
407 | sim_do_command (args); |
408 | } | |
409 | ||
410 | /* Define the target subroutine names */ | |
411 | ||
412 | struct target_ops gdbsim_ops = { | |
413 | "sim", /* to_shortname */ | |
414 | "simulator", /* to_longname */ | |
415 | "Use the compiled-in simulator.", /* to_doc */ | |
416 | gdbsim_open, /* to_open */ | |
417 | gdbsim_close, /* to_close */ | |
418 | NULL, /* to_attach */ | |
419 | gdbsim_detach, /* to_detach */ | |
420 | gdbsim_resume, /* to_resume */ | |
421 | gdbsim_wait, /* to_wait */ | |
422 | gdbsim_fetch_register, /* to_fetch_registers */ | |
423 | gdbsim_store_register, /* to_store_registers */ | |
424 | gdbsim_prepare_to_store, /* to_prepare_to_store */ | |
425 | gdbsim_xfer_inferior_memory, /* to_xfer_memory */ | |
426 | gdbsim_files_info, /* to_files_info */ | |
427 | memory_insert_breakpoint, /* to_insert_breakpoint */ | |
428 | memory_remove_breakpoint, /* to_remove_breakpoint */ | |
429 | NULL, /* to_terminal_init */ | |
430 | NULL, /* to_terminal_inferior */ | |
431 | NULL, /* to_terminal_ours_for_output */ | |
432 | NULL, /* to_terminal_ours */ | |
433 | NULL, /* to_terminal_info */ | |
434 | gdbsim_kill, /* to_kill */ | |
435 | gdbsim_load, /* to_load */ | |
436 | NULL, /* to_lookup_symbol */ | |
437 | gdbsim_create_inferior, /* to_create_inferior */ | |
438 | gdbsim_mourn_inferior, /* to_mourn_inferior */ | |
439 | 0, /* to_can_run */ | |
440 | 0, /* to_notice_signals */ | |
43fc25c8 | 441 | 0, /* to_thread_alive */ |
78b459a7 | 442 | 0, /* to_stop */ |
fb506180 SS |
443 | process_stratum, /* to_stratum */ |
444 | NULL, /* to_next */ | |
445 | 1, /* to_has_all_memory */ | |
446 | 1, /* to_has_memory */ | |
447 | 1, /* to_has_stack */ | |
448 | 1, /* to_has_registers */ | |
449 | 1, /* to_has_execution */ | |
450 | NULL, /* sections */ | |
451 | NULL, /* sections_end */ | |
452 | OPS_MAGIC, /* to_magic */ | |
ec25d19b SC |
453 | }; |
454 | ||
ec25d19b SC |
455 | void |
456 | _initialize_remote_sim () | |
457 | { | |
40b92220 | 458 | add_target (&gdbsim_ops); |
fb506180 SS |
459 | |
460 | add_com ("sim <command>", class_obscure, simulator_command, | |
461 | "Send a command to the simulator."); | |
ec25d19b | 462 | } |