1130803Smarcel/* Target operations for the remote server for GDB.
2130803Smarcel   Copyright 2002, 2004
3130803Smarcel   Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   Contributed by MontaVista Software.
6130803Smarcel
7130803Smarcel   This file is part of GDB.
8130803Smarcel
9130803Smarcel   This program is free software; you can redistribute it and/or modify
10130803Smarcel   it under the terms of the GNU General Public License as published by
11130803Smarcel   the Free Software Foundation; either version 2 of the License, or
12130803Smarcel   (at your option) any later version.
13130803Smarcel
14130803Smarcel   This program is distributed in the hope that it will be useful,
15130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
16130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17130803Smarcel   GNU General Public License for more details.
18130803Smarcel
19130803Smarcel   You should have received a copy of the GNU General Public License
20130803Smarcel   along with this program; if not, write to the Free Software
21130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
22130803Smarcel   Boston, MA 02111-1307, USA.  */
23130803Smarcel
24130803Smarcel#include "server.h"
25130803Smarcel
26130803Smarcelstruct target_ops *the_target;
27130803Smarcel
28130803Smarcelvoid
29130803Smarcelset_desired_inferior (int use_general)
30130803Smarcel{
31130803Smarcel  struct thread_info *found;
32130803Smarcel
33130803Smarcel  if (use_general == 1)
34130803Smarcel    {
35130803Smarcel      found = (struct thread_info *) find_inferior_id (&all_threads,
36130803Smarcel						       general_thread);
37130803Smarcel    }
38130803Smarcel  else
39130803Smarcel    {
40130803Smarcel      found = NULL;
41130803Smarcel
42130803Smarcel      /* If we are continuing any (all) thread(s), use step_thread
43130803Smarcel	 to decide which thread to step and/or send the specified
44130803Smarcel	 signal to.  */
45130803Smarcel      if (step_thread > 0 && (cont_thread == 0 || cont_thread == -1))
46130803Smarcel	found = (struct thread_info *) find_inferior_id (&all_threads,
47130803Smarcel							 step_thread);
48130803Smarcel
49130803Smarcel      if (found == NULL)
50130803Smarcel	found = (struct thread_info *) find_inferior_id (&all_threads,
51130803Smarcel							 cont_thread);
52130803Smarcel    }
53130803Smarcel
54130803Smarcel  if (found == NULL)
55130803Smarcel    current_inferior = (struct thread_info *) all_threads.head;
56130803Smarcel  else
57130803Smarcel    current_inferior = found;
58130803Smarcel}
59130803Smarcel
60130803Smarcelint
61130803Smarcelread_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
62130803Smarcel{
63130803Smarcel  int res;
64130803Smarcel  res = (*the_target->read_memory) (memaddr, myaddr, len);
65130803Smarcel  check_mem_read (memaddr, myaddr, len);
66130803Smarcel  return res;
67130803Smarcel}
68130803Smarcel
69130803Smarcelint
70130803Smarcelwrite_inferior_memory (CORE_ADDR memaddr, const char *myaddr, int len)
71130803Smarcel{
72130803Smarcel  /* Lacking cleanups, there is some potential for a memory leak if the
73130803Smarcel     write fails and we go through error().  Make sure that no more than
74130803Smarcel     one buffer is ever pending by making BUFFER static.  */
75130803Smarcel  static char *buffer = 0;
76130803Smarcel  int res;
77130803Smarcel
78130803Smarcel  if (buffer != NULL)
79130803Smarcel    free (buffer);
80130803Smarcel
81130803Smarcel  buffer = malloc (len);
82130803Smarcel  memcpy (buffer, myaddr, len);
83130803Smarcel  check_mem_write (memaddr, buffer, len);
84130803Smarcel  res = (*the_target->write_memory) (memaddr, buffer, len);
85130803Smarcel  free (buffer);
86130803Smarcel  buffer = NULL;
87130803Smarcel
88130803Smarcel  return res;
89130803Smarcel}
90130803Smarcel
91130803Smarcelunsigned char
92130803Smarcelmywait (char *statusp, int connected_wait)
93130803Smarcel{
94130803Smarcel  unsigned char ret;
95130803Smarcel
96130803Smarcel  if (connected_wait)
97130803Smarcel    server_waiting = 1;
98130803Smarcel
99130803Smarcel  ret = (*the_target->wait) (statusp);
100130803Smarcel
101130803Smarcel  if (connected_wait)
102130803Smarcel    server_waiting = 0;
103130803Smarcel
104130803Smarcel  return ret;
105130803Smarcel}
106130803Smarcel
107130803Smarcelvoid
108130803Smarcelset_target_ops (struct target_ops *target)
109130803Smarcel{
110130803Smarcel  the_target = (struct target_ops *) malloc (sizeof (*the_target));
111130803Smarcel  memcpy (the_target, target, sizeof (*the_target));
112130803Smarcel}
113