OSDN Git Service

Ensure bfd_close after bfd_openw so we don't get multiple bfd_openw
[pf3gnuchains/pf3gnuchains4x.git] / gdb / i386aix-nat.c
1 /* Intel 386 native support.
2    Copyright (C) 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "language.h"
24 #include "gdbcore.h"
25
26 #ifdef USG
27 #include <sys/types.h>
28 #endif
29
30 #include <sys/param.h>
31 #include <sys/dir.h>
32 #include <signal.h>
33 #include <sys/user.h>
34 #include <sys/ioctl.h>
35 #include <fcntl.h>
36
37 #include <sys/file.h>
38 #include "gdb_stat.h"
39
40 #include <stddef.h>
41 #include <sys/ptrace.h>
42
43 /* Does AIX define this in <errno.h>?  */
44 extern int errno;
45
46 #ifdef HAVE_SYS_REG_H
47 #include <sys/reg.h>
48 #endif
49
50 #include "floatformat.h"
51
52 #include "target.h"
53
54 static void fetch_core_registers PARAMS ((char *, unsigned, int, CORE_ADDR));
55
56 \f
57 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
58 /* symbols like 'EAX' come from <sys/reg.h> */
59 static int regmap[] = 
60 {
61   EAX, ECX, EDX, EBX,
62   USP, EBP, ESI, EDI,
63   EIP, EFL, CS, SS,
64   DS, ES, FS, GS,
65 };
66
67 /* blockend is the value of u.u_ar0, and points to the
68  * place where GS is stored
69  */
70
71 int
72 i386_register_u_addr (blockend, regnum)
73      int blockend;
74      int regnum;
75 {
76 #if 0
77   /* this will be needed if fp registers are reinstated */
78   /* for now, you can look at them with 'info float'
79    * sys5 wont let you change them with ptrace anyway
80    */
81   if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) 
82     {
83       int ubase, fpstate;
84       struct user u;
85       ubase = blockend + 4 * (SS + 1) - KSTKSZ;
86       fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
87       return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
88     } 
89   else
90 #endif
91     return (blockend + 4 * regmap[regnum]);
92   
93 }
94
95 /* The code below only work on the aix ps/2 (i386-ibm-aix) -
96  * mtranle@paris - Sat Apr 11 10:34:12 1992
97  */
98
99 struct env387 
100 {
101   unsigned short control;
102   unsigned short r0;
103   unsigned short status;
104   unsigned short r1;
105   unsigned short tag;
106   unsigned short r2;
107   unsigned long eip;
108   unsigned short code_seg;
109   unsigned short opcode;
110   unsigned long operand;
111   unsigned short operand_seg;
112   unsigned short r3;
113   unsigned char regs[8][10];
114 };
115
116 static
117 print_387_status (status, ep)
118      unsigned short status;
119      struct env387 *ep;
120 {
121   int i;
122   int bothstatus;
123   int top;
124   int fpreg;
125   unsigned char *p;
126   
127   bothstatus = ((status != 0) && (ep->status != 0));
128   if (status != 0) 
129     {
130       if (bothstatus)
131         printf_unfiltered ("u: ");
132       print_387_status_word (status);
133     }
134   
135   if (ep->status != 0) 
136     {
137       if (bothstatus)
138         printf_unfiltered ("e: ");
139       print_387_status_word (ep->status);
140     }
141   
142   print_387_control_word (ep->control);
143   printf_unfiltered ("last exception: ");
144   printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
145   printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
146   printf_unfiltered ("%s; ", local_hex_string(ep->eip));
147   printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
148   printf_unfiltered (":%s\n", local_hex_string(ep->operand));
149
150   top = ((ep->status >> 11) & 7);
151
152   printf_unfiltered ("regno  tag  msb              lsb  value\n");
153   for (fpreg = 7; fpreg >= 0; fpreg--) 
154     {
155       double val;
156
157       printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
158
159       switch ((ep->tag >> ((7 - fpreg) * 2)) & 3) 
160         {
161         case 0: printf_unfiltered ("valid "); break;
162         case 1: printf_unfiltered ("zero  "); break;
163         case 2: printf_unfiltered ("trap  "); break;
164         case 3: printf_unfiltered ("empty "); break;
165         }
166       for (i = 9; i >= 0; i--)
167         printf_unfiltered ("%02x", ep->regs[fpreg][i]);
168       
169       i387_to_double ((char *)ep->regs[fpreg], (char *)&val);
170       printf_unfiltered ("  %#g\n", val);
171     }
172 }
173
174 static struct env387 core_env387;
175
176 void
177 i386_float_info ()
178 {
179   struct env387 fps;
180   int fpsaved = 0;
181   /* We need to reverse the order of the registers.  Apparently AIX stores
182      the highest-numbered ones first.  */
183   struct env387 fps_fixed;
184   int i;
185
186   if (inferior_pid)
187     {
188       char buf[10];
189       unsigned short status;
190
191       ptrace (PT_READ_FPR, inferior_pid, buf, offsetof(struct env387, status));
192       memcpy (&status, buf, sizeof (status));
193       fpsaved = status;
194     }
195   else
196     {
197       if ((fpsaved = core_env387.status) != 0)
198         memcpy(&fps, &core_env387, sizeof(fps));
199     }
200   
201   if (fpsaved == 0) 
202     {
203       printf_unfiltered ("no floating point status saved\n");
204       return;
205     }
206
207   if (inferior_pid)
208     {
209       int offset;
210       for (offset = 0; offset < sizeof(fps); offset += 10)
211         {
212           char buf[10];
213           ptrace (PT_READ_FPR, inferior_pid, buf, offset);
214           memcpy ((char *)&fps.control + offset, buf,
215                   MIN(10, sizeof(fps) - offset));
216         }
217     } 
218   fps_fixed = fps;
219   for (i = 0; i < 8; ++i)
220     memcpy (fps_fixed.regs[i], fps.regs[7 - i], 10);
221   print_387_status (0, &fps_fixed);
222 }
223
224 /* Fetch one register.  */
225 static void
226 fetch_register (regno)
227      int regno;
228 {
229   char buf[MAX_REGISTER_RAW_SIZE];
230   if (regno < FP0_REGNUM)
231     *(int *)buf = ptrace (PT_READ_GPR, inferior_pid,
232                           PT_REG(regmap[regno]), 0, 0);
233   else
234     ptrace (PT_READ_FPR, inferior_pid, buf,
235             (regno - FP0_REGNUM)*10 + offsetof(struct env387, regs));
236   supply_register (regno, buf);
237 }
238
239 void
240 fetch_inferior_registers (regno)
241      int regno;
242 {
243   if (regno < 0)
244     for (regno = 0; regno < NUM_REGS; regno++)
245       fetch_register (regno);
246   else
247     fetch_register (regno);
248 }
249
250 /* store one register */
251 static void
252 store_register (regno)
253      int regno;
254 {
255   char buf[80];
256   extern char registers[];
257   errno = 0;
258   if (regno < FP0_REGNUM)
259     ptrace (PT_WRITE_GPR, inferior_pid, PT_REG(regmap[regno]),
260             *(int *) &registers[REGISTER_BYTE (regno)], 0);
261   else
262     ptrace (PT_WRITE_FPR, inferior_pid, &registers[REGISTER_BYTE (regno)],
263             (regno - FP0_REGNUM)*10 + offsetof(struct env387, regs));
264
265   if (errno != 0)
266     {
267       sprintf (buf, "writing register number %d", regno);
268       perror_with_name (buf);
269     }
270 }
271
272 /* Store our register values back into the inferior.
273    If REGNO is -1, do this for all registers.
274    Otherwise, REGNO specifies which register (so we can save time).  */
275 void
276 store_inferior_registers (regno)
277      int regno;
278 {
279   if (regno < 0)
280     for (regno = 0; regno < NUM_REGS; regno++)
281       store_register (regno);
282   else
283     store_register (regno);
284 }
285
286 #ifndef CD_AX                   /* defined in sys/i386/coredump.h */
287 # define CD_AX  0
288 # define CD_BX  1
289 # define CD_CX  2
290 # define CD_DX  3
291 # define CD_SI  4
292 # define CD_DI  5
293 # define CD_BP  6
294 # define CD_SP  7
295 # define CD_FL  8
296 # define CD_IP  9
297 # define CD_CS  10
298 # define CD_DS  11
299 # define CD_ES  12
300 # define CD_FS  13
301 # define CD_GS  14
302 # define CD_SS  15
303 #endif
304
305 /*
306  * The order here in core_regmap[] has to be the same as in 
307  * regmap[] above.
308  */
309 static int core_regmap[] = 
310 {
311   CD_AX, CD_CX, CD_DX, CD_BX,
312   CD_SP, CD_BP, CD_SI, CD_DI,
313   CD_IP, CD_FL, CD_CS, CD_SS,
314   CD_DS, CD_ES, CD_FS, CD_GS,
315 };
316
317 static void
318 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
319      char *core_reg_sect;
320      unsigned core_reg_size;
321      int which;
322      CORE_ADDR reg_addr;        /* ignored */
323 {
324
325   if (which == 0)
326     {
327       /* Integer registers */
328
329 #define cd_regs(n) ((int *)core_reg_sect)[n]
330 #define regs(n) *((int *) &registers[REGISTER_BYTE (n)])
331
332       int i;
333       for (i = 0; i < FP0_REGNUM; i++)
334         regs(i) = cd_regs(core_regmap[i]);
335     }
336   else if (which == 2)
337     {
338       /* Floating point registers */
339
340       if (core_reg_size >= sizeof (core_env387))
341         memcpy (&core_env387, core_reg_sect, core_reg_size);
342       else
343         fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n");
344     }
345 }
346
347 \f
348 /* Register that we are able to handle i386aix core file formats.
349    FIXME: is this really bfd_target_unknown_flavour? */
350
351 static struct core_fns i386aix_core_fns =
352 {
353   bfd_target_unknown_flavour,
354   fetch_core_registers,
355   NULL
356 };
357
358 void
359 _initialize_core_i386aix ()
360 {
361   add_core_fns (&i386aix_core_fns);
362 }