* linux-low.c: Delete inclusion of ansidecl.h, elf/common.h,
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3 2
6aba47ca 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
0fb0cc75 4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
f6519ebc 5 Free Software Foundation, Inc.
7998dfc3 6
c906108c
SS
7 Contributed by Cygnus Support.
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
c5aa993b 14 (at your option) any later version.
c906108c 15
c5aa993b
JM
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
c906108c 20
c5aa993b 21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
23
24#include "defs.h"
25#include <errno.h>
c906108c
SS
26#include "gdb_string.h"
27#include "target.h"
28#include "gdbcmd.h"
29#include "symtab.h"
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
33#include "objfiles.h"
03f2053f 34#include "gdb_wait.h"
4930751a 35#include "dcache.h"
c906108c 36#include <signal.h>
4e052eda 37#include "regcache.h"
0088c768 38#include "gdb_assert.h"
b6591e8b 39#include "gdbcore.h"
9e35dae4 40#include "exceptions.h"
424163ea 41#include "target-descriptions.h"
e1ac3328 42#include "gdbthread.h"
b9db4ced 43#include "solib.h"
07b82ea5 44#include "exec.h"
edb3359d 45#include "inline-frame.h"
c906108c 46
a14ed312 47static void target_info (char *, int);
c906108c 48
a14ed312 49static void kill_or_be_killed (int);
c906108c 50
a14ed312 51static void default_terminal_info (char *, int);
c906108c 52
5009afc5
AS
53static int default_watchpoint_addr_within_range (struct target_ops *,
54 CORE_ADDR, CORE_ADDR, int);
55
e0d24f8d
WZ
56static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
57
a14ed312 58static int nosymbol (char *, CORE_ADDR *);
c906108c 59
4ecb6f27 60static void tcomplain (void) ATTR_NORETURN;
c906108c 61
a14ed312 62static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 63
a14ed312 64static int return_zero (void);
c906108c 65
a14ed312 66static int return_one (void);
c906108c 67
ccaa32c7
GS
68static int return_minus_one (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
a14ed312 76static void nosupport_runtime (void);
392a587b 77
4b8a223f 78static LONGEST default_xfer_partial (struct target_ops *ops,
0088c768 79 enum target_object object,
1b0ba102
AC
80 const char *annex, gdb_byte *readbuf,
81 const gdb_byte *writebuf,
8aa91c1e 82 ULONGEST offset, LONGEST len);
0088c768 83
cf7a04e8
DJ
84static LONGEST current_xfer_partial (struct target_ops *ops,
85 enum target_object object,
86 const char *annex, gdb_byte *readbuf,
87 const gdb_byte *writebuf,
88 ULONGEST offset, LONGEST len);
c906108c 89
cf7a04e8
DJ
90static LONGEST target_xfer_partial (struct target_ops *ops,
91 enum target_object object,
92 const char *annex,
93 void *readbuf, const void *writebuf,
94 ULONGEST offset, LONGEST len);
c906108c 95
c2250ad1
UW
96static struct gdbarch *default_thread_architecture (struct target_ops *ops,
97 ptid_t ptid);
98
a14ed312 99static void init_dummy_target (void);
c906108c 100
aa869812
AC
101static struct target_ops debug_target;
102
a14ed312 103static void debug_to_open (char *, int);
c906108c 104
316f2060 105static void debug_to_prepare_to_store (struct regcache *);
c906108c 106
a14ed312 107static void debug_to_files_info (struct target_ops *);
c906108c 108
a6d9a66e
UW
109static int debug_to_insert_breakpoint (struct gdbarch *,
110 struct bp_target_info *);
c906108c 111
a6d9a66e
UW
112static int debug_to_remove_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
c906108c 114
ccaa32c7
GS
115static int debug_to_can_use_hw_breakpoint (int, int, int);
116
a6d9a66e
UW
117static int debug_to_insert_hw_breakpoint (struct gdbarch *,
118 struct bp_target_info *);
ccaa32c7 119
a6d9a66e
UW
120static int debug_to_remove_hw_breakpoint (struct gdbarch *,
121 struct bp_target_info *);
ccaa32c7
GS
122
123static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
124
125static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
126
127static int debug_to_stopped_by_watchpoint (void);
128
4aa7a7f5 129static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
ccaa32c7 130
5009afc5
AS
131static int debug_to_watchpoint_addr_within_range (struct target_ops *,
132 CORE_ADDR, CORE_ADDR, int);
133
e0d24f8d
WZ
134static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
135
a14ed312 136static void debug_to_terminal_init (void);
c906108c 137
a14ed312 138static void debug_to_terminal_inferior (void);
c906108c 139
a14ed312 140static void debug_to_terminal_ours_for_output (void);
c906108c 141
a790ad35
SC
142static void debug_to_terminal_save_ours (void);
143
a14ed312 144static void debug_to_terminal_ours (void);
c906108c 145
a14ed312 146static void debug_to_terminal_info (char *, int);
c906108c 147
a14ed312 148static void debug_to_load (char *, int);
c906108c 149
a14ed312 150static int debug_to_lookup_symbol (char *, CORE_ADDR *);
c906108c 151
a14ed312 152static int debug_to_can_run (void);
c906108c 153
39f77062 154static void debug_to_notice_signals (ptid_t);
c906108c 155
94cc34af 156static void debug_to_stop (ptid_t);
c906108c 157
5ac10fd1
AC
158/* NOTE: cagney/2004-09-29: Many targets reference this variable in
159 wierd and mysterious ways. Putting the variable here lets those
160 wierd and mysterious ways keep building while they are being
161 converted to the inferior inheritance structure. */
1df84f13 162struct target_ops deprecated_child_ops;
5ac10fd1 163
c906108c 164/* Pointer to array of target architecture structures; the size of the
2bc416ba 165 array; the current index into the array; the allocated size of the
c906108c
SS
166 array. */
167struct target_ops **target_structs;
168unsigned target_struct_size;
169unsigned target_struct_index;
170unsigned target_struct_allocsize;
171#define DEFAULT_ALLOCSIZE 10
172
173/* The initial current target, so that there is always a semi-valid
174 current target. */
175
176static struct target_ops dummy_target;
177
178/* Top of target stack. */
179
258b763a 180static struct target_ops *target_stack;
c906108c
SS
181
182/* The target structure we are currently using to talk to a process
183 or file or whatever "inferior" we have. */
184
185struct target_ops current_target;
186
187/* Command list for target. */
188
189static struct cmd_list_element *targetlist = NULL;
190
cf7a04e8
DJ
191/* Nonzero if we should trust readonly sections from the
192 executable when reading memory. */
193
194static int trust_readonly = 0;
195
8defab1a
DJ
196/* Nonzero if we should show true memory content including
197 memory breakpoint inserted by gdb. */
198
199static int show_memory_breakpoints = 0;
200
c906108c
SS
201/* Non-zero if we want to see trace of target level stuff. */
202
203static int targetdebug = 0;
920d2a44
AC
204static void
205show_targetdebug (struct ui_file *file, int from_tty,
206 struct cmd_list_element *c, const char *value)
207{
208 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
209}
c906108c 210
a14ed312 211static void setup_target_debug (void);
c906108c 212
4e5d721f
DE
213/* The option sets this. */
214static int stack_cache_enabled_p_1 = 1;
215/* And set_stack_cache_enabled_p updates this.
216 The reason for the separation is so that we don't flush the cache for
217 on->on transitions. */
218static int stack_cache_enabled_p = 1;
219
220/* This is called *after* the stack-cache has been set.
221 Flush the cache for off->on and on->off transitions.
222 There's no real need to flush the cache for on->off transitions,
223 except cleanliness. */
224
225static void
226set_stack_cache_enabled_p (char *args, int from_tty,
227 struct cmd_list_element *c)
228{
229 if (stack_cache_enabled_p != stack_cache_enabled_p_1)
230 target_dcache_invalidate ();
231
232 stack_cache_enabled_p = stack_cache_enabled_p_1;
233}
234
235static void
236show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
237 struct cmd_list_element *c, const char *value)
238{
239 fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
240}
241
242/* Cache of memory operations, to speed up remote access. */
243static DCACHE *target_dcache;
244
245/* Invalidate the target dcache. */
246
247void
248target_dcache_invalidate (void)
249{
250 dcache_invalidate (target_dcache);
251}
4930751a 252
c906108c
SS
253/* The user just typed 'target' without the name of a target. */
254
c906108c 255static void
fba45db2 256target_command (char *arg, int from_tty)
c906108c
SS
257{
258 fputs_filtered ("Argument required (target name). Try `help target'\n",
259 gdb_stdout);
260}
261
c35b1492
PA
262/* Default target_has_* methods for process_stratum targets. */
263
264int
265default_child_has_all_memory (struct target_ops *ops)
266{
267 /* If no inferior selected, then we can't read memory here. */
268 if (ptid_equal (inferior_ptid, null_ptid))
269 return 0;
270
271 return 1;
272}
273
274int
275default_child_has_memory (struct target_ops *ops)
276{
277 /* If no inferior selected, then we can't read memory here. */
278 if (ptid_equal (inferior_ptid, null_ptid))
279 return 0;
280
281 return 1;
282}
283
284int
285default_child_has_stack (struct target_ops *ops)
286{
287 /* If no inferior selected, there's no stack. */
288 if (ptid_equal (inferior_ptid, null_ptid))
289 return 0;
290
291 return 1;
292}
293
294int
295default_child_has_registers (struct target_ops *ops)
296{
297 /* Can't read registers from no inferior. */
298 if (ptid_equal (inferior_ptid, null_ptid))
299 return 0;
300
301 return 1;
302}
303
304int
305default_child_has_execution (struct target_ops *ops)
306{
307 /* If there's no thread selected, then we can't make it run through
308 hoops. */
309 if (ptid_equal (inferior_ptid, null_ptid))
310 return 0;
311
312 return 1;
313}
314
315
316int
317target_has_all_memory_1 (void)
318{
319 struct target_ops *t;
320
321 for (t = current_target.beneath; t != NULL; t = t->beneath)
322 if (t->to_has_all_memory (t))
323 return 1;
324
325 return 0;
326}
327
328int
329target_has_memory_1 (void)
330{
331 struct target_ops *t;
332
333 for (t = current_target.beneath; t != NULL; t = t->beneath)
334 if (t->to_has_memory (t))
335 return 1;
336
337 return 0;
338}
339
340int
341target_has_stack_1 (void)
342{
343 struct target_ops *t;
344
345 for (t = current_target.beneath; t != NULL; t = t->beneath)
346 if (t->to_has_stack (t))
347 return 1;
348
349 return 0;
350}
351
352int
353target_has_registers_1 (void)
354{
355 struct target_ops *t;
356
357 for (t = current_target.beneath; t != NULL; t = t->beneath)
358 if (t->to_has_registers (t))
359 return 1;
360
361 return 0;
362}
363
364int
365target_has_execution_1 (void)
366{
367 struct target_ops *t;
368
369 for (t = current_target.beneath; t != NULL; t = t->beneath)
370 if (t->to_has_execution (t))
371 return 1;
372
373 return 0;
374}
375
c906108c
SS
376/* Add a possible target architecture to the list. */
377
378void
fba45db2 379add_target (struct target_ops *t)
c906108c 380{
0088c768 381 /* Provide default values for all "must have" methods. */
0b603eba
AC
382 if (t->to_xfer_partial == NULL)
383 t->to_xfer_partial = default_xfer_partial;
0088c768 384
c35b1492
PA
385 if (t->to_has_all_memory == NULL)
386 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
387
388 if (t->to_has_memory == NULL)
389 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
390
391 if (t->to_has_stack == NULL)
392 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
393
394 if (t->to_has_registers == NULL)
395 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
396
397 if (t->to_has_execution == NULL)
398 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
399
c906108c
SS
400 if (!target_structs)
401 {
402 target_struct_allocsize = DEFAULT_ALLOCSIZE;
403 target_structs = (struct target_ops **) xmalloc
404 (target_struct_allocsize * sizeof (*target_structs));
405 }
406 if (target_struct_size >= target_struct_allocsize)
407 {
408 target_struct_allocsize *= 2;
409 target_structs = (struct target_ops **)
c5aa993b
JM
410 xrealloc ((char *) target_structs,
411 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
412 }
413 target_structs[target_struct_size++] = t;
c906108c
SS
414
415 if (targetlist == NULL)
1bedd215
AC
416 add_prefix_cmd ("target", class_run, target_command, _("\
417Connect to a target machine or process.\n\
c906108c
SS
418The first argument is the type or protocol of the target machine.\n\
419Remaining arguments are interpreted by the target protocol. For more\n\
420information on the arguments for a particular protocol, type\n\
1bedd215 421`help target ' followed by the protocol name."),
c906108c
SS
422 &targetlist, "target ", 0, &cmdlist);
423 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
424}
425
426/* Stub functions */
427
428void
fba45db2 429target_ignore (void)
c906108c
SS
430{
431}
432
7d85a9c0
JB
433void
434target_kill (void)
435{
436 struct target_ops *t;
437
438 for (t = current_target.beneath; t != NULL; t = t->beneath)
439 if (t->to_kill != NULL)
440 {
441 if (targetdebug)
442 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
443
444 t->to_kill (t);
445 return;
446 }
447
448 noprocess ();
449}
450
11cf8741
JM
451void
452target_load (char *arg, int from_tty)
453{
4e5d721f 454 target_dcache_invalidate ();
11cf8741
JM
455 (*current_target.to_load) (arg, from_tty);
456}
457
947b8855
PA
458void
459target_create_inferior (char *exec_file, char *args,
460 char **env, int from_tty)
136d6dae
VP
461{
462 struct target_ops *t;
463 for (t = current_target.beneath; t != NULL; t = t->beneath)
464 {
465 if (t->to_create_inferior != NULL)
466 {
467 t->to_create_inferior (t, exec_file, args, env, from_tty);
947b8855
PA
468 if (targetdebug)
469 fprintf_unfiltered (gdb_stdlog,
470 "target_create_inferior (%s, %s, xxx, %d)\n",
471 exec_file, args, from_tty);
136d6dae
VP
472 return;
473 }
474 }
475
476 internal_error (__FILE__, __LINE__,
477 "could not find a target to create inferior");
478}
479
d9d2d8b6
PA
480void
481target_terminal_inferior (void)
482{
483 /* A background resume (``run&'') should leave GDB in control of the
ba7f6c64
VP
484 terminal. Use target_can_async_p, not target_is_async_p, since at
485 this point the target is not async yet. However, if sync_execution
486 is not set, we know it will become async prior to resume. */
487 if (target_can_async_p () && !sync_execution)
d9d2d8b6
PA
488 return;
489
490 /* If GDB is resuming the inferior in the foreground, install
491 inferior's terminal modes. */
492 (*current_target.to_terminal_inferior) ();
493}
136d6dae 494
c906108c 495static int
fba45db2
KB
496nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
497 struct target_ops *t)
c906108c 498{
c5aa993b
JM
499 errno = EIO; /* Can't read/write this location */
500 return 0; /* No bytes handled */
c906108c
SS
501}
502
503static void
fba45db2 504tcomplain (void)
c906108c 505{
8a3fe4f8 506 error (_("You can't do that when your target is `%s'"),
c906108c
SS
507 current_target.to_shortname);
508}
509
510void
fba45db2 511noprocess (void)
c906108c 512{
8a3fe4f8 513 error (_("You can't do that without a process to debug."));
c906108c
SS
514}
515
c906108c 516static int
fba45db2 517nosymbol (char *name, CORE_ADDR *addrp)
c906108c 518{
c5aa993b 519 return 1; /* Symbol does not exist in target env */
c906108c
SS
520}
521
392a587b 522static void
fba45db2 523nosupport_runtime (void)
c906108c 524{
39f77062 525 if (ptid_equal (inferior_ptid, null_ptid))
c906108c
SS
526 noprocess ();
527 else
8a3fe4f8 528 error (_("No run-time support for this"));
c906108c
SS
529}
530
531
c906108c 532static void
fba45db2 533default_terminal_info (char *args, int from_tty)
c906108c 534{
a3f17187 535 printf_unfiltered (_("No saved terminal information.\n"));
c906108c
SS
536}
537
538/* This is the default target_create_inferior and target_attach function.
539 If the current target is executing, it asks whether to kill it off.
540 If this function returns without calling error(), it has killed off
541 the target, and the operation should be attempted. */
542
543static void
fba45db2 544kill_or_be_killed (int from_tty)
c906108c
SS
545{
546 if (target_has_execution)
547 {
a3f17187 548 printf_unfiltered (_("You are already running a program:\n"));
c906108c 549 target_files_info ();
9e2f0ad4 550 if (query (_("Kill it? ")))
c5aa993b
JM
551 {
552 target_kill ();
553 if (target_has_execution)
8a3fe4f8 554 error (_("Killing the program did not help."));
c5aa993b
JM
555 return;
556 }
557 else
558 {
8a3fe4f8 559 error (_("Program not killed."));
c5aa993b 560 }
c906108c 561 }
c5aa993b 562 tcomplain ();
c906108c
SS
563}
564
0ef643c8
JB
565/* A default implementation for the to_get_ada_task_ptid target method.
566
567 This function builds the PTID by using both LWP and TID as part of
568 the PTID lwp and tid elements. The pid used is the pid of the
569 inferior_ptid. */
570
2c0b251b 571static ptid_t
0ef643c8
JB
572default_get_ada_task_ptid (long lwp, long tid)
573{
574 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
575}
576
7998dfc3
AC
577/* Go through the target stack from top to bottom, copying over zero
578 entries in current_target, then filling in still empty entries. In
579 effect, we are doing class inheritance through the pushed target
580 vectors.
581
582 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
583 is currently implemented, is that it discards any knowledge of
584 which target an inherited method originally belonged to.
585 Consequently, new new target methods should instead explicitly and
586 locally search the target stack for the target that can handle the
587 request. */
c906108c
SS
588
589static void
7998dfc3 590update_current_target (void)
c906108c 591{
7998dfc3
AC
592 struct target_ops *t;
593
08d8bcd7 594 /* First, reset current's contents. */
7998dfc3
AC
595 memset (&current_target, 0, sizeof (current_target));
596
597#define INHERIT(FIELD, TARGET) \
598 if (!current_target.FIELD) \
599 current_target.FIELD = (TARGET)->FIELD
600
601 for (t = target_stack; t; t = t->beneath)
602 {
603 INHERIT (to_shortname, t);
604 INHERIT (to_longname, t);
605 INHERIT (to_doc, t);
b52323fa
UW
606 /* Do not inherit to_open. */
607 /* Do not inherit to_close. */
136d6dae 608 /* Do not inherit to_attach. */
7998dfc3 609 INHERIT (to_post_attach, t);
dc177b7a 610 INHERIT (to_attach_no_wait, t);
136d6dae 611 /* Do not inherit to_detach. */
597320e7 612 /* Do not inherit to_disconnect. */
28439f5e 613 /* Do not inherit to_resume. */
117de6a9 614 /* Do not inherit to_wait. */
28439f5e
PA
615 /* Do not inherit to_fetch_registers. */
616 /* Do not inherit to_store_registers. */
7998dfc3 617 INHERIT (to_prepare_to_store, t);
c8e73a31 618 INHERIT (deprecated_xfer_memory, t);
7998dfc3
AC
619 INHERIT (to_files_info, t);
620 INHERIT (to_insert_breakpoint, t);
621 INHERIT (to_remove_breakpoint, t);
622 INHERIT (to_can_use_hw_breakpoint, t);
623 INHERIT (to_insert_hw_breakpoint, t);
624 INHERIT (to_remove_hw_breakpoint, t);
625 INHERIT (to_insert_watchpoint, t);
626 INHERIT (to_remove_watchpoint, t);
627 INHERIT (to_stopped_data_address, t);
74174d2e 628 INHERIT (to_have_steppable_watchpoint, t);
7998dfc3 629 INHERIT (to_have_continuable_watchpoint, t);
5009afc5
AS
630 INHERIT (to_stopped_by_watchpoint, t);
631 INHERIT (to_watchpoint_addr_within_range, t);
e0d24f8d 632 INHERIT (to_region_ok_for_hw_watchpoint, t);
7998dfc3
AC
633 INHERIT (to_terminal_init, t);
634 INHERIT (to_terminal_inferior, t);
635 INHERIT (to_terminal_ours_for_output, t);
636 INHERIT (to_terminal_ours, t);
637 INHERIT (to_terminal_save_ours, t);
638 INHERIT (to_terminal_info, t);
7d85a9c0 639 /* Do not inherit to_kill. */
7998dfc3
AC
640 INHERIT (to_load, t);
641 INHERIT (to_lookup_symbol, t);
136d6dae 642 /* Do no inherit to_create_inferior. */
7998dfc3
AC
643 INHERIT (to_post_startup_inferior, t);
644 INHERIT (to_acknowledge_created_inferior, t);
645 INHERIT (to_insert_fork_catchpoint, t);
646 INHERIT (to_remove_fork_catchpoint, t);
647 INHERIT (to_insert_vfork_catchpoint, t);
648 INHERIT (to_remove_vfork_catchpoint, t);
ee057212 649 /* Do not inherit to_follow_fork. */
7998dfc3
AC
650 INHERIT (to_insert_exec_catchpoint, t);
651 INHERIT (to_remove_exec_catchpoint, t);
a96d9b2e 652 INHERIT (to_set_syscall_catchpoint, t);
7998dfc3 653 INHERIT (to_has_exited, t);
117de6a9 654 /* Do not inherit to_mourn_inferiour. */
7998dfc3
AC
655 INHERIT (to_can_run, t);
656 INHERIT (to_notice_signals, t);
28439f5e
PA
657 /* Do not inherit to_thread_alive. */
658 /* Do not inherit to_find_new_threads. */
117de6a9 659 /* Do not inherit to_pid_to_str. */
7998dfc3
AC
660 INHERIT (to_extra_thread_info, t);
661 INHERIT (to_stop, t);
4b8a223f 662 /* Do not inherit to_xfer_partial. */
7998dfc3 663 INHERIT (to_rcmd, t);
7998dfc3 664 INHERIT (to_pid_to_exec_file, t);
49d03eab 665 INHERIT (to_log_command, t);
7998dfc3 666 INHERIT (to_stratum, t);
c35b1492
PA
667 /* Do not inherit to_has_all_memory */
668 /* Do not inherit to_has_memory */
669 /* Do not inherit to_has_stack */
670 /* Do not inherit to_has_registers */
671 /* Do not inherit to_has_execution */
7998dfc3 672 INHERIT (to_has_thread_control, t);
7998dfc3
AC
673 INHERIT (to_can_async_p, t);
674 INHERIT (to_is_async_p, t);
675 INHERIT (to_async, t);
b84876c2 676 INHERIT (to_async_mask, t);
7998dfc3
AC
677 INHERIT (to_find_memory_regions, t);
678 INHERIT (to_make_corefile_notes, t);
6b04bdb7
MS
679 INHERIT (to_get_bookmark, t);
680 INHERIT (to_goto_bookmark, t);
117de6a9 681 /* Do not inherit to_get_thread_local_address. */
b2175913 682 INHERIT (to_can_execute_reverse, t);
c2250ad1 683 INHERIT (to_thread_architecture, t);
424163ea 684 /* Do not inherit to_read_description. */
0ef643c8 685 INHERIT (to_get_ada_task_ptid, t);
08388c79 686 /* Do not inherit to_search_memory. */
8a305172 687 INHERIT (to_supports_multi_process, t);
7998dfc3 688 INHERIT (to_magic, t);
fd79ecee 689 /* Do not inherit to_memory_map. */
a76d924d
DJ
690 /* Do not inherit to_flash_erase. */
691 /* Do not inherit to_flash_done. */
7998dfc3
AC
692 }
693#undef INHERIT
694
695 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
696 it. Some entries are defaulted to a method that print an error,
697 others are hard-wired to a standard recursive default. */
c906108c
SS
698
699#define de_fault(field, value) \
7998dfc3
AC
700 if (!current_target.field) \
701 current_target.field = value
0d06e24b 702
2bc416ba
DJ
703 de_fault (to_open,
704 (void (*) (char *, int))
0d06e24b 705 tcomplain);
2bc416ba
DJ
706 de_fault (to_close,
707 (void (*) (int))
0d06e24b 708 target_ignore);
2bc416ba
DJ
709 de_fault (to_post_attach,
710 (void (*) (int))
0d06e24b 711 target_ignore);
2bc416ba 712 de_fault (to_prepare_to_store,
316f2060 713 (void (*) (struct regcache *))
0d06e24b 714 noprocess);
2bc416ba
DJ
715 de_fault (deprecated_xfer_memory,
716 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
0d06e24b 717 nomemory);
2bc416ba
DJ
718 de_fault (to_files_info,
719 (void (*) (struct target_ops *))
0d06e24b 720 target_ignore);
2bc416ba 721 de_fault (to_insert_breakpoint,
0d06e24b 722 memory_insert_breakpoint);
2bc416ba 723 de_fault (to_remove_breakpoint,
0d06e24b 724 memory_remove_breakpoint);
ccaa32c7
GS
725 de_fault (to_can_use_hw_breakpoint,
726 (int (*) (int, int, int))
727 return_zero);
728 de_fault (to_insert_hw_breakpoint,
a6d9a66e 729 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
730 return_minus_one);
731 de_fault (to_remove_hw_breakpoint,
a6d9a66e 732 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
733 return_minus_one);
734 de_fault (to_insert_watchpoint,
735 (int (*) (CORE_ADDR, int, int))
736 return_minus_one);
737 de_fault (to_remove_watchpoint,
738 (int (*) (CORE_ADDR, int, int))
739 return_minus_one);
740 de_fault (to_stopped_by_watchpoint,
741 (int (*) (void))
742 return_zero);
743 de_fault (to_stopped_data_address,
4aa7a7f5 744 (int (*) (struct target_ops *, CORE_ADDR *))
ccaa32c7 745 return_zero);
5009afc5
AS
746 de_fault (to_watchpoint_addr_within_range,
747 default_watchpoint_addr_within_range);
e0d24f8d
WZ
748 de_fault (to_region_ok_for_hw_watchpoint,
749 default_region_ok_for_hw_watchpoint);
2bc416ba
DJ
750 de_fault (to_terminal_init,
751 (void (*) (void))
0d06e24b 752 target_ignore);
2bc416ba
DJ
753 de_fault (to_terminal_inferior,
754 (void (*) (void))
0d06e24b 755 target_ignore);
2bc416ba
DJ
756 de_fault (to_terminal_ours_for_output,
757 (void (*) (void))
0d06e24b 758 target_ignore);
2bc416ba
DJ
759 de_fault (to_terminal_ours,
760 (void (*) (void))
0d06e24b 761 target_ignore);
2bc416ba
DJ
762 de_fault (to_terminal_save_ours,
763 (void (*) (void))
a790ad35 764 target_ignore);
2bc416ba 765 de_fault (to_terminal_info,
0d06e24b 766 default_terminal_info);
2bc416ba
DJ
767 de_fault (to_load,
768 (void (*) (char *, int))
0d06e24b 769 tcomplain);
2bc416ba
DJ
770 de_fault (to_lookup_symbol,
771 (int (*) (char *, CORE_ADDR *))
0d06e24b 772 nosymbol);
2bc416ba
DJ
773 de_fault (to_post_startup_inferior,
774 (void (*) (ptid_t))
0d06e24b 775 target_ignore);
2bc416ba
DJ
776 de_fault (to_acknowledge_created_inferior,
777 (void (*) (int))
0d06e24b 778 target_ignore);
2bc416ba
DJ
779 de_fault (to_insert_fork_catchpoint,
780 (void (*) (int))
0d06e24b 781 tcomplain);
2bc416ba
DJ
782 de_fault (to_remove_fork_catchpoint,
783 (int (*) (int))
0d06e24b 784 tcomplain);
2bc416ba
DJ
785 de_fault (to_insert_vfork_catchpoint,
786 (void (*) (int))
0d06e24b 787 tcomplain);
2bc416ba
DJ
788 de_fault (to_remove_vfork_catchpoint,
789 (int (*) (int))
0d06e24b 790 tcomplain);
2bc416ba
DJ
791 de_fault (to_insert_exec_catchpoint,
792 (void (*) (int))
0d06e24b 793 tcomplain);
2bc416ba
DJ
794 de_fault (to_remove_exec_catchpoint,
795 (int (*) (int))
0d06e24b 796 tcomplain);
a96d9b2e
SDJ
797 de_fault (to_set_syscall_catchpoint,
798 (int (*) (int, int, int, int, int *))
799 tcomplain);
2bc416ba
DJ
800 de_fault (to_has_exited,
801 (int (*) (int, int, int *))
0d06e24b 802 return_zero);
2bc416ba 803 de_fault (to_can_run,
0d06e24b 804 return_zero);
2bc416ba
DJ
805 de_fault (to_notice_signals,
806 (void (*) (ptid_t))
0d06e24b 807 target_ignore);
2bc416ba
DJ
808 de_fault (to_extra_thread_info,
809 (char *(*) (struct thread_info *))
0d06e24b 810 return_zero);
2bc416ba 811 de_fault (to_stop,
94cc34af 812 (void (*) (ptid_t))
0d06e24b 813 target_ignore);
cf7a04e8 814 current_target.to_xfer_partial = current_xfer_partial;
2bc416ba
DJ
815 de_fault (to_rcmd,
816 (void (*) (char *, struct ui_file *))
0d06e24b 817 tcomplain);
2bc416ba
DJ
818 de_fault (to_pid_to_exec_file,
819 (char *(*) (int))
0d06e24b 820 return_zero);
2bc416ba
DJ
821 de_fault (to_async,
822 (void (*) (void (*) (enum inferior_event_type, void*), void*))
0d06e24b 823 tcomplain);
b84876c2
PA
824 de_fault (to_async_mask,
825 (int (*) (int))
826 return_one);
c2250ad1
UW
827 de_fault (to_thread_architecture,
828 default_thread_architecture);
424163ea 829 current_target.to_read_description = NULL;
0ef643c8
JB
830 de_fault (to_get_ada_task_ptid,
831 (ptid_t (*) (long, long))
832 default_get_ada_task_ptid);
8a305172
PA
833 de_fault (to_supports_multi_process,
834 (int (*) (void))
835 return_zero);
c906108c 836#undef de_fault
c906108c 837
7998dfc3
AC
838 /* Finally, position the target-stack beneath the squashed
839 "current_target". That way code looking for a non-inherited
840 target method can quickly and simply find it. */
841 current_target.beneath = target_stack;
b4b61fdb
DJ
842
843 if (targetdebug)
844 setup_target_debug ();
c906108c
SS
845}
846
847/* Push a new target type into the stack of the existing target accessors,
848 possibly superseding some of the existing accessors.
849
850 Result is zero if the pushed target ended up on top of the stack,
851 nonzero if at least one target is on top of it.
852
853 Rather than allow an empty stack, we always have the dummy target at
854 the bottom stratum, so we can call the function vectors without
855 checking them. */
856
857int
fba45db2 858push_target (struct target_ops *t)
c906108c 859{
258b763a 860 struct target_ops **cur;
c906108c
SS
861
862 /* Check magic number. If wrong, it probably means someone changed
863 the struct definition, but not all the places that initialize one. */
864 if (t->to_magic != OPS_MAGIC)
865 {
c5aa993b
JM
866 fprintf_unfiltered (gdb_stderr,
867 "Magic number of %s target struct wrong\n",
868 t->to_shortname);
e2e0b3e5 869 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
c906108c
SS
870 }
871
258b763a
AC
872 /* Find the proper stratum to install this target in. */
873 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
c906108c 874 {
258b763a 875 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
c906108c
SS
876 break;
877 }
878
258b763a 879 /* If there's already targets at this stratum, remove them. */
88c231eb 880 /* FIXME: cagney/2003-10-15: I think this should be popping all
258b763a
AC
881 targets to CUR, and not just those at this stratum level. */
882 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
883 {
884 /* There's already something at this stratum level. Close it,
885 and un-hook it from the stack. */
886 struct target_ops *tmp = (*cur);
887 (*cur) = (*cur)->beneath;
888 tmp->beneath = NULL;
f1c07ab0 889 target_close (tmp, 0);
258b763a 890 }
c906108c
SS
891
892 /* We have removed all targets in our stratum, now add the new one. */
258b763a
AC
893 t->beneath = (*cur);
894 (*cur) = t;
c906108c
SS
895
896 update_current_target ();
897
258b763a
AC
898 /* Not on top? */
899 return (t != target_stack);
c906108c
SS
900}
901
2bc416ba 902/* Remove a target_ops vector from the stack, wherever it may be.
c906108c
SS
903 Return how many times it was removed (0 or 1). */
904
905int
fba45db2 906unpush_target (struct target_ops *t)
c906108c 907{
258b763a
AC
908 struct target_ops **cur;
909 struct target_ops *tmp;
c906108c 910
c8d104ad
PA
911 if (t->to_stratum == dummy_stratum)
912 internal_error (__FILE__, __LINE__,
913 "Attempt to unpush the dummy target");
914
c906108c
SS
915 /* Look for the specified target. Note that we assume that a target
916 can only occur once in the target stack. */
917
258b763a
AC
918 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
919 {
920 if ((*cur) == t)
921 break;
922 }
c906108c 923
258b763a 924 if ((*cur) == NULL)
c906108c
SS
925 return 0; /* Didn't find target_ops, quit now */
926
5269965e
AC
927 /* NOTE: cagney/2003-12-06: In '94 the close call was made
928 unconditional by moving it to before the above check that the
929 target was in the target stack (something about "Change the way
930 pushing and popping of targets work to support target overlays
931 and inheritance"). This doesn't make much sense - only open
932 targets should be closed. */
933 target_close (t, 0);
934
c906108c 935 /* Unchain the target */
258b763a
AC
936 tmp = (*cur);
937 (*cur) = (*cur)->beneath;
938 tmp->beneath = NULL;
c906108c
SS
939
940 update_current_target ();
c906108c
SS
941
942 return 1;
943}
944
945void
fba45db2 946pop_target (void)
c906108c 947{
b52323fa 948 target_close (target_stack, 0); /* Let it clean up */
258b763a 949 if (unpush_target (target_stack) == 1)
c906108c
SS
950 return;
951
c5aa993b
JM
952 fprintf_unfiltered (gdb_stderr,
953 "pop_target couldn't find target %s\n",
954 current_target.to_shortname);
e2e0b3e5 955 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
c906108c
SS
956}
957
aa76d38d 958void
87ab71f0 959pop_all_targets_above (enum strata above_stratum, int quitting)
aa76d38d 960{
87ab71f0 961 while ((int) (current_target.to_stratum) > (int) above_stratum)
aa76d38d 962 {
b52323fa 963 target_close (target_stack, quitting);
aa76d38d
PA
964 if (!unpush_target (target_stack))
965 {
966 fprintf_unfiltered (gdb_stderr,
967 "pop_all_targets couldn't find target %s\n",
b52323fa 968 target_stack->to_shortname);
aa76d38d
PA
969 internal_error (__FILE__, __LINE__,
970 _("failed internal consistency check"));
971 break;
972 }
973 }
974}
975
87ab71f0
PA
976void
977pop_all_targets (int quitting)
978{
979 pop_all_targets_above (dummy_stratum, quitting);
980}
981
72f5cf0e 982/* Using the objfile specified in OBJFILE, find the address for the
9e35dae4
DJ
983 current thread's thread-local storage with offset OFFSET. */
984CORE_ADDR
985target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
986{
987 volatile CORE_ADDR addr = 0;
117de6a9
PA
988 struct target_ops *target;
989
990 for (target = current_target.beneath;
991 target != NULL;
992 target = target->beneath)
993 {
994 if (target->to_get_thread_local_address != NULL)
995 break;
996 }
9e35dae4 997
117de6a9 998 if (target != NULL
1cf3db46 999 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
9e35dae4
DJ
1000 {
1001 ptid_t ptid = inferior_ptid;
1002 volatile struct gdb_exception ex;
1003
1004 TRY_CATCH (ex, RETURN_MASK_ALL)
1005 {
1006 CORE_ADDR lm_addr;
1007
1008 /* Fetch the load module address for this objfile. */
1cf3db46 1009 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
9e35dae4
DJ
1010 objfile);
1011 /* If it's 0, throw the appropriate exception. */
1012 if (lm_addr == 0)
1013 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1014 _("TLS load module not found"));
1015
117de6a9 1016 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
9e35dae4
DJ
1017 }
1018 /* If an error occurred, print TLS related messages here. Otherwise,
1019 throw the error to some higher catcher. */
1020 if (ex.reason < 0)
1021 {
1022 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1023
1024 switch (ex.error)
1025 {
1026 case TLS_NO_LIBRARY_SUPPORT_ERROR:
1027 error (_("Cannot find thread-local variables in this thread library."));
1028 break;
1029 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1030 if (objfile_is_library)
1031 error (_("Cannot find shared library `%s' in dynamic"
1032 " linker's load module list"), objfile->name);
1033 else
1034 error (_("Cannot find executable file `%s' in dynamic"
1035 " linker's load module list"), objfile->name);
1036 break;
1037 case TLS_NOT_ALLOCATED_YET_ERROR:
1038 if (objfile_is_library)
1039 error (_("The inferior has not yet allocated storage for"
1040 " thread-local variables in\n"
1041 "the shared library `%s'\n"
1042 "for %s"),
1043 objfile->name, target_pid_to_str (ptid));
1044 else
1045 error (_("The inferior has not yet allocated storage for"
1046 " thread-local variables in\n"
1047 "the executable `%s'\n"
1048 "for %s"),
1049 objfile->name, target_pid_to_str (ptid));
1050 break;
1051 case TLS_GENERIC_ERROR:
1052 if (objfile_is_library)
1053 error (_("Cannot find thread-local storage for %s, "
1054 "shared library %s:\n%s"),
1055 target_pid_to_str (ptid),
1056 objfile->name, ex.message);
1057 else
1058 error (_("Cannot find thread-local storage for %s, "
1059 "executable file %s:\n%s"),
1060 target_pid_to_str (ptid),
1061 objfile->name, ex.message);
1062 break;
1063 default:
1064 throw_exception (ex);
1065 break;
1066 }
1067 }
1068 }
1069 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1070 TLS is an ABI-specific thing. But we don't do that yet. */
1071 else
1072 error (_("Cannot find thread-local variables on this target"));
1073
1074 return addr;
1075}
1076
c906108c
SS
1077#undef MIN
1078#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1079
1080/* target_read_string -- read a null terminated string, up to LEN bytes,
1081 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1082 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1083 is responsible for freeing it. Return the number of bytes successfully
1084 read. */
1085
1086int
fba45db2 1087target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
1088{
1089 int tlen, origlen, offset, i;
1b0ba102 1090 gdb_byte buf[4];
c906108c
SS
1091 int errcode = 0;
1092 char *buffer;
1093 int buffer_allocated;
1094 char *bufptr;
1095 unsigned int nbytes_read = 0;
1096
6217bf3e
MS
1097 gdb_assert (string);
1098
c906108c
SS
1099 /* Small for testing. */
1100 buffer_allocated = 4;
1101 buffer = xmalloc (buffer_allocated);
1102 bufptr = buffer;
1103
1104 origlen = len;
1105
1106 while (len > 0)
1107 {
1108 tlen = MIN (len, 4 - (memaddr & 3));
1109 offset = memaddr & 3;
1110
1b0ba102 1111 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
c906108c
SS
1112 if (errcode != 0)
1113 {
1114 /* The transfer request might have crossed the boundary to an
1115 unallocated region of memory. Retry the transfer, requesting
1116 a single byte. */
1117 tlen = 1;
1118 offset = 0;
b8eb5af0 1119 errcode = target_read_memory (memaddr, buf, 1);
c906108c
SS
1120 if (errcode != 0)
1121 goto done;
1122 }
1123
1124 if (bufptr - buffer + tlen > buffer_allocated)
1125 {
1126 unsigned int bytes;
1127 bytes = bufptr - buffer;
1128 buffer_allocated *= 2;
1129 buffer = xrealloc (buffer, buffer_allocated);
1130 bufptr = buffer + bytes;
1131 }
1132
1133 for (i = 0; i < tlen; i++)
1134 {
1135 *bufptr++ = buf[i + offset];
1136 if (buf[i + offset] == '\000')
1137 {
1138 nbytes_read += i + 1;
1139 goto done;
1140 }
1141 }
1142
1143 memaddr += tlen;
1144 len -= tlen;
1145 nbytes_read += tlen;
1146 }
c5aa993b 1147done:
6217bf3e 1148 *string = buffer;
c906108c
SS
1149 if (errnop != NULL)
1150 *errnop = errcode;
c906108c
SS
1151 return nbytes_read;
1152}
1153
07b82ea5
PA
1154struct target_section_table *
1155target_get_section_table (struct target_ops *target)
1156{
1157 struct target_ops *t;
1158
1159 if (targetdebug)
1160 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1161
1162 for (t = target; t != NULL; t = t->beneath)
1163 if (t->to_get_section_table != NULL)
1164 return (*t->to_get_section_table) (t);
1165
1166 return NULL;
1167}
1168
8db32d44 1169/* Find a section containing ADDR. */
07b82ea5 1170
0542c86d 1171struct target_section *
8db32d44
AC
1172target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1173{
07b82ea5 1174 struct target_section_table *table = target_get_section_table (target);
0542c86d 1175 struct target_section *secp;
07b82ea5
PA
1176
1177 if (table == NULL)
1178 return NULL;
1179
1180 for (secp = table->sections; secp < table->sections_end; secp++)
8db32d44
AC
1181 {
1182 if (addr >= secp->addr && addr < secp->endaddr)
1183 return secp;
1184 }
1185 return NULL;
1186}
1187
7f79c47e
DE
1188/* Perform a partial memory transfer.
1189 For docs see target.h, to_xfer_partial. */
cf7a04e8
DJ
1190
1191static LONGEST
4e5d721f
DE
1192memory_xfer_partial (struct target_ops *ops, enum target_object object,
1193 void *readbuf, const void *writebuf, ULONGEST memaddr,
1194 LONGEST len)
0779438d 1195{
cf7a04e8
DJ
1196 LONGEST res;
1197 int reg_len;
1198 struct mem_region *region;
4e5d721f 1199 struct inferior *inf;
cf7a04e8
DJ
1200
1201 /* Zero length requests are ok and require no work. */
1202 if (len == 0)
1203 return 0;
1204
07b82ea5
PA
1205 /* For accesses to unmapped overlay sections, read directly from
1206 files. Must do this first, as MEMADDR may need adjustment. */
1207 if (readbuf != NULL && overlay_debugging)
1208 {
1209 struct obj_section *section = find_pc_overlay (memaddr);
1210 if (pc_in_unmapped_range (memaddr, section))
1211 {
1212 struct target_section_table *table
1213 = target_get_section_table (ops);
1214 const char *section_name = section->the_bfd_section->name;
1215 memaddr = overlay_mapped_address (memaddr, section);
1216 return section_table_xfer_memory_partial (readbuf, writebuf,
1217 memaddr, len,
1218 table->sections,
1219 table->sections_end,
1220 section_name);
1221 }
1222 }
1223
1224 /* Try the executable files, if "trust-readonly-sections" is set. */
cf7a04e8
DJ
1225 if (readbuf != NULL && trust_readonly)
1226 {
0542c86d 1227 struct target_section *secp;
07b82ea5 1228 struct target_section_table *table;
cf7a04e8
DJ
1229
1230 secp = target_section_by_addr (ops, memaddr);
1231 if (secp != NULL
1232 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1233 & SEC_READONLY))
07b82ea5
PA
1234 {
1235 table = target_get_section_table (ops);
1236 return section_table_xfer_memory_partial (readbuf, writebuf,
1237 memaddr, len,
1238 table->sections,
1239 table->sections_end,
1240 NULL);
1241 }
98646950
UW
1242 }
1243
cf7a04e8
DJ
1244 /* Try GDB's internal data cache. */
1245 region = lookup_mem_region (memaddr);
4b5752d0
VP
1246 /* region->hi == 0 means there's no upper bound. */
1247 if (memaddr + len < region->hi || region->hi == 0)
cf7a04e8
DJ
1248 reg_len = len;
1249 else
1250 reg_len = region->hi - memaddr;
1251
1252 switch (region->attrib.mode)
1253 {
1254 case MEM_RO:
1255 if (writebuf != NULL)
1256 return -1;
1257 break;
1258
1259 case MEM_WO:
1260 if (readbuf != NULL)
1261 return -1;
1262 break;
a76d924d
DJ
1263
1264 case MEM_FLASH:
1265 /* We only support writing to flash during "load" for now. */
1266 if (writebuf != NULL)
1267 error (_("Writing to flash memory forbidden in this context"));
1268 break;
4b5752d0
VP
1269
1270 case MEM_NONE:
1271 return -1;
cf7a04e8
DJ
1272 }
1273
6c95b8df
PA
1274 if (!ptid_equal (inferior_ptid, null_ptid))
1275 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1276 else
1277 inf = NULL;
4e5d721f
DE
1278
1279 if (inf != NULL
1280 && (region->attrib.cache
1281 || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
cf7a04e8 1282 {
cf7a04e8 1283 if (readbuf != NULL)
25f122dc 1284 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
cf7a04e8
DJ
1285 reg_len, 0);
1286 else
1287 /* FIXME drow/2006-08-09: If we're going to preserve const
1288 correctness dcache_xfer_memory should take readbuf and
1289 writebuf. */
25f122dc 1290 res = dcache_xfer_memory (ops, target_dcache, memaddr,
cf7a04e8
DJ
1291 (void *) writebuf,
1292 reg_len, 1);
1293 if (res <= 0)
1294 return -1;
1295 else
8defab1a
DJ
1296 {
1297 if (readbuf && !show_memory_breakpoints)
1298 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1299 return res;
1300 }
cf7a04e8
DJ
1301 }
1302
1303 /* If none of those methods found the memory we wanted, fall back
1304 to a target partial transfer. Normally a single call to
1305 to_xfer_partial is enough; if it doesn't recognize an object
1306 it will call the to_xfer_partial of the next target down.
1307 But for memory this won't do. Memory is the only target
1308 object which can be read from more than one valid target.
1309 A core file, for instance, could have some of memory but
1310 delegate other bits to the target below it. So, we must
1311 manually try all targets. */
1312
1313 do
1314 {
1315 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
4b5752d0 1316 readbuf, writebuf, memaddr, reg_len);
cf7a04e8 1317 if (res > 0)
8defab1a 1318 break;
cf7a04e8 1319
5ad3a4ca
DJ
1320 /* We want to continue past core files to executables, but not
1321 past a running target's memory. */
c35b1492 1322 if (ops->to_has_all_memory (ops))
8defab1a 1323 break;
5ad3a4ca 1324
cf7a04e8
DJ
1325 ops = ops->beneath;
1326 }
1327 while (ops != NULL);
1328
8defab1a
DJ
1329 if (readbuf && !show_memory_breakpoints)
1330 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1331
41dcd03f
DE
1332 /* Make sure the cache gets updated no matter what - if we are writing
1333 to the stack. Even if this write is not tagged as such, we still need
1334 to update the cache. */
1335
1336 if (res > 0
1337 && inf != NULL
1338 && writebuf != NULL
1339 && !region->attrib.cache
1340 && stack_cache_enabled_p
1341 && object != TARGET_OBJECT_STACK_MEMORY)
1342 {
7d4f32d3 1343 dcache_update (target_dcache, memaddr, (void *) writebuf, res);
41dcd03f
DE
1344 }
1345
cf7a04e8
DJ
1346 /* If we still haven't got anything, return the last error. We
1347 give up. */
1348 return res;
0779438d
AC
1349}
1350
8defab1a
DJ
1351static void
1352restore_show_memory_breakpoints (void *arg)
1353{
1354 show_memory_breakpoints = (uintptr_t) arg;
1355}
1356
1357struct cleanup *
1358make_show_memory_breakpoints_cleanup (int show)
1359{
1360 int current = show_memory_breakpoints;
1361 show_memory_breakpoints = show;
1362
1363 return make_cleanup (restore_show_memory_breakpoints,
1364 (void *) (uintptr_t) current);
1365}
1366
7f79c47e
DE
1367/* For docs see target.h, to_xfer_partial. */
1368
27394598
AC
1369static LONGEST
1370target_xfer_partial (struct target_ops *ops,
1371 enum target_object object, const char *annex,
1372 void *readbuf, const void *writebuf,
1373 ULONGEST offset, LONGEST len)
1374{
1375 LONGEST retval;
1376
1377 gdb_assert (ops->to_xfer_partial != NULL);
cf7a04e8
DJ
1378
1379 /* If this is a memory transfer, let the memory-specific code
1380 have a look at it instead. Memory transfers are more
1381 complicated. */
4e5d721f
DE
1382 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1383 retval = memory_xfer_partial (ops, object, readbuf,
1384 writebuf, offset, len);
cf7a04e8
DJ
1385 else
1386 {
1387 enum target_object raw_object = object;
1388
1389 /* If this is a raw memory transfer, request the normal
1390 memory object from other layers. */
1391 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1392 raw_object = TARGET_OBJECT_MEMORY;
1393
1394 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1395 writebuf, offset, len);
1396 }
1397
27394598
AC
1398 if (targetdebug)
1399 {
1400 const unsigned char *myaddr = NULL;
1401
1402 fprintf_unfiltered (gdb_stdlog,
53b71562 1403 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
27394598
AC
1404 ops->to_shortname,
1405 (int) object,
1406 (annex ? annex : "(null)"),
53b71562
JB
1407 host_address_to_string (readbuf),
1408 host_address_to_string (writebuf),
0b1553bc
UW
1409 core_addr_to_string_nz (offset),
1410 plongest (len), plongest (retval));
27394598
AC
1411
1412 if (readbuf)
1413 myaddr = readbuf;
1414 if (writebuf)
1415 myaddr = writebuf;
1416 if (retval > 0 && myaddr != NULL)
1417 {
1418 int i;
2bc416ba 1419
27394598
AC
1420 fputs_unfiltered (", bytes =", gdb_stdlog);
1421 for (i = 0; i < retval; i++)
1422 {
53b71562 1423 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
27394598
AC
1424 {
1425 if (targetdebug < 2 && i > 0)
1426 {
1427 fprintf_unfiltered (gdb_stdlog, " ...");
1428 break;
1429 }
1430 fprintf_unfiltered (gdb_stdlog, "\n");
1431 }
2bc416ba 1432
27394598
AC
1433 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1434 }
1435 }
2bc416ba 1436
27394598
AC
1437 fputc_unfiltered ('\n', gdb_stdlog);
1438 }
1439 return retval;
1440}
1441
c906108c
SS
1442/* Read LEN bytes of target memory at address MEMADDR, placing the results in
1443 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1444 if any error occurs.
1445
1446 If an error occurs, no guarantee is made about the contents of the data at
1447 MYADDR. In particular, the caller should not depend upon partial reads
1448 filling the buffer with good data. There is no way for the caller to know
1449 how much good data might have been transfered anyway. Callers that can
cf7a04e8
DJ
1450 deal with partial reads should call target_read (which will retry until
1451 it makes no progress, and then return how much was transferred). */
c906108c
SS
1452
1453int
fc1a4b47 1454target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 1455{
c35b1492
PA
1456 /* Dispatch to the topmost target, not the flattened current_target.
1457 Memory accesses check target->to_has_(all_)memory, and the
1458 flattened target doesn't inherit those. */
1459 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1460 myaddr, memaddr, len) == len)
1461 return 0;
0779438d 1462 else
cf7a04e8 1463 return EIO;
c906108c
SS
1464}
1465
4e5d721f
DE
1466/* Like target_read_memory, but specify explicitly that this is a read from
1467 the target's stack. This may trigger different cache behavior. */
1468
1469int
1470target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1471{
1472 /* Dispatch to the topmost target, not the flattened current_target.
1473 Memory accesses check target->to_has_(all_)memory, and the
1474 flattened target doesn't inherit those. */
1475
1476 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1477 myaddr, memaddr, len) == len)
1478 return 0;
1479 else
1480 return EIO;
1481}
1482
7f79c47e
DE
1483/* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1484 Returns either 0 for success or an errno value if any error occurs.
1485 If an error occurs, no guarantee is made about how much data got written.
1486 Callers that can deal with partial writes should call target_write. */
1487
c906108c 1488int
fc1a4b47 1489target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
c906108c 1490{
c35b1492
PA
1491 /* Dispatch to the topmost target, not the flattened current_target.
1492 Memory accesses check target->to_has_(all_)memory, and the
1493 flattened target doesn't inherit those. */
1494 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1495 myaddr, memaddr, len) == len)
1496 return 0;
0779438d 1497 else
cf7a04e8 1498 return EIO;
c906108c 1499}
c5aa993b 1500
fd79ecee
DJ
1501/* Fetch the target's memory map. */
1502
1503VEC(mem_region_s) *
1504target_memory_map (void)
1505{
1506 VEC(mem_region_s) *result;
1507 struct mem_region *last_one, *this_one;
1508 int ix;
1509 struct target_ops *t;
1510
1511 if (targetdebug)
1512 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1513
1514 for (t = current_target.beneath; t != NULL; t = t->beneath)
1515 if (t->to_memory_map != NULL)
1516 break;
1517
1518 if (t == NULL)
1519 return NULL;
1520
1521 result = t->to_memory_map (t);
1522 if (result == NULL)
1523 return NULL;
1524
1525 qsort (VEC_address (mem_region_s, result),
1526 VEC_length (mem_region_s, result),
1527 sizeof (struct mem_region), mem_region_cmp);
1528
1529 /* Check that regions do not overlap. Simultaneously assign
1530 a numbering for the "mem" commands to use to refer to
1531 each region. */
1532 last_one = NULL;
1533 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1534 {
1535 this_one->number = ix;
1536
1537 if (last_one && last_one->hi > this_one->lo)
1538 {
1539 warning (_("Overlapping regions in memory map: ignoring"));
1540 VEC_free (mem_region_s, result);
1541 return NULL;
1542 }
1543 last_one = this_one;
1544 }
1545
1546 return result;
1547}
1548
a76d924d
DJ
1549void
1550target_flash_erase (ULONGEST address, LONGEST length)
1551{
1552 struct target_ops *t;
1553
1554 for (t = current_target.beneath; t != NULL; t = t->beneath)
1555 if (t->to_flash_erase != NULL)
1556 {
1557 if (targetdebug)
1558 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
5af949e3 1559 hex_string (address), phex (length, 0));
8944021f
DJ
1560 t->to_flash_erase (t, address, length);
1561 return;
a76d924d
DJ
1562 }
1563
1564 tcomplain ();
1565}
1566
1567void
1568target_flash_done (void)
1569{
1570 struct target_ops *t;
1571
1572 for (t = current_target.beneath; t != NULL; t = t->beneath)
1573 if (t->to_flash_done != NULL)
1574 {
1575 if (targetdebug)
1576 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
8944021f
DJ
1577 t->to_flash_done (t);
1578 return;
a76d924d
DJ
1579 }
1580
1581 tcomplain ();
1582}
1583
920d2a44
AC
1584static void
1585show_trust_readonly (struct ui_file *file, int from_tty,
1586 struct cmd_list_element *c, const char *value)
1587{
1588 fprintf_filtered (file, _("\
1589Mode for reading from readonly sections is %s.\n"),
1590 value);
1591}
3a11626d 1592
1e3ff5ad
AC
1593/* More generic transfers. */
1594
0088c768 1595static LONGEST
8aa91c1e 1596default_xfer_partial (struct target_ops *ops, enum target_object object,
2bc416ba 1597 const char *annex, gdb_byte *readbuf,
1b0ba102 1598 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
0088c768
AC
1599{
1600 if (object == TARGET_OBJECT_MEMORY
c8e73a31
AC
1601 && ops->deprecated_xfer_memory != NULL)
1602 /* If available, fall back to the target's
1603 "deprecated_xfer_memory" method. */
0088c768 1604 {
4b8a223f 1605 int xfered = -1;
0088c768 1606 errno = 0;
4b8a223f
AC
1607 if (writebuf != NULL)
1608 {
1609 void *buffer = xmalloc (len);
1610 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1611 memcpy (buffer, writebuf, len);
c8e73a31
AC
1612 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1613 1/*write*/, NULL, ops);
4b8a223f
AC
1614 do_cleanups (cleanup);
1615 }
1616 if (readbuf != NULL)
244e85c8
MS
1617 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1618 0/*read*/, NULL, ops);
0088c768
AC
1619 if (xfered > 0)
1620 return xfered;
1621 else if (xfered == 0 && errno == 0)
c8e73a31
AC
1622 /* "deprecated_xfer_memory" uses 0, cross checked against
1623 ERRNO as one indication of an error. */
0088c768
AC
1624 return 0;
1625 else
1626 return -1;
1627 }
1628 else if (ops->beneath != NULL)
cf7a04e8
DJ
1629 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1630 readbuf, writebuf, offset, len);
1631 else
1632 return -1;
1633}
1634
1635/* The xfer_partial handler for the topmost target. Unlike the default,
1636 it does not need to handle memory specially; it just passes all
1637 requests down the stack. */
1638
1639static LONGEST
1640current_xfer_partial (struct target_ops *ops, enum target_object object,
1641 const char *annex, gdb_byte *readbuf,
1642 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1643{
1644 if (ops->beneath != NULL)
1645 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1646 readbuf, writebuf, offset, len);
0088c768
AC
1647 else
1648 return -1;
1649}
1650
7f79c47e 1651/* Target vector read/write partial wrapper functions. */
0088c768 1652
13547ab6 1653static LONGEST
1e3ff5ad
AC
1654target_read_partial (struct target_ops *ops,
1655 enum target_object object,
1b0ba102 1656 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1657 ULONGEST offset, LONGEST len)
1658{
27394598 1659 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1e3ff5ad
AC
1660}
1661
13547ab6 1662static LONGEST
1e3ff5ad
AC
1663target_write_partial (struct target_ops *ops,
1664 enum target_object object,
1b0ba102 1665 const char *annex, const gdb_byte *buf,
1e3ff5ad
AC
1666 ULONGEST offset, LONGEST len)
1667{
27394598 1668 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1e3ff5ad
AC
1669}
1670
1671/* Wrappers to perform the full transfer. */
7f79c47e
DE
1672
1673/* For docs on target_read see target.h. */
1674
1e3ff5ad
AC
1675LONGEST
1676target_read (struct target_ops *ops,
1677 enum target_object object,
1b0ba102 1678 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1679 ULONGEST offset, LONGEST len)
1680{
1681 LONGEST xfered = 0;
1682 while (xfered < len)
1683 {
0088c768 1684 LONGEST xfer = target_read_partial (ops, object, annex,
fc1a4b47 1685 (gdb_byte *) buf + xfered,
0088c768 1686 offset + xfered, len - xfered);
1e3ff5ad 1687 /* Call an observer, notifying them of the xfer progress? */
13547ab6
DJ
1688 if (xfer == 0)
1689 return xfered;
1690 if (xfer < 0)
0088c768 1691 return -1;
1e3ff5ad
AC
1692 xfered += xfer;
1693 QUIT;
1694 }
1695 return len;
1696}
1697
d5086790
VP
1698LONGEST
1699target_read_until_error (struct target_ops *ops,
1700 enum target_object object,
1701 const char *annex, gdb_byte *buf,
1702 ULONGEST offset, LONGEST len)
1703{
1704 LONGEST xfered = 0;
1705 while (xfered < len)
1706 {
1707 LONGEST xfer = target_read_partial (ops, object, annex,
1708 (gdb_byte *) buf + xfered,
1709 offset + xfered, len - xfered);
1710 /* Call an observer, notifying them of the xfer progress? */
1711 if (xfer == 0)
1712 return xfered;
1713 if (xfer < 0)
1714 {
1715 /* We've got an error. Try to read in smaller blocks. */
1716 ULONGEST start = offset + xfered;
1717 ULONGEST remaining = len - xfered;
1718 ULONGEST half;
1719
1720 /* If an attempt was made to read a random memory address,
1721 it's likely that the very first byte is not accessible.
1722 Try reading the first byte, to avoid doing log N tries
1723 below. */
1724 xfer = target_read_partial (ops, object, annex,
1725 (gdb_byte *) buf + xfered, start, 1);
1726 if (xfer <= 0)
1727 return xfered;
1728 start += 1;
1729 remaining -= 1;
1730 half = remaining/2;
1731
1732 while (half > 0)
1733 {
1734 xfer = target_read_partial (ops, object, annex,
1735 (gdb_byte *) buf + xfered,
1736 start, half);
1737 if (xfer == 0)
1738 return xfered;
1739 if (xfer < 0)
1740 {
1741 remaining = half;
1742 }
1743 else
1744 {
1745 /* We have successfully read the first half. So, the
1746 error must be in the second half. Adjust start and
1747 remaining to point at the second half. */
1748 xfered += xfer;
1749 start += xfer;
1750 remaining -= xfer;
1751 }
1752 half = remaining/2;
1753 }
1754
1755 return xfered;
1756 }
1757 xfered += xfer;
1758 QUIT;
1759 }
1760 return len;
1761}
1762
cf7a04e8
DJ
1763/* An alternative to target_write with progress callbacks. */
1764
1e3ff5ad 1765LONGEST
cf7a04e8
DJ
1766target_write_with_progress (struct target_ops *ops,
1767 enum target_object object,
1768 const char *annex, const gdb_byte *buf,
1769 ULONGEST offset, LONGEST len,
1770 void (*progress) (ULONGEST, void *), void *baton)
1e3ff5ad
AC
1771{
1772 LONGEST xfered = 0;
a76d924d
DJ
1773
1774 /* Give the progress callback a chance to set up. */
1775 if (progress)
1776 (*progress) (0, baton);
1777
1e3ff5ad
AC
1778 while (xfered < len)
1779 {
1780 LONGEST xfer = target_write_partial (ops, object, annex,
fc1a4b47 1781 (gdb_byte *) buf + xfered,
1e3ff5ad 1782 offset + xfered, len - xfered);
cf7a04e8 1783
13547ab6
DJ
1784 if (xfer == 0)
1785 return xfered;
1786 if (xfer < 0)
0088c768 1787 return -1;
cf7a04e8
DJ
1788
1789 if (progress)
1790 (*progress) (xfer, baton);
1791
1e3ff5ad
AC
1792 xfered += xfer;
1793 QUIT;
1794 }
1795 return len;
1796}
1797
7f79c47e
DE
1798/* For docs on target_write see target.h. */
1799
cf7a04e8
DJ
1800LONGEST
1801target_write (struct target_ops *ops,
1802 enum target_object object,
1803 const char *annex, const gdb_byte *buf,
1804 ULONGEST offset, LONGEST len)
1805{
1806 return target_write_with_progress (ops, object, annex, buf, offset, len,
1807 NULL, NULL);
1808}
1809
159f81f3
DJ
1810/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1811 the size of the transferred data. PADDING additional bytes are
1812 available in *BUF_P. This is a helper function for
1813 target_read_alloc; see the declaration of that function for more
1814 information. */
13547ab6 1815
159f81f3
DJ
1816static LONGEST
1817target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1818 const char *annex, gdb_byte **buf_p, int padding)
13547ab6
DJ
1819{
1820 size_t buf_alloc, buf_pos;
1821 gdb_byte *buf;
1822 LONGEST n;
1823
1824 /* This function does not have a length parameter; it reads the
1825 entire OBJECT). Also, it doesn't support objects fetched partly
1826 from one target and partly from another (in a different stratum,
1827 e.g. a core file and an executable). Both reasons make it
1828 unsuitable for reading memory. */
1829 gdb_assert (object != TARGET_OBJECT_MEMORY);
1830
1831 /* Start by reading up to 4K at a time. The target will throttle
1832 this number down if necessary. */
1833 buf_alloc = 4096;
1834 buf = xmalloc (buf_alloc);
1835 buf_pos = 0;
1836 while (1)
1837 {
1838 n = target_read_partial (ops, object, annex, &buf[buf_pos],
159f81f3 1839 buf_pos, buf_alloc - buf_pos - padding);
13547ab6
DJ
1840 if (n < 0)
1841 {
1842 /* An error occurred. */
1843 xfree (buf);
1844 return -1;
1845 }
1846 else if (n == 0)
1847 {
1848 /* Read all there was. */
1849 if (buf_pos == 0)
1850 xfree (buf);
1851 else
1852 *buf_p = buf;
1853 return buf_pos;
1854 }
1855
1856 buf_pos += n;
1857
1858 /* If the buffer is filling up, expand it. */
1859 if (buf_alloc < buf_pos * 2)
1860 {
1861 buf_alloc *= 2;
1862 buf = xrealloc (buf, buf_alloc);
1863 }
1864
1865 QUIT;
1866 }
1867}
1868
159f81f3
DJ
1869/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1870 the size of the transferred data. See the declaration in "target.h"
1871 function for more information about the return value. */
1872
1873LONGEST
1874target_read_alloc (struct target_ops *ops, enum target_object object,
1875 const char *annex, gdb_byte **buf_p)
1876{
1877 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1878}
1879
1880/* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1881 returned as a string, allocated using xmalloc. If an error occurs
1882 or the transfer is unsupported, NULL is returned. Empty objects
1883 are returned as allocated but empty strings. A warning is issued
1884 if the result contains any embedded NUL bytes. */
1885
1886char *
1887target_read_stralloc (struct target_ops *ops, enum target_object object,
1888 const char *annex)
1889{
1890 gdb_byte *buffer;
1891 LONGEST transferred;
1892
1893 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1894
1895 if (transferred < 0)
1896 return NULL;
1897
1898 if (transferred == 0)
1899 return xstrdup ("");
1900
1901 buffer[transferred] = 0;
1902 if (strlen (buffer) < transferred)
1903 warning (_("target object %d, annex %s, "
1904 "contained unexpected null characters"),
1905 (int) object, annex ? annex : "(none)");
1906
1907 return (char *) buffer;
1908}
1909
b6591e8b
AC
1910/* Memory transfer methods. */
1911
1912void
1b0ba102 1913get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
b6591e8b
AC
1914 LONGEST len)
1915{
07b82ea5
PA
1916 /* This method is used to read from an alternate, non-current
1917 target. This read must bypass the overlay support (as symbols
1918 don't match this target), and GDB's internal cache (wrong cache
1919 for this target). */
1920 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
b6591e8b
AC
1921 != len)
1922 memory_error (EIO, addr);
1923}
1924
1925ULONGEST
1926get_target_memory_unsigned (struct target_ops *ops,
e17a4113 1927 CORE_ADDR addr, int len, enum bfd_endian byte_order)
b6591e8b 1928{
f6519ebc 1929 gdb_byte buf[sizeof (ULONGEST)];
b6591e8b
AC
1930
1931 gdb_assert (len <= sizeof (buf));
1932 get_target_memory (ops, addr, buf, len);
e17a4113 1933 return extract_unsigned_integer (buf, len, byte_order);
b6591e8b
AC
1934}
1935
c906108c 1936static void
fba45db2 1937target_info (char *args, int from_tty)
c906108c
SS
1938{
1939 struct target_ops *t;
c906108c 1940 int has_all_mem = 0;
c5aa993b 1941
c906108c 1942 if (symfile_objfile != NULL)
a3f17187 1943 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
c906108c 1944
258b763a 1945 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 1946 {
c35b1492 1947 if (!(*t->to_has_memory) (t))
c906108c
SS
1948 continue;
1949
c5aa993b 1950 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1951 continue;
1952 if (has_all_mem)
a3f17187 1953 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
c5aa993b
JM
1954 printf_unfiltered ("%s:\n", t->to_longname);
1955 (t->to_files_info) (t);
c35b1492 1956 has_all_mem = (*t->to_has_all_memory) (t);
c906108c
SS
1957 }
1958}
1959
fd79ecee
DJ
1960/* This function is called before any new inferior is created, e.g.
1961 by running a program, attaching, or connecting to a target.
1962 It cleans up any state from previous invocations which might
1963 change between runs. This is a subset of what target_preopen
1964 resets (things which might change between targets). */
1965
1966void
1967target_pre_inferior (int from_tty)
1968{
b9db4ced
UW
1969 /* Clear out solib state. Otherwise the solib state of the previous
1970 inferior might have survived and is entirely wrong for the new
1971 target. This has been observed on GNU/Linux using glibc 2.3. How
1972 to reproduce:
1973
1974 bash$ ./foo&
1975 [1] 4711
1976 bash$ ./foo&
1977 [1] 4712
1978 bash$ gdb ./foo
1979 [...]
1980 (gdb) attach 4711
1981 (gdb) detach
1982 (gdb) attach 4712
1983 Cannot access memory at address 0xdeadbeef
1984 */
b9db4ced 1985
50c71eaf
PA
1986 /* In some OSs, the shared library list is the same/global/shared
1987 across inferiors. If code is shared between processes, so are
1988 memory regions and features. */
1989 if (!gdbarch_has_global_solist (target_gdbarch))
1990 {
1991 no_shared_libraries (NULL, from_tty);
1992
1993 invalidate_target_mem_regions ();
424163ea 1994
50c71eaf
PA
1995 target_clear_description ();
1996 }
fd79ecee
DJ
1997}
1998
b8fa0bfa
PA
1999/* Callback for iterate_over_inferiors. Gets rid of the given
2000 inferior. */
2001
2002static int
2003dispose_inferior (struct inferior *inf, void *args)
2004{
2005 struct thread_info *thread;
2006
2007 thread = any_thread_of_process (inf->pid);
2008 if (thread)
2009 {
2010 switch_to_thread (thread->ptid);
2011
2012 /* Core inferiors actually should be detached, not killed. */
2013 if (target_has_execution)
2014 target_kill ();
2015 else
2016 target_detach (NULL, 0);
2017 }
2018
2019 return 0;
2020}
2021
c906108c
SS
2022/* This is to be called by the open routine before it does
2023 anything. */
2024
2025void
fba45db2 2026target_preopen (int from_tty)
c906108c 2027{
c5aa993b 2028 dont_repeat ();
c906108c 2029
b8fa0bfa 2030 if (have_inferiors ())
c5aa993b 2031 {
adf40b2e 2032 if (!from_tty
b8fa0bfa
PA
2033 || !have_live_inferiors ()
2034 || query (_("A program is being debugged already. Kill it? ")))
2035 iterate_over_inferiors (dispose_inferior, NULL);
c906108c 2036 else
8a3fe4f8 2037 error (_("Program not killed."));
c906108c
SS
2038 }
2039
2040 /* Calling target_kill may remove the target from the stack. But if
2041 it doesn't (which seems like a win for UDI), remove it now. */
87ab71f0
PA
2042 /* Leave the exec target, though. The user may be switching from a
2043 live process to a core of the same program. */
2044 pop_all_targets_above (file_stratum, 0);
fd79ecee
DJ
2045
2046 target_pre_inferior (from_tty);
c906108c
SS
2047}
2048
2049/* Detach a target after doing deferred register stores. */
2050
2051void
fba45db2 2052target_detach (char *args, int from_tty)
c906108c 2053{
136d6dae
VP
2054 struct target_ops* t;
2055
2567c7d9 2056 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2057 /* Don't remove global breakpoints here. They're removed on
2058 disconnection from the target. */
2059 ;
2060 else
2061 /* If we're in breakpoints-always-inserted mode, have to remove
2062 them before detaching. */
6c95b8df 2063 remove_breakpoints_pid (PIDGET (inferior_ptid));
74960c60 2064
136d6dae
VP
2065 for (t = current_target.beneath; t != NULL; t = t->beneath)
2066 {
2067 if (t->to_detach != NULL)
2068 {
2069 t->to_detach (t, args, from_tty);
947b8855
PA
2070 if (targetdebug)
2071 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2072 args, from_tty);
136d6dae
VP
2073 return;
2074 }
2075 }
2076
2077 internal_error (__FILE__, __LINE__, "could not find a target to detach");
c906108c
SS
2078}
2079
6ad8ae5c
DJ
2080void
2081target_disconnect (char *args, int from_tty)
2082{
597320e7
DJ
2083 struct target_ops *t;
2084
50c71eaf
PA
2085 /* If we're in breakpoints-always-inserted mode or if breakpoints
2086 are global across processes, we have to remove them before
2087 disconnecting. */
74960c60
VP
2088 remove_breakpoints ();
2089
597320e7
DJ
2090 for (t = current_target.beneath; t != NULL; t = t->beneath)
2091 if (t->to_disconnect != NULL)
2092 {
2093 if (targetdebug)
2094 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2095 args, from_tty);
2096 t->to_disconnect (t, args, from_tty);
2097 return;
2098 }
2099
2100 tcomplain ();
6ad8ae5c
DJ
2101}
2102
117de6a9 2103ptid_t
47608cb1 2104target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
117de6a9
PA
2105{
2106 struct target_ops *t;
2107
2108 for (t = current_target.beneath; t != NULL; t = t->beneath)
2109 {
2110 if (t->to_wait != NULL)
2111 {
47608cb1 2112 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
117de6a9
PA
2113
2114 if (targetdebug)
2115 {
2116 char *status_string;
2117
2118 status_string = target_waitstatus_to_string (status);
2119 fprintf_unfiltered (gdb_stdlog,
2120 "target_wait (%d, status) = %d, %s\n",
2121 PIDGET (ptid), PIDGET (retval),
2122 status_string);
2123 xfree (status_string);
2124 }
2125
2126 return retval;
2127 }
2128 }
2129
2130 noprocess ();
2131}
2132
2133char *
2134target_pid_to_str (ptid_t ptid)
2135{
2136 struct target_ops *t;
2137
2138 for (t = current_target.beneath; t != NULL; t = t->beneath)
2139 {
2140 if (t->to_pid_to_str != NULL)
2141 return (*t->to_pid_to_str) (t, ptid);
2142 }
2143
2144 return normal_pid_to_str (ptid);
2145}
2146
e1ac3328
VP
2147void
2148target_resume (ptid_t ptid, int step, enum target_signal signal)
2149{
28439f5e
PA
2150 struct target_ops *t;
2151
4e5d721f 2152 target_dcache_invalidate ();
28439f5e
PA
2153
2154 for (t = current_target.beneath; t != NULL; t = t->beneath)
2155 {
2156 if (t->to_resume != NULL)
2157 {
2158 t->to_resume (t, ptid, step, signal);
2159 if (targetdebug)
2160 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2161 PIDGET (ptid),
2162 step ? "step" : "continue",
2163 target_signal_to_name (signal));
2164
2165 set_executing (ptid, 1);
2166 set_running (ptid, 1);
edb3359d 2167 clear_inline_frame_state (ptid);
28439f5e
PA
2168 return;
2169 }
2170 }
2171
2172 noprocess ();
e1ac3328 2173}
ee057212
DJ
2174/* Look through the list of possible targets for a target that can
2175 follow forks. */
2176
2177int
2178target_follow_fork (int follow_child)
2179{
2180 struct target_ops *t;
2181
2182 for (t = current_target.beneath; t != NULL; t = t->beneath)
2183 {
2184 if (t->to_follow_fork != NULL)
2185 {
2186 int retval = t->to_follow_fork (t, follow_child);
2187 if (targetdebug)
2188 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2189 follow_child, retval);
2190 return retval;
2191 }
2192 }
2193
2194 /* Some target returned a fork event, but did not know how to follow it. */
2195 internal_error (__FILE__, __LINE__,
2196 "could not find a target to follow fork");
2197}
2198
136d6dae
VP
2199void
2200target_mourn_inferior (void)
2201{
2202 struct target_ops *t;
2203 for (t = current_target.beneath; t != NULL; t = t->beneath)
2204 {
2205 if (t->to_mourn_inferior != NULL)
2206 {
2207 t->to_mourn_inferior (t);
947b8855
PA
2208 if (targetdebug)
2209 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2210
2211 /* We no longer need to keep handles on any of the object files.
2212 Make sure to release them to avoid unnecessarily locking any
2213 of them while we're not actually debugging. */
2214 bfd_cache_close_all ();
2215
136d6dae
VP
2216 return;
2217 }
2218 }
2219
2220 internal_error (__FILE__, __LINE__,
2221 "could not find a target to follow mourn inferiour");
2222}
2223
424163ea
DJ
2224/* Look for a target which can describe architectural features, starting
2225 from TARGET. If we find one, return its description. */
2226
2227const struct target_desc *
2228target_read_description (struct target_ops *target)
2229{
2230 struct target_ops *t;
2231
2232 for (t = target; t != NULL; t = t->beneath)
2233 if (t->to_read_description != NULL)
2234 {
2235 const struct target_desc *tdesc;
2236
2237 tdesc = t->to_read_description (t);
2238 if (tdesc)
2239 return tdesc;
2240 }
2241
2242 return NULL;
2243}
2244
08388c79
DE
2245/* The default implementation of to_search_memory.
2246 This implements a basic search of memory, reading target memory and
2247 performing the search here (as opposed to performing the search in on the
2248 target side with, for example, gdbserver). */
2249
2250int
2251simple_search_memory (struct target_ops *ops,
2252 CORE_ADDR start_addr, ULONGEST search_space_len,
2253 const gdb_byte *pattern, ULONGEST pattern_len,
2254 CORE_ADDR *found_addrp)
2255{
2256 /* NOTE: also defined in find.c testcase. */
2257#define SEARCH_CHUNK_SIZE 16000
2258 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2259 /* Buffer to hold memory contents for searching. */
2260 gdb_byte *search_buf;
2261 unsigned search_buf_size;
2262 struct cleanup *old_cleanups;
2263
2264 search_buf_size = chunk_size + pattern_len - 1;
2265
2266 /* No point in trying to allocate a buffer larger than the search space. */
2267 if (search_space_len < search_buf_size)
2268 search_buf_size = search_space_len;
2269
2270 search_buf = malloc (search_buf_size);
2271 if (search_buf == NULL)
5e1471f5 2272 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2273 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2274
2275 /* Prime the search buffer. */
2276
2277 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2278 search_buf, start_addr, search_buf_size) != search_buf_size)
2279 {
5e1471f5 2280 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2281 hex_string (start_addr));
2282 do_cleanups (old_cleanups);
2283 return -1;
2284 }
2285
2286 /* Perform the search.
2287
2288 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2289 When we've scanned N bytes we copy the trailing bytes to the start and
2290 read in another N bytes. */
2291
2292 while (search_space_len >= pattern_len)
2293 {
2294 gdb_byte *found_ptr;
2295 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2296
2297 found_ptr = memmem (search_buf, nr_search_bytes,
2298 pattern, pattern_len);
2299
2300 if (found_ptr != NULL)
2301 {
2302 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2303 *found_addrp = found_addr;
2304 do_cleanups (old_cleanups);
2305 return 1;
2306 }
2307
2308 /* Not found in this chunk, skip to next chunk. */
2309
2310 /* Don't let search_space_len wrap here, it's unsigned. */
2311 if (search_space_len >= chunk_size)
2312 search_space_len -= chunk_size;
2313 else
2314 search_space_len = 0;
2315
2316 if (search_space_len >= pattern_len)
2317 {
2318 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2319 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2320 int nr_to_read;
2321
2322 /* Copy the trailing part of the previous iteration to the front
2323 of the buffer for the next iteration. */
2324 gdb_assert (keep_len == pattern_len - 1);
2325 memcpy (search_buf, search_buf + chunk_size, keep_len);
2326
2327 nr_to_read = min (search_space_len - keep_len, chunk_size);
2328
2329 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2330 search_buf + keep_len, read_addr,
2331 nr_to_read) != nr_to_read)
2332 {
5e1471f5 2333 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2334 hex_string (read_addr));
2335 do_cleanups (old_cleanups);
2336 return -1;
2337 }
2338
2339 start_addr += chunk_size;
2340 }
2341 }
2342
2343 /* Not found. */
2344
2345 do_cleanups (old_cleanups);
2346 return 0;
2347}
2348
2349/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2350 sequence of bytes in PATTERN with length PATTERN_LEN.
2351
2352 The result is 1 if found, 0 if not found, and -1 if there was an error
2353 requiring halting of the search (e.g. memory read error).
2354 If the pattern is found the address is recorded in FOUND_ADDRP. */
2355
2356int
2357target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2358 const gdb_byte *pattern, ULONGEST pattern_len,
2359 CORE_ADDR *found_addrp)
2360{
2361 struct target_ops *t;
2362 int found;
2363
2364 /* We don't use INHERIT to set current_target.to_search_memory,
2365 so we have to scan the target stack and handle targetdebug
2366 ourselves. */
2367
2368 if (targetdebug)
2369 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2370 hex_string (start_addr));
2371
2372 for (t = current_target.beneath; t != NULL; t = t->beneath)
2373 if (t->to_search_memory != NULL)
2374 break;
2375
2376 if (t != NULL)
2377 {
2378 found = t->to_search_memory (t, start_addr, search_space_len,
2379 pattern, pattern_len, found_addrp);
2380 }
2381 else
2382 {
2383 /* If a special version of to_search_memory isn't available, use the
2384 simple version. */
c35b1492 2385 found = simple_search_memory (current_target.beneath,
08388c79
DE
2386 start_addr, search_space_len,
2387 pattern, pattern_len, found_addrp);
2388 }
2389
2390 if (targetdebug)
2391 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2392
2393 return found;
2394}
2395
8edfe269
DJ
2396/* Look through the currently pushed targets. If none of them will
2397 be able to restart the currently running process, issue an error
2398 message. */
2399
2400void
2401target_require_runnable (void)
2402{
2403 struct target_ops *t;
2404
2405 for (t = target_stack; t != NULL; t = t->beneath)
2406 {
2407 /* If this target knows how to create a new program, then
2408 assume we will still be able to after killing the current
2409 one. Either killing and mourning will not pop T, or else
2410 find_default_run_target will find it again. */
2411 if (t->to_create_inferior != NULL)
2412 return;
2413
2414 /* Do not worry about thread_stratum targets that can not
2415 create inferiors. Assume they will be pushed again if
2416 necessary, and continue to the process_stratum. */
85e747d2
UW
2417 if (t->to_stratum == thread_stratum
2418 || t->to_stratum == arch_stratum)
8edfe269
DJ
2419 continue;
2420
2421 error (_("\
2422The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2423 t->to_shortname);
2424 }
2425
2426 /* This function is only called if the target is running. In that
2427 case there should have been a process_stratum target and it
2428 should either know how to create inferiors, or not... */
2429 internal_error (__FILE__, __LINE__, "No targets found");
2430}
2431
c906108c
SS
2432/* Look through the list of possible targets for a target that can
2433 execute a run or attach command without any other data. This is
2434 used to locate the default process stratum.
2435
5f667f2d
PA
2436 If DO_MESG is not NULL, the result is always valid (error() is
2437 called for errors); else, return NULL on error. */
c906108c
SS
2438
2439static struct target_ops *
fba45db2 2440find_default_run_target (char *do_mesg)
c906108c
SS
2441{
2442 struct target_ops **t;
2443 struct target_ops *runable = NULL;
2444 int count;
2445
2446 count = 0;
2447
2448 for (t = target_structs; t < target_structs + target_struct_size;
2449 ++t)
2450 {
c5aa993b 2451 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
2452 {
2453 runable = *t;
2454 ++count;
2455 }
2456 }
2457
2458 if (count != 1)
5f667f2d
PA
2459 {
2460 if (do_mesg)
2461 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2462 else
2463 return NULL;
2464 }
c906108c
SS
2465
2466 return runable;
2467}
2468
2469void
136d6dae 2470find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
2471{
2472 struct target_ops *t;
2473
c5aa993b 2474 t = find_default_run_target ("attach");
136d6dae 2475 (t->to_attach) (t, args, from_tty);
c906108c
SS
2476 return;
2477}
2478
c906108c 2479void
136d6dae
VP
2480find_default_create_inferior (struct target_ops *ops,
2481 char *exec_file, char *allargs, char **env,
c27cda74 2482 int from_tty)
c906108c
SS
2483{
2484 struct target_ops *t;
2485
c5aa993b 2486 t = find_default_run_target ("run");
136d6dae 2487 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
2488 return;
2489}
2490
2c0b251b 2491static int
b84876c2
PA
2492find_default_can_async_p (void)
2493{
2494 struct target_ops *t;
2495
5f667f2d
PA
2496 /* This may be called before the target is pushed on the stack;
2497 look for the default process stratum. If there's none, gdb isn't
2498 configured with a native debugger, and target remote isn't
2499 connected yet. */
2500 t = find_default_run_target (NULL);
2501 if (t && t->to_can_async_p)
b84876c2
PA
2502 return (t->to_can_async_p) ();
2503 return 0;
2504}
2505
2c0b251b 2506static int
b84876c2
PA
2507find_default_is_async_p (void)
2508{
2509 struct target_ops *t;
2510
5f667f2d
PA
2511 /* This may be called before the target is pushed on the stack;
2512 look for the default process stratum. If there's none, gdb isn't
2513 configured with a native debugger, and target remote isn't
2514 connected yet. */
2515 t = find_default_run_target (NULL);
2516 if (t && t->to_is_async_p)
b84876c2
PA
2517 return (t->to_is_async_p) ();
2518 return 0;
2519}
2520
2c0b251b 2521static int
9908b566
VP
2522find_default_supports_non_stop (void)
2523{
2524 struct target_ops *t;
2525
2526 t = find_default_run_target (NULL);
2527 if (t && t->to_supports_non_stop)
2528 return (t->to_supports_non_stop) ();
2529 return 0;
2530}
2531
2532int
2c0b251b 2533target_supports_non_stop (void)
9908b566
VP
2534{
2535 struct target_ops *t;
2536 for (t = &current_target; t != NULL; t = t->beneath)
2537 if (t->to_supports_non_stop)
2538 return t->to_supports_non_stop ();
2539
2540 return 0;
2541}
2542
2543
07e059b5
VP
2544char *
2545target_get_osdata (const char *type)
2546{
2547 char *document;
2548 struct target_ops *t;
2549
739ef7fb
PA
2550 /* If we're already connected to something that can get us OS
2551 related data, use it. Otherwise, try using the native
2552 target. */
2553 if (current_target.to_stratum >= process_stratum)
6d097e65 2554 t = current_target.beneath;
739ef7fb
PA
2555 else
2556 t = find_default_run_target ("get OS data");
07e059b5
VP
2557
2558 if (!t)
2559 return NULL;
2560
6d097e65 2561 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
2562}
2563
6c95b8df
PA
2564/* Determine the current address space of thread PTID. */
2565
2566struct address_space *
2567target_thread_address_space (ptid_t ptid)
2568{
c0694254 2569 struct address_space *aspace;
6c95b8df 2570 struct inferior *inf;
c0694254
PA
2571 struct target_ops *t;
2572
2573 for (t = current_target.beneath; t != NULL; t = t->beneath)
2574 {
2575 if (t->to_thread_address_space != NULL)
2576 {
2577 aspace = t->to_thread_address_space (t, ptid);
2578 gdb_assert (aspace);
6c95b8df 2579
c0694254
PA
2580 if (targetdebug)
2581 fprintf_unfiltered (gdb_stdlog,
2582 "target_thread_address_space (%s) = %d\n",
2583 target_pid_to_str (ptid),
2584 address_space_num (aspace));
2585 return aspace;
2586 }
2587 }
6c95b8df
PA
2588
2589 /* Fall-back to the "main" address space of the inferior. */
2590 inf = find_inferior_pid (ptid_get_pid (ptid));
2591
2592 if (inf == NULL || inf->aspace == NULL)
2593 internal_error (__FILE__, __LINE__, "\
2594Can't determine the current address space of thread %s\n",
2595 target_pid_to_str (ptid));
2596
2597 return inf->aspace;
2598}
2599
e0d24f8d
WZ
2600static int
2601default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2602{
ffe5a37e 2603 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
ccaa32c7
GS
2604}
2605
5009afc5
AS
2606static int
2607default_watchpoint_addr_within_range (struct target_ops *target,
2608 CORE_ADDR addr,
2609 CORE_ADDR start, int length)
2610{
2611 return addr >= start && addr < start + length;
2612}
2613
c2250ad1
UW
2614static struct gdbarch *
2615default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2616{
2617 return target_gdbarch;
2618}
2619
c906108c 2620static int
fba45db2 2621return_zero (void)
c906108c
SS
2622{
2623 return 0;
2624}
2625
2626static int
fba45db2 2627return_one (void)
c906108c
SS
2628{
2629 return 1;
2630}
2631
ccaa32c7
GS
2632static int
2633return_minus_one (void)
2634{
2635 return -1;
2636}
2637
7a292a7a
SS
2638/* Find a single runnable target in the stack and return it. If for
2639 some reason there is more than one, return NULL. */
2640
2641struct target_ops *
fba45db2 2642find_run_target (void)
7a292a7a
SS
2643{
2644 struct target_ops **t;
2645 struct target_ops *runable = NULL;
2646 int count;
c5aa993b 2647
7a292a7a 2648 count = 0;
c5aa993b 2649
7a292a7a
SS
2650 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2651 {
c5aa993b 2652 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
2653 {
2654 runable = *t;
2655 ++count;
2656 }
2657 }
c5aa993b 2658
7a292a7a
SS
2659 return (count == 1 ? runable : NULL);
2660}
2661
ed9a39eb
JM
2662/* Find a single core_stratum target in the list of targets and return it.
2663 If for some reason there is more than one, return NULL. */
2664
c906108c 2665struct target_ops *
fba45db2 2666find_core_target (void)
c906108c
SS
2667{
2668 struct target_ops **t;
2669 struct target_ops *runable = NULL;
2670 int count;
c5aa993b 2671
c906108c 2672 count = 0;
c5aa993b 2673
c906108c
SS
2674 for (t = target_structs; t < target_structs + target_struct_size;
2675 ++t)
2676 {
2677 if ((*t)->to_stratum == core_stratum)
2678 {
2679 runable = *t;
2680 ++count;
2681 }
2682 }
c5aa993b
JM
2683
2684 return (count == 1 ? runable : NULL);
c906108c 2685}
ed9a39eb
JM
2686
2687/*
2688 * Find the next target down the stack from the specified target.
2689 */
2690
2691struct target_ops *
fba45db2 2692find_target_beneath (struct target_ops *t)
ed9a39eb 2693{
258b763a 2694 return t->beneath;
ed9a39eb
JM
2695}
2696
c906108c
SS
2697\f
2698/* The inferior process has died. Long live the inferior! */
2699
2700void
fba45db2 2701generic_mourn_inferior (void)
c906108c 2702{
7f9f62ba 2703 ptid_t ptid;
c906108c 2704
7f9f62ba 2705 ptid = inferior_ptid;
39f77062 2706 inferior_ptid = null_ptid;
7f9f62ba
PA
2707
2708 if (!ptid_equal (ptid, null_ptid))
2709 {
2710 int pid = ptid_get_pid (ptid);
6c95b8df 2711 exit_inferior (pid);
7f9f62ba
PA
2712 }
2713
c906108c
SS
2714 breakpoint_init_inferior (inf_exited);
2715 registers_changed ();
2716
c906108c
SS
2717 reopen_exec_file ();
2718 reinit_frame_cache ();
2719
9a4105ab
AC
2720 if (deprecated_detach_hook)
2721 deprecated_detach_hook ();
c906108c
SS
2722}
2723\f
8807d78b 2724/* Helper function for child_wait and the derivatives of child_wait.
c906108c
SS
2725 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2726 translation of that in OURSTATUS. */
2727void
fba45db2 2728store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c 2729{
c906108c
SS
2730 if (WIFEXITED (hoststatus))
2731 {
2732 ourstatus->kind = TARGET_WAITKIND_EXITED;
2733 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2734 }
2735 else if (!WIFSTOPPED (hoststatus))
2736 {
2737 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2738 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2739 }
2740 else
2741 {
2742 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2743 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2744 }
2745}
2746\f
fd0a2a6f
MK
2747/* Convert a normal process ID to a string. Returns the string in a
2748 static buffer. */
c906108c
SS
2749
2750char *
39f77062 2751normal_pid_to_str (ptid_t ptid)
c906108c 2752{
fd0a2a6f 2753 static char buf[32];
c906108c 2754
5fff8fc0 2755 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
2756 return buf;
2757}
2758
2c0b251b 2759static char *
117de6a9
PA
2760dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2761{
2762 return normal_pid_to_str (ptid);
2763}
2764
9b4eba8e
HZ
2765/* Error-catcher for target_find_memory_regions. */
2766static int
2767dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
be4d1333 2768{
9b4eba8e 2769 error (_("Command not implemented for this target."));
be4d1333
MS
2770 return 0;
2771}
2772
9b4eba8e
HZ
2773/* Error-catcher for target_make_corefile_notes. */
2774static char *
2775dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
be4d1333 2776{
9b4eba8e 2777 error (_("Command not implemented for this target."));
be4d1333
MS
2778 return NULL;
2779}
2780
6b04bdb7
MS
2781/* Error-catcher for target_get_bookmark. */
2782static gdb_byte *
2783dummy_get_bookmark (char *ignore1, int ignore2)
2784{
2785 tcomplain ();
2786 return NULL;
2787}
2788
2789/* Error-catcher for target_goto_bookmark. */
2790static void
2791dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
2792{
2793 tcomplain ();
2794}
2795
c906108c
SS
2796/* Set up the handful of non-empty slots needed by the dummy target
2797 vector. */
2798
2799static void
fba45db2 2800init_dummy_target (void)
c906108c
SS
2801{
2802 dummy_target.to_shortname = "None";
2803 dummy_target.to_longname = "None";
2804 dummy_target.to_doc = "";
2805 dummy_target.to_attach = find_default_attach;
136d6dae
VP
2806 dummy_target.to_detach =
2807 (void (*)(struct target_ops *, char *, int))target_ignore;
c906108c 2808 dummy_target.to_create_inferior = find_default_create_inferior;
b84876c2
PA
2809 dummy_target.to_can_async_p = find_default_can_async_p;
2810 dummy_target.to_is_async_p = find_default_is_async_p;
9908b566 2811 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
117de6a9 2812 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 2813 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
2814 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2815 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
6b04bdb7
MS
2816 dummy_target.to_get_bookmark = dummy_get_bookmark;
2817 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
0b603eba 2818 dummy_target.to_xfer_partial = default_xfer_partial;
c35b1492
PA
2819 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2820 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2821 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2822 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2823 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
c906108c
SS
2824 dummy_target.to_magic = OPS_MAGIC;
2825}
c906108c 2826\f
c906108c 2827static void
fba45db2 2828debug_to_open (char *args, int from_tty)
c906108c
SS
2829{
2830 debug_target.to_open (args, from_tty);
2831
96baa820 2832 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2833}
2834
f1c07ab0
AC
2835void
2836target_close (struct target_ops *targ, int quitting)
2837{
2838 if (targ->to_xclose != NULL)
2839 targ->to_xclose (targ, quitting);
2840 else if (targ->to_close != NULL)
2841 targ->to_close (quitting);
947b8855
PA
2842
2843 if (targetdebug)
2844 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
f1c07ab0
AC
2845}
2846
136d6dae
VP
2847void
2848target_attach (char *args, int from_tty)
2849{
2850 struct target_ops *t;
2851 for (t = current_target.beneath; t != NULL; t = t->beneath)
2852 {
2853 if (t->to_attach != NULL)
2854 {
2855 t->to_attach (t, args, from_tty);
947b8855
PA
2856 if (targetdebug)
2857 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2858 args, from_tty);
136d6dae
VP
2859 return;
2860 }
2861 }
2862
2863 internal_error (__FILE__, __LINE__,
2864 "could not find a target to attach");
2865}
2866
28439f5e
PA
2867int
2868target_thread_alive (ptid_t ptid)
c906108c 2869{
28439f5e
PA
2870 struct target_ops *t;
2871 for (t = current_target.beneath; t != NULL; t = t->beneath)
2872 {
2873 if (t->to_thread_alive != NULL)
2874 {
2875 int retval;
c906108c 2876
28439f5e
PA
2877 retval = t->to_thread_alive (t, ptid);
2878 if (targetdebug)
2879 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2880 PIDGET (ptid), retval);
2881
2882 return retval;
2883 }
2884 }
2885
2886 return 0;
2887}
2888
2889void
2890target_find_new_threads (void)
2891{
2892 struct target_ops *t;
2893 for (t = current_target.beneath; t != NULL; t = t->beneath)
2894 {
2895 if (t->to_find_new_threads != NULL)
2896 {
2897 t->to_find_new_threads (t);
2898 if (targetdebug)
2899 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2900
2901 return;
2902 }
2903 }
c906108c
SS
2904}
2905
c906108c 2906static void
28439f5e 2907debug_to_post_attach (int pid)
c906108c 2908{
28439f5e 2909 debug_target.to_post_attach (pid);
c906108c 2910
28439f5e 2911 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2912}
2913
f00150c9
DE
2914/* Return a pretty printed form of target_waitstatus.
2915 Space for the result is malloc'd, caller must free. */
c906108c 2916
f00150c9
DE
2917char *
2918target_waitstatus_to_string (const struct target_waitstatus *ws)
2919{
2920 const char *kind_str = "status->kind = ";
c906108c 2921
f00150c9 2922 switch (ws->kind)
c906108c
SS
2923 {
2924 case TARGET_WAITKIND_EXITED:
f00150c9
DE
2925 return xstrprintf ("%sexited, status = %d",
2926 kind_str, ws->value.integer);
c906108c 2927 case TARGET_WAITKIND_STOPPED:
f00150c9
DE
2928 return xstrprintf ("%sstopped, signal = %s",
2929 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2930 case TARGET_WAITKIND_SIGNALLED:
f00150c9
DE
2931 return xstrprintf ("%ssignalled, signal = %s",
2932 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2933 case TARGET_WAITKIND_LOADED:
f00150c9 2934 return xstrprintf ("%sloaded", kind_str);
c906108c 2935 case TARGET_WAITKIND_FORKED:
f00150c9 2936 return xstrprintf ("%sforked", kind_str);
c906108c 2937 case TARGET_WAITKIND_VFORKED:
f00150c9 2938 return xstrprintf ("%svforked", kind_str);
c906108c 2939 case TARGET_WAITKIND_EXECD:
f00150c9
DE
2940 return xstrprintf ("%sexecd", kind_str);
2941 case TARGET_WAITKIND_SYSCALL_ENTRY:
a96d9b2e 2942 return xstrprintf ("%sentered syscall", kind_str);
f00150c9 2943 case TARGET_WAITKIND_SYSCALL_RETURN:
a96d9b2e 2944 return xstrprintf ("%sexited syscall", kind_str);
c906108c 2945 case TARGET_WAITKIND_SPURIOUS:
f00150c9
DE
2946 return xstrprintf ("%sspurious", kind_str);
2947 case TARGET_WAITKIND_IGNORE:
2948 return xstrprintf ("%signore", kind_str);
2949 case TARGET_WAITKIND_NO_HISTORY:
2950 return xstrprintf ("%sno-history", kind_str);
c906108c 2951 default:
f00150c9 2952 return xstrprintf ("%sunknown???", kind_str);
c906108c 2953 }
f00150c9
DE
2954}
2955
bf0c5130 2956static void
56be3814
UW
2957debug_print_register (const char * func,
2958 struct regcache *regcache, int regno)
bf0c5130 2959{
f8d29908 2960 struct gdbarch *gdbarch = get_regcache_arch (regcache);
bf0c5130 2961 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 2962 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
2963 && gdbarch_register_name (gdbarch, regno) != NULL
2964 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2965 fprintf_unfiltered (gdb_stdlog, "(%s)",
2966 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
2967 else
2968 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 2969 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 2970 {
e17a4113 2971 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 2972 int i, size = register_size (gdbarch, regno);
d9d9c31f 2973 unsigned char buf[MAX_REGISTER_SIZE];
0ff58721 2974 regcache_raw_collect (regcache, regno, buf);
bf0c5130 2975 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 2976 for (i = 0; i < size; i++)
bf0c5130
AC
2977 {
2978 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2979 }
81c4a259 2980 if (size <= sizeof (LONGEST))
bf0c5130 2981 {
e17a4113 2982 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
0b1553bc
UW
2983 fprintf_unfiltered (gdb_stdlog, " %s %s",
2984 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
2985 }
2986 }
2987 fprintf_unfiltered (gdb_stdlog, "\n");
2988}
2989
28439f5e
PA
2990void
2991target_fetch_registers (struct regcache *regcache, int regno)
c906108c 2992{
28439f5e
PA
2993 struct target_ops *t;
2994 for (t = current_target.beneath; t != NULL; t = t->beneath)
2995 {
2996 if (t->to_fetch_registers != NULL)
2997 {
2998 t->to_fetch_registers (t, regcache, regno);
2999 if (targetdebug)
3000 debug_print_register ("target_fetch_registers", regcache, regno);
3001 return;
3002 }
3003 }
c906108c
SS
3004}
3005
28439f5e
PA
3006void
3007target_store_registers (struct regcache *regcache, int regno)
c906108c 3008{
28439f5e
PA
3009
3010 struct target_ops *t;
3011 for (t = current_target.beneath; t != NULL; t = t->beneath)
3012 {
3013 if (t->to_store_registers != NULL)
3014 {
3015 t->to_store_registers (t, regcache, regno);
3016 if (targetdebug)
3017 {
3018 debug_print_register ("target_store_registers", regcache, regno);
3019 }
3020 return;
3021 }
3022 }
3023
3024 noprocess ();
c906108c
SS
3025}
3026
3027static void
316f2060 3028debug_to_prepare_to_store (struct regcache *regcache)
c906108c 3029{
316f2060 3030 debug_target.to_prepare_to_store (regcache);
c906108c 3031
96baa820 3032 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
3033}
3034
3035static int
961cb7b5 3036deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
3037 int write, struct mem_attrib *attrib,
3038 struct target_ops *target)
c906108c
SS
3039{
3040 int retval;
3041
c8e73a31
AC
3042 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3043 attrib, target);
c906108c 3044
96baa820 3045 fprintf_unfiltered (gdb_stdlog,
53b71562 3046 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
5af949e3
UW
3047 paddress (target_gdbarch, memaddr), len,
3048 write ? "write" : "read", retval);
c906108c 3049
c906108c
SS
3050 if (retval > 0)
3051 {
3052 int i;
3053
96baa820 3054 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
3055 for (i = 0; i < retval; i++)
3056 {
53b71562 3057 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
3058 {
3059 if (targetdebug < 2 && i > 0)
3060 {
3061 fprintf_unfiltered (gdb_stdlog, " ...");
3062 break;
3063 }
3064 fprintf_unfiltered (gdb_stdlog, "\n");
3065 }
2bc416ba 3066
96baa820 3067 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
3068 }
3069 }
3070
96baa820 3071 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
3072
3073 return retval;
3074}
3075
3076static void
fba45db2 3077debug_to_files_info (struct target_ops *target)
c906108c
SS
3078{
3079 debug_target.to_files_info (target);
3080
96baa820 3081 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
3082}
3083
3084static int
a6d9a66e
UW
3085debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3086 struct bp_target_info *bp_tgt)
c906108c
SS
3087{
3088 int retval;
3089
a6d9a66e 3090 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
c906108c 3091
96baa820 3092 fprintf_unfiltered (gdb_stdlog,
104c1213 3093 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3094 (unsigned long) bp_tgt->placed_address,
104c1213 3095 (unsigned long) retval);
c906108c
SS
3096 return retval;
3097}
3098
3099static int
a6d9a66e
UW
3100debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3101 struct bp_target_info *bp_tgt)
c906108c
SS
3102{
3103 int retval;
3104
a6d9a66e 3105 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
c906108c 3106
96baa820 3107 fprintf_unfiltered (gdb_stdlog,
104c1213 3108 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3109 (unsigned long) bp_tgt->placed_address,
104c1213 3110 (unsigned long) retval);
c906108c
SS
3111 return retval;
3112}
3113
ccaa32c7
GS
3114static int
3115debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3116{
3117 int retval;
3118
3119 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3120
3121 fprintf_unfiltered (gdb_stdlog,
3122 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3123 (unsigned long) type,
3124 (unsigned long) cnt,
3125 (unsigned long) from_tty,
3126 (unsigned long) retval);
3127 return retval;
3128}
3129
e0d24f8d
WZ
3130static int
3131debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3132{
3133 CORE_ADDR retval;
3134
3135 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3136
3137 fprintf_unfiltered (gdb_stdlog,
d92524f1 3138 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
e0d24f8d
WZ
3139 (unsigned long) addr,
3140 (unsigned long) len,
3141 (unsigned long) retval);
3142 return retval;
3143}
3144
ccaa32c7
GS
3145static int
3146debug_to_stopped_by_watchpoint (void)
3147{
3148 int retval;
3149
3150 retval = debug_target.to_stopped_by_watchpoint ();
3151
3152 fprintf_unfiltered (gdb_stdlog,
d92524f1 3153 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
3154 (unsigned long) retval);
3155 return retval;
3156}
3157
4aa7a7f5
JJ
3158static int
3159debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 3160{
4aa7a7f5 3161 int retval;
ccaa32c7 3162
4aa7a7f5 3163 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
3164
3165 fprintf_unfiltered (gdb_stdlog,
4aa7a7f5
JJ
3166 "target_stopped_data_address ([0x%lx]) = %ld\n",
3167 (unsigned long)*addr,
3168 (unsigned long)retval);
ccaa32c7
GS
3169 return retval;
3170}
3171
5009afc5
AS
3172static int
3173debug_to_watchpoint_addr_within_range (struct target_ops *target,
3174 CORE_ADDR addr,
3175 CORE_ADDR start, int length)
3176{
3177 int retval;
3178
3179 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3180 start, length);
3181
3182 fprintf_filtered (gdb_stdlog,
3183 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3184 (unsigned long) addr, (unsigned long) start, length,
3185 retval);
3186 return retval;
3187}
3188
ccaa32c7 3189static int
a6d9a66e
UW
3190debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3191 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3192{
3193 int retval;
3194
a6d9a66e 3195 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3196
3197 fprintf_unfiltered (gdb_stdlog,
3198 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3199 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3200 (unsigned long) retval);
3201 return retval;
3202}
3203
3204static int
a6d9a66e
UW
3205debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3206 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3207{
3208 int retval;
3209
a6d9a66e 3210 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3211
3212 fprintf_unfiltered (gdb_stdlog,
3213 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3214 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3215 (unsigned long) retval);
3216 return retval;
3217}
3218
3219static int
3220debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3221{
3222 int retval;
3223
3224 retval = debug_target.to_insert_watchpoint (addr, len, type);
3225
3226 fprintf_unfiltered (gdb_stdlog,
3227 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3228 (unsigned long) addr, len, type, (unsigned long) retval);
3229 return retval;
3230}
3231
3232static int
3233debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3234{
3235 int retval;
3236
ecde4882 3237 retval = debug_target.to_remove_watchpoint (addr, len, type);
ccaa32c7
GS
3238
3239 fprintf_unfiltered (gdb_stdlog,
ecde4882 3240 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
ccaa32c7
GS
3241 (unsigned long) addr, len, type, (unsigned long) retval);
3242 return retval;
3243}
3244
c906108c 3245static void
fba45db2 3246debug_to_terminal_init (void)
c906108c
SS
3247{
3248 debug_target.to_terminal_init ();
3249
96baa820 3250 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
3251}
3252
3253static void
fba45db2 3254debug_to_terminal_inferior (void)
c906108c
SS
3255{
3256 debug_target.to_terminal_inferior ();
3257
96baa820 3258 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
3259}
3260
3261static void
fba45db2 3262debug_to_terminal_ours_for_output (void)
c906108c
SS
3263{
3264 debug_target.to_terminal_ours_for_output ();
3265
96baa820 3266 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
3267}
3268
3269static void
fba45db2 3270debug_to_terminal_ours (void)
c906108c
SS
3271{
3272 debug_target.to_terminal_ours ();
3273
96baa820 3274 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
3275}
3276
a790ad35
SC
3277static void
3278debug_to_terminal_save_ours (void)
3279{
3280 debug_target.to_terminal_save_ours ();
3281
3282 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3283}
3284
c906108c 3285static void
fba45db2 3286debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
3287{
3288 debug_target.to_terminal_info (arg, from_tty);
3289
96baa820 3290 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
3291 from_tty);
3292}
3293
c906108c 3294static void
fba45db2 3295debug_to_load (char *args, int from_tty)
c906108c
SS
3296{
3297 debug_target.to_load (args, from_tty);
3298
96baa820 3299 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
3300}
3301
3302static int
fba45db2 3303debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
3304{
3305 int retval;
3306
3307 retval = debug_target.to_lookup_symbol (name, addrp);
3308
96baa820 3309 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
3310
3311 return retval;
3312}
3313
c906108c 3314static void
39f77062 3315debug_to_post_startup_inferior (ptid_t ptid)
c906108c 3316{
39f77062 3317 debug_target.to_post_startup_inferior (ptid);
c906108c 3318
96baa820 3319 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 3320 PIDGET (ptid));
c906108c
SS
3321}
3322
3323static void
fba45db2 3324debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
3325{
3326 debug_target.to_acknowledge_created_inferior (pid);
3327
96baa820 3328 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
3329 pid);
3330}
3331
fa113d1a 3332static void
fba45db2 3333debug_to_insert_fork_catchpoint (int pid)
c906108c 3334{
fa113d1a 3335 debug_target.to_insert_fork_catchpoint (pid);
c906108c 3336
fa113d1a
AC
3337 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3338 pid);
c906108c
SS
3339}
3340
3341static int
fba45db2 3342debug_to_remove_fork_catchpoint (int pid)
c906108c 3343{
c5aa993b 3344 int retval;
c906108c
SS
3345
3346 retval = debug_target.to_remove_fork_catchpoint (pid);
3347
96baa820 3348 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 3349 pid, retval);
c906108c
SS
3350
3351 return retval;
3352}
3353
fa113d1a 3354static void
fba45db2 3355debug_to_insert_vfork_catchpoint (int pid)
c906108c 3356{
fa113d1a 3357 debug_target.to_insert_vfork_catchpoint (pid);
c906108c 3358
fa113d1a
AC
3359 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3360 pid);
c906108c
SS
3361}
3362
3363static int
fba45db2 3364debug_to_remove_vfork_catchpoint (int pid)
c906108c 3365{
c5aa993b 3366 int retval;
c906108c
SS
3367
3368 retval = debug_target.to_remove_vfork_catchpoint (pid);
3369
96baa820 3370 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 3371 pid, retval);
c906108c
SS
3372
3373 return retval;
3374}
3375
fa113d1a 3376static void
fba45db2 3377debug_to_insert_exec_catchpoint (int pid)
c906108c 3378{
fa113d1a 3379 debug_target.to_insert_exec_catchpoint (pid);
c906108c 3380
fa113d1a
AC
3381 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3382 pid);
c906108c
SS
3383}
3384
3385static int
fba45db2 3386debug_to_remove_exec_catchpoint (int pid)
c906108c 3387{
c5aa993b 3388 int retval;
c906108c
SS
3389
3390 retval = debug_target.to_remove_exec_catchpoint (pid);
3391
96baa820 3392 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 3393 pid, retval);
c906108c
SS
3394
3395 return retval;
3396}
3397
c906108c 3398static int
fba45db2 3399debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 3400{
c5aa993b 3401 int has_exited;
c906108c
SS
3402
3403 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3404
96baa820 3405 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 3406 pid, wait_status, *exit_status, has_exited);
c906108c
SS
3407
3408 return has_exited;
3409}
3410
c906108c 3411static int
fba45db2 3412debug_to_can_run (void)
c906108c
SS
3413{
3414 int retval;
3415
3416 retval = debug_target.to_can_run ();
3417
96baa820 3418 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
3419
3420 return retval;
3421}
3422
3423static void
39f77062 3424debug_to_notice_signals (ptid_t ptid)
c906108c 3425{
39f77062 3426 debug_target.to_notice_signals (ptid);
c906108c 3427
39f77062
KB
3428 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3429 PIDGET (ptid));
c906108c
SS
3430}
3431
c2250ad1
UW
3432static struct gdbarch *
3433debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3434{
3435 struct gdbarch *retval;
3436
3437 retval = debug_target.to_thread_architecture (ops, ptid);
3438
67e5d36c
PM
3439 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %s [%s]\n",
3440 target_pid_to_str (ptid), host_address_to_string (retval),
c2250ad1
UW
3441 gdbarch_bfd_arch_info (retval)->printable_name);
3442 return retval;
3443}
3444
c906108c 3445static void
94cc34af 3446debug_to_stop (ptid_t ptid)
c906108c 3447{
94cc34af 3448 debug_target.to_stop (ptid);
c906108c 3449
94cc34af
PA
3450 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3451 target_pid_to_str (ptid));
c906108c
SS
3452}
3453
96baa820
JM
3454static void
3455debug_to_rcmd (char *command,
d9fcf2fb 3456 struct ui_file *outbuf)
96baa820
JM
3457{
3458 debug_target.to_rcmd (command, outbuf);
3459 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3460}
3461
c906108c 3462static char *
fba45db2 3463debug_to_pid_to_exec_file (int pid)
c906108c 3464{
c5aa993b 3465 char *exec_file;
c906108c
SS
3466
3467 exec_file = debug_target.to_pid_to_exec_file (pid);
3468
96baa820 3469 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 3470 pid, exec_file);
c906108c
SS
3471
3472 return exec_file;
3473}
3474
c906108c 3475static void
fba45db2 3476setup_target_debug (void)
c906108c
SS
3477{
3478 memcpy (&debug_target, &current_target, sizeof debug_target);
3479
3480 current_target.to_open = debug_to_open;
c906108c 3481 current_target.to_post_attach = debug_to_post_attach;
c906108c 3482 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 3483 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
3484 current_target.to_files_info = debug_to_files_info;
3485 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3486 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
3487 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3488 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3489 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3490 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3491 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3492 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3493 current_target.to_stopped_data_address = debug_to_stopped_data_address;
5009afc5 3494 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
e0d24f8d 3495 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
c906108c
SS
3496 current_target.to_terminal_init = debug_to_terminal_init;
3497 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3498 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3499 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 3500 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 3501 current_target.to_terminal_info = debug_to_terminal_info;
c906108c
SS
3502 current_target.to_load = debug_to_load;
3503 current_target.to_lookup_symbol = debug_to_lookup_symbol;
c906108c
SS
3504 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3505 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
c906108c
SS
3506 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3507 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3508 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3509 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
3510 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3511 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 3512 current_target.to_has_exited = debug_to_has_exited;
c906108c
SS
3513 current_target.to_can_run = debug_to_can_run;
3514 current_target.to_notice_signals = debug_to_notice_signals;
c906108c 3515 current_target.to_stop = debug_to_stop;
96baa820 3516 current_target.to_rcmd = debug_to_rcmd;
c906108c 3517 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 3518 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 3519}
c906108c 3520\f
c5aa993b
JM
3521
3522static char targ_desc[] =
3523"Names of targets and files being debugged.\n\
c906108c
SS
3524Shows the entire stack of targets currently in use (including the exec-file,\n\
3525core-file, and process, if any), as well as the symbol file name.";
3526
96baa820
JM
3527static void
3528do_monitor_command (char *cmd,
3529 int from_tty)
3530{
2b5fe715
AC
3531 if ((current_target.to_rcmd
3532 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 3533 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
3534 && (debug_target.to_rcmd
3535 == (void (*) (char *, struct ui_file *)) tcomplain)))
8a3fe4f8 3536 error (_("\"monitor\" command not supported by this target."));
96baa820
JM
3537 target_rcmd (cmd, gdb_stdtarg);
3538}
3539
87680a14
JB
3540/* Print the name of each layers of our target stack. */
3541
3542static void
3543maintenance_print_target_stack (char *cmd, int from_tty)
3544{
3545 struct target_ops *t;
3546
3547 printf_filtered (_("The current target stack is:\n"));
3548
3549 for (t = target_stack; t != NULL; t = t->beneath)
3550 {
3551 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3552 }
3553}
3554
c6ebd6cf
VP
3555/* Controls if async mode is permitted. */
3556int target_async_permitted = 0;
3557
3558/* The set command writes to this variable. If the inferior is
3559 executing, linux_nat_async_permitted is *not* updated. */
3560static int target_async_permitted_1 = 0;
3561
3562static void
3563set_maintenance_target_async_permitted (char *args, int from_tty,
3564 struct cmd_list_element *c)
3565{
c35b1492 3566 if (have_live_inferiors ())
c6ebd6cf
VP
3567 {
3568 target_async_permitted_1 = target_async_permitted;
3569 error (_("Cannot change this setting while the inferior is running."));
3570 }
3571
3572 target_async_permitted = target_async_permitted_1;
3573}
3574
3575static void
3576show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3577 struct cmd_list_element *c,
3578 const char *value)
3579{
3580 fprintf_filtered (file, _("\
3581Controlling the inferior in asynchronous mode is %s.\n"), value);
3582}
3583
c906108c 3584void
fba45db2 3585initialize_targets (void)
c906108c
SS
3586{
3587 init_dummy_target ();
3588 push_target (&dummy_target);
3589
3590 add_info ("target", target_info, targ_desc);
3591 add_info ("files", target_info, targ_desc);
3592
85c07804
AC
3593 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3594Set target debugging."), _("\
3595Show target debugging."), _("\
333dabeb
DJ
3596When non-zero, target debugging is enabled. Higher numbers are more\n\
3597verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804
AC
3598command."),
3599 NULL,
920d2a44 3600 show_targetdebug,
85c07804 3601 &setdebuglist, &showdebuglist);
3a11626d 3602
2bc416ba 3603 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
3604 &trust_readonly, _("\
3605Set mode for reading from readonly sections."), _("\
3606Show mode for reading from readonly sections."), _("\
3a11626d
MS
3607When this mode is on, memory reads from readonly sections (such as .text)\n\
3608will be read from the object file instead of from the target. This will\n\
7915a72c 3609result in significant performance improvement for remote targets."),
2c5b56ce 3610 NULL,
920d2a44 3611 show_trust_readonly,
e707bbc2 3612 &setlist, &showlist);
96baa820
JM
3613
3614 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 3615 _("Send a command to the remote monitor (remote targets only)."));
96baa820 3616
87680a14
JB
3617 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3618 _("Print the name of each layer of the internal target stack."),
3619 &maintenanceprintlist);
3620
c6ebd6cf
VP
3621 add_setshow_boolean_cmd ("target-async", no_class,
3622 &target_async_permitted_1, _("\
3623Set whether gdb controls the inferior in asynchronous mode."), _("\
3624Show whether gdb controls the inferior in asynchronous mode."), _("\
3625Tells gdb whether to control the inferior in asynchronous mode."),
3626 set_maintenance_target_async_permitted,
3627 show_maintenance_target_async_permitted,
3628 &setlist,
3629 &showlist);
3630
4e5d721f 3631 add_setshow_boolean_cmd ("stack-cache", class_support,
9cf1b572 3632 &stack_cache_enabled_p_1, _("\
4e5d721f
DE
3633Set cache use for stack access."), _("\
3634Show cache use for stack access."), _("\
3635When on, use the data cache for all stack access, regardless of any\n\
3636configured memory regions. This improves remote performance significantly.\n\
3637By default, caching for stack access is on."),
3638 set_stack_cache_enabled_p,
3639 show_stack_cache_enabled_p,
3640 &setlist, &showlist);
3641
8add0441 3642 target_dcache = dcache_init ();
c906108c 3643}
This page took 1.339097 seconds and 4 git commands to generate.