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>
45 #if defined(__aarch64__)
46 #define AARCH64_BRK 0xd4200000
47 #define AARCH64_BRK_IMM16_SHIFT 5
48 #define AARCH64_BRK_IMM16_VAL (0xd << AARCH64_BRK_IMM16_SHIFT)
49 #define BREAKPOINT_INSTR (AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
50 #define BREAKPOINT_INSTR_SZ 4
51 #elif defined(__amd64__) || defined(__i386__)
52 #define BREAKPOINT_INSTR 0xcc /* int 0x3 */
53 #define BREAKPOINT_INSTR_SZ 1
54 #define BREAKPOINT_ADJUST_SZ BREAKPOINT_INSTR_SZ
55 #elif defined(__arm__)
56 #define BREAKPOINT_INSTR 0xe7ffffff /* bkpt */
57 #define BREAKPOINT_INSTR_SZ 4
58 #elif defined(__mips__)
59 #define BREAKPOINT_INSTR 0xd /* break */
60 #define BREAKPOINT_INSTR_SZ 4
61 #elif defined(__powerpc__)
62 #define BREAKPOINT_INSTR 0x7fe00008 /* trap */
63 #define BREAKPOINT_INSTR_SZ 4
64 #elif defined(__riscv__)
65 #define BREAKPOINT_INSTR 0x00100073 /* sbreak */
66 #define BREAKPOINT_INSTR_SZ 4
68 #error "Add support for your architecture"
72 * Use 4-bytes holder for breakpoint instruction on all the platforms.
73 * Works for x86 as well until it is endian-little platform.
74 * (We are coping one byte only on x86 from this 4-bytes piece of
77 typedef uint32_t instr_t;
80 proc_stop(struct proc_handle *phdl)
84 if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
85 DPRINTF("kill %d", proc_getpid(phdl));
87 } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
88 DPRINTF("waitpid %d", proc_getpid(phdl));
90 } else if (!WIFSTOPPED(status)) {
91 DPRINTFX("waitpid: unexpected status 0x%x", status);
99 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
100 unsigned long *saved)
102 struct ptrace_io_desc piod;
104 int ret = 0, stopped;
108 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
109 phdl->status == PS_IDLE) {
114 DPRINTFX("adding breakpoint at 0x%lx", address);
117 if (phdl->status != PS_STOP) {
118 if (proc_stop(phdl) != 0)
124 * Read the original instruction.
128 piod.piod_op = PIOD_READ_I;
129 piod.piod_offs = (void *)caddr;
130 piod.piod_addr = &instr;
131 piod.piod_len = BREAKPOINT_INSTR_SZ;
132 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
133 DPRINTF("ERROR: couldn't read instruction at address 0x%jx",
140 * Write a breakpoint instruction to that address.
143 instr = BREAKPOINT_INSTR;
144 piod.piod_op = PIOD_WRITE_I;
145 piod.piod_offs = (void *)caddr;
146 piod.piod_addr = &instr;
147 piod.piod_len = BREAKPOINT_INSTR_SZ;
148 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
149 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
157 /* Restart the process if we had to stop it. */
164 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
167 struct ptrace_io_desc piod;
169 int ret = 0, stopped;
172 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
173 phdl->status == PS_IDLE) {
178 DPRINTFX("removing breakpoint at 0x%lx", address);
181 if (phdl->status != PS_STOP) {
182 if (proc_stop(phdl) != 0)
188 * Overwrite the breakpoint instruction that we setup previously.
192 piod.piod_op = PIOD_WRITE_I;
193 piod.piod_offs = (void *)caddr;
194 piod.piod_addr = &instr;
195 piod.piod_len = BREAKPOINT_INSTR_SZ;
196 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
197 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
203 /* Restart the process if we had to stop it. */
210 * Decrement pc so that we delete the breakpoint at the correct
211 * address, i.e. at the BREAKPOINT_INSTR address.
213 * This is only needed on some architectures where the pc value
214 * when reading registers points at the instruction after the
215 * breakpoint, e.g. x86.
218 proc_bkptregadj(unsigned long *pc)
222 #ifdef BREAKPOINT_ADJUST_SZ
223 *pc = *pc - BREAKPOINT_ADJUST_SZ;
228 * Step over the breakpoint.
231 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
234 unsigned long samesaved;
237 if (proc_regget(phdl, REG_PC, &pc) < 0) {
238 DPRINTFX("ERROR: couldn't get PC register");
241 proc_bkptregadj(&pc);
242 if (proc_bkptdel(phdl, pc, saved) < 0) {
243 DPRINTFX("ERROR: couldn't delete breakpoint");
247 * Go back in time and step over the new instruction just
248 * set up by proc_bkptdel().
250 proc_regset(phdl, REG_PC, pc);
251 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
252 DPRINTFX("ERROR: ptrace step failed");
256 status = proc_getwstat(phdl);
257 if (!WIFSTOPPED(status)) {
258 DPRINTFX("ERROR: don't know why process stopped");
262 * Restore the breakpoint. The saved instruction should be
263 * the same as the one that we were passed in.
265 if (proc_bkptset(phdl, pc, &samesaved) < 0) {
266 DPRINTFX("ERROR: couldn't restore breakpoint");
269 assert(samesaved == saved);