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