1 /* Linux-specific ptrace manipulation routines.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #include "common-defs.h"
20 #include "linux-ptrace.h"
21 #include "linux-procfs.h"
22 #include "linux-waitpid.h"
25 #include "gdb_ptrace.h"
26 #include <sys/procfs.h>
28 /* Stores the ptrace options supported by the running kernel.
29 A value of -1 means we did not check for features yet. A value
30 of 0 means there are no supported features. */
31 static int supported_ptrace_options
= -1;
33 /* Find all possible reasons we could fail to attach PID and append
34 these as strings to the already initialized BUFFER. '\0'
35 termination of BUFFER must be done by the caller. */
38 linux_ptrace_attach_fail_reason (pid_t pid
, struct buffer
*buffer
)
42 tracerpid
= linux_proc_get_tracerpid_nowarn (pid
);
44 buffer_xml_printf (buffer
, _("process %d is already traced "
46 (int) pid
, (int) tracerpid
);
48 if (linux_proc_pid_is_zombie_nowarn (pid
))
49 buffer_xml_printf (buffer
, _("process %d is a zombie "
50 "- the process has already terminated"),
54 /* See linux-ptrace.h. */
57 linux_ptrace_attach_fail_reason_string (ptid_t ptid
, int err
)
59 static char *reason_string
;
62 long lwpid
= ptid_get_lwp (ptid
);
64 xfree (reason_string
);
66 buffer_init (&buffer
);
67 linux_ptrace_attach_fail_reason (lwpid
, &buffer
);
68 buffer_grow_str0 (&buffer
, "");
69 warnings
= buffer_finish (&buffer
);
70 if (warnings
[0] != '\0')
71 reason_string
= xstrprintf ("%s (%d), %s",
72 safe_strerror (err
), err
, warnings
);
74 reason_string
= xstrprintf ("%s (%d)",
75 safe_strerror (err
), err
);
80 #if defined __i386__ || defined __x86_64__
82 /* Address of the 'ret' instruction in asm code block below. */
83 EXTERN_C
void linux_ptrace_test_ret_to_nx_instr (void);
89 #endif /* defined __i386__ || defined __x86_64__ */
91 /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
92 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
94 Test also x86_64 arch for PaX support. */
97 linux_ptrace_test_ret_to_nx (void)
99 #if defined __i386__ || defined __x86_64__
100 pid_t child
, got_pid
;
101 gdb_byte
*return_address
, *pc
;
103 int status
, kill_status
;
107 = (gdb_byte
*) mmap (NULL
, 2, PROT_READ
| PROT_WRITE
,
108 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
109 if (return_address
== MAP_FAILED
)
111 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
112 safe_strerror (errno
));
116 /* Put there 'int3'. */
117 *return_address
= 0xcc;
123 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
124 safe_strerror (errno
));
128 l
= ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) NULL
,
129 (PTRACE_TYPE_ARG4
) NULL
);
131 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
132 safe_strerror (errno
));
136 asm volatile ("pushl %0;"
137 ".globl linux_ptrace_test_ret_to_nx_instr;"
138 "linux_ptrace_test_ret_to_nx_instr:"
140 : : "r" (return_address
) : "%esp", "memory");
141 #elif defined __x86_64__
142 asm volatile ("pushq %0;"
143 ".globl linux_ptrace_test_ret_to_nx_instr;"
144 "linux_ptrace_test_ret_to_nx_instr:"
146 : : "r" ((uint64_t) (uintptr_t) return_address
)
149 # error "!__i386__ && !__x86_64__"
151 gdb_assert_not_reached ("asm block did not terminate");
158 got_pid
= waitpid (child
, &status
, 0);
159 if (got_pid
!= child
)
161 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
162 (long) got_pid
, safe_strerror (errno
));
166 if (WIFSIGNALED (status
))
168 if (WTERMSIG (status
) != SIGKILL
)
169 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
170 (int) WTERMSIG (status
));
172 warning (_("Cannot call inferior functions, Linux kernel PaX "
173 "protection forbids return to non-executable pages!"));
177 if (!WIFSTOPPED (status
))
179 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
184 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
185 if (WSTOPSIG (status
) != SIGTRAP
&& WSTOPSIG (status
) != SIGSEGV
)
187 warning (_("linux_ptrace_test_ret_to_nx: "
188 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
189 (int) WSTOPSIG (status
));
193 if (ptrace (PTRACE_GETREGS
, child
, (PTRACE_TYPE_ARG3
) 0,
194 (PTRACE_TYPE_ARG4
) ®s
) < 0)
196 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_GETREGS: %s"),
197 safe_strerror (errno
));
200 pc
= (gdb_byte
*) (uintptr_t) regs
[EIP
];
201 #elif defined __x86_64__
202 pc
= (gdb_byte
*) (uintptr_t) regs
[RIP
];
204 # error "!__i386__ && !__x86_64__"
207 kill (child
, SIGKILL
);
208 ptrace (PTRACE_KILL
, child
, (PTRACE_TYPE_ARG3
) NULL
,
209 (PTRACE_TYPE_ARG4
) NULL
);
212 got_pid
= waitpid (child
, &kill_status
, 0);
213 if (got_pid
!= child
)
215 warning (_("linux_ptrace_test_ret_to_nx: "
216 "PTRACE_KILL waitpid returned %ld: %s"),
217 (long) got_pid
, safe_strerror (errno
));
220 if (!WIFSIGNALED (kill_status
))
222 warning (_("linux_ptrace_test_ret_to_nx: "
223 "PTRACE_KILL status %d is not WIFSIGNALED!"),
228 /* + 1 is there as x86* stops after the 'int3' instruction. */
229 if (WSTOPSIG (status
) == SIGTRAP
&& pc
== return_address
+ 1)
235 /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
236 if (WSTOPSIG (status
) == SIGSEGV
&& pc
== return_address
)
242 if ((void (*) (void)) pc
!= &linux_ptrace_test_ret_to_nx_instr
)
243 warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
244 "address %p nor is the return instruction %p!"),
245 pc
, return_address
, &linux_ptrace_test_ret_to_nx_instr
);
247 warning (_("Cannot call inferior functions on this system - "
248 "Linux kernel with broken i386 NX (non-executable pages) "
249 "support detected!"));
250 #endif /* defined __i386__ || defined __x86_64__ */
253 /* Helper function to fork a process and make the child process call
254 the function FUNCTION, passing CHILD_STACK as parameter.
256 For MMU-less targets, clone is used instead of fork, and
257 CHILD_STACK is used as stack space for the cloned child. If NULL,
258 stack space is allocated via malloc (and subsequently passed to
259 FUNCTION). For MMU targets, CHILD_STACK is ignored. */
262 linux_fork_to_function (gdb_byte
*child_stack
, int (*function
) (void *))
266 /* Sanity check the function pointer. */
267 gdb_assert (function
!= NULL
);
269 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
270 #define STACK_SIZE 4096
272 if (child_stack
== NULL
)
273 child_stack
= xmalloc (STACK_SIZE
* 4);
275 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
277 child_pid
= __clone2 (function
, child_stack
, STACK_SIZE
,
278 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
279 #else /* !__ia64__ */
280 child_pid
= clone (function
, child_stack
+ STACK_SIZE
,
281 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
282 #endif /* !__ia64__ */
283 #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
288 #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
291 perror_with_name (("fork"));
296 /* A helper function for linux_check_ptrace_features, called after
297 the child forks a grandchild. */
300 linux_grandchild_function (void *child_stack
)
302 /* Free any allocated stack. */
305 /* This code is only reacheable by the grandchild (child's child)
310 /* A helper function for linux_check_ptrace_features, called after
311 the parent process forks a child. The child allows itself to
312 be traced by its parent. */
315 linux_child_function (void *child_stack
)
317 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
318 kill (getpid (), SIGSTOP
);
320 /* Fork a grandchild. */
321 linux_fork_to_function ((gdb_byte
*) child_stack
, linux_grandchild_function
);
323 /* This code is only reacheable by the child (grandchild's parent)
328 static void linux_test_for_tracesysgood (int child_pid
);
329 static void linux_test_for_tracefork (int child_pid
);
330 static void linux_test_for_exitkill (int child_pid
);
332 /* Determine ptrace features available on this target. */
335 linux_check_ptrace_features (void)
337 int child_pid
, ret
, status
;
339 /* Initialize the options. */
340 supported_ptrace_options
= 0;
342 /* Fork a child so we can do some testing. The child will call
343 linux_child_function and will get traced. The child will
344 eventually fork a grandchild so we can test fork event
346 child_pid
= linux_fork_to_function (NULL
, linux_child_function
);
348 ret
= my_waitpid (child_pid
, &status
, 0);
350 perror_with_name (("waitpid"));
351 else if (ret
!= child_pid
)
352 error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
354 if (! WIFSTOPPED (status
))
355 error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
358 linux_test_for_tracesysgood (child_pid
);
360 linux_test_for_tracefork (child_pid
);
362 linux_test_for_exitkill (child_pid
);
364 /* Clean things up and kill any pending children. */
367 ret
= ptrace (PTRACE_KILL
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
368 (PTRACE_TYPE_ARG4
) 0);
370 warning (_("linux_check_ptrace_features: failed to kill child"));
371 my_waitpid (child_pid
, &status
, 0);
373 while (WIFSTOPPED (status
));
376 /* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
380 linux_test_for_tracesysgood (int child_pid
)
384 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
385 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACESYSGOOD
);
388 supported_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
391 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork
395 linux_test_for_tracefork (int child_pid
)
400 /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
401 know for sure that it is not supported. */
402 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
403 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACEFORK
);
408 /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
409 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
410 (PTRACE_TYPE_ARG4
) (PTRACE_O_TRACEFORK
411 | PTRACE_O_TRACEVFORKDONE
));
413 supported_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
415 /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
416 don't know for sure that the feature is available; old
417 versions of PTRACE_SETOPTIONS ignored unknown options.
418 Therefore, we attach to the child process, use PTRACE_SETOPTIONS
419 to enable fork tracing, and let it fork. If the process exits,
420 we assume that we can't use PTRACE_O_TRACEFORK; if we get the
421 fork notification, and we can extract the new child's PID, then
422 we assume that we can.
424 We do not explicitly check for vfork tracing here. It is
425 assumed that vfork tracing is available whenever fork tracing
427 ret
= ptrace (PTRACE_CONT
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
428 (PTRACE_TYPE_ARG4
) 0);
430 warning (_("linux_test_for_tracefork: failed to resume child"));
432 ret
= my_waitpid (child_pid
, &status
, 0);
434 /* Check if we received a fork event notification. */
435 if (ret
== child_pid
&& WIFSTOPPED (status
)
436 && linux_ptrace_get_extended_event (status
) == PTRACE_EVENT_FORK
)
438 /* We did receive a fork event notification. Make sure its PID
441 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
442 (PTRACE_TYPE_ARG4
) &second_pid
);
443 if (ret
== 0 && second_pid
!= 0)
447 /* We got the PID from the grandchild, which means fork
448 tracing is supported. */
449 supported_ptrace_options
|= PTRACE_O_TRACECLONE
;
450 supported_ptrace_options
|= (PTRACE_O_TRACEFORK
451 | PTRACE_O_TRACEVFORK
452 | PTRACE_O_TRACEEXEC
);
454 /* Do some cleanup and kill the grandchild. */
455 my_waitpid (second_pid
, &second_status
, 0);
456 ret
= ptrace (PTRACE_KILL
, second_pid
, (PTRACE_TYPE_ARG3
) 0,
457 (PTRACE_TYPE_ARG4
) 0);
459 warning (_("linux_test_for_tracefork: "
460 "failed to kill second child"));
461 my_waitpid (second_pid
, &status
, 0);
465 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
466 "(%d, status 0x%x)"), ret
, status
);
469 /* Determine if PTRACE_O_EXITKILL can be used. */
472 linux_test_for_exitkill (int child_pid
)
476 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
477 (PTRACE_TYPE_ARG4
) PTRACE_O_EXITKILL
);
480 supported_ptrace_options
|= PTRACE_O_EXITKILL
;
483 /* Enable reporting of all currently supported ptrace events.
484 OPTIONS is a bit mask of extended features we want enabled,
485 if supported by the kernel. PTRACE_O_TRACECLONE is always
486 enabled, if supported. */
489 linux_enable_event_reporting (pid_t pid
, int options
)
491 /* Check if we have initialized the ptrace features for this
492 target. If not, do it now. */
493 if (supported_ptrace_options
== -1)
494 linux_check_ptrace_features ();
496 /* We always want clone events. */
497 options
|= PTRACE_O_TRACECLONE
;
499 /* Filter out unsupported options. */
500 options
&= supported_ptrace_options
;
502 /* Set the options. */
503 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0,
504 (PTRACE_TYPE_ARG4
) (uintptr_t) options
);
507 /* Disable reporting of all currently supported ptrace events. */
510 linux_disable_event_reporting (pid_t pid
)
512 /* Set the options. */
513 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
516 /* Returns non-zero if PTRACE_OPTIONS is contained within
517 SUPPORTED_PTRACE_OPTIONS, therefore supported. Returns 0
521 ptrace_supports_feature (int ptrace_options
)
523 if (supported_ptrace_options
== -1)
524 linux_check_ptrace_features ();
526 return ((supported_ptrace_options
& ptrace_options
) == ptrace_options
);
529 /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
530 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
531 PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
532 since they were all added to the kernel at the same time. */
535 linux_supports_tracefork (void)
537 return ptrace_supports_feature (PTRACE_O_TRACEFORK
);
540 /* Returns non-zero if PTRACE_EVENT_EXEC is supported by ptrace,
541 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
542 PTRACE_EVENT_CLONE, PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK,
543 since they were all added to the kernel at the same time. */
546 linux_supports_traceexec (void)
548 return ptrace_supports_feature (PTRACE_O_TRACEEXEC
);
551 /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
552 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
553 PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
554 since they were all added to the kernel at the same time. */
557 linux_supports_traceclone (void)
559 return ptrace_supports_feature (PTRACE_O_TRACECLONE
);
562 /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
563 ptrace, 0 otherwise. */
566 linux_supports_tracevforkdone (void)
568 return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE
);
571 /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
575 linux_supports_tracesysgood (void)
577 return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD
);
580 /* Display possible problems on this system. Display them only once per GDB
584 linux_ptrace_init_warnings (void)
586 static int warned
= 0;
592 linux_ptrace_test_ret_to_nx ();
595 /* Extract extended ptrace event from wait status. */
598 linux_ptrace_get_extended_event (int wstat
)
600 return (wstat
>> 16);
603 /* Determine whether wait status denotes an extended event. */
606 linux_is_extended_waitstatus (int wstat
)
608 return (linux_ptrace_get_extended_event (wstat
) != 0);
611 /* Return true if the event in LP may be caused by breakpoint. */
614 linux_wstatus_maybe_breakpoint (int wstat
)
616 return (WIFSTOPPED (wstat
)
617 && (WSTOPSIG (wstat
) == SIGTRAP
618 /* SIGILL and SIGSEGV are also treated as traps in case a
619 breakpoint is inserted at the current PC. */
620 || WSTOPSIG (wstat
) == SIGILL
621 || WSTOPSIG (wstat
) == SIGSEGV
));