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