gdbserver: turn non-stop and async target ops into methods
[deliverable/binutils-gdb.git] / gdbserver / target.cc
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2020 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 #include "gdbsupport/byte-vector.h"
24 #include "hostio.h"
25
26 process_stratum_target *the_target;
27
28 int
29 set_desired_thread ()
30 {
31 client_state &cs = get_client_state ();
32 thread_info *found = find_thread_ptid (cs.general_thread);
33
34 current_thread = found;
35 return (current_thread != NULL);
36 }
37
38 /* The thread that was current before prepare_to_access_memory was
39 called. done_accessing_memory uses this to restore the previous
40 selected thread. */
41 static ptid_t prev_general_thread;
42
43 /* See target.h. */
44
45 int
46 prepare_to_access_memory (void)
47 {
48 client_state &cs = get_client_state ();
49
50 /* The first thread found. */
51 struct thread_info *first = NULL;
52 /* The first stopped thread found. */
53 struct thread_info *stopped = NULL;
54 /* The current general thread, if found. */
55 struct thread_info *current = NULL;
56
57 /* Save the general thread value, since prepare_to_access_memory could change
58 it. */
59 prev_general_thread = cs.general_thread;
60
61 int res = the_target->pt->prepare_to_access_memory ();
62 if (res != 0)
63 return res;
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 cs.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 client_state &cs = get_client_state ();
112
113 the_target->pt->done_accessing_memory ();
114
115 /* Restore the previous selected thread. */
116 cs.general_thread = prev_general_thread;
117 switch_to_thread (the_target, cs.general_thread);
118 }
119
120 int
121 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
122 {
123 int res;
124 res = the_target->pt->read_memory (memaddr, myaddr, len);
125 check_mem_read (memaddr, myaddr, len);
126 return res;
127 }
128
129 /* See target/target.h. */
130
131 int
132 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
133 {
134 return read_inferior_memory (memaddr, myaddr, len);
135 }
136
137 /* See target/target.h. */
138
139 int
140 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
141 {
142 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
143 }
144
145 /* See target/target.h. */
146
147 int
148 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
149 ssize_t len)
150 {
151 /* Make a copy of the data because check_mem_write may need to
152 update it. */
153 gdb::byte_vector buffer (myaddr, myaddr + len);
154 check_mem_write (memaddr, buffer.data (), myaddr, len);
155 return the_target->pt->write_memory (memaddr, buffer.data (), len);
156 }
157
158 ptid_t
159 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
160 int connected_wait)
161 {
162 ptid_t ret;
163
164 if (connected_wait)
165 server_waiting = 1;
166
167 ret = target_wait (ptid, ourstatus, options);
168
169 /* We don't expose _LOADED events to gdbserver core. See the
170 `dlls_changed' global. */
171 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
172 ourstatus->kind = TARGET_WAITKIND_STOPPED;
173
174 /* If GDB is connected through TCP/serial, then GDBserver will most
175 probably be running on its own terminal/console, so it's nice to
176 print there why is GDBserver exiting. If however, GDB is
177 connected through stdio, then there's no need to spam the GDB
178 console with this -- the user will already see the exit through
179 regular GDB output, in that same terminal. */
180 if (!remote_connection_is_stdio ())
181 {
182 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
183 fprintf (stderr,
184 "\nChild exited with status %d\n", ourstatus->value.integer);
185 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
186 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
187 gdb_signal_to_host (ourstatus->value.sig),
188 gdb_signal_to_name (ourstatus->value.sig));
189 }
190
191 if (connected_wait)
192 server_waiting = 0;
193
194 return ret;
195 }
196
197 /* See target/target.h. */
198
199 void
200 target_stop_and_wait (ptid_t ptid)
201 {
202 struct target_waitstatus status;
203 bool was_non_stop = non_stop;
204 struct thread_resume resume_info;
205
206 resume_info.thread = ptid;
207 resume_info.kind = resume_stop;
208 resume_info.sig = GDB_SIGNAL_0;
209 the_target->pt->resume (&resume_info, 1);
210
211 non_stop = true;
212 mywait (ptid, &status, 0, 0);
213 non_stop = was_non_stop;
214 }
215
216 /* See target/target.h. */
217
218 ptid_t
219 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
220 {
221 return the_target->pt->wait (ptid, status, options);
222 }
223
224 /* See target/target.h. */
225
226 void
227 target_mourn_inferior (ptid_t ptid)
228 {
229 the_target->pt->mourn (find_process_pid (ptid.pid ()));
230 }
231
232 /* See target/target.h. */
233
234 void
235 target_continue_no_signal (ptid_t ptid)
236 {
237 struct thread_resume resume_info;
238
239 resume_info.thread = ptid;
240 resume_info.kind = resume_continue;
241 resume_info.sig = GDB_SIGNAL_0;
242 the_target->pt->resume (&resume_info, 1);
243 }
244
245 /* See target/target.h. */
246
247 void
248 target_continue (ptid_t ptid, enum gdb_signal signal)
249 {
250 struct thread_resume resume_info;
251
252 resume_info.thread = ptid;
253 resume_info.kind = resume_continue;
254 resume_info.sig = gdb_signal_to_host (signal);
255 the_target->pt->resume (&resume_info, 1);
256 }
257
258 /* See target/target.h. */
259
260 int
261 target_supports_multi_process (void)
262 {
263 return (the_target->supports_multi_process != NULL ?
264 (*the_target->supports_multi_process) () : 0);
265 }
266
267 void
268 set_target_ops (process_stratum_target *target)
269 {
270 the_target = XNEW (process_stratum_target);
271 memcpy (the_target, target, sizeof (*the_target));
272 }
273
274 /* Convert pid to printable format. */
275
276 const char *
277 target_pid_to_str (ptid_t ptid)
278 {
279 static char buf[80];
280
281 if (ptid == minus_one_ptid)
282 xsnprintf (buf, sizeof (buf), "<all threads>");
283 else if (ptid == null_ptid)
284 xsnprintf (buf, sizeof (buf), "<null thread>");
285 else if (ptid.tid () != 0)
286 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
287 ptid.pid (), ptid.tid ());
288 else if (ptid.lwp () != 0)
289 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
290 ptid.pid (), ptid.lwp ());
291 else
292 xsnprintf (buf, sizeof (buf), "Process %d",
293 ptid.pid ());
294
295 return buf;
296 }
297
298 int
299 kill_inferior (process_info *proc)
300 {
301 gdb_agent_about_to_close (proc->pid);
302
303 return the_target->pt->kill (proc);
304 }
305
306 /* Default implementation for breakpoint_kind_for_pc.
307
308 The default behavior for targets that don't implement breakpoint_kind_for_pc
309 is to use the size of a breakpoint as the kind. */
310
311 int
312 default_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
313 {
314 int size = 0;
315
316 gdb_assert (the_target->sw_breakpoint_from_kind != NULL);
317
318 (*the_target->sw_breakpoint_from_kind) (0, &size);
319 return size;
320 }
321
322 /* Define it. */
323
324 target_terminal_state target_terminal::m_terminal_state
325 = target_terminal_state::is_ours;
326
327 /* See target/target.h. */
328
329 void
330 target_terminal::init ()
331 {
332 /* Placeholder needed because of fork_inferior. Not necessary on
333 GDBserver. */
334 }
335
336 /* See target/target.h. */
337
338 void
339 target_terminal::inferior ()
340 {
341 /* Placeholder needed because of fork_inferior. Not necessary on
342 GDBserver. */
343 }
344
345 /* See target/target.h. */
346
347 void
348 target_terminal::ours ()
349 {
350 /* Placeholder needed because of fork_inferior. Not necessary on
351 GDBserver. */
352 }
353
354 /* See target/target.h. */
355
356 void
357 target_terminal::ours_for_output (void)
358 {
359 /* Placeholder. */
360 }
361
362 /* See target/target.h. */
363
364 void
365 target_terminal::info (const char *arg, int from_tty)
366 {
367 /* Placeholder. */
368 }
369
370 /* Default implementations of target ops.
371 See target.h for definitions. */
372
373 void
374 process_target::post_create_inferior ()
375 {
376 /* Nop. */
377 }
378
379 int
380 process_target::prepare_to_access_memory ()
381 {
382 return 0;
383 }
384
385 void
386 process_target::done_accessing_memory ()
387 {
388 /* Nop. */
389 }
390
391 void
392 process_target::look_up_symbols ()
393 {
394 /* Nop. */
395 }
396
397 bool
398 process_target::supports_read_auxv ()
399 {
400 return false;
401 }
402
403 int
404 process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
405 unsigned int len)
406 {
407 gdb_assert_not_reached ("target op read_auxv not supported");
408 }
409
410 bool
411 process_target::supports_z_point_type (char z_type)
412 {
413 return false;
414 }
415
416 int
417 process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
418 int size, raw_breakpoint *bp)
419 {
420 return 1;
421 }
422
423 int
424 process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
425 int size, raw_breakpoint *bp)
426 {
427 return 1;
428 }
429
430 bool
431 process_target::stopped_by_sw_breakpoint ()
432 {
433 return false;
434 }
435
436 bool
437 process_target::supports_stopped_by_sw_breakpoint ()
438 {
439 return false;
440 }
441
442 bool
443 process_target::stopped_by_hw_breakpoint ()
444 {
445 return false;
446 }
447
448 bool
449 process_target::supports_stopped_by_hw_breakpoint ()
450 {
451 return false;
452 }
453
454 bool
455 process_target::supports_hardware_single_step ()
456 {
457 return false;
458 }
459
460 bool
461 process_target::stopped_by_watchpoint ()
462 {
463 return false;
464 }
465
466 CORE_ADDR
467 process_target::stopped_data_address ()
468 {
469 return 0;
470 }
471
472 bool
473 process_target::supports_read_offsets ()
474 {
475 return false;
476 }
477
478 int
479 process_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
480 {
481 gdb_assert_not_reached ("target op read_offsets not supported");
482 }
483
484 bool
485 process_target::supports_get_tls_address ()
486 {
487 return false;
488 }
489
490 int
491 process_target::get_tls_address (thread_info *thread, CORE_ADDR offset,
492 CORE_ADDR load_module, CORE_ADDR *address)
493 {
494 gdb_assert_not_reached ("target op get_tls_address not supported");
495 }
496
497 void
498 process_target::hostio_last_error (char *buf)
499 {
500 hostio_last_error_from_errno (buf);
501 }
502
503 bool
504 process_target::supports_qxfer_osdata ()
505 {
506 return false;
507 }
508
509 int
510 process_target::qxfer_osdata (const char *annex, unsigned char *readbuf,
511 unsigned const char *writebuf,
512 CORE_ADDR offset, int len)
513 {
514 gdb_assert_not_reached ("target op qxfer_osdata not supported");
515 }
516
517 bool
518 process_target::supports_qxfer_siginfo ()
519 {
520 return false;
521 }
522
523 int
524 process_target::qxfer_siginfo (const char *annex, unsigned char *readbuf,
525 unsigned const char *writebuf,
526 CORE_ADDR offset, int len)
527 {
528 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
529 }
530
531 bool
532 process_target::supports_non_stop ()
533 {
534 return false;
535 }
536
537 bool
538 process_target::async (bool enable)
539 {
540 return false;
541 }
542
543 int
544 process_target::start_non_stop (bool enable)
545 {
546 if (enable)
547 return -1;
548 else
549 return 0;
550 }
This page took 0.041736 seconds and 5 git commands to generate.