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