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