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