]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic remote debugging interface for simulators. |
29e57380 | 2 | Copyright 1993, 1994, 1996, 1997, 2000, 2001 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by Cygnus Support. |
4 | Steve Chamberlain (sac@cygnus.com). | |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "inferior.h" | |
03f2053f | 25 | #include "gdb_wait.h" |
c906108c SS |
26 | #include "value.h" |
27 | #include "gdb_string.h" | |
28 | #include <ctype.h> | |
29 | #include <fcntl.h> | |
30 | #include <signal.h> | |
31 | #include <setjmp.h> | |
32 | #include <errno.h> | |
33 | #include "terminal.h" | |
34 | #include "target.h" | |
35 | #include "gdbcore.h" | |
36 | #include "callback.h" | |
37 | #include "remote-sim.h" | |
38 | #include "remote-utils.h" | |
39 | #include "command.h" | |
40 | ||
41 | /* Prototypes */ | |
42 | ||
a14ed312 | 43 | extern void _initialize_remote_sim (void); |
392a587b | 44 | |
507f3c78 | 45 | extern int (*ui_loop_hook) (int signo); |
7a292a7a | 46 | |
a14ed312 | 47 | static void dump_mem (char *buf, int len); |
c906108c | 48 | |
a14ed312 | 49 | static void init_callbacks (void); |
c906108c | 50 | |
a14ed312 | 51 | static void end_callbacks (void); |
c906108c | 52 | |
a14ed312 | 53 | static int gdb_os_write_stdout (host_callback *, const char *, int); |
c906108c | 54 | |
a14ed312 | 55 | static void gdb_os_flush_stdout (host_callback *); |
c906108c | 56 | |
a14ed312 | 57 | static int gdb_os_write_stderr (host_callback *, const char *, int); |
c906108c | 58 | |
a14ed312 | 59 | static void gdb_os_flush_stderr (host_callback *); |
c906108c | 60 | |
a14ed312 | 61 | static int gdb_os_poll_quit (host_callback *); |
c906108c SS |
62 | |
63 | /* printf_filtered is depreciated */ | |
a14ed312 | 64 | static void gdb_os_printf_filtered (host_callback *, const char *, ...); |
c906108c | 65 | |
a14ed312 | 66 | static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 67 | |
a14ed312 | 68 | static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 69 | |
a14ed312 | 70 | static void gdb_os_error (host_callback *, const char *, ...); |
c906108c | 71 | |
a14ed312 | 72 | static void gdbsim_fetch_register (int regno); |
c906108c | 73 | |
a14ed312 | 74 | static void gdbsim_store_register (int regno); |
c906108c | 75 | |
a14ed312 | 76 | static void gdbsim_kill (void); |
c906108c | 77 | |
a14ed312 | 78 | static void gdbsim_load (char *prog, int fromtty); |
c906108c | 79 | |
a14ed312 | 80 | static void gdbsim_create_inferior (char *exec_file, char *args, char **env); |
c906108c | 81 | |
a14ed312 | 82 | static void gdbsim_open (char *args, int from_tty); |
c906108c | 83 | |
a14ed312 | 84 | static void gdbsim_close (int quitting); |
c906108c | 85 | |
a14ed312 | 86 | static void gdbsim_detach (char *args, int from_tty); |
c906108c | 87 | |
a14ed312 | 88 | static void gdbsim_resume (int pid, int step, enum target_signal siggnal); |
c906108c | 89 | |
a14ed312 | 90 | static int gdbsim_wait (int pid, struct target_waitstatus *status); |
c906108c | 91 | |
a14ed312 | 92 | static void gdbsim_prepare_to_store (void); |
c906108c | 93 | |
29e57380 C |
94 | static int gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, |
95 | int len, int write, | |
96 | struct mem_attrib *attrib, | |
97 | struct target_ops *target); | |
c906108c | 98 | |
a14ed312 | 99 | static void gdbsim_files_info (struct target_ops *target); |
c906108c | 100 | |
a14ed312 | 101 | static void gdbsim_mourn_inferior (void); |
c906108c | 102 | |
a14ed312 | 103 | static void gdbsim_stop (void); |
c906108c | 104 | |
a14ed312 | 105 | void simulator_command (char *args, int from_tty); |
c906108c SS |
106 | |
107 | /* Naming convention: | |
108 | ||
109 | sim_* are the interface to the simulator (see remote-sim.h). | |
110 | gdbsim_* are stuff which is internal to gdb. */ | |
111 | ||
112 | /* Forward data declarations */ | |
113 | extern struct target_ops gdbsim_ops; | |
114 | ||
115 | static int program_loaded = 0; | |
116 | ||
117 | /* We must keep track of whether the simulator has been opened or not because | |
118 | GDB can call a target's close routine twice, but sim_close doesn't allow | |
119 | this. We also need to record the result of sim_open so we can pass it | |
120 | back to the other sim_foo routines. */ | |
121 | static SIM_DESC gdbsim_desc = 0; | |
122 | ||
123 | static void | |
fba45db2 | 124 | dump_mem (char *buf, int len) |
c906108c SS |
125 | { |
126 | if (len <= 8) | |
127 | { | |
128 | if (len == 8 || len == 4) | |
129 | { | |
130 | long l[2]; | |
131 | memcpy (l, buf, len); | |
d4f3574e | 132 | printf_filtered ("\t0x%lx", l[0]); |
c906108c SS |
133 | printf_filtered (len == 8 ? " 0x%x\n" : "\n", l[1]); |
134 | } | |
135 | else | |
136 | { | |
137 | int i; | |
138 | printf_filtered ("\t"); | |
139 | for (i = 0; i < len; i++) | |
140 | printf_filtered ("0x%x ", buf[i]); | |
141 | printf_filtered ("\n"); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | static host_callback gdb_callback; | |
147 | static int callbacks_initialized = 0; | |
148 | ||
149 | /* Initialize gdb_callback. */ | |
150 | ||
151 | static void | |
fba45db2 | 152 | init_callbacks (void) |
c906108c | 153 | { |
c5aa993b | 154 | if (!callbacks_initialized) |
c906108c SS |
155 | { |
156 | gdb_callback = default_callback; | |
157 | gdb_callback.init (&gdb_callback); | |
158 | gdb_callback.write_stdout = gdb_os_write_stdout; | |
159 | gdb_callback.flush_stdout = gdb_os_flush_stdout; | |
160 | gdb_callback.write_stderr = gdb_os_write_stderr; | |
161 | gdb_callback.flush_stderr = gdb_os_flush_stderr; | |
162 | gdb_callback.printf_filtered = gdb_os_printf_filtered; | |
163 | gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered; | |
164 | gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered; | |
165 | gdb_callback.error = gdb_os_error; | |
166 | gdb_callback.poll_quit = gdb_os_poll_quit; | |
167 | gdb_callback.magic = HOST_CALLBACK_MAGIC; | |
168 | callbacks_initialized = 1; | |
169 | } | |
170 | } | |
171 | ||
172 | /* Release callbacks (free resources used by them). */ | |
173 | ||
174 | static void | |
fba45db2 | 175 | end_callbacks (void) |
c906108c SS |
176 | { |
177 | if (callbacks_initialized) | |
178 | { | |
179 | gdb_callback.shutdown (&gdb_callback); | |
180 | callbacks_initialized = 0; | |
181 | } | |
182 | } | |
183 | ||
184 | /* GDB version of os_write_stdout callback. */ | |
185 | ||
c5aa993b | 186 | static int |
fba45db2 | 187 | gdb_os_write_stdout (host_callback *p, const char *buf, int len) |
c906108c SS |
188 | { |
189 | int i; | |
190 | char b[2]; | |
191 | ||
d9fcf2fb | 192 | ui_file_write (gdb_stdtarg, buf, len); |
c906108c SS |
193 | return len; |
194 | } | |
195 | ||
196 | /* GDB version of os_flush_stdout callback. */ | |
197 | ||
198 | static void | |
fba45db2 | 199 | gdb_os_flush_stdout (host_callback *p) |
c906108c | 200 | { |
4ce44c66 | 201 | gdb_flush (gdb_stdtarg); |
c906108c SS |
202 | } |
203 | ||
204 | /* GDB version of os_write_stderr callback. */ | |
205 | ||
c5aa993b | 206 | static int |
fba45db2 | 207 | gdb_os_write_stderr (host_callback *p, const char *buf, int len) |
c906108c SS |
208 | { |
209 | int i; | |
210 | char b[2]; | |
211 | ||
c5aa993b | 212 | for (i = 0; i < len; i++) |
c906108c SS |
213 | { |
214 | b[0] = buf[i]; | |
215 | b[1] = 0; | |
43ff13b4 | 216 | fputs_unfiltered (b, gdb_stdtarg); |
c906108c SS |
217 | } |
218 | return len; | |
219 | } | |
220 | ||
221 | /* GDB version of os_flush_stderr callback. */ | |
222 | ||
223 | static void | |
fba45db2 | 224 | gdb_os_flush_stderr (host_callback *p) |
c906108c SS |
225 | { |
226 | gdb_flush (gdb_stderr); | |
227 | } | |
228 | ||
229 | /* GDB version of printf_filtered callback. */ | |
230 | ||
c906108c | 231 | static void |
c5aa993b | 232 | gdb_os_printf_filtered (host_callback * p, const char *format,...) |
c906108c SS |
233 | { |
234 | va_list args; | |
c906108c | 235 | va_start (args, format); |
c906108c SS |
236 | |
237 | vfprintf_filtered (gdb_stdout, format, args); | |
238 | ||
239 | va_end (args); | |
240 | } | |
241 | ||
242 | /* GDB version of error vprintf_filtered. */ | |
243 | ||
c906108c | 244 | static void |
c5aa993b | 245 | gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
246 | { |
247 | vfprintf_filtered (gdb_stdout, format, ap); | |
248 | } | |
249 | ||
250 | /* GDB version of error evprintf_filtered. */ | |
251 | ||
c906108c | 252 | static void |
c5aa993b | 253 | gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
254 | { |
255 | vfprintf_filtered (gdb_stderr, format, ap); | |
256 | } | |
257 | ||
258 | /* GDB version of error callback. */ | |
259 | ||
c906108c | 260 | static void |
c5aa993b | 261 | gdb_os_error (host_callback * p, const char *format,...) |
c906108c SS |
262 | { |
263 | if (error_hook) | |
264 | (*error_hook) (); | |
c5aa993b | 265 | else |
c906108c SS |
266 | { |
267 | va_list args; | |
c906108c | 268 | va_start (args, format); |
4ce44c66 | 269 | verror (format, args); |
c906108c | 270 | va_end (args); |
c906108c SS |
271 | } |
272 | } | |
273 | ||
274 | static void | |
fba45db2 | 275 | gdbsim_fetch_register (int regno) |
c906108c SS |
276 | { |
277 | static int warn_user = 1; | |
c5aa993b | 278 | if (regno == -1) |
c906108c SS |
279 | { |
280 | for (regno = 0; regno < NUM_REGS; regno++) | |
281 | gdbsim_fetch_register (regno); | |
282 | } | |
d4f3574e SS |
283 | else if (REGISTER_NAME (regno) != NULL |
284 | && *REGISTER_NAME (regno) != '\0') | |
c906108c SS |
285 | { |
286 | char buf[MAX_REGISTER_RAW_SIZE]; | |
d4f3574e SS |
287 | int nr_bytes; |
288 | if (REGISTER_SIM_REGNO (regno) >= 0) | |
289 | nr_bytes = sim_fetch_register (gdbsim_desc, | |
290 | REGISTER_SIM_REGNO (regno), | |
291 | buf, REGISTER_RAW_SIZE (regno)); | |
292 | else | |
293 | nr_bytes = 0; | |
c906108c SS |
294 | if (nr_bytes == 0) |
295 | /* register not applicable, supply zero's */ | |
296 | memset (buf, 0, MAX_REGISTER_RAW_SIZE); | |
297 | else if (nr_bytes > 0 && nr_bytes != REGISTER_RAW_SIZE (regno) | |
298 | && warn_user) | |
299 | { | |
d4f3574e SS |
300 | fprintf_unfiltered (gdb_stderr, |
301 | "Size of register %s (%d/%d) incorrect (%d instead of %d))", | |
302 | REGISTER_NAME (regno), | |
303 | regno, REGISTER_SIM_REGNO (regno), | |
304 | nr_bytes, REGISTER_RAW_SIZE (regno)); | |
c906108c SS |
305 | warn_user = 0; |
306 | } | |
307 | supply_register (regno, buf); | |
308 | if (sr_get_debug ()) | |
309 | { | |
310 | printf_filtered ("gdbsim_fetch_register: %d", regno); | |
311 | /* FIXME: We could print something more intelligible. */ | |
312 | dump_mem (buf, REGISTER_RAW_SIZE (regno)); | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
317 | ||
318 | static void | |
fba45db2 | 319 | gdbsim_store_register (int regno) |
c906108c | 320 | { |
c5aa993b | 321 | if (regno == -1) |
c906108c SS |
322 | { |
323 | for (regno = 0; regno < NUM_REGS; regno++) | |
324 | gdbsim_store_register (regno); | |
325 | } | |
d4f3574e SS |
326 | else if (REGISTER_NAME (regno) != NULL |
327 | && *REGISTER_NAME (regno) != '\0' | |
328 | && REGISTER_SIM_REGNO (regno) >= 0) | |
c906108c SS |
329 | { |
330 | char tmp[MAX_REGISTER_RAW_SIZE]; | |
331 | int nr_bytes; | |
332 | read_register_gen (regno, tmp); | |
d4f3574e SS |
333 | nr_bytes = sim_store_register (gdbsim_desc, |
334 | REGISTER_SIM_REGNO (regno), | |
335 | tmp, REGISTER_RAW_SIZE (regno)); | |
c906108c | 336 | if (nr_bytes > 0 && nr_bytes != REGISTER_RAW_SIZE (regno)) |
96baa820 | 337 | internal_error ("Register size different to expected"); |
c906108c SS |
338 | if (sr_get_debug ()) |
339 | { | |
340 | printf_filtered ("gdbsim_store_register: %d", regno); | |
341 | /* FIXME: We could print something more intelligible. */ | |
342 | dump_mem (tmp, REGISTER_RAW_SIZE (regno)); | |
343 | } | |
344 | } | |
345 | } | |
346 | ||
347 | /* Kill the running program. This may involve closing any open files | |
348 | and releasing other resources acquired by the simulated program. */ | |
349 | ||
350 | static void | |
fba45db2 | 351 | gdbsim_kill (void) |
c906108c SS |
352 | { |
353 | if (sr_get_debug ()) | |
354 | printf_filtered ("gdbsim_kill\n"); | |
355 | ||
356 | /* There is no need to `kill' running simulator - the simulator is | |
357 | not running */ | |
358 | inferior_pid = 0; | |
359 | } | |
360 | ||
361 | /* Load an executable file into the target process. This is expected to | |
362 | not only bring new code into the target process, but also to update | |
363 | GDB's symbol tables to match. */ | |
364 | ||
365 | static void | |
fba45db2 | 366 | gdbsim_load (char *prog, int fromtty) |
c906108c SS |
367 | { |
368 | if (sr_get_debug ()) | |
369 | printf_filtered ("gdbsim_load: prog \"%s\"\n", prog); | |
370 | ||
371 | inferior_pid = 0; | |
372 | ||
373 | /* FIXME: We will print two messages on error. | |
374 | Need error to either not print anything if passed NULL or need | |
375 | another routine that doesn't take any arguments. */ | |
376 | if (sim_load (gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL) | |
377 | error ("unable to load program"); | |
378 | ||
379 | /* FIXME: If a load command should reset the targets registers then | |
380 | a call to sim_create_inferior() should go here. */ | |
381 | ||
382 | program_loaded = 1; | |
383 | } | |
384 | ||
385 | ||
386 | /* Start an inferior process and set inferior_pid to its pid. | |
387 | EXEC_FILE is the file to run. | |
388 | ARGS is a string containing the arguments to the program. | |
389 | ENV is the environment vector to pass. Errors reported with error(). | |
390 | On VxWorks and various standalone systems, we ignore exec_file. */ | |
391 | /* This is called not only when we first attach, but also when the | |
392 | user types "run" after having attached. */ | |
393 | ||
394 | static void | |
fba45db2 | 395 | gdbsim_create_inferior (char *exec_file, char *args, char **env) |
c906108c SS |
396 | { |
397 | int len; | |
c5aa993b | 398 | char *arg_buf, **argv; |
c906108c SS |
399 | |
400 | if (exec_file == 0 || exec_bfd == 0) | |
401 | warning ("No executable file specified."); | |
c5aa993b | 402 | if (!program_loaded) |
c906108c SS |
403 | warning ("No program loaded."); |
404 | ||
405 | if (sr_get_debug ()) | |
406 | printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", | |
c5aa993b | 407 | (exec_file ? exec_file : "(NULL)"), |
c906108c SS |
408 | args); |
409 | ||
c5aa993b | 410 | gdbsim_kill (); |
c906108c SS |
411 | remove_breakpoints (); |
412 | init_wait_for_inferior (); | |
413 | ||
414 | if (exec_file != NULL) | |
415 | { | |
c5aa993b | 416 | len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10; |
c906108c SS |
417 | arg_buf = (char *) alloca (len); |
418 | arg_buf[0] = '\0'; | |
419 | strcat (arg_buf, exec_file); | |
420 | strcat (arg_buf, " "); | |
421 | strcat (arg_buf, args); | |
422 | argv = buildargv (arg_buf); | |
7a292a7a | 423 | make_cleanup_freeargv (argv); |
c906108c SS |
424 | } |
425 | else | |
426 | argv = NULL; | |
427 | sim_create_inferior (gdbsim_desc, exec_bfd, argv, env); | |
428 | ||
429 | inferior_pid = 42; | |
c5aa993b | 430 | insert_breakpoints (); /* Needed to get correct instruction in cache */ |
c906108c SS |
431 | |
432 | clear_proceed_status (); | |
433 | ||
434 | /* NB: Entry point already set by sim_create_inferior. */ | |
2acceee2 | 435 | proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); |
c906108c SS |
436 | } |
437 | ||
438 | /* The open routine takes the rest of the parameters from the command, | |
439 | and (if successful) pushes a new target onto the stack. | |
440 | Targets should supply this routine, if only to provide an error message. */ | |
441 | /* Called when selecting the simulator. EG: (gdb) target sim name. */ | |
442 | ||
443 | static void | |
fba45db2 | 444 | gdbsim_open (char *args, int from_tty) |
c906108c SS |
445 | { |
446 | int len; | |
447 | char *arg_buf; | |
448 | char **argv; | |
449 | ||
450 | if (sr_get_debug ()) | |
451 | printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)"); | |
452 | ||
453 | /* Remove current simulator if one exists. Only do this if the simulator | |
454 | has been opened because sim_close requires it. | |
455 | This is important because the call to push_target below will cause | |
456 | sim_close to be called if the simulator is already open, but push_target | |
457 | is called after sim_open! We can't move the call to push_target before | |
458 | the call to sim_open because sim_open may invoke `error'. */ | |
459 | if (gdbsim_desc != NULL) | |
460 | unpush_target (&gdbsim_ops); | |
461 | ||
c5aa993b | 462 | len = (7 + 1 /* gdbsim */ |
c906108c SS |
463 | + strlen (" -E little") |
464 | + strlen (" --architecture=xxxxxxxxxx") | |
465 | + (args ? strlen (args) : 0) | |
c5aa993b | 466 | + 50) /* slack */ ; |
c906108c | 467 | arg_buf = (char *) alloca (len); |
c5aa993b | 468 | strcpy (arg_buf, "gdbsim"); /* 7 */ |
c906108c SS |
469 | /* Specify the byte order for the target when it is both selectable |
470 | and explicitly specified by the user (not auto detected). */ | |
471 | if (TARGET_BYTE_ORDER_SELECTABLE_P | |
472 | && !TARGET_BYTE_ORDER_AUTO) | |
473 | { | |
474 | switch (TARGET_BYTE_ORDER) | |
475 | { | |
476 | case BIG_ENDIAN: | |
477 | strcat (arg_buf, " -E big"); | |
478 | break; | |
479 | case LITTLE_ENDIAN: | |
480 | strcat (arg_buf, " -E little"); | |
481 | break; | |
482 | default: | |
96baa820 | 483 | internal_error ("Value of TARGET_BYTE_ORDER unknown"); |
c906108c SS |
484 | } |
485 | } | |
486 | /* Specify the architecture of the target when it has been | |
487 | explicitly specified */ | |
488 | if (!TARGET_ARCHITECTURE_AUTO) | |
489 | { | |
490 | strcat (arg_buf, " --architecture="); | |
491 | strcat (arg_buf, TARGET_ARCHITECTURE->printable_name); | |
492 | } | |
493 | /* finally, any explicit args */ | |
494 | if (args) | |
495 | { | |
c5aa993b | 496 | strcat (arg_buf, " "); /* 1 */ |
c906108c SS |
497 | strcat (arg_buf, args); |
498 | } | |
499 | argv = buildargv (arg_buf); | |
500 | if (argv == NULL) | |
501 | error ("Insufficient memory available to allocate simulator arg list."); | |
7a292a7a | 502 | make_cleanup_freeargv (argv); |
c906108c SS |
503 | |
504 | init_callbacks (); | |
505 | gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, argv); | |
506 | ||
507 | if (gdbsim_desc == 0) | |
508 | error ("unable to create simulator instance"); | |
509 | ||
510 | push_target (&gdbsim_ops); | |
511 | target_fetch_registers (-1); | |
512 | printf_filtered ("Connected to the simulator.\n"); | |
513 | } | |
514 | ||
515 | /* Does whatever cleanup is required for a target that we are no longer | |
516 | going to be calling. Argument says whether we are quitting gdb and | |
517 | should not get hung in case of errors, or whether we want a clean | |
518 | termination even if it takes a while. This routine is automatically | |
519 | always called just before a routine is popped off the target stack. | |
520 | Closing file descriptors and freeing memory are typical things it should | |
521 | do. */ | |
522 | /* Close out all files and local state before this target loses control. */ | |
523 | ||
524 | static void | |
fba45db2 | 525 | gdbsim_close (int quitting) |
c906108c SS |
526 | { |
527 | if (sr_get_debug ()) | |
528 | printf_filtered ("gdbsim_close: quitting %d\n", quitting); | |
529 | ||
530 | program_loaded = 0; | |
531 | ||
532 | if (gdbsim_desc != NULL) | |
533 | { | |
534 | sim_close (gdbsim_desc, quitting); | |
535 | gdbsim_desc = NULL; | |
536 | } | |
537 | ||
538 | end_callbacks (); | |
75660bc0 | 539 | generic_mourn_inferior (); |
c906108c SS |
540 | } |
541 | ||
542 | /* Takes a program previously attached to and detaches it. | |
543 | The program may resume execution (some targets do, some don't) and will | |
544 | no longer stop on signals, etc. We better not have left any breakpoints | |
545 | in the program or it'll die when it hits one. ARGS is arguments | |
546 | typed by the user (e.g. a signal to send the process). FROM_TTY | |
547 | says whether to be verbose or not. */ | |
548 | /* Terminate the open connection to the remote debugger. | |
549 | Use this when you want to detach and do something else with your gdb. */ | |
550 | ||
551 | static void | |
fba45db2 | 552 | gdbsim_detach (char *args, int from_tty) |
c906108c SS |
553 | { |
554 | if (sr_get_debug ()) | |
555 | printf_filtered ("gdbsim_detach: args \"%s\"\n", args); | |
556 | ||
557 | pop_target (); /* calls gdbsim_close to do the real work */ | |
558 | if (from_tty) | |
559 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); | |
560 | } | |
c5aa993b | 561 | |
c906108c SS |
562 | /* Resume execution of the target process. STEP says whether to single-step |
563 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given | |
564 | to the target, or zero for no signal. */ | |
565 | ||
566 | static enum target_signal resume_siggnal; | |
567 | static int resume_step; | |
568 | ||
569 | static void | |
fba45db2 | 570 | gdbsim_resume (int pid, int step, enum target_signal siggnal) |
c906108c SS |
571 | { |
572 | if (inferior_pid != 42) | |
573 | error ("The program is not being run."); | |
574 | ||
575 | if (sr_get_debug ()) | |
576 | printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); | |
577 | ||
578 | resume_siggnal = siggnal; | |
579 | resume_step = step; | |
580 | } | |
581 | ||
582 | /* Notify the simulator of an asynchronous request to stop. | |
c5aa993b | 583 | |
c906108c SS |
584 | The simulator shall ensure that the stop request is eventually |
585 | delivered to the simulator. If the call is made while the | |
586 | simulator is not running then the stop request is processed when | |
587 | the simulator is next resumed. | |
588 | ||
589 | For simulators that do not support this operation, just abort */ | |
590 | ||
591 | static void | |
fba45db2 | 592 | gdbsim_stop (void) |
c906108c | 593 | { |
c5aa993b | 594 | if (!sim_stop (gdbsim_desc)) |
c906108c SS |
595 | { |
596 | quit (); | |
597 | } | |
598 | } | |
599 | ||
600 | /* GDB version of os_poll_quit callback. | |
601 | Taken from gdb/util.c - should be in a library */ | |
602 | ||
603 | static int | |
fba45db2 | 604 | gdb_os_poll_quit (host_callback *p) |
c906108c | 605 | { |
7a292a7a SS |
606 | if (ui_loop_hook != NULL) |
607 | ui_loop_hook (0); | |
608 | ||
c906108c | 609 | notice_quit (); |
c5aa993b | 610 | if (quit_flag) /* gdb's idea of quit */ |
c906108c | 611 | { |
c5aa993b | 612 | quit_flag = 0; /* we've stolen it */ |
c906108c SS |
613 | return 1; |
614 | } | |
615 | else if (immediate_quit) | |
616 | { | |
617 | return 1; | |
618 | } | |
619 | return 0; | |
620 | } | |
621 | ||
622 | /* Wait for inferior process to do something. Return pid of child, | |
623 | or -1 in case of error; store status through argument pointer STATUS, | |
624 | just as `wait' would. */ | |
625 | ||
626 | static void | |
fba45db2 | 627 | gdbsim_cntrl_c (int signo) |
c906108c SS |
628 | { |
629 | gdbsim_stop (); | |
630 | } | |
631 | ||
632 | static int | |
fba45db2 | 633 | gdbsim_wait (int pid, struct target_waitstatus *status) |
c906108c SS |
634 | { |
635 | static RETSIGTYPE (*prev_sigint) (); | |
636 | int sigrc = 0; | |
637 | enum sim_stop reason = sim_running; | |
638 | ||
639 | if (sr_get_debug ()) | |
640 | printf_filtered ("gdbsim_wait\n"); | |
641 | ||
642 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
643 | { | |
644 | struct sigaction sa, osa; | |
645 | sa.sa_handler = gdbsim_cntrl_c; | |
646 | sigemptyset (&sa.sa_mask); | |
647 | sa.sa_flags = 0; | |
648 | sigaction (SIGINT, &sa, &osa); | |
649 | prev_sigint = osa.sa_handler; | |
650 | } | |
651 | #else | |
652 | prev_sigint = signal (SIGINT, gdbsim_cntrl_c); | |
653 | #endif | |
654 | sim_resume (gdbsim_desc, resume_step, | |
655 | target_signal_to_host (resume_siggnal)); | |
656 | signal (SIGINT, prev_sigint); | |
657 | resume_step = 0; | |
658 | ||
659 | sim_stop_reason (gdbsim_desc, &reason, &sigrc); | |
660 | ||
661 | switch (reason) | |
662 | { | |
663 | case sim_exited: | |
664 | status->kind = TARGET_WAITKIND_EXITED; | |
665 | status->value.integer = sigrc; | |
666 | break; | |
667 | case sim_stopped: | |
668 | switch (sigrc) | |
669 | { | |
670 | case SIGABRT: | |
671 | quit (); | |
672 | break; | |
673 | case SIGINT: | |
674 | case SIGTRAP: | |
675 | default: | |
676 | status->kind = TARGET_WAITKIND_STOPPED; | |
677 | /* The signal in sigrc is a host signal. That probably | |
678 | should be fixed. */ | |
679 | status->value.sig = target_signal_from_host (sigrc); | |
680 | break; | |
681 | } | |
682 | break; | |
683 | case sim_signalled: | |
684 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
685 | /* The signal in sigrc is a host signal. That probably | |
c5aa993b | 686 | should be fixed. */ |
c906108c SS |
687 | status->value.sig = target_signal_from_host (sigrc); |
688 | break; | |
689 | case sim_running: | |
690 | case sim_polling: | |
691 | /* FIXME: Is this correct? */ | |
692 | break; | |
693 | } | |
694 | ||
695 | return inferior_pid; | |
696 | } | |
697 | ||
698 | /* Get ready to modify the registers array. On machines which store | |
699 | individual registers, this doesn't need to do anything. On machines | |
700 | which store all the registers in one fell swoop, this makes sure | |
701 | that registers contains all the registers from the program being | |
702 | debugged. */ | |
703 | ||
704 | static void | |
fba45db2 | 705 | gdbsim_prepare_to_store (void) |
c906108c SS |
706 | { |
707 | /* Do nothing, since we can store individual regs */ | |
708 | } | |
709 | ||
d93bce06 KB |
710 | /* Transfer LEN bytes between GDB address MYADDR and target address |
711 | MEMADDR. If WRITE is non-zero, transfer them to the target, | |
712 | otherwise transfer them from the target. TARGET is unused. | |
713 | ||
714 | Returns the number of bytes transferred. */ | |
715 | ||
c906108c | 716 | static int |
d93bce06 | 717 | gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len, |
29e57380 C |
718 | int write, |
719 | struct mem_attrib *attrib ATTRIBUTE_UNUSED, | |
720 | struct target_ops *target ATTRIBUTE_UNUSED) | |
c906108c | 721 | { |
c5aa993b | 722 | if (!program_loaded) |
c906108c SS |
723 | error ("No program loaded."); |
724 | ||
725 | if (sr_get_debug ()) | |
726 | { | |
d4f3574e SS |
727 | /* FIXME: Send to something other than STDOUT? */ |
728 | printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x"); | |
729 | gdb_print_host_address (myaddr, gdb_stdout); | |
730 | printf_filtered (", memaddr 0x%s, len %d, write %d\n", | |
731 | paddr_nz (memaddr), len, write); | |
c906108c | 732 | if (sr_get_debug () && write) |
c5aa993b | 733 | dump_mem (myaddr, len); |
c906108c SS |
734 | } |
735 | ||
736 | if (write) | |
737 | { | |
738 | len = sim_write (gdbsim_desc, memaddr, myaddr, len); | |
739 | } | |
c5aa993b | 740 | else |
c906108c SS |
741 | { |
742 | len = sim_read (gdbsim_desc, memaddr, myaddr, len); | |
743 | if (sr_get_debug () && len > 0) | |
c5aa993b JM |
744 | dump_mem (myaddr, len); |
745 | } | |
c906108c SS |
746 | return len; |
747 | } | |
748 | ||
749 | static void | |
fba45db2 | 750 | gdbsim_files_info (struct target_ops *target) |
c906108c SS |
751 | { |
752 | char *file = "nothing"; | |
753 | ||
754 | if (exec_bfd) | |
755 | file = bfd_get_filename (exec_bfd); | |
756 | ||
757 | if (sr_get_debug ()) | |
758 | printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); | |
759 | ||
760 | if (exec_bfd) | |
761 | { | |
762 | printf_filtered ("\tAttached to %s running program %s\n", | |
763 | target_shortname, file); | |
764 | sim_info (gdbsim_desc, 0); | |
765 | } | |
766 | } | |
767 | ||
768 | /* Clear the simulator's notion of what the break points are. */ | |
769 | ||
770 | static void | |
fba45db2 | 771 | gdbsim_mourn_inferior (void) |
c5aa993b | 772 | { |
c906108c SS |
773 | if (sr_get_debug ()) |
774 | printf_filtered ("gdbsim_mourn_inferior:\n"); | |
775 | ||
776 | remove_breakpoints (); | |
777 | generic_mourn_inferior (); | |
778 | } | |
779 | ||
780 | static int | |
fba45db2 | 781 | gdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache) |
c906108c SS |
782 | { |
783 | #ifdef SIM_HAS_BREAKPOINTS | |
784 | SIM_RC retcode; | |
785 | ||
786 | retcode = sim_set_breakpoint (gdbsim_desc, addr); | |
787 | ||
788 | switch (retcode) | |
789 | { | |
790 | case SIM_RC_OK: | |
791 | return 0; | |
792 | case SIM_RC_INSUFFICIENT_RESOURCES: | |
793 | return ENOMEM; | |
794 | default: | |
795 | return EIO; | |
796 | } | |
797 | #else | |
798 | return memory_insert_breakpoint (addr, contents_cache); | |
799 | #endif | |
800 | } | |
801 | ||
802 | static int | |
fba45db2 | 803 | gdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache) |
c906108c SS |
804 | { |
805 | #ifdef SIM_HAS_BREAKPOINTS | |
806 | SIM_RC retcode; | |
807 | ||
808 | retcode = sim_clear_breakpoint (gdbsim_desc, addr); | |
809 | ||
810 | switch (retcode) | |
811 | { | |
812 | case SIM_RC_OK: | |
813 | case SIM_RC_UNKNOWN_BREAKPOINT: | |
814 | return 0; | |
815 | case SIM_RC_INSUFFICIENT_RESOURCES: | |
816 | return ENOMEM; | |
817 | default: | |
818 | return EIO; | |
819 | } | |
820 | #else | |
821 | return memory_remove_breakpoint (addr, contents_cache); | |
822 | #endif | |
823 | } | |
824 | ||
825 | /* Pass the command argument through to the simulator verbatim. The | |
826 | simulator must do any command interpretation work. */ | |
827 | ||
828 | void | |
fba45db2 | 829 | simulator_command (char *args, int from_tty) |
c906108c SS |
830 | { |
831 | if (gdbsim_desc == NULL) | |
832 | { | |
833 | ||
834 | /* PREVIOUSLY: The user may give a command before the simulator | |
835 | is opened. [...] (??? assuming of course one wishes to | |
836 | continue to allow commands to be sent to unopened simulators, | |
837 | which isn't entirely unreasonable). */ | |
838 | ||
839 | /* The simulator is a builtin abstraction of a remote target. | |
840 | Consistent with that model, access to the simulator, via sim | |
841 | commands, is restricted to the period when the channel to the | |
842 | simulator is open. */ | |
843 | ||
844 | error ("Not connected to the simulator target"); | |
845 | } | |
846 | ||
847 | sim_do_command (gdbsim_desc, args); | |
848 | ||
849 | /* Invalidate the register cache, in case the simulator command does | |
850 | something funny. */ | |
c5aa993b | 851 | registers_changed (); |
c906108c SS |
852 | } |
853 | ||
854 | /* Define the target subroutine names */ | |
855 | ||
c5aa993b JM |
856 | struct target_ops gdbsim_ops; |
857 | ||
858 | static void | |
859 | init_gdbsim_ops (void) | |
860 | { | |
861 | gdbsim_ops.to_shortname = "sim"; | |
862 | gdbsim_ops.to_longname = "simulator"; | |
863 | gdbsim_ops.to_doc = "Use the compiled-in simulator."; | |
864 | gdbsim_ops.to_open = gdbsim_open; | |
865 | gdbsim_ops.to_close = gdbsim_close; | |
866 | gdbsim_ops.to_attach = NULL; | |
867 | gdbsim_ops.to_post_attach = NULL; | |
868 | gdbsim_ops.to_require_attach = NULL; | |
869 | gdbsim_ops.to_detach = gdbsim_detach; | |
870 | gdbsim_ops.to_require_detach = NULL; | |
871 | gdbsim_ops.to_resume = gdbsim_resume; | |
872 | gdbsim_ops.to_wait = gdbsim_wait; | |
873 | gdbsim_ops.to_post_wait = NULL; | |
874 | gdbsim_ops.to_fetch_registers = gdbsim_fetch_register; | |
875 | gdbsim_ops.to_store_registers = gdbsim_store_register; | |
876 | gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store; | |
877 | gdbsim_ops.to_xfer_memory = gdbsim_xfer_inferior_memory; | |
878 | gdbsim_ops.to_files_info = gdbsim_files_info; | |
879 | gdbsim_ops.to_insert_breakpoint = gdbsim_insert_breakpoint; | |
880 | gdbsim_ops.to_remove_breakpoint = gdbsim_remove_breakpoint; | |
881 | gdbsim_ops.to_terminal_init = NULL; | |
882 | gdbsim_ops.to_terminal_inferior = NULL; | |
883 | gdbsim_ops.to_terminal_ours_for_output = NULL; | |
884 | gdbsim_ops.to_terminal_ours = NULL; | |
885 | gdbsim_ops.to_terminal_info = NULL; | |
886 | gdbsim_ops.to_kill = gdbsim_kill; | |
887 | gdbsim_ops.to_load = gdbsim_load; | |
888 | gdbsim_ops.to_lookup_symbol = NULL; | |
889 | gdbsim_ops.to_create_inferior = gdbsim_create_inferior; | |
c906108c SS |
890 | gdbsim_ops.to_post_startup_inferior = NULL; |
891 | gdbsim_ops.to_acknowledge_created_inferior = NULL; | |
892 | gdbsim_ops.to_clone_and_follow_inferior = NULL; | |
893 | gdbsim_ops.to_post_follow_inferior_by_clone = NULL; | |
894 | gdbsim_ops.to_insert_fork_catchpoint = NULL; | |
895 | gdbsim_ops.to_remove_fork_catchpoint = NULL; | |
896 | gdbsim_ops.to_insert_vfork_catchpoint = NULL; | |
897 | gdbsim_ops.to_remove_vfork_catchpoint = NULL; | |
898 | gdbsim_ops.to_has_forked = NULL; | |
899 | gdbsim_ops.to_has_vforked = NULL; | |
900 | gdbsim_ops.to_can_follow_vfork_prior_to_exec = NULL; | |
901 | gdbsim_ops.to_post_follow_vfork = NULL; | |
902 | gdbsim_ops.to_insert_exec_catchpoint = NULL; | |
903 | gdbsim_ops.to_remove_exec_catchpoint = NULL; | |
904 | gdbsim_ops.to_has_execd = NULL; | |
905 | gdbsim_ops.to_reported_exec_events_per_exec_call = NULL; | |
906 | gdbsim_ops.to_has_exited = NULL; | |
c5aa993b JM |
907 | gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior; |
908 | gdbsim_ops.to_can_run = 0; | |
909 | gdbsim_ops.to_notice_signals = 0; | |
910 | gdbsim_ops.to_thread_alive = 0; | |
911 | gdbsim_ops.to_stop = gdbsim_stop; | |
912 | gdbsim_ops.to_pid_to_exec_file = NULL; | |
913 | gdbsim_ops.to_core_file_to_sym_file = NULL; | |
914 | gdbsim_ops.to_stratum = process_stratum; | |
915 | gdbsim_ops.DONT_USE = NULL; | |
916 | gdbsim_ops.to_has_all_memory = 1; | |
917 | gdbsim_ops.to_has_memory = 1; | |
918 | gdbsim_ops.to_has_stack = 1; | |
919 | gdbsim_ops.to_has_registers = 1; | |
920 | gdbsim_ops.to_has_execution = 1; | |
921 | gdbsim_ops.to_sections = NULL; | |
922 | gdbsim_ops.to_sections_end = NULL; | |
923 | gdbsim_ops.to_magic = OPS_MAGIC; | |
c906108c SS |
924 | |
925 | #ifdef TARGET_REDEFINE_DEFAULT_OPS | |
926 | TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops); | |
927 | #endif | |
928 | } | |
929 | ||
930 | void | |
fba45db2 | 931 | _initialize_remote_sim (void) |
c906108c | 932 | { |
c5aa993b | 933 | init_gdbsim_ops (); |
c906108c SS |
934 | add_target (&gdbsim_ops); |
935 | ||
936 | add_com ("sim <command>", class_obscure, simulator_command, | |
c5aa993b | 937 | "Send a command to the simulator."); |
c906108c | 938 | } |