gdbserver: simply copy the pointer in 'set_target_ops'
[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 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 process_stratum_target *the_target;
31
32 int
33 set_desired_thread ()
34 {
35 client_state &cs = get_client_state ();
36 thread_info *found = find_thread_ptid (cs.general_thread);
37
38 current_thread = found;
39 return (current_thread != NULL);
40 }
41
42 /* The thread that was current before prepare_to_access_memory was
43 called. done_accessing_memory uses this to restore the previous
44 selected thread. */
45 static ptid_t prev_general_thread;
46
47 /* See target.h. */
48
49 int
50 prepare_to_access_memory (void)
51 {
52 client_state &cs = get_client_state ();
53
54 /* The first thread found. */
55 struct thread_info *first = NULL;
56 /* The first stopped thread found. */
57 struct thread_info *stopped = NULL;
58 /* The current general thread, if found. */
59 struct thread_info *current = NULL;
60
61 /* Save the general thread value, since prepare_to_access_memory could change
62 it. */
63 prev_general_thread = cs.general_thread;
64
65 int res = the_target->pt->prepare_to_access_memory ();
66 if (res != 0)
67 return res;
68
69 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
70 {
71 if (mythread_alive (thread->id))
72 {
73 if (stopped == NULL && the_target->pt->supports_thread_stopped ()
74 && target_thread_stopped (thread))
75 stopped = thread;
76
77 if (first == NULL)
78 first = thread;
79
80 if (current == NULL && prev_general_thread == thread->id)
81 current = thread;
82 }
83 });
84
85 /* The thread we end up choosing. */
86 struct thread_info *thread;
87
88 /* Prefer a stopped thread. If none is found, try the current
89 thread. Otherwise, take the first thread in the process. If
90 none is found, undo the effects of
91 target->prepare_to_access_memory() and return error. */
92 if (stopped != NULL)
93 thread = stopped;
94 else if (current != NULL)
95 thread = current;
96 else if (first != NULL)
97 thread = first;
98 else
99 {
100 done_accessing_memory ();
101 return 1;
102 }
103
104 current_thread = thread;
105 cs.general_thread = ptid_of (thread);
106
107 return 0;
108 }
109
110 /* See target.h. */
111
112 void
113 done_accessing_memory (void)
114 {
115 client_state &cs = get_client_state ();
116
117 the_target->pt->done_accessing_memory ();
118
119 /* Restore the previous selected thread. */
120 cs.general_thread = prev_general_thread;
121 switch_to_thread (the_target, cs.general_thread);
122 }
123
124 int
125 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
126 {
127 int res;
128 res = the_target->pt->read_memory (memaddr, myaddr, len);
129 check_mem_read (memaddr, myaddr, len);
130 return res;
131 }
132
133 /* See target/target.h. */
134
135 int
136 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
137 {
138 return read_inferior_memory (memaddr, myaddr, len);
139 }
140
141 /* See target/target.h. */
142
143 int
144 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
145 {
146 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
147 }
148
149 /* See target/target.h. */
150
151 int
152 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
153 ssize_t len)
154 {
155 /* Make a copy of the data because check_mem_write may need to
156 update it. */
157 gdb::byte_vector buffer (myaddr, myaddr + len);
158 check_mem_write (memaddr, buffer.data (), myaddr, len);
159 return the_target->pt->write_memory (memaddr, buffer.data (), len);
160 }
161
162 ptid_t
163 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
164 int connected_wait)
165 {
166 ptid_t ret;
167
168 if (connected_wait)
169 server_waiting = 1;
170
171 ret = target_wait (ptid, ourstatus, options);
172
173 /* We don't expose _LOADED events to gdbserver core. See the
174 `dlls_changed' global. */
175 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
176 ourstatus->kind = TARGET_WAITKIND_STOPPED;
177
178 /* If GDB is connected through TCP/serial, then GDBserver will most
179 probably be running on its own terminal/console, so it's nice to
180 print there why is GDBserver exiting. If however, GDB is
181 connected through stdio, then there's no need to spam the GDB
182 console with this -- the user will already see the exit through
183 regular GDB output, in that same terminal. */
184 if (!remote_connection_is_stdio ())
185 {
186 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
187 fprintf (stderr,
188 "\nChild exited with status %d\n", ourstatus->value.integer);
189 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
190 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
191 gdb_signal_to_host (ourstatus->value.sig),
192 gdb_signal_to_name (ourstatus->value.sig));
193 }
194
195 if (connected_wait)
196 server_waiting = 0;
197
198 return ret;
199 }
200
201 /* See target/target.h. */
202
203 void
204 target_stop_and_wait (ptid_t ptid)
205 {
206 struct target_waitstatus status;
207 bool was_non_stop = non_stop;
208 struct thread_resume resume_info;
209
210 resume_info.thread = ptid;
211 resume_info.kind = resume_stop;
212 resume_info.sig = GDB_SIGNAL_0;
213 the_target->pt->resume (&resume_info, 1);
214
215 non_stop = true;
216 mywait (ptid, &status, 0, 0);
217 non_stop = was_non_stop;
218 }
219
220 /* See target/target.h. */
221
222 ptid_t
223 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
224 {
225 return the_target->pt->wait (ptid, status, options);
226 }
227
228 /* See target/target.h. */
229
230 void
231 target_mourn_inferior (ptid_t ptid)
232 {
233 the_target->pt->mourn (find_process_pid (ptid.pid ()));
234 }
235
236 /* See target/target.h. */
237
238 void
239 target_continue_no_signal (ptid_t ptid)
240 {
241 struct thread_resume resume_info;
242
243 resume_info.thread = ptid;
244 resume_info.kind = resume_continue;
245 resume_info.sig = GDB_SIGNAL_0;
246 the_target->pt->resume (&resume_info, 1);
247 }
248
249 /* See target/target.h. */
250
251 void
252 target_continue (ptid_t ptid, enum gdb_signal signal)
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_to_host (signal);
259 the_target->pt->resume (&resume_info, 1);
260 }
261
262 /* See target/target.h. */
263
264 int
265 target_supports_multi_process (void)
266 {
267 return the_target->pt->supports_multi_process ();
268 }
269
270 void
271 set_target_ops (process_stratum_target *target)
272 {
273 the_target = target;
274 }
275
276 /* Convert pid to printable format. */
277
278 const char *
279 target_pid_to_str (ptid_t ptid)
280 {
281 static char buf[80];
282
283 if (ptid == minus_one_ptid)
284 xsnprintf (buf, sizeof (buf), "<all threads>");
285 else if (ptid == null_ptid)
286 xsnprintf (buf, sizeof (buf), "<null thread>");
287 else if (ptid.tid () != 0)
288 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
289 ptid.pid (), ptid.tid ());
290 else if (ptid.lwp () != 0)
291 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
292 ptid.pid (), ptid.lwp ());
293 else
294 xsnprintf (buf, sizeof (buf), "Process %d",
295 ptid.pid ());
296
297 return buf;
298 }
299
300 int
301 kill_inferior (process_info *proc)
302 {
303 gdb_agent_about_to_close (proc->pid);
304
305 return the_target->pt->kill (proc);
306 }
307
308 /* Define it. */
309
310 target_terminal_state target_terminal::m_terminal_state
311 = target_terminal_state::is_ours;
312
313 /* See target/target.h. */
314
315 void
316 target_terminal::init ()
317 {
318 /* Placeholder needed because of fork_inferior. Not necessary on
319 GDBserver. */
320 }
321
322 /* See target/target.h. */
323
324 void
325 target_terminal::inferior ()
326 {
327 /* Placeholder needed because of fork_inferior. Not necessary on
328 GDBserver. */
329 }
330
331 /* See target/target.h. */
332
333 void
334 target_terminal::ours ()
335 {
336 /* Placeholder needed because of fork_inferior. Not necessary on
337 GDBserver. */
338 }
339
340 /* See target/target.h. */
341
342 void
343 target_terminal::ours_for_output (void)
344 {
345 /* Placeholder. */
346 }
347
348 /* See target/target.h. */
349
350 void
351 target_terminal::info (const char *arg, int from_tty)
352 {
353 /* Placeholder. */
354 }
355
356 /* Default implementations of target ops.
357 See target.h for definitions. */
358
359 void
360 process_target::post_create_inferior ()
361 {
362 /* Nop. */
363 }
364
365 int
366 process_target::prepare_to_access_memory ()
367 {
368 return 0;
369 }
370
371 void
372 process_target::done_accessing_memory ()
373 {
374 /* Nop. */
375 }
376
377 void
378 process_target::look_up_symbols ()
379 {
380 /* Nop. */
381 }
382
383 bool
384 process_target::supports_read_auxv ()
385 {
386 return false;
387 }
388
389 int
390 process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
391 unsigned int len)
392 {
393 gdb_assert_not_reached ("target op read_auxv not supported");
394 }
395
396 bool
397 process_target::supports_z_point_type (char z_type)
398 {
399 return false;
400 }
401
402 int
403 process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
404 int size, raw_breakpoint *bp)
405 {
406 return 1;
407 }
408
409 int
410 process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
411 int size, raw_breakpoint *bp)
412 {
413 return 1;
414 }
415
416 bool
417 process_target::stopped_by_sw_breakpoint ()
418 {
419 return false;
420 }
421
422 bool
423 process_target::supports_stopped_by_sw_breakpoint ()
424 {
425 return false;
426 }
427
428 bool
429 process_target::stopped_by_hw_breakpoint ()
430 {
431 return false;
432 }
433
434 bool
435 process_target::supports_stopped_by_hw_breakpoint ()
436 {
437 return false;
438 }
439
440 bool
441 process_target::supports_hardware_single_step ()
442 {
443 return false;
444 }
445
446 bool
447 process_target::stopped_by_watchpoint ()
448 {
449 return false;
450 }
451
452 CORE_ADDR
453 process_target::stopped_data_address ()
454 {
455 return 0;
456 }
457
458 bool
459 process_target::supports_read_offsets ()
460 {
461 return false;
462 }
463
464 int
465 process_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
466 {
467 gdb_assert_not_reached ("target op read_offsets not supported");
468 }
469
470 bool
471 process_target::supports_get_tls_address ()
472 {
473 return false;
474 }
475
476 int
477 process_target::get_tls_address (thread_info *thread, CORE_ADDR offset,
478 CORE_ADDR load_module, CORE_ADDR *address)
479 {
480 gdb_assert_not_reached ("target op get_tls_address not supported");
481 }
482
483 void
484 process_target::hostio_last_error (char *buf)
485 {
486 hostio_last_error_from_errno (buf);
487 }
488
489 bool
490 process_target::supports_qxfer_osdata ()
491 {
492 return false;
493 }
494
495 int
496 process_target::qxfer_osdata (const char *annex, unsigned char *readbuf,
497 unsigned const char *writebuf,
498 CORE_ADDR offset, int len)
499 {
500 gdb_assert_not_reached ("target op qxfer_osdata not supported");
501 }
502
503 bool
504 process_target::supports_qxfer_siginfo ()
505 {
506 return false;
507 }
508
509 int
510 process_target::qxfer_siginfo (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_siginfo not supported");
515 }
516
517 bool
518 process_target::supports_non_stop ()
519 {
520 return false;
521 }
522
523 bool
524 process_target::async (bool enable)
525 {
526 return false;
527 }
528
529 int
530 process_target::start_non_stop (bool enable)
531 {
532 if (enable)
533 return -1;
534 else
535 return 0;
536 }
537
538 bool
539 process_target::supports_multi_process ()
540 {
541 return false;
542 }
543
544 bool
545 process_target::supports_fork_events ()
546 {
547 return false;
548 }
549
550 bool
551 process_target::supports_vfork_events ()
552 {
553 return false;
554 }
555
556 bool
557 process_target::supports_exec_events ()
558 {
559 return false;
560 }
561
562 void
563 process_target::handle_new_gdb_connection ()
564 {
565 /* Nop. */
566 }
567
568 int
569 process_target::handle_monitor_command (char *mon)
570 {
571 return 0;
572 }
573
574 int
575 process_target::core_of_thread (ptid_t ptid)
576 {
577 return -1;
578 }
579
580 bool
581 process_target::supports_read_loadmap ()
582 {
583 return false;
584 }
585
586 int
587 process_target::read_loadmap (const char *annex, CORE_ADDR offset,
588 unsigned char *myaddr, unsigned int len)
589 {
590 gdb_assert_not_reached ("target op read_loadmap not supported");
591 }
592
593 void
594 process_target::process_qsupported (char **features, int count)
595 {
596 /* Nop. */
597 }
598
599 bool
600 process_target::supports_tracepoints ()
601 {
602 return false;
603 }
604
605 CORE_ADDR
606 process_target::read_pc (regcache *regcache)
607 {
608 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
609 }
610
611 void
612 process_target::write_pc (regcache *regcache, CORE_ADDR pc)
613 {
614 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
615 }
616
617 bool
618 process_target::supports_thread_stopped ()
619 {
620 return false;
621 }
622
623 bool
624 process_target::thread_stopped (thread_info *thread)
625 {
626 gdb_assert_not_reached ("target op thread_stopped not supported");
627 }
628
629 bool
630 process_target::supports_get_tib_address ()
631 {
632 return false;
633 }
634
635 int
636 process_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
637 {
638 gdb_assert_not_reached ("target op get_tib_address not supported");
639 }
640
641 void
642 process_target::pause_all (bool freeze)
643 {
644 /* Nop. */
645 }
646
647 void
648 process_target::unpause_all (bool unfreeze)
649 {
650 /* Nop. */
651 }
652
653 void
654 process_target::stabilize_threads ()
655 {
656 /* Nop. */
657 }
658
659 bool
660 process_target::supports_fast_tracepoints ()
661 {
662 return false;
663 }
664
665 int
666 process_target::install_fast_tracepoint_jump_pad
667 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
668 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
669 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
670 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
671 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
672 char *err)
673 {
674 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
675 "not supported");
676 }
677
678 int
679 process_target::get_min_fast_tracepoint_insn_len ()
680 {
681 return 0;
682 }
683
684 struct emit_ops *
685 process_target::emit_ops ()
686 {
687 return nullptr;
688 }
689
690 bool
691 process_target::supports_disable_randomization ()
692 {
693 return false;
694 }
695
696 bool
697 process_target::supports_qxfer_libraries_svr4 ()
698 {
699 return false;
700 }
701
702 int
703 process_target::qxfer_libraries_svr4 (const char *annex,
704 unsigned char *readbuf,
705 unsigned const char *writebuf,
706 CORE_ADDR offset, int len)
707 {
708 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
709 }
710
711 bool
712 process_target::supports_agent ()
713 {
714 return false;
715 }
716
717 btrace_target_info *
718 process_target::enable_btrace (ptid_t ptid, const btrace_config *conf)
719 {
720 error (_("Target does not support branch tracing."));
721 }
722
723 int
724 process_target::disable_btrace (btrace_target_info *tinfo)
725 {
726 error (_("Target does not support branch tracing."));
727 }
728
729 int
730 process_target::read_btrace (btrace_target_info *tinfo,
731 buffer *buffer,
732 enum btrace_read_type type)
733 {
734 error (_("Target does not support branch tracing."));
735 }
736
737 int
738 process_target::read_btrace_conf (const btrace_target_info *tinfo,
739 buffer *buffer)
740 {
741 error (_("Target does not support branch tracing."));
742 }
743
744 bool
745 process_target::supports_range_stepping ()
746 {
747 return false;
748 }
749
750 bool
751 process_target::supports_pid_to_exec_file ()
752 {
753 return false;
754 }
755
756 char *
757 process_target::pid_to_exec_file (int pid)
758 {
759 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
760 }
761
762 bool
763 process_target::supports_multifs ()
764 {
765 return false;
766 }
767
768 int
769 process_target::multifs_open (int pid, const char *filename,
770 int flags, mode_t mode)
771 {
772 return open (filename, flags, mode);
773 }
774
775 int
776 process_target::multifs_unlink (int pid, const char *filename)
777 {
778 return unlink (filename);
779 }
780
781 ssize_t
782 process_target::multifs_readlink (int pid, const char *filename,
783 char *buf, size_t bufsiz)
784 {
785 return readlink (filename, buf, bufsiz);
786 }
787
788 int
789 process_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
790 {
791 /* The default behavior is to use the size of a breakpoint as the
792 kind. */
793 int size = 0;
794 sw_breakpoint_from_kind (0, &size);
795 return size;
796 }
797
798 int
799 process_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
800 {
801 return breakpoint_kind_from_pc (pcptr);
802 }
803
804 const char *
805 process_target::thread_name (ptid_t thread)
806 {
807 return nullptr;
808 }
809
810 bool
811 process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
812 int *handle_len)
813 {
814 return false;
815 }
816
817 bool
818 process_target::supports_software_single_step ()
819 {
820 return false;
821 }
822
823 bool
824 process_target::supports_catch_syscall ()
825 {
826 return false;
827 }
828
829 int
830 process_target::get_ipa_tdesc_idx ()
831 {
832 return 0;
833 }
This page took 0.056146 seconds and 5 git commands to generate.