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