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