GDB (xrefs)

/tmp/gdb-8.1/gdb/x86-bsd-nat.c
Go to the documentation of this file.
1 /* Native-dependent code for X86 BSD's.
2
3 Copyright (C) 2003-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 "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 #ifdef PT_GETXSTATE_INFO
37 size_t x86bsd_xsave_len;
38 #endif
39
40 /* Support for debug registers. */
41
42 #ifdef HAVE_PT_GETDBREGS
43 static void (*super_mourn_inferior) (struct target_ops *ops);
44
45 /* Implement the "to_mourn_inferior" target_ops method. */
46
47 static void
48 x86bsd_mourn_inferior (struct target_ops *ops)
49 {
50 x86_cleanup_dregs ();
51 super_mourn_inferior (ops);
52 }
53
54 /* Helper macro to access debug register X. FreeBSD/amd64 and modern
55 versions of FreeBSD/i386 provide this macro in system headers. Define
56 a local version for systems that do not provide it. */
57 #ifndef DBREG_DRX
58 #ifdef __NetBSD__
59 #define DBREG_DRX(d, x) ((d)->dr[x])
60 #else
61 #define DBREG_DRX(d, x) ((&d->dr0)[x])
62 #endif
63 #endif
64
65 static unsigned long
66 x86bsd_dr_get (ptid_t ptid, int regnum)
67 {
68 struct dbreg dbregs;
69
70 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
71 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
72 perror_with_name (_("Couldn't read debug registers"));
73
74 return DBREG_DRX ((&dbregs), regnum);
75 }
76
77 static void
78 x86bsd_dr_set (int regnum, unsigned long value)
79 {
80 struct thread_info *thread;
81 struct dbreg dbregs;
82
83 if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid),
84 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
85 perror_with_name (_("Couldn't get debug registers"));
86
87 /* For some mysterious reason, some of the reserved bits in the
88 debug control register get set. Mask these off, otherwise the
89 ptrace call below will fail. */
90 DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00);
91
92 DBREG_DRX ((&dbregs), regnum) = value;
93
94 ALL_NON_EXITED_THREADS (thread)
95 if (thread->inf == current_inferior ())
96 {
97 if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid),
98 (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
99 perror_with_name (_("Couldn't write debug registers"));
100 }
101 }
102
103 static void
104 x86bsd_dr_set_control (unsigned long control)
105 {
106 x86bsd_dr_set (7, control);
107 }
108
109 static void
110 x86bsd_dr_set_addr (int regnum, CORE_ADDR addr)
111 {
112 gdb_assert (regnum >= 0 && regnum <= 4);
113
114 x86bsd_dr_set (regnum, addr);
115 }
116
117 static CORE_ADDR
118 x86bsd_dr_get_addr (int regnum)
119 {
120 return x86bsd_dr_get (inferior_ptid, regnum);
121 }
122
123 static unsigned long
124 x86bsd_dr_get_status (void)
125 {
126 return x86bsd_dr_get (inferior_ptid, 6);
127 }
128
129 static unsigned long
130 x86bsd_dr_get_control (void)
131 {
132 return x86bsd_dr_get (inferior_ptid, 7);
133 }
134
135 #endif /* PT_GETDBREGS */
136
137 /* Create a prototype *BSD/x86 target. The client can override it
138 with local methods. */
139
140 struct target_ops *
141 x86bsd_target (void)
142 {
143 struct target_ops *t;
144
145 t = inf_ptrace_target ();
146
147 #ifdef HAVE_PT_GETDBREGS
148 x86_use_watchpoints (t);
149
150 x86_dr_low.set_control = x86bsd_dr_set_control;
151 x86_dr_low.set_addr = x86bsd_dr_set_addr;
152 x86_dr_low.get_addr = x86bsd_dr_get_addr;
153 x86_dr_low.get_status = x86bsd_dr_get_status;
154 x86_dr_low.get_control = x86bsd_dr_get_control;
155 x86_set_debug_register_length (sizeof (void *));
156 super_mourn_inferior = t->to_mourn_inferior;
157 t->to_mourn_inferior = x86bsd_mourn_inferior;
158 #endif /* HAVE_PT_GETDBREGS */
159
160 return t;
161 }
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
_
#define _(String)
Definition: gdb_locale.h:35
x86_dr_low_type::set_control
void(* set_control)(unsigned long)
Definition: x86-dregs.h:44
inf_ptrace_target
struct target_ops * inf_ptrace_target(void)
Definition: inf-ptrace.c:676
defs.h
x86_dr_low
struct x86_dr_low_type x86_dr_low
Definition: x86-nat.c:37
x86-bsd-nat.h
x86_dr_low_type::get_addr
CORE_ADDR(* get_addr)(int)
Definition: x86-dregs.h:52
ptrace
PTRACE_TYPE_RET ptrace()
x86bsd_target
struct target_ops * x86bsd_target(void)
Definition: x86-bsd-nat.c:141
x86_dr_low_type::set_addr
void(* set_addr)(int, CORE_ADDR)
Definition: x86-dregs.h:48
inf-ptrace.h
x86_dr_low_type::get_status
unsigned long(* get_status)(void)
Definition: x86-dregs.h:56
target_ops
Definition: target.h:412
ptid_t
Definition: ptid.h:35
gdbthread.h
inferior.h
x86-nat.h
thread_info::ptid
ptid_t ptid
Definition: gdbthread.h:219
regnum
int regnum
Definition: aarch64-tdep.c:77
x86_dr_low_type::get_control
unsigned long(* get_control)(void)
Definition: x86-dregs.h:60
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
value
Definition: value.c:169
target_ops::to_mourn_inferior
void(* to_mourn_inferior)(struct target_ops *) TARGET_DEFAULT_FUNC(default_mourn_inferior)
Definition: target.h:606
perror_with_name
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:692
ALL_NON_EXITED_THREADS
#define ALL_NON_EXITED_THREADS(T)
Definition: gdbthread.h:490
inferior_ptid
ptid_t inferior_ptid
Definition: infcmd.c:94
x86bsd_xsave_len
size_t x86bsd_xsave_len
thread_info::inf
struct inferior * inf
Definition: gdbthread.h:261
current_inferior
struct inferior * current_inferior(void)
Definition: inferior.c:58
x86_set_debug_register_length
void x86_set_debug_register_length(int len)
Definition: x86-nat.c:303
PTRACE_TYPE_ARG3
#define PTRACE_TYPE_ARG3
Definition: config.h:642
x86_use_watchpoints
void x86_use_watchpoints(struct target_ops *t)
Definition: x86-nat.c:285
get_ptrace_pid
pid_t get_ptrace_pid(ptid_t ptid)
Definition: inf-ptrace.c:320
x86_cleanup_dregs
void x86_cleanup_dregs(void)
Definition: x86-nat.c:139
thread_info
Definition: gdbthread.h:205