Commit | Line | Data |
---|---|---|
5f572dec | 1 | /* Linux-specific ptrace manipulation routines. |
32d0add0 | 2 | Copyright (C) 2012-2015 Free Software Foundation, Inc. |
5f572dec JK |
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 | ||
727605ca | 19 | #include "common-defs.h" |
5f572dec | 20 | #include "linux-ptrace.h" |
87b0bb13 | 21 | #include "linux-procfs.h" |
125f8a3d | 22 | #include "linux-waitpid.h" |
87b0bb13 | 23 | #include "buffer.h" |
8bdce1ff | 24 | #include "gdb_wait.h" |
5826e159 | 25 | #include "gdb_ptrace.h" |
87b0bb13 | 26 | |
de0d863e DB |
27 | /* Stores the ptrace options supported by the running kernel. |
28 | A value of -1 means we did not check for features yet. A value | |
29 | of 0 means there are no supported features. */ | |
30 | static int supported_ptrace_options = -1; | |
8009206a | 31 | |
7ae1a6a6 PA |
32 | /* Find all possible reasons we could fail to attach PID and append |
33 | these as strings to the already initialized BUFFER. '\0' | |
34 | termination of BUFFER must be done by the caller. */ | |
87b0bb13 JK |
35 | |
36 | void | |
7ae1a6a6 | 37 | linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer) |
87b0bb13 JK |
38 | { |
39 | pid_t tracerpid; | |
40 | ||
8784d563 | 41 | tracerpid = linux_proc_get_tracerpid_nowarn (pid); |
87b0bb13 | 42 | if (tracerpid > 0) |
7ae1a6a6 PA |
43 | buffer_xml_printf (buffer, _("process %d is already traced " |
44 | "by process %d"), | |
87b0bb13 JK |
45 | (int) pid, (int) tracerpid); |
46 | ||
8784d563 | 47 | if (linux_proc_pid_is_zombie_nowarn (pid)) |
7ae1a6a6 PA |
48 | buffer_xml_printf (buffer, _("process %d is a zombie " |
49 | "- the process has already terminated"), | |
87b0bb13 JK |
50 | (int) pid); |
51 | } | |
aa7c7447 | 52 | |
8784d563 PA |
53 | /* See linux-ptrace.h. */ |
54 | ||
55 | char * | |
56 | linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err) | |
57 | { | |
58 | static char *reason_string; | |
59 | struct buffer buffer; | |
60 | char *warnings; | |
61 | long lwpid = ptid_get_lwp (ptid); | |
62 | ||
63 | xfree (reason_string); | |
64 | ||
65 | buffer_init (&buffer); | |
66 | linux_ptrace_attach_fail_reason (lwpid, &buffer); | |
67 | buffer_grow_str0 (&buffer, ""); | |
68 | warnings = buffer_finish (&buffer); | |
69 | if (warnings[0] != '\0') | |
70 | reason_string = xstrprintf ("%s (%d), %s", | |
049bb5de | 71 | safe_strerror (err), err, warnings); |
8784d563 PA |
72 | else |
73 | reason_string = xstrprintf ("%s (%d)", | |
049bb5de | 74 | safe_strerror (err), err); |
8784d563 PA |
75 | xfree (warnings); |
76 | return reason_string; | |
77 | } | |
78 | ||
6e3c039e | 79 | #if defined __i386__ || defined __x86_64__ |
aa7c7447 JK |
80 | |
81 | /* Address of the 'ret' instruction in asm code block below. */ | |
56000a98 | 82 | EXTERN_C void linux_ptrace_test_ret_to_nx_instr (void); |
aa7c7447 JK |
83 | |
84 | #include <sys/reg.h> | |
85 | #include <sys/mman.h> | |
86 | #include <signal.h> | |
aa7c7447 | 87 | |
6e3c039e | 88 | #endif /* defined __i386__ || defined __x86_64__ */ |
aa7c7447 JK |
89 | |
90 | /* Test broken off-trunk Linux kernel patchset for NX support on i386. It was | |
6e3c039e JK |
91 | removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd. |
92 | ||
93 | Test also x86_64 arch for PaX support. */ | |
aa7c7447 JK |
94 | |
95 | static void | |
96 | linux_ptrace_test_ret_to_nx (void) | |
97 | { | |
6e3c039e | 98 | #if defined __i386__ || defined __x86_64__ |
aa7c7447 JK |
99 | pid_t child, got_pid; |
100 | gdb_byte *return_address, *pc; | |
101 | long l; | |
61a31a67 | 102 | int status, kill_status; |
aa7c7447 | 103 | |
224c3ddb SM |
104 | return_address |
105 | = (gdb_byte *) mmap (NULL, 2, PROT_READ | PROT_WRITE, | |
aa7c7447 JK |
106 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
107 | if (return_address == MAP_FAILED) | |
108 | { | |
109 | warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"), | |
049bb5de | 110 | safe_strerror (errno)); |
aa7c7447 JK |
111 | return; |
112 | } | |
113 | ||
114 | /* Put there 'int3'. */ | |
115 | *return_address = 0xcc; | |
116 | ||
117 | child = fork (); | |
118 | switch (child) | |
119 | { | |
120 | case -1: | |
121 | warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"), | |
049bb5de | 122 | safe_strerror (errno)); |
aa7c7447 JK |
123 | return; |
124 | ||
125 | case 0: | |
96d7229d LM |
126 | l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL, |
127 | (PTRACE_TYPE_ARG4) NULL); | |
aa7c7447 JK |
128 | if (l != 0) |
129 | warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"), | |
049bb5de | 130 | safe_strerror (errno)); |
aa7c7447 JK |
131 | else |
132 | { | |
6e3c039e | 133 | #if defined __i386__ |
aa7c7447 JK |
134 | asm volatile ("pushl %0;" |
135 | ".globl linux_ptrace_test_ret_to_nx_instr;" | |
136 | "linux_ptrace_test_ret_to_nx_instr:" | |
137 | "ret" | |
138 | : : "r" (return_address) : "%esp", "memory"); | |
6e3c039e JK |
139 | #elif defined __x86_64__ |
140 | asm volatile ("pushq %0;" | |
141 | ".globl linux_ptrace_test_ret_to_nx_instr;" | |
142 | "linux_ptrace_test_ret_to_nx_instr:" | |
143 | "ret" | |
bdad4180 MF |
144 | : : "r" ((uint64_t) (uintptr_t) return_address) |
145 | : "%rsp", "memory"); | |
6e3c039e JK |
146 | #else |
147 | # error "!__i386__ && !__x86_64__" | |
148 | #endif | |
aa7c7447 JK |
149 | gdb_assert_not_reached ("asm block did not terminate"); |
150 | } | |
151 | ||
152 | _exit (1); | |
153 | } | |
154 | ||
6e3c039e | 155 | errno = 0; |
aa7c7447 | 156 | got_pid = waitpid (child, &status, 0); |
6e3c039e JK |
157 | if (got_pid != child) |
158 | { | |
159 | warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"), | |
049bb5de | 160 | (long) got_pid, safe_strerror (errno)); |
6e3c039e JK |
161 | return; |
162 | } | |
163 | ||
164 | if (WIFSIGNALED (status)) | |
165 | { | |
166 | if (WTERMSIG (status) != SIGKILL) | |
167 | warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"), | |
168 | (int) WTERMSIG (status)); | |
169 | else | |
170 | warning (_("Cannot call inferior functions, Linux kernel PaX " | |
171 | "protection forbids return to non-executable pages!")); | |
172 | return; | |
173 | } | |
174 | ||
175 | if (!WIFSTOPPED (status)) | |
176 | { | |
177 | warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"), | |
178 | status); | |
179 | return; | |
180 | } | |
aa7c7447 JK |
181 | |
182 | /* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */ | |
6e3c039e JK |
183 | if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV) |
184 | { | |
185 | warning (_("linux_ptrace_test_ret_to_nx: " | |
186 | "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"), | |
187 | (int) WSTOPSIG (status)); | |
188 | return; | |
189 | } | |
aa7c7447 JK |
190 | |
191 | errno = 0; | |
6e3c039e | 192 | #if defined __i386__ |
96d7229d LM |
193 | l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (EIP * 4), |
194 | (PTRACE_TYPE_ARG4) NULL); | |
6e3c039e | 195 | #elif defined __x86_64__ |
96d7229d LM |
196 | l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (RIP * 8), |
197 | (PTRACE_TYPE_ARG4) NULL); | |
6e3c039e JK |
198 | #else |
199 | # error "!__i386__ && !__x86_64__" | |
200 | #endif | |
201 | if (errno != 0) | |
202 | { | |
203 | warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"), | |
049bb5de | 204 | safe_strerror (errno)); |
6e3c039e JK |
205 | return; |
206 | } | |
55e5bf59 | 207 | pc = (gdb_byte *) (uintptr_t) l; |
aa7c7447 | 208 | |
61a31a67 | 209 | kill (child, SIGKILL); |
96d7229d LM |
210 | ptrace (PTRACE_KILL, child, (PTRACE_TYPE_ARG3) NULL, |
211 | (PTRACE_TYPE_ARG4) NULL); | |
61a31a67 JK |
212 | |
213 | errno = 0; | |
214 | got_pid = waitpid (child, &kill_status, 0); | |
215 | if (got_pid != child) | |
6e3c039e | 216 | { |
61a31a67 JK |
217 | warning (_("linux_ptrace_test_ret_to_nx: " |
218 | "PTRACE_KILL waitpid returned %ld: %s"), | |
049bb5de | 219 | (long) got_pid, safe_strerror (errno)); |
6e3c039e JK |
220 | return; |
221 | } | |
61a31a67 | 222 | if (!WIFSIGNALED (kill_status)) |
aa7c7447 | 223 | { |
61a31a67 JK |
224 | warning (_("linux_ptrace_test_ret_to_nx: " |
225 | "PTRACE_KILL status %d is not WIFSIGNALED!"), | |
226 | status); | |
227 | return; | |
aa7c7447 JK |
228 | } |
229 | ||
230 | /* + 1 is there as x86* stops after the 'int3' instruction. */ | |
231 | if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1) | |
232 | { | |
233 | /* PASS */ | |
234 | return; | |
235 | } | |
236 | ||
237 | /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */ | |
238 | if (WSTOPSIG (status) == SIGSEGV && pc == return_address) | |
239 | { | |
240 | /* PASS */ | |
241 | return; | |
242 | } | |
243 | ||
6e3c039e JK |
244 | if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr) |
245 | warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return " | |
246 | "address %p nor is the return instruction %p!"), | |
247 | pc, return_address, &linux_ptrace_test_ret_to_nx_instr); | |
248 | else | |
025e6dce PA |
249 | warning (_("Cannot call inferior functions on this system - " |
250 | "Linux kernel with broken i386 NX (non-executable pages) " | |
251 | "support detected!")); | |
6e3c039e | 252 | #endif /* defined __i386__ || defined __x86_64__ */ |
aa7c7447 JK |
253 | } |
254 | ||
96d7229d LM |
255 | /* Helper function to fork a process and make the child process call |
256 | the function FUNCTION, passing CHILD_STACK as parameter. | |
257 | ||
258 | For MMU-less targets, clone is used instead of fork, and | |
259 | CHILD_STACK is used as stack space for the cloned child. If NULL, | |
260 | stack space is allocated via malloc (and subsequently passed to | |
261 | FUNCTION). For MMU targets, CHILD_STACK is ignored. */ | |
262 | ||
263 | static int | |
264 | linux_fork_to_function (gdb_byte *child_stack, void (*function) (gdb_byte *)) | |
265 | { | |
266 | int child_pid; | |
267 | ||
268 | /* Sanity check the function pointer. */ | |
269 | gdb_assert (function != NULL); | |
270 | ||
271 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) | |
272 | #define STACK_SIZE 4096 | |
273 | ||
274 | if (child_stack == NULL) | |
275 | child_stack = xmalloc (STACK_SIZE * 4); | |
276 | ||
277 | /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */ | |
101158d9 | 278 | #ifdef __ia64__ |
96d7229d LM |
279 | child_pid = __clone2 (function, child_stack, STACK_SIZE, |
280 | CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2); | |
101158d9 | 281 | #else /* !__ia64__ */ |
96d7229d LM |
282 | child_pid = clone (function, child_stack + STACK_SIZE, |
283 | CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2); | |
101158d9 | 284 | #endif /* !__ia64__ */ |
96d7229d LM |
285 | #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */ |
286 | child_pid = fork (); | |
287 | ||
288 | if (child_pid == 0) | |
289 | function (NULL); | |
290 | #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */ | |
291 | ||
292 | if (child_pid == -1) | |
293 | perror_with_name (("fork")); | |
294 | ||
295 | return child_pid; | |
296 | } | |
297 | ||
298 | /* A helper function for linux_check_ptrace_features, called after | |
299 | the child forks a grandchild. */ | |
300 | ||
301 | static void | |
302 | linux_grandchild_function (gdb_byte *child_stack) | |
303 | { | |
304 | /* Free any allocated stack. */ | |
305 | xfree (child_stack); | |
306 | ||
307 | /* This code is only reacheable by the grandchild (child's child) | |
308 | process. */ | |
309 | _exit (0); | |
310 | } | |
311 | ||
312 | /* A helper function for linux_check_ptrace_features, called after | |
313 | the parent process forks a child. The child allows itself to | |
314 | be traced by its parent. */ | |
315 | ||
316 | static void | |
317 | linux_child_function (gdb_byte *child_stack) | |
318 | { | |
319 | ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0); | |
320 | kill (getpid (), SIGSTOP); | |
321 | ||
322 | /* Fork a grandchild. */ | |
323 | linux_fork_to_function (child_stack, linux_grandchild_function); | |
324 | ||
325 | /* This code is only reacheable by the child (grandchild's parent) | |
326 | process. */ | |
327 | _exit (0); | |
328 | } | |
329 | ||
8ae377e8 PA |
330 | static void linux_test_for_tracesysgood (int child_pid); |
331 | static void linux_test_for_tracefork (int child_pid); | |
beed38b8 | 332 | static void linux_test_for_exitkill (int child_pid); |
8ae377e8 | 333 | |
96d7229d LM |
334 | /* Determine ptrace features available on this target. */ |
335 | ||
89245bc0 | 336 | void |
96d7229d LM |
337 | linux_check_ptrace_features (void) |
338 | { | |
339 | int child_pid, ret, status; | |
96d7229d LM |
340 | |
341 | /* Initialize the options. */ | |
de0d863e | 342 | supported_ptrace_options = 0; |
96d7229d LM |
343 | |
344 | /* Fork a child so we can do some testing. The child will call | |
345 | linux_child_function and will get traced. The child will | |
346 | eventually fork a grandchild so we can test fork event | |
347 | reporting. */ | |
348 | child_pid = linux_fork_to_function (NULL, linux_child_function); | |
349 | ||
350 | ret = my_waitpid (child_pid, &status, 0); | |
351 | if (ret == -1) | |
352 | perror_with_name (("waitpid")); | |
353 | else if (ret != child_pid) | |
354 | error (_("linux_check_ptrace_features: waitpid: unexpected result %d."), | |
355 | ret); | |
356 | if (! WIFSTOPPED (status)) | |
357 | error (_("linux_check_ptrace_features: waitpid: unexpected status %d."), | |
358 | status); | |
359 | ||
8ae377e8 | 360 | linux_test_for_tracesysgood (child_pid); |
96d7229d | 361 | |
8ae377e8 PA |
362 | linux_test_for_tracefork (child_pid); |
363 | ||
beed38b8 JB |
364 | linux_test_for_exitkill (child_pid); |
365 | ||
8ae377e8 PA |
366 | /* Clean things up and kill any pending children. */ |
367 | do | |
96d7229d LM |
368 | { |
369 | ret = ptrace (PTRACE_KILL, child_pid, (PTRACE_TYPE_ARG3) 0, | |
370 | (PTRACE_TYPE_ARG4) 0); | |
371 | if (ret != 0) | |
8ae377e8 PA |
372 | warning (_("linux_check_ptrace_features: failed to kill child")); |
373 | my_waitpid (child_pid, &status, 0); | |
96d7229d | 374 | } |
8ae377e8 PA |
375 | while (WIFSTOPPED (status)); |
376 | } | |
96d7229d | 377 | |
8ae377e8 PA |
378 | /* Determine if PTRACE_O_TRACESYSGOOD can be used to catch |
379 | syscalls. */ | |
380 | ||
381 | static void | |
382 | linux_test_for_tracesysgood (int child_pid) | |
383 | { | |
8ae377e8 PA |
384 | int ret; |
385 | ||
96d7229d LM |
386 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, |
387 | (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD); | |
8009206a | 388 | |
96d7229d | 389 | if (ret == 0) |
de0d863e | 390 | supported_ptrace_options |= PTRACE_O_TRACESYSGOOD; |
8ae377e8 | 391 | } |
96d7229d | 392 | |
8ae377e8 PA |
393 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork |
394 | events. */ | |
395 | ||
396 | static void | |
397 | linux_test_for_tracefork (int child_pid) | |
398 | { | |
399 | int ret, status; | |
400 | long second_pid; | |
401 | ||
402 | /* First, set the PTRACE_O_TRACEFORK option. If this fails, we | |
403 | know for sure that it is not supported. */ | |
404 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, | |
405 | (PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK); | |
406 | ||
407 | if (ret != 0) | |
408 | return; | |
409 | ||
de0d863e DB |
410 | /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */ |
411 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, | |
412 | (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK | |
413 | | PTRACE_O_TRACEVFORKDONE)); | |
414 | if (ret == 0) | |
415 | supported_ptrace_options |= PTRACE_O_TRACEVFORKDONE; | |
96d7229d LM |
416 | |
417 | /* Setting PTRACE_O_TRACEFORK did not cause an error, however we | |
418 | don't know for sure that the feature is available; old | |
419 | versions of PTRACE_SETOPTIONS ignored unknown options. | |
420 | Therefore, we attach to the child process, use PTRACE_SETOPTIONS | |
421 | to enable fork tracing, and let it fork. If the process exits, | |
422 | we assume that we can't use PTRACE_O_TRACEFORK; if we get the | |
423 | fork notification, and we can extract the new child's PID, then | |
424 | we assume that we can. | |
425 | ||
426 | We do not explicitly check for vfork tracing here. It is | |
427 | assumed that vfork tracing is available whenever fork tracing | |
428 | is available. */ | |
429 | ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0, | |
430 | (PTRACE_TYPE_ARG4) 0); | |
431 | if (ret != 0) | |
8ae377e8 | 432 | warning (_("linux_test_for_tracefork: failed to resume child")); |
96d7229d LM |
433 | |
434 | ret = my_waitpid (child_pid, &status, 0); | |
435 | ||
436 | /* Check if we received a fork event notification. */ | |
437 | if (ret == child_pid && WIFSTOPPED (status) | |
89a5711c | 438 | && linux_ptrace_get_extended_event (status) == PTRACE_EVENT_FORK) |
96d7229d LM |
439 | { |
440 | /* We did receive a fork event notification. Make sure its PID | |
441 | is reported. */ | |
442 | second_pid = 0; | |
443 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0, | |
444 | (PTRACE_TYPE_ARG4) &second_pid); | |
445 | if (ret == 0 && second_pid != 0) | |
446 | { | |
447 | int second_status; | |
448 | ||
449 | /* We got the PID from the grandchild, which means fork | |
450 | tracing is supported. */ | |
de0d863e DB |
451 | supported_ptrace_options |= PTRACE_O_TRACECLONE; |
452 | supported_ptrace_options |= (PTRACE_O_TRACEFORK | |
453 | | PTRACE_O_TRACEVFORK | |
454 | | PTRACE_O_TRACEEXEC); | |
96d7229d LM |
455 | |
456 | /* Do some cleanup and kill the grandchild. */ | |
457 | my_waitpid (second_pid, &second_status, 0); | |
458 | ret = ptrace (PTRACE_KILL, second_pid, (PTRACE_TYPE_ARG3) 0, | |
459 | (PTRACE_TYPE_ARG4) 0); | |
460 | if (ret != 0) | |
8ae377e8 | 461 | warning (_("linux_test_for_tracefork: " |
96d7229d LM |
462 | "failed to kill second child")); |
463 | my_waitpid (second_pid, &status, 0); | |
464 | } | |
465 | } | |
466 | else | |
8ae377e8 | 467 | warning (_("linux_test_for_tracefork: unexpected result from waitpid " |
96d7229d | 468 | "(%d, status 0x%x)"), ret, status); |
96d7229d LM |
469 | } |
470 | ||
beed38b8 JB |
471 | /* Determine if PTRACE_O_EXITKILL can be used. */ |
472 | ||
473 | static void | |
474 | linux_test_for_exitkill (int child_pid) | |
475 | { | |
476 | int ret; | |
477 | ||
478 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, | |
479 | (PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL); | |
480 | ||
481 | if (ret == 0) | |
de0d863e | 482 | supported_ptrace_options |= PTRACE_O_EXITKILL; |
beed38b8 JB |
483 | } |
484 | ||
485 | /* Enable reporting of all currently supported ptrace events. | |
de0d863e DB |
486 | OPTIONS is a bit mask of extended features we want enabled, |
487 | if supported by the kernel. PTRACE_O_TRACECLONE is always | |
488 | enabled, if supported. */ | |
96d7229d LM |
489 | |
490 | void | |
de0d863e | 491 | linux_enable_event_reporting (pid_t pid, int options) |
96d7229d LM |
492 | { |
493 | /* Check if we have initialized the ptrace features for this | |
494 | target. If not, do it now. */ | |
de0d863e | 495 | if (supported_ptrace_options == -1) |
96d7229d LM |
496 | linux_check_ptrace_features (); |
497 | ||
de0d863e DB |
498 | /* We always want clone events. */ |
499 | options |= PTRACE_O_TRACECLONE; | |
500 | ||
501 | /* Filter out unsupported options. */ | |
502 | options &= supported_ptrace_options; | |
beed38b8 | 503 | |
96d7229d LM |
504 | /* Set the options. */ |
505 | ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, | |
de0d863e | 506 | (PTRACE_TYPE_ARG4) (uintptr_t) options); |
96d7229d LM |
507 | } |
508 | ||
c077881a HZ |
509 | /* Disable reporting of all currently supported ptrace events. */ |
510 | ||
511 | void | |
512 | linux_disable_event_reporting (pid_t pid) | |
513 | { | |
514 | /* Set the options. */ | |
515 | ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, 0); | |
516 | } | |
517 | ||
96d7229d | 518 | /* Returns non-zero if PTRACE_OPTIONS is contained within |
de0d863e | 519 | SUPPORTED_PTRACE_OPTIONS, therefore supported. Returns 0 |
96d7229d LM |
520 | otherwise. */ |
521 | ||
522 | static int | |
523 | ptrace_supports_feature (int ptrace_options) | |
524 | { | |
de0d863e | 525 | if (supported_ptrace_options == -1) |
8784d563 | 526 | linux_check_ptrace_features (); |
96d7229d | 527 | |
de0d863e | 528 | return ((supported_ptrace_options & ptrace_options) == ptrace_options); |
96d7229d LM |
529 | } |
530 | ||
531 | /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace, | |
532 | 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is | |
533 | PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK, | |
534 | since they were all added to the kernel at the same time. */ | |
535 | ||
536 | int | |
537 | linux_supports_tracefork (void) | |
538 | { | |
539 | return ptrace_supports_feature (PTRACE_O_TRACEFORK); | |
540 | } | |
541 | ||
94585166 DB |
542 | /* Returns non-zero if PTRACE_EVENT_EXEC is supported by ptrace, |
543 | 0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is | |
544 | PTRACE_EVENT_CLONE, PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK, | |
545 | since they were all added to the kernel at the same time. */ | |
546 | ||
547 | int | |
548 | linux_supports_traceexec (void) | |
549 | { | |
550 | return ptrace_supports_feature (PTRACE_O_TRACEEXEC); | |
551 | } | |
552 | ||
96d7229d LM |
553 | /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace, |
554 | 0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is | |
555 | PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK, | |
556 | since they were all added to the kernel at the same time. */ | |
557 | ||
558 | int | |
559 | linux_supports_traceclone (void) | |
560 | { | |
561 | return ptrace_supports_feature (PTRACE_O_TRACECLONE); | |
562 | } | |
563 | ||
564 | /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by | |
565 | ptrace, 0 otherwise. */ | |
566 | ||
567 | int | |
568 | linux_supports_tracevforkdone (void) | |
569 | { | |
570 | return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE); | |
571 | } | |
572 | ||
573 | /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace, | |
574 | 0 otherwise. */ | |
575 | ||
576 | int | |
577 | linux_supports_tracesysgood (void) | |
578 | { | |
579 | return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD); | |
580 | } | |
581 | ||
aa7c7447 JK |
582 | /* Display possible problems on this system. Display them only once per GDB |
583 | execution. */ | |
584 | ||
585 | void | |
586 | linux_ptrace_init_warnings (void) | |
587 | { | |
588 | static int warned = 0; | |
589 | ||
590 | if (warned) | |
591 | return; | |
592 | warned = 1; | |
593 | ||
594 | linux_ptrace_test_ret_to_nx (); | |
595 | } | |
8009206a | 596 | |
89a5711c DB |
597 | /* Extract extended ptrace event from wait status. */ |
598 | ||
599 | int | |
600 | linux_ptrace_get_extended_event (int wstat) | |
601 | { | |
602 | return (wstat >> 16); | |
603 | } | |
604 | ||
605 | /* Determine whether wait status denotes an extended event. */ | |
606 | ||
607 | int | |
608 | linux_is_extended_waitstatus (int wstat) | |
609 | { | |
610 | return (linux_ptrace_get_extended_event (wstat) != 0); | |
611 | } | |
c9587f88 AT |
612 | |
613 | /* Return true if the event in LP may be caused by breakpoint. */ | |
614 | ||
615 | int | |
616 | linux_wstatus_maybe_breakpoint (int wstat) | |
617 | { | |
618 | return (WIFSTOPPED (wstat) | |
619 | && (WSTOPSIG (wstat) == SIGTRAP | |
620 | /* SIGILL and SIGSEGV are also treated as traps in case a | |
621 | breakpoint is inserted at the current PC. */ | |
622 | || WSTOPSIG (wstat) == SIGILL | |
623 | || WSTOPSIG (wstat) == SIGSEGV)); | |
624 | } |