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