1 /* 2 * Copyright (c) 2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Rui Paulo under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/types.h> 34 #include <sys/ptrace.h> 35 #include <sys/wait.h> 36 #include <machine/_inttypes.h> 37 38 #include <assert.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <signal.h> 42 #include <stdio.h> 43 #include "_libproc.h" 44 45 #if defined(__i386__) || defined(__amd64__) 46 #define BREAKPOINT_INSTR 0xcc /* int 0x3 */ 47 #define BREAKPOINT_INSTR_SZ 1 48 #elif defined(__mips__) 49 #define BREAKPOINT_INSTR 0xd /* break */ 50 #define BREAKPOINT_INSTR_SZ 4 51 #elif defined(__powerpc__) 52 #define BREAKPOINT_INSTR 0x7fe00008 /* trap */ 53 #define BREAKPOINT_INSTR_SZ 4 54 #elif defined(__arm__) 55 #define BREAKPOINT_INSTR 0xe7ffffff /* bkpt */ 56 #define BREAKPOINT_INSTR_SZ 4 57 #else 58 #error "Add support for your architecture" 59 #endif 60 61 static int 62 proc_stop(struct proc_handle *phdl) 63 { 64 int status; 65 66 if (kill(proc_getpid(phdl), SIGSTOP) == -1) { 67 DPRINTF("kill %d", proc_getpid(phdl)); 68 return (-1); 69 } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) { 70 DPRINTF("waitpid %d", proc_getpid(phdl)); 71 return (-1); 72 } else if (!WIFSTOPPED(status)) { 73 DPRINTFX("waitpid: unexpected status 0x%x", status); 74 return (-1); 75 } 76 77 return (0); 78 } 79 80 int 81 proc_bkptset(struct proc_handle *phdl, uintptr_t address, 82 unsigned long *saved) 83 { 84 struct ptrace_io_desc piod; 85 unsigned long paddr, caddr; 86 int ret = 0, stopped; 87 88 *saved = 0; 89 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || 90 phdl->status == PS_IDLE) { 91 errno = ENOENT; 92 return (-1); 93 } 94 95 DPRINTFX("adding breakpoint at 0x%lx", address); 96 97 stopped = 0; 98 if (phdl->status != PS_STOP) { 99 if (proc_stop(phdl) != 0) 100 return (-1); 101 stopped = 1; 102 } 103 104 /* 105 * Read the original instruction. 106 */ 107 caddr = address; 108 paddr = 0; 109 piod.piod_op = PIOD_READ_I; 110 piod.piod_offs = (void *)caddr; 111 piod.piod_addr = &paddr; 112 piod.piod_len = BREAKPOINT_INSTR_SZ; 113 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { 114 DPRINTF("ERROR: couldn't read instruction at address 0x%" 115 PRIuPTR, address); 116 ret = -1; 117 goto done; 118 } 119 *saved = paddr; 120 /* 121 * Write a breakpoint instruction to that address. 122 */ 123 caddr = address; 124 paddr = BREAKPOINT_INSTR; 125 piod.piod_op = PIOD_WRITE_I; 126 piod.piod_offs = (void *)caddr; 127 piod.piod_addr = &paddr; 128 piod.piod_len = BREAKPOINT_INSTR_SZ; 129 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { 130 DPRINTF("ERROR: couldn't write instruction at address 0x%" 131 PRIuPTR, address); 132 ret = -1; 133 goto done; 134 } 135 136 done: 137 if (stopped) 138 /* Restart the process if we had to stop it. */ 139 proc_continue(phdl); 140 141 return (ret); 142 } 143 144 int 145 proc_bkptdel(struct proc_handle *phdl, uintptr_t address, 146 unsigned long saved) 147 { 148 struct ptrace_io_desc piod; 149 unsigned long paddr, caddr; 150 int ret = 0, stopped; 151 152 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || 153 phdl->status == PS_IDLE) { 154 errno = ENOENT; 155 return (-1); 156 } 157 158 DPRINTFX("removing breakpoint at 0x%lx", address); 159 160 stopped = 0; 161 if (phdl->status != PS_STOP) { 162 if (proc_stop(phdl) != 0) 163 return (-1); 164 stopped = 1; 165 } 166 167 /* 168 * Overwrite the breakpoint instruction that we setup previously. 169 */ 170 caddr = address; 171 paddr = saved; 172 piod.piod_op = PIOD_WRITE_I; 173 piod.piod_offs = (void *)caddr; 174 piod.piod_addr = &paddr; 175 piod.piod_len = BREAKPOINT_INSTR_SZ; 176 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { 177 DPRINTF("ERROR: couldn't write instruction at address 0x%" 178 PRIuPTR, address); 179 ret = -1; 180 } 181 182 if (stopped) 183 /* Restart the process if we had to stop it. */ 184 proc_continue(phdl); 185 186 return (ret); 187 } 188 189 /* 190 * Decrement pc so that we delete the breakpoint at the correct 191 * address, i.e. at the BREAKPOINT_INSTR address. 192 */ 193 void 194 proc_bkptregadj(unsigned long *pc) 195 { 196 *pc = *pc - BREAKPOINT_INSTR_SZ; 197 } 198 199 /* 200 * Step over the breakpoint. 201 */ 202 int 203 proc_bkptexec(struct proc_handle *phdl, unsigned long saved) 204 { 205 unsigned long pc; 206 unsigned long samesaved; 207 int status; 208 209 if (proc_regget(phdl, REG_PC, &pc) < 0) { 210 DPRINTFX("ERROR: couldn't get PC register"); 211 return (-1); 212 } 213 proc_bkptregadj(&pc); 214 if (proc_bkptdel(phdl, pc, saved) < 0) { 215 DPRINTFX("ERROR: couldn't delete breakpoint"); 216 return (-1); 217 } 218 /* 219 * Go back in time and step over the new instruction just 220 * set up by proc_bkptdel(). 221 */ 222 proc_regset(phdl, REG_PC, pc); 223 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) { 224 DPRINTFX("ERROR: ptrace step failed"); 225 return (-1); 226 } 227 proc_wstatus(phdl); 228 status = proc_getwstat(phdl); 229 if (!WIFSTOPPED(status)) { 230 DPRINTFX("ERROR: don't know why process stopped"); 231 return (-1); 232 } 233 /* 234 * Restore the breakpoint. The saved instruction should be 235 * the same as the one that we were passed in. 236 */ 237 if (proc_bkptset(phdl, pc, &samesaved) < 0) { 238 DPRINTFX("ERROR: couldn't restore breakpoint"); 239 return (-1); 240 } 241 assert(samesaved == saved); 242 243 return (0); 244 } 245