]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic remote debugging interface for simulators. |
0a65a603 | 2 | |
e2882c85 | 3 | Copyright (C) 1993-2018 Free Software Foundation, Inc. |
0a65a603 | 4 | |
c906108c SS |
5 | Contributed by Cygnus Support. |
6 | Steve Chamberlain (sac@cygnus.com). | |
7 | ||
c5aa993b | 8 | This file is part of GDB. |
c906108c | 9 | |
c5aa993b JM |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 13 | (at your option) any later version. |
c906108c | 14 | |
c5aa993b JM |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
c906108c | 19 | |
c5aa993b | 20 | You should have received a copy of the GNU General Public License |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
22 | |
23 | #include "defs.h" | |
23bf70af | 24 | #include "gdb_bfd.h" |
c906108c | 25 | #include "inferior.h" |
45741a9c | 26 | #include "infrun.h" |
c906108c | 27 | #include "value.h" |
c906108c SS |
28 | #include <ctype.h> |
29 | #include <fcntl.h> | |
30 | #include <signal.h> | |
31 | #include <setjmp.h> | |
c906108c SS |
32 | #include "terminal.h" |
33 | #include "target.h" | |
34 | #include "gdbcore.h" | |
3c25f8c7 AC |
35 | #include "gdb/callback.h" |
36 | #include "gdb/remote-sim.h" | |
c906108c | 37 | #include "command.h" |
4e052eda | 38 | #include "regcache.h" |
8238d0bf | 39 | #include "sim-regno.h" |
a8cf2722 | 40 | #include "arch-utils.h" |
1986bccd | 41 | #include "readline/readline.h" |
9de2bdd7 | 42 | #include "gdbthread.h" |
a4d1e79a | 43 | #include "common/byte-vector.h" |
c906108c SS |
44 | |
45 | /* Prototypes */ | |
46 | ||
a14ed312 | 47 | static void init_callbacks (void); |
c906108c | 48 | |
a14ed312 | 49 | static void end_callbacks (void); |
c906108c | 50 | |
a14ed312 | 51 | static int gdb_os_write_stdout (host_callback *, const char *, int); |
c906108c | 52 | |
a14ed312 | 53 | static void gdb_os_flush_stdout (host_callback *); |
c906108c | 54 | |
a14ed312 | 55 | static int gdb_os_write_stderr (host_callback *, const char *, int); |
c906108c | 56 | |
a14ed312 | 57 | static void gdb_os_flush_stderr (host_callback *); |
c906108c | 58 | |
a14ed312 | 59 | static int gdb_os_poll_quit (host_callback *); |
c906108c | 60 | |
0df8b418 | 61 | /* printf_filtered is depreciated. */ |
a14ed312 | 62 | static void gdb_os_printf_filtered (host_callback *, const char *, ...); |
c906108c | 63 | |
a14ed312 | 64 | static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 65 | |
a14ed312 | 66 | static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 67 | |
c25c4a8b JK |
68 | static void gdb_os_error (host_callback *, const char *, ...) |
69 | ATTRIBUTE_NORETURN; | |
c906108c | 70 | |
f6ac5f3d | 71 | void simulator_command (char *args, int from_tty); |
c906108c | 72 | |
f6ac5f3d | 73 | /* Naming convention: |
c906108c | 74 | |
f6ac5f3d PA |
75 | sim_* are the interface to the simulator (see remote-sim.h). |
76 | gdbsim_* are stuff which is internal to gdb. */ | |
c906108c | 77 | |
f6ac5f3d PA |
78 | struct gdbsim_target final |
79 | : public memory_breakpoint_target<target_ops> | |
80 | { | |
81 | gdbsim_target () | |
82 | { to_stratum = process_stratum; } | |
c906108c | 83 | |
f6ac5f3d PA |
84 | const char *shortname () override |
85 | { return "sim"; } | |
c906108c | 86 | |
f6ac5f3d PA |
87 | const char *longname () override |
88 | { return _("simulator"); } | |
c906108c | 89 | |
f6ac5f3d PA |
90 | const char *doc () override |
91 | { return _("Use the compiled-in simulator."); } | |
c906108c | 92 | |
f6ac5f3d PA |
93 | void open (const char *, int) override; |
94 | void close () override; | |
c906108c | 95 | |
f6ac5f3d | 96 | void detach (inferior *inf, int) override; |
c906108c | 97 | |
f6ac5f3d PA |
98 | void resume (ptid_t, int, enum gdb_signal) override; |
99 | ptid_t wait (ptid_t, struct target_waitstatus *, int) override; | |
c906108c | 100 | |
f6ac5f3d PA |
101 | void fetch_registers (struct regcache *, int) override; |
102 | void store_registers (struct regcache *, int) override; | |
103 | void prepare_to_store (struct regcache *) override; | |
c906108c | 104 | |
f6ac5f3d PA |
105 | enum target_xfer_status xfer_partial (enum target_object object, |
106 | const char *annex, | |
107 | gdb_byte *readbuf, | |
108 | const gdb_byte *writebuf, | |
109 | ULONGEST offset, ULONGEST len, | |
110 | ULONGEST *xfered_len) override; | |
111 | ||
112 | void files_info () override; | |
113 | ||
114 | void kill () override; | |
115 | ||
116 | void load (const char *, int) override; | |
117 | ||
118 | bool can_create_inferior () override { return true; } | |
119 | void create_inferior (const char *, const std::string &, | |
120 | char **, int) override; | |
121 | ||
122 | void mourn_inferior () override; | |
123 | ||
124 | void interrupt () override; | |
125 | ||
126 | int thread_alive (ptid_t ptid) override; | |
c906108c | 127 | |
f6ac5f3d PA |
128 | const char *pid_to_str (ptid_t) override; |
129 | ||
130 | int has_all_memory () override; | |
131 | int has_memory () override; | |
132 | ||
133 | int has_stack () override | |
134 | { return default_child_has_stack (); } | |
135 | ||
136 | int has_registers () override | |
137 | { return default_child_has_registers (); } | |
138 | ||
139 | int has_execution (ptid_t ptid) override | |
140 | { return default_child_has_execution (ptid); } | |
141 | }; | |
142 | ||
143 | static struct gdbsim_target gdbsim_ops; | |
c906108c | 144 | |
db04efe6 KB |
145 | static const struct inferior_data *sim_inferior_data_key; |
146 | ||
147 | /* Simulator-specific, per-inferior state. */ | |
148 | struct sim_inferior_data { | |
149 | /* Flag which indicates whether or not the program has been loaded. */ | |
150 | int program_loaded; | |
151 | ||
152 | /* Simulator descriptor for this inferior. */ | |
153 | SIM_DESC gdbsim_desc; | |
154 | ||
155 | /* This is the ptid we use for this particular simulator instance. Its | |
156 | value is somewhat arbitrary, as the simulator target don't have a | |
157 | notion of tasks or threads, but we need something non-null to place | |
158 | in inferior_ptid. For simulators which permit multiple instances, | |
159 | we also need a unique identifier to use for each inferior. */ | |
160 | ptid_t remote_sim_ptid; | |
161 | ||
162 | /* Signal with which to resume. */ | |
2ea28649 | 163 | enum gdb_signal resume_siggnal; |
db04efe6 KB |
164 | |
165 | /* Flag which indicates whether resume should step or not. */ | |
166 | int resume_step; | |
167 | }; | |
168 | ||
169 | /* Flag indicating the "open" status of this module. It's set to 1 | |
170 | in gdbsim_open() and 0 in gdbsim_close(). */ | |
171 | static int gdbsim_is_open = 0; | |
172 | ||
173 | /* Value of the next pid to allocate for an inferior. As indicated | |
174 | elsewhere, its initial value is somewhat arbitrary; it's critical | |
175 | though that it's not zero or negative. */ | |
176 | static int next_pid; | |
177 | #define INITIAL_PID 42000 | |
178 | ||
179 | /* Argument list to pass to sim_open(). It is allocated in gdbsim_open() | |
180 | and deallocated in gdbsim_close(). The lifetime needs to extend beyond | |
181 | the call to gdbsim_open() due to the fact that other sim instances other | |
182 | than the first will be allocated after the gdbsim_open() call. */ | |
183 | static char **sim_argv = NULL; | |
184 | ||
185 | /* OS-level callback functions for write, flush, etc. */ | |
186 | static host_callback gdb_callback; | |
187 | static int callbacks_initialized = 0; | |
188 | ||
189 | /* Callback for iterate_over_inferiors. It checks to see if the sim | |
190 | descriptor passed via ARG is the same as that for the inferior | |
191 | designated by INF. Return true if so; false otherwise. */ | |
192 | ||
193 | static int | |
194 | check_for_duplicate_sim_descriptor (struct inferior *inf, void *arg) | |
195 | { | |
196 | struct sim_inferior_data *sim_data; | |
653090d3 | 197 | SIM_DESC new_sim_desc = (SIM_DESC) arg; |
db04efe6 | 198 | |
653090d3 SM |
199 | sim_data = ((struct sim_inferior_data *) |
200 | inferior_data (inf, sim_inferior_data_key)); | |
db04efe6 KB |
201 | |
202 | return (sim_data != NULL && sim_data->gdbsim_desc == new_sim_desc); | |
203 | } | |
204 | ||
205 | /* Flags indicating whether or not a sim instance is needed. One of these | |
206 | flags should be passed to get_sim_inferior_data(). */ | |
207 | ||
208 | enum {SIM_INSTANCE_NOT_NEEDED = 0, SIM_INSTANCE_NEEDED = 1}; | |
209 | ||
210 | /* Obtain pointer to per-inferior simulator data, allocating it if necessary. | |
211 | Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */ | |
212 | ||
213 | static struct sim_inferior_data * | |
214 | get_sim_inferior_data (struct inferior *inf, int sim_instance_needed) | |
215 | { | |
216 | SIM_DESC sim_desc = NULL; | |
217 | struct sim_inferior_data *sim_data | |
653090d3 | 218 | = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key); |
db04efe6 KB |
219 | |
220 | /* Try to allocate a new sim instance, if needed. We do this ahead of | |
221 | a potential allocation of a sim_inferior_data struct in order to | |
222 | avoid needlessly allocating that struct in the event that the sim | |
223 | instance allocation fails. */ | |
74228e77 | 224 | if (sim_instance_needed == SIM_INSTANCE_NEEDED |
db04efe6 KB |
225 | && (sim_data == NULL || sim_data->gdbsim_desc == NULL)) |
226 | { | |
227 | struct inferior *idup; | |
228 | sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv); | |
229 | if (sim_desc == NULL) | |
230 | error (_("Unable to create simulator instance for inferior %d."), | |
231 | inf->num); | |
232 | ||
233 | idup = iterate_over_inferiors (check_for_duplicate_sim_descriptor, | |
74228e77 | 234 | sim_desc); |
db04efe6 KB |
235 | if (idup != NULL) |
236 | { | |
237 | /* We don't close the descriptor due to the fact that it's | |
238 | shared with some other inferior. If we were to close it, | |
239 | that might needlessly muck up the other inferior. Of | |
240 | course, it's possible that the damage has already been | |
241 | done... Note that it *will* ultimately be closed during | |
242 | cleanup of the other inferior. */ | |
243 | sim_desc = NULL; | |
244 | error ( | |
245 | _("Inferior %d and inferior %d would have identical simulator state.\n" | |
246 | "(This simulator does not support the running of more than one inferior.)"), | |
74228e77 RM |
247 | inf->num, idup->num); |
248 | } | |
db04efe6 KB |
249 | } |
250 | ||
251 | if (sim_data == NULL) | |
252 | { | |
41bf6aca | 253 | sim_data = XCNEW(struct sim_inferior_data); |
db04efe6 KB |
254 | set_inferior_data (inf, sim_inferior_data_key, sim_data); |
255 | ||
256 | /* Allocate a ptid for this inferior. */ | |
257 | sim_data->remote_sim_ptid = ptid_build (next_pid, 0, next_pid); | |
258 | next_pid++; | |
259 | ||
260 | /* Initialize the other instance variables. */ | |
261 | sim_data->program_loaded = 0; | |
262 | sim_data->gdbsim_desc = sim_desc; | |
a493e3e2 | 263 | sim_data->resume_siggnal = GDB_SIGNAL_0; |
db04efe6 KB |
264 | sim_data->resume_step = 0; |
265 | } | |
266 | else if (sim_desc) | |
267 | { | |
268 | /* This handles the case where sim_data was allocated prior to | |
74228e77 | 269 | needing a sim instance. */ |
db04efe6 KB |
270 | sim_data->gdbsim_desc = sim_desc; |
271 | } | |
c906108c | 272 | |
c906108c | 273 | |
db04efe6 KB |
274 | return sim_data; |
275 | } | |
276 | ||
277 | /* Return pointer to per-inferior simulator data using PTID to find the | |
278 | inferior in question. Return NULL when no inferior is found or | |
279 | when ptid has a zero or negative pid component. */ | |
280 | ||
281 | static struct sim_inferior_data * | |
282 | get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed) | |
283 | { | |
284 | struct inferior *inf; | |
285 | int pid = ptid_get_pid (ptid); | |
286 | ||
287 | if (pid <= 0) | |
288 | return NULL; | |
74228e77 | 289 | |
db04efe6 KB |
290 | inf = find_inferior_pid (pid); |
291 | ||
292 | if (inf) | |
293 | return get_sim_inferior_data (inf, sim_instance_needed); | |
294 | else | |
295 | return NULL; | |
296 | } | |
297 | ||
298 | /* Free the per-inferior simulator data. */ | |
299 | ||
300 | static void | |
301 | sim_inferior_data_cleanup (struct inferior *inf, void *data) | |
302 | { | |
653090d3 | 303 | struct sim_inferior_data *sim_data = (struct sim_inferior_data *) data; |
db04efe6 KB |
304 | |
305 | if (sim_data != NULL) | |
306 | { | |
307 | if (sim_data->gdbsim_desc) | |
308 | { | |
309 | sim_close (sim_data->gdbsim_desc, 0); | |
310 | sim_data->gdbsim_desc = NULL; | |
311 | } | |
312 | xfree (sim_data); | |
313 | } | |
314 | } | |
9de2bdd7 | 315 | |
c906108c | 316 | static void |
146ec4db | 317 | dump_mem (const gdb_byte *buf, int len) |
c906108c | 318 | { |
1385f66c | 319 | fputs_unfiltered ("\t", gdb_stdlog); |
3d468296 AB |
320 | |
321 | if (len == 8 || len == 4) | |
c906108c | 322 | { |
3d468296 | 323 | uint32_t l[2]; |
123f5f96 | 324 | |
3d468296 | 325 | memcpy (l, buf, len); |
1385f66c | 326 | fprintf_unfiltered (gdb_stdlog, "0x%08x", l[0]); |
3d468296 | 327 | if (len == 8) |
1385f66c | 328 | fprintf_unfiltered (gdb_stdlog, " 0x%08x", l[1]); |
3d468296 AB |
329 | } |
330 | else | |
331 | { | |
332 | int i; | |
123f5f96 | 333 | |
3d468296 | 334 | for (i = 0; i < len; i++) |
1385f66c | 335 | fprintf_unfiltered (gdb_stdlog, "0x%02x ", buf[i]); |
c906108c | 336 | } |
3d468296 | 337 | |
1385f66c | 338 | fputs_unfiltered ("\n", gdb_stdlog); |
c906108c SS |
339 | } |
340 | ||
c906108c SS |
341 | /* Initialize gdb_callback. */ |
342 | ||
343 | static void | |
fba45db2 | 344 | init_callbacks (void) |
c906108c | 345 | { |
c5aa993b | 346 | if (!callbacks_initialized) |
c906108c SS |
347 | { |
348 | gdb_callback = default_callback; | |
349 | gdb_callback.init (&gdb_callback); | |
350 | gdb_callback.write_stdout = gdb_os_write_stdout; | |
351 | gdb_callback.flush_stdout = gdb_os_flush_stdout; | |
352 | gdb_callback.write_stderr = gdb_os_write_stderr; | |
353 | gdb_callback.flush_stderr = gdb_os_flush_stderr; | |
354 | gdb_callback.printf_filtered = gdb_os_printf_filtered; | |
355 | gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered; | |
356 | gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered; | |
357 | gdb_callback.error = gdb_os_error; | |
358 | gdb_callback.poll_quit = gdb_os_poll_quit; | |
359 | gdb_callback.magic = HOST_CALLBACK_MAGIC; | |
360 | callbacks_initialized = 1; | |
361 | } | |
362 | } | |
363 | ||
364 | /* Release callbacks (free resources used by them). */ | |
365 | ||
366 | static void | |
fba45db2 | 367 | end_callbacks (void) |
c906108c SS |
368 | { |
369 | if (callbacks_initialized) | |
370 | { | |
371 | gdb_callback.shutdown (&gdb_callback); | |
372 | callbacks_initialized = 0; | |
373 | } | |
374 | } | |
375 | ||
376 | /* GDB version of os_write_stdout callback. */ | |
377 | ||
c5aa993b | 378 | static int |
fba45db2 | 379 | gdb_os_write_stdout (host_callback *p, const char *buf, int len) |
c906108c SS |
380 | { |
381 | int i; | |
382 | char b[2]; | |
383 | ||
d9fcf2fb | 384 | ui_file_write (gdb_stdtarg, buf, len); |
c906108c SS |
385 | return len; |
386 | } | |
387 | ||
388 | /* GDB version of os_flush_stdout callback. */ | |
389 | ||
390 | static void | |
fba45db2 | 391 | gdb_os_flush_stdout (host_callback *p) |
c906108c | 392 | { |
4ce44c66 | 393 | gdb_flush (gdb_stdtarg); |
c906108c SS |
394 | } |
395 | ||
396 | /* GDB version of os_write_stderr callback. */ | |
397 | ||
c5aa993b | 398 | static int |
fba45db2 | 399 | gdb_os_write_stderr (host_callback *p, const char *buf, int len) |
c906108c SS |
400 | { |
401 | int i; | |
402 | char b[2]; | |
403 | ||
c5aa993b | 404 | for (i = 0; i < len; i++) |
c906108c SS |
405 | { |
406 | b[0] = buf[i]; | |
407 | b[1] = 0; | |
22e8e3c7 | 408 | fputs_unfiltered (b, gdb_stdtargerr); |
c906108c SS |
409 | } |
410 | return len; | |
411 | } | |
412 | ||
413 | /* GDB version of os_flush_stderr callback. */ | |
414 | ||
415 | static void | |
fba45db2 | 416 | gdb_os_flush_stderr (host_callback *p) |
c906108c | 417 | { |
22e8e3c7 | 418 | gdb_flush (gdb_stdtargerr); |
c906108c SS |
419 | } |
420 | ||
421 | /* GDB version of printf_filtered callback. */ | |
422 | ||
7104e59b SM |
423 | static void ATTRIBUTE_PRINTF (2, 3) |
424 | gdb_os_printf_filtered (host_callback * p, const char *format, ...) | |
c906108c SS |
425 | { |
426 | va_list args; | |
c906108c | 427 | |
123f5f96 | 428 | va_start (args, format); |
c906108c | 429 | vfprintf_filtered (gdb_stdout, format, args); |
c906108c SS |
430 | va_end (args); |
431 | } | |
432 | ||
433 | /* GDB version of error vprintf_filtered. */ | |
434 | ||
7104e59b | 435 | static void ATTRIBUTE_PRINTF (2, 0) |
c5aa993b | 436 | gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
437 | { |
438 | vfprintf_filtered (gdb_stdout, format, ap); | |
439 | } | |
440 | ||
441 | /* GDB version of error evprintf_filtered. */ | |
442 | ||
7104e59b | 443 | static void ATTRIBUTE_PRINTF (2, 0) |
c5aa993b | 444 | gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
445 | { |
446 | vfprintf_filtered (gdb_stderr, format, ap); | |
447 | } | |
448 | ||
449 | /* GDB version of error callback. */ | |
450 | ||
7104e59b | 451 | static void ATTRIBUTE_PRINTF (2, 3) |
58d4abe1 | 452 | gdb_os_error (host_callback * p, const char *format, ...) |
c906108c | 453 | { |
58d4abe1 | 454 | va_list args; |
123f5f96 | 455 | |
58d4abe1 PA |
456 | va_start (args, format); |
457 | verror (format, args); | |
458 | va_end (args); | |
c906108c SS |
459 | } |
460 | ||
8238d0bf | 461 | int |
e7faf938 | 462 | one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum) |
8238d0bf AC |
463 | { |
464 | /* Only makes sense to supply raw registers. */ | |
e7faf938 | 465 | gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)); |
8238d0bf AC |
466 | return regnum; |
467 | } | |
468 | ||
f6ac5f3d PA |
469 | void |
470 | gdbsim_target::fetch_registers (struct regcache *regcache, int regno) | |
c906108c | 471 | { |
ac7936df | 472 | struct gdbarch *gdbarch = regcache->arch (); |
bcc0c096 | 473 | struct inferior *inf = find_inferior_ptid (regcache_get_ptid (regcache)); |
db04efe6 | 474 | struct sim_inferior_data *sim_data |
bcc0c096 | 475 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); |
123f5f96 | 476 | |
c5aa993b | 477 | if (regno == -1) |
c906108c | 478 | { |
40a6adc1 | 479 | for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++) |
f6ac5f3d | 480 | fetch_registers (regcache, regno); |
8238d0bf | 481 | return; |
c906108c | 482 | } |
8238d0bf | 483 | |
40a6adc1 | 484 | switch (gdbarch_register_sim_regno (gdbarch, regno)) |
c906108c | 485 | { |
8238d0bf AC |
486 | case LEGACY_SIM_REGNO_IGNORE: |
487 | break; | |
488 | case SIM_REGNO_DOES_NOT_EXIST: | |
489 | { | |
490 | /* For moment treat a `does not exist' register the same way | |
74228e77 | 491 | as an ``unavailable'' register. */ |
a4d1e79a | 492 | regcache->raw_supply_zeroed (regno); |
8238d0bf AC |
493 | break; |
494 | } | |
74228e77 | 495 | |
8238d0bf AC |
496 | default: |
497 | { | |
498 | static int warn_user = 1; | |
a4d1e79a AH |
499 | int regsize = register_size (gdbarch, regno); |
500 | gdb::byte_vector buf (regsize, 0); | |
8238d0bf | 501 | int nr_bytes; |
123f5f96 | 502 | |
40a6adc1 | 503 | gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch)); |
db04efe6 | 504 | nr_bytes = sim_fetch_register (sim_data->gdbsim_desc, |
474c1661 | 505 | gdbarch_register_sim_regno |
40a6adc1 | 506 | (gdbarch, regno), |
a4d1e79a AH |
507 | buf.data (), regsize); |
508 | if (nr_bytes > 0 && nr_bytes != regsize && warn_user) | |
8238d0bf AC |
509 | { |
510 | fprintf_unfiltered (gdb_stderr, | |
0df8b418 MS |
511 | "Size of register %s (%d/%d) " |
512 | "incorrect (%d instead of %d))", | |
40a6adc1 | 513 | gdbarch_register_name (gdbarch, regno), |
474c1661 | 514 | regno, |
a4d1e79a AH |
515 | gdbarch_register_sim_regno (gdbarch, regno), |
516 | nr_bytes, regsize); | |
8238d0bf AC |
517 | warn_user = 0; |
518 | } | |
519 | /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' | |
278a7cf7 | 520 | indicating that GDB and the SIM have different ideas about |
8238d0bf AC |
521 | which registers are fetchable. */ |
522 | /* Else if (nr_bytes < 0): an old simulator, that doesn't | |
523 | think to return the register size. Just assume all is ok. */ | |
a4d1e79a | 524 | regcache->raw_supply (regno, buf.data ()); |
ea35711c | 525 | if (remote_debug) |
8238d0bf | 526 | { |
1385f66c PA |
527 | fprintf_unfiltered (gdb_stdlog, |
528 | "gdbsim_fetch_register: %d", regno); | |
8238d0bf | 529 | /* FIXME: We could print something more intelligible. */ |
a4d1e79a | 530 | dump_mem (buf.data (), regsize); |
8238d0bf AC |
531 | } |
532 | break; | |
533 | } | |
c906108c SS |
534 | } |
535 | } | |
536 | ||
537 | ||
f6ac5f3d PA |
538 | void |
539 | gdbsim_target::store_registers (struct regcache *regcache, int regno) | |
c906108c | 540 | { |
ac7936df | 541 | struct gdbarch *gdbarch = regcache->arch (); |
bcc0c096 | 542 | struct inferior *inf = find_inferior_ptid (regcache_get_ptid (regcache)); |
db04efe6 | 543 | struct sim_inferior_data *sim_data |
bcc0c096 | 544 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); |
db04efe6 | 545 | |
c5aa993b | 546 | if (regno == -1) |
c906108c | 547 | { |
40a6adc1 | 548 | for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++) |
f6ac5f3d | 549 | store_registers (regcache, regno); |
8238d0bf | 550 | return; |
c906108c | 551 | } |
40a6adc1 | 552 | else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0) |
c906108c | 553 | { |
a4d1e79a AH |
554 | int regsize = register_size (gdbarch, regno); |
555 | gdb::byte_vector tmp (regsize); | |
c906108c | 556 | int nr_bytes; |
123f5f96 | 557 | |
a4d1e79a | 558 | regcache->cooked_read (regno, tmp.data ()); |
db04efe6 | 559 | nr_bytes = sim_store_register (sim_data->gdbsim_desc, |
474c1661 | 560 | gdbarch_register_sim_regno |
40a6adc1 | 561 | (gdbarch, regno), |
a4d1e79a AH |
562 | tmp.data (), regsize); |
563 | ||
564 | if (nr_bytes > 0 && nr_bytes != regsize) | |
8e65ff28 | 565 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 566 | _("Register size different to expected")); |
dae477fe | 567 | if (nr_bytes < 0) |
74228e77 RM |
568 | internal_error (__FILE__, __LINE__, |
569 | _("Register %d not updated"), regno); | |
dae477fe | 570 | if (nr_bytes == 0) |
74228e77 RM |
571 | warning (_("Register %s not updated"), |
572 | gdbarch_register_name (gdbarch, regno)); | |
dae477fe | 573 | |
ea35711c | 574 | if (remote_debug) |
c906108c | 575 | { |
1385f66c | 576 | fprintf_unfiltered (gdb_stdlog, "gdbsim_store_register: %d", regno); |
c906108c | 577 | /* FIXME: We could print something more intelligible. */ |
a4d1e79a | 578 | dump_mem (tmp.data (), regsize); |
c906108c SS |
579 | } |
580 | } | |
581 | } | |
582 | ||
583 | /* Kill the running program. This may involve closing any open files | |
584 | and releasing other resources acquired by the simulated program. */ | |
585 | ||
f6ac5f3d PA |
586 | void |
587 | gdbsim_target::kill () | |
c906108c | 588 | { |
ea35711c | 589 | if (remote_debug) |
1385f66c | 590 | fprintf_unfiltered (gdb_stdlog, "gdbsim_kill\n"); |
c906108c SS |
591 | |
592 | /* There is no need to `kill' running simulator - the simulator is | |
52bb452f | 593 | not running. Mourning it is enough. */ |
bc1e6c81 | 594 | target_mourn_inferior (inferior_ptid); |
c906108c SS |
595 | } |
596 | ||
597 | /* Load an executable file into the target process. This is expected to | |
598 | not only bring new code into the target process, but also to update | |
599 | GDB's symbol tables to match. */ | |
600 | ||
f6ac5f3d PA |
601 | void |
602 | gdbsim_target::load (const char *args, int fromtty) | |
c906108c | 603 | { |
b2b255bd | 604 | const char *prog; |
db04efe6 KB |
605 | struct sim_inferior_data *sim_data |
606 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); | |
1986bccd | 607 | |
d1a41061 PP |
608 | if (args == NULL) |
609 | error_no_arg (_("program to load")); | |
1986bccd | 610 | |
773a1edc | 611 | gdb_argv argv (args); |
1986bccd AS |
612 | |
613 | prog = tilde_expand (argv[0]); | |
614 | ||
615 | if (argv[1] != NULL) | |
616 | error (_("GDB sim does not yet support a load offset.")); | |
617 | ||
ea35711c | 618 | if (remote_debug) |
1385f66c | 619 | fprintf_unfiltered (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog); |
c906108c | 620 | |
c906108c SS |
621 | /* FIXME: We will print two messages on error. |
622 | Need error to either not print anything if passed NULL or need | |
623 | another routine that doesn't take any arguments. */ | |
db04efe6 | 624 | if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL) |
8a3fe4f8 | 625 | error (_("unable to load program")); |
c906108c SS |
626 | |
627 | /* FIXME: If a load command should reset the targets registers then | |
0df8b418 | 628 | a call to sim_create_inferior() should go here. */ |
c906108c | 629 | |
db04efe6 | 630 | sim_data->program_loaded = 1; |
c906108c SS |
631 | } |
632 | ||
633 | ||
39f77062 | 634 | /* Start an inferior process and set inferior_ptid to its pid. |
c906108c SS |
635 | EXEC_FILE is the file to run. |
636 | ARGS is a string containing the arguments to the program. | |
637 | ENV is the environment vector to pass. Errors reported with error(). | |
638 | On VxWorks and various standalone systems, we ignore exec_file. */ | |
639 | /* This is called not only when we first attach, but also when the | |
640 | user types "run" after having attached. */ | |
641 | ||
f6ac5f3d PA |
642 | void |
643 | gdbsim_target::create_inferior (const char *exec_file, | |
644 | const std::string &allargs, | |
645 | char **env, int from_tty) | |
c906108c | 646 | { |
db04efe6 KB |
647 | struct sim_inferior_data *sim_data |
648 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); | |
c906108c | 649 | int len; |
773a1edc | 650 | char *arg_buf; |
7c5ded6a | 651 | const char *args = allargs.c_str (); |
c906108c SS |
652 | |
653 | if (exec_file == 0 || exec_bfd == 0) | |
8a3fe4f8 | 654 | warning (_("No executable file specified.")); |
db04efe6 | 655 | if (!sim_data->program_loaded) |
8a3fe4f8 | 656 | warning (_("No program loaded.")); |
c906108c | 657 | |
ea35711c | 658 | if (remote_debug) |
1385f66c PA |
659 | fprintf_unfiltered (gdb_stdlog, |
660 | "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", | |
661 | (exec_file ? exec_file : "(NULL)"), | |
662 | args); | |
c906108c | 663 | |
db04efe6 | 664 | if (ptid_equal (inferior_ptid, sim_data->remote_sim_ptid)) |
f6ac5f3d | 665 | kill (); |
c906108c SS |
666 | remove_breakpoints (); |
667 | init_wait_for_inferior (); | |
668 | ||
773a1edc | 669 | gdb_argv built_argv; |
c906108c SS |
670 | if (exec_file != NULL) |
671 | { | |
7c5ded6a | 672 | len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10; |
c906108c SS |
673 | arg_buf = (char *) alloca (len); |
674 | arg_buf[0] = '\0'; | |
675 | strcat (arg_buf, exec_file); | |
676 | strcat (arg_buf, " "); | |
677 | strcat (arg_buf, args); | |
773a1edc | 678 | built_argv.reset (arg_buf); |
c906108c | 679 | } |
ddd60447 | 680 | |
2a7c8fd5 AB |
681 | if (!have_inferiors ()) |
682 | init_thread_list (); | |
683 | ||
773a1edc TT |
684 | if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd, |
685 | built_argv.get (), env) | |
ddd60447 MF |
686 | != SIM_RC_OK) |
687 | error (_("Unable to create sim inferior.")); | |
c906108c | 688 | |
db04efe6 | 689 | inferior_ptid = sim_data->remote_sim_ptid; |
20176d8f | 690 | inferior_appeared (current_inferior (), ptid_get_pid (inferior_ptid)); |
9de2bdd7 PA |
691 | add_thread_silent (inferior_ptid); |
692 | ||
0df8b418 MS |
693 | insert_breakpoints (); /* Needed to get correct instruction |
694 | in cache. */ | |
c906108c | 695 | |
88056fbb | 696 | clear_proceed_status (0); |
c906108c SS |
697 | } |
698 | ||
699 | /* The open routine takes the rest of the parameters from the command, | |
700 | and (if successful) pushes a new target onto the stack. | |
701 | Targets should supply this routine, if only to provide an error message. */ | |
0df8b418 | 702 | /* Called when selecting the simulator. E.g. (gdb) target sim name. */ |
c906108c | 703 | |
f6ac5f3d PA |
704 | void |
705 | gdbsim_target::open (const char *args, int from_tty) | |
c906108c SS |
706 | { |
707 | int len; | |
708 | char *arg_buf; | |
db04efe6 | 709 | struct sim_inferior_data *sim_data; |
87d1b309 | 710 | const char *sysroot; |
db04efe6 | 711 | SIM_DESC gdbsim_desc; |
c906108c | 712 | |
87d1b309 MF |
713 | sysroot = gdb_sysroot; |
714 | if (is_target_filename (sysroot)) | |
715 | sysroot += strlen (TARGET_SYSROOT_PREFIX); | |
716 | ||
ea35711c | 717 | if (remote_debug) |
1385f66c PA |
718 | fprintf_unfiltered (gdb_stdlog, |
719 | "gdbsim_open: args \"%s\"\n", args ? args : "(null)"); | |
c906108c | 720 | |
db04efe6 KB |
721 | /* Ensure that the sim target is not on the target stack. This is |
722 | necessary, because if it is on the target stack, the call to | |
723 | push_target below will invoke sim_close(), thus freeing various | |
724 | state (including a sim instance) that we allocate prior to | |
725 | invoking push_target(). We want to delay the push_target() | |
726 | operation until after we complete those operations which could | |
727 | error out. */ | |
728 | if (gdbsim_is_open) | |
c906108c SS |
729 | unpush_target (&gdbsim_ops); |
730 | ||
c5aa993b | 731 | len = (7 + 1 /* gdbsim */ |
c906108c SS |
732 | + strlen (" -E little") |
733 | + strlen (" --architecture=xxxxxxxxxx") | |
23bf70af | 734 | + strlen (" --sysroot=") + strlen (sysroot) + |
c906108c | 735 | + (args ? strlen (args) : 0) |
c5aa993b | 736 | + 50) /* slack */ ; |
c906108c | 737 | arg_buf = (char *) alloca (len); |
c5aa993b | 738 | strcpy (arg_buf, "gdbsim"); /* 7 */ |
b6d373df | 739 | /* Specify the byte order for the target when it is explicitly |
0df8b418 | 740 | specified by the user (not auto detected). */ |
b6d373df | 741 | switch (selected_byte_order ()) |
c906108c | 742 | { |
a8cf2722 AC |
743 | case BFD_ENDIAN_BIG: |
744 | strcat (arg_buf, " -E big"); | |
745 | break; | |
746 | case BFD_ENDIAN_LITTLE: | |
747 | strcat (arg_buf, " -E little"); | |
748 | break; | |
749 | case BFD_ENDIAN_UNKNOWN: | |
750 | break; | |
c906108c SS |
751 | } |
752 | /* Specify the architecture of the target when it has been | |
753 | explicitly specified */ | |
a8cf2722 | 754 | if (selected_architecture_name () != NULL) |
c906108c SS |
755 | { |
756 | strcat (arg_buf, " --architecture="); | |
a8cf2722 | 757 | strcat (arg_buf, selected_architecture_name ()); |
c906108c | 758 | } |
f4b8c29b MF |
759 | /* Pass along gdb's concept of the sysroot. */ |
760 | strcat (arg_buf, " --sysroot="); | |
23bf70af | 761 | strcat (arg_buf, sysroot); |
c906108c SS |
762 | /* finally, any explicit args */ |
763 | if (args) | |
764 | { | |
c5aa993b | 765 | strcat (arg_buf, " "); /* 1 */ |
c906108c SS |
766 | strcat (arg_buf, args); |
767 | } | |
773a1edc | 768 | |
74cbb09e SM |
769 | gdb_argv argv (arg_buf); |
770 | sim_argv = argv.get (); | |
c906108c SS |
771 | |
772 | init_callbacks (); | |
db04efe6 | 773 | gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv); |
c906108c SS |
774 | |
775 | if (gdbsim_desc == 0) | |
db04efe6 | 776 | { |
db04efe6 KB |
777 | sim_argv = NULL; |
778 | error (_("unable to create simulator instance")); | |
779 | } | |
780 | ||
74cbb09e | 781 | argv.release (); |
773a1edc | 782 | |
db04efe6 KB |
783 | /* Reset the pid numberings for this batch of sim instances. */ |
784 | next_pid = INITIAL_PID; | |
785 | ||
786 | /* Allocate the inferior data, but do not allocate a sim instance | |
787 | since we've already just done that. */ | |
0df8b418 MS |
788 | sim_data = get_sim_inferior_data (current_inferior (), |
789 | SIM_INSTANCE_NOT_NEEDED); | |
db04efe6 KB |
790 | |
791 | sim_data->gdbsim_desc = gdbsim_desc; | |
c906108c SS |
792 | |
793 | push_target (&gdbsim_ops); | |
c906108c | 794 | printf_filtered ("Connected to the simulator.\n"); |
52bb452f DJ |
795 | |
796 | /* There's nothing running after "target sim" or "load"; not until | |
797 | "run". */ | |
798 | inferior_ptid = null_ptid; | |
db04efe6 KB |
799 | |
800 | gdbsim_is_open = 1; | |
801 | } | |
802 | ||
803 | /* Callback for iterate_over_inferiors. Called (indirectly) by | |
804 | gdbsim_close(). */ | |
805 | ||
806 | static int | |
807 | gdbsim_close_inferior (struct inferior *inf, void *arg) | |
808 | { | |
653090d3 SM |
809 | struct sim_inferior_data *sim_data |
810 | = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key); | |
db04efe6 KB |
811 | if (sim_data != NULL) |
812 | { | |
813 | ptid_t ptid = sim_data->remote_sim_ptid; | |
814 | ||
815 | sim_inferior_data_cleanup (inf, sim_data); | |
816 | set_inferior_data (inf, sim_inferior_data_key, NULL); | |
817 | ||
818 | /* Having a ptid allocated and stored in remote_sim_ptid does | |
0df8b418 | 819 | not mean that a corresponding inferior was ever created. |
db04efe6 KB |
820 | Thus we need to verify the existence of an inferior using the |
821 | pid in question before setting inferior_ptid via | |
822 | switch_to_thread() or mourning the inferior. */ | |
c9657e70 | 823 | if (find_inferior_ptid (ptid) != NULL) |
db04efe6 KB |
824 | { |
825 | switch_to_thread (ptid); | |
826 | generic_mourn_inferior (); | |
827 | } | |
828 | } | |
829 | ||
830 | return 0; | |
c906108c SS |
831 | } |
832 | ||
0df8b418 | 833 | /* Close out all files and local state before this target loses control. */ |
c906108c | 834 | |
f6ac5f3d PA |
835 | void |
836 | gdbsim_target::close () | |
c906108c | 837 | { |
db04efe6 KB |
838 | struct sim_inferior_data *sim_data |
839 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); | |
840 | ||
ea35711c | 841 | if (remote_debug) |
1385f66c | 842 | fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n"); |
c906108c | 843 | |
db04efe6 | 844 | iterate_over_inferiors (gdbsim_close_inferior, NULL); |
c906108c | 845 | |
db04efe6 | 846 | if (sim_argv != NULL) |
c906108c | 847 | { |
db04efe6 KB |
848 | freeargv (sim_argv); |
849 | sim_argv = NULL; | |
c906108c SS |
850 | } |
851 | ||
852 | end_callbacks (); | |
db04efe6 KB |
853 | |
854 | gdbsim_is_open = 0; | |
c906108c SS |
855 | } |
856 | ||
857 | /* Takes a program previously attached to and detaches it. | |
858 | The program may resume execution (some targets do, some don't) and will | |
859 | no longer stop on signals, etc. We better not have left any breakpoints | |
6bd6f3b6 SM |
860 | in the program or it'll die when it hits one. FROM_TTY says whether to be |
861 | verbose or not. */ | |
c906108c SS |
862 | /* Terminate the open connection to the remote debugger. |
863 | Use this when you want to detach and do something else with your gdb. */ | |
864 | ||
f6ac5f3d PA |
865 | void |
866 | gdbsim_target::detach (inferior *inf, int from_tty) | |
c906108c | 867 | { |
ea35711c | 868 | if (remote_debug) |
6bd6f3b6 | 869 | fprintf_unfiltered (gdb_stdlog, "gdbsim_detach\n"); |
c906108c | 870 | |
f6ac5f3d | 871 | unpush_target (this); /* calls gdbsim_close to do the real work */ |
c906108c SS |
872 | if (from_tty) |
873 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); | |
874 | } | |
c5aa993b | 875 | |
c906108c SS |
876 | /* Resume execution of the target process. STEP says whether to single-step |
877 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given | |
878 | to the target, or zero for no signal. */ | |
879 | ||
db04efe6 KB |
880 | struct resume_data |
881 | { | |
2ea28649 | 882 | enum gdb_signal siggnal; |
db04efe6 KB |
883 | int step; |
884 | }; | |
885 | ||
886 | static int | |
887 | gdbsim_resume_inferior (struct inferior *inf, void *arg) | |
888 | { | |
889 | struct sim_inferior_data *sim_data | |
890 | = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED); | |
653090d3 | 891 | struct resume_data *rd = (struct resume_data *) arg; |
db04efe6 KB |
892 | |
893 | if (sim_data) | |
894 | { | |
895 | sim_data->resume_siggnal = rd->siggnal; | |
896 | sim_data->resume_step = rd->step; | |
897 | ||
898 | if (remote_debug) | |
1385f66c PA |
899 | fprintf_unfiltered (gdb_stdlog, |
900 | _("gdbsim_resume: pid %d, step %d, signal %d\n"), | |
901 | inf->pid, rd->step, rd->siggnal); | |
db04efe6 KB |
902 | } |
903 | ||
904 | /* When called from iterate_over_inferiors, a zero return causes the | |
905 | iteration process to proceed until there are no more inferiors to | |
906 | consider. */ | |
907 | return 0; | |
908 | } | |
c906108c | 909 | |
f6ac5f3d PA |
910 | void |
911 | gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal) | |
c906108c | 912 | { |
db04efe6 KB |
913 | struct resume_data rd; |
914 | struct sim_inferior_data *sim_data | |
915 | = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED); | |
916 | ||
917 | rd.siggnal = siggnal; | |
918 | rd.step = step; | |
919 | ||
0df8b418 | 920 | /* We don't access any sim_data members within this function. |
db04efe6 KB |
921 | What's of interest is whether or not the call to |
922 | get_sim_inferior_data_by_ptid(), above, is able to obtain a | |
923 | non-NULL pointer. If it managed to obtain a non-NULL pointer, we | |
924 | know we have a single inferior to consider. If it's NULL, we | |
925 | either have multiple inferiors to resume or an error condition. */ | |
926 | ||
927 | if (sim_data) | |
c9657e70 | 928 | gdbsim_resume_inferior (find_inferior_ptid (ptid), &rd); |
db04efe6 KB |
929 | else if (ptid_equal (ptid, minus_one_ptid)) |
930 | iterate_over_inferiors (gdbsim_resume_inferior, &rd); | |
931 | else | |
8a3fe4f8 | 932 | error (_("The program is not being run.")); |
c906108c SS |
933 | } |
934 | ||
bfedc46a | 935 | /* Notify the simulator of an asynchronous request to interrupt. |
c5aa993b | 936 | |
bfedc46a | 937 | The simulator shall ensure that the interrupt request is eventually |
c906108c | 938 | delivered to the simulator. If the call is made while the |
bfedc46a | 939 | simulator is not running then the interrupt request is processed when |
c906108c SS |
940 | the simulator is next resumed. |
941 | ||
db04efe6 KB |
942 | For simulators that do not support this operation, just abort. */ |
943 | ||
944 | static int | |
bfedc46a | 945 | gdbsim_interrupt_inferior (struct inferior *inf, void *arg) |
db04efe6 KB |
946 | { |
947 | struct sim_inferior_data *sim_data | |
948 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); | |
949 | ||
950 | if (sim_data) | |
951 | { | |
952 | if (!sim_stop (sim_data->gdbsim_desc)) | |
953 | { | |
954 | quit (); | |
955 | } | |
956 | } | |
957 | ||
958 | /* When called from iterate_over_inferiors, a zero return causes the | |
959 | iteration process to proceed until there are no more inferiors to | |
960 | consider. */ | |
961 | return 0; | |
962 | } | |
c906108c | 963 | |
f6ac5f3d PA |
964 | void |
965 | gdbsim_target::interrupt () | |
c906108c | 966 | { |
f6cfb427 | 967 | iterate_over_inferiors (gdbsim_interrupt_inferior, NULL); |
c906108c SS |
968 | } |
969 | ||
970 | /* GDB version of os_poll_quit callback. | |
8a0ce09a | 971 | Taken from gdb/util.c - should be in a library. */ |
c906108c SS |
972 | |
973 | static int | |
fba45db2 | 974 | gdb_os_poll_quit (host_callback *p) |
c906108c | 975 | { |
98bbd631 AC |
976 | if (deprecated_ui_loop_hook != NULL) |
977 | deprecated_ui_loop_hook (0); | |
7a292a7a | 978 | |
522002f9 | 979 | if (check_quit_flag ()) /* gdb's idea of quit */ |
abf009ef | 980 | return 1; |
c906108c SS |
981 | return 0; |
982 | } | |
983 | ||
984 | /* Wait for inferior process to do something. Return pid of child, | |
985 | or -1 in case of error; store status through argument pointer STATUS, | |
0df8b418 | 986 | just as `wait' would. */ |
c906108c SS |
987 | |
988 | static void | |
fba45db2 | 989 | gdbsim_cntrl_c (int signo) |
c906108c | 990 | { |
f6ac5f3d | 991 | gdbsim_ops.interrupt (); |
c906108c SS |
992 | } |
993 | ||
f6ac5f3d PA |
994 | ptid_t |
995 | gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status, int options) | |
c906108c | 996 | { |
db04efe6 | 997 | struct sim_inferior_data *sim_data; |
a40805d4 | 998 | static sighandler_t prev_sigint; |
c906108c SS |
999 | int sigrc = 0; |
1000 | enum sim_stop reason = sim_running; | |
1001 | ||
db04efe6 KB |
1002 | /* This target isn't able to (yet) resume more than one inferior at a time. |
1003 | When ptid is minus_one_ptid, just use the current inferior. If we're | |
1004 | given an explicit pid, we'll try to find it and use that instead. */ | |
1005 | if (ptid_equal (ptid, minus_one_ptid)) | |
0df8b418 MS |
1006 | sim_data = get_sim_inferior_data (current_inferior (), |
1007 | SIM_INSTANCE_NEEDED); | |
db04efe6 KB |
1008 | else |
1009 | { | |
1010 | sim_data = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED); | |
1011 | if (sim_data == NULL) | |
1012 | error (_("Unable to wait for pid %d. Inferior not found."), | |
1013 | ptid_get_pid (ptid)); | |
1014 | inferior_ptid = ptid; | |
1015 | } | |
1016 | ||
ea35711c | 1017 | if (remote_debug) |
1385f66c | 1018 | fprintf_unfiltered (gdb_stdlog, "gdbsim_wait\n"); |
c906108c SS |
1019 | |
1020 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
1021 | { | |
1022 | struct sigaction sa, osa; | |
1023 | sa.sa_handler = gdbsim_cntrl_c; | |
1024 | sigemptyset (&sa.sa_mask); | |
1025 | sa.sa_flags = 0; | |
1026 | sigaction (SIGINT, &sa, &osa); | |
1027 | prev_sigint = osa.sa_handler; | |
1028 | } | |
1029 | #else | |
1030 | prev_sigint = signal (SIGINT, gdbsim_cntrl_c); | |
1031 | #endif | |
db04efe6 | 1032 | sim_resume (sim_data->gdbsim_desc, sim_data->resume_step, |
74228e77 | 1033 | sim_data->resume_siggnal); |
db04efe6 | 1034 | |
c906108c | 1035 | signal (SIGINT, prev_sigint); |
db04efe6 | 1036 | sim_data->resume_step = 0; |
c906108c | 1037 | |
db04efe6 | 1038 | sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc); |
c906108c SS |
1039 | |
1040 | switch (reason) | |
1041 | { | |
1042 | case sim_exited: | |
1043 | status->kind = TARGET_WAITKIND_EXITED; | |
1044 | status->value.integer = sigrc; | |
1045 | break; | |
1046 | case sim_stopped: | |
1047 | switch (sigrc) | |
1048 | { | |
a493e3e2 | 1049 | case GDB_SIGNAL_ABRT: |
c906108c SS |
1050 | quit (); |
1051 | break; | |
a493e3e2 PA |
1052 | case GDB_SIGNAL_INT: |
1053 | case GDB_SIGNAL_TRAP: | |
c906108c SS |
1054 | default: |
1055 | status->kind = TARGET_WAITKIND_STOPPED; | |
653090d3 | 1056 | status->value.sig = (enum gdb_signal) sigrc; |
c906108c SS |
1057 | break; |
1058 | } | |
1059 | break; | |
1060 | case sim_signalled: | |
1061 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
653090d3 | 1062 | status->value.sig = (enum gdb_signal) sigrc; |
c906108c SS |
1063 | break; |
1064 | case sim_running: | |
1065 | case sim_polling: | |
0df8b418 | 1066 | /* FIXME: Is this correct? */ |
c906108c SS |
1067 | break; |
1068 | } | |
1069 | ||
39f77062 | 1070 | return inferior_ptid; |
c906108c SS |
1071 | } |
1072 | ||
1073 | /* Get ready to modify the registers array. On machines which store | |
1074 | individual registers, this doesn't need to do anything. On machines | |
1075 | which store all the registers in one fell swoop, this makes sure | |
1076 | that registers contains all the registers from the program being | |
1077 | debugged. */ | |
1078 | ||
f6ac5f3d PA |
1079 | void |
1080 | gdbsim_target::prepare_to_store (struct regcache *regcache) | |
c906108c | 1081 | { |
0df8b418 | 1082 | /* Do nothing, since we can store individual regs. */ |
c906108c SS |
1083 | } |
1084 | ||
146ec4db PA |
1085 | /* Helper for gdbsim_xfer_partial that handles memory transfers. |
1086 | Arguments are like target_xfer_partial. */ | |
d93bce06 | 1087 | |
9b409511 | 1088 | static enum target_xfer_status |
146ec4db PA |
1089 | gdbsim_xfer_memory (struct target_ops *target, |
1090 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
9b409511 | 1091 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) |
c906108c | 1092 | { |
db04efe6 KB |
1093 | struct sim_inferior_data *sim_data |
1094 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); | |
b55e14c7 | 1095 | int l; |
db04efe6 | 1096 | |
0c012db1 KB |
1097 | /* If this target doesn't have memory yet, return 0 causing the |
1098 | request to be passed to a lower target, hopefully an exec | |
1099 | file. */ | |
f6ac5f3d | 1100 | if (!target->has_memory ()) |
9b409511 | 1101 | return TARGET_XFER_EOF; |
52bb452f | 1102 | |
db04efe6 | 1103 | if (!sim_data->program_loaded) |
8a3fe4f8 | 1104 | error (_("No program loaded.")); |
c906108c | 1105 | |
db04efe6 KB |
1106 | /* Note that we obtained the sim_data pointer above using |
1107 | SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly | |
1108 | allocate a sim instance prior to loading a program. If we | |
1109 | get to this point in the code though, gdbsim_desc should be | |
1110 | non-NULL. (Note that a sim instance is needed in order to load | |
1111 | the program...) */ | |
1112 | gdb_assert (sim_data->gdbsim_desc != NULL); | |
1113 | ||
ea35711c | 1114 | if (remote_debug) |
fcde0081 | 1115 | fprintf_unfiltered (gdb_stdlog, |
146ec4db PA |
1116 | "gdbsim_xfer_memory: readbuf %s, writebuf %s, " |
1117 | "memaddr %s, len %s\n", | |
1118 | host_address_to_string (readbuf), | |
1119 | host_address_to_string (writebuf), | |
fcde0081 | 1120 | paddress (target_gdbarch (), memaddr), |
b55e14c7 | 1121 | pulongest (len)); |
c906108c | 1122 | |
146ec4db | 1123 | if (writebuf) |
c906108c | 1124 | { |
fcde0081 | 1125 | if (remote_debug && len > 0) |
146ec4db | 1126 | dump_mem (writebuf, len); |
b55e14c7 | 1127 | l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len); |
c906108c | 1128 | } |
c5aa993b | 1129 | else |
c906108c | 1130 | { |
b55e14c7 | 1131 | l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len); |
ea35711c | 1132 | if (remote_debug && len > 0) |
146ec4db | 1133 | dump_mem (readbuf, len); |
c5aa993b | 1134 | } |
9b409511 YQ |
1135 | if (l > 0) |
1136 | { | |
1137 | *xfered_len = (ULONGEST) l; | |
1138 | return TARGET_XFER_OK; | |
1139 | } | |
1140 | else if (l == 0) | |
1141 | return TARGET_XFER_EOF; | |
1142 | else | |
1143 | return TARGET_XFER_E_IO; | |
c906108c SS |
1144 | } |
1145 | ||
146ec4db PA |
1146 | /* Target to_xfer_partial implementation. */ |
1147 | ||
f6ac5f3d PA |
1148 | enum target_xfer_status |
1149 | gdbsim_target::xfer_partial (enum target_object object, | |
1150 | const char *annex, gdb_byte *readbuf, | |
1151 | const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, | |
1152 | ULONGEST *xfered_len) | |
146ec4db PA |
1153 | { |
1154 | switch (object) | |
1155 | { | |
1156 | case TARGET_OBJECT_MEMORY: | |
f6ac5f3d | 1157 | return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len, |
9b409511 | 1158 | xfered_len); |
146ec4db PA |
1159 | |
1160 | default: | |
2ed4b548 | 1161 | return TARGET_XFER_E_IO; |
146ec4db PA |
1162 | } |
1163 | } | |
1164 | ||
f6ac5f3d PA |
1165 | void |
1166 | gdbsim_target::files_info () | |
c906108c | 1167 | { |
db04efe6 KB |
1168 | struct sim_inferior_data *sim_data |
1169 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); | |
0d18d720 | 1170 | const char *file = "nothing"; |
c906108c SS |
1171 | |
1172 | if (exec_bfd) | |
1173 | file = bfd_get_filename (exec_bfd); | |
1174 | ||
ea35711c | 1175 | if (remote_debug) |
1385f66c | 1176 | fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file); |
c906108c SS |
1177 | |
1178 | if (exec_bfd) | |
1179 | { | |
1385f66c PA |
1180 | fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n", |
1181 | target_shortname, file); | |
db04efe6 | 1182 | sim_info (sim_data->gdbsim_desc, 0); |
c906108c SS |
1183 | } |
1184 | } | |
1185 | ||
1186 | /* Clear the simulator's notion of what the break points are. */ | |
1187 | ||
f6ac5f3d PA |
1188 | void |
1189 | gdbsim_target::mourn_inferior () | |
c5aa993b | 1190 | { |
db04efe6 KB |
1191 | struct sim_inferior_data *sim_data |
1192 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); | |
1193 | ||
ea35711c | 1194 | if (remote_debug) |
1385f66c | 1195 | fprintf_unfiltered (gdb_stdlog, "gdbsim_mourn_inferior:\n"); |
c906108c SS |
1196 | |
1197 | remove_breakpoints (); | |
1198 | generic_mourn_inferior (); | |
db04efe6 | 1199 | delete_thread_silent (sim_data->remote_sim_ptid); |
c906108c SS |
1200 | } |
1201 | ||
c906108c SS |
1202 | /* Pass the command argument through to the simulator verbatim. The |
1203 | simulator must do any command interpretation work. */ | |
1204 | ||
1205 | void | |
0b39b52e | 1206 | simulator_command (const char *args, int from_tty) |
c906108c | 1207 | { |
db04efe6 KB |
1208 | struct sim_inferior_data *sim_data; |
1209 | ||
1210 | /* We use inferior_data() instead of get_sim_inferior_data() here in | |
1211 | order to avoid attaching a sim_inferior_data struct to an | |
1212 | inferior unnecessarily. The reason we take such care here is due | |
1213 | to the fact that this function, simulator_command(), may be called | |
1214 | even when the sim target is not active. If we were to use | |
1215 | get_sim_inferior_data() here, it is possible that this call would | |
1216 | be made either prior to gdbsim_open() or after gdbsim_close(), | |
1217 | thus allocating memory that would not be garbage collected until | |
1218 | the ultimate destruction of the associated inferior. */ | |
1219 | ||
653090d3 SM |
1220 | sim_data = ((struct sim_inferior_data *) |
1221 | inferior_data (current_inferior (), sim_inferior_data_key)); | |
db04efe6 | 1222 | if (sim_data == NULL || sim_data->gdbsim_desc == NULL) |
c906108c SS |
1223 | { |
1224 | ||
1225 | /* PREVIOUSLY: The user may give a command before the simulator | |
74228e77 RM |
1226 | is opened. [...] (??? assuming of course one wishes to |
1227 | continue to allow commands to be sent to unopened simulators, | |
1228 | which isn't entirely unreasonable). */ | |
c906108c SS |
1229 | |
1230 | /* The simulator is a builtin abstraction of a remote target. | |
74228e77 RM |
1231 | Consistent with that model, access to the simulator, via sim |
1232 | commands, is restricted to the period when the channel to the | |
1233 | simulator is open. */ | |
c906108c | 1234 | |
8a3fe4f8 | 1235 | error (_("Not connected to the simulator target")); |
c906108c SS |
1236 | } |
1237 | ||
db04efe6 | 1238 | sim_do_command (sim_data->gdbsim_desc, args); |
c906108c SS |
1239 | |
1240 | /* Invalidate the register cache, in case the simulator command does | |
0df8b418 | 1241 | something funny. */ |
c5aa993b | 1242 | registers_changed (); |
c906108c SS |
1243 | } |
1244 | ||
386535dd PA |
1245 | static void |
1246 | sim_command_completer (struct cmd_list_element *ignore, | |
1247 | completion_tracker &tracker, | |
1248 | const char *text, const char *word) | |
56a9aa1d MF |
1249 | { |
1250 | struct sim_inferior_data *sim_data; | |
1251 | ||
653090d3 SM |
1252 | sim_data = ((struct sim_inferior_data *) |
1253 | inferior_data (current_inferior (), sim_inferior_data_key)); | |
56a9aa1d | 1254 | if (sim_data == NULL || sim_data->gdbsim_desc == NULL) |
386535dd | 1255 | return; |
56a9aa1d | 1256 | |
386535dd PA |
1257 | /* sim_complete_command returns a NULL-terminated malloc'ed array of |
1258 | malloc'ed strings. */ | |
1259 | struct sim_completions_deleter | |
1260 | { | |
1261 | void operator() (char **ptr) const | |
1262 | { | |
1263 | for (size_t i = 0; ptr[i] != NULL; i++) | |
1264 | xfree (ptr[i]); | |
1265 | xfree (ptr); | |
1266 | } | |
1267 | }; | |
1268 | ||
1269 | std::unique_ptr<char *[], sim_completions_deleter> sim_completions | |
1270 | (sim_complete_command (sim_data->gdbsim_desc, text, word)); | |
1271 | if (sim_completions == NULL) | |
1272 | return; | |
34370865 | 1273 | |
386535dd PA |
1274 | /* Count the elements and add completions from tail to head because |
1275 | below we'll swap elements out of the array in case add_completion | |
1276 | throws and the deleter deletes until it finds a NULL element. */ | |
1277 | size_t count = 0; | |
1278 | while (sim_completions[count] != NULL) | |
1279 | count++; | |
34370865 | 1280 | |
386535dd PA |
1281 | for (size_t i = count; i > 0; i--) |
1282 | { | |
1283 | gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]); | |
1284 | sim_completions[i - 1] = NULL; | |
1285 | tracker.add_completion (std::move (match)); | |
1286 | } | |
56a9aa1d MF |
1287 | } |
1288 | ||
9de2bdd7 PA |
1289 | /* Check to see if a thread is still alive. */ |
1290 | ||
f6ac5f3d PA |
1291 | int |
1292 | gdbsim_target::thread_alive (ptid_t ptid) | |
9de2bdd7 | 1293 | { |
db04efe6 KB |
1294 | struct sim_inferior_data *sim_data |
1295 | = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED); | |
1296 | ||
1297 | if (sim_data == NULL) | |
1298 | return 0; | |
1299 | ||
1300 | if (ptid_equal (ptid, sim_data->remote_sim_ptid)) | |
9de2bdd7 PA |
1301 | /* The simulators' task is always alive. */ |
1302 | return 1; | |
1303 | ||
1304 | return 0; | |
1305 | } | |
1306 | ||
1307 | /* Convert a thread ID to a string. Returns the string in a static | |
1308 | buffer. */ | |
1309 | ||
f6ac5f3d PA |
1310 | const char * |
1311 | gdbsim_target::pid_to_str (ptid_t ptid) | |
9de2bdd7 | 1312 | { |
9de2bdd7 PA |
1313 | return normal_pid_to_str (ptid); |
1314 | } | |
1315 | ||
0c012db1 KB |
1316 | /* Simulator memory may be accessed after the program has been loaded. */ |
1317 | ||
f6ac5f3d PA |
1318 | int |
1319 | gdbsim_target::has_all_memory () | |
0c012db1 KB |
1320 | { |
1321 | struct sim_inferior_data *sim_data | |
1322 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); | |
1323 | ||
1324 | if (!sim_data->program_loaded) | |
1325 | return 0; | |
1326 | ||
1327 | return 1; | |
1328 | } | |
1329 | ||
f6ac5f3d PA |
1330 | int |
1331 | gdbsim_target::has_memory () | |
0c012db1 KB |
1332 | { |
1333 | struct sim_inferior_data *sim_data | |
1334 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); | |
1335 | ||
1336 | if (!sim_data->program_loaded) | |
1337 | return 0; | |
1338 | ||
1339 | return 1; | |
1340 | } | |
1341 | ||
c906108c | 1342 | void |
fba45db2 | 1343 | _initialize_remote_sim (void) |
c906108c | 1344 | { |
56a9aa1d MF |
1345 | struct cmd_list_element *c; |
1346 | ||
c906108c SS |
1347 | add_target (&gdbsim_ops); |
1348 | ||
56a9aa1d MF |
1349 | c = add_com ("sim", class_obscure, simulator_command, |
1350 | _("Send a command to the simulator.")); | |
1351 | set_cmd_completer (c, sim_command_completer); | |
9de2bdd7 | 1352 | |
db04efe6 | 1353 | sim_inferior_data_key |
c5d9b215 | 1354 | = register_inferior_data_with_cleanup (NULL, sim_inferior_data_cleanup); |
c906108c | 1355 | } |