Per-process displaced stepping queue.
[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
136d6dae
VP
2080 for (t = current_target.beneath; t != NULL; t = t->beneath)
2081 {
2082 if (t->to_detach != NULL)
2083 {
2084 t->to_detach (t, args, from_tty);
947b8855
PA
2085 if (targetdebug)
2086 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2087 args, from_tty);
136d6dae
VP
2088 return;
2089 }
2090 }
2091
2092 internal_error (__FILE__, __LINE__, "could not find a target to detach");
c906108c
SS
2093}
2094
6ad8ae5c
DJ
2095void
2096target_disconnect (char *args, int from_tty)
2097{
597320e7
DJ
2098 struct target_ops *t;
2099
50c71eaf
PA
2100 /* If we're in breakpoints-always-inserted mode or if breakpoints
2101 are global across processes, we have to remove them before
2102 disconnecting. */
74960c60
VP
2103 remove_breakpoints ();
2104
597320e7
DJ
2105 for (t = current_target.beneath; t != NULL; t = t->beneath)
2106 if (t->to_disconnect != NULL)
2107 {
2108 if (targetdebug)
2109 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2110 args, from_tty);
2111 t->to_disconnect (t, args, from_tty);
2112 return;
2113 }
2114
2115 tcomplain ();
6ad8ae5c
DJ
2116}
2117
117de6a9 2118ptid_t
47608cb1 2119target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
117de6a9
PA
2120{
2121 struct target_ops *t;
2122
2123 for (t = current_target.beneath; t != NULL; t = t->beneath)
2124 {
2125 if (t->to_wait != NULL)
2126 {
47608cb1 2127 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
117de6a9
PA
2128
2129 if (targetdebug)
2130 {
2131 char *status_string;
2132
2133 status_string = target_waitstatus_to_string (status);
2134 fprintf_unfiltered (gdb_stdlog,
2135 "target_wait (%d, status) = %d, %s\n",
2136 PIDGET (ptid), PIDGET (retval),
2137 status_string);
2138 xfree (status_string);
2139 }
2140
2141 return retval;
2142 }
2143 }
2144
2145 noprocess ();
2146}
2147
2148char *
2149target_pid_to_str (ptid_t ptid)
2150{
2151 struct target_ops *t;
2152
2153 for (t = current_target.beneath; t != NULL; t = t->beneath)
2154 {
2155 if (t->to_pid_to_str != NULL)
2156 return (*t->to_pid_to_str) (t, ptid);
2157 }
2158
2159 return normal_pid_to_str (ptid);
2160}
2161
e1ac3328
VP
2162void
2163target_resume (ptid_t ptid, int step, enum target_signal signal)
2164{
28439f5e
PA
2165 struct target_ops *t;
2166
4e5d721f 2167 target_dcache_invalidate ();
28439f5e
PA
2168
2169 for (t = current_target.beneath; t != NULL; t = t->beneath)
2170 {
2171 if (t->to_resume != NULL)
2172 {
2173 t->to_resume (t, ptid, step, signal);
2174 if (targetdebug)
2175 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2176 PIDGET (ptid),
2177 step ? "step" : "continue",
2178 target_signal_to_name (signal));
2179
2180 set_executing (ptid, 1);
2181 set_running (ptid, 1);
edb3359d 2182 clear_inline_frame_state (ptid);
28439f5e
PA
2183 return;
2184 }
2185 }
2186
2187 noprocess ();
e1ac3328 2188}
ee057212
DJ
2189/* Look through the list of possible targets for a target that can
2190 follow forks. */
2191
2192int
2193target_follow_fork (int follow_child)
2194{
2195 struct target_ops *t;
2196
2197 for (t = current_target.beneath; t != NULL; t = t->beneath)
2198 {
2199 if (t->to_follow_fork != NULL)
2200 {
2201 int retval = t->to_follow_fork (t, follow_child);
2202 if (targetdebug)
2203 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2204 follow_child, retval);
2205 return retval;
2206 }
2207 }
2208
2209 /* Some target returned a fork event, but did not know how to follow it. */
2210 internal_error (__FILE__, __LINE__,
2211 "could not find a target to follow fork");
2212}
2213
136d6dae
VP
2214void
2215target_mourn_inferior (void)
2216{
2217 struct target_ops *t;
2218 for (t = current_target.beneath; t != NULL; t = t->beneath)
2219 {
2220 if (t->to_mourn_inferior != NULL)
2221 {
2222 t->to_mourn_inferior (t);
947b8855
PA
2223 if (targetdebug)
2224 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2225
2226 /* We no longer need to keep handles on any of the object files.
2227 Make sure to release them to avoid unnecessarily locking any
2228 of them while we're not actually debugging. */
2229 bfd_cache_close_all ();
2230
136d6dae
VP
2231 return;
2232 }
2233 }
2234
2235 internal_error (__FILE__, __LINE__,
2236 "could not find a target to follow mourn inferiour");
2237}
2238
424163ea
DJ
2239/* Look for a target which can describe architectural features, starting
2240 from TARGET. If we find one, return its description. */
2241
2242const struct target_desc *
2243target_read_description (struct target_ops *target)
2244{
2245 struct target_ops *t;
2246
2247 for (t = target; t != NULL; t = t->beneath)
2248 if (t->to_read_description != NULL)
2249 {
2250 const struct target_desc *tdesc;
2251
2252 tdesc = t->to_read_description (t);
2253 if (tdesc)
2254 return tdesc;
2255 }
2256
2257 return NULL;
2258}
2259
08388c79
DE
2260/* The default implementation of to_search_memory.
2261 This implements a basic search of memory, reading target memory and
2262 performing the search here (as opposed to performing the search in on the
2263 target side with, for example, gdbserver). */
2264
2265int
2266simple_search_memory (struct target_ops *ops,
2267 CORE_ADDR start_addr, ULONGEST search_space_len,
2268 const gdb_byte *pattern, ULONGEST pattern_len,
2269 CORE_ADDR *found_addrp)
2270{
2271 /* NOTE: also defined in find.c testcase. */
2272#define SEARCH_CHUNK_SIZE 16000
2273 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2274 /* Buffer to hold memory contents for searching. */
2275 gdb_byte *search_buf;
2276 unsigned search_buf_size;
2277 struct cleanup *old_cleanups;
2278
2279 search_buf_size = chunk_size + pattern_len - 1;
2280
2281 /* No point in trying to allocate a buffer larger than the search space. */
2282 if (search_space_len < search_buf_size)
2283 search_buf_size = search_space_len;
2284
2285 search_buf = malloc (search_buf_size);
2286 if (search_buf == NULL)
5e1471f5 2287 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2288 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2289
2290 /* Prime the search buffer. */
2291
2292 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2293 search_buf, start_addr, search_buf_size) != search_buf_size)
2294 {
5e1471f5 2295 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2296 hex_string (start_addr));
2297 do_cleanups (old_cleanups);
2298 return -1;
2299 }
2300
2301 /* Perform the search.
2302
2303 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2304 When we've scanned N bytes we copy the trailing bytes to the start and
2305 read in another N bytes. */
2306
2307 while (search_space_len >= pattern_len)
2308 {
2309 gdb_byte *found_ptr;
2310 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2311
2312 found_ptr = memmem (search_buf, nr_search_bytes,
2313 pattern, pattern_len);
2314
2315 if (found_ptr != NULL)
2316 {
2317 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2318 *found_addrp = found_addr;
2319 do_cleanups (old_cleanups);
2320 return 1;
2321 }
2322
2323 /* Not found in this chunk, skip to next chunk. */
2324
2325 /* Don't let search_space_len wrap here, it's unsigned. */
2326 if (search_space_len >= chunk_size)
2327 search_space_len -= chunk_size;
2328 else
2329 search_space_len = 0;
2330
2331 if (search_space_len >= pattern_len)
2332 {
2333 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2334 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2335 int nr_to_read;
2336
2337 /* Copy the trailing part of the previous iteration to the front
2338 of the buffer for the next iteration. */
2339 gdb_assert (keep_len == pattern_len - 1);
2340 memcpy (search_buf, search_buf + chunk_size, keep_len);
2341
2342 nr_to_read = min (search_space_len - keep_len, chunk_size);
2343
2344 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2345 search_buf + keep_len, read_addr,
2346 nr_to_read) != nr_to_read)
2347 {
5e1471f5 2348 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2349 hex_string (read_addr));
2350 do_cleanups (old_cleanups);
2351 return -1;
2352 }
2353
2354 start_addr += chunk_size;
2355 }
2356 }
2357
2358 /* Not found. */
2359
2360 do_cleanups (old_cleanups);
2361 return 0;
2362}
2363
2364/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2365 sequence of bytes in PATTERN with length PATTERN_LEN.
2366
2367 The result is 1 if found, 0 if not found, and -1 if there was an error
2368 requiring halting of the search (e.g. memory read error).
2369 If the pattern is found the address is recorded in FOUND_ADDRP. */
2370
2371int
2372target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2373 const gdb_byte *pattern, ULONGEST pattern_len,
2374 CORE_ADDR *found_addrp)
2375{
2376 struct target_ops *t;
2377 int found;
2378
2379 /* We don't use INHERIT to set current_target.to_search_memory,
2380 so we have to scan the target stack and handle targetdebug
2381 ourselves. */
2382
2383 if (targetdebug)
2384 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2385 hex_string (start_addr));
2386
2387 for (t = current_target.beneath; t != NULL; t = t->beneath)
2388 if (t->to_search_memory != NULL)
2389 break;
2390
2391 if (t != NULL)
2392 {
2393 found = t->to_search_memory (t, start_addr, search_space_len,
2394 pattern, pattern_len, found_addrp);
2395 }
2396 else
2397 {
2398 /* If a special version of to_search_memory isn't available, use the
2399 simple version. */
c35b1492 2400 found = simple_search_memory (current_target.beneath,
08388c79
DE
2401 start_addr, search_space_len,
2402 pattern, pattern_len, found_addrp);
2403 }
2404
2405 if (targetdebug)
2406 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2407
2408 return found;
2409}
2410
8edfe269
DJ
2411/* Look through the currently pushed targets. If none of them will
2412 be able to restart the currently running process, issue an error
2413 message. */
2414
2415void
2416target_require_runnable (void)
2417{
2418 struct target_ops *t;
2419
2420 for (t = target_stack; t != NULL; t = t->beneath)
2421 {
2422 /* If this target knows how to create a new program, then
2423 assume we will still be able to after killing the current
2424 one. Either killing and mourning will not pop T, or else
2425 find_default_run_target will find it again. */
2426 if (t->to_create_inferior != NULL)
2427 return;
2428
2429 /* Do not worry about thread_stratum targets that can not
2430 create inferiors. Assume they will be pushed again if
2431 necessary, and continue to the process_stratum. */
85e747d2
UW
2432 if (t->to_stratum == thread_stratum
2433 || t->to_stratum == arch_stratum)
8edfe269
DJ
2434 continue;
2435
2436 error (_("\
2437The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2438 t->to_shortname);
2439 }
2440
2441 /* This function is only called if the target is running. In that
2442 case there should have been a process_stratum target and it
2443 should either know how to create inferiors, or not... */
2444 internal_error (__FILE__, __LINE__, "No targets found");
2445}
2446
c906108c
SS
2447/* Look through the list of possible targets for a target that can
2448 execute a run or attach command without any other data. This is
2449 used to locate the default process stratum.
2450
5f667f2d
PA
2451 If DO_MESG is not NULL, the result is always valid (error() is
2452 called for errors); else, return NULL on error. */
c906108c
SS
2453
2454static struct target_ops *
fba45db2 2455find_default_run_target (char *do_mesg)
c906108c
SS
2456{
2457 struct target_ops **t;
2458 struct target_ops *runable = NULL;
2459 int count;
2460
2461 count = 0;
2462
2463 for (t = target_structs; t < target_structs + target_struct_size;
2464 ++t)
2465 {
c5aa993b 2466 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
2467 {
2468 runable = *t;
2469 ++count;
2470 }
2471 }
2472
2473 if (count != 1)
5f667f2d
PA
2474 {
2475 if (do_mesg)
2476 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2477 else
2478 return NULL;
2479 }
c906108c
SS
2480
2481 return runable;
2482}
2483
2484void
136d6dae 2485find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
2486{
2487 struct target_ops *t;
2488
c5aa993b 2489 t = find_default_run_target ("attach");
136d6dae 2490 (t->to_attach) (t, args, from_tty);
c906108c
SS
2491 return;
2492}
2493
c906108c 2494void
136d6dae
VP
2495find_default_create_inferior (struct target_ops *ops,
2496 char *exec_file, char *allargs, char **env,
c27cda74 2497 int from_tty)
c906108c
SS
2498{
2499 struct target_ops *t;
2500
c5aa993b 2501 t = find_default_run_target ("run");
136d6dae 2502 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
2503 return;
2504}
2505
2c0b251b 2506static int
b84876c2
PA
2507find_default_can_async_p (void)
2508{
2509 struct target_ops *t;
2510
5f667f2d
PA
2511 /* This may be called before the target is pushed on the stack;
2512 look for the default process stratum. If there's none, gdb isn't
2513 configured with a native debugger, and target remote isn't
2514 connected yet. */
2515 t = find_default_run_target (NULL);
2516 if (t && t->to_can_async_p)
b84876c2
PA
2517 return (t->to_can_async_p) ();
2518 return 0;
2519}
2520
2c0b251b 2521static int
b84876c2
PA
2522find_default_is_async_p (void)
2523{
2524 struct target_ops *t;
2525
5f667f2d
PA
2526 /* This may be called before the target is pushed on the stack;
2527 look for the default process stratum. If there's none, gdb isn't
2528 configured with a native debugger, and target remote isn't
2529 connected yet. */
2530 t = find_default_run_target (NULL);
2531 if (t && t->to_is_async_p)
b84876c2
PA
2532 return (t->to_is_async_p) ();
2533 return 0;
2534}
2535
2c0b251b 2536static int
9908b566
VP
2537find_default_supports_non_stop (void)
2538{
2539 struct target_ops *t;
2540
2541 t = find_default_run_target (NULL);
2542 if (t && t->to_supports_non_stop)
2543 return (t->to_supports_non_stop) ();
2544 return 0;
2545}
2546
2547int
2c0b251b 2548target_supports_non_stop (void)
9908b566
VP
2549{
2550 struct target_ops *t;
2551 for (t = &current_target; t != NULL; t = t->beneath)
2552 if (t->to_supports_non_stop)
2553 return t->to_supports_non_stop ();
2554
2555 return 0;
2556}
2557
2558
07e059b5
VP
2559char *
2560target_get_osdata (const char *type)
2561{
2562 char *document;
2563 struct target_ops *t;
2564
739ef7fb
PA
2565 /* If we're already connected to something that can get us OS
2566 related data, use it. Otherwise, try using the native
2567 target. */
2568 if (current_target.to_stratum >= process_stratum)
6d097e65 2569 t = current_target.beneath;
739ef7fb
PA
2570 else
2571 t = find_default_run_target ("get OS data");
07e059b5
VP
2572
2573 if (!t)
2574 return NULL;
2575
6d097e65 2576 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
2577}
2578
6c95b8df
PA
2579/* Determine the current address space of thread PTID. */
2580
2581struct address_space *
2582target_thread_address_space (ptid_t ptid)
2583{
c0694254 2584 struct address_space *aspace;
6c95b8df 2585 struct inferior *inf;
c0694254
PA
2586 struct target_ops *t;
2587
2588 for (t = current_target.beneath; t != NULL; t = t->beneath)
2589 {
2590 if (t->to_thread_address_space != NULL)
2591 {
2592 aspace = t->to_thread_address_space (t, ptid);
2593 gdb_assert (aspace);
6c95b8df 2594
c0694254
PA
2595 if (targetdebug)
2596 fprintf_unfiltered (gdb_stdlog,
2597 "target_thread_address_space (%s) = %d\n",
2598 target_pid_to_str (ptid),
2599 address_space_num (aspace));
2600 return aspace;
2601 }
2602 }
6c95b8df
PA
2603
2604 /* Fall-back to the "main" address space of the inferior. */
2605 inf = find_inferior_pid (ptid_get_pid (ptid));
2606
2607 if (inf == NULL || inf->aspace == NULL)
2608 internal_error (__FILE__, __LINE__, "\
2609Can't determine the current address space of thread %s\n",
2610 target_pid_to_str (ptid));
2611
2612 return inf->aspace;
2613}
2614
e0d24f8d
WZ
2615static int
2616default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2617{
ffe5a37e 2618 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
ccaa32c7
GS
2619}
2620
5009afc5
AS
2621static int
2622default_watchpoint_addr_within_range (struct target_ops *target,
2623 CORE_ADDR addr,
2624 CORE_ADDR start, int length)
2625{
2626 return addr >= start && addr < start + length;
2627}
2628
c2250ad1
UW
2629static struct gdbarch *
2630default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2631{
2632 return target_gdbarch;
2633}
2634
c906108c 2635static int
fba45db2 2636return_zero (void)
c906108c
SS
2637{
2638 return 0;
2639}
2640
2641static int
fba45db2 2642return_one (void)
c906108c
SS
2643{
2644 return 1;
2645}
2646
ccaa32c7
GS
2647static int
2648return_minus_one (void)
2649{
2650 return -1;
2651}
2652
7a292a7a
SS
2653/* Find a single runnable target in the stack and return it. If for
2654 some reason there is more than one, return NULL. */
2655
2656struct target_ops *
fba45db2 2657find_run_target (void)
7a292a7a
SS
2658{
2659 struct target_ops **t;
2660 struct target_ops *runable = NULL;
2661 int count;
c5aa993b 2662
7a292a7a 2663 count = 0;
c5aa993b 2664
7a292a7a
SS
2665 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2666 {
c5aa993b 2667 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
2668 {
2669 runable = *t;
2670 ++count;
2671 }
2672 }
c5aa993b 2673
7a292a7a
SS
2674 return (count == 1 ? runable : NULL);
2675}
2676
ed9a39eb
JM
2677/* Find a single core_stratum target in the list of targets and return it.
2678 If for some reason there is more than one, return NULL. */
2679
c906108c 2680struct target_ops *
fba45db2 2681find_core_target (void)
c906108c
SS
2682{
2683 struct target_ops **t;
2684 struct target_ops *runable = NULL;
2685 int count;
c5aa993b 2686
c906108c 2687 count = 0;
c5aa993b 2688
c906108c
SS
2689 for (t = target_structs; t < target_structs + target_struct_size;
2690 ++t)
2691 {
2692 if ((*t)->to_stratum == core_stratum)
2693 {
2694 runable = *t;
2695 ++count;
2696 }
2697 }
c5aa993b
JM
2698
2699 return (count == 1 ? runable : NULL);
c906108c 2700}
ed9a39eb
JM
2701
2702/*
2703 * Find the next target down the stack from the specified target.
2704 */
2705
2706struct target_ops *
fba45db2 2707find_target_beneath (struct target_ops *t)
ed9a39eb 2708{
258b763a 2709 return t->beneath;
ed9a39eb
JM
2710}
2711
c906108c
SS
2712\f
2713/* The inferior process has died. Long live the inferior! */
2714
2715void
fba45db2 2716generic_mourn_inferior (void)
c906108c 2717{
7f9f62ba 2718 ptid_t ptid;
c906108c 2719
7f9f62ba 2720 ptid = inferior_ptid;
39f77062 2721 inferior_ptid = null_ptid;
7f9f62ba
PA
2722
2723 if (!ptid_equal (ptid, null_ptid))
2724 {
2725 int pid = ptid_get_pid (ptid);
6c95b8df 2726 exit_inferior (pid);
7f9f62ba
PA
2727 }
2728
c906108c
SS
2729 breakpoint_init_inferior (inf_exited);
2730 registers_changed ();
2731
c906108c
SS
2732 reopen_exec_file ();
2733 reinit_frame_cache ();
2734
9a4105ab
AC
2735 if (deprecated_detach_hook)
2736 deprecated_detach_hook ();
c906108c
SS
2737}
2738\f
8807d78b 2739/* Helper function for child_wait and the derivatives of child_wait.
c906108c
SS
2740 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2741 translation of that in OURSTATUS. */
2742void
fba45db2 2743store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c 2744{
c906108c
SS
2745 if (WIFEXITED (hoststatus))
2746 {
2747 ourstatus->kind = TARGET_WAITKIND_EXITED;
2748 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2749 }
2750 else if (!WIFSTOPPED (hoststatus))
2751 {
2752 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2753 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2754 }
2755 else
2756 {
2757 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2758 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2759 }
2760}
2761\f
fd0a2a6f
MK
2762/* Convert a normal process ID to a string. Returns the string in a
2763 static buffer. */
c906108c
SS
2764
2765char *
39f77062 2766normal_pid_to_str (ptid_t ptid)
c906108c 2767{
fd0a2a6f 2768 static char buf[32];
c906108c 2769
5fff8fc0 2770 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
2771 return buf;
2772}
2773
2c0b251b 2774static char *
117de6a9
PA
2775dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2776{
2777 return normal_pid_to_str (ptid);
2778}
2779
9b4eba8e
HZ
2780/* Error-catcher for target_find_memory_regions. */
2781static int
2782dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
be4d1333 2783{
9b4eba8e 2784 error (_("Command not implemented for this target."));
be4d1333
MS
2785 return 0;
2786}
2787
9b4eba8e
HZ
2788/* Error-catcher for target_make_corefile_notes. */
2789static char *
2790dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
be4d1333 2791{
9b4eba8e 2792 error (_("Command not implemented for this target."));
be4d1333
MS
2793 return NULL;
2794}
2795
6b04bdb7
MS
2796/* Error-catcher for target_get_bookmark. */
2797static gdb_byte *
2798dummy_get_bookmark (char *ignore1, int ignore2)
2799{
2800 tcomplain ();
2801 return NULL;
2802}
2803
2804/* Error-catcher for target_goto_bookmark. */
2805static void
2806dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
2807{
2808 tcomplain ();
2809}
2810
c906108c
SS
2811/* Set up the handful of non-empty slots needed by the dummy target
2812 vector. */
2813
2814static void
fba45db2 2815init_dummy_target (void)
c906108c
SS
2816{
2817 dummy_target.to_shortname = "None";
2818 dummy_target.to_longname = "None";
2819 dummy_target.to_doc = "";
2820 dummy_target.to_attach = find_default_attach;
136d6dae
VP
2821 dummy_target.to_detach =
2822 (void (*)(struct target_ops *, char *, int))target_ignore;
c906108c 2823 dummy_target.to_create_inferior = find_default_create_inferior;
b84876c2
PA
2824 dummy_target.to_can_async_p = find_default_can_async_p;
2825 dummy_target.to_is_async_p = find_default_is_async_p;
9908b566 2826 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
117de6a9 2827 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 2828 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
2829 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2830 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
6b04bdb7
MS
2831 dummy_target.to_get_bookmark = dummy_get_bookmark;
2832 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
0b603eba 2833 dummy_target.to_xfer_partial = default_xfer_partial;
c35b1492
PA
2834 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2835 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2836 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2837 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2838 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
c906108c
SS
2839 dummy_target.to_magic = OPS_MAGIC;
2840}
c906108c 2841\f
c906108c 2842static void
fba45db2 2843debug_to_open (char *args, int from_tty)
c906108c
SS
2844{
2845 debug_target.to_open (args, from_tty);
2846
96baa820 2847 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2848}
2849
f1c07ab0
AC
2850void
2851target_close (struct target_ops *targ, int quitting)
2852{
2853 if (targ->to_xclose != NULL)
2854 targ->to_xclose (targ, quitting);
2855 else if (targ->to_close != NULL)
2856 targ->to_close (quitting);
947b8855
PA
2857
2858 if (targetdebug)
2859 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
f1c07ab0
AC
2860}
2861
136d6dae
VP
2862void
2863target_attach (char *args, int from_tty)
2864{
2865 struct target_ops *t;
2866 for (t = current_target.beneath; t != NULL; t = t->beneath)
2867 {
2868 if (t->to_attach != NULL)
2869 {
2870 t->to_attach (t, args, from_tty);
947b8855
PA
2871 if (targetdebug)
2872 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2873 args, from_tty);
136d6dae
VP
2874 return;
2875 }
2876 }
2877
2878 internal_error (__FILE__, __LINE__,
2879 "could not find a target to attach");
2880}
2881
28439f5e
PA
2882int
2883target_thread_alive (ptid_t ptid)
c906108c 2884{
28439f5e
PA
2885 struct target_ops *t;
2886 for (t = current_target.beneath; t != NULL; t = t->beneath)
2887 {
2888 if (t->to_thread_alive != NULL)
2889 {
2890 int retval;
c906108c 2891
28439f5e
PA
2892 retval = t->to_thread_alive (t, ptid);
2893 if (targetdebug)
2894 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2895 PIDGET (ptid), retval);
2896
2897 return retval;
2898 }
2899 }
2900
2901 return 0;
2902}
2903
2904void
2905target_find_new_threads (void)
2906{
2907 struct target_ops *t;
2908 for (t = current_target.beneath; t != NULL; t = t->beneath)
2909 {
2910 if (t->to_find_new_threads != NULL)
2911 {
2912 t->to_find_new_threads (t);
2913 if (targetdebug)
2914 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2915
2916 return;
2917 }
2918 }
c906108c
SS
2919}
2920
c906108c 2921static void
28439f5e 2922debug_to_post_attach (int pid)
c906108c 2923{
28439f5e 2924 debug_target.to_post_attach (pid);
c906108c 2925
28439f5e 2926 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2927}
2928
f00150c9
DE
2929/* Return a pretty printed form of target_waitstatus.
2930 Space for the result is malloc'd, caller must free. */
c906108c 2931
f00150c9
DE
2932char *
2933target_waitstatus_to_string (const struct target_waitstatus *ws)
2934{
2935 const char *kind_str = "status->kind = ";
c906108c 2936
f00150c9 2937 switch (ws->kind)
c906108c
SS
2938 {
2939 case TARGET_WAITKIND_EXITED:
f00150c9
DE
2940 return xstrprintf ("%sexited, status = %d",
2941 kind_str, ws->value.integer);
c906108c 2942 case TARGET_WAITKIND_STOPPED:
f00150c9
DE
2943 return xstrprintf ("%sstopped, signal = %s",
2944 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2945 case TARGET_WAITKIND_SIGNALLED:
f00150c9
DE
2946 return xstrprintf ("%ssignalled, signal = %s",
2947 kind_str, target_signal_to_name (ws->value.sig));
c906108c 2948 case TARGET_WAITKIND_LOADED:
f00150c9 2949 return xstrprintf ("%sloaded", kind_str);
c906108c 2950 case TARGET_WAITKIND_FORKED:
f00150c9 2951 return xstrprintf ("%sforked", kind_str);
c906108c 2952 case TARGET_WAITKIND_VFORKED:
f00150c9 2953 return xstrprintf ("%svforked", kind_str);
c906108c 2954 case TARGET_WAITKIND_EXECD:
f00150c9
DE
2955 return xstrprintf ("%sexecd", kind_str);
2956 case TARGET_WAITKIND_SYSCALL_ENTRY:
a96d9b2e 2957 return xstrprintf ("%sentered syscall", kind_str);
f00150c9 2958 case TARGET_WAITKIND_SYSCALL_RETURN:
a96d9b2e 2959 return xstrprintf ("%sexited syscall", kind_str);
c906108c 2960 case TARGET_WAITKIND_SPURIOUS:
f00150c9
DE
2961 return xstrprintf ("%sspurious", kind_str);
2962 case TARGET_WAITKIND_IGNORE:
2963 return xstrprintf ("%signore", kind_str);
2964 case TARGET_WAITKIND_NO_HISTORY:
2965 return xstrprintf ("%sno-history", kind_str);
c906108c 2966 default:
f00150c9 2967 return xstrprintf ("%sunknown???", kind_str);
c906108c 2968 }
f00150c9
DE
2969}
2970
bf0c5130 2971static void
56be3814
UW
2972debug_print_register (const char * func,
2973 struct regcache *regcache, int regno)
bf0c5130 2974{
f8d29908 2975 struct gdbarch *gdbarch = get_regcache_arch (regcache);
bf0c5130 2976 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 2977 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
2978 && gdbarch_register_name (gdbarch, regno) != NULL
2979 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2980 fprintf_unfiltered (gdb_stdlog, "(%s)",
2981 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
2982 else
2983 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 2984 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 2985 {
e17a4113 2986 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 2987 int i, size = register_size (gdbarch, regno);
d9d9c31f 2988 unsigned char buf[MAX_REGISTER_SIZE];
0ff58721 2989 regcache_raw_collect (regcache, regno, buf);
bf0c5130 2990 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 2991 for (i = 0; i < size; i++)
bf0c5130
AC
2992 {
2993 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2994 }
81c4a259 2995 if (size <= sizeof (LONGEST))
bf0c5130 2996 {
e17a4113 2997 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
0b1553bc
UW
2998 fprintf_unfiltered (gdb_stdlog, " %s %s",
2999 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
3000 }
3001 }
3002 fprintf_unfiltered (gdb_stdlog, "\n");
3003}
3004
28439f5e
PA
3005void
3006target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3007{
28439f5e
PA
3008 struct target_ops *t;
3009 for (t = current_target.beneath; t != NULL; t = t->beneath)
3010 {
3011 if (t->to_fetch_registers != NULL)
3012 {
3013 t->to_fetch_registers (t, regcache, regno);
3014 if (targetdebug)
3015 debug_print_register ("target_fetch_registers", regcache, regno);
3016 return;
3017 }
3018 }
c906108c
SS
3019}
3020
28439f5e
PA
3021void
3022target_store_registers (struct regcache *regcache, int regno)
c906108c 3023{
28439f5e
PA
3024
3025 struct target_ops *t;
3026 for (t = current_target.beneath; t != NULL; t = t->beneath)
3027 {
3028 if (t->to_store_registers != NULL)
3029 {
3030 t->to_store_registers (t, regcache, regno);
3031 if (targetdebug)
3032 {
3033 debug_print_register ("target_store_registers", regcache, regno);
3034 }
3035 return;
3036 }
3037 }
3038
3039 noprocess ();
c906108c
SS
3040}
3041
dc146f7c
VP
3042int
3043target_core_of_thread (ptid_t ptid)
3044{
3045 struct target_ops *t;
3046
3047 for (t = current_target.beneath; t != NULL; t = t->beneath)
3048 {
3049 if (t->to_core_of_thread != NULL)
3050 {
3051 int retval = t->to_core_of_thread (t, ptid);
3052 if (targetdebug)
3053 fprintf_unfiltered (gdb_stdlog, "target_core_of_thread (%d) = %d\n",
3054 PIDGET (ptid), retval);
3055 return retval;
3056 }
3057 }
3058
3059 return -1;
3060}
3061
c906108c 3062static void
316f2060 3063debug_to_prepare_to_store (struct regcache *regcache)
c906108c 3064{
316f2060 3065 debug_target.to_prepare_to_store (regcache);
c906108c 3066
96baa820 3067 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
3068}
3069
3070static int
961cb7b5 3071deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
3072 int write, struct mem_attrib *attrib,
3073 struct target_ops *target)
c906108c
SS
3074{
3075 int retval;
3076
c8e73a31
AC
3077 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3078 attrib, target);
c906108c 3079
96baa820 3080 fprintf_unfiltered (gdb_stdlog,
53b71562 3081 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
5af949e3
UW
3082 paddress (target_gdbarch, memaddr), len,
3083 write ? "write" : "read", retval);
c906108c 3084
c906108c
SS
3085 if (retval > 0)
3086 {
3087 int i;
3088
96baa820 3089 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
3090 for (i = 0; i < retval; i++)
3091 {
53b71562 3092 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
3093 {
3094 if (targetdebug < 2 && i > 0)
3095 {
3096 fprintf_unfiltered (gdb_stdlog, " ...");
3097 break;
3098 }
3099 fprintf_unfiltered (gdb_stdlog, "\n");
3100 }
2bc416ba 3101
96baa820 3102 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
3103 }
3104 }
3105
96baa820 3106 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
3107
3108 return retval;
3109}
3110
3111static void
fba45db2 3112debug_to_files_info (struct target_ops *target)
c906108c
SS
3113{
3114 debug_target.to_files_info (target);
3115
96baa820 3116 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
3117}
3118
3119static int
a6d9a66e
UW
3120debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3121 struct bp_target_info *bp_tgt)
c906108c
SS
3122{
3123 int retval;
3124
a6d9a66e 3125 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
c906108c 3126
96baa820 3127 fprintf_unfiltered (gdb_stdlog,
104c1213 3128 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3129 (unsigned long) bp_tgt->placed_address,
104c1213 3130 (unsigned long) retval);
c906108c
SS
3131 return retval;
3132}
3133
3134static int
a6d9a66e
UW
3135debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3136 struct bp_target_info *bp_tgt)
c906108c
SS
3137{
3138 int retval;
3139
a6d9a66e 3140 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
c906108c 3141
96baa820 3142 fprintf_unfiltered (gdb_stdlog,
104c1213 3143 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3144 (unsigned long) bp_tgt->placed_address,
104c1213 3145 (unsigned long) retval);
c906108c
SS
3146 return retval;
3147}
3148
ccaa32c7
GS
3149static int
3150debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3151{
3152 int retval;
3153
3154 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3155
3156 fprintf_unfiltered (gdb_stdlog,
3157 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3158 (unsigned long) type,
3159 (unsigned long) cnt,
3160 (unsigned long) from_tty,
3161 (unsigned long) retval);
3162 return retval;
3163}
3164
e0d24f8d
WZ
3165static int
3166debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3167{
3168 CORE_ADDR retval;
3169
3170 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3171
3172 fprintf_unfiltered (gdb_stdlog,
d92524f1 3173 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
e0d24f8d
WZ
3174 (unsigned long) addr,
3175 (unsigned long) len,
3176 (unsigned long) retval);
3177 return retval;
3178}
3179
ccaa32c7
GS
3180static int
3181debug_to_stopped_by_watchpoint (void)
3182{
3183 int retval;
3184
3185 retval = debug_target.to_stopped_by_watchpoint ();
3186
3187 fprintf_unfiltered (gdb_stdlog,
d92524f1 3188 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
3189 (unsigned long) retval);
3190 return retval;
3191}
3192
4aa7a7f5
JJ
3193static int
3194debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 3195{
4aa7a7f5 3196 int retval;
ccaa32c7 3197
4aa7a7f5 3198 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
3199
3200 fprintf_unfiltered (gdb_stdlog,
4aa7a7f5
JJ
3201 "target_stopped_data_address ([0x%lx]) = %ld\n",
3202 (unsigned long)*addr,
3203 (unsigned long)retval);
ccaa32c7
GS
3204 return retval;
3205}
3206
5009afc5
AS
3207static int
3208debug_to_watchpoint_addr_within_range (struct target_ops *target,
3209 CORE_ADDR addr,
3210 CORE_ADDR start, int length)
3211{
3212 int retval;
3213
3214 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3215 start, length);
3216
3217 fprintf_filtered (gdb_stdlog,
3218 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3219 (unsigned long) addr, (unsigned long) start, length,
3220 retval);
3221 return retval;
3222}
3223
ccaa32c7 3224static int
a6d9a66e
UW
3225debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3226 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3227{
3228 int retval;
3229
a6d9a66e 3230 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3231
3232 fprintf_unfiltered (gdb_stdlog,
3233 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3234 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3235 (unsigned long) retval);
3236 return retval;
3237}
3238
3239static int
a6d9a66e
UW
3240debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3241 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3242{
3243 int retval;
3244
a6d9a66e 3245 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3246
3247 fprintf_unfiltered (gdb_stdlog,
3248 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
8181d85f 3249 (unsigned long) bp_tgt->placed_address,
ccaa32c7
GS
3250 (unsigned long) retval);
3251 return retval;
3252}
3253
3254static int
3255debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3256{
3257 int retval;
3258
3259 retval = debug_target.to_insert_watchpoint (addr, len, type);
3260
3261 fprintf_unfiltered (gdb_stdlog,
3262 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3263 (unsigned long) addr, len, type, (unsigned long) retval);
3264 return retval;
3265}
3266
3267static int
3268debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3269{
3270 int retval;
3271
ecde4882 3272 retval = debug_target.to_remove_watchpoint (addr, len, type);
ccaa32c7
GS
3273
3274 fprintf_unfiltered (gdb_stdlog,
ecde4882 3275 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
ccaa32c7
GS
3276 (unsigned long) addr, len, type, (unsigned long) retval);
3277 return retval;
3278}
3279
c906108c 3280static void
fba45db2 3281debug_to_terminal_init (void)
c906108c
SS
3282{
3283 debug_target.to_terminal_init ();
3284
96baa820 3285 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
3286}
3287
3288static void
fba45db2 3289debug_to_terminal_inferior (void)
c906108c
SS
3290{
3291 debug_target.to_terminal_inferior ();
3292
96baa820 3293 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
3294}
3295
3296static void
fba45db2 3297debug_to_terminal_ours_for_output (void)
c906108c
SS
3298{
3299 debug_target.to_terminal_ours_for_output ();
3300
96baa820 3301 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
3302}
3303
3304static void
fba45db2 3305debug_to_terminal_ours (void)
c906108c
SS
3306{
3307 debug_target.to_terminal_ours ();
3308
96baa820 3309 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
3310}
3311
a790ad35
SC
3312static void
3313debug_to_terminal_save_ours (void)
3314{
3315 debug_target.to_terminal_save_ours ();
3316
3317 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3318}
3319
c906108c 3320static void
fba45db2 3321debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
3322{
3323 debug_target.to_terminal_info (arg, from_tty);
3324
96baa820 3325 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
3326 from_tty);
3327}
3328
c906108c 3329static void
fba45db2 3330debug_to_load (char *args, int from_tty)
c906108c
SS
3331{
3332 debug_target.to_load (args, from_tty);
3333
96baa820 3334 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
3335}
3336
3337static int
fba45db2 3338debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
3339{
3340 int retval;
3341
3342 retval = debug_target.to_lookup_symbol (name, addrp);
3343
96baa820 3344 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
3345
3346 return retval;
3347}
3348
c906108c 3349static void
39f77062 3350debug_to_post_startup_inferior (ptid_t ptid)
c906108c 3351{
39f77062 3352 debug_target.to_post_startup_inferior (ptid);
c906108c 3353
96baa820 3354 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 3355 PIDGET (ptid));
c906108c
SS
3356}
3357
3358static void
fba45db2 3359debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
3360{
3361 debug_target.to_acknowledge_created_inferior (pid);
3362
96baa820 3363 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
3364 pid);
3365}
3366
fa113d1a 3367static void
fba45db2 3368debug_to_insert_fork_catchpoint (int pid)
c906108c 3369{
fa113d1a 3370 debug_target.to_insert_fork_catchpoint (pid);
c906108c 3371
fa113d1a
AC
3372 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3373 pid);
c906108c
SS
3374}
3375
3376static int
fba45db2 3377debug_to_remove_fork_catchpoint (int pid)
c906108c 3378{
c5aa993b 3379 int retval;
c906108c
SS
3380
3381 retval = debug_target.to_remove_fork_catchpoint (pid);
3382
96baa820 3383 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 3384 pid, retval);
c906108c
SS
3385
3386 return retval;
3387}
3388
fa113d1a 3389static void
fba45db2 3390debug_to_insert_vfork_catchpoint (int pid)
c906108c 3391{
fa113d1a 3392 debug_target.to_insert_vfork_catchpoint (pid);
c906108c 3393
fa113d1a
AC
3394 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3395 pid);
c906108c
SS
3396}
3397
3398static int
fba45db2 3399debug_to_remove_vfork_catchpoint (int pid)
c906108c 3400{
c5aa993b 3401 int retval;
c906108c
SS
3402
3403 retval = debug_target.to_remove_vfork_catchpoint (pid);
3404
96baa820 3405 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 3406 pid, retval);
c906108c
SS
3407
3408 return retval;
3409}
3410
fa113d1a 3411static void
fba45db2 3412debug_to_insert_exec_catchpoint (int pid)
c906108c 3413{
fa113d1a 3414 debug_target.to_insert_exec_catchpoint (pid);
c906108c 3415
fa113d1a
AC
3416 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3417 pid);
c906108c
SS
3418}
3419
3420static int
fba45db2 3421debug_to_remove_exec_catchpoint (int pid)
c906108c 3422{
c5aa993b 3423 int retval;
c906108c
SS
3424
3425 retval = debug_target.to_remove_exec_catchpoint (pid);
3426
96baa820 3427 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 3428 pid, retval);
c906108c
SS
3429
3430 return retval;
3431}
3432
c906108c 3433static int
fba45db2 3434debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 3435{
c5aa993b 3436 int has_exited;
c906108c
SS
3437
3438 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3439
96baa820 3440 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 3441 pid, wait_status, *exit_status, has_exited);
c906108c
SS
3442
3443 return has_exited;
3444}
3445
c906108c 3446static int
fba45db2 3447debug_to_can_run (void)
c906108c
SS
3448{
3449 int retval;
3450
3451 retval = debug_target.to_can_run ();
3452
96baa820 3453 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
3454
3455 return retval;
3456}
3457
3458static void
39f77062 3459debug_to_notice_signals (ptid_t ptid)
c906108c 3460{
39f77062 3461 debug_target.to_notice_signals (ptid);
c906108c 3462
39f77062
KB
3463 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3464 PIDGET (ptid));
c906108c
SS
3465}
3466
c2250ad1
UW
3467static struct gdbarch *
3468debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3469{
3470 struct gdbarch *retval;
3471
3472 retval = debug_target.to_thread_architecture (ops, ptid);
3473
67e5d36c
PM
3474 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %s [%s]\n",
3475 target_pid_to_str (ptid), host_address_to_string (retval),
c2250ad1
UW
3476 gdbarch_bfd_arch_info (retval)->printable_name);
3477 return retval;
3478}
3479
c906108c 3480static void
94cc34af 3481debug_to_stop (ptid_t ptid)
c906108c 3482{
94cc34af 3483 debug_target.to_stop (ptid);
c906108c 3484
94cc34af
PA
3485 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3486 target_pid_to_str (ptid));
c906108c
SS
3487}
3488
96baa820
JM
3489static void
3490debug_to_rcmd (char *command,
d9fcf2fb 3491 struct ui_file *outbuf)
96baa820
JM
3492{
3493 debug_target.to_rcmd (command, outbuf);
3494 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3495}
3496
c906108c 3497static char *
fba45db2 3498debug_to_pid_to_exec_file (int pid)
c906108c 3499{
c5aa993b 3500 char *exec_file;
c906108c
SS
3501
3502 exec_file = debug_target.to_pid_to_exec_file (pid);
3503
96baa820 3504 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 3505 pid, exec_file);
c906108c
SS
3506
3507 return exec_file;
3508}
3509
c906108c 3510static void
fba45db2 3511setup_target_debug (void)
c906108c
SS
3512{
3513 memcpy (&debug_target, &current_target, sizeof debug_target);
3514
3515 current_target.to_open = debug_to_open;
c906108c 3516 current_target.to_post_attach = debug_to_post_attach;
c906108c 3517 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 3518 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
3519 current_target.to_files_info = debug_to_files_info;
3520 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3521 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
3522 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3523 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3524 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3525 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3526 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3527 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3528 current_target.to_stopped_data_address = debug_to_stopped_data_address;
5009afc5 3529 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
e0d24f8d 3530 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
c906108c
SS
3531 current_target.to_terminal_init = debug_to_terminal_init;
3532 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3533 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3534 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 3535 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 3536 current_target.to_terminal_info = debug_to_terminal_info;
c906108c
SS
3537 current_target.to_load = debug_to_load;
3538 current_target.to_lookup_symbol = debug_to_lookup_symbol;
c906108c
SS
3539 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3540 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
c906108c
SS
3541 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3542 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3543 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3544 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
3545 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3546 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 3547 current_target.to_has_exited = debug_to_has_exited;
c906108c
SS
3548 current_target.to_can_run = debug_to_can_run;
3549 current_target.to_notice_signals = debug_to_notice_signals;
c906108c 3550 current_target.to_stop = debug_to_stop;
96baa820 3551 current_target.to_rcmd = debug_to_rcmd;
c906108c 3552 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 3553 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 3554}
c906108c 3555\f
c5aa993b
JM
3556
3557static char targ_desc[] =
3558"Names of targets and files being debugged.\n\
c906108c
SS
3559Shows the entire stack of targets currently in use (including the exec-file,\n\
3560core-file, and process, if any), as well as the symbol file name.";
3561
96baa820
JM
3562static void
3563do_monitor_command (char *cmd,
3564 int from_tty)
3565{
2b5fe715
AC
3566 if ((current_target.to_rcmd
3567 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 3568 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
3569 && (debug_target.to_rcmd
3570 == (void (*) (char *, struct ui_file *)) tcomplain)))
8a3fe4f8 3571 error (_("\"monitor\" command not supported by this target."));
96baa820
JM
3572 target_rcmd (cmd, gdb_stdtarg);
3573}
3574
87680a14
JB
3575/* Print the name of each layers of our target stack. */
3576
3577static void
3578maintenance_print_target_stack (char *cmd, int from_tty)
3579{
3580 struct target_ops *t;
3581
3582 printf_filtered (_("The current target stack is:\n"));
3583
3584 for (t = target_stack; t != NULL; t = t->beneath)
3585 {
3586 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3587 }
3588}
3589
c6ebd6cf
VP
3590/* Controls if async mode is permitted. */
3591int target_async_permitted = 0;
3592
3593/* The set command writes to this variable. If the inferior is
3594 executing, linux_nat_async_permitted is *not* updated. */
3595static int target_async_permitted_1 = 0;
3596
3597static void
3598set_maintenance_target_async_permitted (char *args, int from_tty,
3599 struct cmd_list_element *c)
3600{
c35b1492 3601 if (have_live_inferiors ())
c6ebd6cf
VP
3602 {
3603 target_async_permitted_1 = target_async_permitted;
3604 error (_("Cannot change this setting while the inferior is running."));
3605 }
3606
3607 target_async_permitted = target_async_permitted_1;
3608}
3609
3610static void
3611show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3612 struct cmd_list_element *c,
3613 const char *value)
3614{
3615 fprintf_filtered (file, _("\
3616Controlling the inferior in asynchronous mode is %s.\n"), value);
3617}
3618
c906108c 3619void
fba45db2 3620initialize_targets (void)
c906108c
SS
3621{
3622 init_dummy_target ();
3623 push_target (&dummy_target);
3624
3625 add_info ("target", target_info, targ_desc);
3626 add_info ("files", target_info, targ_desc);
3627
85c07804
AC
3628 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3629Set target debugging."), _("\
3630Show target debugging."), _("\
333dabeb
DJ
3631When non-zero, target debugging is enabled. Higher numbers are more\n\
3632verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804
AC
3633command."),
3634 NULL,
920d2a44 3635 show_targetdebug,
85c07804 3636 &setdebuglist, &showdebuglist);
3a11626d 3637
2bc416ba 3638 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
3639 &trust_readonly, _("\
3640Set mode for reading from readonly sections."), _("\
3641Show mode for reading from readonly sections."), _("\
3a11626d
MS
3642When this mode is on, memory reads from readonly sections (such as .text)\n\
3643will be read from the object file instead of from the target. This will\n\
7915a72c 3644result in significant performance improvement for remote targets."),
2c5b56ce 3645 NULL,
920d2a44 3646 show_trust_readonly,
e707bbc2 3647 &setlist, &showlist);
96baa820
JM
3648
3649 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 3650 _("Send a command to the remote monitor (remote targets only)."));
96baa820 3651
87680a14
JB
3652 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3653 _("Print the name of each layer of the internal target stack."),
3654 &maintenanceprintlist);
3655
c6ebd6cf
VP
3656 add_setshow_boolean_cmd ("target-async", no_class,
3657 &target_async_permitted_1, _("\
3658Set whether gdb controls the inferior in asynchronous mode."), _("\
3659Show whether gdb controls the inferior in asynchronous mode."), _("\
3660Tells gdb whether to control the inferior in asynchronous mode."),
3661 set_maintenance_target_async_permitted,
3662 show_maintenance_target_async_permitted,
3663 &setlist,
3664 &showlist);
3665
4e5d721f 3666 add_setshow_boolean_cmd ("stack-cache", class_support,
9cf1b572 3667 &stack_cache_enabled_p_1, _("\
4e5d721f
DE
3668Set cache use for stack access."), _("\
3669Show cache use for stack access."), _("\
3670When on, use the data cache for all stack access, regardless of any\n\
3671configured memory regions. This improves remote performance significantly.\n\
3672By default, caching for stack access is on."),
3673 set_stack_cache_enabled_p,
3674 show_stack_cache_enabled_p,
3675 &setlist, &showlist);
3676
8add0441 3677 target_dcache = dcache_init ();
c906108c 3678}
This page took 1.176659 seconds and 4 git commands to generate.