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