1 /* Linux-specific ptrace manipulation routines.
2 Copyright (C) 2012-2013 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/>. */
23 #include "gdb_string.h"
26 #include "linux-ptrace.h"
27 #include "linux-procfs.h"
28 #include "nat/linux-waitpid.h"
30 #include "gdb_assert.h"
35 /* Stores the currently supported ptrace options. A value of
36 -1 means we did not check for features yet. A value of 0 means
37 there are no supported features. */
38 static int current_ptrace_options = -1;
40 /* Find all possible reasons we could fail to attach PID and append these
41 newline terminated reason strings to initialized BUFFER. '\0' termination
42 of BUFFER must be done by the caller. */
45 linux_ptrace_attach_warnings (pid_t pid, struct buffer *buffer)
49 tracerpid = linux_proc_get_tracerpid (pid);
51 buffer_xml_printf (buffer, _("warning: process %d is already traced "
53 (int) pid, (int) tracerpid);
55 if (linux_proc_pid_is_zombie (pid))
56 buffer_xml_printf (buffer, _("warning: process %d is a zombie "
57 "- the process has already terminated\n"),
61 #if defined __i386__ || defined __x86_64__
63 /* Address of the 'ret' instruction in asm code block below. */
64 extern void (linux_ptrace_test_ret_to_nx_instr) (void);
70 #endif /* defined __i386__ || defined __x86_64__ */
72 /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
73 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
75 Test also x86_64 arch for PaX support. */
78 linux_ptrace_test_ret_to_nx (void)
80 #if defined __i386__ || defined __x86_64__
82 gdb_byte *return_address, *pc;
84 int status, kill_status;
86 return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE,
87 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
88 if (return_address == MAP_FAILED)
90 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
95 /* Put there 'int3'. */
96 *return_address = 0xcc;
102 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
107 l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL,
108 (PTRACE_TYPE_ARG4) NULL);
110 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
115 asm volatile ("pushl %0;"
116 ".globl linux_ptrace_test_ret_to_nx_instr;"
117 "linux_ptrace_test_ret_to_nx_instr:"
119 : : "r" (return_address) : "%esp", "memory");
120 #elif defined __x86_64__
121 asm volatile ("pushq %0;"
122 ".globl linux_ptrace_test_ret_to_nx_instr;"
123 "linux_ptrace_test_ret_to_nx_instr:"
125 : : "r" ((uint64_t) (uintptr_t) return_address)
128 # error "!__i386__ && !__x86_64__"
130 gdb_assert_not_reached ("asm block did not terminate");
137 got_pid = waitpid (child, &status, 0);
138 if (got_pid != child)
140 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
141 (long) got_pid, strerror (errno));
145 if (WIFSIGNALED (status))
147 if (WTERMSIG (status) != SIGKILL)
148 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
149 (int) WTERMSIG (status));
151 warning (_("Cannot call inferior functions, Linux kernel PaX "
152 "protection forbids return to non-executable pages!"));
156 if (!WIFSTOPPED (status))
158 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
163 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
164 if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
166 warning (_("linux_ptrace_test_ret_to_nx: "
167 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
168 (int) WSTOPSIG (status));
174 l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (EIP * 4),
175 (PTRACE_TYPE_ARG4) NULL);
176 #elif defined __x86_64__
177 l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (RIP * 8),
178 (PTRACE_TYPE_ARG4) NULL);
180 # error "!__i386__ && !__x86_64__"
184 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
188 pc = (void *) (uintptr_t) l;
190 kill (child, SIGKILL);
191 ptrace (PTRACE_KILL, child, (PTRACE_TYPE_ARG3) NULL,
192 (PTRACE_TYPE_ARG4) NULL);
195 got_pid = waitpid (child, &kill_status, 0);
196 if (got_pid != child)
198 warning (_("linux_ptrace_test_ret_to_nx: "
199 "PTRACE_KILL waitpid returned %ld: %s"),
200 (long) got_pid, strerror (errno));
203 if (!WIFSIGNALED (kill_status))
205 warning (_("linux_ptrace_test_ret_to_nx: "
206 "PTRACE_KILL status %d is not WIFSIGNALED!"),
211 /* + 1 is there as x86* stops after the 'int3' instruction. */
212 if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1)
218 /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
219 if (WSTOPSIG (status) == SIGSEGV && pc == return_address)
225 if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr)
226 warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
227 "address %p nor is the return instruction %p!"),
228 pc, return_address, &linux_ptrace_test_ret_to_nx_instr);
230 warning (_("Cannot call inferior functions on this system - "
231 "Linux kernel with broken i386 NX (non-executable pages) "
232 "support detected!"));
233 #endif /* defined __i386__ || defined __x86_64__ */
236 /* Helper function to fork a process and make the child process call
237 the function FUNCTION, passing CHILD_STACK as parameter.
239 For MMU-less targets, clone is used instead of fork, and
240 CHILD_STACK is used as stack space for the cloned child. If NULL,
241 stack space is allocated via malloc (and subsequently passed to
242 FUNCTION). For MMU targets, CHILD_STACK is ignored. */
245 linux_fork_to_function (gdb_byte *child_stack, void (*function) (gdb_byte *))
249 /* Sanity check the function pointer. */
250 gdb_assert (function != NULL);
252 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
253 #define STACK_SIZE 4096
255 if (child_stack == NULL)
256 child_stack = xmalloc (STACK_SIZE * 4);
258 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
260 child_pid = __clone2 (function, child_stack, STACK_SIZE,
261 CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
262 #else /* !__ia64__ */
263 child_pid = clone (function, child_stack + STACK_SIZE,
264 CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
265 #endif /* !__ia64__ */
266 #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
271 #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
274 perror_with_name (("fork"));
279 /* A helper function for linux_check_ptrace_features, called after
280 the child forks a grandchild. */
283 linux_grandchild_function (gdb_byte *child_stack)
285 /* Free any allocated stack. */
288 /* This code is only reacheable by the grandchild (child's child)
293 /* A helper function for linux_check_ptrace_features, called after
294 the parent process forks a child. The child allows itself to
295 be traced by its parent. */
298 linux_child_function (gdb_byte *child_stack)
300 ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
301 kill (getpid (), SIGSTOP);
303 /* Fork a grandchild. */
304 linux_fork_to_function (child_stack, linux_grandchild_function);
306 /* This code is only reacheable by the child (grandchild's parent)
311 /* Determine ptrace features available on this target. */
314 linux_check_ptrace_features (void)
316 int child_pid, ret, status;
319 /* Initialize the options. */
320 current_ptrace_options = 0;
322 /* Fork a child so we can do some testing. The child will call
323 linux_child_function and will get traced. The child will
324 eventually fork a grandchild so we can test fork event
326 child_pid = linux_fork_to_function (NULL, linux_child_function);
328 ret = my_waitpid (child_pid, &status, 0);
330 perror_with_name (("waitpid"));
331 else if (ret != child_pid)
332 error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
334 if (! WIFSTOPPED (status))
335 error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
338 /* First, set the PTRACE_O_TRACEFORK option. If this fails, we
339 know for sure that it is not supported. */
340 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
341 (PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK);
345 ret = ptrace (PTRACE_KILL, child_pid, (PTRACE_TYPE_ARG3) 0,
346 (PTRACE_TYPE_ARG4) 0);
349 warning (_("linux_check_ptrace_features: failed to kill child"));
353 ret = my_waitpid (child_pid, &status, 0);
354 if (ret != child_pid)
355 warning (_("linux_check_ptrace_features: failed "
356 "to wait for killed child"));
357 else if (!WIFSIGNALED (status))
358 warning (_("linux_check_ptrace_features: unexpected "
359 "wait status 0x%x from killed child"), status);
365 /* gdbserver does not support PTRACE_O_TRACESYSGOOD or
366 PTRACE_O_TRACEVFORKDONE yet. */
368 /* Check if the target supports PTRACE_O_TRACESYSGOOD. */
369 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
370 (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD);
372 current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
374 /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
375 ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
376 (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK
377 | PTRACE_O_TRACEVFORKDONE));
379 current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
382 /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
383 don't know for sure that the feature is available; old
384 versions of PTRACE_SETOPTIONS ignored unknown options.
385 Therefore, we attach to the child process, use PTRACE_SETOPTIONS
386 to enable fork tracing, and let it fork. If the process exits,
387 we assume that we can't use PTRACE_O_TRACEFORK; if we get the
388 fork notification, and we can extract the new child's PID, then
389 we assume that we can.
391 We do not explicitly check for vfork tracing here. It is
392 assumed that vfork tracing is available whenever fork tracing
394 ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0,
395 (PTRACE_TYPE_ARG4) 0);
397 warning (_("linux_check_ptrace_features: failed to resume child"));
399 ret = my_waitpid (child_pid, &status, 0);
401 /* Check if we received a fork event notification. */
402 if (ret == child_pid && WIFSTOPPED (status)
403 && status >> 16 == PTRACE_EVENT_FORK)
405 /* We did receive a fork event notification. Make sure its PID
408 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0,
409 (PTRACE_TYPE_ARG4) &second_pid);
410 if (ret == 0 && second_pid != 0)
414 /* We got the PID from the grandchild, which means fork
415 tracing is supported. */
417 /* Do not enable all the options for now since gdbserver does not
418 properly support them. This restriction will be lifted when
419 gdbserver is augmented to support them. */
420 current_ptrace_options |= PTRACE_O_TRACECLONE;
422 current_ptrace_options |= PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
423 | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC;
425 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to
426 support read-only process state. */
429 /* Do some cleanup and kill the grandchild. */
430 my_waitpid (second_pid, &second_status, 0);
431 ret = ptrace (PTRACE_KILL, second_pid, (PTRACE_TYPE_ARG3) 0,
432 (PTRACE_TYPE_ARG4) 0);
434 warning (_("linux_check_ptrace_features: "
435 "failed to kill second child"));
436 my_waitpid (second_pid, &status, 0);
440 warning (_("linux_check_ptrace_features: unexpected result from waitpid "
441 "(%d, status 0x%x)"), ret, status);
443 /* Clean things up and kill any pending children. */
446 ret = ptrace (PTRACE_KILL, child_pid, (PTRACE_TYPE_ARG3) 0,
447 (PTRACE_TYPE_ARG4) 0);
449 warning (_("linux_check_ptrace_features: failed to kill child"));
450 my_waitpid (child_pid, &status, 0);
452 while (WIFSTOPPED (status));
455 /* Enable reporting of all currently supported ptrace events. */
458 linux_enable_event_reporting (pid_t pid)
460 /* Check if we have initialized the ptrace features for this
461 target. If not, do it now. */
462 if (current_ptrace_options == -1)
463 linux_check_ptrace_features ();
465 /* Set the options. */
466 ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0,
467 (PTRACE_TYPE_ARG4) (uintptr_t) current_ptrace_options);
470 /* Returns non-zero if PTRACE_OPTIONS is contained within
471 CURRENT_PTRACE_OPTIONS, therefore supported. Returns 0
475 ptrace_supports_feature (int ptrace_options)
477 gdb_assert (current_ptrace_options >= 0);
479 return ((current_ptrace_options & ptrace_options) == ptrace_options);
482 /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
483 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
484 PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
485 since they were all added to the kernel at the same time. */
488 linux_supports_tracefork (void)
490 return ptrace_supports_feature (PTRACE_O_TRACEFORK);
493 /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
494 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
495 PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
496 since they were all added to the kernel at the same time. */
499 linux_supports_traceclone (void)
501 return ptrace_supports_feature (PTRACE_O_TRACECLONE);
504 /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
505 ptrace, 0 otherwise. */
508 linux_supports_tracevforkdone (void)
510 return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE);
513 /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
517 linux_supports_tracesysgood (void)
519 return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD);
522 /* Display possible problems on this system. Display them only once per GDB
526 linux_ptrace_init_warnings (void)
528 static int warned = 0;
534 linux_ptrace_test_ret_to_nx ();