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