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