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, 2011
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"
46 #include "tracepoint.h"
48 static void target_info (char *, 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 void tcomplain (void) ATTRIBUTE_NORETURN;
59 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
61 static int return_zero (void);
63 static int return_one (void);
65 static int return_minus_one (void);
67 void target_ignore (void);
69 static void target_command (char *, int);
71 static struct target_ops *find_default_run_target (char *);
73 static LONGEST default_xfer_partial (struct target_ops *ops,
74 enum target_object object,
75 const char *annex, gdb_byte *readbuf,
76 const gdb_byte *writebuf,
77 ULONGEST offset, LONGEST len);
79 static LONGEST current_xfer_partial (struct target_ops *ops,
80 enum target_object object,
81 const char *annex, gdb_byte *readbuf,
82 const gdb_byte *writebuf,
83 ULONGEST offset, LONGEST len);
85 static LONGEST target_xfer_partial (struct target_ops *ops,
86 enum target_object object,
88 void *readbuf, const void *writebuf,
89 ULONGEST offset, LONGEST len);
91 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
94 static void init_dummy_target (void);
96 static struct target_ops debug_target;
98 static void debug_to_open (char *, int);
100 static void debug_to_prepare_to_store (struct regcache *);
102 static void debug_to_files_info (struct target_ops *);
104 static int debug_to_insert_breakpoint (struct gdbarch *,
105 struct bp_target_info *);
107 static int debug_to_remove_breakpoint (struct gdbarch *,
108 struct bp_target_info *);
110 static int debug_to_can_use_hw_breakpoint (int, int, int);
112 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
115 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
116 struct bp_target_info *);
118 static int debug_to_insert_watchpoint (CORE_ADDR, int, int,
119 struct expression *);
121 static int debug_to_remove_watchpoint (CORE_ADDR, int, int,
122 struct expression *);
124 static int debug_to_stopped_by_watchpoint (void);
126 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
128 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
129 CORE_ADDR, CORE_ADDR, int);
131 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
133 static int debug_to_can_accel_watchpoint_condition (CORE_ADDR, int, int,
134 struct expression *);
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_can_run (void);
152 static void debug_to_stop (ptid_t);
154 /* Pointer to array of target architecture structures; the size of the
155 array; the current index into the array; the allocated size of the
157 struct target_ops **target_structs;
158 unsigned target_struct_size;
159 unsigned target_struct_index;
160 unsigned target_struct_allocsize;
161 #define DEFAULT_ALLOCSIZE 10
163 /* The initial current target, so that there is always a semi-valid
166 static struct target_ops dummy_target;
168 /* Top of target stack. */
170 static struct target_ops *target_stack;
172 /* The target structure we are currently using to talk to a process
173 or file or whatever "inferior" we have. */
175 struct target_ops current_target;
177 /* Command list for target. */
179 static struct cmd_list_element *targetlist = NULL;
181 /* Nonzero if we should trust readonly sections from the
182 executable when reading memory. */
184 static int trust_readonly = 0;
186 /* Nonzero if we should show true memory content including
187 memory breakpoint inserted by gdb. */
189 static int show_memory_breakpoints = 0;
191 /* These globals control whether GDB attempts to perform these
192 operations; they are useful for targets that need to prevent
193 inadvertant disruption, such as in non-stop mode. */
195 int may_write_registers = 1;
197 int may_write_memory = 1;
199 int may_insert_breakpoints = 1;
201 int may_insert_tracepoints = 1;
203 int may_insert_fast_tracepoints = 1;
207 /* Non-zero if we want to see trace of target level stuff. */
209 static int targetdebug = 0;
211 show_targetdebug (struct ui_file *file, int from_tty,
212 struct cmd_list_element *c, const char *value)
214 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
217 static void setup_target_debug (void);
219 /* The option sets this. */
220 static int stack_cache_enabled_p_1 = 1;
221 /* And set_stack_cache_enabled_p updates this.
222 The reason for the separation is so that we don't flush the cache for
223 on->on transitions. */
224 static int stack_cache_enabled_p = 1;
226 /* This is called *after* the stack-cache has been set.
227 Flush the cache for off->on and on->off transitions.
228 There's no real need to flush the cache for on->off transitions,
229 except cleanliness. */
232 set_stack_cache_enabled_p (char *args, int from_tty,
233 struct cmd_list_element *c)
235 if (stack_cache_enabled_p != stack_cache_enabled_p_1)
236 target_dcache_invalidate ();
238 stack_cache_enabled_p = stack_cache_enabled_p_1;
242 show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
243 struct cmd_list_element *c, const char *value)
245 fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
248 /* Cache of memory operations, to speed up remote access. */
249 static DCACHE *target_dcache;
251 /* Invalidate the target dcache. */
254 target_dcache_invalidate (void)
256 dcache_invalidate (target_dcache);
259 /* The user just typed 'target' without the name of a target. */
262 target_command (char *arg, int from_tty)
264 fputs_filtered ("Argument required (target name). Try `help target'\n",
268 /* Default target_has_* methods for process_stratum targets. */
271 default_child_has_all_memory (struct target_ops *ops)
273 /* If no inferior selected, then we can't read memory here. */
274 if (ptid_equal (inferior_ptid, null_ptid))
281 default_child_has_memory (struct target_ops *ops)
283 /* If no inferior selected, then we can't read memory here. */
284 if (ptid_equal (inferior_ptid, null_ptid))
291 default_child_has_stack (struct target_ops *ops)
293 /* If no inferior selected, there's no stack. */
294 if (ptid_equal (inferior_ptid, null_ptid))
301 default_child_has_registers (struct target_ops *ops)
303 /* Can't read registers from no inferior. */
304 if (ptid_equal (inferior_ptid, null_ptid))
311 default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
313 /* If there's no thread selected, then we can't make it run through
315 if (ptid_equal (the_ptid, null_ptid))
323 target_has_all_memory_1 (void)
325 struct target_ops *t;
327 for (t = current_target.beneath; t != NULL; t = t->beneath)
328 if (t->to_has_all_memory (t))
335 target_has_memory_1 (void)
337 struct target_ops *t;
339 for (t = current_target.beneath; t != NULL; t = t->beneath)
340 if (t->to_has_memory (t))
347 target_has_stack_1 (void)
349 struct target_ops *t;
351 for (t = current_target.beneath; t != NULL; t = t->beneath)
352 if (t->to_has_stack (t))
359 target_has_registers_1 (void)
361 struct target_ops *t;
363 for (t = current_target.beneath; t != NULL; t = t->beneath)
364 if (t->to_has_registers (t))
371 target_has_execution_1 (ptid_t the_ptid)
373 struct target_ops *t;
375 for (t = current_target.beneath; t != NULL; t = t->beneath)
376 if (t->to_has_execution (t, the_ptid))
383 target_has_execution_current (void)
385 return target_has_execution_1 (inferior_ptid);
388 /* Add a possible target architecture to the list. */
391 add_target (struct target_ops *t)
393 /* Provide default values for all "must have" methods. */
394 if (t->to_xfer_partial == NULL)
395 t->to_xfer_partial = default_xfer_partial;
397 if (t->to_has_all_memory == NULL)
398 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
400 if (t->to_has_memory == NULL)
401 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
403 if (t->to_has_stack == NULL)
404 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
406 if (t->to_has_registers == NULL)
407 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
409 if (t->to_has_execution == NULL)
410 t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
414 target_struct_allocsize = DEFAULT_ALLOCSIZE;
415 target_structs = (struct target_ops **) xmalloc
416 (target_struct_allocsize * sizeof (*target_structs));
418 if (target_struct_size >= target_struct_allocsize)
420 target_struct_allocsize *= 2;
421 target_structs = (struct target_ops **)
422 xrealloc ((char *) target_structs,
423 target_struct_allocsize * sizeof (*target_structs));
425 target_structs[target_struct_size++] = t;
427 if (targetlist == NULL)
428 add_prefix_cmd ("target", class_run, target_command, _("\
429 Connect to a target machine or process.\n\
430 The first argument is the type or protocol of the target machine.\n\
431 Remaining arguments are interpreted by the target protocol. For more\n\
432 information on the arguments for a particular protocol, type\n\
433 `help target ' followed by the protocol name."),
434 &targetlist, "target ", 0, &cmdlist);
435 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
448 struct target_ops *t;
450 for (t = current_target.beneath; t != NULL; t = t->beneath)
451 if (t->to_kill != NULL)
454 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
464 target_load (char *arg, int from_tty)
466 target_dcache_invalidate ();
467 (*current_target.to_load) (arg, from_tty);
471 target_create_inferior (char *exec_file, char *args,
472 char **env, int from_tty)
474 struct target_ops *t;
476 for (t = current_target.beneath; t != NULL; t = t->beneath)
478 if (t->to_create_inferior != NULL)
480 t->to_create_inferior (t, exec_file, args, env, from_tty);
482 fprintf_unfiltered (gdb_stdlog,
483 "target_create_inferior (%s, %s, xxx, %d)\n",
484 exec_file, args, from_tty);
489 internal_error (__FILE__, __LINE__,
490 _("could not find a target to create inferior"));
494 target_terminal_inferior (void)
496 /* A background resume (``run&'') should leave GDB in control of the
497 terminal. Use target_can_async_p, not target_is_async_p, since at
498 this point the target is not async yet. However, if sync_execution
499 is not set, we know it will become async prior to resume. */
500 if (target_can_async_p () && !sync_execution)
503 /* If GDB is resuming the inferior in the foreground, install
504 inferior's terminal modes. */
505 (*current_target.to_terminal_inferior) ();
509 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
510 struct target_ops *t)
512 errno = EIO; /* Can't read/write this location. */
513 return 0; /* No bytes handled. */
519 error (_("You can't do that when your target is `%s'"),
520 current_target.to_shortname);
526 error (_("You can't do that without a process to debug."));
530 default_terminal_info (char *args, int from_tty)
532 printf_unfiltered (_("No saved terminal information.\n"));
535 /* A default implementation for the to_get_ada_task_ptid target method.
537 This function builds the PTID by using both LWP and TID as part of
538 the PTID lwp and tid elements. The pid used is the pid of the
542 default_get_ada_task_ptid (long lwp, long tid)
544 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
547 static enum exec_direction_kind
548 default_execution_direction (void)
550 if (!target_can_execute_reverse)
552 else if (!target_can_async_p ())
555 gdb_assert_not_reached ("\
556 to_execution_direction must be implemented for reverse async");
559 /* Go through the target stack from top to bottom, copying over zero
560 entries in current_target, then filling in still empty entries. In
561 effect, we are doing class inheritance through the pushed target
564 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
565 is currently implemented, is that it discards any knowledge of
566 which target an inherited method originally belonged to.
567 Consequently, new new target methods should instead explicitly and
568 locally search the target stack for the target that can handle the
572 update_current_target (void)
574 struct target_ops *t;
576 /* First, reset current's contents. */
577 memset (¤t_target, 0, sizeof (current_target));
579 #define INHERIT(FIELD, TARGET) \
580 if (!current_target.FIELD) \
581 current_target.FIELD = (TARGET)->FIELD
583 for (t = target_stack; t; t = t->beneath)
585 INHERIT (to_shortname, t);
586 INHERIT (to_longname, t);
588 /* Do not inherit to_open. */
589 /* Do not inherit to_close. */
590 /* Do not inherit to_attach. */
591 INHERIT (to_post_attach, t);
592 INHERIT (to_attach_no_wait, t);
593 /* Do not inherit to_detach. */
594 /* Do not inherit to_disconnect. */
595 /* Do not inherit to_resume. */
596 /* Do not inherit to_wait. */
597 /* Do not inherit to_fetch_registers. */
598 /* Do not inherit to_store_registers. */
599 INHERIT (to_prepare_to_store, t);
600 INHERIT (deprecated_xfer_memory, t);
601 INHERIT (to_files_info, t);
602 INHERIT (to_insert_breakpoint, t);
603 INHERIT (to_remove_breakpoint, t);
604 INHERIT (to_can_use_hw_breakpoint, t);
605 INHERIT (to_insert_hw_breakpoint, t);
606 INHERIT (to_remove_hw_breakpoint, t);
607 /* Do not inherit to_ranged_break_num_registers. */
608 INHERIT (to_insert_watchpoint, t);
609 INHERIT (to_remove_watchpoint, t);
610 /* Do not inherit to_insert_mask_watchpoint. */
611 /* Do not inherit to_remove_mask_watchpoint. */
612 INHERIT (to_stopped_data_address, t);
613 INHERIT (to_have_steppable_watchpoint, t);
614 INHERIT (to_have_continuable_watchpoint, t);
615 INHERIT (to_stopped_by_watchpoint, t);
616 INHERIT (to_watchpoint_addr_within_range, t);
617 INHERIT (to_region_ok_for_hw_watchpoint, t);
618 INHERIT (to_can_accel_watchpoint_condition, t);
619 /* Do not inherit to_masked_watch_num_registers. */
620 INHERIT (to_terminal_init, t);
621 INHERIT (to_terminal_inferior, t);
622 INHERIT (to_terminal_ours_for_output, t);
623 INHERIT (to_terminal_ours, t);
624 INHERIT (to_terminal_save_ours, t);
625 INHERIT (to_terminal_info, t);
626 /* Do not inherit to_kill. */
627 INHERIT (to_load, t);
628 /* Do no inherit to_create_inferior. */
629 INHERIT (to_post_startup_inferior, t);
630 INHERIT (to_insert_fork_catchpoint, t);
631 INHERIT (to_remove_fork_catchpoint, t);
632 INHERIT (to_insert_vfork_catchpoint, t);
633 INHERIT (to_remove_vfork_catchpoint, t);
634 /* Do not inherit to_follow_fork. */
635 INHERIT (to_insert_exec_catchpoint, t);
636 INHERIT (to_remove_exec_catchpoint, t);
637 INHERIT (to_set_syscall_catchpoint, t);
638 INHERIT (to_has_exited, t);
639 /* Do not inherit to_mourn_inferior. */
640 INHERIT (to_can_run, t);
641 /* Do not inherit to_pass_signals. */
642 /* Do not inherit to_thread_alive. */
643 /* Do not inherit to_find_new_threads. */
644 /* Do not inherit to_pid_to_str. */
645 INHERIT (to_extra_thread_info, t);
646 INHERIT (to_thread_name, t);
647 INHERIT (to_stop, t);
648 /* Do not inherit to_xfer_partial. */
649 INHERIT (to_rcmd, t);
650 INHERIT (to_pid_to_exec_file, t);
651 INHERIT (to_log_command, t);
652 INHERIT (to_stratum, t);
653 /* Do not inherit to_has_all_memory. */
654 /* Do not inherit to_has_memory. */
655 /* Do not inherit to_has_stack. */
656 /* Do not inherit to_has_registers. */
657 /* Do not inherit to_has_execution. */
658 INHERIT (to_has_thread_control, t);
659 INHERIT (to_can_async_p, t);
660 INHERIT (to_is_async_p, t);
661 INHERIT (to_async, t);
662 INHERIT (to_find_memory_regions, t);
663 INHERIT (to_make_corefile_notes, t);
664 INHERIT (to_get_bookmark, t);
665 INHERIT (to_goto_bookmark, t);
666 /* Do not inherit to_get_thread_local_address. */
667 INHERIT (to_can_execute_reverse, t);
668 INHERIT (to_execution_direction, t);
669 INHERIT (to_thread_architecture, t);
670 /* Do not inherit to_read_description. */
671 INHERIT (to_get_ada_task_ptid, t);
672 /* Do not inherit to_search_memory. */
673 INHERIT (to_supports_multi_process, t);
674 INHERIT (to_supports_enable_disable_tracepoint, t);
675 INHERIT (to_supports_string_tracing, t);
676 INHERIT (to_trace_init, t);
677 INHERIT (to_download_tracepoint, t);
678 INHERIT (to_can_download_tracepoint, t);
679 INHERIT (to_download_trace_state_variable, t);
680 INHERIT (to_enable_tracepoint, t);
681 INHERIT (to_disable_tracepoint, t);
682 INHERIT (to_trace_set_readonly_regions, t);
683 INHERIT (to_trace_start, t);
684 INHERIT (to_get_trace_status, t);
685 INHERIT (to_trace_stop, t);
686 INHERIT (to_trace_find, t);
687 INHERIT (to_get_trace_state_variable_value, t);
688 INHERIT (to_save_trace_data, t);
689 INHERIT (to_upload_tracepoints, t);
690 INHERIT (to_upload_trace_state_variables, t);
691 INHERIT (to_get_raw_trace_data, t);
692 INHERIT (to_set_disconnected_tracing, t);
693 INHERIT (to_set_circular_trace_buffer, t);
694 INHERIT (to_get_tib_address, t);
695 INHERIT (to_set_permissions, t);
696 INHERIT (to_static_tracepoint_marker_at, t);
697 INHERIT (to_static_tracepoint_markers_by_strid, t);
698 INHERIT (to_traceframe_info, t);
699 INHERIT (to_magic, t);
700 /* Do not inherit to_memory_map. */
701 /* Do not inherit to_flash_erase. */
702 /* Do not inherit to_flash_done. */
706 /* Clean up a target struct so it no longer has any zero pointers in
707 it. Some entries are defaulted to a method that print an error,
708 others are hard-wired to a standard recursive default. */
710 #define de_fault(field, value) \
711 if (!current_target.field) \
712 current_target.field = value
715 (void (*) (char *, int))
720 de_fault (to_post_attach,
723 de_fault (to_prepare_to_store,
724 (void (*) (struct regcache *))
726 de_fault (deprecated_xfer_memory,
727 (int (*) (CORE_ADDR, gdb_byte *, int, int,
728 struct mem_attrib *, struct target_ops *))
730 de_fault (to_files_info,
731 (void (*) (struct target_ops *))
733 de_fault (to_insert_breakpoint,
734 memory_insert_breakpoint);
735 de_fault (to_remove_breakpoint,
736 memory_remove_breakpoint);
737 de_fault (to_can_use_hw_breakpoint,
738 (int (*) (int, int, int))
740 de_fault (to_insert_hw_breakpoint,
741 (int (*) (struct gdbarch *, struct bp_target_info *))
743 de_fault (to_remove_hw_breakpoint,
744 (int (*) (struct gdbarch *, struct bp_target_info *))
746 de_fault (to_insert_watchpoint,
747 (int (*) (CORE_ADDR, int, int, struct expression *))
749 de_fault (to_remove_watchpoint,
750 (int (*) (CORE_ADDR, int, int, struct expression *))
752 de_fault (to_stopped_by_watchpoint,
755 de_fault (to_stopped_data_address,
756 (int (*) (struct target_ops *, CORE_ADDR *))
758 de_fault (to_watchpoint_addr_within_range,
759 default_watchpoint_addr_within_range);
760 de_fault (to_region_ok_for_hw_watchpoint,
761 default_region_ok_for_hw_watchpoint);
762 de_fault (to_can_accel_watchpoint_condition,
763 (int (*) (CORE_ADDR, int, int, struct expression *))
765 de_fault (to_terminal_init,
768 de_fault (to_terminal_inferior,
771 de_fault (to_terminal_ours_for_output,
774 de_fault (to_terminal_ours,
777 de_fault (to_terminal_save_ours,
780 de_fault (to_terminal_info,
781 default_terminal_info);
783 (void (*) (char *, int))
785 de_fault (to_post_startup_inferior,
788 de_fault (to_insert_fork_catchpoint,
791 de_fault (to_remove_fork_catchpoint,
794 de_fault (to_insert_vfork_catchpoint,
797 de_fault (to_remove_vfork_catchpoint,
800 de_fault (to_insert_exec_catchpoint,
803 de_fault (to_remove_exec_catchpoint,
806 de_fault (to_set_syscall_catchpoint,
807 (int (*) (int, int, int, int, int *))
809 de_fault (to_has_exited,
810 (int (*) (int, int, int *))
812 de_fault (to_can_run,
814 de_fault (to_extra_thread_info,
815 (char *(*) (struct thread_info *))
817 de_fault (to_thread_name,
818 (char *(*) (struct thread_info *))
823 current_target.to_xfer_partial = current_xfer_partial;
825 (void (*) (char *, struct ui_file *))
827 de_fault (to_pid_to_exec_file,
831 (void (*) (void (*) (enum inferior_event_type, void*), void*))
833 de_fault (to_thread_architecture,
834 default_thread_architecture);
835 current_target.to_read_description = NULL;
836 de_fault (to_get_ada_task_ptid,
837 (ptid_t (*) (long, long))
838 default_get_ada_task_ptid);
839 de_fault (to_supports_multi_process,
842 de_fault (to_supports_enable_disable_tracepoint,
845 de_fault (to_supports_string_tracing,
848 de_fault (to_trace_init,
851 de_fault (to_download_tracepoint,
852 (void (*) (struct bp_location *))
854 de_fault (to_can_download_tracepoint,
857 de_fault (to_download_trace_state_variable,
858 (void (*) (struct trace_state_variable *))
860 de_fault (to_enable_tracepoint,
861 (void (*) (struct bp_location *))
863 de_fault (to_disable_tracepoint,
864 (void (*) (struct bp_location *))
866 de_fault (to_trace_set_readonly_regions,
869 de_fault (to_trace_start,
872 de_fault (to_get_trace_status,
873 (int (*) (struct trace_status *))
875 de_fault (to_trace_stop,
878 de_fault (to_trace_find,
879 (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
881 de_fault (to_get_trace_state_variable_value,
882 (int (*) (int, LONGEST *))
884 de_fault (to_save_trace_data,
885 (int (*) (const char *))
887 de_fault (to_upload_tracepoints,
888 (int (*) (struct uploaded_tp **))
890 de_fault (to_upload_trace_state_variables,
891 (int (*) (struct uploaded_tsv **))
893 de_fault (to_get_raw_trace_data,
894 (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
896 de_fault (to_set_disconnected_tracing,
899 de_fault (to_set_circular_trace_buffer,
902 de_fault (to_get_tib_address,
903 (int (*) (ptid_t, CORE_ADDR *))
905 de_fault (to_set_permissions,
908 de_fault (to_static_tracepoint_marker_at,
909 (int (*) (CORE_ADDR, struct static_tracepoint_marker *))
911 de_fault (to_static_tracepoint_markers_by_strid,
912 (VEC(static_tracepoint_marker_p) * (*) (const char *))
914 de_fault (to_traceframe_info,
915 (struct traceframe_info * (*) (void))
917 de_fault (to_execution_direction, default_execution_direction);
921 /* Finally, position the target-stack beneath the squashed
922 "current_target". That way code looking for a non-inherited
923 target method can quickly and simply find it. */
924 current_target.beneath = target_stack;
927 setup_target_debug ();
930 /* Push a new target type into the stack of the existing target accessors,
931 possibly superseding some of the existing accessors.
933 Rather than allow an empty stack, we always have the dummy target at
934 the bottom stratum, so we can call the function vectors without
938 push_target (struct target_ops *t)
940 struct target_ops **cur;
942 /* Check magic number. If wrong, it probably means someone changed
943 the struct definition, but not all the places that initialize one. */
944 if (t->to_magic != OPS_MAGIC)
946 fprintf_unfiltered (gdb_stderr,
947 "Magic number of %s target struct wrong\n",
949 internal_error (__FILE__, __LINE__,
950 _("failed internal consistency check"));
953 /* Find the proper stratum to install this target in. */
954 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
956 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
960 /* If there's already targets at this stratum, remove them. */
961 /* FIXME: cagney/2003-10-15: I think this should be popping all
962 targets to CUR, and not just those at this stratum level. */
963 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
965 /* There's already something at this stratum level. Close it,
966 and un-hook it from the stack. */
967 struct target_ops *tmp = (*cur);
969 (*cur) = (*cur)->beneath;
971 target_close (tmp, 0);
974 /* We have removed all targets in our stratum, now add the new one. */
978 update_current_target ();
981 /* Remove a target_ops vector from the stack, wherever it may be.
982 Return how many times it was removed (0 or 1). */
985 unpush_target (struct target_ops *t)
987 struct target_ops **cur;
988 struct target_ops *tmp;
990 if (t->to_stratum == dummy_stratum)
991 internal_error (__FILE__, __LINE__,
992 _("Attempt to unpush the dummy target"));
994 /* Look for the specified target. Note that we assume that a target
995 can only occur once in the target stack. */
997 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1004 return 0; /* Didn't find target_ops, quit now. */
1006 /* NOTE: cagney/2003-12-06: In '94 the close call was made
1007 unconditional by moving it to before the above check that the
1008 target was in the target stack (something about "Change the way
1009 pushing and popping of targets work to support target overlays
1010 and inheritance"). This doesn't make much sense - only open
1011 targets should be closed. */
1012 target_close (t, 0);
1014 /* Unchain the target. */
1016 (*cur) = (*cur)->beneath;
1017 tmp->beneath = NULL;
1019 update_current_target ();
1027 target_close (target_stack, 0); /* Let it clean up. */
1028 if (unpush_target (target_stack) == 1)
1031 fprintf_unfiltered (gdb_stderr,
1032 "pop_target couldn't find target %s\n",
1033 current_target.to_shortname);
1034 internal_error (__FILE__, __LINE__,
1035 _("failed internal consistency check"));
1039 pop_all_targets_above (enum strata above_stratum, int quitting)
1041 while ((int) (current_target.to_stratum) > (int) above_stratum)
1043 target_close (target_stack, quitting);
1044 if (!unpush_target (target_stack))
1046 fprintf_unfiltered (gdb_stderr,
1047 "pop_all_targets couldn't find target %s\n",
1048 target_stack->to_shortname);
1049 internal_error (__FILE__, __LINE__,
1050 _("failed internal consistency check"));
1057 pop_all_targets (int quitting)
1059 pop_all_targets_above (dummy_stratum, quitting);
1062 /* Return 1 if T is now pushed in the target stack. Return 0 otherwise. */
1065 target_is_pushed (struct target_ops *t)
1067 struct target_ops **cur;
1069 /* Check magic number. If wrong, it probably means someone changed
1070 the struct definition, but not all the places that initialize one. */
1071 if (t->to_magic != OPS_MAGIC)
1073 fprintf_unfiltered (gdb_stderr,
1074 "Magic number of %s target struct wrong\n",
1076 internal_error (__FILE__, __LINE__,
1077 _("failed internal consistency check"));
1080 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1087 /* Using the objfile specified in OBJFILE, find the address for the
1088 current thread's thread-local storage with offset OFFSET. */
1090 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1092 volatile CORE_ADDR addr = 0;
1093 struct target_ops *target;
1095 for (target = current_target.beneath;
1097 target = target->beneath)
1099 if (target->to_get_thread_local_address != NULL)
1104 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
1106 ptid_t ptid = inferior_ptid;
1107 volatile struct gdb_exception ex;
1109 TRY_CATCH (ex, RETURN_MASK_ALL)
1113 /* Fetch the load module address for this objfile. */
1114 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
1116 /* If it's 0, throw the appropriate exception. */
1118 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1119 _("TLS load module not found"));
1121 addr = target->to_get_thread_local_address (target, ptid,
1124 /* If an error occurred, print TLS related messages here. Otherwise,
1125 throw the error to some higher catcher. */
1128 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1132 case TLS_NO_LIBRARY_SUPPORT_ERROR:
1133 error (_("Cannot find thread-local variables "
1134 "in this thread library."));
1136 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1137 if (objfile_is_library)
1138 error (_("Cannot find shared library `%s' in dynamic"
1139 " linker's load module list"), objfile->name);
1141 error (_("Cannot find executable file `%s' in dynamic"
1142 " linker's load module list"), objfile->name);
1144 case TLS_NOT_ALLOCATED_YET_ERROR:
1145 if (objfile_is_library)
1146 error (_("The inferior has not yet allocated storage for"
1147 " thread-local variables in\n"
1148 "the shared library `%s'\n"
1150 objfile->name, target_pid_to_str (ptid));
1152 error (_("The inferior has not yet allocated storage for"
1153 " thread-local variables in\n"
1154 "the executable `%s'\n"
1156 objfile->name, target_pid_to_str (ptid));
1158 case TLS_GENERIC_ERROR:
1159 if (objfile_is_library)
1160 error (_("Cannot find thread-local storage for %s, "
1161 "shared library %s:\n%s"),
1162 target_pid_to_str (ptid),
1163 objfile->name, ex.message);
1165 error (_("Cannot find thread-local storage for %s, "
1166 "executable file %s:\n%s"),
1167 target_pid_to_str (ptid),
1168 objfile->name, ex.message);
1171 throw_exception (ex);
1176 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1177 TLS is an ABI-specific thing. But we don't do that yet. */
1179 error (_("Cannot find thread-local variables on this target"));
1185 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1187 /* target_read_string -- read a null terminated string, up to LEN bytes,
1188 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1189 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1190 is responsible for freeing it. Return the number of bytes successfully
1194 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1196 int tlen, origlen, offset, i;
1200 int buffer_allocated;
1202 unsigned int nbytes_read = 0;
1204 gdb_assert (string);
1206 /* Small for testing. */
1207 buffer_allocated = 4;
1208 buffer = xmalloc (buffer_allocated);
1215 tlen = MIN (len, 4 - (memaddr & 3));
1216 offset = memaddr & 3;
1218 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1221 /* The transfer request might have crossed the boundary to an
1222 unallocated region of memory. Retry the transfer, requesting
1226 errcode = target_read_memory (memaddr, buf, 1);
1231 if (bufptr - buffer + tlen > buffer_allocated)
1235 bytes = bufptr - buffer;
1236 buffer_allocated *= 2;
1237 buffer = xrealloc (buffer, buffer_allocated);
1238 bufptr = buffer + bytes;
1241 for (i = 0; i < tlen; i++)
1243 *bufptr++ = buf[i + offset];
1244 if (buf[i + offset] == '\000')
1246 nbytes_read += i + 1;
1253 nbytes_read += tlen;
1262 struct target_section_table *
1263 target_get_section_table (struct target_ops *target)
1265 struct target_ops *t;
1268 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1270 for (t = target; t != NULL; t = t->beneath)
1271 if (t->to_get_section_table != NULL)
1272 return (*t->to_get_section_table) (t);
1277 /* Find a section containing ADDR. */
1279 struct target_section *
1280 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1282 struct target_section_table *table = target_get_section_table (target);
1283 struct target_section *secp;
1288 for (secp = table->sections; secp < table->sections_end; secp++)
1290 if (addr >= secp->addr && addr < secp->endaddr)
1296 /* Read memory from the live target, even if currently inspecting a
1297 traceframe. The return is the same as that of target_read. */
1300 target_read_live_memory (enum target_object object,
1301 ULONGEST memaddr, gdb_byte *myaddr, LONGEST len)
1304 struct cleanup *cleanup;
1306 /* Switch momentarily out of tfind mode so to access live memory.
1307 Note that this must not clear global state, such as the frame
1308 cache, which must still remain valid for the previous traceframe.
1309 We may be _building_ the frame cache at this point. */
1310 cleanup = make_cleanup_restore_traceframe_number ();
1311 set_traceframe_number (-1);
1313 ret = target_read (current_target.beneath, object, NULL,
1314 myaddr, memaddr, len);
1316 do_cleanups (cleanup);
1320 /* Using the set of read-only target sections of OPS, read live
1321 read-only memory. Note that the actual reads start from the
1322 top-most target again.
1324 For interface/parameters/return description see target.h,
1328 memory_xfer_live_readonly_partial (struct target_ops *ops,
1329 enum target_object object,
1330 gdb_byte *readbuf, ULONGEST memaddr,
1333 struct target_section *secp;
1334 struct target_section_table *table;
1336 secp = target_section_by_addr (ops, memaddr);
1338 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1341 struct target_section *p;
1342 ULONGEST memend = memaddr + len;
1344 table = target_get_section_table (ops);
1346 for (p = table->sections; p < table->sections_end; p++)
1348 if (memaddr >= p->addr)
1350 if (memend <= p->endaddr)
1352 /* Entire transfer is within this section. */
1353 return target_read_live_memory (object, memaddr,
1356 else if (memaddr >= p->endaddr)
1358 /* This section ends before the transfer starts. */
1363 /* This section overlaps the transfer. Just do half. */
1364 len = p->endaddr - memaddr;
1365 return target_read_live_memory (object, memaddr,
1375 /* Perform a partial memory transfer.
1376 For docs see target.h, to_xfer_partial. */
1379 memory_xfer_partial (struct target_ops *ops, enum target_object object,
1380 void *readbuf, const void *writebuf, ULONGEST memaddr,
1385 struct mem_region *region;
1386 struct inferior *inf;
1388 /* Zero length requests are ok and require no work. */
1392 /* For accesses to unmapped overlay sections, read directly from
1393 files. Must do this first, as MEMADDR may need adjustment. */
1394 if (readbuf != NULL && overlay_debugging)
1396 struct obj_section *section = find_pc_overlay (memaddr);
1398 if (pc_in_unmapped_range (memaddr, section))
1400 struct target_section_table *table
1401 = target_get_section_table (ops);
1402 const char *section_name = section->the_bfd_section->name;
1404 memaddr = overlay_mapped_address (memaddr, section);
1405 return section_table_xfer_memory_partial (readbuf, writebuf,
1408 table->sections_end,
1413 /* Try the executable files, if "trust-readonly-sections" is set. */
1414 if (readbuf != NULL && trust_readonly)
1416 struct target_section *secp;
1417 struct target_section_table *table;
1419 secp = target_section_by_addr (ops, memaddr);
1421 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1424 table = target_get_section_table (ops);
1425 return section_table_xfer_memory_partial (readbuf, writebuf,
1428 table->sections_end,
1433 /* If reading unavailable memory in the context of traceframes, and
1434 this address falls within a read-only section, fallback to
1435 reading from live memory. */
1436 if (readbuf != NULL && get_traceframe_number () != -1)
1438 VEC(mem_range_s) *available;
1440 /* If we fail to get the set of available memory, then the
1441 target does not support querying traceframe info, and so we
1442 attempt reading from the traceframe anyway (assuming the
1443 target implements the old QTro packet then). */
1444 if (traceframe_available_memory (&available, memaddr, len))
1446 struct cleanup *old_chain;
1448 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
1450 if (VEC_empty (mem_range_s, available)
1451 || VEC_index (mem_range_s, available, 0)->start != memaddr)
1453 /* Don't read into the traceframe's available
1455 if (!VEC_empty (mem_range_s, available))
1457 LONGEST oldlen = len;
1459 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
1460 gdb_assert (len <= oldlen);
1463 do_cleanups (old_chain);
1465 /* This goes through the topmost target again. */
1466 res = memory_xfer_live_readonly_partial (ops, object,
1467 readbuf, memaddr, len);
1471 /* No use trying further, we know some memory starting
1472 at MEMADDR isn't available. */
1476 /* Don't try to read more than how much is available, in
1477 case the target implements the deprecated QTro packet to
1478 cater for older GDBs (the target's knowledge of read-only
1479 sections may be outdated by now). */
1480 len = VEC_index (mem_range_s, available, 0)->length;
1482 do_cleanups (old_chain);
1486 /* Try GDB's internal data cache. */
1487 region = lookup_mem_region (memaddr);
1488 /* region->hi == 0 means there's no upper bound. */
1489 if (memaddr + len < region->hi || region->hi == 0)
1492 reg_len = region->hi - memaddr;
1494 switch (region->attrib.mode)
1497 if (writebuf != NULL)
1502 if (readbuf != NULL)
1507 /* We only support writing to flash during "load" for now. */
1508 if (writebuf != NULL)
1509 error (_("Writing to flash memory forbidden in this context"));
1516 if (!ptid_equal (inferior_ptid, null_ptid))
1517 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1522 /* The dcache reads whole cache lines; that doesn't play well
1523 with reading from a trace buffer, because reading outside of
1524 the collected memory range fails. */
1525 && get_traceframe_number () == -1
1526 && (region->attrib.cache
1527 || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
1529 if (readbuf != NULL)
1530 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
1533 /* FIXME drow/2006-08-09: If we're going to preserve const
1534 correctness dcache_xfer_memory should take readbuf and
1536 res = dcache_xfer_memory (ops, target_dcache, memaddr,
1543 if (readbuf && !show_memory_breakpoints)
1544 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1549 /* If none of those methods found the memory we wanted, fall back
1550 to a target partial transfer. Normally a single call to
1551 to_xfer_partial is enough; if it doesn't recognize an object
1552 it will call the to_xfer_partial of the next target down.
1553 But for memory this won't do. Memory is the only target
1554 object which can be read from more than one valid target.
1555 A core file, for instance, could have some of memory but
1556 delegate other bits to the target below it. So, we must
1557 manually try all targets. */
1561 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1562 readbuf, writebuf, memaddr, reg_len);
1566 /* We want to continue past core files to executables, but not
1567 past a running target's memory. */
1568 if (ops->to_has_all_memory (ops))
1573 while (ops != NULL);
1575 if (res > 0 && readbuf != NULL && !show_memory_breakpoints)
1576 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1578 /* Make sure the cache gets updated no matter what - if we are writing
1579 to the stack. Even if this write is not tagged as such, we still need
1580 to update the cache. */
1585 && !region->attrib.cache
1586 && stack_cache_enabled_p
1587 && object != TARGET_OBJECT_STACK_MEMORY)
1589 dcache_update (target_dcache, memaddr, (void *) writebuf, res);
1592 /* If we still haven't got anything, return the last error. We
1598 restore_show_memory_breakpoints (void *arg)
1600 show_memory_breakpoints = (uintptr_t) arg;
1604 make_show_memory_breakpoints_cleanup (int show)
1606 int current = show_memory_breakpoints;
1608 show_memory_breakpoints = show;
1609 return make_cleanup (restore_show_memory_breakpoints,
1610 (void *) (uintptr_t) current);
1613 /* For docs see target.h, to_xfer_partial. */
1616 target_xfer_partial (struct target_ops *ops,
1617 enum target_object object, const char *annex,
1618 void *readbuf, const void *writebuf,
1619 ULONGEST offset, LONGEST len)
1623 gdb_assert (ops->to_xfer_partial != NULL);
1625 if (writebuf && !may_write_memory)
1626 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1627 core_addr_to_string_nz (offset), plongest (len));
1629 /* If this is a memory transfer, let the memory-specific code
1630 have a look at it instead. Memory transfers are more
1632 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1633 retval = memory_xfer_partial (ops, object, readbuf,
1634 writebuf, offset, len);
1637 enum target_object raw_object = object;
1639 /* If this is a raw memory transfer, request the normal
1640 memory object from other layers. */
1641 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1642 raw_object = TARGET_OBJECT_MEMORY;
1644 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1645 writebuf, offset, len);
1650 const unsigned char *myaddr = NULL;
1652 fprintf_unfiltered (gdb_stdlog,
1653 "%s:target_xfer_partial "
1654 "(%d, %s, %s, %s, %s, %s) = %s",
1657 (annex ? annex : "(null)"),
1658 host_address_to_string (readbuf),
1659 host_address_to_string (writebuf),
1660 core_addr_to_string_nz (offset),
1661 plongest (len), plongest (retval));
1667 if (retval > 0 && myaddr != NULL)
1671 fputs_unfiltered (", bytes =", gdb_stdlog);
1672 for (i = 0; i < retval; i++)
1674 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1676 if (targetdebug < 2 && i > 0)
1678 fprintf_unfiltered (gdb_stdlog, " ...");
1681 fprintf_unfiltered (gdb_stdlog, "\n");
1684 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1688 fputc_unfiltered ('\n', gdb_stdlog);
1693 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1694 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1695 if any error occurs.
1697 If an error occurs, no guarantee is made about the contents of the data at
1698 MYADDR. In particular, the caller should not depend upon partial reads
1699 filling the buffer with good data. There is no way for the caller to know
1700 how much good data might have been transfered anyway. Callers that can
1701 deal with partial reads should call target_read (which will retry until
1702 it makes no progress, and then return how much was transferred). */
1705 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1707 /* Dispatch to the topmost target, not the flattened current_target.
1708 Memory accesses check target->to_has_(all_)memory, and the
1709 flattened target doesn't inherit those. */
1710 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1711 myaddr, memaddr, len) == len)
1717 /* Like target_read_memory, but specify explicitly that this is a read from
1718 the target's stack. This may trigger different cache behavior. */
1721 target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1723 /* Dispatch to the topmost target, not the flattened current_target.
1724 Memory accesses check target->to_has_(all_)memory, and the
1725 flattened target doesn't inherit those. */
1727 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1728 myaddr, memaddr, len) == len)
1734 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1735 Returns either 0 for success or an errno value if any error occurs.
1736 If an error occurs, no guarantee is made about how much data got written.
1737 Callers that can deal with partial writes should call target_write. */
1740 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1742 /* Dispatch to the topmost target, not the flattened current_target.
1743 Memory accesses check target->to_has_(all_)memory, and the
1744 flattened target doesn't inherit those. */
1745 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1746 myaddr, memaddr, len) == len)
1752 /* Fetch the target's memory map. */
1755 target_memory_map (void)
1757 VEC(mem_region_s) *result;
1758 struct mem_region *last_one, *this_one;
1760 struct target_ops *t;
1763 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1765 for (t = current_target.beneath; t != NULL; t = t->beneath)
1766 if (t->to_memory_map != NULL)
1772 result = t->to_memory_map (t);
1776 qsort (VEC_address (mem_region_s, result),
1777 VEC_length (mem_region_s, result),
1778 sizeof (struct mem_region), mem_region_cmp);
1780 /* Check that regions do not overlap. Simultaneously assign
1781 a numbering for the "mem" commands to use to refer to
1784 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1786 this_one->number = ix;
1788 if (last_one && last_one->hi > this_one->lo)
1790 warning (_("Overlapping regions in memory map: ignoring"));
1791 VEC_free (mem_region_s, result);
1794 last_one = this_one;
1801 target_flash_erase (ULONGEST address, LONGEST length)
1803 struct target_ops *t;
1805 for (t = current_target.beneath; t != NULL; t = t->beneath)
1806 if (t->to_flash_erase != NULL)
1809 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1810 hex_string (address), phex (length, 0));
1811 t->to_flash_erase (t, address, length);
1819 target_flash_done (void)
1821 struct target_ops *t;
1823 for (t = current_target.beneath; t != NULL; t = t->beneath)
1824 if (t->to_flash_done != NULL)
1827 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1828 t->to_flash_done (t);
1836 show_trust_readonly (struct ui_file *file, int from_tty,
1837 struct cmd_list_element *c, const char *value)
1839 fprintf_filtered (file,
1840 _("Mode for reading from readonly sections is %s.\n"),
1844 /* More generic transfers. */
1847 default_xfer_partial (struct target_ops *ops, enum target_object object,
1848 const char *annex, gdb_byte *readbuf,
1849 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1851 if (object == TARGET_OBJECT_MEMORY
1852 && ops->deprecated_xfer_memory != NULL)
1853 /* If available, fall back to the target's
1854 "deprecated_xfer_memory" method. */
1859 if (writebuf != NULL)
1861 void *buffer = xmalloc (len);
1862 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1864 memcpy (buffer, writebuf, len);
1865 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1866 1/*write*/, NULL, ops);
1867 do_cleanups (cleanup);
1869 if (readbuf != NULL)
1870 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1871 0/*read*/, NULL, ops);
1874 else if (xfered == 0 && errno == 0)
1875 /* "deprecated_xfer_memory" uses 0, cross checked against
1876 ERRNO as one indication of an error. */
1881 else if (ops->beneath != NULL)
1882 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1883 readbuf, writebuf, offset, len);
1888 /* The xfer_partial handler for the topmost target. Unlike the default,
1889 it does not need to handle memory specially; it just passes all
1890 requests down the stack. */
1893 current_xfer_partial (struct target_ops *ops, enum target_object object,
1894 const char *annex, gdb_byte *readbuf,
1895 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1897 if (ops->beneath != NULL)
1898 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1899 readbuf, writebuf, offset, len);
1904 /* Target vector read/write partial wrapper functions. */
1907 target_read_partial (struct target_ops *ops,
1908 enum target_object object,
1909 const char *annex, gdb_byte *buf,
1910 ULONGEST offset, LONGEST len)
1912 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1916 target_write_partial (struct target_ops *ops,
1917 enum target_object object,
1918 const char *annex, const gdb_byte *buf,
1919 ULONGEST offset, LONGEST len)
1921 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1924 /* Wrappers to perform the full transfer. */
1926 /* For docs on target_read see target.h. */
1929 target_read (struct target_ops *ops,
1930 enum target_object object,
1931 const char *annex, gdb_byte *buf,
1932 ULONGEST offset, LONGEST len)
1936 while (xfered < len)
1938 LONGEST xfer = target_read_partial (ops, object, annex,
1939 (gdb_byte *) buf + xfered,
1940 offset + xfered, len - xfered);
1942 /* Call an observer, notifying them of the xfer progress? */
1953 /* Assuming that the entire [begin, end) range of memory cannot be
1954 read, try to read whatever subrange is possible to read.
1956 The function returns, in RESULT, either zero or one memory block.
1957 If there's a readable subrange at the beginning, it is completely
1958 read and returned. Any further readable subrange will not be read.
1959 Otherwise, if there's a readable subrange at the end, it will be
1960 completely read and returned. Any readable subranges before it
1961 (obviously, not starting at the beginning), will be ignored. In
1962 other cases -- either no readable subrange, or readable subrange(s)
1963 that is neither at the beginning, or end, nothing is returned.
1965 The purpose of this function is to handle a read across a boundary
1966 of accessible memory in a case when memory map is not available.
1967 The above restrictions are fine for this case, but will give
1968 incorrect results if the memory is 'patchy'. However, supporting
1969 'patchy' memory would require trying to read every single byte,
1970 and it seems unacceptable solution. Explicit memory map is
1971 recommended for this case -- and target_read_memory_robust will
1972 take care of reading multiple ranges then. */
1975 read_whatever_is_readable (struct target_ops *ops,
1976 ULONGEST begin, ULONGEST end,
1977 VEC(memory_read_result_s) **result)
1979 gdb_byte *buf = xmalloc (end - begin);
1980 ULONGEST current_begin = begin;
1981 ULONGEST current_end = end;
1983 memory_read_result_s r;
1985 /* If we previously failed to read 1 byte, nothing can be done here. */
1986 if (end - begin <= 1)
1992 /* Check that either first or the last byte is readable, and give up
1993 if not. This heuristic is meant to permit reading accessible memory
1994 at the boundary of accessible region. */
1995 if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1996 buf, begin, 1) == 1)
2001 else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2002 buf + (end-begin) - 1, end - 1, 1) == 1)
2013 /* Loop invariant is that the [current_begin, current_end) was previously
2014 found to be not readable as a whole.
2016 Note loop condition -- if the range has 1 byte, we can't divide the range
2017 so there's no point trying further. */
2018 while (current_end - current_begin > 1)
2020 ULONGEST first_half_begin, first_half_end;
2021 ULONGEST second_half_begin, second_half_end;
2023 ULONGEST middle = current_begin + (current_end - current_begin)/2;
2027 first_half_begin = current_begin;
2028 first_half_end = middle;
2029 second_half_begin = middle;
2030 second_half_end = current_end;
2034 first_half_begin = middle;
2035 first_half_end = current_end;
2036 second_half_begin = current_begin;
2037 second_half_end = middle;
2040 xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2041 buf + (first_half_begin - begin),
2043 first_half_end - first_half_begin);
2045 if (xfer == first_half_end - first_half_begin)
2047 /* This half reads up fine. So, the error must be in the
2049 current_begin = second_half_begin;
2050 current_end = second_half_end;
2054 /* This half is not readable. Because we've tried one byte, we
2055 know some part of this half if actually redable. Go to the next
2056 iteration to divide again and try to read.
2058 We don't handle the other half, because this function only tries
2059 to read a single readable subrange. */
2060 current_begin = first_half_begin;
2061 current_end = first_half_end;
2067 /* The [begin, current_begin) range has been read. */
2069 r.end = current_begin;
2074 /* The [current_end, end) range has been read. */
2075 LONGEST rlen = end - current_end;
2077 r.data = xmalloc (rlen);
2078 memcpy (r.data, buf + current_end - begin, rlen);
2079 r.begin = current_end;
2083 VEC_safe_push(memory_read_result_s, (*result), &r);
2087 free_memory_read_result_vector (void *x)
2089 VEC(memory_read_result_s) *v = x;
2090 memory_read_result_s *current;
2093 for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
2095 xfree (current->data);
2097 VEC_free (memory_read_result_s, v);
2100 VEC(memory_read_result_s) *
2101 read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
2103 VEC(memory_read_result_s) *result = 0;
2106 while (xfered < len)
2108 struct mem_region *region = lookup_mem_region (offset + xfered);
2111 /* If there is no explicit region, a fake one should be created. */
2112 gdb_assert (region);
2114 if (region->hi == 0)
2115 rlen = len - xfered;
2117 rlen = region->hi - offset;
2119 if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
2121 /* Cannot read this region. Note that we can end up here only
2122 if the region is explicitly marked inaccessible, or
2123 'inaccessible-by-default' is in effect. */
2128 LONGEST to_read = min (len - xfered, rlen);
2129 gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
2131 LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2132 (gdb_byte *) buffer,
2133 offset + xfered, to_read);
2134 /* Call an observer, notifying them of the xfer progress? */
2137 /* Got an error reading full chunk. See if maybe we can read
2140 read_whatever_is_readable (ops, offset + xfered,
2141 offset + xfered + to_read, &result);
2146 struct memory_read_result r;
2148 r.begin = offset + xfered;
2149 r.end = r.begin + xfer;
2150 VEC_safe_push (memory_read_result_s, result, &r);
2160 /* An alternative to target_write with progress callbacks. */
2163 target_write_with_progress (struct target_ops *ops,
2164 enum target_object object,
2165 const char *annex, const gdb_byte *buf,
2166 ULONGEST offset, LONGEST len,
2167 void (*progress) (ULONGEST, void *), void *baton)
2171 /* Give the progress callback a chance to set up. */
2173 (*progress) (0, baton);
2175 while (xfered < len)
2177 LONGEST xfer = target_write_partial (ops, object, annex,
2178 (gdb_byte *) buf + xfered,
2179 offset + xfered, len - xfered);
2187 (*progress) (xfer, baton);
2195 /* For docs on target_write see target.h. */
2198 target_write (struct target_ops *ops,
2199 enum target_object object,
2200 const char *annex, const gdb_byte *buf,
2201 ULONGEST offset, LONGEST len)
2203 return target_write_with_progress (ops, object, annex, buf, offset, len,
2207 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2208 the size of the transferred data. PADDING additional bytes are
2209 available in *BUF_P. This is a helper function for
2210 target_read_alloc; see the declaration of that function for more
2214 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
2215 const char *annex, gdb_byte **buf_p, int padding)
2217 size_t buf_alloc, buf_pos;
2221 /* This function does not have a length parameter; it reads the
2222 entire OBJECT). Also, it doesn't support objects fetched partly
2223 from one target and partly from another (in a different stratum,
2224 e.g. a core file and an executable). Both reasons make it
2225 unsuitable for reading memory. */
2226 gdb_assert (object != TARGET_OBJECT_MEMORY);
2228 /* Start by reading up to 4K at a time. The target will throttle
2229 this number down if necessary. */
2231 buf = xmalloc (buf_alloc);
2235 n = target_read_partial (ops, object, annex, &buf[buf_pos],
2236 buf_pos, buf_alloc - buf_pos - padding);
2239 /* An error occurred. */
2245 /* Read all there was. */
2255 /* If the buffer is filling up, expand it. */
2256 if (buf_alloc < buf_pos * 2)
2259 buf = xrealloc (buf, buf_alloc);
2266 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2267 the size of the transferred data. See the declaration in "target.h"
2268 function for more information about the return value. */
2271 target_read_alloc (struct target_ops *ops, enum target_object object,
2272 const char *annex, gdb_byte **buf_p)
2274 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
2277 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
2278 returned as a string, allocated using xmalloc. If an error occurs
2279 or the transfer is unsupported, NULL is returned. Empty objects
2280 are returned as allocated but empty strings. A warning is issued
2281 if the result contains any embedded NUL bytes. */
2284 target_read_stralloc (struct target_ops *ops, enum target_object object,
2288 LONGEST transferred;
2290 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
2292 if (transferred < 0)
2295 if (transferred == 0)
2296 return xstrdup ("");
2298 buffer[transferred] = 0;
2299 if (strlen (buffer) < transferred)
2300 warning (_("target object %d, annex %s, "
2301 "contained unexpected null characters"),
2302 (int) object, annex ? annex : "(none)");
2304 return (char *) buffer;
2307 /* Memory transfer methods. */
2310 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
2313 /* This method is used to read from an alternate, non-current
2314 target. This read must bypass the overlay support (as symbols
2315 don't match this target), and GDB's internal cache (wrong cache
2316 for this target). */
2317 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
2319 memory_error (EIO, addr);
2323 get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2324 int len, enum bfd_endian byte_order)
2326 gdb_byte buf[sizeof (ULONGEST)];
2328 gdb_assert (len <= sizeof (buf));
2329 get_target_memory (ops, addr, buf, len);
2330 return extract_unsigned_integer (buf, len, byte_order);
2334 target_insert_breakpoint (struct gdbarch *gdbarch,
2335 struct bp_target_info *bp_tgt)
2337 if (!may_insert_breakpoints)
2339 warning (_("May not insert breakpoints"));
2343 return (*current_target.to_insert_breakpoint) (gdbarch, bp_tgt);
2347 target_remove_breakpoint (struct gdbarch *gdbarch,
2348 struct bp_target_info *bp_tgt)
2350 /* This is kind of a weird case to handle, but the permission might
2351 have been changed after breakpoints were inserted - in which case
2352 we should just take the user literally and assume that any
2353 breakpoints should be left in place. */
2354 if (!may_insert_breakpoints)
2356 warning (_("May not remove breakpoints"));
2360 return (*current_target.to_remove_breakpoint) (gdbarch, bp_tgt);
2364 target_info (char *args, int from_tty)
2366 struct target_ops *t;
2367 int has_all_mem = 0;
2369 if (symfile_objfile != NULL)
2370 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
2372 for (t = target_stack; t != NULL; t = t->beneath)
2374 if (!(*t->to_has_memory) (t))
2377 if ((int) (t->to_stratum) <= (int) dummy_stratum)
2380 printf_unfiltered (_("\tWhile running this, "
2381 "GDB does not access memory from...\n"));
2382 printf_unfiltered ("%s:\n", t->to_longname);
2383 (t->to_files_info) (t);
2384 has_all_mem = (*t->to_has_all_memory) (t);
2388 /* This function is called before any new inferior is created, e.g.
2389 by running a program, attaching, or connecting to a target.
2390 It cleans up any state from previous invocations which might
2391 change between runs. This is a subset of what target_preopen
2392 resets (things which might change between targets). */
2395 target_pre_inferior (int from_tty)
2397 /* Clear out solib state. Otherwise the solib state of the previous
2398 inferior might have survived and is entirely wrong for the new
2399 target. This has been observed on GNU/Linux using glibc 2.3. How
2411 Cannot access memory at address 0xdeadbeef
2414 /* In some OSs, the shared library list is the same/global/shared
2415 across inferiors. If code is shared between processes, so are
2416 memory regions and features. */
2417 if (!gdbarch_has_global_solist (target_gdbarch))
2419 no_shared_libraries (NULL, from_tty);
2421 invalidate_target_mem_regions ();
2423 target_clear_description ();
2427 /* Callback for iterate_over_inferiors. Gets rid of the given
2431 dispose_inferior (struct inferior *inf, void *args)
2433 struct thread_info *thread;
2435 thread = any_thread_of_process (inf->pid);
2438 switch_to_thread (thread->ptid);
2440 /* Core inferiors actually should be detached, not killed. */
2441 if (target_has_execution)
2444 target_detach (NULL, 0);
2450 /* This is to be called by the open routine before it does
2454 target_preopen (int from_tty)
2458 if (have_inferiors ())
2461 || !have_live_inferiors ()
2462 || query (_("A program is being debugged already. Kill it? ")))
2463 iterate_over_inferiors (dispose_inferior, NULL);
2465 error (_("Program not killed."));
2468 /* Calling target_kill may remove the target from the stack. But if
2469 it doesn't (which seems like a win for UDI), remove it now. */
2470 /* Leave the exec target, though. The user may be switching from a
2471 live process to a core of the same program. */
2472 pop_all_targets_above (file_stratum, 0);
2474 target_pre_inferior (from_tty);
2477 /* Detach a target after doing deferred register stores. */
2480 target_detach (char *args, int from_tty)
2482 struct target_ops* t;
2484 if (gdbarch_has_global_breakpoints (target_gdbarch))
2485 /* Don't remove global breakpoints here. They're removed on
2486 disconnection from the target. */
2489 /* If we're in breakpoints-always-inserted mode, have to remove
2490 them before detaching. */
2491 remove_breakpoints_pid (PIDGET (inferior_ptid));
2493 prepare_for_detach ();
2495 for (t = current_target.beneath; t != NULL; t = t->beneath)
2497 if (t->to_detach != NULL)
2499 t->to_detach (t, args, from_tty);
2501 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2507 internal_error (__FILE__, __LINE__, _("could not find a target to detach"));
2511 target_disconnect (char *args, int from_tty)
2513 struct target_ops *t;
2515 /* If we're in breakpoints-always-inserted mode or if breakpoints
2516 are global across processes, we have to remove them before
2518 remove_breakpoints ();
2520 for (t = current_target.beneath; t != NULL; t = t->beneath)
2521 if (t->to_disconnect != NULL)
2524 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2526 t->to_disconnect (t, args, from_tty);
2534 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
2536 struct target_ops *t;
2538 for (t = current_target.beneath; t != NULL; t = t->beneath)
2540 if (t->to_wait != NULL)
2542 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2546 char *status_string;
2548 status_string = target_waitstatus_to_string (status);
2549 fprintf_unfiltered (gdb_stdlog,
2550 "target_wait (%d, status) = %d, %s\n",
2551 PIDGET (ptid), PIDGET (retval),
2553 xfree (status_string);
2564 target_pid_to_str (ptid_t ptid)
2566 struct target_ops *t;
2568 for (t = current_target.beneath; t != NULL; t = t->beneath)
2570 if (t->to_pid_to_str != NULL)
2571 return (*t->to_pid_to_str) (t, ptid);
2574 return normal_pid_to_str (ptid);
2578 target_thread_name (struct thread_info *info)
2580 struct target_ops *t;
2582 for (t = current_target.beneath; t != NULL; t = t->beneath)
2584 if (t->to_thread_name != NULL)
2585 return (*t->to_thread_name) (info);
2592 target_resume (ptid_t ptid, int step, enum target_signal signal)
2594 struct target_ops *t;
2596 target_dcache_invalidate ();
2598 for (t = current_target.beneath; t != NULL; t = t->beneath)
2600 if (t->to_resume != NULL)
2602 t->to_resume (t, ptid, step, signal);
2604 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2606 step ? "step" : "continue",
2607 target_signal_to_name (signal));
2609 registers_changed_ptid (ptid);
2610 set_executing (ptid, 1);
2611 set_running (ptid, 1);
2612 clear_inline_frame_state (ptid);
2621 target_pass_signals (int numsigs, unsigned char *pass_signals)
2623 struct target_ops *t;
2625 for (t = current_target.beneath; t != NULL; t = t->beneath)
2627 if (t->to_pass_signals != NULL)
2633 fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2636 for (i = 0; i < numsigs; i++)
2637 if (pass_signals[i])
2638 fprintf_unfiltered (gdb_stdlog, " %s",
2639 target_signal_to_name (i));
2641 fprintf_unfiltered (gdb_stdlog, " })\n");
2644 (*t->to_pass_signals) (numsigs, pass_signals);
2650 /* Look through the list of possible targets for a target that can
2654 target_follow_fork (int follow_child)
2656 struct target_ops *t;
2658 for (t = current_target.beneath; t != NULL; t = t->beneath)
2660 if (t->to_follow_fork != NULL)
2662 int retval = t->to_follow_fork (t, follow_child);
2665 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2666 follow_child, retval);
2671 /* Some target returned a fork event, but did not know how to follow it. */
2672 internal_error (__FILE__, __LINE__,
2673 _("could not find a target to follow fork"));
2677 target_mourn_inferior (void)
2679 struct target_ops *t;
2681 for (t = current_target.beneath; t != NULL; t = t->beneath)
2683 if (t->to_mourn_inferior != NULL)
2685 t->to_mourn_inferior (t);
2687 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2689 /* We no longer need to keep handles on any of the object files.
2690 Make sure to release them to avoid unnecessarily locking any
2691 of them while we're not actually debugging. */
2692 bfd_cache_close_all ();
2698 internal_error (__FILE__, __LINE__,
2699 _("could not find a target to follow mourn inferior"));
2702 /* Look for a target which can describe architectural features, starting
2703 from TARGET. If we find one, return its description. */
2705 const struct target_desc *
2706 target_read_description (struct target_ops *target)
2708 struct target_ops *t;
2710 for (t = target; t != NULL; t = t->beneath)
2711 if (t->to_read_description != NULL)
2713 const struct target_desc *tdesc;
2715 tdesc = t->to_read_description (t);
2723 /* The default implementation of to_search_memory.
2724 This implements a basic search of memory, reading target memory and
2725 performing the search here (as opposed to performing the search in on the
2726 target side with, for example, gdbserver). */
2729 simple_search_memory (struct target_ops *ops,
2730 CORE_ADDR start_addr, ULONGEST search_space_len,
2731 const gdb_byte *pattern, ULONGEST pattern_len,
2732 CORE_ADDR *found_addrp)
2734 /* NOTE: also defined in find.c testcase. */
2735 #define SEARCH_CHUNK_SIZE 16000
2736 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2737 /* Buffer to hold memory contents for searching. */
2738 gdb_byte *search_buf;
2739 unsigned search_buf_size;
2740 struct cleanup *old_cleanups;
2742 search_buf_size = chunk_size + pattern_len - 1;
2744 /* No point in trying to allocate a buffer larger than the search space. */
2745 if (search_space_len < search_buf_size)
2746 search_buf_size = search_space_len;
2748 search_buf = malloc (search_buf_size);
2749 if (search_buf == NULL)
2750 error (_("Unable to allocate memory to perform the search."));
2751 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2753 /* Prime the search buffer. */
2755 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2756 search_buf, start_addr, search_buf_size) != search_buf_size)
2758 warning (_("Unable to access target memory at %s, halting search."),
2759 hex_string (start_addr));
2760 do_cleanups (old_cleanups);
2764 /* Perform the search.
2766 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2767 When we've scanned N bytes we copy the trailing bytes to the start and
2768 read in another N bytes. */
2770 while (search_space_len >= pattern_len)
2772 gdb_byte *found_ptr;
2773 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2775 found_ptr = memmem (search_buf, nr_search_bytes,
2776 pattern, pattern_len);
2778 if (found_ptr != NULL)
2780 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2782 *found_addrp = found_addr;
2783 do_cleanups (old_cleanups);
2787 /* Not found in this chunk, skip to next chunk. */
2789 /* Don't let search_space_len wrap here, it's unsigned. */
2790 if (search_space_len >= chunk_size)
2791 search_space_len -= chunk_size;
2793 search_space_len = 0;
2795 if (search_space_len >= pattern_len)
2797 unsigned keep_len = search_buf_size - chunk_size;
2798 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
2801 /* Copy the trailing part of the previous iteration to the front
2802 of the buffer for the next iteration. */
2803 gdb_assert (keep_len == pattern_len - 1);
2804 memcpy (search_buf, search_buf + chunk_size, keep_len);
2806 nr_to_read = min (search_space_len - keep_len, chunk_size);
2808 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2809 search_buf + keep_len, read_addr,
2810 nr_to_read) != nr_to_read)
2812 warning (_("Unable to access target "
2813 "memory at %s, halting search."),
2814 hex_string (read_addr));
2815 do_cleanups (old_cleanups);
2819 start_addr += chunk_size;
2825 do_cleanups (old_cleanups);
2829 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2830 sequence of bytes in PATTERN with length PATTERN_LEN.
2832 The result is 1 if found, 0 if not found, and -1 if there was an error
2833 requiring halting of the search (e.g. memory read error).
2834 If the pattern is found the address is recorded in FOUND_ADDRP. */
2837 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2838 const gdb_byte *pattern, ULONGEST pattern_len,
2839 CORE_ADDR *found_addrp)
2841 struct target_ops *t;
2844 /* We don't use INHERIT to set current_target.to_search_memory,
2845 so we have to scan the target stack and handle targetdebug
2849 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2850 hex_string (start_addr));
2852 for (t = current_target.beneath; t != NULL; t = t->beneath)
2853 if (t->to_search_memory != NULL)
2858 found = t->to_search_memory (t, start_addr, search_space_len,
2859 pattern, pattern_len, found_addrp);
2863 /* If a special version of to_search_memory isn't available, use the
2865 found = simple_search_memory (current_target.beneath,
2866 start_addr, search_space_len,
2867 pattern, pattern_len, found_addrp);
2871 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2876 /* Look through the currently pushed targets. If none of them will
2877 be able to restart the currently running process, issue an error
2881 target_require_runnable (void)
2883 struct target_ops *t;
2885 for (t = target_stack; t != NULL; t = t->beneath)
2887 /* If this target knows how to create a new program, then
2888 assume we will still be able to after killing the current
2889 one. Either killing and mourning will not pop T, or else
2890 find_default_run_target will find it again. */
2891 if (t->to_create_inferior != NULL)
2894 /* Do not worry about thread_stratum targets that can not
2895 create inferiors. Assume they will be pushed again if
2896 necessary, and continue to the process_stratum. */
2897 if (t->to_stratum == thread_stratum
2898 || t->to_stratum == arch_stratum)
2901 error (_("The \"%s\" target does not support \"run\". "
2902 "Try \"help target\" or \"continue\"."),
2906 /* This function is only called if the target is running. In that
2907 case there should have been a process_stratum target and it
2908 should either know how to create inferiors, or not... */
2909 internal_error (__FILE__, __LINE__, _("No targets found"));
2912 /* Look through the list of possible targets for a target that can
2913 execute a run or attach command without any other data. This is
2914 used to locate the default process stratum.
2916 If DO_MESG is not NULL, the result is always valid (error() is
2917 called for errors); else, return NULL on error. */
2919 static struct target_ops *
2920 find_default_run_target (char *do_mesg)
2922 struct target_ops **t;
2923 struct target_ops *runable = NULL;
2928 for (t = target_structs; t < target_structs + target_struct_size;
2931 if ((*t)->to_can_run && target_can_run (*t))
2941 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2950 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2952 struct target_ops *t;
2954 t = find_default_run_target ("attach");
2955 (t->to_attach) (t, args, from_tty);
2960 find_default_create_inferior (struct target_ops *ops,
2961 char *exec_file, char *allargs, char **env,
2964 struct target_ops *t;
2966 t = find_default_run_target ("run");
2967 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2972 find_default_can_async_p (void)
2974 struct target_ops *t;
2976 /* This may be called before the target is pushed on the stack;
2977 look for the default process stratum. If there's none, gdb isn't
2978 configured with a native debugger, and target remote isn't
2980 t = find_default_run_target (NULL);
2981 if (t && t->to_can_async_p)
2982 return (t->to_can_async_p) ();
2987 find_default_is_async_p (void)
2989 struct target_ops *t;
2991 /* This may be called before the target is pushed on the stack;
2992 look for the default process stratum. If there's none, gdb isn't
2993 configured with a native debugger, and target remote isn't
2995 t = find_default_run_target (NULL);
2996 if (t && t->to_is_async_p)
2997 return (t->to_is_async_p) ();
3002 find_default_supports_non_stop (void)
3004 struct target_ops *t;
3006 t = find_default_run_target (NULL);
3007 if (t && t->to_supports_non_stop)
3008 return (t->to_supports_non_stop) ();
3013 target_supports_non_stop (void)
3015 struct target_ops *t;
3017 for (t = ¤t_target; t != NULL; t = t->beneath)
3018 if (t->to_supports_non_stop)
3019 return t->to_supports_non_stop ();
3025 find_default_supports_disable_randomization (void)
3027 struct target_ops *t;
3029 t = find_default_run_target (NULL);
3030 if (t && t->to_supports_disable_randomization)
3031 return (t->to_supports_disable_randomization) ();
3036 target_supports_disable_randomization (void)
3038 struct target_ops *t;
3040 for (t = ¤t_target; t != NULL; t = t->beneath)
3041 if (t->to_supports_disable_randomization)
3042 return t->to_supports_disable_randomization ();
3048 target_get_osdata (const char *type)
3050 struct target_ops *t;
3052 /* If we're already connected to something that can get us OS
3053 related data, use it. Otherwise, try using the native
3055 if (current_target.to_stratum >= process_stratum)
3056 t = current_target.beneath;
3058 t = find_default_run_target ("get OS data");
3063 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
3066 /* Determine the current address space of thread PTID. */
3068 struct address_space *
3069 target_thread_address_space (ptid_t ptid)
3071 struct address_space *aspace;
3072 struct inferior *inf;
3073 struct target_ops *t;
3075 for (t = current_target.beneath; t != NULL; t = t->beneath)
3077 if (t->to_thread_address_space != NULL)
3079 aspace = t->to_thread_address_space (t, ptid);
3080 gdb_assert (aspace);
3083 fprintf_unfiltered (gdb_stdlog,
3084 "target_thread_address_space (%s) = %d\n",
3085 target_pid_to_str (ptid),
3086 address_space_num (aspace));
3091 /* Fall-back to the "main" address space of the inferior. */
3092 inf = find_inferior_pid (ptid_get_pid (ptid));
3094 if (inf == NULL || inf->aspace == NULL)
3095 internal_error (__FILE__, __LINE__,
3096 _("Can't determine the current "
3097 "address space of thread %s\n"),
3098 target_pid_to_str (ptid));
3104 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3106 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
3110 default_watchpoint_addr_within_range (struct target_ops *target,
3112 CORE_ADDR start, int length)
3114 return addr >= start && addr < start + length;
3117 static struct gdbarch *
3118 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
3120 return target_gdbarch;
3136 return_minus_one (void)
3141 /* Find a single runnable target in the stack and return it. If for
3142 some reason there is more than one, return NULL. */
3145 find_run_target (void)
3147 struct target_ops **t;
3148 struct target_ops *runable = NULL;
3153 for (t = target_structs; t < target_structs + target_struct_size; ++t)
3155 if ((*t)->to_can_run && target_can_run (*t))
3162 return (count == 1 ? runable : NULL);
3166 * Find the next target down the stack from the specified target.
3170 find_target_beneath (struct target_ops *t)
3176 /* The inferior process has died. Long live the inferior! */
3179 generic_mourn_inferior (void)
3183 ptid = inferior_ptid;
3184 inferior_ptid = null_ptid;
3186 if (!ptid_equal (ptid, null_ptid))
3188 int pid = ptid_get_pid (ptid);
3189 exit_inferior (pid);
3192 breakpoint_init_inferior (inf_exited);
3193 registers_changed ();
3195 reopen_exec_file ();
3196 reinit_frame_cache ();
3198 if (deprecated_detach_hook)
3199 deprecated_detach_hook ();
3202 /* Helper function for child_wait and the derivatives of child_wait.
3203 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
3204 translation of that in OURSTATUS. */
3206 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
3208 if (WIFEXITED (hoststatus))
3210 ourstatus->kind = TARGET_WAITKIND_EXITED;
3211 ourstatus->value.integer = WEXITSTATUS (hoststatus);
3213 else if (!WIFSTOPPED (hoststatus))
3215 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
3216 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
3220 ourstatus->kind = TARGET_WAITKIND_STOPPED;
3221 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
3225 /* Convert a normal process ID to a string. Returns the string in a
3229 normal_pid_to_str (ptid_t ptid)
3231 static char buf[32];
3233 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
3238 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
3240 return normal_pid_to_str (ptid);
3243 /* Error-catcher for target_find_memory_regions. */
3245 dummy_find_memory_regions (find_memory_region_ftype ignore1, void *ignore2)
3247 error (_("Command not implemented for this target."));
3251 /* Error-catcher for target_make_corefile_notes. */
3253 dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
3255 error (_("Command not implemented for this target."));
3259 /* Error-catcher for target_get_bookmark. */
3261 dummy_get_bookmark (char *ignore1, int ignore2)
3267 /* Error-catcher for target_goto_bookmark. */
3269 dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
3274 /* Set up the handful of non-empty slots needed by the dummy target
3278 init_dummy_target (void)
3280 dummy_target.to_shortname = "None";
3281 dummy_target.to_longname = "None";
3282 dummy_target.to_doc = "";
3283 dummy_target.to_attach = find_default_attach;
3284 dummy_target.to_detach =
3285 (void (*)(struct target_ops *, char *, int))target_ignore;
3286 dummy_target.to_create_inferior = find_default_create_inferior;
3287 dummy_target.to_can_async_p = find_default_can_async_p;
3288 dummy_target.to_is_async_p = find_default_is_async_p;
3289 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
3290 dummy_target.to_supports_disable_randomization
3291 = find_default_supports_disable_randomization;
3292 dummy_target.to_pid_to_str = dummy_pid_to_str;
3293 dummy_target.to_stratum = dummy_stratum;
3294 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
3295 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
3296 dummy_target.to_get_bookmark = dummy_get_bookmark;
3297 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
3298 dummy_target.to_xfer_partial = default_xfer_partial;
3299 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3300 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
3301 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
3302 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
3303 dummy_target.to_has_execution
3304 = (int (*) (struct target_ops *, ptid_t)) return_zero;
3305 dummy_target.to_stopped_by_watchpoint = return_zero;
3306 dummy_target.to_stopped_data_address =
3307 (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
3308 dummy_target.to_magic = OPS_MAGIC;
3312 debug_to_open (char *args, int from_tty)
3314 debug_target.to_open (args, from_tty);
3316 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
3320 target_close (struct target_ops *targ, int quitting)
3322 if (targ->to_xclose != NULL)
3323 targ->to_xclose (targ, quitting);
3324 else if (targ->to_close != NULL)
3325 targ->to_close (quitting);
3328 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
3332 target_attach (char *args, int from_tty)
3334 struct target_ops *t;
3336 for (t = current_target.beneath; t != NULL; t = t->beneath)
3338 if (t->to_attach != NULL)
3340 t->to_attach (t, args, from_tty);
3342 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
3348 internal_error (__FILE__, __LINE__,
3349 _("could not find a target to attach"));
3353 target_thread_alive (ptid_t ptid)
3355 struct target_ops *t;
3357 for (t = current_target.beneath; t != NULL; t = t->beneath)
3359 if (t->to_thread_alive != NULL)
3363 retval = t->to_thread_alive (t, ptid);
3365 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
3366 PIDGET (ptid), retval);
3376 target_find_new_threads (void)
3378 struct target_ops *t;
3380 for (t = current_target.beneath; t != NULL; t = t->beneath)
3382 if (t->to_find_new_threads != NULL)
3384 t->to_find_new_threads (t);
3386 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
3394 target_stop (ptid_t ptid)
3398 warning (_("May not interrupt or stop the target, ignoring attempt"));
3402 (*current_target.to_stop) (ptid);
3406 debug_to_post_attach (int pid)
3408 debug_target.to_post_attach (pid);
3410 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
3413 /* Return a pretty printed form of target_waitstatus.
3414 Space for the result is malloc'd, caller must free. */
3417 target_waitstatus_to_string (const struct target_waitstatus *ws)
3419 const char *kind_str = "status->kind = ";
3423 case TARGET_WAITKIND_EXITED:
3424 return xstrprintf ("%sexited, status = %d",
3425 kind_str, ws->value.integer);
3426 case TARGET_WAITKIND_STOPPED:
3427 return xstrprintf ("%sstopped, signal = %s",
3428 kind_str, target_signal_to_name (ws->value.sig));
3429 case TARGET_WAITKIND_SIGNALLED:
3430 return xstrprintf ("%ssignalled, signal = %s",
3431 kind_str, target_signal_to_name (ws->value.sig));
3432 case TARGET_WAITKIND_LOADED:
3433 return xstrprintf ("%sloaded", kind_str);
3434 case TARGET_WAITKIND_FORKED:
3435 return xstrprintf ("%sforked", kind_str);
3436 case TARGET_WAITKIND_VFORKED:
3437 return xstrprintf ("%svforked", kind_str);
3438 case TARGET_WAITKIND_EXECD:
3439 return xstrprintf ("%sexecd", kind_str);
3440 case TARGET_WAITKIND_SYSCALL_ENTRY:
3441 return xstrprintf ("%sentered syscall", kind_str);
3442 case TARGET_WAITKIND_SYSCALL_RETURN:
3443 return xstrprintf ("%sexited syscall", kind_str);
3444 case TARGET_WAITKIND_SPURIOUS:
3445 return xstrprintf ("%sspurious", kind_str);
3446 case TARGET_WAITKIND_IGNORE:
3447 return xstrprintf ("%signore", kind_str);
3448 case TARGET_WAITKIND_NO_HISTORY:
3449 return xstrprintf ("%sno-history", kind_str);
3450 case TARGET_WAITKIND_NO_RESUMED:
3451 return xstrprintf ("%sno-resumed", kind_str);
3453 return xstrprintf ("%sunknown???", kind_str);
3458 debug_print_register (const char * func,
3459 struct regcache *regcache, int regno)
3461 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3463 fprintf_unfiltered (gdb_stdlog, "%s ", func);
3464 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
3465 && gdbarch_register_name (gdbarch, regno) != NULL
3466 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3467 fprintf_unfiltered (gdb_stdlog, "(%s)",
3468 gdbarch_register_name (gdbarch, regno));
3470 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
3471 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
3473 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3474 int i, size = register_size (gdbarch, regno);
3475 unsigned char buf[MAX_REGISTER_SIZE];
3477 regcache_raw_collect (regcache, regno, buf);
3478 fprintf_unfiltered (gdb_stdlog, " = ");
3479 for (i = 0; i < size; i++)
3481 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3483 if (size <= sizeof (LONGEST))
3485 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
3487 fprintf_unfiltered (gdb_stdlog, " %s %s",
3488 core_addr_to_string_nz (val), plongest (val));
3491 fprintf_unfiltered (gdb_stdlog, "\n");
3495 target_fetch_registers (struct regcache *regcache, int regno)
3497 struct target_ops *t;
3499 for (t = current_target.beneath; t != NULL; t = t->beneath)
3501 if (t->to_fetch_registers != NULL)
3503 t->to_fetch_registers (t, regcache, regno);
3505 debug_print_register ("target_fetch_registers", regcache, regno);
3512 target_store_registers (struct regcache *regcache, int regno)
3514 struct target_ops *t;
3516 if (!may_write_registers)
3517 error (_("Writing to registers is not allowed (regno %d)"), regno);
3519 for (t = current_target.beneath; t != NULL; t = t->beneath)
3521 if (t->to_store_registers != NULL)
3523 t->to_store_registers (t, regcache, regno);
3526 debug_print_register ("target_store_registers", regcache, regno);
3536 target_core_of_thread (ptid_t ptid)
3538 struct target_ops *t;
3540 for (t = current_target.beneath; t != NULL; t = t->beneath)
3542 if (t->to_core_of_thread != NULL)
3544 int retval = t->to_core_of_thread (t, ptid);
3547 fprintf_unfiltered (gdb_stdlog,
3548 "target_core_of_thread (%d) = %d\n",
3549 PIDGET (ptid), retval);
3558 target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3560 struct target_ops *t;
3562 for (t = current_target.beneath; t != NULL; t = t->beneath)
3564 if (t->to_verify_memory != NULL)
3566 int retval = t->to_verify_memory (t, data, memaddr, size);
3569 fprintf_unfiltered (gdb_stdlog,
3570 "target_verify_memory (%s, %s) = %d\n",
3571 paddress (target_gdbarch, memaddr),
3581 /* The documentation for this function is in its prototype declaration in
3585 target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3587 struct target_ops *t;
3589 for (t = current_target.beneath; t != NULL; t = t->beneath)
3590 if (t->to_insert_mask_watchpoint != NULL)
3594 ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
3597 fprintf_unfiltered (gdb_stdlog, "\
3598 target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
3599 core_addr_to_string (addr),
3600 core_addr_to_string (mask), rw, ret);
3608 /* The documentation for this function is in its prototype declaration in
3612 target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3614 struct target_ops *t;
3616 for (t = current_target.beneath; t != NULL; t = t->beneath)
3617 if (t->to_remove_mask_watchpoint != NULL)
3621 ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
3624 fprintf_unfiltered (gdb_stdlog, "\
3625 target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
3626 core_addr_to_string (addr),
3627 core_addr_to_string (mask), rw, ret);
3635 /* The documentation for this function is in its prototype declaration
3639 target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
3641 struct target_ops *t;
3643 for (t = current_target.beneath; t != NULL; t = t->beneath)
3644 if (t->to_masked_watch_num_registers != NULL)
3645 return t->to_masked_watch_num_registers (t, addr, mask);
3650 /* The documentation for this function is in its prototype declaration
3654 target_ranged_break_num_registers (void)
3656 struct target_ops *t;
3658 for (t = current_target.beneath; t != NULL; t = t->beneath)
3659 if (t->to_ranged_break_num_registers != NULL)
3660 return t->to_ranged_break_num_registers (t);
3666 debug_to_prepare_to_store (struct regcache *regcache)
3668 debug_target.to_prepare_to_store (regcache);
3670 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
3674 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
3675 int write, struct mem_attrib *attrib,
3676 struct target_ops *target)
3680 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3683 fprintf_unfiltered (gdb_stdlog,
3684 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
3685 paddress (target_gdbarch, memaddr), len,
3686 write ? "write" : "read", retval);
3692 fputs_unfiltered (", bytes =", gdb_stdlog);
3693 for (i = 0; i < retval; i++)
3695 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
3697 if (targetdebug < 2 && i > 0)
3699 fprintf_unfiltered (gdb_stdlog, " ...");
3702 fprintf_unfiltered (gdb_stdlog, "\n");
3705 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
3709 fputc_unfiltered ('\n', gdb_stdlog);
3715 debug_to_files_info (struct target_ops *target)
3717 debug_target.to_files_info (target);
3719 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
3723 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3724 struct bp_target_info *bp_tgt)
3728 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
3730 fprintf_unfiltered (gdb_stdlog,
3731 "target_insert_breakpoint (%s, xxx) = %ld\n",
3732 core_addr_to_string (bp_tgt->placed_address),
3733 (unsigned long) retval);
3738 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3739 struct bp_target_info *bp_tgt)
3743 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
3745 fprintf_unfiltered (gdb_stdlog,
3746 "target_remove_breakpoint (%s, xxx) = %ld\n",
3747 core_addr_to_string (bp_tgt->placed_address),
3748 (unsigned long) retval);
3753 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3757 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3759 fprintf_unfiltered (gdb_stdlog,
3760 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3761 (unsigned long) type,
3762 (unsigned long) cnt,
3763 (unsigned long) from_tty,
3764 (unsigned long) retval);
3769 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3773 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3775 fprintf_unfiltered (gdb_stdlog,
3776 "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
3777 core_addr_to_string (addr), (unsigned long) len,
3778 core_addr_to_string (retval));
3783 debug_to_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
3784 struct expression *cond)
3788 retval = debug_target.to_can_accel_watchpoint_condition (addr, len,
3791 fprintf_unfiltered (gdb_stdlog,
3792 "target_can_accel_watchpoint_condition "
3793 "(%s, %d, %d, %s) = %ld\n",
3794 core_addr_to_string (addr), len, rw,
3795 host_address_to_string (cond), (unsigned long) retval);
3800 debug_to_stopped_by_watchpoint (void)
3804 retval = debug_target.to_stopped_by_watchpoint ();
3806 fprintf_unfiltered (gdb_stdlog,
3807 "target_stopped_by_watchpoint () = %ld\n",
3808 (unsigned long) retval);
3813 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
3817 retval = debug_target.to_stopped_data_address (target, addr);
3819 fprintf_unfiltered (gdb_stdlog,
3820 "target_stopped_data_address ([%s]) = %ld\n",
3821 core_addr_to_string (*addr),
3822 (unsigned long)retval);
3827 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3829 CORE_ADDR start, int length)
3833 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3836 fprintf_filtered (gdb_stdlog,
3837 "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
3838 core_addr_to_string (addr), core_addr_to_string (start),
3844 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3845 struct bp_target_info *bp_tgt)
3849 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3851 fprintf_unfiltered (gdb_stdlog,
3852 "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
3853 core_addr_to_string (bp_tgt->placed_address),
3854 (unsigned long) retval);
3859 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3860 struct bp_target_info *bp_tgt)
3864 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3866 fprintf_unfiltered (gdb_stdlog,
3867 "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
3868 core_addr_to_string (bp_tgt->placed_address),
3869 (unsigned long) retval);
3874 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type,
3875 struct expression *cond)
3879 retval = debug_target.to_insert_watchpoint (addr, len, type, cond);
3881 fprintf_unfiltered (gdb_stdlog,
3882 "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
3883 core_addr_to_string (addr), len, type,
3884 host_address_to_string (cond), (unsigned long) retval);
3889 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type,
3890 struct expression *cond)
3894 retval = debug_target.to_remove_watchpoint (addr, len, type, cond);
3896 fprintf_unfiltered (gdb_stdlog,
3897 "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
3898 core_addr_to_string (addr), len, type,
3899 host_address_to_string (cond), (unsigned long) retval);
3904 debug_to_terminal_init (void)
3906 debug_target.to_terminal_init ();
3908 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3912 debug_to_terminal_inferior (void)
3914 debug_target.to_terminal_inferior ();
3916 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3920 debug_to_terminal_ours_for_output (void)
3922 debug_target.to_terminal_ours_for_output ();
3924 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3928 debug_to_terminal_ours (void)
3930 debug_target.to_terminal_ours ();
3932 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3936 debug_to_terminal_save_ours (void)
3938 debug_target.to_terminal_save_ours ();
3940 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3944 debug_to_terminal_info (char *arg, int from_tty)
3946 debug_target.to_terminal_info (arg, from_tty);
3948 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3953 debug_to_load (char *args, int from_tty)
3955 debug_target.to_load (args, from_tty);
3957 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3961 debug_to_post_startup_inferior (ptid_t ptid)
3963 debug_target.to_post_startup_inferior (ptid);
3965 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3970 debug_to_insert_fork_catchpoint (int pid)
3974 retval = debug_target.to_insert_fork_catchpoint (pid);
3976 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
3983 debug_to_remove_fork_catchpoint (int pid)
3987 retval = debug_target.to_remove_fork_catchpoint (pid);
3989 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3996 debug_to_insert_vfork_catchpoint (int pid)
4000 retval = debug_target.to_insert_vfork_catchpoint (pid);
4002 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4009 debug_to_remove_vfork_catchpoint (int pid)
4013 retval = debug_target.to_remove_vfork_catchpoint (pid);
4015 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
4022 debug_to_insert_exec_catchpoint (int pid)
4026 retval = debug_target.to_insert_exec_catchpoint (pid);
4028 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4035 debug_to_remove_exec_catchpoint (int pid)
4039 retval = debug_target.to_remove_exec_catchpoint (pid);
4041 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
4048 debug_to_has_exited (int pid, int wait_status, int *exit_status)
4052 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
4054 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
4055 pid, wait_status, *exit_status, has_exited);
4061 debug_to_can_run (void)
4065 retval = debug_target.to_can_run ();
4067 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
4072 static struct gdbarch *
4073 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
4075 struct gdbarch *retval;
4077 retval = debug_target.to_thread_architecture (ops, ptid);
4079 fprintf_unfiltered (gdb_stdlog,
4080 "target_thread_architecture (%s) = %s [%s]\n",
4081 target_pid_to_str (ptid),
4082 host_address_to_string (retval),
4083 gdbarch_bfd_arch_info (retval)->printable_name);
4088 debug_to_stop (ptid_t ptid)
4090 debug_target.to_stop (ptid);
4092 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
4093 target_pid_to_str (ptid));
4097 debug_to_rcmd (char *command,
4098 struct ui_file *outbuf)
4100 debug_target.to_rcmd (command, outbuf);
4101 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
4105 debug_to_pid_to_exec_file (int pid)
4109 exec_file = debug_target.to_pid_to_exec_file (pid);
4111 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
4118 setup_target_debug (void)
4120 memcpy (&debug_target, ¤t_target, sizeof debug_target);
4122 current_target.to_open = debug_to_open;
4123 current_target.to_post_attach = debug_to_post_attach;
4124 current_target.to_prepare_to_store = debug_to_prepare_to_store;
4125 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
4126 current_target.to_files_info = debug_to_files_info;
4127 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
4128 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
4129 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
4130 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
4131 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
4132 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
4133 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
4134 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
4135 current_target.to_stopped_data_address = debug_to_stopped_data_address;
4136 current_target.to_watchpoint_addr_within_range
4137 = debug_to_watchpoint_addr_within_range;
4138 current_target.to_region_ok_for_hw_watchpoint
4139 = debug_to_region_ok_for_hw_watchpoint;
4140 current_target.to_can_accel_watchpoint_condition
4141 = debug_to_can_accel_watchpoint_condition;
4142 current_target.to_terminal_init = debug_to_terminal_init;
4143 current_target.to_terminal_inferior = debug_to_terminal_inferior;
4144 current_target.to_terminal_ours_for_output
4145 = debug_to_terminal_ours_for_output;
4146 current_target.to_terminal_ours = debug_to_terminal_ours;
4147 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
4148 current_target.to_terminal_info = debug_to_terminal_info;
4149 current_target.to_load = debug_to_load;
4150 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
4151 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
4152 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
4153 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
4154 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
4155 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
4156 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
4157 current_target.to_has_exited = debug_to_has_exited;
4158 current_target.to_can_run = debug_to_can_run;
4159 current_target.to_stop = debug_to_stop;
4160 current_target.to_rcmd = debug_to_rcmd;
4161 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
4162 current_target.to_thread_architecture = debug_to_thread_architecture;
4166 static char targ_desc[] =
4167 "Names of targets and files being debugged.\nShows the entire \
4168 stack of targets currently in use (including the exec-file,\n\
4169 core-file, and process, if any), as well as the symbol file name.";
4172 do_monitor_command (char *cmd,
4175 if ((current_target.to_rcmd
4176 == (void (*) (char *, struct ui_file *)) tcomplain)
4177 || (current_target.to_rcmd == debug_to_rcmd
4178 && (debug_target.to_rcmd
4179 == (void (*) (char *, struct ui_file *)) tcomplain)))
4180 error (_("\"monitor\" command not supported by this target."));
4181 target_rcmd (cmd, gdb_stdtarg);
4184 /* Print the name of each layers of our target stack. */
4187 maintenance_print_target_stack (char *cmd, int from_tty)
4189 struct target_ops *t;
4191 printf_filtered (_("The current target stack is:\n"));
4193 for (t = target_stack; t != NULL; t = t->beneath)
4195 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
4199 /* Controls if async mode is permitted. */
4200 int target_async_permitted = 0;
4202 /* The set command writes to this variable. If the inferior is
4203 executing, linux_nat_async_permitted is *not* updated. */
4204 static int target_async_permitted_1 = 0;
4207 set_maintenance_target_async_permitted (char *args, int from_tty,
4208 struct cmd_list_element *c)
4210 if (have_live_inferiors ())
4212 target_async_permitted_1 = target_async_permitted;
4213 error (_("Cannot change this setting while the inferior is running."));
4216 target_async_permitted = target_async_permitted_1;
4220 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
4221 struct cmd_list_element *c,
4224 fprintf_filtered (file,
4225 _("Controlling the inferior in "
4226 "asynchronous mode is %s.\n"), value);
4229 /* Temporary copies of permission settings. */
4231 static int may_write_registers_1 = 1;
4232 static int may_write_memory_1 = 1;
4233 static int may_insert_breakpoints_1 = 1;
4234 static int may_insert_tracepoints_1 = 1;
4235 static int may_insert_fast_tracepoints_1 = 1;
4236 static int may_stop_1 = 1;
4238 /* Make the user-set values match the real values again. */
4241 update_target_permissions (void)
4243 may_write_registers_1 = may_write_registers;
4244 may_write_memory_1 = may_write_memory;
4245 may_insert_breakpoints_1 = may_insert_breakpoints;
4246 may_insert_tracepoints_1 = may_insert_tracepoints;
4247 may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
4248 may_stop_1 = may_stop;
4251 /* The one function handles (most of) the permission flags in the same
4255 set_target_permissions (char *args, int from_tty,
4256 struct cmd_list_element *c)
4258 if (target_has_execution)
4260 update_target_permissions ();
4261 error (_("Cannot change this setting while the inferior is running."));
4264 /* Make the real values match the user-changed values. */
4265 may_write_registers = may_write_registers_1;
4266 may_insert_breakpoints = may_insert_breakpoints_1;
4267 may_insert_tracepoints = may_insert_tracepoints_1;
4268 may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
4269 may_stop = may_stop_1;
4270 update_observer_mode ();
4273 /* Set memory write permission independently of observer mode. */
4276 set_write_memory_permission (char *args, int from_tty,
4277 struct cmd_list_element *c)
4279 /* Make the real values match the user-changed values. */
4280 may_write_memory = may_write_memory_1;
4281 update_observer_mode ();
4286 initialize_targets (void)
4288 init_dummy_target ();
4289 push_target (&dummy_target);
4291 add_info ("target", target_info, targ_desc);
4292 add_info ("files", target_info, targ_desc);
4294 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
4295 Set target debugging."), _("\
4296 Show target debugging."), _("\
4297 When non-zero, target debugging is enabled. Higher numbers are more\n\
4298 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
4302 &setdebuglist, &showdebuglist);
4304 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
4305 &trust_readonly, _("\
4306 Set mode for reading from readonly sections."), _("\
4307 Show mode for reading from readonly sections."), _("\
4308 When this mode is on, memory reads from readonly sections (such as .text)\n\
4309 will be read from the object file instead of from the target. This will\n\
4310 result in significant performance improvement for remote targets."),
4312 show_trust_readonly,
4313 &setlist, &showlist);
4315 add_com ("monitor", class_obscure, do_monitor_command,
4316 _("Send a command to the remote monitor (remote targets only)."));
4318 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
4319 _("Print the name of each layer of the internal target stack."),
4320 &maintenanceprintlist);
4322 add_setshow_boolean_cmd ("target-async", no_class,
4323 &target_async_permitted_1, _("\
4324 Set whether gdb controls the inferior in asynchronous mode."), _("\
4325 Show whether gdb controls the inferior in asynchronous mode."), _("\
4326 Tells gdb whether to control the inferior in asynchronous mode."),
4327 set_maintenance_target_async_permitted,
4328 show_maintenance_target_async_permitted,
4332 add_setshow_boolean_cmd ("stack-cache", class_support,
4333 &stack_cache_enabled_p_1, _("\
4334 Set cache use for stack access."), _("\
4335 Show cache use for stack access."), _("\
4336 When on, use the data cache for all stack access, regardless of any\n\
4337 configured memory regions. This improves remote performance significantly.\n\
4338 By default, caching for stack access is on."),
4339 set_stack_cache_enabled_p,
4340 show_stack_cache_enabled_p,
4341 &setlist, &showlist);
4343 add_setshow_boolean_cmd ("may-write-registers", class_support,
4344 &may_write_registers_1, _("\
4345 Set permission to write into registers."), _("\
4346 Show permission to write into registers."), _("\
4347 When this permission is on, GDB may write into the target's registers.\n\
4348 Otherwise, any sort of write attempt will result in an error."),
4349 set_target_permissions, NULL,
4350 &setlist, &showlist);
4352 add_setshow_boolean_cmd ("may-write-memory", class_support,
4353 &may_write_memory_1, _("\
4354 Set permission to write into target memory."), _("\
4355 Show permission to write into target memory."), _("\
4356 When this permission is on, GDB may write into the target's memory.\n\
4357 Otherwise, any sort of write attempt will result in an error."),
4358 set_write_memory_permission, NULL,
4359 &setlist, &showlist);
4361 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
4362 &may_insert_breakpoints_1, _("\
4363 Set permission to insert breakpoints in the target."), _("\
4364 Show permission to insert breakpoints in the target."), _("\
4365 When this permission is on, GDB may insert breakpoints in the program.\n\
4366 Otherwise, any sort of insertion attempt will result in an error."),
4367 set_target_permissions, NULL,
4368 &setlist, &showlist);
4370 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
4371 &may_insert_tracepoints_1, _("\
4372 Set permission to insert tracepoints in the target."), _("\
4373 Show permission to insert tracepoints in the target."), _("\
4374 When this permission is on, GDB may insert tracepoints in the program.\n\
4375 Otherwise, any sort of insertion attempt will result in an error."),
4376 set_target_permissions, NULL,
4377 &setlist, &showlist);
4379 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
4380 &may_insert_fast_tracepoints_1, _("\
4381 Set permission to insert fast tracepoints in the target."), _("\
4382 Show permission to insert fast tracepoints in the target."), _("\
4383 When this permission is on, GDB may insert fast tracepoints.\n\
4384 Otherwise, any sort of insertion attempt will result in an error."),
4385 set_target_permissions, NULL,
4386 &setlist, &showlist);
4388 add_setshow_boolean_cmd ("may-interrupt", class_support,
4390 Set permission to interrupt or signal the target."), _("\
4391 Show permission to interrupt or signal the target."), _("\
4392 When this permission is on, GDB may interrupt/stop the target's execution.\n\
4393 Otherwise, any attempt to interrupt or stop will be ignored."),
4394 set_target_permissions, NULL,
4395 &setlist, &showlist);
4398 target_dcache = dcache_init ();