GDB (xrefs)
Loading...
Searching...
No Matches
x86-bsd-nat.c
Go to the documentation of this file.
1/* Native-dependent code for X86 BSD's.
2
3 Copyright (C) 2003-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 "gdbthread.h"
23
24/* We include <signal.h> to make sure `struct fxsave64' is defined on
25 NetBSD, since NetBSD's <machine/reg.h> needs it. */
26#include <signal.h>
27#include <sys/types.h>
28#include <sys/ptrace.h>
29#include <machine/reg.h>
30
31#include "x86-nat.h"
32#include "x86-bsd-nat.h"
33#include "inf-ptrace.h"
34
35
36/* Support for debug registers. */
37
38#ifdef HAVE_PT_GETDBREGS
39
40static PTRACE_TYPE_RET
41gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr)
42{
43#ifdef __NetBSD__
44 /* Support for NetBSD threads: unlike other ptrace implementations in this
45 file, NetBSD requires that we pass both the pid and lwp. */
46 return ptrace (request, ptid.pid (), addr, ptid.lwp ());
47#else
48 pid_t pid = get_ptrace_pid (ptid);
49 return ptrace (request, pid, addr, 0);
50#endif
51}
52
53/* Helper macro to access debug register X. FreeBSD/amd64 and modern
54 versions of FreeBSD/i386 provide this macro in system headers. Define
55 a local version for systems that do not provide it. */
56#ifndef DBREG_DRX
57#ifdef __NetBSD__
58#define DBREG_DRX(d, x) ((d)->dr[x])
59#else
60#define DBREG_DRX(d, x) ((&d->dr0)[x])
61#endif
62#endif
63
64static unsigned long
65x86bsd_dr_get (ptid_t ptid, int regnum)
66{
67 struct dbreg dbregs;
68
69 if (gdb_ptrace (PT_GETDBREGS, ptid, (PTRACE_TYPE_ARG3) &dbregs) == -1)
70 perror_with_name (_("Couldn't read debug registers"));
71
72 return DBREG_DRX ((&dbregs), regnum);
73}
74
75static void
76x86bsd_dr_set (ptid_t ptid, int regnum, unsigned long value)
77{
78 struct dbreg dbregs;
79
80 if (gdb_ptrace (PT_GETDBREGS, ptid, (PTRACE_TYPE_ARG3) &dbregs) == -1)
81 perror_with_name (_("Couldn't get debug registers"));
82
83 /* For some mysterious reason, some of the reserved bits in the
84 debug control register get set. Mask these off, otherwise the
85 ptrace call below will fail. */
86 DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00);
87
88 DBREG_DRX ((&dbregs), regnum) = value;
89
90 for (thread_info *thread : current_inferior ()->non_exited_threads ())
91 {
92 if (gdb_ptrace (PT_SETDBREGS, thread->ptid,
93 (PTRACE_TYPE_ARG3) &dbregs) == -1)
94 perror_with_name (_("Couldn't write debug registers"));
95 }
96}
97
98static void
99x86bsd_dr_set_control (unsigned long control)
100{
101 x86bsd_dr_set (inferior_ptid, 7, control);
102}
103
104static void
105x86bsd_dr_set_addr (int regnum, CORE_ADDR addr)
106{
107 gdb_assert (regnum >= 0 && regnum <= 4);
108
109 x86bsd_dr_set (inferior_ptid, regnum, addr);
110}
111
112static CORE_ADDR
113x86bsd_dr_get_addr (int regnum)
114{
115 return x86bsd_dr_get (inferior_ptid, regnum);
116}
117
118static unsigned long
119x86bsd_dr_get_status (void)
120{
121 return x86bsd_dr_get (inferior_ptid, 6);
122}
123
124static unsigned long
125x86bsd_dr_get_control (void)
126{
127 return x86bsd_dr_get (inferior_ptid, 7);
128}
129
130#endif /* PT_GETDBREGS */
131
133void
135{
136#ifdef HAVE_PT_GETDBREGS
137 x86_dr_low.set_control = x86bsd_dr_set_control;
138 x86_dr_low.set_addr = x86bsd_dr_set_addr;
139 x86_dr_low.get_addr = x86bsd_dr_get_addr;
140 x86_dr_low.get_status = x86bsd_dr_get_status;
141 x86_dr_low.get_control = x86bsd_dr_get_control;
142 x86_set_debug_register_length (sizeof (void *));
143#endif /* HAVE_PT_GETDBREGS */
144}
int regnum
static PTRACE_TYPE_RET gdb_ptrace(PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr, PTRACE_TYPE_ARG4 data)
#define PTRACE_TYPE_RET
Definition config.h:675
#define PTRACE_TYPE_ARG1
Definition config.h:663
#define ptrace(request, pid, addr, data)
Definition gdb_ptrace.h:141
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1791
pid_t get_ptrace_pid(ptid_t ptid)
Definition inf-ptrace.c:238
ptid_t inferior_ptid
Definition infcmd.c:74
struct inferior * current_inferior(void)
Definition inferior.c:55
#define PTRACE_TYPE_ARG3
int value
Definition py-param.c:79
Definition value.h:130
void(* set_addr)(int, CORE_ADDR)
Definition x86-dregs.h:49
unsigned long(* get_control)(void)
Definition x86-dregs.h:61
unsigned long(* get_status)(void)
Definition x86-dregs.h:57
void(* set_control)(unsigned long)
Definition x86-dregs.h:45
CORE_ADDR(* get_addr)(int)
Definition x86-dregs.h:53
void _initialize_x86_bsd_nat()
struct x86_dr_low_type x86_dr_low
Definition x86-nat.c:39
void x86_set_debug_register_length(int len)
Definition x86-nat.c:236