2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Rui Paulo under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
36 #include <machine/_inttypes.h>
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
55 #error "Add support for your architecture"
59 proc_stop(struct proc_handle *phdl)
63 if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
64 DPRINTF("kill %d", proc_getpid(phdl));
66 } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
67 DPRINTF("waitpid %d", proc_getpid(phdl));
69 } else if (!WIFSTOPPED(status)) {
70 DPRINTFX("waitpid: unexpected status 0x%x", status);
78 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
81 struct ptrace_io_desc piod;
82 unsigned long paddr, caddr;
86 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
87 phdl->status == PS_IDLE) {
92 DPRINTFX("adding breakpoint at 0x%lx", address);
95 if (phdl->status != PS_STOP) {
96 if (proc_stop(phdl) != 0)
102 * Read the original instruction.
106 piod.piod_op = PIOD_READ_I;
107 piod.piod_offs = (void *)caddr;
108 piod.piod_addr = &paddr;
109 piod.piod_len = BREAKPOINT_INSTR_SZ;
110 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
111 DPRINTF("ERROR: couldn't read instruction at address 0x%"
118 * Write a breakpoint instruction to that address.
121 paddr = BREAKPOINT_INSTR;
122 piod.piod_op = PIOD_WRITE_I;
123 piod.piod_offs = (void *)caddr;
124 piod.piod_addr = &paddr;
125 piod.piod_len = BREAKPOINT_INSTR_SZ;
126 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
127 DPRINTF("ERROR: couldn't write instruction at address 0x%"
135 /* Restart the process if we had to stop it. */
142 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
145 struct ptrace_io_desc piod;
146 unsigned long paddr, caddr;
147 int ret = 0, stopped;
149 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
150 phdl->status == PS_IDLE) {
155 DPRINTFX("removing breakpoint at 0x%lx", address);
158 if (phdl->status != PS_STOP) {
159 if (proc_stop(phdl) != 0)
165 * Overwrite the breakpoint instruction that we setup previously.
169 piod.piod_op = PIOD_WRITE_I;
170 piod.piod_offs = (void *)caddr;
171 piod.piod_addr = &paddr;
172 piod.piod_len = BREAKPOINT_INSTR_SZ;
173 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
174 DPRINTF("ERROR: couldn't write instruction at address 0x%"
180 /* Restart the process if we had to stop it. */
187 * Decrement pc so that we delete the breakpoint at the correct
188 * address, i.e. at the BREAKPOINT_INSTR address.
191 proc_bkptregadj(unsigned long *pc)
193 *pc = *pc - BREAKPOINT_INSTR_SZ;
197 * Step over the breakpoint.
200 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
203 unsigned long samesaved;
206 if (proc_regget(phdl, REG_PC, &pc) < 0) {
207 DPRINTFX("ERROR: couldn't get PC register");
210 proc_bkptregadj(&pc);
211 if (proc_bkptdel(phdl, pc, saved) < 0) {
212 DPRINTFX("ERROR: couldn't delete breakpoint");
216 * Go back in time and step over the new instruction just
217 * set up by proc_bkptdel().
219 proc_regset(phdl, REG_PC, pc);
220 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
221 DPRINTFX("ERROR: ptrace step failed");
225 status = proc_getwstat(phdl);
226 if (!WIFSTOPPED(status)) {
227 DPRINTFX("ERROR: don't know why process stopped");
231 * Restore the breakpoint. The saved instruction should be
232 * the same as the one that we were passed in.
234 if (proc_bkptset(phdl, pc, &samesaved) < 0) {
235 DPRINTFX("ERROR: couldn't restore breakpoint");
238 assert(samesaved == saved);