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