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