Per-inferior target_terminal state, fix PR gdb/13211, more
[deliverable/binutils-gdb.git] / gdb / gdbserver / target.c
CommitLineData
ce3a066d 1/* Target operations for the remote server for GDB.
e2882c85 2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
ce3a066d
DJ
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
ce3a066d
DJ
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce3a066d
DJ
20
21#include "server.h"
c144c7a0 22#include "tracepoint.h"
ce3a066d
DJ
23
24struct target_ops *the_target;
25
f0db101d 26int
f557a88a 27set_desired_thread ()
0d62e5e8 28{
f557a88a 29 thread_info *found = find_thread_ptid (general_thread);
0d62e5e8 30
f0db101d
PA
31 current_thread = found;
32 return (current_thread != NULL);
0d62e5e8
DJ
33}
34
a67a9fae
PA
35/* The thread that was current before prepare_to_access_memory was
36 called. done_accessing_memory uses this to restore the previous
37 selected thread. */
38static ptid_t prev_general_thread;
39
40/* See target.h. */
41
42int
43prepare_to_access_memory (void)
44{
bac608e7
SM
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;
a67a9fae 51
bac608e7
SM
52 /* Save the general thread value, since prepare_to_access_memory could change
53 it. */
a67a9fae
PA
54 prev_general_thread = general_thread;
55
56 if (the_target->prepare_to_access_memory != NULL)
57 {
58 int res;
59
60 res = the_target->prepare_to_access_memory ();
61 if (res != 0)
62 return res;
63 }
64
bac608e7
SM
65 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
66 {
67 if (mythread_alive (thread->id))
68 {
69 if (stopped == NULL && the_target->thread_stopped != NULL
70 && thread_stopped (thread))
71 stopped = thread;
72
73 if (first == NULL)
74 first = thread;
75
76 if (current == NULL && prev_general_thread == thread->id)
77 current = thread;
78 }
79 });
80
81 /* The thread we end up choosing. */
82 struct thread_info *thread;
a67a9fae
PA
83
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. */
bac608e7
SM
88 if (stopped != NULL)
89 thread = stopped;
90 else if (current != NULL)
91 thread = current;
92 else if (first != NULL)
93 thread = first;
a67a9fae
PA
94 else
95 {
96 done_accessing_memory ();
97 return 1;
98 }
99
100 current_thread = thread;
101 general_thread = ptid_of (thread);
102
103 return 0;
104}
105
106/* See target.h. */
107
108void
109done_accessing_memory (void)
110{
111 if (the_target->done_accessing_memory != NULL)
112 the_target->done_accessing_memory ();
113
114 /* Restore the previous selected thread. */
115 general_thread = prev_general_thread;
75352e28 116 switch_to_thread (general_thread);
a67a9fae
PA
117}
118
c3e735a6 119int
f450004a 120read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
611cb4a5 121{
c3e735a6
DJ
122 int res;
123 res = (*the_target->read_memory) (memaddr, myaddr, len);
611cb4a5 124 check_mem_read (memaddr, myaddr, len);
c3e735a6 125 return res;
611cb4a5
DJ
126}
127
721ec300
GB
128/* See target/target.h. */
129
130int
131target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
132{
133 return read_inferior_memory (memaddr, myaddr, len);
134}
135
136/* See target/target.h. */
137
138int
139target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
140{
141 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
142}
143
611cb4a5 144int
f450004a
DJ
145write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
146 int len)
0d62e5e8
DJ
147{
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. */
f450004a 151 static unsigned char *buffer = 0;
0d62e5e8
DJ
152 int res;
153
154 if (buffer != NULL)
155 free (buffer);
156
224c3ddb 157 buffer = (unsigned char *) xmalloc (len);
0d62e5e8 158 memcpy (buffer, myaddr, len);
b9fd1791 159 check_mem_write (memaddr, buffer, myaddr, len);
0d62e5e8
DJ
160 res = (*the_target->write_memory) (memaddr, buffer, len);
161 free (buffer);
162 buffer = NULL;
163
164 return res;
165}
166
721ec300
GB
167/* See target/target.h. */
168
169int
170target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
171{
172 return write_inferior_memory (memaddr, myaddr, len);
173}
174
95954743
PA
175ptid_t
176mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
bd99dc85 177 int connected_wait)
611cb4a5 178{
95954743 179 ptid_t ret;
0d62e5e8
DJ
180
181 if (connected_wait)
182 server_waiting = 1;
183
f2b9e3df 184 ret = target_wait (ptid, ourstatus, options);
bd99dc85 185
4210d83e
PA
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;
190
1a3d890b
PA
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 ())
198 {
199 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
200 fprintf (stderr,
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));
206 }
0d62e5e8
DJ
207
208 if (connected_wait)
209 server_waiting = 0;
210
211 return ret;
611cb4a5
DJ
212}
213
f8c1d06b
GB
214/* See target/target.h. */
215
216void
03f4463b 217target_stop_and_wait (ptid_t ptid)
f8c1d06b
GB
218{
219 struct target_waitstatus status;
220 int was_non_stop = non_stop;
17e16485 221 struct thread_resume resume_info;
f8c1d06b 222
17e16485
YQ
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);
f8c1d06b
GB
227
228 non_stop = 1;
229 mywait (ptid, &status, 0, 0);
230 non_stop = was_non_stop;
231}
232
233/* See target/target.h. */
234
f2b9e3df
SDJ
235ptid_t
236target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
237{
238 return (*the_target->wait) (ptid, status, options);
239}
240
241/* See target/target.h. */
242
bc1e6c81
SDJ
243void
244target_mourn_inferior (ptid_t ptid)
245{
246 (*the_target->mourn) (find_process_pid (ptid_get_pid (ptid)));
247}
248
249/* See target/target.h. */
250
f8c1d06b 251void
03f4463b 252target_continue_no_signal (ptid_t ptid)
f8c1d06b
GB
253{
254 struct thread_resume resume_info;
255
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);
260}
261
049a8570
SDJ
262/* See target/target.h. */
263
264void
265target_continue (ptid_t ptid, enum gdb_signal signal)
266{
267 struct thread_resume resume_info;
268
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);
273}
274
1fb77080
SDJ
275/* See target/target.h. */
276
277int
278target_supports_multi_process (void)
279{
280 return (the_target->supports_multi_process != NULL ?
281 (*the_target->supports_multi_process) () : 0);
282}
283
bd99dc85
PA
284int
285start_non_stop (int nonstop)
286{
287 if (the_target->start_non_stop == NULL)
288 {
289 if (nonstop)
290 return -1;
291 else
292 return 0;
293 }
294
295 return (*the_target->start_non_stop) (nonstop);
296}
297
ce3a066d
DJ
298void
299set_target_ops (struct target_ops *target)
300{
8d749320 301 the_target = XNEW (struct target_ops);
ce3a066d
DJ
302 memcpy (the_target, target, sizeof (*the_target));
303}
95954743
PA
304
305/* Convert pid to printable format. */
306
307const char *
308target_pid_to_str (ptid_t ptid)
309{
310 static char buf[80];
311
312 if (ptid_equal (ptid, minus_one_ptid))
6cebaf6e 313 xsnprintf (buf, sizeof (buf), "<all threads>");
95954743 314 else if (ptid_equal (ptid, null_ptid))
6cebaf6e 315 xsnprintf (buf, sizeof (buf), "<null thread>");
95954743 316 else if (ptid_get_tid (ptid) != 0)
6cebaf6e 317 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
318 ptid_get_pid (ptid), ptid_get_tid (ptid));
95954743 319 else if (ptid_get_lwp (ptid) != 0)
6cebaf6e 320 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
321 ptid_get_pid (ptid), ptid_get_lwp (ptid));
95954743 322 else
6cebaf6e 323 xsnprintf (buf, sizeof (buf), "Process %d",
324 ptid_get_pid (ptid));
95954743
PA
325
326 return buf;
327}
8336d594 328
7255706c
YQ
329int
330kill_inferior (int pid)
331{
332 gdb_agent_about_to_close (pid);
333
334 return (*the_target->kill) (pid);
335}
70b90b91
YQ
336
337/* Target can do hardware single step. */
338
339int
340target_can_do_hardware_single_step (void)
341{
342 return 1;
343}
2e6ee069
AT
344
345/* Default implementation for breakpoint_kind_for_pc.
346
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. */
349
350int
351default_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
352{
353 int size = 0;
354
355 gdb_assert (the_target->sw_breakpoint_from_kind != NULL);
356
357 (*the_target->sw_breakpoint_from_kind) (0, &size);
358 return size;
359}
2090129c 360
223ffa71
TT
361/* Define it. */
362
e671cd59
PA
363target_terminal_state target_terminal::m_terminal_state
364 = target_terminal_state::is_ours;
223ffa71 365
2090129c
SDJ
366/* See target/target.h. */
367
368void
223ffa71 369target_terminal::init ()
2090129c
SDJ
370{
371 /* Placeholder needed because of fork_inferior. Not necessary on
372 GDBserver. */
373}
374
375/* See target/target.h. */
376
377void
223ffa71 378target_terminal::inferior ()
2090129c
SDJ
379{
380 /* Placeholder needed because of fork_inferior. Not necessary on
381 GDBserver. */
382}
383
384/* See target/target.h. */
385
386void
223ffa71 387target_terminal::ours ()
2090129c
SDJ
388{
389 /* Placeholder needed because of fork_inferior. Not necessary on
390 GDBserver. */
391}
223ffa71
TT
392
393/* See target/target.h. */
394
395void
396target_terminal::ours_for_output (void)
397{
398 /* Placeholder. */
399}
400
401/* See target/target.h. */
402
403void
404target_terminal::info (const char *arg, int from_tty)
405{
406 /* Placeholder. */
407}
This page took 1.168995 seconds and 4 git commands to generate.