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