1 /* Select target systems and architectures at runtime for GDB.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 Contributed by Cygnus Support.
9 This file is part of GDB.
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
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
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.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdb_string.h"
38 #include "gdb_assert.h"
40 #include "exceptions.h"
41 #include "target-descriptions.h"
42 #include "gdbthread.h"
46 static void target_info (char *, int);
48 static void kill_or_be_killed (int);
50 static void default_terminal_info (char *, int);
52 static int default_watchpoint_addr_within_range (struct target_ops
*,
53 CORE_ADDR
, CORE_ADDR
, int);
55 static int default_region_ok_for_hw_watchpoint (CORE_ADDR
, int);
57 static int nosymbol (char *, CORE_ADDR
*);
59 static void tcomplain (void) ATTR_NORETURN
;
61 static int nomemory (CORE_ADDR
, char *, int, int, struct target_ops
*);
63 static int return_zero (void);
65 static int return_one (void);
67 static int return_minus_one (void);
69 void target_ignore (void);
71 static void target_command (char *, int);
73 static struct target_ops
*find_default_run_target (char *);
75 static void nosupport_runtime (void);
77 static LONGEST
default_xfer_partial (struct target_ops
*ops
,
78 enum target_object object
,
79 const char *annex
, gdb_byte
*readbuf
,
80 const gdb_byte
*writebuf
,
81 ULONGEST offset
, LONGEST len
);
83 static LONGEST
current_xfer_partial (struct target_ops
*ops
,
84 enum target_object object
,
85 const char *annex
, gdb_byte
*readbuf
,
86 const gdb_byte
*writebuf
,
87 ULONGEST offset
, LONGEST len
);
89 static LONGEST
target_xfer_partial (struct target_ops
*ops
,
90 enum target_object object
,
92 void *readbuf
, const void *writebuf
,
93 ULONGEST offset
, LONGEST len
);
95 static void init_dummy_target (void);
97 static struct target_ops debug_target
;
99 static void debug_to_open (char *, int);
101 static void debug_to_prepare_to_store (struct regcache
*);
103 static void debug_to_files_info (struct target_ops
*);
105 static int debug_to_insert_breakpoint (struct bp_target_info
*);
107 static int debug_to_remove_breakpoint (struct bp_target_info
*);
109 static int debug_to_can_use_hw_breakpoint (int, int, int);
111 static int debug_to_insert_hw_breakpoint (struct bp_target_info
*);
113 static int debug_to_remove_hw_breakpoint (struct bp_target_info
*);
115 static int debug_to_insert_watchpoint (CORE_ADDR
, int, int);
117 static int debug_to_remove_watchpoint (CORE_ADDR
, int, int);
119 static int debug_to_stopped_by_watchpoint (void);
121 static int debug_to_stopped_data_address (struct target_ops
*, CORE_ADDR
*);
123 static int debug_to_watchpoint_addr_within_range (struct target_ops
*,
124 CORE_ADDR
, CORE_ADDR
, int);
126 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR
, int);
128 static void debug_to_terminal_init (void);
130 static void debug_to_terminal_inferior (void);
132 static void debug_to_terminal_ours_for_output (void);
134 static void debug_to_terminal_save_ours (void);
136 static void debug_to_terminal_ours (void);
138 static void debug_to_terminal_info (char *, int);
140 static void debug_to_load (char *, int);
142 static int debug_to_lookup_symbol (char *, CORE_ADDR
*);
144 static int debug_to_can_run (void);
146 static void debug_to_notice_signals (ptid_t
);
148 static void debug_to_stop (ptid_t
);
150 /* NOTE: cagney/2004-09-29: Many targets reference this variable in
151 wierd and mysterious ways. Putting the variable here lets those
152 wierd and mysterious ways keep building while they are being
153 converted to the inferior inheritance structure. */
154 struct target_ops deprecated_child_ops
;
156 /* Pointer to array of target architecture structures; the size of the
157 array; the current index into the array; the allocated size of the
159 struct target_ops
**target_structs
;
160 unsigned target_struct_size
;
161 unsigned target_struct_index
;
162 unsigned target_struct_allocsize
;
163 #define DEFAULT_ALLOCSIZE 10
165 /* The initial current target, so that there is always a semi-valid
168 static struct target_ops dummy_target
;
170 /* Top of target stack. */
172 static struct target_ops
*target_stack
;
174 /* The target structure we are currently using to talk to a process
175 or file or whatever "inferior" we have. */
177 struct target_ops current_target
;
179 /* Command list for target. */
181 static struct cmd_list_element
*targetlist
= NULL
;
183 /* Nonzero if we should trust readonly sections from the
184 executable when reading memory. */
186 static int trust_readonly
= 0;
188 /* Nonzero if we should show true memory content including
189 memory breakpoint inserted by gdb. */
191 static int show_memory_breakpoints
= 0;
193 /* Non-zero if we want to see trace of target level stuff. */
195 static int targetdebug
= 0;
197 show_targetdebug (struct ui_file
*file
, int from_tty
,
198 struct cmd_list_element
*c
, const char *value
)
200 fprintf_filtered (file
, _("Target debugging is %s.\n"), value
);
203 static void setup_target_debug (void);
205 DCACHE
*target_dcache
;
207 /* The user just typed 'target' without the name of a target. */
210 target_command (char *arg
, int from_tty
)
212 fputs_filtered ("Argument required (target name). Try `help target'\n",
216 /* Default target_has_* methods for process_stratum targets. */
219 default_child_has_all_memory (struct target_ops
*ops
)
221 /* If no inferior selected, then we can't read memory here. */
222 if (ptid_equal (inferior_ptid
, null_ptid
))
229 default_child_has_memory (struct target_ops
*ops
)
231 /* If no inferior selected, then we can't read memory here. */
232 if (ptid_equal (inferior_ptid
, null_ptid
))
239 default_child_has_stack (struct target_ops
*ops
)
241 /* If no inferior selected, there's no stack. */
242 if (ptid_equal (inferior_ptid
, null_ptid
))
249 default_child_has_registers (struct target_ops
*ops
)
251 /* Can't read registers from no inferior. */
252 if (ptid_equal (inferior_ptid
, null_ptid
))
259 default_child_has_execution (struct target_ops
*ops
)
261 /* If there's no thread selected, then we can't make it run through
263 if (ptid_equal (inferior_ptid
, null_ptid
))
271 target_has_all_memory_1 (void)
273 struct target_ops
*t
;
275 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
276 if (t
->to_has_all_memory (t
))
283 target_has_memory_1 (void)
285 struct target_ops
*t
;
287 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
288 if (t
->to_has_memory (t
))
295 target_has_stack_1 (void)
297 struct target_ops
*t
;
299 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
300 if (t
->to_has_stack (t
))
307 target_has_registers_1 (void)
309 struct target_ops
*t
;
311 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
312 if (t
->to_has_registers (t
))
319 target_has_execution_1 (void)
321 struct target_ops
*t
;
323 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
324 if (t
->to_has_execution (t
))
330 /* Add a possible target architecture to the list. */
333 add_target (struct target_ops
*t
)
335 /* Provide default values for all "must have" methods. */
336 if (t
->to_xfer_partial
== NULL
)
337 t
->to_xfer_partial
= default_xfer_partial
;
339 if (t
->to_has_all_memory
== NULL
)
340 t
->to_has_all_memory
= (int (*) (struct target_ops
*)) return_zero
;
342 if (t
->to_has_memory
== NULL
)
343 t
->to_has_memory
= (int (*) (struct target_ops
*)) return_zero
;
345 if (t
->to_has_stack
== NULL
)
346 t
->to_has_stack
= (int (*) (struct target_ops
*)) return_zero
;
348 if (t
->to_has_registers
== NULL
)
349 t
->to_has_registers
= (int (*) (struct target_ops
*)) return_zero
;
351 if (t
->to_has_execution
== NULL
)
352 t
->to_has_execution
= (int (*) (struct target_ops
*)) return_zero
;
356 target_struct_allocsize
= DEFAULT_ALLOCSIZE
;
357 target_structs
= (struct target_ops
**) xmalloc
358 (target_struct_allocsize
* sizeof (*target_structs
));
360 if (target_struct_size
>= target_struct_allocsize
)
362 target_struct_allocsize
*= 2;
363 target_structs
= (struct target_ops
**)
364 xrealloc ((char *) target_structs
,
365 target_struct_allocsize
* sizeof (*target_structs
));
367 target_structs
[target_struct_size
++] = t
;
369 if (targetlist
== NULL
)
370 add_prefix_cmd ("target", class_run
, target_command
, _("\
371 Connect to a target machine or process.\n\
372 The first argument is the type or protocol of the target machine.\n\
373 Remaining arguments are interpreted by the target protocol. For more\n\
374 information on the arguments for a particular protocol, type\n\
375 `help target ' followed by the protocol name."),
376 &targetlist
, "target ", 0, &cmdlist
);
377 add_cmd (t
->to_shortname
, no_class
, t
->to_open
, t
->to_doc
, &targetlist
);
390 struct target_ops
*t
;
392 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
393 if (t
->to_kill
!= NULL
)
396 fprintf_unfiltered (gdb_stdlog
, "target_kill ()\n");
406 target_load (char *arg
, int from_tty
)
408 dcache_invalidate (target_dcache
);
409 (*current_target
.to_load
) (arg
, from_tty
);
413 target_create_inferior (char *exec_file
, char *args
,
414 char **env
, int from_tty
)
416 struct target_ops
*t
;
417 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
419 if (t
->to_create_inferior
!= NULL
)
421 t
->to_create_inferior (t
, exec_file
, args
, env
, from_tty
);
423 fprintf_unfiltered (gdb_stdlog
,
424 "target_create_inferior (%s, %s, xxx, %d)\n",
425 exec_file
, args
, from_tty
);
430 internal_error (__FILE__
, __LINE__
,
431 "could not find a target to create inferior");
435 target_terminal_inferior (void)
437 /* A background resume (``run&'') should leave GDB in control of the
439 if (target_is_async_p () && !sync_execution
)
442 /* If GDB is resuming the inferior in the foreground, install
443 inferior's terminal modes. */
444 (*current_target
.to_terminal_inferior
) ();
448 nomemory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
449 struct target_ops
*t
)
451 errno
= EIO
; /* Can't read/write this location */
452 return 0; /* No bytes handled */
458 error (_("You can't do that when your target is `%s'"),
459 current_target
.to_shortname
);
465 error (_("You can't do that without a process to debug."));
469 nosymbol (char *name
, CORE_ADDR
*addrp
)
471 return 1; /* Symbol does not exist in target env */
475 nosupport_runtime (void)
477 if (ptid_equal (inferior_ptid
, null_ptid
))
480 error (_("No run-time support for this"));
485 default_terminal_info (char *args
, int from_tty
)
487 printf_unfiltered (_("No saved terminal information.\n"));
490 /* This is the default target_create_inferior and target_attach function.
491 If the current target is executing, it asks whether to kill it off.
492 If this function returns without calling error(), it has killed off
493 the target, and the operation should be attempted. */
496 kill_or_be_killed (int from_tty
)
498 if (target_has_execution
)
500 printf_unfiltered (_("You are already running a program:\n"));
501 target_files_info ();
502 if (query (_("Kill it? ")))
505 if (target_has_execution
)
506 error (_("Killing the program did not help."));
511 error (_("Program not killed."));
517 /* A default implementation for the to_get_ada_task_ptid target method.
519 This function builds the PTID by using both LWP and TID as part of
520 the PTID lwp and tid elements. The pid used is the pid of the
524 default_get_ada_task_ptid (long lwp
, long tid
)
526 return ptid_build (ptid_get_pid (inferior_ptid
), lwp
, tid
);
529 /* Go through the target stack from top to bottom, copying over zero
530 entries in current_target, then filling in still empty entries. In
531 effect, we are doing class inheritance through the pushed target
534 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
535 is currently implemented, is that it discards any knowledge of
536 which target an inherited method originally belonged to.
537 Consequently, new new target methods should instead explicitly and
538 locally search the target stack for the target that can handle the
542 update_current_target (void)
544 struct target_ops
*t
;
546 /* First, reset current's contents. */
547 memset (¤t_target
, 0, sizeof (current_target
));
549 #define INHERIT(FIELD, TARGET) \
550 if (!current_target.FIELD) \
551 current_target.FIELD = (TARGET)->FIELD
553 for (t
= target_stack
; t
; t
= t
->beneath
)
555 INHERIT (to_shortname
, t
);
556 INHERIT (to_longname
, t
);
558 /* Do not inherit to_open. */
559 /* Do not inherit to_close. */
560 /* Do not inherit to_attach. */
561 INHERIT (to_post_attach
, t
);
562 INHERIT (to_attach_no_wait
, t
);
563 /* Do not inherit to_detach. */
564 /* Do not inherit to_disconnect. */
565 /* Do not inherit to_resume. */
566 /* Do not inherit to_wait. */
567 /* Do not inherit to_fetch_registers. */
568 /* Do not inherit to_store_registers. */
569 INHERIT (to_prepare_to_store
, t
);
570 INHERIT (deprecated_xfer_memory
, t
);
571 INHERIT (to_files_info
, t
);
572 INHERIT (to_insert_breakpoint
, t
);
573 INHERIT (to_remove_breakpoint
, t
);
574 INHERIT (to_can_use_hw_breakpoint
, t
);
575 INHERIT (to_insert_hw_breakpoint
, t
);
576 INHERIT (to_remove_hw_breakpoint
, t
);
577 INHERIT (to_insert_watchpoint
, t
);
578 INHERIT (to_remove_watchpoint
, t
);
579 INHERIT (to_stopped_data_address
, t
);
580 INHERIT (to_have_steppable_watchpoint
, t
);
581 INHERIT (to_have_continuable_watchpoint
, t
);
582 INHERIT (to_stopped_by_watchpoint
, t
);
583 INHERIT (to_watchpoint_addr_within_range
, t
);
584 INHERIT (to_region_ok_for_hw_watchpoint
, t
);
585 INHERIT (to_terminal_init
, t
);
586 INHERIT (to_terminal_inferior
, t
);
587 INHERIT (to_terminal_ours_for_output
, t
);
588 INHERIT (to_terminal_ours
, t
);
589 INHERIT (to_terminal_save_ours
, t
);
590 INHERIT (to_terminal_info
, t
);
591 /* Do not inherit to_kill. */
592 INHERIT (to_load
, t
);
593 INHERIT (to_lookup_symbol
, t
);
594 /* Do no inherit to_create_inferior. */
595 INHERIT (to_post_startup_inferior
, t
);
596 INHERIT (to_acknowledge_created_inferior
, t
);
597 INHERIT (to_insert_fork_catchpoint
, t
);
598 INHERIT (to_remove_fork_catchpoint
, t
);
599 INHERIT (to_insert_vfork_catchpoint
, t
);
600 INHERIT (to_remove_vfork_catchpoint
, t
);
601 /* Do not inherit to_follow_fork. */
602 INHERIT (to_insert_exec_catchpoint
, t
);
603 INHERIT (to_remove_exec_catchpoint
, t
);
604 INHERIT (to_has_exited
, t
);
605 /* Do not inherit to_mourn_inferiour. */
606 INHERIT (to_can_run
, t
);
607 INHERIT (to_notice_signals
, t
);
608 /* Do not inherit to_thread_alive. */
609 /* Do not inherit to_find_new_threads. */
610 /* Do not inherit to_pid_to_str. */
611 INHERIT (to_extra_thread_info
, t
);
612 INHERIT (to_stop
, t
);
613 /* Do not inherit to_xfer_partial. */
614 INHERIT (to_rcmd
, t
);
615 INHERIT (to_pid_to_exec_file
, t
);
616 INHERIT (to_log_command
, t
);
617 INHERIT (to_stratum
, t
);
618 /* Do not inherit to_has_all_memory */
619 /* Do not inherit to_has_memory */
620 /* Do not inherit to_has_stack */
621 /* Do not inherit to_has_registers */
622 /* Do not inherit to_has_execution */
623 INHERIT (to_has_thread_control
, t
);
624 INHERIT (to_can_async_p
, t
);
625 INHERIT (to_is_async_p
, t
);
626 INHERIT (to_async
, t
);
627 INHERIT (to_async_mask
, t
);
628 INHERIT (to_find_memory_regions
, t
);
629 INHERIT (to_make_corefile_notes
, t
);
630 /* Do not inherit to_get_thread_local_address. */
631 INHERIT (to_can_execute_reverse
, t
);
632 /* Do not inherit to_read_description. */
633 INHERIT (to_get_ada_task_ptid
, t
);
634 /* Do not inherit to_search_memory. */
635 INHERIT (to_supports_multi_process
, t
);
636 INHERIT (to_magic
, t
);
637 /* Do not inherit to_memory_map. */
638 /* Do not inherit to_flash_erase. */
639 /* Do not inherit to_flash_done. */
643 /* Clean up a target struct so it no longer has any zero pointers in
644 it. Some entries are defaulted to a method that print an error,
645 others are hard-wired to a standard recursive default. */
647 #define de_fault(field, value) \
648 if (!current_target.field) \
649 current_target.field = value
652 (void (*) (char *, int))
657 de_fault (to_post_attach
,
660 de_fault (to_prepare_to_store
,
661 (void (*) (struct regcache
*))
663 de_fault (deprecated_xfer_memory
,
664 (int (*) (CORE_ADDR
, gdb_byte
*, int, int, struct mem_attrib
*, struct target_ops
*))
666 de_fault (to_files_info
,
667 (void (*) (struct target_ops
*))
669 de_fault (to_insert_breakpoint
,
670 memory_insert_breakpoint
);
671 de_fault (to_remove_breakpoint
,
672 memory_remove_breakpoint
);
673 de_fault (to_can_use_hw_breakpoint
,
674 (int (*) (int, int, int))
676 de_fault (to_insert_hw_breakpoint
,
677 (int (*) (struct bp_target_info
*))
679 de_fault (to_remove_hw_breakpoint
,
680 (int (*) (struct bp_target_info
*))
682 de_fault (to_insert_watchpoint
,
683 (int (*) (CORE_ADDR
, int, int))
685 de_fault (to_remove_watchpoint
,
686 (int (*) (CORE_ADDR
, int, int))
688 de_fault (to_stopped_by_watchpoint
,
691 de_fault (to_stopped_data_address
,
692 (int (*) (struct target_ops
*, CORE_ADDR
*))
694 de_fault (to_watchpoint_addr_within_range
,
695 default_watchpoint_addr_within_range
);
696 de_fault (to_region_ok_for_hw_watchpoint
,
697 default_region_ok_for_hw_watchpoint
);
698 de_fault (to_terminal_init
,
701 de_fault (to_terminal_inferior
,
704 de_fault (to_terminal_ours_for_output
,
707 de_fault (to_terminal_ours
,
710 de_fault (to_terminal_save_ours
,
713 de_fault (to_terminal_info
,
714 default_terminal_info
);
716 (void (*) (char *, int))
718 de_fault (to_lookup_symbol
,
719 (int (*) (char *, CORE_ADDR
*))
721 de_fault (to_post_startup_inferior
,
724 de_fault (to_acknowledge_created_inferior
,
727 de_fault (to_insert_fork_catchpoint
,
730 de_fault (to_remove_fork_catchpoint
,
733 de_fault (to_insert_vfork_catchpoint
,
736 de_fault (to_remove_vfork_catchpoint
,
739 de_fault (to_insert_exec_catchpoint
,
742 de_fault (to_remove_exec_catchpoint
,
745 de_fault (to_has_exited
,
746 (int (*) (int, int, int *))
748 de_fault (to_can_run
,
750 de_fault (to_notice_signals
,
753 de_fault (to_extra_thread_info
,
754 (char *(*) (struct thread_info
*))
759 current_target
.to_xfer_partial
= current_xfer_partial
;
761 (void (*) (char *, struct ui_file
*))
763 de_fault (to_pid_to_exec_file
,
767 (void (*) (void (*) (enum inferior_event_type
, void*), void*))
769 de_fault (to_async_mask
,
772 current_target
.to_read_description
= NULL
;
773 de_fault (to_get_ada_task_ptid
,
774 (ptid_t (*) (long, long))
775 default_get_ada_task_ptid
);
776 de_fault (to_supports_multi_process
,
781 /* Finally, position the target-stack beneath the squashed
782 "current_target". That way code looking for a non-inherited
783 target method can quickly and simply find it. */
784 current_target
.beneath
= target_stack
;
787 setup_target_debug ();
790 /* Push a new target type into the stack of the existing target accessors,
791 possibly superseding some of the existing accessors.
793 Result is zero if the pushed target ended up on top of the stack,
794 nonzero if at least one target is on top of it.
796 Rather than allow an empty stack, we always have the dummy target at
797 the bottom stratum, so we can call the function vectors without
801 push_target (struct target_ops
*t
)
803 struct target_ops
**cur
;
805 /* Check magic number. If wrong, it probably means someone changed
806 the struct definition, but not all the places that initialize one. */
807 if (t
->to_magic
!= OPS_MAGIC
)
809 fprintf_unfiltered (gdb_stderr
,
810 "Magic number of %s target struct wrong\n",
812 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
815 /* Find the proper stratum to install this target in. */
816 for (cur
= &target_stack
; (*cur
) != NULL
; cur
= &(*cur
)->beneath
)
818 if ((int) (t
->to_stratum
) >= (int) (*cur
)->to_stratum
)
822 /* If there's already targets at this stratum, remove them. */
823 /* FIXME: cagney/2003-10-15: I think this should be popping all
824 targets to CUR, and not just those at this stratum level. */
825 while ((*cur
) != NULL
&& t
->to_stratum
== (*cur
)->to_stratum
)
827 /* There's already something at this stratum level. Close it,
828 and un-hook it from the stack. */
829 struct target_ops
*tmp
= (*cur
);
830 (*cur
) = (*cur
)->beneath
;
832 target_close (tmp
, 0);
835 /* We have removed all targets in our stratum, now add the new one. */
839 update_current_target ();
842 return (t
!= target_stack
);
845 /* Remove a target_ops vector from the stack, wherever it may be.
846 Return how many times it was removed (0 or 1). */
849 unpush_target (struct target_ops
*t
)
851 struct target_ops
**cur
;
852 struct target_ops
*tmp
;
854 if (t
->to_stratum
== dummy_stratum
)
855 internal_error (__FILE__
, __LINE__
,
856 "Attempt to unpush the dummy target");
858 /* Look for the specified target. Note that we assume that a target
859 can only occur once in the target stack. */
861 for (cur
= &target_stack
; (*cur
) != NULL
; cur
= &(*cur
)->beneath
)
868 return 0; /* Didn't find target_ops, quit now */
870 /* NOTE: cagney/2003-12-06: In '94 the close call was made
871 unconditional by moving it to before the above check that the
872 target was in the target stack (something about "Change the way
873 pushing and popping of targets work to support target overlays
874 and inheritance"). This doesn't make much sense - only open
875 targets should be closed. */
878 /* Unchain the target */
880 (*cur
) = (*cur
)->beneath
;
883 update_current_target ();
891 target_close (target_stack
, 0); /* Let it clean up */
892 if (unpush_target (target_stack
) == 1)
895 fprintf_unfiltered (gdb_stderr
,
896 "pop_target couldn't find target %s\n",
897 current_target
.to_shortname
);
898 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
902 pop_all_targets_above (enum strata above_stratum
, int quitting
)
904 while ((int) (current_target
.to_stratum
) > (int) above_stratum
)
906 target_close (target_stack
, quitting
);
907 if (!unpush_target (target_stack
))
909 fprintf_unfiltered (gdb_stderr
,
910 "pop_all_targets couldn't find target %s\n",
911 target_stack
->to_shortname
);
912 internal_error (__FILE__
, __LINE__
,
913 _("failed internal consistency check"));
920 pop_all_targets (int quitting
)
922 pop_all_targets_above (dummy_stratum
, quitting
);
925 /* Using the objfile specified in OBJFILE, find the address for the
926 current thread's thread-local storage with offset OFFSET. */
928 target_translate_tls_address (struct objfile
*objfile
, CORE_ADDR offset
)
930 volatile CORE_ADDR addr
= 0;
931 struct target_ops
*target
;
933 for (target
= current_target
.beneath
;
935 target
= target
->beneath
)
937 if (target
->to_get_thread_local_address
!= NULL
)
942 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch
))
944 ptid_t ptid
= inferior_ptid
;
945 volatile struct gdb_exception ex
;
947 TRY_CATCH (ex
, RETURN_MASK_ALL
)
951 /* Fetch the load module address for this objfile. */
952 lm_addr
= gdbarch_fetch_tls_load_module_address (target_gdbarch
,
954 /* If it's 0, throw the appropriate exception. */
956 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR
,
957 _("TLS load module not found"));
959 addr
= target
->to_get_thread_local_address (target
, ptid
, lm_addr
, offset
);
961 /* If an error occurred, print TLS related messages here. Otherwise,
962 throw the error to some higher catcher. */
965 int objfile_is_library
= (objfile
->flags
& OBJF_SHARED
);
969 case TLS_NO_LIBRARY_SUPPORT_ERROR
:
970 error (_("Cannot find thread-local variables in this thread library."));
972 case TLS_LOAD_MODULE_NOT_FOUND_ERROR
:
973 if (objfile_is_library
)
974 error (_("Cannot find shared library `%s' in dynamic"
975 " linker's load module list"), objfile
->name
);
977 error (_("Cannot find executable file `%s' in dynamic"
978 " linker's load module list"), objfile
->name
);
980 case TLS_NOT_ALLOCATED_YET_ERROR
:
981 if (objfile_is_library
)
982 error (_("The inferior has not yet allocated storage for"
983 " thread-local variables in\n"
984 "the shared library `%s'\n"
986 objfile
->name
, target_pid_to_str (ptid
));
988 error (_("The inferior has not yet allocated storage for"
989 " thread-local variables in\n"
990 "the executable `%s'\n"
992 objfile
->name
, target_pid_to_str (ptid
));
994 case TLS_GENERIC_ERROR
:
995 if (objfile_is_library
)
996 error (_("Cannot find thread-local storage for %s, "
997 "shared library %s:\n%s"),
998 target_pid_to_str (ptid
),
999 objfile
->name
, ex
.message
);
1001 error (_("Cannot find thread-local storage for %s, "
1002 "executable file %s:\n%s"),
1003 target_pid_to_str (ptid
),
1004 objfile
->name
, ex
.message
);
1007 throw_exception (ex
);
1012 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1013 TLS is an ABI-specific thing. But we don't do that yet. */
1015 error (_("Cannot find thread-local variables on this target"));
1021 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1023 /* target_read_string -- read a null terminated string, up to LEN bytes,
1024 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1025 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1026 is responsible for freeing it. Return the number of bytes successfully
1030 target_read_string (CORE_ADDR memaddr
, char **string
, int len
, int *errnop
)
1032 int tlen
, origlen
, offset
, i
;
1036 int buffer_allocated
;
1038 unsigned int nbytes_read
= 0;
1040 gdb_assert (string
);
1042 /* Small for testing. */
1043 buffer_allocated
= 4;
1044 buffer
= xmalloc (buffer_allocated
);
1051 tlen
= MIN (len
, 4 - (memaddr
& 3));
1052 offset
= memaddr
& 3;
1054 errcode
= target_read_memory (memaddr
& ~3, buf
, sizeof buf
);
1057 /* The transfer request might have crossed the boundary to an
1058 unallocated region of memory. Retry the transfer, requesting
1062 errcode
= target_read_memory (memaddr
, buf
, 1);
1067 if (bufptr
- buffer
+ tlen
> buffer_allocated
)
1070 bytes
= bufptr
- buffer
;
1071 buffer_allocated
*= 2;
1072 buffer
= xrealloc (buffer
, buffer_allocated
);
1073 bufptr
= buffer
+ bytes
;
1076 for (i
= 0; i
< tlen
; i
++)
1078 *bufptr
++ = buf
[i
+ offset
];
1079 if (buf
[i
+ offset
] == '\000')
1081 nbytes_read
+= i
+ 1;
1088 nbytes_read
+= tlen
;
1097 struct target_section_table
*
1098 target_get_section_table (struct target_ops
*target
)
1100 struct target_ops
*t
;
1103 fprintf_unfiltered (gdb_stdlog
, "target_get_section_table ()\n");
1105 for (t
= target
; t
!= NULL
; t
= t
->beneath
)
1106 if (t
->to_get_section_table
!= NULL
)
1107 return (*t
->to_get_section_table
) (t
);
1112 /* Find a section containing ADDR. */
1114 struct target_section
*
1115 target_section_by_addr (struct target_ops
*target
, CORE_ADDR addr
)
1117 struct target_section_table
*table
= target_get_section_table (target
);
1118 struct target_section
*secp
;
1123 for (secp
= table
->sections
; secp
< table
->sections_end
; secp
++)
1125 if (addr
>= secp
->addr
&& addr
< secp
->endaddr
)
1131 /* Perform a partial memory transfer. The arguments and return
1132 value are just as for target_xfer_partial. */
1135 memory_xfer_partial (struct target_ops
*ops
, void *readbuf
, const void *writebuf
,
1136 ULONGEST memaddr
, LONGEST len
)
1140 struct mem_region
*region
;
1142 /* Zero length requests are ok and require no work. */
1146 /* For accesses to unmapped overlay sections, read directly from
1147 files. Must do this first, as MEMADDR may need adjustment. */
1148 if (readbuf
!= NULL
&& overlay_debugging
)
1150 struct obj_section
*section
= find_pc_overlay (memaddr
);
1151 if (pc_in_unmapped_range (memaddr
, section
))
1153 struct target_section_table
*table
1154 = target_get_section_table (ops
);
1155 const char *section_name
= section
->the_bfd_section
->name
;
1156 memaddr
= overlay_mapped_address (memaddr
, section
);
1157 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1160 table
->sections_end
,
1165 /* Try the executable files, if "trust-readonly-sections" is set. */
1166 if (readbuf
!= NULL
&& trust_readonly
)
1168 struct target_section
*secp
;
1169 struct target_section_table
*table
;
1171 secp
= target_section_by_addr (ops
, memaddr
);
1173 && (bfd_get_section_flags (secp
->bfd
, secp
->the_bfd_section
)
1176 table
= target_get_section_table (ops
);
1177 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1180 table
->sections_end
,
1185 /* Try GDB's internal data cache. */
1186 region
= lookup_mem_region (memaddr
);
1187 /* region->hi == 0 means there's no upper bound. */
1188 if (memaddr
+ len
< region
->hi
|| region
->hi
== 0)
1191 reg_len
= region
->hi
- memaddr
;
1193 switch (region
->attrib
.mode
)
1196 if (writebuf
!= NULL
)
1201 if (readbuf
!= NULL
)
1206 /* We only support writing to flash during "load" for now. */
1207 if (writebuf
!= NULL
)
1208 error (_("Writing to flash memory forbidden in this context"));
1215 if (region
->attrib
.cache
)
1217 /* FIXME drow/2006-08-09: This call discards OPS, so the raw
1218 memory request will start back at current_target. */
1219 if (readbuf
!= NULL
)
1220 res
= dcache_xfer_memory (target_dcache
, memaddr
, readbuf
,
1223 /* FIXME drow/2006-08-09: If we're going to preserve const
1224 correctness dcache_xfer_memory should take readbuf and
1226 res
= dcache_xfer_memory (target_dcache
, memaddr
,
1233 if (readbuf
&& !show_memory_breakpoints
)
1234 breakpoint_restore_shadows (readbuf
, memaddr
, reg_len
);
1239 /* If none of those methods found the memory we wanted, fall back
1240 to a target partial transfer. Normally a single call to
1241 to_xfer_partial is enough; if it doesn't recognize an object
1242 it will call the to_xfer_partial of the next target down.
1243 But for memory this won't do. Memory is the only target
1244 object which can be read from more than one valid target.
1245 A core file, for instance, could have some of memory but
1246 delegate other bits to the target below it. So, we must
1247 manually try all targets. */
1251 res
= ops
->to_xfer_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
1252 readbuf
, writebuf
, memaddr
, reg_len
);
1256 /* We want to continue past core files to executables, but not
1257 past a running target's memory. */
1258 if (ops
->to_has_all_memory (ops
))
1263 while (ops
!= NULL
);
1265 if (readbuf
&& !show_memory_breakpoints
)
1266 breakpoint_restore_shadows (readbuf
, memaddr
, reg_len
);
1268 /* If we still haven't got anything, return the last error. We
1274 restore_show_memory_breakpoints (void *arg
)
1276 show_memory_breakpoints
= (uintptr_t) arg
;
1280 make_show_memory_breakpoints_cleanup (int show
)
1282 int current
= show_memory_breakpoints
;
1283 show_memory_breakpoints
= show
;
1285 return make_cleanup (restore_show_memory_breakpoints
,
1286 (void *) (uintptr_t) current
);
1290 target_xfer_partial (struct target_ops
*ops
,
1291 enum target_object object
, const char *annex
,
1292 void *readbuf
, const void *writebuf
,
1293 ULONGEST offset
, LONGEST len
)
1297 gdb_assert (ops
->to_xfer_partial
!= NULL
);
1299 /* If this is a memory transfer, let the memory-specific code
1300 have a look at it instead. Memory transfers are more
1302 if (object
== TARGET_OBJECT_MEMORY
)
1303 retval
= memory_xfer_partial (ops
, readbuf
, writebuf
, offset
, len
);
1306 enum target_object raw_object
= object
;
1308 /* If this is a raw memory transfer, request the normal
1309 memory object from other layers. */
1310 if (raw_object
== TARGET_OBJECT_RAW_MEMORY
)
1311 raw_object
= TARGET_OBJECT_MEMORY
;
1313 retval
= ops
->to_xfer_partial (ops
, raw_object
, annex
, readbuf
,
1314 writebuf
, offset
, len
);
1319 const unsigned char *myaddr
= NULL
;
1321 fprintf_unfiltered (gdb_stdlog
,
1322 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1325 (annex
? annex
: "(null)"),
1326 host_address_to_string (readbuf
),
1327 host_address_to_string (writebuf
),
1328 core_addr_to_string_nz (offset
),
1329 plongest (len
), plongest (retval
));
1335 if (retval
> 0 && myaddr
!= NULL
)
1339 fputs_unfiltered (", bytes =", gdb_stdlog
);
1340 for (i
= 0; i
< retval
; i
++)
1342 if ((((intptr_t) &(myaddr
[i
])) & 0xf) == 0)
1344 if (targetdebug
< 2 && i
> 0)
1346 fprintf_unfiltered (gdb_stdlog
, " ...");
1349 fprintf_unfiltered (gdb_stdlog
, "\n");
1352 fprintf_unfiltered (gdb_stdlog
, " %02x", myaddr
[i
] & 0xff);
1356 fputc_unfiltered ('\n', gdb_stdlog
);
1361 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1362 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1363 if any error occurs.
1365 If an error occurs, no guarantee is made about the contents of the data at
1366 MYADDR. In particular, the caller should not depend upon partial reads
1367 filling the buffer with good data. There is no way for the caller to know
1368 how much good data might have been transfered anyway. Callers that can
1369 deal with partial reads should call target_read (which will retry until
1370 it makes no progress, and then return how much was transferred). */
1373 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
1375 /* Dispatch to the topmost target, not the flattened current_target.
1376 Memory accesses check target->to_has_(all_)memory, and the
1377 flattened target doesn't inherit those. */
1378 if (target_read (current_target
.beneath
, TARGET_OBJECT_MEMORY
, NULL
,
1379 myaddr
, memaddr
, len
) == len
)
1386 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
1388 /* Dispatch to the topmost target, not the flattened current_target.
1389 Memory accesses check target->to_has_(all_)memory, and the
1390 flattened target doesn't inherit those. */
1391 if (target_write (current_target
.beneath
, TARGET_OBJECT_MEMORY
, NULL
,
1392 myaddr
, memaddr
, len
) == len
)
1398 /* Fetch the target's memory map. */
1401 target_memory_map (void)
1403 VEC(mem_region_s
) *result
;
1404 struct mem_region
*last_one
, *this_one
;
1406 struct target_ops
*t
;
1409 fprintf_unfiltered (gdb_stdlog
, "target_memory_map ()\n");
1411 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1412 if (t
->to_memory_map
!= NULL
)
1418 result
= t
->to_memory_map (t
);
1422 qsort (VEC_address (mem_region_s
, result
),
1423 VEC_length (mem_region_s
, result
),
1424 sizeof (struct mem_region
), mem_region_cmp
);
1426 /* Check that regions do not overlap. Simultaneously assign
1427 a numbering for the "mem" commands to use to refer to
1430 for (ix
= 0; VEC_iterate (mem_region_s
, result
, ix
, this_one
); ix
++)
1432 this_one
->number
= ix
;
1434 if (last_one
&& last_one
->hi
> this_one
->lo
)
1436 warning (_("Overlapping regions in memory map: ignoring"));
1437 VEC_free (mem_region_s
, result
);
1440 last_one
= this_one
;
1447 target_flash_erase (ULONGEST address
, LONGEST length
)
1449 struct target_ops
*t
;
1451 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1452 if (t
->to_flash_erase
!= NULL
)
1455 fprintf_unfiltered (gdb_stdlog
, "target_flash_erase (%s, %s)\n",
1456 paddr (address
), phex (length
, 0));
1457 t
->to_flash_erase (t
, address
, length
);
1465 target_flash_done (void)
1467 struct target_ops
*t
;
1469 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1470 if (t
->to_flash_done
!= NULL
)
1473 fprintf_unfiltered (gdb_stdlog
, "target_flash_done\n");
1474 t
->to_flash_done (t
);
1482 show_trust_readonly (struct ui_file
*file
, int from_tty
,
1483 struct cmd_list_element
*c
, const char *value
)
1485 fprintf_filtered (file
, _("\
1486 Mode for reading from readonly sections is %s.\n"),
1490 /* More generic transfers. */
1493 default_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1494 const char *annex
, gdb_byte
*readbuf
,
1495 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1497 if (object
== TARGET_OBJECT_MEMORY
1498 && ops
->deprecated_xfer_memory
!= NULL
)
1499 /* If available, fall back to the target's
1500 "deprecated_xfer_memory" method. */
1504 if (writebuf
!= NULL
)
1506 void *buffer
= xmalloc (len
);
1507 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
1508 memcpy (buffer
, writebuf
, len
);
1509 xfered
= ops
->deprecated_xfer_memory (offset
, buffer
, len
,
1510 1/*write*/, NULL
, ops
);
1511 do_cleanups (cleanup
);
1513 if (readbuf
!= NULL
)
1514 xfered
= ops
->deprecated_xfer_memory (offset
, readbuf
, len
,
1515 0/*read*/, NULL
, ops
);
1518 else if (xfered
== 0 && errno
== 0)
1519 /* "deprecated_xfer_memory" uses 0, cross checked against
1520 ERRNO as one indication of an error. */
1525 else if (ops
->beneath
!= NULL
)
1526 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
1527 readbuf
, writebuf
, offset
, len
);
1532 /* The xfer_partial handler for the topmost target. Unlike the default,
1533 it does not need to handle memory specially; it just passes all
1534 requests down the stack. */
1537 current_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1538 const char *annex
, gdb_byte
*readbuf
,
1539 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1541 if (ops
->beneath
!= NULL
)
1542 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
1543 readbuf
, writebuf
, offset
, len
);
1548 /* Target vector read/write partial wrapper functions.
1550 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1551 (inbuf, outbuf)", instead of separate read/write methods, make life
1555 target_read_partial (struct target_ops
*ops
,
1556 enum target_object object
,
1557 const char *annex
, gdb_byte
*buf
,
1558 ULONGEST offset
, LONGEST len
)
1560 return target_xfer_partial (ops
, object
, annex
, buf
, NULL
, offset
, len
);
1564 target_write_partial (struct target_ops
*ops
,
1565 enum target_object object
,
1566 const char *annex
, const gdb_byte
*buf
,
1567 ULONGEST offset
, LONGEST len
)
1569 return target_xfer_partial (ops
, object
, annex
, NULL
, buf
, offset
, len
);
1572 /* Wrappers to perform the full transfer. */
1574 target_read (struct target_ops
*ops
,
1575 enum target_object object
,
1576 const char *annex
, gdb_byte
*buf
,
1577 ULONGEST offset
, LONGEST len
)
1580 while (xfered
< len
)
1582 LONGEST xfer
= target_read_partial (ops
, object
, annex
,
1583 (gdb_byte
*) buf
+ xfered
,
1584 offset
+ xfered
, len
- xfered
);
1585 /* Call an observer, notifying them of the xfer progress? */
1597 target_read_until_error (struct target_ops
*ops
,
1598 enum target_object object
,
1599 const char *annex
, gdb_byte
*buf
,
1600 ULONGEST offset
, LONGEST len
)
1603 while (xfered
< len
)
1605 LONGEST xfer
= target_read_partial (ops
, object
, annex
,
1606 (gdb_byte
*) buf
+ xfered
,
1607 offset
+ xfered
, len
- xfered
);
1608 /* Call an observer, notifying them of the xfer progress? */
1613 /* We've got an error. Try to read in smaller blocks. */
1614 ULONGEST start
= offset
+ xfered
;
1615 ULONGEST remaining
= len
- xfered
;
1618 /* If an attempt was made to read a random memory address,
1619 it's likely that the very first byte is not accessible.
1620 Try reading the first byte, to avoid doing log N tries
1622 xfer
= target_read_partial (ops
, object
, annex
,
1623 (gdb_byte
*) buf
+ xfered
, start
, 1);
1632 xfer
= target_read_partial (ops
, object
, annex
,
1633 (gdb_byte
*) buf
+ xfered
,
1643 /* We have successfully read the first half. So, the
1644 error must be in the second half. Adjust start and
1645 remaining to point at the second half. */
1662 /* An alternative to target_write with progress callbacks. */
1665 target_write_with_progress (struct target_ops
*ops
,
1666 enum target_object object
,
1667 const char *annex
, const gdb_byte
*buf
,
1668 ULONGEST offset
, LONGEST len
,
1669 void (*progress
) (ULONGEST
, void *), void *baton
)
1673 /* Give the progress callback a chance to set up. */
1675 (*progress
) (0, baton
);
1677 while (xfered
< len
)
1679 LONGEST xfer
= target_write_partial (ops
, object
, annex
,
1680 (gdb_byte
*) buf
+ xfered
,
1681 offset
+ xfered
, len
- xfered
);
1689 (*progress
) (xfer
, baton
);
1698 target_write (struct target_ops
*ops
,
1699 enum target_object object
,
1700 const char *annex
, const gdb_byte
*buf
,
1701 ULONGEST offset
, LONGEST len
)
1703 return target_write_with_progress (ops
, object
, annex
, buf
, offset
, len
,
1707 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1708 the size of the transferred data. PADDING additional bytes are
1709 available in *BUF_P. This is a helper function for
1710 target_read_alloc; see the declaration of that function for more
1714 target_read_alloc_1 (struct target_ops
*ops
, enum target_object object
,
1715 const char *annex
, gdb_byte
**buf_p
, int padding
)
1717 size_t buf_alloc
, buf_pos
;
1721 /* This function does not have a length parameter; it reads the
1722 entire OBJECT). Also, it doesn't support objects fetched partly
1723 from one target and partly from another (in a different stratum,
1724 e.g. a core file and an executable). Both reasons make it
1725 unsuitable for reading memory. */
1726 gdb_assert (object
!= TARGET_OBJECT_MEMORY
);
1728 /* Start by reading up to 4K at a time. The target will throttle
1729 this number down if necessary. */
1731 buf
= xmalloc (buf_alloc
);
1735 n
= target_read_partial (ops
, object
, annex
, &buf
[buf_pos
],
1736 buf_pos
, buf_alloc
- buf_pos
- padding
);
1739 /* An error occurred. */
1745 /* Read all there was. */
1755 /* If the buffer is filling up, expand it. */
1756 if (buf_alloc
< buf_pos
* 2)
1759 buf
= xrealloc (buf
, buf_alloc
);
1766 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1767 the size of the transferred data. See the declaration in "target.h"
1768 function for more information about the return value. */
1771 target_read_alloc (struct target_ops
*ops
, enum target_object object
,
1772 const char *annex
, gdb_byte
**buf_p
)
1774 return target_read_alloc_1 (ops
, object
, annex
, buf_p
, 0);
1777 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1778 returned as a string, allocated using xmalloc. If an error occurs
1779 or the transfer is unsupported, NULL is returned. Empty objects
1780 are returned as allocated but empty strings. A warning is issued
1781 if the result contains any embedded NUL bytes. */
1784 target_read_stralloc (struct target_ops
*ops
, enum target_object object
,
1788 LONGEST transferred
;
1790 transferred
= target_read_alloc_1 (ops
, object
, annex
, &buffer
, 1);
1792 if (transferred
< 0)
1795 if (transferred
== 0)
1796 return xstrdup ("");
1798 buffer
[transferred
] = 0;
1799 if (strlen (buffer
) < transferred
)
1800 warning (_("target object %d, annex %s, "
1801 "contained unexpected null characters"),
1802 (int) object
, annex
? annex
: "(none)");
1804 return (char *) buffer
;
1807 /* Memory transfer methods. */
1810 get_target_memory (struct target_ops
*ops
, CORE_ADDR addr
, gdb_byte
*buf
,
1813 /* This method is used to read from an alternate, non-current
1814 target. This read must bypass the overlay support (as symbols
1815 don't match this target), and GDB's internal cache (wrong cache
1816 for this target). */
1817 if (target_read (ops
, TARGET_OBJECT_RAW_MEMORY
, NULL
, buf
, addr
, len
)
1819 memory_error (EIO
, addr
);
1823 get_target_memory_unsigned (struct target_ops
*ops
,
1824 CORE_ADDR addr
, int len
)
1826 gdb_byte buf
[sizeof (ULONGEST
)];
1828 gdb_assert (len
<= sizeof (buf
));
1829 get_target_memory (ops
, addr
, buf
, len
);
1830 return extract_unsigned_integer (buf
, len
);
1834 target_info (char *args
, int from_tty
)
1836 struct target_ops
*t
;
1837 int has_all_mem
= 0;
1839 if (symfile_objfile
!= NULL
)
1840 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile
->name
);
1842 for (t
= target_stack
; t
!= NULL
; t
= t
->beneath
)
1844 if (!(*t
->to_has_memory
) (t
))
1847 if ((int) (t
->to_stratum
) <= (int) dummy_stratum
)
1850 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1851 printf_unfiltered ("%s:\n", t
->to_longname
);
1852 (t
->to_files_info
) (t
);
1853 has_all_mem
= (*t
->to_has_all_memory
) (t
);
1857 /* This function is called before any new inferior is created, e.g.
1858 by running a program, attaching, or connecting to a target.
1859 It cleans up any state from previous invocations which might
1860 change between runs. This is a subset of what target_preopen
1861 resets (things which might change between targets). */
1864 target_pre_inferior (int from_tty
)
1866 /* Clear out solib state. Otherwise the solib state of the previous
1867 inferior might have survived and is entirely wrong for the new
1868 target. This has been observed on GNU/Linux using glibc 2.3. How
1880 Cannot access memory at address 0xdeadbeef
1883 /* In some OSs, the shared library list is the same/global/shared
1884 across inferiors. If code is shared between processes, so are
1885 memory regions and features. */
1886 if (!gdbarch_has_global_solist (target_gdbarch
))
1888 no_shared_libraries (NULL
, from_tty
);
1890 invalidate_target_mem_regions ();
1892 target_clear_description ();
1896 /* This is to be called by the open routine before it does
1900 target_preopen (int from_tty
)
1904 if (target_has_execution
)
1907 || query (_("A program is being debugged already. Kill it? ")))
1910 error (_("Program not killed."));
1913 /* Calling target_kill may remove the target from the stack. But if
1914 it doesn't (which seems like a win for UDI), remove it now. */
1915 /* Leave the exec target, though. The user may be switching from a
1916 live process to a core of the same program. */
1917 pop_all_targets_above (file_stratum
, 0);
1919 target_pre_inferior (from_tty
);
1922 /* Detach a target after doing deferred register stores. */
1925 target_detach (char *args
, int from_tty
)
1927 struct target_ops
* t
;
1929 if (gdbarch_has_global_breakpoints (target_gdbarch
))
1930 /* Don't remove global breakpoints here. They're removed on
1931 disconnection from the target. */
1934 /* If we're in breakpoints-always-inserted mode, have to remove
1935 them before detaching. */
1936 remove_breakpoints ();
1938 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1940 if (t
->to_detach
!= NULL
)
1942 t
->to_detach (t
, args
, from_tty
);
1944 fprintf_unfiltered (gdb_stdlog
, "target_detach (%s, %d)\n",
1950 internal_error (__FILE__
, __LINE__
, "could not find a target to detach");
1954 target_disconnect (char *args
, int from_tty
)
1956 struct target_ops
*t
;
1958 /* If we're in breakpoints-always-inserted mode or if breakpoints
1959 are global across processes, we have to remove them before
1961 remove_breakpoints ();
1963 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1964 if (t
->to_disconnect
!= NULL
)
1967 fprintf_unfiltered (gdb_stdlog
, "target_disconnect (%s, %d)\n",
1969 t
->to_disconnect (t
, args
, from_tty
);
1977 target_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1979 struct target_ops
*t
;
1981 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
1983 if (t
->to_wait
!= NULL
)
1985 ptid_t retval
= (*t
->to_wait
) (t
, ptid
, status
, options
);
1989 char *status_string
;
1991 status_string
= target_waitstatus_to_string (status
);
1992 fprintf_unfiltered (gdb_stdlog
,
1993 "target_wait (%d, status) = %d, %s\n",
1994 PIDGET (ptid
), PIDGET (retval
),
1996 xfree (status_string
);
2007 target_pid_to_str (ptid_t ptid
)
2009 struct target_ops
*t
;
2011 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2013 if (t
->to_pid_to_str
!= NULL
)
2014 return (*t
->to_pid_to_str
) (t
, ptid
);
2017 return normal_pid_to_str (ptid
);
2021 target_resume (ptid_t ptid
, int step
, enum target_signal signal
)
2023 struct target_ops
*t
;
2025 dcache_invalidate (target_dcache
);
2027 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2029 if (t
->to_resume
!= NULL
)
2031 t
->to_resume (t
, ptid
, step
, signal
);
2033 fprintf_unfiltered (gdb_stdlog
, "target_resume (%d, %s, %s)\n",
2035 step
? "step" : "continue",
2036 target_signal_to_name (signal
));
2038 set_executing (ptid
, 1);
2039 set_running (ptid
, 1);
2046 /* Look through the list of possible targets for a target that can
2050 target_follow_fork (int follow_child
)
2052 struct target_ops
*t
;
2054 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2056 if (t
->to_follow_fork
!= NULL
)
2058 int retval
= t
->to_follow_fork (t
, follow_child
);
2060 fprintf_unfiltered (gdb_stdlog
, "target_follow_fork (%d) = %d\n",
2061 follow_child
, retval
);
2066 /* Some target returned a fork event, but did not know how to follow it. */
2067 internal_error (__FILE__
, __LINE__
,
2068 "could not find a target to follow fork");
2072 target_mourn_inferior (void)
2074 struct target_ops
*t
;
2075 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2077 if (t
->to_mourn_inferior
!= NULL
)
2079 t
->to_mourn_inferior (t
);
2081 fprintf_unfiltered (gdb_stdlog
, "target_mourn_inferior ()\n");
2083 /* We no longer need to keep handles on any of the object files.
2084 Make sure to release them to avoid unnecessarily locking any
2085 of them while we're not actually debugging. */
2086 bfd_cache_close_all ();
2092 internal_error (__FILE__
, __LINE__
,
2093 "could not find a target to follow mourn inferiour");
2096 /* Look for a target which can describe architectural features, starting
2097 from TARGET. If we find one, return its description. */
2099 const struct target_desc
*
2100 target_read_description (struct target_ops
*target
)
2102 struct target_ops
*t
;
2104 for (t
= target
; t
!= NULL
; t
= t
->beneath
)
2105 if (t
->to_read_description
!= NULL
)
2107 const struct target_desc
*tdesc
;
2109 tdesc
= t
->to_read_description (t
);
2117 /* The default implementation of to_search_memory.
2118 This implements a basic search of memory, reading target memory and
2119 performing the search here (as opposed to performing the search in on the
2120 target side with, for example, gdbserver). */
2123 simple_search_memory (struct target_ops
*ops
,
2124 CORE_ADDR start_addr
, ULONGEST search_space_len
,
2125 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2126 CORE_ADDR
*found_addrp
)
2128 /* NOTE: also defined in find.c testcase. */
2129 #define SEARCH_CHUNK_SIZE 16000
2130 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
2131 /* Buffer to hold memory contents for searching. */
2132 gdb_byte
*search_buf
;
2133 unsigned search_buf_size
;
2134 struct cleanup
*old_cleanups
;
2136 search_buf_size
= chunk_size
+ pattern_len
- 1;
2138 /* No point in trying to allocate a buffer larger than the search space. */
2139 if (search_space_len
< search_buf_size
)
2140 search_buf_size
= search_space_len
;
2142 search_buf
= malloc (search_buf_size
);
2143 if (search_buf
== NULL
)
2144 error (_("Unable to allocate memory to perform the search."));
2145 old_cleanups
= make_cleanup (free_current_contents
, &search_buf
);
2147 /* Prime the search buffer. */
2149 if (target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2150 search_buf
, start_addr
, search_buf_size
) != search_buf_size
)
2152 warning (_("Unable to access target memory at %s, halting search."),
2153 hex_string (start_addr
));
2154 do_cleanups (old_cleanups
);
2158 /* Perform the search.
2160 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2161 When we've scanned N bytes we copy the trailing bytes to the start and
2162 read in another N bytes. */
2164 while (search_space_len
>= pattern_len
)
2166 gdb_byte
*found_ptr
;
2167 unsigned nr_search_bytes
= min (search_space_len
, search_buf_size
);
2169 found_ptr
= memmem (search_buf
, nr_search_bytes
,
2170 pattern
, pattern_len
);
2172 if (found_ptr
!= NULL
)
2174 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
2175 *found_addrp
= found_addr
;
2176 do_cleanups (old_cleanups
);
2180 /* Not found in this chunk, skip to next chunk. */
2182 /* Don't let search_space_len wrap here, it's unsigned. */
2183 if (search_space_len
>= chunk_size
)
2184 search_space_len
-= chunk_size
;
2186 search_space_len
= 0;
2188 if (search_space_len
>= pattern_len
)
2190 unsigned keep_len
= search_buf_size
- chunk_size
;
2191 CORE_ADDR read_addr
= start_addr
+ keep_len
;
2194 /* Copy the trailing part of the previous iteration to the front
2195 of the buffer for the next iteration. */
2196 gdb_assert (keep_len
== pattern_len
- 1);
2197 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
2199 nr_to_read
= min (search_space_len
- keep_len
, chunk_size
);
2201 if (target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2202 search_buf
+ keep_len
, read_addr
,
2203 nr_to_read
) != nr_to_read
)
2205 warning (_("Unable to access target memory at %s, halting search."),
2206 hex_string (read_addr
));
2207 do_cleanups (old_cleanups
);
2211 start_addr
+= chunk_size
;
2217 do_cleanups (old_cleanups
);
2221 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2222 sequence of bytes in PATTERN with length PATTERN_LEN.
2224 The result is 1 if found, 0 if not found, and -1 if there was an error
2225 requiring halting of the search (e.g. memory read error).
2226 If the pattern is found the address is recorded in FOUND_ADDRP. */
2229 target_search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
2230 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2231 CORE_ADDR
*found_addrp
)
2233 struct target_ops
*t
;
2236 /* We don't use INHERIT to set current_target.to_search_memory,
2237 so we have to scan the target stack and handle targetdebug
2241 fprintf_unfiltered (gdb_stdlog
, "target_search_memory (%s, ...)\n",
2242 hex_string (start_addr
));
2244 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2245 if (t
->to_search_memory
!= NULL
)
2250 found
= t
->to_search_memory (t
, start_addr
, search_space_len
,
2251 pattern
, pattern_len
, found_addrp
);
2255 /* If a special version of to_search_memory isn't available, use the
2257 found
= simple_search_memory (current_target
.beneath
,
2258 start_addr
, search_space_len
,
2259 pattern
, pattern_len
, found_addrp
);
2263 fprintf_unfiltered (gdb_stdlog
, " = %d\n", found
);
2268 /* Look through the currently pushed targets. If none of them will
2269 be able to restart the currently running process, issue an error
2273 target_require_runnable (void)
2275 struct target_ops
*t
;
2277 for (t
= target_stack
; t
!= NULL
; t
= t
->beneath
)
2279 /* If this target knows how to create a new program, then
2280 assume we will still be able to after killing the current
2281 one. Either killing and mourning will not pop T, or else
2282 find_default_run_target will find it again. */
2283 if (t
->to_create_inferior
!= NULL
)
2286 /* Do not worry about thread_stratum targets that can not
2287 create inferiors. Assume they will be pushed again if
2288 necessary, and continue to the process_stratum. */
2289 if (t
->to_stratum
== thread_stratum
)
2293 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2297 /* This function is only called if the target is running. In that
2298 case there should have been a process_stratum target and it
2299 should either know how to create inferiors, or not... */
2300 internal_error (__FILE__
, __LINE__
, "No targets found");
2303 /* Look through the list of possible targets for a target that can
2304 execute a run or attach command without any other data. This is
2305 used to locate the default process stratum.
2307 If DO_MESG is not NULL, the result is always valid (error() is
2308 called for errors); else, return NULL on error. */
2310 static struct target_ops
*
2311 find_default_run_target (char *do_mesg
)
2313 struct target_ops
**t
;
2314 struct target_ops
*runable
= NULL
;
2319 for (t
= target_structs
; t
< target_structs
+ target_struct_size
;
2322 if ((*t
)->to_can_run
&& target_can_run (*t
))
2332 error (_("Don't know how to %s. Try \"help target\"."), do_mesg
);
2341 find_default_attach (struct target_ops
*ops
, char *args
, int from_tty
)
2343 struct target_ops
*t
;
2345 t
= find_default_run_target ("attach");
2346 (t
->to_attach
) (t
, args
, from_tty
);
2351 find_default_create_inferior (struct target_ops
*ops
,
2352 char *exec_file
, char *allargs
, char **env
,
2355 struct target_ops
*t
;
2357 t
= find_default_run_target ("run");
2358 (t
->to_create_inferior
) (t
, exec_file
, allargs
, env
, from_tty
);
2363 find_default_can_async_p (void)
2365 struct target_ops
*t
;
2367 /* This may be called before the target is pushed on the stack;
2368 look for the default process stratum. If there's none, gdb isn't
2369 configured with a native debugger, and target remote isn't
2371 t
= find_default_run_target (NULL
);
2372 if (t
&& t
->to_can_async_p
)
2373 return (t
->to_can_async_p
) ();
2378 find_default_is_async_p (void)
2380 struct target_ops
*t
;
2382 /* This may be called before the target is pushed on the stack;
2383 look for the default process stratum. If there's none, gdb isn't
2384 configured with a native debugger, and target remote isn't
2386 t
= find_default_run_target (NULL
);
2387 if (t
&& t
->to_is_async_p
)
2388 return (t
->to_is_async_p
) ();
2393 find_default_supports_non_stop (void)
2395 struct target_ops
*t
;
2397 t
= find_default_run_target (NULL
);
2398 if (t
&& t
->to_supports_non_stop
)
2399 return (t
->to_supports_non_stop
) ();
2404 target_supports_non_stop (void)
2406 struct target_ops
*t
;
2407 for (t
= ¤t_target
; t
!= NULL
; t
= t
->beneath
)
2408 if (t
->to_supports_non_stop
)
2409 return t
->to_supports_non_stop ();
2416 target_get_osdata (const char *type
)
2419 struct target_ops
*t
;
2421 /* If we're already connected to something that can get us OS
2422 related data, use it. Otherwise, try using the native
2424 if (current_target
.to_stratum
>= process_stratum
)
2425 t
= current_target
.beneath
;
2427 t
= find_default_run_target ("get OS data");
2432 return target_read_stralloc (t
, TARGET_OBJECT_OSDATA
, type
);
2436 default_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
2438 return (len
<= gdbarch_ptr_bit (target_gdbarch
) / TARGET_CHAR_BIT
);
2442 default_watchpoint_addr_within_range (struct target_ops
*target
,
2444 CORE_ADDR start
, int length
)
2446 return addr
>= start
&& addr
< start
+ length
;
2462 return_minus_one (void)
2467 /* Find a single runnable target in the stack and return it. If for
2468 some reason there is more than one, return NULL. */
2471 find_run_target (void)
2473 struct target_ops
**t
;
2474 struct target_ops
*runable
= NULL
;
2479 for (t
= target_structs
; t
< target_structs
+ target_struct_size
; ++t
)
2481 if ((*t
)->to_can_run
&& target_can_run (*t
))
2488 return (count
== 1 ? runable
: NULL
);
2491 /* Find a single core_stratum target in the list of targets and return it.
2492 If for some reason there is more than one, return NULL. */
2495 find_core_target (void)
2497 struct target_ops
**t
;
2498 struct target_ops
*runable
= NULL
;
2503 for (t
= target_structs
; t
< target_structs
+ target_struct_size
;
2506 if ((*t
)->to_stratum
== core_stratum
)
2513 return (count
== 1 ? runable
: NULL
);
2517 * Find the next target down the stack from the specified target.
2521 find_target_beneath (struct target_ops
*t
)
2527 /* The inferior process has died. Long live the inferior! */
2530 generic_mourn_inferior (void)
2534 ptid
= inferior_ptid
;
2535 inferior_ptid
= null_ptid
;
2537 if (!ptid_equal (ptid
, null_ptid
))
2539 int pid
= ptid_get_pid (ptid
);
2540 delete_inferior (pid
);
2543 breakpoint_init_inferior (inf_exited
);
2544 registers_changed ();
2546 reopen_exec_file ();
2547 reinit_frame_cache ();
2549 if (deprecated_detach_hook
)
2550 deprecated_detach_hook ();
2553 /* Helper function for child_wait and the derivatives of child_wait.
2554 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2555 translation of that in OURSTATUS. */
2557 store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
2559 if (WIFEXITED (hoststatus
))
2561 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2562 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
2564 else if (!WIFSTOPPED (hoststatus
))
2566 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2567 ourstatus
->value
.sig
= target_signal_from_host (WTERMSIG (hoststatus
));
2571 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2572 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (hoststatus
));
2576 /* Convert a normal process ID to a string. Returns the string in a
2580 normal_pid_to_str (ptid_t ptid
)
2582 static char buf
[32];
2584 xsnprintf (buf
, sizeof buf
, "process %d", ptid_get_pid (ptid
));
2589 dummy_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
2591 return normal_pid_to_str (ptid
);
2594 /* Error-catcher for target_find_memory_regions */
2595 static int dummy_find_memory_regions (int (*ignore1
) (), void *ignore2
)
2597 error (_("No target."));
2601 /* Error-catcher for target_make_corefile_notes */
2602 static char * dummy_make_corefile_notes (bfd
*ignore1
, int *ignore2
)
2604 error (_("No target."));
2608 /* Set up the handful of non-empty slots needed by the dummy target
2612 init_dummy_target (void)
2614 dummy_target
.to_shortname
= "None";
2615 dummy_target
.to_longname
= "None";
2616 dummy_target
.to_doc
= "";
2617 dummy_target
.to_attach
= find_default_attach
;
2618 dummy_target
.to_detach
=
2619 (void (*)(struct target_ops
*, char *, int))target_ignore
;
2620 dummy_target
.to_create_inferior
= find_default_create_inferior
;
2621 dummy_target
.to_can_async_p
= find_default_can_async_p
;
2622 dummy_target
.to_is_async_p
= find_default_is_async_p
;
2623 dummy_target
.to_supports_non_stop
= find_default_supports_non_stop
;
2624 dummy_target
.to_pid_to_str
= dummy_pid_to_str
;
2625 dummy_target
.to_stratum
= dummy_stratum
;
2626 dummy_target
.to_find_memory_regions
= dummy_find_memory_regions
;
2627 dummy_target
.to_make_corefile_notes
= dummy_make_corefile_notes
;
2628 dummy_target
.to_xfer_partial
= default_xfer_partial
;
2629 dummy_target
.to_has_all_memory
= (int (*) (struct target_ops
*)) return_zero
;
2630 dummy_target
.to_has_memory
= (int (*) (struct target_ops
*)) return_zero
;
2631 dummy_target
.to_has_stack
= (int (*) (struct target_ops
*)) return_zero
;
2632 dummy_target
.to_has_registers
= (int (*) (struct target_ops
*)) return_zero
;
2633 dummy_target
.to_has_execution
= (int (*) (struct target_ops
*)) return_zero
;
2634 dummy_target
.to_magic
= OPS_MAGIC
;
2638 debug_to_open (char *args
, int from_tty
)
2640 debug_target
.to_open (args
, from_tty
);
2642 fprintf_unfiltered (gdb_stdlog
, "target_open (%s, %d)\n", args
, from_tty
);
2646 target_close (struct target_ops
*targ
, int quitting
)
2648 if (targ
->to_xclose
!= NULL
)
2649 targ
->to_xclose (targ
, quitting
);
2650 else if (targ
->to_close
!= NULL
)
2651 targ
->to_close (quitting
);
2654 fprintf_unfiltered (gdb_stdlog
, "target_close (%d)\n", quitting
);
2658 target_attach (char *args
, int from_tty
)
2660 struct target_ops
*t
;
2661 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2663 if (t
->to_attach
!= NULL
)
2665 t
->to_attach (t
, args
, from_tty
);
2667 fprintf_unfiltered (gdb_stdlog
, "target_attach (%s, %d)\n",
2673 internal_error (__FILE__
, __LINE__
,
2674 "could not find a target to attach");
2678 target_thread_alive (ptid_t ptid
)
2680 struct target_ops
*t
;
2681 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2683 if (t
->to_thread_alive
!= NULL
)
2687 retval
= t
->to_thread_alive (t
, ptid
);
2689 fprintf_unfiltered (gdb_stdlog
, "target_thread_alive (%d) = %d\n",
2690 PIDGET (ptid
), retval
);
2700 target_find_new_threads (void)
2702 struct target_ops
*t
;
2703 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2705 if (t
->to_find_new_threads
!= NULL
)
2707 t
->to_find_new_threads (t
);
2709 fprintf_unfiltered (gdb_stdlog
, "target_find_new_threads ()\n");
2717 debug_to_post_attach (int pid
)
2719 debug_target
.to_post_attach (pid
);
2721 fprintf_unfiltered (gdb_stdlog
, "target_post_attach (%d)\n", pid
);
2724 /* Return a pretty printed form of target_waitstatus.
2725 Space for the result is malloc'd, caller must free. */
2728 target_waitstatus_to_string (const struct target_waitstatus
*ws
)
2730 const char *kind_str
= "status->kind = ";
2734 case TARGET_WAITKIND_EXITED
:
2735 return xstrprintf ("%sexited, status = %d",
2736 kind_str
, ws
->value
.integer
);
2737 case TARGET_WAITKIND_STOPPED
:
2738 return xstrprintf ("%sstopped, signal = %s",
2739 kind_str
, target_signal_to_name (ws
->value
.sig
));
2740 case TARGET_WAITKIND_SIGNALLED
:
2741 return xstrprintf ("%ssignalled, signal = %s",
2742 kind_str
, target_signal_to_name (ws
->value
.sig
));
2743 case TARGET_WAITKIND_LOADED
:
2744 return xstrprintf ("%sloaded", kind_str
);
2745 case TARGET_WAITKIND_FORKED
:
2746 return xstrprintf ("%sforked", kind_str
);
2747 case TARGET_WAITKIND_VFORKED
:
2748 return xstrprintf ("%svforked", kind_str
);
2749 case TARGET_WAITKIND_EXECD
:
2750 return xstrprintf ("%sexecd", kind_str
);
2751 case TARGET_WAITKIND_SYSCALL_ENTRY
:
2752 return xstrprintf ("%ssyscall-entry", kind_str
);
2753 case TARGET_WAITKIND_SYSCALL_RETURN
:
2754 return xstrprintf ("%ssyscall-return", kind_str
);
2755 case TARGET_WAITKIND_SPURIOUS
:
2756 return xstrprintf ("%sspurious", kind_str
);
2757 case TARGET_WAITKIND_IGNORE
:
2758 return xstrprintf ("%signore", kind_str
);
2759 case TARGET_WAITKIND_NO_HISTORY
:
2760 return xstrprintf ("%sno-history", kind_str
);
2762 return xstrprintf ("%sunknown???", kind_str
);
2767 debug_print_register (const char * func
,
2768 struct regcache
*regcache
, int regno
)
2770 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2771 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
2772 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
2773 && gdbarch_register_name (gdbarch
, regno
) != NULL
2774 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
2775 fprintf_unfiltered (gdb_stdlog
, "(%s)",
2776 gdbarch_register_name (gdbarch
, regno
));
2778 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
2779 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
2781 int i
, size
= register_size (gdbarch
, regno
);
2782 unsigned char buf
[MAX_REGISTER_SIZE
];
2783 regcache_raw_collect (regcache
, regno
, buf
);
2784 fprintf_unfiltered (gdb_stdlog
, " = ");
2785 for (i
= 0; i
< size
; i
++)
2787 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
2789 if (size
<= sizeof (LONGEST
))
2791 ULONGEST val
= extract_unsigned_integer (buf
, size
);
2792 fprintf_unfiltered (gdb_stdlog
, " %s %s",
2793 core_addr_to_string_nz (val
), plongest (val
));
2796 fprintf_unfiltered (gdb_stdlog
, "\n");
2800 target_fetch_registers (struct regcache
*regcache
, int regno
)
2802 struct target_ops
*t
;
2803 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2805 if (t
->to_fetch_registers
!= NULL
)
2807 t
->to_fetch_registers (t
, regcache
, regno
);
2809 debug_print_register ("target_fetch_registers", regcache
, regno
);
2816 target_store_registers (struct regcache
*regcache
, int regno
)
2819 struct target_ops
*t
;
2820 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
2822 if (t
->to_store_registers
!= NULL
)
2824 t
->to_store_registers (t
, regcache
, regno
);
2827 debug_print_register ("target_store_registers", regcache
, regno
);
2837 debug_to_prepare_to_store (struct regcache
*regcache
)
2839 debug_target
.to_prepare_to_store (regcache
);
2841 fprintf_unfiltered (gdb_stdlog
, "target_prepare_to_store ()\n");
2845 deprecated_debug_xfer_memory (CORE_ADDR memaddr
, bfd_byte
*myaddr
, int len
,
2846 int write
, struct mem_attrib
*attrib
,
2847 struct target_ops
*target
)
2851 retval
= debug_target
.deprecated_xfer_memory (memaddr
, myaddr
, len
, write
,
2854 fprintf_unfiltered (gdb_stdlog
,
2855 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2856 paddress (memaddr
), len
, write
? "write" : "read",
2863 fputs_unfiltered (", bytes =", gdb_stdlog
);
2864 for (i
= 0; i
< retval
; i
++)
2866 if ((((intptr_t) &(myaddr
[i
])) & 0xf) == 0)
2868 if (targetdebug
< 2 && i
> 0)
2870 fprintf_unfiltered (gdb_stdlog
, " ...");
2873 fprintf_unfiltered (gdb_stdlog
, "\n");
2876 fprintf_unfiltered (gdb_stdlog
, " %02x", myaddr
[i
] & 0xff);
2880 fputc_unfiltered ('\n', gdb_stdlog
);
2886 debug_to_files_info (struct target_ops
*target
)
2888 debug_target
.to_files_info (target
);
2890 fprintf_unfiltered (gdb_stdlog
, "target_files_info (xxx)\n");
2894 debug_to_insert_breakpoint (struct bp_target_info
*bp_tgt
)
2898 retval
= debug_target
.to_insert_breakpoint (bp_tgt
);
2900 fprintf_unfiltered (gdb_stdlog
,
2901 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2902 (unsigned long) bp_tgt
->placed_address
,
2903 (unsigned long) retval
);
2908 debug_to_remove_breakpoint (struct bp_target_info
*bp_tgt
)
2912 retval
= debug_target
.to_remove_breakpoint (bp_tgt
);
2914 fprintf_unfiltered (gdb_stdlog
,
2915 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2916 (unsigned long) bp_tgt
->placed_address
,
2917 (unsigned long) retval
);
2922 debug_to_can_use_hw_breakpoint (int type
, int cnt
, int from_tty
)
2926 retval
= debug_target
.to_can_use_hw_breakpoint (type
, cnt
, from_tty
);
2928 fprintf_unfiltered (gdb_stdlog
,
2929 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2930 (unsigned long) type
,
2931 (unsigned long) cnt
,
2932 (unsigned long) from_tty
,
2933 (unsigned long) retval
);
2938 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
2942 retval
= debug_target
.to_region_ok_for_hw_watchpoint (addr
, len
);
2944 fprintf_unfiltered (gdb_stdlog
,
2945 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
2946 (unsigned long) addr
,
2947 (unsigned long) len
,
2948 (unsigned long) retval
);
2953 debug_to_stopped_by_watchpoint (void)
2957 retval
= debug_target
.to_stopped_by_watchpoint ();
2959 fprintf_unfiltered (gdb_stdlog
,
2960 "target_stopped_by_watchpoint () = %ld\n",
2961 (unsigned long) retval
);
2966 debug_to_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr
)
2970 retval
= debug_target
.to_stopped_data_address (target
, addr
);
2972 fprintf_unfiltered (gdb_stdlog
,
2973 "target_stopped_data_address ([0x%lx]) = %ld\n",
2974 (unsigned long)*addr
,
2975 (unsigned long)retval
);
2980 debug_to_watchpoint_addr_within_range (struct target_ops
*target
,
2982 CORE_ADDR start
, int length
)
2986 retval
= debug_target
.to_watchpoint_addr_within_range (target
, addr
,
2989 fprintf_filtered (gdb_stdlog
,
2990 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
2991 (unsigned long) addr
, (unsigned long) start
, length
,
2997 debug_to_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
3001 retval
= debug_target
.to_insert_hw_breakpoint (bp_tgt
);
3003 fprintf_unfiltered (gdb_stdlog
,
3004 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3005 (unsigned long) bp_tgt
->placed_address
,
3006 (unsigned long) retval
);
3011 debug_to_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
3015 retval
= debug_target
.to_remove_hw_breakpoint (bp_tgt
);
3017 fprintf_unfiltered (gdb_stdlog
,
3018 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3019 (unsigned long) bp_tgt
->placed_address
,
3020 (unsigned long) retval
);
3025 debug_to_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
3029 retval
= debug_target
.to_insert_watchpoint (addr
, len
, type
);
3031 fprintf_unfiltered (gdb_stdlog
,
3032 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3033 (unsigned long) addr
, len
, type
, (unsigned long) retval
);
3038 debug_to_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
3042 retval
= debug_target
.to_remove_watchpoint (addr
, len
, type
);
3044 fprintf_unfiltered (gdb_stdlog
,
3045 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3046 (unsigned long) addr
, len
, type
, (unsigned long) retval
);
3051 debug_to_terminal_init (void)
3053 debug_target
.to_terminal_init ();
3055 fprintf_unfiltered (gdb_stdlog
, "target_terminal_init ()\n");
3059 debug_to_terminal_inferior (void)
3061 debug_target
.to_terminal_inferior ();
3063 fprintf_unfiltered (gdb_stdlog
, "target_terminal_inferior ()\n");
3067 debug_to_terminal_ours_for_output (void)
3069 debug_target
.to_terminal_ours_for_output ();
3071 fprintf_unfiltered (gdb_stdlog
, "target_terminal_ours_for_output ()\n");
3075 debug_to_terminal_ours (void)
3077 debug_target
.to_terminal_ours ();
3079 fprintf_unfiltered (gdb_stdlog
, "target_terminal_ours ()\n");
3083 debug_to_terminal_save_ours (void)
3085 debug_target
.to_terminal_save_ours ();
3087 fprintf_unfiltered (gdb_stdlog
, "target_terminal_save_ours ()\n");
3091 debug_to_terminal_info (char *arg
, int from_tty
)
3093 debug_target
.to_terminal_info (arg
, from_tty
);
3095 fprintf_unfiltered (gdb_stdlog
, "target_terminal_info (%s, %d)\n", arg
,
3100 debug_to_load (char *args
, int from_tty
)
3102 debug_target
.to_load (args
, from_tty
);
3104 fprintf_unfiltered (gdb_stdlog
, "target_load (%s, %d)\n", args
, from_tty
);
3108 debug_to_lookup_symbol (char *name
, CORE_ADDR
*addrp
)
3112 retval
= debug_target
.to_lookup_symbol (name
, addrp
);
3114 fprintf_unfiltered (gdb_stdlog
, "target_lookup_symbol (%s, xxx)\n", name
);
3120 debug_to_post_startup_inferior (ptid_t ptid
)
3122 debug_target
.to_post_startup_inferior (ptid
);
3124 fprintf_unfiltered (gdb_stdlog
, "target_post_startup_inferior (%d)\n",
3129 debug_to_acknowledge_created_inferior (int pid
)
3131 debug_target
.to_acknowledge_created_inferior (pid
);
3133 fprintf_unfiltered (gdb_stdlog
, "target_acknowledge_created_inferior (%d)\n",
3138 debug_to_insert_fork_catchpoint (int pid
)
3140 debug_target
.to_insert_fork_catchpoint (pid
);
3142 fprintf_unfiltered (gdb_stdlog
, "target_insert_fork_catchpoint (%d)\n",
3147 debug_to_remove_fork_catchpoint (int pid
)
3151 retval
= debug_target
.to_remove_fork_catchpoint (pid
);
3153 fprintf_unfiltered (gdb_stdlog
, "target_remove_fork_catchpoint (%d) = %d\n",
3160 debug_to_insert_vfork_catchpoint (int pid
)
3162 debug_target
.to_insert_vfork_catchpoint (pid
);
3164 fprintf_unfiltered (gdb_stdlog
, "target_insert_vfork_catchpoint (%d)\n",
3169 debug_to_remove_vfork_catchpoint (int pid
)
3173 retval
= debug_target
.to_remove_vfork_catchpoint (pid
);
3175 fprintf_unfiltered (gdb_stdlog
, "target_remove_vfork_catchpoint (%d) = %d\n",
3182 debug_to_insert_exec_catchpoint (int pid
)
3184 debug_target
.to_insert_exec_catchpoint (pid
);
3186 fprintf_unfiltered (gdb_stdlog
, "target_insert_exec_catchpoint (%d)\n",
3191 debug_to_remove_exec_catchpoint (int pid
)
3195 retval
= debug_target
.to_remove_exec_catchpoint (pid
);
3197 fprintf_unfiltered (gdb_stdlog
, "target_remove_exec_catchpoint (%d) = %d\n",
3204 debug_to_has_exited (int pid
, int wait_status
, int *exit_status
)
3208 has_exited
= debug_target
.to_has_exited (pid
, wait_status
, exit_status
);
3210 fprintf_unfiltered (gdb_stdlog
, "target_has_exited (%d, %d, %d) = %d\n",
3211 pid
, wait_status
, *exit_status
, has_exited
);
3217 debug_to_can_run (void)
3221 retval
= debug_target
.to_can_run ();
3223 fprintf_unfiltered (gdb_stdlog
, "target_can_run () = %d\n", retval
);
3229 debug_to_notice_signals (ptid_t ptid
)
3231 debug_target
.to_notice_signals (ptid
);
3233 fprintf_unfiltered (gdb_stdlog
, "target_notice_signals (%d)\n",
3238 debug_to_stop (ptid_t ptid
)
3240 debug_target
.to_stop (ptid
);
3242 fprintf_unfiltered (gdb_stdlog
, "target_stop (%s)\n",
3243 target_pid_to_str (ptid
));
3247 debug_to_rcmd (char *command
,
3248 struct ui_file
*outbuf
)
3250 debug_target
.to_rcmd (command
, outbuf
);
3251 fprintf_unfiltered (gdb_stdlog
, "target_rcmd (%s, ...)\n", command
);
3255 debug_to_pid_to_exec_file (int pid
)
3259 exec_file
= debug_target
.to_pid_to_exec_file (pid
);
3261 fprintf_unfiltered (gdb_stdlog
, "target_pid_to_exec_file (%d) = %s\n",
3268 setup_target_debug (void)
3270 memcpy (&debug_target
, ¤t_target
, sizeof debug_target
);
3272 current_target
.to_open
= debug_to_open
;
3273 current_target
.to_post_attach
= debug_to_post_attach
;
3274 current_target
.to_prepare_to_store
= debug_to_prepare_to_store
;
3275 current_target
.deprecated_xfer_memory
= deprecated_debug_xfer_memory
;
3276 current_target
.to_files_info
= debug_to_files_info
;
3277 current_target
.to_insert_breakpoint
= debug_to_insert_breakpoint
;
3278 current_target
.to_remove_breakpoint
= debug_to_remove_breakpoint
;
3279 current_target
.to_can_use_hw_breakpoint
= debug_to_can_use_hw_breakpoint
;
3280 current_target
.to_insert_hw_breakpoint
= debug_to_insert_hw_breakpoint
;
3281 current_target
.to_remove_hw_breakpoint
= debug_to_remove_hw_breakpoint
;
3282 current_target
.to_insert_watchpoint
= debug_to_insert_watchpoint
;
3283 current_target
.to_remove_watchpoint
= debug_to_remove_watchpoint
;
3284 current_target
.to_stopped_by_watchpoint
= debug_to_stopped_by_watchpoint
;
3285 current_target
.to_stopped_data_address
= debug_to_stopped_data_address
;
3286 current_target
.to_watchpoint_addr_within_range
= debug_to_watchpoint_addr_within_range
;
3287 current_target
.to_region_ok_for_hw_watchpoint
= debug_to_region_ok_for_hw_watchpoint
;
3288 current_target
.to_terminal_init
= debug_to_terminal_init
;
3289 current_target
.to_terminal_inferior
= debug_to_terminal_inferior
;
3290 current_target
.to_terminal_ours_for_output
= debug_to_terminal_ours_for_output
;
3291 current_target
.to_terminal_ours
= debug_to_terminal_ours
;
3292 current_target
.to_terminal_save_ours
= debug_to_terminal_save_ours
;
3293 current_target
.to_terminal_info
= debug_to_terminal_info
;
3294 current_target
.to_load
= debug_to_load
;
3295 current_target
.to_lookup_symbol
= debug_to_lookup_symbol
;
3296 current_target
.to_post_startup_inferior
= debug_to_post_startup_inferior
;
3297 current_target
.to_acknowledge_created_inferior
= debug_to_acknowledge_created_inferior
;
3298 current_target
.to_insert_fork_catchpoint
= debug_to_insert_fork_catchpoint
;
3299 current_target
.to_remove_fork_catchpoint
= debug_to_remove_fork_catchpoint
;
3300 current_target
.to_insert_vfork_catchpoint
= debug_to_insert_vfork_catchpoint
;
3301 current_target
.to_remove_vfork_catchpoint
= debug_to_remove_vfork_catchpoint
;
3302 current_target
.to_insert_exec_catchpoint
= debug_to_insert_exec_catchpoint
;
3303 current_target
.to_remove_exec_catchpoint
= debug_to_remove_exec_catchpoint
;
3304 current_target
.to_has_exited
= debug_to_has_exited
;
3305 current_target
.to_can_run
= debug_to_can_run
;
3306 current_target
.to_notice_signals
= debug_to_notice_signals
;
3307 current_target
.to_stop
= debug_to_stop
;
3308 current_target
.to_rcmd
= debug_to_rcmd
;
3309 current_target
.to_pid_to_exec_file
= debug_to_pid_to_exec_file
;
3313 static char targ_desc
[] =
3314 "Names of targets and files being debugged.\n\
3315 Shows the entire stack of targets currently in use (including the exec-file,\n\
3316 core-file, and process, if any), as well as the symbol file name.";
3319 do_monitor_command (char *cmd
,
3322 if ((current_target
.to_rcmd
3323 == (void (*) (char *, struct ui_file
*)) tcomplain
)
3324 || (current_target
.to_rcmd
== debug_to_rcmd
3325 && (debug_target
.to_rcmd
3326 == (void (*) (char *, struct ui_file
*)) tcomplain
)))
3327 error (_("\"monitor\" command not supported by this target."));
3328 target_rcmd (cmd
, gdb_stdtarg
);
3331 /* Print the name of each layers of our target stack. */
3334 maintenance_print_target_stack (char *cmd
, int from_tty
)
3336 struct target_ops
*t
;
3338 printf_filtered (_("The current target stack is:\n"));
3340 for (t
= target_stack
; t
!= NULL
; t
= t
->beneath
)
3342 printf_filtered (" - %s (%s)\n", t
->to_shortname
, t
->to_longname
);
3346 /* Controls if async mode is permitted. */
3347 int target_async_permitted
= 0;
3349 /* The set command writes to this variable. If the inferior is
3350 executing, linux_nat_async_permitted is *not* updated. */
3351 static int target_async_permitted_1
= 0;
3354 set_maintenance_target_async_permitted (char *args
, int from_tty
,
3355 struct cmd_list_element
*c
)
3357 if (have_live_inferiors ())
3359 target_async_permitted_1
= target_async_permitted
;
3360 error (_("Cannot change this setting while the inferior is running."));
3363 target_async_permitted
= target_async_permitted_1
;
3367 show_maintenance_target_async_permitted (struct ui_file
*file
, int from_tty
,
3368 struct cmd_list_element
*c
,
3371 fprintf_filtered (file
, _("\
3372 Controlling the inferior in asynchronous mode is %s.\n"), value
);
3376 initialize_targets (void)
3378 init_dummy_target ();
3379 push_target (&dummy_target
);
3381 add_info ("target", target_info
, targ_desc
);
3382 add_info ("files", target_info
, targ_desc
);
3384 add_setshow_zinteger_cmd ("target", class_maintenance
, &targetdebug
, _("\
3385 Set target debugging."), _("\
3386 Show target debugging."), _("\
3387 When non-zero, target debugging is enabled. Higher numbers are more\n\
3388 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3392 &setdebuglist
, &showdebuglist
);
3394 add_setshow_boolean_cmd ("trust-readonly-sections", class_support
,
3395 &trust_readonly
, _("\
3396 Set mode for reading from readonly sections."), _("\
3397 Show mode for reading from readonly sections."), _("\
3398 When this mode is on, memory reads from readonly sections (such as .text)\n\
3399 will be read from the object file instead of from the target. This will\n\
3400 result in significant performance improvement for remote targets."),
3402 show_trust_readonly
,
3403 &setlist
, &showlist
);
3405 add_com ("monitor", class_obscure
, do_monitor_command
,
3406 _("Send a command to the remote monitor (remote targets only)."));
3408 add_cmd ("target-stack", class_maintenance
, maintenance_print_target_stack
,
3409 _("Print the name of each layer of the internal target stack."),
3410 &maintenanceprintlist
);
3412 add_setshow_boolean_cmd ("target-async", no_class
,
3413 &target_async_permitted_1
, _("\
3414 Set whether gdb controls the inferior in asynchronous mode."), _("\
3415 Show whether gdb controls the inferior in asynchronous mode."), _("\
3416 Tells gdb whether to control the inferior in asynchronous mode."),
3417 set_maintenance_target_async_permitted
,
3418 show_maintenance_target_async_permitted
,
3422 target_dcache
= dcache_init ();