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 $
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.
8 This file is part of GDB.
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.
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.
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. */
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>
36 #if defined(HAVE_GREGSET_T) || defined(HAVE_FPREGSET_T)
37 #include <sys/procfs.h>
40 #define PCB_OFFSET (UPAGES * PAGE_SIZE - sizeof(struct pcb))
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[] =
46 tEAX, tECX, tEDX, tEBX,
47 tESP, tEBP, tESI, tEDI,
48 tEIP, tEFLAGS, tCS, tSS,
52 static struct save87 pcb_savefpu;
55 fetch_inferior_registers (regno)
58 struct reg inferior_registers; /* ptrace order, not gcc/gdb order */
61 ptrace (PT_GETREGS, inferior_pid,
62 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
64 for (r = 0; r < NUM_REGS; r++)
65 memcpy (®isters[REGISTER_BYTE (r)], ((int *)&inferior_registers) + tregmap[r], 4);
71 store_inferior_registers (regno)
74 struct reg inferior_registers; /* ptrace order, not gcc/gdb order */
77 for (r = 0; r < NUM_REGS; r++)
78 memcpy (((int *)&inferior_registers) + tregmap[r], ®isters[REGISTER_BYTE (r)], 4);
80 ptrace (PT_SETREGS, inferior_pid,
81 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
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.
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.
100 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
102 unsigned core_reg_size;
111 struct user *tmp_uaddr;
114 * First get virtual address of user structure. Then calculate offset.
118 &((struct user *) core_reg_sect)->u_kproc.kp_proc.p_addr,
120 offset = -reg_addr - (int) tmp_uaddr;
122 for (regno = 0; regno < NUM_REGS; regno++)
124 cregno = tregmap[regno];
126 addr = PCB_OFFSET + offsetof (struct pcb, pcb_gs);
128 addr = offset + 4 * cregno;
129 if (addr < 0 || addr >= core_reg_size)
136 supply_register (regno, core_reg_sect + addr);
141 error ("Register %s not found in core file.", gdb_register_names[bad_reg]);
144 #if __FreeBSD_version >= 440000
145 addr = PCB_OFFSET + offsetof (struct pcb, pcb_save);
147 addr = PCB_OFFSET + offsetof (struct pcb, pcb_savefpu);
149 memcpy (&pcb_savefpu, core_reg_sect + addr, sizeof pcb_savefpu);
153 #include "expression.h"
154 #include "language.h" /* for local_hex_string */
155 #include "floatformat.h"
157 #include <sys/param.h>
159 #include <sys/ioctl.h>
164 #include <sys/time.h>
165 #include <sys/resource.h>
167 #define curpcb Xcurpcb /* XXX avoid leaking declaration from pcb.h */
168 #include <sys/user.h>
170 #include <sys/file.h>
171 #include "gdb_stat.h"
172 #include <sys/ptrace.h>
174 extern void print_387_control_word (); /* i387-tdep.h */
175 extern void print_387_status_word ();
177 #define fpstate save87
178 #if __FreeBSD_version >= 440000
179 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_save.sv_87)
181 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_savefpu)
185 i387_to_double (from, to)
190 /* push extended mode on 387 stack, then pop in double mode
192 * first, set exception masks so no error is generated -
193 * number will be rounded to inf or 0, if necessary
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 */
200 asm ("fldcw (%esp)"); /* load new value into 387 */
202 asm ("movl 8(%ebp),%eax");
203 asm ("fldt (%eax)"); /* push extended number on 387 stack */
205 asm ("movl 12(%ebp),%eax");
206 asm ("fstpl (%eax)"); /* pop double */
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 */
217 unsigned short control;
219 unsigned short status;
224 unsigned short code_seg;
225 unsigned short opcode;
226 unsigned long operand;
227 unsigned short operand_seg;
229 unsigned char regs[8][10];
233 print_387_status (status, ep)
234 unsigned short status;
242 bothstatus = ((status != 0) && (ep->status != 0));
246 printf_unfiltered ("u: ");
247 print_387_status_word ((unsigned int)status);
253 printf_unfiltered ("e: ");
254 print_387_status_word ((unsigned int)ep->status);
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));
265 top = (ep->status >> 11) & 7;
267 printf_unfiltered (" regno tag msb lsb value\n");
268 for (fpreg = 7; fpreg >= 0; fpreg--)
275 unsigned short *usregs;
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.
281 st_regno = (fpreg + 8 - top) & 7;
283 printf_unfiltered ("%%st(%d) %s ", st_regno, fpreg == top ? "=>" : " ");
285 switch ((ep->tag >> (fpreg * 2)) & 3)
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;
292 for (i = 9; i >= 0; i--)
293 printf_unfiltered ("%02x", ep->regs[st_regno][i]);
295 printf_unfiltered (" ");
298 * Handle weird cases better than floatformat_to_double () and
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);
309 printf_unfiltered ("Pseudo Denormal (0 as a double)");
310 else if (mantissa_or == 0)
311 printf_unfiltered ("%s0", sign);
313 printf_unfiltered ("Denormal (0 as a double)");
315 else if (exp == 0x7fff)
318 printf_unfiltered ("Pseudo ");
319 if (mantissa_or == 0)
320 printf_unfiltered ("%sInf", sign);
322 printf_unfiltered ("%s NaN",
323 usregs[3] & 0x4000 ? "Quiet" : "Signaling");
325 printf_unfiltered (" (NaN)");
328 printf_unfiltered ("Unnormal (NaN)");
332 /* Use this we stop trapping on overflow. */
333 floatformat_to_double(&floatformat_i387_ext,
334 (char *) ep->regs[st_regno], &val);
336 i387_to_double((char *) ep->regs[st_regno], (char *) &val);
338 printf_unfiltered ("%g", val);
340 printf_unfiltered ("\n");
347 struct user u; /* just for address computations */
349 /* fpstate defined in <sys/user.h> */
350 struct fpstate *fpstatep;
351 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
354 unsigned int rounded_addr;
355 unsigned int rounded_size;
356 /*extern int corechan;*/
358 extern int inferior_pid;
360 uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
361 if (inferior_pid != 0 && core_bfd == NULL)
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];
368 fpstatep = &pcb_savefpu;
370 print_387_status (fpstatep->sv_ex_sw, (struct env387 *)fpstatep);
372 #endif /* FLOAT_INFO */
377 return (sizeof (struct user));
380 #ifdef SETUP_ARBITRARY_FRAME
383 setup_arbitrary_frame (argc, argv)
388 error ("i386 frame specifications require two arguments: sp and pc");
390 return create_new_frame (argv[0], argv[1]);
392 #endif /* SETUP_ARBITRARY_FRAME */
394 #ifdef HAVE_GREGSET_T
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);
420 #endif /* HAVE_GREGSET_T */
422 #ifdef HAVE_FPREGSET_T
427 memcpy (&pcb_savefpu, fp, sizeof pcb_savefpu);
429 #endif /* HAVE_FPREGSET_T */
431 /* Register that we are able to handle aout (trad-core) file formats. */
433 static struct core_fns aout_core_fns =
435 bfd_target_unknown_flavour,
436 fetch_core_registers,
441 _initialize_core_aout ()
443 add_core_fns (&aout_core_fns);
450 * 1: trace watchpoint requests
451 * 2: trace `watchpoint hit?' tests, too
453 #define WATCHPOINT_DEBUG 0
455 #include "breakpoint.h"
458 can_watch(type, cnt, ot)
462 static int cnt_watch, cnt_awatch;
466 case bp_hardware_watchpoint:
470 case bp_access_watchpoint:
479 rv = cnt_watch + cnt_awatch <= 4? 1: -1;
483 printf_filtered("can_watch(%d, %d, %d) = %d (counts: w: %d, rw: %d)\n",
484 type, cnt, ot, rv, cnt_watch, cnt_awatch);
491 stopped_by_watchpoint()
494 extern int inferior_pid;
496 if (current_target.to_shortname == 0 ||
497 ! (strcmp(current_target.to_shortname, "child") == 0 ||
498 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
501 if (inferior_pid != 0 && core_bfd == NULL)
503 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
505 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
507 perror("ptrace(PT_GETDBREGS) failed");
510 #if WATCHPOINT_DEBUG > 1
511 printf_filtered("stopped_by_watchpoint(): DR6 = %#x\n", dbr.dr6);
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
518 return dbr.dr6 & 0x0f;
522 warning("Can't set a watchpoint on a core file.");
528 insert_watchpoint(addr, len, type)
532 extern int inferior_pid;
534 if (current_target.to_shortname == 0 ||
535 ! (strcmp(current_target.to_shortname, "child") == 0 ||
536 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
539 if (inferior_pid != 0 && core_bfd == NULL)
541 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
545 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
547 perror("ptrace(PT_GETDBREGS) failed");
551 for (i = 0, mask = 0x03; i < 4; i++, mask <<= 2)
552 if ((dbr.dr7 & mask) == 0)
555 warning("no more hardware watchpoints available");
562 warning("watchpoint length %d unsupported, using lenght = 4",
568 warning("weird watchpoint length 3, using 2");
573 warning("weird watchpoint length 0, using 1");
579 case 1: sbits = 0; break;
580 case 2: sbits = 4; break;
581 case 4: sbits = 0x0c; break;
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.
594 warning("weird watchpoint type %d, using a write watchpoint");
604 sbits <<= 4 * i + 16;
608 *(&dbr.dr0 + i) = (unsigned int)addr;
611 printf_filtered("insert_watchpoint(), inserting DR7 = %#x, DR%d = %#x\n",
614 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
616 perror("ptrace(PT_SETDBREGS) failed");
622 warning("Can't set a watchpoint on a core file.");
628 remove_watchpoint(addr, len, type)
632 extern int inferior_pid;
634 if (current_target.to_shortname == 0 ||
635 ! (strcmp(current_target.to_shortname, "child") == 0 ||
636 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
639 if (inferior_pid != 0 && core_bfd == NULL)
641 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
643 unsigned int sbits, *dbregp;
645 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
647 perror("ptrace(PT_GETDBREGS) failed");
651 for (i = 0, dbregp = &dbr.dr0; i < 4; i++, dbregp++)
652 if (*dbregp == (unsigned int)addr)
656 warning("watchpoint for address %#x not found", addr);
661 sbits = 0xf << (4 * i + 16);
666 printf_filtered("remove_watchpoint(): removing watchpoint for %#x, DR7 = %#x\n",
669 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
671 perror("ptrace(PT_SETDBREGS) failed");
677 warning("Can't set a watchpoint on a core file.");
682 #endif /* PT_GETDBREGS */