* i386nbsd-nat.c: Update copyright years.
[platform/upstream/binutils.git] / gdb / i386nbsd-nat.c
1 /* Native-dependent code for NetBSD/i386, for GDB.
2    Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002
3    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 <sys/types.h>
24 #include <sys/ptrace.h>
25 #include <machine/reg.h>
26 #include <machine/frame.h>
27 #include "inferior.h"
28 #include "gdbcore.h" /* for registers_fetched() */
29 #include "regcache.h"
30
31 /* Defines for XMM0_REGNUM etc. */
32 #include "i386-tdep.h"
33
34 #define RF(dst, src) \
35         memcpy(&registers[REGISTER_BYTE(dst)], &src, sizeof(src))
36
37 #define RS(src, dst) \
38         memcpy(&dst, &registers[REGISTER_BYTE(src)], sizeof(dst))
39      
40 struct env387
41   {
42     unsigned short control;
43     unsigned short r0;
44     unsigned short status;
45     unsigned short r1;
46     unsigned short tag;  
47     unsigned short r2;
48     unsigned long eip;
49     unsigned short code_seg;
50     unsigned short opcode;
51     unsigned long operand; 
52     unsigned short operand_seg;
53     unsigned short r3;
54     unsigned char regs[8][10];
55   };
56
57 void
58 fetch_inferior_registers (int regno)
59 {
60   struct reg inferior_registers;
61   struct env387 inferior_fpregisters;
62
63   ptrace (PT_GETREGS, PIDGET (inferior_ptid),
64           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
65   ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
66           (PTRACE_ARG3_TYPE) &inferior_fpregisters, 0);
67
68   RF ( 0, inferior_registers.r_eax);
69   RF ( 1, inferior_registers.r_ecx);
70   RF ( 2, inferior_registers.r_edx);
71   RF ( 3, inferior_registers.r_ebx);
72   RF ( 4, inferior_registers.r_esp);
73   RF ( 5, inferior_registers.r_ebp);
74   RF ( 6, inferior_registers.r_esi);
75   RF ( 7, inferior_registers.r_edi);
76   RF ( 8, inferior_registers.r_eip);
77   RF ( 9, inferior_registers.r_eflags);
78   RF (10, inferior_registers.r_cs);
79   RF (11, inferior_registers.r_ss);
80   RF (12, inferior_registers.r_ds);
81   RF (13, inferior_registers.r_es);
82   RF (14, inferior_registers.r_fs);
83   RF (15, inferior_registers.r_gs);
84
85   RF (FP0_REGNUM,     inferior_fpregisters.regs[0]);
86   RF (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
87   RF (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
88   RF (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
89   RF (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
90   RF (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
91   RF (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
92   RF (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
93
94   RF (FCTRL_REGNUM,   inferior_fpregisters.control);
95   RF (FSTAT_REGNUM,   inferior_fpregisters.status);
96   RF (FTAG_REGNUM,    inferior_fpregisters.tag);
97   RF (FCS_REGNUM,     inferior_fpregisters.code_seg);
98   RF (FCOFF_REGNUM,   inferior_fpregisters.eip);
99   RF (FDS_REGNUM,     inferior_fpregisters.operand_seg);
100   RF (FDOFF_REGNUM,   inferior_fpregisters.operand);
101   RF (FOP_REGNUM,     inferior_fpregisters.opcode);
102
103   registers_fetched ();
104 }
105
106 void
107 store_inferior_registers (int regno)
108 {
109   struct reg inferior_registers;
110   struct env387 inferior_fpregisters;
111
112   RS ( 0, inferior_registers.r_eax);
113   RS ( 1, inferior_registers.r_ecx);
114   RS ( 2, inferior_registers.r_edx);
115   RS ( 3, inferior_registers.r_ebx);
116   RS ( 4, inferior_registers.r_esp);
117   RS ( 5, inferior_registers.r_ebp);
118   RS ( 6, inferior_registers.r_esi);
119   RS ( 7, inferior_registers.r_edi);
120   RS ( 8, inferior_registers.r_eip);
121   RS ( 9, inferior_registers.r_eflags);
122   RS (10, inferior_registers.r_cs);
123   RS (11, inferior_registers.r_ss);
124   RS (12, inferior_registers.r_ds);
125   RS (13, inferior_registers.r_es);
126   RS (14, inferior_registers.r_fs);
127   RS (15, inferior_registers.r_gs);
128
129   
130   RS (FP0_REGNUM,     inferior_fpregisters.regs[0]);
131   RS (FP0_REGNUM + 1, inferior_fpregisters.regs[1]);
132   RS (FP0_REGNUM + 2, inferior_fpregisters.regs[2]);
133   RS (FP0_REGNUM + 3, inferior_fpregisters.regs[3]);
134   RS (FP0_REGNUM + 4, inferior_fpregisters.regs[4]);
135   RS (FP0_REGNUM + 5, inferior_fpregisters.regs[5]);
136   RS (FP0_REGNUM + 6, inferior_fpregisters.regs[6]);
137   RS (FP0_REGNUM + 7, inferior_fpregisters.regs[7]);
138
139   RS (FCTRL_REGNUM,   inferior_fpregisters.control);
140   RS (FSTAT_REGNUM,   inferior_fpregisters.status);
141   RS (FTAG_REGNUM,    inferior_fpregisters.tag);
142   RS (FCS_REGNUM,     inferior_fpregisters.code_seg);
143   RS (FCOFF_REGNUM,   inferior_fpregisters.eip);
144   RS (FDS_REGNUM,     inferior_fpregisters.operand_seg);
145   RS (FDOFF_REGNUM,   inferior_fpregisters.operand);
146   RS (FOP_REGNUM,     inferior_fpregisters.opcode);
147   
148   ptrace (PT_SETREGS, PIDGET (inferior_ptid),
149           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
150   ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
151           (PTRACE_ARG3_TYPE) &inferior_fpregisters, 0);
152 }
153 \f
154 struct md_core
155 {
156   struct reg intreg;
157   struct env387 freg;
158 };
159
160 static void
161 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
162                       CORE_ADDR ignore)
163 {
164   struct md_core *core_reg = (struct md_core *) core_reg_sect;
165
166   /* integer registers */
167   memcpy (&registers[REGISTER_BYTE (0)], &core_reg->intreg,
168           sizeof (struct reg));
169
170   /* floating point registers */
171   RF (FP0_REGNUM,     core_reg->freg.regs[0]);
172   RF (FP0_REGNUM + 1, core_reg->freg.regs[1]);
173   RF (FP0_REGNUM + 2, core_reg->freg.regs[2]);
174   RF (FP0_REGNUM + 3, core_reg->freg.regs[3]);
175   RF (FP0_REGNUM + 4, core_reg->freg.regs[4]);
176   RF (FP0_REGNUM + 5, core_reg->freg.regs[5]);
177   RF (FP0_REGNUM + 6, core_reg->freg.regs[6]);
178   RF (FP0_REGNUM + 7, core_reg->freg.regs[7]);
179
180   RF (FCTRL_REGNUM,   core_reg->freg.control);
181   RF (FSTAT_REGNUM,   core_reg->freg.status);
182   RF (FTAG_REGNUM,    core_reg->freg.tag);
183   RF (FCS_REGNUM,     core_reg->freg.code_seg);
184   RF (FCOFF_REGNUM,   core_reg->freg.eip);
185   RF (FDS_REGNUM,     core_reg->freg.operand_seg);
186   RF (FDOFF_REGNUM,   core_reg->freg.operand);
187   RF (FOP_REGNUM,     core_reg->freg.opcode);
188
189   registers_fetched ();
190 }
191
192 /* Register that we are able to handle i386nbsd core file formats.
193    FIXME: is this really bfd_target_unknown_flavour? */
194
195 static struct core_fns i386nbsd_core_fns =
196 {
197   bfd_target_unknown_flavour,           /* core_flavour */
198   default_check_format,                 /* check_format */
199   default_core_sniffer,                 /* core_sniffer */
200   fetch_core_registers,                 /* core_read_registers */
201   NULL                                  /* next */
202 };
203
204 void
205 _initialize_i386nbsd_nat (void)
206 {
207   add_core_fns (&i386nbsd_core_fns);
208 }