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