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