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