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