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 /* Structure used to look up a thread to use as current when accessing
40 /* The PTID of the current general thread. This is an input
42 ptid_t current_gen_ptid
;
44 /* The first thread found. */
45 struct thread_info
*first
;
47 /* The first stopped thread found. */
48 struct thread_info
*stopped
;
50 /* The current general thread, if found. */
51 struct thread_info
*current
;
54 /* Callback for find_inferior. Search for a thread to use as current
55 when accessing memory. */
58 thread_search_callback (thread_info
*thread
, void *args
)
60 struct thread_search
*s
= (struct thread_search
*) args
;
62 if (thread
->id
.pid () == ptid_get_pid (s
->current_gen_ptid
)
63 && mythread_alive (ptid_of (thread
)))
65 if (s
->stopped
== NULL
66 && the_target
->thread_stopped
!= NULL
67 && thread_stopped (thread
))
73 if (s
->current
== NULL
&& s
->current_gen_ptid
== thread
->id
)
80 /* The thread that was current before prepare_to_access_memory was
81 called. done_accessing_memory uses this to restore the previous
83 static ptid_t prev_general_thread
;
88 prepare_to_access_memory (void)
90 struct thread_search search
;
91 struct thread_info
*thread
;
93 memset (&search
, 0, sizeof (search
));
94 search
.current_gen_ptid
= general_thread
;
95 prev_general_thread
= general_thread
;
97 if (the_target
->prepare_to_access_memory
!= NULL
)
101 res
= the_target
->prepare_to_access_memory ();
106 find_inferior (&all_threads
, thread_search_callback
, &search
);
108 /* Prefer a stopped thread. If none is found, try the current
109 thread. Otherwise, take the first thread in the process. If
110 none is found, undo the effects of
111 target->prepare_to_access_memory() and return error. */
112 if (search
.stopped
!= NULL
)
113 thread
= search
.stopped
;
114 else if (search
.current
!= NULL
)
115 thread
= search
.current
;
116 else if (search
.first
!= NULL
)
117 thread
= search
.first
;
120 done_accessing_memory ();
124 current_thread
= thread
;
125 general_thread
= ptid_of (thread
);
133 done_accessing_memory (void)
135 if (the_target
->done_accessing_memory
!= NULL
)
136 the_target
->done_accessing_memory ();
138 /* Restore the previous selected thread. */
139 general_thread
= prev_general_thread
;
140 switch_to_thread (general_thread
);
144 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
147 res
= (*the_target
->read_memory
) (memaddr
, myaddr
, len
);
148 check_mem_read (memaddr
, myaddr
, len
);
152 /* See target/target.h. */
155 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
157 return read_inferior_memory (memaddr
, myaddr
, len
);
160 /* See target/target.h. */
163 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
165 return read_inferior_memory (memaddr
, (gdb_byte
*) result
, sizeof (*result
));
169 write_inferior_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
,
172 /* Lacking cleanups, there is some potential for a memory leak if the
173 write fails and we go through error(). Make sure that no more than
174 one buffer is ever pending by making BUFFER static. */
175 static unsigned char *buffer
= 0;
181 buffer
= (unsigned char *) xmalloc (len
);
182 memcpy (buffer
, myaddr
, len
);
183 check_mem_write (memaddr
, buffer
, myaddr
, len
);
184 res
= (*the_target
->write_memory
) (memaddr
, buffer
, len
);
191 /* See target/target.h. */
194 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
196 return write_inferior_memory (memaddr
, myaddr
, len
);
200 mywait (ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
,
208 ret
= target_wait (ptid
, ourstatus
, options
);
210 /* We don't expose _LOADED events to gdbserver core. See the
211 `dlls_changed' global. */
212 if (ourstatus
->kind
== TARGET_WAITKIND_LOADED
)
213 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
215 /* If GDB is connected through TCP/serial, then GDBserver will most
216 probably be running on its own terminal/console, so it's nice to
217 print there why is GDBserver exiting. If however, GDB is
218 connected through stdio, then there's no need to spam the GDB
219 console with this -- the user will already see the exit through
220 regular GDB output, in that same terminal. */
221 if (!remote_connection_is_stdio ())
223 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
225 "\nChild exited with status %d\n", ourstatus
->value
.integer
);
226 else if (ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
227 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
228 gdb_signal_to_host (ourstatus
->value
.sig
),
229 gdb_signal_to_name (ourstatus
->value
.sig
));
238 /* See target/target.h. */
241 target_stop_and_wait (ptid_t ptid
)
243 struct target_waitstatus status
;
244 int was_non_stop
= non_stop
;
245 struct thread_resume resume_info
;
247 resume_info
.thread
= ptid
;
248 resume_info
.kind
= resume_stop
;
249 resume_info
.sig
= GDB_SIGNAL_0
;
250 (*the_target
->resume
) (&resume_info
, 1);
253 mywait (ptid
, &status
, 0, 0);
254 non_stop
= was_non_stop
;
257 /* See target/target.h. */
260 target_wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
262 return (*the_target
->wait
) (ptid
, status
, options
);
265 /* See target/target.h. */
268 target_mourn_inferior (ptid_t ptid
)
270 (*the_target
->mourn
) (find_process_pid (ptid_get_pid (ptid
)));
273 /* See target/target.h. */
276 target_continue_no_signal (ptid_t ptid
)
278 struct thread_resume resume_info
;
280 resume_info
.thread
= ptid
;
281 resume_info
.kind
= resume_continue
;
282 resume_info
.sig
= GDB_SIGNAL_0
;
283 (*the_target
->resume
) (&resume_info
, 1);
286 /* See target/target.h. */
289 target_continue (ptid_t ptid
, enum gdb_signal signal
)
291 struct thread_resume resume_info
;
293 resume_info
.thread
= ptid
;
294 resume_info
.kind
= resume_continue
;
295 resume_info
.sig
= gdb_signal_to_host (signal
);
296 (*the_target
->resume
) (&resume_info
, 1);
299 /* See target/target.h. */
302 target_supports_multi_process (void)
304 return (the_target
->supports_multi_process
!= NULL
?
305 (*the_target
->supports_multi_process
) () : 0);
309 start_non_stop (int nonstop
)
311 if (the_target
->start_non_stop
== NULL
)
319 return (*the_target
->start_non_stop
) (nonstop
);
323 set_target_ops (struct target_ops
*target
)
325 the_target
= XNEW (struct target_ops
);
326 memcpy (the_target
, target
, sizeof (*the_target
));
329 /* Convert pid to printable format. */
332 target_pid_to_str (ptid_t ptid
)
336 if (ptid_equal (ptid
, minus_one_ptid
))
337 xsnprintf (buf
, sizeof (buf
), "<all threads>");
338 else if (ptid_equal (ptid
, null_ptid
))
339 xsnprintf (buf
, sizeof (buf
), "<null thread>");
340 else if (ptid_get_tid (ptid
) != 0)
341 xsnprintf (buf
, sizeof (buf
), "Thread %d.0x%lx",
342 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
343 else if (ptid_get_lwp (ptid
) != 0)
344 xsnprintf (buf
, sizeof (buf
), "LWP %d.%ld",
345 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
347 xsnprintf (buf
, sizeof (buf
), "Process %d",
348 ptid_get_pid (ptid
));
354 kill_inferior (int pid
)
356 gdb_agent_about_to_close (pid
);
358 return (*the_target
->kill
) (pid
);
361 /* Target can do hardware single step. */
364 target_can_do_hardware_single_step (void)
369 /* Default implementation for breakpoint_kind_for_pc.
371 The default behavior for targets that don't implement breakpoint_kind_for_pc
372 is to use the size of a breakpoint as the kind. */
375 default_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
379 gdb_assert (the_target
->sw_breakpoint_from_kind
!= NULL
);
381 (*the_target
->sw_breakpoint_from_kind
) (0, &size
);
387 enum target_terminal::terminal_state
target_terminal::terminal_state
388 = target_terminal::terminal_is_ours
;
390 /* See target/target.h. */
393 target_terminal::init ()
395 /* Placeholder needed because of fork_inferior. Not necessary on
399 /* See target/target.h. */
402 target_terminal::inferior ()
404 /* Placeholder needed because of fork_inferior. Not necessary on
408 /* See target/target.h. */
411 target_terminal::ours ()
413 /* Placeholder needed because of fork_inferior. Not necessary on
417 /* See target/target.h. */
420 target_terminal::ours_for_output (void)
425 /* See target/target.h. */
428 target_terminal::info (const char *arg
, int from_tty
)