convert to_follow_fork
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3 2
ecd75fc8 3 Copyright (C) 1990-2014 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"
23#include <errno.h>
0e9f083f 24#include <string.h>
c906108c 25#include "target.h"
68c765e2 26#include "target-dcache.h"
c906108c
SS
27#include "gdbcmd.h"
28#include "symtab.h"
29#include "inferior.h"
30#include "bfd.h"
31#include "symfile.h"
32#include "objfiles.h"
4930751a 33#include "dcache.h"
c906108c 34#include <signal.h>
4e052eda 35#include "regcache.h"
0088c768 36#include "gdb_assert.h"
b6591e8b 37#include "gdbcore.h"
9e35dae4 38#include "exceptions.h"
424163ea 39#include "target-descriptions.h"
e1ac3328 40#include "gdbthread.h"
b9db4ced 41#include "solib.h"
07b82ea5 42#include "exec.h"
edb3359d 43#include "inline-frame.h"
2f4d8875 44#include "tracepoint.h"
7313baad 45#include "gdb/fileio.h"
8ffcbaaf 46#include "agent.h"
c906108c 47
a14ed312 48static void target_info (char *, int);
c906108c 49
0a4f40a2 50static void default_terminal_info (struct target_ops *, const char *, int);
c906108c 51
5009afc5
AS
52static int default_watchpoint_addr_within_range (struct target_ops *,
53 CORE_ADDR, CORE_ADDR, int);
54
31568a15
TT
55static int default_region_ok_for_hw_watchpoint (struct target_ops *,
56 CORE_ADDR, int);
e0d24f8d 57
a53f3625
TT
58static void default_rcmd (struct target_ops *, char *, struct ui_file *);
59
4229b31d
TT
60static ptid_t default_get_ada_task_ptid (struct target_ops *self,
61 long lwp, long tid);
62
098dba18
TT
63static int default_follow_fork (struct target_ops *self, int follow_child,
64 int detach_fork);
65
c25c4a8b 66static void tcomplain (void) ATTRIBUTE_NORETURN;
c906108c 67
a14ed312 68static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 69
a14ed312 70static int return_zero (void);
c906108c 71
a14ed312 72void target_ignore (void);
c906108c 73
a14ed312 74static void target_command (char *, int);
c906108c 75
a14ed312 76static struct target_ops *find_default_run_target (char *);
c906108c 77
4ac248ca
YQ
78static target_xfer_partial_ftype default_xfer_partial;
79
c2250ad1
UW
80static struct gdbarch *default_thread_architecture (struct target_ops *ops,
81 ptid_t ptid);
82
0b5a2719
TT
83static int dummy_find_memory_regions (struct target_ops *self,
84 find_memory_region_ftype ignore1,
85 void *ignore2);
86
16f796b1
TT
87static char *dummy_make_corefile_notes (struct target_ops *self,
88 bfd *ignore1, int *ignore2);
89
6b84065d
TT
90static int find_default_can_async_p (struct target_ops *ignore);
91
92static int find_default_is_async_p (struct target_ops *ignore);
93
fe31bf5b
TT
94static enum exec_direction_kind default_execution_direction
95 (struct target_ops *self);
96
1101cb7b
TT
97#include "target-delegates.c"
98
a14ed312 99static void init_dummy_target (void);
c906108c 100
aa869812
AC
101static struct target_ops debug_target;
102
a14ed312 103static void debug_to_open (char *, int);
c906108c 104
f32dbf8c
MM
105static void debug_to_prepare_to_store (struct target_ops *self,
106 struct regcache *);
c906108c 107
a14ed312 108static void debug_to_files_info (struct target_ops *);
c906108c 109
3db08215 110static int debug_to_insert_breakpoint (struct target_ops *, struct gdbarch *,
a6d9a66e 111 struct bp_target_info *);
c906108c 112
3db08215 113static int debug_to_remove_breakpoint (struct target_ops *, struct gdbarch *,
a6d9a66e 114 struct bp_target_info *);
c906108c 115
5461485a
TT
116static int debug_to_can_use_hw_breakpoint (struct target_ops *self,
117 int, int, int);
ccaa32c7 118
23a26771
TT
119static int debug_to_insert_hw_breakpoint (struct target_ops *self,
120 struct gdbarch *,
a6d9a66e 121 struct bp_target_info *);
ccaa32c7 122
a64dc96c
TT
123static int debug_to_remove_hw_breakpoint (struct target_ops *self,
124 struct gdbarch *,
a6d9a66e 125 struct bp_target_info *);
ccaa32c7 126
7bb99c53
TT
127static int debug_to_insert_watchpoint (struct target_ops *self,
128 CORE_ADDR, int, int,
0cf6dd15 129 struct expression *);
ccaa32c7 130
11b5219a
TT
131static int debug_to_remove_watchpoint (struct target_ops *self,
132 CORE_ADDR, int, int,
0cf6dd15 133 struct expression *);
ccaa32c7 134
4aa7a7f5 135static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
ccaa32c7 136
5009afc5
AS
137static int debug_to_watchpoint_addr_within_range (struct target_ops *,
138 CORE_ADDR, CORE_ADDR, int);
139
31568a15
TT
140static int debug_to_region_ok_for_hw_watchpoint (struct target_ops *self,
141 CORE_ADDR, int);
e0d24f8d 142
c3a5ff89
TT
143static int debug_to_can_accel_watchpoint_condition (struct target_ops *self,
144 CORE_ADDR, int, int,
0cf6dd15
TJB
145 struct expression *);
146
c42bf286 147static void debug_to_terminal_init (struct target_ops *self);
c906108c 148
d2f640d4 149static void debug_to_terminal_inferior (struct target_ops *self);
c906108c 150
2e1e1a19 151static void debug_to_terminal_ours_for_output (struct target_ops *self);
c906108c 152
ae3bd431 153static void debug_to_terminal_save_ours (struct target_ops *self);
a790ad35 154
e3594fd1 155static void debug_to_terminal_ours (struct target_ops *self);
c906108c 156
71a9f134 157static void debug_to_load (struct target_ops *self, char *, int);
c906108c 158
da82bd6b 159static int debug_to_can_run (struct target_ops *self);
c906108c 160
1eab8a48 161static void debug_to_stop (struct target_ops *self, ptid_t);
c906108c 162
c906108c 163/* Pointer to array of target architecture structures; the size of the
2bc416ba 164 array; the current index into the array; the allocated size of the
c906108c
SS
165 array. */
166struct target_ops **target_structs;
167unsigned target_struct_size;
c906108c
SS
168unsigned target_struct_allocsize;
169#define DEFAULT_ALLOCSIZE 10
170
171/* The initial current target, so that there is always a semi-valid
172 current target. */
173
174static struct target_ops dummy_target;
175
176/* Top of target stack. */
177
258b763a 178static struct target_ops *target_stack;
c906108c
SS
179
180/* The target structure we are currently using to talk to a process
181 or file or whatever "inferior" we have. */
182
183struct target_ops current_target;
184
185/* Command list for target. */
186
187static struct cmd_list_element *targetlist = NULL;
188
cf7a04e8
DJ
189/* Nonzero if we should trust readonly sections from the
190 executable when reading memory. */
191
192static int trust_readonly = 0;
193
8defab1a
DJ
194/* Nonzero if we should show true memory content including
195 memory breakpoint inserted by gdb. */
196
197static int show_memory_breakpoints = 0;
198
d914c394
SS
199/* These globals control whether GDB attempts to perform these
200 operations; they are useful for targets that need to prevent
201 inadvertant disruption, such as in non-stop mode. */
202
203int may_write_registers = 1;
204
205int may_write_memory = 1;
206
207int may_insert_breakpoints = 1;
208
209int may_insert_tracepoints = 1;
210
211int may_insert_fast_tracepoints = 1;
212
213int may_stop = 1;
214
c906108c
SS
215/* Non-zero if we want to see trace of target level stuff. */
216
ccce17b0 217static unsigned int targetdebug = 0;
920d2a44
AC
218static void
219show_targetdebug (struct ui_file *file, int from_tty,
220 struct cmd_list_element *c, const char *value)
221{
222 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
223}
c906108c 224
a14ed312 225static void setup_target_debug (void);
c906108c 226
c906108c
SS
227/* The user just typed 'target' without the name of a target. */
228
c906108c 229static void
fba45db2 230target_command (char *arg, int from_tty)
c906108c
SS
231{
232 fputs_filtered ("Argument required (target name). Try `help target'\n",
233 gdb_stdout);
234}
235
c35b1492
PA
236/* Default target_has_* methods for process_stratum targets. */
237
238int
239default_child_has_all_memory (struct target_ops *ops)
240{
241 /* If no inferior selected, then we can't read memory here. */
242 if (ptid_equal (inferior_ptid, null_ptid))
243 return 0;
244
245 return 1;
246}
247
248int
249default_child_has_memory (struct target_ops *ops)
250{
251 /* If no inferior selected, then we can't read memory here. */
252 if (ptid_equal (inferior_ptid, null_ptid))
253 return 0;
254
255 return 1;
256}
257
258int
259default_child_has_stack (struct target_ops *ops)
260{
261 /* If no inferior selected, there's no stack. */
262 if (ptid_equal (inferior_ptid, null_ptid))
263 return 0;
264
265 return 1;
266}
267
268int
269default_child_has_registers (struct target_ops *ops)
270{
271 /* Can't read registers from no inferior. */
272 if (ptid_equal (inferior_ptid, null_ptid))
273 return 0;
274
275 return 1;
276}
277
278int
aeaec162 279default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
c35b1492
PA
280{
281 /* If there's no thread selected, then we can't make it run through
282 hoops. */
aeaec162 283 if (ptid_equal (the_ptid, null_ptid))
c35b1492
PA
284 return 0;
285
286 return 1;
287}
288
289
290int
291target_has_all_memory_1 (void)
292{
293 struct target_ops *t;
294
295 for (t = current_target.beneath; t != NULL; t = t->beneath)
296 if (t->to_has_all_memory (t))
297 return 1;
298
299 return 0;
300}
301
302int
303target_has_memory_1 (void)
304{
305 struct target_ops *t;
306
307 for (t = current_target.beneath; t != NULL; t = t->beneath)
308 if (t->to_has_memory (t))
309 return 1;
310
311 return 0;
312}
313
314int
315target_has_stack_1 (void)
316{
317 struct target_ops *t;
318
319 for (t = current_target.beneath; t != NULL; t = t->beneath)
320 if (t->to_has_stack (t))
321 return 1;
322
323 return 0;
324}
325
326int
327target_has_registers_1 (void)
328{
329 struct target_ops *t;
330
331 for (t = current_target.beneath; t != NULL; t = t->beneath)
332 if (t->to_has_registers (t))
333 return 1;
334
335 return 0;
336}
337
338int
aeaec162 339target_has_execution_1 (ptid_t the_ptid)
c35b1492
PA
340{
341 struct target_ops *t;
342
343 for (t = current_target.beneath; t != NULL; t = t->beneath)
aeaec162 344 if (t->to_has_execution (t, the_ptid))
c35b1492
PA
345 return 1;
346
347 return 0;
348}
349
aeaec162
TT
350int
351target_has_execution_current (void)
352{
353 return target_has_execution_1 (inferior_ptid);
354}
355
c22a2b88
TT
356/* Complete initialization of T. This ensures that various fields in
357 T are set, if needed by the target implementation. */
c906108c
SS
358
359void
c22a2b88 360complete_target_initialization (struct target_ops *t)
c906108c 361{
0088c768 362 /* Provide default values for all "must have" methods. */
0b603eba
AC
363 if (t->to_xfer_partial == NULL)
364 t->to_xfer_partial = default_xfer_partial;
0088c768 365
c35b1492
PA
366 if (t->to_has_all_memory == NULL)
367 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
368
369 if (t->to_has_memory == NULL)
370 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
371
372 if (t->to_has_stack == NULL)
373 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
374
375 if (t->to_has_registers == NULL)
376 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
377
378 if (t->to_has_execution == NULL)
aeaec162 379 t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
1101cb7b
TT
380
381 install_delegators (t);
c22a2b88
TT
382}
383
384/* Add possible target architecture T to the list and add a new
385 command 'target T->to_shortname'. Set COMPLETER as the command's
386 completer if not NULL. */
387
388void
389add_target_with_completer (struct target_ops *t,
390 completer_ftype *completer)
391{
392 struct cmd_list_element *c;
393
394 complete_target_initialization (t);
c35b1492 395
c906108c
SS
396 if (!target_structs)
397 {
398 target_struct_allocsize = DEFAULT_ALLOCSIZE;
399 target_structs = (struct target_ops **) xmalloc
400 (target_struct_allocsize * sizeof (*target_structs));
401 }
402 if (target_struct_size >= target_struct_allocsize)
403 {
404 target_struct_allocsize *= 2;
405 target_structs = (struct target_ops **)
c5aa993b
JM
406 xrealloc ((char *) target_structs,
407 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
408 }
409 target_structs[target_struct_size++] = t;
c906108c
SS
410
411 if (targetlist == NULL)
1bedd215
AC
412 add_prefix_cmd ("target", class_run, target_command, _("\
413Connect to a target machine or process.\n\
c906108c
SS
414The first argument is the type or protocol of the target machine.\n\
415Remaining arguments are interpreted by the target protocol. For more\n\
416information on the arguments for a particular protocol, type\n\
1bedd215 417`help target ' followed by the protocol name."),
c906108c 418 &targetlist, "target ", 0, &cmdlist);
9852c492
YQ
419 c = add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc,
420 &targetlist);
421 if (completer != NULL)
422 set_cmd_completer (c, completer);
423}
424
425/* Add a possible target architecture to the list. */
426
427void
428add_target (struct target_ops *t)
429{
430 add_target_with_completer (t, NULL);
c906108c
SS
431}
432
b48d48eb
MM
433/* See target.h. */
434
435void
436add_deprecated_target_alias (struct target_ops *t, char *alias)
437{
438 struct cmd_list_element *c;
439 char *alt;
440
441 /* If we use add_alias_cmd, here, we do not get the deprecated warning,
442 see PR cli/15104. */
443 c = add_cmd (alias, no_class, t->to_open, t->to_doc, &targetlist);
444 alt = xstrprintf ("target %s", t->to_shortname);
445 deprecate_cmd (c, alt);
446}
447
c906108c
SS
448/* Stub functions */
449
450void
fba45db2 451target_ignore (void)
c906108c
SS
452{
453}
454
7d85a9c0
JB
455void
456target_kill (void)
457{
423a4807
TT
458 if (targetdebug)
459 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
7d85a9c0 460
423a4807 461 current_target.to_kill (&current_target);
7d85a9c0
JB
462}
463
11cf8741
JM
464void
465target_load (char *arg, int from_tty)
466{
4e5d721f 467 target_dcache_invalidate ();
71a9f134 468 (*current_target.to_load) (&current_target, arg, from_tty);
11cf8741
JM
469}
470
947b8855
PA
471void
472target_create_inferior (char *exec_file, char *args,
473 char **env, int from_tty)
136d6dae
VP
474{
475 struct target_ops *t;
5d502164 476
136d6dae
VP
477 for (t = current_target.beneath; t != NULL; t = t->beneath)
478 {
479 if (t->to_create_inferior != NULL)
480 {
481 t->to_create_inferior (t, exec_file, args, env, from_tty);
947b8855
PA
482 if (targetdebug)
483 fprintf_unfiltered (gdb_stdlog,
484 "target_create_inferior (%s, %s, xxx, %d)\n",
485 exec_file, args, from_tty);
136d6dae
VP
486 return;
487 }
488 }
489
490 internal_error (__FILE__, __LINE__,
9b20d036 491 _("could not find a target to create inferior"));
136d6dae
VP
492}
493
d9d2d8b6
PA
494void
495target_terminal_inferior (void)
496{
497 /* A background resume (``run&'') should leave GDB in control of the
c378eb4e 498 terminal. Use target_can_async_p, not target_is_async_p, since at
ba7f6c64
VP
499 this point the target is not async yet. However, if sync_execution
500 is not set, we know it will become async prior to resume. */
501 if (target_can_async_p () && !sync_execution)
d9d2d8b6
PA
502 return;
503
504 /* If GDB is resuming the inferior in the foreground, install
505 inferior's terminal modes. */
d2f640d4 506 (*current_target.to_terminal_inferior) (&current_target);
d9d2d8b6 507}
136d6dae 508
c906108c 509static int
fba45db2
KB
510nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
511 struct target_ops *t)
c906108c 512{
c378eb4e
MS
513 errno = EIO; /* Can't read/write this location. */
514 return 0; /* No bytes handled. */
c906108c
SS
515}
516
517static void
fba45db2 518tcomplain (void)
c906108c 519{
8a3fe4f8 520 error (_("You can't do that when your target is `%s'"),
c906108c
SS
521 current_target.to_shortname);
522}
523
524void
fba45db2 525noprocess (void)
c906108c 526{
8a3fe4f8 527 error (_("You can't do that without a process to debug."));
c906108c
SS
528}
529
c906108c 530static void
0a4f40a2 531default_terminal_info (struct target_ops *self, const char *args, int from_tty)
c906108c 532{
a3f17187 533 printf_unfiltered (_("No saved terminal information.\n"));
c906108c
SS
534}
535
0ef643c8
JB
536/* A default implementation for the to_get_ada_task_ptid target method.
537
538 This function builds the PTID by using both LWP and TID as part of
539 the PTID lwp and tid elements. The pid used is the pid of the
540 inferior_ptid. */
541
2c0b251b 542static ptid_t
1e6b91a4 543default_get_ada_task_ptid (struct target_ops *self, long lwp, long tid)
0ef643c8
JB
544{
545 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
546}
547
32231432 548static enum exec_direction_kind
4c612759 549default_execution_direction (struct target_ops *self)
32231432
PA
550{
551 if (!target_can_execute_reverse)
552 return EXEC_FORWARD;
553 else if (!target_can_async_p ())
554 return EXEC_FORWARD;
555 else
556 gdb_assert_not_reached ("\
557to_execution_direction must be implemented for reverse async");
558}
559
7998dfc3
AC
560/* Go through the target stack from top to bottom, copying over zero
561 entries in current_target, then filling in still empty entries. In
562 effect, we are doing class inheritance through the pushed target
563 vectors.
564
565 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
566 is currently implemented, is that it discards any knowledge of
567 which target an inherited method originally belonged to.
568 Consequently, new new target methods should instead explicitly and
569 locally search the target stack for the target that can handle the
570 request. */
c906108c
SS
571
572static void
7998dfc3 573update_current_target (void)
c906108c 574{
7998dfc3
AC
575 struct target_ops *t;
576
08d8bcd7 577 /* First, reset current's contents. */
7998dfc3
AC
578 memset (&current_target, 0, sizeof (current_target));
579
1101cb7b
TT
580 /* Install the delegators. */
581 install_delegators (&current_target);
582
7998dfc3
AC
583#define INHERIT(FIELD, TARGET) \
584 if (!current_target.FIELD) \
585 current_target.FIELD = (TARGET)->FIELD
586
587 for (t = target_stack; t; t = t->beneath)
588 {
589 INHERIT (to_shortname, t);
590 INHERIT (to_longname, t);
591 INHERIT (to_doc, t);
b52323fa
UW
592 /* Do not inherit to_open. */
593 /* Do not inherit to_close. */
136d6dae 594 /* Do not inherit to_attach. */
bebd3233 595 /* Do not inherit to_post_attach. */
dc177b7a 596 INHERIT (to_attach_no_wait, t);
136d6dae 597 /* Do not inherit to_detach. */
597320e7 598 /* Do not inherit to_disconnect. */
28439f5e 599 /* Do not inherit to_resume. */
117de6a9 600 /* Do not inherit to_wait. */
28439f5e
PA
601 /* Do not inherit to_fetch_registers. */
602 /* Do not inherit to_store_registers. */
6c628163 603 /* Do not inherit to_prepare_to_store. */
c8e73a31 604 INHERIT (deprecated_xfer_memory, t);
f86e59b2 605 /* Do not inherit to_files_info. */
3db08215
MM
606 /* Do not inherit to_insert_breakpoint. */
607 /* Do not inherit to_remove_breakpoint. */
52b51d06 608 /* Do not inherit to_can_use_hw_breakpoint. */
61b371f9 609 /* Do not inherit to_insert_hw_breakpoint. */
418dabac 610 /* Do not inherit to_remove_hw_breakpoint. */
f1310107 611 /* Do not inherit to_ranged_break_num_registers. */
016facd4 612 /* Do not inherit to_insert_watchpoint. */
61dd109f 613 /* Do not inherit to_remove_watchpoint. */
9c06b0b4
TJB
614 /* Do not inherit to_insert_mask_watchpoint. */
615 /* Do not inherit to_remove_mask_watchpoint. */
6b84065d 616 /* Do not inherit to_stopped_data_address. */
74174d2e 617 INHERIT (to_have_steppable_watchpoint, t);
7998dfc3 618 INHERIT (to_have_continuable_watchpoint, t);
6b84065d 619 /* Do not inherit to_stopped_by_watchpoint. */
65f160a9 620 /* Do not inherit to_watchpoint_addr_within_range. */
d03655e4 621 /* Do not inherit to_region_ok_for_hw_watchpoint. */
77cdffe9 622 /* Do not inherit to_can_accel_watchpoint_condition. */
9c06b0b4 623 /* Do not inherit to_masked_watch_num_registers. */
0343661d 624 /* Do not inherit to_terminal_init. */
ddeaacc9 625 /* Do not inherit to_terminal_inferior. */
74fcbef9 626 /* Do not inherit to_terminal_ours_for_output. */
e4a733f1 627 /* Do not inherit to_terminal_ours. */
c6ea8f79 628 /* Do not inherit to_terminal_save_ours. */
e19e919f 629 /* Do not inherit to_terminal_info. */
7d85a9c0 630 /* Do not inherit to_kill. */
7634da87 631 /* Do not inherit to_load. */
136d6dae 632 /* Do no inherit to_create_inferior. */
340ba4bf 633 /* Do not inherit to_post_startup_inferior. */
5958ebeb 634 /* Do not inherit to_insert_fork_catchpoint. */
e1a21fb7 635 /* Do not inherit to_remove_fork_catchpoint. */
7e18a8dc 636 /* Do not inherit to_insert_vfork_catchpoint. */
95c3375e 637 /* Do not inherit to_remove_vfork_catchpoint. */
ee057212 638 /* Do not inherit to_follow_fork. */
62f64d7a 639 /* Do not inherit to_insert_exec_catchpoint. */
cda0f38c 640 /* Do not inherit to_remove_exec_catchpoint. */
6a9fa051 641 /* Do not inherit to_set_syscall_catchpoint. */
0db88c1d 642 /* Do not inherit to_has_exited. */
82892036 643 /* Do not inherit to_mourn_inferior. */
7998dfc3 644 INHERIT (to_can_run, t);
2455069d 645 /* Do not inherit to_pass_signals. */
9b224c5e 646 /* Do not inherit to_program_signals. */
28439f5e
PA
647 /* Do not inherit to_thread_alive. */
648 /* Do not inherit to_find_new_threads. */
117de6a9 649 /* Do not inherit to_pid_to_str. */
4a7e6dda 650 /* Do not inherit to_extra_thread_info. */
825828fc 651 /* Do not inherit to_thread_name. */
46ee7e8d 652 /* Do not inherit to_stop. */
4b8a223f 653 /* Do not inherit to_xfer_partial. */
a53f3625 654 /* Do not inherit to_rcmd. */
830ca330 655 /* Do not inherit to_pid_to_exec_file. */
d9cb0195 656 /* Do not inherit to_log_command. */
7998dfc3 657 INHERIT (to_stratum, t);
c378eb4e
MS
658 /* Do not inherit to_has_all_memory. */
659 /* Do not inherit to_has_memory. */
660 /* Do not inherit to_has_stack. */
661 /* Do not inherit to_has_registers. */
662 /* Do not inherit to_has_execution. */
7998dfc3 663 INHERIT (to_has_thread_control, t);
6b84065d
TT
664 /* Do not inherit to_can_async_p. */
665 /* Do not inherit to_is_async_p. */
666 /* Do not inherit to_async. */
0b5a2719 667 /* Do not inherit to_find_memory_regions. */
16f796b1 668 /* Do not inherit to_make_corefile_notes. */
3dbafbbb 669 /* Do not inherit to_get_bookmark. */
9bb9d61d 670 /* Do not inherit to_goto_bookmark. */
117de6a9 671 /* Do not inherit to_get_thread_local_address. */
53e1cfc7 672 /* Do not inherit to_can_execute_reverse. */
fe31bf5b 673 /* Do not inherit to_execution_direction. */
43eba180 674 /* Do not inherit to_thread_architecture. */
424163ea 675 /* Do not inherit to_read_description. */
4229b31d 676 /* Do not inherit to_get_ada_task_ptid. */
08388c79 677 /* Do not inherit to_search_memory. */
a7304748 678 /* Do not inherit to_supports_multi_process. */
aab1b22d 679 /* Do not inherit to_supports_enable_disable_tracepoint. */
9409d39e 680 /* Do not inherit to_supports_string_tracing. */
5536135b 681 /* Do not inherit to_trace_init. */
9a980a22 682 /* Do not inherit to_download_tracepoint. */
719acc4a 683 /* Do not inherit to_can_download_tracepoint. */
94eb98b9 684 /* Do not inherit to_download_trace_state_variable. */
151f70f1 685 /* Do not inherit to_enable_tracepoint. */
05c41993 686 /* Do not inherit to_disable_tracepoint. */
86dd181d 687 /* Do not inherit to_trace_set_readonly_regions. */
25da2e80 688 /* Do not inherit to_trace_start. */
4072d4ff 689 /* Do not inherit to_get_trace_status. */
6fea14cd 690 /* Do not inherit to_get_tracepoint_status. */
e51c07ea 691 /* Do not inherit to_trace_stop. */
afc94e66 692 /* Do not inherit to_trace_find. */
959bcd0b 693 /* Do not inherit to_get_trace_state_variable_value. */
a2e6c147 694 /* Do not inherit to_save_trace_data. */
1e949b00 695 /* Do not inherit to_upload_tracepoints. */
08120467 696 /* Do not inherit to_upload_trace_state_variables. */
ace92e7d 697 /* Do not inherit to_get_raw_trace_data. */
9249843f 698 /* Do not inherit to_get_min_fast_tracepoint_insn_len. */
0bcfeddf 699 /* Do not inherit to_set_disconnected_tracing. */
8d526939 700 /* Do not inherit to_set_circular_trace_buffer. */
91df8d1d 701 /* Do not inherit to_set_trace_buffer_size. */
8586ccaa 702 /* Do not inherit to_set_trace_notes. */
22bcceee 703 /* Do not inherit to_get_tib_address. */
dcd6917f 704 /* Do not inherit to_set_permissions. */
4c3e4425 705 /* Do not inherit to_static_tracepoint_marker_at. */
d6522a22 706 /* Do not inherit to_static_tracepoint_markers_by_strid. */
92155eeb 707 /* Do not inherit to_traceframe_info. */
d9db5b21 708 /* Do not inherit to_use_agent. */
9a7d8b48 709 /* Do not inherit to_can_use_agent. */
0de91722 710 /* Do not inherit to_augmented_libraries_svr4_read. */
7998dfc3 711 INHERIT (to_magic, t);
ccfde2a0
TT
712 /* Do not inherit
713 to_supports_evaluation_of_breakpoint_conditions. */
843f59ed 714 /* Do not inherit to_can_run_breakpoint_commands. */
fd79ecee 715 /* Do not inherit to_memory_map. */
a76d924d
DJ
716 /* Do not inherit to_flash_erase. */
717 /* Do not inherit to_flash_done. */
7998dfc3
AC
718 }
719#undef INHERIT
720
721 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
722 it. Some entries are defaulted to a method that print an error,
723 others are hard-wired to a standard recursive default. */
c906108c
SS
724
725#define de_fault(field, value) \
7998dfc3
AC
726 if (!current_target.field) \
727 current_target.field = value
0d06e24b 728
2bc416ba
DJ
729 de_fault (to_open,
730 (void (*) (char *, int))
0d06e24b 731 tcomplain);
2bc416ba 732 de_fault (to_close,
de90e03d 733 (void (*) (struct target_ops *))
0d06e24b 734 target_ignore);
2bc416ba 735 de_fault (deprecated_xfer_memory,
3e43a32a
MS
736 (int (*) (CORE_ADDR, gdb_byte *, int, int,
737 struct mem_attrib *, struct target_ops *))
0d06e24b 738 nomemory);
2bc416ba 739 de_fault (to_can_run,
da82bd6b 740 (int (*) (struct target_ops *))
0d06e24b 741 return_zero);
424163ea 742 current_target.to_read_description = NULL;
32231432 743
c906108c 744#undef de_fault
c906108c 745
7998dfc3
AC
746 /* Finally, position the target-stack beneath the squashed
747 "current_target". That way code looking for a non-inherited
748 target method can quickly and simply find it. */
749 current_target.beneath = target_stack;
b4b61fdb
DJ
750
751 if (targetdebug)
752 setup_target_debug ();
c906108c
SS
753}
754
755/* Push a new target type into the stack of the existing target accessors,
756 possibly superseding some of the existing accessors.
757
c906108c
SS
758 Rather than allow an empty stack, we always have the dummy target at
759 the bottom stratum, so we can call the function vectors without
760 checking them. */
761
b26a4dcb 762void
fba45db2 763push_target (struct target_ops *t)
c906108c 764{
258b763a 765 struct target_ops **cur;
c906108c
SS
766
767 /* Check magic number. If wrong, it probably means someone changed
768 the struct definition, but not all the places that initialize one. */
769 if (t->to_magic != OPS_MAGIC)
770 {
c5aa993b
JM
771 fprintf_unfiltered (gdb_stderr,
772 "Magic number of %s target struct wrong\n",
773 t->to_shortname);
3e43a32a
MS
774 internal_error (__FILE__, __LINE__,
775 _("failed internal consistency check"));
c906108c
SS
776 }
777
258b763a
AC
778 /* Find the proper stratum to install this target in. */
779 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
c906108c 780 {
258b763a 781 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
c906108c
SS
782 break;
783 }
784
258b763a 785 /* If there's already targets at this stratum, remove them. */
88c231eb 786 /* FIXME: cagney/2003-10-15: I think this should be popping all
258b763a
AC
787 targets to CUR, and not just those at this stratum level. */
788 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
789 {
790 /* There's already something at this stratum level. Close it,
791 and un-hook it from the stack. */
792 struct target_ops *tmp = (*cur);
5d502164 793
258b763a
AC
794 (*cur) = (*cur)->beneath;
795 tmp->beneath = NULL;
460014f5 796 target_close (tmp);
258b763a 797 }
c906108c
SS
798
799 /* We have removed all targets in our stratum, now add the new one. */
258b763a
AC
800 t->beneath = (*cur);
801 (*cur) = t;
c906108c
SS
802
803 update_current_target ();
c906108c
SS
804}
805
2bc416ba 806/* Remove a target_ops vector from the stack, wherever it may be.
c906108c
SS
807 Return how many times it was removed (0 or 1). */
808
809int
fba45db2 810unpush_target (struct target_ops *t)
c906108c 811{
258b763a
AC
812 struct target_ops **cur;
813 struct target_ops *tmp;
c906108c 814
c8d104ad
PA
815 if (t->to_stratum == dummy_stratum)
816 internal_error (__FILE__, __LINE__,
9b20d036 817 _("Attempt to unpush the dummy target"));
c8d104ad 818
c906108c 819 /* Look for the specified target. Note that we assume that a target
c378eb4e 820 can only occur once in the target stack. */
c906108c 821
258b763a
AC
822 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
823 {
824 if ((*cur) == t)
825 break;
826 }
c906108c 827
305436e0
PA
828 /* If we don't find target_ops, quit. Only open targets should be
829 closed. */
258b763a 830 if ((*cur) == NULL)
305436e0 831 return 0;
5269965e 832
c378eb4e 833 /* Unchain the target. */
258b763a
AC
834 tmp = (*cur);
835 (*cur) = (*cur)->beneath;
836 tmp->beneath = NULL;
c906108c
SS
837
838 update_current_target ();
c906108c 839
305436e0
PA
840 /* Finally close the target. Note we do this after unchaining, so
841 any target method calls from within the target_close
842 implementation don't end up in T anymore. */
460014f5 843 target_close (t);
305436e0 844
c906108c
SS
845 return 1;
846}
847
aa76d38d 848void
460014f5 849pop_all_targets_above (enum strata above_stratum)
aa76d38d 850{
87ab71f0 851 while ((int) (current_target.to_stratum) > (int) above_stratum)
aa76d38d 852 {
aa76d38d
PA
853 if (!unpush_target (target_stack))
854 {
855 fprintf_unfiltered (gdb_stderr,
856 "pop_all_targets couldn't find target %s\n",
b52323fa 857 target_stack->to_shortname);
aa76d38d
PA
858 internal_error (__FILE__, __LINE__,
859 _("failed internal consistency check"));
860 break;
861 }
862 }
863}
864
87ab71f0 865void
460014f5 866pop_all_targets (void)
87ab71f0 867{
460014f5 868 pop_all_targets_above (dummy_stratum);
87ab71f0
PA
869}
870
c0edd9ed
JK
871/* Return 1 if T is now pushed in the target stack. Return 0 otherwise. */
872
873int
874target_is_pushed (struct target_ops *t)
875{
876 struct target_ops **cur;
877
878 /* Check magic number. If wrong, it probably means someone changed
879 the struct definition, but not all the places that initialize one. */
880 if (t->to_magic != OPS_MAGIC)
881 {
882 fprintf_unfiltered (gdb_stderr,
883 "Magic number of %s target struct wrong\n",
884 t->to_shortname);
3e43a32a
MS
885 internal_error (__FILE__, __LINE__,
886 _("failed internal consistency check"));
c0edd9ed
JK
887 }
888
889 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
890 if (*cur == t)
891 return 1;
892
893 return 0;
894}
895
72f5cf0e 896/* Using the objfile specified in OBJFILE, find the address for the
9e35dae4
DJ
897 current thread's thread-local storage with offset OFFSET. */
898CORE_ADDR
899target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
900{
901 volatile CORE_ADDR addr = 0;
117de6a9
PA
902 struct target_ops *target;
903
904 for (target = current_target.beneath;
905 target != NULL;
906 target = target->beneath)
907 {
908 if (target->to_get_thread_local_address != NULL)
909 break;
910 }
9e35dae4 911
117de6a9 912 if (target != NULL
f5656ead 913 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch ()))
9e35dae4
DJ
914 {
915 ptid_t ptid = inferior_ptid;
916 volatile struct gdb_exception ex;
917
918 TRY_CATCH (ex, RETURN_MASK_ALL)
919 {
920 CORE_ADDR lm_addr;
921
922 /* Fetch the load module address for this objfile. */
f5656ead 923 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
9e35dae4
DJ
924 objfile);
925 /* If it's 0, throw the appropriate exception. */
926 if (lm_addr == 0)
927 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
928 _("TLS load module not found"));
929
3e43a32a
MS
930 addr = target->to_get_thread_local_address (target, ptid,
931 lm_addr, offset);
9e35dae4
DJ
932 }
933 /* If an error occurred, print TLS related messages here. Otherwise,
934 throw the error to some higher catcher. */
935 if (ex.reason < 0)
936 {
937 int objfile_is_library = (objfile->flags & OBJF_SHARED);
938
939 switch (ex.error)
940 {
941 case TLS_NO_LIBRARY_SUPPORT_ERROR:
3e43a32a
MS
942 error (_("Cannot find thread-local variables "
943 "in this thread library."));
9e35dae4
DJ
944 break;
945 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
946 if (objfile_is_library)
947 error (_("Cannot find shared library `%s' in dynamic"
4262abfb 948 " linker's load module list"), objfile_name (objfile));
9e35dae4
DJ
949 else
950 error (_("Cannot find executable file `%s' in dynamic"
4262abfb 951 " linker's load module list"), objfile_name (objfile));
9e35dae4
DJ
952 break;
953 case TLS_NOT_ALLOCATED_YET_ERROR:
954 if (objfile_is_library)
955 error (_("The inferior has not yet allocated storage for"
956 " thread-local variables in\n"
957 "the shared library `%s'\n"
958 "for %s"),
4262abfb 959 objfile_name (objfile), target_pid_to_str (ptid));
9e35dae4
DJ
960 else
961 error (_("The inferior has not yet allocated storage for"
962 " thread-local variables in\n"
963 "the executable `%s'\n"
964 "for %s"),
4262abfb 965 objfile_name (objfile), target_pid_to_str (ptid));
9e35dae4
DJ
966 break;
967 case TLS_GENERIC_ERROR:
968 if (objfile_is_library)
969 error (_("Cannot find thread-local storage for %s, "
970 "shared library %s:\n%s"),
971 target_pid_to_str (ptid),
4262abfb 972 objfile_name (objfile), ex.message);
9e35dae4
DJ
973 else
974 error (_("Cannot find thread-local storage for %s, "
975 "executable file %s:\n%s"),
976 target_pid_to_str (ptid),
4262abfb 977 objfile_name (objfile), ex.message);
9e35dae4
DJ
978 break;
979 default:
980 throw_exception (ex);
981 break;
982 }
983 }
984 }
985 /* It wouldn't be wrong here to try a gdbarch method, too; finding
986 TLS is an ABI-specific thing. But we don't do that yet. */
987 else
988 error (_("Cannot find thread-local variables on this target"));
989
990 return addr;
991}
992
6be7b56e 993const char *
9b409511 994target_xfer_status_to_string (enum target_xfer_status err)
6be7b56e
PA
995{
996#define CASE(X) case X: return #X
997 switch (err)
998 {
999 CASE(TARGET_XFER_E_IO);
1000 CASE(TARGET_XFER_E_UNAVAILABLE);
1001 default:
1002 return "<unknown>";
1003 }
1004#undef CASE
1005};
1006
1007
c906108c
SS
1008#undef MIN
1009#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1010
1011/* target_read_string -- read a null terminated string, up to LEN bytes,
1012 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1013 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1014 is responsible for freeing it. Return the number of bytes successfully
1015 read. */
1016
1017int
fba45db2 1018target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c 1019{
c2e8b827 1020 int tlen, offset, i;
1b0ba102 1021 gdb_byte buf[4];
c906108c
SS
1022 int errcode = 0;
1023 char *buffer;
1024 int buffer_allocated;
1025 char *bufptr;
1026 unsigned int nbytes_read = 0;
1027
6217bf3e
MS
1028 gdb_assert (string);
1029
c906108c
SS
1030 /* Small for testing. */
1031 buffer_allocated = 4;
1032 buffer = xmalloc (buffer_allocated);
1033 bufptr = buffer;
1034
c906108c
SS
1035 while (len > 0)
1036 {
1037 tlen = MIN (len, 4 - (memaddr & 3));
1038 offset = memaddr & 3;
1039
1b0ba102 1040 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
c906108c
SS
1041 if (errcode != 0)
1042 {
1043 /* The transfer request might have crossed the boundary to an
c378eb4e 1044 unallocated region of memory. Retry the transfer, requesting
c906108c
SS
1045 a single byte. */
1046 tlen = 1;
1047 offset = 0;
b8eb5af0 1048 errcode = target_read_memory (memaddr, buf, 1);
c906108c
SS
1049 if (errcode != 0)
1050 goto done;
1051 }
1052
1053 if (bufptr - buffer + tlen > buffer_allocated)
1054 {
1055 unsigned int bytes;
5d502164 1056
c906108c
SS
1057 bytes = bufptr - buffer;
1058 buffer_allocated *= 2;
1059 buffer = xrealloc (buffer, buffer_allocated);
1060 bufptr = buffer + bytes;
1061 }
1062
1063 for (i = 0; i < tlen; i++)
1064 {
1065 *bufptr++ = buf[i + offset];
1066 if (buf[i + offset] == '\000')
1067 {
1068 nbytes_read += i + 1;
1069 goto done;
1070 }
1071 }
1072
1073 memaddr += tlen;
1074 len -= tlen;
1075 nbytes_read += tlen;
1076 }
c5aa993b 1077done:
6217bf3e 1078 *string = buffer;
c906108c
SS
1079 if (errnop != NULL)
1080 *errnop = errcode;
c906108c
SS
1081 return nbytes_read;
1082}
1083
07b82ea5
PA
1084struct target_section_table *
1085target_get_section_table (struct target_ops *target)
1086{
1087 struct target_ops *t;
1088
1089 if (targetdebug)
1090 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1091
1092 for (t = target; t != NULL; t = t->beneath)
1093 if (t->to_get_section_table != NULL)
1094 return (*t->to_get_section_table) (t);
1095
1096 return NULL;
1097}
1098
8db32d44 1099/* Find a section containing ADDR. */
07b82ea5 1100
0542c86d 1101struct target_section *
8db32d44
AC
1102target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1103{
07b82ea5 1104 struct target_section_table *table = target_get_section_table (target);
0542c86d 1105 struct target_section *secp;
07b82ea5
PA
1106
1107 if (table == NULL)
1108 return NULL;
1109
1110 for (secp = table->sections; secp < table->sections_end; secp++)
8db32d44
AC
1111 {
1112 if (addr >= secp->addr && addr < secp->endaddr)
1113 return secp;
1114 }
1115 return NULL;
1116}
1117
e6e4e701
PA
1118/* Read memory from the live target, even if currently inspecting a
1119 traceframe. The return is the same as that of target_read. */
1120
9b409511 1121static enum target_xfer_status
e6e4e701 1122target_read_live_memory (enum target_object object,
9b409511
YQ
1123 ULONGEST memaddr, gdb_byte *myaddr, ULONGEST len,
1124 ULONGEST *xfered_len)
e6e4e701 1125{
9b409511 1126 enum target_xfer_status ret;
e6e4e701
PA
1127 struct cleanup *cleanup;
1128
1129 /* Switch momentarily out of tfind mode so to access live memory.
1130 Note that this must not clear global state, such as the frame
1131 cache, which must still remain valid for the previous traceframe.
1132 We may be _building_ the frame cache at this point. */
1133 cleanup = make_cleanup_restore_traceframe_number ();
1134 set_traceframe_number (-1);
1135
9b409511
YQ
1136 ret = target_xfer_partial (current_target.beneath, object, NULL,
1137 myaddr, NULL, memaddr, len, xfered_len);
e6e4e701
PA
1138
1139 do_cleanups (cleanup);
1140 return ret;
1141}
1142
1143/* Using the set of read-only target sections of OPS, read live
1144 read-only memory. Note that the actual reads start from the
5657161f
PA
1145 top-most target again.
1146
1147 For interface/parameters/return description see target.h,
1148 to_xfer_partial. */
e6e4e701 1149
9b409511 1150static enum target_xfer_status
e6e4e701
PA
1151memory_xfer_live_readonly_partial (struct target_ops *ops,
1152 enum target_object object,
1153 gdb_byte *readbuf, ULONGEST memaddr,
9b409511 1154 ULONGEST len, ULONGEST *xfered_len)
e6e4e701
PA
1155{
1156 struct target_section *secp;
1157 struct target_section_table *table;
1158
1159 secp = target_section_by_addr (ops, memaddr);
1160 if (secp != NULL
2b2848e2
DE
1161 && (bfd_get_section_flags (secp->the_bfd_section->owner,
1162 secp->the_bfd_section)
e6e4e701
PA
1163 & SEC_READONLY))
1164 {
1165 struct target_section *p;
1166 ULONGEST memend = memaddr + len;
1167
1168 table = target_get_section_table (ops);
1169
1170 for (p = table->sections; p < table->sections_end; p++)
1171 {
1172 if (memaddr >= p->addr)
1173 {
1174 if (memend <= p->endaddr)
1175 {
1176 /* Entire transfer is within this section. */
1177 return target_read_live_memory (object, memaddr,
9b409511 1178 readbuf, len, xfered_len);
e6e4e701
PA
1179 }
1180 else if (memaddr >= p->endaddr)
1181 {
1182 /* This section ends before the transfer starts. */
1183 continue;
1184 }
1185 else
1186 {
1187 /* This section overlaps the transfer. Just do half. */
1188 len = p->endaddr - memaddr;
1189 return target_read_live_memory (object, memaddr,
9b409511 1190 readbuf, len, xfered_len);
e6e4e701
PA
1191 }
1192 }
1193 }
1194 }
1195
9b409511 1196 return TARGET_XFER_EOF;
e6e4e701
PA
1197}
1198
9f713294
YQ
1199/* Read memory from more than one valid target. A core file, for
1200 instance, could have some of memory but delegate other bits to
1201 the target below it. So, we must manually try all targets. */
1202
9b409511 1203static enum target_xfer_status
17fde6d0 1204raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf,
9b409511
YQ
1205 const gdb_byte *writebuf, ULONGEST memaddr, LONGEST len,
1206 ULONGEST *xfered_len)
9f713294 1207{
9b409511 1208 enum target_xfer_status res;
9f713294
YQ
1209
1210 do
1211 {
1212 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
9b409511
YQ
1213 readbuf, writebuf, memaddr, len,
1214 xfered_len);
1215 if (res == TARGET_XFER_OK)
9f713294
YQ
1216 break;
1217
633785ff
MM
1218 /* Stop if the target reports that the memory is not available. */
1219 if (res == TARGET_XFER_E_UNAVAILABLE)
1220 break;
1221
9f713294
YQ
1222 /* We want to continue past core files to executables, but not
1223 past a running target's memory. */
1224 if (ops->to_has_all_memory (ops))
1225 break;
1226
1227 ops = ops->beneath;
1228 }
1229 while (ops != NULL);
1230
1231 return res;
1232}
1233
7f79c47e
DE
1234/* Perform a partial memory transfer.
1235 For docs see target.h, to_xfer_partial. */
cf7a04e8 1236
9b409511 1237static enum target_xfer_status
f0ba3972 1238memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
17fde6d0 1239 gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST memaddr,
9b409511 1240 ULONGEST len, ULONGEST *xfered_len)
0779438d 1241{
9b409511 1242 enum target_xfer_status res;
cf7a04e8
DJ
1243 int reg_len;
1244 struct mem_region *region;
4e5d721f 1245 struct inferior *inf;
cf7a04e8 1246
07b82ea5
PA
1247 /* For accesses to unmapped overlay sections, read directly from
1248 files. Must do this first, as MEMADDR may need adjustment. */
1249 if (readbuf != NULL && overlay_debugging)
1250 {
1251 struct obj_section *section = find_pc_overlay (memaddr);
5d502164 1252
07b82ea5
PA
1253 if (pc_in_unmapped_range (memaddr, section))
1254 {
1255 struct target_section_table *table
1256 = target_get_section_table (ops);
1257 const char *section_name = section->the_bfd_section->name;
5d502164 1258
07b82ea5
PA
1259 memaddr = overlay_mapped_address (memaddr, section);
1260 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 1261 memaddr, len, xfered_len,
07b82ea5
PA
1262 table->sections,
1263 table->sections_end,
1264 section_name);
1265 }
1266 }
1267
1268 /* Try the executable files, if "trust-readonly-sections" is set. */
cf7a04e8
DJ
1269 if (readbuf != NULL && trust_readonly)
1270 {
0542c86d 1271 struct target_section *secp;
07b82ea5 1272 struct target_section_table *table;
cf7a04e8
DJ
1273
1274 secp = target_section_by_addr (ops, memaddr);
1275 if (secp != NULL
2b2848e2
DE
1276 && (bfd_get_section_flags (secp->the_bfd_section->owner,
1277 secp->the_bfd_section)
cf7a04e8 1278 & SEC_READONLY))
07b82ea5
PA
1279 {
1280 table = target_get_section_table (ops);
1281 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 1282 memaddr, len, xfered_len,
07b82ea5
PA
1283 table->sections,
1284 table->sections_end,
1285 NULL);
1286 }
98646950
UW
1287 }
1288
e6e4e701
PA
1289 /* If reading unavailable memory in the context of traceframes, and
1290 this address falls within a read-only section, fallback to
1291 reading from live memory. */
1292 if (readbuf != NULL && get_traceframe_number () != -1)
1293 {
1294 VEC(mem_range_s) *available;
1295
1296 /* If we fail to get the set of available memory, then the
1297 target does not support querying traceframe info, and so we
1298 attempt reading from the traceframe anyway (assuming the
1299 target implements the old QTro packet then). */
1300 if (traceframe_available_memory (&available, memaddr, len))
1301 {
1302 struct cleanup *old_chain;
1303
1304 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
1305
1306 if (VEC_empty (mem_range_s, available)
1307 || VEC_index (mem_range_s, available, 0)->start != memaddr)
1308 {
1309 /* Don't read into the traceframe's available
1310 memory. */
1311 if (!VEC_empty (mem_range_s, available))
1312 {
1313 LONGEST oldlen = len;
1314
1315 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
1316 gdb_assert (len <= oldlen);
1317 }
1318
1319 do_cleanups (old_chain);
1320
1321 /* This goes through the topmost target again. */
1322 res = memory_xfer_live_readonly_partial (ops, object,
9b409511
YQ
1323 readbuf, memaddr,
1324 len, xfered_len);
1325 if (res == TARGET_XFER_OK)
1326 return TARGET_XFER_OK;
1327 else
1328 {
1329 /* No use trying further, we know some memory starting
1330 at MEMADDR isn't available. */
1331 *xfered_len = len;
1332 return TARGET_XFER_E_UNAVAILABLE;
1333 }
e6e4e701
PA
1334 }
1335
1336 /* Don't try to read more than how much is available, in
1337 case the target implements the deprecated QTro packet to
1338 cater for older GDBs (the target's knowledge of read-only
1339 sections may be outdated by now). */
1340 len = VEC_index (mem_range_s, available, 0)->length;
1341
1342 do_cleanups (old_chain);
1343 }
1344 }
1345
cf7a04e8
DJ
1346 /* Try GDB's internal data cache. */
1347 region = lookup_mem_region (memaddr);
4b5752d0
VP
1348 /* region->hi == 0 means there's no upper bound. */
1349 if (memaddr + len < region->hi || region->hi == 0)
cf7a04e8
DJ
1350 reg_len = len;
1351 else
1352 reg_len = region->hi - memaddr;
1353
1354 switch (region->attrib.mode)
1355 {
1356 case MEM_RO:
1357 if (writebuf != NULL)
2ed4b548 1358 return TARGET_XFER_E_IO;
cf7a04e8
DJ
1359 break;
1360
1361 case MEM_WO:
1362 if (readbuf != NULL)
2ed4b548 1363 return TARGET_XFER_E_IO;
cf7a04e8 1364 break;
a76d924d
DJ
1365
1366 case MEM_FLASH:
1367 /* We only support writing to flash during "load" for now. */
1368 if (writebuf != NULL)
1369 error (_("Writing to flash memory forbidden in this context"));
1370 break;
4b5752d0
VP
1371
1372 case MEM_NONE:
2ed4b548 1373 return TARGET_XFER_E_IO;
cf7a04e8
DJ
1374 }
1375
6c95b8df
PA
1376 if (!ptid_equal (inferior_ptid, null_ptid))
1377 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1378 else
1379 inf = NULL;
4e5d721f
DE
1380
1381 if (inf != NULL
2f4d8875
PA
1382 /* The dcache reads whole cache lines; that doesn't play well
1383 with reading from a trace buffer, because reading outside of
1384 the collected memory range fails. */
1385 && get_traceframe_number () == -1
4e5d721f 1386 && (region->attrib.cache
29453a14
YQ
1387 || (stack_cache_enabled_p () && object == TARGET_OBJECT_STACK_MEMORY)
1388 || (code_cache_enabled_p () && object == TARGET_OBJECT_CODE_MEMORY)))
cf7a04e8 1389 {
2a2f9fe4 1390 DCACHE *dcache = target_dcache_get_or_init ();
9b409511 1391 int l;
2a2f9fe4 1392
cf7a04e8 1393 if (readbuf != NULL)
9b409511 1394 l = dcache_xfer_memory (ops, dcache, memaddr, readbuf, reg_len, 0);
cf7a04e8
DJ
1395 else
1396 /* FIXME drow/2006-08-09: If we're going to preserve const
1397 correctness dcache_xfer_memory should take readbuf and
1398 writebuf. */
9b409511 1399 l = dcache_xfer_memory (ops, dcache, memaddr, (void *) writebuf,
cf7a04e8 1400 reg_len, 1);
9b409511
YQ
1401 if (l <= 0)
1402 return TARGET_XFER_E_IO;
cf7a04e8 1403 else
9b409511
YQ
1404 {
1405 *xfered_len = (ULONGEST) l;
1406 return TARGET_XFER_OK;
1407 }
cf7a04e8
DJ
1408 }
1409
1410 /* If none of those methods found the memory we wanted, fall back
1411 to a target partial transfer. Normally a single call to
1412 to_xfer_partial is enough; if it doesn't recognize an object
1413 it will call the to_xfer_partial of the next target down.
1414 But for memory this won't do. Memory is the only target
9b409511
YQ
1415 object which can be read from more than one valid target.
1416 A core file, for instance, could have some of memory but
1417 delegate other bits to the target below it. So, we must
1418 manually try all targets. */
1419
1420 res = raw_memory_xfer_partial (ops, readbuf, writebuf, memaddr, reg_len,
1421 xfered_len);
cf7a04e8 1422
41dcd03f
DE
1423 /* Make sure the cache gets updated no matter what - if we are writing
1424 to the stack. Even if this write is not tagged as such, we still need
1425 to update the cache. */
1426
9b409511 1427 if (res == TARGET_XFER_OK
41dcd03f
DE
1428 && inf != NULL
1429 && writebuf != NULL
f2de9785 1430 && target_dcache_init_p ()
41dcd03f 1431 && !region->attrib.cache
29453a14
YQ
1432 && ((stack_cache_enabled_p () && object != TARGET_OBJECT_STACK_MEMORY)
1433 || (code_cache_enabled_p () && object != TARGET_OBJECT_CODE_MEMORY)))
41dcd03f 1434 {
f2de9785 1435 DCACHE *dcache = target_dcache_get ();
2a2f9fe4 1436
9b409511 1437 dcache_update (dcache, memaddr, (void *) writebuf, reg_len);
41dcd03f
DE
1438 }
1439
cf7a04e8
DJ
1440 /* If we still haven't got anything, return the last error. We
1441 give up. */
1442 return res;
0779438d
AC
1443}
1444
f0ba3972
PA
1445/* Perform a partial memory transfer. For docs see target.h,
1446 to_xfer_partial. */
1447
9b409511 1448static enum target_xfer_status
f0ba3972 1449memory_xfer_partial (struct target_ops *ops, enum target_object object,
9b409511
YQ
1450 gdb_byte *readbuf, const gdb_byte *writebuf,
1451 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
f0ba3972 1452{
9b409511 1453 enum target_xfer_status res;
f0ba3972
PA
1454
1455 /* Zero length requests are ok and require no work. */
1456 if (len == 0)
9b409511 1457 return TARGET_XFER_EOF;
f0ba3972
PA
1458
1459 /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1460 breakpoint insns, thus hiding out from higher layers whether
1461 there are software breakpoints inserted in the code stream. */
1462 if (readbuf != NULL)
1463 {
9b409511
YQ
1464 res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len,
1465 xfered_len);
f0ba3972 1466
9b409511 1467 if (res == TARGET_XFER_OK && !show_memory_breakpoints)
f0ba3972
PA
1468 breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, res);
1469 }
1470 else
1471 {
1472 void *buf;
1473 struct cleanup *old_chain;
1474
67c059c2
AB
1475 /* A large write request is likely to be partially satisfied
1476 by memory_xfer_partial_1. We will continually malloc
1477 and free a copy of the entire write request for breakpoint
1478 shadow handling even though we only end up writing a small
1479 subset of it. Cap writes to 4KB to mitigate this. */
1480 len = min (4096, len);
1481
f0ba3972
PA
1482 buf = xmalloc (len);
1483 old_chain = make_cleanup (xfree, buf);
1484 memcpy (buf, writebuf, len);
1485
1486 breakpoint_xfer_memory (NULL, buf, writebuf, memaddr, len);
9b409511
YQ
1487 res = memory_xfer_partial_1 (ops, object, NULL, buf, memaddr, len,
1488 xfered_len);
f0ba3972
PA
1489
1490 do_cleanups (old_chain);
1491 }
1492
1493 return res;
1494}
1495
8defab1a
DJ
1496static void
1497restore_show_memory_breakpoints (void *arg)
1498{
1499 show_memory_breakpoints = (uintptr_t) arg;
1500}
1501
1502struct cleanup *
1503make_show_memory_breakpoints_cleanup (int show)
1504{
1505 int current = show_memory_breakpoints;
8defab1a 1506
5d502164 1507 show_memory_breakpoints = show;
8defab1a
DJ
1508 return make_cleanup (restore_show_memory_breakpoints,
1509 (void *) (uintptr_t) current);
1510}
1511
7f79c47e
DE
1512/* For docs see target.h, to_xfer_partial. */
1513
9b409511 1514enum target_xfer_status
27394598
AC
1515target_xfer_partial (struct target_ops *ops,
1516 enum target_object object, const char *annex,
4ac248ca 1517 gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511
YQ
1518 ULONGEST offset, ULONGEST len,
1519 ULONGEST *xfered_len)
27394598 1520{
9b409511 1521 enum target_xfer_status retval;
27394598
AC
1522
1523 gdb_assert (ops->to_xfer_partial != NULL);
cf7a04e8 1524
ce6d0892
YQ
1525 /* Transfer is done when LEN is zero. */
1526 if (len == 0)
9b409511 1527 return TARGET_XFER_EOF;
ce6d0892 1528
d914c394
SS
1529 if (writebuf && !may_write_memory)
1530 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1531 core_addr_to_string_nz (offset), plongest (len));
1532
9b409511
YQ
1533 *xfered_len = 0;
1534
cf7a04e8
DJ
1535 /* If this is a memory transfer, let the memory-specific code
1536 have a look at it instead. Memory transfers are more
1537 complicated. */
29453a14
YQ
1538 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY
1539 || object == TARGET_OBJECT_CODE_MEMORY)
4e5d721f 1540 retval = memory_xfer_partial (ops, object, readbuf,
9b409511 1541 writebuf, offset, len, xfered_len);
9f713294 1542 else if (object == TARGET_OBJECT_RAW_MEMORY)
cf7a04e8 1543 {
9f713294 1544 /* Request the normal memory object from other layers. */
9b409511
YQ
1545 retval = raw_memory_xfer_partial (ops, readbuf, writebuf, offset, len,
1546 xfered_len);
cf7a04e8 1547 }
9f713294
YQ
1548 else
1549 retval = ops->to_xfer_partial (ops, object, annex, readbuf,
9b409511 1550 writebuf, offset, len, xfered_len);
cf7a04e8 1551
27394598
AC
1552 if (targetdebug)
1553 {
1554 const unsigned char *myaddr = NULL;
1555
1556 fprintf_unfiltered (gdb_stdlog,
3e43a32a 1557 "%s:target_xfer_partial "
9b409511 1558 "(%d, %s, %s, %s, %s, %s) = %d, %s",
27394598
AC
1559 ops->to_shortname,
1560 (int) object,
1561 (annex ? annex : "(null)"),
53b71562
JB
1562 host_address_to_string (readbuf),
1563 host_address_to_string (writebuf),
0b1553bc 1564 core_addr_to_string_nz (offset),
9b409511
YQ
1565 pulongest (len), retval,
1566 pulongest (*xfered_len));
27394598
AC
1567
1568 if (readbuf)
1569 myaddr = readbuf;
1570 if (writebuf)
1571 myaddr = writebuf;
9b409511 1572 if (retval == TARGET_XFER_OK && myaddr != NULL)
27394598
AC
1573 {
1574 int i;
2bc416ba 1575
27394598 1576 fputs_unfiltered (", bytes =", gdb_stdlog);
9b409511 1577 for (i = 0; i < *xfered_len; i++)
27394598 1578 {
53b71562 1579 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
27394598
AC
1580 {
1581 if (targetdebug < 2 && i > 0)
1582 {
1583 fprintf_unfiltered (gdb_stdlog, " ...");
1584 break;
1585 }
1586 fprintf_unfiltered (gdb_stdlog, "\n");
1587 }
2bc416ba 1588
27394598
AC
1589 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1590 }
1591 }
2bc416ba 1592
27394598
AC
1593 fputc_unfiltered ('\n', gdb_stdlog);
1594 }
9b409511
YQ
1595
1596 /* Check implementations of to_xfer_partial update *XFERED_LEN
1597 properly. Do assertion after printing debug messages, so that we
1598 can find more clues on assertion failure from debugging messages. */
1599 if (retval == TARGET_XFER_OK || retval == TARGET_XFER_E_UNAVAILABLE)
1600 gdb_assert (*xfered_len > 0);
1601
27394598
AC
1602 return retval;
1603}
1604
578d3588
PA
1605/* Read LEN bytes of target memory at address MEMADDR, placing the
1606 results in GDB's memory at MYADDR. Returns either 0 for success or
9b409511 1607 TARGET_XFER_E_IO if any error occurs.
c906108c
SS
1608
1609 If an error occurs, no guarantee is made about the contents of the data at
1610 MYADDR. In particular, the caller should not depend upon partial reads
1611 filling the buffer with good data. There is no way for the caller to know
1612 how much good data might have been transfered anyway. Callers that can
cf7a04e8 1613 deal with partial reads should call target_read (which will retry until
c378eb4e 1614 it makes no progress, and then return how much was transferred). */
c906108c
SS
1615
1616int
1b162304 1617target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
c906108c 1618{
c35b1492
PA
1619 /* Dispatch to the topmost target, not the flattened current_target.
1620 Memory accesses check target->to_has_(all_)memory, and the
1621 flattened target doesn't inherit those. */
1622 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1623 myaddr, memaddr, len) == len)
1624 return 0;
0779438d 1625 else
578d3588 1626 return TARGET_XFER_E_IO;
c906108c
SS
1627}
1628
aee4bf85
PA
1629/* Like target_read_memory, but specify explicitly that this is a read
1630 from the target's raw memory. That is, this read bypasses the
1631 dcache, breakpoint shadowing, etc. */
1632
1633int
1634target_read_raw_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
1635{
1636 /* See comment in target_read_memory about why the request starts at
1637 current_target.beneath. */
1638 if (target_read (current_target.beneath, TARGET_OBJECT_RAW_MEMORY, NULL,
1639 myaddr, memaddr, len) == len)
1640 return 0;
1641 else
1642 return TARGET_XFER_E_IO;
1643}
1644
4e5d721f
DE
1645/* Like target_read_memory, but specify explicitly that this is a read from
1646 the target's stack. This may trigger different cache behavior. */
1647
1648int
45aa4659 1649target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
4e5d721f 1650{
aee4bf85
PA
1651 /* See comment in target_read_memory about why the request starts at
1652 current_target.beneath. */
4e5d721f
DE
1653 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1654 myaddr, memaddr, len) == len)
1655 return 0;
1656 else
578d3588 1657 return TARGET_XFER_E_IO;
4e5d721f
DE
1658}
1659
29453a14
YQ
1660/* Like target_read_memory, but specify explicitly that this is a read from
1661 the target's code. This may trigger different cache behavior. */
1662
1663int
1664target_read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
1665{
aee4bf85
PA
1666 /* See comment in target_read_memory about why the request starts at
1667 current_target.beneath. */
29453a14
YQ
1668 if (target_read (current_target.beneath, TARGET_OBJECT_CODE_MEMORY, NULL,
1669 myaddr, memaddr, len) == len)
1670 return 0;
1671 else
1672 return TARGET_XFER_E_IO;
1673}
1674
7f79c47e 1675/* Write LEN bytes from MYADDR to target memory at address MEMADDR.
9b409511 1676 Returns either 0 for success or TARGET_XFER_E_IO if any
578d3588
PA
1677 error occurs. If an error occurs, no guarantee is made about how
1678 much data got written. Callers that can deal with partial writes
1679 should call target_write. */
7f79c47e 1680
c906108c 1681int
45aa4659 1682target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
c906108c 1683{
aee4bf85
PA
1684 /* See comment in target_read_memory about why the request starts at
1685 current_target.beneath. */
c35b1492 1686 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1687 myaddr, memaddr, len) == len)
1688 return 0;
0779438d 1689 else
578d3588 1690 return TARGET_XFER_E_IO;
c906108c 1691}
c5aa993b 1692
f0ba3972 1693/* Write LEN bytes from MYADDR to target raw memory at address
9b409511 1694 MEMADDR. Returns either 0 for success or TARGET_XFER_E_IO
578d3588
PA
1695 if any error occurs. If an error occurs, no guarantee is made
1696 about how much data got written. Callers that can deal with
1697 partial writes should call target_write. */
f0ba3972
PA
1698
1699int
45aa4659 1700target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
f0ba3972 1701{
aee4bf85
PA
1702 /* See comment in target_read_memory about why the request starts at
1703 current_target.beneath. */
f0ba3972
PA
1704 if (target_write (current_target.beneath, TARGET_OBJECT_RAW_MEMORY, NULL,
1705 myaddr, memaddr, len) == len)
1706 return 0;
1707 else
578d3588 1708 return TARGET_XFER_E_IO;
f0ba3972
PA
1709}
1710
fd79ecee
DJ
1711/* Fetch the target's memory map. */
1712
1713VEC(mem_region_s) *
1714target_memory_map (void)
1715{
1716 VEC(mem_region_s) *result;
1717 struct mem_region *last_one, *this_one;
1718 int ix;
1719 struct target_ops *t;
1720
1721 if (targetdebug)
1722 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1723
1724 for (t = current_target.beneath; t != NULL; t = t->beneath)
1725 if (t->to_memory_map != NULL)
1726 break;
1727
1728 if (t == NULL)
1729 return NULL;
1730
1731 result = t->to_memory_map (t);
1732 if (result == NULL)
1733 return NULL;
1734
1735 qsort (VEC_address (mem_region_s, result),
1736 VEC_length (mem_region_s, result),
1737 sizeof (struct mem_region), mem_region_cmp);
1738
1739 /* Check that regions do not overlap. Simultaneously assign
1740 a numbering for the "mem" commands to use to refer to
1741 each region. */
1742 last_one = NULL;
1743 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1744 {
1745 this_one->number = ix;
1746
1747 if (last_one && last_one->hi > this_one->lo)
1748 {
1749 warning (_("Overlapping regions in memory map: ignoring"));
1750 VEC_free (mem_region_s, result);
1751 return NULL;
1752 }
1753 last_one = this_one;
1754 }
1755
1756 return result;
1757}
1758
a76d924d
DJ
1759void
1760target_flash_erase (ULONGEST address, LONGEST length)
1761{
1762 struct target_ops *t;
1763
1764 for (t = current_target.beneath; t != NULL; t = t->beneath)
1765 if (t->to_flash_erase != NULL)
5d502164
MS
1766 {
1767 if (targetdebug)
1768 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1769 hex_string (address), phex (length, 0));
1770 t->to_flash_erase (t, address, length);
1771 return;
1772 }
a76d924d
DJ
1773
1774 tcomplain ();
1775}
1776
1777void
1778target_flash_done (void)
1779{
1780 struct target_ops *t;
1781
1782 for (t = current_target.beneath; t != NULL; t = t->beneath)
1783 if (t->to_flash_done != NULL)
5d502164
MS
1784 {
1785 if (targetdebug)
1786 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1787 t->to_flash_done (t);
1788 return;
1789 }
a76d924d
DJ
1790
1791 tcomplain ();
1792}
1793
920d2a44
AC
1794static void
1795show_trust_readonly (struct ui_file *file, int from_tty,
1796 struct cmd_list_element *c, const char *value)
1797{
3e43a32a
MS
1798 fprintf_filtered (file,
1799 _("Mode for reading from readonly sections is %s.\n"),
920d2a44
AC
1800 value);
1801}
3a11626d 1802
1e3ff5ad
AC
1803/* More generic transfers. */
1804
9b409511 1805static enum target_xfer_status
8aa91c1e 1806default_xfer_partial (struct target_ops *ops, enum target_object object,
2bc416ba 1807 const char *annex, gdb_byte *readbuf,
9b409511
YQ
1808 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
1809 ULONGEST *xfered_len)
0088c768
AC
1810{
1811 if (object == TARGET_OBJECT_MEMORY
c8e73a31
AC
1812 && ops->deprecated_xfer_memory != NULL)
1813 /* If available, fall back to the target's
1814 "deprecated_xfer_memory" method. */
0088c768 1815 {
4b8a223f 1816 int xfered = -1;
5d502164 1817
0088c768 1818 errno = 0;
4b8a223f
AC
1819 if (writebuf != NULL)
1820 {
1821 void *buffer = xmalloc (len);
1822 struct cleanup *cleanup = make_cleanup (xfree, buffer);
5d502164 1823
4b8a223f 1824 memcpy (buffer, writebuf, len);
c8e73a31
AC
1825 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1826 1/*write*/, NULL, ops);
4b8a223f
AC
1827 do_cleanups (cleanup);
1828 }
1829 if (readbuf != NULL)
244e85c8
MS
1830 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1831 0/*read*/, NULL, ops);
0088c768 1832 if (xfered > 0)
9b409511
YQ
1833 {
1834 *xfered_len = (ULONGEST) xfered;
1835 return TARGET_XFER_E_IO;
1836 }
0088c768 1837 else if (xfered == 0 && errno == 0)
c8e73a31
AC
1838 /* "deprecated_xfer_memory" uses 0, cross checked against
1839 ERRNO as one indication of an error. */
9b409511 1840 return TARGET_XFER_EOF;
0088c768 1841 else
9b409511 1842 return TARGET_XFER_E_IO;
0088c768 1843 }
0088c768 1844 else
6b84065d
TT
1845 {
1846 gdb_assert (ops->beneath != NULL);
1847 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1848 readbuf, writebuf, offset, len,
1849 xfered_len);
1850 }
0088c768
AC
1851}
1852
7f79c47e 1853/* Target vector read/write partial wrapper functions. */
0088c768 1854
9b409511 1855static enum target_xfer_status
1e3ff5ad
AC
1856target_read_partial (struct target_ops *ops,
1857 enum target_object object,
1b0ba102 1858 const char *annex, gdb_byte *buf,
9b409511
YQ
1859 ULONGEST offset, ULONGEST len,
1860 ULONGEST *xfered_len)
1e3ff5ad 1861{
9b409511
YQ
1862 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len,
1863 xfered_len);
1e3ff5ad
AC
1864}
1865
8a55ffb0 1866static enum target_xfer_status
1e3ff5ad
AC
1867target_write_partial (struct target_ops *ops,
1868 enum target_object object,
1b0ba102 1869 const char *annex, const gdb_byte *buf,
9b409511 1870 ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
1e3ff5ad 1871{
9b409511
YQ
1872 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len,
1873 xfered_len);
1e3ff5ad
AC
1874}
1875
1876/* Wrappers to perform the full transfer. */
7f79c47e
DE
1877
1878/* For docs on target_read see target.h. */
1879
1e3ff5ad
AC
1880LONGEST
1881target_read (struct target_ops *ops,
1882 enum target_object object,
1b0ba102 1883 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1884 ULONGEST offset, LONGEST len)
1885{
1886 LONGEST xfered = 0;
5d502164 1887
1e3ff5ad
AC
1888 while (xfered < len)
1889 {
9b409511
YQ
1890 ULONGEST xfered_len;
1891 enum target_xfer_status status;
1892
1893 status = target_read_partial (ops, object, annex,
1894 (gdb_byte *) buf + xfered,
1895 offset + xfered, len - xfered,
1896 &xfered_len);
5d502164 1897
1e3ff5ad 1898 /* Call an observer, notifying them of the xfer progress? */
9b409511 1899 if (status == TARGET_XFER_EOF)
13547ab6 1900 return xfered;
9b409511
YQ
1901 else if (status == TARGET_XFER_OK)
1902 {
1903 xfered += xfered_len;
1904 QUIT;
1905 }
1906 else
0088c768 1907 return -1;
9b409511 1908
1e3ff5ad
AC
1909 }
1910 return len;
1911}
1912
f1a507a1
JB
1913/* Assuming that the entire [begin, end) range of memory cannot be
1914 read, try to read whatever subrange is possible to read.
1915
1916 The function returns, in RESULT, either zero or one memory block.
1917 If there's a readable subrange at the beginning, it is completely
1918 read and returned. Any further readable subrange will not be read.
1919 Otherwise, if there's a readable subrange at the end, it will be
1920 completely read and returned. Any readable subranges before it
1921 (obviously, not starting at the beginning), will be ignored. In
1922 other cases -- either no readable subrange, or readable subrange(s)
1923 that is neither at the beginning, or end, nothing is returned.
1924
1925 The purpose of this function is to handle a read across a boundary
1926 of accessible memory in a case when memory map is not available.
1927 The above restrictions are fine for this case, but will give
1928 incorrect results if the memory is 'patchy'. However, supporting
1929 'patchy' memory would require trying to read every single byte,
1930 and it seems unacceptable solution. Explicit memory map is
1931 recommended for this case -- and target_read_memory_robust will
1932 take care of reading multiple ranges then. */
8dedea02
VP
1933
1934static void
3e43a32a
MS
1935read_whatever_is_readable (struct target_ops *ops,
1936 ULONGEST begin, ULONGEST end,
8dedea02 1937 VEC(memory_read_result_s) **result)
d5086790 1938{
f1a507a1 1939 gdb_byte *buf = xmalloc (end - begin);
8dedea02
VP
1940 ULONGEST current_begin = begin;
1941 ULONGEST current_end = end;
1942 int forward;
1943 memory_read_result_s r;
9b409511 1944 ULONGEST xfered_len;
8dedea02
VP
1945
1946 /* If we previously failed to read 1 byte, nothing can be done here. */
1947 if (end - begin <= 1)
13b3fd9b
MS
1948 {
1949 xfree (buf);
1950 return;
1951 }
8dedea02
VP
1952
1953 /* Check that either first or the last byte is readable, and give up
c378eb4e 1954 if not. This heuristic is meant to permit reading accessible memory
8dedea02
VP
1955 at the boundary of accessible region. */
1956 if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
9b409511 1957 buf, begin, 1, &xfered_len) == TARGET_XFER_OK)
8dedea02
VP
1958 {
1959 forward = 1;
1960 ++current_begin;
1961 }
1962 else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
9b409511
YQ
1963 buf + (end-begin) - 1, end - 1, 1,
1964 &xfered_len) == TARGET_XFER_OK)
8dedea02
VP
1965 {
1966 forward = 0;
1967 --current_end;
1968 }
1969 else
1970 {
13b3fd9b 1971 xfree (buf);
8dedea02
VP
1972 return;
1973 }
1974
1975 /* Loop invariant is that the [current_begin, current_end) was previously
1976 found to be not readable as a whole.
1977
1978 Note loop condition -- if the range has 1 byte, we can't divide the range
1979 so there's no point trying further. */
1980 while (current_end - current_begin > 1)
1981 {
1982 ULONGEST first_half_begin, first_half_end;
1983 ULONGEST second_half_begin, second_half_end;
1984 LONGEST xfer;
8dedea02 1985 ULONGEST middle = current_begin + (current_end - current_begin)/2;
f1a507a1 1986
8dedea02
VP
1987 if (forward)
1988 {
1989 first_half_begin = current_begin;
1990 first_half_end = middle;
1991 second_half_begin = middle;
1992 second_half_end = current_end;
1993 }
1994 else
1995 {
1996 first_half_begin = middle;
1997 first_half_end = current_end;
1998 second_half_begin = current_begin;
1999 second_half_end = middle;
2000 }
2001
2002 xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2003 buf + (first_half_begin - begin),
2004 first_half_begin,
2005 first_half_end - first_half_begin);
2006
2007 if (xfer == first_half_end - first_half_begin)
2008 {
c378eb4e 2009 /* This half reads up fine. So, the error must be in the
3e43a32a 2010 other half. */
8dedea02
VP
2011 current_begin = second_half_begin;
2012 current_end = second_half_end;
2013 }
2014 else
2015 {
c378eb4e
MS
2016 /* This half is not readable. Because we've tried one byte, we
2017 know some part of this half if actually redable. Go to the next
8dedea02
VP
2018 iteration to divide again and try to read.
2019
2020 We don't handle the other half, because this function only tries
2021 to read a single readable subrange. */
2022 current_begin = first_half_begin;
2023 current_end = first_half_end;
2024 }
2025 }
2026
2027 if (forward)
2028 {
2029 /* The [begin, current_begin) range has been read. */
2030 r.begin = begin;
2031 r.end = current_begin;
2032 r.data = buf;
2033 }
2034 else
2035 {
2036 /* The [current_end, end) range has been read. */
2037 LONGEST rlen = end - current_end;
f1a507a1 2038
8dedea02
VP
2039 r.data = xmalloc (rlen);
2040 memcpy (r.data, buf + current_end - begin, rlen);
2041 r.begin = current_end;
2042 r.end = end;
2043 xfree (buf);
2044 }
2045 VEC_safe_push(memory_read_result_s, (*result), &r);
2046}
2047
2048void
2049free_memory_read_result_vector (void *x)
2050{
2051 VEC(memory_read_result_s) *v = x;
2052 memory_read_result_s *current;
2053 int ix;
2054
2055 for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
2056 {
2057 xfree (current->data);
2058 }
2059 VEC_free (memory_read_result_s, v);
2060}
2061
2062VEC(memory_read_result_s) *
2063read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
2064{
2065 VEC(memory_read_result_s) *result = 0;
2066
2067 LONGEST xfered = 0;
d5086790
VP
2068 while (xfered < len)
2069 {
8dedea02
VP
2070 struct mem_region *region = lookup_mem_region (offset + xfered);
2071 LONGEST rlen;
5d502164 2072
8dedea02
VP
2073 /* If there is no explicit region, a fake one should be created. */
2074 gdb_assert (region);
2075
2076 if (region->hi == 0)
2077 rlen = len - xfered;
2078 else
2079 rlen = region->hi - offset;
2080
2081 if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
d5086790 2082 {
c378eb4e 2083 /* Cannot read this region. Note that we can end up here only
8dedea02
VP
2084 if the region is explicitly marked inaccessible, or
2085 'inaccessible-by-default' is in effect. */
2086 xfered += rlen;
2087 }
2088 else
2089 {
2090 LONGEST to_read = min (len - xfered, rlen);
2091 gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
2092
2093 LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2094 (gdb_byte *) buffer,
2095 offset + xfered, to_read);
2096 /* Call an observer, notifying them of the xfer progress? */
d5086790 2097 if (xfer <= 0)
d5086790 2098 {
c378eb4e 2099 /* Got an error reading full chunk. See if maybe we can read
8dedea02
VP
2100 some subrange. */
2101 xfree (buffer);
3e43a32a
MS
2102 read_whatever_is_readable (ops, offset + xfered,
2103 offset + xfered + to_read, &result);
8dedea02 2104 xfered += to_read;
d5086790 2105 }
8dedea02
VP
2106 else
2107 {
2108 struct memory_read_result r;
2109 r.data = buffer;
2110 r.begin = offset + xfered;
2111 r.end = r.begin + xfer;
2112 VEC_safe_push (memory_read_result_s, result, &r);
2113 xfered += xfer;
2114 }
2115 QUIT;
d5086790 2116 }
d5086790 2117 }
8dedea02 2118 return result;
d5086790
VP
2119}
2120
8dedea02 2121
cf7a04e8
DJ
2122/* An alternative to target_write with progress callbacks. */
2123
1e3ff5ad 2124LONGEST
cf7a04e8
DJ
2125target_write_with_progress (struct target_ops *ops,
2126 enum target_object object,
2127 const char *annex, const gdb_byte *buf,
2128 ULONGEST offset, LONGEST len,
2129 void (*progress) (ULONGEST, void *), void *baton)
1e3ff5ad
AC
2130{
2131 LONGEST xfered = 0;
a76d924d
DJ
2132
2133 /* Give the progress callback a chance to set up. */
2134 if (progress)
2135 (*progress) (0, baton);
2136
1e3ff5ad
AC
2137 while (xfered < len)
2138 {
9b409511
YQ
2139 ULONGEST xfered_len;
2140 enum target_xfer_status status;
2141
2142 status = target_write_partial (ops, object, annex,
2143 (gdb_byte *) buf + xfered,
2144 offset + xfered, len - xfered,
2145 &xfered_len);
cf7a04e8 2146
9b409511 2147 if (status == TARGET_XFER_EOF)
13547ab6 2148 return xfered;
9b409511 2149 if (TARGET_XFER_STATUS_ERROR_P (status))
0088c768 2150 return -1;
cf7a04e8 2151
9b409511 2152 gdb_assert (status == TARGET_XFER_OK);
cf7a04e8 2153 if (progress)
9b409511 2154 (*progress) (xfered_len, baton);
cf7a04e8 2155
9b409511 2156 xfered += xfered_len;
1e3ff5ad
AC
2157 QUIT;
2158 }
2159 return len;
2160}
2161
7f79c47e
DE
2162/* For docs on target_write see target.h. */
2163
cf7a04e8
DJ
2164LONGEST
2165target_write (struct target_ops *ops,
2166 enum target_object object,
2167 const char *annex, const gdb_byte *buf,
2168 ULONGEST offset, LONGEST len)
2169{
2170 return target_write_with_progress (ops, object, annex, buf, offset, len,
2171 NULL, NULL);
2172}
2173
159f81f3
DJ
2174/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2175 the size of the transferred data. PADDING additional bytes are
2176 available in *BUF_P. This is a helper function for
2177 target_read_alloc; see the declaration of that function for more
2178 information. */
13547ab6 2179
159f81f3
DJ
2180static LONGEST
2181target_read_alloc_1 (struct target_ops *ops, enum target_object object,
2182 const char *annex, gdb_byte **buf_p, int padding)
13547ab6
DJ
2183{
2184 size_t buf_alloc, buf_pos;
2185 gdb_byte *buf;
13547ab6
DJ
2186
2187 /* This function does not have a length parameter; it reads the
2188 entire OBJECT). Also, it doesn't support objects fetched partly
2189 from one target and partly from another (in a different stratum,
2190 e.g. a core file and an executable). Both reasons make it
2191 unsuitable for reading memory. */
2192 gdb_assert (object != TARGET_OBJECT_MEMORY);
2193
2194 /* Start by reading up to 4K at a time. The target will throttle
2195 this number down if necessary. */
2196 buf_alloc = 4096;
2197 buf = xmalloc (buf_alloc);
2198 buf_pos = 0;
2199 while (1)
2200 {
9b409511
YQ
2201 ULONGEST xfered_len;
2202 enum target_xfer_status status;
2203
2204 status = target_read_partial (ops, object, annex, &buf[buf_pos],
2205 buf_pos, buf_alloc - buf_pos - padding,
2206 &xfered_len);
2207
2208 if (status == TARGET_XFER_EOF)
13547ab6
DJ
2209 {
2210 /* Read all there was. */
2211 if (buf_pos == 0)
2212 xfree (buf);
2213 else
2214 *buf_p = buf;
2215 return buf_pos;
2216 }
9b409511
YQ
2217 else if (status != TARGET_XFER_OK)
2218 {
2219 /* An error occurred. */
2220 xfree (buf);
2221 return TARGET_XFER_E_IO;
2222 }
13547ab6 2223
9b409511 2224 buf_pos += xfered_len;
13547ab6
DJ
2225
2226 /* If the buffer is filling up, expand it. */
2227 if (buf_alloc < buf_pos * 2)
2228 {
2229 buf_alloc *= 2;
2230 buf = xrealloc (buf, buf_alloc);
2231 }
2232
2233 QUIT;
2234 }
2235}
2236
159f81f3
DJ
2237/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2238 the size of the transferred data. See the declaration in "target.h"
2239 function for more information about the return value. */
2240
2241LONGEST
2242target_read_alloc (struct target_ops *ops, enum target_object object,
2243 const char *annex, gdb_byte **buf_p)
2244{
2245 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
2246}
2247
2248/* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
2249 returned as a string, allocated using xmalloc. If an error occurs
2250 or the transfer is unsupported, NULL is returned. Empty objects
2251 are returned as allocated but empty strings. A warning is issued
2252 if the result contains any embedded NUL bytes. */
2253
2254char *
2255target_read_stralloc (struct target_ops *ops, enum target_object object,
2256 const char *annex)
2257{
39086a0e
PA
2258 gdb_byte *buffer;
2259 char *bufstr;
7313baad 2260 LONGEST i, transferred;
159f81f3 2261
39086a0e
PA
2262 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
2263 bufstr = (char *) buffer;
159f81f3
DJ
2264
2265 if (transferred < 0)
2266 return NULL;
2267
2268 if (transferred == 0)
2269 return xstrdup ("");
2270
39086a0e 2271 bufstr[transferred] = 0;
7313baad
UW
2272
2273 /* Check for embedded NUL bytes; but allow trailing NULs. */
39086a0e
PA
2274 for (i = strlen (bufstr); i < transferred; i++)
2275 if (bufstr[i] != 0)
7313baad
UW
2276 {
2277 warning (_("target object %d, annex %s, "
2278 "contained unexpected null characters"),
2279 (int) object, annex ? annex : "(none)");
2280 break;
2281 }
159f81f3 2282
39086a0e 2283 return bufstr;
159f81f3
DJ
2284}
2285
b6591e8b
AC
2286/* Memory transfer methods. */
2287
2288void
1b0ba102 2289get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
b6591e8b
AC
2290 LONGEST len)
2291{
07b82ea5
PA
2292 /* This method is used to read from an alternate, non-current
2293 target. This read must bypass the overlay support (as symbols
2294 don't match this target), and GDB's internal cache (wrong cache
2295 for this target). */
2296 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
b6591e8b 2297 != len)
578d3588 2298 memory_error (TARGET_XFER_E_IO, addr);
b6591e8b
AC
2299}
2300
2301ULONGEST
5d502164
MS
2302get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2303 int len, enum bfd_endian byte_order)
b6591e8b 2304{
f6519ebc 2305 gdb_byte buf[sizeof (ULONGEST)];
b6591e8b
AC
2306
2307 gdb_assert (len <= sizeof (buf));
2308 get_target_memory (ops, addr, buf, len);
e17a4113 2309 return extract_unsigned_integer (buf, len, byte_order);
b6591e8b
AC
2310}
2311
3db08215
MM
2312/* See target.h. */
2313
d914c394
SS
2314int
2315target_insert_breakpoint (struct gdbarch *gdbarch,
2316 struct bp_target_info *bp_tgt)
2317{
2318 if (!may_insert_breakpoints)
2319 {
2320 warning (_("May not insert breakpoints"));
2321 return 1;
2322 }
2323
6b84065d
TT
2324 return current_target.to_insert_breakpoint (&current_target,
2325 gdbarch, bp_tgt);
d914c394
SS
2326}
2327
3db08215
MM
2328/* See target.h. */
2329
d914c394 2330int
6b84065d
TT
2331target_remove_breakpoint (struct gdbarch *gdbarch,
2332 struct bp_target_info *bp_tgt)
d914c394
SS
2333{
2334 /* This is kind of a weird case to handle, but the permission might
2335 have been changed after breakpoints were inserted - in which case
2336 we should just take the user literally and assume that any
2337 breakpoints should be left in place. */
2338 if (!may_insert_breakpoints)
2339 {
2340 warning (_("May not remove breakpoints"));
2341 return 1;
2342 }
2343
6b84065d
TT
2344 return current_target.to_remove_breakpoint (&current_target,
2345 gdbarch, bp_tgt);
d914c394
SS
2346}
2347
c906108c 2348static void
fba45db2 2349target_info (char *args, int from_tty)
c906108c
SS
2350{
2351 struct target_ops *t;
c906108c 2352 int has_all_mem = 0;
c5aa993b 2353
c906108c 2354 if (symfile_objfile != NULL)
4262abfb
JK
2355 printf_unfiltered (_("Symbols from \"%s\".\n"),
2356 objfile_name (symfile_objfile));
c906108c 2357
258b763a 2358 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 2359 {
c35b1492 2360 if (!(*t->to_has_memory) (t))
c906108c
SS
2361 continue;
2362
c5aa993b 2363 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
2364 continue;
2365 if (has_all_mem)
3e43a32a
MS
2366 printf_unfiltered (_("\tWhile running this, "
2367 "GDB does not access memory from...\n"));
c5aa993b
JM
2368 printf_unfiltered ("%s:\n", t->to_longname);
2369 (t->to_files_info) (t);
c35b1492 2370 has_all_mem = (*t->to_has_all_memory) (t);
c906108c
SS
2371 }
2372}
2373
fd79ecee
DJ
2374/* This function is called before any new inferior is created, e.g.
2375 by running a program, attaching, or connecting to a target.
2376 It cleans up any state from previous invocations which might
2377 change between runs. This is a subset of what target_preopen
2378 resets (things which might change between targets). */
2379
2380void
2381target_pre_inferior (int from_tty)
2382{
c378eb4e 2383 /* Clear out solib state. Otherwise the solib state of the previous
b9db4ced 2384 inferior might have survived and is entirely wrong for the new
c378eb4e 2385 target. This has been observed on GNU/Linux using glibc 2.3. How
b9db4ced
UW
2386 to reproduce:
2387
2388 bash$ ./foo&
2389 [1] 4711
2390 bash$ ./foo&
2391 [1] 4712
2392 bash$ gdb ./foo
2393 [...]
2394 (gdb) attach 4711
2395 (gdb) detach
2396 (gdb) attach 4712
2397 Cannot access memory at address 0xdeadbeef
2398 */
b9db4ced 2399
50c71eaf
PA
2400 /* In some OSs, the shared library list is the same/global/shared
2401 across inferiors. If code is shared between processes, so are
2402 memory regions and features. */
f5656ead 2403 if (!gdbarch_has_global_solist (target_gdbarch ()))
50c71eaf
PA
2404 {
2405 no_shared_libraries (NULL, from_tty);
2406
2407 invalidate_target_mem_regions ();
424163ea 2408
50c71eaf
PA
2409 target_clear_description ();
2410 }
8ffcbaaf
YQ
2411
2412 agent_capability_invalidate ();
fd79ecee
DJ
2413}
2414
b8fa0bfa
PA
2415/* Callback for iterate_over_inferiors. Gets rid of the given
2416 inferior. */
2417
2418static int
2419dispose_inferior (struct inferior *inf, void *args)
2420{
2421 struct thread_info *thread;
2422
2423 thread = any_thread_of_process (inf->pid);
2424 if (thread)
2425 {
2426 switch_to_thread (thread->ptid);
2427
2428 /* Core inferiors actually should be detached, not killed. */
2429 if (target_has_execution)
2430 target_kill ();
2431 else
2432 target_detach (NULL, 0);
2433 }
2434
2435 return 0;
2436}
2437
c906108c
SS
2438/* This is to be called by the open routine before it does
2439 anything. */
2440
2441void
fba45db2 2442target_preopen (int from_tty)
c906108c 2443{
c5aa993b 2444 dont_repeat ();
c906108c 2445
b8fa0bfa 2446 if (have_inferiors ())
c5aa993b 2447 {
adf40b2e 2448 if (!from_tty
b8fa0bfa
PA
2449 || !have_live_inferiors ()
2450 || query (_("A program is being debugged already. Kill it? ")))
2451 iterate_over_inferiors (dispose_inferior, NULL);
c906108c 2452 else
8a3fe4f8 2453 error (_("Program not killed."));
c906108c
SS
2454 }
2455
2456 /* Calling target_kill may remove the target from the stack. But if
2457 it doesn't (which seems like a win for UDI), remove it now. */
87ab71f0
PA
2458 /* Leave the exec target, though. The user may be switching from a
2459 live process to a core of the same program. */
460014f5 2460 pop_all_targets_above (file_stratum);
fd79ecee
DJ
2461
2462 target_pre_inferior (from_tty);
c906108c
SS
2463}
2464
2465/* Detach a target after doing deferred register stores. */
2466
2467void
52554a0e 2468target_detach (const char *args, int from_tty)
c906108c 2469{
136d6dae
VP
2470 struct target_ops* t;
2471
f5656ead 2472 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
2473 /* Don't remove global breakpoints here. They're removed on
2474 disconnection from the target. */
2475 ;
2476 else
2477 /* If we're in breakpoints-always-inserted mode, have to remove
2478 them before detaching. */
dfd4cc63 2479 remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
74960c60 2480
24291992
PA
2481 prepare_for_detach ();
2482
09da0d0a
TT
2483 current_target.to_detach (&current_target, args, from_tty);
2484 if (targetdebug)
2485 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2486 args, from_tty);
c906108c
SS
2487}
2488
6ad8ae5c
DJ
2489void
2490target_disconnect (char *args, int from_tty)
2491{
597320e7
DJ
2492 struct target_ops *t;
2493
50c71eaf
PA
2494 /* If we're in breakpoints-always-inserted mode or if breakpoints
2495 are global across processes, we have to remove them before
2496 disconnecting. */
74960c60
VP
2497 remove_breakpoints ();
2498
597320e7
DJ
2499 for (t = current_target.beneath; t != NULL; t = t->beneath)
2500 if (t->to_disconnect != NULL)
2501 {
2502 if (targetdebug)
2503 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2504 args, from_tty);
2505 t->to_disconnect (t, args, from_tty);
2506 return;
2507 }
2508
2509 tcomplain ();
6ad8ae5c
DJ
2510}
2511
117de6a9 2512ptid_t
47608cb1 2513target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
117de6a9
PA
2514{
2515 struct target_ops *t;
6b84065d
TT
2516 ptid_t retval = (current_target.to_wait) (&current_target, ptid,
2517 status, options);
117de6a9 2518
6b84065d 2519 if (targetdebug)
117de6a9 2520 {
6b84065d
TT
2521 char *status_string;
2522 char *options_string;
117de6a9 2523
6b84065d
TT
2524 status_string = target_waitstatus_to_string (status);
2525 options_string = target_options_to_string (options);
2526 fprintf_unfiltered (gdb_stdlog,
2527 "target_wait (%d, status, options={%s})"
2528 " = %d, %s\n",
2529 ptid_get_pid (ptid), options_string,
2530 ptid_get_pid (retval), status_string);
2531 xfree (status_string);
2532 xfree (options_string);
117de6a9
PA
2533 }
2534
6b84065d 2535 return retval;
117de6a9
PA
2536}
2537
2538char *
2539target_pid_to_str (ptid_t ptid)
2540{
2541 struct target_ops *t;
2542
2543 for (t = current_target.beneath; t != NULL; t = t->beneath)
2544 {
2545 if (t->to_pid_to_str != NULL)
2546 return (*t->to_pid_to_str) (t, ptid);
2547 }
2548
2549 return normal_pid_to_str (ptid);
2550}
2551
4694da01
TT
2552char *
2553target_thread_name (struct thread_info *info)
2554{
825828fc 2555 return current_target.to_thread_name (&current_target, info);
4694da01
TT
2556}
2557
e1ac3328 2558void
2ea28649 2559target_resume (ptid_t ptid, int step, enum gdb_signal signal)
e1ac3328 2560{
28439f5e
PA
2561 struct target_ops *t;
2562
4e5d721f 2563 target_dcache_invalidate ();
28439f5e 2564
6b84065d
TT
2565 current_target.to_resume (&current_target, ptid, step, signal);
2566 if (targetdebug)
2567 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2568 ptid_get_pid (ptid),
2569 step ? "step" : "continue",
2570 gdb_signal_to_name (signal));
28439f5e 2571
6b84065d
TT
2572 registers_changed_ptid (ptid);
2573 set_executing (ptid, 1);
2574 set_running (ptid, 1);
2575 clear_inline_frame_state (ptid);
e1ac3328 2576}
2455069d
UW
2577
2578void
2579target_pass_signals (int numsigs, unsigned char *pass_signals)
2580{
2581 struct target_ops *t;
2582
2583 for (t = current_target.beneath; t != NULL; t = t->beneath)
2584 {
2585 if (t->to_pass_signals != NULL)
2586 {
2587 if (targetdebug)
2588 {
2589 int i;
2590
2591 fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2592 numsigs);
2593
2594 for (i = 0; i < numsigs; i++)
2595 if (pass_signals[i])
2596 fprintf_unfiltered (gdb_stdlog, " %s",
2ea28649 2597 gdb_signal_to_name (i));
2455069d
UW
2598
2599 fprintf_unfiltered (gdb_stdlog, " })\n");
2600 }
2601
94bedb42 2602 (*t->to_pass_signals) (t, numsigs, pass_signals);
2455069d
UW
2603 return;
2604 }
2605 }
2606}
2607
9b224c5e
PA
2608void
2609target_program_signals (int numsigs, unsigned char *program_signals)
2610{
2611 struct target_ops *t;
2612
2613 for (t = current_target.beneath; t != NULL; t = t->beneath)
2614 {
2615 if (t->to_program_signals != NULL)
2616 {
2617 if (targetdebug)
2618 {
2619 int i;
2620
2621 fprintf_unfiltered (gdb_stdlog, "target_program_signals (%d, {",
2622 numsigs);
2623
2624 for (i = 0; i < numsigs; i++)
2625 if (program_signals[i])
2626 fprintf_unfiltered (gdb_stdlog, " %s",
2ea28649 2627 gdb_signal_to_name (i));
9b224c5e
PA
2628
2629 fprintf_unfiltered (gdb_stdlog, " })\n");
2630 }
2631
daf5e9b6 2632 (*t->to_program_signals) (t, numsigs, program_signals);
9b224c5e
PA
2633 return;
2634 }
2635 }
2636}
2637
098dba18
TT
2638static int
2639default_follow_fork (struct target_ops *self, int follow_child,
2640 int detach_fork)
2641{
2642 /* Some target returned a fork event, but did not know how to follow it. */
2643 internal_error (__FILE__, __LINE__,
2644 _("could not find a target to follow fork"));
2645}
2646
ee057212
DJ
2647/* Look through the list of possible targets for a target that can
2648 follow forks. */
2649
2650int
07107ca6 2651target_follow_fork (int follow_child, int detach_fork)
ee057212 2652{
098dba18
TT
2653 int retval = current_target.to_follow_fork (&current_target,
2654 follow_child, detach_fork);
ee057212 2655
098dba18
TT
2656 if (targetdebug)
2657 fprintf_unfiltered (gdb_stdlog,
2658 "target_follow_fork (%d, %d) = %d\n",
2659 follow_child, detach_fork, retval);
2660 return retval;
ee057212
DJ
2661}
2662
136d6dae
VP
2663void
2664target_mourn_inferior (void)
2665{
2666 struct target_ops *t;
5d502164 2667
136d6dae
VP
2668 for (t = current_target.beneath; t != NULL; t = t->beneath)
2669 {
2670 if (t->to_mourn_inferior != NULL)
2671 {
2672 t->to_mourn_inferior (t);
947b8855
PA
2673 if (targetdebug)
2674 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2675
2676 /* We no longer need to keep handles on any of the object files.
2677 Make sure to release them to avoid unnecessarily locking any
2678 of them while we're not actually debugging. */
2679 bfd_cache_close_all ();
2680
136d6dae
VP
2681 return;
2682 }
2683 }
2684
2685 internal_error (__FILE__, __LINE__,
9b20d036 2686 _("could not find a target to follow mourn inferior"));
136d6dae
VP
2687}
2688
424163ea
DJ
2689/* Look for a target which can describe architectural features, starting
2690 from TARGET. If we find one, return its description. */
2691
2692const struct target_desc *
2693target_read_description (struct target_ops *target)
2694{
2695 struct target_ops *t;
2696
2697 for (t = target; t != NULL; t = t->beneath)
2698 if (t->to_read_description != NULL)
2699 {
2700 const struct target_desc *tdesc;
2701
2702 tdesc = t->to_read_description (t);
2703 if (tdesc)
2704 return tdesc;
2705 }
2706
2707 return NULL;
2708}
2709
08388c79
DE
2710/* The default implementation of to_search_memory.
2711 This implements a basic search of memory, reading target memory and
2712 performing the search here (as opposed to performing the search in on the
2713 target side with, for example, gdbserver). */
2714
2715int
2716simple_search_memory (struct target_ops *ops,
2717 CORE_ADDR start_addr, ULONGEST search_space_len,
2718 const gdb_byte *pattern, ULONGEST pattern_len,
2719 CORE_ADDR *found_addrp)
2720{
2721 /* NOTE: also defined in find.c testcase. */
2722#define SEARCH_CHUNK_SIZE 16000
2723 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2724 /* Buffer to hold memory contents for searching. */
2725 gdb_byte *search_buf;
2726 unsigned search_buf_size;
2727 struct cleanup *old_cleanups;
2728
2729 search_buf_size = chunk_size + pattern_len - 1;
2730
2731 /* No point in trying to allocate a buffer larger than the search space. */
2732 if (search_space_len < search_buf_size)
2733 search_buf_size = search_space_len;
2734
2735 search_buf = malloc (search_buf_size);
2736 if (search_buf == NULL)
5e1471f5 2737 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2738 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2739
2740 /* Prime the search buffer. */
2741
2742 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2743 search_buf, start_addr, search_buf_size) != search_buf_size)
2744 {
b3dc46ff
AB
2745 warning (_("Unable to access %s bytes of target "
2746 "memory at %s, halting search."),
2747 pulongest (search_buf_size), hex_string (start_addr));
08388c79
DE
2748 do_cleanups (old_cleanups);
2749 return -1;
2750 }
2751
2752 /* Perform the search.
2753
2754 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2755 When we've scanned N bytes we copy the trailing bytes to the start and
2756 read in another N bytes. */
2757
2758 while (search_space_len >= pattern_len)
2759 {
2760 gdb_byte *found_ptr;
2761 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2762
2763 found_ptr = memmem (search_buf, nr_search_bytes,
2764 pattern, pattern_len);
2765
2766 if (found_ptr != NULL)
2767 {
2768 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
5d502164 2769
08388c79
DE
2770 *found_addrp = found_addr;
2771 do_cleanups (old_cleanups);
2772 return 1;
2773 }
2774
2775 /* Not found in this chunk, skip to next chunk. */
2776
2777 /* Don't let search_space_len wrap here, it's unsigned. */
2778 if (search_space_len >= chunk_size)
2779 search_space_len -= chunk_size;
2780 else
2781 search_space_len = 0;
2782
2783 if (search_space_len >= pattern_len)
2784 {
2785 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2786 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2787 int nr_to_read;
2788
2789 /* Copy the trailing part of the previous iteration to the front
2790 of the buffer for the next iteration. */
2791 gdb_assert (keep_len == pattern_len - 1);
2792 memcpy (search_buf, search_buf + chunk_size, keep_len);
2793
2794 nr_to_read = min (search_space_len - keep_len, chunk_size);
2795
2796 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2797 search_buf + keep_len, read_addr,
2798 nr_to_read) != nr_to_read)
2799 {
b3dc46ff 2800 warning (_("Unable to access %s bytes of target "
9b20d036 2801 "memory at %s, halting search."),
b3dc46ff 2802 plongest (nr_to_read),
08388c79
DE
2803 hex_string (read_addr));
2804 do_cleanups (old_cleanups);
2805 return -1;
2806 }
2807
2808 start_addr += chunk_size;
2809 }
2810 }
2811
2812 /* Not found. */
2813
2814 do_cleanups (old_cleanups);
2815 return 0;
2816}
2817
2818/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2819 sequence of bytes in PATTERN with length PATTERN_LEN.
2820
2821 The result is 1 if found, 0 if not found, and -1 if there was an error
2822 requiring halting of the search (e.g. memory read error).
2823 If the pattern is found the address is recorded in FOUND_ADDRP. */
2824
2825int
2826target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2827 const gdb_byte *pattern, ULONGEST pattern_len,
2828 CORE_ADDR *found_addrp)
2829{
2830 struct target_ops *t;
2831 int found;
2832
2833 /* We don't use INHERIT to set current_target.to_search_memory,
2834 so we have to scan the target stack and handle targetdebug
2835 ourselves. */
2836
2837 if (targetdebug)
2838 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2839 hex_string (start_addr));
2840
2841 for (t = current_target.beneath; t != NULL; t = t->beneath)
2842 if (t->to_search_memory != NULL)
2843 break;
2844
2845 if (t != NULL)
2846 {
2847 found = t->to_search_memory (t, start_addr, search_space_len,
2848 pattern, pattern_len, found_addrp);
2849 }
2850 else
2851 {
2852 /* If a special version of to_search_memory isn't available, use the
2853 simple version. */
c35b1492 2854 found = simple_search_memory (current_target.beneath,
08388c79
DE
2855 start_addr, search_space_len,
2856 pattern, pattern_len, found_addrp);
2857 }
2858
2859 if (targetdebug)
2860 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2861
2862 return found;
2863}
2864
8edfe269
DJ
2865/* Look through the currently pushed targets. If none of them will
2866 be able to restart the currently running process, issue an error
2867 message. */
2868
2869void
2870target_require_runnable (void)
2871{
2872 struct target_ops *t;
2873
2874 for (t = target_stack; t != NULL; t = t->beneath)
2875 {
2876 /* If this target knows how to create a new program, then
2877 assume we will still be able to after killing the current
2878 one. Either killing and mourning will not pop T, or else
2879 find_default_run_target will find it again. */
2880 if (t->to_create_inferior != NULL)
2881 return;
2882
2883 /* Do not worry about thread_stratum targets that can not
2884 create inferiors. Assume they will be pushed again if
2885 necessary, and continue to the process_stratum. */
85e747d2
UW
2886 if (t->to_stratum == thread_stratum
2887 || t->to_stratum == arch_stratum)
8edfe269
DJ
2888 continue;
2889
3e43a32a
MS
2890 error (_("The \"%s\" target does not support \"run\". "
2891 "Try \"help target\" or \"continue\"."),
8edfe269
DJ
2892 t->to_shortname);
2893 }
2894
2895 /* This function is only called if the target is running. In that
2896 case there should have been a process_stratum target and it
c378eb4e 2897 should either know how to create inferiors, or not... */
9b20d036 2898 internal_error (__FILE__, __LINE__, _("No targets found"));
8edfe269
DJ
2899}
2900
c906108c
SS
2901/* Look through the list of possible targets for a target that can
2902 execute a run or attach command without any other data. This is
2903 used to locate the default process stratum.
2904
5f667f2d
PA
2905 If DO_MESG is not NULL, the result is always valid (error() is
2906 called for errors); else, return NULL on error. */
c906108c
SS
2907
2908static struct target_ops *
fba45db2 2909find_default_run_target (char *do_mesg)
c906108c
SS
2910{
2911 struct target_ops **t;
2912 struct target_ops *runable = NULL;
2913 int count;
2914
2915 count = 0;
2916
2917 for (t = target_structs; t < target_structs + target_struct_size;
2918 ++t)
2919 {
c5aa993b 2920 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
2921 {
2922 runable = *t;
2923 ++count;
2924 }
2925 }
2926
2927 if (count != 1)
5f667f2d
PA
2928 {
2929 if (do_mesg)
2930 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2931 else
2932 return NULL;
2933 }
c906108c
SS
2934
2935 return runable;
2936}
2937
2938void
136d6dae 2939find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
2940{
2941 struct target_ops *t;
2942
c5aa993b 2943 t = find_default_run_target ("attach");
136d6dae 2944 (t->to_attach) (t, args, from_tty);
c906108c
SS
2945 return;
2946}
2947
c906108c 2948void
136d6dae
VP
2949find_default_create_inferior (struct target_ops *ops,
2950 char *exec_file, char *allargs, char **env,
c27cda74 2951 int from_tty)
c906108c
SS
2952{
2953 struct target_ops *t;
2954
c5aa993b 2955 t = find_default_run_target ("run");
136d6dae 2956 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
2957 return;
2958}
2959
2c0b251b 2960static int
6a109b6b 2961find_default_can_async_p (struct target_ops *ignore)
b84876c2
PA
2962{
2963 struct target_ops *t;
2964
5f667f2d
PA
2965 /* This may be called before the target is pushed on the stack;
2966 look for the default process stratum. If there's none, gdb isn't
2967 configured with a native debugger, and target remote isn't
2968 connected yet. */
2969 t = find_default_run_target (NULL);
6b84065d 2970 if (t && t->to_can_async_p != delegate_can_async_p)
6a109b6b 2971 return (t->to_can_async_p) (t);
b84876c2
PA
2972 return 0;
2973}
2974
2c0b251b 2975static int
6a109b6b 2976find_default_is_async_p (struct target_ops *ignore)
b84876c2
PA
2977{
2978 struct target_ops *t;
2979
5f667f2d
PA
2980 /* This may be called before the target is pushed on the stack;
2981 look for the default process stratum. If there's none, gdb isn't
2982 configured with a native debugger, and target remote isn't
2983 connected yet. */
2984 t = find_default_run_target (NULL);
6b84065d 2985 if (t && t->to_is_async_p != delegate_is_async_p)
6a109b6b 2986 return (t->to_is_async_p) (t);
b84876c2
PA
2987 return 0;
2988}
2989
2c0b251b 2990static int
2a9a2795 2991find_default_supports_non_stop (struct target_ops *self)
9908b566
VP
2992{
2993 struct target_ops *t;
2994
2995 t = find_default_run_target (NULL);
2996 if (t && t->to_supports_non_stop)
2a9a2795 2997 return (t->to_supports_non_stop) (t);
9908b566
VP
2998 return 0;
2999}
3000
3001int
2c0b251b 3002target_supports_non_stop (void)
9908b566
VP
3003{
3004 struct target_ops *t;
5d502164 3005
9908b566
VP
3006 for (t = &current_target; t != NULL; t = t->beneath)
3007 if (t->to_supports_non_stop)
2a9a2795 3008 return t->to_supports_non_stop (t);
9908b566
VP
3009
3010 return 0;
3011}
3012
145b16a9
UW
3013/* Implement the "info proc" command. */
3014
451b7c33 3015int
145b16a9
UW
3016target_info_proc (char *args, enum info_proc_what what)
3017{
3018 struct target_ops *t;
3019
3020 /* If we're already connected to something that can get us OS
3021 related data, use it. Otherwise, try using the native
3022 target. */
3023 if (current_target.to_stratum >= process_stratum)
3024 t = current_target.beneath;
3025 else
3026 t = find_default_run_target (NULL);
3027
3028 for (; t != NULL; t = t->beneath)
3029 {
3030 if (t->to_info_proc != NULL)
3031 {
3032 t->to_info_proc (t, args, what);
3033
3034 if (targetdebug)
3035 fprintf_unfiltered (gdb_stdlog,
3036 "target_info_proc (\"%s\", %d)\n", args, what);
3037
451b7c33 3038 return 1;
145b16a9
UW
3039 }
3040 }
3041
451b7c33 3042 return 0;
145b16a9
UW
3043}
3044
03583c20 3045static int
2bfc0540 3046find_default_supports_disable_randomization (struct target_ops *self)
03583c20
UW
3047{
3048 struct target_ops *t;
3049
3050 t = find_default_run_target (NULL);
3051 if (t && t->to_supports_disable_randomization)
2bfc0540 3052 return (t->to_supports_disable_randomization) (t);
03583c20
UW
3053 return 0;
3054}
3055
3056int
3057target_supports_disable_randomization (void)
3058{
3059 struct target_ops *t;
3060
3061 for (t = &current_target; t != NULL; t = t->beneath)
3062 if (t->to_supports_disable_randomization)
2bfc0540 3063 return t->to_supports_disable_randomization (t);
03583c20
UW
3064
3065 return 0;
3066}
9908b566 3067
07e059b5
VP
3068char *
3069target_get_osdata (const char *type)
3070{
07e059b5
VP
3071 struct target_ops *t;
3072
739ef7fb
PA
3073 /* If we're already connected to something that can get us OS
3074 related data, use it. Otherwise, try using the native
3075 target. */
3076 if (current_target.to_stratum >= process_stratum)
6d097e65 3077 t = current_target.beneath;
739ef7fb
PA
3078 else
3079 t = find_default_run_target ("get OS data");
07e059b5
VP
3080
3081 if (!t)
3082 return NULL;
3083
6d097e65 3084 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
3085}
3086
6c95b8df
PA
3087/* Determine the current address space of thread PTID. */
3088
3089struct address_space *
3090target_thread_address_space (ptid_t ptid)
3091{
c0694254 3092 struct address_space *aspace;
6c95b8df 3093 struct inferior *inf;
c0694254
PA
3094 struct target_ops *t;
3095
3096 for (t = current_target.beneath; t != NULL; t = t->beneath)
3097 {
3098 if (t->to_thread_address_space != NULL)
3099 {
3100 aspace = t->to_thread_address_space (t, ptid);
3101 gdb_assert (aspace);
6c95b8df 3102
c0694254
PA
3103 if (targetdebug)
3104 fprintf_unfiltered (gdb_stdlog,
3105 "target_thread_address_space (%s) = %d\n",
3106 target_pid_to_str (ptid),
3107 address_space_num (aspace));
3108 return aspace;
3109 }
3110 }
6c95b8df
PA
3111
3112 /* Fall-back to the "main" address space of the inferior. */
3113 inf = find_inferior_pid (ptid_get_pid (ptid));
3114
3115 if (inf == NULL || inf->aspace == NULL)
3e43a32a 3116 internal_error (__FILE__, __LINE__,
9b20d036
MS
3117 _("Can't determine the current "
3118 "address space of thread %s\n"),
6c95b8df
PA
3119 target_pid_to_str (ptid));
3120
3121 return inf->aspace;
3122}
3123
7313baad
UW
3124
3125/* Target file operations. */
3126
3127static struct target_ops *
3128default_fileio_target (void)
3129{
3130 /* If we're already connected to something that can perform
3131 file I/O, use it. Otherwise, try using the native target. */
3132 if (current_target.to_stratum >= process_stratum)
3133 return current_target.beneath;
3134 else
3135 return find_default_run_target ("file I/O");
3136}
3137
3138/* Open FILENAME on the target, using FLAGS and MODE. Return a
3139 target file descriptor, or -1 if an error occurs (and set
3140 *TARGET_ERRNO). */
3141int
3142target_fileio_open (const char *filename, int flags, int mode,
3143 int *target_errno)
3144{
3145 struct target_ops *t;
3146
3147 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3148 {
3149 if (t->to_fileio_open != NULL)
3150 {
cd897586 3151 int fd = t->to_fileio_open (t, filename, flags, mode, target_errno);
7313baad
UW
3152
3153 if (targetdebug)
3154 fprintf_unfiltered (gdb_stdlog,
3155 "target_fileio_open (%s,0x%x,0%o) = %d (%d)\n",
3156 filename, flags, mode,
3157 fd, fd != -1 ? 0 : *target_errno);
3158 return fd;
3159 }
3160 }
3161
3162 *target_errno = FILEIO_ENOSYS;
3163 return -1;
3164}
3165
3166/* Write up to LEN bytes from WRITE_BUF to FD on the target.
3167 Return the number of bytes written, or -1 if an error occurs
3168 (and set *TARGET_ERRNO). */
3169int
3170target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
3171 ULONGEST offset, int *target_errno)
3172{
3173 struct target_ops *t;
3174
3175 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3176 {
3177 if (t->to_fileio_pwrite != NULL)
3178 {
0d866f62 3179 int ret = t->to_fileio_pwrite (t, fd, write_buf, len, offset,
7313baad
UW
3180 target_errno);
3181
3182 if (targetdebug)
3183 fprintf_unfiltered (gdb_stdlog,
a71b5a38 3184 "target_fileio_pwrite (%d,...,%d,%s) "
7313baad 3185 "= %d (%d)\n",
a71b5a38 3186 fd, len, pulongest (offset),
7313baad
UW
3187 ret, ret != -1 ? 0 : *target_errno);
3188 return ret;
3189 }
3190 }
3191
3192 *target_errno = FILEIO_ENOSYS;
3193 return -1;
3194}
3195
3196/* Read up to LEN bytes FD on the target into READ_BUF.
3197 Return the number of bytes read, or -1 if an error occurs
3198 (and set *TARGET_ERRNO). */
3199int
3200target_fileio_pread (int fd, gdb_byte *read_buf, int len,
3201 ULONGEST offset, int *target_errno)
3202{
3203 struct target_ops *t;
3204
3205 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3206 {
3207 if (t->to_fileio_pread != NULL)
3208 {
a3be983c 3209 int ret = t->to_fileio_pread (t, fd, read_buf, len, offset,
7313baad
UW
3210 target_errno);
3211
3212 if (targetdebug)
3213 fprintf_unfiltered (gdb_stdlog,
a71b5a38 3214 "target_fileio_pread (%d,...,%d,%s) "
7313baad 3215 "= %d (%d)\n",
a71b5a38 3216 fd, len, pulongest (offset),
7313baad
UW
3217 ret, ret != -1 ? 0 : *target_errno);
3218 return ret;
3219 }
3220 }
3221
3222 *target_errno = FILEIO_ENOSYS;
3223 return -1;
3224}
3225
3226/* Close FD on the target. Return 0, or -1 if an error occurs
3227 (and set *TARGET_ERRNO). */
3228int
3229target_fileio_close (int fd, int *target_errno)
3230{
3231 struct target_ops *t;
3232
3233 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3234 {
3235 if (t->to_fileio_close != NULL)
3236 {
df39ea25 3237 int ret = t->to_fileio_close (t, fd, target_errno);
7313baad
UW
3238
3239 if (targetdebug)
3240 fprintf_unfiltered (gdb_stdlog,
3241 "target_fileio_close (%d) = %d (%d)\n",
3242 fd, ret, ret != -1 ? 0 : *target_errno);
3243 return ret;
3244 }
3245 }
3246
3247 *target_errno = FILEIO_ENOSYS;
3248 return -1;
3249}
3250
3251/* Unlink FILENAME on the target. Return 0, or -1 if an error
3252 occurs (and set *TARGET_ERRNO). */
3253int
3254target_fileio_unlink (const char *filename, int *target_errno)
3255{
3256 struct target_ops *t;
3257
3258 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3259 {
3260 if (t->to_fileio_unlink != NULL)
3261 {
dbbca37d 3262 int ret = t->to_fileio_unlink (t, filename, target_errno);
7313baad
UW
3263
3264 if (targetdebug)
3265 fprintf_unfiltered (gdb_stdlog,
3266 "target_fileio_unlink (%s) = %d (%d)\n",
3267 filename, ret, ret != -1 ? 0 : *target_errno);
3268 return ret;
3269 }
3270 }
3271
3272 *target_errno = FILEIO_ENOSYS;
3273 return -1;
3274}
3275
b9e7b9c3
UW
3276/* Read value of symbolic link FILENAME on the target. Return a
3277 null-terminated string allocated via xmalloc, or NULL if an error
3278 occurs (and set *TARGET_ERRNO). */
3279char *
3280target_fileio_readlink (const char *filename, int *target_errno)
3281{
3282 struct target_ops *t;
3283
3284 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3285 {
3286 if (t->to_fileio_readlink != NULL)
3287 {
fab5aa7c 3288 char *ret = t->to_fileio_readlink (t, filename, target_errno);
b9e7b9c3
UW
3289
3290 if (targetdebug)
3291 fprintf_unfiltered (gdb_stdlog,
3292 "target_fileio_readlink (%s) = %s (%d)\n",
3293 filename, ret? ret : "(nil)",
3294 ret? 0 : *target_errno);
3295 return ret;
3296 }
3297 }
3298
3299 *target_errno = FILEIO_ENOSYS;
3300 return NULL;
3301}
3302
7313baad
UW
3303static void
3304target_fileio_close_cleanup (void *opaque)
3305{
3306 int fd = *(int *) opaque;
3307 int target_errno;
3308
3309 target_fileio_close (fd, &target_errno);
3310}
3311
3312/* Read target file FILENAME. Store the result in *BUF_P and
3313 return the size of the transferred data. PADDING additional bytes are
3314 available in *BUF_P. This is a helper function for
3315 target_fileio_read_alloc; see the declaration of that function for more
3316 information. */
3317
3318static LONGEST
3319target_fileio_read_alloc_1 (const char *filename,
3320 gdb_byte **buf_p, int padding)
3321{
3322 struct cleanup *close_cleanup;
3323 size_t buf_alloc, buf_pos;
3324 gdb_byte *buf;
3325 LONGEST n;
3326 int fd;
3327 int target_errno;
3328
3329 fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
3330 if (fd == -1)
3331 return -1;
3332
3333 close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
3334
3335 /* Start by reading up to 4K at a time. The target will throttle
3336 this number down if necessary. */
3337 buf_alloc = 4096;
3338 buf = xmalloc (buf_alloc);
3339 buf_pos = 0;
3340 while (1)
3341 {
3342 n = target_fileio_pread (fd, &buf[buf_pos],
3343 buf_alloc - buf_pos - padding, buf_pos,
3344 &target_errno);
3345 if (n < 0)
3346 {
3347 /* An error occurred. */
3348 do_cleanups (close_cleanup);
3349 xfree (buf);
3350 return -1;
3351 }
3352 else if (n == 0)
3353 {
3354 /* Read all there was. */
3355 do_cleanups (close_cleanup);
3356 if (buf_pos == 0)
3357 xfree (buf);
3358 else
3359 *buf_p = buf;
3360 return buf_pos;
3361 }
3362
3363 buf_pos += n;
3364
3365 /* If the buffer is filling up, expand it. */
3366 if (buf_alloc < buf_pos * 2)
3367 {
3368 buf_alloc *= 2;
3369 buf = xrealloc (buf, buf_alloc);
3370 }
3371
3372 QUIT;
3373 }
3374}
3375
3376/* Read target file FILENAME. Store the result in *BUF_P and return
3377 the size of the transferred data. See the declaration in "target.h"
3378 function for more information about the return value. */
3379
3380LONGEST
3381target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
3382{
3383 return target_fileio_read_alloc_1 (filename, buf_p, 0);
3384}
3385
3386/* Read target file FILENAME. The result is NUL-terminated and
3387 returned as a string, allocated using xmalloc. If an error occurs
3388 or the transfer is unsupported, NULL is returned. Empty objects
3389 are returned as allocated but empty strings. A warning is issued
3390 if the result contains any embedded NUL bytes. */
3391
3392char *
3393target_fileio_read_stralloc (const char *filename)
3394{
39086a0e
PA
3395 gdb_byte *buffer;
3396 char *bufstr;
7313baad
UW
3397 LONGEST i, transferred;
3398
39086a0e
PA
3399 transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
3400 bufstr = (char *) buffer;
7313baad
UW
3401
3402 if (transferred < 0)
3403 return NULL;
3404
3405 if (transferred == 0)
3406 return xstrdup ("");
3407
39086a0e 3408 bufstr[transferred] = 0;
7313baad
UW
3409
3410 /* Check for embedded NUL bytes; but allow trailing NULs. */
39086a0e
PA
3411 for (i = strlen (bufstr); i < transferred; i++)
3412 if (bufstr[i] != 0)
7313baad
UW
3413 {
3414 warning (_("target file %s "
3415 "contained unexpected null characters"),
3416 filename);
3417 break;
3418 }
3419
39086a0e 3420 return bufstr;
7313baad
UW
3421}
3422
3423
e0d24f8d 3424static int
31568a15
TT
3425default_region_ok_for_hw_watchpoint (struct target_ops *self,
3426 CORE_ADDR addr, int len)
e0d24f8d 3427{
f5656ead 3428 return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT);
ccaa32c7
GS
3429}
3430
5009afc5
AS
3431static int
3432default_watchpoint_addr_within_range (struct target_ops *target,
3433 CORE_ADDR addr,
3434 CORE_ADDR start, int length)
3435{
3436 return addr >= start && addr < start + length;
3437}
3438
c2250ad1
UW
3439static struct gdbarch *
3440default_thread_architecture (struct target_ops *ops, ptid_t ptid)
3441{
f5656ead 3442 return target_gdbarch ();
c2250ad1
UW
3443}
3444
c906108c 3445static int
fba45db2 3446return_zero (void)
c906108c
SS
3447{
3448 return 0;
3449}
3450
ed9a39eb
JM
3451/*
3452 * Find the next target down the stack from the specified target.
3453 */
3454
3455struct target_ops *
fba45db2 3456find_target_beneath (struct target_ops *t)
ed9a39eb 3457{
258b763a 3458 return t->beneath;
ed9a39eb
JM
3459}
3460
8b06beed
TT
3461/* See target.h. */
3462
3463struct target_ops *
3464find_target_at (enum strata stratum)
3465{
3466 struct target_ops *t;
3467
3468 for (t = current_target.beneath; t != NULL; t = t->beneath)
3469 if (t->to_stratum == stratum)
3470 return t;
3471
3472 return NULL;
3473}
3474
c906108c
SS
3475\f
3476/* The inferior process has died. Long live the inferior! */
3477
3478void
fba45db2 3479generic_mourn_inferior (void)
c906108c 3480{
7f9f62ba 3481 ptid_t ptid;
c906108c 3482
7f9f62ba 3483 ptid = inferior_ptid;
39f77062 3484 inferior_ptid = null_ptid;
7f9f62ba 3485
f59f708a
PA
3486 /* Mark breakpoints uninserted in case something tries to delete a
3487 breakpoint while we delete the inferior's threads (which would
3488 fail, since the inferior is long gone). */
3489 mark_breakpoints_out ();
3490
7f9f62ba
PA
3491 if (!ptid_equal (ptid, null_ptid))
3492 {
3493 int pid = ptid_get_pid (ptid);
6c95b8df 3494 exit_inferior (pid);
7f9f62ba
PA
3495 }
3496
f59f708a
PA
3497 /* Note this wipes step-resume breakpoints, so needs to be done
3498 after exit_inferior, which ends up referencing the step-resume
3499 breakpoints through clear_thread_inferior_resources. */
c906108c 3500 breakpoint_init_inferior (inf_exited);
f59f708a 3501
c906108c
SS
3502 registers_changed ();
3503
c906108c
SS
3504 reopen_exec_file ();
3505 reinit_frame_cache ();
3506
9a4105ab
AC
3507 if (deprecated_detach_hook)
3508 deprecated_detach_hook ();
c906108c
SS
3509}
3510\f
fd0a2a6f
MK
3511/* Convert a normal process ID to a string. Returns the string in a
3512 static buffer. */
c906108c
SS
3513
3514char *
39f77062 3515normal_pid_to_str (ptid_t ptid)
c906108c 3516{
fd0a2a6f 3517 static char buf[32];
c906108c 3518
5fff8fc0 3519 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
3520 return buf;
3521}
3522
2c0b251b 3523static char *
117de6a9
PA
3524dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
3525{
3526 return normal_pid_to_str (ptid);
3527}
3528
9b4eba8e
HZ
3529/* Error-catcher for target_find_memory_regions. */
3530static int
2e73927c
TT
3531dummy_find_memory_regions (struct target_ops *self,
3532 find_memory_region_ftype ignore1, void *ignore2)
be4d1333 3533{
9b4eba8e 3534 error (_("Command not implemented for this target."));
be4d1333
MS
3535 return 0;
3536}
3537
9b4eba8e
HZ
3538/* Error-catcher for target_make_corefile_notes. */
3539static char *
fc6691b2
TT
3540dummy_make_corefile_notes (struct target_ops *self,
3541 bfd *ignore1, int *ignore2)
be4d1333 3542{
9b4eba8e 3543 error (_("Command not implemented for this target."));
be4d1333
MS
3544 return NULL;
3545}
3546
c906108c
SS
3547/* Set up the handful of non-empty slots needed by the dummy target
3548 vector. */
3549
3550static void
fba45db2 3551init_dummy_target (void)
c906108c
SS
3552{
3553 dummy_target.to_shortname = "None";
3554 dummy_target.to_longname = "None";
3555 dummy_target.to_doc = "";
c906108c 3556 dummy_target.to_create_inferior = find_default_create_inferior;
9908b566 3557 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
03583c20
UW
3558 dummy_target.to_supports_disable_randomization
3559 = find_default_supports_disable_randomization;
117de6a9 3560 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 3561 dummy_target.to_stratum = dummy_stratum;
c35b1492
PA
3562 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3563 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
3564 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
3565 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
aeaec162
TT
3566 dummy_target.to_has_execution
3567 = (int (*) (struct target_ops *, ptid_t)) return_zero;
c906108c 3568 dummy_target.to_magic = OPS_MAGIC;
1101cb7b
TT
3569
3570 install_dummy_methods (&dummy_target);
c906108c 3571}
c906108c 3572\f
c906108c 3573static void
fba45db2 3574debug_to_open (char *args, int from_tty)
c906108c
SS
3575{
3576 debug_target.to_open (args, from_tty);
3577
96baa820 3578 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
3579}
3580
f1c07ab0 3581void
460014f5 3582target_close (struct target_ops *targ)
f1c07ab0 3583{
7fdc1521
TT
3584 gdb_assert (!target_is_pushed (targ));
3585
f1c07ab0 3586 if (targ->to_xclose != NULL)
460014f5 3587 targ->to_xclose (targ);
f1c07ab0 3588 else if (targ->to_close != NULL)
de90e03d 3589 targ->to_close (targ);
947b8855
PA
3590
3591 if (targetdebug)
460014f5 3592 fprintf_unfiltered (gdb_stdlog, "target_close ()\n");
f1c07ab0
AC
3593}
3594
136d6dae
VP
3595void
3596target_attach (char *args, int from_tty)
3597{
e9a29200
TT
3598 current_target.to_attach (&current_target, args, from_tty);
3599 if (targetdebug)
3600 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
3601 args, from_tty);
136d6dae
VP
3602}
3603
28439f5e
PA
3604int
3605target_thread_alive (ptid_t ptid)
c906108c 3606{
28439f5e 3607 struct target_ops *t;
5d502164 3608
28439f5e
PA
3609 for (t = current_target.beneath; t != NULL; t = t->beneath)
3610 {
3611 if (t->to_thread_alive != NULL)
3612 {
3613 int retval;
c906108c 3614
28439f5e
PA
3615 retval = t->to_thread_alive (t, ptid);
3616 if (targetdebug)
3617 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
dfd4cc63 3618 ptid_get_pid (ptid), retval);
28439f5e
PA
3619
3620 return retval;
3621 }
3622 }
3623
3624 return 0;
3625}
3626
3627void
3628target_find_new_threads (void)
3629{
3630 struct target_ops *t;
5d502164 3631
28439f5e
PA
3632 for (t = current_target.beneath; t != NULL; t = t->beneath)
3633 {
3634 if (t->to_find_new_threads != NULL)
3635 {
3636 t->to_find_new_threads (t);
3637 if (targetdebug)
3638 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
3639
3640 return;
3641 }
3642 }
c906108c
SS
3643}
3644
d914c394
SS
3645void
3646target_stop (ptid_t ptid)
3647{
3648 if (!may_stop)
3649 {
3650 warning (_("May not interrupt or stop the target, ignoring attempt"));
3651 return;
3652 }
3653
1eab8a48 3654 (*current_target.to_stop) (&current_target, ptid);
d914c394
SS
3655}
3656
c906108c 3657static void
f045800c 3658debug_to_post_attach (struct target_ops *self, int pid)
c906108c 3659{
f045800c 3660 debug_target.to_post_attach (&debug_target, pid);
c906108c 3661
28439f5e 3662 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
3663}
3664
09826ec5
PA
3665/* Concatenate ELEM to LIST, a comma separate list, and return the
3666 result. The LIST incoming argument is released. */
3667
3668static char *
3669str_comma_list_concat_elem (char *list, const char *elem)
3670{
3671 if (list == NULL)
3672 return xstrdup (elem);
3673 else
3674 return reconcat (list, list, ", ", elem, (char *) NULL);
3675}
3676
3677/* Helper for target_options_to_string. If OPT is present in
3678 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3679 Returns the new resulting string. OPT is removed from
3680 TARGET_OPTIONS. */
3681
3682static char *
3683do_option (int *target_options, char *ret,
3684 int opt, char *opt_str)
3685{
3686 if ((*target_options & opt) != 0)
3687 {
3688 ret = str_comma_list_concat_elem (ret, opt_str);
3689 *target_options &= ~opt;
3690 }
3691
3692 return ret;
3693}
3694
3695char *
3696target_options_to_string (int target_options)
3697{
3698 char *ret = NULL;
3699
3700#define DO_TARG_OPTION(OPT) \
3701 ret = do_option (&target_options, ret, OPT, #OPT)
3702
3703 DO_TARG_OPTION (TARGET_WNOHANG);
3704
3705 if (target_options != 0)
3706 ret = str_comma_list_concat_elem (ret, "unknown???");
3707
3708 if (ret == NULL)
3709 ret = xstrdup ("");
3710 return ret;
3711}
3712
bf0c5130 3713static void
56be3814
UW
3714debug_print_register (const char * func,
3715 struct regcache *regcache, int regno)
bf0c5130 3716{
f8d29908 3717 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5d502164 3718
bf0c5130 3719 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 3720 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
3721 && gdbarch_register_name (gdbarch, regno) != NULL
3722 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3723 fprintf_unfiltered (gdb_stdlog, "(%s)",
3724 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
3725 else
3726 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 3727 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 3728 {
e17a4113 3729 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 3730 int i, size = register_size (gdbarch, regno);
e362b510 3731 gdb_byte buf[MAX_REGISTER_SIZE];
5d502164 3732
0ff58721 3733 regcache_raw_collect (regcache, regno, buf);
bf0c5130 3734 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 3735 for (i = 0; i < size; i++)
bf0c5130
AC
3736 {
3737 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3738 }
81c4a259 3739 if (size <= sizeof (LONGEST))
bf0c5130 3740 {
e17a4113 3741 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
5d502164 3742
0b1553bc
UW
3743 fprintf_unfiltered (gdb_stdlog, " %s %s",
3744 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
3745 }
3746 }
3747 fprintf_unfiltered (gdb_stdlog, "\n");
3748}
3749
28439f5e
PA
3750void
3751target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3752{
ad5989bd
TT
3753 current_target.to_fetch_registers (&current_target, regcache, regno);
3754 if (targetdebug)
3755 debug_print_register ("target_fetch_registers", regcache, regno);
c906108c
SS
3756}
3757
28439f5e
PA
3758void
3759target_store_registers (struct regcache *regcache, int regno)
c906108c 3760{
28439f5e 3761 struct target_ops *t;
5d502164 3762
d914c394
SS
3763 if (!may_write_registers)
3764 error (_("Writing to registers is not allowed (regno %d)"), regno);
3765
6b84065d
TT
3766 current_target.to_store_registers (&current_target, regcache, regno);
3767 if (targetdebug)
28439f5e 3768 {
6b84065d 3769 debug_print_register ("target_store_registers", regcache, regno);
28439f5e 3770 }
c906108c
SS
3771}
3772
dc146f7c
VP
3773int
3774target_core_of_thread (ptid_t ptid)
3775{
3776 struct target_ops *t;
3777
3778 for (t = current_target.beneath; t != NULL; t = t->beneath)
3779 {
3780 if (t->to_core_of_thread != NULL)
3781 {
3782 int retval = t->to_core_of_thread (t, ptid);
5d502164 3783
dc146f7c 3784 if (targetdebug)
3e43a32a
MS
3785 fprintf_unfiltered (gdb_stdlog,
3786 "target_core_of_thread (%d) = %d\n",
dfd4cc63 3787 ptid_get_pid (ptid), retval);
dc146f7c
VP
3788 return retval;
3789 }
3790 }
3791
3792 return -1;
3793}
3794
4a5e7a5b
PA
3795int
3796target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3797{
3798 struct target_ops *t;
3799
3800 for (t = current_target.beneath; t != NULL; t = t->beneath)
3801 {
3802 if (t->to_verify_memory != NULL)
3803 {
3804 int retval = t->to_verify_memory (t, data, memaddr, size);
5d502164 3805
4a5e7a5b 3806 if (targetdebug)
3e43a32a
MS
3807 fprintf_unfiltered (gdb_stdlog,
3808 "target_verify_memory (%s, %s) = %d\n",
f5656ead 3809 paddress (target_gdbarch (), memaddr),
4a5e7a5b
PA
3810 pulongest (size),
3811 retval);
3812 return retval;
3813 }
3814 }
3815
3816 tcomplain ();
3817}
3818
9c06b0b4
TJB
3819/* The documentation for this function is in its prototype declaration in
3820 target.h. */
3821
3822int
3823target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3824{
cd4ae029 3825 int ret;
9c06b0b4 3826
cd4ae029
TT
3827 ret = current_target.to_insert_mask_watchpoint (&current_target,
3828 addr, mask, rw);
9c06b0b4 3829
cd4ae029
TT
3830 if (targetdebug)
3831 fprintf_unfiltered (gdb_stdlog, "\
9c06b0b4 3832target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
cd4ae029
TT
3833 core_addr_to_string (addr),
3834 core_addr_to_string (mask), rw, ret);
3835
3836 return ret;
9c06b0b4
TJB
3837}
3838
3839/* The documentation for this function is in its prototype declaration in
3840 target.h. */
3841
3842int
3843target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3844{
8b1c364c 3845 int ret;
9c06b0b4 3846
8b1c364c
TT
3847 ret = current_target.to_remove_mask_watchpoint (&current_target,
3848 addr, mask, rw);
9c06b0b4 3849
8b1c364c
TT
3850 if (targetdebug)
3851 fprintf_unfiltered (gdb_stdlog, "\
9c06b0b4 3852target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
8b1c364c
TT
3853 core_addr_to_string (addr),
3854 core_addr_to_string (mask), rw, ret);
9c06b0b4 3855
8b1c364c 3856 return ret;
9c06b0b4
TJB
3857}
3858
3859/* The documentation for this function is in its prototype declaration
3860 in target.h. */
3861
3862int
3863target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
3864{
6c7e5e5c
TT
3865 return current_target.to_masked_watch_num_registers (&current_target,
3866 addr, mask);
9c06b0b4
TJB
3867}
3868
f1310107
TJB
3869/* The documentation for this function is in its prototype declaration
3870 in target.h. */
3871
3872int
3873target_ranged_break_num_registers (void)
3874{
a134316b 3875 return current_target.to_ranged_break_num_registers (&current_target);
f1310107
TJB
3876}
3877
02d27625
MM
3878/* See target.h. */
3879
02d27625
MM
3880struct btrace_target_info *
3881target_enable_btrace (ptid_t ptid)
3882{
3883 struct target_ops *t;
3884
3885 for (t = current_target.beneath; t != NULL; t = t->beneath)
3886 if (t->to_enable_btrace != NULL)
e3c49f88 3887 return t->to_enable_btrace (t, ptid);
02d27625
MM
3888
3889 tcomplain ();
3890 return NULL;
3891}
3892
3893/* See target.h. */
3894
3895void
3896target_disable_btrace (struct btrace_target_info *btinfo)
3897{
3898 struct target_ops *t;
3899
3900 for (t = current_target.beneath; t != NULL; t = t->beneath)
3901 if (t->to_disable_btrace != NULL)
d92f7ee3 3902 {
25e95349 3903 t->to_disable_btrace (t, btinfo);
d92f7ee3
SDJ
3904 return;
3905 }
02d27625
MM
3906
3907 tcomplain ();
3908}
3909
3910/* See target.h. */
3911
3912void
3913target_teardown_btrace (struct btrace_target_info *btinfo)
3914{
3915 struct target_ops *t;
3916
3917 for (t = current_target.beneath; t != NULL; t = t->beneath)
3918 if (t->to_teardown_btrace != NULL)
d92f7ee3 3919 {
1777056d 3920 t->to_teardown_btrace (t, btinfo);
d92f7ee3
SDJ
3921 return;
3922 }
02d27625
MM
3923
3924 tcomplain ();
3925}
3926
3927/* See target.h. */
3928
969c39fb
MM
3929enum btrace_error
3930target_read_btrace (VEC (btrace_block_s) **btrace,
3931 struct btrace_target_info *btinfo,
02d27625
MM
3932 enum btrace_read_type type)
3933{
3934 struct target_ops *t;
3935
3936 for (t = current_target.beneath; t != NULL; t = t->beneath)
3937 if (t->to_read_btrace != NULL)
39c49f83 3938 return t->to_read_btrace (t, btrace, btinfo, type);
02d27625
MM
3939
3940 tcomplain ();
969c39fb 3941 return BTRACE_ERR_NOT_SUPPORTED;
02d27625
MM
3942}
3943
d02ed0bb
MM
3944/* See target.h. */
3945
7c1687a9
MM
3946void
3947target_stop_recording (void)
3948{
3949 struct target_ops *t;
3950
3951 for (t = current_target.beneath; t != NULL; t = t->beneath)
3952 if (t->to_stop_recording != NULL)
3953 {
c6cd7c02 3954 t->to_stop_recording (t);
7c1687a9
MM
3955 return;
3956 }
3957
3958 /* This is optional. */
3959}
3960
3961/* See target.h. */
3962
d02ed0bb
MM
3963void
3964target_info_record (void)
3965{
3966 struct target_ops *t;
3967
3968 for (t = current_target.beneath; t != NULL; t = t->beneath)
3969 if (t->to_info_record != NULL)
3970 {
630d6a4a 3971 t->to_info_record (t);
d02ed0bb
MM
3972 return;
3973 }
3974
3975 tcomplain ();
3976}
3977
3978/* See target.h. */
3979
3980void
85e1311a 3981target_save_record (const char *filename)
d02ed0bb
MM
3982{
3983 struct target_ops *t;
3984
3985 for (t = current_target.beneath; t != NULL; t = t->beneath)
3986 if (t->to_save_record != NULL)
3987 {
1390f529 3988 t->to_save_record (t, filename);
d02ed0bb
MM
3989 return;
3990 }
3991
3992 tcomplain ();
3993}
3994
3995/* See target.h. */
3996
3997int
3998target_supports_delete_record (void)
3999{
4000 struct target_ops *t;
4001
4002 for (t = current_target.beneath; t != NULL; t = t->beneath)
4003 if (t->to_delete_record != NULL)
4004 return 1;
4005
4006 return 0;
4007}
4008
4009/* See target.h. */
4010
4011void
4012target_delete_record (void)
4013{
4014 struct target_ops *t;
4015
4016 for (t = current_target.beneath; t != NULL; t = t->beneath)
4017 if (t->to_delete_record != NULL)
4018 {
d1b55219 4019 t->to_delete_record (t);
d02ed0bb
MM
4020 return;
4021 }
4022
4023 tcomplain ();
4024}
4025
4026/* See target.h. */
4027
4028int
4029target_record_is_replaying (void)
4030{
4031 struct target_ops *t;
4032
4033 for (t = current_target.beneath; t != NULL; t = t->beneath)
4034 if (t->to_record_is_replaying != NULL)
1c63c994 4035 return t->to_record_is_replaying (t);
d02ed0bb
MM
4036
4037 return 0;
4038}
4039
4040/* See target.h. */
4041
4042void
4043target_goto_record_begin (void)
4044{
4045 struct target_ops *t;
4046
4047 for (t = current_target.beneath; t != NULL; t = t->beneath)
4048 if (t->to_goto_record_begin != NULL)
4049 {
08475817 4050 t->to_goto_record_begin (t);
d02ed0bb
MM
4051 return;
4052 }
4053
4054 tcomplain ();
4055}
4056
4057/* See target.h. */
4058
4059void
4060target_goto_record_end (void)
4061{
4062 struct target_ops *t;
4063
4064 for (t = current_target.beneath; t != NULL; t = t->beneath)
4065 if (t->to_goto_record_end != NULL)
4066 {
307a1b91 4067 t->to_goto_record_end (t);
d02ed0bb
MM
4068 return;
4069 }
4070
4071 tcomplain ();
4072}
4073
4074/* See target.h. */
4075
4076void
4077target_goto_record (ULONGEST insn)
4078{
4079 struct target_ops *t;
4080
4081 for (t = current_target.beneath; t != NULL; t = t->beneath)
4082 if (t->to_goto_record != NULL)
4083 {
606183ac 4084 t->to_goto_record (t, insn);
d02ed0bb
MM
4085 return;
4086 }
4087
4088 tcomplain ();
4089}
4090
67c86d06
MM
4091/* See target.h. */
4092
4093void
4094target_insn_history (int size, int flags)
4095{
4096 struct target_ops *t;
4097
4098 for (t = current_target.beneath; t != NULL; t = t->beneath)
4099 if (t->to_insn_history != NULL)
4100 {
7a6c5609 4101 t->to_insn_history (t, size, flags);
67c86d06
MM
4102 return;
4103 }
4104
4105 tcomplain ();
4106}
4107
4108/* See target.h. */
4109
4110void
4111target_insn_history_from (ULONGEST from, int size, int flags)
4112{
4113 struct target_ops *t;
4114
4115 for (t = current_target.beneath; t != NULL; t = t->beneath)
4116 if (t->to_insn_history_from != NULL)
4117 {
9abc3ff3 4118 t->to_insn_history_from (t, from, size, flags);
67c86d06
MM
4119 return;
4120 }
4121
4122 tcomplain ();
4123}
4124
4125/* See target.h. */
4126
4127void
4128target_insn_history_range (ULONGEST begin, ULONGEST end, int flags)
4129{
4130 struct target_ops *t;
4131
4132 for (t = current_target.beneath; t != NULL; t = t->beneath)
4133 if (t->to_insn_history_range != NULL)
4134 {
4e99c6b7 4135 t->to_insn_history_range (t, begin, end, flags);
67c86d06
MM
4136 return;
4137 }
4138
4139 tcomplain ();
4140}
4141
15984c13
MM
4142/* See target.h. */
4143
4144void
4145target_call_history (int size, int flags)
4146{
4147 struct target_ops *t;
4148
4149 for (t = current_target.beneath; t != NULL; t = t->beneath)
4150 if (t->to_call_history != NULL)
4151 {
5df2fcba 4152 t->to_call_history (t, size, flags);
15984c13
MM
4153 return;
4154 }
4155
4156 tcomplain ();
4157}
4158
4159/* See target.h. */
4160
4161void
4162target_call_history_from (ULONGEST begin, int size, int flags)
4163{
4164 struct target_ops *t;
4165
4166 for (t = current_target.beneath; t != NULL; t = t->beneath)
4167 if (t->to_call_history_from != NULL)
4168 {
ec0aea04 4169 t->to_call_history_from (t, begin, size, flags);
15984c13
MM
4170 return;
4171 }
4172
4173 tcomplain ();
4174}
4175
4176/* See target.h. */
4177
4178void
4179target_call_history_range (ULONGEST begin, ULONGEST end, int flags)
4180{
4181 struct target_ops *t;
4182
4183 for (t = current_target.beneath; t != NULL; t = t->beneath)
4184 if (t->to_call_history_range != NULL)
4185 {
f0d960ea 4186 t->to_call_history_range (t, begin, end, flags);
15984c13
MM
4187 return;
4188 }
4189
4190 tcomplain ();
4191}
4192
c906108c 4193static void
f32dbf8c 4194debug_to_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 4195{
f32dbf8c 4196 debug_target.to_prepare_to_store (&debug_target, regcache);
c906108c 4197
96baa820 4198 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
4199}
4200
ea001bdc
MM
4201/* See target.h. */
4202
4203const struct frame_unwind *
4204target_get_unwinder (void)
4205{
4206 struct target_ops *t;
4207
4208 for (t = current_target.beneath; t != NULL; t = t->beneath)
4209 if (t->to_get_unwinder != NULL)
4210 return t->to_get_unwinder;
4211
4212 return NULL;
4213}
4214
4215/* See target.h. */
4216
4217const struct frame_unwind *
4218target_get_tailcall_unwinder (void)
4219{
4220 struct target_ops *t;
4221
4222 for (t = current_target.beneath; t != NULL; t = t->beneath)
4223 if (t->to_get_tailcall_unwinder != NULL)
4224 return t->to_get_tailcall_unwinder;
4225
4226 return NULL;
4227}
4228
118e6252
MM
4229/* See target.h. */
4230
4231CORE_ADDR
4232forward_target_decr_pc_after_break (struct target_ops *ops,
4233 struct gdbarch *gdbarch)
4234{
4235 for (; ops != NULL; ops = ops->beneath)
4236 if (ops->to_decr_pc_after_break != NULL)
4237 return ops->to_decr_pc_after_break (ops, gdbarch);
4238
4239 return gdbarch_decr_pc_after_break (gdbarch);
4240}
4241
4242/* See target.h. */
4243
4244CORE_ADDR
4245target_decr_pc_after_break (struct gdbarch *gdbarch)
4246{
4247 return forward_target_decr_pc_after_break (current_target.beneath, gdbarch);
4248}
4249
c906108c 4250static int
961cb7b5 4251deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
4252 int write, struct mem_attrib *attrib,
4253 struct target_ops *target)
c906108c
SS
4254{
4255 int retval;
4256
c8e73a31
AC
4257 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
4258 attrib, target);
c906108c 4259
96baa820 4260 fprintf_unfiltered (gdb_stdlog,
53b71562 4261 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
f5656ead 4262 paddress (target_gdbarch (), memaddr), len,
5af949e3 4263 write ? "write" : "read", retval);
c906108c 4264
c906108c
SS
4265 if (retval > 0)
4266 {
4267 int i;
4268
96baa820 4269 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
4270 for (i = 0; i < retval; i++)
4271 {
53b71562 4272 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
4273 {
4274 if (targetdebug < 2 && i > 0)
4275 {
4276 fprintf_unfiltered (gdb_stdlog, " ...");
4277 break;
4278 }
4279 fprintf_unfiltered (gdb_stdlog, "\n");
4280 }
2bc416ba 4281
96baa820 4282 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
4283 }
4284 }
4285
96baa820 4286 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
4287
4288 return retval;
4289}
4290
4291static void
fba45db2 4292debug_to_files_info (struct target_ops *target)
c906108c
SS
4293{
4294 debug_target.to_files_info (target);
4295
96baa820 4296 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
4297}
4298
4299static int
3db08215 4300debug_to_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
a6d9a66e 4301 struct bp_target_info *bp_tgt)
c906108c
SS
4302{
4303 int retval;
4304
6b84065d 4305 retval = debug_target.to_insert_breakpoint (&debug_target, gdbarch, bp_tgt);
c906108c 4306
96baa820 4307 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4308 "target_insert_breakpoint (%s, xxx) = %ld\n",
4309 core_addr_to_string (bp_tgt->placed_address),
104c1213 4310 (unsigned long) retval);
c906108c
SS
4311 return retval;
4312}
4313
4314static int
3db08215 4315debug_to_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
a6d9a66e 4316 struct bp_target_info *bp_tgt)
c906108c
SS
4317{
4318 int retval;
4319
6b84065d 4320 retval = debug_target.to_remove_breakpoint (&debug_target, gdbarch, bp_tgt);
c906108c 4321
96baa820 4322 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4323 "target_remove_breakpoint (%s, xxx) = %ld\n",
4324 core_addr_to_string (bp_tgt->placed_address),
104c1213 4325 (unsigned long) retval);
c906108c
SS
4326 return retval;
4327}
4328
ccaa32c7 4329static int
5461485a
TT
4330debug_to_can_use_hw_breakpoint (struct target_ops *self,
4331 int type, int cnt, int from_tty)
ccaa32c7
GS
4332{
4333 int retval;
4334
5461485a
TT
4335 retval = debug_target.to_can_use_hw_breakpoint (&debug_target,
4336 type, cnt, from_tty);
ccaa32c7
GS
4337
4338 fprintf_unfiltered (gdb_stdlog,
4339 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
4340 (unsigned long) type,
4341 (unsigned long) cnt,
4342 (unsigned long) from_tty,
4343 (unsigned long) retval);
4344 return retval;
4345}
4346
e0d24f8d 4347static int
31568a15
TT
4348debug_to_region_ok_for_hw_watchpoint (struct target_ops *self,
4349 CORE_ADDR addr, int len)
e0d24f8d
WZ
4350{
4351 CORE_ADDR retval;
4352
31568a15
TT
4353 retval = debug_target.to_region_ok_for_hw_watchpoint (&debug_target,
4354 addr, len);
e0d24f8d
WZ
4355
4356 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4357 "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
4358 core_addr_to_string (addr), (unsigned long) len,
4359 core_addr_to_string (retval));
e0d24f8d
WZ
4360 return retval;
4361}
4362
0cf6dd15 4363static int
c3a5ff89
TT
4364debug_to_can_accel_watchpoint_condition (struct target_ops *self,
4365 CORE_ADDR addr, int len, int rw,
0cf6dd15
TJB
4366 struct expression *cond)
4367{
4368 int retval;
4369
c3a5ff89
TT
4370 retval = debug_target.to_can_accel_watchpoint_condition (&debug_target,
4371 addr, len,
3e43a32a 4372 rw, cond);
0cf6dd15
TJB
4373
4374 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
4375 "target_can_accel_watchpoint_condition "
4376 "(%s, %d, %d, %s) = %ld\n",
bd91e7ae
OS
4377 core_addr_to_string (addr), len, rw,
4378 host_address_to_string (cond), (unsigned long) retval);
0cf6dd15
TJB
4379 return retval;
4380}
4381
ccaa32c7 4382static int
6a109b6b 4383debug_to_stopped_by_watchpoint (struct target_ops *ops)
ccaa32c7
GS
4384{
4385 int retval;
4386
6a109b6b 4387 retval = debug_target.to_stopped_by_watchpoint (&debug_target);
ccaa32c7
GS
4388
4389 fprintf_unfiltered (gdb_stdlog,
d92524f1 4390 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
4391 (unsigned long) retval);
4392 return retval;
4393}
4394
4aa7a7f5
JJ
4395static int
4396debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 4397{
4aa7a7f5 4398 int retval;
ccaa32c7 4399
4aa7a7f5 4400 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
4401
4402 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4403 "target_stopped_data_address ([%s]) = %ld\n",
4404 core_addr_to_string (*addr),
4aa7a7f5 4405 (unsigned long)retval);
ccaa32c7
GS
4406 return retval;
4407}
4408
5009afc5
AS
4409static int
4410debug_to_watchpoint_addr_within_range (struct target_ops *target,
4411 CORE_ADDR addr,
4412 CORE_ADDR start, int length)
4413{
4414 int retval;
4415
4416 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
4417 start, length);
4418
4419 fprintf_filtered (gdb_stdlog,
bd91e7ae
OS
4420 "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
4421 core_addr_to_string (addr), core_addr_to_string (start),
4422 length, retval);
5009afc5
AS
4423 return retval;
4424}
4425
ccaa32c7 4426static int
23a26771
TT
4427debug_to_insert_hw_breakpoint (struct target_ops *self,
4428 struct gdbarch *gdbarch,
a6d9a66e 4429 struct bp_target_info *bp_tgt)
ccaa32c7
GS
4430{
4431 int retval;
4432
23a26771
TT
4433 retval = debug_target.to_insert_hw_breakpoint (&debug_target,
4434 gdbarch, bp_tgt);
ccaa32c7
GS
4435
4436 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4437 "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
4438 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
4439 (unsigned long) retval);
4440 return retval;
4441}
4442
4443static int
a64dc96c
TT
4444debug_to_remove_hw_breakpoint (struct target_ops *self,
4445 struct gdbarch *gdbarch,
a6d9a66e 4446 struct bp_target_info *bp_tgt)
ccaa32c7
GS
4447{
4448 int retval;
4449
a64dc96c
TT
4450 retval = debug_target.to_remove_hw_breakpoint (&debug_target,
4451 gdbarch, bp_tgt);
ccaa32c7
GS
4452
4453 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4454 "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
4455 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
4456 (unsigned long) retval);
4457 return retval;
4458}
4459
4460static int
7bb99c53
TT
4461debug_to_insert_watchpoint (struct target_ops *self,
4462 CORE_ADDR addr, int len, int type,
0cf6dd15 4463 struct expression *cond)
ccaa32c7
GS
4464{
4465 int retval;
4466
7bb99c53
TT
4467 retval = debug_target.to_insert_watchpoint (&debug_target,
4468 addr, len, type, cond);
ccaa32c7
GS
4469
4470 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4471 "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
4472 core_addr_to_string (addr), len, type,
4473 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
4474 return retval;
4475}
4476
4477static int
11b5219a
TT
4478debug_to_remove_watchpoint (struct target_ops *self,
4479 CORE_ADDR addr, int len, int type,
0cf6dd15 4480 struct expression *cond)
ccaa32c7
GS
4481{
4482 int retval;
4483
11b5219a
TT
4484 retval = debug_target.to_remove_watchpoint (&debug_target,
4485 addr, len, type, cond);
ccaa32c7
GS
4486
4487 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4488 "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
4489 core_addr_to_string (addr), len, type,
4490 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
4491 return retval;
4492}
4493
c906108c 4494static void
c42bf286 4495debug_to_terminal_init (struct target_ops *self)
c906108c 4496{
c42bf286 4497 debug_target.to_terminal_init (&debug_target);
c906108c 4498
96baa820 4499 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
4500}
4501
4502static void
d2f640d4 4503debug_to_terminal_inferior (struct target_ops *self)
c906108c 4504{
d2f640d4 4505 debug_target.to_terminal_inferior (&debug_target);
c906108c 4506
96baa820 4507 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
4508}
4509
4510static void
2e1e1a19 4511debug_to_terminal_ours_for_output (struct target_ops *self)
c906108c 4512{
2e1e1a19 4513 debug_target.to_terminal_ours_for_output (&debug_target);
c906108c 4514
96baa820 4515 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
4516}
4517
4518static void
e3594fd1 4519debug_to_terminal_ours (struct target_ops *self)
c906108c 4520{
e3594fd1 4521 debug_target.to_terminal_ours (&debug_target);
c906108c 4522
96baa820 4523 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
4524}
4525
a790ad35 4526static void
ae3bd431 4527debug_to_terminal_save_ours (struct target_ops *self)
a790ad35 4528{
ae3bd431 4529 debug_target.to_terminal_save_ours (&debug_target);
a790ad35
SC
4530
4531 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
4532}
4533
c906108c 4534static void
0a4f40a2
TT
4535debug_to_terminal_info (struct target_ops *self,
4536 const char *arg, int from_tty)
c906108c 4537{
0a4f40a2 4538 debug_target.to_terminal_info (&debug_target, arg, from_tty);
c906108c 4539
96baa820 4540 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
4541 from_tty);
4542}
4543
c906108c 4544static void
71a9f134 4545debug_to_load (struct target_ops *self, char *args, int from_tty)
c906108c 4546{
71a9f134 4547 debug_target.to_load (&debug_target, args, from_tty);
c906108c 4548
96baa820 4549 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
4550}
4551
c906108c 4552static void
2e97a79e 4553debug_to_post_startup_inferior (struct target_ops *self, ptid_t ptid)
c906108c 4554{
2e97a79e 4555 debug_target.to_post_startup_inferior (&debug_target, ptid);
c906108c 4556
96baa820 4557 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
dfd4cc63 4558 ptid_get_pid (ptid));
c906108c
SS
4559}
4560
77b06cd7 4561static int
a863b201 4562debug_to_insert_fork_catchpoint (struct target_ops *self, int pid)
c906108c 4563{
77b06cd7
TJB
4564 int retval;
4565
a863b201 4566 retval = debug_target.to_insert_fork_catchpoint (&debug_target, pid);
77b06cd7
TJB
4567
4568 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
4569 pid, retval);
c906108c 4570
77b06cd7 4571 return retval;
c906108c
SS
4572}
4573
4574static int
973fc227 4575debug_to_remove_fork_catchpoint (struct target_ops *self, int pid)
c906108c 4576{
c5aa993b 4577 int retval;
c906108c 4578
973fc227 4579 retval = debug_target.to_remove_fork_catchpoint (&debug_target, pid);
c906108c 4580
96baa820 4581 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 4582 pid, retval);
c906108c
SS
4583
4584 return retval;
4585}
4586
77b06cd7 4587static int
3ecc7da0 4588debug_to_insert_vfork_catchpoint (struct target_ops *self, int pid)
c906108c 4589{
77b06cd7
TJB
4590 int retval;
4591
3ecc7da0 4592 retval = debug_target.to_insert_vfork_catchpoint (&debug_target, pid);
c906108c 4593
77b06cd7
TJB
4594 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4595 pid, retval);
4596
4597 return retval;
c906108c
SS
4598}
4599
4600static int
e98cf0cd 4601debug_to_remove_vfork_catchpoint (struct target_ops *self, int pid)
c906108c 4602{
c5aa993b 4603 int retval;
c906108c 4604
e98cf0cd 4605 retval = debug_target.to_remove_vfork_catchpoint (&debug_target, pid);
c906108c 4606
96baa820 4607 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 4608 pid, retval);
c906108c
SS
4609
4610 return retval;
4611}
4612
77b06cd7 4613static int
ba025e51 4614debug_to_insert_exec_catchpoint (struct target_ops *self, int pid)
c906108c 4615{
77b06cd7
TJB
4616 int retval;
4617
ba025e51 4618 retval = debug_target.to_insert_exec_catchpoint (&debug_target, pid);
c906108c 4619
77b06cd7
TJB
4620 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4621 pid, retval);
4622
4623 return retval;
c906108c
SS
4624}
4625
4626static int
758e29d2 4627debug_to_remove_exec_catchpoint (struct target_ops *self, int pid)
c906108c 4628{
c5aa993b 4629 int retval;
c906108c 4630
758e29d2 4631 retval = debug_target.to_remove_exec_catchpoint (&debug_target, pid);
c906108c 4632
96baa820 4633 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 4634 pid, retval);
c906108c
SS
4635
4636 return retval;
4637}
4638
c906108c 4639static int
d796e1d6
TT
4640debug_to_has_exited (struct target_ops *self,
4641 int pid, int wait_status, int *exit_status)
c906108c 4642{
c5aa993b 4643 int has_exited;
c906108c 4644
d796e1d6
TT
4645 has_exited = debug_target.to_has_exited (&debug_target,
4646 pid, wait_status, exit_status);
c906108c 4647
96baa820 4648 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 4649 pid, wait_status, *exit_status, has_exited);
c906108c
SS
4650
4651 return has_exited;
4652}
4653
c906108c 4654static int
da82bd6b 4655debug_to_can_run (struct target_ops *self)
c906108c
SS
4656{
4657 int retval;
4658
da82bd6b 4659 retval = debug_target.to_can_run (&debug_target);
c906108c 4660
96baa820 4661 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
4662
4663 return retval;
4664}
4665
c2250ad1
UW
4666static struct gdbarch *
4667debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
4668{
4669 struct gdbarch *retval;
4670
4671 retval = debug_target.to_thread_architecture (ops, ptid);
4672
3e43a32a
MS
4673 fprintf_unfiltered (gdb_stdlog,
4674 "target_thread_architecture (%s) = %s [%s]\n",
4675 target_pid_to_str (ptid),
4676 host_address_to_string (retval),
c2250ad1
UW
4677 gdbarch_bfd_arch_info (retval)->printable_name);
4678 return retval;
4679}
4680
c906108c 4681static void
1eab8a48 4682debug_to_stop (struct target_ops *self, ptid_t ptid)
c906108c 4683{
1eab8a48 4684 debug_target.to_stop (&debug_target, ptid);
c906108c 4685
94cc34af
PA
4686 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
4687 target_pid_to_str (ptid));
c906108c
SS
4688}
4689
96baa820 4690static void
1aac633b 4691debug_to_rcmd (struct target_ops *self, char *command,
d9fcf2fb 4692 struct ui_file *outbuf)
96baa820 4693{
1aac633b 4694 debug_target.to_rcmd (&debug_target, command, outbuf);
96baa820
JM
4695 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
4696}
4697
c906108c 4698static char *
8dd27370 4699debug_to_pid_to_exec_file (struct target_ops *self, int pid)
c906108c 4700{
c5aa993b 4701 char *exec_file;
c906108c 4702
8dd27370 4703 exec_file = debug_target.to_pid_to_exec_file (&debug_target, pid);
c906108c 4704
96baa820 4705 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 4706 pid, exec_file);
c906108c
SS
4707
4708 return exec_file;
4709}
4710
c906108c 4711static void
fba45db2 4712setup_target_debug (void)
c906108c
SS
4713{
4714 memcpy (&debug_target, &current_target, sizeof debug_target);
4715
4716 current_target.to_open = debug_to_open;
c906108c 4717 current_target.to_post_attach = debug_to_post_attach;
c906108c 4718 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 4719 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
4720 current_target.to_files_info = debug_to_files_info;
4721 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
4722 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
4723 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
4724 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
4725 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
4726 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
4727 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
4728 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
4729 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3e43a32a
MS
4730 current_target.to_watchpoint_addr_within_range
4731 = debug_to_watchpoint_addr_within_range;
4732 current_target.to_region_ok_for_hw_watchpoint
4733 = debug_to_region_ok_for_hw_watchpoint;
4734 current_target.to_can_accel_watchpoint_condition
4735 = debug_to_can_accel_watchpoint_condition;
c906108c
SS
4736 current_target.to_terminal_init = debug_to_terminal_init;
4737 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3e43a32a
MS
4738 current_target.to_terminal_ours_for_output
4739 = debug_to_terminal_ours_for_output;
c906108c 4740 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 4741 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 4742 current_target.to_terminal_info = debug_to_terminal_info;
c906108c 4743 current_target.to_load = debug_to_load;
c906108c 4744 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
c906108c
SS
4745 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
4746 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
4747 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
4748 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
4749 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
4750 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 4751 current_target.to_has_exited = debug_to_has_exited;
c906108c 4752 current_target.to_can_run = debug_to_can_run;
c906108c 4753 current_target.to_stop = debug_to_stop;
96baa820 4754 current_target.to_rcmd = debug_to_rcmd;
c906108c 4755 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 4756 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 4757}
c906108c 4758\f
c5aa993b
JM
4759
4760static char targ_desc[] =
3e43a32a
MS
4761"Names of targets and files being debugged.\nShows the entire \
4762stack of targets currently in use (including the exec-file,\n\
c906108c
SS
4763core-file, and process, if any), as well as the symbol file name.";
4764
a53f3625
TT
4765static void
4766default_rcmd (struct target_ops *self, char *command, struct ui_file *output)
4767{
4768 error (_("\"monitor\" command not supported by this target."));
4769}
4770
96baa820
JM
4771static void
4772do_monitor_command (char *cmd,
4773 int from_tty)
4774{
96baa820
JM
4775 target_rcmd (cmd, gdb_stdtarg);
4776}
4777
87680a14
JB
4778/* Print the name of each layers of our target stack. */
4779
4780static void
4781maintenance_print_target_stack (char *cmd, int from_tty)
4782{
4783 struct target_ops *t;
4784
4785 printf_filtered (_("The current target stack is:\n"));
4786
4787 for (t = target_stack; t != NULL; t = t->beneath)
4788 {
4789 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
4790 }
4791}
4792
c6ebd6cf
VP
4793/* Controls if async mode is permitted. */
4794int target_async_permitted = 0;
4795
4796/* The set command writes to this variable. If the inferior is
b5419e49 4797 executing, target_async_permitted is *not* updated. */
c6ebd6cf
VP
4798static int target_async_permitted_1 = 0;
4799
4800static void
9401a810
PA
4801set_target_async_command (char *args, int from_tty,
4802 struct cmd_list_element *c)
c6ebd6cf 4803{
c35b1492 4804 if (have_live_inferiors ())
c6ebd6cf
VP
4805 {
4806 target_async_permitted_1 = target_async_permitted;
4807 error (_("Cannot change this setting while the inferior is running."));
4808 }
4809
4810 target_async_permitted = target_async_permitted_1;
4811}
4812
4813static void
9401a810
PA
4814show_target_async_command (struct ui_file *file, int from_tty,
4815 struct cmd_list_element *c,
4816 const char *value)
c6ebd6cf 4817{
3e43a32a
MS
4818 fprintf_filtered (file,
4819 _("Controlling the inferior in "
4820 "asynchronous mode is %s.\n"), value);
c6ebd6cf
VP
4821}
4822
d914c394
SS
4823/* Temporary copies of permission settings. */
4824
4825static int may_write_registers_1 = 1;
4826static int may_write_memory_1 = 1;
4827static int may_insert_breakpoints_1 = 1;
4828static int may_insert_tracepoints_1 = 1;
4829static int may_insert_fast_tracepoints_1 = 1;
4830static int may_stop_1 = 1;
4831
4832/* Make the user-set values match the real values again. */
4833
4834void
4835update_target_permissions (void)
4836{
4837 may_write_registers_1 = may_write_registers;
4838 may_write_memory_1 = may_write_memory;
4839 may_insert_breakpoints_1 = may_insert_breakpoints;
4840 may_insert_tracepoints_1 = may_insert_tracepoints;
4841 may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
4842 may_stop_1 = may_stop;
4843}
4844
4845/* The one function handles (most of) the permission flags in the same
4846 way. */
4847
4848static void
4849set_target_permissions (char *args, int from_tty,
4850 struct cmd_list_element *c)
4851{
4852 if (target_has_execution)
4853 {
4854 update_target_permissions ();
4855 error (_("Cannot change this setting while the inferior is running."));
4856 }
4857
4858 /* Make the real values match the user-changed values. */
4859 may_write_registers = may_write_registers_1;
4860 may_insert_breakpoints = may_insert_breakpoints_1;
4861 may_insert_tracepoints = may_insert_tracepoints_1;
4862 may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
4863 may_stop = may_stop_1;
4864 update_observer_mode ();
4865}
4866
4867/* Set memory write permission independently of observer mode. */
4868
4869static void
4870set_write_memory_permission (char *args, int from_tty,
4871 struct cmd_list_element *c)
4872{
4873 /* Make the real values match the user-changed values. */
4874 may_write_memory = may_write_memory_1;
4875 update_observer_mode ();
4876}
4877
4878
c906108c 4879void
fba45db2 4880initialize_targets (void)
c906108c
SS
4881{
4882 init_dummy_target ();
4883 push_target (&dummy_target);
4884
4885 add_info ("target", target_info, targ_desc);
4886 add_info ("files", target_info, targ_desc);
4887
ccce17b0 4888 add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
85c07804
AC
4889Set target debugging."), _("\
4890Show target debugging."), _("\
333dabeb
DJ
4891When non-zero, target debugging is enabled. Higher numbers are more\n\
4892verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804 4893command."),
ccce17b0
YQ
4894 NULL,
4895 show_targetdebug,
4896 &setdebuglist, &showdebuglist);
3a11626d 4897
2bc416ba 4898 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
4899 &trust_readonly, _("\
4900Set mode for reading from readonly sections."), _("\
4901Show mode for reading from readonly sections."), _("\
3a11626d
MS
4902When this mode is on, memory reads from readonly sections (such as .text)\n\
4903will be read from the object file instead of from the target. This will\n\
7915a72c 4904result in significant performance improvement for remote targets."),
2c5b56ce 4905 NULL,
920d2a44 4906 show_trust_readonly,
e707bbc2 4907 &setlist, &showlist);
96baa820
JM
4908
4909 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 4910 _("Send a command to the remote monitor (remote targets only)."));
96baa820 4911
87680a14
JB
4912 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
4913 _("Print the name of each layer of the internal target stack."),
4914 &maintenanceprintlist);
4915
c6ebd6cf
VP
4916 add_setshow_boolean_cmd ("target-async", no_class,
4917 &target_async_permitted_1, _("\
4918Set whether gdb controls the inferior in asynchronous mode."), _("\
4919Show whether gdb controls the inferior in asynchronous mode."), _("\
4920Tells gdb whether to control the inferior in asynchronous mode."),
9401a810
PA
4921 set_target_async_command,
4922 show_target_async_command,
c6ebd6cf
VP
4923 &setlist,
4924 &showlist);
4925
d914c394
SS
4926 add_setshow_boolean_cmd ("may-write-registers", class_support,
4927 &may_write_registers_1, _("\
4928Set permission to write into registers."), _("\
4929Show permission to write into registers."), _("\
4930When this permission is on, GDB may write into the target's registers.\n\
4931Otherwise, any sort of write attempt will result in an error."),
4932 set_target_permissions, NULL,
4933 &setlist, &showlist);
4934
4935 add_setshow_boolean_cmd ("may-write-memory", class_support,
4936 &may_write_memory_1, _("\
4937Set permission to write into target memory."), _("\
4938Show permission to write into target memory."), _("\
4939When this permission is on, GDB may write into the target's memory.\n\
4940Otherwise, any sort of write attempt will result in an error."),
4941 set_write_memory_permission, NULL,
4942 &setlist, &showlist);
4943
4944 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
4945 &may_insert_breakpoints_1, _("\
4946Set permission to insert breakpoints in the target."), _("\
4947Show permission to insert breakpoints in the target."), _("\
4948When this permission is on, GDB may insert breakpoints in the program.\n\
4949Otherwise, any sort of insertion attempt will result in an error."),
4950 set_target_permissions, NULL,
4951 &setlist, &showlist);
4952
4953 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
4954 &may_insert_tracepoints_1, _("\
4955Set permission to insert tracepoints in the target."), _("\
4956Show permission to insert tracepoints in the target."), _("\
4957When this permission is on, GDB may insert tracepoints in the program.\n\
4958Otherwise, any sort of insertion attempt will result in an error."),
4959 set_target_permissions, NULL,
4960 &setlist, &showlist);
4961
4962 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
4963 &may_insert_fast_tracepoints_1, _("\
4964Set permission to insert fast tracepoints in the target."), _("\
4965Show permission to insert fast tracepoints in the target."), _("\
4966When this permission is on, GDB may insert fast tracepoints.\n\
4967Otherwise, any sort of insertion attempt will result in an error."),
4968 set_target_permissions, NULL,
4969 &setlist, &showlist);
4970
4971 add_setshow_boolean_cmd ("may-interrupt", class_support,
4972 &may_stop_1, _("\
4973Set permission to interrupt or signal the target."), _("\
4974Show permission to interrupt or signal the target."), _("\
4975When this permission is on, GDB may interrupt/stop the target's execution.\n\
4976Otherwise, any attempt to interrupt or stop will be ignored."),
4977 set_target_permissions, NULL,
4978 &setlist, &showlist);
c906108c 4979}
This page took 1.940792 seconds and 4 git commands to generate.