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.3 2003/07/13 07:13:51 dillon 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 #if __FreeBSD_version >= 440000
143 addr = PCB_OFFSET + offsetof (struct pcb, pcb_save);
145 addr = PCB_OFFSET + offsetof (struct pcb, pcb_savefpu);
147 memcpy (&pcb_savefpu, core_reg_sect + addr, sizeof pcb_savefpu);
151 #include "expression.h"
152 #include "language.h" /* for local_hex_string */
153 #include "floatformat.h"
155 #include <sys/param.h>
157 #include <sys/ioctl.h>
162 #include <sys/time.h>
163 #include <sys/resource.h>
165 #define curpcb Xcurpcb /* XXX avoid leaking declaration from pcb.h */
166 #include <sys/user.h>
168 #include <sys/file.h>
169 #include "gdb_stat.h"
170 #include <sys/ptrace.h>
172 extern void print_387_control_word (); /* i387-tdep.h */
173 extern void print_387_status_word ();
175 #define fpstate save87
176 #if __FreeBSD_version >= 440000
177 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_save.sv_87)
179 #define U_FPSTATE(u) (((struct pcb *)((char *)&u + PCB_OFFSET))->pcb_savefpu)
183 i387_to_double (from, to)
188 /* push extended mode on 387 stack, then pop in double mode
190 * first, set exception masks so no error is generated -
191 * number will be rounded to inf or 0, if necessary
193 asm ("pushl %eax"); /* grab a stack slot */
194 asm ("fstcw (%esp)"); /* get 387 control word */
195 asm ("movl (%esp),%eax"); /* save old value */
196 asm ("orl $0x3f,%eax"); /* mask all exceptions */
198 asm ("fldcw (%esp)"); /* load new value into 387 */
200 asm ("movl 8(%ebp),%eax");
201 asm ("fldt (%eax)"); /* push extended number on 387 stack */
203 asm ("movl 12(%ebp),%eax");
204 asm ("fstpl (%eax)"); /* pop double */
207 asm ("popl %eax"); /* flush modified control word */
208 asm ("fnclex"); /* clear exceptions */
209 asm ("fldcw (%esp)"); /* restore original control word */
210 asm ("popl %eax"); /* flush saved copy */
215 unsigned short control;
217 unsigned short status;
222 unsigned short code_seg;
223 unsigned short opcode;
224 unsigned long operand;
225 unsigned short operand_seg;
227 unsigned char regs[8][10];
231 print_387_status (status, ep)
232 unsigned short status;
240 bothstatus = ((status != 0) && (ep->status != 0));
244 printf_unfiltered ("u: ");
245 print_387_status_word ((unsigned int)status);
251 printf_unfiltered ("e: ");
252 print_387_status_word ((unsigned int)ep->status);
255 print_387_control_word ((unsigned int)ep->control);
256 printf_unfiltered ("last instruction: ");
257 printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
258 printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
259 printf_unfiltered ("%s; ", local_hex_string(ep->eip));
260 printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
261 printf_unfiltered (":%s\n", local_hex_string(ep->operand));
263 top = (ep->status >> 11) & 7;
265 printf_unfiltered (" regno tag msb lsb value\n");
266 for (fpreg = 7; fpreg >= 0; fpreg--)
273 unsigned short *usregs;
276 /* The physical regno `fpreg' is only relevant as an index into the
277 * tag word. Logical `%st' numbers are required for indexing ep->regs.
279 st_regno = (fpreg + 8 - top) & 7;
281 printf_unfiltered ("%%st(%d) %s ", st_regno, fpreg == top ? "=>" : " ");
283 switch ((ep->tag >> (fpreg * 2)) & 3)
285 case 0: printf_unfiltered ("valid "); break;
286 case 1: printf_unfiltered ("zero "); break;
287 case 2: printf_unfiltered ("trap "); break;
288 case 3: printf_unfiltered ("empty "); break;
290 for (i = 9; i >= 0; i--)
291 printf_unfiltered ("%02x", ep->regs[st_regno][i]);
293 printf_unfiltered (" ");
296 * Handle weird cases better than floatformat_to_double () and
299 usregs = (unsigned short *) ep->regs[st_regno];
300 sign = usregs[4] & 0x8000 ? "-" : "";
301 exp = usregs[4] & 0x7fff;
302 normal = usregs[3] & 0x8000;
303 mantissa_or = usregs[0] | usregs[1] | usregs[2] | (usregs[3] & 0x7fff);
307 printf_unfiltered ("Pseudo Denormal (0 as a double)");
308 else if (mantissa_or == 0)
309 printf_unfiltered ("%s0", sign);
311 printf_unfiltered ("Denormal (0 as a double)");
313 else if (exp == 0x7fff)
316 printf_unfiltered ("Pseudo ");
317 if (mantissa_or == 0)
318 printf_unfiltered ("%sInf", sign);
320 printf_unfiltered ("%s NaN",
321 usregs[3] & 0x4000 ? "Quiet" : "Signaling");
323 printf_unfiltered (" (NaN)");
326 printf_unfiltered ("Unnormal (NaN)");
330 /* Use this we stop trapping on overflow. */
331 floatformat_to_double(&floatformat_i387_ext,
332 (char *) ep->regs[st_regno], &val);
334 i387_to_double((char *) ep->regs[st_regno], (char *) &val);
336 printf_unfiltered ("%g", val);
338 printf_unfiltered ("\n");
345 struct user u; /* just for address computations */
347 /* fpstate defined in <sys/user.h> */
348 struct fpstate *fpstatep;
349 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
352 unsigned int rounded_addr;
353 unsigned int rounded_size;
354 /*extern int corechan;*/
356 extern int inferior_pid;
358 uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
359 if (inferior_pid != 0 && core_bfd == NULL)
361 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
362 ptrace(PT_GETFPREGS, pid, &buf[0], sizeof(struct fpreg));
363 fpstatep = (struct fpstate *)&buf[0];
366 fpstatep = &pcb_savefpu;
368 print_387_status (fpstatep->sv_ex_sw, (struct env387 *)fpstatep);
370 #endif /* FLOAT_INFO */
375 return (sizeof (struct user));
378 #ifdef SETUP_ARBITRARY_FRAME
381 setup_arbitrary_frame (argc, argv)
386 error ("i386 frame specifications require two arguments: sp and pc");
388 return create_new_frame (argv[0], argv[1]);
390 #endif /* SETUP_ARBITRARY_FRAME */
392 #ifdef HAVE_GREGSET_T
399 /* These must be ordered the same as REGISTER_NAMES in
400 config/i386/tm-i386.h. */
401 supply_register (regno++, (char *)&gp->r_eax);
402 supply_register (regno++, (char *)&gp->r_ecx);
403 supply_register (regno++, (char *)&gp->r_edx);
404 supply_register (regno++, (char *)&gp->r_ebx);
405 supply_register (regno++, (char *)&gp->r_esp);
406 supply_register (regno++, (char *)&gp->r_ebp);
407 supply_register (regno++, (char *)&gp->r_esi);
408 supply_register (regno++, (char *)&gp->r_edi);
409 supply_register (regno++, (char *)&gp->r_eip);
410 supply_register (regno++, (char *)&gp->r_eflags);
411 supply_register (regno++, (char *)&gp->r_cs);
412 supply_register (regno++, (char *)&gp->r_ss);
413 supply_register (regno++, (char *)&gp->r_ds);
414 supply_register (regno++, (char *)&gp->r_es);
415 supply_register (regno++, (char *)&gp->r_fs);
416 supply_register (regno++, (char *)&gp->r_gs);
418 #endif /* HAVE_GREGSET_T */
420 #ifdef HAVE_FPREGSET_T
425 memcpy (&pcb_savefpu, fp, sizeof pcb_savefpu);
427 #endif /* HAVE_FPREGSET_T */
429 /* Register that we are able to handle aout (trad-core) file formats. */
431 static struct core_fns aout_core_fns =
433 bfd_target_unknown_flavour,
434 fetch_core_registers,
439 _initialize_core_aout ()
441 add_core_fns (&aout_core_fns);
448 * 1: trace watchpoint requests
449 * 2: trace `watchpoint hit?' tests, too
451 #define WATCHPOINT_DEBUG 0
453 #include "breakpoint.h"
456 can_watch(type, cnt, ot)
460 static int cnt_watch, cnt_awatch;
464 case bp_hardware_watchpoint:
468 case bp_access_watchpoint:
477 rv = cnt_watch + cnt_awatch <= 4? 1: -1;
481 printf_filtered("can_watch(%d, %d, %d) = %d (counts: w: %d, rw: %d)\n",
482 type, cnt, ot, rv, cnt_watch, cnt_awatch);
489 stopped_by_watchpoint()
492 extern int inferior_pid;
494 if (current_target.to_shortname == 0 ||
495 ! (strcmp(current_target.to_shortname, "child") == 0 ||
496 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
499 if (inferior_pid != 0 && core_bfd == NULL)
501 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
503 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
505 perror("ptrace(PT_GETDBREGS) failed");
508 #if WATCHPOINT_DEBUG > 1
509 printf_filtered("stopped_by_watchpoint(): DR6 = %#x\n", dbr.dr6);
512 * If a hardware watchpoint was hit, one of the lower 4 bits in
513 * DR6 is set (the actual bit indicates which of DR0...DR3 triggered
516 return dbr.dr6 & 0x0f;
520 warning("Can't set a watchpoint on a core file.");
526 insert_watchpoint(addr, len, type)
530 extern int inferior_pid;
532 if (current_target.to_shortname == 0 ||
533 ! (strcmp(current_target.to_shortname, "child") == 0 ||
534 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
537 if (inferior_pid != 0 && core_bfd == NULL)
539 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
543 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
545 perror("ptrace(PT_GETDBREGS) failed");
549 for (i = 0, mask = 0x03; i < 4; i++, mask <<= 2)
550 if ((dbr.dr7 & mask) == 0)
553 warning("no more hardware watchpoints available");
560 warning("watchpoint length %d unsupported, using lenght = 4",
566 warning("weird watchpoint length 3, using 2");
571 warning("weird watchpoint length 0, using 1");
577 case 1: sbits = 0; break;
578 case 2: sbits = 4; break;
579 case 4: sbits = 0x0c; break;
583 * The `type' value is 0 for `watch on write', 1 for `watch on
584 * read', 2 for `watch on both'. The i386 debug register
585 * breakpoint types are 0 for `execute' (not used in GDB), 1 for
586 * `write', and 4 for `read/write'. Plain `read' trapping is
587 * not supported on i386, value 3 is illegal.
592 warning("weird watchpoint type %d, using a write watchpoint");
602 sbits <<= 4 * i + 16;
606 *(&dbr.dr0 + i) = (unsigned int)addr;
609 printf_filtered("insert_watchpoint(), inserting DR7 = %#x, DR%d = %#x\n",
612 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
614 perror("ptrace(PT_SETDBREGS) failed");
620 warning("Can't set a watchpoint on a core file.");
626 remove_watchpoint(addr, len, type)
630 extern int inferior_pid;
632 if (current_target.to_shortname == 0 ||
633 ! (strcmp(current_target.to_shortname, "child") == 0 ||
634 strcmp(current_target.to_shortname, "freebsd-uthreads") == 0))
637 if (inferior_pid != 0 && core_bfd == NULL)
639 int pid = inferior_pid & ((1 << 17) - 1); /* XXX extract pid from tid */
641 unsigned int sbits, *dbregp;
643 if (ptrace(PT_GETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
645 perror("ptrace(PT_GETDBREGS) failed");
649 for (i = 0, dbregp = &dbr.dr0; i < 4; i++, dbregp++)
650 if (*dbregp == (unsigned int)addr)
654 warning("watchpoint for address %#x not found", addr);
659 sbits = 0xf << (4 * i + 16);
664 printf_filtered("remove_watchpoint(): removing watchpoint for %#x, DR7 = %#x\n",
667 if (ptrace(PT_SETDBREGS, pid, (caddr_t)&dbr, 0) == -1)
669 perror("ptrace(PT_SETDBREGS) failed");
675 warning("Can't set a watchpoint on a core file.");
680 #endif /* PT_GETDBREGS */