]>
Commit | Line | Data |
---|---|---|
040baaf6 GB |
1 | /* Native-dependent code for GNU/Linux x86 (i386 and x86-64). |
2 | ||
213516ef | 3 | Copyright (C) 1999-2023 Free Software Foundation, Inc. |
040baaf6 GB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "inferior.h" | |
22 | #include "elf/common.h" | |
23 | #include "gdb_proc_service.h" | |
5826e159 | 24 | #include "nat/gdb_ptrace.h" |
040baaf6 GB |
25 | #include <sys/user.h> |
26 | #include <sys/procfs.h> | |
72fde3df | 27 | #include <sys/uio.h> |
040baaf6 | 28 | |
df7e5265 | 29 | #include "x86-nat.h" |
040baaf6 GB |
30 | #ifndef __x86_64__ |
31 | #include "i386-linux-nat.h" | |
32 | #endif | |
33 | #include "x86-linux-nat.h" | |
34 | #include "i386-linux-tdep.h" | |
35 | #ifdef __x86_64__ | |
36 | #include "amd64-linux-tdep.h" | |
37 | #endif | |
268a13a5 | 38 | #include "gdbsupport/x86-xstate.h" |
9848bf83 | 39 | #include "nat/x86-xstate.h" |
040baaf6 | 40 | #include "nat/linux-btrace.h" |
7b669087 | 41 | #include "nat/linux-nat.h" |
4b134ca1 | 42 | #include "nat/x86-linux.h" |
8e5d4070 | 43 | #include "nat/x86-linux-dregs.h" |
ca9b78ce | 44 | #include "nat/linux-ptrace.h" |
040baaf6 | 45 | |
135340af | 46 | /* linux_nat_target::low_new_fork implementation. */ |
040baaf6 | 47 | |
135340af PA |
48 | void |
49 | x86_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid) | |
040baaf6 GB |
50 | { |
51 | pid_t parent_pid; | |
df7e5265 GB |
52 | struct x86_debug_reg_state *parent_state; |
53 | struct x86_debug_reg_state *child_state; | |
040baaf6 GB |
54 | |
55 | /* NULL means no watchpoint has ever been set in the parent. In | |
56 | that case, there's nothing to do. */ | |
57 | if (parent->arch_private == NULL) | |
58 | return; | |
59 | ||
60 | /* Linux kernel before 2.6.33 commit | |
61 | 72f674d203cd230426437cdcf7dd6f681dad8b0d | |
62 | will inherit hardware debug registers from parent | |
63 | on fork/vfork/clone. Newer Linux kernels create such tasks with | |
64 | zeroed debug registers. | |
65 | ||
66 | GDB core assumes the child inherits the watchpoints/hw | |
67 | breakpoints of the parent, and will remove them all from the | |
68 | forked off process. Copy the debug registers mirrors into the | |
69 | new process so that all breakpoints and watchpoints can be | |
70 | removed together. The debug registers mirror will become zeroed | |
71 | in the end before detaching the forked off process, thus making | |
72 | this compatible with older Linux kernels too. */ | |
73 | ||
e99b03dc | 74 | parent_pid = parent->ptid.pid (); |
df7e5265 GB |
75 | parent_state = x86_debug_reg_state (parent_pid); |
76 | child_state = x86_debug_reg_state (child_pid); | |
040baaf6 GB |
77 | *child_state = *parent_state; |
78 | } | |
79 | \f | |
80 | ||
f6ac5f3d PA |
81 | x86_linux_nat_target::~x86_linux_nat_target () |
82 | { | |
83 | } | |
040baaf6 | 84 | |
200fd287 AB |
85 | /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ |
86 | ||
f6ac5f3d PA |
87 | void |
88 | x86_linux_nat_target::post_startup_inferior (ptid_t ptid) | |
040baaf6 | 89 | { |
df7e5265 | 90 | x86_cleanup_dregs (); |
f6ac5f3d | 91 | linux_nat_target::post_startup_inferior (ptid); |
040baaf6 GB |
92 | } |
93 | ||
94 | #ifdef __x86_64__ | |
95 | /* Value of CS segment register: | |
96 | 64bit process: 0x33 | |
97 | 32bit process: 0x23 */ | |
98 | #define AMD64_LINUX_USER64_CS 0x33 | |
99 | ||
100 | /* Value of DS segment register: | |
101 | LP64 process: 0x0 | |
102 | X32 process: 0x2b */ | |
103 | #define AMD64_LINUX_X32_DS 0x2b | |
104 | #endif | |
105 | ||
106 | /* Get Linux/x86 target description from running target. */ | |
107 | ||
f6ac5f3d PA |
108 | const struct target_desc * |
109 | x86_linux_nat_target::read_description () | |
040baaf6 GB |
110 | { |
111 | int tid; | |
112 | int is_64bit = 0; | |
113 | #ifdef __x86_64__ | |
114 | int is_x32; | |
115 | #endif | |
116 | static uint64_t xcr0; | |
117 | uint64_t xcr0_features_bits; | |
118 | ||
a4a688ff JB |
119 | if (inferior_ptid == null_ptid) |
120 | return this->beneath ()->read_description (); | |
121 | ||
fbf3c4b9 | 122 | tid = inferior_ptid.pid (); |
040baaf6 GB |
123 | |
124 | #ifdef __x86_64__ | |
125 | { | |
126 | unsigned long cs; | |
127 | unsigned long ds; | |
128 | ||
129 | /* Get CS register. */ | |
130 | errno = 0; | |
131 | cs = ptrace (PTRACE_PEEKUSER, tid, | |
132 | offsetof (struct user_regs_struct, cs), 0); | |
133 | if (errno != 0) | |
134 | perror_with_name (_("Couldn't get CS register")); | |
135 | ||
136 | is_64bit = cs == AMD64_LINUX_USER64_CS; | |
137 | ||
138 | /* Get DS register. */ | |
139 | errno = 0; | |
140 | ds = ptrace (PTRACE_PEEKUSER, tid, | |
141 | offsetof (struct user_regs_struct, ds), 0); | |
142 | if (errno != 0) | |
143 | perror_with_name (_("Couldn't get DS register")); | |
144 | ||
145 | is_x32 = ds == AMD64_LINUX_X32_DS; | |
146 | ||
147 | if (sizeof (void *) == 4 && is_64bit && !is_x32) | |
148 | error (_("Can't debug 64-bit process with 32-bit GDB")); | |
149 | } | |
150 | #elif HAVE_PTRACE_GETFPXREGS | |
151 | if (have_ptrace_getfpxregs == -1) | |
152 | { | |
153 | elf_fpxregset_t fpxregs; | |
154 | ||
155 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) | |
156 | { | |
157 | have_ptrace_getfpxregs = 0; | |
0bdb2f78 | 158 | have_ptrace_getregset = TRIBOOL_FALSE; |
35b4818d | 159 | return i386_linux_read_description (X86_XSTATE_X87_MASK); |
040baaf6 GB |
160 | } |
161 | } | |
162 | #endif | |
163 | ||
0bdb2f78 | 164 | if (have_ptrace_getregset == TRIBOOL_UNKNOWN) |
040baaf6 | 165 | { |
df7e5265 | 166 | uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; |
040baaf6 GB |
167 | struct iovec iov; |
168 | ||
169 | iov.iov_base = xstateregs; | |
170 | iov.iov_len = sizeof (xstateregs); | |
171 | ||
172 | /* Check if PTRACE_GETREGSET works. */ | |
173 | if (ptrace (PTRACE_GETREGSET, tid, | |
174 | (unsigned int) NT_X86_XSTATE, &iov) < 0) | |
0bdb2f78 | 175 | have_ptrace_getregset = TRIBOOL_FALSE; |
040baaf6 GB |
176 | else |
177 | { | |
0bdb2f78 | 178 | have_ptrace_getregset = TRIBOOL_TRUE; |
040baaf6 GB |
179 | |
180 | /* Get XCR0 from XSAVE extended state. */ | |
181 | xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET | |
182 | / sizeof (uint64_t))]; | |
9848bf83 JB |
183 | |
184 | m_xsave_layout = x86_fetch_xsave_layout (xcr0, x86_xsave_length ()); | |
040baaf6 GB |
185 | } |
186 | } | |
187 | ||
188 | /* Check the native XCR0 only if PTRACE_GETREGSET is available. If | |
189 | PTRACE_GETREGSET is not available then set xcr0_features_bits to | |
190 | zero so that the "no-features" descriptions are returned by the | |
191 | switches below. */ | |
0bdb2f78 | 192 | if (have_ptrace_getregset == TRIBOOL_TRUE) |
df7e5265 | 193 | xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK; |
040baaf6 GB |
194 | else |
195 | xcr0_features_bits = 0; | |
196 | ||
197 | if (is_64bit) | |
198 | { | |
199 | #ifdef __x86_64__ | |
9d3d478b | 200 | return amd64_linux_read_description (xcr0_features_bits, is_x32); |
040baaf6 GB |
201 | #endif |
202 | } | |
203 | else | |
204 | { | |
35b4818d YQ |
205 | const struct target_desc * tdesc |
206 | = i386_linux_read_description (xcr0_features_bits); | |
207 | ||
208 | if (tdesc == NULL) | |
209 | tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK); | |
210 | ||
211 | return tdesc; | |
040baaf6 GB |
212 | } |
213 | ||
214 | gdb_assert_not_reached ("failed to return tdesc"); | |
215 | } | |
216 | \f | |
217 | ||
218 | /* Enable branch tracing. */ | |
219 | ||
f6ac5f3d | 220 | struct btrace_target_info * |
696c0d5e | 221 | x86_linux_nat_target::enable_btrace (thread_info *tp, |
f6ac5f3d | 222 | const struct btrace_config *conf) |
040baaf6 | 223 | { |
9ee23a85 | 224 | struct btrace_target_info *tinfo = nullptr; |
696c0d5e | 225 | ptid_t ptid = tp->ptid; |
a70b8144 | 226 | try |
9ee23a85 MM |
227 | { |
228 | tinfo = linux_enable_btrace (ptid, conf); | |
229 | } | |
230d2906 | 230 | catch (const gdb_exception_error &exception) |
9ee23a85 MM |
231 | { |
232 | error (_("Could not enable branch tracing for %s: %s"), | |
3d6e9d23 | 233 | target_pid_to_str (ptid).c_str (), exception.what ()); |
9ee23a85 | 234 | } |
040baaf6 | 235 | |
040baaf6 GB |
236 | return tinfo; |
237 | } | |
238 | ||
239 | /* Disable branch tracing. */ | |
240 | ||
f6ac5f3d PA |
241 | void |
242 | x86_linux_nat_target::disable_btrace (struct btrace_target_info *tinfo) | |
040baaf6 GB |
243 | { |
244 | enum btrace_error errcode = linux_disable_btrace (tinfo); | |
245 | ||
246 | if (errcode != BTRACE_ERR_NONE) | |
247 | error (_("Could not disable branch tracing.")); | |
248 | } | |
249 | ||
250 | /* Teardown branch tracing. */ | |
251 | ||
f6ac5f3d PA |
252 | void |
253 | x86_linux_nat_target::teardown_btrace (struct btrace_target_info *tinfo) | |
040baaf6 GB |
254 | { |
255 | /* Ignore errors. */ | |
256 | linux_disable_btrace (tinfo); | |
257 | } | |
258 | ||
f6ac5f3d PA |
259 | enum btrace_error |
260 | x86_linux_nat_target::read_btrace (struct btrace_data *data, | |
261 | struct btrace_target_info *btinfo, | |
262 | enum btrace_read_type type) | |
040baaf6 GB |
263 | { |
264 | return linux_read_btrace (data, btinfo, type); | |
265 | } | |
f4abbc16 MM |
266 | |
267 | /* See to_btrace_conf in target.h. */ | |
268 | ||
f6ac5f3d PA |
269 | const struct btrace_config * |
270 | x86_linux_nat_target::btrace_conf (const struct btrace_target_info *btinfo) | |
f4abbc16 MM |
271 | { |
272 | return linux_btrace_conf (btinfo); | |
273 | } | |
274 | ||
040baaf6 GB |
275 | \f |
276 | ||
277 | /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to | |
278 | the thread local storage (or its descriptor) and returns PS_OK | |
279 | on success. Returns PS_ERR on failure. */ | |
280 | ||
281 | ps_err_e | |
282 | x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr) | |
283 | { | |
284 | /* NOTE: cagney/2003-08-26: The definition of this buffer is found | |
285 | in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x | |
286 | 4 byte integers in size: `entry_number', `base_addr', `limit', | |
287 | and a bunch of status bits. | |
288 | ||
289 | The values returned by this ptrace call should be part of the | |
290 | regcache buffer, and ps_get_thread_area should channel its | |
291 | request through the regcache. That way remote targets could | |
292 | provide the value using the remote protocol and not this direct | |
293 | call. | |
294 | ||
295 | Is this function needed? I'm guessing that the `base' is the | |
296 | address of a descriptor that libthread_db uses to find the | |
297 | thread local address base that GDB needs. Perhaps that | |
298 | descriptor is defined by the ABI. Anyway, given that | |
299 | libthread_db calls this function without prompting (gdb | |
300 | requesting tls base) I guess it needs info in there anyway. */ | |
301 | unsigned int desc[4]; | |
302 | ||
303 | /* This code assumes that "int" is 32 bits and that | |
304 | GET_THREAD_AREA returns no more than 4 int values. */ | |
305 | gdb_assert (sizeof (int) == 4); | |
306 | ||
307 | #ifndef PTRACE_GET_THREAD_AREA | |
308 | #define PTRACE_GET_THREAD_AREA 25 | |
309 | #endif | |
310 | ||
311 | if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0) | |
312 | return PS_ERR; | |
313 | ||
314 | *base_addr = desc[1]; | |
315 | return PS_OK; | |
316 | } | |
317 | \f | |
318 | ||
6c265988 | 319 | void _initialize_x86_linux_nat (); |
f6ac5f3d | 320 | void |
135340af | 321 | _initialize_x86_linux_nat () |
040baaf6 | 322 | { |
040baaf6 | 323 | /* Initialize the debug register function vectors. */ |
df7e5265 GB |
324 | x86_dr_low.set_control = x86_linux_dr_set_control; |
325 | x86_dr_low.set_addr = x86_linux_dr_set_addr; | |
326 | x86_dr_low.get_addr = x86_linux_dr_get_addr; | |
327 | x86_dr_low.get_status = x86_linux_dr_get_status; | |
328 | x86_dr_low.get_control = x86_linux_dr_get_control; | |
329 | x86_set_debug_register_length (sizeof (void *)); | |
040baaf6 | 330 | } |