]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target.c
(mav_reg_required_here): Allow REG_TYPE_CN as alternative when REG_TYPE_MVF,
[thirdparty/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3
AC
2
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
c6f0559b 4 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
7998dfc3 5
c906108c
SS
6 Contributed by Cygnus Support.
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b
JM
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25#include "defs.h"
26#include <errno.h>
c906108c
SS
27#include "gdb_string.h"
28#include "target.h"
29#include "gdbcmd.h"
30#include "symtab.h"
31#include "inferior.h"
32#include "bfd.h"
33#include "symfile.h"
34#include "objfiles.h"
03f2053f 35#include "gdb_wait.h"
4930751a 36#include "dcache.h"
c906108c 37#include <signal.h>
4e052eda 38#include "regcache.h"
0088c768 39#include "gdb_assert.h"
b6591e8b 40#include "gdbcore.h"
c906108c 41
a14ed312 42static void target_info (char *, int);
c906108c 43
a14ed312 44static void maybe_kill_then_attach (char *, int);
c906108c 45
a14ed312 46static void kill_or_be_killed (int);
c906108c 47
a14ed312 48static void default_terminal_info (char *, int);
c906108c 49
ccaa32c7
GS
50static int default_region_size_ok_for_hw_watchpoint (int);
51
a14ed312 52static int nosymbol (char *, CORE_ADDR *);
c906108c 53
a14ed312 54static void tcomplain (void);
c906108c 55
a14ed312 56static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 57
a14ed312 58static int return_zero (void);
c906108c 59
a14ed312 60static int return_one (void);
c906108c 61
ccaa32c7
GS
62static int return_minus_one (void);
63
a14ed312 64void target_ignore (void);
c906108c 65
a14ed312 66static void target_command (char *, int);
c906108c 67
a14ed312 68static struct target_ops *find_default_run_target (char *);
c906108c 69
a14ed312 70static void nosupport_runtime (void);
392a587b 71
4b8a223f 72static LONGEST default_xfer_partial (struct target_ops *ops,
0088c768 73 enum target_object object,
8aa91c1e
AC
74 const char *annex, void *readbuf,
75 const void *writebuf,
76 ULONGEST offset, LONGEST len);
0088c768 77
917317f4
JM
78/* Transfer LEN bytes between target address MEMADDR and GDB address
79 MYADDR. Returns 0 for success, errno code for failure (which
80 includes partial transfers -- if you want a more useful response to
81 partial transfers, try either target_read_memory_partial or
82 target_write_memory_partial). */
c906108c 83
570b8f7c
AC
84static int target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
85 int write);
c906108c 86
a14ed312 87static void init_dummy_target (void);
c906108c 88
a14ed312 89static void debug_to_open (char *, int);
c906108c 90
a14ed312 91static void debug_to_close (int);
c906108c 92
a14ed312 93static void debug_to_attach (char *, int);
c906108c 94
a14ed312 95static void debug_to_detach (char *, int);
c906108c 96
6ad8ae5c
DJ
97static void debug_to_disconnect (char *, int);
98
39f77062 99static void debug_to_resume (ptid_t, int, enum target_signal);
c906108c 100
39f77062 101static ptid_t debug_to_wait (ptid_t, struct target_waitstatus *);
c906108c 102
a14ed312 103static void debug_to_fetch_registers (int);
c906108c 104
a14ed312 105static void debug_to_store_registers (int);
c906108c 106
a14ed312 107static void debug_to_prepare_to_store (void);
c906108c 108
5ae5f592
AC
109static int debug_to_xfer_memory (CORE_ADDR, char *, int, int,
110 struct mem_attrib *, struct target_ops *);
c906108c 111
a14ed312 112static void debug_to_files_info (struct target_ops *);
c906108c 113
a14ed312 114static int debug_to_insert_breakpoint (CORE_ADDR, char *);
c906108c 115
a14ed312 116static int debug_to_remove_breakpoint (CORE_ADDR, char *);
c906108c 117
ccaa32c7
GS
118static int debug_to_can_use_hw_breakpoint (int, int, int);
119
120static int debug_to_insert_hw_breakpoint (CORE_ADDR, char *);
121
122static int debug_to_remove_hw_breakpoint (CORE_ADDR, char *);
123
124static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
125
126static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
127
128static int debug_to_stopped_by_watchpoint (void);
129
130static CORE_ADDR debug_to_stopped_data_address (void);
131
132static int debug_to_region_size_ok_for_hw_watchpoint (int);
133
a14ed312 134static void debug_to_terminal_init (void);
c906108c 135
a14ed312 136static void debug_to_terminal_inferior (void);
c906108c 137
a14ed312 138static void debug_to_terminal_ours_for_output (void);
c906108c 139
a790ad35
SC
140static void debug_to_terminal_save_ours (void);
141
a14ed312 142static void debug_to_terminal_ours (void);
c906108c 143
a14ed312 144static void debug_to_terminal_info (char *, int);
c906108c 145
a14ed312 146static void debug_to_kill (void);
c906108c 147
a14ed312 148static void debug_to_load (char *, int);
c906108c 149
a14ed312 150static int debug_to_lookup_symbol (char *, CORE_ADDR *);
c906108c 151
a14ed312 152static void debug_to_mourn_inferior (void);
c906108c 153
a14ed312 154static int debug_to_can_run (void);
c906108c 155
39f77062 156static void debug_to_notice_signals (ptid_t);
c906108c 157
39f77062 158static int debug_to_thread_alive (ptid_t);
c906108c 159
a14ed312 160static void debug_to_stop (void);
c906108c 161
5ac10fd1
AC
162/* NOTE: cagney/2004-09-29: Many targets reference this variable in
163 wierd and mysterious ways. Putting the variable here lets those
164 wierd and mysterious ways keep building while they are being
165 converted to the inferior inheritance structure. */
166struct target_ops child_ops;
167
c906108c
SS
168/* Pointer to array of target architecture structures; the size of the
169 array; the current index into the array; the allocated size of the
170 array. */
171struct target_ops **target_structs;
172unsigned target_struct_size;
173unsigned target_struct_index;
174unsigned target_struct_allocsize;
175#define DEFAULT_ALLOCSIZE 10
176
177/* The initial current target, so that there is always a semi-valid
178 current target. */
179
180static struct target_ops dummy_target;
181
182/* Top of target stack. */
183
258b763a 184static struct target_ops *target_stack;
c906108c
SS
185
186/* The target structure we are currently using to talk to a process
187 or file or whatever "inferior" we have. */
188
189struct target_ops current_target;
190
191/* Command list for target. */
192
193static struct cmd_list_element *targetlist = NULL;
194
195/* Nonzero if we are debugging an attached outside process
196 rather than an inferior. */
197
198int attach_flag;
199
c906108c
SS
200/* Non-zero if we want to see trace of target level stuff. */
201
202static int targetdebug = 0;
203
a14ed312 204static void setup_target_debug (void);
c906108c 205
4930751a
C
206DCACHE *target_dcache;
207
c906108c
SS
208/* The user just typed 'target' without the name of a target. */
209
c906108c 210static void
fba45db2 211target_command (char *arg, int from_tty)
c906108c
SS
212{
213 fputs_filtered ("Argument required (target name). Try `help target'\n",
214 gdb_stdout);
215}
216
217/* Add a possible target architecture to the list. */
218
219void
fba45db2 220add_target (struct target_ops *t)
c906108c 221{
0088c768 222 /* Provide default values for all "must have" methods. */
0b603eba
AC
223 if (t->to_xfer_partial == NULL)
224 t->to_xfer_partial = default_xfer_partial;
0088c768 225
c906108c
SS
226 if (!target_structs)
227 {
228 target_struct_allocsize = DEFAULT_ALLOCSIZE;
229 target_structs = (struct target_ops **) xmalloc
230 (target_struct_allocsize * sizeof (*target_structs));
231 }
232 if (target_struct_size >= target_struct_allocsize)
233 {
234 target_struct_allocsize *= 2;
235 target_structs = (struct target_ops **)
c5aa993b
JM
236 xrealloc ((char *) target_structs,
237 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
238 }
239 target_structs[target_struct_size++] = t;
c906108c
SS
240
241 if (targetlist == NULL)
242 add_prefix_cmd ("target", class_run, target_command,
243 "Connect to a target machine or process.\n\
244The first argument is the type or protocol of the target machine.\n\
245Remaining arguments are interpreted by the target protocol. For more\n\
246information on the arguments for a particular protocol, type\n\
247`help target ' followed by the protocol name.",
248 &targetlist, "target ", 0, &cmdlist);
249 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
250}
251
252/* Stub functions */
253
254void
fba45db2 255target_ignore (void)
c906108c
SS
256{
257}
258
11cf8741
JM
259void
260target_load (char *arg, int from_tty)
261{
4930751a 262 dcache_invalidate (target_dcache);
11cf8741
JM
263 (*current_target.to_load) (arg, from_tty);
264}
265
c906108c 266static int
fba45db2
KB
267nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
268 struct target_ops *t)
c906108c 269{
c5aa993b
JM
270 errno = EIO; /* Can't read/write this location */
271 return 0; /* No bytes handled */
c906108c
SS
272}
273
274static void
fba45db2 275tcomplain (void)
c906108c
SS
276{
277 error ("You can't do that when your target is `%s'",
278 current_target.to_shortname);
279}
280
281void
fba45db2 282noprocess (void)
c906108c
SS
283{
284 error ("You can't do that without a process to debug.");
285}
286
c906108c 287static int
fba45db2 288nosymbol (char *name, CORE_ADDR *addrp)
c906108c 289{
c5aa993b 290 return 1; /* Symbol does not exist in target env */
c906108c
SS
291}
292
392a587b 293static void
fba45db2 294nosupport_runtime (void)
c906108c 295{
39f77062 296 if (ptid_equal (inferior_ptid, null_ptid))
c906108c
SS
297 noprocess ();
298 else
299 error ("No run-time support for this");
300}
301
302
c906108c 303static void
fba45db2 304default_terminal_info (char *args, int from_tty)
c906108c 305{
c5aa993b 306 printf_unfiltered ("No saved terminal information.\n");
c906108c
SS
307}
308
309/* This is the default target_create_inferior and target_attach function.
310 If the current target is executing, it asks whether to kill it off.
311 If this function returns without calling error(), it has killed off
312 the target, and the operation should be attempted. */
313
314static void
fba45db2 315kill_or_be_killed (int from_tty)
c906108c
SS
316{
317 if (target_has_execution)
318 {
319 printf_unfiltered ("You are already running a program:\n");
320 target_files_info ();
c5aa993b
JM
321 if (query ("Kill it? "))
322 {
323 target_kill ();
324 if (target_has_execution)
325 error ("Killing the program did not help.");
326 return;
327 }
328 else
329 {
330 error ("Program not killed.");
331 }
c906108c 332 }
c5aa993b 333 tcomplain ();
c906108c
SS
334}
335
336static void
fba45db2 337maybe_kill_then_attach (char *args, int from_tty)
c906108c
SS
338{
339 kill_or_be_killed (from_tty);
340 target_attach (args, from_tty);
341}
342
343static void
c27cda74
AC
344maybe_kill_then_create_inferior (char *exec, char *args, char **env,
345 int from_tty)
c906108c
SS
346{
347 kill_or_be_killed (0);
c27cda74 348 target_create_inferior (exec, args, env, from_tty);
c906108c
SS
349}
350
7998dfc3
AC
351/* Go through the target stack from top to bottom, copying over zero
352 entries in current_target, then filling in still empty entries. In
353 effect, we are doing class inheritance through the pushed target
354 vectors.
355
356 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
357 is currently implemented, is that it discards any knowledge of
358 which target an inherited method originally belonged to.
359 Consequently, new new target methods should instead explicitly and
360 locally search the target stack for the target that can handle the
361 request. */
c906108c
SS
362
363static void
7998dfc3 364update_current_target (void)
c906108c 365{
7998dfc3
AC
366 struct target_ops *t;
367
368 /* First, reset curren'ts contents. */
369 memset (&current_target, 0, sizeof (current_target));
370
371#define INHERIT(FIELD, TARGET) \
372 if (!current_target.FIELD) \
373 current_target.FIELD = (TARGET)->FIELD
374
375 for (t = target_stack; t; t = t->beneath)
376 {
377 INHERIT (to_shortname, t);
378 INHERIT (to_longname, t);
379 INHERIT (to_doc, t);
380 INHERIT (to_open, t);
381 INHERIT (to_close, t);
382 INHERIT (to_attach, t);
383 INHERIT (to_post_attach, t);
384 INHERIT (to_detach, t);
385 INHERIT (to_disconnect, t);
386 INHERIT (to_resume, t);
387 INHERIT (to_wait, t);
7998dfc3
AC
388 INHERIT (to_fetch_registers, t);
389 INHERIT (to_store_registers, t);
390 INHERIT (to_prepare_to_store, t);
391 INHERIT (to_xfer_memory, t);
392 INHERIT (to_files_info, t);
393 INHERIT (to_insert_breakpoint, t);
394 INHERIT (to_remove_breakpoint, t);
395 INHERIT (to_can_use_hw_breakpoint, t);
396 INHERIT (to_insert_hw_breakpoint, t);
397 INHERIT (to_remove_hw_breakpoint, t);
398 INHERIT (to_insert_watchpoint, t);
399 INHERIT (to_remove_watchpoint, t);
400 INHERIT (to_stopped_data_address, t);
401 INHERIT (to_stopped_by_watchpoint, t);
402 INHERIT (to_have_continuable_watchpoint, t);
403 INHERIT (to_region_size_ok_for_hw_watchpoint, t);
404 INHERIT (to_terminal_init, t);
405 INHERIT (to_terminal_inferior, t);
406 INHERIT (to_terminal_ours_for_output, t);
407 INHERIT (to_terminal_ours, t);
408 INHERIT (to_terminal_save_ours, t);
409 INHERIT (to_terminal_info, t);
410 INHERIT (to_kill, t);
411 INHERIT (to_load, t);
412 INHERIT (to_lookup_symbol, t);
413 INHERIT (to_create_inferior, t);
414 INHERIT (to_post_startup_inferior, t);
415 INHERIT (to_acknowledge_created_inferior, t);
416 INHERIT (to_insert_fork_catchpoint, t);
417 INHERIT (to_remove_fork_catchpoint, t);
418 INHERIT (to_insert_vfork_catchpoint, t);
419 INHERIT (to_remove_vfork_catchpoint, t);
420 INHERIT (to_follow_fork, t);
421 INHERIT (to_insert_exec_catchpoint, t);
422 INHERIT (to_remove_exec_catchpoint, t);
423 INHERIT (to_reported_exec_events_per_exec_call, t);
424 INHERIT (to_has_exited, t);
425 INHERIT (to_mourn_inferior, t);
426 INHERIT (to_can_run, t);
427 INHERIT (to_notice_signals, t);
428 INHERIT (to_thread_alive, t);
429 INHERIT (to_find_new_threads, t);
430 INHERIT (to_pid_to_str, t);
431 INHERIT (to_extra_thread_info, t);
432 INHERIT (to_stop, t);
4b8a223f 433 /* Do not inherit to_xfer_partial. */
7998dfc3
AC
434 INHERIT (to_rcmd, t);
435 INHERIT (to_enable_exception_callback, t);
436 INHERIT (to_get_current_exception_event, t);
437 INHERIT (to_pid_to_exec_file, t);
438 INHERIT (to_stratum, t);
439 INHERIT (to_has_all_memory, t);
440 INHERIT (to_has_memory, t);
441 INHERIT (to_has_stack, t);
442 INHERIT (to_has_registers, t);
443 INHERIT (to_has_execution, t);
444 INHERIT (to_has_thread_control, t);
445 INHERIT (to_sections, t);
446 INHERIT (to_sections_end, t);
447 INHERIT (to_can_async_p, t);
448 INHERIT (to_is_async_p, t);
449 INHERIT (to_async, t);
450 INHERIT (to_async_mask_value, t);
451 INHERIT (to_find_memory_regions, t);
452 INHERIT (to_make_corefile_notes, t);
453 INHERIT (to_get_thread_local_address, t);
454 INHERIT (to_magic, t);
455 }
456#undef INHERIT
457
458 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
459 it. Some entries are defaulted to a method that print an error,
460 others are hard-wired to a standard recursive default. */
c906108c
SS
461
462#define de_fault(field, value) \
7998dfc3
AC
463 if (!current_target.field) \
464 current_target.field = value
0d06e24b
JM
465
466 de_fault (to_open,
467 (void (*) (char *, int))
468 tcomplain);
469 de_fault (to_close,
470 (void (*) (int))
471 target_ignore);
472 de_fault (to_attach,
473 maybe_kill_then_attach);
474 de_fault (to_post_attach,
475 (void (*) (int))
476 target_ignore);
0d06e24b
JM
477 de_fault (to_detach,
478 (void (*) (char *, int))
479 target_ignore);
6ad8ae5c
DJ
480 de_fault (to_disconnect,
481 (void (*) (char *, int))
482 tcomplain);
0d06e24b 483 de_fault (to_resume,
39f77062 484 (void (*) (ptid_t, int, enum target_signal))
0d06e24b
JM
485 noprocess);
486 de_fault (to_wait,
39f77062 487 (ptid_t (*) (ptid_t, struct target_waitstatus *))
0d06e24b 488 noprocess);
0d06e24b
JM
489 de_fault (to_fetch_registers,
490 (void (*) (int))
491 target_ignore);
492 de_fault (to_store_registers,
493 (void (*) (int))
494 noprocess);
495 de_fault (to_prepare_to_store,
496 (void (*) (void))
497 noprocess);
498 de_fault (to_xfer_memory,
29e57380 499 (int (*) (CORE_ADDR, char *, int, int, struct mem_attrib *, struct target_ops *))
0d06e24b
JM
500 nomemory);
501 de_fault (to_files_info,
502 (void (*) (struct target_ops *))
503 target_ignore);
504 de_fault (to_insert_breakpoint,
505 memory_insert_breakpoint);
506 de_fault (to_remove_breakpoint,
507 memory_remove_breakpoint);
ccaa32c7
GS
508 de_fault (to_can_use_hw_breakpoint,
509 (int (*) (int, int, int))
510 return_zero);
511 de_fault (to_insert_hw_breakpoint,
512 (int (*) (CORE_ADDR, char *))
513 return_minus_one);
514 de_fault (to_remove_hw_breakpoint,
515 (int (*) (CORE_ADDR, char *))
516 return_minus_one);
517 de_fault (to_insert_watchpoint,
518 (int (*) (CORE_ADDR, int, int))
519 return_minus_one);
520 de_fault (to_remove_watchpoint,
521 (int (*) (CORE_ADDR, int, int))
522 return_minus_one);
523 de_fault (to_stopped_by_watchpoint,
524 (int (*) (void))
525 return_zero);
526 de_fault (to_stopped_data_address,
527 (CORE_ADDR (*) (void))
528 return_zero);
529 de_fault (to_region_size_ok_for_hw_watchpoint,
530 default_region_size_ok_for_hw_watchpoint);
0d06e24b
JM
531 de_fault (to_terminal_init,
532 (void (*) (void))
533 target_ignore);
534 de_fault (to_terminal_inferior,
535 (void (*) (void))
536 target_ignore);
537 de_fault (to_terminal_ours_for_output,
538 (void (*) (void))
539 target_ignore);
540 de_fault (to_terminal_ours,
541 (void (*) (void))
542 target_ignore);
a790ad35
SC
543 de_fault (to_terminal_save_ours,
544 (void (*) (void))
545 target_ignore);
0d06e24b
JM
546 de_fault (to_terminal_info,
547 default_terminal_info);
548 de_fault (to_kill,
549 (void (*) (void))
550 noprocess);
551 de_fault (to_load,
552 (void (*) (char *, int))
553 tcomplain);
554 de_fault (to_lookup_symbol,
555 (int (*) (char *, CORE_ADDR *))
556 nosymbol);
557 de_fault (to_create_inferior,
558 maybe_kill_then_create_inferior);
559 de_fault (to_post_startup_inferior,
39f77062 560 (void (*) (ptid_t))
0d06e24b
JM
561 target_ignore);
562 de_fault (to_acknowledge_created_inferior,
563 (void (*) (int))
564 target_ignore);
0d06e24b
JM
565 de_fault (to_insert_fork_catchpoint,
566 (int (*) (int))
567 tcomplain);
568 de_fault (to_remove_fork_catchpoint,
569 (int (*) (int))
570 tcomplain);
571 de_fault (to_insert_vfork_catchpoint,
572 (int (*) (int))
573 tcomplain);
574 de_fault (to_remove_vfork_catchpoint,
575 (int (*) (int))
576 tcomplain);
4c9ba7e0 577 de_fault (to_follow_fork,
6604731b 578 (int (*) (int))
0d06e24b
JM
579 target_ignore);
580 de_fault (to_insert_exec_catchpoint,
581 (int (*) (int))
582 tcomplain);
583 de_fault (to_remove_exec_catchpoint,
584 (int (*) (int))
585 tcomplain);
0d06e24b
JM
586 de_fault (to_reported_exec_events_per_exec_call,
587 (int (*) (void))
588 return_one);
0d06e24b
JM
589 de_fault (to_has_exited,
590 (int (*) (int, int, int *))
591 return_zero);
592 de_fault (to_mourn_inferior,
593 (void (*) (void))
594 noprocess);
595 de_fault (to_can_run,
596 return_zero);
597 de_fault (to_notice_signals,
39f77062 598 (void (*) (ptid_t))
0d06e24b
JM
599 target_ignore);
600 de_fault (to_thread_alive,
39f77062 601 (int (*) (ptid_t))
0d06e24b
JM
602 return_zero);
603 de_fault (to_find_new_threads,
604 (void (*) (void))
605 target_ignore);
606 de_fault (to_extra_thread_info,
607 (char *(*) (struct thread_info *))
608 return_zero);
609 de_fault (to_stop,
610 (void (*) (void))
611 target_ignore);
4b8a223f 612 current_target.to_xfer_partial = default_xfer_partial;
0d06e24b 613 de_fault (to_rcmd,
d9fcf2fb 614 (void (*) (char *, struct ui_file *))
0d06e24b
JM
615 tcomplain);
616 de_fault (to_enable_exception_callback,
617 (struct symtab_and_line * (*) (enum exception_event_kind, int))
618 nosupport_runtime);
619 de_fault (to_get_current_exception_event,
620 (struct exception_event_record * (*) (void))
621 nosupport_runtime);
622 de_fault (to_pid_to_exec_file,
623 (char *(*) (int))
624 return_zero);
0d06e24b
JM
625 de_fault (to_can_async_p,
626 (int (*) (void))
627 return_zero);
628 de_fault (to_is_async_p,
629 (int (*) (void))
630 return_zero);
631 de_fault (to_async,
632 (void (*) (void (*) (enum inferior_event_type, void*), void*))
633 tcomplain);
c906108c 634#undef de_fault
c906108c 635
7998dfc3
AC
636 /* Finally, position the target-stack beneath the squashed
637 "current_target". That way code looking for a non-inherited
638 target method can quickly and simply find it. */
639 current_target.beneath = target_stack;
c906108c
SS
640}
641
642/* Push a new target type into the stack of the existing target accessors,
643 possibly superseding some of the existing accessors.
644
645 Result is zero if the pushed target ended up on top of the stack,
646 nonzero if at least one target is on top of it.
647
648 Rather than allow an empty stack, we always have the dummy target at
649 the bottom stratum, so we can call the function vectors without
650 checking them. */
651
652int
fba45db2 653push_target (struct target_ops *t)
c906108c 654{
258b763a 655 struct target_ops **cur;
c906108c
SS
656
657 /* Check magic number. If wrong, it probably means someone changed
658 the struct definition, but not all the places that initialize one. */
659 if (t->to_magic != OPS_MAGIC)
660 {
c5aa993b
JM
661 fprintf_unfiltered (gdb_stderr,
662 "Magic number of %s target struct wrong\n",
663 t->to_shortname);
e1e9e218 664 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
665 }
666
258b763a
AC
667 /* Find the proper stratum to install this target in. */
668 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
c906108c 669 {
258b763a 670 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
c906108c
SS
671 break;
672 }
673
258b763a
AC
674 /* If there's already targets at this stratum, remove them. */
675 /* FIXME: cagney/2003-10-15: I think this should be poping all
676 targets to CUR, and not just those at this stratum level. */
677 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
678 {
679 /* There's already something at this stratum level. Close it,
680 and un-hook it from the stack. */
681 struct target_ops *tmp = (*cur);
682 (*cur) = (*cur)->beneath;
683 tmp->beneath = NULL;
f1c07ab0 684 target_close (tmp, 0);
258b763a 685 }
c906108c
SS
686
687 /* We have removed all targets in our stratum, now add the new one. */
258b763a
AC
688 t->beneath = (*cur);
689 (*cur) = t;
c906108c
SS
690
691 update_current_target ();
692
c906108c
SS
693 if (targetdebug)
694 setup_target_debug ();
c906108c 695
258b763a
AC
696 /* Not on top? */
697 return (t != target_stack);
c906108c
SS
698}
699
700/* Remove a target_ops vector from the stack, wherever it may be.
701 Return how many times it was removed (0 or 1). */
702
703int
fba45db2 704unpush_target (struct target_ops *t)
c906108c 705{
258b763a
AC
706 struct target_ops **cur;
707 struct target_ops *tmp;
c906108c 708
c906108c
SS
709 /* Look for the specified target. Note that we assume that a target
710 can only occur once in the target stack. */
711
258b763a
AC
712 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
713 {
714 if ((*cur) == t)
715 break;
716 }
c906108c 717
258b763a 718 if ((*cur) == NULL)
c906108c
SS
719 return 0; /* Didn't find target_ops, quit now */
720
5269965e
AC
721 /* NOTE: cagney/2003-12-06: In '94 the close call was made
722 unconditional by moving it to before the above check that the
723 target was in the target stack (something about "Change the way
724 pushing and popping of targets work to support target overlays
725 and inheritance"). This doesn't make much sense - only open
726 targets should be closed. */
727 target_close (t, 0);
728
c906108c 729 /* Unchain the target */
258b763a
AC
730 tmp = (*cur);
731 (*cur) = (*cur)->beneath;
732 tmp->beneath = NULL;
c906108c
SS
733
734 update_current_target ();
c906108c
SS
735
736 return 1;
737}
738
739void
fba45db2 740pop_target (void)
c906108c 741{
f1c07ab0 742 target_close (&current_target, 0); /* Let it clean up */
258b763a 743 if (unpush_target (target_stack) == 1)
c906108c
SS
744 return;
745
c5aa993b
JM
746 fprintf_unfiltered (gdb_stderr,
747 "pop_target couldn't find target %s\n",
748 current_target.to_shortname);
e1e9e218 749 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
750}
751
752#undef MIN
753#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
754
755/* target_read_string -- read a null terminated string, up to LEN bytes,
756 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
757 Set *STRING to a pointer to malloc'd memory containing the data; the caller
758 is responsible for freeing it. Return the number of bytes successfully
759 read. */
760
761int
fba45db2 762target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
763{
764 int tlen, origlen, offset, i;
765 char buf[4];
766 int errcode = 0;
767 char *buffer;
768 int buffer_allocated;
769 char *bufptr;
770 unsigned int nbytes_read = 0;
771
772 /* Small for testing. */
773 buffer_allocated = 4;
774 buffer = xmalloc (buffer_allocated);
775 bufptr = buffer;
776
777 origlen = len;
778
779 while (len > 0)
780 {
781 tlen = MIN (len, 4 - (memaddr & 3));
782 offset = memaddr & 3;
783
d4b2399a 784 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
c906108c
SS
785 if (errcode != 0)
786 {
787 /* The transfer request might have crossed the boundary to an
788 unallocated region of memory. Retry the transfer, requesting
789 a single byte. */
790 tlen = 1;
791 offset = 0;
d4b2399a 792 errcode = target_xfer_memory (memaddr, buf, 1, 0);
c906108c
SS
793 if (errcode != 0)
794 goto done;
795 }
796
797 if (bufptr - buffer + tlen > buffer_allocated)
798 {
799 unsigned int bytes;
800 bytes = bufptr - buffer;
801 buffer_allocated *= 2;
802 buffer = xrealloc (buffer, buffer_allocated);
803 bufptr = buffer + bytes;
804 }
805
806 for (i = 0; i < tlen; i++)
807 {
808 *bufptr++ = buf[i + offset];
809 if (buf[i + offset] == '\000')
810 {
811 nbytes_read += i + 1;
812 goto done;
813 }
814 }
815
816 memaddr += tlen;
817 len -= tlen;
818 nbytes_read += tlen;
819 }
c5aa993b 820done:
c906108c
SS
821 if (errnop != NULL)
822 *errnop = errcode;
823 if (string != NULL)
824 *string = buffer;
825 return nbytes_read;
826}
827
8db32d44
AC
828/* Find a section containing ADDR. */
829struct section_table *
830target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
831{
832 struct section_table *secp;
833 for (secp = target->to_sections;
834 secp < target->to_sections_end;
835 secp++)
836 {
837 if (addr >= secp->addr && addr < secp->endaddr)
838 return secp;
839 }
840 return NULL;
841}
842
c906108c
SS
843/* Read LEN bytes of target memory at address MEMADDR, placing the results in
844 GDB's memory at MYADDR. Returns either 0 for success or an errno value
845 if any error occurs.
846
847 If an error occurs, no guarantee is made about the contents of the data at
848 MYADDR. In particular, the caller should not depend upon partial reads
849 filling the buffer with good data. There is no way for the caller to know
850 how much good data might have been transfered anyway. Callers that can
851 deal with partial reads should call target_read_memory_partial. */
852
853int
fba45db2 854target_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 855{
d4b2399a 856 return target_xfer_memory (memaddr, myaddr, len, 0);
c906108c
SS
857}
858
c906108c 859int
fba45db2 860target_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 861{
d4b2399a 862 return target_xfer_memory (memaddr, myaddr, len, 1);
c906108c 863}
c5aa993b 864
3a11626d
MS
865static int trust_readonly = 0;
866
67e0617e
C
867/* Move memory to or from the targets. The top target gets priority;
868 if it cannot handle it, it is offered to the next one down, etc.
c906108c 869
67e0617e 870 Result is -1 on error, or the number of bytes transfered. */
c906108c 871
4930751a 872int
29e57380
C
873do_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
874 struct mem_attrib *attrib)
c906108c 875{
c906108c 876 int res;
4930751a 877 int done = 0;
c906108c 878 struct target_ops *t;
c906108c
SS
879
880 /* Zero length requests are ok and require no work. */
881 if (len == 0)
882 return 0;
883
c906108c
SS
884 /* to_xfer_memory is not guaranteed to set errno, even when it returns
885 0. */
886 errno = 0;
887
3a11626d
MS
888 if (!write && trust_readonly)
889 {
8db32d44 890 struct section_table *secp;
2ceb85d0
BE
891 /* User-settable option, "trust-readonly-sections". If true,
892 then memory from any SEC_READONLY bfd section may be read
8db32d44
AC
893 directly from the bfd file. */
894 secp = target_section_by_addr (&current_target, memaddr);
895 if (secp != NULL
896 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
897 & SEC_READONLY))
898 return xfer_memory (memaddr, myaddr, len, 0, attrib, &current_target);
3a11626d
MS
899 }
900
67e0617e 901 /* The quick case is that the top target can handle the transfer. */
c906108c 902 res = current_target.to_xfer_memory
29e57380 903 (memaddr, myaddr, len, write, attrib, &current_target);
c906108c 904
67e0617e
C
905 /* If res <= 0 then we call it again in the loop. Ah well. */
906 if (res <= 0)
c906108c 907 {
258b763a 908 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 909 {
c906108c
SS
910 if (!t->to_has_memory)
911 continue;
912
29e57380 913 res = t->to_xfer_memory (memaddr, myaddr, len, write, attrib, t);
c906108c
SS
914 if (res > 0)
915 break; /* Handled all or part of xfer */
916 if (t->to_has_all_memory)
917 break;
918 }
919
4930751a 920 if (res <= 0)
67e0617e 921 return -1;
4930751a 922 }
67e0617e
C
923
924 return res;
4930751a
C
925}
926
67e0617e
C
927
928/* Perform a memory transfer. Iterate until the entire region has
929 been transfered.
930
931 Result is 0 or errno value. */
932
4930751a
C
933static int
934target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
935{
936 int res;
29e57380
C
937 int reg_len;
938 struct mem_region *region;
4930751a
C
939
940 /* Zero length requests are ok and require no work. */
941 if (len == 0)
942 {
943 return 0;
944 }
945
946 while (len > 0)
947 {
29e57380
C
948 region = lookup_mem_region(memaddr);
949 if (memaddr + len < region->hi)
950 reg_len = len;
951 else
952 reg_len = region->hi - memaddr;
953
954 switch (region->attrib.mode)
c906108c 955 {
29e57380
C
956 case MEM_RO:
957 if (write)
958 return EIO;
959 break;
960
961 case MEM_WO:
c906108c 962 if (!write)
c906108c 963 return EIO;
29e57380 964 break;
c906108c 965 }
4930751a 966
29e57380
C
967 while (reg_len > 0)
968 {
969 if (region->attrib.cache)
8add0441 970 res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
29e57380
C
971 reg_len, write);
972 else
8add0441 973 res = do_xfer_memory (memaddr, myaddr, reg_len, write,
29e57380
C
974 &region->attrib);
975
976 if (res <= 0)
977 {
978 /* If this address is for nonexistent memory, read zeros
979 if reading, or do nothing if writing. Return
980 error. */
981 if (!write)
982 memset (myaddr, 0, len);
983 if (errno == 0)
984 return EIO;
985 else
986 return errno;
987 }
988
989 memaddr += res;
990 myaddr += res;
991 len -= res;
992 reg_len -= res;
993 }
c906108c 994 }
4930751a 995
c906108c
SS
996 return 0; /* We managed to cover it all somehow. */
997}
998
999
67e0617e
C
1000/* Perform a partial memory transfer.
1001
1002 Result is -1 on error, or the number of bytes transfered. */
917317f4
JM
1003
1004static int
4930751a 1005target_xfer_memory_partial (CORE_ADDR memaddr, char *myaddr, int len,
917317f4
JM
1006 int write_p, int *err)
1007{
1008 int res;
29e57380
C
1009 int reg_len;
1010 struct mem_region *region;
917317f4
JM
1011
1012 /* Zero length requests are ok and require no work. */
1013 if (len == 0)
1014 {
1015 *err = 0;
1016 return 0;
1017 }
1018
29e57380
C
1019 region = lookup_mem_region(memaddr);
1020 if (memaddr + len < region->hi)
1021 reg_len = len;
1022 else
1023 reg_len = region->hi - memaddr;
1024
1025 switch (region->attrib.mode)
1026 {
1027 case MEM_RO:
1028 if (write_p)
1029 {
1030 *err = EIO;
873406a6 1031 return -1;
29e57380
C
1032 }
1033 break;
1034
1035 case MEM_WO:
1036 if (write_p)
1037 {
1038 *err = EIO;
873406a6 1039 return -1;
29e57380
C
1040 }
1041 break;
1042 }
1043
1044 if (region->attrib.cache)
1045 res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
1046 reg_len, write_p);
1047 else
1048 res = do_xfer_memory (memaddr, myaddr, reg_len, write_p,
1049 &region->attrib);
1050
4930751a 1051 if (res <= 0)
917317f4 1052 {
4930751a
C
1053 if (errno != 0)
1054 *err = errno;
1055 else
1056 *err = EIO;
917317f4 1057
4930751a 1058 return -1;
917317f4
JM
1059 }
1060
4930751a 1061 *err = 0;
67e0617e 1062 return res;
917317f4
JM
1063}
1064
1065int
1066target_read_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1067{
1068 return target_xfer_memory_partial (memaddr, buf, len, 0, err);
1069}
1070
1071int
1072target_write_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1073{
1074 return target_xfer_memory_partial (memaddr, buf, len, 1, err);
1075}
1076
1e3ff5ad
AC
1077/* More generic transfers. */
1078
0088c768 1079static LONGEST
8aa91c1e
AC
1080default_xfer_partial (struct target_ops *ops, enum target_object object,
1081 const char *annex, void *readbuf,
1082 const void *writebuf, ULONGEST offset, LONGEST len)
0088c768
AC
1083{
1084 if (object == TARGET_OBJECT_MEMORY
1085 && ops->to_xfer_memory != NULL)
1086 /* If available, fall back to the target's "to_xfer_memory"
1087 method. */
1088 {
4b8a223f 1089 int xfered = -1;
0088c768 1090 errno = 0;
4b8a223f
AC
1091 if (writebuf != NULL)
1092 {
1093 void *buffer = xmalloc (len);
1094 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1095 memcpy (buffer, writebuf, len);
1096 xfered = ops->to_xfer_memory (offset, buffer, len, 1/*write*/, NULL,
1097 ops);
1098 do_cleanups (cleanup);
1099 }
1100 if (readbuf != NULL)
1101 xfered = ops->to_xfer_memory (offset, readbuf, len, 0/*read*/, NULL,
0088c768 1102 ops);
0088c768
AC
1103 if (xfered > 0)
1104 return xfered;
1105 else if (xfered == 0 && errno == 0)
1106 /* "to_xfer_memory" uses 0, cross checked against ERRNO as one
1107 indication of an error. */
1108 return 0;
1109 else
1110 return -1;
1111 }
1112 else if (ops->beneath != NULL)
4b8a223f 1113 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
8aa91c1e 1114 readbuf, writebuf, offset, len);
0088c768
AC
1115 else
1116 return -1;
1117}
1118
1119/* Target vector read/write partial wrapper functions.
1120
1121 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1122 (inbuf, outbuf)", instead of separate read/write methods, make life
1123 easier. */
1124
1e3ff5ad
AC
1125LONGEST
1126target_read_partial (struct target_ops *ops,
1127 enum target_object object,
1128 const char *annex, void *buf,
1129 ULONGEST offset, LONGEST len)
1130{
4b8a223f 1131 gdb_assert (ops->to_xfer_partial != NULL);
8aa91c1e 1132 return ops->to_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1e3ff5ad
AC
1133}
1134
1135LONGEST
1136target_write_partial (struct target_ops *ops,
1137 enum target_object object,
1138 const char *annex, const void *buf,
1139 ULONGEST offset, LONGEST len)
1140{
4b8a223f 1141 gdb_assert (ops->to_xfer_partial != NULL);
8aa91c1e 1142 return ops->to_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1e3ff5ad
AC
1143}
1144
1145/* Wrappers to perform the full transfer. */
1146LONGEST
1147target_read (struct target_ops *ops,
1148 enum target_object object,
1149 const char *annex, void *buf,
1150 ULONGEST offset, LONGEST len)
1151{
1152 LONGEST xfered = 0;
1153 while (xfered < len)
1154 {
0088c768
AC
1155 LONGEST xfer = target_read_partial (ops, object, annex,
1156 (bfd_byte *) buf + xfered,
1157 offset + xfered, len - xfered);
1e3ff5ad 1158 /* Call an observer, notifying them of the xfer progress? */
0088c768
AC
1159 if (xfer <= 0)
1160 /* Call memory_error? */
1161 return -1;
1e3ff5ad
AC
1162 xfered += xfer;
1163 QUIT;
1164 }
1165 return len;
1166}
1167
1168LONGEST
1169target_write (struct target_ops *ops,
1170 enum target_object object,
1171 const char *annex, const void *buf,
1172 ULONGEST offset, LONGEST len)
1173{
1174 LONGEST xfered = 0;
1175 while (xfered < len)
1176 {
1177 LONGEST xfer = target_write_partial (ops, object, annex,
1178 (bfd_byte *) buf + xfered,
1179 offset + xfered, len - xfered);
1180 /* Call an observer, notifying them of the xfer progress? */
0088c768
AC
1181 if (xfer <= 0)
1182 /* Call memory_error? */
1183 return -1;
1e3ff5ad
AC
1184 xfered += xfer;
1185 QUIT;
1186 }
1187 return len;
1188}
1189
b6591e8b
AC
1190/* Memory transfer methods. */
1191
1192void
1193get_target_memory (struct target_ops *ops, CORE_ADDR addr, void *buf,
1194 LONGEST len)
1195{
1196 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL, buf, addr, len)
1197 != len)
1198 memory_error (EIO, addr);
1199}
1200
1201ULONGEST
1202get_target_memory_unsigned (struct target_ops *ops,
1203 CORE_ADDR addr, int len)
1204{
1205 char buf[sizeof (ULONGEST)];
1206
1207 gdb_assert (len <= sizeof (buf));
1208 get_target_memory (ops, addr, buf, len);
1209 return extract_unsigned_integer (buf, len);
1210}
1211
c906108c 1212static void
fba45db2 1213target_info (char *args, int from_tty)
c906108c
SS
1214{
1215 struct target_ops *t;
c906108c 1216 int has_all_mem = 0;
c5aa993b 1217
c906108c
SS
1218 if (symfile_objfile != NULL)
1219 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
1220
258b763a 1221 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 1222 {
c906108c
SS
1223 if (!t->to_has_memory)
1224 continue;
1225
c5aa993b 1226 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1227 continue;
1228 if (has_all_mem)
c5aa993b
JM
1229 printf_unfiltered ("\tWhile running this, GDB does not access memory from...\n");
1230 printf_unfiltered ("%s:\n", t->to_longname);
1231 (t->to_files_info) (t);
c906108c
SS
1232 has_all_mem = t->to_has_all_memory;
1233 }
1234}
1235
1236/* This is to be called by the open routine before it does
1237 anything. */
1238
1239void
fba45db2 1240target_preopen (int from_tty)
c906108c 1241{
c5aa993b 1242 dont_repeat ();
c906108c
SS
1243
1244 if (target_has_execution)
c5aa993b 1245 {
adf40b2e
JM
1246 if (!from_tty
1247 || query ("A program is being debugged already. Kill it? "))
c5aa993b 1248 target_kill ();
c906108c 1249 else
c5aa993b 1250 error ("Program not killed.");
c906108c
SS
1251 }
1252
1253 /* Calling target_kill may remove the target from the stack. But if
1254 it doesn't (which seems like a win for UDI), remove it now. */
1255
1256 if (target_has_execution)
1257 pop_target ();
1258}
1259
1260/* Detach a target after doing deferred register stores. */
1261
1262void
fba45db2 1263target_detach (char *args, int from_tty)
c906108c 1264{
c906108c
SS
1265 (current_target.to_detach) (args, from_tty);
1266}
1267
6ad8ae5c
DJ
1268void
1269target_disconnect (char *args, int from_tty)
1270{
6ad8ae5c
DJ
1271 (current_target.to_disconnect) (args, from_tty);
1272}
1273
c906108c 1274void
fba45db2 1275target_link (char *modname, CORE_ADDR *t_reloc)
c906108c 1276{
cb137aa5 1277 if (DEPRECATED_STREQ (current_target.to_shortname, "rombug"))
c906108c
SS
1278 {
1279 (current_target.to_lookup_symbol) (modname, t_reloc);
1280 if (*t_reloc == 0)
c5aa993b 1281 error ("Unable to link to %s and get relocation in rombug", modname);
c906108c
SS
1282 }
1283 else
2acceee2 1284 *t_reloc = (CORE_ADDR) -1;
c906108c
SS
1285}
1286
ed9a39eb
JM
1287int
1288target_async_mask (int mask)
1289{
1290 int saved_async_masked_status = target_async_mask_value;
1291 target_async_mask_value = mask;
1292 return saved_async_masked_status;
1293}
1294
c906108c
SS
1295/* Look through the list of possible targets for a target that can
1296 execute a run or attach command without any other data. This is
1297 used to locate the default process stratum.
1298
1299 Result is always valid (error() is called for errors). */
1300
1301static struct target_ops *
fba45db2 1302find_default_run_target (char *do_mesg)
c906108c
SS
1303{
1304 struct target_ops **t;
1305 struct target_ops *runable = NULL;
1306 int count;
1307
1308 count = 0;
1309
1310 for (t = target_structs; t < target_structs + target_struct_size;
1311 ++t)
1312 {
c5aa993b 1313 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
1314 {
1315 runable = *t;
1316 ++count;
1317 }
1318 }
1319
1320 if (count != 1)
1321 error ("Don't know how to %s. Try \"help target\".", do_mesg);
1322
1323 return runable;
1324}
1325
1326void
fba45db2 1327find_default_attach (char *args, int from_tty)
c906108c
SS
1328{
1329 struct target_ops *t;
1330
c5aa993b 1331 t = find_default_run_target ("attach");
c906108c
SS
1332 (t->to_attach) (args, from_tty);
1333 return;
1334}
1335
c906108c 1336void
c27cda74
AC
1337find_default_create_inferior (char *exec_file, char *allargs, char **env,
1338 int from_tty)
c906108c
SS
1339{
1340 struct target_ops *t;
1341
c5aa993b 1342 t = find_default_run_target ("run");
c27cda74 1343 (t->to_create_inferior) (exec_file, allargs, env, from_tty);
c906108c
SS
1344 return;
1345}
1346
ccaa32c7
GS
1347static int
1348default_region_size_ok_for_hw_watchpoint (int byte_count)
1349{
437b434f 1350 return (byte_count <= TYPE_LENGTH (builtin_type_void_data_ptr));
ccaa32c7
GS
1351}
1352
c906108c 1353static int
fba45db2 1354return_zero (void)
c906108c
SS
1355{
1356 return 0;
1357}
1358
1359static int
fba45db2 1360return_one (void)
c906108c
SS
1361{
1362 return 1;
1363}
1364
ccaa32c7
GS
1365static int
1366return_minus_one (void)
1367{
1368 return -1;
1369}
1370
6426a772
JM
1371/*
1372 * Resize the to_sections pointer. Also make sure that anyone that
1373 * was holding on to an old value of it gets updated.
1374 * Returns the old size.
1375 */
1376
1377int
1378target_resize_to_sections (struct target_ops *target, int num_added)
1379{
1380 struct target_ops **t;
1381 struct section_table *old_value;
1382 int old_count;
1383
1384 old_value = target->to_sections;
1385
1386 if (target->to_sections)
1387 {
1388 old_count = target->to_sections_end - target->to_sections;
1389 target->to_sections = (struct section_table *)
1390 xrealloc ((char *) target->to_sections,
1391 (sizeof (struct section_table)) * (num_added + old_count));
1392 }
1393 else
1394 {
1395 old_count = 0;
1396 target->to_sections = (struct section_table *)
1397 xmalloc ((sizeof (struct section_table)) * num_added);
1398 }
1399 target->to_sections_end = target->to_sections + (num_added + old_count);
1400
1401 /* Check to see if anyone else was pointing to this structure.
1402 If old_value was null, then no one was. */
1403
1404 if (old_value)
1405 {
1406 for (t = target_structs; t < target_structs + target_struct_size;
1407 ++t)
1408 {
1409 if ((*t)->to_sections == old_value)
1410 {
1411 (*t)->to_sections = target->to_sections;
1412 (*t)->to_sections_end = target->to_sections_end;
1413 }
1414 }
e354df01
NW
1415 /* There is a flattened view of the target stack in current_target,
1416 so its to_sections pointer might also need updating. */
1417 if (current_target.to_sections == old_value)
1418 {
1419 current_target.to_sections = target->to_sections;
1420 current_target.to_sections_end = target->to_sections_end;
1421 }
6426a772
JM
1422 }
1423
1424 return old_count;
1425
1426}
1427
07cd4b97
JB
1428/* Remove all target sections taken from ABFD.
1429
1430 Scan the current target stack for targets whose section tables
1431 refer to sections from BFD, and remove those sections. We use this
1432 when we notice that the inferior has unloaded a shared object, for
1433 example. */
1434void
1435remove_target_sections (bfd *abfd)
1436{
1437 struct target_ops **t;
1438
1439 for (t = target_structs; t < target_structs + target_struct_size; t++)
1440 {
1441 struct section_table *src, *dest;
1442
1443 dest = (*t)->to_sections;
1444 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
1445 if (src->bfd != abfd)
1446 {
1447 /* Keep this section. */
1448 if (dest < src) *dest = *src;
1449 dest++;
1450 }
1451
1452 /* If we've dropped any sections, resize the section table. */
1453 if (dest < src)
1454 target_resize_to_sections (*t, dest - src);
1455 }
1456}
1457
1458
1459
1460
7a292a7a
SS
1461/* Find a single runnable target in the stack and return it. If for
1462 some reason there is more than one, return NULL. */
1463
1464struct target_ops *
fba45db2 1465find_run_target (void)
7a292a7a
SS
1466{
1467 struct target_ops **t;
1468 struct target_ops *runable = NULL;
1469 int count;
c5aa993b 1470
7a292a7a 1471 count = 0;
c5aa993b 1472
7a292a7a
SS
1473 for (t = target_structs; t < target_structs + target_struct_size; ++t)
1474 {
c5aa993b 1475 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
1476 {
1477 runable = *t;
1478 ++count;
1479 }
1480 }
c5aa993b 1481
7a292a7a
SS
1482 return (count == 1 ? runable : NULL);
1483}
1484
ed9a39eb
JM
1485/* Find a single core_stratum target in the list of targets and return it.
1486 If for some reason there is more than one, return NULL. */
1487
c906108c 1488struct target_ops *
fba45db2 1489find_core_target (void)
c906108c
SS
1490{
1491 struct target_ops **t;
1492 struct target_ops *runable = NULL;
1493 int count;
c5aa993b 1494
c906108c 1495 count = 0;
c5aa993b 1496
c906108c
SS
1497 for (t = target_structs; t < target_structs + target_struct_size;
1498 ++t)
1499 {
1500 if ((*t)->to_stratum == core_stratum)
1501 {
1502 runable = *t;
1503 ++count;
1504 }
1505 }
c5aa993b
JM
1506
1507 return (count == 1 ? runable : NULL);
c906108c 1508}
ed9a39eb
JM
1509
1510/*
1511 * Find the next target down the stack from the specified target.
1512 */
1513
1514struct target_ops *
fba45db2 1515find_target_beneath (struct target_ops *t)
ed9a39eb 1516{
258b763a 1517 return t->beneath;
ed9a39eb
JM
1518}
1519
c906108c
SS
1520\f
1521/* The inferior process has died. Long live the inferior! */
1522
1523void
fba45db2 1524generic_mourn_inferior (void)
c906108c
SS
1525{
1526 extern int show_breakpoint_hit_counts;
1527
39f77062 1528 inferior_ptid = null_ptid;
c906108c
SS
1529 attach_flag = 0;
1530 breakpoint_init_inferior (inf_exited);
1531 registers_changed ();
1532
c906108c
SS
1533 reopen_exec_file ();
1534 reinit_frame_cache ();
1535
1536 /* It is confusing to the user for ignore counts to stick around
1537 from previous runs of the inferior. So clear them. */
1538 /* However, it is more confusing for the ignore counts to disappear when
1539 using hit counts. So don't clear them if we're counting hits. */
1540 if (!show_breakpoint_hit_counts)
1541 breakpoint_clear_ignore_counts ();
c5b739b5 1542
9a4105ab
AC
1543 if (deprecated_detach_hook)
1544 deprecated_detach_hook ();
c906108c
SS
1545}
1546\f
c906108c
SS
1547/* Helper function for child_wait and the Lynx derivatives of child_wait.
1548 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1549 translation of that in OURSTATUS. */
1550void
fba45db2 1551store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c
SS
1552{
1553#ifdef CHILD_SPECIAL_WAITSTATUS
1554 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1555 if it wants to deal with hoststatus. */
1556 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1557 return;
1558#endif
1559
1560 if (WIFEXITED (hoststatus))
1561 {
1562 ourstatus->kind = TARGET_WAITKIND_EXITED;
1563 ourstatus->value.integer = WEXITSTATUS (hoststatus);
1564 }
1565 else if (!WIFSTOPPED (hoststatus))
1566 {
1567 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1568 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1569 }
1570 else
1571 {
1572 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1573 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1574 }
1575}
1576\f
c906108c 1577/* Returns zero to leave the inferior alone, one to interrupt it. */
507f3c78 1578int (*target_activity_function) (void);
c906108c
SS
1579int target_activity_fd;
1580\f
1581/* Convert a normal process ID to a string. Returns the string in a static
1582 buffer. */
1583
1584char *
39f77062 1585normal_pid_to_str (ptid_t ptid)
c906108c
SS
1586{
1587 static char buf[30];
1588
39f77062 1589 sprintf (buf, "process %d", PIDGET (ptid));
c906108c
SS
1590 return buf;
1591}
1592
be4d1333 1593/* Error-catcher for target_find_memory_regions */
be4d1333
MS
1594static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
1595{
1596 error ("No target.");
1597 return 0;
1598}
1599
1600/* Error-catcher for target_make_corefile_notes */
be4d1333
MS
1601static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
1602{
1603 error ("No target.");
1604 return NULL;
1605}
1606
c906108c
SS
1607/* Set up the handful of non-empty slots needed by the dummy target
1608 vector. */
1609
1610static void
fba45db2 1611init_dummy_target (void)
c906108c
SS
1612{
1613 dummy_target.to_shortname = "None";
1614 dummy_target.to_longname = "None";
1615 dummy_target.to_doc = "";
1616 dummy_target.to_attach = find_default_attach;
c906108c 1617 dummy_target.to_create_inferior = find_default_create_inferior;
ed9a39eb 1618 dummy_target.to_pid_to_str = normal_pid_to_str;
c906108c 1619 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
1620 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
1621 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
0b603eba 1622 dummy_target.to_xfer_partial = default_xfer_partial;
c906108c
SS
1623 dummy_target.to_magic = OPS_MAGIC;
1624}
c906108c 1625\f
c5aa993b 1626
c906108c
SS
1627static struct target_ops debug_target;
1628
1629static void
fba45db2 1630debug_to_open (char *args, int from_tty)
c906108c
SS
1631{
1632 debug_target.to_open (args, from_tty);
1633
96baa820 1634 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
1635}
1636
1637static void
fba45db2 1638debug_to_close (int quitting)
c906108c 1639{
f1c07ab0 1640 target_close (&debug_target, quitting);
96baa820 1641 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
c906108c
SS
1642}
1643
f1c07ab0
AC
1644void
1645target_close (struct target_ops *targ, int quitting)
1646{
1647 if (targ->to_xclose != NULL)
1648 targ->to_xclose (targ, quitting);
1649 else if (targ->to_close != NULL)
1650 targ->to_close (quitting);
1651}
1652
c906108c 1653static void
fba45db2 1654debug_to_attach (char *args, int from_tty)
c906108c
SS
1655{
1656 debug_target.to_attach (args, from_tty);
1657
96baa820 1658 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
c906108c
SS
1659}
1660
1661
1662static void
fba45db2 1663debug_to_post_attach (int pid)
c906108c
SS
1664{
1665 debug_target.to_post_attach (pid);
1666
96baa820 1667 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
1668}
1669
c906108c 1670static void
fba45db2 1671debug_to_detach (char *args, int from_tty)
c906108c
SS
1672{
1673 debug_target.to_detach (args, from_tty);
1674
96baa820 1675 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
c906108c
SS
1676}
1677
6ad8ae5c
DJ
1678static void
1679debug_to_disconnect (char *args, int from_tty)
1680{
1681 debug_target.to_disconnect (args, from_tty);
1682
1683 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
1684 args, from_tty);
1685}
1686
c906108c 1687static void
39f77062 1688debug_to_resume (ptid_t ptid, int step, enum target_signal siggnal)
c906108c 1689{
39f77062 1690 debug_target.to_resume (ptid, step, siggnal);
c906108c 1691
39f77062 1692 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", PIDGET (ptid),
c906108c
SS
1693 step ? "step" : "continue",
1694 target_signal_to_name (siggnal));
1695}
1696
39f77062
KB
1697static ptid_t
1698debug_to_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 1699{
39f77062 1700 ptid_t retval;
c906108c 1701
39f77062 1702 retval = debug_target.to_wait (ptid, status);
c906108c 1703
96baa820 1704 fprintf_unfiltered (gdb_stdlog,
39f77062
KB
1705 "target_wait (%d, status) = %d, ", PIDGET (ptid),
1706 PIDGET (retval));
96baa820 1707 fprintf_unfiltered (gdb_stdlog, "status->kind = ");
c906108c
SS
1708 switch (status->kind)
1709 {
1710 case TARGET_WAITKIND_EXITED:
96baa820 1711 fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
c906108c
SS
1712 status->value.integer);
1713 break;
1714 case TARGET_WAITKIND_STOPPED:
96baa820 1715 fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
c906108c
SS
1716 target_signal_to_name (status->value.sig));
1717 break;
1718 case TARGET_WAITKIND_SIGNALLED:
96baa820 1719 fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
c906108c
SS
1720 target_signal_to_name (status->value.sig));
1721 break;
1722 case TARGET_WAITKIND_LOADED:
96baa820 1723 fprintf_unfiltered (gdb_stdlog, "loaded\n");
c906108c
SS
1724 break;
1725 case TARGET_WAITKIND_FORKED:
96baa820 1726 fprintf_unfiltered (gdb_stdlog, "forked\n");
c906108c
SS
1727 break;
1728 case TARGET_WAITKIND_VFORKED:
96baa820 1729 fprintf_unfiltered (gdb_stdlog, "vforked\n");
c906108c
SS
1730 break;
1731 case TARGET_WAITKIND_EXECD:
96baa820 1732 fprintf_unfiltered (gdb_stdlog, "execd\n");
c906108c
SS
1733 break;
1734 case TARGET_WAITKIND_SPURIOUS:
96baa820 1735 fprintf_unfiltered (gdb_stdlog, "spurious\n");
c906108c
SS
1736 break;
1737 default:
96baa820 1738 fprintf_unfiltered (gdb_stdlog, "unknown???\n");
c906108c
SS
1739 break;
1740 }
1741
1742 return retval;
1743}
1744
bf0c5130
AC
1745static void
1746debug_print_register (const char * func, int regno)
1747{
1748 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1749 if (regno >= 0 && regno < NUM_REGS + NUM_PSEUDO_REGS
1750 && REGISTER_NAME (regno) != NULL && REGISTER_NAME (regno)[0] != '\0')
1751 fprintf_unfiltered (gdb_stdlog, "(%s)", REGISTER_NAME (regno));
1752 else
1753 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1754 if (regno >= 0)
1755 {
1756 int i;
d9d9c31f 1757 unsigned char buf[MAX_REGISTER_SIZE];
4caf0990 1758 deprecated_read_register_gen (regno, buf);
bf0c5130 1759 fprintf_unfiltered (gdb_stdlog, " = ");
3acba339 1760 for (i = 0; i < register_size (current_gdbarch, regno); i++)
bf0c5130
AC
1761 {
1762 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1763 }
3acba339 1764 if (register_size (current_gdbarch, regno) <= sizeof (LONGEST))
bf0c5130
AC
1765 {
1766 fprintf_unfiltered (gdb_stdlog, " 0x%s %s",
1767 paddr_nz (read_register (regno)),
1768 paddr_d (read_register (regno)));
1769 }
1770 }
1771 fprintf_unfiltered (gdb_stdlog, "\n");
1772}
1773
c906108c 1774static void
fba45db2 1775debug_to_fetch_registers (int regno)
c906108c
SS
1776{
1777 debug_target.to_fetch_registers (regno);
bf0c5130 1778 debug_print_register ("target_fetch_registers", regno);
c906108c
SS
1779}
1780
1781static void
fba45db2 1782debug_to_store_registers (int regno)
c906108c
SS
1783{
1784 debug_target.to_store_registers (regno);
bf0c5130
AC
1785 debug_print_register ("target_store_registers", regno);
1786 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
1787}
1788
1789static void
fba45db2 1790debug_to_prepare_to_store (void)
c906108c
SS
1791{
1792 debug_target.to_prepare_to_store ();
1793
96baa820 1794 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
1795}
1796
1797static int
fba45db2 1798debug_to_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
29e57380 1799 struct mem_attrib *attrib,
fba45db2 1800 struct target_ops *target)
c906108c
SS
1801{
1802 int retval;
1803
29e57380
C
1804 retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write,
1805 attrib, target);
c906108c 1806
96baa820 1807 fprintf_unfiltered (gdb_stdlog,
c906108c 1808 "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
c5aa993b 1809 (unsigned int) memaddr, /* possable truncate long long */
c906108c
SS
1810 len, write ? "write" : "read", retval);
1811
c906108c
SS
1812 if (retval > 0)
1813 {
1814 int i;
1815
96baa820 1816 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
1817 for (i = 0; i < retval; i++)
1818 {
1819 if ((((long) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
1820 {
1821 if (targetdebug < 2 && i > 0)
1822 {
1823 fprintf_unfiltered (gdb_stdlog, " ...");
1824 break;
1825 }
1826 fprintf_unfiltered (gdb_stdlog, "\n");
1827 }
1828
96baa820 1829 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
1830 }
1831 }
1832
96baa820 1833 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
1834
1835 return retval;
1836}
1837
1838static void
fba45db2 1839debug_to_files_info (struct target_ops *target)
c906108c
SS
1840{
1841 debug_target.to_files_info (target);
1842
96baa820 1843 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
1844}
1845
1846static int
fba45db2 1847debug_to_insert_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
1848{
1849 int retval;
1850
1851 retval = debug_target.to_insert_breakpoint (addr, save);
1852
96baa820 1853 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
1854 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
1855 (unsigned long) addr,
1856 (unsigned long) retval);
c906108c
SS
1857 return retval;
1858}
1859
1860static int
fba45db2 1861debug_to_remove_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
1862{
1863 int retval;
1864
1865 retval = debug_target.to_remove_breakpoint (addr, save);
1866
96baa820 1867 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
1868 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
1869 (unsigned long) addr,
1870 (unsigned long) retval);
c906108c
SS
1871 return retval;
1872}
1873
ccaa32c7
GS
1874static int
1875debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
1876{
1877 int retval;
1878
1879 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
1880
1881 fprintf_unfiltered (gdb_stdlog,
1882 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
1883 (unsigned long) type,
1884 (unsigned long) cnt,
1885 (unsigned long) from_tty,
1886 (unsigned long) retval);
1887 return retval;
1888}
1889
1890static int
1891debug_to_region_size_ok_for_hw_watchpoint (int byte_count)
1892{
1893 CORE_ADDR retval;
1894
1895 retval = debug_target.to_region_size_ok_for_hw_watchpoint (byte_count);
1896
1897 fprintf_unfiltered (gdb_stdlog,
1898 "TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT (%ld) = 0x%lx\n",
1899 (unsigned long) byte_count,
1900 (unsigned long) retval);
1901 return retval;
1902}
1903
1904static int
1905debug_to_stopped_by_watchpoint (void)
1906{
1907 int retval;
1908
1909 retval = debug_target.to_stopped_by_watchpoint ();
1910
1911 fprintf_unfiltered (gdb_stdlog,
1912 "STOPPED_BY_WATCHPOINT () = %ld\n",
1913 (unsigned long) retval);
1914 return retval;
1915}
1916
1917static CORE_ADDR
1918debug_to_stopped_data_address (void)
1919{
1920 CORE_ADDR retval;
1921
1922 retval = debug_target.to_stopped_data_address ();
1923
1924 fprintf_unfiltered (gdb_stdlog,
1925 "target_stopped_data_address () = 0x%lx\n",
1926 (unsigned long) retval);
1927 return retval;
1928}
1929
1930static int
1931debug_to_insert_hw_breakpoint (CORE_ADDR addr, char *save)
1932{
1933 int retval;
1934
1935 retval = debug_target.to_insert_hw_breakpoint (addr, save);
1936
1937 fprintf_unfiltered (gdb_stdlog,
1938 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
1939 (unsigned long) addr,
1940 (unsigned long) retval);
1941 return retval;
1942}
1943
1944static int
1945debug_to_remove_hw_breakpoint (CORE_ADDR addr, char *save)
1946{
1947 int retval;
1948
1949 retval = debug_target.to_remove_hw_breakpoint (addr, save);
1950
1951 fprintf_unfiltered (gdb_stdlog,
1952 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
1953 (unsigned long) addr,
1954 (unsigned long) retval);
1955 return retval;
1956}
1957
1958static int
1959debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
1960{
1961 int retval;
1962
1963 retval = debug_target.to_insert_watchpoint (addr, len, type);
1964
1965 fprintf_unfiltered (gdb_stdlog,
1966 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
1967 (unsigned long) addr, len, type, (unsigned long) retval);
1968 return retval;
1969}
1970
1971static int
1972debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
1973{
1974 int retval;
1975
1976 retval = debug_target.to_insert_watchpoint (addr, len, type);
1977
1978 fprintf_unfiltered (gdb_stdlog,
1979 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
1980 (unsigned long) addr, len, type, (unsigned long) retval);
1981 return retval;
1982}
1983
c906108c 1984static void
fba45db2 1985debug_to_terminal_init (void)
c906108c
SS
1986{
1987 debug_target.to_terminal_init ();
1988
96baa820 1989 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
1990}
1991
1992static void
fba45db2 1993debug_to_terminal_inferior (void)
c906108c
SS
1994{
1995 debug_target.to_terminal_inferior ();
1996
96baa820 1997 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
1998}
1999
2000static void
fba45db2 2001debug_to_terminal_ours_for_output (void)
c906108c
SS
2002{
2003 debug_target.to_terminal_ours_for_output ();
2004
96baa820 2005 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
2006}
2007
2008static void
fba45db2 2009debug_to_terminal_ours (void)
c906108c
SS
2010{
2011 debug_target.to_terminal_ours ();
2012
96baa820 2013 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
2014}
2015
a790ad35
SC
2016static void
2017debug_to_terminal_save_ours (void)
2018{
2019 debug_target.to_terminal_save_ours ();
2020
2021 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
2022}
2023
c906108c 2024static void
fba45db2 2025debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
2026{
2027 debug_target.to_terminal_info (arg, from_tty);
2028
96baa820 2029 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
2030 from_tty);
2031}
2032
2033static void
fba45db2 2034debug_to_kill (void)
c906108c
SS
2035{
2036 debug_target.to_kill ();
2037
96baa820 2038 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
c906108c
SS
2039}
2040
2041static void
fba45db2 2042debug_to_load (char *args, int from_tty)
c906108c
SS
2043{
2044 debug_target.to_load (args, from_tty);
2045
96baa820 2046 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
2047}
2048
2049static int
fba45db2 2050debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
2051{
2052 int retval;
2053
2054 retval = debug_target.to_lookup_symbol (name, addrp);
2055
96baa820 2056 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
2057
2058 return retval;
2059}
2060
2061static void
c27cda74
AC
2062debug_to_create_inferior (char *exec_file, char *args, char **env,
2063 int from_tty)
c906108c 2064{
c27cda74 2065 debug_target.to_create_inferior (exec_file, args, env, from_tty);
c906108c 2066
c27cda74
AC
2067 fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx, %d)\n",
2068 exec_file, args, from_tty);
c906108c
SS
2069}
2070
2071static void
39f77062 2072debug_to_post_startup_inferior (ptid_t ptid)
c906108c 2073{
39f77062 2074 debug_target.to_post_startup_inferior (ptid);
c906108c 2075
96baa820 2076 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 2077 PIDGET (ptid));
c906108c
SS
2078}
2079
2080static void
fba45db2 2081debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
2082{
2083 debug_target.to_acknowledge_created_inferior (pid);
2084
96baa820 2085 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
2086 pid);
2087}
2088
c906108c 2089static int
fba45db2 2090debug_to_insert_fork_catchpoint (int pid)
c906108c 2091{
c5aa993b 2092 int retval;
c906108c
SS
2093
2094 retval = debug_target.to_insert_fork_catchpoint (pid);
2095
96baa820 2096 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
c5aa993b 2097 pid, retval);
c906108c
SS
2098
2099 return retval;
2100}
2101
2102static int
fba45db2 2103debug_to_remove_fork_catchpoint (int pid)
c906108c 2104{
c5aa993b 2105 int retval;
c906108c
SS
2106
2107 retval = debug_target.to_remove_fork_catchpoint (pid);
2108
96baa820 2109 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 2110 pid, retval);
c906108c
SS
2111
2112 return retval;
2113}
2114
2115static int
fba45db2 2116debug_to_insert_vfork_catchpoint (int pid)
c906108c 2117{
c5aa993b 2118 int retval;
c906108c
SS
2119
2120 retval = debug_target.to_insert_vfork_catchpoint (pid);
2121
96baa820 2122 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)= %d\n",
c5aa993b 2123 pid, retval);
c906108c
SS
2124
2125 return retval;
2126}
2127
2128static int
fba45db2 2129debug_to_remove_vfork_catchpoint (int pid)
c906108c 2130{
c5aa993b 2131 int retval;
c906108c
SS
2132
2133 retval = debug_target.to_remove_vfork_catchpoint (pid);
2134
96baa820 2135 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 2136 pid, retval);
c906108c
SS
2137
2138 return retval;
2139}
2140
6604731b
DJ
2141static int
2142debug_to_follow_fork (int follow_child)
c906108c 2143{
6604731b 2144 int retval = debug_target.to_follow_fork (follow_child);
c906108c 2145
6604731b
DJ
2146 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2147 follow_child, retval);
2148
2149 return retval;
c906108c
SS
2150}
2151
2152static int
fba45db2 2153debug_to_insert_exec_catchpoint (int pid)
c906108c 2154{
c5aa993b 2155 int retval;
c906108c
SS
2156
2157 retval = debug_target.to_insert_exec_catchpoint (pid);
2158
96baa820 2159 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
c5aa993b 2160 pid, retval);
c906108c
SS
2161
2162 return retval;
2163}
2164
2165static int
fba45db2 2166debug_to_remove_exec_catchpoint (int pid)
c906108c 2167{
c5aa993b 2168 int retval;
c906108c
SS
2169
2170 retval = debug_target.to_remove_exec_catchpoint (pid);
2171
96baa820 2172 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 2173 pid, retval);
c906108c
SS
2174
2175 return retval;
2176}
2177
c906108c 2178static int
fba45db2 2179debug_to_reported_exec_events_per_exec_call (void)
c906108c 2180{
c5aa993b 2181 int reported_exec_events;
c906108c
SS
2182
2183 reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
2184
96baa820 2185 fprintf_unfiltered (gdb_stdlog,
c906108c 2186 "target_reported_exec_events_per_exec_call () = %d\n",
c5aa993b 2187 reported_exec_events);
c906108c
SS
2188
2189 return reported_exec_events;
2190}
2191
c906108c 2192static int
fba45db2 2193debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 2194{
c5aa993b 2195 int has_exited;
c906108c
SS
2196
2197 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2198
96baa820 2199 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 2200 pid, wait_status, *exit_status, has_exited);
c906108c
SS
2201
2202 return has_exited;
2203}
2204
2205static void
fba45db2 2206debug_to_mourn_inferior (void)
c906108c
SS
2207{
2208 debug_target.to_mourn_inferior ();
2209
96baa820 2210 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
c906108c
SS
2211}
2212
2213static int
fba45db2 2214debug_to_can_run (void)
c906108c
SS
2215{
2216 int retval;
2217
2218 retval = debug_target.to_can_run ();
2219
96baa820 2220 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
2221
2222 return retval;
2223}
2224
2225static void
39f77062 2226debug_to_notice_signals (ptid_t ptid)
c906108c 2227{
39f77062 2228 debug_target.to_notice_signals (ptid);
c906108c 2229
39f77062
KB
2230 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
2231 PIDGET (ptid));
c906108c
SS
2232}
2233
2234static int
39f77062 2235debug_to_thread_alive (ptid_t ptid)
c906108c
SS
2236{
2237 int retval;
2238
39f77062 2239 retval = debug_target.to_thread_alive (ptid);
c906108c 2240
96baa820 2241 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
39f77062 2242 PIDGET (ptid), retval);
c906108c
SS
2243
2244 return retval;
2245}
2246
0d06e24b 2247static void
fba45db2 2248debug_to_find_new_threads (void)
0d06e24b
JM
2249{
2250 debug_target.to_find_new_threads ();
2251
2252 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
2253}
2254
c906108c 2255static void
fba45db2 2256debug_to_stop (void)
c906108c
SS
2257{
2258 debug_target.to_stop ();
2259
96baa820 2260 fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
c906108c
SS
2261}
2262
1e3ff5ad 2263static LONGEST
8aa91c1e
AC
2264debug_to_xfer_partial (struct target_ops *ops, enum target_object object,
2265 const char *annex, void *readbuf, const void *writebuf,
2266 ULONGEST offset, LONGEST len)
1e3ff5ad
AC
2267{
2268 LONGEST retval;
2269
4b8a223f 2270 retval = debug_target.to_xfer_partial (&debug_target, object, annex,
8aa91c1e 2271 readbuf, writebuf, offset, len);
1e3ff5ad
AC
2272
2273 fprintf_unfiltered (gdb_stdlog,
4b8a223f 2274 "target_xfer_partial (%d, %s, 0x%lx, 0x%lx, 0x%s, %s) = %s\n",
1e3ff5ad 2275 (int) object, (annex ? annex : "(null)"),
8aa91c1e 2276 (long) readbuf, (long) writebuf, paddr_nz (offset),
1e3ff5ad 2277 paddr_d (len), paddr_d (retval));
c906108c
SS
2278
2279 return retval;
2280}
2281
96baa820
JM
2282static void
2283debug_to_rcmd (char *command,
d9fcf2fb 2284 struct ui_file *outbuf)
96baa820
JM
2285{
2286 debug_target.to_rcmd (command, outbuf);
2287 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
2288}
2289
c906108c 2290static struct symtab_and_line *
fba45db2 2291debug_to_enable_exception_callback (enum exception_event_kind kind, int enable)
c906108c 2292{
7a292a7a
SS
2293 struct symtab_and_line *result;
2294 result = debug_target.to_enable_exception_callback (kind, enable);
96baa820 2295 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2296 "target get_exception_callback_sal (%d, %d)\n",
2297 kind, enable);
7a292a7a 2298 return result;
c906108c
SS
2299}
2300
2301static struct exception_event_record *
fba45db2 2302debug_to_get_current_exception_event (void)
c906108c 2303{
7a292a7a 2304 struct exception_event_record *result;
c5aa993b 2305 result = debug_target.to_get_current_exception_event ();
96baa820 2306 fprintf_unfiltered (gdb_stdlog, "target get_current_exception_event ()\n");
7a292a7a 2307 return result;
c906108c
SS
2308}
2309
2310static char *
fba45db2 2311debug_to_pid_to_exec_file (int pid)
c906108c 2312{
c5aa993b 2313 char *exec_file;
c906108c
SS
2314
2315 exec_file = debug_target.to_pid_to_exec_file (pid);
2316
96baa820 2317 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 2318 pid, exec_file);
c906108c
SS
2319
2320 return exec_file;
2321}
2322
c906108c 2323static void
fba45db2 2324setup_target_debug (void)
c906108c
SS
2325{
2326 memcpy (&debug_target, &current_target, sizeof debug_target);
2327
2328 current_target.to_open = debug_to_open;
2329 current_target.to_close = debug_to_close;
2330 current_target.to_attach = debug_to_attach;
2331 current_target.to_post_attach = debug_to_post_attach;
c906108c 2332 current_target.to_detach = debug_to_detach;
6ad8ae5c 2333 current_target.to_disconnect = debug_to_disconnect;
c906108c
SS
2334 current_target.to_resume = debug_to_resume;
2335 current_target.to_wait = debug_to_wait;
c906108c
SS
2336 current_target.to_fetch_registers = debug_to_fetch_registers;
2337 current_target.to_store_registers = debug_to_store_registers;
2338 current_target.to_prepare_to_store = debug_to_prepare_to_store;
2339 current_target.to_xfer_memory = debug_to_xfer_memory;
2340 current_target.to_files_info = debug_to_files_info;
2341 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2342 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
2343 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
2344 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
2345 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
2346 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
2347 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
2348 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
2349 current_target.to_stopped_data_address = debug_to_stopped_data_address;
2350 current_target.to_region_size_ok_for_hw_watchpoint = debug_to_region_size_ok_for_hw_watchpoint;
c906108c
SS
2351 current_target.to_terminal_init = debug_to_terminal_init;
2352 current_target.to_terminal_inferior = debug_to_terminal_inferior;
2353 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2354 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 2355 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c
SS
2356 current_target.to_terminal_info = debug_to_terminal_info;
2357 current_target.to_kill = debug_to_kill;
2358 current_target.to_load = debug_to_load;
2359 current_target.to_lookup_symbol = debug_to_lookup_symbol;
2360 current_target.to_create_inferior = debug_to_create_inferior;
2361 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2362 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
c906108c
SS
2363 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2364 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2365 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2366 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
6604731b 2367 current_target.to_follow_fork = debug_to_follow_fork;
c906108c
SS
2368 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2369 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 2370 current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
c906108c
SS
2371 current_target.to_has_exited = debug_to_has_exited;
2372 current_target.to_mourn_inferior = debug_to_mourn_inferior;
2373 current_target.to_can_run = debug_to_can_run;
2374 current_target.to_notice_signals = debug_to_notice_signals;
2375 current_target.to_thread_alive = debug_to_thread_alive;
0d06e24b 2376 current_target.to_find_new_threads = debug_to_find_new_threads;
c906108c 2377 current_target.to_stop = debug_to_stop;
4b8a223f 2378 current_target.to_xfer_partial = debug_to_xfer_partial;
96baa820 2379 current_target.to_rcmd = debug_to_rcmd;
c906108c
SS
2380 current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
2381 current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
2382 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c906108c
SS
2383
2384}
c906108c 2385\f
c5aa993b
JM
2386
2387static char targ_desc[] =
2388"Names of targets and files being debugged.\n\
c906108c
SS
2389Shows the entire stack of targets currently in use (including the exec-file,\n\
2390core-file, and process, if any), as well as the symbol file name.";
2391
96baa820
JM
2392static void
2393do_monitor_command (char *cmd,
2394 int from_tty)
2395{
2b5fe715
AC
2396 if ((current_target.to_rcmd
2397 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 2398 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
2399 && (debug_target.to_rcmd
2400 == (void (*) (char *, struct ui_file *)) tcomplain)))
96baa820
JM
2401 {
2402 error ("\"monitor\" command not supported by this target.\n");
2403 }
2404 target_rcmd (cmd, gdb_stdtarg);
2405}
2406
c906108c 2407void
fba45db2 2408initialize_targets (void)
c906108c
SS
2409{
2410 init_dummy_target ();
2411 push_target (&dummy_target);
2412
2413 add_info ("target", target_info, targ_desc);
2414 add_info ("files", target_info, targ_desc);
2415
cb1a6d5f 2416 deprecated_add_show_from_set
3a11626d
MS
2417 (add_set_cmd ("target", class_maintenance, var_zinteger,
2418 (char *) &targetdebug,
2419 "Set target debugging.\n\
333dabeb
DJ
2420When non-zero, target debugging is enabled. Higher numbers are more\n\
2421verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
2422command.", &setdebuglist),
3a11626d
MS
2423 &showdebuglist);
2424
e707bbc2
AC
2425 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
2426 &trust_readonly, "\
3b64bf98
AC
2427Set mode for reading from readonly sections.", "\
2428Show mode for reading from readonly sections.", "\
3a11626d
MS
2429When this mode is on, memory reads from readonly sections (such as .text)\n\
2430will be read from the object file instead of from the target. This will\n\
e707bbc2 2431result in significant performance improvement for remote targets.", "\
3b64bf98 2432Mode for reading from readonly sections is %s.",
e707bbc2
AC
2433 NULL, NULL,
2434 &setlist, &showlist);
96baa820
JM
2435
2436 add_com ("monitor", class_obscure, do_monitor_command,
2437 "Send a command to the remote monitor (remote targets only).");
2438
8add0441 2439 target_dcache = dcache_init ();
c906108c 2440}