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