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