GDB (xrefs)
Loading...
Searching...
No Matches
linux-ptrace.h
Go to the documentation of this file.
1/* Copyright (C) 2011-2023 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18#ifndef NAT_LINUX_PTRACE_H
19#define NAT_LINUX_PTRACE_H
20
21#include "nat/gdb_ptrace.h"
22#include "gdbsupport/gdb_wait.h"
23
24#ifdef __UCLIBC__
25#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
26/* PTRACE_TEXT_ADDR and friends. */
27#include <asm/ptrace.h>
28#define HAS_NOMMU
29#endif
30#endif
31
32#if !defined(PTRACE_TYPE_ARG3)
33#define PTRACE_TYPE_ARG3 void *
34#endif
35
36#if !defined(PTRACE_TYPE_ARG4)
37#define PTRACE_TYPE_ARG4 void *
38#endif
39
40#ifndef PTRACE_GETSIGINFO
41# define PTRACE_GETSIGINFO 0x4202
42# define PTRACE_SETSIGINFO 0x4203
43#endif /* PTRACE_GETSIGINF */
44
45#ifndef PTRACE_GETREGSET
46#define PTRACE_GETREGSET 0x4204
47#endif
48
49#ifndef PTRACE_SETREGSET
50#define PTRACE_SETREGSET 0x4205
51#endif
52
53/* If the system headers did not provide the constants, hard-code the normal
54 values. */
55#ifndef PTRACE_EVENT_FORK
56
57#define PTRACE_SETOPTIONS 0x4200
58#define PTRACE_GETEVENTMSG 0x4201
59
60/* options set using PTRACE_SETOPTIONS */
61#define PTRACE_O_TRACESYSGOOD 0x00000001
62#define PTRACE_O_TRACEFORK 0x00000002
63#define PTRACE_O_TRACEVFORK 0x00000004
64#define PTRACE_O_TRACECLONE 0x00000008
65#define PTRACE_O_TRACEEXEC 0x00000010
66#define PTRACE_O_TRACEVFORKDONE 0x00000020
67#define PTRACE_O_TRACEEXIT 0x00000040
68
69/* Wait extended result codes for the above trace options. */
70#define PTRACE_EVENT_FORK 1
71#define PTRACE_EVENT_VFORK 2
72#define PTRACE_EVENT_CLONE 3
73#define PTRACE_EVENT_EXEC 4
74#define PTRACE_EVENT_VFORK_DONE 5
75#define PTRACE_EVENT_EXIT 6
76
77#endif /* PTRACE_EVENT_FORK */
78
79#ifndef PTRACE_O_EXITKILL
80/* Only defined in Linux Kernel 3.8 or later. */
81#define PTRACE_O_EXITKILL 0x00100000
82#endif
83
84#if (defined __bfin__ || defined __frv__ || defined __sh__) \
85 && !defined PTRACE_GETFDPIC
86#define PTRACE_GETFDPIC 31
87#define PTRACE_GETFDPIC_EXEC 0
88#define PTRACE_GETFDPIC_INTERP 1
89#endif
90
91/* We can't always assume that this flag is available, but all systems
92 with the ptrace event handlers also have __WALL, so it's safe to use
93 in some contexts. */
94#ifndef __WALL
95#define __WALL 0x40000000 /* Wait for any child. */
96#endif
97
98/* True if whether a breakpoint/watchpoint triggered can be determined
99 from the si_code of SIGTRAP's siginfo_t (TRAP_BRKPT/TRAP_HWBKPT).
100 That is, if the kernel can tell us whether the thread executed a
101 software breakpoint, we trust it. The kernel will be determining
102 that from the hardware (e.g., from which exception was raised in
103 the CPU). Relying on whether a breakpoint is planted in memory at
104 the time the SIGTRAP is processed to determine whether the thread
105 stopped for a software breakpoint can be too late. E.g., the
106 breakpoint could have been removed since. Or the thread could have
107 stepped an instruction the size of a breakpoint instruction, and
108 before the stop is processed a breakpoint is inserted at its
109 address. Getting these wrong is disastrous on decr_pc_after_break
110 architectures. The moribund location mechanism helps with that
111 somewhat but it is an heuristic, and can well fail. Getting that
112 information out of the kernel and ultimately out of the CPU is the
113 way to go. That said, some architecture may get the si_code wrong,
114 and as such we're leaving fallback code in place. We'll remove
115 this after a while if no problem is reported. */
116#define USE_SIGTRAP_SIGINFO 1
117
118/* The x86 kernel gets some of the si_code values backwards, like
119 this:
120
121 | what | si_code |
122 |------------------------------------------+-------------|
123 | software breakpoints (int3) | SI_KERNEL |
124 | single-steps | TRAP_TRACE |
125 | single-stepping a syscall | TRAP_BRKPT |
126 | user sent SIGTRAP | 0 |
127 | exec SIGTRAP (when no PTRACE_EVENT_EXEC) | 0 |
128 | hardware breakpoints/watchpoints | TRAP_HWBKPT |
129
130 That is, it reports SI_KERNEL for software breakpoints (and only
131 for those), and TRAP_BRKPT for single-stepping a syscall... If the
132 kernel is ever fixed, we'll just have to detect it like we detect
133 optional ptrace features: by forking and debugging ourselves,
134 running to a breakpoint and checking what comes out of
135 siginfo->si_code.
136
137 The ppc kernel does use TRAP_BRKPT for software breakpoints
138 in PowerPC code, but it uses SI_KERNEL for software breakpoints
139 in SPU code on a Cell/B.E. However, SI_KERNEL is never seen
140 on a SIGTRAP for any other reason.
141
142 The MIPS kernel up until 4.5 used SI_KERNEL for all kernel
143 generated traps. Since:
144
145 - MIPS doesn't do hardware single-step.
146 - We don't need to care about exec SIGTRAPs --- we assume
147 PTRACE_EVENT_EXEC is available.
148 - The MIPS kernel doesn't support hardware breakpoints.
149
150 on MIPS, all we need to care about is distinguishing between
151 software breakpoints and hardware watchpoints, which can be done by
152 peeking the debug registers.
153
154 Beginning with Linux 4.6, the MIPS port reports proper TRAP_BRKPT and
155 TRAP_HWBKPT codes, so we also match them.
156
157 The generic Linux target code should use GDB_ARCH_IS_TRAP_* instead
158 of TRAP_* to abstract out these peculiarities. */
159#if defined __i386__ || defined __x86_64__
160# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL)
161# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
162#elif defined __powerpc__
163# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL || (X) == TRAP_BRKPT)
164# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
165#elif defined __mips__
166# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL || (X) == TRAP_BRKPT)
167# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == SI_KERNEL || (X) == TRAP_HWBKPT)
168#else
169# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == TRAP_BRKPT)
170# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
171#endif
172
173#ifndef TRAP_HWBKPT
174# define TRAP_HWBKPT 4
175#endif
176
177extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
178
179/* Find all possible reasons we could have failed to attach to PTID
180 and return them as a string. ERR is the error PTRACE_ATTACH failed
181 with (an errno). */
182extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
183
184extern void linux_ptrace_init_warnings (void);
185extern void linux_check_ptrace_features (void);
186extern void linux_enable_event_reporting (pid_t pid, int attached);
187extern void linux_disable_event_reporting (pid_t pid);
188extern int linux_ptrace_get_extended_event (int wstat);
189extern int linux_is_extended_waitstatus (int wstat);
190extern int linux_wstatus_maybe_breakpoint (int wstat);
191
192#endif /* NAT_LINUX_PTRACE_H */
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1791
int linux_is_extended_waitstatus(int wstat)
void linux_check_ptrace_features(void)
int linux_ptrace_get_extended_event(int wstat)
int linux_wstatus_maybe_breakpoint(int wstat)
void linux_ptrace_init_warnings(void)
void linux_disable_event_reporting(pid_t pid)
void linux_enable_event_reporting(pid_t pid, int attached)
std::string linux_ptrace_attach_fail_reason_string(ptid_t ptid, int err)
std::string linux_ptrace_attach_fail_reason(pid_t pid)