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