1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2022 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/>. */
22 #include "tracepoint.h"
23 #include "gdbsupport/byte-vector.h"
27 #include <sys/types.h>
30 process_stratum_target
*the_target
;
37 client_state
&cs
= get_client_state ();
38 thread_info
*found
= find_thread_ptid (cs
.general_thread
);
42 process_info
*proc
= find_process_pid (cs
.general_thread
.pid ());
46 ("did not find thread nor process for general_thread %s",
47 cs
.general_thread
.to_string ().c_str ());
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
);
58 switch_to_thread (found
);
60 return (current_thread
!= NULL
);
66 set_desired_process ()
68 client_state
&cs
= get_client_state ();
70 process_info
*proc
= find_process_pid (cs
.general_thread
.pid ());
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;
83 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
85 /* At the time of writing, GDB only sends write packets with LEN==0,
86 not read packets (see comment in target_write_memory), but it
87 doesn't hurt to prevent problems if it ever does, or we're
88 connected to some client other than GDB that does. */
92 int res
= the_target
->read_memory (memaddr
, myaddr
, len
);
93 check_mem_read (memaddr
, myaddr
, len
);
97 /* See target/target.h. */
100 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
102 return read_inferior_memory (memaddr
, myaddr
, len
);
105 /* See target/target.h. */
108 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
110 return read_inferior_memory (memaddr
, (gdb_byte
*) result
, sizeof (*result
));
113 /* See target/target.h. */
116 target_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
119 /* GDB may send X packets with LEN==0, for probing packet support.
120 If we let such a request go through, then buffer.data() below may
121 return NULL, which may confuse target implementations. Handle it
122 here to avoid lower levels having to care about this case. */
126 /* Make a copy of the data because check_mem_write may need to
128 gdb::byte_vector
buffer (myaddr
, myaddr
+ len
);
129 check_mem_write (memaddr
, buffer
.data (), myaddr
, len
);
130 return the_target
->write_memory (memaddr
, buffer
.data (), len
);
134 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
135 target_wait_flags options
, int connected_wait
)
142 ret
= target_wait (ptid
, ourstatus
, options
);
144 /* We don't expose _LOADED events to gdbserver core. See the
145 `dlls_changed' global. */
146 if (ourstatus
->kind () == TARGET_WAITKIND_LOADED
)
147 ourstatus
->set_stopped (GDB_SIGNAL_0
);
149 /* If GDB is connected through TCP/serial, then GDBserver will most
150 probably be running on its own terminal/console, so it's nice to
151 print there why is GDBserver exiting. If however, GDB is
152 connected through stdio, then there's no need to spam the GDB
153 console with this -- the user will already see the exit through
154 regular GDB output, in that same terminal. */
155 if (!remote_connection_is_stdio ())
157 if (ourstatus
->kind () == TARGET_WAITKIND_EXITED
)
159 "\nChild exited with status %d\n", ourstatus
->exit_status ());
160 else if (ourstatus
->kind () == TARGET_WAITKIND_SIGNALLED
)
161 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
162 gdb_signal_to_host (ourstatus
->sig ()),
163 gdb_signal_to_name (ourstatus
->sig ()));
172 /* See target/target.h. */
175 target_stop_and_wait (ptid_t ptid
)
177 struct target_waitstatus status
;
178 bool was_non_stop
= non_stop
;
179 struct thread_resume resume_info
;
181 resume_info
.thread
= ptid
;
182 resume_info
.kind
= resume_stop
;
183 resume_info
.sig
= GDB_SIGNAL_0
;
184 the_target
->resume (&resume_info
, 1);
187 mywait (ptid
, &status
, 0, 0);
188 non_stop
= was_non_stop
;
191 /* See target/target.h. */
194 target_wait (ptid_t ptid
, struct target_waitstatus
*status
,
195 target_wait_flags options
)
197 return the_target
->wait (ptid
, status
, options
);
200 /* See target/target.h. */
203 target_mourn_inferior (ptid_t ptid
)
205 the_target
->mourn (find_process_pid (ptid
.pid ()));
208 /* See target/target.h. */
211 target_continue_no_signal (ptid_t ptid
)
213 struct thread_resume resume_info
;
215 resume_info
.thread
= ptid
;
216 resume_info
.kind
= resume_continue
;
217 resume_info
.sig
= GDB_SIGNAL_0
;
218 the_target
->resume (&resume_info
, 1);
221 /* See target/target.h. */
224 target_continue (ptid_t ptid
, enum gdb_signal signal
)
226 struct thread_resume resume_info
;
228 resume_info
.thread
= ptid
;
229 resume_info
.kind
= resume_continue
;
230 resume_info
.sig
= gdb_signal_to_host (signal
);
231 the_target
->resume (&resume_info
, 1);
234 /* See target/target.h. */
237 target_supports_multi_process (void)
239 return the_target
->supports_multi_process ();
243 set_target_ops (process_stratum_target
*target
)
248 /* Convert pid to printable format. */
251 target_pid_to_str (ptid_t ptid
)
253 if (ptid
== minus_one_ptid
)
254 return string_printf("<all threads>");
255 else if (ptid
== null_ptid
)
256 return string_printf("<null thread>");
257 else if (ptid
.tid () != 0)
258 return string_printf("Thread %d.0x%s",
260 phex_nz (ptid
.tid (), sizeof (ULONGEST
)));
261 else if (ptid
.lwp () != 0)
262 return string_printf("LWP %d.%ld",
263 ptid
.pid (), ptid
.lwp ());
265 return string_printf("Process %d",
270 kill_inferior (process_info
*proc
)
272 gdb_agent_about_to_close (proc
->pid
);
274 return the_target
->kill (proc
);
279 target_terminal_state
target_terminal::m_terminal_state
280 = target_terminal_state::is_ours
;
282 /* See target/target.h. */
285 target_terminal::init ()
287 /* Placeholder needed because of fork_inferior. Not necessary on
291 /* See target/target.h. */
294 target_terminal::inferior ()
296 /* Placeholder needed because of fork_inferior. Not necessary on
300 /* See target/target.h. */
303 target_terminal::ours ()
305 /* Placeholder needed because of fork_inferior. Not necessary on
309 /* See target/target.h. */
312 target_terminal::ours_for_output (void)
317 /* See target/target.h. */
320 target_terminal::info (const char *arg
, int from_tty
)
325 /* Default implementations of target ops.
326 See target.h for definitions. */
329 process_stratum_target::post_create_inferior ()
335 process_stratum_target::look_up_symbols ()
341 process_stratum_target::supports_read_auxv ()
347 process_stratum_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
350 gdb_assert_not_reached ("target op read_auxv not supported");
354 process_stratum_target::supports_z_point_type (char z_type
)
360 process_stratum_target::insert_point (enum raw_bkpt_type type
,
362 int size
, raw_breakpoint
*bp
)
368 process_stratum_target::remove_point (enum raw_bkpt_type type
,
370 int size
, raw_breakpoint
*bp
)
376 process_stratum_target::stopped_by_sw_breakpoint ()
382 process_stratum_target::supports_stopped_by_sw_breakpoint ()
388 process_stratum_target::stopped_by_hw_breakpoint ()
394 process_stratum_target::supports_stopped_by_hw_breakpoint ()
400 process_stratum_target::supports_hardware_single_step ()
406 process_stratum_target::stopped_by_watchpoint ()
412 process_stratum_target::stopped_data_address ()
418 process_stratum_target::supports_read_offsets ()
424 process_stratum_target::supports_memory_tagging ()
430 process_stratum_target::fetch_memtags (CORE_ADDR address
, size_t len
,
431 gdb::byte_vector
&tags
, int type
)
433 gdb_assert_not_reached ("target op fetch_memtags not supported");
437 process_stratum_target::store_memtags (CORE_ADDR address
, size_t len
,
438 const gdb::byte_vector
&tags
, int type
)
440 gdb_assert_not_reached ("target op store_memtags not supported");
444 process_stratum_target::read_offsets (CORE_ADDR
*text
, CORE_ADDR
*data
)
446 gdb_assert_not_reached ("target op read_offsets not supported");
450 process_stratum_target::supports_get_tls_address ()
456 process_stratum_target::get_tls_address (thread_info
*thread
,
458 CORE_ADDR load_module
,
461 gdb_assert_not_reached ("target op get_tls_address not supported");
465 process_stratum_target::supports_qxfer_osdata ()
471 process_stratum_target::qxfer_osdata (const char *annex
,
472 unsigned char *readbuf
,
473 unsigned const char *writebuf
,
474 CORE_ADDR offset
, int len
)
476 gdb_assert_not_reached ("target op qxfer_osdata not supported");
480 process_stratum_target::supports_qxfer_siginfo ()
486 process_stratum_target::qxfer_siginfo (const char *annex
,
487 unsigned char *readbuf
,
488 unsigned const char *writebuf
,
489 CORE_ADDR offset
, int len
)
491 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
495 process_stratum_target::supports_non_stop ()
501 process_stratum_target::async (bool enable
)
507 process_stratum_target::start_non_stop (bool enable
)
516 process_stratum_target::supports_multi_process ()
522 process_stratum_target::supports_fork_events ()
528 process_stratum_target::supports_vfork_events ()
534 process_stratum_target::supports_exec_events ()
540 process_stratum_target::handle_new_gdb_connection ()
546 process_stratum_target::handle_monitor_command (char *mon
)
552 process_stratum_target::core_of_thread (ptid_t ptid
)
558 process_stratum_target::supports_read_loadmap ()
564 process_stratum_target::read_loadmap (const char *annex
,
566 unsigned char *myaddr
,
569 gdb_assert_not_reached ("target op read_loadmap not supported");
573 process_stratum_target::process_qsupported
574 (gdb::array_view
<const char * const> features
)
580 process_stratum_target::supports_tracepoints ()
586 process_stratum_target::read_pc (regcache
*regcache
)
588 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
592 process_stratum_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
594 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
598 process_stratum_target::supports_thread_stopped ()
604 process_stratum_target::thread_stopped (thread_info
*thread
)
606 gdb_assert_not_reached ("target op thread_stopped not supported");
610 process_stratum_target::supports_get_tib_address ()
616 process_stratum_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*address
)
618 gdb_assert_not_reached ("target op get_tib_address not supported");
622 process_stratum_target::pause_all (bool freeze
)
628 process_stratum_target::unpause_all (bool unfreeze
)
634 process_stratum_target::stabilize_threads ()
640 process_stratum_target::supports_fast_tracepoints ()
646 process_stratum_target::install_fast_tracepoint_jump_pad
647 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
648 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
649 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
650 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
651 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
654 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
659 process_stratum_target::get_min_fast_tracepoint_insn_len ()
665 process_stratum_target::emit_ops ()
671 process_stratum_target::supports_disable_randomization ()
677 process_stratum_target::supports_qxfer_libraries_svr4 ()
683 process_stratum_target::qxfer_libraries_svr4 (const char *annex
,
684 unsigned char *readbuf
,
685 unsigned const char *writebuf
,
686 CORE_ADDR offset
, int len
)
688 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
692 process_stratum_target::supports_agent ()
698 process_stratum_target::enable_btrace (thread_info
*tp
,
699 const btrace_config
*conf
)
701 error (_("Target does not support branch tracing."));
705 process_stratum_target::disable_btrace (btrace_target_info
*tinfo
)
707 error (_("Target does not support branch tracing."));
711 process_stratum_target::read_btrace (btrace_target_info
*tinfo
,
713 enum btrace_read_type type
)
715 error (_("Target does not support branch tracing."));
719 process_stratum_target::read_btrace_conf (const btrace_target_info
*tinfo
,
722 error (_("Target does not support branch tracing."));
726 process_stratum_target::supports_range_stepping ()
732 process_stratum_target::supports_pid_to_exec_file ()
738 process_stratum_target::pid_to_exec_file (int pid
)
740 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
744 process_stratum_target::supports_multifs ()
750 process_stratum_target::multifs_open (int pid
, const char *filename
,
751 int flags
, mode_t mode
)
753 return open (filename
, flags
, mode
);
757 process_stratum_target::multifs_unlink (int pid
, const char *filename
)
759 return unlink (filename
);
763 process_stratum_target::multifs_readlink (int pid
, const char *filename
,
764 char *buf
, size_t bufsiz
)
766 return readlink (filename
, buf
, bufsiz
);
770 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
772 /* The default behavior is to use the size of a breakpoint as the
775 sw_breakpoint_from_kind (0, &size
);
780 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
782 return breakpoint_kind_from_pc (pcptr
);
786 process_stratum_target::thread_name (ptid_t thread
)
792 process_stratum_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
799 process_stratum_target::thread_pending_parent (thread_info
*thread
)
805 process_stratum_target::thread_pending_child (thread_info
*thread
)
811 process_stratum_target::supports_software_single_step ()
817 process_stratum_target::supports_catch_syscall ()
823 process_stratum_target::get_ipa_tdesc_idx ()