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