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