Remove ptid_get_lwp
[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{
c12a5089
SC
29 client_state &cs = get_client_state ();
30 thread_info *found = find_thread_ptid (cs.general_thread);
0d62e5e8 31
f0db101d
PA
32 current_thread = found;
33 return (current_thread != NULL);
0d62e5e8
DJ
34}
35
a67a9fae
PA
36/* The thread that was current before prepare_to_access_memory was
37 called. done_accessing_memory uses this to restore the previous
38 selected thread. */
39static ptid_t prev_general_thread;
40
41/* See target.h. */
42
43int
44prepare_to_access_memory (void)
45{
c12a5089
SC
46 client_state &cs = get_client_state ();
47
bac608e7
SM
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;
a67a9fae 54
bac608e7
SM
55 /* Save the general thread value, since prepare_to_access_memory could change
56 it. */
c12a5089 57 prev_general_thread = cs.general_thread;
a67a9fae
PA
58
59 if (the_target->prepare_to_access_memory != NULL)
60 {
61 int res;
62
63 res = the_target->prepare_to_access_memory ();
64 if (res != 0)
65 return res;
66 }
67
bac608e7
SM
68 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
69 {
70 if (mythread_alive (thread->id))
71 {
72 if (stopped == NULL && the_target->thread_stopped != NULL
73 && thread_stopped (thread))
74 stopped = thread;
75
76 if (first == NULL)
77 first = thread;
78
79 if (current == NULL && prev_general_thread == thread->id)
80 current = thread;
81 }
82 });
83
84 /* The thread we end up choosing. */
85 struct thread_info *thread;
a67a9fae
PA
86
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. */
bac608e7
SM
91 if (stopped != NULL)
92 thread = stopped;
93 else if (current != NULL)
94 thread = current;
95 else if (first != NULL)
96 thread = first;
a67a9fae
PA
97 else
98 {
99 done_accessing_memory ();
100 return 1;
101 }
102
103 current_thread = thread;
c12a5089 104 cs.general_thread = ptid_of (thread);
a67a9fae
PA
105
106 return 0;
107}
108
109/* See target.h. */
110
111void
112done_accessing_memory (void)
113{
c12a5089
SC
114 client_state &cs = get_client_state ();
115
a67a9fae
PA
116 if (the_target->done_accessing_memory != NULL)
117 the_target->done_accessing_memory ();
118
119 /* Restore the previous selected thread. */
c12a5089
SC
120 cs.general_thread = prev_general_thread;
121 switch_to_thread (cs.general_thread);
a67a9fae
PA
122}
123
c3e735a6 124int
f450004a 125read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
611cb4a5 126{
c3e735a6
DJ
127 int res;
128 res = (*the_target->read_memory) (memaddr, myaddr, len);
611cb4a5 129 check_mem_read (memaddr, myaddr, len);
c3e735a6 130 return res;
611cb4a5
DJ
131}
132
721ec300
GB
133/* See target/target.h. */
134
135int
136target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
137{
138 return read_inferior_memory (memaddr, myaddr, len);
139}
140
141/* See target/target.h. */
142
143int
144target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
145{
146 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
147}
148
611cb4a5 149int
f450004a
DJ
150write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
151 int len)
0d62e5e8
DJ
152{
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. */
f450004a 156 static unsigned char *buffer = 0;
0d62e5e8
DJ
157 int res;
158
159 if (buffer != NULL)
160 free (buffer);
161
224c3ddb 162 buffer = (unsigned char *) xmalloc (len);
0d62e5e8 163 memcpy (buffer, myaddr, len);
b9fd1791 164 check_mem_write (memaddr, buffer, myaddr, len);
0d62e5e8
DJ
165 res = (*the_target->write_memory) (memaddr, buffer, len);
166 free (buffer);
167 buffer = NULL;
168
169 return res;
170}
171
721ec300
GB
172/* See target/target.h. */
173
174int
175target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
176{
177 return write_inferior_memory (memaddr, myaddr, len);
178}
179
95954743
PA
180ptid_t
181mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
bd99dc85 182 int connected_wait)
611cb4a5 183{
95954743 184 ptid_t ret;
0d62e5e8
DJ
185
186 if (connected_wait)
187 server_waiting = 1;
188
f2b9e3df 189 ret = target_wait (ptid, ourstatus, options);
bd99dc85 190
4210d83e
PA
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;
195
1a3d890b
PA
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 ())
203 {
204 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
205 fprintf (stderr,
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));
211 }
0d62e5e8
DJ
212
213 if (connected_wait)
214 server_waiting = 0;
215
216 return ret;
611cb4a5
DJ
217}
218
f8c1d06b
GB
219/* See target/target.h. */
220
221void
03f4463b 222target_stop_and_wait (ptid_t ptid)
f8c1d06b
GB
223{
224 struct target_waitstatus status;
225 int was_non_stop = non_stop;
17e16485 226 struct thread_resume resume_info;
f8c1d06b 227
17e16485
YQ
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);
f8c1d06b
GB
232
233 non_stop = 1;
234 mywait (ptid, &status, 0, 0);
235 non_stop = was_non_stop;
236}
237
238/* See target/target.h. */
239
f2b9e3df
SDJ
240ptid_t
241target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
242{
243 return (*the_target->wait) (ptid, status, options);
244}
245
246/* See target/target.h. */
247
bc1e6c81
SDJ
248void
249target_mourn_inferior (ptid_t ptid)
250{
e99b03dc 251 (*the_target->mourn) (find_process_pid (ptid.pid ()));
bc1e6c81
SDJ
252}
253
254/* See target/target.h. */
255
f8c1d06b 256void
03f4463b 257target_continue_no_signal (ptid_t ptid)
f8c1d06b
GB
258{
259 struct thread_resume resume_info;
260
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);
265}
266
049a8570
SDJ
267/* See target/target.h. */
268
269void
270target_continue (ptid_t ptid, enum gdb_signal signal)
271{
272 struct thread_resume resume_info;
273
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);
278}
279
1fb77080
SDJ
280/* See target/target.h. */
281
282int
283target_supports_multi_process (void)
284{
285 return (the_target->supports_multi_process != NULL ?
286 (*the_target->supports_multi_process) () : 0);
287}
288
bd99dc85
PA
289int
290start_non_stop (int nonstop)
291{
292 if (the_target->start_non_stop == NULL)
293 {
294 if (nonstop)
295 return -1;
296 else
297 return 0;
298 }
299
300 return (*the_target->start_non_stop) (nonstop);
301}
302
ce3a066d
DJ
303void
304set_target_ops (struct target_ops *target)
305{
8d749320 306 the_target = XNEW (struct target_ops);
ce3a066d
DJ
307 memcpy (the_target, target, sizeof (*the_target));
308}
95954743
PA
309
310/* Convert pid to printable format. */
311
312const char *
313target_pid_to_str (ptid_t ptid)
314{
315 static char buf[80];
316
317 if (ptid_equal (ptid, minus_one_ptid))
6cebaf6e 318 xsnprintf (buf, sizeof (buf), "<all threads>");
95954743 319 else if (ptid_equal (ptid, null_ptid))
6cebaf6e 320 xsnprintf (buf, sizeof (buf), "<null thread>");
95954743 321 else if (ptid_get_tid (ptid) != 0)
6cebaf6e 322 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
e99b03dc 323 ptid.pid (), ptid_get_tid (ptid));
e38504b3 324 else if (ptid.lwp () != 0)
6cebaf6e 325 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
e38504b3 326 ptid.pid (), ptid.lwp ());
95954743 327 else
6cebaf6e 328 xsnprintf (buf, sizeof (buf), "Process %d",
e99b03dc 329 ptid.pid ());
95954743
PA
330
331 return buf;
332}
8336d594 333
7255706c
YQ
334int
335kill_inferior (int pid)
336{
337 gdb_agent_about_to_close (pid);
338
339 return (*the_target->kill) (pid);
340}
70b90b91
YQ
341
342/* Target can do hardware single step. */
343
344int
345target_can_do_hardware_single_step (void)
346{
347 return 1;
348}
2e6ee069
AT
349
350/* Default implementation for breakpoint_kind_for_pc.
351
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. */
354
355int
356default_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
357{
358 int size = 0;
359
360 gdb_assert (the_target->sw_breakpoint_from_kind != NULL);
361
362 (*the_target->sw_breakpoint_from_kind) (0, &size);
363 return size;
364}
2090129c 365
223ffa71
TT
366/* Define it. */
367
e671cd59
PA
368target_terminal_state target_terminal::m_terminal_state
369 = target_terminal_state::is_ours;
223ffa71 370
2090129c
SDJ
371/* See target/target.h. */
372
373void
223ffa71 374target_terminal::init ()
2090129c
SDJ
375{
376 /* Placeholder needed because of fork_inferior. Not necessary on
377 GDBserver. */
378}
379
380/* See target/target.h. */
381
382void
223ffa71 383target_terminal::inferior ()
2090129c
SDJ
384{
385 /* Placeholder needed because of fork_inferior. Not necessary on
386 GDBserver. */
387}
388
389/* See target/target.h. */
390
391void
223ffa71 392target_terminal::ours ()
2090129c
SDJ
393{
394 /* Placeholder needed because of fork_inferior. Not necessary on
395 GDBserver. */
396}
223ffa71
TT
397
398/* See target/target.h. */
399
400void
401target_terminal::ours_for_output (void)
402{
403 /* Placeholder. */
404}
405
406/* See target/target.h. */
407
408void
409target_terminal::info (const char *arg, int from_tty)
410{
411 /* Placeholder. */
412}
This page took 1.208912 seconds and 4 git commands to generate.