2011-11-14 Stan Shebs <stan@codesourcery.com>
[external/binutils.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2
3    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5    Free Software Foundation, Inc.
6
7    Contributed by Cygnus Support.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 #include "defs.h"
25 #include <errno.h>
26 #include "gdb_string.h"
27 #include "target.h"
28 #include "gdbcmd.h"
29 #include "symtab.h"
30 #include "inferior.h"
31 #include "bfd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "gdb_wait.h"
35 #include "dcache.h"
36 #include <signal.h>
37 #include "regcache.h"
38 #include "gdb_assert.h"
39 #include "gdbcore.h"
40 #include "exceptions.h"
41 #include "target-descriptions.h"
42 #include "gdbthread.h"
43 #include "solib.h"
44 #include "exec.h"
45 #include "inline-frame.h"
46 #include "tracepoint.h"
47
48 static void target_info (char *, int);
49
50 static void default_terminal_info (char *, int);
51
52 static int default_watchpoint_addr_within_range (struct target_ops *,
53                                                  CORE_ADDR, CORE_ADDR, int);
54
55 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
56
57 static void tcomplain (void) ATTRIBUTE_NORETURN;
58
59 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
60
61 static int return_zero (void);
62
63 static int return_one (void);
64
65 static int return_minus_one (void);
66
67 void target_ignore (void);
68
69 static void target_command (char *, int);
70
71 static struct target_ops *find_default_run_target (char *);
72
73 static LONGEST default_xfer_partial (struct target_ops *ops,
74                                      enum target_object object,
75                                      const char *annex, gdb_byte *readbuf,
76                                      const gdb_byte *writebuf,
77                                      ULONGEST offset, LONGEST len);
78
79 static LONGEST current_xfer_partial (struct target_ops *ops,
80                                      enum target_object object,
81                                      const char *annex, gdb_byte *readbuf,
82                                      const gdb_byte *writebuf,
83                                      ULONGEST offset, LONGEST len);
84
85 static LONGEST target_xfer_partial (struct target_ops *ops,
86                                     enum target_object object,
87                                     const char *annex,
88                                     void *readbuf, const void *writebuf,
89                                     ULONGEST offset, LONGEST len);
90
91 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
92                                                     ptid_t ptid);
93
94 static void init_dummy_target (void);
95
96 static struct target_ops debug_target;
97
98 static void debug_to_open (char *, int);
99
100 static void debug_to_prepare_to_store (struct regcache *);
101
102 static void debug_to_files_info (struct target_ops *);
103
104 static int debug_to_insert_breakpoint (struct gdbarch *,
105                                        struct bp_target_info *);
106
107 static int debug_to_remove_breakpoint (struct gdbarch *,
108                                        struct bp_target_info *);
109
110 static int debug_to_can_use_hw_breakpoint (int, int, int);
111
112 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
113                                           struct bp_target_info *);
114
115 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
116                                           struct bp_target_info *);
117
118 static int debug_to_insert_watchpoint (CORE_ADDR, int, int,
119                                        struct expression *);
120
121 static int debug_to_remove_watchpoint (CORE_ADDR, int, int,
122                                        struct expression *);
123
124 static int debug_to_stopped_by_watchpoint (void);
125
126 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
127
128 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
129                                                   CORE_ADDR, CORE_ADDR, int);
130
131 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
132
133 static int debug_to_can_accel_watchpoint_condition (CORE_ADDR, int, int,
134                                                     struct expression *);
135
136 static void debug_to_terminal_init (void);
137
138 static void debug_to_terminal_inferior (void);
139
140 static void debug_to_terminal_ours_for_output (void);
141
142 static void debug_to_terminal_save_ours (void);
143
144 static void debug_to_terminal_ours (void);
145
146 static void debug_to_terminal_info (char *, int);
147
148 static void debug_to_load (char *, int);
149
150 static int debug_to_can_run (void);
151
152 static void debug_to_stop (ptid_t);
153
154 /* Pointer to array of target architecture structures; the size of the
155    array; the current index into the array; the allocated size of the
156    array.  */
157 struct target_ops **target_structs;
158 unsigned target_struct_size;
159 unsigned target_struct_index;
160 unsigned target_struct_allocsize;
161 #define DEFAULT_ALLOCSIZE       10
162
163 /* The initial current target, so that there is always a semi-valid
164    current target.  */
165
166 static struct target_ops dummy_target;
167
168 /* Top of target stack.  */
169
170 static struct target_ops *target_stack;
171
172 /* The target structure we are currently using to talk to a process
173    or file or whatever "inferior" we have.  */
174
175 struct target_ops current_target;
176
177 /* Command list for target.  */
178
179 static struct cmd_list_element *targetlist = NULL;
180
181 /* Nonzero if we should trust readonly sections from the
182    executable when reading memory.  */
183
184 static int trust_readonly = 0;
185
186 /* Nonzero if we should show true memory content including
187    memory breakpoint inserted by gdb.  */
188
189 static int show_memory_breakpoints = 0;
190
191 /* These globals control whether GDB attempts to perform these
192    operations; they are useful for targets that need to prevent
193    inadvertant disruption, such as in non-stop mode.  */
194
195 int may_write_registers = 1;
196
197 int may_write_memory = 1;
198
199 int may_insert_breakpoints = 1;
200
201 int may_insert_tracepoints = 1;
202
203 int may_insert_fast_tracepoints = 1;
204
205 int may_stop = 1;
206
207 /* Non-zero if we want to see trace of target level stuff.  */
208
209 static int targetdebug = 0;
210 static void
211 show_targetdebug (struct ui_file *file, int from_tty,
212                   struct cmd_list_element *c, const char *value)
213 {
214   fprintf_filtered (file, _("Target debugging is %s.\n"), value);
215 }
216
217 static void setup_target_debug (void);
218
219 /* The option sets this.  */
220 static int stack_cache_enabled_p_1 = 1;
221 /* And set_stack_cache_enabled_p updates this.
222    The reason for the separation is so that we don't flush the cache for
223    on->on transitions.  */
224 static int stack_cache_enabled_p = 1;
225
226 /* This is called *after* the stack-cache has been set.
227    Flush the cache for off->on and on->off transitions.
228    There's no real need to flush the cache for on->off transitions,
229    except cleanliness.  */
230
231 static void
232 set_stack_cache_enabled_p (char *args, int from_tty,
233                            struct cmd_list_element *c)
234 {
235   if (stack_cache_enabled_p != stack_cache_enabled_p_1)
236     target_dcache_invalidate ();
237
238   stack_cache_enabled_p = stack_cache_enabled_p_1;
239 }
240
241 static void
242 show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
243                             struct cmd_list_element *c, const char *value)
244 {
245   fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
246 }
247
248 /* Cache of memory operations, to speed up remote access.  */
249 static DCACHE *target_dcache;
250
251 /* Invalidate the target dcache.  */
252
253 void
254 target_dcache_invalidate (void)
255 {
256   dcache_invalidate (target_dcache);
257 }
258
259 /* The user just typed 'target' without the name of a target.  */
260
261 static void
262 target_command (char *arg, int from_tty)
263 {
264   fputs_filtered ("Argument required (target name).  Try `help target'\n",
265                   gdb_stdout);
266 }
267
268 /* Default target_has_* methods for process_stratum targets.  */
269
270 int
271 default_child_has_all_memory (struct target_ops *ops)
272 {
273   /* If no inferior selected, then we can't read memory here.  */
274   if (ptid_equal (inferior_ptid, null_ptid))
275     return 0;
276
277   return 1;
278 }
279
280 int
281 default_child_has_memory (struct target_ops *ops)
282 {
283   /* If no inferior selected, then we can't read memory here.  */
284   if (ptid_equal (inferior_ptid, null_ptid))
285     return 0;
286
287   return 1;
288 }
289
290 int
291 default_child_has_stack (struct target_ops *ops)
292 {
293   /* If no inferior selected, there's no stack.  */
294   if (ptid_equal (inferior_ptid, null_ptid))
295     return 0;
296
297   return 1;
298 }
299
300 int
301 default_child_has_registers (struct target_ops *ops)
302 {
303   /* Can't read registers from no inferior.  */
304   if (ptid_equal (inferior_ptid, null_ptid))
305     return 0;
306
307   return 1;
308 }
309
310 int
311 default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
312 {
313   /* If there's no thread selected, then we can't make it run through
314      hoops.  */
315   if (ptid_equal (the_ptid, null_ptid))
316     return 0;
317
318   return 1;
319 }
320
321
322 int
323 target_has_all_memory_1 (void)
324 {
325   struct target_ops *t;
326
327   for (t = current_target.beneath; t != NULL; t = t->beneath)
328     if (t->to_has_all_memory (t))
329       return 1;
330
331   return 0;
332 }
333
334 int
335 target_has_memory_1 (void)
336 {
337   struct target_ops *t;
338
339   for (t = current_target.beneath; t != NULL; t = t->beneath)
340     if (t->to_has_memory (t))
341       return 1;
342
343   return 0;
344 }
345
346 int
347 target_has_stack_1 (void)
348 {
349   struct target_ops *t;
350
351   for (t = current_target.beneath; t != NULL; t = t->beneath)
352     if (t->to_has_stack (t))
353       return 1;
354
355   return 0;
356 }
357
358 int
359 target_has_registers_1 (void)
360 {
361   struct target_ops *t;
362
363   for (t = current_target.beneath; t != NULL; t = t->beneath)
364     if (t->to_has_registers (t))
365       return 1;
366
367   return 0;
368 }
369
370 int
371 target_has_execution_1 (ptid_t the_ptid)
372 {
373   struct target_ops *t;
374
375   for (t = current_target.beneath; t != NULL; t = t->beneath)
376     if (t->to_has_execution (t, the_ptid))
377       return 1;
378
379   return 0;
380 }
381
382 int
383 target_has_execution_current (void)
384 {
385   return target_has_execution_1 (inferior_ptid);
386 }
387
388 /* Add a possible target architecture to the list.  */
389
390 void
391 add_target (struct target_ops *t)
392 {
393   /* Provide default values for all "must have" methods.  */
394   if (t->to_xfer_partial == NULL)
395     t->to_xfer_partial = default_xfer_partial;
396
397   if (t->to_has_all_memory == NULL)
398     t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
399
400   if (t->to_has_memory == NULL)
401     t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
402
403   if (t->to_has_stack == NULL)
404     t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
405
406   if (t->to_has_registers == NULL)
407     t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
408
409   if (t->to_has_execution == NULL)
410     t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
411
412   if (!target_structs)
413     {
414       target_struct_allocsize = DEFAULT_ALLOCSIZE;
415       target_structs = (struct target_ops **) xmalloc
416         (target_struct_allocsize * sizeof (*target_structs));
417     }
418   if (target_struct_size >= target_struct_allocsize)
419     {
420       target_struct_allocsize *= 2;
421       target_structs = (struct target_ops **)
422         xrealloc ((char *) target_structs,
423                   target_struct_allocsize * sizeof (*target_structs));
424     }
425   target_structs[target_struct_size++] = t;
426
427   if (targetlist == NULL)
428     add_prefix_cmd ("target", class_run, target_command, _("\
429 Connect to a target machine or process.\n\
430 The first argument is the type or protocol of the target machine.\n\
431 Remaining arguments are interpreted by the target protocol.  For more\n\
432 information on the arguments for a particular protocol, type\n\
433 `help target ' followed by the protocol name."),
434                     &targetlist, "target ", 0, &cmdlist);
435   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
436 }
437
438 /* Stub functions */
439
440 void
441 target_ignore (void)
442 {
443 }
444
445 void
446 target_kill (void)
447 {
448   struct target_ops *t;
449
450   for (t = current_target.beneath; t != NULL; t = t->beneath)
451     if (t->to_kill != NULL)
452       {
453         if (targetdebug)
454           fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
455
456         t->to_kill (t);
457         return;
458       }
459
460   noprocess ();
461 }
462
463 void
464 target_load (char *arg, int from_tty)
465 {
466   target_dcache_invalidate ();
467   (*current_target.to_load) (arg, from_tty);
468 }
469
470 void
471 target_create_inferior (char *exec_file, char *args,
472                         char **env, int from_tty)
473 {
474   struct target_ops *t;
475
476   for (t = current_target.beneath; t != NULL; t = t->beneath)
477     {
478       if (t->to_create_inferior != NULL)        
479         {
480           t->to_create_inferior (t, exec_file, args, env, from_tty);
481           if (targetdebug)
482             fprintf_unfiltered (gdb_stdlog,
483                                 "target_create_inferior (%s, %s, xxx, %d)\n",
484                                 exec_file, args, from_tty);
485           return;
486         }
487     }
488
489   internal_error (__FILE__, __LINE__,
490                   _("could not find a target to create inferior"));
491 }
492
493 void
494 target_terminal_inferior (void)
495 {
496   /* A background resume (``run&'') should leave GDB in control of the
497      terminal.  Use target_can_async_p, not target_is_async_p, since at
498      this point the target is not async yet.  However, if sync_execution
499      is not set, we know it will become async prior to resume.  */
500   if (target_can_async_p () && !sync_execution)
501     return;
502
503   /* If GDB is resuming the inferior in the foreground, install
504      inferior's terminal modes.  */
505   (*current_target.to_terminal_inferior) ();
506 }
507
508 static int
509 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
510           struct target_ops *t)
511 {
512   errno = EIO;                  /* Can't read/write this location.  */
513   return 0;                     /* No bytes handled.  */
514 }
515
516 static void
517 tcomplain (void)
518 {
519   error (_("You can't do that when your target is `%s'"),
520          current_target.to_shortname);
521 }
522
523 void
524 noprocess (void)
525 {
526   error (_("You can't do that without a process to debug."));
527 }
528
529 static void
530 default_terminal_info (char *args, int from_tty)
531 {
532   printf_unfiltered (_("No saved terminal information.\n"));
533 }
534
535 /* A default implementation for the to_get_ada_task_ptid target method.
536
537    This function builds the PTID by using both LWP and TID as part of
538    the PTID lwp and tid elements.  The pid used is the pid of the
539    inferior_ptid.  */
540
541 static ptid_t
542 default_get_ada_task_ptid (long lwp, long tid)
543 {
544   return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
545 }
546
547 static enum exec_direction_kind
548 default_execution_direction (void)
549 {
550   if (!target_can_execute_reverse)
551     return EXEC_FORWARD;
552   else if (!target_can_async_p ())
553     return EXEC_FORWARD;
554   else
555     gdb_assert_not_reached ("\
556 to_execution_direction must be implemented for reverse async");
557 }
558
559 /* Go through the target stack from top to bottom, copying over zero
560    entries in current_target, then filling in still empty entries.  In
561    effect, we are doing class inheritance through the pushed target
562    vectors.
563
564    NOTE: cagney/2003-10-17: The problem with this inheritance, as it
565    is currently implemented, is that it discards any knowledge of
566    which target an inherited method originally belonged to.
567    Consequently, new new target methods should instead explicitly and
568    locally search the target stack for the target that can handle the
569    request.  */
570
571 static void
572 update_current_target (void)
573 {
574   struct target_ops *t;
575
576   /* First, reset current's contents.  */
577   memset (&current_target, 0, sizeof (current_target));
578
579 #define INHERIT(FIELD, TARGET) \
580       if (!current_target.FIELD) \
581         current_target.FIELD = (TARGET)->FIELD
582
583   for (t = target_stack; t; t = t->beneath)
584     {
585       INHERIT (to_shortname, t);
586       INHERIT (to_longname, t);
587       INHERIT (to_doc, t);
588       /* Do not inherit to_open.  */
589       /* Do not inherit to_close.  */
590       /* Do not inherit to_attach.  */
591       INHERIT (to_post_attach, t);
592       INHERIT (to_attach_no_wait, t);
593       /* Do not inherit to_detach.  */
594       /* Do not inherit to_disconnect.  */
595       /* Do not inherit to_resume.  */
596       /* Do not inherit to_wait.  */
597       /* Do not inherit to_fetch_registers.  */
598       /* Do not inherit to_store_registers.  */
599       INHERIT (to_prepare_to_store, t);
600       INHERIT (deprecated_xfer_memory, t);
601       INHERIT (to_files_info, t);
602       INHERIT (to_insert_breakpoint, t);
603       INHERIT (to_remove_breakpoint, t);
604       INHERIT (to_can_use_hw_breakpoint, t);
605       INHERIT (to_insert_hw_breakpoint, t);
606       INHERIT (to_remove_hw_breakpoint, t);
607       /* Do not inherit to_ranged_break_num_registers.  */
608       INHERIT (to_insert_watchpoint, t);
609       INHERIT (to_remove_watchpoint, t);
610       /* Do not inherit to_insert_mask_watchpoint.  */
611       /* Do not inherit to_remove_mask_watchpoint.  */
612       INHERIT (to_stopped_data_address, t);
613       INHERIT (to_have_steppable_watchpoint, t);
614       INHERIT (to_have_continuable_watchpoint, t);
615       INHERIT (to_stopped_by_watchpoint, t);
616       INHERIT (to_watchpoint_addr_within_range, t);
617       INHERIT (to_region_ok_for_hw_watchpoint, t);
618       INHERIT (to_can_accel_watchpoint_condition, t);
619       /* Do not inherit to_masked_watch_num_registers.  */
620       INHERIT (to_terminal_init, t);
621       INHERIT (to_terminal_inferior, t);
622       INHERIT (to_terminal_ours_for_output, t);
623       INHERIT (to_terminal_ours, t);
624       INHERIT (to_terminal_save_ours, t);
625       INHERIT (to_terminal_info, t);
626       /* Do not inherit to_kill.  */
627       INHERIT (to_load, t);
628       /* Do no inherit to_create_inferior.  */
629       INHERIT (to_post_startup_inferior, t);
630       INHERIT (to_insert_fork_catchpoint, t);
631       INHERIT (to_remove_fork_catchpoint, t);
632       INHERIT (to_insert_vfork_catchpoint, t);
633       INHERIT (to_remove_vfork_catchpoint, t);
634       /* Do not inherit to_follow_fork.  */
635       INHERIT (to_insert_exec_catchpoint, t);
636       INHERIT (to_remove_exec_catchpoint, t);
637       INHERIT (to_set_syscall_catchpoint, t);
638       INHERIT (to_has_exited, t);
639       /* Do not inherit to_mourn_inferior.  */
640       INHERIT (to_can_run, t);
641       /* Do not inherit to_pass_signals.  */
642       /* Do not inherit to_thread_alive.  */
643       /* Do not inherit to_find_new_threads.  */
644       /* Do not inherit to_pid_to_str.  */
645       INHERIT (to_extra_thread_info, t);
646       INHERIT (to_thread_name, t);
647       INHERIT (to_stop, t);
648       /* Do not inherit to_xfer_partial.  */
649       INHERIT (to_rcmd, t);
650       INHERIT (to_pid_to_exec_file, t);
651       INHERIT (to_log_command, t);
652       INHERIT (to_stratum, t);
653       /* Do not inherit to_has_all_memory.  */
654       /* Do not inherit to_has_memory.  */
655       /* Do not inherit to_has_stack.  */
656       /* Do not inherit to_has_registers.  */
657       /* Do not inherit to_has_execution.  */
658       INHERIT (to_has_thread_control, t);
659       INHERIT (to_can_async_p, t);
660       INHERIT (to_is_async_p, t);
661       INHERIT (to_async, t);
662       INHERIT (to_find_memory_regions, t);
663       INHERIT (to_make_corefile_notes, t);
664       INHERIT (to_get_bookmark, t);
665       INHERIT (to_goto_bookmark, t);
666       /* Do not inherit to_get_thread_local_address.  */
667       INHERIT (to_can_execute_reverse, t);
668       INHERIT (to_execution_direction, t);
669       INHERIT (to_thread_architecture, t);
670       /* Do not inherit to_read_description.  */
671       INHERIT (to_get_ada_task_ptid, t);
672       /* Do not inherit to_search_memory.  */
673       INHERIT (to_supports_multi_process, t);
674       INHERIT (to_supports_enable_disable_tracepoint, t);
675       INHERIT (to_supports_string_tracing, t);
676       INHERIT (to_trace_init, t);
677       INHERIT (to_download_tracepoint, t);
678       INHERIT (to_can_download_tracepoint, t);
679       INHERIT (to_download_trace_state_variable, t);
680       INHERIT (to_enable_tracepoint, t);
681       INHERIT (to_disable_tracepoint, t);
682       INHERIT (to_trace_set_readonly_regions, t);
683       INHERIT (to_trace_start, t);
684       INHERIT (to_get_trace_status, t);
685       INHERIT (to_trace_stop, t);
686       INHERIT (to_trace_find, t);
687       INHERIT (to_get_trace_state_variable_value, t);
688       INHERIT (to_save_trace_data, t);
689       INHERIT (to_upload_tracepoints, t);
690       INHERIT (to_upload_trace_state_variables, t);
691       INHERIT (to_get_raw_trace_data, t);
692       INHERIT (to_get_min_fast_tracepoint_insn_len, t);
693       INHERIT (to_set_disconnected_tracing, t);
694       INHERIT (to_set_circular_trace_buffer, t);
695       INHERIT (to_get_tib_address, t);
696       INHERIT (to_set_permissions, t);
697       INHERIT (to_static_tracepoint_marker_at, t);
698       INHERIT (to_static_tracepoint_markers_by_strid, t);
699       INHERIT (to_traceframe_info, t);
700       INHERIT (to_magic, t);
701       /* Do not inherit to_memory_map.  */
702       /* Do not inherit to_flash_erase.  */
703       /* Do not inherit to_flash_done.  */
704     }
705 #undef INHERIT
706
707   /* Clean up a target struct so it no longer has any zero pointers in
708      it.  Some entries are defaulted to a method that print an error,
709      others are hard-wired to a standard recursive default.  */
710
711 #define de_fault(field, value) \
712   if (!current_target.field)               \
713     current_target.field = value
714
715   de_fault (to_open,
716             (void (*) (char *, int))
717             tcomplain);
718   de_fault (to_close,
719             (void (*) (int))
720             target_ignore);
721   de_fault (to_post_attach,
722             (void (*) (int))
723             target_ignore);
724   de_fault (to_prepare_to_store,
725             (void (*) (struct regcache *))
726             noprocess);
727   de_fault (deprecated_xfer_memory,
728             (int (*) (CORE_ADDR, gdb_byte *, int, int,
729                       struct mem_attrib *, struct target_ops *))
730             nomemory);
731   de_fault (to_files_info,
732             (void (*) (struct target_ops *))
733             target_ignore);
734   de_fault (to_insert_breakpoint,
735             memory_insert_breakpoint);
736   de_fault (to_remove_breakpoint,
737             memory_remove_breakpoint);
738   de_fault (to_can_use_hw_breakpoint,
739             (int (*) (int, int, int))
740             return_zero);
741   de_fault (to_insert_hw_breakpoint,
742             (int (*) (struct gdbarch *, struct bp_target_info *))
743             return_minus_one);
744   de_fault (to_remove_hw_breakpoint,
745             (int (*) (struct gdbarch *, struct bp_target_info *))
746             return_minus_one);
747   de_fault (to_insert_watchpoint,
748             (int (*) (CORE_ADDR, int, int, struct expression *))
749             return_minus_one);
750   de_fault (to_remove_watchpoint,
751             (int (*) (CORE_ADDR, int, int, struct expression *))
752             return_minus_one);
753   de_fault (to_stopped_by_watchpoint,
754             (int (*) (void))
755             return_zero);
756   de_fault (to_stopped_data_address,
757             (int (*) (struct target_ops *, CORE_ADDR *))
758             return_zero);
759   de_fault (to_watchpoint_addr_within_range,
760             default_watchpoint_addr_within_range);
761   de_fault (to_region_ok_for_hw_watchpoint,
762             default_region_ok_for_hw_watchpoint);
763   de_fault (to_can_accel_watchpoint_condition,
764             (int (*) (CORE_ADDR, int, int, struct expression *))
765             return_zero);
766   de_fault (to_terminal_init,
767             (void (*) (void))
768             target_ignore);
769   de_fault (to_terminal_inferior,
770             (void (*) (void))
771             target_ignore);
772   de_fault (to_terminal_ours_for_output,
773             (void (*) (void))
774             target_ignore);
775   de_fault (to_terminal_ours,
776             (void (*) (void))
777             target_ignore);
778   de_fault (to_terminal_save_ours,
779             (void (*) (void))
780             target_ignore);
781   de_fault (to_terminal_info,
782             default_terminal_info);
783   de_fault (to_load,
784             (void (*) (char *, int))
785             tcomplain);
786   de_fault (to_post_startup_inferior,
787             (void (*) (ptid_t))
788             target_ignore);
789   de_fault (to_insert_fork_catchpoint,
790             (int (*) (int))
791             return_one);
792   de_fault (to_remove_fork_catchpoint,
793             (int (*) (int))
794             return_one);
795   de_fault (to_insert_vfork_catchpoint,
796             (int (*) (int))
797             return_one);
798   de_fault (to_remove_vfork_catchpoint,
799             (int (*) (int))
800             return_one);
801   de_fault (to_insert_exec_catchpoint,
802             (int (*) (int))
803             return_one);
804   de_fault (to_remove_exec_catchpoint,
805             (int (*) (int))
806             return_one);
807   de_fault (to_set_syscall_catchpoint,
808             (int (*) (int, int, int, int, int *))
809             return_one);
810   de_fault (to_has_exited,
811             (int (*) (int, int, int *))
812             return_zero);
813   de_fault (to_can_run,
814             return_zero);
815   de_fault (to_extra_thread_info,
816             (char *(*) (struct thread_info *))
817             return_zero);
818   de_fault (to_thread_name,
819             (char *(*) (struct thread_info *))
820             return_zero);
821   de_fault (to_stop,
822             (void (*) (ptid_t))
823             target_ignore);
824   current_target.to_xfer_partial = current_xfer_partial;
825   de_fault (to_rcmd,
826             (void (*) (char *, struct ui_file *))
827             tcomplain);
828   de_fault (to_pid_to_exec_file,
829             (char *(*) (int))
830             return_zero);
831   de_fault (to_async,
832             (void (*) (void (*) (enum inferior_event_type, void*), void*))
833             tcomplain);
834   de_fault (to_thread_architecture,
835             default_thread_architecture);
836   current_target.to_read_description = NULL;
837   de_fault (to_get_ada_task_ptid,
838             (ptid_t (*) (long, long))
839             default_get_ada_task_ptid);
840   de_fault (to_supports_multi_process,
841             (int (*) (void))
842             return_zero);
843   de_fault (to_supports_enable_disable_tracepoint,
844             (int (*) (void))
845             return_zero);
846   de_fault (to_supports_string_tracing,
847             (int (*) (void))
848             return_zero);
849   de_fault (to_trace_init,
850             (void (*) (void))
851             tcomplain);
852   de_fault (to_download_tracepoint,
853             (void (*) (struct bp_location *))
854             tcomplain);
855   de_fault (to_can_download_tracepoint,
856             (int (*) (void))
857             return_zero);
858   de_fault (to_download_trace_state_variable,
859             (void (*) (struct trace_state_variable *))
860             tcomplain);
861   de_fault (to_enable_tracepoint,
862             (void (*) (struct bp_location *))
863             tcomplain);
864   de_fault (to_disable_tracepoint,
865             (void (*) (struct bp_location *))
866             tcomplain);
867   de_fault (to_trace_set_readonly_regions,
868             (void (*) (void))
869             tcomplain);
870   de_fault (to_trace_start,
871             (void (*) (void))
872             tcomplain);
873   de_fault (to_get_trace_status,
874             (int (*) (struct trace_status *))
875             return_minus_one);
876   de_fault (to_trace_stop,
877             (void (*) (void))
878             tcomplain);
879   de_fault (to_trace_find,
880             (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
881             return_minus_one);
882   de_fault (to_get_trace_state_variable_value,
883             (int (*) (int, LONGEST *))
884             return_zero);
885   de_fault (to_save_trace_data,
886             (int (*) (const char *))
887             tcomplain);
888   de_fault (to_upload_tracepoints,
889             (int (*) (struct uploaded_tp **))
890             return_zero);
891   de_fault (to_upload_trace_state_variables,
892             (int (*) (struct uploaded_tsv **))
893             return_zero);
894   de_fault (to_get_raw_trace_data,
895             (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
896             tcomplain);
897   de_fault (to_get_min_fast_tracepoint_insn_len,
898             (int (*) (void))
899             return_minus_one);
900   de_fault (to_set_disconnected_tracing,
901             (void (*) (int))
902             target_ignore);
903   de_fault (to_set_circular_trace_buffer,
904             (void (*) (int))
905             target_ignore);
906   de_fault (to_get_tib_address,
907             (int (*) (ptid_t, CORE_ADDR *))
908             tcomplain);
909   de_fault (to_set_permissions,
910             (void (*) (void))
911             target_ignore);
912   de_fault (to_static_tracepoint_marker_at,
913             (int (*) (CORE_ADDR, struct static_tracepoint_marker *))
914             return_zero);
915   de_fault (to_static_tracepoint_markers_by_strid,
916             (VEC(static_tracepoint_marker_p) * (*) (const char *))
917             tcomplain);
918   de_fault (to_traceframe_info,
919             (struct traceframe_info * (*) (void))
920             tcomplain);
921   de_fault (to_execution_direction, default_execution_direction);
922
923 #undef de_fault
924
925   /* Finally, position the target-stack beneath the squashed
926      "current_target".  That way code looking for a non-inherited
927      target method can quickly and simply find it.  */
928   current_target.beneath = target_stack;
929
930   if (targetdebug)
931     setup_target_debug ();
932 }
933
934 /* Push a new target type into the stack of the existing target accessors,
935    possibly superseding some of the existing accessors.
936
937    Rather than allow an empty stack, we always have the dummy target at
938    the bottom stratum, so we can call the function vectors without
939    checking them.  */
940
941 void
942 push_target (struct target_ops *t)
943 {
944   struct target_ops **cur;
945
946   /* Check magic number.  If wrong, it probably means someone changed
947      the struct definition, but not all the places that initialize one.  */
948   if (t->to_magic != OPS_MAGIC)
949     {
950       fprintf_unfiltered (gdb_stderr,
951                           "Magic number of %s target struct wrong\n",
952                           t->to_shortname);
953       internal_error (__FILE__, __LINE__,
954                       _("failed internal consistency check"));
955     }
956
957   /* Find the proper stratum to install this target in.  */
958   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
959     {
960       if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
961         break;
962     }
963
964   /* If there's already targets at this stratum, remove them.  */
965   /* FIXME: cagney/2003-10-15: I think this should be popping all
966      targets to CUR, and not just those at this stratum level.  */
967   while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
968     {
969       /* There's already something at this stratum level.  Close it,
970          and un-hook it from the stack.  */
971       struct target_ops *tmp = (*cur);
972
973       (*cur) = (*cur)->beneath;
974       tmp->beneath = NULL;
975       target_close (tmp, 0);
976     }
977
978   /* We have removed all targets in our stratum, now add the new one.  */
979   t->beneath = (*cur);
980   (*cur) = t;
981
982   update_current_target ();
983 }
984
985 /* Remove a target_ops vector from the stack, wherever it may be.
986    Return how many times it was removed (0 or 1).  */
987
988 int
989 unpush_target (struct target_ops *t)
990 {
991   struct target_ops **cur;
992   struct target_ops *tmp;
993
994   if (t->to_stratum == dummy_stratum)
995     internal_error (__FILE__, __LINE__,
996                     _("Attempt to unpush the dummy target"));
997
998   /* Look for the specified target.  Note that we assume that a target
999      can only occur once in the target stack.  */
1000
1001   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1002     {
1003       if ((*cur) == t)
1004         break;
1005     }
1006
1007   if ((*cur) == NULL)
1008     return 0;                   /* Didn't find target_ops, quit now.  */
1009
1010   /* NOTE: cagney/2003-12-06: In '94 the close call was made
1011      unconditional by moving it to before the above check that the
1012      target was in the target stack (something about "Change the way
1013      pushing and popping of targets work to support target overlays
1014      and inheritance").  This doesn't make much sense - only open
1015      targets should be closed.  */
1016   target_close (t, 0);
1017
1018   /* Unchain the target.  */
1019   tmp = (*cur);
1020   (*cur) = (*cur)->beneath;
1021   tmp->beneath = NULL;
1022
1023   update_current_target ();
1024
1025   return 1;
1026 }
1027
1028 void
1029 pop_target (void)
1030 {
1031   target_close (target_stack, 0);       /* Let it clean up.  */
1032   if (unpush_target (target_stack) == 1)
1033     return;
1034
1035   fprintf_unfiltered (gdb_stderr,
1036                       "pop_target couldn't find target %s\n",
1037                       current_target.to_shortname);
1038   internal_error (__FILE__, __LINE__,
1039                   _("failed internal consistency check"));
1040 }
1041
1042 void
1043 pop_all_targets_above (enum strata above_stratum, int quitting)
1044 {
1045   while ((int) (current_target.to_stratum) > (int) above_stratum)
1046     {
1047       target_close (target_stack, quitting);
1048       if (!unpush_target (target_stack))
1049         {
1050           fprintf_unfiltered (gdb_stderr,
1051                               "pop_all_targets couldn't find target %s\n",
1052                               target_stack->to_shortname);
1053           internal_error (__FILE__, __LINE__,
1054                           _("failed internal consistency check"));
1055           break;
1056         }
1057     }
1058 }
1059
1060 void
1061 pop_all_targets (int quitting)
1062 {
1063   pop_all_targets_above (dummy_stratum, quitting);
1064 }
1065
1066 /* Return 1 if T is now pushed in the target stack.  Return 0 otherwise.  */
1067
1068 int
1069 target_is_pushed (struct target_ops *t)
1070 {
1071   struct target_ops **cur;
1072
1073   /* Check magic number.  If wrong, it probably means someone changed
1074      the struct definition, but not all the places that initialize one.  */
1075   if (t->to_magic != OPS_MAGIC)
1076     {
1077       fprintf_unfiltered (gdb_stderr,
1078                           "Magic number of %s target struct wrong\n",
1079                           t->to_shortname);
1080       internal_error (__FILE__, __LINE__,
1081                       _("failed internal consistency check"));
1082     }
1083
1084   for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1085     if (*cur == t)
1086       return 1;
1087
1088   return 0;
1089 }
1090
1091 /* Using the objfile specified in OBJFILE, find the address for the
1092    current thread's thread-local storage with offset OFFSET.  */
1093 CORE_ADDR
1094 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1095 {
1096   volatile CORE_ADDR addr = 0;
1097   struct target_ops *target;
1098
1099   for (target = current_target.beneath;
1100        target != NULL;
1101        target = target->beneath)
1102     {
1103       if (target->to_get_thread_local_address != NULL)
1104         break;
1105     }
1106
1107   if (target != NULL
1108       && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
1109     {
1110       ptid_t ptid = inferior_ptid;
1111       volatile struct gdb_exception ex;
1112
1113       TRY_CATCH (ex, RETURN_MASK_ALL)
1114         {
1115           CORE_ADDR lm_addr;
1116           
1117           /* Fetch the load module address for this objfile.  */
1118           lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
1119                                                            objfile);
1120           /* If it's 0, throw the appropriate exception.  */
1121           if (lm_addr == 0)
1122             throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1123                          _("TLS load module not found"));
1124
1125           addr = target->to_get_thread_local_address (target, ptid,
1126                                                       lm_addr, offset);
1127         }
1128       /* If an error occurred, print TLS related messages here.  Otherwise,
1129          throw the error to some higher catcher.  */
1130       if (ex.reason < 0)
1131         {
1132           int objfile_is_library = (objfile->flags & OBJF_SHARED);
1133
1134           switch (ex.error)
1135             {
1136             case TLS_NO_LIBRARY_SUPPORT_ERROR:
1137               error (_("Cannot find thread-local variables "
1138                        "in this thread library."));
1139               break;
1140             case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1141               if (objfile_is_library)
1142                 error (_("Cannot find shared library `%s' in dynamic"
1143                          " linker's load module list"), objfile->name);
1144               else
1145                 error (_("Cannot find executable file `%s' in dynamic"
1146                          " linker's load module list"), objfile->name);
1147               break;
1148             case TLS_NOT_ALLOCATED_YET_ERROR:
1149               if (objfile_is_library)
1150                 error (_("The inferior has not yet allocated storage for"
1151                          " thread-local variables in\n"
1152                          "the shared library `%s'\n"
1153                          "for %s"),
1154                        objfile->name, target_pid_to_str (ptid));
1155               else
1156                 error (_("The inferior has not yet allocated storage for"
1157                          " thread-local variables in\n"
1158                          "the executable `%s'\n"
1159                          "for %s"),
1160                        objfile->name, target_pid_to_str (ptid));
1161               break;
1162             case TLS_GENERIC_ERROR:
1163               if (objfile_is_library)
1164                 error (_("Cannot find thread-local storage for %s, "
1165                          "shared library %s:\n%s"),
1166                        target_pid_to_str (ptid),
1167                        objfile->name, ex.message);
1168               else
1169                 error (_("Cannot find thread-local storage for %s, "
1170                          "executable file %s:\n%s"),
1171                        target_pid_to_str (ptid),
1172                        objfile->name, ex.message);
1173               break;
1174             default:
1175               throw_exception (ex);
1176               break;
1177             }
1178         }
1179     }
1180   /* It wouldn't be wrong here to try a gdbarch method, too; finding
1181      TLS is an ABI-specific thing.  But we don't do that yet.  */
1182   else
1183     error (_("Cannot find thread-local variables on this target"));
1184
1185   return addr;
1186 }
1187
1188 #undef  MIN
1189 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1190
1191 /* target_read_string -- read a null terminated string, up to LEN bytes,
1192    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
1193    Set *STRING to a pointer to malloc'd memory containing the data; the caller
1194    is responsible for freeing it.  Return the number of bytes successfully
1195    read.  */
1196
1197 int
1198 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1199 {
1200   int tlen, origlen, offset, i;
1201   gdb_byte buf[4];
1202   int errcode = 0;
1203   char *buffer;
1204   int buffer_allocated;
1205   char *bufptr;
1206   unsigned int nbytes_read = 0;
1207
1208   gdb_assert (string);
1209
1210   /* Small for testing.  */
1211   buffer_allocated = 4;
1212   buffer = xmalloc (buffer_allocated);
1213   bufptr = buffer;
1214
1215   origlen = len;
1216
1217   while (len > 0)
1218     {
1219       tlen = MIN (len, 4 - (memaddr & 3));
1220       offset = memaddr & 3;
1221
1222       errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1223       if (errcode != 0)
1224         {
1225           /* The transfer request might have crossed the boundary to an
1226              unallocated region of memory.  Retry the transfer, requesting
1227              a single byte.  */
1228           tlen = 1;
1229           offset = 0;
1230           errcode = target_read_memory (memaddr, buf, 1);
1231           if (errcode != 0)
1232             goto done;
1233         }
1234
1235       if (bufptr - buffer + tlen > buffer_allocated)
1236         {
1237           unsigned int bytes;
1238
1239           bytes = bufptr - buffer;
1240           buffer_allocated *= 2;
1241           buffer = xrealloc (buffer, buffer_allocated);
1242           bufptr = buffer + bytes;
1243         }
1244
1245       for (i = 0; i < tlen; i++)
1246         {
1247           *bufptr++ = buf[i + offset];
1248           if (buf[i + offset] == '\000')
1249             {
1250               nbytes_read += i + 1;
1251               goto done;
1252             }
1253         }
1254
1255       memaddr += tlen;
1256       len -= tlen;
1257       nbytes_read += tlen;
1258     }
1259 done:
1260   *string = buffer;
1261   if (errnop != NULL)
1262     *errnop = errcode;
1263   return nbytes_read;
1264 }
1265
1266 struct target_section_table *
1267 target_get_section_table (struct target_ops *target)
1268 {
1269   struct target_ops *t;
1270
1271   if (targetdebug)
1272     fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1273
1274   for (t = target; t != NULL; t = t->beneath)
1275     if (t->to_get_section_table != NULL)
1276       return (*t->to_get_section_table) (t);
1277
1278   return NULL;
1279 }
1280
1281 /* Find a section containing ADDR.  */
1282
1283 struct target_section *
1284 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1285 {
1286   struct target_section_table *table = target_get_section_table (target);
1287   struct target_section *secp;
1288
1289   if (table == NULL)
1290     return NULL;
1291
1292   for (secp = table->sections; secp < table->sections_end; secp++)
1293     {
1294       if (addr >= secp->addr && addr < secp->endaddr)
1295         return secp;
1296     }
1297   return NULL;
1298 }
1299
1300 /* Read memory from the live target, even if currently inspecting a
1301    traceframe.  The return is the same as that of target_read.  */
1302
1303 static LONGEST
1304 target_read_live_memory (enum target_object object,
1305                          ULONGEST memaddr, gdb_byte *myaddr, LONGEST len)
1306 {
1307   int ret;
1308   struct cleanup *cleanup;
1309
1310   /* Switch momentarily out of tfind mode so to access live memory.
1311      Note that this must not clear global state, such as the frame
1312      cache, which must still remain valid for the previous traceframe.
1313      We may be _building_ the frame cache at this point.  */
1314   cleanup = make_cleanup_restore_traceframe_number ();
1315   set_traceframe_number (-1);
1316
1317   ret = target_read (current_target.beneath, object, NULL,
1318                      myaddr, memaddr, len);
1319
1320   do_cleanups (cleanup);
1321   return ret;
1322 }
1323
1324 /* Using the set of read-only target sections of OPS, read live
1325    read-only memory.  Note that the actual reads start from the
1326    top-most target again.
1327
1328    For interface/parameters/return description see target.h,
1329    to_xfer_partial.  */
1330
1331 static LONGEST
1332 memory_xfer_live_readonly_partial (struct target_ops *ops,
1333                                    enum target_object object,
1334                                    gdb_byte *readbuf, ULONGEST memaddr,
1335                                    LONGEST len)
1336 {
1337   struct target_section *secp;
1338   struct target_section_table *table;
1339
1340   secp = target_section_by_addr (ops, memaddr);
1341   if (secp != NULL
1342       && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1343           & SEC_READONLY))
1344     {
1345       struct target_section *p;
1346       ULONGEST memend = memaddr + len;
1347
1348       table = target_get_section_table (ops);
1349
1350       for (p = table->sections; p < table->sections_end; p++)
1351         {
1352           if (memaddr >= p->addr)
1353             {
1354               if (memend <= p->endaddr)
1355                 {
1356                   /* Entire transfer is within this section.  */
1357                   return target_read_live_memory (object, memaddr,
1358                                                   readbuf, len);
1359                 }
1360               else if (memaddr >= p->endaddr)
1361                 {
1362                   /* This section ends before the transfer starts.  */
1363                   continue;
1364                 }
1365               else
1366                 {
1367                   /* This section overlaps the transfer.  Just do half.  */
1368                   len = p->endaddr - memaddr;
1369                   return target_read_live_memory (object, memaddr,
1370                                                   readbuf, len);
1371                 }
1372             }
1373         }
1374     }
1375
1376   return 0;
1377 }
1378
1379 /* Perform a partial memory transfer.
1380    For docs see target.h, to_xfer_partial.  */
1381
1382 static LONGEST
1383 memory_xfer_partial (struct target_ops *ops, enum target_object object,
1384                      void *readbuf, const void *writebuf, ULONGEST memaddr,
1385                      LONGEST len)
1386 {
1387   LONGEST res;
1388   int reg_len;
1389   struct mem_region *region;
1390   struct inferior *inf;
1391
1392   /* Zero length requests are ok and require no work.  */
1393   if (len == 0)
1394     return 0;
1395
1396   /* For accesses to unmapped overlay sections, read directly from
1397      files.  Must do this first, as MEMADDR may need adjustment.  */
1398   if (readbuf != NULL && overlay_debugging)
1399     {
1400       struct obj_section *section = find_pc_overlay (memaddr);
1401
1402       if (pc_in_unmapped_range (memaddr, section))
1403         {
1404           struct target_section_table *table
1405             = target_get_section_table (ops);
1406           const char *section_name = section->the_bfd_section->name;
1407
1408           memaddr = overlay_mapped_address (memaddr, section);
1409           return section_table_xfer_memory_partial (readbuf, writebuf,
1410                                                     memaddr, len,
1411                                                     table->sections,
1412                                                     table->sections_end,
1413                                                     section_name);
1414         }
1415     }
1416
1417   /* Try the executable files, if "trust-readonly-sections" is set.  */
1418   if (readbuf != NULL && trust_readonly)
1419     {
1420       struct target_section *secp;
1421       struct target_section_table *table;
1422
1423       secp = target_section_by_addr (ops, memaddr);
1424       if (secp != NULL
1425           && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1426               & SEC_READONLY))
1427         {
1428           table = target_get_section_table (ops);
1429           return section_table_xfer_memory_partial (readbuf, writebuf,
1430                                                     memaddr, len,
1431                                                     table->sections,
1432                                                     table->sections_end,
1433                                                     NULL);
1434         }
1435     }
1436
1437   /* If reading unavailable memory in the context of traceframes, and
1438      this address falls within a read-only section, fallback to
1439      reading from live memory.  */
1440   if (readbuf != NULL && get_traceframe_number () != -1)
1441     {
1442       VEC(mem_range_s) *available;
1443
1444       /* If we fail to get the set of available memory, then the
1445          target does not support querying traceframe info, and so we
1446          attempt reading from the traceframe anyway (assuming the
1447          target implements the old QTro packet then).  */
1448       if (traceframe_available_memory (&available, memaddr, len))
1449         {
1450           struct cleanup *old_chain;
1451
1452           old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
1453
1454           if (VEC_empty (mem_range_s, available)
1455               || VEC_index (mem_range_s, available, 0)->start != memaddr)
1456             {
1457               /* Don't read into the traceframe's available
1458                  memory.  */
1459               if (!VEC_empty (mem_range_s, available))
1460                 {
1461                   LONGEST oldlen = len;
1462
1463                   len = VEC_index (mem_range_s, available, 0)->start - memaddr;
1464                   gdb_assert (len <= oldlen);
1465                 }
1466
1467               do_cleanups (old_chain);
1468
1469               /* This goes through the topmost target again.  */
1470               res = memory_xfer_live_readonly_partial (ops, object,
1471                                                        readbuf, memaddr, len);
1472               if (res > 0)
1473                 return res;
1474
1475               /* No use trying further, we know some memory starting
1476                  at MEMADDR isn't available.  */
1477               return -1;
1478             }
1479
1480           /* Don't try to read more than how much is available, in
1481              case the target implements the deprecated QTro packet to
1482              cater for older GDBs (the target's knowledge of read-only
1483              sections may be outdated by now).  */
1484           len = VEC_index (mem_range_s, available, 0)->length;
1485
1486           do_cleanups (old_chain);
1487         }
1488     }
1489
1490   /* Try GDB's internal data cache.  */
1491   region = lookup_mem_region (memaddr);
1492   /* region->hi == 0 means there's no upper bound.  */
1493   if (memaddr + len < region->hi || region->hi == 0)
1494     reg_len = len;
1495   else
1496     reg_len = region->hi - memaddr;
1497
1498   switch (region->attrib.mode)
1499     {
1500     case MEM_RO:
1501       if (writebuf != NULL)
1502         return -1;
1503       break;
1504
1505     case MEM_WO:
1506       if (readbuf != NULL)
1507         return -1;
1508       break;
1509
1510     case MEM_FLASH:
1511       /* We only support writing to flash during "load" for now.  */
1512       if (writebuf != NULL)
1513         error (_("Writing to flash memory forbidden in this context"));
1514       break;
1515
1516     case MEM_NONE:
1517       return -1;
1518     }
1519
1520   if (!ptid_equal (inferior_ptid, null_ptid))
1521     inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1522   else
1523     inf = NULL;
1524
1525   if (inf != NULL
1526       /* The dcache reads whole cache lines; that doesn't play well
1527          with reading from a trace buffer, because reading outside of
1528          the collected memory range fails.  */
1529       && get_traceframe_number () == -1
1530       && (region->attrib.cache
1531           || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
1532     {
1533       if (readbuf != NULL)
1534         res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
1535                                   reg_len, 0);
1536       else
1537         /* FIXME drow/2006-08-09: If we're going to preserve const
1538            correctness dcache_xfer_memory should take readbuf and
1539            writebuf.  */
1540         res = dcache_xfer_memory (ops, target_dcache, memaddr,
1541                                   (void *) writebuf,
1542                                   reg_len, 1);
1543       if (res <= 0)
1544         return -1;
1545       else
1546         {
1547           if (readbuf && !show_memory_breakpoints)
1548             breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1549           return res;
1550         }
1551     }
1552
1553   /* If none of those methods found the memory we wanted, fall back
1554      to a target partial transfer.  Normally a single call to
1555      to_xfer_partial is enough; if it doesn't recognize an object
1556      it will call the to_xfer_partial of the next target down.
1557      But for memory this won't do.  Memory is the only target
1558      object which can be read from more than one valid target.
1559      A core file, for instance, could have some of memory but
1560      delegate other bits to the target below it.  So, we must
1561      manually try all targets.  */
1562
1563   do
1564     {
1565       res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1566                                   readbuf, writebuf, memaddr, reg_len);
1567       if (res > 0)
1568         break;
1569
1570       /* We want to continue past core files to executables, but not
1571          past a running target's memory.  */
1572       if (ops->to_has_all_memory (ops))
1573         break;
1574
1575       ops = ops->beneath;
1576     }
1577   while (ops != NULL);
1578
1579   if (res > 0 && readbuf != NULL && !show_memory_breakpoints)
1580     breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1581
1582   /* Make sure the cache gets updated no matter what - if we are writing
1583      to the stack.  Even if this write is not tagged as such, we still need
1584      to update the cache.  */
1585
1586   if (res > 0
1587       && inf != NULL
1588       && writebuf != NULL
1589       && !region->attrib.cache
1590       && stack_cache_enabled_p
1591       && object != TARGET_OBJECT_STACK_MEMORY)
1592     {
1593       dcache_update (target_dcache, memaddr, (void *) writebuf, res);
1594     }
1595
1596   /* If we still haven't got anything, return the last error.  We
1597      give up.  */
1598   return res;
1599 }
1600
1601 static void
1602 restore_show_memory_breakpoints (void *arg)
1603 {
1604   show_memory_breakpoints = (uintptr_t) arg;
1605 }
1606
1607 struct cleanup *
1608 make_show_memory_breakpoints_cleanup (int show)
1609 {
1610   int current = show_memory_breakpoints;
1611
1612   show_memory_breakpoints = show;
1613   return make_cleanup (restore_show_memory_breakpoints,
1614                        (void *) (uintptr_t) current);
1615 }
1616
1617 /* For docs see target.h, to_xfer_partial.  */
1618
1619 static LONGEST
1620 target_xfer_partial (struct target_ops *ops,
1621                      enum target_object object, const char *annex,
1622                      void *readbuf, const void *writebuf,
1623                      ULONGEST offset, LONGEST len)
1624 {
1625   LONGEST retval;
1626
1627   gdb_assert (ops->to_xfer_partial != NULL);
1628
1629   if (writebuf && !may_write_memory)
1630     error (_("Writing to memory is not allowed (addr %s, len %s)"),
1631            core_addr_to_string_nz (offset), plongest (len));
1632
1633   /* If this is a memory transfer, let the memory-specific code
1634      have a look at it instead.  Memory transfers are more
1635      complicated.  */
1636   if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1637     retval = memory_xfer_partial (ops, object, readbuf,
1638                                   writebuf, offset, len);
1639   else
1640     {
1641       enum target_object raw_object = object;
1642
1643       /* If this is a raw memory transfer, request the normal
1644          memory object from other layers.  */
1645       if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1646         raw_object = TARGET_OBJECT_MEMORY;
1647
1648       retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1649                                      writebuf, offset, len);
1650     }
1651
1652   if (targetdebug)
1653     {
1654       const unsigned char *myaddr = NULL;
1655
1656       fprintf_unfiltered (gdb_stdlog,
1657                           "%s:target_xfer_partial "
1658                           "(%d, %s, %s, %s, %s, %s) = %s",
1659                           ops->to_shortname,
1660                           (int) object,
1661                           (annex ? annex : "(null)"),
1662                           host_address_to_string (readbuf),
1663                           host_address_to_string (writebuf),
1664                           core_addr_to_string_nz (offset),
1665                           plongest (len), plongest (retval));
1666
1667       if (readbuf)
1668         myaddr = readbuf;
1669       if (writebuf)
1670         myaddr = writebuf;
1671       if (retval > 0 && myaddr != NULL)
1672         {
1673           int i;
1674
1675           fputs_unfiltered (", bytes =", gdb_stdlog);
1676           for (i = 0; i < retval; i++)
1677             {
1678               if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1679                 {
1680                   if (targetdebug < 2 && i > 0)
1681                     {
1682                       fprintf_unfiltered (gdb_stdlog, " ...");
1683                       break;
1684                     }
1685                   fprintf_unfiltered (gdb_stdlog, "\n");
1686                 }
1687
1688               fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1689             }
1690         }
1691
1692       fputc_unfiltered ('\n', gdb_stdlog);
1693     }
1694   return retval;
1695 }
1696
1697 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1698    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
1699    if any error occurs.
1700
1701    If an error occurs, no guarantee is made about the contents of the data at
1702    MYADDR.  In particular, the caller should not depend upon partial reads
1703    filling the buffer with good data.  There is no way for the caller to know
1704    how much good data might have been transfered anyway.  Callers that can
1705    deal with partial reads should call target_read (which will retry until
1706    it makes no progress, and then return how much was transferred).  */
1707
1708 int
1709 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1710 {
1711   /* Dispatch to the topmost target, not the flattened current_target.
1712      Memory accesses check target->to_has_(all_)memory, and the
1713      flattened target doesn't inherit those.  */
1714   if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1715                    myaddr, memaddr, len) == len)
1716     return 0;
1717   else
1718     return EIO;
1719 }
1720
1721 /* Like target_read_memory, but specify explicitly that this is a read from
1722    the target's stack.  This may trigger different cache behavior.  */
1723
1724 int
1725 target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1726 {
1727   /* Dispatch to the topmost target, not the flattened current_target.
1728      Memory accesses check target->to_has_(all_)memory, and the
1729      flattened target doesn't inherit those.  */
1730
1731   if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1732                    myaddr, memaddr, len) == len)
1733     return 0;
1734   else
1735     return EIO;
1736 }
1737
1738 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1739    Returns either 0 for success or an errno value if any error occurs.
1740    If an error occurs, no guarantee is made about how much data got written.
1741    Callers that can deal with partial writes should call target_write.  */
1742
1743 int
1744 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1745 {
1746   /* Dispatch to the topmost target, not the flattened current_target.
1747      Memory accesses check target->to_has_(all_)memory, and the
1748      flattened target doesn't inherit those.  */
1749   if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1750                     myaddr, memaddr, len) == len)
1751     return 0;
1752   else
1753     return EIO;
1754 }
1755
1756 /* Fetch the target's memory map.  */
1757
1758 VEC(mem_region_s) *
1759 target_memory_map (void)
1760 {
1761   VEC(mem_region_s) *result;
1762   struct mem_region *last_one, *this_one;
1763   int ix;
1764   struct target_ops *t;
1765
1766   if (targetdebug)
1767     fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1768
1769   for (t = current_target.beneath; t != NULL; t = t->beneath)
1770     if (t->to_memory_map != NULL)
1771       break;
1772
1773   if (t == NULL)
1774     return NULL;
1775
1776   result = t->to_memory_map (t);
1777   if (result == NULL)
1778     return NULL;
1779
1780   qsort (VEC_address (mem_region_s, result),
1781          VEC_length (mem_region_s, result),
1782          sizeof (struct mem_region), mem_region_cmp);
1783
1784   /* Check that regions do not overlap.  Simultaneously assign
1785      a numbering for the "mem" commands to use to refer to
1786      each region.  */
1787   last_one = NULL;
1788   for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1789     {
1790       this_one->number = ix;
1791
1792       if (last_one && last_one->hi > this_one->lo)
1793         {
1794           warning (_("Overlapping regions in memory map: ignoring"));
1795           VEC_free (mem_region_s, result);
1796           return NULL;
1797         }
1798       last_one = this_one;
1799     }
1800
1801   return result;
1802 }
1803
1804 void
1805 target_flash_erase (ULONGEST address, LONGEST length)
1806 {
1807   struct target_ops *t;
1808
1809   for (t = current_target.beneath; t != NULL; t = t->beneath)
1810     if (t->to_flash_erase != NULL)
1811       {
1812         if (targetdebug)
1813           fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1814                               hex_string (address), phex (length, 0));
1815         t->to_flash_erase (t, address, length);
1816         return;
1817       }
1818
1819   tcomplain ();
1820 }
1821
1822 void
1823 target_flash_done (void)
1824 {
1825   struct target_ops *t;
1826
1827   for (t = current_target.beneath; t != NULL; t = t->beneath)
1828     if (t->to_flash_done != NULL)
1829       {
1830         if (targetdebug)
1831           fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1832         t->to_flash_done (t);
1833         return;
1834       }
1835
1836   tcomplain ();
1837 }
1838
1839 static void
1840 show_trust_readonly (struct ui_file *file, int from_tty,
1841                      struct cmd_list_element *c, const char *value)
1842 {
1843   fprintf_filtered (file,
1844                     _("Mode for reading from readonly sections is %s.\n"),
1845                     value);
1846 }
1847
1848 /* More generic transfers.  */
1849
1850 static LONGEST
1851 default_xfer_partial (struct target_ops *ops, enum target_object object,
1852                       const char *annex, gdb_byte *readbuf,
1853                       const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1854 {
1855   if (object == TARGET_OBJECT_MEMORY
1856       && ops->deprecated_xfer_memory != NULL)
1857     /* If available, fall back to the target's
1858        "deprecated_xfer_memory" method.  */
1859     {
1860       int xfered = -1;
1861
1862       errno = 0;
1863       if (writebuf != NULL)
1864         {
1865           void *buffer = xmalloc (len);
1866           struct cleanup *cleanup = make_cleanup (xfree, buffer);
1867
1868           memcpy (buffer, writebuf, len);
1869           xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1870                                                 1/*write*/, NULL, ops);
1871           do_cleanups (cleanup);
1872         }
1873       if (readbuf != NULL)
1874         xfered = ops->deprecated_xfer_memory (offset, readbuf, len, 
1875                                               0/*read*/, NULL, ops);
1876       if (xfered > 0)
1877         return xfered;
1878       else if (xfered == 0 && errno == 0)
1879         /* "deprecated_xfer_memory" uses 0, cross checked against
1880            ERRNO as one indication of an error.  */
1881         return 0;
1882       else
1883         return -1;
1884     }
1885   else if (ops->beneath != NULL)
1886     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1887                                           readbuf, writebuf, offset, len);
1888   else
1889     return -1;
1890 }
1891
1892 /* The xfer_partial handler for the topmost target.  Unlike the default,
1893    it does not need to handle memory specially; it just passes all
1894    requests down the stack.  */
1895
1896 static LONGEST
1897 current_xfer_partial (struct target_ops *ops, enum target_object object,
1898                       const char *annex, gdb_byte *readbuf,
1899                       const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1900 {
1901   if (ops->beneath != NULL)
1902     return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1903                                           readbuf, writebuf, offset, len);
1904   else
1905     return -1;
1906 }
1907
1908 /* Target vector read/write partial wrapper functions.  */
1909
1910 static LONGEST
1911 target_read_partial (struct target_ops *ops,
1912                      enum target_object object,
1913                      const char *annex, gdb_byte *buf,
1914                      ULONGEST offset, LONGEST len)
1915 {
1916   return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1917 }
1918
1919 static LONGEST
1920 target_write_partial (struct target_ops *ops,
1921                       enum target_object object,
1922                       const char *annex, const gdb_byte *buf,
1923                       ULONGEST offset, LONGEST len)
1924 {
1925   return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1926 }
1927
1928 /* Wrappers to perform the full transfer.  */
1929
1930 /* For docs on target_read see target.h.  */
1931
1932 LONGEST
1933 target_read (struct target_ops *ops,
1934              enum target_object object,
1935              const char *annex, gdb_byte *buf,
1936              ULONGEST offset, LONGEST len)
1937 {
1938   LONGEST xfered = 0;
1939
1940   while (xfered < len)
1941     {
1942       LONGEST xfer = target_read_partial (ops, object, annex,
1943                                           (gdb_byte *) buf + xfered,
1944                                           offset + xfered, len - xfered);
1945
1946       /* Call an observer, notifying them of the xfer progress?  */
1947       if (xfer == 0)
1948         return xfered;
1949       if (xfer < 0)
1950         return -1;
1951       xfered += xfer;
1952       QUIT;
1953     }
1954   return len;
1955 }
1956
1957 /* Assuming that the entire [begin, end) range of memory cannot be
1958    read, try to read whatever subrange is possible to read.
1959
1960    The function returns, in RESULT, either zero or one memory block.
1961    If there's a readable subrange at the beginning, it is completely
1962    read and returned.  Any further readable subrange will not be read.
1963    Otherwise, if there's a readable subrange at the end, it will be
1964    completely read and returned.  Any readable subranges before it
1965    (obviously, not starting at the beginning), will be ignored.  In
1966    other cases -- either no readable subrange, or readable subrange(s)
1967    that is neither at the beginning, or end, nothing is returned.
1968
1969    The purpose of this function is to handle a read across a boundary
1970    of accessible memory in a case when memory map is not available.
1971    The above restrictions are fine for this case, but will give
1972    incorrect results if the memory is 'patchy'.  However, supporting
1973    'patchy' memory would require trying to read every single byte,
1974    and it seems unacceptable solution.  Explicit memory map is
1975    recommended for this case -- and target_read_memory_robust will
1976    take care of reading multiple ranges then.  */
1977
1978 static void
1979 read_whatever_is_readable (struct target_ops *ops,
1980                            ULONGEST begin, ULONGEST end,
1981                            VEC(memory_read_result_s) **result)
1982 {
1983   gdb_byte *buf = xmalloc (end - begin);
1984   ULONGEST current_begin = begin;
1985   ULONGEST current_end = end;
1986   int forward;
1987   memory_read_result_s r;
1988
1989   /* If we previously failed to read 1 byte, nothing can be done here.  */
1990   if (end - begin <= 1)
1991     {
1992       xfree (buf);
1993       return;
1994     }
1995
1996   /* Check that either first or the last byte is readable, and give up
1997      if not.  This heuristic is meant to permit reading accessible memory
1998      at the boundary of accessible region.  */
1999   if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2000                            buf, begin, 1) == 1)
2001     {
2002       forward = 1;
2003       ++current_begin;
2004     }
2005   else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2006                                 buf + (end-begin) - 1, end - 1, 1) == 1)
2007     {
2008       forward = 0;
2009       --current_end;
2010     }
2011   else
2012     {
2013       xfree (buf);
2014       return;
2015     }
2016
2017   /* Loop invariant is that the [current_begin, current_end) was previously
2018      found to be not readable as a whole.
2019
2020      Note loop condition -- if the range has 1 byte, we can't divide the range
2021      so there's no point trying further.  */
2022   while (current_end - current_begin > 1)
2023     {
2024       ULONGEST first_half_begin, first_half_end;
2025       ULONGEST second_half_begin, second_half_end;
2026       LONGEST xfer;
2027       ULONGEST middle = current_begin + (current_end - current_begin)/2;
2028
2029       if (forward)
2030         {
2031           first_half_begin = current_begin;
2032           first_half_end = middle;
2033           second_half_begin = middle;
2034           second_half_end = current_end;
2035         }
2036       else
2037         {
2038           first_half_begin = middle;
2039           first_half_end = current_end;
2040           second_half_begin = current_begin;
2041           second_half_end = middle;
2042         }
2043
2044       xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2045                           buf + (first_half_begin - begin),
2046                           first_half_begin,
2047                           first_half_end - first_half_begin);
2048
2049       if (xfer == first_half_end - first_half_begin)
2050         {
2051           /* This half reads up fine.  So, the error must be in the
2052              other half.  */
2053           current_begin = second_half_begin;
2054           current_end = second_half_end;
2055         }
2056       else
2057         {
2058           /* This half is not readable.  Because we've tried one byte, we
2059              know some part of this half if actually redable.  Go to the next
2060              iteration to divide again and try to read.
2061
2062              We don't handle the other half, because this function only tries
2063              to read a single readable subrange.  */
2064           current_begin = first_half_begin;
2065           current_end = first_half_end;
2066         }
2067     }
2068
2069   if (forward)
2070     {
2071       /* The [begin, current_begin) range has been read.  */
2072       r.begin = begin;
2073       r.end = current_begin;
2074       r.data = buf;
2075     }
2076   else
2077     {
2078       /* The [current_end, end) range has been read.  */
2079       LONGEST rlen = end - current_end;
2080
2081       r.data = xmalloc (rlen);
2082       memcpy (r.data, buf + current_end - begin, rlen);
2083       r.begin = current_end;
2084       r.end = end;
2085       xfree (buf);
2086     }
2087   VEC_safe_push(memory_read_result_s, (*result), &r);
2088 }
2089
2090 void
2091 free_memory_read_result_vector (void *x)
2092 {
2093   VEC(memory_read_result_s) *v = x;
2094   memory_read_result_s *current;
2095   int ix;
2096
2097   for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
2098     {
2099       xfree (current->data);
2100     }
2101   VEC_free (memory_read_result_s, v);
2102 }
2103
2104 VEC(memory_read_result_s) *
2105 read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
2106 {
2107   VEC(memory_read_result_s) *result = 0;
2108
2109   LONGEST xfered = 0;
2110   while (xfered < len)
2111     {
2112       struct mem_region *region = lookup_mem_region (offset + xfered);
2113       LONGEST rlen;
2114
2115       /* If there is no explicit region, a fake one should be created.  */
2116       gdb_assert (region);
2117
2118       if (region->hi == 0)
2119         rlen = len - xfered;
2120       else
2121         rlen = region->hi - offset;
2122
2123       if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
2124         {
2125           /* Cannot read this region.  Note that we can end up here only
2126              if the region is explicitly marked inaccessible, or
2127              'inaccessible-by-default' is in effect.  */
2128           xfered += rlen;
2129         }
2130       else
2131         {
2132           LONGEST to_read = min (len - xfered, rlen);
2133           gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
2134
2135           LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2136                                       (gdb_byte *) buffer,
2137                                       offset + xfered, to_read);
2138           /* Call an observer, notifying them of the xfer progress?  */
2139           if (xfer <= 0)
2140             {
2141               /* Got an error reading full chunk.  See if maybe we can read
2142                  some subrange.  */
2143               xfree (buffer);
2144               read_whatever_is_readable (ops, offset + xfered,
2145                                          offset + xfered + to_read, &result);
2146               xfered += to_read;
2147             }
2148           else
2149             {
2150               struct memory_read_result r;
2151               r.data = buffer;
2152               r.begin = offset + xfered;
2153               r.end = r.begin + xfer;
2154               VEC_safe_push (memory_read_result_s, result, &r);
2155               xfered += xfer;
2156             }
2157           QUIT;
2158         }
2159     }
2160   return result;
2161 }
2162
2163
2164 /* An alternative to target_write with progress callbacks.  */
2165
2166 LONGEST
2167 target_write_with_progress (struct target_ops *ops,
2168                             enum target_object object,
2169                             const char *annex, const gdb_byte *buf,
2170                             ULONGEST offset, LONGEST len,
2171                             void (*progress) (ULONGEST, void *), void *baton)
2172 {
2173   LONGEST xfered = 0;
2174
2175   /* Give the progress callback a chance to set up.  */
2176   if (progress)
2177     (*progress) (0, baton);
2178
2179   while (xfered < len)
2180     {
2181       LONGEST xfer = target_write_partial (ops, object, annex,
2182                                            (gdb_byte *) buf + xfered,
2183                                            offset + xfered, len - xfered);
2184
2185       if (xfer == 0)
2186         return xfered;
2187       if (xfer < 0)
2188         return -1;
2189
2190       if (progress)
2191         (*progress) (xfer, baton);
2192
2193       xfered += xfer;
2194       QUIT;
2195     }
2196   return len;
2197 }
2198
2199 /* For docs on target_write see target.h.  */
2200
2201 LONGEST
2202 target_write (struct target_ops *ops,
2203               enum target_object object,
2204               const char *annex, const gdb_byte *buf,
2205               ULONGEST offset, LONGEST len)
2206 {
2207   return target_write_with_progress (ops, object, annex, buf, offset, len,
2208                                      NULL, NULL);
2209 }
2210
2211 /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
2212    the size of the transferred data.  PADDING additional bytes are
2213    available in *BUF_P.  This is a helper function for
2214    target_read_alloc; see the declaration of that function for more
2215    information.  */
2216
2217 static LONGEST
2218 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
2219                      const char *annex, gdb_byte **buf_p, int padding)
2220 {
2221   size_t buf_alloc, buf_pos;
2222   gdb_byte *buf;
2223   LONGEST n;
2224
2225   /* This function does not have a length parameter; it reads the
2226      entire OBJECT).  Also, it doesn't support objects fetched partly
2227      from one target and partly from another (in a different stratum,
2228      e.g. a core file and an executable).  Both reasons make it
2229      unsuitable for reading memory.  */
2230   gdb_assert (object != TARGET_OBJECT_MEMORY);
2231
2232   /* Start by reading up to 4K at a time.  The target will throttle
2233      this number down if necessary.  */
2234   buf_alloc = 4096;
2235   buf = xmalloc (buf_alloc);
2236   buf_pos = 0;
2237   while (1)
2238     {
2239       n = target_read_partial (ops, object, annex, &buf[buf_pos],
2240                                buf_pos, buf_alloc - buf_pos - padding);
2241       if (n < 0)
2242         {
2243           /* An error occurred.  */
2244           xfree (buf);
2245           return -1;
2246         }
2247       else if (n == 0)
2248         {
2249           /* Read all there was.  */
2250           if (buf_pos == 0)
2251             xfree (buf);
2252           else
2253             *buf_p = buf;
2254           return buf_pos;
2255         }
2256
2257       buf_pos += n;
2258
2259       /* If the buffer is filling up, expand it.  */
2260       if (buf_alloc < buf_pos * 2)
2261         {
2262           buf_alloc *= 2;
2263           buf = xrealloc (buf, buf_alloc);
2264         }
2265
2266       QUIT;
2267     }
2268 }
2269
2270 /* Read OBJECT/ANNEX using OPS.  Store the result in *BUF_P and return
2271    the size of the transferred data.  See the declaration in "target.h"
2272    function for more information about the return value.  */
2273
2274 LONGEST
2275 target_read_alloc (struct target_ops *ops, enum target_object object,
2276                    const char *annex, gdb_byte **buf_p)
2277 {
2278   return target_read_alloc_1 (ops, object, annex, buf_p, 0);
2279 }
2280
2281 /* Read OBJECT/ANNEX using OPS.  The result is NUL-terminated and
2282    returned as a string, allocated using xmalloc.  If an error occurs
2283    or the transfer is unsupported, NULL is returned.  Empty objects
2284    are returned as allocated but empty strings.  A warning is issued
2285    if the result contains any embedded NUL bytes.  */
2286
2287 char *
2288 target_read_stralloc (struct target_ops *ops, enum target_object object,
2289                       const char *annex)
2290 {
2291   gdb_byte *buffer;
2292   LONGEST transferred;
2293
2294   transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
2295
2296   if (transferred < 0)
2297     return NULL;
2298
2299   if (transferred == 0)
2300     return xstrdup ("");
2301
2302   buffer[transferred] = 0;
2303   if (strlen (buffer) < transferred)
2304     warning (_("target object %d, annex %s, "
2305                "contained unexpected null characters"),
2306              (int) object, annex ? annex : "(none)");
2307
2308   return (char *) buffer;
2309 }
2310
2311 /* Memory transfer methods.  */
2312
2313 void
2314 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
2315                    LONGEST len)
2316 {
2317   /* This method is used to read from an alternate, non-current
2318      target.  This read must bypass the overlay support (as symbols
2319      don't match this target), and GDB's internal cache (wrong cache
2320      for this target).  */
2321   if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
2322       != len)
2323     memory_error (EIO, addr);
2324 }
2325
2326 ULONGEST
2327 get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2328                             int len, enum bfd_endian byte_order)
2329 {
2330   gdb_byte buf[sizeof (ULONGEST)];
2331
2332   gdb_assert (len <= sizeof (buf));
2333   get_target_memory (ops, addr, buf, len);
2334   return extract_unsigned_integer (buf, len, byte_order);
2335 }
2336
2337 int
2338 target_insert_breakpoint (struct gdbarch *gdbarch,
2339                           struct bp_target_info *bp_tgt)
2340 {
2341   if (!may_insert_breakpoints)
2342     {
2343       warning (_("May not insert breakpoints"));
2344       return 1;
2345     }
2346
2347   return (*current_target.to_insert_breakpoint) (gdbarch, bp_tgt);
2348 }
2349
2350 int
2351 target_remove_breakpoint (struct gdbarch *gdbarch,
2352                           struct bp_target_info *bp_tgt)
2353 {
2354   /* This is kind of a weird case to handle, but the permission might
2355      have been changed after breakpoints were inserted - in which case
2356      we should just take the user literally and assume that any
2357      breakpoints should be left in place.  */
2358   if (!may_insert_breakpoints)
2359     {
2360       warning (_("May not remove breakpoints"));
2361       return 1;
2362     }
2363
2364   return (*current_target.to_remove_breakpoint) (gdbarch, bp_tgt);
2365 }
2366
2367 static void
2368 target_info (char *args, int from_tty)
2369 {
2370   struct target_ops *t;
2371   int has_all_mem = 0;
2372
2373   if (symfile_objfile != NULL)
2374     printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
2375
2376   for (t = target_stack; t != NULL; t = t->beneath)
2377     {
2378       if (!(*t->to_has_memory) (t))
2379         continue;
2380
2381       if ((int) (t->to_stratum) <= (int) dummy_stratum)
2382         continue;
2383       if (has_all_mem)
2384         printf_unfiltered (_("\tWhile running this, "
2385                              "GDB does not access memory from...\n"));
2386       printf_unfiltered ("%s:\n", t->to_longname);
2387       (t->to_files_info) (t);
2388       has_all_mem = (*t->to_has_all_memory) (t);
2389     }
2390 }
2391
2392 /* This function is called before any new inferior is created, e.g.
2393    by running a program, attaching, or connecting to a target.
2394    It cleans up any state from previous invocations which might
2395    change between runs.  This is a subset of what target_preopen
2396    resets (things which might change between targets).  */
2397
2398 void
2399 target_pre_inferior (int from_tty)
2400 {
2401   /* Clear out solib state.  Otherwise the solib state of the previous
2402      inferior might have survived and is entirely wrong for the new
2403      target.  This has been observed on GNU/Linux using glibc 2.3.  How
2404      to reproduce:
2405
2406      bash$ ./foo&
2407      [1] 4711
2408      bash$ ./foo&
2409      [1] 4712
2410      bash$ gdb ./foo
2411      [...]
2412      (gdb) attach 4711
2413      (gdb) detach
2414      (gdb) attach 4712
2415      Cannot access memory at address 0xdeadbeef
2416   */
2417
2418   /* In some OSs, the shared library list is the same/global/shared
2419      across inferiors.  If code is shared between processes, so are
2420      memory regions and features.  */
2421   if (!gdbarch_has_global_solist (target_gdbarch))
2422     {
2423       no_shared_libraries (NULL, from_tty);
2424
2425       invalidate_target_mem_regions ();
2426
2427       target_clear_description ();
2428     }
2429 }
2430
2431 /* Callback for iterate_over_inferiors.  Gets rid of the given
2432    inferior.  */
2433
2434 static int
2435 dispose_inferior (struct inferior *inf, void *args)
2436 {
2437   struct thread_info *thread;
2438
2439   thread = any_thread_of_process (inf->pid);
2440   if (thread)
2441     {
2442       switch_to_thread (thread->ptid);
2443
2444       /* Core inferiors actually should be detached, not killed.  */
2445       if (target_has_execution)
2446         target_kill ();
2447       else
2448         target_detach (NULL, 0);
2449     }
2450
2451   return 0;
2452 }
2453
2454 /* This is to be called by the open routine before it does
2455    anything.  */
2456
2457 void
2458 target_preopen (int from_tty)
2459 {
2460   dont_repeat ();
2461
2462   if (have_inferiors ())
2463     {
2464       if (!from_tty
2465           || !have_live_inferiors ()
2466           || query (_("A program is being debugged already.  Kill it? ")))
2467         iterate_over_inferiors (dispose_inferior, NULL);
2468       else
2469         error (_("Program not killed."));
2470     }
2471
2472   /* Calling target_kill may remove the target from the stack.  But if
2473      it doesn't (which seems like a win for UDI), remove it now.  */
2474   /* Leave the exec target, though.  The user may be switching from a
2475      live process to a core of the same program.  */
2476   pop_all_targets_above (file_stratum, 0);
2477
2478   target_pre_inferior (from_tty);
2479 }
2480
2481 /* Detach a target after doing deferred register stores.  */
2482
2483 void
2484 target_detach (char *args, int from_tty)
2485 {
2486   struct target_ops* t;
2487   
2488   if (gdbarch_has_global_breakpoints (target_gdbarch))
2489     /* Don't remove global breakpoints here.  They're removed on
2490        disconnection from the target.  */
2491     ;
2492   else
2493     /* If we're in breakpoints-always-inserted mode, have to remove
2494        them before detaching.  */
2495     remove_breakpoints_pid (PIDGET (inferior_ptid));
2496
2497   prepare_for_detach ();
2498
2499   for (t = current_target.beneath; t != NULL; t = t->beneath)
2500     {
2501       if (t->to_detach != NULL)
2502         {
2503           t->to_detach (t, args, from_tty);
2504           if (targetdebug)
2505             fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2506                                 args, from_tty);
2507           return;
2508         }
2509     }
2510
2511   internal_error (__FILE__, __LINE__, _("could not find a target to detach"));
2512 }
2513
2514 void
2515 target_disconnect (char *args, int from_tty)
2516 {
2517   struct target_ops *t;
2518
2519   /* If we're in breakpoints-always-inserted mode or if breakpoints
2520      are global across processes, we have to remove them before
2521      disconnecting.  */
2522   remove_breakpoints ();
2523
2524   for (t = current_target.beneath; t != NULL; t = t->beneath)
2525     if (t->to_disconnect != NULL)
2526         {
2527           if (targetdebug)
2528             fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2529                                 args, from_tty);
2530           t->to_disconnect (t, args, from_tty);
2531           return;
2532         }
2533
2534   tcomplain ();
2535 }
2536
2537 ptid_t
2538 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
2539 {
2540   struct target_ops *t;
2541
2542   for (t = current_target.beneath; t != NULL; t = t->beneath)
2543     {
2544       if (t->to_wait != NULL)
2545         {
2546           ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2547
2548           if (targetdebug)
2549             {
2550               char *status_string;
2551
2552               status_string = target_waitstatus_to_string (status);
2553               fprintf_unfiltered (gdb_stdlog,
2554                                   "target_wait (%d, status) = %d,   %s\n",
2555                                   PIDGET (ptid), PIDGET (retval),
2556                                   status_string);
2557               xfree (status_string);
2558             }
2559
2560           return retval;
2561         }
2562     }
2563
2564   noprocess ();
2565 }
2566
2567 char *
2568 target_pid_to_str (ptid_t ptid)
2569 {
2570   struct target_ops *t;
2571
2572   for (t = current_target.beneath; t != NULL; t = t->beneath)
2573     {
2574       if (t->to_pid_to_str != NULL)
2575         return (*t->to_pid_to_str) (t, ptid);
2576     }
2577
2578   return normal_pid_to_str (ptid);
2579 }
2580
2581 char *
2582 target_thread_name (struct thread_info *info)
2583 {
2584   struct target_ops *t;
2585
2586   for (t = current_target.beneath; t != NULL; t = t->beneath)
2587     {
2588       if (t->to_thread_name != NULL)
2589         return (*t->to_thread_name) (info);
2590     }
2591
2592   return NULL;
2593 }
2594
2595 void
2596 target_resume (ptid_t ptid, int step, enum target_signal signal)
2597 {
2598   struct target_ops *t;
2599
2600   target_dcache_invalidate ();
2601
2602   for (t = current_target.beneath; t != NULL; t = t->beneath)
2603     {
2604       if (t->to_resume != NULL)
2605         {
2606           t->to_resume (t, ptid, step, signal);
2607           if (targetdebug)
2608             fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2609                                 PIDGET (ptid),
2610                                 step ? "step" : "continue",
2611                                 target_signal_to_name (signal));
2612
2613           registers_changed_ptid (ptid);
2614           set_executing (ptid, 1);
2615           set_running (ptid, 1);
2616           clear_inline_frame_state (ptid);
2617           return;
2618         }
2619     }
2620
2621   noprocess ();
2622 }
2623
2624 void
2625 target_pass_signals (int numsigs, unsigned char *pass_signals)
2626 {
2627   struct target_ops *t;
2628
2629   for (t = current_target.beneath; t != NULL; t = t->beneath)
2630     {
2631       if (t->to_pass_signals != NULL)
2632         {
2633           if (targetdebug)
2634             {
2635               int i;
2636
2637               fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2638                                   numsigs);
2639
2640               for (i = 0; i < numsigs; i++)
2641                 if (pass_signals[i])
2642                   fprintf_unfiltered (gdb_stdlog, " %s",
2643                                       target_signal_to_name (i));
2644
2645               fprintf_unfiltered (gdb_stdlog, " })\n");
2646             }
2647
2648           (*t->to_pass_signals) (numsigs, pass_signals);
2649           return;
2650         }
2651     }
2652 }
2653
2654 /* Look through the list of possible targets for a target that can
2655    follow forks.  */
2656
2657 int
2658 target_follow_fork (int follow_child)
2659 {
2660   struct target_ops *t;
2661
2662   for (t = current_target.beneath; t != NULL; t = t->beneath)
2663     {
2664       if (t->to_follow_fork != NULL)
2665         {
2666           int retval = t->to_follow_fork (t, follow_child);
2667
2668           if (targetdebug)
2669             fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2670                                 follow_child, retval);
2671           return retval;
2672         }
2673     }
2674
2675   /* Some target returned a fork event, but did not know how to follow it.  */
2676   internal_error (__FILE__, __LINE__,
2677                   _("could not find a target to follow fork"));
2678 }
2679
2680 void
2681 target_mourn_inferior (void)
2682 {
2683   struct target_ops *t;
2684
2685   for (t = current_target.beneath; t != NULL; t = t->beneath)
2686     {
2687       if (t->to_mourn_inferior != NULL) 
2688         {
2689           t->to_mourn_inferior (t);
2690           if (targetdebug)
2691             fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2692
2693           /* We no longer need to keep handles on any of the object files.
2694              Make sure to release them to avoid unnecessarily locking any
2695              of them while we're not actually debugging.  */
2696           bfd_cache_close_all ();
2697
2698           return;
2699         }
2700     }
2701
2702   internal_error (__FILE__, __LINE__,
2703                   _("could not find a target to follow mourn inferior"));
2704 }
2705
2706 /* Look for a target which can describe architectural features, starting
2707    from TARGET.  If we find one, return its description.  */
2708
2709 const struct target_desc *
2710 target_read_description (struct target_ops *target)
2711 {
2712   struct target_ops *t;
2713
2714   for (t = target; t != NULL; t = t->beneath)
2715     if (t->to_read_description != NULL)
2716       {
2717         const struct target_desc *tdesc;
2718
2719         tdesc = t->to_read_description (t);
2720         if (tdesc)
2721           return tdesc;
2722       }
2723
2724   return NULL;
2725 }
2726
2727 /* The default implementation of to_search_memory.
2728    This implements a basic search of memory, reading target memory and
2729    performing the search here (as opposed to performing the search in on the
2730    target side with, for example, gdbserver).  */
2731
2732 int
2733 simple_search_memory (struct target_ops *ops,
2734                       CORE_ADDR start_addr, ULONGEST search_space_len,
2735                       const gdb_byte *pattern, ULONGEST pattern_len,
2736                       CORE_ADDR *found_addrp)
2737 {
2738   /* NOTE: also defined in find.c testcase.  */
2739 #define SEARCH_CHUNK_SIZE 16000
2740   const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2741   /* Buffer to hold memory contents for searching.  */
2742   gdb_byte *search_buf;
2743   unsigned search_buf_size;
2744   struct cleanup *old_cleanups;
2745
2746   search_buf_size = chunk_size + pattern_len - 1;
2747
2748   /* No point in trying to allocate a buffer larger than the search space.  */
2749   if (search_space_len < search_buf_size)
2750     search_buf_size = search_space_len;
2751
2752   search_buf = malloc (search_buf_size);
2753   if (search_buf == NULL)
2754     error (_("Unable to allocate memory to perform the search."));
2755   old_cleanups = make_cleanup (free_current_contents, &search_buf);
2756
2757   /* Prime the search buffer.  */
2758
2759   if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2760                    search_buf, start_addr, search_buf_size) != search_buf_size)
2761     {
2762       warning (_("Unable to access target memory at %s, halting search."),
2763                hex_string (start_addr));
2764       do_cleanups (old_cleanups);
2765       return -1;
2766     }
2767
2768   /* Perform the search.
2769
2770      The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2771      When we've scanned N bytes we copy the trailing bytes to the start and
2772      read in another N bytes.  */
2773
2774   while (search_space_len >= pattern_len)
2775     {
2776       gdb_byte *found_ptr;
2777       unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2778
2779       found_ptr = memmem (search_buf, nr_search_bytes,
2780                           pattern, pattern_len);
2781
2782       if (found_ptr != NULL)
2783         {
2784           CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2785
2786           *found_addrp = found_addr;
2787           do_cleanups (old_cleanups);
2788           return 1;
2789         }
2790
2791       /* Not found in this chunk, skip to next chunk.  */
2792
2793       /* Don't let search_space_len wrap here, it's unsigned.  */
2794       if (search_space_len >= chunk_size)
2795         search_space_len -= chunk_size;
2796       else
2797         search_space_len = 0;
2798
2799       if (search_space_len >= pattern_len)
2800         {
2801           unsigned keep_len = search_buf_size - chunk_size;
2802           CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
2803           int nr_to_read;
2804
2805           /* Copy the trailing part of the previous iteration to the front
2806              of the buffer for the next iteration.  */
2807           gdb_assert (keep_len == pattern_len - 1);
2808           memcpy (search_buf, search_buf + chunk_size, keep_len);
2809
2810           nr_to_read = min (search_space_len - keep_len, chunk_size);
2811
2812           if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2813                            search_buf + keep_len, read_addr,
2814                            nr_to_read) != nr_to_read)
2815             {
2816               warning (_("Unable to access target "
2817                          "memory at %s, halting search."),
2818                        hex_string (read_addr));
2819               do_cleanups (old_cleanups);
2820               return -1;
2821             }
2822
2823           start_addr += chunk_size;
2824         }
2825     }
2826
2827   /* Not found.  */
2828
2829   do_cleanups (old_cleanups);
2830   return 0;
2831 }
2832
2833 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2834    sequence of bytes in PATTERN with length PATTERN_LEN.
2835
2836    The result is 1 if found, 0 if not found, and -1 if there was an error
2837    requiring halting of the search (e.g. memory read error).
2838    If the pattern is found the address is recorded in FOUND_ADDRP.  */
2839
2840 int
2841 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2842                       const gdb_byte *pattern, ULONGEST pattern_len,
2843                       CORE_ADDR *found_addrp)
2844 {
2845   struct target_ops *t;
2846   int found;
2847
2848   /* We don't use INHERIT to set current_target.to_search_memory,
2849      so we have to scan the target stack and handle targetdebug
2850      ourselves.  */
2851
2852   if (targetdebug)
2853     fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2854                         hex_string (start_addr));
2855
2856   for (t = current_target.beneath; t != NULL; t = t->beneath)
2857     if (t->to_search_memory != NULL)
2858       break;
2859
2860   if (t != NULL)
2861     {
2862       found = t->to_search_memory (t, start_addr, search_space_len,
2863                                    pattern, pattern_len, found_addrp);
2864     }
2865   else
2866     {
2867       /* If a special version of to_search_memory isn't available, use the
2868          simple version.  */
2869       found = simple_search_memory (current_target.beneath,
2870                                     start_addr, search_space_len,
2871                                     pattern, pattern_len, found_addrp);
2872     }
2873
2874   if (targetdebug)
2875     fprintf_unfiltered (gdb_stdlog, "  = %d\n", found);
2876
2877   return found;
2878 }
2879
2880 /* Look through the currently pushed targets.  If none of them will
2881    be able to restart the currently running process, issue an error
2882    message.  */
2883
2884 void
2885 target_require_runnable (void)
2886 {
2887   struct target_ops *t;
2888
2889   for (t = target_stack; t != NULL; t = t->beneath)
2890     {
2891       /* If this target knows how to create a new program, then
2892          assume we will still be able to after killing the current
2893          one.  Either killing and mourning will not pop T, or else
2894          find_default_run_target will find it again.  */
2895       if (t->to_create_inferior != NULL)
2896         return;
2897
2898       /* Do not worry about thread_stratum targets that can not
2899          create inferiors.  Assume they will be pushed again if
2900          necessary, and continue to the process_stratum.  */
2901       if (t->to_stratum == thread_stratum
2902           || t->to_stratum == arch_stratum)
2903         continue;
2904
2905       error (_("The \"%s\" target does not support \"run\".  "
2906                "Try \"help target\" or \"continue\"."),
2907              t->to_shortname);
2908     }
2909
2910   /* This function is only called if the target is running.  In that
2911      case there should have been a process_stratum target and it
2912      should either know how to create inferiors, or not...  */
2913   internal_error (__FILE__, __LINE__, _("No targets found"));
2914 }
2915
2916 /* Look through the list of possible targets for a target that can
2917    execute a run or attach command without any other data.  This is
2918    used to locate the default process stratum.
2919
2920    If DO_MESG is not NULL, the result is always valid (error() is
2921    called for errors); else, return NULL on error.  */
2922
2923 static struct target_ops *
2924 find_default_run_target (char *do_mesg)
2925 {
2926   struct target_ops **t;
2927   struct target_ops *runable = NULL;
2928   int count;
2929
2930   count = 0;
2931
2932   for (t = target_structs; t < target_structs + target_struct_size;
2933        ++t)
2934     {
2935       if ((*t)->to_can_run && target_can_run (*t))
2936         {
2937           runable = *t;
2938           ++count;
2939         }
2940     }
2941
2942   if (count != 1)
2943     {
2944       if (do_mesg)
2945         error (_("Don't know how to %s.  Try \"help target\"."), do_mesg);
2946       else
2947         return NULL;
2948     }
2949
2950   return runable;
2951 }
2952
2953 void
2954 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2955 {
2956   struct target_ops *t;
2957
2958   t = find_default_run_target ("attach");
2959   (t->to_attach) (t, args, from_tty);
2960   return;
2961 }
2962
2963 void
2964 find_default_create_inferior (struct target_ops *ops,
2965                               char *exec_file, char *allargs, char **env,
2966                               int from_tty)
2967 {
2968   struct target_ops *t;
2969
2970   t = find_default_run_target ("run");
2971   (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2972   return;
2973 }
2974
2975 static int
2976 find_default_can_async_p (void)
2977 {
2978   struct target_ops *t;
2979
2980   /* This may be called before the target is pushed on the stack;
2981      look for the default process stratum.  If there's none, gdb isn't
2982      configured with a native debugger, and target remote isn't
2983      connected yet.  */
2984   t = find_default_run_target (NULL);
2985   if (t && t->to_can_async_p)
2986     return (t->to_can_async_p) ();
2987   return 0;
2988 }
2989
2990 static int
2991 find_default_is_async_p (void)
2992 {
2993   struct target_ops *t;
2994
2995   /* This may be called before the target is pushed on the stack;
2996      look for the default process stratum.  If there's none, gdb isn't
2997      configured with a native debugger, and target remote isn't
2998      connected yet.  */
2999   t = find_default_run_target (NULL);
3000   if (t && t->to_is_async_p)
3001     return (t->to_is_async_p) ();
3002   return 0;
3003 }
3004
3005 static int
3006 find_default_supports_non_stop (void)
3007 {
3008   struct target_ops *t;
3009
3010   t = find_default_run_target (NULL);
3011   if (t && t->to_supports_non_stop)
3012     return (t->to_supports_non_stop) ();
3013   return 0;
3014 }
3015
3016 int
3017 target_supports_non_stop (void)
3018 {
3019   struct target_ops *t;
3020
3021   for (t = &current_target; t != NULL; t = t->beneath)
3022     if (t->to_supports_non_stop)
3023       return t->to_supports_non_stop ();
3024
3025   return 0;
3026 }
3027
3028 static int
3029 find_default_supports_disable_randomization (void)
3030 {
3031   struct target_ops *t;
3032
3033   t = find_default_run_target (NULL);
3034   if (t && t->to_supports_disable_randomization)
3035     return (t->to_supports_disable_randomization) ();
3036   return 0;
3037 }
3038
3039 int
3040 target_supports_disable_randomization (void)
3041 {
3042   struct target_ops *t;
3043
3044   for (t = &current_target; t != NULL; t = t->beneath)
3045     if (t->to_supports_disable_randomization)
3046       return t->to_supports_disable_randomization ();
3047
3048   return 0;
3049 }
3050
3051 char *
3052 target_get_osdata (const char *type)
3053 {
3054   struct target_ops *t;
3055
3056   /* If we're already connected to something that can get us OS
3057      related data, use it.  Otherwise, try using the native
3058      target.  */
3059   if (current_target.to_stratum >= process_stratum)
3060     t = current_target.beneath;
3061   else
3062     t = find_default_run_target ("get OS data");
3063
3064   if (!t)
3065     return NULL;
3066
3067   return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
3068 }
3069
3070 /* Determine the current address space of thread PTID.  */
3071
3072 struct address_space *
3073 target_thread_address_space (ptid_t ptid)
3074 {
3075   struct address_space *aspace;
3076   struct inferior *inf;
3077   struct target_ops *t;
3078
3079   for (t = current_target.beneath; t != NULL; t = t->beneath)
3080     {
3081       if (t->to_thread_address_space != NULL)
3082         {
3083           aspace = t->to_thread_address_space (t, ptid);
3084           gdb_assert (aspace);
3085
3086           if (targetdebug)
3087             fprintf_unfiltered (gdb_stdlog,
3088                                 "target_thread_address_space (%s) = %d\n",
3089                                 target_pid_to_str (ptid),
3090                                 address_space_num (aspace));
3091           return aspace;
3092         }
3093     }
3094
3095   /* Fall-back to the "main" address space of the inferior.  */
3096   inf = find_inferior_pid (ptid_get_pid (ptid));
3097
3098   if (inf == NULL || inf->aspace == NULL)
3099     internal_error (__FILE__, __LINE__,
3100                     _("Can't determine the current "
3101                       "address space of thread %s\n"),
3102                     target_pid_to_str (ptid));
3103
3104   return inf->aspace;
3105 }
3106
3107 static int
3108 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3109 {
3110   return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
3111 }
3112
3113 static int
3114 default_watchpoint_addr_within_range (struct target_ops *target,
3115                                       CORE_ADDR addr,
3116                                       CORE_ADDR start, int length)
3117 {
3118   return addr >= start && addr < start + length;
3119 }
3120
3121 static struct gdbarch *
3122 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
3123 {
3124   return target_gdbarch;
3125 }
3126
3127 static int
3128 return_zero (void)
3129 {
3130   return 0;
3131 }
3132
3133 static int
3134 return_one (void)
3135 {
3136   return 1;
3137 }
3138
3139 static int
3140 return_minus_one (void)
3141 {
3142   return -1;
3143 }
3144
3145 /* Find a single runnable target in the stack and return it.  If for
3146    some reason there is more than one, return NULL.  */
3147
3148 struct target_ops *
3149 find_run_target (void)
3150 {
3151   struct target_ops **t;
3152   struct target_ops *runable = NULL;
3153   int count;
3154
3155   count = 0;
3156
3157   for (t = target_structs; t < target_structs + target_struct_size; ++t)
3158     {
3159       if ((*t)->to_can_run && target_can_run (*t))
3160         {
3161           runable = *t;
3162           ++count;
3163         }
3164     }
3165
3166   return (count == 1 ? runable : NULL);
3167 }
3168
3169 /*
3170  * Find the next target down the stack from the specified target.
3171  */
3172
3173 struct target_ops *
3174 find_target_beneath (struct target_ops *t)
3175 {
3176   return t->beneath;
3177 }
3178
3179 \f
3180 /* The inferior process has died.  Long live the inferior!  */
3181
3182 void
3183 generic_mourn_inferior (void)
3184 {
3185   ptid_t ptid;
3186
3187   ptid = inferior_ptid;
3188   inferior_ptid = null_ptid;
3189
3190   if (!ptid_equal (ptid, null_ptid))
3191     {
3192       int pid = ptid_get_pid (ptid);
3193       exit_inferior (pid);
3194     }
3195
3196   breakpoint_init_inferior (inf_exited);
3197   registers_changed ();
3198
3199   reopen_exec_file ();
3200   reinit_frame_cache ();
3201
3202   if (deprecated_detach_hook)
3203     deprecated_detach_hook ();
3204 }
3205 \f
3206 /* Helper function for child_wait and the derivatives of child_wait.
3207    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
3208    translation of that in OURSTATUS.  */
3209 void
3210 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
3211 {
3212   if (WIFEXITED (hoststatus))
3213     {
3214       ourstatus->kind = TARGET_WAITKIND_EXITED;
3215       ourstatus->value.integer = WEXITSTATUS (hoststatus);
3216     }
3217   else if (!WIFSTOPPED (hoststatus))
3218     {
3219       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
3220       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
3221     }
3222   else
3223     {
3224       ourstatus->kind = TARGET_WAITKIND_STOPPED;
3225       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
3226     }
3227 }
3228 \f
3229 /* Convert a normal process ID to a string.  Returns the string in a
3230    static buffer.  */
3231
3232 char *
3233 normal_pid_to_str (ptid_t ptid)
3234 {
3235   static char buf[32];
3236
3237   xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
3238   return buf;
3239 }
3240
3241 static char *
3242 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
3243 {
3244   return normal_pid_to_str (ptid);
3245 }
3246
3247 /* Error-catcher for target_find_memory_regions.  */
3248 static int
3249 dummy_find_memory_regions (find_memory_region_ftype ignore1, void *ignore2)
3250 {
3251   error (_("Command not implemented for this target."));
3252   return 0;
3253 }
3254
3255 /* Error-catcher for target_make_corefile_notes.  */
3256 static char *
3257 dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
3258 {
3259   error (_("Command not implemented for this target."));
3260   return NULL;
3261 }
3262
3263 /* Error-catcher for target_get_bookmark.  */
3264 static gdb_byte *
3265 dummy_get_bookmark (char *ignore1, int ignore2)
3266 {
3267   tcomplain ();
3268   return NULL;
3269 }
3270
3271 /* Error-catcher for target_goto_bookmark.  */
3272 static void
3273 dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
3274 {
3275   tcomplain ();
3276 }
3277
3278 /* Set up the handful of non-empty slots needed by the dummy target
3279    vector.  */
3280
3281 static void
3282 init_dummy_target (void)
3283 {
3284   dummy_target.to_shortname = "None";
3285   dummy_target.to_longname = "None";
3286   dummy_target.to_doc = "";
3287   dummy_target.to_attach = find_default_attach;
3288   dummy_target.to_detach = 
3289     (void (*)(struct target_ops *, char *, int))target_ignore;
3290   dummy_target.to_create_inferior = find_default_create_inferior;
3291   dummy_target.to_can_async_p = find_default_can_async_p;
3292   dummy_target.to_is_async_p = find_default_is_async_p;
3293   dummy_target.to_supports_non_stop = find_default_supports_non_stop;
3294   dummy_target.to_supports_disable_randomization
3295     = find_default_supports_disable_randomization;
3296   dummy_target.to_pid_to_str = dummy_pid_to_str;
3297   dummy_target.to_stratum = dummy_stratum;
3298   dummy_target.to_find_memory_regions = dummy_find_memory_regions;
3299   dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
3300   dummy_target.to_get_bookmark = dummy_get_bookmark;
3301   dummy_target.to_goto_bookmark = dummy_goto_bookmark;
3302   dummy_target.to_xfer_partial = default_xfer_partial;
3303   dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3304   dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
3305   dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
3306   dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
3307   dummy_target.to_has_execution
3308     = (int (*) (struct target_ops *, ptid_t)) return_zero;
3309   dummy_target.to_stopped_by_watchpoint = return_zero;
3310   dummy_target.to_stopped_data_address =
3311     (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
3312   dummy_target.to_magic = OPS_MAGIC;
3313 }
3314 \f
3315 static void
3316 debug_to_open (char *args, int from_tty)
3317 {
3318   debug_target.to_open (args, from_tty);
3319
3320   fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
3321 }
3322
3323 void
3324 target_close (struct target_ops *targ, int quitting)
3325 {
3326   if (targ->to_xclose != NULL)
3327     targ->to_xclose (targ, quitting);
3328   else if (targ->to_close != NULL)
3329     targ->to_close (quitting);
3330
3331   if (targetdebug)
3332     fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
3333 }
3334
3335 void
3336 target_attach (char *args, int from_tty)
3337 {
3338   struct target_ops *t;
3339
3340   for (t = current_target.beneath; t != NULL; t = t->beneath)
3341     {
3342       if (t->to_attach != NULL) 
3343         {
3344           t->to_attach (t, args, from_tty);
3345           if (targetdebug)
3346             fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
3347                                 args, from_tty);
3348           return;
3349         }
3350     }
3351
3352   internal_error (__FILE__, __LINE__,
3353                   _("could not find a target to attach"));
3354 }
3355
3356 int
3357 target_thread_alive (ptid_t ptid)
3358 {
3359   struct target_ops *t;
3360
3361   for (t = current_target.beneath; t != NULL; t = t->beneath)
3362     {
3363       if (t->to_thread_alive != NULL)
3364         {
3365           int retval;
3366
3367           retval = t->to_thread_alive (t, ptid);
3368           if (targetdebug)
3369             fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
3370                                 PIDGET (ptid), retval);
3371
3372           return retval;
3373         }
3374     }
3375
3376   return 0;
3377 }
3378
3379 void
3380 target_find_new_threads (void)
3381 {
3382   struct target_ops *t;
3383
3384   for (t = current_target.beneath; t != NULL; t = t->beneath)
3385     {
3386       if (t->to_find_new_threads != NULL)
3387         {
3388           t->to_find_new_threads (t);
3389           if (targetdebug)
3390             fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
3391
3392           return;
3393         }
3394     }
3395 }
3396
3397 void
3398 target_stop (ptid_t ptid)
3399 {
3400   if (!may_stop)
3401     {
3402       warning (_("May not interrupt or stop the target, ignoring attempt"));
3403       return;
3404     }
3405
3406   (*current_target.to_stop) (ptid);
3407 }
3408
3409 static void
3410 debug_to_post_attach (int pid)
3411 {
3412   debug_target.to_post_attach (pid);
3413
3414   fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
3415 }
3416
3417 /* Return a pretty printed form of target_waitstatus.
3418    Space for the result is malloc'd, caller must free.  */
3419
3420 char *
3421 target_waitstatus_to_string (const struct target_waitstatus *ws)
3422 {
3423   const char *kind_str = "status->kind = ";
3424
3425   switch (ws->kind)
3426     {
3427     case TARGET_WAITKIND_EXITED:
3428       return xstrprintf ("%sexited, status = %d",
3429                          kind_str, ws->value.integer);
3430     case TARGET_WAITKIND_STOPPED:
3431       return xstrprintf ("%sstopped, signal = %s",
3432                          kind_str, target_signal_to_name (ws->value.sig));
3433     case TARGET_WAITKIND_SIGNALLED:
3434       return xstrprintf ("%ssignalled, signal = %s",
3435                          kind_str, target_signal_to_name (ws->value.sig));
3436     case TARGET_WAITKIND_LOADED:
3437       return xstrprintf ("%sloaded", kind_str);
3438     case TARGET_WAITKIND_FORKED:
3439       return xstrprintf ("%sforked", kind_str);
3440     case TARGET_WAITKIND_VFORKED:
3441       return xstrprintf ("%svforked", kind_str);
3442     case TARGET_WAITKIND_EXECD:
3443       return xstrprintf ("%sexecd", kind_str);
3444     case TARGET_WAITKIND_SYSCALL_ENTRY:
3445       return xstrprintf ("%sentered syscall", kind_str);
3446     case TARGET_WAITKIND_SYSCALL_RETURN:
3447       return xstrprintf ("%sexited syscall", kind_str);
3448     case TARGET_WAITKIND_SPURIOUS:
3449       return xstrprintf ("%sspurious", kind_str);
3450     case TARGET_WAITKIND_IGNORE:
3451       return xstrprintf ("%signore", kind_str);
3452     case TARGET_WAITKIND_NO_HISTORY:
3453       return xstrprintf ("%sno-history", kind_str);
3454     case TARGET_WAITKIND_NO_RESUMED:
3455       return xstrprintf ("%sno-resumed", kind_str);
3456     default:
3457       return xstrprintf ("%sunknown???", kind_str);
3458     }
3459 }
3460
3461 static void
3462 debug_print_register (const char * func,
3463                       struct regcache *regcache, int regno)
3464 {
3465   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3466
3467   fprintf_unfiltered (gdb_stdlog, "%s ", func);
3468   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
3469       && gdbarch_register_name (gdbarch, regno) != NULL
3470       && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3471     fprintf_unfiltered (gdb_stdlog, "(%s)",
3472                         gdbarch_register_name (gdbarch, regno));
3473   else
3474     fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
3475   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
3476     {
3477       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3478       int i, size = register_size (gdbarch, regno);
3479       unsigned char buf[MAX_REGISTER_SIZE];
3480
3481       regcache_raw_collect (regcache, regno, buf);
3482       fprintf_unfiltered (gdb_stdlog, " = ");
3483       for (i = 0; i < size; i++)
3484         {
3485           fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3486         }
3487       if (size <= sizeof (LONGEST))
3488         {
3489           ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
3490
3491           fprintf_unfiltered (gdb_stdlog, " %s %s",
3492                               core_addr_to_string_nz (val), plongest (val));
3493         }
3494     }
3495   fprintf_unfiltered (gdb_stdlog, "\n");
3496 }
3497
3498 void
3499 target_fetch_registers (struct regcache *regcache, int regno)
3500 {
3501   struct target_ops *t;
3502
3503   for (t = current_target.beneath; t != NULL; t = t->beneath)
3504     {
3505       if (t->to_fetch_registers != NULL)
3506         {
3507           t->to_fetch_registers (t, regcache, regno);
3508           if (targetdebug)
3509             debug_print_register ("target_fetch_registers", regcache, regno);
3510           return;
3511         }
3512     }
3513 }
3514
3515 void
3516 target_store_registers (struct regcache *regcache, int regno)
3517 {
3518   struct target_ops *t;
3519
3520   if (!may_write_registers)
3521     error (_("Writing to registers is not allowed (regno %d)"), regno);
3522
3523   for (t = current_target.beneath; t != NULL; t = t->beneath)
3524     {
3525       if (t->to_store_registers != NULL)
3526         {
3527           t->to_store_registers (t, regcache, regno);
3528           if (targetdebug)
3529             {
3530               debug_print_register ("target_store_registers", regcache, regno);
3531             }
3532           return;
3533         }
3534     }
3535
3536   noprocess ();
3537 }
3538
3539 int
3540 target_core_of_thread (ptid_t ptid)
3541 {
3542   struct target_ops *t;
3543
3544   for (t = current_target.beneath; t != NULL; t = t->beneath)
3545     {
3546       if (t->to_core_of_thread != NULL)
3547         {
3548           int retval = t->to_core_of_thread (t, ptid);
3549
3550           if (targetdebug)
3551             fprintf_unfiltered (gdb_stdlog,
3552                                 "target_core_of_thread (%d) = %d\n",
3553                                 PIDGET (ptid), retval);
3554           return retval;
3555         }
3556     }
3557
3558   return -1;
3559 }
3560
3561 int
3562 target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3563 {
3564   struct target_ops *t;
3565
3566   for (t = current_target.beneath; t != NULL; t = t->beneath)
3567     {
3568       if (t->to_verify_memory != NULL)
3569         {
3570           int retval = t->to_verify_memory (t, data, memaddr, size);
3571
3572           if (targetdebug)
3573             fprintf_unfiltered (gdb_stdlog,
3574                                 "target_verify_memory (%s, %s) = %d\n",
3575                                 paddress (target_gdbarch, memaddr),
3576                                 pulongest (size),
3577                                 retval);
3578           return retval;
3579         }
3580     }
3581
3582   tcomplain ();
3583 }
3584
3585 /* The documentation for this function is in its prototype declaration in
3586    target.h.  */
3587
3588 int
3589 target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3590 {
3591   struct target_ops *t;
3592
3593   for (t = current_target.beneath; t != NULL; t = t->beneath)
3594     if (t->to_insert_mask_watchpoint != NULL)
3595       {
3596         int ret;
3597
3598         ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
3599
3600         if (targetdebug)
3601           fprintf_unfiltered (gdb_stdlog, "\
3602 target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
3603                               core_addr_to_string (addr),
3604                               core_addr_to_string (mask), rw, ret);
3605
3606         return ret;
3607       }
3608
3609   return 1;
3610 }
3611
3612 /* The documentation for this function is in its prototype declaration in
3613    target.h.  */
3614
3615 int
3616 target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3617 {
3618   struct target_ops *t;
3619
3620   for (t = current_target.beneath; t != NULL; t = t->beneath)
3621     if (t->to_remove_mask_watchpoint != NULL)
3622       {
3623         int ret;
3624
3625         ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
3626
3627         if (targetdebug)
3628           fprintf_unfiltered (gdb_stdlog, "\
3629 target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
3630                               core_addr_to_string (addr),
3631                               core_addr_to_string (mask), rw, ret);
3632
3633         return ret;
3634       }
3635
3636   return 1;
3637 }
3638
3639 /* The documentation for this function is in its prototype declaration
3640    in target.h.  */
3641
3642 int
3643 target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
3644 {
3645   struct target_ops *t;
3646
3647   for (t = current_target.beneath; t != NULL; t = t->beneath)
3648     if (t->to_masked_watch_num_registers != NULL)
3649       return t->to_masked_watch_num_registers (t, addr, mask);
3650
3651   return -1;
3652 }
3653
3654 /* The documentation for this function is in its prototype declaration
3655    in target.h.  */
3656
3657 int
3658 target_ranged_break_num_registers (void)
3659 {
3660   struct target_ops *t;
3661
3662   for (t = current_target.beneath; t != NULL; t = t->beneath)
3663     if (t->to_ranged_break_num_registers != NULL)
3664       return t->to_ranged_break_num_registers (t);
3665
3666   return -1;
3667 }
3668
3669 static void
3670 debug_to_prepare_to_store (struct regcache *regcache)
3671 {
3672   debug_target.to_prepare_to_store (regcache);
3673
3674   fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
3675 }
3676
3677 static int
3678 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
3679                               int write, struct mem_attrib *attrib,
3680                               struct target_ops *target)
3681 {
3682   int retval;
3683
3684   retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3685                                                 attrib, target);
3686
3687   fprintf_unfiltered (gdb_stdlog,
3688                       "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
3689                       paddress (target_gdbarch, memaddr), len,
3690                       write ? "write" : "read", retval);
3691
3692   if (retval > 0)
3693     {
3694       int i;
3695
3696       fputs_unfiltered (", bytes =", gdb_stdlog);
3697       for (i = 0; i < retval; i++)
3698         {
3699           if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
3700             {
3701               if (targetdebug < 2 && i > 0)
3702                 {
3703                   fprintf_unfiltered (gdb_stdlog, " ...");
3704                   break;
3705                 }
3706               fprintf_unfiltered (gdb_stdlog, "\n");
3707             }
3708
3709           fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
3710         }
3711     }
3712
3713   fputc_unfiltered ('\n', gdb_stdlog);
3714
3715   return retval;
3716 }
3717
3718 static void
3719 debug_to_files_info (struct target_ops *target)
3720 {
3721   debug_target.to_files_info (target);
3722
3723   fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
3724 }
3725
3726 static int
3727 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3728                             struct bp_target_info *bp_tgt)
3729 {
3730   int retval;
3731
3732   retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
3733
3734   fprintf_unfiltered (gdb_stdlog,
3735                       "target_insert_breakpoint (%s, xxx) = %ld\n",
3736                       core_addr_to_string (bp_tgt->placed_address),
3737                       (unsigned long) retval);
3738   return retval;
3739 }
3740
3741 static int
3742 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3743                             struct bp_target_info *bp_tgt)
3744 {
3745   int retval;
3746
3747   retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
3748
3749   fprintf_unfiltered (gdb_stdlog,
3750                       "target_remove_breakpoint (%s, xxx) = %ld\n",
3751                       core_addr_to_string (bp_tgt->placed_address),
3752                       (unsigned long) retval);
3753   return retval;
3754 }
3755
3756 static int
3757 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3758 {
3759   int retval;
3760
3761   retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3762
3763   fprintf_unfiltered (gdb_stdlog,
3764                       "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3765                       (unsigned long) type,
3766                       (unsigned long) cnt,
3767                       (unsigned long) from_tty,
3768                       (unsigned long) retval);
3769   return retval;
3770 }
3771
3772 static int
3773 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3774 {
3775   CORE_ADDR retval;
3776
3777   retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3778
3779   fprintf_unfiltered (gdb_stdlog,
3780                       "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
3781                       core_addr_to_string (addr), (unsigned long) len,
3782                       core_addr_to_string (retval));
3783   return retval;
3784 }
3785
3786 static int
3787 debug_to_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
3788                                          struct expression *cond)
3789 {
3790   int retval;
3791
3792   retval = debug_target.to_can_accel_watchpoint_condition (addr, len,
3793                                                            rw, cond);
3794
3795   fprintf_unfiltered (gdb_stdlog,
3796                       "target_can_accel_watchpoint_condition "
3797                       "(%s, %d, %d, %s) = %ld\n",
3798                       core_addr_to_string (addr), len, rw,
3799                       host_address_to_string (cond), (unsigned long) retval);
3800   return retval;
3801 }
3802
3803 static int
3804 debug_to_stopped_by_watchpoint (void)
3805 {
3806   int retval;
3807
3808   retval = debug_target.to_stopped_by_watchpoint ();
3809
3810   fprintf_unfiltered (gdb_stdlog,
3811                       "target_stopped_by_watchpoint () = %ld\n",
3812                       (unsigned long) retval);
3813   return retval;
3814 }
3815
3816 static int
3817 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
3818 {
3819   int retval;
3820
3821   retval = debug_target.to_stopped_data_address (target, addr);
3822
3823   fprintf_unfiltered (gdb_stdlog,
3824                       "target_stopped_data_address ([%s]) = %ld\n",
3825                       core_addr_to_string (*addr),
3826                       (unsigned long)retval);
3827   return retval;
3828 }
3829
3830 static int
3831 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3832                                        CORE_ADDR addr,
3833                                        CORE_ADDR start, int length)
3834 {
3835   int retval;
3836
3837   retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3838                                                          start, length);
3839
3840   fprintf_filtered (gdb_stdlog,
3841                     "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
3842                     core_addr_to_string (addr), core_addr_to_string (start),
3843                     length, retval);
3844   return retval;
3845 }
3846
3847 static int
3848 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3849                                struct bp_target_info *bp_tgt)
3850 {
3851   int retval;
3852
3853   retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3854
3855   fprintf_unfiltered (gdb_stdlog,
3856                       "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
3857                       core_addr_to_string (bp_tgt->placed_address),
3858                       (unsigned long) retval);
3859   return retval;
3860 }
3861
3862 static int
3863 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3864                                struct bp_target_info *bp_tgt)
3865 {
3866   int retval;
3867
3868   retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3869
3870   fprintf_unfiltered (gdb_stdlog,
3871                       "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
3872                       core_addr_to_string (bp_tgt->placed_address),
3873                       (unsigned long) retval);
3874   return retval;
3875 }
3876
3877 static int
3878 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type,
3879                             struct expression *cond)
3880 {
3881   int retval;
3882
3883   retval = debug_target.to_insert_watchpoint (addr, len, type, cond);
3884
3885   fprintf_unfiltered (gdb_stdlog,
3886                       "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
3887                       core_addr_to_string (addr), len, type,
3888                       host_address_to_string (cond), (unsigned long) retval);
3889   return retval;
3890 }
3891
3892 static int
3893 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type,
3894                             struct expression *cond)
3895 {
3896   int retval;
3897
3898   retval = debug_target.to_remove_watchpoint (addr, len, type, cond);
3899
3900   fprintf_unfiltered (gdb_stdlog,
3901                       "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
3902                       core_addr_to_string (addr), len, type,
3903                       host_address_to_string (cond), (unsigned long) retval);
3904   return retval;
3905 }
3906
3907 static void
3908 debug_to_terminal_init (void)
3909 {
3910   debug_target.to_terminal_init ();
3911
3912   fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3913 }
3914
3915 static void
3916 debug_to_terminal_inferior (void)
3917 {
3918   debug_target.to_terminal_inferior ();
3919
3920   fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3921 }
3922
3923 static void
3924 debug_to_terminal_ours_for_output (void)
3925 {
3926   debug_target.to_terminal_ours_for_output ();
3927
3928   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3929 }
3930
3931 static void
3932 debug_to_terminal_ours (void)
3933 {
3934   debug_target.to_terminal_ours ();
3935
3936   fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3937 }
3938
3939 static void
3940 debug_to_terminal_save_ours (void)
3941 {
3942   debug_target.to_terminal_save_ours ();
3943
3944   fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3945 }
3946
3947 static void
3948 debug_to_terminal_info (char *arg, int from_tty)
3949 {
3950   debug_target.to_terminal_info (arg, from_tty);
3951
3952   fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3953                       from_tty);
3954 }
3955
3956 static void
3957 debug_to_load (char *args, int from_tty)
3958 {
3959   debug_target.to_load (args, from_tty);
3960
3961   fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3962 }
3963
3964 static void
3965 debug_to_post_startup_inferior (ptid_t ptid)
3966 {
3967   debug_target.to_post_startup_inferior (ptid);
3968
3969   fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3970                       PIDGET (ptid));
3971 }
3972
3973 static int
3974 debug_to_insert_fork_catchpoint (int pid)
3975 {
3976   int retval;
3977
3978   retval = debug_target.to_insert_fork_catchpoint (pid);
3979
3980   fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
3981                       pid, retval);
3982
3983   return retval;
3984 }
3985
3986 static int
3987 debug_to_remove_fork_catchpoint (int pid)
3988 {
3989   int retval;
3990
3991   retval = debug_target.to_remove_fork_catchpoint (pid);
3992
3993   fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3994                       pid, retval);
3995
3996   return retval;
3997 }
3998
3999 static int
4000 debug_to_insert_vfork_catchpoint (int pid)
4001 {
4002   int retval;
4003
4004   retval = debug_target.to_insert_vfork_catchpoint (pid);
4005
4006   fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4007                       pid, retval);
4008
4009   return retval;
4010 }
4011
4012 static int
4013 debug_to_remove_vfork_catchpoint (int pid)
4014 {
4015   int retval;
4016
4017   retval = debug_target.to_remove_vfork_catchpoint (pid);
4018
4019   fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
4020                       pid, retval);
4021
4022   return retval;
4023 }
4024
4025 static int
4026 debug_to_insert_exec_catchpoint (int pid)
4027 {
4028   int retval;
4029
4030   retval = debug_target.to_insert_exec_catchpoint (pid);
4031
4032   fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4033                       pid, retval);
4034
4035   return retval;
4036 }
4037
4038 static int
4039 debug_to_remove_exec_catchpoint (int pid)
4040 {
4041   int retval;
4042
4043   retval = debug_target.to_remove_exec_catchpoint (pid);
4044
4045   fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
4046                       pid, retval);
4047
4048   return retval;
4049 }
4050
4051 static int
4052 debug_to_has_exited (int pid, int wait_status, int *exit_status)
4053 {
4054   int has_exited;
4055
4056   has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
4057
4058   fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
4059                       pid, wait_status, *exit_status, has_exited);
4060
4061   return has_exited;
4062 }
4063
4064 static int
4065 debug_to_can_run (void)
4066 {
4067   int retval;
4068
4069   retval = debug_target.to_can_run ();
4070
4071   fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
4072
4073   return retval;
4074 }
4075
4076 static struct gdbarch *
4077 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
4078 {
4079   struct gdbarch *retval;
4080
4081   retval = debug_target.to_thread_architecture (ops, ptid);
4082
4083   fprintf_unfiltered (gdb_stdlog, 
4084                       "target_thread_architecture (%s) = %s [%s]\n",
4085                       target_pid_to_str (ptid),
4086                       host_address_to_string (retval),
4087                       gdbarch_bfd_arch_info (retval)->printable_name);
4088   return retval;
4089 }
4090
4091 static void
4092 debug_to_stop (ptid_t ptid)
4093 {
4094   debug_target.to_stop (ptid);
4095
4096   fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
4097                       target_pid_to_str (ptid));
4098 }
4099
4100 static void
4101 debug_to_rcmd (char *command,
4102                struct ui_file *outbuf)
4103 {
4104   debug_target.to_rcmd (command, outbuf);
4105   fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
4106 }
4107
4108 static char *
4109 debug_to_pid_to_exec_file (int pid)
4110 {
4111   char *exec_file;
4112
4113   exec_file = debug_target.to_pid_to_exec_file (pid);
4114
4115   fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
4116                       pid, exec_file);
4117
4118   return exec_file;
4119 }
4120
4121 static void
4122 setup_target_debug (void)
4123 {
4124   memcpy (&debug_target, &current_target, sizeof debug_target);
4125
4126   current_target.to_open = debug_to_open;
4127   current_target.to_post_attach = debug_to_post_attach;
4128   current_target.to_prepare_to_store = debug_to_prepare_to_store;
4129   current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
4130   current_target.to_files_info = debug_to_files_info;
4131   current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
4132   current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
4133   current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
4134   current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
4135   current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
4136   current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
4137   current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
4138   current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
4139   current_target.to_stopped_data_address = debug_to_stopped_data_address;
4140   current_target.to_watchpoint_addr_within_range
4141     = debug_to_watchpoint_addr_within_range;
4142   current_target.to_region_ok_for_hw_watchpoint
4143     = debug_to_region_ok_for_hw_watchpoint;
4144   current_target.to_can_accel_watchpoint_condition
4145     = debug_to_can_accel_watchpoint_condition;
4146   current_target.to_terminal_init = debug_to_terminal_init;
4147   current_target.to_terminal_inferior = debug_to_terminal_inferior;
4148   current_target.to_terminal_ours_for_output
4149     = debug_to_terminal_ours_for_output;
4150   current_target.to_terminal_ours = debug_to_terminal_ours;
4151   current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
4152   current_target.to_terminal_info = debug_to_terminal_info;
4153   current_target.to_load = debug_to_load;
4154   current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
4155   current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
4156   current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
4157   current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
4158   current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
4159   current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
4160   current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
4161   current_target.to_has_exited = debug_to_has_exited;
4162   current_target.to_can_run = debug_to_can_run;
4163   current_target.to_stop = debug_to_stop;
4164   current_target.to_rcmd = debug_to_rcmd;
4165   current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
4166   current_target.to_thread_architecture = debug_to_thread_architecture;
4167 }
4168 \f
4169
4170 static char targ_desc[] =
4171 "Names of targets and files being debugged.\nShows the entire \
4172 stack of targets currently in use (including the exec-file,\n\
4173 core-file, and process, if any), as well as the symbol file name.";
4174
4175 static void
4176 do_monitor_command (char *cmd,
4177                  int from_tty)
4178 {
4179   if ((current_target.to_rcmd
4180        == (void (*) (char *, struct ui_file *)) tcomplain)
4181       || (current_target.to_rcmd == debug_to_rcmd
4182           && (debug_target.to_rcmd
4183               == (void (*) (char *, struct ui_file *)) tcomplain)))
4184     error (_("\"monitor\" command not supported by this target."));
4185   target_rcmd (cmd, gdb_stdtarg);
4186 }
4187
4188 /* Print the name of each layers of our target stack.  */
4189
4190 static void
4191 maintenance_print_target_stack (char *cmd, int from_tty)
4192 {
4193   struct target_ops *t;
4194
4195   printf_filtered (_("The current target stack is:\n"));
4196
4197   for (t = target_stack; t != NULL; t = t->beneath)
4198     {
4199       printf_filtered ("  - %s (%s)\n", t->to_shortname, t->to_longname);
4200     }
4201 }
4202
4203 /* Controls if async mode is permitted.  */
4204 int target_async_permitted = 0;
4205
4206 /* The set command writes to this variable.  If the inferior is
4207    executing, linux_nat_async_permitted is *not* updated.  */
4208 static int target_async_permitted_1 = 0;
4209
4210 static void
4211 set_maintenance_target_async_permitted (char *args, int from_tty,
4212                                         struct cmd_list_element *c)
4213 {
4214   if (have_live_inferiors ())
4215     {
4216       target_async_permitted_1 = target_async_permitted;
4217       error (_("Cannot change this setting while the inferior is running."));
4218     }
4219
4220   target_async_permitted = target_async_permitted_1;
4221 }
4222
4223 static void
4224 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
4225                                          struct cmd_list_element *c,
4226                                          const char *value)
4227 {
4228   fprintf_filtered (file,
4229                     _("Controlling the inferior in "
4230                       "asynchronous mode is %s.\n"), value);
4231 }
4232
4233 /* Temporary copies of permission settings.  */
4234
4235 static int may_write_registers_1 = 1;
4236 static int may_write_memory_1 = 1;
4237 static int may_insert_breakpoints_1 = 1;
4238 static int may_insert_tracepoints_1 = 1;
4239 static int may_insert_fast_tracepoints_1 = 1;
4240 static int may_stop_1 = 1;
4241
4242 /* Make the user-set values match the real values again.  */
4243
4244 void
4245 update_target_permissions (void)
4246 {
4247   may_write_registers_1 = may_write_registers;
4248   may_write_memory_1 = may_write_memory;
4249   may_insert_breakpoints_1 = may_insert_breakpoints;
4250   may_insert_tracepoints_1 = may_insert_tracepoints;
4251   may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
4252   may_stop_1 = may_stop;
4253 }
4254
4255 /* The one function handles (most of) the permission flags in the same
4256    way.  */
4257
4258 static void
4259 set_target_permissions (char *args, int from_tty,
4260                         struct cmd_list_element *c)
4261 {
4262   if (target_has_execution)
4263     {
4264       update_target_permissions ();
4265       error (_("Cannot change this setting while the inferior is running."));
4266     }
4267
4268   /* Make the real values match the user-changed values.  */
4269   may_write_registers = may_write_registers_1;
4270   may_insert_breakpoints = may_insert_breakpoints_1;
4271   may_insert_tracepoints = may_insert_tracepoints_1;
4272   may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
4273   may_stop = may_stop_1;
4274   update_observer_mode ();
4275 }
4276
4277 /* Set memory write permission independently of observer mode.  */
4278
4279 static void
4280 set_write_memory_permission (char *args, int from_tty,
4281                         struct cmd_list_element *c)
4282 {
4283   /* Make the real values match the user-changed values.  */
4284   may_write_memory = may_write_memory_1;
4285   update_observer_mode ();
4286 }
4287
4288
4289 void
4290 initialize_targets (void)
4291 {
4292   init_dummy_target ();
4293   push_target (&dummy_target);
4294
4295   add_info ("target", target_info, targ_desc);
4296   add_info ("files", target_info, targ_desc);
4297
4298   add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
4299 Set target debugging."), _("\
4300 Show target debugging."), _("\
4301 When non-zero, target debugging is enabled.  Higher numbers are more\n\
4302 verbose.  Changes do not take effect until the next \"run\" or \"target\"\n\
4303 command."),
4304                             NULL,
4305                             show_targetdebug,
4306                             &setdebuglist, &showdebuglist);
4307
4308   add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
4309                            &trust_readonly, _("\
4310 Set mode for reading from readonly sections."), _("\
4311 Show mode for reading from readonly sections."), _("\
4312 When this mode is on, memory reads from readonly sections (such as .text)\n\
4313 will be read from the object file instead of from the target.  This will\n\
4314 result in significant performance improvement for remote targets."),
4315                            NULL,
4316                            show_trust_readonly,
4317                            &setlist, &showlist);
4318
4319   add_com ("monitor", class_obscure, do_monitor_command,
4320            _("Send a command to the remote monitor (remote targets only)."));
4321
4322   add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
4323            _("Print the name of each layer of the internal target stack."),
4324            &maintenanceprintlist);
4325
4326   add_setshow_boolean_cmd ("target-async", no_class,
4327                            &target_async_permitted_1, _("\
4328 Set whether gdb controls the inferior in asynchronous mode."), _("\
4329 Show whether gdb controls the inferior in asynchronous mode."), _("\
4330 Tells gdb whether to control the inferior in asynchronous mode."),
4331                            set_maintenance_target_async_permitted,
4332                            show_maintenance_target_async_permitted,
4333                            &setlist,
4334                            &showlist);
4335
4336   add_setshow_boolean_cmd ("stack-cache", class_support,
4337                            &stack_cache_enabled_p_1, _("\
4338 Set cache use for stack access."), _("\
4339 Show cache use for stack access."), _("\
4340 When on, use the data cache for all stack access, regardless of any\n\
4341 configured memory regions.  This improves remote performance significantly.\n\
4342 By default, caching for stack access is on."),
4343                            set_stack_cache_enabled_p,
4344                            show_stack_cache_enabled_p,
4345                            &setlist, &showlist);
4346
4347   add_setshow_boolean_cmd ("may-write-registers", class_support,
4348                            &may_write_registers_1, _("\
4349 Set permission to write into registers."), _("\
4350 Show permission to write into registers."), _("\
4351 When this permission is on, GDB may write into the target's registers.\n\
4352 Otherwise, any sort of write attempt will result in an error."),
4353                            set_target_permissions, NULL,
4354                            &setlist, &showlist);
4355
4356   add_setshow_boolean_cmd ("may-write-memory", class_support,
4357                            &may_write_memory_1, _("\
4358 Set permission to write into target memory."), _("\
4359 Show permission to write into target memory."), _("\
4360 When this permission is on, GDB may write into the target's memory.\n\
4361 Otherwise, any sort of write attempt will result in an error."),
4362                            set_write_memory_permission, NULL,
4363                            &setlist, &showlist);
4364
4365   add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
4366                            &may_insert_breakpoints_1, _("\
4367 Set permission to insert breakpoints in the target."), _("\
4368 Show permission to insert breakpoints in the target."), _("\
4369 When this permission is on, GDB may insert breakpoints in the program.\n\
4370 Otherwise, any sort of insertion attempt will result in an error."),
4371                            set_target_permissions, NULL,
4372                            &setlist, &showlist);
4373
4374   add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
4375                            &may_insert_tracepoints_1, _("\
4376 Set permission to insert tracepoints in the target."), _("\
4377 Show permission to insert tracepoints in the target."), _("\
4378 When this permission is on, GDB may insert tracepoints in the program.\n\
4379 Otherwise, any sort of insertion attempt will result in an error."),
4380                            set_target_permissions, NULL,
4381                            &setlist, &showlist);
4382
4383   add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
4384                            &may_insert_fast_tracepoints_1, _("\
4385 Set permission to insert fast tracepoints in the target."), _("\
4386 Show permission to insert fast tracepoints in the target."), _("\
4387 When this permission is on, GDB may insert fast tracepoints.\n\
4388 Otherwise, any sort of insertion attempt will result in an error."),
4389                            set_target_permissions, NULL,
4390                            &setlist, &showlist);
4391
4392   add_setshow_boolean_cmd ("may-interrupt", class_support,
4393                            &may_stop_1, _("\
4394 Set permission to interrupt or signal the target."), _("\
4395 Show permission to interrupt or signal the target."), _("\
4396 When this permission is on, GDB may interrupt/stop the target's execution.\n\
4397 Otherwise, any attempt to interrupt or stop will be ignored."),
4398                            set_target_permissions, NULL,
4399                            &setlist, &showlist);
4400
4401
4402   target_dcache = dcache_init ();
4403 }