]>
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 | |
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. */ | |
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 */ | |
45dc9be3 | 203 | proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0); |
40b92220 | 204 | } |
ec25d19b | 205 | |
40b92220 JK |
206 | /* The open routine takes the rest of the parameters from the command, |
207 | and (if successful) pushes a new target onto the stack. | |
208 | Targets should supply this routine, if only to provide an error message. */ | |
209 | /* Called when selecting the simulator. EG: (gdb) target sim name. */ | |
ec25d19b | 210 | |
a944e79a | 211 | static void |
40b92220 JK |
212 | gdbsim_open (args, from_tty) |
213 | char *args; | |
ec25d19b SC |
214 | int from_tty; |
215 | { | |
40b92220 | 216 | if (sr_get_debug ()) |
47424e79 | 217 | printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)"); |
40b92220 | 218 | |
47424e79 | 219 | sim_open (args); |
40b92220 JK |
220 | |
221 | push_target (&gdbsim_ops); | |
222 | target_fetch_registers (-1); | |
223 | ||
224 | printf_filtered ("Connected to the simulator.\n"); | |
ec25d19b SC |
225 | } |
226 | ||
40b92220 JK |
227 | /* Does whatever cleanup is required for a target that we are no longer |
228 | going to be calling. Argument says whether we are quitting gdb and | |
229 | should not get hung in case of errors, or whether we want a clean | |
230 | termination even if it takes a while. This routine is automatically | |
231 | always called just before a routine is popped off the target stack. | |
232 | Closing file descriptors and freeing memory are typical things it should | |
233 | do. */ | |
ec25d19b SC |
234 | /* Close out all files and local state before this target loses control. */ |
235 | ||
a944e79a | 236 | static void |
40b92220 | 237 | gdbsim_close (quitting) |
ec25d19b SC |
238 | int quitting; |
239 | { | |
40b92220 JK |
240 | if (sr_get_debug ()) |
241 | printf_filtered ("gdbsim_close: quitting %d\n", quitting); | |
242 | ||
243 | program_loaded = 0; | |
244 | ||
47424e79 | 245 | sim_close (quitting); |
ec25d19b SC |
246 | } |
247 | ||
40b92220 JK |
248 | /* Takes a program previously attached to and detaches it. |
249 | The program may resume execution (some targets do, some don't) and will | |
250 | no longer stop on signals, etc. We better not have left any breakpoints | |
251 | in the program or it'll die when it hits one. ARGS is arguments | |
252 | typed by the user (e.g. a signal to send the process). FROM_TTY | |
253 | says whether to be verbose or not. */ | |
ec25d19b | 254 | /* Terminate the open connection to the remote debugger. |
40b92220 JK |
255 | Use this when you want to detach and do something else with your gdb. */ |
256 | ||
257 | static void | |
258 | gdbsim_detach (args,from_tty) | |
ec25d19b SC |
259 | char *args; |
260 | int from_tty; | |
261 | { | |
40b92220 JK |
262 | if (sr_get_debug ()) |
263 | printf_filtered ("gdbsim_detach: args \"%s\"\n", args); | |
a944e79a | 264 | |
40b92220 JK |
265 | pop_target (); /* calls gdbsim_close to do the real work */ |
266 | if (from_tty) | |
267 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); | |
ec25d19b SC |
268 | } |
269 | ||
40b92220 JK |
270 | /* Resume execution of the target process. STEP says whether to single-step |
271 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given | |
272 | to the target, or zero for no signal. */ | |
273 | ||
274 | static void | |
275 | gdbsim_resume (pid, step, siggnal) | |
67ac9759 JK |
276 | int pid, step; |
277 | enum target_signal siggnal; | |
40b92220 JK |
278 | { |
279 | if (sr_get_debug ()) | |
280 | printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); | |
ec25d19b | 281 | |
67ac9759 | 282 | sim_resume (step, target_signal_to_host (siggnal)); |
40b92220 | 283 | } |
ec25d19b | 284 | |
40b92220 JK |
285 | /* Wait for inferior process to do something. Return pid of child, |
286 | or -1 in case of error; store status through argument pointer STATUS, | |
287 | just as `wait' would. */ | |
ec25d19b | 288 | |
40b92220 | 289 | static int |
de43d7d0 SG |
290 | gdbsim_wait (pid, status) |
291 | int pid; | |
67ac9759 | 292 | struct target_waitstatus *status; |
ec25d19b | 293 | { |
592f517a | 294 | int sigrc; |
c7efaa16 | 295 | enum sim_stop reason; |
592f517a | 296 | |
40b92220 | 297 | if (sr_get_debug ()) |
67ac9759 JK |
298 | printf_filtered ("gdbsim_wait\n"); |
299 | ||
c7efaa16 | 300 | sim_stop_reason (&reason, &sigrc); |
67ac9759 JK |
301 | switch (reason) |
302 | { | |
303 | case sim_exited: | |
304 | status->kind = TARGET_WAITKIND_EXITED; | |
305 | status->value.integer = sigrc; | |
306 | break; | |
307 | case sim_stopped: | |
308 | status->kind = TARGET_WAITKIND_STOPPED; | |
309 | /* The signal in sigrc is a host signal. That probably | |
310 | should be fixed. */ | |
311 | status->value.sig = target_signal_from_host (sigrc); | |
312 | break; | |
313 | case sim_signalled: | |
314 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
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 | } | |
320 | ||
3f0184ac | 321 | return inferior_pid; |
ec25d19b SC |
322 | } |
323 | ||
40b92220 JK |
324 | /* Get ready to modify the registers array. On machines which store |
325 | individual registers, this doesn't need to do anything. On machines | |
326 | which store all the registers in one fell swoop, this makes sure | |
327 | that registers contains all the registers from the program being | |
328 | debugged. */ | |
329 | ||
ec25d19b | 330 | static void |
40b92220 | 331 | gdbsim_prepare_to_store () |
ec25d19b | 332 | { |
40b92220 | 333 | /* Do nothing, since we can store individual regs */ |
ec25d19b SC |
334 | } |
335 | ||
40b92220 JK |
336 | static int |
337 | gdbsim_xfer_inferior_memory (memaddr, myaddr, len, write, target) | |
ec25d19b SC |
338 | CORE_ADDR memaddr; |
339 | char *myaddr; | |
340 | int len; | |
341 | int write; | |
342 | struct target_ops *target; /* ignored */ | |
343 | { | |
40b92220 JK |
344 | if (! program_loaded) |
345 | error ("No program loaded."); | |
346 | ||
347 | if (sr_get_debug ()) | |
348 | { | |
349 | printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x%x, memaddr 0x%x, len %d, write %d\n", | |
350 | myaddr, memaddr, len, write); | |
351 | if (sr_get_debug () && write) | |
352 | dump_mem(myaddr, len); | |
353 | } | |
354 | ||
ec25d19b | 355 | if (write) |
40b92220 JK |
356 | { |
357 | len = sim_write (memaddr, myaddr, len); | |
358 | } | |
ec25d19b | 359 | else |
40b92220 JK |
360 | { |
361 | len = sim_read (memaddr, myaddr, len); | |
362 | if (sr_get_debug () && len > 0) | |
363 | dump_mem(myaddr, len); | |
364 | } | |
ec25d19b SC |
365 | return len; |
366 | } | |
367 | ||
40b92220 JK |
368 | static void |
369 | gdbsim_files_info (target) | |
370 | struct target_ops *target; | |
371 | { | |
372 | char *file = "nothing"; | |
ec25d19b | 373 | |
40b92220 JK |
374 | if (exec_bfd) |
375 | file = bfd_get_filename (exec_bfd); | |
ec25d19b | 376 | |
40b92220 JK |
377 | if (sr_get_debug ()) |
378 | printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); | |
379 | ||
380 | if (exec_bfd) | |
381 | { | |
382 | printf_filtered ("\tAttached to %s running program %s\n", | |
383 | target_shortname, file); | |
47424e79 | 384 | sim_info (0); |
40b92220 | 385 | } |
ec25d19b SC |
386 | } |
387 | ||
40b92220 | 388 | /* Clear the sims notion of what the break points are. */ |
ec25d19b | 389 | |
40b92220 JK |
390 | static void |
391 | gdbsim_mourn_inferior () | |
392 | { | |
393 | if (sr_get_debug ()) | |
394 | printf_filtered ("gdbsim_mourn_inferior:\n"); | |
ec25d19b | 395 | |
40b92220 JK |
396 | remove_breakpoints (); |
397 | generic_mourn_inferior (); | |
ec25d19b | 398 | } |
40b92220 | 399 | |
ec25d19b SC |
400 | /* Define the target subroutine names */ |
401 | ||
40b92220 | 402 | struct target_ops gdbsim_ops = |
ec25d19b SC |
403 | { |
404 | "sim", "simulator", | |
405 | "Use the simulator", | |
40b92220 JK |
406 | gdbsim_open, gdbsim_close, |
407 | 0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */ | |
408 | gdbsim_fetch_register, gdbsim_store_register, | |
409 | gdbsim_prepare_to_store, | |
410 | gdbsim_xfer_inferior_memory, | |
411 | gdbsim_files_info, | |
1311f8d1 DE |
412 | memory_insert_breakpoint, |
413 | memory_remove_breakpoint, | |
ec25d19b | 414 | 0, 0, 0, 0, 0, /* Terminal handling */ |
40b92220 | 415 | gdbsim_kill, /* kill */ |
47424e79 | 416 | gdbsim_load, /* load */ |
ec25d19b | 417 | 0, /* lookup_symbol */ |
47424e79 DE |
418 | gdbsim_create_inferior, /* create_inferior */ |
419 | gdbsim_mourn_inferior, /* mourn_inferior */ | |
ec25d19b SC |
420 | 0, /* can_run */ |
421 | 0, /* notice_signals */ | |
422 | process_stratum, 0, /* next */ | |
423 | 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */ | |
40b92220 | 424 | 0, 0, /* Section pointers */ |
ec25d19b SC |
425 | OPS_MAGIC, /* Always the last thing */ |
426 | }; | |
427 | ||
ec25d19b SC |
428 | void |
429 | _initialize_remote_sim () | |
430 | { | |
40b92220 | 431 | add_target (&gdbsim_ops); |
ec25d19b | 432 | } |