| 1 | /* Native-dependent code for FreeBSD/i386. |
| 2 | |
| 3 | Copyright (C) 2001-2019 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 | #include <sys/user.h> |
| 29 | |
| 30 | #include "fbsd-nat.h" |
| 31 | #include "i386-tdep.h" |
| 32 | #include "x86-nat.h" |
| 33 | #include "common/x86-xstate.h" |
| 34 | #include "x86-bsd-nat.h" |
| 35 | #include "i386-bsd-nat.h" |
| 36 | |
| 37 | class i386_fbsd_nat_target final |
| 38 | : public i386_bsd_nat_target<fbsd_nat_target> |
| 39 | { |
| 40 | public: |
| 41 | /* Add some extra features to the common *BSD/i386 target. */ |
| 42 | #ifdef PT_GETXSTATE_INFO |
| 43 | const struct target_desc *read_description () override; |
| 44 | #endif |
| 45 | |
| 46 | void resume (ptid_t, int, enum gdb_signal) override; |
| 47 | |
| 48 | #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO) |
| 49 | bool supports_stopped_by_hw_breakpoint () override; |
| 50 | #endif |
| 51 | }; |
| 52 | |
| 53 | static i386_fbsd_nat_target the_i386_fbsd_nat_target; |
| 54 | |
| 55 | /* Resume execution of the inferior process. If STEP is nonzero, |
| 56 | single-step it. If SIGNAL is nonzero, give it that signal. */ |
| 57 | |
| 58 | void |
| 59 | i386_fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signal) |
| 60 | { |
| 61 | pid_t pid = ptid.pid (); |
| 62 | int request = PT_STEP; |
| 63 | |
| 64 | if (pid == -1) |
| 65 | /* Resume all threads. This only gets used in the non-threaded |
| 66 | case, where "resume all threads" and "resume inferior_ptid" are |
| 67 | the same. */ |
| 68 | pid = inferior_ptid.pid (); |
| 69 | |
| 70 | if (!step) |
| 71 | { |
| 72 | struct regcache *regcache = get_current_regcache (); |
| 73 | ULONGEST eflags; |
| 74 | |
| 75 | /* Workaround for a bug in FreeBSD. Make sure that the trace |
| 76 | flag is off when doing a continue. There is a code path |
| 77 | through the kernel which leaves the flag set when it should |
| 78 | have been cleared. If a process has a signal pending (such |
| 79 | as SIGALRM) and we do a PT_STEP, the process never really has |
| 80 | a chance to run because the kernel needs to notify the |
| 81 | debugger that a signal is being sent. Therefore, the process |
| 82 | never goes through the kernel's trap() function which would |
| 83 | normally clear it. */ |
| 84 | |
| 85 | regcache_cooked_read_unsigned (regcache, I386_EFLAGS_REGNUM, |
| 86 | &eflags); |
| 87 | if (eflags & 0x0100) |
| 88 | regcache_cooked_write_unsigned (regcache, I386_EFLAGS_REGNUM, |
| 89 | eflags & ~0x0100); |
| 90 | |
| 91 | request = PT_CONTINUE; |
| 92 | } |
| 93 | |
| 94 | /* An addres of (caddr_t) 1 tells ptrace to continue from where it |
| 95 | was. (If GDB wanted it to start some other way, we have already |
| 96 | written a new PC value to the child.) */ |
| 97 | if (ptrace (request, pid, (caddr_t) 1, |
| 98 | gdb_signal_to_host (signal)) == -1) |
| 99 | perror_with_name (("ptrace")); |
| 100 | } |
| 101 | \f |
| 102 | |
| 103 | /* Support for debugging kernel virtual memory images. */ |
| 104 | |
| 105 | #include <machine/pcb.h> |
| 106 | |
| 107 | #include "bsd-kvm.h" |
| 108 | |
| 109 | static int |
| 110 | i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) |
| 111 | { |
| 112 | /* The following is true for FreeBSD 4.7: |
| 113 | |
| 114 | The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs. |
| 115 | This accounts for all callee-saved registers specified by the |
| 116 | psABI and then some. Here %esp contains the stack pointer at the |
| 117 | point just after the call to cpu_switch(). From this information |
| 118 | we reconstruct the register state as it would look when we just |
| 119 | returned from cpu_switch(). */ |
| 120 | |
| 121 | /* The stack pointer shouldn't be zero. */ |
| 122 | if (pcb->pcb_esp == 0) |
| 123 | return 0; |
| 124 | |
| 125 | pcb->pcb_esp += 4; |
| 126 | regcache->raw_supply (I386_EDI_REGNUM, &pcb->pcb_edi); |
| 127 | regcache->raw_supply (I386_ESI_REGNUM, &pcb->pcb_esi); |
| 128 | regcache->raw_supply (I386_EBP_REGNUM, &pcb->pcb_ebp); |
| 129 | regcache->raw_supply (I386_ESP_REGNUM, &pcb->pcb_esp); |
| 130 | regcache->raw_supply (I386_EBX_REGNUM, &pcb->pcb_ebx); |
| 131 | regcache->raw_supply (I386_EIP_REGNUM, &pcb->pcb_eip); |
| 132 | regcache->raw_supply (I386_GS_REGNUM, &pcb->pcb_gs); |
| 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | \f |
| 137 | |
| 138 | #ifdef PT_GETXSTATE_INFO |
| 139 | /* Implement the read_description method. */ |
| 140 | |
| 141 | const struct target_desc * |
| 142 | i386_fbsd_nat_target::read_description () |
| 143 | { |
| 144 | static int xsave_probed; |
| 145 | static uint64_t xcr0; |
| 146 | |
| 147 | if (!xsave_probed) |
| 148 | { |
| 149 | struct ptrace_xstate_info info; |
| 150 | |
| 151 | if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (), |
| 152 | (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0) |
| 153 | { |
| 154 | x86bsd_xsave_len = info.xsave_len; |
| 155 | xcr0 = info.xsave_mask; |
| 156 | } |
| 157 | xsave_probed = 1; |
| 158 | } |
| 159 | |
| 160 | if (x86bsd_xsave_len == 0) |
| 161 | xcr0 = X86_XSTATE_SSE_MASK; |
| 162 | |
| 163 | return i386_target_description (xcr0, false); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO) |
| 168 | /* Implement the supports_stopped_by_hw_breakpoints method. */ |
| 169 | |
| 170 | bool |
| 171 | i386_fbsd_nat_target::supports_stopped_by_hw_breakpoint () |
| 172 | { |
| 173 | return true; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | void |
| 178 | _initialize_i386fbsd_nat (void) |
| 179 | { |
| 180 | add_inf_child_target (&the_i386_fbsd_nat_target); |
| 181 | |
| 182 | /* Support debugging kernel virtual memory images. */ |
| 183 | bsd_kvm_add_target (i386fbsd_supply_pcb); |
| 184 | |
| 185 | #ifdef KERN_PROC_SIGTRAMP |
| 186 | /* Normally signal frames are detected via i386fbsd_sigtramp_p. |
| 187 | However, FreeBSD 9.2 through 10.1 do not include the page holding |
| 188 | the signal code in core dumps. These releases do provide a |
| 189 | kern.proc.sigtramp.<pid> sysctl that returns the location of the |
| 190 | signal trampoline for a running process. We fetch the location |
| 191 | of the current (gdb) process and use this to identify signal |
| 192 | frames in core dumps from these releases. */ |
| 193 | { |
| 194 | int mib[4]; |
| 195 | struct kinfo_sigtramp kst; |
| 196 | size_t len; |
| 197 | |
| 198 | mib[0] = CTL_KERN; |
| 199 | mib[1] = KERN_PROC; |
| 200 | mib[2] = KERN_PROC_SIGTRAMP; |
| 201 | mib[3] = getpid (); |
| 202 | len = sizeof (kst); |
| 203 | if (sysctl (mib, 4, &kst, &len, NULL, 0) == 0) |
| 204 | { |
| 205 | i386fbsd_sigtramp_start_addr = (uintptr_t) kst.ksigtramp_start; |
| 206 | i386fbsd_sigtramp_end_addr = (uintptr_t) kst.ksigtramp_end; |
| 207 | } |
| 208 | } |
| 209 | #endif |
| 210 | } |