Per-inferior target_terminal state, fix PR gdb/13211, more
[deliverable/binutils-gdb.git] / gdb / gdbserver / target.c
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "tracepoint.h"
23
24 struct target_ops *the_target;
25
26 int
27 set_desired_thread ()
28 {
29 thread_info *found = find_thread_ptid (general_thread);
30
31 current_thread = found;
32 return (current_thread != NULL);
33 }
34
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. */
38 static ptid_t prev_general_thread;
39
40 /* See target.h. */
41
42 int
43 prepare_to_access_memory (void)
44 {
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;
51
52 /* Save the general thread value, since prepare_to_access_memory could change
53 it. */
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
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;
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. */
88 if (stopped != NULL)
89 thread = stopped;
90 else if (current != NULL)
91 thread = current;
92 else if (first != NULL)
93 thread = first;
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
108 void
109 done_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;
116 switch_to_thread (general_thread);
117 }
118
119 int
120 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
121 {
122 int res;
123 res = (*the_target->read_memory) (memaddr, myaddr, len);
124 check_mem_read (memaddr, myaddr, len);
125 return res;
126 }
127
128 /* See target/target.h. */
129
130 int
131 target_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
138 int
139 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
140 {
141 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
142 }
143
144 int
145 write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
146 int len)
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. */
151 static unsigned char *buffer = 0;
152 int res;
153
154 if (buffer != NULL)
155 free (buffer);
156
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);
161 free (buffer);
162 buffer = NULL;
163
164 return res;
165 }
166
167 /* See target/target.h. */
168
169 int
170 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
171 {
172 return write_inferior_memory (memaddr, myaddr, len);
173 }
174
175 ptid_t
176 mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
177 int connected_wait)
178 {
179 ptid_t ret;
180
181 if (connected_wait)
182 server_waiting = 1;
183
184 ret = target_wait (ptid, ourstatus, options);
185
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
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 }
207
208 if (connected_wait)
209 server_waiting = 0;
210
211 return ret;
212 }
213
214 /* See target/target.h. */
215
216 void
217 target_stop_and_wait (ptid_t ptid)
218 {
219 struct target_waitstatus status;
220 int was_non_stop = non_stop;
221 struct thread_resume resume_info;
222
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);
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
235 ptid_t
236 target_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
243 void
244 target_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
251 void
252 target_continue_no_signal (ptid_t ptid)
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
262 /* See target/target.h. */
263
264 void
265 target_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
275 /* See target/target.h. */
276
277 int
278 target_supports_multi_process (void)
279 {
280 return (the_target->supports_multi_process != NULL ?
281 (*the_target->supports_multi_process) () : 0);
282 }
283
284 int
285 start_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
298 void
299 set_target_ops (struct target_ops *target)
300 {
301 the_target = XNEW (struct target_ops);
302 memcpy (the_target, target, sizeof (*the_target));
303 }
304
305 /* Convert pid to printable format. */
306
307 const char *
308 target_pid_to_str (ptid_t ptid)
309 {
310 static char buf[80];
311
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));
322 else
323 xsnprintf (buf, sizeof (buf), "Process %d",
324 ptid_get_pid (ptid));
325
326 return buf;
327 }
328
329 int
330 kill_inferior (int pid)
331 {
332 gdb_agent_about_to_close (pid);
333
334 return (*the_target->kill) (pid);
335 }
336
337 /* Target can do hardware single step. */
338
339 int
340 target_can_do_hardware_single_step (void)
341 {
342 return 1;
343 }
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
350 int
351 default_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 }
360
361 /* Define it. */
362
363 target_terminal_state target_terminal::m_terminal_state
364 = target_terminal_state::is_ours;
365
366 /* See target/target.h. */
367
368 void
369 target_terminal::init ()
370 {
371 /* Placeholder needed because of fork_inferior. Not necessary on
372 GDBserver. */
373 }
374
375 /* See target/target.h. */
376
377 void
378 target_terminal::inferior ()
379 {
380 /* Placeholder needed because of fork_inferior. Not necessary on
381 GDBserver. */
382 }
383
384 /* See target/target.h. */
385
386 void
387 target_terminal::ours ()
388 {
389 /* Placeholder needed because of fork_inferior. Not necessary on
390 GDBserver. */
391 }
392
393 /* See target/target.h. */
394
395 void
396 target_terminal::ours_for_output (void)
397 {
398 /* Placeholder. */
399 }
400
401 /* See target/target.h. */
402
403 void
404 target_terminal::info (const char *arg, int from_tty)
405 {
406 /* Placeholder. */
407 }
This page took 0.047336 seconds and 4 git commands to generate.