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