Commit | Line | Data |
---|---|---|
5f572dec JK |
1 | /* Linux-specific ptrace manipulation routines. |
2 | Copyright (C) 2012 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. */ | |
18 | ||
19 | #ifdef GDBSERVER | |
20 | #include "server.h" | |
21 | #else | |
22 | #include "defs.h" | |
23 | #include "gdb_string.h" | |
24 | #endif | |
25 | ||
26 | #include "linux-ptrace.h" | |
87b0bb13 JK |
27 | #include "linux-procfs.h" |
28 | #include "buffer.h" | |
aa7c7447 | 29 | #include "gdb_assert.h" |
87b0bb13 JK |
30 | |
31 | /* Find all possible reasons we could fail to attach PID and append these | |
32 | newline terminated reason strings to initialized BUFFER. '\0' termination | |
33 | of BUFFER must be done by the caller. */ | |
34 | ||
35 | void | |
36 | linux_ptrace_attach_warnings (pid_t pid, struct buffer *buffer) | |
37 | { | |
38 | pid_t tracerpid; | |
39 | ||
40 | tracerpid = linux_proc_get_tracerpid (pid); | |
41 | if (tracerpid > 0) | |
42 | buffer_xml_printf (buffer, _("warning: process %d is already traced " | |
43 | "by process %d\n"), | |
44 | (int) pid, (int) tracerpid); | |
45 | ||
46 | if (linux_proc_pid_is_zombie (pid)) | |
47 | buffer_xml_printf (buffer, _("warning: process %d is a zombie " | |
48 | "- the process has already terminated\n"), | |
49 | (int) pid); | |
50 | } | |
aa7c7447 JK |
51 | |
52 | #ifdef __i386__ | |
53 | ||
54 | /* Address of the 'ret' instruction in asm code block below. */ | |
55 | extern void (linux_ptrace_test_ret_to_nx_instr) (void); | |
56 | ||
57 | #include <sys/reg.h> | |
58 | #include <sys/mman.h> | |
59 | #include <signal.h> | |
60 | #include <sys/wait.h> | |
61 | #include <stdint.h> | |
62 | ||
63 | #endif /* __i386__ */ | |
64 | ||
65 | /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was | |
66 | removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd. */ | |
67 | ||
68 | static void | |
69 | linux_ptrace_test_ret_to_nx (void) | |
70 | { | |
71 | #ifdef __i386__ | |
72 | pid_t child, got_pid; | |
73 | gdb_byte *return_address, *pc; | |
74 | long l; | |
75 | int status; | |
76 | ||
77 | return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE, | |
78 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
79 | if (return_address == MAP_FAILED) | |
80 | { | |
81 | warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"), | |
82 | strerror (errno)); | |
83 | return; | |
84 | } | |
85 | ||
86 | /* Put there 'int3'. */ | |
87 | *return_address = 0xcc; | |
88 | ||
89 | child = fork (); | |
90 | switch (child) | |
91 | { | |
92 | case -1: | |
93 | warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"), | |
94 | strerror (errno)); | |
95 | return; | |
96 | ||
97 | case 0: | |
98 | l = ptrace (PTRACE_TRACEME, 0, NULL, NULL); | |
99 | if (l != 0) | |
100 | warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"), | |
101 | strerror (errno)); | |
102 | else | |
103 | { | |
104 | asm volatile ("pushl %0;" | |
105 | ".globl linux_ptrace_test_ret_to_nx_instr;" | |
106 | "linux_ptrace_test_ret_to_nx_instr:" | |
107 | "ret" | |
108 | : : "r" (return_address) : "%esp", "memory"); | |
109 | gdb_assert_not_reached ("asm block did not terminate"); | |
110 | } | |
111 | ||
112 | _exit (1); | |
113 | } | |
114 | ||
115 | got_pid = waitpid (child, &status, 0); | |
116 | gdb_assert (got_pid == child); | |
117 | gdb_assert (WIFSTOPPED (status)); | |
118 | ||
119 | /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */ | |
120 | gdb_assert (WSTOPSIG (status) == SIGTRAP || WSTOPSIG (status) == SIGSEGV); | |
121 | ||
122 | errno = 0; | |
123 | l = ptrace (PTRACE_PEEKUSER, child, (void *) (uintptr_t) (EIP * 4), NULL); | |
124 | gdb_assert (errno == 0); | |
125 | pc = (void *) (uintptr_t) l; | |
126 | ||
127 | if (ptrace (PTRACE_KILL, child, NULL, NULL) != 0) | |
128 | warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_KILL: %s"), | |
129 | strerror (errno)); | |
130 | else | |
131 | { | |
132 | int kill_status; | |
133 | ||
134 | got_pid = waitpid (child, &kill_status, 0); | |
135 | gdb_assert (got_pid == child); | |
136 | gdb_assert (WIFSIGNALED (kill_status)); | |
137 | } | |
138 | ||
139 | /* + 1 is there as x86* stops after the 'int3' instruction. */ | |
140 | if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1) | |
141 | { | |
142 | /* PASS */ | |
143 | return; | |
144 | } | |
145 | ||
146 | /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */ | |
147 | if (WSTOPSIG (status) == SIGSEGV && pc == return_address) | |
148 | { | |
149 | /* PASS */ | |
150 | return; | |
151 | } | |
152 | ||
153 | gdb_assert ((void (*) (void)) pc == &linux_ptrace_test_ret_to_nx_instr); | |
154 | ||
155 | warning (_("Cannot call inferior functions, you have broken " | |
156 | "Linux kernel i386 NX (non-executable pages) support!")); | |
157 | #endif /* __i386__ */ | |
158 | } | |
159 | ||
160 | /* Display possible problems on this system. Display them only once per GDB | |
161 | execution. */ | |
162 | ||
163 | void | |
164 | linux_ptrace_init_warnings (void) | |
165 | { | |
166 | static int warned = 0; | |
167 | ||
168 | if (warned) | |
169 | return; | |
170 | warned = 1; | |
171 | ||
172 | linux_ptrace_test_ret_to_nx (); | |
173 | } |