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