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 $
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 addr = PCB_OFFSET + offsetof (struct pcb, pcb_save);
145 memcpy (&pcb_savefpu, core_reg_sect + addr, sizeof pcb_savefpu);
149 #include "expression.h"
150 #include "language.h" /* for local_hex_string */
151 #include "floatformat.h"
153 #include <sys/param.h>
155 #include <sys/ioctl.h>
160 #include <sys/time.h>
161 #include <sys/resource.h>
163 #define curpcb Xcurpcb /* XXX avoid leaking declaration from pcb.h */
164 #include <sys/user.h>
166 #include <sys/file.h>
167 #include "gdb_stat.h"
168 #include <sys/ptrace.h>
170 extern void print_387_control_word (); /* i387-tdep.h */
171 extern void print_387_status_word ();
173 #define fpstate save87
174 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_save.sv_87)
177 i387_to_double (from, to)
182 /* push extended mode on 387 stack, then pop in double mode
184 * first, set exception masks so no error is generated -
185 * number will be rounded to inf or 0, if necessary
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 */
192 asm ("fldcw (%esp)"); /* load new value into 387 */
194 asm ("movl 8(%ebp),%eax");
195 asm ("fldt (%eax)"); /* push extended number on 387 stack */
197 asm ("movl 12(%ebp),%eax");
198 asm ("fstpl (%eax)"); /* pop double */
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 */
209 unsigned short control;
211 unsigned short status;
216 unsigned short code_seg;
217 unsigned short opcode;
218 unsigned long operand;
219 unsigned short operand_seg;
221 unsigned char regs[8][10];
225 print_387_status (status, ep)
226 unsigned short status;
234 bothstatus = ((status != 0) && (ep->status != 0));
238 printf_unfiltered ("u: ");
239 print_387_status_word ((unsigned int)status);
245 printf_unfiltered ("e: ");
246 print_387_status_word ((unsigned int)ep->status);
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));
257 top = (ep->status >> 11) & 7;
259 printf_unfiltered (" regno tag msb lsb value\n");
260 for (fpreg = 7; fpreg >= 0; fpreg--)
267 unsigned short *usregs;
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.
273 st_regno = (fpreg + 8 - top) & 7;
275 printf_unfiltered ("%%st(%d) %s ", st_regno, fpreg == top ? "=>" : " ");
277 switch ((ep->tag >> (fpreg * 2)) & 3)
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;
284 for (i = 9; i >= 0; i--)
285 printf_unfiltered ("%02x", ep->regs[st_regno][i]);
287 printf_unfiltered (" ");
290 * Handle weird cases better than floatformat_to_double () and
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);
301 printf_unfiltered ("Pseudo Denormal (0 as a double)");
302 else if (mantissa_or == 0)
303 printf_unfiltered ("%s0", sign);
305 printf_unfiltered ("Denormal (0 as a double)");
307 else if (exp == 0x7fff)
310 printf_unfiltered ("Pseudo ");
311 if (mantissa_or == 0)
312 printf_unfiltered ("%sInf", sign);
314 printf_unfiltered ("%s NaN",
315 usregs[3] & 0x4000 ? "Quiet" : "Signaling");
317 printf_unfiltered (" (NaN)");
320 printf_unfiltered ("Unnormal (NaN)");
324 /* Use this we stop trapping on overflow. */
325 floatformat_to_double(&floatformat_i387_ext,
326 (char *) ep->regs[st_regno], &val);
328 i387_to_double((char *) ep->regs[st_regno], (char *) &val);
330 printf_unfiltered ("%g", val);
332 printf_unfiltered ("\n");
339 struct user u; /* just for address computations */
341 /* fpstate defined in <sys/user.h> */
342 struct fpstate *fpstatep;
343 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
346 unsigned int rounded_addr;
347 unsigned int rounded_size;
348 /*extern int corechan;*/
350 extern int inferior_pid;
352 uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
353 if (inferior_pid != 0 && core_bfd == NULL)
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];
360 fpstatep = &pcb_savefpu;
362 print_387_status (fpstatep->sv_ex_sw, (struct env387 *)fpstatep);
364 #endif /* FLOAT_INFO */
369 return (sizeof (struct user));
372 #ifdef SETUP_ARBITRARY_FRAME
375 setup_arbitrary_frame (argc, argv)
380 error ("i386 frame specifications require two arguments: sp and pc");
382 return create_new_frame (argv[0], argv[1]);
384 #endif /* SETUP_ARBITRARY_FRAME */
386 #ifdef HAVE_GREGSET_T
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);
412 #endif /* HAVE_GREGSET_T */
414 #ifdef HAVE_FPREGSET_T
419 memcpy (&pcb_savefpu, fp, sizeof pcb_savefpu);
421 #endif /* HAVE_FPREGSET_T */
423 /* Register that we are able to handle aout (trad-core) file formats. */
425 static struct core_fns aout_core_fns =
427 bfd_target_unknown_flavour,
428 fetch_core_registers,
433 _initialize_core_aout ()
435 add_core_fns (&aout_core_fns);
442 * 1: trace watchpoint requests
443 * 2: trace `watchpoint hit?' tests, too
445 #define WATCHPOINT_DEBUG 0
447 #include "breakpoint.h"
450 can_watch(type, cnt, ot)
454 static int cnt_watch, cnt_awatch;
458 case bp_hardware_watchpoint:
462 case bp_access_watchpoint:
471 rv = cnt_watch + cnt_awatch <= 4? 1: -1;
475 printf_filtered("can_watch(%d, %d, %d) = %d (counts: w: %d, rw: %d)\n",
476 type, cnt, ot, rv, cnt_watch, cnt_awatch);
483 stopped_by_watchpoint()
486 extern int inferior_pid;
488 if (current_target.to_shortname == 0 ||
489 ! (strcmp(current_target.to_shortname, "child") == 0 ||
490 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
493 if (inferior_pid != 0 && core_bfd == NULL)
495 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
497 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
499 perror("ptrace(PT_GETDBREGS) failed");
502 #if WATCHPOINT_DEBUG > 1
503 printf_filtered("stopped_by_watchpoint(): DR6 = %#x\n", dbr.dr6);
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
510 return dbr.dr6 & 0x0f;
514 warning("Can't set a watchpoint on a core file.");
520 insert_watchpoint(addr, len, type)
524 extern int inferior_pid;
526 if (current_target.to_shortname == 0 ||
527 ! (strcmp(current_target.to_shortname, "child") == 0 ||
528 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
531 if (inferior_pid != 0 && core_bfd == NULL)
533 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
537 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
539 perror("ptrace(PT_GETDBREGS) failed");
543 for (i = 0, mask = 0x03; i < 4; i++, mask <<= 2)
544 if ((dbr.dr7 & mask) == 0)
547 warning("no more hardware watchpoints available");
554 warning("watchpoint length %d unsupported, using lenght = 4",
560 warning("weird watchpoint length 3, using 2");
565 warning("weird watchpoint length 0, using 1");
571 case 1: sbits = 0; break;
572 case 2: sbits = 4; break;
573 case 4: sbits = 0x0c; break;
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.
586 warning("weird watchpoint type %d, using a write watchpoint");
596 sbits <<= 4 * i + 16;
600 *(&dbr.dr0 + i) = (unsigned int)addr;
603 printf_filtered("insert_watchpoint(), inserting DR7 = %#x, DR%d = %#x\n",
606 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
608 perror("ptrace(PT_SETDBREGS) failed");
614 warning("Can't set a watchpoint on a core file.");
620 remove_watchpoint(addr, len, type)
624 extern int inferior_pid;
626 if (current_target.to_shortname == 0 ||
627 ! (strcmp(current_target.to_shortname, "child") == 0 ||
628 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
631 if (inferior_pid != 0 && core_bfd == NULL)
633 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
635 unsigned int sbits, *dbregp;
637 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
639 perror("ptrace(PT_GETDBREGS) failed");
643 for (i = 0, dbregp = &dbr.dr0; i < 4; i++, dbregp++)
644 if (*dbregp == (unsigned int)addr)
648 warning("watchpoint for address %#x not found", addr);
653 sbits = 0xf << (4 * i + 16);
658 printf_filtered("remove_watchpoint(): removing watchpoint for %#x, DR7 = %#x\n",
661 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
663 perror("ptrace(PT_SETDBREGS) failed");
669 warning("Can't set a watchpoint on a core file.");
674 #endif /* PT_GETDBREGS */