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