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"
29 #include "gdb_assert.h"
32 /* Find all possible reasons we could fail to attach PID and append these
33 newline terminated reason strings to initialized BUFFER. '\0' termination
34 of BUFFER must be done by the caller. */
37 linux_ptrace_attach_warnings (pid_t pid, struct buffer *buffer)
41 tracerpid = linux_proc_get_tracerpid (pid);
43 buffer_xml_printf (buffer, _("warning: process %d is already traced "
45 (int) pid, (int) tracerpid);
47 if (linux_proc_pid_is_zombie (pid))
48 buffer_xml_printf (buffer, _("warning: process %d is a zombie "
49 "- the process has already terminated\n"),
53 #if defined __i386__ || defined __x86_64__
55 /* Address of the 'ret' instruction in asm code block below. */
56 extern void (linux_ptrace_test_ret_to_nx_instr) (void);
63 #endif /* defined __i386__ || defined __x86_64__ */
65 /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
66 removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
68 Test also x86_64 arch for PaX support. */
71 linux_ptrace_test_ret_to_nx (void)
73 #if defined __i386__ || defined __x86_64__
75 gdb_byte *return_address, *pc;
79 return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE,
80 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
81 if (return_address == MAP_FAILED)
83 warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
88 /* Put there 'int3'. */
89 *return_address = 0xcc;
95 warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
100 l = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
102 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
107 asm volatile ("pushl %0;"
108 ".globl linux_ptrace_test_ret_to_nx_instr;"
109 "linux_ptrace_test_ret_to_nx_instr:"
111 : : "r" (return_address) : "%esp", "memory");
112 #elif defined __x86_64__
113 asm volatile ("pushq %0;"
114 ".globl linux_ptrace_test_ret_to_nx_instr;"
115 "linux_ptrace_test_ret_to_nx_instr:"
117 : : "r" ((uint64_t) (uintptr_t) return_address)
120 # error "!__i386__ && !__x86_64__"
122 gdb_assert_not_reached ("asm block did not terminate");
129 got_pid = waitpid (child, &status, 0);
130 if (got_pid != child)
132 warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
133 (long) got_pid, strerror (errno));
137 if (WIFSIGNALED (status))
139 if (WTERMSIG (status) != SIGKILL)
140 warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
141 (int) WTERMSIG (status));
143 warning (_("Cannot call inferior functions, Linux kernel PaX "
144 "protection forbids return to non-executable pages!"));
148 if (!WIFSTOPPED (status))
150 warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
155 /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
156 if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
158 warning (_("linux_ptrace_test_ret_to_nx: "
159 "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
160 (int) WSTOPSIG (status));
166 l = ptrace (PTRACE_PEEKUSER, child, (void *) (uintptr_t) (EIP * 4), NULL);
167 #elif defined __x86_64__
168 l = ptrace (PTRACE_PEEKUSER, child, (void *) (uintptr_t) (RIP * 8), NULL);
170 # error "!__i386__ && !__x86_64__"
174 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
178 pc = (void *) (uintptr_t) l;
180 if (ptrace (PTRACE_KILL, child, NULL, NULL) != 0)
182 warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_KILL: %s"),
191 got_pid = waitpid (child, &kill_status, 0);
192 if (got_pid != child)
194 warning (_("linux_ptrace_test_ret_to_nx: "
195 "PTRACE_KILL waitpid returned %ld: %s"),
196 (long) got_pid, strerror (errno));
199 if (!WIFSIGNALED (kill_status))
201 warning (_("linux_ptrace_test_ret_to_nx: "
202 "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, you have broken "
228 "Linux kernel i386 NX (non-executable pages) support!"));
229 #endif /* defined __i386__ || defined __x86_64__ */
232 /* Display possible problems on this system. Display them only once per GDB
236 linux_ptrace_init_warnings (void)
238 static int warned = 0;
244 linux_ptrace_test_ret_to_nx ();