convert to_has_exited
[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
6b84065d
TT
81static int find_default_can_async_p (struct target_ops *ignore);
82
83static int find_default_is_async_p (struct target_ops *ignore);
84
1101cb7b
TT
85#include "target-delegates.c"
86
a14ed312 87static void init_dummy_target (void);
c906108c 88
aa869812
AC
89static struct target_ops debug_target;
90
a14ed312 91static void debug_to_open (char *, int);
c906108c 92
f32dbf8c
MM
93static void debug_to_prepare_to_store (struct target_ops *self,
94 struct regcache *);
c906108c 95
a14ed312 96static void debug_to_files_info (struct target_ops *);
c906108c 97
3db08215 98static int debug_to_insert_breakpoint (struct target_ops *, struct gdbarch *,
a6d9a66e 99 struct bp_target_info *);
c906108c 100
3db08215 101static int debug_to_remove_breakpoint (struct target_ops *, struct gdbarch *,
a6d9a66e 102 struct bp_target_info *);
c906108c 103
5461485a
TT
104static int debug_to_can_use_hw_breakpoint (struct target_ops *self,
105 int, int, int);
ccaa32c7 106
23a26771
TT
107static int debug_to_insert_hw_breakpoint (struct target_ops *self,
108 struct gdbarch *,
a6d9a66e 109 struct bp_target_info *);
ccaa32c7 110
a64dc96c
TT
111static int debug_to_remove_hw_breakpoint (struct target_ops *self,
112 struct gdbarch *,
a6d9a66e 113 struct bp_target_info *);
ccaa32c7 114
7bb99c53
TT
115static int debug_to_insert_watchpoint (struct target_ops *self,
116 CORE_ADDR, int, int,
0cf6dd15 117 struct expression *);
ccaa32c7 118
11b5219a
TT
119static int debug_to_remove_watchpoint (struct target_ops *self,
120 CORE_ADDR, int, int,
0cf6dd15 121 struct expression *);
ccaa32c7 122
4aa7a7f5 123static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
ccaa32c7 124
5009afc5
AS
125static int debug_to_watchpoint_addr_within_range (struct target_ops *,
126 CORE_ADDR, CORE_ADDR, int);
127
31568a15
TT
128static int debug_to_region_ok_for_hw_watchpoint (struct target_ops *self,
129 CORE_ADDR, int);
e0d24f8d 130
c3a5ff89
TT
131static int debug_to_can_accel_watchpoint_condition (struct target_ops *self,
132 CORE_ADDR, int, int,
0cf6dd15
TJB
133 struct expression *);
134
c42bf286 135static void debug_to_terminal_init (struct target_ops *self);
c906108c 136
d2f640d4 137static void debug_to_terminal_inferior (struct target_ops *self);
c906108c 138
2e1e1a19 139static void debug_to_terminal_ours_for_output (struct target_ops *self);
c906108c 140
ae3bd431 141static void debug_to_terminal_save_ours (struct target_ops *self);
a790ad35 142
e3594fd1 143static void debug_to_terminal_ours (struct target_ops *self);
c906108c 144
71a9f134 145static void debug_to_load (struct target_ops *self, char *, int);
c906108c 146
da82bd6b 147static int debug_to_can_run (struct target_ops *self);
c906108c 148
1eab8a48 149static void debug_to_stop (struct target_ops *self, ptid_t);
c906108c 150
c906108c 151/* Pointer to array of target architecture structures; the size of the
2bc416ba 152 array; the current index into the array; the allocated size of the
c906108c
SS
153 array. */
154struct target_ops **target_structs;
155unsigned target_struct_size;
c906108c
SS
156unsigned target_struct_allocsize;
157#define DEFAULT_ALLOCSIZE 10
158
159/* The initial current target, so that there is always a semi-valid
160 current target. */
161
162static struct target_ops dummy_target;
163
164/* Top of target stack. */
165
258b763a 166static struct target_ops *target_stack;
c906108c
SS
167
168/* The target structure we are currently using to talk to a process
169 or file or whatever "inferior" we have. */
170
171struct target_ops current_target;
172
173/* Command list for target. */
174
175static struct cmd_list_element *targetlist = NULL;
176
cf7a04e8
DJ
177/* Nonzero if we should trust readonly sections from the
178 executable when reading memory. */
179
180static int trust_readonly = 0;
181
8defab1a
DJ
182/* Nonzero if we should show true memory content including
183 memory breakpoint inserted by gdb. */
184
185static int show_memory_breakpoints = 0;
186
d914c394
SS
187/* These globals control whether GDB attempts to perform these
188 operations; they are useful for targets that need to prevent
189 inadvertant disruption, such as in non-stop mode. */
190
191int may_write_registers = 1;
192
193int may_write_memory = 1;
194
195int may_insert_breakpoints = 1;
196
197int may_insert_tracepoints = 1;
198
199int may_insert_fast_tracepoints = 1;
200
201int may_stop = 1;
202
c906108c
SS
203/* Non-zero if we want to see trace of target level stuff. */
204
ccce17b0 205static unsigned int targetdebug = 0;
920d2a44
AC
206static void
207show_targetdebug (struct ui_file *file, int from_tty,
208 struct cmd_list_element *c, const char *value)
209{
210 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
211}
c906108c 212
a14ed312 213static void setup_target_debug (void);
c906108c 214
c906108c
SS
215/* The user just typed 'target' without the name of a target. */
216
c906108c 217static void
fba45db2 218target_command (char *arg, int from_tty)
c906108c
SS
219{
220 fputs_filtered ("Argument required (target name). Try `help target'\n",
221 gdb_stdout);
222}
223
c35b1492
PA
224/* Default target_has_* methods for process_stratum targets. */
225
226int
227default_child_has_all_memory (struct target_ops *ops)
228{
229 /* If no inferior selected, then we can't read memory here. */
230 if (ptid_equal (inferior_ptid, null_ptid))
231 return 0;
232
233 return 1;
234}
235
236int
237default_child_has_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_stack (struct target_ops *ops)
248{
249 /* If no inferior selected, there's no stack. */
250 if (ptid_equal (inferior_ptid, null_ptid))
251 return 0;
252
253 return 1;
254}
255
256int
257default_child_has_registers (struct target_ops *ops)
258{
259 /* Can't read registers from no inferior. */
260 if (ptid_equal (inferior_ptid, null_ptid))
261 return 0;
262
263 return 1;
264}
265
266int
aeaec162 267default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
c35b1492
PA
268{
269 /* If there's no thread selected, then we can't make it run through
270 hoops. */
aeaec162 271 if (ptid_equal (the_ptid, null_ptid))
c35b1492
PA
272 return 0;
273
274 return 1;
275}
276
277
278int
279target_has_all_memory_1 (void)
280{
281 struct target_ops *t;
282
283 for (t = current_target.beneath; t != NULL; t = t->beneath)
284 if (t->to_has_all_memory (t))
285 return 1;
286
287 return 0;
288}
289
290int
291target_has_memory_1 (void)
292{
293 struct target_ops *t;
294
295 for (t = current_target.beneath; t != NULL; t = t->beneath)
296 if (t->to_has_memory (t))
297 return 1;
298
299 return 0;
300}
301
302int
303target_has_stack_1 (void)
304{
305 struct target_ops *t;
306
307 for (t = current_target.beneath; t != NULL; t = t->beneath)
308 if (t->to_has_stack (t))
309 return 1;
310
311 return 0;
312}
313
314int
315target_has_registers_1 (void)
316{
317 struct target_ops *t;
318
319 for (t = current_target.beneath; t != NULL; t = t->beneath)
320 if (t->to_has_registers (t))
321 return 1;
322
323 return 0;
324}
325
326int
aeaec162 327target_has_execution_1 (ptid_t the_ptid)
c35b1492
PA
328{
329 struct target_ops *t;
330
331 for (t = current_target.beneath; t != NULL; t = t->beneath)
aeaec162 332 if (t->to_has_execution (t, the_ptid))
c35b1492
PA
333 return 1;
334
335 return 0;
336}
337
aeaec162
TT
338int
339target_has_execution_current (void)
340{
341 return target_has_execution_1 (inferior_ptid);
342}
343
c22a2b88
TT
344/* Complete initialization of T. This ensures that various fields in
345 T are set, if needed by the target implementation. */
c906108c
SS
346
347void
c22a2b88 348complete_target_initialization (struct target_ops *t)
c906108c 349{
0088c768 350 /* Provide default values for all "must have" methods. */
0b603eba
AC
351 if (t->to_xfer_partial == NULL)
352 t->to_xfer_partial = default_xfer_partial;
0088c768 353
c35b1492
PA
354 if (t->to_has_all_memory == NULL)
355 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
356
357 if (t->to_has_memory == NULL)
358 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
359
360 if (t->to_has_stack == NULL)
361 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
362
363 if (t->to_has_registers == NULL)
364 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
365
366 if (t->to_has_execution == NULL)
aeaec162 367 t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
1101cb7b
TT
368
369 install_delegators (t);
c22a2b88
TT
370}
371
372/* Add possible target architecture T to the list and add a new
373 command 'target T->to_shortname'. Set COMPLETER as the command's
374 completer if not NULL. */
375
376void
377add_target_with_completer (struct target_ops *t,
378 completer_ftype *completer)
379{
380 struct cmd_list_element *c;
381
382 complete_target_initialization (t);
c35b1492 383
c906108c
SS
384 if (!target_structs)
385 {
386 target_struct_allocsize = DEFAULT_ALLOCSIZE;
387 target_structs = (struct target_ops **) xmalloc
388 (target_struct_allocsize * sizeof (*target_structs));
389 }
390 if (target_struct_size >= target_struct_allocsize)
391 {
392 target_struct_allocsize *= 2;
393 target_structs = (struct target_ops **)
c5aa993b
JM
394 xrealloc ((char *) target_structs,
395 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
396 }
397 target_structs[target_struct_size++] = t;
c906108c
SS
398
399 if (targetlist == NULL)
1bedd215
AC
400 add_prefix_cmd ("target", class_run, target_command, _("\
401Connect to a target machine or process.\n\
c906108c
SS
402The first argument is the type or protocol of the target machine.\n\
403Remaining arguments are interpreted by the target protocol. For more\n\
404information on the arguments for a particular protocol, type\n\
1bedd215 405`help target ' followed by the protocol name."),
c906108c 406 &targetlist, "target ", 0, &cmdlist);
9852c492
YQ
407 c = add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc,
408 &targetlist);
409 if (completer != NULL)
410 set_cmd_completer (c, completer);
411}
412
413/* Add a possible target architecture to the list. */
414
415void
416add_target (struct target_ops *t)
417{
418 add_target_with_completer (t, NULL);
c906108c
SS
419}
420
b48d48eb
MM
421/* See target.h. */
422
423void
424add_deprecated_target_alias (struct target_ops *t, char *alias)
425{
426 struct cmd_list_element *c;
427 char *alt;
428
429 /* If we use add_alias_cmd, here, we do not get the deprecated warning,
430 see PR cli/15104. */
431 c = add_cmd (alias, no_class, t->to_open, t->to_doc, &targetlist);
432 alt = xstrprintf ("target %s", t->to_shortname);
433 deprecate_cmd (c, alt);
434}
435
c906108c
SS
436/* Stub functions */
437
438void
fba45db2 439target_ignore (void)
c906108c
SS
440{
441}
442
7d85a9c0
JB
443void
444target_kill (void)
445{
446 struct target_ops *t;
447
448 for (t = current_target.beneath; t != NULL; t = t->beneath)
449 if (t->to_kill != NULL)
450 {
451 if (targetdebug)
452 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
453
454 t->to_kill (t);
455 return;
456 }
457
458 noprocess ();
459}
460
11cf8741
JM
461void
462target_load (char *arg, int from_tty)
463{
4e5d721f 464 target_dcache_invalidate ();
71a9f134 465 (*current_target.to_load) (&current_target, arg, from_tty);
11cf8741
JM
466}
467
947b8855
PA
468void
469target_create_inferior (char *exec_file, char *args,
470 char **env, int from_tty)
136d6dae
VP
471{
472 struct target_ops *t;
5d502164 473
136d6dae
VP
474 for (t = current_target.beneath; t != NULL; t = t->beneath)
475 {
476 if (t->to_create_inferior != NULL)
477 {
478 t->to_create_inferior (t, exec_file, args, env, from_tty);
947b8855
PA
479 if (targetdebug)
480 fprintf_unfiltered (gdb_stdlog,
481 "target_create_inferior (%s, %s, xxx, %d)\n",
482 exec_file, args, from_tty);
136d6dae
VP
483 return;
484 }
485 }
486
487 internal_error (__FILE__, __LINE__,
9b20d036 488 _("could not find a target to create inferior"));
136d6dae
VP
489}
490
d9d2d8b6
PA
491void
492target_terminal_inferior (void)
493{
494 /* A background resume (``run&'') should leave GDB in control of the
c378eb4e 495 terminal. Use target_can_async_p, not target_is_async_p, since at
ba7f6c64
VP
496 this point the target is not async yet. However, if sync_execution
497 is not set, we know it will become async prior to resume. */
498 if (target_can_async_p () && !sync_execution)
d9d2d8b6
PA
499 return;
500
501 /* If GDB is resuming the inferior in the foreground, install
502 inferior's terminal modes. */
d2f640d4 503 (*current_target.to_terminal_inferior) (&current_target);
d9d2d8b6 504}
136d6dae 505
c906108c 506static int
fba45db2
KB
507nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
508 struct target_ops *t)
c906108c 509{
c378eb4e
MS
510 errno = EIO; /* Can't read/write this location. */
511 return 0; /* No bytes handled. */
c906108c
SS
512}
513
514static void
fba45db2 515tcomplain (void)
c906108c 516{
8a3fe4f8 517 error (_("You can't do that when your target is `%s'"),
c906108c
SS
518 current_target.to_shortname);
519}
520
521void
fba45db2 522noprocess (void)
c906108c 523{
8a3fe4f8 524 error (_("You can't do that without a process to debug."));
c906108c
SS
525}
526
c906108c 527static void
0a4f40a2 528default_terminal_info (struct target_ops *self, const char *args, int from_tty)
c906108c 529{
a3f17187 530 printf_unfiltered (_("No saved terminal information.\n"));
c906108c
SS
531}
532
0ef643c8
JB
533/* A default implementation for the to_get_ada_task_ptid target method.
534
535 This function builds the PTID by using both LWP and TID as part of
536 the PTID lwp and tid elements. The pid used is the pid of the
537 inferior_ptid. */
538
2c0b251b 539static ptid_t
1e6b91a4 540default_get_ada_task_ptid (struct target_ops *self, long lwp, long tid)
0ef643c8
JB
541{
542 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
543}
544
32231432 545static enum exec_direction_kind
4c612759 546default_execution_direction (struct target_ops *self)
32231432
PA
547{
548 if (!target_can_execute_reverse)
549 return EXEC_FORWARD;
550 else if (!target_can_async_p ())
551 return EXEC_FORWARD;
552 else
553 gdb_assert_not_reached ("\
554to_execution_direction must be implemented for reverse async");
555}
556
7998dfc3
AC
557/* Go through the target stack from top to bottom, copying over zero
558 entries in current_target, then filling in still empty entries. In
559 effect, we are doing class inheritance through the pushed target
560 vectors.
561
562 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
563 is currently implemented, is that it discards any knowledge of
564 which target an inherited method originally belonged to.
565 Consequently, new new target methods should instead explicitly and
566 locally search the target stack for the target that can handle the
567 request. */
c906108c
SS
568
569static void
7998dfc3 570update_current_target (void)
c906108c 571{
7998dfc3
AC
572 struct target_ops *t;
573
08d8bcd7 574 /* First, reset current's contents. */
7998dfc3
AC
575 memset (&current_target, 0, sizeof (current_target));
576
1101cb7b
TT
577 /* Install the delegators. */
578 install_delegators (&current_target);
579
7998dfc3
AC
580#define INHERIT(FIELD, TARGET) \
581 if (!current_target.FIELD) \
582 current_target.FIELD = (TARGET)->FIELD
583
584 for (t = target_stack; t; t = t->beneath)
585 {
586 INHERIT (to_shortname, t);
587 INHERIT (to_longname, t);
588 INHERIT (to_doc, t);
b52323fa
UW
589 /* Do not inherit to_open. */
590 /* Do not inherit to_close. */
136d6dae 591 /* Do not inherit to_attach. */
bebd3233 592 /* Do not inherit to_post_attach. */
dc177b7a 593 INHERIT (to_attach_no_wait, t);
136d6dae 594 /* Do not inherit to_detach. */
597320e7 595 /* Do not inherit to_disconnect. */
28439f5e 596 /* Do not inherit to_resume. */
117de6a9 597 /* Do not inherit to_wait. */
28439f5e
PA
598 /* Do not inherit to_fetch_registers. */
599 /* Do not inherit to_store_registers. */
6c628163 600 /* Do not inherit to_prepare_to_store. */
c8e73a31 601 INHERIT (deprecated_xfer_memory, t);
f86e59b2 602 /* Do not inherit to_files_info. */
3db08215
MM
603 /* Do not inherit to_insert_breakpoint. */
604 /* Do not inherit to_remove_breakpoint. */
52b51d06 605 /* Do not inherit to_can_use_hw_breakpoint. */
61b371f9 606 /* Do not inherit to_insert_hw_breakpoint. */
418dabac 607 /* Do not inherit to_remove_hw_breakpoint. */
f1310107 608 /* Do not inherit to_ranged_break_num_registers. */
016facd4 609 /* Do not inherit to_insert_watchpoint. */
61dd109f 610 /* Do not inherit to_remove_watchpoint. */
9c06b0b4
TJB
611 /* Do not inherit to_insert_mask_watchpoint. */
612 /* Do not inherit to_remove_mask_watchpoint. */
6b84065d 613 /* Do not inherit to_stopped_data_address. */
74174d2e 614 INHERIT (to_have_steppable_watchpoint, t);
7998dfc3 615 INHERIT (to_have_continuable_watchpoint, t);
6b84065d 616 /* Do not inherit to_stopped_by_watchpoint. */
65f160a9 617 /* Do not inherit to_watchpoint_addr_within_range. */
d03655e4 618 /* Do not inherit to_region_ok_for_hw_watchpoint. */
77cdffe9 619 /* Do not inherit to_can_accel_watchpoint_condition. */
9c06b0b4 620 /* Do not inherit to_masked_watch_num_registers. */
0343661d 621 /* Do not inherit to_terminal_init. */
ddeaacc9 622 /* Do not inherit to_terminal_inferior. */
74fcbef9 623 /* Do not inherit to_terminal_ours_for_output. */
e4a733f1 624 /* Do not inherit to_terminal_ours. */
c6ea8f79 625 /* Do not inherit to_terminal_save_ours. */
e19e919f 626 /* Do not inherit to_terminal_info. */
7d85a9c0 627 /* Do not inherit to_kill. */
7634da87 628 /* Do not inherit to_load. */
136d6dae 629 /* Do no inherit to_create_inferior. */
340ba4bf 630 /* Do not inherit to_post_startup_inferior. */
5958ebeb 631 /* Do not inherit to_insert_fork_catchpoint. */
e1a21fb7 632 /* Do not inherit to_remove_fork_catchpoint. */
7e18a8dc 633 /* Do not inherit to_insert_vfork_catchpoint. */
95c3375e 634 /* Do not inherit to_remove_vfork_catchpoint. */
ee057212 635 /* Do not inherit to_follow_fork. */
62f64d7a 636 /* Do not inherit to_insert_exec_catchpoint. */
cda0f38c 637 /* Do not inherit to_remove_exec_catchpoint. */
6a9fa051 638 /* Do not inherit to_set_syscall_catchpoint. */
0db88c1d 639 /* Do not inherit to_has_exited. */
82892036 640 /* Do not inherit to_mourn_inferior. */
7998dfc3 641 INHERIT (to_can_run, t);
2455069d 642 /* Do not inherit to_pass_signals. */
9b224c5e 643 /* Do not inherit to_program_signals. */
28439f5e
PA
644 /* Do not inherit to_thread_alive. */
645 /* Do not inherit to_find_new_threads. */
117de6a9 646 /* Do not inherit to_pid_to_str. */
7998dfc3 647 INHERIT (to_extra_thread_info, t);
4694da01 648 INHERIT (to_thread_name, t);
7998dfc3 649 INHERIT (to_stop, t);
4b8a223f 650 /* Do not inherit to_xfer_partial. */
a53f3625 651 /* Do not inherit to_rcmd. */
7998dfc3 652 INHERIT (to_pid_to_exec_file, t);
49d03eab 653 INHERIT (to_log_command, t);
7998dfc3 654 INHERIT (to_stratum, t);
c378eb4e
MS
655 /* Do not inherit to_has_all_memory. */
656 /* Do not inherit to_has_memory. */
657 /* Do not inherit to_has_stack. */
658 /* Do not inherit to_has_registers. */
659 /* Do not inherit to_has_execution. */
7998dfc3 660 INHERIT (to_has_thread_control, t);
6b84065d
TT
661 /* Do not inherit to_can_async_p. */
662 /* Do not inherit to_is_async_p. */
663 /* Do not inherit to_async. */
7998dfc3
AC
664 INHERIT (to_find_memory_regions, t);
665 INHERIT (to_make_corefile_notes, t);
6b04bdb7
MS
666 INHERIT (to_get_bookmark, t);
667 INHERIT (to_goto_bookmark, t);
117de6a9 668 /* Do not inherit to_get_thread_local_address. */
b2175913 669 INHERIT (to_can_execute_reverse, t);
32231432 670 INHERIT (to_execution_direction, t);
c2250ad1 671 INHERIT (to_thread_architecture, t);
424163ea 672 /* Do not inherit to_read_description. */
0ef643c8 673 INHERIT (to_get_ada_task_ptid, t);
08388c79 674 /* Do not inherit to_search_memory. */
8a305172 675 INHERIT (to_supports_multi_process, t);
d248b706 676 INHERIT (to_supports_enable_disable_tracepoint, t);
3065dfb6 677 INHERIT (to_supports_string_tracing, t);
35b1e5cc
SS
678 INHERIT (to_trace_init, t);
679 INHERIT (to_download_tracepoint, t);
1e4d1764 680 INHERIT (to_can_download_tracepoint, t);
35b1e5cc 681 INHERIT (to_download_trace_state_variable, t);
d248b706
KY
682 INHERIT (to_enable_tracepoint, t);
683 INHERIT (to_disable_tracepoint, t);
35b1e5cc
SS
684 INHERIT (to_trace_set_readonly_regions, t);
685 INHERIT (to_trace_start, t);
686 INHERIT (to_get_trace_status, t);
f196051f 687 INHERIT (to_get_tracepoint_status, t);
35b1e5cc
SS
688 INHERIT (to_trace_stop, t);
689 INHERIT (to_trace_find, t);
690 INHERIT (to_get_trace_state_variable_value, t);
00bf0b85
SS
691 INHERIT (to_save_trace_data, t);
692 INHERIT (to_upload_tracepoints, t);
693 INHERIT (to_upload_trace_state_variables, t);
694 INHERIT (to_get_raw_trace_data, t);
405f8e94 695 INHERIT (to_get_min_fast_tracepoint_insn_len, t);
35b1e5cc 696 INHERIT (to_set_disconnected_tracing, t);
4daf5ac0 697 INHERIT (to_set_circular_trace_buffer, t);
f6f899bf 698 INHERIT (to_set_trace_buffer_size, t);
f196051f 699 INHERIT (to_set_trace_notes, t);
711e434b 700 INHERIT (to_get_tib_address, t);
d914c394 701 INHERIT (to_set_permissions, t);
0fb4aa4b
PA
702 INHERIT (to_static_tracepoint_marker_at, t);
703 INHERIT (to_static_tracepoint_markers_by_strid, t);
b3b9301e 704 INHERIT (to_traceframe_info, t);
d1feda86
YQ
705 INHERIT (to_use_agent, t);
706 INHERIT (to_can_use_agent, t);
ced63ec0 707 INHERIT (to_augmented_libraries_svr4_read, t);
7998dfc3 708 INHERIT (to_magic, t);
b775012e 709 INHERIT (to_supports_evaluation_of_breakpoint_conditions, t);
d3ce09f5 710 INHERIT (to_can_run_breakpoint_commands, t);
fd79ecee 711 /* Do not inherit to_memory_map. */
a76d924d
DJ
712 /* Do not inherit to_flash_erase. */
713 /* Do not inherit to_flash_done. */
7998dfc3
AC
714 }
715#undef INHERIT
716
717 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
718 it. Some entries are defaulted to a method that print an error,
719 others are hard-wired to a standard recursive default. */
c906108c
SS
720
721#define de_fault(field, value) \
7998dfc3
AC
722 if (!current_target.field) \
723 current_target.field = value
0d06e24b 724
2bc416ba
DJ
725 de_fault (to_open,
726 (void (*) (char *, int))
0d06e24b 727 tcomplain);
2bc416ba 728 de_fault (to_close,
de90e03d 729 (void (*) (struct target_ops *))
0d06e24b 730 target_ignore);
2bc416ba 731 de_fault (deprecated_xfer_memory,
3e43a32a
MS
732 (int (*) (CORE_ADDR, gdb_byte *, int, int,
733 struct mem_attrib *, struct target_ops *))
0d06e24b 734 nomemory);
2bc416ba 735 de_fault (to_can_run,
da82bd6b 736 (int (*) (struct target_ops *))
0d06e24b 737 return_zero);
2bc416ba 738 de_fault (to_extra_thread_info,
c15906d8 739 (char *(*) (struct target_ops *, struct thread_info *))
1b67eb02 740 return_null);
4694da01 741 de_fault (to_thread_name,
503a628d 742 (char *(*) (struct target_ops *, struct thread_info *))
1b67eb02 743 return_null);
2bc416ba 744 de_fault (to_stop,
1eab8a48 745 (void (*) (struct target_ops *, ptid_t))
0d06e24b 746 target_ignore);
2bc416ba 747 de_fault (to_pid_to_exec_file,
8dd27370 748 (char *(*) (struct target_ops *, int))
1b67eb02 749 return_null);
c2250ad1
UW
750 de_fault (to_thread_architecture,
751 default_thread_architecture);
424163ea 752 current_target.to_read_description = NULL;
0ef643c8 753 de_fault (to_get_ada_task_ptid,
1e6b91a4 754 (ptid_t (*) (struct target_ops *, long, long))
0ef643c8 755 default_get_ada_task_ptid);
8a305172 756 de_fault (to_supports_multi_process,
86ce2668 757 (int (*) (struct target_ops *))
8a305172 758 return_zero);
d248b706 759 de_fault (to_supports_enable_disable_tracepoint,
7d178d6a 760 (int (*) (struct target_ops *))
d248b706 761 return_zero);
3065dfb6 762 de_fault (to_supports_string_tracing,
6de37a3a 763 (int (*) (struct target_ops *))
3065dfb6 764 return_zero);
35b1e5cc 765 de_fault (to_trace_init,
ecae04e1 766 (void (*) (struct target_ops *))
35b1e5cc
SS
767 tcomplain);
768 de_fault (to_download_tracepoint,
548f7808 769 (void (*) (struct target_ops *, struct bp_location *))
35b1e5cc 770 tcomplain);
1e4d1764 771 de_fault (to_can_download_tracepoint,
a52a8357 772 (int (*) (struct target_ops *))
1e4d1764 773 return_zero);
35b1e5cc 774 de_fault (to_download_trace_state_variable,
559d2b81 775 (void (*) (struct target_ops *, struct trace_state_variable *))
35b1e5cc 776 tcomplain);
d248b706 777 de_fault (to_enable_tracepoint,
46670d57 778 (void (*) (struct target_ops *, struct bp_location *))
d248b706
KY
779 tcomplain);
780 de_fault (to_disable_tracepoint,
780b049c 781 (void (*) (struct target_ops *, struct bp_location *))
d248b706 782 tcomplain);
35b1e5cc 783 de_fault (to_trace_set_readonly_regions,
583f9a86 784 (void (*) (struct target_ops *))
35b1e5cc
SS
785 tcomplain);
786 de_fault (to_trace_start,
e2d1aae3 787 (void (*) (struct target_ops *))
35b1e5cc
SS
788 tcomplain);
789 de_fault (to_get_trace_status,
8bd200f1 790 (int (*) (struct target_ops *, struct trace_status *))
35b1e5cc 791 return_minus_one);
f196051f 792 de_fault (to_get_tracepoint_status,
db90e85c
TT
793 (void (*) (struct target_ops *, struct breakpoint *,
794 struct uploaded_tp *))
f196051f 795 tcomplain);
35b1e5cc 796 de_fault (to_trace_stop,
74499f1b 797 (void (*) (struct target_ops *))
35b1e5cc
SS
798 tcomplain);
799 de_fault (to_trace_find,
bd4c6793
TT
800 (int (*) (struct target_ops *,
801 enum trace_find_type, int, CORE_ADDR, CORE_ADDR, int *))
4136fdd2 802 return_minus_one);
35b1e5cc 803 de_fault (to_get_trace_state_variable_value,
4011015b 804 (int (*) (struct target_ops *, int, LONGEST *))
35b1e5cc 805 return_zero);
00bf0b85 806 de_fault (to_save_trace_data,
dc3decaf 807 (int (*) (struct target_ops *, const char *))
00bf0b85
SS
808 tcomplain);
809 de_fault (to_upload_tracepoints,
ab6617cc 810 (int (*) (struct target_ops *, struct uploaded_tp **))
00bf0b85
SS
811 return_zero);
812 de_fault (to_upload_trace_state_variables,
181e3713 813 (int (*) (struct target_ops *, struct uploaded_tsv **))
00bf0b85
SS
814 return_zero);
815 de_fault (to_get_raw_trace_data,
88ee6f45 816 (LONGEST (*) (struct target_ops *, gdb_byte *, ULONGEST, LONGEST))
00bf0b85 817 tcomplain);
405f8e94 818 de_fault (to_get_min_fast_tracepoint_insn_len,
0e67620a 819 (int (*) (struct target_ops *))
405f8e94 820 return_minus_one);
35b1e5cc 821 de_fault (to_set_disconnected_tracing,
37b25738 822 (void (*) (struct target_ops *, int))
4daf5ac0
SS
823 target_ignore);
824 de_fault (to_set_circular_trace_buffer,
736d5b1f 825 (void (*) (struct target_ops *, int))
4daf5ac0 826 target_ignore);
f6f899bf 827 de_fault (to_set_trace_buffer_size,
4da384be 828 (void (*) (struct target_ops *, LONGEST))
f6f899bf 829 target_ignore);
f196051f 830 de_fault (to_set_trace_notes,
d9e68a2c
TT
831 (int (*) (struct target_ops *,
832 const char *, const char *, const char *))
f196051f 833 return_zero);
711e434b 834 de_fault (to_get_tib_address,
bd7ae0f5 835 (int (*) (struct target_ops *, ptid_t, CORE_ADDR *))
711e434b 836 tcomplain);
d914c394 837 de_fault (to_set_permissions,
c378d69d 838 (void (*) (struct target_ops *))
d914c394 839 target_ignore);
0fb4aa4b 840 de_fault (to_static_tracepoint_marker_at,
61fc905d
TT
841 (int (*) (struct target_ops *,
842 CORE_ADDR, struct static_tracepoint_marker *))
0fb4aa4b
PA
843 return_zero);
844 de_fault (to_static_tracepoint_markers_by_strid,
c686c57f
TT
845 (VEC(static_tracepoint_marker_p) * (*) (struct target_ops *,
846 const char *))
0fb4aa4b 847 tcomplain);
b3b9301e 848 de_fault (to_traceframe_info,
a893e81f 849 (struct traceframe_info * (*) (struct target_ops *))
1b67eb02 850 return_null);
b775012e 851 de_fault (to_supports_evaluation_of_breakpoint_conditions,
efcc2da7 852 (int (*) (struct target_ops *))
b775012e 853 return_zero);
d3ce09f5 854 de_fault (to_can_run_breakpoint_commands,
78eff0ec 855 (int (*) (struct target_ops *))
d3ce09f5 856 return_zero);
d1feda86 857 de_fault (to_use_agent,
2c152180 858 (int (*) (struct target_ops *, int))
d1feda86
YQ
859 tcomplain);
860 de_fault (to_can_use_agent,
fe38f897 861 (int (*) (struct target_ops *))
d1feda86 862 return_zero);
ced63ec0 863 de_fault (to_augmented_libraries_svr4_read,
5436ff03 864 (int (*) (struct target_ops *))
ced63ec0 865 return_zero);
32231432
PA
866 de_fault (to_execution_direction, default_execution_direction);
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{
2679 struct target_ops *t;
2680
2681 for (t = current_target.beneath; t != NULL; t = t->beneath)
2682 {
2683 if (t->to_thread_name != NULL)
503a628d 2684 return (*t->to_thread_name) (t, info);
4694da01
TT
2685 }
2686
2687 return NULL;
2688}
2689
e1ac3328 2690void
2ea28649 2691target_resume (ptid_t ptid, int step, enum gdb_signal signal)
e1ac3328 2692{
28439f5e
PA
2693 struct target_ops *t;
2694
4e5d721f 2695 target_dcache_invalidate ();
28439f5e 2696
6b84065d
TT
2697 current_target.to_resume (&current_target, ptid, step, signal);
2698 if (targetdebug)
2699 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2700 ptid_get_pid (ptid),
2701 step ? "step" : "continue",
2702 gdb_signal_to_name (signal));
28439f5e 2703
6b84065d
TT
2704 registers_changed_ptid (ptid);
2705 set_executing (ptid, 1);
2706 set_running (ptid, 1);
2707 clear_inline_frame_state (ptid);
e1ac3328 2708}
2455069d
UW
2709
2710void
2711target_pass_signals (int numsigs, unsigned char *pass_signals)
2712{
2713 struct target_ops *t;
2714
2715 for (t = current_target.beneath; t != NULL; t = t->beneath)
2716 {
2717 if (t->to_pass_signals != NULL)
2718 {
2719 if (targetdebug)
2720 {
2721 int i;
2722
2723 fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2724 numsigs);
2725
2726 for (i = 0; i < numsigs; i++)
2727 if (pass_signals[i])
2728 fprintf_unfiltered (gdb_stdlog, " %s",
2ea28649 2729 gdb_signal_to_name (i));
2455069d
UW
2730
2731 fprintf_unfiltered (gdb_stdlog, " })\n");
2732 }
2733
94bedb42 2734 (*t->to_pass_signals) (t, numsigs, pass_signals);
2455069d
UW
2735 return;
2736 }
2737 }
2738}
2739
9b224c5e
PA
2740void
2741target_program_signals (int numsigs, unsigned char *program_signals)
2742{
2743 struct target_ops *t;
2744
2745 for (t = current_target.beneath; t != NULL; t = t->beneath)
2746 {
2747 if (t->to_program_signals != NULL)
2748 {
2749 if (targetdebug)
2750 {
2751 int i;
2752
2753 fprintf_unfiltered (gdb_stdlog, "target_program_signals (%d, {",
2754 numsigs);
2755
2756 for (i = 0; i < numsigs; i++)
2757 if (program_signals[i])
2758 fprintf_unfiltered (gdb_stdlog, " %s",
2ea28649 2759 gdb_signal_to_name (i));
9b224c5e
PA
2760
2761 fprintf_unfiltered (gdb_stdlog, " })\n");
2762 }
2763
daf5e9b6 2764 (*t->to_program_signals) (t, numsigs, program_signals);
9b224c5e
PA
2765 return;
2766 }
2767 }
2768}
2769
ee057212
DJ
2770/* Look through the list of possible targets for a target that can
2771 follow forks. */
2772
2773int
07107ca6 2774target_follow_fork (int follow_child, int detach_fork)
ee057212
DJ
2775{
2776 struct target_ops *t;
2777
2778 for (t = current_target.beneath; t != NULL; t = t->beneath)
2779 {
2780 if (t->to_follow_fork != NULL)
2781 {
07107ca6 2782 int retval = t->to_follow_fork (t, follow_child, detach_fork);
5d502164 2783
ee057212 2784 if (targetdebug)
07107ca6
LM
2785 fprintf_unfiltered (gdb_stdlog,
2786 "target_follow_fork (%d, %d) = %d\n",
2787 follow_child, detach_fork, retval);
ee057212
DJ
2788 return retval;
2789 }
2790 }
2791
2792 /* Some target returned a fork event, but did not know how to follow it. */
2793 internal_error (__FILE__, __LINE__,
9b20d036 2794 _("could not find a target to follow fork"));
ee057212
DJ
2795}
2796
136d6dae
VP
2797void
2798target_mourn_inferior (void)
2799{
2800 struct target_ops *t;
5d502164 2801
136d6dae
VP
2802 for (t = current_target.beneath; t != NULL; t = t->beneath)
2803 {
2804 if (t->to_mourn_inferior != NULL)
2805 {
2806 t->to_mourn_inferior (t);
947b8855
PA
2807 if (targetdebug)
2808 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2809
2810 /* We no longer need to keep handles on any of the object files.
2811 Make sure to release them to avoid unnecessarily locking any
2812 of them while we're not actually debugging. */
2813 bfd_cache_close_all ();
2814
136d6dae
VP
2815 return;
2816 }
2817 }
2818
2819 internal_error (__FILE__, __LINE__,
9b20d036 2820 _("could not find a target to follow mourn inferior"));
136d6dae
VP
2821}
2822
424163ea
DJ
2823/* Look for a target which can describe architectural features, starting
2824 from TARGET. If we find one, return its description. */
2825
2826const struct target_desc *
2827target_read_description (struct target_ops *target)
2828{
2829 struct target_ops *t;
2830
2831 for (t = target; t != NULL; t = t->beneath)
2832 if (t->to_read_description != NULL)
2833 {
2834 const struct target_desc *tdesc;
2835
2836 tdesc = t->to_read_description (t);
2837 if (tdesc)
2838 return tdesc;
2839 }
2840
2841 return NULL;
2842}
2843
08388c79
DE
2844/* The default implementation of to_search_memory.
2845 This implements a basic search of memory, reading target memory and
2846 performing the search here (as opposed to performing the search in on the
2847 target side with, for example, gdbserver). */
2848
2849int
2850simple_search_memory (struct target_ops *ops,
2851 CORE_ADDR start_addr, ULONGEST search_space_len,
2852 const gdb_byte *pattern, ULONGEST pattern_len,
2853 CORE_ADDR *found_addrp)
2854{
2855 /* NOTE: also defined in find.c testcase. */
2856#define SEARCH_CHUNK_SIZE 16000
2857 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2858 /* Buffer to hold memory contents for searching. */
2859 gdb_byte *search_buf;
2860 unsigned search_buf_size;
2861 struct cleanup *old_cleanups;
2862
2863 search_buf_size = chunk_size + pattern_len - 1;
2864
2865 /* No point in trying to allocate a buffer larger than the search space. */
2866 if (search_space_len < search_buf_size)
2867 search_buf_size = search_space_len;
2868
2869 search_buf = malloc (search_buf_size);
2870 if (search_buf == NULL)
5e1471f5 2871 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2872 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2873
2874 /* Prime the search buffer. */
2875
2876 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2877 search_buf, start_addr, search_buf_size) != search_buf_size)
2878 {
b3dc46ff
AB
2879 warning (_("Unable to access %s bytes of target "
2880 "memory at %s, halting search."),
2881 pulongest (search_buf_size), hex_string (start_addr));
08388c79
DE
2882 do_cleanups (old_cleanups);
2883 return -1;
2884 }
2885
2886 /* Perform the search.
2887
2888 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2889 When we've scanned N bytes we copy the trailing bytes to the start and
2890 read in another N bytes. */
2891
2892 while (search_space_len >= pattern_len)
2893 {
2894 gdb_byte *found_ptr;
2895 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2896
2897 found_ptr = memmem (search_buf, nr_search_bytes,
2898 pattern, pattern_len);
2899
2900 if (found_ptr != NULL)
2901 {
2902 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
5d502164 2903
08388c79
DE
2904 *found_addrp = found_addr;
2905 do_cleanups (old_cleanups);
2906 return 1;
2907 }
2908
2909 /* Not found in this chunk, skip to next chunk. */
2910
2911 /* Don't let search_space_len wrap here, it's unsigned. */
2912 if (search_space_len >= chunk_size)
2913 search_space_len -= chunk_size;
2914 else
2915 search_space_len = 0;
2916
2917 if (search_space_len >= pattern_len)
2918 {
2919 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2920 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2921 int nr_to_read;
2922
2923 /* Copy the trailing part of the previous iteration to the front
2924 of the buffer for the next iteration. */
2925 gdb_assert (keep_len == pattern_len - 1);
2926 memcpy (search_buf, search_buf + chunk_size, keep_len);
2927
2928 nr_to_read = min (search_space_len - keep_len, chunk_size);
2929
2930 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2931 search_buf + keep_len, read_addr,
2932 nr_to_read) != nr_to_read)
2933 {
b3dc46ff 2934 warning (_("Unable to access %s bytes of target "
9b20d036 2935 "memory at %s, halting search."),
b3dc46ff 2936 plongest (nr_to_read),
08388c79
DE
2937 hex_string (read_addr));
2938 do_cleanups (old_cleanups);
2939 return -1;
2940 }
2941
2942 start_addr += chunk_size;
2943 }
2944 }
2945
2946 /* Not found. */
2947
2948 do_cleanups (old_cleanups);
2949 return 0;
2950}
2951
2952/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2953 sequence of bytes in PATTERN with length PATTERN_LEN.
2954
2955 The result is 1 if found, 0 if not found, and -1 if there was an error
2956 requiring halting of the search (e.g. memory read error).
2957 If the pattern is found the address is recorded in FOUND_ADDRP. */
2958
2959int
2960target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2961 const gdb_byte *pattern, ULONGEST pattern_len,
2962 CORE_ADDR *found_addrp)
2963{
2964 struct target_ops *t;
2965 int found;
2966
2967 /* We don't use INHERIT to set current_target.to_search_memory,
2968 so we have to scan the target stack and handle targetdebug
2969 ourselves. */
2970
2971 if (targetdebug)
2972 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2973 hex_string (start_addr));
2974
2975 for (t = current_target.beneath; t != NULL; t = t->beneath)
2976 if (t->to_search_memory != NULL)
2977 break;
2978
2979 if (t != NULL)
2980 {
2981 found = t->to_search_memory (t, start_addr, search_space_len,
2982 pattern, pattern_len, found_addrp);
2983 }
2984 else
2985 {
2986 /* If a special version of to_search_memory isn't available, use the
2987 simple version. */
c35b1492 2988 found = simple_search_memory (current_target.beneath,
08388c79
DE
2989 start_addr, search_space_len,
2990 pattern, pattern_len, found_addrp);
2991 }
2992
2993 if (targetdebug)
2994 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2995
2996 return found;
2997}
2998
8edfe269
DJ
2999/* Look through the currently pushed targets. If none of them will
3000 be able to restart the currently running process, issue an error
3001 message. */
3002
3003void
3004target_require_runnable (void)
3005{
3006 struct target_ops *t;
3007
3008 for (t = target_stack; t != NULL; t = t->beneath)
3009 {
3010 /* If this target knows how to create a new program, then
3011 assume we will still be able to after killing the current
3012 one. Either killing and mourning will not pop T, or else
3013 find_default_run_target will find it again. */
3014 if (t->to_create_inferior != NULL)
3015 return;
3016
3017 /* Do not worry about thread_stratum targets that can not
3018 create inferiors. Assume they will be pushed again if
3019 necessary, and continue to the process_stratum. */
85e747d2
UW
3020 if (t->to_stratum == thread_stratum
3021 || t->to_stratum == arch_stratum)
8edfe269
DJ
3022 continue;
3023
3e43a32a
MS
3024 error (_("The \"%s\" target does not support \"run\". "
3025 "Try \"help target\" or \"continue\"."),
8edfe269
DJ
3026 t->to_shortname);
3027 }
3028
3029 /* This function is only called if the target is running. In that
3030 case there should have been a process_stratum target and it
c378eb4e 3031 should either know how to create inferiors, or not... */
9b20d036 3032 internal_error (__FILE__, __LINE__, _("No targets found"));
8edfe269
DJ
3033}
3034
c906108c
SS
3035/* Look through the list of possible targets for a target that can
3036 execute a run or attach command without any other data. This is
3037 used to locate the default process stratum.
3038
5f667f2d
PA
3039 If DO_MESG is not NULL, the result is always valid (error() is
3040 called for errors); else, return NULL on error. */
c906108c
SS
3041
3042static struct target_ops *
fba45db2 3043find_default_run_target (char *do_mesg)
c906108c
SS
3044{
3045 struct target_ops **t;
3046 struct target_ops *runable = NULL;
3047 int count;
3048
3049 count = 0;
3050
3051 for (t = target_structs; t < target_structs + target_struct_size;
3052 ++t)
3053 {
c5aa993b 3054 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
3055 {
3056 runable = *t;
3057 ++count;
3058 }
3059 }
3060
3061 if (count != 1)
5f667f2d
PA
3062 {
3063 if (do_mesg)
3064 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
3065 else
3066 return NULL;
3067 }
c906108c
SS
3068
3069 return runable;
3070}
3071
3072void
136d6dae 3073find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
3074{
3075 struct target_ops *t;
3076
c5aa993b 3077 t = find_default_run_target ("attach");
136d6dae 3078 (t->to_attach) (t, args, from_tty);
c906108c
SS
3079 return;
3080}
3081
c906108c 3082void
136d6dae
VP
3083find_default_create_inferior (struct target_ops *ops,
3084 char *exec_file, char *allargs, char **env,
c27cda74 3085 int from_tty)
c906108c
SS
3086{
3087 struct target_ops *t;
3088
c5aa993b 3089 t = find_default_run_target ("run");
136d6dae 3090 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
3091 return;
3092}
3093
2c0b251b 3094static int
6a109b6b 3095find_default_can_async_p (struct target_ops *ignore)
b84876c2
PA
3096{
3097 struct target_ops *t;
3098
5f667f2d
PA
3099 /* This may be called before the target is pushed on the stack;
3100 look for the default process stratum. If there's none, gdb isn't
3101 configured with a native debugger, and target remote isn't
3102 connected yet. */
3103 t = find_default_run_target (NULL);
6b84065d 3104 if (t && t->to_can_async_p != delegate_can_async_p)
6a109b6b 3105 return (t->to_can_async_p) (t);
b84876c2
PA
3106 return 0;
3107}
3108
2c0b251b 3109static int
6a109b6b 3110find_default_is_async_p (struct target_ops *ignore)
b84876c2
PA
3111{
3112 struct target_ops *t;
3113
5f667f2d
PA
3114 /* This may be called before the target is pushed on the stack;
3115 look for the default process stratum. If there's none, gdb isn't
3116 configured with a native debugger, and target remote isn't
3117 connected yet. */
3118 t = find_default_run_target (NULL);
6b84065d 3119 if (t && t->to_is_async_p != delegate_is_async_p)
6a109b6b 3120 return (t->to_is_async_p) (t);
b84876c2
PA
3121 return 0;
3122}
3123
2c0b251b 3124static int
2a9a2795 3125find_default_supports_non_stop (struct target_ops *self)
9908b566
VP
3126{
3127 struct target_ops *t;
3128
3129 t = find_default_run_target (NULL);
3130 if (t && t->to_supports_non_stop)
2a9a2795 3131 return (t->to_supports_non_stop) (t);
9908b566
VP
3132 return 0;
3133}
3134
3135int
2c0b251b 3136target_supports_non_stop (void)
9908b566
VP
3137{
3138 struct target_ops *t;
5d502164 3139
9908b566
VP
3140 for (t = &current_target; t != NULL; t = t->beneath)
3141 if (t->to_supports_non_stop)
2a9a2795 3142 return t->to_supports_non_stop (t);
9908b566
VP
3143
3144 return 0;
3145}
3146
145b16a9
UW
3147/* Implement the "info proc" command. */
3148
451b7c33 3149int
145b16a9
UW
3150target_info_proc (char *args, enum info_proc_what what)
3151{
3152 struct target_ops *t;
3153
3154 /* If we're already connected to something that can get us OS
3155 related data, use it. Otherwise, try using the native
3156 target. */
3157 if (current_target.to_stratum >= process_stratum)
3158 t = current_target.beneath;
3159 else
3160 t = find_default_run_target (NULL);
3161
3162 for (; t != NULL; t = t->beneath)
3163 {
3164 if (t->to_info_proc != NULL)
3165 {
3166 t->to_info_proc (t, args, what);
3167
3168 if (targetdebug)
3169 fprintf_unfiltered (gdb_stdlog,
3170 "target_info_proc (\"%s\", %d)\n", args, what);
3171
451b7c33 3172 return 1;
145b16a9
UW
3173 }
3174 }
3175
451b7c33 3176 return 0;
145b16a9
UW
3177}
3178
03583c20 3179static int
2bfc0540 3180find_default_supports_disable_randomization (struct target_ops *self)
03583c20
UW
3181{
3182 struct target_ops *t;
3183
3184 t = find_default_run_target (NULL);
3185 if (t && t->to_supports_disable_randomization)
2bfc0540 3186 return (t->to_supports_disable_randomization) (t);
03583c20
UW
3187 return 0;
3188}
3189
3190int
3191target_supports_disable_randomization (void)
3192{
3193 struct target_ops *t;
3194
3195 for (t = &current_target; t != NULL; t = t->beneath)
3196 if (t->to_supports_disable_randomization)
2bfc0540 3197 return t->to_supports_disable_randomization (t);
03583c20
UW
3198
3199 return 0;
3200}
9908b566 3201
07e059b5
VP
3202char *
3203target_get_osdata (const char *type)
3204{
07e059b5
VP
3205 struct target_ops *t;
3206
739ef7fb
PA
3207 /* If we're already connected to something that can get us OS
3208 related data, use it. Otherwise, try using the native
3209 target. */
3210 if (current_target.to_stratum >= process_stratum)
6d097e65 3211 t = current_target.beneath;
739ef7fb
PA
3212 else
3213 t = find_default_run_target ("get OS data");
07e059b5
VP
3214
3215 if (!t)
3216 return NULL;
3217
6d097e65 3218 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
3219}
3220
6c95b8df
PA
3221/* Determine the current address space of thread PTID. */
3222
3223struct address_space *
3224target_thread_address_space (ptid_t ptid)
3225{
c0694254 3226 struct address_space *aspace;
6c95b8df 3227 struct inferior *inf;
c0694254
PA
3228 struct target_ops *t;
3229
3230 for (t = current_target.beneath; t != NULL; t = t->beneath)
3231 {
3232 if (t->to_thread_address_space != NULL)
3233 {
3234 aspace = t->to_thread_address_space (t, ptid);
3235 gdb_assert (aspace);
6c95b8df 3236
c0694254
PA
3237 if (targetdebug)
3238 fprintf_unfiltered (gdb_stdlog,
3239 "target_thread_address_space (%s) = %d\n",
3240 target_pid_to_str (ptid),
3241 address_space_num (aspace));
3242 return aspace;
3243 }
3244 }
6c95b8df
PA
3245
3246 /* Fall-back to the "main" address space of the inferior. */
3247 inf = find_inferior_pid (ptid_get_pid (ptid));
3248
3249 if (inf == NULL || inf->aspace == NULL)
3e43a32a 3250 internal_error (__FILE__, __LINE__,
9b20d036
MS
3251 _("Can't determine the current "
3252 "address space of thread %s\n"),
6c95b8df
PA
3253 target_pid_to_str (ptid));
3254
3255 return inf->aspace;
3256}
3257
7313baad
UW
3258
3259/* Target file operations. */
3260
3261static struct target_ops *
3262default_fileio_target (void)
3263{
3264 /* If we're already connected to something that can perform
3265 file I/O, use it. Otherwise, try using the native target. */
3266 if (current_target.to_stratum >= process_stratum)
3267 return current_target.beneath;
3268 else
3269 return find_default_run_target ("file I/O");
3270}
3271
3272/* Open FILENAME on the target, using FLAGS and MODE. Return a
3273 target file descriptor, or -1 if an error occurs (and set
3274 *TARGET_ERRNO). */
3275int
3276target_fileio_open (const char *filename, int flags, int mode,
3277 int *target_errno)
3278{
3279 struct target_ops *t;
3280
3281 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3282 {
3283 if (t->to_fileio_open != NULL)
3284 {
cd897586 3285 int fd = t->to_fileio_open (t, filename, flags, mode, target_errno);
7313baad
UW
3286
3287 if (targetdebug)
3288 fprintf_unfiltered (gdb_stdlog,
3289 "target_fileio_open (%s,0x%x,0%o) = %d (%d)\n",
3290 filename, flags, mode,
3291 fd, fd != -1 ? 0 : *target_errno);
3292 return fd;
3293 }
3294 }
3295
3296 *target_errno = FILEIO_ENOSYS;
3297 return -1;
3298}
3299
3300/* Write up to LEN bytes from WRITE_BUF to FD on the target.
3301 Return the number of bytes written, or -1 if an error occurs
3302 (and set *TARGET_ERRNO). */
3303int
3304target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
3305 ULONGEST offset, int *target_errno)
3306{
3307 struct target_ops *t;
3308
3309 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3310 {
3311 if (t->to_fileio_pwrite != NULL)
3312 {
0d866f62 3313 int ret = t->to_fileio_pwrite (t, fd, write_buf, len, offset,
7313baad
UW
3314 target_errno);
3315
3316 if (targetdebug)
3317 fprintf_unfiltered (gdb_stdlog,
a71b5a38 3318 "target_fileio_pwrite (%d,...,%d,%s) "
7313baad 3319 "= %d (%d)\n",
a71b5a38 3320 fd, len, pulongest (offset),
7313baad
UW
3321 ret, ret != -1 ? 0 : *target_errno);
3322 return ret;
3323 }
3324 }
3325
3326 *target_errno = FILEIO_ENOSYS;
3327 return -1;
3328}
3329
3330/* Read up to LEN bytes FD on the target into READ_BUF.
3331 Return the number of bytes read, or -1 if an error occurs
3332 (and set *TARGET_ERRNO). */
3333int
3334target_fileio_pread (int fd, gdb_byte *read_buf, int len,
3335 ULONGEST offset, int *target_errno)
3336{
3337 struct target_ops *t;
3338
3339 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3340 {
3341 if (t->to_fileio_pread != NULL)
3342 {
a3be983c 3343 int ret = t->to_fileio_pread (t, fd, read_buf, len, offset,
7313baad
UW
3344 target_errno);
3345
3346 if (targetdebug)
3347 fprintf_unfiltered (gdb_stdlog,
a71b5a38 3348 "target_fileio_pread (%d,...,%d,%s) "
7313baad 3349 "= %d (%d)\n",
a71b5a38 3350 fd, len, pulongest (offset),
7313baad
UW
3351 ret, ret != -1 ? 0 : *target_errno);
3352 return ret;
3353 }
3354 }
3355
3356 *target_errno = FILEIO_ENOSYS;
3357 return -1;
3358}
3359
3360/* Close FD on the target. Return 0, or -1 if an error occurs
3361 (and set *TARGET_ERRNO). */
3362int
3363target_fileio_close (int fd, int *target_errno)
3364{
3365 struct target_ops *t;
3366
3367 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3368 {
3369 if (t->to_fileio_close != NULL)
3370 {
df39ea25 3371 int ret = t->to_fileio_close (t, fd, target_errno);
7313baad
UW
3372
3373 if (targetdebug)
3374 fprintf_unfiltered (gdb_stdlog,
3375 "target_fileio_close (%d) = %d (%d)\n",
3376 fd, ret, ret != -1 ? 0 : *target_errno);
3377 return ret;
3378 }
3379 }
3380
3381 *target_errno = FILEIO_ENOSYS;
3382 return -1;
3383}
3384
3385/* Unlink FILENAME on the target. Return 0, or -1 if an error
3386 occurs (and set *TARGET_ERRNO). */
3387int
3388target_fileio_unlink (const char *filename, int *target_errno)
3389{
3390 struct target_ops *t;
3391
3392 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3393 {
3394 if (t->to_fileio_unlink != NULL)
3395 {
dbbca37d 3396 int ret = t->to_fileio_unlink (t, filename, target_errno);
7313baad
UW
3397
3398 if (targetdebug)
3399 fprintf_unfiltered (gdb_stdlog,
3400 "target_fileio_unlink (%s) = %d (%d)\n",
3401 filename, ret, ret != -1 ? 0 : *target_errno);
3402 return ret;
3403 }
3404 }
3405
3406 *target_errno = FILEIO_ENOSYS;
3407 return -1;
3408}
3409
b9e7b9c3
UW
3410/* Read value of symbolic link FILENAME on the target. Return a
3411 null-terminated string allocated via xmalloc, or NULL if an error
3412 occurs (and set *TARGET_ERRNO). */
3413char *
3414target_fileio_readlink (const char *filename, int *target_errno)
3415{
3416 struct target_ops *t;
3417
3418 for (t = default_fileio_target (); t != NULL; t = t->beneath)
3419 {
3420 if (t->to_fileio_readlink != NULL)
3421 {
fab5aa7c 3422 char *ret = t->to_fileio_readlink (t, filename, target_errno);
b9e7b9c3
UW
3423
3424 if (targetdebug)
3425 fprintf_unfiltered (gdb_stdlog,
3426 "target_fileio_readlink (%s) = %s (%d)\n",
3427 filename, ret? ret : "(nil)",
3428 ret? 0 : *target_errno);
3429 return ret;
3430 }
3431 }
3432
3433 *target_errno = FILEIO_ENOSYS;
3434 return NULL;
3435}
3436
7313baad
UW
3437static void
3438target_fileio_close_cleanup (void *opaque)
3439{
3440 int fd = *(int *) opaque;
3441 int target_errno;
3442
3443 target_fileio_close (fd, &target_errno);
3444}
3445
3446/* Read target file FILENAME. Store the result in *BUF_P and
3447 return the size of the transferred data. PADDING additional bytes are
3448 available in *BUF_P. This is a helper function for
3449 target_fileio_read_alloc; see the declaration of that function for more
3450 information. */
3451
3452static LONGEST
3453target_fileio_read_alloc_1 (const char *filename,
3454 gdb_byte **buf_p, int padding)
3455{
3456 struct cleanup *close_cleanup;
3457 size_t buf_alloc, buf_pos;
3458 gdb_byte *buf;
3459 LONGEST n;
3460 int fd;
3461 int target_errno;
3462
3463 fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
3464 if (fd == -1)
3465 return -1;
3466
3467 close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
3468
3469 /* Start by reading up to 4K at a time. The target will throttle
3470 this number down if necessary. */
3471 buf_alloc = 4096;
3472 buf = xmalloc (buf_alloc);
3473 buf_pos = 0;
3474 while (1)
3475 {
3476 n = target_fileio_pread (fd, &buf[buf_pos],
3477 buf_alloc - buf_pos - padding, buf_pos,
3478 &target_errno);
3479 if (n < 0)
3480 {
3481 /* An error occurred. */
3482 do_cleanups (close_cleanup);
3483 xfree (buf);
3484 return -1;
3485 }
3486 else if (n == 0)
3487 {
3488 /* Read all there was. */
3489 do_cleanups (close_cleanup);
3490 if (buf_pos == 0)
3491 xfree (buf);
3492 else
3493 *buf_p = buf;
3494 return buf_pos;
3495 }
3496
3497 buf_pos += n;
3498
3499 /* If the buffer is filling up, expand it. */
3500 if (buf_alloc < buf_pos * 2)
3501 {
3502 buf_alloc *= 2;
3503 buf = xrealloc (buf, buf_alloc);
3504 }
3505
3506 QUIT;
3507 }
3508}
3509
3510/* Read target file FILENAME. Store the result in *BUF_P and return
3511 the size of the transferred data. See the declaration in "target.h"
3512 function for more information about the return value. */
3513
3514LONGEST
3515target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
3516{
3517 return target_fileio_read_alloc_1 (filename, buf_p, 0);
3518}
3519
3520/* Read target file FILENAME. The result is NUL-terminated and
3521 returned as a string, allocated using xmalloc. If an error occurs
3522 or the transfer is unsupported, NULL is returned. Empty objects
3523 are returned as allocated but empty strings. A warning is issued
3524 if the result contains any embedded NUL bytes. */
3525
3526char *
3527target_fileio_read_stralloc (const char *filename)
3528{
39086a0e
PA
3529 gdb_byte *buffer;
3530 char *bufstr;
7313baad
UW
3531 LONGEST i, transferred;
3532
39086a0e
PA
3533 transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
3534 bufstr = (char *) buffer;
7313baad
UW
3535
3536 if (transferred < 0)
3537 return NULL;
3538
3539 if (transferred == 0)
3540 return xstrdup ("");
3541
39086a0e 3542 bufstr[transferred] = 0;
7313baad
UW
3543
3544 /* Check for embedded NUL bytes; but allow trailing NULs. */
39086a0e
PA
3545 for (i = strlen (bufstr); i < transferred; i++)
3546 if (bufstr[i] != 0)
7313baad
UW
3547 {
3548 warning (_("target file %s "
3549 "contained unexpected null characters"),
3550 filename);
3551 break;
3552 }
3553
39086a0e 3554 return bufstr;
7313baad
UW
3555}
3556
3557
e0d24f8d 3558static int
31568a15
TT
3559default_region_ok_for_hw_watchpoint (struct target_ops *self,
3560 CORE_ADDR addr, int len)
e0d24f8d 3561{
f5656ead 3562 return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT);
ccaa32c7
GS
3563}
3564
5009afc5
AS
3565static int
3566default_watchpoint_addr_within_range (struct target_ops *target,
3567 CORE_ADDR addr,
3568 CORE_ADDR start, int length)
3569{
3570 return addr >= start && addr < start + length;
3571}
3572
c2250ad1
UW
3573static struct gdbarch *
3574default_thread_architecture (struct target_ops *ops, ptid_t ptid)
3575{
f5656ead 3576 return target_gdbarch ();
c2250ad1
UW
3577}
3578
c906108c 3579static int
fba45db2 3580return_zero (void)
c906108c
SS
3581{
3582 return 0;
3583}
3584
ccaa32c7
GS
3585static int
3586return_minus_one (void)
3587{
3588 return -1;
3589}
3590
1b67eb02
AS
3591static void *
3592return_null (void)
3593{
3594 return 0;
3595}
3596
ed9a39eb
JM
3597/*
3598 * Find the next target down the stack from the specified target.
3599 */
3600
3601struct target_ops *
fba45db2 3602find_target_beneath (struct target_ops *t)
ed9a39eb 3603{
258b763a 3604 return t->beneath;
ed9a39eb
JM
3605}
3606
8b06beed
TT
3607/* See target.h. */
3608
3609struct target_ops *
3610find_target_at (enum strata stratum)
3611{
3612 struct target_ops *t;
3613
3614 for (t = current_target.beneath; t != NULL; t = t->beneath)
3615 if (t->to_stratum == stratum)
3616 return t;
3617
3618 return NULL;
3619}
3620
c906108c
SS
3621\f
3622/* The inferior process has died. Long live the inferior! */
3623
3624void
fba45db2 3625generic_mourn_inferior (void)
c906108c 3626{
7f9f62ba 3627 ptid_t ptid;
c906108c 3628
7f9f62ba 3629 ptid = inferior_ptid;
39f77062 3630 inferior_ptid = null_ptid;
7f9f62ba 3631
f59f708a
PA
3632 /* Mark breakpoints uninserted in case something tries to delete a
3633 breakpoint while we delete the inferior's threads (which would
3634 fail, since the inferior is long gone). */
3635 mark_breakpoints_out ();
3636
7f9f62ba
PA
3637 if (!ptid_equal (ptid, null_ptid))
3638 {
3639 int pid = ptid_get_pid (ptid);
6c95b8df 3640 exit_inferior (pid);
7f9f62ba
PA
3641 }
3642
f59f708a
PA
3643 /* Note this wipes step-resume breakpoints, so needs to be done
3644 after exit_inferior, which ends up referencing the step-resume
3645 breakpoints through clear_thread_inferior_resources. */
c906108c 3646 breakpoint_init_inferior (inf_exited);
f59f708a 3647
c906108c
SS
3648 registers_changed ();
3649
c906108c
SS
3650 reopen_exec_file ();
3651 reinit_frame_cache ();
3652
9a4105ab
AC
3653 if (deprecated_detach_hook)
3654 deprecated_detach_hook ();
c906108c
SS
3655}
3656\f
fd0a2a6f
MK
3657/* Convert a normal process ID to a string. Returns the string in a
3658 static buffer. */
c906108c
SS
3659
3660char *
39f77062 3661normal_pid_to_str (ptid_t ptid)
c906108c 3662{
fd0a2a6f 3663 static char buf[32];
c906108c 3664
5fff8fc0 3665 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
3666 return buf;
3667}
3668
2c0b251b 3669static char *
117de6a9
PA
3670dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
3671{
3672 return normal_pid_to_str (ptid);
3673}
3674
9b4eba8e
HZ
3675/* Error-catcher for target_find_memory_regions. */
3676static int
2e73927c
TT
3677dummy_find_memory_regions (struct target_ops *self,
3678 find_memory_region_ftype ignore1, void *ignore2)
be4d1333 3679{
9b4eba8e 3680 error (_("Command not implemented for this target."));
be4d1333
MS
3681 return 0;
3682}
3683
9b4eba8e
HZ
3684/* Error-catcher for target_make_corefile_notes. */
3685static char *
fc6691b2
TT
3686dummy_make_corefile_notes (struct target_ops *self,
3687 bfd *ignore1, int *ignore2)
be4d1333 3688{
9b4eba8e 3689 error (_("Command not implemented for this target."));
be4d1333
MS
3690 return NULL;
3691}
3692
6b04bdb7
MS
3693/* Error-catcher for target_get_bookmark. */
3694static gdb_byte *
dd0e2830 3695dummy_get_bookmark (struct target_ops *self, char *ignore1, int ignore2)
6b04bdb7
MS
3696{
3697 tcomplain ();
3698 return NULL;
3699}
3700
3701/* Error-catcher for target_goto_bookmark. */
3702static void
3c80fb48 3703dummy_goto_bookmark (struct target_ops *self, gdb_byte *ignore, int from_tty)
6b04bdb7
MS
3704{
3705 tcomplain ();
3706}
3707
c906108c
SS
3708/* Set up the handful of non-empty slots needed by the dummy target
3709 vector. */
3710
3711static void
fba45db2 3712init_dummy_target (void)
c906108c
SS
3713{
3714 dummy_target.to_shortname = "None";
3715 dummy_target.to_longname = "None";
3716 dummy_target.to_doc = "";
c906108c 3717 dummy_target.to_create_inferior = find_default_create_inferior;
9908b566 3718 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
03583c20
UW
3719 dummy_target.to_supports_disable_randomization
3720 = find_default_supports_disable_randomization;
117de6a9 3721 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 3722 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
3723 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
3724 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
6b04bdb7
MS
3725 dummy_target.to_get_bookmark = dummy_get_bookmark;
3726 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
c35b1492
PA
3727 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3728 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
3729 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
3730 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
aeaec162
TT
3731 dummy_target.to_has_execution
3732 = (int (*) (struct target_ops *, ptid_t)) return_zero;
c906108c 3733 dummy_target.to_magic = OPS_MAGIC;
1101cb7b
TT
3734
3735 install_dummy_methods (&dummy_target);
c906108c 3736}
c906108c 3737\f
c906108c 3738static void
fba45db2 3739debug_to_open (char *args, int from_tty)
c906108c
SS
3740{
3741 debug_target.to_open (args, from_tty);
3742
96baa820 3743 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
3744}
3745
f1c07ab0 3746void
460014f5 3747target_close (struct target_ops *targ)
f1c07ab0 3748{
7fdc1521
TT
3749 gdb_assert (!target_is_pushed (targ));
3750
f1c07ab0 3751 if (targ->to_xclose != NULL)
460014f5 3752 targ->to_xclose (targ);
f1c07ab0 3753 else if (targ->to_close != NULL)
de90e03d 3754 targ->to_close (targ);
947b8855
PA
3755
3756 if (targetdebug)
460014f5 3757 fprintf_unfiltered (gdb_stdlog, "target_close ()\n");
f1c07ab0
AC
3758}
3759
136d6dae
VP
3760void
3761target_attach (char *args, int from_tty)
3762{
e9a29200
TT
3763 current_target.to_attach (&current_target, args, from_tty);
3764 if (targetdebug)
3765 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
3766 args, from_tty);
136d6dae
VP
3767}
3768
28439f5e
PA
3769int
3770target_thread_alive (ptid_t ptid)
c906108c 3771{
28439f5e 3772 struct target_ops *t;
5d502164 3773
28439f5e
PA
3774 for (t = current_target.beneath; t != NULL; t = t->beneath)
3775 {
3776 if (t->to_thread_alive != NULL)
3777 {
3778 int retval;
c906108c 3779
28439f5e
PA
3780 retval = t->to_thread_alive (t, ptid);
3781 if (targetdebug)
3782 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
dfd4cc63 3783 ptid_get_pid (ptid), retval);
28439f5e
PA
3784
3785 return retval;
3786 }
3787 }
3788
3789 return 0;
3790}
3791
3792void
3793target_find_new_threads (void)
3794{
3795 struct target_ops *t;
5d502164 3796
28439f5e
PA
3797 for (t = current_target.beneath; t != NULL; t = t->beneath)
3798 {
3799 if (t->to_find_new_threads != NULL)
3800 {
3801 t->to_find_new_threads (t);
3802 if (targetdebug)
3803 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
3804
3805 return;
3806 }
3807 }
c906108c
SS
3808}
3809
d914c394
SS
3810void
3811target_stop (ptid_t ptid)
3812{
3813 if (!may_stop)
3814 {
3815 warning (_("May not interrupt or stop the target, ignoring attempt"));
3816 return;
3817 }
3818
1eab8a48 3819 (*current_target.to_stop) (&current_target, ptid);
d914c394
SS
3820}
3821
c906108c 3822static void
f045800c 3823debug_to_post_attach (struct target_ops *self, int pid)
c906108c 3824{
f045800c 3825 debug_target.to_post_attach (&debug_target, pid);
c906108c 3826
28439f5e 3827 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
3828}
3829
09826ec5
PA
3830/* Concatenate ELEM to LIST, a comma separate list, and return the
3831 result. The LIST incoming argument is released. */
3832
3833static char *
3834str_comma_list_concat_elem (char *list, const char *elem)
3835{
3836 if (list == NULL)
3837 return xstrdup (elem);
3838 else
3839 return reconcat (list, list, ", ", elem, (char *) NULL);
3840}
3841
3842/* Helper for target_options_to_string. If OPT is present in
3843 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3844 Returns the new resulting string. OPT is removed from
3845 TARGET_OPTIONS. */
3846
3847static char *
3848do_option (int *target_options, char *ret,
3849 int opt, char *opt_str)
3850{
3851 if ((*target_options & opt) != 0)
3852 {
3853 ret = str_comma_list_concat_elem (ret, opt_str);
3854 *target_options &= ~opt;
3855 }
3856
3857 return ret;
3858}
3859
3860char *
3861target_options_to_string (int target_options)
3862{
3863 char *ret = NULL;
3864
3865#define DO_TARG_OPTION(OPT) \
3866 ret = do_option (&target_options, ret, OPT, #OPT)
3867
3868 DO_TARG_OPTION (TARGET_WNOHANG);
3869
3870 if (target_options != 0)
3871 ret = str_comma_list_concat_elem (ret, "unknown???");
3872
3873 if (ret == NULL)
3874 ret = xstrdup ("");
3875 return ret;
3876}
3877
bf0c5130 3878static void
56be3814
UW
3879debug_print_register (const char * func,
3880 struct regcache *regcache, int regno)
bf0c5130 3881{
f8d29908 3882 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5d502164 3883
bf0c5130 3884 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 3885 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
3886 && gdbarch_register_name (gdbarch, regno) != NULL
3887 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3888 fprintf_unfiltered (gdb_stdlog, "(%s)",
3889 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
3890 else
3891 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 3892 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 3893 {
e17a4113 3894 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 3895 int i, size = register_size (gdbarch, regno);
e362b510 3896 gdb_byte buf[MAX_REGISTER_SIZE];
5d502164 3897
0ff58721 3898 regcache_raw_collect (regcache, regno, buf);
bf0c5130 3899 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 3900 for (i = 0; i < size; i++)
bf0c5130
AC
3901 {
3902 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3903 }
81c4a259 3904 if (size <= sizeof (LONGEST))
bf0c5130 3905 {
e17a4113 3906 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
5d502164 3907
0b1553bc
UW
3908 fprintf_unfiltered (gdb_stdlog, " %s %s",
3909 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
3910 }
3911 }
3912 fprintf_unfiltered (gdb_stdlog, "\n");
3913}
3914
28439f5e
PA
3915void
3916target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3917{
28439f5e 3918 struct target_ops *t;
5d502164 3919
28439f5e
PA
3920 for (t = current_target.beneath; t != NULL; t = t->beneath)
3921 {
3922 if (t->to_fetch_registers != NULL)
3923 {
3924 t->to_fetch_registers (t, regcache, regno);
3925 if (targetdebug)
3926 debug_print_register ("target_fetch_registers", regcache, regno);
3927 return;
3928 }
3929 }
c906108c
SS
3930}
3931
28439f5e
PA
3932void
3933target_store_registers (struct regcache *regcache, int regno)
c906108c 3934{
28439f5e 3935 struct target_ops *t;
5d502164 3936
d914c394
SS
3937 if (!may_write_registers)
3938 error (_("Writing to registers is not allowed (regno %d)"), regno);
3939
6b84065d
TT
3940 current_target.to_store_registers (&current_target, regcache, regno);
3941 if (targetdebug)
28439f5e 3942 {
6b84065d 3943 debug_print_register ("target_store_registers", regcache, regno);
28439f5e 3944 }
c906108c
SS
3945}
3946
dc146f7c
VP
3947int
3948target_core_of_thread (ptid_t ptid)
3949{
3950 struct target_ops *t;
3951
3952 for (t = current_target.beneath; t != NULL; t = t->beneath)
3953 {
3954 if (t->to_core_of_thread != NULL)
3955 {
3956 int retval = t->to_core_of_thread (t, ptid);
5d502164 3957
dc146f7c 3958 if (targetdebug)
3e43a32a
MS
3959 fprintf_unfiltered (gdb_stdlog,
3960 "target_core_of_thread (%d) = %d\n",
dfd4cc63 3961 ptid_get_pid (ptid), retval);
dc146f7c
VP
3962 return retval;
3963 }
3964 }
3965
3966 return -1;
3967}
3968
4a5e7a5b
PA
3969int
3970target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3971{
3972 struct target_ops *t;
3973
3974 for (t = current_target.beneath; t != NULL; t = t->beneath)
3975 {
3976 if (t->to_verify_memory != NULL)
3977 {
3978 int retval = t->to_verify_memory (t, data, memaddr, size);
5d502164 3979
4a5e7a5b 3980 if (targetdebug)
3e43a32a
MS
3981 fprintf_unfiltered (gdb_stdlog,
3982 "target_verify_memory (%s, %s) = %d\n",
f5656ead 3983 paddress (target_gdbarch (), memaddr),
4a5e7a5b
PA
3984 pulongest (size),
3985 retval);
3986 return retval;
3987 }
3988 }
3989
3990 tcomplain ();
3991}
3992
9c06b0b4
TJB
3993/* The documentation for this function is in its prototype declaration in
3994 target.h. */
3995
3996int
3997target_insert_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_insert_mask_watchpoint != NULL)
4003 {
4004 int ret;
4005
4006 ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
4007
4008 if (targetdebug)
4009 fprintf_unfiltered (gdb_stdlog, "\
4010target_insert_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 in
4021 target.h. */
4022
4023int
4024target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
4025{
4026 struct target_ops *t;
4027
4028 for (t = current_target.beneath; t != NULL; t = t->beneath)
4029 if (t->to_remove_mask_watchpoint != NULL)
4030 {
4031 int ret;
4032
4033 ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
4034
4035 if (targetdebug)
4036 fprintf_unfiltered (gdb_stdlog, "\
4037target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
4038 core_addr_to_string (addr),
4039 core_addr_to_string (mask), rw, ret);
4040
4041 return ret;
4042 }
4043
4044 return 1;
4045}
4046
4047/* The documentation for this function is in its prototype declaration
4048 in target.h. */
4049
4050int
4051target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
4052{
4053 struct target_ops *t;
4054
4055 for (t = current_target.beneath; t != NULL; t = t->beneath)
4056 if (t->to_masked_watch_num_registers != NULL)
4057 return t->to_masked_watch_num_registers (t, addr, mask);
4058
4059 return -1;
4060}
4061
f1310107
TJB
4062/* The documentation for this function is in its prototype declaration
4063 in target.h. */
4064
4065int
4066target_ranged_break_num_registers (void)
4067{
4068 struct target_ops *t;
4069
4070 for (t = current_target.beneath; t != NULL; t = t->beneath)
4071 if (t->to_ranged_break_num_registers != NULL)
4072 return t->to_ranged_break_num_registers (t);
4073
4074 return -1;
4075}
4076
02d27625
MM
4077/* See target.h. */
4078
02d27625
MM
4079struct btrace_target_info *
4080target_enable_btrace (ptid_t ptid)
4081{
4082 struct target_ops *t;
4083
4084 for (t = current_target.beneath; t != NULL; t = t->beneath)
4085 if (t->to_enable_btrace != NULL)
e3c49f88 4086 return t->to_enable_btrace (t, ptid);
02d27625
MM
4087
4088 tcomplain ();
4089 return NULL;
4090}
4091
4092/* See target.h. */
4093
4094void
4095target_disable_btrace (struct btrace_target_info *btinfo)
4096{
4097 struct target_ops *t;
4098
4099 for (t = current_target.beneath; t != NULL; t = t->beneath)
4100 if (t->to_disable_btrace != NULL)
d92f7ee3 4101 {
25e95349 4102 t->to_disable_btrace (t, btinfo);
d92f7ee3
SDJ
4103 return;
4104 }
02d27625
MM
4105
4106 tcomplain ();
4107}
4108
4109/* See target.h. */
4110
4111void
4112target_teardown_btrace (struct btrace_target_info *btinfo)
4113{
4114 struct target_ops *t;
4115
4116 for (t = current_target.beneath; t != NULL; t = t->beneath)
4117 if (t->to_teardown_btrace != NULL)
d92f7ee3 4118 {
1777056d 4119 t->to_teardown_btrace (t, btinfo);
d92f7ee3
SDJ
4120 return;
4121 }
02d27625
MM
4122
4123 tcomplain ();
4124}
4125
4126/* See target.h. */
4127
969c39fb
MM
4128enum btrace_error
4129target_read_btrace (VEC (btrace_block_s) **btrace,
4130 struct btrace_target_info *btinfo,
02d27625
MM
4131 enum btrace_read_type type)
4132{
4133 struct target_ops *t;
4134
4135 for (t = current_target.beneath; t != NULL; t = t->beneath)
4136 if (t->to_read_btrace != NULL)
39c49f83 4137 return t->to_read_btrace (t, btrace, btinfo, type);
02d27625
MM
4138
4139 tcomplain ();
969c39fb 4140 return BTRACE_ERR_NOT_SUPPORTED;
02d27625
MM
4141}
4142
d02ed0bb
MM
4143/* See target.h. */
4144
7c1687a9
MM
4145void
4146target_stop_recording (void)
4147{
4148 struct target_ops *t;
4149
4150 for (t = current_target.beneath; t != NULL; t = t->beneath)
4151 if (t->to_stop_recording != NULL)
4152 {
c6cd7c02 4153 t->to_stop_recording (t);
7c1687a9
MM
4154 return;
4155 }
4156
4157 /* This is optional. */
4158}
4159
4160/* See target.h. */
4161
d02ed0bb
MM
4162void
4163target_info_record (void)
4164{
4165 struct target_ops *t;
4166
4167 for (t = current_target.beneath; t != NULL; t = t->beneath)
4168 if (t->to_info_record != NULL)
4169 {
630d6a4a 4170 t->to_info_record (t);
d02ed0bb
MM
4171 return;
4172 }
4173
4174 tcomplain ();
4175}
4176
4177/* See target.h. */
4178
4179void
85e1311a 4180target_save_record (const char *filename)
d02ed0bb
MM
4181{
4182 struct target_ops *t;
4183
4184 for (t = current_target.beneath; t != NULL; t = t->beneath)
4185 if (t->to_save_record != NULL)
4186 {
1390f529 4187 t->to_save_record (t, filename);
d02ed0bb
MM
4188 return;
4189 }
4190
4191 tcomplain ();
4192}
4193
4194/* See target.h. */
4195
4196int
4197target_supports_delete_record (void)
4198{
4199 struct target_ops *t;
4200
4201 for (t = current_target.beneath; t != NULL; t = t->beneath)
4202 if (t->to_delete_record != NULL)
4203 return 1;
4204
4205 return 0;
4206}
4207
4208/* See target.h. */
4209
4210void
4211target_delete_record (void)
4212{
4213 struct target_ops *t;
4214
4215 for (t = current_target.beneath; t != NULL; t = t->beneath)
4216 if (t->to_delete_record != NULL)
4217 {
d1b55219 4218 t->to_delete_record (t);
d02ed0bb
MM
4219 return;
4220 }
4221
4222 tcomplain ();
4223}
4224
4225/* See target.h. */
4226
4227int
4228target_record_is_replaying (void)
4229{
4230 struct target_ops *t;
4231
4232 for (t = current_target.beneath; t != NULL; t = t->beneath)
4233 if (t->to_record_is_replaying != NULL)
1c63c994 4234 return t->to_record_is_replaying (t);
d02ed0bb
MM
4235
4236 return 0;
4237}
4238
4239/* See target.h. */
4240
4241void
4242target_goto_record_begin (void)
4243{
4244 struct target_ops *t;
4245
4246 for (t = current_target.beneath; t != NULL; t = t->beneath)
4247 if (t->to_goto_record_begin != NULL)
4248 {
08475817 4249 t->to_goto_record_begin (t);
d02ed0bb
MM
4250 return;
4251 }
4252
4253 tcomplain ();
4254}
4255
4256/* See target.h. */
4257
4258void
4259target_goto_record_end (void)
4260{
4261 struct target_ops *t;
4262
4263 for (t = current_target.beneath; t != NULL; t = t->beneath)
4264 if (t->to_goto_record_end != NULL)
4265 {
307a1b91 4266 t->to_goto_record_end (t);
d02ed0bb
MM
4267 return;
4268 }
4269
4270 tcomplain ();
4271}
4272
4273/* See target.h. */
4274
4275void
4276target_goto_record (ULONGEST insn)
4277{
4278 struct target_ops *t;
4279
4280 for (t = current_target.beneath; t != NULL; t = t->beneath)
4281 if (t->to_goto_record != NULL)
4282 {
606183ac 4283 t->to_goto_record (t, insn);
d02ed0bb
MM
4284 return;
4285 }
4286
4287 tcomplain ();
4288}
4289
67c86d06
MM
4290/* See target.h. */
4291
4292void
4293target_insn_history (int size, int flags)
4294{
4295 struct target_ops *t;
4296
4297 for (t = current_target.beneath; t != NULL; t = t->beneath)
4298 if (t->to_insn_history != NULL)
4299 {
7a6c5609 4300 t->to_insn_history (t, size, flags);
67c86d06
MM
4301 return;
4302 }
4303
4304 tcomplain ();
4305}
4306
4307/* See target.h. */
4308
4309void
4310target_insn_history_from (ULONGEST from, int size, int flags)
4311{
4312 struct target_ops *t;
4313
4314 for (t = current_target.beneath; t != NULL; t = t->beneath)
4315 if (t->to_insn_history_from != NULL)
4316 {
9abc3ff3 4317 t->to_insn_history_from (t, from, size, flags);
67c86d06
MM
4318 return;
4319 }
4320
4321 tcomplain ();
4322}
4323
4324/* See target.h. */
4325
4326void
4327target_insn_history_range (ULONGEST begin, ULONGEST end, int flags)
4328{
4329 struct target_ops *t;
4330
4331 for (t = current_target.beneath; t != NULL; t = t->beneath)
4332 if (t->to_insn_history_range != NULL)
4333 {
4e99c6b7 4334 t->to_insn_history_range (t, begin, end, flags);
67c86d06
MM
4335 return;
4336 }
4337
4338 tcomplain ();
4339}
4340
15984c13
MM
4341/* See target.h. */
4342
4343void
4344target_call_history (int size, int flags)
4345{
4346 struct target_ops *t;
4347
4348 for (t = current_target.beneath; t != NULL; t = t->beneath)
4349 if (t->to_call_history != NULL)
4350 {
5df2fcba 4351 t->to_call_history (t, size, flags);
15984c13
MM
4352 return;
4353 }
4354
4355 tcomplain ();
4356}
4357
4358/* See target.h. */
4359
4360void
4361target_call_history_from (ULONGEST begin, int size, int flags)
4362{
4363 struct target_ops *t;
4364
4365 for (t = current_target.beneath; t != NULL; t = t->beneath)
4366 if (t->to_call_history_from != NULL)
4367 {
ec0aea04 4368 t->to_call_history_from (t, begin, size, flags);
15984c13
MM
4369 return;
4370 }
4371
4372 tcomplain ();
4373}
4374
4375/* See target.h. */
4376
4377void
4378target_call_history_range (ULONGEST begin, ULONGEST end, int flags)
4379{
4380 struct target_ops *t;
4381
4382 for (t = current_target.beneath; t != NULL; t = t->beneath)
4383 if (t->to_call_history_range != NULL)
4384 {
f0d960ea 4385 t->to_call_history_range (t, begin, end, flags);
15984c13
MM
4386 return;
4387 }
4388
4389 tcomplain ();
4390}
4391
c906108c 4392static void
f32dbf8c 4393debug_to_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 4394{
f32dbf8c 4395 debug_target.to_prepare_to_store (&debug_target, regcache);
c906108c 4396
96baa820 4397 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
4398}
4399
ea001bdc
MM
4400/* See target.h. */
4401
4402const struct frame_unwind *
4403target_get_unwinder (void)
4404{
4405 struct target_ops *t;
4406
4407 for (t = current_target.beneath; t != NULL; t = t->beneath)
4408 if (t->to_get_unwinder != NULL)
4409 return t->to_get_unwinder;
4410
4411 return NULL;
4412}
4413
4414/* See target.h. */
4415
4416const struct frame_unwind *
4417target_get_tailcall_unwinder (void)
4418{
4419 struct target_ops *t;
4420
4421 for (t = current_target.beneath; t != NULL; t = t->beneath)
4422 if (t->to_get_tailcall_unwinder != NULL)
4423 return t->to_get_tailcall_unwinder;
4424
4425 return NULL;
4426}
4427
118e6252
MM
4428/* See target.h. */
4429
4430CORE_ADDR
4431forward_target_decr_pc_after_break (struct target_ops *ops,
4432 struct gdbarch *gdbarch)
4433{
4434 for (; ops != NULL; ops = ops->beneath)
4435 if (ops->to_decr_pc_after_break != NULL)
4436 return ops->to_decr_pc_after_break (ops, gdbarch);
4437
4438 return gdbarch_decr_pc_after_break (gdbarch);
4439}
4440
4441/* See target.h. */
4442
4443CORE_ADDR
4444target_decr_pc_after_break (struct gdbarch *gdbarch)
4445{
4446 return forward_target_decr_pc_after_break (current_target.beneath, gdbarch);
4447}
4448
c906108c 4449static int
961cb7b5 4450deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
4451 int write, struct mem_attrib *attrib,
4452 struct target_ops *target)
c906108c
SS
4453{
4454 int retval;
4455
c8e73a31
AC
4456 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
4457 attrib, target);
c906108c 4458
96baa820 4459 fprintf_unfiltered (gdb_stdlog,
53b71562 4460 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
f5656ead 4461 paddress (target_gdbarch (), memaddr), len,
5af949e3 4462 write ? "write" : "read", retval);
c906108c 4463
c906108c
SS
4464 if (retval > 0)
4465 {
4466 int i;
4467
96baa820 4468 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
4469 for (i = 0; i < retval; i++)
4470 {
53b71562 4471 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
4472 {
4473 if (targetdebug < 2 && i > 0)
4474 {
4475 fprintf_unfiltered (gdb_stdlog, " ...");
4476 break;
4477 }
4478 fprintf_unfiltered (gdb_stdlog, "\n");
4479 }
2bc416ba 4480
96baa820 4481 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
4482 }
4483 }
4484
96baa820 4485 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
4486
4487 return retval;
4488}
4489
4490static void
fba45db2 4491debug_to_files_info (struct target_ops *target)
c906108c
SS
4492{
4493 debug_target.to_files_info (target);
4494
96baa820 4495 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
4496}
4497
4498static int
3db08215 4499debug_to_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
a6d9a66e 4500 struct bp_target_info *bp_tgt)
c906108c
SS
4501{
4502 int retval;
4503
6b84065d 4504 retval = debug_target.to_insert_breakpoint (&debug_target, gdbarch, bp_tgt);
c906108c 4505
96baa820 4506 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4507 "target_insert_breakpoint (%s, xxx) = %ld\n",
4508 core_addr_to_string (bp_tgt->placed_address),
104c1213 4509 (unsigned long) retval);
c906108c
SS
4510 return retval;
4511}
4512
4513static int
3db08215 4514debug_to_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
a6d9a66e 4515 struct bp_target_info *bp_tgt)
c906108c
SS
4516{
4517 int retval;
4518
6b84065d 4519 retval = debug_target.to_remove_breakpoint (&debug_target, gdbarch, bp_tgt);
c906108c 4520
96baa820 4521 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4522 "target_remove_breakpoint (%s, xxx) = %ld\n",
4523 core_addr_to_string (bp_tgt->placed_address),
104c1213 4524 (unsigned long) retval);
c906108c
SS
4525 return retval;
4526}
4527
ccaa32c7 4528static int
5461485a
TT
4529debug_to_can_use_hw_breakpoint (struct target_ops *self,
4530 int type, int cnt, int from_tty)
ccaa32c7
GS
4531{
4532 int retval;
4533
5461485a
TT
4534 retval = debug_target.to_can_use_hw_breakpoint (&debug_target,
4535 type, cnt, from_tty);
ccaa32c7
GS
4536
4537 fprintf_unfiltered (gdb_stdlog,
4538 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
4539 (unsigned long) type,
4540 (unsigned long) cnt,
4541 (unsigned long) from_tty,
4542 (unsigned long) retval);
4543 return retval;
4544}
4545
e0d24f8d 4546static int
31568a15
TT
4547debug_to_region_ok_for_hw_watchpoint (struct target_ops *self,
4548 CORE_ADDR addr, int len)
e0d24f8d
WZ
4549{
4550 CORE_ADDR retval;
4551
31568a15
TT
4552 retval = debug_target.to_region_ok_for_hw_watchpoint (&debug_target,
4553 addr, len);
e0d24f8d
WZ
4554
4555 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4556 "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
4557 core_addr_to_string (addr), (unsigned long) len,
4558 core_addr_to_string (retval));
e0d24f8d
WZ
4559 return retval;
4560}
4561
0cf6dd15 4562static int
c3a5ff89
TT
4563debug_to_can_accel_watchpoint_condition (struct target_ops *self,
4564 CORE_ADDR addr, int len, int rw,
0cf6dd15
TJB
4565 struct expression *cond)
4566{
4567 int retval;
4568
c3a5ff89
TT
4569 retval = debug_target.to_can_accel_watchpoint_condition (&debug_target,
4570 addr, len,
3e43a32a 4571 rw, cond);
0cf6dd15
TJB
4572
4573 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
4574 "target_can_accel_watchpoint_condition "
4575 "(%s, %d, %d, %s) = %ld\n",
bd91e7ae
OS
4576 core_addr_to_string (addr), len, rw,
4577 host_address_to_string (cond), (unsigned long) retval);
0cf6dd15
TJB
4578 return retval;
4579}
4580
ccaa32c7 4581static int
6a109b6b 4582debug_to_stopped_by_watchpoint (struct target_ops *ops)
ccaa32c7
GS
4583{
4584 int retval;
4585
6a109b6b 4586 retval = debug_target.to_stopped_by_watchpoint (&debug_target);
ccaa32c7
GS
4587
4588 fprintf_unfiltered (gdb_stdlog,
d92524f1 4589 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
4590 (unsigned long) retval);
4591 return retval;
4592}
4593
4aa7a7f5
JJ
4594static int
4595debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 4596{
4aa7a7f5 4597 int retval;
ccaa32c7 4598
4aa7a7f5 4599 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
4600
4601 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4602 "target_stopped_data_address ([%s]) = %ld\n",
4603 core_addr_to_string (*addr),
4aa7a7f5 4604 (unsigned long)retval);
ccaa32c7
GS
4605 return retval;
4606}
4607
5009afc5
AS
4608static int
4609debug_to_watchpoint_addr_within_range (struct target_ops *target,
4610 CORE_ADDR addr,
4611 CORE_ADDR start, int length)
4612{
4613 int retval;
4614
4615 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
4616 start, length);
4617
4618 fprintf_filtered (gdb_stdlog,
bd91e7ae
OS
4619 "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
4620 core_addr_to_string (addr), core_addr_to_string (start),
4621 length, retval);
5009afc5
AS
4622 return retval;
4623}
4624
ccaa32c7 4625static int
23a26771
TT
4626debug_to_insert_hw_breakpoint (struct target_ops *self,
4627 struct gdbarch *gdbarch,
a6d9a66e 4628 struct bp_target_info *bp_tgt)
ccaa32c7
GS
4629{
4630 int retval;
4631
23a26771
TT
4632 retval = debug_target.to_insert_hw_breakpoint (&debug_target,
4633 gdbarch, bp_tgt);
ccaa32c7
GS
4634
4635 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4636 "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
4637 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
4638 (unsigned long) retval);
4639 return retval;
4640}
4641
4642static int
a64dc96c
TT
4643debug_to_remove_hw_breakpoint (struct target_ops *self,
4644 struct gdbarch *gdbarch,
a6d9a66e 4645 struct bp_target_info *bp_tgt)
ccaa32c7
GS
4646{
4647 int retval;
4648
a64dc96c
TT
4649 retval = debug_target.to_remove_hw_breakpoint (&debug_target,
4650 gdbarch, bp_tgt);
ccaa32c7
GS
4651
4652 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4653 "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
4654 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
4655 (unsigned long) retval);
4656 return retval;
4657}
4658
4659static int
7bb99c53
TT
4660debug_to_insert_watchpoint (struct target_ops *self,
4661 CORE_ADDR addr, int len, int type,
0cf6dd15 4662 struct expression *cond)
ccaa32c7
GS
4663{
4664 int retval;
4665
7bb99c53
TT
4666 retval = debug_target.to_insert_watchpoint (&debug_target,
4667 addr, len, type, cond);
ccaa32c7
GS
4668
4669 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4670 "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
4671 core_addr_to_string (addr), len, type,
4672 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
4673 return retval;
4674}
4675
4676static int
11b5219a
TT
4677debug_to_remove_watchpoint (struct target_ops *self,
4678 CORE_ADDR addr, int len, int type,
0cf6dd15 4679 struct expression *cond)
ccaa32c7
GS
4680{
4681 int retval;
4682
11b5219a
TT
4683 retval = debug_target.to_remove_watchpoint (&debug_target,
4684 addr, len, type, cond);
ccaa32c7
GS
4685
4686 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
4687 "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
4688 core_addr_to_string (addr), len, type,
4689 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
4690 return retval;
4691}
4692
c906108c 4693static void
c42bf286 4694debug_to_terminal_init (struct target_ops *self)
c906108c 4695{
c42bf286 4696 debug_target.to_terminal_init (&debug_target);
c906108c 4697
96baa820 4698 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
4699}
4700
4701static void
d2f640d4 4702debug_to_terminal_inferior (struct target_ops *self)
c906108c 4703{
d2f640d4 4704 debug_target.to_terminal_inferior (&debug_target);
c906108c 4705
96baa820 4706 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
4707}
4708
4709static void
2e1e1a19 4710debug_to_terminal_ours_for_output (struct target_ops *self)
c906108c 4711{
2e1e1a19 4712 debug_target.to_terminal_ours_for_output (&debug_target);
c906108c 4713
96baa820 4714 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
4715}
4716
4717static void
e3594fd1 4718debug_to_terminal_ours (struct target_ops *self)
c906108c 4719{
e3594fd1 4720 debug_target.to_terminal_ours (&debug_target);
c906108c 4721
96baa820 4722 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
4723}
4724
a790ad35 4725static void
ae3bd431 4726debug_to_terminal_save_ours (struct target_ops *self)
a790ad35 4727{
ae3bd431 4728 debug_target.to_terminal_save_ours (&debug_target);
a790ad35
SC
4729
4730 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
4731}
4732
c906108c 4733static void
0a4f40a2
TT
4734debug_to_terminal_info (struct target_ops *self,
4735 const char *arg, int from_tty)
c906108c 4736{
0a4f40a2 4737 debug_target.to_terminal_info (&debug_target, arg, from_tty);
c906108c 4738
96baa820 4739 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
4740 from_tty);
4741}
4742
c906108c 4743static void
71a9f134 4744debug_to_load (struct target_ops *self, char *args, int from_tty)
c906108c 4745{
71a9f134 4746 debug_target.to_load (&debug_target, args, from_tty);
c906108c 4747
96baa820 4748 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
4749}
4750
c906108c 4751static void
2e97a79e 4752debug_to_post_startup_inferior (struct target_ops *self, ptid_t ptid)
c906108c 4753{
2e97a79e 4754 debug_target.to_post_startup_inferior (&debug_target, ptid);
c906108c 4755
96baa820 4756 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
dfd4cc63 4757 ptid_get_pid (ptid));
c906108c
SS
4758}
4759
77b06cd7 4760static int
a863b201 4761debug_to_insert_fork_catchpoint (struct target_ops *self, int pid)
c906108c 4762{
77b06cd7
TJB
4763 int retval;
4764
a863b201 4765 retval = debug_target.to_insert_fork_catchpoint (&debug_target, pid);
77b06cd7
TJB
4766
4767 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
4768 pid, retval);
c906108c 4769
77b06cd7 4770 return retval;
c906108c
SS
4771}
4772
4773static int
973fc227 4774debug_to_remove_fork_catchpoint (struct target_ops *self, int pid)
c906108c 4775{
c5aa993b 4776 int retval;
c906108c 4777
973fc227 4778 retval = debug_target.to_remove_fork_catchpoint (&debug_target, pid);
c906108c 4779
96baa820 4780 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 4781 pid, retval);
c906108c
SS
4782
4783 return retval;
4784}
4785
77b06cd7 4786static int
3ecc7da0 4787debug_to_insert_vfork_catchpoint (struct target_ops *self, int pid)
c906108c 4788{
77b06cd7
TJB
4789 int retval;
4790
3ecc7da0 4791 retval = debug_target.to_insert_vfork_catchpoint (&debug_target, pid);
c906108c 4792
77b06cd7
TJB
4793 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4794 pid, retval);
4795
4796 return retval;
c906108c
SS
4797}
4798
4799static int
e98cf0cd 4800debug_to_remove_vfork_catchpoint (struct target_ops *self, int pid)
c906108c 4801{
c5aa993b 4802 int retval;
c906108c 4803
e98cf0cd 4804 retval = debug_target.to_remove_vfork_catchpoint (&debug_target, pid);
c906108c 4805
96baa820 4806 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 4807 pid, retval);
c906108c
SS
4808
4809 return retval;
4810}
4811
77b06cd7 4812static int
ba025e51 4813debug_to_insert_exec_catchpoint (struct target_ops *self, int pid)
c906108c 4814{
77b06cd7
TJB
4815 int retval;
4816
ba025e51 4817 retval = debug_target.to_insert_exec_catchpoint (&debug_target, pid);
c906108c 4818
77b06cd7
TJB
4819 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4820 pid, retval);
4821
4822 return retval;
c906108c
SS
4823}
4824
4825static int
758e29d2 4826debug_to_remove_exec_catchpoint (struct target_ops *self, int pid)
c906108c 4827{
c5aa993b 4828 int retval;
c906108c 4829
758e29d2 4830 retval = debug_target.to_remove_exec_catchpoint (&debug_target, pid);
c906108c 4831
96baa820 4832 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 4833 pid, retval);
c906108c
SS
4834
4835 return retval;
4836}
4837
c906108c 4838static int
d796e1d6
TT
4839debug_to_has_exited (struct target_ops *self,
4840 int pid, int wait_status, int *exit_status)
c906108c 4841{
c5aa993b 4842 int has_exited;
c906108c 4843
d796e1d6
TT
4844 has_exited = debug_target.to_has_exited (&debug_target,
4845 pid, wait_status, exit_status);
c906108c 4846
96baa820 4847 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 4848 pid, wait_status, *exit_status, has_exited);
c906108c
SS
4849
4850 return has_exited;
4851}
4852
c906108c 4853static int
da82bd6b 4854debug_to_can_run (struct target_ops *self)
c906108c
SS
4855{
4856 int retval;
4857
da82bd6b 4858 retval = debug_target.to_can_run (&debug_target);
c906108c 4859
96baa820 4860 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
4861
4862 return retval;
4863}
4864
c2250ad1
UW
4865static struct gdbarch *
4866debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
4867{
4868 struct gdbarch *retval;
4869
4870 retval = debug_target.to_thread_architecture (ops, ptid);
4871
3e43a32a
MS
4872 fprintf_unfiltered (gdb_stdlog,
4873 "target_thread_architecture (%s) = %s [%s]\n",
4874 target_pid_to_str (ptid),
4875 host_address_to_string (retval),
c2250ad1
UW
4876 gdbarch_bfd_arch_info (retval)->printable_name);
4877 return retval;
4878}
4879
c906108c 4880static void
1eab8a48 4881debug_to_stop (struct target_ops *self, ptid_t ptid)
c906108c 4882{
1eab8a48 4883 debug_target.to_stop (&debug_target, ptid);
c906108c 4884
94cc34af
PA
4885 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
4886 target_pid_to_str (ptid));
c906108c
SS
4887}
4888
96baa820 4889static void
1aac633b 4890debug_to_rcmd (struct target_ops *self, char *command,
d9fcf2fb 4891 struct ui_file *outbuf)
96baa820 4892{
1aac633b 4893 debug_target.to_rcmd (&debug_target, command, outbuf);
96baa820
JM
4894 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
4895}
4896
c906108c 4897static char *
8dd27370 4898debug_to_pid_to_exec_file (struct target_ops *self, int pid)
c906108c 4899{
c5aa993b 4900 char *exec_file;
c906108c 4901
8dd27370 4902 exec_file = debug_target.to_pid_to_exec_file (&debug_target, pid);
c906108c 4903
96baa820 4904 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 4905 pid, exec_file);
c906108c
SS
4906
4907 return exec_file;
4908}
4909
c906108c 4910static void
fba45db2 4911setup_target_debug (void)
c906108c
SS
4912{
4913 memcpy (&debug_target, &current_target, sizeof debug_target);
4914
4915 current_target.to_open = debug_to_open;
c906108c 4916 current_target.to_post_attach = debug_to_post_attach;
c906108c 4917 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 4918 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
4919 current_target.to_files_info = debug_to_files_info;
4920 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
4921 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
4922 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
4923 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
4924 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
4925 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
4926 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
4927 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
4928 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3e43a32a
MS
4929 current_target.to_watchpoint_addr_within_range
4930 = debug_to_watchpoint_addr_within_range;
4931 current_target.to_region_ok_for_hw_watchpoint
4932 = debug_to_region_ok_for_hw_watchpoint;
4933 current_target.to_can_accel_watchpoint_condition
4934 = debug_to_can_accel_watchpoint_condition;
c906108c
SS
4935 current_target.to_terminal_init = debug_to_terminal_init;
4936 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3e43a32a
MS
4937 current_target.to_terminal_ours_for_output
4938 = debug_to_terminal_ours_for_output;
c906108c 4939 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 4940 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 4941 current_target.to_terminal_info = debug_to_terminal_info;
c906108c 4942 current_target.to_load = debug_to_load;
c906108c 4943 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
c906108c
SS
4944 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
4945 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
4946 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
4947 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
4948 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
4949 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 4950 current_target.to_has_exited = debug_to_has_exited;
c906108c 4951 current_target.to_can_run = debug_to_can_run;
c906108c 4952 current_target.to_stop = debug_to_stop;
96baa820 4953 current_target.to_rcmd = debug_to_rcmd;
c906108c 4954 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 4955 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 4956}
c906108c 4957\f
c5aa993b
JM
4958
4959static char targ_desc[] =
3e43a32a
MS
4960"Names of targets and files being debugged.\nShows the entire \
4961stack of targets currently in use (including the exec-file,\n\
c906108c
SS
4962core-file, and process, if any), as well as the symbol file name.";
4963
a53f3625
TT
4964static void
4965default_rcmd (struct target_ops *self, char *command, struct ui_file *output)
4966{
4967 error (_("\"monitor\" command not supported by this target."));
4968}
4969
96baa820
JM
4970static void
4971do_monitor_command (char *cmd,
4972 int from_tty)
4973{
96baa820
JM
4974 target_rcmd (cmd, gdb_stdtarg);
4975}
4976
87680a14
JB
4977/* Print the name of each layers of our target stack. */
4978
4979static void
4980maintenance_print_target_stack (char *cmd, int from_tty)
4981{
4982 struct target_ops *t;
4983
4984 printf_filtered (_("The current target stack is:\n"));
4985
4986 for (t = target_stack; t != NULL; t = t->beneath)
4987 {
4988 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
4989 }
4990}
4991
c6ebd6cf
VP
4992/* Controls if async mode is permitted. */
4993int target_async_permitted = 0;
4994
4995/* The set command writes to this variable. If the inferior is
b5419e49 4996 executing, target_async_permitted is *not* updated. */
c6ebd6cf
VP
4997static int target_async_permitted_1 = 0;
4998
4999static void
9401a810
PA
5000set_target_async_command (char *args, int from_tty,
5001 struct cmd_list_element *c)
c6ebd6cf 5002{
c35b1492 5003 if (have_live_inferiors ())
c6ebd6cf
VP
5004 {
5005 target_async_permitted_1 = target_async_permitted;
5006 error (_("Cannot change this setting while the inferior is running."));
5007 }
5008
5009 target_async_permitted = target_async_permitted_1;
5010}
5011
5012static void
9401a810
PA
5013show_target_async_command (struct ui_file *file, int from_tty,
5014 struct cmd_list_element *c,
5015 const char *value)
c6ebd6cf 5016{
3e43a32a
MS
5017 fprintf_filtered (file,
5018 _("Controlling the inferior in "
5019 "asynchronous mode is %s.\n"), value);
c6ebd6cf
VP
5020}
5021
d914c394
SS
5022/* Temporary copies of permission settings. */
5023
5024static int may_write_registers_1 = 1;
5025static int may_write_memory_1 = 1;
5026static int may_insert_breakpoints_1 = 1;
5027static int may_insert_tracepoints_1 = 1;
5028static int may_insert_fast_tracepoints_1 = 1;
5029static int may_stop_1 = 1;
5030
5031/* Make the user-set values match the real values again. */
5032
5033void
5034update_target_permissions (void)
5035{
5036 may_write_registers_1 = may_write_registers;
5037 may_write_memory_1 = may_write_memory;
5038 may_insert_breakpoints_1 = may_insert_breakpoints;
5039 may_insert_tracepoints_1 = may_insert_tracepoints;
5040 may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
5041 may_stop_1 = may_stop;
5042}
5043
5044/* The one function handles (most of) the permission flags in the same
5045 way. */
5046
5047static void
5048set_target_permissions (char *args, int from_tty,
5049 struct cmd_list_element *c)
5050{
5051 if (target_has_execution)
5052 {
5053 update_target_permissions ();
5054 error (_("Cannot change this setting while the inferior is running."));
5055 }
5056
5057 /* Make the real values match the user-changed values. */
5058 may_write_registers = may_write_registers_1;
5059 may_insert_breakpoints = may_insert_breakpoints_1;
5060 may_insert_tracepoints = may_insert_tracepoints_1;
5061 may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
5062 may_stop = may_stop_1;
5063 update_observer_mode ();
5064}
5065
5066/* Set memory write permission independently of observer mode. */
5067
5068static void
5069set_write_memory_permission (char *args, int from_tty,
5070 struct cmd_list_element *c)
5071{
5072 /* Make the real values match the user-changed values. */
5073 may_write_memory = may_write_memory_1;
5074 update_observer_mode ();
5075}
5076
5077
c906108c 5078void
fba45db2 5079initialize_targets (void)
c906108c
SS
5080{
5081 init_dummy_target ();
5082 push_target (&dummy_target);
5083
5084 add_info ("target", target_info, targ_desc);
5085 add_info ("files", target_info, targ_desc);
5086
ccce17b0 5087 add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
85c07804
AC
5088Set target debugging."), _("\
5089Show target debugging."), _("\
333dabeb
DJ
5090When non-zero, target debugging is enabled. Higher numbers are more\n\
5091verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804 5092command."),
ccce17b0
YQ
5093 NULL,
5094 show_targetdebug,
5095 &setdebuglist, &showdebuglist);
3a11626d 5096
2bc416ba 5097 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
5098 &trust_readonly, _("\
5099Set mode for reading from readonly sections."), _("\
5100Show mode for reading from readonly sections."), _("\
3a11626d
MS
5101When this mode is on, memory reads from readonly sections (such as .text)\n\
5102will be read from the object file instead of from the target. This will\n\
7915a72c 5103result in significant performance improvement for remote targets."),
2c5b56ce 5104 NULL,
920d2a44 5105 show_trust_readonly,
e707bbc2 5106 &setlist, &showlist);
96baa820
JM
5107
5108 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 5109 _("Send a command to the remote monitor (remote targets only)."));
96baa820 5110
87680a14
JB
5111 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
5112 _("Print the name of each layer of the internal target stack."),
5113 &maintenanceprintlist);
5114
c6ebd6cf
VP
5115 add_setshow_boolean_cmd ("target-async", no_class,
5116 &target_async_permitted_1, _("\
5117Set whether gdb controls the inferior in asynchronous mode."), _("\
5118Show whether gdb controls the inferior in asynchronous mode."), _("\
5119Tells gdb whether to control the inferior in asynchronous mode."),
9401a810
PA
5120 set_target_async_command,
5121 show_target_async_command,
c6ebd6cf
VP
5122 &setlist,
5123 &showlist);
5124
d914c394
SS
5125 add_setshow_boolean_cmd ("may-write-registers", class_support,
5126 &may_write_registers_1, _("\
5127Set permission to write into registers."), _("\
5128Show permission to write into registers."), _("\
5129When this permission is on, GDB may write into the target's registers.\n\
5130Otherwise, any sort of write attempt will result in an error."),
5131 set_target_permissions, NULL,
5132 &setlist, &showlist);
5133
5134 add_setshow_boolean_cmd ("may-write-memory", class_support,
5135 &may_write_memory_1, _("\
5136Set permission to write into target memory."), _("\
5137Show permission to write into target memory."), _("\
5138When this permission is on, GDB may write into the target's memory.\n\
5139Otherwise, any sort of write attempt will result in an error."),
5140 set_write_memory_permission, NULL,
5141 &setlist, &showlist);
5142
5143 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
5144 &may_insert_breakpoints_1, _("\
5145Set permission to insert breakpoints in the target."), _("\
5146Show permission to insert breakpoints in the target."), _("\
5147When this permission is on, GDB may insert breakpoints in the program.\n\
5148Otherwise, any sort of insertion attempt will result in an error."),
5149 set_target_permissions, NULL,
5150 &setlist, &showlist);
5151
5152 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
5153 &may_insert_tracepoints_1, _("\
5154Set permission to insert tracepoints in the target."), _("\
5155Show permission to insert tracepoints in the target."), _("\
5156When this permission is on, GDB may insert tracepoints in the program.\n\
5157Otherwise, any sort of insertion attempt will result in an error."),
5158 set_target_permissions, NULL,
5159 &setlist, &showlist);
5160
5161 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
5162 &may_insert_fast_tracepoints_1, _("\
5163Set permission to insert fast tracepoints in the target."), _("\
5164Show permission to insert fast tracepoints in the target."), _("\
5165When this permission is on, GDB may insert fast tracepoints.\n\
5166Otherwise, any sort of insertion attempt will result in an error."),
5167 set_target_permissions, NULL,
5168 &setlist, &showlist);
5169
5170 add_setshow_boolean_cmd ("may-interrupt", class_support,
5171 &may_stop_1, _("\
5172Set permission to interrupt or signal the target."), _("\
5173Show permission to interrupt or signal the target."), _("\
5174When this permission is on, GDB may interrupt/stop the target's execution.\n\
5175Otherwise, any attempt to interrupt or stop will be ignored."),
5176 set_target_permissions, NULL,
5177 &setlist, &showlist);
c906108c 5178}
This page took 1.924209 seconds and 4 git commands to generate.