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