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