1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2019 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"
24 struct target_ops
*the_target
;
29 client_state
&cs
= get_client_state ();
30 thread_info
*found
= find_thread_ptid (cs
.general_thread
);
32 current_thread
= found
;
33 return (current_thread
!= NULL
);
36 /* The thread that was current before prepare_to_access_memory was
37 called. done_accessing_memory uses this to restore the previous
39 static ptid_t prev_general_thread
;
44 prepare_to_access_memory (void)
46 client_state
&cs
= get_client_state ();
48 /* The first thread found. */
49 struct thread_info
*first
= NULL
;
50 /* The first stopped thread found. */
51 struct thread_info
*stopped
= NULL
;
52 /* The current general thread, if found. */
53 struct thread_info
*current
= NULL
;
55 /* Save the general thread value, since prepare_to_access_memory could change
57 prev_general_thread
= cs
.general_thread
;
59 if (the_target
->prepare_to_access_memory
!= NULL
)
63 res
= the_target
->prepare_to_access_memory ();
68 for_each_thread (prev_general_thread
.pid (), [&] (thread_info
*thread
)
70 if (mythread_alive (thread
->id
))
72 if (stopped
== NULL
&& the_target
->thread_stopped
!= NULL
73 && thread_stopped (thread
))
79 if (current
== NULL
&& prev_general_thread
== thread
->id
)
84 /* The thread we end up choosing. */
85 struct thread_info
*thread
;
87 /* Prefer a stopped thread. If none is found, try the current
88 thread. Otherwise, take the first thread in the process. If
89 none is found, undo the effects of
90 target->prepare_to_access_memory() and return error. */
93 else if (current
!= NULL
)
95 else if (first
!= NULL
)
99 done_accessing_memory ();
103 current_thread
= thread
;
104 cs
.general_thread
= ptid_of (thread
);
112 done_accessing_memory (void)
114 client_state
&cs
= get_client_state ();
116 if (the_target
->done_accessing_memory
!= NULL
)
117 the_target
->done_accessing_memory ();
119 /* Restore the previous selected thread. */
120 cs
.general_thread
= prev_general_thread
;
121 switch_to_thread (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
));
150 write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
153 /* Lacking cleanups, there is some potential for a memory leak if the
154 write fails and we go through error(). Make sure that no more than
155 one buffer is ever pending by making BUFFER static. */
156 static unsigned char *buffer
= 0;
162 buffer
= (unsigned char *) xmalloc (len
);
163 memcpy (buffer
, myaddr
, len
);
164 check_mem_write (memaddr
, buffer
, myaddr
, len
);
165 res
= (*the_target
->write_memory
) (memaddr
, buffer
, len
);
172 /* See target/target.h. */
175 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
177 return write_inferior_memory (memaddr
, myaddr
, len
);
181 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
,
189 ret
= target_wait (ptid
, ourstatus
, options
);
191 /* We don't expose _LOADED events to gdbserver core. See the
192 `dlls_changed' global. */
193 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
194 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
196 /* If GDB is connected through TCP/serial, then GDBserver will most
197 probably be running on its own terminal/console, so it's nice to
198 print there why is GDBserver exiting. If however, GDB is
199 connected through stdio, then there's no need to spam the GDB
200 console with this -- the user will already see the exit through
201 regular GDB output, in that same terminal. */
202 if (!remote_connection_is_stdio ())
204 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
206 "\nChild exited with status %d\n", ourstatus
->value
.integer
);
207 else if (ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
208 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
209 gdb_signal_to_host (ourstatus
->value
.sig
),
210 gdb_signal_to_name (ourstatus
->value
.sig
));
219 /* See target/target.h. */
222 target_stop_and_wait (ptid_t ptid
)
224 struct target_waitstatus status
;
225 int was_non_stop
= non_stop
;
226 struct thread_resume resume_info
;
228 resume_info
.thread
= ptid
;
229 resume_info
.kind
= resume_stop
;
230 resume_info
.sig
= GDB_SIGNAL_0
;
231 (*the_target
->resume
) (&resume_info
, 1);
234 mywait (ptid
, &status
, 0, 0);
235 non_stop
= was_non_stop
;
238 /* See target/target.h. */
241 target_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
243 return (*the_target
->wait
) (ptid
, status
, options
);
246 /* See target/target.h. */
249 target_mourn_inferior (ptid_t ptid
)
251 (*the_target
->mourn
) (find_process_pid (ptid
.pid ()));
254 /* See target/target.h. */
257 target_continue_no_signal (ptid_t ptid
)
259 struct thread_resume resume_info
;
261 resume_info
.thread
= ptid
;
262 resume_info
.kind
= resume_continue
;
263 resume_info
.sig
= GDB_SIGNAL_0
;
264 (*the_target
->resume
) (&resume_info
, 1);
267 /* See target/target.h. */
270 target_continue (ptid_t ptid
, enum gdb_signal signal
)
272 struct thread_resume resume_info
;
274 resume_info
.thread
= ptid
;
275 resume_info
.kind
= resume_continue
;
276 resume_info
.sig
= gdb_signal_to_host (signal
);
277 (*the_target
->resume
) (&resume_info
, 1);
280 /* See target/target.h. */
283 target_supports_multi_process (void)
285 return (the_target
->supports_multi_process
!= NULL
?
286 (*the_target
->supports_multi_process
) () : 0);
290 start_non_stop (int nonstop
)
292 if (the_target
->start_non_stop
== NULL
)
300 return (*the_target
->start_non_stop
) (nonstop
);
304 set_target_ops (struct target_ops
*target
)
306 the_target
= XNEW (struct target_ops
);
307 memcpy (the_target
, target
, sizeof (*the_target
));
310 /* Convert pid to printable format. */
313 target_pid_to_str (ptid_t ptid
)
317 if (ptid
== minus_one_ptid
)
318 xsnprintf (buf
, sizeof (buf
), "<all threads>");
319 else if (ptid
== null_ptid
)
320 xsnprintf (buf
, sizeof (buf
), "<null thread>");
321 else if (ptid
.tid () != 0)
322 xsnprintf (buf
, sizeof (buf
), "Thread %d.0x%lx",
323 ptid
.pid (), ptid
.tid ());
324 else if (ptid
.lwp () != 0)
325 xsnprintf (buf
, sizeof (buf
), "LWP %d.%ld",
326 ptid
.pid (), ptid
.lwp ());
328 xsnprintf (buf
, sizeof (buf
), "Process %d",
335 kill_inferior (process_info
*proc
)
337 gdb_agent_about_to_close (proc
->pid
);
339 return (*the_target
->kill
) (proc
);
342 /* Target can do hardware single step. */
345 target_can_do_hardware_single_step (void)
350 /* Default implementation for breakpoint_kind_for_pc.
352 The default behavior for targets that don't implement breakpoint_kind_for_pc
353 is to use the size of a breakpoint as the kind. */
356 default_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
360 gdb_assert (the_target
->sw_breakpoint_from_kind
!= NULL
);
362 (*the_target
->sw_breakpoint_from_kind
) (0, &size
);
368 target_terminal_state
target_terminal::m_terminal_state
369 = target_terminal_state::is_ours
;
371 /* See target/target.h. */
374 target_terminal::init ()
376 /* Placeholder needed because of fork_inferior. Not necessary on
380 /* See target/target.h. */
383 target_terminal::inferior ()
385 /* Placeholder needed because of fork_inferior. Not necessary on
389 /* See target/target.h. */
392 target_terminal::ours ()
394 /* Placeholder needed because of fork_inferior. Not necessary on
398 /* See target/target.h. */
401 target_terminal::ours_for_output (void)
406 /* See target/target.h. */
409 target_terminal::info (const char *arg
, int from_tty
)