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 struct gdbarch *default_thread_architecture (struct target_ops *ops,
99 static void init_dummy_target (void);
101 static struct target_ops debug_target;
103 static void debug_to_open (char *, int);
105 static void debug_to_prepare_to_store (struct regcache *);
107 static void debug_to_files_info (struct target_ops *);
109 static int debug_to_insert_breakpoint (struct gdbarch *,
110 struct bp_target_info *);
112 static int debug_to_remove_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
115 static int debug_to_can_use_hw_breakpoint (int, int, int);
117 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
118 struct bp_target_info *);
120 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
121 struct bp_target_info *);
123 static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
125 static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
127 static int debug_to_stopped_by_watchpoint (void);
129 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
131 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
132 CORE_ADDR, CORE_ADDR, int);
134 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
136 static void debug_to_terminal_init (void);
138 static void debug_to_terminal_inferior (void);
140 static void debug_to_terminal_ours_for_output (void);
142 static void debug_to_terminal_save_ours (void);
144 static void debug_to_terminal_ours (void);
146 static void debug_to_terminal_info (char *, int);
148 static void debug_to_load (char *, int);
150 static int debug_to_lookup_symbol (char *, CORE_ADDR *);
152 static int debug_to_can_run (void);
154 static void debug_to_notice_signals (ptid_t);
156 static void debug_to_stop (ptid_t);
158 /* NOTE: cagney/2004-09-29: Many targets reference this variable in
159 wierd and mysterious ways. Putting the variable here lets those
160 wierd and mysterious ways keep building while they are being
161 converted to the inferior inheritance structure. */
162 struct target_ops deprecated_child_ops;
164 /* Pointer to array of target architecture structures; the size of the
165 array; the current index into the array; the allocated size of the
167 struct target_ops **target_structs;
168 unsigned target_struct_size;
169 unsigned target_struct_index;
170 unsigned target_struct_allocsize;
171 #define DEFAULT_ALLOCSIZE 10
173 /* The initial current target, so that there is always a semi-valid
176 static struct target_ops dummy_target;
178 /* Top of target stack. */
180 static struct target_ops *target_stack;
182 /* The target structure we are currently using to talk to a process
183 or file or whatever "inferior" we have. */
185 struct target_ops current_target;
187 /* Command list for target. */
189 static struct cmd_list_element *targetlist = NULL;
191 /* Nonzero if we should trust readonly sections from the
192 executable when reading memory. */
194 static int trust_readonly = 0;
196 /* Nonzero if we should show true memory content including
197 memory breakpoint inserted by gdb. */
199 static int show_memory_breakpoints = 0;
201 /* Non-zero if we want to see trace of target level stuff. */
203 static int targetdebug = 0;
205 show_targetdebug (struct ui_file *file, int from_tty,
206 struct cmd_list_element *c, const char *value)
208 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
211 static void setup_target_debug (void);
213 DCACHE *target_dcache;
215 /* The user just typed 'target' without the name of a target. */
218 target_command (char *arg, int from_tty)
220 fputs_filtered ("Argument required (target name). Try `help target'\n",
224 /* Default target_has_* methods for process_stratum targets. */
227 default_child_has_all_memory (struct target_ops *ops)
229 /* If no inferior selected, then we can't read memory here. */
230 if (ptid_equal (inferior_ptid, null_ptid))
237 default_child_has_memory (struct target_ops *ops)
239 /* If no inferior selected, then we can't read memory here. */
240 if (ptid_equal (inferior_ptid, null_ptid))
247 default_child_has_stack (struct target_ops *ops)
249 /* If no inferior selected, there's no stack. */
250 if (ptid_equal (inferior_ptid, null_ptid))
257 default_child_has_registers (struct target_ops *ops)
259 /* Can't read registers from no inferior. */
260 if (ptid_equal (inferior_ptid, null_ptid))
267 default_child_has_execution (struct target_ops *ops)
269 /* If there's no thread selected, then we can't make it run through
271 if (ptid_equal (inferior_ptid, null_ptid))
279 target_has_all_memory_1 (void)
281 struct target_ops *t;
283 for (t = current_target.beneath; t != NULL; t = t->beneath)
284 if (t->to_has_all_memory (t))
291 target_has_memory_1 (void)
293 struct target_ops *t;
295 for (t = current_target.beneath; t != NULL; t = t->beneath)
296 if (t->to_has_memory (t))
303 target_has_stack_1 (void)
305 struct target_ops *t;
307 for (t = current_target.beneath; t != NULL; t = t->beneath)
308 if (t->to_has_stack (t))
315 target_has_registers_1 (void)
317 struct target_ops *t;
319 for (t = current_target.beneath; t != NULL; t = t->beneath)
320 if (t->to_has_registers (t))
327 target_has_execution_1 (void)
329 struct target_ops *t;
331 for (t = current_target.beneath; t != NULL; t = t->beneath)
332 if (t->to_has_execution (t))
338 /* Add a possible target architecture to the list. */
341 add_target (struct target_ops *t)
343 /* Provide default values for all "must have" methods. */
344 if (t->to_xfer_partial == NULL)
345 t->to_xfer_partial = default_xfer_partial;
347 if (t->to_has_all_memory == NULL)
348 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
350 if (t->to_has_memory == NULL)
351 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
353 if (t->to_has_stack == NULL)
354 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
356 if (t->to_has_registers == NULL)
357 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
359 if (t->to_has_execution == NULL)
360 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
364 target_struct_allocsize = DEFAULT_ALLOCSIZE;
365 target_structs = (struct target_ops **) xmalloc
366 (target_struct_allocsize * sizeof (*target_structs));
368 if (target_struct_size >= target_struct_allocsize)
370 target_struct_allocsize *= 2;
371 target_structs = (struct target_ops **)
372 xrealloc ((char *) target_structs,
373 target_struct_allocsize * sizeof (*target_structs));
375 target_structs[target_struct_size++] = t;
377 if (targetlist == NULL)
378 add_prefix_cmd ("target", class_run, target_command, _("\
379 Connect to a target machine or process.\n\
380 The first argument is the type or protocol of the target machine.\n\
381 Remaining arguments are interpreted by the target protocol. For more\n\
382 information on the arguments for a particular protocol, type\n\
383 `help target ' followed by the protocol name."),
384 &targetlist, "target ", 0, &cmdlist);
385 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
398 struct target_ops *t;
400 for (t = current_target.beneath; t != NULL; t = t->beneath)
401 if (t->to_kill != NULL)
404 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
414 target_load (char *arg, int from_tty)
416 dcache_invalidate (target_dcache);
417 (*current_target.to_load) (arg, from_tty);
421 target_create_inferior (char *exec_file, char *args,
422 char **env, int from_tty)
424 struct target_ops *t;
425 for (t = current_target.beneath; t != NULL; t = t->beneath)
427 if (t->to_create_inferior != NULL)
429 t->to_create_inferior (t, exec_file, args, env, from_tty);
431 fprintf_unfiltered (gdb_stdlog,
432 "target_create_inferior (%s, %s, xxx, %d)\n",
433 exec_file, args, from_tty);
438 internal_error (__FILE__, __LINE__,
439 "could not find a target to create inferior");
443 target_terminal_inferior (void)
445 /* A background resume (``run&'') should leave GDB in control of the
447 if (target_is_async_p () && !sync_execution)
450 /* If GDB is resuming the inferior in the foreground, install
451 inferior's terminal modes. */
452 (*current_target.to_terminal_inferior) ();
456 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
457 struct target_ops *t)
459 errno = EIO; /* Can't read/write this location */
460 return 0; /* No bytes handled */
466 error (_("You can't do that when your target is `%s'"),
467 current_target.to_shortname);
473 error (_("You can't do that without a process to debug."));
477 nosymbol (char *name, CORE_ADDR *addrp)
479 return 1; /* Symbol does not exist in target env */
483 nosupport_runtime (void)
485 if (ptid_equal (inferior_ptid, null_ptid))
488 error (_("No run-time support for this"));
493 default_terminal_info (char *args, int from_tty)
495 printf_unfiltered (_("No saved terminal information.\n"));
498 /* This is the default target_create_inferior and target_attach function.
499 If the current target is executing, it asks whether to kill it off.
500 If this function returns without calling error(), it has killed off
501 the target, and the operation should be attempted. */
504 kill_or_be_killed (int from_tty)
506 if (target_has_execution)
508 printf_unfiltered (_("You are already running a program:\n"));
509 target_files_info ();
510 if (query (_("Kill it? ")))
513 if (target_has_execution)
514 error (_("Killing the program did not help."));
519 error (_("Program not killed."));
525 /* A default implementation for the to_get_ada_task_ptid target method.
527 This function builds the PTID by using both LWP and TID as part of
528 the PTID lwp and tid elements. The pid used is the pid of the
532 default_get_ada_task_ptid (long lwp, long tid)
534 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
537 /* Go through the target stack from top to bottom, copying over zero
538 entries in current_target, then filling in still empty entries. In
539 effect, we are doing class inheritance through the pushed target
542 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
543 is currently implemented, is that it discards any knowledge of
544 which target an inherited method originally belonged to.
545 Consequently, new new target methods should instead explicitly and
546 locally search the target stack for the target that can handle the
550 update_current_target (void)
552 struct target_ops *t;
554 /* First, reset current's contents. */
555 memset (¤t_target, 0, sizeof (current_target));
557 #define INHERIT(FIELD, TARGET) \
558 if (!current_target.FIELD) \
559 current_target.FIELD = (TARGET)->FIELD
561 for (t = target_stack; t; t = t->beneath)
563 INHERIT (to_shortname, t);
564 INHERIT (to_longname, t);
566 /* Do not inherit to_open. */
567 /* Do not inherit to_close. */
568 /* Do not inherit to_attach. */
569 INHERIT (to_post_attach, t);
570 INHERIT (to_attach_no_wait, t);
571 /* Do not inherit to_detach. */
572 /* Do not inherit to_disconnect. */
573 /* Do not inherit to_resume. */
574 /* Do not inherit to_wait. */
575 /* Do not inherit to_fetch_registers. */
576 /* Do not inherit to_store_registers. */
577 INHERIT (to_prepare_to_store, t);
578 INHERIT (deprecated_xfer_memory, t);
579 INHERIT (to_files_info, t);
580 INHERIT (to_insert_breakpoint, t);
581 INHERIT (to_remove_breakpoint, t);
582 INHERIT (to_can_use_hw_breakpoint, t);
583 INHERIT (to_insert_hw_breakpoint, t);
584 INHERIT (to_remove_hw_breakpoint, t);
585 INHERIT (to_insert_watchpoint, t);
586 INHERIT (to_remove_watchpoint, t);
587 INHERIT (to_stopped_data_address, t);
588 INHERIT (to_have_steppable_watchpoint, t);
589 INHERIT (to_have_continuable_watchpoint, t);
590 INHERIT (to_stopped_by_watchpoint, t);
591 INHERIT (to_watchpoint_addr_within_range, t);
592 INHERIT (to_region_ok_for_hw_watchpoint, t);
593 INHERIT (to_terminal_init, t);
594 INHERIT (to_terminal_inferior, t);
595 INHERIT (to_terminal_ours_for_output, t);
596 INHERIT (to_terminal_ours, t);
597 INHERIT (to_terminal_save_ours, t);
598 INHERIT (to_terminal_info, t);
599 /* Do not inherit to_kill. */
600 INHERIT (to_load, t);
601 INHERIT (to_lookup_symbol, t);
602 /* Do no inherit to_create_inferior. */
603 INHERIT (to_post_startup_inferior, t);
604 INHERIT (to_acknowledge_created_inferior, t);
605 INHERIT (to_insert_fork_catchpoint, t);
606 INHERIT (to_remove_fork_catchpoint, t);
607 INHERIT (to_insert_vfork_catchpoint, t);
608 INHERIT (to_remove_vfork_catchpoint, t);
609 /* Do not inherit to_follow_fork. */
610 INHERIT (to_insert_exec_catchpoint, t);
611 INHERIT (to_remove_exec_catchpoint, t);
612 INHERIT (to_has_exited, t);
613 /* Do not inherit to_mourn_inferiour. */
614 INHERIT (to_can_run, t);
615 INHERIT (to_notice_signals, t);
616 /* Do not inherit to_thread_alive. */
617 /* Do not inherit to_find_new_threads. */
618 /* Do not inherit to_pid_to_str. */
619 INHERIT (to_extra_thread_info, t);
620 INHERIT (to_stop, t);
621 /* Do not inherit to_xfer_partial. */
622 INHERIT (to_rcmd, t);
623 INHERIT (to_pid_to_exec_file, t);
624 INHERIT (to_log_command, t);
625 INHERIT (to_stratum, t);
626 /* Do not inherit to_has_all_memory */
627 /* Do not inherit to_has_memory */
628 /* Do not inherit to_has_stack */
629 /* Do not inherit to_has_registers */
630 /* Do not inherit to_has_execution */
631 INHERIT (to_has_thread_control, t);
632 INHERIT (to_can_async_p, t);
633 INHERIT (to_is_async_p, t);
634 INHERIT (to_async, t);
635 INHERIT (to_async_mask, t);
636 INHERIT (to_find_memory_regions, t);
637 INHERIT (to_make_corefile_notes, t);
638 /* Do not inherit to_get_thread_local_address. */
639 INHERIT (to_can_execute_reverse, t);
640 INHERIT (to_thread_architecture, t);
641 /* Do not inherit to_read_description. */
642 INHERIT (to_get_ada_task_ptid, t);
643 /* Do not inherit to_search_memory. */
644 INHERIT (to_supports_multi_process, t);
645 INHERIT (to_magic, t);
646 /* Do not inherit to_memory_map. */
647 /* Do not inherit to_flash_erase. */
648 /* Do not inherit to_flash_done. */
652 /* Clean up a target struct so it no longer has any zero pointers in
653 it. Some entries are defaulted to a method that print an error,
654 others are hard-wired to a standard recursive default. */
656 #define de_fault(field, value) \
657 if (!current_target.field) \
658 current_target.field = value
661 (void (*) (char *, int))
666 de_fault (to_post_attach,
669 de_fault (to_prepare_to_store,
670 (void (*) (struct regcache *))
672 de_fault (deprecated_xfer_memory,
673 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
675 de_fault (to_files_info,
676 (void (*) (struct target_ops *))
678 de_fault (to_insert_breakpoint,
679 memory_insert_breakpoint);
680 de_fault (to_remove_breakpoint,
681 memory_remove_breakpoint);
682 de_fault (to_can_use_hw_breakpoint,
683 (int (*) (int, int, int))
685 de_fault (to_insert_hw_breakpoint,
686 (int (*) (struct gdbarch *, struct bp_target_info *))
688 de_fault (to_remove_hw_breakpoint,
689 (int (*) (struct gdbarch *, struct bp_target_info *))
691 de_fault (to_insert_watchpoint,
692 (int (*) (CORE_ADDR, int, int))
694 de_fault (to_remove_watchpoint,
695 (int (*) (CORE_ADDR, int, int))
697 de_fault (to_stopped_by_watchpoint,
700 de_fault (to_stopped_data_address,
701 (int (*) (struct target_ops *, CORE_ADDR *))
703 de_fault (to_watchpoint_addr_within_range,
704 default_watchpoint_addr_within_range);
705 de_fault (to_region_ok_for_hw_watchpoint,
706 default_region_ok_for_hw_watchpoint);
707 de_fault (to_terminal_init,
710 de_fault (to_terminal_inferior,
713 de_fault (to_terminal_ours_for_output,
716 de_fault (to_terminal_ours,
719 de_fault (to_terminal_save_ours,
722 de_fault (to_terminal_info,
723 default_terminal_info);
725 (void (*) (char *, int))
727 de_fault (to_lookup_symbol,
728 (int (*) (char *, CORE_ADDR *))
730 de_fault (to_post_startup_inferior,
733 de_fault (to_acknowledge_created_inferior,
736 de_fault (to_insert_fork_catchpoint,
739 de_fault (to_remove_fork_catchpoint,
742 de_fault (to_insert_vfork_catchpoint,
745 de_fault (to_remove_vfork_catchpoint,
748 de_fault (to_insert_exec_catchpoint,
751 de_fault (to_remove_exec_catchpoint,
754 de_fault (to_has_exited,
755 (int (*) (int, int, int *))
757 de_fault (to_can_run,
759 de_fault (to_notice_signals,
762 de_fault (to_extra_thread_info,
763 (char *(*) (struct thread_info *))
768 current_target.to_xfer_partial = current_xfer_partial;
770 (void (*) (char *, struct ui_file *))
772 de_fault (to_pid_to_exec_file,
776 (void (*) (void (*) (enum inferior_event_type, void*), void*))
778 de_fault (to_async_mask,
781 de_fault (to_thread_architecture,
782 default_thread_architecture);
783 current_target.to_read_description = NULL;
784 de_fault (to_get_ada_task_ptid,
785 (ptid_t (*) (long, long))
786 default_get_ada_task_ptid);
787 de_fault (to_supports_multi_process,
792 /* Finally, position the target-stack beneath the squashed
793 "current_target". That way code looking for a non-inherited
794 target method can quickly and simply find it. */
795 current_target.beneath = target_stack;
798 setup_target_debug ();
801 /* Push a new target type into the stack of the existing target accessors,
802 possibly superseding some of the existing accessors.
804 Result is zero if the pushed target ended up on top of the stack,
805 nonzero if at least one target is on top of it.
807 Rather than allow an empty stack, we always have the dummy target at
808 the bottom stratum, so we can call the function vectors without
812 push_target (struct target_ops *t)
814 struct target_ops **cur;
816 /* Check magic number. If wrong, it probably means someone changed
817 the struct definition, but not all the places that initialize one. */
818 if (t->to_magic != OPS_MAGIC)
820 fprintf_unfiltered (gdb_stderr,
821 "Magic number of %s target struct wrong\n",
823 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
826 /* Find the proper stratum to install this target in. */
827 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
829 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
833 /* If there's already targets at this stratum, remove them. */
834 /* FIXME: cagney/2003-10-15: I think this should be popping all
835 targets to CUR, and not just those at this stratum level. */
836 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
838 /* There's already something at this stratum level. Close it,
839 and un-hook it from the stack. */
840 struct target_ops *tmp = (*cur);
841 (*cur) = (*cur)->beneath;
843 target_close (tmp, 0);
846 /* We have removed all targets in our stratum, now add the new one. */
850 update_current_target ();
853 return (t != target_stack);
856 /* Remove a target_ops vector from the stack, wherever it may be.
857 Return how many times it was removed (0 or 1). */
860 unpush_target (struct target_ops *t)
862 struct target_ops **cur;
863 struct target_ops *tmp;
865 if (t->to_stratum == dummy_stratum)
866 internal_error (__FILE__, __LINE__,
867 "Attempt to unpush the dummy target");
869 /* Look for the specified target. Note that we assume that a target
870 can only occur once in the target stack. */
872 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
879 return 0; /* Didn't find target_ops, quit now */
881 /* NOTE: cagney/2003-12-06: In '94 the close call was made
882 unconditional by moving it to before the above check that the
883 target was in the target stack (something about "Change the way
884 pushing and popping of targets work to support target overlays
885 and inheritance"). This doesn't make much sense - only open
886 targets should be closed. */
889 /* Unchain the target */
891 (*cur) = (*cur)->beneath;
894 update_current_target ();
902 target_close (target_stack, 0); /* Let it clean up */
903 if (unpush_target (target_stack) == 1)
906 fprintf_unfiltered (gdb_stderr,
907 "pop_target couldn't find target %s\n",
908 current_target.to_shortname);
909 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
913 pop_all_targets_above (enum strata above_stratum, int quitting)
915 while ((int) (current_target.to_stratum) > (int) above_stratum)
917 target_close (target_stack, quitting);
918 if (!unpush_target (target_stack))
920 fprintf_unfiltered (gdb_stderr,
921 "pop_all_targets couldn't find target %s\n",
922 target_stack->to_shortname);
923 internal_error (__FILE__, __LINE__,
924 _("failed internal consistency check"));
931 pop_all_targets (int quitting)
933 pop_all_targets_above (dummy_stratum, quitting);
936 /* Using the objfile specified in OBJFILE, find the address for the
937 current thread's thread-local storage with offset OFFSET. */
939 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
941 volatile CORE_ADDR addr = 0;
942 struct target_ops *target;
944 for (target = current_target.beneath;
946 target = target->beneath)
948 if (target->to_get_thread_local_address != NULL)
953 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
955 ptid_t ptid = inferior_ptid;
956 volatile struct gdb_exception ex;
958 TRY_CATCH (ex, RETURN_MASK_ALL)
962 /* Fetch the load module address for this objfile. */
963 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
965 /* If it's 0, throw the appropriate exception. */
967 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
968 _("TLS load module not found"));
970 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
972 /* If an error occurred, print TLS related messages here. Otherwise,
973 throw the error to some higher catcher. */
976 int objfile_is_library = (objfile->flags & OBJF_SHARED);
980 case TLS_NO_LIBRARY_SUPPORT_ERROR:
981 error (_("Cannot find thread-local variables in this thread library."));
983 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
984 if (objfile_is_library)
985 error (_("Cannot find shared library `%s' in dynamic"
986 " linker's load module list"), objfile->name);
988 error (_("Cannot find executable file `%s' in dynamic"
989 " linker's load module list"), objfile->name);
991 case TLS_NOT_ALLOCATED_YET_ERROR:
992 if (objfile_is_library)
993 error (_("The inferior has not yet allocated storage for"
994 " thread-local variables in\n"
995 "the shared library `%s'\n"
997 objfile->name, target_pid_to_str (ptid));
999 error (_("The inferior has not yet allocated storage for"
1000 " thread-local variables in\n"
1001 "the executable `%s'\n"
1003 objfile->name, target_pid_to_str (ptid));
1005 case TLS_GENERIC_ERROR:
1006 if (objfile_is_library)
1007 error (_("Cannot find thread-local storage for %s, "
1008 "shared library %s:\n%s"),
1009 target_pid_to_str (ptid),
1010 objfile->name, ex.message);
1012 error (_("Cannot find thread-local storage for %s, "
1013 "executable file %s:\n%s"),
1014 target_pid_to_str (ptid),
1015 objfile->name, ex.message);
1018 throw_exception (ex);
1023 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1024 TLS is an ABI-specific thing. But we don't do that yet. */
1026 error (_("Cannot find thread-local variables on this target"));
1032 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1034 /* target_read_string -- read a null terminated string, up to LEN bytes,
1035 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1036 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1037 is responsible for freeing it. Return the number of bytes successfully
1041 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1043 int tlen, origlen, offset, i;
1047 int buffer_allocated;
1049 unsigned int nbytes_read = 0;
1051 gdb_assert (string);
1053 /* Small for testing. */
1054 buffer_allocated = 4;
1055 buffer = xmalloc (buffer_allocated);
1062 tlen = MIN (len, 4 - (memaddr & 3));
1063 offset = memaddr & 3;
1065 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1068 /* The transfer request might have crossed the boundary to an
1069 unallocated region of memory. Retry the transfer, requesting
1073 errcode = target_read_memory (memaddr, buf, 1);
1078 if (bufptr - buffer + tlen > buffer_allocated)
1081 bytes = bufptr - buffer;
1082 buffer_allocated *= 2;
1083 buffer = xrealloc (buffer, buffer_allocated);
1084 bufptr = buffer + bytes;
1087 for (i = 0; i < tlen; i++)
1089 *bufptr++ = buf[i + offset];
1090 if (buf[i + offset] == '\000')
1092 nbytes_read += i + 1;
1099 nbytes_read += tlen;
1108 struct target_section_table *
1109 target_get_section_table (struct target_ops *target)
1111 struct target_ops *t;
1114 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1116 for (t = target; t != NULL; t = t->beneath)
1117 if (t->to_get_section_table != NULL)
1118 return (*t->to_get_section_table) (t);
1123 /* Find a section containing ADDR. */
1125 struct target_section *
1126 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1128 struct target_section_table *table = target_get_section_table (target);
1129 struct target_section *secp;
1134 for (secp = table->sections; secp < table->sections_end; secp++)
1136 if (addr >= secp->addr && addr < secp->endaddr)
1142 /* Perform a partial memory transfer. The arguments and return
1143 value are just as for target_xfer_partial. */
1146 memory_xfer_partial (struct target_ops *ops, void *readbuf, const void *writebuf,
1147 ULONGEST memaddr, LONGEST len)
1151 struct mem_region *region;
1153 /* Zero length requests are ok and require no work. */
1157 /* For accesses to unmapped overlay sections, read directly from
1158 files. Must do this first, as MEMADDR may need adjustment. */
1159 if (readbuf != NULL && overlay_debugging)
1161 struct obj_section *section = find_pc_overlay (memaddr);
1162 if (pc_in_unmapped_range (memaddr, section))
1164 struct target_section_table *table
1165 = target_get_section_table (ops);
1166 const char *section_name = section->the_bfd_section->name;
1167 memaddr = overlay_mapped_address (memaddr, section);
1168 return section_table_xfer_memory_partial (readbuf, writebuf,
1171 table->sections_end,
1176 /* Try the executable files, if "trust-readonly-sections" is set. */
1177 if (readbuf != NULL && trust_readonly)
1179 struct target_section *secp;
1180 struct target_section_table *table;
1182 secp = target_section_by_addr (ops, memaddr);
1184 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1187 table = target_get_section_table (ops);
1188 return section_table_xfer_memory_partial (readbuf, writebuf,
1191 table->sections_end,
1196 /* Try GDB's internal data cache. */
1197 region = lookup_mem_region (memaddr);
1198 /* region->hi == 0 means there's no upper bound. */
1199 if (memaddr + len < region->hi || region->hi == 0)
1202 reg_len = region->hi - memaddr;
1204 switch (region->attrib.mode)
1207 if (writebuf != NULL)
1212 if (readbuf != NULL)
1217 /* We only support writing to flash during "load" for now. */
1218 if (writebuf != NULL)
1219 error (_("Writing to flash memory forbidden in this context"));
1226 if (region->attrib.cache)
1228 /* FIXME drow/2006-08-09: This call discards OPS, so the raw
1229 memory request will start back at current_target. */
1230 if (readbuf != NULL)
1231 res = dcache_xfer_memory (target_dcache, memaddr, readbuf,
1234 /* FIXME drow/2006-08-09: If we're going to preserve const
1235 correctness dcache_xfer_memory should take readbuf and
1237 res = dcache_xfer_memory (target_dcache, memaddr,
1244 if (readbuf && !show_memory_breakpoints)
1245 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1250 /* If none of those methods found the memory we wanted, fall back
1251 to a target partial transfer. Normally a single call to
1252 to_xfer_partial is enough; if it doesn't recognize an object
1253 it will call the to_xfer_partial of the next target down.
1254 But for memory this won't do. Memory is the only target
1255 object which can be read from more than one valid target.
1256 A core file, for instance, could have some of memory but
1257 delegate other bits to the target below it. So, we must
1258 manually try all targets. */
1262 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1263 readbuf, writebuf, memaddr, reg_len);
1267 /* We want to continue past core files to executables, but not
1268 past a running target's memory. */
1269 if (ops->to_has_all_memory (ops))
1274 while (ops != NULL);
1276 if (readbuf && !show_memory_breakpoints)
1277 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1279 /* If we still haven't got anything, return the last error. We
1285 restore_show_memory_breakpoints (void *arg)
1287 show_memory_breakpoints = (uintptr_t) arg;
1291 make_show_memory_breakpoints_cleanup (int show)
1293 int current = show_memory_breakpoints;
1294 show_memory_breakpoints = show;
1296 return make_cleanup (restore_show_memory_breakpoints,
1297 (void *) (uintptr_t) current);
1301 target_xfer_partial (struct target_ops *ops,
1302 enum target_object object, const char *annex,
1303 void *readbuf, const void *writebuf,
1304 ULONGEST offset, LONGEST len)
1308 gdb_assert (ops->to_xfer_partial != NULL);
1310 /* If this is a memory transfer, let the memory-specific code
1311 have a look at it instead. Memory transfers are more
1313 if (object == TARGET_OBJECT_MEMORY)
1314 retval = memory_xfer_partial (ops, readbuf, writebuf, offset, len);
1317 enum target_object raw_object = object;
1319 /* If this is a raw memory transfer, request the normal
1320 memory object from other layers. */
1321 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1322 raw_object = TARGET_OBJECT_MEMORY;
1324 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1325 writebuf, offset, len);
1330 const unsigned char *myaddr = NULL;
1332 fprintf_unfiltered (gdb_stdlog,
1333 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1336 (annex ? annex : "(null)"),
1337 host_address_to_string (readbuf),
1338 host_address_to_string (writebuf),
1339 core_addr_to_string_nz (offset),
1340 plongest (len), plongest (retval));
1346 if (retval > 0 && myaddr != NULL)
1350 fputs_unfiltered (", bytes =", gdb_stdlog);
1351 for (i = 0; i < retval; i++)
1353 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1355 if (targetdebug < 2 && i > 0)
1357 fprintf_unfiltered (gdb_stdlog, " ...");
1360 fprintf_unfiltered (gdb_stdlog, "\n");
1363 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1367 fputc_unfiltered ('\n', gdb_stdlog);
1372 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1373 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1374 if any error occurs.
1376 If an error occurs, no guarantee is made about the contents of the data at
1377 MYADDR. In particular, the caller should not depend upon partial reads
1378 filling the buffer with good data. There is no way for the caller to know
1379 how much good data might have been transfered anyway. Callers that can
1380 deal with partial reads should call target_read (which will retry until
1381 it makes no progress, and then return how much was transferred). */
1384 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1386 /* Dispatch to the topmost target, not the flattened current_target.
1387 Memory accesses check target->to_has_(all_)memory, and the
1388 flattened target doesn't inherit those. */
1389 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1390 myaddr, memaddr, len) == len)
1397 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1399 /* Dispatch to the topmost target, not the flattened current_target.
1400 Memory accesses check target->to_has_(all_)memory, and the
1401 flattened target doesn't inherit those. */
1402 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1403 myaddr, memaddr, len) == len)
1409 /* Fetch the target's memory map. */
1412 target_memory_map (void)
1414 VEC(mem_region_s) *result;
1415 struct mem_region *last_one, *this_one;
1417 struct target_ops *t;
1420 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1422 for (t = current_target.beneath; t != NULL; t = t->beneath)
1423 if (t->to_memory_map != NULL)
1429 result = t->to_memory_map (t);
1433 qsort (VEC_address (mem_region_s, result),
1434 VEC_length (mem_region_s, result),
1435 sizeof (struct mem_region), mem_region_cmp);
1437 /* Check that regions do not overlap. Simultaneously assign
1438 a numbering for the "mem" commands to use to refer to
1441 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1443 this_one->number = ix;
1445 if (last_one && last_one->hi > this_one->lo)
1447 warning (_("Overlapping regions in memory map: ignoring"));
1448 VEC_free (mem_region_s, result);
1451 last_one = this_one;
1458 target_flash_erase (ULONGEST address, LONGEST length)
1460 struct target_ops *t;
1462 for (t = current_target.beneath; t != NULL; t = t->beneath)
1463 if (t->to_flash_erase != NULL)
1466 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1467 hex_string (address), phex (length, 0));
1468 t->to_flash_erase (t, address, length);
1476 target_flash_done (void)
1478 struct target_ops *t;
1480 for (t = current_target.beneath; t != NULL; t = t->beneath)
1481 if (t->to_flash_done != NULL)
1484 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1485 t->to_flash_done (t);
1493 show_trust_readonly (struct ui_file *file, int from_tty,
1494 struct cmd_list_element *c, const char *value)
1496 fprintf_filtered (file, _("\
1497 Mode for reading from readonly sections is %s.\n"),
1501 /* More generic transfers. */
1504 default_xfer_partial (struct target_ops *ops, enum target_object object,
1505 const char *annex, gdb_byte *readbuf,
1506 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1508 if (object == TARGET_OBJECT_MEMORY
1509 && ops->deprecated_xfer_memory != NULL)
1510 /* If available, fall back to the target's
1511 "deprecated_xfer_memory" method. */
1515 if (writebuf != NULL)
1517 void *buffer = xmalloc (len);
1518 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1519 memcpy (buffer, writebuf, len);
1520 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1521 1/*write*/, NULL, ops);
1522 do_cleanups (cleanup);
1524 if (readbuf != NULL)
1525 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1526 0/*read*/, NULL, ops);
1529 else if (xfered == 0 && errno == 0)
1530 /* "deprecated_xfer_memory" uses 0, cross checked against
1531 ERRNO as one indication of an error. */
1536 else if (ops->beneath != NULL)
1537 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1538 readbuf, writebuf, offset, len);
1543 /* The xfer_partial handler for the topmost target. Unlike the default,
1544 it does not need to handle memory specially; it just passes all
1545 requests down the stack. */
1548 current_xfer_partial (struct target_ops *ops, enum target_object object,
1549 const char *annex, gdb_byte *readbuf,
1550 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1552 if (ops->beneath != NULL)
1553 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1554 readbuf, writebuf, offset, len);
1559 /* Target vector read/write partial wrapper functions.
1561 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1562 (inbuf, outbuf)", instead of separate read/write methods, make life
1566 target_read_partial (struct target_ops *ops,
1567 enum target_object object,
1568 const char *annex, gdb_byte *buf,
1569 ULONGEST offset, LONGEST len)
1571 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1575 target_write_partial (struct target_ops *ops,
1576 enum target_object object,
1577 const char *annex, const gdb_byte *buf,
1578 ULONGEST offset, LONGEST len)
1580 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1583 /* Wrappers to perform the full transfer. */
1585 target_read (struct target_ops *ops,
1586 enum target_object object,
1587 const char *annex, gdb_byte *buf,
1588 ULONGEST offset, LONGEST len)
1591 while (xfered < len)
1593 LONGEST xfer = target_read_partial (ops, object, annex,
1594 (gdb_byte *) buf + xfered,
1595 offset + xfered, len - xfered);
1596 /* Call an observer, notifying them of the xfer progress? */
1608 target_read_until_error (struct target_ops *ops,
1609 enum target_object object,
1610 const char *annex, gdb_byte *buf,
1611 ULONGEST offset, LONGEST len)
1614 while (xfered < len)
1616 LONGEST xfer = target_read_partial (ops, object, annex,
1617 (gdb_byte *) buf + xfered,
1618 offset + xfered, len - xfered);
1619 /* Call an observer, notifying them of the xfer progress? */
1624 /* We've got an error. Try to read in smaller blocks. */
1625 ULONGEST start = offset + xfered;
1626 ULONGEST remaining = len - xfered;
1629 /* If an attempt was made to read a random memory address,
1630 it's likely that the very first byte is not accessible.
1631 Try reading the first byte, to avoid doing log N tries
1633 xfer = target_read_partial (ops, object, annex,
1634 (gdb_byte *) buf + xfered, start, 1);
1643 xfer = target_read_partial (ops, object, annex,
1644 (gdb_byte *) buf + xfered,
1654 /* We have successfully read the first half. So, the
1655 error must be in the second half. Adjust start and
1656 remaining to point at the second half. */
1673 /* An alternative to target_write with progress callbacks. */
1676 target_write_with_progress (struct target_ops *ops,
1677 enum target_object object,
1678 const char *annex, const gdb_byte *buf,
1679 ULONGEST offset, LONGEST len,
1680 void (*progress) (ULONGEST, void *), void *baton)
1684 /* Give the progress callback a chance to set up. */
1686 (*progress) (0, baton);
1688 while (xfered < len)
1690 LONGEST xfer = target_write_partial (ops, object, annex,
1691 (gdb_byte *) buf + xfered,
1692 offset + xfered, len - xfered);
1700 (*progress) (xfer, baton);
1709 target_write (struct target_ops *ops,
1710 enum target_object object,
1711 const char *annex, const gdb_byte *buf,
1712 ULONGEST offset, LONGEST len)
1714 return target_write_with_progress (ops, object, annex, buf, offset, len,
1718 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1719 the size of the transferred data. PADDING additional bytes are
1720 available in *BUF_P. This is a helper function for
1721 target_read_alloc; see the declaration of that function for more
1725 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1726 const char *annex, gdb_byte **buf_p, int padding)
1728 size_t buf_alloc, buf_pos;
1732 /* This function does not have a length parameter; it reads the
1733 entire OBJECT). Also, it doesn't support objects fetched partly
1734 from one target and partly from another (in a different stratum,
1735 e.g. a core file and an executable). Both reasons make it
1736 unsuitable for reading memory. */
1737 gdb_assert (object != TARGET_OBJECT_MEMORY);
1739 /* Start by reading up to 4K at a time. The target will throttle
1740 this number down if necessary. */
1742 buf = xmalloc (buf_alloc);
1746 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1747 buf_pos, buf_alloc - buf_pos - padding);
1750 /* An error occurred. */
1756 /* Read all there was. */
1766 /* If the buffer is filling up, expand it. */
1767 if (buf_alloc < buf_pos * 2)
1770 buf = xrealloc (buf, buf_alloc);
1777 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1778 the size of the transferred data. See the declaration in "target.h"
1779 function for more information about the return value. */
1782 target_read_alloc (struct target_ops *ops, enum target_object object,
1783 const char *annex, gdb_byte **buf_p)
1785 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1788 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1789 returned as a string, allocated using xmalloc. If an error occurs
1790 or the transfer is unsupported, NULL is returned. Empty objects
1791 are returned as allocated but empty strings. A warning is issued
1792 if the result contains any embedded NUL bytes. */
1795 target_read_stralloc (struct target_ops *ops, enum target_object object,
1799 LONGEST transferred;
1801 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1803 if (transferred < 0)
1806 if (transferred == 0)
1807 return xstrdup ("");
1809 buffer[transferred] = 0;
1810 if (strlen (buffer) < transferred)
1811 warning (_("target object %d, annex %s, "
1812 "contained unexpected null characters"),
1813 (int) object, annex ? annex : "(none)");
1815 return (char *) buffer;
1818 /* Memory transfer methods. */
1821 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1824 /* This method is used to read from an alternate, non-current
1825 target. This read must bypass the overlay support (as symbols
1826 don't match this target), and GDB's internal cache (wrong cache
1827 for this target). */
1828 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
1830 memory_error (EIO, addr);
1834 get_target_memory_unsigned (struct target_ops *ops,
1835 CORE_ADDR addr, int len)
1837 gdb_byte buf[sizeof (ULONGEST)];
1839 gdb_assert (len <= sizeof (buf));
1840 get_target_memory (ops, addr, buf, len);
1841 return extract_unsigned_integer (buf, len);
1845 target_info (char *args, int from_tty)
1847 struct target_ops *t;
1848 int has_all_mem = 0;
1850 if (symfile_objfile != NULL)
1851 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
1853 for (t = target_stack; t != NULL; t = t->beneath)
1855 if (!(*t->to_has_memory) (t))
1858 if ((int) (t->to_stratum) <= (int) dummy_stratum)
1861 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1862 printf_unfiltered ("%s:\n", t->to_longname);
1863 (t->to_files_info) (t);
1864 has_all_mem = (*t->to_has_all_memory) (t);
1868 /* This function is called before any new inferior is created, e.g.
1869 by running a program, attaching, or connecting to a target.
1870 It cleans up any state from previous invocations which might
1871 change between runs. This is a subset of what target_preopen
1872 resets (things which might change between targets). */
1875 target_pre_inferior (int from_tty)
1877 /* Clear out solib state. Otherwise the solib state of the previous
1878 inferior might have survived and is entirely wrong for the new
1879 target. This has been observed on GNU/Linux using glibc 2.3. How
1891 Cannot access memory at address 0xdeadbeef
1894 /* In some OSs, the shared library list is the same/global/shared
1895 across inferiors. If code is shared between processes, so are
1896 memory regions and features. */
1897 if (!gdbarch_has_global_solist (target_gdbarch))
1899 no_shared_libraries (NULL, from_tty);
1901 invalidate_target_mem_regions ();
1903 target_clear_description ();
1907 /* This is to be called by the open routine before it does
1911 target_preopen (int from_tty)
1915 if (target_has_execution)
1918 || query (_("A program is being debugged already. Kill it? ")))
1921 error (_("Program not killed."));
1924 /* Calling target_kill may remove the target from the stack. But if
1925 it doesn't (which seems like a win for UDI), remove it now. */
1926 /* Leave the exec target, though. The user may be switching from a
1927 live process to a core of the same program. */
1928 pop_all_targets_above (file_stratum, 0);
1930 target_pre_inferior (from_tty);
1933 /* Detach a target after doing deferred register stores. */
1936 target_detach (char *args, int from_tty)
1938 struct target_ops* t;
1940 if (gdbarch_has_global_breakpoints (target_gdbarch))
1941 /* Don't remove global breakpoints here. They're removed on
1942 disconnection from the target. */
1945 /* If we're in breakpoints-always-inserted mode, have to remove
1946 them before detaching. */
1947 remove_breakpoints ();
1949 for (t = current_target.beneath; t != NULL; t = t->beneath)
1951 if (t->to_detach != NULL)
1953 t->to_detach (t, args, from_tty);
1955 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
1961 internal_error (__FILE__, __LINE__, "could not find a target to detach");
1965 target_disconnect (char *args, int from_tty)
1967 struct target_ops *t;
1969 /* If we're in breakpoints-always-inserted mode or if breakpoints
1970 are global across processes, we have to remove them before
1972 remove_breakpoints ();
1974 for (t = current_target.beneath; t != NULL; t = t->beneath)
1975 if (t->to_disconnect != NULL)
1978 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
1980 t->to_disconnect (t, args, from_tty);
1988 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
1990 struct target_ops *t;
1992 for (t = current_target.beneath; t != NULL; t = t->beneath)
1994 if (t->to_wait != NULL)
1996 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2000 char *status_string;
2002 status_string = target_waitstatus_to_string (status);
2003 fprintf_unfiltered (gdb_stdlog,
2004 "target_wait (%d, status) = %d, %s\n",
2005 PIDGET (ptid), PIDGET (retval),
2007 xfree (status_string);
2018 target_pid_to_str (ptid_t ptid)
2020 struct target_ops *t;
2022 for (t = current_target.beneath; t != NULL; t = t->beneath)
2024 if (t->to_pid_to_str != NULL)
2025 return (*t->to_pid_to_str) (t, ptid);
2028 return normal_pid_to_str (ptid);
2032 target_resume (ptid_t ptid, int step, enum target_signal signal)
2034 struct target_ops *t;
2036 dcache_invalidate (target_dcache);
2038 for (t = current_target.beneath; t != NULL; t = t->beneath)
2040 if (t->to_resume != NULL)
2042 t->to_resume (t, ptid, step, signal);
2044 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2046 step ? "step" : "continue",
2047 target_signal_to_name (signal));
2049 set_executing (ptid, 1);
2050 set_running (ptid, 1);
2051 clear_inline_frame_state (ptid);
2058 /* Look through the list of possible targets for a target that can
2062 target_follow_fork (int follow_child)
2064 struct target_ops *t;
2066 for (t = current_target.beneath; t != NULL; t = t->beneath)
2068 if (t->to_follow_fork != NULL)
2070 int retval = t->to_follow_fork (t, follow_child);
2072 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2073 follow_child, retval);
2078 /* Some target returned a fork event, but did not know how to follow it. */
2079 internal_error (__FILE__, __LINE__,
2080 "could not find a target to follow fork");
2084 target_mourn_inferior (void)
2086 struct target_ops *t;
2087 for (t = current_target.beneath; t != NULL; t = t->beneath)
2089 if (t->to_mourn_inferior != NULL)
2091 t->to_mourn_inferior (t);
2093 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2095 /* We no longer need to keep handles on any of the object files.
2096 Make sure to release them to avoid unnecessarily locking any
2097 of them while we're not actually debugging. */
2098 bfd_cache_close_all ();
2104 internal_error (__FILE__, __LINE__,
2105 "could not find a target to follow mourn inferiour");
2108 /* Look for a target which can describe architectural features, starting
2109 from TARGET. If we find one, return its description. */
2111 const struct target_desc *
2112 target_read_description (struct target_ops *target)
2114 struct target_ops *t;
2116 for (t = target; t != NULL; t = t->beneath)
2117 if (t->to_read_description != NULL)
2119 const struct target_desc *tdesc;
2121 tdesc = t->to_read_description (t);
2129 /* The default implementation of to_search_memory.
2130 This implements a basic search of memory, reading target memory and
2131 performing the search here (as opposed to performing the search in on the
2132 target side with, for example, gdbserver). */
2135 simple_search_memory (struct target_ops *ops,
2136 CORE_ADDR start_addr, ULONGEST search_space_len,
2137 const gdb_byte *pattern, ULONGEST pattern_len,
2138 CORE_ADDR *found_addrp)
2140 /* NOTE: also defined in find.c testcase. */
2141 #define SEARCH_CHUNK_SIZE 16000
2142 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2143 /* Buffer to hold memory contents for searching. */
2144 gdb_byte *search_buf;
2145 unsigned search_buf_size;
2146 struct cleanup *old_cleanups;
2148 search_buf_size = chunk_size + pattern_len - 1;
2150 /* No point in trying to allocate a buffer larger than the search space. */
2151 if (search_space_len < search_buf_size)
2152 search_buf_size = search_space_len;
2154 search_buf = malloc (search_buf_size);
2155 if (search_buf == NULL)
2156 error (_("Unable to allocate memory to perform the search."));
2157 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2159 /* Prime the search buffer. */
2161 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2162 search_buf, start_addr, search_buf_size) != search_buf_size)
2164 warning (_("Unable to access target memory at %s, halting search."),
2165 hex_string (start_addr));
2166 do_cleanups (old_cleanups);
2170 /* Perform the search.
2172 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2173 When we've scanned N bytes we copy the trailing bytes to the start and
2174 read in another N bytes. */
2176 while (search_space_len >= pattern_len)
2178 gdb_byte *found_ptr;
2179 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2181 found_ptr = memmem (search_buf, nr_search_bytes,
2182 pattern, pattern_len);
2184 if (found_ptr != NULL)
2186 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2187 *found_addrp = found_addr;
2188 do_cleanups (old_cleanups);
2192 /* Not found in this chunk, skip to next chunk. */
2194 /* Don't let search_space_len wrap here, it's unsigned. */
2195 if (search_space_len >= chunk_size)
2196 search_space_len -= chunk_size;
2198 search_space_len = 0;
2200 if (search_space_len >= pattern_len)
2202 unsigned keep_len = search_buf_size - chunk_size;
2203 CORE_ADDR read_addr = start_addr + keep_len;
2206 /* Copy the trailing part of the previous iteration to the front
2207 of the buffer for the next iteration. */
2208 gdb_assert (keep_len == pattern_len - 1);
2209 memcpy (search_buf, search_buf + chunk_size, keep_len);
2211 nr_to_read = min (search_space_len - keep_len, chunk_size);
2213 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2214 search_buf + keep_len, read_addr,
2215 nr_to_read) != nr_to_read)
2217 warning (_("Unable to access target memory at %s, halting search."),
2218 hex_string (read_addr));
2219 do_cleanups (old_cleanups);
2223 start_addr += chunk_size;
2229 do_cleanups (old_cleanups);
2233 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2234 sequence of bytes in PATTERN with length PATTERN_LEN.
2236 The result is 1 if found, 0 if not found, and -1 if there was an error
2237 requiring halting of the search (e.g. memory read error).
2238 If the pattern is found the address is recorded in FOUND_ADDRP. */
2241 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2242 const gdb_byte *pattern, ULONGEST pattern_len,
2243 CORE_ADDR *found_addrp)
2245 struct target_ops *t;
2248 /* We don't use INHERIT to set current_target.to_search_memory,
2249 so we have to scan the target stack and handle targetdebug
2253 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2254 hex_string (start_addr));
2256 for (t = current_target.beneath; t != NULL; t = t->beneath)
2257 if (t->to_search_memory != NULL)
2262 found = t->to_search_memory (t, start_addr, search_space_len,
2263 pattern, pattern_len, found_addrp);
2267 /* If a special version of to_search_memory isn't available, use the
2269 found = simple_search_memory (current_target.beneath,
2270 start_addr, search_space_len,
2271 pattern, pattern_len, found_addrp);
2275 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2280 /* Look through the currently pushed targets. If none of them will
2281 be able to restart the currently running process, issue an error
2285 target_require_runnable (void)
2287 struct target_ops *t;
2289 for (t = target_stack; t != NULL; t = t->beneath)
2291 /* If this target knows how to create a new program, then
2292 assume we will still be able to after killing the current
2293 one. Either killing and mourning will not pop T, or else
2294 find_default_run_target will find it again. */
2295 if (t->to_create_inferior != NULL)
2298 /* Do not worry about thread_stratum targets that can not
2299 create inferiors. Assume they will be pushed again if
2300 necessary, and continue to the process_stratum. */
2301 if (t->to_stratum == thread_stratum)
2305 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2309 /* This function is only called if the target is running. In that
2310 case there should have been a process_stratum target and it
2311 should either know how to create inferiors, or not... */
2312 internal_error (__FILE__, __LINE__, "No targets found");
2315 /* Look through the list of possible targets for a target that can
2316 execute a run or attach command without any other data. This is
2317 used to locate the default process stratum.
2319 If DO_MESG is not NULL, the result is always valid (error() is
2320 called for errors); else, return NULL on error. */
2322 static struct target_ops *
2323 find_default_run_target (char *do_mesg)
2325 struct target_ops **t;
2326 struct target_ops *runable = NULL;
2331 for (t = target_structs; t < target_structs + target_struct_size;
2334 if ((*t)->to_can_run && target_can_run (*t))
2344 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2353 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2355 struct target_ops *t;
2357 t = find_default_run_target ("attach");
2358 (t->to_attach) (t, args, from_tty);
2363 find_default_create_inferior (struct target_ops *ops,
2364 char *exec_file, char *allargs, char **env,
2367 struct target_ops *t;
2369 t = find_default_run_target ("run");
2370 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2375 find_default_can_async_p (void)
2377 struct target_ops *t;
2379 /* This may be called before the target is pushed on the stack;
2380 look for the default process stratum. If there's none, gdb isn't
2381 configured with a native debugger, and target remote isn't
2383 t = find_default_run_target (NULL);
2384 if (t && t->to_can_async_p)
2385 return (t->to_can_async_p) ();
2390 find_default_is_async_p (void)
2392 struct target_ops *t;
2394 /* This may be called before the target is pushed on the stack;
2395 look for the default process stratum. If there's none, gdb isn't
2396 configured with a native debugger, and target remote isn't
2398 t = find_default_run_target (NULL);
2399 if (t && t->to_is_async_p)
2400 return (t->to_is_async_p) ();
2405 find_default_supports_non_stop (void)
2407 struct target_ops *t;
2409 t = find_default_run_target (NULL);
2410 if (t && t->to_supports_non_stop)
2411 return (t->to_supports_non_stop) ();
2416 target_supports_non_stop (void)
2418 struct target_ops *t;
2419 for (t = ¤t_target; t != NULL; t = t->beneath)
2420 if (t->to_supports_non_stop)
2421 return t->to_supports_non_stop ();
2428 target_get_osdata (const char *type)
2431 struct target_ops *t;
2433 /* If we're already connected to something that can get us OS
2434 related data, use it. Otherwise, try using the native
2436 if (current_target.to_stratum >= process_stratum)
2437 t = current_target.beneath;
2439 t = find_default_run_target ("get OS data");
2444 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
2448 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2450 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
2454 default_watchpoint_addr_within_range (struct target_ops *target,
2456 CORE_ADDR start, int length)
2458 return addr >= start && addr < start + length;
2461 static struct gdbarch *
2462 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2464 return target_gdbarch;
2480 return_minus_one (void)
2485 /* Find a single runnable target in the stack and return it. If for
2486 some reason there is more than one, return NULL. */
2489 find_run_target (void)
2491 struct target_ops **t;
2492 struct target_ops *runable = NULL;
2497 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2499 if ((*t)->to_can_run && target_can_run (*t))
2506 return (count == 1 ? runable : NULL);
2509 /* Find a single core_stratum target in the list of targets and return it.
2510 If for some reason there is more than one, return NULL. */
2513 find_core_target (void)
2515 struct target_ops **t;
2516 struct target_ops *runable = NULL;
2521 for (t = target_structs; t < target_structs + target_struct_size;
2524 if ((*t)->to_stratum == core_stratum)
2531 return (count == 1 ? runable : NULL);
2535 * Find the next target down the stack from the specified target.
2539 find_target_beneath (struct target_ops *t)
2545 /* The inferior process has died. Long live the inferior! */
2548 generic_mourn_inferior (void)
2552 ptid = inferior_ptid;
2553 inferior_ptid = null_ptid;
2555 if (!ptid_equal (ptid, null_ptid))
2557 int pid = ptid_get_pid (ptid);
2558 delete_inferior (pid);
2561 breakpoint_init_inferior (inf_exited);
2562 registers_changed ();
2564 reopen_exec_file ();
2565 reinit_frame_cache ();
2567 if (deprecated_detach_hook)
2568 deprecated_detach_hook ();
2571 /* Helper function for child_wait and the derivatives of child_wait.
2572 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2573 translation of that in OURSTATUS. */
2575 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2577 if (WIFEXITED (hoststatus))
2579 ourstatus->kind = TARGET_WAITKIND_EXITED;
2580 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2582 else if (!WIFSTOPPED (hoststatus))
2584 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2585 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2589 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2590 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2594 /* Convert a normal process ID to a string. Returns the string in a
2598 normal_pid_to_str (ptid_t ptid)
2600 static char buf[32];
2602 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2607 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2609 return normal_pid_to_str (ptid);
2612 /* Error-catcher for target_find_memory_regions */
2613 static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2615 error (_("No target."));
2619 /* Error-catcher for target_make_corefile_notes */
2620 static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2622 error (_("No target."));
2626 /* Set up the handful of non-empty slots needed by the dummy target
2630 init_dummy_target (void)
2632 dummy_target.to_shortname = "None";
2633 dummy_target.to_longname = "None";
2634 dummy_target.to_doc = "";
2635 dummy_target.to_attach = find_default_attach;
2636 dummy_target.to_detach =
2637 (void (*)(struct target_ops *, char *, int))target_ignore;
2638 dummy_target.to_create_inferior = find_default_create_inferior;
2639 dummy_target.to_can_async_p = find_default_can_async_p;
2640 dummy_target.to_is_async_p = find_default_is_async_p;
2641 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
2642 dummy_target.to_pid_to_str = dummy_pid_to_str;
2643 dummy_target.to_stratum = dummy_stratum;
2644 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2645 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2646 dummy_target.to_xfer_partial = default_xfer_partial;
2647 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2648 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2649 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2650 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2651 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
2652 dummy_target.to_magic = OPS_MAGIC;
2656 debug_to_open (char *args, int from_tty)
2658 debug_target.to_open (args, from_tty);
2660 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2664 target_close (struct target_ops *targ, int quitting)
2666 if (targ->to_xclose != NULL)
2667 targ->to_xclose (targ, quitting);
2668 else if (targ->to_close != NULL)
2669 targ->to_close (quitting);
2672 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2676 target_attach (char *args, int from_tty)
2678 struct target_ops *t;
2679 for (t = current_target.beneath; t != NULL; t = t->beneath)
2681 if (t->to_attach != NULL)
2683 t->to_attach (t, args, from_tty);
2685 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2691 internal_error (__FILE__, __LINE__,
2692 "could not find a target to attach");
2696 target_thread_alive (ptid_t ptid)
2698 struct target_ops *t;
2699 for (t = current_target.beneath; t != NULL; t = t->beneath)
2701 if (t->to_thread_alive != NULL)
2705 retval = t->to_thread_alive (t, ptid);
2707 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2708 PIDGET (ptid), retval);
2718 target_find_new_threads (void)
2720 struct target_ops *t;
2721 for (t = current_target.beneath; t != NULL; t = t->beneath)
2723 if (t->to_find_new_threads != NULL)
2725 t->to_find_new_threads (t);
2727 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2735 debug_to_post_attach (int pid)
2737 debug_target.to_post_attach (pid);
2739 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2742 /* Return a pretty printed form of target_waitstatus.
2743 Space for the result is malloc'd, caller must free. */
2746 target_waitstatus_to_string (const struct target_waitstatus *ws)
2748 const char *kind_str = "status->kind = ";
2752 case TARGET_WAITKIND_EXITED:
2753 return xstrprintf ("%sexited, status = %d",
2754 kind_str, ws->value.integer);
2755 case TARGET_WAITKIND_STOPPED:
2756 return xstrprintf ("%sstopped, signal = %s",
2757 kind_str, target_signal_to_name (ws->value.sig));
2758 case TARGET_WAITKIND_SIGNALLED:
2759 return xstrprintf ("%ssignalled, signal = %s",
2760 kind_str, target_signal_to_name (ws->value.sig));
2761 case TARGET_WAITKIND_LOADED:
2762 return xstrprintf ("%sloaded", kind_str);
2763 case TARGET_WAITKIND_FORKED:
2764 return xstrprintf ("%sforked", kind_str);
2765 case TARGET_WAITKIND_VFORKED:
2766 return xstrprintf ("%svforked", kind_str);
2767 case TARGET_WAITKIND_EXECD:
2768 return xstrprintf ("%sexecd", kind_str);
2769 case TARGET_WAITKIND_SYSCALL_ENTRY:
2770 return xstrprintf ("%ssyscall-entry", kind_str);
2771 case TARGET_WAITKIND_SYSCALL_RETURN:
2772 return xstrprintf ("%ssyscall-return", kind_str);
2773 case TARGET_WAITKIND_SPURIOUS:
2774 return xstrprintf ("%sspurious", kind_str);
2775 case TARGET_WAITKIND_IGNORE:
2776 return xstrprintf ("%signore", kind_str);
2777 case TARGET_WAITKIND_NO_HISTORY:
2778 return xstrprintf ("%sno-history", kind_str);
2780 return xstrprintf ("%sunknown???", kind_str);
2785 debug_print_register (const char * func,
2786 struct regcache *regcache, int regno)
2788 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2789 fprintf_unfiltered (gdb_stdlog, "%s ", func);
2790 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
2791 && gdbarch_register_name (gdbarch, regno) != NULL
2792 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2793 fprintf_unfiltered (gdb_stdlog, "(%s)",
2794 gdbarch_register_name (gdbarch, regno));
2796 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
2797 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
2799 int i, size = register_size (gdbarch, regno);
2800 unsigned char buf[MAX_REGISTER_SIZE];
2801 regcache_raw_collect (regcache, regno, buf);
2802 fprintf_unfiltered (gdb_stdlog, " = ");
2803 for (i = 0; i < size; i++)
2805 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2807 if (size <= sizeof (LONGEST))
2809 ULONGEST val = extract_unsigned_integer (buf, size);
2810 fprintf_unfiltered (gdb_stdlog, " %s %s",
2811 core_addr_to_string_nz (val), plongest (val));
2814 fprintf_unfiltered (gdb_stdlog, "\n");
2818 target_fetch_registers (struct regcache *regcache, int regno)
2820 struct target_ops *t;
2821 for (t = current_target.beneath; t != NULL; t = t->beneath)
2823 if (t->to_fetch_registers != NULL)
2825 t->to_fetch_registers (t, regcache, regno);
2827 debug_print_register ("target_fetch_registers", regcache, regno);
2834 target_store_registers (struct regcache *regcache, int regno)
2837 struct target_ops *t;
2838 for (t = current_target.beneath; t != NULL; t = t->beneath)
2840 if (t->to_store_registers != NULL)
2842 t->to_store_registers (t, regcache, regno);
2845 debug_print_register ("target_store_registers", regcache, regno);
2855 debug_to_prepare_to_store (struct regcache *regcache)
2857 debug_target.to_prepare_to_store (regcache);
2859 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
2863 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
2864 int write, struct mem_attrib *attrib,
2865 struct target_ops *target)
2869 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2872 fprintf_unfiltered (gdb_stdlog,
2873 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2874 paddress (target_gdbarch, memaddr), len,
2875 write ? "write" : "read", retval);
2881 fputs_unfiltered (", bytes =", gdb_stdlog);
2882 for (i = 0; i < retval; i++)
2884 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
2886 if (targetdebug < 2 && i > 0)
2888 fprintf_unfiltered (gdb_stdlog, " ...");
2891 fprintf_unfiltered (gdb_stdlog, "\n");
2894 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
2898 fputc_unfiltered ('\n', gdb_stdlog);
2904 debug_to_files_info (struct target_ops *target)
2906 debug_target.to_files_info (target);
2908 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
2912 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
2913 struct bp_target_info *bp_tgt)
2917 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
2919 fprintf_unfiltered (gdb_stdlog,
2920 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2921 (unsigned long) bp_tgt->placed_address,
2922 (unsigned long) retval);
2927 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
2928 struct bp_target_info *bp_tgt)
2932 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
2934 fprintf_unfiltered (gdb_stdlog,
2935 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2936 (unsigned long) bp_tgt->placed_address,
2937 (unsigned long) retval);
2942 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2946 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2948 fprintf_unfiltered (gdb_stdlog,
2949 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2950 (unsigned long) type,
2951 (unsigned long) cnt,
2952 (unsigned long) from_tty,
2953 (unsigned long) retval);
2958 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2962 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2964 fprintf_unfiltered (gdb_stdlog,
2965 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
2966 (unsigned long) addr,
2967 (unsigned long) len,
2968 (unsigned long) retval);
2973 debug_to_stopped_by_watchpoint (void)
2977 retval = debug_target.to_stopped_by_watchpoint ();
2979 fprintf_unfiltered (gdb_stdlog,
2980 "target_stopped_by_watchpoint () = %ld\n",
2981 (unsigned long) retval);
2986 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
2990 retval = debug_target.to_stopped_data_address (target, addr);
2992 fprintf_unfiltered (gdb_stdlog,
2993 "target_stopped_data_address ([0x%lx]) = %ld\n",
2994 (unsigned long)*addr,
2995 (unsigned long)retval);
3000 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3002 CORE_ADDR start, int length)
3006 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3009 fprintf_filtered (gdb_stdlog,
3010 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3011 (unsigned long) addr, (unsigned long) start, length,
3017 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3018 struct bp_target_info *bp_tgt)
3022 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3024 fprintf_unfiltered (gdb_stdlog,
3025 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3026 (unsigned long) bp_tgt->placed_address,
3027 (unsigned long) retval);
3032 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3033 struct bp_target_info *bp_tgt)
3037 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3039 fprintf_unfiltered (gdb_stdlog,
3040 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3041 (unsigned long) bp_tgt->placed_address,
3042 (unsigned long) retval);
3047 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3051 retval = debug_target.to_insert_watchpoint (addr, len, type);
3053 fprintf_unfiltered (gdb_stdlog,
3054 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3055 (unsigned long) addr, len, type, (unsigned long) retval);
3060 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3064 retval = debug_target.to_remove_watchpoint (addr, len, type);
3066 fprintf_unfiltered (gdb_stdlog,
3067 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3068 (unsigned long) addr, len, type, (unsigned long) retval);
3073 debug_to_terminal_init (void)
3075 debug_target.to_terminal_init ();
3077 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3081 debug_to_terminal_inferior (void)
3083 debug_target.to_terminal_inferior ();
3085 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3089 debug_to_terminal_ours_for_output (void)
3091 debug_target.to_terminal_ours_for_output ();
3093 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3097 debug_to_terminal_ours (void)
3099 debug_target.to_terminal_ours ();
3101 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3105 debug_to_terminal_save_ours (void)
3107 debug_target.to_terminal_save_ours ();
3109 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3113 debug_to_terminal_info (char *arg, int from_tty)
3115 debug_target.to_terminal_info (arg, from_tty);
3117 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3122 debug_to_load (char *args, int from_tty)
3124 debug_target.to_load (args, from_tty);
3126 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3130 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
3134 retval = debug_target.to_lookup_symbol (name, addrp);
3136 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
3142 debug_to_post_startup_inferior (ptid_t ptid)
3144 debug_target.to_post_startup_inferior (ptid);
3146 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3151 debug_to_acknowledge_created_inferior (int pid)
3153 debug_target.to_acknowledge_created_inferior (pid);
3155 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
3160 debug_to_insert_fork_catchpoint (int pid)
3162 debug_target.to_insert_fork_catchpoint (pid);
3164 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3169 debug_to_remove_fork_catchpoint (int pid)
3173 retval = debug_target.to_remove_fork_catchpoint (pid);
3175 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3182 debug_to_insert_vfork_catchpoint (int pid)
3184 debug_target.to_insert_vfork_catchpoint (pid);
3186 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3191 debug_to_remove_vfork_catchpoint (int pid)
3195 retval = debug_target.to_remove_vfork_catchpoint (pid);
3197 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
3204 debug_to_insert_exec_catchpoint (int pid)
3206 debug_target.to_insert_exec_catchpoint (pid);
3208 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3213 debug_to_remove_exec_catchpoint (int pid)
3217 retval = debug_target.to_remove_exec_catchpoint (pid);
3219 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
3226 debug_to_has_exited (int pid, int wait_status, int *exit_status)
3230 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3232 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
3233 pid, wait_status, *exit_status, has_exited);
3239 debug_to_can_run (void)
3243 retval = debug_target.to_can_run ();
3245 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
3251 debug_to_notice_signals (ptid_t ptid)
3253 debug_target.to_notice_signals (ptid);
3255 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3259 static struct gdbarch *
3260 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3262 struct gdbarch *retval;
3264 retval = debug_target.to_thread_architecture (ops, ptid);
3266 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %p [%s]\n",
3267 target_pid_to_str (ptid), retval,
3268 gdbarch_bfd_arch_info (retval)->printable_name);
3273 debug_to_stop (ptid_t ptid)
3275 debug_target.to_stop (ptid);
3277 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3278 target_pid_to_str (ptid));
3282 debug_to_rcmd (char *command,
3283 struct ui_file *outbuf)
3285 debug_target.to_rcmd (command, outbuf);
3286 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3290 debug_to_pid_to_exec_file (int pid)
3294 exec_file = debug_target.to_pid_to_exec_file (pid);
3296 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
3303 setup_target_debug (void)
3305 memcpy (&debug_target, ¤t_target, sizeof debug_target);
3307 current_target.to_open = debug_to_open;
3308 current_target.to_post_attach = debug_to_post_attach;
3309 current_target.to_prepare_to_store = debug_to_prepare_to_store;
3310 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
3311 current_target.to_files_info = debug_to_files_info;
3312 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3313 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
3314 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3315 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3316 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3317 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3318 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3319 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3320 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3321 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
3322 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
3323 current_target.to_terminal_init = debug_to_terminal_init;
3324 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3325 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3326 current_target.to_terminal_ours = debug_to_terminal_ours;
3327 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
3328 current_target.to_terminal_info = debug_to_terminal_info;
3329 current_target.to_load = debug_to_load;
3330 current_target.to_lookup_symbol = debug_to_lookup_symbol;
3331 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3332 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
3333 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3334 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3335 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3336 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
3337 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3338 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
3339 current_target.to_has_exited = debug_to_has_exited;
3340 current_target.to_can_run = debug_to_can_run;
3341 current_target.to_notice_signals = debug_to_notice_signals;
3342 current_target.to_stop = debug_to_stop;
3343 current_target.to_rcmd = debug_to_rcmd;
3344 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
3345 current_target.to_thread_architecture = debug_to_thread_architecture;
3349 static char targ_desc[] =
3350 "Names of targets and files being debugged.\n\
3351 Shows the entire stack of targets currently in use (including the exec-file,\n\
3352 core-file, and process, if any), as well as the symbol file name.";
3355 do_monitor_command (char *cmd,
3358 if ((current_target.to_rcmd
3359 == (void (*) (char *, struct ui_file *)) tcomplain)
3360 || (current_target.to_rcmd == debug_to_rcmd
3361 && (debug_target.to_rcmd
3362 == (void (*) (char *, struct ui_file *)) tcomplain)))
3363 error (_("\"monitor\" command not supported by this target."));
3364 target_rcmd (cmd, gdb_stdtarg);
3367 /* Print the name of each layers of our target stack. */
3370 maintenance_print_target_stack (char *cmd, int from_tty)
3372 struct target_ops *t;
3374 printf_filtered (_("The current target stack is:\n"));
3376 for (t = target_stack; t != NULL; t = t->beneath)
3378 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3382 /* Controls if async mode is permitted. */
3383 int target_async_permitted = 0;
3385 /* The set command writes to this variable. If the inferior is
3386 executing, linux_nat_async_permitted is *not* updated. */
3387 static int target_async_permitted_1 = 0;
3390 set_maintenance_target_async_permitted (char *args, int from_tty,
3391 struct cmd_list_element *c)
3393 if (have_live_inferiors ())
3395 target_async_permitted_1 = target_async_permitted;
3396 error (_("Cannot change this setting while the inferior is running."));
3399 target_async_permitted = target_async_permitted_1;
3403 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3404 struct cmd_list_element *c,
3407 fprintf_filtered (file, _("\
3408 Controlling the inferior in asynchronous mode is %s.\n"), value);
3412 initialize_targets (void)
3414 init_dummy_target ();
3415 push_target (&dummy_target);
3417 add_info ("target", target_info, targ_desc);
3418 add_info ("files", target_info, targ_desc);
3420 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3421 Set target debugging."), _("\
3422 Show target debugging."), _("\
3423 When non-zero, target debugging is enabled. Higher numbers are more\n\
3424 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3428 &setdebuglist, &showdebuglist);
3430 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
3431 &trust_readonly, _("\
3432 Set mode for reading from readonly sections."), _("\
3433 Show mode for reading from readonly sections."), _("\
3434 When this mode is on, memory reads from readonly sections (such as .text)\n\
3435 will be read from the object file instead of from the target. This will\n\
3436 result in significant performance improvement for remote targets."),
3438 show_trust_readonly,
3439 &setlist, &showlist);
3441 add_com ("monitor", class_obscure, do_monitor_command,
3442 _("Send a command to the remote monitor (remote targets only)."));
3444 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3445 _("Print the name of each layer of the internal target stack."),
3446 &maintenanceprintlist);
3448 add_setshow_boolean_cmd ("target-async", no_class,
3449 &target_async_permitted_1, _("\
3450 Set whether gdb controls the inferior in asynchronous mode."), _("\
3451 Show whether gdb controls the inferior in asynchronous mode."), _("\
3452 Tells gdb whether to control the inferior in asynchronous mode."),
3453 set_maintenance_target_async_permitted,
3454 show_maintenance_target_async_permitted,
3458 target_dcache = dcache_init ();