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