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