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