2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
7 * This software was developed by Rui Paulo under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/ptrace.h>
47 #if defined(__aarch64__)
48 #define AARCH64_BRK 0xd4200000
49 #define AARCH64_BRK_IMM16_SHIFT 5
50 #define AARCH64_BRK_IMM16_VAL (0xd << AARCH64_BRK_IMM16_SHIFT)
51 #define BREAKPOINT_INSTR (AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
52 #define BREAKPOINT_INSTR_SZ 4
53 #elif defined(__amd64__) || defined(__i386__)
54 #define BREAKPOINT_INSTR 0xcc /* int 0x3 */
55 #define BREAKPOINT_INSTR_SZ 1
56 #define BREAKPOINT_ADJUST_SZ BREAKPOINT_INSTR_SZ
57 #elif defined(__arm__)
58 #define BREAKPOINT_INSTR 0xe7ffffff /* bkpt */
59 #define BREAKPOINT_INSTR_SZ 4
60 #elif defined(__mips__)
61 #define BREAKPOINT_INSTR 0xd /* break */
62 #define BREAKPOINT_INSTR_SZ 4
63 #elif defined(__powerpc__)
64 #define BREAKPOINT_INSTR 0x7fe00008 /* trap */
65 #define BREAKPOINT_INSTR_SZ 4
66 #elif defined(__riscv)
67 #define BREAKPOINT_INSTR 0x00100073 /* sbreak */
68 #define BREAKPOINT_INSTR_SZ 4
70 #error "Add support for your architecture"
74 * Use 4-bytes holder for breakpoint instruction on all the platforms.
75 * Works for x86 as well until it is endian-little platform.
76 * (We are coping one byte only on x86 from this 4-bytes piece of
79 typedef uint32_t instr_t;
82 proc_stop(struct proc_handle *phdl)
86 if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
87 DPRINTF("kill %d", proc_getpid(phdl));
89 } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
90 DPRINTF("waitpid %d", proc_getpid(phdl));
92 } else if (!WIFSTOPPED(status)) {
93 DPRINTFX("waitpid: unexpected status 0x%x", status);
101 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
102 unsigned long *saved)
104 struct ptrace_io_desc piod;
106 int ret = 0, stopped;
110 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
111 phdl->status == PS_IDLE) {
116 DPRINTFX("adding breakpoint at 0x%lx", address);
119 if (phdl->status != PS_STOP) {
120 if (proc_stop(phdl) != 0)
126 * Read the original instruction.
130 piod.piod_op = PIOD_READ_I;
131 piod.piod_offs = (void *)caddr;
132 piod.piod_addr = &instr;
133 piod.piod_len = BREAKPOINT_INSTR_SZ;
134 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
135 DPRINTF("ERROR: couldn't read instruction at address 0x%jx",
142 * Write a breakpoint instruction to that address.
145 instr = BREAKPOINT_INSTR;
146 piod.piod_op = PIOD_WRITE_I;
147 piod.piod_offs = (void *)caddr;
148 piod.piod_addr = &instr;
149 piod.piod_len = BREAKPOINT_INSTR_SZ;
150 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
151 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
159 /* Restart the process if we had to stop it. */
166 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
169 struct ptrace_io_desc piod;
171 int ret = 0, stopped;
174 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
175 phdl->status == PS_IDLE) {
180 DPRINTFX("removing breakpoint at 0x%lx", address);
183 if (phdl->status != PS_STOP) {
184 if (proc_stop(phdl) != 0)
190 * Overwrite the breakpoint instruction that we setup previously.
194 piod.piod_op = PIOD_WRITE_I;
195 piod.piod_offs = (void *)caddr;
196 piod.piod_addr = &instr;
197 piod.piod_len = BREAKPOINT_INSTR_SZ;
198 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
199 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
205 /* Restart the process if we had to stop it. */
212 * Decrement pc so that we delete the breakpoint at the correct
213 * address, i.e. at the BREAKPOINT_INSTR address.
215 * This is only needed on some architectures where the pc value
216 * when reading registers points at the instruction after the
217 * breakpoint, e.g. x86.
220 proc_bkptregadj(unsigned long *pc)
224 #ifdef BREAKPOINT_ADJUST_SZ
225 *pc = *pc - BREAKPOINT_ADJUST_SZ;
230 * Step over the breakpoint.
233 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
236 unsigned long samesaved;
239 if (proc_regget(phdl, REG_PC, &pc) < 0) {
240 DPRINTFX("ERROR: couldn't get PC register");
243 proc_bkptregadj(&pc);
244 if (proc_bkptdel(phdl, pc, saved) < 0) {
245 DPRINTFX("ERROR: couldn't delete breakpoint");
249 * Go back in time and step over the new instruction just
250 * set up by proc_bkptdel().
252 proc_regset(phdl, REG_PC, pc);
253 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
254 DPRINTFX("ERROR: ptrace step failed");
258 status = proc_getwstat(phdl);
259 if (!WIFSTOPPED(status)) {
260 DPRINTFX("ERROR: don't know why process stopped");
264 * Restore the breakpoint. The saved instruction should be
265 * the same as the one that we were passed in.
267 if (proc_bkptset(phdl, pc, &samesaved) < 0) {
268 DPRINTFX("ERROR: couldn't restore breakpoint");
271 assert(samesaved == saved);