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