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