More updated translations
[binutils-gdb.git] / gdb / x86-linux-nat.c
blob2afa04f6288da22026671af98e4975f81f27174c
1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "inferior.h"
21 #include "elf/common.h"
22 #include "gdb_proc_service.h"
23 #include "nat/gdb_ptrace.h"
24 #include <sys/user.h>
25 #include <sys/procfs.h>
26 #include <sys/uio.h>
28 #include "x86-nat.h"
29 #ifndef __x86_64__
30 #include "nat/i386-linux.h"
31 #endif
32 #include "x86-linux-nat.h"
33 #include "i386-linux-tdep.h"
34 #ifdef __x86_64__
35 #include "amd64-linux-tdep.h"
36 #endif
37 #include "gdbsupport/x86-xstate.h"
38 #include "nat/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 #include "nat/x86-linux-tdesc.h"
46 /* linux_nat_target::low_new_fork implementation. */
48 void
49 x86_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
51 pid_t parent_pid;
52 struct x86_debug_reg_state *parent_state;
53 struct x86_debug_reg_state *child_state;
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;
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.
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. */
74 parent_pid = parent->ptid.pid ();
75 parent_state = x86_debug_reg_state (parent_pid);
76 child_state = x86_debug_reg_state (child_pid);
77 *child_state = *parent_state;
81 x86_linux_nat_target::~x86_linux_nat_target ()
85 /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
87 void
88 x86_linux_nat_target::post_startup_inferior (ptid_t ptid)
90 x86_cleanup_dregs ();
91 linux_nat_target::post_startup_inferior (ptid);
94 /* Get Linux/x86 target description from running target. */
96 const struct target_desc *
97 x86_linux_nat_target::read_description ()
99 /* The x86_linux_tdesc_for_tid call only reads xcr0 the first time it is
100 called, the xcr0 value is stored here and reused on subsequent calls. */
101 static uint64_t xcr0_storage;
103 if (inferior_ptid == null_ptid)
104 return this->beneath ()->read_description ();
106 int tid = inferior_ptid.pid ();
108 return x86_linux_tdesc_for_tid (tid, &xcr0_storage, &this->m_xsave_layout);
112 /* Enable branch tracing. */
114 struct btrace_target_info *
115 x86_linux_nat_target::enable_btrace (thread_info *tp,
116 const struct btrace_config *conf)
118 struct btrace_target_info *tinfo = nullptr;
119 ptid_t ptid = tp->ptid;
122 tinfo = linux_enable_btrace (ptid, conf);
124 catch (const gdb_exception_error &exception)
126 error (_("Could not enable branch tracing for %s: %s"),
127 target_pid_to_str (ptid).c_str (), exception.what ());
130 return tinfo;
133 /* Disable branch tracing. */
135 void
136 x86_linux_nat_target::disable_btrace (struct btrace_target_info *tinfo)
138 enum btrace_error errcode = linux_disable_btrace (tinfo);
140 if (errcode != BTRACE_ERR_NONE)
141 error (_("Could not disable branch tracing."));
144 /* Teardown branch tracing. */
146 void
147 x86_linux_nat_target::teardown_btrace (struct btrace_target_info *tinfo)
149 /* Ignore errors. */
150 linux_disable_btrace (tinfo);
153 enum btrace_error
154 x86_linux_nat_target::read_btrace (struct btrace_data *data,
155 struct btrace_target_info *btinfo,
156 enum btrace_read_type type)
158 return linux_read_btrace (data, btinfo, type);
161 /* See to_btrace_conf in target.h. */
163 const struct btrace_config *
164 x86_linux_nat_target::btrace_conf (const struct btrace_target_info *btinfo)
166 return linux_btrace_conf (btinfo);
171 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
172 the thread local storage (or its descriptor) and returns PS_OK
173 on success. Returns PS_ERR on failure. */
175 ps_err_e
176 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
178 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
179 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
180 4 byte integers in size: `entry_number', `base_addr', `limit',
181 and a bunch of status bits.
183 The values returned by this ptrace call should be part of the
184 regcache buffer, and ps_get_thread_area should channel its
185 request through the regcache. That way remote targets could
186 provide the value using the remote protocol and not this direct
187 call.
189 Is this function needed? I'm guessing that the `base' is the
190 address of a descriptor that libthread_db uses to find the
191 thread local address base that GDB needs. Perhaps that
192 descriptor is defined by the ABI. Anyway, given that
193 libthread_db calls this function without prompting (gdb
194 requesting tls base) I guess it needs info in there anyway. */
195 unsigned int desc[4];
197 /* This code assumes that "int" is 32 bits and that
198 GET_THREAD_AREA returns no more than 4 int values. */
199 gdb_assert (sizeof (int) == 4);
201 #ifndef PTRACE_GET_THREAD_AREA
202 #define PTRACE_GET_THREAD_AREA 25
203 #endif
205 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
206 return PS_ERR;
208 *base_addr = desc[1];
209 return PS_OK;
213 void _initialize_x86_linux_nat ();
214 void
215 _initialize_x86_linux_nat ()
217 /* Initialize the debug register function vectors. */
218 x86_dr_low.set_control = x86_linux_dr_set_control;
219 x86_dr_low.set_addr = x86_linux_dr_set_addr;
220 x86_dr_low.get_addr = x86_linux_dr_get_addr;
221 x86_dr_low.get_status = x86_linux_dr_get_status;
222 x86_dr_low.get_control = x86_linux_dr_get_control;
223 x86_set_debug_register_length (sizeof (void *));