Convert struct target_ops to C++
[deliverable/binutils-gdb.git] / gdb / x86-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2
3 Copyright (C) 1999-2018 Free Software Foundation, Inc.
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"
24 #include "nat/gdb_ptrace.h"
25 #include <sys/user.h>
26 #include <sys/procfs.h>
27 #include <sys/uio.h>
28
29 #include "x86-nat.h"
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
38 #include "x86-xstate.h"
39 #include "nat/linux-btrace.h"
40 #include "nat/linux-nat.h"
41 #include "nat/x86-linux.h"
42 #include "nat/x86-linux-dregs.h"
43 #include "nat/linux-ptrace.h"
44
45 /* linux_nat_new_fork hook. */
46
47 static void
48 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
49 {
50 pid_t parent_pid;
51 struct x86_debug_reg_state *parent_state;
52 struct x86_debug_reg_state *child_state;
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
73 parent_pid = ptid_get_pid (parent->ptid);
74 parent_state = x86_debug_reg_state (parent_pid);
75 child_state = x86_debug_reg_state (child_pid);
76 *child_state = *parent_state;
77 }
78 \f
79
80 x86_linux_nat_target::~x86_linux_nat_target ()
81 {
82 }
83
84 void
85 x86_linux_nat_target::post_startup_inferior (ptid_t ptid)
86 {
87 x86_cleanup_dregs ();
88 linux_nat_target::post_startup_inferior (ptid);
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
105 const struct target_desc *
106 x86_linux_nat_target::read_description ()
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
116 /* GNU/Linux LWP ID's are process ID's. */
117 tid = ptid_get_lwp (inferior_ptid);
118 if (tid == 0)
119 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
120
121 #ifdef __x86_64__
122 {
123 unsigned long cs;
124 unsigned long ds;
125
126 /* Get CS register. */
127 errno = 0;
128 cs = ptrace (PTRACE_PEEKUSER, tid,
129 offsetof (struct user_regs_struct, cs), 0);
130 if (errno != 0)
131 perror_with_name (_("Couldn't get CS register"));
132
133 is_64bit = cs == AMD64_LINUX_USER64_CS;
134
135 /* Get DS register. */
136 errno = 0;
137 ds = ptrace (PTRACE_PEEKUSER, tid,
138 offsetof (struct user_regs_struct, ds), 0);
139 if (errno != 0)
140 perror_with_name (_("Couldn't get DS register"));
141
142 is_x32 = ds == AMD64_LINUX_X32_DS;
143
144 if (sizeof (void *) == 4 && is_64bit && !is_x32)
145 error (_("Can't debug 64-bit process with 32-bit GDB"));
146 }
147 #elif HAVE_PTRACE_GETFPXREGS
148 if (have_ptrace_getfpxregs == -1)
149 {
150 elf_fpxregset_t fpxregs;
151
152 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
153 {
154 have_ptrace_getfpxregs = 0;
155 have_ptrace_getregset = TRIBOOL_FALSE;
156 return i386_linux_read_description (X86_XSTATE_X87_MASK);
157 }
158 }
159 #endif
160
161 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
162 {
163 uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
164 struct iovec iov;
165
166 iov.iov_base = xstateregs;
167 iov.iov_len = sizeof (xstateregs);
168
169 /* Check if PTRACE_GETREGSET works. */
170 if (ptrace (PTRACE_GETREGSET, tid,
171 (unsigned int) NT_X86_XSTATE, &iov) < 0)
172 have_ptrace_getregset = TRIBOOL_FALSE;
173 else
174 {
175 have_ptrace_getregset = TRIBOOL_TRUE;
176
177 /* Get XCR0 from XSAVE extended state. */
178 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
179 / sizeof (uint64_t))];
180 }
181 }
182
183 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
184 PTRACE_GETREGSET is not available then set xcr0_features_bits to
185 zero so that the "no-features" descriptions are returned by the
186 switches below. */
187 if (have_ptrace_getregset == TRIBOOL_TRUE)
188 xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
189 else
190 xcr0_features_bits = 0;
191
192 if (is_64bit)
193 {
194 #ifdef __x86_64__
195 return amd64_linux_read_description (xcr0_features_bits, is_x32);
196 #endif
197 }
198 else
199 {
200 const struct target_desc * tdesc
201 = i386_linux_read_description (xcr0_features_bits);
202
203 if (tdesc == NULL)
204 tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK);
205
206 return tdesc;
207 }
208
209 gdb_assert_not_reached ("failed to return tdesc");
210 }
211 \f
212
213 /* Enable branch tracing. */
214
215 struct btrace_target_info *
216 x86_linux_nat_target::enable_btrace (ptid_t ptid,
217 const struct btrace_config *conf)
218 {
219 struct btrace_target_info *tinfo = nullptr;
220 TRY
221 {
222 tinfo = linux_enable_btrace (ptid, conf);
223 }
224 CATCH (exception, RETURN_MASK_ERROR)
225 {
226 error (_("Could not enable branch tracing for %s: %s"),
227 target_pid_to_str (ptid), exception.message);
228 }
229 END_CATCH
230
231 return tinfo;
232 }
233
234 /* Disable branch tracing. */
235
236 void
237 x86_linux_nat_target::disable_btrace (struct btrace_target_info *tinfo)
238 {
239 enum btrace_error errcode = linux_disable_btrace (tinfo);
240
241 if (errcode != BTRACE_ERR_NONE)
242 error (_("Could not disable branch tracing."));
243 }
244
245 /* Teardown branch tracing. */
246
247 void
248 x86_linux_nat_target::teardown_btrace (struct btrace_target_info *tinfo)
249 {
250 /* Ignore errors. */
251 linux_disable_btrace (tinfo);
252 }
253
254 enum btrace_error
255 x86_linux_nat_target::read_btrace (struct btrace_data *data,
256 struct btrace_target_info *btinfo,
257 enum btrace_read_type type)
258 {
259 return linux_read_btrace (data, btinfo, type);
260 }
261
262 /* See to_btrace_conf in target.h. */
263
264 const struct btrace_config *
265 x86_linux_nat_target::btrace_conf (const struct btrace_target_info *btinfo)
266 {
267 return linux_btrace_conf (btinfo);
268 }
269
270 \f
271
272 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
273 the thread local storage (or its descriptor) and returns PS_OK
274 on success. Returns PS_ERR on failure. */
275
276 ps_err_e
277 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
278 {
279 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
280 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
281 4 byte integers in size: `entry_number', `base_addr', `limit',
282 and a bunch of status bits.
283
284 The values returned by this ptrace call should be part of the
285 regcache buffer, and ps_get_thread_area should channel its
286 request through the regcache. That way remote targets could
287 provide the value using the remote protocol and not this direct
288 call.
289
290 Is this function needed? I'm guessing that the `base' is the
291 address of a descriptor that libthread_db uses to find the
292 thread local address base that GDB needs. Perhaps that
293 descriptor is defined by the ABI. Anyway, given that
294 libthread_db calls this function without prompting (gdb
295 requesting tls base) I guess it needs info in there anyway. */
296 unsigned int desc[4];
297
298 /* This code assumes that "int" is 32 bits and that
299 GET_THREAD_AREA returns no more than 4 int values. */
300 gdb_assert (sizeof (int) == 4);
301
302 #ifndef PTRACE_GET_THREAD_AREA
303 #define PTRACE_GET_THREAD_AREA 25
304 #endif
305
306 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
307 return PS_ERR;
308
309 *base_addr = desc[1];
310 return PS_OK;
311 }
312 \f
313
314 /* Add an x86 GNU/Linux target. */
315
316 void
317 x86_linux_add_target (linux_nat_target *t)
318 {
319 /* Initialize the debug register function vectors. */
320 x86_dr_low.set_control = x86_linux_dr_set_control;
321 x86_dr_low.set_addr = x86_linux_dr_set_addr;
322 x86_dr_low.get_addr = x86_linux_dr_get_addr;
323 x86_dr_low.get_status = x86_linux_dr_get_status;
324 x86_dr_low.get_control = x86_linux_dr_get_control;
325 x86_set_debug_register_length (sizeof (void *));
326
327 add_target (t);
328 linux_nat_set_new_thread (t, x86_linux_new_thread);
329 linux_nat_set_delete_thread (t, x86_linux_delete_thread);
330 linux_nat_set_new_fork (t, x86_linux_new_fork);
331 linux_nat_set_forget_process (t, x86_forget_process);
332 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
333 }
This page took 0.046793 seconds and 5 git commands to generate.