inf-ptrace.c revision 1.1 1 1.1 christos /* Low-level child interface to ptrace.
2 1.1 christos
3 1.1 christos Copyright (C) 1988-2014 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "command.h"
22 1.1 christos #include "inferior.h"
23 1.1 christos #include "inflow.h"
24 1.1 christos #include "terminal.h"
25 1.1 christos #include "gdbcore.h"
26 1.1 christos #include "regcache.h"
27 1.1 christos
28 1.1 christos #include "gdb_assert.h"
29 1.1 christos #include <string.h>
30 1.1 christos #include "gdb_ptrace.h"
31 1.1 christos #include "gdb_wait.h"
32 1.1 christos #include <signal.h>
33 1.1 christos
34 1.1 christos #include "inf-ptrace.h"
35 1.1 christos #include "inf-child.h"
36 1.1 christos #include "gdbthread.h"
37 1.1 christos
38 1.1 christos
39 1.1 christos
41 1.1 christos #ifdef PT_GET_PROCESS_STATE
42 1.1 christos
43 1.1 christos static int
44 1.1 christos inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
45 1.1 christos int detach_fork)
46 1.1 christos {
47 1.1 christos pid_t pid, fpid;
48 1.1 christos ptrace_state_t pe;
49 1.1 christos
50 1.1 christos pid = ptid_get_pid (inferior_ptid);
51 1.1 christos
52 1.1 christos if (ptrace (PT_GET_PROCESS_STATE, pid,
53 1.1 christos (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
54 1.1 christos perror_with_name (("ptrace"));
55 1.1 christos
56 1.1 christos gdb_assert (pe.pe_report_event == PTRACE_FORK);
57 1.1 christos fpid = pe.pe_other_pid;
58 1.1 christos
59 1.1 christos if (follow_child)
60 1.1 christos {
61 1.1 christos struct inferior *parent_inf, *child_inf;
62 1.1 christos struct thread_info *tp;
63 1.1 christos
64 1.1 christos parent_inf = find_inferior_pid (pid);
65 1.1 christos
66 1.1 christos /* Add the child. */
67 1.1 christos child_inf = add_inferior (fpid);
68 1.1 christos child_inf->attach_flag = parent_inf->attach_flag;
69 1.1 christos copy_terminal_info (child_inf, parent_inf);
70 1.1 christos child_inf->pspace = parent_inf->pspace;
71 1.1 christos child_inf->aspace = parent_inf->aspace;
72 1.1 christos
73 1.1 christos /* Before detaching from the parent, remove all breakpoints from
74 1.1 christos it. */
75 1.1 christos remove_breakpoints ();
76 1.1 christos
77 1.1 christos if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
78 1.1 christos perror_with_name (("ptrace"));
79 1.1 christos
80 1.1 christos /* Switch inferior_ptid out of the parent's way. */
81 1.1 christos inferior_ptid = pid_to_ptid (fpid);
82 1.1 christos
83 1.1 christos /* Delete the parent. */
84 1.1 christos detach_inferior (pid);
85 1.1 christos
86 1.1 christos add_thread_silent (inferior_ptid);
87 1.1 christos }
88 1.1 christos else
89 1.1 christos {
90 1.1 christos /* Breakpoints have already been detached from the child by
91 1.1 christos infrun.c. */
92 1.1 christos
93 1.1 christos if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1)
94 1.1 christos perror_with_name (("ptrace"));
95 1.1 christos }
96 1.1 christos
97 1.1 christos return 0;
98 1.1 christos }
99 1.1 christos
100 1.1 christos #endif /* PT_GET_PROCESS_STATE */
101 1.1 christos
102 1.1 christos
104 1.1 christos /* Prepare to be traced. */
105 1.1 christos
106 1.1 christos static void
107 1.1 christos inf_ptrace_me (void)
108 1.1 christos {
109 1.1 christos /* "Trace me, Dr. Memory!" */
110 1.1 christos ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
111 1.1 christos }
112 1.1 christos
113 1.1 christos /* Start a new inferior Unix child process. EXEC_FILE is the file to
114 1.1 christos run, ALLARGS is a string containing the arguments to the program.
115 1.1 christos ENV is the environment vector to pass. If FROM_TTY is non-zero, be
116 1.1 christos chatty about it. */
117 1.1 christos
118 1.1 christos static void
119 1.1 christos inf_ptrace_create_inferior (struct target_ops *ops,
120 1.1 christos char *exec_file, char *allargs, char **env,
121 1.1 christos int from_tty)
122 1.1 christos {
123 1.1 christos int pid;
124 1.1 christos
125 1.1 christos /* Do not change either targets above or the same target if already present.
126 1.1 christos The reason is the target stack is shared across multiple inferiors. */
127 1.1 christos int ops_already_pushed = target_is_pushed (ops);
128 1.1 christos struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
129 1.1 christos
130 1.1 christos if (! ops_already_pushed)
131 1.1 christos {
132 1.1 christos /* Clear possible core file with its process_stratum. */
133 1.1 christos push_target (ops);
134 1.1 christos make_cleanup_unpush_target (ops);
135 1.1 christos }
136 1.1 christos
137 1.1 christos pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
138 1.1 christos NULL, NULL, NULL);
139 1.1 christos
140 1.1 christos discard_cleanups (back_to);
141 1.1 christos
142 1.1 christos startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
143 1.1 christos
144 1.1 christos /* On some targets, there must be some explicit actions taken after
145 1.1 christos the inferior has been started up. */
146 1.1 christos target_post_startup_inferior (pid_to_ptid (pid));
147 1.1 christos }
148 1.1 christos
149 1.1 christos #ifdef PT_GET_PROCESS_STATE
150 1.1 christos
151 1.1 christos static void
152 1.1 christos inf_ptrace_post_startup_inferior (ptid_t pid)
153 1.1 christos {
154 1.1 christos ptrace_event_t pe;
155 1.1 christos
156 1.1 christos /* Set the initial event mask. */
157 1.1 christos memset (&pe, 0, sizeof pe);
158 1.1 christos pe.pe_set_event |= PTRACE_FORK;
159 1.1 christos if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
160 1.1 christos (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
161 1.1 christos perror_with_name (("ptrace"));
162 1.1 christos }
163 1.1 christos
164 1.1 christos #endif
165 1.1 christos
166 1.1 christos /* Clean up a rotting corpse of an inferior after it died. */
167 1.1 christos
168 1.1 christos static void
169 1.1 christos inf_ptrace_mourn_inferior (struct target_ops *ops)
170 1.1 christos {
171 1.1 christos int status;
172 1.1 christos
173 1.1 christos /* Wait just one more time to collect the inferior's exit status.
174 1.1 christos Do not check whether this succeeds though, since we may be
175 1.1 christos dealing with a process that we attached to. Such a process will
176 1.1 christos only report its exit status to its original parent. */
177 1.1 christos waitpid (ptid_get_pid (inferior_ptid), &status, 0);
178 1.1 christos
179 1.1 christos generic_mourn_inferior ();
180 1.1 christos
181 1.1 christos if (!have_inferiors ())
182 1.1 christos unpush_target (ops);
183 1.1 christos }
184 1.1 christos
185 1.1 christos /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
186 1.1 christos be chatty about it. */
187 1.1 christos
188 1.1 christos static void
189 1.1 christos inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
190 1.1 christos {
191 1.1 christos char *exec_file;
192 1.1 christos pid_t pid;
193 1.1 christos struct inferior *inf;
194 1.1 christos
195 1.1 christos /* Do not change either targets above or the same target if already present.
196 1.1 christos The reason is the target stack is shared across multiple inferiors. */
197 1.1 christos int ops_already_pushed = target_is_pushed (ops);
198 1.1 christos struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
199 1.1 christos
200 1.1 christos pid = parse_pid_to_attach (args);
201 1.1 christos
202 1.1 christos if (pid == getpid ()) /* Trying to masturbate? */
203 1.1 christos error (_("I refuse to debug myself!"));
204 1.1 christos
205 1.1 christos if (! ops_already_pushed)
206 1.1 christos {
207 1.1 christos /* target_pid_to_str already uses the target. Also clear possible core
208 1.1 christos file with its process_stratum. */
209 1.1 christos push_target (ops);
210 1.1 christos make_cleanup_unpush_target (ops);
211 1.1 christos }
212 1.1 christos
213 1.1 christos if (from_tty)
214 1.1 christos {
215 1.1 christos exec_file = get_exec_file (0);
216 1.1 christos
217 1.1 christos if (exec_file)
218 1.1 christos printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
219 1.1 christos target_pid_to_str (pid_to_ptid (pid)));
220 1.1 christos else
221 1.1 christos printf_unfiltered (_("Attaching to %s\n"),
222 1.1 christos target_pid_to_str (pid_to_ptid (pid)));
223 1.1 christos
224 1.1 christos gdb_flush (gdb_stdout);
225 1.1 christos }
226 1.1 christos
227 1.1 christos #ifdef PT_ATTACH
228 1.1 christos errno = 0;
229 1.1 christos ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
230 1.1 christos if (errno != 0)
231 1.1 christos perror_with_name (("ptrace"));
232 1.1 christos #else
233 1.1 christos error (_("This system does not support attaching to a process"));
234 1.1 christos #endif
235 1.1 christos
236 1.1 christos inf = current_inferior ();
237 1.1 christos inferior_appeared (inf, pid);
238 1.1 christos inf->attach_flag = 1;
239 1.1 christos inferior_ptid = pid_to_ptid (pid);
240 1.1 christos
241 1.1 christos /* Always add a main thread. If some target extends the ptrace
242 1.1 christos target, it should decorate the ptid later with more info. */
243 1.1 christos add_thread_silent (inferior_ptid);
244 1.1 christos
245 1.1 christos discard_cleanups (back_to);
246 1.1 christos }
247 1.1 christos
248 1.1 christos #ifdef PT_GET_PROCESS_STATE
249 1.1 christos
250 1.1 christos static void
251 1.1 christos inf_ptrace_post_attach (int pid)
252 1.1 christos {
253 1.1 christos ptrace_event_t pe;
254 1.1 christos
255 1.1 christos /* Set the initial event mask. */
256 1.1 christos memset (&pe, 0, sizeof pe);
257 1.1 christos pe.pe_set_event |= PTRACE_FORK;
258 1.1 christos if (ptrace (PT_SET_EVENT_MASK, pid,
259 1.1 christos (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
260 1.1 christos perror_with_name (("ptrace"));
261 1.1 christos }
262 1.1 christos
263 1.1 christos #endif
264 1.1 christos
265 1.1 christos /* Detach from the inferior, optionally passing it the signal
266 1.1 christos specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
267 1.1 christos
268 1.1 christos static void
269 1.1 christos inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
270 1.1 christos {
271 1.1 christos pid_t pid = ptid_get_pid (inferior_ptid);
272 1.1 christos int sig = 0;
273 1.1 christos
274 1.1 christos if (from_tty)
275 1.1 christos {
276 1.1 christos char *exec_file = get_exec_file (0);
277 1.1 christos if (exec_file == 0)
278 1.1 christos exec_file = "";
279 1.1 christos printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
280 1.1 christos target_pid_to_str (pid_to_ptid (pid)));
281 1.1 christos gdb_flush (gdb_stdout);
282 1.1 christos }
283 1.1 christos if (args)
284 1.1 christos sig = atoi (args);
285 1.1 christos
286 1.1 christos #ifdef PT_DETACH
287 1.1 christos /* We'd better not have left any breakpoints in the program or it'll
288 1.1 christos die when it hits one. Also note that this may only work if we
289 1.1 christos previously attached to the inferior. It *might* work if we
290 1.1 christos started the process ourselves. */
291 1.1 christos errno = 0;
292 1.1 christos ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
293 1.1 christos if (errno != 0)
294 1.1 christos perror_with_name (("ptrace"));
295 1.1 christos #else
296 1.1 christos error (_("This system does not support detaching from a process"));
297 1.1 christos #endif
298 1.1 christos
299 1.1 christos inferior_ptid = null_ptid;
300 1.1 christos detach_inferior (pid);
301 1.1 christos
302 1.1 christos if (!have_inferiors ())
303 1.1 christos unpush_target (ops);
304 1.1 christos }
305 1.1 christos
306 1.1 christos /* Kill the inferior. */
307 1.1 christos
308 1.1 christos static void
309 1.1 christos inf_ptrace_kill (struct target_ops *ops)
310 1.1 christos {
311 1.1 christos pid_t pid = ptid_get_pid (inferior_ptid);
312 1.1 christos int status;
313 1.1 christos
314 1.1 christos if (pid == 0)
315 1.1 christos return;
316 1.1 christos
317 1.1 christos ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
318 1.1 christos waitpid (pid, &status, 0);
319 1.1 christos
320 1.1 christos target_mourn_inferior ();
321 1.1 christos }
322 1.1 christos
323 1.1 christos /* Stop the inferior. */
324 1.1 christos
325 1.1 christos static void
326 1.1 christos inf_ptrace_stop (ptid_t ptid)
327 1.1 christos {
328 1.1 christos /* Send a SIGINT to the process group. This acts just like the user
329 1.1 christos typed a ^C on the controlling terminal. Note that using a
330 1.1 christos negative process number in kill() is a System V-ism. The proper
331 1.1 christos BSD interface is killpg(). However, all modern BSDs support the
332 1.1 christos System V interface too. */
333 1.1 christos kill (-inferior_process_group (), SIGINT);
334 1.1 christos }
335 1.1 christos
336 1.1 christos /* Resume execution of thread PTID, or all threads if PTID is -1. If
337 1.1 christos STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
338 1.1 christos that signal. */
339 1.1 christos
340 1.1 christos static void
341 1.1 christos inf_ptrace_resume (struct target_ops *ops,
342 1.1 christos ptid_t ptid, int step, enum gdb_signal signal)
343 1.1 christos {
344 1.1 christos pid_t pid = ptid_get_pid (ptid);
345 1.1 christos int request, sig;
346 1.1 christos
347 1.1 christos if (pid == -1)
348 1.1 christos /* Resume all threads. Traditionally ptrace() only supports
349 1.1 christos single-threaded processes, so simply resume the inferior. */
350 1.1 christos pid = ptid_get_pid (inferior_ptid);
351 1.1 christos
352 1.1 christos if (catch_syscall_enabled () > 0)
353 1.1 christos request = PT_SYSCALL;
354 1.1 christos else
355 1.1 christos request = PT_CONTINUE;
356 1.1 christos
357 1.1 christos if (step)
358 1.1 christos {
359 1.1 christos /* If this system does not support PT_STEP, a higher level
360 1.1 christos function will have called single_step() to transmute the step
361 1.1 christos request into a continue request (by setting breakpoints on
362 1.1 christos all possible successor instructions), so we don't have to
363 1.1 christos worry about that here. */
364 1.1 christos request = PT_STEP;
365 1.1 christos #ifdef __NetBSD__
366 1.1 christos /*
367 1.1 christos * On NetBSD the data field of PT_STEP contains the thread
368 1.1 christos * to be stepped; all other threads are continued if this value is > 0
369 1.1 christos */
370 1.1 christos sig = ptid_get_lwp(ptid);
371 1.1 christos #else
372 1.1 christos sig = 0;
373 1.1 christos #endif
374 1.1 christos } else
375 1.1 christos sig = gdb_signal_to_host (signal);
376 1.1 christos
377 1.1 christos /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
378 1.1 christos where it was. If GDB wanted it to start some other way, we have
379 1.1 christos already written a new program counter value to the child. */
380 1.1 christos errno = 0;
381 1.1 christos ptrace (request, pid, (PTRACE_TYPE_ARG3)1, sig);
382 1.1 christos if (errno != 0)
383 1.1 christos perror_with_name (("ptrace"));
384 1.1 christos }
385 1.1 christos
386 1.1 christos /* Wait for the child specified by PTID to do something. Return the
387 1.1 christos process ID of the child, or MINUS_ONE_PTID in case of error; store
388 1.1 christos the status in *OURSTATUS. */
389 1.1 christos
390 1.1 christos static ptid_t
391 1.1 christos inf_ptrace_wait (struct target_ops *ops,
392 1.1 christos ptid_t ptid, struct target_waitstatus *ourstatus, int options)
393 1.1 christos {
394 1.1 christos pid_t pid;
395 1.1 christos int status, save_errno;
396 1.1 christos
397 1.1 christos do
398 1.1 christos {
399 1.1 christos set_sigint_trap ();
400 1.1 christos
401 1.1 christos do
402 1.1 christos {
403 1.1 christos pid = waitpid (ptid_get_pid (ptid), &status, 0);
404 1.1 christos save_errno = errno;
405 1.1 christos }
406 1.1 christos while (pid == -1 && errno == EINTR);
407 1.1 christos
408 1.1 christos clear_sigint_trap ();
409 1.1 christos
410 1.1 christos if (pid == -1)
411 1.1 christos {
412 1.1 christos fprintf_unfiltered (gdb_stderr,
413 1.1 christos _("Child process unexpectedly missing: %s.\n"),
414 1.1 christos safe_strerror (save_errno));
415 1.1 christos
416 1.1 christos /* Claim it exited with unknown signal. */
417 1.1 christos ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
418 1.1 christos ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
419 1.1 christos return inferior_ptid;
420 1.1 christos }
421 1.1 christos
422 1.1 christos /* Ignore terminated detached child processes. */
423 1.1 christos if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
424 1.1 christos pid = -1;
425 1.1 christos }
426 1.1 christos while (pid == -1);
427 1.1 christos
428 1.1 christos #ifdef PT_GET_PROCESS_STATE
429 1.1 christos if (WIFSTOPPED (status))
430 1.1 christos {
431 1.1 christos ptrace_state_t pe;
432 1.1 christos pid_t fpid;
433 1.1 christos
434 1.1 christos if (ptrace (PT_GET_PROCESS_STATE, pid,
435 1.1 christos (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
436 1.1 christos perror_with_name (("ptrace"));
437 1.1 christos
438 1.1 christos switch (pe.pe_report_event)
439 1.1 christos {
440 1.1 christos case PTRACE_FORK:
441 1.1 christos ourstatus->kind = TARGET_WAITKIND_FORKED;
442 1.1 christos ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
443 1.1 christos
444 1.1 christos /* Make sure the other end of the fork is stopped too. */
445 1.1 christos fpid = waitpid (pe.pe_other_pid, &status, 0);
446 1.1 christos if (fpid == -1)
447 1.1 christos perror_with_name (("waitpid"));
448 1.1 christos
449 1.1 christos if (ptrace (PT_GET_PROCESS_STATE, fpid,
450 1.1 christos (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
451 1.1 christos perror_with_name (("ptrace"));
452 1.1 christos
453 1.1 christos gdb_assert (pe.pe_report_event == PTRACE_FORK);
454 1.1 christos gdb_assert (pe.pe_other_pid == pid);
455 1.1 christos if (fpid == ptid_get_pid (inferior_ptid))
456 1.1 christos {
457 1.1 christos ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
458 1.1 christos return pid_to_ptid (fpid);
459 1.1 christos }
460 1.1 christos
461 1.1 christos return pid_to_ptid (pid);
462 1.1 christos }
463 1.1 christos }
464 1.1 christos #endif
465 1.1 christos
466 1.1 christos store_waitstatus (ourstatus, status);
467 1.1 christos return pid_to_ptid (pid);
468 1.1 christos }
469 1.1 christos
470 1.1 christos /* Attempt a transfer all LEN bytes starting at OFFSET between the
471 1.1 christos inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
472 1.1 christos Return the number of bytes actually transferred. */
473 1.1 christos
474 1.1 christos static LONGEST
475 1.1 christos inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
476 1.1 christos const char *annex, gdb_byte *readbuf,
477 1.1 christos const gdb_byte *writebuf,
478 1.1 christos ULONGEST offset, LONGEST len)
479 1.1 christos {
480 1.1 christos pid_t pid = ptid_get_pid (inferior_ptid);
481 1.1 christos
482 1.1 christos switch (object)
483 1.1 christos {
484 1.1 christos case TARGET_OBJECT_MEMORY:
485 1.1 christos #ifdef PT_IO
486 1.1 christos /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
487 1.1 christos request that promises to be much more efficient in reading
488 1.1 christos and writing data in the traced process's address space. */
489 1.1 christos {
490 1.1 christos struct ptrace_io_desc piod;
491 1.1 christos
492 1.1 christos /* NOTE: We assume that there are no distinct address spaces
493 1.1 christos for instruction and data. However, on OpenBSD 3.9 and
494 1.1 christos later, PIOD_WRITE_D doesn't allow changing memory that's
495 1.1 christos mapped read-only. Since most code segments will be
496 1.1 christos read-only, using PIOD_WRITE_D will prevent us from
497 1.1 christos inserting breakpoints, so we use PIOD_WRITE_I instead. */
498 1.1 christos piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
499 1.1 christos piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
500 1.1 christos piod.piod_offs = (void *) (long) offset;
501 1.1 christos piod.piod_len = len;
502 1.1 christos
503 1.1 christos errno = 0;
504 1.1 christos if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
505 1.1 christos /* Return the actual number of bytes read or written. */
506 1.1 christos return piod.piod_len;
507 1.1 christos /* If the PT_IO request is somehow not supported, fallback on
508 1.1 christos using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
509 1.1 christos to indicate failure. */
510 1.1 christos if (errno != EINVAL)
511 1.1 christos return 0;
512 1.1 christos }
513 1.1 christos #endif
514 1.1 christos {
515 1.1 christos union
516 1.1 christos {
517 1.1 christos PTRACE_TYPE_RET word;
518 1.1 christos gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
519 1.1 christos } buffer;
520 1.1 christos ULONGEST rounded_offset;
521 1.1 christos LONGEST partial_len;
522 1.1 christos
523 1.1 christos /* Round the start offset down to the next long word
524 1.1 christos boundary. */
525 1.1 christos rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
526 1.1 christos
527 1.1 christos /* Since ptrace will transfer a single word starting at that
528 1.1 christos rounded_offset the partial_len needs to be adjusted down to
529 1.1 christos that (remember this function only does a single transfer).
530 1.1 christos Should the required length be even less, adjust it down
531 1.1 christos again. */
532 1.1 christos partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
533 1.1 christos if (partial_len > len)
534 1.1 christos partial_len = len;
535 1.1 christos
536 1.1 christos if (writebuf)
537 1.1 christos {
538 1.1 christos /* If OFFSET:PARTIAL_LEN is smaller than
539 1.1 christos ROUNDED_OFFSET:WORDSIZE then a read/modify write will
540 1.1 christos be needed. Read in the entire word. */
541 1.1 christos if (rounded_offset < offset
542 1.1 christos || (offset + partial_len
543 1.1 christos < rounded_offset + sizeof (PTRACE_TYPE_RET)))
544 1.1 christos /* Need part of initial word -- fetch it. */
545 1.1 christos buffer.word = ptrace (PT_READ_I, pid,
546 1.1 christos (PTRACE_TYPE_ARG3)(uintptr_t)
547 1.1 christos rounded_offset, 0);
548 1.1 christos
549 1.1 christos /* Copy data to be written over corresponding part of
550 1.1 christos buffer. */
551 1.1 christos memcpy (buffer.byte + (offset - rounded_offset),
552 1.1 christos writebuf, partial_len);
553 1.1 christos
554 1.1 christos errno = 0;
555 1.1 christos ptrace (PT_WRITE_D, pid,
556 1.1 christos (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
557 1.1 christos buffer.word);
558 1.1 christos if (errno)
559 1.1 christos {
560 1.1 christos /* Using the appropriate one (I or D) is necessary for
561 1.1 christos Gould NP1, at least. */
562 1.1 christos errno = 0;
563 1.1 christos ptrace (PT_WRITE_I, pid,
564 1.1 christos (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
565 1.1 christos buffer.word);
566 1.1 christos if (errno)
567 1.1 christos return 0;
568 1.1 christos }
569 1.1 christos }
570 1.1 christos
571 1.1 christos if (readbuf)
572 1.1 christos {
573 1.1 christos errno = 0;
574 1.1 christos buffer.word = ptrace (PT_READ_I, pid,
575 1.1 christos (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
576 1.1 christos 0);
577 1.1 christos if (errno)
578 1.1 christos return 0;
579 1.1 christos /* Copy appropriate bytes out of the buffer. */
580 1.1 christos memcpy (readbuf, buffer.byte + (offset - rounded_offset),
581 1.1 christos partial_len);
582 1.1 christos }
583 1.1 christos
584 1.1 christos return partial_len;
585 1.1 christos }
586 1.1 christos
587 1.1 christos case TARGET_OBJECT_UNWIND_TABLE:
588 1.1 christos return -1;
589 1.1 christos
590 1.1 christos case TARGET_OBJECT_AUXV:
591 1.1 christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
592 1.1 christos /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
593 1.1 christos request that allows us to read the auxilliary vector. Other
594 1.1 christos BSD's may follow if they feel the need to support PIE. */
595 1.1 christos {
596 1.1 christos struct ptrace_io_desc piod;
597 1.1 christos
598 1.1 christos if (writebuf)
599 1.1 christos return -1;
600 1.1 christos piod.piod_op = PIOD_READ_AUXV;
601 1.1 christos piod.piod_addr = readbuf;
602 1.1 christos piod.piod_offs = (void *) (long) offset;
603 1.1 christos piod.piod_len = len;
604 1.1 christos
605 1.1 christos errno = 0;
606 1.1 christos if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
607 1.1 christos /* Return the actual number of bytes read or written. */
608 1.1 christos return piod.piod_len;
609 1.1 christos }
610 1.1 christos #endif
611 1.1 christos return -1;
612 1.1 christos
613 1.1 christos case TARGET_OBJECT_WCOOKIE:
614 1.1 christos return -1;
615 1.1 christos
616 1.1 christos default:
617 1.1 christos return -1;
618 1.1 christos }
619 1.1 christos }
620 1.1 christos
621 1.1 christos /* Return non-zero if the thread specified by PTID is alive. */
622 1.1 christos
623 1.1 christos static int
624 1.1 christos inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
625 1.1 christos {
626 1.1 christos /* ??? Is kill the right way to do this? */
627 1.1 christos return (kill (ptid_get_pid (ptid), 0) != -1);
628 1.1 christos }
629 1.1 christos
630 1.1 christos /* Print status information about what we're accessing. */
631 1.1 christos
632 1.1 christos static void
633 1.1 christos inf_ptrace_files_info (struct target_ops *ignore)
634 1.1 christos {
635 1.1 christos struct inferior *inf = current_inferior ();
636 1.1 christos
637 1.1 christos printf_filtered (_("\tUsing the running image of %s %s.\n"),
638 1.1 christos inf->attach_flag ? "attached" : "child",
639 1.1 christos target_pid_to_str (inferior_ptid));
640 1.1 christos }
641 1.1 christos
642 1.1 christos static char *
643 1.1 christos inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
644 1.1 christos {
645 1.1 christos return normal_pid_to_str (ptid);
646 1.1 christos }
647 1.1 christos
648 1.1 christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
649 1.1 christos
650 1.1 christos /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
651 1.1 christos Return 0 if *READPTR is already at the end of the buffer.
652 1.1 christos Return -1 if there is insufficient buffer for a whole entry.
653 1.1 christos Return 1 if an entry was read into *TYPEP and *VALP. */
654 1.1 christos
655 1.1 christos static int
656 1.1 christos inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
657 1.1 christos gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
658 1.1 christos {
659 1.1 christos struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
660 1.1 christos struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
661 1.1 christos const int sizeof_auxv_type = TYPE_LENGTH (int_type);
662 1.1 christos const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
663 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
664 1.1 christos gdb_byte *ptr = *readptr;
665 1.1 christos
666 1.1 christos if (endptr == ptr)
667 1.1 christos return 0;
668 1.1 christos
669 1.1 christos if (endptr - ptr < 2 * sizeof_auxv_val)
670 1.1 christos return -1;
671 1.1 christos
672 1.1 christos *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
673 1.1 christos ptr += sizeof_auxv_val; /* Alignment. */
674 1.1 christos *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
675 1.1 christos ptr += sizeof_auxv_val;
676 1.1 christos
677 1.1 christos *readptr = ptr;
678 1.1 christos return 1;
679 1.1 christos }
680 1.1 christos
681 1.1 christos #endif
682 1.1 christos
683 1.1 christos /* Create a prototype ptrace target. The client can override it with
684 1.1 christos local methods. */
685 1.1 christos
686 1.1 christos struct target_ops *
687 1.1 christos inf_ptrace_target (void)
688 1.1 christos {
689 1.1 christos struct target_ops *t = inf_child_target ();
690 1.1 christos
691 1.1 christos t->to_attach = inf_ptrace_attach;
692 1.1 christos t->to_detach = inf_ptrace_detach;
693 1.1 christos t->to_resume = inf_ptrace_resume;
694 1.1 christos t->to_wait = inf_ptrace_wait;
695 1.1 christos t->to_files_info = inf_ptrace_files_info;
696 1.1 christos t->to_kill = inf_ptrace_kill;
697 1.1 christos t->to_create_inferior = inf_ptrace_create_inferior;
698 1.1 christos #ifdef PT_GET_PROCESS_STATE
699 1.1 christos t->to_follow_fork = inf_ptrace_follow_fork;
700 1.1 christos t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
701 1.1 christos t->to_post_attach = inf_ptrace_post_attach;
702 1.1 christos #endif
703 1.1 christos t->to_mourn_inferior = inf_ptrace_mourn_inferior;
704 1.1 christos t->to_thread_alive = inf_ptrace_thread_alive;
705 1.1 christos t->to_pid_to_str = inf_ptrace_pid_to_str;
706 1.1 christos t->to_stop = inf_ptrace_stop;
707 1.1 christos t->to_xfer_partial = inf_ptrace_xfer_partial;
708 1.1 christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
709 1.1 christos t->to_auxv_parse = inf_ptrace_auxv_parse;
710 1.1 christos #endif
711 1.1 christos
712 1.1 christos return t;
713 1.1 christos }
714 1.1 christos
715 1.1 christos
717 1.1 christos /* Pointer to a function that returns the offset within the user area
718 1.1 christos where a particular register is stored. */
719 1.1 christos static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
720 1.1 christos
721 1.1 christos /* Fetch register REGNUM from the inferior. */
722 1.1 christos
723 1.1 christos static void
724 1.1 christos inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
725 1.1 christos {
726 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
727 1.1 christos CORE_ADDR addr;
728 1.1 christos size_t size;
729 1.1 christos PTRACE_TYPE_RET *buf;
730 1.1 christos int pid, i;
731 1.1 christos
732 1.1 christos /* This isn't really an address, but ptrace thinks of it as one. */
733 1.1 christos addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
734 1.1 christos if (addr == (CORE_ADDR)-1
735 1.1 christos || gdbarch_cannot_fetch_register (gdbarch, regnum))
736 1.1 christos {
737 1.1 christos regcache_raw_supply (regcache, regnum, NULL);
738 1.1 christos return;
739 1.1 christos }
740 1.1 christos
741 1.1 christos /* Cater for systems like GNU/Linux, that implement threads as
742 1.1 christos separate processes. */
743 1.1 christos pid = ptid_get_lwp (inferior_ptid);
744 1.1 christos if (pid == 0)
745 1.1 christos pid = ptid_get_pid (inferior_ptid);
746 1.1 christos
747 1.1 christos size = register_size (gdbarch, regnum);
748 1.1 christos gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
749 1.1 christos buf = alloca (size);
750 1.1 christos
751 1.1 christos /* Read the register contents from the inferior a chunk at a time. */
752 1.1 christos for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
753 1.1 christos {
754 1.1 christos errno = 0;
755 1.1 christos buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
756 1.1 christos if (errno != 0)
757 1.1 christos error (_("Couldn't read register %s (#%d): %s."),
758 1.1 christos gdbarch_register_name (gdbarch, regnum),
759 1.1 christos regnum, safe_strerror (errno));
760 1.1 christos
761 1.1 christos addr += sizeof (PTRACE_TYPE_RET);
762 1.1 christos }
763 1.1 christos regcache_raw_supply (regcache, regnum, buf);
764 1.1 christos }
765 1.1 christos
766 1.1 christos /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
767 1.1 christos for all registers. */
768 1.1 christos
769 1.1 christos static void
770 1.1 christos inf_ptrace_fetch_registers (struct target_ops *ops,
771 1.1 christos struct regcache *regcache, int regnum)
772 1.1 christos {
773 1.1 christos if (regnum == -1)
774 1.1 christos for (regnum = 0;
775 1.1 christos regnum < gdbarch_num_regs (get_regcache_arch (regcache));
776 1.1 christos regnum++)
777 1.1 christos inf_ptrace_fetch_register (regcache, regnum);
778 1.1 christos else
779 1.1 christos inf_ptrace_fetch_register (regcache, regnum);
780 1.1 christos }
781 1.1 christos
782 1.1 christos /* Store register REGNUM into the inferior. */
783 1.1 christos
784 1.1 christos static void
785 1.1 christos inf_ptrace_store_register (const struct regcache *regcache, int regnum)
786 1.1 christos {
787 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
788 1.1 christos CORE_ADDR addr;
789 1.1 christos size_t size;
790 1.1 christos PTRACE_TYPE_RET *buf;
791 1.1 christos int pid, i;
792 1.1 christos
793 1.1 christos /* This isn't really an address, but ptrace thinks of it as one. */
794 1.1 christos addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
795 1.1 christos if (addr == (CORE_ADDR)-1
796 1.1 christos || gdbarch_cannot_store_register (gdbarch, regnum))
797 1.1 christos return;
798 1.1 christos
799 1.1 christos /* Cater for systems like GNU/Linux, that implement threads as
800 1.1 christos separate processes. */
801 1.1 christos pid = ptid_get_lwp (inferior_ptid);
802 1.1 christos if (pid == 0)
803 1.1 christos pid = ptid_get_pid (inferior_ptid);
804 1.1 christos
805 1.1 christos size = register_size (gdbarch, regnum);
806 1.1 christos gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
807 1.1 christos buf = alloca (size);
808 1.1 christos
809 1.1 christos /* Write the register contents into the inferior a chunk at a time. */
810 1.1 christos regcache_raw_collect (regcache, regnum, buf);
811 1.1 christos for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
812 1.1 christos {
813 1.1 christos errno = 0;
814 1.1 christos ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
815 1.1 christos if (errno != 0)
816 1.1 christos error (_("Couldn't write register %s (#%d): %s."),
817 1.1 christos gdbarch_register_name (gdbarch, regnum),
818 1.1 christos regnum, safe_strerror (errno));
819 1.1 christos
820 1.1 christos addr += sizeof (PTRACE_TYPE_RET);
821 1.1 christos }
822 1.1 christos }
823 1.1 christos
824 1.1 christos /* Store register REGNUM back into the inferior. If REGNUM is -1, do
825 1.1 christos this for all registers. */
826 1.1 christos
827 1.1 christos static void
828 1.1 christos inf_ptrace_store_registers (struct target_ops *ops,
829 1.1 christos struct regcache *regcache, int regnum)
830 1.1 christos {
831 1.1 christos if (regnum == -1)
832 1.1 christos for (regnum = 0;
833 1.1 christos regnum < gdbarch_num_regs (get_regcache_arch (regcache));
834 1.1 christos regnum++)
835 1.1 christos inf_ptrace_store_register (regcache, regnum);
836 1.1 christos else
837 1.1 christos inf_ptrace_store_register (regcache, regnum);
838 1.1 christos }
839 1.1 christos
840 1.1 christos /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
841 1.1 christos a function returning the offset within the user area where a
842 1.1 christos particular register is stored. */
843 1.1 christos
844 1.1 christos struct target_ops *
845 1.1 christos inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
846 1.1 christos (struct gdbarch *, int, int))
847 1.1 christos {
848 1.1 christos struct target_ops *t = inf_ptrace_target();
849 1.1 christos
850 1.1 christos gdb_assert (register_u_offset);
851 1.1 christos inf_ptrace_register_u_offset = register_u_offset;
852 1.1 christos t->to_fetch_registers = inf_ptrace_fetch_registers;
853 1.1 christos t->to_store_registers = inf_ptrace_store_registers;
854
855 return t;
856 }
857