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