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