Commit | Line | Data |
---|---|---|
618f726f | 1 | /* Copyright (C) 2011-2016 Free Software Foundation, Inc. |
af96c192 YQ |
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 COMMON_LINUX_PTRACE_H | |
19 | #define COMMON_LINUX_PTRACE_H | |
20 | ||
87b0bb13 JK |
21 | struct buffer; |
22 | ||
5826e159 | 23 | #include "nat/gdb_ptrace.h" |
af96c192 | 24 | |
96d7229d LM |
25 | #ifdef __UCLIBC__ |
26 | #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__)) | |
27 | /* PTRACE_TEXT_ADDR and friends. */ | |
28 | #include <asm/ptrace.h> | |
29 | #define HAS_NOMMU | |
30 | #endif | |
31 | #endif | |
32 | ||
33 | #if !defined(PTRACE_TYPE_ARG3) | |
34 | #define PTRACE_TYPE_ARG3 void * | |
35 | #endif | |
36 | ||
37 | #if !defined(PTRACE_TYPE_ARG4) | |
38 | #define PTRACE_TYPE_ARG4 void * | |
39 | #endif | |
40 | ||
af96c192 YQ |
41 | #ifndef PTRACE_GETSIGINFO |
42 | # define PTRACE_GETSIGINFO 0x4202 | |
43 | # define PTRACE_SETSIGINFO 0x4203 | |
44 | #endif /* PTRACE_GETSIGINF */ | |
45 | ||
ca9b78ce YQ |
46 | #ifndef PTRACE_GETREGSET |
47 | #define PTRACE_GETREGSET 0x4204 | |
48 | #endif | |
49 | ||
50 | #ifndef PTRACE_SETREGSET | |
51 | #define PTRACE_SETREGSET 0x4205 | |
52 | #endif | |
53 | ||
af96c192 YQ |
54 | /* If the system headers did not provide the constants, hard-code the normal |
55 | values. */ | |
56 | #ifndef PTRACE_EVENT_FORK | |
57 | ||
58 | #define PTRACE_SETOPTIONS 0x4200 | |
59 | #define PTRACE_GETEVENTMSG 0x4201 | |
60 | ||
61 | /* options set using PTRACE_SETOPTIONS */ | |
62 | #define PTRACE_O_TRACESYSGOOD 0x00000001 | |
63 | #define PTRACE_O_TRACEFORK 0x00000002 | |
64 | #define PTRACE_O_TRACEVFORK 0x00000004 | |
65 | #define PTRACE_O_TRACECLONE 0x00000008 | |
66 | #define PTRACE_O_TRACEEXEC 0x00000010 | |
67 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 | |
68 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
69 | ||
70 | /* Wait extended result codes for the above trace options. */ | |
71 | #define PTRACE_EVENT_FORK 1 | |
72 | #define PTRACE_EVENT_VFORK 2 | |
73 | #define PTRACE_EVENT_CLONE 3 | |
74 | #define PTRACE_EVENT_EXEC 4 | |
75 | #define PTRACE_EVENT_VFORK_DONE 5 | |
76 | #define PTRACE_EVENT_EXIT 6 | |
77 | ||
78 | #endif /* PTRACE_EVENT_FORK */ | |
79 | ||
beed38b8 JB |
80 | #ifndef PTRACE_O_EXITKILL |
81 | /* Only defined in Linux Kernel 3.8 or later. */ | |
82 | #define PTRACE_O_EXITKILL 0x00100000 | |
83 | #endif | |
84 | ||
723b724b MF |
85 | #if (defined __bfin__ || defined __frv__ || defined __sh__) \ |
86 | && !defined PTRACE_GETFDPIC | |
87 | #define PTRACE_GETFDPIC 31 | |
88 | #define PTRACE_GETFDPIC_EXEC 0 | |
89 | #define PTRACE_GETFDPIC_INTERP 1 | |
90 | #endif | |
91 | ||
af96c192 YQ |
92 | /* We can't always assume that this flag is available, but all systems |
93 | with the ptrace event handlers also have __WALL, so it's safe to use | |
94 | in some contexts. */ | |
95 | #ifndef __WALL | |
96 | #define __WALL 0x40000000 /* Wait for any child. */ | |
97 | #endif | |
98 | ||
faf09f01 PA |
99 | /* True if whether a breakpoint/watchpoint triggered can be determined |
100 | from the si_code of SIGTRAP's siginfo_t (TRAP_BRKPT/TRAP_HWBKPT). | |
101 | That is, if the kernel can tell us whether the thread executed a | |
102 | software breakpoint, we trust it. The kernel will be determining | |
103 | that from the hardware (e.g., from which exception was raised in | |
104 | the CPU). Relying on whether a breakpoint is planted in memory at | |
105 | the time the SIGTRAP is processed to determine whether the thread | |
106 | stopped for a software breakpoint can be too late. E.g., the | |
107 | breakpoint could have been removed since. Or the thread could have | |
108 | stepped an instruction the size of a breakpoint instruction, and | |
109 | before the stop is processed a breakpoint is inserted at its | |
110 | address. Getting these wrong is disastrous on decr_pc_after_break | |
111 | architectures. The moribund location mechanism helps with that | |
112 | somewhat but it is an heuristic, and can well fail. Getting that | |
113 | information out of the kernel and ultimately out of the CPU is the | |
114 | way to go. That said, some architecture may get the si_code wrong, | |
115 | and as such we're leaving fallback code in place. We'll remove | |
116 | this after a while if no problem is reported. */ | |
117 | #define USE_SIGTRAP_SIGINFO 1 | |
118 | ||
119 | /* The x86 kernel gets some of the si_code values backwards, like | |
120 | this: | |
121 | ||
e7ad2f14 PA |
122 | | what | si_code | |
123 | |------------------------------------------+-------------| | |
124 | | software breakpoints (int3) | SI_KERNEL | | |
125 | | single-steps | TRAP_TRACE | | |
126 | | single-stepping a syscall | TRAP_BRKPT | | |
127 | | user sent SIGTRAP | 0 | | |
128 | | exec SIGTRAP (when no PTRACE_EVENT_EXEC) | 0 | | |
129 | | hardware breakpoints/watchpoints | TRAP_HWBKPT | | |
faf09f01 PA |
130 | |
131 | That is, it reports SI_KERNEL for software breakpoints (and only | |
132 | for those), and TRAP_BRKPT for single-stepping a syscall... If the | |
133 | kernel is ever fixed, we'll just have to detect it like we detect | |
134 | optional ptrace features: by forking and debugging ourselves, | |
135 | running to a breakpoint and checking what comes out of | |
136 | siginfo->si_code. | |
137 | ||
1db33b5a UW |
138 | The ppc kernel does use TRAP_BRKPT for software breakpoints |
139 | in PowerPC code, but it uses SI_KERNEL for software breakpoints | |
140 | in SPU code on a Cell/B.E. However, SI_KERNEL is never seen | |
141 | on a SIGTRAP for any other reason. | |
142 | ||
77770d83 PA |
143 | The MIPS kernel up until 4.5 used SI_KERNEL for all kernel |
144 | generated traps. Since: | |
e7ad2f14 PA |
145 | |
146 | - MIPS doesn't do hardware single-step. | |
147 | - We don't need to care about exec SIGTRAPs --- we assume | |
148 | PTRACE_EVENT_EXEC is available. | |
149 | - The MIPS kernel doesn't support hardware breakpoints. | |
150 | ||
151 | on MIPS, all we need to care about is distinguishing between | |
152 | software breakpoints and hardware watchpoints, which can be done by | |
153 | peeking the debug registers. | |
154 | ||
77770d83 PA |
155 | Beginning with Linux 4.6, the MIPS port reports proper TRAP_BRKPT and |
156 | TRAP_HWBKPT codes, so we also match them. | |
157 | ||
e7ad2f14 PA |
158 | The generic Linux target code should use GDB_ARCH_IS_TRAP_* instead |
159 | of TRAP_* to abstract out these peculiarities. */ | |
faf09f01 | 160 | #if defined __i386__ || defined __x86_64__ |
1db33b5a | 161 | # define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL) |
e7ad2f14 | 162 | # define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT) |
1db33b5a UW |
163 | #elif defined __powerpc__ |
164 | # define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL || (X) == TRAP_BRKPT) | |
e7ad2f14 PA |
165 | # define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT) |
166 | #elif defined __mips__ | |
77770d83 PA |
167 | # define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL || (X) == TRAP_BRKPT) |
168 | # define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == SI_KERNEL || (X) == TRAP_HWBKPT) | |
faf09f01 | 169 | #else |
1db33b5a | 170 | # define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == TRAP_BRKPT) |
e7ad2f14 | 171 | # define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT) |
faf09f01 PA |
172 | #endif |
173 | ||
174 | #ifndef TRAP_HWBKPT | |
175 | # define TRAP_HWBKPT 4 | |
176 | #endif | |
177 | ||
7ae1a6a6 | 178 | extern void linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer); |
8784d563 PA |
179 | |
180 | /* Find all possible reasons we could have failed to attach to PTID | |
181 | and return them as a string. ERR is the error PTRACE_ATTACH failed | |
182 | with (an errno). The result is stored in a static buffer. This | |
183 | string should be copied into a buffer by the client if the string | |
184 | will not be immediately used, or if it must persist. */ | |
185 | extern char *linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err); | |
186 | ||
aa7c7447 | 187 | extern void linux_ptrace_init_warnings (void); |
89245bc0 | 188 | extern void linux_check_ptrace_features (void); |
beed38b8 | 189 | extern void linux_enable_event_reporting (pid_t pid, int attached); |
c077881a | 190 | extern void linux_disable_event_reporting (pid_t pid); |
96d7229d | 191 | extern int linux_supports_tracefork (void); |
94585166 | 192 | extern int linux_supports_traceexec (void); |
96d7229d LM |
193 | extern int linux_supports_traceclone (void); |
194 | extern int linux_supports_tracevforkdone (void); | |
195 | extern int linux_supports_tracesysgood (void); | |
89a5711c DB |
196 | extern int linux_ptrace_get_extended_event (int wstat); |
197 | extern int linux_is_extended_waitstatus (int wstat); | |
c9587f88 | 198 | extern int linux_wstatus_maybe_breakpoint (int wstat); |
87b0bb13 | 199 | |
af96c192 | 200 | #endif /* COMMON_LINUX_PTRACE_H */ |