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