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