119370Spst/* Select target systems and architectures at runtime for GDB.
2130809Smarcel
3130809Smarcel   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4130809Smarcel   1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5130809Smarcel
619370Spst   Contributed by Cygnus Support.
719370Spst
899002Sobrien   This file is part of GDB.
919370Spst
1099002Sobrien   This program is free software; you can redistribute it and/or modify
1199002Sobrien   it under the terms of the GNU General Public License as published by
1299002Sobrien   the Free Software Foundation; either version 2 of the License, or
1399002Sobrien   (at your option) any later version.
1419370Spst
1599002Sobrien   This program is distributed in the hope that it will be useful,
1699002Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1799002Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1899002Sobrien   GNU General Public License for more details.
1919370Spst
2099002Sobrien   You should have received a copy of the GNU General Public License
2199002Sobrien   along with this program; if not, write to the Free Software
2299002Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2399002Sobrien   Boston, MA 02111-1307, USA.  */
2419370Spst
2519370Spst#include "defs.h"
2619370Spst#include <errno.h>
2719370Spst#include "gdb_string.h"
2819370Spst#include "target.h"
2919370Spst#include "gdbcmd.h"
3019370Spst#include "symtab.h"
3119370Spst#include "inferior.h"
3219370Spst#include "bfd.h"
3319370Spst#include "symfile.h"
3419370Spst#include "objfiles.h"
3599002Sobrien#include "gdb_wait.h"
3699002Sobrien#include "dcache.h"
3719370Spst#include <signal.h>
3899002Sobrien#include "regcache.h"
39130809Smarcel#include "gdb_assert.h"
40130809Smarcel#include "gdbcore.h"
4119370Spst
4299002Sobrienstatic void target_info (char *, int);
4319370Spst
4499002Sobrienstatic void maybe_kill_then_create_inferior (char *, char *, char **);
4519370Spst
4699002Sobrienstatic void maybe_kill_then_attach (char *, int);
4719370Spst
4899002Sobrienstatic void kill_or_be_killed (int);
4919370Spst
5099002Sobrienstatic void default_terminal_info (char *, int);
5119370Spst
52130809Smarcelstatic int default_region_size_ok_for_hw_watchpoint (int);
53130809Smarcel
5499002Sobrienstatic int nosymbol (char *, CORE_ADDR *);
5519370Spst
5699002Sobrienstatic void tcomplain (void);
5719370Spst
5899002Sobrienstatic int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
5919370Spst
6099002Sobrienstatic int return_zero (void);
6119370Spst
6299002Sobrienstatic int return_one (void);
6319370Spst
64130809Smarcelstatic int return_minus_one (void);
65130809Smarcel
6699002Sobrienvoid target_ignore (void);
6746289Sdfr
6899002Sobrienstatic void target_command (char *, int);
6919370Spst
7099002Sobrienstatic struct target_ops *find_default_run_target (char *);
7119370Spst
7299002Sobrienstatic void nosupport_runtime (void);
7346289Sdfr
74130809Smarcelstatic LONGEST default_xfer_partial (struct target_ops *ops,
75130809Smarcel				     enum target_object object,
76130809Smarcel				     const char *annex, void *readbuf,
77130809Smarcel				     const void *writebuf,
78130809Smarcel				     ULONGEST offset, LONGEST len);
7999002Sobrien
8099002Sobrien/* Transfer LEN bytes between target address MEMADDR and GDB address
8199002Sobrien   MYADDR.  Returns 0 for success, errno code for failure (which
8299002Sobrien   includes partial transfers -- if you want a more useful response to
8399002Sobrien   partial transfers, try either target_read_memory_partial or
8499002Sobrien   target_write_memory_partial).  */
8599002Sobrien
86130809Smarcelstatic int target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
87130809Smarcel			       int write);
8846289Sdfr
8999002Sobrienstatic void init_dummy_target (void);
9046289Sdfr
9199002Sobrienstatic void debug_to_open (char *, int);
9246289Sdfr
9399002Sobrienstatic void debug_to_close (int);
9446289Sdfr
9599002Sobrienstatic void debug_to_attach (char *, int);
9646289Sdfr
9799002Sobrienstatic void debug_to_detach (char *, int);
9846289Sdfr
99130809Smarcelstatic void debug_to_disconnect (char *, int);
100130809Smarcel
10199002Sobrienstatic void debug_to_resume (ptid_t, int, enum target_signal);
10246289Sdfr
10399002Sobrienstatic ptid_t debug_to_wait (ptid_t, struct target_waitstatus *);
10446289Sdfr
10599002Sobrienstatic void debug_to_fetch_registers (int);
10646289Sdfr
10799002Sobrienstatic void debug_to_store_registers (int);
10846289Sdfr
10999002Sobrienstatic void debug_to_prepare_to_store (void);
11046289Sdfr
111130809Smarcelstatic int debug_to_xfer_memory (CORE_ADDR, char *, int, int,
112130809Smarcel				 struct mem_attrib *, struct target_ops *);
11346289Sdfr
11499002Sobrienstatic void debug_to_files_info (struct target_ops *);
11546289Sdfr
11699002Sobrienstatic int debug_to_insert_breakpoint (CORE_ADDR, char *);
11746289Sdfr
11899002Sobrienstatic int debug_to_remove_breakpoint (CORE_ADDR, char *);
11946289Sdfr
120130809Smarcelstatic int debug_to_can_use_hw_breakpoint (int, int, int);
121130809Smarcel
122130809Smarcelstatic int debug_to_insert_hw_breakpoint (CORE_ADDR, char *);
123130809Smarcel
124130809Smarcelstatic int debug_to_remove_hw_breakpoint (CORE_ADDR, char *);
125130809Smarcel
126130809Smarcelstatic int debug_to_insert_watchpoint (CORE_ADDR, int, int);
127130809Smarcel
128130809Smarcelstatic int debug_to_remove_watchpoint (CORE_ADDR, int, int);
129130809Smarcel
130130809Smarcelstatic int debug_to_stopped_by_watchpoint (void);
131130809Smarcel
132130809Smarcelstatic CORE_ADDR debug_to_stopped_data_address (void);
133130809Smarcel
134130809Smarcelstatic int debug_to_region_size_ok_for_hw_watchpoint (int);
135130809Smarcel
13699002Sobrienstatic void debug_to_terminal_init (void);
13746289Sdfr
13899002Sobrienstatic void debug_to_terminal_inferior (void);
13946289Sdfr
14099002Sobrienstatic void debug_to_terminal_ours_for_output (void);
14146289Sdfr
142130809Smarcelstatic void debug_to_terminal_save_ours (void);
143130809Smarcel
14499002Sobrienstatic void debug_to_terminal_ours (void);
14546289Sdfr
14699002Sobrienstatic void debug_to_terminal_info (char *, int);
14746289Sdfr
14899002Sobrienstatic void debug_to_kill (void);
14946289Sdfr
15099002Sobrienstatic void debug_to_load (char *, int);
15146289Sdfr
15299002Sobrienstatic int debug_to_lookup_symbol (char *, CORE_ADDR *);
15346289Sdfr
15499002Sobrienstatic void debug_to_create_inferior (char *, char *, char **);
15546289Sdfr
15699002Sobrienstatic void debug_to_mourn_inferior (void);
15746289Sdfr
15899002Sobrienstatic int debug_to_can_run (void);
15946289Sdfr
16099002Sobrienstatic void debug_to_notice_signals (ptid_t);
16146289Sdfr
16299002Sobrienstatic int debug_to_thread_alive (ptid_t);
16346289Sdfr
16499002Sobrienstatic void debug_to_stop (void);
16546289Sdfr
16619370Spst/* Pointer to array of target architecture structures; the size of the
16719370Spst   array; the current index into the array; the allocated size of the
16819370Spst   array.  */
16919370Spststruct target_ops **target_structs;
17019370Spstunsigned target_struct_size;
17119370Spstunsigned target_struct_index;
17219370Spstunsigned target_struct_allocsize;
17319370Spst#define	DEFAULT_ALLOCSIZE	10
17419370Spst
17519370Spst/* The initial current target, so that there is always a semi-valid
17619370Spst   current target.  */
17719370Spst
17846289Sdfrstatic struct target_ops dummy_target;
17919370Spst
18019370Spst/* Top of target stack.  */
18119370Spst
182130809Smarcelstatic struct target_ops *target_stack;
18319370Spst
18419370Spst/* The target structure we are currently using to talk to a process
18519370Spst   or file or whatever "inferior" we have.  */
18619370Spst
18719370Spststruct target_ops current_target;
18819370Spst
18919370Spst/* Command list for target.  */
19019370Spst
19119370Spststatic struct cmd_list_element *targetlist = NULL;
19219370Spst
19319370Spst/* Nonzero if we are debugging an attached outside process
19419370Spst   rather than an inferior.  */
19519370Spst
19619370Spstint attach_flag;
19719370Spst
19819370Spst/* Non-zero if we want to see trace of target level stuff.  */
19919370Spst
20019370Spststatic int targetdebug = 0;
20119370Spst
20299002Sobrienstatic void setup_target_debug (void);
20319370Spst
20499002SobrienDCACHE *target_dcache;
20519370Spst
20619370Spst/* The user just typed 'target' without the name of a target.  */
20719370Spst
20819370Spststatic void
20999002Sobrientarget_command (char *arg, int from_tty)
21019370Spst{
21119370Spst  fputs_filtered ("Argument required (target name).  Try `help target'\n",
21219370Spst		  gdb_stdout);
21319370Spst}
21419370Spst
21519370Spst/* Add a possible target architecture to the list.  */
21619370Spst
21719370Spstvoid
21899002Sobrienadd_target (struct target_ops *t)
21919370Spst{
220130809Smarcel  /* Provide default values for all "must have" methods.  */
221130809Smarcel  if (t->to_xfer_partial == NULL)
222130809Smarcel    t->to_xfer_partial = default_xfer_partial;
223130809Smarcel
22419370Spst  if (!target_structs)
22519370Spst    {
22619370Spst      target_struct_allocsize = DEFAULT_ALLOCSIZE;
22719370Spst      target_structs = (struct target_ops **) xmalloc
22819370Spst	(target_struct_allocsize * sizeof (*target_structs));
22919370Spst    }
23019370Spst  if (target_struct_size >= target_struct_allocsize)
23119370Spst    {
23219370Spst      target_struct_allocsize *= 2;
23319370Spst      target_structs = (struct target_ops **)
23499002Sobrien	xrealloc ((char *) target_structs,
23599002Sobrien		  target_struct_allocsize * sizeof (*target_structs));
23619370Spst    }
23719370Spst  target_structs[target_struct_size++] = t;
23819370Spst
23919370Spst  if (targetlist == NULL)
24019370Spst    add_prefix_cmd ("target", class_run, target_command,
24119370Spst		    "Connect to a target machine or process.\n\
24219370SpstThe first argument is the type or protocol of the target machine.\n\
24319370SpstRemaining arguments are interpreted by the target protocol.  For more\n\
24419370Spstinformation on the arguments for a particular protocol, type\n\
24519370Spst`help target ' followed by the protocol name.",
24619370Spst		    &targetlist, "target ", 0, &cmdlist);
24719370Spst  add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
24819370Spst}
24919370Spst
25019370Spst/* Stub functions */
25119370Spst
25246289Sdfrvoid
25399002Sobrientarget_ignore (void)
25419370Spst{
25519370Spst}
25619370Spst
25799002Sobrienvoid
25899002Sobrientarget_load (char *arg, int from_tty)
25999002Sobrien{
26099002Sobrien  dcache_invalidate (target_dcache);
26199002Sobrien  (*current_target.to_load) (arg, from_tty);
26299002Sobrien}
26399002Sobrien
26419370Spststatic int
26599002Sobriennomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
26699002Sobrien	  struct target_ops *t)
26719370Spst{
26899002Sobrien  errno = EIO;			/* Can't read/write this location */
26999002Sobrien  return 0;			/* No bytes handled */
27019370Spst}
27119370Spst
27219370Spststatic void
27399002Sobrientcomplain (void)
27419370Spst{
27519370Spst  error ("You can't do that when your target is `%s'",
27619370Spst	 current_target.to_shortname);
27719370Spst}
27819370Spst
27919370Spstvoid
28099002Sobriennoprocess (void)
28119370Spst{
28246289Sdfr  error ("You can't do that without a process to debug.");
28319370Spst}
28419370Spst
28519370Spststatic int
28699002Sobriennosymbol (char *name, CORE_ADDR *addrp)
28719370Spst{
28899002Sobrien  return 1;			/* Symbol does not exist in target env */
28919370Spst}
29019370Spst
29199002Sobrienstatic void
29299002Sobriennosupport_runtime (void)
29346289Sdfr{
29499002Sobrien  if (ptid_equal (inferior_ptid, null_ptid))
29546289Sdfr    noprocess ();
29646289Sdfr  else
29746289Sdfr    error ("No run-time support for this");
29846289Sdfr}
29946289Sdfr
30046289Sdfr
30119370Spststatic void
30299002Sobriendefault_terminal_info (char *args, int from_tty)
30319370Spst{
30499002Sobrien  printf_unfiltered ("No saved terminal information.\n");
30519370Spst}
30619370Spst
30719370Spst/* This is the default target_create_inferior and target_attach function.
30819370Spst   If the current target is executing, it asks whether to kill it off.
30919370Spst   If this function returns without calling error(), it has killed off
31019370Spst   the target, and the operation should be attempted.  */
31119370Spst
31219370Spststatic void
31399002Sobrienkill_or_be_killed (int from_tty)
31419370Spst{
31519370Spst  if (target_has_execution)
31619370Spst    {
31719370Spst      printf_unfiltered ("You are already running a program:\n");
31819370Spst      target_files_info ();
31999002Sobrien      if (query ("Kill it? "))
32099002Sobrien	{
32199002Sobrien	  target_kill ();
32299002Sobrien	  if (target_has_execution)
32399002Sobrien	    error ("Killing the program did not help.");
32499002Sobrien	  return;
32599002Sobrien	}
32699002Sobrien      else
32799002Sobrien	{
32899002Sobrien	  error ("Program not killed.");
32999002Sobrien	}
33019370Spst    }
33199002Sobrien  tcomplain ();
33219370Spst}
33319370Spst
33419370Spststatic void
33599002Sobrienmaybe_kill_then_attach (char *args, int from_tty)
33619370Spst{
33719370Spst  kill_or_be_killed (from_tty);
33819370Spst  target_attach (args, from_tty);
33919370Spst}
34019370Spst
34119370Spststatic void
34299002Sobrienmaybe_kill_then_create_inferior (char *exec, char *args, char **env)
34319370Spst{
34419370Spst  kill_or_be_killed (0);
34519370Spst  target_create_inferior (exec, args, env);
34619370Spst}
34719370Spst
348130809Smarcel/* Go through the target stack from top to bottom, copying over zero
349130809Smarcel   entries in current_target, then filling in still empty entries.  In
350130809Smarcel   effect, we are doing class inheritance through the pushed target
351130809Smarcel   vectors.
35246289Sdfr
353130809Smarcel   NOTE: cagney/2003-10-17: The problem with this inheritance, as it
354130809Smarcel   is currently implemented, is that it discards any knowledge of
355130809Smarcel   which target an inherited method originally belonged to.
356130809Smarcel   Consequently, new new target methods should instead explicitly and
357130809Smarcel   locally search the target stack for the target that can handle the
358130809Smarcel   request.  */
35919370Spst
36019370Spststatic void
361130809Smarcelupdate_current_target (void)
36219370Spst{
363130809Smarcel  struct target_ops *t;
36419370Spst
365130809Smarcel  /* First, reset curren'ts contents.  */
366130809Smarcel  memset (&current_target, 0, sizeof (current_target));
367130809Smarcel
368130809Smarcel#define INHERIT(FIELD, TARGET) \
369130809Smarcel      if (!current_target.FIELD) \
370130809Smarcel	current_target.FIELD = (TARGET)->FIELD
371130809Smarcel
372130809Smarcel  for (t = target_stack; t; t = t->beneath)
373130809Smarcel    {
374130809Smarcel      INHERIT (to_shortname, t);
375130809Smarcel      INHERIT (to_longname, t);
376130809Smarcel      INHERIT (to_doc, t);
377130809Smarcel      INHERIT (to_open, t);
378130809Smarcel      INHERIT (to_close, t);
379130809Smarcel      INHERIT (to_attach, t);
380130809Smarcel      INHERIT (to_post_attach, t);
381130809Smarcel      INHERIT (to_detach, t);
382130809Smarcel      INHERIT (to_disconnect, t);
383130809Smarcel      INHERIT (to_resume, t);
384130809Smarcel      INHERIT (to_wait, t);
385130809Smarcel      INHERIT (to_post_wait, t);
386130809Smarcel      INHERIT (to_fetch_registers, t);
387130809Smarcel      INHERIT (to_store_registers, t);
388130809Smarcel      INHERIT (to_prepare_to_store, t);
389130809Smarcel      INHERIT (to_xfer_memory, t);
390130809Smarcel      INHERIT (to_files_info, t);
391130809Smarcel      INHERIT (to_insert_breakpoint, t);
392130809Smarcel      INHERIT (to_remove_breakpoint, t);
393130809Smarcel      INHERIT (to_can_use_hw_breakpoint, t);
394130809Smarcel      INHERIT (to_insert_hw_breakpoint, t);
395130809Smarcel      INHERIT (to_remove_hw_breakpoint, t);
396130809Smarcel      INHERIT (to_insert_watchpoint, t);
397130809Smarcel      INHERIT (to_remove_watchpoint, t);
398130809Smarcel      INHERIT (to_stopped_data_address, t);
399130809Smarcel      INHERIT (to_stopped_by_watchpoint, t);
400130809Smarcel      INHERIT (to_have_continuable_watchpoint, t);
401130809Smarcel      INHERIT (to_region_size_ok_for_hw_watchpoint, t);
402130809Smarcel      INHERIT (to_terminal_init, t);
403130809Smarcel      INHERIT (to_terminal_inferior, t);
404130809Smarcel      INHERIT (to_terminal_ours_for_output, t);
405130809Smarcel      INHERIT (to_terminal_ours, t);
406130809Smarcel      INHERIT (to_terminal_save_ours, t);
407130809Smarcel      INHERIT (to_terminal_info, t);
408130809Smarcel      INHERIT (to_kill, t);
409130809Smarcel      INHERIT (to_load, t);
410130809Smarcel      INHERIT (to_lookup_symbol, t);
411130809Smarcel      INHERIT (to_create_inferior, t);
412130809Smarcel      INHERIT (to_post_startup_inferior, t);
413130809Smarcel      INHERIT (to_acknowledge_created_inferior, t);
414130809Smarcel      INHERIT (to_insert_fork_catchpoint, t);
415130809Smarcel      INHERIT (to_remove_fork_catchpoint, t);
416130809Smarcel      INHERIT (to_insert_vfork_catchpoint, t);
417130809Smarcel      INHERIT (to_remove_vfork_catchpoint, t);
418130809Smarcel      INHERIT (to_follow_fork, t);
419130809Smarcel      INHERIT (to_insert_exec_catchpoint, t);
420130809Smarcel      INHERIT (to_remove_exec_catchpoint, t);
421130809Smarcel      INHERIT (to_reported_exec_events_per_exec_call, t);
422130809Smarcel      INHERIT (to_has_exited, t);
423130809Smarcel      INHERIT (to_mourn_inferior, t);
424130809Smarcel      INHERIT (to_can_run, t);
425130809Smarcel      INHERIT (to_notice_signals, t);
426130809Smarcel      INHERIT (to_thread_alive, t);
427130809Smarcel      INHERIT (to_find_new_threads, t);
428130809Smarcel      INHERIT (to_pid_to_str, t);
429130809Smarcel      INHERIT (to_extra_thread_info, t);
430130809Smarcel      INHERIT (to_stop, t);
431130809Smarcel      /* Do not inherit to_xfer_partial.  */
432130809Smarcel      INHERIT (to_rcmd, t);
433130809Smarcel      INHERIT (to_enable_exception_callback, t);
434130809Smarcel      INHERIT (to_get_current_exception_event, t);
435130809Smarcel      INHERIT (to_pid_to_exec_file, t);
436130809Smarcel      INHERIT (to_stratum, t);
437130809Smarcel      INHERIT (to_has_all_memory, t);
438130809Smarcel      INHERIT (to_has_memory, t);
439130809Smarcel      INHERIT (to_has_stack, t);
440130809Smarcel      INHERIT (to_has_registers, t);
441130809Smarcel      INHERIT (to_has_execution, t);
442130809Smarcel      INHERIT (to_has_thread_control, t);
443130809Smarcel      INHERIT (to_sections, t);
444130809Smarcel      INHERIT (to_sections_end, t);
445130809Smarcel      INHERIT (to_can_async_p, t);
446130809Smarcel      INHERIT (to_is_async_p, t);
447130809Smarcel      INHERIT (to_async, t);
448130809Smarcel      INHERIT (to_async_mask_value, t);
449130809Smarcel      INHERIT (to_find_memory_regions, t);
450130809Smarcel      INHERIT (to_make_corefile_notes, t);
451130809Smarcel      INHERIT (to_get_thread_local_address, t);
452130809Smarcel      INHERIT (to_magic, t);
453130809Smarcel    }
454130809Smarcel#undef INHERIT
455130809Smarcel
456130809Smarcel  /* Clean up a target struct so it no longer has any zero pointers in
457130809Smarcel     it.  Some entries are defaulted to a method that print an error,
458130809Smarcel     others are hard-wired to a standard recursive default.  */
459130809Smarcel
46019370Spst#define de_fault(field, value) \
461130809Smarcel  if (!current_target.field)               \
462130809Smarcel    current_target.field = value
46319370Spst
46499002Sobrien  de_fault (to_open,
46599002Sobrien	    (void (*) (char *, int))
46699002Sobrien	    tcomplain);
46799002Sobrien  de_fault (to_close,
46899002Sobrien	    (void (*) (int))
46999002Sobrien	    target_ignore);
47099002Sobrien  de_fault (to_attach,
47199002Sobrien	    maybe_kill_then_attach);
47299002Sobrien  de_fault (to_post_attach,
47399002Sobrien	    (void (*) (int))
47499002Sobrien	    target_ignore);
47599002Sobrien  de_fault (to_detach,
47699002Sobrien	    (void (*) (char *, int))
47799002Sobrien	    target_ignore);
478130809Smarcel  de_fault (to_disconnect,
479130809Smarcel	    (void (*) (char *, int))
480130809Smarcel	    tcomplain);
48199002Sobrien  de_fault (to_resume,
48299002Sobrien	    (void (*) (ptid_t, int, enum target_signal))
48399002Sobrien	    noprocess);
48499002Sobrien  de_fault (to_wait,
48599002Sobrien	    (ptid_t (*) (ptid_t, struct target_waitstatus *))
48699002Sobrien	    noprocess);
48799002Sobrien  de_fault (to_post_wait,
48899002Sobrien	    (void (*) (ptid_t, int))
48999002Sobrien	    target_ignore);
49099002Sobrien  de_fault (to_fetch_registers,
49199002Sobrien	    (void (*) (int))
49299002Sobrien	    target_ignore);
49399002Sobrien  de_fault (to_store_registers,
49499002Sobrien	    (void (*) (int))
49599002Sobrien	    noprocess);
49699002Sobrien  de_fault (to_prepare_to_store,
49799002Sobrien	    (void (*) (void))
49899002Sobrien	    noprocess);
49999002Sobrien  de_fault (to_xfer_memory,
50099002Sobrien	    (int (*) (CORE_ADDR, char *, int, int, struct mem_attrib *, struct target_ops *))
50199002Sobrien	    nomemory);
50299002Sobrien  de_fault (to_files_info,
50399002Sobrien	    (void (*) (struct target_ops *))
50499002Sobrien	    target_ignore);
50599002Sobrien  de_fault (to_insert_breakpoint,
50699002Sobrien	    memory_insert_breakpoint);
50799002Sobrien  de_fault (to_remove_breakpoint,
50899002Sobrien	    memory_remove_breakpoint);
509130809Smarcel  de_fault (to_can_use_hw_breakpoint,
510130809Smarcel	    (int (*) (int, int, int))
511130809Smarcel	    return_zero);
512130809Smarcel  de_fault (to_insert_hw_breakpoint,
513130809Smarcel	    (int (*) (CORE_ADDR, char *))
514130809Smarcel	    return_minus_one);
515130809Smarcel  de_fault (to_remove_hw_breakpoint,
516130809Smarcel	    (int (*) (CORE_ADDR, char *))
517130809Smarcel	    return_minus_one);
518130809Smarcel  de_fault (to_insert_watchpoint,
519130809Smarcel	    (int (*) (CORE_ADDR, int, int))
520130809Smarcel	    return_minus_one);
521130809Smarcel  de_fault (to_remove_watchpoint,
522130809Smarcel	    (int (*) (CORE_ADDR, int, int))
523130809Smarcel	    return_minus_one);
524130809Smarcel  de_fault (to_stopped_by_watchpoint,
525130809Smarcel	    (int (*) (void))
526130809Smarcel	    return_zero);
527130809Smarcel  de_fault (to_stopped_data_address,
528130809Smarcel	    (CORE_ADDR (*) (void))
529130809Smarcel	    return_zero);
530130809Smarcel  de_fault (to_region_size_ok_for_hw_watchpoint,
531130809Smarcel	    default_region_size_ok_for_hw_watchpoint);
53299002Sobrien  de_fault (to_terminal_init,
53399002Sobrien	    (void (*) (void))
53499002Sobrien	    target_ignore);
53599002Sobrien  de_fault (to_terminal_inferior,
53699002Sobrien	    (void (*) (void))
53799002Sobrien	    target_ignore);
53899002Sobrien  de_fault (to_terminal_ours_for_output,
53999002Sobrien	    (void (*) (void))
54099002Sobrien	    target_ignore);
54199002Sobrien  de_fault (to_terminal_ours,
54299002Sobrien	    (void (*) (void))
54399002Sobrien	    target_ignore);
544130809Smarcel  de_fault (to_terminal_save_ours,
545130809Smarcel	    (void (*) (void))
546130809Smarcel	    target_ignore);
54799002Sobrien  de_fault (to_terminal_info,
54899002Sobrien	    default_terminal_info);
54999002Sobrien  de_fault (to_kill,
55099002Sobrien	    (void (*) (void))
55199002Sobrien	    noprocess);
55299002Sobrien  de_fault (to_load,
55399002Sobrien	    (void (*) (char *, int))
55499002Sobrien	    tcomplain);
55599002Sobrien  de_fault (to_lookup_symbol,
55699002Sobrien	    (int (*) (char *, CORE_ADDR *))
55799002Sobrien	    nosymbol);
55899002Sobrien  de_fault (to_create_inferior,
55999002Sobrien	    maybe_kill_then_create_inferior);
56099002Sobrien  de_fault (to_post_startup_inferior,
56199002Sobrien	    (void (*) (ptid_t))
56299002Sobrien	    target_ignore);
56399002Sobrien  de_fault (to_acknowledge_created_inferior,
56499002Sobrien	    (void (*) (int))
56599002Sobrien	    target_ignore);
56699002Sobrien  de_fault (to_insert_fork_catchpoint,
56799002Sobrien	    (int (*) (int))
56899002Sobrien	    tcomplain);
56999002Sobrien  de_fault (to_remove_fork_catchpoint,
57099002Sobrien	    (int (*) (int))
57199002Sobrien	    tcomplain);
57299002Sobrien  de_fault (to_insert_vfork_catchpoint,
57399002Sobrien	    (int (*) (int))
57499002Sobrien	    tcomplain);
57599002Sobrien  de_fault (to_remove_vfork_catchpoint,
57699002Sobrien	    (int (*) (int))
57799002Sobrien	    tcomplain);
578130809Smarcel  de_fault (to_follow_fork,
579130809Smarcel	    (int (*) (int))
58099002Sobrien	    target_ignore);
58199002Sobrien  de_fault (to_insert_exec_catchpoint,
58299002Sobrien	    (int (*) (int))
58399002Sobrien	    tcomplain);
58499002Sobrien  de_fault (to_remove_exec_catchpoint,
58599002Sobrien	    (int (*) (int))
58699002Sobrien	    tcomplain);
58799002Sobrien  de_fault (to_reported_exec_events_per_exec_call,
58899002Sobrien	    (int (*) (void))
58999002Sobrien	    return_one);
59099002Sobrien  de_fault (to_has_exited,
59199002Sobrien	    (int (*) (int, int, int *))
59299002Sobrien	    return_zero);
59399002Sobrien  de_fault (to_mourn_inferior,
59499002Sobrien	    (void (*) (void))
59599002Sobrien	    noprocess);
59699002Sobrien  de_fault (to_can_run,
59799002Sobrien	    return_zero);
59899002Sobrien  de_fault (to_notice_signals,
59999002Sobrien	    (void (*) (ptid_t))
60099002Sobrien	    target_ignore);
60199002Sobrien  de_fault (to_thread_alive,
60299002Sobrien	    (int (*) (ptid_t))
60399002Sobrien	    return_zero);
60499002Sobrien  de_fault (to_find_new_threads,
60599002Sobrien	    (void (*) (void))
60699002Sobrien	    target_ignore);
60799002Sobrien  de_fault (to_extra_thread_info,
60899002Sobrien	    (char *(*) (struct thread_info *))
60999002Sobrien	    return_zero);
61099002Sobrien  de_fault (to_stop,
61199002Sobrien	    (void (*) (void))
61299002Sobrien	    target_ignore);
613130809Smarcel  current_target.to_xfer_partial = default_xfer_partial;
61499002Sobrien  de_fault (to_rcmd,
61599002Sobrien	    (void (*) (char *, struct ui_file *))
61699002Sobrien	    tcomplain);
61799002Sobrien  de_fault (to_enable_exception_callback,
61899002Sobrien	    (struct symtab_and_line * (*) (enum exception_event_kind, int))
61999002Sobrien	    nosupport_runtime);
62099002Sobrien  de_fault (to_get_current_exception_event,
62199002Sobrien	    (struct exception_event_record * (*) (void))
62299002Sobrien	    nosupport_runtime);
62399002Sobrien  de_fault (to_pid_to_exec_file,
62499002Sobrien	    (char *(*) (int))
62599002Sobrien	    return_zero);
62699002Sobrien  de_fault (to_can_async_p,
62799002Sobrien	    (int (*) (void))
62899002Sobrien	    return_zero);
62999002Sobrien  de_fault (to_is_async_p,
63099002Sobrien	    (int (*) (void))
63199002Sobrien	    return_zero);
63299002Sobrien  de_fault (to_async,
63399002Sobrien	    (void (*) (void (*) (enum inferior_event_type, void*), void*))
63499002Sobrien	    tcomplain);
63519370Spst#undef de_fault
63619370Spst
637130809Smarcel  /* Finally, position the target-stack beneath the squashed
638130809Smarcel     "current_target".  That way code looking for a non-inherited
639130809Smarcel     target method can quickly and simply find it.  */
640130809Smarcel  current_target.beneath = target_stack;
64119370Spst}
64219370Spst
64319370Spst/* Push a new target type into the stack of the existing target accessors,
64419370Spst   possibly superseding some of the existing accessors.
64519370Spst
64619370Spst   Result is zero if the pushed target ended up on top of the stack,
64719370Spst   nonzero if at least one target is on top of it.
64819370Spst
64919370Spst   Rather than allow an empty stack, we always have the dummy target at
65019370Spst   the bottom stratum, so we can call the function vectors without
65119370Spst   checking them.  */
65219370Spst
65319370Spstint
65499002Sobrienpush_target (struct target_ops *t)
65519370Spst{
656130809Smarcel  struct target_ops **cur;
65719370Spst
65819370Spst  /* Check magic number.  If wrong, it probably means someone changed
65919370Spst     the struct definition, but not all the places that initialize one.  */
66019370Spst  if (t->to_magic != OPS_MAGIC)
66119370Spst    {
66299002Sobrien      fprintf_unfiltered (gdb_stderr,
66399002Sobrien			  "Magic number of %s target struct wrong\n",
66499002Sobrien			  t->to_shortname);
66599002Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
66619370Spst    }
66719370Spst
668130809Smarcel  /* Find the proper stratum to install this target in.  */
669130809Smarcel  for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
67019370Spst    {
671130809Smarcel      if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
67219370Spst	break;
67319370Spst    }
67419370Spst
675130809Smarcel  /* If there's already targets at this stratum, remove them.  */
676130809Smarcel  /* FIXME: cagney/2003-10-15: I think this should be poping all
677130809Smarcel     targets to CUR, and not just those at this stratum level.  */
678130809Smarcel  while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
679130809Smarcel    {
680130809Smarcel      /* There's already something at this stratum level.  Close it,
681130809Smarcel         and un-hook it from the stack.  */
682130809Smarcel      struct target_ops *tmp = (*cur);
683130809Smarcel      (*cur) = (*cur)->beneath;
684130809Smarcel      tmp->beneath = NULL;
685130809Smarcel      target_close (tmp, 0);
686130809Smarcel    }
68719370Spst
68819370Spst  /* We have removed all targets in our stratum, now add the new one.  */
689130809Smarcel  t->beneath = (*cur);
690130809Smarcel  (*cur) = t;
69119370Spst
69219370Spst  update_current_target ();
69319370Spst
69419370Spst  if (targetdebug)
69519370Spst    setup_target_debug ();
69619370Spst
697130809Smarcel  /* Not on top?  */
698130809Smarcel  return (t != target_stack);
69919370Spst}
70019370Spst
70119370Spst/* Remove a target_ops vector from the stack, wherever it may be.
70219370Spst   Return how many times it was removed (0 or 1).  */
70319370Spst
70419370Spstint
70599002Sobrienunpush_target (struct target_ops *t)
70619370Spst{
707130809Smarcel  struct target_ops **cur;
708130809Smarcel  struct target_ops *tmp;
70919370Spst
71019370Spst  /* Look for the specified target.  Note that we assume that a target
71119370Spst     can only occur once in the target stack. */
71219370Spst
713130809Smarcel  for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
714130809Smarcel    {
715130809Smarcel      if ((*cur) == t)
716130809Smarcel	break;
717130809Smarcel    }
71819370Spst
719130809Smarcel  if ((*cur) == NULL)
72019370Spst    return 0;			/* Didn't find target_ops, quit now */
72119370Spst
722130809Smarcel  /* NOTE: cagney/2003-12-06: In '94 the close call was made
723130809Smarcel     unconditional by moving it to before the above check that the
724130809Smarcel     target was in the target stack (something about "Change the way
725130809Smarcel     pushing and popping of targets work to support target overlays
726130809Smarcel     and inheritance").  This doesn't make much sense - only open
727130809Smarcel     targets should be closed.  */
728130809Smarcel  target_close (t, 0);
729130809Smarcel
73019370Spst  /* Unchain the target */
731130809Smarcel  tmp = (*cur);
732130809Smarcel  (*cur) = (*cur)->beneath;
733130809Smarcel  tmp->beneath = NULL;
73419370Spst
73519370Spst  update_current_target ();
73619370Spst
73719370Spst  return 1;
73819370Spst}
73919370Spst
74019370Spstvoid
74199002Sobrienpop_target (void)
74219370Spst{
743130809Smarcel  target_close (&current_target, 0);	/* Let it clean up */
744130809Smarcel  if (unpush_target (target_stack) == 1)
74519370Spst    return;
74619370Spst
74799002Sobrien  fprintf_unfiltered (gdb_stderr,
74899002Sobrien		      "pop_target couldn't find target %s\n",
74999002Sobrien		      current_target.to_shortname);
75099002Sobrien  internal_error (__FILE__, __LINE__, "failed internal consistency check");
75119370Spst}
75219370Spst
75319370Spst#undef	MIN
75419370Spst#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
75519370Spst
75619370Spst/* target_read_string -- read a null terminated string, up to LEN bytes,
75719370Spst   from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
75819370Spst   Set *STRING to a pointer to malloc'd memory containing the data; the caller
75919370Spst   is responsible for freeing it.  Return the number of bytes successfully
76019370Spst   read.  */
76119370Spst
76219370Spstint
76399002Sobrientarget_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
76419370Spst{
76519370Spst  int tlen, origlen, offset, i;
76619370Spst  char buf[4];
76719370Spst  int errcode = 0;
76819370Spst  char *buffer;
76919370Spst  int buffer_allocated;
77019370Spst  char *bufptr;
77119370Spst  unsigned int nbytes_read = 0;
77219370Spst
77319370Spst  /* Small for testing.  */
77419370Spst  buffer_allocated = 4;
77519370Spst  buffer = xmalloc (buffer_allocated);
77619370Spst  bufptr = buffer;
77719370Spst
77819370Spst  origlen = len;
77919370Spst
78019370Spst  while (len > 0)
78119370Spst    {
78219370Spst      tlen = MIN (len, 4 - (memaddr & 3));
78319370Spst      offset = memaddr & 3;
78419370Spst
78599002Sobrien      errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
78619370Spst      if (errcode != 0)
78746289Sdfr	{
78846289Sdfr	  /* The transfer request might have crossed the boundary to an
78946289Sdfr	     unallocated region of memory. Retry the transfer, requesting
79046289Sdfr	     a single byte.  */
79146289Sdfr	  tlen = 1;
79246289Sdfr	  offset = 0;
79399002Sobrien	  errcode = target_xfer_memory (memaddr, buf, 1, 0);
79446289Sdfr	  if (errcode != 0)
79546289Sdfr	    goto done;
79646289Sdfr	}
79719370Spst
79819370Spst      if (bufptr - buffer + tlen > buffer_allocated)
79919370Spst	{
80019370Spst	  unsigned int bytes;
80119370Spst	  bytes = bufptr - buffer;
80219370Spst	  buffer_allocated *= 2;
80319370Spst	  buffer = xrealloc (buffer, buffer_allocated);
80419370Spst	  bufptr = buffer + bytes;
80519370Spst	}
80619370Spst
80719370Spst      for (i = 0; i < tlen; i++)
80819370Spst	{
80919370Spst	  *bufptr++ = buf[i + offset];
81019370Spst	  if (buf[i + offset] == '\000')
81119370Spst	    {
81219370Spst	      nbytes_read += i + 1;
81319370Spst	      goto done;
81419370Spst	    }
81519370Spst	}
81619370Spst
81719370Spst      memaddr += tlen;
81819370Spst      len -= tlen;
81919370Spst      nbytes_read += tlen;
82019370Spst    }
82199002Sobriendone:
82219370Spst  if (errnop != NULL)
82319370Spst    *errnop = errcode;
82419370Spst  if (string != NULL)
82519370Spst    *string = buffer;
82619370Spst  return nbytes_read;
82719370Spst}
82819370Spst
829130809Smarcel/* Find a section containing ADDR.  */
830130809Smarcelstruct section_table *
831130809Smarceltarget_section_by_addr (struct target_ops *target, CORE_ADDR addr)
832130809Smarcel{
833130809Smarcel  struct section_table *secp;
834130809Smarcel  for (secp = target->to_sections;
835130809Smarcel       secp < target->to_sections_end;
836130809Smarcel       secp++)
837130809Smarcel    {
838130809Smarcel      if (addr >= secp->addr && addr < secp->endaddr)
839130809Smarcel	return secp;
840130809Smarcel    }
841130809Smarcel  return NULL;
842130809Smarcel}
843130809Smarcel
84419370Spst/* Read LEN bytes of target memory at address MEMADDR, placing the results in
84519370Spst   GDB's memory at MYADDR.  Returns either 0 for success or an errno value
84619370Spst   if any error occurs.
84719370Spst
84819370Spst   If an error occurs, no guarantee is made about the contents of the data at
84919370Spst   MYADDR.  In particular, the caller should not depend upon partial reads
85019370Spst   filling the buffer with good data.  There is no way for the caller to know
85119370Spst   how much good data might have been transfered anyway.  Callers that can
85219370Spst   deal with partial reads should call target_read_memory_partial. */
85319370Spst
85419370Spstint
85599002Sobrientarget_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
85619370Spst{
85799002Sobrien  return target_xfer_memory (memaddr, myaddr, len, 0);
85819370Spst}
85919370Spst
86046289Sdfrint
86199002Sobrientarget_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
86246289Sdfr{
86399002Sobrien  return target_xfer_memory (memaddr, myaddr, len, 1);
86446289Sdfr}
86546289Sdfr
86699002Sobrienstatic int trust_readonly = 0;
86719370Spst
86899002Sobrien/* Move memory to or from the targets.  The top target gets priority;
86999002Sobrien   if it cannot handle it, it is offered to the next one down, etc.
87019370Spst
87199002Sobrien   Result is -1 on error, or the number of bytes transfered.  */
87219370Spst
87319370Spstint
87499002Sobriendo_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
87599002Sobrien		struct mem_attrib *attrib)
87619370Spst{
87719370Spst  int res;
87899002Sobrien  int done = 0;
87919370Spst  struct target_ops *t;
88019370Spst
88146289Sdfr  /* Zero length requests are ok and require no work.  */
88246289Sdfr  if (len == 0)
88346289Sdfr    return 0;
88446289Sdfr
88519370Spst  /* to_xfer_memory is not guaranteed to set errno, even when it returns
88619370Spst     0.  */
88719370Spst  errno = 0;
88819370Spst
88999002Sobrien  if (!write && trust_readonly)
89099002Sobrien    {
89199002Sobrien      struct section_table *secp;
892130809Smarcel      /* User-settable option, "trust-readonly-sections".  If true,
893130809Smarcel         then memory from any SEC_READONLY bfd section may be read
894130809Smarcel         directly from the bfd file.  */
895130809Smarcel      secp = target_section_by_addr (&current_target, memaddr);
896130809Smarcel      if (secp != NULL
897130809Smarcel	  && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
898130809Smarcel	      & SEC_READONLY))
899130809Smarcel	return xfer_memory (memaddr, myaddr, len, 0, attrib, &current_target);
90099002Sobrien    }
90199002Sobrien
90299002Sobrien  /* The quick case is that the top target can handle the transfer.  */
90319370Spst  res = current_target.to_xfer_memory
90499002Sobrien    (memaddr, myaddr, len, write, attrib, &current_target);
90519370Spst
90699002Sobrien  /* If res <= 0 then we call it again in the loop.  Ah well. */
90799002Sobrien  if (res <= 0)
90819370Spst    {
909130809Smarcel      for (t = target_stack; t != NULL; t = t->beneath)
91019370Spst	{
91119370Spst	  if (!t->to_has_memory)
91219370Spst	    continue;
91319370Spst
91499002Sobrien	  res = t->to_xfer_memory (memaddr, myaddr, len, write, attrib, t);
91519370Spst	  if (res > 0)
91619370Spst	    break;		/* Handled all or part of xfer */
91719370Spst	  if (t->to_has_all_memory)
91819370Spst	    break;
91919370Spst	}
92019370Spst
92119370Spst      if (res <= 0)
92299002Sobrien	return -1;
92399002Sobrien    }
92499002Sobrien
92599002Sobrien  return res;
92699002Sobrien}
92799002Sobrien
92899002Sobrien
92999002Sobrien/* Perform a memory transfer.  Iterate until the entire region has
93099002Sobrien   been transfered.
93199002Sobrien
93299002Sobrien   Result is 0 or errno value.  */
93399002Sobrien
93499002Sobrienstatic int
93599002Sobrientarget_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
93699002Sobrien{
93799002Sobrien  int res;
93899002Sobrien  int reg_len;
93999002Sobrien  struct mem_region *region;
94099002Sobrien
94199002Sobrien  /* Zero length requests are ok and require no work.  */
94299002Sobrien  if (len == 0)
94399002Sobrien    {
94499002Sobrien      return 0;
94599002Sobrien    }
94699002Sobrien
94799002Sobrien  while (len > 0)
94899002Sobrien    {
94999002Sobrien      region = lookup_mem_region(memaddr);
95099002Sobrien      if (memaddr + len < region->hi)
95199002Sobrien	reg_len = len;
95299002Sobrien      else
95399002Sobrien	reg_len = region->hi - memaddr;
95499002Sobrien
95599002Sobrien      switch (region->attrib.mode)
95619370Spst	{
95799002Sobrien	case MEM_RO:
95899002Sobrien	  if (write)
95999002Sobrien	    return EIO;
96099002Sobrien	  break;
96199002Sobrien
96299002Sobrien	case MEM_WO:
96319370Spst	  if (!write)
96419370Spst	    return EIO;
96599002Sobrien	  break;
96699002Sobrien	}
96799002Sobrien
96899002Sobrien      while (reg_len > 0)
96999002Sobrien	{
97099002Sobrien	  if (region->attrib.cache)
97199002Sobrien	    res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
97299002Sobrien				     reg_len, write);
97319370Spst	  else
97499002Sobrien	    res = do_xfer_memory (memaddr, myaddr, reg_len, write,
97599002Sobrien				 &region->attrib);
97699002Sobrien
97799002Sobrien	  if (res <= 0)
97899002Sobrien	    {
97999002Sobrien	      /* If this address is for nonexistent memory, read zeros
98099002Sobrien		 if reading, or do nothing if writing.  Return
98199002Sobrien		 error. */
98299002Sobrien	      if (!write)
98399002Sobrien		memset (myaddr, 0, len);
98499002Sobrien	      if (errno == 0)
98599002Sobrien		return EIO;
98699002Sobrien	      else
98799002Sobrien		return errno;
98899002Sobrien	    }
98999002Sobrien
99099002Sobrien	  memaddr += res;
99199002Sobrien	  myaddr  += res;
99299002Sobrien	  len     -= res;
99399002Sobrien	  reg_len -= res;
99419370Spst	}
99519370Spst    }
99699002Sobrien
99719370Spst  return 0;			/* We managed to cover it all somehow. */
99819370Spst}
99919370Spst
100019370Spst
100199002Sobrien/* Perform a partial memory transfer.
100299002Sobrien
100399002Sobrien   Result is -1 on error, or the number of bytes transfered.  */
100499002Sobrien
100599002Sobrienstatic int
100699002Sobrientarget_xfer_memory_partial (CORE_ADDR memaddr, char *myaddr, int len,
100799002Sobrien			    int write_p, int *err)
100899002Sobrien{
100999002Sobrien  int res;
101099002Sobrien  int reg_len;
101199002Sobrien  struct mem_region *region;
101299002Sobrien
101399002Sobrien  /* Zero length requests are ok and require no work.  */
101499002Sobrien  if (len == 0)
101599002Sobrien    {
101699002Sobrien      *err = 0;
101799002Sobrien      return 0;
101899002Sobrien    }
101999002Sobrien
102099002Sobrien  region = lookup_mem_region(memaddr);
102199002Sobrien  if (memaddr + len < region->hi)
102299002Sobrien    reg_len = len;
102399002Sobrien  else
102499002Sobrien    reg_len = region->hi - memaddr;
102599002Sobrien
102699002Sobrien  switch (region->attrib.mode)
102799002Sobrien    {
102899002Sobrien    case MEM_RO:
102999002Sobrien      if (write_p)
103099002Sobrien	{
103199002Sobrien	  *err = EIO;
103299002Sobrien	  return -1;
103399002Sobrien	}
103499002Sobrien      break;
103599002Sobrien
103699002Sobrien    case MEM_WO:
103799002Sobrien      if (write_p)
103899002Sobrien	{
103999002Sobrien	  *err = EIO;
104099002Sobrien	  return -1;
104199002Sobrien	}
104299002Sobrien      break;
104399002Sobrien    }
104499002Sobrien
104599002Sobrien  if (region->attrib.cache)
104699002Sobrien    res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
104799002Sobrien			      reg_len, write_p);
104899002Sobrien  else
104999002Sobrien    res = do_xfer_memory (memaddr, myaddr, reg_len, write_p,
105099002Sobrien			  &region->attrib);
105199002Sobrien
105299002Sobrien  if (res <= 0)
105399002Sobrien    {
105499002Sobrien      if (errno != 0)
105599002Sobrien	*err = errno;
105699002Sobrien      else
105799002Sobrien	*err = EIO;
105899002Sobrien
105999002Sobrien        return -1;
106099002Sobrien    }
106199002Sobrien
106299002Sobrien  *err = 0;
106399002Sobrien  return res;
106499002Sobrien}
106599002Sobrien
106699002Sobrienint
106799002Sobrientarget_read_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
106899002Sobrien{
106999002Sobrien  return target_xfer_memory_partial (memaddr, buf, len, 0, err);
107099002Sobrien}
107199002Sobrien
107299002Sobrienint
107399002Sobrientarget_write_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
107499002Sobrien{
107599002Sobrien  return target_xfer_memory_partial (memaddr, buf, len, 1, err);
107699002Sobrien}
107799002Sobrien
1078130809Smarcel/* More generic transfers.  */
1079130809Smarcel
1080130809Smarcelstatic LONGEST
1081130809Smarceldefault_xfer_partial (struct target_ops *ops, enum target_object object,
1082130809Smarcel		      const char *annex, void *readbuf,
1083130809Smarcel		      const void *writebuf, ULONGEST offset, LONGEST len)
1084130809Smarcel{
1085130809Smarcel  if (object == TARGET_OBJECT_MEMORY
1086130809Smarcel      && ops->to_xfer_memory != NULL)
1087130809Smarcel    /* If available, fall back to the target's "to_xfer_memory"
1088130809Smarcel       method.  */
1089130809Smarcel    {
1090130809Smarcel      int xfered = -1;
1091130809Smarcel      errno = 0;
1092130809Smarcel      if (writebuf != NULL)
1093130809Smarcel	{
1094130809Smarcel	  void *buffer = xmalloc (len);
1095130809Smarcel	  struct cleanup *cleanup = make_cleanup (xfree, buffer);
1096130809Smarcel	  memcpy (buffer, writebuf, len);
1097130809Smarcel	  xfered = ops->to_xfer_memory (offset, buffer, len, 1/*write*/, NULL,
1098130809Smarcel					ops);
1099130809Smarcel	  do_cleanups (cleanup);
1100130809Smarcel	}
1101130809Smarcel      if (readbuf != NULL)
1102130809Smarcel	xfered = ops->to_xfer_memory (offset, readbuf, len, 0/*read*/, NULL,
1103130809Smarcel				      ops);
1104130809Smarcel      if (xfered > 0)
1105130809Smarcel	return xfered;
1106130809Smarcel      else if (xfered == 0 && errno == 0)
1107130809Smarcel	/* "to_xfer_memory" uses 0, cross checked against ERRNO as one
1108130809Smarcel           indication of an error.  */
1109130809Smarcel	return 0;
1110130809Smarcel      else
1111130809Smarcel	return -1;
1112130809Smarcel    }
1113130809Smarcel  else if (ops->beneath != NULL)
1114130809Smarcel    return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1115130809Smarcel					  readbuf, writebuf, offset, len);
1116130809Smarcel  else
1117130809Smarcel    return -1;
1118130809Smarcel}
1119130809Smarcel
1120130809Smarcel/* Target vector read/write partial wrapper functions.
1121130809Smarcel
1122130809Smarcel   NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1123130809Smarcel   (inbuf, outbuf)", instead of separate read/write methods, make life
1124130809Smarcel   easier.  */
1125130809Smarcel
1126130809SmarcelLONGEST
1127130809Smarceltarget_read_partial (struct target_ops *ops,
1128130809Smarcel		     enum target_object object,
1129130809Smarcel		     const char *annex, void *buf,
1130130809Smarcel		     ULONGEST offset, LONGEST len)
1131130809Smarcel{
1132130809Smarcel  gdb_assert (ops->to_xfer_partial != NULL);
1133130809Smarcel  return ops->to_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1134130809Smarcel}
1135130809Smarcel
1136130809SmarcelLONGEST
1137130809Smarceltarget_write_partial (struct target_ops *ops,
1138130809Smarcel		      enum target_object object,
1139130809Smarcel		      const char *annex, const void *buf,
1140130809Smarcel		      ULONGEST offset, LONGEST len)
1141130809Smarcel{
1142130809Smarcel  gdb_assert (ops->to_xfer_partial != NULL);
1143130809Smarcel  return ops->to_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1144130809Smarcel}
1145130809Smarcel
1146130809Smarcel/* Wrappers to perform the full transfer.  */
1147130809SmarcelLONGEST
1148130809Smarceltarget_read (struct target_ops *ops,
1149130809Smarcel	     enum target_object object,
1150130809Smarcel	     const char *annex, void *buf,
1151130809Smarcel	     ULONGEST offset, LONGEST len)
1152130809Smarcel{
1153130809Smarcel  LONGEST xfered = 0;
1154130809Smarcel  while (xfered < len)
1155130809Smarcel    {
1156130809Smarcel      LONGEST xfer = target_read_partial (ops, object, annex,
1157130809Smarcel					  (bfd_byte *) buf + xfered,
1158130809Smarcel					  offset + xfered, len - xfered);
1159130809Smarcel      /* Call an observer, notifying them of the xfer progress?  */
1160130809Smarcel      if (xfer <= 0)
1161130809Smarcel	/* Call memory_error?  */
1162130809Smarcel	return -1;
1163130809Smarcel      xfered += xfer;
1164130809Smarcel      QUIT;
1165130809Smarcel    }
1166130809Smarcel  return len;
1167130809Smarcel}
1168130809Smarcel
1169130809SmarcelLONGEST
1170130809Smarceltarget_write (struct target_ops *ops,
1171130809Smarcel	      enum target_object object,
1172130809Smarcel	      const char *annex, const void *buf,
1173130809Smarcel	      ULONGEST offset, LONGEST len)
1174130809Smarcel{
1175130809Smarcel  LONGEST xfered = 0;
1176130809Smarcel  while (xfered < len)
1177130809Smarcel    {
1178130809Smarcel      LONGEST xfer = target_write_partial (ops, object, annex,
1179130809Smarcel					   (bfd_byte *) buf + xfered,
1180130809Smarcel					   offset + xfered, len - xfered);
1181130809Smarcel      /* Call an observer, notifying them of the xfer progress?  */
1182130809Smarcel      if (xfer <= 0)
1183130809Smarcel	/* Call memory_error?  */
1184130809Smarcel	return -1;
1185130809Smarcel      xfered += xfer;
1186130809Smarcel      QUIT;
1187130809Smarcel    }
1188130809Smarcel  return len;
1189130809Smarcel}
1190130809Smarcel
1191130809Smarcel/* Memory transfer methods.  */
1192130809Smarcel
1193130809Smarcelvoid
1194130809Smarcelget_target_memory (struct target_ops *ops, CORE_ADDR addr, void *buf,
1195130809Smarcel		   LONGEST len)
1196130809Smarcel{
1197130809Smarcel  if (target_read (ops, TARGET_OBJECT_MEMORY, NULL, buf, addr, len)
1198130809Smarcel      != len)
1199130809Smarcel    memory_error (EIO, addr);
1200130809Smarcel}
1201130809Smarcel
1202130809SmarcelULONGEST
1203130809Smarcelget_target_memory_unsigned (struct target_ops *ops,
1204130809Smarcel			    CORE_ADDR addr, int len)
1205130809Smarcel{
1206130809Smarcel  char buf[sizeof (ULONGEST)];
1207130809Smarcel
1208130809Smarcel  gdb_assert (len <= sizeof (buf));
1209130809Smarcel  get_target_memory (ops, addr, buf, len);
1210130809Smarcel  return extract_unsigned_integer (buf, len);
1211130809Smarcel}
1212130809Smarcel
121319370Spststatic void
121499002Sobrientarget_info (char *args, int from_tty)
121519370Spst{
121619370Spst  struct target_ops *t;
121719370Spst  int has_all_mem = 0;
121899002Sobrien
121919370Spst  if (symfile_objfile != NULL)
122019370Spst    printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
122119370Spst
122219370Spst#ifdef FILES_INFO_HOOK
122319370Spst  if (FILES_INFO_HOOK ())
122419370Spst    return;
122519370Spst#endif
122619370Spst
1227130809Smarcel  for (t = target_stack; t != NULL; t = t->beneath)
122819370Spst    {
122919370Spst      if (!t->to_has_memory)
123019370Spst	continue;
123119370Spst
123299002Sobrien      if ((int) (t->to_stratum) <= (int) dummy_stratum)
123319370Spst	continue;
123419370Spst      if (has_all_mem)
123599002Sobrien	printf_unfiltered ("\tWhile running this, GDB does not access memory from...\n");
123699002Sobrien      printf_unfiltered ("%s:\n", t->to_longname);
123799002Sobrien      (t->to_files_info) (t);
123819370Spst      has_all_mem = t->to_has_all_memory;
123919370Spst    }
124019370Spst}
124119370Spst
124219370Spst/* This is to be called by the open routine before it does
124319370Spst   anything.  */
124419370Spst
124519370Spstvoid
124699002Sobrientarget_preopen (int from_tty)
124719370Spst{
124899002Sobrien  dont_repeat ();
124919370Spst
125019370Spst  if (target_has_execution)
125199002Sobrien    {
125299002Sobrien      if (!from_tty
125399002Sobrien          || query ("A program is being debugged already.  Kill it? "))
125499002Sobrien	target_kill ();
125519370Spst      else
125699002Sobrien	error ("Program not killed.");
125719370Spst    }
125819370Spst
125919370Spst  /* Calling target_kill may remove the target from the stack.  But if
126019370Spst     it doesn't (which seems like a win for UDI), remove it now.  */
126119370Spst
126219370Spst  if (target_has_execution)
126319370Spst    pop_target ();
126419370Spst}
126519370Spst
126619370Spst/* Detach a target after doing deferred register stores.  */
126719370Spst
126819370Spstvoid
126999002Sobrientarget_detach (char *args, int from_tty)
127019370Spst{
127119370Spst  /* Handle any optimized stores to the inferior.  */
127219370Spst#ifdef DO_DEFERRED_STORES
127319370Spst  DO_DEFERRED_STORES;
127419370Spst#endif
127519370Spst  (current_target.to_detach) (args, from_tty);
127619370Spst}
127719370Spst
127819370Spstvoid
1279130809Smarceltarget_disconnect (char *args, int from_tty)
1280130809Smarcel{
1281130809Smarcel  /* Handle any optimized stores to the inferior.  */
1282130809Smarcel#ifdef DO_DEFERRED_STORES
1283130809Smarcel  DO_DEFERRED_STORES;
1284130809Smarcel#endif
1285130809Smarcel  (current_target.to_disconnect) (args, from_tty);
1286130809Smarcel}
1287130809Smarcel
1288130809Smarcelvoid
128999002Sobrientarget_link (char *modname, CORE_ADDR *t_reloc)
129019370Spst{
1291130809Smarcel  if (DEPRECATED_STREQ (current_target.to_shortname, "rombug"))
129219370Spst    {
129319370Spst      (current_target.to_lookup_symbol) (modname, t_reloc);
129419370Spst      if (*t_reloc == 0)
129599002Sobrien	error ("Unable to link to %s and get relocation in rombug", modname);
129619370Spst    }
129719370Spst  else
129899002Sobrien    *t_reloc = (CORE_ADDR) -1;
129919370Spst}
130019370Spst
130199002Sobrienint
130299002Sobrientarget_async_mask (int mask)
130399002Sobrien{
130499002Sobrien  int saved_async_masked_status = target_async_mask_value;
130599002Sobrien  target_async_mask_value = mask;
130699002Sobrien  return saved_async_masked_status;
130799002Sobrien}
130899002Sobrien
130919370Spst/* Look through the list of possible targets for a target that can
131019370Spst   execute a run or attach command without any other data.  This is
131119370Spst   used to locate the default process stratum.
131219370Spst
131319370Spst   Result is always valid (error() is called for errors).  */
131419370Spst
131519370Spststatic struct target_ops *
131699002Sobrienfind_default_run_target (char *do_mesg)
131719370Spst{
131819370Spst  struct target_ops **t;
131919370Spst  struct target_ops *runable = NULL;
132019370Spst  int count;
132119370Spst
132219370Spst  count = 0;
132319370Spst
132419370Spst  for (t = target_structs; t < target_structs + target_struct_size;
132519370Spst       ++t)
132619370Spst    {
132799002Sobrien      if ((*t)->to_can_run && target_can_run (*t))
132819370Spst	{
132919370Spst	  runable = *t;
133019370Spst	  ++count;
133119370Spst	}
133219370Spst    }
133319370Spst
133419370Spst  if (count != 1)
133519370Spst    error ("Don't know how to %s.  Try \"help target\".", do_mesg);
133619370Spst
133719370Spst  return runable;
133819370Spst}
133919370Spst
134019370Spstvoid
134199002Sobrienfind_default_attach (char *args, int from_tty)
134219370Spst{
134319370Spst  struct target_ops *t;
134419370Spst
134599002Sobrien  t = find_default_run_target ("attach");
134619370Spst  (t->to_attach) (args, from_tty);
134719370Spst  return;
134819370Spst}
134919370Spst
135019370Spstvoid
135199002Sobrienfind_default_create_inferior (char *exec_file, char *allargs, char **env)
135219370Spst{
135319370Spst  struct target_ops *t;
135419370Spst
135599002Sobrien  t = find_default_run_target ("run");
135619370Spst  (t->to_create_inferior) (exec_file, allargs, env);
135719370Spst  return;
135819370Spst}
135919370Spst
1360130809Smarcelstatic int
1361130809Smarceldefault_region_size_ok_for_hw_watchpoint (int byte_count)
136246289Sdfr{
1363130809Smarcel  return (byte_count <= TYPE_LENGTH (builtin_type_void_data_ptr));
136446289Sdfr}
136546289Sdfr
136619370Spststatic int
136799002Sobrienreturn_zero (void)
136819370Spst{
136919370Spst  return 0;
137019370Spst}
137119370Spst
137246289Sdfrstatic int
137399002Sobrienreturn_one (void)
137446289Sdfr{
137546289Sdfr  return 1;
137646289Sdfr}
137746289Sdfr
1378130809Smarcelstatic int
1379130809Smarcelreturn_minus_one (void)
1380130809Smarcel{
1381130809Smarcel  return -1;
1382130809Smarcel}
1383130809Smarcel
138499002Sobrien/*
138599002Sobrien * Resize the to_sections pointer.  Also make sure that anyone that
138699002Sobrien * was holding on to an old value of it gets updated.
138799002Sobrien * Returns the old size.
138899002Sobrien */
138999002Sobrien
139099002Sobrienint
139199002Sobrientarget_resize_to_sections (struct target_ops *target, int num_added)
139299002Sobrien{
139399002Sobrien  struct target_ops **t;
139499002Sobrien  struct section_table *old_value;
139599002Sobrien  int old_count;
139699002Sobrien
139799002Sobrien  old_value = target->to_sections;
139899002Sobrien
139999002Sobrien  if (target->to_sections)
140099002Sobrien    {
140199002Sobrien      old_count = target->to_sections_end - target->to_sections;
140299002Sobrien      target->to_sections = (struct section_table *)
140399002Sobrien	xrealloc ((char *) target->to_sections,
140499002Sobrien		  (sizeof (struct section_table)) * (num_added + old_count));
140599002Sobrien    }
140699002Sobrien  else
140799002Sobrien    {
140899002Sobrien      old_count = 0;
140999002Sobrien      target->to_sections = (struct section_table *)
141099002Sobrien	xmalloc ((sizeof (struct section_table)) * num_added);
141199002Sobrien    }
141299002Sobrien  target->to_sections_end = target->to_sections + (num_added + old_count);
141399002Sobrien
141499002Sobrien  /* Check to see if anyone else was pointing to this structure.
141599002Sobrien     If old_value was null, then no one was. */
141699002Sobrien
141799002Sobrien  if (old_value)
141899002Sobrien    {
141999002Sobrien      for (t = target_structs; t < target_structs + target_struct_size;
142099002Sobrien	   ++t)
142199002Sobrien	{
142299002Sobrien	  if ((*t)->to_sections == old_value)
142399002Sobrien	    {
142499002Sobrien	      (*t)->to_sections = target->to_sections;
142599002Sobrien	      (*t)->to_sections_end = target->to_sections_end;
142699002Sobrien	    }
142799002Sobrien	}
1428232934Sdim      /* There is a flattened view of the target stack in current_target,
1429232934Sdim	 so its to_sections pointer might also need updating. */
1430232934Sdim      if (current_target.to_sections == old_value)
1431232934Sdim	{
1432232934Sdim	  current_target.to_sections = target->to_sections;
1433232934Sdim	  current_target.to_sections_end = target->to_sections_end;
1434232934Sdim	}
143599002Sobrien    }
143699002Sobrien
143799002Sobrien  return old_count;
143899002Sobrien
143999002Sobrien}
144099002Sobrien
144199002Sobrien/* Remove all target sections taken from ABFD.
144299002Sobrien
144399002Sobrien   Scan the current target stack for targets whose section tables
144499002Sobrien   refer to sections from BFD, and remove those sections.  We use this
144599002Sobrien   when we notice that the inferior has unloaded a shared object, for
144699002Sobrien   example.  */
144799002Sobrienvoid
144899002Sobrienremove_target_sections (bfd *abfd)
144999002Sobrien{
145099002Sobrien  struct target_ops **t;
145199002Sobrien
145299002Sobrien  for (t = target_structs; t < target_structs + target_struct_size; t++)
145399002Sobrien    {
145499002Sobrien      struct section_table *src, *dest;
145599002Sobrien
145699002Sobrien      dest = (*t)->to_sections;
145799002Sobrien      for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
145899002Sobrien	if (src->bfd != abfd)
145999002Sobrien	  {
146099002Sobrien	    /* Keep this section.  */
146199002Sobrien	    if (dest < src) *dest = *src;
146299002Sobrien	    dest++;
146399002Sobrien	  }
146499002Sobrien
146599002Sobrien      /* If we've dropped any sections, resize the section table.  */
146699002Sobrien      if (dest < src)
146799002Sobrien	target_resize_to_sections (*t, dest - src);
146899002Sobrien    }
146999002Sobrien}
147099002Sobrien
147199002Sobrien
147299002Sobrien
147399002Sobrien
147499002Sobrien/* Find a single runnable target in the stack and return it.  If for
147599002Sobrien   some reason there is more than one, return NULL.  */
147699002Sobrien
147719370Spststruct target_ops *
147899002Sobrienfind_run_target (void)
147919370Spst{
148019370Spst  struct target_ops **t;
148119370Spst  struct target_ops *runable = NULL;
148219370Spst  int count;
148399002Sobrien
148419370Spst  count = 0;
148599002Sobrien
148699002Sobrien  for (t = target_structs; t < target_structs + target_struct_size; ++t)
148799002Sobrien    {
148899002Sobrien      if ((*t)->to_can_run && target_can_run (*t))
148999002Sobrien	{
149099002Sobrien	  runable = *t;
149199002Sobrien	  ++count;
149299002Sobrien	}
149399002Sobrien    }
149499002Sobrien
149599002Sobrien  return (count == 1 ? runable : NULL);
149699002Sobrien}
149799002Sobrien
149899002Sobrien/* Find a single core_stratum target in the list of targets and return it.
149999002Sobrien   If for some reason there is more than one, return NULL.  */
150099002Sobrien
150199002Sobrienstruct target_ops *
150299002Sobrienfind_core_target (void)
150399002Sobrien{
150499002Sobrien  struct target_ops **t;
150599002Sobrien  struct target_ops *runable = NULL;
150699002Sobrien  int count;
150799002Sobrien
150899002Sobrien  count = 0;
150999002Sobrien
151019370Spst  for (t = target_structs; t < target_structs + target_struct_size;
151119370Spst       ++t)
151219370Spst    {
151399002Sobrien      if ((*t)->to_stratum == core_stratum)
151419370Spst	{
151519370Spst	  runable = *t;
151619370Spst	  ++count;
151719370Spst	}
151819370Spst    }
151999002Sobrien
152099002Sobrien  return (count == 1 ? runable : NULL);
152119370Spst}
152299002Sobrien
152399002Sobrien/*
152499002Sobrien * Find the next target down the stack from the specified target.
152599002Sobrien */
152699002Sobrien
152799002Sobrienstruct target_ops *
152899002Sobrienfind_target_beneath (struct target_ops *t)
152999002Sobrien{
1530130809Smarcel  return t->beneath;
153199002Sobrien}
153299002Sobrien
153319370Spst
153419370Spst/* The inferior process has died.  Long live the inferior!  */
153519370Spst
153619370Spstvoid
153799002Sobriengeneric_mourn_inferior (void)
153819370Spst{
153919370Spst  extern int show_breakpoint_hit_counts;
154019370Spst
154199002Sobrien  inferior_ptid = null_ptid;
154219370Spst  attach_flag = 0;
154346289Sdfr  breakpoint_init_inferior (inf_exited);
154419370Spst  registers_changed ();
154519370Spst
154619370Spst#ifdef CLEAR_DEFERRED_STORES
154719370Spst  /* Delete any pending stores to the inferior... */
154819370Spst  CLEAR_DEFERRED_STORES;
154919370Spst#endif
155019370Spst
155119370Spst  reopen_exec_file ();
155219370Spst  reinit_frame_cache ();
155319370Spst
155419370Spst  /* It is confusing to the user for ignore counts to stick around
155519370Spst     from previous runs of the inferior.  So clear them.  */
155619370Spst  /* However, it is more confusing for the ignore counts to disappear when
155719370Spst     using hit counts.  So don't clear them if we're counting hits.  */
155819370Spst  if (!show_breakpoint_hit_counts)
155919370Spst    breakpoint_clear_ignore_counts ();
156019370Spst
156199002Sobrien  if (detach_hook)
156299002Sobrien    detach_hook ();
156319370Spst}
156419370Spst
156519370Spst/* Helper function for child_wait and the Lynx derivatives of child_wait.
156619370Spst   HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
156719370Spst   translation of that in OURSTATUS.  */
156819370Spstvoid
156999002Sobrienstore_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
157019370Spst{
157119370Spst#ifdef CHILD_SPECIAL_WAITSTATUS
157219370Spst  /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
157319370Spst     if it wants to deal with hoststatus.  */
157419370Spst  if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
157519370Spst    return;
157619370Spst#endif
157719370Spst
157819370Spst  if (WIFEXITED (hoststatus))
157919370Spst    {
158019370Spst      ourstatus->kind = TARGET_WAITKIND_EXITED;
158119370Spst      ourstatus->value.integer = WEXITSTATUS (hoststatus);
158219370Spst    }
158319370Spst  else if (!WIFSTOPPED (hoststatus))
158419370Spst    {
158519370Spst      ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
158619370Spst      ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
158719370Spst    }
158819370Spst  else
158919370Spst    {
159019370Spst      ourstatus->kind = TARGET_WAITKIND_STOPPED;
159119370Spst      ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
159219370Spst    }
159319370Spst}
159419370Spst
159519370Spst/* Returns zero to leave the inferior alone, one to interrupt it.  */
159699002Sobrienint (*target_activity_function) (void);
159719370Spstint target_activity_fd;
159819370Spst
159919370Spst/* Convert a normal process ID to a string.  Returns the string in a static
160019370Spst   buffer.  */
160119370Spst
160219370Spstchar *
160399002Sobriennormal_pid_to_str (ptid_t ptid)
160419370Spst{
160519370Spst  static char buf[30];
160619370Spst
160799002Sobrien  sprintf (buf, "process %d", PIDGET (ptid));
160819370Spst  return buf;
160919370Spst}
161046289Sdfr
161199002Sobrien/* Error-catcher for target_find_memory_regions */
161299002Sobrienstatic int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
161399002Sobrien{
161499002Sobrien  error ("No target.");
161599002Sobrien  return 0;
161699002Sobrien}
161799002Sobrien
161899002Sobrien/* Error-catcher for target_make_corefile_notes */
161999002Sobrienstatic char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
162099002Sobrien{
162199002Sobrien  error ("No target.");
162299002Sobrien  return NULL;
162399002Sobrien}
162499002Sobrien
162546289Sdfr/* Set up the handful of non-empty slots needed by the dummy target
162646289Sdfr   vector.  */
162746289Sdfr
162846289Sdfrstatic void
162999002Sobrieninit_dummy_target (void)
163046289Sdfr{
163146289Sdfr  dummy_target.to_shortname = "None";
163246289Sdfr  dummy_target.to_longname = "None";
163346289Sdfr  dummy_target.to_doc = "";
163446289Sdfr  dummy_target.to_attach = find_default_attach;
163546289Sdfr  dummy_target.to_create_inferior = find_default_create_inferior;
163699002Sobrien  dummy_target.to_pid_to_str = normal_pid_to_str;
163746289Sdfr  dummy_target.to_stratum = dummy_stratum;
163899002Sobrien  dummy_target.to_find_memory_regions = dummy_find_memory_regions;
163999002Sobrien  dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
1640130809Smarcel  dummy_target.to_xfer_partial = default_xfer_partial;
164146289Sdfr  dummy_target.to_magic = OPS_MAGIC;
164246289Sdfr}
164399002Sobrien
164446289Sdfr
164519370Spststatic struct target_ops debug_target;
164619370Spst
164719370Spststatic void
164899002Sobriendebug_to_open (char *args, int from_tty)
164919370Spst{
165019370Spst  debug_target.to_open (args, from_tty);
165119370Spst
165299002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
165319370Spst}
165419370Spst
165519370Spststatic void
165699002Sobriendebug_to_close (int quitting)
165719370Spst{
1658130809Smarcel  target_close (&debug_target, quitting);
165999002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
166019370Spst}
166119370Spst
1662130809Smarcelvoid
1663130809Smarceltarget_close (struct target_ops *targ, int quitting)
1664130809Smarcel{
1665130809Smarcel  if (targ->to_xclose != NULL)
1666130809Smarcel    targ->to_xclose (targ, quitting);
1667130809Smarcel  else if (targ->to_close != NULL)
1668130809Smarcel    targ->to_close (quitting);
1669130809Smarcel}
1670130809Smarcel
167119370Spststatic void
167299002Sobriendebug_to_attach (char *args, int from_tty)
167319370Spst{
167419370Spst  debug_target.to_attach (args, from_tty);
167519370Spst
167699002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
167719370Spst}
167819370Spst
167946289Sdfr
168019370Spststatic void
168199002Sobriendebug_to_post_attach (int pid)
168246289Sdfr{
168346289Sdfr  debug_target.to_post_attach (pid);
168446289Sdfr
168599002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
168646289Sdfr}
168746289Sdfr
168846289Sdfrstatic void
168999002Sobriendebug_to_detach (char *args, int from_tty)
169019370Spst{
169119370Spst  debug_target.to_detach (args, from_tty);
169219370Spst
169399002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
169419370Spst}
169519370Spst
169619370Spststatic void
1697130809Smarceldebug_to_disconnect (char *args, int from_tty)
169846289Sdfr{
1699130809Smarcel  debug_target.to_disconnect (args, from_tty);
170046289Sdfr
1701130809Smarcel  fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
1702130809Smarcel		      args, from_tty);
170346289Sdfr}
170446289Sdfr
170546289Sdfrstatic void
170699002Sobriendebug_to_resume (ptid_t ptid, int step, enum target_signal siggnal)
170719370Spst{
170899002Sobrien  debug_target.to_resume (ptid, step, siggnal);
170919370Spst
171099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", PIDGET (ptid),
171119370Spst		      step ? "step" : "continue",
171219370Spst		      target_signal_to_name (siggnal));
171319370Spst}
171419370Spst
171599002Sobrienstatic ptid_t
171699002Sobriendebug_to_wait (ptid_t ptid, struct target_waitstatus *status)
171719370Spst{
171899002Sobrien  ptid_t retval;
171919370Spst
172099002Sobrien  retval = debug_target.to_wait (ptid, status);
172119370Spst
172299002Sobrien  fprintf_unfiltered (gdb_stdlog,
172399002Sobrien		      "target_wait (%d, status) = %d,   ", PIDGET (ptid),
172499002Sobrien		      PIDGET (retval));
172599002Sobrien  fprintf_unfiltered (gdb_stdlog, "status->kind = ");
172619370Spst  switch (status->kind)
172719370Spst    {
172819370Spst    case TARGET_WAITKIND_EXITED:
172999002Sobrien      fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
173046289Sdfr			  status->value.integer);
173119370Spst      break;
173219370Spst    case TARGET_WAITKIND_STOPPED:
173399002Sobrien      fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
173419370Spst			  target_signal_to_name (status->value.sig));
173519370Spst      break;
173619370Spst    case TARGET_WAITKIND_SIGNALLED:
173799002Sobrien      fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
173819370Spst			  target_signal_to_name (status->value.sig));
173919370Spst      break;
174019370Spst    case TARGET_WAITKIND_LOADED:
174199002Sobrien      fprintf_unfiltered (gdb_stdlog, "loaded\n");
174219370Spst      break;
174346289Sdfr    case TARGET_WAITKIND_FORKED:
174499002Sobrien      fprintf_unfiltered (gdb_stdlog, "forked\n");
174546289Sdfr      break;
174646289Sdfr    case TARGET_WAITKIND_VFORKED:
174799002Sobrien      fprintf_unfiltered (gdb_stdlog, "vforked\n");
174846289Sdfr      break;
174946289Sdfr    case TARGET_WAITKIND_EXECD:
175099002Sobrien      fprintf_unfiltered (gdb_stdlog, "execd\n");
175146289Sdfr      break;
175219370Spst    case TARGET_WAITKIND_SPURIOUS:
175399002Sobrien      fprintf_unfiltered (gdb_stdlog, "spurious\n");
175419370Spst      break;
175519370Spst    default:
175699002Sobrien      fprintf_unfiltered (gdb_stdlog, "unknown???\n");
175719370Spst      break;
175819370Spst    }
175919370Spst
176019370Spst  return retval;
176119370Spst}
176219370Spst
176319370Spststatic void
176499002Sobriendebug_to_post_wait (ptid_t ptid, int status)
176546289Sdfr{
176699002Sobrien  debug_target.to_post_wait (ptid, status);
176746289Sdfr
176899002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_post_wait (%d, %d)\n",
176999002Sobrien		      PIDGET (ptid), status);
177046289Sdfr}
177146289Sdfr
177246289Sdfrstatic void
1773130809Smarceldebug_print_register (const char * func, int regno)
1774130809Smarcel{
1775130809Smarcel  fprintf_unfiltered (gdb_stdlog, "%s ", func);
1776130809Smarcel  if (regno >= 0 && regno < NUM_REGS + NUM_PSEUDO_REGS
1777130809Smarcel      && REGISTER_NAME (regno) != NULL && REGISTER_NAME (regno)[0] != '\0')
1778130809Smarcel    fprintf_unfiltered (gdb_stdlog, "(%s)", REGISTER_NAME (regno));
1779130809Smarcel  else
1780130809Smarcel    fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1781130809Smarcel  if (regno >= 0)
1782130809Smarcel    {
1783130809Smarcel      int i;
1784130809Smarcel      unsigned char buf[MAX_REGISTER_SIZE];
1785130809Smarcel      deprecated_read_register_gen (regno, buf);
1786130809Smarcel      fprintf_unfiltered (gdb_stdlog, " = ");
1787130809Smarcel      for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno); i++)
1788130809Smarcel	{
1789130809Smarcel	  fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1790130809Smarcel	}
1791130809Smarcel      if (DEPRECATED_REGISTER_RAW_SIZE (regno) <= sizeof (LONGEST))
1792130809Smarcel	{
1793130809Smarcel	  fprintf_unfiltered (gdb_stdlog, " 0x%s %s",
1794130809Smarcel			      paddr_nz (read_register (regno)),
1795130809Smarcel			      paddr_d (read_register (regno)));
1796130809Smarcel	}
1797130809Smarcel    }
1798130809Smarcel  fprintf_unfiltered (gdb_stdlog, "\n");
1799130809Smarcel}
1800130809Smarcel
1801130809Smarcelstatic void
180299002Sobriendebug_to_fetch_registers (int regno)
180319370Spst{
180419370Spst  debug_target.to_fetch_registers (regno);
1805130809Smarcel  debug_print_register ("target_fetch_registers", regno);
180619370Spst}
180719370Spst
180819370Spststatic void
180999002Sobriendebug_to_store_registers (int regno)
181019370Spst{
181119370Spst  debug_target.to_store_registers (regno);
1812130809Smarcel  debug_print_register ("target_store_registers", regno);
1813130809Smarcel  fprintf_unfiltered (gdb_stdlog, "\n");
181419370Spst}
181519370Spst
181619370Spststatic void
181799002Sobriendebug_to_prepare_to_store (void)
181819370Spst{
181919370Spst  debug_target.to_prepare_to_store ();
182019370Spst
182199002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
182219370Spst}
182319370Spst
182419370Spststatic int
182599002Sobriendebug_to_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
182699002Sobrien		      struct mem_attrib *attrib,
182799002Sobrien		      struct target_ops *target)
182819370Spst{
182919370Spst  int retval;
183019370Spst
183199002Sobrien  retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write,
183299002Sobrien					attrib, target);
183319370Spst
183499002Sobrien  fprintf_unfiltered (gdb_stdlog,
183546289Sdfr		      "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
183699002Sobrien		      (unsigned int) memaddr,	/* possable truncate long long */
183746289Sdfr		      len, write ? "write" : "read", retval);
183819370Spst
183946289Sdfr
184099002Sobrien
184119370Spst  if (retval > 0)
184219370Spst    {
184319370Spst      int i;
184419370Spst
184599002Sobrien      fputs_unfiltered (", bytes =", gdb_stdlog);
184619370Spst      for (i = 0; i < retval; i++)
184746289Sdfr	{
184846289Sdfr	  if ((((long) &(myaddr[i])) & 0xf) == 0)
184999002Sobrien	    fprintf_unfiltered (gdb_stdlog, "\n");
185099002Sobrien	  fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
185146289Sdfr	}
185219370Spst    }
185319370Spst
185499002Sobrien  fputc_unfiltered ('\n', gdb_stdlog);
185519370Spst
185619370Spst  return retval;
185719370Spst}
185819370Spst
185919370Spststatic void
186099002Sobriendebug_to_files_info (struct target_ops *target)
186119370Spst{
186219370Spst  debug_target.to_files_info (target);
186319370Spst
186499002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
186519370Spst}
186619370Spst
186719370Spststatic int
186899002Sobriendebug_to_insert_breakpoint (CORE_ADDR addr, char *save)
186919370Spst{
187019370Spst  int retval;
187119370Spst
187219370Spst  retval = debug_target.to_insert_breakpoint (addr, save);
187319370Spst
187499002Sobrien  fprintf_unfiltered (gdb_stdlog,
187599002Sobrien		      "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
187699002Sobrien		      (unsigned long) addr,
187799002Sobrien		      (unsigned long) retval);
187819370Spst  return retval;
187919370Spst}
188019370Spst
188119370Spststatic int
188299002Sobriendebug_to_remove_breakpoint (CORE_ADDR addr, char *save)
188319370Spst{
188419370Spst  int retval;
188519370Spst
188619370Spst  retval = debug_target.to_remove_breakpoint (addr, save);
188719370Spst
188899002Sobrien  fprintf_unfiltered (gdb_stdlog,
188999002Sobrien		      "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
189099002Sobrien		      (unsigned long) addr,
189199002Sobrien		      (unsigned long) retval);
189219370Spst  return retval;
189319370Spst}
189419370Spst
1895130809Smarcelstatic int
1896130809Smarceldebug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
1897130809Smarcel{
1898130809Smarcel  int retval;
1899130809Smarcel
1900130809Smarcel  retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
1901130809Smarcel
1902130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1903130809Smarcel		      "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
1904130809Smarcel		      (unsigned long) type,
1905130809Smarcel		      (unsigned long) cnt,
1906130809Smarcel		      (unsigned long) from_tty,
1907130809Smarcel		      (unsigned long) retval);
1908130809Smarcel  return retval;
1909130809Smarcel}
1910130809Smarcel
1911130809Smarcelstatic int
1912130809Smarceldebug_to_region_size_ok_for_hw_watchpoint (int byte_count)
1913130809Smarcel{
1914130809Smarcel  CORE_ADDR retval;
1915130809Smarcel
1916130809Smarcel  retval = debug_target.to_region_size_ok_for_hw_watchpoint (byte_count);
1917130809Smarcel
1918130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1919130809Smarcel		      "TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT (%ld) = 0x%lx\n",
1920130809Smarcel		      (unsigned long) byte_count,
1921130809Smarcel		      (unsigned long) retval);
1922130809Smarcel  return retval;
1923130809Smarcel}
1924130809Smarcel
1925130809Smarcelstatic int
1926130809Smarceldebug_to_stopped_by_watchpoint (void)
1927130809Smarcel{
1928130809Smarcel  int retval;
1929130809Smarcel
1930130809Smarcel  retval = debug_target.to_stopped_by_watchpoint ();
1931130809Smarcel
1932130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1933130809Smarcel		      "STOPPED_BY_WATCHPOINT () = %ld\n",
1934130809Smarcel		      (unsigned long) retval);
1935130809Smarcel  return retval;
1936130809Smarcel}
1937130809Smarcel
1938130809Smarcelstatic CORE_ADDR
1939130809Smarceldebug_to_stopped_data_address (void)
1940130809Smarcel{
1941130809Smarcel  CORE_ADDR retval;
1942130809Smarcel
1943130809Smarcel  retval = debug_target.to_stopped_data_address ();
1944130809Smarcel
1945130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1946130809Smarcel		      "target_stopped_data_address () = 0x%lx\n",
1947130809Smarcel		      (unsigned long) retval);
1948130809Smarcel  return retval;
1949130809Smarcel}
1950130809Smarcel
1951130809Smarcelstatic int
1952130809Smarceldebug_to_insert_hw_breakpoint (CORE_ADDR addr, char *save)
1953130809Smarcel{
1954130809Smarcel  int retval;
1955130809Smarcel
1956130809Smarcel  retval = debug_target.to_insert_hw_breakpoint (addr, save);
1957130809Smarcel
1958130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1959130809Smarcel		      "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
1960130809Smarcel		      (unsigned long) addr,
1961130809Smarcel		      (unsigned long) retval);
1962130809Smarcel  return retval;
1963130809Smarcel}
1964130809Smarcel
1965130809Smarcelstatic int
1966130809Smarceldebug_to_remove_hw_breakpoint (CORE_ADDR addr, char *save)
1967130809Smarcel{
1968130809Smarcel  int retval;
1969130809Smarcel
1970130809Smarcel  retval = debug_target.to_remove_hw_breakpoint (addr, save);
1971130809Smarcel
1972130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1973130809Smarcel		      "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
1974130809Smarcel		      (unsigned long) addr,
1975130809Smarcel		      (unsigned long) retval);
1976130809Smarcel  return retval;
1977130809Smarcel}
1978130809Smarcel
1979130809Smarcelstatic int
1980130809Smarceldebug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
1981130809Smarcel{
1982130809Smarcel  int retval;
1983130809Smarcel
1984130809Smarcel  retval = debug_target.to_insert_watchpoint (addr, len, type);
1985130809Smarcel
1986130809Smarcel  fprintf_unfiltered (gdb_stdlog,
1987130809Smarcel		      "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
1988130809Smarcel		      (unsigned long) addr, len, type, (unsigned long) retval);
1989130809Smarcel  return retval;
1990130809Smarcel}
1991130809Smarcel
1992130809Smarcelstatic int
1993130809Smarceldebug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
1994130809Smarcel{
1995130809Smarcel  int retval;
1996130809Smarcel
1997130809Smarcel  retval = debug_target.to_insert_watchpoint (addr, len, type);
1998130809Smarcel
1999130809Smarcel  fprintf_unfiltered (gdb_stdlog,
2000130809Smarcel		      "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
2001130809Smarcel		      (unsigned long) addr, len, type, (unsigned long) retval);
2002130809Smarcel  return retval;
2003130809Smarcel}
2004130809Smarcel
200519370Spststatic void
200699002Sobriendebug_to_terminal_init (void)
200719370Spst{
200819370Spst  debug_target.to_terminal_init ();
200919370Spst
201099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
201119370Spst}
201219370Spst
201319370Spststatic void
201499002Sobriendebug_to_terminal_inferior (void)
201519370Spst{
201619370Spst  debug_target.to_terminal_inferior ();
201719370Spst
201899002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
201919370Spst}
202019370Spst
202119370Spststatic void
202299002Sobriendebug_to_terminal_ours_for_output (void)
202319370Spst{
202419370Spst  debug_target.to_terminal_ours_for_output ();
202519370Spst
202699002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
202719370Spst}
202819370Spst
202919370Spststatic void
203099002Sobriendebug_to_terminal_ours (void)
203119370Spst{
203219370Spst  debug_target.to_terminal_ours ();
203319370Spst
203499002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
203519370Spst}
203619370Spst
203719370Spststatic void
2038130809Smarceldebug_to_terminal_save_ours (void)
2039130809Smarcel{
2040130809Smarcel  debug_target.to_terminal_save_ours ();
2041130809Smarcel
2042130809Smarcel  fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
2043130809Smarcel}
2044130809Smarcel
2045130809Smarcelstatic void
204699002Sobriendebug_to_terminal_info (char *arg, int from_tty)
204719370Spst{
204819370Spst  debug_target.to_terminal_info (arg, from_tty);
204919370Spst
205099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
205119370Spst		      from_tty);
205219370Spst}
205319370Spst
205419370Spststatic void
205599002Sobriendebug_to_kill (void)
205619370Spst{
205719370Spst  debug_target.to_kill ();
205819370Spst
205999002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
206019370Spst}
206119370Spst
206219370Spststatic void
206399002Sobriendebug_to_load (char *args, int from_tty)
206419370Spst{
206519370Spst  debug_target.to_load (args, from_tty);
206619370Spst
206799002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
206819370Spst}
206919370Spst
207019370Spststatic int
207199002Sobriendebug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
207219370Spst{
207319370Spst  int retval;
207419370Spst
207519370Spst  retval = debug_target.to_lookup_symbol (name, addrp);
207619370Spst
207799002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
207819370Spst
207919370Spst  return retval;
208019370Spst}
208119370Spst
208219370Spststatic void
208399002Sobriendebug_to_create_inferior (char *exec_file, char *args, char **env)
208419370Spst{
208519370Spst  debug_target.to_create_inferior (exec_file, args, env);
208619370Spst
208799002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx)\n",
208819370Spst		      exec_file, args);
208919370Spst}
209019370Spst
209119370Spststatic void
209299002Sobriendebug_to_post_startup_inferior (ptid_t ptid)
209346289Sdfr{
209499002Sobrien  debug_target.to_post_startup_inferior (ptid);
209546289Sdfr
209699002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
209799002Sobrien		      PIDGET (ptid));
209846289Sdfr}
209946289Sdfr
210046289Sdfrstatic void
210199002Sobriendebug_to_acknowledge_created_inferior (int pid)
210246289Sdfr{
210346289Sdfr  debug_target.to_acknowledge_created_inferior (pid);
210446289Sdfr
210599002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
210646289Sdfr		      pid);
210746289Sdfr}
210846289Sdfr
210946289Sdfrstatic int
211099002Sobriendebug_to_insert_fork_catchpoint (int pid)
211146289Sdfr{
211299002Sobrien  int retval;
211346289Sdfr
211446289Sdfr  retval = debug_target.to_insert_fork_catchpoint (pid);
211546289Sdfr
211699002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
211799002Sobrien		      pid, retval);
211846289Sdfr
211946289Sdfr  return retval;
212046289Sdfr}
212146289Sdfr
212246289Sdfrstatic int
212399002Sobriendebug_to_remove_fork_catchpoint (int pid)
212446289Sdfr{
212599002Sobrien  int retval;
212646289Sdfr
212746289Sdfr  retval = debug_target.to_remove_fork_catchpoint (pid);
212846289Sdfr
212999002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
213099002Sobrien		      pid, retval);
213146289Sdfr
213246289Sdfr  return retval;
213346289Sdfr}
213446289Sdfr
213546289Sdfrstatic int
213699002Sobriendebug_to_insert_vfork_catchpoint (int pid)
213746289Sdfr{
213899002Sobrien  int retval;
213946289Sdfr
214046289Sdfr  retval = debug_target.to_insert_vfork_catchpoint (pid);
214146289Sdfr
214299002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)= %d\n",
214399002Sobrien		      pid, retval);
214446289Sdfr
214546289Sdfr  return retval;
214646289Sdfr}
214746289Sdfr
214846289Sdfrstatic int
214999002Sobriendebug_to_remove_vfork_catchpoint (int pid)
215046289Sdfr{
215199002Sobrien  int retval;
215246289Sdfr
215346289Sdfr  retval = debug_target.to_remove_vfork_catchpoint (pid);
215446289Sdfr
215599002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
215699002Sobrien		      pid, retval);
215746289Sdfr
215846289Sdfr  return retval;
215946289Sdfr}
216046289Sdfr
216146289Sdfrstatic int
2162130809Smarceldebug_to_follow_fork (int follow_child)
216346289Sdfr{
2164130809Smarcel  int retval =  debug_target.to_follow_fork (follow_child);
216546289Sdfr
2166130809Smarcel  fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2167130809Smarcel		      follow_child, retval);
216846289Sdfr
2169130809Smarcel  return retval;
217046289Sdfr}
217146289Sdfr
217246289Sdfrstatic int
217399002Sobriendebug_to_insert_exec_catchpoint (int pid)
217446289Sdfr{
217599002Sobrien  int retval;
217646289Sdfr
217746289Sdfr  retval = debug_target.to_insert_exec_catchpoint (pid);
217846289Sdfr
217999002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
218099002Sobrien		      pid, retval);
218146289Sdfr
218246289Sdfr  return retval;
218346289Sdfr}
218446289Sdfr
218546289Sdfrstatic int
218699002Sobriendebug_to_remove_exec_catchpoint (int pid)
218746289Sdfr{
218899002Sobrien  int retval;
218946289Sdfr
219046289Sdfr  retval = debug_target.to_remove_exec_catchpoint (pid);
219146289Sdfr
219299002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
219399002Sobrien		      pid, retval);
219446289Sdfr
219546289Sdfr  return retval;
219646289Sdfr}
219746289Sdfr
219846289Sdfrstatic int
219999002Sobriendebug_to_reported_exec_events_per_exec_call (void)
220046289Sdfr{
220199002Sobrien  int reported_exec_events;
220246289Sdfr
220346289Sdfr  reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
220446289Sdfr
220599002Sobrien  fprintf_unfiltered (gdb_stdlog,
220646289Sdfr		      "target_reported_exec_events_per_exec_call () = %d\n",
220799002Sobrien		      reported_exec_events);
220846289Sdfr
220946289Sdfr  return reported_exec_events;
221046289Sdfr}
221146289Sdfr
221246289Sdfrstatic int
221399002Sobriendebug_to_has_exited (int pid, int wait_status, int *exit_status)
221446289Sdfr{
221599002Sobrien  int has_exited;
221646289Sdfr
221746289Sdfr  has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
221846289Sdfr
221999002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
222099002Sobrien		      pid, wait_status, *exit_status, has_exited);
222146289Sdfr
222246289Sdfr  return has_exited;
222346289Sdfr}
222446289Sdfr
222546289Sdfrstatic void
222699002Sobriendebug_to_mourn_inferior (void)
222719370Spst{
222819370Spst  debug_target.to_mourn_inferior ();
222919370Spst
223099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
223119370Spst}
223219370Spst
223319370Spststatic int
223499002Sobriendebug_to_can_run (void)
223519370Spst{
223619370Spst  int retval;
223719370Spst
223819370Spst  retval = debug_target.to_can_run ();
223919370Spst
224099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
224119370Spst
224219370Spst  return retval;
224319370Spst}
224419370Spst
224519370Spststatic void
224699002Sobriendebug_to_notice_signals (ptid_t ptid)
224719370Spst{
224899002Sobrien  debug_target.to_notice_signals (ptid);
224919370Spst
225099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
225199002Sobrien                      PIDGET (ptid));
225219370Spst}
225319370Spst
225419370Spststatic int
225599002Sobriendebug_to_thread_alive (ptid_t ptid)
225619370Spst{
225746289Sdfr  int retval;
225819370Spst
225999002Sobrien  retval = debug_target.to_thread_alive (ptid);
226046289Sdfr
226199002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
226299002Sobrien		      PIDGET (ptid), retval);
226346289Sdfr
226446289Sdfr  return retval;
226519370Spst}
226619370Spst
226719370Spststatic void
226899002Sobriendebug_to_find_new_threads (void)
226919370Spst{
227099002Sobrien  debug_target.to_find_new_threads ();
227199002Sobrien
227299002Sobrien  fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
227399002Sobrien}
227499002Sobrien
227599002Sobrienstatic void
227699002Sobriendebug_to_stop (void)
227799002Sobrien{
227819370Spst  debug_target.to_stop ();
227919370Spst
228099002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
228119370Spst}
228219370Spst
2283130809Smarcelstatic LONGEST
2284130809Smarceldebug_to_xfer_partial (struct target_ops *ops, enum target_object object,
2285130809Smarcel		       const char *annex, void *readbuf, const void *writebuf,
2286130809Smarcel		       ULONGEST offset, LONGEST len)
228746289Sdfr{
2288130809Smarcel  LONGEST retval;
228946289Sdfr
2290130809Smarcel  retval = debug_target.to_xfer_partial (&debug_target, object, annex,
2291130809Smarcel					 readbuf, writebuf, offset, len);
229246289Sdfr
2293130809Smarcel  fprintf_unfiltered (gdb_stdlog,
2294130809Smarcel		      "target_xfer_partial (%d, %s, 0x%lx,  0x%lx,  0x%s, %s) = %s\n",
2295130809Smarcel		      (int) object, (annex ? annex : "(null)"),
2296130809Smarcel		      (long) readbuf, (long) writebuf, paddr_nz (offset),
2297130809Smarcel		      paddr_d (len), paddr_d (retval));
229846289Sdfr
229946289Sdfr  return retval;
230046289Sdfr}
230146289Sdfr
230299002Sobrienstatic void
230399002Sobriendebug_to_rcmd (char *command,
230499002Sobrien	       struct ui_file *outbuf)
230599002Sobrien{
230699002Sobrien  debug_target.to_rcmd (command, outbuf);
230799002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
230899002Sobrien}
230999002Sobrien
231046289Sdfrstatic struct symtab_and_line *
231199002Sobriendebug_to_enable_exception_callback (enum exception_event_kind kind, int enable)
231246289Sdfr{
231399002Sobrien  struct symtab_and_line *result;
231499002Sobrien  result = debug_target.to_enable_exception_callback (kind, enable);
231599002Sobrien  fprintf_unfiltered (gdb_stdlog,
231646289Sdfr		      "target get_exception_callback_sal (%d, %d)\n",
231746289Sdfr		      kind, enable);
231899002Sobrien  return result;
231946289Sdfr}
232046289Sdfr
232146289Sdfrstatic struct exception_event_record *
232299002Sobriendebug_to_get_current_exception_event (void)
232346289Sdfr{
232499002Sobrien  struct exception_event_record *result;
232599002Sobrien  result = debug_target.to_get_current_exception_event ();
232699002Sobrien  fprintf_unfiltered (gdb_stdlog, "target get_current_exception_event ()\n");
232799002Sobrien  return result;
232846289Sdfr}
232946289Sdfr
233046289Sdfrstatic char *
233199002Sobriendebug_to_pid_to_exec_file (int pid)
233246289Sdfr{
233399002Sobrien  char *exec_file;
233446289Sdfr
233546289Sdfr  exec_file = debug_target.to_pid_to_exec_file (pid);
233646289Sdfr
233799002Sobrien  fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
233899002Sobrien		      pid, exec_file);
233946289Sdfr
234046289Sdfr  return exec_file;
234146289Sdfr}
234246289Sdfr
234319370Spststatic void
234499002Sobriensetup_target_debug (void)
234519370Spst{
234619370Spst  memcpy (&debug_target, &current_target, sizeof debug_target);
234719370Spst
234819370Spst  current_target.to_open = debug_to_open;
234919370Spst  current_target.to_close = debug_to_close;
235019370Spst  current_target.to_attach = debug_to_attach;
235146289Sdfr  current_target.to_post_attach = debug_to_post_attach;
235219370Spst  current_target.to_detach = debug_to_detach;
2353130809Smarcel  current_target.to_disconnect = debug_to_disconnect;
235419370Spst  current_target.to_resume = debug_to_resume;
235519370Spst  current_target.to_wait = debug_to_wait;
235646289Sdfr  current_target.to_post_wait = debug_to_post_wait;
235719370Spst  current_target.to_fetch_registers = debug_to_fetch_registers;
235819370Spst  current_target.to_store_registers = debug_to_store_registers;
235919370Spst  current_target.to_prepare_to_store = debug_to_prepare_to_store;
236019370Spst  current_target.to_xfer_memory = debug_to_xfer_memory;
236119370Spst  current_target.to_files_info = debug_to_files_info;
236219370Spst  current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
236319370Spst  current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2364130809Smarcel  current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
2365130809Smarcel  current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
2366130809Smarcel  current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
2367130809Smarcel  current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
2368130809Smarcel  current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
2369130809Smarcel  current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
2370130809Smarcel  current_target.to_stopped_data_address = debug_to_stopped_data_address;
2371130809Smarcel  current_target.to_region_size_ok_for_hw_watchpoint = debug_to_region_size_ok_for_hw_watchpoint;
237219370Spst  current_target.to_terminal_init = debug_to_terminal_init;
237319370Spst  current_target.to_terminal_inferior = debug_to_terminal_inferior;
237419370Spst  current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
237519370Spst  current_target.to_terminal_ours = debug_to_terminal_ours;
2376130809Smarcel  current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
237719370Spst  current_target.to_terminal_info = debug_to_terminal_info;
237819370Spst  current_target.to_kill = debug_to_kill;
237919370Spst  current_target.to_load = debug_to_load;
238019370Spst  current_target.to_lookup_symbol = debug_to_lookup_symbol;
238119370Spst  current_target.to_create_inferior = debug_to_create_inferior;
238246289Sdfr  current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
238346289Sdfr  current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
238446289Sdfr  current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
238546289Sdfr  current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
238646289Sdfr  current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
238746289Sdfr  current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2388130809Smarcel  current_target.to_follow_fork = debug_to_follow_fork;
238946289Sdfr  current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
239046289Sdfr  current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
239146289Sdfr  current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
239246289Sdfr  current_target.to_has_exited = debug_to_has_exited;
239319370Spst  current_target.to_mourn_inferior = debug_to_mourn_inferior;
239419370Spst  current_target.to_can_run = debug_to_can_run;
239519370Spst  current_target.to_notice_signals = debug_to_notice_signals;
239619370Spst  current_target.to_thread_alive = debug_to_thread_alive;
239799002Sobrien  current_target.to_find_new_threads = debug_to_find_new_threads;
239819370Spst  current_target.to_stop = debug_to_stop;
2399130809Smarcel  current_target.to_xfer_partial = debug_to_xfer_partial;
240099002Sobrien  current_target.to_rcmd = debug_to_rcmd;
240146289Sdfr  current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
240246289Sdfr  current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
240346289Sdfr  current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
240446289Sdfr
240519370Spst}
240619370Spst
240799002Sobrien
240899002Sobrienstatic char targ_desc[] =
240999002Sobrien"Names of targets and files being debugged.\n\
241019370SpstShows the entire stack of targets currently in use (including the exec-file,\n\
241119370Spstcore-file, and process, if any), as well as the symbol file name.";
241219370Spst
241399002Sobrienstatic void
241499002Sobriendo_monitor_command (char *cmd,
241599002Sobrien		 int from_tty)
241699002Sobrien{
241799002Sobrien  if ((current_target.to_rcmd
241899002Sobrien       == (void (*) (char *, struct ui_file *)) tcomplain)
241999002Sobrien      || (current_target.to_rcmd == debug_to_rcmd
242099002Sobrien	  && (debug_target.to_rcmd
242199002Sobrien	      == (void (*) (char *, struct ui_file *)) tcomplain)))
242299002Sobrien    {
242399002Sobrien      error ("\"monitor\" command not supported by this target.\n");
242499002Sobrien    }
242599002Sobrien  target_rcmd (cmd, gdb_stdtarg);
242699002Sobrien}
242799002Sobrien
242819370Spstvoid
242999002Sobrieninitialize_targets (void)
243019370Spst{
243146289Sdfr  init_dummy_target ();
243219370Spst  push_target (&dummy_target);
243319370Spst
243419370Spst  add_info ("target", target_info, targ_desc);
243519370Spst  add_info ("files", target_info, targ_desc);
243619370Spst
243799002Sobrien  add_show_from_set
243899002Sobrien    (add_set_cmd ("target", class_maintenance, var_zinteger,
243999002Sobrien		  (char *) &targetdebug,
244099002Sobrien		  "Set target debugging.\n\
244199002SobrienWhen non-zero, target debugging is enabled.", &setdebuglist),
244299002Sobrien     &showdebuglist);
244319370Spst
2444130809Smarcel  add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
2445130809Smarcel			   &trust_readonly, "\
2446130809SmarcelSet mode for reading from readonly sections.\n\
244799002SobrienWhen this mode is on, memory reads from readonly sections (such as .text)\n\
244899002Sobrienwill be read from the object file instead of from the target.  This will\n\
2449130809Smarcelresult in significant performance improvement for remote targets.", "\
2450130809SmarcelShow mode for reading from readonly sections.\n",
2451130809Smarcel			   NULL, NULL,
2452130809Smarcel			   &setlist, &showlist);
245399002Sobrien
245499002Sobrien  add_com ("monitor", class_obscure, do_monitor_command,
245599002Sobrien	   "Send a command to the remote monitor (remote targets only).");
245699002Sobrien
245799002Sobrien  target_dcache = dcache_init ();
245819370Spst}
2459