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