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