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"
26 #include <sys/types.h>
29 process_stratum_target
*the_target
;
36 client_state
&cs
= get_client_state ();
37 thread_info
*found
= find_thread_ptid (cs
.general_thread
);
41 process_info
*proc
= find_process_pid (cs
.general_thread
.pid ());
45 ("did not find thread nor process for general_thread %s",
46 cs
.general_thread
.to_string ().c_str ());
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
);
57 switch_to_thread (found
);
59 return (current_thread
!= NULL
);
65 set_desired_process ()
67 client_state
&cs
= get_client_state ();
69 process_info
*proc
= find_process_pid (cs
.general_thread
.pid ());
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;
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. */
93 int res
= the_target
->read_memory (memaddr
, myaddr
, len
);
94 check_mem_read (memaddr
, myaddr
, len
);
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
,
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. */
127 /* Make a copy of the data because check_mem_write may need to
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
);
135 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
136 target_wait_flags options
, int connected_wait
)
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
)
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 ()));
173 /* See target/target.h. */
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);
188 mywait (ptid
, &status
, 0, 0);
189 non_stop
= was_non_stop
;
192 /* See target/target.h. */
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. */
204 target_mourn_inferior (ptid_t ptid
)
206 the_target
->mourn (find_process_pid (ptid
.pid ()));
209 /* See target/target.h. */
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. */
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 ();
244 set_target_ops (process_stratum_target
*target
)
249 /* Convert pid to printable format. */
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",
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 ());
266 return string_printf("Process %d",
271 kill_inferior (process_info
*proc
)
273 gdb_agent_about_to_close (proc
->pid
);
275 return the_target
->kill (proc
);
280 target_terminal_state
target_terminal::m_terminal_state
281 = target_terminal_state::is_ours
;
283 /* See target/target.h. */
286 target_terminal::init ()
288 /* Placeholder needed because of fork_inferior. Not necessary on
292 /* See target/target.h. */
295 target_terminal::inferior ()
297 /* Placeholder needed because of fork_inferior. Not necessary on
301 /* See target/target.h. */
304 target_terminal::ours ()
306 /* Placeholder needed because of fork_inferior. Not necessary on
310 /* See target/target.h. */
313 target_terminal::ours_for_output (void)
318 /* See target/target.h. */
321 target_terminal::info (const char *arg
, int from_tty
)
326 /* Default implementations of target ops.
327 See target.h for definitions. */
330 process_stratum_target::post_create_inferior ()
336 process_stratum_target::look_up_symbols ()
342 process_stratum_target::supports_read_auxv ()
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");
355 process_stratum_target::supports_z_point_type (char z_type
)
361 process_stratum_target::insert_point (enum raw_bkpt_type type
,
363 int size
, raw_breakpoint
*bp
)
369 process_stratum_target::remove_point (enum raw_bkpt_type type
,
371 int size
, raw_breakpoint
*bp
)
377 process_stratum_target::stopped_by_sw_breakpoint ()
383 process_stratum_target::supports_stopped_by_sw_breakpoint ()
389 process_stratum_target::stopped_by_hw_breakpoint ()
395 process_stratum_target::supports_stopped_by_hw_breakpoint ()
401 process_stratum_target::supports_hardware_single_step ()
407 process_stratum_target::stopped_by_watchpoint ()
413 process_stratum_target::stopped_data_address ()
419 process_stratum_target::supports_read_offsets ()
425 process_stratum_target::supports_memory_tagging ()
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");
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");
451 process_stratum_target::supports_get_tls_address ()
457 process_stratum_target::get_tls_address (thread_info
*thread
,
459 CORE_ADDR load_module
,
462 gdb_assert_not_reached ("target op get_tls_address not supported");
466 process_stratum_target::supports_qxfer_osdata ()
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");
481 process_stratum_target::supports_qxfer_siginfo ()
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");
496 process_stratum_target::supports_non_stop ()
502 process_stratum_target::async (bool enable
)
508 process_stratum_target::start_non_stop (bool enable
)
517 process_stratum_target::supports_multi_process ()
523 process_stratum_target::supports_fork_events ()
529 process_stratum_target::supports_vfork_events ()
535 process_stratum_target::supported_thread_options ()
541 process_stratum_target::supports_exec_events ()
547 process_stratum_target::handle_new_gdb_connection ()
553 process_stratum_target::handle_monitor_command (char *mon
)
559 process_stratum_target::core_of_thread (ptid_t ptid
)
565 process_stratum_target::supports_read_loadmap ()
571 process_stratum_target::read_loadmap (const char *annex
,
573 unsigned char *myaddr
,
576 gdb_assert_not_reached ("target op read_loadmap not supported");
580 process_stratum_target::process_qsupported
581 (gdb::array_view
<const char * const> features
)
587 process_stratum_target::supports_tracepoints ()
593 process_stratum_target::read_pc (regcache
*regcache
)
595 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
599 process_stratum_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
601 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
605 process_stratum_target::supports_thread_stopped ()
611 process_stratum_target::thread_stopped (thread_info
*thread
)
613 gdb_assert_not_reached ("target op thread_stopped not supported");
617 process_stratum_target::any_resumed ()
623 process_stratum_target::supports_get_tib_address ()
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");
635 process_stratum_target::pause_all (bool freeze
)
641 process_stratum_target::unpause_all (bool unfreeze
)
647 process_stratum_target::stabilize_threads ()
653 process_stratum_target::supports_fast_tracepoints ()
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
,
667 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
672 process_stratum_target::get_min_fast_tracepoint_insn_len ()
678 process_stratum_target::emit_ops ()
684 process_stratum_target::supports_disable_randomization ()
690 process_stratum_target::supports_qxfer_libraries_svr4 ()
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");
705 process_stratum_target::supports_agent ()
711 process_stratum_target::supports_btrace ()
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
,
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
,
741 error (_("Target does not support branch tracing."));
745 process_stratum_target::supports_range_stepping ()
751 process_stratum_target::supports_pid_to_exec_file ()
757 process_stratum_target::pid_to_exec_file (int pid
)
759 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
763 process_stratum_target::supports_multifs ()
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
);
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
794 sw_breakpoint_from_kind (0, &size
);
799 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
801 return breakpoint_kind_from_pc (pcptr
);
805 process_stratum_target::thread_name (ptid_t thread
)
811 process_stratum_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
818 process_stratum_target::thread_pending_parent (thread_info
*thread
)
824 process_stratum_target::thread_pending_child (thread_info
*thread
,
825 target_waitkind
*kind
)
831 process_stratum_target::supports_software_single_step ()
837 process_stratum_target::supports_catch_syscall ()
843 process_stratum_target::get_ipa_tdesc_idx ()