Automatic date update in version.in
[binutils-gdb.git] / gdbserver / target.cc
blob23b699d33bb94fa5102158b6db95f035ed2d58ef
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 "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>
30 process_stratum_target *the_target;
32 /* See target.h. */
34 bool
35 set_desired_thread ()
37 client_state &cs = get_client_state ();
38 thread_info *found = find_thread_ptid (cs.general_thread);
40 if (found == nullptr)
42 process_info *proc = find_process_pid (cs.general_thread.pid ());
43 if (proc == nullptr)
45 threads_debug_printf
46 ("did not find thread nor process for general_thread %s",
47 cs.general_thread.to_string ().c_str ());
49 else
51 threads_debug_printf
52 ("did not find thread for general_thread %s, but found process",
53 cs.general_thread.to_string ().c_str ());
55 switch_to_process (proc);
57 else
58 switch_to_thread (found);
60 return (current_thread != NULL);
63 /* See target.h. */
65 bool
66 set_desired_process ()
68 client_state &cs = get_client_state ();
70 process_info *proc = find_process_pid (cs.general_thread.pid ());
71 if (proc == nullptr)
73 threads_debug_printf
74 ("did not find process for general_thread %s",
75 cs.general_thread.to_string ().c_str ());
77 switch_to_process (proc);
79 return proc != nullptr;
82 /* See target.h. */
84 int
85 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
87 /* At the time of writing, GDB only sends write packets with LEN==0,
88 not read packets (see comment in target_write_memory), but it
89 doesn't hurt to prevent problems if it ever does, or we're
90 connected to some client other than GDB that does. */
91 if (len == 0)
92 return 0;
94 int res = the_target->read_memory (memaddr, myaddr, len);
95 check_mem_read (memaddr, myaddr, len);
96 return res;
99 /* See target/target.h. */
102 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
104 return read_inferior_memory (memaddr, myaddr, len);
107 /* See target/target.h. */
110 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
112 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
115 /* See target/target.h. */
118 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
119 ssize_t len)
121 /* GDB may send X packets with LEN==0, for probing packet support.
122 If we let such a request go through, then buffer.data() below may
123 return NULL, which may confuse target implementations. Handle it
124 here to avoid lower levels having to care about this case. */
125 if (len == 0)
126 return 0;
128 /* Make a copy of the data because check_mem_write may need to
129 update it. */
130 gdb::byte_vector buffer (myaddr, myaddr + len);
131 check_mem_write (memaddr, buffer.data (), myaddr, len);
132 return the_target->write_memory (memaddr, buffer.data (), len);
135 ptid_t
136 mywait (ptid_t ptid, struct target_waitstatus *ourstatus,
137 target_wait_flags options, int connected_wait)
139 ptid_t ret;
141 if (connected_wait)
142 server_waiting = 1;
144 ret = target_wait (ptid, ourstatus, options);
146 /* We don't expose _LOADED events to gdbserver core. See the
147 `dlls_changed' global. */
148 if (ourstatus->kind () == TARGET_WAITKIND_LOADED)
149 ourstatus->set_stopped (GDB_SIGNAL_0);
151 /* If GDB is connected through TCP/serial, then GDBserver will most
152 probably be running on its own terminal/console, so it's nice to
153 print there why is GDBserver exiting. If however, GDB is
154 connected through stdio, then there's no need to spam the GDB
155 console with this -- the user will already see the exit through
156 regular GDB output, in that same terminal. */
157 if (!remote_connection_is_stdio ())
159 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
160 fprintf (stderr,
161 "\nChild exited with status %d\n", ourstatus->exit_status ());
162 else if (ourstatus->kind () == TARGET_WAITKIND_SIGNALLED)
163 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
164 gdb_signal_to_host (ourstatus->sig ()),
165 gdb_signal_to_name (ourstatus->sig ()));
168 if (connected_wait)
169 server_waiting = 0;
171 return ret;
174 /* See target/target.h. */
176 void
177 target_stop_and_wait (ptid_t ptid)
179 struct target_waitstatus status;
180 bool was_non_stop = non_stop;
181 struct thread_resume resume_info;
183 resume_info.thread = ptid;
184 resume_info.kind = resume_stop;
185 resume_info.sig = GDB_SIGNAL_0;
186 the_target->resume (&resume_info, 1);
188 non_stop = true;
189 mywait (ptid, &status, 0, 0);
190 non_stop = was_non_stop;
193 /* See target/target.h. */
195 ptid_t
196 target_wait (ptid_t ptid, struct target_waitstatus *status,
197 target_wait_flags options)
199 return the_target->wait (ptid, status, options);
202 /* See target/target.h. */
204 void
205 target_mourn_inferior (ptid_t ptid)
207 the_target->mourn (find_process_pid (ptid.pid ()));
210 /* See target/target.h. */
212 void
213 target_continue_no_signal (ptid_t ptid)
215 struct thread_resume resume_info;
217 resume_info.thread = ptid;
218 resume_info.kind = resume_continue;
219 resume_info.sig = GDB_SIGNAL_0;
220 the_target->resume (&resume_info, 1);
223 /* See target/target.h. */
225 void
226 target_continue (ptid_t ptid, enum gdb_signal signal)
228 struct thread_resume resume_info;
230 resume_info.thread = ptid;
231 resume_info.kind = resume_continue;
232 resume_info.sig = gdb_signal_to_host (signal);
233 the_target->resume (&resume_info, 1);
236 /* See target/target.h. */
239 target_supports_multi_process (void)
241 return the_target->supports_multi_process ();
244 void
245 set_target_ops (process_stratum_target *target)
247 the_target = target;
250 /* Convert pid to printable format. */
252 std::string
253 target_pid_to_str (ptid_t ptid)
255 if (ptid == minus_one_ptid)
256 return string_printf("<all threads>");
257 else if (ptid == null_ptid)
258 return string_printf("<null thread>");
259 else if (ptid.tid () != 0)
260 return string_printf("Thread %d.0x%s",
261 ptid.pid (),
262 phex_nz (ptid.tid (), sizeof (ULONGEST)));
263 else if (ptid.lwp () != 0)
264 return string_printf("LWP %d.%ld",
265 ptid.pid (), ptid.lwp ());
266 else
267 return string_printf("Process %d",
268 ptid.pid ());
272 kill_inferior (process_info *proc)
274 gdb_agent_about_to_close (proc->pid);
276 return the_target->kill (proc);
279 /* Define it. */
281 target_terminal_state target_terminal::m_terminal_state
282 = target_terminal_state::is_ours;
284 /* See target/target.h. */
286 void
287 target_terminal::init ()
289 /* Placeholder needed because of fork_inferior. Not necessary on
290 GDBserver. */
293 /* See target/target.h. */
295 void
296 target_terminal::inferior ()
298 /* Placeholder needed because of fork_inferior. Not necessary on
299 GDBserver. */
302 /* See target/target.h. */
304 void
305 target_terminal::ours ()
307 /* Placeholder needed because of fork_inferior. Not necessary on
308 GDBserver. */
311 /* See target/target.h. */
313 void
314 target_terminal::ours_for_output (void)
316 /* Placeholder. */
319 /* See target/target.h. */
321 void
322 target_terminal::info (const char *arg, int from_tty)
324 /* Placeholder. */
327 /* Default implementations of target ops.
328 See target.h for definitions. */
330 void
331 process_stratum_target::post_create_inferior ()
333 /* Nop. */
336 void
337 process_stratum_target::look_up_symbols ()
339 /* Nop. */
342 bool
343 process_stratum_target::supports_read_auxv ()
345 return false;
349 process_stratum_target::read_auxv (int pid, CORE_ADDR offset,
350 unsigned char *myaddr, unsigned int len)
352 gdb_assert_not_reached ("target op read_auxv not supported");
355 bool
356 process_stratum_target::supports_z_point_type (char z_type)
358 return false;
362 process_stratum_target::insert_point (enum raw_bkpt_type type,
363 CORE_ADDR addr,
364 int size, raw_breakpoint *bp)
366 return 1;
370 process_stratum_target::remove_point (enum raw_bkpt_type type,
371 CORE_ADDR addr,
372 int size, raw_breakpoint *bp)
374 return 1;
377 bool
378 process_stratum_target::stopped_by_sw_breakpoint ()
380 return false;
383 bool
384 process_stratum_target::supports_stopped_by_sw_breakpoint ()
386 return false;
389 bool
390 process_stratum_target::stopped_by_hw_breakpoint ()
392 return false;
395 bool
396 process_stratum_target::supports_stopped_by_hw_breakpoint ()
398 return false;
401 bool
402 process_stratum_target::supports_hardware_single_step ()
404 return false;
407 bool
408 process_stratum_target::stopped_by_watchpoint ()
410 return false;
413 CORE_ADDR
414 process_stratum_target::stopped_data_address ()
416 return 0;
419 bool
420 process_stratum_target::supports_read_offsets ()
422 return false;
425 bool
426 process_stratum_target::supports_memory_tagging ()
428 return false;
431 bool
432 process_stratum_target::fetch_memtags (CORE_ADDR address, size_t len,
433 gdb::byte_vector &tags, int type)
435 gdb_assert_not_reached ("target op fetch_memtags not supported");
438 bool
439 process_stratum_target::store_memtags (CORE_ADDR address, size_t len,
440 const gdb::byte_vector &tags, int type)
442 gdb_assert_not_reached ("target op store_memtags not supported");
446 process_stratum_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
448 gdb_assert_not_reached ("target op read_offsets not supported");
451 bool
452 process_stratum_target::supports_get_tls_address ()
454 return false;
458 process_stratum_target::get_tls_address (thread_info *thread,
459 CORE_ADDR offset,
460 CORE_ADDR load_module,
461 CORE_ADDR *address)
463 gdb_assert_not_reached ("target op get_tls_address not supported");
466 bool
467 process_stratum_target::supports_qxfer_osdata ()
469 return false;
473 process_stratum_target::qxfer_osdata (const char *annex,
474 unsigned char *readbuf,
475 unsigned const char *writebuf,
476 CORE_ADDR offset, int len)
478 gdb_assert_not_reached ("target op qxfer_osdata not supported");
481 bool
482 process_stratum_target::supports_qxfer_siginfo ()
484 return false;
488 process_stratum_target::qxfer_siginfo (const char *annex,
489 unsigned char *readbuf,
490 unsigned const char *writebuf,
491 CORE_ADDR offset, int len)
493 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
496 bool
497 process_stratum_target::supports_non_stop ()
499 return false;
502 bool
503 process_stratum_target::async (bool enable)
505 return false;
509 process_stratum_target::start_non_stop (bool enable)
511 if (enable)
512 return -1;
513 else
514 return 0;
517 bool
518 process_stratum_target::supports_multi_process ()
520 return false;
523 bool
524 process_stratum_target::supports_fork_events ()
526 return false;
529 bool
530 process_stratum_target::supports_vfork_events ()
532 return false;
535 gdb_thread_options
536 process_stratum_target::supported_thread_options ()
538 return 0;
541 bool
542 process_stratum_target::supports_exec_events ()
544 return false;
547 void
548 process_stratum_target::handle_new_gdb_connection ()
550 /* Nop. */
554 process_stratum_target::handle_monitor_command (char *mon)
556 return 0;
560 process_stratum_target::core_of_thread (ptid_t ptid)
562 return -1;
565 bool
566 process_stratum_target::supports_read_loadmap ()
568 return false;
572 process_stratum_target::read_loadmap (const char *annex,
573 CORE_ADDR offset,
574 unsigned char *myaddr,
575 unsigned int len)
577 gdb_assert_not_reached ("target op read_loadmap not supported");
580 void
581 process_stratum_target::process_qsupported
582 (gdb::array_view<const char * const> features)
584 /* Nop. */
587 bool
588 process_stratum_target::supports_tracepoints ()
590 return false;
593 CORE_ADDR
594 process_stratum_target::read_pc (regcache *regcache)
596 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
599 void
600 process_stratum_target::write_pc (regcache *regcache, CORE_ADDR pc)
602 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
605 bool
606 process_stratum_target::supports_thread_stopped ()
608 return false;
611 bool
612 process_stratum_target::thread_stopped (thread_info *thread)
614 gdb_assert_not_reached ("target op thread_stopped not supported");
617 bool
618 process_stratum_target::any_resumed ()
620 return true;
623 bool
624 process_stratum_target::supports_get_tib_address ()
626 return false;
630 process_stratum_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
632 gdb_assert_not_reached ("target op get_tib_address not supported");
635 void
636 process_stratum_target::pause_all (bool freeze)
638 /* Nop. */
641 void
642 process_stratum_target::unpause_all (bool unfreeze)
644 /* Nop. */
647 void
648 process_stratum_target::stabilize_threads ()
650 /* Nop. */
653 bool
654 process_stratum_target::supports_fast_tracepoints ()
656 return false;
660 process_stratum_target::install_fast_tracepoint_jump_pad
661 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
662 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
663 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
664 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
665 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
666 char *err)
668 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
669 "not supported");
673 process_stratum_target::get_min_fast_tracepoint_insn_len ()
675 return 0;
678 struct emit_ops *
679 process_stratum_target::emit_ops ()
681 return nullptr;
684 bool
685 process_stratum_target::supports_disable_randomization ()
687 return false;
690 bool
691 process_stratum_target::supports_qxfer_libraries_svr4 ()
693 return false;
697 process_stratum_target::qxfer_libraries_svr4 (const char *annex,
698 unsigned char *readbuf,
699 unsigned const char *writebuf,
700 CORE_ADDR offset, int len)
702 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
705 bool
706 process_stratum_target::supports_agent ()
708 return false;
711 bool
712 process_stratum_target::supports_btrace ()
714 return false;
717 btrace_target_info *
718 process_stratum_target::enable_btrace (thread_info *tp,
719 const btrace_config *conf)
721 error (_("Target does not support branch tracing."));
725 process_stratum_target::disable_btrace (btrace_target_info *tinfo)
727 error (_("Target does not support branch tracing."));
731 process_stratum_target::read_btrace (btrace_target_info *tinfo,
732 std::string *buffer,
733 enum btrace_read_type type)
735 error (_("Target does not support branch tracing."));
739 process_stratum_target::read_btrace_conf (const btrace_target_info *tinfo,
740 std::string *buffer)
742 error (_("Target does not support branch tracing."));
745 bool
746 process_stratum_target::supports_range_stepping ()
748 return false;
751 bool
752 process_stratum_target::supports_pid_to_exec_file ()
754 return false;
757 const char *
758 process_stratum_target::pid_to_exec_file (int pid)
760 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
763 bool
764 process_stratum_target::supports_multifs ()
766 return false;
770 process_stratum_target::multifs_open (int pid, const char *filename,
771 int flags, mode_t mode)
773 return open (filename, flags, mode);
777 process_stratum_target::multifs_unlink (int pid, const char *filename)
779 return unlink (filename);
782 ssize_t
783 process_stratum_target::multifs_readlink (int pid, const char *filename,
784 char *buf, size_t bufsiz)
786 return readlink (filename, buf, bufsiz);
790 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
792 /* The default behavior is to use the size of a breakpoint as the
793 kind. */
794 int size = 0;
795 sw_breakpoint_from_kind (0, &size);
796 return size;
800 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
802 return breakpoint_kind_from_pc (pcptr);
805 const char *
806 process_stratum_target::thread_name (ptid_t thread)
808 return nullptr;
811 bool
812 process_stratum_target::thread_handle (ptid_t ptid, gdb_byte **handle,
813 int *handle_len)
815 return false;
818 thread_info *
819 process_stratum_target::thread_pending_parent (thread_info *thread)
821 return nullptr;
824 thread_info *
825 process_stratum_target::thread_pending_child (thread_info *thread,
826 target_waitkind *kind)
828 return nullptr;
831 bool
832 process_stratum_target::supports_software_single_step ()
834 return false;
837 bool
838 process_stratum_target::supports_catch_syscall ()
840 return false;
844 process_stratum_target::get_ipa_tdesc_idx ()
846 return 0;