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