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"
25 process_stratum_target
*the_target
;
30 client_state
&cs
= get_client_state ();
31 thread_info
*found
= find_thread_ptid (cs
.general_thread
);
33 current_thread
= found
;
34 return (current_thread
!= NULL
);
37 /* The thread that was current before prepare_to_access_memory was
38 called. done_accessing_memory uses this to restore the previous
40 static ptid_t prev_general_thread
;
45 prepare_to_access_memory (void)
47 client_state
&cs
= get_client_state ();
49 /* The first thread found. */
50 struct thread_info
*first
= NULL
;
51 /* The first stopped thread found. */
52 struct thread_info
*stopped
= NULL
;
53 /* The current general thread, if found. */
54 struct thread_info
*current
= NULL
;
56 /* Save the general thread value, since prepare_to_access_memory could change
58 prev_general_thread
= cs
.general_thread
;
60 int res
= the_target
->pt
->prepare_to_access_memory ();
64 for_each_thread (prev_general_thread
.pid (), [&] (thread_info
*thread
)
66 if (mythread_alive (thread
->id
))
68 if (stopped
== NULL
&& the_target
->thread_stopped
!= NULL
69 && thread_stopped (thread
))
75 if (current
== NULL
&& prev_general_thread
== thread
->id
)
80 /* The thread we end up choosing. */
81 struct thread_info
*thread
;
83 /* Prefer a stopped thread. If none is found, try the current
84 thread. Otherwise, take the first thread in the process. If
85 none is found, undo the effects of
86 target->prepare_to_access_memory() and return error. */
89 else if (current
!= NULL
)
91 else if (first
!= NULL
)
95 done_accessing_memory ();
99 current_thread
= thread
;
100 cs
.general_thread
= ptid_of (thread
);
108 done_accessing_memory (void)
110 client_state
&cs
= get_client_state ();
112 the_target
->pt
->done_accessing_memory ();
114 /* Restore the previous selected thread. */
115 cs
.general_thread
= prev_general_thread
;
116 switch_to_thread (the_target
, cs
.general_thread
);
120 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
123 res
= the_target
->pt
->read_memory (memaddr
, myaddr
, len
);
124 check_mem_read (memaddr
, myaddr
, len
);
128 /* See target/target.h. */
131 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
133 return read_inferior_memory (memaddr
, myaddr
, len
);
136 /* See target/target.h. */
139 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
141 return read_inferior_memory (memaddr
, (gdb_byte
*) result
, sizeof (*result
));
144 /* See target/target.h. */
147 target_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
150 /* Make a copy of the data because check_mem_write may need to
152 gdb::byte_vector
buffer (myaddr
, myaddr
+ len
);
153 check_mem_write (memaddr
, buffer
.data (), myaddr
, len
);
154 return the_target
->pt
->write_memory (memaddr
, buffer
.data (), len
);
158 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
,
166 ret
= target_wait (ptid
, ourstatus
, options
);
168 /* We don't expose _LOADED events to gdbserver core. See the
169 `dlls_changed' global. */
170 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
171 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
173 /* If GDB is connected through TCP/serial, then GDBserver will most
174 probably be running on its own terminal/console, so it's nice to
175 print there why is GDBserver exiting. If however, GDB is
176 connected through stdio, then there's no need to spam the GDB
177 console with this -- the user will already see the exit through
178 regular GDB output, in that same terminal. */
179 if (!remote_connection_is_stdio ())
181 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
183 "\nChild exited with status %d\n", ourstatus
->value
.integer
);
184 else if (ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
185 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
186 gdb_signal_to_host (ourstatus
->value
.sig
),
187 gdb_signal_to_name (ourstatus
->value
.sig
));
196 /* See target/target.h. */
199 target_stop_and_wait (ptid_t ptid
)
201 struct target_waitstatus status
;
202 bool was_non_stop
= non_stop
;
203 struct thread_resume resume_info
;
205 resume_info
.thread
= ptid
;
206 resume_info
.kind
= resume_stop
;
207 resume_info
.sig
= GDB_SIGNAL_0
;
208 the_target
->pt
->resume (&resume_info
, 1);
211 mywait (ptid
, &status
, 0, 0);
212 non_stop
= was_non_stop
;
215 /* See target/target.h. */
218 target_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
220 return the_target
->pt
->wait (ptid
, status
, options
);
223 /* See target/target.h. */
226 target_mourn_inferior (ptid_t ptid
)
228 the_target
->pt
->mourn (find_process_pid (ptid
.pid ()));
231 /* See target/target.h. */
234 target_continue_no_signal (ptid_t ptid
)
236 struct thread_resume resume_info
;
238 resume_info
.thread
= ptid
;
239 resume_info
.kind
= resume_continue
;
240 resume_info
.sig
= GDB_SIGNAL_0
;
241 the_target
->pt
->resume (&resume_info
, 1);
244 /* See target/target.h. */
247 target_continue (ptid_t ptid
, enum gdb_signal signal
)
249 struct thread_resume resume_info
;
251 resume_info
.thread
= ptid
;
252 resume_info
.kind
= resume_continue
;
253 resume_info
.sig
= gdb_signal_to_host (signal
);
254 the_target
->pt
->resume (&resume_info
, 1);
257 /* See target/target.h. */
260 target_supports_multi_process (void)
262 return (the_target
->supports_multi_process
!= NULL
?
263 (*the_target
->supports_multi_process
) () : 0);
267 start_non_stop (int nonstop
)
269 if (the_target
->start_non_stop
== NULL
)
277 return (*the_target
->start_non_stop
) (nonstop
);
281 set_target_ops (process_stratum_target
*target
)
283 the_target
= XNEW (process_stratum_target
);
284 memcpy (the_target
, target
, sizeof (*the_target
));
287 /* Convert pid to printable format. */
290 target_pid_to_str (ptid_t ptid
)
294 if (ptid
== minus_one_ptid
)
295 xsnprintf (buf
, sizeof (buf
), "<all threads>");
296 else if (ptid
== null_ptid
)
297 xsnprintf (buf
, sizeof (buf
), "<null thread>");
298 else if (ptid
.tid () != 0)
299 xsnprintf (buf
, sizeof (buf
), "Thread %d.0x%lx",
300 ptid
.pid (), ptid
.tid ());
301 else if (ptid
.lwp () != 0)
302 xsnprintf (buf
, sizeof (buf
), "LWP %d.%ld",
303 ptid
.pid (), ptid
.lwp ());
305 xsnprintf (buf
, sizeof (buf
), "Process %d",
312 kill_inferior (process_info
*proc
)
314 gdb_agent_about_to_close (proc
->pid
);
316 return the_target
->pt
->kill (proc
);
319 /* Target can do hardware single step. */
322 target_can_do_hardware_single_step (void)
327 /* Default implementation for breakpoint_kind_for_pc.
329 The default behavior for targets that don't implement breakpoint_kind_for_pc
330 is to use the size of a breakpoint as the kind. */
333 default_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
337 gdb_assert (the_target
->sw_breakpoint_from_kind
!= NULL
);
339 (*the_target
->sw_breakpoint_from_kind
) (0, &size
);
345 target_terminal_state
target_terminal::m_terminal_state
346 = target_terminal_state::is_ours
;
348 /* See target/target.h. */
351 target_terminal::init ()
353 /* Placeholder needed because of fork_inferior. Not necessary on
357 /* See target/target.h. */
360 target_terminal::inferior ()
362 /* Placeholder needed because of fork_inferior. Not necessary on
366 /* See target/target.h. */
369 target_terminal::ours ()
371 /* Placeholder needed because of fork_inferior. Not necessary on
375 /* See target/target.h. */
378 target_terminal::ours_for_output (void)
383 /* See target/target.h. */
386 target_terminal::info (const char *arg
, int from_tty
)
391 /* Default implementations of target ops.
392 See target.h for definitions. */
395 process_target::post_create_inferior ()
401 process_target::prepare_to_access_memory ()
407 process_target::done_accessing_memory ()
413 process_target::look_up_symbols ()
419 process_target::supports_read_auxv ()
425 process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
428 gdb_assert_not_reached ("target op read_auxv not supported");
432 process_target::supports_z_point_type (char z_type
)
438 process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
439 int size
, raw_breakpoint
*bp
)
445 process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
446 int size
, raw_breakpoint
*bp
)
452 process_target::stopped_by_sw_breakpoint ()
458 process_target::supports_stopped_by_sw_breakpoint ()