Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / gnu / usr.bin / binutils / gdb / i386 / freebsd-nat.c
1 /* $FreeBSD: src/gnu/usr.bin/binutils/gdb/i386/freebsd-nat.c,v 1.21.4.4 2001/08/15 02:42:27 peter Exp $ */
2 /* $DragonFly: src/gnu/usr.bin/binutils/gdb/i386/Attic/freebsd-nat.c,v 1.2 2003/06/17 04:25:44 dillon Exp $ */
3 /* Native-dependent code for BSD Unix running on i386's, for GDB.
4    Copyright 1988, 1989, 1991, 1992, 1994, 1996 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <machine/reg.h>
27 #include <machine/frame.h>
28 #include <sys/param.h>
29 #include <sys/user.h>
30 #include "gdbcore.h"
31 #include "value.h"
32 #include "inferior.h"
33
34 #if defined(HAVE_GREGSET_T) || defined(HAVE_FPREGSET_T)
35 #include <sys/procfs.h>
36 #endif
37
38 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
39 /* symbols like 'tEAX' come from <machine/reg.h> */
40 static int tregmap[] =
41 {
42   tEAX, tECX, tEDX, tEBX,
43   tESP, tEBP, tESI, tEDI,
44   tEIP, tEFLAGS, tCS, tSS,
45   tDS, tES, tFS, tGS,
46 };
47
48 static struct save87 pcb_savefpu;
49
50 void
51 fetch_inferior_registers (regno)
52      int regno;
53 {
54   struct reg inferior_registers;        /* ptrace order, not gcc/gdb order */
55   int r;
56
57   ptrace (PT_GETREGS, inferior_pid,
58           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
59
60   for (r = 0; r < NUM_REGS; r++)
61     memcpy (&registers[REGISTER_BYTE (r)], ((int *)&inferior_registers) + tregmap[r], 4);
62
63   registers_fetched ();
64 }
65
66 void
67 store_inferior_registers (regno)
68      int regno;
69 {
70   struct reg inferior_registers;        /* ptrace order, not gcc/gdb order */
71   int r;
72
73   for (r = 0; r < NUM_REGS; r++)
74     memcpy (((int *)&inferior_registers) + tregmap[r], &registers[REGISTER_BYTE (r)], 4);
75
76   ptrace (PT_SETREGS, inferior_pid,
77           (PTRACE_ARG3_TYPE) &inferior_registers, 0);
78 }
79
80 /* Extract the register values out of the core file and store
81    them where `read_register' will find them.
82    Extract the floating point state out of the core file and store
83    it where `float_info' will find it.
84
85    CORE_REG_SECT points to the register values themselves, read into memory.
86    CORE_REG_SIZE is the size of that area.
87    WHICH says which set of registers we are handling (0 = int, 2 = float
88          on machines where they are discontiguous).
89    REG_ADDR is the offset from u.u_ar0 to the register values relative to
90             core_reg_sect.  This is used with old-fashioned core files to
91             locate the registers in a large upage-plus-stack ".reg" section.
92             Original upage address X is at location core_reg_sect+x+reg_addr.
93  */
94
95 static void
96 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
97      char *core_reg_sect;
98      unsigned core_reg_size;
99      int which;
100      CORE_ADDR reg_addr;
101 {
102   register int regno;
103   register int cregno;
104   register int addr;
105   int bad_reg = -1;
106   int offset;
107   struct user *tmp_uaddr;
108
109   /* 
110    * First get virtual address of user structure. Then calculate offset.
111    */
112   memcpy(&tmp_uaddr,
113          &((struct user *) core_reg_sect)->u_kproc.kp_proc.p_addr,
114          sizeof(tmp_uaddr));
115   offset = -reg_addr - (int) tmp_uaddr;
116   
117   for (regno = 0; regno < NUM_REGS; regno++)
118     {
119       cregno = tregmap[regno];
120       if (cregno == tGS)
121         addr = offsetof (struct user, u_pcb) + offsetof (struct pcb, pcb_gs);
122       else
123         addr = offset + 4 * cregno;
124       if (addr < 0 || addr >= core_reg_size)
125         {
126           if (bad_reg < 0)
127             bad_reg = regno;
128         }
129       else
130         {
131           supply_register (regno, core_reg_sect + addr);
132         }
133     }
134   if (bad_reg >= 0)
135     {
136       error ("Register %s not found in core file.", gdb_register_names[bad_reg]);
137     }
138
139 #if __FreeBSD_version >= 440000
140   addr = offsetof (struct user, u_pcb) + offsetof (struct pcb, pcb_save);
141 #else
142   addr = offsetof (struct user, u_pcb) + offsetof (struct pcb, pcb_savefpu);
143 #endif
144   memcpy (&pcb_savefpu, core_reg_sect + addr, sizeof pcb_savefpu);
145 }
146
147 #ifdef FLOAT_INFO
148 #include "expression.h"
149 #include "language.h"                   /* for local_hex_string */
150 #include "floatformat.h"
151
152 #include <sys/param.h>
153 #include <signal.h>
154 #include <sys/ioctl.h>
155 #include <fcntl.h>
156
157 #include <a.out.h>
158
159 #include <sys/time.h>
160 #include <sys/resource.h>
161 #include <sys/uio.h>
162 #define curpcb Xcurpcb  /* XXX avoid leaking declaration from pcb.h */
163 #include <sys/user.h>
164 #undef curpcb
165 #include <sys/file.h>
166 #include "gdb_stat.h"
167 #include <sys/ptrace.h>
168
169 extern void print_387_control_word ();          /* i387-tdep.h */
170 extern void print_387_status_word ();
171
172 #define fpstate         save87
173 #if __FreeBSD_version >= 440000
174 #define U_FPSTATE(u)    u.u_pcb.pcb_save.sv_87
175 #else
176 #define U_FPSTATE(u)    u.u_pcb.pcb_savefpu
177 #endif
178
179 static void
180 i387_to_double (from, to)
181      char *from;
182      char *to;
183 {
184   long *lp;
185   /* push extended mode on 387 stack, then pop in double mode
186    *
187    * first, set exception masks so no error is generated -
188    * number will be rounded to inf or 0, if necessary
189    */
190   asm ("pushl %eax");           /* grab a stack slot */
191   asm ("fstcw (%esp)");         /* get 387 control word */
192   asm ("movl (%esp),%eax");     /* save old value */
193   asm ("orl $0x3f,%eax");               /* mask all exceptions */
194   asm ("pushl %eax");
195   asm ("fldcw (%esp)");         /* load new value into 387 */
196
197   asm ("movl 8(%ebp),%eax");
198   asm ("fldt (%eax)");          /* push extended number on 387 stack */
199   asm ("fwait");
200   asm ("movl 12(%ebp),%eax");
201   asm ("fstpl (%eax)");         /* pop double */
202   asm ("fwait");
203
204   asm ("popl %eax");            /* flush modified control word */
205   asm ("fnclex");                       /* clear exceptions */
206   asm ("fldcw (%esp)");         /* restore original control word */
207   asm ("popl %eax");            /* flush saved copy */
208 }
209
210 struct env387 
211 {
212   unsigned short control;
213   unsigned short r0;
214   unsigned short status;
215   unsigned short r1;
216   unsigned short tag;
217   unsigned short r2;
218   unsigned long eip;
219   unsigned short code_seg;
220   unsigned short opcode;
221   unsigned long operand;
222   unsigned short operand_seg;
223   unsigned short r3;
224   unsigned char regs[8][10];
225 };
226
227 static void
228 print_387_status (status, ep)
229      unsigned short status;
230      struct env387 *ep;
231 {
232   int i;
233   int bothstatus;
234   int top;
235   int fpreg;
236   
237   bothstatus = ((status != 0) && (ep->status != 0));
238   if (status != 0) 
239     {
240       if (bothstatus)
241         printf_unfiltered ("u: ");
242       print_387_status_word ((unsigned int)status);
243     }
244   
245   if (ep->status != 0) 
246     {
247       if (bothstatus)
248         printf_unfiltered ("e: ");
249       print_387_status_word ((unsigned int)ep->status);
250     }
251   
252   print_387_control_word ((unsigned int)ep->control);
253   printf_unfiltered ("last instruction: ");
254   printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
255   printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
256   printf_unfiltered ("%s; ", local_hex_string(ep->eip));
257   printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
258   printf_unfiltered (":%s\n", local_hex_string(ep->operand));
259
260   top = (ep->status >> 11) & 7;
261   
262   printf_unfiltered (" regno     tag  msb              lsb  value\n");
263   for (fpreg = 7; fpreg >= 0; fpreg--) 
264     {
265       int exp;
266       int mantissa_or;
267       int normal;
268       char *sign;
269       int st_regno;
270       unsigned short *usregs;
271       double val;
272       
273       /* The physical regno `fpreg' is only relevant as an index into the
274        * tag word.  Logical `%st' numbers are required for indexing ep->regs.
275        */
276       st_regno = (fpreg + 8 - top) & 7;
277
278       printf_unfiltered ("%%st(%d) %s ", st_regno, fpreg == top ? "=>" : "  ");
279
280       switch ((ep->tag >> (fpreg * 2)) & 3) 
281         {
282         case 0: printf_unfiltered ("valid "); break;
283         case 1: printf_unfiltered ("zero  "); break;
284         case 2: printf_unfiltered ("trap  "); break;
285         case 3: printf_unfiltered ("empty "); break;
286         }
287       for (i = 9; i >= 0; i--)
288         printf_unfiltered ("%02x", ep->regs[st_regno][i]);
289       
290       printf_unfiltered ("  ");
291
292       /*
293        * Handle weird cases better than floatformat_to_double () and
294        * printf ().
295        */
296       usregs = (unsigned short *) ep->regs[st_regno];
297       sign = usregs[4] & 0x8000 ? "-" : "";
298       exp = usregs[4] & 0x7fff;
299       normal = usregs[3] & 0x8000;
300       mantissa_or = usregs[0] | usregs[1] | usregs[2] | (usregs[3] & 0x7fff);
301       if (exp == 0)
302         {
303           if (normal)
304             printf_unfiltered ("Pseudo Denormal (0 as a double)");
305           else if (mantissa_or == 0)
306             printf_unfiltered ("%s0", sign);
307           else
308             printf_unfiltered ("Denormal (0 as a double)");
309         }
310       else if (exp == 0x7fff)
311         {
312           if (!normal)
313             printf_unfiltered ("Pseudo ");
314           if (mantissa_or == 0)
315             printf_unfiltered ("%sInf", sign);
316           else
317             printf_unfiltered ("%s NaN",
318                                usregs[3] & 0x4000 ? "Quiet" : "Signaling");
319           if (!normal)
320             printf_unfiltered (" (NaN)");
321         }
322       else if (!normal)
323         printf_unfiltered ("Unnormal (NaN)");
324       else
325         {
326 #if 0
327           /* Use this we stop trapping on overflow.  */
328           floatformat_to_double(&floatformat_i387_ext,
329                                 (char *) ep->regs[st_regno], &val);
330 #else
331           i387_to_double((char *) ep->regs[st_regno], (char *) &val);
332 #endif
333           printf_unfiltered ("%g", val);
334         }
335       printf_unfiltered ("\n");
336     }
337 }
338
339 void
340 i386_float_info ()
341 {
342   struct user u; /* just for address computations */
343   int i;
344   /* fpstate defined in <sys/user.h> */
345   struct fpstate *fpstatep;
346   char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
347   unsigned int uaddr;
348   char fpvalid;
349   unsigned int rounded_addr;
350   unsigned int rounded_size;
351   /*extern int corechan;*/
352   int skip;
353   extern int inferior_pid;
354   
355   uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
356   if (inferior_pid != 0 && core_bfd == NULL) 
357     {
358       int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
359       ptrace(PT_GETFPREGS, pid, &buf[0], sizeof(struct fpreg));
360       fpstatep = (struct fpstate *)&buf[0];
361     } 
362   else 
363     fpstatep = &pcb_savefpu;
364
365   print_387_status (fpstatep->sv_ex_sw, (struct env387 *)fpstatep);
366 }
367 #endif /* FLOAT_INFO */
368
369 int
370 kernel_u_size ()
371 {
372   return (sizeof (struct user));
373 }
374
375 #ifdef  SETUP_ARBITRARY_FRAME
376 #include "frame.h"
377 struct frame_info *
378 setup_arbitrary_frame (argc, argv)
379         int argc;
380         CORE_ADDR *argv;
381 {
382     if (argc != 2)
383         error ("i386 frame specifications require two arguments: sp and pc");
384
385     return create_new_frame (argv[0], argv[1]);
386 }
387 #endif  /* SETUP_ARBITRARY_FRAME */
388
389 #ifdef HAVE_GREGSET_T
390 void
391 supply_gregset (gp)
392   gregset_t *gp;
393 {
394   int regno = 0;
395
396   /* These must be ordered the same as REGISTER_NAMES in
397      config/i386/tm-i386.h. */
398   supply_register (regno++, (char *)&gp->r_eax);
399   supply_register (regno++, (char *)&gp->r_ecx);
400   supply_register (regno++, (char *)&gp->r_edx);
401   supply_register (regno++, (char *)&gp->r_ebx);
402   supply_register (regno++, (char *)&gp->r_esp);
403   supply_register (regno++, (char *)&gp->r_ebp);
404   supply_register (regno++, (char *)&gp->r_esi);
405   supply_register (regno++, (char *)&gp->r_edi);
406   supply_register (regno++, (char *)&gp->r_eip);
407   supply_register (regno++, (char *)&gp->r_eflags);
408   supply_register (regno++, (char *)&gp->r_cs);
409   supply_register (regno++, (char *)&gp->r_ss);
410   supply_register (regno++, (char *)&gp->r_ds);
411   supply_register (regno++, (char *)&gp->r_es);
412   supply_register (regno++, (char *)&gp->r_fs);
413   supply_register (regno++, (char *)&gp->r_gs);
414 }
415 #endif  /* HAVE_GREGSET_T */
416
417 #ifdef HAVE_FPREGSET_T
418 void
419 supply_fpregset (fp)
420   fpregset_t *fp;
421 {
422   memcpy (&pcb_savefpu, fp, sizeof pcb_savefpu);
423 }
424 #endif  /* HAVE_FPREGSET_T */
425 \f
426 /* Register that we are able to handle aout (trad-core) file formats.  */
427
428 static struct core_fns aout_core_fns =
429 {
430   bfd_target_unknown_flavour,
431   fetch_core_registers,
432   NULL
433 };
434
435 void
436 _initialize_core_aout ()
437 {
438   add_core_fns (&aout_core_fns);
439 }
440
441 #ifdef PT_GETDBREGS
442
443 /*
444  * 0: no trace output
445  * 1: trace watchpoint requests
446  * 2: trace `watchpoint hit?' tests, too
447  */
448 #define WATCHPOINT_DEBUG 0
449
450 #include "breakpoint.h"
451
452 int
453 can_watch(type, cnt, ot)
454      int type, cnt, ot;
455 {
456   int rv;
457   static int cnt_watch, cnt_awatch;
458
459   switch (type)
460     {
461     case bp_hardware_watchpoint:
462       cnt_watch = cnt;
463       break;
464       
465     case bp_access_watchpoint:
466       cnt_awatch = cnt;
467       break;
468       
469     default:
470       rv = 0;
471       goto overandout;
472     }
473
474   rv = cnt_watch + cnt_awatch <= 4? 1: -1;
475
476  overandout:
477 #if WATCHPOINT_DEBUG
478   printf_filtered("can_watch(%d, %d, %d) = %d (counts: w: %d, rw: %d)\n",
479                   type, cnt, ot, rv, cnt_watch, cnt_awatch);
480 #endif
481
482   return rv;
483 }
484
485 int
486 stopped_by_watchpoint()
487 {
488   struct dbreg dbr;
489   extern int inferior_pid;
490
491   if (current_target.to_shortname == 0 ||
492       ! (strcmp(current_target.to_shortname, "child") == 0 ||
493          strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
494     return 0;
495   
496   if (inferior_pid != 0 && core_bfd == NULL) 
497     {
498       int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
499   
500       if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
501         {
502           perror("ptrace(PT_GETDBREGS) failed");
503           return 0;
504         }
505 #if WATCHPOINT_DEBUG > 1
506       printf_filtered("stopped_by_watchpoint(): DR6 = %#x\n", dbr.dr6);
507 #endif
508       /*
509        * If a hardware watchpoint was hit, one of the lower 4 bits in
510        * DR6 is set (the actual bit indicates which of DR0...DR3 triggered
511        * the trap).
512        */
513       return dbr.dr6 & 0x0f;
514     } 
515   else
516     {
517       warning("Can't set a watchpoint on a core file.");
518       return 0;
519     }
520 }
521
522 int
523 insert_watchpoint(addr, len, type)
524      int addr, len, type;
525 {
526   struct dbreg dbr;
527   extern int inferior_pid;
528   
529   if (current_target.to_shortname == 0 ||
530       ! (strcmp(current_target.to_shortname, "child") == 0 ||
531          strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
532     return 0;
533   
534   if (inferior_pid != 0 && core_bfd == NULL) 
535     {
536       int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
537       int i, mask;
538       unsigned int sbits;
539
540       if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
541         {
542           perror("ptrace(PT_GETDBREGS) failed");
543           return 0;
544         }
545
546       for (i = 0, mask = 0x03; i < 4; i++, mask <<= 2)
547         if ((dbr.dr7 & mask) == 0)
548           break;
549       if (i >= 4) {
550         warning("no more hardware watchpoints available");
551         return -1;
552       }
553
554       /* paranoia */
555       if (len > 4)
556         {
557           warning("watchpoint length %d unsupported, using lenght = 4",
558                   len);
559           len = 4;
560         }
561       else if (len == 3)
562         {
563           warning("weird watchpoint length 3, using 2");
564           len = 2;
565         }
566       else if (len == 0)
567         {
568           warning("weird watchpoint length 0, using 1");
569           len = 1;
570         }
571
572       switch (len)
573         {
574         case 1: sbits = 0; break;
575         case 2: sbits = 4; break;
576         case 4: sbits = 0x0c; break;
577         }
578       
579       /*
580        *  The `type' value is 0 for `watch on write', 1 for `watch on
581        * read', 2 for `watch on both'.  The i386 debug register
582        * breakpoint types are 0 for `execute' (not used in GDB), 1 for
583        * `write', and 4 for `read/write'.  Plain `read' trapping is
584        * not supported on i386, value 3 is illegal.
585        */
586       switch (type)
587         {
588         default:
589           warning("weird watchpoint type %d, using a write watchpoint");
590           /* FALLTHROUGH */
591         case 0:
592           sbits |= 1;
593           break;
594
595         case 2:
596           sbits |= 3;
597           break;
598         }
599       sbits <<= 4 * i + 16;
600       sbits |= 1 << 2 * i;
601
602       dbr.dr7 |= sbits;
603       *(&dbr.dr0 + i) = (unsigned int)addr;
604
605 #if WATCHPOINT_DEBUG
606       printf_filtered("insert_watchpoint(), inserting DR7 = %#x, DR%d = %#x\n",
607                       dbr.dr7, i, addr);
608 #endif
609       if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
610         {
611           perror("ptrace(PT_SETDBREGS) failed");
612           return 0;
613         }
614     }
615   else
616     {
617       warning("Can't set a watchpoint on a core file.");
618       return 0;
619     }
620 }
621
622 int
623 remove_watchpoint(addr, len, type)
624      int addr, len, type;
625 {
626   struct dbreg dbr;
627   extern int inferior_pid;
628   
629   if (current_target.to_shortname == 0 ||
630       ! (strcmp(current_target.to_shortname, "child") == 0 ||
631          strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
632     return 0;
633
634   if (inferior_pid != 0 && core_bfd == NULL) 
635     {
636       int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
637       int i;
638       unsigned int sbits, *dbregp;
639   
640       if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
641         {
642           perror("ptrace(PT_GETDBREGS) failed");
643           return 0;
644         }
645
646       for (i = 0, dbregp = &dbr.dr0; i < 4; i++, dbregp++)
647         if (*dbregp == (unsigned int)addr)
648           break;
649       if (i >= 4)
650         {
651           warning("watchpoint for address %#x not found", addr);
652           return -1;
653         }
654
655       *dbregp = 0;
656       sbits = 0xf << (4 * i + 16);
657       sbits |= 3 << 2 * i;
658       dbr.dr7 &= ~sbits;
659
660 #if WATCHPOINT_DEBUG
661       printf_filtered("remove_watchpoint(): removing watchpoint for %#x, DR7 = %#x\n",
662                       addr, dbr.dr7);
663 #endif
664       if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
665         {
666           perror("ptrace(PT_SETDBREGS) failed");
667           return 0;
668         }
669     }
670   else
671     {
672       warning("Can't set a watchpoint on a core file.");
673       return 0;
674     }
675 }
676
677 #endif /* PT_GETDBREGS */