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