Read stop_pc after updating the gdbarch when exec'ing
[external/binutils.git] / gdb / amd64-bsd-nat.c
1 /* Native-dependent code for AMD64 BSD's.
2
3    Copyright (C) 2003-2017 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 "regcache.h"
23 #include "target.h"
24
25 /* We include <signal.h> to make sure `struct fxsave64' is defined on
26    NetBSD, since NetBSD's <machine/reg.h> needs it.  */
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <machine/reg.h>
31
32 #include "amd64-tdep.h"
33 #include "amd64-nat.h"
34 #include "x86-bsd-nat.h"
35 #include "inf-ptrace.h"
36 \f
37
38 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
39    for all registers (including the floating-point registers).  */
40
41 static void
42 amd64bsd_fetch_inferior_registers (struct target_ops *ops,
43                                    struct regcache *regcache, int regnum)
44 {
45   struct gdbarch *gdbarch = get_regcache_arch (regcache);
46   pid_t pid = get_ptrace_pid (regcache_get_ptid (regcache));
47
48   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
49     {
50       struct reg regs;
51
52       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
53         perror_with_name (_("Couldn't get registers"));
54
55       amd64_supply_native_gregset (regcache, &regs, -1);
56       if (regnum != -1)
57         return;
58     }
59
60 #ifdef PT_GETFSBASE
61   if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
62     {
63       register_t base;
64
65       if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
66         perror_with_name (_("Couldn't get segment register fs_base"));
67
68       regcache_raw_supply (regcache, AMD64_FSBASE_REGNUM, &base);
69       if (regnum != -1)
70         return;
71     }
72 #endif
73 #ifdef PT_GETGSBASE
74   if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
75     {
76       register_t base;
77
78       if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
79         perror_with_name (_("Couldn't get segment register gs_base"));
80
81       regcache_raw_supply (regcache, AMD64_GSBASE_REGNUM, &base);
82       if (regnum != -1)
83         return;
84     }
85 #endif
86
87   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
88     {
89       struct fpreg fpregs;
90 #ifdef PT_GETXSTATE_INFO
91       void *xstateregs;
92
93       if (x86bsd_xsave_len != 0)
94         {
95           xstateregs = alloca (x86bsd_xsave_len);
96           if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0)
97               == -1)
98             perror_with_name (_("Couldn't get extended state status"));
99
100           amd64_supply_xsave (regcache, -1, xstateregs);
101           return;
102         }
103 #endif
104
105       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
106         perror_with_name (_("Couldn't get floating point status"));
107
108       amd64_supply_fxsave (regcache, -1, &fpregs);
109     }
110 }
111
112 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
113    this for all registers (including the floating-point registers).  */
114
115 static void
116 amd64bsd_store_inferior_registers (struct target_ops *ops,
117                                    struct regcache *regcache, int regnum)
118 {
119   struct gdbarch *gdbarch = get_regcache_arch (regcache);
120   pid_t pid = get_ptrace_pid (regcache_get_ptid (regcache));
121
122   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
123     {
124       struct reg regs;
125
126       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
127         perror_with_name (_("Couldn't get registers"));
128
129       amd64_collect_native_gregset (regcache, &regs, regnum);
130
131       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
132         perror_with_name (_("Couldn't write registers"));
133
134       if (regnum != -1)
135         return;
136     }
137
138 #ifdef PT_SETFSBASE
139   if (regnum == -1 || regnum == AMD64_FSBASE_REGNUM)
140     {
141       register_t base;
142
143       regcache_raw_collect (regcache, AMD64_FSBASE_REGNUM, &base);
144
145       if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
146         perror_with_name (_("Couldn't write segment register fs_base"));
147       if (regnum != -1)
148         return;
149     }
150 #endif
151 #ifdef PT_SETGSBASE
152   if (regnum == -1 || regnum == AMD64_GSBASE_REGNUM)
153     {
154       register_t base;
155
156       regcache_raw_collect (regcache, AMD64_GSBASE_REGNUM, &base);
157
158       if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
159         perror_with_name (_("Couldn't write segment register gs_base"));
160       if (regnum != -1)
161         return;
162     }
163 #endif
164
165   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
166     {
167       struct fpreg fpregs;
168 #ifdef PT_GETXSTATE_INFO
169       void *xstateregs;
170
171       if (x86bsd_xsave_len != 0)
172         {
173           xstateregs = alloca (x86bsd_xsave_len);
174           if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0)
175               == -1)
176             perror_with_name (_("Couldn't get extended state status"));
177
178           amd64_collect_xsave (regcache, regnum, xstateregs, 0);
179
180           if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs,
181                       x86bsd_xsave_len) == -1)
182             perror_with_name (_("Couldn't write extended state status"));
183           return;
184         }
185 #endif
186
187       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
188         perror_with_name (_("Couldn't get floating point status"));
189
190       amd64_collect_fxsave (regcache, regnum, &fpregs);
191
192       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
193         perror_with_name (_("Couldn't write floating point status"));
194     }
195 }
196
197 /* Create a prototype *BSD/amd64 target.  The client can override it
198    with local methods.  */
199
200 struct target_ops *
201 amd64bsd_target (void)
202 {
203   struct target_ops *t;
204
205   t = x86bsd_target ();
206   t->to_fetch_registers = amd64bsd_fetch_inferior_registers;
207   t->to_store_registers = amd64bsd_store_inferior_registers;
208   return t;
209 }