Commit | Line | Data |
---|---|---|
a3405d12 JB |
1 | /* Native-dependent code for X86 BSD's. |
2 | ||
61baf725 | 3 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
a3405d12 JB |
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" | |
5077bfff | 22 | #include "gdbthread.h" |
a3405d12 JB |
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" | |
03b62bbb | 32 | #include "x86-bsd-nat.h" |
a3405d12 JB |
33 | #include "inf-ptrace.h" |
34 | \f | |
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 | /* Not all versions of FreeBSD/i386 that support the debug registers | |
55 | have this macro. */ | |
56 | #ifndef DBREG_DRX | |
57 | #define DBREG_DRX(d, x) ((&d->dr0)[x]) | |
58 | #endif | |
59 | ||
60 | static unsigned long | |
61 | x86bsd_dr_get (ptid_t ptid, int regnum) | |
62 | { | |
63 | struct dbreg dbregs; | |
64 | ||
65 | if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid), | |
66 | (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) | |
67 | perror_with_name (_("Couldn't read debug registers")); | |
68 | ||
69 | return DBREG_DRX ((&dbregs), regnum); | |
70 | } | |
71 | ||
72 | static void | |
73 | x86bsd_dr_set (int regnum, unsigned long value) | |
74 | { | |
5077bfff | 75 | struct thread_info *thread; |
a3405d12 JB |
76 | struct dbreg dbregs; |
77 | ||
78 | if (ptrace (PT_GETDBREGS, get_ptrace_pid (inferior_ptid), | |
79 | (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) | |
80 | perror_with_name (_("Couldn't get debug registers")); | |
81 | ||
82 | /* For some mysterious reason, some of the reserved bits in the | |
83 | debug control register get set. Mask these off, otherwise the | |
84 | ptrace call below will fail. */ | |
85 | DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00); | |
86 | ||
87 | DBREG_DRX ((&dbregs), regnum) = value; | |
88 | ||
5077bfff JB |
89 | ALL_NON_EXITED_THREADS (thread) |
90 | if (thread->inf == current_inferior ()) | |
91 | { | |
92 | if (ptrace (PT_SETDBREGS, get_ptrace_pid (thread->ptid), | |
93 | (PTRACE_TYPE_ARG3) &dbregs, 0) == -1) | |
94 | perror_with_name (_("Couldn't write debug registers")); | |
95 | } | |
a3405d12 JB |
96 | } |
97 | ||
98 | static void | |
99 | x86bsd_dr_set_control (unsigned long control) | |
100 | { | |
101 | x86bsd_dr_set (7, control); | |
102 | } | |
103 | ||
104 | static void | |
105 | x86bsd_dr_set_addr (int regnum, CORE_ADDR addr) | |
106 | { | |
107 | gdb_assert (regnum >= 0 && regnum <= 4); | |
108 | ||
109 | x86bsd_dr_set (regnum, addr); | |
110 | } | |
111 | ||
112 | static CORE_ADDR | |
113 | x86bsd_dr_get_addr (int regnum) | |
114 | { | |
115 | return x86bsd_dr_get (inferior_ptid, regnum); | |
116 | } | |
117 | ||
118 | static unsigned long | |
119 | x86bsd_dr_get_status (void) | |
120 | { | |
121 | return x86bsd_dr_get (inferior_ptid, 6); | |
122 | } | |
123 | ||
124 | static unsigned long | |
125 | x86bsd_dr_get_control (void) | |
126 | { | |
127 | return x86bsd_dr_get (inferior_ptid, 7); | |
128 | } | |
129 | ||
130 | #endif /* PT_GETDBREGS */ | |
131 | ||
132 | /* Create a prototype *BSD/x86 target. The client can override it | |
133 | with local methods. */ | |
134 | ||
135 | struct target_ops * | |
136 | x86bsd_target (void) | |
137 | { | |
138 | struct target_ops *t; | |
139 | ||
140 | t = inf_ptrace_target (); | |
141 | ||
142 | #ifdef HAVE_PT_GETDBREGS | |
143 | x86_use_watchpoints (t); | |
144 | ||
145 | x86_dr_low.set_control = x86bsd_dr_set_control; | |
146 | x86_dr_low.set_addr = x86bsd_dr_set_addr; | |
147 | x86_dr_low.get_addr = x86bsd_dr_get_addr; | |
148 | x86_dr_low.get_status = x86bsd_dr_get_status; | |
149 | x86_dr_low.get_control = x86bsd_dr_get_control; | |
150 | x86_set_debug_register_length (sizeof (void *)); | |
151 | super_mourn_inferior = t->to_mourn_inferior; | |
152 | t->to_mourn_inferior = x86bsd_mourn_inferior; | |
153 | #endif /* HAVE_PT_GETDBREGS */ | |
154 | ||
155 | return t; | |
156 | } |