GDB (API)
/home/stan/gdb/src/gdb/target.c
Go to the documentation of this file.
00001 /* Select target systems and architectures at runtime for GDB.
00002 
00003    Copyright (C) 1990-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Cygnus Support.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include <errno.h>
00024 #include "gdb_string.h"
00025 #include "target.h"
00026 #include "gdbcmd.h"
00027 #include "symtab.h"
00028 #include "inferior.h"
00029 #include "bfd.h"
00030 #include "symfile.h"
00031 #include "objfiles.h"
00032 #include "dcache.h"
00033 #include <signal.h>
00034 #include "regcache.h"
00035 #include "gdb_assert.h"
00036 #include "gdbcore.h"
00037 #include "exceptions.h"
00038 #include "target-descriptions.h"
00039 #include "gdbthread.h"
00040 #include "solib.h"
00041 #include "exec.h"
00042 #include "inline-frame.h"
00043 #include "tracepoint.h"
00044 #include "gdb/fileio.h"
00045 #include "agent.h"
00046 
00047 static void target_info (char *, int);
00048 
00049 static void default_terminal_info (const char *, int);
00050 
00051 static int default_watchpoint_addr_within_range (struct target_ops *,
00052                                                  CORE_ADDR, CORE_ADDR, int);
00053 
00054 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
00055 
00056 static void tcomplain (void) ATTRIBUTE_NORETURN;
00057 
00058 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
00059 
00060 static int return_zero (void);
00061 
00062 static int return_one (void);
00063 
00064 static int return_minus_one (void);
00065 
00066 void target_ignore (void);
00067 
00068 static void target_command (char *, int);
00069 
00070 static struct target_ops *find_default_run_target (char *);
00071 
00072 static LONGEST default_xfer_partial (struct target_ops *ops,
00073                                      enum target_object object,
00074                                      const char *annex, gdb_byte *readbuf,
00075                                      const gdb_byte *writebuf,
00076                                      ULONGEST offset, LONGEST len);
00077 
00078 static LONGEST current_xfer_partial (struct target_ops *ops,
00079                                      enum target_object object,
00080                                      const char *annex, gdb_byte *readbuf,
00081                                      const gdb_byte *writebuf,
00082                                      ULONGEST offset, LONGEST len);
00083 
00084 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
00085                                                     ptid_t ptid);
00086 
00087 static void init_dummy_target (void);
00088 
00089 static struct target_ops debug_target;
00090 
00091 static void debug_to_open (char *, int);
00092 
00093 static void debug_to_prepare_to_store (struct regcache *);
00094 
00095 static void debug_to_files_info (struct target_ops *);
00096 
00097 static int debug_to_insert_breakpoint (struct gdbarch *,
00098                                        struct bp_target_info *);
00099 
00100 static int debug_to_remove_breakpoint (struct gdbarch *,
00101                                        struct bp_target_info *);
00102 
00103 static int debug_to_can_use_hw_breakpoint (int, int, int);
00104 
00105 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
00106                                           struct bp_target_info *);
00107 
00108 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
00109                                           struct bp_target_info *);
00110 
00111 static int debug_to_insert_watchpoint (CORE_ADDR, int, int,
00112                                        struct expression *);
00113 
00114 static int debug_to_remove_watchpoint (CORE_ADDR, int, int,
00115                                        struct expression *);
00116 
00117 static int debug_to_stopped_by_watchpoint (void);
00118 
00119 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
00120 
00121 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
00122                                                   CORE_ADDR, CORE_ADDR, int);
00123 
00124 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
00125 
00126 static int debug_to_can_accel_watchpoint_condition (CORE_ADDR, int, int,
00127                                                     struct expression *);
00128 
00129 static void debug_to_terminal_init (void);
00130 
00131 static void debug_to_terminal_inferior (void);
00132 
00133 static void debug_to_terminal_ours_for_output (void);
00134 
00135 static void debug_to_terminal_save_ours (void);
00136 
00137 static void debug_to_terminal_ours (void);
00138 
00139 static void debug_to_load (char *, int);
00140 
00141 static int debug_to_can_run (void);
00142 
00143 static void debug_to_stop (ptid_t);
00144 
00145 /* Pointer to array of target architecture structures; the size of the
00146    array; the current index into the array; the allocated size of the
00147    array.  */
00148 struct target_ops **target_structs;
00149 unsigned target_struct_size;
00150 unsigned target_struct_allocsize;
00151 #define DEFAULT_ALLOCSIZE       10
00152 
00153 /* The initial current target, so that there is always a semi-valid
00154    current target.  */
00155 
00156 static struct target_ops dummy_target;
00157 
00158 /* Top of target stack.  */
00159 
00160 static struct target_ops *target_stack;
00161 
00162 /* The target structure we are currently using to talk to a process
00163    or file or whatever "inferior" we have.  */
00164 
00165 struct target_ops current_target;
00166 
00167 /* Command list for target.  */
00168 
00169 static struct cmd_list_element *targetlist = NULL;
00170 
00171 /* Nonzero if we should trust readonly sections from the
00172    executable when reading memory.  */
00173 
00174 static int trust_readonly = 0;
00175 
00176 /* Nonzero if we should show true memory content including
00177    memory breakpoint inserted by gdb.  */
00178 
00179 static int show_memory_breakpoints = 0;
00180 
00181 /* These globals control whether GDB attempts to perform these
00182    operations; they are useful for targets that need to prevent
00183    inadvertant disruption, such as in non-stop mode.  */
00184 
00185 int may_write_registers = 1;
00186 
00187 int may_write_memory = 1;
00188 
00189 int may_insert_breakpoints = 1;
00190 
00191 int may_insert_tracepoints = 1;
00192 
00193 int may_insert_fast_tracepoints = 1;
00194 
00195 int may_stop = 1;
00196 
00197 /* Non-zero if we want to see trace of target level stuff.  */
00198 
00199 static unsigned int targetdebug = 0;
00200 static void
00201 show_targetdebug (struct ui_file *file, int from_tty,
00202                   struct cmd_list_element *c, const char *value)
00203 {
00204   fprintf_filtered (file, _("Target debugging is %s.\n"), value);
00205 }
00206 
00207 static void setup_target_debug (void);
00208 
00209 /* The option sets this.  */
00210 static int stack_cache_enabled_p_1 = 1;
00211 /* And set_stack_cache_enabled_p updates this.
00212    The reason for the separation is so that we don't flush the cache for
00213    on->on transitions.  */
00214 static int stack_cache_enabled_p = 1;
00215 
00216 /* This is called *after* the stack-cache has been set.
00217    Flush the cache for off->on and on->off transitions.
00218    There's no real need to flush the cache for on->off transitions,
00219    except cleanliness.  */
00220 
00221 static void
00222 set_stack_cache_enabled_p (char *args, int from_tty,
00223                            struct cmd_list_element *c)
00224 {
00225   if (stack_cache_enabled_p != stack_cache_enabled_p_1)
00226     target_dcache_invalidate ();
00227 
00228   stack_cache_enabled_p = stack_cache_enabled_p_1;
00229 }
00230 
00231 static void
00232 show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
00233                             struct cmd_list_element *c, const char *value)
00234 {
00235   fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
00236 }
00237 
00238 /* Cache of memory operations, to speed up remote access.  */
00239 static DCACHE *target_dcache;
00240 
00241 /* Invalidate the target dcache.  */
00242 
00243 void
00244 target_dcache_invalidate (void)
00245 {
00246   dcache_invalidate (target_dcache);
00247 }
00248 
00249 /* The user just typed 'target' without the name of a target.  */
00250 
00251 static void
00252 target_command (char *arg, int from_tty)
00253 {
00254   fputs_filtered ("Argument required (target name).  Try `help target'\n",
00255                   gdb_stdout);
00256 }
00257 
00258 /* Default target_has_* methods for process_stratum targets.  */
00259 
00260 int
00261 default_child_has_all_memory (struct target_ops *ops)
00262 {
00263   /* If no inferior selected, then we can't read memory here.  */
00264   if (ptid_equal (inferior_ptid, null_ptid))
00265     return 0;
00266 
00267   return 1;
00268 }
00269 
00270 int
00271 default_child_has_memory (struct target_ops *ops)
00272 {
00273   /* If no inferior selected, then we can't read memory here.  */
00274   if (ptid_equal (inferior_ptid, null_ptid))
00275     return 0;
00276 
00277   return 1;
00278 }
00279 
00280 int
00281 default_child_has_stack (struct target_ops *ops)
00282 {
00283   /* If no inferior selected, there's no stack.  */
00284   if (ptid_equal (inferior_ptid, null_ptid))
00285     return 0;
00286 
00287   return 1;
00288 }
00289 
00290 int
00291 default_child_has_registers (struct target_ops *ops)
00292 {
00293   /* Can't read registers from no inferior.  */
00294   if (ptid_equal (inferior_ptid, null_ptid))
00295     return 0;
00296 
00297   return 1;
00298 }
00299 
00300 int
00301 default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
00302 {
00303   /* If there's no thread selected, then we can't make it run through
00304      hoops.  */
00305   if (ptid_equal (the_ptid, null_ptid))
00306     return 0;
00307 
00308   return 1;
00309 }
00310 
00311 
00312 int
00313 target_has_all_memory_1 (void)
00314 {
00315   struct target_ops *t;
00316 
00317   for (t = current_target.beneath; t != NULL; t = t->beneath)
00318     if (t->to_has_all_memory (t))
00319       return 1;
00320 
00321   return 0;
00322 }
00323 
00324 int
00325 target_has_memory_1 (void)
00326 {
00327   struct target_ops *t;
00328 
00329   for (t = current_target.beneath; t != NULL; t = t->beneath)
00330     if (t->to_has_memory (t))
00331       return 1;
00332 
00333   return 0;
00334 }
00335 
00336 int
00337 target_has_stack_1 (void)
00338 {
00339   struct target_ops *t;
00340 
00341   for (t = current_target.beneath; t != NULL; t = t->beneath)
00342     if (t->to_has_stack (t))
00343       return 1;
00344 
00345   return 0;
00346 }
00347 
00348 int
00349 target_has_registers_1 (void)
00350 {
00351   struct target_ops *t;
00352 
00353   for (t = current_target.beneath; t != NULL; t = t->beneath)
00354     if (t->to_has_registers (t))
00355       return 1;
00356 
00357   return 0;
00358 }
00359 
00360 int
00361 target_has_execution_1 (ptid_t the_ptid)
00362 {
00363   struct target_ops *t;
00364 
00365   for (t = current_target.beneath; t != NULL; t = t->beneath)
00366     if (t->to_has_execution (t, the_ptid))
00367       return 1;
00368 
00369   return 0;
00370 }
00371 
00372 int
00373 target_has_execution_current (void)
00374 {
00375   return target_has_execution_1 (inferior_ptid);
00376 }
00377 
00378 /* Complete initialization of T.  This ensures that various fields in
00379    T are set, if needed by the target implementation.  */
00380 
00381 void
00382 complete_target_initialization (struct target_ops *t)
00383 {
00384   /* Provide default values for all "must have" methods.  */
00385   if (t->to_xfer_partial == NULL)
00386     t->to_xfer_partial = default_xfer_partial;
00387 
00388   if (t->to_has_all_memory == NULL)
00389     t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
00390 
00391   if (t->to_has_memory == NULL)
00392     t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
00393 
00394   if (t->to_has_stack == NULL)
00395     t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
00396 
00397   if (t->to_has_registers == NULL)
00398     t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
00399 
00400   if (t->to_has_execution == NULL)
00401     t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
00402 }
00403 
00404 /* Add possible target architecture T to the list and add a new
00405    command 'target T->to_shortname'.  Set COMPLETER as the command's
00406    completer if not NULL.  */
00407 
00408 void
00409 add_target_with_completer (struct target_ops *t,
00410                            completer_ftype *completer)
00411 {
00412   struct cmd_list_element *c;
00413 
00414   complete_target_initialization (t);
00415 
00416   if (!target_structs)
00417     {
00418       target_struct_allocsize = DEFAULT_ALLOCSIZE;
00419       target_structs = (struct target_ops **) xmalloc
00420         (target_struct_allocsize * sizeof (*target_structs));
00421     }
00422   if (target_struct_size >= target_struct_allocsize)
00423     {
00424       target_struct_allocsize *= 2;
00425       target_structs = (struct target_ops **)
00426         xrealloc ((char *) target_structs,
00427                   target_struct_allocsize * sizeof (*target_structs));
00428     }
00429   target_structs[target_struct_size++] = t;
00430 
00431   if (targetlist == NULL)
00432     add_prefix_cmd ("target", class_run, target_command, _("\
00433 Connect to a target machine or process.\n\
00434 The first argument is the type or protocol of the target machine.\n\
00435 Remaining arguments are interpreted by the target protocol.  For more\n\
00436 information on the arguments for a particular protocol, type\n\
00437 `help target ' followed by the protocol name."),
00438                     &targetlist, "target ", 0, &cmdlist);
00439   c = add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc,
00440                &targetlist);
00441   if (completer != NULL)
00442     set_cmd_completer (c, completer);
00443 }
00444 
00445 /* Add a possible target architecture to the list.  */
00446 
00447 void
00448 add_target (struct target_ops *t)
00449 {
00450   add_target_with_completer (t, NULL);
00451 }
00452 
00453 /* See target.h.  */
00454 
00455 void
00456 add_deprecated_target_alias (struct target_ops *t, char *alias)
00457 {
00458   struct cmd_list_element *c;
00459   char *alt;
00460 
00461   /* If we use add_alias_cmd, here, we do not get the deprecated warning,
00462      see PR cli/15104.  */
00463   c = add_cmd (alias, no_class, t->to_open, t->to_doc, &targetlist);
00464   alt = xstrprintf ("target %s", t->to_shortname);
00465   deprecate_cmd (c, alt);
00466 }
00467 
00468 /* Stub functions */
00469 
00470 void
00471 target_ignore (void)
00472 {
00473 }
00474 
00475 void
00476 target_kill (void)
00477 {
00478   struct target_ops *t;
00479 
00480   for (t = current_target.beneath; t != NULL; t = t->beneath)
00481     if (t->to_kill != NULL)
00482       {
00483         if (targetdebug)
00484           fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
00485 
00486         t->to_kill (t);
00487         return;
00488       }
00489 
00490   noprocess ();
00491 }
00492 
00493 void
00494 target_load (char *arg, int from_tty)
00495 {
00496   target_dcache_invalidate ();
00497   (*current_target.to_load) (arg, from_tty);
00498 }
00499 
00500 void
00501 target_create_inferior (char *exec_file, char *args,
00502                         char **env, int from_tty)
00503 {
00504   struct target_ops *t;
00505 
00506   for (t = current_target.beneath; t != NULL; t = t->beneath)
00507     {
00508       if (t->to_create_inferior != NULL)        
00509         {
00510           t->to_create_inferior (t, exec_file, args, env, from_tty);
00511           if (targetdebug)
00512             fprintf_unfiltered (gdb_stdlog,
00513                                 "target_create_inferior (%s, %s, xxx, %d)\n",
00514                                 exec_file, args, from_tty);
00515           return;
00516         }
00517     }
00518 
00519   internal_error (__FILE__, __LINE__,
00520                   _("could not find a target to create inferior"));
00521 }
00522 
00523 void
00524 target_terminal_inferior (void)
00525 {
00526   /* A background resume (``run&'') should leave GDB in control of the
00527      terminal.  Use target_can_async_p, not target_is_async_p, since at
00528      this point the target is not async yet.  However, if sync_execution
00529      is not set, we know it will become async prior to resume.  */
00530   if (target_can_async_p () && !sync_execution)
00531     return;
00532 
00533   /* If GDB is resuming the inferior in the foreground, install
00534      inferior's terminal modes.  */
00535   (*current_target.to_terminal_inferior) ();
00536 }
00537 
00538 static int
00539 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
00540           struct target_ops *t)
00541 {
00542   errno = EIO;                  /* Can't read/write this location.  */
00543   return 0;                     /* No bytes handled.  */
00544 }
00545 
00546 static void
00547 tcomplain (void)
00548 {
00549   error (_("You can't do that when your target is `%s'"),
00550          current_target.to_shortname);
00551 }
00552 
00553 void
00554 noprocess (void)
00555 {
00556   error (_("You can't do that without a process to debug."));
00557 }
00558 
00559 static void
00560 default_terminal_info (const char *args, int from_tty)
00561 {
00562   printf_unfiltered (_("No saved terminal information.\n"));
00563 }
00564 
00565 /* A default implementation for the to_get_ada_task_ptid target method.
00566 
00567    This function builds the PTID by using both LWP and TID as part of
00568    the PTID lwp and tid elements.  The pid used is the pid of the
00569    inferior_ptid.  */
00570 
00571 static ptid_t
00572 default_get_ada_task_ptid (long lwp, long tid)
00573 {
00574   return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
00575 }
00576 
00577 static enum exec_direction_kind
00578 default_execution_direction (void)
00579 {
00580   if (!target_can_execute_reverse)
00581     return EXEC_FORWARD;
00582   else if (!target_can_async_p ())
00583     return EXEC_FORWARD;
00584   else
00585     gdb_assert_not_reached ("\
00586 to_execution_direction must be implemented for reverse async");
00587 }
00588 
00589 /* Go through the target stack from top to bottom, copying over zero
00590    entries in current_target, then filling in still empty entries.  In
00591    effect, we are doing class inheritance through the pushed target
00592    vectors.
00593 
00594    NOTE: cagney/2003-10-17: The problem with this inheritance, as it
00595    is currently implemented, is that it discards any knowledge of
00596    which target an inherited method originally belonged to.
00597    Consequently, new new target methods should instead explicitly and
00598    locally search the target stack for the target that can handle the
00599    request.  */
00600 
00601 static void
00602 update_current_target (void)
00603 {
00604   struct target_ops *t;
00605 
00606   /* First, reset current's contents.  */
00607   memset (&current_target, 0, sizeof (current_target));
00608 
00609 #define INHERIT(FIELD, TARGET) \
00610       if (!current_target.FIELD) \
00611         current_target.FIELD = (TARGET)->FIELD
00612 
00613   for (t = target_stack; t; t = t->beneath)
00614     {
00615       INHERIT (to_shortname, t);
00616       INHERIT (to_longname, t);
00617       INHERIT (to_doc, t);
00618       /* Do not inherit to_open.  */
00619       /* Do not inherit to_close.  */
00620       /* Do not inherit to_attach.  */
00621       INHERIT (to_post_attach, t);
00622       INHERIT (to_attach_no_wait, t);
00623       /* Do not inherit to_detach.  */
00624       /* Do not inherit to_disconnect.  */
00625       /* Do not inherit to_resume.  */
00626       /* Do not inherit to_wait.  */
00627       /* Do not inherit to_fetch_registers.  */
00628       /* Do not inherit to_store_registers.  */
00629       INHERIT (to_prepare_to_store, t);
00630       INHERIT (deprecated_xfer_memory, t);
00631       INHERIT (to_files_info, t);
00632       INHERIT (to_insert_breakpoint, t);
00633       INHERIT (to_remove_breakpoint, t);
00634       INHERIT (to_can_use_hw_breakpoint, t);
00635       INHERIT (to_insert_hw_breakpoint, t);
00636       INHERIT (to_remove_hw_breakpoint, t);
00637       /* Do not inherit to_ranged_break_num_registers.  */
00638       INHERIT (to_insert_watchpoint, t);
00639       INHERIT (to_remove_watchpoint, t);
00640       /* Do not inherit to_insert_mask_watchpoint.  */
00641       /* Do not inherit to_remove_mask_watchpoint.  */
00642       INHERIT (to_stopped_data_address, t);
00643       INHERIT (to_have_steppable_watchpoint, t);
00644       INHERIT (to_have_continuable_watchpoint, t);
00645       INHERIT (to_stopped_by_watchpoint, t);
00646       INHERIT (to_watchpoint_addr_within_range, t);
00647       INHERIT (to_region_ok_for_hw_watchpoint, t);
00648       INHERIT (to_can_accel_watchpoint_condition, t);
00649       /* Do not inherit to_masked_watch_num_registers.  */
00650       INHERIT (to_terminal_init, t);
00651       INHERIT (to_terminal_inferior, t);
00652       INHERIT (to_terminal_ours_for_output, t);
00653       INHERIT (to_terminal_ours, t);
00654       INHERIT (to_terminal_save_ours, t);
00655       INHERIT (to_terminal_info, t);
00656       /* Do not inherit to_kill.  */
00657       INHERIT (to_load, t);
00658       /* Do no inherit to_create_inferior.  */
00659       INHERIT (to_post_startup_inferior, t);
00660       INHERIT (to_insert_fork_catchpoint, t);
00661       INHERIT (to_remove_fork_catchpoint, t);
00662       INHERIT (to_insert_vfork_catchpoint, t);
00663       INHERIT (to_remove_vfork_catchpoint, t);
00664       /* Do not inherit to_follow_fork.  */
00665       INHERIT (to_insert_exec_catchpoint, t);
00666       INHERIT (to_remove_exec_catchpoint, t);
00667       INHERIT (to_set_syscall_catchpoint, t);
00668       INHERIT (to_has_exited, t);
00669       /* Do not inherit to_mourn_inferior.  */
00670       INHERIT (to_can_run, t);
00671       /* Do not inherit to_pass_signals.  */
00672       /* Do not inherit to_program_signals.  */
00673       /* Do not inherit to_thread_alive.  */
00674       /* Do not inherit to_find_new_threads.  */
00675       /* Do not inherit to_pid_to_str.  */
00676       INHERIT (to_extra_thread_info, t);
00677       INHERIT (to_thread_name, t);
00678       INHERIT (to_stop, t);
00679       /* Do not inherit to_xfer_partial.  */
00680       INHERIT (to_rcmd, t);
00681       INHERIT (to_pid_to_exec_file, t);
00682       INHERIT (to_log_command, t);
00683       INHERIT (to_stratum, t);
00684       /* Do not inherit to_has_all_memory.  */
00685       /* Do not inherit to_has_memory.  */
00686       /* Do not inherit to_has_stack.  */
00687       /* Do not inherit to_has_registers.  */
00688       /* Do not inherit to_has_execution.  */
00689       INHERIT (to_has_thread_control, t);
00690       INHERIT (to_can_async_p, t);
00691       INHERIT (to_is_async_p, t);
00692       INHERIT (to_async, t);
00693       INHERIT (to_find_memory_regions, t);
00694       INHERIT (to_make_corefile_notes, t);
00695       INHERIT (to_get_bookmark, t);
00696       INHERIT (to_goto_bookmark, t);
00697       /* Do not inherit to_get_thread_local_address.  */
00698       INHERIT (to_can_execute_reverse, t);
00699       INHERIT (to_execution_direction, t);
00700       INHERIT (to_thread_architecture, t);
00701       /* Do not inherit to_read_description.  */
00702       INHERIT (to_get_ada_task_ptid, t);
00703       /* Do not inherit to_search_memory.  */
00704       INHERIT (to_supports_multi_process, t);
00705       INHERIT (to_supports_enable_disable_tracepoint, t);
00706       INHERIT (to_supports_string_tracing, t);
00707       INHERIT (to_trace_init, t);
00708       INHERIT (to_download_tracepoint, t);
00709       INHERIT (to_can_download_tracepoint, t);
00710       INHERIT (to_download_trace_state_variable, t);
00711       INHERIT (to_enable_tracepoint, t);
00712       INHERIT (to_disable_tracepoint, t);
00713       INHERIT (to_trace_set_readonly_regions, t);
00714       INHERIT (to_trace_start, t);
00715       INHERIT (to_get_trace_status, t);
00716       INHERIT (to_get_tracepoint_status, t);
00717       INHERIT (to_trace_stop, t);
00718       INHERIT (to_trace_find, t);
00719       INHERIT (to_get_trace_state_variable_value, t);
00720       INHERIT (to_save_trace_data, t);
00721       INHERIT (to_upload_tracepoints, t);
00722       INHERIT (to_upload_trace_state_variables, t);
00723       INHERIT (to_get_raw_trace_data, t);
00724       INHERIT (to_get_min_fast_tracepoint_insn_len, t);
00725       INHERIT (to_set_disconnected_tracing, t);
00726       INHERIT (to_set_circular_trace_buffer, t);
00727       INHERIT (to_set_trace_buffer_size, t);
00728       INHERIT (to_set_trace_notes, t);
00729       INHERIT (to_get_tib_address, t);
00730       INHERIT (to_set_permissions, t);
00731       INHERIT (to_static_tracepoint_marker_at, t);
00732       INHERIT (to_static_tracepoint_markers_by_strid, t);
00733       INHERIT (to_traceframe_info, t);
00734       INHERIT (to_use_agent, t);
00735       INHERIT (to_can_use_agent, t);
00736       INHERIT (to_augmented_libraries_svr4_read, t);
00737       INHERIT (to_magic, t);
00738       INHERIT (to_supports_evaluation_of_breakpoint_conditions, t);
00739       INHERIT (to_can_run_breakpoint_commands, t);
00740       /* Do not inherit to_memory_map.  */
00741       /* Do not inherit to_flash_erase.  */
00742       /* Do not inherit to_flash_done.  */
00743     }
00744 #undef INHERIT
00745 
00746   /* Clean up a target struct so it no longer has any zero pointers in
00747      it.  Some entries are defaulted to a method that print an error,
00748      others are hard-wired to a standard recursive default.  */
00749 
00750 #define de_fault(field, value) \
00751   if (!current_target.field)               \
00752     current_target.field = value
00753 
00754   de_fault (to_open,
00755             (void (*) (char *, int))
00756             tcomplain);
00757   de_fault (to_close,
00758             (void (*) (void))
00759             target_ignore);
00760   de_fault (to_post_attach,
00761             (void (*) (int))
00762             target_ignore);
00763   de_fault (to_prepare_to_store,
00764             (void (*) (struct regcache *))
00765             noprocess);
00766   de_fault (deprecated_xfer_memory,
00767             (int (*) (CORE_ADDR, gdb_byte *, int, int,
00768                       struct mem_attrib *, struct target_ops *))
00769             nomemory);
00770   de_fault (to_files_info,
00771             (void (*) (struct target_ops *))
00772             target_ignore);
00773   de_fault (to_insert_breakpoint,
00774             memory_insert_breakpoint);
00775   de_fault (to_remove_breakpoint,
00776             memory_remove_breakpoint);
00777   de_fault (to_can_use_hw_breakpoint,
00778             (int (*) (int, int, int))
00779             return_zero);
00780   de_fault (to_insert_hw_breakpoint,
00781             (int (*) (struct gdbarch *, struct bp_target_info *))
00782             return_minus_one);
00783   de_fault (to_remove_hw_breakpoint,
00784             (int (*) (struct gdbarch *, struct bp_target_info *))
00785             return_minus_one);
00786   de_fault (to_insert_watchpoint,
00787             (int (*) (CORE_ADDR, int, int, struct expression *))
00788             return_minus_one);
00789   de_fault (to_remove_watchpoint,
00790             (int (*) (CORE_ADDR, int, int, struct expression *))
00791             return_minus_one);
00792   de_fault (to_stopped_by_watchpoint,
00793             (int (*) (void))
00794             return_zero);
00795   de_fault (to_stopped_data_address,
00796             (int (*) (struct target_ops *, CORE_ADDR *))
00797             return_zero);
00798   de_fault (to_watchpoint_addr_within_range,
00799             default_watchpoint_addr_within_range);
00800   de_fault (to_region_ok_for_hw_watchpoint,
00801             default_region_ok_for_hw_watchpoint);
00802   de_fault (to_can_accel_watchpoint_condition,
00803             (int (*) (CORE_ADDR, int, int, struct expression *))
00804             return_zero);
00805   de_fault (to_terminal_init,
00806             (void (*) (void))
00807             target_ignore);
00808   de_fault (to_terminal_inferior,
00809             (void (*) (void))
00810             target_ignore);
00811   de_fault (to_terminal_ours_for_output,
00812             (void (*) (void))
00813             target_ignore);
00814   de_fault (to_terminal_ours,
00815             (void (*) (void))
00816             target_ignore);
00817   de_fault (to_terminal_save_ours,
00818             (void (*) (void))
00819             target_ignore);
00820   de_fault (to_terminal_info,
00821             default_terminal_info);
00822   de_fault (to_load,
00823             (void (*) (char *, int))
00824             tcomplain);
00825   de_fault (to_post_startup_inferior,
00826             (void (*) (ptid_t))
00827             target_ignore);
00828   de_fault (to_insert_fork_catchpoint,
00829             (int (*) (int))
00830             return_one);
00831   de_fault (to_remove_fork_catchpoint,
00832             (int (*) (int))
00833             return_one);
00834   de_fault (to_insert_vfork_catchpoint,
00835             (int (*) (int))
00836             return_one);
00837   de_fault (to_remove_vfork_catchpoint,
00838             (int (*) (int))
00839             return_one);
00840   de_fault (to_insert_exec_catchpoint,
00841             (int (*) (int))
00842             return_one);
00843   de_fault (to_remove_exec_catchpoint,
00844             (int (*) (int))
00845             return_one);
00846   de_fault (to_set_syscall_catchpoint,
00847             (int (*) (int, int, int, int, int *))
00848             return_one);
00849   de_fault (to_has_exited,
00850             (int (*) (int, int, int *))
00851             return_zero);
00852   de_fault (to_can_run,
00853             return_zero);
00854   de_fault (to_extra_thread_info,
00855             (char *(*) (struct thread_info *))
00856             return_zero);
00857   de_fault (to_thread_name,
00858             (char *(*) (struct thread_info *))
00859             return_zero);
00860   de_fault (to_stop,
00861             (void (*) (ptid_t))
00862             target_ignore);
00863   current_target.to_xfer_partial = current_xfer_partial;
00864   de_fault (to_rcmd,
00865             (void (*) (char *, struct ui_file *))
00866             tcomplain);
00867   de_fault (to_pid_to_exec_file,
00868             (char *(*) (int))
00869             return_zero);
00870   de_fault (to_async,
00871             (void (*) (void (*) (enum inferior_event_type, void*), void*))
00872             tcomplain);
00873   de_fault (to_thread_architecture,
00874             default_thread_architecture);
00875   current_target.to_read_description = NULL;
00876   de_fault (to_get_ada_task_ptid,
00877             (ptid_t (*) (long, long))
00878             default_get_ada_task_ptid);
00879   de_fault (to_supports_multi_process,
00880             (int (*) (void))
00881             return_zero);
00882   de_fault (to_supports_enable_disable_tracepoint,
00883             (int (*) (void))
00884             return_zero);
00885   de_fault (to_supports_string_tracing,
00886             (int (*) (void))
00887             return_zero);
00888   de_fault (to_trace_init,
00889             (void (*) (void))
00890             tcomplain);
00891   de_fault (to_download_tracepoint,
00892             (void (*) (struct bp_location *))
00893             tcomplain);
00894   de_fault (to_can_download_tracepoint,
00895             (int (*) (void))
00896             return_zero);
00897   de_fault (to_download_trace_state_variable,
00898             (void (*) (struct trace_state_variable *))
00899             tcomplain);
00900   de_fault (to_enable_tracepoint,
00901             (void (*) (struct bp_location *))
00902             tcomplain);
00903   de_fault (to_disable_tracepoint,
00904             (void (*) (struct bp_location *))
00905             tcomplain);
00906   de_fault (to_trace_set_readonly_regions,
00907             (void (*) (void))
00908             tcomplain);
00909   de_fault (to_trace_start,
00910             (void (*) (void))
00911             tcomplain);
00912   de_fault (to_get_trace_status,
00913             (int (*) (struct trace_status *))
00914             return_minus_one);
00915   de_fault (to_get_tracepoint_status,
00916             (void (*) (struct breakpoint *, struct uploaded_tp *))
00917             tcomplain);
00918   de_fault (to_trace_stop,
00919             (void (*) (void))
00920             tcomplain);
00921   de_fault (to_trace_find,
00922             (int (*) (enum trace_find_type, int, CORE_ADDR, CORE_ADDR, int *))
00923             return_minus_one);
00924   de_fault (to_get_trace_state_variable_value,
00925             (int (*) (int, LONGEST *))
00926             return_zero);
00927   de_fault (to_save_trace_data,
00928             (int (*) (const char *))
00929             tcomplain);
00930   de_fault (to_upload_tracepoints,
00931             (int (*) (struct uploaded_tp **))
00932             return_zero);
00933   de_fault (to_upload_trace_state_variables,
00934             (int (*) (struct uploaded_tsv **))
00935             return_zero);
00936   de_fault (to_get_raw_trace_data,
00937             (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
00938             tcomplain);
00939   de_fault (to_get_min_fast_tracepoint_insn_len,
00940             (int (*) (void))
00941             return_minus_one);
00942   de_fault (to_set_disconnected_tracing,
00943             (void (*) (int))
00944             target_ignore);
00945   de_fault (to_set_circular_trace_buffer,
00946             (void (*) (int))
00947             target_ignore);
00948   de_fault (to_set_trace_buffer_size,
00949             (void (*) (LONGEST))
00950             target_ignore);
00951   de_fault (to_set_trace_notes,
00952             (int (*) (const char *, const char *, const char *))
00953             return_zero);
00954   de_fault (to_get_tib_address,
00955             (int (*) (ptid_t, CORE_ADDR *))
00956             tcomplain);
00957   de_fault (to_set_permissions,
00958             (void (*) (void))
00959             target_ignore);
00960   de_fault (to_static_tracepoint_marker_at,
00961             (int (*) (CORE_ADDR, struct static_tracepoint_marker *))
00962             return_zero);
00963   de_fault (to_static_tracepoint_markers_by_strid,
00964             (VEC(static_tracepoint_marker_p) * (*) (const char *))
00965             tcomplain);
00966   de_fault (to_traceframe_info,
00967             (struct traceframe_info * (*) (void))
00968             return_zero);
00969   de_fault (to_supports_evaluation_of_breakpoint_conditions,
00970             (int (*) (void))
00971             return_zero);
00972   de_fault (to_can_run_breakpoint_commands,
00973             (int (*) (void))
00974             return_zero);
00975   de_fault (to_use_agent,
00976             (int (*) (int))
00977             tcomplain);
00978   de_fault (to_can_use_agent,
00979             (int (*) (void))
00980             return_zero);
00981   de_fault (to_augmented_libraries_svr4_read,
00982             (int (*) (void))
00983             return_zero);
00984   de_fault (to_execution_direction, default_execution_direction);
00985 
00986 #undef de_fault
00987 
00988   /* Finally, position the target-stack beneath the squashed
00989      "current_target".  That way code looking for a non-inherited
00990      target method can quickly and simply find it.  */
00991   current_target.beneath = target_stack;
00992 
00993   if (targetdebug)
00994     setup_target_debug ();
00995 }
00996 
00997 /* Push a new target type into the stack of the existing target accessors,
00998    possibly superseding some of the existing accessors.
00999 
01000    Rather than allow an empty stack, we always have the dummy target at
01001    the bottom stratum, so we can call the function vectors without
01002    checking them.  */
01003 
01004 void
01005 push_target (struct target_ops *t)
01006 {
01007   struct target_ops **cur;
01008 
01009   /* Check magic number.  If wrong, it probably means someone changed
01010      the struct definition, but not all the places that initialize one.  */
01011   if (t->to_magic != OPS_MAGIC)
01012     {
01013       fprintf_unfiltered (gdb_stderr,
01014                           "Magic number of %s target struct wrong\n",
01015                           t->to_shortname);
01016       internal_error (__FILE__, __LINE__,
01017                       _("failed internal consistency check"));
01018     }
01019 
01020   /* Find the proper stratum to install this target in.  */
01021   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
01022     {
01023       if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
01024         break;
01025     }
01026 
01027   /* If there's already targets at this stratum, remove them.  */
01028   /* FIXME: cagney/2003-10-15: I think this should be popping all
01029      targets to CUR, and not just those at this stratum level.  */
01030   while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
01031     {
01032       /* There's already something at this stratum level.  Close it,
01033          and un-hook it from the stack.  */
01034       struct target_ops *tmp = (*cur);
01035 
01036       (*cur) = (*cur)->beneath;
01037       tmp->beneath = NULL;
01038       target_close (tmp);
01039     }
01040 
01041   /* We have removed all targets in our stratum, now add the new one.  */
01042   t->beneath = (*cur);
01043   (*cur) = t;
01044 
01045   update_current_target ();
01046 }
01047 
01048 /* Remove a target_ops vector from the stack, wherever it may be.
01049    Return how many times it was removed (0 or 1).  */
01050 
01051 int
01052 unpush_target (struct target_ops *t)
01053 {
01054   struct target_ops **cur;
01055   struct target_ops *tmp;
01056 
01057   if (t->to_stratum == dummy_stratum)
01058     internal_error (__FILE__, __LINE__,
01059                     _("Attempt to unpush the dummy target"));
01060 
01061   /* Look for the specified target.  Note that we assume that a target
01062      can only occur once in the target stack.  */
01063 
01064   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
01065     {
01066       if ((*cur) == t)
01067         break;
01068     }
01069 
01070   /* If we don't find target_ops, quit.  Only open targets should be
01071      closed.  */
01072   if ((*cur) == NULL)
01073     return 0;                   
01074 
01075   /* Unchain the target.  */
01076   tmp = (*cur);
01077   (*cur) = (*cur)->beneath;
01078   tmp->beneath = NULL;
01079 
01080   update_current_target ();
01081 
01082   /* Finally close the target.  Note we do this after unchaining, so
01083      any target method calls from within the target_close
01084      implementation don't end up in T anymore.  */
01085   target_close (t);
01086 
01087   return 1;
01088 }
01089 
01090 void
01091 pop_all_targets_above (enum strata above_stratum)
01092 {
01093   while ((int) (current_target.to_stratum) > (int) above_stratum)
01094     {
01095       if (!unpush_target (target_stack))
01096         {
01097           fprintf_unfiltered (gdb_stderr,
01098                               "pop_all_targets couldn't find target %s\n",
01099                               target_stack->to_shortname);
01100           internal_error (__FILE__, __LINE__,
01101                           _("failed internal consistency check"));
01102           break;
01103         }
01104     }
01105 }
01106 
01107 void
01108 pop_all_targets (void)
01109 {
01110   pop_all_targets_above (dummy_stratum);
01111 }
01112 
01113 /* Return 1 if T is now pushed in the target stack.  Return 0 otherwise.  */
01114 
01115 int
01116 target_is_pushed (struct target_ops *t)
01117 {
01118   struct target_ops **cur;
01119 
01120   /* Check magic number.  If wrong, it probably means someone changed
01121      the struct definition, but not all the places that initialize one.  */
01122   if (t->to_magic != OPS_MAGIC)
01123     {
01124       fprintf_unfiltered (gdb_stderr,
01125                           "Magic number of %s target struct wrong\n",
01126                           t->to_shortname);
01127       internal_error (__FILE__, __LINE__,
01128                       _("failed internal consistency check"));
01129     }
01130 
01131   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
01132     if (*cur == t)
01133       return 1;
01134 
01135   return 0;
01136 }
01137 
01138 /* Using the objfile specified in OBJFILE, find the address for the
01139    current thread's thread-local storage with offset OFFSET.  */
01140 CORE_ADDR
01141 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
01142 {
01143   volatile CORE_ADDR addr = 0;
01144   struct target_ops *target;
01145 
01146   for (target = current_target.beneath;
01147        target != NULL;
01148        target = target->beneath)
01149     {
01150       if (target->to_get_thread_local_address != NULL)
01151         break;
01152     }
01153 
01154   if (target != NULL
01155       && gdbarch_fetch_tls_load_module_address_p (target_gdbarch ()))
01156     {
01157       ptid_t ptid = inferior_ptid;
01158       volatile struct gdb_exception ex;
01159 
01160       TRY_CATCH (ex, RETURN_MASK_ALL)
01161         {
01162           CORE_ADDR lm_addr;
01163           
01164           /* Fetch the load module address for this objfile.  */
01165           lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
01166                                                            objfile);
01167           /* If it's 0, throw the appropriate exception.  */
01168           if (lm_addr == 0)
01169             throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
01170                          _("TLS load module not found"));
01171 
01172           addr = target->to_get_thread_local_address (target, ptid,
01173                                                       lm_addr, offset);
01174         }
01175       /* If an error occurred, print TLS related messages here.  Otherwise,
01176          throw the error to some higher catcher.  */
01177       if (ex.reason < 0)
01178         {
01179           int objfile_is_library = (objfile->flags & OBJF_SHARED);
01180 
01181           switch (ex.error)
01182             {
01183             case TLS_NO_LIBRARY_SUPPORT_ERROR:
01184               error (_("Cannot find thread-local variables "
01185                        "in this thread library."));
01186               break;
01187             case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
01188               if (objfile_is_library)
01189                 error (_("Cannot find shared library `%s' in dynamic"
01190                          " linker's load module list"), objfile_name (objfile));
01191               else
01192                 error (_("Cannot find executable file `%s' in dynamic"
01193                          " linker's load module list"), objfile_name (objfile));
01194               break;
01195             case TLS_NOT_ALLOCATED_YET_ERROR:
01196               if (objfile_is_library)
01197                 error (_("The inferior has not yet allocated storage for"
01198                          " thread-local variables in\n"
01199                          "the shared library `%s'\n"
01200                          "for %s"),
01201                        objfile_name (objfile), target_pid_to_str (ptid));
01202               else
01203                 error (_("The inferior has not yet allocated storage for"
01204                          " thread-local variables in\n"
01205                          "the executable `%s'\n"
01206                          "for %s"),
01207                        objfile_name (objfile), target_pid_to_str (ptid));
01208               break;
01209             case TLS_GENERIC_ERROR:
01210               if (objfile_is_library)
01211                 error (_("Cannot find thread-local storage for %s, "
01212                          "shared library %s:\n%s"),
01213                        target_pid_to_str (ptid),
01214                        objfile_name (objfile), ex.message);
01215               else
01216                 error (_("Cannot find thread-local storage for %s, "
01217                          "executable file %s:\n%s"),
01218                        target_pid_to_str (ptid),
01219                        objfile_name (objfile), ex.message);
01220               break;
01221             default:
01222               throw_exception (ex);
01223               break;
01224             }
01225         }
01226     }
01227   /* It wouldn't be wrong here to try a gdbarch method, too; finding
01228      TLS is an ABI-specific thing.  But we don't do that yet.  */
01229   else
01230     error (_("Cannot find thread-local variables on this target"));
01231 
01232   return addr;
01233 }
01234 
01235 const char *
01236 target_xfer_error_to_string (enum target_xfer_error err)
01237 {
01238 #define CASE(X) case X: return #X
01239   switch (err)
01240     {
01241       CASE(TARGET_XFER_E_IO);
01242       CASE(TARGET_XFER_E_UNAVAILABLE);
01243     default:
01244       return "<unknown>";
01245     }
01246 #undef CASE
01247 };
01248 
01249 
01250 #undef  MIN
01251 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
01252 
01253 /* target_read_string -- read a null terminated string, up to LEN bytes,
01254    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
01255    Set *STRING to a pointer to malloc'd memory containing the data; the caller
01256    is responsible for freeing it.  Return the number of bytes successfully
01257    read.  */
01258 
01259 int
01260 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
01261 {
01262   int tlen, offset, i;
01263   gdb_byte buf[4];
01264   int errcode = 0;
01265   char *buffer;
01266   int buffer_allocated;
01267   char *bufptr;
01268   unsigned int nbytes_read = 0;
01269 
01270   gdb_assert (string);
01271 
01272   /* Small for testing.  */
01273   buffer_allocated = 4;
01274   buffer = xmalloc (buffer_allocated);
01275   bufptr = buffer;
01276 
01277   while (len > 0)
01278     {
01279       tlen = MIN (len, 4 - (memaddr & 3));
01280       offset = memaddr & 3;
01281 
01282       errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
01283       if (errcode != 0)
01284         {
01285           /* The transfer request might have crossed the boundary to an
01286              unallocated region of memory.  Retry the transfer, requesting
01287              a single byte.  */
01288           tlen = 1;
01289           offset = 0;
01290           errcode = target_read_memory (memaddr, buf, 1);
01291           if (errcode != 0)
01292             goto done;
01293         }
01294 
01295       if (bufptr - buffer + tlen > buffer_allocated)
01296         {
01297           unsigned int bytes;
01298 
01299           bytes = bufptr - buffer;
01300           buffer_allocated *= 2;
01301           buffer = xrealloc (buffer, buffer_allocated);
01302           bufptr = buffer + bytes;
01303         }
01304 
01305       for (i = 0; i < tlen; i++)
01306         {
01307           *bufptr++ = buf[i + offset];
01308           if (buf[i + offset] == '\000')
01309             {
01310               nbytes_read += i + 1;
01311               goto done;
01312             }
01313         }
01314 
01315       memaddr += tlen;
01316       len -= tlen;
01317       nbytes_read += tlen;
01318     }
01319 done:
01320   *string = buffer;
01321   if (errnop != NULL)
01322     *errnop = errcode;
01323   return nbytes_read;
01324 }
01325 
01326 struct target_section_table *
01327 target_get_section_table (struct target_ops *target)
01328 {
01329   struct target_ops *t;
01330 
01331   if (targetdebug)
01332     fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
01333 
01334   for (t = target; t != NULL; t = t->beneath)
01335     if (t->to_get_section_table != NULL)
01336       return (*t->to_get_section_table) (t);
01337 
01338   return NULL;
01339 }
01340 
01341 /* Find a section containing ADDR.  */
01342 
01343 struct target_section *
01344 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
01345 {
01346   struct target_section_table *table = target_get_section_table (target);
01347   struct target_section *secp;
01348 
01349   if (table == NULL)
01350     return NULL;
01351 
01352   for (secp = table->sections; secp < table->sections_end; secp++)
01353     {
01354       if (addr >= secp->addr && addr < secp->endaddr)
01355         return secp;
01356     }
01357   return NULL;
01358 }
01359 
01360 /* Read memory from the live target, even if currently inspecting a
01361    traceframe.  The return is the same as that of target_read.  */
01362 
01363 static LONGEST
01364 target_read_live_memory (enum target_object object,
01365                          ULONGEST memaddr, gdb_byte *myaddr, LONGEST len)
01366 {
01367   LONGEST ret;
01368   struct cleanup *cleanup;
01369 
01370   /* Switch momentarily out of tfind mode so to access live memory.
01371      Note that this must not clear global state, such as the frame
01372      cache, which must still remain valid for the previous traceframe.
01373      We may be _building_ the frame cache at this point.  */
01374   cleanup = make_cleanup_restore_traceframe_number ();
01375   set_traceframe_number (-1);
01376 
01377   ret = target_read (current_target.beneath, object, NULL,
01378                      myaddr, memaddr, len);
01379 
01380   do_cleanups (cleanup);
01381   return ret;
01382 }
01383 
01384 /* Using the set of read-only target sections of OPS, read live
01385    read-only memory.  Note that the actual reads start from the
01386    top-most target again.
01387 
01388    For interface/parameters/return description see target.h,
01389    to_xfer_partial.  */
01390 
01391 static LONGEST
01392 memory_xfer_live_readonly_partial (struct target_ops *ops,
01393                                    enum target_object object,
01394                                    gdb_byte *readbuf, ULONGEST memaddr,
01395                                    LONGEST len)
01396 {
01397   struct target_section *secp;
01398   struct target_section_table *table;
01399 
01400   secp = target_section_by_addr (ops, memaddr);
01401   if (secp != NULL
01402       && (bfd_get_section_flags (secp->the_bfd_section->owner,
01403                                  secp->the_bfd_section)
01404           & SEC_READONLY))
01405     {
01406       struct target_section *p;
01407       ULONGEST memend = memaddr + len;
01408 
01409       table = target_get_section_table (ops);
01410 
01411       for (p = table->sections; p < table->sections_end; p++)
01412         {
01413           if (memaddr >= p->addr)
01414             {
01415               if (memend <= p->endaddr)
01416                 {
01417                   /* Entire transfer is within this section.  */
01418                   return target_read_live_memory (object, memaddr,
01419                                                   readbuf, len);
01420                 }
01421               else if (memaddr >= p->endaddr)
01422                 {
01423                   /* This section ends before the transfer starts.  */
01424                   continue;
01425                 }
01426               else
01427                 {
01428                   /* This section overlaps the transfer.  Just do half.  */
01429                   len = p->endaddr - memaddr;
01430                   return target_read_live_memory (object, memaddr,
01431                                                   readbuf, len);
01432                 }
01433             }
01434         }
01435     }
01436 
01437   return 0;
01438 }
01439 
01440 /* Perform a partial memory transfer.
01441    For docs see target.h, to_xfer_partial.  */
01442 
01443 static LONGEST
01444 memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
01445                        void *readbuf, const void *writebuf, ULONGEST memaddr,
01446                        LONGEST len)
01447 {
01448   LONGEST res;
01449   int reg_len;
01450   struct mem_region *region;
01451   struct inferior *inf;
01452 
01453   /* For accesses to unmapped overlay sections, read directly from
01454      files.  Must do this first, as MEMADDR may need adjustment.  */
01455   if (readbuf != NULL && overlay_debugging)
01456     {
01457       struct obj_section *section = find_pc_overlay (memaddr);
01458 
01459       if (pc_in_unmapped_range (memaddr, section))
01460         {
01461           struct target_section_table *table
01462             = target_get_section_table (ops);
01463           const char *section_name = section->the_bfd_section->name;
01464 
01465           memaddr = overlay_mapped_address (memaddr, section);
01466           return section_table_xfer_memory_partial (readbuf, writebuf,
01467                                                     memaddr, len,
01468                                                     table->sections,
01469                                                     table->sections_end,
01470                                                     section_name);
01471         }
01472     }
01473 
01474   /* Try the executable files, if "trust-readonly-sections" is set.  */
01475   if (readbuf != NULL && trust_readonly)
01476     {
01477       struct target_section *secp;
01478       struct target_section_table *table;
01479 
01480       secp = target_section_by_addr (ops, memaddr);
01481       if (secp != NULL
01482           && (bfd_get_section_flags (secp->the_bfd_section->owner,
01483                                      secp->the_bfd_section)
01484               & SEC_READONLY))
01485         {
01486           table = target_get_section_table (ops);
01487           return section_table_xfer_memory_partial (readbuf, writebuf,
01488                                                     memaddr, len,
01489                                                     table->sections,
01490                                                     table->sections_end,
01491                                                     NULL);
01492         }
01493     }
01494 
01495   /* If reading unavailable memory in the context of traceframes, and
01496      this address falls within a read-only section, fallback to
01497      reading from live memory.  */
01498   if (readbuf != NULL && get_traceframe_number () != -1)
01499     {
01500       VEC(mem_range_s) *available;
01501 
01502       /* If we fail to get the set of available memory, then the
01503          target does not support querying traceframe info, and so we
01504          attempt reading from the traceframe anyway (assuming the
01505          target implements the old QTro packet then).  */
01506       if (traceframe_available_memory (&available, memaddr, len))
01507         {
01508           struct cleanup *old_chain;
01509 
01510           old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
01511 
01512           if (VEC_empty (mem_range_s, available)
01513               || VEC_index (mem_range_s, available, 0)->start != memaddr)
01514             {
01515               /* Don't read into the traceframe's available
01516                  memory.  */
01517               if (!VEC_empty (mem_range_s, available))
01518                 {
01519                   LONGEST oldlen = len;
01520 
01521                   len = VEC_index (mem_range_s, available, 0)->start - memaddr;
01522                   gdb_assert (len <= oldlen);
01523                 }
01524 
01525               do_cleanups (old_chain);
01526 
01527               /* This goes through the topmost target again.  */
01528               res = memory_xfer_live_readonly_partial (ops, object,
01529                                                        readbuf, memaddr, len);
01530               if (res > 0)
01531                 return res;
01532 
01533               /* No use trying further, we know some memory starting
01534                  at MEMADDR isn't available.  */
01535               return TARGET_XFER_E_UNAVAILABLE;
01536             }
01537 
01538           /* Don't try to read more than how much is available, in
01539              case the target implements the deprecated QTro packet to
01540              cater for older GDBs (the target's knowledge of read-only
01541              sections may be outdated by now).  */
01542           len = VEC_index (mem_range_s, available, 0)->length;
01543 
01544           do_cleanups (old_chain);
01545         }
01546     }
01547 
01548   /* Try GDB's internal data cache.  */
01549   region = lookup_mem_region (memaddr);
01550   /* region->hi == 0 means there's no upper bound.  */
01551   if (memaddr + len < region->hi || region->hi == 0)
01552     reg_len = len;
01553   else
01554     reg_len = region->hi - memaddr;
01555 
01556   switch (region->attrib.mode)
01557     {
01558     case MEM_RO:
01559       if (writebuf != NULL)
01560         return -1;
01561       break;
01562 
01563     case MEM_WO:
01564       if (readbuf != NULL)
01565         return -1;
01566       break;
01567 
01568     case MEM_FLASH:
01569       /* We only support writing to flash during "load" for now.  */
01570       if (writebuf != NULL)
01571         error (_("Writing to flash memory forbidden in this context"));
01572       break;
01573 
01574     case MEM_NONE:
01575       return -1;
01576     }
01577 
01578   if (!ptid_equal (inferior_ptid, null_ptid))
01579     inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
01580   else
01581     inf = NULL;
01582 
01583   if (inf != NULL
01584       /* The dcache reads whole cache lines; that doesn't play well
01585          with reading from a trace buffer, because reading outside of
01586          the collected memory range fails.  */
01587       && get_traceframe_number () == -1
01588       && (region->attrib.cache
01589           || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
01590     {
01591       if (readbuf != NULL)
01592         res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
01593                                   reg_len, 0);
01594       else
01595         /* FIXME drow/2006-08-09: If we're going to preserve const
01596            correctness dcache_xfer_memory should take readbuf and
01597            writebuf.  */
01598         res = dcache_xfer_memory (ops, target_dcache, memaddr,
01599                                   (void *) writebuf,
01600                                   reg_len, 1);
01601       if (res <= 0)
01602         return -1;
01603       else
01604         return res;
01605     }
01606 
01607   /* If none of those methods found the memory we wanted, fall back
01608      to a target partial transfer.  Normally a single call to
01609      to_xfer_partial is enough; if it doesn't recognize an object
01610      it will call the to_xfer_partial of the next target down.
01611      But for memory this won't do.  Memory is the only target
01612      object which can be read from more than one valid target.
01613      A core file, for instance, could have some of memory but
01614      delegate other bits to the target below it.  So, we must
01615      manually try all targets.  */
01616 
01617   do
01618     {
01619       res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
01620                                   readbuf, writebuf, memaddr, reg_len);
01621       if (res > 0)
01622         break;
01623 
01624       /* We want to continue past core files to executables, but not
01625          past a running target's memory.  */
01626       if (ops->to_has_all_memory (ops))
01627         break;
01628 
01629       ops = ops->beneath;
01630     }
01631   while (ops != NULL);
01632 
01633   /* Make sure the cache gets updated no matter what - if we are writing
01634      to the stack.  Even if this write is not tagged as such, we still need
01635      to update the cache.  */
01636 
01637   if (res > 0
01638       && inf != NULL
01639       && writebuf != NULL
01640       && !region->attrib.cache
01641       && stack_cache_enabled_p
01642       && object != TARGET_OBJECT_STACK_MEMORY)
01643     {
01644       dcache_update (target_dcache, memaddr, (void *) writebuf, res);
01645     }
01646 
01647   /* If we still haven't got anything, return the last error.  We
01648      give up.  */
01649   return res;
01650 }
01651 
01652 /* Perform a partial memory transfer.  For docs see target.h,
01653    to_xfer_partial.  */
01654 
01655 static LONGEST
01656 memory_xfer_partial (struct target_ops *ops, enum target_object object,
01657                      void *readbuf, const void *writebuf, ULONGEST memaddr,
01658                      LONGEST len)
01659 {
01660   int res;
01661 
01662   /* Zero length requests are ok and require no work.  */
01663   if (len == 0)
01664     return 0;
01665 
01666   /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
01667      breakpoint insns, thus hiding out from higher layers whether
01668      there are software breakpoints inserted in the code stream.  */
01669   if (readbuf != NULL)
01670     {
01671       res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len);
01672 
01673       if (res > 0 && !show_memory_breakpoints)
01674         breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, res);
01675     }
01676   else
01677     {
01678       void *buf;
01679       struct cleanup *old_chain;
01680 
01681       buf = xmalloc (len);
01682       old_chain = make_cleanup (xfree, buf);
01683       memcpy (buf, writebuf, len);
01684 
01685       breakpoint_xfer_memory (NULL, buf, writebuf, memaddr, len);
01686       res = memory_xfer_partial_1 (ops, object, NULL, buf, memaddr, len);
01687 
01688       do_cleanups (old_chain);
01689     }
01690 
01691   return res;
01692 }
01693 
01694 static void
01695 restore_show_memory_breakpoints (void *arg)
01696 {
01697   show_memory_breakpoints = (uintptr_t) arg;
01698 }
01699 
01700 struct cleanup *
01701 make_show_memory_breakpoints_cleanup (int show)
01702 {
01703   int current = show_memory_breakpoints;
01704 
01705   show_memory_breakpoints = show;
01706   return make_cleanup (restore_show_memory_breakpoints,
01707                        (void *) (uintptr_t) current);
01708 }
01709 
01710 /* For docs see target.h, to_xfer_partial.  */
01711 
01712 LONGEST
01713 target_xfer_partial (struct target_ops *ops,
01714                      enum target_object object, const char *annex,
01715                      void *readbuf, const void *writebuf,
01716                      ULONGEST offset, LONGEST len)
01717 {
01718   LONGEST retval;
01719 
01720   gdb_assert (ops->to_xfer_partial != NULL);
01721 
01722   if (writebuf && !may_write_memory)
01723     error (_("Writing to memory is not allowed (addr %s, len %s)"),
01724            core_addr_to_string_nz (offset), plongest (len));
01725 
01726   /* If this is a memory transfer, let the memory-specific code
01727      have a look at it instead.  Memory transfers are more
01728      complicated.  */
01729   if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
01730     retval = memory_xfer_partial (ops, object, readbuf,
01731                                   writebuf, offset, len);
01732   else
01733     {
01734       enum target_object raw_object = object;
01735 
01736       /* If this is a raw memory transfer, request the normal
01737          memory object from other layers.  */
01738       if (raw_object == TARGET_OBJECT_RAW_MEMORY)
01739         raw_object = TARGET_OBJECT_MEMORY;
01740 
01741       retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
01742                                      writebuf, offset, len);
01743     }
01744 
01745   if (targetdebug)
01746     {
01747       const unsigned char *myaddr = NULL;
01748 
01749       fprintf_unfiltered (gdb_stdlog,
01750                           "%s:target_xfer_partial "
01751                           "(%d, %s, %s, %s, %s, %s) = %s",
01752                           ops->to_shortname,
01753                           (int) object,
01754                           (annex ? annex : "(null)"),
01755                           host_address_to_string (readbuf),
01756                           host_address_to_string (writebuf),
01757                           core_addr_to_string_nz (offset),
01758                           plongest (len), plongest (retval));
01759 
01760       if (readbuf)
01761         myaddr = readbuf;
01762       if (writebuf)
01763         myaddr = writebuf;
01764       if (retval > 0 && myaddr != NULL)
01765         {
01766           int i;
01767 
01768           fputs_unfiltered (", bytes =", gdb_stdlog);
01769           for (i = 0; i < retval; i++)
01770             {
01771               if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
01772                 {
01773                   if (targetdebug < 2 && i > 0)
01774                     {
01775                       fprintf_unfiltered (gdb_stdlog, " ...");
01776                       break;
01777                     }
01778                   fprintf_unfiltered (gdb_stdlog, "\n");
01779                 }
01780 
01781               fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
01782             }
01783         }
01784 
01785       fputc_unfiltered ('\n', gdb_stdlog);
01786     }
01787   return retval;
01788 }
01789 
01790 /* Read LEN bytes of target memory at address MEMADDR, placing the
01791    results in GDB's memory at MYADDR.  Returns either 0 for success or
01792    a target_xfer_error value if any error occurs.
01793 
01794    If an error occurs, no guarantee is made about the contents of the data at
01795    MYADDR.  In particular, the caller should not depend upon partial reads
01796    filling the buffer with good data.  There is no way for the caller to know
01797    how much good data might have been transfered anyway.  Callers that can
01798    deal with partial reads should call target_read (which will retry until
01799    it makes no progress, and then return how much was transferred).  */
01800 
01801 int
01802 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
01803 {
01804   /* Dispatch to the topmost target, not the flattened current_target.
01805      Memory accesses check target->to_has_(all_)memory, and the
01806      flattened target doesn't inherit those.  */
01807   if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
01808                    myaddr, memaddr, len) == len)
01809     return 0;
01810   else
01811     return TARGET_XFER_E_IO;
01812 }
01813 
01814 /* Like target_read_memory, but specify explicitly that this is a read from
01815    the target's stack.  This may trigger different cache behavior.  */
01816 
01817 int
01818 target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
01819 {
01820   /* Dispatch to the topmost target, not the flattened current_target.
01821      Memory accesses check target->to_has_(all_)memory, and the
01822      flattened target doesn't inherit those.  */
01823 
01824   if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
01825                    myaddr, memaddr, len) == len)
01826     return 0;
01827   else
01828     return TARGET_XFER_E_IO;
01829 }
01830 
01831 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
01832    Returns either 0 for success or a target_xfer_error value if any
01833    error occurs.  If an error occurs, no guarantee is made about how
01834    much data got written.  Callers that can deal with partial writes
01835    should call target_write.  */
01836 
01837 int
01838 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
01839 {
01840   /* Dispatch to the topmost target, not the flattened current_target.
01841      Memory accesses check target->to_has_(all_)memory, and the
01842      flattened target doesn't inherit those.  */
01843   if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
01844                     myaddr, memaddr, len) == len)
01845     return 0;
01846   else
01847     return TARGET_XFER_E_IO;
01848 }
01849 
01850 /* Write LEN bytes from MYADDR to target raw memory at address
01851    MEMADDR.  Returns either 0 for success or a target_xfer_error value
01852    if any error occurs.  If an error occurs, no guarantee is made
01853    about how much data got written.  Callers that can deal with
01854    partial writes should call target_write.  */
01855 
01856 int
01857 target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
01858 {
01859   /* Dispatch to the topmost target, not the flattened current_target.
01860      Memory accesses check target->to_has_(all_)memory, and the
01861      flattened target doesn't inherit those.  */
01862   if (target_write (current_target.beneath, TARGET_OBJECT_RAW_MEMORY, NULL,
01863                     myaddr, memaddr, len) == len)
01864     return 0;
01865   else
01866     return TARGET_XFER_E_IO;
01867 }
01868 
01869 /* Fetch the target's memory map.  */
01870 
01871 VEC(mem_region_s) *
01872 target_memory_map (void)
01873 {
01874   VEC(mem_region_s) *result;
01875   struct mem_region *last_one, *this_one;
01876   int ix;
01877   struct target_ops *t;
01878 
01879   if (targetdebug)
01880     fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
01881 
01882   for (t = current_target.beneath; t != NULL; t = t->beneath)
01883     if (t->to_memory_map != NULL)
01884       break;
01885 
01886   if (t == NULL)
01887     return NULL;
01888 
01889   result = t->to_memory_map (t);
01890   if (result == NULL)
01891     return NULL;
01892 
01893   qsort (VEC_address (mem_region_s, result),
01894          VEC_length (mem_region_s, result),
01895          sizeof (struct mem_region), mem_region_cmp);
01896 
01897   /* Check that regions do not overlap.  Simultaneously assign
01898      a numbering for the "mem" commands to use to refer to
01899      each region.  */
01900   last_one = NULL;
01901   for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
01902     {
01903       this_one->number = ix;
01904 
01905       if (last_one && last_one->hi > this_one->lo)
01906         {
01907           warning (_("Overlapping regions in memory map: ignoring"));
01908           VEC_free (mem_region_s, result);
01909           return NULL;
01910         }
01911       last_one = this_one;
01912     }
01913 
01914   return result;
01915 }
01916 
01917 void
01918 target_flash_erase (ULONGEST address, LONGEST length)
01919 {
01920   struct target_ops *t;
01921 
01922   for (t = current_target.beneath; t != NULL; t = t->beneath)
01923     if (t->to_flash_erase != NULL)
01924       {
01925         if (targetdebug)
01926           fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
01927                               hex_string (address), phex (length, 0));
01928         t->to_flash_erase (t, address, length);
01929         return;
01930       }
01931 
01932   tcomplain ();
01933 }
01934 
01935 void
01936 target_flash_done (void)
01937 {
01938   struct target_ops *t;
01939 
01940   for (t = current_target.beneath; t != NULL; t = t->beneath)
01941     if (t->to_flash_done != NULL)
01942       {
01943         if (targetdebug)
01944           fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
01945         t->to_flash_done (t);
01946         return;
01947       }
01948 
01949   tcomplain ();
01950 }
01951 
01952 static void
01953 show_trust_readonly (struct ui_file *file, int from_tty,
01954                      struct cmd_list_element *c, const char *value)
01955 {
01956   fprintf_filtered (file,
01957                     _("Mode for reading from readonly sections is %s.\n"),
01958                     value);
01959 }
01960 
01961 /* More generic transfers.  */
01962 
01963 static LONGEST
01964 default_xfer_partial (struct target_ops *ops, enum target_object object,
01965                       const char *annex, gdb_byte *readbuf,
01966                       const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
01967 {
01968   if (object == TARGET_OBJECT_MEMORY
01969       && ops->deprecated_xfer_memory != NULL)
01970     /* If available, fall back to the target's
01971        "deprecated_xfer_memory" method.  */
01972     {
01973       int xfered = -1;
01974 
01975       errno = 0;
01976       if (writebuf != NULL)
01977         {
01978           void *buffer = xmalloc (len);
01979           struct cleanup *cleanup = make_cleanup (xfree, buffer);
01980 
01981           memcpy (buffer, writebuf, len);
01982           xfered = ops->deprecated_xfer_memory (offset, buffer, len,
01983                                                 1/*write*/, NULL, ops);
01984           do_cleanups (cleanup);
01985         }
01986       if (readbuf != NULL)
01987         xfered = ops->deprecated_xfer_memory (offset, readbuf, len, 
01988                                               0/*read*/, NULL, ops);
01989       if (xfered > 0)
01990         return xfered;
01991       else if (xfered == 0 && errno == 0)
01992         /* "deprecated_xfer_memory" uses 0, cross checked against
01993            ERRNO as one indication of an error.  */
01994         return 0;
01995       else
01996         return -1;
01997     }
01998   else if (ops->beneath != NULL)
01999     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
02000                                           readbuf, writebuf, offset, len);
02001   else
02002     return -1;
02003 }
02004 
02005 /* The xfer_partial handler for the topmost target.  Unlike the default,
02006    it does not need to handle memory specially; it just passes all
02007    requests down the stack.  */
02008 
02009 static LONGEST
02010 current_xfer_partial (struct target_ops *ops, enum target_object object,
02011                       const char *annex, gdb_byte *readbuf,
02012                       const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
02013 {
02014   if (ops->beneath != NULL)
02015     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
02016                                           readbuf, writebuf, offset, len);
02017   else
02018     return -1;
02019 }
02020 
02021 /* Target vector read/write partial wrapper functions.  */
02022 
02023 static LONGEST
02024 target_read_partial (struct target_ops *ops,
02025                      enum target_object object,
02026                      const char *annex, gdb_byte *buf,
02027                      ULONGEST offset, LONGEST len)
02028 {
02029   return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
02030 }
02031 
02032 static LONGEST
02033 target_write_partial (struct target_ops *ops,
02034                       enum target_object object,
02035                       const char *annex, const gdb_byte *buf,
02036                       ULONGEST offset, LONGEST len)
02037 {
02038   return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
02039 }
02040 
02041 /* Wrappers to perform the full transfer.  */
02042 
02043 /* For docs on target_read see target.h.  */
02044 
02045 LONGEST
02046 target_read (struct target_ops *ops,
02047              enum target_object object,
02048              const char *annex, gdb_byte *buf,
02049              ULONGEST offset, LONGEST len)
02050 {
02051   LONGEST xfered = 0;
02052 
02053   while (xfered < len)
02054     {
02055       LONGEST xfer = target_read_partial (ops, object, annex,
02056                                           (gdb_byte *) buf + xfered,
02057                                           offset + xfered, len - xfered);
02058 
02059       /* Call an observer, notifying them of the xfer progress?  */
02060       if (xfer == 0)
02061         return xfered;
02062       if (xfer < 0)
02063         return -1;
02064       xfered += xfer;
02065       QUIT;
02066     }
02067   return len;
02068 }
02069 
02070 /* Assuming that the entire [begin, end) range of memory cannot be
02071    read, try to read whatever subrange is possible to read.
02072 
02073    The function returns, in RESULT, either zero or one memory block.
02074    If there's a readable subrange at the beginning, it is completely
02075    read and returned.  Any further readable subrange will not be read.
02076    Otherwise, if there's a readable subrange at the end, it will be
02077    completely read and returned.  Any readable subranges before it
02078    (obviously, not starting at the beginning), will be ignored.  In
02079    other cases -- either no readable subrange, or readable subrange(s)
02080    that is neither at the beginning, or end, nothing is returned.
02081 
02082    The purpose of this function is to handle a read across a boundary
02083    of accessible memory in a case when memory map is not available.
02084    The above restrictions are fine for this case, but will give
02085    incorrect results if the memory is 'patchy'.  However, supporting
02086    'patchy' memory would require trying to read every single byte,
02087    and it seems unacceptable solution.  Explicit memory map is
02088    recommended for this case -- and target_read_memory_robust will
02089    take care of reading multiple ranges then.  */
02090 
02091 static void
02092 read_whatever_is_readable (struct target_ops *ops,
02093                            ULONGEST begin, ULONGEST end,
02094                            VEC(memory_read_result_s) **result)
02095 {
02096   gdb_byte *buf = xmalloc (end - begin);
02097   ULONGEST current_begin = begin;
02098   ULONGEST current_end = end;
02099   int forward;
02100   memory_read_result_s r;
02101 
02102   /* If we previously failed to read 1 byte, nothing can be done here.  */
02103   if (end - begin <= 1)
02104     {
02105       xfree (buf);
02106       return;
02107     }
02108 
02109   /* Check that either first or the last byte is readable, and give up
02110      if not.  This heuristic is meant to permit reading accessible memory
02111      at the boundary of accessible region.  */
02112   if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
02113                            buf, begin, 1) == 1)
02114     {
02115       forward = 1;
02116       ++current_begin;
02117     }
02118   else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
02119                                 buf + (end-begin) - 1, end - 1, 1) == 1)
02120     {
02121       forward = 0;
02122       --current_end;
02123     }
02124   else
02125     {
02126       xfree (buf);
02127       return;
02128     }
02129 
02130   /* Loop invariant is that the [current_begin, current_end) was previously
02131      found to be not readable as a whole.
02132 
02133      Note loop condition -- if the range has 1 byte, we can't divide the range
02134      so there's no point trying further.  */
02135   while (current_end - current_begin > 1)
02136     {
02137       ULONGEST first_half_begin, first_half_end;
02138       ULONGEST second_half_begin, second_half_end;
02139       LONGEST xfer;
02140       ULONGEST middle = current_begin + (current_end - current_begin)/2;
02141 
02142       if (forward)
02143         {
02144           first_half_begin = current_begin;
02145           first_half_end = middle;
02146           second_half_begin = middle;
02147           second_half_end = current_end;
02148         }
02149       else
02150         {
02151           first_half_begin = middle;
02152           first_half_end = current_end;
02153           second_half_begin = current_begin;
02154           second_half_end = middle;
02155         }
02156 
02157       xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
02158                           buf + (first_half_begin - begin),
02159                           first_half_begin,
02160                           first_half_end - first_half_begin);
02161 
02162       if (xfer == first_half_end - first_half_begin)
02163         {
02164           /* This half reads up fine.  So, the error must be in the
02165              other half.  */
02166           current_begin = second_half_begin;
02167           current_end = second_half_end;
02168         }
02169       else
02170         {
02171           /* This half is not readable.  Because we've tried one byte, we
02172              know some part of this half if actually redable.  Go to the next
02173              iteration to divide again and try to read.
02174 
02175              We don't handle the other half, because this function only tries
02176              to read a single readable subrange.  */
02177           current_begin = first_half_begin;
02178           current_end = first_half_end;
02179         }
02180     }
02181 
02182   if (forward)
02183     {
02184       /* The [begin, current_begin) range has been read.  */
02185       r.begin = begin;
02186       r.end = current_begin;
02187       r.data = buf;
02188     }
02189   else
02190     {
02191       /* The [current_end, end) range has been read.  */
02192       LONGEST rlen = end - current_end;
02193 
02194       r.data = xmalloc (rlen);
02195       memcpy (r.data, buf + current_end - begin, rlen);
02196       r.begin = current_end;
02197       r.end = end;
02198       xfree (buf);
02199     }
02200   VEC_safe_push(memory_read_result_s, (*result), &r);
02201 }
02202 
02203 void
02204 free_memory_read_result_vector (void *x)
02205 {
02206   VEC(memory_read_result_s) *v = x;
02207   memory_read_result_s *current;
02208   int ix;
02209 
02210   for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
02211     {
02212       xfree (current->data);
02213     }
02214   VEC_free (memory_read_result_s, v);
02215 }
02216 
02217 VEC(memory_read_result_s) *
02218 read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
02219 {
02220   VEC(memory_read_result_s) *result = 0;
02221 
02222   LONGEST xfered = 0;
02223   while (xfered < len)
02224     {
02225       struct mem_region *region = lookup_mem_region (offset + xfered);
02226       LONGEST rlen;
02227 
02228       /* If there is no explicit region, a fake one should be created.  */
02229       gdb_assert (region);
02230 
02231       if (region->hi == 0)
02232         rlen = len - xfered;
02233       else
02234         rlen = region->hi - offset;
02235 
02236       if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
02237         {
02238           /* Cannot read this region.  Note that we can end up here only
02239              if the region is explicitly marked inaccessible, or
02240              'inaccessible-by-default' is in effect.  */
02241           xfered += rlen;
02242         }
02243       else
02244         {
02245           LONGEST to_read = min (len - xfered, rlen);
02246           gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
02247 
02248           LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
02249                                       (gdb_byte *) buffer,
02250                                       offset + xfered, to_read);
02251           /* Call an observer, notifying them of the xfer progress?  */
02252           if (xfer <= 0)
02253             {
02254               /* Got an error reading full chunk.  See if maybe we can read
02255                  some subrange.  */
02256               xfree (buffer);
02257               read_whatever_is_readable (ops, offset + xfered,
02258                                          offset + xfered + to_read, &result);
02259               xfered += to_read;
02260             }
02261           else
02262             {
02263               struct memory_read_result r;
02264               r.data = buffer;
02265               r.begin = offset + xfered;
02266               r.end = r.begin + xfer;
02267               VEC_safe_push (memory_read_result_s, result, &r);
02268               xfered += xfer;
02269             }
02270           QUIT;
02271         }
02272     }
02273   return result;
02274 }
02275 
02276 
02277 /* An alternative to target_write with progress callbacks.  */
02278 
02279 LONGEST
02280 target_write_with_progress (struct target_ops *ops,
02281                             enum target_object object,
02282                             const char *annex, const gdb_byte *buf,
02283                             ULONGEST offset, LONGEST len,
02284                             void (*progress) (ULONGEST, void *), void *baton)
02285 {
02286   LONGEST xfered = 0;
02287 
02288   /* Give the progress callback a chance to set up.  */
02289   if (progress)
02290     (*progress) (0, baton);
02291 
02292   while (xfered < len)
02293     {
02294       LONGEST xfer = target_write_partial (ops, object, annex,
02295                                            (gdb_byte *) buf + xfered,
02296                                            offset + xfered, len - xfered);
02297 
02298       if (xfer == 0)
02299         return xfered;
02300       if (xfer < 0)
02301         return -1;
02302 
02303       if (progress)
02304         (*progress) (xfer, baton);
02305 
02306       xfered += xfer;
02307       QUIT;
02308     }
02309   return len;
02310 }
02311 
02312 /* For docs on target_write see target.h.  */
02313 
02314 LONGEST
02315 target_write (struct target_ops *ops,
02316               enum target_object object,
02317               const char *annex, const gdb_byte *buf,
02318               ULONGEST offset, LONGEST len)
02319 {
02320   return target_write_with_progress (ops, object, annex, buf, offset, len,
02321                                      NULL, NULL);
02322 }
02323 
02324 /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
02325    the size of the transferred data.  PADDING additional bytes are
02326    available in *BUF_P.  This is a helper function for
02327    target_read_alloc; see the declaration of that function for more
02328    information.  */
02329 
02330 static LONGEST
02331 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
02332                      const char *annex, gdb_byte **buf_p, int padding)
02333 {
02334   size_t buf_alloc, buf_pos;
02335   gdb_byte *buf;
02336   LONGEST n;
02337 
02338   /* This function does not have a length parameter; it reads the
02339      entire OBJECT).  Also, it doesn't support objects fetched partly
02340      from one target and partly from another (in a different stratum,
02341      e.g. a core file and an executable).  Both reasons make it
02342      unsuitable for reading memory.  */
02343   gdb_assert (object != TARGET_OBJECT_MEMORY);
02344 
02345   /* Start by reading up to 4K at a time.  The target will throttle
02346      this number down if necessary.  */
02347   buf_alloc = 4096;
02348   buf = xmalloc (buf_alloc);
02349   buf_pos = 0;
02350   while (1)
02351     {
02352       n = target_read_partial (ops, object, annex, &buf[buf_pos],
02353                                buf_pos, buf_alloc - buf_pos - padding);
02354       if (n < 0)
02355         {
02356           /* An error occurred.  */
02357           xfree (buf);
02358           return -1;
02359         }
02360       else if (n == 0)
02361         {
02362           /* Read all there was.  */
02363           if (buf_pos == 0)
02364             xfree (buf);
02365           else
02366             *buf_p = buf;
02367           return buf_pos;
02368         }
02369 
02370       buf_pos += n;
02371 
02372       /* If the buffer is filling up, expand it.  */
02373       if (buf_alloc < buf_pos * 2)
02374         {
02375           buf_alloc *= 2;
02376           buf = xrealloc (buf, buf_alloc);
02377         }
02378 
02379       QUIT;
02380     }
02381 }
02382 
02383 /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
02384    the size of the transferred data.  See the declaration in "target.h"
02385    function for more information about the return value.  */
02386 
02387 LONGEST
02388 target_read_alloc (struct target_ops *ops, enum target_object object,
02389                    const char *annex, gdb_byte **buf_p)
02390 {
02391   return target_read_alloc_1 (ops, object, annex, buf_p, 0);
02392 }
02393 
02394 /* Read OBJECT/ANNEX using OPS.  The result is NUL-terminated and
02395    returned as a string, allocated using xmalloc.  If an error occurs
02396    or the transfer is unsupported, NULL is returned.  Empty objects
02397    are returned as allocated but empty strings.  A warning is issued
02398    if the result contains any embedded NUL bytes.  */
02399 
02400 char *
02401 target_read_stralloc (struct target_ops *ops, enum target_object object,
02402                       const char *annex)
02403 {
02404   gdb_byte *buffer;
02405   char *bufstr;
02406   LONGEST i, transferred;
02407 
02408   transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
02409   bufstr = (char *) buffer;
02410 
02411   if (transferred < 0)
02412     return NULL;
02413 
02414   if (transferred == 0)
02415     return xstrdup ("");
02416 
02417   bufstr[transferred] = 0;
02418 
02419   /* Check for embedded NUL bytes; but allow trailing NULs.  */
02420   for (i = strlen (bufstr); i < transferred; i++)
02421     if (bufstr[i] != 0)
02422       {
02423         warning (_("target object %d, annex %s, "
02424                    "contained unexpected null characters"),
02425                  (int) object, annex ? annex : "(none)");
02426         break;
02427       }
02428 
02429   return bufstr;
02430 }
02431 
02432 /* Memory transfer methods.  */
02433 
02434 void
02435 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
02436                    LONGEST len)
02437 {
02438   /* This method is used to read from an alternate, non-current
02439      target.  This read must bypass the overlay support (as symbols
02440      don't match this target), and GDB's internal cache (wrong cache
02441      for this target).  */
02442   if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
02443       != len)
02444     memory_error (TARGET_XFER_E_IO, addr);
02445 }
02446 
02447 ULONGEST
02448 get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
02449                             int len, enum bfd_endian byte_order)
02450 {
02451   gdb_byte buf[sizeof (ULONGEST)];
02452 
02453   gdb_assert (len <= sizeof (buf));
02454   get_target_memory (ops, addr, buf, len);
02455   return extract_unsigned_integer (buf, len, byte_order);
02456 }
02457 
02458 int
02459 target_insert_breakpoint (struct gdbarch *gdbarch,
02460                           struct bp_target_info *bp_tgt)
02461 {
02462   if (!may_insert_breakpoints)
02463     {
02464       warning (_("May not insert breakpoints"));
02465       return 1;
02466     }
02467 
02468   return (*current_target.to_insert_breakpoint) (gdbarch, bp_tgt);
02469 }
02470 
02471 int
02472 target_remove_breakpoint (struct gdbarch *gdbarch,
02473                           struct bp_target_info *bp_tgt)
02474 {
02475   /* This is kind of a weird case to handle, but the permission might
02476      have been changed after breakpoints were inserted - in which case
02477      we should just take the user literally and assume that any
02478      breakpoints should be left in place.  */
02479   if (!may_insert_breakpoints)
02480     {
02481       warning (_("May not remove breakpoints"));
02482       return 1;
02483     }
02484 
02485   return (*current_target.to_remove_breakpoint) (gdbarch, bp_tgt);
02486 }
02487 
02488 static void
02489 target_info (char *args, int from_tty)
02490 {
02491   struct target_ops *t;
02492   int has_all_mem = 0;
02493 
02494   if (symfile_objfile != NULL)
02495     printf_unfiltered (_("Symbols from \"%s\".\n"),
02496                        objfile_name (symfile_objfile));
02497 
02498   for (t = target_stack; t != NULL; t = t->beneath)
02499     {
02500       if (!(*t->to_has_memory) (t))
02501         continue;
02502 
02503       if ((int) (t->to_stratum) <= (int) dummy_stratum)
02504         continue;
02505       if (has_all_mem)
02506         printf_unfiltered (_("\tWhile running this, "
02507                              "GDB does not access memory from...\n"));
02508       printf_unfiltered ("%s:\n", t->to_longname);
02509       (t->to_files_info) (t);
02510       has_all_mem = (*t->to_has_all_memory) (t);
02511     }
02512 }
02513 
02514 /* This function is called before any new inferior is created, e.g.
02515    by running a program, attaching, or connecting to a target.
02516    It cleans up any state from previous invocations which might
02517    change between runs.  This is a subset of what target_preopen
02518    resets (things which might change between targets).  */
02519 
02520 void
02521 target_pre_inferior (int from_tty)
02522 {
02523   /* Clear out solib state.  Otherwise the solib state of the previous
02524      inferior might have survived and is entirely wrong for the new
02525      target.  This has been observed on GNU/Linux using glibc 2.3.  How
02526      to reproduce:
02527 
02528      bash$ ./foo&
02529      [1] 4711
02530      bash$ ./foo&
02531      [1] 4712
02532      bash$ gdb ./foo
02533      [...]
02534      (gdb) attach 4711
02535      (gdb) detach
02536      (gdb) attach 4712
02537      Cannot access memory at address 0xdeadbeef
02538   */
02539 
02540   /* In some OSs, the shared library list is the same/global/shared
02541      across inferiors.  If code is shared between processes, so are
02542      memory regions and features.  */
02543   if (!gdbarch_has_global_solist (target_gdbarch ()))
02544     {
02545       no_shared_libraries (NULL, from_tty);
02546 
02547       invalidate_target_mem_regions ();
02548 
02549       target_clear_description ();
02550     }
02551 
02552   agent_capability_invalidate ();
02553 }
02554 
02555 /* Callback for iterate_over_inferiors.  Gets rid of the given
02556    inferior.  */
02557 
02558 static int
02559 dispose_inferior (struct inferior *inf, void *args)
02560 {
02561   struct thread_info *thread;
02562 
02563   thread = any_thread_of_process (inf->pid);
02564   if (thread)
02565     {
02566       switch_to_thread (thread->ptid);
02567 
02568       /* Core inferiors actually should be detached, not killed.  */
02569       if (target_has_execution)
02570         target_kill ();
02571       else
02572         target_detach (NULL, 0);
02573     }
02574 
02575   return 0;
02576 }
02577 
02578 /* This is to be called by the open routine before it does
02579    anything.  */
02580 
02581 void
02582 target_preopen (int from_tty)
02583 {
02584   dont_repeat ();
02585 
02586   if (have_inferiors ())
02587     {
02588       if (!from_tty
02589           || !have_live_inferiors ()
02590           || query (_("A program is being debugged already.  Kill it? ")))
02591         iterate_over_inferiors (dispose_inferior, NULL);
02592       else
02593         error (_("Program not killed."));
02594     }
02595 
02596   /* Calling target_kill may remove the target from the stack.  But if
02597      it doesn't (which seems like a win for UDI), remove it now.  */
02598   /* Leave the exec target, though.  The user may be switching from a
02599      live process to a core of the same program.  */
02600   pop_all_targets_above (file_stratum);
02601 
02602   target_pre_inferior (from_tty);
02603 }
02604 
02605 /* Detach a target after doing deferred register stores.  */
02606 
02607 void
02608 target_detach (char *args, int from_tty)
02609 {
02610   struct target_ops* t;
02611   
02612   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
02613     /* Don't remove global breakpoints here.  They're removed on
02614        disconnection from the target.  */
02615     ;
02616   else
02617     /* If we're in breakpoints-always-inserted mode, have to remove
02618        them before detaching.  */
02619     remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
02620 
02621   prepare_for_detach ();
02622 
02623   for (t = current_target.beneath; t != NULL; t = t->beneath)
02624     {
02625       if (t->to_detach != NULL)
02626         {
02627           t->to_detach (t, args, from_tty);
02628           if (targetdebug)
02629             fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
02630                                 args, from_tty);
02631           return;
02632         }
02633     }
02634 
02635   internal_error (__FILE__, __LINE__, _("could not find a target to detach"));
02636 }
02637 
02638 void
02639 target_disconnect (char *args, int from_tty)
02640 {
02641   struct target_ops *t;
02642 
02643   /* If we're in breakpoints-always-inserted mode or if breakpoints
02644      are global across processes, we have to remove them before
02645      disconnecting.  */
02646   remove_breakpoints ();
02647 
02648   for (t = current_target.beneath; t != NULL; t = t->beneath)
02649     if (t->to_disconnect != NULL)
02650         {
02651           if (targetdebug)
02652             fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
02653                                 args, from_tty);
02654           t->to_disconnect (t, args, from_tty);
02655           return;
02656         }
02657 
02658   tcomplain ();
02659 }
02660 
02661 ptid_t
02662 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
02663 {
02664   struct target_ops *t;
02665 
02666   for (t = current_target.beneath; t != NULL; t = t->beneath)
02667     {
02668       if (t->to_wait != NULL)
02669         {
02670           ptid_t retval = (*t->to_wait) (t, ptid, status, options);
02671 
02672           if (targetdebug)
02673             {
02674               char *status_string;
02675               char *options_string;
02676 
02677               status_string = target_waitstatus_to_string (status);
02678               options_string = target_options_to_string (options);
02679               fprintf_unfiltered (gdb_stdlog,
02680                                   "target_wait (%d, status, options={%s})"
02681                                   " = %d,   %s\n",
02682                                   ptid_get_pid (ptid), options_string,
02683                                   ptid_get_pid (retval), status_string);
02684               xfree (status_string);
02685               xfree (options_string);
02686             }
02687 
02688           return retval;
02689         }
02690     }
02691 
02692   noprocess ();
02693 }
02694 
02695 char *
02696 target_pid_to_str (ptid_t ptid)
02697 {
02698   struct target_ops *t;
02699 
02700   for (t = current_target.beneath; t != NULL; t = t->beneath)
02701     {
02702       if (t->to_pid_to_str != NULL)
02703         return (*t->to_pid_to_str) (t, ptid);
02704     }
02705 
02706   return normal_pid_to_str (ptid);
02707 }
02708 
02709 char *
02710 target_thread_name (struct thread_info *info)
02711 {
02712   struct target_ops *t;
02713 
02714   for (t = current_target.beneath; t != NULL; t = t->beneath)
02715     {
02716       if (t->to_thread_name != NULL)
02717         return (*t->to_thread_name) (info);
02718     }
02719 
02720   return NULL;
02721 }
02722 
02723 void
02724 target_resume (ptid_t ptid, int step, enum gdb_signal signal)
02725 {
02726   struct target_ops *t;
02727 
02728   target_dcache_invalidate ();
02729 
02730   for (t = current_target.beneath; t != NULL; t = t->beneath)
02731     {
02732       if (t->to_resume != NULL)
02733         {
02734           t->to_resume (t, ptid, step, signal);
02735           if (targetdebug)
02736             fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
02737                                 ptid_get_pid (ptid),
02738                                 step ? "step" : "continue",
02739                                 gdb_signal_to_name (signal));
02740 
02741           registers_changed_ptid (ptid);
02742           set_executing (ptid, 1);
02743           set_running (ptid, 1);
02744           clear_inline_frame_state (ptid);
02745           return;
02746         }
02747     }
02748 
02749   noprocess ();
02750 }
02751 
02752 void
02753 target_pass_signals (int numsigs, unsigned char *pass_signals)
02754 {
02755   struct target_ops *t;
02756 
02757   for (t = current_target.beneath; t != NULL; t = t->beneath)
02758     {
02759       if (t->to_pass_signals != NULL)
02760         {
02761           if (targetdebug)
02762             {
02763               int i;
02764 
02765               fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
02766                                   numsigs);
02767 
02768               for (i = 0; i < numsigs; i++)
02769                 if (pass_signals[i])
02770                   fprintf_unfiltered (gdb_stdlog, " %s",
02771                                       gdb_signal_to_name (i));
02772 
02773               fprintf_unfiltered (gdb_stdlog, " })\n");
02774             }
02775 
02776           (*t->to_pass_signals) (numsigs, pass_signals);
02777           return;
02778         }
02779     }
02780 }
02781 
02782 void
02783 target_program_signals (int numsigs, unsigned char *program_signals)
02784 {
02785   struct target_ops *t;
02786 
02787   for (t = current_target.beneath; t != NULL; t = t->beneath)
02788     {
02789       if (t->to_program_signals != NULL)
02790         {
02791           if (targetdebug)
02792             {
02793               int i;
02794 
02795               fprintf_unfiltered (gdb_stdlog, "target_program_signals (%d, {",
02796                                   numsigs);
02797 
02798               for (i = 0; i < numsigs; i++)
02799                 if (program_signals[i])
02800                   fprintf_unfiltered (gdb_stdlog, " %s",
02801                                       gdb_signal_to_name (i));
02802 
02803               fprintf_unfiltered (gdb_stdlog, " })\n");
02804             }
02805 
02806           (*t->to_program_signals) (numsigs, program_signals);
02807           return;
02808         }
02809     }
02810 }
02811 
02812 /* Look through the list of possible targets for a target that can
02813    follow forks.  */
02814 
02815 int
02816 target_follow_fork (int follow_child, int detach_fork)
02817 {
02818   struct target_ops *t;
02819 
02820   for (t = current_target.beneath; t != NULL; t = t->beneath)
02821     {
02822       if (t->to_follow_fork != NULL)
02823         {
02824           int retval = t->to_follow_fork (t, follow_child, detach_fork);
02825 
02826           if (targetdebug)
02827             fprintf_unfiltered (gdb_stdlog,
02828                                 "target_follow_fork (%d, %d) = %d\n",
02829                                 follow_child, detach_fork, retval);
02830           return retval;
02831         }
02832     }
02833 
02834   /* Some target returned a fork event, but did not know how to follow it.  */
02835   internal_error (__FILE__, __LINE__,
02836                   _("could not find a target to follow fork"));
02837 }
02838 
02839 void
02840 target_mourn_inferior (void)
02841 {
02842   struct target_ops *t;
02843 
02844   for (t = current_target.beneath; t != NULL; t = t->beneath)
02845     {
02846       if (t->to_mourn_inferior != NULL) 
02847         {
02848           t->to_mourn_inferior (t);
02849           if (targetdebug)
02850             fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
02851 
02852           /* We no longer need to keep handles on any of the object files.
02853              Make sure to release them to avoid unnecessarily locking any
02854              of them while we're not actually debugging.  */
02855           bfd_cache_close_all ();
02856 
02857           return;
02858         }
02859     }
02860 
02861   internal_error (__FILE__, __LINE__,
02862                   _("could not find a target to follow mourn inferior"));
02863 }
02864 
02865 /* Look for a target which can describe architectural features, starting
02866    from TARGET.  If we find one, return its description.  */
02867 
02868 const struct target_desc *
02869 target_read_description (struct target_ops *target)
02870 {
02871   struct target_ops *t;
02872 
02873   for (t = target; t != NULL; t = t->beneath)
02874     if (t->to_read_description != NULL)
02875       {
02876         const struct target_desc *tdesc;
02877 
02878         tdesc = t->to_read_description (t);
02879         if (tdesc)
02880           return tdesc;
02881       }
02882 
02883   return NULL;
02884 }
02885 
02886 /* The default implementation of to_search_memory.
02887    This implements a basic search of memory, reading target memory and
02888    performing the search here (as opposed to performing the search in on the
02889    target side with, for example, gdbserver).  */
02890 
02891 int
02892 simple_search_memory (struct target_ops *ops,
02893                       CORE_ADDR start_addr, ULONGEST search_space_len,
02894                       const gdb_byte *pattern, ULONGEST pattern_len,
02895                       CORE_ADDR *found_addrp)
02896 {
02897   /* NOTE: also defined in find.c testcase.  */
02898 #define SEARCH_CHUNK_SIZE 16000
02899   const unsigned chunk_size = SEARCH_CHUNK_SIZE;
02900   /* Buffer to hold memory contents for searching.  */
02901   gdb_byte *search_buf;
02902   unsigned search_buf_size;
02903   struct cleanup *old_cleanups;
02904 
02905   search_buf_size = chunk_size + pattern_len - 1;
02906 
02907   /* No point in trying to allocate a buffer larger than the search space.  */
02908   if (search_space_len < search_buf_size)
02909     search_buf_size = search_space_len;
02910 
02911   search_buf = malloc (search_buf_size);
02912   if (search_buf == NULL)
02913     error (_("Unable to allocate memory to perform the search."));
02914   old_cleanups = make_cleanup (free_current_contents, &search_buf);
02915 
02916   /* Prime the search buffer.  */
02917 
02918   if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
02919                    search_buf, start_addr, search_buf_size) != search_buf_size)
02920     {
02921       warning (_("Unable to access %s bytes of target "
02922                  "memory at %s, halting search."),
02923                pulongest (search_buf_size), hex_string (start_addr));
02924       do_cleanups (old_cleanups);
02925       return -1;
02926     }
02927 
02928   /* Perform the search.
02929 
02930      The loop is kept simple by allocating [N + pattern-length - 1] bytes.
02931      When we've scanned N bytes we copy the trailing bytes to the start and
02932      read in another N bytes.  */
02933 
02934   while (search_space_len >= pattern_len)
02935     {
02936       gdb_byte *found_ptr;
02937       unsigned nr_search_bytes = min (search_space_len, search_buf_size);
02938 
02939       found_ptr = memmem (search_buf, nr_search_bytes,
02940                           pattern, pattern_len);
02941 
02942       if (found_ptr != NULL)
02943         {
02944           CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
02945 
02946           *found_addrp = found_addr;
02947           do_cleanups (old_cleanups);
02948           return 1;
02949         }
02950 
02951       /* Not found in this chunk, skip to next chunk.  */
02952 
02953       /* Don't let search_space_len wrap here, it's unsigned.  */
02954       if (search_space_len >= chunk_size)
02955         search_space_len -= chunk_size;
02956       else
02957         search_space_len = 0;
02958 
02959       if (search_space_len >= pattern_len)
02960         {
02961           unsigned keep_len = search_buf_size - chunk_size;
02962           CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
02963           int nr_to_read;
02964 
02965           /* Copy the trailing part of the previous iteration to the front
02966              of the buffer for the next iteration.  */
02967           gdb_assert (keep_len == pattern_len - 1);
02968           memcpy (search_buf, search_buf + chunk_size, keep_len);
02969 
02970           nr_to_read = min (search_space_len - keep_len, chunk_size);
02971 
02972           if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
02973                            search_buf + keep_len, read_addr,
02974                            nr_to_read) != nr_to_read)
02975             {
02976               warning (_("Unable to access %s bytes of target "
02977                          "memory at %s, halting search."),
02978                        plongest (nr_to_read),
02979                        hex_string (read_addr));
02980               do_cleanups (old_cleanups);
02981               return -1;
02982             }
02983 
02984           start_addr += chunk_size;
02985         }
02986     }
02987 
02988   /* Not found.  */
02989 
02990   do_cleanups (old_cleanups);
02991   return 0;
02992 }
02993 
02994 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
02995    sequence of bytes in PATTERN with length PATTERN_LEN.
02996 
02997    The result is 1 if found, 0 if not found, and -1 if there was an error
02998    requiring halting of the search (e.g. memory read error).
02999    If the pattern is found the address is recorded in FOUND_ADDRP.  */
03000 
03001 int
03002 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
03003                       const gdb_byte *pattern, ULONGEST pattern_len,
03004                       CORE_ADDR *found_addrp)
03005 {
03006   struct target_ops *t;
03007   int found;
03008 
03009   /* We don't use INHERIT to set current_target.to_search_memory,
03010      so we have to scan the target stack and handle targetdebug
03011      ourselves.  */
03012 
03013   if (targetdebug)
03014     fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
03015                         hex_string (start_addr));
03016 
03017   for (t = current_target.beneath; t != NULL; t = t->beneath)
03018     if (t->to_search_memory != NULL)
03019       break;
03020 
03021   if (t != NULL)
03022     {
03023       found = t->to_search_memory (t, start_addr, search_space_len,
03024                                    pattern, pattern_len, found_addrp);
03025     }
03026   else
03027     {
03028       /* If a special version of to_search_memory isn't available, use the
03029          simple version.  */
03030       found = simple_search_memory (current_target.beneath,
03031                                     start_addr, search_space_len,
03032                                     pattern, pattern_len, found_addrp);
03033     }
03034 
03035   if (targetdebug)
03036     fprintf_unfiltered (gdb_stdlog, "  = %d\n", found);
03037 
03038   return found;
03039 }
03040 
03041 /* Look through the currently pushed targets.  If none of them will
03042    be able to restart the currently running process, issue an error
03043    message.  */
03044 
03045 void
03046 target_require_runnable (void)
03047 {
03048   struct target_ops *t;
03049 
03050   for (t = target_stack; t != NULL; t = t->beneath)
03051     {
03052       /* If this target knows how to create a new program, then
03053          assume we will still be able to after killing the current
03054          one.  Either killing and mourning will not pop T, or else
03055          find_default_run_target will find it again.  */
03056       if (t->to_create_inferior != NULL)
03057         return;
03058 
03059       /* Do not worry about thread_stratum targets that can not
03060          create inferiors.  Assume they will be pushed again if
03061          necessary, and continue to the process_stratum.  */
03062       if (t->to_stratum == thread_stratum
03063           || t->to_stratum == arch_stratum)
03064         continue;
03065 
03066       error (_("The \"%s\" target does not support \"run\".  "
03067                "Try \"help target\" or \"continue\"."),
03068              t->to_shortname);
03069     }
03070 
03071   /* This function is only called if the target is running.  In that
03072      case there should have been a process_stratum target and it
03073      should either know how to create inferiors, or not...  */
03074   internal_error (__FILE__, __LINE__, _("No targets found"));
03075 }
03076 
03077 /* Look through the list of possible targets for a target that can
03078    execute a run or attach command without any other data.  This is
03079    used to locate the default process stratum.
03080 
03081    If DO_MESG is not NULL, the result is always valid (error() is
03082    called for errors); else, return NULL on error.  */
03083 
03084 static struct target_ops *
03085 find_default_run_target (char *do_mesg)
03086 {
03087   struct target_ops **t;
03088   struct target_ops *runable = NULL;
03089   int count;
03090 
03091   count = 0;
03092 
03093   for (t = target_structs; t < target_structs + target_struct_size;
03094        ++t)
03095     {
03096       if ((*t)->to_can_run && target_can_run (*t))
03097         {
03098           runable = *t;
03099           ++count;
03100         }
03101     }
03102 
03103   if (count != 1)
03104     {
03105       if (do_mesg)
03106         error (_("Don't know how to %s.  Try \"help target\"."), do_mesg);
03107       else
03108         return NULL;
03109     }
03110 
03111   return runable;
03112 }
03113 
03114 void
03115 find_default_attach (struct target_ops *ops, char *args, int from_tty)
03116 {
03117   struct target_ops *t;
03118 
03119   t = find_default_run_target ("attach");
03120   (t->to_attach) (t, args, from_tty);
03121   return;
03122 }
03123 
03124 void
03125 find_default_create_inferior (struct target_ops *ops,
03126                               char *exec_file, char *allargs, char **env,
03127                               int from_tty)
03128 {
03129   struct target_ops *t;
03130 
03131   t = find_default_run_target ("run");
03132   (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
03133   return;
03134 }
03135 
03136 static int
03137 find_default_can_async_p (void)
03138 {
03139   struct target_ops *t;
03140 
03141   /* This may be called before the target is pushed on the stack;
03142      look for the default process stratum.  If there's none, gdb isn't
03143      configured with a native debugger, and target remote isn't
03144      connected yet.  */
03145   t = find_default_run_target (NULL);
03146   if (t && t->to_can_async_p)
03147     return (t->to_can_async_p) ();
03148   return 0;
03149 }
03150 
03151 static int
03152 find_default_is_async_p (void)
03153 {
03154   struct target_ops *t;
03155 
03156   /* This may be called before the target is pushed on the stack;
03157      look for the default process stratum.  If there's none, gdb isn't
03158      configured with a native debugger, and target remote isn't
03159      connected yet.  */
03160   t = find_default_run_target (NULL);
03161   if (t && t->to_is_async_p)
03162     return (t->to_is_async_p) ();
03163   return 0;
03164 }
03165 
03166 static int
03167 find_default_supports_non_stop (void)
03168 {
03169   struct target_ops *t;
03170 
03171   t = find_default_run_target (NULL);
03172   if (t && t->to_supports_non_stop)
03173     return (t->to_supports_non_stop) ();
03174   return 0;
03175 }
03176 
03177 int
03178 target_supports_non_stop (void)
03179 {
03180   struct target_ops *t;
03181 
03182   for (t = &current_target; t != NULL; t = t->beneath)
03183     if (t->to_supports_non_stop)
03184       return t->to_supports_non_stop ();
03185 
03186   return 0;
03187 }
03188 
03189 /* Implement the "info proc" command.  */
03190 
03191 int
03192 target_info_proc (char *args, enum info_proc_what what)
03193 {
03194   struct target_ops *t;
03195 
03196   /* If we're already connected to something that can get us OS
03197      related data, use it.  Otherwise, try using the native
03198      target.  */
03199   if (current_target.to_stratum >= process_stratum)
03200     t = current_target.beneath;
03201   else
03202     t = find_default_run_target (NULL);
03203 
03204   for (; t != NULL; t = t->beneath)
03205     {
03206       if (t->to_info_proc != NULL)
03207         {
03208           t->to_info_proc (t, args, what);
03209 
03210           if (targetdebug)
03211             fprintf_unfiltered (gdb_stdlog,
03212                                 "target_info_proc (\"%s\", %d)\n", args, what);
03213 
03214           return 1;
03215         }
03216     }
03217 
03218   return 0;
03219 }
03220 
03221 static int
03222 find_default_supports_disable_randomization (void)
03223 {
03224   struct target_ops *t;
03225 
03226   t = find_default_run_target (NULL);
03227   if (t && t->to_supports_disable_randomization)
03228     return (t->to_supports_disable_randomization) ();
03229   return 0;
03230 }
03231 
03232 int
03233 target_supports_disable_randomization (void)
03234 {
03235   struct target_ops *t;
03236 
03237   for (t = &current_target; t != NULL; t = t->beneath)
03238     if (t->to_supports_disable_randomization)
03239       return t->to_supports_disable_randomization ();
03240 
03241   return 0;
03242 }
03243 
03244 char *
03245 target_get_osdata (const char *type)
03246 {
03247   struct target_ops *t;
03248 
03249   /* If we're already connected to something that can get us OS
03250      related data, use it.  Otherwise, try using the native
03251      target.  */
03252   if (current_target.to_stratum >= process_stratum)
03253     t = current_target.beneath;
03254   else
03255     t = find_default_run_target ("get OS data");
03256 
03257   if (!t)
03258     return NULL;
03259 
03260   return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
03261 }
03262 
03263 /* Determine the current address space of thread PTID.  */
03264 
03265 struct address_space *
03266 target_thread_address_space (ptid_t ptid)
03267 {
03268   struct address_space *aspace;
03269   struct inferior *inf;
03270   struct target_ops *t;
03271 
03272   for (t = current_target.beneath; t != NULL; t = t->beneath)
03273     {
03274       if (t->to_thread_address_space != NULL)
03275         {
03276           aspace = t->to_thread_address_space (t, ptid);
03277           gdb_assert (aspace);
03278 
03279           if (targetdebug)
03280             fprintf_unfiltered (gdb_stdlog,
03281                                 "target_thread_address_space (%s) = %d\n",
03282                                 target_pid_to_str (ptid),
03283                                 address_space_num (aspace));
03284           return aspace;
03285         }
03286     }
03287 
03288   /* Fall-back to the "main" address space of the inferior.  */
03289   inf = find_inferior_pid (ptid_get_pid (ptid));
03290 
03291   if (inf == NULL || inf->aspace == NULL)
03292     internal_error (__FILE__, __LINE__,
03293                     _("Can't determine the current "
03294                       "address space of thread %s\n"),
03295                     target_pid_to_str (ptid));
03296 
03297   return inf->aspace;
03298 }
03299 
03300 
03301 /* Target file operations.  */
03302 
03303 static struct target_ops *
03304 default_fileio_target (void)
03305 {
03306   /* If we're already connected to something that can perform
03307      file I/O, use it. Otherwise, try using the native target.  */
03308   if (current_target.to_stratum >= process_stratum)
03309     return current_target.beneath;
03310   else
03311     return find_default_run_target ("file I/O");
03312 }
03313 
03314 /* Open FILENAME on the target, using FLAGS and MODE.  Return a
03315    target file descriptor, or -1 if an error occurs (and set
03316    *TARGET_ERRNO).  */
03317 int
03318 target_fileio_open (const char *filename, int flags, int mode,
03319                     int *target_errno)
03320 {
03321   struct target_ops *t;
03322 
03323   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03324     {
03325       if (t->to_fileio_open != NULL)
03326         {
03327           int fd = t->to_fileio_open (filename, flags, mode, target_errno);
03328 
03329           if (targetdebug)
03330             fprintf_unfiltered (gdb_stdlog,
03331                                 "target_fileio_open (%s,0x%x,0%o) = %d (%d)\n",
03332                                 filename, flags, mode,
03333                                 fd, fd != -1 ? 0 : *target_errno);
03334           return fd;
03335         }
03336     }
03337 
03338   *target_errno = FILEIO_ENOSYS;
03339   return -1;
03340 }
03341 
03342 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
03343    Return the number of bytes written, or -1 if an error occurs
03344    (and set *TARGET_ERRNO).  */
03345 int
03346 target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
03347                       ULONGEST offset, int *target_errno)
03348 {
03349   struct target_ops *t;
03350 
03351   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03352     {
03353       if (t->to_fileio_pwrite != NULL)
03354         {
03355           int ret = t->to_fileio_pwrite (fd, write_buf, len, offset,
03356                                          target_errno);
03357 
03358           if (targetdebug)
03359             fprintf_unfiltered (gdb_stdlog,
03360                                 "target_fileio_pwrite (%d,...,%d,%s) "
03361                                 "= %d (%d)\n",
03362                                 fd, len, pulongest (offset),
03363                                 ret, ret != -1 ? 0 : *target_errno);
03364           return ret;
03365         }
03366     }
03367 
03368   *target_errno = FILEIO_ENOSYS;
03369   return -1;
03370 }
03371 
03372 /* Read up to LEN bytes FD on the target into READ_BUF.
03373    Return the number of bytes read, or -1 if an error occurs
03374    (and set *TARGET_ERRNO).  */
03375 int
03376 target_fileio_pread (int fd, gdb_byte *read_buf, int len,
03377                      ULONGEST offset, int *target_errno)
03378 {
03379   struct target_ops *t;
03380 
03381   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03382     {
03383       if (t->to_fileio_pread != NULL)
03384         {
03385           int ret = t->to_fileio_pread (fd, read_buf, len, offset,
03386                                         target_errno);
03387 
03388           if (targetdebug)
03389             fprintf_unfiltered (gdb_stdlog,
03390                                 "target_fileio_pread (%d,...,%d,%s) "
03391                                 "= %d (%d)\n",
03392                                 fd, len, pulongest (offset),
03393                                 ret, ret != -1 ? 0 : *target_errno);
03394           return ret;
03395         }
03396     }
03397 
03398   *target_errno = FILEIO_ENOSYS;
03399   return -1;
03400 }
03401 
03402 /* Close FD on the target.  Return 0, or -1 if an error occurs
03403    (and set *TARGET_ERRNO).  */
03404 int
03405 target_fileio_close (int fd, int *target_errno)
03406 {
03407   struct target_ops *t;
03408 
03409   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03410     {
03411       if (t->to_fileio_close != NULL)
03412         {
03413           int ret = t->to_fileio_close (fd, target_errno);
03414 
03415           if (targetdebug)
03416             fprintf_unfiltered (gdb_stdlog,
03417                                 "target_fileio_close (%d) = %d (%d)\n",
03418                                 fd, ret, ret != -1 ? 0 : *target_errno);
03419           return ret;
03420         }
03421     }
03422 
03423   *target_errno = FILEIO_ENOSYS;
03424   return -1;
03425 }
03426 
03427 /* Unlink FILENAME on the target.  Return 0, or -1 if an error
03428    occurs (and set *TARGET_ERRNO).  */
03429 int
03430 target_fileio_unlink (const char *filename, int *target_errno)
03431 {
03432   struct target_ops *t;
03433 
03434   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03435     {
03436       if (t->to_fileio_unlink != NULL)
03437         {
03438           int ret = t->to_fileio_unlink (filename, target_errno);
03439 
03440           if (targetdebug)
03441             fprintf_unfiltered (gdb_stdlog,
03442                                 "target_fileio_unlink (%s) = %d (%d)\n",
03443                                 filename, ret, ret != -1 ? 0 : *target_errno);
03444           return ret;
03445         }
03446     }
03447 
03448   *target_errno = FILEIO_ENOSYS;
03449   return -1;
03450 }
03451 
03452 /* Read value of symbolic link FILENAME on the target.  Return a
03453    null-terminated string allocated via xmalloc, or NULL if an error
03454    occurs (and set *TARGET_ERRNO).  */
03455 char *
03456 target_fileio_readlink (const char *filename, int *target_errno)
03457 {
03458   struct target_ops *t;
03459 
03460   for (t = default_fileio_target (); t != NULL; t = t->beneath)
03461     {
03462       if (t->to_fileio_readlink != NULL)
03463         {
03464           char *ret = t->to_fileio_readlink (filename, target_errno);
03465 
03466           if (targetdebug)
03467             fprintf_unfiltered (gdb_stdlog,
03468                                 "target_fileio_readlink (%s) = %s (%d)\n",
03469                                 filename, ret? ret : "(nil)",
03470                                 ret? 0 : *target_errno);
03471           return ret;
03472         }
03473     }
03474 
03475   *target_errno = FILEIO_ENOSYS;
03476   return NULL;
03477 }
03478 
03479 static void
03480 target_fileio_close_cleanup (void *opaque)
03481 {
03482   int fd = *(int *) opaque;
03483   int target_errno;
03484 
03485   target_fileio_close (fd, &target_errno);
03486 }
03487 
03488 /* Read target file FILENAME.  Store the result in *BUF_P and
03489    return the size of the transferred data.  PADDING additional bytes are
03490    available in *BUF_P.  This is a helper function for
03491    target_fileio_read_alloc; see the declaration of that function for more
03492    information.  */
03493 
03494 static LONGEST
03495 target_fileio_read_alloc_1 (const char *filename,
03496                             gdb_byte **buf_p, int padding)
03497 {
03498   struct cleanup *close_cleanup;
03499   size_t buf_alloc, buf_pos;
03500   gdb_byte *buf;
03501   LONGEST n;
03502   int fd;
03503   int target_errno;
03504 
03505   fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
03506   if (fd == -1)
03507     return -1;
03508 
03509   close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
03510 
03511   /* Start by reading up to 4K at a time.  The target will throttle
03512      this number down if necessary.  */
03513   buf_alloc = 4096;
03514   buf = xmalloc (buf_alloc);
03515   buf_pos = 0;
03516   while (1)
03517     {
03518       n = target_fileio_pread (fd, &buf[buf_pos],
03519                                buf_alloc - buf_pos - padding, buf_pos,
03520                                &target_errno);
03521       if (n < 0)
03522         {
03523           /* An error occurred.  */
03524           do_cleanups (close_cleanup);
03525           xfree (buf);
03526           return -1;
03527         }
03528       else if (n == 0)
03529         {
03530           /* Read all there was.  */
03531           do_cleanups (close_cleanup);
03532           if (buf_pos == 0)
03533             xfree (buf);
03534           else
03535             *buf_p = buf;
03536           return buf_pos;
03537         }
03538 
03539       buf_pos += n;
03540 
03541       /* If the buffer is filling up, expand it.  */
03542       if (buf_alloc < buf_pos * 2)
03543         {
03544           buf_alloc *= 2;
03545           buf = xrealloc (buf, buf_alloc);
03546         }
03547 
03548       QUIT;
03549     }
03550 }
03551 
03552 /* Read target file FILENAME.  Store the result in *BUF_P and return
03553    the size of the transferred data.  See the declaration in "target.h"
03554    function for more information about the return value.  */
03555 
03556 LONGEST
03557 target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
03558 {
03559   return target_fileio_read_alloc_1 (filename, buf_p, 0);
03560 }
03561 
03562 /* Read target file FILENAME.  The result is NUL-terminated and
03563    returned as a string, allocated using xmalloc.  If an error occurs
03564    or the transfer is unsupported, NULL is returned.  Empty objects
03565    are returned as allocated but empty strings.  A warning is issued
03566    if the result contains any embedded NUL bytes.  */
03567 
03568 char *
03569 target_fileio_read_stralloc (const char *filename)
03570 {
03571   gdb_byte *buffer;
03572   char *bufstr;
03573   LONGEST i, transferred;
03574 
03575   transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
03576   bufstr = (char *) buffer;
03577 
03578   if (transferred < 0)
03579     return NULL;
03580 
03581   if (transferred == 0)
03582     return xstrdup ("");
03583 
03584   bufstr[transferred] = 0;
03585 
03586   /* Check for embedded NUL bytes; but allow trailing NULs.  */
03587   for (i = strlen (bufstr); i < transferred; i++)
03588     if (bufstr[i] != 0)
03589       {
03590         warning (_("target file %s "
03591                    "contained unexpected null characters"),
03592                  filename);
03593         break;
03594       }
03595 
03596   return bufstr;
03597 }
03598 
03599 
03600 static int
03601 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
03602 {
03603   return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT);
03604 }
03605 
03606 static int
03607 default_watchpoint_addr_within_range (struct target_ops *target,
03608                                       CORE_ADDR addr,
03609                                       CORE_ADDR start, int length)
03610 {
03611   return addr >= start && addr < start + length;
03612 }
03613 
03614 static struct gdbarch *
03615 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
03616 {
03617   return target_gdbarch ();
03618 }
03619 
03620 static int
03621 return_zero (void)
03622 {
03623   return 0;
03624 }
03625 
03626 static int
03627 return_one (void)
03628 {
03629   return 1;
03630 }
03631 
03632 static int
03633 return_minus_one (void)
03634 {
03635   return -1;
03636 }
03637 
03638 /*
03639  * Find the next target down the stack from the specified target.
03640  */
03641 
03642 struct target_ops *
03643 find_target_beneath (struct target_ops *t)
03644 {
03645   return t->beneath;
03646 }
03647 
03648 
03649 /* The inferior process has died.  Long live the inferior!  */
03650 
03651 void
03652 generic_mourn_inferior (void)
03653 {
03654   ptid_t ptid;
03655 
03656   ptid = inferior_ptid;
03657   inferior_ptid = null_ptid;
03658 
03659   /* Mark breakpoints uninserted in case something tries to delete a
03660      breakpoint while we delete the inferior's threads (which would
03661      fail, since the inferior is long gone).  */
03662   mark_breakpoints_out ();
03663 
03664   if (!ptid_equal (ptid, null_ptid))
03665     {
03666       int pid = ptid_get_pid (ptid);
03667       exit_inferior (pid);
03668     }
03669 
03670   /* Note this wipes step-resume breakpoints, so needs to be done
03671      after exit_inferior, which ends up referencing the step-resume
03672      breakpoints through clear_thread_inferior_resources.  */
03673   breakpoint_init_inferior (inf_exited);
03674 
03675   registers_changed ();
03676 
03677   reopen_exec_file ();
03678   reinit_frame_cache ();
03679 
03680   if (deprecated_detach_hook)
03681     deprecated_detach_hook ();
03682 }
03683 
03684 /* Convert a normal process ID to a string.  Returns the string in a
03685    static buffer.  */
03686 
03687 char *
03688 normal_pid_to_str (ptid_t ptid)
03689 {
03690   static char buf[32];
03691 
03692   xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
03693   return buf;
03694 }
03695 
03696 static char *
03697 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
03698 {
03699   return normal_pid_to_str (ptid);
03700 }
03701 
03702 /* Error-catcher for target_find_memory_regions.  */
03703 static int
03704 dummy_find_memory_regions (find_memory_region_ftype ignore1, void *ignore2)
03705 {
03706   error (_("Command not implemented for this target."));
03707   return 0;
03708 }
03709 
03710 /* Error-catcher for target_make_corefile_notes.  */
03711 static char *
03712 dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
03713 {
03714   error (_("Command not implemented for this target."));
03715   return NULL;
03716 }
03717 
03718 /* Error-catcher for target_get_bookmark.  */
03719 static gdb_byte *
03720 dummy_get_bookmark (char *ignore1, int ignore2)
03721 {
03722   tcomplain ();
03723   return NULL;
03724 }
03725 
03726 /* Error-catcher for target_goto_bookmark.  */
03727 static void
03728 dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
03729 {
03730   tcomplain ();
03731 }
03732 
03733 /* Set up the handful of non-empty slots needed by the dummy target
03734    vector.  */
03735 
03736 static void
03737 init_dummy_target (void)
03738 {
03739   dummy_target.to_shortname = "None";
03740   dummy_target.to_longname = "None";
03741   dummy_target.to_doc = "";
03742   dummy_target.to_attach = find_default_attach;
03743   dummy_target.to_detach = 
03744     (void (*)(struct target_ops *, char *, int))target_ignore;
03745   dummy_target.to_create_inferior = find_default_create_inferior;
03746   dummy_target.to_can_async_p = find_default_can_async_p;
03747   dummy_target.to_is_async_p = find_default_is_async_p;
03748   dummy_target.to_supports_non_stop = find_default_supports_non_stop;
03749   dummy_target.to_supports_disable_randomization
03750     = find_default_supports_disable_randomization;
03751   dummy_target.to_pid_to_str = dummy_pid_to_str;
03752   dummy_target.to_stratum = dummy_stratum;
03753   dummy_target.to_find_memory_regions = dummy_find_memory_regions;
03754   dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
03755   dummy_target.to_get_bookmark = dummy_get_bookmark;
03756   dummy_target.to_goto_bookmark = dummy_goto_bookmark;
03757   dummy_target.to_xfer_partial = default_xfer_partial;
03758   dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
03759   dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
03760   dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
03761   dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
03762   dummy_target.to_has_execution
03763     = (int (*) (struct target_ops *, ptid_t)) return_zero;
03764   dummy_target.to_stopped_by_watchpoint = return_zero;
03765   dummy_target.to_stopped_data_address =
03766     (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
03767   dummy_target.to_magic = OPS_MAGIC;
03768 }
03769 
03770 static void
03771 debug_to_open (char *args, int from_tty)
03772 {
03773   debug_target.to_open (args, from_tty);
03774 
03775   fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
03776 }
03777 
03778 void
03779 target_close (struct target_ops *targ)
03780 {
03781   gdb_assert (!target_is_pushed (targ));
03782 
03783   if (targ->to_xclose != NULL)
03784     targ->to_xclose (targ);
03785   else if (targ->to_close != NULL)
03786     targ->to_close ();
03787 
03788   if (targetdebug)
03789     fprintf_unfiltered (gdb_stdlog, "target_close ()\n");
03790 }
03791 
03792 void
03793 target_attach (char *args, int from_tty)
03794 {
03795   struct target_ops *t;
03796 
03797   for (t = current_target.beneath; t != NULL; t = t->beneath)
03798     {
03799       if (t->to_attach != NULL) 
03800         {
03801           t->to_attach (t, args, from_tty);
03802           if (targetdebug)
03803             fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
03804                                 args, from_tty);
03805           return;
03806         }
03807     }
03808 
03809   internal_error (__FILE__, __LINE__,
03810                   _("could not find a target to attach"));
03811 }
03812 
03813 int
03814 target_thread_alive (ptid_t ptid)
03815 {
03816   struct target_ops *t;
03817 
03818   for (t = current_target.beneath; t != NULL; t = t->beneath)
03819     {
03820       if (t->to_thread_alive != NULL)
03821         {
03822           int retval;
03823 
03824           retval = t->to_thread_alive (t, ptid);
03825           if (targetdebug)
03826             fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
03827                                 ptid_get_pid (ptid), retval);
03828 
03829           return retval;
03830         }
03831     }
03832 
03833   return 0;
03834 }
03835 
03836 void
03837 target_find_new_threads (void)
03838 {
03839   struct target_ops *t;
03840 
03841   for (t = current_target.beneath; t != NULL; t = t->beneath)
03842     {
03843       if (t->to_find_new_threads != NULL)
03844         {
03845           t->to_find_new_threads (t);
03846           if (targetdebug)
03847             fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
03848 
03849           return;
03850         }
03851     }
03852 }
03853 
03854 void
03855 target_stop (ptid_t ptid)
03856 {
03857   if (!may_stop)
03858     {
03859       warning (_("May not interrupt or stop the target, ignoring attempt"));
03860       return;
03861     }
03862 
03863   (*current_target.to_stop) (ptid);
03864 }
03865 
03866 static void
03867 debug_to_post_attach (int pid)
03868 {
03869   debug_target.to_post_attach (pid);
03870 
03871   fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
03872 }
03873 
03874 /* Concatenate ELEM to LIST, a comma separate list, and return the
03875    result.  The LIST incoming argument is released.  */
03876 
03877 static char *
03878 str_comma_list_concat_elem (char *list, const char *elem)
03879 {
03880   if (list == NULL)
03881     return xstrdup (elem);
03882   else
03883     return reconcat (list, list, ", ", elem, (char *) NULL);
03884 }
03885 
03886 /* Helper for target_options_to_string.  If OPT is present in
03887    TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
03888    Returns the new resulting string.  OPT is removed from
03889    TARGET_OPTIONS.  */
03890 
03891 static char *
03892 do_option (int *target_options, char *ret,
03893            int opt, char *opt_str)
03894 {
03895   if ((*target_options & opt) != 0)
03896     {
03897       ret = str_comma_list_concat_elem (ret, opt_str);
03898       *target_options &= ~opt;
03899     }
03900 
03901   return ret;
03902 }
03903 
03904 char *
03905 target_options_to_string (int target_options)
03906 {
03907   char *ret = NULL;
03908 
03909 #define DO_TARG_OPTION(OPT) \
03910   ret = do_option (&target_options, ret, OPT, #OPT)
03911 
03912   DO_TARG_OPTION (TARGET_WNOHANG);
03913 
03914   if (target_options != 0)
03915     ret = str_comma_list_concat_elem (ret, "unknown???");
03916 
03917   if (ret == NULL)
03918     ret = xstrdup ("");
03919   return ret;
03920 }
03921 
03922 static void
03923 debug_print_register (const char * func,
03924                       struct regcache *regcache, int regno)
03925 {
03926   struct gdbarch *gdbarch = get_regcache_arch (regcache);
03927 
03928   fprintf_unfiltered (gdb_stdlog, "%s ", func);
03929   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
03930       && gdbarch_register_name (gdbarch, regno) != NULL
03931       && gdbarch_register_name (gdbarch, regno)[0] != '\0')
03932     fprintf_unfiltered (gdb_stdlog, "(%s)",
03933                         gdbarch_register_name (gdbarch, regno));
03934   else
03935     fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
03936   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
03937     {
03938       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03939       int i, size = register_size (gdbarch, regno);
03940       gdb_byte buf[MAX_REGISTER_SIZE];
03941 
03942       regcache_raw_collect (regcache, regno, buf);
03943       fprintf_unfiltered (gdb_stdlog, " = ");
03944       for (i = 0; i < size; i++)
03945         {
03946           fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
03947         }
03948       if (size <= sizeof (LONGEST))
03949         {
03950           ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
03951 
03952           fprintf_unfiltered (gdb_stdlog, " %s %s",
03953                               core_addr_to_string_nz (val), plongest (val));
03954         }
03955     }
03956   fprintf_unfiltered (gdb_stdlog, "\n");
03957 }
03958 
03959 void
03960 target_fetch_registers (struct regcache *regcache, int regno)
03961 {
03962   struct target_ops *t;
03963 
03964   for (t = current_target.beneath; t != NULL; t = t->beneath)
03965     {
03966       if (t->to_fetch_registers != NULL)
03967         {
03968           t->to_fetch_registers (t, regcache, regno);
03969           if (targetdebug)
03970             debug_print_register ("target_fetch_registers", regcache, regno);
03971           return;
03972         }
03973     }
03974 }
03975 
03976 void
03977 target_store_registers (struct regcache *regcache, int regno)
03978 {
03979   struct target_ops *t;
03980 
03981   if (!may_write_registers)
03982     error (_("Writing to registers is not allowed (regno %d)"), regno);
03983 
03984   for (t = current_target.beneath; t != NULL; t = t->beneath)
03985     {
03986       if (t->to_store_registers != NULL)
03987         {
03988           t->to_store_registers (t, regcache, regno);
03989           if (targetdebug)
03990             {
03991               debug_print_register ("target_store_registers", regcache, regno);
03992             }
03993           return;
03994         }
03995     }
03996 
03997   noprocess ();
03998 }
03999 
04000 int
04001 target_core_of_thread (ptid_t ptid)
04002 {
04003   struct target_ops *t;
04004 
04005   for (t = current_target.beneath; t != NULL; t = t->beneath)
04006     {
04007       if (t->to_core_of_thread != NULL)
04008         {
04009           int retval = t->to_core_of_thread (t, ptid);
04010 
04011           if (targetdebug)
04012             fprintf_unfiltered (gdb_stdlog,
04013                                 "target_core_of_thread (%d) = %d\n",
04014                                 ptid_get_pid (ptid), retval);
04015           return retval;
04016         }
04017     }
04018 
04019   return -1;
04020 }
04021 
04022 int
04023 target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
04024 {
04025   struct target_ops *t;
04026 
04027   for (t = current_target.beneath; t != NULL; t = t->beneath)
04028     {
04029       if (t->to_verify_memory != NULL)
04030         {
04031           int retval = t->to_verify_memory (t, data, memaddr, size);
04032 
04033           if (targetdebug)
04034             fprintf_unfiltered (gdb_stdlog,
04035                                 "target_verify_memory (%s, %s) = %d\n",
04036                                 paddress (target_gdbarch (), memaddr),
04037                                 pulongest (size),
04038                                 retval);
04039           return retval;
04040         }
04041     }
04042 
04043   tcomplain ();
04044 }
04045 
04046 /* The documentation for this function is in its prototype declaration in
04047    target.h.  */
04048 
04049 int
04050 target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
04051 {
04052   struct target_ops *t;
04053 
04054   for (t = current_target.beneath; t != NULL; t = t->beneath)
04055     if (t->to_insert_mask_watchpoint != NULL)
04056       {
04057         int ret;
04058 
04059         ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
04060 
04061         if (targetdebug)
04062           fprintf_unfiltered (gdb_stdlog, "\
04063 target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
04064                               core_addr_to_string (addr),
04065                               core_addr_to_string (mask), rw, ret);
04066 
04067         return ret;
04068       }
04069 
04070   return 1;
04071 }
04072 
04073 /* The documentation for this function is in its prototype declaration in
04074    target.h.  */
04075 
04076 int
04077 target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
04078 {
04079   struct target_ops *t;
04080 
04081   for (t = current_target.beneath; t != NULL; t = t->beneath)
04082     if (t->to_remove_mask_watchpoint != NULL)
04083       {
04084         int ret;
04085 
04086         ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
04087 
04088         if (targetdebug)
04089           fprintf_unfiltered (gdb_stdlog, "\
04090 target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
04091                               core_addr_to_string (addr),
04092                               core_addr_to_string (mask), rw, ret);
04093 
04094         return ret;
04095       }
04096 
04097   return 1;
04098 }
04099 
04100 /* The documentation for this function is in its prototype declaration
04101    in target.h.  */
04102 
04103 int
04104 target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
04105 {
04106   struct target_ops *t;
04107 
04108   for (t = current_target.beneath; t != NULL; t = t->beneath)
04109     if (t->to_masked_watch_num_registers != NULL)
04110       return t->to_masked_watch_num_registers (t, addr, mask);
04111 
04112   return -1;
04113 }
04114 
04115 /* The documentation for this function is in its prototype declaration
04116    in target.h.  */
04117 
04118 int
04119 target_ranged_break_num_registers (void)
04120 {
04121   struct target_ops *t;
04122 
04123   for (t = current_target.beneath; t != NULL; t = t->beneath)
04124     if (t->to_ranged_break_num_registers != NULL)
04125       return t->to_ranged_break_num_registers (t);
04126 
04127   return -1;
04128 }
04129 
04130 /* See target.h.  */
04131 
04132 int
04133 target_supports_btrace (void)
04134 {
04135   struct target_ops *t;
04136 
04137   for (t = current_target.beneath; t != NULL; t = t->beneath)
04138     if (t->to_supports_btrace != NULL)
04139       return t->to_supports_btrace ();
04140 
04141   return 0;
04142 }
04143 
04144 /* See target.h.  */
04145 
04146 struct btrace_target_info *
04147 target_enable_btrace (ptid_t ptid)
04148 {
04149   struct target_ops *t;
04150 
04151   for (t = current_target.beneath; t != NULL; t = t->beneath)
04152     if (t->to_enable_btrace != NULL)
04153       return t->to_enable_btrace (ptid);
04154 
04155   tcomplain ();
04156   return NULL;
04157 }
04158 
04159 /* See target.h.  */
04160 
04161 void
04162 target_disable_btrace (struct btrace_target_info *btinfo)
04163 {
04164   struct target_ops *t;
04165 
04166   for (t = current_target.beneath; t != NULL; t = t->beneath)
04167     if (t->to_disable_btrace != NULL)
04168       return t->to_disable_btrace (btinfo);
04169 
04170   tcomplain ();
04171 }
04172 
04173 /* See target.h.  */
04174 
04175 void
04176 target_teardown_btrace (struct btrace_target_info *btinfo)
04177 {
04178   struct target_ops *t;
04179 
04180   for (t = current_target.beneath; t != NULL; t = t->beneath)
04181     if (t->to_teardown_btrace != NULL)
04182       return t->to_teardown_btrace (btinfo);
04183 
04184   tcomplain ();
04185 }
04186 
04187 /* See target.h.  */
04188 
04189 VEC (btrace_block_s) *
04190 target_read_btrace (struct btrace_target_info *btinfo,
04191                     enum btrace_read_type type)
04192 {
04193   struct target_ops *t;
04194 
04195   for (t = current_target.beneath; t != NULL; t = t->beneath)
04196     if (t->to_read_btrace != NULL)
04197       return t->to_read_btrace (btinfo, type);
04198 
04199   tcomplain ();
04200   return NULL;
04201 }
04202 
04203 /* See target.h.  */
04204 
04205 void
04206 target_stop_recording (void)
04207 {
04208   struct target_ops *t;
04209 
04210   for (t = current_target.beneath; t != NULL; t = t->beneath)
04211     if (t->to_stop_recording != NULL)
04212       {
04213         t->to_stop_recording ();
04214         return;
04215       }
04216 
04217   /* This is optional.  */
04218 }
04219 
04220 /* See target.h.  */
04221 
04222 void
04223 target_info_record (void)
04224 {
04225   struct target_ops *t;
04226 
04227   for (t = current_target.beneath; t != NULL; t = t->beneath)
04228     if (t->to_info_record != NULL)
04229       {
04230         t->to_info_record ();
04231         return;
04232       }
04233 
04234   tcomplain ();
04235 }
04236 
04237 /* See target.h.  */
04238 
04239 void
04240 target_save_record (const char *filename)
04241 {
04242   struct target_ops *t;
04243 
04244   for (t = current_target.beneath; t != NULL; t = t->beneath)
04245     if (t->to_save_record != NULL)
04246       {
04247         t->to_save_record (filename);
04248         return;
04249       }
04250 
04251   tcomplain ();
04252 }
04253 
04254 /* See target.h.  */
04255 
04256 int
04257 target_supports_delete_record (void)
04258 {
04259   struct target_ops *t;
04260 
04261   for (t = current_target.beneath; t != NULL; t = t->beneath)
04262     if (t->to_delete_record != NULL)
04263       return 1;
04264 
04265   return 0;
04266 }
04267 
04268 /* See target.h.  */
04269 
04270 void
04271 target_delete_record (void)
04272 {
04273   struct target_ops *t;
04274 
04275   for (t = current_target.beneath; t != NULL; t = t->beneath)
04276     if (t->to_delete_record != NULL)
04277       {
04278         t->to_delete_record ();
04279         return;
04280       }
04281 
04282   tcomplain ();
04283 }
04284 
04285 /* See target.h.  */
04286 
04287 int
04288 target_record_is_replaying (void)
04289 {
04290   struct target_ops *t;
04291 
04292   for (t = current_target.beneath; t != NULL; t = t->beneath)
04293     if (t->to_record_is_replaying != NULL)
04294         return t->to_record_is_replaying ();
04295 
04296   return 0;
04297 }
04298 
04299 /* See target.h.  */
04300 
04301 void
04302 target_goto_record_begin (void)
04303 {
04304   struct target_ops *t;
04305 
04306   for (t = current_target.beneath; t != NULL; t = t->beneath)
04307     if (t->to_goto_record_begin != NULL)
04308       {
04309         t->to_goto_record_begin ();
04310         return;
04311       }
04312 
04313   tcomplain ();
04314 }
04315 
04316 /* See target.h.  */
04317 
04318 void
04319 target_goto_record_end (void)
04320 {
04321   struct target_ops *t;
04322 
04323   for (t = current_target.beneath; t != NULL; t = t->beneath)
04324     if (t->to_goto_record_end != NULL)
04325       {
04326         t->to_goto_record_end ();
04327         return;
04328       }
04329 
04330   tcomplain ();
04331 }
04332 
04333 /* See target.h.  */
04334 
04335 void
04336 target_goto_record (ULONGEST insn)
04337 {
04338   struct target_ops *t;
04339 
04340   for (t = current_target.beneath; t != NULL; t = t->beneath)
04341     if (t->to_goto_record != NULL)
04342       {
04343         t->to_goto_record (insn);
04344         return;
04345       }
04346 
04347   tcomplain ();
04348 }
04349 
04350 /* See target.h.  */
04351 
04352 void
04353 target_insn_history (int size, int flags)
04354 {
04355   struct target_ops *t;
04356 
04357   for (t = current_target.beneath; t != NULL; t = t->beneath)
04358     if (t->to_insn_history != NULL)
04359       {
04360         t->to_insn_history (size, flags);
04361         return;
04362       }
04363 
04364   tcomplain ();
04365 }
04366 
04367 /* See target.h.  */
04368 
04369 void
04370 target_insn_history_from (ULONGEST from, int size, int flags)
04371 {
04372   struct target_ops *t;
04373 
04374   for (t = current_target.beneath; t != NULL; t = t->beneath)
04375     if (t->to_insn_history_from != NULL)
04376       {
04377         t->to_insn_history_from (from, size, flags);
04378         return;
04379       }
04380 
04381   tcomplain ();
04382 }
04383 
04384 /* See target.h.  */
04385 
04386 void
04387 target_insn_history_range (ULONGEST begin, ULONGEST end, int flags)
04388 {
04389   struct target_ops *t;
04390 
04391   for (t = current_target.beneath; t != NULL; t = t->beneath)
04392     if (t->to_insn_history_range != NULL)
04393       {
04394         t->to_insn_history_range (begin, end, flags);
04395         return;
04396       }
04397 
04398   tcomplain ();
04399 }
04400 
04401 /* See target.h.  */
04402 
04403 void
04404 target_call_history (int size, int flags)
04405 {
04406   struct target_ops *t;
04407 
04408   for (t = current_target.beneath; t != NULL; t = t->beneath)
04409     if (t->to_call_history != NULL)
04410       {
04411         t->to_call_history (size, flags);
04412         return;
04413       }
04414 
04415   tcomplain ();
04416 }
04417 
04418 /* See target.h.  */
04419 
04420 void
04421 target_call_history_from (ULONGEST begin, int size, int flags)
04422 {
04423   struct target_ops *t;
04424 
04425   for (t = current_target.beneath; t != NULL; t = t->beneath)
04426     if (t->to_call_history_from != NULL)
04427       {
04428         t->to_call_history_from (begin, size, flags);
04429         return;
04430       }
04431 
04432   tcomplain ();
04433 }
04434 
04435 /* See target.h.  */
04436 
04437 void
04438 target_call_history_range (ULONGEST begin, ULONGEST end, int flags)
04439 {
04440   struct target_ops *t;
04441 
04442   for (t = current_target.beneath; t != NULL; t = t->beneath)
04443     if (t->to_call_history_range != NULL)
04444       {
04445         t->to_call_history_range (begin, end, flags);
04446         return;
04447       }
04448 
04449   tcomplain ();
04450 }
04451 
04452 static void
04453 debug_to_prepare_to_store (struct regcache *regcache)
04454 {
04455   debug_target.to_prepare_to_store (regcache);
04456 
04457   fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
04458 }
04459 
04460 static int
04461 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
04462                               int write, struct mem_attrib *attrib,
04463                               struct target_ops *target)
04464 {
04465   int retval;
04466 
04467   retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
04468                                                 attrib, target);
04469 
04470   fprintf_unfiltered (gdb_stdlog,
04471                       "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
04472                       paddress (target_gdbarch (), memaddr), len,
04473                       write ? "write" : "read", retval);
04474 
04475   if (retval > 0)
04476     {
04477       int i;
04478 
04479       fputs_unfiltered (", bytes =", gdb_stdlog);
04480       for (i = 0; i < retval; i++)
04481         {
04482           if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
04483             {
04484               if (targetdebug < 2 && i > 0)
04485                 {
04486                   fprintf_unfiltered (gdb_stdlog, " ...");
04487                   break;
04488                 }
04489               fprintf_unfiltered (gdb_stdlog, "\n");
04490             }
04491 
04492           fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
04493         }
04494     }
04495 
04496   fputc_unfiltered ('\n', gdb_stdlog);
04497 
04498   return retval;
04499 }
04500 
04501 static void
04502 debug_to_files_info (struct target_ops *target)
04503 {
04504   debug_target.to_files_info (target);
04505 
04506   fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
04507 }
04508 
04509 static int
04510 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
04511                             struct bp_target_info *bp_tgt)
04512 {
04513   int retval;
04514 
04515   retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
04516 
04517   fprintf_unfiltered (gdb_stdlog,
04518                       "target_insert_breakpoint (%s, xxx) = %ld\n",
04519                       core_addr_to_string (bp_tgt->placed_address),
04520                       (unsigned long) retval);
04521   return retval;
04522 }
04523 
04524 static int
04525 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
04526                             struct bp_target_info *bp_tgt)
04527 {
04528   int retval;
04529 
04530   retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
04531 
04532   fprintf_unfiltered (gdb_stdlog,
04533                       "target_remove_breakpoint (%s, xxx) = %ld\n",
04534                       core_addr_to_string (bp_tgt->placed_address),
04535                       (unsigned long) retval);
04536   return retval;
04537 }
04538 
04539 static int
04540 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
04541 {
04542   int retval;
04543 
04544   retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
04545 
04546   fprintf_unfiltered (gdb_stdlog,
04547                       "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
04548                       (unsigned long) type,
04549                       (unsigned long) cnt,
04550                       (unsigned long) from_tty,
04551                       (unsigned long) retval);
04552   return retval;
04553 }
04554 
04555 static int
04556 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
04557 {
04558   CORE_ADDR retval;
04559 
04560   retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
04561 
04562   fprintf_unfiltered (gdb_stdlog,
04563                       "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
04564                       core_addr_to_string (addr), (unsigned long) len,
04565                       core_addr_to_string (retval));
04566   return retval;
04567 }
04568 
04569 static int
04570 debug_to_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
04571                                          struct expression *cond)
04572 {
04573   int retval;
04574 
04575   retval = debug_target.to_can_accel_watchpoint_condition (addr, len,
04576                                                            rw, cond);
04577 
04578   fprintf_unfiltered (gdb_stdlog,
04579                       "target_can_accel_watchpoint_condition "
04580                       "(%s, %d, %d, %s) = %ld\n",
04581                       core_addr_to_string (addr), len, rw,
04582                       host_address_to_string (cond), (unsigned long) retval);
04583   return retval;
04584 }
04585 
04586 static int
04587 debug_to_stopped_by_watchpoint (void)
04588 {
04589   int retval;
04590 
04591   retval = debug_target.to_stopped_by_watchpoint ();
04592 
04593   fprintf_unfiltered (gdb_stdlog,
04594                       "target_stopped_by_watchpoint () = %ld\n",
04595                       (unsigned long) retval);
04596   return retval;
04597 }
04598 
04599 static int
04600 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
04601 {
04602   int retval;
04603 
04604   retval = debug_target.to_stopped_data_address (target, addr);
04605 
04606   fprintf_unfiltered (gdb_stdlog,
04607                       "target_stopped_data_address ([%s]) = %ld\n",
04608                       core_addr_to_string (*addr),
04609                       (unsigned long)retval);
04610   return retval;
04611 }
04612 
04613 static int
04614 debug_to_watchpoint_addr_within_range (struct target_ops *target,
04615                                        CORE_ADDR addr,
04616                                        CORE_ADDR start, int length)
04617 {
04618   int retval;
04619 
04620   retval = debug_target.to_watchpoint_addr_within_range (target, addr,
04621                                                          start, length);
04622 
04623   fprintf_filtered (gdb_stdlog,
04624                     "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
04625                     core_addr_to_string (addr), core_addr_to_string (start),
04626                     length, retval);
04627   return retval;
04628 }
04629 
04630 static int
04631 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
04632                                struct bp_target_info *bp_tgt)
04633 {
04634   int retval;
04635 
04636   retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
04637 
04638   fprintf_unfiltered (gdb_stdlog,
04639                       "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
04640                       core_addr_to_string (bp_tgt->placed_address),
04641                       (unsigned long) retval);
04642   return retval;
04643 }
04644 
04645 static int
04646 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
04647                                struct bp_target_info *bp_tgt)
04648 {
04649   int retval;
04650 
04651   retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
04652 
04653   fprintf_unfiltered (gdb_stdlog,
04654                       "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
04655                       core_addr_to_string (bp_tgt->placed_address),
04656                       (unsigned long) retval);
04657   return retval;
04658 }
04659 
04660 static int
04661 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type,
04662                             struct expression *cond)
04663 {
04664   int retval;
04665 
04666   retval = debug_target.to_insert_watchpoint (addr, len, type, cond);
04667 
04668   fprintf_unfiltered (gdb_stdlog,
04669                       "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
04670                       core_addr_to_string (addr), len, type,
04671                       host_address_to_string (cond), (unsigned long) retval);
04672   return retval;
04673 }
04674 
04675 static int
04676 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type,
04677                             struct expression *cond)
04678 {
04679   int retval;
04680 
04681   retval = debug_target.to_remove_watchpoint (addr, len, type, cond);
04682 
04683   fprintf_unfiltered (gdb_stdlog,
04684                       "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
04685                       core_addr_to_string (addr), len, type,
04686                       host_address_to_string (cond), (unsigned long) retval);
04687   return retval;
04688 }
04689 
04690 static void
04691 debug_to_terminal_init (void)
04692 {
04693   debug_target.to_terminal_init ();
04694 
04695   fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
04696 }
04697 
04698 static void
04699 debug_to_terminal_inferior (void)
04700 {
04701   debug_target.to_terminal_inferior ();
04702 
04703   fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
04704 }
04705 
04706 static void
04707 debug_to_terminal_ours_for_output (void)
04708 {
04709   debug_target.to_terminal_ours_for_output ();
04710 
04711   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
04712 }
04713 
04714 static void
04715 debug_to_terminal_ours (void)
04716 {
04717   debug_target.to_terminal_ours ();
04718 
04719   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
04720 }
04721 
04722 static void
04723 debug_to_terminal_save_ours (void)
04724 {
04725   debug_target.to_terminal_save_ours ();
04726 
04727   fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
04728 }
04729 
04730 static void
04731 debug_to_terminal_info (const char *arg, int from_tty)
04732 {
04733   debug_target.to_terminal_info (arg, from_tty);
04734 
04735   fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
04736                       from_tty);
04737 }
04738 
04739 static void
04740 debug_to_load (char *args, int from_tty)
04741 {
04742   debug_target.to_load (args, from_tty);
04743 
04744   fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
04745 }
04746 
04747 static void
04748 debug_to_post_startup_inferior (ptid_t ptid)
04749 {
04750   debug_target.to_post_startup_inferior (ptid);
04751 
04752   fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
04753                       ptid_get_pid (ptid));
04754 }
04755 
04756 static int
04757 debug_to_insert_fork_catchpoint (int pid)
04758 {
04759   int retval;
04760 
04761   retval = debug_target.to_insert_fork_catchpoint (pid);
04762 
04763   fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
04764                       pid, retval);
04765 
04766   return retval;
04767 }
04768 
04769 static int
04770 debug_to_remove_fork_catchpoint (int pid)
04771 {
04772   int retval;
04773 
04774   retval = debug_target.to_remove_fork_catchpoint (pid);
04775 
04776   fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
04777                       pid, retval);
04778 
04779   return retval;
04780 }
04781 
04782 static int
04783 debug_to_insert_vfork_catchpoint (int pid)
04784 {
04785   int retval;
04786 
04787   retval = debug_target.to_insert_vfork_catchpoint (pid);
04788 
04789   fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
04790                       pid, retval);
04791 
04792   return retval;
04793 }
04794 
04795 static int
04796 debug_to_remove_vfork_catchpoint (int pid)
04797 {
04798   int retval;
04799 
04800   retval = debug_target.to_remove_vfork_catchpoint (pid);
04801 
04802   fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
04803                       pid, retval);
04804 
04805   return retval;
04806 }
04807 
04808 static int
04809 debug_to_insert_exec_catchpoint (int pid)
04810 {
04811   int retval;
04812 
04813   retval = debug_target.to_insert_exec_catchpoint (pid);
04814 
04815   fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
04816                       pid, retval);
04817 
04818   return retval;
04819 }
04820 
04821 static int
04822 debug_to_remove_exec_catchpoint (int pid)
04823 {
04824   int retval;
04825 
04826   retval = debug_target.to_remove_exec_catchpoint (pid);
04827 
04828   fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
04829                       pid, retval);
04830 
04831   return retval;
04832 }
04833 
04834 static int
04835 debug_to_has_exited (int pid, int wait_status, int *exit_status)
04836 {
04837   int has_exited;
04838 
04839   has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
04840 
04841   fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
04842                       pid, wait_status, *exit_status, has_exited);
04843 
04844   return has_exited;
04845 }
04846 
04847 static int
04848 debug_to_can_run (void)
04849 {
04850   int retval;
04851 
04852   retval = debug_target.to_can_run ();
04853 
04854   fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
04855 
04856   return retval;
04857 }
04858 
04859 static struct gdbarch *
04860 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
04861 {
04862   struct gdbarch *retval;
04863 
04864   retval = debug_target.to_thread_architecture (ops, ptid);
04865 
04866   fprintf_unfiltered (gdb_stdlog, 
04867                       "target_thread_architecture (%s) = %s [%s]\n",
04868                       target_pid_to_str (ptid),
04869                       host_address_to_string (retval),
04870                       gdbarch_bfd_arch_info (retval)->printable_name);
04871   return retval;
04872 }
04873 
04874 static void
04875 debug_to_stop (ptid_t ptid)
04876 {
04877   debug_target.to_stop (ptid);
04878 
04879   fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
04880                       target_pid_to_str (ptid));
04881 }
04882 
04883 static void
04884 debug_to_rcmd (char *command,
04885                struct ui_file *outbuf)
04886 {
04887   debug_target.to_rcmd (command, outbuf);
04888   fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
04889 }
04890 
04891 static char *
04892 debug_to_pid_to_exec_file (int pid)
04893 {
04894   char *exec_file;
04895 
04896   exec_file = debug_target.to_pid_to_exec_file (pid);
04897 
04898   fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
04899                       pid, exec_file);
04900 
04901   return exec_file;
04902 }
04903 
04904 static void
04905 setup_target_debug (void)
04906 {
04907   memcpy (&debug_target, &current_target, sizeof debug_target);
04908 
04909   current_target.to_open = debug_to_open;
04910   current_target.to_post_attach = debug_to_post_attach;
04911   current_target.to_prepare_to_store = debug_to_prepare_to_store;
04912   current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
04913   current_target.to_files_info = debug_to_files_info;
04914   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
04915   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
04916   current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
04917   current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
04918   current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
04919   current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
04920   current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
04921   current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
04922   current_target.to_stopped_data_address = debug_to_stopped_data_address;
04923   current_target.to_watchpoint_addr_within_range
04924     = debug_to_watchpoint_addr_within_range;
04925   current_target.to_region_ok_for_hw_watchpoint
04926     = debug_to_region_ok_for_hw_watchpoint;
04927   current_target.to_can_accel_watchpoint_condition
04928     = debug_to_can_accel_watchpoint_condition;
04929   current_target.to_terminal_init = debug_to_terminal_init;
04930   current_target.to_terminal_inferior = debug_to_terminal_inferior;
04931   current_target.to_terminal_ours_for_output
04932     = debug_to_terminal_ours_for_output;
04933   current_target.to_terminal_ours = debug_to_terminal_ours;
04934   current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
04935   current_target.to_terminal_info = debug_to_terminal_info;
04936   current_target.to_load = debug_to_load;
04937   current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
04938   current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
04939   current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
04940   current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
04941   current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
04942   current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
04943   current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
04944   current_target.to_has_exited = debug_to_has_exited;
04945   current_target.to_can_run = debug_to_can_run;
04946   current_target.to_stop = debug_to_stop;
04947   current_target.to_rcmd = debug_to_rcmd;
04948   current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
04949   current_target.to_thread_architecture = debug_to_thread_architecture;
04950 }
04951 
04952 
04953 static char targ_desc[] =
04954 "Names of targets and files being debugged.\nShows the entire \
04955 stack of targets currently in use (including the exec-file,\n\
04956 core-file, and process, if any), as well as the symbol file name.";
04957 
04958 static void
04959 do_monitor_command (char *cmd,
04960                  int from_tty)
04961 {
04962   if ((current_target.to_rcmd
04963        == (void (*) (char *, struct ui_file *)) tcomplain)
04964       || (current_target.to_rcmd == debug_to_rcmd
04965           && (debug_target.to_rcmd
04966               == (void (*) (char *, struct ui_file *)) tcomplain)))
04967     error (_("\"monitor\" command not supported by this target."));
04968   target_rcmd (cmd, gdb_stdtarg);
04969 }
04970 
04971 /* Print the name of each layers of our target stack.  */
04972 
04973 static void
04974 maintenance_print_target_stack (char *cmd, int from_tty)
04975 {
04976   struct target_ops *t;
04977 
04978   printf_filtered (_("The current target stack is:\n"));
04979 
04980   for (t = target_stack; t != NULL; t = t->beneath)
04981     {
04982       printf_filtered ("  - %s (%s)\n", t->to_shortname, t->to_longname);
04983     }
04984 }
04985 
04986 /* Controls if async mode is permitted.  */
04987 int target_async_permitted = 0;
04988 
04989 /* The set command writes to this variable.  If the inferior is
04990    executing, target_async_permitted is *not* updated.  */
04991 static int target_async_permitted_1 = 0;
04992 
04993 static void
04994 set_target_async_command (char *args, int from_tty,
04995                           struct cmd_list_element *c)
04996 {
04997   if (have_live_inferiors ())
04998     {
04999       target_async_permitted_1 = target_async_permitted;
05000       error (_("Cannot change this setting while the inferior is running."));
05001     }
05002 
05003   target_async_permitted = target_async_permitted_1;
05004 }
05005 
05006 static void
05007 show_target_async_command (struct ui_file *file, int from_tty,
05008                            struct cmd_list_element *c,
05009                            const char *value)
05010 {
05011   fprintf_filtered (file,
05012                     _("Controlling the inferior in "
05013                       "asynchronous mode is %s.\n"), value);
05014 }
05015 
05016 /* Temporary copies of permission settings.  */
05017 
05018 static int may_write_registers_1 = 1;
05019 static int may_write_memory_1 = 1;
05020 static int may_insert_breakpoints_1 = 1;
05021 static int may_insert_tracepoints_1 = 1;
05022 static int may_insert_fast_tracepoints_1 = 1;
05023 static int may_stop_1 = 1;
05024 
05025 /* Make the user-set values match the real values again.  */
05026 
05027 void
05028 update_target_permissions (void)
05029 {
05030   may_write_registers_1 = may_write_registers;
05031   may_write_memory_1 = may_write_memory;
05032   may_insert_breakpoints_1 = may_insert_breakpoints;
05033   may_insert_tracepoints_1 = may_insert_tracepoints;
05034   may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
05035   may_stop_1 = may_stop;
05036 }
05037 
05038 /* The one function handles (most of) the permission flags in the same
05039    way.  */
05040 
05041 static void
05042 set_target_permissions (char *args, int from_tty,
05043                         struct cmd_list_element *c)
05044 {
05045   if (target_has_execution)
05046     {
05047       update_target_permissions ();
05048       error (_("Cannot change this setting while the inferior is running."));
05049     }
05050 
05051   /* Make the real values match the user-changed values.  */
05052   may_write_registers = may_write_registers_1;
05053   may_insert_breakpoints = may_insert_breakpoints_1;
05054   may_insert_tracepoints = may_insert_tracepoints_1;
05055   may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
05056   may_stop = may_stop_1;
05057   update_observer_mode ();
05058 }
05059 
05060 /* Set memory write permission independently of observer mode.  */
05061 
05062 static void
05063 set_write_memory_permission (char *args, int from_tty,
05064                         struct cmd_list_element *c)
05065 {
05066   /* Make the real values match the user-changed values.  */
05067   may_write_memory = may_write_memory_1;
05068   update_observer_mode ();
05069 }
05070 
05071 
05072 void
05073 initialize_targets (void)
05074 {
05075   init_dummy_target ();
05076   push_target (&dummy_target);
05077 
05078   add_info ("target", target_info, targ_desc);
05079   add_info ("files", target_info, targ_desc);
05080 
05081   add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
05082 Set target debugging."), _("\
05083 Show target debugging."), _("\
05084 When non-zero, target debugging is enabled.  Higher numbers are more\n\
05085 verbose.  Changes do not take effect until the next \"run\" or \"target\"\n\
05086 command."),
05087                              NULL,
05088                              show_targetdebug,
05089                              &setdebuglist, &showdebuglist);
05090 
05091   add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
05092                            &trust_readonly, _("\
05093 Set mode for reading from readonly sections."), _("\
05094 Show mode for reading from readonly sections."), _("\
05095 When this mode is on, memory reads from readonly sections (such as .text)\n\
05096 will be read from the object file instead of from the target.  This will\n\
05097 result in significant performance improvement for remote targets."),
05098                            NULL,
05099                            show_trust_readonly,
05100                            &setlist, &showlist);
05101 
05102   add_com ("monitor", class_obscure, do_monitor_command,
05103            _("Send a command to the remote monitor (remote targets only)."));
05104 
05105   add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
05106            _("Print the name of each layer of the internal target stack."),
05107            &maintenanceprintlist);
05108 
05109   add_setshow_boolean_cmd ("target-async", no_class,
05110                            &target_async_permitted_1, _("\
05111 Set whether gdb controls the inferior in asynchronous mode."), _("\
05112 Show whether gdb controls the inferior in asynchronous mode."), _("\
05113 Tells gdb whether to control the inferior in asynchronous mode."),
05114                            set_target_async_command,
05115                            show_target_async_command,
05116                            &setlist,
05117                            &showlist);
05118 
05119   add_setshow_boolean_cmd ("stack-cache", class_support,
05120                            &stack_cache_enabled_p_1, _("\
05121 Set cache use for stack access."), _("\
05122 Show cache use for stack access."), _("\
05123 When on, use the data cache for all stack access, regardless of any\n\
05124 configured memory regions.  This improves remote performance significantly.\n\
05125 By default, caching for stack access is on."),
05126                            set_stack_cache_enabled_p,
05127                            show_stack_cache_enabled_p,
05128                            &setlist, &showlist);
05129 
05130   add_setshow_boolean_cmd ("may-write-registers", class_support,
05131                            &may_write_registers_1, _("\
05132 Set permission to write into registers."), _("\
05133 Show permission to write into registers."), _("\
05134 When this permission is on, GDB may write into the target's registers.\n\
05135 Otherwise, any sort of write attempt will result in an error."),
05136                            set_target_permissions, NULL,
05137                            &setlist, &showlist);
05138 
05139   add_setshow_boolean_cmd ("may-write-memory", class_support,
05140                            &may_write_memory_1, _("\
05141 Set permission to write into target memory."), _("\
05142 Show permission to write into target memory."), _("\
05143 When this permission is on, GDB may write into the target's memory.\n\
05144 Otherwise, any sort of write attempt will result in an error."),
05145                            set_write_memory_permission, NULL,
05146                            &setlist, &showlist);
05147 
05148   add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
05149                            &may_insert_breakpoints_1, _("\
05150 Set permission to insert breakpoints in the target."), _("\
05151 Show permission to insert breakpoints in the target."), _("\
05152 When this permission is on, GDB may insert breakpoints in the program.\n\
05153 Otherwise, any sort of insertion attempt will result in an error."),
05154                            set_target_permissions, NULL,
05155                            &setlist, &showlist);
05156 
05157   add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
05158                            &may_insert_tracepoints_1, _("\
05159 Set permission to insert tracepoints in the target."), _("\
05160 Show permission to insert tracepoints in the target."), _("\
05161 When this permission is on, GDB may insert tracepoints in the program.\n\
05162 Otherwise, any sort of insertion attempt will result in an error."),
05163                            set_target_permissions, NULL,
05164                            &setlist, &showlist);
05165 
05166   add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
05167                            &may_insert_fast_tracepoints_1, _("\
05168 Set permission to insert fast tracepoints in the target."), _("\
05169 Show permission to insert fast tracepoints in the target."), _("\
05170 When this permission is on, GDB may insert fast tracepoints.\n\
05171 Otherwise, any sort of insertion attempt will result in an error."),
05172                            set_target_permissions, NULL,
05173                            &setlist, &showlist);
05174 
05175   add_setshow_boolean_cmd ("may-interrupt", class_support,
05176                            &may_stop_1, _("\
05177 Set permission to interrupt or signal the target."), _("\
05178 Show permission to interrupt or signal the target."), _("\
05179 When this permission is on, GDB may interrupt/stop the target's execution.\n\
05180 Otherwise, any attempt to interrupt or stop will be ignored."),
05181                            set_target_permissions, NULL,
05182                            &setlist, &showlist);
05183 
05184 
05185   target_dcache = dcache_init ();
05186 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines