1 /* Native-dependent code for AMD64 BSD's.
2 
3    Copyright 2003, 2004 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "target.h"
26 
27 /* We include <signal.h> to make sure `struct fxsave64' is defined on
28    NetBSD, since NetBSD's <machine/reg.h> needs it.  */
29 #include "gdb_assert.h"
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/ptrace.h>
33 #include <machine/reg.h>
34 
35 #include "amd64-tdep.h"
36 #include "amd64-nat.h"
37 #include "inf-ptrace.h"
38 
39 
40 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
41    for all registers (including the floating-point registers).  */
42 
43 static void
44 amd64bsd_fetch_inferior_registers (int regnum)
45 {
46   int pid;
47 
48   /* Cater for systems like OpenBSD, that implement threads as
49      separate processes.  */
50   pid = ptid_get_lwp (inferior_ptid);
51   if (pid == 0)
52     pid = ptid_get_pid (inferior_ptid);
53 
54   if (regnum == -1 || amd64_native_gregset_supplies_p (regnum))
55     {
56       struct reg regs;
57 
58       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
59 	perror_with_name ("Couldn't get registers");
60 
61       amd64_supply_native_gregset (current_regcache, &regs, -1);
62       if (regnum != -1)
63 	return;
64     }
65 
66   if (regnum == -1 || !amd64_native_gregset_supplies_p (regnum))
67     {
68       struct fpreg fpregs;
69 
70       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
71 	perror_with_name ("Couldn't get floating point status");
72 
73       amd64_supply_fxsave (current_regcache, -1, &fpregs);
74     }
75 }
76 
77 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
78    this for all registers (including the floating-point registers).  */
79 
80 static void
81 amd64bsd_store_inferior_registers (int regnum)
82 {
83   int pid;
84 
85   /* Cater for systems like OpenBSD, that implement threads as
86      separate processes.  */
87   pid = ptid_get_lwp (inferior_ptid);
88   if (pid == 0)
89     pid = ptid_get_pid (inferior_ptid);
90 
91   if (regnum == -1 || amd64_native_gregset_supplies_p (regnum))
92     {
93       struct reg regs;
94 
95       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
96         perror_with_name ("Couldn't get registers");
97 
98       amd64_collect_native_gregset (current_regcache, &regs, regnum);
99 
100       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
101         perror_with_name ("Couldn't write registers");
102 
103       if (regnum != -1)
104 	return;
105     }
106 
107   if (regnum == -1 || !amd64_native_gregset_supplies_p (regnum))
108     {
109       struct fpreg fpregs;
110 
111       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
112 	perror_with_name ("Couldn't get floating point status");
113 
114       amd64_collect_fxsave (current_regcache, regnum, &fpregs);
115 
116       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
117 	perror_with_name ("Couldn't write floating point status");
118     }
119 }
120 
121 /* Create a prototype *BSD/amd64 target.  The client can override it
122    with local methods.  */
123 
124 struct target_ops *
125 amd64bsd_target (void)
126 {
127   struct target_ops *t;
128 
129   t = inf_ptrace_target ();
130   t->to_fetch_registers = amd64bsd_fetch_inferior_registers;
131   t->to_store_registers = amd64bsd_store_inferior_registers;
132   return t;
133 }
134