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