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