1 /* Linux-specific ptrace manipulation routines.
2 Copyright (C) 2012-2014 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/>. */
26 #include "linux-ptrace.h"
27 #include "linux-procfs.h"
28 #include "linux-waitpid.h"
34 /* Stores the currently supported ptrace options. A value of
35 -1 means we did not check for features yet. A value of 0 means
36 there are no supported features. */
37 static int current_ptrace_options
= -1;
39 /* Additional flags to test. */
41 static int additional_flags
;
43 /* Find all possible reasons we could fail to attach PID and append
44 these as strings to the already initialized BUFFER. '\0'
45 termination of BUFFER must be done by the caller. */
48 linux_ptrace_attach_fail_reason (pid_t pid
, struct buffer
*buffer
)
52 tracerpid
= linux_proc_get_tracerpid (pid
);
54 buffer_xml_printf (buffer
, _("process %d is already traced "
56 (int) pid
, (int) tracerpid
);
58 if (linux_proc_pid_is_zombie (pid
))
59 buffer_xml_printf (buffer
, _("process %d is a zombie "
60 "- the process has already terminated"),
64 #if defined __i386__ || defined __x86_64__
66 /* Address of the 'ret' instruction in asm code block below. */
67 extern void (linux_ptrace_test_ret_to_nx_instr
) (void);
73 #endif /* defined __i386__ || defined __x86_64__ */
75 /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
76 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
78 Test also x86_64 arch for PaX support. */
81 linux_ptrace_test_ret_to_nx (void)
83 #if defined __i386__ || defined __x86_64__
85 gdb_byte
*return_address
, *pc
;
87 int status
, kill_status
;
89 return_address
= mmap (NULL
, 2, PROT_READ
| PROT_WRITE
,
90 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
91 if (return_address
== MAP_FAILED
)
93 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
98 /* Put there 'int3'. */
99 *return_address
= 0xcc;
105 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
110 l
= ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) NULL
,
111 (PTRACE_TYPE_ARG4
) NULL
);
113 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
118 asm volatile ("pushl %0;"
119 ".globl linux_ptrace_test_ret_to_nx_instr;"
120 "linux_ptrace_test_ret_to_nx_instr:"
122 : : "r" (return_address
) : "%esp", "memory");
123 #elif defined __x86_64__
124 asm volatile ("pushq %0;"
125 ".globl linux_ptrace_test_ret_to_nx_instr;"
126 "linux_ptrace_test_ret_to_nx_instr:"
128 : : "r" ((uint64_t) (uintptr_t) return_address
)
131 # error "!__i386__ && !__x86_64__"
133 gdb_assert_not_reached ("asm block did not terminate");
140 got_pid
= waitpid (child
, &status
, 0);
141 if (got_pid
!= child
)
143 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
144 (long) got_pid
, strerror (errno
));
148 if (WIFSIGNALED (status
))
150 if (WTERMSIG (status
) != SIGKILL
)
151 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
152 (int) WTERMSIG (status
));
154 warning (_("Cannot call inferior functions, Linux kernel PaX "
155 "protection forbids return to non-executable pages!"));
159 if (!WIFSTOPPED (status
))
161 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
166 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
167 if (WSTOPSIG (status
) != SIGTRAP
&& WSTOPSIG (status
) != SIGSEGV
)
169 warning (_("linux_ptrace_test_ret_to_nx: "
170 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
171 (int) WSTOPSIG (status
));
177 l
= ptrace (PTRACE_PEEKUSER
, child
, (PTRACE_TYPE_ARG3
) (uintptr_t) (EIP
* 4),
178 (PTRACE_TYPE_ARG4
) NULL
);
179 #elif defined __x86_64__
180 l
= ptrace (PTRACE_PEEKUSER
, child
, (PTRACE_TYPE_ARG3
) (uintptr_t) (RIP
* 8),
181 (PTRACE_TYPE_ARG4
) NULL
);
183 # error "!__i386__ && !__x86_64__"
187 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
191 pc
= (void *) (uintptr_t) l
;
193 kill (child
, SIGKILL
);
194 ptrace (PTRACE_KILL
, child
, (PTRACE_TYPE_ARG3
) NULL
,
195 (PTRACE_TYPE_ARG4
) NULL
);
198 got_pid
= waitpid (child
, &kill_status
, 0);
199 if (got_pid
!= child
)
201 warning (_("linux_ptrace_test_ret_to_nx: "
202 "PTRACE_KILL waitpid returned %ld: %s"),
203 (long) got_pid
, strerror (errno
));
206 if (!WIFSIGNALED (kill_status
))
208 warning (_("linux_ptrace_test_ret_to_nx: "
209 "PTRACE_KILL status %d is not WIFSIGNALED!"),
214 /* + 1 is there as x86* stops after the 'int3' instruction. */
215 if (WSTOPSIG (status
) == SIGTRAP
&& pc
== return_address
+ 1)
221 /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
222 if (WSTOPSIG (status
) == SIGSEGV
&& pc
== return_address
)
228 if ((void (*) (void)) pc
!= &linux_ptrace_test_ret_to_nx_instr
)
229 warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
230 "address %p nor is the return instruction %p!"),
231 pc
, return_address
, &linux_ptrace_test_ret_to_nx_instr
);
233 warning (_("Cannot call inferior functions on this system - "
234 "Linux kernel with broken i386 NX (non-executable pages) "
235 "support detected!"));
236 #endif /* defined __i386__ || defined __x86_64__ */
239 /* Helper function to fork a process and make the child process call
240 the function FUNCTION, passing CHILD_STACK as parameter.
242 For MMU-less targets, clone is used instead of fork, and
243 CHILD_STACK is used as stack space for the cloned child. If NULL,
244 stack space is allocated via malloc (and subsequently passed to
245 FUNCTION). For MMU targets, CHILD_STACK is ignored. */
248 linux_fork_to_function (gdb_byte
*child_stack
, void (*function
) (gdb_byte
*))
252 /* Sanity check the function pointer. */
253 gdb_assert (function
!= NULL
);
255 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
256 #define STACK_SIZE 4096
258 if (child_stack
== NULL
)
259 child_stack
= xmalloc (STACK_SIZE
* 4);
261 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
263 child_pid
= __clone2 (function
, child_stack
, STACK_SIZE
,
264 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
265 #else /* !__ia64__ */
266 child_pid
= clone (function
, child_stack
+ STACK_SIZE
,
267 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
268 #endif /* !__ia64__ */
269 #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
274 #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
277 perror_with_name (("fork"));
282 /* A helper function for linux_check_ptrace_features, called after
283 the child forks a grandchild. */
286 linux_grandchild_function (gdb_byte
*child_stack
)
288 /* Free any allocated stack. */
291 /* This code is only reacheable by the grandchild (child's child)
296 /* A helper function for linux_check_ptrace_features, called after
297 the parent process forks a child. The child allows itself to
298 be traced by its parent. */
301 linux_child_function (gdb_byte
*child_stack
)
303 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
304 kill (getpid (), SIGSTOP
);
306 /* Fork a grandchild. */
307 linux_fork_to_function (child_stack
, linux_grandchild_function
);
309 /* This code is only reacheable by the child (grandchild's parent)
314 static void linux_test_for_tracesysgood (int child_pid
);
315 static void linux_test_for_tracefork (int child_pid
);
317 /* Determine ptrace features available on this target. */
320 linux_check_ptrace_features (void)
322 int child_pid
, ret
, status
;
324 /* Initialize the options. */
325 current_ptrace_options
= 0;
327 /* Fork a child so we can do some testing. The child will call
328 linux_child_function and will get traced. The child will
329 eventually fork a grandchild so we can test fork event
331 child_pid
= linux_fork_to_function (NULL
, linux_child_function
);
333 ret
= my_waitpid (child_pid
, &status
, 0);
335 perror_with_name (("waitpid"));
336 else if (ret
!= child_pid
)
337 error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
339 if (! WIFSTOPPED (status
))
340 error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
343 linux_test_for_tracesysgood (child_pid
);
345 linux_test_for_tracefork (child_pid
);
347 /* Clean things up and kill any pending children. */
350 ret
= ptrace (PTRACE_KILL
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
351 (PTRACE_TYPE_ARG4
) 0);
353 warning (_("linux_check_ptrace_features: failed to kill child"));
354 my_waitpid (child_pid
, &status
, 0);
356 while (WIFSTOPPED (status
));
359 /* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
363 linux_test_for_tracesysgood (int child_pid
)
367 if ((additional_flags
& PTRACE_O_TRACESYSGOOD
) == 0)
370 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
371 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACESYSGOOD
);
374 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
377 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork
381 linux_test_for_tracefork (int child_pid
)
386 /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
387 know for sure that it is not supported. */
388 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
389 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACEFORK
);
394 if ((additional_flags
& PTRACE_O_TRACEVFORKDONE
) != 0)
396 /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
397 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
398 (PTRACE_TYPE_ARG4
) (PTRACE_O_TRACEFORK
399 | PTRACE_O_TRACEVFORKDONE
));
401 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
404 /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
405 don't know for sure that the feature is available; old
406 versions of PTRACE_SETOPTIONS ignored unknown options.
407 Therefore, we attach to the child process, use PTRACE_SETOPTIONS
408 to enable fork tracing, and let it fork. If the process exits,
409 we assume that we can't use PTRACE_O_TRACEFORK; if we get the
410 fork notification, and we can extract the new child's PID, then
411 we assume that we can.
413 We do not explicitly check for vfork tracing here. It is
414 assumed that vfork tracing is available whenever fork tracing
416 ret
= ptrace (PTRACE_CONT
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
417 (PTRACE_TYPE_ARG4
) 0);
419 warning (_("linux_test_for_tracefork: failed to resume child"));
421 ret
= my_waitpid (child_pid
, &status
, 0);
423 /* Check if we received a fork event notification. */
424 if (ret
== child_pid
&& WIFSTOPPED (status
)
425 && status
>> 16 == PTRACE_EVENT_FORK
)
427 /* We did receive a fork event notification. Make sure its PID
430 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
431 (PTRACE_TYPE_ARG4
) &second_pid
);
432 if (ret
== 0 && second_pid
!= 0)
436 /* We got the PID from the grandchild, which means fork
437 tracing is supported. */
438 current_ptrace_options
|= PTRACE_O_TRACECLONE
;
439 current_ptrace_options
|= (additional_flags
& (PTRACE_O_TRACEFORK
440 | PTRACE_O_TRACEVFORK
441 | PTRACE_O_TRACEEXEC
));
443 /* Do some cleanup and kill the grandchild. */
444 my_waitpid (second_pid
, &second_status
, 0);
445 ret
= ptrace (PTRACE_KILL
, second_pid
, (PTRACE_TYPE_ARG3
) 0,
446 (PTRACE_TYPE_ARG4
) 0);
448 warning (_("linux_test_for_tracefork: "
449 "failed to kill second child"));
450 my_waitpid (second_pid
, &status
, 0);
454 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
455 "(%d, status 0x%x)"), ret
, status
);
458 /* Enable reporting of all currently supported ptrace events. */
461 linux_enable_event_reporting (pid_t pid
)
463 /* Check if we have initialized the ptrace features for this
464 target. If not, do it now. */
465 if (current_ptrace_options
== -1)
466 linux_check_ptrace_features ();
468 /* Set the options. */
469 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0,
470 (PTRACE_TYPE_ARG4
) (uintptr_t) current_ptrace_options
);
473 /* Disable reporting of all currently supported ptrace events. */
476 linux_disable_event_reporting (pid_t pid
)
478 /* Set the options. */
479 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
482 /* Returns non-zero if PTRACE_OPTIONS is contained within
483 CURRENT_PTRACE_OPTIONS, therefore supported. Returns 0
487 ptrace_supports_feature (int ptrace_options
)
489 gdb_assert (current_ptrace_options
>= 0);
491 return ((current_ptrace_options
& ptrace_options
) == ptrace_options
);
494 /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
495 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
496 PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
497 since they were all added to the kernel at the same time. */
500 linux_supports_tracefork (void)
502 return ptrace_supports_feature (PTRACE_O_TRACEFORK
);
505 /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
506 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
507 PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
508 since they were all added to the kernel at the same time. */
511 linux_supports_traceclone (void)
513 return ptrace_supports_feature (PTRACE_O_TRACECLONE
);
516 /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
517 ptrace, 0 otherwise. */
520 linux_supports_tracevforkdone (void)
522 return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE
);
525 /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
529 linux_supports_tracesysgood (void)
531 return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD
);
534 /* Display possible problems on this system. Display them only once per GDB
538 linux_ptrace_init_warnings (void)
540 static int warned
= 0;
546 linux_ptrace_test_ret_to_nx ();
549 /* Set additional ptrace flags to use. Some such flags may be checked
550 by the implementation above. This function must be called before
551 any other function in this file; otherwise the flags may not take
552 effect appropriately. */
555 linux_ptrace_set_additional_flags (int flags
)
557 additional_flags
= flags
;