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, 2010
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"
45 #include "inline-frame.h"
47 static void target_info (char *, int);
49 static void kill_or_be_killed (int);
51 static void default_terminal_info (char *, int);
53 static int default_watchpoint_addr_within_range (struct target_ops *,
54 CORE_ADDR, CORE_ADDR, int);
56 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
58 static int nosymbol (char *, CORE_ADDR *);
60 static void tcomplain (void) ATTR_NORETURN;
62 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
64 static int return_zero (void);
66 static int return_one (void);
68 static int return_minus_one (void);
70 void target_ignore (void);
72 static void target_command (char *, int);
74 static struct target_ops *find_default_run_target (char *);
76 static void nosupport_runtime (void);
78 static LONGEST default_xfer_partial (struct target_ops *ops,
79 enum target_object object,
80 const char *annex, gdb_byte *readbuf,
81 const gdb_byte *writebuf,
82 ULONGEST offset, LONGEST len);
84 static 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);
90 static LONGEST target_xfer_partial (struct target_ops *ops,
91 enum target_object object,
93 void *readbuf, const void *writebuf,
94 ULONGEST offset, LONGEST len);
96 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
99 static void init_dummy_target (void);
101 static struct target_ops debug_target;
103 static void debug_to_open (char *, int);
105 static void debug_to_prepare_to_store (struct regcache *);
107 static void debug_to_files_info (struct target_ops *);
109 static int debug_to_insert_breakpoint (struct gdbarch *,
110 struct bp_target_info *);
112 static int debug_to_remove_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
115 static int debug_to_can_use_hw_breakpoint (int, int, int);
117 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
118 struct bp_target_info *);
120 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
121 struct bp_target_info *);
123 static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
125 static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
127 static int debug_to_stopped_by_watchpoint (void);
129 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
131 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
132 CORE_ADDR, CORE_ADDR, int);
134 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
136 static void debug_to_terminal_init (void);
138 static void debug_to_terminal_inferior (void);
140 static void debug_to_terminal_ours_for_output (void);
142 static void debug_to_terminal_save_ours (void);
144 static void debug_to_terminal_ours (void);
146 static void debug_to_terminal_info (char *, int);
148 static void debug_to_load (char *, int);
150 static int debug_to_lookup_symbol (char *, CORE_ADDR *);
152 static int debug_to_can_run (void);
154 static void debug_to_notice_signals (ptid_t);
156 static void debug_to_stop (ptid_t);
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. */
162 struct target_ops deprecated_child_ops;
164 /* Pointer to array of target architecture structures; the size of the
165 array; the current index into the array; the allocated size of the
167 struct target_ops **target_structs;
168 unsigned target_struct_size;
169 unsigned target_struct_index;
170 unsigned target_struct_allocsize;
171 #define DEFAULT_ALLOCSIZE 10
173 /* The initial current target, so that there is always a semi-valid
176 static struct target_ops dummy_target;
178 /* Top of target stack. */
180 static struct target_ops *target_stack;
182 /* The target structure we are currently using to talk to a process
183 or file or whatever "inferior" we have. */
185 struct target_ops current_target;
187 /* Command list for target. */
189 static struct cmd_list_element *targetlist = NULL;
191 /* Nonzero if we should trust readonly sections from the
192 executable when reading memory. */
194 static int trust_readonly = 0;
196 /* Nonzero if we should show true memory content including
197 memory breakpoint inserted by gdb. */
199 static int show_memory_breakpoints = 0;
201 /* Non-zero if we want to see trace of target level stuff. */
203 static int targetdebug = 0;
205 show_targetdebug (struct ui_file *file, int from_tty,
206 struct cmd_list_element *c, const char *value)
208 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
211 static void setup_target_debug (void);
213 /* The option sets this. */
214 static int stack_cache_enabled_p_1 = 1;
215 /* And set_stack_cache_enabled_p updates this.
216 The reason for the separation is so that we don't flush the cache for
217 on->on transitions. */
218 static int stack_cache_enabled_p = 1;
220 /* This is called *after* the stack-cache has been set.
221 Flush the cache for off->on and on->off transitions.
222 There's no real need to flush the cache for on->off transitions,
223 except cleanliness. */
226 set_stack_cache_enabled_p (char *args, int from_tty,
227 struct cmd_list_element *c)
229 if (stack_cache_enabled_p != stack_cache_enabled_p_1)
230 target_dcache_invalidate ();
232 stack_cache_enabled_p = stack_cache_enabled_p_1;
236 show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
237 struct cmd_list_element *c, const char *value)
239 fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
242 /* Cache of memory operations, to speed up remote access. */
243 static DCACHE *target_dcache;
245 /* Invalidate the target dcache. */
248 target_dcache_invalidate (void)
250 dcache_invalidate (target_dcache);
253 /* The user just typed 'target' without the name of a target. */
256 target_command (char *arg, int from_tty)
258 fputs_filtered ("Argument required (target name). Try `help target'\n",
262 /* Default target_has_* methods for process_stratum targets. */
265 default_child_has_all_memory (struct target_ops *ops)
267 /* If no inferior selected, then we can't read memory here. */
268 if (ptid_equal (inferior_ptid, null_ptid))
275 default_child_has_memory (struct target_ops *ops)
277 /* If no inferior selected, then we can't read memory here. */
278 if (ptid_equal (inferior_ptid, null_ptid))
285 default_child_has_stack (struct target_ops *ops)
287 /* If no inferior selected, there's no stack. */
288 if (ptid_equal (inferior_ptid, null_ptid))
295 default_child_has_registers (struct target_ops *ops)
297 /* Can't read registers from no inferior. */
298 if (ptid_equal (inferior_ptid, null_ptid))
305 default_child_has_execution (struct target_ops *ops)
307 /* If there's no thread selected, then we can't make it run through
309 if (ptid_equal (inferior_ptid, null_ptid))
317 target_has_all_memory_1 (void)
319 struct target_ops *t;
321 for (t = current_target.beneath; t != NULL; t = t->beneath)
322 if (t->to_has_all_memory (t))
329 target_has_memory_1 (void)
331 struct target_ops *t;
333 for (t = current_target.beneath; t != NULL; t = t->beneath)
334 if (t->to_has_memory (t))
341 target_has_stack_1 (void)
343 struct target_ops *t;
345 for (t = current_target.beneath; t != NULL; t = t->beneath)
346 if (t->to_has_stack (t))
353 target_has_registers_1 (void)
355 struct target_ops *t;
357 for (t = current_target.beneath; t != NULL; t = t->beneath)
358 if (t->to_has_registers (t))
365 target_has_execution_1 (void)
367 struct target_ops *t;
369 for (t = current_target.beneath; t != NULL; t = t->beneath)
370 if (t->to_has_execution (t))
376 /* Add a possible target architecture to the list. */
379 add_target (struct target_ops *t)
381 /* Provide default values for all "must have" methods. */
382 if (t->to_xfer_partial == NULL)
383 t->to_xfer_partial = default_xfer_partial;
385 if (t->to_has_all_memory == NULL)
386 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
388 if (t->to_has_memory == NULL)
389 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
391 if (t->to_has_stack == NULL)
392 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
394 if (t->to_has_registers == NULL)
395 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
397 if (t->to_has_execution == NULL)
398 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
402 target_struct_allocsize = DEFAULT_ALLOCSIZE;
403 target_structs = (struct target_ops **) xmalloc
404 (target_struct_allocsize * sizeof (*target_structs));
406 if (target_struct_size >= target_struct_allocsize)
408 target_struct_allocsize *= 2;
409 target_structs = (struct target_ops **)
410 xrealloc ((char *) target_structs,
411 target_struct_allocsize * sizeof (*target_structs));
413 target_structs[target_struct_size++] = t;
415 if (targetlist == NULL)
416 add_prefix_cmd ("target", class_run, target_command, _("\
417 Connect to a target machine or process.\n\
418 The first argument is the type or protocol of the target machine.\n\
419 Remaining arguments are interpreted by the target protocol. For more\n\
420 information on the arguments for a particular protocol, type\n\
421 `help target ' followed by the protocol name."),
422 &targetlist, "target ", 0, &cmdlist);
423 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
436 struct target_ops *t;
438 for (t = current_target.beneath; t != NULL; t = t->beneath)
439 if (t->to_kill != NULL)
442 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
452 target_load (char *arg, int from_tty)
454 target_dcache_invalidate ();
455 (*current_target.to_load) (arg, from_tty);
459 target_create_inferior (char *exec_file, char *args,
460 char **env, int from_tty)
462 struct target_ops *t;
463 for (t = current_target.beneath; t != NULL; t = t->beneath)
465 if (t->to_create_inferior != NULL)
467 t->to_create_inferior (t, exec_file, args, env, from_tty);
469 fprintf_unfiltered (gdb_stdlog,
470 "target_create_inferior (%s, %s, xxx, %d)\n",
471 exec_file, args, from_tty);
476 internal_error (__FILE__, __LINE__,
477 "could not find a target to create inferior");
481 target_terminal_inferior (void)
483 /* A background resume (``run&'') should leave GDB in control of the
484 terminal. Use target_can_async_p, not target_is_async_p, since at
485 this point the target is not async yet. However, if sync_execution
486 is not set, we know it will become async prior to resume. */
487 if (target_can_async_p () && !sync_execution)
490 /* If GDB is resuming the inferior in the foreground, install
491 inferior's terminal modes. */
492 (*current_target.to_terminal_inferior) ();
496 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
497 struct target_ops *t)
499 errno = EIO; /* Can't read/write this location */
500 return 0; /* No bytes handled */
506 error (_("You can't do that when your target is `%s'"),
507 current_target.to_shortname);
513 error (_("You can't do that without a process to debug."));
517 nosymbol (char *name, CORE_ADDR *addrp)
519 return 1; /* Symbol does not exist in target env */
523 nosupport_runtime (void)
525 if (ptid_equal (inferior_ptid, null_ptid))
528 error (_("No run-time support for this"));
533 default_terminal_info (char *args, int from_tty)
535 printf_unfiltered (_("No saved terminal information.\n"));
538 /* This is the default target_create_inferior and target_attach function.
539 If the current target is executing, it asks whether to kill it off.
540 If this function returns without calling error(), it has killed off
541 the target, and the operation should be attempted. */
544 kill_or_be_killed (int from_tty)
546 if (target_has_execution)
548 printf_unfiltered (_("You are already running a program:\n"));
549 target_files_info ();
550 if (query (_("Kill it? ")))
553 if (target_has_execution)
554 error (_("Killing the program did not help."));
559 error (_("Program not killed."));
565 /* A default implementation for the to_get_ada_task_ptid target method.
567 This function builds the PTID by using both LWP and TID as part of
568 the PTID lwp and tid elements. The pid used is the pid of the
572 default_get_ada_task_ptid (long lwp, long tid)
574 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
577 /* Go through the target stack from top to bottom, copying over zero
578 entries in current_target, then filling in still empty entries. In
579 effect, we are doing class inheritance through the pushed target
582 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
583 is currently implemented, is that it discards any knowledge of
584 which target an inherited method originally belonged to.
585 Consequently, new new target methods should instead explicitly and
586 locally search the target stack for the target that can handle the
590 update_current_target (void)
592 struct target_ops *t;
594 /* First, reset current's contents. */
595 memset (¤t_target, 0, sizeof (current_target));
597 #define INHERIT(FIELD, TARGET) \
598 if (!current_target.FIELD) \
599 current_target.FIELD = (TARGET)->FIELD
601 for (t = target_stack; t; t = t->beneath)
603 INHERIT (to_shortname, t);
604 INHERIT (to_longname, t);
606 /* Do not inherit to_open. */
607 /* Do not inherit to_close. */
608 /* Do not inherit to_attach. */
609 INHERIT (to_post_attach, t);
610 INHERIT (to_attach_no_wait, t);
611 /* Do not inherit to_detach. */
612 /* Do not inherit to_disconnect. */
613 /* Do not inherit to_resume. */
614 /* Do not inherit to_wait. */
615 /* Do not inherit to_fetch_registers. */
616 /* Do not inherit to_store_registers. */
617 INHERIT (to_prepare_to_store, t);
618 INHERIT (deprecated_xfer_memory, t);
619 INHERIT (to_files_info, t);
620 INHERIT (to_insert_breakpoint, t);
621 INHERIT (to_remove_breakpoint, t);
622 INHERIT (to_can_use_hw_breakpoint, t);
623 INHERIT (to_insert_hw_breakpoint, t);
624 INHERIT (to_remove_hw_breakpoint, t);
625 INHERIT (to_insert_watchpoint, t);
626 INHERIT (to_remove_watchpoint, t);
627 INHERIT (to_stopped_data_address, t);
628 INHERIT (to_have_steppable_watchpoint, t);
629 INHERIT (to_have_continuable_watchpoint, t);
630 INHERIT (to_stopped_by_watchpoint, t);
631 INHERIT (to_watchpoint_addr_within_range, t);
632 INHERIT (to_region_ok_for_hw_watchpoint, t);
633 INHERIT (to_terminal_init, t);
634 INHERIT (to_terminal_inferior, t);
635 INHERIT (to_terminal_ours_for_output, t);
636 INHERIT (to_terminal_ours, t);
637 INHERIT (to_terminal_save_ours, t);
638 INHERIT (to_terminal_info, t);
639 /* Do not inherit to_kill. */
640 INHERIT (to_load, t);
641 INHERIT (to_lookup_symbol, t);
642 /* Do no inherit to_create_inferior. */
643 INHERIT (to_post_startup_inferior, t);
644 INHERIT (to_acknowledge_created_inferior, t);
645 INHERIT (to_insert_fork_catchpoint, t);
646 INHERIT (to_remove_fork_catchpoint, t);
647 INHERIT (to_insert_vfork_catchpoint, t);
648 INHERIT (to_remove_vfork_catchpoint, t);
649 /* Do not inherit to_follow_fork. */
650 INHERIT (to_insert_exec_catchpoint, t);
651 INHERIT (to_remove_exec_catchpoint, t);
652 INHERIT (to_set_syscall_catchpoint, t);
653 INHERIT (to_has_exited, t);
654 /* Do not inherit to_mourn_inferiour. */
655 INHERIT (to_can_run, t);
656 INHERIT (to_notice_signals, t);
657 /* Do not inherit to_thread_alive. */
658 /* Do not inherit to_find_new_threads. */
659 /* Do not inherit to_pid_to_str. */
660 INHERIT (to_extra_thread_info, t);
661 INHERIT (to_stop, t);
662 /* Do not inherit to_xfer_partial. */
663 INHERIT (to_rcmd, t);
664 INHERIT (to_pid_to_exec_file, t);
665 INHERIT (to_log_command, t);
666 INHERIT (to_stratum, t);
667 /* Do not inherit to_has_all_memory */
668 /* Do not inherit to_has_memory */
669 /* Do not inherit to_has_stack */
670 /* Do not inherit to_has_registers */
671 /* Do not inherit to_has_execution */
672 INHERIT (to_has_thread_control, t);
673 INHERIT (to_can_async_p, t);
674 INHERIT (to_is_async_p, t);
675 INHERIT (to_async, t);
676 INHERIT (to_async_mask, t);
677 INHERIT (to_find_memory_regions, t);
678 INHERIT (to_make_corefile_notes, t);
679 INHERIT (to_get_bookmark, t);
680 INHERIT (to_goto_bookmark, t);
681 /* Do not inherit to_get_thread_local_address. */
682 INHERIT (to_can_execute_reverse, t);
683 INHERIT (to_thread_architecture, t);
684 /* Do not inherit to_read_description. */
685 INHERIT (to_get_ada_task_ptid, t);
686 /* Do not inherit to_search_memory. */
687 INHERIT (to_supports_multi_process, t);
688 INHERIT (to_trace_init, t);
689 INHERIT (to_download_tracepoint, t);
690 INHERIT (to_download_trace_state_variable, t);
691 INHERIT (to_trace_set_readonly_regions, t);
692 INHERIT (to_trace_start, t);
693 INHERIT (to_get_trace_status, t);
694 INHERIT (to_trace_stop, t);
695 INHERIT (to_trace_find, t);
696 INHERIT (to_get_trace_state_variable_value, t);
697 INHERIT (to_save_trace_data, t);
698 INHERIT (to_upload_tracepoints, t);
699 INHERIT (to_upload_trace_state_variables, t);
700 INHERIT (to_get_raw_trace_data, t);
701 INHERIT (to_set_disconnected_tracing, t);
702 INHERIT (to_magic, t);
703 /* Do not inherit to_memory_map. */
704 /* Do not inherit to_flash_erase. */
705 /* Do not inherit to_flash_done. */
709 /* Clean up a target struct so it no longer has any zero pointers in
710 it. Some entries are defaulted to a method that print an error,
711 others are hard-wired to a standard recursive default. */
713 #define de_fault(field, value) \
714 if (!current_target.field) \
715 current_target.field = value
718 (void (*) (char *, int))
723 de_fault (to_post_attach,
726 de_fault (to_prepare_to_store,
727 (void (*) (struct regcache *))
729 de_fault (deprecated_xfer_memory,
730 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
732 de_fault (to_files_info,
733 (void (*) (struct target_ops *))
735 de_fault (to_insert_breakpoint,
736 memory_insert_breakpoint);
737 de_fault (to_remove_breakpoint,
738 memory_remove_breakpoint);
739 de_fault (to_can_use_hw_breakpoint,
740 (int (*) (int, int, int))
742 de_fault (to_insert_hw_breakpoint,
743 (int (*) (struct gdbarch *, struct bp_target_info *))
745 de_fault (to_remove_hw_breakpoint,
746 (int (*) (struct gdbarch *, struct bp_target_info *))
748 de_fault (to_insert_watchpoint,
749 (int (*) (CORE_ADDR, int, int))
751 de_fault (to_remove_watchpoint,
752 (int (*) (CORE_ADDR, int, int))
754 de_fault (to_stopped_by_watchpoint,
757 de_fault (to_stopped_data_address,
758 (int (*) (struct target_ops *, CORE_ADDR *))
760 de_fault (to_watchpoint_addr_within_range,
761 default_watchpoint_addr_within_range);
762 de_fault (to_region_ok_for_hw_watchpoint,
763 default_region_ok_for_hw_watchpoint);
764 de_fault (to_terminal_init,
767 de_fault (to_terminal_inferior,
770 de_fault (to_terminal_ours_for_output,
773 de_fault (to_terminal_ours,
776 de_fault (to_terminal_save_ours,
779 de_fault (to_terminal_info,
780 default_terminal_info);
782 (void (*) (char *, int))
784 de_fault (to_lookup_symbol,
785 (int (*) (char *, CORE_ADDR *))
787 de_fault (to_post_startup_inferior,
790 de_fault (to_acknowledge_created_inferior,
793 de_fault (to_insert_fork_catchpoint,
796 de_fault (to_remove_fork_catchpoint,
799 de_fault (to_insert_vfork_catchpoint,
802 de_fault (to_remove_vfork_catchpoint,
805 de_fault (to_insert_exec_catchpoint,
808 de_fault (to_remove_exec_catchpoint,
811 de_fault (to_set_syscall_catchpoint,
812 (int (*) (int, int, int, int, int *))
814 de_fault (to_has_exited,
815 (int (*) (int, int, int *))
817 de_fault (to_can_run,
819 de_fault (to_notice_signals,
822 de_fault (to_extra_thread_info,
823 (char *(*) (struct thread_info *))
828 current_target.to_xfer_partial = current_xfer_partial;
830 (void (*) (char *, struct ui_file *))
832 de_fault (to_pid_to_exec_file,
836 (void (*) (void (*) (enum inferior_event_type, void*), void*))
838 de_fault (to_async_mask,
841 de_fault (to_thread_architecture,
842 default_thread_architecture);
843 current_target.to_read_description = NULL;
844 de_fault (to_get_ada_task_ptid,
845 (ptid_t (*) (long, long))
846 default_get_ada_task_ptid);
847 de_fault (to_supports_multi_process,
850 de_fault (to_trace_init,
853 de_fault (to_download_tracepoint,
854 (void (*) (struct breakpoint *))
856 de_fault (to_download_trace_state_variable,
857 (void (*) (struct trace_state_variable *))
859 de_fault (to_trace_set_readonly_regions,
862 de_fault (to_trace_start,
865 de_fault (to_get_trace_status,
866 (int (*) (struct trace_status *))
868 de_fault (to_trace_stop,
871 de_fault (to_trace_find,
872 (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
874 de_fault (to_get_trace_state_variable_value,
875 (int (*) (int, LONGEST *))
877 de_fault (to_save_trace_data,
880 de_fault (to_upload_tracepoints,
881 (int (*) (struct uploaded_tp **))
883 de_fault (to_upload_trace_state_variables,
884 (int (*) (struct uploaded_tsv **))
886 de_fault (to_get_raw_trace_data,
887 (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
889 de_fault (to_set_disconnected_tracing,
894 /* Finally, position the target-stack beneath the squashed
895 "current_target". That way code looking for a non-inherited
896 target method can quickly and simply find it. */
897 current_target.beneath = target_stack;
900 setup_target_debug ();
903 /* Push a new target type into the stack of the existing target accessors,
904 possibly superseding some of the existing accessors.
906 Result is zero if the pushed target ended up on top of the stack,
907 nonzero if at least one target is on top of it.
909 Rather than allow an empty stack, we always have the dummy target at
910 the bottom stratum, so we can call the function vectors without
914 push_target (struct target_ops *t)
916 struct target_ops **cur;
918 /* Check magic number. If wrong, it probably means someone changed
919 the struct definition, but not all the places that initialize one. */
920 if (t->to_magic != OPS_MAGIC)
922 fprintf_unfiltered (gdb_stderr,
923 "Magic number of %s target struct wrong\n",
925 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
928 /* Find the proper stratum to install this target in. */
929 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
931 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
935 /* If there's already targets at this stratum, remove them. */
936 /* FIXME: cagney/2003-10-15: I think this should be popping all
937 targets to CUR, and not just those at this stratum level. */
938 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
940 /* There's already something at this stratum level. Close it,
941 and un-hook it from the stack. */
942 struct target_ops *tmp = (*cur);
943 (*cur) = (*cur)->beneath;
945 target_close (tmp, 0);
948 /* We have removed all targets in our stratum, now add the new one. */
952 update_current_target ();
955 return (t != target_stack);
958 /* Remove a target_ops vector from the stack, wherever it may be.
959 Return how many times it was removed (0 or 1). */
962 unpush_target (struct target_ops *t)
964 struct target_ops **cur;
965 struct target_ops *tmp;
967 if (t->to_stratum == dummy_stratum)
968 internal_error (__FILE__, __LINE__,
969 "Attempt to unpush the dummy target");
971 /* Look for the specified target. Note that we assume that a target
972 can only occur once in the target stack. */
974 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
981 return 0; /* Didn't find target_ops, quit now */
983 /* NOTE: cagney/2003-12-06: In '94 the close call was made
984 unconditional by moving it to before the above check that the
985 target was in the target stack (something about "Change the way
986 pushing and popping of targets work to support target overlays
987 and inheritance"). This doesn't make much sense - only open
988 targets should be closed. */
991 /* Unchain the target */
993 (*cur) = (*cur)->beneath;
996 update_current_target ();
1004 target_close (target_stack, 0); /* Let it clean up */
1005 if (unpush_target (target_stack) == 1)
1008 fprintf_unfiltered (gdb_stderr,
1009 "pop_target couldn't find target %s\n",
1010 current_target.to_shortname);
1011 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
1015 pop_all_targets_above (enum strata above_stratum, int quitting)
1017 while ((int) (current_target.to_stratum) > (int) above_stratum)
1019 target_close (target_stack, quitting);
1020 if (!unpush_target (target_stack))
1022 fprintf_unfiltered (gdb_stderr,
1023 "pop_all_targets couldn't find target %s\n",
1024 target_stack->to_shortname);
1025 internal_error (__FILE__, __LINE__,
1026 _("failed internal consistency check"));
1033 pop_all_targets (int quitting)
1035 pop_all_targets_above (dummy_stratum, quitting);
1038 /* Using the objfile specified in OBJFILE, find the address for the
1039 current thread's thread-local storage with offset OFFSET. */
1041 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1043 volatile CORE_ADDR addr = 0;
1044 struct target_ops *target;
1046 for (target = current_target.beneath;
1048 target = target->beneath)
1050 if (target->to_get_thread_local_address != NULL)
1055 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
1057 ptid_t ptid = inferior_ptid;
1058 volatile struct gdb_exception ex;
1060 TRY_CATCH (ex, RETURN_MASK_ALL)
1064 /* Fetch the load module address for this objfile. */
1065 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
1067 /* If it's 0, throw the appropriate exception. */
1069 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1070 _("TLS load module not found"));
1072 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
1074 /* If an error occurred, print TLS related messages here. Otherwise,
1075 throw the error to some higher catcher. */
1078 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1082 case TLS_NO_LIBRARY_SUPPORT_ERROR:
1083 error (_("Cannot find thread-local variables in this thread library."));
1085 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1086 if (objfile_is_library)
1087 error (_("Cannot find shared library `%s' in dynamic"
1088 " linker's load module list"), objfile->name);
1090 error (_("Cannot find executable file `%s' in dynamic"
1091 " linker's load module list"), objfile->name);
1093 case TLS_NOT_ALLOCATED_YET_ERROR:
1094 if (objfile_is_library)
1095 error (_("The inferior has not yet allocated storage for"
1096 " thread-local variables in\n"
1097 "the shared library `%s'\n"
1099 objfile->name, target_pid_to_str (ptid));
1101 error (_("The inferior has not yet allocated storage for"
1102 " thread-local variables in\n"
1103 "the executable `%s'\n"
1105 objfile->name, target_pid_to_str (ptid));
1107 case TLS_GENERIC_ERROR:
1108 if (objfile_is_library)
1109 error (_("Cannot find thread-local storage for %s, "
1110 "shared library %s:\n%s"),
1111 target_pid_to_str (ptid),
1112 objfile->name, ex.message);
1114 error (_("Cannot find thread-local storage for %s, "
1115 "executable file %s:\n%s"),
1116 target_pid_to_str (ptid),
1117 objfile->name, ex.message);
1120 throw_exception (ex);
1125 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1126 TLS is an ABI-specific thing. But we don't do that yet. */
1128 error (_("Cannot find thread-local variables on this target"));
1134 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1136 /* target_read_string -- read a null terminated string, up to LEN bytes,
1137 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1138 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1139 is responsible for freeing it. Return the number of bytes successfully
1143 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1145 int tlen, origlen, offset, i;
1149 int buffer_allocated;
1151 unsigned int nbytes_read = 0;
1153 gdb_assert (string);
1155 /* Small for testing. */
1156 buffer_allocated = 4;
1157 buffer = xmalloc (buffer_allocated);
1164 tlen = MIN (len, 4 - (memaddr & 3));
1165 offset = memaddr & 3;
1167 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1170 /* The transfer request might have crossed the boundary to an
1171 unallocated region of memory. Retry the transfer, requesting
1175 errcode = target_read_memory (memaddr, buf, 1);
1180 if (bufptr - buffer + tlen > buffer_allocated)
1183 bytes = bufptr - buffer;
1184 buffer_allocated *= 2;
1185 buffer = xrealloc (buffer, buffer_allocated);
1186 bufptr = buffer + bytes;
1189 for (i = 0; i < tlen; i++)
1191 *bufptr++ = buf[i + offset];
1192 if (buf[i + offset] == '\000')
1194 nbytes_read += i + 1;
1201 nbytes_read += tlen;
1210 struct target_section_table *
1211 target_get_section_table (struct target_ops *target)
1213 struct target_ops *t;
1216 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1218 for (t = target; t != NULL; t = t->beneath)
1219 if (t->to_get_section_table != NULL)
1220 return (*t->to_get_section_table) (t);
1225 /* Find a section containing ADDR. */
1227 struct target_section *
1228 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1230 struct target_section_table *table = target_get_section_table (target);
1231 struct target_section *secp;
1236 for (secp = table->sections; secp < table->sections_end; secp++)
1238 if (addr >= secp->addr && addr < secp->endaddr)
1244 /* Perform a partial memory transfer.
1245 For docs see target.h, to_xfer_partial. */
1248 memory_xfer_partial (struct target_ops *ops, enum target_object object,
1249 void *readbuf, const void *writebuf, ULONGEST memaddr,
1254 struct mem_region *region;
1255 struct inferior *inf;
1257 /* Zero length requests are ok and require no work. */
1261 /* For accesses to unmapped overlay sections, read directly from
1262 files. Must do this first, as MEMADDR may need adjustment. */
1263 if (readbuf != NULL && overlay_debugging)
1265 struct obj_section *section = find_pc_overlay (memaddr);
1266 if (pc_in_unmapped_range (memaddr, section))
1268 struct target_section_table *table
1269 = target_get_section_table (ops);
1270 const char *section_name = section->the_bfd_section->name;
1271 memaddr = overlay_mapped_address (memaddr, section);
1272 return section_table_xfer_memory_partial (readbuf, writebuf,
1275 table->sections_end,
1280 /* Try the executable files, if "trust-readonly-sections" is set. */
1281 if (readbuf != NULL && trust_readonly)
1283 struct target_section *secp;
1284 struct target_section_table *table;
1286 secp = target_section_by_addr (ops, memaddr);
1288 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1291 table = target_get_section_table (ops);
1292 return section_table_xfer_memory_partial (readbuf, writebuf,
1295 table->sections_end,
1300 /* Try GDB's internal data cache. */
1301 region = lookup_mem_region (memaddr);
1302 /* region->hi == 0 means there's no upper bound. */
1303 if (memaddr + len < region->hi || region->hi == 0)
1306 reg_len = region->hi - memaddr;
1308 switch (region->attrib.mode)
1311 if (writebuf != NULL)
1316 if (readbuf != NULL)
1321 /* We only support writing to flash during "load" for now. */
1322 if (writebuf != NULL)
1323 error (_("Writing to flash memory forbidden in this context"));
1330 if (!ptid_equal (inferior_ptid, null_ptid))
1331 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1336 && (region->attrib.cache
1337 || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
1339 if (readbuf != NULL)
1340 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
1343 /* FIXME drow/2006-08-09: If we're going to preserve const
1344 correctness dcache_xfer_memory should take readbuf and
1346 res = dcache_xfer_memory (ops, target_dcache, memaddr,
1353 if (readbuf && !show_memory_breakpoints)
1354 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1359 /* If none of those methods found the memory we wanted, fall back
1360 to a target partial transfer. Normally a single call to
1361 to_xfer_partial is enough; if it doesn't recognize an object
1362 it will call the to_xfer_partial of the next target down.
1363 But for memory this won't do. Memory is the only target
1364 object which can be read from more than one valid target.
1365 A core file, for instance, could have some of memory but
1366 delegate other bits to the target below it. So, we must
1367 manually try all targets. */
1371 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1372 readbuf, writebuf, memaddr, reg_len);
1376 /* We want to continue past core files to executables, but not
1377 past a running target's memory. */
1378 if (ops->to_has_all_memory (ops))
1383 while (ops != NULL);
1385 if (readbuf && !show_memory_breakpoints)
1386 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1388 /* Make sure the cache gets updated no matter what - if we are writing
1389 to the stack. Even if this write is not tagged as such, we still need
1390 to update the cache. */
1395 && !region->attrib.cache
1396 && stack_cache_enabled_p
1397 && object != TARGET_OBJECT_STACK_MEMORY)
1399 dcache_update (target_dcache, memaddr, (void *) writebuf, res);
1402 /* If we still haven't got anything, return the last error. We
1408 restore_show_memory_breakpoints (void *arg)
1410 show_memory_breakpoints = (uintptr_t) arg;
1414 make_show_memory_breakpoints_cleanup (int show)
1416 int current = show_memory_breakpoints;
1417 show_memory_breakpoints = show;
1419 return make_cleanup (restore_show_memory_breakpoints,
1420 (void *) (uintptr_t) current);
1423 /* For docs see target.h, to_xfer_partial. */
1426 target_xfer_partial (struct target_ops *ops,
1427 enum target_object object, const char *annex,
1428 void *readbuf, const void *writebuf,
1429 ULONGEST offset, LONGEST len)
1433 gdb_assert (ops->to_xfer_partial != NULL);
1435 /* If this is a memory transfer, let the memory-specific code
1436 have a look at it instead. Memory transfers are more
1438 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1439 retval = memory_xfer_partial (ops, object, readbuf,
1440 writebuf, offset, len);
1443 enum target_object raw_object = object;
1445 /* If this is a raw memory transfer, request the normal
1446 memory object from other layers. */
1447 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1448 raw_object = TARGET_OBJECT_MEMORY;
1450 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1451 writebuf, offset, len);
1456 const unsigned char *myaddr = NULL;
1458 fprintf_unfiltered (gdb_stdlog,
1459 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1462 (annex ? annex : "(null)"),
1463 host_address_to_string (readbuf),
1464 host_address_to_string (writebuf),
1465 core_addr_to_string_nz (offset),
1466 plongest (len), plongest (retval));
1472 if (retval > 0 && myaddr != NULL)
1476 fputs_unfiltered (", bytes =", gdb_stdlog);
1477 for (i = 0; i < retval; i++)
1479 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1481 if (targetdebug < 2 && i > 0)
1483 fprintf_unfiltered (gdb_stdlog, " ...");
1486 fprintf_unfiltered (gdb_stdlog, "\n");
1489 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1493 fputc_unfiltered ('\n', gdb_stdlog);
1498 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1499 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1500 if any error occurs.
1502 If an error occurs, no guarantee is made about the contents of the data at
1503 MYADDR. In particular, the caller should not depend upon partial reads
1504 filling the buffer with good data. There is no way for the caller to know
1505 how much good data might have been transfered anyway. Callers that can
1506 deal with partial reads should call target_read (which will retry until
1507 it makes no progress, and then return how much was transferred). */
1510 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1512 /* Dispatch to the topmost target, not the flattened current_target.
1513 Memory accesses check target->to_has_(all_)memory, and the
1514 flattened target doesn't inherit those. */
1515 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1516 myaddr, memaddr, len) == len)
1522 /* Like target_read_memory, but specify explicitly that this is a read from
1523 the target's stack. This may trigger different cache behavior. */
1526 target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1528 /* Dispatch to the topmost target, not the flattened current_target.
1529 Memory accesses check target->to_has_(all_)memory, and the
1530 flattened target doesn't inherit those. */
1532 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1533 myaddr, memaddr, len) == len)
1539 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1540 Returns either 0 for success or an errno value if any error occurs.
1541 If an error occurs, no guarantee is made about how much data got written.
1542 Callers that can deal with partial writes should call target_write. */
1545 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1547 /* Dispatch to the topmost target, not the flattened current_target.
1548 Memory accesses check target->to_has_(all_)memory, and the
1549 flattened target doesn't inherit those. */
1550 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1551 myaddr, memaddr, len) == len)
1557 /* Fetch the target's memory map. */
1560 target_memory_map (void)
1562 VEC(mem_region_s) *result;
1563 struct mem_region *last_one, *this_one;
1565 struct target_ops *t;
1568 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1570 for (t = current_target.beneath; t != NULL; t = t->beneath)
1571 if (t->to_memory_map != NULL)
1577 result = t->to_memory_map (t);
1581 qsort (VEC_address (mem_region_s, result),
1582 VEC_length (mem_region_s, result),
1583 sizeof (struct mem_region), mem_region_cmp);
1585 /* Check that regions do not overlap. Simultaneously assign
1586 a numbering for the "mem" commands to use to refer to
1589 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1591 this_one->number = ix;
1593 if (last_one && last_one->hi > this_one->lo)
1595 warning (_("Overlapping regions in memory map: ignoring"));
1596 VEC_free (mem_region_s, result);
1599 last_one = this_one;
1606 target_flash_erase (ULONGEST address, LONGEST length)
1608 struct target_ops *t;
1610 for (t = current_target.beneath; t != NULL; t = t->beneath)
1611 if (t->to_flash_erase != NULL)
1614 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1615 hex_string (address), phex (length, 0));
1616 t->to_flash_erase (t, address, length);
1624 target_flash_done (void)
1626 struct target_ops *t;
1628 for (t = current_target.beneath; t != NULL; t = t->beneath)
1629 if (t->to_flash_done != NULL)
1632 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1633 t->to_flash_done (t);
1641 show_trust_readonly (struct ui_file *file, int from_tty,
1642 struct cmd_list_element *c, const char *value)
1644 fprintf_filtered (file, _("\
1645 Mode for reading from readonly sections is %s.\n"),
1649 /* More generic transfers. */
1652 default_xfer_partial (struct target_ops *ops, enum target_object object,
1653 const char *annex, gdb_byte *readbuf,
1654 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1656 if (object == TARGET_OBJECT_MEMORY
1657 && ops->deprecated_xfer_memory != NULL)
1658 /* If available, fall back to the target's
1659 "deprecated_xfer_memory" method. */
1663 if (writebuf != NULL)
1665 void *buffer = xmalloc (len);
1666 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1667 memcpy (buffer, writebuf, len);
1668 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1669 1/*write*/, NULL, ops);
1670 do_cleanups (cleanup);
1672 if (readbuf != NULL)
1673 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1674 0/*read*/, NULL, ops);
1677 else if (xfered == 0 && errno == 0)
1678 /* "deprecated_xfer_memory" uses 0, cross checked against
1679 ERRNO as one indication of an error. */
1684 else if (ops->beneath != NULL)
1685 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1686 readbuf, writebuf, offset, len);
1691 /* The xfer_partial handler for the topmost target. Unlike the default,
1692 it does not need to handle memory specially; it just passes all
1693 requests down the stack. */
1696 current_xfer_partial (struct target_ops *ops, enum target_object object,
1697 const char *annex, gdb_byte *readbuf,
1698 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1700 if (ops->beneath != NULL)
1701 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1702 readbuf, writebuf, offset, len);
1707 /* Target vector read/write partial wrapper functions. */
1710 target_read_partial (struct target_ops *ops,
1711 enum target_object object,
1712 const char *annex, gdb_byte *buf,
1713 ULONGEST offset, LONGEST len)
1715 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1719 target_write_partial (struct target_ops *ops,
1720 enum target_object object,
1721 const char *annex, const gdb_byte *buf,
1722 ULONGEST offset, LONGEST len)
1724 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1727 /* Wrappers to perform the full transfer. */
1729 /* For docs on target_read see target.h. */
1732 target_read (struct target_ops *ops,
1733 enum target_object object,
1734 const char *annex, gdb_byte *buf,
1735 ULONGEST offset, LONGEST len)
1738 while (xfered < len)
1740 LONGEST xfer = target_read_partial (ops, object, annex,
1741 (gdb_byte *) buf + xfered,
1742 offset + xfered, len - xfered);
1743 /* Call an observer, notifying them of the xfer progress? */
1755 target_read_until_error (struct target_ops *ops,
1756 enum target_object object,
1757 const char *annex, gdb_byte *buf,
1758 ULONGEST offset, LONGEST len)
1761 while (xfered < len)
1763 LONGEST xfer = target_read_partial (ops, object, annex,
1764 (gdb_byte *) buf + xfered,
1765 offset + xfered, len - xfered);
1766 /* Call an observer, notifying them of the xfer progress? */
1771 /* We've got an error. Try to read in smaller blocks. */
1772 ULONGEST start = offset + xfered;
1773 ULONGEST remaining = len - xfered;
1776 /* If an attempt was made to read a random memory address,
1777 it's likely that the very first byte is not accessible.
1778 Try reading the first byte, to avoid doing log N tries
1780 xfer = target_read_partial (ops, object, annex,
1781 (gdb_byte *) buf + xfered, start, 1);
1790 xfer = target_read_partial (ops, object, annex,
1791 (gdb_byte *) buf + xfered,
1801 /* We have successfully read the first half. So, the
1802 error must be in the second half. Adjust start and
1803 remaining to point at the second half. */
1819 /* An alternative to target_write with progress callbacks. */
1822 target_write_with_progress (struct target_ops *ops,
1823 enum target_object object,
1824 const char *annex, const gdb_byte *buf,
1825 ULONGEST offset, LONGEST len,
1826 void (*progress) (ULONGEST, void *), void *baton)
1830 /* Give the progress callback a chance to set up. */
1832 (*progress) (0, baton);
1834 while (xfered < len)
1836 LONGEST xfer = target_write_partial (ops, object, annex,
1837 (gdb_byte *) buf + xfered,
1838 offset + xfered, len - xfered);
1846 (*progress) (xfer, baton);
1854 /* For docs on target_write see target.h. */
1857 target_write (struct target_ops *ops,
1858 enum target_object object,
1859 const char *annex, const gdb_byte *buf,
1860 ULONGEST offset, LONGEST len)
1862 return target_write_with_progress (ops, object, annex, buf, offset, len,
1866 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1867 the size of the transferred data. PADDING additional bytes are
1868 available in *BUF_P. This is a helper function for
1869 target_read_alloc; see the declaration of that function for more
1873 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1874 const char *annex, gdb_byte **buf_p, int padding)
1876 size_t buf_alloc, buf_pos;
1880 /* This function does not have a length parameter; it reads the
1881 entire OBJECT). Also, it doesn't support objects fetched partly
1882 from one target and partly from another (in a different stratum,
1883 e.g. a core file and an executable). Both reasons make it
1884 unsuitable for reading memory. */
1885 gdb_assert (object != TARGET_OBJECT_MEMORY);
1887 /* Start by reading up to 4K at a time. The target will throttle
1888 this number down if necessary. */
1890 buf = xmalloc (buf_alloc);
1894 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1895 buf_pos, buf_alloc - buf_pos - padding);
1898 /* An error occurred. */
1904 /* Read all there was. */
1914 /* If the buffer is filling up, expand it. */
1915 if (buf_alloc < buf_pos * 2)
1918 buf = xrealloc (buf, buf_alloc);
1925 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1926 the size of the transferred data. See the declaration in "target.h"
1927 function for more information about the return value. */
1930 target_read_alloc (struct target_ops *ops, enum target_object object,
1931 const char *annex, gdb_byte **buf_p)
1933 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1936 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1937 returned as a string, allocated using xmalloc. If an error occurs
1938 or the transfer is unsupported, NULL is returned. Empty objects
1939 are returned as allocated but empty strings. A warning is issued
1940 if the result contains any embedded NUL bytes. */
1943 target_read_stralloc (struct target_ops *ops, enum target_object object,
1947 LONGEST transferred;
1949 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1951 if (transferred < 0)
1954 if (transferred == 0)
1955 return xstrdup ("");
1957 buffer[transferred] = 0;
1958 if (strlen (buffer) < transferred)
1959 warning (_("target object %d, annex %s, "
1960 "contained unexpected null characters"),
1961 (int) object, annex ? annex : "(none)");
1963 return (char *) buffer;
1966 /* Memory transfer methods. */
1969 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1972 /* This method is used to read from an alternate, non-current
1973 target. This read must bypass the overlay support (as symbols
1974 don't match this target), and GDB's internal cache (wrong cache
1975 for this target). */
1976 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
1978 memory_error (EIO, addr);
1982 get_target_memory_unsigned (struct target_ops *ops,
1983 CORE_ADDR addr, int len, enum bfd_endian byte_order)
1985 gdb_byte buf[sizeof (ULONGEST)];
1987 gdb_assert (len <= sizeof (buf));
1988 get_target_memory (ops, addr, buf, len);
1989 return extract_unsigned_integer (buf, len, byte_order);
1993 target_info (char *args, int from_tty)
1995 struct target_ops *t;
1996 int has_all_mem = 0;
1998 if (symfile_objfile != NULL)
1999 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
2001 for (t = target_stack; t != NULL; t = t->beneath)
2003 if (!(*t->to_has_memory) (t))
2006 if ((int) (t->to_stratum) <= (int) dummy_stratum)
2009 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
2010 printf_unfiltered ("%s:\n", t->to_longname);
2011 (t->to_files_info) (t);
2012 has_all_mem = (*t->to_has_all_memory) (t);
2016 /* This function is called before any new inferior is created, e.g.
2017 by running a program, attaching, or connecting to a target.
2018 It cleans up any state from previous invocations which might
2019 change between runs. This is a subset of what target_preopen
2020 resets (things which might change between targets). */
2023 target_pre_inferior (int from_tty)
2025 /* Clear out solib state. Otherwise the solib state of the previous
2026 inferior might have survived and is entirely wrong for the new
2027 target. This has been observed on GNU/Linux using glibc 2.3. How
2039 Cannot access memory at address 0xdeadbeef
2042 /* In some OSs, the shared library list is the same/global/shared
2043 across inferiors. If code is shared between processes, so are
2044 memory regions and features. */
2045 if (!gdbarch_has_global_solist (target_gdbarch))
2047 no_shared_libraries (NULL, from_tty);
2049 invalidate_target_mem_regions ();
2051 target_clear_description ();
2055 /* Callback for iterate_over_inferiors. Gets rid of the given
2059 dispose_inferior (struct inferior *inf, void *args)
2061 struct thread_info *thread;
2063 thread = any_thread_of_process (inf->pid);
2066 switch_to_thread (thread->ptid);
2068 /* Core inferiors actually should be detached, not killed. */
2069 if (target_has_execution)
2072 target_detach (NULL, 0);
2078 /* This is to be called by the open routine before it does
2082 target_preopen (int from_tty)
2086 if (have_inferiors ())
2089 || !have_live_inferiors ()
2090 || query (_("A program is being debugged already. Kill it? ")))
2091 iterate_over_inferiors (dispose_inferior, NULL);
2093 error (_("Program not killed."));
2096 /* Calling target_kill may remove the target from the stack. But if
2097 it doesn't (which seems like a win for UDI), remove it now. */
2098 /* Leave the exec target, though. The user may be switching from a
2099 live process to a core of the same program. */
2100 pop_all_targets_above (file_stratum, 0);
2102 target_pre_inferior (from_tty);
2105 /* Detach a target after doing deferred register stores. */
2108 target_detach (char *args, int from_tty)
2110 struct target_ops* t;
2112 if (gdbarch_has_global_breakpoints (target_gdbarch))
2113 /* Don't remove global breakpoints here. They're removed on
2114 disconnection from the target. */
2117 /* If we're in breakpoints-always-inserted mode, have to remove
2118 them before detaching. */
2119 remove_breakpoints_pid (PIDGET (inferior_ptid));
2121 for (t = current_target.beneath; t != NULL; t = t->beneath)
2123 if (t->to_detach != NULL)
2125 t->to_detach (t, args, from_tty);
2127 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2133 internal_error (__FILE__, __LINE__, "could not find a target to detach");
2137 target_disconnect (char *args, int from_tty)
2139 struct target_ops *t;
2141 /* If we're in breakpoints-always-inserted mode or if breakpoints
2142 are global across processes, we have to remove them before
2144 remove_breakpoints ();
2146 for (t = current_target.beneath; t != NULL; t = t->beneath)
2147 if (t->to_disconnect != NULL)
2150 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2152 t->to_disconnect (t, args, from_tty);
2160 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
2162 struct target_ops *t;
2164 for (t = current_target.beneath; t != NULL; t = t->beneath)
2166 if (t->to_wait != NULL)
2168 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2172 char *status_string;
2174 status_string = target_waitstatus_to_string (status);
2175 fprintf_unfiltered (gdb_stdlog,
2176 "target_wait (%d, status) = %d, %s\n",
2177 PIDGET (ptid), PIDGET (retval),
2179 xfree (status_string);
2190 target_pid_to_str (ptid_t ptid)
2192 struct target_ops *t;
2194 for (t = current_target.beneath; t != NULL; t = t->beneath)
2196 if (t->to_pid_to_str != NULL)
2197 return (*t->to_pid_to_str) (t, ptid);
2200 return normal_pid_to_str (ptid);
2204 target_resume (ptid_t ptid, int step, enum target_signal signal)
2206 struct target_ops *t;
2208 target_dcache_invalidate ();
2210 for (t = current_target.beneath; t != NULL; t = t->beneath)
2212 if (t->to_resume != NULL)
2214 t->to_resume (t, ptid, step, signal);
2216 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2218 step ? "step" : "continue",
2219 target_signal_to_name (signal));
2221 set_executing (ptid, 1);
2222 set_running (ptid, 1);
2223 clear_inline_frame_state (ptid);
2230 /* Look through the list of possible targets for a target that can
2234 target_follow_fork (int follow_child)
2236 struct target_ops *t;
2238 for (t = current_target.beneath; t != NULL; t = t->beneath)
2240 if (t->to_follow_fork != NULL)
2242 int retval = t->to_follow_fork (t, follow_child);
2244 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2245 follow_child, retval);
2250 /* Some target returned a fork event, but did not know how to follow it. */
2251 internal_error (__FILE__, __LINE__,
2252 "could not find a target to follow fork");
2256 target_mourn_inferior (void)
2258 struct target_ops *t;
2259 for (t = current_target.beneath; t != NULL; t = t->beneath)
2261 if (t->to_mourn_inferior != NULL)
2263 t->to_mourn_inferior (t);
2265 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2267 /* We no longer need to keep handles on any of the object files.
2268 Make sure to release them to avoid unnecessarily locking any
2269 of them while we're not actually debugging. */
2270 bfd_cache_close_all ();
2276 internal_error (__FILE__, __LINE__,
2277 "could not find a target to follow mourn inferiour");
2280 /* Look for a target which can describe architectural features, starting
2281 from TARGET. If we find one, return its description. */
2283 const struct target_desc *
2284 target_read_description (struct target_ops *target)
2286 struct target_ops *t;
2288 for (t = target; t != NULL; t = t->beneath)
2289 if (t->to_read_description != NULL)
2291 const struct target_desc *tdesc;
2293 tdesc = t->to_read_description (t);
2301 /* The default implementation of to_search_memory.
2302 This implements a basic search of memory, reading target memory and
2303 performing the search here (as opposed to performing the search in on the
2304 target side with, for example, gdbserver). */
2307 simple_search_memory (struct target_ops *ops,
2308 CORE_ADDR start_addr, ULONGEST search_space_len,
2309 const gdb_byte *pattern, ULONGEST pattern_len,
2310 CORE_ADDR *found_addrp)
2312 /* NOTE: also defined in find.c testcase. */
2313 #define SEARCH_CHUNK_SIZE 16000
2314 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2315 /* Buffer to hold memory contents for searching. */
2316 gdb_byte *search_buf;
2317 unsigned search_buf_size;
2318 struct cleanup *old_cleanups;
2320 search_buf_size = chunk_size + pattern_len - 1;
2322 /* No point in trying to allocate a buffer larger than the search space. */
2323 if (search_space_len < search_buf_size)
2324 search_buf_size = search_space_len;
2326 search_buf = malloc (search_buf_size);
2327 if (search_buf == NULL)
2328 error (_("Unable to allocate memory to perform the search."));
2329 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2331 /* Prime the search buffer. */
2333 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2334 search_buf, start_addr, search_buf_size) != search_buf_size)
2336 warning (_("Unable to access target memory at %s, halting search."),
2337 hex_string (start_addr));
2338 do_cleanups (old_cleanups);
2342 /* Perform the search.
2344 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2345 When we've scanned N bytes we copy the trailing bytes to the start and
2346 read in another N bytes. */
2348 while (search_space_len >= pattern_len)
2350 gdb_byte *found_ptr;
2351 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2353 found_ptr = memmem (search_buf, nr_search_bytes,
2354 pattern, pattern_len);
2356 if (found_ptr != NULL)
2358 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2359 *found_addrp = found_addr;
2360 do_cleanups (old_cleanups);
2364 /* Not found in this chunk, skip to next chunk. */
2366 /* Don't let search_space_len wrap here, it's unsigned. */
2367 if (search_space_len >= chunk_size)
2368 search_space_len -= chunk_size;
2370 search_space_len = 0;
2372 if (search_space_len >= pattern_len)
2374 unsigned keep_len = search_buf_size - chunk_size;
2375 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
2378 /* Copy the trailing part of the previous iteration to the front
2379 of the buffer for the next iteration. */
2380 gdb_assert (keep_len == pattern_len - 1);
2381 memcpy (search_buf, search_buf + chunk_size, keep_len);
2383 nr_to_read = min (search_space_len - keep_len, chunk_size);
2385 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2386 search_buf + keep_len, read_addr,
2387 nr_to_read) != nr_to_read)
2389 warning (_("Unable to access target memory at %s, halting search."),
2390 hex_string (read_addr));
2391 do_cleanups (old_cleanups);
2395 start_addr += chunk_size;
2401 do_cleanups (old_cleanups);
2405 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2406 sequence of bytes in PATTERN with length PATTERN_LEN.
2408 The result is 1 if found, 0 if not found, and -1 if there was an error
2409 requiring halting of the search (e.g. memory read error).
2410 If the pattern is found the address is recorded in FOUND_ADDRP. */
2413 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2414 const gdb_byte *pattern, ULONGEST pattern_len,
2415 CORE_ADDR *found_addrp)
2417 struct target_ops *t;
2420 /* We don't use INHERIT to set current_target.to_search_memory,
2421 so we have to scan the target stack and handle targetdebug
2425 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2426 hex_string (start_addr));
2428 for (t = current_target.beneath; t != NULL; t = t->beneath)
2429 if (t->to_search_memory != NULL)
2434 found = t->to_search_memory (t, start_addr, search_space_len,
2435 pattern, pattern_len, found_addrp);
2439 /* If a special version of to_search_memory isn't available, use the
2441 found = simple_search_memory (current_target.beneath,
2442 start_addr, search_space_len,
2443 pattern, pattern_len, found_addrp);
2447 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2452 /* Look through the currently pushed targets. If none of them will
2453 be able to restart the currently running process, issue an error
2457 target_require_runnable (void)
2459 struct target_ops *t;
2461 for (t = target_stack; t != NULL; t = t->beneath)
2463 /* If this target knows how to create a new program, then
2464 assume we will still be able to after killing the current
2465 one. Either killing and mourning will not pop T, or else
2466 find_default_run_target will find it again. */
2467 if (t->to_create_inferior != NULL)
2470 /* Do not worry about thread_stratum targets that can not
2471 create inferiors. Assume they will be pushed again if
2472 necessary, and continue to the process_stratum. */
2473 if (t->to_stratum == thread_stratum
2474 || t->to_stratum == arch_stratum)
2478 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2482 /* This function is only called if the target is running. In that
2483 case there should have been a process_stratum target and it
2484 should either know how to create inferiors, or not... */
2485 internal_error (__FILE__, __LINE__, "No targets found");
2488 /* Look through the list of possible targets for a target that can
2489 execute a run or attach command without any other data. This is
2490 used to locate the default process stratum.
2492 If DO_MESG is not NULL, the result is always valid (error() is
2493 called for errors); else, return NULL on error. */
2495 static struct target_ops *
2496 find_default_run_target (char *do_mesg)
2498 struct target_ops **t;
2499 struct target_ops *runable = NULL;
2504 for (t = target_structs; t < target_structs + target_struct_size;
2507 if ((*t)->to_can_run && target_can_run (*t))
2517 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2526 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2528 struct target_ops *t;
2530 t = find_default_run_target ("attach");
2531 (t->to_attach) (t, args, from_tty);
2536 find_default_create_inferior (struct target_ops *ops,
2537 char *exec_file, char *allargs, char **env,
2540 struct target_ops *t;
2542 t = find_default_run_target ("run");
2543 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2548 find_default_can_async_p (void)
2550 struct target_ops *t;
2552 /* This may be called before the target is pushed on the stack;
2553 look for the default process stratum. If there's none, gdb isn't
2554 configured with a native debugger, and target remote isn't
2556 t = find_default_run_target (NULL);
2557 if (t && t->to_can_async_p)
2558 return (t->to_can_async_p) ();
2563 find_default_is_async_p (void)
2565 struct target_ops *t;
2567 /* This may be called before the target is pushed on the stack;
2568 look for the default process stratum. If there's none, gdb isn't
2569 configured with a native debugger, and target remote isn't
2571 t = find_default_run_target (NULL);
2572 if (t && t->to_is_async_p)
2573 return (t->to_is_async_p) ();
2578 find_default_supports_non_stop (void)
2580 struct target_ops *t;
2582 t = find_default_run_target (NULL);
2583 if (t && t->to_supports_non_stop)
2584 return (t->to_supports_non_stop) ();
2589 target_supports_non_stop (void)
2591 struct target_ops *t;
2592 for (t = ¤t_target; t != NULL; t = t->beneath)
2593 if (t->to_supports_non_stop)
2594 return t->to_supports_non_stop ();
2601 target_get_osdata (const char *type)
2604 struct target_ops *t;
2606 /* If we're already connected to something that can get us OS
2607 related data, use it. Otherwise, try using the native
2609 if (current_target.to_stratum >= process_stratum)
2610 t = current_target.beneath;
2612 t = find_default_run_target ("get OS data");
2617 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
2620 /* Determine the current address space of thread PTID. */
2622 struct address_space *
2623 target_thread_address_space (ptid_t ptid)
2625 struct address_space *aspace;
2626 struct inferior *inf;
2627 struct target_ops *t;
2629 for (t = current_target.beneath; t != NULL; t = t->beneath)
2631 if (t->to_thread_address_space != NULL)
2633 aspace = t->to_thread_address_space (t, ptid);
2634 gdb_assert (aspace);
2637 fprintf_unfiltered (gdb_stdlog,
2638 "target_thread_address_space (%s) = %d\n",
2639 target_pid_to_str (ptid),
2640 address_space_num (aspace));
2645 /* Fall-back to the "main" address space of the inferior. */
2646 inf = find_inferior_pid (ptid_get_pid (ptid));
2648 if (inf == NULL || inf->aspace == NULL)
2649 internal_error (__FILE__, __LINE__, "\
2650 Can't determine the current address space of thread %s\n",
2651 target_pid_to_str (ptid));
2657 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2659 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
2663 default_watchpoint_addr_within_range (struct target_ops *target,
2665 CORE_ADDR start, int length)
2667 return addr >= start && addr < start + length;
2670 static struct gdbarch *
2671 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2673 return target_gdbarch;
2689 return_minus_one (void)
2694 /* Find a single runnable target in the stack and return it. If for
2695 some reason there is more than one, return NULL. */
2698 find_run_target (void)
2700 struct target_ops **t;
2701 struct target_ops *runable = NULL;
2706 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2708 if ((*t)->to_can_run && target_can_run (*t))
2715 return (count == 1 ? runable : NULL);
2718 /* Find a single core_stratum target in the list of targets and return it.
2719 If for some reason there is more than one, return NULL. */
2722 find_core_target (void)
2724 struct target_ops **t;
2725 struct target_ops *runable = NULL;
2730 for (t = target_structs; t < target_structs + target_struct_size;
2733 if ((*t)->to_stratum == core_stratum)
2740 return (count == 1 ? runable : NULL);
2744 * Find the next target down the stack from the specified target.
2748 find_target_beneath (struct target_ops *t)
2754 /* The inferior process has died. Long live the inferior! */
2757 generic_mourn_inferior (void)
2761 ptid = inferior_ptid;
2762 inferior_ptid = null_ptid;
2764 if (!ptid_equal (ptid, null_ptid))
2766 int pid = ptid_get_pid (ptid);
2767 exit_inferior (pid);
2770 breakpoint_init_inferior (inf_exited);
2771 registers_changed ();
2773 reopen_exec_file ();
2774 reinit_frame_cache ();
2776 if (deprecated_detach_hook)
2777 deprecated_detach_hook ();
2780 /* Helper function for child_wait and the derivatives of child_wait.
2781 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2782 translation of that in OURSTATUS. */
2784 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2786 if (WIFEXITED (hoststatus))
2788 ourstatus->kind = TARGET_WAITKIND_EXITED;
2789 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2791 else if (!WIFSTOPPED (hoststatus))
2793 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2794 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2798 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2799 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2803 /* Convert a normal process ID to a string. Returns the string in a
2807 normal_pid_to_str (ptid_t ptid)
2809 static char buf[32];
2811 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2816 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2818 return normal_pid_to_str (ptid);
2821 /* Error-catcher for target_find_memory_regions. */
2823 dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2825 error (_("Command not implemented for this target."));
2829 /* Error-catcher for target_make_corefile_notes. */
2831 dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2833 error (_("Command not implemented for this target."));
2837 /* Error-catcher for target_get_bookmark. */
2839 dummy_get_bookmark (char *ignore1, int ignore2)
2845 /* Error-catcher for target_goto_bookmark. */
2847 dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
2852 /* Set up the handful of non-empty slots needed by the dummy target
2856 init_dummy_target (void)
2858 dummy_target.to_shortname = "None";
2859 dummy_target.to_longname = "None";
2860 dummy_target.to_doc = "";
2861 dummy_target.to_attach = find_default_attach;
2862 dummy_target.to_detach =
2863 (void (*)(struct target_ops *, char *, int))target_ignore;
2864 dummy_target.to_create_inferior = find_default_create_inferior;
2865 dummy_target.to_can_async_p = find_default_can_async_p;
2866 dummy_target.to_is_async_p = find_default_is_async_p;
2867 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
2868 dummy_target.to_pid_to_str = dummy_pid_to_str;
2869 dummy_target.to_stratum = dummy_stratum;
2870 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2871 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2872 dummy_target.to_get_bookmark = dummy_get_bookmark;
2873 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
2874 dummy_target.to_xfer_partial = default_xfer_partial;
2875 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2876 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2877 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2878 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2879 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
2880 dummy_target.to_magic = OPS_MAGIC;
2884 debug_to_open (char *args, int from_tty)
2886 debug_target.to_open (args, from_tty);
2888 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2892 target_close (struct target_ops *targ, int quitting)
2894 if (targ->to_xclose != NULL)
2895 targ->to_xclose (targ, quitting);
2896 else if (targ->to_close != NULL)
2897 targ->to_close (quitting);
2900 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2904 target_attach (char *args, int from_tty)
2906 struct target_ops *t;
2907 for (t = current_target.beneath; t != NULL; t = t->beneath)
2909 if (t->to_attach != NULL)
2911 t->to_attach (t, args, from_tty);
2913 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2919 internal_error (__FILE__, __LINE__,
2920 "could not find a target to attach");
2924 target_thread_alive (ptid_t ptid)
2926 struct target_ops *t;
2927 for (t = current_target.beneath; t != NULL; t = t->beneath)
2929 if (t->to_thread_alive != NULL)
2933 retval = t->to_thread_alive (t, ptid);
2935 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2936 PIDGET (ptid), retval);
2946 target_find_new_threads (void)
2948 struct target_ops *t;
2949 for (t = current_target.beneath; t != NULL; t = t->beneath)
2951 if (t->to_find_new_threads != NULL)
2953 t->to_find_new_threads (t);
2955 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2963 debug_to_post_attach (int pid)
2965 debug_target.to_post_attach (pid);
2967 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2970 /* Return a pretty printed form of target_waitstatus.
2971 Space for the result is malloc'd, caller must free. */
2974 target_waitstatus_to_string (const struct target_waitstatus *ws)
2976 const char *kind_str = "status->kind = ";
2980 case TARGET_WAITKIND_EXITED:
2981 return xstrprintf ("%sexited, status = %d",
2982 kind_str, ws->value.integer);
2983 case TARGET_WAITKIND_STOPPED:
2984 return xstrprintf ("%sstopped, signal = %s",
2985 kind_str, target_signal_to_name (ws->value.sig));
2986 case TARGET_WAITKIND_SIGNALLED:
2987 return xstrprintf ("%ssignalled, signal = %s",
2988 kind_str, target_signal_to_name (ws->value.sig));
2989 case TARGET_WAITKIND_LOADED:
2990 return xstrprintf ("%sloaded", kind_str);
2991 case TARGET_WAITKIND_FORKED:
2992 return xstrprintf ("%sforked", kind_str);
2993 case TARGET_WAITKIND_VFORKED:
2994 return xstrprintf ("%svforked", kind_str);
2995 case TARGET_WAITKIND_EXECD:
2996 return xstrprintf ("%sexecd", kind_str);
2997 case TARGET_WAITKIND_SYSCALL_ENTRY:
2998 return xstrprintf ("%sentered syscall", kind_str);
2999 case TARGET_WAITKIND_SYSCALL_RETURN:
3000 return xstrprintf ("%sexited syscall", kind_str);
3001 case TARGET_WAITKIND_SPURIOUS:
3002 return xstrprintf ("%sspurious", kind_str);
3003 case TARGET_WAITKIND_IGNORE:
3004 return xstrprintf ("%signore", kind_str);
3005 case TARGET_WAITKIND_NO_HISTORY:
3006 return xstrprintf ("%sno-history", kind_str);
3008 return xstrprintf ("%sunknown???", kind_str);
3013 debug_print_register (const char * func,
3014 struct regcache *regcache, int regno)
3016 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3017 fprintf_unfiltered (gdb_stdlog, "%s ", func);
3018 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
3019 && gdbarch_register_name (gdbarch, regno) != NULL
3020 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3021 fprintf_unfiltered (gdb_stdlog, "(%s)",
3022 gdbarch_register_name (gdbarch, regno));
3024 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
3025 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
3027 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3028 int i, size = register_size (gdbarch, regno);
3029 unsigned char buf[MAX_REGISTER_SIZE];
3030 regcache_raw_collect (regcache, regno, buf);
3031 fprintf_unfiltered (gdb_stdlog, " = ");
3032 for (i = 0; i < size; i++)
3034 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3036 if (size <= sizeof (LONGEST))
3038 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
3039 fprintf_unfiltered (gdb_stdlog, " %s %s",
3040 core_addr_to_string_nz (val), plongest (val));
3043 fprintf_unfiltered (gdb_stdlog, "\n");
3047 target_fetch_registers (struct regcache *regcache, int regno)
3049 struct target_ops *t;
3050 for (t = current_target.beneath; t != NULL; t = t->beneath)
3052 if (t->to_fetch_registers != NULL)
3054 t->to_fetch_registers (t, regcache, regno);
3056 debug_print_register ("target_fetch_registers", regcache, regno);
3063 target_store_registers (struct regcache *regcache, int regno)
3066 struct target_ops *t;
3067 for (t = current_target.beneath; t != NULL; t = t->beneath)
3069 if (t->to_store_registers != NULL)
3071 t->to_store_registers (t, regcache, regno);
3074 debug_print_register ("target_store_registers", regcache, regno);
3084 target_core_of_thread (ptid_t ptid)
3086 struct target_ops *t;
3088 for (t = current_target.beneath; t != NULL; t = t->beneath)
3090 if (t->to_core_of_thread != NULL)
3092 int retval = t->to_core_of_thread (t, ptid);
3094 fprintf_unfiltered (gdb_stdlog, "target_core_of_thread (%d) = %d\n",
3095 PIDGET (ptid), retval);
3104 debug_to_prepare_to_store (struct regcache *regcache)
3106 debug_target.to_prepare_to_store (regcache);
3108 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
3112 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
3113 int write, struct mem_attrib *attrib,
3114 struct target_ops *target)
3118 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3121 fprintf_unfiltered (gdb_stdlog,
3122 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
3123 paddress (target_gdbarch, memaddr), len,
3124 write ? "write" : "read", retval);
3130 fputs_unfiltered (", bytes =", gdb_stdlog);
3131 for (i = 0; i < retval; i++)
3133 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
3135 if (targetdebug < 2 && i > 0)
3137 fprintf_unfiltered (gdb_stdlog, " ...");
3140 fprintf_unfiltered (gdb_stdlog, "\n");
3143 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
3147 fputc_unfiltered ('\n', gdb_stdlog);
3153 debug_to_files_info (struct target_ops *target)
3155 debug_target.to_files_info (target);
3157 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
3161 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3162 struct bp_target_info *bp_tgt)
3166 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
3168 fprintf_unfiltered (gdb_stdlog,
3169 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
3170 (unsigned long) bp_tgt->placed_address,
3171 (unsigned long) retval);
3176 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3177 struct bp_target_info *bp_tgt)
3181 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
3183 fprintf_unfiltered (gdb_stdlog,
3184 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
3185 (unsigned long) bp_tgt->placed_address,
3186 (unsigned long) retval);
3191 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3195 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3197 fprintf_unfiltered (gdb_stdlog,
3198 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3199 (unsigned long) type,
3200 (unsigned long) cnt,
3201 (unsigned long) from_tty,
3202 (unsigned long) retval);
3207 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3211 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3213 fprintf_unfiltered (gdb_stdlog,
3214 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
3215 (unsigned long) addr,
3216 (unsigned long) len,
3217 (unsigned long) retval);
3222 debug_to_stopped_by_watchpoint (void)
3226 retval = debug_target.to_stopped_by_watchpoint ();
3228 fprintf_unfiltered (gdb_stdlog,
3229 "target_stopped_by_watchpoint () = %ld\n",
3230 (unsigned long) retval);
3235 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
3239 retval = debug_target.to_stopped_data_address (target, addr);
3241 fprintf_unfiltered (gdb_stdlog,
3242 "target_stopped_data_address ([0x%lx]) = %ld\n",
3243 (unsigned long)*addr,
3244 (unsigned long)retval);
3249 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3251 CORE_ADDR start, int length)
3255 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3258 fprintf_filtered (gdb_stdlog,
3259 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3260 (unsigned long) addr, (unsigned long) start, length,
3266 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3267 struct bp_target_info *bp_tgt)
3271 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3273 fprintf_unfiltered (gdb_stdlog,
3274 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3275 (unsigned long) bp_tgt->placed_address,
3276 (unsigned long) retval);
3281 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3282 struct bp_target_info *bp_tgt)
3286 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3288 fprintf_unfiltered (gdb_stdlog,
3289 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3290 (unsigned long) bp_tgt->placed_address,
3291 (unsigned long) retval);
3296 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3300 retval = debug_target.to_insert_watchpoint (addr, len, type);
3302 fprintf_unfiltered (gdb_stdlog,
3303 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3304 (unsigned long) addr, len, type, (unsigned long) retval);
3309 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3313 retval = debug_target.to_remove_watchpoint (addr, len, type);
3315 fprintf_unfiltered (gdb_stdlog,
3316 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3317 (unsigned long) addr, len, type, (unsigned long) retval);
3322 debug_to_terminal_init (void)
3324 debug_target.to_terminal_init ();
3326 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3330 debug_to_terminal_inferior (void)
3332 debug_target.to_terminal_inferior ();
3334 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3338 debug_to_terminal_ours_for_output (void)
3340 debug_target.to_terminal_ours_for_output ();
3342 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3346 debug_to_terminal_ours (void)
3348 debug_target.to_terminal_ours ();
3350 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3354 debug_to_terminal_save_ours (void)
3356 debug_target.to_terminal_save_ours ();
3358 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3362 debug_to_terminal_info (char *arg, int from_tty)
3364 debug_target.to_terminal_info (arg, from_tty);
3366 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3371 debug_to_load (char *args, int from_tty)
3373 debug_target.to_load (args, from_tty);
3375 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3379 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
3383 retval = debug_target.to_lookup_symbol (name, addrp);
3385 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
3391 debug_to_post_startup_inferior (ptid_t ptid)
3393 debug_target.to_post_startup_inferior (ptid);
3395 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3400 debug_to_acknowledge_created_inferior (int pid)
3402 debug_target.to_acknowledge_created_inferior (pid);
3404 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
3409 debug_to_insert_fork_catchpoint (int pid)
3411 debug_target.to_insert_fork_catchpoint (pid);
3413 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3418 debug_to_remove_fork_catchpoint (int pid)
3422 retval = debug_target.to_remove_fork_catchpoint (pid);
3424 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3431 debug_to_insert_vfork_catchpoint (int pid)
3433 debug_target.to_insert_vfork_catchpoint (pid);
3435 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3440 debug_to_remove_vfork_catchpoint (int pid)
3444 retval = debug_target.to_remove_vfork_catchpoint (pid);
3446 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
3453 debug_to_insert_exec_catchpoint (int pid)
3455 debug_target.to_insert_exec_catchpoint (pid);
3457 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3462 debug_to_remove_exec_catchpoint (int pid)
3466 retval = debug_target.to_remove_exec_catchpoint (pid);
3468 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
3475 debug_to_has_exited (int pid, int wait_status, int *exit_status)
3479 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3481 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
3482 pid, wait_status, *exit_status, has_exited);
3488 debug_to_can_run (void)
3492 retval = debug_target.to_can_run ();
3494 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
3500 debug_to_notice_signals (ptid_t ptid)
3502 debug_target.to_notice_signals (ptid);
3504 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3508 static struct gdbarch *
3509 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3511 struct gdbarch *retval;
3513 retval = debug_target.to_thread_architecture (ops, ptid);
3515 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %s [%s]\n",
3516 target_pid_to_str (ptid), host_address_to_string (retval),
3517 gdbarch_bfd_arch_info (retval)->printable_name);
3522 debug_to_stop (ptid_t ptid)
3524 debug_target.to_stop (ptid);
3526 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3527 target_pid_to_str (ptid));
3531 debug_to_rcmd (char *command,
3532 struct ui_file *outbuf)
3534 debug_target.to_rcmd (command, outbuf);
3535 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3539 debug_to_pid_to_exec_file (int pid)
3543 exec_file = debug_target.to_pid_to_exec_file (pid);
3545 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
3552 setup_target_debug (void)
3554 memcpy (&debug_target, ¤t_target, sizeof debug_target);
3556 current_target.to_open = debug_to_open;
3557 current_target.to_post_attach = debug_to_post_attach;
3558 current_target.to_prepare_to_store = debug_to_prepare_to_store;
3559 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
3560 current_target.to_files_info = debug_to_files_info;
3561 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3562 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
3563 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3564 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3565 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3566 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3567 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3568 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3569 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3570 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
3571 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
3572 current_target.to_terminal_init = debug_to_terminal_init;
3573 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3574 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3575 current_target.to_terminal_ours = debug_to_terminal_ours;
3576 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
3577 current_target.to_terminal_info = debug_to_terminal_info;
3578 current_target.to_load = debug_to_load;
3579 current_target.to_lookup_symbol = debug_to_lookup_symbol;
3580 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3581 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
3582 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3583 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3584 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3585 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
3586 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3587 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
3588 current_target.to_has_exited = debug_to_has_exited;
3589 current_target.to_can_run = debug_to_can_run;
3590 current_target.to_notice_signals = debug_to_notice_signals;
3591 current_target.to_stop = debug_to_stop;
3592 current_target.to_rcmd = debug_to_rcmd;
3593 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
3594 current_target.to_thread_architecture = debug_to_thread_architecture;
3598 static char targ_desc[] =
3599 "Names of targets and files being debugged.\n\
3600 Shows the entire stack of targets currently in use (including the exec-file,\n\
3601 core-file, and process, if any), as well as the symbol file name.";
3604 do_monitor_command (char *cmd,
3607 if ((current_target.to_rcmd
3608 == (void (*) (char *, struct ui_file *)) tcomplain)
3609 || (current_target.to_rcmd == debug_to_rcmd
3610 && (debug_target.to_rcmd
3611 == (void (*) (char *, struct ui_file *)) tcomplain)))
3612 error (_("\"monitor\" command not supported by this target."));
3613 target_rcmd (cmd, gdb_stdtarg);
3616 /* Print the name of each layers of our target stack. */
3619 maintenance_print_target_stack (char *cmd, int from_tty)
3621 struct target_ops *t;
3623 printf_filtered (_("The current target stack is:\n"));
3625 for (t = target_stack; t != NULL; t = t->beneath)
3627 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3631 /* Controls if async mode is permitted. */
3632 int target_async_permitted = 0;
3634 /* The set command writes to this variable. If the inferior is
3635 executing, linux_nat_async_permitted is *not* updated. */
3636 static int target_async_permitted_1 = 0;
3639 set_maintenance_target_async_permitted (char *args, int from_tty,
3640 struct cmd_list_element *c)
3642 if (have_live_inferiors ())
3644 target_async_permitted_1 = target_async_permitted;
3645 error (_("Cannot change this setting while the inferior is running."));
3648 target_async_permitted = target_async_permitted_1;
3652 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3653 struct cmd_list_element *c,
3656 fprintf_filtered (file, _("\
3657 Controlling the inferior in asynchronous mode is %s.\n"), value);
3661 initialize_targets (void)
3663 init_dummy_target ();
3664 push_target (&dummy_target);
3666 add_info ("target", target_info, targ_desc);
3667 add_info ("files", target_info, targ_desc);
3669 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3670 Set target debugging."), _("\
3671 Show target debugging."), _("\
3672 When non-zero, target debugging is enabled. Higher numbers are more\n\
3673 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3677 &setdebuglist, &showdebuglist);
3679 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
3680 &trust_readonly, _("\
3681 Set mode for reading from readonly sections."), _("\
3682 Show mode for reading from readonly sections."), _("\
3683 When this mode is on, memory reads from readonly sections (such as .text)\n\
3684 will be read from the object file instead of from the target. This will\n\
3685 result in significant performance improvement for remote targets."),
3687 show_trust_readonly,
3688 &setlist, &showlist);
3690 add_com ("monitor", class_obscure, do_monitor_command,
3691 _("Send a command to the remote monitor (remote targets only)."));
3693 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3694 _("Print the name of each layer of the internal target stack."),
3695 &maintenanceprintlist);
3697 add_setshow_boolean_cmd ("target-async", no_class,
3698 &target_async_permitted_1, _("\
3699 Set whether gdb controls the inferior in asynchronous mode."), _("\
3700 Show whether gdb controls the inferior in asynchronous mode."), _("\
3701 Tells gdb whether to control the inferior in asynchronous mode."),
3702 set_maintenance_target_async_permitted,
3703 show_maintenance_target_async_permitted,
3707 add_setshow_boolean_cmd ("stack-cache", class_support,
3708 &stack_cache_enabled_p_1, _("\
3709 Set cache use for stack access."), _("\
3710 Show cache use for stack access."), _("\
3711 When on, use the data cache for all stack access, regardless of any\n\
3712 configured memory regions. This improves remote performance significantly.\n\
3713 By default, caching for stack access is on."),
3714 set_stack_cache_enabled_p,
3715 show_stack_cache_enabled_p,
3716 &setlist, &showlist);
3718 target_dcache = dcache_init ();