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>
44 #if defined(__i386__) || defined(__amd64__)
45 #define BREAKPOINT_INSTR 0xcc /* int 0x3 */
46 #define BREAKPOINT_INSTR_SZ 1
48 #error "Add support for your architecture"
52 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
55 struct ptrace_io_desc piod;
56 unsigned long paddr, caddr;
59 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
60 phdl->status == PS_IDLE) {
66 * Read the original instruction.
70 piod.piod_op = PIOD_READ_I;
71 piod.piod_offs = (void *)caddr;
72 piod.piod_addr = &paddr;
73 piod.piod_len = BREAKPOINT_INSTR_SZ;
74 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
75 DPRINTF("ERROR: couldn't read instruction at address 0x%" PRIuPTR,
81 * Write a breakpoint instruction to that address.
84 paddr = BREAKPOINT_INSTR;
85 piod.piod_op = PIOD_WRITE_I;
86 piod.piod_offs = (void *)caddr;
87 piod.piod_addr = &paddr;
88 piod.piod_len = BREAKPOINT_INSTR_SZ;
89 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
90 warn("ERROR: couldn't write instruction at address 0x%" PRIuPTR,
99 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
102 struct ptrace_io_desc piod;
103 unsigned long paddr, caddr;
105 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
106 phdl->status == PS_IDLE) {
110 DPRINTF("removing breakpoint at 0x%lx\n", address);
112 * Overwrite the breakpoint instruction that we setup previously.
116 piod.piod_op = PIOD_WRITE_I;
117 piod.piod_offs = (void *)caddr;
118 piod.piod_addr = &paddr;
119 piod.piod_len = BREAKPOINT_INSTR_SZ;
120 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
121 DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR,
130 * Decrement pc so that we delete the breakpoint at the correct
131 * address, i.e. at the BREAKPOINT_INSTR address.
134 proc_bkptregadj(unsigned long *pc)
136 *pc = *pc - BREAKPOINT_INSTR_SZ;
140 * Step over the breakpoint.
143 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
146 unsigned long samesaved;
149 if (proc_regget(phdl, REG_PC, &pc) < 0) {
150 warn("ERROR: couldn't get PC register");
153 proc_bkptregadj(&pc);
154 if (proc_bkptdel(phdl, pc, saved) < 0) {
155 warn("ERROR: couldn't delete breakpoint");
159 * Go back in time and step over the new instruction just
160 * set up by proc_bkptdel().
162 proc_regset(phdl, REG_PC, pc);
163 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
164 warn("ERROR: ptrace step failed");
168 status = proc_getwstat(phdl);
169 if (!WIFSTOPPED(status)) {
170 warn("ERROR: don't know why process stopped");
174 * Restore the breakpoint. The saved instruction should be
175 * the same as the one that we were passed in.
177 if (proc_bkptset(phdl, pc, &samesaved) < 0) {
178 warn("ERROR: couldn't restore breakpoint");
181 assert(samesaved == saved);