gdb/
[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
739ef7fb
PA
2221 /* If we're already connected to something that can get us OS
2222 related data, use it. Otherwise, try using the native
2223 target. */
2224 if (current_target.to_stratum >= process_stratum)
6d097e65 2225 t = current_target.beneath;
739ef7fb
PA
2226 else
2227 t = find_default_run_target ("get OS data");
07e059b5
VP
2228
2229 if (!t)
2230 return NULL;
2231
6d097e65 2232 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
2233}
2234
e0d24f8d
WZ
2235static int
2236default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2237{
ffe5a37e 2238 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
ccaa32c7
GS
2239}
2240
5009afc5
AS
2241static int
2242default_watchpoint_addr_within_range (struct target_ops *target,
2243 CORE_ADDR addr,
2244 CORE_ADDR start, int length)
2245{
2246 return addr >= start && addr < start + length;
2247}
2248
c906108c 2249static int
fba45db2 2250return_zero (void)
c906108c
SS
2251{
2252 return 0;
2253}
2254
2255static int
fba45db2 2256return_one (void)
c906108c
SS
2257{
2258 return 1;
2259}
2260
ccaa32c7
GS
2261static int
2262return_minus_one (void)
2263{
2264 return -1;
2265}
2266
6426a772
JM
2267/*
2268 * Resize the to_sections pointer. Also make sure that anyone that
2269 * was holding on to an old value of it gets updated.
2270 * Returns the old size.
2271 */
2272
2273int
2274target_resize_to_sections (struct target_ops *target, int num_added)
2275{
2276 struct target_ops **t;
2277 struct section_table *old_value;
2278 int old_count;
2279
2280 old_value = target->to_sections;
2281
2282 if (target->to_sections)
2283 {
2284 old_count = target->to_sections_end - target->to_sections;
2285 target->to_sections = (struct section_table *)
2286 xrealloc ((char *) target->to_sections,
2287 (sizeof (struct section_table)) * (num_added + old_count));
2288 }
2289 else
2290 {
2291 old_count = 0;
2292 target->to_sections = (struct section_table *)
2293 xmalloc ((sizeof (struct section_table)) * num_added);
2294 }
2295 target->to_sections_end = target->to_sections + (num_added + old_count);
2296
2297 /* Check to see if anyone else was pointing to this structure.
2298 If old_value was null, then no one was. */
2bc416ba 2299
6426a772
JM
2300 if (old_value)
2301 {
2302 for (t = target_structs; t < target_structs + target_struct_size;
2303 ++t)
2304 {
2305 if ((*t)->to_sections == old_value)
2306 {
2307 (*t)->to_sections = target->to_sections;
2308 (*t)->to_sections_end = target->to_sections_end;
2309 }
2310 }
e354df01
NW
2311 /* There is a flattened view of the target stack in current_target,
2312 so its to_sections pointer might also need updating. */
2313 if (current_target.to_sections == old_value)
2314 {
2315 current_target.to_sections = target->to_sections;
2316 current_target.to_sections_end = target->to_sections_end;
2317 }
6426a772 2318 }
2bc416ba 2319
6426a772
JM
2320 return old_count;
2321
2322}
2323
07cd4b97
JB
2324/* Remove all target sections taken from ABFD.
2325
2326 Scan the current target stack for targets whose section tables
2327 refer to sections from BFD, and remove those sections. We use this
2328 when we notice that the inferior has unloaded a shared object, for
2329 example. */
2330void
2331remove_target_sections (bfd *abfd)
2332{
2333 struct target_ops **t;
2334
2335 for (t = target_structs; t < target_structs + target_struct_size; t++)
2336 {
2337 struct section_table *src, *dest;
2338
2339 dest = (*t)->to_sections;
2340 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
2341 if (src->bfd != abfd)
2342 {
2343 /* Keep this section. */
2344 if (dest < src) *dest = *src;
2345 dest++;
2346 }
2347
2348 /* If we've dropped any sections, resize the section table. */
2349 if (dest < src)
2350 target_resize_to_sections (*t, dest - src);
2351 }
2352}
2353
2354
2355
2356
7a292a7a
SS
2357/* Find a single runnable target in the stack and return it. If for
2358 some reason there is more than one, return NULL. */
2359
2360struct target_ops *
fba45db2 2361find_run_target (void)
7a292a7a
SS
2362{
2363 struct target_ops **t;
2364 struct target_ops *runable = NULL;
2365 int count;
c5aa993b 2366
7a292a7a 2367 count = 0;
c5aa993b 2368
7a292a7a
SS
2369 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2370 {
c5aa993b 2371 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
2372 {
2373 runable = *t;
2374 ++count;
2375 }
2376 }
c5aa993b 2377
7a292a7a
SS
2378 return (count == 1 ? runable : NULL);
2379}
2380
ed9a39eb
JM
2381/* Find a single core_stratum target in the list of targets and return it.
2382 If for some reason there is more than one, return NULL. */
2383
c906108c 2384struct target_ops *
fba45db2 2385find_core_target (void)
c906108c
SS
2386{
2387 struct target_ops **t;
2388 struct target_ops *runable = NULL;
2389 int count;
c5aa993b 2390
c906108c 2391 count = 0;
c5aa993b 2392
c906108c
SS
2393 for (t = target_structs; t < target_structs + target_struct_size;
2394 ++t)
2395 {
2396 if ((*t)->to_stratum == core_stratum)
2397 {
2398 runable = *t;
2399 ++count;
2400 }
2401 }
c5aa993b
JM
2402
2403 return (count == 1 ? runable : NULL);
c906108c 2404}
ed9a39eb
JM
2405
2406/*
2407 * Find the next target down the stack from the specified target.
2408 */
2409
2410struct target_ops *
fba45db2 2411find_target_beneath (struct target_ops *t)
ed9a39eb 2412{
258b763a 2413 return t->beneath;
ed9a39eb
JM
2414}
2415
c906108c
SS
2416\f
2417/* The inferior process has died. Long live the inferior! */
2418
2419void
fba45db2 2420generic_mourn_inferior (void)
c906108c 2421{
7f9f62ba 2422 ptid_t ptid;
c906108c 2423
7f9f62ba 2424 ptid = inferior_ptid;
39f77062 2425 inferior_ptid = null_ptid;
7f9f62ba
PA
2426
2427 if (!ptid_equal (ptid, null_ptid))
2428 {
2429 int pid = ptid_get_pid (ptid);
2430 delete_inferior (pid);
2431 }
2432
c906108c
SS
2433 breakpoint_init_inferior (inf_exited);
2434 registers_changed ();
2435
c906108c
SS
2436 reopen_exec_file ();
2437 reinit_frame_cache ();
2438
9a4105ab
AC
2439 if (deprecated_detach_hook)
2440 deprecated_detach_hook ();
c906108c
SS
2441}
2442\f
8807d78b 2443/* Helper function for child_wait and the derivatives of child_wait.
c906108c
SS
2444 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2445 translation of that in OURSTATUS. */
2446void
fba45db2 2447store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c 2448{
c906108c
SS
2449 if (WIFEXITED (hoststatus))
2450 {
2451 ourstatus->kind = TARGET_WAITKIND_EXITED;
2452 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2453 }
2454 else if (!WIFSTOPPED (hoststatus))
2455 {
2456 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2457 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2458 }
2459 else
2460 {
2461 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2462 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2463 }
2464}
2465\f
fd0a2a6f
MK
2466/* Convert a normal process ID to a string. Returns the string in a
2467 static buffer. */
c906108c
SS
2468
2469char *
39f77062 2470normal_pid_to_str (ptid_t ptid)
c906108c 2471{
fd0a2a6f 2472 static char buf[32];
c906108c 2473
5fff8fc0 2474 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
2475 return buf;
2476}
2477
be4d1333 2478/* Error-catcher for target_find_memory_regions */
be4d1333
MS
2479static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2480{
8a3fe4f8 2481 error (_("No target."));
be4d1333
MS
2482 return 0;
2483}
2484
2485/* Error-catcher for target_make_corefile_notes */
be4d1333
MS
2486static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2487{
8a3fe4f8 2488 error (_("No target."));
be4d1333
MS
2489 return NULL;
2490}
2491
c906108c
SS
2492/* Set up the handful of non-empty slots needed by the dummy target
2493 vector. */
2494
2495static void
fba45db2 2496init_dummy_target (void)
c906108c
SS
2497{
2498 dummy_target.to_shortname = "None";
2499 dummy_target.to_longname = "None";
2500 dummy_target.to_doc = "";
2501 dummy_target.to_attach = find_default_attach;
136d6dae
VP
2502 dummy_target.to_detach =
2503 (void (*)(struct target_ops *, char *, int))target_ignore;
c906108c 2504 dummy_target.to_create_inferior = find_default_create_inferior;
b84876c2
PA
2505 dummy_target.to_can_async_p = find_default_can_async_p;
2506 dummy_target.to_is_async_p = find_default_is_async_p;
9908b566 2507 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
ed9a39eb 2508 dummy_target.to_pid_to_str = normal_pid_to_str;
c906108c 2509 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
2510 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2511 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
0b603eba 2512 dummy_target.to_xfer_partial = default_xfer_partial;
c906108c
SS
2513 dummy_target.to_magic = OPS_MAGIC;
2514}
c906108c 2515\f
c906108c 2516static void
fba45db2 2517debug_to_open (char *args, int from_tty)
c906108c
SS
2518{
2519 debug_target.to_open (args, from_tty);
2520
96baa820 2521 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2522}
2523
f1c07ab0
AC
2524void
2525target_close (struct target_ops *targ, int quitting)
2526{
2527 if (targ->to_xclose != NULL)
2528 targ->to_xclose (targ, quitting);
2529 else if (targ->to_close != NULL)
2530 targ->to_close (quitting);
947b8855
PA
2531
2532 if (targetdebug)
2533 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
f1c07ab0
AC
2534}
2535
136d6dae
VP
2536void
2537target_attach (char *args, int from_tty)
2538{
2539 struct target_ops *t;
2540 for (t = current_target.beneath; t != NULL; t = t->beneath)
2541 {
2542 if (t->to_attach != NULL)
2543 {
2544 t->to_attach (t, args, from_tty);
947b8855
PA
2545 if (targetdebug)
2546 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2547 args, from_tty);
136d6dae
VP
2548 return;
2549 }
2550 }
2551
2552 internal_error (__FILE__, __LINE__,
2553 "could not find a target to attach");
2554}
2555
c906108c 2556static void
fba45db2 2557debug_to_post_attach (int pid)
c906108c
SS
2558{
2559 debug_target.to_post_attach (pid);
2560
96baa820 2561 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2562}
2563
c906108c 2564static void
39f77062 2565debug_to_resume (ptid_t ptid, int step, enum target_signal siggnal)
c906108c 2566{
39f77062 2567 debug_target.to_resume (ptid, step, siggnal);
c906108c 2568
39f77062 2569 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", PIDGET (ptid),
c906108c
SS
2570 step ? "step" : "continue",
2571 target_signal_to_name (siggnal));
2572}
2573
f00150c9
DE
2574/* Return a pretty printed form of target_waitstatus.
2575 Space for the result is malloc'd, caller must free. */
c906108c 2576
f00150c9
DE
2577char *
2578target_waitstatus_to_string (const struct target_waitstatus *ws)
2579{
2580 const char *kind_str = "status->kind = ";
c906108c 2581
f00150c9 2582 switch (ws->kind)
c906108c
SS
2583 {
2584 case TARGET_WAITKIND_EXITED:
f00150c9
DE
2585 return xstrprintf ("%sexited, status = %d",
2586 kind_str, ws->value.integer);
c906108c 2587 case TARGET_WAITKIND_STOPPED:
f00150c9
DE
2588 return xstrprintf ("%sstopped, signal = %s",
2589 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2590 case TARGET_WAITKIND_SIGNALLED:
f00150c9
DE
2591 return xstrprintf ("%ssignalled, signal = %s",
2592 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2593 case TARGET_WAITKIND_LOADED:
f00150c9 2594 return xstrprintf ("%sloaded", kind_str);
c906108c 2595 case TARGET_WAITKIND_FORKED:
f00150c9 2596 return xstrprintf ("%sforked", kind_str);
c906108c 2597 case TARGET_WAITKIND_VFORKED:
f00150c9 2598 return xstrprintf ("%svforked", kind_str);
c906108c 2599 case TARGET_WAITKIND_EXECD:
f00150c9
DE
2600 return xstrprintf ("%sexecd", kind_str);
2601 case TARGET_WAITKIND_SYSCALL_ENTRY:
2602 return xstrprintf ("%ssyscall-entry", kind_str);
2603 case TARGET_WAITKIND_SYSCALL_RETURN:
2604 return xstrprintf ("%ssyscall-return", kind_str);
c906108c 2605 case TARGET_WAITKIND_SPURIOUS:
f00150c9
DE
2606 return xstrprintf ("%sspurious", kind_str);
2607 case TARGET_WAITKIND_IGNORE:
2608 return xstrprintf ("%signore", kind_str);
2609 case TARGET_WAITKIND_NO_HISTORY:
2610 return xstrprintf ("%sno-history", kind_str);
c906108c 2611 default:
f00150c9 2612 return xstrprintf ("%sunknown???", kind_str);
c906108c 2613 }
f00150c9
DE
2614}
2615
2616static ptid_t
2617debug_to_wait (ptid_t ptid, struct target_waitstatus *status)
2618{
2619 ptid_t retval;
2620 char *status_string;
2621
2622 retval = debug_target.to_wait (ptid, status);
2623
2624 fprintf_unfiltered (gdb_stdlog,
2625 "target_wait (%d, status) = %d, ", PIDGET (ptid),
2626 PIDGET (retval));
2627
2628 status_string = target_waitstatus_to_string (status);
2629 fprintf_unfiltered (gdb_stdlog, "%s\n", status_string);
2630 xfree (status_string);
c906108c
SS
2631
2632 return retval;
2633}
2634
bf0c5130 2635static void
56be3814
UW
2636debug_print_register (const char * func,
2637 struct regcache *regcache, int regno)
bf0c5130 2638{
f8d29908 2639 struct gdbarch *gdbarch = get_regcache_arch (regcache);
bf0c5130 2640 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 2641 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
2642 && gdbarch_register_name (gdbarch, regno) != NULL
2643 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2644 fprintf_unfiltered (gdb_stdlog, "(%s)",
2645 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
2646 else
2647 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 2648 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 2649 {
f8d29908 2650 int i, size = register_size (gdbarch, regno);
d9d9c31f 2651 unsigned char buf[MAX_REGISTER_SIZE];
0ff58721 2652 regcache_raw_collect (regcache, regno, buf);
bf0c5130 2653 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 2654 for (i = 0; i < size; i++)
bf0c5130
AC
2655 {
2656 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2657 }
81c4a259 2658 if (size <= sizeof (LONGEST))
bf0c5130 2659 {
81c4a259 2660 ULONGEST val = extract_unsigned_integer (buf, size);
0b1553bc
UW
2661 fprintf_unfiltered (gdb_stdlog, " %s %s",
2662 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
2663 }
2664 }
2665 fprintf_unfiltered (gdb_stdlog, "\n");
2666}
2667
c906108c 2668static void
56be3814 2669debug_to_fetch_registers (struct regcache *regcache, int regno)
c906108c 2670{
56be3814
UW
2671 debug_target.to_fetch_registers (regcache, regno);
2672 debug_print_register ("target_fetch_registers", regcache, regno);
c906108c
SS
2673}
2674
2675static void
56be3814 2676debug_to_store_registers (struct regcache *regcache, int regno)
c906108c 2677{
56be3814
UW
2678 debug_target.to_store_registers (regcache, regno);
2679 debug_print_register ("target_store_registers", regcache, regno);
bf0c5130 2680 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
2681}
2682
2683static void
316f2060 2684debug_to_prepare_to_store (struct regcache *regcache)
c906108c 2685{
316f2060 2686 debug_target.to_prepare_to_store (regcache);
c906108c 2687
96baa820 2688 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
2689}
2690
2691static int
961cb7b5 2692deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
2693 int write, struct mem_attrib *attrib,
2694 struct target_ops *target)
c906108c
SS
2695{
2696 int retval;
2697
c8e73a31
AC
2698 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2699 attrib, target);
c906108c 2700
96baa820 2701 fprintf_unfiltered (gdb_stdlog,
53b71562
JB
2702 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2703 paddress (memaddr), len, write ? "write" : "read",
2704 retval);
c906108c 2705
c906108c
SS
2706 if (retval > 0)
2707 {
2708 int i;
2709
96baa820 2710 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
2711 for (i = 0; i < retval; i++)
2712 {
53b71562 2713 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
2714 {
2715 if (targetdebug < 2 && i > 0)
2716 {
2717 fprintf_unfiltered (gdb_stdlog, " ...");
2718 break;
2719 }
2720 fprintf_unfiltered (gdb_stdlog, "\n");
2721 }
2bc416ba 2722
96baa820 2723 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
2724 }
2725 }
2726
96baa820 2727 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
2728
2729 return retval;
2730}
2731
2732static void
fba45db2 2733debug_to_files_info (struct target_ops *target)
c906108c
SS
2734{
2735 debug_target.to_files_info (target);
2736
96baa820 2737 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
2738}
2739
2740static int
8181d85f 2741debug_to_insert_breakpoint (struct bp_target_info *bp_tgt)
c906108c
SS
2742{
2743 int retval;
2744
8181d85f 2745 retval = debug_target.to_insert_breakpoint (bp_tgt);
c906108c 2746
96baa820 2747 fprintf_unfiltered (gdb_stdlog,
104c1213 2748 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 2749 (unsigned long) bp_tgt->placed_address,
104c1213 2750 (unsigned long) retval);
c906108c
SS
2751 return retval;
2752}
2753
2754static int
8181d85f 2755debug_to_remove_breakpoint (struct bp_target_info *bp_tgt)
c906108c
SS
2756{
2757 int retval;
2758
8181d85f 2759 retval = debug_target.to_remove_breakpoint (bp_tgt);
c906108c 2760
96baa820 2761 fprintf_unfiltered (gdb_stdlog,
104c1213 2762 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 2763 (unsigned long) bp_tgt->placed_address,
104c1213 2764 (unsigned long) retval);
c906108c
SS
2765 return retval;
2766}
2767
ccaa32c7
GS
2768static int
2769debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2770{
2771 int retval;
2772
2773 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2774
2775 fprintf_unfiltered (gdb_stdlog,
2776 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2777 (unsigned long) type,
2778 (unsigned long) cnt,
2779 (unsigned long) from_tty,
2780 (unsigned long) retval);
2781 return retval;
2782}
2783
e0d24f8d
WZ
2784static int
2785debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2786{
2787 CORE_ADDR retval;
2788
2789 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2790
2791 fprintf_unfiltered (gdb_stdlog,
2792 "TARGET_REGION_OK_FOR_HW_WATCHPOINT (%ld, %ld) = 0x%lx\n",
2793 (unsigned long) addr,
2794 (unsigned long) len,
2795 (unsigned long) retval);
2796 return retval;
2797}
2798
ccaa32c7
GS
2799static int
2800debug_to_stopped_by_watchpoint (void)
2801{
2802 int retval;
2803
2804 retval = debug_target.to_stopped_by_watchpoint ();
2805
2806 fprintf_unfiltered (gdb_stdlog,
2807 "STOPPED_BY_WATCHPOINT () = %ld\n",
2808 (unsigned long) retval);
2809 return retval;
2810}
2811
4aa7a7f5
JJ
2812static int
2813debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 2814{
4aa7a7f5 2815 int retval;
ccaa32c7 2816
4aa7a7f5 2817 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
2818
2819 fprintf_unfiltered (gdb_stdlog,
4aa7a7f5
JJ
2820 "target_stopped_data_address ([0x%lx]) = %ld\n",
2821 (unsigned long)*addr,
2822 (unsigned long)retval);
ccaa32c7
GS
2823 return retval;
2824}
2825
5009afc5
AS
2826static int
2827debug_to_watchpoint_addr_within_range (struct target_ops *target,
2828 CORE_ADDR addr,
2829 CORE_ADDR start, int length)
2830{
2831 int retval;
2832
2833 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
2834 start, length);
2835
2836 fprintf_filtered (gdb_stdlog,
2837 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
2838 (unsigned long) addr, (unsigned long) start, length,
2839 retval);
2840 return retval;
2841}
2842
ccaa32c7 2843static int
8181d85f 2844debug_to_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
ccaa32c7
GS
2845{
2846 int retval;
2847
8181d85f 2848 retval = debug_target.to_insert_hw_breakpoint (bp_tgt);
ccaa32c7
GS
2849
2850 fprintf_unfiltered (gdb_stdlog,
2851 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 2852 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
2853 (unsigned long) retval);
2854 return retval;
2855}
2856
2857static int
8181d85f 2858debug_to_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
ccaa32c7
GS
2859{
2860 int retval;
2861
8181d85f 2862 retval = debug_target.to_remove_hw_breakpoint (bp_tgt);
ccaa32c7
GS
2863
2864 fprintf_unfiltered (gdb_stdlog,
2865 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 2866 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
2867 (unsigned long) retval);
2868 return retval;
2869}
2870
2871static int
2872debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
2873{
2874 int retval;
2875
2876 retval = debug_target.to_insert_watchpoint (addr, len, type);
2877
2878 fprintf_unfiltered (gdb_stdlog,
2879 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
2880 (unsigned long) addr, len, type, (unsigned long) retval);
2881 return retval;
2882}
2883
2884static int
2885debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
2886{
2887 int retval;
2888
ecde4882 2889 retval = debug_target.to_remove_watchpoint (addr, len, type);
ccaa32c7
GS
2890
2891 fprintf_unfiltered (gdb_stdlog,
ecde4882 2892 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
ccaa32c7
GS
2893 (unsigned long) addr, len, type, (unsigned long) retval);
2894 return retval;
2895}
2896
c906108c 2897static void
fba45db2 2898debug_to_terminal_init (void)
c906108c
SS
2899{
2900 debug_target.to_terminal_init ();
2901
96baa820 2902 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
2903}
2904
2905static void
fba45db2 2906debug_to_terminal_inferior (void)
c906108c
SS
2907{
2908 debug_target.to_terminal_inferior ();
2909
96baa820 2910 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
2911}
2912
2913static void
fba45db2 2914debug_to_terminal_ours_for_output (void)
c906108c
SS
2915{
2916 debug_target.to_terminal_ours_for_output ();
2917
96baa820 2918 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
2919}
2920
2921static void
fba45db2 2922debug_to_terminal_ours (void)
c906108c
SS
2923{
2924 debug_target.to_terminal_ours ();
2925
96baa820 2926 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
2927}
2928
a790ad35
SC
2929static void
2930debug_to_terminal_save_ours (void)
2931{
2932 debug_target.to_terminal_save_ours ();
2933
2934 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
2935}
2936
c906108c 2937static void
fba45db2 2938debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
2939{
2940 debug_target.to_terminal_info (arg, from_tty);
2941
96baa820 2942 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
2943 from_tty);
2944}
2945
2946static void
fba45db2 2947debug_to_kill (void)
c906108c
SS
2948{
2949 debug_target.to_kill ();
2950
96baa820 2951 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
c906108c
SS
2952}
2953
2954static void
fba45db2 2955debug_to_load (char *args, int from_tty)
c906108c
SS
2956{
2957 debug_target.to_load (args, from_tty);
2958
96baa820 2959 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
2960}
2961
2962static int
fba45db2 2963debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
2964{
2965 int retval;
2966
2967 retval = debug_target.to_lookup_symbol (name, addrp);
2968
96baa820 2969 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
2970
2971 return retval;
2972}
2973
c906108c 2974static void
39f77062 2975debug_to_post_startup_inferior (ptid_t ptid)
c906108c 2976{
39f77062 2977 debug_target.to_post_startup_inferior (ptid);
c906108c 2978
96baa820 2979 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 2980 PIDGET (ptid));
c906108c
SS
2981}
2982
2983static void
fba45db2 2984debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
2985{
2986 debug_target.to_acknowledge_created_inferior (pid);
2987
96baa820 2988 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
2989 pid);
2990}
2991
fa113d1a 2992static void
fba45db2 2993debug_to_insert_fork_catchpoint (int pid)
c906108c 2994{
fa113d1a 2995 debug_target.to_insert_fork_catchpoint (pid);
c906108c 2996
fa113d1a
AC
2997 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
2998 pid);
c906108c
SS
2999}
3000
3001static int
fba45db2 3002debug_to_remove_fork_catchpoint (int pid)
c906108c 3003{
c5aa993b 3004 int retval;
c906108c
SS
3005
3006 retval = debug_target.to_remove_fork_catchpoint (pid);
3007
96baa820 3008 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 3009 pid, retval);
c906108c
SS
3010
3011 return retval;
3012}
3013
fa113d1a 3014static void
fba45db2 3015debug_to_insert_vfork_catchpoint (int pid)
c906108c 3016{
fa113d1a 3017 debug_target.to_insert_vfork_catchpoint (pid);
c906108c 3018
fa113d1a
AC
3019 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3020 pid);
c906108c
SS
3021}
3022
3023static int
fba45db2 3024debug_to_remove_vfork_catchpoint (int pid)
c906108c 3025{
c5aa993b 3026 int retval;
c906108c
SS
3027
3028 retval = debug_target.to_remove_vfork_catchpoint (pid);
3029
96baa820 3030 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 3031 pid, retval);
c906108c
SS
3032
3033 return retval;
3034}
3035
fa113d1a 3036static void
fba45db2 3037debug_to_insert_exec_catchpoint (int pid)
c906108c 3038{
fa113d1a 3039 debug_target.to_insert_exec_catchpoint (pid);
c906108c 3040
fa113d1a
AC
3041 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3042 pid);
c906108c
SS
3043}
3044
3045static int
fba45db2 3046debug_to_remove_exec_catchpoint (int pid)
c906108c 3047{
c5aa993b 3048 int retval;
c906108c
SS
3049
3050 retval = debug_target.to_remove_exec_catchpoint (pid);
3051
96baa820 3052 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 3053 pid, retval);
c906108c
SS
3054
3055 return retval;
3056}
3057
c906108c 3058static int
fba45db2 3059debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 3060{
c5aa993b 3061 int has_exited;
c906108c
SS
3062
3063 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3064
96baa820 3065 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 3066 pid, wait_status, *exit_status, has_exited);
c906108c
SS
3067
3068 return has_exited;
3069}
3070
c906108c 3071static int
fba45db2 3072debug_to_can_run (void)
c906108c
SS
3073{
3074 int retval;
3075
3076 retval = debug_target.to_can_run ();
3077
96baa820 3078 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
3079
3080 return retval;
3081}
3082
3083static void
39f77062 3084debug_to_notice_signals (ptid_t ptid)
c906108c 3085{
39f77062 3086 debug_target.to_notice_signals (ptid);
c906108c 3087
39f77062
KB
3088 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3089 PIDGET (ptid));
c906108c
SS
3090}
3091
3092static int
39f77062 3093debug_to_thread_alive (ptid_t ptid)
c906108c
SS
3094{
3095 int retval;
3096
39f77062 3097 retval = debug_target.to_thread_alive (ptid);
c906108c 3098
96baa820 3099 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
39f77062 3100 PIDGET (ptid), retval);
c906108c
SS
3101
3102 return retval;
3103}
3104
0d06e24b 3105static void
fba45db2 3106debug_to_find_new_threads (void)
0d06e24b
JM
3107{
3108 debug_target.to_find_new_threads ();
3109
3110 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
3111}
3112
c906108c 3113static void
94cc34af 3114debug_to_stop (ptid_t ptid)
c906108c 3115{
94cc34af 3116 debug_target.to_stop (ptid);
c906108c 3117
94cc34af
PA
3118 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3119 target_pid_to_str (ptid));
c906108c
SS
3120}
3121
96baa820
JM
3122static void
3123debug_to_rcmd (char *command,
d9fcf2fb 3124 struct ui_file *outbuf)
96baa820
JM
3125{
3126 debug_target.to_rcmd (command, outbuf);
3127 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3128}
3129
c906108c 3130static char *
fba45db2 3131debug_to_pid_to_exec_file (int pid)
c906108c 3132{
c5aa993b 3133 char *exec_file;
c906108c
SS
3134
3135 exec_file = debug_target.to_pid_to_exec_file (pid);
3136
96baa820 3137 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 3138 pid, exec_file);
c906108c
SS
3139
3140 return exec_file;
3141}
3142
c906108c 3143static void
fba45db2 3144setup_target_debug (void)
c906108c
SS
3145{
3146 memcpy (&debug_target, &current_target, sizeof debug_target);
3147
3148 current_target.to_open = debug_to_open;
c906108c 3149 current_target.to_post_attach = debug_to_post_attach;
c906108c
SS
3150 current_target.to_resume = debug_to_resume;
3151 current_target.to_wait = debug_to_wait;
c906108c
SS
3152 current_target.to_fetch_registers = debug_to_fetch_registers;
3153 current_target.to_store_registers = debug_to_store_registers;
3154 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 3155 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
3156 current_target.to_files_info = debug_to_files_info;
3157 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3158 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
3159 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3160 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3161 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3162 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3163 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3164 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3165 current_target.to_stopped_data_address = debug_to_stopped_data_address;
5009afc5 3166 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
e0d24f8d 3167 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
c906108c
SS
3168 current_target.to_terminal_init = debug_to_terminal_init;
3169 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3170 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3171 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 3172 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c
SS
3173 current_target.to_terminal_info = debug_to_terminal_info;
3174 current_target.to_kill = debug_to_kill;
3175 current_target.to_load = debug_to_load;
3176 current_target.to_lookup_symbol = debug_to_lookup_symbol;
c906108c
SS
3177 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3178 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
c906108c
SS
3179 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3180 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3181 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3182 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
3183 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3184 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 3185 current_target.to_has_exited = debug_to_has_exited;
c906108c
SS
3186 current_target.to_can_run = debug_to_can_run;
3187 current_target.to_notice_signals = debug_to_notice_signals;
3188 current_target.to_thread_alive = debug_to_thread_alive;
0d06e24b 3189 current_target.to_find_new_threads = debug_to_find_new_threads;
c906108c 3190 current_target.to_stop = debug_to_stop;
96baa820 3191 current_target.to_rcmd = debug_to_rcmd;
c906108c 3192 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c906108c 3193}
c906108c 3194\f
c5aa993b
JM
3195
3196static char targ_desc[] =
3197"Names of targets and files being debugged.\n\
c906108c
SS
3198Shows the entire stack of targets currently in use (including the exec-file,\n\
3199core-file, and process, if any), as well as the symbol file name.";
3200
96baa820
JM
3201static void
3202do_monitor_command (char *cmd,
3203 int from_tty)
3204{
2b5fe715
AC
3205 if ((current_target.to_rcmd
3206 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 3207 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
3208 && (debug_target.to_rcmd
3209 == (void (*) (char *, struct ui_file *)) tcomplain)))
8a3fe4f8 3210 error (_("\"monitor\" command not supported by this target."));
96baa820
JM
3211 target_rcmd (cmd, gdb_stdtarg);
3212}
3213
87680a14
JB
3214/* Print the name of each layers of our target stack. */
3215
3216static void
3217maintenance_print_target_stack (char *cmd, int from_tty)
3218{
3219 struct target_ops *t;
3220
3221 printf_filtered (_("The current target stack is:\n"));
3222
3223 for (t = target_stack; t != NULL; t = t->beneath)
3224 {
3225 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3226 }
3227}
3228
c6ebd6cf
VP
3229/* Controls if async mode is permitted. */
3230int target_async_permitted = 0;
3231
3232/* The set command writes to this variable. If the inferior is
3233 executing, linux_nat_async_permitted is *not* updated. */
3234static int target_async_permitted_1 = 0;
3235
3236static void
3237set_maintenance_target_async_permitted (char *args, int from_tty,
3238 struct cmd_list_element *c)
3239{
3240 if (target_has_execution)
3241 {
3242 target_async_permitted_1 = target_async_permitted;
3243 error (_("Cannot change this setting while the inferior is running."));
3244 }
3245
3246 target_async_permitted = target_async_permitted_1;
3247}
3248
3249static void
3250show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3251 struct cmd_list_element *c,
3252 const char *value)
3253{
3254 fprintf_filtered (file, _("\
3255Controlling the inferior in asynchronous mode is %s.\n"), value);
3256}
3257
c906108c 3258void
fba45db2 3259initialize_targets (void)
c906108c
SS
3260{
3261 init_dummy_target ();
3262 push_target (&dummy_target);
3263
3264 add_info ("target", target_info, targ_desc);
3265 add_info ("files", target_info, targ_desc);
3266
85c07804
AC
3267 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3268Set target debugging."), _("\
3269Show target debugging."), _("\
333dabeb
DJ
3270When non-zero, target debugging is enabled. Higher numbers are more\n\
3271verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804
AC
3272command."),
3273 NULL,
920d2a44 3274 show_targetdebug,
85c07804 3275 &setdebuglist, &showdebuglist);
3a11626d 3276
2bc416ba 3277 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
3278 &trust_readonly, _("\
3279Set mode for reading from readonly sections."), _("\
3280Show mode for reading from readonly sections."), _("\
3a11626d
MS
3281When this mode is on, memory reads from readonly sections (such as .text)\n\
3282will be read from the object file instead of from the target. This will\n\
7915a72c 3283result in significant performance improvement for remote targets."),
2c5b56ce 3284 NULL,
920d2a44 3285 show_trust_readonly,
e707bbc2 3286 &setlist, &showlist);
96baa820
JM
3287
3288 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 3289 _("Send a command to the remote monitor (remote targets only)."));
96baa820 3290
87680a14
JB
3291 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3292 _("Print the name of each layer of the internal target stack."),
3293 &maintenanceprintlist);
3294
c6ebd6cf
VP
3295 add_setshow_boolean_cmd ("target-async", no_class,
3296 &target_async_permitted_1, _("\
3297Set whether gdb controls the inferior in asynchronous mode."), _("\
3298Show whether gdb controls the inferior in asynchronous mode."), _("\
3299Tells gdb whether to control the inferior in asynchronous mode."),
3300 set_maintenance_target_async_permitted,
3301 show_maintenance_target_async_permitted,
3302 &setlist,
3303 &showlist);
3304
8add0441 3305 target_dcache = dcache_init ();
c906108c 3306}
This page took 1.028375 seconds and 4 git commands to generate.