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.5 2004/08/09 13:55:35 eirikn 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.
6 This file is part of GDB.
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.
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.
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. */
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>
34 #if defined(HAVE_GREGSET_T) || defined(HAVE_FPREGSET_T)
35 #include <sys/procfs.h>
38 #define PCB_OFFSET (UPAGES * PAGE_SIZE - sizeof(struct pcb))
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[] =
44 tEAX, tECX, tEDX, tEBX,
45 tESP, tEBP, tESI, tEDI,
46 tEIP, tEFLAGS, tCS, tSS,
50 static struct save87 pcb_savefpu;
53 fetch_inferior_registers (regno)
56 struct reg inferior_registers; /* ptrace order, not gcc/gdb order */
59 ptrace (PT_GETREGS, inferior_pid,
60 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
62 for (r = 0; r < NUM_REGS; r++)
63 memcpy (®isters[REGISTER_BYTE (r)], ((int *)&inferior_registers) + tregmap[r], 4);
69 store_inferior_registers (regno)
72 struct reg inferior_registers; /* ptrace order, not gcc/gdb order */
75 for (r = 0; r < NUM_REGS; r++)
76 memcpy (((int *)&inferior_registers) + tregmap[r], ®isters[REGISTER_BYTE (r)], 4);
78 ptrace (PT_SETREGS, inferior_pid,
79 (PTRACE_ARG3_TYPE) &inferior_registers, 0);
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.
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.
98 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
100 unsigned core_reg_size;
109 struct user *tmp_uaddr;
112 * First get virtual address of user structure. Then calculate offset.
116 &((struct user *) core_reg_sect)->u_kproc.kp_proc.p_addr,
118 offset = -reg_addr - (int) tmp_uaddr;
120 for (regno = 0; regno < NUM_REGS; regno++)
122 cregno = tregmap[regno];
124 addr = PCB_OFFSET + offsetof (struct pcb, pcb_gs);
126 addr = offset + 4 * cregno;
127 if (addr < 0 || addr >= core_reg_size)
134 supply_register (regno, core_reg_sect + addr);
139 error ("Register %s not found in core file.", gdb_register_names[bad_reg]);
142 addr = PCB_OFFSET + offsetof (struct pcb, pcb_save);
143 memcpy (&pcb_savefpu, core_reg_sect + addr, sizeof pcb_savefpu);
147 #include "expression.h"
148 #include "language.h" /* for local_hex_string */
149 #include "floatformat.h"
151 #include <sys/param.h>
153 #include <sys/ioctl.h>
158 #include <sys/time.h>
159 #include <sys/resource.h>
161 #define curpcb Xcurpcb /* XXX avoid leaking declaration from pcb.h */
162 #include <sys/user.h>
164 #include <sys/file.h>
165 #include "gdb_stat.h"
166 #include <sys/ptrace.h>
168 extern void print_387_control_word (); /* i387-tdep.h */
169 extern void print_387_status_word ();
171 #define fpstate save87
172 #if __FreeBSD_version >= 440000 || defined(__DragonFly__)
173 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_save.sv_87)
175 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_savefpu)
179 i387_to_double (from, to)
184 /* push extended mode on 387 stack, then pop in double mode
186 * first, set exception masks so no error is generated -
187 * number will be rounded to inf or 0, if necessary
189 asm ("pushl %eax"); /* grab a stack slot */
190 asm ("fstcw (%esp)"); /* get 387 control word */
191 asm ("movl (%esp),%eax"); /* save old value */
192 asm ("orl $0x3f,%eax"); /* mask all exceptions */
194 asm ("fldcw (%esp)"); /* load new value into 387 */
196 asm ("movl 8(%ebp),%eax");
197 asm ("fldt (%eax)"); /* push extended number on 387 stack */
199 asm ("movl 12(%ebp),%eax");
200 asm ("fstpl (%eax)"); /* pop double */
203 asm ("popl %eax"); /* flush modified control word */
204 asm ("fnclex"); /* clear exceptions */
205 asm ("fldcw (%esp)"); /* restore original control word */
206 asm ("popl %eax"); /* flush saved copy */
211 unsigned short control;
213 unsigned short status;
218 unsigned short code_seg;
219 unsigned short opcode;
220 unsigned long operand;
221 unsigned short operand_seg;
223 unsigned char regs[8][10];
227 print_387_status (status, ep)
228 unsigned short status;
236 bothstatus = ((status != 0) && (ep->status != 0));
240 printf_unfiltered ("u: ");
241 print_387_status_word ((unsigned int)status);
247 printf_unfiltered ("e: ");
248 print_387_status_word ((unsigned int)ep->status);
251 print_387_control_word ((unsigned int)ep->control);
252 printf_unfiltered ("last instruction: ");
253 printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
254 printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
255 printf_unfiltered ("%s; ", local_hex_string(ep->eip));
256 printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
257 printf_unfiltered (":%s\n", local_hex_string(ep->operand));
259 top = (ep->status >> 11) & 7;
261 printf_unfiltered (" regno tag msb lsb value\n");
262 for (fpreg = 7; fpreg >= 0; fpreg--)
269 unsigned short *usregs;
272 /* The physical regno `fpreg' is only relevant as an index into the
273 * tag word. Logical `%st' numbers are required for indexing ep->regs.
275 st_regno = (fpreg + 8 - top) & 7;
277 printf_unfiltered ("%%st(%d) %s ", st_regno, fpreg == top ? "=>" : " ");
279 switch ((ep->tag >> (fpreg * 2)) & 3)
281 case 0: printf_unfiltered ("valid "); break;
282 case 1: printf_unfiltered ("zero "); break;
283 case 2: printf_unfiltered ("trap "); break;
284 case 3: printf_unfiltered ("empty "); break;
286 for (i = 9; i >= 0; i--)
287 printf_unfiltered ("%02x", ep->regs[st_regno][i]);
289 printf_unfiltered (" ");
292 * Handle weird cases better than floatformat_to_double () and
295 usregs = (unsigned short *) ep->regs[st_regno];
296 sign = usregs[4] & 0x8000 ? "-" : "";
297 exp = usregs[4] & 0x7fff;
298 normal = usregs[3] & 0x8000;
299 mantissa_or = usregs[0] | usregs[1] | usregs[2] | (usregs[3] & 0x7fff);
303 printf_unfiltered ("Pseudo Denormal (0 as a double)");
304 else if (mantissa_or == 0)
305 printf_unfiltered ("%s0", sign);
307 printf_unfiltered ("Denormal (0 as a double)");
309 else if (exp == 0x7fff)
312 printf_unfiltered ("Pseudo ");
313 if (mantissa_or == 0)
314 printf_unfiltered ("%sInf", sign);
316 printf_unfiltered ("%s NaN",
317 usregs[3] & 0x4000 ? "Quiet" : "Signaling");
319 printf_unfiltered (" (NaN)");
322 printf_unfiltered ("Unnormal (NaN)");
326 /* Use this we stop trapping on overflow. */
327 floatformat_to_double(&floatformat_i387_ext,
328 (char *) ep->regs[st_regno], &val);
330 i387_to_double((char *) ep->regs[st_regno], (char *) &val);
332 printf_unfiltered ("%g", val);
334 printf_unfiltered ("\n");
341 struct user u; /* just for address computations */
343 /* fpstate defined in <sys/user.h> */
344 struct fpstate *fpstatep;
345 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
348 unsigned int rounded_addr;
349 unsigned int rounded_size;
350 /*extern int corechan;*/
352 extern int inferior_pid;
354 uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
355 if (inferior_pid != 0 && core_bfd == NULL)
357 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
358 ptrace(PT_GETFPREGS, pid, &buf[0], sizeof(struct fpreg));
359 fpstatep = (struct fpstate *)&buf[0];
362 fpstatep = &pcb_savefpu;
364 print_387_status (fpstatep->sv_ex_sw, (struct env387 *)fpstatep);
366 #endif /* FLOAT_INFO */
371 return (sizeof (struct user));
374 #ifdef SETUP_ARBITRARY_FRAME
377 setup_arbitrary_frame (argc, argv)
382 error ("i386 frame specifications require two arguments: sp and pc");
384 return create_new_frame (argv[0], argv[1]);
386 #endif /* SETUP_ARBITRARY_FRAME */
388 #ifdef HAVE_GREGSET_T
395 /* These must be ordered the same as REGISTER_NAMES in
396 config/i386/tm-i386.h. */
397 supply_register (regno++, (char *)&gp->r_eax);
398 supply_register (regno++, (char *)&gp->r_ecx);
399 supply_register (regno++, (char *)&gp->r_edx);
400 supply_register (regno++, (char *)&gp->r_ebx);
401 supply_register (regno++, (char *)&gp->r_esp);
402 supply_register (regno++, (char *)&gp->r_ebp);
403 supply_register (regno++, (char *)&gp->r_esi);
404 supply_register (regno++, (char *)&gp->r_edi);
405 supply_register (regno++, (char *)&gp->r_eip);
406 supply_register (regno++, (char *)&gp->r_eflags);
407 supply_register (regno++, (char *)&gp->r_cs);
408 supply_register (regno++, (char *)&gp->r_ss);
409 supply_register (regno++, (char *)&gp->r_ds);
410 supply_register (regno++, (char *)&gp->r_es);
411 supply_register (regno++, (char *)&gp->r_fs);
412 supply_register (regno++, (char *)&gp->r_gs);
414 #endif /* HAVE_GREGSET_T */
416 #ifdef HAVE_FPREGSET_T
421 memcpy (&pcb_savefpu, fp, sizeof pcb_savefpu);
423 #endif /* HAVE_FPREGSET_T */
425 /* Register that we are able to handle aout (trad-core) file formats. */
427 static struct core_fns aout_core_fns =
429 bfd_target_unknown_flavour,
430 fetch_core_registers,
435 _initialize_core_aout ()
437 add_core_fns (&aout_core_fns);
444 * 1: trace watchpoint requests
445 * 2: trace `watchpoint hit?' tests, too
447 #define WATCHPOINT_DEBUG 0
449 #include "breakpoint.h"
452 can_watch(type, cnt, ot)
456 static int cnt_watch, cnt_awatch;
460 case bp_hardware_watchpoint:
464 case bp_access_watchpoint:
473 rv = cnt_watch + cnt_awatch <= 4? 1: -1;
477 printf_filtered("can_watch(%d, %d, %d) = %d (counts: w: %d, rw: %d)\n",
478 type, cnt, ot, rv, cnt_watch, cnt_awatch);
485 stopped_by_watchpoint()
488 extern int inferior_pid;
490 if (current_target.to_shortname == 0 ||
491 ! (strcmp(current_target.to_shortname, "child") == 0 ||
492 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
495 if (inferior_pid != 0 && core_bfd == NULL)
497 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
499 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
501 perror("ptrace(PT_GETDBREGS) failed");
504 #if WATCHPOINT_DEBUG > 1
505 printf_filtered("stopped_by_watchpoint(): DR6 = %#x\n", dbr.dr6);
508 * If a hardware watchpoint was hit, one of the lower 4 bits in
509 * DR6 is set (the actual bit indicates which of DR0...DR3 triggered
512 return dbr.dr6 & 0x0f;
516 warning("Can't set a watchpoint on a core file.");
522 insert_watchpoint(addr, len, type)
526 extern int inferior_pid;
528 if (current_target.to_shortname == 0 ||
529 ! (strcmp(current_target.to_shortname, "child") == 0 ||
530 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
533 if (inferior_pid != 0 && core_bfd == NULL)
535 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
539 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
541 perror("ptrace(PT_GETDBREGS) failed");
545 for (i = 0, mask = 0x03; i < 4; i++, mask <<= 2)
546 if ((dbr.dr7 & mask) == 0)
549 warning("no more hardware watchpoints available");
556 warning("watchpoint length %d unsupported, using lenght = 4",
562 warning("weird watchpoint length 3, using 2");
567 warning("weird watchpoint length 0, using 1");
573 case 1: sbits = 0; break;
574 case 2: sbits = 4; break;
575 case 4: sbits = 0x0c; break;
579 * The `type' value is 0 for `watch on write', 1 for `watch on
580 * read', 2 for `watch on both'. The i386 debug register
581 * breakpoint types are 0 for `execute' (not used in GDB), 1 for
582 * `write', and 4 for `read/write'. Plain `read' trapping is
583 * not supported on i386, value 3 is illegal.
588 warning("weird watchpoint type %d, using a write watchpoint");
598 sbits <<= 4 * i + 16;
602 *(&dbr.dr0 + i) = (unsigned int)addr;
605 printf_filtered("insert_watchpoint(), inserting DR7 = %#x, DR%d = %#x\n",
608 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
610 perror("ptrace(PT_SETDBREGS) failed");
616 warning("Can't set a watchpoint on a core file.");
622 remove_watchpoint(addr, len, type)
626 extern int inferior_pid;
628 if (current_target.to_shortname == 0 ||
629 ! (strcmp(current_target.to_shortname, "child") == 0 ||
630 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
633 if (inferior_pid != 0 && core_bfd == NULL)
635 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
637 unsigned int sbits, *dbregp;
639 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
641 perror("ptrace(PT_GETDBREGS) failed");
645 for (i = 0, dbregp = &dbr.dr0; i < 4; i++, dbregp++)
646 if (*dbregp == (unsigned int)addr)
650 warning("watchpoint for address %#x not found", addr);
655 sbits = 0xf << (4 * i + 16);
660 printf_filtered("remove_watchpoint(): removing watchpoint for %#x, DR7 = %#x\n",
663 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
665 perror("ptrace(PT_SETDBREGS) failed");
671 warning("Can't set a watchpoint on a core file.");
676 #endif /* PT_GETDBREGS */