1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2020 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
;
35 client_state
&cs
= get_client_state ();
36 thread_info
*found
= find_thread_ptid (cs
.general_thread
);
38 current_thread
= found
;
39 return (current_thread
!= NULL
);
42 /* The thread that was current before prepare_to_access_memory was
43 called. done_accessing_memory uses this to restore the previous
45 static ptid_t prev_general_thread
;
50 prepare_to_access_memory (void)
52 client_state
&cs
= get_client_state ();
54 /* The first thread found. */
55 struct thread_info
*first
= NULL
;
56 /* The first stopped thread found. */
57 struct thread_info
*stopped
= NULL
;
58 /* The current general thread, if found. */
59 struct thread_info
*current
= NULL
;
61 /* Save the general thread value, since prepare_to_access_memory could change
63 prev_general_thread
= cs
.general_thread
;
65 int res
= the_target
->prepare_to_access_memory ();
69 for_each_thread (prev_general_thread
.pid (), [&] (thread_info
*thread
)
71 if (mythread_alive (thread
->id
))
73 if (stopped
== NULL
&& the_target
->supports_thread_stopped ()
74 && target_thread_stopped (thread
))
80 if (current
== NULL
&& prev_general_thread
== thread
->id
)
85 /* The thread we end up choosing. */
86 struct thread_info
*thread
;
88 /* Prefer a stopped thread. If none is found, try the current
89 thread. Otherwise, take the first thread in the process. If
90 none is found, undo the effects of
91 target->prepare_to_access_memory() and return error. */
94 else if (current
!= NULL
)
96 else if (first
!= NULL
)
100 done_accessing_memory ();
104 current_thread
= thread
;
105 cs
.general_thread
= ptid_of (thread
);
113 done_accessing_memory (void)
115 client_state
&cs
= get_client_state ();
117 the_target
->done_accessing_memory ();
119 /* Restore the previous selected thread. */
120 cs
.general_thread
= prev_general_thread
;
121 switch_to_thread (the_target
, cs
.general_thread
);
125 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
128 res
= the_target
->read_memory (memaddr
, myaddr
, len
);
129 check_mem_read (memaddr
, myaddr
, len
);
133 /* See target/target.h. */
136 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
138 return read_inferior_memory (memaddr
, myaddr
, len
);
141 /* See target/target.h. */
144 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
146 return read_inferior_memory (memaddr
, (gdb_byte
*) result
, sizeof (*result
));
149 /* See target/target.h. */
152 target_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
155 /* Make a copy of the data because check_mem_write may need to
157 gdb::byte_vector
buffer (myaddr
, myaddr
+ len
);
158 check_mem_write (memaddr
, buffer
.data (), myaddr
, len
);
159 return the_target
->write_memory (memaddr
, buffer
.data (), len
);
163 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
164 target_wait_flags options
, int connected_wait
)
171 ret
= target_wait (ptid
, ourstatus
, options
);
173 /* We don't expose _LOADED events to gdbserver core. See the
174 `dlls_changed' global. */
175 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
176 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
178 /* If GDB is connected through TCP/serial, then GDBserver will most
179 probably be running on its own terminal/console, so it's nice to
180 print there why is GDBserver exiting. If however, GDB is
181 connected through stdio, then there's no need to spam the GDB
182 console with this -- the user will already see the exit through
183 regular GDB output, in that same terminal. */
184 if (!remote_connection_is_stdio ())
186 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
188 "\nChild exited with status %d\n", ourstatus
->value
.integer
);
189 else if (ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
190 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
191 gdb_signal_to_host (ourstatus
->value
.sig
),
192 gdb_signal_to_name (ourstatus
->value
.sig
));
201 /* See target/target.h. */
204 target_stop_and_wait (ptid_t ptid
)
206 struct target_waitstatus status
;
207 bool was_non_stop
= non_stop
;
208 struct thread_resume resume_info
;
210 resume_info
.thread
= ptid
;
211 resume_info
.kind
= resume_stop
;
212 resume_info
.sig
= GDB_SIGNAL_0
;
213 the_target
->resume (&resume_info
, 1);
216 mywait (ptid
, &status
, 0, 0);
217 non_stop
= was_non_stop
;
220 /* See target/target.h. */
223 target_wait (ptid_t ptid
, struct target_waitstatus
*status
,
224 target_wait_flags options
)
226 return the_target
->wait (ptid
, status
, options
);
229 /* See target/target.h. */
232 target_mourn_inferior (ptid_t ptid
)
234 the_target
->mourn (find_process_pid (ptid
.pid ()));
237 /* See target/target.h. */
240 target_continue_no_signal (ptid_t ptid
)
242 struct thread_resume resume_info
;
244 resume_info
.thread
= ptid
;
245 resume_info
.kind
= resume_continue
;
246 resume_info
.sig
= GDB_SIGNAL_0
;
247 the_target
->resume (&resume_info
, 1);
250 /* See target/target.h. */
253 target_continue (ptid_t ptid
, enum gdb_signal signal
)
255 struct thread_resume resume_info
;
257 resume_info
.thread
= ptid
;
258 resume_info
.kind
= resume_continue
;
259 resume_info
.sig
= gdb_signal_to_host (signal
);
260 the_target
->resume (&resume_info
, 1);
263 /* See target/target.h. */
266 target_supports_multi_process (void)
268 return the_target
->supports_multi_process ();
272 set_target_ops (process_stratum_target
*target
)
277 /* Convert pid to printable format. */
280 target_pid_to_str (ptid_t ptid
)
284 if (ptid
== minus_one_ptid
)
285 xsnprintf (buf
, sizeof (buf
), "<all threads>");
286 else if (ptid
== null_ptid
)
287 xsnprintf (buf
, sizeof (buf
), "<null thread>");
288 else if (ptid
.tid () != 0)
289 xsnprintf (buf
, sizeof (buf
), "Thread %d.0x%lx",
290 ptid
.pid (), ptid
.tid ());
291 else if (ptid
.lwp () != 0)
292 xsnprintf (buf
, sizeof (buf
), "LWP %d.%ld",
293 ptid
.pid (), ptid
.lwp ());
295 xsnprintf (buf
, sizeof (buf
), "Process %d",
302 kill_inferior (process_info
*proc
)
304 gdb_agent_about_to_close (proc
->pid
);
306 return the_target
->kill (proc
);
311 target_terminal_state
target_terminal::m_terminal_state
312 = target_terminal_state::is_ours
;
314 /* See target/target.h. */
317 target_terminal::init ()
319 /* Placeholder needed because of fork_inferior. Not necessary on
323 /* See target/target.h. */
326 target_terminal::inferior ()
328 /* Placeholder needed because of fork_inferior. Not necessary on
332 /* See target/target.h. */
335 target_terminal::ours ()
337 /* Placeholder needed because of fork_inferior. Not necessary on
341 /* See target/target.h. */
344 target_terminal::ours_for_output (void)
349 /* See target/target.h. */
352 target_terminal::info (const char *arg
, int from_tty
)
357 /* Default implementations of target ops.
358 See target.h for definitions. */
361 process_stratum_target::post_create_inferior ()
367 process_stratum_target::prepare_to_access_memory ()
373 process_stratum_target::done_accessing_memory ()
379 process_stratum_target::look_up_symbols ()
385 process_stratum_target::supports_read_auxv ()
391 process_stratum_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
394 gdb_assert_not_reached ("target op read_auxv not supported");
398 process_stratum_target::supports_z_point_type (char z_type
)
404 process_stratum_target::insert_point (enum raw_bkpt_type type
,
406 int size
, raw_breakpoint
*bp
)
412 process_stratum_target::remove_point (enum raw_bkpt_type type
,
414 int size
, raw_breakpoint
*bp
)
420 process_stratum_target::stopped_by_sw_breakpoint ()
426 process_stratum_target::supports_stopped_by_sw_breakpoint ()
432 process_stratum_target::stopped_by_hw_breakpoint ()
438 process_stratum_target::supports_stopped_by_hw_breakpoint ()
444 process_stratum_target::supports_hardware_single_step ()
450 process_stratum_target::stopped_by_watchpoint ()
456 process_stratum_target::stopped_data_address ()
462 process_stratum_target::supports_read_offsets ()
468 process_stratum_target::read_offsets (CORE_ADDR
*text
, CORE_ADDR
*data
)
470 gdb_assert_not_reached ("target op read_offsets not supported");
474 process_stratum_target::supports_get_tls_address ()
480 process_stratum_target::get_tls_address (thread_info
*thread
,
482 CORE_ADDR load_module
,
485 gdb_assert_not_reached ("target op get_tls_address not supported");
489 process_stratum_target::hostio_last_error (char *buf
)
491 hostio_last_error_from_errno (buf
);
495 process_stratum_target::supports_qxfer_osdata ()
501 process_stratum_target::qxfer_osdata (const char *annex
,
502 unsigned char *readbuf
,
503 unsigned const char *writebuf
,
504 CORE_ADDR offset
, int len
)
506 gdb_assert_not_reached ("target op qxfer_osdata not supported");
510 process_stratum_target::supports_qxfer_siginfo ()
516 process_stratum_target::qxfer_siginfo (const char *annex
,
517 unsigned char *readbuf
,
518 unsigned const char *writebuf
,
519 CORE_ADDR offset
, int len
)
521 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
525 process_stratum_target::supports_non_stop ()
531 process_stratum_target::async (bool enable
)
537 process_stratum_target::start_non_stop (bool enable
)
546 process_stratum_target::supports_multi_process ()
552 process_stratum_target::supports_fork_events ()
558 process_stratum_target::supports_vfork_events ()
564 process_stratum_target::supports_exec_events ()
570 process_stratum_target::handle_new_gdb_connection ()
576 process_stratum_target::handle_monitor_command (char *mon
)
582 process_stratum_target::core_of_thread (ptid_t ptid
)
588 process_stratum_target::supports_read_loadmap ()
594 process_stratum_target::read_loadmap (const char *annex
,
596 unsigned char *myaddr
,
599 gdb_assert_not_reached ("target op read_loadmap not supported");
603 process_stratum_target::process_qsupported
604 (gdb::array_view
<const char * const> features
)
610 process_stratum_target::supports_tracepoints ()
616 process_stratum_target::read_pc (regcache
*regcache
)
618 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
622 process_stratum_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
624 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
628 process_stratum_target::supports_thread_stopped ()
634 process_stratum_target::thread_stopped (thread_info
*thread
)
636 gdb_assert_not_reached ("target op thread_stopped not supported");
640 process_stratum_target::supports_get_tib_address ()
646 process_stratum_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*address
)
648 gdb_assert_not_reached ("target op get_tib_address not supported");
652 process_stratum_target::pause_all (bool freeze
)
658 process_stratum_target::unpause_all (bool unfreeze
)
664 process_stratum_target::stabilize_threads ()
670 process_stratum_target::supports_fast_tracepoints ()
676 process_stratum_target::install_fast_tracepoint_jump_pad
677 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
678 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
679 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
680 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
681 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
684 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
689 process_stratum_target::get_min_fast_tracepoint_insn_len ()
695 process_stratum_target::emit_ops ()
701 process_stratum_target::supports_disable_randomization ()
707 process_stratum_target::supports_qxfer_libraries_svr4 ()
713 process_stratum_target::qxfer_libraries_svr4 (const char *annex
,
714 unsigned char *readbuf
,
715 unsigned const char *writebuf
,
716 CORE_ADDR offset
, int len
)
718 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
722 process_stratum_target::supports_agent ()
728 process_stratum_target::enable_btrace (ptid_t ptid
, const btrace_config
*conf
)
730 error (_("Target does not support branch tracing."));
734 process_stratum_target::disable_btrace (btrace_target_info
*tinfo
)
736 error (_("Target does not support branch tracing."));
740 process_stratum_target::read_btrace (btrace_target_info
*tinfo
,
742 enum btrace_read_type type
)
744 error (_("Target does not support branch tracing."));
748 process_stratum_target::read_btrace_conf (const btrace_target_info
*tinfo
,
751 error (_("Target does not support branch tracing."));
755 process_stratum_target::supports_range_stepping ()
761 process_stratum_target::supports_pid_to_exec_file ()
767 process_stratum_target::pid_to_exec_file (int pid
)
769 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
773 process_stratum_target::supports_multifs ()
779 process_stratum_target::multifs_open (int pid
, const char *filename
,
780 int flags
, mode_t mode
)
782 return open (filename
, flags
, mode
);
786 process_stratum_target::multifs_unlink (int pid
, const char *filename
)
788 return unlink (filename
);
792 process_stratum_target::multifs_readlink (int pid
, const char *filename
,
793 char *buf
, size_t bufsiz
)
795 return readlink (filename
, buf
, bufsiz
);
799 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
801 /* The default behavior is to use the size of a breakpoint as the
804 sw_breakpoint_from_kind (0, &size
);
809 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
811 return breakpoint_kind_from_pc (pcptr
);
815 process_stratum_target::thread_name (ptid_t thread
)
821 process_stratum_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
828 process_stratum_target::supports_software_single_step ()
834 process_stratum_target::supports_catch_syscall ()
840 process_stratum_target::get_ipa_tdesc_idx ()