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/>. */
19 #include "common-defs.h"
20 #include "linux-ptrace.h"
21 #include "linux-procfs.h"
22 #include "linux-waitpid.h"
28 /* Stores the currently supported ptrace options. A value of
29 -1 means we did not check for features yet. A value of 0 means
30 there are no supported features. */
31 static int current_ptrace_options
= -1;
33 /* Additional flags to test. */
35 static int additional_flags
;
37 /* Find all possible reasons we could fail to attach PID and append
38 these as strings to the already initialized BUFFER. '\0'
39 termination of BUFFER must be done by the caller. */
42 linux_ptrace_attach_fail_reason (pid_t pid
, struct buffer
*buffer
)
46 tracerpid
= linux_proc_get_tracerpid (pid
);
48 buffer_xml_printf (buffer
, _("process %d is already traced "
50 (int) pid
, (int) tracerpid
);
52 if (linux_proc_pid_is_zombie (pid
))
53 buffer_xml_printf (buffer
, _("process %d is a zombie "
54 "- the process has already terminated"),
58 #if defined __i386__ || defined __x86_64__
60 /* Address of the 'ret' instruction in asm code block below. */
61 extern void (linux_ptrace_test_ret_to_nx_instr
) (void);
67 #endif /* defined __i386__ || defined __x86_64__ */
69 /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
70 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
72 Test also x86_64 arch for PaX support. */
75 linux_ptrace_test_ret_to_nx (void)
77 #if defined __i386__ || defined __x86_64__
79 gdb_byte
*return_address
, *pc
;
81 int status
, kill_status
;
83 return_address
= mmap (NULL
, 2, PROT_READ
| PROT_WRITE
,
84 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
85 if (return_address
== MAP_FAILED
)
87 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
92 /* Put there 'int3'. */
93 *return_address
= 0xcc;
99 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
104 l
= ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) NULL
,
105 (PTRACE_TYPE_ARG4
) NULL
);
107 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
112 asm volatile ("pushl %0;"
113 ".globl linux_ptrace_test_ret_to_nx_instr;"
114 "linux_ptrace_test_ret_to_nx_instr:"
116 : : "r" (return_address
) : "%esp", "memory");
117 #elif defined __x86_64__
118 asm volatile ("pushq %0;"
119 ".globl linux_ptrace_test_ret_to_nx_instr;"
120 "linux_ptrace_test_ret_to_nx_instr:"
122 : : "r" ((uint64_t) (uintptr_t) return_address
)
125 # error "!__i386__ && !__x86_64__"
127 gdb_assert_not_reached ("asm block did not terminate");
134 got_pid
= waitpid (child
, &status
, 0);
135 if (got_pid
!= child
)
137 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
138 (long) got_pid
, strerror (errno
));
142 if (WIFSIGNALED (status
))
144 if (WTERMSIG (status
) != SIGKILL
)
145 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
146 (int) WTERMSIG (status
));
148 warning (_("Cannot call inferior functions, Linux kernel PaX "
149 "protection forbids return to non-executable pages!"));
153 if (!WIFSTOPPED (status
))
155 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
160 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
161 if (WSTOPSIG (status
) != SIGTRAP
&& WSTOPSIG (status
) != SIGSEGV
)
163 warning (_("linux_ptrace_test_ret_to_nx: "
164 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
165 (int) WSTOPSIG (status
));
171 l
= ptrace (PTRACE_PEEKUSER
, child
, (PTRACE_TYPE_ARG3
) (uintptr_t) (EIP
* 4),
172 (PTRACE_TYPE_ARG4
) NULL
);
173 #elif defined __x86_64__
174 l
= ptrace (PTRACE_PEEKUSER
, child
, (PTRACE_TYPE_ARG3
) (uintptr_t) (RIP
* 8),
175 (PTRACE_TYPE_ARG4
) NULL
);
177 # error "!__i386__ && !__x86_64__"
181 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
185 pc
= (void *) (uintptr_t) l
;
187 kill (child
, SIGKILL
);
188 ptrace (PTRACE_KILL
, child
, (PTRACE_TYPE_ARG3
) NULL
,
189 (PTRACE_TYPE_ARG4
) NULL
);
192 got_pid
= waitpid (child
, &kill_status
, 0);
193 if (got_pid
!= child
)
195 warning (_("linux_ptrace_test_ret_to_nx: "
196 "PTRACE_KILL waitpid returned %ld: %s"),
197 (long) got_pid
, strerror (errno
));
200 if (!WIFSIGNALED (kill_status
))
202 warning (_("linux_ptrace_test_ret_to_nx: "
203 "PTRACE_KILL status %d is not WIFSIGNALED!"),
208 /* + 1 is there as x86* stops after the 'int3' instruction. */
209 if (WSTOPSIG (status
) == SIGTRAP
&& pc
== return_address
+ 1)
215 /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
216 if (WSTOPSIG (status
) == SIGSEGV
&& pc
== return_address
)
222 if ((void (*) (void)) pc
!= &linux_ptrace_test_ret_to_nx_instr
)
223 warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
224 "address %p nor is the return instruction %p!"),
225 pc
, return_address
, &linux_ptrace_test_ret_to_nx_instr
);
227 warning (_("Cannot call inferior functions on this system - "
228 "Linux kernel with broken i386 NX (non-executable pages) "
229 "support detected!"));
230 #endif /* defined __i386__ || defined __x86_64__ */
233 /* Helper function to fork a process and make the child process call
234 the function FUNCTION, passing CHILD_STACK as parameter.
236 For MMU-less targets, clone is used instead of fork, and
237 CHILD_STACK is used as stack space for the cloned child. If NULL,
238 stack space is allocated via malloc (and subsequently passed to
239 FUNCTION). For MMU targets, CHILD_STACK is ignored. */
242 linux_fork_to_function (gdb_byte
*child_stack
, void (*function
) (gdb_byte
*))
246 /* Sanity check the function pointer. */
247 gdb_assert (function
!= NULL
);
249 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
250 #define STACK_SIZE 4096
252 if (child_stack
== NULL
)
253 child_stack
= xmalloc (STACK_SIZE
* 4);
255 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
257 child_pid
= __clone2 (function
, child_stack
, STACK_SIZE
,
258 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
259 #else /* !__ia64__ */
260 child_pid
= clone (function
, child_stack
+ STACK_SIZE
,
261 CLONE_VM
| SIGCHLD
, child_stack
+ STACK_SIZE
* 2);
262 #endif /* !__ia64__ */
263 #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
268 #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
271 perror_with_name (("fork"));
276 /* A helper function for linux_check_ptrace_features, called after
277 the child forks a grandchild. */
280 linux_grandchild_function (gdb_byte
*child_stack
)
282 /* Free any allocated stack. */
285 /* This code is only reacheable by the grandchild (child's child)
290 /* A helper function for linux_check_ptrace_features, called after
291 the parent process forks a child. The child allows itself to
292 be traced by its parent. */
295 linux_child_function (gdb_byte
*child_stack
)
297 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
298 kill (getpid (), SIGSTOP
);
300 /* Fork a grandchild. */
301 linux_fork_to_function (child_stack
, linux_grandchild_function
);
303 /* This code is only reacheable by the child (grandchild's parent)
308 static void linux_test_for_tracesysgood (int child_pid
);
309 static void linux_test_for_tracefork (int child_pid
);
311 /* Determine ptrace features available on this target. */
314 linux_check_ptrace_features (void)
316 int child_pid
, ret
, status
;
318 /* Initialize the options. */
319 current_ptrace_options
= 0;
321 /* Fork a child so we can do some testing. The child will call
322 linux_child_function and will get traced. The child will
323 eventually fork a grandchild so we can test fork event
325 child_pid
= linux_fork_to_function (NULL
, linux_child_function
);
327 ret
= my_waitpid (child_pid
, &status
, 0);
329 perror_with_name (("waitpid"));
330 else if (ret
!= child_pid
)
331 error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
333 if (! WIFSTOPPED (status
))
334 error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
337 linux_test_for_tracesysgood (child_pid
);
339 linux_test_for_tracefork (child_pid
);
341 /* Clean things up and kill any pending children. */
344 ret
= ptrace (PTRACE_KILL
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
345 (PTRACE_TYPE_ARG4
) 0);
347 warning (_("linux_check_ptrace_features: failed to kill child"));
348 my_waitpid (child_pid
, &status
, 0);
350 while (WIFSTOPPED (status
));
353 /* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
357 linux_test_for_tracesysgood (int child_pid
)
361 if ((additional_flags
& PTRACE_O_TRACESYSGOOD
) == 0)
364 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
365 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACESYSGOOD
);
368 current_ptrace_options
|= PTRACE_O_TRACESYSGOOD
;
371 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork
375 linux_test_for_tracefork (int child_pid
)
380 /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
381 know for sure that it is not supported. */
382 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
383 (PTRACE_TYPE_ARG4
) PTRACE_O_TRACEFORK
);
388 if ((additional_flags
& PTRACE_O_TRACEVFORKDONE
) != 0)
390 /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
391 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
392 (PTRACE_TYPE_ARG4
) (PTRACE_O_TRACEFORK
393 | PTRACE_O_TRACEVFORKDONE
));
395 current_ptrace_options
|= PTRACE_O_TRACEVFORKDONE
;
398 /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
399 don't know for sure that the feature is available; old
400 versions of PTRACE_SETOPTIONS ignored unknown options.
401 Therefore, we attach to the child process, use PTRACE_SETOPTIONS
402 to enable fork tracing, and let it fork. If the process exits,
403 we assume that we can't use PTRACE_O_TRACEFORK; if we get the
404 fork notification, and we can extract the new child's PID, then
405 we assume that we can.
407 We do not explicitly check for vfork tracing here. It is
408 assumed that vfork tracing is available whenever fork tracing
410 ret
= ptrace (PTRACE_CONT
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
411 (PTRACE_TYPE_ARG4
) 0);
413 warning (_("linux_test_for_tracefork: failed to resume child"));
415 ret
= my_waitpid (child_pid
, &status
, 0);
417 /* Check if we received a fork event notification. */
418 if (ret
== child_pid
&& WIFSTOPPED (status
)
419 && status
>> 16 == PTRACE_EVENT_FORK
)
421 /* We did receive a fork event notification. Make sure its PID
424 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, (PTRACE_TYPE_ARG3
) 0,
425 (PTRACE_TYPE_ARG4
) &second_pid
);
426 if (ret
== 0 && second_pid
!= 0)
430 /* We got the PID from the grandchild, which means fork
431 tracing is supported. */
432 current_ptrace_options
|= PTRACE_O_TRACECLONE
;
433 current_ptrace_options
|= (additional_flags
& (PTRACE_O_TRACEFORK
434 | PTRACE_O_TRACEVFORK
435 | PTRACE_O_TRACEEXEC
));
437 /* Do some cleanup and kill the grandchild. */
438 my_waitpid (second_pid
, &second_status
, 0);
439 ret
= ptrace (PTRACE_KILL
, second_pid
, (PTRACE_TYPE_ARG3
) 0,
440 (PTRACE_TYPE_ARG4
) 0);
442 warning (_("linux_test_for_tracefork: "
443 "failed to kill second child"));
444 my_waitpid (second_pid
, &status
, 0);
448 warning (_("linux_test_for_tracefork: unexpected result from waitpid "
449 "(%d, status 0x%x)"), ret
, status
);
452 /* Enable reporting of all currently supported ptrace events. */
455 linux_enable_event_reporting (pid_t pid
)
457 /* Check if we have initialized the ptrace features for this
458 target. If not, do it now. */
459 if (current_ptrace_options
== -1)
460 linux_check_ptrace_features ();
462 /* Set the options. */
463 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0,
464 (PTRACE_TYPE_ARG4
) (uintptr_t) current_ptrace_options
);
467 /* Disable reporting of all currently supported ptrace events. */
470 linux_disable_event_reporting (pid_t pid
)
472 /* Set the options. */
473 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
476 /* Returns non-zero if PTRACE_OPTIONS is contained within
477 CURRENT_PTRACE_OPTIONS, therefore supported. Returns 0
481 ptrace_supports_feature (int ptrace_options
)
483 gdb_assert (current_ptrace_options
>= 0);
485 return ((current_ptrace_options
& ptrace_options
) == ptrace_options
);
488 /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
489 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
490 PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
491 since they were all added to the kernel at the same time. */
494 linux_supports_tracefork (void)
496 return ptrace_supports_feature (PTRACE_O_TRACEFORK
);
499 /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
500 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
501 PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
502 since they were all added to the kernel at the same time. */
505 linux_supports_traceclone (void)
507 return ptrace_supports_feature (PTRACE_O_TRACECLONE
);
510 /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
511 ptrace, 0 otherwise. */
514 linux_supports_tracevforkdone (void)
516 return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE
);
519 /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
523 linux_supports_tracesysgood (void)
525 return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD
);
528 /* Display possible problems on this system. Display them only once per GDB
532 linux_ptrace_init_warnings (void)
534 static int warned
= 0;
540 linux_ptrace_test_ret_to_nx ();
543 /* Set additional ptrace flags to use. Some such flags may be checked
544 by the implementation above. This function must be called before
545 any other function in this file; otherwise the flags may not take
546 effect appropriately. */
549 linux_ptrace_set_additional_flags (int flags
)
551 additional_flags
= flags
;