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