Improve ptrace-error detection on Linux targets
[deliverable/binutils-gdb.git] / gdb / nat / linux-ptrace.c
CommitLineData
5f572dec 1/* Linux-specific ptrace manipulation routines.
42a4f53d 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
5f572dec
JK
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
268a13a5 19#include "gdbsupport/common-defs.h"
5f572dec 20#include "linux-ptrace.h"
87b0bb13 21#include "linux-procfs.h"
125f8a3d 22#include "linux-waitpid.h"
268a13a5 23#include "gdbsupport/buffer.h"
381beca6
SDJ
24#include "gdbsupport/gdb-dlfcn.h"
25#include "nat/fork-inferior.h"
26#include "gdbsupport/filestuff.h"
2132fe85 27#ifdef HAVE_SYS_PROCFS_H
39b22471 28#include <sys/procfs.h>
2132fe85 29#endif
87b0bb13 30
de0d863e
DB
31/* Stores the ptrace options supported by the running kernel.
32 A value of -1 means we did not check for features yet. A value
33 of 0 means there are no supported features. */
34static int supported_ptrace_options = -1;
8009206a 35
381beca6 36typedef int (*selinux_ftype) (const char *);
87b0bb13 37
381beca6
SDJ
38/* Helper function which checks if ptrace is probably restricted
39 (i.e., if ERR is either EACCES or EPERM), and returns a string with
40 possible workarounds. */
41
42static std::string
43linux_ptrace_restricted_fail_reason (int err)
44{
45 if (err != EACCES && err != EPERM)
46 {
47 /* It just makes sense to perform the checks below if errno was
48 either EACCES or EPERM. */
49 return {};
50 }
51
52 std::string ret;
53 gdb_dlhandle_up handle;
54
55 try
56 {
57 handle = gdb_dlopen ("libselinux.so.1");
58 }
59 catch (const gdb_exception_error &e)
60 {
61 handle.reset (nullptr);
62 }
63
64 if (handle != nullptr)
65 {
66 selinux_ftype selinux_get_bool
67 = (selinux_ftype) gdb_dlsym (handle, "security_get_boolean_active");
68
69 if (selinux_get_bool != NULL
70 && (*selinux_get_bool) ("deny_ptrace") == 1)
71 string_appendf (ret,
72 _("\n\
73The SELinux 'deny_ptrace' option is enabled and preventing GDB\n\
74from using 'ptrace'. You can disable it by executing (as root):\n\
75\n\
76 setsebool deny_ptrace off\n"));
77 }
78
79 gdb_file_up yama_ptrace_scope
80 = gdb_fopen_cloexec ("/proc/sys/kernel/yama/ptrace_scope", "r");
81
82 if (yama_ptrace_scope != nullptr)
83 {
84 char yama_scope = fgetc (yama_ptrace_scope.get ());
85
86 if (yama_scope != '0')
87 string_appendf (ret,
88 _("\n\
89The Linux kernel's Yama ptrace scope is in effect, which can prevent\n\
90GDB from using 'ptrace'. You can disable it by executing (as root):\n\
91\n\
92 echo 0 > /proc/sys/kernel/yama/ptrace_scope\n"));
93 }
94
95 if (ret.empty ())
96 {
97 /* It wasn't possible to determine the exact reason for the
98 ptrace error. Let's just emit a generic error message
99 pointing the user to our documentation, where she can find
100 instructions on how to try to diagnose the problem. */
101 ret = _("\n\
102There might be restrictions preventing ptrace from working. Please see\n\
103the appendix \"Linux kernel ptrace restrictions\" in the GDB documentation\n\
104for more details.");
105 }
106
107 /* The user may be debugging remotely, so we have to warn that
108 the instructions above should be performed in the target. */
109 string_appendf (ret,
110 _("\n\
111If you are debugging the inferior remotely, the ptrace restriction(s) must\n\
112be disabled in the target system (e.g., where GDBserver is running)."));
113
114 return ret;
115}
116
117/* Find all possible reasons we could fail to attach PID and return
118 these as a string. An empty string is returned if we didn't find
119 any reason. Helper for linux_ptrace_attach_fail_reason and
120 linux_ptrace_attach_fail_reason_lwp. */
121
122static std::string
123linux_ptrace_attach_fail_reason_1 (pid_t pid)
87b0bb13 124{
4d9b86e1
SM
125 pid_t tracerpid = linux_proc_get_tracerpid_nowarn (pid);
126 std::string result;
87b0bb13 127
87b0bb13 128 if (tracerpid > 0)
4d9b86e1
SM
129 string_appendf (result,
130 _("process %d is already traced by process %d"),
131 (int) pid, (int) tracerpid);
87b0bb13 132
8784d563 133 if (linux_proc_pid_is_zombie_nowarn (pid))
4d9b86e1
SM
134 string_appendf (result,
135 _("process %d is a zombie - the process has already "
136 "terminated"),
137 (int) pid);
138
139 return result;
87b0bb13 140}
aa7c7447 141
8784d563
PA
142/* See linux-ptrace.h. */
143
4d9b86e1 144std::string
381beca6
SDJ
145linux_ptrace_attach_fail_reason (pid_t pid, int err)
146{
147 std::string result = linux_ptrace_attach_fail_reason_1 (pid);
148 std::string ptrace_restrict = linux_ptrace_restricted_fail_reason (err);
149
150 if (!ptrace_restrict.empty ())
151 result += "\n" + ptrace_restrict;
152
153 return result;
154}
155
156/* See linux-ptrace.h. */
157
158std::string
159linux_ptrace_attach_fail_reason_lwp (ptid_t ptid, int err)
8784d563 160{
4d9b86e1 161 long lwpid = ptid.lwp ();
381beca6 162 std::string reason = linux_ptrace_attach_fail_reason_1 (lwpid);
4d9b86e1
SM
163
164 if (!reason.empty ())
165 return string_printf ("%s (%d), %s", safe_strerror (err), err,
166 reason.c_str ());
8784d563 167 else
4d9b86e1 168 return string_printf ("%s (%d)", safe_strerror (err), err);
8784d563
PA
169}
170
381beca6
SDJ
171/* See linux-ptrace.h. */
172
173std::string
174linux_ptrace_me_fail_reason (int err)
175{
176 return linux_ptrace_restricted_fail_reason (err);
177}
178
6e3c039e 179#if defined __i386__ || defined __x86_64__
aa7c7447
JK
180
181/* Address of the 'ret' instruction in asm code block below. */
56000a98 182EXTERN_C void linux_ptrace_test_ret_to_nx_instr (void);
aa7c7447
JK
183
184#include <sys/reg.h>
185#include <sys/mman.h>
186#include <signal.h>
aa7c7447 187
6e3c039e 188#endif /* defined __i386__ || defined __x86_64__ */
aa7c7447 189
a65f6835
PW
190/* Kill CHILD. WHO is used to report warnings. */
191
192static void
193kill_child (pid_t child, const char *who)
194{
195 pid_t got_pid;
196 int kill_status;
197
198 if (kill (child, SIGKILL) != 0)
199 {
4ef5dbe4 200 warning (_("%s: failed to kill child pid %ld %s"),
a65f6835
PW
201 who, (long) child, safe_strerror (errno));
202 return;
203 }
204
205 errno = 0;
206 got_pid = my_waitpid (child, &kill_status, 0);
207 if (got_pid != child)
208 {
209 warning (_("%s: "
210 "kill waitpid returned %ld: %s"),
211 who, (long) got_pid, safe_strerror (errno));
212 return;
213 }
214 if (!WIFSIGNALED (kill_status))
215 {
216 warning (_("%s: "
217 "kill status %d is not WIFSIGNALED!"),
218 who, kill_status);
219 return;
220 }
221}
222
aa7c7447 223/* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
6e3c039e
JK
224 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
225
226 Test also x86_64 arch for PaX support. */
aa7c7447
JK
227
228static void
229linux_ptrace_test_ret_to_nx (void)
230{
6e3c039e 231#if defined __i386__ || defined __x86_64__
aa7c7447
JK
232 pid_t child, got_pid;
233 gdb_byte *return_address, *pc;
234 long l;
a65f6835 235 int status;
40c31709 236 elf_gregset_t regs;
aa7c7447 237
224c3ddb
SM
238 return_address
239 = (gdb_byte *) mmap (NULL, 2, PROT_READ | PROT_WRITE,
aa7c7447
JK
240 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
241 if (return_address == MAP_FAILED)
242 {
243 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
049bb5de 244 safe_strerror (errno));
aa7c7447
JK
245 return;
246 }
247
248 /* Put there 'int3'. */
249 *return_address = 0xcc;
250
251 child = fork ();
252 switch (child)
253 {
254 case -1:
255 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
049bb5de 256 safe_strerror (errno));
aa7c7447
JK
257 return;
258
259 case 0:
96d7229d
LM
260 l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL,
261 (PTRACE_TYPE_ARG4) NULL);
aa7c7447
JK
262 if (l != 0)
263 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
049bb5de 264 safe_strerror (errno));
aa7c7447
JK
265 else
266 {
6e3c039e 267#if defined __i386__
aa7c7447
JK
268 asm volatile ("pushl %0;"
269 ".globl linux_ptrace_test_ret_to_nx_instr;"
270 "linux_ptrace_test_ret_to_nx_instr:"
271 "ret"
7406a500 272 : : "r" (return_address) : "memory");
6e3c039e
JK
273#elif defined __x86_64__
274 asm volatile ("pushq %0;"
275 ".globl linux_ptrace_test_ret_to_nx_instr;"
276 "linux_ptrace_test_ret_to_nx_instr:"
277 "ret"
bdad4180 278 : : "r" ((uint64_t) (uintptr_t) return_address)
7406a500 279 : "memory");
6e3c039e
JK
280#else
281# error "!__i386__ && !__x86_64__"
282#endif
aa7c7447
JK
283 gdb_assert_not_reached ("asm block did not terminate");
284 }
285
286 _exit (1);
287 }
288
6e3c039e 289 errno = 0;
aa7c7447 290 got_pid = waitpid (child, &status, 0);
6e3c039e
JK
291 if (got_pid != child)
292 {
293 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
049bb5de 294 (long) got_pid, safe_strerror (errno));
6e3c039e
JK
295 return;
296 }
297
298 if (WIFSIGNALED (status))
299 {
300 if (WTERMSIG (status) != SIGKILL)
301 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
302 (int) WTERMSIG (status));
303 else
304 warning (_("Cannot call inferior functions, Linux kernel PaX "
305 "protection forbids return to non-executable pages!"));
306 return;
307 }
308
309 if (!WIFSTOPPED (status))
310 {
311 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
312 status);
a65f6835 313 kill_child (child, "linux_ptrace_test_ret_to_nx");
6e3c039e
JK
314 return;
315 }
aa7c7447
JK
316
317 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
6e3c039e
JK
318 if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
319 {
320 warning (_("linux_ptrace_test_ret_to_nx: "
321 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
322 (int) WSTOPSIG (status));
a65f6835 323 kill_child (child, "linux_ptrace_test_ret_to_nx");
6e3c039e
JK
324 return;
325 }
aa7c7447 326
40c31709
PA
327 if (ptrace (PTRACE_GETREGS, child, (PTRACE_TYPE_ARG3) 0,
328 (PTRACE_TYPE_ARG4) &regs) < 0)
329 {
330 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_GETREGS: %s"),
331 safe_strerror (errno));
332 }
6e3c039e 333#if defined __i386__
40c31709 334 pc = (gdb_byte *) (uintptr_t) regs[EIP];
6e3c039e 335#elif defined __x86_64__
40c31709 336 pc = (gdb_byte *) (uintptr_t) regs[RIP];
6e3c039e
JK
337#else
338# error "!__i386__ && !__x86_64__"
339#endif
aa7c7447 340
a65f6835 341 kill_child (child, "linux_ptrace_test_ret_to_nx");
aa7c7447
JK
342
343 /* + 1 is there as x86* stops after the 'int3' instruction. */
344 if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1)
345 {
346 /* PASS */
347 return;
348 }
349
350 /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
351 if (WSTOPSIG (status) == SIGSEGV && pc == return_address)
352 {
353 /* PASS */
354 return;
355 }
356
6e3c039e
JK
357 if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr)
358 warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
359 "address %p nor is the return instruction %p!"),
360 pc, return_address, &linux_ptrace_test_ret_to_nx_instr);
361 else
025e6dce
PA
362 warning (_("Cannot call inferior functions on this system - "
363 "Linux kernel with broken i386 NX (non-executable pages) "
364 "support detected!"));
6e3c039e 365#endif /* defined __i386__ || defined __x86_64__ */
aa7c7447
JK
366}
367
381beca6
SDJ
368/* If the PTRACE_TRACEME call on linux_child_function errors, we need
369 to be able to send ERRNO back to the parent so that it can check
370 whether there are restrictions in place preventing ptrace from
371 working. We do that with a pipe. */
372static int errno_pipe[2];
373
96d7229d
LM
374/* Helper function to fork a process and make the child process call
375 the function FUNCTION, passing CHILD_STACK as parameter.
376
377 For MMU-less targets, clone is used instead of fork, and
378 CHILD_STACK is used as stack space for the cloned child. If NULL,
379 stack space is allocated via malloc (and subsequently passed to
380 FUNCTION). For MMU targets, CHILD_STACK is ignored. */
381
382static int
ba4dd7c4 383linux_fork_to_function (gdb_byte *child_stack, int (*function) (void *))
96d7229d
LM
384{
385 int child_pid;
386
387 /* Sanity check the function pointer. */
388 gdb_assert (function != NULL);
389
381beca6
SDJ
390 /* Create the pipe that will be used by the child to pass ERRNO
391 after the PTRACE_TRACEME call. */
392 if (pipe (errno_pipe) != 0)
393 trace_start_error_with_name ("pipe");
394
96d7229d
LM
395#if defined(__UCLIBC__) && defined(HAS_NOMMU)
396#define STACK_SIZE 4096
397
398 if (child_stack == NULL)
ffce45d2 399 child_stack = (gdb_byte *) xmalloc (STACK_SIZE * 4);
96d7229d
LM
400
401 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
101158d9 402#ifdef __ia64__
96d7229d
LM
403 child_pid = __clone2 (function, child_stack, STACK_SIZE,
404 CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
101158d9 405#else /* !__ia64__ */
96d7229d
LM
406 child_pid = clone (function, child_stack + STACK_SIZE,
407 CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
101158d9 408#endif /* !__ia64__ */
96d7229d
LM
409#else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
410 child_pid = fork ();
411
412 if (child_pid == 0)
413 function (NULL);
414#endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
415
416 if (child_pid == -1)
417 perror_with_name (("fork"));
418
419 return child_pid;
420}
421
422/* A helper function for linux_check_ptrace_features, called after
423 the child forks a grandchild. */
424
ba4dd7c4
YQ
425static int
426linux_grandchild_function (void *child_stack)
96d7229d
LM
427{
428 /* Free any allocated stack. */
429 xfree (child_stack);
430
431 /* This code is only reacheable by the grandchild (child's child)
432 process. */
433 _exit (0);
434}
435
436/* A helper function for linux_check_ptrace_features, called after
437 the parent process forks a child. The child allows itself to
438 be traced by its parent. */
439
ba4dd7c4
YQ
440static int
441linux_child_function (void *child_stack)
96d7229d 442{
381beca6
SDJ
443 /* Close read end. */
444 close (errno_pipe[0]);
445
446 int ret = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
447 (PTRACE_TYPE_ARG4) 0);
448 int ptrace_errno = errno;
449
450 /* Write ERRNO to the pipe, even if it's zero, and close the writing
451 end of the pipe. */
452 write (errno_pipe[1], &ptrace_errno, sizeof (ptrace_errno));
453 close (errno_pipe[1]);
454
455 if (ret != 0)
456 _exit (0);
457
96d7229d
LM
458 kill (getpid (), SIGSTOP);
459
460 /* Fork a grandchild. */
d18547d8 461 linux_fork_to_function ((gdb_byte *) child_stack, linux_grandchild_function);
96d7229d
LM
462
463 /* This code is only reacheable by the child (grandchild's parent)
464 process. */
465 _exit (0);
466}
467
8ae377e8
PA
468static void linux_test_for_tracesysgood (int child_pid);
469static void linux_test_for_tracefork (int child_pid);
beed38b8 470static void linux_test_for_exitkill (int child_pid);
8ae377e8 471
381beca6
SDJ
472/* Helper function to wait for the child to send us the ptrace ERRNO,
473 and check if it's OK. */
474
475static void
476linux_check_child_ptrace_errno ()
477{
478 int child_errno;
479 fd_set rset;
480 struct timeval timeout;
481
482 /* Close the writing end of the pipe. */
483 close (errno_pipe[1]);
484
485 FD_ZERO (&rset);
486 FD_SET (errno_pipe[0], &rset);
487
488 /* One second should be plenty of time to wait for the child's
489 reply. */
490 timeout.tv_sec = 1;
491 timeout.tv_usec = 0;
492
493 int ret = select (errno_pipe[0] + 1, &rset, NULL, NULL, &timeout);
494
495 if (ret < 0)
496 trace_start_error_with_name ("select");
497 else if (ret == 0)
498 error (_("Timeout while waiting for child's ptrace errno"));
499 else
500 read (errno_pipe[0], &child_errno, sizeof (child_errno));
501
502 if (child_errno != 0)
503 {
504 /* The child can't use PTRACE_TRACEME. We just bail out. */
505 std::string reason = linux_ptrace_restricted_fail_reason (child_errno);
506
507 errno = child_errno;
508 trace_start_error_with_name ("ptrace", reason.c_str ());
509 }
510
511 close (errno_pipe[0]);
512}
513
96d7229d
LM
514/* Determine ptrace features available on this target. */
515
89245bc0 516void
96d7229d
LM
517linux_check_ptrace_features (void)
518{
519 int child_pid, ret, status;
96d7229d
LM
520
521 /* Initialize the options. */
de0d863e 522 supported_ptrace_options = 0;
96d7229d
LM
523
524 /* Fork a child so we can do some testing. The child will call
525 linux_child_function and will get traced. The child will
526 eventually fork a grandchild so we can test fork event
527 reporting. */
528 child_pid = linux_fork_to_function (NULL, linux_child_function);
529
381beca6
SDJ
530 /* Check if the child can successfully use ptrace. */
531 linux_check_child_ptrace_errno ();
532
96d7229d
LM
533 ret = my_waitpid (child_pid, &status, 0);
534 if (ret == -1)
535 perror_with_name (("waitpid"));
536 else if (ret != child_pid)
537 error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
538 ret);
539 if (! WIFSTOPPED (status))
540 error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
541 status);
542
8ae377e8 543 linux_test_for_tracesysgood (child_pid);
96d7229d 544
8ae377e8
PA
545 linux_test_for_tracefork (child_pid);
546
beed38b8
JB
547 linux_test_for_exitkill (child_pid);
548
a65f6835
PW
549 /* Kill child_pid. */
550 kill_child (child_pid, "linux_check_ptrace_features");
8ae377e8 551}
96d7229d 552
8ae377e8
PA
553/* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
554 syscalls. */
555
556static void
557linux_test_for_tracesysgood (int child_pid)
558{
8ae377e8
PA
559 int ret;
560
96d7229d
LM
561 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
562 (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD);
8009206a 563
96d7229d 564 if (ret == 0)
de0d863e 565 supported_ptrace_options |= PTRACE_O_TRACESYSGOOD;
8ae377e8 566}
96d7229d 567
8ae377e8
PA
568/* Determine if PTRACE_O_TRACEFORK can be used to follow fork
569 events. */
570
571static void
572linux_test_for_tracefork (int child_pid)
573{
574 int ret, status;
575 long second_pid;
576
577 /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
578 know for sure that it is not supported. */
579 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
580 (PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK);
581
582 if (ret != 0)
583 return;
584
de0d863e
DB
585 /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
586 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
587 (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK
588 | PTRACE_O_TRACEVFORKDONE));
589 if (ret == 0)
590 supported_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
96d7229d
LM
591
592 /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
593 don't know for sure that the feature is available; old
594 versions of PTRACE_SETOPTIONS ignored unknown options.
595 Therefore, we attach to the child process, use PTRACE_SETOPTIONS
596 to enable fork tracing, and let it fork. If the process exits,
597 we assume that we can't use PTRACE_O_TRACEFORK; if we get the
598 fork notification, and we can extract the new child's PID, then
599 we assume that we can.
600
601 We do not explicitly check for vfork tracing here. It is
602 assumed that vfork tracing is available whenever fork tracing
603 is available. */
604 ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0,
605 (PTRACE_TYPE_ARG4) 0);
606 if (ret != 0)
8ae377e8 607 warning (_("linux_test_for_tracefork: failed to resume child"));
96d7229d
LM
608
609 ret = my_waitpid (child_pid, &status, 0);
610
611 /* Check if we received a fork event notification. */
612 if (ret == child_pid && WIFSTOPPED (status)
89a5711c 613 && linux_ptrace_get_extended_event (status) == PTRACE_EVENT_FORK)
96d7229d
LM
614 {
615 /* We did receive a fork event notification. Make sure its PID
616 is reported. */
617 second_pid = 0;
618 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0,
619 (PTRACE_TYPE_ARG4) &second_pid);
620 if (ret == 0 && second_pid != 0)
621 {
622 int second_status;
623
624 /* We got the PID from the grandchild, which means fork
625 tracing is supported. */
de0d863e
DB
626 supported_ptrace_options |= PTRACE_O_TRACECLONE;
627 supported_ptrace_options |= (PTRACE_O_TRACEFORK
628 | PTRACE_O_TRACEVFORK
629 | PTRACE_O_TRACEEXEC);
96d7229d
LM
630
631 /* Do some cleanup and kill the grandchild. */
632 my_waitpid (second_pid, &second_status, 0);
a65f6835 633 kill_child (second_pid, "linux_test_for_tracefork");
96d7229d
LM
634 }
635 }
636 else
8ae377e8 637 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
96d7229d 638 "(%d, status 0x%x)"), ret, status);
96d7229d
LM
639}
640
beed38b8
JB
641/* Determine if PTRACE_O_EXITKILL can be used. */
642
643static void
644linux_test_for_exitkill (int child_pid)
645{
646 int ret;
647
648 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
649 (PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL);
650
651 if (ret == 0)
de0d863e 652 supported_ptrace_options |= PTRACE_O_EXITKILL;
beed38b8
JB
653}
654
655/* Enable reporting of all currently supported ptrace events.
de0d863e
DB
656 OPTIONS is a bit mask of extended features we want enabled,
657 if supported by the kernel. PTRACE_O_TRACECLONE is always
658 enabled, if supported. */
96d7229d
LM
659
660void
de0d863e 661linux_enable_event_reporting (pid_t pid, int options)
96d7229d
LM
662{
663 /* Check if we have initialized the ptrace features for this
664 target. If not, do it now. */
de0d863e 665 if (supported_ptrace_options == -1)
96d7229d
LM
666 linux_check_ptrace_features ();
667
de0d863e
DB
668 /* We always want clone events. */
669 options |= PTRACE_O_TRACECLONE;
670
671 /* Filter out unsupported options. */
672 options &= supported_ptrace_options;
beed38b8 673
96d7229d
LM
674 /* Set the options. */
675 ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0,
de0d863e 676 (PTRACE_TYPE_ARG4) (uintptr_t) options);
96d7229d
LM
677}
678
c077881a
HZ
679/* Disable reporting of all currently supported ptrace events. */
680
681void
682linux_disable_event_reporting (pid_t pid)
683{
684 /* Set the options. */
685 ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, 0);
686}
687
96d7229d 688/* Returns non-zero if PTRACE_OPTIONS is contained within
de0d863e 689 SUPPORTED_PTRACE_OPTIONS, therefore supported. Returns 0
96d7229d
LM
690 otherwise. */
691
692static int
693ptrace_supports_feature (int ptrace_options)
694{
de0d863e 695 if (supported_ptrace_options == -1)
8784d563 696 linux_check_ptrace_features ();
96d7229d 697
de0d863e 698 return ((supported_ptrace_options & ptrace_options) == ptrace_options);
96d7229d
LM
699}
700
701/* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
702 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
703 PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
704 since they were all added to the kernel at the same time. */
705
706int
707linux_supports_tracefork (void)
708{
709 return ptrace_supports_feature (PTRACE_O_TRACEFORK);
710}
711
94585166
DB
712/* Returns non-zero if PTRACE_EVENT_EXEC is supported by ptrace,
713 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
714 PTRACE_EVENT_CLONE, PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK,
715 since they were all added to the kernel at the same time. */
716
717int
718linux_supports_traceexec (void)
719{
720 return ptrace_supports_feature (PTRACE_O_TRACEEXEC);
721}
722
96d7229d
LM
723/* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
724 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
725 PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
726 since they were all added to the kernel at the same time. */
727
728int
729linux_supports_traceclone (void)
730{
731 return ptrace_supports_feature (PTRACE_O_TRACECLONE);
732}
733
734/* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
735 ptrace, 0 otherwise. */
736
737int
738linux_supports_tracevforkdone (void)
739{
740 return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE);
741}
742
743/* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
744 0 otherwise. */
745
746int
747linux_supports_tracesysgood (void)
748{
749 return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD);
750}
751
aa7c7447
JK
752/* Display possible problems on this system. Display them only once per GDB
753 execution. */
754
755void
756linux_ptrace_init_warnings (void)
757{
758 static int warned = 0;
759
760 if (warned)
761 return;
762 warned = 1;
763
764 linux_ptrace_test_ret_to_nx ();
765}
8009206a 766
89a5711c
DB
767/* Extract extended ptrace event from wait status. */
768
769int
770linux_ptrace_get_extended_event (int wstat)
771{
772 return (wstat >> 16);
773}
774
775/* Determine whether wait status denotes an extended event. */
776
777int
778linux_is_extended_waitstatus (int wstat)
779{
780 return (linux_ptrace_get_extended_event (wstat) != 0);
781}
c9587f88
AT
782
783/* Return true if the event in LP may be caused by breakpoint. */
784
785int
786linux_wstatus_maybe_breakpoint (int wstat)
787{
788 return (WIFSTOPPED (wstat)
789 && (WSTOPSIG (wstat) == SIGTRAP
790 /* SIGILL and SIGSEGV are also treated as traps in case a
791 breakpoint is inserted at the current PC. */
792 || WSTOPSIG (wstat) == SIGILL
793 || WSTOPSIG (wstat) == SIGSEGV));
794}
This page took 0.657412 seconds and 4 git commands to generate.