]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target.c
support_dt_relr aarch64
[thirdparty/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3 2
1d506c26 3 Copyright (C) 1990-2024 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"
d9f719f1 53#include <unordered_map>
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
f0f9ff95
TT
59static void generic_tls_error (void) ATTRIBUTE_NORETURN;
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
c25c4a8b 67static void tcomplain (void) ATTRIBUTE_NORETURN;
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. */
75static std::unordered_map<const target_info *, target_open_ftype *>
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
72f5cf0e 1253/* Using the objfile specified in OBJFILE, find the address for the
9e35dae4
DJ
1254 current thread's thread-local storage with offset OFFSET. */
1255CORE_ADDR
1256target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1257{
1258 volatile CORE_ADDR addr = 0;
328d42d8 1259 struct target_ops *target = current_inferior ()->top_target ();
99d9c3b9 1260 gdbarch *gdbarch = current_inferior ()->arch ();
9e35dae4 1261
4cc98c36
JB
1262 /* If OBJFILE is a separate debug object file, look for the
1263 original object file. */
1264 if (objfile->separate_debug_objfile_backlink != NULL)
1265 objfile = objfile->separate_debug_objfile_backlink;
1266
6e056c81 1267 if (gdbarch_fetch_tls_load_module_address_p (gdbarch))
9e35dae4
DJ
1268 {
1269 ptid_t ptid = inferior_ptid;
9e35dae4 1270
a70b8144 1271 try
9e35dae4
DJ
1272 {
1273 CORE_ADDR lm_addr;
1274
1275 /* Fetch the load module address for this objfile. */
6e056c81 1276 lm_addr = gdbarch_fetch_tls_load_module_address (gdbarch,
dda83cd7 1277 objfile);
9e35dae4 1278
6e056c81
JB
1279 if (gdbarch_get_thread_local_address_p (gdbarch))
1280 addr = gdbarch_get_thread_local_address (gdbarch, ptid, lm_addr,
1281 offset);
1282 else
1283 addr = target->get_thread_local_address (ptid, lm_addr, offset);
9e35dae4
DJ
1284 }
1285 /* If an error occurred, print TLS related messages here. Otherwise,
dda83cd7 1286 throw the error to some higher catcher. */
230d2906 1287 catch (const gdb_exception &ex)
9e35dae4
DJ
1288 {
1289 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1290
1291 switch (ex.error)
1292 {
1293 case TLS_NO_LIBRARY_SUPPORT_ERROR:
3e43a32a
MS
1294 error (_("Cannot find thread-local variables "
1295 "in this thread library."));
9e35dae4
DJ
1296 break;
1297 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1298 if (objfile_is_library)
1299 error (_("Cannot find shared library `%s' in dynamic"
dda83cd7 1300 " linker's load module list"), objfile_name (objfile));
9e35dae4
DJ
1301 else
1302 error (_("Cannot find executable file `%s' in dynamic"
dda83cd7 1303 " linker's load module list"), objfile_name (objfile));
9e35dae4
DJ
1304 break;
1305 case TLS_NOT_ALLOCATED_YET_ERROR:
1306 if (objfile_is_library)
1307 error (_("The inferior has not yet allocated storage for"
dda83cd7
SM
1308 " thread-local variables in\n"
1309 "the shared library `%s'\n"
1310 "for %s"),
a068643d
TT
1311 objfile_name (objfile),
1312 target_pid_to_str (ptid).c_str ());
9e35dae4
DJ
1313 else
1314 error (_("The inferior has not yet allocated storage for"
dda83cd7
SM
1315 " thread-local variables in\n"
1316 "the executable `%s'\n"
1317 "for %s"),
a068643d
TT
1318 objfile_name (objfile),
1319 target_pid_to_str (ptid).c_str ());
9e35dae4
DJ
1320 break;
1321 case TLS_GENERIC_ERROR:
1322 if (objfile_is_library)
1323 error (_("Cannot find thread-local storage for %s, "
dda83cd7 1324 "shared library %s:\n%s"),
a068643d 1325 target_pid_to_str (ptid).c_str (),
3d6e9d23 1326 objfile_name (objfile), ex.what ());
9e35dae4
DJ
1327 else
1328 error (_("Cannot find thread-local storage for %s, "
dda83cd7 1329 "executable file %s:\n%s"),
a068643d 1330 target_pid_to_str (ptid).c_str (),
3d6e9d23 1331 objfile_name (objfile), ex.what ());
9e35dae4
DJ
1332 break;
1333 default:
eedc3f4f 1334 throw;
9e35dae4
DJ
1335 break;
1336 }
1337 }
1338 }
9e35dae4
DJ
1339 else
1340 error (_("Cannot find thread-local variables on this target"));
1341
1342 return addr;
1343}
1344
6be7b56e 1345const char *
01cb8804 1346target_xfer_status_to_string (enum target_xfer_status status)
6be7b56e
PA
1347{
1348#define CASE(X) case X: return #X
01cb8804 1349 switch (status)
6be7b56e
PA
1350 {
1351 CASE(TARGET_XFER_E_IO);
bc113b4e 1352 CASE(TARGET_XFER_UNAVAILABLE);
6be7b56e
PA
1353 default:
1354 return "<unknown>";
1355 }
1356#undef CASE
1357};
1358
1359
25b5a04e 1360const std::vector<target_section> *
07b82ea5
PA
1361target_get_section_table (struct target_ops *target)
1362{
f6ac5f3d 1363 return target->get_section_table ();
07b82ea5
PA
1364}
1365
8db32d44 1366/* Find a section containing ADDR. */
07b82ea5 1367
19cf757a 1368const struct target_section *
8db32d44
AC
1369target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1370{
25b5a04e 1371 const std::vector<target_section> *table = target_get_section_table (target);
07b82ea5
PA
1372
1373 if (table == NULL)
1374 return NULL;
1375
19cf757a 1376 for (const target_section &secp : *table)
8db32d44 1377 {
bb2a6777
TT
1378 if (addr >= secp.addr && addr < secp.endaddr)
1379 return &secp;
8db32d44
AC
1380 }
1381 return NULL;
1382}
1383
336aa7b7
AB
1384/* See target.h. */
1385
25b5a04e 1386const std::vector<target_section> *
336aa7b7
AB
1387default_get_section_table ()
1388{
1389 return &current_program_space->target_sections ();
1390}
0fec99e8
PA
1391
1392/* Helper for the memory xfer routines. Checks the attributes of the
1393 memory region of MEMADDR against the read or write being attempted.
1394 If the access is permitted returns true, otherwise returns false.
1395 REGION_P is an optional output parameter. If not-NULL, it is
1396 filled with a pointer to the memory region of MEMADDR. REG_LEN
1397 returns LEN trimmed to the end of the region. This is how much the
1398 caller can continue requesting, if the access is permitted. A
1399 single xfer request must not straddle memory region boundaries. */
1400
1401static int
1402memory_xfer_check_region (gdb_byte *readbuf, const gdb_byte *writebuf,
1403 ULONGEST memaddr, ULONGEST len, ULONGEST *reg_len,
1404 struct mem_region **region_p)
1405{
1406 struct mem_region *region;
1407
1408 region = lookup_mem_region (memaddr);
1409
1410 if (region_p != NULL)
1411 *region_p = region;
1412
1413 switch (region->attrib.mode)
1414 {
1415 case MEM_RO:
1416 if (writebuf != NULL)
1417 return 0;
1418 break;
1419
1420 case MEM_WO:
1421 if (readbuf != NULL)
1422 return 0;
1423 break;
1424
1425 case MEM_FLASH:
1426 /* We only support writing to flash during "load" for now. */
1427 if (writebuf != NULL)
1428 error (_("Writing to flash memory forbidden in this context"));
1429 break;
1430
1431 case MEM_NONE:
1432 return 0;
1433 }
1434
1435 /* region->hi == 0 means there's no upper bound. */
1436 if (memaddr + len < region->hi || region->hi == 0)
1437 *reg_len = len;
1438 else
1439 *reg_len = region->hi - memaddr;
1440
1441 return 1;
1442}
1443
9f713294
YQ
1444/* Read memory from more than one valid target. A core file, for
1445 instance, could have some of memory but delegate other bits to
1446 the target below it. So, we must manually try all targets. */
1447
cc9f16aa 1448enum target_xfer_status
17fde6d0 1449raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf,
9b409511
YQ
1450 const gdb_byte *writebuf, ULONGEST memaddr, LONGEST len,
1451 ULONGEST *xfered_len)
9f713294 1452{
9b409511 1453 enum target_xfer_status res;
9f713294
YQ
1454
1455 do
1456 {
f6ac5f3d
PA
1457 res = ops->xfer_partial (TARGET_OBJECT_MEMORY, NULL,
1458 readbuf, writebuf, memaddr, len,
1459 xfered_len);
9b409511 1460 if (res == TARGET_XFER_OK)
9f713294
YQ
1461 break;
1462
633785ff 1463 /* Stop if the target reports that the memory is not available. */
bc113b4e 1464 if (res == TARGET_XFER_UNAVAILABLE)
633785ff
MM
1465 break;
1466
2735d421 1467 /* Don't continue past targets which have all the memory.
dda83cd7 1468 At one time, this code was necessary to read data from
2735d421
KB
1469 executables / shared libraries when data for the requested
1470 addresses weren't available in the core file. But now the
1471 core target handles this case itself. */
f6ac5f3d 1472 if (ops->has_all_memory ())
9f713294
YQ
1473 break;
1474
b6a8c27b 1475 ops = ops->beneath ();
9f713294
YQ
1476 }
1477 while (ops != NULL);
1478
0f26cec1
PA
1479 /* The cache works at the raw memory level. Make sure the cache
1480 gets updated with raw contents no matter what kind of memory
1481 object was originally being written. Note we do write-through
1482 first, so that if it fails, we don't write to the cache contents
1483 that never made it to the target. */
1484 if (writebuf != NULL
d7e15655 1485 && inferior_ptid != null_ptid
41336620 1486 && target_dcache_init_p (current_program_space->aspace)
0f26cec1
PA
1487 && (stack_cache_enabled_p () || code_cache_enabled_p ()))
1488 {
41336620 1489 DCACHE *dcache = target_dcache_get (current_program_space->aspace);
0f26cec1
PA
1490
1491 /* Note that writing to an area of memory which wasn't present
1492 in the cache doesn't cause it to be loaded in. */
1493 dcache_update (dcache, res, memaddr, writebuf, *xfered_len);
1494 }
1495
9f713294
YQ
1496 return res;
1497}
1498
7f79c47e
DE
1499/* Perform a partial memory transfer.
1500 For docs see target.h, to_xfer_partial. */
cf7a04e8 1501
9b409511 1502static enum target_xfer_status
f0ba3972 1503memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
17fde6d0 1504 gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST memaddr,
9b409511 1505 ULONGEST len, ULONGEST *xfered_len)
0779438d 1506{
9b409511 1507 enum target_xfer_status res;
0fec99e8 1508 ULONGEST reg_len;
cf7a04e8 1509 struct mem_region *region;
4e5d721f 1510 struct inferior *inf;
cf7a04e8 1511
07b82ea5
PA
1512 /* For accesses to unmapped overlay sections, read directly from
1513 files. Must do this first, as MEMADDR may need adjustment. */
1514 if (readbuf != NULL && overlay_debugging)
1515 {
1516 struct obj_section *section = find_pc_overlay (memaddr);
5d502164 1517
07b82ea5
PA
1518 if (pc_in_unmapped_range (memaddr, section))
1519 {
25b5a04e 1520 const std::vector<target_section> *table = target_get_section_table (ops);
07b82ea5 1521 const char *section_name = section->the_bfd_section->name;
5d502164 1522
07b82ea5 1523 memaddr = overlay_mapped_address (memaddr, section);
e56cb451
KB
1524
1525 auto match_cb = [=] (const struct target_section *s)
1526 {
1527 return (strcmp (section_name, s->the_bfd_section->name) == 0);
1528 };
1529
07b82ea5 1530 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 1531 memaddr, len, xfered_len,
bb2a6777 1532 *table, match_cb);
07b82ea5
PA
1533 }
1534 }
1535
1536 /* Try the executable files, if "trust-readonly-sections" is set. */
cf7a04e8
DJ
1537 if (readbuf != NULL && trust_readonly)
1538 {
19cf757a
AB
1539 const struct target_section *secp
1540 = target_section_by_addr (ops, memaddr);
cf7a04e8 1541 if (secp != NULL
fd361982 1542 && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
07b82ea5 1543 {
25b5a04e 1544 const std::vector<target_section> *table = target_get_section_table (ops);
07b82ea5 1545 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 1546 memaddr, len, xfered_len,
bb2a6777 1547 *table);
07b82ea5 1548 }
98646950
UW
1549 }
1550
cf7a04e8 1551 /* Try GDB's internal data cache. */
cf7a04e8 1552
0fec99e8
PA
1553 if (!memory_xfer_check_region (readbuf, writebuf, memaddr, len, &reg_len,
1554 &region))
1555 return TARGET_XFER_E_IO;
cf7a04e8 1556
d7e15655 1557 if (inferior_ptid != null_ptid)
00431a78 1558 inf = current_inferior ();
6c95b8df
PA
1559 else
1560 inf = NULL;
4e5d721f
DE
1561
1562 if (inf != NULL
0f26cec1 1563 && readbuf != NULL
2f4d8875
PA
1564 /* The dcache reads whole cache lines; that doesn't play well
1565 with reading from a trace buffer, because reading outside of
1566 the collected memory range fails. */
1567 && get_traceframe_number () == -1
4e5d721f 1568 && (region->attrib.cache
29453a14
YQ
1569 || (stack_cache_enabled_p () && object == TARGET_OBJECT_STACK_MEMORY)
1570 || (code_cache_enabled_p () && object == TARGET_OBJECT_CODE_MEMORY)))
cf7a04e8 1571 {
41336620
SM
1572 DCACHE *dcache
1573 = target_dcache_get_or_init (current_program_space->aspace);
2a2f9fe4 1574
0f26cec1
PA
1575 return dcache_read_memory_partial (ops, dcache, memaddr, readbuf,
1576 reg_len, xfered_len);
cf7a04e8
DJ
1577 }
1578
1579 /* If none of those methods found the memory we wanted, fall back
1580 to a target partial transfer. Normally a single call to
1581 to_xfer_partial is enough; if it doesn't recognize an object
1582 it will call the to_xfer_partial of the next target down.
1583 But for memory this won't do. Memory is the only target
9b409511
YQ
1584 object which can be read from more than one valid target.
1585 A core file, for instance, could have some of memory but
1586 delegate other bits to the target below it. So, we must
1587 manually try all targets. */
1588
1589 res = raw_memory_xfer_partial (ops, readbuf, writebuf, memaddr, reg_len,
1590 xfered_len);
cf7a04e8
DJ
1591
1592 /* If we still haven't got anything, return the last error. We
1593 give up. */
1594 return res;
0779438d
AC
1595}
1596
f0ba3972
PA
1597/* Perform a partial memory transfer. For docs see target.h,
1598 to_xfer_partial. */
1599
9b409511 1600static enum target_xfer_status
f0ba3972 1601memory_xfer_partial (struct target_ops *ops, enum target_object object,
9b409511
YQ
1602 gdb_byte *readbuf, const gdb_byte *writebuf,
1603 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
f0ba3972 1604{
9b409511 1605 enum target_xfer_status res;
f0ba3972
PA
1606
1607 /* Zero length requests are ok and require no work. */
1608 if (len == 0)
9b409511 1609 return TARGET_XFER_EOF;
f0ba3972 1610
99d9c3b9
SM
1611 memaddr = gdbarch_remove_non_address_bits (current_inferior ()->arch (),
1612 memaddr);
a738ea1d 1613
f0ba3972
PA
1614 /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1615 breakpoint insns, thus hiding out from higher layers whether
1616 there are software breakpoints inserted in the code stream. */
1617 if (readbuf != NULL)
1618 {
9b409511
YQ
1619 res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len,
1620 xfered_len);
f0ba3972 1621
9b409511 1622 if (res == TARGET_XFER_OK && !show_memory_breakpoints)
c63528fc 1623 breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, *xfered_len);
f0ba3972
PA
1624 }
1625 else
1626 {
67c059c2
AB
1627 /* A large write request is likely to be partially satisfied
1628 by memory_xfer_partial_1. We will continually malloc
1629 and free a copy of the entire write request for breakpoint
1630 shadow handling even though we only end up writing a small
09c98b44
DB
1631 subset of it. Cap writes to a limit specified by the target
1632 to mitigate this. */
f6ac5f3d 1633 len = std::min (ops->get_memory_xfer_limit (), len);
67c059c2 1634
26fcd5d7
TT
1635 gdb::byte_vector buf (writebuf, writebuf + len);
1636 breakpoint_xfer_memory (NULL, buf.data (), writebuf, memaddr, len);
1637 res = memory_xfer_partial_1 (ops, object, NULL, buf.data (), memaddr, len,
9b409511 1638 xfered_len);
f0ba3972
PA
1639 }
1640
1641 return res;
1642}
1643
cb85b21b
TT
1644scoped_restore_tmpl<int>
1645make_scoped_restore_show_memory_breakpoints (int show)
8defab1a 1646{
cb85b21b 1647 return make_scoped_restore (&show_memory_breakpoints, show);
8defab1a
DJ
1648}
1649
7f79c47e
DE
1650/* For docs see target.h, to_xfer_partial. */
1651
9b409511 1652enum target_xfer_status
27394598
AC
1653target_xfer_partial (struct target_ops *ops,
1654 enum target_object object, const char *annex,
4ac248ca 1655 gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511
YQ
1656 ULONGEST offset, ULONGEST len,
1657 ULONGEST *xfered_len)
27394598 1658{
9b409511 1659 enum target_xfer_status retval;
27394598 1660
ce6d0892
YQ
1661 /* Transfer is done when LEN is zero. */
1662 if (len == 0)
9b409511 1663 return TARGET_XFER_EOF;
ce6d0892 1664
d914c394
SS
1665 if (writebuf && !may_write_memory)
1666 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1667 core_addr_to_string_nz (offset), plongest (len));
1668
9b409511
YQ
1669 *xfered_len = 0;
1670
cf7a04e8
DJ
1671 /* If this is a memory transfer, let the memory-specific code
1672 have a look at it instead. Memory transfers are more
1673 complicated. */
29453a14
YQ
1674 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY
1675 || object == TARGET_OBJECT_CODE_MEMORY)
4e5d721f 1676 retval = memory_xfer_partial (ops, object, readbuf,
9b409511 1677 writebuf, offset, len, xfered_len);
9f713294 1678 else if (object == TARGET_OBJECT_RAW_MEMORY)
cf7a04e8 1679 {
0fec99e8
PA
1680 /* Skip/avoid accessing the target if the memory region
1681 attributes block the access. Check this here instead of in
1682 raw_memory_xfer_partial as otherwise we'd end up checking
1683 this twice in the case of the memory_xfer_partial path is
1684 taken; once before checking the dcache, and another in the
1685 tail call to raw_memory_xfer_partial. */
1686 if (!memory_xfer_check_region (readbuf, writebuf, offset, len, &len,
1687 NULL))
1688 return TARGET_XFER_E_IO;
1689
9f713294 1690 /* Request the normal memory object from other layers. */
9b409511
YQ
1691 retval = raw_memory_xfer_partial (ops, readbuf, writebuf, offset, len,
1692 xfered_len);
cf7a04e8 1693 }
9f713294 1694 else
f6ac5f3d
PA
1695 retval = ops->xfer_partial (object, annex, readbuf,
1696 writebuf, offset, len, xfered_len);
cf7a04e8 1697
27394598
AC
1698 if (targetdebug)
1699 {
1700 const unsigned char *myaddr = NULL;
cb5dfff8
SM
1701 std::string s
1702 = string_printf ("%s:target_xfer_partial "
1703 "(%d, %s, %s, %s, %s, %s) = %d, %s",
1704 ops->shortname (), (int) object,
1705 (annex ? annex : "(null)"),
1706 host_address_to_string (readbuf),
1707 host_address_to_string (writebuf),
1708 core_addr_to_string_nz (offset), pulongest (len),
1709 retval, pulongest (*xfered_len));
27394598
AC
1710
1711 if (readbuf)
1712 myaddr = readbuf;
1713 if (writebuf)
1714 myaddr = writebuf;
9b409511 1715 if (retval == TARGET_XFER_OK && myaddr != NULL)
27394598
AC
1716 {
1717 int i;
2bc416ba 1718
cb5dfff8 1719 string_appendf (s, ", bytes =");
9b409511 1720 for (i = 0; i < *xfered_len; i++)
27394598 1721 {
53b71562 1722 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
27394598
AC
1723 {
1724 if (targetdebug < 2 && i > 0)
1725 {
cb5dfff8 1726 string_appendf (s, " ...");
27394598
AC
1727 break;
1728 }
cb5dfff8
SM
1729
1730 target_debug_printf_nofunc ("%s", s.c_str ());
1731 s.clear();
27394598 1732 }
2bc416ba 1733
cb5dfff8 1734 string_appendf (s, " %02x", myaddr[i] & 0xff);
27394598
AC
1735 }
1736 }
2bc416ba 1737
cb5dfff8 1738 target_debug_printf_nofunc ("%s", s.c_str ());
27394598 1739 }
9b409511
YQ
1740
1741 /* Check implementations of to_xfer_partial update *XFERED_LEN
1742 properly. Do assertion after printing debug messages, so that we
1743 can find more clues on assertion failure from debugging messages. */
bc113b4e 1744 if (retval == TARGET_XFER_OK || retval == TARGET_XFER_UNAVAILABLE)
9b409511
YQ
1745 gdb_assert (*xfered_len > 0);
1746
27394598
AC
1747 return retval;
1748}
1749
578d3588
PA
1750/* Read LEN bytes of target memory at address MEMADDR, placing the
1751 results in GDB's memory at MYADDR. Returns either 0 for success or
d09f2c3f 1752 -1 if any error occurs.
c906108c
SS
1753
1754 If an error occurs, no guarantee is made about the contents of the data at
1755 MYADDR. In particular, the caller should not depend upon partial reads
1756 filling the buffer with good data. There is no way for the caller to know
1757 how much good data might have been transfered anyway. Callers that can
cf7a04e8 1758 deal with partial reads should call target_read (which will retry until
c378eb4e 1759 it makes no progress, and then return how much was transferred). */
c906108c
SS
1760
1761int
1b162304 1762target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
c906108c 1763{
328d42d8
SM
1764 if (target_read (current_inferior ()->top_target (),
1765 TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1766 myaddr, memaddr, len) == len)
1767 return 0;
0779438d 1768 else
d09f2c3f 1769 return -1;
c906108c
SS
1770}
1771
721ec300
GB
1772/* See target/target.h. */
1773
1774int
1775target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
1776{
1777 gdb_byte buf[4];
1778 int r;
1779
1780 r = target_read_memory (memaddr, buf, sizeof buf);
1781 if (r != 0)
1782 return r;
99d9c3b9
SM
1783 *result = extract_unsigned_integer
1784 (buf, sizeof buf,
1785 gdbarch_byte_order (current_inferior ()->arch ()));
721ec300
GB
1786 return 0;
1787}
1788
aee4bf85
PA
1789/* Like target_read_memory, but specify explicitly that this is a read
1790 from the target's raw memory. That is, this read bypasses the
1791 dcache, breakpoint shadowing, etc. */
1792
1793int
1794target_read_raw_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
1795{
328d42d8
SM
1796 if (target_read (current_inferior ()->top_target (),
1797 TARGET_OBJECT_RAW_MEMORY, NULL,
aee4bf85
PA
1798 myaddr, memaddr, len) == len)
1799 return 0;
1800 else
d09f2c3f 1801 return -1;
aee4bf85
PA
1802}
1803
4e5d721f
DE
1804/* Like target_read_memory, but specify explicitly that this is a read from
1805 the target's stack. This may trigger different cache behavior. */
1806
1807int
45aa4659 1808target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
4e5d721f 1809{
328d42d8
SM
1810 if (target_read (current_inferior ()->top_target (),
1811 TARGET_OBJECT_STACK_MEMORY, NULL,
4e5d721f
DE
1812 myaddr, memaddr, len) == len)
1813 return 0;
1814 else
d09f2c3f 1815 return -1;
4e5d721f
DE
1816}
1817
29453a14
YQ
1818/* Like target_read_memory, but specify explicitly that this is a read from
1819 the target's code. This may trigger different cache behavior. */
1820
1821int
1822target_read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
1823{
328d42d8
SM
1824 if (target_read (current_inferior ()->top_target (),
1825 TARGET_OBJECT_CODE_MEMORY, NULL,
29453a14
YQ
1826 myaddr, memaddr, len) == len)
1827 return 0;
1828 else
d09f2c3f 1829 return -1;
29453a14
YQ
1830}
1831
7f79c47e 1832/* Write LEN bytes from MYADDR to target memory at address MEMADDR.
d09f2c3f
PA
1833 Returns either 0 for success or -1 if any error occurs. If an
1834 error occurs, no guarantee is made about how much data got written.
1835 Callers that can deal with partial writes should call
1836 target_write. */
7f79c47e 1837
c906108c 1838int
45aa4659 1839target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
c906108c 1840{
328d42d8
SM
1841 if (target_write (current_inferior ()->top_target (),
1842 TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1843 myaddr, memaddr, len) == len)
1844 return 0;
0779438d 1845 else
d09f2c3f 1846 return -1;
c906108c 1847}
c5aa993b 1848
f0ba3972 1849/* Write LEN bytes from MYADDR to target raw memory at address
d09f2c3f
PA
1850 MEMADDR. Returns either 0 for success or -1 if any error occurs.
1851 If an error occurs, no guarantee is made about how much data got
1852 written. Callers that can deal with partial writes should call
1853 target_write. */
f0ba3972
PA
1854
1855int
45aa4659 1856target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
f0ba3972 1857{
328d42d8
SM
1858 if (target_write (current_inferior ()->top_target (),
1859 TARGET_OBJECT_RAW_MEMORY, NULL,
f0ba3972
PA
1860 myaddr, memaddr, len) == len)
1861 return 0;
1862 else
d09f2c3f 1863 return -1;
f0ba3972
PA
1864}
1865
fd79ecee
DJ
1866/* Fetch the target's memory map. */
1867
a664f67e 1868std::vector<mem_region>
fd79ecee
DJ
1869target_memory_map (void)
1870{
328d42d8
SM
1871 target_ops *target = current_inferior ()->top_target ();
1872 std::vector<mem_region> result = target->memory_map ();
a664f67e
SM
1873 if (result.empty ())
1874 return result;
fd79ecee 1875
a664f67e 1876 std::sort (result.begin (), result.end ());
fd79ecee
DJ
1877
1878 /* Check that regions do not overlap. Simultaneously assign
1879 a numbering for the "mem" commands to use to refer to
1880 each region. */
a664f67e
SM
1881 mem_region *last_one = NULL;
1882 for (size_t ix = 0; ix < result.size (); ix++)
fd79ecee 1883 {
a664f67e 1884 mem_region *this_one = &result[ix];
fd79ecee
DJ
1885 this_one->number = ix;
1886
a664f67e 1887 if (last_one != NULL && last_one->hi > this_one->lo)
fd79ecee
DJ
1888 {
1889 warning (_("Overlapping regions in memory map: ignoring"));
a664f67e 1890 return std::vector<mem_region> ();
fd79ecee 1891 }
a664f67e 1892
fd79ecee
DJ
1893 last_one = this_one;
1894 }
1895
1896 return result;
1897}
1898
a76d924d
DJ
1899void
1900target_flash_erase (ULONGEST address, LONGEST length)
1901{
328d42d8 1902 current_inferior ()->top_target ()->flash_erase (address, length);
a76d924d
DJ
1903}
1904
1905void
1906target_flash_done (void)
1907{
328d42d8 1908 current_inferior ()->top_target ()->flash_done ();
a76d924d
DJ
1909}
1910
920d2a44
AC
1911static void
1912show_trust_readonly (struct ui_file *file, int from_tty,
1913 struct cmd_list_element *c, const char *value)
1914{
6cb06a8c
TT
1915 gdb_printf (file,
1916 _("Mode for reading from readonly sections is %s.\n"),
1917 value);
920d2a44 1918}
3a11626d 1919
7f79c47e 1920/* Target vector read/write partial wrapper functions. */
0088c768 1921
9b409511 1922static enum target_xfer_status
1e3ff5ad
AC
1923target_read_partial (struct target_ops *ops,
1924 enum target_object object,
1b0ba102 1925 const char *annex, gdb_byte *buf,
9b409511
YQ
1926 ULONGEST offset, ULONGEST len,
1927 ULONGEST *xfered_len)
1e3ff5ad 1928{
9b409511
YQ
1929 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len,
1930 xfered_len);
1e3ff5ad
AC
1931}
1932
8a55ffb0 1933static enum target_xfer_status
1e3ff5ad
AC
1934target_write_partial (struct target_ops *ops,
1935 enum target_object object,
1b0ba102 1936 const char *annex, const gdb_byte *buf,
9b409511 1937 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
1e3ff5ad 1938{
9b409511
YQ
1939 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len,
1940 xfered_len);
1e3ff5ad
AC
1941}
1942
1943/* Wrappers to perform the full transfer. */
7f79c47e
DE
1944
1945/* For docs on target_read see target.h. */
1946
1e3ff5ad
AC
1947LONGEST
1948target_read (struct target_ops *ops,
1949 enum target_object object,
1b0ba102 1950 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1951 ULONGEST offset, LONGEST len)
1952{
279a6fed 1953 LONGEST xfered_total = 0;
d309493c
SM
1954 int unit_size = 1;
1955
1956 /* If we are reading from a memory object, find the length of an addressable
1957 unit for that architecture. */
1958 if (object == TARGET_OBJECT_MEMORY
1959 || object == TARGET_OBJECT_STACK_MEMORY
1960 || object == TARGET_OBJECT_CODE_MEMORY
1961 || object == TARGET_OBJECT_RAW_MEMORY)
99d9c3b9
SM
1962 unit_size = gdbarch_addressable_memory_unit_size
1963 (current_inferior ()->arch ());
5d502164 1964
279a6fed 1965 while (xfered_total < len)
1e3ff5ad 1966 {
279a6fed 1967 ULONGEST xfered_partial;
9b409511
YQ
1968 enum target_xfer_status status;
1969
1970 status = target_read_partial (ops, object, annex,
d309493c 1971 buf + xfered_total * unit_size,
279a6fed
SM
1972 offset + xfered_total, len - xfered_total,
1973 &xfered_partial);
5d502164 1974
1e3ff5ad 1975 /* Call an observer, notifying them of the xfer progress? */
9b409511 1976 if (status == TARGET_XFER_EOF)
279a6fed 1977 return xfered_total;
9b409511
YQ
1978 else if (status == TARGET_XFER_OK)
1979 {
279a6fed 1980 xfered_total += xfered_partial;
9b409511
YQ
1981 QUIT;
1982 }
1983 else
279a6fed 1984 return TARGET_XFER_E_IO;
9b409511 1985
1e3ff5ad
AC
1986 }
1987 return len;
1988}
1989
f1a507a1
JB
1990/* Assuming that the entire [begin, end) range of memory cannot be
1991 read, try to read whatever subrange is possible to read.
1992
1993 The function returns, in RESULT, either zero or one memory block.
1994 If there's a readable subrange at the beginning, it is completely
1995 read and returned. Any further readable subrange will not be read.
1996 Otherwise, if there's a readable subrange at the end, it will be
1997 completely read and returned. Any readable subranges before it
1998 (obviously, not starting at the beginning), will be ignored. In
1999 other cases -- either no readable subrange, or readable subrange(s)
2000 that is neither at the beginning, or end, nothing is returned.
2001
2002 The purpose of this function is to handle a read across a boundary
2003 of accessible memory in a case when memory map is not available.
2004 The above restrictions are fine for this case, but will give
2005 incorrect results if the memory is 'patchy'. However, supporting
2006 'patchy' memory would require trying to read every single byte,
2007 and it seems unacceptable solution. Explicit memory map is
2008 recommended for this case -- and target_read_memory_robust will
2009 take care of reading multiple ranges then. */
8dedea02
VP
2010
2011static void
3e43a32a 2012read_whatever_is_readable (struct target_ops *ops,
279a6fed 2013 const ULONGEST begin, const ULONGEST end,
d309493c 2014 int unit_size,
386c8614 2015 std::vector<memory_read_result> *result)
d5086790 2016{
8dedea02
VP
2017 ULONGEST current_begin = begin;
2018 ULONGEST current_end = end;
2019 int forward;
9b409511 2020 ULONGEST xfered_len;
8dedea02
VP
2021
2022 /* If we previously failed to read 1 byte, nothing can be done here. */
2023 if (end - begin <= 1)
386c8614
TT
2024 return;
2025
2026 gdb::unique_xmalloc_ptr<gdb_byte> buf ((gdb_byte *) xmalloc (end - begin));
8dedea02
VP
2027
2028 /* Check that either first or the last byte is readable, and give up
c378eb4e 2029 if not. This heuristic is meant to permit reading accessible memory
8dedea02
VP
2030 at the boundary of accessible region. */
2031 if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
386c8614 2032 buf.get (), begin, 1, &xfered_len) == TARGET_XFER_OK)
8dedea02
VP
2033 {
2034 forward = 1;
2035 ++current_begin;
2036 }
2037 else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
386c8614 2038 buf.get () + (end - begin) - 1, end - 1, 1,
9b409511 2039 &xfered_len) == TARGET_XFER_OK)
8dedea02
VP
2040 {
2041 forward = 0;
2042 --current_end;
2043 }
2044 else
386c8614 2045 return;
8dedea02
VP
2046
2047 /* Loop invariant is that the [current_begin, current_end) was previously
2048 found to be not readable as a whole.
2049
2050 Note loop condition -- if the range has 1 byte, we can't divide the range
2051 so there's no point trying further. */
2052 while (current_end - current_begin > 1)
2053 {
2054 ULONGEST first_half_begin, first_half_end;
2055 ULONGEST second_half_begin, second_half_end;
2056 LONGEST xfer;
279a6fed 2057 ULONGEST middle = current_begin + (current_end - current_begin) / 2;
f1a507a1 2058
8dedea02
VP
2059 if (forward)
2060 {
2061 first_half_begin = current_begin;
2062 first_half_end = middle;
2063 second_half_begin = middle;
2064 second_half_end = current_end;
2065 }
2066 else
2067 {
2068 first_half_begin = middle;
2069 first_half_end = current_end;
2070 second_half_begin = current_begin;
2071 second_half_end = middle;
2072 }
2073
2074 xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
386c8614 2075 buf.get () + (first_half_begin - begin) * unit_size,
8dedea02
VP
2076 first_half_begin,
2077 first_half_end - first_half_begin);
2078
2079 if (xfer == first_half_end - first_half_begin)
2080 {
c378eb4e 2081 /* This half reads up fine. So, the error must be in the
3e43a32a 2082 other half. */
8dedea02
VP
2083 current_begin = second_half_begin;
2084 current_end = second_half_end;
2085 }
2086 else
2087 {
c378eb4e 2088 /* This half is not readable. Because we've tried one byte, we
279a6fed 2089 know some part of this half if actually readable. Go to the next
8dedea02
VP
2090 iteration to divide again and try to read.
2091
2092 We don't handle the other half, because this function only tries
2093 to read a single readable subrange. */
2094 current_begin = first_half_begin;
2095 current_end = first_half_end;
2096 }
2097 }
2098
2099 if (forward)
2100 {
2101 /* The [begin, current_begin) range has been read. */
386c8614 2102 result->emplace_back (begin, current_end, std::move (buf));
8dedea02
VP
2103 }
2104 else
2105 {
2106 /* The [current_end, end) range has been read. */
279a6fed 2107 LONGEST region_len = end - current_end;
f1a507a1 2108
386c8614
TT
2109 gdb::unique_xmalloc_ptr<gdb_byte> data
2110 ((gdb_byte *) xmalloc (region_len * unit_size));
2111 memcpy (data.get (), buf.get () + (current_end - begin) * unit_size,
d309493c 2112 region_len * unit_size);
386c8614 2113 result->emplace_back (current_end, end, std::move (data));
8dedea02 2114 }
8dedea02
VP
2115}
2116
386c8614 2117std::vector<memory_read_result>
279a6fed
SM
2118read_memory_robust (struct target_ops *ops,
2119 const ULONGEST offset, const LONGEST len)
8dedea02 2120{
386c8614 2121 std::vector<memory_read_result> result;
99d9c3b9
SM
2122 int unit_size
2123 = gdbarch_addressable_memory_unit_size (current_inferior ()->arch ());
8dedea02 2124
279a6fed
SM
2125 LONGEST xfered_total = 0;
2126 while (xfered_total < len)
d5086790 2127 {
279a6fed
SM
2128 struct mem_region *region = lookup_mem_region (offset + xfered_total);
2129 LONGEST region_len;
5d502164 2130
8dedea02
VP
2131 /* If there is no explicit region, a fake one should be created. */
2132 gdb_assert (region);
2133
2134 if (region->hi == 0)
279a6fed 2135 region_len = len - xfered_total;
8dedea02 2136 else
279a6fed 2137 region_len = region->hi - offset;
8dedea02
VP
2138
2139 if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
d5086790 2140 {
c378eb4e 2141 /* Cannot read this region. Note that we can end up here only
8dedea02
VP
2142 if the region is explicitly marked inaccessible, or
2143 'inaccessible-by-default' is in effect. */
279a6fed 2144 xfered_total += region_len;
8dedea02
VP
2145 }
2146 else
2147 {
325fac50 2148 LONGEST to_read = std::min (len - xfered_total, region_len);
386c8614
TT
2149 gdb::unique_xmalloc_ptr<gdb_byte> buffer
2150 ((gdb_byte *) xmalloc (to_read * unit_size));
8dedea02 2151
279a6fed 2152 LONGEST xfered_partial =
386c8614 2153 target_read (ops, TARGET_OBJECT_MEMORY, NULL, buffer.get (),
279a6fed 2154 offset + xfered_total, to_read);
8dedea02 2155 /* Call an observer, notifying them of the xfer progress? */
279a6fed 2156 if (xfered_partial <= 0)
d5086790 2157 {
c378eb4e 2158 /* Got an error reading full chunk. See if maybe we can read
8dedea02 2159 some subrange. */
e084c964
DB
2160 read_whatever_is_readable (ops, offset + xfered_total,
2161 offset + xfered_total + to_read,
2162 unit_size, &result);
279a6fed 2163 xfered_total += to_read;
d5086790 2164 }
8dedea02
VP
2165 else
2166 {
386c8614
TT
2167 result.emplace_back (offset + xfered_total,
2168 offset + xfered_total + xfered_partial,
2169 std::move (buffer));
279a6fed 2170 xfered_total += xfered_partial;
8dedea02
VP
2171 }
2172 QUIT;
d5086790 2173 }
d5086790 2174 }
9d78f827 2175
8dedea02 2176 return result;
d5086790
VP
2177}
2178
8dedea02 2179
cf7a04e8
DJ
2180/* An alternative to target_write with progress callbacks. */
2181
1e3ff5ad 2182LONGEST
cf7a04e8
DJ
2183target_write_with_progress (struct target_ops *ops,
2184 enum target_object object,
2185 const char *annex, const gdb_byte *buf,
2186 ULONGEST offset, LONGEST len,
2187 void (*progress) (ULONGEST, void *), void *baton)
1e3ff5ad 2188{
279a6fed 2189 LONGEST xfered_total = 0;
d309493c
SM
2190 int unit_size = 1;
2191
2192 /* If we are writing to a memory object, find the length of an addressable
2193 unit for that architecture. */
2194 if (object == TARGET_OBJECT_MEMORY
2195 || object == TARGET_OBJECT_STACK_MEMORY
2196 || object == TARGET_OBJECT_CODE_MEMORY
2197 || object == TARGET_OBJECT_RAW_MEMORY)
99d9c3b9
SM
2198 unit_size = gdbarch_addressable_memory_unit_size
2199 (current_inferior ()->arch ());
a76d924d
DJ
2200
2201 /* Give the progress callback a chance to set up. */
2202 if (progress)
2203 (*progress) (0, baton);
2204
279a6fed 2205 while (xfered_total < len)
1e3ff5ad 2206 {
279a6fed 2207 ULONGEST xfered_partial;
9b409511
YQ
2208 enum target_xfer_status status;
2209
2210 status = target_write_partial (ops, object, annex,
d309493c 2211 buf + xfered_total * unit_size,
279a6fed
SM
2212 offset + xfered_total, len - xfered_total,
2213 &xfered_partial);
cf7a04e8 2214
5c328c05 2215 if (status != TARGET_XFER_OK)
279a6fed 2216 return status == TARGET_XFER_EOF ? xfered_total : TARGET_XFER_E_IO;
cf7a04e8
DJ
2217
2218 if (progress)
279a6fed 2219 (*progress) (xfered_partial, baton);
cf7a04e8 2220
279a6fed 2221 xfered_total += xfered_partial;
1e3ff5ad
AC
2222 QUIT;
2223 }
2224 return len;
2225}
2226
7f79c47e
DE
2227/* For docs on target_write see target.h. */
2228
cf7a04e8
DJ
2229LONGEST
2230target_write (struct target_ops *ops,
2231 enum target_object object,
2232 const char *annex, const gdb_byte *buf,
2233 ULONGEST offset, LONGEST len)
2234{
2235 return target_write_with_progress (ops, object, annex, buf, offset, len,
2236 NULL, NULL);
2237}
2238
9018be22
SM
2239/* Help for target_read_alloc and target_read_stralloc. See their comments
2240 for details. */
13547ab6 2241
9018be22 2242template <typename T>
6b09f134 2243std::optional<gdb::def_vector<T>>
159f81f3 2244target_read_alloc_1 (struct target_ops *ops, enum target_object object,
9018be22 2245 const char *annex)
13547ab6 2246{
9018be22
SM
2247 gdb::def_vector<T> buf;
2248 size_t buf_pos = 0;
2249 const int chunk = 4096;
13547ab6
DJ
2250
2251 /* This function does not have a length parameter; it reads the
2252 entire OBJECT). Also, it doesn't support objects fetched partly
2253 from one target and partly from another (in a different stratum,
2254 e.g. a core file and an executable). Both reasons make it
2255 unsuitable for reading memory. */
2256 gdb_assert (object != TARGET_OBJECT_MEMORY);
2257
2258 /* Start by reading up to 4K at a time. The target will throttle
2259 this number down if necessary. */
13547ab6
DJ
2260 while (1)
2261 {
9b409511
YQ
2262 ULONGEST xfered_len;
2263 enum target_xfer_status status;
2264
9018be22
SM
2265 buf.resize (buf_pos + chunk);
2266
2267 status = target_read_partial (ops, object, annex,
2268 (gdb_byte *) &buf[buf_pos],
2269 buf_pos, chunk,
9b409511
YQ
2270 &xfered_len);
2271
2272 if (status == TARGET_XFER_EOF)
13547ab6
DJ
2273 {
2274 /* Read all there was. */
9018be22
SM
2275 buf.resize (buf_pos);
2276 return buf;
13547ab6 2277 }
9b409511
YQ
2278 else if (status != TARGET_XFER_OK)
2279 {
2280 /* An error occurred. */
9018be22 2281 return {};
9b409511 2282 }
13547ab6 2283
9b409511 2284 buf_pos += xfered_len;
13547ab6 2285
13547ab6
DJ
2286 QUIT;
2287 }
2288}
2289
9018be22 2290/* See target.h */
159f81f3 2291
6b09f134 2292std::optional<gdb::byte_vector>
159f81f3 2293target_read_alloc (struct target_ops *ops, enum target_object object,
9018be22 2294 const char *annex)
159f81f3 2295{
9018be22 2296 return target_read_alloc_1<gdb_byte> (ops, object, annex);
159f81f3
DJ
2297}
2298
b7b030ad 2299/* See target.h. */
159f81f3 2300
6b09f134 2301std::optional<gdb::char_vector>
159f81f3
DJ
2302target_read_stralloc (struct target_ops *ops, enum target_object object,
2303 const char *annex)
2304{
6b09f134 2305 std::optional<gdb::char_vector> buf
9018be22 2306 = target_read_alloc_1<char> (ops, object, annex);
159f81f3 2307
9018be22
SM
2308 if (!buf)
2309 return {};
159f81f3 2310
d00a27c5 2311 if (buf->empty () || buf->back () != '\0')
9018be22 2312 buf->push_back ('\0');
7313baad
UW
2313
2314 /* Check for embedded NUL bytes; but allow trailing NULs. */
9018be22
SM
2315 for (auto it = std::find (buf->begin (), buf->end (), '\0');
2316 it != buf->end (); it++)
2317 if (*it != '\0')
7313baad
UW
2318 {
2319 warning (_("target object %d, annex %s, "
2320 "contained unexpected null characters"),
2321 (int) object, annex ? annex : "(none)");
2322 break;
2323 }
159f81f3 2324
9018be22 2325 return buf;
159f81f3
DJ
2326}
2327
b6591e8b
AC
2328/* Memory transfer methods. */
2329
2330void
1b0ba102 2331get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
b6591e8b
AC
2332 LONGEST len)
2333{
07b82ea5
PA
2334 /* This method is used to read from an alternate, non-current
2335 target. This read must bypass the overlay support (as symbols
2336 don't match this target), and GDB's internal cache (wrong cache
2337 for this target). */
2338 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
b6591e8b 2339 != len)
578d3588 2340 memory_error (TARGET_XFER_E_IO, addr);
b6591e8b
AC
2341}
2342
2343ULONGEST
5d502164
MS
2344get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2345 int len, enum bfd_endian byte_order)
b6591e8b 2346{
f6519ebc 2347 gdb_byte buf[sizeof (ULONGEST)];
b6591e8b
AC
2348
2349 gdb_assert (len <= sizeof (buf));
2350 get_target_memory (ops, addr, buf, len);
e17a4113 2351 return extract_unsigned_integer (buf, len, byte_order);
b6591e8b
AC
2352}
2353
3db08215
MM
2354/* See target.h. */
2355
d914c394
SS
2356int
2357target_insert_breakpoint (struct gdbarch *gdbarch,
2358 struct bp_target_info *bp_tgt)
2359{
2360 if (!may_insert_breakpoints)
2361 {
2362 warning (_("May not insert breakpoints"));
2363 return 1;
2364 }
2365
328d42d8
SM
2366 target_ops *target = current_inferior ()->top_target ();
2367
2368 return target->insert_breakpoint (gdbarch, bp_tgt);
d914c394
SS
2369}
2370
3db08215
MM
2371/* See target.h. */
2372
d914c394 2373int
6b84065d 2374target_remove_breakpoint (struct gdbarch *gdbarch,
73971819
PA
2375 struct bp_target_info *bp_tgt,
2376 enum remove_bp_reason reason)
d914c394
SS
2377{
2378 /* This is kind of a weird case to handle, but the permission might
2379 have been changed after breakpoints were inserted - in which case
2380 we should just take the user literally and assume that any
2381 breakpoints should be left in place. */
2382 if (!may_insert_breakpoints)
2383 {
2384 warning (_("May not remove breakpoints"));
2385 return 1;
2386 }
2387
328d42d8
SM
2388 target_ops *target = current_inferior ()->top_target ();
2389
2390 return target->remove_breakpoint (gdbarch, bp_tgt, reason);
d914c394
SS
2391}
2392
c906108c 2393static void
1d12d88f 2394info_target_command (const char *args, int from_tty)
c906108c 2395{
c906108c 2396 int has_all_mem = 0;
c5aa993b 2397
a42d7dd8
TT
2398 if (current_program_space->symfile_object_file != NULL)
2399 {
2400 objfile *objf = current_program_space->symfile_object_file;
5260dcf0
AB
2401 gdb_printf (_("Symbols from \"%ps\".\n"),
2402 styled_string (file_name_style.style (),
2403 objfile_name (objf)));
a42d7dd8 2404 }
c906108c 2405
328d42d8
SM
2406 for (target_ops *t = current_inferior ()->top_target ();
2407 t != NULL;
2408 t = t->beneath ())
c906108c 2409 {
f6ac5f3d 2410 if (!t->has_memory ())
c906108c
SS
2411 continue;
2412
66b4deae 2413 if ((int) (t->stratum ()) <= (int) dummy_stratum)
c906108c
SS
2414 continue;
2415 if (has_all_mem)
6cb06a8c
TT
2416 gdb_printf (_("\tWhile running this, "
2417 "GDB does not access memory from...\n"));
2418 gdb_printf ("%s:\n", t->longname ());
f6ac5f3d
PA
2419 t->files_info ();
2420 has_all_mem = t->has_all_memory ();
c906108c
SS
2421 }
2422}
2423
fd79ecee
DJ
2424/* This function is called before any new inferior is created, e.g.
2425 by running a program, attaching, or connecting to a target.
2426 It cleans up any state from previous invocations which might
2427 change between runs. This is a subset of what target_preopen
2428 resets (things which might change between targets). */
2429
2430void
2431target_pre_inferior (int from_tty)
2432{
c378eb4e 2433 /* Clear out solib state. Otherwise the solib state of the previous
b9db4ced 2434 inferior might have survived and is entirely wrong for the new
c378eb4e 2435 target. This has been observed on GNU/Linux using glibc 2.3. How
b9db4ced
UW
2436 to reproduce:
2437
2438 bash$ ./foo&
2439 [1] 4711
2440 bash$ ./foo&
2441 [1] 4712
2442 bash$ gdb ./foo
2443 [...]
2444 (gdb) attach 4711
2445 (gdb) detach
2446 (gdb) attach 4712
2447 Cannot access memory at address 0xdeadbeef
2448 */
b9db4ced 2449
50c71eaf
PA
2450 /* In some OSs, the shared library list is the same/global/shared
2451 across inferiors. If code is shared between processes, so are
2452 memory regions and features. */
99d9c3b9 2453 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
50c71eaf
PA
2454 {
2455 no_shared_libraries (NULL, from_tty);
2456
2457 invalidate_target_mem_regions ();
424163ea 2458
50c71eaf
PA
2459 target_clear_description ();
2460 }
8ffcbaaf 2461
e9756d52
PP
2462 /* attach_flag may be set if the previous process associated with
2463 the inferior was attached to. */
30220b46 2464 current_inferior ()->attach_flag = false;
e9756d52 2465
5d5658a1
PA
2466 current_inferior ()->highest_thread_num = 0;
2467
a81871f7
PA
2468 update_previous_thread ();
2469
8ffcbaaf 2470 agent_capability_invalidate ();
fd79ecee
DJ
2471}
2472
c906108c
SS
2473/* This is to be called by the open routine before it does
2474 anything. */
2475
2476void
fba45db2 2477target_preopen (int from_tty)
c906108c 2478{
c5aa993b 2479 dont_repeat ();
c906108c 2480
5b6d1e4f 2481 if (current_inferior ()->pid != 0)
c5aa993b 2482 {
adf40b2e 2483 if (!from_tty
55f6301a 2484 || !target_has_execution ()
b8fa0bfa 2485 || query (_("A program is being debugged already. Kill it? ")))
5b6d1e4f
PA
2486 {
2487 /* Core inferiors actually should be detached, not
2488 killed. */
55f6301a 2489 if (target_has_execution ())
5b6d1e4f
PA
2490 target_kill ();
2491 else
2492 target_detach (current_inferior (), 0);
2493 }
c906108c 2494 else
8a3fe4f8 2495 error (_("Program not killed."));
c906108c
SS
2496 }
2497
a81871f7
PA
2498 /* Release reference to old previous thread. */
2499 update_previous_thread ();
2500
c906108c
SS
2501 /* Calling target_kill may remove the target from the stack. But if
2502 it doesn't (which seems like a win for UDI), remove it now. */
87ab71f0
PA
2503 /* Leave the exec target, though. The user may be switching from a
2504 live process to a core of the same program. */
c8181f70 2505 current_inferior ()->pop_all_targets_above (file_stratum);
fd79ecee
DJ
2506
2507 target_pre_inferior (from_tty);
c906108c
SS
2508}
2509
6bd6f3b6 2510/* See target.h. */
c906108c
SS
2511
2512void
6e1e1966 2513target_detach (inferior *inf, int from_tty)
c906108c 2514{
ddff2a2d
AB
2515 /* Thread's don't need to be resumed until the end of this function. */
2516 scoped_disable_commit_resumed disable_commit_resumed ("detaching");
2517
5783e150
PW
2518 /* After we have detached, we will clear the register cache for this inferior
2519 by calling registers_changed_ptid. We must save the pid_ptid before
2520 detaching, as the target detach method will clear inf->pid. */
2521 ptid_t save_pid_ptid = ptid_t (inf->pid);
2522
6e1e1966 2523 /* As long as some to_detach implementations rely on the current_inferior
99d9c3b9
SM
2524 (either directly, or indirectly, like through reading memory), INF needs
2525 to be the current inferior. When that requirement will become no longer
2526 true, then we can remove this assertion. */
6e1e1966
SM
2527 gdb_assert (inf == current_inferior ());
2528
24291992
PA
2529 prepare_for_detach ();
2530
9056c917
SM
2531 gdb::observers::inferior_pre_detach.notify (inf);
2532
5b6d1e4f
PA
2533 /* Hold a strong reference because detaching may unpush the
2534 target. */
2535 auto proc_target_ref = target_ops_ref::new_reference (inf->process_target ());
2536
328d42d8 2537 current_inferior ()->top_target ()->detach (inf, from_tty);
799efbe8 2538
5b6d1e4f
PA
2539 process_stratum_target *proc_target
2540 = as_process_stratum_target (proc_target_ref.get ());
2541
2542 registers_changed_ptid (proc_target, save_pid_ptid);
799efbe8
PW
2543
2544 /* We have to ensure we have no frame cache left. Normally,
5783e150
PW
2545 registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
2546 inferior_ptid matches save_pid_ptid, but in our case, it does not
799efbe8
PW
2547 call it, as inferior_ptid has been reset. */
2548 reinit_frame_cache ();
ddff2a2d
AB
2549
2550 disable_commit_resumed.reset_and_commit ();
c906108c
SS
2551}
2552
6ad8ae5c 2553void
fee354ee 2554target_disconnect (const char *args, int from_tty)
6ad8ae5c 2555{
50c71eaf
PA
2556 /* If we're in breakpoints-always-inserted mode or if breakpoints
2557 are global across processes, we have to remove them before
2558 disconnecting. */
74960c60
VP
2559 remove_breakpoints ();
2560
328d42d8 2561 current_inferior ()->top_target ()->disconnect (args, from_tty);
6ad8ae5c
DJ
2562}
2563
f2b9e3df
SDJ
2564/* See target/target.h. */
2565
117de6a9 2566ptid_t
b60cea74
TT
2567target_wait (ptid_t ptid, struct target_waitstatus *status,
2568 target_wait_flags options)
117de6a9 2569{
328d42d8 2570 target_ops *target = current_inferior ()->top_target ();
1192f124
SM
2571 process_stratum_target *proc_target = current_inferior ()->process_target ();
2572
2573 gdb_assert (!proc_target->commit_resumed_state);
d3a07122 2574
0c1e6e26 2575 if (!target_can_async_p (target))
d3a07122
SM
2576 gdb_assert ((options & TARGET_WNOHANG) == 0);
2577
fb85cece
PM
2578 try
2579 {
2580 gdb::observers::target_pre_wait.notify (ptid);
2581 ptid_t event_ptid = target->wait (ptid, status, options);
2582 gdb::observers::target_post_wait.notify (event_ptid);
2583 return event_ptid;
2584 }
2585 catch (...)
2586 {
2587 gdb::observers::target_post_wait.notify (null_ptid);
2588 throw;
2589 }
117de6a9
PA
2590}
2591
0b333c5e
PA
2592/* See target.h. */
2593
2594ptid_t
2595default_target_wait (struct target_ops *ops,
2596 ptid_t ptid, struct target_waitstatus *status,
b60cea74 2597 target_wait_flags options)
0b333c5e 2598{
183be222 2599 status->set_ignore ();
0b333c5e
PA
2600 return minus_one_ptid;
2601}
2602
a068643d 2603std::string
117de6a9
PA
2604target_pid_to_str (ptid_t ptid)
2605{
328d42d8 2606 return current_inferior ()->top_target ()->pid_to_str (ptid);
117de6a9
PA
2607}
2608
73ede765 2609const char *
4694da01
TT
2610target_thread_name (struct thread_info *info)
2611{
5b6d1e4f
PA
2612 gdb_assert (info->inf == current_inferior ());
2613
328d42d8 2614 return current_inferior ()->top_target ()->thread_name (info);
4694da01
TT
2615}
2616
e04ee09e
KB
2617struct thread_info *
2618target_thread_handle_to_thread_info (const gdb_byte *thread_handle,
2619 int handle_len,
2620 struct inferior *inf)
2621{
328d42d8
SM
2622 target_ops *target = current_inferior ()->top_target ();
2623
2624 return target->thread_handle_to_thread_info (thread_handle, handle_len, inf);
e04ee09e
KB
2625}
2626
3d6c6204
KB
2627/* See target.h. */
2628
1f08d324 2629gdb::array_view<const gdb_byte>
3d6c6204
KB
2630target_thread_info_to_thread_handle (struct thread_info *tip)
2631{
328d42d8
SM
2632 target_ops *target = current_inferior ()->top_target ();
2633
2634 return target->thread_info_to_thread_handle (tip);
3d6c6204
KB
2635}
2636
e1ac3328 2637void
d51926f0 2638target_resume (ptid_t scope_ptid, int step, enum gdb_signal signal)
e1ac3328 2639{
5b6d1e4f 2640 process_stratum_target *curr_target = current_inferior ()->process_target ();
1192f124 2641 gdb_assert (!curr_target->commit_resumed_state);
5b6d1e4f 2642
d51926f0
PA
2643 gdb_assert (inferior_ptid != null_ptid);
2644 gdb_assert (inferior_ptid.matches (scope_ptid));
2645
41336620 2646 target_dcache_invalidate (current_program_space->aspace);
28439f5e 2647
d51926f0 2648 current_inferior ()->top_target ()->resume (scope_ptid, step, signal);
28439f5e 2649
d51926f0 2650 registers_changed_ptid (curr_target, scope_ptid);
251bde03 2651 /* We only set the internal executing state here. The user/frontend
f2ffa92b
PA
2652 running state is set at a higher level. This also clears the
2653 thread's stop_pc as side effect. */
d51926f0
PA
2654 set_executing (curr_target, scope_ptid, true);
2655 clear_inline_frame_state (curr_target, scope_ptid);
38ba82db
JB
2656
2657 if (target_can_async_p ())
4a570176 2658 target_async (true);
e1ac3328 2659}
2455069d 2660
85ad3aaf
PA
2661/* See target.h. */
2662
2663void
1192f124 2664target_commit_resumed ()
85ad3aaf 2665{
1192f124
SM
2666 gdb_assert (current_inferior ()->process_target ()->commit_resumed_state);
2667 current_inferior ()->top_target ()->commit_resumed ();
85ad3aaf
PA
2668}
2669
b4b1a226
SM
2670/* See target.h. */
2671
2672bool
2673target_has_pending_events ()
2674{
2675 return current_inferior ()->top_target ()->has_pending_events ();
2676}
2677
2455069d 2678void
adc6a863 2679target_pass_signals (gdb::array_view<const unsigned char> pass_signals)
2455069d 2680{
328d42d8 2681 current_inferior ()->top_target ()->pass_signals (pass_signals);
2455069d
UW
2682}
2683
9b224c5e 2684void
adc6a863 2685target_program_signals (gdb::array_view<const unsigned char> program_signals)
9b224c5e 2686{
328d42d8 2687 current_inferior ()->top_target ()->program_signals (program_signals);
9b224c5e
PA
2688}
2689
e97007b6 2690static void
82d1f134
SM
2691default_follow_fork (struct target_ops *self, inferior *child_inf,
2692 ptid_t child_ptid, target_waitkind fork_kind,
2693 bool follow_child, bool detach_fork)
098dba18
TT
2694{
2695 /* Some target returned a fork event, but did not know how to follow it. */
f34652de 2696 internal_error (_("could not find a target to follow fork"));
098dba18
TT
2697}
2698
0d36baa9
PA
2699static void
2700default_follow_clone (struct target_ops *self, ptid_t child_ptid)
2701{
2702 /* Some target returned a clone event, but did not know how to follow it. */
2703 internal_error (_("could not find a target to follow clone"));
2704}
2705
e97007b6 2706/* See target.h. */
ee057212 2707
e97007b6 2708void
82d1f134
SM
2709target_follow_fork (inferior *child_inf, ptid_t child_ptid,
2710 target_waitkind fork_kind, bool follow_child,
2711 bool detach_fork)
ee057212 2712{
328d42d8
SM
2713 target_ops *target = current_inferior ()->top_target ();
2714
82d1f134
SM
2715 /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
2716 DETACH_FORK. */
2717 if (child_inf != nullptr)
2718 {
2719 gdb_assert (follow_child || !detach_fork);
2720 gdb_assert (child_inf->pid == child_ptid.pid ());
2721 }
2722 else
2723 gdb_assert (!follow_child && detach_fork);
2724
2725 return target->follow_fork (child_inf, child_ptid, fork_kind, follow_child,
2726 detach_fork);
ee057212
DJ
2727}
2728
294c36eb 2729/* See target.h. */
94585166
DB
2730
2731void
294c36eb
SM
2732target_follow_exec (inferior *follow_inf, ptid_t ptid,
2733 const char *execd_pathname)
94585166 2734{
294c36eb
SM
2735 current_inferior ()->top_target ()->follow_exec (follow_inf, ptid,
2736 execd_pathname);
94585166
DB
2737}
2738
8d657035
TT
2739static void
2740default_mourn_inferior (struct target_ops *self)
2741{
f34652de 2742 internal_error (_("could not find a target to follow mourn inferior"));
8d657035
TT
2743}
2744
136d6dae 2745void
bc1e6c81 2746target_mourn_inferior (ptid_t ptid)
136d6dae 2747{
dffdd8b5 2748 gdb_assert (ptid.pid () == inferior_ptid.pid ());
328d42d8 2749 current_inferior ()->top_target ()->mourn_inferior ();
136d6dae
VP
2750}
2751
424163ea
DJ
2752/* Look for a target which can describe architectural features, starting
2753 from TARGET. If we find one, return its description. */
2754
2755const struct target_desc *
2756target_read_description (struct target_ops *target)
2757{
f6ac5f3d 2758 return target->read_description ();
424163ea
DJ
2759}
2760
08388c79 2761
58a5184e
TT
2762/* Default implementation of memory-searching. */
2763
2764static int
2765default_search_memory (struct target_ops *self,
2766 CORE_ADDR start_addr, ULONGEST search_space_len,
2767 const gdb_byte *pattern, ULONGEST pattern_len,
2768 CORE_ADDR *found_addrp)
2769{
4a72de73
TT
2770 auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
2771 {
328d42d8
SM
2772 return target_read (current_inferior ()->top_target (),
2773 TARGET_OBJECT_MEMORY, NULL,
4a72de73
TT
2774 result, addr, len) == len;
2775 };
2776
58a5184e 2777 /* Start over from the top of the target stack. */
4a72de73 2778 return simple_search_memory (read_memory, start_addr, search_space_len,
58a5184e
TT
2779 pattern, pattern_len, found_addrp);
2780}
2781
08388c79
DE
2782/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2783 sequence of bytes in PATTERN with length PATTERN_LEN.
2784
2785 The result is 1 if found, 0 if not found, and -1 if there was an error
2786 requiring halting of the search (e.g. memory read error).
2787 If the pattern is found the address is recorded in FOUND_ADDRP. */
2788
2789int
2790target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2791 const gdb_byte *pattern, ULONGEST pattern_len,
2792 CORE_ADDR *found_addrp)
2793{
328d42d8
SM
2794 target_ops *target = current_inferior ()->top_target ();
2795
2796 return target->search_memory (start_addr, search_space_len, pattern,
2797 pattern_len, found_addrp);
08388c79
DE
2798}
2799
8edfe269
DJ
2800/* Look through the currently pushed targets. If none of them will
2801 be able to restart the currently running process, issue an error
2802 message. */
2803
2804void
2805target_require_runnable (void)
2806{
328d42d8
SM
2807 for (target_ops *t = current_inferior ()->top_target ();
2808 t != NULL;
2809 t = t->beneath ())
8edfe269
DJ
2810 {
2811 /* If this target knows how to create a new program, then
2812 assume we will still be able to after killing the current
2813 one. Either killing and mourning will not pop T, or else
2814 find_default_run_target will find it again. */
f6ac5f3d 2815 if (t->can_create_inferior ())
8edfe269
DJ
2816 return;
2817
548740d6 2818 /* Do not worry about targets at certain strata that can not
8edfe269
DJ
2819 create inferiors. Assume they will be pushed again if
2820 necessary, and continue to the process_stratum. */
66b4deae 2821 if (t->stratum () > process_stratum)
8edfe269
DJ
2822 continue;
2823
3e43a32a
MS
2824 error (_("The \"%s\" target does not support \"run\". "
2825 "Try \"help target\" or \"continue\"."),
f6ac5f3d 2826 t->shortname ());
8edfe269
DJ
2827 }
2828
2829 /* This function is only called if the target is running. In that
2830 case there should have been a process_stratum target and it
c378eb4e 2831 should either know how to create inferiors, or not... */
f34652de 2832 internal_error (_("No targets found"));
8edfe269
DJ
2833}
2834
6a3cb8e8
PA
2835/* Whether GDB is allowed to fall back to the default run target for
2836 "run", "attach", etc. when no target is connected yet. */
491144b5 2837static bool auto_connect_native_target = true;
6a3cb8e8
PA
2838
2839static void
2840show_auto_connect_native_target (struct ui_file *file, int from_tty,
2841 struct cmd_list_element *c, const char *value)
2842{
6cb06a8c
TT
2843 gdb_printf (file,
2844 _("Whether GDB may automatically connect to the "
2845 "native target is %s.\n"),
2846 value);
6a3cb8e8
PA
2847}
2848
d9f719f1
PA
2849/* A pointer to the target that can respond to "run" or "attach".
2850 Native targets are always singletons and instantiated early at GDB
2851 startup. */
2852static target_ops *the_native_target;
2853
2854/* See target.h. */
2855
2856void
2857set_native_target (target_ops *target)
2858{
2859 if (the_native_target != NULL)
f34652de 2860 internal_error (_("native target already set (\"%s\")."),
d9f719f1
PA
2861 the_native_target->longname ());
2862
2863 the_native_target = target;
2864}
2865
2866/* See target.h. */
2867
2868target_ops *
2869get_native_target ()
2870{
2871 return the_native_target;
2872}
2873
c906108c
SS
2874/* Look through the list of possible targets for a target that can
2875 execute a run or attach command without any other data. This is
2876 used to locate the default process stratum.
2877
5f667f2d
PA
2878 If DO_MESG is not NULL, the result is always valid (error() is
2879 called for errors); else, return NULL on error. */
c906108c
SS
2880
2881static struct target_ops *
a121b7c1 2882find_default_run_target (const char *do_mesg)
c906108c 2883{
d9f719f1
PA
2884 if (auto_connect_native_target && the_native_target != NULL)
2885 return the_native_target;
c906108c 2886
d9f719f1
PA
2887 if (do_mesg != NULL)
2888 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2889 return NULL;
c906108c
SS
2890}
2891
b3ccfe11 2892/* See target.h. */
c906108c 2893
b3ccfe11
TT
2894struct target_ops *
2895find_attach_target (void)
c906108c 2896{
b3ccfe11 2897 /* If a target on the current stack can attach, use it. */
328d42d8
SM
2898 for (target_ops *t = current_inferior ()->top_target ();
2899 t != NULL;
2900 t = t->beneath ())
b3ccfe11 2901 {
f6ac5f3d 2902 if (t->can_attach ())
d9f719f1 2903 return t;
b3ccfe11 2904 }
c906108c 2905
b3ccfe11 2906 /* Otherwise, use the default run target for attaching. */
d9f719f1 2907 return find_default_run_target ("attach");
b84876c2
PA
2908}
2909
b3ccfe11 2910/* See target.h. */
b84876c2 2911
b3ccfe11
TT
2912struct target_ops *
2913find_run_target (void)
9908b566 2914{
f6ac5f3d 2915 /* If a target on the current stack can run, use it. */
328d42d8
SM
2916 for (target_ops *t = current_inferior ()->top_target ();
2917 t != NULL;
2918 t = t->beneath ())
b3ccfe11 2919 {
f6ac5f3d 2920 if (t->can_create_inferior ())
d9f719f1 2921 return t;
b3ccfe11 2922 }
5d502164 2923
b3ccfe11 2924 /* Otherwise, use the default run target. */
d9f719f1 2925 return find_default_run_target ("run");
9908b566
VP
2926}
2927
f6ac5f3d
PA
2928bool
2929target_ops::info_proc (const char *args, enum info_proc_what what)
2930{
2931 return false;
2932}
2933
145b16a9
UW
2934/* Implement the "info proc" command. */
2935
451b7c33 2936int
7bc112c1 2937target_info_proc (const char *args, enum info_proc_what what)
145b16a9
UW
2938{
2939 struct target_ops *t;
2940
2941 /* If we're already connected to something that can get us OS
2942 related data, use it. Otherwise, try using the native
2943 target. */
f6ac5f3d
PA
2944 t = find_target_at (process_stratum);
2945 if (t == NULL)
145b16a9
UW
2946 t = find_default_run_target (NULL);
2947
b6a8c27b 2948 for (; t != NULL; t = t->beneath ())
145b16a9 2949 {
f6ac5f3d 2950 if (t->info_proc (args, what))
145b16a9 2951 {
cb5dfff8 2952 target_debug_printf_nofunc ("target_info_proc (\"%s\", %d)", args, what);
451b7c33 2953 return 1;
145b16a9
UW
2954 }
2955 }
2956
451b7c33 2957 return 0;
145b16a9
UW
2958}
2959
03583c20 2960static int
2bfc0540 2961find_default_supports_disable_randomization (struct target_ops *self)
03583c20
UW
2962{
2963 struct target_ops *t;
2964
2965 t = find_default_run_target (NULL);
f6ac5f3d
PA
2966 if (t != NULL)
2967 return t->supports_disable_randomization ();
03583c20
UW
2968 return 0;
2969}
2970
2971int
2972target_supports_disable_randomization (void)
2973{
328d42d8 2974 return current_inferior ()->top_target ()->supports_disable_randomization ();
03583c20 2975}
9908b566 2976
1fb77080
SDJ
2977/* See target/target.h. */
2978
2979int
2980target_supports_multi_process (void)
2981{
328d42d8 2982 return current_inferior ()->top_target ()->supports_multi_process ();
1fb77080
SDJ
2983}
2984
b7b030ad
TT
2985/* See target.h. */
2986
6b09f134 2987std::optional<gdb::char_vector>
07e059b5
VP
2988target_get_osdata (const char *type)
2989{
07e059b5
VP
2990 struct target_ops *t;
2991
739ef7fb
PA
2992 /* If we're already connected to something that can get us OS
2993 related data, use it. Otherwise, try using the native
2994 target. */
f6ac5f3d
PA
2995 t = find_target_at (process_stratum);
2996 if (t == NULL)
739ef7fb 2997 t = find_default_run_target ("get OS data");
07e059b5
VP
2998
2999 if (!t)
9018be22 3000 return {};
07e059b5 3001
6d097e65 3002 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
3003}
3004
b6a8c27b
PA
3005/* See target.h. */
3006
3007target_ops *
3008target_ops::beneath () const
3009{
5b6d1e4f 3010 return current_inferior ()->find_target_beneath (this);
b6a8c27b
PA
3011}
3012
f6ac5f3d
PA
3013void
3014target_ops::close ()
3015{
3016}
3017
3018bool
3019target_ops::can_attach ()
3020{
3021 return 0;
3022}
3023
3024void
3025target_ops::attach (const char *, int)
3026{
3027 gdb_assert_not_reached ("target_ops::attach called");
3028}
3029
3030bool
3031target_ops::can_create_inferior ()
3032{
3033 return 0;
3034}
3035
3036void
3037target_ops::create_inferior (const char *, const std::string &,
3038 char **, int)
3039{
3040 gdb_assert_not_reached ("target_ops::create_inferior called");
3041}
3042
57810aa7 3043bool
f6ac5f3d
PA
3044target_ops::can_run ()
3045{
57810aa7 3046 return false;
f6ac5f3d
PA
3047}
3048
3049int
3050target_can_run ()
3051{
328d42d8
SM
3052 for (target_ops *t = current_inferior ()->top_target ();
3053 t != NULL;
3054 t = t->beneath ())
f6ac5f3d
PA
3055 {
3056 if (t->can_run ())
3057 return 1;
3058 }
3059
3060 return 0;
3061}
7313baad
UW
3062
3063/* Target file operations. */
3064
3065static struct target_ops *
3066default_fileio_target (void)
3067{
f6ac5f3d
PA
3068 struct target_ops *t;
3069
7313baad
UW
3070 /* If we're already connected to something that can perform
3071 file I/O, use it. Otherwise, try using the native target. */
f6ac5f3d
PA
3072 t = find_target_at (process_stratum);
3073 if (t != NULL)
3074 return t;
3075 return find_default_run_target ("file I/O");
7313baad
UW
3076}
3077
1c4b552b
GB
3078/* File handle for target file operations. */
3079
5ff79300 3080struct fileio_fh_t
1c4b552b 3081{
20db9c52
PA
3082 /* The target on which this file is open. NULL if the target is
3083 meanwhile closed while the handle is open. */
5ff79300 3084 target_ops *target;
1c4b552b
GB
3085
3086 /* The file descriptor on the target. */
5ff79300 3087 int target_fd;
1c4b552b 3088
5ff79300
PA
3089 /* Check whether this fileio_fh_t represents a closed file. */
3090 bool is_closed ()
3091 {
3092 return target_fd < 0;
3093 }
3094};
1c4b552b
GB
3095
3096/* Vector of currently open file handles. The value returned by
3097 target_fileio_open and passed as the FD argument to other
3098 target_fileio_* functions is an index into this vector. This
3099 vector's entries are never freed; instead, files are marked as
3100 closed, and the handle becomes available for reuse. */
5ff79300 3101static std::vector<fileio_fh_t> fileio_fhandles;
1c4b552b
GB
3102
3103/* Index into fileio_fhandles of the lowest handle that might be
3104 closed. This permits handle reuse without searching the whole
3105 list each time a new file is opened. */
3106static int lowest_closed_fd;
3107
d7cb0ef3 3108/* See target.h. */
20db9c52 3109
d7cb0ef3 3110void
20db9c52
PA
3111fileio_handles_invalidate_target (target_ops *targ)
3112{
3113 for (fileio_fh_t &fh : fileio_fhandles)
3114 if (fh.target == targ)
3115 fh.target = NULL;
3116}
3117
1c4b552b
GB
3118/* Acquire a target fileio file descriptor. */
3119
3120static int
5ff79300 3121acquire_fileio_fd (target_ops *target, int target_fd)
1c4b552b 3122{
1c4b552b 3123 /* Search for closed handles to reuse. */
5ff79300
PA
3124 for (; lowest_closed_fd < fileio_fhandles.size (); lowest_closed_fd++)
3125 {
3126 fileio_fh_t &fh = fileio_fhandles[lowest_closed_fd];
3127
3128 if (fh.is_closed ())
3129 break;
3130 }
1c4b552b
GB
3131
3132 /* Push a new handle if no closed handles were found. */
5ff79300
PA
3133 if (lowest_closed_fd == fileio_fhandles.size ())
3134 fileio_fhandles.push_back (fileio_fh_t {target, target_fd});
3135 else
3136 fileio_fhandles[lowest_closed_fd] = {target, target_fd};
1c4b552b 3137
5ff79300
PA
3138 /* Should no longer be marked closed. */
3139 gdb_assert (!fileio_fhandles[lowest_closed_fd].is_closed ());
1c4b552b
GB
3140
3141 /* Return its index, and start the next lookup at
3142 the next index. */
3143 return lowest_closed_fd++;
3144}
3145
3146/* Release a target fileio file descriptor. */
3147
3148static void
3149release_fileio_fd (int fd, fileio_fh_t *fh)
3150{
5ff79300 3151 fh->target_fd = -1;
325fac50 3152 lowest_closed_fd = std::min (lowest_closed_fd, fd);
1c4b552b
GB
3153}
3154
3155/* Return a pointer to the fileio_fhandle_t corresponding to FD. */
3156
5ff79300
PA
3157static fileio_fh_t *
3158fileio_fd_to_fh (int fd)
3159{
3160 return &fileio_fhandles[fd];
3161}
1c4b552b 3162
f6ac5f3d
PA
3163
3164/* Default implementations of file i/o methods. We don't want these
3165 to delegate automatically, because we need to know which target
3166 supported the method, in order to call it directly from within
3167 pread/pwrite, etc. */
3168
3169int
3170target_ops::fileio_open (struct inferior *inf, const char *filename,
3171 int flags, int mode, int warn_if_slow,
b872057a 3172 fileio_error *target_errno)
f6ac5f3d
PA
3173{
3174 *target_errno = FILEIO_ENOSYS;
3175 return -1;
3176}
3177
3178int
3179target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
b872057a 3180 ULONGEST offset, fileio_error *target_errno)
f6ac5f3d
PA
3181{
3182 *target_errno = FILEIO_ENOSYS;
3183 return -1;
3184}
3185
3186int
3187target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len,
b872057a 3188 ULONGEST offset, fileio_error *target_errno)
f6ac5f3d
PA
3189{
3190 *target_errno = FILEIO_ENOSYS;
3191 return -1;
3192}
3193
3194int
b872057a 3195target_ops::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
f6ac5f3d
PA
3196{
3197 *target_errno = FILEIO_ENOSYS;
3198 return -1;
3199}
3200
3201int
b872057a 3202target_ops::fileio_close (int fd, fileio_error *target_errno)
f6ac5f3d
PA
3203{
3204 *target_errno = FILEIO_ENOSYS;
3205 return -1;
3206}
3207
3208int
3209target_ops::fileio_unlink (struct inferior *inf, const char *filename,
b872057a 3210 fileio_error *target_errno)
f6ac5f3d
PA
3211{
3212 *target_errno = FILEIO_ENOSYS;
3213 return -1;
3214}
3215
6b09f134 3216std::optional<std::string>
f6ac5f3d 3217target_ops::fileio_readlink (struct inferior *inf, const char *filename,
b872057a 3218 fileio_error *target_errno)
f6ac5f3d
PA
3219{
3220 *target_errno = FILEIO_ENOSYS;
3221 return {};
3222}
3223
4111f652 3224/* See target.h. */
12e2a5fd 3225
4111f652
PA
3226int
3227target_fileio_open (struct inferior *inf, const char *filename,
b872057a 3228 int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
7313baad 3229{
b6a8c27b 3230 for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
7313baad 3231 {
f6ac5f3d
PA
3232 int fd = t->fileio_open (inf, filename, flags, mode,
3233 warn_if_slow, target_errno);
7313baad 3234
f6ac5f3d
PA
3235 if (fd == -1 && *target_errno == FILEIO_ENOSYS)
3236 continue;
1c4b552b 3237
f6ac5f3d
PA
3238 if (fd < 0)
3239 fd = -1;
3240 else
3241 fd = acquire_fileio_fd (t, fd);
3242
cb5dfff8
SM
3243 target_debug_printf_nofunc ("target_fileio_open (%d,%s,0x%x,0%o,%d) = %d (%d)",
3244 inf == NULL ? 0 : inf->num, filename, flags, mode,
3245 warn_if_slow, fd, fd != -1 ? 0 : *target_errno);
f6ac5f3d 3246 return fd;
7313baad
UW
3247 }
3248
3249 *target_errno = FILEIO_ENOSYS;
3250 return -1;
3251}
3252
12e2a5fd
GB
3253/* See target.h. */
3254
7313baad
UW
3255int
3256target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
b872057a 3257 ULONGEST offset, fileio_error *target_errno)
7313baad 3258{
1c4b552b
GB
3259 fileio_fh_t *fh = fileio_fd_to_fh (fd);
3260 int ret = -1;
7313baad 3261
5ff79300 3262 if (fh->is_closed ())
b872057a 3263 *target_errno = FILEIO_EBADF;
20db9c52 3264 else if (fh->target == NULL)
b872057a 3265 *target_errno = FILEIO_EIO;
1c4b552b 3266 else
f6ac5f3d
PA
3267 ret = fh->target->fileio_pwrite (fh->target_fd, write_buf,
3268 len, offset, target_errno);
7313baad 3269
cb5dfff8
SM
3270 target_debug_printf_nofunc ("target_fileio_pwrite (%d,...,%d,%s) = %d (%d)", fd,
3271 len, pulongest (offset), ret,
3272 ret != -1 ? 0 : *target_errno);
1c4b552b 3273 return ret;
7313baad
UW
3274}
3275
12e2a5fd
GB
3276/* See target.h. */
3277
7313baad
UW
3278int
3279target_fileio_pread (int fd, gdb_byte *read_buf, int len,
b872057a 3280 ULONGEST offset, fileio_error *target_errno)
7313baad 3281{
1c4b552b
GB
3282 fileio_fh_t *fh = fileio_fd_to_fh (fd);
3283 int ret = -1;
7313baad 3284
5ff79300 3285 if (fh->is_closed ())
b872057a 3286 *target_errno = FILEIO_EBADF;
20db9c52 3287 else if (fh->target == NULL)
b872057a 3288 *target_errno = FILEIO_EIO;
1c4b552b 3289 else
f6ac5f3d
PA
3290 ret = fh->target->fileio_pread (fh->target_fd, read_buf,
3291 len, offset, target_errno);
7313baad 3292
cb5dfff8
SM
3293 target_debug_printf_nofunc ("target_fileio_pread (%d,...,%d,%s) = %d (%d)", fd, len,
3294 pulongest (offset), ret, ret != -1 ? 0 : *target_errno);
9b15c1f0
GB
3295 return ret;
3296}
3297
3298/* See target.h. */
12e2a5fd 3299
9b15c1f0 3300int
b872057a 3301target_fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
9b15c1f0
GB
3302{
3303 fileio_fh_t *fh = fileio_fd_to_fh (fd);
3304 int ret = -1;
3305
5ff79300 3306 if (fh->is_closed ())
b872057a 3307 *target_errno = FILEIO_EBADF;
20db9c52 3308 else if (fh->target == NULL)
b872057a 3309 *target_errno = FILEIO_EIO;
9b15c1f0 3310 else
f6ac5f3d 3311 ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
9b15c1f0 3312
cb5dfff8
SM
3313 target_debug_printf_nofunc ("target_fileio_fstat (%d) = %d (%d)", fd, ret,
3314 ret != -1 ? 0 : *target_errno);
1c4b552b 3315 return ret;
7313baad
UW
3316}
3317
12e2a5fd
GB
3318/* See target.h. */
3319
7313baad 3320int
b872057a 3321target_fileio_close (int fd, fileio_error *target_errno)
7313baad 3322{
1c4b552b
GB
3323 fileio_fh_t *fh = fileio_fd_to_fh (fd);
3324 int ret = -1;
7313baad 3325
5ff79300 3326 if (fh->is_closed ())
b872057a 3327 *target_errno = FILEIO_EBADF;
1c4b552b 3328 else
7313baad 3329 {
20db9c52 3330 if (fh->target != NULL)
f6ac5f3d
PA
3331 ret = fh->target->fileio_close (fh->target_fd,
3332 target_errno);
20db9c52
PA
3333 else
3334 ret = 0;
1c4b552b 3335 release_fileio_fd (fd, fh);
7313baad
UW
3336 }
3337
cb5dfff8
SM
3338 target_debug_printf_nofunc ("target_fileio_close (%d) = %d (%d)", fd, ret,
3339 ret != -1 ? 0 : *target_errno);
1c4b552b 3340 return ret;
7313baad
UW
3341}
3342
12e2a5fd
GB
3343/* See target.h. */
3344
7313baad 3345int
07c138c8 3346target_fileio_unlink (struct inferior *inf, const char *filename,
b872057a 3347 fileio_error *target_errno)
7313baad 3348{
b6a8c27b 3349 for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
7313baad 3350 {
f6ac5f3d 3351 int ret = t->fileio_unlink (inf, filename, target_errno);
7313baad 3352
f6ac5f3d
PA
3353 if (ret == -1 && *target_errno == FILEIO_ENOSYS)
3354 continue;
3355
cb5dfff8
SM
3356 target_debug_printf_nofunc ("target_fileio_unlink (%d,%s) = %d (%d)",
3357 inf == NULL ? 0 : inf->num, filename, ret,
3358 ret != -1 ? 0 : *target_errno);
f6ac5f3d 3359 return ret;
7313baad
UW
3360 }
3361
3362 *target_errno = FILEIO_ENOSYS;
3363 return -1;
3364}
3365
12e2a5fd
GB
3366/* See target.h. */
3367
6b09f134 3368std::optional<std::string>
07c138c8 3369target_fileio_readlink (struct inferior *inf, const char *filename,
b872057a 3370 fileio_error *target_errno)
b9e7b9c3 3371{
b6a8c27b 3372 for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
b9e7b9c3 3373 {
6b09f134 3374 std::optional<std::string> ret
f6ac5f3d 3375 = t->fileio_readlink (inf, filename, target_errno);
b9e7b9c3 3376
f6ac5f3d
PA
3377 if (!ret.has_value () && *target_errno == FILEIO_ENOSYS)
3378 continue;
3379
cb5dfff8
SM
3380 target_debug_printf_nofunc ("target_fileio_readlink (%d,%s) = %s (%d)",
3381 inf == NULL ? 0 : inf->num, filename,
3382 ret ? ret->c_str () : "(nil)",
3383 ret ? 0 : *target_errno);
f6ac5f3d 3384 return ret;
b9e7b9c3
UW
3385 }
3386
3387 *target_errno = FILEIO_ENOSYS;
e0d3522b 3388 return {};
b9e7b9c3
UW
3389}
3390
770623f7
TT
3391/* Like scoped_fd, but specific to target fileio. */
3392
3393class scoped_target_fd
7313baad 3394{
770623f7
TT
3395public:
3396 explicit scoped_target_fd (int fd) noexcept
3397 : m_fd (fd)
3398 {
3399 }
7313baad 3400
770623f7
TT
3401 ~scoped_target_fd ()
3402 {
3403 if (m_fd >= 0)
3404 {
b872057a 3405 fileio_error target_errno;
770623f7
TT
3406
3407 target_fileio_close (m_fd, &target_errno);
3408 }
3409 }
3410
3411 DISABLE_COPY_AND_ASSIGN (scoped_target_fd);
3412
3413 int get () const noexcept
3414 {
3415 return m_fd;
3416 }
3417
3418private:
3419 int m_fd;
3420};
7313baad 3421
07c138c8
GB
3422/* Read target file FILENAME, in the filesystem as seen by INF. If
3423 INF is NULL, use the filesystem seen by the debugger (GDB or, for
3424 remote targets, the remote stub). Store the result in *BUF_P and
3425 return the size of the transferred data. PADDING additional bytes
3426 are available in *BUF_P. This is a helper function for
3427 target_fileio_read_alloc; see the declaration of that function for
3428 more information. */
7313baad 3429
f7af1fcd
JK
3430static LONGEST
3431target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
3432 gdb_byte **buf_p, int padding)
3433{
db1ff28b
JK
3434 size_t buf_alloc, buf_pos;
3435 gdb_byte *buf;
3436 LONGEST n;
b872057a 3437 fileio_error target_errno;
f7af1fcd 3438
770623f7 3439 scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
4111f652 3440 0700, false, &target_errno));
770623f7 3441 if (fd.get () == -1)
f7af1fcd
JK
3442 return -1;
3443
db1ff28b
JK
3444 /* Start by reading up to 4K at a time. The target will throttle
3445 this number down if necessary. */
3446 buf_alloc = 4096;
224c3ddb 3447 buf = (gdb_byte *) xmalloc (buf_alloc);
db1ff28b
JK
3448 buf_pos = 0;
3449 while (1)
3450 {
770623f7 3451 n = target_fileio_pread (fd.get (), &buf[buf_pos],
db1ff28b
JK
3452 buf_alloc - buf_pos - padding, buf_pos,
3453 &target_errno);
3454 if (n < 0)
3455 {
3456 /* An error occurred. */
db1ff28b
JK
3457 xfree (buf);
3458 return -1;
3459 }
3460 else if (n == 0)
3461 {
3462 /* Read all there was. */
db1ff28b
JK
3463 if (buf_pos == 0)
3464 xfree (buf);
3465 else
3466 *buf_p = buf;
3467 return buf_pos;
3468 }
3469
3470 buf_pos += n;
3471
3472 /* If the buffer is filling up, expand it. */
3473 if (buf_alloc < buf_pos * 2)
3474 {
3475 buf_alloc *= 2;
224c3ddb 3476 buf = (gdb_byte *) xrealloc (buf, buf_alloc);
db1ff28b
JK
3477 }
3478
3479 QUIT;
3480 }
f7af1fcd
JK
3481}
3482
12e2a5fd 3483/* See target.h. */
7313baad
UW
3484
3485LONGEST
07c138c8
GB
3486target_fileio_read_alloc (struct inferior *inf, const char *filename,
3487 gdb_byte **buf_p)
7313baad 3488{
07c138c8 3489 return target_fileio_read_alloc_1 (inf, filename, buf_p, 0);
7313baad
UW
3490}
3491
db1ff28b 3492/* See target.h. */
f7af1fcd 3493
87028b87 3494gdb::unique_xmalloc_ptr<char>
f7af1fcd
JK
3495target_fileio_read_stralloc (struct inferior *inf, const char *filename)
3496{
db1ff28b
JK
3497 gdb_byte *buffer;
3498 char *bufstr;
3499 LONGEST i, transferred;
3500
3501 transferred = target_fileio_read_alloc_1 (inf, filename, &buffer, 1);
3502 bufstr = (char *) buffer;
3503
3504 if (transferred < 0)
87028b87 3505 return gdb::unique_xmalloc_ptr<char> (nullptr);
db1ff28b
JK
3506
3507 if (transferred == 0)
b02f78f9 3508 return make_unique_xstrdup ("");
db1ff28b
JK
3509
3510 bufstr[transferred] = 0;
3511
3512 /* Check for embedded NUL bytes; but allow trailing NULs. */
3513 for (i = strlen (bufstr); i < transferred; i++)
3514 if (bufstr[i] != 0)
3515 {
3516 warning (_("target file %s "
3517 "contained unexpected null characters"),
3518 filename);
3519 break;
3520 }
3521
87028b87 3522 return gdb::unique_xmalloc_ptr<char> (bufstr);
f7af1fcd 3523}
7313baad 3524
db1ff28b 3525
e0d24f8d 3526static int
31568a15
TT
3527default_region_ok_for_hw_watchpoint (struct target_ops *self,
3528 CORE_ADDR addr, int len)
e0d24f8d 3529{
99d9c3b9
SM
3530 gdbarch *arch = current_inferior ()->arch ();
3531 return (len <= gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT);
ccaa32c7
GS
3532}
3533
5009afc5
AS
3534static int
3535default_watchpoint_addr_within_range (struct target_ops *target,
3536 CORE_ADDR addr,
3537 CORE_ADDR start, int length)
3538{
3539 return addr >= start && addr < start + length;
3540}
3541
8b06beed
TT
3542/* See target.h. */
3543
a1740ee1
PA
3544target_ops *
3545target_stack::find_beneath (const target_ops *t) const
8b06beed 3546{
a1740ee1 3547 /* Look for a non-empty slot at stratum levels beneath T's. */
66b4deae 3548 for (int stratum = t->stratum () - 1; stratum >= 0; --stratum)
91e3d1d1
AB
3549 if (m_stack[stratum].get () != NULL)
3550 return m_stack[stratum].get ();
8b06beed
TT
3551
3552 return NULL;
3553}
3554
a1740ee1
PA
3555/* See target.h. */
3556
3557struct target_ops *
3558find_target_at (enum strata stratum)
3559{
5b6d1e4f 3560 return current_inferior ()->target_at (stratum);
a1740ee1
PA
3561}
3562
c906108c 3563\f
0f48b757
PA
3564
3565/* See target.h */
3566
3567void
3568target_announce_detach (int from_tty)
3569{
3570 pid_t pid;
a121b7c1 3571 const char *exec_file;
0f48b757
PA
3572
3573 if (!from_tty)
3574 return;
3575
e99b03dc 3576 pid = inferior_ptid.pid ();
0a70e1a8 3577 exec_file = current_program_space->exec_filename ();
41e321a8 3578 if (exec_file == nullptr)
6cb06a8c
TT
3579 gdb_printf ("Detaching from pid %s\n",
3580 target_pid_to_str (ptid_t (pid)).c_str ());
41e321a8 3581 else
5260dcf0
AB
3582 gdb_printf (_("Detaching from program: %ps, %s\n"),
3583 styled_string (file_name_style.style (), exec_file),
6cb06a8c 3584 target_pid_to_str (ptid_t (pid)).c_str ());
bc521517
TT
3585}
3586
3587/* See target.h */
3588
3589void
3590target_announce_attach (int from_tty, int pid)
3591{
3592 if (!from_tty)
3593 return;
3594
0a70e1a8 3595 const char *exec_file = current_program_space->exec_filename ();
bc521517 3596
249f1cf8 3597 if (exec_file != nullptr)
5260dcf0
AB
3598 gdb_printf ("Attaching to program: %ps, %s\n",
3599 styled_string (file_name_style.style (), exec_file),
6cb06a8c 3600 target_pid_to_str (ptid_t (pid)).c_str ());
bc521517 3601 else
6cb06a8c
TT
3602 gdb_printf ("Attaching to %s\n",
3603 target_pid_to_str (ptid_t (pid)).c_str ());
0f48b757
PA
3604}
3605
c906108c
SS
3606/* The inferior process has died. Long live the inferior! */
3607
3608void
fba45db2 3609generic_mourn_inferior (void)
c906108c 3610{
00431a78 3611 inferior *inf = current_inferior ();
c906108c 3612
a0776b13 3613 switch_to_no_thread ();
7f9f62ba 3614
f59f708a
PA
3615 /* Mark breakpoints uninserted in case something tries to delete a
3616 breakpoint while we delete the inferior's threads (which would
3617 fail, since the inferior is long gone). */
c72348e3 3618 mark_breakpoints_out (inf->pspace);
f59f708a 3619
00431a78
PA
3620 if (inf->pid != 0)
3621 exit_inferior (inf);
7f9f62ba 3622
f59f708a
PA
3623 /* Note this wipes step-resume breakpoints, so needs to be done
3624 after exit_inferior, which ends up referencing the step-resume
3625 breakpoints through clear_thread_inferior_resources. */
f5928702 3626 breakpoint_init_inferior (inf, inf_exited);
f59f708a 3627
c906108c
SS
3628 registers_changed ();
3629
c906108c
SS
3630 reopen_exec_file ();
3631 reinit_frame_cache ();
3632
9a4105ab
AC
3633 if (deprecated_detach_hook)
3634 deprecated_detach_hook ();
c906108c
SS
3635}
3636\f
fd0a2a6f
MK
3637/* Convert a normal process ID to a string. Returns the string in a
3638 static buffer. */
c906108c 3639
a068643d 3640std::string
39f77062 3641normal_pid_to_str (ptid_t ptid)
c906108c 3642{
a068643d 3643 return string_printf ("process %d", ptid.pid ());
c906108c
SS
3644}
3645
a068643d 3646static std::string
770234d3 3647default_pid_to_str (struct target_ops *ops, ptid_t ptid)
117de6a9
PA
3648{
3649 return normal_pid_to_str (ptid);
3650}
3651
9b4eba8e
HZ
3652/* Error-catcher for target_find_memory_regions. */
3653static int
2e73927c
TT
3654dummy_find_memory_regions (struct target_ops *self,
3655 find_memory_region_ftype ignore1, void *ignore2)
be4d1333 3656{
9b4eba8e 3657 error (_("Command not implemented for this target."));
be4d1333
MS
3658 return 0;
3659}
3660
9b4eba8e 3661/* Error-catcher for target_make_corefile_notes. */
24f5300a 3662static gdb::unique_xmalloc_ptr<char>
fc6691b2
TT
3663dummy_make_corefile_notes (struct target_ops *self,
3664 bfd *ignore1, int *ignore2)
be4d1333 3665{
9b4eba8e 3666 error (_("Command not implemented for this target."));
be4d1333
MS
3667 return NULL;
3668}
3669
f6ac5f3d
PA
3670#include "target-delegates.c"
3671
06b5b831
TT
3672/* The initial current target, so that there is always a semi-valid
3673 current target. */
3674
3675static dummy_target the_dummy_target;
c906108c 3676
5b6d1e4f
PA
3677/* See target.h. */
3678
3679target_ops *
3680get_dummy_target ()
3681{
3682 return &the_dummy_target;
3683}
3684
d9f719f1
PA
3685static const target_info dummy_target_info = {
3686 "None",
3687 N_("None"),
3688 ""
3689};
3690
66b4deae
PA
3691strata
3692dummy_target::stratum () const
f6ac5f3d 3693{
66b4deae 3694 return dummy_stratum;
f6ac5f3d
PA
3695}
3696
66b4deae
PA
3697strata
3698debug_target::stratum () const
f6ac5f3d 3699{
66b4deae 3700 return debug_stratum;
f6ac5f3d
PA
3701}
3702
d9f719f1
PA
3703const target_info &
3704dummy_target::info () const
f6ac5f3d 3705{
d9f719f1 3706 return dummy_target_info;
f6ac5f3d
PA
3707}
3708
d9f719f1
PA
3709const target_info &
3710debug_target::info () const
f6ac5f3d 3711{
b6a8c27b 3712 return beneath ()->info ();
f6ac5f3d
PA
3713}
3714
c906108c 3715\f
c906108c 3716
28439f5e
PA
3717int
3718target_thread_alive (ptid_t ptid)
c906108c 3719{
328d42d8 3720 return current_inferior ()->top_target ()->thread_alive (ptid);
28439f5e
PA
3721}
3722
3723void
e8032dde 3724target_update_thread_list (void)
28439f5e 3725{
328d42d8 3726 current_inferior ()->top_target ()->update_thread_list ();
c906108c
SS
3727}
3728
d914c394
SS
3729void
3730target_stop (ptid_t ptid)
3731{
1192f124
SM
3732 process_stratum_target *proc_target = current_inferior ()->process_target ();
3733
3734 gdb_assert (!proc_target->commit_resumed_state);
3735
d914c394
SS
3736 if (!may_stop)
3737 {
3738 warning (_("May not interrupt or stop the target, ignoring attempt"));
3739 return;
3740 }
3741
328d42d8 3742 current_inferior ()->top_target ()->stop (ptid);
d914c394
SS
3743}
3744
bfedc46a 3745void
e671cd59 3746target_interrupt ()
bfedc46a
PA
3747{
3748 if (!may_stop)
3749 {
3750 warning (_("May not interrupt or stop the target, ignoring attempt"));
3751 return;
3752 }
3753
328d42d8 3754 current_inferior ()->top_target ()->interrupt ();
bfedc46a
PA
3755}
3756
abc56d60
PA
3757/* See target.h. */
3758
93692b58
PA
3759void
3760target_pass_ctrlc (void)
3761{
5b6d1e4f
PA
3762 /* Pass the Ctrl-C to the first target that has a thread
3763 running. */
3764 for (inferior *inf : all_inferiors ())
3765 {
3766 target_ops *proc_target = inf->process_target ();
3767 if (proc_target == NULL)
3768 continue;
3769
43667cc6 3770 for (thread_info *thr : inf->non_exited_threads ())
5b6d1e4f
PA
3771 {
3772 /* A thread can be THREAD_STOPPED and executing, while
3773 running an infcall. */
611841bb 3774 if (thr->state == THREAD_RUNNING || thr->executing ())
5b6d1e4f
PA
3775 {
3776 /* We can get here quite deep in target layers. Avoid
3777 switching thread context or anything that would
3778 communicate with the target (e.g., to fetch
3779 registers), or flushing e.g., the frame cache. We
3780 just switch inferior in order to be able to call
3781 through the target_stack. */
3782 scoped_restore_current_inferior restore_inferior;
3783 set_current_inferior (inf);
328d42d8 3784 current_inferior ()->top_target ()->pass_ctrlc ();
5b6d1e4f
PA
3785 return;
3786 }
3787 }
3788 }
93692b58
PA
3789}
3790
3791/* See target.h. */
3792
3793void
3794default_target_pass_ctrlc (struct target_ops *ops)
3795{
e671cd59 3796 target_interrupt ();
93692b58
PA
3797}
3798
f8c1d06b
GB
3799/* See target/target.h. */
3800
3801void
03f4463b 3802target_stop_and_wait (ptid_t ptid)
f8c1d06b
GB
3803{
3804 struct target_waitstatus status;
491144b5 3805 bool was_non_stop = non_stop;
f8c1d06b 3806
491144b5 3807 non_stop = true;
f8c1d06b
GB
3808 target_stop (ptid);
3809
f8c1d06b
GB
3810 target_wait (ptid, &status, 0);
3811
3812 non_stop = was_non_stop;
3813}
3814
3815/* See target/target.h. */
3816
3817void
03f4463b 3818target_continue_no_signal (ptid_t ptid)
f8c1d06b
GB
3819{
3820 target_resume (ptid, 0, GDB_SIGNAL_0);
3821}
3822
049a8570
SDJ
3823/* See target/target.h. */
3824
3825void
3826target_continue (ptid_t ptid, enum gdb_signal signal)
3827{
3828 target_resume (ptid, 0, signal);
3829}
3830
fdbac7d8 3831/* Concatenate ELEM to LIST, a comma-separated list. */
09826ec5 3832
09ce46f2
SM
3833static void
3834str_comma_list_concat_elem (std::string *list, const char *elem)
09826ec5 3835{
09ce46f2
SM
3836 if (!list->empty ())
3837 list->append (", ");
3838
3839 list->append (elem);
09826ec5
PA
3840}
3841
3842/* Helper for target_options_to_string. If OPT is present in
3843 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
09ce46f2 3844 OPT is removed from TARGET_OPTIONS. */
09826ec5 3845
09ce46f2 3846static void
b60cea74
TT
3847do_option (target_wait_flags *target_options, std::string *ret,
3848 target_wait_flag opt, const char *opt_str)
09826ec5
PA
3849{
3850 if ((*target_options & opt) != 0)
3851 {
09ce46f2 3852 str_comma_list_concat_elem (ret, opt_str);
09826ec5
PA
3853 *target_options &= ~opt;
3854 }
09826ec5
PA
3855}
3856
fdbac7d8
SM
3857/* See target.h. */
3858
09ce46f2 3859std::string
b60cea74 3860target_options_to_string (target_wait_flags target_options)
09826ec5 3861{
09ce46f2 3862 std::string ret;
09826ec5
PA
3863
3864#define DO_TARG_OPTION(OPT) \
09ce46f2 3865 do_option (&target_options, &ret, OPT, #OPT)
09826ec5
PA
3866
3867 DO_TARG_OPTION (TARGET_WNOHANG);
3868
3869 if (target_options != 0)
09ce46f2 3870 str_comma_list_concat_elem (&ret, "unknown???");
09826ec5 3871
09826ec5
PA
3872 return ret;
3873}
3874
28439f5e
PA
3875void
3876target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3877{
328d42d8 3878 current_inferior ()->top_target ()->fetch_registers (regcache, regno);
cb5dfff8 3879 target_debug_printf ("%s", regcache->register_debug_string (regno).c_str ());
c906108c
SS
3880}
3881
28439f5e
PA
3882void
3883target_store_registers (struct regcache *regcache, int regno)
c906108c 3884{
d914c394
SS
3885 if (!may_write_registers)
3886 error (_("Writing to registers is not allowed (regno %d)"), regno);
3887
328d42d8 3888 current_inferior ()->top_target ()->store_registers (regcache, regno);
cb5dfff8 3889 target_debug_printf ("%s", regcache->register_debug_string (regno).c_str ());
c906108c
SS
3890}
3891
dc146f7c
VP
3892int
3893target_core_of_thread (ptid_t ptid)
3894{
328d42d8 3895 return current_inferior ()->top_target ()->core_of_thread (ptid);
dc146f7c
VP
3896}
3897
936d2992
PA
3898int
3899simple_verify_memory (struct target_ops *ops,
3900 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
3901{
3902 LONGEST total_xfered = 0;
3903
3904 while (total_xfered < size)
3905 {
3906 ULONGEST xfered_len;
3907 enum target_xfer_status status;
3908 gdb_byte buf[1024];
768adc05 3909 ULONGEST howmuch = std::min<ULONGEST> (sizeof (buf), size - total_xfered);
936d2992
PA
3910
3911 status = target_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
3912 buf, NULL, lma + total_xfered, howmuch,
3913 &xfered_len);
3914 if (status == TARGET_XFER_OK
3915 && memcmp (data + total_xfered, buf, xfered_len) == 0)
3916 {
3917 total_xfered += xfered_len;
3918 QUIT;
3919 }
3920 else
3921 return 0;
3922 }
3923 return 1;
3924}
3925
3926/* Default implementation of memory verification. */
3927
3928static int
3929default_verify_memory (struct target_ops *self,
3930 const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3931{
3932 /* Start over from the top of the target stack. */
328d42d8 3933 return simple_verify_memory (current_inferior ()->top_target (),
936d2992
PA
3934 data, memaddr, size);
3935}
3936
4a5e7a5b
PA
3937int
3938target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3939{
328d42d8
SM
3940 target_ops *target = current_inferior ()->top_target ();
3941
3942 return target->verify_memory (data, memaddr, size);
4a5e7a5b
PA
3943}
3944
9c06b0b4
TJB
3945/* The documentation for this function is in its prototype declaration in
3946 target.h. */
3947
3948int
f4b0a671
SM
3949target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
3950 enum target_hw_bp_type rw)
9c06b0b4 3951{
328d42d8
SM
3952 target_ops *target = current_inferior ()->top_target ();
3953
3954 return target->insert_mask_watchpoint (addr, mask, rw);
9c06b0b4
TJB
3955}
3956
3957/* The documentation for this function is in its prototype declaration in
3958 target.h. */
3959
3960int
f4b0a671
SM
3961target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
3962 enum target_hw_bp_type rw)
9c06b0b4 3963{
328d42d8
SM
3964 target_ops *target = current_inferior ()->top_target ();
3965
3966 return target->remove_mask_watchpoint (addr, mask, rw);
9c06b0b4
TJB
3967}
3968
3969/* The documentation for this function is in its prototype declaration
3970 in target.h. */
3971
3972int
3973target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
3974{
328d42d8
SM
3975 target_ops *target = current_inferior ()->top_target ();
3976
3977 return target->masked_watch_num_registers (addr, mask);
9c06b0b4
TJB
3978}
3979
f1310107
TJB
3980/* The documentation for this function is in its prototype declaration
3981 in target.h. */
3982
3983int
3984target_ranged_break_num_registers (void)
3985{
328d42d8 3986 return current_inferior ()->top_target ()->ranged_break_num_registers ();
f1310107
TJB
3987}
3988
02d27625
MM
3989/* See target.h. */
3990
02d27625 3991struct btrace_target_info *
696c0d5e 3992target_enable_btrace (thread_info *tp, const struct btrace_config *conf)
02d27625 3993{
696c0d5e 3994 return current_inferior ()->top_target ()->enable_btrace (tp, conf);
02d27625
MM
3995}
3996
3997/* See target.h. */
3998
3999void
4000target_disable_btrace (struct btrace_target_info *btinfo)
4001{
328d42d8 4002 current_inferior ()->top_target ()->disable_btrace (btinfo);
02d27625
MM
4003}
4004
4005/* See target.h. */
4006
4007void
4008target_teardown_btrace (struct btrace_target_info *btinfo)
4009{
328d42d8 4010 current_inferior ()->top_target ()->teardown_btrace (btinfo);
02d27625
MM
4011}
4012
4013/* See target.h. */
4014
969c39fb 4015enum btrace_error
734b0e4b 4016target_read_btrace (struct btrace_data *btrace,
969c39fb 4017 struct btrace_target_info *btinfo,
02d27625
MM
4018 enum btrace_read_type type)
4019{
328d42d8
SM
4020 target_ops *target = current_inferior ()->top_target ();
4021
4022 return target->read_btrace (btrace, btinfo, type);
02d27625
MM
4023}
4024
d02ed0bb
MM
4025/* See target.h. */
4026
f4abbc16
MM
4027const struct btrace_config *
4028target_btrace_conf (const struct btrace_target_info *btinfo)
4029{
328d42d8 4030 return current_inferior ()->top_target ()->btrace_conf (btinfo);
f4abbc16
MM
4031}
4032
4033/* See target.h. */
4034
7c1687a9
MM
4035void
4036target_stop_recording (void)
4037{
328d42d8 4038 current_inferior ()->top_target ()->stop_recording ();
7c1687a9
MM
4039}
4040
4041/* See target.h. */
4042
d02ed0bb 4043void
85e1311a 4044target_save_record (const char *filename)
d02ed0bb 4045{
328d42d8 4046 current_inferior ()->top_target ()->save_record (filename);
d02ed0bb
MM
4047}
4048
4049/* See target.h. */
4050
4051int
f6ac5f3d 4052target_supports_delete_record ()
d02ed0bb 4053{
328d42d8 4054 return current_inferior ()->top_target ()->supports_delete_record ();
d02ed0bb
MM
4055}
4056
4057/* See target.h. */
4058
4059void
4060target_delete_record (void)
4061{
328d42d8 4062 current_inferior ()->top_target ()->delete_record ();
d02ed0bb
MM
4063}
4064
4065/* See target.h. */
4066
b158a20f
TW
4067enum record_method
4068target_record_method (ptid_t ptid)
4069{
328d42d8 4070 return current_inferior ()->top_target ()->record_method (ptid);
b158a20f
TW
4071}
4072
4073/* See target.h. */
4074
d02ed0bb 4075int
a52eab48 4076target_record_is_replaying (ptid_t ptid)
d02ed0bb 4077{
328d42d8 4078 return current_inferior ()->top_target ()->record_is_replaying (ptid);
d02ed0bb
MM
4079}
4080
4081/* See target.h. */
4082
7ff27e9b
MM
4083int
4084target_record_will_replay (ptid_t ptid, int dir)
4085{
328d42d8 4086 return current_inferior ()->top_target ()->record_will_replay (ptid, dir);
7ff27e9b
MM
4087}
4088
4089/* See target.h. */
4090
797094dd
MM
4091void
4092target_record_stop_replaying (void)
4093{
328d42d8 4094 current_inferior ()->top_target ()->record_stop_replaying ();
797094dd
MM
4095}
4096
4097/* See target.h. */
4098
d02ed0bb
MM
4099void
4100target_goto_record_begin (void)
4101{
328d42d8 4102 current_inferior ()->top_target ()->goto_record_begin ();
d02ed0bb
MM
4103}
4104
4105/* See target.h. */
4106
4107void
4108target_goto_record_end (void)
4109{
328d42d8 4110 current_inferior ()->top_target ()->goto_record_end ();
d02ed0bb
MM
4111}
4112
4113/* See target.h. */
4114
4115void
4116target_goto_record (ULONGEST insn)
4117{
328d42d8 4118 current_inferior ()->top_target ()->goto_record (insn);
d02ed0bb
MM
4119}
4120
67c86d06
MM
4121/* See target.h. */
4122
4123void
9a24775b 4124target_insn_history (int size, gdb_disassembly_flags flags)
67c86d06 4125{
328d42d8 4126 current_inferior ()->top_target ()->insn_history (size, flags);
67c86d06
MM
4127}
4128
4129/* See target.h. */
4130
4131void
9a24775b
PA
4132target_insn_history_from (ULONGEST from, int size,
4133 gdb_disassembly_flags flags)
67c86d06 4134{
328d42d8 4135 current_inferior ()->top_target ()->insn_history_from (from, size, flags);
67c86d06
MM
4136}
4137
4138/* See target.h. */
4139
4140void
9a24775b
PA
4141target_insn_history_range (ULONGEST begin, ULONGEST end,
4142 gdb_disassembly_flags flags)
67c86d06 4143{
328d42d8 4144 current_inferior ()->top_target ()->insn_history_range (begin, end, flags);
67c86d06
MM
4145}
4146
15984c13
MM
4147/* See target.h. */
4148
4149void
0cb7c7b0 4150target_call_history (int size, record_print_flags flags)
15984c13 4151{
328d42d8 4152 current_inferior ()->top_target ()->call_history (size, flags);
15984c13
MM
4153}
4154
4155/* See target.h. */
4156
4157void
0cb7c7b0 4158target_call_history_from (ULONGEST begin, int size, record_print_flags flags)
15984c13 4159{
328d42d8 4160 current_inferior ()->top_target ()->call_history_from (begin, size, flags);
15984c13
MM
4161}
4162
4163/* See target.h. */
4164
4165void
0cb7c7b0 4166target_call_history_range (ULONGEST begin, ULONGEST end, record_print_flags flags)
15984c13 4167{
328d42d8 4168 current_inferior ()->top_target ()->call_history_range (begin, end, flags);
15984c13
MM
4169}
4170
ea001bdc
MM
4171/* See target.h. */
4172
4173const struct frame_unwind *
4174target_get_unwinder (void)
4175{
328d42d8 4176 return current_inferior ()->top_target ()->get_unwinder ();
ea001bdc
MM
4177}
4178
4179/* See target.h. */
4180
4181const struct frame_unwind *
4182target_get_tailcall_unwinder (void)
4183{
328d42d8 4184 return current_inferior ()->top_target ()->get_tailcall_unwinder ();
ea001bdc
MM
4185}
4186
5fff78c4
MM
4187/* See target.h. */
4188
4189void
4190target_prepare_to_generate_core (void)
4191{
328d42d8 4192 current_inferior ()->top_target ()->prepare_to_generate_core ();
5fff78c4
MM
4193}
4194
4195/* See target.h. */
4196
4197void
4198target_done_generating_core (void)
4199{
328d42d8 4200 current_inferior ()->top_target ()->done_generating_core ();
5fff78c4
MM
4201}
4202
c906108c 4203\f
c5aa993b
JM
4204
4205static char targ_desc[] =
3e43a32a
MS
4206"Names of targets and files being debugged.\nShows the entire \
4207stack of targets currently in use (including the exec-file,\n\
c906108c
SS
4208core-file, and process, if any), as well as the symbol file name.";
4209
a53f3625 4210static void
a30bf1f1
TT
4211default_rcmd (struct target_ops *self, const char *command,
4212 struct ui_file *output)
a53f3625
TT
4213{
4214 error (_("\"monitor\" command not supported by this target."));
4215}
4216
96baa820 4217static void
0b39b52e 4218do_monitor_command (const char *cmd, int from_tty)
96baa820 4219{
96baa820
JM
4220 target_rcmd (cmd, gdb_stdtarg);
4221}
4222
78cbbba8
LM
4223/* Erases all the memory regions marked as flash. CMD and FROM_TTY are
4224 ignored. */
4225
4226void
0b39b52e 4227flash_erase_command (const char *cmd, int from_tty)
78cbbba8
LM
4228{
4229 /* Used to communicate termination of flash operations to the target. */
4230 bool found_flash_region = false;
99d9c3b9 4231 gdbarch *gdbarch = current_inferior ()->arch ();
78cbbba8 4232
a664f67e 4233 std::vector<mem_region> mem_regions = target_memory_map ();
78cbbba8
LM
4234
4235 /* Iterate over all memory regions. */
a664f67e 4236 for (const mem_region &m : mem_regions)
78cbbba8 4237 {
78cbbba8 4238 /* Is this a flash memory region? */
a664f67e 4239 if (m.attrib.mode == MEM_FLASH)
dda83cd7
SM
4240 {
4241 found_flash_region = true;
4242 target_flash_erase (m.lo, m.hi - m.lo);
78cbbba8 4243
76f9c9cf 4244 ui_out_emit_tuple tuple_emitter (current_uiout, "erased-regions");
78cbbba8 4245
dda83cd7
SM
4246 current_uiout->message (_("Erasing flash memory region at address "));
4247 current_uiout->field_core_addr ("address", gdbarch, m.lo);
4248 current_uiout->message (", size = ");
4249 current_uiout->field_string ("size", hex_string (m.hi - m.lo));
4250 current_uiout->message ("\n");
4251 }
78cbbba8
LM
4252 }
4253
4254 /* Did we do any flash operations? If so, we need to finalize them. */
4255 if (found_flash_region)
4256 target_flash_done ();
4257 else
4258 current_uiout->message (_("No flash memory regions found.\n"));
4259}
4260
87680a14
JB
4261/* Print the name of each layers of our target stack. */
4262
4263static void
d3cb6b99 4264maintenance_print_target_stack (const char *cmd, int from_tty)
87680a14 4265{
6cb06a8c 4266 gdb_printf (_("The current target stack is:\n"));
87680a14 4267
328d42d8
SM
4268 for (target_ops *t = current_inferior ()->top_target ();
4269 t != NULL;
4270 t = t->beneath ())
87680a14 4271 {
66b4deae 4272 if (t->stratum () == debug_stratum)
f6ac5f3d 4273 continue;
6cb06a8c 4274 gdb_printf (" - %s (%s)\n", t->shortname (), t->longname ());
87680a14
JB
4275 }
4276}
4277
372316f1
PA
4278/* See target.h. */
4279
4280void
4a570176 4281target_async (bool enable)
372316f1 4282{
bf94662b
AB
4283 /* If we are trying to enable async mode then it must be the case that
4284 async mode is possible for this target. */
4285 gdb_assert (!enable || target_can_async_p ());
372316f1 4286 infrun_async (enable);
328d42d8 4287 current_inferior ()->top_target ()->async (enable);
372316f1
PA
4288}
4289
65706a29
PA
4290/* See target.h. */
4291
4292void
2db17c87 4293target_thread_events (bool enable)
65706a29 4294{
328d42d8 4295 current_inferior ()->top_target ()->thread_events (enable);
65706a29
PA
4296}
4297
65c459ab
PA
4298/* See target.h. */
4299
4300bool
4301target_supports_set_thread_options (gdb_thread_options options)
4302{
4303 inferior *inf = current_inferior ();
4304 return inf->top_target ()->supports_set_thread_options (options);
4305}
4306
329ea579
PA
4307/* Controls if targets can report that they can/are async. This is
4308 just for maintainers to use when debugging gdb. */
491144b5 4309bool target_async_permitted = true;
c6ebd6cf 4310
c6ebd6cf 4311static void
fdf1350d 4312set_maint_target_async (bool permitted)
c6ebd6cf 4313{
c35b1492 4314 if (have_live_inferiors ())
fdf1350d 4315 error (_("Cannot change this setting while the inferior is running."));
c6ebd6cf 4316
fdf1350d
SM
4317 target_async_permitted = permitted;
4318}
4319
4320static bool
4321get_maint_target_async ()
4322{
4323 return target_async_permitted;
c6ebd6cf
VP
4324}
4325
4326static void
fdf1350d
SM
4327show_maint_target_async (ui_file *file, int from_tty,
4328 cmd_list_element *c, const char *value)
c6ebd6cf 4329{
6cb06a8c
TT
4330 gdb_printf (file,
4331 _("Controlling the inferior in "
4332 "asynchronous mode is %s.\n"), value);
c6ebd6cf
VP
4333}
4334
fbea99ea
PA
4335/* Return true if the target operates in non-stop mode even with "set
4336 non-stop off". */
4337
4338static int
4339target_always_non_stop_p (void)
4340{
328d42d8 4341 return current_inferior ()->top_target ()->always_non_stop_p ();
fbea99ea
PA
4342}
4343
4344/* See target.h. */
4345
6ff267e1
SM
4346bool
4347target_is_non_stop_p ()
fbea99ea 4348{
e5b9b39f
PA
4349 return ((non_stop
4350 || target_non_stop_enabled == AUTO_BOOLEAN_TRUE
4351 || (target_non_stop_enabled == AUTO_BOOLEAN_AUTO
4352 && target_always_non_stop_p ()))
4353 && target_can_async_p ());
fbea99ea
PA
4354}
4355
a0714d30
TBA
4356/* See target.h. */
4357
4358bool
4359exists_non_stop_target ()
4360{
4361 if (target_is_non_stop_p ())
4362 return true;
4363
4364 scoped_restore_current_thread restore_thread;
4365
4366 for (inferior *inf : all_inferiors ())
4367 {
4368 switch_to_inferior_no_thread (inf);
4369 if (target_is_non_stop_p ())
4370 return true;
4371 }
4372
4373 return false;
4374}
4375
fbea99ea
PA
4376/* Controls if targets can report that they always run in non-stop
4377 mode. This is just for maintainers to use when debugging gdb. */
4378enum auto_boolean target_non_stop_enabled = AUTO_BOOLEAN_AUTO;
4379
fdf1350d 4380/* Set callback for maint target-non-stop setting. */
fbea99ea
PA
4381
4382static void
fdf1350d 4383set_maint_target_non_stop (auto_boolean enabled)
fbea99ea
PA
4384{
4385 if (have_live_inferiors ())
fdf1350d 4386 error (_("Cannot change this setting while the inferior is running."));
fbea99ea 4387
fdf1350d 4388 target_non_stop_enabled = enabled;
fbea99ea
PA
4389}
4390
fdf1350d
SM
4391/* Get callback for maint target-non-stop setting. */
4392
4393static auto_boolean
4394get_maint_target_non_stop ()
4395{
4396 return target_non_stop_enabled;
4397}
fbea99ea
PA
4398
4399static void
fdf1350d
SM
4400show_maint_target_non_stop (ui_file *file, int from_tty,
4401 cmd_list_element *c, const char *value)
fbea99ea
PA
4402{
4403 if (target_non_stop_enabled == AUTO_BOOLEAN_AUTO)
6cb06a8c
TT
4404 gdb_printf (file,
4405 _("Whether the target is always in non-stop mode "
4406 "is %s (currently %s).\n"), value,
4407 target_always_non_stop_p () ? "on" : "off");
fbea99ea 4408 else
6cb06a8c
TT
4409 gdb_printf (file,
4410 _("Whether the target is always in non-stop mode "
4411 "is %s.\n"), value);
fbea99ea
PA
4412}
4413
d914c394
SS
4414/* Temporary copies of permission settings. */
4415
491144b5
CB
4416static bool may_write_registers_1 = true;
4417static bool may_write_memory_1 = true;
4418static bool may_insert_breakpoints_1 = true;
4419static bool may_insert_tracepoints_1 = true;
4420static bool may_insert_fast_tracepoints_1 = true;
4421static bool may_stop_1 = true;
d914c394
SS
4422
4423/* Make the user-set values match the real values again. */
4424
4425void
4426update_target_permissions (void)
4427{
4428 may_write_registers_1 = may_write_registers;
4429 may_write_memory_1 = may_write_memory;
4430 may_insert_breakpoints_1 = may_insert_breakpoints;
4431 may_insert_tracepoints_1 = may_insert_tracepoints;
4432 may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
4433 may_stop_1 = may_stop;
4434}
4435
4436/* The one function handles (most of) the permission flags in the same
4437 way. */
4438
4439static void
eb4c3f4a 4440set_target_permissions (const char *args, int from_tty,
d914c394
SS
4441 struct cmd_list_element *c)
4442{
55f6301a 4443 if (target_has_execution ())
d914c394
SS
4444 {
4445 update_target_permissions ();
4446 error (_("Cannot change this setting while the inferior is running."));
4447 }
4448
4449 /* Make the real values match the user-changed values. */
d914c394
SS
4450 may_insert_breakpoints = may_insert_breakpoints_1;
4451 may_insert_tracepoints = may_insert_tracepoints_1;
4452 may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
4453 may_stop = may_stop_1;
4454 update_observer_mode ();
4455}
4456
d8a001f5 4457/* Set some permissions independently of observer mode. */
d914c394
SS
4458
4459static void
d8a001f5
TT
4460set_write_memory_registers_permission (const char *args, int from_tty,
4461 struct cmd_list_element *c)
d914c394
SS
4462{
4463 /* Make the real values match the user-changed values. */
4464 may_write_memory = may_write_memory_1;
d8a001f5 4465 may_write_registers = may_write_registers_1;
d914c394
SS
4466 update_observer_mode ();
4467}
4468
6c265988
SM
4469void _initialize_target ();
4470
c906108c 4471void
5b6d1e4f 4472_initialize_target ()
c906108c 4473{
f6ac5f3d 4474 the_debug_target = new debug_target ();
c906108c 4475
11db9430
SM
4476 add_info ("target", info_target_command, targ_desc);
4477 add_info ("files", info_target_command, targ_desc);
c906108c 4478
ccce17b0 4479 add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
85c07804
AC
4480Set target debugging."), _("\
4481Show target debugging."), _("\
333dabeb 4482When non-zero, target debugging is enabled. Higher numbers are more\n\
3cecbbbe
TT
4483verbose."),
4484 set_targetdebug,
ccce17b0
YQ
4485 show_targetdebug,
4486 &setdebuglist, &showdebuglist);
3a11626d 4487
2bc416ba 4488 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
4489 &trust_readonly, _("\
4490Set mode for reading from readonly sections."), _("\
4491Show mode for reading from readonly sections."), _("\
3a11626d
MS
4492When this mode is on, memory reads from readonly sections (such as .text)\n\
4493will be read from the object file instead of from the target. This will\n\
7915a72c 4494result in significant performance improvement for remote targets."),
2c5b56ce 4495 NULL,
920d2a44 4496 show_trust_readonly,
e707bbc2 4497 &setlist, &showlist);
96baa820
JM
4498
4499 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 4500 _("Send a command to the remote monitor (remote targets only)."));
96baa820 4501
87680a14 4502 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
dda83cd7
SM
4503 _("Print the name of each layer of the internal target stack."),
4504 &maintenanceprintlist);
87680a14 4505
c6ebd6cf 4506 add_setshow_boolean_cmd ("target-async", no_class,
fdf1350d 4507 _("\
c6ebd6cf
VP
4508Set whether gdb controls the inferior in asynchronous mode."), _("\
4509Show whether gdb controls the inferior in asynchronous mode."), _("\
4510Tells gdb whether to control the inferior in asynchronous mode."),
fdf1350d
SM
4511 set_maint_target_async,
4512 get_maint_target_async,
4513 show_maint_target_async,
329ea579
PA
4514 &maintenance_set_cmdlist,
4515 &maintenance_show_cmdlist);
c6ebd6cf 4516
fbea99ea 4517 add_setshow_auto_boolean_cmd ("target-non-stop", no_class,
fdf1350d 4518 _("\
fbea99ea
PA
4519Set whether gdb always controls the inferior in non-stop mode."), _("\
4520Show whether gdb always controls the inferior in non-stop mode."), _("\
4521Tells gdb whether to control the inferior in non-stop mode."),
fdf1350d
SM
4522 set_maint_target_non_stop,
4523 get_maint_target_non_stop,
4524 show_maint_target_non_stop,
fbea99ea
PA
4525 &maintenance_set_cmdlist,
4526 &maintenance_show_cmdlist);
4527
d914c394
SS
4528 add_setshow_boolean_cmd ("may-write-registers", class_support,
4529 &may_write_registers_1, _("\
4530Set permission to write into registers."), _("\
4531Show permission to write into registers."), _("\
4532When this permission is on, GDB may write into the target's registers.\n\
4533Otherwise, any sort of write attempt will result in an error."),
d8a001f5 4534 set_write_memory_registers_permission, NULL,
d914c394
SS
4535 &setlist, &showlist);
4536
4537 add_setshow_boolean_cmd ("may-write-memory", class_support,
4538 &may_write_memory_1, _("\
4539Set permission to write into target memory."), _("\
4540Show permission to write into target memory."), _("\
4541When this permission is on, GDB may write into the target's memory.\n\
4542Otherwise, any sort of write attempt will result in an error."),
d8a001f5 4543 set_write_memory_registers_permission, NULL,
d914c394
SS
4544 &setlist, &showlist);
4545
4546 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
4547 &may_insert_breakpoints_1, _("\
4548Set permission to insert breakpoints in the target."), _("\
4549Show permission to insert breakpoints in the target."), _("\
4550When this permission is on, GDB may insert breakpoints in the program.\n\
4551Otherwise, any sort of insertion attempt will result in an error."),
4552 set_target_permissions, NULL,
4553 &setlist, &showlist);
4554
4555 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
4556 &may_insert_tracepoints_1, _("\
4557Set permission to insert tracepoints in the target."), _("\
4558Show permission to insert tracepoints in the target."), _("\
4559When this permission is on, GDB may insert tracepoints in the program.\n\
4560Otherwise, any sort of insertion attempt will result in an error."),
4561 set_target_permissions, NULL,
4562 &setlist, &showlist);
4563
4564 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
4565 &may_insert_fast_tracepoints_1, _("\
4566Set permission to insert fast tracepoints in the target."), _("\
4567Show permission to insert fast tracepoints in the target."), _("\
4568When this permission is on, GDB may insert fast tracepoints.\n\
4569Otherwise, any sort of insertion attempt will result in an error."),
4570 set_target_permissions, NULL,
4571 &setlist, &showlist);
4572
4573 add_setshow_boolean_cmd ("may-interrupt", class_support,
4574 &may_stop_1, _("\
4575Set permission to interrupt or signal the target."), _("\
4576Show permission to interrupt or signal the target."), _("\
4577When this permission is on, GDB may interrupt/stop the target's execution.\n\
4578Otherwise, any attempt to interrupt or stop will be ignored."),
4579 set_target_permissions, NULL,
4580 &setlist, &showlist);
6a3cb8e8 4581
78cbbba8 4582 add_com ("flash-erase", no_class, flash_erase_command,
dda83cd7 4583 _("Erase all flash memory regions."));
78cbbba8 4584
6a3cb8e8
PA
4585 add_setshow_boolean_cmd ("auto-connect-native-target", class_support,
4586 &auto_connect_native_target, _("\
4587Set whether GDB may automatically connect to the native target."), _("\
4588Show whether GDB may automatically connect to the native target."), _("\
4589When on, and GDB is not connected to a target yet, GDB\n\
4590attempts \"run\" and other commands with the native target."),
4591 NULL, show_auto_connect_native_target,
4592 &setlist, &showlist);
c906108c 4593}