RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdbserver / target.cc
blob6db32da2e959284fea72f37ec2ddacf8071022e4
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
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>
29 process_stratum_target *the_target;
31 /* See target.h. */
33 bool
34 set_desired_thread ()
36 client_state &cs = get_client_state ();
37 thread_info *found = find_thread_ptid (cs.general_thread);
39 if (found == nullptr)
41 process_info *proc = find_process_pid (cs.general_thread.pid ());
42 if (proc == nullptr)
44 threads_debug_printf
45 ("did not find thread nor process for general_thread %s",
46 cs.general_thread.to_string ().c_str ());
48 else
50 threads_debug_printf
51 ("did not find thread for general_thread %s, but found process",
52 cs.general_thread.to_string ().c_str ());
54 switch_to_process (proc);
56 else
57 switch_to_thread (found);
59 return (current_thread != NULL);
62 /* See target.h. */
64 bool
65 set_desired_process ()
67 client_state &cs = get_client_state ();
69 process_info *proc = find_process_pid (cs.general_thread.pid ());
70 if (proc == nullptr)
72 threads_debug_printf
73 ("did not find process for general_thread %s",
74 cs.general_thread.to_string ().c_str ());
76 switch_to_process (proc);
78 return proc != nullptr;
81 /* See target.h. */
83 int
84 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
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;
93 int res = the_target->read_memory (memaddr, myaddr, len);
94 check_mem_read (memaddr, myaddr, len);
95 return res;
98 /* See target/target.h. */
101 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
103 return read_inferior_memory (memaddr, myaddr, len);
106 /* See target/target.h. */
109 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
111 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
114 /* See target/target.h. */
117 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
118 ssize_t len)
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;
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);
134 ptid_t
135 mywait (ptid_t ptid, struct target_waitstatus *ourstatus,
136 target_wait_flags options, int connected_wait)
138 ptid_t ret;
140 if (connected_wait)
141 server_waiting = 1;
143 ret = target_wait (ptid, ourstatus, options);
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);
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 ())
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 ()));
167 if (connected_wait)
168 server_waiting = 0;
170 return ret;
173 /* See target/target.h. */
175 void
176 target_stop_and_wait (ptid_t ptid)
178 struct target_waitstatus status;
179 bool was_non_stop = non_stop;
180 struct thread_resume resume_info;
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);
187 non_stop = true;
188 mywait (ptid, &status, 0, 0);
189 non_stop = was_non_stop;
192 /* See target/target.h. */
194 ptid_t
195 target_wait (ptid_t ptid, struct target_waitstatus *status,
196 target_wait_flags options)
198 return the_target->wait (ptid, status, options);
201 /* See target/target.h. */
203 void
204 target_mourn_inferior (ptid_t ptid)
206 the_target->mourn (find_process_pid (ptid.pid ()));
209 /* See target/target.h. */
211 void
212 target_continue_no_signal (ptid_t ptid)
214 struct thread_resume resume_info;
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);
222 /* See target/target.h. */
224 void
225 target_continue (ptid_t ptid, enum gdb_signal signal)
227 struct thread_resume resume_info;
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);
235 /* See target/target.h. */
238 target_supports_multi_process (void)
240 return the_target->supports_multi_process ();
243 void
244 set_target_ops (process_stratum_target *target)
246 the_target = target;
249 /* Convert pid to printable format. */
251 std::string
252 target_pid_to_str (ptid_t ptid)
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 ());
271 kill_inferior (process_info *proc)
273 gdb_agent_about_to_close (proc->pid);
275 return the_target->kill (proc);
278 /* Define it. */
280 target_terminal_state target_terminal::m_terminal_state
281 = target_terminal_state::is_ours;
283 /* See target/target.h. */
285 void
286 target_terminal::init ()
288 /* Placeholder needed because of fork_inferior. Not necessary on
289 GDBserver. */
292 /* See target/target.h. */
294 void
295 target_terminal::inferior ()
297 /* Placeholder needed because of fork_inferior. Not necessary on
298 GDBserver. */
301 /* See target/target.h. */
303 void
304 target_terminal::ours ()
306 /* Placeholder needed because of fork_inferior. Not necessary on
307 GDBserver. */
310 /* See target/target.h. */
312 void
313 target_terminal::ours_for_output (void)
315 /* Placeholder. */
318 /* See target/target.h. */
320 void
321 target_terminal::info (const char *arg, int from_tty)
323 /* Placeholder. */
326 /* Default implementations of target ops.
327 See target.h for definitions. */
329 void
330 process_stratum_target::post_create_inferior ()
332 /* Nop. */
335 void
336 process_stratum_target::look_up_symbols ()
338 /* Nop. */
341 bool
342 process_stratum_target::supports_read_auxv ()
344 return false;
348 process_stratum_target::read_auxv (int pid, CORE_ADDR offset,
349 unsigned char *myaddr, unsigned int len)
351 gdb_assert_not_reached ("target op read_auxv not supported");
354 bool
355 process_stratum_target::supports_z_point_type (char z_type)
357 return false;
361 process_stratum_target::insert_point (enum raw_bkpt_type type,
362 CORE_ADDR addr,
363 int size, raw_breakpoint *bp)
365 return 1;
369 process_stratum_target::remove_point (enum raw_bkpt_type type,
370 CORE_ADDR addr,
371 int size, raw_breakpoint *bp)
373 return 1;
376 bool
377 process_stratum_target::stopped_by_sw_breakpoint ()
379 return false;
382 bool
383 process_stratum_target::supports_stopped_by_sw_breakpoint ()
385 return false;
388 bool
389 process_stratum_target::stopped_by_hw_breakpoint ()
391 return false;
394 bool
395 process_stratum_target::supports_stopped_by_hw_breakpoint ()
397 return false;
400 bool
401 process_stratum_target::supports_hardware_single_step ()
403 return false;
406 bool
407 process_stratum_target::stopped_by_watchpoint ()
409 return false;
412 CORE_ADDR
413 process_stratum_target::stopped_data_address ()
415 return 0;
418 bool
419 process_stratum_target::supports_read_offsets ()
421 return false;
424 bool
425 process_stratum_target::supports_memory_tagging ()
427 return false;
430 bool
431 process_stratum_target::fetch_memtags (CORE_ADDR address, size_t len,
432 gdb::byte_vector &tags, int type)
434 gdb_assert_not_reached ("target op fetch_memtags not supported");
437 bool
438 process_stratum_target::store_memtags (CORE_ADDR address, size_t len,
439 const gdb::byte_vector &tags, int type)
441 gdb_assert_not_reached ("target op store_memtags not supported");
445 process_stratum_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
447 gdb_assert_not_reached ("target op read_offsets not supported");
450 bool
451 process_stratum_target::supports_get_tls_address ()
453 return false;
457 process_stratum_target::get_tls_address (thread_info *thread,
458 CORE_ADDR offset,
459 CORE_ADDR load_module,
460 CORE_ADDR *address)
462 gdb_assert_not_reached ("target op get_tls_address not supported");
465 bool
466 process_stratum_target::supports_qxfer_osdata ()
468 return false;
472 process_stratum_target::qxfer_osdata (const char *annex,
473 unsigned char *readbuf,
474 unsigned const char *writebuf,
475 CORE_ADDR offset, int len)
477 gdb_assert_not_reached ("target op qxfer_osdata not supported");
480 bool
481 process_stratum_target::supports_qxfer_siginfo ()
483 return false;
487 process_stratum_target::qxfer_siginfo (const char *annex,
488 unsigned char *readbuf,
489 unsigned const char *writebuf,
490 CORE_ADDR offset, int len)
492 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
495 bool
496 process_stratum_target::supports_non_stop ()
498 return false;
501 bool
502 process_stratum_target::async (bool enable)
504 return false;
508 process_stratum_target::start_non_stop (bool enable)
510 if (enable)
511 return -1;
512 else
513 return 0;
516 bool
517 process_stratum_target::supports_multi_process ()
519 return false;
522 bool
523 process_stratum_target::supports_fork_events ()
525 return false;
528 bool
529 process_stratum_target::supports_vfork_events ()
531 return false;
534 gdb_thread_options
535 process_stratum_target::supported_thread_options ()
537 return 0;
540 bool
541 process_stratum_target::supports_exec_events ()
543 return false;
546 void
547 process_stratum_target::handle_new_gdb_connection ()
549 /* Nop. */
553 process_stratum_target::handle_monitor_command (char *mon)
555 return 0;
559 process_stratum_target::core_of_thread (ptid_t ptid)
561 return -1;
564 bool
565 process_stratum_target::supports_read_loadmap ()
567 return false;
571 process_stratum_target::read_loadmap (const char *annex,
572 CORE_ADDR offset,
573 unsigned char *myaddr,
574 unsigned int len)
576 gdb_assert_not_reached ("target op read_loadmap not supported");
579 void
580 process_stratum_target::process_qsupported
581 (gdb::array_view<const char * const> features)
583 /* Nop. */
586 bool
587 process_stratum_target::supports_tracepoints ()
589 return false;
592 CORE_ADDR
593 process_stratum_target::read_pc (regcache *regcache)
595 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
598 void
599 process_stratum_target::write_pc (regcache *regcache, CORE_ADDR pc)
601 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
604 bool
605 process_stratum_target::supports_thread_stopped ()
607 return false;
610 bool
611 process_stratum_target::thread_stopped (thread_info *thread)
613 gdb_assert_not_reached ("target op thread_stopped not supported");
616 bool
617 process_stratum_target::any_resumed ()
619 return true;
622 bool
623 process_stratum_target::supports_get_tib_address ()
625 return false;
629 process_stratum_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
631 gdb_assert_not_reached ("target op get_tib_address not supported");
634 void
635 process_stratum_target::pause_all (bool freeze)
637 /* Nop. */
640 void
641 process_stratum_target::unpause_all (bool unfreeze)
643 /* Nop. */
646 void
647 process_stratum_target::stabilize_threads ()
649 /* Nop. */
652 bool
653 process_stratum_target::supports_fast_tracepoints ()
655 return false;
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)
667 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
668 "not supported");
672 process_stratum_target::get_min_fast_tracepoint_insn_len ()
674 return 0;
677 struct emit_ops *
678 process_stratum_target::emit_ops ()
680 return nullptr;
683 bool
684 process_stratum_target::supports_disable_randomization ()
686 return false;
689 bool
690 process_stratum_target::supports_qxfer_libraries_svr4 ()
692 return false;
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)
701 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
704 bool
705 process_stratum_target::supports_agent ()
707 return false;
710 bool
711 process_stratum_target::supports_btrace ()
713 return false;
716 btrace_target_info *
717 process_stratum_target::enable_btrace (thread_info *tp,
718 const btrace_config *conf)
720 error (_("Target does not support branch tracing."));
724 process_stratum_target::disable_btrace (btrace_target_info *tinfo)
726 error (_("Target does not support branch tracing."));
730 process_stratum_target::read_btrace (btrace_target_info *tinfo,
731 std::string *buffer,
732 enum btrace_read_type type)
734 error (_("Target does not support branch tracing."));
738 process_stratum_target::read_btrace_conf (const btrace_target_info *tinfo,
739 std::string *buffer)
741 error (_("Target does not support branch tracing."));
744 bool
745 process_stratum_target::supports_range_stepping ()
747 return false;
750 bool
751 process_stratum_target::supports_pid_to_exec_file ()
753 return false;
756 const char *
757 process_stratum_target::pid_to_exec_file (int pid)
759 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
762 bool
763 process_stratum_target::supports_multifs ()
765 return false;
769 process_stratum_target::multifs_open (int pid, const char *filename,
770 int flags, mode_t mode)
772 return open (filename, flags, mode);
776 process_stratum_target::multifs_unlink (int pid, const char *filename)
778 return unlink (filename);
781 ssize_t
782 process_stratum_target::multifs_readlink (int pid, const char *filename,
783 char *buf, size_t bufsiz)
785 return readlink (filename, buf, bufsiz);
789 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
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;
799 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
801 return breakpoint_kind_from_pc (pcptr);
804 const char *
805 process_stratum_target::thread_name (ptid_t thread)
807 return nullptr;
810 bool
811 process_stratum_target::thread_handle (ptid_t ptid, gdb_byte **handle,
812 int *handle_len)
814 return false;
817 thread_info *
818 process_stratum_target::thread_pending_parent (thread_info *thread)
820 return nullptr;
823 thread_info *
824 process_stratum_target::thread_pending_child (thread_info *thread,
825 target_waitkind *kind)
827 return nullptr;
830 bool
831 process_stratum_target::supports_software_single_step ()
833 return false;
836 bool
837 process_stratum_target::supports_catch_syscall ()
839 return false;
843 process_stratum_target::get_ipa_tdesc_idx ()
845 return 0;