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