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