xref: /dflybsd-src/contrib/gdb-7/gdb/target.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Select target systems and architectures at runtime for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1990-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Support.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include <errno.h>
245796c8dcSSimon Schubert #include "gdb_string.h"
255796c8dcSSimon Schubert #include "target.h"
265796c8dcSSimon Schubert #include "gdbcmd.h"
275796c8dcSSimon Schubert #include "symtab.h"
285796c8dcSSimon Schubert #include "inferior.h"
295796c8dcSSimon Schubert #include "bfd.h"
305796c8dcSSimon Schubert #include "symfile.h"
315796c8dcSSimon Schubert #include "objfiles.h"
325796c8dcSSimon Schubert #include "dcache.h"
335796c8dcSSimon Schubert #include <signal.h>
345796c8dcSSimon Schubert #include "regcache.h"
355796c8dcSSimon Schubert #include "gdb_assert.h"
365796c8dcSSimon Schubert #include "gdbcore.h"
375796c8dcSSimon Schubert #include "exceptions.h"
385796c8dcSSimon Schubert #include "target-descriptions.h"
395796c8dcSSimon Schubert #include "gdbthread.h"
405796c8dcSSimon Schubert #include "solib.h"
415796c8dcSSimon Schubert #include "exec.h"
425796c8dcSSimon Schubert #include "inline-frame.h"
43cf7f2e2dSJohn Marino #include "tracepoint.h"
44*ef5ccd6cSJohn Marino #include "gdb/fileio.h"
45*ef5ccd6cSJohn Marino #include "agent.h"
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static void target_info (char *, int);
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert static void default_terminal_info (char *, int);
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert static int default_watchpoint_addr_within_range (struct target_ops *,
525796c8dcSSimon Schubert 						 CORE_ADDR, CORE_ADDR, int);
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
555796c8dcSSimon Schubert 
56cf7f2e2dSJohn Marino static void tcomplain (void) ATTRIBUTE_NORETURN;
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert static int return_zero (void);
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert static int return_one (void);
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert static int return_minus_one (void);
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert void target_ignore (void);
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert static void target_command (char *, int);
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert static struct target_ops *find_default_run_target (char *);
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert static LONGEST default_xfer_partial (struct target_ops *ops,
735796c8dcSSimon Schubert 				     enum target_object object,
745796c8dcSSimon Schubert 				     const char *annex, gdb_byte *readbuf,
755796c8dcSSimon Schubert 				     const gdb_byte *writebuf,
765796c8dcSSimon Schubert 				     ULONGEST offset, LONGEST len);
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert static LONGEST current_xfer_partial (struct target_ops *ops,
795796c8dcSSimon Schubert 				     enum target_object object,
805796c8dcSSimon Schubert 				     const char *annex, gdb_byte *readbuf,
815796c8dcSSimon Schubert 				     const gdb_byte *writebuf,
825796c8dcSSimon Schubert 				     ULONGEST offset, LONGEST len);
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static LONGEST target_xfer_partial (struct target_ops *ops,
855796c8dcSSimon Schubert 				    enum target_object object,
865796c8dcSSimon Schubert 				    const char *annex,
875796c8dcSSimon Schubert 				    void *readbuf, const void *writebuf,
885796c8dcSSimon Schubert 				    ULONGEST offset, LONGEST len);
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static struct gdbarch *default_thread_architecture (struct target_ops *ops,
915796c8dcSSimon Schubert 						    ptid_t ptid);
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert static void init_dummy_target (void);
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert static struct target_ops debug_target;
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static void debug_to_open (char *, int);
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert static void debug_to_prepare_to_store (struct regcache *);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert static void debug_to_files_info (struct target_ops *);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert static int debug_to_insert_breakpoint (struct gdbarch *,
1045796c8dcSSimon Schubert 				       struct bp_target_info *);
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert static int debug_to_remove_breakpoint (struct gdbarch *,
1075796c8dcSSimon Schubert 				       struct bp_target_info *);
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static int debug_to_can_use_hw_breakpoint (int, int, int);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert static int debug_to_insert_hw_breakpoint (struct gdbarch *,
1125796c8dcSSimon Schubert 					  struct bp_target_info *);
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert static int debug_to_remove_hw_breakpoint (struct gdbarch *,
1155796c8dcSSimon Schubert 					  struct bp_target_info *);
1165796c8dcSSimon Schubert 
117cf7f2e2dSJohn Marino static int debug_to_insert_watchpoint (CORE_ADDR, int, int,
118cf7f2e2dSJohn Marino 				       struct expression *);
1195796c8dcSSimon Schubert 
120cf7f2e2dSJohn Marino static int debug_to_remove_watchpoint (CORE_ADDR, int, int,
121cf7f2e2dSJohn Marino 				       struct expression *);
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert static int debug_to_stopped_by_watchpoint (void);
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static int debug_to_watchpoint_addr_within_range (struct target_ops *,
1285796c8dcSSimon Schubert 						  CORE_ADDR, CORE_ADDR, int);
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
1315796c8dcSSimon Schubert 
132cf7f2e2dSJohn Marino static int debug_to_can_accel_watchpoint_condition (CORE_ADDR, int, int,
133cf7f2e2dSJohn Marino 						    struct expression *);
134cf7f2e2dSJohn Marino 
1355796c8dcSSimon Schubert static void debug_to_terminal_init (void);
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert static void debug_to_terminal_inferior (void);
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert static void debug_to_terminal_ours_for_output (void);
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert static void debug_to_terminal_save_ours (void);
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert static void debug_to_terminal_ours (void);
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert static void debug_to_terminal_info (char *, int);
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert static void debug_to_load (char *, int);
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert static int debug_to_can_run (void);
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert static void debug_to_stop (ptid_t);
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert /* Pointer to array of target architecture structures; the size of the
1545796c8dcSSimon Schubert    array; the current index into the array; the allocated size of the
1555796c8dcSSimon Schubert    array.  */
1565796c8dcSSimon Schubert struct target_ops **target_structs;
1575796c8dcSSimon Schubert unsigned target_struct_size;
1585796c8dcSSimon Schubert unsigned target_struct_index;
1595796c8dcSSimon Schubert unsigned target_struct_allocsize;
1605796c8dcSSimon Schubert #define	DEFAULT_ALLOCSIZE	10
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert /* The initial current target, so that there is always a semi-valid
1635796c8dcSSimon Schubert    current target.  */
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert static struct target_ops dummy_target;
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert /* Top of target stack.  */
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert static struct target_ops *target_stack;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* The target structure we are currently using to talk to a process
1725796c8dcSSimon Schubert    or file or whatever "inferior" we have.  */
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert struct target_ops current_target;
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert /* Command list for target.  */
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert static struct cmd_list_element *targetlist = NULL;
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* Nonzero if we should trust readonly sections from the
1815796c8dcSSimon Schubert    executable when reading memory.  */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert static int trust_readonly = 0;
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert /* Nonzero if we should show true memory content including
1865796c8dcSSimon Schubert    memory breakpoint inserted by gdb.  */
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert static int show_memory_breakpoints = 0;
1895796c8dcSSimon Schubert 
190cf7f2e2dSJohn Marino /* These globals control whether GDB attempts to perform these
191cf7f2e2dSJohn Marino    operations; they are useful for targets that need to prevent
192cf7f2e2dSJohn Marino    inadvertant disruption, such as in non-stop mode.  */
193cf7f2e2dSJohn Marino 
194cf7f2e2dSJohn Marino int may_write_registers = 1;
195cf7f2e2dSJohn Marino 
196cf7f2e2dSJohn Marino int may_write_memory = 1;
197cf7f2e2dSJohn Marino 
198cf7f2e2dSJohn Marino int may_insert_breakpoints = 1;
199cf7f2e2dSJohn Marino 
200cf7f2e2dSJohn Marino int may_insert_tracepoints = 1;
201cf7f2e2dSJohn Marino 
202cf7f2e2dSJohn Marino int may_insert_fast_tracepoints = 1;
203cf7f2e2dSJohn Marino 
204cf7f2e2dSJohn Marino int may_stop = 1;
205cf7f2e2dSJohn Marino 
2065796c8dcSSimon Schubert /* Non-zero if we want to see trace of target level stuff.  */
2075796c8dcSSimon Schubert 
208*ef5ccd6cSJohn Marino static unsigned int targetdebug = 0;
2095796c8dcSSimon Schubert static void
show_targetdebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2105796c8dcSSimon Schubert show_targetdebug (struct ui_file *file, int from_tty,
2115796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
2125796c8dcSSimon Schubert {
2135796c8dcSSimon Schubert   fprintf_filtered (file, _("Target debugging is %s.\n"), value);
2145796c8dcSSimon Schubert }
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert static void setup_target_debug (void);
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert /* The option sets this.  */
2195796c8dcSSimon Schubert static int stack_cache_enabled_p_1 = 1;
2205796c8dcSSimon Schubert /* And set_stack_cache_enabled_p updates this.
2215796c8dcSSimon Schubert    The reason for the separation is so that we don't flush the cache for
2225796c8dcSSimon Schubert    on->on transitions.  */
2235796c8dcSSimon Schubert static int stack_cache_enabled_p = 1;
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert /* This is called *after* the stack-cache has been set.
2265796c8dcSSimon Schubert    Flush the cache for off->on and on->off transitions.
2275796c8dcSSimon Schubert    There's no real need to flush the cache for on->off transitions,
2285796c8dcSSimon Schubert    except cleanliness.  */
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert static void
set_stack_cache_enabled_p(char * args,int from_tty,struct cmd_list_element * c)2315796c8dcSSimon Schubert set_stack_cache_enabled_p (char *args, int from_tty,
2325796c8dcSSimon Schubert 			   struct cmd_list_element *c)
2335796c8dcSSimon Schubert {
2345796c8dcSSimon Schubert   if (stack_cache_enabled_p != stack_cache_enabled_p_1)
2355796c8dcSSimon Schubert     target_dcache_invalidate ();
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert   stack_cache_enabled_p = stack_cache_enabled_p_1;
2385796c8dcSSimon Schubert }
2395796c8dcSSimon Schubert 
2405796c8dcSSimon Schubert static void
show_stack_cache_enabled_p(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2415796c8dcSSimon Schubert show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
2425796c8dcSSimon Schubert 			    struct cmd_list_element *c, const char *value)
2435796c8dcSSimon Schubert {
2445796c8dcSSimon Schubert   fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
2455796c8dcSSimon Schubert }
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert /* Cache of memory operations, to speed up remote access.  */
2485796c8dcSSimon Schubert static DCACHE *target_dcache;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert /* Invalidate the target dcache.  */
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert void
target_dcache_invalidate(void)2535796c8dcSSimon Schubert target_dcache_invalidate (void)
2545796c8dcSSimon Schubert {
2555796c8dcSSimon Schubert   dcache_invalidate (target_dcache);
2565796c8dcSSimon Schubert }
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert /* The user just typed 'target' without the name of a target.  */
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert static void
target_command(char * arg,int from_tty)2615796c8dcSSimon Schubert target_command (char *arg, int from_tty)
2625796c8dcSSimon Schubert {
2635796c8dcSSimon Schubert   fputs_filtered ("Argument required (target name).  Try `help target'\n",
2645796c8dcSSimon Schubert 		  gdb_stdout);
2655796c8dcSSimon Schubert }
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert /* Default target_has_* methods for process_stratum targets.  */
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert int
default_child_has_all_memory(struct target_ops * ops)2705796c8dcSSimon Schubert default_child_has_all_memory (struct target_ops *ops)
2715796c8dcSSimon Schubert {
2725796c8dcSSimon Schubert   /* If no inferior selected, then we can't read memory here.  */
2735796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
2745796c8dcSSimon Schubert     return 0;
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert   return 1;
2775796c8dcSSimon Schubert }
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert int
default_child_has_memory(struct target_ops * ops)2805796c8dcSSimon Schubert default_child_has_memory (struct target_ops *ops)
2815796c8dcSSimon Schubert {
2825796c8dcSSimon Schubert   /* If no inferior selected, then we can't read memory here.  */
2835796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
2845796c8dcSSimon Schubert     return 0;
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert   return 1;
2875796c8dcSSimon Schubert }
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert int
default_child_has_stack(struct target_ops * ops)2905796c8dcSSimon Schubert default_child_has_stack (struct target_ops *ops)
2915796c8dcSSimon Schubert {
2925796c8dcSSimon Schubert   /* If no inferior selected, there's no stack.  */
2935796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
2945796c8dcSSimon Schubert     return 0;
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   return 1;
2975796c8dcSSimon Schubert }
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert int
default_child_has_registers(struct target_ops * ops)3005796c8dcSSimon Schubert default_child_has_registers (struct target_ops *ops)
3015796c8dcSSimon Schubert {
3025796c8dcSSimon Schubert   /* Can't read registers from no inferior.  */
3035796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
3045796c8dcSSimon Schubert     return 0;
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert   return 1;
3075796c8dcSSimon Schubert }
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert int
default_child_has_execution(struct target_ops * ops,ptid_t the_ptid)310c50c785cSJohn Marino default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
3115796c8dcSSimon Schubert {
3125796c8dcSSimon Schubert   /* If there's no thread selected, then we can't make it run through
3135796c8dcSSimon Schubert      hoops.  */
314c50c785cSJohn Marino   if (ptid_equal (the_ptid, null_ptid))
3155796c8dcSSimon Schubert     return 0;
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert   return 1;
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert int
target_has_all_memory_1(void)3225796c8dcSSimon Schubert target_has_all_memory_1 (void)
3235796c8dcSSimon Schubert {
3245796c8dcSSimon Schubert   struct target_ops *t;
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
3275796c8dcSSimon Schubert     if (t->to_has_all_memory (t))
3285796c8dcSSimon Schubert       return 1;
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   return 0;
3315796c8dcSSimon Schubert }
3325796c8dcSSimon Schubert 
3335796c8dcSSimon Schubert int
target_has_memory_1(void)3345796c8dcSSimon Schubert target_has_memory_1 (void)
3355796c8dcSSimon Schubert {
3365796c8dcSSimon Schubert   struct target_ops *t;
3375796c8dcSSimon Schubert 
3385796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
3395796c8dcSSimon Schubert     if (t->to_has_memory (t))
3405796c8dcSSimon Schubert       return 1;
3415796c8dcSSimon Schubert 
3425796c8dcSSimon Schubert   return 0;
3435796c8dcSSimon Schubert }
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert int
target_has_stack_1(void)3465796c8dcSSimon Schubert target_has_stack_1 (void)
3475796c8dcSSimon Schubert {
3485796c8dcSSimon Schubert   struct target_ops *t;
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
3515796c8dcSSimon Schubert     if (t->to_has_stack (t))
3525796c8dcSSimon Schubert       return 1;
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert   return 0;
3555796c8dcSSimon Schubert }
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert int
target_has_registers_1(void)3585796c8dcSSimon Schubert target_has_registers_1 (void)
3595796c8dcSSimon Schubert {
3605796c8dcSSimon Schubert   struct target_ops *t;
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
3635796c8dcSSimon Schubert     if (t->to_has_registers (t))
3645796c8dcSSimon Schubert       return 1;
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert   return 0;
3675796c8dcSSimon Schubert }
3685796c8dcSSimon Schubert 
3695796c8dcSSimon Schubert int
target_has_execution_1(ptid_t the_ptid)370c50c785cSJohn Marino target_has_execution_1 (ptid_t the_ptid)
3715796c8dcSSimon Schubert {
3725796c8dcSSimon Schubert   struct target_ops *t;
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
375c50c785cSJohn Marino     if (t->to_has_execution (t, the_ptid))
3765796c8dcSSimon Schubert       return 1;
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   return 0;
3795796c8dcSSimon Schubert }
3805796c8dcSSimon Schubert 
381c50c785cSJohn Marino int
target_has_execution_current(void)382c50c785cSJohn Marino target_has_execution_current (void)
383c50c785cSJohn Marino {
384c50c785cSJohn Marino   return target_has_execution_1 (inferior_ptid);
385c50c785cSJohn Marino }
386c50c785cSJohn Marino 
3875796c8dcSSimon Schubert /* Add a possible target architecture to the list.  */
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert void
add_target(struct target_ops * t)3905796c8dcSSimon Schubert add_target (struct target_ops *t)
3915796c8dcSSimon Schubert {
3925796c8dcSSimon Schubert   /* Provide default values for all "must have" methods.  */
3935796c8dcSSimon Schubert   if (t->to_xfer_partial == NULL)
3945796c8dcSSimon Schubert     t->to_xfer_partial = default_xfer_partial;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert   if (t->to_has_all_memory == NULL)
3975796c8dcSSimon Schubert     t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   if (t->to_has_memory == NULL)
4005796c8dcSSimon Schubert     t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert   if (t->to_has_stack == NULL)
4035796c8dcSSimon Schubert     t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
4045796c8dcSSimon Schubert 
4055796c8dcSSimon Schubert   if (t->to_has_registers == NULL)
4065796c8dcSSimon Schubert     t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   if (t->to_has_execution == NULL)
409c50c785cSJohn Marino     t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
4105796c8dcSSimon Schubert 
4115796c8dcSSimon Schubert   if (!target_structs)
4125796c8dcSSimon Schubert     {
4135796c8dcSSimon Schubert       target_struct_allocsize = DEFAULT_ALLOCSIZE;
4145796c8dcSSimon Schubert       target_structs = (struct target_ops **) xmalloc
4155796c8dcSSimon Schubert 	(target_struct_allocsize * sizeof (*target_structs));
4165796c8dcSSimon Schubert     }
4175796c8dcSSimon Schubert   if (target_struct_size >= target_struct_allocsize)
4185796c8dcSSimon Schubert     {
4195796c8dcSSimon Schubert       target_struct_allocsize *= 2;
4205796c8dcSSimon Schubert       target_structs = (struct target_ops **)
4215796c8dcSSimon Schubert 	xrealloc ((char *) target_structs,
4225796c8dcSSimon Schubert 		  target_struct_allocsize * sizeof (*target_structs));
4235796c8dcSSimon Schubert     }
4245796c8dcSSimon Schubert   target_structs[target_struct_size++] = t;
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert   if (targetlist == NULL)
4275796c8dcSSimon Schubert     add_prefix_cmd ("target", class_run, target_command, _("\
4285796c8dcSSimon Schubert Connect to a target machine or process.\n\
4295796c8dcSSimon Schubert The first argument is the type or protocol of the target machine.\n\
4305796c8dcSSimon Schubert Remaining arguments are interpreted by the target protocol.  For more\n\
4315796c8dcSSimon Schubert information on the arguments for a particular protocol, type\n\
4325796c8dcSSimon Schubert `help target ' followed by the protocol name."),
4335796c8dcSSimon Schubert 		    &targetlist, "target ", 0, &cmdlist);
4345796c8dcSSimon Schubert   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
4355796c8dcSSimon Schubert }
4365796c8dcSSimon Schubert 
437*ef5ccd6cSJohn Marino /* See target.h.  */
438*ef5ccd6cSJohn Marino 
439*ef5ccd6cSJohn Marino void
add_deprecated_target_alias(struct target_ops * t,char * alias)440*ef5ccd6cSJohn Marino add_deprecated_target_alias (struct target_ops *t, char *alias)
441*ef5ccd6cSJohn Marino {
442*ef5ccd6cSJohn Marino   struct cmd_list_element *c;
443*ef5ccd6cSJohn Marino   char *alt;
444*ef5ccd6cSJohn Marino 
445*ef5ccd6cSJohn Marino   /* If we use add_alias_cmd, here, we do not get the deprecated warning,
446*ef5ccd6cSJohn Marino      see PR cli/15104.  */
447*ef5ccd6cSJohn Marino   c = add_cmd (alias, no_class, t->to_open, t->to_doc, &targetlist);
448*ef5ccd6cSJohn Marino   alt = xstrprintf ("target %s", t->to_shortname);
449*ef5ccd6cSJohn Marino   deprecate_cmd (c, alt);
450*ef5ccd6cSJohn Marino }
451*ef5ccd6cSJohn Marino 
4525796c8dcSSimon Schubert /* Stub functions */
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert void
target_ignore(void)4555796c8dcSSimon Schubert target_ignore (void)
4565796c8dcSSimon Schubert {
4575796c8dcSSimon Schubert }
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert void
target_kill(void)4605796c8dcSSimon Schubert target_kill (void)
4615796c8dcSSimon Schubert {
4625796c8dcSSimon Schubert   struct target_ops *t;
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
4655796c8dcSSimon Schubert     if (t->to_kill != NULL)
4665796c8dcSSimon Schubert       {
4675796c8dcSSimon Schubert 	if (targetdebug)
4685796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert         t->to_kill (t);
4715796c8dcSSimon Schubert 	return;
4725796c8dcSSimon Schubert       }
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert   noprocess ();
4755796c8dcSSimon Schubert }
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert void
target_load(char * arg,int from_tty)4785796c8dcSSimon Schubert target_load (char *arg, int from_tty)
4795796c8dcSSimon Schubert {
4805796c8dcSSimon Schubert   target_dcache_invalidate ();
4815796c8dcSSimon Schubert   (*current_target.to_load) (arg, from_tty);
4825796c8dcSSimon Schubert }
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert void
target_create_inferior(char * exec_file,char * args,char ** env,int from_tty)4855796c8dcSSimon Schubert target_create_inferior (char *exec_file, char *args,
4865796c8dcSSimon Schubert 			char **env, int from_tty)
4875796c8dcSSimon Schubert {
4885796c8dcSSimon Schubert   struct target_ops *t;
489cf7f2e2dSJohn Marino 
4905796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
4915796c8dcSSimon Schubert     {
4925796c8dcSSimon Schubert       if (t->to_create_inferior != NULL)
4935796c8dcSSimon Schubert 	{
4945796c8dcSSimon Schubert 	  t->to_create_inferior (t, exec_file, args, env, from_tty);
4955796c8dcSSimon Schubert 	  if (targetdebug)
4965796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog,
4975796c8dcSSimon Schubert 				"target_create_inferior (%s, %s, xxx, %d)\n",
4985796c8dcSSimon Schubert 				exec_file, args, from_tty);
4995796c8dcSSimon Schubert 	  return;
5005796c8dcSSimon Schubert 	}
5015796c8dcSSimon Schubert     }
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
504c50c785cSJohn Marino 		  _("could not find a target to create inferior"));
5055796c8dcSSimon Schubert }
5065796c8dcSSimon Schubert 
5075796c8dcSSimon Schubert void
target_terminal_inferior(void)5085796c8dcSSimon Schubert target_terminal_inferior (void)
5095796c8dcSSimon Schubert {
5105796c8dcSSimon Schubert   /* A background resume (``run&'') should leave GDB in control of the
511cf7f2e2dSJohn Marino      terminal.  Use target_can_async_p, not target_is_async_p, since at
512cf7f2e2dSJohn Marino      this point the target is not async yet.  However, if sync_execution
513cf7f2e2dSJohn Marino      is not set, we know it will become async prior to resume.  */
514cf7f2e2dSJohn Marino   if (target_can_async_p () && !sync_execution)
5155796c8dcSSimon Schubert     return;
5165796c8dcSSimon Schubert 
5175796c8dcSSimon Schubert   /* If GDB is resuming the inferior in the foreground, install
5185796c8dcSSimon Schubert      inferior's terminal modes.  */
5195796c8dcSSimon Schubert   (*current_target.to_terminal_inferior) ();
5205796c8dcSSimon Schubert }
5215796c8dcSSimon Schubert 
5225796c8dcSSimon Schubert static int
nomemory(CORE_ADDR memaddr,char * myaddr,int len,int write,struct target_ops * t)5235796c8dcSSimon Schubert nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
5245796c8dcSSimon Schubert 	  struct target_ops *t)
5255796c8dcSSimon Schubert {
526c50c785cSJohn Marino   errno = EIO;			/* Can't read/write this location.  */
527c50c785cSJohn Marino   return 0;			/* No bytes handled.  */
5285796c8dcSSimon Schubert }
5295796c8dcSSimon Schubert 
5305796c8dcSSimon Schubert static void
tcomplain(void)5315796c8dcSSimon Schubert tcomplain (void)
5325796c8dcSSimon Schubert {
5335796c8dcSSimon Schubert   error (_("You can't do that when your target is `%s'"),
5345796c8dcSSimon Schubert 	 current_target.to_shortname);
5355796c8dcSSimon Schubert }
5365796c8dcSSimon Schubert 
5375796c8dcSSimon Schubert void
noprocess(void)5385796c8dcSSimon Schubert noprocess (void)
5395796c8dcSSimon Schubert {
5405796c8dcSSimon Schubert   error (_("You can't do that without a process to debug."));
5415796c8dcSSimon Schubert }
5425796c8dcSSimon Schubert 
5435796c8dcSSimon Schubert static void
default_terminal_info(char * args,int from_tty)5445796c8dcSSimon Schubert default_terminal_info (char *args, int from_tty)
5455796c8dcSSimon Schubert {
5465796c8dcSSimon Schubert   printf_unfiltered (_("No saved terminal information.\n"));
5475796c8dcSSimon Schubert }
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert /* A default implementation for the to_get_ada_task_ptid target method.
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert    This function builds the PTID by using both LWP and TID as part of
5525796c8dcSSimon Schubert    the PTID lwp and tid elements.  The pid used is the pid of the
5535796c8dcSSimon Schubert    inferior_ptid.  */
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert static ptid_t
default_get_ada_task_ptid(long lwp,long tid)5565796c8dcSSimon Schubert default_get_ada_task_ptid (long lwp, long tid)
5575796c8dcSSimon Schubert {
5585796c8dcSSimon Schubert   return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
5595796c8dcSSimon Schubert }
5605796c8dcSSimon Schubert 
561a45ae5f8SJohn Marino static enum exec_direction_kind
default_execution_direction(void)562a45ae5f8SJohn Marino default_execution_direction (void)
563a45ae5f8SJohn Marino {
564a45ae5f8SJohn Marino   if (!target_can_execute_reverse)
565a45ae5f8SJohn Marino     return EXEC_FORWARD;
566a45ae5f8SJohn Marino   else if (!target_can_async_p ())
567a45ae5f8SJohn Marino     return EXEC_FORWARD;
568a45ae5f8SJohn Marino   else
569a45ae5f8SJohn Marino     gdb_assert_not_reached ("\
570a45ae5f8SJohn Marino to_execution_direction must be implemented for reverse async");
571a45ae5f8SJohn Marino }
572a45ae5f8SJohn Marino 
5735796c8dcSSimon Schubert /* Go through the target stack from top to bottom, copying over zero
5745796c8dcSSimon Schubert    entries in current_target, then filling in still empty entries.  In
5755796c8dcSSimon Schubert    effect, we are doing class inheritance through the pushed target
5765796c8dcSSimon Schubert    vectors.
5775796c8dcSSimon Schubert 
5785796c8dcSSimon Schubert    NOTE: cagney/2003-10-17: The problem with this inheritance, as it
5795796c8dcSSimon Schubert    is currently implemented, is that it discards any knowledge of
5805796c8dcSSimon Schubert    which target an inherited method originally belonged to.
5815796c8dcSSimon Schubert    Consequently, new new target methods should instead explicitly and
5825796c8dcSSimon Schubert    locally search the target stack for the target that can handle the
5835796c8dcSSimon Schubert    request.  */
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert static void
update_current_target(void)5865796c8dcSSimon Schubert update_current_target (void)
5875796c8dcSSimon Schubert {
5885796c8dcSSimon Schubert   struct target_ops *t;
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   /* First, reset current's contents.  */
5915796c8dcSSimon Schubert   memset (&current_target, 0, sizeof (current_target));
5925796c8dcSSimon Schubert 
5935796c8dcSSimon Schubert #define INHERIT(FIELD, TARGET) \
5945796c8dcSSimon Schubert       if (!current_target.FIELD) \
5955796c8dcSSimon Schubert 	current_target.FIELD = (TARGET)->FIELD
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert   for (t = target_stack; t; t = t->beneath)
5985796c8dcSSimon Schubert     {
5995796c8dcSSimon Schubert       INHERIT (to_shortname, t);
6005796c8dcSSimon Schubert       INHERIT (to_longname, t);
6015796c8dcSSimon Schubert       INHERIT (to_doc, t);
6025796c8dcSSimon Schubert       /* Do not inherit to_open.  */
6035796c8dcSSimon Schubert       /* Do not inherit to_close.  */
6045796c8dcSSimon Schubert       /* Do not inherit to_attach.  */
6055796c8dcSSimon Schubert       INHERIT (to_post_attach, t);
6065796c8dcSSimon Schubert       INHERIT (to_attach_no_wait, t);
6075796c8dcSSimon Schubert       /* Do not inherit to_detach.  */
6085796c8dcSSimon Schubert       /* Do not inherit to_disconnect.  */
6095796c8dcSSimon Schubert       /* Do not inherit to_resume.  */
6105796c8dcSSimon Schubert       /* Do not inherit to_wait.  */
6115796c8dcSSimon Schubert       /* Do not inherit to_fetch_registers.  */
6125796c8dcSSimon Schubert       /* Do not inherit to_store_registers.  */
6135796c8dcSSimon Schubert       INHERIT (to_prepare_to_store, t);
6145796c8dcSSimon Schubert       INHERIT (deprecated_xfer_memory, t);
6155796c8dcSSimon Schubert       INHERIT (to_files_info, t);
6165796c8dcSSimon Schubert       INHERIT (to_insert_breakpoint, t);
6175796c8dcSSimon Schubert       INHERIT (to_remove_breakpoint, t);
6185796c8dcSSimon Schubert       INHERIT (to_can_use_hw_breakpoint, t);
6195796c8dcSSimon Schubert       INHERIT (to_insert_hw_breakpoint, t);
6205796c8dcSSimon Schubert       INHERIT (to_remove_hw_breakpoint, t);
621c50c785cSJohn Marino       /* Do not inherit to_ranged_break_num_registers.  */
6225796c8dcSSimon Schubert       INHERIT (to_insert_watchpoint, t);
6235796c8dcSSimon Schubert       INHERIT (to_remove_watchpoint, t);
624a45ae5f8SJohn Marino       /* Do not inherit to_insert_mask_watchpoint.  */
625a45ae5f8SJohn Marino       /* Do not inherit to_remove_mask_watchpoint.  */
6265796c8dcSSimon Schubert       INHERIT (to_stopped_data_address, t);
6275796c8dcSSimon Schubert       INHERIT (to_have_steppable_watchpoint, t);
6285796c8dcSSimon Schubert       INHERIT (to_have_continuable_watchpoint, t);
6295796c8dcSSimon Schubert       INHERIT (to_stopped_by_watchpoint, t);
6305796c8dcSSimon Schubert       INHERIT (to_watchpoint_addr_within_range, t);
6315796c8dcSSimon Schubert       INHERIT (to_region_ok_for_hw_watchpoint, t);
632cf7f2e2dSJohn Marino       INHERIT (to_can_accel_watchpoint_condition, t);
633a45ae5f8SJohn Marino       /* Do not inherit to_masked_watch_num_registers.  */
6345796c8dcSSimon Schubert       INHERIT (to_terminal_init, t);
6355796c8dcSSimon Schubert       INHERIT (to_terminal_inferior, t);
6365796c8dcSSimon Schubert       INHERIT (to_terminal_ours_for_output, t);
6375796c8dcSSimon Schubert       INHERIT (to_terminal_ours, t);
6385796c8dcSSimon Schubert       INHERIT (to_terminal_save_ours, t);
6395796c8dcSSimon Schubert       INHERIT (to_terminal_info, t);
6405796c8dcSSimon Schubert       /* Do not inherit to_kill.  */
6415796c8dcSSimon Schubert       INHERIT (to_load, t);
6425796c8dcSSimon Schubert       /* Do no inherit to_create_inferior.  */
6435796c8dcSSimon Schubert       INHERIT (to_post_startup_inferior, t);
6445796c8dcSSimon Schubert       INHERIT (to_insert_fork_catchpoint, t);
6455796c8dcSSimon Schubert       INHERIT (to_remove_fork_catchpoint, t);
6465796c8dcSSimon Schubert       INHERIT (to_insert_vfork_catchpoint, t);
6475796c8dcSSimon Schubert       INHERIT (to_remove_vfork_catchpoint, t);
6485796c8dcSSimon Schubert       /* Do not inherit to_follow_fork.  */
6495796c8dcSSimon Schubert       INHERIT (to_insert_exec_catchpoint, t);
6505796c8dcSSimon Schubert       INHERIT (to_remove_exec_catchpoint, t);
6515796c8dcSSimon Schubert       INHERIT (to_set_syscall_catchpoint, t);
6525796c8dcSSimon Schubert       INHERIT (to_has_exited, t);
653cf7f2e2dSJohn Marino       /* Do not inherit to_mourn_inferior.  */
6545796c8dcSSimon Schubert       INHERIT (to_can_run, t);
655a45ae5f8SJohn Marino       /* Do not inherit to_pass_signals.  */
656*ef5ccd6cSJohn Marino       /* Do not inherit to_program_signals.  */
6575796c8dcSSimon Schubert       /* Do not inherit to_thread_alive.  */
6585796c8dcSSimon Schubert       /* Do not inherit to_find_new_threads.  */
6595796c8dcSSimon Schubert       /* Do not inherit to_pid_to_str.  */
6605796c8dcSSimon Schubert       INHERIT (to_extra_thread_info, t);
661c50c785cSJohn Marino       INHERIT (to_thread_name, t);
6625796c8dcSSimon Schubert       INHERIT (to_stop, t);
6635796c8dcSSimon Schubert       /* Do not inherit to_xfer_partial.  */
6645796c8dcSSimon Schubert       INHERIT (to_rcmd, t);
6655796c8dcSSimon Schubert       INHERIT (to_pid_to_exec_file, t);
6665796c8dcSSimon Schubert       INHERIT (to_log_command, t);
6675796c8dcSSimon Schubert       INHERIT (to_stratum, t);
668c50c785cSJohn Marino       /* Do not inherit to_has_all_memory.  */
669c50c785cSJohn Marino       /* Do not inherit to_has_memory.  */
670c50c785cSJohn Marino       /* Do not inherit to_has_stack.  */
671c50c785cSJohn Marino       /* Do not inherit to_has_registers.  */
672c50c785cSJohn Marino       /* Do not inherit to_has_execution.  */
6735796c8dcSSimon Schubert       INHERIT (to_has_thread_control, t);
6745796c8dcSSimon Schubert       INHERIT (to_can_async_p, t);
6755796c8dcSSimon Schubert       INHERIT (to_is_async_p, t);
6765796c8dcSSimon Schubert       INHERIT (to_async, t);
6775796c8dcSSimon Schubert       INHERIT (to_find_memory_regions, t);
6785796c8dcSSimon Schubert       INHERIT (to_make_corefile_notes, t);
679cf7f2e2dSJohn Marino       INHERIT (to_get_bookmark, t);
680cf7f2e2dSJohn Marino       INHERIT (to_goto_bookmark, t);
6815796c8dcSSimon Schubert       /* Do not inherit to_get_thread_local_address.  */
6825796c8dcSSimon Schubert       INHERIT (to_can_execute_reverse, t);
683a45ae5f8SJohn Marino       INHERIT (to_execution_direction, t);
6845796c8dcSSimon Schubert       INHERIT (to_thread_architecture, t);
6855796c8dcSSimon Schubert       /* Do not inherit to_read_description.  */
6865796c8dcSSimon Schubert       INHERIT (to_get_ada_task_ptid, t);
6875796c8dcSSimon Schubert       /* Do not inherit to_search_memory.  */
6885796c8dcSSimon Schubert       INHERIT (to_supports_multi_process, t);
689a45ae5f8SJohn Marino       INHERIT (to_supports_enable_disable_tracepoint, t);
690a45ae5f8SJohn Marino       INHERIT (to_supports_string_tracing, t);
691cf7f2e2dSJohn Marino       INHERIT (to_trace_init, t);
692cf7f2e2dSJohn Marino       INHERIT (to_download_tracepoint, t);
693a45ae5f8SJohn Marino       INHERIT (to_can_download_tracepoint, t);
694cf7f2e2dSJohn Marino       INHERIT (to_download_trace_state_variable, t);
695a45ae5f8SJohn Marino       INHERIT (to_enable_tracepoint, t);
696a45ae5f8SJohn Marino       INHERIT (to_disable_tracepoint, t);
697cf7f2e2dSJohn Marino       INHERIT (to_trace_set_readonly_regions, t);
698cf7f2e2dSJohn Marino       INHERIT (to_trace_start, t);
699cf7f2e2dSJohn Marino       INHERIT (to_get_trace_status, t);
700a45ae5f8SJohn Marino       INHERIT (to_get_tracepoint_status, t);
701cf7f2e2dSJohn Marino       INHERIT (to_trace_stop, t);
702cf7f2e2dSJohn Marino       INHERIT (to_trace_find, t);
703cf7f2e2dSJohn Marino       INHERIT (to_get_trace_state_variable_value, t);
704cf7f2e2dSJohn Marino       INHERIT (to_save_trace_data, t);
705cf7f2e2dSJohn Marino       INHERIT (to_upload_tracepoints, t);
706cf7f2e2dSJohn Marino       INHERIT (to_upload_trace_state_variables, t);
707cf7f2e2dSJohn Marino       INHERIT (to_get_raw_trace_data, t);
708a45ae5f8SJohn Marino       INHERIT (to_get_min_fast_tracepoint_insn_len, t);
709cf7f2e2dSJohn Marino       INHERIT (to_set_disconnected_tracing, t);
710cf7f2e2dSJohn Marino       INHERIT (to_set_circular_trace_buffer, t);
711*ef5ccd6cSJohn Marino       INHERIT (to_set_trace_buffer_size, t);
712a45ae5f8SJohn Marino       INHERIT (to_set_trace_notes, t);
713cf7f2e2dSJohn Marino       INHERIT (to_get_tib_address, t);
714cf7f2e2dSJohn Marino       INHERIT (to_set_permissions, t);
715cf7f2e2dSJohn Marino       INHERIT (to_static_tracepoint_marker_at, t);
716cf7f2e2dSJohn Marino       INHERIT (to_static_tracepoint_markers_by_strid, t);
717c50c785cSJohn Marino       INHERIT (to_traceframe_info, t);
718*ef5ccd6cSJohn Marino       INHERIT (to_use_agent, t);
719*ef5ccd6cSJohn Marino       INHERIT (to_can_use_agent, t);
7205796c8dcSSimon Schubert       INHERIT (to_magic, t);
721*ef5ccd6cSJohn Marino       INHERIT (to_supports_evaluation_of_breakpoint_conditions, t);
722*ef5ccd6cSJohn Marino       INHERIT (to_can_run_breakpoint_commands, t);
7235796c8dcSSimon Schubert       /* Do not inherit to_memory_map.  */
7245796c8dcSSimon Schubert       /* Do not inherit to_flash_erase.  */
7255796c8dcSSimon Schubert       /* Do not inherit to_flash_done.  */
7265796c8dcSSimon Schubert     }
7275796c8dcSSimon Schubert #undef INHERIT
7285796c8dcSSimon Schubert 
7295796c8dcSSimon Schubert   /* Clean up a target struct so it no longer has any zero pointers in
7305796c8dcSSimon Schubert      it.  Some entries are defaulted to a method that print an error,
7315796c8dcSSimon Schubert      others are hard-wired to a standard recursive default.  */
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert #define de_fault(field, value) \
7345796c8dcSSimon Schubert   if (!current_target.field)               \
7355796c8dcSSimon Schubert     current_target.field = value
7365796c8dcSSimon Schubert 
7375796c8dcSSimon Schubert   de_fault (to_open,
7385796c8dcSSimon Schubert 	    (void (*) (char *, int))
7395796c8dcSSimon Schubert 	    tcomplain);
7405796c8dcSSimon Schubert   de_fault (to_close,
7415796c8dcSSimon Schubert 	    (void (*) (int))
7425796c8dcSSimon Schubert 	    target_ignore);
7435796c8dcSSimon Schubert   de_fault (to_post_attach,
7445796c8dcSSimon Schubert 	    (void (*) (int))
7455796c8dcSSimon Schubert 	    target_ignore);
7465796c8dcSSimon Schubert   de_fault (to_prepare_to_store,
7475796c8dcSSimon Schubert 	    (void (*) (struct regcache *))
7485796c8dcSSimon Schubert 	    noprocess);
7495796c8dcSSimon Schubert   de_fault (deprecated_xfer_memory,
750c50c785cSJohn Marino 	    (int (*) (CORE_ADDR, gdb_byte *, int, int,
751c50c785cSJohn Marino 		      struct mem_attrib *, struct target_ops *))
7525796c8dcSSimon Schubert 	    nomemory);
7535796c8dcSSimon Schubert   de_fault (to_files_info,
7545796c8dcSSimon Schubert 	    (void (*) (struct target_ops *))
7555796c8dcSSimon Schubert 	    target_ignore);
7565796c8dcSSimon Schubert   de_fault (to_insert_breakpoint,
7575796c8dcSSimon Schubert 	    memory_insert_breakpoint);
7585796c8dcSSimon Schubert   de_fault (to_remove_breakpoint,
7595796c8dcSSimon Schubert 	    memory_remove_breakpoint);
7605796c8dcSSimon Schubert   de_fault (to_can_use_hw_breakpoint,
7615796c8dcSSimon Schubert 	    (int (*) (int, int, int))
7625796c8dcSSimon Schubert 	    return_zero);
7635796c8dcSSimon Schubert   de_fault (to_insert_hw_breakpoint,
7645796c8dcSSimon Schubert 	    (int (*) (struct gdbarch *, struct bp_target_info *))
7655796c8dcSSimon Schubert 	    return_minus_one);
7665796c8dcSSimon Schubert   de_fault (to_remove_hw_breakpoint,
7675796c8dcSSimon Schubert 	    (int (*) (struct gdbarch *, struct bp_target_info *))
7685796c8dcSSimon Schubert 	    return_minus_one);
7695796c8dcSSimon Schubert   de_fault (to_insert_watchpoint,
770cf7f2e2dSJohn Marino 	    (int (*) (CORE_ADDR, int, int, struct expression *))
7715796c8dcSSimon Schubert 	    return_minus_one);
7725796c8dcSSimon Schubert   de_fault (to_remove_watchpoint,
773cf7f2e2dSJohn Marino 	    (int (*) (CORE_ADDR, int, int, struct expression *))
7745796c8dcSSimon Schubert 	    return_minus_one);
7755796c8dcSSimon Schubert   de_fault (to_stopped_by_watchpoint,
7765796c8dcSSimon Schubert 	    (int (*) (void))
7775796c8dcSSimon Schubert 	    return_zero);
7785796c8dcSSimon Schubert   de_fault (to_stopped_data_address,
7795796c8dcSSimon Schubert 	    (int (*) (struct target_ops *, CORE_ADDR *))
7805796c8dcSSimon Schubert 	    return_zero);
7815796c8dcSSimon Schubert   de_fault (to_watchpoint_addr_within_range,
7825796c8dcSSimon Schubert 	    default_watchpoint_addr_within_range);
7835796c8dcSSimon Schubert   de_fault (to_region_ok_for_hw_watchpoint,
7845796c8dcSSimon Schubert 	    default_region_ok_for_hw_watchpoint);
785cf7f2e2dSJohn Marino   de_fault (to_can_accel_watchpoint_condition,
786cf7f2e2dSJohn Marino             (int (*) (CORE_ADDR, int, int, struct expression *))
787cf7f2e2dSJohn Marino             return_zero);
7885796c8dcSSimon Schubert   de_fault (to_terminal_init,
7895796c8dcSSimon Schubert 	    (void (*) (void))
7905796c8dcSSimon Schubert 	    target_ignore);
7915796c8dcSSimon Schubert   de_fault (to_terminal_inferior,
7925796c8dcSSimon Schubert 	    (void (*) (void))
7935796c8dcSSimon Schubert 	    target_ignore);
7945796c8dcSSimon Schubert   de_fault (to_terminal_ours_for_output,
7955796c8dcSSimon Schubert 	    (void (*) (void))
7965796c8dcSSimon Schubert 	    target_ignore);
7975796c8dcSSimon Schubert   de_fault (to_terminal_ours,
7985796c8dcSSimon Schubert 	    (void (*) (void))
7995796c8dcSSimon Schubert 	    target_ignore);
8005796c8dcSSimon Schubert   de_fault (to_terminal_save_ours,
8015796c8dcSSimon Schubert 	    (void (*) (void))
8025796c8dcSSimon Schubert 	    target_ignore);
8035796c8dcSSimon Schubert   de_fault (to_terminal_info,
8045796c8dcSSimon Schubert 	    default_terminal_info);
8055796c8dcSSimon Schubert   de_fault (to_load,
8065796c8dcSSimon Schubert 	    (void (*) (char *, int))
8075796c8dcSSimon Schubert 	    tcomplain);
8085796c8dcSSimon Schubert   de_fault (to_post_startup_inferior,
8095796c8dcSSimon Schubert 	    (void (*) (ptid_t))
8105796c8dcSSimon Schubert 	    target_ignore);
8115796c8dcSSimon Schubert   de_fault (to_insert_fork_catchpoint,
812c50c785cSJohn Marino 	    (int (*) (int))
813c50c785cSJohn Marino 	    return_one);
8145796c8dcSSimon Schubert   de_fault (to_remove_fork_catchpoint,
8155796c8dcSSimon Schubert 	    (int (*) (int))
816c50c785cSJohn Marino 	    return_one);
8175796c8dcSSimon Schubert   de_fault (to_insert_vfork_catchpoint,
818c50c785cSJohn Marino 	    (int (*) (int))
819c50c785cSJohn Marino 	    return_one);
8205796c8dcSSimon Schubert   de_fault (to_remove_vfork_catchpoint,
8215796c8dcSSimon Schubert 	    (int (*) (int))
822c50c785cSJohn Marino 	    return_one);
8235796c8dcSSimon Schubert   de_fault (to_insert_exec_catchpoint,
824c50c785cSJohn Marino 	    (int (*) (int))
825c50c785cSJohn Marino 	    return_one);
8265796c8dcSSimon Schubert   de_fault (to_remove_exec_catchpoint,
8275796c8dcSSimon Schubert 	    (int (*) (int))
828c50c785cSJohn Marino 	    return_one);
8295796c8dcSSimon Schubert   de_fault (to_set_syscall_catchpoint,
8305796c8dcSSimon Schubert 	    (int (*) (int, int, int, int, int *))
831c50c785cSJohn Marino 	    return_one);
8325796c8dcSSimon Schubert   de_fault (to_has_exited,
8335796c8dcSSimon Schubert 	    (int (*) (int, int, int *))
8345796c8dcSSimon Schubert 	    return_zero);
8355796c8dcSSimon Schubert   de_fault (to_can_run,
8365796c8dcSSimon Schubert 	    return_zero);
8375796c8dcSSimon Schubert   de_fault (to_extra_thread_info,
8385796c8dcSSimon Schubert 	    (char *(*) (struct thread_info *))
8395796c8dcSSimon Schubert 	    return_zero);
840c50c785cSJohn Marino   de_fault (to_thread_name,
841c50c785cSJohn Marino 	    (char *(*) (struct thread_info *))
842c50c785cSJohn Marino 	    return_zero);
8435796c8dcSSimon Schubert   de_fault (to_stop,
8445796c8dcSSimon Schubert 	    (void (*) (ptid_t))
8455796c8dcSSimon Schubert 	    target_ignore);
8465796c8dcSSimon Schubert   current_target.to_xfer_partial = current_xfer_partial;
8475796c8dcSSimon Schubert   de_fault (to_rcmd,
8485796c8dcSSimon Schubert 	    (void (*) (char *, struct ui_file *))
8495796c8dcSSimon Schubert 	    tcomplain);
8505796c8dcSSimon Schubert   de_fault (to_pid_to_exec_file,
8515796c8dcSSimon Schubert 	    (char *(*) (int))
8525796c8dcSSimon Schubert 	    return_zero);
8535796c8dcSSimon Schubert   de_fault (to_async,
8545796c8dcSSimon Schubert 	    (void (*) (void (*) (enum inferior_event_type, void*), void*))
8555796c8dcSSimon Schubert 	    tcomplain);
8565796c8dcSSimon Schubert   de_fault (to_thread_architecture,
8575796c8dcSSimon Schubert 	    default_thread_architecture);
8585796c8dcSSimon Schubert   current_target.to_read_description = NULL;
8595796c8dcSSimon Schubert   de_fault (to_get_ada_task_ptid,
8605796c8dcSSimon Schubert             (ptid_t (*) (long, long))
8615796c8dcSSimon Schubert             default_get_ada_task_ptid);
8625796c8dcSSimon Schubert   de_fault (to_supports_multi_process,
8635796c8dcSSimon Schubert 	    (int (*) (void))
8645796c8dcSSimon Schubert 	    return_zero);
865a45ae5f8SJohn Marino   de_fault (to_supports_enable_disable_tracepoint,
866a45ae5f8SJohn Marino 	    (int (*) (void))
867a45ae5f8SJohn Marino 	    return_zero);
868a45ae5f8SJohn Marino   de_fault (to_supports_string_tracing,
869a45ae5f8SJohn Marino 	    (int (*) (void))
870a45ae5f8SJohn Marino 	    return_zero);
871cf7f2e2dSJohn Marino   de_fault (to_trace_init,
872cf7f2e2dSJohn Marino 	    (void (*) (void))
873cf7f2e2dSJohn Marino 	    tcomplain);
874cf7f2e2dSJohn Marino   de_fault (to_download_tracepoint,
875a45ae5f8SJohn Marino 	    (void (*) (struct bp_location *))
876cf7f2e2dSJohn Marino 	    tcomplain);
877a45ae5f8SJohn Marino   de_fault (to_can_download_tracepoint,
878a45ae5f8SJohn Marino 	    (int (*) (void))
879a45ae5f8SJohn Marino 	    return_zero);
880cf7f2e2dSJohn Marino   de_fault (to_download_trace_state_variable,
881cf7f2e2dSJohn Marino 	    (void (*) (struct trace_state_variable *))
882cf7f2e2dSJohn Marino 	    tcomplain);
883a45ae5f8SJohn Marino   de_fault (to_enable_tracepoint,
884a45ae5f8SJohn Marino 	    (void (*) (struct bp_location *))
885a45ae5f8SJohn Marino 	    tcomplain);
886a45ae5f8SJohn Marino   de_fault (to_disable_tracepoint,
887a45ae5f8SJohn Marino 	    (void (*) (struct bp_location *))
888a45ae5f8SJohn Marino 	    tcomplain);
889cf7f2e2dSJohn Marino   de_fault (to_trace_set_readonly_regions,
890cf7f2e2dSJohn Marino 	    (void (*) (void))
891cf7f2e2dSJohn Marino 	    tcomplain);
892cf7f2e2dSJohn Marino   de_fault (to_trace_start,
893cf7f2e2dSJohn Marino 	    (void (*) (void))
894cf7f2e2dSJohn Marino 	    tcomplain);
895cf7f2e2dSJohn Marino   de_fault (to_get_trace_status,
896cf7f2e2dSJohn Marino 	    (int (*) (struct trace_status *))
897cf7f2e2dSJohn Marino 	    return_minus_one);
898a45ae5f8SJohn Marino   de_fault (to_get_tracepoint_status,
899a45ae5f8SJohn Marino 	    (void (*) (struct breakpoint *, struct uploaded_tp *))
900a45ae5f8SJohn Marino 	    tcomplain);
901cf7f2e2dSJohn Marino   de_fault (to_trace_stop,
902cf7f2e2dSJohn Marino 	    (void (*) (void))
903cf7f2e2dSJohn Marino 	    tcomplain);
904cf7f2e2dSJohn Marino   de_fault (to_trace_find,
905cf7f2e2dSJohn Marino 	    (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
906cf7f2e2dSJohn Marino 	    return_minus_one);
907cf7f2e2dSJohn Marino   de_fault (to_get_trace_state_variable_value,
908cf7f2e2dSJohn Marino 	    (int (*) (int, LONGEST *))
909cf7f2e2dSJohn Marino 	    return_zero);
910cf7f2e2dSJohn Marino   de_fault (to_save_trace_data,
911cf7f2e2dSJohn Marino 	    (int (*) (const char *))
912cf7f2e2dSJohn Marino 	    tcomplain);
913cf7f2e2dSJohn Marino   de_fault (to_upload_tracepoints,
914cf7f2e2dSJohn Marino 	    (int (*) (struct uploaded_tp **))
915cf7f2e2dSJohn Marino 	    return_zero);
916cf7f2e2dSJohn Marino   de_fault (to_upload_trace_state_variables,
917cf7f2e2dSJohn Marino 	    (int (*) (struct uploaded_tsv **))
918cf7f2e2dSJohn Marino 	    return_zero);
919cf7f2e2dSJohn Marino   de_fault (to_get_raw_trace_data,
920cf7f2e2dSJohn Marino 	    (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
921cf7f2e2dSJohn Marino 	    tcomplain);
922a45ae5f8SJohn Marino   de_fault (to_get_min_fast_tracepoint_insn_len,
923a45ae5f8SJohn Marino 	    (int (*) (void))
924a45ae5f8SJohn Marino 	    return_minus_one);
925cf7f2e2dSJohn Marino   de_fault (to_set_disconnected_tracing,
926cf7f2e2dSJohn Marino 	    (void (*) (int))
927cf7f2e2dSJohn Marino 	    target_ignore);
928cf7f2e2dSJohn Marino   de_fault (to_set_circular_trace_buffer,
929cf7f2e2dSJohn Marino 	    (void (*) (int))
930cf7f2e2dSJohn Marino 	    target_ignore);
931*ef5ccd6cSJohn Marino   de_fault (to_set_trace_buffer_size,
932*ef5ccd6cSJohn Marino 	    (void (*) (LONGEST))
933*ef5ccd6cSJohn Marino 	    target_ignore);
934a45ae5f8SJohn Marino   de_fault (to_set_trace_notes,
935a45ae5f8SJohn Marino 	    (int (*) (char *, char *, char *))
936a45ae5f8SJohn Marino 	    return_zero);
937cf7f2e2dSJohn Marino   de_fault (to_get_tib_address,
938cf7f2e2dSJohn Marino 	    (int (*) (ptid_t, CORE_ADDR *))
939cf7f2e2dSJohn Marino 	    tcomplain);
940cf7f2e2dSJohn Marino   de_fault (to_set_permissions,
941cf7f2e2dSJohn Marino 	    (void (*) (void))
942cf7f2e2dSJohn Marino 	    target_ignore);
943cf7f2e2dSJohn Marino   de_fault (to_static_tracepoint_marker_at,
944cf7f2e2dSJohn Marino 	    (int (*) (CORE_ADDR, struct static_tracepoint_marker *))
945cf7f2e2dSJohn Marino 	    return_zero);
946cf7f2e2dSJohn Marino   de_fault (to_static_tracepoint_markers_by_strid,
947cf7f2e2dSJohn Marino 	    (VEC(static_tracepoint_marker_p) * (*) (const char *))
948cf7f2e2dSJohn Marino 	    tcomplain);
949c50c785cSJohn Marino   de_fault (to_traceframe_info,
950c50c785cSJohn Marino 	    (struct traceframe_info * (*) (void))
951c50c785cSJohn Marino 	    tcomplain);
952*ef5ccd6cSJohn Marino   de_fault (to_supports_evaluation_of_breakpoint_conditions,
953*ef5ccd6cSJohn Marino 	    (int (*) (void))
954*ef5ccd6cSJohn Marino 	    return_zero);
955*ef5ccd6cSJohn Marino   de_fault (to_can_run_breakpoint_commands,
956*ef5ccd6cSJohn Marino 	    (int (*) (void))
957*ef5ccd6cSJohn Marino 	    return_zero);
958*ef5ccd6cSJohn Marino   de_fault (to_use_agent,
959*ef5ccd6cSJohn Marino 	    (int (*) (int))
960*ef5ccd6cSJohn Marino 	    tcomplain);
961*ef5ccd6cSJohn Marino   de_fault (to_can_use_agent,
962*ef5ccd6cSJohn Marino 	    (int (*) (void))
963*ef5ccd6cSJohn Marino 	    return_zero);
964a45ae5f8SJohn Marino   de_fault (to_execution_direction, default_execution_direction);
965a45ae5f8SJohn Marino 
9665796c8dcSSimon Schubert #undef de_fault
9675796c8dcSSimon Schubert 
9685796c8dcSSimon Schubert   /* Finally, position the target-stack beneath the squashed
9695796c8dcSSimon Schubert      "current_target".  That way code looking for a non-inherited
9705796c8dcSSimon Schubert      target method can quickly and simply find it.  */
9715796c8dcSSimon Schubert   current_target.beneath = target_stack;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert   if (targetdebug)
9745796c8dcSSimon Schubert     setup_target_debug ();
9755796c8dcSSimon Schubert }
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert /* Push a new target type into the stack of the existing target accessors,
9785796c8dcSSimon Schubert    possibly superseding some of the existing accessors.
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert    Rather than allow an empty stack, we always have the dummy target at
9815796c8dcSSimon Schubert    the bottom stratum, so we can call the function vectors without
9825796c8dcSSimon Schubert    checking them.  */
9835796c8dcSSimon Schubert 
984cf7f2e2dSJohn Marino void
push_target(struct target_ops * t)9855796c8dcSSimon Schubert push_target (struct target_ops *t)
9865796c8dcSSimon Schubert {
9875796c8dcSSimon Schubert   struct target_ops **cur;
9885796c8dcSSimon Schubert 
9895796c8dcSSimon Schubert   /* Check magic number.  If wrong, it probably means someone changed
9905796c8dcSSimon Schubert      the struct definition, but not all the places that initialize one.  */
9915796c8dcSSimon Schubert   if (t->to_magic != OPS_MAGIC)
9925796c8dcSSimon Schubert     {
9935796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stderr,
9945796c8dcSSimon Schubert 			  "Magic number of %s target struct wrong\n",
9955796c8dcSSimon Schubert 			  t->to_shortname);
996c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
997c50c785cSJohn Marino 		      _("failed internal consistency check"));
9985796c8dcSSimon Schubert     }
9995796c8dcSSimon Schubert 
10005796c8dcSSimon Schubert   /* Find the proper stratum to install this target in.  */
10015796c8dcSSimon Schubert   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
10025796c8dcSSimon Schubert     {
10035796c8dcSSimon Schubert       if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
10045796c8dcSSimon Schubert 	break;
10055796c8dcSSimon Schubert     }
10065796c8dcSSimon Schubert 
10075796c8dcSSimon Schubert   /* If there's already targets at this stratum, remove them.  */
10085796c8dcSSimon Schubert   /* FIXME: cagney/2003-10-15: I think this should be popping all
10095796c8dcSSimon Schubert      targets to CUR, and not just those at this stratum level.  */
10105796c8dcSSimon Schubert   while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
10115796c8dcSSimon Schubert     {
10125796c8dcSSimon Schubert       /* There's already something at this stratum level.  Close it,
10135796c8dcSSimon Schubert          and un-hook it from the stack.  */
10145796c8dcSSimon Schubert       struct target_ops *tmp = (*cur);
1015cf7f2e2dSJohn Marino 
10165796c8dcSSimon Schubert       (*cur) = (*cur)->beneath;
10175796c8dcSSimon Schubert       tmp->beneath = NULL;
10185796c8dcSSimon Schubert       target_close (tmp, 0);
10195796c8dcSSimon Schubert     }
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert   /* We have removed all targets in our stratum, now add the new one.  */
10225796c8dcSSimon Schubert   t->beneath = (*cur);
10235796c8dcSSimon Schubert   (*cur) = t;
10245796c8dcSSimon Schubert 
10255796c8dcSSimon Schubert   update_current_target ();
10265796c8dcSSimon Schubert }
10275796c8dcSSimon Schubert 
10285796c8dcSSimon Schubert /* Remove a target_ops vector from the stack, wherever it may be.
10295796c8dcSSimon Schubert    Return how many times it was removed (0 or 1).  */
10305796c8dcSSimon Schubert 
10315796c8dcSSimon Schubert int
unpush_target(struct target_ops * t)10325796c8dcSSimon Schubert unpush_target (struct target_ops *t)
10335796c8dcSSimon Schubert {
10345796c8dcSSimon Schubert   struct target_ops **cur;
10355796c8dcSSimon Schubert   struct target_ops *tmp;
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert   if (t->to_stratum == dummy_stratum)
10385796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
1039c50c785cSJohn Marino 		    _("Attempt to unpush the dummy target"));
10405796c8dcSSimon Schubert 
10415796c8dcSSimon Schubert   /* Look for the specified target.  Note that we assume that a target
10425796c8dcSSimon Schubert      can only occur once in the target stack.  */
10435796c8dcSSimon Schubert 
10445796c8dcSSimon Schubert   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
10455796c8dcSSimon Schubert     {
10465796c8dcSSimon Schubert       if ((*cur) == t)
10475796c8dcSSimon Schubert 	break;
10485796c8dcSSimon Schubert     }
10495796c8dcSSimon Schubert 
1050*ef5ccd6cSJohn Marino   /* If we don't find target_ops, quit.  Only open targets should be
1051*ef5ccd6cSJohn Marino      closed.  */
10525796c8dcSSimon Schubert   if ((*cur) == NULL)
1053*ef5ccd6cSJohn Marino     return 0;
10545796c8dcSSimon Schubert 
1055c50c785cSJohn Marino   /* Unchain the target.  */
10565796c8dcSSimon Schubert   tmp = (*cur);
10575796c8dcSSimon Schubert   (*cur) = (*cur)->beneath;
10585796c8dcSSimon Schubert   tmp->beneath = NULL;
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert   update_current_target ();
10615796c8dcSSimon Schubert 
1062*ef5ccd6cSJohn Marino   /* Finally close the target.  Note we do this after unchaining, so
1063*ef5ccd6cSJohn Marino      any target method calls from within the target_close
1064*ef5ccd6cSJohn Marino      implementation don't end up in T anymore.  */
1065*ef5ccd6cSJohn Marino   target_close (t, 0);
1066*ef5ccd6cSJohn Marino 
10675796c8dcSSimon Schubert   return 1;
10685796c8dcSSimon Schubert }
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert void
pop_target(void)10715796c8dcSSimon Schubert pop_target (void)
10725796c8dcSSimon Schubert {
1073c50c785cSJohn Marino   target_close (target_stack, 0);	/* Let it clean up.  */
10745796c8dcSSimon Schubert   if (unpush_target (target_stack) == 1)
10755796c8dcSSimon Schubert     return;
10765796c8dcSSimon Schubert 
10775796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stderr,
10785796c8dcSSimon Schubert 		      "pop_target couldn't find target %s\n",
10795796c8dcSSimon Schubert 		      current_target.to_shortname);
1080cf7f2e2dSJohn Marino   internal_error (__FILE__, __LINE__,
1081cf7f2e2dSJohn Marino 		  _("failed internal consistency check"));
10825796c8dcSSimon Schubert }
10835796c8dcSSimon Schubert 
10845796c8dcSSimon Schubert void
pop_all_targets_above(enum strata above_stratum,int quitting)10855796c8dcSSimon Schubert pop_all_targets_above (enum strata above_stratum, int quitting)
10865796c8dcSSimon Schubert {
10875796c8dcSSimon Schubert   while ((int) (current_target.to_stratum) > (int) above_stratum)
10885796c8dcSSimon Schubert     {
10895796c8dcSSimon Schubert       target_close (target_stack, quitting);
10905796c8dcSSimon Schubert       if (!unpush_target (target_stack))
10915796c8dcSSimon Schubert 	{
10925796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr,
10935796c8dcSSimon Schubert 			      "pop_all_targets couldn't find target %s\n",
10945796c8dcSSimon Schubert 			      target_stack->to_shortname);
10955796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
10965796c8dcSSimon Schubert 			  _("failed internal consistency check"));
10975796c8dcSSimon Schubert 	  break;
10985796c8dcSSimon Schubert 	}
10995796c8dcSSimon Schubert     }
11005796c8dcSSimon Schubert }
11015796c8dcSSimon Schubert 
11025796c8dcSSimon Schubert void
pop_all_targets(int quitting)11035796c8dcSSimon Schubert pop_all_targets (int quitting)
11045796c8dcSSimon Schubert {
11055796c8dcSSimon Schubert   pop_all_targets_above (dummy_stratum, quitting);
11065796c8dcSSimon Schubert }
11075796c8dcSSimon Schubert 
1108c50c785cSJohn Marino /* Return 1 if T is now pushed in the target stack.  Return 0 otherwise.  */
1109c50c785cSJohn Marino 
1110c50c785cSJohn Marino int
target_is_pushed(struct target_ops * t)1111c50c785cSJohn Marino target_is_pushed (struct target_ops *t)
1112c50c785cSJohn Marino {
1113c50c785cSJohn Marino   struct target_ops **cur;
1114c50c785cSJohn Marino 
1115c50c785cSJohn Marino   /* Check magic number.  If wrong, it probably means someone changed
1116c50c785cSJohn Marino      the struct definition, but not all the places that initialize one.  */
1117c50c785cSJohn Marino   if (t->to_magic != OPS_MAGIC)
1118c50c785cSJohn Marino     {
1119c50c785cSJohn Marino       fprintf_unfiltered (gdb_stderr,
1120c50c785cSJohn Marino 			  "Magic number of %s target struct wrong\n",
1121c50c785cSJohn Marino 			  t->to_shortname);
1122c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
1123c50c785cSJohn Marino 		      _("failed internal consistency check"));
1124c50c785cSJohn Marino     }
1125c50c785cSJohn Marino 
1126c50c785cSJohn Marino   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1127c50c785cSJohn Marino     if (*cur == t)
1128c50c785cSJohn Marino       return 1;
1129c50c785cSJohn Marino 
1130c50c785cSJohn Marino   return 0;
1131c50c785cSJohn Marino }
1132c50c785cSJohn Marino 
11335796c8dcSSimon Schubert /* Using the objfile specified in OBJFILE, find the address for the
11345796c8dcSSimon Schubert    current thread's thread-local storage with offset OFFSET.  */
11355796c8dcSSimon Schubert CORE_ADDR
target_translate_tls_address(struct objfile * objfile,CORE_ADDR offset)11365796c8dcSSimon Schubert target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
11375796c8dcSSimon Schubert {
11385796c8dcSSimon Schubert   volatile CORE_ADDR addr = 0;
11395796c8dcSSimon Schubert   struct target_ops *target;
11405796c8dcSSimon Schubert 
11415796c8dcSSimon Schubert   for (target = current_target.beneath;
11425796c8dcSSimon Schubert        target != NULL;
11435796c8dcSSimon Schubert        target = target->beneath)
11445796c8dcSSimon Schubert     {
11455796c8dcSSimon Schubert       if (target->to_get_thread_local_address != NULL)
11465796c8dcSSimon Schubert 	break;
11475796c8dcSSimon Schubert     }
11485796c8dcSSimon Schubert 
11495796c8dcSSimon Schubert   if (target != NULL
1150*ef5ccd6cSJohn Marino       && gdbarch_fetch_tls_load_module_address_p (target_gdbarch ()))
11515796c8dcSSimon Schubert     {
11525796c8dcSSimon Schubert       ptid_t ptid = inferior_ptid;
11535796c8dcSSimon Schubert       volatile struct gdb_exception ex;
11545796c8dcSSimon Schubert 
11555796c8dcSSimon Schubert       TRY_CATCH (ex, RETURN_MASK_ALL)
11565796c8dcSSimon Schubert 	{
11575796c8dcSSimon Schubert 	  CORE_ADDR lm_addr;
11585796c8dcSSimon Schubert 
11595796c8dcSSimon Schubert 	  /* Fetch the load module address for this objfile.  */
1160*ef5ccd6cSJohn Marino 	  lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
11615796c8dcSSimon Schubert 	                                                   objfile);
11625796c8dcSSimon Schubert 	  /* If it's 0, throw the appropriate exception.  */
11635796c8dcSSimon Schubert 	  if (lm_addr == 0)
11645796c8dcSSimon Schubert 	    throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
11655796c8dcSSimon Schubert 			 _("TLS load module not found"));
11665796c8dcSSimon Schubert 
1167c50c785cSJohn Marino 	  addr = target->to_get_thread_local_address (target, ptid,
1168c50c785cSJohn Marino 						      lm_addr, offset);
11695796c8dcSSimon Schubert 	}
11705796c8dcSSimon Schubert       /* If an error occurred, print TLS related messages here.  Otherwise,
11715796c8dcSSimon Schubert          throw the error to some higher catcher.  */
11725796c8dcSSimon Schubert       if (ex.reason < 0)
11735796c8dcSSimon Schubert 	{
11745796c8dcSSimon Schubert 	  int objfile_is_library = (objfile->flags & OBJF_SHARED);
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert 	  switch (ex.error)
11775796c8dcSSimon Schubert 	    {
11785796c8dcSSimon Schubert 	    case TLS_NO_LIBRARY_SUPPORT_ERROR:
1179c50c785cSJohn Marino 	      error (_("Cannot find thread-local variables "
1180c50c785cSJohn Marino 		       "in this thread library."));
11815796c8dcSSimon Schubert 	      break;
11825796c8dcSSimon Schubert 	    case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
11835796c8dcSSimon Schubert 	      if (objfile_is_library)
11845796c8dcSSimon Schubert 		error (_("Cannot find shared library `%s' in dynamic"
11855796c8dcSSimon Schubert 		         " linker's load module list"), objfile->name);
11865796c8dcSSimon Schubert 	      else
11875796c8dcSSimon Schubert 		error (_("Cannot find executable file `%s' in dynamic"
11885796c8dcSSimon Schubert 		         " linker's load module list"), objfile->name);
11895796c8dcSSimon Schubert 	      break;
11905796c8dcSSimon Schubert 	    case TLS_NOT_ALLOCATED_YET_ERROR:
11915796c8dcSSimon Schubert 	      if (objfile_is_library)
11925796c8dcSSimon Schubert 		error (_("The inferior has not yet allocated storage for"
11935796c8dcSSimon Schubert 		         " thread-local variables in\n"
11945796c8dcSSimon Schubert 		         "the shared library `%s'\n"
11955796c8dcSSimon Schubert 		         "for %s"),
11965796c8dcSSimon Schubert 		       objfile->name, target_pid_to_str (ptid));
11975796c8dcSSimon Schubert 	      else
11985796c8dcSSimon Schubert 		error (_("The inferior has not yet allocated storage for"
11995796c8dcSSimon Schubert 		         " thread-local variables in\n"
12005796c8dcSSimon Schubert 		         "the executable `%s'\n"
12015796c8dcSSimon Schubert 		         "for %s"),
12025796c8dcSSimon Schubert 		       objfile->name, target_pid_to_str (ptid));
12035796c8dcSSimon Schubert 	      break;
12045796c8dcSSimon Schubert 	    case TLS_GENERIC_ERROR:
12055796c8dcSSimon Schubert 	      if (objfile_is_library)
12065796c8dcSSimon Schubert 		error (_("Cannot find thread-local storage for %s, "
12075796c8dcSSimon Schubert 		         "shared library %s:\n%s"),
12085796c8dcSSimon Schubert 		       target_pid_to_str (ptid),
12095796c8dcSSimon Schubert 		       objfile->name, ex.message);
12105796c8dcSSimon Schubert 	      else
12115796c8dcSSimon Schubert 		error (_("Cannot find thread-local storage for %s, "
12125796c8dcSSimon Schubert 		         "executable file %s:\n%s"),
12135796c8dcSSimon Schubert 		       target_pid_to_str (ptid),
12145796c8dcSSimon Schubert 		       objfile->name, ex.message);
12155796c8dcSSimon Schubert 	      break;
12165796c8dcSSimon Schubert 	    default:
12175796c8dcSSimon Schubert 	      throw_exception (ex);
12185796c8dcSSimon Schubert 	      break;
12195796c8dcSSimon Schubert 	    }
12205796c8dcSSimon Schubert 	}
12215796c8dcSSimon Schubert     }
12225796c8dcSSimon Schubert   /* It wouldn't be wrong here to try a gdbarch method, too; finding
12235796c8dcSSimon Schubert      TLS is an ABI-specific thing.  But we don't do that yet.  */
12245796c8dcSSimon Schubert   else
12255796c8dcSSimon Schubert     error (_("Cannot find thread-local variables on this target"));
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert   return addr;
12285796c8dcSSimon Schubert }
12295796c8dcSSimon Schubert 
12305796c8dcSSimon Schubert #undef	MIN
12315796c8dcSSimon Schubert #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
12325796c8dcSSimon Schubert 
12335796c8dcSSimon Schubert /* target_read_string -- read a null terminated string, up to LEN bytes,
12345796c8dcSSimon Schubert    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
12355796c8dcSSimon Schubert    Set *STRING to a pointer to malloc'd memory containing the data; the caller
12365796c8dcSSimon Schubert    is responsible for freeing it.  Return the number of bytes successfully
12375796c8dcSSimon Schubert    read.  */
12385796c8dcSSimon Schubert 
12395796c8dcSSimon Schubert int
target_read_string(CORE_ADDR memaddr,char ** string,int len,int * errnop)12405796c8dcSSimon Schubert target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
12415796c8dcSSimon Schubert {
1242*ef5ccd6cSJohn Marino   int tlen, offset, i;
12435796c8dcSSimon Schubert   gdb_byte buf[4];
12445796c8dcSSimon Schubert   int errcode = 0;
12455796c8dcSSimon Schubert   char *buffer;
12465796c8dcSSimon Schubert   int buffer_allocated;
12475796c8dcSSimon Schubert   char *bufptr;
12485796c8dcSSimon Schubert   unsigned int nbytes_read = 0;
12495796c8dcSSimon Schubert 
12505796c8dcSSimon Schubert   gdb_assert (string);
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert   /* Small for testing.  */
12535796c8dcSSimon Schubert   buffer_allocated = 4;
12545796c8dcSSimon Schubert   buffer = xmalloc (buffer_allocated);
12555796c8dcSSimon Schubert   bufptr = buffer;
12565796c8dcSSimon Schubert 
12575796c8dcSSimon Schubert   while (len > 0)
12585796c8dcSSimon Schubert     {
12595796c8dcSSimon Schubert       tlen = MIN (len, 4 - (memaddr & 3));
12605796c8dcSSimon Schubert       offset = memaddr & 3;
12615796c8dcSSimon Schubert 
12625796c8dcSSimon Schubert       errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
12635796c8dcSSimon Schubert       if (errcode != 0)
12645796c8dcSSimon Schubert 	{
12655796c8dcSSimon Schubert 	  /* The transfer request might have crossed the boundary to an
12665796c8dcSSimon Schubert 	     unallocated region of memory.  Retry the transfer, requesting
12675796c8dcSSimon Schubert 	     a single byte.  */
12685796c8dcSSimon Schubert 	  tlen = 1;
12695796c8dcSSimon Schubert 	  offset = 0;
12705796c8dcSSimon Schubert 	  errcode = target_read_memory (memaddr, buf, 1);
12715796c8dcSSimon Schubert 	  if (errcode != 0)
12725796c8dcSSimon Schubert 	    goto done;
12735796c8dcSSimon Schubert 	}
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert       if (bufptr - buffer + tlen > buffer_allocated)
12765796c8dcSSimon Schubert 	{
12775796c8dcSSimon Schubert 	  unsigned int bytes;
1278cf7f2e2dSJohn Marino 
12795796c8dcSSimon Schubert 	  bytes = bufptr - buffer;
12805796c8dcSSimon Schubert 	  buffer_allocated *= 2;
12815796c8dcSSimon Schubert 	  buffer = xrealloc (buffer, buffer_allocated);
12825796c8dcSSimon Schubert 	  bufptr = buffer + bytes;
12835796c8dcSSimon Schubert 	}
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert       for (i = 0; i < tlen; i++)
12865796c8dcSSimon Schubert 	{
12875796c8dcSSimon Schubert 	  *bufptr++ = buf[i + offset];
12885796c8dcSSimon Schubert 	  if (buf[i + offset] == '\000')
12895796c8dcSSimon Schubert 	    {
12905796c8dcSSimon Schubert 	      nbytes_read += i + 1;
12915796c8dcSSimon Schubert 	      goto done;
12925796c8dcSSimon Schubert 	    }
12935796c8dcSSimon Schubert 	}
12945796c8dcSSimon Schubert 
12955796c8dcSSimon Schubert       memaddr += tlen;
12965796c8dcSSimon Schubert       len -= tlen;
12975796c8dcSSimon Schubert       nbytes_read += tlen;
12985796c8dcSSimon Schubert     }
12995796c8dcSSimon Schubert done:
13005796c8dcSSimon Schubert   *string = buffer;
13015796c8dcSSimon Schubert   if (errnop != NULL)
13025796c8dcSSimon Schubert     *errnop = errcode;
13035796c8dcSSimon Schubert   return nbytes_read;
13045796c8dcSSimon Schubert }
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert struct target_section_table *
target_get_section_table(struct target_ops * target)13075796c8dcSSimon Schubert target_get_section_table (struct target_ops *target)
13085796c8dcSSimon Schubert {
13095796c8dcSSimon Schubert   struct target_ops *t;
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert   if (targetdebug)
13125796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
13135796c8dcSSimon Schubert 
13145796c8dcSSimon Schubert   for (t = target; t != NULL; t = t->beneath)
13155796c8dcSSimon Schubert     if (t->to_get_section_table != NULL)
13165796c8dcSSimon Schubert       return (*t->to_get_section_table) (t);
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert   return NULL;
13195796c8dcSSimon Schubert }
13205796c8dcSSimon Schubert 
13215796c8dcSSimon Schubert /* Find a section containing ADDR.  */
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert struct target_section *
target_section_by_addr(struct target_ops * target,CORE_ADDR addr)13245796c8dcSSimon Schubert target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
13255796c8dcSSimon Schubert {
13265796c8dcSSimon Schubert   struct target_section_table *table = target_get_section_table (target);
13275796c8dcSSimon Schubert   struct target_section *secp;
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   if (table == NULL)
13305796c8dcSSimon Schubert     return NULL;
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert   for (secp = table->sections; secp < table->sections_end; secp++)
13335796c8dcSSimon Schubert     {
13345796c8dcSSimon Schubert       if (addr >= secp->addr && addr < secp->endaddr)
13355796c8dcSSimon Schubert 	return secp;
13365796c8dcSSimon Schubert     }
13375796c8dcSSimon Schubert   return NULL;
13385796c8dcSSimon Schubert }
13395796c8dcSSimon Schubert 
1340c50c785cSJohn Marino /* Read memory from the live target, even if currently inspecting a
1341c50c785cSJohn Marino    traceframe.  The return is the same as that of target_read.  */
1342c50c785cSJohn Marino 
1343c50c785cSJohn Marino static LONGEST
target_read_live_memory(enum target_object object,ULONGEST memaddr,gdb_byte * myaddr,LONGEST len)1344c50c785cSJohn Marino target_read_live_memory (enum target_object object,
1345c50c785cSJohn Marino 			 ULONGEST memaddr, gdb_byte *myaddr, LONGEST len)
1346c50c785cSJohn Marino {
1347c50c785cSJohn Marino   int ret;
1348c50c785cSJohn Marino   struct cleanup *cleanup;
1349c50c785cSJohn Marino 
1350c50c785cSJohn Marino   /* Switch momentarily out of tfind mode so to access live memory.
1351c50c785cSJohn Marino      Note that this must not clear global state, such as the frame
1352c50c785cSJohn Marino      cache, which must still remain valid for the previous traceframe.
1353c50c785cSJohn Marino      We may be _building_ the frame cache at this point.  */
1354c50c785cSJohn Marino   cleanup = make_cleanup_restore_traceframe_number ();
1355c50c785cSJohn Marino   set_traceframe_number (-1);
1356c50c785cSJohn Marino 
1357c50c785cSJohn Marino   ret = target_read (current_target.beneath, object, NULL,
1358c50c785cSJohn Marino 		     myaddr, memaddr, len);
1359c50c785cSJohn Marino 
1360c50c785cSJohn Marino   do_cleanups (cleanup);
1361c50c785cSJohn Marino   return ret;
1362c50c785cSJohn Marino }
1363c50c785cSJohn Marino 
1364c50c785cSJohn Marino /* Using the set of read-only target sections of OPS, read live
1365c50c785cSJohn Marino    read-only memory.  Note that the actual reads start from the
1366c50c785cSJohn Marino    top-most target again.
1367c50c785cSJohn Marino 
1368c50c785cSJohn Marino    For interface/parameters/return description see target.h,
1369c50c785cSJohn Marino    to_xfer_partial.  */
1370c50c785cSJohn Marino 
1371c50c785cSJohn Marino static LONGEST
memory_xfer_live_readonly_partial(struct target_ops * ops,enum target_object object,gdb_byte * readbuf,ULONGEST memaddr,LONGEST len)1372c50c785cSJohn Marino memory_xfer_live_readonly_partial (struct target_ops *ops,
1373c50c785cSJohn Marino 				   enum target_object object,
1374c50c785cSJohn Marino 				   gdb_byte *readbuf, ULONGEST memaddr,
1375c50c785cSJohn Marino 				   LONGEST len)
1376c50c785cSJohn Marino {
1377c50c785cSJohn Marino   struct target_section *secp;
1378c50c785cSJohn Marino   struct target_section_table *table;
1379c50c785cSJohn Marino 
1380c50c785cSJohn Marino   secp = target_section_by_addr (ops, memaddr);
1381c50c785cSJohn Marino   if (secp != NULL
1382c50c785cSJohn Marino       && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1383c50c785cSJohn Marino 	  & SEC_READONLY))
1384c50c785cSJohn Marino     {
1385c50c785cSJohn Marino       struct target_section *p;
1386c50c785cSJohn Marino       ULONGEST memend = memaddr + len;
1387c50c785cSJohn Marino 
1388c50c785cSJohn Marino       table = target_get_section_table (ops);
1389c50c785cSJohn Marino 
1390c50c785cSJohn Marino       for (p = table->sections; p < table->sections_end; p++)
1391c50c785cSJohn Marino 	{
1392c50c785cSJohn Marino 	  if (memaddr >= p->addr)
1393c50c785cSJohn Marino 	    {
1394c50c785cSJohn Marino 	      if (memend <= p->endaddr)
1395c50c785cSJohn Marino 		{
1396c50c785cSJohn Marino 		  /* Entire transfer is within this section.  */
1397c50c785cSJohn Marino 		  return target_read_live_memory (object, memaddr,
1398c50c785cSJohn Marino 						  readbuf, len);
1399c50c785cSJohn Marino 		}
1400c50c785cSJohn Marino 	      else if (memaddr >= p->endaddr)
1401c50c785cSJohn Marino 		{
1402c50c785cSJohn Marino 		  /* This section ends before the transfer starts.  */
1403c50c785cSJohn Marino 		  continue;
1404c50c785cSJohn Marino 		}
1405c50c785cSJohn Marino 	      else
1406c50c785cSJohn Marino 		{
1407c50c785cSJohn Marino 		  /* This section overlaps the transfer.  Just do half.  */
1408c50c785cSJohn Marino 		  len = p->endaddr - memaddr;
1409c50c785cSJohn Marino 		  return target_read_live_memory (object, memaddr,
1410c50c785cSJohn Marino 						  readbuf, len);
1411c50c785cSJohn Marino 		}
1412c50c785cSJohn Marino 	    }
1413c50c785cSJohn Marino 	}
1414c50c785cSJohn Marino     }
1415c50c785cSJohn Marino 
1416c50c785cSJohn Marino   return 0;
1417c50c785cSJohn Marino }
1418c50c785cSJohn Marino 
1419cf7f2e2dSJohn Marino /* Perform a partial memory transfer.
1420cf7f2e2dSJohn Marino    For docs see target.h, to_xfer_partial.  */
14215796c8dcSSimon Schubert 
14225796c8dcSSimon Schubert static LONGEST
memory_xfer_partial_1(struct target_ops * ops,enum target_object object,void * readbuf,const void * writebuf,ULONGEST memaddr,LONGEST len)1423a45ae5f8SJohn Marino memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
14245796c8dcSSimon Schubert 		       void *readbuf, const void *writebuf, ULONGEST memaddr,
14255796c8dcSSimon Schubert 		       LONGEST len)
14265796c8dcSSimon Schubert {
14275796c8dcSSimon Schubert   LONGEST res;
14285796c8dcSSimon Schubert   int reg_len;
14295796c8dcSSimon Schubert   struct mem_region *region;
14305796c8dcSSimon Schubert   struct inferior *inf;
14315796c8dcSSimon Schubert 
14325796c8dcSSimon Schubert   /* For accesses to unmapped overlay sections, read directly from
14335796c8dcSSimon Schubert      files.  Must do this first, as MEMADDR may need adjustment.  */
14345796c8dcSSimon Schubert   if (readbuf != NULL && overlay_debugging)
14355796c8dcSSimon Schubert     {
14365796c8dcSSimon Schubert       struct obj_section *section = find_pc_overlay (memaddr);
1437cf7f2e2dSJohn Marino 
14385796c8dcSSimon Schubert       if (pc_in_unmapped_range (memaddr, section))
14395796c8dcSSimon Schubert 	{
14405796c8dcSSimon Schubert 	  struct target_section_table *table
14415796c8dcSSimon Schubert 	    = target_get_section_table (ops);
14425796c8dcSSimon Schubert 	  const char *section_name = section->the_bfd_section->name;
1443cf7f2e2dSJohn Marino 
14445796c8dcSSimon Schubert 	  memaddr = overlay_mapped_address (memaddr, section);
14455796c8dcSSimon Schubert 	  return section_table_xfer_memory_partial (readbuf, writebuf,
14465796c8dcSSimon Schubert 						    memaddr, len,
14475796c8dcSSimon Schubert 						    table->sections,
14485796c8dcSSimon Schubert 						    table->sections_end,
14495796c8dcSSimon Schubert 						    section_name);
14505796c8dcSSimon Schubert 	}
14515796c8dcSSimon Schubert     }
14525796c8dcSSimon Schubert 
14535796c8dcSSimon Schubert   /* Try the executable files, if "trust-readonly-sections" is set.  */
14545796c8dcSSimon Schubert   if (readbuf != NULL && trust_readonly)
14555796c8dcSSimon Schubert     {
14565796c8dcSSimon Schubert       struct target_section *secp;
14575796c8dcSSimon Schubert       struct target_section_table *table;
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert       secp = target_section_by_addr (ops, memaddr);
14605796c8dcSSimon Schubert       if (secp != NULL
14615796c8dcSSimon Schubert 	  && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
14625796c8dcSSimon Schubert 	      & SEC_READONLY))
14635796c8dcSSimon Schubert 	{
14645796c8dcSSimon Schubert 	  table = target_get_section_table (ops);
14655796c8dcSSimon Schubert 	  return section_table_xfer_memory_partial (readbuf, writebuf,
14665796c8dcSSimon Schubert 						    memaddr, len,
14675796c8dcSSimon Schubert 						    table->sections,
14685796c8dcSSimon Schubert 						    table->sections_end,
14695796c8dcSSimon Schubert 						    NULL);
14705796c8dcSSimon Schubert 	}
14715796c8dcSSimon Schubert     }
14725796c8dcSSimon Schubert 
1473c50c785cSJohn Marino   /* If reading unavailable memory in the context of traceframes, and
1474c50c785cSJohn Marino      this address falls within a read-only section, fallback to
1475c50c785cSJohn Marino      reading from live memory.  */
1476c50c785cSJohn Marino   if (readbuf != NULL && get_traceframe_number () != -1)
1477c50c785cSJohn Marino     {
1478c50c785cSJohn Marino       VEC(mem_range_s) *available;
1479c50c785cSJohn Marino 
1480c50c785cSJohn Marino       /* If we fail to get the set of available memory, then the
1481c50c785cSJohn Marino 	 target does not support querying traceframe info, and so we
1482c50c785cSJohn Marino 	 attempt reading from the traceframe anyway (assuming the
1483c50c785cSJohn Marino 	 target implements the old QTro packet then).  */
1484c50c785cSJohn Marino       if (traceframe_available_memory (&available, memaddr, len))
1485c50c785cSJohn Marino 	{
1486c50c785cSJohn Marino 	  struct cleanup *old_chain;
1487c50c785cSJohn Marino 
1488c50c785cSJohn Marino 	  old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
1489c50c785cSJohn Marino 
1490c50c785cSJohn Marino 	  if (VEC_empty (mem_range_s, available)
1491c50c785cSJohn Marino 	      || VEC_index (mem_range_s, available, 0)->start != memaddr)
1492c50c785cSJohn Marino 	    {
1493c50c785cSJohn Marino 	      /* Don't read into the traceframe's available
1494c50c785cSJohn Marino 		 memory.  */
1495c50c785cSJohn Marino 	      if (!VEC_empty (mem_range_s, available))
1496c50c785cSJohn Marino 		{
1497c50c785cSJohn Marino 		  LONGEST oldlen = len;
1498c50c785cSJohn Marino 
1499c50c785cSJohn Marino 		  len = VEC_index (mem_range_s, available, 0)->start - memaddr;
1500c50c785cSJohn Marino 		  gdb_assert (len <= oldlen);
1501c50c785cSJohn Marino 		}
1502c50c785cSJohn Marino 
1503c50c785cSJohn Marino 	      do_cleanups (old_chain);
1504c50c785cSJohn Marino 
1505c50c785cSJohn Marino 	      /* This goes through the topmost target again.  */
1506c50c785cSJohn Marino 	      res = memory_xfer_live_readonly_partial (ops, object,
1507c50c785cSJohn Marino 						       readbuf, memaddr, len);
1508c50c785cSJohn Marino 	      if (res > 0)
1509c50c785cSJohn Marino 		return res;
1510c50c785cSJohn Marino 
1511c50c785cSJohn Marino 	      /* No use trying further, we know some memory starting
1512c50c785cSJohn Marino 		 at MEMADDR isn't available.  */
1513c50c785cSJohn Marino 	      return -1;
1514c50c785cSJohn Marino 	    }
1515c50c785cSJohn Marino 
1516c50c785cSJohn Marino 	  /* Don't try to read more than how much is available, in
1517c50c785cSJohn Marino 	     case the target implements the deprecated QTro packet to
1518c50c785cSJohn Marino 	     cater for older GDBs (the target's knowledge of read-only
1519c50c785cSJohn Marino 	     sections may be outdated by now).  */
1520c50c785cSJohn Marino 	  len = VEC_index (mem_range_s, available, 0)->length;
1521c50c785cSJohn Marino 
1522c50c785cSJohn Marino 	  do_cleanups (old_chain);
1523c50c785cSJohn Marino 	}
1524c50c785cSJohn Marino     }
1525c50c785cSJohn Marino 
15265796c8dcSSimon Schubert   /* Try GDB's internal data cache.  */
15275796c8dcSSimon Schubert   region = lookup_mem_region (memaddr);
15285796c8dcSSimon Schubert   /* region->hi == 0 means there's no upper bound.  */
15295796c8dcSSimon Schubert   if (memaddr + len < region->hi || region->hi == 0)
15305796c8dcSSimon Schubert     reg_len = len;
15315796c8dcSSimon Schubert   else
15325796c8dcSSimon Schubert     reg_len = region->hi - memaddr;
15335796c8dcSSimon Schubert 
15345796c8dcSSimon Schubert   switch (region->attrib.mode)
15355796c8dcSSimon Schubert     {
15365796c8dcSSimon Schubert     case MEM_RO:
15375796c8dcSSimon Schubert       if (writebuf != NULL)
15385796c8dcSSimon Schubert 	return -1;
15395796c8dcSSimon Schubert       break;
15405796c8dcSSimon Schubert 
15415796c8dcSSimon Schubert     case MEM_WO:
15425796c8dcSSimon Schubert       if (readbuf != NULL)
15435796c8dcSSimon Schubert 	return -1;
15445796c8dcSSimon Schubert       break;
15455796c8dcSSimon Schubert 
15465796c8dcSSimon Schubert     case MEM_FLASH:
15475796c8dcSSimon Schubert       /* We only support writing to flash during "load" for now.  */
15485796c8dcSSimon Schubert       if (writebuf != NULL)
15495796c8dcSSimon Schubert 	error (_("Writing to flash memory forbidden in this context"));
15505796c8dcSSimon Schubert       break;
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert     case MEM_NONE:
15535796c8dcSSimon Schubert       return -1;
15545796c8dcSSimon Schubert     }
15555796c8dcSSimon Schubert 
1556cf7f2e2dSJohn Marino   if (!ptid_equal (inferior_ptid, null_ptid))
15575796c8dcSSimon Schubert     inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1558cf7f2e2dSJohn Marino   else
1559cf7f2e2dSJohn Marino     inf = NULL;
15605796c8dcSSimon Schubert 
15615796c8dcSSimon Schubert   if (inf != NULL
1562cf7f2e2dSJohn Marino       /* The dcache reads whole cache lines; that doesn't play well
1563cf7f2e2dSJohn Marino 	 with reading from a trace buffer, because reading outside of
1564cf7f2e2dSJohn Marino 	 the collected memory range fails.  */
1565cf7f2e2dSJohn Marino       && get_traceframe_number () == -1
15665796c8dcSSimon Schubert       && (region->attrib.cache
15675796c8dcSSimon Schubert 	  || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
15685796c8dcSSimon Schubert     {
15695796c8dcSSimon Schubert       if (readbuf != NULL)
15705796c8dcSSimon Schubert 	res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
15715796c8dcSSimon Schubert 				  reg_len, 0);
15725796c8dcSSimon Schubert       else
15735796c8dcSSimon Schubert 	/* FIXME drow/2006-08-09: If we're going to preserve const
15745796c8dcSSimon Schubert 	   correctness dcache_xfer_memory should take readbuf and
15755796c8dcSSimon Schubert 	   writebuf.  */
15765796c8dcSSimon Schubert 	res = dcache_xfer_memory (ops, target_dcache, memaddr,
15775796c8dcSSimon Schubert 				  (void *) writebuf,
15785796c8dcSSimon Schubert 				  reg_len, 1);
15795796c8dcSSimon Schubert       if (res <= 0)
15805796c8dcSSimon Schubert 	return -1;
15815796c8dcSSimon Schubert       else
15825796c8dcSSimon Schubert 	return res;
15835796c8dcSSimon Schubert     }
15845796c8dcSSimon Schubert 
15855796c8dcSSimon Schubert   /* If none of those methods found the memory we wanted, fall back
15865796c8dcSSimon Schubert      to a target partial transfer.  Normally a single call to
15875796c8dcSSimon Schubert      to_xfer_partial is enough; if it doesn't recognize an object
15885796c8dcSSimon Schubert      it will call the to_xfer_partial of the next target down.
15895796c8dcSSimon Schubert      But for memory this won't do.  Memory is the only target
15905796c8dcSSimon Schubert      object which can be read from more than one valid target.
15915796c8dcSSimon Schubert      A core file, for instance, could have some of memory but
15925796c8dcSSimon Schubert      delegate other bits to the target below it.  So, we must
15935796c8dcSSimon Schubert      manually try all targets.  */
15945796c8dcSSimon Schubert 
15955796c8dcSSimon Schubert   do
15965796c8dcSSimon Schubert     {
15975796c8dcSSimon Schubert       res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
15985796c8dcSSimon Schubert 				  readbuf, writebuf, memaddr, reg_len);
15995796c8dcSSimon Schubert       if (res > 0)
16005796c8dcSSimon Schubert 	break;
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert       /* We want to continue past core files to executables, but not
16035796c8dcSSimon Schubert 	 past a running target's memory.  */
16045796c8dcSSimon Schubert       if (ops->to_has_all_memory (ops))
16055796c8dcSSimon Schubert 	break;
16065796c8dcSSimon Schubert 
16075796c8dcSSimon Schubert       ops = ops->beneath;
16085796c8dcSSimon Schubert     }
16095796c8dcSSimon Schubert   while (ops != NULL);
16105796c8dcSSimon Schubert 
16115796c8dcSSimon Schubert   /* Make sure the cache gets updated no matter what - if we are writing
16125796c8dcSSimon Schubert      to the stack.  Even if this write is not tagged as such, we still need
16135796c8dcSSimon Schubert      to update the cache.  */
16145796c8dcSSimon Schubert 
16155796c8dcSSimon Schubert   if (res > 0
16165796c8dcSSimon Schubert       && inf != NULL
16175796c8dcSSimon Schubert       && writebuf != NULL
16185796c8dcSSimon Schubert       && !region->attrib.cache
16195796c8dcSSimon Schubert       && stack_cache_enabled_p
16205796c8dcSSimon Schubert       && object != TARGET_OBJECT_STACK_MEMORY)
16215796c8dcSSimon Schubert     {
16225796c8dcSSimon Schubert       dcache_update (target_dcache, memaddr, (void *) writebuf, res);
16235796c8dcSSimon Schubert     }
16245796c8dcSSimon Schubert 
16255796c8dcSSimon Schubert   /* If we still haven't got anything, return the last error.  We
16265796c8dcSSimon Schubert      give up.  */
16275796c8dcSSimon Schubert   return res;
16285796c8dcSSimon Schubert }
16295796c8dcSSimon Schubert 
1630a45ae5f8SJohn Marino /* Perform a partial memory transfer.  For docs see target.h,
1631a45ae5f8SJohn Marino    to_xfer_partial.  */
1632a45ae5f8SJohn Marino 
1633a45ae5f8SJohn Marino static LONGEST
memory_xfer_partial(struct target_ops * ops,enum target_object object,void * readbuf,const void * writebuf,ULONGEST memaddr,LONGEST len)1634a45ae5f8SJohn Marino memory_xfer_partial (struct target_ops *ops, enum target_object object,
1635a45ae5f8SJohn Marino 		     void *readbuf, const void *writebuf, ULONGEST memaddr,
1636a45ae5f8SJohn Marino 		     LONGEST len)
1637a45ae5f8SJohn Marino {
1638a45ae5f8SJohn Marino   int res;
1639a45ae5f8SJohn Marino 
1640a45ae5f8SJohn Marino   /* Zero length requests are ok and require no work.  */
1641a45ae5f8SJohn Marino   if (len == 0)
1642a45ae5f8SJohn Marino     return 0;
1643a45ae5f8SJohn Marino 
1644a45ae5f8SJohn Marino   /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1645a45ae5f8SJohn Marino      breakpoint insns, thus hiding out from higher layers whether
1646a45ae5f8SJohn Marino      there are software breakpoints inserted in the code stream.  */
1647a45ae5f8SJohn Marino   if (readbuf != NULL)
1648a45ae5f8SJohn Marino     {
1649a45ae5f8SJohn Marino       res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len);
1650a45ae5f8SJohn Marino 
1651a45ae5f8SJohn Marino       if (res > 0 && !show_memory_breakpoints)
1652a45ae5f8SJohn Marino 	breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, res);
1653a45ae5f8SJohn Marino     }
1654a45ae5f8SJohn Marino   else
1655a45ae5f8SJohn Marino     {
1656a45ae5f8SJohn Marino       void *buf;
1657a45ae5f8SJohn Marino       struct cleanup *old_chain;
1658a45ae5f8SJohn Marino 
1659a45ae5f8SJohn Marino       buf = xmalloc (len);
1660a45ae5f8SJohn Marino       old_chain = make_cleanup (xfree, buf);
1661a45ae5f8SJohn Marino       memcpy (buf, writebuf, len);
1662a45ae5f8SJohn Marino 
1663a45ae5f8SJohn Marino       breakpoint_xfer_memory (NULL, buf, writebuf, memaddr, len);
1664a45ae5f8SJohn Marino       res = memory_xfer_partial_1 (ops, object, NULL, buf, memaddr, len);
1665a45ae5f8SJohn Marino 
1666a45ae5f8SJohn Marino       do_cleanups (old_chain);
1667a45ae5f8SJohn Marino     }
1668a45ae5f8SJohn Marino 
1669a45ae5f8SJohn Marino   return res;
1670a45ae5f8SJohn Marino }
1671a45ae5f8SJohn Marino 
16725796c8dcSSimon Schubert static void
restore_show_memory_breakpoints(void * arg)16735796c8dcSSimon Schubert restore_show_memory_breakpoints (void *arg)
16745796c8dcSSimon Schubert {
16755796c8dcSSimon Schubert   show_memory_breakpoints = (uintptr_t) arg;
16765796c8dcSSimon Schubert }
16775796c8dcSSimon Schubert 
16785796c8dcSSimon Schubert struct cleanup *
make_show_memory_breakpoints_cleanup(int show)16795796c8dcSSimon Schubert make_show_memory_breakpoints_cleanup (int show)
16805796c8dcSSimon Schubert {
16815796c8dcSSimon Schubert   int current = show_memory_breakpoints;
16825796c8dcSSimon Schubert 
1683cf7f2e2dSJohn Marino   show_memory_breakpoints = show;
16845796c8dcSSimon Schubert   return make_cleanup (restore_show_memory_breakpoints,
16855796c8dcSSimon Schubert 		       (void *) (uintptr_t) current);
16865796c8dcSSimon Schubert }
16875796c8dcSSimon Schubert 
1688cf7f2e2dSJohn Marino /* For docs see target.h, to_xfer_partial.  */
1689cf7f2e2dSJohn Marino 
16905796c8dcSSimon Schubert static LONGEST
target_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,void * readbuf,const void * writebuf,ULONGEST offset,LONGEST len)16915796c8dcSSimon Schubert target_xfer_partial (struct target_ops *ops,
16925796c8dcSSimon Schubert 		     enum target_object object, const char *annex,
16935796c8dcSSimon Schubert 		     void *readbuf, const void *writebuf,
16945796c8dcSSimon Schubert 		     ULONGEST offset, LONGEST len)
16955796c8dcSSimon Schubert {
16965796c8dcSSimon Schubert   LONGEST retval;
16975796c8dcSSimon Schubert 
16985796c8dcSSimon Schubert   gdb_assert (ops->to_xfer_partial != NULL);
16995796c8dcSSimon Schubert 
1700cf7f2e2dSJohn Marino   if (writebuf && !may_write_memory)
1701cf7f2e2dSJohn Marino     error (_("Writing to memory is not allowed (addr %s, len %s)"),
1702cf7f2e2dSJohn Marino 	   core_addr_to_string_nz (offset), plongest (len));
1703cf7f2e2dSJohn Marino 
17045796c8dcSSimon Schubert   /* If this is a memory transfer, let the memory-specific code
17055796c8dcSSimon Schubert      have a look at it instead.  Memory transfers are more
17065796c8dcSSimon Schubert      complicated.  */
17075796c8dcSSimon Schubert   if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
17085796c8dcSSimon Schubert     retval = memory_xfer_partial (ops, object, readbuf,
17095796c8dcSSimon Schubert 				  writebuf, offset, len);
17105796c8dcSSimon Schubert   else
17115796c8dcSSimon Schubert     {
17125796c8dcSSimon Schubert       enum target_object raw_object = object;
17135796c8dcSSimon Schubert 
17145796c8dcSSimon Schubert       /* If this is a raw memory transfer, request the normal
17155796c8dcSSimon Schubert 	 memory object from other layers.  */
17165796c8dcSSimon Schubert       if (raw_object == TARGET_OBJECT_RAW_MEMORY)
17175796c8dcSSimon Schubert 	raw_object = TARGET_OBJECT_MEMORY;
17185796c8dcSSimon Schubert 
17195796c8dcSSimon Schubert       retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
17205796c8dcSSimon Schubert 				     writebuf, offset, len);
17215796c8dcSSimon Schubert     }
17225796c8dcSSimon Schubert 
17235796c8dcSSimon Schubert   if (targetdebug)
17245796c8dcSSimon Schubert     {
17255796c8dcSSimon Schubert       const unsigned char *myaddr = NULL;
17265796c8dcSSimon Schubert 
17275796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog,
1728c50c785cSJohn Marino 			  "%s:target_xfer_partial "
1729c50c785cSJohn Marino 			  "(%d, %s, %s, %s, %s, %s) = %s",
17305796c8dcSSimon Schubert 			  ops->to_shortname,
17315796c8dcSSimon Schubert 			  (int) object,
17325796c8dcSSimon Schubert 			  (annex ? annex : "(null)"),
17335796c8dcSSimon Schubert 			  host_address_to_string (readbuf),
17345796c8dcSSimon Schubert 			  host_address_to_string (writebuf),
17355796c8dcSSimon Schubert 			  core_addr_to_string_nz (offset),
17365796c8dcSSimon Schubert 			  plongest (len), plongest (retval));
17375796c8dcSSimon Schubert 
17385796c8dcSSimon Schubert       if (readbuf)
17395796c8dcSSimon Schubert 	myaddr = readbuf;
17405796c8dcSSimon Schubert       if (writebuf)
17415796c8dcSSimon Schubert 	myaddr = writebuf;
17425796c8dcSSimon Schubert       if (retval > 0 && myaddr != NULL)
17435796c8dcSSimon Schubert 	{
17445796c8dcSSimon Schubert 	  int i;
17455796c8dcSSimon Schubert 
17465796c8dcSSimon Schubert 	  fputs_unfiltered (", bytes =", gdb_stdlog);
17475796c8dcSSimon Schubert 	  for (i = 0; i < retval; i++)
17485796c8dcSSimon Schubert 	    {
17495796c8dcSSimon Schubert 	      if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
17505796c8dcSSimon Schubert 		{
17515796c8dcSSimon Schubert 		  if (targetdebug < 2 && i > 0)
17525796c8dcSSimon Schubert 		    {
17535796c8dcSSimon Schubert 		      fprintf_unfiltered (gdb_stdlog, " ...");
17545796c8dcSSimon Schubert 		      break;
17555796c8dcSSimon Schubert 		    }
17565796c8dcSSimon Schubert 		  fprintf_unfiltered (gdb_stdlog, "\n");
17575796c8dcSSimon Schubert 		}
17585796c8dcSSimon Schubert 
17595796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
17605796c8dcSSimon Schubert 	    }
17615796c8dcSSimon Schubert 	}
17625796c8dcSSimon Schubert 
17635796c8dcSSimon Schubert       fputc_unfiltered ('\n', gdb_stdlog);
17645796c8dcSSimon Schubert     }
17655796c8dcSSimon Schubert   return retval;
17665796c8dcSSimon Schubert }
17675796c8dcSSimon Schubert 
17685796c8dcSSimon Schubert /* Read LEN bytes of target memory at address MEMADDR, placing the results in
17695796c8dcSSimon Schubert    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
17705796c8dcSSimon Schubert    if any error occurs.
17715796c8dcSSimon Schubert 
17725796c8dcSSimon Schubert    If an error occurs, no guarantee is made about the contents of the data at
17735796c8dcSSimon Schubert    MYADDR.  In particular, the caller should not depend upon partial reads
17745796c8dcSSimon Schubert    filling the buffer with good data.  There is no way for the caller to know
17755796c8dcSSimon Schubert    how much good data might have been transfered anyway.  Callers that can
17765796c8dcSSimon Schubert    deal with partial reads should call target_read (which will retry until
17775796c8dcSSimon Schubert    it makes no progress, and then return how much was transferred).  */
17785796c8dcSSimon Schubert 
17795796c8dcSSimon Schubert int
target_read_memory(CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)1780*ef5ccd6cSJohn Marino target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
17815796c8dcSSimon Schubert {
17825796c8dcSSimon Schubert   /* Dispatch to the topmost target, not the flattened current_target.
17835796c8dcSSimon Schubert      Memory accesses check target->to_has_(all_)memory, and the
17845796c8dcSSimon Schubert      flattened target doesn't inherit those.  */
17855796c8dcSSimon Schubert   if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
17865796c8dcSSimon Schubert 		   myaddr, memaddr, len) == len)
17875796c8dcSSimon Schubert     return 0;
17885796c8dcSSimon Schubert   else
17895796c8dcSSimon Schubert     return EIO;
17905796c8dcSSimon Schubert }
17915796c8dcSSimon Schubert 
17925796c8dcSSimon Schubert /* Like target_read_memory, but specify explicitly that this is a read from
17935796c8dcSSimon Schubert    the target's stack.  This may trigger different cache behavior.  */
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert int
target_read_stack(CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)1796*ef5ccd6cSJohn Marino target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
17975796c8dcSSimon Schubert {
17985796c8dcSSimon Schubert   /* Dispatch to the topmost target, not the flattened current_target.
17995796c8dcSSimon Schubert      Memory accesses check target->to_has_(all_)memory, and the
18005796c8dcSSimon Schubert      flattened target doesn't inherit those.  */
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert   if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
18035796c8dcSSimon Schubert 		   myaddr, memaddr, len) == len)
18045796c8dcSSimon Schubert     return 0;
18055796c8dcSSimon Schubert   else
18065796c8dcSSimon Schubert     return EIO;
18075796c8dcSSimon Schubert }
18085796c8dcSSimon Schubert 
1809cf7f2e2dSJohn Marino /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1810cf7f2e2dSJohn Marino    Returns either 0 for success or an errno value if any error occurs.
1811cf7f2e2dSJohn Marino    If an error occurs, no guarantee is made about how much data got written.
1812cf7f2e2dSJohn Marino    Callers that can deal with partial writes should call target_write.  */
1813cf7f2e2dSJohn Marino 
18145796c8dcSSimon Schubert int
target_write_memory(CORE_ADDR memaddr,const gdb_byte * myaddr,ssize_t len)1815*ef5ccd6cSJohn Marino target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
18165796c8dcSSimon Schubert {
18175796c8dcSSimon Schubert   /* Dispatch to the topmost target, not the flattened current_target.
18185796c8dcSSimon Schubert      Memory accesses check target->to_has_(all_)memory, and the
18195796c8dcSSimon Schubert      flattened target doesn't inherit those.  */
18205796c8dcSSimon Schubert   if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
18215796c8dcSSimon Schubert 		    myaddr, memaddr, len) == len)
18225796c8dcSSimon Schubert     return 0;
18235796c8dcSSimon Schubert   else
18245796c8dcSSimon Schubert     return EIO;
18255796c8dcSSimon Schubert }
18265796c8dcSSimon Schubert 
1827a45ae5f8SJohn Marino /* Write LEN bytes from MYADDR to target raw memory at address
1828a45ae5f8SJohn Marino    MEMADDR.  Returns either 0 for success or an errno value if any
1829a45ae5f8SJohn Marino    error occurs.  If an error occurs, no guarantee is made about how
1830a45ae5f8SJohn Marino    much data got written.  Callers that can deal with partial writes
1831a45ae5f8SJohn Marino    should call target_write.  */
1832a45ae5f8SJohn Marino 
1833a45ae5f8SJohn Marino int
target_write_raw_memory(CORE_ADDR memaddr,const gdb_byte * myaddr,ssize_t len)1834*ef5ccd6cSJohn Marino target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
1835a45ae5f8SJohn Marino {
1836a45ae5f8SJohn Marino   /* Dispatch to the topmost target, not the flattened current_target.
1837a45ae5f8SJohn Marino      Memory accesses check target->to_has_(all_)memory, and the
1838a45ae5f8SJohn Marino      flattened target doesn't inherit those.  */
1839a45ae5f8SJohn Marino   if (target_write (current_target.beneath, TARGET_OBJECT_RAW_MEMORY, NULL,
1840a45ae5f8SJohn Marino 		    myaddr, memaddr, len) == len)
1841a45ae5f8SJohn Marino     return 0;
1842a45ae5f8SJohn Marino   else
1843a45ae5f8SJohn Marino     return EIO;
1844a45ae5f8SJohn Marino }
1845a45ae5f8SJohn Marino 
18465796c8dcSSimon Schubert /* Fetch the target's memory map.  */
18475796c8dcSSimon Schubert 
VEC(mem_region_s)18485796c8dcSSimon Schubert VEC(mem_region_s) *
18495796c8dcSSimon Schubert target_memory_map (void)
18505796c8dcSSimon Schubert {
18515796c8dcSSimon Schubert   VEC(mem_region_s) *result;
18525796c8dcSSimon Schubert   struct mem_region *last_one, *this_one;
18535796c8dcSSimon Schubert   int ix;
18545796c8dcSSimon Schubert   struct target_ops *t;
18555796c8dcSSimon Schubert 
18565796c8dcSSimon Schubert   if (targetdebug)
18575796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
18605796c8dcSSimon Schubert     if (t->to_memory_map != NULL)
18615796c8dcSSimon Schubert       break;
18625796c8dcSSimon Schubert 
18635796c8dcSSimon Schubert   if (t == NULL)
18645796c8dcSSimon Schubert     return NULL;
18655796c8dcSSimon Schubert 
18665796c8dcSSimon Schubert   result = t->to_memory_map (t);
18675796c8dcSSimon Schubert   if (result == NULL)
18685796c8dcSSimon Schubert     return NULL;
18695796c8dcSSimon Schubert 
18705796c8dcSSimon Schubert   qsort (VEC_address (mem_region_s, result),
18715796c8dcSSimon Schubert 	 VEC_length (mem_region_s, result),
18725796c8dcSSimon Schubert 	 sizeof (struct mem_region), mem_region_cmp);
18735796c8dcSSimon Schubert 
18745796c8dcSSimon Schubert   /* Check that regions do not overlap.  Simultaneously assign
18755796c8dcSSimon Schubert      a numbering for the "mem" commands to use to refer to
18765796c8dcSSimon Schubert      each region.  */
18775796c8dcSSimon Schubert   last_one = NULL;
18785796c8dcSSimon Schubert   for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
18795796c8dcSSimon Schubert     {
18805796c8dcSSimon Schubert       this_one->number = ix;
18815796c8dcSSimon Schubert 
18825796c8dcSSimon Schubert       if (last_one && last_one->hi > this_one->lo)
18835796c8dcSSimon Schubert 	{
18845796c8dcSSimon Schubert 	  warning (_("Overlapping regions in memory map: ignoring"));
18855796c8dcSSimon Schubert 	  VEC_free (mem_region_s, result);
18865796c8dcSSimon Schubert 	  return NULL;
18875796c8dcSSimon Schubert 	}
18885796c8dcSSimon Schubert       last_one = this_one;
18895796c8dcSSimon Schubert     }
18905796c8dcSSimon Schubert 
18915796c8dcSSimon Schubert   return result;
18925796c8dcSSimon Schubert }
18935796c8dcSSimon Schubert 
18945796c8dcSSimon Schubert void
target_flash_erase(ULONGEST address,LONGEST length)18955796c8dcSSimon Schubert target_flash_erase (ULONGEST address, LONGEST length)
18965796c8dcSSimon Schubert {
18975796c8dcSSimon Schubert   struct target_ops *t;
18985796c8dcSSimon Schubert 
18995796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
19005796c8dcSSimon Schubert     if (t->to_flash_erase != NULL)
19015796c8dcSSimon Schubert       {
19025796c8dcSSimon Schubert 	if (targetdebug)
19035796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
19045796c8dcSSimon Schubert 			      hex_string (address), phex (length, 0));
19055796c8dcSSimon Schubert 	t->to_flash_erase (t, address, length);
19065796c8dcSSimon Schubert 	return;
19075796c8dcSSimon Schubert       }
19085796c8dcSSimon Schubert 
19095796c8dcSSimon Schubert   tcomplain ();
19105796c8dcSSimon Schubert }
19115796c8dcSSimon Schubert 
19125796c8dcSSimon Schubert void
target_flash_done(void)19135796c8dcSSimon Schubert target_flash_done (void)
19145796c8dcSSimon Schubert {
19155796c8dcSSimon Schubert   struct target_ops *t;
19165796c8dcSSimon Schubert 
19175796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
19185796c8dcSSimon Schubert     if (t->to_flash_done != NULL)
19195796c8dcSSimon Schubert       {
19205796c8dcSSimon Schubert 	if (targetdebug)
19215796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
19225796c8dcSSimon Schubert 	t->to_flash_done (t);
19235796c8dcSSimon Schubert 	return;
19245796c8dcSSimon Schubert       }
19255796c8dcSSimon Schubert 
19265796c8dcSSimon Schubert   tcomplain ();
19275796c8dcSSimon Schubert }
19285796c8dcSSimon Schubert 
19295796c8dcSSimon Schubert static void
show_trust_readonly(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)19305796c8dcSSimon Schubert show_trust_readonly (struct ui_file *file, int from_tty,
19315796c8dcSSimon Schubert 		     struct cmd_list_element *c, const char *value)
19325796c8dcSSimon Schubert {
1933c50c785cSJohn Marino   fprintf_filtered (file,
1934c50c785cSJohn Marino 		    _("Mode for reading from readonly sections is %s.\n"),
19355796c8dcSSimon Schubert 		    value);
19365796c8dcSSimon Schubert }
19375796c8dcSSimon Schubert 
19385796c8dcSSimon Schubert /* More generic transfers.  */
19395796c8dcSSimon Schubert 
19405796c8dcSSimon Schubert static LONGEST
default_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)19415796c8dcSSimon Schubert default_xfer_partial (struct target_ops *ops, enum target_object object,
19425796c8dcSSimon Schubert 		      const char *annex, gdb_byte *readbuf,
19435796c8dcSSimon Schubert 		      const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
19445796c8dcSSimon Schubert {
19455796c8dcSSimon Schubert   if (object == TARGET_OBJECT_MEMORY
19465796c8dcSSimon Schubert       && ops->deprecated_xfer_memory != NULL)
19475796c8dcSSimon Schubert     /* If available, fall back to the target's
19485796c8dcSSimon Schubert        "deprecated_xfer_memory" method.  */
19495796c8dcSSimon Schubert     {
19505796c8dcSSimon Schubert       int xfered = -1;
1951cf7f2e2dSJohn Marino 
19525796c8dcSSimon Schubert       errno = 0;
19535796c8dcSSimon Schubert       if (writebuf != NULL)
19545796c8dcSSimon Schubert 	{
19555796c8dcSSimon Schubert 	  void *buffer = xmalloc (len);
19565796c8dcSSimon Schubert 	  struct cleanup *cleanup = make_cleanup (xfree, buffer);
1957cf7f2e2dSJohn Marino 
19585796c8dcSSimon Schubert 	  memcpy (buffer, writebuf, len);
19595796c8dcSSimon Schubert 	  xfered = ops->deprecated_xfer_memory (offset, buffer, len,
19605796c8dcSSimon Schubert 						1/*write*/, NULL, ops);
19615796c8dcSSimon Schubert 	  do_cleanups (cleanup);
19625796c8dcSSimon Schubert 	}
19635796c8dcSSimon Schubert       if (readbuf != NULL)
19645796c8dcSSimon Schubert 	xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
19655796c8dcSSimon Schubert 					      0/*read*/, NULL, ops);
19665796c8dcSSimon Schubert       if (xfered > 0)
19675796c8dcSSimon Schubert 	return xfered;
19685796c8dcSSimon Schubert       else if (xfered == 0 && errno == 0)
19695796c8dcSSimon Schubert 	/* "deprecated_xfer_memory" uses 0, cross checked against
19705796c8dcSSimon Schubert            ERRNO as one indication of an error.  */
19715796c8dcSSimon Schubert 	return 0;
19725796c8dcSSimon Schubert       else
19735796c8dcSSimon Schubert 	return -1;
19745796c8dcSSimon Schubert     }
19755796c8dcSSimon Schubert   else if (ops->beneath != NULL)
19765796c8dcSSimon Schubert     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
19775796c8dcSSimon Schubert 					  readbuf, writebuf, offset, len);
19785796c8dcSSimon Schubert   else
19795796c8dcSSimon Schubert     return -1;
19805796c8dcSSimon Schubert }
19815796c8dcSSimon Schubert 
19825796c8dcSSimon Schubert /* The xfer_partial handler for the topmost target.  Unlike the default,
19835796c8dcSSimon Schubert    it does not need to handle memory specially; it just passes all
19845796c8dcSSimon Schubert    requests down the stack.  */
19855796c8dcSSimon Schubert 
19865796c8dcSSimon Schubert static LONGEST
current_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)19875796c8dcSSimon Schubert current_xfer_partial (struct target_ops *ops, enum target_object object,
19885796c8dcSSimon Schubert 		      const char *annex, gdb_byte *readbuf,
19895796c8dcSSimon Schubert 		      const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
19905796c8dcSSimon Schubert {
19915796c8dcSSimon Schubert   if (ops->beneath != NULL)
19925796c8dcSSimon Schubert     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
19935796c8dcSSimon Schubert 					  readbuf, writebuf, offset, len);
19945796c8dcSSimon Schubert   else
19955796c8dcSSimon Schubert     return -1;
19965796c8dcSSimon Schubert }
19975796c8dcSSimon Schubert 
1998cf7f2e2dSJohn Marino /* Target vector read/write partial wrapper functions.  */
19995796c8dcSSimon Schubert 
20005796c8dcSSimon Schubert static LONGEST
target_read_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * buf,ULONGEST offset,LONGEST len)20015796c8dcSSimon Schubert target_read_partial (struct target_ops *ops,
20025796c8dcSSimon Schubert 		     enum target_object object,
20035796c8dcSSimon Schubert 		     const char *annex, gdb_byte *buf,
20045796c8dcSSimon Schubert 		     ULONGEST offset, LONGEST len)
20055796c8dcSSimon Schubert {
20065796c8dcSSimon Schubert   return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
20075796c8dcSSimon Schubert }
20085796c8dcSSimon Schubert 
20095796c8dcSSimon Schubert static LONGEST
target_write_partial(struct target_ops * ops,enum target_object object,const char * annex,const gdb_byte * buf,ULONGEST offset,LONGEST len)20105796c8dcSSimon Schubert target_write_partial (struct target_ops *ops,
20115796c8dcSSimon Schubert 		      enum target_object object,
20125796c8dcSSimon Schubert 		      const char *annex, const gdb_byte *buf,
20135796c8dcSSimon Schubert 		      ULONGEST offset, LONGEST len)
20145796c8dcSSimon Schubert {
20155796c8dcSSimon Schubert   return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
20165796c8dcSSimon Schubert }
20175796c8dcSSimon Schubert 
20185796c8dcSSimon Schubert /* Wrappers to perform the full transfer.  */
2019cf7f2e2dSJohn Marino 
2020cf7f2e2dSJohn Marino /* For docs on target_read see target.h.  */
2021cf7f2e2dSJohn Marino 
20225796c8dcSSimon Schubert LONGEST
target_read(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * buf,ULONGEST offset,LONGEST len)20235796c8dcSSimon Schubert target_read (struct target_ops *ops,
20245796c8dcSSimon Schubert 	     enum target_object object,
20255796c8dcSSimon Schubert 	     const char *annex, gdb_byte *buf,
20265796c8dcSSimon Schubert 	     ULONGEST offset, LONGEST len)
20275796c8dcSSimon Schubert {
20285796c8dcSSimon Schubert   LONGEST xfered = 0;
2029cf7f2e2dSJohn Marino 
20305796c8dcSSimon Schubert   while (xfered < len)
20315796c8dcSSimon Schubert     {
20325796c8dcSSimon Schubert       LONGEST xfer = target_read_partial (ops, object, annex,
20335796c8dcSSimon Schubert 					  (gdb_byte *) buf + xfered,
20345796c8dcSSimon Schubert 					  offset + xfered, len - xfered);
2035cf7f2e2dSJohn Marino 
20365796c8dcSSimon Schubert       /* Call an observer, notifying them of the xfer progress?  */
20375796c8dcSSimon Schubert       if (xfer == 0)
20385796c8dcSSimon Schubert 	return xfered;
20395796c8dcSSimon Schubert       if (xfer < 0)
20405796c8dcSSimon Schubert 	return -1;
20415796c8dcSSimon Schubert       xfered += xfer;
20425796c8dcSSimon Schubert       QUIT;
20435796c8dcSSimon Schubert     }
20445796c8dcSSimon Schubert   return len;
20455796c8dcSSimon Schubert }
20465796c8dcSSimon Schubert 
2047c50c785cSJohn Marino /* Assuming that the entire [begin, end) range of memory cannot be
2048c50c785cSJohn Marino    read, try to read whatever subrange is possible to read.
2049cf7f2e2dSJohn Marino 
2050c50c785cSJohn Marino    The function returns, in RESULT, either zero or one memory block.
2051c50c785cSJohn Marino    If there's a readable subrange at the beginning, it is completely
2052c50c785cSJohn Marino    read and returned.  Any further readable subrange will not be read.
2053c50c785cSJohn Marino    Otherwise, if there's a readable subrange at the end, it will be
2054c50c785cSJohn Marino    completely read and returned.  Any readable subranges before it
2055c50c785cSJohn Marino    (obviously, not starting at the beginning), will be ignored.  In
2056c50c785cSJohn Marino    other cases -- either no readable subrange, or readable subrange(s)
2057c50c785cSJohn Marino    that is neither at the beginning, or end, nothing is returned.
2058cf7f2e2dSJohn Marino 
2059c50c785cSJohn Marino    The purpose of this function is to handle a read across a boundary
2060c50c785cSJohn Marino    of accessible memory in a case when memory map is not available.
2061c50c785cSJohn Marino    The above restrictions are fine for this case, but will give
2062c50c785cSJohn Marino    incorrect results if the memory is 'patchy'.  However, supporting
2063c50c785cSJohn Marino    'patchy' memory would require trying to read every single byte,
2064c50c785cSJohn Marino    and it seems unacceptable solution.  Explicit memory map is
2065c50c785cSJohn Marino    recommended for this case -- and target_read_memory_robust will
2066c50c785cSJohn Marino    take care of reading multiple ranges then.  */
20675796c8dcSSimon Schubert 
2068c50c785cSJohn Marino static void
read_whatever_is_readable(struct target_ops * ops,ULONGEST begin,ULONGEST end,VEC (memory_read_result_s)** result)2069c50c785cSJohn Marino read_whatever_is_readable (struct target_ops *ops,
2070c50c785cSJohn Marino 			   ULONGEST begin, ULONGEST end,
2071c50c785cSJohn Marino 			   VEC(memory_read_result_s) **result)
2072c50c785cSJohn Marino {
2073c50c785cSJohn Marino   gdb_byte *buf = xmalloc (end - begin);
2074c50c785cSJohn Marino   ULONGEST current_begin = begin;
2075c50c785cSJohn Marino   ULONGEST current_end = end;
2076c50c785cSJohn Marino   int forward;
2077c50c785cSJohn Marino   memory_read_result_s r;
20785796c8dcSSimon Schubert 
2079c50c785cSJohn Marino   /* If we previously failed to read 1 byte, nothing can be done here.  */
2080c50c785cSJohn Marino   if (end - begin <= 1)
20815796c8dcSSimon Schubert     {
2082c50c785cSJohn Marino       xfree (buf);
2083c50c785cSJohn Marino       return;
2084c50c785cSJohn Marino     }
2085c50c785cSJohn Marino 
2086c50c785cSJohn Marino   /* Check that either first or the last byte is readable, and give up
2087c50c785cSJohn Marino      if not.  This heuristic is meant to permit reading accessible memory
2088c50c785cSJohn Marino      at the boundary of accessible region.  */
2089c50c785cSJohn Marino   if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2090c50c785cSJohn Marino 			   buf, begin, 1) == 1)
20915796c8dcSSimon Schubert     {
2092c50c785cSJohn Marino       forward = 1;
2093c50c785cSJohn Marino       ++current_begin;
2094c50c785cSJohn Marino     }
2095c50c785cSJohn Marino   else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2096c50c785cSJohn Marino 				buf + (end-begin) - 1, end - 1, 1) == 1)
2097c50c785cSJohn Marino     {
2098c50c785cSJohn Marino       forward = 0;
2099c50c785cSJohn Marino       --current_end;
21005796c8dcSSimon Schubert     }
21015796c8dcSSimon Schubert   else
21025796c8dcSSimon Schubert     {
2103c50c785cSJohn Marino       xfree (buf);
2104c50c785cSJohn Marino       return;
21055796c8dcSSimon Schubert     }
21065796c8dcSSimon Schubert 
2107c50c785cSJohn Marino   /* Loop invariant is that the [current_begin, current_end) was previously
2108c50c785cSJohn Marino      found to be not readable as a whole.
2109c50c785cSJohn Marino 
2110c50c785cSJohn Marino      Note loop condition -- if the range has 1 byte, we can't divide the range
2111c50c785cSJohn Marino      so there's no point trying further.  */
2112c50c785cSJohn Marino   while (current_end - current_begin > 1)
2113c50c785cSJohn Marino     {
2114c50c785cSJohn Marino       ULONGEST first_half_begin, first_half_end;
2115c50c785cSJohn Marino       ULONGEST second_half_begin, second_half_end;
2116c50c785cSJohn Marino       LONGEST xfer;
2117c50c785cSJohn Marino       ULONGEST middle = current_begin + (current_end - current_begin)/2;
2118c50c785cSJohn Marino 
2119c50c785cSJohn Marino       if (forward)
2120c50c785cSJohn Marino 	{
2121c50c785cSJohn Marino 	  first_half_begin = current_begin;
2122c50c785cSJohn Marino 	  first_half_end = middle;
2123c50c785cSJohn Marino 	  second_half_begin = middle;
2124c50c785cSJohn Marino 	  second_half_end = current_end;
21255796c8dcSSimon Schubert 	}
2126c50c785cSJohn Marino       else
2127c50c785cSJohn Marino 	{
2128c50c785cSJohn Marino 	  first_half_begin = middle;
2129c50c785cSJohn Marino 	  first_half_end = current_end;
2130c50c785cSJohn Marino 	  second_half_begin = current_begin;
2131c50c785cSJohn Marino 	  second_half_end = middle;
2132c50c785cSJohn Marino 	}
2133c50c785cSJohn Marino 
2134c50c785cSJohn Marino       xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2135c50c785cSJohn Marino 			  buf + (first_half_begin - begin),
2136c50c785cSJohn Marino 			  first_half_begin,
2137c50c785cSJohn Marino 			  first_half_end - first_half_begin);
2138c50c785cSJohn Marino 
2139c50c785cSJohn Marino       if (xfer == first_half_end - first_half_begin)
2140c50c785cSJohn Marino 	{
2141c50c785cSJohn Marino 	  /* This half reads up fine.  So, the error must be in the
2142c50c785cSJohn Marino 	     other half.  */
2143c50c785cSJohn Marino 	  current_begin = second_half_begin;
2144c50c785cSJohn Marino 	  current_end = second_half_end;
2145c50c785cSJohn Marino 	}
2146c50c785cSJohn Marino       else
2147c50c785cSJohn Marino 	{
2148c50c785cSJohn Marino 	  /* This half is not readable.  Because we've tried one byte, we
2149c50c785cSJohn Marino 	     know some part of this half if actually redable.  Go to the next
2150c50c785cSJohn Marino 	     iteration to divide again and try to read.
2151c50c785cSJohn Marino 
2152c50c785cSJohn Marino 	     We don't handle the other half, because this function only tries
2153c50c785cSJohn Marino 	     to read a single readable subrange.  */
2154c50c785cSJohn Marino 	  current_begin = first_half_begin;
2155c50c785cSJohn Marino 	  current_end = first_half_end;
2156c50c785cSJohn Marino 	}
2157c50c785cSJohn Marino     }
2158c50c785cSJohn Marino 
2159c50c785cSJohn Marino   if (forward)
2160c50c785cSJohn Marino     {
2161c50c785cSJohn Marino       /* The [begin, current_begin) range has been read.  */
2162c50c785cSJohn Marino       r.begin = begin;
2163c50c785cSJohn Marino       r.end = current_begin;
2164c50c785cSJohn Marino       r.data = buf;
2165c50c785cSJohn Marino     }
2166c50c785cSJohn Marino   else
2167c50c785cSJohn Marino     {
2168c50c785cSJohn Marino       /* The [current_end, end) range has been read.  */
2169c50c785cSJohn Marino       LONGEST rlen = end - current_end;
2170c50c785cSJohn Marino 
2171c50c785cSJohn Marino       r.data = xmalloc (rlen);
2172c50c785cSJohn Marino       memcpy (r.data, buf + current_end - begin, rlen);
2173c50c785cSJohn Marino       r.begin = current_end;
2174c50c785cSJohn Marino       r.end = end;
2175c50c785cSJohn Marino       xfree (buf);
2176c50c785cSJohn Marino     }
2177c50c785cSJohn Marino   VEC_safe_push(memory_read_result_s, (*result), &r);
2178c50c785cSJohn Marino }
2179c50c785cSJohn Marino 
2180c50c785cSJohn Marino void
free_memory_read_result_vector(void * x)2181c50c785cSJohn Marino free_memory_read_result_vector (void *x)
2182c50c785cSJohn Marino {
2183c50c785cSJohn Marino   VEC(memory_read_result_s) *v = x;
2184c50c785cSJohn Marino   memory_read_result_s *current;
2185c50c785cSJohn Marino   int ix;
2186c50c785cSJohn Marino 
2187c50c785cSJohn Marino   for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
2188c50c785cSJohn Marino     {
2189c50c785cSJohn Marino       xfree (current->data);
2190c50c785cSJohn Marino     }
2191c50c785cSJohn Marino   VEC_free (memory_read_result_s, v);
2192c50c785cSJohn Marino }
2193c50c785cSJohn Marino 
VEC(memory_read_result_s)2194c50c785cSJohn Marino VEC(memory_read_result_s) *
2195c50c785cSJohn Marino read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
2196c50c785cSJohn Marino {
2197c50c785cSJohn Marino   VEC(memory_read_result_s) *result = 0;
2198c50c785cSJohn Marino 
2199c50c785cSJohn Marino   LONGEST xfered = 0;
2200c50c785cSJohn Marino   while (xfered < len)
2201c50c785cSJohn Marino     {
2202c50c785cSJohn Marino       struct mem_region *region = lookup_mem_region (offset + xfered);
2203c50c785cSJohn Marino       LONGEST rlen;
2204c50c785cSJohn Marino 
2205c50c785cSJohn Marino       /* If there is no explicit region, a fake one should be created.  */
2206c50c785cSJohn Marino       gdb_assert (region);
2207c50c785cSJohn Marino 
2208c50c785cSJohn Marino       if (region->hi == 0)
2209c50c785cSJohn Marino 	rlen = len - xfered;
2210c50c785cSJohn Marino       else
2211c50c785cSJohn Marino 	rlen = region->hi - offset;
2212c50c785cSJohn Marino 
2213c50c785cSJohn Marino       if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
2214c50c785cSJohn Marino 	{
2215c50c785cSJohn Marino 	  /* Cannot read this region.  Note that we can end up here only
2216c50c785cSJohn Marino 	     if the region is explicitly marked inaccessible, or
2217c50c785cSJohn Marino 	     'inaccessible-by-default' is in effect.  */
2218c50c785cSJohn Marino 	  xfered += rlen;
2219c50c785cSJohn Marino 	}
2220c50c785cSJohn Marino       else
2221c50c785cSJohn Marino 	{
2222c50c785cSJohn Marino 	  LONGEST to_read = min (len - xfered, rlen);
2223c50c785cSJohn Marino 	  gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
2224c50c785cSJohn Marino 
2225c50c785cSJohn Marino 	  LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2226c50c785cSJohn Marino 				      (gdb_byte *) buffer,
2227c50c785cSJohn Marino 				      offset + xfered, to_read);
2228c50c785cSJohn Marino 	  /* Call an observer, notifying them of the xfer progress?  */
2229c50c785cSJohn Marino 	  if (xfer <= 0)
2230c50c785cSJohn Marino 	    {
2231c50c785cSJohn Marino 	      /* Got an error reading full chunk.  See if maybe we can read
2232c50c785cSJohn Marino 		 some subrange.  */
2233c50c785cSJohn Marino 	      xfree (buffer);
2234c50c785cSJohn Marino 	      read_whatever_is_readable (ops, offset + xfered,
2235c50c785cSJohn Marino 					 offset + xfered + to_read, &result);
2236c50c785cSJohn Marino 	      xfered += to_read;
2237c50c785cSJohn Marino 	    }
2238c50c785cSJohn Marino 	  else
2239c50c785cSJohn Marino 	    {
2240c50c785cSJohn Marino 	      struct memory_read_result r;
2241c50c785cSJohn Marino 	      r.data = buffer;
2242c50c785cSJohn Marino 	      r.begin = offset + xfered;
2243c50c785cSJohn Marino 	      r.end = r.begin + xfer;
2244c50c785cSJohn Marino 	      VEC_safe_push (memory_read_result_s, result, &r);
22455796c8dcSSimon Schubert 	      xfered += xfer;
2246c50c785cSJohn Marino 	    }
22475796c8dcSSimon Schubert 	  QUIT;
22485796c8dcSSimon Schubert 	}
22495796c8dcSSimon Schubert     }
2250c50c785cSJohn Marino   return result;
2251c50c785cSJohn Marino }
2252c50c785cSJohn Marino 
22535796c8dcSSimon Schubert 
22545796c8dcSSimon Schubert /* An alternative to target_write with progress callbacks.  */
22555796c8dcSSimon Schubert 
22565796c8dcSSimon Schubert LONGEST
target_write_with_progress(struct target_ops * ops,enum target_object object,const char * annex,const gdb_byte * buf,ULONGEST offset,LONGEST len,void (* progress)(ULONGEST,void *),void * baton)22575796c8dcSSimon Schubert target_write_with_progress (struct target_ops *ops,
22585796c8dcSSimon Schubert 			    enum target_object object,
22595796c8dcSSimon Schubert 			    const char *annex, const gdb_byte *buf,
22605796c8dcSSimon Schubert 			    ULONGEST offset, LONGEST len,
22615796c8dcSSimon Schubert 			    void (*progress) (ULONGEST, void *), void *baton)
22625796c8dcSSimon Schubert {
22635796c8dcSSimon Schubert   LONGEST xfered = 0;
22645796c8dcSSimon Schubert 
22655796c8dcSSimon Schubert   /* Give the progress callback a chance to set up.  */
22665796c8dcSSimon Schubert   if (progress)
22675796c8dcSSimon Schubert     (*progress) (0, baton);
22685796c8dcSSimon Schubert 
22695796c8dcSSimon Schubert   while (xfered < len)
22705796c8dcSSimon Schubert     {
22715796c8dcSSimon Schubert       LONGEST xfer = target_write_partial (ops, object, annex,
22725796c8dcSSimon Schubert 					   (gdb_byte *) buf + xfered,
22735796c8dcSSimon Schubert 					   offset + xfered, len - xfered);
22745796c8dcSSimon Schubert 
22755796c8dcSSimon Schubert       if (xfer == 0)
22765796c8dcSSimon Schubert 	return xfered;
22775796c8dcSSimon Schubert       if (xfer < 0)
22785796c8dcSSimon Schubert 	return -1;
22795796c8dcSSimon Schubert 
22805796c8dcSSimon Schubert       if (progress)
22815796c8dcSSimon Schubert 	(*progress) (xfer, baton);
22825796c8dcSSimon Schubert 
22835796c8dcSSimon Schubert       xfered += xfer;
22845796c8dcSSimon Schubert       QUIT;
22855796c8dcSSimon Schubert     }
22865796c8dcSSimon Schubert   return len;
22875796c8dcSSimon Schubert }
22885796c8dcSSimon Schubert 
2289cf7f2e2dSJohn Marino /* For docs on target_write see target.h.  */
2290cf7f2e2dSJohn Marino 
22915796c8dcSSimon Schubert LONGEST
target_write(struct target_ops * ops,enum target_object object,const char * annex,const gdb_byte * buf,ULONGEST offset,LONGEST len)22925796c8dcSSimon Schubert target_write (struct target_ops *ops,
22935796c8dcSSimon Schubert 	      enum target_object object,
22945796c8dcSSimon Schubert 	      const char *annex, const gdb_byte *buf,
22955796c8dcSSimon Schubert 	      ULONGEST offset, LONGEST len)
22965796c8dcSSimon Schubert {
22975796c8dcSSimon Schubert   return target_write_with_progress (ops, object, annex, buf, offset, len,
22985796c8dcSSimon Schubert 				     NULL, NULL);
22995796c8dcSSimon Schubert }
23005796c8dcSSimon Schubert 
23015796c8dcSSimon Schubert /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
23025796c8dcSSimon Schubert    the size of the transferred data.  PADDING additional bytes are
23035796c8dcSSimon Schubert    available in *BUF_P.  This is a helper function for
23045796c8dcSSimon Schubert    target_read_alloc; see the declaration of that function for more
23055796c8dcSSimon Schubert    information.  */
23065796c8dcSSimon Schubert 
23075796c8dcSSimon Schubert static LONGEST
target_read_alloc_1(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte ** buf_p,int padding)23085796c8dcSSimon Schubert target_read_alloc_1 (struct target_ops *ops, enum target_object object,
23095796c8dcSSimon Schubert 		     const char *annex, gdb_byte **buf_p, int padding)
23105796c8dcSSimon Schubert {
23115796c8dcSSimon Schubert   size_t buf_alloc, buf_pos;
23125796c8dcSSimon Schubert   gdb_byte *buf;
23135796c8dcSSimon Schubert   LONGEST n;
23145796c8dcSSimon Schubert 
23155796c8dcSSimon Schubert   /* This function does not have a length parameter; it reads the
23165796c8dcSSimon Schubert      entire OBJECT).  Also, it doesn't support objects fetched partly
23175796c8dcSSimon Schubert      from one target and partly from another (in a different stratum,
23185796c8dcSSimon Schubert      e.g. a core file and an executable).  Both reasons make it
23195796c8dcSSimon Schubert      unsuitable for reading memory.  */
23205796c8dcSSimon Schubert   gdb_assert (object != TARGET_OBJECT_MEMORY);
23215796c8dcSSimon Schubert 
23225796c8dcSSimon Schubert   /* Start by reading up to 4K at a time.  The target will throttle
23235796c8dcSSimon Schubert      this number down if necessary.  */
23245796c8dcSSimon Schubert   buf_alloc = 4096;
23255796c8dcSSimon Schubert   buf = xmalloc (buf_alloc);
23265796c8dcSSimon Schubert   buf_pos = 0;
23275796c8dcSSimon Schubert   while (1)
23285796c8dcSSimon Schubert     {
23295796c8dcSSimon Schubert       n = target_read_partial (ops, object, annex, &buf[buf_pos],
23305796c8dcSSimon Schubert 			       buf_pos, buf_alloc - buf_pos - padding);
23315796c8dcSSimon Schubert       if (n < 0)
23325796c8dcSSimon Schubert 	{
23335796c8dcSSimon Schubert 	  /* An error occurred.  */
23345796c8dcSSimon Schubert 	  xfree (buf);
23355796c8dcSSimon Schubert 	  return -1;
23365796c8dcSSimon Schubert 	}
23375796c8dcSSimon Schubert       else if (n == 0)
23385796c8dcSSimon Schubert 	{
23395796c8dcSSimon Schubert 	  /* Read all there was.  */
23405796c8dcSSimon Schubert 	  if (buf_pos == 0)
23415796c8dcSSimon Schubert 	    xfree (buf);
23425796c8dcSSimon Schubert 	  else
23435796c8dcSSimon Schubert 	    *buf_p = buf;
23445796c8dcSSimon Schubert 	  return buf_pos;
23455796c8dcSSimon Schubert 	}
23465796c8dcSSimon Schubert 
23475796c8dcSSimon Schubert       buf_pos += n;
23485796c8dcSSimon Schubert 
23495796c8dcSSimon Schubert       /* If the buffer is filling up, expand it.  */
23505796c8dcSSimon Schubert       if (buf_alloc < buf_pos * 2)
23515796c8dcSSimon Schubert 	{
23525796c8dcSSimon Schubert 	  buf_alloc *= 2;
23535796c8dcSSimon Schubert 	  buf = xrealloc (buf, buf_alloc);
23545796c8dcSSimon Schubert 	}
23555796c8dcSSimon Schubert 
23565796c8dcSSimon Schubert       QUIT;
23575796c8dcSSimon Schubert     }
23585796c8dcSSimon Schubert }
23595796c8dcSSimon Schubert 
23605796c8dcSSimon Schubert /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
23615796c8dcSSimon Schubert    the size of the transferred data.  See the declaration in "target.h"
23625796c8dcSSimon Schubert    function for more information about the return value.  */
23635796c8dcSSimon Schubert 
23645796c8dcSSimon Schubert LONGEST
target_read_alloc(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte ** buf_p)23655796c8dcSSimon Schubert target_read_alloc (struct target_ops *ops, enum target_object object,
23665796c8dcSSimon Schubert 		   const char *annex, gdb_byte **buf_p)
23675796c8dcSSimon Schubert {
23685796c8dcSSimon Schubert   return target_read_alloc_1 (ops, object, annex, buf_p, 0);
23695796c8dcSSimon Schubert }
23705796c8dcSSimon Schubert 
23715796c8dcSSimon Schubert /* Read OBJECT/ANNEX using OPS.  The result is NUL-terminated and
23725796c8dcSSimon Schubert    returned as a string, allocated using xmalloc.  If an error occurs
23735796c8dcSSimon Schubert    or the transfer is unsupported, NULL is returned.  Empty objects
23745796c8dcSSimon Schubert    are returned as allocated but empty strings.  A warning is issued
23755796c8dcSSimon Schubert    if the result contains any embedded NUL bytes.  */
23765796c8dcSSimon Schubert 
23775796c8dcSSimon Schubert char *
target_read_stralloc(struct target_ops * ops,enum target_object object,const char * annex)23785796c8dcSSimon Schubert target_read_stralloc (struct target_ops *ops, enum target_object object,
23795796c8dcSSimon Schubert 		      const char *annex)
23805796c8dcSSimon Schubert {
23815796c8dcSSimon Schubert   gdb_byte *buffer;
2382*ef5ccd6cSJohn Marino   char *bufstr;
2383*ef5ccd6cSJohn Marino   LONGEST i, transferred;
23845796c8dcSSimon Schubert 
23855796c8dcSSimon Schubert   transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
2386*ef5ccd6cSJohn Marino   bufstr = (char *) buffer;
23875796c8dcSSimon Schubert 
23885796c8dcSSimon Schubert   if (transferred < 0)
23895796c8dcSSimon Schubert     return NULL;
23905796c8dcSSimon Schubert 
23915796c8dcSSimon Schubert   if (transferred == 0)
23925796c8dcSSimon Schubert     return xstrdup ("");
23935796c8dcSSimon Schubert 
2394*ef5ccd6cSJohn Marino   bufstr[transferred] = 0;
2395*ef5ccd6cSJohn Marino 
2396*ef5ccd6cSJohn Marino   /* Check for embedded NUL bytes; but allow trailing NULs.  */
2397*ef5ccd6cSJohn Marino   for (i = strlen (bufstr); i < transferred; i++)
2398*ef5ccd6cSJohn Marino     if (bufstr[i] != 0)
2399*ef5ccd6cSJohn Marino       {
24005796c8dcSSimon Schubert 	warning (_("target object %d, annex %s, "
24015796c8dcSSimon Schubert 		   "contained unexpected null characters"),
24025796c8dcSSimon Schubert 		 (int) object, annex ? annex : "(none)");
2403*ef5ccd6cSJohn Marino 	break;
2404*ef5ccd6cSJohn Marino       }
24055796c8dcSSimon Schubert 
2406*ef5ccd6cSJohn Marino   return bufstr;
24075796c8dcSSimon Schubert }
24085796c8dcSSimon Schubert 
24095796c8dcSSimon Schubert /* Memory transfer methods.  */
24105796c8dcSSimon Schubert 
24115796c8dcSSimon Schubert void
get_target_memory(struct target_ops * ops,CORE_ADDR addr,gdb_byte * buf,LONGEST len)24125796c8dcSSimon Schubert get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
24135796c8dcSSimon Schubert 		   LONGEST len)
24145796c8dcSSimon Schubert {
24155796c8dcSSimon Schubert   /* This method is used to read from an alternate, non-current
24165796c8dcSSimon Schubert      target.  This read must bypass the overlay support (as symbols
24175796c8dcSSimon Schubert      don't match this target), and GDB's internal cache (wrong cache
24185796c8dcSSimon Schubert      for this target).  */
24195796c8dcSSimon Schubert   if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
24205796c8dcSSimon Schubert       != len)
24215796c8dcSSimon Schubert     memory_error (EIO, addr);
24225796c8dcSSimon Schubert }
24235796c8dcSSimon Schubert 
24245796c8dcSSimon Schubert ULONGEST
get_target_memory_unsigned(struct target_ops * ops,CORE_ADDR addr,int len,enum bfd_endian byte_order)2425cf7f2e2dSJohn Marino get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2426cf7f2e2dSJohn Marino 			    int len, enum bfd_endian byte_order)
24275796c8dcSSimon Schubert {
24285796c8dcSSimon Schubert   gdb_byte buf[sizeof (ULONGEST)];
24295796c8dcSSimon Schubert 
24305796c8dcSSimon Schubert   gdb_assert (len <= sizeof (buf));
24315796c8dcSSimon Schubert   get_target_memory (ops, addr, buf, len);
24325796c8dcSSimon Schubert   return extract_unsigned_integer (buf, len, byte_order);
24335796c8dcSSimon Schubert }
24345796c8dcSSimon Schubert 
2435cf7f2e2dSJohn Marino int
target_insert_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)2436cf7f2e2dSJohn Marino target_insert_breakpoint (struct gdbarch *gdbarch,
2437cf7f2e2dSJohn Marino 			  struct bp_target_info *bp_tgt)
2438cf7f2e2dSJohn Marino {
2439cf7f2e2dSJohn Marino   if (!may_insert_breakpoints)
2440cf7f2e2dSJohn Marino     {
2441cf7f2e2dSJohn Marino       warning (_("May not insert breakpoints"));
2442cf7f2e2dSJohn Marino       return 1;
2443cf7f2e2dSJohn Marino     }
2444cf7f2e2dSJohn Marino 
2445cf7f2e2dSJohn Marino   return (*current_target.to_insert_breakpoint) (gdbarch, bp_tgt);
2446cf7f2e2dSJohn Marino }
2447cf7f2e2dSJohn Marino 
2448cf7f2e2dSJohn Marino int
target_remove_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)2449cf7f2e2dSJohn Marino target_remove_breakpoint (struct gdbarch *gdbarch,
2450cf7f2e2dSJohn Marino 			  struct bp_target_info *bp_tgt)
2451cf7f2e2dSJohn Marino {
2452cf7f2e2dSJohn Marino   /* This is kind of a weird case to handle, but the permission might
2453cf7f2e2dSJohn Marino      have been changed after breakpoints were inserted - in which case
2454cf7f2e2dSJohn Marino      we should just take the user literally and assume that any
2455cf7f2e2dSJohn Marino      breakpoints should be left in place.  */
2456cf7f2e2dSJohn Marino   if (!may_insert_breakpoints)
2457cf7f2e2dSJohn Marino     {
2458cf7f2e2dSJohn Marino       warning (_("May not remove breakpoints"));
2459cf7f2e2dSJohn Marino       return 1;
2460cf7f2e2dSJohn Marino     }
2461cf7f2e2dSJohn Marino 
2462cf7f2e2dSJohn Marino   return (*current_target.to_remove_breakpoint) (gdbarch, bp_tgt);
2463cf7f2e2dSJohn Marino }
2464cf7f2e2dSJohn Marino 
24655796c8dcSSimon Schubert static void
target_info(char * args,int from_tty)24665796c8dcSSimon Schubert target_info (char *args, int from_tty)
24675796c8dcSSimon Schubert {
24685796c8dcSSimon Schubert   struct target_ops *t;
24695796c8dcSSimon Schubert   int has_all_mem = 0;
24705796c8dcSSimon Schubert 
24715796c8dcSSimon Schubert   if (symfile_objfile != NULL)
24725796c8dcSSimon Schubert     printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
24735796c8dcSSimon Schubert 
24745796c8dcSSimon Schubert   for (t = target_stack; t != NULL; t = t->beneath)
24755796c8dcSSimon Schubert     {
24765796c8dcSSimon Schubert       if (!(*t->to_has_memory) (t))
24775796c8dcSSimon Schubert 	continue;
24785796c8dcSSimon Schubert 
24795796c8dcSSimon Schubert       if ((int) (t->to_stratum) <= (int) dummy_stratum)
24805796c8dcSSimon Schubert 	continue;
24815796c8dcSSimon Schubert       if (has_all_mem)
2482c50c785cSJohn Marino 	printf_unfiltered (_("\tWhile running this, "
2483c50c785cSJohn Marino 			     "GDB does not access memory from...\n"));
24845796c8dcSSimon Schubert       printf_unfiltered ("%s:\n", t->to_longname);
24855796c8dcSSimon Schubert       (t->to_files_info) (t);
24865796c8dcSSimon Schubert       has_all_mem = (*t->to_has_all_memory) (t);
24875796c8dcSSimon Schubert     }
24885796c8dcSSimon Schubert }
24895796c8dcSSimon Schubert 
24905796c8dcSSimon Schubert /* This function is called before any new inferior is created, e.g.
24915796c8dcSSimon Schubert    by running a program, attaching, or connecting to a target.
24925796c8dcSSimon Schubert    It cleans up any state from previous invocations which might
24935796c8dcSSimon Schubert    change between runs.  This is a subset of what target_preopen
24945796c8dcSSimon Schubert    resets (things which might change between targets).  */
24955796c8dcSSimon Schubert 
24965796c8dcSSimon Schubert void
target_pre_inferior(int from_tty)24975796c8dcSSimon Schubert target_pre_inferior (int from_tty)
24985796c8dcSSimon Schubert {
24995796c8dcSSimon Schubert   /* Clear out solib state.  Otherwise the solib state of the previous
25005796c8dcSSimon Schubert      inferior might have survived and is entirely wrong for the new
25015796c8dcSSimon Schubert      target.  This has been observed on GNU/Linux using glibc 2.3.  How
25025796c8dcSSimon Schubert      to reproduce:
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert      bash$ ./foo&
25055796c8dcSSimon Schubert      [1] 4711
25065796c8dcSSimon Schubert      bash$ ./foo&
25075796c8dcSSimon Schubert      [1] 4712
25085796c8dcSSimon Schubert      bash$ gdb ./foo
25095796c8dcSSimon Schubert      [...]
25105796c8dcSSimon Schubert      (gdb) attach 4711
25115796c8dcSSimon Schubert      (gdb) detach
25125796c8dcSSimon Schubert      (gdb) attach 4712
25135796c8dcSSimon Schubert      Cannot access memory at address 0xdeadbeef
25145796c8dcSSimon Schubert   */
25155796c8dcSSimon Schubert 
25165796c8dcSSimon Schubert   /* In some OSs, the shared library list is the same/global/shared
25175796c8dcSSimon Schubert      across inferiors.  If code is shared between processes, so are
25185796c8dcSSimon Schubert      memory regions and features.  */
2519*ef5ccd6cSJohn Marino   if (!gdbarch_has_global_solist (target_gdbarch ()))
25205796c8dcSSimon Schubert     {
25215796c8dcSSimon Schubert       no_shared_libraries (NULL, from_tty);
25225796c8dcSSimon Schubert 
25235796c8dcSSimon Schubert       invalidate_target_mem_regions ();
25245796c8dcSSimon Schubert 
25255796c8dcSSimon Schubert       target_clear_description ();
25265796c8dcSSimon Schubert     }
2527*ef5ccd6cSJohn Marino 
2528*ef5ccd6cSJohn Marino   agent_capability_invalidate ();
25295796c8dcSSimon Schubert }
25305796c8dcSSimon Schubert 
25315796c8dcSSimon Schubert /* Callback for iterate_over_inferiors.  Gets rid of the given
25325796c8dcSSimon Schubert    inferior.  */
25335796c8dcSSimon Schubert 
25345796c8dcSSimon Schubert static int
dispose_inferior(struct inferior * inf,void * args)25355796c8dcSSimon Schubert dispose_inferior (struct inferior *inf, void *args)
25365796c8dcSSimon Schubert {
25375796c8dcSSimon Schubert   struct thread_info *thread;
25385796c8dcSSimon Schubert 
25395796c8dcSSimon Schubert   thread = any_thread_of_process (inf->pid);
25405796c8dcSSimon Schubert   if (thread)
25415796c8dcSSimon Schubert     {
25425796c8dcSSimon Schubert       switch_to_thread (thread->ptid);
25435796c8dcSSimon Schubert 
25445796c8dcSSimon Schubert       /* Core inferiors actually should be detached, not killed.  */
25455796c8dcSSimon Schubert       if (target_has_execution)
25465796c8dcSSimon Schubert 	target_kill ();
25475796c8dcSSimon Schubert       else
25485796c8dcSSimon Schubert 	target_detach (NULL, 0);
25495796c8dcSSimon Schubert     }
25505796c8dcSSimon Schubert 
25515796c8dcSSimon Schubert   return 0;
25525796c8dcSSimon Schubert }
25535796c8dcSSimon Schubert 
25545796c8dcSSimon Schubert /* This is to be called by the open routine before it does
25555796c8dcSSimon Schubert    anything.  */
25565796c8dcSSimon Schubert 
25575796c8dcSSimon Schubert void
target_preopen(int from_tty)25585796c8dcSSimon Schubert target_preopen (int from_tty)
25595796c8dcSSimon Schubert {
25605796c8dcSSimon Schubert   dont_repeat ();
25615796c8dcSSimon Schubert 
25625796c8dcSSimon Schubert   if (have_inferiors ())
25635796c8dcSSimon Schubert     {
25645796c8dcSSimon Schubert       if (!from_tty
25655796c8dcSSimon Schubert 	  || !have_live_inferiors ()
25665796c8dcSSimon Schubert 	  || query (_("A program is being debugged already.  Kill it? ")))
25675796c8dcSSimon Schubert 	iterate_over_inferiors (dispose_inferior, NULL);
25685796c8dcSSimon Schubert       else
25695796c8dcSSimon Schubert 	error (_("Program not killed."));
25705796c8dcSSimon Schubert     }
25715796c8dcSSimon Schubert 
25725796c8dcSSimon Schubert   /* Calling target_kill may remove the target from the stack.  But if
25735796c8dcSSimon Schubert      it doesn't (which seems like a win for UDI), remove it now.  */
25745796c8dcSSimon Schubert   /* Leave the exec target, though.  The user may be switching from a
25755796c8dcSSimon Schubert      live process to a core of the same program.  */
25765796c8dcSSimon Schubert   pop_all_targets_above (file_stratum, 0);
25775796c8dcSSimon Schubert 
25785796c8dcSSimon Schubert   target_pre_inferior (from_tty);
25795796c8dcSSimon Schubert }
25805796c8dcSSimon Schubert 
25815796c8dcSSimon Schubert /* Detach a target after doing deferred register stores.  */
25825796c8dcSSimon Schubert 
25835796c8dcSSimon Schubert void
target_detach(char * args,int from_tty)25845796c8dcSSimon Schubert target_detach (char *args, int from_tty)
25855796c8dcSSimon Schubert {
25865796c8dcSSimon Schubert   struct target_ops* t;
25875796c8dcSSimon Schubert 
2588*ef5ccd6cSJohn Marino   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
25895796c8dcSSimon Schubert     /* Don't remove global breakpoints here.  They're removed on
25905796c8dcSSimon Schubert        disconnection from the target.  */
25915796c8dcSSimon Schubert     ;
25925796c8dcSSimon Schubert   else
25935796c8dcSSimon Schubert     /* If we're in breakpoints-always-inserted mode, have to remove
25945796c8dcSSimon Schubert        them before detaching.  */
2595cf7f2e2dSJohn Marino     remove_breakpoints_pid (PIDGET (inferior_ptid));
2596cf7f2e2dSJohn Marino 
2597cf7f2e2dSJohn Marino   prepare_for_detach ();
25985796c8dcSSimon Schubert 
25995796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
26005796c8dcSSimon Schubert     {
26015796c8dcSSimon Schubert       if (t->to_detach != NULL)
26025796c8dcSSimon Schubert 	{
26035796c8dcSSimon Schubert 	  t->to_detach (t, args, from_tty);
26045796c8dcSSimon Schubert 	  if (targetdebug)
26055796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
26065796c8dcSSimon Schubert 				args, from_tty);
26075796c8dcSSimon Schubert 	  return;
26085796c8dcSSimon Schubert 	}
26095796c8dcSSimon Schubert     }
26105796c8dcSSimon Schubert 
2611c50c785cSJohn Marino   internal_error (__FILE__, __LINE__, _("could not find a target to detach"));
26125796c8dcSSimon Schubert }
26135796c8dcSSimon Schubert 
26145796c8dcSSimon Schubert void
target_disconnect(char * args,int from_tty)26155796c8dcSSimon Schubert target_disconnect (char *args, int from_tty)
26165796c8dcSSimon Schubert {
26175796c8dcSSimon Schubert   struct target_ops *t;
26185796c8dcSSimon Schubert 
26195796c8dcSSimon Schubert   /* If we're in breakpoints-always-inserted mode or if breakpoints
26205796c8dcSSimon Schubert      are global across processes, we have to remove them before
26215796c8dcSSimon Schubert      disconnecting.  */
26225796c8dcSSimon Schubert   remove_breakpoints ();
26235796c8dcSSimon Schubert 
26245796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
26255796c8dcSSimon Schubert     if (t->to_disconnect != NULL)
26265796c8dcSSimon Schubert 	{
26275796c8dcSSimon Schubert 	  if (targetdebug)
26285796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
26295796c8dcSSimon Schubert 				args, from_tty);
26305796c8dcSSimon Schubert 	  t->to_disconnect (t, args, from_tty);
26315796c8dcSSimon Schubert 	  return;
26325796c8dcSSimon Schubert 	}
26335796c8dcSSimon Schubert 
26345796c8dcSSimon Schubert   tcomplain ();
26355796c8dcSSimon Schubert }
26365796c8dcSSimon Schubert 
26375796c8dcSSimon Schubert ptid_t
target_wait(ptid_t ptid,struct target_waitstatus * status,int options)26385796c8dcSSimon Schubert target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
26395796c8dcSSimon Schubert {
26405796c8dcSSimon Schubert   struct target_ops *t;
26415796c8dcSSimon Schubert 
26425796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
26435796c8dcSSimon Schubert     {
26445796c8dcSSimon Schubert       if (t->to_wait != NULL)
26455796c8dcSSimon Schubert 	{
26465796c8dcSSimon Schubert 	  ptid_t retval = (*t->to_wait) (t, ptid, status, options);
26475796c8dcSSimon Schubert 
26485796c8dcSSimon Schubert 	  if (targetdebug)
26495796c8dcSSimon Schubert 	    {
26505796c8dcSSimon Schubert 	      char *status_string;
2651*ef5ccd6cSJohn Marino 	      char *options_string;
26525796c8dcSSimon Schubert 
26535796c8dcSSimon Schubert 	      status_string = target_waitstatus_to_string (status);
2654*ef5ccd6cSJohn Marino 	      options_string = target_options_to_string (options);
26555796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog,
2656*ef5ccd6cSJohn Marino 				  "target_wait (%d, status, options={%s})"
2657*ef5ccd6cSJohn Marino 				  " = %d,   %s\n",
2658*ef5ccd6cSJohn Marino 				  PIDGET (ptid), options_string,
2659*ef5ccd6cSJohn Marino 				  PIDGET (retval), status_string);
26605796c8dcSSimon Schubert 	      xfree (status_string);
2661*ef5ccd6cSJohn Marino 	      xfree (options_string);
26625796c8dcSSimon Schubert 	    }
26635796c8dcSSimon Schubert 
26645796c8dcSSimon Schubert 	  return retval;
26655796c8dcSSimon Schubert 	}
26665796c8dcSSimon Schubert     }
26675796c8dcSSimon Schubert 
26685796c8dcSSimon Schubert   noprocess ();
26695796c8dcSSimon Schubert }
26705796c8dcSSimon Schubert 
26715796c8dcSSimon Schubert char *
target_pid_to_str(ptid_t ptid)26725796c8dcSSimon Schubert target_pid_to_str (ptid_t ptid)
26735796c8dcSSimon Schubert {
26745796c8dcSSimon Schubert   struct target_ops *t;
26755796c8dcSSimon Schubert 
26765796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
26775796c8dcSSimon Schubert     {
26785796c8dcSSimon Schubert       if (t->to_pid_to_str != NULL)
26795796c8dcSSimon Schubert 	return (*t->to_pid_to_str) (t, ptid);
26805796c8dcSSimon Schubert     }
26815796c8dcSSimon Schubert 
26825796c8dcSSimon Schubert   return normal_pid_to_str (ptid);
26835796c8dcSSimon Schubert }
26845796c8dcSSimon Schubert 
2685c50c785cSJohn Marino char *
target_thread_name(struct thread_info * info)2686c50c785cSJohn Marino target_thread_name (struct thread_info *info)
2687c50c785cSJohn Marino {
2688c50c785cSJohn Marino   struct target_ops *t;
2689c50c785cSJohn Marino 
2690c50c785cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
2691c50c785cSJohn Marino     {
2692c50c785cSJohn Marino       if (t->to_thread_name != NULL)
2693c50c785cSJohn Marino 	return (*t->to_thread_name) (info);
2694c50c785cSJohn Marino     }
2695c50c785cSJohn Marino 
2696c50c785cSJohn Marino   return NULL;
2697c50c785cSJohn Marino }
2698c50c785cSJohn Marino 
26995796c8dcSSimon Schubert void
target_resume(ptid_t ptid,int step,enum gdb_signal signal)2700*ef5ccd6cSJohn Marino target_resume (ptid_t ptid, int step, enum gdb_signal signal)
27015796c8dcSSimon Schubert {
27025796c8dcSSimon Schubert   struct target_ops *t;
27035796c8dcSSimon Schubert 
27045796c8dcSSimon Schubert   target_dcache_invalidate ();
27055796c8dcSSimon Schubert 
27065796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
27075796c8dcSSimon Schubert     {
27085796c8dcSSimon Schubert       if (t->to_resume != NULL)
27095796c8dcSSimon Schubert 	{
27105796c8dcSSimon Schubert 	  t->to_resume (t, ptid, step, signal);
27115796c8dcSSimon Schubert 	  if (targetdebug)
27125796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
27135796c8dcSSimon Schubert 				PIDGET (ptid),
27145796c8dcSSimon Schubert 				step ? "step" : "continue",
2715*ef5ccd6cSJohn Marino 				gdb_signal_to_name (signal));
27165796c8dcSSimon Schubert 
2717cf7f2e2dSJohn Marino 	  registers_changed_ptid (ptid);
27185796c8dcSSimon Schubert 	  set_executing (ptid, 1);
27195796c8dcSSimon Schubert 	  set_running (ptid, 1);
27205796c8dcSSimon Schubert 	  clear_inline_frame_state (ptid);
27215796c8dcSSimon Schubert 	  return;
27225796c8dcSSimon Schubert 	}
27235796c8dcSSimon Schubert     }
27245796c8dcSSimon Schubert 
27255796c8dcSSimon Schubert   noprocess ();
27265796c8dcSSimon Schubert }
2727a45ae5f8SJohn Marino 
2728a45ae5f8SJohn Marino void
target_pass_signals(int numsigs,unsigned char * pass_signals)2729a45ae5f8SJohn Marino target_pass_signals (int numsigs, unsigned char *pass_signals)
2730a45ae5f8SJohn Marino {
2731a45ae5f8SJohn Marino   struct target_ops *t;
2732a45ae5f8SJohn Marino 
2733a45ae5f8SJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
2734a45ae5f8SJohn Marino     {
2735a45ae5f8SJohn Marino       if (t->to_pass_signals != NULL)
2736a45ae5f8SJohn Marino 	{
2737a45ae5f8SJohn Marino 	  if (targetdebug)
2738a45ae5f8SJohn Marino 	    {
2739a45ae5f8SJohn Marino 	      int i;
2740a45ae5f8SJohn Marino 
2741a45ae5f8SJohn Marino 	      fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2742a45ae5f8SJohn Marino 				  numsigs);
2743a45ae5f8SJohn Marino 
2744a45ae5f8SJohn Marino 	      for (i = 0; i < numsigs; i++)
2745a45ae5f8SJohn Marino 		if (pass_signals[i])
2746a45ae5f8SJohn Marino 		  fprintf_unfiltered (gdb_stdlog, " %s",
2747*ef5ccd6cSJohn Marino 				      gdb_signal_to_name (i));
2748a45ae5f8SJohn Marino 
2749a45ae5f8SJohn Marino 	      fprintf_unfiltered (gdb_stdlog, " })\n");
2750a45ae5f8SJohn Marino 	    }
2751a45ae5f8SJohn Marino 
2752a45ae5f8SJohn Marino 	  (*t->to_pass_signals) (numsigs, pass_signals);
2753a45ae5f8SJohn Marino 	  return;
2754a45ae5f8SJohn Marino 	}
2755a45ae5f8SJohn Marino     }
2756a45ae5f8SJohn Marino }
2757a45ae5f8SJohn Marino 
2758*ef5ccd6cSJohn Marino void
target_program_signals(int numsigs,unsigned char * program_signals)2759*ef5ccd6cSJohn Marino target_program_signals (int numsigs, unsigned char *program_signals)
2760*ef5ccd6cSJohn Marino {
2761*ef5ccd6cSJohn Marino   struct target_ops *t;
2762*ef5ccd6cSJohn Marino 
2763*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
2764*ef5ccd6cSJohn Marino     {
2765*ef5ccd6cSJohn Marino       if (t->to_program_signals != NULL)
2766*ef5ccd6cSJohn Marino 	{
2767*ef5ccd6cSJohn Marino 	  if (targetdebug)
2768*ef5ccd6cSJohn Marino 	    {
2769*ef5ccd6cSJohn Marino 	      int i;
2770*ef5ccd6cSJohn Marino 
2771*ef5ccd6cSJohn Marino 	      fprintf_unfiltered (gdb_stdlog, "target_program_signals (%d, {",
2772*ef5ccd6cSJohn Marino 				  numsigs);
2773*ef5ccd6cSJohn Marino 
2774*ef5ccd6cSJohn Marino 	      for (i = 0; i < numsigs; i++)
2775*ef5ccd6cSJohn Marino 		if (program_signals[i])
2776*ef5ccd6cSJohn Marino 		  fprintf_unfiltered (gdb_stdlog, " %s",
2777*ef5ccd6cSJohn Marino 				      gdb_signal_to_name (i));
2778*ef5ccd6cSJohn Marino 
2779*ef5ccd6cSJohn Marino 	      fprintf_unfiltered (gdb_stdlog, " })\n");
2780*ef5ccd6cSJohn Marino 	    }
2781*ef5ccd6cSJohn Marino 
2782*ef5ccd6cSJohn Marino 	  (*t->to_program_signals) (numsigs, program_signals);
2783*ef5ccd6cSJohn Marino 	  return;
2784*ef5ccd6cSJohn Marino 	}
2785*ef5ccd6cSJohn Marino     }
2786*ef5ccd6cSJohn Marino }
2787*ef5ccd6cSJohn Marino 
27885796c8dcSSimon Schubert /* Look through the list of possible targets for a target that can
27895796c8dcSSimon Schubert    follow forks.  */
27905796c8dcSSimon Schubert 
27915796c8dcSSimon Schubert int
target_follow_fork(int follow_child)27925796c8dcSSimon Schubert target_follow_fork (int follow_child)
27935796c8dcSSimon Schubert {
27945796c8dcSSimon Schubert   struct target_ops *t;
27955796c8dcSSimon Schubert 
27965796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
27975796c8dcSSimon Schubert     {
27985796c8dcSSimon Schubert       if (t->to_follow_fork != NULL)
27995796c8dcSSimon Schubert 	{
28005796c8dcSSimon Schubert 	  int retval = t->to_follow_fork (t, follow_child);
2801cf7f2e2dSJohn Marino 
28025796c8dcSSimon Schubert 	  if (targetdebug)
28035796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
28045796c8dcSSimon Schubert 				follow_child, retval);
28055796c8dcSSimon Schubert 	  return retval;
28065796c8dcSSimon Schubert 	}
28075796c8dcSSimon Schubert     }
28085796c8dcSSimon Schubert 
28095796c8dcSSimon Schubert   /* Some target returned a fork event, but did not know how to follow it.  */
28105796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
2811c50c785cSJohn Marino 		  _("could not find a target to follow fork"));
28125796c8dcSSimon Schubert }
28135796c8dcSSimon Schubert 
28145796c8dcSSimon Schubert void
target_mourn_inferior(void)28155796c8dcSSimon Schubert target_mourn_inferior (void)
28165796c8dcSSimon Schubert {
28175796c8dcSSimon Schubert   struct target_ops *t;
2818cf7f2e2dSJohn Marino 
28195796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
28205796c8dcSSimon Schubert     {
28215796c8dcSSimon Schubert       if (t->to_mourn_inferior != NULL)
28225796c8dcSSimon Schubert 	{
28235796c8dcSSimon Schubert 	  t->to_mourn_inferior (t);
28245796c8dcSSimon Schubert 	  if (targetdebug)
28255796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
28265796c8dcSSimon Schubert 
28275796c8dcSSimon Schubert           /* We no longer need to keep handles on any of the object files.
28285796c8dcSSimon Schubert              Make sure to release them to avoid unnecessarily locking any
28295796c8dcSSimon Schubert              of them while we're not actually debugging.  */
28305796c8dcSSimon Schubert           bfd_cache_close_all ();
28315796c8dcSSimon Schubert 
28325796c8dcSSimon Schubert 	  return;
28335796c8dcSSimon Schubert 	}
28345796c8dcSSimon Schubert     }
28355796c8dcSSimon Schubert 
28365796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
2837c50c785cSJohn Marino 		  _("could not find a target to follow mourn inferior"));
28385796c8dcSSimon Schubert }
28395796c8dcSSimon Schubert 
28405796c8dcSSimon Schubert /* Look for a target which can describe architectural features, starting
28415796c8dcSSimon Schubert    from TARGET.  If we find one, return its description.  */
28425796c8dcSSimon Schubert 
28435796c8dcSSimon Schubert const struct target_desc *
target_read_description(struct target_ops * target)28445796c8dcSSimon Schubert target_read_description (struct target_ops *target)
28455796c8dcSSimon Schubert {
28465796c8dcSSimon Schubert   struct target_ops *t;
28475796c8dcSSimon Schubert 
28485796c8dcSSimon Schubert   for (t = target; t != NULL; t = t->beneath)
28495796c8dcSSimon Schubert     if (t->to_read_description != NULL)
28505796c8dcSSimon Schubert       {
28515796c8dcSSimon Schubert 	const struct target_desc *tdesc;
28525796c8dcSSimon Schubert 
28535796c8dcSSimon Schubert 	tdesc = t->to_read_description (t);
28545796c8dcSSimon Schubert 	if (tdesc)
28555796c8dcSSimon Schubert 	  return tdesc;
28565796c8dcSSimon Schubert       }
28575796c8dcSSimon Schubert 
28585796c8dcSSimon Schubert   return NULL;
28595796c8dcSSimon Schubert }
28605796c8dcSSimon Schubert 
28615796c8dcSSimon Schubert /* The default implementation of to_search_memory.
28625796c8dcSSimon Schubert    This implements a basic search of memory, reading target memory and
28635796c8dcSSimon Schubert    performing the search here (as opposed to performing the search in on the
28645796c8dcSSimon Schubert    target side with, for example, gdbserver).  */
28655796c8dcSSimon Schubert 
28665796c8dcSSimon Schubert int
simple_search_memory(struct target_ops * ops,CORE_ADDR start_addr,ULONGEST search_space_len,const gdb_byte * pattern,ULONGEST pattern_len,CORE_ADDR * found_addrp)28675796c8dcSSimon Schubert simple_search_memory (struct target_ops *ops,
28685796c8dcSSimon Schubert 		      CORE_ADDR start_addr, ULONGEST search_space_len,
28695796c8dcSSimon Schubert 		      const gdb_byte *pattern, ULONGEST pattern_len,
28705796c8dcSSimon Schubert 		      CORE_ADDR *found_addrp)
28715796c8dcSSimon Schubert {
28725796c8dcSSimon Schubert   /* NOTE: also defined in find.c testcase.  */
28735796c8dcSSimon Schubert #define SEARCH_CHUNK_SIZE 16000
28745796c8dcSSimon Schubert   const unsigned chunk_size = SEARCH_CHUNK_SIZE;
28755796c8dcSSimon Schubert   /* Buffer to hold memory contents for searching.  */
28765796c8dcSSimon Schubert   gdb_byte *search_buf;
28775796c8dcSSimon Schubert   unsigned search_buf_size;
28785796c8dcSSimon Schubert   struct cleanup *old_cleanups;
28795796c8dcSSimon Schubert 
28805796c8dcSSimon Schubert   search_buf_size = chunk_size + pattern_len - 1;
28815796c8dcSSimon Schubert 
28825796c8dcSSimon Schubert   /* No point in trying to allocate a buffer larger than the search space.  */
28835796c8dcSSimon Schubert   if (search_space_len < search_buf_size)
28845796c8dcSSimon Schubert     search_buf_size = search_space_len;
28855796c8dcSSimon Schubert 
28865796c8dcSSimon Schubert   search_buf = malloc (search_buf_size);
28875796c8dcSSimon Schubert   if (search_buf == NULL)
28885796c8dcSSimon Schubert     error (_("Unable to allocate memory to perform the search."));
28895796c8dcSSimon Schubert   old_cleanups = make_cleanup (free_current_contents, &search_buf);
28905796c8dcSSimon Schubert 
28915796c8dcSSimon Schubert   /* Prime the search buffer.  */
28925796c8dcSSimon Schubert 
28935796c8dcSSimon Schubert   if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
28945796c8dcSSimon Schubert 		   search_buf, start_addr, search_buf_size) != search_buf_size)
28955796c8dcSSimon Schubert     {
2896*ef5ccd6cSJohn Marino       warning (_("Unable to access %s bytes of target "
2897*ef5ccd6cSJohn Marino 		 "memory at %s, halting search."),
2898*ef5ccd6cSJohn Marino 	       pulongest (search_buf_size), hex_string (start_addr));
28995796c8dcSSimon Schubert       do_cleanups (old_cleanups);
29005796c8dcSSimon Schubert       return -1;
29015796c8dcSSimon Schubert     }
29025796c8dcSSimon Schubert 
29035796c8dcSSimon Schubert   /* Perform the search.
29045796c8dcSSimon Schubert 
29055796c8dcSSimon Schubert      The loop is kept simple by allocating [N + pattern-length - 1] bytes.
29065796c8dcSSimon Schubert      When we've scanned N bytes we copy the trailing bytes to the start and
29075796c8dcSSimon Schubert      read in another N bytes.  */
29085796c8dcSSimon Schubert 
29095796c8dcSSimon Schubert   while (search_space_len >= pattern_len)
29105796c8dcSSimon Schubert     {
29115796c8dcSSimon Schubert       gdb_byte *found_ptr;
29125796c8dcSSimon Schubert       unsigned nr_search_bytes = min (search_space_len, search_buf_size);
29135796c8dcSSimon Schubert 
29145796c8dcSSimon Schubert       found_ptr = memmem (search_buf, nr_search_bytes,
29155796c8dcSSimon Schubert 			  pattern, pattern_len);
29165796c8dcSSimon Schubert 
29175796c8dcSSimon Schubert       if (found_ptr != NULL)
29185796c8dcSSimon Schubert 	{
29195796c8dcSSimon Schubert 	  CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2920cf7f2e2dSJohn Marino 
29215796c8dcSSimon Schubert 	  *found_addrp = found_addr;
29225796c8dcSSimon Schubert 	  do_cleanups (old_cleanups);
29235796c8dcSSimon Schubert 	  return 1;
29245796c8dcSSimon Schubert 	}
29255796c8dcSSimon Schubert 
29265796c8dcSSimon Schubert       /* Not found in this chunk, skip to next chunk.  */
29275796c8dcSSimon Schubert 
29285796c8dcSSimon Schubert       /* Don't let search_space_len wrap here, it's unsigned.  */
29295796c8dcSSimon Schubert       if (search_space_len >= chunk_size)
29305796c8dcSSimon Schubert 	search_space_len -= chunk_size;
29315796c8dcSSimon Schubert       else
29325796c8dcSSimon Schubert 	search_space_len = 0;
29335796c8dcSSimon Schubert 
29345796c8dcSSimon Schubert       if (search_space_len >= pattern_len)
29355796c8dcSSimon Schubert 	{
29365796c8dcSSimon Schubert 	  unsigned keep_len = search_buf_size - chunk_size;
2937cf7f2e2dSJohn Marino 	  CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
29385796c8dcSSimon Schubert 	  int nr_to_read;
29395796c8dcSSimon Schubert 
29405796c8dcSSimon Schubert 	  /* Copy the trailing part of the previous iteration to the front
29415796c8dcSSimon Schubert 	     of the buffer for the next iteration.  */
29425796c8dcSSimon Schubert 	  gdb_assert (keep_len == pattern_len - 1);
29435796c8dcSSimon Schubert 	  memcpy (search_buf, search_buf + chunk_size, keep_len);
29445796c8dcSSimon Schubert 
29455796c8dcSSimon Schubert 	  nr_to_read = min (search_space_len - keep_len, chunk_size);
29465796c8dcSSimon Schubert 
29475796c8dcSSimon Schubert 	  if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
29485796c8dcSSimon Schubert 			   search_buf + keep_len, read_addr,
29495796c8dcSSimon Schubert 			   nr_to_read) != nr_to_read)
29505796c8dcSSimon Schubert 	    {
2951*ef5ccd6cSJohn Marino 	      warning (_("Unable to access %s bytes of target "
2952c50c785cSJohn Marino 			 "memory at %s, halting search."),
2953*ef5ccd6cSJohn Marino 		       plongest (nr_to_read),
29545796c8dcSSimon Schubert 		       hex_string (read_addr));
29555796c8dcSSimon Schubert 	      do_cleanups (old_cleanups);
29565796c8dcSSimon Schubert 	      return -1;
29575796c8dcSSimon Schubert 	    }
29585796c8dcSSimon Schubert 
29595796c8dcSSimon Schubert 	  start_addr += chunk_size;
29605796c8dcSSimon Schubert 	}
29615796c8dcSSimon Schubert     }
29625796c8dcSSimon Schubert 
29635796c8dcSSimon Schubert   /* Not found.  */
29645796c8dcSSimon Schubert 
29655796c8dcSSimon Schubert   do_cleanups (old_cleanups);
29665796c8dcSSimon Schubert   return 0;
29675796c8dcSSimon Schubert }
29685796c8dcSSimon Schubert 
29695796c8dcSSimon Schubert /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
29705796c8dcSSimon Schubert    sequence of bytes in PATTERN with length PATTERN_LEN.
29715796c8dcSSimon Schubert 
29725796c8dcSSimon Schubert    The result is 1 if found, 0 if not found, and -1 if there was an error
29735796c8dcSSimon Schubert    requiring halting of the search (e.g. memory read error).
29745796c8dcSSimon Schubert    If the pattern is found the address is recorded in FOUND_ADDRP.  */
29755796c8dcSSimon Schubert 
29765796c8dcSSimon Schubert int
target_search_memory(CORE_ADDR start_addr,ULONGEST search_space_len,const gdb_byte * pattern,ULONGEST pattern_len,CORE_ADDR * found_addrp)29775796c8dcSSimon Schubert target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
29785796c8dcSSimon Schubert 		      const gdb_byte *pattern, ULONGEST pattern_len,
29795796c8dcSSimon Schubert 		      CORE_ADDR *found_addrp)
29805796c8dcSSimon Schubert {
29815796c8dcSSimon Schubert   struct target_ops *t;
29825796c8dcSSimon Schubert   int found;
29835796c8dcSSimon Schubert 
29845796c8dcSSimon Schubert   /* We don't use INHERIT to set current_target.to_search_memory,
29855796c8dcSSimon Schubert      so we have to scan the target stack and handle targetdebug
29865796c8dcSSimon Schubert      ourselves.  */
29875796c8dcSSimon Schubert 
29885796c8dcSSimon Schubert   if (targetdebug)
29895796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
29905796c8dcSSimon Schubert 			hex_string (start_addr));
29915796c8dcSSimon Schubert 
29925796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
29935796c8dcSSimon Schubert     if (t->to_search_memory != NULL)
29945796c8dcSSimon Schubert       break;
29955796c8dcSSimon Schubert 
29965796c8dcSSimon Schubert   if (t != NULL)
29975796c8dcSSimon Schubert     {
29985796c8dcSSimon Schubert       found = t->to_search_memory (t, start_addr, search_space_len,
29995796c8dcSSimon Schubert 				   pattern, pattern_len, found_addrp);
30005796c8dcSSimon Schubert     }
30015796c8dcSSimon Schubert   else
30025796c8dcSSimon Schubert     {
30035796c8dcSSimon Schubert       /* If a special version of to_search_memory isn't available, use the
30045796c8dcSSimon Schubert 	 simple version.  */
30055796c8dcSSimon Schubert       found = simple_search_memory (current_target.beneath,
30065796c8dcSSimon Schubert 				    start_addr, search_space_len,
30075796c8dcSSimon Schubert 				    pattern, pattern_len, found_addrp);
30085796c8dcSSimon Schubert     }
30095796c8dcSSimon Schubert 
30105796c8dcSSimon Schubert   if (targetdebug)
30115796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "  = %d\n", found);
30125796c8dcSSimon Schubert 
30135796c8dcSSimon Schubert   return found;
30145796c8dcSSimon Schubert }
30155796c8dcSSimon Schubert 
30165796c8dcSSimon Schubert /* Look through the currently pushed targets.  If none of them will
30175796c8dcSSimon Schubert    be able to restart the currently running process, issue an error
30185796c8dcSSimon Schubert    message.  */
30195796c8dcSSimon Schubert 
30205796c8dcSSimon Schubert void
target_require_runnable(void)30215796c8dcSSimon Schubert target_require_runnable (void)
30225796c8dcSSimon Schubert {
30235796c8dcSSimon Schubert   struct target_ops *t;
30245796c8dcSSimon Schubert 
30255796c8dcSSimon Schubert   for (t = target_stack; t != NULL; t = t->beneath)
30265796c8dcSSimon Schubert     {
30275796c8dcSSimon Schubert       /* If this target knows how to create a new program, then
30285796c8dcSSimon Schubert 	 assume we will still be able to after killing the current
30295796c8dcSSimon Schubert 	 one.  Either killing and mourning will not pop T, or else
30305796c8dcSSimon Schubert 	 find_default_run_target will find it again.  */
30315796c8dcSSimon Schubert       if (t->to_create_inferior != NULL)
30325796c8dcSSimon Schubert 	return;
30335796c8dcSSimon Schubert 
30345796c8dcSSimon Schubert       /* Do not worry about thread_stratum targets that can not
30355796c8dcSSimon Schubert 	 create inferiors.  Assume they will be pushed again if
30365796c8dcSSimon Schubert 	 necessary, and continue to the process_stratum.  */
30375796c8dcSSimon Schubert       if (t->to_stratum == thread_stratum
30385796c8dcSSimon Schubert 	  || t->to_stratum == arch_stratum)
30395796c8dcSSimon Schubert 	continue;
30405796c8dcSSimon Schubert 
3041c50c785cSJohn Marino       error (_("The \"%s\" target does not support \"run\".  "
3042c50c785cSJohn Marino 	       "Try \"help target\" or \"continue\"."),
30435796c8dcSSimon Schubert 	     t->to_shortname);
30445796c8dcSSimon Schubert     }
30455796c8dcSSimon Schubert 
30465796c8dcSSimon Schubert   /* This function is only called if the target is running.  In that
30475796c8dcSSimon Schubert      case there should have been a process_stratum target and it
30485796c8dcSSimon Schubert      should either know how to create inferiors, or not...  */
3049c50c785cSJohn Marino   internal_error (__FILE__, __LINE__, _("No targets found"));
30505796c8dcSSimon Schubert }
30515796c8dcSSimon Schubert 
30525796c8dcSSimon Schubert /* Look through the list of possible targets for a target that can
30535796c8dcSSimon Schubert    execute a run or attach command without any other data.  This is
30545796c8dcSSimon Schubert    used to locate the default process stratum.
30555796c8dcSSimon Schubert 
30565796c8dcSSimon Schubert    If DO_MESG is not NULL, the result is always valid (error() is
30575796c8dcSSimon Schubert    called for errors); else, return NULL on error.  */
30585796c8dcSSimon Schubert 
30595796c8dcSSimon Schubert static struct target_ops *
find_default_run_target(char * do_mesg)30605796c8dcSSimon Schubert find_default_run_target (char *do_mesg)
30615796c8dcSSimon Schubert {
30625796c8dcSSimon Schubert   struct target_ops **t;
30635796c8dcSSimon Schubert   struct target_ops *runable = NULL;
30645796c8dcSSimon Schubert   int count;
30655796c8dcSSimon Schubert 
30665796c8dcSSimon Schubert   count = 0;
30675796c8dcSSimon Schubert 
30685796c8dcSSimon Schubert   for (t = target_structs; t < target_structs + target_struct_size;
30695796c8dcSSimon Schubert        ++t)
30705796c8dcSSimon Schubert     {
30715796c8dcSSimon Schubert       if ((*t)->to_can_run && target_can_run (*t))
30725796c8dcSSimon Schubert 	{
30735796c8dcSSimon Schubert 	  runable = *t;
30745796c8dcSSimon Schubert 	  ++count;
30755796c8dcSSimon Schubert 	}
30765796c8dcSSimon Schubert     }
30775796c8dcSSimon Schubert 
30785796c8dcSSimon Schubert   if (count != 1)
30795796c8dcSSimon Schubert     {
30805796c8dcSSimon Schubert       if (do_mesg)
30815796c8dcSSimon Schubert 	error (_("Don't know how to %s.  Try \"help target\"."), do_mesg);
30825796c8dcSSimon Schubert       else
30835796c8dcSSimon Schubert 	return NULL;
30845796c8dcSSimon Schubert     }
30855796c8dcSSimon Schubert 
30865796c8dcSSimon Schubert   return runable;
30875796c8dcSSimon Schubert }
30885796c8dcSSimon Schubert 
30895796c8dcSSimon Schubert void
find_default_attach(struct target_ops * ops,char * args,int from_tty)30905796c8dcSSimon Schubert find_default_attach (struct target_ops *ops, char *args, int from_tty)
30915796c8dcSSimon Schubert {
30925796c8dcSSimon Schubert   struct target_ops *t;
30935796c8dcSSimon Schubert 
30945796c8dcSSimon Schubert   t = find_default_run_target ("attach");
30955796c8dcSSimon Schubert   (t->to_attach) (t, args, from_tty);
30965796c8dcSSimon Schubert   return;
30975796c8dcSSimon Schubert }
30985796c8dcSSimon Schubert 
30995796c8dcSSimon Schubert void
find_default_create_inferior(struct target_ops * ops,char * exec_file,char * allargs,char ** env,int from_tty)31005796c8dcSSimon Schubert find_default_create_inferior (struct target_ops *ops,
31015796c8dcSSimon Schubert 			      char *exec_file, char *allargs, char **env,
31025796c8dcSSimon Schubert 			      int from_tty)
31035796c8dcSSimon Schubert {
31045796c8dcSSimon Schubert   struct target_ops *t;
31055796c8dcSSimon Schubert 
31065796c8dcSSimon Schubert   t = find_default_run_target ("run");
31075796c8dcSSimon Schubert   (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
31085796c8dcSSimon Schubert   return;
31095796c8dcSSimon Schubert }
31105796c8dcSSimon Schubert 
31115796c8dcSSimon Schubert static int
find_default_can_async_p(void)31125796c8dcSSimon Schubert find_default_can_async_p (void)
31135796c8dcSSimon Schubert {
31145796c8dcSSimon Schubert   struct target_ops *t;
31155796c8dcSSimon Schubert 
31165796c8dcSSimon Schubert   /* This may be called before the target is pushed on the stack;
31175796c8dcSSimon Schubert      look for the default process stratum.  If there's none, gdb isn't
31185796c8dcSSimon Schubert      configured with a native debugger, and target remote isn't
31195796c8dcSSimon Schubert      connected yet.  */
31205796c8dcSSimon Schubert   t = find_default_run_target (NULL);
31215796c8dcSSimon Schubert   if (t && t->to_can_async_p)
31225796c8dcSSimon Schubert     return (t->to_can_async_p) ();
31235796c8dcSSimon Schubert   return 0;
31245796c8dcSSimon Schubert }
31255796c8dcSSimon Schubert 
31265796c8dcSSimon Schubert static int
find_default_is_async_p(void)31275796c8dcSSimon Schubert find_default_is_async_p (void)
31285796c8dcSSimon Schubert {
31295796c8dcSSimon Schubert   struct target_ops *t;
31305796c8dcSSimon Schubert 
31315796c8dcSSimon Schubert   /* This may be called before the target is pushed on the stack;
31325796c8dcSSimon Schubert      look for the default process stratum.  If there's none, gdb isn't
31335796c8dcSSimon Schubert      configured with a native debugger, and target remote isn't
31345796c8dcSSimon Schubert      connected yet.  */
31355796c8dcSSimon Schubert   t = find_default_run_target (NULL);
31365796c8dcSSimon Schubert   if (t && t->to_is_async_p)
31375796c8dcSSimon Schubert     return (t->to_is_async_p) ();
31385796c8dcSSimon Schubert   return 0;
31395796c8dcSSimon Schubert }
31405796c8dcSSimon Schubert 
31415796c8dcSSimon Schubert static int
find_default_supports_non_stop(void)31425796c8dcSSimon Schubert find_default_supports_non_stop (void)
31435796c8dcSSimon Schubert {
31445796c8dcSSimon Schubert   struct target_ops *t;
31455796c8dcSSimon Schubert 
31465796c8dcSSimon Schubert   t = find_default_run_target (NULL);
31475796c8dcSSimon Schubert   if (t && t->to_supports_non_stop)
31485796c8dcSSimon Schubert     return (t->to_supports_non_stop) ();
31495796c8dcSSimon Schubert   return 0;
31505796c8dcSSimon Schubert }
31515796c8dcSSimon Schubert 
31525796c8dcSSimon Schubert int
target_supports_non_stop(void)31535796c8dcSSimon Schubert target_supports_non_stop (void)
31545796c8dcSSimon Schubert {
31555796c8dcSSimon Schubert   struct target_ops *t;
3156cf7f2e2dSJohn Marino 
31575796c8dcSSimon Schubert   for (t = &current_target; t != NULL; t = t->beneath)
31585796c8dcSSimon Schubert     if (t->to_supports_non_stop)
31595796c8dcSSimon Schubert       return t->to_supports_non_stop ();
31605796c8dcSSimon Schubert 
31615796c8dcSSimon Schubert   return 0;
31625796c8dcSSimon Schubert }
31635796c8dcSSimon Schubert 
3164*ef5ccd6cSJohn Marino /* Implement the "info proc" command.  */
3165*ef5ccd6cSJohn Marino 
3166*ef5ccd6cSJohn Marino int
target_info_proc(char * args,enum info_proc_what what)3167*ef5ccd6cSJohn Marino target_info_proc (char *args, enum info_proc_what what)
3168*ef5ccd6cSJohn Marino {
3169*ef5ccd6cSJohn Marino   struct target_ops *t;
3170*ef5ccd6cSJohn Marino 
3171*ef5ccd6cSJohn Marino   /* If we're already connected to something that can get us OS
3172*ef5ccd6cSJohn Marino      related data, use it.  Otherwise, try using the native
3173*ef5ccd6cSJohn Marino      target.  */
3174*ef5ccd6cSJohn Marino   if (current_target.to_stratum >= process_stratum)
3175*ef5ccd6cSJohn Marino     t = current_target.beneath;
3176*ef5ccd6cSJohn Marino   else
3177*ef5ccd6cSJohn Marino     t = find_default_run_target (NULL);
3178*ef5ccd6cSJohn Marino 
3179*ef5ccd6cSJohn Marino   for (; t != NULL; t = t->beneath)
3180*ef5ccd6cSJohn Marino     {
3181*ef5ccd6cSJohn Marino       if (t->to_info_proc != NULL)
3182*ef5ccd6cSJohn Marino 	{
3183*ef5ccd6cSJohn Marino 	  t->to_info_proc (t, args, what);
3184*ef5ccd6cSJohn Marino 
3185*ef5ccd6cSJohn Marino 	  if (targetdebug)
3186*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3187*ef5ccd6cSJohn Marino 				"target_info_proc (\"%s\", %d)\n", args, what);
3188*ef5ccd6cSJohn Marino 
3189*ef5ccd6cSJohn Marino 	  return 1;
3190*ef5ccd6cSJohn Marino 	}
3191*ef5ccd6cSJohn Marino     }
3192*ef5ccd6cSJohn Marino 
3193*ef5ccd6cSJohn Marino   return 0;
3194*ef5ccd6cSJohn Marino }
3195*ef5ccd6cSJohn Marino 
3196a45ae5f8SJohn Marino static int
find_default_supports_disable_randomization(void)3197a45ae5f8SJohn Marino find_default_supports_disable_randomization (void)
3198a45ae5f8SJohn Marino {
3199a45ae5f8SJohn Marino   struct target_ops *t;
3200a45ae5f8SJohn Marino 
3201a45ae5f8SJohn Marino   t = find_default_run_target (NULL);
3202a45ae5f8SJohn Marino   if (t && t->to_supports_disable_randomization)
3203a45ae5f8SJohn Marino     return (t->to_supports_disable_randomization) ();
3204a45ae5f8SJohn Marino   return 0;
3205a45ae5f8SJohn Marino }
3206a45ae5f8SJohn Marino 
3207a45ae5f8SJohn Marino int
target_supports_disable_randomization(void)3208a45ae5f8SJohn Marino target_supports_disable_randomization (void)
3209a45ae5f8SJohn Marino {
3210a45ae5f8SJohn Marino   struct target_ops *t;
3211a45ae5f8SJohn Marino 
3212a45ae5f8SJohn Marino   for (t = &current_target; t != NULL; t = t->beneath)
3213a45ae5f8SJohn Marino     if (t->to_supports_disable_randomization)
3214a45ae5f8SJohn Marino       return t->to_supports_disable_randomization ();
3215a45ae5f8SJohn Marino 
3216a45ae5f8SJohn Marino   return 0;
3217a45ae5f8SJohn Marino }
32185796c8dcSSimon Schubert 
32195796c8dcSSimon Schubert char *
target_get_osdata(const char * type)32205796c8dcSSimon Schubert target_get_osdata (const char *type)
32215796c8dcSSimon Schubert {
32225796c8dcSSimon Schubert   struct target_ops *t;
32235796c8dcSSimon Schubert 
32245796c8dcSSimon Schubert   /* If we're already connected to something that can get us OS
32255796c8dcSSimon Schubert      related data, use it.  Otherwise, try using the native
32265796c8dcSSimon Schubert      target.  */
32275796c8dcSSimon Schubert   if (current_target.to_stratum >= process_stratum)
32285796c8dcSSimon Schubert     t = current_target.beneath;
32295796c8dcSSimon Schubert   else
32305796c8dcSSimon Schubert     t = find_default_run_target ("get OS data");
32315796c8dcSSimon Schubert 
32325796c8dcSSimon Schubert   if (!t)
32335796c8dcSSimon Schubert     return NULL;
32345796c8dcSSimon Schubert 
32355796c8dcSSimon Schubert   return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
32365796c8dcSSimon Schubert }
32375796c8dcSSimon Schubert 
3238cf7f2e2dSJohn Marino /* Determine the current address space of thread PTID.  */
3239cf7f2e2dSJohn Marino 
3240cf7f2e2dSJohn Marino struct address_space *
target_thread_address_space(ptid_t ptid)3241cf7f2e2dSJohn Marino target_thread_address_space (ptid_t ptid)
3242cf7f2e2dSJohn Marino {
3243cf7f2e2dSJohn Marino   struct address_space *aspace;
3244cf7f2e2dSJohn Marino   struct inferior *inf;
3245cf7f2e2dSJohn Marino   struct target_ops *t;
3246cf7f2e2dSJohn Marino 
3247cf7f2e2dSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
3248cf7f2e2dSJohn Marino     {
3249cf7f2e2dSJohn Marino       if (t->to_thread_address_space != NULL)
3250cf7f2e2dSJohn Marino 	{
3251cf7f2e2dSJohn Marino 	  aspace = t->to_thread_address_space (t, ptid);
3252cf7f2e2dSJohn Marino 	  gdb_assert (aspace);
3253cf7f2e2dSJohn Marino 
3254cf7f2e2dSJohn Marino 	  if (targetdebug)
3255cf7f2e2dSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3256cf7f2e2dSJohn Marino 				"target_thread_address_space (%s) = %d\n",
3257cf7f2e2dSJohn Marino 				target_pid_to_str (ptid),
3258cf7f2e2dSJohn Marino 				address_space_num (aspace));
3259cf7f2e2dSJohn Marino 	  return aspace;
3260cf7f2e2dSJohn Marino 	}
3261cf7f2e2dSJohn Marino     }
3262cf7f2e2dSJohn Marino 
3263cf7f2e2dSJohn Marino   /* Fall-back to the "main" address space of the inferior.  */
3264cf7f2e2dSJohn Marino   inf = find_inferior_pid (ptid_get_pid (ptid));
3265cf7f2e2dSJohn Marino 
3266cf7f2e2dSJohn Marino   if (inf == NULL || inf->aspace == NULL)
3267c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
3268c50c785cSJohn Marino 		    _("Can't determine the current "
3269c50c785cSJohn Marino 		      "address space of thread %s\n"),
3270cf7f2e2dSJohn Marino 		    target_pid_to_str (ptid));
3271cf7f2e2dSJohn Marino 
3272cf7f2e2dSJohn Marino   return inf->aspace;
3273cf7f2e2dSJohn Marino }
3274cf7f2e2dSJohn Marino 
3275*ef5ccd6cSJohn Marino 
3276*ef5ccd6cSJohn Marino /* Target file operations.  */
3277*ef5ccd6cSJohn Marino 
3278*ef5ccd6cSJohn Marino static struct target_ops *
default_fileio_target(void)3279*ef5ccd6cSJohn Marino default_fileio_target (void)
3280*ef5ccd6cSJohn Marino {
3281*ef5ccd6cSJohn Marino   /* If we're already connected to something that can perform
3282*ef5ccd6cSJohn Marino      file I/O, use it. Otherwise, try using the native target.  */
3283*ef5ccd6cSJohn Marino   if (current_target.to_stratum >= process_stratum)
3284*ef5ccd6cSJohn Marino     return current_target.beneath;
3285*ef5ccd6cSJohn Marino   else
3286*ef5ccd6cSJohn Marino     return find_default_run_target ("file I/O");
3287*ef5ccd6cSJohn Marino }
3288*ef5ccd6cSJohn Marino 
3289*ef5ccd6cSJohn Marino /* Open FILENAME on the target, using FLAGS and MODE.  Return a
3290*ef5ccd6cSJohn Marino    target file descriptor, or -1 if an error occurs (and set
3291*ef5ccd6cSJohn Marino    *TARGET_ERRNO).  */
3292*ef5ccd6cSJohn Marino int
target_fileio_open(const char * filename,int flags,int mode,int * target_errno)3293*ef5ccd6cSJohn Marino target_fileio_open (const char *filename, int flags, int mode,
3294*ef5ccd6cSJohn Marino 		    int *target_errno)
3295*ef5ccd6cSJohn Marino {
3296*ef5ccd6cSJohn Marino   struct target_ops *t;
3297*ef5ccd6cSJohn Marino 
3298*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3299*ef5ccd6cSJohn Marino     {
3300*ef5ccd6cSJohn Marino       if (t->to_fileio_open != NULL)
3301*ef5ccd6cSJohn Marino 	{
3302*ef5ccd6cSJohn Marino 	  int fd = t->to_fileio_open (filename, flags, mode, target_errno);
3303*ef5ccd6cSJohn Marino 
3304*ef5ccd6cSJohn Marino 	  if (targetdebug)
3305*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3306*ef5ccd6cSJohn Marino 				"target_fileio_open (%s,0x%x,0%o) = %d (%d)\n",
3307*ef5ccd6cSJohn Marino 				filename, flags, mode,
3308*ef5ccd6cSJohn Marino 				fd, fd != -1 ? 0 : *target_errno);
3309*ef5ccd6cSJohn Marino 	  return fd;
3310*ef5ccd6cSJohn Marino 	}
3311*ef5ccd6cSJohn Marino     }
3312*ef5ccd6cSJohn Marino 
3313*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3314*ef5ccd6cSJohn Marino   return -1;
3315*ef5ccd6cSJohn Marino }
3316*ef5ccd6cSJohn Marino 
3317*ef5ccd6cSJohn Marino /* Write up to LEN bytes from WRITE_BUF to FD on the target.
3318*ef5ccd6cSJohn Marino    Return the number of bytes written, or -1 if an error occurs
3319*ef5ccd6cSJohn Marino    (and set *TARGET_ERRNO).  */
3320*ef5ccd6cSJohn Marino int
target_fileio_pwrite(int fd,const gdb_byte * write_buf,int len,ULONGEST offset,int * target_errno)3321*ef5ccd6cSJohn Marino target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
3322*ef5ccd6cSJohn Marino 		      ULONGEST offset, int *target_errno)
3323*ef5ccd6cSJohn Marino {
3324*ef5ccd6cSJohn Marino   struct target_ops *t;
3325*ef5ccd6cSJohn Marino 
3326*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3327*ef5ccd6cSJohn Marino     {
3328*ef5ccd6cSJohn Marino       if (t->to_fileio_pwrite != NULL)
3329*ef5ccd6cSJohn Marino 	{
3330*ef5ccd6cSJohn Marino 	  int ret = t->to_fileio_pwrite (fd, write_buf, len, offset,
3331*ef5ccd6cSJohn Marino 					 target_errno);
3332*ef5ccd6cSJohn Marino 
3333*ef5ccd6cSJohn Marino 	  if (targetdebug)
3334*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3335*ef5ccd6cSJohn Marino 				"target_fileio_pwrite (%d,...,%d,%s) "
3336*ef5ccd6cSJohn Marino 				"= %d (%d)\n",
3337*ef5ccd6cSJohn Marino 				fd, len, pulongest (offset),
3338*ef5ccd6cSJohn Marino 				ret, ret != -1 ? 0 : *target_errno);
3339*ef5ccd6cSJohn Marino 	  return ret;
3340*ef5ccd6cSJohn Marino 	}
3341*ef5ccd6cSJohn Marino     }
3342*ef5ccd6cSJohn Marino 
3343*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3344*ef5ccd6cSJohn Marino   return -1;
3345*ef5ccd6cSJohn Marino }
3346*ef5ccd6cSJohn Marino 
3347*ef5ccd6cSJohn Marino /* Read up to LEN bytes FD on the target into READ_BUF.
3348*ef5ccd6cSJohn Marino    Return the number of bytes read, or -1 if an error occurs
3349*ef5ccd6cSJohn Marino    (and set *TARGET_ERRNO).  */
3350*ef5ccd6cSJohn Marino int
target_fileio_pread(int fd,gdb_byte * read_buf,int len,ULONGEST offset,int * target_errno)3351*ef5ccd6cSJohn Marino target_fileio_pread (int fd, gdb_byte *read_buf, int len,
3352*ef5ccd6cSJohn Marino 		     ULONGEST offset, int *target_errno)
3353*ef5ccd6cSJohn Marino {
3354*ef5ccd6cSJohn Marino   struct target_ops *t;
3355*ef5ccd6cSJohn Marino 
3356*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3357*ef5ccd6cSJohn Marino     {
3358*ef5ccd6cSJohn Marino       if (t->to_fileio_pread != NULL)
3359*ef5ccd6cSJohn Marino 	{
3360*ef5ccd6cSJohn Marino 	  int ret = t->to_fileio_pread (fd, read_buf, len, offset,
3361*ef5ccd6cSJohn Marino 					target_errno);
3362*ef5ccd6cSJohn Marino 
3363*ef5ccd6cSJohn Marino 	  if (targetdebug)
3364*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3365*ef5ccd6cSJohn Marino 				"target_fileio_pread (%d,...,%d,%s) "
3366*ef5ccd6cSJohn Marino 				"= %d (%d)\n",
3367*ef5ccd6cSJohn Marino 				fd, len, pulongest (offset),
3368*ef5ccd6cSJohn Marino 				ret, ret != -1 ? 0 : *target_errno);
3369*ef5ccd6cSJohn Marino 	  return ret;
3370*ef5ccd6cSJohn Marino 	}
3371*ef5ccd6cSJohn Marino     }
3372*ef5ccd6cSJohn Marino 
3373*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3374*ef5ccd6cSJohn Marino   return -1;
3375*ef5ccd6cSJohn Marino }
3376*ef5ccd6cSJohn Marino 
3377*ef5ccd6cSJohn Marino /* Close FD on the target.  Return 0, or -1 if an error occurs
3378*ef5ccd6cSJohn Marino    (and set *TARGET_ERRNO).  */
3379*ef5ccd6cSJohn Marino int
target_fileio_close(int fd,int * target_errno)3380*ef5ccd6cSJohn Marino target_fileio_close (int fd, int *target_errno)
3381*ef5ccd6cSJohn Marino {
3382*ef5ccd6cSJohn Marino   struct target_ops *t;
3383*ef5ccd6cSJohn Marino 
3384*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3385*ef5ccd6cSJohn Marino     {
3386*ef5ccd6cSJohn Marino       if (t->to_fileio_close != NULL)
3387*ef5ccd6cSJohn Marino 	{
3388*ef5ccd6cSJohn Marino 	  int ret = t->to_fileio_close (fd, target_errno);
3389*ef5ccd6cSJohn Marino 
3390*ef5ccd6cSJohn Marino 	  if (targetdebug)
3391*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3392*ef5ccd6cSJohn Marino 				"target_fileio_close (%d) = %d (%d)\n",
3393*ef5ccd6cSJohn Marino 				fd, ret, ret != -1 ? 0 : *target_errno);
3394*ef5ccd6cSJohn Marino 	  return ret;
3395*ef5ccd6cSJohn Marino 	}
3396*ef5ccd6cSJohn Marino     }
3397*ef5ccd6cSJohn Marino 
3398*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3399*ef5ccd6cSJohn Marino   return -1;
3400*ef5ccd6cSJohn Marino }
3401*ef5ccd6cSJohn Marino 
3402*ef5ccd6cSJohn Marino /* Unlink FILENAME on the target.  Return 0, or -1 if an error
3403*ef5ccd6cSJohn Marino    occurs (and set *TARGET_ERRNO).  */
3404*ef5ccd6cSJohn Marino int
target_fileio_unlink(const char * filename,int * target_errno)3405*ef5ccd6cSJohn Marino target_fileio_unlink (const char *filename, int *target_errno)
3406*ef5ccd6cSJohn Marino {
3407*ef5ccd6cSJohn Marino   struct target_ops *t;
3408*ef5ccd6cSJohn Marino 
3409*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3410*ef5ccd6cSJohn Marino     {
3411*ef5ccd6cSJohn Marino       if (t->to_fileio_unlink != NULL)
3412*ef5ccd6cSJohn Marino 	{
3413*ef5ccd6cSJohn Marino 	  int ret = t->to_fileio_unlink (filename, target_errno);
3414*ef5ccd6cSJohn Marino 
3415*ef5ccd6cSJohn Marino 	  if (targetdebug)
3416*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3417*ef5ccd6cSJohn Marino 				"target_fileio_unlink (%s) = %d (%d)\n",
3418*ef5ccd6cSJohn Marino 				filename, ret, ret != -1 ? 0 : *target_errno);
3419*ef5ccd6cSJohn Marino 	  return ret;
3420*ef5ccd6cSJohn Marino 	}
3421*ef5ccd6cSJohn Marino     }
3422*ef5ccd6cSJohn Marino 
3423*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3424*ef5ccd6cSJohn Marino   return -1;
3425*ef5ccd6cSJohn Marino }
3426*ef5ccd6cSJohn Marino 
3427*ef5ccd6cSJohn Marino /* Read value of symbolic link FILENAME on the target.  Return a
3428*ef5ccd6cSJohn Marino    null-terminated string allocated via xmalloc, or NULL if an error
3429*ef5ccd6cSJohn Marino    occurs (and set *TARGET_ERRNO).  */
3430*ef5ccd6cSJohn Marino char *
target_fileio_readlink(const char * filename,int * target_errno)3431*ef5ccd6cSJohn Marino target_fileio_readlink (const char *filename, int *target_errno)
3432*ef5ccd6cSJohn Marino {
3433*ef5ccd6cSJohn Marino   struct target_ops *t;
3434*ef5ccd6cSJohn Marino 
3435*ef5ccd6cSJohn Marino   for (t = default_fileio_target (); t != NULL; t = t->beneath)
3436*ef5ccd6cSJohn Marino     {
3437*ef5ccd6cSJohn Marino       if (t->to_fileio_readlink != NULL)
3438*ef5ccd6cSJohn Marino 	{
3439*ef5ccd6cSJohn Marino 	  char *ret = t->to_fileio_readlink (filename, target_errno);
3440*ef5ccd6cSJohn Marino 
3441*ef5ccd6cSJohn Marino 	  if (targetdebug)
3442*ef5ccd6cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
3443*ef5ccd6cSJohn Marino 				"target_fileio_readlink (%s) = %s (%d)\n",
3444*ef5ccd6cSJohn Marino 				filename, ret? ret : "(nil)",
3445*ef5ccd6cSJohn Marino 				ret? 0 : *target_errno);
3446*ef5ccd6cSJohn Marino 	  return ret;
3447*ef5ccd6cSJohn Marino 	}
3448*ef5ccd6cSJohn Marino     }
3449*ef5ccd6cSJohn Marino 
3450*ef5ccd6cSJohn Marino   *target_errno = FILEIO_ENOSYS;
3451*ef5ccd6cSJohn Marino   return NULL;
3452*ef5ccd6cSJohn Marino }
3453*ef5ccd6cSJohn Marino 
3454*ef5ccd6cSJohn Marino static void
target_fileio_close_cleanup(void * opaque)3455*ef5ccd6cSJohn Marino target_fileio_close_cleanup (void *opaque)
3456*ef5ccd6cSJohn Marino {
3457*ef5ccd6cSJohn Marino   int fd = *(int *) opaque;
3458*ef5ccd6cSJohn Marino   int target_errno;
3459*ef5ccd6cSJohn Marino 
3460*ef5ccd6cSJohn Marino   target_fileio_close (fd, &target_errno);
3461*ef5ccd6cSJohn Marino }
3462*ef5ccd6cSJohn Marino 
3463*ef5ccd6cSJohn Marino /* Read target file FILENAME.  Store the result in *BUF_P and
3464*ef5ccd6cSJohn Marino    return the size of the transferred data.  PADDING additional bytes are
3465*ef5ccd6cSJohn Marino    available in *BUF_P.  This is a helper function for
3466*ef5ccd6cSJohn Marino    target_fileio_read_alloc; see the declaration of that function for more
3467*ef5ccd6cSJohn Marino    information.  */
3468*ef5ccd6cSJohn Marino 
3469*ef5ccd6cSJohn Marino static LONGEST
target_fileio_read_alloc_1(const char * filename,gdb_byte ** buf_p,int padding)3470*ef5ccd6cSJohn Marino target_fileio_read_alloc_1 (const char *filename,
3471*ef5ccd6cSJohn Marino 			    gdb_byte **buf_p, int padding)
3472*ef5ccd6cSJohn Marino {
3473*ef5ccd6cSJohn Marino   struct cleanup *close_cleanup;
3474*ef5ccd6cSJohn Marino   size_t buf_alloc, buf_pos;
3475*ef5ccd6cSJohn Marino   gdb_byte *buf;
3476*ef5ccd6cSJohn Marino   LONGEST n;
3477*ef5ccd6cSJohn Marino   int fd;
3478*ef5ccd6cSJohn Marino   int target_errno;
3479*ef5ccd6cSJohn Marino 
3480*ef5ccd6cSJohn Marino   fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
3481*ef5ccd6cSJohn Marino   if (fd == -1)
3482*ef5ccd6cSJohn Marino     return -1;
3483*ef5ccd6cSJohn Marino 
3484*ef5ccd6cSJohn Marino   close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
3485*ef5ccd6cSJohn Marino 
3486*ef5ccd6cSJohn Marino   /* Start by reading up to 4K at a time.  The target will throttle
3487*ef5ccd6cSJohn Marino      this number down if necessary.  */
3488*ef5ccd6cSJohn Marino   buf_alloc = 4096;
3489*ef5ccd6cSJohn Marino   buf = xmalloc (buf_alloc);
3490*ef5ccd6cSJohn Marino   buf_pos = 0;
3491*ef5ccd6cSJohn Marino   while (1)
3492*ef5ccd6cSJohn Marino     {
3493*ef5ccd6cSJohn Marino       n = target_fileio_pread (fd, &buf[buf_pos],
3494*ef5ccd6cSJohn Marino 			       buf_alloc - buf_pos - padding, buf_pos,
3495*ef5ccd6cSJohn Marino 			       &target_errno);
3496*ef5ccd6cSJohn Marino       if (n < 0)
3497*ef5ccd6cSJohn Marino 	{
3498*ef5ccd6cSJohn Marino 	  /* An error occurred.  */
3499*ef5ccd6cSJohn Marino 	  do_cleanups (close_cleanup);
3500*ef5ccd6cSJohn Marino 	  xfree (buf);
3501*ef5ccd6cSJohn Marino 	  return -1;
3502*ef5ccd6cSJohn Marino 	}
3503*ef5ccd6cSJohn Marino       else if (n == 0)
3504*ef5ccd6cSJohn Marino 	{
3505*ef5ccd6cSJohn Marino 	  /* Read all there was.  */
3506*ef5ccd6cSJohn Marino 	  do_cleanups (close_cleanup);
3507*ef5ccd6cSJohn Marino 	  if (buf_pos == 0)
3508*ef5ccd6cSJohn Marino 	    xfree (buf);
3509*ef5ccd6cSJohn Marino 	  else
3510*ef5ccd6cSJohn Marino 	    *buf_p = buf;
3511*ef5ccd6cSJohn Marino 	  return buf_pos;
3512*ef5ccd6cSJohn Marino 	}
3513*ef5ccd6cSJohn Marino 
3514*ef5ccd6cSJohn Marino       buf_pos += n;
3515*ef5ccd6cSJohn Marino 
3516*ef5ccd6cSJohn Marino       /* If the buffer is filling up, expand it.  */
3517*ef5ccd6cSJohn Marino       if (buf_alloc < buf_pos * 2)
3518*ef5ccd6cSJohn Marino 	{
3519*ef5ccd6cSJohn Marino 	  buf_alloc *= 2;
3520*ef5ccd6cSJohn Marino 	  buf = xrealloc (buf, buf_alloc);
3521*ef5ccd6cSJohn Marino 	}
3522*ef5ccd6cSJohn Marino 
3523*ef5ccd6cSJohn Marino       QUIT;
3524*ef5ccd6cSJohn Marino     }
3525*ef5ccd6cSJohn Marino }
3526*ef5ccd6cSJohn Marino 
3527*ef5ccd6cSJohn Marino /* Read target file FILENAME.  Store the result in *BUF_P and return
3528*ef5ccd6cSJohn Marino    the size of the transferred data.  See the declaration in "target.h"
3529*ef5ccd6cSJohn Marino    function for more information about the return value.  */
3530*ef5ccd6cSJohn Marino 
3531*ef5ccd6cSJohn Marino LONGEST
target_fileio_read_alloc(const char * filename,gdb_byte ** buf_p)3532*ef5ccd6cSJohn Marino target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
3533*ef5ccd6cSJohn Marino {
3534*ef5ccd6cSJohn Marino   return target_fileio_read_alloc_1 (filename, buf_p, 0);
3535*ef5ccd6cSJohn Marino }
3536*ef5ccd6cSJohn Marino 
3537*ef5ccd6cSJohn Marino /* Read target file FILENAME.  The result is NUL-terminated and
3538*ef5ccd6cSJohn Marino    returned as a string, allocated using xmalloc.  If an error occurs
3539*ef5ccd6cSJohn Marino    or the transfer is unsupported, NULL is returned.  Empty objects
3540*ef5ccd6cSJohn Marino    are returned as allocated but empty strings.  A warning is issued
3541*ef5ccd6cSJohn Marino    if the result contains any embedded NUL bytes.  */
3542*ef5ccd6cSJohn Marino 
3543*ef5ccd6cSJohn Marino char *
target_fileio_read_stralloc(const char * filename)3544*ef5ccd6cSJohn Marino target_fileio_read_stralloc (const char *filename)
3545*ef5ccd6cSJohn Marino {
3546*ef5ccd6cSJohn Marino   gdb_byte *buffer;
3547*ef5ccd6cSJohn Marino   char *bufstr;
3548*ef5ccd6cSJohn Marino   LONGEST i, transferred;
3549*ef5ccd6cSJohn Marino 
3550*ef5ccd6cSJohn Marino   transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
3551*ef5ccd6cSJohn Marino   bufstr = (char *) buffer;
3552*ef5ccd6cSJohn Marino 
3553*ef5ccd6cSJohn Marino   if (transferred < 0)
3554*ef5ccd6cSJohn Marino     return NULL;
3555*ef5ccd6cSJohn Marino 
3556*ef5ccd6cSJohn Marino   if (transferred == 0)
3557*ef5ccd6cSJohn Marino     return xstrdup ("");
3558*ef5ccd6cSJohn Marino 
3559*ef5ccd6cSJohn Marino   bufstr[transferred] = 0;
3560*ef5ccd6cSJohn Marino 
3561*ef5ccd6cSJohn Marino   /* Check for embedded NUL bytes; but allow trailing NULs.  */
3562*ef5ccd6cSJohn Marino   for (i = strlen (bufstr); i < transferred; i++)
3563*ef5ccd6cSJohn Marino     if (bufstr[i] != 0)
3564*ef5ccd6cSJohn Marino       {
3565*ef5ccd6cSJohn Marino 	warning (_("target file %s "
3566*ef5ccd6cSJohn Marino 		   "contained unexpected null characters"),
3567*ef5ccd6cSJohn Marino 		 filename);
3568*ef5ccd6cSJohn Marino 	break;
3569*ef5ccd6cSJohn Marino       }
3570*ef5ccd6cSJohn Marino 
3571*ef5ccd6cSJohn Marino   return bufstr;
3572*ef5ccd6cSJohn Marino }
3573*ef5ccd6cSJohn Marino 
3574*ef5ccd6cSJohn Marino 
35755796c8dcSSimon Schubert static int
default_region_ok_for_hw_watchpoint(CORE_ADDR addr,int len)35765796c8dcSSimon Schubert default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
35775796c8dcSSimon Schubert {
3578*ef5ccd6cSJohn Marino   return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT);
35795796c8dcSSimon Schubert }
35805796c8dcSSimon Schubert 
35815796c8dcSSimon Schubert static int
default_watchpoint_addr_within_range(struct target_ops * target,CORE_ADDR addr,CORE_ADDR start,int length)35825796c8dcSSimon Schubert default_watchpoint_addr_within_range (struct target_ops *target,
35835796c8dcSSimon Schubert 				      CORE_ADDR addr,
35845796c8dcSSimon Schubert 				      CORE_ADDR start, int length)
35855796c8dcSSimon Schubert {
35865796c8dcSSimon Schubert   return addr >= start && addr < start + length;
35875796c8dcSSimon Schubert }
35885796c8dcSSimon Schubert 
35895796c8dcSSimon Schubert static struct gdbarch *
default_thread_architecture(struct target_ops * ops,ptid_t ptid)35905796c8dcSSimon Schubert default_thread_architecture (struct target_ops *ops, ptid_t ptid)
35915796c8dcSSimon Schubert {
3592*ef5ccd6cSJohn Marino   return target_gdbarch ();
35935796c8dcSSimon Schubert }
35945796c8dcSSimon Schubert 
35955796c8dcSSimon Schubert static int
return_zero(void)35965796c8dcSSimon Schubert return_zero (void)
35975796c8dcSSimon Schubert {
35985796c8dcSSimon Schubert   return 0;
35995796c8dcSSimon Schubert }
36005796c8dcSSimon Schubert 
36015796c8dcSSimon Schubert static int
return_one(void)36025796c8dcSSimon Schubert return_one (void)
36035796c8dcSSimon Schubert {
36045796c8dcSSimon Schubert   return 1;
36055796c8dcSSimon Schubert }
36065796c8dcSSimon Schubert 
36075796c8dcSSimon Schubert static int
return_minus_one(void)36085796c8dcSSimon Schubert return_minus_one (void)
36095796c8dcSSimon Schubert {
36105796c8dcSSimon Schubert   return -1;
36115796c8dcSSimon Schubert }
36125796c8dcSSimon Schubert 
36135796c8dcSSimon Schubert /* Find a single runnable target in the stack and return it.  If for
36145796c8dcSSimon Schubert    some reason there is more than one, return NULL.  */
36155796c8dcSSimon Schubert 
36165796c8dcSSimon Schubert struct target_ops *
find_run_target(void)36175796c8dcSSimon Schubert find_run_target (void)
36185796c8dcSSimon Schubert {
36195796c8dcSSimon Schubert   struct target_ops **t;
36205796c8dcSSimon Schubert   struct target_ops *runable = NULL;
36215796c8dcSSimon Schubert   int count;
36225796c8dcSSimon Schubert 
36235796c8dcSSimon Schubert   count = 0;
36245796c8dcSSimon Schubert 
36255796c8dcSSimon Schubert   for (t = target_structs; t < target_structs + target_struct_size; ++t)
36265796c8dcSSimon Schubert     {
36275796c8dcSSimon Schubert       if ((*t)->to_can_run && target_can_run (*t))
36285796c8dcSSimon Schubert 	{
36295796c8dcSSimon Schubert 	  runable = *t;
36305796c8dcSSimon Schubert 	  ++count;
36315796c8dcSSimon Schubert 	}
36325796c8dcSSimon Schubert     }
36335796c8dcSSimon Schubert 
36345796c8dcSSimon Schubert   return (count == 1 ? runable : NULL);
36355796c8dcSSimon Schubert }
36365796c8dcSSimon Schubert 
36375796c8dcSSimon Schubert /*
36385796c8dcSSimon Schubert  * Find the next target down the stack from the specified target.
36395796c8dcSSimon Schubert  */
36405796c8dcSSimon Schubert 
36415796c8dcSSimon Schubert struct target_ops *
find_target_beneath(struct target_ops * t)36425796c8dcSSimon Schubert find_target_beneath (struct target_ops *t)
36435796c8dcSSimon Schubert {
36445796c8dcSSimon Schubert   return t->beneath;
36455796c8dcSSimon Schubert }
36465796c8dcSSimon Schubert 
36475796c8dcSSimon Schubert 
36485796c8dcSSimon Schubert /* The inferior process has died.  Long live the inferior!  */
36495796c8dcSSimon Schubert 
36505796c8dcSSimon Schubert void
generic_mourn_inferior(void)36515796c8dcSSimon Schubert generic_mourn_inferior (void)
36525796c8dcSSimon Schubert {
36535796c8dcSSimon Schubert   ptid_t ptid;
36545796c8dcSSimon Schubert 
36555796c8dcSSimon Schubert   ptid = inferior_ptid;
36565796c8dcSSimon Schubert   inferior_ptid = null_ptid;
36575796c8dcSSimon Schubert 
3658*ef5ccd6cSJohn Marino   /* Mark breakpoints uninserted in case something tries to delete a
3659*ef5ccd6cSJohn Marino      breakpoint while we delete the inferior's threads (which would
3660*ef5ccd6cSJohn Marino      fail, since the inferior is long gone).  */
3661*ef5ccd6cSJohn Marino   mark_breakpoints_out ();
3662*ef5ccd6cSJohn Marino 
36635796c8dcSSimon Schubert   if (!ptid_equal (ptid, null_ptid))
36645796c8dcSSimon Schubert     {
36655796c8dcSSimon Schubert       int pid = ptid_get_pid (ptid);
3666cf7f2e2dSJohn Marino       exit_inferior (pid);
36675796c8dcSSimon Schubert     }
36685796c8dcSSimon Schubert 
3669*ef5ccd6cSJohn Marino   /* Note this wipes step-resume breakpoints, so needs to be done
3670*ef5ccd6cSJohn Marino      after exit_inferior, which ends up referencing the step-resume
3671*ef5ccd6cSJohn Marino      breakpoints through clear_thread_inferior_resources.  */
36725796c8dcSSimon Schubert   breakpoint_init_inferior (inf_exited);
3673*ef5ccd6cSJohn Marino 
36745796c8dcSSimon Schubert   registers_changed ();
36755796c8dcSSimon Schubert 
36765796c8dcSSimon Schubert   reopen_exec_file ();
36775796c8dcSSimon Schubert   reinit_frame_cache ();
36785796c8dcSSimon Schubert 
36795796c8dcSSimon Schubert   if (deprecated_detach_hook)
36805796c8dcSSimon Schubert     deprecated_detach_hook ();
36815796c8dcSSimon Schubert }
36825796c8dcSSimon Schubert 
36835796c8dcSSimon Schubert /* Convert a normal process ID to a string.  Returns the string in a
36845796c8dcSSimon Schubert    static buffer.  */
36855796c8dcSSimon Schubert 
36865796c8dcSSimon Schubert char *
normal_pid_to_str(ptid_t ptid)36875796c8dcSSimon Schubert normal_pid_to_str (ptid_t ptid)
36885796c8dcSSimon Schubert {
36895796c8dcSSimon Schubert   static char buf[32];
36905796c8dcSSimon Schubert 
36915796c8dcSSimon Schubert   xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
36925796c8dcSSimon Schubert   return buf;
36935796c8dcSSimon Schubert }
36945796c8dcSSimon Schubert 
36955796c8dcSSimon Schubert static char *
dummy_pid_to_str(struct target_ops * ops,ptid_t ptid)36965796c8dcSSimon Schubert dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
36975796c8dcSSimon Schubert {
36985796c8dcSSimon Schubert   return normal_pid_to_str (ptid);
36995796c8dcSSimon Schubert }
37005796c8dcSSimon Schubert 
3701cf7f2e2dSJohn Marino /* Error-catcher for target_find_memory_regions.  */
3702cf7f2e2dSJohn Marino static int
dummy_find_memory_regions(find_memory_region_ftype ignore1,void * ignore2)3703c50c785cSJohn Marino dummy_find_memory_regions (find_memory_region_ftype ignore1, void *ignore2)
37045796c8dcSSimon Schubert {
3705cf7f2e2dSJohn Marino   error (_("Command not implemented for this target."));
37065796c8dcSSimon Schubert   return 0;
37075796c8dcSSimon Schubert }
37085796c8dcSSimon Schubert 
3709cf7f2e2dSJohn Marino /* Error-catcher for target_make_corefile_notes.  */
3710cf7f2e2dSJohn Marino static char *
dummy_make_corefile_notes(bfd * ignore1,int * ignore2)3711cf7f2e2dSJohn Marino dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
37125796c8dcSSimon Schubert {
3713cf7f2e2dSJohn Marino   error (_("Command not implemented for this target."));
37145796c8dcSSimon Schubert   return NULL;
37155796c8dcSSimon Schubert }
37165796c8dcSSimon Schubert 
3717cf7f2e2dSJohn Marino /* Error-catcher for target_get_bookmark.  */
3718cf7f2e2dSJohn Marino static gdb_byte *
dummy_get_bookmark(char * ignore1,int ignore2)3719cf7f2e2dSJohn Marino dummy_get_bookmark (char *ignore1, int ignore2)
3720cf7f2e2dSJohn Marino {
3721cf7f2e2dSJohn Marino   tcomplain ();
3722cf7f2e2dSJohn Marino   return NULL;
3723cf7f2e2dSJohn Marino }
3724cf7f2e2dSJohn Marino 
3725cf7f2e2dSJohn Marino /* Error-catcher for target_goto_bookmark.  */
3726cf7f2e2dSJohn Marino static void
dummy_goto_bookmark(gdb_byte * ignore,int from_tty)3727cf7f2e2dSJohn Marino dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
3728cf7f2e2dSJohn Marino {
3729cf7f2e2dSJohn Marino   tcomplain ();
3730cf7f2e2dSJohn Marino }
3731cf7f2e2dSJohn Marino 
37325796c8dcSSimon Schubert /* Set up the handful of non-empty slots needed by the dummy target
37335796c8dcSSimon Schubert    vector.  */
37345796c8dcSSimon Schubert 
37355796c8dcSSimon Schubert static void
init_dummy_target(void)37365796c8dcSSimon Schubert init_dummy_target (void)
37375796c8dcSSimon Schubert {
37385796c8dcSSimon Schubert   dummy_target.to_shortname = "None";
37395796c8dcSSimon Schubert   dummy_target.to_longname = "None";
37405796c8dcSSimon Schubert   dummy_target.to_doc = "";
37415796c8dcSSimon Schubert   dummy_target.to_attach = find_default_attach;
37425796c8dcSSimon Schubert   dummy_target.to_detach =
37435796c8dcSSimon Schubert     (void (*)(struct target_ops *, char *, int))target_ignore;
37445796c8dcSSimon Schubert   dummy_target.to_create_inferior = find_default_create_inferior;
37455796c8dcSSimon Schubert   dummy_target.to_can_async_p = find_default_can_async_p;
37465796c8dcSSimon Schubert   dummy_target.to_is_async_p = find_default_is_async_p;
37475796c8dcSSimon Schubert   dummy_target.to_supports_non_stop = find_default_supports_non_stop;
3748a45ae5f8SJohn Marino   dummy_target.to_supports_disable_randomization
3749a45ae5f8SJohn Marino     = find_default_supports_disable_randomization;
37505796c8dcSSimon Schubert   dummy_target.to_pid_to_str = dummy_pid_to_str;
37515796c8dcSSimon Schubert   dummy_target.to_stratum = dummy_stratum;
37525796c8dcSSimon Schubert   dummy_target.to_find_memory_regions = dummy_find_memory_regions;
37535796c8dcSSimon Schubert   dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
3754cf7f2e2dSJohn Marino   dummy_target.to_get_bookmark = dummy_get_bookmark;
3755cf7f2e2dSJohn Marino   dummy_target.to_goto_bookmark = dummy_goto_bookmark;
37565796c8dcSSimon Schubert   dummy_target.to_xfer_partial = default_xfer_partial;
37575796c8dcSSimon Schubert   dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
37585796c8dcSSimon Schubert   dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
37595796c8dcSSimon Schubert   dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
37605796c8dcSSimon Schubert   dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
3761c50c785cSJohn Marino   dummy_target.to_has_execution
3762c50c785cSJohn Marino     = (int (*) (struct target_ops *, ptid_t)) return_zero;
3763cf7f2e2dSJohn Marino   dummy_target.to_stopped_by_watchpoint = return_zero;
3764cf7f2e2dSJohn Marino   dummy_target.to_stopped_data_address =
3765cf7f2e2dSJohn Marino     (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
37665796c8dcSSimon Schubert   dummy_target.to_magic = OPS_MAGIC;
37675796c8dcSSimon Schubert }
37685796c8dcSSimon Schubert 
37695796c8dcSSimon Schubert static void
debug_to_open(char * args,int from_tty)37705796c8dcSSimon Schubert debug_to_open (char *args, int from_tty)
37715796c8dcSSimon Schubert {
37725796c8dcSSimon Schubert   debug_target.to_open (args, from_tty);
37735796c8dcSSimon Schubert 
37745796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
37755796c8dcSSimon Schubert }
37765796c8dcSSimon Schubert 
37775796c8dcSSimon Schubert void
target_close(struct target_ops * targ,int quitting)37785796c8dcSSimon Schubert target_close (struct target_ops *targ, int quitting)
37795796c8dcSSimon Schubert {
37805796c8dcSSimon Schubert   if (targ->to_xclose != NULL)
37815796c8dcSSimon Schubert     targ->to_xclose (targ, quitting);
37825796c8dcSSimon Schubert   else if (targ->to_close != NULL)
37835796c8dcSSimon Schubert     targ->to_close (quitting);
37845796c8dcSSimon Schubert 
37855796c8dcSSimon Schubert   if (targetdebug)
37865796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
37875796c8dcSSimon Schubert }
37885796c8dcSSimon Schubert 
37895796c8dcSSimon Schubert void
target_attach(char * args,int from_tty)37905796c8dcSSimon Schubert target_attach (char *args, int from_tty)
37915796c8dcSSimon Schubert {
37925796c8dcSSimon Schubert   struct target_ops *t;
3793cf7f2e2dSJohn Marino 
37945796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
37955796c8dcSSimon Schubert     {
37965796c8dcSSimon Schubert       if (t->to_attach != NULL)
37975796c8dcSSimon Schubert 	{
37985796c8dcSSimon Schubert 	  t->to_attach (t, args, from_tty);
37995796c8dcSSimon Schubert 	  if (targetdebug)
38005796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
38015796c8dcSSimon Schubert 				args, from_tty);
38025796c8dcSSimon Schubert 	  return;
38035796c8dcSSimon Schubert 	}
38045796c8dcSSimon Schubert     }
38055796c8dcSSimon Schubert 
38065796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
3807c50c785cSJohn Marino 		  _("could not find a target to attach"));
38085796c8dcSSimon Schubert }
38095796c8dcSSimon Schubert 
38105796c8dcSSimon Schubert int
target_thread_alive(ptid_t ptid)38115796c8dcSSimon Schubert target_thread_alive (ptid_t ptid)
38125796c8dcSSimon Schubert {
38135796c8dcSSimon Schubert   struct target_ops *t;
3814cf7f2e2dSJohn Marino 
38155796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
38165796c8dcSSimon Schubert     {
38175796c8dcSSimon Schubert       if (t->to_thread_alive != NULL)
38185796c8dcSSimon Schubert 	{
38195796c8dcSSimon Schubert 	  int retval;
38205796c8dcSSimon Schubert 
38215796c8dcSSimon Schubert 	  retval = t->to_thread_alive (t, ptid);
38225796c8dcSSimon Schubert 	  if (targetdebug)
38235796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
38245796c8dcSSimon Schubert 				PIDGET (ptid), retval);
38255796c8dcSSimon Schubert 
38265796c8dcSSimon Schubert 	  return retval;
38275796c8dcSSimon Schubert 	}
38285796c8dcSSimon Schubert     }
38295796c8dcSSimon Schubert 
38305796c8dcSSimon Schubert   return 0;
38315796c8dcSSimon Schubert }
38325796c8dcSSimon Schubert 
38335796c8dcSSimon Schubert void
target_find_new_threads(void)38345796c8dcSSimon Schubert target_find_new_threads (void)
38355796c8dcSSimon Schubert {
38365796c8dcSSimon Schubert   struct target_ops *t;
3837cf7f2e2dSJohn Marino 
38385796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
38395796c8dcSSimon Schubert     {
38405796c8dcSSimon Schubert       if (t->to_find_new_threads != NULL)
38415796c8dcSSimon Schubert 	{
38425796c8dcSSimon Schubert 	  t->to_find_new_threads (t);
38435796c8dcSSimon Schubert 	  if (targetdebug)
38445796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
38455796c8dcSSimon Schubert 
38465796c8dcSSimon Schubert 	  return;
38475796c8dcSSimon Schubert 	}
38485796c8dcSSimon Schubert     }
38495796c8dcSSimon Schubert }
38505796c8dcSSimon Schubert 
3851cf7f2e2dSJohn Marino void
target_stop(ptid_t ptid)3852cf7f2e2dSJohn Marino target_stop (ptid_t ptid)
3853cf7f2e2dSJohn Marino {
3854cf7f2e2dSJohn Marino   if (!may_stop)
3855cf7f2e2dSJohn Marino     {
3856cf7f2e2dSJohn Marino       warning (_("May not interrupt or stop the target, ignoring attempt"));
3857cf7f2e2dSJohn Marino       return;
3858cf7f2e2dSJohn Marino     }
3859cf7f2e2dSJohn Marino 
3860cf7f2e2dSJohn Marino   (*current_target.to_stop) (ptid);
3861cf7f2e2dSJohn Marino }
3862cf7f2e2dSJohn Marino 
38635796c8dcSSimon Schubert static void
debug_to_post_attach(int pid)38645796c8dcSSimon Schubert debug_to_post_attach (int pid)
38655796c8dcSSimon Schubert {
38665796c8dcSSimon Schubert   debug_target.to_post_attach (pid);
38675796c8dcSSimon Schubert 
38685796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
38695796c8dcSSimon Schubert }
38705796c8dcSSimon Schubert 
38715796c8dcSSimon Schubert /* Return a pretty printed form of target_waitstatus.
38725796c8dcSSimon Schubert    Space for the result is malloc'd, caller must free.  */
38735796c8dcSSimon Schubert 
38745796c8dcSSimon Schubert char *
target_waitstatus_to_string(const struct target_waitstatus * ws)38755796c8dcSSimon Schubert target_waitstatus_to_string (const struct target_waitstatus *ws)
38765796c8dcSSimon Schubert {
38775796c8dcSSimon Schubert   const char *kind_str = "status->kind = ";
38785796c8dcSSimon Schubert 
38795796c8dcSSimon Schubert   switch (ws->kind)
38805796c8dcSSimon Schubert     {
38815796c8dcSSimon Schubert     case TARGET_WAITKIND_EXITED:
38825796c8dcSSimon Schubert       return xstrprintf ("%sexited, status = %d",
38835796c8dcSSimon Schubert 			 kind_str, ws->value.integer);
38845796c8dcSSimon Schubert     case TARGET_WAITKIND_STOPPED:
38855796c8dcSSimon Schubert       return xstrprintf ("%sstopped, signal = %s",
3886*ef5ccd6cSJohn Marino 			 kind_str, gdb_signal_to_name (ws->value.sig));
38875796c8dcSSimon Schubert     case TARGET_WAITKIND_SIGNALLED:
38885796c8dcSSimon Schubert       return xstrprintf ("%ssignalled, signal = %s",
3889*ef5ccd6cSJohn Marino 			 kind_str, gdb_signal_to_name (ws->value.sig));
38905796c8dcSSimon Schubert     case TARGET_WAITKIND_LOADED:
38915796c8dcSSimon Schubert       return xstrprintf ("%sloaded", kind_str);
38925796c8dcSSimon Schubert     case TARGET_WAITKIND_FORKED:
38935796c8dcSSimon Schubert       return xstrprintf ("%sforked", kind_str);
38945796c8dcSSimon Schubert     case TARGET_WAITKIND_VFORKED:
38955796c8dcSSimon Schubert       return xstrprintf ("%svforked", kind_str);
38965796c8dcSSimon Schubert     case TARGET_WAITKIND_EXECD:
38975796c8dcSSimon Schubert       return xstrprintf ("%sexecd", kind_str);
3898*ef5ccd6cSJohn Marino     case TARGET_WAITKIND_VFORK_DONE:
3899*ef5ccd6cSJohn Marino       return xstrprintf ("%svfork-done", kind_str);
39005796c8dcSSimon Schubert     case TARGET_WAITKIND_SYSCALL_ENTRY:
39015796c8dcSSimon Schubert       return xstrprintf ("%sentered syscall", kind_str);
39025796c8dcSSimon Schubert     case TARGET_WAITKIND_SYSCALL_RETURN:
39035796c8dcSSimon Schubert       return xstrprintf ("%sexited syscall", kind_str);
39045796c8dcSSimon Schubert     case TARGET_WAITKIND_SPURIOUS:
39055796c8dcSSimon Schubert       return xstrprintf ("%sspurious", kind_str);
39065796c8dcSSimon Schubert     case TARGET_WAITKIND_IGNORE:
39075796c8dcSSimon Schubert       return xstrprintf ("%signore", kind_str);
39085796c8dcSSimon Schubert     case TARGET_WAITKIND_NO_HISTORY:
39095796c8dcSSimon Schubert       return xstrprintf ("%sno-history", kind_str);
3910a45ae5f8SJohn Marino     case TARGET_WAITKIND_NO_RESUMED:
3911a45ae5f8SJohn Marino       return xstrprintf ("%sno-resumed", kind_str);
39125796c8dcSSimon Schubert     default:
39135796c8dcSSimon Schubert       return xstrprintf ("%sunknown???", kind_str);
39145796c8dcSSimon Schubert     }
39155796c8dcSSimon Schubert }
39165796c8dcSSimon Schubert 
3917*ef5ccd6cSJohn Marino /* Concatenate ELEM to LIST, a comma separate list, and return the
3918*ef5ccd6cSJohn Marino    result.  The LIST incoming argument is released.  */
3919*ef5ccd6cSJohn Marino 
3920*ef5ccd6cSJohn Marino static char *
str_comma_list_concat_elem(char * list,const char * elem)3921*ef5ccd6cSJohn Marino str_comma_list_concat_elem (char *list, const char *elem)
3922*ef5ccd6cSJohn Marino {
3923*ef5ccd6cSJohn Marino   if (list == NULL)
3924*ef5ccd6cSJohn Marino     return xstrdup (elem);
3925*ef5ccd6cSJohn Marino   else
3926*ef5ccd6cSJohn Marino     return reconcat (list, list, ", ", elem, (char *) NULL);
3927*ef5ccd6cSJohn Marino }
3928*ef5ccd6cSJohn Marino 
3929*ef5ccd6cSJohn Marino /* Helper for target_options_to_string.  If OPT is present in
3930*ef5ccd6cSJohn Marino    TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3931*ef5ccd6cSJohn Marino    Returns the new resulting string.  OPT is removed from
3932*ef5ccd6cSJohn Marino    TARGET_OPTIONS.  */
3933*ef5ccd6cSJohn Marino 
3934*ef5ccd6cSJohn Marino static char *
do_option(int * target_options,char * ret,int opt,char * opt_str)3935*ef5ccd6cSJohn Marino do_option (int *target_options, char *ret,
3936*ef5ccd6cSJohn Marino 	   int opt, char *opt_str)
3937*ef5ccd6cSJohn Marino {
3938*ef5ccd6cSJohn Marino   if ((*target_options & opt) != 0)
3939*ef5ccd6cSJohn Marino     {
3940*ef5ccd6cSJohn Marino       ret = str_comma_list_concat_elem (ret, opt_str);
3941*ef5ccd6cSJohn Marino       *target_options &= ~opt;
3942*ef5ccd6cSJohn Marino     }
3943*ef5ccd6cSJohn Marino 
3944*ef5ccd6cSJohn Marino   return ret;
3945*ef5ccd6cSJohn Marino }
3946*ef5ccd6cSJohn Marino 
3947*ef5ccd6cSJohn Marino char *
target_options_to_string(int target_options)3948*ef5ccd6cSJohn Marino target_options_to_string (int target_options)
3949*ef5ccd6cSJohn Marino {
3950*ef5ccd6cSJohn Marino   char *ret = NULL;
3951*ef5ccd6cSJohn Marino 
3952*ef5ccd6cSJohn Marino #define DO_TARG_OPTION(OPT) \
3953*ef5ccd6cSJohn Marino   ret = do_option (&target_options, ret, OPT, #OPT)
3954*ef5ccd6cSJohn Marino 
3955*ef5ccd6cSJohn Marino   DO_TARG_OPTION (TARGET_WNOHANG);
3956*ef5ccd6cSJohn Marino 
3957*ef5ccd6cSJohn Marino   if (target_options != 0)
3958*ef5ccd6cSJohn Marino     ret = str_comma_list_concat_elem (ret, "unknown???");
3959*ef5ccd6cSJohn Marino 
3960*ef5ccd6cSJohn Marino   if (ret == NULL)
3961*ef5ccd6cSJohn Marino     ret = xstrdup ("");
3962*ef5ccd6cSJohn Marino   return ret;
3963*ef5ccd6cSJohn Marino }
3964*ef5ccd6cSJohn Marino 
39655796c8dcSSimon Schubert static void
debug_print_register(const char * func,struct regcache * regcache,int regno)39665796c8dcSSimon Schubert debug_print_register (const char * func,
39675796c8dcSSimon Schubert 		      struct regcache *regcache, int regno)
39685796c8dcSSimon Schubert {
39695796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3970cf7f2e2dSJohn Marino 
39715796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "%s ", func);
39725796c8dcSSimon Schubert   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
39735796c8dcSSimon Schubert       && gdbarch_register_name (gdbarch, regno) != NULL
39745796c8dcSSimon Schubert       && gdbarch_register_name (gdbarch, regno)[0] != '\0')
39755796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "(%s)",
39765796c8dcSSimon Schubert 			gdbarch_register_name (gdbarch, regno));
39775796c8dcSSimon Schubert   else
39785796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
39795796c8dcSSimon Schubert   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
39805796c8dcSSimon Schubert     {
39815796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
39825796c8dcSSimon Schubert       int i, size = register_size (gdbarch, regno);
3983*ef5ccd6cSJohn Marino       gdb_byte buf[MAX_REGISTER_SIZE];
3984cf7f2e2dSJohn Marino 
39855796c8dcSSimon Schubert       regcache_raw_collect (regcache, regno, buf);
39865796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, " = ");
39875796c8dcSSimon Schubert       for (i = 0; i < size; i++)
39885796c8dcSSimon Schubert 	{
39895796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
39905796c8dcSSimon Schubert 	}
39915796c8dcSSimon Schubert       if (size <= sizeof (LONGEST))
39925796c8dcSSimon Schubert 	{
39935796c8dcSSimon Schubert 	  ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
3994cf7f2e2dSJohn Marino 
39955796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " %s %s",
39965796c8dcSSimon Schubert 			      core_addr_to_string_nz (val), plongest (val));
39975796c8dcSSimon Schubert 	}
39985796c8dcSSimon Schubert     }
39995796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "\n");
40005796c8dcSSimon Schubert }
40015796c8dcSSimon Schubert 
40025796c8dcSSimon Schubert void
target_fetch_registers(struct regcache * regcache,int regno)40035796c8dcSSimon Schubert target_fetch_registers (struct regcache *regcache, int regno)
40045796c8dcSSimon Schubert {
40055796c8dcSSimon Schubert   struct target_ops *t;
4006cf7f2e2dSJohn Marino 
40075796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
40085796c8dcSSimon Schubert     {
40095796c8dcSSimon Schubert       if (t->to_fetch_registers != NULL)
40105796c8dcSSimon Schubert 	{
40115796c8dcSSimon Schubert 	  t->to_fetch_registers (t, regcache, regno);
40125796c8dcSSimon Schubert 	  if (targetdebug)
40135796c8dcSSimon Schubert 	    debug_print_register ("target_fetch_registers", regcache, regno);
40145796c8dcSSimon Schubert 	  return;
40155796c8dcSSimon Schubert 	}
40165796c8dcSSimon Schubert     }
40175796c8dcSSimon Schubert }
40185796c8dcSSimon Schubert 
40195796c8dcSSimon Schubert void
target_store_registers(struct regcache * regcache,int regno)40205796c8dcSSimon Schubert target_store_registers (struct regcache *regcache, int regno)
40215796c8dcSSimon Schubert {
40225796c8dcSSimon Schubert   struct target_ops *t;
4023cf7f2e2dSJohn Marino 
4024cf7f2e2dSJohn Marino   if (!may_write_registers)
4025cf7f2e2dSJohn Marino     error (_("Writing to registers is not allowed (regno %d)"), regno);
4026cf7f2e2dSJohn Marino 
40275796c8dcSSimon Schubert   for (t = current_target.beneath; t != NULL; t = t->beneath)
40285796c8dcSSimon Schubert     {
40295796c8dcSSimon Schubert       if (t->to_store_registers != NULL)
40305796c8dcSSimon Schubert 	{
40315796c8dcSSimon Schubert 	  t->to_store_registers (t, regcache, regno);
40325796c8dcSSimon Schubert 	  if (targetdebug)
40335796c8dcSSimon Schubert 	    {
40345796c8dcSSimon Schubert 	      debug_print_register ("target_store_registers", regcache, regno);
40355796c8dcSSimon Schubert 	    }
40365796c8dcSSimon Schubert 	  return;
40375796c8dcSSimon Schubert 	}
40385796c8dcSSimon Schubert     }
40395796c8dcSSimon Schubert 
40405796c8dcSSimon Schubert   noprocess ();
40415796c8dcSSimon Schubert }
40425796c8dcSSimon Schubert 
4043cf7f2e2dSJohn Marino int
target_core_of_thread(ptid_t ptid)4044cf7f2e2dSJohn Marino target_core_of_thread (ptid_t ptid)
4045cf7f2e2dSJohn Marino {
4046cf7f2e2dSJohn Marino   struct target_ops *t;
4047cf7f2e2dSJohn Marino 
4048cf7f2e2dSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4049cf7f2e2dSJohn Marino     {
4050cf7f2e2dSJohn Marino       if (t->to_core_of_thread != NULL)
4051cf7f2e2dSJohn Marino 	{
4052cf7f2e2dSJohn Marino 	  int retval = t->to_core_of_thread (t, ptid);
4053cf7f2e2dSJohn Marino 
4054cf7f2e2dSJohn Marino 	  if (targetdebug)
4055c50c785cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
4056c50c785cSJohn Marino 				"target_core_of_thread (%d) = %d\n",
4057cf7f2e2dSJohn Marino 				PIDGET (ptid), retval);
4058cf7f2e2dSJohn Marino 	  return retval;
4059cf7f2e2dSJohn Marino 	}
4060cf7f2e2dSJohn Marino     }
4061cf7f2e2dSJohn Marino 
4062cf7f2e2dSJohn Marino   return -1;
4063cf7f2e2dSJohn Marino }
4064cf7f2e2dSJohn Marino 
4065cf7f2e2dSJohn Marino int
target_verify_memory(const gdb_byte * data,CORE_ADDR memaddr,ULONGEST size)4066cf7f2e2dSJohn Marino target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
4067cf7f2e2dSJohn Marino {
4068cf7f2e2dSJohn Marino   struct target_ops *t;
4069cf7f2e2dSJohn Marino 
4070cf7f2e2dSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4071cf7f2e2dSJohn Marino     {
4072cf7f2e2dSJohn Marino       if (t->to_verify_memory != NULL)
4073cf7f2e2dSJohn Marino 	{
4074cf7f2e2dSJohn Marino 	  int retval = t->to_verify_memory (t, data, memaddr, size);
4075cf7f2e2dSJohn Marino 
4076cf7f2e2dSJohn Marino 	  if (targetdebug)
4077c50c785cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
4078c50c785cSJohn Marino 				"target_verify_memory (%s, %s) = %d\n",
4079*ef5ccd6cSJohn Marino 				paddress (target_gdbarch (), memaddr),
4080cf7f2e2dSJohn Marino 				pulongest (size),
4081cf7f2e2dSJohn Marino 				retval);
4082cf7f2e2dSJohn Marino 	  return retval;
4083cf7f2e2dSJohn Marino 	}
4084cf7f2e2dSJohn Marino     }
4085cf7f2e2dSJohn Marino 
4086cf7f2e2dSJohn Marino   tcomplain ();
4087cf7f2e2dSJohn Marino }
4088cf7f2e2dSJohn Marino 
4089a45ae5f8SJohn Marino /* The documentation for this function is in its prototype declaration in
4090a45ae5f8SJohn Marino    target.h.  */
4091a45ae5f8SJohn Marino 
4092a45ae5f8SJohn Marino int
target_insert_mask_watchpoint(CORE_ADDR addr,CORE_ADDR mask,int rw)4093a45ae5f8SJohn Marino target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
4094a45ae5f8SJohn Marino {
4095a45ae5f8SJohn Marino   struct target_ops *t;
4096a45ae5f8SJohn Marino 
4097a45ae5f8SJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4098a45ae5f8SJohn Marino     if (t->to_insert_mask_watchpoint != NULL)
4099a45ae5f8SJohn Marino       {
4100a45ae5f8SJohn Marino 	int ret;
4101a45ae5f8SJohn Marino 
4102a45ae5f8SJohn Marino 	ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
4103a45ae5f8SJohn Marino 
4104a45ae5f8SJohn Marino 	if (targetdebug)
4105a45ae5f8SJohn Marino 	  fprintf_unfiltered (gdb_stdlog, "\
4106a45ae5f8SJohn Marino target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
4107a45ae5f8SJohn Marino 			      core_addr_to_string (addr),
4108a45ae5f8SJohn Marino 			      core_addr_to_string (mask), rw, ret);
4109a45ae5f8SJohn Marino 
4110a45ae5f8SJohn Marino 	return ret;
4111a45ae5f8SJohn Marino       }
4112a45ae5f8SJohn Marino 
4113a45ae5f8SJohn Marino   return 1;
4114a45ae5f8SJohn Marino }
4115a45ae5f8SJohn Marino 
4116a45ae5f8SJohn Marino /* The documentation for this function is in its prototype declaration in
4117a45ae5f8SJohn Marino    target.h.  */
4118a45ae5f8SJohn Marino 
4119a45ae5f8SJohn Marino int
target_remove_mask_watchpoint(CORE_ADDR addr,CORE_ADDR mask,int rw)4120a45ae5f8SJohn Marino target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
4121a45ae5f8SJohn Marino {
4122a45ae5f8SJohn Marino   struct target_ops *t;
4123a45ae5f8SJohn Marino 
4124a45ae5f8SJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4125a45ae5f8SJohn Marino     if (t->to_remove_mask_watchpoint != NULL)
4126a45ae5f8SJohn Marino       {
4127a45ae5f8SJohn Marino 	int ret;
4128a45ae5f8SJohn Marino 
4129a45ae5f8SJohn Marino 	ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
4130a45ae5f8SJohn Marino 
4131a45ae5f8SJohn Marino 	if (targetdebug)
4132a45ae5f8SJohn Marino 	  fprintf_unfiltered (gdb_stdlog, "\
4133a45ae5f8SJohn Marino target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
4134a45ae5f8SJohn Marino 			      core_addr_to_string (addr),
4135a45ae5f8SJohn Marino 			      core_addr_to_string (mask), rw, ret);
4136a45ae5f8SJohn Marino 
4137a45ae5f8SJohn Marino 	return ret;
4138a45ae5f8SJohn Marino       }
4139a45ae5f8SJohn Marino 
4140a45ae5f8SJohn Marino   return 1;
4141a45ae5f8SJohn Marino }
4142a45ae5f8SJohn Marino 
4143a45ae5f8SJohn Marino /* The documentation for this function is in its prototype declaration
4144a45ae5f8SJohn Marino    in target.h.  */
4145a45ae5f8SJohn Marino 
4146a45ae5f8SJohn Marino int
target_masked_watch_num_registers(CORE_ADDR addr,CORE_ADDR mask)4147a45ae5f8SJohn Marino target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
4148a45ae5f8SJohn Marino {
4149a45ae5f8SJohn Marino   struct target_ops *t;
4150a45ae5f8SJohn Marino 
4151a45ae5f8SJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4152a45ae5f8SJohn Marino     if (t->to_masked_watch_num_registers != NULL)
4153a45ae5f8SJohn Marino       return t->to_masked_watch_num_registers (t, addr, mask);
4154a45ae5f8SJohn Marino 
4155a45ae5f8SJohn Marino   return -1;
4156a45ae5f8SJohn Marino }
4157a45ae5f8SJohn Marino 
4158c50c785cSJohn Marino /* The documentation for this function is in its prototype declaration
4159c50c785cSJohn Marino    in target.h.  */
4160c50c785cSJohn Marino 
4161c50c785cSJohn Marino int
target_ranged_break_num_registers(void)4162c50c785cSJohn Marino target_ranged_break_num_registers (void)
4163c50c785cSJohn Marino {
4164c50c785cSJohn Marino   struct target_ops *t;
4165c50c785cSJohn Marino 
4166c50c785cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4167c50c785cSJohn Marino     if (t->to_ranged_break_num_registers != NULL)
4168c50c785cSJohn Marino       return t->to_ranged_break_num_registers (t);
4169c50c785cSJohn Marino 
4170c50c785cSJohn Marino   return -1;
4171c50c785cSJohn Marino }
4172c50c785cSJohn Marino 
4173*ef5ccd6cSJohn Marino /* See target.h.  */
4174*ef5ccd6cSJohn Marino 
4175*ef5ccd6cSJohn Marino int
target_supports_btrace(void)4176*ef5ccd6cSJohn Marino target_supports_btrace (void)
4177*ef5ccd6cSJohn Marino {
4178*ef5ccd6cSJohn Marino   struct target_ops *t;
4179*ef5ccd6cSJohn Marino 
4180*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4181*ef5ccd6cSJohn Marino     if (t->to_supports_btrace != NULL)
4182*ef5ccd6cSJohn Marino       return t->to_supports_btrace ();
4183*ef5ccd6cSJohn Marino 
4184*ef5ccd6cSJohn Marino   return 0;
4185*ef5ccd6cSJohn Marino }
4186*ef5ccd6cSJohn Marino 
4187*ef5ccd6cSJohn Marino /* See target.h.  */
4188*ef5ccd6cSJohn Marino 
4189*ef5ccd6cSJohn Marino struct btrace_target_info *
target_enable_btrace(ptid_t ptid)4190*ef5ccd6cSJohn Marino target_enable_btrace (ptid_t ptid)
4191*ef5ccd6cSJohn Marino {
4192*ef5ccd6cSJohn Marino   struct target_ops *t;
4193*ef5ccd6cSJohn Marino 
4194*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4195*ef5ccd6cSJohn Marino     if (t->to_enable_btrace != NULL)
4196*ef5ccd6cSJohn Marino       return t->to_enable_btrace (ptid);
4197*ef5ccd6cSJohn Marino 
4198*ef5ccd6cSJohn Marino   tcomplain ();
4199*ef5ccd6cSJohn Marino   return NULL;
4200*ef5ccd6cSJohn Marino }
4201*ef5ccd6cSJohn Marino 
4202*ef5ccd6cSJohn Marino /* See target.h.  */
4203*ef5ccd6cSJohn Marino 
4204*ef5ccd6cSJohn Marino void
target_disable_btrace(struct btrace_target_info * btinfo)4205*ef5ccd6cSJohn Marino target_disable_btrace (struct btrace_target_info *btinfo)
4206*ef5ccd6cSJohn Marino {
4207*ef5ccd6cSJohn Marino   struct target_ops *t;
4208*ef5ccd6cSJohn Marino 
4209*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4210*ef5ccd6cSJohn Marino     if (t->to_disable_btrace != NULL)
4211*ef5ccd6cSJohn Marino       return t->to_disable_btrace (btinfo);
4212*ef5ccd6cSJohn Marino 
4213*ef5ccd6cSJohn Marino   tcomplain ();
4214*ef5ccd6cSJohn Marino }
4215*ef5ccd6cSJohn Marino 
4216*ef5ccd6cSJohn Marino /* See target.h.  */
4217*ef5ccd6cSJohn Marino 
4218*ef5ccd6cSJohn Marino void
target_teardown_btrace(struct btrace_target_info * btinfo)4219*ef5ccd6cSJohn Marino target_teardown_btrace (struct btrace_target_info *btinfo)
4220*ef5ccd6cSJohn Marino {
4221*ef5ccd6cSJohn Marino   struct target_ops *t;
4222*ef5ccd6cSJohn Marino 
4223*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4224*ef5ccd6cSJohn Marino     if (t->to_teardown_btrace != NULL)
4225*ef5ccd6cSJohn Marino       return t->to_teardown_btrace (btinfo);
4226*ef5ccd6cSJohn Marino 
4227*ef5ccd6cSJohn Marino   tcomplain ();
4228*ef5ccd6cSJohn Marino }
4229*ef5ccd6cSJohn Marino 
4230*ef5ccd6cSJohn Marino /* See target.h.  */
4231*ef5ccd6cSJohn Marino 
VEC(btrace_block_s)4232*ef5ccd6cSJohn Marino VEC (btrace_block_s) *
4233*ef5ccd6cSJohn Marino target_read_btrace (struct btrace_target_info *btinfo,
4234*ef5ccd6cSJohn Marino 		    enum btrace_read_type type)
4235*ef5ccd6cSJohn Marino {
4236*ef5ccd6cSJohn Marino   struct target_ops *t;
4237*ef5ccd6cSJohn Marino 
4238*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4239*ef5ccd6cSJohn Marino     if (t->to_read_btrace != NULL)
4240*ef5ccd6cSJohn Marino       return t->to_read_btrace (btinfo, type);
4241*ef5ccd6cSJohn Marino 
4242*ef5ccd6cSJohn Marino   tcomplain ();
4243*ef5ccd6cSJohn Marino   return NULL;
4244*ef5ccd6cSJohn Marino }
4245*ef5ccd6cSJohn Marino 
4246*ef5ccd6cSJohn Marino /* See target.h.  */
4247*ef5ccd6cSJohn Marino 
4248*ef5ccd6cSJohn Marino void
target_stop_recording(void)4249*ef5ccd6cSJohn Marino target_stop_recording (void)
4250*ef5ccd6cSJohn Marino {
4251*ef5ccd6cSJohn Marino   struct target_ops *t;
4252*ef5ccd6cSJohn Marino 
4253*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4254*ef5ccd6cSJohn Marino     if (t->to_stop_recording != NULL)
4255*ef5ccd6cSJohn Marino       {
4256*ef5ccd6cSJohn Marino 	t->to_stop_recording ();
4257*ef5ccd6cSJohn Marino 	return;
4258*ef5ccd6cSJohn Marino       }
4259*ef5ccd6cSJohn Marino 
4260*ef5ccd6cSJohn Marino   /* This is optional.  */
4261*ef5ccd6cSJohn Marino }
4262*ef5ccd6cSJohn Marino 
4263*ef5ccd6cSJohn Marino /* See target.h.  */
4264*ef5ccd6cSJohn Marino 
4265*ef5ccd6cSJohn Marino void
target_info_record(void)4266*ef5ccd6cSJohn Marino target_info_record (void)
4267*ef5ccd6cSJohn Marino {
4268*ef5ccd6cSJohn Marino   struct target_ops *t;
4269*ef5ccd6cSJohn Marino 
4270*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4271*ef5ccd6cSJohn Marino     if (t->to_info_record != NULL)
4272*ef5ccd6cSJohn Marino       {
4273*ef5ccd6cSJohn Marino 	t->to_info_record ();
4274*ef5ccd6cSJohn Marino 	return;
4275*ef5ccd6cSJohn Marino       }
4276*ef5ccd6cSJohn Marino 
4277*ef5ccd6cSJohn Marino   tcomplain ();
4278*ef5ccd6cSJohn Marino }
4279*ef5ccd6cSJohn Marino 
4280*ef5ccd6cSJohn Marino /* See target.h.  */
4281*ef5ccd6cSJohn Marino 
4282*ef5ccd6cSJohn Marino void
target_save_record(char * filename)4283*ef5ccd6cSJohn Marino target_save_record (char *filename)
4284*ef5ccd6cSJohn Marino {
4285*ef5ccd6cSJohn Marino   struct target_ops *t;
4286*ef5ccd6cSJohn Marino 
4287*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4288*ef5ccd6cSJohn Marino     if (t->to_save_record != NULL)
4289*ef5ccd6cSJohn Marino       {
4290*ef5ccd6cSJohn Marino 	t->to_save_record (filename);
4291*ef5ccd6cSJohn Marino 	return;
4292*ef5ccd6cSJohn Marino       }
4293*ef5ccd6cSJohn Marino 
4294*ef5ccd6cSJohn Marino   tcomplain ();
4295*ef5ccd6cSJohn Marino }
4296*ef5ccd6cSJohn Marino 
4297*ef5ccd6cSJohn Marino /* See target.h.  */
4298*ef5ccd6cSJohn Marino 
4299*ef5ccd6cSJohn Marino int
target_supports_delete_record(void)4300*ef5ccd6cSJohn Marino target_supports_delete_record (void)
4301*ef5ccd6cSJohn Marino {
4302*ef5ccd6cSJohn Marino   struct target_ops *t;
4303*ef5ccd6cSJohn Marino 
4304*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4305*ef5ccd6cSJohn Marino     if (t->to_delete_record != NULL)
4306*ef5ccd6cSJohn Marino       return 1;
4307*ef5ccd6cSJohn Marino 
4308*ef5ccd6cSJohn Marino   return 0;
4309*ef5ccd6cSJohn Marino }
4310*ef5ccd6cSJohn Marino 
4311*ef5ccd6cSJohn Marino /* See target.h.  */
4312*ef5ccd6cSJohn Marino 
4313*ef5ccd6cSJohn Marino void
target_delete_record(void)4314*ef5ccd6cSJohn Marino target_delete_record (void)
4315*ef5ccd6cSJohn Marino {
4316*ef5ccd6cSJohn Marino   struct target_ops *t;
4317*ef5ccd6cSJohn Marino 
4318*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4319*ef5ccd6cSJohn Marino     if (t->to_delete_record != NULL)
4320*ef5ccd6cSJohn Marino       {
4321*ef5ccd6cSJohn Marino 	t->to_delete_record ();
4322*ef5ccd6cSJohn Marino 	return;
4323*ef5ccd6cSJohn Marino       }
4324*ef5ccd6cSJohn Marino 
4325*ef5ccd6cSJohn Marino   tcomplain ();
4326*ef5ccd6cSJohn Marino }
4327*ef5ccd6cSJohn Marino 
4328*ef5ccd6cSJohn Marino /* See target.h.  */
4329*ef5ccd6cSJohn Marino 
4330*ef5ccd6cSJohn Marino int
target_record_is_replaying(void)4331*ef5ccd6cSJohn Marino target_record_is_replaying (void)
4332*ef5ccd6cSJohn Marino {
4333*ef5ccd6cSJohn Marino   struct target_ops *t;
4334*ef5ccd6cSJohn Marino 
4335*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4336*ef5ccd6cSJohn Marino     if (t->to_record_is_replaying != NULL)
4337*ef5ccd6cSJohn Marino 	return t->to_record_is_replaying ();
4338*ef5ccd6cSJohn Marino 
4339*ef5ccd6cSJohn Marino   return 0;
4340*ef5ccd6cSJohn Marino }
4341*ef5ccd6cSJohn Marino 
4342*ef5ccd6cSJohn Marino /* See target.h.  */
4343*ef5ccd6cSJohn Marino 
4344*ef5ccd6cSJohn Marino void
target_goto_record_begin(void)4345*ef5ccd6cSJohn Marino target_goto_record_begin (void)
4346*ef5ccd6cSJohn Marino {
4347*ef5ccd6cSJohn Marino   struct target_ops *t;
4348*ef5ccd6cSJohn Marino 
4349*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4350*ef5ccd6cSJohn Marino     if (t->to_goto_record_begin != NULL)
4351*ef5ccd6cSJohn Marino       {
4352*ef5ccd6cSJohn Marino 	t->to_goto_record_begin ();
4353*ef5ccd6cSJohn Marino 	return;
4354*ef5ccd6cSJohn Marino       }
4355*ef5ccd6cSJohn Marino 
4356*ef5ccd6cSJohn Marino   tcomplain ();
4357*ef5ccd6cSJohn Marino }
4358*ef5ccd6cSJohn Marino 
4359*ef5ccd6cSJohn Marino /* See target.h.  */
4360*ef5ccd6cSJohn Marino 
4361*ef5ccd6cSJohn Marino void
target_goto_record_end(void)4362*ef5ccd6cSJohn Marino target_goto_record_end (void)
4363*ef5ccd6cSJohn Marino {
4364*ef5ccd6cSJohn Marino   struct target_ops *t;
4365*ef5ccd6cSJohn Marino 
4366*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4367*ef5ccd6cSJohn Marino     if (t->to_goto_record_end != NULL)
4368*ef5ccd6cSJohn Marino       {
4369*ef5ccd6cSJohn Marino 	t->to_goto_record_end ();
4370*ef5ccd6cSJohn Marino 	return;
4371*ef5ccd6cSJohn Marino       }
4372*ef5ccd6cSJohn Marino 
4373*ef5ccd6cSJohn Marino   tcomplain ();
4374*ef5ccd6cSJohn Marino }
4375*ef5ccd6cSJohn Marino 
4376*ef5ccd6cSJohn Marino /* See target.h.  */
4377*ef5ccd6cSJohn Marino 
4378*ef5ccd6cSJohn Marino void
target_goto_record(ULONGEST insn)4379*ef5ccd6cSJohn Marino target_goto_record (ULONGEST insn)
4380*ef5ccd6cSJohn Marino {
4381*ef5ccd6cSJohn Marino   struct target_ops *t;
4382*ef5ccd6cSJohn Marino 
4383*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4384*ef5ccd6cSJohn Marino     if (t->to_goto_record != NULL)
4385*ef5ccd6cSJohn Marino       {
4386*ef5ccd6cSJohn Marino 	t->to_goto_record (insn);
4387*ef5ccd6cSJohn Marino 	return;
4388*ef5ccd6cSJohn Marino       }
4389*ef5ccd6cSJohn Marino 
4390*ef5ccd6cSJohn Marino   tcomplain ();
4391*ef5ccd6cSJohn Marino }
4392*ef5ccd6cSJohn Marino 
4393*ef5ccd6cSJohn Marino /* See target.h.  */
4394*ef5ccd6cSJohn Marino 
4395*ef5ccd6cSJohn Marino void
target_insn_history(int size,int flags)4396*ef5ccd6cSJohn Marino target_insn_history (int size, int flags)
4397*ef5ccd6cSJohn Marino {
4398*ef5ccd6cSJohn Marino   struct target_ops *t;
4399*ef5ccd6cSJohn Marino 
4400*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4401*ef5ccd6cSJohn Marino     if (t->to_insn_history != NULL)
4402*ef5ccd6cSJohn Marino       {
4403*ef5ccd6cSJohn Marino 	t->to_insn_history (size, flags);
4404*ef5ccd6cSJohn Marino 	return;
4405*ef5ccd6cSJohn Marino       }
4406*ef5ccd6cSJohn Marino 
4407*ef5ccd6cSJohn Marino   tcomplain ();
4408*ef5ccd6cSJohn Marino }
4409*ef5ccd6cSJohn Marino 
4410*ef5ccd6cSJohn Marino /* See target.h.  */
4411*ef5ccd6cSJohn Marino 
4412*ef5ccd6cSJohn Marino void
target_insn_history_from(ULONGEST from,int size,int flags)4413*ef5ccd6cSJohn Marino target_insn_history_from (ULONGEST from, int size, int flags)
4414*ef5ccd6cSJohn Marino {
4415*ef5ccd6cSJohn Marino   struct target_ops *t;
4416*ef5ccd6cSJohn Marino 
4417*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4418*ef5ccd6cSJohn Marino     if (t->to_insn_history_from != NULL)
4419*ef5ccd6cSJohn Marino       {
4420*ef5ccd6cSJohn Marino 	t->to_insn_history_from (from, size, flags);
4421*ef5ccd6cSJohn Marino 	return;
4422*ef5ccd6cSJohn Marino       }
4423*ef5ccd6cSJohn Marino 
4424*ef5ccd6cSJohn Marino   tcomplain ();
4425*ef5ccd6cSJohn Marino }
4426*ef5ccd6cSJohn Marino 
4427*ef5ccd6cSJohn Marino /* See target.h.  */
4428*ef5ccd6cSJohn Marino 
4429*ef5ccd6cSJohn Marino void
target_insn_history_range(ULONGEST begin,ULONGEST end,int flags)4430*ef5ccd6cSJohn Marino target_insn_history_range (ULONGEST begin, ULONGEST end, int flags)
4431*ef5ccd6cSJohn Marino {
4432*ef5ccd6cSJohn Marino   struct target_ops *t;
4433*ef5ccd6cSJohn Marino 
4434*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4435*ef5ccd6cSJohn Marino     if (t->to_insn_history_range != NULL)
4436*ef5ccd6cSJohn Marino       {
4437*ef5ccd6cSJohn Marino 	t->to_insn_history_range (begin, end, flags);
4438*ef5ccd6cSJohn Marino 	return;
4439*ef5ccd6cSJohn Marino       }
4440*ef5ccd6cSJohn Marino 
4441*ef5ccd6cSJohn Marino   tcomplain ();
4442*ef5ccd6cSJohn Marino }
4443*ef5ccd6cSJohn Marino 
4444*ef5ccd6cSJohn Marino /* See target.h.  */
4445*ef5ccd6cSJohn Marino 
4446*ef5ccd6cSJohn Marino void
target_call_history(int size,int flags)4447*ef5ccd6cSJohn Marino target_call_history (int size, int flags)
4448*ef5ccd6cSJohn Marino {
4449*ef5ccd6cSJohn Marino   struct target_ops *t;
4450*ef5ccd6cSJohn Marino 
4451*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4452*ef5ccd6cSJohn Marino     if (t->to_call_history != NULL)
4453*ef5ccd6cSJohn Marino       {
4454*ef5ccd6cSJohn Marino 	t->to_call_history (size, flags);
4455*ef5ccd6cSJohn Marino 	return;
4456*ef5ccd6cSJohn Marino       }
4457*ef5ccd6cSJohn Marino 
4458*ef5ccd6cSJohn Marino   tcomplain ();
4459*ef5ccd6cSJohn Marino }
4460*ef5ccd6cSJohn Marino 
4461*ef5ccd6cSJohn Marino /* See target.h.  */
4462*ef5ccd6cSJohn Marino 
4463*ef5ccd6cSJohn Marino void
target_call_history_from(ULONGEST begin,int size,int flags)4464*ef5ccd6cSJohn Marino target_call_history_from (ULONGEST begin, int size, int flags)
4465*ef5ccd6cSJohn Marino {
4466*ef5ccd6cSJohn Marino   struct target_ops *t;
4467*ef5ccd6cSJohn Marino 
4468*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4469*ef5ccd6cSJohn Marino     if (t->to_call_history_from != NULL)
4470*ef5ccd6cSJohn Marino       {
4471*ef5ccd6cSJohn Marino 	t->to_call_history_from (begin, size, flags);
4472*ef5ccd6cSJohn Marino 	return;
4473*ef5ccd6cSJohn Marino       }
4474*ef5ccd6cSJohn Marino 
4475*ef5ccd6cSJohn Marino   tcomplain ();
4476*ef5ccd6cSJohn Marino }
4477*ef5ccd6cSJohn Marino 
4478*ef5ccd6cSJohn Marino /* See target.h.  */
4479*ef5ccd6cSJohn Marino 
4480*ef5ccd6cSJohn Marino void
target_call_history_range(ULONGEST begin,ULONGEST end,int flags)4481*ef5ccd6cSJohn Marino target_call_history_range (ULONGEST begin, ULONGEST end, int flags)
4482*ef5ccd6cSJohn Marino {
4483*ef5ccd6cSJohn Marino   struct target_ops *t;
4484*ef5ccd6cSJohn Marino 
4485*ef5ccd6cSJohn Marino   for (t = current_target.beneath; t != NULL; t = t->beneath)
4486*ef5ccd6cSJohn Marino     if (t->to_call_history_range != NULL)
4487*ef5ccd6cSJohn Marino       {
4488*ef5ccd6cSJohn Marino 	t->to_call_history_range (begin, end, flags);
4489*ef5ccd6cSJohn Marino 	return;
4490*ef5ccd6cSJohn Marino       }
4491*ef5ccd6cSJohn Marino 
4492*ef5ccd6cSJohn Marino   tcomplain ();
4493*ef5ccd6cSJohn Marino }
4494*ef5ccd6cSJohn Marino 
44955796c8dcSSimon Schubert static void
debug_to_prepare_to_store(struct regcache * regcache)44965796c8dcSSimon Schubert debug_to_prepare_to_store (struct regcache *regcache)
44975796c8dcSSimon Schubert {
44985796c8dcSSimon Schubert   debug_target.to_prepare_to_store (regcache);
44995796c8dcSSimon Schubert 
45005796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
45015796c8dcSSimon Schubert }
45025796c8dcSSimon Schubert 
45035796c8dcSSimon Schubert static int
deprecated_debug_xfer_memory(CORE_ADDR memaddr,bfd_byte * myaddr,int len,int write,struct mem_attrib * attrib,struct target_ops * target)45045796c8dcSSimon Schubert deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
45055796c8dcSSimon Schubert 			      int write, struct mem_attrib *attrib,
45065796c8dcSSimon Schubert 			      struct target_ops *target)
45075796c8dcSSimon Schubert {
45085796c8dcSSimon Schubert   int retval;
45095796c8dcSSimon Schubert 
45105796c8dcSSimon Schubert   retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
45115796c8dcSSimon Schubert 						attrib, target);
45125796c8dcSSimon Schubert 
45135796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
45145796c8dcSSimon Schubert 		      "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
4515*ef5ccd6cSJohn Marino 		      paddress (target_gdbarch (), memaddr), len,
45165796c8dcSSimon Schubert 		      write ? "write" : "read", retval);
45175796c8dcSSimon Schubert 
45185796c8dcSSimon Schubert   if (retval > 0)
45195796c8dcSSimon Schubert     {
45205796c8dcSSimon Schubert       int i;
45215796c8dcSSimon Schubert 
45225796c8dcSSimon Schubert       fputs_unfiltered (", bytes =", gdb_stdlog);
45235796c8dcSSimon Schubert       for (i = 0; i < retval; i++)
45245796c8dcSSimon Schubert 	{
45255796c8dcSSimon Schubert 	  if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
45265796c8dcSSimon Schubert 	    {
45275796c8dcSSimon Schubert 	      if (targetdebug < 2 && i > 0)
45285796c8dcSSimon Schubert 		{
45295796c8dcSSimon Schubert 		  fprintf_unfiltered (gdb_stdlog, " ...");
45305796c8dcSSimon Schubert 		  break;
45315796c8dcSSimon Schubert 		}
45325796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "\n");
45335796c8dcSSimon Schubert 	    }
45345796c8dcSSimon Schubert 
45355796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
45365796c8dcSSimon Schubert 	}
45375796c8dcSSimon Schubert     }
45385796c8dcSSimon Schubert 
45395796c8dcSSimon Schubert   fputc_unfiltered ('\n', gdb_stdlog);
45405796c8dcSSimon Schubert 
45415796c8dcSSimon Schubert   return retval;
45425796c8dcSSimon Schubert }
45435796c8dcSSimon Schubert 
45445796c8dcSSimon Schubert static void
debug_to_files_info(struct target_ops * target)45455796c8dcSSimon Schubert debug_to_files_info (struct target_ops *target)
45465796c8dcSSimon Schubert {
45475796c8dcSSimon Schubert   debug_target.to_files_info (target);
45485796c8dcSSimon Schubert 
45495796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
45505796c8dcSSimon Schubert }
45515796c8dcSSimon Schubert 
45525796c8dcSSimon Schubert static int
debug_to_insert_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)45535796c8dcSSimon Schubert debug_to_insert_breakpoint (struct gdbarch *gdbarch,
45545796c8dcSSimon Schubert 			    struct bp_target_info *bp_tgt)
45555796c8dcSSimon Schubert {
45565796c8dcSSimon Schubert   int retval;
45575796c8dcSSimon Schubert 
45585796c8dcSSimon Schubert   retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
45595796c8dcSSimon Schubert 
45605796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4561cf7f2e2dSJohn Marino 		      "target_insert_breakpoint (%s, xxx) = %ld\n",
4562cf7f2e2dSJohn Marino 		      core_addr_to_string (bp_tgt->placed_address),
45635796c8dcSSimon Schubert 		      (unsigned long) retval);
45645796c8dcSSimon Schubert   return retval;
45655796c8dcSSimon Schubert }
45665796c8dcSSimon Schubert 
45675796c8dcSSimon Schubert static int
debug_to_remove_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)45685796c8dcSSimon Schubert debug_to_remove_breakpoint (struct gdbarch *gdbarch,
45695796c8dcSSimon Schubert 			    struct bp_target_info *bp_tgt)
45705796c8dcSSimon Schubert {
45715796c8dcSSimon Schubert   int retval;
45725796c8dcSSimon Schubert 
45735796c8dcSSimon Schubert   retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
45745796c8dcSSimon Schubert 
45755796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4576cf7f2e2dSJohn Marino 		      "target_remove_breakpoint (%s, xxx) = %ld\n",
4577cf7f2e2dSJohn Marino 		      core_addr_to_string (bp_tgt->placed_address),
45785796c8dcSSimon Schubert 		      (unsigned long) retval);
45795796c8dcSSimon Schubert   return retval;
45805796c8dcSSimon Schubert }
45815796c8dcSSimon Schubert 
45825796c8dcSSimon Schubert static int
debug_to_can_use_hw_breakpoint(int type,int cnt,int from_tty)45835796c8dcSSimon Schubert debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
45845796c8dcSSimon Schubert {
45855796c8dcSSimon Schubert   int retval;
45865796c8dcSSimon Schubert 
45875796c8dcSSimon Schubert   retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
45885796c8dcSSimon Schubert 
45895796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
45905796c8dcSSimon Schubert 		      "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
45915796c8dcSSimon Schubert 		      (unsigned long) type,
45925796c8dcSSimon Schubert 		      (unsigned long) cnt,
45935796c8dcSSimon Schubert 		      (unsigned long) from_tty,
45945796c8dcSSimon Schubert 		      (unsigned long) retval);
45955796c8dcSSimon Schubert   return retval;
45965796c8dcSSimon Schubert }
45975796c8dcSSimon Schubert 
45985796c8dcSSimon Schubert static int
debug_to_region_ok_for_hw_watchpoint(CORE_ADDR addr,int len)45995796c8dcSSimon Schubert debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
46005796c8dcSSimon Schubert {
46015796c8dcSSimon Schubert   CORE_ADDR retval;
46025796c8dcSSimon Schubert 
46035796c8dcSSimon Schubert   retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
46045796c8dcSSimon Schubert 
46055796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4606cf7f2e2dSJohn Marino 		      "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
4607cf7f2e2dSJohn Marino 		      core_addr_to_string (addr), (unsigned long) len,
4608cf7f2e2dSJohn Marino 		      core_addr_to_string (retval));
4609cf7f2e2dSJohn Marino   return retval;
4610cf7f2e2dSJohn Marino }
4611cf7f2e2dSJohn Marino 
4612cf7f2e2dSJohn Marino static int
debug_to_can_accel_watchpoint_condition(CORE_ADDR addr,int len,int rw,struct expression * cond)4613cf7f2e2dSJohn Marino debug_to_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
4614cf7f2e2dSJohn Marino 					 struct expression *cond)
4615cf7f2e2dSJohn Marino {
4616cf7f2e2dSJohn Marino   int retval;
4617cf7f2e2dSJohn Marino 
4618c50c785cSJohn Marino   retval = debug_target.to_can_accel_watchpoint_condition (addr, len,
4619c50c785cSJohn Marino 							   rw, cond);
4620cf7f2e2dSJohn Marino 
4621cf7f2e2dSJohn Marino   fprintf_unfiltered (gdb_stdlog,
4622c50c785cSJohn Marino 		      "target_can_accel_watchpoint_condition "
4623c50c785cSJohn Marino 		      "(%s, %d, %d, %s) = %ld\n",
4624cf7f2e2dSJohn Marino 		      core_addr_to_string (addr), len, rw,
4625cf7f2e2dSJohn Marino 		      host_address_to_string (cond), (unsigned long) retval);
46265796c8dcSSimon Schubert   return retval;
46275796c8dcSSimon Schubert }
46285796c8dcSSimon Schubert 
46295796c8dcSSimon Schubert static int
debug_to_stopped_by_watchpoint(void)46305796c8dcSSimon Schubert debug_to_stopped_by_watchpoint (void)
46315796c8dcSSimon Schubert {
46325796c8dcSSimon Schubert   int retval;
46335796c8dcSSimon Schubert 
46345796c8dcSSimon Schubert   retval = debug_target.to_stopped_by_watchpoint ();
46355796c8dcSSimon Schubert 
46365796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
46375796c8dcSSimon Schubert 		      "target_stopped_by_watchpoint () = %ld\n",
46385796c8dcSSimon Schubert 		      (unsigned long) retval);
46395796c8dcSSimon Schubert   return retval;
46405796c8dcSSimon Schubert }
46415796c8dcSSimon Schubert 
46425796c8dcSSimon Schubert static int
debug_to_stopped_data_address(struct target_ops * target,CORE_ADDR * addr)46435796c8dcSSimon Schubert debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
46445796c8dcSSimon Schubert {
46455796c8dcSSimon Schubert   int retval;
46465796c8dcSSimon Schubert 
46475796c8dcSSimon Schubert   retval = debug_target.to_stopped_data_address (target, addr);
46485796c8dcSSimon Schubert 
46495796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4650cf7f2e2dSJohn Marino 		      "target_stopped_data_address ([%s]) = %ld\n",
4651cf7f2e2dSJohn Marino 		      core_addr_to_string (*addr),
46525796c8dcSSimon Schubert 		      (unsigned long)retval);
46535796c8dcSSimon Schubert   return retval;
46545796c8dcSSimon Schubert }
46555796c8dcSSimon Schubert 
46565796c8dcSSimon Schubert static int
debug_to_watchpoint_addr_within_range(struct target_ops * target,CORE_ADDR addr,CORE_ADDR start,int length)46575796c8dcSSimon Schubert debug_to_watchpoint_addr_within_range (struct target_ops *target,
46585796c8dcSSimon Schubert 				       CORE_ADDR addr,
46595796c8dcSSimon Schubert 				       CORE_ADDR start, int length)
46605796c8dcSSimon Schubert {
46615796c8dcSSimon Schubert   int retval;
46625796c8dcSSimon Schubert 
46635796c8dcSSimon Schubert   retval = debug_target.to_watchpoint_addr_within_range (target, addr,
46645796c8dcSSimon Schubert 							 start, length);
46655796c8dcSSimon Schubert 
46665796c8dcSSimon Schubert   fprintf_filtered (gdb_stdlog,
4667cf7f2e2dSJohn Marino 		    "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
4668cf7f2e2dSJohn Marino 		    core_addr_to_string (addr), core_addr_to_string (start),
4669cf7f2e2dSJohn Marino 		    length, retval);
46705796c8dcSSimon Schubert   return retval;
46715796c8dcSSimon Schubert }
46725796c8dcSSimon Schubert 
46735796c8dcSSimon Schubert static int
debug_to_insert_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)46745796c8dcSSimon Schubert debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
46755796c8dcSSimon Schubert 			       struct bp_target_info *bp_tgt)
46765796c8dcSSimon Schubert {
46775796c8dcSSimon Schubert   int retval;
46785796c8dcSSimon Schubert 
46795796c8dcSSimon Schubert   retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
46805796c8dcSSimon Schubert 
46815796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4682cf7f2e2dSJohn Marino 		      "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
4683cf7f2e2dSJohn Marino 		      core_addr_to_string (bp_tgt->placed_address),
46845796c8dcSSimon Schubert 		      (unsigned long) retval);
46855796c8dcSSimon Schubert   return retval;
46865796c8dcSSimon Schubert }
46875796c8dcSSimon Schubert 
46885796c8dcSSimon Schubert static int
debug_to_remove_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)46895796c8dcSSimon Schubert debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
46905796c8dcSSimon Schubert 			       struct bp_target_info *bp_tgt)
46915796c8dcSSimon Schubert {
46925796c8dcSSimon Schubert   int retval;
46935796c8dcSSimon Schubert 
46945796c8dcSSimon Schubert   retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
46955796c8dcSSimon Schubert 
46965796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4697cf7f2e2dSJohn Marino 		      "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
4698cf7f2e2dSJohn Marino 		      core_addr_to_string (bp_tgt->placed_address),
46995796c8dcSSimon Schubert 		      (unsigned long) retval);
47005796c8dcSSimon Schubert   return retval;
47015796c8dcSSimon Schubert }
47025796c8dcSSimon Schubert 
47035796c8dcSSimon Schubert static int
debug_to_insert_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)4704cf7f2e2dSJohn Marino debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type,
4705cf7f2e2dSJohn Marino 			    struct expression *cond)
47065796c8dcSSimon Schubert {
47075796c8dcSSimon Schubert   int retval;
47085796c8dcSSimon Schubert 
4709cf7f2e2dSJohn Marino   retval = debug_target.to_insert_watchpoint (addr, len, type, cond);
47105796c8dcSSimon Schubert 
47115796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4712cf7f2e2dSJohn Marino 		      "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
4713cf7f2e2dSJohn Marino 		      core_addr_to_string (addr), len, type,
4714cf7f2e2dSJohn Marino 		      host_address_to_string (cond), (unsigned long) retval);
47155796c8dcSSimon Schubert   return retval;
47165796c8dcSSimon Schubert }
47175796c8dcSSimon Schubert 
47185796c8dcSSimon Schubert static int
debug_to_remove_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)4719cf7f2e2dSJohn Marino debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type,
4720cf7f2e2dSJohn Marino 			    struct expression *cond)
47215796c8dcSSimon Schubert {
47225796c8dcSSimon Schubert   int retval;
47235796c8dcSSimon Schubert 
4724cf7f2e2dSJohn Marino   retval = debug_target.to_remove_watchpoint (addr, len, type, cond);
47255796c8dcSSimon Schubert 
47265796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog,
4727cf7f2e2dSJohn Marino 		      "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
4728cf7f2e2dSJohn Marino 		      core_addr_to_string (addr), len, type,
4729cf7f2e2dSJohn Marino 		      host_address_to_string (cond), (unsigned long) retval);
47305796c8dcSSimon Schubert   return retval;
47315796c8dcSSimon Schubert }
47325796c8dcSSimon Schubert 
47335796c8dcSSimon Schubert static void
debug_to_terminal_init(void)47345796c8dcSSimon Schubert debug_to_terminal_init (void)
47355796c8dcSSimon Schubert {
47365796c8dcSSimon Schubert   debug_target.to_terminal_init ();
47375796c8dcSSimon Schubert 
47385796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
47395796c8dcSSimon Schubert }
47405796c8dcSSimon Schubert 
47415796c8dcSSimon Schubert static void
debug_to_terminal_inferior(void)47425796c8dcSSimon Schubert debug_to_terminal_inferior (void)
47435796c8dcSSimon Schubert {
47445796c8dcSSimon Schubert   debug_target.to_terminal_inferior ();
47455796c8dcSSimon Schubert 
47465796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
47475796c8dcSSimon Schubert }
47485796c8dcSSimon Schubert 
47495796c8dcSSimon Schubert static void
debug_to_terminal_ours_for_output(void)47505796c8dcSSimon Schubert debug_to_terminal_ours_for_output (void)
47515796c8dcSSimon Schubert {
47525796c8dcSSimon Schubert   debug_target.to_terminal_ours_for_output ();
47535796c8dcSSimon Schubert 
47545796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
47555796c8dcSSimon Schubert }
47565796c8dcSSimon Schubert 
47575796c8dcSSimon Schubert static void
debug_to_terminal_ours(void)47585796c8dcSSimon Schubert debug_to_terminal_ours (void)
47595796c8dcSSimon Schubert {
47605796c8dcSSimon Schubert   debug_target.to_terminal_ours ();
47615796c8dcSSimon Schubert 
47625796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
47635796c8dcSSimon Schubert }
47645796c8dcSSimon Schubert 
47655796c8dcSSimon Schubert static void
debug_to_terminal_save_ours(void)47665796c8dcSSimon Schubert debug_to_terminal_save_ours (void)
47675796c8dcSSimon Schubert {
47685796c8dcSSimon Schubert   debug_target.to_terminal_save_ours ();
47695796c8dcSSimon Schubert 
47705796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
47715796c8dcSSimon Schubert }
47725796c8dcSSimon Schubert 
47735796c8dcSSimon Schubert static void
debug_to_terminal_info(char * arg,int from_tty)47745796c8dcSSimon Schubert debug_to_terminal_info (char *arg, int from_tty)
47755796c8dcSSimon Schubert {
47765796c8dcSSimon Schubert   debug_target.to_terminal_info (arg, from_tty);
47775796c8dcSSimon Schubert 
47785796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
47795796c8dcSSimon Schubert 		      from_tty);
47805796c8dcSSimon Schubert }
47815796c8dcSSimon Schubert 
47825796c8dcSSimon Schubert static void
debug_to_load(char * args,int from_tty)47835796c8dcSSimon Schubert debug_to_load (char *args, int from_tty)
47845796c8dcSSimon Schubert {
47855796c8dcSSimon Schubert   debug_target.to_load (args, from_tty);
47865796c8dcSSimon Schubert 
47875796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
47885796c8dcSSimon Schubert }
47895796c8dcSSimon Schubert 
47905796c8dcSSimon Schubert static void
debug_to_post_startup_inferior(ptid_t ptid)47915796c8dcSSimon Schubert debug_to_post_startup_inferior (ptid_t ptid)
47925796c8dcSSimon Schubert {
47935796c8dcSSimon Schubert   debug_target.to_post_startup_inferior (ptid);
47945796c8dcSSimon Schubert 
47955796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
47965796c8dcSSimon Schubert 		      PIDGET (ptid));
47975796c8dcSSimon Schubert }
47985796c8dcSSimon Schubert 
4799c50c785cSJohn Marino static int
debug_to_insert_fork_catchpoint(int pid)48005796c8dcSSimon Schubert debug_to_insert_fork_catchpoint (int pid)
48015796c8dcSSimon Schubert {
4802c50c785cSJohn Marino   int retval;
48035796c8dcSSimon Schubert 
4804c50c785cSJohn Marino   retval = debug_target.to_insert_fork_catchpoint (pid);
4805c50c785cSJohn Marino 
4806c50c785cSJohn Marino   fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
4807c50c785cSJohn Marino 		      pid, retval);
4808c50c785cSJohn Marino 
4809c50c785cSJohn Marino   return retval;
48105796c8dcSSimon Schubert }
48115796c8dcSSimon Schubert 
48125796c8dcSSimon Schubert static int
debug_to_remove_fork_catchpoint(int pid)48135796c8dcSSimon Schubert debug_to_remove_fork_catchpoint (int pid)
48145796c8dcSSimon Schubert {
48155796c8dcSSimon Schubert   int retval;
48165796c8dcSSimon Schubert 
48175796c8dcSSimon Schubert   retval = debug_target.to_remove_fork_catchpoint (pid);
48185796c8dcSSimon Schubert 
48195796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
48205796c8dcSSimon Schubert 		      pid, retval);
48215796c8dcSSimon Schubert 
48225796c8dcSSimon Schubert   return retval;
48235796c8dcSSimon Schubert }
48245796c8dcSSimon Schubert 
4825c50c785cSJohn Marino static int
debug_to_insert_vfork_catchpoint(int pid)48265796c8dcSSimon Schubert debug_to_insert_vfork_catchpoint (int pid)
48275796c8dcSSimon Schubert {
4828c50c785cSJohn Marino   int retval;
48295796c8dcSSimon Schubert 
4830c50c785cSJohn Marino   retval = debug_target.to_insert_vfork_catchpoint (pid);
4831c50c785cSJohn Marino 
4832c50c785cSJohn Marino   fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4833c50c785cSJohn Marino 		      pid, retval);
4834c50c785cSJohn Marino 
4835c50c785cSJohn Marino   return retval;
48365796c8dcSSimon Schubert }
48375796c8dcSSimon Schubert 
48385796c8dcSSimon Schubert static int
debug_to_remove_vfork_catchpoint(int pid)48395796c8dcSSimon Schubert debug_to_remove_vfork_catchpoint (int pid)
48405796c8dcSSimon Schubert {
48415796c8dcSSimon Schubert   int retval;
48425796c8dcSSimon Schubert 
48435796c8dcSSimon Schubert   retval = debug_target.to_remove_vfork_catchpoint (pid);
48445796c8dcSSimon Schubert 
48455796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
48465796c8dcSSimon Schubert 		      pid, retval);
48475796c8dcSSimon Schubert 
48485796c8dcSSimon Schubert   return retval;
48495796c8dcSSimon Schubert }
48505796c8dcSSimon Schubert 
4851c50c785cSJohn Marino static int
debug_to_insert_exec_catchpoint(int pid)48525796c8dcSSimon Schubert debug_to_insert_exec_catchpoint (int pid)
48535796c8dcSSimon Schubert {
4854c50c785cSJohn Marino   int retval;
48555796c8dcSSimon Schubert 
4856c50c785cSJohn Marino   retval = debug_target.to_insert_exec_catchpoint (pid);
4857c50c785cSJohn Marino 
4858c50c785cSJohn Marino   fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4859c50c785cSJohn Marino 		      pid, retval);
4860c50c785cSJohn Marino 
4861c50c785cSJohn Marino   return retval;
48625796c8dcSSimon Schubert }
48635796c8dcSSimon Schubert 
48645796c8dcSSimon Schubert static int
debug_to_remove_exec_catchpoint(int pid)48655796c8dcSSimon Schubert debug_to_remove_exec_catchpoint (int pid)
48665796c8dcSSimon Schubert {
48675796c8dcSSimon Schubert   int retval;
48685796c8dcSSimon Schubert 
48695796c8dcSSimon Schubert   retval = debug_target.to_remove_exec_catchpoint (pid);
48705796c8dcSSimon Schubert 
48715796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
48725796c8dcSSimon Schubert 		      pid, retval);
48735796c8dcSSimon Schubert 
48745796c8dcSSimon Schubert   return retval;
48755796c8dcSSimon Schubert }
48765796c8dcSSimon Schubert 
48775796c8dcSSimon Schubert static int
debug_to_has_exited(int pid,int wait_status,int * exit_status)48785796c8dcSSimon Schubert debug_to_has_exited (int pid, int wait_status, int *exit_status)
48795796c8dcSSimon Schubert {
48805796c8dcSSimon Schubert   int has_exited;
48815796c8dcSSimon Schubert 
48825796c8dcSSimon Schubert   has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
48835796c8dcSSimon Schubert 
48845796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
48855796c8dcSSimon Schubert 		      pid, wait_status, *exit_status, has_exited);
48865796c8dcSSimon Schubert 
48875796c8dcSSimon Schubert   return has_exited;
48885796c8dcSSimon Schubert }
48895796c8dcSSimon Schubert 
48905796c8dcSSimon Schubert static int
debug_to_can_run(void)48915796c8dcSSimon Schubert debug_to_can_run (void)
48925796c8dcSSimon Schubert {
48935796c8dcSSimon Schubert   int retval;
48945796c8dcSSimon Schubert 
48955796c8dcSSimon Schubert   retval = debug_target.to_can_run ();
48965796c8dcSSimon Schubert 
48975796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
48985796c8dcSSimon Schubert 
48995796c8dcSSimon Schubert   return retval;
49005796c8dcSSimon Schubert }
49015796c8dcSSimon Schubert 
49025796c8dcSSimon Schubert static struct gdbarch *
debug_to_thread_architecture(struct target_ops * ops,ptid_t ptid)49035796c8dcSSimon Schubert debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
49045796c8dcSSimon Schubert {
49055796c8dcSSimon Schubert   struct gdbarch *retval;
49065796c8dcSSimon Schubert 
49075796c8dcSSimon Schubert   retval = debug_target.to_thread_architecture (ops, ptid);
49085796c8dcSSimon Schubert 
4909c50c785cSJohn Marino   fprintf_unfiltered (gdb_stdlog,
4910c50c785cSJohn Marino 		      "target_thread_architecture (%s) = %s [%s]\n",
4911c50c785cSJohn Marino 		      target_pid_to_str (ptid),
4912c50c785cSJohn Marino 		      host_address_to_string (retval),
49135796c8dcSSimon Schubert 		      gdbarch_bfd_arch_info (retval)->printable_name);
49145796c8dcSSimon Schubert   return retval;
49155796c8dcSSimon Schubert }
49165796c8dcSSimon Schubert 
49175796c8dcSSimon Schubert static void
debug_to_stop(ptid_t ptid)49185796c8dcSSimon Schubert debug_to_stop (ptid_t ptid)
49195796c8dcSSimon Schubert {
49205796c8dcSSimon Schubert   debug_target.to_stop (ptid);
49215796c8dcSSimon Schubert 
49225796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
49235796c8dcSSimon Schubert 		      target_pid_to_str (ptid));
49245796c8dcSSimon Schubert }
49255796c8dcSSimon Schubert 
49265796c8dcSSimon Schubert static void
debug_to_rcmd(char * command,struct ui_file * outbuf)49275796c8dcSSimon Schubert debug_to_rcmd (char *command,
49285796c8dcSSimon Schubert 	       struct ui_file *outbuf)
49295796c8dcSSimon Schubert {
49305796c8dcSSimon Schubert   debug_target.to_rcmd (command, outbuf);
49315796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
49325796c8dcSSimon Schubert }
49335796c8dcSSimon Schubert 
49345796c8dcSSimon Schubert static char *
debug_to_pid_to_exec_file(int pid)49355796c8dcSSimon Schubert debug_to_pid_to_exec_file (int pid)
49365796c8dcSSimon Schubert {
49375796c8dcSSimon Schubert   char *exec_file;
49385796c8dcSSimon Schubert 
49395796c8dcSSimon Schubert   exec_file = debug_target.to_pid_to_exec_file (pid);
49405796c8dcSSimon Schubert 
49415796c8dcSSimon Schubert   fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
49425796c8dcSSimon Schubert 		      pid, exec_file);
49435796c8dcSSimon Schubert 
49445796c8dcSSimon Schubert   return exec_file;
49455796c8dcSSimon Schubert }
49465796c8dcSSimon Schubert 
49475796c8dcSSimon Schubert static void
setup_target_debug(void)49485796c8dcSSimon Schubert setup_target_debug (void)
49495796c8dcSSimon Schubert {
49505796c8dcSSimon Schubert   memcpy (&debug_target, &current_target, sizeof debug_target);
49515796c8dcSSimon Schubert 
49525796c8dcSSimon Schubert   current_target.to_open = debug_to_open;
49535796c8dcSSimon Schubert   current_target.to_post_attach = debug_to_post_attach;
49545796c8dcSSimon Schubert   current_target.to_prepare_to_store = debug_to_prepare_to_store;
49555796c8dcSSimon Schubert   current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
49565796c8dcSSimon Schubert   current_target.to_files_info = debug_to_files_info;
49575796c8dcSSimon Schubert   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
49585796c8dcSSimon Schubert   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
49595796c8dcSSimon Schubert   current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
49605796c8dcSSimon Schubert   current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
49615796c8dcSSimon Schubert   current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
49625796c8dcSSimon Schubert   current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
49635796c8dcSSimon Schubert   current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
49645796c8dcSSimon Schubert   current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
49655796c8dcSSimon Schubert   current_target.to_stopped_data_address = debug_to_stopped_data_address;
4966c50c785cSJohn Marino   current_target.to_watchpoint_addr_within_range
4967c50c785cSJohn Marino     = debug_to_watchpoint_addr_within_range;
4968c50c785cSJohn Marino   current_target.to_region_ok_for_hw_watchpoint
4969c50c785cSJohn Marino     = debug_to_region_ok_for_hw_watchpoint;
4970c50c785cSJohn Marino   current_target.to_can_accel_watchpoint_condition
4971c50c785cSJohn Marino     = debug_to_can_accel_watchpoint_condition;
49725796c8dcSSimon Schubert   current_target.to_terminal_init = debug_to_terminal_init;
49735796c8dcSSimon Schubert   current_target.to_terminal_inferior = debug_to_terminal_inferior;
4974c50c785cSJohn Marino   current_target.to_terminal_ours_for_output
4975c50c785cSJohn Marino     = debug_to_terminal_ours_for_output;
49765796c8dcSSimon Schubert   current_target.to_terminal_ours = debug_to_terminal_ours;
49775796c8dcSSimon Schubert   current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
49785796c8dcSSimon Schubert   current_target.to_terminal_info = debug_to_terminal_info;
49795796c8dcSSimon Schubert   current_target.to_load = debug_to_load;
49805796c8dcSSimon Schubert   current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
49815796c8dcSSimon Schubert   current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
49825796c8dcSSimon Schubert   current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
49835796c8dcSSimon Schubert   current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
49845796c8dcSSimon Schubert   current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
49855796c8dcSSimon Schubert   current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
49865796c8dcSSimon Schubert   current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
49875796c8dcSSimon Schubert   current_target.to_has_exited = debug_to_has_exited;
49885796c8dcSSimon Schubert   current_target.to_can_run = debug_to_can_run;
49895796c8dcSSimon Schubert   current_target.to_stop = debug_to_stop;
49905796c8dcSSimon Schubert   current_target.to_rcmd = debug_to_rcmd;
49915796c8dcSSimon Schubert   current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
49925796c8dcSSimon Schubert   current_target.to_thread_architecture = debug_to_thread_architecture;
49935796c8dcSSimon Schubert }
49945796c8dcSSimon Schubert 
49955796c8dcSSimon Schubert 
49965796c8dcSSimon Schubert static char targ_desc[] =
4997c50c785cSJohn Marino "Names of targets and files being debugged.\nShows the entire \
4998c50c785cSJohn Marino stack of targets currently in use (including the exec-file,\n\
49995796c8dcSSimon Schubert core-file, and process, if any), as well as the symbol file name.";
50005796c8dcSSimon Schubert 
50015796c8dcSSimon Schubert static void
do_monitor_command(char * cmd,int from_tty)50025796c8dcSSimon Schubert do_monitor_command (char *cmd,
50035796c8dcSSimon Schubert 		 int from_tty)
50045796c8dcSSimon Schubert {
50055796c8dcSSimon Schubert   if ((current_target.to_rcmd
50065796c8dcSSimon Schubert        == (void (*) (char *, struct ui_file *)) tcomplain)
50075796c8dcSSimon Schubert       || (current_target.to_rcmd == debug_to_rcmd
50085796c8dcSSimon Schubert 	  && (debug_target.to_rcmd
50095796c8dcSSimon Schubert 	      == (void (*) (char *, struct ui_file *)) tcomplain)))
50105796c8dcSSimon Schubert     error (_("\"monitor\" command not supported by this target."));
50115796c8dcSSimon Schubert   target_rcmd (cmd, gdb_stdtarg);
50125796c8dcSSimon Schubert }
50135796c8dcSSimon Schubert 
50145796c8dcSSimon Schubert /* Print the name of each layers of our target stack.  */
50155796c8dcSSimon Schubert 
50165796c8dcSSimon Schubert static void
maintenance_print_target_stack(char * cmd,int from_tty)50175796c8dcSSimon Schubert maintenance_print_target_stack (char *cmd, int from_tty)
50185796c8dcSSimon Schubert {
50195796c8dcSSimon Schubert   struct target_ops *t;
50205796c8dcSSimon Schubert 
50215796c8dcSSimon Schubert   printf_filtered (_("The current target stack is:\n"));
50225796c8dcSSimon Schubert 
50235796c8dcSSimon Schubert   for (t = target_stack; t != NULL; t = t->beneath)
50245796c8dcSSimon Schubert     {
50255796c8dcSSimon Schubert       printf_filtered ("  - %s (%s)\n", t->to_shortname, t->to_longname);
50265796c8dcSSimon Schubert     }
50275796c8dcSSimon Schubert }
50285796c8dcSSimon Schubert 
50295796c8dcSSimon Schubert /* Controls if async mode is permitted.  */
50305796c8dcSSimon Schubert int target_async_permitted = 0;
50315796c8dcSSimon Schubert 
50325796c8dcSSimon Schubert /* The set command writes to this variable.  If the inferior is
50335796c8dcSSimon Schubert    executing, linux_nat_async_permitted is *not* updated.  */
50345796c8dcSSimon Schubert static int target_async_permitted_1 = 0;
50355796c8dcSSimon Schubert 
50365796c8dcSSimon Schubert static void
set_target_async_command(char * args,int from_tty,struct cmd_list_element * c)5037*ef5ccd6cSJohn Marino set_target_async_command (char *args, int from_tty,
50385796c8dcSSimon Schubert 			  struct cmd_list_element *c)
50395796c8dcSSimon Schubert {
50405796c8dcSSimon Schubert   if (have_live_inferiors ())
50415796c8dcSSimon Schubert     {
50425796c8dcSSimon Schubert       target_async_permitted_1 = target_async_permitted;
50435796c8dcSSimon Schubert       error (_("Cannot change this setting while the inferior is running."));
50445796c8dcSSimon Schubert     }
50455796c8dcSSimon Schubert 
50465796c8dcSSimon Schubert   target_async_permitted = target_async_permitted_1;
50475796c8dcSSimon Schubert }
50485796c8dcSSimon Schubert 
50495796c8dcSSimon Schubert static void
show_target_async_command(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)5050*ef5ccd6cSJohn Marino show_target_async_command (struct ui_file *file, int from_tty,
50515796c8dcSSimon Schubert 			   struct cmd_list_element *c,
50525796c8dcSSimon Schubert 			   const char *value)
50535796c8dcSSimon Schubert {
5054c50c785cSJohn Marino   fprintf_filtered (file,
5055c50c785cSJohn Marino 		    _("Controlling the inferior in "
5056c50c785cSJohn Marino 		      "asynchronous mode is %s.\n"), value);
50575796c8dcSSimon Schubert }
50585796c8dcSSimon Schubert 
5059cf7f2e2dSJohn Marino /* Temporary copies of permission settings.  */
5060cf7f2e2dSJohn Marino 
5061cf7f2e2dSJohn Marino static int may_write_registers_1 = 1;
5062cf7f2e2dSJohn Marino static int may_write_memory_1 = 1;
5063cf7f2e2dSJohn Marino static int may_insert_breakpoints_1 = 1;
5064cf7f2e2dSJohn Marino static int may_insert_tracepoints_1 = 1;
5065cf7f2e2dSJohn Marino static int may_insert_fast_tracepoints_1 = 1;
5066cf7f2e2dSJohn Marino static int may_stop_1 = 1;
5067cf7f2e2dSJohn Marino 
5068cf7f2e2dSJohn Marino /* Make the user-set values match the real values again.  */
5069cf7f2e2dSJohn Marino 
5070cf7f2e2dSJohn Marino void
update_target_permissions(void)5071cf7f2e2dSJohn Marino update_target_permissions (void)
5072cf7f2e2dSJohn Marino {
5073cf7f2e2dSJohn Marino   may_write_registers_1 = may_write_registers;
5074cf7f2e2dSJohn Marino   may_write_memory_1 = may_write_memory;
5075cf7f2e2dSJohn Marino   may_insert_breakpoints_1 = may_insert_breakpoints;
5076cf7f2e2dSJohn Marino   may_insert_tracepoints_1 = may_insert_tracepoints;
5077cf7f2e2dSJohn Marino   may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
5078cf7f2e2dSJohn Marino   may_stop_1 = may_stop;
5079cf7f2e2dSJohn Marino }
5080cf7f2e2dSJohn Marino 
5081cf7f2e2dSJohn Marino /* The one function handles (most of) the permission flags in the same
5082cf7f2e2dSJohn Marino    way.  */
5083cf7f2e2dSJohn Marino 
5084cf7f2e2dSJohn Marino static void
set_target_permissions(char * args,int from_tty,struct cmd_list_element * c)5085cf7f2e2dSJohn Marino set_target_permissions (char *args, int from_tty,
5086cf7f2e2dSJohn Marino 			struct cmd_list_element *c)
5087cf7f2e2dSJohn Marino {
5088cf7f2e2dSJohn Marino   if (target_has_execution)
5089cf7f2e2dSJohn Marino     {
5090cf7f2e2dSJohn Marino       update_target_permissions ();
5091cf7f2e2dSJohn Marino       error (_("Cannot change this setting while the inferior is running."));
5092cf7f2e2dSJohn Marino     }
5093cf7f2e2dSJohn Marino 
5094cf7f2e2dSJohn Marino   /* Make the real values match the user-changed values.  */
5095cf7f2e2dSJohn Marino   may_write_registers = may_write_registers_1;
5096cf7f2e2dSJohn Marino   may_insert_breakpoints = may_insert_breakpoints_1;
5097cf7f2e2dSJohn Marino   may_insert_tracepoints = may_insert_tracepoints_1;
5098cf7f2e2dSJohn Marino   may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
5099cf7f2e2dSJohn Marino   may_stop = may_stop_1;
5100cf7f2e2dSJohn Marino   update_observer_mode ();
5101cf7f2e2dSJohn Marino }
5102cf7f2e2dSJohn Marino 
5103cf7f2e2dSJohn Marino /* Set memory write permission independently of observer mode.  */
5104cf7f2e2dSJohn Marino 
5105cf7f2e2dSJohn Marino static void
set_write_memory_permission(char * args,int from_tty,struct cmd_list_element * c)5106cf7f2e2dSJohn Marino set_write_memory_permission (char *args, int from_tty,
5107cf7f2e2dSJohn Marino 			struct cmd_list_element *c)
5108cf7f2e2dSJohn Marino {
5109cf7f2e2dSJohn Marino   /* Make the real values match the user-changed values.  */
5110cf7f2e2dSJohn Marino   may_write_memory = may_write_memory_1;
5111cf7f2e2dSJohn Marino   update_observer_mode ();
5112cf7f2e2dSJohn Marino }
5113cf7f2e2dSJohn Marino 
5114cf7f2e2dSJohn Marino 
51155796c8dcSSimon Schubert void
initialize_targets(void)51165796c8dcSSimon Schubert initialize_targets (void)
51175796c8dcSSimon Schubert {
51185796c8dcSSimon Schubert   init_dummy_target ();
51195796c8dcSSimon Schubert   push_target (&dummy_target);
51205796c8dcSSimon Schubert 
51215796c8dcSSimon Schubert   add_info ("target", target_info, targ_desc);
51225796c8dcSSimon Schubert   add_info ("files", target_info, targ_desc);
51235796c8dcSSimon Schubert 
5124*ef5ccd6cSJohn Marino   add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
51255796c8dcSSimon Schubert Set target debugging."), _("\
51265796c8dcSSimon Schubert Show target debugging."), _("\
51275796c8dcSSimon Schubert When non-zero, target debugging is enabled.  Higher numbers are more\n\
51285796c8dcSSimon Schubert verbose.  Changes do not take effect until the next \"run\" or \"target\"\n\
51295796c8dcSSimon Schubert command."),
51305796c8dcSSimon Schubert 			     NULL,
51315796c8dcSSimon Schubert 			     show_targetdebug,
51325796c8dcSSimon Schubert 			     &setdebuglist, &showdebuglist);
51335796c8dcSSimon Schubert 
51345796c8dcSSimon Schubert   add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
51355796c8dcSSimon Schubert 			   &trust_readonly, _("\
51365796c8dcSSimon Schubert Set mode for reading from readonly sections."), _("\
51375796c8dcSSimon Schubert Show mode for reading from readonly sections."), _("\
51385796c8dcSSimon Schubert When this mode is on, memory reads from readonly sections (such as .text)\n\
51395796c8dcSSimon Schubert will be read from the object file instead of from the target.  This will\n\
51405796c8dcSSimon Schubert result in significant performance improvement for remote targets."),
51415796c8dcSSimon Schubert 			   NULL,
51425796c8dcSSimon Schubert 			   show_trust_readonly,
51435796c8dcSSimon Schubert 			   &setlist, &showlist);
51445796c8dcSSimon Schubert 
51455796c8dcSSimon Schubert   add_com ("monitor", class_obscure, do_monitor_command,
51465796c8dcSSimon Schubert 	   _("Send a command to the remote monitor (remote targets only)."));
51475796c8dcSSimon Schubert 
51485796c8dcSSimon Schubert   add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
51495796c8dcSSimon Schubert            _("Print the name of each layer of the internal target stack."),
51505796c8dcSSimon Schubert            &maintenanceprintlist);
51515796c8dcSSimon Schubert 
51525796c8dcSSimon Schubert   add_setshow_boolean_cmd ("target-async", no_class,
51535796c8dcSSimon Schubert 			   &target_async_permitted_1, _("\
51545796c8dcSSimon Schubert Set whether gdb controls the inferior in asynchronous mode."), _("\
51555796c8dcSSimon Schubert Show whether gdb controls the inferior in asynchronous mode."), _("\
51565796c8dcSSimon Schubert Tells gdb whether to control the inferior in asynchronous mode."),
5157*ef5ccd6cSJohn Marino 			   set_target_async_command,
5158*ef5ccd6cSJohn Marino 			   show_target_async_command,
51595796c8dcSSimon Schubert 			   &setlist,
51605796c8dcSSimon Schubert 			   &showlist);
51615796c8dcSSimon Schubert 
51625796c8dcSSimon Schubert   add_setshow_boolean_cmd ("stack-cache", class_support,
51635796c8dcSSimon Schubert 			   &stack_cache_enabled_p_1, _("\
51645796c8dcSSimon Schubert Set cache use for stack access."), _("\
51655796c8dcSSimon Schubert Show cache use for stack access."), _("\
51665796c8dcSSimon Schubert When on, use the data cache for all stack access, regardless of any\n\
51675796c8dcSSimon Schubert configured memory regions.  This improves remote performance significantly.\n\
51685796c8dcSSimon Schubert By default, caching for stack access is on."),
51695796c8dcSSimon Schubert 			   set_stack_cache_enabled_p,
51705796c8dcSSimon Schubert 			   show_stack_cache_enabled_p,
51715796c8dcSSimon Schubert 			   &setlist, &showlist);
51725796c8dcSSimon Schubert 
5173cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-write-registers", class_support,
5174cf7f2e2dSJohn Marino 			   &may_write_registers_1, _("\
5175cf7f2e2dSJohn Marino Set permission to write into registers."), _("\
5176cf7f2e2dSJohn Marino Show permission to write into registers."), _("\
5177cf7f2e2dSJohn Marino When this permission is on, GDB may write into the target's registers.\n\
5178cf7f2e2dSJohn Marino Otherwise, any sort of write attempt will result in an error."),
5179cf7f2e2dSJohn Marino 			   set_target_permissions, NULL,
5180cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5181cf7f2e2dSJohn Marino 
5182cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-write-memory", class_support,
5183cf7f2e2dSJohn Marino 			   &may_write_memory_1, _("\
5184cf7f2e2dSJohn Marino Set permission to write into target memory."), _("\
5185cf7f2e2dSJohn Marino Show permission to write into target memory."), _("\
5186cf7f2e2dSJohn Marino When this permission is on, GDB may write into the target's memory.\n\
5187cf7f2e2dSJohn Marino Otherwise, any sort of write attempt will result in an error."),
5188cf7f2e2dSJohn Marino 			   set_write_memory_permission, NULL,
5189cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5190cf7f2e2dSJohn Marino 
5191cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
5192cf7f2e2dSJohn Marino 			   &may_insert_breakpoints_1, _("\
5193cf7f2e2dSJohn Marino Set permission to insert breakpoints in the target."), _("\
5194cf7f2e2dSJohn Marino Show permission to insert breakpoints in the target."), _("\
5195cf7f2e2dSJohn Marino When this permission is on, GDB may insert breakpoints in the program.\n\
5196cf7f2e2dSJohn Marino Otherwise, any sort of insertion attempt will result in an error."),
5197cf7f2e2dSJohn Marino 			   set_target_permissions, NULL,
5198cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5199cf7f2e2dSJohn Marino 
5200cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
5201cf7f2e2dSJohn Marino 			   &may_insert_tracepoints_1, _("\
5202cf7f2e2dSJohn Marino Set permission to insert tracepoints in the target."), _("\
5203cf7f2e2dSJohn Marino Show permission to insert tracepoints in the target."), _("\
5204cf7f2e2dSJohn Marino When this permission is on, GDB may insert tracepoints in the program.\n\
5205cf7f2e2dSJohn Marino Otherwise, any sort of insertion attempt will result in an error."),
5206cf7f2e2dSJohn Marino 			   set_target_permissions, NULL,
5207cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5208cf7f2e2dSJohn Marino 
5209cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
5210cf7f2e2dSJohn Marino 			   &may_insert_fast_tracepoints_1, _("\
5211cf7f2e2dSJohn Marino Set permission to insert fast tracepoints in the target."), _("\
5212cf7f2e2dSJohn Marino Show permission to insert fast tracepoints in the target."), _("\
5213cf7f2e2dSJohn Marino When this permission is on, GDB may insert fast tracepoints.\n\
5214cf7f2e2dSJohn Marino Otherwise, any sort of insertion attempt will result in an error."),
5215cf7f2e2dSJohn Marino 			   set_target_permissions, NULL,
5216cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5217cf7f2e2dSJohn Marino 
5218cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("may-interrupt", class_support,
5219cf7f2e2dSJohn Marino 			   &may_stop_1, _("\
5220cf7f2e2dSJohn Marino Set permission to interrupt or signal the target."), _("\
5221cf7f2e2dSJohn Marino Show permission to interrupt or signal the target."), _("\
5222cf7f2e2dSJohn Marino When this permission is on, GDB may interrupt/stop the target's execution.\n\
5223cf7f2e2dSJohn Marino Otherwise, any attempt to interrupt or stop will be ignored."),
5224cf7f2e2dSJohn Marino 			   set_target_permissions, NULL,
5225cf7f2e2dSJohn Marino 			   &setlist, &showlist);
5226cf7f2e2dSJohn Marino 
5227cf7f2e2dSJohn Marino 
52285796c8dcSSimon Schubert   target_dcache = dcache_init ();
52295796c8dcSSimon Schubert }
5230