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