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