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