1 /* Select target systems and architectures at runtime for GDB.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 Contributed by Cygnus Support.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdb_string.h"
38 #include "gdb_assert.h"
40 #include "exceptions.h"
41 #include "target-descriptions.h"
42 #include "gdbthread.h"
45 #include "inline-frame.h"
47 static void target_info (char *, int);
49 static void kill_or_be_killed (int);
51 static void default_terminal_info (char *, int);
53 static int default_watchpoint_addr_within_range (struct target_ops *,
54 CORE_ADDR, CORE_ADDR, int);
56 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
58 static int nosymbol (char *, CORE_ADDR *);
60 static void tcomplain (void) ATTR_NORETURN;
62 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
64 static int return_zero (void);
66 static int return_one (void);
68 static int return_minus_one (void);
70 void target_ignore (void);
72 static void target_command (char *, int);
74 static struct target_ops *find_default_run_target (char *);
76 static void nosupport_runtime (void);
78 static LONGEST default_xfer_partial (struct target_ops *ops,
79 enum target_object object,
80 const char *annex, gdb_byte *readbuf,
81 const gdb_byte *writebuf,
82 ULONGEST offset, LONGEST len);
84 static LONGEST current_xfer_partial (struct target_ops *ops,
85 enum target_object object,
86 const char *annex, gdb_byte *readbuf,
87 const gdb_byte *writebuf,
88 ULONGEST offset, LONGEST len);
90 static LONGEST target_xfer_partial (struct target_ops *ops,
91 enum target_object object,
93 void *readbuf, const void *writebuf,
94 ULONGEST offset, LONGEST len);
96 static void init_dummy_target (void);
98 static struct target_ops debug_target;
100 static void debug_to_open (char *, int);
102 static void debug_to_prepare_to_store (struct regcache *);
104 static void debug_to_files_info (struct target_ops *);
106 static int debug_to_insert_breakpoint (struct bp_target_info *);
108 static int debug_to_remove_breakpoint (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 bp_target_info *);
114 static int debug_to_remove_hw_breakpoint (struct bp_target_info *);
116 static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
118 static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
120 static int debug_to_stopped_by_watchpoint (void);
122 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
124 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
125 CORE_ADDR, CORE_ADDR, int);
127 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
129 static void debug_to_terminal_init (void);
131 static void debug_to_terminal_inferior (void);
133 static void debug_to_terminal_ours_for_output (void);
135 static void debug_to_terminal_save_ours (void);
137 static void debug_to_terminal_ours (void);
139 static void debug_to_terminal_info (char *, int);
141 static void debug_to_load (char *, int);
143 static int debug_to_lookup_symbol (char *, CORE_ADDR *);
145 static int debug_to_can_run (void);
147 static void debug_to_notice_signals (ptid_t);
149 static void debug_to_stop (ptid_t);
151 /* NOTE: cagney/2004-09-29: Many targets reference this variable in
152 wierd and mysterious ways. Putting the variable here lets those
153 wierd and mysterious ways keep building while they are being
154 converted to the inferior inheritance structure. */
155 struct target_ops deprecated_child_ops;
157 /* Pointer to array of target architecture structures; the size of the
158 array; the current index into the array; the allocated size of the
160 struct target_ops **target_structs;
161 unsigned target_struct_size;
162 unsigned target_struct_index;
163 unsigned target_struct_allocsize;
164 #define DEFAULT_ALLOCSIZE 10
166 /* The initial current target, so that there is always a semi-valid
169 static struct target_ops dummy_target;
171 /* Top of target stack. */
173 static struct target_ops *target_stack;
175 /* The target structure we are currently using to talk to a process
176 or file or whatever "inferior" we have. */
178 struct target_ops current_target;
180 /* Command list for target. */
182 static struct cmd_list_element *targetlist = NULL;
184 /* Nonzero if we should trust readonly sections from the
185 executable when reading memory. */
187 static int trust_readonly = 0;
189 /* Nonzero if we should show true memory content including
190 memory breakpoint inserted by gdb. */
192 static int show_memory_breakpoints = 0;
194 /* Non-zero if we want to see trace of target level stuff. */
196 static int targetdebug = 0;
198 show_targetdebug (struct ui_file *file, int from_tty,
199 struct cmd_list_element *c, const char *value)
201 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
204 static void setup_target_debug (void);
206 DCACHE *target_dcache;
208 /* The user just typed 'target' without the name of a target. */
211 target_command (char *arg, int from_tty)
213 fputs_filtered ("Argument required (target name). Try `help target'\n",
217 /* Default target_has_* methods for process_stratum targets. */
220 default_child_has_all_memory (struct target_ops *ops)
222 /* If no inferior selected, then we can't read memory here. */
223 if (ptid_equal (inferior_ptid, null_ptid))
230 default_child_has_memory (struct target_ops *ops)
232 /* If no inferior selected, then we can't read memory here. */
233 if (ptid_equal (inferior_ptid, null_ptid))
240 default_child_has_stack (struct target_ops *ops)
242 /* If no inferior selected, there's no stack. */
243 if (ptid_equal (inferior_ptid, null_ptid))
250 default_child_has_registers (struct target_ops *ops)
252 /* Can't read registers from no inferior. */
253 if (ptid_equal (inferior_ptid, null_ptid))
260 default_child_has_execution (struct target_ops *ops)
262 /* If there's no thread selected, then we can't make it run through
264 if (ptid_equal (inferior_ptid, null_ptid))
272 target_has_all_memory_1 (void)
274 struct target_ops *t;
276 for (t = current_target.beneath; t != NULL; t = t->beneath)
277 if (t->to_has_all_memory (t))
284 target_has_memory_1 (void)
286 struct target_ops *t;
288 for (t = current_target.beneath; t != NULL; t = t->beneath)
289 if (t->to_has_memory (t))
296 target_has_stack_1 (void)
298 struct target_ops *t;
300 for (t = current_target.beneath; t != NULL; t = t->beneath)
301 if (t->to_has_stack (t))
308 target_has_registers_1 (void)
310 struct target_ops *t;
312 for (t = current_target.beneath; t != NULL; t = t->beneath)
313 if (t->to_has_registers (t))
320 target_has_execution_1 (void)
322 struct target_ops *t;
324 for (t = current_target.beneath; t != NULL; t = t->beneath)
325 if (t->to_has_execution (t))
331 /* Add a possible target architecture to the list. */
334 add_target (struct target_ops *t)
336 /* Provide default values for all "must have" methods. */
337 if (t->to_xfer_partial == NULL)
338 t->to_xfer_partial = default_xfer_partial;
340 if (t->to_has_all_memory == NULL)
341 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
343 if (t->to_has_memory == NULL)
344 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
346 if (t->to_has_stack == NULL)
347 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
349 if (t->to_has_registers == NULL)
350 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
352 if (t->to_has_execution == NULL)
353 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
357 target_struct_allocsize = DEFAULT_ALLOCSIZE;
358 target_structs = (struct target_ops **) xmalloc
359 (target_struct_allocsize * sizeof (*target_structs));
361 if (target_struct_size >= target_struct_allocsize)
363 target_struct_allocsize *= 2;
364 target_structs = (struct target_ops **)
365 xrealloc ((char *) target_structs,
366 target_struct_allocsize * sizeof (*target_structs));
368 target_structs[target_struct_size++] = t;
370 if (targetlist == NULL)
371 add_prefix_cmd ("target", class_run, target_command, _("\
372 Connect to a target machine or process.\n\
373 The first argument is the type or protocol of the target machine.\n\
374 Remaining arguments are interpreted by the target protocol. For more\n\
375 information on the arguments for a particular protocol, type\n\
376 `help target ' followed by the protocol name."),
377 &targetlist, "target ", 0, &cmdlist);
378 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
391 struct target_ops *t;
393 for (t = current_target.beneath; t != NULL; t = t->beneath)
394 if (t->to_kill != NULL)
397 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
407 target_load (char *arg, int from_tty)
409 dcache_invalidate (target_dcache);
410 (*current_target.to_load) (arg, from_tty);
414 target_create_inferior (char *exec_file, char *args,
415 char **env, int from_tty)
417 struct target_ops *t;
418 for (t = current_target.beneath; t != NULL; t = t->beneath)
420 if (t->to_create_inferior != NULL)
422 t->to_create_inferior (t, exec_file, args, env, from_tty);
424 fprintf_unfiltered (gdb_stdlog,
425 "target_create_inferior (%s, %s, xxx, %d)\n",
426 exec_file, args, from_tty);
431 internal_error (__FILE__, __LINE__,
432 "could not find a target to create inferior");
436 target_terminal_inferior (void)
438 /* A background resume (``run&'') should leave GDB in control of the
440 if (target_is_async_p () && !sync_execution)
443 /* If GDB is resuming the inferior in the foreground, install
444 inferior's terminal modes. */
445 (*current_target.to_terminal_inferior) ();
449 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
450 struct target_ops *t)
452 errno = EIO; /* Can't read/write this location */
453 return 0; /* No bytes handled */
459 error (_("You can't do that when your target is `%s'"),
460 current_target.to_shortname);
466 error (_("You can't do that without a process to debug."));
470 nosymbol (char *name, CORE_ADDR *addrp)
472 return 1; /* Symbol does not exist in target env */
476 nosupport_runtime (void)
478 if (ptid_equal (inferior_ptid, null_ptid))
481 error (_("No run-time support for this"));
486 default_terminal_info (char *args, int from_tty)
488 printf_unfiltered (_("No saved terminal information.\n"));
491 /* This is the default target_create_inferior and target_attach function.
492 If the current target is executing, it asks whether to kill it off.
493 If this function returns without calling error(), it has killed off
494 the target, and the operation should be attempted. */
497 kill_or_be_killed (int from_tty)
499 if (target_has_execution)
501 printf_unfiltered (_("You are already running a program:\n"));
502 target_files_info ();
503 if (query (_("Kill it? ")))
506 if (target_has_execution)
507 error (_("Killing the program did not help."));
512 error (_("Program not killed."));
518 /* A default implementation for the to_get_ada_task_ptid target method.
520 This function builds the PTID by using both LWP and TID as part of
521 the PTID lwp and tid elements. The pid used is the pid of the
525 default_get_ada_task_ptid (long lwp, long tid)
527 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
530 /* Go through the target stack from top to bottom, copying over zero
531 entries in current_target, then filling in still empty entries. In
532 effect, we are doing class inheritance through the pushed target
535 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
536 is currently implemented, is that it discards any knowledge of
537 which target an inherited method originally belonged to.
538 Consequently, new new target methods should instead explicitly and
539 locally search the target stack for the target that can handle the
543 update_current_target (void)
545 struct target_ops *t;
547 /* First, reset current's contents. */
548 memset (¤t_target, 0, sizeof (current_target));
550 #define INHERIT(FIELD, TARGET) \
551 if (!current_target.FIELD) \
552 current_target.FIELD = (TARGET)->FIELD
554 for (t = target_stack; t; t = t->beneath)
556 INHERIT (to_shortname, t);
557 INHERIT (to_longname, t);
559 /* Do not inherit to_open. */
560 /* Do not inherit to_close. */
561 /* Do not inherit to_attach. */
562 INHERIT (to_post_attach, t);
563 INHERIT (to_attach_no_wait, t);
564 /* Do not inherit to_detach. */
565 /* Do not inherit to_disconnect. */
566 /* Do not inherit to_resume. */
567 /* Do not inherit to_wait. */
568 /* Do not inherit to_fetch_registers. */
569 /* Do not inherit to_store_registers. */
570 INHERIT (to_prepare_to_store, t);
571 INHERIT (deprecated_xfer_memory, t);
572 INHERIT (to_files_info, t);
573 INHERIT (to_insert_breakpoint, t);
574 INHERIT (to_remove_breakpoint, t);
575 INHERIT (to_can_use_hw_breakpoint, t);
576 INHERIT (to_insert_hw_breakpoint, t);
577 INHERIT (to_remove_hw_breakpoint, t);
578 INHERIT (to_insert_watchpoint, t);
579 INHERIT (to_remove_watchpoint, t);
580 INHERIT (to_stopped_data_address, t);
581 INHERIT (to_have_steppable_watchpoint, t);
582 INHERIT (to_have_continuable_watchpoint, t);
583 INHERIT (to_stopped_by_watchpoint, t);
584 INHERIT (to_watchpoint_addr_within_range, t);
585 INHERIT (to_region_ok_for_hw_watchpoint, t);
586 INHERIT (to_terminal_init, t);
587 INHERIT (to_terminal_inferior, t);
588 INHERIT (to_terminal_ours_for_output, t);
589 INHERIT (to_terminal_ours, t);
590 INHERIT (to_terminal_save_ours, t);
591 INHERIT (to_terminal_info, t);
592 /* Do not inherit to_kill. */
593 INHERIT (to_load, t);
594 INHERIT (to_lookup_symbol, t);
595 /* Do no inherit to_create_inferior. */
596 INHERIT (to_post_startup_inferior, t);
597 INHERIT (to_acknowledge_created_inferior, t);
598 INHERIT (to_insert_fork_catchpoint, t);
599 INHERIT (to_remove_fork_catchpoint, t);
600 INHERIT (to_insert_vfork_catchpoint, t);
601 INHERIT (to_remove_vfork_catchpoint, t);
602 /* Do not inherit to_follow_fork. */
603 INHERIT (to_insert_exec_catchpoint, t);
604 INHERIT (to_remove_exec_catchpoint, t);
605 INHERIT (to_has_exited, t);
606 /* Do not inherit to_mourn_inferiour. */
607 INHERIT (to_can_run, t);
608 INHERIT (to_notice_signals, t);
609 /* Do not inherit to_thread_alive. */
610 /* Do not inherit to_find_new_threads. */
611 /* Do not inherit to_pid_to_str. */
612 INHERIT (to_extra_thread_info, t);
613 INHERIT (to_stop, t);
614 /* Do not inherit to_xfer_partial. */
615 INHERIT (to_rcmd, t);
616 INHERIT (to_pid_to_exec_file, t);
617 INHERIT (to_log_command, t);
618 INHERIT (to_stratum, t);
619 /* Do not inherit to_has_all_memory */
620 /* Do not inherit to_has_memory */
621 /* Do not inherit to_has_stack */
622 /* Do not inherit to_has_registers */
623 /* Do not inherit to_has_execution */
624 INHERIT (to_has_thread_control, t);
625 INHERIT (to_can_async_p, t);
626 INHERIT (to_is_async_p, t);
627 INHERIT (to_async, t);
628 INHERIT (to_async_mask, t);
629 INHERIT (to_find_memory_regions, t);
630 INHERIT (to_make_corefile_notes, t);
631 /* Do not inherit to_get_thread_local_address. */
632 INHERIT (to_can_execute_reverse, t);
633 /* Do not inherit to_read_description. */
634 INHERIT (to_get_ada_task_ptid, t);
635 /* Do not inherit to_search_memory. */
636 INHERIT (to_supports_multi_process, t);
637 INHERIT (to_magic, t);
638 /* Do not inherit to_memory_map. */
639 /* Do not inherit to_flash_erase. */
640 /* Do not inherit to_flash_done. */
644 /* Clean up a target struct so it no longer has any zero pointers in
645 it. Some entries are defaulted to a method that print an error,
646 others are hard-wired to a standard recursive default. */
648 #define de_fault(field, value) \
649 if (!current_target.field) \
650 current_target.field = value
653 (void (*) (char *, int))
658 de_fault (to_post_attach,
661 de_fault (to_prepare_to_store,
662 (void (*) (struct regcache *))
664 de_fault (deprecated_xfer_memory,
665 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
667 de_fault (to_files_info,
668 (void (*) (struct target_ops *))
670 de_fault (to_insert_breakpoint,
671 memory_insert_breakpoint);
672 de_fault (to_remove_breakpoint,
673 memory_remove_breakpoint);
674 de_fault (to_can_use_hw_breakpoint,
675 (int (*) (int, int, int))
677 de_fault (to_insert_hw_breakpoint,
678 (int (*) (struct bp_target_info *))
680 de_fault (to_remove_hw_breakpoint,
681 (int (*) (struct bp_target_info *))
683 de_fault (to_insert_watchpoint,
684 (int (*) (CORE_ADDR, int, int))
686 de_fault (to_remove_watchpoint,
687 (int (*) (CORE_ADDR, int, int))
689 de_fault (to_stopped_by_watchpoint,
692 de_fault (to_stopped_data_address,
693 (int (*) (struct target_ops *, CORE_ADDR *))
695 de_fault (to_watchpoint_addr_within_range,
696 default_watchpoint_addr_within_range);
697 de_fault (to_region_ok_for_hw_watchpoint,
698 default_region_ok_for_hw_watchpoint);
699 de_fault (to_terminal_init,
702 de_fault (to_terminal_inferior,
705 de_fault (to_terminal_ours_for_output,
708 de_fault (to_terminal_ours,
711 de_fault (to_terminal_save_ours,
714 de_fault (to_terminal_info,
715 default_terminal_info);
717 (void (*) (char *, int))
719 de_fault (to_lookup_symbol,
720 (int (*) (char *, CORE_ADDR *))
722 de_fault (to_post_startup_inferior,
725 de_fault (to_acknowledge_created_inferior,
728 de_fault (to_insert_fork_catchpoint,
731 de_fault (to_remove_fork_catchpoint,
734 de_fault (to_insert_vfork_catchpoint,
737 de_fault (to_remove_vfork_catchpoint,
740 de_fault (to_insert_exec_catchpoint,
743 de_fault (to_remove_exec_catchpoint,
746 de_fault (to_has_exited,
747 (int (*) (int, int, int *))
749 de_fault (to_can_run,
751 de_fault (to_notice_signals,
754 de_fault (to_extra_thread_info,
755 (char *(*) (struct thread_info *))
760 current_target.to_xfer_partial = current_xfer_partial;
762 (void (*) (char *, struct ui_file *))
764 de_fault (to_pid_to_exec_file,
768 (void (*) (void (*) (enum inferior_event_type, void*), void*))
770 de_fault (to_async_mask,
773 current_target.to_read_description = NULL;
774 de_fault (to_get_ada_task_ptid,
775 (ptid_t (*) (long, long))
776 default_get_ada_task_ptid);
777 de_fault (to_supports_multi_process,
782 /* Finally, position the target-stack beneath the squashed
783 "current_target". That way code looking for a non-inherited
784 target method can quickly and simply find it. */
785 current_target.beneath = target_stack;
788 setup_target_debug ();
791 /* Push a new target type into the stack of the existing target accessors,
792 possibly superseding some of the existing accessors.
794 Result is zero if the pushed target ended up on top of the stack,
795 nonzero if at least one target is on top of it.
797 Rather than allow an empty stack, we always have the dummy target at
798 the bottom stratum, so we can call the function vectors without
802 push_target (struct target_ops *t)
804 struct target_ops **cur;
806 /* Check magic number. If wrong, it probably means someone changed
807 the struct definition, but not all the places that initialize one. */
808 if (t->to_magic != OPS_MAGIC)
810 fprintf_unfiltered (gdb_stderr,
811 "Magic number of %s target struct wrong\n",
813 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
816 /* Find the proper stratum to install this target in. */
817 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
819 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
823 /* If there's already targets at this stratum, remove them. */
824 /* FIXME: cagney/2003-10-15: I think this should be popping all
825 targets to CUR, and not just those at this stratum level. */
826 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
828 /* There's already something at this stratum level. Close it,
829 and un-hook it from the stack. */
830 struct target_ops *tmp = (*cur);
831 (*cur) = (*cur)->beneath;
833 target_close (tmp, 0);
836 /* We have removed all targets in our stratum, now add the new one. */
840 update_current_target ();
843 return (t != target_stack);
846 /* Remove a target_ops vector from the stack, wherever it may be.
847 Return how many times it was removed (0 or 1). */
850 unpush_target (struct target_ops *t)
852 struct target_ops **cur;
853 struct target_ops *tmp;
855 if (t->to_stratum == dummy_stratum)
856 internal_error (__FILE__, __LINE__,
857 "Attempt to unpush the dummy target");
859 /* Look for the specified target. Note that we assume that a target
860 can only occur once in the target stack. */
862 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
869 return 0; /* Didn't find target_ops, quit now */
871 /* NOTE: cagney/2003-12-06: In '94 the close call was made
872 unconditional by moving it to before the above check that the
873 target was in the target stack (something about "Change the way
874 pushing and popping of targets work to support target overlays
875 and inheritance"). This doesn't make much sense - only open
876 targets should be closed. */
879 /* Unchain the target */
881 (*cur) = (*cur)->beneath;
884 update_current_target ();
892 target_close (target_stack, 0); /* Let it clean up */
893 if (unpush_target (target_stack) == 1)
896 fprintf_unfiltered (gdb_stderr,
897 "pop_target couldn't find target %s\n",
898 current_target.to_shortname);
899 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
903 pop_all_targets_above (enum strata above_stratum, int quitting)
905 while ((int) (current_target.to_stratum) > (int) above_stratum)
907 target_close (target_stack, quitting);
908 if (!unpush_target (target_stack))
910 fprintf_unfiltered (gdb_stderr,
911 "pop_all_targets couldn't find target %s\n",
912 target_stack->to_shortname);
913 internal_error (__FILE__, __LINE__,
914 _("failed internal consistency check"));
921 pop_all_targets (int quitting)
923 pop_all_targets_above (dummy_stratum, quitting);
926 /* Using the objfile specified in OBJFILE, find the address for the
927 current thread's thread-local storage with offset OFFSET. */
929 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
931 volatile CORE_ADDR addr = 0;
932 struct target_ops *target;
934 for (target = current_target.beneath;
936 target = target->beneath)
938 if (target->to_get_thread_local_address != NULL)
943 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
945 ptid_t ptid = inferior_ptid;
946 volatile struct gdb_exception ex;
948 TRY_CATCH (ex, RETURN_MASK_ALL)
952 /* Fetch the load module address for this objfile. */
953 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
955 /* If it's 0, throw the appropriate exception. */
957 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
958 _("TLS load module not found"));
960 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
962 /* If an error occurred, print TLS related messages here. Otherwise,
963 throw the error to some higher catcher. */
966 int objfile_is_library = (objfile->flags & OBJF_SHARED);
970 case TLS_NO_LIBRARY_SUPPORT_ERROR:
971 error (_("Cannot find thread-local variables in this thread library."));
973 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
974 if (objfile_is_library)
975 error (_("Cannot find shared library `%s' in dynamic"
976 " linker's load module list"), objfile->name);
978 error (_("Cannot find executable file `%s' in dynamic"
979 " linker's load module list"), objfile->name);
981 case TLS_NOT_ALLOCATED_YET_ERROR:
982 if (objfile_is_library)
983 error (_("The inferior has not yet allocated storage for"
984 " thread-local variables in\n"
985 "the shared library `%s'\n"
987 objfile->name, target_pid_to_str (ptid));
989 error (_("The inferior has not yet allocated storage for"
990 " thread-local variables in\n"
991 "the executable `%s'\n"
993 objfile->name, target_pid_to_str (ptid));
995 case TLS_GENERIC_ERROR:
996 if (objfile_is_library)
997 error (_("Cannot find thread-local storage for %s, "
998 "shared library %s:\n%s"),
999 target_pid_to_str (ptid),
1000 objfile->name, ex.message);
1002 error (_("Cannot find thread-local storage for %s, "
1003 "executable file %s:\n%s"),
1004 target_pid_to_str (ptid),
1005 objfile->name, ex.message);
1008 throw_exception (ex);
1013 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1014 TLS is an ABI-specific thing. But we don't do that yet. */
1016 error (_("Cannot find thread-local variables on this target"));
1022 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1024 /* target_read_string -- read a null terminated string, up to LEN bytes,
1025 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1026 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1027 is responsible for freeing it. Return the number of bytes successfully
1031 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1033 int tlen, origlen, offset, i;
1037 int buffer_allocated;
1039 unsigned int nbytes_read = 0;
1041 gdb_assert (string);
1043 /* Small for testing. */
1044 buffer_allocated = 4;
1045 buffer = xmalloc (buffer_allocated);
1052 tlen = MIN (len, 4 - (memaddr & 3));
1053 offset = memaddr & 3;
1055 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1058 /* The transfer request might have crossed the boundary to an
1059 unallocated region of memory. Retry the transfer, requesting
1063 errcode = target_read_memory (memaddr, buf, 1);
1068 if (bufptr - buffer + tlen > buffer_allocated)
1071 bytes = bufptr - buffer;
1072 buffer_allocated *= 2;
1073 buffer = xrealloc (buffer, buffer_allocated);
1074 bufptr = buffer + bytes;
1077 for (i = 0; i < tlen; i++)
1079 *bufptr++ = buf[i + offset];
1080 if (buf[i + offset] == '\000')
1082 nbytes_read += i + 1;
1089 nbytes_read += tlen;
1098 struct target_section_table *
1099 target_get_section_table (struct target_ops *target)
1101 struct target_ops *t;
1104 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1106 for (t = target; t != NULL; t = t->beneath)
1107 if (t->to_get_section_table != NULL)
1108 return (*t->to_get_section_table) (t);
1113 /* Find a section containing ADDR. */
1115 struct target_section *
1116 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1118 struct target_section_table *table = target_get_section_table (target);
1119 struct target_section *secp;
1124 for (secp = table->sections; secp < table->sections_end; secp++)
1126 if (addr >= secp->addr && addr < secp->endaddr)
1132 /* Perform a partial memory transfer. The arguments and return
1133 value are just as for target_xfer_partial. */
1136 memory_xfer_partial (struct target_ops *ops, void *readbuf, const void *writebuf,
1137 ULONGEST memaddr, LONGEST len)
1141 struct mem_region *region;
1143 /* Zero length requests are ok and require no work. */
1147 /* For accesses to unmapped overlay sections, read directly from
1148 files. Must do this first, as MEMADDR may need adjustment. */
1149 if (readbuf != NULL && overlay_debugging)
1151 struct obj_section *section = find_pc_overlay (memaddr);
1152 if (pc_in_unmapped_range (memaddr, section))
1154 struct target_section_table *table
1155 = target_get_section_table (ops);
1156 const char *section_name = section->the_bfd_section->name;
1157 memaddr = overlay_mapped_address (memaddr, section);
1158 return section_table_xfer_memory_partial (readbuf, writebuf,
1161 table->sections_end,
1166 /* Try the executable files, if "trust-readonly-sections" is set. */
1167 if (readbuf != NULL && trust_readonly)
1169 struct target_section *secp;
1170 struct target_section_table *table;
1172 secp = target_section_by_addr (ops, memaddr);
1174 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1177 table = target_get_section_table (ops);
1178 return section_table_xfer_memory_partial (readbuf, writebuf,
1181 table->sections_end,
1186 /* Try GDB's internal data cache. */
1187 region = lookup_mem_region (memaddr);
1188 /* region->hi == 0 means there's no upper bound. */
1189 if (memaddr + len < region->hi || region->hi == 0)
1192 reg_len = region->hi - memaddr;
1194 switch (region->attrib.mode)
1197 if (writebuf != NULL)
1202 if (readbuf != NULL)
1207 /* We only support writing to flash during "load" for now. */
1208 if (writebuf != NULL)
1209 error (_("Writing to flash memory forbidden in this context"));
1216 if (region->attrib.cache)
1218 /* FIXME drow/2006-08-09: This call discards OPS, so the raw
1219 memory request will start back at current_target. */
1220 if (readbuf != NULL)
1221 res = dcache_xfer_memory (target_dcache, memaddr, readbuf,
1224 /* FIXME drow/2006-08-09: If we're going to preserve const
1225 correctness dcache_xfer_memory should take readbuf and
1227 res = dcache_xfer_memory (target_dcache, memaddr,
1234 if (readbuf && !show_memory_breakpoints)
1235 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1240 /* If none of those methods found the memory we wanted, fall back
1241 to a target partial transfer. Normally a single call to
1242 to_xfer_partial is enough; if it doesn't recognize an object
1243 it will call the to_xfer_partial of the next target down.
1244 But for memory this won't do. Memory is the only target
1245 object which can be read from more than one valid target.
1246 A core file, for instance, could have some of memory but
1247 delegate other bits to the target below it. So, we must
1248 manually try all targets. */
1252 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1253 readbuf, writebuf, memaddr, reg_len);
1257 /* We want to continue past core files to executables, but not
1258 past a running target's memory. */
1259 if (ops->to_has_all_memory (ops))
1264 while (ops != NULL);
1266 if (readbuf && !show_memory_breakpoints)
1267 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1269 /* If we still haven't got anything, return the last error. We
1275 restore_show_memory_breakpoints (void *arg)
1277 show_memory_breakpoints = (uintptr_t) arg;
1281 make_show_memory_breakpoints_cleanup (int show)
1283 int current = show_memory_breakpoints;
1284 show_memory_breakpoints = show;
1286 return make_cleanup (restore_show_memory_breakpoints,
1287 (void *) (uintptr_t) current);
1291 target_xfer_partial (struct target_ops *ops,
1292 enum target_object object, const char *annex,
1293 void *readbuf, const void *writebuf,
1294 ULONGEST offset, LONGEST len)
1298 gdb_assert (ops->to_xfer_partial != NULL);
1300 /* If this is a memory transfer, let the memory-specific code
1301 have a look at it instead. Memory transfers are more
1303 if (object == TARGET_OBJECT_MEMORY)
1304 retval = memory_xfer_partial (ops, readbuf, writebuf, offset, len);
1307 enum target_object raw_object = object;
1309 /* If this is a raw memory transfer, request the normal
1310 memory object from other layers. */
1311 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1312 raw_object = TARGET_OBJECT_MEMORY;
1314 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1315 writebuf, offset, len);
1320 const unsigned char *myaddr = NULL;
1322 fprintf_unfiltered (gdb_stdlog,
1323 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1326 (annex ? annex : "(null)"),
1327 host_address_to_string (readbuf),
1328 host_address_to_string (writebuf),
1329 core_addr_to_string_nz (offset),
1330 plongest (len), plongest (retval));
1336 if (retval > 0 && myaddr != NULL)
1340 fputs_unfiltered (", bytes =", gdb_stdlog);
1341 for (i = 0; i < retval; i++)
1343 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1345 if (targetdebug < 2 && i > 0)
1347 fprintf_unfiltered (gdb_stdlog, " ...");
1350 fprintf_unfiltered (gdb_stdlog, "\n");
1353 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1357 fputc_unfiltered ('\n', gdb_stdlog);
1362 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1363 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1364 if any error occurs.
1366 If an error occurs, no guarantee is made about the contents of the data at
1367 MYADDR. In particular, the caller should not depend upon partial reads
1368 filling the buffer with good data. There is no way for the caller to know
1369 how much good data might have been transfered anyway. Callers that can
1370 deal with partial reads should call target_read (which will retry until
1371 it makes no progress, and then return how much was transferred). */
1374 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1376 /* Dispatch to the topmost target, not the flattened current_target.
1377 Memory accesses check target->to_has_(all_)memory, and the
1378 flattened target doesn't inherit those. */
1379 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1380 myaddr, memaddr, len) == len)
1387 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1389 /* Dispatch to the topmost target, not the flattened current_target.
1390 Memory accesses check target->to_has_(all_)memory, and the
1391 flattened target doesn't inherit those. */
1392 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1393 myaddr, memaddr, len) == len)
1399 /* Fetch the target's memory map. */
1402 target_memory_map (void)
1404 VEC(mem_region_s) *result;
1405 struct mem_region *last_one, *this_one;
1407 struct target_ops *t;
1410 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1412 for (t = current_target.beneath; t != NULL; t = t->beneath)
1413 if (t->to_memory_map != NULL)
1419 result = t->to_memory_map (t);
1423 qsort (VEC_address (mem_region_s, result),
1424 VEC_length (mem_region_s, result),
1425 sizeof (struct mem_region), mem_region_cmp);
1427 /* Check that regions do not overlap. Simultaneously assign
1428 a numbering for the "mem" commands to use to refer to
1431 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1433 this_one->number = ix;
1435 if (last_one && last_one->hi > this_one->lo)
1437 warning (_("Overlapping regions in memory map: ignoring"));
1438 VEC_free (mem_region_s, result);
1441 last_one = this_one;
1448 target_flash_erase (ULONGEST address, LONGEST length)
1450 struct target_ops *t;
1452 for (t = current_target.beneath; t != NULL; t = t->beneath)
1453 if (t->to_flash_erase != NULL)
1456 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1457 paddr (address), phex (length, 0));
1458 t->to_flash_erase (t, address, length);
1466 target_flash_done (void)
1468 struct target_ops *t;
1470 for (t = current_target.beneath; t != NULL; t = t->beneath)
1471 if (t->to_flash_done != NULL)
1474 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1475 t->to_flash_done (t);
1483 show_trust_readonly (struct ui_file *file, int from_tty,
1484 struct cmd_list_element *c, const char *value)
1486 fprintf_filtered (file, _("\
1487 Mode for reading from readonly sections is %s.\n"),
1491 /* More generic transfers. */
1494 default_xfer_partial (struct target_ops *ops, enum target_object object,
1495 const char *annex, gdb_byte *readbuf,
1496 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1498 if (object == TARGET_OBJECT_MEMORY
1499 && ops->deprecated_xfer_memory != NULL)
1500 /* If available, fall back to the target's
1501 "deprecated_xfer_memory" method. */
1505 if (writebuf != NULL)
1507 void *buffer = xmalloc (len);
1508 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1509 memcpy (buffer, writebuf, len);
1510 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1511 1/*write*/, NULL, ops);
1512 do_cleanups (cleanup);
1514 if (readbuf != NULL)
1515 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1516 0/*read*/, NULL, ops);
1519 else if (xfered == 0 && errno == 0)
1520 /* "deprecated_xfer_memory" uses 0, cross checked against
1521 ERRNO as one indication of an error. */
1526 else if (ops->beneath != NULL)
1527 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1528 readbuf, writebuf, offset, len);
1533 /* The xfer_partial handler for the topmost target. Unlike the default,
1534 it does not need to handle memory specially; it just passes all
1535 requests down the stack. */
1538 current_xfer_partial (struct target_ops *ops, enum target_object object,
1539 const char *annex, gdb_byte *readbuf,
1540 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1542 if (ops->beneath != NULL)
1543 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1544 readbuf, writebuf, offset, len);
1549 /* Target vector read/write partial wrapper functions.
1551 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1552 (inbuf, outbuf)", instead of separate read/write methods, make life
1556 target_read_partial (struct target_ops *ops,
1557 enum target_object object,
1558 const char *annex, gdb_byte *buf,
1559 ULONGEST offset, LONGEST len)
1561 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1565 target_write_partial (struct target_ops *ops,
1566 enum target_object object,
1567 const char *annex, const gdb_byte *buf,
1568 ULONGEST offset, LONGEST len)
1570 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1573 /* Wrappers to perform the full transfer. */
1575 target_read (struct target_ops *ops,
1576 enum target_object object,
1577 const char *annex, gdb_byte *buf,
1578 ULONGEST offset, LONGEST len)
1581 while (xfered < len)
1583 LONGEST xfer = target_read_partial (ops, object, annex,
1584 (gdb_byte *) buf + xfered,
1585 offset + xfered, len - xfered);
1586 /* Call an observer, notifying them of the xfer progress? */
1598 target_read_until_error (struct target_ops *ops,
1599 enum target_object object,
1600 const char *annex, gdb_byte *buf,
1601 ULONGEST offset, LONGEST len)
1604 while (xfered < len)
1606 LONGEST xfer = target_read_partial (ops, object, annex,
1607 (gdb_byte *) buf + xfered,
1608 offset + xfered, len - xfered);
1609 /* Call an observer, notifying them of the xfer progress? */
1614 /* We've got an error. Try to read in smaller blocks. */
1615 ULONGEST start = offset + xfered;
1616 ULONGEST remaining = len - xfered;
1619 /* If an attempt was made to read a random memory address,
1620 it's likely that the very first byte is not accessible.
1621 Try reading the first byte, to avoid doing log N tries
1623 xfer = target_read_partial (ops, object, annex,
1624 (gdb_byte *) buf + xfered, start, 1);
1633 xfer = target_read_partial (ops, object, annex,
1634 (gdb_byte *) buf + xfered,
1644 /* We have successfully read the first half. So, the
1645 error must be in the second half. Adjust start and
1646 remaining to point at the second half. */
1663 /* An alternative to target_write with progress callbacks. */
1666 target_write_with_progress (struct target_ops *ops,
1667 enum target_object object,
1668 const char *annex, const gdb_byte *buf,
1669 ULONGEST offset, LONGEST len,
1670 void (*progress) (ULONGEST, void *), void *baton)
1674 /* Give the progress callback a chance to set up. */
1676 (*progress) (0, baton);
1678 while (xfered < len)
1680 LONGEST xfer = target_write_partial (ops, object, annex,
1681 (gdb_byte *) buf + xfered,
1682 offset + xfered, len - xfered);
1690 (*progress) (xfer, baton);
1699 target_write (struct target_ops *ops,
1700 enum target_object object,
1701 const char *annex, const gdb_byte *buf,
1702 ULONGEST offset, LONGEST len)
1704 return target_write_with_progress (ops, object, annex, buf, offset, len,
1708 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1709 the size of the transferred data. PADDING additional bytes are
1710 available in *BUF_P. This is a helper function for
1711 target_read_alloc; see the declaration of that function for more
1715 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1716 const char *annex, gdb_byte **buf_p, int padding)
1718 size_t buf_alloc, buf_pos;
1722 /* This function does not have a length parameter; it reads the
1723 entire OBJECT). Also, it doesn't support objects fetched partly
1724 from one target and partly from another (in a different stratum,
1725 e.g. a core file and an executable). Both reasons make it
1726 unsuitable for reading memory. */
1727 gdb_assert (object != TARGET_OBJECT_MEMORY);
1729 /* Start by reading up to 4K at a time. The target will throttle
1730 this number down if necessary. */
1732 buf = xmalloc (buf_alloc);
1736 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1737 buf_pos, buf_alloc - buf_pos - padding);
1740 /* An error occurred. */
1746 /* Read all there was. */
1756 /* If the buffer is filling up, expand it. */
1757 if (buf_alloc < buf_pos * 2)
1760 buf = xrealloc (buf, buf_alloc);
1767 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1768 the size of the transferred data. See the declaration in "target.h"
1769 function for more information about the return value. */
1772 target_read_alloc (struct target_ops *ops, enum target_object object,
1773 const char *annex, gdb_byte **buf_p)
1775 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1778 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1779 returned as a string, allocated using xmalloc. If an error occurs
1780 or the transfer is unsupported, NULL is returned. Empty objects
1781 are returned as allocated but empty strings. A warning is issued
1782 if the result contains any embedded NUL bytes. */
1785 target_read_stralloc (struct target_ops *ops, enum target_object object,
1789 LONGEST transferred;
1791 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1793 if (transferred < 0)
1796 if (transferred == 0)
1797 return xstrdup ("");
1799 buffer[transferred] = 0;
1800 if (strlen (buffer) < transferred)
1801 warning (_("target object %d, annex %s, "
1802 "contained unexpected null characters"),
1803 (int) object, annex ? annex : "(none)");
1805 return (char *) buffer;
1808 /* Memory transfer methods. */
1811 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1814 /* This method is used to read from an alternate, non-current
1815 target. This read must bypass the overlay support (as symbols
1816 don't match this target), and GDB's internal cache (wrong cache
1817 for this target). */
1818 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
1820 memory_error (EIO, addr);
1824 get_target_memory_unsigned (struct target_ops *ops,
1825 CORE_ADDR addr, int len)
1827 gdb_byte buf[sizeof (ULONGEST)];
1829 gdb_assert (len <= sizeof (buf));
1830 get_target_memory (ops, addr, buf, len);
1831 return extract_unsigned_integer (buf, len);
1835 target_info (char *args, int from_tty)
1837 struct target_ops *t;
1838 int has_all_mem = 0;
1840 if (symfile_objfile != NULL)
1841 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
1843 for (t = target_stack; t != NULL; t = t->beneath)
1845 if (!(*t->to_has_memory) (t))
1848 if ((int) (t->to_stratum) <= (int) dummy_stratum)
1851 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1852 printf_unfiltered ("%s:\n", t->to_longname);
1853 (t->to_files_info) (t);
1854 has_all_mem = (*t->to_has_all_memory) (t);
1858 /* This function is called before any new inferior is created, e.g.
1859 by running a program, attaching, or connecting to a target.
1860 It cleans up any state from previous invocations which might
1861 change between runs. This is a subset of what target_preopen
1862 resets (things which might change between targets). */
1865 target_pre_inferior (int from_tty)
1867 /* Clear out solib state. Otherwise the solib state of the previous
1868 inferior might have survived and is entirely wrong for the new
1869 target. This has been observed on GNU/Linux using glibc 2.3. How
1881 Cannot access memory at address 0xdeadbeef
1884 /* In some OSs, the shared library list is the same/global/shared
1885 across inferiors. If code is shared between processes, so are
1886 memory regions and features. */
1887 if (!gdbarch_has_global_solist (target_gdbarch))
1889 no_shared_libraries (NULL, from_tty);
1891 invalidate_target_mem_regions ();
1893 target_clear_description ();
1897 /* This is to be called by the open routine before it does
1901 target_preopen (int from_tty)
1905 if (target_has_execution)
1908 || query (_("A program is being debugged already. Kill it? ")))
1911 error (_("Program not killed."));
1914 /* Calling target_kill may remove the target from the stack. But if
1915 it doesn't (which seems like a win for UDI), remove it now. */
1916 /* Leave the exec target, though. The user may be switching from a
1917 live process to a core of the same program. */
1918 pop_all_targets_above (file_stratum, 0);
1920 target_pre_inferior (from_tty);
1923 /* Detach a target after doing deferred register stores. */
1926 target_detach (char *args, int from_tty)
1928 struct target_ops* t;
1930 if (gdbarch_has_global_breakpoints (target_gdbarch))
1931 /* Don't remove global breakpoints here. They're removed on
1932 disconnection from the target. */
1935 /* If we're in breakpoints-always-inserted mode, have to remove
1936 them before detaching. */
1937 remove_breakpoints ();
1939 for (t = current_target.beneath; t != NULL; t = t->beneath)
1941 if (t->to_detach != NULL)
1943 t->to_detach (t, args, from_tty);
1945 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
1951 internal_error (__FILE__, __LINE__, "could not find a target to detach");
1955 target_disconnect (char *args, int from_tty)
1957 struct target_ops *t;
1959 /* If we're in breakpoints-always-inserted mode or if breakpoints
1960 are global across processes, we have to remove them before
1962 remove_breakpoints ();
1964 for (t = current_target.beneath; t != NULL; t = t->beneath)
1965 if (t->to_disconnect != NULL)
1968 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
1970 t->to_disconnect (t, args, from_tty);
1978 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
1980 struct target_ops *t;
1982 for (t = current_target.beneath; t != NULL; t = t->beneath)
1984 if (t->to_wait != NULL)
1986 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
1990 char *status_string;
1992 status_string = target_waitstatus_to_string (status);
1993 fprintf_unfiltered (gdb_stdlog,
1994 "target_wait (%d, status) = %d, %s\n",
1995 PIDGET (ptid), PIDGET (retval),
1997 xfree (status_string);
2008 target_pid_to_str (ptid_t ptid)
2010 struct target_ops *t;
2012 for (t = current_target.beneath; t != NULL; t = t->beneath)
2014 if (t->to_pid_to_str != NULL)
2015 return (*t->to_pid_to_str) (t, ptid);
2018 return normal_pid_to_str (ptid);
2022 target_resume (ptid_t ptid, int step, enum target_signal signal)
2024 struct target_ops *t;
2026 dcache_invalidate (target_dcache);
2028 for (t = current_target.beneath; t != NULL; t = t->beneath)
2030 if (t->to_resume != NULL)
2032 t->to_resume (t, ptid, step, signal);
2034 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2036 step ? "step" : "continue",
2037 target_signal_to_name (signal));
2039 set_executing (ptid, 1);
2040 set_running (ptid, 1);
2041 clear_inline_frame_state (ptid);
2048 /* Look through the list of possible targets for a target that can
2052 target_follow_fork (int follow_child)
2054 struct target_ops *t;
2056 for (t = current_target.beneath; t != NULL; t = t->beneath)
2058 if (t->to_follow_fork != NULL)
2060 int retval = t->to_follow_fork (t, follow_child);
2062 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2063 follow_child, retval);
2068 /* Some target returned a fork event, but did not know how to follow it. */
2069 internal_error (__FILE__, __LINE__,
2070 "could not find a target to follow fork");
2074 target_mourn_inferior (void)
2076 struct target_ops *t;
2077 for (t = current_target.beneath; t != NULL; t = t->beneath)
2079 if (t->to_mourn_inferior != NULL)
2081 t->to_mourn_inferior (t);
2083 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2085 /* We no longer need to keep handles on any of the object files.
2086 Make sure to release them to avoid unnecessarily locking any
2087 of them while we're not actually debugging. */
2088 bfd_cache_close_all ();
2094 internal_error (__FILE__, __LINE__,
2095 "could not find a target to follow mourn inferiour");
2098 /* Look for a target which can describe architectural features, starting
2099 from TARGET. If we find one, return its description. */
2101 const struct target_desc *
2102 target_read_description (struct target_ops *target)
2104 struct target_ops *t;
2106 for (t = target; t != NULL; t = t->beneath)
2107 if (t->to_read_description != NULL)
2109 const struct target_desc *tdesc;
2111 tdesc = t->to_read_description (t);
2119 /* The default implementation of to_search_memory.
2120 This implements a basic search of memory, reading target memory and
2121 performing the search here (as opposed to performing the search in on the
2122 target side with, for example, gdbserver). */
2125 simple_search_memory (struct target_ops *ops,
2126 CORE_ADDR start_addr, ULONGEST search_space_len,
2127 const gdb_byte *pattern, ULONGEST pattern_len,
2128 CORE_ADDR *found_addrp)
2130 /* NOTE: also defined in find.c testcase. */
2131 #define SEARCH_CHUNK_SIZE 16000
2132 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2133 /* Buffer to hold memory contents for searching. */
2134 gdb_byte *search_buf;
2135 unsigned search_buf_size;
2136 struct cleanup *old_cleanups;
2138 search_buf_size = chunk_size + pattern_len - 1;
2140 /* No point in trying to allocate a buffer larger than the search space. */
2141 if (search_space_len < search_buf_size)
2142 search_buf_size = search_space_len;
2144 search_buf = malloc (search_buf_size);
2145 if (search_buf == NULL)
2146 error (_("Unable to allocate memory to perform the search."));
2147 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2149 /* Prime the search buffer. */
2151 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2152 search_buf, start_addr, search_buf_size) != search_buf_size)
2154 warning (_("Unable to access target memory at %s, halting search."),
2155 hex_string (start_addr));
2156 do_cleanups (old_cleanups);
2160 /* Perform the search.
2162 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2163 When we've scanned N bytes we copy the trailing bytes to the start and
2164 read in another N bytes. */
2166 while (search_space_len >= pattern_len)
2168 gdb_byte *found_ptr;
2169 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2171 found_ptr = memmem (search_buf, nr_search_bytes,
2172 pattern, pattern_len);
2174 if (found_ptr != NULL)
2176 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2177 *found_addrp = found_addr;
2178 do_cleanups (old_cleanups);
2182 /* Not found in this chunk, skip to next chunk. */
2184 /* Don't let search_space_len wrap here, it's unsigned. */
2185 if (search_space_len >= chunk_size)
2186 search_space_len -= chunk_size;
2188 search_space_len = 0;
2190 if (search_space_len >= pattern_len)
2192 unsigned keep_len = search_buf_size - chunk_size;
2193 CORE_ADDR read_addr = start_addr + keep_len;
2196 /* Copy the trailing part of the previous iteration to the front
2197 of the buffer for the next iteration. */
2198 gdb_assert (keep_len == pattern_len - 1);
2199 memcpy (search_buf, search_buf + chunk_size, keep_len);
2201 nr_to_read = min (search_space_len - keep_len, chunk_size);
2203 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2204 search_buf + keep_len, read_addr,
2205 nr_to_read) != nr_to_read)
2207 warning (_("Unable to access target memory at %s, halting search."),
2208 hex_string (read_addr));
2209 do_cleanups (old_cleanups);
2213 start_addr += chunk_size;
2219 do_cleanups (old_cleanups);
2223 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2224 sequence of bytes in PATTERN with length PATTERN_LEN.
2226 The result is 1 if found, 0 if not found, and -1 if there was an error
2227 requiring halting of the search (e.g. memory read error).
2228 If the pattern is found the address is recorded in FOUND_ADDRP. */
2231 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2232 const gdb_byte *pattern, ULONGEST pattern_len,
2233 CORE_ADDR *found_addrp)
2235 struct target_ops *t;
2238 /* We don't use INHERIT to set current_target.to_search_memory,
2239 so we have to scan the target stack and handle targetdebug
2243 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2244 hex_string (start_addr));
2246 for (t = current_target.beneath; t != NULL; t = t->beneath)
2247 if (t->to_search_memory != NULL)
2252 found = t->to_search_memory (t, start_addr, search_space_len,
2253 pattern, pattern_len, found_addrp);
2257 /* If a special version of to_search_memory isn't available, use the
2259 found = simple_search_memory (current_target.beneath,
2260 start_addr, search_space_len,
2261 pattern, pattern_len, found_addrp);
2265 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2270 /* Look through the currently pushed targets. If none of them will
2271 be able to restart the currently running process, issue an error
2275 target_require_runnable (void)
2277 struct target_ops *t;
2279 for (t = target_stack; t != NULL; t = t->beneath)
2281 /* If this target knows how to create a new program, then
2282 assume we will still be able to after killing the current
2283 one. Either killing and mourning will not pop T, or else
2284 find_default_run_target will find it again. */
2285 if (t->to_create_inferior != NULL)
2288 /* Do not worry about thread_stratum targets that can not
2289 create inferiors. Assume they will be pushed again if
2290 necessary, and continue to the process_stratum. */
2291 if (t->to_stratum == thread_stratum)
2295 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2299 /* This function is only called if the target is running. In that
2300 case there should have been a process_stratum target and it
2301 should either know how to create inferiors, or not... */
2302 internal_error (__FILE__, __LINE__, "No targets found");
2305 /* Look through the list of possible targets for a target that can
2306 execute a run or attach command without any other data. This is
2307 used to locate the default process stratum.
2309 If DO_MESG is not NULL, the result is always valid (error() is
2310 called for errors); else, return NULL on error. */
2312 static struct target_ops *
2313 find_default_run_target (char *do_mesg)
2315 struct target_ops **t;
2316 struct target_ops *runable = NULL;
2321 for (t = target_structs; t < target_structs + target_struct_size;
2324 if ((*t)->to_can_run && target_can_run (*t))
2334 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2343 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2345 struct target_ops *t;
2347 t = find_default_run_target ("attach");
2348 (t->to_attach) (t, args, from_tty);
2353 find_default_create_inferior (struct target_ops *ops,
2354 char *exec_file, char *allargs, char **env,
2357 struct target_ops *t;
2359 t = find_default_run_target ("run");
2360 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2365 find_default_can_async_p (void)
2367 struct target_ops *t;
2369 /* This may be called before the target is pushed on the stack;
2370 look for the default process stratum. If there's none, gdb isn't
2371 configured with a native debugger, and target remote isn't
2373 t = find_default_run_target (NULL);
2374 if (t && t->to_can_async_p)
2375 return (t->to_can_async_p) ();
2380 find_default_is_async_p (void)
2382 struct target_ops *t;
2384 /* This may be called before the target is pushed on the stack;
2385 look for the default process stratum. If there's none, gdb isn't
2386 configured with a native debugger, and target remote isn't
2388 t = find_default_run_target (NULL);
2389 if (t && t->to_is_async_p)
2390 return (t->to_is_async_p) ();
2395 find_default_supports_non_stop (void)
2397 struct target_ops *t;
2399 t = find_default_run_target (NULL);
2400 if (t && t->to_supports_non_stop)
2401 return (t->to_supports_non_stop) ();
2406 target_supports_non_stop (void)
2408 struct target_ops *t;
2409 for (t = ¤t_target; t != NULL; t = t->beneath)
2410 if (t->to_supports_non_stop)
2411 return t->to_supports_non_stop ();
2418 target_get_osdata (const char *type)
2421 struct target_ops *t;
2423 /* If we're already connected to something that can get us OS
2424 related data, use it. Otherwise, try using the native
2426 if (current_target.to_stratum >= process_stratum)
2427 t = current_target.beneath;
2429 t = find_default_run_target ("get OS data");
2434 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
2438 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2440 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
2444 default_watchpoint_addr_within_range (struct target_ops *target,
2446 CORE_ADDR start, int length)
2448 return addr >= start && addr < start + length;
2464 return_minus_one (void)
2469 /* Find a single runnable target in the stack and return it. If for
2470 some reason there is more than one, return NULL. */
2473 find_run_target (void)
2475 struct target_ops **t;
2476 struct target_ops *runable = NULL;
2481 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2483 if ((*t)->to_can_run && target_can_run (*t))
2490 return (count == 1 ? runable : NULL);
2493 /* Find a single core_stratum target in the list of targets and return it.
2494 If for some reason there is more than one, return NULL. */
2497 find_core_target (void)
2499 struct target_ops **t;
2500 struct target_ops *runable = NULL;
2505 for (t = target_structs; t < target_structs + target_struct_size;
2508 if ((*t)->to_stratum == core_stratum)
2515 return (count == 1 ? runable : NULL);
2519 * Find the next target down the stack from the specified target.
2523 find_target_beneath (struct target_ops *t)
2529 /* The inferior process has died. Long live the inferior! */
2532 generic_mourn_inferior (void)
2536 ptid = inferior_ptid;
2537 inferior_ptid = null_ptid;
2539 if (!ptid_equal (ptid, null_ptid))
2541 int pid = ptid_get_pid (ptid);
2542 delete_inferior (pid);
2545 breakpoint_init_inferior (inf_exited);
2546 registers_changed ();
2548 reopen_exec_file ();
2549 reinit_frame_cache ();
2551 if (deprecated_detach_hook)
2552 deprecated_detach_hook ();
2555 /* Helper function for child_wait and the derivatives of child_wait.
2556 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2557 translation of that in OURSTATUS. */
2559 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2561 if (WIFEXITED (hoststatus))
2563 ourstatus->kind = TARGET_WAITKIND_EXITED;
2564 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2566 else if (!WIFSTOPPED (hoststatus))
2568 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2569 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2573 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2574 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2578 /* Convert a normal process ID to a string. Returns the string in a
2582 normal_pid_to_str (ptid_t ptid)
2584 static char buf[32];
2586 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2591 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2593 return normal_pid_to_str (ptid);
2596 /* Error-catcher for target_find_memory_regions */
2597 static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2599 error (_("No target."));
2603 /* Error-catcher for target_make_corefile_notes */
2604 static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2606 error (_("No target."));
2610 /* Set up the handful of non-empty slots needed by the dummy target
2614 init_dummy_target (void)
2616 dummy_target.to_shortname = "None";
2617 dummy_target.to_longname = "None";
2618 dummy_target.to_doc = "";
2619 dummy_target.to_attach = find_default_attach;
2620 dummy_target.to_detach =
2621 (void (*)(struct target_ops *, char *, int))target_ignore;
2622 dummy_target.to_create_inferior = find_default_create_inferior;
2623 dummy_target.to_can_async_p = find_default_can_async_p;
2624 dummy_target.to_is_async_p = find_default_is_async_p;
2625 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
2626 dummy_target.to_pid_to_str = dummy_pid_to_str;
2627 dummy_target.to_stratum = dummy_stratum;
2628 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2629 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2630 dummy_target.to_xfer_partial = default_xfer_partial;
2631 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2632 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2633 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2634 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2635 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
2636 dummy_target.to_magic = OPS_MAGIC;
2640 debug_to_open (char *args, int from_tty)
2642 debug_target.to_open (args, from_tty);
2644 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2648 target_close (struct target_ops *targ, int quitting)
2650 if (targ->to_xclose != NULL)
2651 targ->to_xclose (targ, quitting);
2652 else if (targ->to_close != NULL)
2653 targ->to_close (quitting);
2656 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2660 target_attach (char *args, int from_tty)
2662 struct target_ops *t;
2663 for (t = current_target.beneath; t != NULL; t = t->beneath)
2665 if (t->to_attach != NULL)
2667 t->to_attach (t, args, from_tty);
2669 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2675 internal_error (__FILE__, __LINE__,
2676 "could not find a target to attach");
2680 target_thread_alive (ptid_t ptid)
2682 struct target_ops *t;
2683 for (t = current_target.beneath; t != NULL; t = t->beneath)
2685 if (t->to_thread_alive != NULL)
2689 retval = t->to_thread_alive (t, ptid);
2691 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2692 PIDGET (ptid), retval);
2702 target_find_new_threads (void)
2704 struct target_ops *t;
2705 for (t = current_target.beneath; t != NULL; t = t->beneath)
2707 if (t->to_find_new_threads != NULL)
2709 t->to_find_new_threads (t);
2711 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2719 debug_to_post_attach (int pid)
2721 debug_target.to_post_attach (pid);
2723 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2726 /* Return a pretty printed form of target_waitstatus.
2727 Space for the result is malloc'd, caller must free. */
2730 target_waitstatus_to_string (const struct target_waitstatus *ws)
2732 const char *kind_str = "status->kind = ";
2736 case TARGET_WAITKIND_EXITED:
2737 return xstrprintf ("%sexited, status = %d",
2738 kind_str, ws->value.integer);
2739 case TARGET_WAITKIND_STOPPED:
2740 return xstrprintf ("%sstopped, signal = %s",
2741 kind_str, target_signal_to_name (ws->value.sig));
2742 case TARGET_WAITKIND_SIGNALLED:
2743 return xstrprintf ("%ssignalled, signal = %s",
2744 kind_str, target_signal_to_name (ws->value.sig));
2745 case TARGET_WAITKIND_LOADED:
2746 return xstrprintf ("%sloaded", kind_str);
2747 case TARGET_WAITKIND_FORKED:
2748 return xstrprintf ("%sforked", kind_str);
2749 case TARGET_WAITKIND_VFORKED:
2750 return xstrprintf ("%svforked", kind_str);
2751 case TARGET_WAITKIND_EXECD:
2752 return xstrprintf ("%sexecd", kind_str);
2753 case TARGET_WAITKIND_SYSCALL_ENTRY:
2754 return xstrprintf ("%ssyscall-entry", kind_str);
2755 case TARGET_WAITKIND_SYSCALL_RETURN:
2756 return xstrprintf ("%ssyscall-return", kind_str);
2757 case TARGET_WAITKIND_SPURIOUS:
2758 return xstrprintf ("%sspurious", kind_str);
2759 case TARGET_WAITKIND_IGNORE:
2760 return xstrprintf ("%signore", kind_str);
2761 case TARGET_WAITKIND_NO_HISTORY:
2762 return xstrprintf ("%sno-history", kind_str);
2764 return xstrprintf ("%sunknown???", kind_str);
2769 debug_print_register (const char * func,
2770 struct regcache *regcache, int regno)
2772 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2773 fprintf_unfiltered (gdb_stdlog, "%s ", func);
2774 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
2775 && gdbarch_register_name (gdbarch, regno) != NULL
2776 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2777 fprintf_unfiltered (gdb_stdlog, "(%s)",
2778 gdbarch_register_name (gdbarch, regno));
2780 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
2781 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
2783 int i, size = register_size (gdbarch, regno);
2784 unsigned char buf[MAX_REGISTER_SIZE];
2785 regcache_raw_collect (regcache, regno, buf);
2786 fprintf_unfiltered (gdb_stdlog, " = ");
2787 for (i = 0; i < size; i++)
2789 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2791 if (size <= sizeof (LONGEST))
2793 ULONGEST val = extract_unsigned_integer (buf, size);
2794 fprintf_unfiltered (gdb_stdlog, " %s %s",
2795 core_addr_to_string_nz (val), plongest (val));
2798 fprintf_unfiltered (gdb_stdlog, "\n");
2802 target_fetch_registers (struct regcache *regcache, int regno)
2804 struct target_ops *t;
2805 for (t = current_target.beneath; t != NULL; t = t->beneath)
2807 if (t->to_fetch_registers != NULL)
2809 t->to_fetch_registers (t, regcache, regno);
2811 debug_print_register ("target_fetch_registers", regcache, regno);
2818 target_store_registers (struct regcache *regcache, int regno)
2821 struct target_ops *t;
2822 for (t = current_target.beneath; t != NULL; t = t->beneath)
2824 if (t->to_store_registers != NULL)
2826 t->to_store_registers (t, regcache, regno);
2829 debug_print_register ("target_store_registers", regcache, regno);
2839 debug_to_prepare_to_store (struct regcache *regcache)
2841 debug_target.to_prepare_to_store (regcache);
2843 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
2847 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
2848 int write, struct mem_attrib *attrib,
2849 struct target_ops *target)
2853 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2856 fprintf_unfiltered (gdb_stdlog,
2857 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2858 paddress (memaddr), len, write ? "write" : "read",
2865 fputs_unfiltered (", bytes =", gdb_stdlog);
2866 for (i = 0; i < retval; i++)
2868 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
2870 if (targetdebug < 2 && i > 0)
2872 fprintf_unfiltered (gdb_stdlog, " ...");
2875 fprintf_unfiltered (gdb_stdlog, "\n");
2878 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
2882 fputc_unfiltered ('\n', gdb_stdlog);
2888 debug_to_files_info (struct target_ops *target)
2890 debug_target.to_files_info (target);
2892 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
2896 debug_to_insert_breakpoint (struct bp_target_info *bp_tgt)
2900 retval = debug_target.to_insert_breakpoint (bp_tgt);
2902 fprintf_unfiltered (gdb_stdlog,
2903 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2904 (unsigned long) bp_tgt->placed_address,
2905 (unsigned long) retval);
2910 debug_to_remove_breakpoint (struct bp_target_info *bp_tgt)
2914 retval = debug_target.to_remove_breakpoint (bp_tgt);
2916 fprintf_unfiltered (gdb_stdlog,
2917 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2918 (unsigned long) bp_tgt->placed_address,
2919 (unsigned long) retval);
2924 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2928 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2930 fprintf_unfiltered (gdb_stdlog,
2931 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2932 (unsigned long) type,
2933 (unsigned long) cnt,
2934 (unsigned long) from_tty,
2935 (unsigned long) retval);
2940 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2944 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2946 fprintf_unfiltered (gdb_stdlog,
2947 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
2948 (unsigned long) addr,
2949 (unsigned long) len,
2950 (unsigned long) retval);
2955 debug_to_stopped_by_watchpoint (void)
2959 retval = debug_target.to_stopped_by_watchpoint ();
2961 fprintf_unfiltered (gdb_stdlog,
2962 "target_stopped_by_watchpoint () = %ld\n",
2963 (unsigned long) retval);
2968 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
2972 retval = debug_target.to_stopped_data_address (target, addr);
2974 fprintf_unfiltered (gdb_stdlog,
2975 "target_stopped_data_address ([0x%lx]) = %ld\n",
2976 (unsigned long)*addr,
2977 (unsigned long)retval);
2982 debug_to_watchpoint_addr_within_range (struct target_ops *target,
2984 CORE_ADDR start, int length)
2988 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
2991 fprintf_filtered (gdb_stdlog,
2992 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
2993 (unsigned long) addr, (unsigned long) start, length,
2999 debug_to_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
3003 retval = debug_target.to_insert_hw_breakpoint (bp_tgt);
3005 fprintf_unfiltered (gdb_stdlog,
3006 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3007 (unsigned long) bp_tgt->placed_address,
3008 (unsigned long) retval);
3013 debug_to_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
3017 retval = debug_target.to_remove_hw_breakpoint (bp_tgt);
3019 fprintf_unfiltered (gdb_stdlog,
3020 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3021 (unsigned long) bp_tgt->placed_address,
3022 (unsigned long) retval);
3027 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3031 retval = debug_target.to_insert_watchpoint (addr, len, type);
3033 fprintf_unfiltered (gdb_stdlog,
3034 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3035 (unsigned long) addr, len, type, (unsigned long) retval);
3040 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3044 retval = debug_target.to_remove_watchpoint (addr, len, type);
3046 fprintf_unfiltered (gdb_stdlog,
3047 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3048 (unsigned long) addr, len, type, (unsigned long) retval);
3053 debug_to_terminal_init (void)
3055 debug_target.to_terminal_init ();
3057 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3061 debug_to_terminal_inferior (void)
3063 debug_target.to_terminal_inferior ();
3065 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3069 debug_to_terminal_ours_for_output (void)
3071 debug_target.to_terminal_ours_for_output ();
3073 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3077 debug_to_terminal_ours (void)
3079 debug_target.to_terminal_ours ();
3081 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3085 debug_to_terminal_save_ours (void)
3087 debug_target.to_terminal_save_ours ();
3089 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3093 debug_to_terminal_info (char *arg, int from_tty)
3095 debug_target.to_terminal_info (arg, from_tty);
3097 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3102 debug_to_load (char *args, int from_tty)
3104 debug_target.to_load (args, from_tty);
3106 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3110 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
3114 retval = debug_target.to_lookup_symbol (name, addrp);
3116 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
3122 debug_to_post_startup_inferior (ptid_t ptid)
3124 debug_target.to_post_startup_inferior (ptid);
3126 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3131 debug_to_acknowledge_created_inferior (int pid)
3133 debug_target.to_acknowledge_created_inferior (pid);
3135 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
3140 debug_to_insert_fork_catchpoint (int pid)
3142 debug_target.to_insert_fork_catchpoint (pid);
3144 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3149 debug_to_remove_fork_catchpoint (int pid)
3153 retval = debug_target.to_remove_fork_catchpoint (pid);
3155 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3162 debug_to_insert_vfork_catchpoint (int pid)
3164 debug_target.to_insert_vfork_catchpoint (pid);
3166 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3171 debug_to_remove_vfork_catchpoint (int pid)
3175 retval = debug_target.to_remove_vfork_catchpoint (pid);
3177 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
3184 debug_to_insert_exec_catchpoint (int pid)
3186 debug_target.to_insert_exec_catchpoint (pid);
3188 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3193 debug_to_remove_exec_catchpoint (int pid)
3197 retval = debug_target.to_remove_exec_catchpoint (pid);
3199 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
3206 debug_to_has_exited (int pid, int wait_status, int *exit_status)
3210 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3212 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
3213 pid, wait_status, *exit_status, has_exited);
3219 debug_to_can_run (void)
3223 retval = debug_target.to_can_run ();
3225 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
3231 debug_to_notice_signals (ptid_t ptid)
3233 debug_target.to_notice_signals (ptid);
3235 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3240 debug_to_stop (ptid_t ptid)
3242 debug_target.to_stop (ptid);
3244 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3245 target_pid_to_str (ptid));
3249 debug_to_rcmd (char *command,
3250 struct ui_file *outbuf)
3252 debug_target.to_rcmd (command, outbuf);
3253 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3257 debug_to_pid_to_exec_file (int pid)
3261 exec_file = debug_target.to_pid_to_exec_file (pid);
3263 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
3270 setup_target_debug (void)
3272 memcpy (&debug_target, ¤t_target, sizeof debug_target);
3274 current_target.to_open = debug_to_open;
3275 current_target.to_post_attach = debug_to_post_attach;
3276 current_target.to_prepare_to_store = debug_to_prepare_to_store;
3277 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
3278 current_target.to_files_info = debug_to_files_info;
3279 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3280 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
3281 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3282 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3283 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3284 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3285 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3286 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3287 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3288 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
3289 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
3290 current_target.to_terminal_init = debug_to_terminal_init;
3291 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3292 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3293 current_target.to_terminal_ours = debug_to_terminal_ours;
3294 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
3295 current_target.to_terminal_info = debug_to_terminal_info;
3296 current_target.to_load = debug_to_load;
3297 current_target.to_lookup_symbol = debug_to_lookup_symbol;
3298 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3299 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
3300 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3301 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3302 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3303 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
3304 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3305 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
3306 current_target.to_has_exited = debug_to_has_exited;
3307 current_target.to_can_run = debug_to_can_run;
3308 current_target.to_notice_signals = debug_to_notice_signals;
3309 current_target.to_stop = debug_to_stop;
3310 current_target.to_rcmd = debug_to_rcmd;
3311 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
3315 static char targ_desc[] =
3316 "Names of targets and files being debugged.\n\
3317 Shows the entire stack of targets currently in use (including the exec-file,\n\
3318 core-file, and process, if any), as well as the symbol file name.";
3321 do_monitor_command (char *cmd,
3324 if ((current_target.to_rcmd
3325 == (void (*) (char *, struct ui_file *)) tcomplain)
3326 || (current_target.to_rcmd == debug_to_rcmd
3327 && (debug_target.to_rcmd
3328 == (void (*) (char *, struct ui_file *)) tcomplain)))
3329 error (_("\"monitor\" command not supported by this target."));
3330 target_rcmd (cmd, gdb_stdtarg);
3333 /* Print the name of each layers of our target stack. */
3336 maintenance_print_target_stack (char *cmd, int from_tty)
3338 struct target_ops *t;
3340 printf_filtered (_("The current target stack is:\n"));
3342 for (t = target_stack; t != NULL; t = t->beneath)
3344 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3348 /* Controls if async mode is permitted. */
3349 int target_async_permitted = 0;
3351 /* The set command writes to this variable. If the inferior is
3352 executing, linux_nat_async_permitted is *not* updated. */
3353 static int target_async_permitted_1 = 0;
3356 set_maintenance_target_async_permitted (char *args, int from_tty,
3357 struct cmd_list_element *c)
3359 if (have_live_inferiors ())
3361 target_async_permitted_1 = target_async_permitted;
3362 error (_("Cannot change this setting while the inferior is running."));
3365 target_async_permitted = target_async_permitted_1;
3369 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3370 struct cmd_list_element *c,
3373 fprintf_filtered (file, _("\
3374 Controlling the inferior in asynchronous mode is %s.\n"), value);
3378 initialize_targets (void)
3380 init_dummy_target ();
3381 push_target (&dummy_target);
3383 add_info ("target", target_info, targ_desc);
3384 add_info ("files", target_info, targ_desc);
3386 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3387 Set target debugging."), _("\
3388 Show target debugging."), _("\
3389 When non-zero, target debugging is enabled. Higher numbers are more\n\
3390 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3394 &setdebuglist, &showdebuglist);
3396 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
3397 &trust_readonly, _("\
3398 Set mode for reading from readonly sections."), _("\
3399 Show mode for reading from readonly sections."), _("\
3400 When this mode is on, memory reads from readonly sections (such as .text)\n\
3401 will be read from the object file instead of from the target. This will\n\
3402 result in significant performance improvement for remote targets."),
3404 show_trust_readonly,
3405 &setlist, &showlist);
3407 add_com ("monitor", class_obscure, do_monitor_command,
3408 _("Send a command to the remote monitor (remote targets only)."));
3410 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3411 _("Print the name of each layer of the internal target stack."),
3412 &maintenanceprintlist);
3414 add_setshow_boolean_cmd ("target-async", no_class,
3415 &target_async_permitted_1, _("\
3416 Set whether gdb controls the inferior in asynchronous mode."), _("\
3417 Show whether gdb controls the inferior in asynchronous mode."), _("\
3418 Tells gdb whether to control the inferior in asynchronous mode."),
3419 set_maintenance_target_async_permitted,
3420 show_maintenance_target_async_permitted,
3424 target_dcache = dcache_init ();