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