1/* Target operations for the remote server for GDB.
2   Copyright (C) 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
3
4   Contributed by MontaVista Software.
5
6   This file is part of GDB.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21#include "server.h"
22
23struct target_ops *the_target;
24
25void
26set_desired_inferior (int use_general)
27{
28  struct thread_info *found;
29
30  if (use_general == 1)
31    {
32      found = (struct thread_info *) find_inferior_id (&all_threads,
33						       general_thread);
34    }
35  else
36    {
37      found = NULL;
38
39      /* If we are continuing any (all) thread(s), use step_thread
40	 to decide which thread to step and/or send the specified
41	 signal to.  */
42      if ((step_thread != 0 && step_thread != -1)
43	  && (cont_thread == 0 || cont_thread == -1))
44	found = (struct thread_info *) find_inferior_id (&all_threads,
45							 step_thread);
46
47      if (found == NULL)
48	found = (struct thread_info *) find_inferior_id (&all_threads,
49							 cont_thread);
50    }
51
52  if (found == NULL)
53    current_inferior = (struct thread_info *) all_threads.head;
54  else
55    current_inferior = found;
56}
57
58int
59read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
60{
61  int res;
62  res = (*the_target->read_memory) (memaddr, myaddr, len);
63  check_mem_read (memaddr, myaddr, len);
64  return res;
65}
66
67int
68write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
69		       int len)
70{
71  /* Lacking cleanups, there is some potential for a memory leak if the
72     write fails and we go through error().  Make sure that no more than
73     one buffer is ever pending by making BUFFER static.  */
74  static unsigned char *buffer = 0;
75  int res;
76
77  if (buffer != NULL)
78    free (buffer);
79
80  buffer = malloc (len);
81  memcpy (buffer, myaddr, len);
82  check_mem_write (memaddr, buffer, len);
83  res = (*the_target->write_memory) (memaddr, buffer, len);
84  free (buffer);
85  buffer = NULL;
86
87  return res;
88}
89
90unsigned char
91mywait (char *statusp, int connected_wait)
92{
93  unsigned char ret;
94
95  if (connected_wait)
96    server_waiting = 1;
97
98  ret = (*the_target->wait) (statusp);
99
100  if (connected_wait)
101    server_waiting = 0;
102
103  return ret;
104}
105
106void
107set_target_ops (struct target_ops *target)
108{
109  the_target = (struct target_ops *) malloc (sizeof (*the_target));
110  memcpy (the_target, target, sizeof (*the_target));
111}
112