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