*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3 2
6aba47ca 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4c38e0a4 4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
f6519ebc 5 Free Software Foundation, Inc.
7998dfc3 6
c906108c
SS
7 Contributed by Cygnus Support.
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
c5aa993b 14 (at your option) any later version.
c906108c 15
c5aa993b
JM
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
c906108c 20
c5aa993b 21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
23
24#include "defs.h"
25#include <errno.h>
c906108c
SS
26#include "gdb_string.h"
27#include "target.h"
28#include "gdbcmd.h"
29#include "symtab.h"
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
33#include "objfiles.h"
03f2053f 34#include "gdb_wait.h"
4930751a 35#include "dcache.h"
c906108c 36#include <signal.h>
4e052eda 37#include "regcache.h"
0088c768 38#include "gdb_assert.h"
b6591e8b 39#include "gdbcore.h"
9e35dae4 40#include "exceptions.h"
424163ea 41#include "target-descriptions.h"
e1ac3328 42#include "gdbthread.h"
b9db4ced 43#include "solib.h"
07b82ea5 44#include "exec.h"
edb3359d 45#include "inline-frame.h"
c906108c 46
a14ed312 47static void target_info (char *, int);
c906108c 48
a14ed312 49static void default_terminal_info (char *, int);
c906108c 50
5009afc5
AS
51static int default_watchpoint_addr_within_range (struct target_ops *,
52 CORE_ADDR, CORE_ADDR, int);
53
e0d24f8d
WZ
54static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
55
a14ed312 56static int nosymbol (char *, CORE_ADDR *);
c906108c 57
4ecb6f27 58static void tcomplain (void) ATTR_NORETURN;
c906108c 59
a14ed312 60static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 61
a14ed312 62static int return_zero (void);
c906108c 63
a14ed312 64static int return_one (void);
c906108c 65
ccaa32c7
GS
66static int return_minus_one (void);
67
a14ed312 68void target_ignore (void);
c906108c 69
a14ed312 70static void target_command (char *, int);
c906108c 71
a14ed312 72static struct target_ops *find_default_run_target (char *);
c906108c 73
4b8a223f 74static LONGEST default_xfer_partial (struct target_ops *ops,
0088c768 75 enum target_object object,
1b0ba102
AC
76 const char *annex, gdb_byte *readbuf,
77 const gdb_byte *writebuf,
8aa91c1e 78 ULONGEST offset, LONGEST len);
0088c768 79
cf7a04e8
DJ
80static LONGEST current_xfer_partial (struct target_ops *ops,
81 enum target_object object,
82 const char *annex, gdb_byte *readbuf,
83 const gdb_byte *writebuf,
84 ULONGEST offset, LONGEST len);
c906108c 85
cf7a04e8
DJ
86static LONGEST target_xfer_partial (struct target_ops *ops,
87 enum target_object object,
88 const char *annex,
89 void *readbuf, const void *writebuf,
90 ULONGEST offset, LONGEST len);
c906108c 91
c2250ad1
UW
92static struct gdbarch *default_thread_architecture (struct target_ops *ops,
93 ptid_t ptid);
94
a14ed312 95static void init_dummy_target (void);
c906108c 96
aa869812
AC
97static struct target_ops debug_target;
98
a14ed312 99static void debug_to_open (char *, int);
c906108c 100
316f2060 101static void debug_to_prepare_to_store (struct regcache *);
c906108c 102
a14ed312 103static void debug_to_files_info (struct target_ops *);
c906108c 104
a6d9a66e
UW
105static int debug_to_insert_breakpoint (struct gdbarch *,
106 struct bp_target_info *);
c906108c 107
a6d9a66e
UW
108static int debug_to_remove_breakpoint (struct gdbarch *,
109 struct bp_target_info *);
c906108c 110
ccaa32c7
GS
111static int debug_to_can_use_hw_breakpoint (int, int, int);
112
a6d9a66e
UW
113static int debug_to_insert_hw_breakpoint (struct gdbarch *,
114 struct bp_target_info *);
ccaa32c7 115
a6d9a66e
UW
116static int debug_to_remove_hw_breakpoint (struct gdbarch *,
117 struct bp_target_info *);
ccaa32c7
GS
118
119static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
120
121static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
122
123static int debug_to_stopped_by_watchpoint (void);
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
e0d24f8d
WZ
130static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
131
a14ed312 132static void debug_to_terminal_init (void);
c906108c 133
a14ed312 134static void debug_to_terminal_inferior (void);
c906108c 135
a14ed312 136static void debug_to_terminal_ours_for_output (void);
c906108c 137
a790ad35
SC
138static void debug_to_terminal_save_ours (void);
139
a14ed312 140static void debug_to_terminal_ours (void);
c906108c 141
a14ed312 142static void debug_to_terminal_info (char *, int);
c906108c 143
a14ed312 144static void debug_to_load (char *, int);
c906108c 145
a14ed312 146static int debug_to_lookup_symbol (char *, CORE_ADDR *);
c906108c 147
a14ed312 148static int debug_to_can_run (void);
c906108c 149
39f77062 150static void debug_to_notice_signals (ptid_t);
c906108c 151
94cc34af 152static void debug_to_stop (ptid_t);
c906108c 153
5ac10fd1
AC
154/* NOTE: cagney/2004-09-29: Many targets reference this variable in
155 wierd and mysterious ways. Putting the variable here lets those
156 wierd and mysterious ways keep building while they are being
157 converted to the inferior inheritance structure. */
1df84f13 158struct target_ops deprecated_child_ops;
5ac10fd1 159
c906108c 160/* Pointer to array of target architecture structures; the size of the
2bc416ba 161 array; the current index into the array; the allocated size of the
c906108c
SS
162 array. */
163struct target_ops **target_structs;
164unsigned target_struct_size;
165unsigned target_struct_index;
166unsigned target_struct_allocsize;
167#define DEFAULT_ALLOCSIZE 10
168
169/* The initial current target, so that there is always a semi-valid
170 current target. */
171
172static struct target_ops dummy_target;
173
174/* Top of target stack. */
175
258b763a 176static struct target_ops *target_stack;
c906108c
SS
177
178/* The target structure we are currently using to talk to a process
179 or file or whatever "inferior" we have. */
180
181struct target_ops current_target;
182
183/* Command list for target. */
184
185static struct cmd_list_element *targetlist = NULL;
186
cf7a04e8
DJ
187/* Nonzero if we should trust readonly sections from the
188 executable when reading memory. */
189
190static int trust_readonly = 0;
191
8defab1a
DJ
192/* Nonzero if we should show true memory content including
193 memory breakpoint inserted by gdb. */
194
195static int show_memory_breakpoints = 0;
196
c906108c
SS
197/* Non-zero if we want to see trace of target level stuff. */
198
199static int targetdebug = 0;
920d2a44
AC
200static void
201show_targetdebug (struct ui_file *file, int from_tty,
202 struct cmd_list_element *c, const char *value)
203{
204 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
205}
c906108c 206
a14ed312 207static void setup_target_debug (void);
c906108c 208
4e5d721f
DE
209/* The option sets this. */
210static int stack_cache_enabled_p_1 = 1;
211/* And set_stack_cache_enabled_p updates this.
212 The reason for the separation is so that we don't flush the cache for
213 on->on transitions. */
214static int stack_cache_enabled_p = 1;
215
216/* This is called *after* the stack-cache has been set.
217 Flush the cache for off->on and on->off transitions.
218 There's no real need to flush the cache for on->off transitions,
219 except cleanliness. */
220
221static void
222set_stack_cache_enabled_p (char *args, int from_tty,
223 struct cmd_list_element *c)
224{
225 if (stack_cache_enabled_p != stack_cache_enabled_p_1)
226 target_dcache_invalidate ();
227
228 stack_cache_enabled_p = stack_cache_enabled_p_1;
229}
230
231static void
232show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
233 struct cmd_list_element *c, const char *value)
234{
235 fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
236}
237
238/* Cache of memory operations, to speed up remote access. */
239static DCACHE *target_dcache;
240
241/* Invalidate the target dcache. */
242
243void
244target_dcache_invalidate (void)
245{
246 dcache_invalidate (target_dcache);
247}
4930751a 248
c906108c
SS
249/* The user just typed 'target' without the name of a target. */
250
c906108c 251static void
fba45db2 252target_command (char *arg, int from_tty)
c906108c
SS
253{
254 fputs_filtered ("Argument required (target name). Try `help target'\n",
255 gdb_stdout);
256}
257
c35b1492
PA
258/* Default target_has_* methods for process_stratum targets. */
259
260int
261default_child_has_all_memory (struct target_ops *ops)
262{
263 /* If no inferior selected, then we can't read memory here. */
264 if (ptid_equal (inferior_ptid, null_ptid))
265 return 0;
266
267 return 1;
268}
269
270int
271default_child_has_memory (struct target_ops *ops)
272{
273 /* If no inferior selected, then we can't read memory here. */
274 if (ptid_equal (inferior_ptid, null_ptid))
275 return 0;
276
277 return 1;
278}
279
280int
281default_child_has_stack (struct target_ops *ops)
282{
283 /* If no inferior selected, there's no stack. */
284 if (ptid_equal (inferior_ptid, null_ptid))
285 return 0;
286
287 return 1;
288}
289
290int
291default_child_has_registers (struct target_ops *ops)
292{
293 /* Can't read registers from no inferior. */
294 if (ptid_equal (inferior_ptid, null_ptid))
295 return 0;
296
297 return 1;
298}
299
300int
301default_child_has_execution (struct target_ops *ops)
302{
303 /* If there's no thread selected, then we can't make it run through
304 hoops. */
305 if (ptid_equal (inferior_ptid, null_ptid))
306 return 0;
307
308 return 1;
309}
310
311
312int
313target_has_all_memory_1 (void)
314{
315 struct target_ops *t;
316
317 for (t = current_target.beneath; t != NULL; t = t->beneath)
318 if (t->to_has_all_memory (t))
319 return 1;
320
321 return 0;
322}
323
324int
325target_has_memory_1 (void)
326{
327 struct target_ops *t;
328
329 for (t = current_target.beneath; t != NULL; t = t->beneath)
330 if (t->to_has_memory (t))
331 return 1;
332
333 return 0;
334}
335
336int
337target_has_stack_1 (void)
338{
339 struct target_ops *t;
340
341 for (t = current_target.beneath; t != NULL; t = t->beneath)
342 if (t->to_has_stack (t))
343 return 1;
344
345 return 0;
346}
347
348int
349target_has_registers_1 (void)
350{
351 struct target_ops *t;
352
353 for (t = current_target.beneath; t != NULL; t = t->beneath)
354 if (t->to_has_registers (t))
355 return 1;
356
357 return 0;
358}
359
360int
361target_has_execution_1 (void)
362{
363 struct target_ops *t;
364
365 for (t = current_target.beneath; t != NULL; t = t->beneath)
366 if (t->to_has_execution (t))
367 return 1;
368
369 return 0;
370}
371
c906108c
SS
372/* Add a possible target architecture to the list. */
373
374void
fba45db2 375add_target (struct target_ops *t)
c906108c 376{
0088c768 377 /* Provide default values for all "must have" methods. */
0b603eba
AC
378 if (t->to_xfer_partial == NULL)
379 t->to_xfer_partial = default_xfer_partial;
0088c768 380
c35b1492
PA
381 if (t->to_has_all_memory == NULL)
382 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
383
384 if (t->to_has_memory == NULL)
385 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
386
387 if (t->to_has_stack == NULL)
388 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
389
390 if (t->to_has_registers == NULL)
391 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
392
393 if (t->to_has_execution == NULL)
394 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
395
c906108c
SS
396 if (!target_structs)
397 {
398 target_struct_allocsize = DEFAULT_ALLOCSIZE;
399 target_structs = (struct target_ops **) xmalloc
400 (target_struct_allocsize * sizeof (*target_structs));
401 }
402 if (target_struct_size >= target_struct_allocsize)
403 {
404 target_struct_allocsize *= 2;
405 target_structs = (struct target_ops **)
c5aa993b
JM
406 xrealloc ((char *) target_structs,
407 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
408 }
409 target_structs[target_struct_size++] = t;
c906108c
SS
410
411 if (targetlist == NULL)
1bedd215
AC
412 add_prefix_cmd ("target", class_run, target_command, _("\
413Connect to a target machine or process.\n\
c906108c
SS
414The first argument is the type or protocol of the target machine.\n\
415Remaining arguments are interpreted by the target protocol. For more\n\
416information on the arguments for a particular protocol, type\n\
1bedd215 417`help target ' followed by the protocol name."),
c906108c
SS
418 &targetlist, "target ", 0, &cmdlist);
419 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
420}
421
422/* Stub functions */
423
424void
fba45db2 425target_ignore (void)
c906108c
SS
426{
427}
428
7d85a9c0
JB
429void
430target_kill (void)
431{
432 struct target_ops *t;
433
434 for (t = current_target.beneath; t != NULL; t = t->beneath)
435 if (t->to_kill != NULL)
436 {
437 if (targetdebug)
438 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
439
440 t->to_kill (t);
441 return;
442 }
443
444 noprocess ();
445}
446
11cf8741
JM
447void
448target_load (char *arg, int from_tty)
449{
4e5d721f 450 target_dcache_invalidate ();
11cf8741
JM
451 (*current_target.to_load) (arg, from_tty);
452}
453
947b8855
PA
454void
455target_create_inferior (char *exec_file, char *args,
456 char **env, int from_tty)
136d6dae
VP
457{
458 struct target_ops *t;
459 for (t = current_target.beneath; t != NULL; t = t->beneath)
460 {
461 if (t->to_create_inferior != NULL)
462 {
463 t->to_create_inferior (t, exec_file, args, env, from_tty);
947b8855
PA
464 if (targetdebug)
465 fprintf_unfiltered (gdb_stdlog,
466 "target_create_inferior (%s, %s, xxx, %d)\n",
467 exec_file, args, from_tty);
136d6dae
VP
468 return;
469 }
470 }
471
472 internal_error (__FILE__, __LINE__,
473 "could not find a target to create inferior");
474}
475
d9d2d8b6
PA
476void
477target_terminal_inferior (void)
478{
479 /* A background resume (``run&'') should leave GDB in control of the
ba7f6c64
VP
480 terminal. Use target_can_async_p, not target_is_async_p, since at
481 this point the target is not async yet. However, if sync_execution
482 is not set, we know it will become async prior to resume. */
483 if (target_can_async_p () && !sync_execution)
d9d2d8b6
PA
484 return;
485
486 /* If GDB is resuming the inferior in the foreground, install
487 inferior's terminal modes. */
488 (*current_target.to_terminal_inferior) ();
489}
136d6dae 490
c906108c 491static int
fba45db2
KB
492nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
493 struct target_ops *t)
c906108c 494{
c5aa993b
JM
495 errno = EIO; /* Can't read/write this location */
496 return 0; /* No bytes handled */
c906108c
SS
497}
498
499static void
fba45db2 500tcomplain (void)
c906108c 501{
8a3fe4f8 502 error (_("You can't do that when your target is `%s'"),
c906108c
SS
503 current_target.to_shortname);
504}
505
506void
fba45db2 507noprocess (void)
c906108c 508{
8a3fe4f8 509 error (_("You can't do that without a process to debug."));
c906108c
SS
510}
511
c906108c 512static int
fba45db2 513nosymbol (char *name, CORE_ADDR *addrp)
c906108c 514{
c5aa993b 515 return 1; /* Symbol does not exist in target env */
c906108c
SS
516}
517
c906108c 518static void
fba45db2 519default_terminal_info (char *args, int from_tty)
c906108c 520{
a3f17187 521 printf_unfiltered (_("No saved terminal information.\n"));
c906108c
SS
522}
523
0ef643c8
JB
524/* A default implementation for the to_get_ada_task_ptid target method.
525
526 This function builds the PTID by using both LWP and TID as part of
527 the PTID lwp and tid elements. The pid used is the pid of the
528 inferior_ptid. */
529
2c0b251b 530static ptid_t
0ef643c8
JB
531default_get_ada_task_ptid (long lwp, long tid)
532{
533 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
534}
535
7998dfc3
AC
536/* Go through the target stack from top to bottom, copying over zero
537 entries in current_target, then filling in still empty entries. In
538 effect, we are doing class inheritance through the pushed target
539 vectors.
540
541 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
542 is currently implemented, is that it discards any knowledge of
543 which target an inherited method originally belonged to.
544 Consequently, new new target methods should instead explicitly and
545 locally search the target stack for the target that can handle the
546 request. */
c906108c
SS
547
548static void
7998dfc3 549update_current_target (void)
c906108c 550{
7998dfc3
AC
551 struct target_ops *t;
552
08d8bcd7 553 /* First, reset current's contents. */
7998dfc3
AC
554 memset (&current_target, 0, sizeof (current_target));
555
556#define INHERIT(FIELD, TARGET) \
557 if (!current_target.FIELD) \
558 current_target.FIELD = (TARGET)->FIELD
559
560 for (t = target_stack; t; t = t->beneath)
561 {
562 INHERIT (to_shortname, t);
563 INHERIT (to_longname, t);
564 INHERIT (to_doc, t);
b52323fa
UW
565 /* Do not inherit to_open. */
566 /* Do not inherit to_close. */
136d6dae 567 /* Do not inherit to_attach. */
7998dfc3 568 INHERIT (to_post_attach, t);
dc177b7a 569 INHERIT (to_attach_no_wait, t);
136d6dae 570 /* Do not inherit to_detach. */
597320e7 571 /* Do not inherit to_disconnect. */
28439f5e 572 /* Do not inherit to_resume. */
117de6a9 573 /* Do not inherit to_wait. */
28439f5e
PA
574 /* Do not inherit to_fetch_registers. */
575 /* Do not inherit to_store_registers. */
7998dfc3 576 INHERIT (to_prepare_to_store, t);
c8e73a31 577 INHERIT (deprecated_xfer_memory, t);
7998dfc3
AC
578 INHERIT (to_files_info, t);
579 INHERIT (to_insert_breakpoint, t);
580 INHERIT (to_remove_breakpoint, t);
581 INHERIT (to_can_use_hw_breakpoint, t);
582 INHERIT (to_insert_hw_breakpoint, t);
583 INHERIT (to_remove_hw_breakpoint, t);
584 INHERIT (to_insert_watchpoint, t);
585 INHERIT (to_remove_watchpoint, t);
586 INHERIT (to_stopped_data_address, t);
74174d2e 587 INHERIT (to_have_steppable_watchpoint, t);
7998dfc3 588 INHERIT (to_have_continuable_watchpoint, t);
5009afc5
AS
589 INHERIT (to_stopped_by_watchpoint, t);
590 INHERIT (to_watchpoint_addr_within_range, t);
e0d24f8d 591 INHERIT (to_region_ok_for_hw_watchpoint, t);
7998dfc3
AC
592 INHERIT (to_terminal_init, t);
593 INHERIT (to_terminal_inferior, t);
594 INHERIT (to_terminal_ours_for_output, t);
595 INHERIT (to_terminal_ours, t);
596 INHERIT (to_terminal_save_ours, t);
597 INHERIT (to_terminal_info, t);
7d85a9c0 598 /* Do not inherit to_kill. */
7998dfc3
AC
599 INHERIT (to_load, t);
600 INHERIT (to_lookup_symbol, t);
136d6dae 601 /* Do no inherit to_create_inferior. */
7998dfc3
AC
602 INHERIT (to_post_startup_inferior, t);
603 INHERIT (to_acknowledge_created_inferior, t);
604 INHERIT (to_insert_fork_catchpoint, t);
605 INHERIT (to_remove_fork_catchpoint, t);
606 INHERIT (to_insert_vfork_catchpoint, t);
607 INHERIT (to_remove_vfork_catchpoint, t);
ee057212 608 /* Do not inherit to_follow_fork. */
7998dfc3
AC
609 INHERIT (to_insert_exec_catchpoint, t);
610 INHERIT (to_remove_exec_catchpoint, t);
a96d9b2e 611 INHERIT (to_set_syscall_catchpoint, t);
7998dfc3 612 INHERIT (to_has_exited, t);
117de6a9 613 /* Do not inherit to_mourn_inferiour. */
7998dfc3
AC
614 INHERIT (to_can_run, t);
615 INHERIT (to_notice_signals, t);
28439f5e
PA
616 /* Do not inherit to_thread_alive. */
617 /* Do not inherit to_find_new_threads. */
117de6a9 618 /* Do not inherit to_pid_to_str. */
7998dfc3
AC
619 INHERIT (to_extra_thread_info, t);
620 INHERIT (to_stop, t);
4b8a223f 621 /* Do not inherit to_xfer_partial. */
7998dfc3 622 INHERIT (to_rcmd, t);
7998dfc3 623 INHERIT (to_pid_to_exec_file, t);
49d03eab 624 INHERIT (to_log_command, t);
7998dfc3 625 INHERIT (to_stratum, t);
c35b1492
PA
626 /* Do not inherit to_has_all_memory */
627 /* Do not inherit to_has_memory */
628 /* Do not inherit to_has_stack */
629 /* Do not inherit to_has_registers */
630 /* Do not inherit to_has_execution */
7998dfc3 631 INHERIT (to_has_thread_control, t);
7998dfc3
AC
632 INHERIT (to_can_async_p, t);
633 INHERIT (to_is_async_p, t);
634 INHERIT (to_async, t);
b84876c2 635 INHERIT (to_async_mask, t);
7998dfc3
AC
636 INHERIT (to_find_memory_regions, t);
637 INHERIT (to_make_corefile_notes, t);
6b04bdb7
MS
638 INHERIT (to_get_bookmark, t);
639 INHERIT (to_goto_bookmark, t);
117de6a9 640 /* Do not inherit to_get_thread_local_address. */
b2175913 641 INHERIT (to_can_execute_reverse, t);
c2250ad1 642 INHERIT (to_thread_architecture, t);
424163ea 643 /* Do not inherit to_read_description. */
0ef643c8 644 INHERIT (to_get_ada_task_ptid, t);
08388c79 645 /* Do not inherit to_search_memory. */
8a305172 646 INHERIT (to_supports_multi_process, t);
35b1e5cc
SS
647 INHERIT (to_trace_init, t);
648 INHERIT (to_download_tracepoint, t);
649 INHERIT (to_download_trace_state_variable, t);
650 INHERIT (to_trace_set_readonly_regions, t);
651 INHERIT (to_trace_start, t);
652 INHERIT (to_get_trace_status, t);
653 INHERIT (to_trace_stop, t);
654 INHERIT (to_trace_find, t);
655 INHERIT (to_get_trace_state_variable_value, t);
00bf0b85
SS
656 INHERIT (to_save_trace_data, t);
657 INHERIT (to_upload_tracepoints, t);
658 INHERIT (to_upload_trace_state_variables, t);
659 INHERIT (to_get_raw_trace_data, t);
35b1e5cc 660 INHERIT (to_set_disconnected_tracing, t);
7998dfc3 661 INHERIT (to_magic, t);
fd79ecee 662 /* Do not inherit to_memory_map. */
a76d924d
DJ
663 /* Do not inherit to_flash_erase. */
664 /* Do not inherit to_flash_done. */
7998dfc3
AC
665 }
666#undef INHERIT
667
668 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
669 it. Some entries are defaulted to a method that print an error,
670 others are hard-wired to a standard recursive default. */
c906108c
SS
671
672#define de_fault(field, value) \
7998dfc3
AC
673 if (!current_target.field) \
674 current_target.field = value
0d06e24b 675
2bc416ba
DJ
676 de_fault (to_open,
677 (void (*) (char *, int))
0d06e24b 678 tcomplain);
2bc416ba
DJ
679 de_fault (to_close,
680 (void (*) (int))
0d06e24b 681 target_ignore);
2bc416ba
DJ
682 de_fault (to_post_attach,
683 (void (*) (int))
0d06e24b 684 target_ignore);
2bc416ba 685 de_fault (to_prepare_to_store,
316f2060 686 (void (*) (struct regcache *))
0d06e24b 687 noprocess);
2bc416ba
DJ
688 de_fault (deprecated_xfer_memory,
689 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
0d06e24b 690 nomemory);
2bc416ba
DJ
691 de_fault (to_files_info,
692 (void (*) (struct target_ops *))
0d06e24b 693 target_ignore);
2bc416ba 694 de_fault (to_insert_breakpoint,
0d06e24b 695 memory_insert_breakpoint);
2bc416ba 696 de_fault (to_remove_breakpoint,
0d06e24b 697 memory_remove_breakpoint);
ccaa32c7
GS
698 de_fault (to_can_use_hw_breakpoint,
699 (int (*) (int, int, int))
700 return_zero);
701 de_fault (to_insert_hw_breakpoint,
a6d9a66e 702 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
703 return_minus_one);
704 de_fault (to_remove_hw_breakpoint,
a6d9a66e 705 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
706 return_minus_one);
707 de_fault (to_insert_watchpoint,
708 (int (*) (CORE_ADDR, int, int))
709 return_minus_one);
710 de_fault (to_remove_watchpoint,
711 (int (*) (CORE_ADDR, int, int))
712 return_minus_one);
713 de_fault (to_stopped_by_watchpoint,
714 (int (*) (void))
715 return_zero);
716 de_fault (to_stopped_data_address,
4aa7a7f5 717 (int (*) (struct target_ops *, CORE_ADDR *))
ccaa32c7 718 return_zero);
5009afc5
AS
719 de_fault (to_watchpoint_addr_within_range,
720 default_watchpoint_addr_within_range);
e0d24f8d
WZ
721 de_fault (to_region_ok_for_hw_watchpoint,
722 default_region_ok_for_hw_watchpoint);
2bc416ba
DJ
723 de_fault (to_terminal_init,
724 (void (*) (void))
0d06e24b 725 target_ignore);
2bc416ba
DJ
726 de_fault (to_terminal_inferior,
727 (void (*) (void))
0d06e24b 728 target_ignore);
2bc416ba
DJ
729 de_fault (to_terminal_ours_for_output,
730 (void (*) (void))
0d06e24b 731 target_ignore);
2bc416ba
DJ
732 de_fault (to_terminal_ours,
733 (void (*) (void))
0d06e24b 734 target_ignore);
2bc416ba
DJ
735 de_fault (to_terminal_save_ours,
736 (void (*) (void))
a790ad35 737 target_ignore);
2bc416ba 738 de_fault (to_terminal_info,
0d06e24b 739 default_terminal_info);
2bc416ba
DJ
740 de_fault (to_load,
741 (void (*) (char *, int))
0d06e24b 742 tcomplain);
2bc416ba
DJ
743 de_fault (to_lookup_symbol,
744 (int (*) (char *, CORE_ADDR *))
0d06e24b 745 nosymbol);
2bc416ba
DJ
746 de_fault (to_post_startup_inferior,
747 (void (*) (ptid_t))
0d06e24b 748 target_ignore);
2bc416ba
DJ
749 de_fault (to_acknowledge_created_inferior,
750 (void (*) (int))
0d06e24b 751 target_ignore);
2bc416ba
DJ
752 de_fault (to_insert_fork_catchpoint,
753 (void (*) (int))
0d06e24b 754 tcomplain);
2bc416ba
DJ
755 de_fault (to_remove_fork_catchpoint,
756 (int (*) (int))
0d06e24b 757 tcomplain);
2bc416ba
DJ
758 de_fault (to_insert_vfork_catchpoint,
759 (void (*) (int))
0d06e24b 760 tcomplain);
2bc416ba
DJ
761 de_fault (to_remove_vfork_catchpoint,
762 (int (*) (int))
0d06e24b 763 tcomplain);
2bc416ba
DJ
764 de_fault (to_insert_exec_catchpoint,
765 (void (*) (int))
0d06e24b 766 tcomplain);
2bc416ba
DJ
767 de_fault (to_remove_exec_catchpoint,
768 (int (*) (int))
0d06e24b 769 tcomplain);
a96d9b2e
SDJ
770 de_fault (to_set_syscall_catchpoint,
771 (int (*) (int, int, int, int, int *))
772 tcomplain);
2bc416ba
DJ
773 de_fault (to_has_exited,
774 (int (*) (int, int, int *))
0d06e24b 775 return_zero);
2bc416ba 776 de_fault (to_can_run,
0d06e24b 777 return_zero);
2bc416ba
DJ
778 de_fault (to_notice_signals,
779 (void (*) (ptid_t))
0d06e24b 780 target_ignore);
2bc416ba
DJ
781 de_fault (to_extra_thread_info,
782 (char *(*) (struct thread_info *))
0d06e24b 783 return_zero);
2bc416ba 784 de_fault (to_stop,
94cc34af 785 (void (*) (ptid_t))
0d06e24b 786 target_ignore);
cf7a04e8 787 current_target.to_xfer_partial = current_xfer_partial;
2bc416ba
DJ
788 de_fault (to_rcmd,
789 (void (*) (char *, struct ui_file *))
0d06e24b 790 tcomplain);
2bc416ba
DJ
791 de_fault (to_pid_to_exec_file,
792 (char *(*) (int))
0d06e24b 793 return_zero);
2bc416ba
DJ
794 de_fault (to_async,
795 (void (*) (void (*) (enum inferior_event_type, void*), void*))
0d06e24b 796 tcomplain);
b84876c2
PA
797 de_fault (to_async_mask,
798 (int (*) (int))
799 return_one);
c2250ad1
UW
800 de_fault (to_thread_architecture,
801 default_thread_architecture);
424163ea 802 current_target.to_read_description = NULL;
0ef643c8
JB
803 de_fault (to_get_ada_task_ptid,
804 (ptid_t (*) (long, long))
805 default_get_ada_task_ptid);
8a305172
PA
806 de_fault (to_supports_multi_process,
807 (int (*) (void))
808 return_zero);
35b1e5cc
SS
809 de_fault (to_trace_init,
810 (void (*) (void))
811 tcomplain);
812 de_fault (to_download_tracepoint,
813 (void (*) (struct breakpoint *))
814 tcomplain);
815 de_fault (to_download_trace_state_variable,
816 (void (*) (struct trace_state_variable *))
817 tcomplain);
818 de_fault (to_trace_set_readonly_regions,
819 (void (*) (void))
820 tcomplain);
821 de_fault (to_trace_start,
822 (void (*) (void))
823 tcomplain);
824 de_fault (to_get_trace_status,
00bf0b85 825 (int (*) (struct trace_status *))
35b1e5cc
SS
826 return_minus_one);
827 de_fault (to_trace_stop,
828 (void (*) (void))
829 tcomplain);
830 de_fault (to_trace_find,
831 (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
832 return_zero);
833 de_fault (to_get_trace_state_variable_value,
834 (int (*) (int, LONGEST *))
835 return_zero);
00bf0b85
SS
836 de_fault (to_save_trace_data,
837 (int (*) (char *))
838 tcomplain);
839 de_fault (to_upload_tracepoints,
840 (int (*) (struct uploaded_tp **))
841 return_zero);
842 de_fault (to_upload_trace_state_variables,
843 (int (*) (struct uploaded_tsv **))
844 return_zero);
845 de_fault (to_get_raw_trace_data,
846 (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
847 tcomplain);
35b1e5cc
SS
848 de_fault (to_set_disconnected_tracing,
849 (void (*) (int))
850 tcomplain);
c906108c 851#undef de_fault
c906108c 852
7998dfc3
AC
853 /* Finally, position the target-stack beneath the squashed
854 "current_target". That way code looking for a non-inherited
855 target method can quickly and simply find it. */
856 current_target.beneath = target_stack;
b4b61fdb
DJ
857
858 if (targetdebug)
859 setup_target_debug ();
c906108c
SS
860}
861
862/* Push a new target type into the stack of the existing target accessors,
863 possibly superseding some of the existing accessors.
864
865 Result is zero if the pushed target ended up on top of the stack,
866 nonzero if at least one target is on top of it.
867
868 Rather than allow an empty stack, we always have the dummy target at
869 the bottom stratum, so we can call the function vectors without
870 checking them. */
871
872int
fba45db2 873push_target (struct target_ops *t)
c906108c 874{
258b763a 875 struct target_ops **cur;
c906108c
SS
876
877 /* Check magic number. If wrong, it probably means someone changed
878 the struct definition, but not all the places that initialize one. */
879 if (t->to_magic != OPS_MAGIC)
880 {
c5aa993b
JM
881 fprintf_unfiltered (gdb_stderr,
882 "Magic number of %s target struct wrong\n",
883 t->to_shortname);
e2e0b3e5 884 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
c906108c
SS
885 }
886
258b763a
AC
887 /* Find the proper stratum to install this target in. */
888 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
c906108c 889 {
258b763a 890 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
c906108c
SS
891 break;
892 }
893
258b763a 894 /* If there's already targets at this stratum, remove them. */
88c231eb 895 /* FIXME: cagney/2003-10-15: I think this should be popping all
258b763a
AC
896 targets to CUR, and not just those at this stratum level. */
897 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
898 {
899 /* There's already something at this stratum level. Close it,
900 and un-hook it from the stack. */
901 struct target_ops *tmp = (*cur);
902 (*cur) = (*cur)->beneath;
903 tmp->beneath = NULL;
f1c07ab0 904 target_close (tmp, 0);
258b763a 905 }
c906108c
SS
906
907 /* We have removed all targets in our stratum, now add the new one. */
258b763a
AC
908 t->beneath = (*cur);
909 (*cur) = t;
c906108c
SS
910
911 update_current_target ();
912
258b763a
AC
913 /* Not on top? */
914 return (t != target_stack);
c906108c
SS
915}
916
2bc416ba 917/* Remove a target_ops vector from the stack, wherever it may be.
c906108c
SS
918 Return how many times it was removed (0 or 1). */
919
920int
fba45db2 921unpush_target (struct target_ops *t)
c906108c 922{
258b763a
AC
923 struct target_ops **cur;
924 struct target_ops *tmp;
c906108c 925
c8d104ad
PA
926 if (t->to_stratum == dummy_stratum)
927 internal_error (__FILE__, __LINE__,
928 "Attempt to unpush the dummy target");
929
c906108c
SS
930 /* Look for the specified target. Note that we assume that a target
931 can only occur once in the target stack. */
932
258b763a
AC
933 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
934 {
935 if ((*cur) == t)
936 break;
937 }
c906108c 938
258b763a 939 if ((*cur) == NULL)
c906108c
SS
940 return 0; /* Didn't find target_ops, quit now */
941
5269965e
AC
942 /* NOTE: cagney/2003-12-06: In '94 the close call was made
943 unconditional by moving it to before the above check that the
944 target was in the target stack (something about "Change the way
945 pushing and popping of targets work to support target overlays
946 and inheritance"). This doesn't make much sense - only open
947 targets should be closed. */
948 target_close (t, 0);
949
c906108c 950 /* Unchain the target */
258b763a
AC
951 tmp = (*cur);
952 (*cur) = (*cur)->beneath;
953 tmp->beneath = NULL;
c906108c
SS
954
955 update_current_target ();
c906108c
SS
956
957 return 1;
958}
959
960void
fba45db2 961pop_target (void)
c906108c 962{
b52323fa 963 target_close (target_stack, 0); /* Let it clean up */
258b763a 964 if (unpush_target (target_stack) == 1)
c906108c
SS
965 return;
966
c5aa993b
JM
967 fprintf_unfiltered (gdb_stderr,
968 "pop_target couldn't find target %s\n",
969 current_target.to_shortname);
e2e0b3e5 970 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
c906108c
SS
971}
972
aa76d38d 973void
87ab71f0 974pop_all_targets_above (enum strata above_stratum, int quitting)
aa76d38d 975{
87ab71f0 976 while ((int) (current_target.to_stratum) > (int) above_stratum)
aa76d38d 977 {
b52323fa 978 target_close (target_stack, quitting);
aa76d38d
PA
979 if (!unpush_target (target_stack))
980 {
981 fprintf_unfiltered (gdb_stderr,
982 "pop_all_targets couldn't find target %s\n",
b52323fa 983 target_stack->to_shortname);
aa76d38d
PA
984 internal_error (__FILE__, __LINE__,
985 _("failed internal consistency check"));
986 break;
987 }
988 }
989}
990
87ab71f0
PA
991void
992pop_all_targets (int quitting)
993{
994 pop_all_targets_above (dummy_stratum, quitting);
995}
996
72f5cf0e 997/* Using the objfile specified in OBJFILE, find the address for the
9e35dae4
DJ
998 current thread's thread-local storage with offset OFFSET. */
999CORE_ADDR
1000target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1001{
1002 volatile CORE_ADDR addr = 0;
117de6a9
PA
1003 struct target_ops *target;
1004
1005 for (target = current_target.beneath;
1006 target != NULL;
1007 target = target->beneath)
1008 {
1009 if (target->to_get_thread_local_address != NULL)
1010 break;
1011 }
9e35dae4 1012
117de6a9 1013 if (target != NULL
1cf3db46 1014 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
9e35dae4
DJ
1015 {
1016 ptid_t ptid = inferior_ptid;
1017 volatile struct gdb_exception ex;
1018
1019 TRY_CATCH (ex, RETURN_MASK_ALL)
1020 {
1021 CORE_ADDR lm_addr;
1022
1023 /* Fetch the load module address for this objfile. */
1cf3db46 1024 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
9e35dae4
DJ
1025 objfile);
1026 /* If it's 0, throw the appropriate exception. */
1027 if (lm_addr == 0)
1028 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1029 _("TLS load module not found"));
1030
117de6a9 1031 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
9e35dae4
DJ
1032 }
1033 /* If an error occurred, print TLS related messages here. Otherwise,
1034 throw the error to some higher catcher. */
1035 if (ex.reason < 0)
1036 {
1037 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1038
1039 switch (ex.error)
1040 {
1041 case TLS_NO_LIBRARY_SUPPORT_ERROR:
1042 error (_("Cannot find thread-local variables in this thread library."));
1043 break;
1044 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1045 if (objfile_is_library)
1046 error (_("Cannot find shared library `%s' in dynamic"
1047 " linker's load module list"), objfile->name);
1048 else
1049 error (_("Cannot find executable file `%s' in dynamic"
1050 " linker's load module list"), objfile->name);
1051 break;
1052 case TLS_NOT_ALLOCATED_YET_ERROR:
1053 if (objfile_is_library)
1054 error (_("The inferior has not yet allocated storage for"
1055 " thread-local variables in\n"
1056 "the shared library `%s'\n"
1057 "for %s"),
1058 objfile->name, target_pid_to_str (ptid));
1059 else
1060 error (_("The inferior has not yet allocated storage for"
1061 " thread-local variables in\n"
1062 "the executable `%s'\n"
1063 "for %s"),
1064 objfile->name, target_pid_to_str (ptid));
1065 break;
1066 case TLS_GENERIC_ERROR:
1067 if (objfile_is_library)
1068 error (_("Cannot find thread-local storage for %s, "
1069 "shared library %s:\n%s"),
1070 target_pid_to_str (ptid),
1071 objfile->name, ex.message);
1072 else
1073 error (_("Cannot find thread-local storage for %s, "
1074 "executable file %s:\n%s"),
1075 target_pid_to_str (ptid),
1076 objfile->name, ex.message);
1077 break;
1078 default:
1079 throw_exception (ex);
1080 break;
1081 }
1082 }
1083 }
1084 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1085 TLS is an ABI-specific thing. But we don't do that yet. */
1086 else
1087 error (_("Cannot find thread-local variables on this target"));
1088
1089 return addr;
1090}
1091
c906108c
SS
1092#undef MIN
1093#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1094
1095/* target_read_string -- read a null terminated string, up to LEN bytes,
1096 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1097 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1098 is responsible for freeing it. Return the number of bytes successfully
1099 read. */
1100
1101int
fba45db2 1102target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
1103{
1104 int tlen, origlen, offset, i;
1b0ba102 1105 gdb_byte buf[4];
c906108c
SS
1106 int errcode = 0;
1107 char *buffer;
1108 int buffer_allocated;
1109 char *bufptr;
1110 unsigned int nbytes_read = 0;
1111
6217bf3e
MS
1112 gdb_assert (string);
1113
c906108c
SS
1114 /* Small for testing. */
1115 buffer_allocated = 4;
1116 buffer = xmalloc (buffer_allocated);
1117 bufptr = buffer;
1118
1119 origlen = len;
1120
1121 while (len > 0)
1122 {
1123 tlen = MIN (len, 4 - (memaddr & 3));
1124 offset = memaddr & 3;
1125
1b0ba102 1126 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
c906108c
SS
1127 if (errcode != 0)
1128 {
1129 /* The transfer request might have crossed the boundary to an
1130 unallocated region of memory. Retry the transfer, requesting
1131 a single byte. */
1132 tlen = 1;
1133 offset = 0;
b8eb5af0 1134 errcode = target_read_memory (memaddr, buf, 1);
c906108c
SS
1135 if (errcode != 0)
1136 goto done;
1137 }
1138
1139 if (bufptr - buffer + tlen > buffer_allocated)
1140 {
1141 unsigned int bytes;
1142 bytes = bufptr - buffer;
1143 buffer_allocated *= 2;
1144 buffer = xrealloc (buffer, buffer_allocated);
1145 bufptr = buffer + bytes;
1146 }
1147
1148 for (i = 0; i < tlen; i++)
1149 {
1150 *bufptr++ = buf[i + offset];
1151 if (buf[i + offset] == '\000')
1152 {
1153 nbytes_read += i + 1;
1154 goto done;
1155 }
1156 }
1157
1158 memaddr += tlen;
1159 len -= tlen;
1160 nbytes_read += tlen;
1161 }
c5aa993b 1162done:
6217bf3e 1163 *string = buffer;
c906108c
SS
1164 if (errnop != NULL)
1165 *errnop = errcode;
c906108c
SS
1166 return nbytes_read;
1167}
1168
07b82ea5
PA
1169struct target_section_table *
1170target_get_section_table (struct target_ops *target)
1171{
1172 struct target_ops *t;
1173
1174 if (targetdebug)
1175 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1176
1177 for (t = target; t != NULL; t = t->beneath)
1178 if (t->to_get_section_table != NULL)
1179 return (*t->to_get_section_table) (t);
1180
1181 return NULL;
1182}
1183
8db32d44 1184/* Find a section containing ADDR. */
07b82ea5 1185
0542c86d 1186struct target_section *
8db32d44
AC
1187target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1188{
07b82ea5 1189 struct target_section_table *table = target_get_section_table (target);
0542c86d 1190 struct target_section *secp;
07b82ea5
PA
1191
1192 if (table == NULL)
1193 return NULL;
1194
1195 for (secp = table->sections; secp < table->sections_end; secp++)
8db32d44
AC
1196 {
1197 if (addr >= secp->addr && addr < secp->endaddr)
1198 return secp;
1199 }
1200 return NULL;
1201}
1202
7f79c47e
DE
1203/* Perform a partial memory transfer.
1204 For docs see target.h, to_xfer_partial. */
cf7a04e8
DJ
1205
1206static LONGEST
4e5d721f
DE
1207memory_xfer_partial (struct target_ops *ops, enum target_object object,
1208 void *readbuf, const void *writebuf, ULONGEST memaddr,
1209 LONGEST len)
0779438d 1210{
cf7a04e8
DJ
1211 LONGEST res;
1212 int reg_len;
1213 struct mem_region *region;
4e5d721f 1214 struct inferior *inf;
cf7a04e8
DJ
1215
1216 /* Zero length requests are ok and require no work. */
1217 if (len == 0)
1218 return 0;
1219
07b82ea5
PA
1220 /* For accesses to unmapped overlay sections, read directly from
1221 files. Must do this first, as MEMADDR may need adjustment. */
1222 if (readbuf != NULL && overlay_debugging)
1223 {
1224 struct obj_section *section = find_pc_overlay (memaddr);
1225 if (pc_in_unmapped_range (memaddr, section))
1226 {
1227 struct target_section_table *table
1228 = target_get_section_table (ops);
1229 const char *section_name = section->the_bfd_section->name;
1230 memaddr = overlay_mapped_address (memaddr, section);
1231 return section_table_xfer_memory_partial (readbuf, writebuf,
1232 memaddr, len,
1233 table->sections,
1234 table->sections_end,
1235 section_name);
1236 }
1237 }
1238
1239 /* Try the executable files, if "trust-readonly-sections" is set. */
cf7a04e8
DJ
1240 if (readbuf != NULL && trust_readonly)
1241 {
0542c86d 1242 struct target_section *secp;
07b82ea5 1243 struct target_section_table *table;
cf7a04e8
DJ
1244
1245 secp = target_section_by_addr (ops, memaddr);
1246 if (secp != NULL
1247 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1248 & SEC_READONLY))
07b82ea5
PA
1249 {
1250 table = target_get_section_table (ops);
1251 return section_table_xfer_memory_partial (readbuf, writebuf,
1252 memaddr, len,
1253 table->sections,
1254 table->sections_end,
1255 NULL);
1256 }
98646950
UW
1257 }
1258
cf7a04e8
DJ
1259 /* Try GDB's internal data cache. */
1260 region = lookup_mem_region (memaddr);
4b5752d0
VP
1261 /* region->hi == 0 means there's no upper bound. */
1262 if (memaddr + len < region->hi || region->hi == 0)
cf7a04e8
DJ
1263 reg_len = len;
1264 else
1265 reg_len = region->hi - memaddr;
1266
1267 switch (region->attrib.mode)
1268 {
1269 case MEM_RO:
1270 if (writebuf != NULL)
1271 return -1;
1272 break;
1273
1274 case MEM_WO:
1275 if (readbuf != NULL)
1276 return -1;
1277 break;
a76d924d
DJ
1278
1279 case MEM_FLASH:
1280 /* We only support writing to flash during "load" for now. */
1281 if (writebuf != NULL)
1282 error (_("Writing to flash memory forbidden in this context"));
1283 break;
4b5752d0
VP
1284
1285 case MEM_NONE:
1286 return -1;
cf7a04e8
DJ
1287 }
1288
6c95b8df
PA
1289 if (!ptid_equal (inferior_ptid, null_ptid))
1290 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1291 else
1292 inf = NULL;
4e5d721f
DE
1293
1294 if (inf != NULL
1295 && (region->attrib.cache
1296 || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
cf7a04e8 1297 {
cf7a04e8 1298 if (readbuf != NULL)
25f122dc 1299 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
cf7a04e8
DJ
1300 reg_len, 0);
1301 else
1302 /* FIXME drow/2006-08-09: If we're going to preserve const
1303 correctness dcache_xfer_memory should take readbuf and
1304 writebuf. */
25f122dc 1305 res = dcache_xfer_memory (ops, target_dcache, memaddr,
cf7a04e8
DJ
1306 (void *) writebuf,
1307 reg_len, 1);
1308 if (res <= 0)
1309 return -1;
1310 else
8defab1a
DJ
1311 {
1312 if (readbuf && !show_memory_breakpoints)
1313 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1314 return res;
1315 }
cf7a04e8
DJ
1316 }
1317
1318 /* If none of those methods found the memory we wanted, fall back
1319 to a target partial transfer. Normally a single call to
1320 to_xfer_partial is enough; if it doesn't recognize an object
1321 it will call the to_xfer_partial of the next target down.
1322 But for memory this won't do. Memory is the only target
1323 object which can be read from more than one valid target.
1324 A core file, for instance, could have some of memory but
1325 delegate other bits to the target below it. So, we must
1326 manually try all targets. */
1327
1328 do
1329 {
1330 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
4b5752d0 1331 readbuf, writebuf, memaddr, reg_len);
cf7a04e8 1332 if (res > 0)
8defab1a 1333 break;
cf7a04e8 1334
5ad3a4ca
DJ
1335 /* We want to continue past core files to executables, but not
1336 past a running target's memory. */
c35b1492 1337 if (ops->to_has_all_memory (ops))
8defab1a 1338 break;
5ad3a4ca 1339
cf7a04e8
DJ
1340 ops = ops->beneath;
1341 }
1342 while (ops != NULL);
1343
8defab1a
DJ
1344 if (readbuf && !show_memory_breakpoints)
1345 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1346
41dcd03f
DE
1347 /* Make sure the cache gets updated no matter what - if we are writing
1348 to the stack. Even if this write is not tagged as such, we still need
1349 to update the cache. */
1350
1351 if (res > 0
1352 && inf != NULL
1353 && writebuf != NULL
1354 && !region->attrib.cache
1355 && stack_cache_enabled_p
1356 && object != TARGET_OBJECT_STACK_MEMORY)
1357 {
7d4f32d3 1358 dcache_update (target_dcache, memaddr, (void *) writebuf, res);
41dcd03f
DE
1359 }
1360
cf7a04e8
DJ
1361 /* If we still haven't got anything, return the last error. We
1362 give up. */
1363 return res;
0779438d
AC
1364}
1365
8defab1a
DJ
1366static void
1367restore_show_memory_breakpoints (void *arg)
1368{
1369 show_memory_breakpoints = (uintptr_t) arg;
1370}
1371
1372struct cleanup *
1373make_show_memory_breakpoints_cleanup (int show)
1374{
1375 int current = show_memory_breakpoints;
1376 show_memory_breakpoints = show;
1377
1378 return make_cleanup (restore_show_memory_breakpoints,
1379 (void *) (uintptr_t) current);
1380}
1381
7f79c47e
DE
1382/* For docs see target.h, to_xfer_partial. */
1383
27394598
AC
1384static LONGEST
1385target_xfer_partial (struct target_ops *ops,
1386 enum target_object object, const char *annex,
1387 void *readbuf, const void *writebuf,
1388 ULONGEST offset, LONGEST len)
1389{
1390 LONGEST retval;
1391
1392 gdb_assert (ops->to_xfer_partial != NULL);
cf7a04e8
DJ
1393
1394 /* If this is a memory transfer, let the memory-specific code
1395 have a look at it instead. Memory transfers are more
1396 complicated. */
4e5d721f
DE
1397 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1398 retval = memory_xfer_partial (ops, object, readbuf,
1399 writebuf, offset, len);
cf7a04e8
DJ
1400 else
1401 {
1402 enum target_object raw_object = object;
1403
1404 /* If this is a raw memory transfer, request the normal
1405 memory object from other layers. */
1406 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1407 raw_object = TARGET_OBJECT_MEMORY;
1408
1409 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1410 writebuf, offset, len);
1411 }
1412
27394598
AC
1413 if (targetdebug)
1414 {
1415 const unsigned char *myaddr = NULL;
1416
1417 fprintf_unfiltered (gdb_stdlog,
53b71562 1418 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
27394598
AC
1419 ops->to_shortname,
1420 (int) object,
1421 (annex ? annex : "(null)"),
53b71562
JB
1422 host_address_to_string (readbuf),
1423 host_address_to_string (writebuf),
0b1553bc
UW
1424 core_addr_to_string_nz (offset),
1425 plongest (len), plongest (retval));
27394598
AC
1426
1427 if (readbuf)
1428 myaddr = readbuf;
1429 if (writebuf)
1430 myaddr = writebuf;
1431 if (retval > 0 && myaddr != NULL)
1432 {
1433 int i;
2bc416ba 1434
27394598
AC
1435 fputs_unfiltered (", bytes =", gdb_stdlog);
1436 for (i = 0; i < retval; i++)
1437 {
53b71562 1438 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
27394598
AC
1439 {
1440 if (targetdebug < 2 && i > 0)
1441 {
1442 fprintf_unfiltered (gdb_stdlog, " ...");
1443 break;
1444 }
1445 fprintf_unfiltered (gdb_stdlog, "\n");
1446 }
2bc416ba 1447
27394598
AC
1448 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1449 }
1450 }
2bc416ba 1451
27394598
AC
1452 fputc_unfiltered ('\n', gdb_stdlog);
1453 }
1454 return retval;
1455}
1456
c906108c
SS
1457/* Read LEN bytes of target memory at address MEMADDR, placing the results in
1458 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1459 if any error occurs.
1460
1461 If an error occurs, no guarantee is made about the contents of the data at
1462 MYADDR. In particular, the caller should not depend upon partial reads
1463 filling the buffer with good data. There is no way for the caller to know
1464 how much good data might have been transfered anyway. Callers that can
cf7a04e8
DJ
1465 deal with partial reads should call target_read (which will retry until
1466 it makes no progress, and then return how much was transferred). */
c906108c
SS
1467
1468int
fc1a4b47 1469target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 1470{
c35b1492
PA
1471 /* Dispatch to the topmost target, not the flattened current_target.
1472 Memory accesses check target->to_has_(all_)memory, and the
1473 flattened target doesn't inherit those. */
1474 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1475 myaddr, memaddr, len) == len)
1476 return 0;
0779438d 1477 else
cf7a04e8 1478 return EIO;
c906108c
SS
1479}
1480
4e5d721f
DE
1481/* Like target_read_memory, but specify explicitly that this is a read from
1482 the target's stack. This may trigger different cache behavior. */
1483
1484int
1485target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1486{
1487 /* Dispatch to the topmost target, not the flattened current_target.
1488 Memory accesses check target->to_has_(all_)memory, and the
1489 flattened target doesn't inherit those. */
1490
1491 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1492 myaddr, memaddr, len) == len)
1493 return 0;
1494 else
1495 return EIO;
1496}
1497
7f79c47e
DE
1498/* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1499 Returns either 0 for success or an errno value if any error occurs.
1500 If an error occurs, no guarantee is made about how much data got written.
1501 Callers that can deal with partial writes should call target_write. */
1502
c906108c 1503int
fc1a4b47 1504target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
c906108c 1505{
c35b1492
PA
1506 /* Dispatch to the topmost target, not the flattened current_target.
1507 Memory accesses check target->to_has_(all_)memory, and the
1508 flattened target doesn't inherit those. */
1509 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1510 myaddr, memaddr, len) == len)
1511 return 0;
0779438d 1512 else
cf7a04e8 1513 return EIO;
c906108c 1514}
c5aa993b 1515
fd79ecee
DJ
1516/* Fetch the target's memory map. */
1517
1518VEC(mem_region_s) *
1519target_memory_map (void)
1520{
1521 VEC(mem_region_s) *result;
1522 struct mem_region *last_one, *this_one;
1523 int ix;
1524 struct target_ops *t;
1525
1526 if (targetdebug)
1527 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1528
1529 for (t = current_target.beneath; t != NULL; t = t->beneath)
1530 if (t->to_memory_map != NULL)
1531 break;
1532
1533 if (t == NULL)
1534 return NULL;
1535
1536 result = t->to_memory_map (t);
1537 if (result == NULL)
1538 return NULL;
1539
1540 qsort (VEC_address (mem_region_s, result),
1541 VEC_length (mem_region_s, result),
1542 sizeof (struct mem_region), mem_region_cmp);
1543
1544 /* Check that regions do not overlap. Simultaneously assign
1545 a numbering for the "mem" commands to use to refer to
1546 each region. */
1547 last_one = NULL;
1548 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1549 {
1550 this_one->number = ix;
1551
1552 if (last_one && last_one->hi > this_one->lo)
1553 {
1554 warning (_("Overlapping regions in memory map: ignoring"));
1555 VEC_free (mem_region_s, result);
1556 return NULL;
1557 }
1558 last_one = this_one;
1559 }
1560
1561 return result;
1562}
1563
a76d924d
DJ
1564void
1565target_flash_erase (ULONGEST address, LONGEST length)
1566{
1567 struct target_ops *t;
1568
1569 for (t = current_target.beneath; t != NULL; t = t->beneath)
1570 if (t->to_flash_erase != NULL)
1571 {
1572 if (targetdebug)
1573 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
5af949e3 1574 hex_string (address), phex (length, 0));
8944021f
DJ
1575 t->to_flash_erase (t, address, length);
1576 return;
a76d924d
DJ
1577 }
1578
1579 tcomplain ();
1580}
1581
1582void
1583target_flash_done (void)
1584{
1585 struct target_ops *t;
1586
1587 for (t = current_target.beneath; t != NULL; t = t->beneath)
1588 if (t->to_flash_done != NULL)
1589 {
1590 if (targetdebug)
1591 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
8944021f
DJ
1592 t->to_flash_done (t);
1593 return;
a76d924d
DJ
1594 }
1595
1596 tcomplain ();
1597}
1598
920d2a44
AC
1599static void
1600show_trust_readonly (struct ui_file *file, int from_tty,
1601 struct cmd_list_element *c, const char *value)
1602{
1603 fprintf_filtered (file, _("\
1604Mode for reading from readonly sections is %s.\n"),
1605 value);
1606}
3a11626d 1607
1e3ff5ad
AC
1608/* More generic transfers. */
1609
0088c768 1610static LONGEST
8aa91c1e 1611default_xfer_partial (struct target_ops *ops, enum target_object object,
2bc416ba 1612 const char *annex, gdb_byte *readbuf,
1b0ba102 1613 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
0088c768
AC
1614{
1615 if (object == TARGET_OBJECT_MEMORY
c8e73a31
AC
1616 && ops->deprecated_xfer_memory != NULL)
1617 /* If available, fall back to the target's
1618 "deprecated_xfer_memory" method. */
0088c768 1619 {
4b8a223f 1620 int xfered = -1;
0088c768 1621 errno = 0;
4b8a223f
AC
1622 if (writebuf != NULL)
1623 {
1624 void *buffer = xmalloc (len);
1625 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1626 memcpy (buffer, writebuf, len);
c8e73a31
AC
1627 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1628 1/*write*/, NULL, ops);
4b8a223f
AC
1629 do_cleanups (cleanup);
1630 }
1631 if (readbuf != NULL)
244e85c8
MS
1632 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1633 0/*read*/, NULL, ops);
0088c768
AC
1634 if (xfered > 0)
1635 return xfered;
1636 else if (xfered == 0 && errno == 0)
c8e73a31
AC
1637 /* "deprecated_xfer_memory" uses 0, cross checked against
1638 ERRNO as one indication of an error. */
0088c768
AC
1639 return 0;
1640 else
1641 return -1;
1642 }
1643 else if (ops->beneath != NULL)
cf7a04e8
DJ
1644 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1645 readbuf, writebuf, offset, len);
1646 else
1647 return -1;
1648}
1649
1650/* The xfer_partial handler for the topmost target. Unlike the default,
1651 it does not need to handle memory specially; it just passes all
1652 requests down the stack. */
1653
1654static LONGEST
1655current_xfer_partial (struct target_ops *ops, enum target_object object,
1656 const char *annex, gdb_byte *readbuf,
1657 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1658{
1659 if (ops->beneath != NULL)
1660 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1661 readbuf, writebuf, offset, len);
0088c768
AC
1662 else
1663 return -1;
1664}
1665
7f79c47e 1666/* Target vector read/write partial wrapper functions. */
0088c768 1667
13547ab6 1668static LONGEST
1e3ff5ad
AC
1669target_read_partial (struct target_ops *ops,
1670 enum target_object object,
1b0ba102 1671 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1672 ULONGEST offset, LONGEST len)
1673{
27394598 1674 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1e3ff5ad
AC
1675}
1676
13547ab6 1677static LONGEST
1e3ff5ad
AC
1678target_write_partial (struct target_ops *ops,
1679 enum target_object object,
1b0ba102 1680 const char *annex, const gdb_byte *buf,
1e3ff5ad
AC
1681 ULONGEST offset, LONGEST len)
1682{
27394598 1683 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1e3ff5ad
AC
1684}
1685
1686/* Wrappers to perform the full transfer. */
7f79c47e
DE
1687
1688/* For docs on target_read see target.h. */
1689
1e3ff5ad
AC
1690LONGEST
1691target_read (struct target_ops *ops,
1692 enum target_object object,
1b0ba102 1693 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1694 ULONGEST offset, LONGEST len)
1695{
1696 LONGEST xfered = 0;
1697 while (xfered < len)
1698 {
0088c768 1699 LONGEST xfer = target_read_partial (ops, object, annex,
fc1a4b47 1700 (gdb_byte *) buf + xfered,
0088c768 1701 offset + xfered, len - xfered);
1e3ff5ad 1702 /* Call an observer, notifying them of the xfer progress? */
13547ab6
DJ
1703 if (xfer == 0)
1704 return xfered;
1705 if (xfer < 0)
0088c768 1706 return -1;
1e3ff5ad
AC
1707 xfered += xfer;
1708 QUIT;
1709 }
1710 return len;
1711}
1712
d5086790
VP
1713LONGEST
1714target_read_until_error (struct target_ops *ops,
1715 enum target_object object,
1716 const char *annex, gdb_byte *buf,
1717 ULONGEST offset, LONGEST len)
1718{
1719 LONGEST xfered = 0;
1720 while (xfered < len)
1721 {
1722 LONGEST xfer = target_read_partial (ops, object, annex,
1723 (gdb_byte *) buf + xfered,
1724 offset + xfered, len - xfered);
1725 /* Call an observer, notifying them of the xfer progress? */
1726 if (xfer == 0)
1727 return xfered;
1728 if (xfer < 0)
1729 {
1730 /* We've got an error. Try to read in smaller blocks. */
1731 ULONGEST start = offset + xfered;
1732 ULONGEST remaining = len - xfered;
1733 ULONGEST half;
1734
1735 /* If an attempt was made to read a random memory address,
1736 it's likely that the very first byte is not accessible.
1737 Try reading the first byte, to avoid doing log N tries
1738 below. */
1739 xfer = target_read_partial (ops, object, annex,
1740 (gdb_byte *) buf + xfered, start, 1);
1741 if (xfer <= 0)
1742 return xfered;
1743 start += 1;
1744 remaining -= 1;
1745 half = remaining/2;
1746
1747 while (half > 0)
1748 {
1749 xfer = target_read_partial (ops, object, annex,
1750 (gdb_byte *) buf + xfered,
1751 start, half);
1752 if (xfer == 0)
1753 return xfered;
1754 if (xfer < 0)
1755 {
1756 remaining = half;
1757 }
1758 else
1759 {
1760 /* We have successfully read the first half. So, the
1761 error must be in the second half. Adjust start and
1762 remaining to point at the second half. */
1763 xfered += xfer;
1764 start += xfer;
1765 remaining -= xfer;
1766 }
1767 half = remaining/2;
1768 }
1769
1770 return xfered;
1771 }
1772 xfered += xfer;
1773 QUIT;
1774 }
1775 return len;
1776}
1777
cf7a04e8
DJ
1778/* An alternative to target_write with progress callbacks. */
1779
1e3ff5ad 1780LONGEST
cf7a04e8
DJ
1781target_write_with_progress (struct target_ops *ops,
1782 enum target_object object,
1783 const char *annex, const gdb_byte *buf,
1784 ULONGEST offset, LONGEST len,
1785 void (*progress) (ULONGEST, void *), void *baton)
1e3ff5ad
AC
1786{
1787 LONGEST xfered = 0;
a76d924d
DJ
1788
1789 /* Give the progress callback a chance to set up. */
1790 if (progress)
1791 (*progress) (0, baton);
1792
1e3ff5ad
AC
1793 while (xfered < len)
1794 {
1795 LONGEST xfer = target_write_partial (ops, object, annex,
fc1a4b47 1796 (gdb_byte *) buf + xfered,
1e3ff5ad 1797 offset + xfered, len - xfered);
cf7a04e8 1798
13547ab6
DJ
1799 if (xfer == 0)
1800 return xfered;
1801 if (xfer < 0)
0088c768 1802 return -1;
cf7a04e8
DJ
1803
1804 if (progress)
1805 (*progress) (xfer, baton);
1806
1e3ff5ad
AC
1807 xfered += xfer;
1808 QUIT;
1809 }
1810 return len;
1811}
1812
7f79c47e
DE
1813/* For docs on target_write see target.h. */
1814
cf7a04e8
DJ
1815LONGEST
1816target_write (struct target_ops *ops,
1817 enum target_object object,
1818 const char *annex, const gdb_byte *buf,
1819 ULONGEST offset, LONGEST len)
1820{
1821 return target_write_with_progress (ops, object, annex, buf, offset, len,
1822 NULL, NULL);
1823}
1824
159f81f3
DJ
1825/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1826 the size of the transferred data. PADDING additional bytes are
1827 available in *BUF_P. This is a helper function for
1828 target_read_alloc; see the declaration of that function for more
1829 information. */
13547ab6 1830
159f81f3
DJ
1831static LONGEST
1832target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1833 const char *annex, gdb_byte **buf_p, int padding)
13547ab6
DJ
1834{
1835 size_t buf_alloc, buf_pos;
1836 gdb_byte *buf;
1837 LONGEST n;
1838
1839 /* This function does not have a length parameter; it reads the
1840 entire OBJECT). Also, it doesn't support objects fetched partly
1841 from one target and partly from another (in a different stratum,
1842 e.g. a core file and an executable). Both reasons make it
1843 unsuitable for reading memory. */
1844 gdb_assert (object != TARGET_OBJECT_MEMORY);
1845
1846 /* Start by reading up to 4K at a time. The target will throttle
1847 this number down if necessary. */
1848 buf_alloc = 4096;
1849 buf = xmalloc (buf_alloc);
1850 buf_pos = 0;
1851 while (1)
1852 {
1853 n = target_read_partial (ops, object, annex, &buf[buf_pos],
159f81f3 1854 buf_pos, buf_alloc - buf_pos - padding);
13547ab6
DJ
1855 if (n < 0)
1856 {
1857 /* An error occurred. */
1858 xfree (buf);
1859 return -1;
1860 }
1861 else if (n == 0)
1862 {
1863 /* Read all there was. */
1864 if (buf_pos == 0)
1865 xfree (buf);
1866 else
1867 *buf_p = buf;
1868 return buf_pos;
1869 }
1870
1871 buf_pos += n;
1872
1873 /* If the buffer is filling up, expand it. */
1874 if (buf_alloc < buf_pos * 2)
1875 {
1876 buf_alloc *= 2;
1877 buf = xrealloc (buf, buf_alloc);
1878 }
1879
1880 QUIT;
1881 }
1882}
1883
159f81f3
DJ
1884/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1885 the size of the transferred data. See the declaration in "target.h"
1886 function for more information about the return value. */
1887
1888LONGEST
1889target_read_alloc (struct target_ops *ops, enum target_object object,
1890 const char *annex, gdb_byte **buf_p)
1891{
1892 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1893}
1894
1895/* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1896 returned as a string, allocated using xmalloc. If an error occurs
1897 or the transfer is unsupported, NULL is returned. Empty objects
1898 are returned as allocated but empty strings. A warning is issued
1899 if the result contains any embedded NUL bytes. */
1900
1901char *
1902target_read_stralloc (struct target_ops *ops, enum target_object object,
1903 const char *annex)
1904{
1905 gdb_byte *buffer;
1906 LONGEST transferred;
1907
1908 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1909
1910 if (transferred < 0)
1911 return NULL;
1912
1913 if (transferred == 0)
1914 return xstrdup ("");
1915
1916 buffer[transferred] = 0;
1917 if (strlen (buffer) < transferred)
1918 warning (_("target object %d, annex %s, "
1919 "contained unexpected null characters"),
1920 (int) object, annex ? annex : "(none)");
1921
1922 return (char *) buffer;
1923}
1924
b6591e8b
AC
1925/* Memory transfer methods. */
1926
1927void
1b0ba102 1928get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
b6591e8b
AC
1929 LONGEST len)
1930{
07b82ea5
PA
1931 /* This method is used to read from an alternate, non-current
1932 target. This read must bypass the overlay support (as symbols
1933 don't match this target), and GDB's internal cache (wrong cache
1934 for this target). */
1935 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
b6591e8b
AC
1936 != len)
1937 memory_error (EIO, addr);
1938}
1939
1940ULONGEST
1941get_target_memory_unsigned (struct target_ops *ops,
e17a4113 1942 CORE_ADDR addr, int len, enum bfd_endian byte_order)
b6591e8b 1943{
f6519ebc 1944 gdb_byte buf[sizeof (ULONGEST)];
b6591e8b
AC
1945
1946 gdb_assert (len <= sizeof (buf));
1947 get_target_memory (ops, addr, buf, len);
e17a4113 1948 return extract_unsigned_integer (buf, len, byte_order);
b6591e8b
AC
1949}
1950
c906108c 1951static void
fba45db2 1952target_info (char *args, int from_tty)
c906108c
SS
1953{
1954 struct target_ops *t;
c906108c 1955 int has_all_mem = 0;
c5aa993b 1956
c906108c 1957 if (symfile_objfile != NULL)
a3f17187 1958 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
c906108c 1959
258b763a 1960 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 1961 {
c35b1492 1962 if (!(*t->to_has_memory) (t))
c906108c
SS
1963 continue;
1964
c5aa993b 1965 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1966 continue;
1967 if (has_all_mem)
a3f17187 1968 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
c5aa993b
JM
1969 printf_unfiltered ("%s:\n", t->to_longname);
1970 (t->to_files_info) (t);
c35b1492 1971 has_all_mem = (*t->to_has_all_memory) (t);
c906108c
SS
1972 }
1973}
1974
fd79ecee
DJ
1975/* This function is called before any new inferior is created, e.g.
1976 by running a program, attaching, or connecting to a target.
1977 It cleans up any state from previous invocations which might
1978 change between runs. This is a subset of what target_preopen
1979 resets (things which might change between targets). */
1980
1981void
1982target_pre_inferior (int from_tty)
1983{
b9db4ced
UW
1984 /* Clear out solib state. Otherwise the solib state of the previous
1985 inferior might have survived and is entirely wrong for the new
1986 target. This has been observed on GNU/Linux using glibc 2.3. How
1987 to reproduce:
1988
1989 bash$ ./foo&
1990 [1] 4711
1991 bash$ ./foo&
1992 [1] 4712
1993 bash$ gdb ./foo
1994 [...]
1995 (gdb) attach 4711
1996 (gdb) detach
1997 (gdb) attach 4712
1998 Cannot access memory at address 0xdeadbeef
1999 */
b9db4ced 2000
50c71eaf
PA
2001 /* In some OSs, the shared library list is the same/global/shared
2002 across inferiors. If code is shared between processes, so are
2003 memory regions and features. */
2004 if (!gdbarch_has_global_solist (target_gdbarch))
2005 {
2006 no_shared_libraries (NULL, from_tty);
2007
2008 invalidate_target_mem_regions ();
424163ea 2009
50c71eaf
PA
2010 target_clear_description ();
2011 }
fd79ecee
DJ
2012}
2013
b8fa0bfa
PA
2014/* Callback for iterate_over_inferiors. Gets rid of the given
2015 inferior. */
2016
2017static int
2018dispose_inferior (struct inferior *inf, void *args)
2019{
2020 struct thread_info *thread;
2021
2022 thread = any_thread_of_process (inf->pid);
2023 if (thread)
2024 {
2025 switch_to_thread (thread->ptid);
2026
2027 /* Core inferiors actually should be detached, not killed. */
2028 if (target_has_execution)
2029 target_kill ();
2030 else
2031 target_detach (NULL, 0);
2032 }
2033
2034 return 0;
2035}
2036
c906108c
SS
2037/* This is to be called by the open routine before it does
2038 anything. */
2039
2040void
fba45db2 2041target_preopen (int from_tty)
c906108c 2042{
c5aa993b 2043 dont_repeat ();
c906108c 2044
b8fa0bfa 2045 if (have_inferiors ())
c5aa993b 2046 {
adf40b2e 2047 if (!from_tty
b8fa0bfa
PA
2048 || !have_live_inferiors ()
2049 || query (_("A program is being debugged already. Kill it? ")))
2050 iterate_over_inferiors (dispose_inferior, NULL);
c906108c 2051 else
8a3fe4f8 2052 error (_("Program not killed."));
c906108c
SS
2053 }
2054
2055 /* Calling target_kill may remove the target from the stack. But if
2056 it doesn't (which seems like a win for UDI), remove it now. */
87ab71f0
PA
2057 /* Leave the exec target, though. The user may be switching from a
2058 live process to a core of the same program. */
2059 pop_all_targets_above (file_stratum, 0);
fd79ecee
DJ
2060
2061 target_pre_inferior (from_tty);
c906108c
SS
2062}
2063
2064/* Detach a target after doing deferred register stores. */
2065
2066void
fba45db2 2067target_detach (char *args, int from_tty)
c906108c 2068{
136d6dae
VP
2069 struct target_ops* t;
2070
2567c7d9 2071 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2072 /* Don't remove global breakpoints here. They're removed on
2073 disconnection from the target. */
2074 ;
2075 else
2076 /* If we're in breakpoints-always-inserted mode, have to remove
2077 them before detaching. */
6c95b8df 2078 remove_breakpoints_pid (PIDGET (inferior_ptid));
74960c60 2079
24291992
PA
2080 prepare_for_detach ();
2081
136d6dae
VP
2082 for (t = current_target.beneath; t != NULL; t = t->beneath)
2083 {
2084 if (t->to_detach != NULL)
2085 {
2086 t->to_detach (t, args, from_tty);
947b8855
PA
2087 if (targetdebug)
2088 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2089 args, from_tty);
136d6dae
VP
2090 return;
2091 }
2092 }
2093
2094 internal_error (__FILE__, __LINE__, "could not find a target to detach");
c906108c
SS
2095}
2096
6ad8ae5c
DJ
2097void
2098target_disconnect (char *args, int from_tty)
2099{
597320e7
DJ
2100 struct target_ops *t;
2101
50c71eaf
PA
2102 /* If we're in breakpoints-always-inserted mode or if breakpoints
2103 are global across processes, we have to remove them before
2104 disconnecting. */
74960c60
VP
2105 remove_breakpoints ();
2106
597320e7
DJ
2107 for (t = current_target.beneath; t != NULL; t = t->beneath)
2108 if (t->to_disconnect != NULL)
2109 {
2110 if (targetdebug)
2111 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2112 args, from_tty);
2113 t->to_disconnect (t, args, from_tty);
2114 return;
2115 }
2116
2117 tcomplain ();
6ad8ae5c
DJ
2118}
2119
117de6a9 2120ptid_t
47608cb1 2121target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
117de6a9
PA
2122{
2123 struct target_ops *t;
2124
2125 for (t = current_target.beneath; t != NULL; t = t->beneath)
2126 {
2127 if (t->to_wait != NULL)
2128 {
47608cb1 2129 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
117de6a9
PA
2130
2131 if (targetdebug)
2132 {
2133 char *status_string;
2134
2135 status_string = target_waitstatus_to_string (status);
2136 fprintf_unfiltered (gdb_stdlog,
2137 "target_wait (%d, status) = %d, %s\n",
2138 PIDGET (ptid), PIDGET (retval),
2139 status_string);
2140 xfree (status_string);
2141 }
2142
2143 return retval;
2144 }
2145 }
2146
2147 noprocess ();
2148}
2149
2150char *
2151target_pid_to_str (ptid_t ptid)
2152{
2153 struct target_ops *t;
2154
2155 for (t = current_target.beneath; t != NULL; t = t->beneath)
2156 {
2157 if (t->to_pid_to_str != NULL)
2158 return (*t->to_pid_to_str) (t, ptid);
2159 }
2160
2161 return normal_pid_to_str (ptid);
2162}
2163
e1ac3328
VP
2164void
2165target_resume (ptid_t ptid, int step, enum target_signal signal)
2166{
28439f5e
PA
2167 struct target_ops *t;
2168
4e5d721f 2169 target_dcache_invalidate ();
28439f5e
PA
2170
2171 for (t = current_target.beneath; t != NULL; t = t->beneath)
2172 {
2173 if (t->to_resume != NULL)
2174 {
2175 t->to_resume (t, ptid, step, signal);
2176 if (targetdebug)
2177 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2178 PIDGET (ptid),
2179 step ? "step" : "continue",
2180 target_signal_to_name (signal));
2181
2182 set_executing (ptid, 1);
2183 set_running (ptid, 1);
edb3359d 2184 clear_inline_frame_state (ptid);
28439f5e
PA
2185 return;
2186 }
2187 }
2188
2189 noprocess ();
e1ac3328 2190}
ee057212
DJ
2191/* Look through the list of possible targets for a target that can
2192 follow forks. */
2193
2194int
2195target_follow_fork (int follow_child)
2196{
2197 struct target_ops *t;
2198
2199 for (t = current_target.beneath; t != NULL; t = t->beneath)
2200 {
2201 if (t->to_follow_fork != NULL)
2202 {
2203 int retval = t->to_follow_fork (t, follow_child);
2204 if (targetdebug)
2205 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2206 follow_child, retval);
2207 return retval;
2208 }
2209 }
2210
2211 /* Some target returned a fork event, but did not know how to follow it. */
2212 internal_error (__FILE__, __LINE__,
2213 "could not find a target to follow fork");
2214}
2215
136d6dae
VP
2216void
2217target_mourn_inferior (void)
2218{
2219 struct target_ops *t;
2220 for (t = current_target.beneath; t != NULL; t = t->beneath)
2221 {
2222 if (t->to_mourn_inferior != NULL)
2223 {
2224 t->to_mourn_inferior (t);
947b8855
PA
2225 if (targetdebug)
2226 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2227
2228 /* We no longer need to keep handles on any of the object files.
2229 Make sure to release them to avoid unnecessarily locking any
2230 of them while we're not actually debugging. */
2231 bfd_cache_close_all ();
2232
136d6dae
VP
2233 return;
2234 }
2235 }
2236
2237 internal_error (__FILE__, __LINE__,
2238 "could not find a target to follow mourn inferiour");
2239}
2240
424163ea
DJ
2241/* Look for a target which can describe architectural features, starting
2242 from TARGET. If we find one, return its description. */
2243
2244const struct target_desc *
2245target_read_description (struct target_ops *target)
2246{
2247 struct target_ops *t;
2248
2249 for (t = target; t != NULL; t = t->beneath)
2250 if (t->to_read_description != NULL)
2251 {
2252 const struct target_desc *tdesc;
2253
2254 tdesc = t->to_read_description (t);
2255 if (tdesc)
2256 return tdesc;
2257 }
2258
2259 return NULL;
2260}
2261
08388c79
DE
2262/* The default implementation of to_search_memory.
2263 This implements a basic search of memory, reading target memory and
2264 performing the search here (as opposed to performing the search in on the
2265 target side with, for example, gdbserver). */
2266
2267int
2268simple_search_memory (struct target_ops *ops,
2269 CORE_ADDR start_addr, ULONGEST search_space_len,
2270 const gdb_byte *pattern, ULONGEST pattern_len,
2271 CORE_ADDR *found_addrp)
2272{
2273 /* NOTE: also defined in find.c testcase. */
2274#define SEARCH_CHUNK_SIZE 16000
2275 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2276 /* Buffer to hold memory contents for searching. */
2277 gdb_byte *search_buf;
2278 unsigned search_buf_size;
2279 struct cleanup *old_cleanups;
2280
2281 search_buf_size = chunk_size + pattern_len - 1;
2282
2283 /* No point in trying to allocate a buffer larger than the search space. */
2284 if (search_space_len < search_buf_size)
2285 search_buf_size = search_space_len;
2286
2287 search_buf = malloc (search_buf_size);
2288 if (search_buf == NULL)
5e1471f5 2289 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2290 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2291
2292 /* Prime the search buffer. */
2293
2294 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2295 search_buf, start_addr, search_buf_size) != search_buf_size)
2296 {
5e1471f5 2297 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2298 hex_string (start_addr));
2299 do_cleanups (old_cleanups);
2300 return -1;
2301 }
2302
2303 /* Perform the search.
2304
2305 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2306 When we've scanned N bytes we copy the trailing bytes to the start and
2307 read in another N bytes. */
2308
2309 while (search_space_len >= pattern_len)
2310 {
2311 gdb_byte *found_ptr;
2312 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2313
2314 found_ptr = memmem (search_buf, nr_search_bytes,
2315 pattern, pattern_len);
2316
2317 if (found_ptr != NULL)
2318 {
2319 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2320 *found_addrp = found_addr;
2321 do_cleanups (old_cleanups);
2322 return 1;
2323 }
2324
2325 /* Not found in this chunk, skip to next chunk. */
2326
2327 /* Don't let search_space_len wrap here, it's unsigned. */
2328 if (search_space_len >= chunk_size)
2329 search_space_len -= chunk_size;
2330 else
2331 search_space_len = 0;
2332
2333 if (search_space_len >= pattern_len)
2334 {
2335 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2336 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2337 int nr_to_read;
2338
2339 /* Copy the trailing part of the previous iteration to the front
2340 of the buffer for the next iteration. */
2341 gdb_assert (keep_len == pattern_len - 1);
2342 memcpy (search_buf, search_buf + chunk_size, keep_len);
2343
2344 nr_to_read = min (search_space_len - keep_len, chunk_size);
2345
2346 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2347 search_buf + keep_len, read_addr,
2348 nr_to_read) != nr_to_read)
2349 {
5e1471f5 2350 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2351 hex_string (read_addr));
2352 do_cleanups (old_cleanups);
2353 return -1;
2354 }
2355
2356 start_addr += chunk_size;
2357 }
2358 }
2359
2360 /* Not found. */
2361
2362 do_cleanups (old_cleanups);
2363 return 0;
2364}
2365
2366/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2367 sequence of bytes in PATTERN with length PATTERN_LEN.
2368
2369 The result is 1 if found, 0 if not found, and -1 if there was an error
2370 requiring halting of the search (e.g. memory read error).
2371 If the pattern is found the address is recorded in FOUND_ADDRP. */
2372
2373int
2374target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2375 const gdb_byte *pattern, ULONGEST pattern_len,
2376 CORE_ADDR *found_addrp)
2377{
2378 struct target_ops *t;
2379 int found;
2380
2381 /* We don't use INHERIT to set current_target.to_search_memory,
2382 so we have to scan the target stack and handle targetdebug
2383 ourselves. */
2384
2385 if (targetdebug)
2386 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2387 hex_string (start_addr));
2388
2389 for (t = current_target.beneath; t != NULL; t = t->beneath)
2390 if (t->to_search_memory != NULL)
2391 break;
2392
2393 if (t != NULL)
2394 {
2395 found = t->to_search_memory (t, start_addr, search_space_len,
2396 pattern, pattern_len, found_addrp);
2397 }
2398 else
2399 {
2400 /* If a special version of to_search_memory isn't available, use the
2401 simple version. */
c35b1492 2402 found = simple_search_memory (current_target.beneath,
08388c79
DE
2403 start_addr, search_space_len,
2404 pattern, pattern_len, found_addrp);
2405 }
2406
2407 if (targetdebug)
2408 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2409
2410 return found;
2411}
2412
8edfe269
DJ
2413/* Look through the currently pushed targets. If none of them will
2414 be able to restart the currently running process, issue an error
2415 message. */
2416
2417void
2418target_require_runnable (void)
2419{
2420 struct target_ops *t;
2421
2422 for (t = target_stack; t != NULL; t = t->beneath)
2423 {
2424 /* If this target knows how to create a new program, then
2425 assume we will still be able to after killing the current
2426 one. Either killing and mourning will not pop T, or else
2427 find_default_run_target will find it again. */
2428 if (t->to_create_inferior != NULL)
2429 return;
2430
2431 /* Do not worry about thread_stratum targets that can not
2432 create inferiors. Assume they will be pushed again if
2433 necessary, and continue to the process_stratum. */
85e747d2
UW
2434 if (t->to_stratum == thread_stratum
2435 || t->to_stratum == arch_stratum)
8edfe269
DJ
2436 continue;
2437
2438 error (_("\
2439The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2440 t->to_shortname);
2441 }
2442
2443 /* This function is only called if the target is running. In that
2444 case there should have been a process_stratum target and it
2445 should either know how to create inferiors, or not... */
2446 internal_error (__FILE__, __LINE__, "No targets found");
2447}
2448
c906108c
SS
2449/* Look through the list of possible targets for a target that can
2450 execute a run or attach command without any other data. This is
2451 used to locate the default process stratum.
2452
5f667f2d
PA
2453 If DO_MESG is not NULL, the result is always valid (error() is
2454 called for errors); else, return NULL on error. */
c906108c
SS
2455
2456static struct target_ops *
fba45db2 2457find_default_run_target (char *do_mesg)
c906108c
SS
2458{
2459 struct target_ops **t;
2460 struct target_ops *runable = NULL;
2461 int count;
2462
2463 count = 0;
2464
2465 for (t = target_structs; t < target_structs + target_struct_size;
2466 ++t)
2467 {
c5aa993b 2468 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
2469 {
2470 runable = *t;
2471 ++count;
2472 }
2473 }
2474
2475 if (count != 1)
5f667f2d
PA
2476 {
2477 if (do_mesg)
2478 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2479 else
2480 return NULL;
2481 }
c906108c
SS
2482
2483 return runable;
2484}
2485
2486void
136d6dae 2487find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
2488{
2489 struct target_ops *t;
2490
c5aa993b 2491 t = find_default_run_target ("attach");
136d6dae 2492 (t->to_attach) (t, args, from_tty);
c906108c
SS
2493 return;
2494}
2495
c906108c 2496void
136d6dae
VP
2497find_default_create_inferior (struct target_ops *ops,
2498 char *exec_file, char *allargs, char **env,
c27cda74 2499 int from_tty)
c906108c
SS
2500{
2501 struct target_ops *t;
2502
c5aa993b 2503 t = find_default_run_target ("run");
136d6dae 2504 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
2505 return;
2506}
2507
2c0b251b 2508static int
b84876c2
PA
2509find_default_can_async_p (void)
2510{
2511 struct target_ops *t;
2512
5f667f2d
PA
2513 /* This may be called before the target is pushed on the stack;
2514 look for the default process stratum. If there's none, gdb isn't
2515 configured with a native debugger, and target remote isn't
2516 connected yet. */
2517 t = find_default_run_target (NULL);
2518 if (t && t->to_can_async_p)
b84876c2
PA
2519 return (t->to_can_async_p) ();
2520 return 0;
2521}
2522
2c0b251b 2523static int
b84876c2
PA
2524find_default_is_async_p (void)
2525{
2526 struct target_ops *t;
2527
5f667f2d
PA
2528 /* This may be called before the target is pushed on the stack;
2529 look for the default process stratum. If there's none, gdb isn't
2530 configured with a native debugger, and target remote isn't
2531 connected yet. */
2532 t = find_default_run_target (NULL);
2533 if (t && t->to_is_async_p)
b84876c2
PA
2534 return (t->to_is_async_p) ();
2535 return 0;
2536}
2537
2c0b251b 2538static int
9908b566
VP
2539find_default_supports_non_stop (void)
2540{
2541 struct target_ops *t;
2542
2543 t = find_default_run_target (NULL);
2544 if (t && t->to_supports_non_stop)
2545 return (t->to_supports_non_stop) ();
2546 return 0;
2547}
2548
2549int
2c0b251b 2550target_supports_non_stop (void)
9908b566
VP
2551{
2552 struct target_ops *t;
2553 for (t = &current_target; t != NULL; t = t->beneath)
2554 if (t->to_supports_non_stop)
2555 return t->to_supports_non_stop ();
2556
2557 return 0;
2558}
2559
2560
07e059b5
VP
2561char *
2562target_get_osdata (const char *type)
2563{
2564 char *document;
2565 struct target_ops *t;
2566
739ef7fb
PA
2567 /* If we're already connected to something that can get us OS
2568 related data, use it. Otherwise, try using the native
2569 target. */
2570 if (current_target.to_stratum >= process_stratum)
6d097e65 2571 t = current_target.beneath;
739ef7fb
PA
2572 else
2573 t = find_default_run_target ("get OS data");
07e059b5
VP
2574
2575 if (!t)
2576 return NULL;
2577
6d097e65 2578 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
2579}
2580
6c95b8df
PA
2581/* Determine the current address space of thread PTID. */
2582
2583struct address_space *
2584target_thread_address_space (ptid_t ptid)
2585{
c0694254 2586 struct address_space *aspace;
6c95b8df 2587 struct inferior *inf;
c0694254
PA
2588 struct target_ops *t;
2589
2590 for (t = current_target.beneath; t != NULL; t = t->beneath)
2591 {
2592 if (t->to_thread_address_space != NULL)
2593 {
2594 aspace = t->to_thread_address_space (t, ptid);
2595 gdb_assert (aspace);
6c95b8df 2596
c0694254
PA
2597 if (targetdebug)
2598 fprintf_unfiltered (gdb_stdlog,
2599 "target_thread_address_space (%s) = %d\n",
2600 target_pid_to_str (ptid),
2601 address_space_num (aspace));
2602 return aspace;
2603 }
2604 }
6c95b8df
PA
2605
2606 /* Fall-back to the "main" address space of the inferior. */
2607 inf = find_inferior_pid (ptid_get_pid (ptid));
2608
2609 if (inf == NULL || inf->aspace == NULL)
2610 internal_error (__FILE__, __LINE__, "\
2611Can't determine the current address space of thread %s\n",
2612 target_pid_to_str (ptid));
2613
2614 return inf->aspace;
2615}
2616
e0d24f8d
WZ
2617static int
2618default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2619{
ffe5a37e 2620 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
ccaa32c7
GS
2621}
2622
5009afc5
AS
2623static int
2624default_watchpoint_addr_within_range (struct target_ops *target,
2625 CORE_ADDR addr,
2626 CORE_ADDR start, int length)
2627{
2628 return addr >= start && addr < start + length;
2629}
2630
c2250ad1
UW
2631static struct gdbarch *
2632default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2633{
2634 return target_gdbarch;
2635}
2636
c906108c 2637static int
fba45db2 2638return_zero (void)
c906108c
SS
2639{
2640 return 0;
2641}
2642
2643static int
fba45db2 2644return_one (void)
c906108c
SS
2645{
2646 return 1;
2647}
2648
ccaa32c7
GS
2649static int
2650return_minus_one (void)
2651{
2652 return -1;
2653}
2654
7a292a7a
SS
2655/* Find a single runnable target in the stack and return it. If for
2656 some reason there is more than one, return NULL. */
2657
2658struct target_ops *
fba45db2 2659find_run_target (void)
7a292a7a
SS
2660{
2661 struct target_ops **t;
2662 struct target_ops *runable = NULL;
2663 int count;
c5aa993b 2664
7a292a7a 2665 count = 0;
c5aa993b 2666
7a292a7a
SS
2667 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2668 {
c5aa993b 2669 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
2670 {
2671 runable = *t;
2672 ++count;
2673 }
2674 }
c5aa993b 2675
7a292a7a
SS
2676 return (count == 1 ? runable : NULL);
2677}
2678
ed9a39eb
JM
2679/* Find a single core_stratum target in the list of targets and return it.
2680 If for some reason there is more than one, return NULL. */
2681
c906108c 2682struct target_ops *
fba45db2 2683find_core_target (void)
c906108c
SS
2684{
2685 struct target_ops **t;
2686 struct target_ops *runable = NULL;
2687 int count;
c5aa993b 2688
c906108c 2689 count = 0;
c5aa993b 2690
c906108c
SS
2691 for (t = target_structs; t < target_structs + target_struct_size;
2692 ++t)
2693 {
2694 if ((*t)->to_stratum == core_stratum)
2695 {
2696 runable = *t;
2697 ++count;
2698 }
2699 }
c5aa993b
JM
2700
2701 return (count == 1 ? runable : NULL);
c906108c 2702}
ed9a39eb
JM
2703
2704/*
2705 * Find the next target down the stack from the specified target.
2706 */
2707
2708struct target_ops *
fba45db2 2709find_target_beneath (struct target_ops *t)
ed9a39eb 2710{
258b763a 2711 return t->beneath;
ed9a39eb
JM
2712}
2713
c906108c
SS
2714\f
2715/* The inferior process has died. Long live the inferior! */
2716
2717void
fba45db2 2718generic_mourn_inferior (void)
c906108c 2719{
7f9f62ba 2720 ptid_t ptid;
c906108c 2721
7f9f62ba 2722 ptid = inferior_ptid;
39f77062 2723 inferior_ptid = null_ptid;
7f9f62ba
PA
2724
2725 if (!ptid_equal (ptid, null_ptid))
2726 {
2727 int pid = ptid_get_pid (ptid);
6c95b8df 2728 exit_inferior (pid);
7f9f62ba
PA
2729 }
2730
c906108c
SS
2731 breakpoint_init_inferior (inf_exited);
2732 registers_changed ();
2733
c906108c
SS
2734 reopen_exec_file ();
2735 reinit_frame_cache ();
2736
9a4105ab
AC
2737 if (deprecated_detach_hook)
2738 deprecated_detach_hook ();
c906108c
SS
2739}
2740\f
8807d78b 2741/* Helper function for child_wait and the derivatives of child_wait.
c906108c
SS
2742 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2743 translation of that in OURSTATUS. */
2744void
fba45db2 2745store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c 2746{
c906108c
SS
2747 if (WIFEXITED (hoststatus))
2748 {
2749 ourstatus->kind = TARGET_WAITKIND_EXITED;
2750 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2751 }
2752 else if (!WIFSTOPPED (hoststatus))
2753 {
2754 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2755 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2756 }
2757 else
2758 {
2759 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2760 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2761 }
2762}
2763\f
fd0a2a6f
MK
2764/* Convert a normal process ID to a string. Returns the string in a
2765 static buffer. */
c906108c
SS
2766
2767char *
39f77062 2768normal_pid_to_str (ptid_t ptid)
c906108c 2769{
fd0a2a6f 2770 static char buf[32];
c906108c 2771
5fff8fc0 2772 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
2773 return buf;
2774}
2775
2c0b251b 2776static char *
117de6a9
PA
2777dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2778{
2779 return normal_pid_to_str (ptid);
2780}
2781
9b4eba8e
HZ
2782/* Error-catcher for target_find_memory_regions. */
2783static int
2784dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
be4d1333 2785{
9b4eba8e 2786 error (_("Command not implemented for this target."));
be4d1333
MS
2787 return 0;
2788}
2789
9b4eba8e
HZ
2790/* Error-catcher for target_make_corefile_notes. */
2791static char *
2792dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
be4d1333 2793{
9b4eba8e 2794 error (_("Command not implemented for this target."));
be4d1333
MS
2795 return NULL;
2796}
2797
6b04bdb7
MS
2798/* Error-catcher for target_get_bookmark. */
2799static gdb_byte *
2800dummy_get_bookmark (char *ignore1, int ignore2)
2801{
2802 tcomplain ();
2803 return NULL;
2804}
2805
2806/* Error-catcher for target_goto_bookmark. */
2807static void
2808dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
2809{
2810 tcomplain ();
2811}
2812
c906108c
SS
2813/* Set up the handful of non-empty slots needed by the dummy target
2814 vector. */
2815
2816static void
fba45db2 2817init_dummy_target (void)
c906108c
SS
2818{
2819 dummy_target.to_shortname = "None";
2820 dummy_target.to_longname = "None";
2821 dummy_target.to_doc = "";
2822 dummy_target.to_attach = find_default_attach;
136d6dae
VP
2823 dummy_target.to_detach =
2824 (void (*)(struct target_ops *, char *, int))target_ignore;
c906108c 2825 dummy_target.to_create_inferior = find_default_create_inferior;
b84876c2
PA
2826 dummy_target.to_can_async_p = find_default_can_async_p;
2827 dummy_target.to_is_async_p = find_default_is_async_p;
9908b566 2828 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
117de6a9 2829 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 2830 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
2831 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2832 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
6b04bdb7
MS
2833 dummy_target.to_get_bookmark = dummy_get_bookmark;
2834 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
0b603eba 2835 dummy_target.to_xfer_partial = default_xfer_partial;
c35b1492
PA
2836 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2837 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2838 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2839 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2840 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
7155de5a
HZ
2841 dummy_target.to_stopped_by_watchpoint = return_zero;
2842 dummy_target.to_stopped_data_address =
2843 (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
c906108c
SS
2844 dummy_target.to_magic = OPS_MAGIC;
2845}
c906108c 2846\f
c906108c 2847static void
fba45db2 2848debug_to_open (char *args, int from_tty)
c906108c
SS
2849{
2850 debug_target.to_open (args, from_tty);
2851
96baa820 2852 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2853}
2854
f1c07ab0
AC
2855void
2856target_close (struct target_ops *targ, int quitting)
2857{
2858 if (targ->to_xclose != NULL)
2859 targ->to_xclose (targ, quitting);
2860 else if (targ->to_close != NULL)
2861 targ->to_close (quitting);
947b8855
PA
2862
2863 if (targetdebug)
2864 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
f1c07ab0
AC
2865}
2866
136d6dae
VP
2867void
2868target_attach (char *args, int from_tty)
2869{
2870 struct target_ops *t;
2871 for (t = current_target.beneath; t != NULL; t = t->beneath)
2872 {
2873 if (t->to_attach != NULL)
2874 {
2875 t->to_attach (t, args, from_tty);
947b8855
PA
2876 if (targetdebug)
2877 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2878 args, from_tty);
136d6dae
VP
2879 return;
2880 }
2881 }
2882
2883 internal_error (__FILE__, __LINE__,
2884 "could not find a target to attach");
2885}
2886
28439f5e
PA
2887int
2888target_thread_alive (ptid_t ptid)
c906108c 2889{
28439f5e
PA
2890 struct target_ops *t;
2891 for (t = current_target.beneath; t != NULL; t = t->beneath)
2892 {
2893 if (t->to_thread_alive != NULL)
2894 {
2895 int retval;
c906108c 2896
28439f5e
PA
2897 retval = t->to_thread_alive (t, ptid);
2898 if (targetdebug)
2899 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2900 PIDGET (ptid), retval);
2901
2902 return retval;
2903 }
2904 }
2905
2906 return 0;
2907}
2908
2909void
2910target_find_new_threads (void)
2911{
2912 struct target_ops *t;
2913 for (t = current_target.beneath; t != NULL; t = t->beneath)
2914 {
2915 if (t->to_find_new_threads != NULL)
2916 {
2917 t->to_find_new_threads (t);
2918 if (targetdebug)
2919 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2920
2921 return;
2922 }
2923 }
c906108c
SS
2924}
2925
c906108c 2926static void
28439f5e 2927debug_to_post_attach (int pid)
c906108c 2928{
28439f5e 2929 debug_target.to_post_attach (pid);
c906108c 2930
28439f5e 2931 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2932}
2933
f00150c9
DE
2934/* Return a pretty printed form of target_waitstatus.
2935 Space for the result is malloc'd, caller must free. */
c906108c 2936
f00150c9
DE
2937char *
2938target_waitstatus_to_string (const struct target_waitstatus *ws)
2939{
2940 const char *kind_str = "status->kind = ";
c906108c 2941
f00150c9 2942 switch (ws->kind)
c906108c
SS
2943 {
2944 case TARGET_WAITKIND_EXITED:
f00150c9
DE
2945 return xstrprintf ("%sexited, status = %d",
2946 kind_str, ws->value.integer);
c906108c 2947 case TARGET_WAITKIND_STOPPED:
f00150c9
DE
2948 return xstrprintf ("%sstopped, signal = %s",
2949 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2950 case TARGET_WAITKIND_SIGNALLED:
f00150c9
DE
2951 return xstrprintf ("%ssignalled, signal = %s",
2952 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2953 case TARGET_WAITKIND_LOADED:
f00150c9 2954 return xstrprintf ("%sloaded", kind_str);
c906108c 2955 case TARGET_WAITKIND_FORKED:
f00150c9 2956 return xstrprintf ("%sforked", kind_str);
c906108c 2957 case TARGET_WAITKIND_VFORKED:
f00150c9 2958 return xstrprintf ("%svforked", kind_str);
c906108c 2959 case TARGET_WAITKIND_EXECD:
f00150c9
DE
2960 return xstrprintf ("%sexecd", kind_str);
2961 case TARGET_WAITKIND_SYSCALL_ENTRY:
a96d9b2e 2962 return xstrprintf ("%sentered syscall", kind_str);
f00150c9 2963 case TARGET_WAITKIND_SYSCALL_RETURN:
a96d9b2e 2964 return xstrprintf ("%sexited syscall", kind_str);
c906108c 2965 case TARGET_WAITKIND_SPURIOUS:
f00150c9
DE
2966 return xstrprintf ("%sspurious", kind_str);
2967 case TARGET_WAITKIND_IGNORE:
2968 return xstrprintf ("%signore", kind_str);
2969 case TARGET_WAITKIND_NO_HISTORY:
2970 return xstrprintf ("%sno-history", kind_str);
c906108c 2971 default:
f00150c9 2972 return xstrprintf ("%sunknown???", kind_str);
c906108c 2973 }
f00150c9
DE
2974}
2975
bf0c5130 2976static void
56be3814
UW
2977debug_print_register (const char * func,
2978 struct regcache *regcache, int regno)
bf0c5130 2979{
f8d29908 2980 struct gdbarch *gdbarch = get_regcache_arch (regcache);
bf0c5130 2981 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 2982 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
2983 && gdbarch_register_name (gdbarch, regno) != NULL
2984 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2985 fprintf_unfiltered (gdb_stdlog, "(%s)",
2986 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
2987 else
2988 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 2989 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 2990 {
e17a4113 2991 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 2992 int i, size = register_size (gdbarch, regno);
d9d9c31f 2993 unsigned char buf[MAX_REGISTER_SIZE];
0ff58721 2994 regcache_raw_collect (regcache, regno, buf);
bf0c5130 2995 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 2996 for (i = 0; i < size; i++)
bf0c5130
AC
2997 {
2998 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2999 }
81c4a259 3000 if (size <= sizeof (LONGEST))
bf0c5130 3001 {
e17a4113 3002 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
0b1553bc
UW
3003 fprintf_unfiltered (gdb_stdlog, " %s %s",
3004 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
3005 }
3006 }
3007 fprintf_unfiltered (gdb_stdlog, "\n");
3008}
3009
28439f5e
PA
3010void
3011target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3012{
28439f5e
PA
3013 struct target_ops *t;
3014 for (t = current_target.beneath; t != NULL; t = t->beneath)
3015 {
3016 if (t->to_fetch_registers != NULL)
3017 {
3018 t->to_fetch_registers (t, regcache, regno);
3019 if (targetdebug)
3020 debug_print_register ("target_fetch_registers", regcache, regno);
3021 return;
3022 }
3023 }
c906108c
SS
3024}
3025
28439f5e
PA
3026void
3027target_store_registers (struct regcache *regcache, int regno)
c906108c 3028{
28439f5e
PA
3029
3030 struct target_ops *t;
3031 for (t = current_target.beneath; t != NULL; t = t->beneath)
3032 {
3033 if (t->to_store_registers != NULL)
3034 {
3035 t->to_store_registers (t, regcache, regno);
3036 if (targetdebug)
3037 {
3038 debug_print_register ("target_store_registers", regcache, regno);
3039 }
3040 return;
3041 }
3042 }
3043
3044 noprocess ();
c906108c
SS
3045}
3046
dc146f7c
VP
3047int
3048target_core_of_thread (ptid_t ptid)
3049{
3050 struct target_ops *t;
3051
3052 for (t = current_target.beneath; t != NULL; t = t->beneath)
3053 {
3054 if (t->to_core_of_thread != NULL)
3055 {
3056 int retval = t->to_core_of_thread (t, ptid);
3057 if (targetdebug)
3058 fprintf_unfiltered (gdb_stdlog, "target_core_of_thread (%d) = %d\n",
3059 PIDGET (ptid), retval);
3060 return retval;
3061 }
3062 }
3063
3064 return -1;
3065}
3066
c906108c 3067static void
316f2060 3068debug_to_prepare_to_store (struct regcache *regcache)
c906108c 3069{
316f2060 3070 debug_target.to_prepare_to_store (regcache);
c906108c 3071
96baa820 3072 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
3073}
3074
3075static int
961cb7b5 3076deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
3077 int write, struct mem_attrib *attrib,
3078 struct target_ops *target)
c906108c
SS
3079{
3080 int retval;
3081
c8e73a31
AC
3082 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3083 attrib, target);
c906108c 3084
96baa820 3085 fprintf_unfiltered (gdb_stdlog,
53b71562 3086 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
5af949e3
UW
3087 paddress (target_gdbarch, memaddr), len,
3088 write ? "write" : "read", retval);
c906108c 3089
c906108c
SS
3090 if (retval > 0)
3091 {
3092 int i;
3093
96baa820 3094 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
3095 for (i = 0; i < retval; i++)
3096 {
53b71562 3097 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
3098 {
3099 if (targetdebug < 2 && i > 0)
3100 {
3101 fprintf_unfiltered (gdb_stdlog, " ...");
3102 break;
3103 }
3104 fprintf_unfiltered (gdb_stdlog, "\n");
3105 }
2bc416ba 3106
96baa820 3107 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
3108 }
3109 }
3110
96baa820 3111 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
3112
3113 return retval;
3114}
3115
3116static void
fba45db2 3117debug_to_files_info (struct target_ops *target)
c906108c
SS
3118{
3119 debug_target.to_files_info (target);
3120
96baa820 3121 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
3122}
3123
3124static int
a6d9a66e
UW
3125debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3126 struct bp_target_info *bp_tgt)
c906108c
SS
3127{
3128 int retval;
3129
a6d9a66e 3130 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
c906108c 3131
96baa820 3132 fprintf_unfiltered (gdb_stdlog,
104c1213 3133 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3134 (unsigned long) bp_tgt->placed_address,
104c1213 3135 (unsigned long) retval);
c906108c
SS
3136 return retval;
3137}
3138
3139static int
a6d9a66e
UW
3140debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3141 struct bp_target_info *bp_tgt)
c906108c
SS
3142{
3143 int retval;
3144
a6d9a66e 3145 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
c906108c 3146
96baa820 3147 fprintf_unfiltered (gdb_stdlog,
104c1213 3148 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3149 (unsigned long) bp_tgt->placed_address,
104c1213 3150 (unsigned long) retval);
c906108c
SS
3151 return retval;
3152}
3153
ccaa32c7
GS
3154static int
3155debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3156{
3157 int retval;
3158
3159 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3160
3161 fprintf_unfiltered (gdb_stdlog,
3162 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3163 (unsigned long) type,
3164 (unsigned long) cnt,
3165 (unsigned long) from_tty,
3166 (unsigned long) retval);
3167 return retval;
3168}
3169
e0d24f8d
WZ
3170static int
3171debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3172{
3173 CORE_ADDR retval;
3174
3175 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3176
3177 fprintf_unfiltered (gdb_stdlog,
d92524f1 3178 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
e0d24f8d
WZ
3179 (unsigned long) addr,
3180 (unsigned long) len,
3181 (unsigned long) retval);
3182 return retval;
3183}
3184
ccaa32c7
GS
3185static int
3186debug_to_stopped_by_watchpoint (void)
3187{
3188 int retval;
3189
3190 retval = debug_target.to_stopped_by_watchpoint ();
3191
3192 fprintf_unfiltered (gdb_stdlog,
d92524f1 3193 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
3194 (unsigned long) retval);
3195 return retval;
3196}
3197
4aa7a7f5
JJ
3198static int
3199debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 3200{
4aa7a7f5 3201 int retval;
ccaa32c7 3202
4aa7a7f5 3203 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
3204
3205 fprintf_unfiltered (gdb_stdlog,
4aa7a7f5
JJ
3206 "target_stopped_data_address ([0x%lx]) = %ld\n",
3207 (unsigned long)*addr,
3208 (unsigned long)retval);
ccaa32c7
GS
3209 return retval;
3210}
3211
5009afc5
AS
3212static int
3213debug_to_watchpoint_addr_within_range (struct target_ops *target,
3214 CORE_ADDR addr,
3215 CORE_ADDR start, int length)
3216{
3217 int retval;
3218
3219 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3220 start, length);
3221
3222 fprintf_filtered (gdb_stdlog,
3223 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3224 (unsigned long) addr, (unsigned long) start, length,
3225 retval);
3226 return retval;
3227}
3228
ccaa32c7 3229static int
a6d9a66e
UW
3230debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3231 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3232{
3233 int retval;
3234
a6d9a66e 3235 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3236
3237 fprintf_unfiltered (gdb_stdlog,
3238 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3239 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3240 (unsigned long) retval);
3241 return retval;
3242}
3243
3244static int
a6d9a66e
UW
3245debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3246 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3247{
3248 int retval;
3249
a6d9a66e 3250 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3251
3252 fprintf_unfiltered (gdb_stdlog,
3253 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3254 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3255 (unsigned long) retval);
3256 return retval;
3257}
3258
3259static int
3260debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3261{
3262 int retval;
3263
3264 retval = debug_target.to_insert_watchpoint (addr, len, type);
3265
3266 fprintf_unfiltered (gdb_stdlog,
3267 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3268 (unsigned long) addr, len, type, (unsigned long) retval);
3269 return retval;
3270}
3271
3272static int
3273debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3274{
3275 int retval;
3276
ecde4882 3277 retval = debug_target.to_remove_watchpoint (addr, len, type);
ccaa32c7
GS
3278
3279 fprintf_unfiltered (gdb_stdlog,
ecde4882 3280 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
ccaa32c7
GS
3281 (unsigned long) addr, len, type, (unsigned long) retval);
3282 return retval;
3283}
3284
c906108c 3285static void
fba45db2 3286debug_to_terminal_init (void)
c906108c
SS
3287{
3288 debug_target.to_terminal_init ();
3289
96baa820 3290 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
3291}
3292
3293static void
fba45db2 3294debug_to_terminal_inferior (void)
c906108c
SS
3295{
3296 debug_target.to_terminal_inferior ();
3297
96baa820 3298 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
3299}
3300
3301static void
fba45db2 3302debug_to_terminal_ours_for_output (void)
c906108c
SS
3303{
3304 debug_target.to_terminal_ours_for_output ();
3305
96baa820 3306 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
3307}
3308
3309static void
fba45db2 3310debug_to_terminal_ours (void)
c906108c
SS
3311{
3312 debug_target.to_terminal_ours ();
3313
96baa820 3314 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
3315}
3316
a790ad35
SC
3317static void
3318debug_to_terminal_save_ours (void)
3319{
3320 debug_target.to_terminal_save_ours ();
3321
3322 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3323}
3324
c906108c 3325static void
fba45db2 3326debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
3327{
3328 debug_target.to_terminal_info (arg, from_tty);
3329
96baa820 3330 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
3331 from_tty);
3332}
3333
c906108c 3334static void
fba45db2 3335debug_to_load (char *args, int from_tty)
c906108c
SS
3336{
3337 debug_target.to_load (args, from_tty);
3338
96baa820 3339 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
3340}
3341
3342static int
fba45db2 3343debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
3344{
3345 int retval;
3346
3347 retval = debug_target.to_lookup_symbol (name, addrp);
3348
96baa820 3349 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
3350
3351 return retval;
3352}
3353
c906108c 3354static void
39f77062 3355debug_to_post_startup_inferior (ptid_t ptid)
c906108c 3356{
39f77062 3357 debug_target.to_post_startup_inferior (ptid);
c906108c 3358
96baa820 3359 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 3360 PIDGET (ptid));
c906108c
SS
3361}
3362
3363static void
fba45db2 3364debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
3365{
3366 debug_target.to_acknowledge_created_inferior (pid);
3367
96baa820 3368 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
3369 pid);
3370}
3371
fa113d1a 3372static void
fba45db2 3373debug_to_insert_fork_catchpoint (int pid)
c906108c 3374{
fa113d1a 3375 debug_target.to_insert_fork_catchpoint (pid);
c906108c 3376
fa113d1a
AC
3377 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3378 pid);
c906108c
SS
3379}
3380
3381static int
fba45db2 3382debug_to_remove_fork_catchpoint (int pid)
c906108c 3383{
c5aa993b 3384 int retval;
c906108c
SS
3385
3386 retval = debug_target.to_remove_fork_catchpoint (pid);
3387
96baa820 3388 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 3389 pid, retval);
c906108c
SS
3390
3391 return retval;
3392}
3393
fa113d1a 3394static void
fba45db2 3395debug_to_insert_vfork_catchpoint (int pid)
c906108c 3396{
fa113d1a 3397 debug_target.to_insert_vfork_catchpoint (pid);
c906108c 3398
fa113d1a
AC
3399 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3400 pid);
c906108c
SS
3401}
3402
3403static int
fba45db2 3404debug_to_remove_vfork_catchpoint (int pid)
c906108c 3405{
c5aa993b 3406 int retval;
c906108c
SS
3407
3408 retval = debug_target.to_remove_vfork_catchpoint (pid);
3409
96baa820 3410 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 3411 pid, retval);
c906108c
SS
3412
3413 return retval;
3414}
3415
fa113d1a 3416static void
fba45db2 3417debug_to_insert_exec_catchpoint (int pid)
c906108c 3418{
fa113d1a 3419 debug_target.to_insert_exec_catchpoint (pid);
c906108c 3420
fa113d1a
AC
3421 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3422 pid);
c906108c
SS
3423}
3424
3425static int
fba45db2 3426debug_to_remove_exec_catchpoint (int pid)
c906108c 3427{
c5aa993b 3428 int retval;
c906108c
SS
3429
3430 retval = debug_target.to_remove_exec_catchpoint (pid);
3431
96baa820 3432 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 3433 pid, retval);
c906108c
SS
3434
3435 return retval;
3436}
3437
c906108c 3438static int
fba45db2 3439debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 3440{
c5aa993b 3441 int has_exited;
c906108c
SS
3442
3443 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3444
96baa820 3445 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 3446 pid, wait_status, *exit_status, has_exited);
c906108c
SS
3447
3448 return has_exited;
3449}
3450
c906108c 3451static int
fba45db2 3452debug_to_can_run (void)
c906108c
SS
3453{
3454 int retval;
3455
3456 retval = debug_target.to_can_run ();
3457
96baa820 3458 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
3459
3460 return retval;
3461}
3462
3463static void
39f77062 3464debug_to_notice_signals (ptid_t ptid)
c906108c 3465{
39f77062 3466 debug_target.to_notice_signals (ptid);
c906108c 3467
39f77062
KB
3468 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3469 PIDGET (ptid));
c906108c
SS
3470}
3471
c2250ad1
UW
3472static struct gdbarch *
3473debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3474{
3475 struct gdbarch *retval;
3476
3477 retval = debug_target.to_thread_architecture (ops, ptid);
3478
67e5d36c
PM
3479 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %s [%s]\n",
3480 target_pid_to_str (ptid), host_address_to_string (retval),
c2250ad1
UW
3481 gdbarch_bfd_arch_info (retval)->printable_name);
3482 return retval;
3483}
3484
c906108c 3485static void
94cc34af 3486debug_to_stop (ptid_t ptid)
c906108c 3487{
94cc34af 3488 debug_target.to_stop (ptid);
c906108c 3489
94cc34af
PA
3490 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3491 target_pid_to_str (ptid));
c906108c
SS
3492}
3493
96baa820
JM
3494static void
3495debug_to_rcmd (char *command,
d9fcf2fb 3496 struct ui_file *outbuf)
96baa820
JM
3497{
3498 debug_target.to_rcmd (command, outbuf);
3499 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3500}
3501
c906108c 3502static char *
fba45db2 3503debug_to_pid_to_exec_file (int pid)
c906108c 3504{
c5aa993b 3505 char *exec_file;
c906108c
SS
3506
3507 exec_file = debug_target.to_pid_to_exec_file (pid);
3508
96baa820 3509 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 3510 pid, exec_file);
c906108c
SS
3511
3512 return exec_file;
3513}
3514
c906108c 3515static void
fba45db2 3516setup_target_debug (void)
c906108c
SS
3517{
3518 memcpy (&debug_target, &current_target, sizeof debug_target);
3519
3520 current_target.to_open = debug_to_open;
c906108c 3521 current_target.to_post_attach = debug_to_post_attach;
c906108c 3522 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 3523 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
3524 current_target.to_files_info = debug_to_files_info;
3525 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3526 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
3527 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3528 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3529 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3530 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3531 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3532 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3533 current_target.to_stopped_data_address = debug_to_stopped_data_address;
5009afc5 3534 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
e0d24f8d 3535 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
c906108c
SS
3536 current_target.to_terminal_init = debug_to_terminal_init;
3537 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3538 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3539 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 3540 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 3541 current_target.to_terminal_info = debug_to_terminal_info;
c906108c
SS
3542 current_target.to_load = debug_to_load;
3543 current_target.to_lookup_symbol = debug_to_lookup_symbol;
c906108c
SS
3544 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3545 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
c906108c
SS
3546 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3547 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3548 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3549 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
3550 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3551 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 3552 current_target.to_has_exited = debug_to_has_exited;
c906108c
SS
3553 current_target.to_can_run = debug_to_can_run;
3554 current_target.to_notice_signals = debug_to_notice_signals;
c906108c 3555 current_target.to_stop = debug_to_stop;
96baa820 3556 current_target.to_rcmd = debug_to_rcmd;
c906108c 3557 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 3558 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 3559}
c906108c 3560\f
c5aa993b
JM
3561
3562static char targ_desc[] =
3563"Names of targets and files being debugged.\n\
c906108c
SS
3564Shows the entire stack of targets currently in use (including the exec-file,\n\
3565core-file, and process, if any), as well as the symbol file name.";
3566
96baa820
JM
3567static void
3568do_monitor_command (char *cmd,
3569 int from_tty)
3570{
2b5fe715
AC
3571 if ((current_target.to_rcmd
3572 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 3573 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
3574 && (debug_target.to_rcmd
3575 == (void (*) (char *, struct ui_file *)) tcomplain)))
8a3fe4f8 3576 error (_("\"monitor\" command not supported by this target."));
96baa820
JM
3577 target_rcmd (cmd, gdb_stdtarg);
3578}
3579
87680a14
JB
3580/* Print the name of each layers of our target stack. */
3581
3582static void
3583maintenance_print_target_stack (char *cmd, int from_tty)
3584{
3585 struct target_ops *t;
3586
3587 printf_filtered (_("The current target stack is:\n"));
3588
3589 for (t = target_stack; t != NULL; t = t->beneath)
3590 {
3591 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3592 }
3593}
3594
c6ebd6cf
VP
3595/* Controls if async mode is permitted. */
3596int target_async_permitted = 0;
3597
3598/* The set command writes to this variable. If the inferior is
3599 executing, linux_nat_async_permitted is *not* updated. */
3600static int target_async_permitted_1 = 0;
3601
3602static void
3603set_maintenance_target_async_permitted (char *args, int from_tty,
3604 struct cmd_list_element *c)
3605{
c35b1492 3606 if (have_live_inferiors ())
c6ebd6cf
VP
3607 {
3608 target_async_permitted_1 = target_async_permitted;
3609 error (_("Cannot change this setting while the inferior is running."));
3610 }
3611
3612 target_async_permitted = target_async_permitted_1;
3613}
3614
3615static void
3616show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3617 struct cmd_list_element *c,
3618 const char *value)
3619{
3620 fprintf_filtered (file, _("\
3621Controlling the inferior in asynchronous mode is %s.\n"), value);
3622}
3623
c906108c 3624void
fba45db2 3625initialize_targets (void)
c906108c
SS
3626{
3627 init_dummy_target ();
3628 push_target (&dummy_target);
3629
3630 add_info ("target", target_info, targ_desc);
3631 add_info ("files", target_info, targ_desc);
3632
85c07804
AC
3633 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3634Set target debugging."), _("\
3635Show target debugging."), _("\
333dabeb
DJ
3636When non-zero, target debugging is enabled. Higher numbers are more\n\
3637verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804
AC
3638command."),
3639 NULL,
920d2a44 3640 show_targetdebug,
85c07804 3641 &setdebuglist, &showdebuglist);
3a11626d 3642
2bc416ba 3643 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
3644 &trust_readonly, _("\
3645Set mode for reading from readonly sections."), _("\
3646Show mode for reading from readonly sections."), _("\
3a11626d
MS
3647When this mode is on, memory reads from readonly sections (such as .text)\n\
3648will be read from the object file instead of from the target. This will\n\
7915a72c 3649result in significant performance improvement for remote targets."),
2c5b56ce 3650 NULL,
920d2a44 3651 show_trust_readonly,
e707bbc2 3652 &setlist, &showlist);
96baa820
JM
3653
3654 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 3655 _("Send a command to the remote monitor (remote targets only)."));
96baa820 3656
87680a14
JB
3657 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3658 _("Print the name of each layer of the internal target stack."),
3659 &maintenanceprintlist);
3660
c6ebd6cf
VP
3661 add_setshow_boolean_cmd ("target-async", no_class,
3662 &target_async_permitted_1, _("\
3663Set whether gdb controls the inferior in asynchronous mode."), _("\
3664Show whether gdb controls the inferior in asynchronous mode."), _("\
3665Tells gdb whether to control the inferior in asynchronous mode."),
3666 set_maintenance_target_async_permitted,
3667 show_maintenance_target_async_permitted,
3668 &setlist,
3669 &showlist);
3670
4e5d721f 3671 add_setshow_boolean_cmd ("stack-cache", class_support,
9cf1b572 3672 &stack_cache_enabled_p_1, _("\
4e5d721f
DE
3673Set cache use for stack access."), _("\
3674Show cache use for stack access."), _("\
3675When on, use the data cache for all stack access, regardless of any\n\
3676configured memory regions. This improves remote performance significantly.\n\
3677By default, caching for stack access is on."),
3678 set_stack_cache_enabled_p,
3679 show_stack_cache_enabled_p,
3680 &setlist, &showlist);
3681
8add0441 3682 target_dcache = dcache_init ();
c906108c 3683}
This page took 1.306802 seconds and 4 git commands to generate.