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