packaging: Add python3-base dependency
[platform/upstream/gdb.git] / gdb / x86-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2
3    Copyright (C) 1999-2023 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 "gdbsupport/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_target::low_new_fork implementation.  */
46
47 void
48 x86_linux_nat_target::low_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 = parent->ptid.pid ();
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 /* Implement the virtual inf_ptrace_target::post_startup_inferior method.  */
85
86 void
87 x86_linux_nat_target::post_startup_inferior (ptid_t ptid)
88 {
89   x86_cleanup_dregs ();
90   linux_nat_target::post_startup_inferior (ptid);
91 }
92
93 #ifdef __x86_64__
94 /* Value of CS segment register:
95      64bit process: 0x33
96      32bit process: 0x23  */
97 #define AMD64_LINUX_USER64_CS 0x33
98
99 /* Value of DS segment register:
100      LP64 process: 0x0
101      X32 process: 0x2b  */
102 #define AMD64_LINUX_X32_DS 0x2b
103 #endif
104
105 /* Get Linux/x86 target description from running target.  */
106
107 const struct target_desc *
108 x86_linux_nat_target::read_description ()
109 {
110   int tid;
111   int is_64bit = 0;
112 #ifdef __x86_64__
113   int is_x32;
114 #endif
115   static uint64_t xcr0;
116   uint64_t xcr0_features_bits;
117
118   tid = inferior_ptid.pid ();
119
120 #ifdef __x86_64__
121   {
122     unsigned long cs;
123     unsigned long ds;
124
125     /* Get CS register.  */
126     errno = 0;
127     cs = ptrace (PTRACE_PEEKUSER, tid,
128                  offsetof (struct user_regs_struct, cs), 0);
129     if (errno != 0)
130       perror_with_name (_("Couldn't get CS register"));
131
132     is_64bit = cs == AMD64_LINUX_USER64_CS;
133
134     /* Get DS register.  */
135     errno = 0;
136     ds = ptrace (PTRACE_PEEKUSER, tid,
137                  offsetof (struct user_regs_struct, ds), 0);
138     if (errno != 0)
139       perror_with_name (_("Couldn't get DS register"));
140
141     is_x32 = ds == AMD64_LINUX_X32_DS;
142
143     if (sizeof (void *) == 4 && is_64bit && !is_x32)
144       error (_("Can't debug 64-bit process with 32-bit GDB"));
145   }
146 #elif HAVE_PTRACE_GETFPXREGS
147   if (have_ptrace_getfpxregs == -1)
148     {
149       elf_fpxregset_t fpxregs;
150
151       if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
152         {
153           have_ptrace_getfpxregs = 0;
154           have_ptrace_getregset = TRIBOOL_FALSE;
155           return i386_linux_read_description (X86_XSTATE_X87_MASK);
156         }
157     }
158 #endif
159
160   if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
161     {
162       uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
163       struct iovec iov;
164
165       iov.iov_base = xstateregs;
166       iov.iov_len = sizeof (xstateregs);
167
168       /* Check if PTRACE_GETREGSET works.  */
169       if (ptrace (PTRACE_GETREGSET, tid,
170                   (unsigned int) NT_X86_XSTATE, &iov) < 0)
171         have_ptrace_getregset = TRIBOOL_FALSE;
172       else
173         {
174           have_ptrace_getregset = TRIBOOL_TRUE;
175
176           /* Get XCR0 from XSAVE extended state.  */
177           xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
178                              / sizeof (uint64_t))];
179         }
180     }
181
182   /* Check the native XCR0 only if PTRACE_GETREGSET is available.  If
183      PTRACE_GETREGSET is not available then set xcr0_features_bits to
184      zero so that the "no-features" descriptions are returned by the
185      switches below.  */
186   if (have_ptrace_getregset == TRIBOOL_TRUE)
187     xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
188   else
189     xcr0_features_bits = 0;
190
191   if (is_64bit)
192     {
193 #ifdef __x86_64__
194       return amd64_linux_read_description (xcr0_features_bits, is_x32);
195 #endif
196     }
197   else
198     {
199       const struct target_desc * tdesc
200         = i386_linux_read_description (xcr0_features_bits);
201
202       if (tdesc == NULL)
203         tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK);
204
205       return tdesc;
206     }
207
208   gdb_assert_not_reached ("failed to return tdesc");
209 }
210 \f
211
212 /* Enable branch tracing.  */
213
214 struct btrace_target_info *
215 x86_linux_nat_target::enable_btrace (thread_info *tp,
216                                      const struct btrace_config *conf)
217 {
218   struct btrace_target_info *tinfo = nullptr;
219   ptid_t ptid = tp->ptid;
220   try
221     {
222       tinfo = linux_enable_btrace (ptid, conf);
223     }
224   catch (const gdb_exception_error &exception)
225     {
226       error (_("Could not enable branch tracing for %s: %s"),
227              target_pid_to_str (ptid).c_str (), exception.what ());
228     }
229
230   return tinfo;
231 }
232
233 /* Disable branch tracing.  */
234
235 void
236 x86_linux_nat_target::disable_btrace (struct btrace_target_info *tinfo)
237 {
238   enum btrace_error errcode = linux_disable_btrace (tinfo);
239
240   if (errcode != BTRACE_ERR_NONE)
241     error (_("Could not disable branch tracing."));
242 }
243
244 /* Teardown branch tracing.  */
245
246 void
247 x86_linux_nat_target::teardown_btrace (struct btrace_target_info *tinfo)
248 {
249   /* Ignore errors.  */
250   linux_disable_btrace (tinfo);
251 }
252
253 enum btrace_error
254 x86_linux_nat_target::read_btrace (struct btrace_data *data,
255                                    struct btrace_target_info *btinfo,
256                                    enum btrace_read_type type)
257 {
258   return linux_read_btrace (data, btinfo, type);
259 }
260
261 /* See to_btrace_conf in target.h.  */
262
263 const struct btrace_config *
264 x86_linux_nat_target::btrace_conf (const struct btrace_target_info *btinfo)
265 {
266   return linux_btrace_conf (btinfo);
267 }
268
269 \f
270
271 /* Helper for ps_get_thread_area.  Sets BASE_ADDR to a pointer to
272    the thread local storage (or its descriptor) and returns PS_OK
273    on success.  Returns PS_ERR on failure.  */
274
275 ps_err_e
276 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
277 {
278   /* NOTE: cagney/2003-08-26: The definition of this buffer is found
279      in the kernel header <asm-i386/ldt.h>.  It, after padding, is 4 x
280      4 byte integers in size: `entry_number', `base_addr', `limit',
281      and a bunch of status bits.
282
283      The values returned by this ptrace call should be part of the
284      regcache buffer, and ps_get_thread_area should channel its
285      request through the regcache.  That way remote targets could
286      provide the value using the remote protocol and not this direct
287      call.
288
289      Is this function needed?  I'm guessing that the `base' is the
290      address of a descriptor that libthread_db uses to find the
291      thread local address base that GDB needs.  Perhaps that
292      descriptor is defined by the ABI.  Anyway, given that
293      libthread_db calls this function without prompting (gdb
294      requesting tls base) I guess it needs info in there anyway.  */
295   unsigned int desc[4];
296
297   /* This code assumes that "int" is 32 bits and that
298      GET_THREAD_AREA returns no more than 4 int values.  */
299   gdb_assert (sizeof (int) == 4);
300
301 #ifndef PTRACE_GET_THREAD_AREA
302 #define PTRACE_GET_THREAD_AREA 25
303 #endif
304
305   if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
306     return PS_ERR;
307
308   *base_addr = desc[1];
309   return PS_OK;
310 }
311 \f
312
313 void _initialize_x86_linux_nat ();
314 void
315 _initialize_x86_linux_nat ()
316 {
317   /* Initialize the debug register function vectors.  */
318   x86_dr_low.set_control = x86_linux_dr_set_control;
319   x86_dr_low.set_addr = x86_linux_dr_set_addr;
320   x86_dr_low.get_addr = x86_linux_dr_get_addr;
321   x86_dr_low.get_status = x86_linux_dr_get_status;
322   x86_dr_low.get_control = x86_linux_dr_get_control;
323   x86_set_debug_register_length (sizeof (void *));
324 }