GDBserver
target.c
Go to the documentation of this file.
1 /* Target operations for the remote server for GDB.
2  Copyright (C) 2002-2015 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 #include "tracepoint.h"
23 
25 
26 void
27 set_desired_thread (int use_general)
28 {
29  struct thread_info *found;
30 
31  if (use_general == 1)
33  else
34  found = find_thread_ptid (cont_thread);
35 
36  if (found == NULL)
38  else
39  current_thread = found;
40 }
41 
42 int
43 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
44 {
45  int res;
46  res = (*the_target->read_memory) (memaddr, myaddr, len);
47  check_mem_read (memaddr, myaddr, len);
48  return res;
49 }
50 
51 /* See target/target.h. */
52 
53 int
54 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
55 {
56  return read_inferior_memory (memaddr, myaddr, len);
57 }
58 
59 /* See target/target.h. */
60 
61 int
62 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
63 {
64  return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
65 }
66 
67 int
68 write_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 = xmalloc (len);
81  memcpy (buffer, myaddr, len);
82  check_mem_write (memaddr, buffer, myaddr, len);
83  res = (*the_target->write_memory) (memaddr, buffer, len);
84  free (buffer);
85  buffer = NULL;
86 
87  return res;
88 }
89 
90 /* See target/target.h. */
91 
92 int
93 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
94 {
95  return write_inferior_memory (memaddr, myaddr, len);
96 }
97 
98 ptid_t
99 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
100  int connected_wait)
101 {
102  ptid_t ret;
103 
104  if (connected_wait)
105  server_waiting = 1;
106 
107  ret = (*the_target->wait) (ptid, ourstatus, options);
108 
109  /* We don't expose _LOADED events to gdbserver core. See the
110  `dlls_changed' global. */
111  if (ourstatus->kind == TARGET_WAITKIND_LOADED)
112  ourstatus->kind = TARGET_WAITKIND_STOPPED;
113 
114  /* If GDB is connected through TCP/serial, then GDBserver will most
115  probably be running on its own terminal/console, so it's nice to
116  print there why is GDBserver exiting. If however, GDB is
117  connected through stdio, then there's no need to spam the GDB
118  console with this -- the user will already see the exit through
119  regular GDB output, in that same terminal. */
121  {
122  if (ourstatus->kind == TARGET_WAITKIND_EXITED)
123  fprintf (stderr,
124  "\nChild exited with status %d\n", ourstatus->value.integer);
125  else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
126  fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
127  gdb_signal_to_host (ourstatus->value.sig),
128  gdb_signal_to_name (ourstatus->value.sig));
129  }
130 
131  if (connected_wait)
132  server_waiting = 0;
133 
134  return ret;
135 }
136 
137 /* See target/target.h. */
138 
139 void
141 {
142  struct target_waitstatus status;
143  int was_non_stop = non_stop;
144  struct thread_resume resume_info;
145 
146  resume_info.thread = ptid;
147  resume_info.kind = resume_stop;
148  resume_info.sig = GDB_SIGNAL_0;
149  (*the_target->resume) (&resume_info, 1);
150 
151  non_stop = 1;
152  mywait (ptid, &status, 0, 0);
153  non_stop = was_non_stop;
154 }
155 
156 /* See target/target.h. */
157 
158 void
160 {
161  struct thread_resume resume_info;
162 
163  resume_info.thread = ptid;
164  resume_info.kind = resume_continue;
165  resume_info.sig = GDB_SIGNAL_0;
166  (*the_target->resume) (&resume_info, 1);
167 }
168 
169 int
170 start_non_stop (int nonstop)
171 {
172  if (the_target->start_non_stop == NULL)
173  {
174  if (nonstop)
175  return -1;
176  else
177  return 0;
178  }
179 
180  return (*the_target->start_non_stop) (nonstop);
181 }
182 
183 void
184 set_target_ops (struct target_ops *target)
185 {
186  the_target = (struct target_ops *) xmalloc (sizeof (*the_target));
187  memcpy (the_target, target, sizeof (*the_target));
188 }
189 
190 /* Convert pid to printable format. */
191 
192 const char *
194 {
195  static char buf[80];
196 
197  if (ptid_equal (ptid, minus_one_ptid))
198  xsnprintf (buf, sizeof (buf), "<all threads>");
199  else if (ptid_equal (ptid, null_ptid))
200  xsnprintf (buf, sizeof (buf), "<null thread>");
201  else if (ptid_get_tid (ptid) != 0)
202  xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
203  ptid_get_pid (ptid), ptid_get_tid (ptid));
204  else if (ptid_get_lwp (ptid) != 0)
205  xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
206  ptid_get_pid (ptid), ptid_get_lwp (ptid));
207  else
208  xsnprintf (buf, sizeof (buf), "Process %d",
209  ptid_get_pid (ptid));
210 
211  return buf;
212 }
213 
214 int
215 kill_inferior (int pid)
216 {
218 
219  return (*the_target->kill) (pid);
220 }
void check_mem_read(CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
Definition: mem-break.c:1723
struct thread_info * current_thread
Definition: inferiors.c:28
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: inferiors.c:141
int ptid_equal(ptid_t ptid1, ptid_t ptid2)
Definition: ptid.c:76
bfd_vma CORE_ADDR
Definition: common-types.h:41
void set_desired_thread(int use_general)
Definition: target.c:27
int server_waiting
Definition: server.c:50
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:54
int non_stop
Definition: server.c:62
struct target_ops * the_target
Definition: target.c:24
Definition: ptid.h:35
ptid_t general_thread
Definition: server.c:48
int remote_connection_is_stdio(void)
Definition: remote-utils.c:134
int(* write_memory)(CORE_ADDR memaddr, const unsigned char *myaddr, int len)
Definition: target.h:169
enum resume_kind kind
Definition: target.h:45
int read_inferior_memory(CORE_ADDR memaddr, unsigned char *myaddr, int len)
Definition: target.c:43
int gdb_signal_to_host(enum gdb_signal)
Definition: signals.c:631
int target_read_uint32(CORE_ADDR memaddr, uint32_t *result)
Definition: target.c:62
void target_continue_no_signal(ptid_t ptid)
Definition: target.c:159
const char * gdb_signal_to_name(enum gdb_signal)
Definition: signals.c:78
void set_target_ops(struct target_ops *target)
Definition: target.c:184
void check_mem_write(CORE_ADDR mem_addr, unsigned char *buf, const unsigned char *myaddr, int mem_len)
Definition: mem-break.c:1806
ptid_t cont_thread
Definition: server.c:45
int kill_inferior(int pid)
Definition: target.c:215
ptid_t mywait(ptid_t ptid, struct target_waitstatus *ourstatus, int options, int connected_wait)
Definition: target.c:99
struct thread_info * get_first_thread(void)
Definition: inferiors.c:135
int write_inferior_memory(CORE_ADDR memaddr, const unsigned char *myaddr, int len)
Definition: target.c:68
int ptid_get_pid(ptid_t ptid)
Definition: ptid.c:52
ptid_t(* wait)(ptid_t ptid, struct target_waitstatus *status, int options)
Definition: target.h:122
bfd_byte gdb_byte
Definition: common-types.h:38
ptid_t null_ptid
Definition: ptid.c:25
long ptid_get_tid(ptid_t ptid)
Definition: ptid.c:68
ptid_t thread
Definition: target.h:42
int xsnprintf(char *str, size_t size, const char *format,...)
Definition: common-utils.c:134
void target_stop_and_wait(ptid_t ptid)
Definition: target.c:140
const char * target_pid_to_str(ptid_t ptid)
Definition: target.c:193
Definition: buffer.h:23
void(* resume)(struct thread_resume *resume_info, size_t n)
Definition: target.h:109
int(* read_memory)(CORE_ADDR memaddr, unsigned char *myaddr, int len)
Definition: target.h:160
PTR xmalloc(size_t size)
Definition: common-utils.c:34
int(* start_non_stop)(int)
Definition: target.h:279
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition: target.c:93
long ptid_get_lwp(ptid_t ptid)
Definition: ptid.c:60
void gdb_agent_about_to_close(int pid)
Definition: tracepoint.c:3960
int(* kill)(int pid)
Definition: target.h:89
ptid_t minus_one_ptid
Definition: ptid.c:26
int start_non_stop(int nonstop)
Definition: target.c:170