GDBserver

target.c
Go to the documentation of this file.
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2018 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
24 struct target_ops *the_target;
25
26 int
27 set_desired_thread ()
28 {
29 thread_info *found = find_thread_ptid (general_thread);
30
31 current_thread = found;
32 return (current_thread != NULL);
33 }
34
35 /* The thread that was current before prepare_to_access_memory was
36 called. done_accessing_memory uses this to restore the previous
37 selected thread. */
38 static ptid_t prev_general_thread;
39
40 /* See target.h. */
41
42 int
43 prepare_to_access_memory (void)
44 {
45 /* The first thread found. */
46 struct thread_info *first = NULL;
47 /* The first stopped thread found. */
48 struct thread_info *stopped = NULL;
49 /* The current general thread, if found. */
50 struct thread_info *current = NULL;
51
52 /* Save the general thread value, since prepare_to_access_memory could change
53 it. */
54 prev_general_thread = general_thread;
55
56 if (the_target->prepare_to_access_memory != NULL)
57 {
58 int res;
59
60 res = the_target->prepare_to_access_memory ();
61 if (res != 0)
62 return res;
63 }
64
65 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
66 {
67 if (mythread_alive (thread->id))
68 {
69 if (stopped == NULL && the_target->thread_stopped != NULL
70 && thread_stopped (thread))
71 stopped = thread;
72
73 if (first == NULL)
74 first = thread;
75
76 if (current == NULL && prev_general_thread == thread->id)
77 current = thread;
78 }
79 });
80
81 /* The thread we end up choosing. */
82 struct thread_info *thread;
83
84 /* Prefer a stopped thread. If none is found, try the current
85 thread. Otherwise, take the first thread in the process. If
86 none is found, undo the effects of
87 target->prepare_to_access_memory() and return error. */
88 if (stopped != NULL)
89 thread = stopped;
90 else if (current != NULL)
91 thread = current;
92 else if (first != NULL)
93 thread = first;
94 else
95 {
96 done_accessing_memory ();
97 return 1;
98 }
99
100 current_thread = thread;
101 general_thread = ptid_of (thread);
102
103 return 0;
104 }
105
106 /* See target.h. */
107
108 void
109 done_accessing_memory (void)
110 {
111 if (the_target->done_accessing_memory != NULL)
112 the_target->done_accessing_memory ();
113
114 /* Restore the previous selected thread. */
115 general_thread = prev_general_thread;
116 switch_to_thread (general_thread);
117 }
118
119 int
120 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
121 {
122 int res;
123 res = (*the_target->read_memory) (memaddr, myaddr, len);
124 check_mem_read (memaddr, myaddr, len);
125 return res;
126 }
127
128 /* See target/target.h. */
129
130 int
131 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
132 {
133 return read_inferior_memory (memaddr, myaddr, len);
134 }
135
136 /* See target/target.h. */
137
138 int
139 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
140 {
141 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
142 }
143
144 int
145 write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
146 int len)
147 {
148 /* Lacking cleanups, there is some potential for a memory leak if the
149 write fails and we go through error(). Make sure that no more than
150 one buffer is ever pending by making BUFFER static. */
151 static unsigned char *buffer = 0;
152 int res;
153
154 if (buffer != NULL)
155 free (buffer);
156
157 buffer = (unsigned char *) xmalloc (len);
158 memcpy (buffer, myaddr, len);
159 check_mem_write (memaddr, buffer, myaddr, len);
160 res = (*the_target->write_memory) (memaddr, buffer, len);
161 free (buffer);
162 buffer = NULL;
163
164 return res;
165 }
166
167 /* See target/target.h. */
168
169 int
170 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
171 {
172 return write_inferior_memory (memaddr, myaddr, len);
173 }
174
175 ptid_t
176 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
177 int connected_wait)
178 {
179 ptid_t ret;
180
181 if (connected_wait)
182 server_waiting = 1;
183
184 ret = target_wait (ptid, ourstatus, options);
185
186 /* We don't expose _LOADED events to gdbserver core. See the
187 `dlls_changed' global. */
188 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
189 ourstatus->kind = TARGET_WAITKIND_STOPPED;
190
191 /* If GDB is connected through TCP/serial, then GDBserver will most
192 probably be running on its own terminal/console, so it's nice to
193 print there why is GDBserver exiting. If however, GDB is
194 connected through stdio, then there's no need to spam the GDB
195 console with this -- the user will already see the exit through
196 regular GDB output, in that same terminal. */
197 if (!remote_connection_is_stdio ())
198 {
199 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
200 fprintf (stderr,
201 "\nChild exited with status %d\n", ourstatus->value.integer);
202 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
203 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
204 gdb_signal_to_host (ourstatus->value.sig),
205 gdb_signal_to_name (ourstatus->value.sig));
206 }
207
208 if (connected_wait)
209 server_waiting = 0;
210
211 return ret;
212 }
213
214 /* See target/target.h. */
215
216 void
217 target_stop_and_wait (ptid_t ptid)
218 {
219 struct target_waitstatus status;
220 int was_non_stop = non_stop;
221 struct thread_resume resume_info;
222
223 resume_info.thread = ptid;
224 resume_info.kind = resume_stop;
225 resume_info.sig = GDB_SIGNAL_0;
226 (*the_target->resume) (&resume_info, 1);
227
228 non_stop = 1;
229 mywait (ptid, &status, 0, 0);
230 non_stop = was_non_stop;
231 }
232
233 /* See target/target.h. */
234
235 ptid_t
236 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
237 {
238 return (*the_target->wait) (ptid, status, options);
239 }
240
241 /* See target/target.h. */
242
243 void
244 target_mourn_inferior (ptid_t ptid)
245 {
246 (*the_target->mourn) (find_process_pid (ptid_get_pid (ptid)));
247 }
248
249 /* See target/target.h. */
250
251 void
252 target_continue_no_signal (ptid_t ptid)
253 {
254 struct thread_resume resume_info;
255
256 resume_info.thread = ptid;
257 resume_info.kind = resume_continue;
258 resume_info.sig = GDB_SIGNAL_0;
259 (*the_target->resume) (&resume_info, 1);
260 }
261
262 /* See target/target.h. */
263
264 void
265 target_continue (ptid_t ptid, enum gdb_signal signal)
266 {
267 struct thread_resume resume_info;
268
269 resume_info.thread = ptid;
270 resume_info.kind = resume_continue;
271 resume_info.sig = gdb_signal_to_host (signal);
272 (*the_target->resume) (&resume_info, 1);
273 }
274
275 /* See target/target.h. */
276
277 int
278 target_supports_multi_process (void)
279 {
280 return (the_target->supports_multi_process != NULL ?
281 (*the_target->supports_multi_process) () : 0);
282 }
283
284 int
285 start_non_stop (int nonstop)
286 {
287 if (the_target->start_non_stop == NULL)
288 {
289 if (nonstop)
290 return -1;
291 else
292 return 0;
293 }
294
295 return (*the_target->start_non_stop) (nonstop);
296 }
297
298 void
299 set_target_ops (struct target_ops *target)
300 {
301 the_target = XNEW (struct target_ops);
302 memcpy (the_target, target, sizeof (*the_target));
303 }
304
305 /* Convert pid to printable format. */
306
307 const char *
308 target_pid_to_str (ptid_t ptid)
309 {
310 static char buf[80];
311
312 if (ptid_equal (ptid, minus_one_ptid))
313 xsnprintf (buf, sizeof (buf), "<all threads>");
314 else if (ptid_equal (ptid, null_ptid))
315 xsnprintf (buf, sizeof (buf), "<null thread>");
316 else if (ptid_get_tid (ptid) != 0)
317 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
318 ptid_get_pid (ptid), ptid_get_tid (ptid));
319 else if (ptid_get_lwp (ptid) != 0)
320 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
321 ptid_get_pid (ptid), ptid_get_lwp (ptid));
322 else
323 xsnprintf (buf, sizeof (buf), "Process %d",
324 ptid_get_pid (ptid));
325
326 return buf;
327 }
328
329 int
330 kill_inferior (int pid)
331 {
332 gdb_agent_about_to_close (pid);
333
334 return (*the_target->kill) (pid);
335 }
336
337 /* Target can do hardware single step. */
338
339 int
340 target_can_do_hardware_single_step (void)
341 {
342 return 1;
343 }
344
345 /* Default implementation for breakpoint_kind_for_pc.
346
347 The default behavior for targets that don't implement breakpoint_kind_for_pc
348 is to use the size of a breakpoint as the kind. */
349
350 int
351 default_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
352 {
353 int size = 0;
354
355 gdb_assert (the_target->sw_breakpoint_from_kind != NULL);
356
357 (*the_target->sw_breakpoint_from_kind) (0, &size);
358 return size;
359 }
360
361 /* Define it. */
362
363 enum target_terminal::terminal_state target_terminal::terminal_state
364 = target_terminal::terminal_is_ours;
365
366 /* See target/target.h. */
367
368 void
369 target_terminal::init ()
370 {
371 /* Placeholder needed because of fork_inferior. Not necessary on
372 GDBserver. */
373 }
374
375 /* See target/target.h. */
376
377 void
378 target_terminal::inferior ()
379 {
380 /* Placeholder needed because of fork_inferior. Not necessary on
381 GDBserver. */
382 }
383
384 /* See target/target.h. */
385
386 void
387 target_terminal::ours ()
388 {
389 /* Placeholder needed because of fork_inferior. Not necessary on
390 GDBserver. */
391 }
392
393 /* See target/target.h. */
394
395 void
396 target_terminal::ours_for_output (void)
397 {
398 /* Placeholder. */
399 }
400
401 /* See target/target.h. */
402
403 void
404 target_terminal::info (const char *arg, int from_tty)
405 {
406 /* Placeholder. */
407 }
check_mem_read
void check_mem_read(CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
Definition: mem-break.c:1919
target_mourn_inferior
void target_mourn_inferior(ptid_t ptid)
Definition: target.c:244
current_thread
struct thread_info * current_thread
Definition: inferiors.c:28
target_can_do_hardware_single_step
int target_can_do_hardware_single_step(void)
Definition: target.c:340
target_ops::prepare_to_access_memory
int(* prepare_to_access_memory)(void)
Definition: target.h:153
ptid_t::pid
constexpr int pid() const
Definition: ptid.h:53
set_desired_thread
int set_desired_thread()
Definition: target.c:27
find_thread_ptid
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: inferiors.c:64
target_supports_multi_process
int target_supports_multi_process(void)
Definition: target.c:278
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
ptid_get_pid
int ptid_get_pid(const ptid_t &ptid)
Definition: ptid.c:47
ptid_of
static ptid_t ptid_of(const thread_info *thread)
Definition: gdbthread.h:206
default_breakpoint_kind_from_pc
int default_breakpoint_kind_from_pc(CORE_ADDR *pcptr)
Definition: target.c:351
server_waiting
int server_waiting
Definition: server.c:80
target_read_memory
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:131
mythread_alive
#define mythread_alive(pid)
Definition: target.h:526
target_ops::mourn
void(* mourn)(struct process_info *proc)
Definition: target.h:104
target_ops::supports_multi_process
int(* supports_multi_process)(void)
Definition: target.h:287
for_each_thread
static void for_each_thread(Func func)
Definition: gdbthread.h:142
non_stop
int non_stop
Definition: server.c:98
the_target
struct target_ops * the_target
Definition: target.c:24
XNEW
#define XNEW(T)
Definition: poison.h:109
general_thread
ptid_t general_thread
Definition: server.c:78
thread_info
Definition: gdbthread.h:30
thread_resume::sig
int sig
Definition: target.h:54
prev_general_thread
static ptid_t prev_general_thread
Definition: target.c:38
remote_connection_is_stdio
int remote_connection_is_stdio(void)
Definition: remote-utils.c:137
target_ops::write_memory
int(* write_memory)(CORE_ADDR memaddr, const unsigned char *myaddr, int len)
Definition: target.h:175
target_ops::thread_stopped
int(* thread_stopped)(struct thread_info *thread)
Definition: target.h:327
target_ops
Definition: target.h:66
find_process_pid
process_info * find_process_pid(int pid)
Definition: inferiors.c:164
thread_resume::kind
enum resume_kind kind
Definition: target.h:47
ptid_get_tid
long ptid_get_tid(const ptid_t &ptid)
Definition: ptid.c:63
ptid_t
Definition: ptid.h:35
read_inferior_memory
int read_inferior_memory(CORE_ADDR memaddr, unsigned char *myaddr, int len)
Definition: target.c:120
target_ops::done_accessing_memory
void(* done_accessing_memory)(void)
Definition: target.h:157
gdb_signal_to_host
int gdb_signal_to_host(enum gdb_signal)
Definition: signals.c:639
tracepoint.h
free
void free(T *ptr)=delete
server.h
target_continue
void target_continue(ptid_t ptid, enum gdb_signal signal)
Definition: target.c:265
thread_resume
Definition: target.h:42
target_read_uint32
int target_read_uint32(CORE_ADDR memaddr, uint32_t *result)
Definition: target.c:139
target_continue_no_signal
void target_continue_no_signal(ptid_t ptid)
Definition: target.c:252
gdb_signal_to_name
const char * gdb_signal_to_name(enum gdb_signal)
Definition: signals.c:78
set_target_ops
void set_target_ops(struct target_ops *target)
Definition: target.c:299
check_mem_write
void check_mem_write(CORE_ADDR mem_addr, unsigned char *buf, const unsigned char *myaddr, int mem_len)
Definition: mem-break.c:2002
target_wait
ptid_t target_wait(ptid_t ptid, struct target_waitstatus *status, int options)
Definition: target.c:236
ptid_get_lwp
long ptid_get_lwp(const ptid_t &ptid)
Definition: ptid.c:55
kill_inferior
int kill_inferior(int pid)
Definition: target.c:330
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
mywait
ptid_t mywait(ptid_t ptid, struct target_waitstatus *ourstatus, int options, int connected_wait)
Definition: target.c:176
write_inferior_memory
int write_inferior_memory(CORE_ADDR memaddr, const unsigned char *myaddr, int len)
Definition: target.c:145
target_ops::wait
ptid_t(* wait)(ptid_t ptid, struct target_waitstatus *status, int options)
Definition: target.h:128
switch_to_thread
void switch_to_thread(ptid_t ptid)
Definition: inferiors.c:231
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
target_ops::sw_breakpoint_from_kind
const gdb_byte *(* sw_breakpoint_from_kind)(int kind, int *size)
Definition: target.h:457
null_ptid
ptid_t null_ptid
Definition: ptid.c:25
thread_resume::thread
ptid_t thread
Definition: target.h:44
xsnprintf
int xsnprintf(char *str, size_t size, const char *format,...)
Definition: common-utils.c:134
target_stop_and_wait
void target_stop_and_wait(ptid_t ptid)
Definition: target.c:217
prepare_to_access_memory
int prepare_to_access_memory(void)
Definition: target.c:43
target_pid_to_str
const char * target_pid_to_str(ptid_t ptid)
Definition: target.c:308
buffer
Definition: buffer.h:23
target_ops::resume
void(* resume)(struct thread_resume *resume_info, size_t n)
Definition: target.h:115
target_ops::read_memory
int(* read_memory)(CORE_ADDR memaddr, unsigned char *myaddr, int len)
Definition: target.h:166
ptid_equal
int ptid_equal(const ptid_t &ptid1, const ptid_t &ptid2)
Definition: ptid.c:71
thread_stopped
#define thread_stopped(thread)
Definition: target.h:570
xmalloc
PTR xmalloc(size_t size)
Definition: common-utils.c:35
target_ops::start_non_stop
int(* start_non_stop)(int)
Definition: target.h:284
first
struct callback_event * first
Definition: event-loop.c:141
target_write_memory
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition: target.c:170
gdb_agent_about_to_close
void gdb_agent_about_to_close(int pid)
Definition: tracepoint.c:3960
target_ops::kill
int(* kill)(int pid)
Definition: target.h:95
minus_one_ptid
ptid_t minus_one_ptid
Definition: ptid.c:26
done_accessing_memory
void done_accessing_memory(void)
Definition: target.c:109
start_non_stop
int start_non_stop(int nonstop)
Definition: target.c:285