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