1/* Native-dependent code for FreeBSD/i386. 2 3 Copyright (C) 2001, 2002, 2003, 2004, 2007 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20#include "defs.h" 21#include "inferior.h" 22#include "regcache.h" 23#include "target.h" 24 25#include <sys/types.h> 26#include <sys/ptrace.h> 27#include <sys/sysctl.h> 28 29#include "fbsd-nat.h" 30#include "i386-tdep.h" 31#include "i386bsd-nat.h" 32 33/* Resume execution of the inferior process. If STEP is nonzero, 34 single-step it. If SIGNAL is nonzero, give it that signal. */ 35 36static void 37i386fbsd_resume (ptid_t ptid, int step, enum target_signal signal) 38{ 39 pid_t pid = ptid_get_pid (ptid); 40 int request = PT_STEP; 41 42 if (pid == -1) 43 /* Resume all threads. This only gets used in the non-threaded 44 case, where "resume all threads" and "resume inferior_ptid" are 45 the same. */ 46 pid = ptid_get_pid (inferior_ptid); 47 48 if (!step) 49 { 50 struct regcache *regcache = get_current_regcache (); 51 ULONGEST eflags; 52 53 /* Workaround for a bug in FreeBSD. Make sure that the trace 54 flag is off when doing a continue. There is a code path 55 through the kernel which leaves the flag set when it should 56 have been cleared. If a process has a signal pending (such 57 as SIGALRM) and we do a PT_STEP, the process never really has 58 a chance to run because the kernel needs to notify the 59 debugger that a signal is being sent. Therefore, the process 60 never goes through the kernel's trap() function which would 61 normally clear it. */ 62 63 regcache_cooked_read_unsigned (regcache, I386_EFLAGS_REGNUM, 64 &eflags); 65 if (eflags & 0x0100) 66 regcache_cooked_write_unsigned (regcache, I386_EFLAGS_REGNUM, 67 eflags & ~0x0100); 68 69 request = PT_CONTINUE; 70 } 71 72 /* An addres of (caddr_t) 1 tells ptrace to continue from where it 73 was. (If GDB wanted it to start some other way, we have already 74 written a new PC value to the child.) */ 75 if (ptrace (request, pid, (caddr_t) 1, 76 target_signal_to_host (signal)) == -1) 77 perror_with_name (("ptrace")); 78} 79 80 81/* Support for debugging kernel virtual memory images. */ 82 83#include <sys/types.h> 84#include <machine/pcb.h> 85 86#include "bsd-kvm.h" 87 88static int 89i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) 90{ 91 /* The following is true for FreeBSD 4.7: 92 93 The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs. 94 This accounts for all callee-saved registers specified by the 95 psABI and then some. Here %esp contains the stack pointer at the 96 point just after the call to cpu_switch(). From this information 97 we reconstruct the register state as it would look when we just 98 returned from cpu_switch(). */ 99 100 /* The stack pointer shouldn't be zero. */ 101 if (pcb->pcb_esp == 0) 102 return 0; 103 104 pcb->pcb_esp += 4; 105 regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi); 106 regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi); 107 regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp); 108 regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp); 109 regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx); 110 regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip); 111 regcache_raw_supply (regcache, I386_GS_REGNUM, &pcb->pcb_gs); 112 113 return 1; 114} 115 116 117/* Prevent warning from -Wmissing-prototypes. */ 118void _initialize_i386fbsd_nat (void); 119 120void 121_initialize_i386fbsd_nat (void) 122{ 123 struct target_ops *t; 124 125 /* Add some extra features to the common *BSD/i386 target. */ 126 t = i386bsd_target (); 127 t->to_resume = i386fbsd_resume; 128 t->to_pid_to_exec_file = fbsd_pid_to_exec_file; 129 t->to_find_memory_regions = fbsd_find_memory_regions; 130 t->to_make_corefile_notes = fbsd_make_corefile_notes; 131 add_target (t); 132 133 /* Support debugging kernel virtual memory images. */ 134 bsd_kvm_add_target (i386fbsd_supply_pcb); 135 136 /* FreeBSD provides a kern.ps_strings sysctl that we can use to 137 locate the sigtramp. That way we can still recognize a sigtramp 138 if its location is changed in a new kernel. Of course this is 139 still based on the assumption that the sigtramp is placed 140 directly under the location where the program arguments and 141 environment can be found. */ 142#ifdef KERN_PS_STRINGS 143 { 144 int mib[2]; 145 u_long ps_strings; 146 size_t len; 147 148 mib[0] = CTL_KERN; 149 mib[1] = KERN_PS_STRINGS; 150 len = sizeof (ps_strings); 151 if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0) 152 { 153 i386fbsd_sigtramp_start_addr = ps_strings - 128; 154 i386fbsd_sigtramp_end_addr = ps_strings; 155 } 156 } 157#endif 158} 159