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