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