1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2017 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 thread_info
*found
= find_thread_ptid (general_thread
);
31 current_thread
= found
;
32 return (current_thread
!= NULL
);
35 /* The thread that was current before prepare_to_access_memory was
36 called. done_accessing_memory uses this to restore the previous
38 static ptid_t prev_general_thread
;
43 prepare_to_access_memory (void)
45 /* The first thread found. */
46 struct thread_info
*first
= NULL
;
47 /* The first stopped thread found. */
48 struct thread_info
*stopped
= NULL
;
49 /* The current general thread, if found. */
50 struct thread_info
*current
= NULL
;
52 /* Save the general thread value, since prepare_to_access_memory could change
54 prev_general_thread
= general_thread
;
56 if (the_target
->prepare_to_access_memory
!= NULL
)
60 res
= the_target
->prepare_to_access_memory ();
65 for_each_thread (prev_general_thread
.pid (), [&] (thread_info
*thread
)
67 if (mythread_alive (thread
->id
))
69 if (stopped
== NULL
&& the_target
->thread_stopped
!= NULL
70 && thread_stopped (thread
))
76 if (current
== NULL
&& prev_general_thread
== thread
->id
)
81 /* The thread we end up choosing. */
82 struct thread_info
*thread
;
84 /* Prefer a stopped thread. If none is found, try the current
85 thread. Otherwise, take the first thread in the process. If
86 none is found, undo the effects of
87 target->prepare_to_access_memory() and return error. */
90 else if (current
!= NULL
)
92 else if (first
!= NULL
)
96 done_accessing_memory ();
100 current_thread
= thread
;
101 general_thread
= ptid_of (thread
);
109 done_accessing_memory (void)
111 if (the_target
->done_accessing_memory
!= NULL
)
112 the_target
->done_accessing_memory ();
114 /* Restore the previous selected thread. */
115 general_thread
= prev_general_thread
;
116 switch_to_thread (general_thread
);
120 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
123 res
= (*the_target
->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
));
145 write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
148 /* Lacking cleanups, there is some potential for a memory leak if the
149 write fails and we go through error(). Make sure that no more than
150 one buffer is ever pending by making BUFFER static. */
151 static unsigned char *buffer
= 0;
157 buffer
= (unsigned char *) xmalloc (len
);
158 memcpy (buffer
, myaddr
, len
);
159 check_mem_write (memaddr
, buffer
, myaddr
, len
);
160 res
= (*the_target
->write_memory
) (memaddr
, buffer
, len
);
167 /* See target/target.h. */
170 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
172 return write_inferior_memory (memaddr
, myaddr
, len
);
176 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
,
184 ret
= target_wait (ptid
, ourstatus
, options
);
186 /* We don't expose _LOADED events to gdbserver core. See the
187 `dlls_changed' global. */
188 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
189 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
191 /* If GDB is connected through TCP/serial, then GDBserver will most
192 probably be running on its own terminal/console, so it's nice to
193 print there why is GDBserver exiting. If however, GDB is
194 connected through stdio, then there's no need to spam the GDB
195 console with this -- the user will already see the exit through
196 regular GDB output, in that same terminal. */
197 if (!remote_connection_is_stdio ())
199 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
201 "\nChild exited with status %d\n", ourstatus
->value
.integer
);
202 else if (ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
203 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
204 gdb_signal_to_host (ourstatus
->value
.sig
),
205 gdb_signal_to_name (ourstatus
->value
.sig
));
214 /* See target/target.h. */
217 target_stop_and_wait (ptid_t ptid
)
219 struct target_waitstatus status
;
220 int was_non_stop
= non_stop
;
221 struct thread_resume resume_info
;
223 resume_info
.thread
= ptid
;
224 resume_info
.kind
= resume_stop
;
225 resume_info
.sig
= GDB_SIGNAL_0
;
226 (*the_target
->resume
) (&resume_info
, 1);
229 mywait (ptid
, &status
, 0, 0);
230 non_stop
= was_non_stop
;
233 /* See target/target.h. */
236 target_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
238 return (*the_target
->wait
) (ptid
, status
, options
);
241 /* See target/target.h. */
244 target_mourn_inferior (ptid_t ptid
)
246 (*the_target
->mourn
) (find_process_pid (ptid_get_pid (ptid
)));
249 /* See target/target.h. */
252 target_continue_no_signal (ptid_t ptid
)
254 struct thread_resume resume_info
;
256 resume_info
.thread
= ptid
;
257 resume_info
.kind
= resume_continue
;
258 resume_info
.sig
= GDB_SIGNAL_0
;
259 (*the_target
->resume
) (&resume_info
, 1);
262 /* See target/target.h. */
265 target_continue (ptid_t ptid
, enum gdb_signal signal
)
267 struct thread_resume resume_info
;
269 resume_info
.thread
= ptid
;
270 resume_info
.kind
= resume_continue
;
271 resume_info
.sig
= gdb_signal_to_host (signal
);
272 (*the_target
->resume
) (&resume_info
, 1);
275 /* See target/target.h. */
278 target_supports_multi_process (void)
280 return (the_target
->supports_multi_process
!= NULL
?
281 (*the_target
->supports_multi_process
) () : 0);
285 start_non_stop (int nonstop
)
287 if (the_target
->start_non_stop
== NULL
)
295 return (*the_target
->start_non_stop
) (nonstop
);
299 set_target_ops (struct target_ops
*target
)
301 the_target
= XNEW (struct target_ops
);
302 memcpy (the_target
, target
, sizeof (*the_target
));
305 /* Convert pid to printable format. */
308 target_pid_to_str (ptid_t ptid
)
312 if (ptid_equal (ptid
, minus_one_ptid
))
313 xsnprintf (buf
, sizeof (buf
), "<all threads>");
314 else if (ptid_equal (ptid
, null_ptid
))
315 xsnprintf (buf
, sizeof (buf
), "<null thread>");
316 else if (ptid_get_tid (ptid
) != 0)
317 xsnprintf (buf
, sizeof (buf
), "Thread %d.0x%lx",
318 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
319 else if (ptid_get_lwp (ptid
) != 0)
320 xsnprintf (buf
, sizeof (buf
), "LWP %d.%ld",
321 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
323 xsnprintf (buf
, sizeof (buf
), "Process %d",
324 ptid_get_pid (ptid
));
330 kill_inferior (int pid
)
332 gdb_agent_about_to_close (pid
);
334 return (*the_target
->kill
) (pid
);
337 /* Target can do hardware single step. */
340 target_can_do_hardware_single_step (void)
345 /* Default implementation for breakpoint_kind_for_pc.
347 The default behavior for targets that don't implement breakpoint_kind_for_pc
348 is to use the size of a breakpoint as the kind. */
351 default_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
355 gdb_assert (the_target
->sw_breakpoint_from_kind
!= NULL
);
357 (*the_target
->sw_breakpoint_from_kind
) (0, &size
);
363 enum target_terminal::terminal_state
target_terminal::terminal_state
364 = target_terminal::terminal_is_ours
;
366 /* See target/target.h. */
369 target_terminal::init ()
371 /* Placeholder needed because of fork_inferior. Not necessary on
375 /* See target/target.h. */
378 target_terminal::inferior ()
380 /* Placeholder needed because of fork_inferior. Not necessary on
384 /* See target/target.h. */
387 target_terminal::ours ()
389 /* Placeholder needed because of fork_inferior. Not necessary on
393 /* See target/target.h. */
396 target_terminal::ours_for_output (void)
401 /* See target/target.h. */
404 target_terminal::info (const char *arg
, int from_tty
)