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