]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Select target systems and architectures at runtime for GDB. |
7998dfc3 | 2 | |
3666a048 | 3 | Copyright (C) 1990-2021 Free Software Foundation, Inc. |
7998dfc3 | 4 | |
c906108c SS |
5 | Contributed by Cygnus Support. |
6 | ||
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
c906108c | 23 | #include "target.h" |
68c765e2 | 24 | #include "target-dcache.h" |
c906108c SS |
25 | #include "gdbcmd.h" |
26 | #include "symtab.h" | |
27 | #include "inferior.h" | |
45741a9c | 28 | #include "infrun.h" |
c906108c SS |
29 | #include "bfd.h" |
30 | #include "symfile.h" | |
31 | #include "objfiles.h" | |
4930751a | 32 | #include "dcache.h" |
c906108c | 33 | #include <signal.h> |
4e052eda | 34 | #include "regcache.h" |
b6591e8b | 35 | #include "gdbcore.h" |
424163ea | 36 | #include "target-descriptions.h" |
e1ac3328 | 37 | #include "gdbthread.h" |
b9db4ced | 38 | #include "solib.h" |
07b82ea5 | 39 | #include "exec.h" |
edb3359d | 40 | #include "inline-frame.h" |
2f4d8875 | 41 | #include "tracepoint.h" |
7313baad | 42 | #include "gdb/fileio.h" |
268a13a5 | 43 | #include "gdbsupport/agent.h" |
8de71aab | 44 | #include "auxv.h" |
a7068b60 | 45 | #include "target-debug.h" |
41fd2b0f PA |
46 | #include "top.h" |
47 | #include "event-top.h" | |
325fac50 | 48 | #include <algorithm> |
268a13a5 | 49 | #include "gdbsupport/byte-vector.h" |
4a72de73 | 50 | #include "gdbsupport/search.h" |
e671cd59 | 51 | #include "terminal.h" |
d9f719f1 | 52 | #include <unordered_map> |
121b3efd | 53 | #include "target-connection.h" |
670e35fa | 54 | #include "valprint.h" |
c906108c | 55 | |
f0f9ff95 TT |
56 | static void generic_tls_error (void) ATTRIBUTE_NORETURN; |
57 | ||
0a4f40a2 | 58 | static void default_terminal_info (struct target_ops *, const char *, int); |
c906108c | 59 | |
5009afc5 AS |
60 | static int default_watchpoint_addr_within_range (struct target_ops *, |
61 | CORE_ADDR, CORE_ADDR, int); | |
62 | ||
31568a15 TT |
63 | static int default_region_ok_for_hw_watchpoint (struct target_ops *, |
64 | CORE_ADDR, int); | |
e0d24f8d | 65 | |
a30bf1f1 | 66 | static void default_rcmd (struct target_ops *, const char *, struct ui_file *); |
a53f3625 | 67 | |
4229b31d TT |
68 | static ptid_t default_get_ada_task_ptid (struct target_ops *self, |
69 | long lwp, long tid); | |
70 | ||
8d657035 TT |
71 | static void default_mourn_inferior (struct target_ops *self); |
72 | ||
58a5184e TT |
73 | static int default_search_memory (struct target_ops *ops, |
74 | CORE_ADDR start_addr, | |
75 | ULONGEST search_space_len, | |
76 | const gdb_byte *pattern, | |
77 | ULONGEST pattern_len, | |
78 | CORE_ADDR *found_addrp); | |
79 | ||
936d2992 PA |
80 | static int default_verify_memory (struct target_ops *self, |
81 | const gdb_byte *data, | |
82 | CORE_ADDR memaddr, ULONGEST size); | |
83 | ||
c25c4a8b | 84 | static void tcomplain (void) ATTRIBUTE_NORETURN; |
c906108c | 85 | |
a121b7c1 | 86 | static struct target_ops *find_default_run_target (const char *); |
c906108c | 87 | |
0b5a2719 TT |
88 | static int dummy_find_memory_regions (struct target_ops *self, |
89 | find_memory_region_ftype ignore1, | |
90 | void *ignore2); | |
91 | ||
24f5300a SM |
92 | static gdb::unique_xmalloc_ptr<char> dummy_make_corefile_notes |
93 | (struct target_ops *self, bfd *ignore1, int *ignore2); | |
16f796b1 | 94 | |
a068643d | 95 | static std::string default_pid_to_str (struct target_ops *ops, ptid_t ptid); |
770234d3 | 96 | |
fe31bf5b TT |
97 | static enum exec_direction_kind default_execution_direction |
98 | (struct target_ops *self); | |
99 | ||
d9f719f1 PA |
100 | /* Mapping between target_info objects (which have address identity) |
101 | and corresponding open/factory function/callback. Each add_target | |
102 | call adds one entry to this map, and registers a "target | |
103 | TARGET_NAME" command that when invoked calls the factory registered | |
104 | here. The target_info object is associated with the command via | |
105 | the command's context. */ | |
106 | static std::unordered_map<const target_info *, target_open_ftype *> | |
107 | target_factories; | |
c906108c | 108 | |
06b5b831 | 109 | /* The singleton debug target. */ |
c906108c | 110 | |
f6ac5f3d | 111 | static struct target_ops *the_debug_target; |
c906108c | 112 | |
c906108c SS |
113 | /* Command list for target. */ |
114 | ||
115 | static struct cmd_list_element *targetlist = NULL; | |
116 | ||
491144b5 | 117 | /* True if we should trust readonly sections from the |
cf7a04e8 DJ |
118 | executable when reading memory. */ |
119 | ||
491144b5 | 120 | static bool trust_readonly = false; |
cf7a04e8 | 121 | |
8defab1a DJ |
122 | /* Nonzero if we should show true memory content including |
123 | memory breakpoint inserted by gdb. */ | |
124 | ||
125 | static int show_memory_breakpoints = 0; | |
126 | ||
d914c394 SS |
127 | /* These globals control whether GDB attempts to perform these |
128 | operations; they are useful for targets that need to prevent | |
30baf67b | 129 | inadvertent disruption, such as in non-stop mode. */ |
d914c394 | 130 | |
491144b5 | 131 | bool may_write_registers = true; |
d914c394 | 132 | |
491144b5 | 133 | bool may_write_memory = true; |
d914c394 | 134 | |
491144b5 | 135 | bool may_insert_breakpoints = true; |
d914c394 | 136 | |
491144b5 | 137 | bool may_insert_tracepoints = true; |
d914c394 | 138 | |
491144b5 | 139 | bool may_insert_fast_tracepoints = true; |
d914c394 | 140 | |
491144b5 | 141 | bool may_stop = true; |
d914c394 | 142 | |
c906108c SS |
143 | /* Non-zero if we want to see trace of target level stuff. */ |
144 | ||
ccce17b0 | 145 | static unsigned int targetdebug = 0; |
3cecbbbe TT |
146 | |
147 | static void | |
eb4c3f4a | 148 | set_targetdebug (const char *args, int from_tty, struct cmd_list_element *c) |
3cecbbbe | 149 | { |
f6ac5f3d | 150 | if (targetdebug) |
02980c56 | 151 | current_inferior ()->push_target (the_debug_target); |
f6ac5f3d | 152 | else |
fadf6add | 153 | current_inferior ()->unpush_target (the_debug_target); |
3cecbbbe TT |
154 | } |
155 | ||
920d2a44 AC |
156 | static void |
157 | show_targetdebug (struct ui_file *file, int from_tty, | |
158 | struct cmd_list_element *c, const char *value) | |
159 | { | |
160 | fprintf_filtered (file, _("Target debugging is %s.\n"), value); | |
161 | } | |
c906108c | 162 | |
c35b1492 | 163 | int |
a739972c | 164 | target_has_memory () |
c35b1492 | 165 | { |
328d42d8 SM |
166 | for (target_ops *t = current_inferior ()->top_target (); |
167 | t != NULL; | |
168 | t = t->beneath ()) | |
f6ac5f3d | 169 | if (t->has_memory ()) |
c35b1492 PA |
170 | return 1; |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | int | |
841de120 | 176 | target_has_stack () |
c35b1492 | 177 | { |
328d42d8 SM |
178 | for (target_ops *t = current_inferior ()->top_target (); |
179 | t != NULL; | |
180 | t = t->beneath ()) | |
f6ac5f3d | 181 | if (t->has_stack ()) |
c35b1492 PA |
182 | return 1; |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | int | |
9dccd06e | 188 | target_has_registers () |
c35b1492 | 189 | { |
328d42d8 SM |
190 | for (target_ops *t = current_inferior ()->top_target (); |
191 | t != NULL; | |
192 | t = t->beneath ()) | |
f6ac5f3d | 193 | if (t->has_registers ()) |
c35b1492 PA |
194 | return 1; |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
5018ce90 | 199 | bool |
55f6301a | 200 | target_has_execution (inferior *inf) |
c35b1492 | 201 | { |
55f6301a TT |
202 | if (inf == nullptr) |
203 | inf = current_inferior (); | |
204 | ||
5b6d1e4f PA |
205 | for (target_ops *t = inf->top_target (); |
206 | t != nullptr; | |
207 | t = inf->find_target_beneath (t)) | |
5018ce90 PA |
208 | if (t->has_execution (inf)) |
209 | return true; | |
c35b1492 | 210 | |
5018ce90 | 211 | return false; |
c35b1492 PA |
212 | } |
213 | ||
d777bf0d SM |
214 | const char * |
215 | target_shortname () | |
216 | { | |
328d42d8 | 217 | return current_inferior ()->top_target ()->shortname (); |
d777bf0d SM |
218 | } |
219 | ||
220 | /* See target.h. */ | |
221 | ||
222 | bool | |
223 | target_attach_no_wait () | |
224 | { | |
328d42d8 | 225 | return current_inferior ()->top_target ()->attach_no_wait (); |
d777bf0d SM |
226 | } |
227 | ||
228 | /* See target.h. */ | |
229 | ||
230 | void | |
231 | target_post_attach (int pid) | |
232 | { | |
328d42d8 | 233 | return current_inferior ()->top_target ()->post_attach (pid); |
d777bf0d SM |
234 | } |
235 | ||
236 | /* See target.h. */ | |
237 | ||
238 | void | |
239 | target_prepare_to_store (regcache *regcache) | |
240 | { | |
328d42d8 | 241 | return current_inferior ()->top_target ()->prepare_to_store (regcache); |
d777bf0d SM |
242 | } |
243 | ||
244 | /* See target.h. */ | |
245 | ||
246 | bool | |
247 | target_supports_enable_disable_tracepoint () | |
248 | { | |
328d42d8 SM |
249 | target_ops *target = current_inferior ()->top_target (); |
250 | ||
251 | return target->supports_enable_disable_tracepoint (); | |
d777bf0d SM |
252 | } |
253 | ||
254 | bool | |
255 | target_supports_string_tracing () | |
256 | { | |
328d42d8 | 257 | return current_inferior ()->top_target ()->supports_string_tracing (); |
d777bf0d SM |
258 | } |
259 | ||
260 | /* See target.h. */ | |
261 | ||
262 | bool | |
263 | target_supports_evaluation_of_breakpoint_conditions () | |
264 | { | |
328d42d8 SM |
265 | target_ops *target = current_inferior ()->top_target (); |
266 | ||
267 | return target->supports_evaluation_of_breakpoint_conditions (); | |
d777bf0d SM |
268 | } |
269 | ||
270 | /* See target.h. */ | |
271 | ||
272 | bool | |
273 | target_supports_dumpcore () | |
274 | { | |
328d42d8 | 275 | return current_inferior ()->top_target ()->supports_dumpcore (); |
d777bf0d SM |
276 | } |
277 | ||
278 | /* See target.h. */ | |
279 | ||
280 | void | |
281 | target_dumpcore (const char *filename) | |
282 | { | |
328d42d8 | 283 | return current_inferior ()->top_target ()->dumpcore (filename); |
d777bf0d SM |
284 | } |
285 | ||
286 | /* See target.h. */ | |
287 | ||
288 | bool | |
289 | target_can_run_breakpoint_commands () | |
290 | { | |
328d42d8 | 291 | return current_inferior ()->top_target ()->can_run_breakpoint_commands (); |
d777bf0d SM |
292 | } |
293 | ||
294 | /* See target.h. */ | |
295 | ||
296 | void | |
297 | target_files_info () | |
298 | { | |
328d42d8 | 299 | return current_inferior ()->top_target ()->files_info (); |
d777bf0d SM |
300 | } |
301 | ||
302 | /* See target.h. */ | |
303 | ||
304 | void | |
305 | target_post_startup_inferior (ptid_t ptid) | |
306 | { | |
328d42d8 | 307 | return current_inferior ()->top_target ()->post_startup_inferior (ptid); |
d777bf0d SM |
308 | } |
309 | ||
310 | /* See target.h. */ | |
311 | ||
312 | int | |
313 | target_insert_fork_catchpoint (int pid) | |
314 | { | |
328d42d8 | 315 | return current_inferior ()->top_target ()->insert_fork_catchpoint (pid); |
d777bf0d SM |
316 | } |
317 | ||
318 | /* See target.h. */ | |
319 | ||
320 | int | |
321 | target_remove_fork_catchpoint (int pid) | |
322 | { | |
328d42d8 | 323 | return current_inferior ()->top_target ()->remove_fork_catchpoint (pid); |
d777bf0d SM |
324 | } |
325 | ||
326 | /* See target.h. */ | |
327 | ||
328 | int | |
329 | target_insert_vfork_catchpoint (int pid) | |
330 | { | |
328d42d8 | 331 | return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid); |
d777bf0d SM |
332 | } |
333 | ||
334 | /* See target.h. */ | |
335 | ||
336 | int | |
337 | target_remove_vfork_catchpoint (int pid) | |
338 | { | |
328d42d8 | 339 | return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid); |
d777bf0d SM |
340 | } |
341 | ||
342 | /* See target.h. */ | |
343 | ||
344 | int | |
345 | target_insert_exec_catchpoint (int pid) | |
346 | { | |
328d42d8 | 347 | return current_inferior ()->top_target ()->insert_exec_catchpoint (pid); |
d777bf0d SM |
348 | } |
349 | ||
350 | /* See target.h. */ | |
351 | ||
352 | int | |
353 | target_remove_exec_catchpoint (int pid) | |
354 | { | |
328d42d8 | 355 | return current_inferior ()->top_target ()->remove_exec_catchpoint (pid); |
d777bf0d SM |
356 | } |
357 | ||
358 | /* See target.h. */ | |
359 | ||
360 | int | |
361 | target_set_syscall_catchpoint (int pid, bool needed, int any_count, | |
362 | gdb::array_view<const int> syscall_counts) | |
363 | { | |
328d42d8 SM |
364 | target_ops *target = current_inferior ()->top_target (); |
365 | ||
366 | return target->set_syscall_catchpoint (pid, needed, any_count, | |
367 | syscall_counts); | |
d777bf0d SM |
368 | } |
369 | ||
370 | /* See target.h. */ | |
371 | ||
372 | void | |
373 | target_rcmd (const char *command, struct ui_file *outbuf) | |
374 | { | |
328d42d8 | 375 | return current_inferior ()->top_target ()->rcmd (command, outbuf); |
d777bf0d SM |
376 | } |
377 | ||
378 | /* See target.h. */ | |
379 | ||
380 | bool | |
381 | target_can_lock_scheduler () | |
382 | { | |
328d42d8 SM |
383 | target_ops *target = current_inferior ()->top_target (); |
384 | ||
385 | return (target->get_thread_control_capabilities ()& tc_schedlock) != 0; | |
d777bf0d SM |
386 | } |
387 | ||
388 | /* See target.h. */ | |
389 | ||
390 | bool | |
391 | target_can_async_p () | |
392 | { | |
328d42d8 | 393 | return current_inferior ()->top_target ()->can_async_p (); |
d777bf0d SM |
394 | } |
395 | ||
396 | /* See target.h. */ | |
397 | ||
398 | bool | |
399 | target_is_async_p () | |
400 | { | |
328d42d8 | 401 | return current_inferior ()->top_target ()->is_async_p (); |
d777bf0d SM |
402 | } |
403 | ||
404 | exec_direction_kind | |
405 | target_execution_direction () | |
406 | { | |
328d42d8 | 407 | return current_inferior ()->top_target ()->execution_direction (); |
d777bf0d SM |
408 | } |
409 | ||
410 | /* See target.h. */ | |
411 | ||
412 | const char * | |
413 | target_extra_thread_info (thread_info *tp) | |
414 | { | |
328d42d8 | 415 | return current_inferior ()->top_target ()->extra_thread_info (tp); |
d777bf0d SM |
416 | } |
417 | ||
418 | /* See target.h. */ | |
419 | ||
420 | char * | |
421 | target_pid_to_exec_file (int pid) | |
422 | { | |
328d42d8 | 423 | return current_inferior ()->top_target ()->pid_to_exec_file (pid); |
d777bf0d SM |
424 | } |
425 | ||
426 | /* See target.h. */ | |
427 | ||
428 | gdbarch * | |
429 | target_thread_architecture (ptid_t ptid) | |
430 | { | |
328d42d8 | 431 | return current_inferior ()->top_target ()->thread_architecture (ptid); |
d777bf0d SM |
432 | } |
433 | ||
434 | /* See target.h. */ | |
435 | ||
436 | int | |
437 | target_find_memory_regions (find_memory_region_ftype func, void *data) | |
438 | { | |
328d42d8 | 439 | return current_inferior ()->top_target ()->find_memory_regions (func, data); |
d777bf0d SM |
440 | } |
441 | ||
442 | /* See target.h. */ | |
443 | ||
444 | gdb::unique_xmalloc_ptr<char> | |
445 | target_make_corefile_notes (bfd *bfd, int *size_p) | |
446 | { | |
328d42d8 | 447 | return current_inferior ()->top_target ()->make_corefile_notes (bfd, size_p); |
d777bf0d SM |
448 | } |
449 | ||
450 | gdb_byte * | |
451 | target_get_bookmark (const char *args, int from_tty) | |
452 | { | |
328d42d8 | 453 | return current_inferior ()->top_target ()->get_bookmark (args, from_tty); |
d777bf0d SM |
454 | } |
455 | ||
456 | void | |
457 | target_goto_bookmark (const gdb_byte *arg, int from_tty) | |
458 | { | |
328d42d8 | 459 | return current_inferior ()->top_target ()->goto_bookmark (arg, from_tty); |
d777bf0d SM |
460 | } |
461 | ||
462 | /* See target.h. */ | |
463 | ||
464 | bool | |
465 | target_stopped_by_watchpoint () | |
466 | { | |
328d42d8 | 467 | return current_inferior ()->top_target ()->stopped_by_watchpoint (); |
d777bf0d SM |
468 | } |
469 | ||
470 | /* See target.h. */ | |
471 | ||
472 | bool | |
473 | target_stopped_by_sw_breakpoint () | |
474 | { | |
328d42d8 | 475 | return current_inferior ()->top_target ()->stopped_by_sw_breakpoint (); |
d777bf0d SM |
476 | } |
477 | ||
478 | bool | |
479 | target_supports_stopped_by_sw_breakpoint () | |
480 | { | |
328d42d8 SM |
481 | target_ops *target = current_inferior ()->top_target (); |
482 | ||
483 | return target->supports_stopped_by_sw_breakpoint (); | |
d777bf0d SM |
484 | } |
485 | ||
486 | bool | |
487 | target_stopped_by_hw_breakpoint () | |
488 | { | |
328d42d8 | 489 | return current_inferior ()->top_target ()->stopped_by_hw_breakpoint (); |
d777bf0d SM |
490 | } |
491 | ||
492 | bool | |
493 | target_supports_stopped_by_hw_breakpoint () | |
494 | { | |
328d42d8 SM |
495 | target_ops *target = current_inferior ()->top_target (); |
496 | ||
497 | return target->supports_stopped_by_hw_breakpoint (); | |
d777bf0d SM |
498 | } |
499 | ||
500 | /* See target.h. */ | |
501 | ||
502 | bool | |
503 | target_have_steppable_watchpoint () | |
504 | { | |
328d42d8 | 505 | return current_inferior ()->top_target ()->have_steppable_watchpoint (); |
d777bf0d SM |
506 | } |
507 | ||
508 | /* See target.h. */ | |
509 | ||
510 | int | |
511 | target_can_use_hardware_watchpoint (bptype type, int cnt, int othertype) | |
512 | { | |
328d42d8 SM |
513 | target_ops *target = current_inferior ()->top_target (); |
514 | ||
515 | return target->can_use_hw_breakpoint (type, cnt, othertype); | |
d777bf0d SM |
516 | } |
517 | ||
518 | /* See target.h. */ | |
519 | ||
520 | int | |
521 | target_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) | |
522 | { | |
328d42d8 SM |
523 | target_ops *target = current_inferior ()->top_target (); |
524 | ||
525 | return target->region_ok_for_hw_watchpoint (addr, len); | |
d777bf0d SM |
526 | } |
527 | ||
528 | ||
529 | int | |
530 | target_can_do_single_step () | |
531 | { | |
328d42d8 | 532 | return current_inferior ()->top_target ()->can_do_single_step (); |
d777bf0d SM |
533 | } |
534 | ||
535 | /* See target.h. */ | |
536 | ||
537 | int | |
538 | target_insert_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type, | |
539 | expression *cond) | |
540 | { | |
328d42d8 SM |
541 | target_ops *target = current_inferior ()->top_target (); |
542 | ||
543 | return target->insert_watchpoint (addr, len, type, cond); | |
d777bf0d SM |
544 | } |
545 | ||
546 | /* See target.h. */ | |
547 | ||
548 | int | |
549 | target_remove_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type, | |
550 | expression *cond) | |
551 | { | |
328d42d8 SM |
552 | target_ops *target = current_inferior ()->top_target (); |
553 | ||
554 | return target->remove_watchpoint (addr, len, type, cond); | |
d777bf0d SM |
555 | } |
556 | ||
557 | /* See target.h. */ | |
558 | ||
559 | int | |
560 | target_insert_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt) | |
561 | { | |
328d42d8 SM |
562 | target_ops *target = current_inferior ()->top_target (); |
563 | ||
564 | return target->insert_hw_breakpoint (gdbarch, bp_tgt); | |
d777bf0d SM |
565 | } |
566 | ||
567 | /* See target.h. */ | |
568 | ||
569 | int | |
570 | target_remove_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt) | |
571 | { | |
328d42d8 SM |
572 | target_ops *target = current_inferior ()->top_target (); |
573 | ||
574 | return target->remove_hw_breakpoint (gdbarch, bp_tgt); | |
d777bf0d SM |
575 | } |
576 | ||
577 | /* See target.h. */ | |
578 | ||
579 | bool | |
580 | target_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int type, | |
581 | expression *cond) | |
582 | { | |
328d42d8 SM |
583 | target_ops *target = current_inferior ()->top_target (); |
584 | ||
585 | return target->can_accel_watchpoint_condition (addr, len, type, cond); | |
d777bf0d SM |
586 | } |
587 | ||
588 | /* See target.h. */ | |
589 | ||
590 | bool | |
591 | target_can_execute_reverse () | |
592 | { | |
328d42d8 | 593 | return current_inferior ()->top_target ()->can_execute_reverse (); |
d777bf0d SM |
594 | } |
595 | ||
596 | ptid_t | |
597 | target_get_ada_task_ptid (long lwp, long tid) | |
598 | { | |
328d42d8 | 599 | return current_inferior ()->top_target ()->get_ada_task_ptid (lwp, tid); |
d777bf0d SM |
600 | } |
601 | ||
602 | bool | |
603 | target_filesystem_is_local () | |
604 | { | |
328d42d8 | 605 | return current_inferior ()->top_target ()->filesystem_is_local (); |
d777bf0d SM |
606 | } |
607 | ||
608 | void | |
609 | target_trace_init () | |
610 | { | |
328d42d8 | 611 | return current_inferior ()->top_target ()->trace_init (); |
d777bf0d SM |
612 | } |
613 | ||
614 | void | |
615 | target_download_tracepoint (bp_location *location) | |
616 | { | |
328d42d8 | 617 | return current_inferior ()->top_target ()->download_tracepoint (location); |
d777bf0d SM |
618 | } |
619 | ||
620 | bool | |
621 | target_can_download_tracepoint () | |
622 | { | |
328d42d8 | 623 | return current_inferior ()->top_target ()->can_download_tracepoint (); |
d777bf0d SM |
624 | } |
625 | ||
626 | void | |
627 | target_download_trace_state_variable (const trace_state_variable &tsv) | |
628 | { | |
328d42d8 SM |
629 | target_ops *target = current_inferior ()->top_target (); |
630 | ||
631 | return target->download_trace_state_variable (tsv); | |
d777bf0d SM |
632 | } |
633 | ||
634 | void | |
635 | target_enable_tracepoint (bp_location *loc) | |
636 | { | |
328d42d8 | 637 | return current_inferior ()->top_target ()->enable_tracepoint (loc); |
d777bf0d SM |
638 | } |
639 | ||
640 | void | |
641 | target_disable_tracepoint (bp_location *loc) | |
642 | { | |
328d42d8 | 643 | return current_inferior ()->top_target ()->disable_tracepoint (loc); |
d777bf0d SM |
644 | } |
645 | ||
646 | void | |
647 | target_trace_start () | |
648 | { | |
328d42d8 | 649 | return current_inferior ()->top_target ()->trace_start (); |
d777bf0d SM |
650 | } |
651 | ||
652 | void | |
653 | target_trace_set_readonly_regions () | |
654 | { | |
328d42d8 | 655 | return current_inferior ()->top_target ()->trace_set_readonly_regions (); |
d777bf0d SM |
656 | } |
657 | ||
658 | int | |
659 | target_get_trace_status (trace_status *ts) | |
660 | { | |
328d42d8 | 661 | return current_inferior ()->top_target ()->get_trace_status (ts); |
d777bf0d SM |
662 | } |
663 | ||
664 | void | |
665 | target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp) | |
666 | { | |
328d42d8 | 667 | return current_inferior ()->top_target ()->get_tracepoint_status (tp, utp); |
d777bf0d SM |
668 | } |
669 | ||
670 | void | |
671 | target_trace_stop () | |
672 | { | |
328d42d8 | 673 | return current_inferior ()->top_target ()->trace_stop (); |
d777bf0d SM |
674 | } |
675 | ||
676 | int | |
677 | target_trace_find (trace_find_type type, int num, | |
678 | CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) | |
679 | { | |
328d42d8 SM |
680 | target_ops *target = current_inferior ()->top_target (); |
681 | ||
682 | return target->trace_find (type, num, addr1, addr2, tpp); | |
d777bf0d SM |
683 | } |
684 | ||
685 | bool | |
686 | target_get_trace_state_variable_value (int tsv, LONGEST *val) | |
687 | { | |
328d42d8 SM |
688 | target_ops *target = current_inferior ()->top_target (); |
689 | ||
690 | return target->get_trace_state_variable_value (tsv, val); | |
d777bf0d SM |
691 | } |
692 | ||
693 | int | |
694 | target_save_trace_data (const char *filename) | |
695 | { | |
328d42d8 | 696 | return current_inferior ()->top_target ()->save_trace_data (filename); |
d777bf0d SM |
697 | } |
698 | ||
699 | int | |
700 | target_upload_tracepoints (uploaded_tp **utpp) | |
701 | { | |
328d42d8 | 702 | return current_inferior ()->top_target ()->upload_tracepoints (utpp); |
d777bf0d SM |
703 | } |
704 | ||
705 | int | |
706 | target_upload_trace_state_variables (uploaded_tsv **utsvp) | |
707 | { | |
328d42d8 SM |
708 | target_ops *target = current_inferior ()->top_target (); |
709 | ||
710 | return target->upload_trace_state_variables (utsvp); | |
d777bf0d SM |
711 | } |
712 | ||
713 | LONGEST | |
714 | target_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) | |
715 | { | |
328d42d8 SM |
716 | target_ops *target = current_inferior ()->top_target (); |
717 | ||
718 | return target->get_raw_trace_data (buf, offset, len); | |
d777bf0d SM |
719 | } |
720 | ||
721 | int | |
722 | target_get_min_fast_tracepoint_insn_len () | |
723 | { | |
328d42d8 SM |
724 | target_ops *target = current_inferior ()->top_target (); |
725 | ||
726 | return target->get_min_fast_tracepoint_insn_len (); | |
d777bf0d SM |
727 | } |
728 | ||
729 | void | |
730 | target_set_disconnected_tracing (int val) | |
731 | { | |
328d42d8 | 732 | return current_inferior ()->top_target ()->set_disconnected_tracing (val); |
d777bf0d SM |
733 | } |
734 | ||
735 | void | |
736 | target_set_circular_trace_buffer (int val) | |
737 | { | |
328d42d8 | 738 | return current_inferior ()->top_target ()->set_circular_trace_buffer (val); |
d777bf0d SM |
739 | } |
740 | ||
741 | void | |
742 | target_set_trace_buffer_size (LONGEST val) | |
743 | { | |
328d42d8 | 744 | return current_inferior ()->top_target ()->set_trace_buffer_size (val); |
d777bf0d SM |
745 | } |
746 | ||
747 | bool | |
748 | target_set_trace_notes (const char *user, const char *notes, | |
749 | const char *stopnotes) | |
750 | { | |
328d42d8 SM |
751 | target_ops *target = current_inferior ()->top_target (); |
752 | ||
753 | return target->set_trace_notes (user, notes, stopnotes); | |
d777bf0d SM |
754 | } |
755 | ||
756 | bool | |
757 | target_get_tib_address (ptid_t ptid, CORE_ADDR *addr) | |
758 | { | |
328d42d8 | 759 | return current_inferior ()->top_target ()->get_tib_address (ptid, addr); |
d777bf0d SM |
760 | } |
761 | ||
762 | void | |
763 | target_set_permissions () | |
764 | { | |
328d42d8 | 765 | return current_inferior ()->top_target ()->set_permissions (); |
d777bf0d SM |
766 | } |
767 | ||
768 | bool | |
769 | target_static_tracepoint_marker_at (CORE_ADDR addr, | |
770 | static_tracepoint_marker *marker) | |
771 | { | |
328d42d8 SM |
772 | target_ops *target = current_inferior ()->top_target (); |
773 | ||
774 | return target->static_tracepoint_marker_at (addr, marker); | |
d777bf0d SM |
775 | } |
776 | ||
777 | std::vector<static_tracepoint_marker> | |
778 | target_static_tracepoint_markers_by_strid (const char *marker_id) | |
779 | { | |
328d42d8 SM |
780 | target_ops *target = current_inferior ()->top_target (); |
781 | ||
782 | return target->static_tracepoint_markers_by_strid (marker_id); | |
d777bf0d SM |
783 | } |
784 | ||
785 | traceframe_info_up | |
786 | target_traceframe_info () | |
787 | { | |
328d42d8 | 788 | return current_inferior ()->top_target ()->traceframe_info (); |
d777bf0d SM |
789 | } |
790 | ||
791 | bool | |
792 | target_use_agent (bool use) | |
793 | { | |
328d42d8 | 794 | return current_inferior ()->top_target ()->use_agent (use); |
d777bf0d SM |
795 | } |
796 | ||
797 | bool | |
798 | target_can_use_agent () | |
799 | { | |
328d42d8 | 800 | return current_inferior ()->top_target ()->can_use_agent (); |
d777bf0d SM |
801 | } |
802 | ||
803 | bool | |
804 | target_augmented_libraries_svr4_read () | |
805 | { | |
328d42d8 | 806 | return current_inferior ()->top_target ()->augmented_libraries_svr4_read (); |
d777bf0d SM |
807 | } |
808 | ||
809 | bool | |
810 | target_supports_memory_tagging () | |
811 | { | |
328d42d8 | 812 | return current_inferior ()->top_target ()->supports_memory_tagging (); |
d777bf0d SM |
813 | } |
814 | ||
815 | bool | |
816 | target_fetch_memtags (CORE_ADDR address, size_t len, gdb::byte_vector &tags, | |
817 | int type) | |
818 | { | |
328d42d8 | 819 | return current_inferior ()->top_target ()->fetch_memtags (address, len, tags, type); |
d777bf0d SM |
820 | } |
821 | ||
822 | bool | |
823 | target_store_memtags (CORE_ADDR address, size_t len, | |
824 | const gdb::byte_vector &tags, int type) | |
825 | { | |
328d42d8 | 826 | return current_inferior ()->top_target ()->store_memtags (address, len, tags, type); |
d777bf0d SM |
827 | } |
828 | ||
829 | void | |
830 | target_log_command (const char *p) | |
831 | { | |
328d42d8 | 832 | return current_inferior ()->top_target ()->log_command (p); |
d777bf0d SM |
833 | } |
834 | ||
8981c758 TT |
835 | /* This is used to implement the various target commands. */ |
836 | ||
837 | static void | |
eb4c3f4a | 838 | open_target (const char *args, int from_tty, struct cmd_list_element *command) |
8981c758 | 839 | { |
d9f719f1 PA |
840 | auto *ti = static_cast<target_info *> (get_cmd_context (command)); |
841 | target_open_ftype *func = target_factories[ti]; | |
8981c758 TT |
842 | |
843 | if (targetdebug) | |
d9f719f1 PA |
844 | fprintf_unfiltered (gdb_stdlog, "-> %s->open (...)\n", |
845 | ti->shortname); | |
8981c758 | 846 | |
d9f719f1 | 847 | func (args, from_tty); |
8981c758 TT |
848 | |
849 | if (targetdebug) | |
d9f719f1 PA |
850 | fprintf_unfiltered (gdb_stdlog, "<- %s->open (%s, %d)\n", |
851 | ti->shortname, args, from_tty); | |
8981c758 TT |
852 | } |
853 | ||
d9f719f1 | 854 | /* See target.h. */ |
c22a2b88 TT |
855 | |
856 | void | |
d9f719f1 PA |
857 | add_target (const target_info &t, target_open_ftype *func, |
858 | completer_ftype *completer) | |
c22a2b88 TT |
859 | { |
860 | struct cmd_list_element *c; | |
861 | ||
d9f719f1 PA |
862 | auto &func_slot = target_factories[&t]; |
863 | if (func_slot != nullptr) | |
864 | internal_error (__FILE__, __LINE__, | |
865 | _("target already added (\"%s\")."), t.shortname); | |
866 | func_slot = func; | |
c906108c SS |
867 | |
868 | if (targetlist == NULL) | |
3b6acaee | 869 | add_basic_prefix_cmd ("target", class_run, _("\ |
1bedd215 | 870 | Connect to a target machine or process.\n\ |
c906108c SS |
871 | The first argument is the type or protocol of the target machine.\n\ |
872 | Remaining arguments are interpreted by the target protocol. For more\n\ | |
873 | information on the arguments for a particular protocol, type\n\ | |
1bedd215 | 874 | `help target ' followed by the protocol name."), |
3b6acaee | 875 | &targetlist, "target ", 0, &cmdlist); |
d9f719f1 PA |
876 | c = add_cmd (t.shortname, no_class, t.doc, &targetlist); |
877 | set_cmd_context (c, (void *) &t); | |
8981c758 | 878 | set_cmd_sfunc (c, open_target); |
9852c492 YQ |
879 | if (completer != NULL) |
880 | set_cmd_completer (c, completer); | |
881 | } | |
882 | ||
b48d48eb MM |
883 | /* See target.h. */ |
884 | ||
885 | void | |
d9f719f1 | 886 | add_deprecated_target_alias (const target_info &tinfo, const char *alias) |
b48d48eb MM |
887 | { |
888 | struct cmd_list_element *c; | |
889 | char *alt; | |
890 | ||
891 | /* If we use add_alias_cmd, here, we do not get the deprecated warning, | |
892 | see PR cli/15104. */ | |
d9f719f1 | 893 | c = add_cmd (alias, no_class, tinfo.doc, &targetlist); |
8981c758 | 894 | set_cmd_sfunc (c, open_target); |
d9f719f1 PA |
895 | set_cmd_context (c, (void *) &tinfo); |
896 | alt = xstrprintf ("target %s", tinfo.shortname); | |
b48d48eb MM |
897 | deprecate_cmd (c, alt); |
898 | } | |
899 | ||
c906108c SS |
900 | /* Stub functions */ |
901 | ||
7d85a9c0 JB |
902 | void |
903 | target_kill (void) | |
904 | { | |
328d42d8 | 905 | current_inferior ()->top_target ()->kill (); |
7d85a9c0 JB |
906 | } |
907 | ||
11cf8741 | 908 | void |
9cbe5fff | 909 | target_load (const char *arg, int from_tty) |
11cf8741 | 910 | { |
4e5d721f | 911 | target_dcache_invalidate (); |
328d42d8 | 912 | current_inferior ()->top_target ()->load (arg, from_tty); |
11cf8741 JM |
913 | } |
914 | ||
223ffa71 | 915 | /* Define it. */ |
5842f62a | 916 | |
e671cd59 PA |
917 | target_terminal_state target_terminal::m_terminal_state |
918 | = target_terminal_state::is_ours; | |
5842f62a | 919 | |
223ffa71 | 920 | /* See target/target.h. */ |
5842f62a PA |
921 | |
922 | void | |
223ffa71 | 923 | target_terminal::init (void) |
5842f62a | 924 | { |
328d42d8 | 925 | current_inferior ()->top_target ()->terminal_init (); |
5842f62a | 926 | |
e671cd59 | 927 | m_terminal_state = target_terminal_state::is_ours; |
5842f62a PA |
928 | } |
929 | ||
223ffa71 | 930 | /* See target/target.h. */ |
2f99e8fc | 931 | |
d9d2d8b6 | 932 | void |
223ffa71 | 933 | target_terminal::inferior (void) |
d9d2d8b6 | 934 | { |
41fd2b0f PA |
935 | struct ui *ui = current_ui; |
936 | ||
d9d2d8b6 | 937 | /* A background resume (``run&'') should leave GDB in control of the |
3b12939d PA |
938 | terminal. */ |
939 | if (ui->prompt_state != PROMPT_BLOCKED) | |
d9d2d8b6 PA |
940 | return; |
941 | ||
215d3118 PA |
942 | /* Since we always run the inferior in the main console (unless "set |
943 | inferior-tty" is in effect), when some UI other than the main one | |
223ffa71 TT |
944 | calls target_terminal::inferior, then we leave the main UI's |
945 | terminal settings as is. */ | |
215d3118 PA |
946 | if (ui != main_ui) |
947 | return; | |
948 | ||
d9d2d8b6 PA |
949 | /* If GDB is resuming the inferior in the foreground, install |
950 | inferior's terminal modes. */ | |
e671cd59 PA |
951 | |
952 | struct inferior *inf = current_inferior (); | |
953 | ||
954 | if (inf->terminal_state != target_terminal_state::is_inferior) | |
955 | { | |
328d42d8 | 956 | current_inferior ()->top_target ()->terminal_inferior (); |
e671cd59 PA |
957 | inf->terminal_state = target_terminal_state::is_inferior; |
958 | } | |
959 | ||
960 | m_terminal_state = target_terminal_state::is_inferior; | |
961 | ||
962 | /* If the user hit C-c before, pretend that it was hit right | |
963 | here. */ | |
964 | if (check_quit_flag ()) | |
965 | target_pass_ctrlc (); | |
966 | } | |
967 | ||
968 | /* See target/target.h. */ | |
969 | ||
970 | void | |
971 | target_terminal::restore_inferior (void) | |
972 | { | |
973 | struct ui *ui = current_ui; | |
974 | ||
975 | /* See target_terminal::inferior(). */ | |
976 | if (ui->prompt_state != PROMPT_BLOCKED || ui != main_ui) | |
977 | return; | |
978 | ||
979 | /* Restore the terminal settings of inferiors that were in the | |
980 | foreground but are now ours_for_output due to a temporary | |
981 | target_target::ours_for_output() call. */ | |
982 | ||
983 | { | |
984 | scoped_restore_current_inferior restore_inferior; | |
e671cd59 | 985 | |
84b68c77 | 986 | for (::inferior *inf : all_inferiors ()) |
e671cd59 PA |
987 | { |
988 | if (inf->terminal_state == target_terminal_state::is_ours_for_output) | |
989 | { | |
990 | set_current_inferior (inf); | |
328d42d8 | 991 | current_inferior ()->top_target ()->terminal_inferior (); |
e671cd59 PA |
992 | inf->terminal_state = target_terminal_state::is_inferior; |
993 | } | |
994 | } | |
995 | } | |
996 | ||
997 | m_terminal_state = target_terminal_state::is_inferior; | |
93692b58 PA |
998 | |
999 | /* If the user hit C-c before, pretend that it was hit right | |
1000 | here. */ | |
1001 | if (check_quit_flag ()) | |
1002 | target_pass_ctrlc (); | |
5842f62a PA |
1003 | } |
1004 | ||
e671cd59 PA |
1005 | /* Switch terminal state to DESIRED_STATE, either is_ours, or |
1006 | is_ours_for_output. */ | |
1007 | ||
1008 | static void | |
1009 | target_terminal_is_ours_kind (target_terminal_state desired_state) | |
1010 | { | |
1011 | scoped_restore_current_inferior restore_inferior; | |
e671cd59 PA |
1012 | |
1013 | /* Must do this in two passes. First, have all inferiors save the | |
1014 | current terminal settings. Then, after all inferiors have add a | |
1015 | chance to safely save the terminal settings, restore GDB's | |
1016 | terminal settings. */ | |
1017 | ||
08036331 | 1018 | for (inferior *inf : all_inferiors ()) |
e671cd59 PA |
1019 | { |
1020 | if (inf->terminal_state == target_terminal_state::is_inferior) | |
1021 | { | |
1022 | set_current_inferior (inf); | |
328d42d8 | 1023 | current_inferior ()->top_target ()->terminal_save_inferior (); |
e671cd59 PA |
1024 | } |
1025 | } | |
1026 | ||
08036331 | 1027 | for (inferior *inf : all_inferiors ()) |
e671cd59 PA |
1028 | { |
1029 | /* Note we don't check is_inferior here like above because we | |
1030 | need to handle 'is_ours_for_output -> is_ours' too. Careful | |
1031 | to never transition from 'is_ours' to 'is_ours_for_output', | |
1032 | though. */ | |
1033 | if (inf->terminal_state != target_terminal_state::is_ours | |
1034 | && inf->terminal_state != desired_state) | |
1035 | { | |
1036 | set_current_inferior (inf); | |
1037 | if (desired_state == target_terminal_state::is_ours) | |
328d42d8 | 1038 | current_inferior ()->top_target ()->terminal_ours (); |
e671cd59 | 1039 | else if (desired_state == target_terminal_state::is_ours_for_output) |
328d42d8 | 1040 | current_inferior ()->top_target ()->terminal_ours_for_output (); |
e671cd59 PA |
1041 | else |
1042 | gdb_assert_not_reached ("unhandled desired state"); | |
1043 | inf->terminal_state = desired_state; | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | ||
223ffa71 | 1048 | /* See target/target.h. */ |
5842f62a PA |
1049 | |
1050 | void | |
223ffa71 | 1051 | target_terminal::ours () |
5842f62a | 1052 | { |
41fd2b0f PA |
1053 | struct ui *ui = current_ui; |
1054 | ||
223ffa71 | 1055 | /* See target_terminal::inferior. */ |
215d3118 PA |
1056 | if (ui != main_ui) |
1057 | return; | |
1058 | ||
e671cd59 | 1059 | if (m_terminal_state == target_terminal_state::is_ours) |
5842f62a PA |
1060 | return; |
1061 | ||
e671cd59 PA |
1062 | target_terminal_is_ours_kind (target_terminal_state::is_ours); |
1063 | m_terminal_state = target_terminal_state::is_ours; | |
5842f62a PA |
1064 | } |
1065 | ||
223ffa71 | 1066 | /* See target/target.h. */ |
5842f62a PA |
1067 | |
1068 | void | |
223ffa71 | 1069 | target_terminal::ours_for_output () |
5842f62a | 1070 | { |
215d3118 PA |
1071 | struct ui *ui = current_ui; |
1072 | ||
223ffa71 | 1073 | /* See target_terminal::inferior. */ |
215d3118 PA |
1074 | if (ui != main_ui) |
1075 | return; | |
1076 | ||
e671cd59 | 1077 | if (!target_terminal::is_inferior ()) |
5842f62a | 1078 | return; |
e671cd59 PA |
1079 | |
1080 | target_terminal_is_ours_kind (target_terminal_state::is_ours_for_output); | |
1081 | target_terminal::m_terminal_state = target_terminal_state::is_ours_for_output; | |
d9d2d8b6 | 1082 | } |
136d6dae | 1083 | |
223ffa71 TT |
1084 | /* See target/target.h. */ |
1085 | ||
1086 | void | |
1087 | target_terminal::info (const char *arg, int from_tty) | |
1088 | { | |
328d42d8 | 1089 | current_inferior ()->top_target ()->terminal_info (arg, from_tty); |
223ffa71 TT |
1090 | } |
1091 | ||
b0ed115f TT |
1092 | /* See target.h. */ |
1093 | ||
20f0d60d | 1094 | bool |
b0ed115f TT |
1095 | target_supports_terminal_ours (void) |
1096 | { | |
5b6d1e4f PA |
1097 | /* The current top target is the target at the top of the target |
1098 | stack of the current inferior. While normally there's always an | |
1099 | inferior, we must check for nullptr here because we can get here | |
1100 | very early during startup, before the initial inferior is first | |
1101 | created. */ | |
1102 | inferior *inf = current_inferior (); | |
20f0d60d | 1103 | |
5b6d1e4f | 1104 | if (inf == nullptr) |
20f0d60d | 1105 | return false; |
5b6d1e4f | 1106 | return inf->top_target ()->supports_terminal_ours (); |
b0ed115f TT |
1107 | } |
1108 | ||
c906108c | 1109 | static void |
fba45db2 | 1110 | tcomplain (void) |
c906108c | 1111 | { |
8a3fe4f8 | 1112 | error (_("You can't do that when your target is `%s'"), |
328d42d8 | 1113 | current_inferior ()->top_target ()->shortname ()); |
c906108c SS |
1114 | } |
1115 | ||
1116 | void | |
fba45db2 | 1117 | noprocess (void) |
c906108c | 1118 | { |
8a3fe4f8 | 1119 | error (_("You can't do that without a process to debug.")); |
c906108c SS |
1120 | } |
1121 | ||
c906108c | 1122 | static void |
0a4f40a2 | 1123 | default_terminal_info (struct target_ops *self, const char *args, int from_tty) |
c906108c | 1124 | { |
a3f17187 | 1125 | printf_unfiltered (_("No saved terminal information.\n")); |
c906108c SS |
1126 | } |
1127 | ||
0ef643c8 JB |
1128 | /* A default implementation for the to_get_ada_task_ptid target method. |
1129 | ||
1130 | This function builds the PTID by using both LWP and TID as part of | |
1131 | the PTID lwp and tid elements. The pid used is the pid of the | |
1132 | inferior_ptid. */ | |
1133 | ||
2c0b251b | 1134 | static ptid_t |
1e6b91a4 | 1135 | default_get_ada_task_ptid (struct target_ops *self, long lwp, long tid) |
0ef643c8 | 1136 | { |
e99b03dc | 1137 | return ptid_t (inferior_ptid.pid (), lwp, tid); |
0ef643c8 JB |
1138 | } |
1139 | ||
32231432 | 1140 | static enum exec_direction_kind |
4c612759 | 1141 | default_execution_direction (struct target_ops *self) |
32231432 | 1142 | { |
05374cfd | 1143 | if (!target_can_execute_reverse ()) |
32231432 PA |
1144 | return EXEC_FORWARD; |
1145 | else if (!target_can_async_p ()) | |
1146 | return EXEC_FORWARD; | |
1147 | else | |
1148 | gdb_assert_not_reached ("\ | |
1149 | to_execution_direction must be implemented for reverse async"); | |
1150 | } | |
1151 | ||
a1740ee1 | 1152 | /* See target.h. */ |
c906108c | 1153 | |
5b6d1e4f PA |
1154 | void |
1155 | decref_target (target_ops *t) | |
1156 | { | |
1157 | t->decref (); | |
1158 | if (t->refcount () == 0) | |
121b3efd PA |
1159 | { |
1160 | if (t->stratum () == process_stratum) | |
1161 | connection_list_remove (as_process_stratum_target (t)); | |
1162 | target_close (t); | |
1163 | } | |
5b6d1e4f PA |
1164 | } |
1165 | ||
1166 | /* See target.h. */ | |
1167 | ||
b26a4dcb | 1168 | void |
a1740ee1 | 1169 | target_stack::push (target_ops *t) |
c906108c | 1170 | { |
5b6d1e4f PA |
1171 | t->incref (); |
1172 | ||
66b4deae PA |
1173 | strata stratum = t->stratum (); |
1174 | ||
121b3efd PA |
1175 | if (stratum == process_stratum) |
1176 | connection_list_add (as_process_stratum_target (t)); | |
1177 | ||
5b6d1e4f PA |
1178 | /* If there's already a target at this stratum, remove it. */ |
1179 | ||
66b4deae | 1180 | if (m_stack[stratum] != NULL) |
494409bb | 1181 | unpush (m_stack[stratum]); |
c906108c | 1182 | |
a1740ee1 | 1183 | /* Now add the new one. */ |
66b4deae | 1184 | m_stack[stratum] = t; |
5d502164 | 1185 | |
66b4deae PA |
1186 | if (m_top < stratum) |
1187 | m_top = stratum; | |
a1740ee1 PA |
1188 | } |
1189 | ||
1190 | /* See target.h. */ | |
c906108c | 1191 | |
a1740ee1 PA |
1192 | bool |
1193 | target_stack::unpush (target_ops *t) | |
c906108c | 1194 | { |
1688cb29 TT |
1195 | gdb_assert (t != NULL); |
1196 | ||
66b4deae PA |
1197 | strata stratum = t->stratum (); |
1198 | ||
1199 | if (stratum == dummy_stratum) | |
c8d104ad | 1200 | internal_error (__FILE__, __LINE__, |
9b20d036 | 1201 | _("Attempt to unpush the dummy target")); |
c8d104ad | 1202 | |
a1740ee1 PA |
1203 | /* Look for the specified target. Note that a target can only occur |
1204 | once in the target stack. */ | |
c906108c | 1205 | |
66b4deae | 1206 | if (m_stack[stratum] != t) |
258b763a | 1207 | { |
a1740ee1 PA |
1208 | /* If T wasn't pushed, quit. Only open targets should be |
1209 | closed. */ | |
1210 | return false; | |
258b763a | 1211 | } |
c906108c | 1212 | |
c378eb4e | 1213 | /* Unchain the target. */ |
66b4deae | 1214 | m_stack[stratum] = NULL; |
a1740ee1 | 1215 | |
66b4deae | 1216 | if (m_top == stratum) |
bedc4734 | 1217 | m_top = this->find_beneath (t)->stratum (); |
c906108c | 1218 | |
5b6d1e4f PA |
1219 | /* Finally close the target, if there are no inferiors |
1220 | referencing this target still. Note we do this after unchaining, | |
1221 | so any target method calls from within the target_close | |
1222 | implementation don't end up in T anymore. Do leave the target | |
1223 | open if we have are other inferiors referencing this target | |
1224 | still. */ | |
1225 | decref_target (t); | |
305436e0 | 1226 | |
a1740ee1 | 1227 | return true; |
c906108c SS |
1228 | } |
1229 | ||
915ef8b1 PA |
1230 | /* Unpush TARGET and assert that it worked. */ |
1231 | ||
1232 | static void | |
1233 | unpush_target_and_assert (struct target_ops *target) | |
1234 | { | |
fadf6add | 1235 | if (!current_inferior ()->unpush_target (target)) |
915ef8b1 PA |
1236 | { |
1237 | fprintf_unfiltered (gdb_stderr, | |
1238 | "pop_all_targets couldn't find target %s\n", | |
f6ac5f3d | 1239 | target->shortname ()); |
915ef8b1 PA |
1240 | internal_error (__FILE__, __LINE__, |
1241 | _("failed internal consistency check")); | |
1242 | } | |
1243 | } | |
1244 | ||
aa76d38d | 1245 | void |
460014f5 | 1246 | pop_all_targets_above (enum strata above_stratum) |
aa76d38d | 1247 | { |
328d42d8 SM |
1248 | while ((int) (current_inferior ()->top_target ()->stratum ()) |
1249 | > (int) above_stratum) | |
1250 | unpush_target_and_assert (current_inferior ()->top_target ()); | |
915ef8b1 PA |
1251 | } |
1252 | ||
1253 | /* See target.h. */ | |
1254 | ||
1255 | void | |
1256 | pop_all_targets_at_and_above (enum strata stratum) | |
1257 | { | |
328d42d8 SM |
1258 | while ((int) (current_inferior ()->top_target ()->stratum ()) |
1259 | >= (int) stratum) | |
1260 | unpush_target_and_assert (current_inferior ()->top_target ()); | |
aa76d38d PA |
1261 | } |
1262 | ||
87ab71f0 | 1263 | void |
460014f5 | 1264 | pop_all_targets (void) |
87ab71f0 | 1265 | { |
460014f5 | 1266 | pop_all_targets_above (dummy_stratum); |
87ab71f0 PA |
1267 | } |
1268 | ||
fadf6add SM |
1269 | void |
1270 | target_unpusher::operator() (struct target_ops *ops) const | |
1271 | { | |
1272 | current_inferior ()->unpush_target (ops); | |
1273 | } | |
1274 | ||
f0f9ff95 TT |
1275 | /* Default implementation of to_get_thread_local_address. */ |
1276 | ||
1277 | static void | |
1278 | generic_tls_error (void) | |
1279 | { | |
1280 | throw_error (TLS_GENERIC_ERROR, | |
1281 | _("Cannot find thread-local variables on this target")); | |
1282 | } | |
1283 | ||
72f5cf0e | 1284 | /* Using the objfile specified in OBJFILE, find the address for the |
9e35dae4 DJ |
1285 | current thread's thread-local storage with offset OFFSET. */ |
1286 | CORE_ADDR | |
1287 | target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset) | |
1288 | { | |
1289 | volatile CORE_ADDR addr = 0; | |
328d42d8 | 1290 | struct target_ops *target = current_inferior ()->top_target (); |
6e056c81 | 1291 | struct gdbarch *gdbarch = target_gdbarch (); |
9e35dae4 | 1292 | |
6e056c81 | 1293 | if (gdbarch_fetch_tls_load_module_address_p (gdbarch)) |
9e35dae4 DJ |
1294 | { |
1295 | ptid_t ptid = inferior_ptid; | |
9e35dae4 | 1296 | |
a70b8144 | 1297 | try |
9e35dae4 DJ |
1298 | { |
1299 | CORE_ADDR lm_addr; | |
1300 | ||
1301 | /* Fetch the load module address for this objfile. */ | |
6e056c81 | 1302 | lm_addr = gdbarch_fetch_tls_load_module_address (gdbarch, |
dda83cd7 | 1303 | objfile); |
9e35dae4 | 1304 | |
6e056c81 JB |
1305 | if (gdbarch_get_thread_local_address_p (gdbarch)) |
1306 | addr = gdbarch_get_thread_local_address (gdbarch, ptid, lm_addr, | |
1307 | offset); | |
1308 | else | |
1309 | addr = target->get_thread_local_address (ptid, lm_addr, offset); | |
9e35dae4 DJ |
1310 | } |
1311 | /* If an error occurred, print TLS related messages here. Otherwise, | |
dda83cd7 | 1312 | throw the error to some higher catcher. */ |
230d2906 | 1313 | catch (const gdb_exception &ex) |
9e35dae4 DJ |
1314 | { |
1315 | int objfile_is_library = (objfile->flags & OBJF_SHARED); | |
1316 | ||
1317 | switch (ex.error) | |
1318 | { | |
1319 | case TLS_NO_LIBRARY_SUPPORT_ERROR: | |
3e43a32a MS |
1320 | error (_("Cannot find thread-local variables " |
1321 | "in this thread library.")); | |
9e35dae4 DJ |
1322 | break; |
1323 | case TLS_LOAD_MODULE_NOT_FOUND_ERROR: | |
1324 | if (objfile_is_library) | |
1325 | error (_("Cannot find shared library `%s' in dynamic" | |
dda83cd7 | 1326 | " linker's load module list"), objfile_name (objfile)); |
9e35dae4 DJ |
1327 | else |
1328 | error (_("Cannot find executable file `%s' in dynamic" | |
dda83cd7 | 1329 | " linker's load module list"), objfile_name (objfile)); |
9e35dae4 DJ |
1330 | break; |
1331 | case TLS_NOT_ALLOCATED_YET_ERROR: | |
1332 | if (objfile_is_library) | |
1333 | error (_("The inferior has not yet allocated storage for" | |
dda83cd7 SM |
1334 | " thread-local variables in\n" |
1335 | "the shared library `%s'\n" | |
1336 | "for %s"), | |
a068643d TT |
1337 | objfile_name (objfile), |
1338 | target_pid_to_str (ptid).c_str ()); | |
9e35dae4 DJ |
1339 | else |
1340 | error (_("The inferior has not yet allocated storage for" | |
dda83cd7 SM |
1341 | " thread-local variables in\n" |
1342 | "the executable `%s'\n" | |
1343 | "for %s"), | |
a068643d TT |
1344 | objfile_name (objfile), |
1345 | target_pid_to_str (ptid).c_str ()); | |
9e35dae4 DJ |
1346 | break; |
1347 | case TLS_GENERIC_ERROR: | |
1348 | if (objfile_is_library) | |
1349 | error (_("Cannot find thread-local storage for %s, " | |
dda83cd7 | 1350 | "shared library %s:\n%s"), |
a068643d | 1351 | target_pid_to_str (ptid).c_str (), |
3d6e9d23 | 1352 | objfile_name (objfile), ex.what ()); |
9e35dae4 DJ |
1353 | else |
1354 | error (_("Cannot find thread-local storage for %s, " | |
dda83cd7 | 1355 | "executable file %s:\n%s"), |
a068643d | 1356 | target_pid_to_str (ptid).c_str (), |
3d6e9d23 | 1357 | objfile_name (objfile), ex.what ()); |
9e35dae4 DJ |
1358 | break; |
1359 | default: | |
eedc3f4f | 1360 | throw; |
9e35dae4 DJ |
1361 | break; |
1362 | } | |
1363 | } | |
1364 | } | |
9e35dae4 DJ |
1365 | else |
1366 | error (_("Cannot find thread-local variables on this target")); | |
1367 | ||
1368 | return addr; | |
1369 | } | |
1370 | ||
6be7b56e | 1371 | const char * |
01cb8804 | 1372 | target_xfer_status_to_string (enum target_xfer_status status) |
6be7b56e PA |
1373 | { |
1374 | #define CASE(X) case X: return #X | |
01cb8804 | 1375 | switch (status) |
6be7b56e PA |
1376 | { |
1377 | CASE(TARGET_XFER_E_IO); | |
bc113b4e | 1378 | CASE(TARGET_XFER_UNAVAILABLE); |
6be7b56e PA |
1379 | default: |
1380 | return "<unknown>"; | |
1381 | } | |
1382 | #undef CASE | |
1383 | }; | |
1384 | ||
1385 | ||
66920317 | 1386 | /* See target.h. */ |
c906108c | 1387 | |
66920317 TT |
1388 | gdb::unique_xmalloc_ptr<char> |
1389 | target_read_string (CORE_ADDR memaddr, int len, int *bytes_read) | |
c906108c | 1390 | { |
670e35fa | 1391 | gdb::unique_xmalloc_ptr<gdb_byte> buffer; |
5d502164 | 1392 | |
66920317 TT |
1393 | int ignore; |
1394 | if (bytes_read == nullptr) | |
1395 | bytes_read = &ignore; | |
1396 | ||
670e35fa TT |
1397 | /* Note that the endian-ness does not matter here. */ |
1398 | int errcode = read_string (memaddr, -1, 1, len, BFD_ENDIAN_LITTLE, | |
66920317 TT |
1399 | &buffer, bytes_read); |
1400 | if (errcode != 0) | |
1401 | return {}; | |
670e35fa | 1402 | |
66920317 | 1403 | return gdb::unique_xmalloc_ptr<char> ((char *) buffer.release ()); |
c906108c SS |
1404 | } |
1405 | ||
19cf757a | 1406 | const target_section_table * |
07b82ea5 PA |
1407 | target_get_section_table (struct target_ops *target) |
1408 | { | |
f6ac5f3d | 1409 | return target->get_section_table (); |
07b82ea5 PA |
1410 | } |
1411 | ||
8db32d44 | 1412 | /* Find a section containing ADDR. */ |
07b82ea5 | 1413 | |
19cf757a | 1414 | const struct target_section * |
8db32d44 AC |
1415 | target_section_by_addr (struct target_ops *target, CORE_ADDR addr) |
1416 | { | |
19cf757a | 1417 | const target_section_table *table = target_get_section_table (target); |
07b82ea5 PA |
1418 | |
1419 | if (table == NULL) | |
1420 | return NULL; | |
1421 | ||
19cf757a | 1422 | for (const target_section &secp : *table) |
8db32d44 | 1423 | { |
bb2a6777 TT |
1424 | if (addr >= secp.addr && addr < secp.endaddr) |
1425 | return &secp; | |
8db32d44 AC |
1426 | } |
1427 | return NULL; | |
1428 | } | |
1429 | ||
336aa7b7 AB |
1430 | /* See target.h. */ |
1431 | ||
1432 | const target_section_table * | |
1433 | default_get_section_table () | |
1434 | { | |
1435 | return ¤t_program_space->target_sections (); | |
1436 | } | |
0fec99e8 PA |
1437 | |
1438 | /* Helper for the memory xfer routines. Checks the attributes of the | |
1439 | memory region of MEMADDR against the read or write being attempted. | |
1440 | If the access is permitted returns true, otherwise returns false. | |
1441 | REGION_P is an optional output parameter. If not-NULL, it is | |
1442 | filled with a pointer to the memory region of MEMADDR. REG_LEN | |
1443 | returns LEN trimmed to the end of the region. This is how much the | |
1444 | caller can continue requesting, if the access is permitted. A | |
1445 | single xfer request must not straddle memory region boundaries. */ | |
1446 | ||
1447 | static int | |
1448 | memory_xfer_check_region (gdb_byte *readbuf, const gdb_byte *writebuf, | |
1449 | ULONGEST memaddr, ULONGEST len, ULONGEST *reg_len, | |
1450 | struct mem_region **region_p) | |
1451 | { | |
1452 | struct mem_region *region; | |
1453 | ||
1454 | region = lookup_mem_region (memaddr); | |
1455 | ||
1456 | if (region_p != NULL) | |
1457 | *region_p = region; | |
1458 | ||
1459 | switch (region->attrib.mode) | |
1460 | { | |
1461 | case MEM_RO: | |
1462 | if (writebuf != NULL) | |
1463 | return 0; | |
1464 | break; | |
1465 | ||
1466 | case MEM_WO: | |
1467 | if (readbuf != NULL) | |
1468 | return 0; | |
1469 | break; | |
1470 | ||
1471 | case MEM_FLASH: | |
1472 | /* We only support writing to flash during "load" for now. */ | |
1473 | if (writebuf != NULL) | |
1474 | error (_("Writing to flash memory forbidden in this context")); | |
1475 | break; | |
1476 | ||
1477 | case MEM_NONE: | |
1478 | return 0; | |
1479 | } | |
1480 | ||
1481 | /* region->hi == 0 means there's no upper bound. */ | |
1482 | if (memaddr + len < region->hi || region->hi == 0) | |
1483 | *reg_len = len; | |
1484 | else | |
1485 | *reg_len = region->hi - memaddr; | |
1486 | ||
1487 | return 1; | |
1488 | } | |
1489 | ||
9f713294 YQ |
1490 | /* Read memory from more than one valid target. A core file, for |
1491 | instance, could have some of memory but delegate other bits to | |
1492 | the target below it. So, we must manually try all targets. */ | |
1493 | ||
cc9f16aa | 1494 | enum target_xfer_status |
17fde6d0 | 1495 | raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf, |
9b409511 YQ |
1496 | const gdb_byte *writebuf, ULONGEST memaddr, LONGEST len, |
1497 | ULONGEST *xfered_len) | |
9f713294 | 1498 | { |
9b409511 | 1499 | enum target_xfer_status res; |
9f713294 YQ |
1500 | |
1501 | do | |
1502 | { | |
f6ac5f3d PA |
1503 | res = ops->xfer_partial (TARGET_OBJECT_MEMORY, NULL, |
1504 | readbuf, writebuf, memaddr, len, | |
1505 | xfered_len); | |
9b409511 | 1506 | if (res == TARGET_XFER_OK) |
9f713294 YQ |
1507 | break; |
1508 | ||
633785ff | 1509 | /* Stop if the target reports that the memory is not available. */ |
bc113b4e | 1510 | if (res == TARGET_XFER_UNAVAILABLE) |
633785ff MM |
1511 | break; |
1512 | ||
2735d421 | 1513 | /* Don't continue past targets which have all the memory. |
dda83cd7 | 1514 | At one time, this code was necessary to read data from |
2735d421 KB |
1515 | executables / shared libraries when data for the requested |
1516 | addresses weren't available in the core file. But now the | |
1517 | core target handles this case itself. */ | |
f6ac5f3d | 1518 | if (ops->has_all_memory ()) |
9f713294 YQ |
1519 | break; |
1520 | ||
b6a8c27b | 1521 | ops = ops->beneath (); |
9f713294 YQ |
1522 | } |
1523 | while (ops != NULL); | |
1524 | ||
0f26cec1 PA |
1525 | /* The cache works at the raw memory level. Make sure the cache |
1526 | gets updated with raw contents no matter what kind of memory | |
1527 | object was originally being written. Note we do write-through | |
1528 | first, so that if it fails, we don't write to the cache contents | |
1529 | that never made it to the target. */ | |
1530 | if (writebuf != NULL | |
d7e15655 | 1531 | && inferior_ptid != null_ptid |
0f26cec1 PA |
1532 | && target_dcache_init_p () |
1533 | && (stack_cache_enabled_p () || code_cache_enabled_p ())) | |
1534 | { | |
1535 | DCACHE *dcache = target_dcache_get (); | |
1536 | ||
1537 | /* Note that writing to an area of memory which wasn't present | |
1538 | in the cache doesn't cause it to be loaded in. */ | |
1539 | dcache_update (dcache, res, memaddr, writebuf, *xfered_len); | |
1540 | } | |
1541 | ||
9f713294 YQ |
1542 | return res; |
1543 | } | |
1544 | ||
7f79c47e DE |
1545 | /* Perform a partial memory transfer. |
1546 | For docs see target.h, to_xfer_partial. */ | |
cf7a04e8 | 1547 | |
9b409511 | 1548 | static enum target_xfer_status |
f0ba3972 | 1549 | memory_xfer_partial_1 (struct target_ops *ops, enum target_object object, |
17fde6d0 | 1550 | gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST memaddr, |
9b409511 | 1551 | ULONGEST len, ULONGEST *xfered_len) |
0779438d | 1552 | { |
9b409511 | 1553 | enum target_xfer_status res; |
0fec99e8 | 1554 | ULONGEST reg_len; |
cf7a04e8 | 1555 | struct mem_region *region; |
4e5d721f | 1556 | struct inferior *inf; |
cf7a04e8 | 1557 | |
07b82ea5 PA |
1558 | /* For accesses to unmapped overlay sections, read directly from |
1559 | files. Must do this first, as MEMADDR may need adjustment. */ | |
1560 | if (readbuf != NULL && overlay_debugging) | |
1561 | { | |
1562 | struct obj_section *section = find_pc_overlay (memaddr); | |
5d502164 | 1563 | |
07b82ea5 PA |
1564 | if (pc_in_unmapped_range (memaddr, section)) |
1565 | { | |
19cf757a | 1566 | const target_section_table *table = target_get_section_table (ops); |
07b82ea5 | 1567 | const char *section_name = section->the_bfd_section->name; |
5d502164 | 1568 | |
07b82ea5 | 1569 | memaddr = overlay_mapped_address (memaddr, section); |
e56cb451 KB |
1570 | |
1571 | auto match_cb = [=] (const struct target_section *s) | |
1572 | { | |
1573 | return (strcmp (section_name, s->the_bfd_section->name) == 0); | |
1574 | }; | |
1575 | ||
07b82ea5 | 1576 | return section_table_xfer_memory_partial (readbuf, writebuf, |
9b409511 | 1577 | memaddr, len, xfered_len, |
bb2a6777 | 1578 | *table, match_cb); |
07b82ea5 PA |
1579 | } |
1580 | } | |
1581 | ||
1582 | /* Try the executable files, if "trust-readonly-sections" is set. */ | |
cf7a04e8 DJ |
1583 | if (readbuf != NULL && trust_readonly) |
1584 | { | |
19cf757a AB |
1585 | const struct target_section *secp |
1586 | = target_section_by_addr (ops, memaddr); | |
cf7a04e8 | 1587 | if (secp != NULL |
fd361982 | 1588 | && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY)) |
07b82ea5 | 1589 | { |
19cf757a | 1590 | const target_section_table *table = target_get_section_table (ops); |
07b82ea5 | 1591 | return section_table_xfer_memory_partial (readbuf, writebuf, |
9b409511 | 1592 | memaddr, len, xfered_len, |
bb2a6777 | 1593 | *table); |
07b82ea5 | 1594 | } |
98646950 UW |
1595 | } |
1596 | ||
cf7a04e8 | 1597 | /* Try GDB's internal data cache. */ |
cf7a04e8 | 1598 | |
0fec99e8 PA |
1599 | if (!memory_xfer_check_region (readbuf, writebuf, memaddr, len, ®_len, |
1600 | ®ion)) | |
1601 | return TARGET_XFER_E_IO; | |
cf7a04e8 | 1602 | |
d7e15655 | 1603 | if (inferior_ptid != null_ptid) |
00431a78 | 1604 | inf = current_inferior (); |
6c95b8df PA |
1605 | else |
1606 | inf = NULL; | |
4e5d721f DE |
1607 | |
1608 | if (inf != NULL | |
0f26cec1 | 1609 | && readbuf != NULL |
2f4d8875 PA |
1610 | /* The dcache reads whole cache lines; that doesn't play well |
1611 | with reading from a trace buffer, because reading outside of | |
1612 | the collected memory range fails. */ | |
1613 | && get_traceframe_number () == -1 | |
4e5d721f | 1614 | && (region->attrib.cache |
29453a14 YQ |
1615 | || (stack_cache_enabled_p () && object == TARGET_OBJECT_STACK_MEMORY) |
1616 | || (code_cache_enabled_p () && object == TARGET_OBJECT_CODE_MEMORY))) | |
cf7a04e8 | 1617 | { |
2a2f9fe4 YQ |
1618 | DCACHE *dcache = target_dcache_get_or_init (); |
1619 | ||
0f26cec1 PA |
1620 | return dcache_read_memory_partial (ops, dcache, memaddr, readbuf, |
1621 | reg_len, xfered_len); | |
cf7a04e8 DJ |
1622 | } |
1623 | ||
1624 | /* If none of those methods found the memory we wanted, fall back | |
1625 | to a target partial transfer. Normally a single call to | |
1626 | to_xfer_partial is enough; if it doesn't recognize an object | |
1627 | it will call the to_xfer_partial of the next target down. | |
1628 | But for memory this won't do. Memory is the only target | |
9b409511 YQ |
1629 | object which can be read from more than one valid target. |
1630 | A core file, for instance, could have some of memory but | |
1631 | delegate other bits to the target below it. So, we must | |
1632 | manually try all targets. */ | |
1633 | ||
1634 | res = raw_memory_xfer_partial (ops, readbuf, writebuf, memaddr, reg_len, | |
1635 | xfered_len); | |
cf7a04e8 DJ |
1636 | |
1637 | /* If we still haven't got anything, return the last error. We | |
1638 | give up. */ | |
1639 | return res; | |
0779438d AC |
1640 | } |
1641 | ||
f0ba3972 PA |
1642 | /* Perform a partial memory transfer. For docs see target.h, |
1643 | to_xfer_partial. */ | |
1644 | ||
9b409511 | 1645 | static enum target_xfer_status |
f0ba3972 | 1646 | memory_xfer_partial (struct target_ops *ops, enum target_object object, |
9b409511 YQ |
1647 | gdb_byte *readbuf, const gdb_byte *writebuf, |
1648 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) | |
f0ba3972 | 1649 | { |
9b409511 | 1650 | enum target_xfer_status res; |
f0ba3972 PA |
1651 | |
1652 | /* Zero length requests are ok and require no work. */ | |
1653 | if (len == 0) | |
9b409511 | 1654 | return TARGET_XFER_EOF; |
f0ba3972 | 1655 | |
a738ea1d YQ |
1656 | memaddr = address_significant (target_gdbarch (), memaddr); |
1657 | ||
f0ba3972 PA |
1658 | /* Fill in READBUF with breakpoint shadows, or WRITEBUF with |
1659 | breakpoint insns, thus hiding out from higher layers whether | |
1660 | there are software breakpoints inserted in the code stream. */ | |
1661 | if (readbuf != NULL) | |
1662 | { | |
9b409511 YQ |
1663 | res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len, |
1664 | xfered_len); | |
f0ba3972 | 1665 | |
9b409511 | 1666 | if (res == TARGET_XFER_OK && !show_memory_breakpoints) |
c63528fc | 1667 | breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, *xfered_len); |
f0ba3972 PA |
1668 | } |
1669 | else | |
1670 | { | |
67c059c2 AB |
1671 | /* A large write request is likely to be partially satisfied |
1672 | by memory_xfer_partial_1. We will continually malloc | |
1673 | and free a copy of the entire write request for breakpoint | |
1674 | shadow handling even though we only end up writing a small | |
09c98b44 DB |
1675 | subset of it. Cap writes to a limit specified by the target |
1676 | to mitigate this. */ | |
f6ac5f3d | 1677 | len = std::min (ops->get_memory_xfer_limit (), len); |
67c059c2 | 1678 | |
26fcd5d7 TT |
1679 | gdb::byte_vector buf (writebuf, writebuf + len); |
1680 | breakpoint_xfer_memory (NULL, buf.data (), writebuf, memaddr, len); | |
1681 | res = memory_xfer_partial_1 (ops, object, NULL, buf.data (), memaddr, len, | |
9b409511 | 1682 | xfered_len); |
f0ba3972 PA |
1683 | } |
1684 | ||
1685 | return res; | |
1686 | } | |
1687 | ||
cb85b21b TT |
1688 | scoped_restore_tmpl<int> |
1689 | make_scoped_restore_show_memory_breakpoints (int show) | |
8defab1a | 1690 | { |
cb85b21b | 1691 | return make_scoped_restore (&show_memory_breakpoints, show); |
8defab1a DJ |
1692 | } |
1693 | ||
7f79c47e DE |
1694 | /* For docs see target.h, to_xfer_partial. */ |
1695 | ||
9b409511 | 1696 | enum target_xfer_status |
27394598 AC |
1697 | target_xfer_partial (struct target_ops *ops, |
1698 | enum target_object object, const char *annex, | |
4ac248ca | 1699 | gdb_byte *readbuf, const gdb_byte *writebuf, |
9b409511 YQ |
1700 | ULONGEST offset, ULONGEST len, |
1701 | ULONGEST *xfered_len) | |
27394598 | 1702 | { |
9b409511 | 1703 | enum target_xfer_status retval; |
27394598 | 1704 | |
ce6d0892 YQ |
1705 | /* Transfer is done when LEN is zero. */ |
1706 | if (len == 0) | |
9b409511 | 1707 | return TARGET_XFER_EOF; |
ce6d0892 | 1708 | |
d914c394 SS |
1709 | if (writebuf && !may_write_memory) |
1710 | error (_("Writing to memory is not allowed (addr %s, len %s)"), | |
1711 | core_addr_to_string_nz (offset), plongest (len)); | |
1712 | ||
9b409511 YQ |
1713 | *xfered_len = 0; |
1714 | ||
cf7a04e8 DJ |
1715 | /* If this is a memory transfer, let the memory-specific code |
1716 | have a look at it instead. Memory transfers are more | |
1717 | complicated. */ | |
29453a14 YQ |
1718 | if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY |
1719 | || object == TARGET_OBJECT_CODE_MEMORY) | |
4e5d721f | 1720 | retval = memory_xfer_partial (ops, object, readbuf, |
9b409511 | 1721 | writebuf, offset, len, xfered_len); |
9f713294 | 1722 | else if (object == TARGET_OBJECT_RAW_MEMORY) |
cf7a04e8 | 1723 | { |
0fec99e8 PA |
1724 | /* Skip/avoid accessing the target if the memory region |
1725 | attributes block the access. Check this here instead of in | |
1726 | raw_memory_xfer_partial as otherwise we'd end up checking | |
1727 | this twice in the case of the memory_xfer_partial path is | |
1728 | taken; once before checking the dcache, and another in the | |
1729 | tail call to raw_memory_xfer_partial. */ | |
1730 | if (!memory_xfer_check_region (readbuf, writebuf, offset, len, &len, | |
1731 | NULL)) | |
1732 | return TARGET_XFER_E_IO; | |
1733 | ||
9f713294 | 1734 | /* Request the normal memory object from other layers. */ |
9b409511 YQ |
1735 | retval = raw_memory_xfer_partial (ops, readbuf, writebuf, offset, len, |
1736 | xfered_len); | |
cf7a04e8 | 1737 | } |
9f713294 | 1738 | else |
f6ac5f3d PA |
1739 | retval = ops->xfer_partial (object, annex, readbuf, |
1740 | writebuf, offset, len, xfered_len); | |
cf7a04e8 | 1741 | |
27394598 AC |
1742 | if (targetdebug) |
1743 | { | |
1744 | const unsigned char *myaddr = NULL; | |
1745 | ||
1746 | fprintf_unfiltered (gdb_stdlog, | |
3e43a32a | 1747 | "%s:target_xfer_partial " |
9b409511 | 1748 | "(%d, %s, %s, %s, %s, %s) = %d, %s", |
f6ac5f3d | 1749 | ops->shortname (), |
27394598 AC |
1750 | (int) object, |
1751 | (annex ? annex : "(null)"), | |
53b71562 JB |
1752 | host_address_to_string (readbuf), |
1753 | host_address_to_string (writebuf), | |
0b1553bc | 1754 | core_addr_to_string_nz (offset), |
9b409511 YQ |
1755 | pulongest (len), retval, |
1756 | pulongest (*xfered_len)); | |
27394598 AC |
1757 | |
1758 | if (readbuf) | |
1759 | myaddr = readbuf; | |
1760 | if (writebuf) | |
1761 | myaddr = writebuf; | |
9b409511 | 1762 | if (retval == TARGET_XFER_OK && myaddr != NULL) |
27394598 AC |
1763 | { |
1764 | int i; | |
2bc416ba | 1765 | |
27394598 | 1766 | fputs_unfiltered (", bytes =", gdb_stdlog); |
9b409511 | 1767 | for (i = 0; i < *xfered_len; i++) |
27394598 | 1768 | { |
53b71562 | 1769 | if ((((intptr_t) &(myaddr[i])) & 0xf) == 0) |
27394598 AC |
1770 | { |
1771 | if (targetdebug < 2 && i > 0) | |
1772 | { | |
1773 | fprintf_unfiltered (gdb_stdlog, " ..."); | |
1774 | break; | |
1775 | } | |
1776 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
1777 | } | |
2bc416ba | 1778 | |
27394598 AC |
1779 | fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff); |
1780 | } | |
1781 | } | |
2bc416ba | 1782 | |
27394598 AC |
1783 | fputc_unfiltered ('\n', gdb_stdlog); |
1784 | } | |
9b409511 YQ |
1785 | |
1786 | /* Check implementations of to_xfer_partial update *XFERED_LEN | |
1787 | properly. Do assertion after printing debug messages, so that we | |
1788 | can find more clues on assertion failure from debugging messages. */ | |
bc113b4e | 1789 | if (retval == TARGET_XFER_OK || retval == TARGET_XFER_UNAVAILABLE) |
9b409511 YQ |
1790 | gdb_assert (*xfered_len > 0); |
1791 | ||
27394598 AC |
1792 | return retval; |
1793 | } | |
1794 | ||
578d3588 PA |
1795 | /* Read LEN bytes of target memory at address MEMADDR, placing the |
1796 | results in GDB's memory at MYADDR. Returns either 0 for success or | |
d09f2c3f | 1797 | -1 if any error occurs. |
c906108c SS |
1798 | |
1799 | If an error occurs, no guarantee is made about the contents of the data at | |
1800 | MYADDR. In particular, the caller should not depend upon partial reads | |
1801 | filling the buffer with good data. There is no way for the caller to know | |
1802 | how much good data might have been transfered anyway. Callers that can | |
cf7a04e8 | 1803 | deal with partial reads should call target_read (which will retry until |
c378eb4e | 1804 | it makes no progress, and then return how much was transferred). */ |
c906108c SS |
1805 | |
1806 | int | |
1b162304 | 1807 | target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) |
c906108c | 1808 | { |
328d42d8 SM |
1809 | if (target_read (current_inferior ()->top_target (), |
1810 | TARGET_OBJECT_MEMORY, NULL, | |
cf7a04e8 DJ |
1811 | myaddr, memaddr, len) == len) |
1812 | return 0; | |
0779438d | 1813 | else |
d09f2c3f | 1814 | return -1; |
c906108c SS |
1815 | } |
1816 | ||
721ec300 GB |
1817 | /* See target/target.h. */ |
1818 | ||
1819 | int | |
1820 | target_read_uint32 (CORE_ADDR memaddr, uint32_t *result) | |
1821 | { | |
1822 | gdb_byte buf[4]; | |
1823 | int r; | |
1824 | ||
1825 | r = target_read_memory (memaddr, buf, sizeof buf); | |
1826 | if (r != 0) | |
1827 | return r; | |
1828 | *result = extract_unsigned_integer (buf, sizeof buf, | |
1829 | gdbarch_byte_order (target_gdbarch ())); | |
1830 | return 0; | |
1831 | } | |
1832 | ||
aee4bf85 PA |
1833 | /* Like target_read_memory, but specify explicitly that this is a read |
1834 | from the target's raw memory. That is, this read bypasses the | |
1835 | dcache, breakpoint shadowing, etc. */ | |
1836 | ||
1837 | int | |
1838 | target_read_raw_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) | |
1839 | { | |
328d42d8 SM |
1840 | if (target_read (current_inferior ()->top_target (), |
1841 | TARGET_OBJECT_RAW_MEMORY, NULL, | |
aee4bf85 PA |
1842 | myaddr, memaddr, len) == len) |
1843 | return 0; | |
1844 | else | |
d09f2c3f | 1845 | return -1; |
aee4bf85 PA |
1846 | } |
1847 | ||
4e5d721f DE |
1848 | /* Like target_read_memory, but specify explicitly that this is a read from |
1849 | the target's stack. This may trigger different cache behavior. */ | |
1850 | ||
1851 | int | |
45aa4659 | 1852 | target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) |
4e5d721f | 1853 | { |
328d42d8 SM |
1854 | if (target_read (current_inferior ()->top_target (), |
1855 | TARGET_OBJECT_STACK_MEMORY, NULL, | |
4e5d721f DE |
1856 | myaddr, memaddr, len) == len) |
1857 | return 0; | |
1858 | else | |
d09f2c3f | 1859 | return -1; |
4e5d721f DE |
1860 | } |
1861 | ||
29453a14 YQ |
1862 | /* Like target_read_memory, but specify explicitly that this is a read from |
1863 | the target's code. This may trigger different cache behavior. */ | |
1864 | ||
1865 | int | |
1866 | target_read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) | |
1867 | { | |
328d42d8 SM |
1868 | if (target_read (current_inferior ()->top_target (), |
1869 | TARGET_OBJECT_CODE_MEMORY, NULL, | |
29453a14 YQ |
1870 | myaddr, memaddr, len) == len) |
1871 | return 0; | |
1872 | else | |
d09f2c3f | 1873 | return -1; |
29453a14 YQ |
1874 | } |
1875 | ||
7f79c47e | 1876 | /* Write LEN bytes from MYADDR to target memory at address MEMADDR. |
d09f2c3f PA |
1877 | Returns either 0 for success or -1 if any error occurs. If an |
1878 | error occurs, no guarantee is made about how much data got written. | |
1879 | Callers that can deal with partial writes should call | |
1880 | target_write. */ | |
7f79c47e | 1881 | |
c906108c | 1882 | int |
45aa4659 | 1883 | target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len) |
c906108c | 1884 | { |
328d42d8 SM |
1885 | if (target_write (current_inferior ()->top_target (), |
1886 | TARGET_OBJECT_MEMORY, NULL, | |
cf7a04e8 DJ |
1887 | myaddr, memaddr, len) == len) |
1888 | return 0; | |
0779438d | 1889 | else |
d09f2c3f | 1890 | return -1; |
c906108c | 1891 | } |
c5aa993b | 1892 | |
f0ba3972 | 1893 | /* Write LEN bytes from MYADDR to target raw memory at address |
d09f2c3f PA |
1894 | MEMADDR. Returns either 0 for success or -1 if any error occurs. |
1895 | If an error occurs, no guarantee is made about how much data got | |
1896 | written. Callers that can deal with partial writes should call | |
1897 | target_write. */ | |
f0ba3972 PA |
1898 | |
1899 | int | |
45aa4659 | 1900 | target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len) |
f0ba3972 | 1901 | { |
328d42d8 SM |
1902 | if (target_write (current_inferior ()->top_target (), |
1903 | TARGET_OBJECT_RAW_MEMORY, NULL, | |
f0ba3972 PA |
1904 | myaddr, memaddr, len) == len) |
1905 | return 0; | |
1906 | else | |
d09f2c3f | 1907 | return -1; |
f0ba3972 PA |
1908 | } |
1909 | ||
fd79ecee DJ |
1910 | /* Fetch the target's memory map. */ |
1911 | ||
a664f67e | 1912 | std::vector<mem_region> |
fd79ecee DJ |
1913 | target_memory_map (void) |
1914 | { | |
328d42d8 SM |
1915 | target_ops *target = current_inferior ()->top_target (); |
1916 | std::vector<mem_region> result = target->memory_map (); | |
a664f67e SM |
1917 | if (result.empty ()) |
1918 | return result; | |
fd79ecee | 1919 | |
a664f67e | 1920 | std::sort (result.begin (), result.end ()); |
fd79ecee DJ |
1921 | |
1922 | /* Check that regions do not overlap. Simultaneously assign | |
1923 | a numbering for the "mem" commands to use to refer to | |
1924 | each region. */ | |
a664f67e SM |
1925 | mem_region *last_one = NULL; |
1926 | for (size_t ix = 0; ix < result.size (); ix++) | |
fd79ecee | 1927 | { |
a664f67e | 1928 | mem_region *this_one = &result[ix]; |
fd79ecee DJ |
1929 | this_one->number = ix; |
1930 | ||
a664f67e | 1931 | if (last_one != NULL && last_one->hi > this_one->lo) |
fd79ecee DJ |
1932 | { |
1933 | warning (_("Overlapping regions in memory map: ignoring")); | |
a664f67e | 1934 | return std::vector<mem_region> (); |
fd79ecee | 1935 | } |
a664f67e | 1936 | |
fd79ecee DJ |
1937 | last_one = this_one; |
1938 | } | |
1939 | ||
1940 | return result; | |
1941 | } | |
1942 | ||
a76d924d DJ |
1943 | void |
1944 | target_flash_erase (ULONGEST address, LONGEST length) | |
1945 | { | |
328d42d8 | 1946 | current_inferior ()->top_target ()->flash_erase (address, length); |
a76d924d DJ |
1947 | } |
1948 | ||
1949 | void | |
1950 | target_flash_done (void) | |
1951 | { | |
328d42d8 | 1952 | current_inferior ()->top_target ()->flash_done (); |
a76d924d DJ |
1953 | } |
1954 | ||
920d2a44 AC |
1955 | static void |
1956 | show_trust_readonly (struct ui_file *file, int from_tty, | |
1957 | struct cmd_list_element *c, const char *value) | |
1958 | { | |
3e43a32a MS |
1959 | fprintf_filtered (file, |
1960 | _("Mode for reading from readonly sections is %s.\n"), | |
920d2a44 AC |
1961 | value); |
1962 | } | |
3a11626d | 1963 | |
7f79c47e | 1964 | /* Target vector read/write partial wrapper functions. */ |
0088c768 | 1965 | |
9b409511 | 1966 | static enum target_xfer_status |
1e3ff5ad AC |
1967 | target_read_partial (struct target_ops *ops, |
1968 | enum target_object object, | |
1b0ba102 | 1969 | const char *annex, gdb_byte *buf, |
9b409511 YQ |
1970 | ULONGEST offset, ULONGEST len, |
1971 | ULONGEST *xfered_len) | |
1e3ff5ad | 1972 | { |
9b409511 YQ |
1973 | return target_xfer_partial (ops, object, annex, buf, NULL, offset, len, |
1974 | xfered_len); | |
1e3ff5ad AC |
1975 | } |
1976 | ||
8a55ffb0 | 1977 | static enum target_xfer_status |
1e3ff5ad AC |
1978 | target_write_partial (struct target_ops *ops, |
1979 | enum target_object object, | |
1b0ba102 | 1980 | const char *annex, const gdb_byte *buf, |
9b409511 | 1981 | ULONGEST offset, LONGEST len, ULONGEST *xfered_len) |
1e3ff5ad | 1982 | { |
9b409511 YQ |
1983 | return target_xfer_partial (ops, object, annex, NULL, buf, offset, len, |
1984 | xfered_len); | |
1e3ff5ad AC |
1985 | } |
1986 | ||
1987 | /* Wrappers to perform the full transfer. */ | |
7f79c47e DE |
1988 | |
1989 | /* For docs on target_read see target.h. */ | |
1990 | ||
1e3ff5ad AC |
1991 | LONGEST |
1992 | target_read (struct target_ops *ops, | |
1993 | enum target_object object, | |
1b0ba102 | 1994 | const char *annex, gdb_byte *buf, |
1e3ff5ad AC |
1995 | ULONGEST offset, LONGEST len) |
1996 | { | |
279a6fed | 1997 | LONGEST xfered_total = 0; |
d309493c SM |
1998 | int unit_size = 1; |
1999 | ||
2000 | /* If we are reading from a memory object, find the length of an addressable | |
2001 | unit for that architecture. */ | |
2002 | if (object == TARGET_OBJECT_MEMORY | |
2003 | || object == TARGET_OBJECT_STACK_MEMORY | |
2004 | || object == TARGET_OBJECT_CODE_MEMORY | |
2005 | || object == TARGET_OBJECT_RAW_MEMORY) | |
2006 | unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ()); | |
5d502164 | 2007 | |
279a6fed | 2008 | while (xfered_total < len) |
1e3ff5ad | 2009 | { |
279a6fed | 2010 | ULONGEST xfered_partial; |
9b409511 YQ |
2011 | enum target_xfer_status status; |
2012 | ||
2013 | status = target_read_partial (ops, object, annex, | |
d309493c | 2014 | buf + xfered_total * unit_size, |
279a6fed SM |
2015 | offset + xfered_total, len - xfered_total, |
2016 | &xfered_partial); | |
5d502164 | 2017 | |
1e3ff5ad | 2018 | /* Call an observer, notifying them of the xfer progress? */ |
9b409511 | 2019 | if (status == TARGET_XFER_EOF) |
279a6fed | 2020 | return xfered_total; |
9b409511 YQ |
2021 | else if (status == TARGET_XFER_OK) |
2022 | { | |
279a6fed | 2023 | xfered_total += xfered_partial; |
9b409511 YQ |
2024 | QUIT; |
2025 | } | |
2026 | else | |
279a6fed | 2027 | return TARGET_XFER_E_IO; |
9b409511 | 2028 | |
1e3ff5ad AC |
2029 | } |
2030 | return len; | |
2031 | } | |
2032 | ||
f1a507a1 JB |
2033 | /* Assuming that the entire [begin, end) range of memory cannot be |
2034 | read, try to read whatever subrange is possible to read. | |
2035 | ||
2036 | The function returns, in RESULT, either zero or one memory block. | |
2037 | If there's a readable subrange at the beginning, it is completely | |
2038 | read and returned. Any further readable subrange will not be read. | |
2039 | Otherwise, if there's a readable subrange at the end, it will be | |
2040 | completely read and returned. Any readable subranges before it | |
2041 | (obviously, not starting at the beginning), will be ignored. In | |
2042 | other cases -- either no readable subrange, or readable subrange(s) | |
2043 | that is neither at the beginning, or end, nothing is returned. | |
2044 | ||
2045 | The purpose of this function is to handle a read across a boundary | |
2046 | of accessible memory in a case when memory map is not available. | |
2047 | The above restrictions are fine for this case, but will give | |
2048 | incorrect results if the memory is 'patchy'. However, supporting | |
2049 | 'patchy' memory would require trying to read every single byte, | |
2050 | and it seems unacceptable solution. Explicit memory map is | |
2051 | recommended for this case -- and target_read_memory_robust will | |
2052 | take care of reading multiple ranges then. */ | |
8dedea02 VP |
2053 | |
2054 | static void | |
3e43a32a | 2055 | read_whatever_is_readable (struct target_ops *ops, |
279a6fed | 2056 | const ULONGEST begin, const ULONGEST end, |
d309493c | 2057 | int unit_size, |
386c8614 | 2058 | std::vector<memory_read_result> *result) |
d5086790 | 2059 | { |
8dedea02 VP |
2060 | ULONGEST current_begin = begin; |
2061 | ULONGEST current_end = end; | |
2062 | int forward; | |
9b409511 | 2063 | ULONGEST xfered_len; |
8dedea02 VP |
2064 | |
2065 | /* If we previously failed to read 1 byte, nothing can be done here. */ | |
2066 | if (end - begin <= 1) | |
386c8614 TT |
2067 | return; |
2068 | ||
2069 | gdb::unique_xmalloc_ptr<gdb_byte> buf ((gdb_byte *) xmalloc (end - begin)); | |
8dedea02 VP |
2070 | |
2071 | /* Check that either first or the last byte is readable, and give up | |
c378eb4e | 2072 | if not. This heuristic is meant to permit reading accessible memory |
8dedea02 VP |
2073 | at the boundary of accessible region. */ |
2074 | if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL, | |
386c8614 | 2075 | buf.get (), begin, 1, &xfered_len) == TARGET_XFER_OK) |
8dedea02 VP |
2076 | { |
2077 | forward = 1; | |
2078 | ++current_begin; | |
2079 | } | |
2080 | else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL, | |
386c8614 | 2081 | buf.get () + (end - begin) - 1, end - 1, 1, |
9b409511 | 2082 | &xfered_len) == TARGET_XFER_OK) |
8dedea02 VP |
2083 | { |
2084 | forward = 0; | |
2085 | --current_end; | |
2086 | } | |
2087 | else | |
386c8614 | 2088 | return; |
8dedea02 VP |
2089 | |
2090 | /* Loop invariant is that the [current_begin, current_end) was previously | |
2091 | found to be not readable as a whole. | |
2092 | ||
2093 | Note loop condition -- if the range has 1 byte, we can't divide the range | |
2094 | so there's no point trying further. */ | |
2095 | while (current_end - current_begin > 1) | |
2096 | { | |
2097 | ULONGEST first_half_begin, first_half_end; | |
2098 | ULONGEST second_half_begin, second_half_end; | |
2099 | LONGEST xfer; | |
279a6fed | 2100 | ULONGEST middle = current_begin + (current_end - current_begin) / 2; |
f1a507a1 | 2101 | |
8dedea02 VP |
2102 | if (forward) |
2103 | { | |
2104 | first_half_begin = current_begin; | |
2105 | first_half_end = middle; | |
2106 | second_half_begin = middle; | |
2107 | second_half_end = current_end; | |
2108 | } | |
2109 | else | |
2110 | { | |
2111 | first_half_begin = middle; | |
2112 | first_half_end = current_end; | |
2113 | second_half_begin = current_begin; | |
2114 | second_half_end = middle; | |
2115 | } | |
2116 | ||
2117 | xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL, | |
386c8614 | 2118 | buf.get () + (first_half_begin - begin) * unit_size, |
8dedea02 VP |
2119 | first_half_begin, |
2120 | first_half_end - first_half_begin); | |
2121 | ||
2122 | if (xfer == first_half_end - first_half_begin) | |
2123 | { | |
c378eb4e | 2124 | /* This half reads up fine. So, the error must be in the |
3e43a32a | 2125 | other half. */ |
8dedea02 VP |
2126 | current_begin = second_half_begin; |
2127 | current_end = second_half_end; | |
2128 | } | |
2129 | else | |
2130 | { | |
c378eb4e | 2131 | /* This half is not readable. Because we've tried one byte, we |
279a6fed | 2132 | know some part of this half if actually readable. Go to the next |
8dedea02 VP |
2133 | iteration to divide again and try to read. |
2134 | ||
2135 | We don't handle the other half, because this function only tries | |
2136 | to read a single readable subrange. */ | |
2137 | current_begin = first_half_begin; | |
2138 | current_end = first_half_end; | |
2139 | } | |
2140 | } | |
2141 | ||
2142 | if (forward) | |
2143 | { | |
2144 | /* The [begin, current_begin) range has been read. */ | |
386c8614 | 2145 | result->emplace_back (begin, current_end, std::move (buf)); |
8dedea02 VP |
2146 | } |
2147 | else | |
2148 | { | |
2149 | /* The [current_end, end) range has been read. */ | |
279a6fed | 2150 | LONGEST region_len = end - current_end; |
f1a507a1 | 2151 | |
386c8614 TT |
2152 | gdb::unique_xmalloc_ptr<gdb_byte> data |
2153 | ((gdb_byte *) xmalloc (region_len * unit_size)); | |
2154 | memcpy (data.get (), buf.get () + (current_end - begin) * unit_size, | |
d309493c | 2155 | region_len * unit_size); |
386c8614 | 2156 | result->emplace_back (current_end, end, std::move (data)); |
8dedea02 | 2157 | } |
8dedea02 VP |
2158 | } |
2159 | ||
386c8614 | 2160 | std::vector<memory_read_result> |
279a6fed SM |
2161 | read_memory_robust (struct target_ops *ops, |
2162 | const ULONGEST offset, const LONGEST len) | |
8dedea02 | 2163 | { |
386c8614 | 2164 | std::vector<memory_read_result> result; |
d309493c | 2165 | int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ()); |
8dedea02 | 2166 | |
279a6fed SM |
2167 | LONGEST xfered_total = 0; |
2168 | while (xfered_total < len) | |
d5086790 | 2169 | { |
279a6fed SM |
2170 | struct mem_region *region = lookup_mem_region (offset + xfered_total); |
2171 | LONGEST region_len; | |
5d502164 | 2172 | |
8dedea02 VP |
2173 | /* If there is no explicit region, a fake one should be created. */ |
2174 | gdb_assert (region); | |
2175 | ||
2176 | if (region->hi == 0) | |
279a6fed | 2177 | region_len = len - xfered_total; |
8dedea02 | 2178 | else |
279a6fed | 2179 | region_len = region->hi - offset; |
8dedea02 VP |
2180 | |
2181 | if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO) | |
d5086790 | 2182 | { |
c378eb4e | 2183 | /* Cannot read this region. Note that we can end up here only |
8dedea02 VP |
2184 | if the region is explicitly marked inaccessible, or |
2185 | 'inaccessible-by-default' is in effect. */ | |
279a6fed | 2186 | xfered_total += region_len; |
8dedea02 VP |
2187 | } |
2188 | else | |
2189 | { | |
325fac50 | 2190 | LONGEST to_read = std::min (len - xfered_total, region_len); |
386c8614 TT |
2191 | gdb::unique_xmalloc_ptr<gdb_byte> buffer |
2192 | ((gdb_byte *) xmalloc (to_read * unit_size)); | |
8dedea02 | 2193 | |
279a6fed | 2194 | LONGEST xfered_partial = |
386c8614 | 2195 | target_read (ops, TARGET_OBJECT_MEMORY, NULL, buffer.get (), |
279a6fed | 2196 | offset + xfered_total, to_read); |
8dedea02 | 2197 | /* Call an observer, notifying them of the xfer progress? */ |
279a6fed | 2198 | if (xfered_partial <= 0) |
d5086790 | 2199 | { |
c378eb4e | 2200 | /* Got an error reading full chunk. See if maybe we can read |
8dedea02 | 2201 | some subrange. */ |
e084c964 DB |
2202 | read_whatever_is_readable (ops, offset + xfered_total, |
2203 | offset + xfered_total + to_read, | |
2204 | unit_size, &result); | |
279a6fed | 2205 | xfered_total += to_read; |
d5086790 | 2206 | } |
8dedea02 VP |
2207 | else |
2208 | { | |
386c8614 TT |
2209 | result.emplace_back (offset + xfered_total, |
2210 | offset + xfered_total + xfered_partial, | |
2211 | std::move (buffer)); | |
279a6fed | 2212 | xfered_total += xfered_partial; |
8dedea02 VP |
2213 | } |
2214 | QUIT; | |
d5086790 | 2215 | } |
d5086790 | 2216 | } |
9d78f827 | 2217 | |
8dedea02 | 2218 | return result; |
d5086790 VP |
2219 | } |
2220 | ||
8dedea02 | 2221 | |
cf7a04e8 DJ |
2222 | /* An alternative to target_write with progress callbacks. */ |
2223 | ||
1e3ff5ad | 2224 | LONGEST |
cf7a04e8 DJ |
2225 | target_write_with_progress (struct target_ops *ops, |
2226 | enum target_object object, | |
2227 | const char *annex, const gdb_byte *buf, | |
2228 | ULONGEST offset, LONGEST len, | |
2229 | void (*progress) (ULONGEST, void *), void *baton) | |
1e3ff5ad | 2230 | { |
279a6fed | 2231 | LONGEST xfered_total = 0; |
d309493c SM |
2232 | int unit_size = 1; |
2233 | ||
2234 | /* If we are writing to a memory object, find the length of an addressable | |
2235 | unit for that architecture. */ | |
2236 | if (object == TARGET_OBJECT_MEMORY | |
2237 | || object == TARGET_OBJECT_STACK_MEMORY | |
2238 | || object == TARGET_OBJECT_CODE_MEMORY | |
2239 | || object == TARGET_OBJECT_RAW_MEMORY) | |
2240 | unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ()); | |
a76d924d DJ |
2241 | |
2242 | /* Give the progress callback a chance to set up. */ | |
2243 | if (progress) | |
2244 | (*progress) (0, baton); | |
2245 | ||
279a6fed | 2246 | while (xfered_total < len) |
1e3ff5ad | 2247 | { |
279a6fed | 2248 | ULONGEST xfered_partial; |
9b409511 YQ |
2249 | enum target_xfer_status status; |
2250 | ||
2251 | status = target_write_partial (ops, object, annex, | |
d309493c | 2252 | buf + xfered_total * unit_size, |
279a6fed SM |
2253 | offset + xfered_total, len - xfered_total, |
2254 | &xfered_partial); | |
cf7a04e8 | 2255 | |
5c328c05 | 2256 | if (status != TARGET_XFER_OK) |
279a6fed | 2257 | return status == TARGET_XFER_EOF ? xfered_total : TARGET_XFER_E_IO; |
cf7a04e8 DJ |
2258 | |
2259 | if (progress) | |
279a6fed | 2260 | (*progress) (xfered_partial, baton); |
cf7a04e8 | 2261 | |
279a6fed | 2262 | xfered_total += xfered_partial; |
1e3ff5ad AC |
2263 | QUIT; |
2264 | } | |
2265 | return len; | |
2266 | } | |
2267 | ||
7f79c47e DE |
2268 | /* For docs on target_write see target.h. */ |
2269 | ||
cf7a04e8 DJ |
2270 | LONGEST |
2271 | target_write (struct target_ops *ops, | |
2272 | enum target_object object, | |
2273 | const char *annex, const gdb_byte *buf, | |
2274 | ULONGEST offset, LONGEST len) | |
2275 | { | |
2276 | return target_write_with_progress (ops, object, annex, buf, offset, len, | |
2277 | NULL, NULL); | |
2278 | } | |
2279 | ||
9018be22 SM |
2280 | /* Help for target_read_alloc and target_read_stralloc. See their comments |
2281 | for details. */ | |
13547ab6 | 2282 | |
9018be22 SM |
2283 | template <typename T> |
2284 | gdb::optional<gdb::def_vector<T>> | |
159f81f3 | 2285 | target_read_alloc_1 (struct target_ops *ops, enum target_object object, |
9018be22 | 2286 | const char *annex) |
13547ab6 | 2287 | { |
9018be22 SM |
2288 | gdb::def_vector<T> buf; |
2289 | size_t buf_pos = 0; | |
2290 | const int chunk = 4096; | |
13547ab6 DJ |
2291 | |
2292 | /* This function does not have a length parameter; it reads the | |
2293 | entire OBJECT). Also, it doesn't support objects fetched partly | |
2294 | from one target and partly from another (in a different stratum, | |
2295 | e.g. a core file and an executable). Both reasons make it | |
2296 | unsuitable for reading memory. */ | |
2297 | gdb_assert (object != TARGET_OBJECT_MEMORY); | |
2298 | ||
2299 | /* Start by reading up to 4K at a time. The target will throttle | |
2300 | this number down if necessary. */ | |
13547ab6 DJ |
2301 | while (1) |
2302 | { | |
9b409511 YQ |
2303 | ULONGEST xfered_len; |
2304 | enum target_xfer_status status; | |
2305 | ||
9018be22 SM |
2306 | buf.resize (buf_pos + chunk); |
2307 | ||
2308 | status = target_read_partial (ops, object, annex, | |
2309 | (gdb_byte *) &buf[buf_pos], | |
2310 | buf_pos, chunk, | |
9b409511 YQ |
2311 | &xfered_len); |
2312 | ||
2313 | if (status == TARGET_XFER_EOF) | |
13547ab6 DJ |
2314 | { |
2315 | /* Read all there was. */ | |
9018be22 SM |
2316 | buf.resize (buf_pos); |
2317 | return buf; | |
13547ab6 | 2318 | } |
9b409511 YQ |
2319 | else if (status != TARGET_XFER_OK) |
2320 | { | |
2321 | /* An error occurred. */ | |
9018be22 | 2322 | return {}; |
9b409511 | 2323 | } |
13547ab6 | 2324 | |
9b409511 | 2325 | buf_pos += xfered_len; |
13547ab6 | 2326 | |
13547ab6 DJ |
2327 | QUIT; |
2328 | } | |
2329 | } | |
2330 | ||
9018be22 | 2331 | /* See target.h */ |
159f81f3 | 2332 | |
9018be22 | 2333 | gdb::optional<gdb::byte_vector> |
159f81f3 | 2334 | target_read_alloc (struct target_ops *ops, enum target_object object, |
9018be22 | 2335 | const char *annex) |
159f81f3 | 2336 | { |
9018be22 | 2337 | return target_read_alloc_1<gdb_byte> (ops, object, annex); |
159f81f3 DJ |
2338 | } |
2339 | ||
b7b030ad | 2340 | /* See target.h. */ |
159f81f3 | 2341 | |
9018be22 | 2342 | gdb::optional<gdb::char_vector> |
159f81f3 DJ |
2343 | target_read_stralloc (struct target_ops *ops, enum target_object object, |
2344 | const char *annex) | |
2345 | { | |
9018be22 SM |
2346 | gdb::optional<gdb::char_vector> buf |
2347 | = target_read_alloc_1<char> (ops, object, annex); | |
159f81f3 | 2348 | |
9018be22 SM |
2349 | if (!buf) |
2350 | return {}; | |
159f81f3 | 2351 | |
d00a27c5 | 2352 | if (buf->empty () || buf->back () != '\0') |
9018be22 | 2353 | buf->push_back ('\0'); |
7313baad UW |
2354 | |
2355 | /* Check for embedded NUL bytes; but allow trailing NULs. */ | |
9018be22 SM |
2356 | for (auto it = std::find (buf->begin (), buf->end (), '\0'); |
2357 | it != buf->end (); it++) | |
2358 | if (*it != '\0') | |
7313baad UW |
2359 | { |
2360 | warning (_("target object %d, annex %s, " | |
2361 | "contained unexpected null characters"), | |
2362 | (int) object, annex ? annex : "(none)"); | |
2363 | break; | |
2364 | } | |
159f81f3 | 2365 | |
9018be22 | 2366 | return buf; |
159f81f3 DJ |
2367 | } |
2368 | ||
b6591e8b AC |
2369 | /* Memory transfer methods. */ |
2370 | ||
2371 | void | |
1b0ba102 | 2372 | get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf, |
b6591e8b AC |
2373 | LONGEST len) |
2374 | { | |
07b82ea5 PA |
2375 | /* This method is used to read from an alternate, non-current |
2376 | target. This read must bypass the overlay support (as symbols | |
2377 | don't match this target), and GDB's internal cache (wrong cache | |
2378 | for this target). */ | |
2379 | if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len) | |
b6591e8b | 2380 | != len) |
578d3588 | 2381 | memory_error (TARGET_XFER_E_IO, addr); |
b6591e8b AC |
2382 | } |
2383 | ||
2384 | ULONGEST | |
5d502164 MS |
2385 | get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr, |
2386 | int len, enum bfd_endian byte_order) | |
b6591e8b | 2387 | { |
f6519ebc | 2388 | gdb_byte buf[sizeof (ULONGEST)]; |
b6591e8b AC |
2389 | |
2390 | gdb_assert (len <= sizeof (buf)); | |
2391 | get_target_memory (ops, addr, buf, len); | |
e17a4113 | 2392 | return extract_unsigned_integer (buf, len, byte_order); |
b6591e8b AC |
2393 | } |
2394 | ||
3db08215 MM |
2395 | /* See target.h. */ |
2396 | ||
d914c394 SS |
2397 | int |
2398 | target_insert_breakpoint (struct gdbarch *gdbarch, | |
2399 | struct bp_target_info *bp_tgt) | |
2400 | { | |
2401 | if (!may_insert_breakpoints) | |
2402 | { | |
2403 | warning (_("May not insert breakpoints")); | |
2404 | return 1; | |
2405 | } | |
2406 | ||
328d42d8 SM |
2407 | target_ops *target = current_inferior ()->top_target (); |
2408 | ||
2409 | return target->insert_breakpoint (gdbarch, bp_tgt); | |
d914c394 SS |
2410 | } |
2411 | ||
3db08215 MM |
2412 | /* See target.h. */ |
2413 | ||
d914c394 | 2414 | int |
6b84065d | 2415 | target_remove_breakpoint (struct gdbarch *gdbarch, |
73971819 PA |
2416 | struct bp_target_info *bp_tgt, |
2417 | enum remove_bp_reason reason) | |
d914c394 SS |
2418 | { |
2419 | /* This is kind of a weird case to handle, but the permission might | |
2420 | have been changed after breakpoints were inserted - in which case | |
2421 | we should just take the user literally and assume that any | |
2422 | breakpoints should be left in place. */ | |
2423 | if (!may_insert_breakpoints) | |
2424 | { | |
2425 | warning (_("May not remove breakpoints")); | |
2426 | return 1; | |
2427 | } | |
2428 | ||
328d42d8 SM |
2429 | target_ops *target = current_inferior ()->top_target (); |
2430 | ||
2431 | return target->remove_breakpoint (gdbarch, bp_tgt, reason); | |
d914c394 SS |
2432 | } |
2433 | ||
c906108c | 2434 | static void |
1d12d88f | 2435 | info_target_command (const char *args, int from_tty) |
c906108c | 2436 | { |
c906108c | 2437 | int has_all_mem = 0; |
c5aa993b | 2438 | |
a42d7dd8 TT |
2439 | if (current_program_space->symfile_object_file != NULL) |
2440 | { | |
2441 | objfile *objf = current_program_space->symfile_object_file; | |
2442 | printf_unfiltered (_("Symbols from \"%s\".\n"), | |
2443 | objfile_name (objf)); | |
2444 | } | |
c906108c | 2445 | |
328d42d8 SM |
2446 | for (target_ops *t = current_inferior ()->top_target (); |
2447 | t != NULL; | |
2448 | t = t->beneath ()) | |
c906108c | 2449 | { |
f6ac5f3d | 2450 | if (!t->has_memory ()) |
c906108c SS |
2451 | continue; |
2452 | ||
66b4deae | 2453 | if ((int) (t->stratum ()) <= (int) dummy_stratum) |
c906108c SS |
2454 | continue; |
2455 | if (has_all_mem) | |
3e43a32a MS |
2456 | printf_unfiltered (_("\tWhile running this, " |
2457 | "GDB does not access memory from...\n")); | |
f6ac5f3d PA |
2458 | printf_unfiltered ("%s:\n", t->longname ()); |
2459 | t->files_info (); | |
2460 | has_all_mem = t->has_all_memory (); | |
c906108c SS |
2461 | } |
2462 | } | |
2463 | ||
fd79ecee DJ |
2464 | /* This function is called before any new inferior is created, e.g. |
2465 | by running a program, attaching, or connecting to a target. | |
2466 | It cleans up any state from previous invocations which might | |
2467 | change between runs. This is a subset of what target_preopen | |
2468 | resets (things which might change between targets). */ | |
2469 | ||
2470 | void | |
2471 | target_pre_inferior (int from_tty) | |
2472 | { | |
c378eb4e | 2473 | /* Clear out solib state. Otherwise the solib state of the previous |
b9db4ced | 2474 | inferior might have survived and is entirely wrong for the new |
c378eb4e | 2475 | target. This has been observed on GNU/Linux using glibc 2.3. How |
b9db4ced UW |
2476 | to reproduce: |
2477 | ||
2478 | bash$ ./foo& | |
2479 | [1] 4711 | |
2480 | bash$ ./foo& | |
2481 | [1] 4712 | |
2482 | bash$ gdb ./foo | |
2483 | [...] | |
2484 | (gdb) attach 4711 | |
2485 | (gdb) detach | |
2486 | (gdb) attach 4712 | |
2487 | Cannot access memory at address 0xdeadbeef | |
2488 | */ | |
b9db4ced | 2489 | |
50c71eaf PA |
2490 | /* In some OSs, the shared library list is the same/global/shared |
2491 | across inferiors. If code is shared between processes, so are | |
2492 | memory regions and features. */ | |
f5656ead | 2493 | if (!gdbarch_has_global_solist (target_gdbarch ())) |
50c71eaf PA |
2494 | { |
2495 | no_shared_libraries (NULL, from_tty); | |
2496 | ||
2497 | invalidate_target_mem_regions (); | |
424163ea | 2498 | |
50c71eaf PA |
2499 | target_clear_description (); |
2500 | } | |
8ffcbaaf | 2501 | |
e9756d52 PP |
2502 | /* attach_flag may be set if the previous process associated with |
2503 | the inferior was attached to. */ | |
2504 | current_inferior ()->attach_flag = 0; | |
2505 | ||
5d5658a1 PA |
2506 | current_inferior ()->highest_thread_num = 0; |
2507 | ||
8ffcbaaf | 2508 | agent_capability_invalidate (); |
fd79ecee DJ |
2509 | } |
2510 | ||
c906108c SS |
2511 | /* This is to be called by the open routine before it does |
2512 | anything. */ | |
2513 | ||
2514 | void | |
fba45db2 | 2515 | target_preopen (int from_tty) |
c906108c | 2516 | { |
c5aa993b | 2517 | dont_repeat (); |
c906108c | 2518 | |
5b6d1e4f | 2519 | if (current_inferior ()->pid != 0) |
c5aa993b | 2520 | { |
adf40b2e | 2521 | if (!from_tty |
55f6301a | 2522 | || !target_has_execution () |
b8fa0bfa | 2523 | || query (_("A program is being debugged already. Kill it? "))) |
5b6d1e4f PA |
2524 | { |
2525 | /* Core inferiors actually should be detached, not | |
2526 | killed. */ | |
55f6301a | 2527 | if (target_has_execution ()) |
5b6d1e4f PA |
2528 | target_kill (); |
2529 | else | |
2530 | target_detach (current_inferior (), 0); | |
2531 | } | |
c906108c | 2532 | else |
8a3fe4f8 | 2533 | error (_("Program not killed.")); |
c906108c SS |
2534 | } |
2535 | ||
2536 | /* Calling target_kill may remove the target from the stack. But if | |
2537 | it doesn't (which seems like a win for UDI), remove it now. */ | |
87ab71f0 PA |
2538 | /* Leave the exec target, though. The user may be switching from a |
2539 | live process to a core of the same program. */ | |
460014f5 | 2540 | pop_all_targets_above (file_stratum); |
fd79ecee DJ |
2541 | |
2542 | target_pre_inferior (from_tty); | |
c906108c SS |
2543 | } |
2544 | ||
6bd6f3b6 | 2545 | /* See target.h. */ |
c906108c SS |
2546 | |
2547 | void | |
6e1e1966 | 2548 | target_detach (inferior *inf, int from_tty) |
c906108c | 2549 | { |
5783e150 PW |
2550 | /* After we have detached, we will clear the register cache for this inferior |
2551 | by calling registers_changed_ptid. We must save the pid_ptid before | |
2552 | detaching, as the target detach method will clear inf->pid. */ | |
2553 | ptid_t save_pid_ptid = ptid_t (inf->pid); | |
2554 | ||
6e1e1966 SM |
2555 | /* As long as some to_detach implementations rely on the current_inferior |
2556 | (either directly, or indirectly, like through target_gdbarch or by | |
2557 | reading memory), INF needs to be the current inferior. When that | |
2558 | requirement will become no longer true, then we can remove this | |
2559 | assertion. */ | |
2560 | gdb_assert (inf == current_inferior ()); | |
2561 | ||
24291992 PA |
2562 | prepare_for_detach (); |
2563 | ||
5b6d1e4f PA |
2564 | /* Hold a strong reference because detaching may unpush the |
2565 | target. */ | |
2566 | auto proc_target_ref = target_ops_ref::new_reference (inf->process_target ()); | |
2567 | ||
328d42d8 | 2568 | current_inferior ()->top_target ()->detach (inf, from_tty); |
799efbe8 | 2569 | |
5b6d1e4f PA |
2570 | process_stratum_target *proc_target |
2571 | = as_process_stratum_target (proc_target_ref.get ()); | |
2572 | ||
2573 | registers_changed_ptid (proc_target, save_pid_ptid); | |
799efbe8 PW |
2574 | |
2575 | /* We have to ensure we have no frame cache left. Normally, | |
5783e150 PW |
2576 | registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when |
2577 | inferior_ptid matches save_pid_ptid, but in our case, it does not | |
799efbe8 PW |
2578 | call it, as inferior_ptid has been reset. */ |
2579 | reinit_frame_cache (); | |
c906108c SS |
2580 | } |
2581 | ||
6ad8ae5c | 2582 | void |
fee354ee | 2583 | target_disconnect (const char *args, int from_tty) |
6ad8ae5c | 2584 | { |
50c71eaf PA |
2585 | /* If we're in breakpoints-always-inserted mode or if breakpoints |
2586 | are global across processes, we have to remove them before | |
2587 | disconnecting. */ | |
74960c60 VP |
2588 | remove_breakpoints (); |
2589 | ||
328d42d8 | 2590 | current_inferior ()->top_target ()->disconnect (args, from_tty); |
6ad8ae5c DJ |
2591 | } |
2592 | ||
f2b9e3df SDJ |
2593 | /* See target/target.h. */ |
2594 | ||
117de6a9 | 2595 | ptid_t |
b60cea74 TT |
2596 | target_wait (ptid_t ptid, struct target_waitstatus *status, |
2597 | target_wait_flags options) | |
117de6a9 | 2598 | { |
328d42d8 | 2599 | target_ops *target = current_inferior ()->top_target (); |
1192f124 SM |
2600 | process_stratum_target *proc_target = current_inferior ()->process_target (); |
2601 | ||
2602 | gdb_assert (!proc_target->commit_resumed_state); | |
d3a07122 SM |
2603 | |
2604 | if (!target->can_async_p ()) | |
2605 | gdb_assert ((options & TARGET_WNOHANG) == 0); | |
2606 | ||
2607 | return target->wait (ptid, status, options); | |
117de6a9 PA |
2608 | } |
2609 | ||
0b333c5e PA |
2610 | /* See target.h. */ |
2611 | ||
2612 | ptid_t | |
2613 | default_target_wait (struct target_ops *ops, | |
2614 | ptid_t ptid, struct target_waitstatus *status, | |
b60cea74 | 2615 | target_wait_flags options) |
0b333c5e PA |
2616 | { |
2617 | status->kind = TARGET_WAITKIND_IGNORE; | |
2618 | return minus_one_ptid; | |
2619 | } | |
2620 | ||
a068643d | 2621 | std::string |
117de6a9 PA |
2622 | target_pid_to_str (ptid_t ptid) |
2623 | { | |
328d42d8 | 2624 | return current_inferior ()->top_target ()->pid_to_str (ptid); |
117de6a9 PA |
2625 | } |
2626 | ||
73ede765 | 2627 | const char * |
4694da01 TT |
2628 | target_thread_name (struct thread_info *info) |
2629 | { | |
5b6d1e4f PA |
2630 | gdb_assert (info->inf == current_inferior ()); |
2631 | ||
328d42d8 | 2632 | return current_inferior ()->top_target ()->thread_name (info); |
4694da01 TT |
2633 | } |
2634 | ||
e04ee09e KB |
2635 | struct thread_info * |
2636 | target_thread_handle_to_thread_info (const gdb_byte *thread_handle, | |
2637 | int handle_len, | |
2638 | struct inferior *inf) | |
2639 | { | |
328d42d8 SM |
2640 | target_ops *target = current_inferior ()->top_target (); |
2641 | ||
2642 | return target->thread_handle_to_thread_info (thread_handle, handle_len, inf); | |
e04ee09e KB |
2643 | } |
2644 | ||
3d6c6204 KB |
2645 | /* See target.h. */ |
2646 | ||
2647 | gdb::byte_vector | |
2648 | target_thread_info_to_thread_handle (struct thread_info *tip) | |
2649 | { | |
328d42d8 SM |
2650 | target_ops *target = current_inferior ()->top_target (); |
2651 | ||
2652 | return target->thread_info_to_thread_handle (tip); | |
3d6c6204 KB |
2653 | } |
2654 | ||
e1ac3328 | 2655 | void |
2ea28649 | 2656 | target_resume (ptid_t ptid, int step, enum gdb_signal signal) |
e1ac3328 | 2657 | { |
5b6d1e4f | 2658 | process_stratum_target *curr_target = current_inferior ()->process_target (); |
1192f124 | 2659 | gdb_assert (!curr_target->commit_resumed_state); |
5b6d1e4f | 2660 | |
4e5d721f | 2661 | target_dcache_invalidate (); |
28439f5e | 2662 | |
328d42d8 | 2663 | current_inferior ()->top_target ()->resume (ptid, step, signal); |
28439f5e | 2664 | |
5b6d1e4f | 2665 | registers_changed_ptid (curr_target, ptid); |
251bde03 | 2666 | /* We only set the internal executing state here. The user/frontend |
f2ffa92b PA |
2667 | running state is set at a higher level. This also clears the |
2668 | thread's stop_pc as side effect. */ | |
719546c4 | 2669 | set_executing (curr_target, ptid, true); |
5b6d1e4f | 2670 | clear_inline_frame_state (curr_target, ptid); |
e1ac3328 | 2671 | } |
2455069d | 2672 | |
85ad3aaf PA |
2673 | /* See target.h. */ |
2674 | ||
2675 | void | |
1192f124 | 2676 | target_commit_resumed () |
85ad3aaf | 2677 | { |
1192f124 SM |
2678 | gdb_assert (current_inferior ()->process_target ()->commit_resumed_state); |
2679 | current_inferior ()->top_target ()->commit_resumed (); | |
85ad3aaf PA |
2680 | } |
2681 | ||
b4b1a226 SM |
2682 | /* See target.h. */ |
2683 | ||
2684 | bool | |
2685 | target_has_pending_events () | |
2686 | { | |
2687 | return current_inferior ()->top_target ()->has_pending_events (); | |
2688 | } | |
2689 | ||
2455069d | 2690 | void |
adc6a863 | 2691 | target_pass_signals (gdb::array_view<const unsigned char> pass_signals) |
2455069d | 2692 | { |
328d42d8 | 2693 | current_inferior ()->top_target ()->pass_signals (pass_signals); |
2455069d UW |
2694 | } |
2695 | ||
9b224c5e | 2696 | void |
adc6a863 | 2697 | target_program_signals (gdb::array_view<const unsigned char> program_signals) |
9b224c5e | 2698 | { |
328d42d8 | 2699 | current_inferior ()->top_target ()->program_signals (program_signals); |
9b224c5e PA |
2700 | } |
2701 | ||
e97007b6 | 2702 | static void |
5ab2fbf1 SM |
2703 | default_follow_fork (struct target_ops *self, bool follow_child, |
2704 | bool detach_fork) | |
098dba18 TT |
2705 | { |
2706 | /* Some target returned a fork event, but did not know how to follow it. */ | |
2707 | internal_error (__FILE__, __LINE__, | |
2708 | _("could not find a target to follow fork")); | |
2709 | } | |
2710 | ||
e97007b6 | 2711 | /* See target.h. */ |
ee057212 | 2712 | |
e97007b6 | 2713 | void |
5ab2fbf1 | 2714 | target_follow_fork (bool follow_child, bool detach_fork) |
ee057212 | 2715 | { |
328d42d8 SM |
2716 | target_ops *target = current_inferior ()->top_target (); |
2717 | ||
2718 | return target->follow_fork (follow_child, detach_fork); | |
ee057212 DJ |
2719 | } |
2720 | ||
94585166 DB |
2721 | /* Target wrapper for follow exec hook. */ |
2722 | ||
2723 | void | |
4ca51187 | 2724 | target_follow_exec (struct inferior *inf, const char *execd_pathname) |
94585166 | 2725 | { |
328d42d8 | 2726 | current_inferior ()->top_target ()->follow_exec (inf, execd_pathname); |
94585166 DB |
2727 | } |
2728 | ||
8d657035 TT |
2729 | static void |
2730 | default_mourn_inferior (struct target_ops *self) | |
2731 | { | |
2732 | internal_error (__FILE__, __LINE__, | |
2733 | _("could not find a target to follow mourn inferior")); | |
2734 | } | |
2735 | ||
136d6dae | 2736 | void |
bc1e6c81 | 2737 | target_mourn_inferior (ptid_t ptid) |
136d6dae | 2738 | { |
dffdd8b5 | 2739 | gdb_assert (ptid.pid () == inferior_ptid.pid ()); |
328d42d8 | 2740 | current_inferior ()->top_target ()->mourn_inferior (); |
136d6dae | 2741 | |
8d657035 TT |
2742 | /* We no longer need to keep handles on any of the object files. |
2743 | Make sure to release them to avoid unnecessarily locking any | |
2744 | of them while we're not actually debugging. */ | |
2745 | bfd_cache_close_all (); | |
136d6dae VP |
2746 | } |
2747 | ||
424163ea DJ |
2748 | /* Look for a target which can describe architectural features, starting |
2749 | from TARGET. If we find one, return its description. */ | |
2750 | ||
2751 | const struct target_desc * | |
2752 | target_read_description (struct target_ops *target) | |
2753 | { | |
f6ac5f3d | 2754 | return target->read_description (); |
424163ea DJ |
2755 | } |
2756 | ||
08388c79 | 2757 | |
58a5184e TT |
2758 | /* Default implementation of memory-searching. */ |
2759 | ||
2760 | static int | |
2761 | default_search_memory (struct target_ops *self, | |
2762 | CORE_ADDR start_addr, ULONGEST search_space_len, | |
2763 | const gdb_byte *pattern, ULONGEST pattern_len, | |
2764 | CORE_ADDR *found_addrp) | |
2765 | { | |
4a72de73 TT |
2766 | auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len) |
2767 | { | |
328d42d8 SM |
2768 | return target_read (current_inferior ()->top_target (), |
2769 | TARGET_OBJECT_MEMORY, NULL, | |
4a72de73 TT |
2770 | result, addr, len) == len; |
2771 | }; | |
2772 | ||
58a5184e | 2773 | /* Start over from the top of the target stack. */ |
4a72de73 | 2774 | return simple_search_memory (read_memory, start_addr, search_space_len, |
58a5184e TT |
2775 | pattern, pattern_len, found_addrp); |
2776 | } | |
2777 | ||
08388c79 DE |
2778 | /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the |
2779 | sequence of bytes in PATTERN with length PATTERN_LEN. | |
2780 | ||
2781 | The result is 1 if found, 0 if not found, and -1 if there was an error | |
2782 | requiring halting of the search (e.g. memory read error). | |
2783 | If the pattern is found the address is recorded in FOUND_ADDRP. */ | |
2784 | ||
2785 | int | |
2786 | target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len, | |
2787 | const gdb_byte *pattern, ULONGEST pattern_len, | |
2788 | CORE_ADDR *found_addrp) | |
2789 | { | |
328d42d8 SM |
2790 | target_ops *target = current_inferior ()->top_target (); |
2791 | ||
2792 | return target->search_memory (start_addr, search_space_len, pattern, | |
2793 | pattern_len, found_addrp); | |
08388c79 DE |
2794 | } |
2795 | ||
8edfe269 DJ |
2796 | /* Look through the currently pushed targets. If none of them will |
2797 | be able to restart the currently running process, issue an error | |
2798 | message. */ | |
2799 | ||
2800 | void | |
2801 | target_require_runnable (void) | |
2802 | { | |
328d42d8 SM |
2803 | for (target_ops *t = current_inferior ()->top_target (); |
2804 | t != NULL; | |
2805 | t = t->beneath ()) | |
8edfe269 DJ |
2806 | { |
2807 | /* If this target knows how to create a new program, then | |
2808 | assume we will still be able to after killing the current | |
2809 | one. Either killing and mourning will not pop T, or else | |
2810 | find_default_run_target will find it again. */ | |
f6ac5f3d | 2811 | if (t->can_create_inferior ()) |
8edfe269 DJ |
2812 | return; |
2813 | ||
548740d6 | 2814 | /* Do not worry about targets at certain strata that can not |
8edfe269 DJ |
2815 | create inferiors. Assume they will be pushed again if |
2816 | necessary, and continue to the process_stratum. */ | |
66b4deae | 2817 | if (t->stratum () > process_stratum) |
8edfe269 DJ |
2818 | continue; |
2819 | ||
3e43a32a MS |
2820 | error (_("The \"%s\" target does not support \"run\". " |
2821 | "Try \"help target\" or \"continue\"."), | |
f6ac5f3d | 2822 | t->shortname ()); |
8edfe269 DJ |
2823 | } |
2824 | ||
2825 | /* This function is only called if the target is running. In that | |
2826 | case there should have been a process_stratum target and it | |
c378eb4e | 2827 | should either know how to create inferiors, or not... */ |
9b20d036 | 2828 | internal_error (__FILE__, __LINE__, _("No targets found")); |
8edfe269 DJ |
2829 | } |
2830 | ||
6a3cb8e8 PA |
2831 | /* Whether GDB is allowed to fall back to the default run target for |
2832 | "run", "attach", etc. when no target is connected yet. */ | |
491144b5 | 2833 | static bool auto_connect_native_target = true; |
6a3cb8e8 PA |
2834 | |
2835 | static void | |
2836 | show_auto_connect_native_target (struct ui_file *file, int from_tty, | |
2837 | struct cmd_list_element *c, const char *value) | |
2838 | { | |
2839 | fprintf_filtered (file, | |
2840 | _("Whether GDB may automatically connect to the " | |
2841 | "native target is %s.\n"), | |
2842 | value); | |
2843 | } | |
2844 | ||
d9f719f1 PA |
2845 | /* A pointer to the target that can respond to "run" or "attach". |
2846 | Native targets are always singletons and instantiated early at GDB | |
2847 | startup. */ | |
2848 | static target_ops *the_native_target; | |
2849 | ||
2850 | /* See target.h. */ | |
2851 | ||
2852 | void | |
2853 | set_native_target (target_ops *target) | |
2854 | { | |
2855 | if (the_native_target != NULL) | |
2856 | internal_error (__FILE__, __LINE__, | |
2857 | _("native target already set (\"%s\")."), | |
2858 | the_native_target->longname ()); | |
2859 | ||
2860 | the_native_target = target; | |
2861 | } | |
2862 | ||
2863 | /* See target.h. */ | |
2864 | ||
2865 | target_ops * | |
2866 | get_native_target () | |
2867 | { | |
2868 | return the_native_target; | |
2869 | } | |
2870 | ||
c906108c SS |
2871 | /* Look through the list of possible targets for a target that can |
2872 | execute a run or attach command without any other data. This is | |
2873 | used to locate the default process stratum. | |
2874 | ||
5f667f2d PA |
2875 | If DO_MESG is not NULL, the result is always valid (error() is |
2876 | called for errors); else, return NULL on error. */ | |
c906108c SS |
2877 | |
2878 | static struct target_ops * | |
a121b7c1 | 2879 | find_default_run_target (const char *do_mesg) |
c906108c | 2880 | { |
d9f719f1 PA |
2881 | if (auto_connect_native_target && the_native_target != NULL) |
2882 | return the_native_target; | |
c906108c | 2883 | |
d9f719f1 PA |
2884 | if (do_mesg != NULL) |
2885 | error (_("Don't know how to %s. Try \"help target\"."), do_mesg); | |
2886 | return NULL; | |
c906108c SS |
2887 | } |
2888 | ||
b3ccfe11 | 2889 | /* See target.h. */ |
c906108c | 2890 | |
b3ccfe11 TT |
2891 | struct target_ops * |
2892 | find_attach_target (void) | |
c906108c | 2893 | { |
b3ccfe11 | 2894 | /* If a target on the current stack can attach, use it. */ |
328d42d8 SM |
2895 | for (target_ops *t = current_inferior ()->top_target (); |
2896 | t != NULL; | |
2897 | t = t->beneath ()) | |
b3ccfe11 | 2898 | { |
f6ac5f3d | 2899 | if (t->can_attach ()) |
d9f719f1 | 2900 | return t; |
b3ccfe11 | 2901 | } |
c906108c | 2902 | |
b3ccfe11 | 2903 | /* Otherwise, use the default run target for attaching. */ |
d9f719f1 | 2904 | return find_default_run_target ("attach"); |
b84876c2 PA |
2905 | } |
2906 | ||
b3ccfe11 | 2907 | /* See target.h. */ |
b84876c2 | 2908 | |
b3ccfe11 TT |
2909 | struct target_ops * |
2910 | find_run_target (void) | |
9908b566 | 2911 | { |
f6ac5f3d | 2912 | /* If a target on the current stack can run, use it. */ |
328d42d8 SM |
2913 | for (target_ops *t = current_inferior ()->top_target (); |
2914 | t != NULL; | |
2915 | t = t->beneath ()) | |
b3ccfe11 | 2916 | { |
f6ac5f3d | 2917 | if (t->can_create_inferior ()) |
d9f719f1 | 2918 | return t; |
b3ccfe11 | 2919 | } |
5d502164 | 2920 | |
b3ccfe11 | 2921 | /* Otherwise, use the default run target. */ |
d9f719f1 | 2922 | return find_default_run_target ("run"); |
9908b566 VP |
2923 | } |
2924 | ||
f6ac5f3d PA |
2925 | bool |
2926 | target_ops::info_proc (const char *args, enum info_proc_what what) | |
2927 | { | |
2928 | return false; | |
2929 | } | |
2930 | ||
145b16a9 UW |
2931 | /* Implement the "info proc" command. */ |
2932 | ||
451b7c33 | 2933 | int |
7bc112c1 | 2934 | target_info_proc (const char *args, enum info_proc_what what) |
145b16a9 UW |
2935 | { |
2936 | struct target_ops *t; | |
2937 | ||
2938 | /* If we're already connected to something that can get us OS | |
2939 | related data, use it. Otherwise, try using the native | |
2940 | target. */ | |
f6ac5f3d PA |
2941 | t = find_target_at (process_stratum); |
2942 | if (t == NULL) | |
145b16a9 UW |
2943 | t = find_default_run_target (NULL); |
2944 | ||
b6a8c27b | 2945 | for (; t != NULL; t = t->beneath ()) |
145b16a9 | 2946 | { |
f6ac5f3d | 2947 | if (t->info_proc (args, what)) |
145b16a9 | 2948 | { |
145b16a9 UW |
2949 | if (targetdebug) |
2950 | fprintf_unfiltered (gdb_stdlog, | |
2951 | "target_info_proc (\"%s\", %d)\n", args, what); | |
2952 | ||
451b7c33 | 2953 | return 1; |
145b16a9 UW |
2954 | } |
2955 | } | |
2956 | ||
451b7c33 | 2957 | return 0; |
145b16a9 UW |
2958 | } |
2959 | ||
03583c20 | 2960 | static int |
2bfc0540 | 2961 | find_default_supports_disable_randomization (struct target_ops *self) |
03583c20 UW |
2962 | { |
2963 | struct target_ops *t; | |
2964 | ||
2965 | t = find_default_run_target (NULL); | |
f6ac5f3d PA |
2966 | if (t != NULL) |
2967 | return t->supports_disable_randomization (); | |
03583c20 UW |
2968 | return 0; |
2969 | } | |
2970 | ||
2971 | int | |
2972 | target_supports_disable_randomization (void) | |
2973 | { | |
328d42d8 | 2974 | return current_inferior ()->top_target ()->supports_disable_randomization (); |
03583c20 | 2975 | } |
9908b566 | 2976 | |
1fb77080 SDJ |
2977 | /* See target/target.h. */ |
2978 | ||
2979 | int | |
2980 | target_supports_multi_process (void) | |
2981 | { | |
328d42d8 | 2982 | return current_inferior ()->top_target ()->supports_multi_process (); |
1fb77080 SDJ |
2983 | } |
2984 | ||
b7b030ad TT |
2985 | /* See target.h. */ |
2986 | ||
9018be22 | 2987 | gdb::optional<gdb::char_vector> |
07e059b5 VP |
2988 | target_get_osdata (const char *type) |
2989 | { | |
07e059b5 VP |
2990 | struct target_ops *t; |
2991 | ||
739ef7fb PA |
2992 | /* If we're already connected to something that can get us OS |
2993 | related data, use it. Otherwise, try using the native | |
2994 | target. */ | |
f6ac5f3d PA |
2995 | t = find_target_at (process_stratum); |
2996 | if (t == NULL) | |
739ef7fb | 2997 | t = find_default_run_target ("get OS data"); |
07e059b5 VP |
2998 | |
2999 | if (!t) | |
9018be22 | 3000 | return {}; |
07e059b5 | 3001 | |
6d097e65 | 3002 | return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type); |
07e059b5 VP |
3003 | } |
3004 | ||
8eaff7cd TT |
3005 | /* Determine the current address space of thread PTID. */ |
3006 | ||
3007 | struct address_space * | |
3008 | target_thread_address_space (ptid_t ptid) | |
3009 | { | |
3010 | struct address_space *aspace; | |
3011 | ||
328d42d8 | 3012 | aspace = current_inferior ()->top_target ()->thread_address_space (ptid); |
8eaff7cd TT |
3013 | gdb_assert (aspace != NULL); |
3014 | ||
8eaff7cd TT |
3015 | return aspace; |
3016 | } | |
3017 | ||
b6a8c27b PA |
3018 | /* See target.h. */ |
3019 | ||
3020 | target_ops * | |
3021 | target_ops::beneath () const | |
3022 | { | |
5b6d1e4f | 3023 | return current_inferior ()->find_target_beneath (this); |
b6a8c27b PA |
3024 | } |
3025 | ||
f6ac5f3d PA |
3026 | void |
3027 | target_ops::close () | |
3028 | { | |
3029 | } | |
3030 | ||
3031 | bool | |
3032 | target_ops::can_attach () | |
3033 | { | |
3034 | return 0; | |
3035 | } | |
3036 | ||
3037 | void | |
3038 | target_ops::attach (const char *, int) | |
3039 | { | |
3040 | gdb_assert_not_reached ("target_ops::attach called"); | |
3041 | } | |
3042 | ||
3043 | bool | |
3044 | target_ops::can_create_inferior () | |
3045 | { | |
3046 | return 0; | |
3047 | } | |
3048 | ||
3049 | void | |
3050 | target_ops::create_inferior (const char *, const std::string &, | |
3051 | char **, int) | |
3052 | { | |
3053 | gdb_assert_not_reached ("target_ops::create_inferior called"); | |
3054 | } | |
3055 | ||
57810aa7 | 3056 | bool |
f6ac5f3d PA |
3057 | target_ops::can_run () |
3058 | { | |
57810aa7 | 3059 | return false; |
f6ac5f3d PA |
3060 | } |
3061 | ||
3062 | int | |
3063 | target_can_run () | |
3064 | { | |
328d42d8 SM |
3065 | for (target_ops *t = current_inferior ()->top_target (); |
3066 | t != NULL; | |
3067 | t = t->beneath ()) | |
f6ac5f3d PA |
3068 | { |
3069 | if (t->can_run ()) | |
3070 | return 1; | |
3071 | } | |
3072 | ||
3073 | return 0; | |
3074 | } | |
7313baad UW |
3075 | |
3076 | /* Target file operations. */ | |
3077 | ||
3078 | static struct target_ops * | |
3079 | default_fileio_target (void) | |
3080 | { | |
f6ac5f3d PA |
3081 | struct target_ops *t; |
3082 | ||
7313baad UW |
3083 | /* If we're already connected to something that can perform |
3084 | file I/O, use it. Otherwise, try using the native target. */ | |
f6ac5f3d PA |
3085 | t = find_target_at (process_stratum); |
3086 | if (t != NULL) | |
3087 | return t; | |
3088 | return find_default_run_target ("file I/O"); | |
7313baad UW |
3089 | } |
3090 | ||
1c4b552b GB |
3091 | /* File handle for target file operations. */ |
3092 | ||
5ff79300 | 3093 | struct fileio_fh_t |
1c4b552b | 3094 | { |
20db9c52 PA |
3095 | /* The target on which this file is open. NULL if the target is |
3096 | meanwhile closed while the handle is open. */ | |
5ff79300 | 3097 | target_ops *target; |
1c4b552b GB |
3098 | |
3099 | /* The file descriptor on the target. */ | |
5ff79300 | 3100 | int target_fd; |
1c4b552b | 3101 | |
5ff79300 PA |
3102 | /* Check whether this fileio_fh_t represents a closed file. */ |
3103 | bool is_closed () | |
3104 | { | |
3105 | return target_fd < 0; | |
3106 | } | |
3107 | }; | |
1c4b552b GB |
3108 | |
3109 | /* Vector of currently open file handles. The value returned by | |
3110 | target_fileio_open and passed as the FD argument to other | |
3111 | target_fileio_* functions is an index into this vector. This | |
3112 | vector's entries are never freed; instead, files are marked as | |
3113 | closed, and the handle becomes available for reuse. */ | |
5ff79300 | 3114 | static std::vector<fileio_fh_t> fileio_fhandles; |
1c4b552b GB |
3115 | |
3116 | /* Index into fileio_fhandles of the lowest handle that might be | |
3117 | closed. This permits handle reuse without searching the whole | |
3118 | list each time a new file is opened. */ | |
3119 | static int lowest_closed_fd; | |
3120 | ||
20db9c52 PA |
3121 | /* Invalidate the target associated with open handles that were open |
3122 | on target TARG, since we're about to close (and maybe destroy) the | |
3123 | target. The handles remain open from the client's perspective, but | |
3124 | trying to do anything with them other than closing them will fail | |
3125 | with EIO. */ | |
3126 | ||
3127 | static void | |
3128 | fileio_handles_invalidate_target (target_ops *targ) | |
3129 | { | |
3130 | for (fileio_fh_t &fh : fileio_fhandles) | |
3131 | if (fh.target == targ) | |
3132 | fh.target = NULL; | |
3133 | } | |
3134 | ||
1c4b552b GB |
3135 | /* Acquire a target fileio file descriptor. */ |
3136 | ||
3137 | static int | |
5ff79300 | 3138 | acquire_fileio_fd (target_ops *target, int target_fd) |
1c4b552b | 3139 | { |
1c4b552b | 3140 | /* Search for closed handles to reuse. */ |
5ff79300 PA |
3141 | for (; lowest_closed_fd < fileio_fhandles.size (); lowest_closed_fd++) |
3142 | { | |
3143 | fileio_fh_t &fh = fileio_fhandles[lowest_closed_fd]; | |
3144 | ||
3145 | if (fh.is_closed ()) | |
3146 | break; | |
3147 | } | |
1c4b552b GB |
3148 | |
3149 | /* Push a new handle if no closed handles were found. */ | |
5ff79300 PA |
3150 | if (lowest_closed_fd == fileio_fhandles.size ()) |
3151 | fileio_fhandles.push_back (fileio_fh_t {target, target_fd}); | |
3152 | else | |
3153 | fileio_fhandles[lowest_closed_fd] = {target, target_fd}; | |
1c4b552b | 3154 | |
5ff79300 PA |
3155 | /* Should no longer be marked closed. */ |
3156 | gdb_assert (!fileio_fhandles[lowest_closed_fd].is_closed ()); | |
1c4b552b GB |
3157 | |
3158 | /* Return its index, and start the next lookup at | |
3159 | the next index. */ | |
3160 | return lowest_closed_fd++; | |
3161 | } | |
3162 | ||
3163 | /* Release a target fileio file descriptor. */ | |
3164 | ||
3165 | static void | |
3166 | release_fileio_fd (int fd, fileio_fh_t *fh) | |
3167 | { | |
5ff79300 | 3168 | fh->target_fd = -1; |
325fac50 | 3169 | lowest_closed_fd = std::min (lowest_closed_fd, fd); |
1c4b552b GB |
3170 | } |
3171 | ||
3172 | /* Return a pointer to the fileio_fhandle_t corresponding to FD. */ | |
3173 | ||
5ff79300 PA |
3174 | static fileio_fh_t * |
3175 | fileio_fd_to_fh (int fd) | |
3176 | { | |
3177 | return &fileio_fhandles[fd]; | |
3178 | } | |
1c4b552b | 3179 | |
f6ac5f3d PA |
3180 | |
3181 | /* Default implementations of file i/o methods. We don't want these | |
3182 | to delegate automatically, because we need to know which target | |
3183 | supported the method, in order to call it directly from within | |
3184 | pread/pwrite, etc. */ | |
3185 | ||
3186 | int | |
3187 | target_ops::fileio_open (struct inferior *inf, const char *filename, | |
3188 | int flags, int mode, int warn_if_slow, | |
3189 | int *target_errno) | |
3190 | { | |
3191 | *target_errno = FILEIO_ENOSYS; | |
3192 | return -1; | |
3193 | } | |
3194 | ||
3195 | int | |
3196 | target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
3197 | ULONGEST offset, int *target_errno) | |
3198 | { | |
3199 | *target_errno = FILEIO_ENOSYS; | |
3200 | return -1; | |
3201 | } | |
3202 | ||
3203 | int | |
3204 | target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len, | |
3205 | ULONGEST offset, int *target_errno) | |
3206 | { | |
3207 | *target_errno = FILEIO_ENOSYS; | |
3208 | return -1; | |
3209 | } | |
3210 | ||
3211 | int | |
3212 | target_ops::fileio_fstat (int fd, struct stat *sb, int *target_errno) | |
3213 | { | |
3214 | *target_errno = FILEIO_ENOSYS; | |
3215 | return -1; | |
3216 | } | |
3217 | ||
3218 | int | |
3219 | target_ops::fileio_close (int fd, int *target_errno) | |
3220 | { | |
3221 | *target_errno = FILEIO_ENOSYS; | |
3222 | return -1; | |
3223 | } | |
3224 | ||
3225 | int | |
3226 | target_ops::fileio_unlink (struct inferior *inf, const char *filename, | |
3227 | int *target_errno) | |
3228 | { | |
3229 | *target_errno = FILEIO_ENOSYS; | |
3230 | return -1; | |
3231 | } | |
3232 | ||
3233 | gdb::optional<std::string> | |
3234 | target_ops::fileio_readlink (struct inferior *inf, const char *filename, | |
3235 | int *target_errno) | |
3236 | { | |
3237 | *target_errno = FILEIO_ENOSYS; | |
3238 | return {}; | |
3239 | } | |
3240 | ||
4111f652 | 3241 | /* See target.h. */ |
12e2a5fd | 3242 | |
4111f652 PA |
3243 | int |
3244 | target_fileio_open (struct inferior *inf, const char *filename, | |
3245 | int flags, int mode, bool warn_if_slow, int *target_errno) | |
7313baad | 3246 | { |
b6a8c27b | 3247 | for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ()) |
7313baad | 3248 | { |
f6ac5f3d PA |
3249 | int fd = t->fileio_open (inf, filename, flags, mode, |
3250 | warn_if_slow, target_errno); | |
7313baad | 3251 | |
f6ac5f3d PA |
3252 | if (fd == -1 && *target_errno == FILEIO_ENOSYS) |
3253 | continue; | |
1c4b552b | 3254 | |
f6ac5f3d PA |
3255 | if (fd < 0) |
3256 | fd = -1; | |
3257 | else | |
3258 | fd = acquire_fileio_fd (t, fd); | |
3259 | ||
3260 | if (targetdebug) | |
3261 | fprintf_unfiltered (gdb_stdlog, | |
4313b8c0 | 3262 | "target_fileio_open (%d,%s,0x%x,0%o,%d)" |
07c138c8 GB |
3263 | " = %d (%d)\n", |
3264 | inf == NULL ? 0 : inf->num, | |
7313baad | 3265 | filename, flags, mode, |
4313b8c0 GB |
3266 | warn_if_slow, fd, |
3267 | fd != -1 ? 0 : *target_errno); | |
f6ac5f3d | 3268 | return fd; |
7313baad UW |
3269 | } |
3270 | ||
3271 | *target_errno = FILEIO_ENOSYS; | |
3272 | return -1; | |
3273 | } | |
3274 | ||
12e2a5fd GB |
3275 | /* See target.h. */ |
3276 | ||
7313baad UW |
3277 | int |
3278 | target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
3279 | ULONGEST offset, int *target_errno) | |
3280 | { | |
1c4b552b GB |
3281 | fileio_fh_t *fh = fileio_fd_to_fh (fd); |
3282 | int ret = -1; | |
7313baad | 3283 | |
5ff79300 | 3284 | if (fh->is_closed ()) |
1c4b552b | 3285 | *target_errno = EBADF; |
20db9c52 PA |
3286 | else if (fh->target == NULL) |
3287 | *target_errno = EIO; | |
1c4b552b | 3288 | else |
f6ac5f3d PA |
3289 | ret = fh->target->fileio_pwrite (fh->target_fd, write_buf, |
3290 | len, offset, target_errno); | |
7313baad | 3291 | |
1c4b552b GB |
3292 | if (targetdebug) |
3293 | fprintf_unfiltered (gdb_stdlog, | |
3294 | "target_fileio_pwrite (%d,...,%d,%s) " | |
3295 | "= %d (%d)\n", | |
3296 | fd, len, pulongest (offset), | |
3297 | ret, ret != -1 ? 0 : *target_errno); | |
3298 | return ret; | |
7313baad UW |
3299 | } |
3300 | ||
12e2a5fd GB |
3301 | /* See target.h. */ |
3302 | ||
7313baad UW |
3303 | int |
3304 | target_fileio_pread (int fd, gdb_byte *read_buf, int len, | |
3305 | ULONGEST offset, int *target_errno) | |
3306 | { | |
1c4b552b GB |
3307 | fileio_fh_t *fh = fileio_fd_to_fh (fd); |
3308 | int ret = -1; | |
7313baad | 3309 | |
5ff79300 | 3310 | if (fh->is_closed ()) |
1c4b552b | 3311 | *target_errno = EBADF; |
20db9c52 PA |
3312 | else if (fh->target == NULL) |
3313 | *target_errno = EIO; | |
1c4b552b | 3314 | else |
f6ac5f3d PA |
3315 | ret = fh->target->fileio_pread (fh->target_fd, read_buf, |
3316 | len, offset, target_errno); | |
7313baad | 3317 | |
1c4b552b GB |
3318 | if (targetdebug) |
3319 | fprintf_unfiltered (gdb_stdlog, | |
3320 | "target_fileio_pread (%d,...,%d,%s) " | |
3321 | "= %d (%d)\n", | |
3322 | fd, len, pulongest (offset), | |
3323 | ret, ret != -1 ? 0 : *target_errno); | |
9b15c1f0 GB |
3324 | return ret; |
3325 | } | |
3326 | ||
3327 | /* See target.h. */ | |
12e2a5fd | 3328 | |
9b15c1f0 GB |
3329 | int |
3330 | target_fileio_fstat (int fd, struct stat *sb, int *target_errno) | |
3331 | { | |
3332 | fileio_fh_t *fh = fileio_fd_to_fh (fd); | |
3333 | int ret = -1; | |
3334 | ||
5ff79300 | 3335 | if (fh->is_closed ()) |
9b15c1f0 | 3336 | *target_errno = EBADF; |
20db9c52 PA |
3337 | else if (fh->target == NULL) |
3338 | *target_errno = EIO; | |
9b15c1f0 | 3339 | else |
f6ac5f3d | 3340 | ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno); |
9b15c1f0 GB |
3341 | |
3342 | if (targetdebug) | |
3343 | fprintf_unfiltered (gdb_stdlog, | |
3344 | "target_fileio_fstat (%d) = %d (%d)\n", | |
3345 | fd, ret, ret != -1 ? 0 : *target_errno); | |
1c4b552b | 3346 | return ret; |
7313baad UW |
3347 | } |
3348 | ||
12e2a5fd GB |
3349 | /* See target.h. */ |
3350 | ||
7313baad UW |
3351 | int |
3352 | target_fileio_close (int fd, int *target_errno) | |
3353 | { | |
1c4b552b GB |
3354 | fileio_fh_t *fh = fileio_fd_to_fh (fd); |
3355 | int ret = -1; | |
7313baad | 3356 | |
5ff79300 | 3357 | if (fh->is_closed ()) |
1c4b552b GB |
3358 | *target_errno = EBADF; |
3359 | else | |
7313baad | 3360 | { |
20db9c52 | 3361 | if (fh->target != NULL) |
f6ac5f3d PA |
3362 | ret = fh->target->fileio_close (fh->target_fd, |
3363 | target_errno); | |
20db9c52 PA |
3364 | else |
3365 | ret = 0; | |
1c4b552b | 3366 | release_fileio_fd (fd, fh); |
7313baad UW |
3367 | } |
3368 | ||
1c4b552b GB |
3369 | if (targetdebug) |
3370 | fprintf_unfiltered (gdb_stdlog, | |
3371 | "target_fileio_close (%d) = %d (%d)\n", | |
3372 | fd, ret, ret != -1 ? 0 : *target_errno); | |
3373 | return ret; | |
7313baad UW |
3374 | } |
3375 | ||
12e2a5fd GB |
3376 | /* See target.h. */ |
3377 | ||
7313baad | 3378 | int |
07c138c8 GB |
3379 | target_fileio_unlink (struct inferior *inf, const char *filename, |
3380 | int *target_errno) | |
7313baad | 3381 | { |
b6a8c27b | 3382 | for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ()) |
7313baad | 3383 | { |
f6ac5f3d | 3384 | int ret = t->fileio_unlink (inf, filename, target_errno); |
7313baad | 3385 | |
f6ac5f3d PA |
3386 | if (ret == -1 && *target_errno == FILEIO_ENOSYS) |
3387 | continue; | |
3388 | ||
3389 | if (targetdebug) | |
3390 | fprintf_unfiltered (gdb_stdlog, | |
3391 | "target_fileio_unlink (%d,%s)" | |
3392 | " = %d (%d)\n", | |
3393 | inf == NULL ? 0 : inf->num, filename, | |
3394 | ret, ret != -1 ? 0 : *target_errno); | |
3395 | return ret; | |
7313baad UW |
3396 | } |
3397 | ||
3398 | *target_errno = FILEIO_ENOSYS; | |
3399 | return -1; | |
3400 | } | |
3401 | ||
12e2a5fd GB |
3402 | /* See target.h. */ |
3403 | ||
e0d3522b | 3404 | gdb::optional<std::string> |
07c138c8 GB |
3405 | target_fileio_readlink (struct inferior *inf, const char *filename, |
3406 | int *target_errno) | |
b9e7b9c3 | 3407 | { |
b6a8c27b | 3408 | for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ()) |
b9e7b9c3 | 3409 | { |
f6ac5f3d PA |
3410 | gdb::optional<std::string> ret |
3411 | = t->fileio_readlink (inf, filename, target_errno); | |
b9e7b9c3 | 3412 | |
f6ac5f3d PA |
3413 | if (!ret.has_value () && *target_errno == FILEIO_ENOSYS) |
3414 | continue; | |
3415 | ||
3416 | if (targetdebug) | |
3417 | fprintf_unfiltered (gdb_stdlog, | |
3418 | "target_fileio_readlink (%d,%s)" | |
3419 | " = %s (%d)\n", | |
3420 | inf == NULL ? 0 : inf->num, | |
3421 | filename, ret ? ret->c_str () : "(nil)", | |
3422 | ret ? 0 : *target_errno); | |
3423 | return ret; | |
b9e7b9c3 UW |
3424 | } |
3425 | ||
3426 | *target_errno = FILEIO_ENOSYS; | |
e0d3522b | 3427 | return {}; |
b9e7b9c3 UW |
3428 | } |
3429 | ||
770623f7 TT |
3430 | /* Like scoped_fd, but specific to target fileio. */ |
3431 | ||
3432 | class scoped_target_fd | |
7313baad | 3433 | { |
770623f7 TT |
3434 | public: |
3435 | explicit scoped_target_fd (int fd) noexcept | |
3436 | : m_fd (fd) | |
3437 | { | |
3438 | } | |
7313baad | 3439 | |
770623f7 TT |
3440 | ~scoped_target_fd () |
3441 | { | |
3442 | if (m_fd >= 0) | |
3443 | { | |
3444 | int target_errno; | |
3445 | ||
3446 | target_fileio_close (m_fd, &target_errno); | |
3447 | } | |
3448 | } | |
3449 | ||
3450 | DISABLE_COPY_AND_ASSIGN (scoped_target_fd); | |
3451 | ||
3452 | int get () const noexcept | |
3453 | { | |
3454 | return m_fd; | |
3455 | } | |
3456 | ||
3457 | private: | |
3458 | int m_fd; | |
3459 | }; | |
7313baad | 3460 | |
07c138c8 GB |
3461 | /* Read target file FILENAME, in the filesystem as seen by INF. If |
3462 | INF is NULL, use the filesystem seen by the debugger (GDB or, for | |
3463 | remote targets, the remote stub). Store the result in *BUF_P and | |
3464 | return the size of the transferred data. PADDING additional bytes | |
3465 | are available in *BUF_P. This is a helper function for | |
3466 | target_fileio_read_alloc; see the declaration of that function for | |
3467 | more information. */ | |
7313baad | 3468 | |
f7af1fcd JK |
3469 | static LONGEST |
3470 | target_fileio_read_alloc_1 (struct inferior *inf, const char *filename, | |
3471 | gdb_byte **buf_p, int padding) | |
3472 | { | |
db1ff28b JK |
3473 | size_t buf_alloc, buf_pos; |
3474 | gdb_byte *buf; | |
3475 | LONGEST n; | |
db1ff28b | 3476 | int target_errno; |
f7af1fcd | 3477 | |
770623f7 | 3478 | scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY, |
4111f652 | 3479 | 0700, false, &target_errno)); |
770623f7 | 3480 | if (fd.get () == -1) |
f7af1fcd JK |
3481 | return -1; |
3482 | ||
db1ff28b JK |
3483 | /* Start by reading up to 4K at a time. The target will throttle |
3484 | this number down if necessary. */ | |
3485 | buf_alloc = 4096; | |
224c3ddb | 3486 | buf = (gdb_byte *) xmalloc (buf_alloc); |
db1ff28b JK |
3487 | buf_pos = 0; |
3488 | while (1) | |
3489 | { | |
770623f7 | 3490 | n = target_fileio_pread (fd.get (), &buf[buf_pos], |
db1ff28b JK |
3491 | buf_alloc - buf_pos - padding, buf_pos, |
3492 | &target_errno); | |
3493 | if (n < 0) | |
3494 | { | |
3495 | /* An error occurred. */ | |
db1ff28b JK |
3496 | xfree (buf); |
3497 | return -1; | |
3498 | } | |
3499 | else if (n == 0) | |
3500 | { | |
3501 | /* Read all there was. */ | |
db1ff28b JK |
3502 | if (buf_pos == 0) |
3503 | xfree (buf); | |
3504 | else | |
3505 | *buf_p = buf; | |
3506 | return buf_pos; | |
3507 | } | |
3508 | ||
3509 | buf_pos += n; | |
3510 | ||
3511 | /* If the buffer is filling up, expand it. */ | |
3512 | if (buf_alloc < buf_pos * 2) | |
3513 | { | |
3514 | buf_alloc *= 2; | |
224c3ddb | 3515 | buf = (gdb_byte *) xrealloc (buf, buf_alloc); |
db1ff28b JK |
3516 | } |
3517 | ||
3518 | QUIT; | |
3519 | } | |
f7af1fcd JK |
3520 | } |
3521 | ||
12e2a5fd | 3522 | /* See target.h. */ |
7313baad UW |
3523 | |
3524 | LONGEST | |
07c138c8 GB |
3525 | target_fileio_read_alloc (struct inferior *inf, const char *filename, |
3526 | gdb_byte **buf_p) | |
7313baad | 3527 | { |
07c138c8 | 3528 | return target_fileio_read_alloc_1 (inf, filename, buf_p, 0); |
7313baad UW |
3529 | } |
3530 | ||
db1ff28b | 3531 | /* See target.h. */ |
f7af1fcd | 3532 | |
87028b87 | 3533 | gdb::unique_xmalloc_ptr<char> |
f7af1fcd JK |
3534 | target_fileio_read_stralloc (struct inferior *inf, const char *filename) |
3535 | { | |
db1ff28b JK |
3536 | gdb_byte *buffer; |
3537 | char *bufstr; | |
3538 | LONGEST i, transferred; | |
3539 | ||
3540 | transferred = target_fileio_read_alloc_1 (inf, filename, &buffer, 1); | |
3541 | bufstr = (char *) buffer; | |
3542 | ||
3543 | if (transferred < 0) | |
87028b87 | 3544 | return gdb::unique_xmalloc_ptr<char> (nullptr); |
db1ff28b JK |
3545 | |
3546 | if (transferred == 0) | |
b02f78f9 | 3547 | return make_unique_xstrdup (""); |
db1ff28b JK |
3548 | |
3549 | bufstr[transferred] = 0; | |
3550 | ||
3551 | /* Check for embedded NUL bytes; but allow trailing NULs. */ | |
3552 | for (i = strlen (bufstr); i < transferred; i++) | |
3553 | if (bufstr[i] != 0) | |
3554 | { | |
3555 | warning (_("target file %s " | |
3556 | "contained unexpected null characters"), | |
3557 | filename); | |
3558 | break; | |
3559 | } | |
3560 | ||
87028b87 | 3561 | return gdb::unique_xmalloc_ptr<char> (bufstr); |
f7af1fcd | 3562 | } |
7313baad | 3563 | |
db1ff28b | 3564 | |
e0d24f8d | 3565 | static int |
31568a15 TT |
3566 | default_region_ok_for_hw_watchpoint (struct target_ops *self, |
3567 | CORE_ADDR addr, int len) | |
e0d24f8d | 3568 | { |
f5656ead | 3569 | return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT); |
ccaa32c7 GS |
3570 | } |
3571 | ||
5009afc5 AS |
3572 | static int |
3573 | default_watchpoint_addr_within_range (struct target_ops *target, | |
3574 | CORE_ADDR addr, | |
3575 | CORE_ADDR start, int length) | |
3576 | { | |
3577 | return addr >= start && addr < start + length; | |
3578 | } | |
3579 | ||
8b06beed TT |
3580 | /* See target.h. */ |
3581 | ||
a1740ee1 PA |
3582 | target_ops * |
3583 | target_stack::find_beneath (const target_ops *t) const | |
8b06beed | 3584 | { |
a1740ee1 | 3585 | /* Look for a non-empty slot at stratum levels beneath T's. */ |
66b4deae | 3586 | for (int stratum = t->stratum () - 1; stratum >= 0; --stratum) |
a1740ee1 PA |
3587 | if (m_stack[stratum] != NULL) |
3588 | return m_stack[stratum]; | |
8b06beed TT |
3589 | |
3590 | return NULL; | |
3591 | } | |
3592 | ||
a1740ee1 PA |
3593 | /* See target.h. */ |
3594 | ||
3595 | struct target_ops * | |
3596 | find_target_at (enum strata stratum) | |
3597 | { | |
5b6d1e4f | 3598 | return current_inferior ()->target_at (stratum); |
a1740ee1 PA |
3599 | } |
3600 | ||
c906108c | 3601 | \f |
0f48b757 PA |
3602 | |
3603 | /* See target.h */ | |
3604 | ||
3605 | void | |
3606 | target_announce_detach (int from_tty) | |
3607 | { | |
3608 | pid_t pid; | |
a121b7c1 | 3609 | const char *exec_file; |
0f48b757 PA |
3610 | |
3611 | if (!from_tty) | |
3612 | return; | |
3613 | ||
3614 | exec_file = get_exec_file (0); | |
3615 | if (exec_file == NULL) | |
3616 | exec_file = ""; | |
3617 | ||
e99b03dc | 3618 | pid = inferior_ptid.pid (); |
0f48b757 | 3619 | printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file, |
a068643d | 3620 | target_pid_to_str (ptid_t (pid)).c_str ()); |
0f48b757 PA |
3621 | } |
3622 | ||
c906108c SS |
3623 | /* The inferior process has died. Long live the inferior! */ |
3624 | ||
3625 | void | |
fba45db2 | 3626 | generic_mourn_inferior (void) |
c906108c | 3627 | { |
00431a78 | 3628 | inferior *inf = current_inferior (); |
c906108c | 3629 | |
a0776b13 | 3630 | switch_to_no_thread (); |
7f9f62ba | 3631 | |
f59f708a PA |
3632 | /* Mark breakpoints uninserted in case something tries to delete a |
3633 | breakpoint while we delete the inferior's threads (which would | |
3634 | fail, since the inferior is long gone). */ | |
3635 | mark_breakpoints_out (); | |
3636 | ||
00431a78 PA |
3637 | if (inf->pid != 0) |
3638 | exit_inferior (inf); | |
7f9f62ba | 3639 | |
f59f708a PA |
3640 | /* Note this wipes step-resume breakpoints, so needs to be done |
3641 | after exit_inferior, which ends up referencing the step-resume | |
3642 | breakpoints through clear_thread_inferior_resources. */ | |
c906108c | 3643 | breakpoint_init_inferior (inf_exited); |
f59f708a | 3644 | |
c906108c SS |
3645 | registers_changed (); |
3646 | ||
c906108c SS |
3647 | reopen_exec_file (); |
3648 | reinit_frame_cache (); | |
3649 | ||
9a4105ab AC |
3650 | if (deprecated_detach_hook) |
3651 | deprecated_detach_hook (); | |
c906108c SS |
3652 | } |
3653 | \f | |
fd0a2a6f MK |
3654 | /* Convert a normal process ID to a string. Returns the string in a |
3655 | static buffer. */ | |
c906108c | 3656 | |
a068643d | 3657 | std::string |
39f77062 | 3658 | normal_pid_to_str (ptid_t ptid) |
c906108c | 3659 | { |
a068643d | 3660 | return string_printf ("process %d", ptid.pid ()); |
c906108c SS |
3661 | } |
3662 | ||
a068643d | 3663 | static std::string |
770234d3 | 3664 | default_pid_to_str (struct target_ops *ops, ptid_t ptid) |
117de6a9 PA |
3665 | { |
3666 | return normal_pid_to_str (ptid); | |
3667 | } | |
3668 | ||
9b4eba8e HZ |
3669 | /* Error-catcher for target_find_memory_regions. */ |
3670 | static int | |
2e73927c TT |
3671 | dummy_find_memory_regions (struct target_ops *self, |
3672 | find_memory_region_ftype ignore1, void *ignore2) | |
be4d1333 | 3673 | { |
9b4eba8e | 3674 | error (_("Command not implemented for this target.")); |
be4d1333 MS |
3675 | return 0; |
3676 | } | |
3677 | ||
9b4eba8e | 3678 | /* Error-catcher for target_make_corefile_notes. */ |
24f5300a | 3679 | static gdb::unique_xmalloc_ptr<char> |
fc6691b2 TT |
3680 | dummy_make_corefile_notes (struct target_ops *self, |
3681 | bfd *ignore1, int *ignore2) | |
be4d1333 | 3682 | { |
9b4eba8e | 3683 | error (_("Command not implemented for this target.")); |
be4d1333 MS |
3684 | return NULL; |
3685 | } | |
3686 | ||
f6ac5f3d PA |
3687 | #include "target-delegates.c" |
3688 | ||
06b5b831 TT |
3689 | /* The initial current target, so that there is always a semi-valid |
3690 | current target. */ | |
3691 | ||
3692 | static dummy_target the_dummy_target; | |
c906108c | 3693 | |
5b6d1e4f PA |
3694 | /* See target.h. */ |
3695 | ||
3696 | target_ops * | |
3697 | get_dummy_target () | |
3698 | { | |
3699 | return &the_dummy_target; | |
3700 | } | |
3701 | ||
d9f719f1 PA |
3702 | static const target_info dummy_target_info = { |
3703 | "None", | |
3704 | N_("None"), | |
3705 | "" | |
3706 | }; | |
3707 | ||
66b4deae PA |
3708 | strata |
3709 | dummy_target::stratum () const | |
f6ac5f3d | 3710 | { |
66b4deae | 3711 | return dummy_stratum; |
f6ac5f3d PA |
3712 | } |
3713 | ||
66b4deae PA |
3714 | strata |
3715 | debug_target::stratum () const | |
f6ac5f3d | 3716 | { |
66b4deae | 3717 | return debug_stratum; |
f6ac5f3d PA |
3718 | } |
3719 | ||
d9f719f1 PA |
3720 | const target_info & |
3721 | dummy_target::info () const | |
f6ac5f3d | 3722 | { |
d9f719f1 | 3723 | return dummy_target_info; |
f6ac5f3d PA |
3724 | } |
3725 | ||
d9f719f1 PA |
3726 | const target_info & |
3727 | debug_target::info () const | |
f6ac5f3d | 3728 | { |
b6a8c27b | 3729 | return beneath ()->info (); |
f6ac5f3d PA |
3730 | } |
3731 | ||
c906108c | 3732 | \f |
c906108c | 3733 | |
f1c07ab0 | 3734 | void |
460014f5 | 3735 | target_close (struct target_ops *targ) |
f1c07ab0 | 3736 | { |
27f0a431 SM |
3737 | for (inferior *inf : all_inferiors ()) |
3738 | gdb_assert (!inf->target_is_pushed (targ)); | |
7fdc1521 | 3739 | |
20db9c52 PA |
3740 | fileio_handles_invalidate_target (targ); |
3741 | ||
f6ac5f3d | 3742 | targ->close (); |
947b8855 PA |
3743 | |
3744 | if (targetdebug) | |
460014f5 | 3745 | fprintf_unfiltered (gdb_stdlog, "target_close ()\n"); |
f1c07ab0 AC |
3746 | } |
3747 | ||
28439f5e PA |
3748 | int |
3749 | target_thread_alive (ptid_t ptid) | |
c906108c | 3750 | { |
328d42d8 | 3751 | return current_inferior ()->top_target ()->thread_alive (ptid); |
28439f5e PA |
3752 | } |
3753 | ||
3754 | void | |
e8032dde | 3755 | target_update_thread_list (void) |
28439f5e | 3756 | { |
328d42d8 | 3757 | current_inferior ()->top_target ()->update_thread_list (); |
c906108c SS |
3758 | } |
3759 | ||
d914c394 SS |
3760 | void |
3761 | target_stop (ptid_t ptid) | |
3762 | { | |
1192f124 SM |
3763 | process_stratum_target *proc_target = current_inferior ()->process_target (); |
3764 | ||
3765 | gdb_assert (!proc_target->commit_resumed_state); | |
3766 | ||
d914c394 SS |
3767 | if (!may_stop) |
3768 | { | |
3769 | warning (_("May not interrupt or stop the target, ignoring attempt")); | |
3770 | return; | |
3771 | } | |
3772 | ||
328d42d8 | 3773 | current_inferior ()->top_target ()->stop (ptid); |
d914c394 SS |
3774 | } |
3775 | ||
bfedc46a | 3776 | void |
e671cd59 | 3777 | target_interrupt () |
bfedc46a PA |
3778 | { |
3779 | if (!may_stop) | |
3780 | { | |
3781 | warning (_("May not interrupt or stop the target, ignoring attempt")); | |
3782 | return; | |
3783 | } | |
3784 | ||
328d42d8 | 3785 | current_inferior ()->top_target ()->interrupt (); |
bfedc46a PA |
3786 | } |
3787 | ||
abc56d60 PA |
3788 | /* See target.h. */ |
3789 | ||
93692b58 PA |
3790 | void |
3791 | target_pass_ctrlc (void) | |
3792 | { | |
5b6d1e4f PA |
3793 | /* Pass the Ctrl-C to the first target that has a thread |
3794 | running. */ | |
3795 | for (inferior *inf : all_inferiors ()) | |
3796 | { | |
3797 | target_ops *proc_target = inf->process_target (); | |
3798 | if (proc_target == NULL) | |
3799 | continue; | |
3800 | ||
43667cc6 | 3801 | for (thread_info *thr : inf->non_exited_threads ()) |
5b6d1e4f PA |
3802 | { |
3803 | /* A thread can be THREAD_STOPPED and executing, while | |
3804 | running an infcall. */ | |
3805 | if (thr->state == THREAD_RUNNING || thr->executing) | |
3806 | { | |
3807 | /* We can get here quite deep in target layers. Avoid | |
3808 | switching thread context or anything that would | |
3809 | communicate with the target (e.g., to fetch | |
3810 | registers), or flushing e.g., the frame cache. We | |
3811 | just switch inferior in order to be able to call | |
3812 | through the target_stack. */ | |
3813 | scoped_restore_current_inferior restore_inferior; | |
3814 | set_current_inferior (inf); | |
328d42d8 | 3815 | current_inferior ()->top_target ()->pass_ctrlc (); |
5b6d1e4f PA |
3816 | return; |
3817 | } | |
3818 | } | |
3819 | } | |
93692b58 PA |
3820 | } |
3821 | ||
3822 | /* See target.h. */ | |
3823 | ||
3824 | void | |
3825 | default_target_pass_ctrlc (struct target_ops *ops) | |
3826 | { | |
e671cd59 | 3827 | target_interrupt (); |
93692b58 PA |
3828 | } |
3829 | ||
f8c1d06b GB |
3830 | /* See target/target.h. */ |
3831 | ||
3832 | void | |
03f4463b | 3833 | target_stop_and_wait (ptid_t ptid) |
f8c1d06b GB |
3834 | { |
3835 | struct target_waitstatus status; | |
491144b5 | 3836 | bool was_non_stop = non_stop; |
f8c1d06b | 3837 | |
491144b5 | 3838 | non_stop = true; |
f8c1d06b GB |
3839 | target_stop (ptid); |
3840 | ||
3841 | memset (&status, 0, sizeof (status)); | |
3842 | target_wait (ptid, &status, 0); | |
3843 | ||
3844 | non_stop = was_non_stop; | |
3845 | } | |
3846 | ||
3847 | /* See target/target.h. */ | |
3848 | ||
3849 | void | |
03f4463b | 3850 | target_continue_no_signal (ptid_t ptid) |
f8c1d06b GB |
3851 | { |
3852 | target_resume (ptid, 0, GDB_SIGNAL_0); | |
3853 | } | |
3854 | ||
049a8570 SDJ |
3855 | /* See target/target.h. */ |
3856 | ||
3857 | void | |
3858 | target_continue (ptid_t ptid, enum gdb_signal signal) | |
3859 | { | |
3860 | target_resume (ptid, 0, signal); | |
3861 | } | |
3862 | ||
fdbac7d8 | 3863 | /* Concatenate ELEM to LIST, a comma-separated list. */ |
09826ec5 | 3864 | |
09ce46f2 SM |
3865 | static void |
3866 | str_comma_list_concat_elem (std::string *list, const char *elem) | |
09826ec5 | 3867 | { |
09ce46f2 SM |
3868 | if (!list->empty ()) |
3869 | list->append (", "); | |
3870 | ||
3871 | list->append (elem); | |
09826ec5 PA |
3872 | } |
3873 | ||
3874 | /* Helper for target_options_to_string. If OPT is present in | |
3875 | TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET. | |
09ce46f2 | 3876 | OPT is removed from TARGET_OPTIONS. */ |
09826ec5 | 3877 | |
09ce46f2 | 3878 | static void |
b60cea74 TT |
3879 | do_option (target_wait_flags *target_options, std::string *ret, |
3880 | target_wait_flag opt, const char *opt_str) | |
09826ec5 PA |
3881 | { |
3882 | if ((*target_options & opt) != 0) | |
3883 | { | |
09ce46f2 | 3884 | str_comma_list_concat_elem (ret, opt_str); |
09826ec5 PA |
3885 | *target_options &= ~opt; |
3886 | } | |
09826ec5 PA |
3887 | } |
3888 | ||
fdbac7d8 SM |
3889 | /* See target.h. */ |
3890 | ||
09ce46f2 | 3891 | std::string |
b60cea74 | 3892 | target_options_to_string (target_wait_flags target_options) |
09826ec5 | 3893 | { |
09ce46f2 | 3894 | std::string ret; |
09826ec5 PA |
3895 | |
3896 | #define DO_TARG_OPTION(OPT) \ | |
09ce46f2 | 3897 | do_option (&target_options, &ret, OPT, #OPT) |
09826ec5 PA |
3898 | |
3899 | DO_TARG_OPTION (TARGET_WNOHANG); | |
3900 | ||
3901 | if (target_options != 0) | |
09ce46f2 | 3902 | str_comma_list_concat_elem (&ret, "unknown???"); |
09826ec5 | 3903 | |
09826ec5 PA |
3904 | return ret; |
3905 | } | |
3906 | ||
28439f5e PA |
3907 | void |
3908 | target_fetch_registers (struct regcache *regcache, int regno) | |
c906108c | 3909 | { |
328d42d8 | 3910 | current_inferior ()->top_target ()->fetch_registers (regcache, regno); |
ad5989bd | 3911 | if (targetdebug) |
ef79d9a3 | 3912 | regcache->debug_print_register ("target_fetch_registers", regno); |
c906108c SS |
3913 | } |
3914 | ||
28439f5e PA |
3915 | void |
3916 | target_store_registers (struct regcache *regcache, int regno) | |
c906108c | 3917 | { |
d914c394 SS |
3918 | if (!may_write_registers) |
3919 | error (_("Writing to registers is not allowed (regno %d)"), regno); | |
3920 | ||
328d42d8 | 3921 | current_inferior ()->top_target ()->store_registers (regcache, regno); |
6b84065d | 3922 | if (targetdebug) |
28439f5e | 3923 | { |
ef79d9a3 | 3924 | regcache->debug_print_register ("target_store_registers", regno); |
28439f5e | 3925 | } |
c906108c SS |
3926 | } |
3927 | ||
dc146f7c VP |
3928 | int |
3929 | target_core_of_thread (ptid_t ptid) | |
3930 | { | |
328d42d8 | 3931 | return current_inferior ()->top_target ()->core_of_thread (ptid); |
dc146f7c VP |
3932 | } |
3933 | ||
936d2992 PA |
3934 | int |
3935 | simple_verify_memory (struct target_ops *ops, | |
3936 | const gdb_byte *data, CORE_ADDR lma, ULONGEST size) | |
3937 | { | |
3938 | LONGEST total_xfered = 0; | |
3939 | ||
3940 | while (total_xfered < size) | |
3941 | { | |
3942 | ULONGEST xfered_len; | |
3943 | enum target_xfer_status status; | |
3944 | gdb_byte buf[1024]; | |
768adc05 | 3945 | ULONGEST howmuch = std::min<ULONGEST> (sizeof (buf), size - total_xfered); |
936d2992 PA |
3946 | |
3947 | status = target_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL, | |
3948 | buf, NULL, lma + total_xfered, howmuch, | |
3949 | &xfered_len); | |
3950 | if (status == TARGET_XFER_OK | |
3951 | && memcmp (data + total_xfered, buf, xfered_len) == 0) | |
3952 | { | |
3953 | total_xfered += xfered_len; | |
3954 | QUIT; | |
3955 | } | |
3956 | else | |
3957 | return 0; | |
3958 | } | |
3959 | return 1; | |
3960 | } | |
3961 | ||
3962 | /* Default implementation of memory verification. */ | |
3963 | ||
3964 | static int | |
3965 | default_verify_memory (struct target_ops *self, | |
3966 | const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size) | |
3967 | { | |
3968 | /* Start over from the top of the target stack. */ | |
328d42d8 | 3969 | return simple_verify_memory (current_inferior ()->top_target (), |
936d2992 PA |
3970 | data, memaddr, size); |
3971 | } | |
3972 | ||
4a5e7a5b PA |
3973 | int |
3974 | target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size) | |
3975 | { | |
328d42d8 SM |
3976 | target_ops *target = current_inferior ()->top_target (); |
3977 | ||
3978 | return target->verify_memory (data, memaddr, size); | |
4a5e7a5b PA |
3979 | } |
3980 | ||
9c06b0b4 TJB |
3981 | /* The documentation for this function is in its prototype declaration in |
3982 | target.h. */ | |
3983 | ||
3984 | int | |
f4b0a671 SM |
3985 | target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, |
3986 | enum target_hw_bp_type rw) | |
9c06b0b4 | 3987 | { |
328d42d8 SM |
3988 | target_ops *target = current_inferior ()->top_target (); |
3989 | ||
3990 | return target->insert_mask_watchpoint (addr, mask, rw); | |
9c06b0b4 TJB |
3991 | } |
3992 | ||
3993 | /* The documentation for this function is in its prototype declaration in | |
3994 | target.h. */ | |
3995 | ||
3996 | int | |
f4b0a671 SM |
3997 | target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, |
3998 | enum target_hw_bp_type rw) | |
9c06b0b4 | 3999 | { |
328d42d8 SM |
4000 | target_ops *target = current_inferior ()->top_target (); |
4001 | ||
4002 | return target->remove_mask_watchpoint (addr, mask, rw); | |
9c06b0b4 TJB |
4003 | } |
4004 | ||
4005 | /* The documentation for this function is in its prototype declaration | |
4006 | in target.h. */ | |
4007 | ||
4008 | int | |
4009 | target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask) | |
4010 | { | |
328d42d8 SM |
4011 | target_ops *target = current_inferior ()->top_target (); |
4012 | ||
4013 | return target->masked_watch_num_registers (addr, mask); | |
9c06b0b4 TJB |
4014 | } |
4015 | ||
f1310107 TJB |
4016 | /* The documentation for this function is in its prototype declaration |
4017 | in target.h. */ | |
4018 | ||
4019 | int | |
4020 | target_ranged_break_num_registers (void) | |
4021 | { | |
328d42d8 | 4022 | return current_inferior ()->top_target ()->ranged_break_num_registers (); |
f1310107 TJB |
4023 | } |
4024 | ||
02d27625 MM |
4025 | /* See target.h. */ |
4026 | ||
02d27625 | 4027 | struct btrace_target_info * |
f4abbc16 | 4028 | target_enable_btrace (ptid_t ptid, const struct btrace_config *conf) |
02d27625 | 4029 | { |
328d42d8 | 4030 | return current_inferior ()->top_target ()->enable_btrace (ptid, conf); |
02d27625 MM |
4031 | } |
4032 | ||
4033 | /* See target.h. */ | |
4034 | ||
4035 | void | |
4036 | target_disable_btrace (struct btrace_target_info *btinfo) | |
4037 | { | |
328d42d8 | 4038 | current_inferior ()->top_target ()->disable_btrace (btinfo); |
02d27625 MM |
4039 | } |
4040 | ||
4041 | /* See target.h. */ | |
4042 | ||
4043 | void | |
4044 | target_teardown_btrace (struct btrace_target_info *btinfo) | |
4045 | { | |
328d42d8 | 4046 | current_inferior ()->top_target ()->teardown_btrace (btinfo); |
02d27625 MM |
4047 | } |
4048 | ||
4049 | /* See target.h. */ | |
4050 | ||
969c39fb | 4051 | enum btrace_error |
734b0e4b | 4052 | target_read_btrace (struct btrace_data *btrace, |
969c39fb | 4053 | struct btrace_target_info *btinfo, |
02d27625 MM |
4054 | enum btrace_read_type type) |
4055 | { | |
328d42d8 SM |
4056 | target_ops *target = current_inferior ()->top_target (); |
4057 | ||
4058 | return target->read_btrace (btrace, btinfo, type); | |
02d27625 MM |
4059 | } |
4060 | ||
d02ed0bb MM |
4061 | /* See target.h. */ |
4062 | ||
f4abbc16 MM |
4063 | const struct btrace_config * |
4064 | target_btrace_conf (const struct btrace_target_info *btinfo) | |
4065 | { | |
328d42d8 | 4066 | return current_inferior ()->top_target ()->btrace_conf (btinfo); |
f4abbc16 MM |
4067 | } |
4068 | ||
4069 | /* See target.h. */ | |
4070 | ||
7c1687a9 MM |
4071 | void |
4072 | target_stop_recording (void) | |
4073 | { | |
328d42d8 | 4074 | current_inferior ()->top_target ()->stop_recording (); |
7c1687a9 MM |
4075 | } |
4076 | ||
4077 | /* See target.h. */ | |
4078 | ||
d02ed0bb | 4079 | void |
85e1311a | 4080 | target_save_record (const char *filename) |
d02ed0bb | 4081 | { |
328d42d8 | 4082 | current_inferior ()->top_target ()->save_record (filename); |
d02ed0bb MM |
4083 | } |
4084 | ||
4085 | /* See target.h. */ | |
4086 | ||
4087 | int | |
f6ac5f3d | 4088 | target_supports_delete_record () |
d02ed0bb | 4089 | { |
328d42d8 | 4090 | return current_inferior ()->top_target ()->supports_delete_record (); |
d02ed0bb MM |
4091 | } |
4092 | ||
4093 | /* See target.h. */ | |
4094 | ||
4095 | void | |
4096 | target_delete_record (void) | |
4097 | { | |
328d42d8 | 4098 | current_inferior ()->top_target ()->delete_record (); |
d02ed0bb MM |
4099 | } |
4100 | ||
4101 | /* See target.h. */ | |
4102 | ||
b158a20f TW |
4103 | enum record_method |
4104 | target_record_method (ptid_t ptid) | |
4105 | { | |
328d42d8 | 4106 | return current_inferior ()->top_target ()->record_method (ptid); |
b158a20f TW |
4107 | } |
4108 | ||
4109 | /* See target.h. */ | |
4110 | ||
d02ed0bb | 4111 | int |
a52eab48 | 4112 | target_record_is_replaying (ptid_t ptid) |
d02ed0bb | 4113 | { |
328d42d8 | 4114 | return current_inferior ()->top_target ()->record_is_replaying (ptid); |
d02ed0bb MM |
4115 | } |
4116 | ||
4117 | /* See target.h. */ | |
4118 | ||
7ff27e9b MM |
4119 | int |
4120 | target_record_will_replay (ptid_t ptid, int dir) | |
4121 | { | |
328d42d8 | 4122 | return current_inferior ()->top_target ()->record_will_replay (ptid, dir); |
7ff27e9b MM |
4123 | } |
4124 | ||
4125 | /* See target.h. */ | |
4126 | ||
797094dd MM |
4127 | void |
4128 | target_record_stop_replaying (void) | |
4129 | { | |
328d42d8 | 4130 | current_inferior ()->top_target ()->record_stop_replaying (); |
797094dd MM |
4131 | } |
4132 | ||
4133 | /* See target.h. */ | |
4134 | ||
d02ed0bb MM |
4135 | void |
4136 | target_goto_record_begin (void) | |
4137 | { | |
328d42d8 | 4138 | current_inferior ()->top_target ()->goto_record_begin (); |
d02ed0bb MM |
4139 | } |
4140 | ||
4141 | /* See target.h. */ | |
4142 | ||
4143 | void | |
4144 | target_goto_record_end (void) | |
4145 | { | |
328d42d8 | 4146 | current_inferior ()->top_target ()->goto_record_end (); |
d02ed0bb MM |
4147 | } |
4148 | ||
4149 | /* See target.h. */ | |
4150 | ||
4151 | void | |
4152 | target_goto_record (ULONGEST insn) | |
4153 | { | |
328d42d8 | 4154 | current_inferior ()->top_target ()->goto_record (insn); |
d02ed0bb MM |
4155 | } |
4156 | ||
67c86d06 MM |
4157 | /* See target.h. */ |
4158 | ||
4159 | void | |
9a24775b | 4160 | target_insn_history (int size, gdb_disassembly_flags flags) |
67c86d06 | 4161 | { |
328d42d8 | 4162 | current_inferior ()->top_target ()->insn_history (size, flags); |
67c86d06 MM |
4163 | } |
4164 | ||
4165 | /* See target.h. */ | |
4166 | ||
4167 | void | |
9a24775b PA |
4168 | target_insn_history_from (ULONGEST from, int size, |
4169 | gdb_disassembly_flags flags) | |
67c86d06 | 4170 | { |
328d42d8 | 4171 | current_inferior ()->top_target ()->insn_history_from (from, size, flags); |
67c86d06 MM |
4172 | } |
4173 | ||
4174 | /* See target.h. */ | |
4175 | ||
4176 | void | |
9a24775b PA |
4177 | target_insn_history_range (ULONGEST begin, ULONGEST end, |
4178 | gdb_disassembly_flags flags) | |
67c86d06 | 4179 | { |
328d42d8 | 4180 | current_inferior ()->top_target ()->insn_history_range (begin, end, flags); |
67c86d06 MM |
4181 | } |
4182 | ||
15984c13 MM |
4183 | /* See target.h. */ |
4184 | ||
4185 | void | |
0cb7c7b0 | 4186 | target_call_history (int size, record_print_flags flags) |
15984c13 | 4187 | { |
328d42d8 | 4188 | current_inferior ()->top_target ()->call_history (size, flags); |
15984c13 MM |
4189 | } |
4190 | ||
4191 | /* See target.h. */ | |
4192 | ||
4193 | void | |
0cb7c7b0 | 4194 | target_call_history_from (ULONGEST begin, int size, record_print_flags flags) |
15984c13 | 4195 | { |
328d42d8 | 4196 | current_inferior ()->top_target ()->call_history_from (begin, size, flags); |
15984c13 MM |
4197 | } |
4198 | ||
4199 | /* See target.h. */ | |
4200 | ||
4201 | void | |
0cb7c7b0 | 4202 | target_call_history_range (ULONGEST begin, ULONGEST end, record_print_flags flags) |
15984c13 | 4203 | { |
328d42d8 | 4204 | current_inferior ()->top_target ()->call_history_range (begin, end, flags); |
15984c13 MM |
4205 | } |
4206 | ||
ea001bdc MM |
4207 | /* See target.h. */ |
4208 | ||
4209 | const struct frame_unwind * | |
4210 | target_get_unwinder (void) | |
4211 | { | |
328d42d8 | 4212 | return current_inferior ()->top_target ()->get_unwinder (); |
ea001bdc MM |
4213 | } |
4214 | ||
4215 | /* See target.h. */ | |
4216 | ||
4217 | const struct frame_unwind * | |
4218 | target_get_tailcall_unwinder (void) | |
4219 | { | |
328d42d8 | 4220 | return current_inferior ()->top_target ()->get_tailcall_unwinder (); |
ea001bdc MM |
4221 | } |
4222 | ||
5fff78c4 MM |
4223 | /* See target.h. */ |
4224 | ||
4225 | void | |
4226 | target_prepare_to_generate_core (void) | |
4227 | { | |
328d42d8 | 4228 | current_inferior ()->top_target ()->prepare_to_generate_core (); |
5fff78c4 MM |
4229 | } |
4230 | ||
4231 | /* See target.h. */ | |
4232 | ||
4233 | void | |
4234 | target_done_generating_core (void) | |
4235 | { | |
328d42d8 | 4236 | current_inferior ()->top_target ()->done_generating_core (); |
5fff78c4 MM |
4237 | } |
4238 | ||
c906108c | 4239 | \f |
c5aa993b JM |
4240 | |
4241 | static char targ_desc[] = | |
3e43a32a MS |
4242 | "Names of targets and files being debugged.\nShows the entire \ |
4243 | stack of targets currently in use (including the exec-file,\n\ | |
c906108c SS |
4244 | core-file, and process, if any), as well as the symbol file name."; |
4245 | ||
a53f3625 | 4246 | static void |
a30bf1f1 TT |
4247 | default_rcmd (struct target_ops *self, const char *command, |
4248 | struct ui_file *output) | |
a53f3625 TT |
4249 | { |
4250 | error (_("\"monitor\" command not supported by this target.")); | |
4251 | } | |
4252 | ||
96baa820 | 4253 | static void |
0b39b52e | 4254 | do_monitor_command (const char *cmd, int from_tty) |
96baa820 | 4255 | { |
96baa820 JM |
4256 | target_rcmd (cmd, gdb_stdtarg); |
4257 | } | |
4258 | ||
78cbbba8 LM |
4259 | /* Erases all the memory regions marked as flash. CMD and FROM_TTY are |
4260 | ignored. */ | |
4261 | ||
4262 | void | |
0b39b52e | 4263 | flash_erase_command (const char *cmd, int from_tty) |
78cbbba8 LM |
4264 | { |
4265 | /* Used to communicate termination of flash operations to the target. */ | |
4266 | bool found_flash_region = false; | |
78cbbba8 LM |
4267 | struct gdbarch *gdbarch = target_gdbarch (); |
4268 | ||
a664f67e | 4269 | std::vector<mem_region> mem_regions = target_memory_map (); |
78cbbba8 LM |
4270 | |
4271 | /* Iterate over all memory regions. */ | |
a664f67e | 4272 | for (const mem_region &m : mem_regions) |
78cbbba8 | 4273 | { |
78cbbba8 | 4274 | /* Is this a flash memory region? */ |
a664f67e | 4275 | if (m.attrib.mode == MEM_FLASH) |
dda83cd7 SM |
4276 | { |
4277 | found_flash_region = true; | |
4278 | target_flash_erase (m.lo, m.hi - m.lo); | |
78cbbba8 | 4279 | |
76f9c9cf | 4280 | ui_out_emit_tuple tuple_emitter (current_uiout, "erased-regions"); |
78cbbba8 | 4281 | |
dda83cd7 SM |
4282 | current_uiout->message (_("Erasing flash memory region at address ")); |
4283 | current_uiout->field_core_addr ("address", gdbarch, m.lo); | |
4284 | current_uiout->message (", size = "); | |
4285 | current_uiout->field_string ("size", hex_string (m.hi - m.lo)); | |
4286 | current_uiout->message ("\n"); | |
4287 | } | |
78cbbba8 LM |
4288 | } |
4289 | ||
4290 | /* Did we do any flash operations? If so, we need to finalize them. */ | |
4291 | if (found_flash_region) | |
4292 | target_flash_done (); | |
4293 | else | |
4294 | current_uiout->message (_("No flash memory regions found.\n")); | |
4295 | } | |
4296 | ||
87680a14 JB |
4297 | /* Print the name of each layers of our target stack. */ |
4298 | ||
4299 | static void | |
d3cb6b99 | 4300 | maintenance_print_target_stack (const char *cmd, int from_tty) |
87680a14 | 4301 | { |
87680a14 JB |
4302 | printf_filtered (_("The current target stack is:\n")); |
4303 | ||
328d42d8 SM |
4304 | for (target_ops *t = current_inferior ()->top_target (); |
4305 | t != NULL; | |
4306 | t = t->beneath ()) | |
87680a14 | 4307 | { |
66b4deae | 4308 | if (t->stratum () == debug_stratum) |
f6ac5f3d PA |
4309 | continue; |
4310 | printf_filtered (" - %s (%s)\n", t->shortname (), t->longname ()); | |
87680a14 JB |
4311 | } |
4312 | } | |
4313 | ||
372316f1 PA |
4314 | /* See target.h. */ |
4315 | ||
4316 | void | |
4317 | target_async (int enable) | |
4318 | { | |
4319 | infrun_async (enable); | |
328d42d8 | 4320 | current_inferior ()->top_target ()->async (enable); |
372316f1 PA |
4321 | } |
4322 | ||
65706a29 PA |
4323 | /* See target.h. */ |
4324 | ||
4325 | void | |
4326 | target_thread_events (int enable) | |
4327 | { | |
328d42d8 | 4328 | current_inferior ()->top_target ()->thread_events (enable); |
65706a29 PA |
4329 | } |
4330 | ||
329ea579 PA |
4331 | /* Controls if targets can report that they can/are async. This is |
4332 | just for maintainers to use when debugging gdb. */ | |
491144b5 | 4333 | bool target_async_permitted = true; |
c6ebd6cf VP |
4334 | |
4335 | /* The set command writes to this variable. If the inferior is | |
b5419e49 | 4336 | executing, target_async_permitted is *not* updated. */ |
491144b5 | 4337 | static bool target_async_permitted_1 = true; |
c6ebd6cf VP |
4338 | |
4339 | static void | |
eb4c3f4a | 4340 | maint_set_target_async_command (const char *args, int from_tty, |
329ea579 | 4341 | struct cmd_list_element *c) |
c6ebd6cf | 4342 | { |
c35b1492 | 4343 | if (have_live_inferiors ()) |
c6ebd6cf VP |
4344 | { |
4345 | target_async_permitted_1 = target_async_permitted; | |
4346 | error (_("Cannot change this setting while the inferior is running.")); | |
4347 | } | |
4348 | ||
4349 | target_async_permitted = target_async_permitted_1; | |
4350 | } | |
4351 | ||
4352 | static void | |
329ea579 PA |
4353 | maint_show_target_async_command (struct ui_file *file, int from_tty, |
4354 | struct cmd_list_element *c, | |
4355 | const char *value) | |
c6ebd6cf | 4356 | { |
3e43a32a MS |
4357 | fprintf_filtered (file, |
4358 | _("Controlling the inferior in " | |
4359 | "asynchronous mode is %s.\n"), value); | |
c6ebd6cf VP |
4360 | } |
4361 | ||
fbea99ea PA |
4362 | /* Return true if the target operates in non-stop mode even with "set |
4363 | non-stop off". */ | |
4364 | ||
4365 | static int | |
4366 | target_always_non_stop_p (void) | |
4367 | { | |
328d42d8 | 4368 | return current_inferior ()->top_target ()->always_non_stop_p (); |
fbea99ea PA |
4369 | } |
4370 | ||
4371 | /* See target.h. */ | |
4372 | ||
6ff267e1 SM |
4373 | bool |
4374 | target_is_non_stop_p () | |
fbea99ea | 4375 | { |
e5b9b39f PA |
4376 | return ((non_stop |
4377 | || target_non_stop_enabled == AUTO_BOOLEAN_TRUE | |
4378 | || (target_non_stop_enabled == AUTO_BOOLEAN_AUTO | |
4379 | && target_always_non_stop_p ())) | |
4380 | && target_can_async_p ()); | |
fbea99ea PA |
4381 | } |
4382 | ||
a0714d30 TBA |
4383 | /* See target.h. */ |
4384 | ||
4385 | bool | |
4386 | exists_non_stop_target () | |
4387 | { | |
4388 | if (target_is_non_stop_p ()) | |
4389 | return true; | |
4390 | ||
4391 | scoped_restore_current_thread restore_thread; | |
4392 | ||
4393 | for (inferior *inf : all_inferiors ()) | |
4394 | { | |
4395 | switch_to_inferior_no_thread (inf); | |
4396 | if (target_is_non_stop_p ()) | |
4397 | return true; | |
4398 | } | |
4399 | ||
4400 | return false; | |
4401 | } | |
4402 | ||
fbea99ea PA |
4403 | /* Controls if targets can report that they always run in non-stop |
4404 | mode. This is just for maintainers to use when debugging gdb. */ | |
4405 | enum auto_boolean target_non_stop_enabled = AUTO_BOOLEAN_AUTO; | |
4406 | ||
4407 | /* The set command writes to this variable. If the inferior is | |
4408 | executing, target_non_stop_enabled is *not* updated. */ | |
4409 | static enum auto_boolean target_non_stop_enabled_1 = AUTO_BOOLEAN_AUTO; | |
4410 | ||
4411 | /* Implementation of "maint set target-non-stop". */ | |
4412 | ||
4413 | static void | |
eb4c3f4a | 4414 | maint_set_target_non_stop_command (const char *args, int from_tty, |
fbea99ea PA |
4415 | struct cmd_list_element *c) |
4416 | { | |
4417 | if (have_live_inferiors ()) | |
4418 | { | |
4419 | target_non_stop_enabled_1 = target_non_stop_enabled; | |
4420 | error (_("Cannot change this setting while the inferior is running.")); | |
4421 | } | |
4422 | ||
4423 | target_non_stop_enabled = target_non_stop_enabled_1; | |
4424 | } | |
4425 | ||
4426 | /* Implementation of "maint show target-non-stop". */ | |
4427 | ||
4428 | static void | |
4429 | maint_show_target_non_stop_command (struct ui_file *file, int from_tty, | |
4430 | struct cmd_list_element *c, | |
4431 | const char *value) | |
4432 | { | |
4433 | if (target_non_stop_enabled == AUTO_BOOLEAN_AUTO) | |
4434 | fprintf_filtered (file, | |
4435 | _("Whether the target is always in non-stop mode " | |
4436 | "is %s (currently %s).\n"), value, | |
4437 | target_always_non_stop_p () ? "on" : "off"); | |
4438 | else | |
4439 | fprintf_filtered (file, | |
4440 | _("Whether the target is always in non-stop mode " | |
4441 | "is %s.\n"), value); | |
4442 | } | |
4443 | ||
d914c394 SS |
4444 | /* Temporary copies of permission settings. */ |
4445 | ||
491144b5 CB |
4446 | static bool may_write_registers_1 = true; |
4447 | static bool may_write_memory_1 = true; | |
4448 | static bool may_insert_breakpoints_1 = true; | |
4449 | static bool may_insert_tracepoints_1 = true; | |
4450 | static bool may_insert_fast_tracepoints_1 = true; | |
4451 | static bool may_stop_1 = true; | |
d914c394 SS |
4452 | |
4453 | /* Make the user-set values match the real values again. */ | |
4454 | ||
4455 | void | |
4456 | update_target_permissions (void) | |
4457 | { | |
4458 | may_write_registers_1 = may_write_registers; | |
4459 | may_write_memory_1 = may_write_memory; | |
4460 | may_insert_breakpoints_1 = may_insert_breakpoints; | |
4461 | may_insert_tracepoints_1 = may_insert_tracepoints; | |
4462 | may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints; | |
4463 | may_stop_1 = may_stop; | |
4464 | } | |
4465 | ||
4466 | /* The one function handles (most of) the permission flags in the same | |
4467 | way. */ | |
4468 | ||
4469 | static void | |
eb4c3f4a | 4470 | set_target_permissions (const char *args, int from_tty, |
d914c394 SS |
4471 | struct cmd_list_element *c) |
4472 | { | |
55f6301a | 4473 | if (target_has_execution ()) |
d914c394 SS |
4474 | { |
4475 | update_target_permissions (); | |
4476 | error (_("Cannot change this setting while the inferior is running.")); | |
4477 | } | |
4478 | ||
4479 | /* Make the real values match the user-changed values. */ | |
4480 | may_write_registers = may_write_registers_1; | |
4481 | may_insert_breakpoints = may_insert_breakpoints_1; | |
4482 | may_insert_tracepoints = may_insert_tracepoints_1; | |
4483 | may_insert_fast_tracepoints = may_insert_fast_tracepoints_1; | |
4484 | may_stop = may_stop_1; | |
4485 | update_observer_mode (); | |
4486 | } | |
4487 | ||
4488 | /* Set memory write permission independently of observer mode. */ | |
4489 | ||
4490 | static void | |
eb4c3f4a | 4491 | set_write_memory_permission (const char *args, int from_tty, |
d914c394 SS |
4492 | struct cmd_list_element *c) |
4493 | { | |
4494 | /* Make the real values match the user-changed values. */ | |
4495 | may_write_memory = may_write_memory_1; | |
4496 | update_observer_mode (); | |
4497 | } | |
4498 | ||
6c265988 SM |
4499 | void _initialize_target (); |
4500 | ||
c906108c | 4501 | void |
5b6d1e4f | 4502 | _initialize_target () |
c906108c | 4503 | { |
f6ac5f3d | 4504 | the_debug_target = new debug_target (); |
c906108c | 4505 | |
11db9430 SM |
4506 | add_info ("target", info_target_command, targ_desc); |
4507 | add_info ("files", info_target_command, targ_desc); | |
c906108c | 4508 | |
ccce17b0 | 4509 | add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\ |
85c07804 AC |
4510 | Set target debugging."), _("\ |
4511 | Show target debugging."), _("\ | |
333dabeb | 4512 | When non-zero, target debugging is enabled. Higher numbers are more\n\ |
3cecbbbe TT |
4513 | verbose."), |
4514 | set_targetdebug, | |
ccce17b0 YQ |
4515 | show_targetdebug, |
4516 | &setdebuglist, &showdebuglist); | |
3a11626d | 4517 | |
2bc416ba | 4518 | add_setshow_boolean_cmd ("trust-readonly-sections", class_support, |
7915a72c AC |
4519 | &trust_readonly, _("\ |
4520 | Set mode for reading from readonly sections."), _("\ | |
4521 | Show mode for reading from readonly sections."), _("\ | |
3a11626d MS |
4522 | When this mode is on, memory reads from readonly sections (such as .text)\n\ |
4523 | will be read from the object file instead of from the target. This will\n\ | |
7915a72c | 4524 | result in significant performance improvement for remote targets."), |
2c5b56ce | 4525 | NULL, |
920d2a44 | 4526 | show_trust_readonly, |
e707bbc2 | 4527 | &setlist, &showlist); |
96baa820 JM |
4528 | |
4529 | add_com ("monitor", class_obscure, do_monitor_command, | |
1bedd215 | 4530 | _("Send a command to the remote monitor (remote targets only).")); |
96baa820 | 4531 | |
87680a14 | 4532 | add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack, |
dda83cd7 SM |
4533 | _("Print the name of each layer of the internal target stack."), |
4534 | &maintenanceprintlist); | |
87680a14 | 4535 | |
c6ebd6cf VP |
4536 | add_setshow_boolean_cmd ("target-async", no_class, |
4537 | &target_async_permitted_1, _("\ | |
4538 | Set whether gdb controls the inferior in asynchronous mode."), _("\ | |
4539 | Show whether gdb controls the inferior in asynchronous mode."), _("\ | |
4540 | Tells gdb whether to control the inferior in asynchronous mode."), | |
329ea579 PA |
4541 | maint_set_target_async_command, |
4542 | maint_show_target_async_command, | |
4543 | &maintenance_set_cmdlist, | |
4544 | &maintenance_show_cmdlist); | |
c6ebd6cf | 4545 | |
fbea99ea PA |
4546 | add_setshow_auto_boolean_cmd ("target-non-stop", no_class, |
4547 | &target_non_stop_enabled_1, _("\ | |
4548 | Set whether gdb always controls the inferior in non-stop mode."), _("\ | |
4549 | Show whether gdb always controls the inferior in non-stop mode."), _("\ | |
4550 | Tells gdb whether to control the inferior in non-stop mode."), | |
4551 | maint_set_target_non_stop_command, | |
4552 | maint_show_target_non_stop_command, | |
4553 | &maintenance_set_cmdlist, | |
4554 | &maintenance_show_cmdlist); | |
4555 | ||
d914c394 SS |
4556 | add_setshow_boolean_cmd ("may-write-registers", class_support, |
4557 | &may_write_registers_1, _("\ | |
4558 | Set permission to write into registers."), _("\ | |
4559 | Show permission to write into registers."), _("\ | |
4560 | When this permission is on, GDB may write into the target's registers.\n\ | |
4561 | Otherwise, any sort of write attempt will result in an error."), | |
4562 | set_target_permissions, NULL, | |
4563 | &setlist, &showlist); | |
4564 | ||
4565 | add_setshow_boolean_cmd ("may-write-memory", class_support, | |
4566 | &may_write_memory_1, _("\ | |
4567 | Set permission to write into target memory."), _("\ | |
4568 | Show permission to write into target memory."), _("\ | |
4569 | When this permission is on, GDB may write into the target's memory.\n\ | |
4570 | Otherwise, any sort of write attempt will result in an error."), | |
4571 | set_write_memory_permission, NULL, | |
4572 | &setlist, &showlist); | |
4573 | ||
4574 | add_setshow_boolean_cmd ("may-insert-breakpoints", class_support, | |
4575 | &may_insert_breakpoints_1, _("\ | |
4576 | Set permission to insert breakpoints in the target."), _("\ | |
4577 | Show permission to insert breakpoints in the target."), _("\ | |
4578 | When this permission is on, GDB may insert breakpoints in the program.\n\ | |
4579 | Otherwise, any sort of insertion attempt will result in an error."), | |
4580 | set_target_permissions, NULL, | |
4581 | &setlist, &showlist); | |
4582 | ||
4583 | add_setshow_boolean_cmd ("may-insert-tracepoints", class_support, | |
4584 | &may_insert_tracepoints_1, _("\ | |
4585 | Set permission to insert tracepoints in the target."), _("\ | |
4586 | Show permission to insert tracepoints in the target."), _("\ | |
4587 | When this permission is on, GDB may insert tracepoints in the program.\n\ | |
4588 | Otherwise, any sort of insertion attempt will result in an error."), | |
4589 | set_target_permissions, NULL, | |
4590 | &setlist, &showlist); | |
4591 | ||
4592 | add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support, | |
4593 | &may_insert_fast_tracepoints_1, _("\ | |
4594 | Set permission to insert fast tracepoints in the target."), _("\ | |
4595 | Show permission to insert fast tracepoints in the target."), _("\ | |
4596 | When this permission is on, GDB may insert fast tracepoints.\n\ | |
4597 | Otherwise, any sort of insertion attempt will result in an error."), | |
4598 | set_target_permissions, NULL, | |
4599 | &setlist, &showlist); | |
4600 | ||
4601 | add_setshow_boolean_cmd ("may-interrupt", class_support, | |
4602 | &may_stop_1, _("\ | |
4603 | Set permission to interrupt or signal the target."), _("\ | |
4604 | Show permission to interrupt or signal the target."), _("\ | |
4605 | When this permission is on, GDB may interrupt/stop the target's execution.\n\ | |
4606 | Otherwise, any attempt to interrupt or stop will be ignored."), | |
4607 | set_target_permissions, NULL, | |
4608 | &setlist, &showlist); | |
6a3cb8e8 | 4609 | |
78cbbba8 | 4610 | add_com ("flash-erase", no_class, flash_erase_command, |
dda83cd7 | 4611 | _("Erase all flash memory regions.")); |
78cbbba8 | 4612 | |
6a3cb8e8 PA |
4613 | add_setshow_boolean_cmd ("auto-connect-native-target", class_support, |
4614 | &auto_connect_native_target, _("\ | |
4615 | Set whether GDB may automatically connect to the native target."), _("\ | |
4616 | Show whether GDB may automatically connect to the native target."), _("\ | |
4617 | When on, and GDB is not connected to a target yet, GDB\n\ | |
4618 | attempts \"run\" and other commands with the native target."), | |
4619 | NULL, show_auto_connect_native_target, | |
4620 | &setlist, &showlist); | |
c906108c | 4621 | } |