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;
105 int ret = 0, stopped;
109 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
110 phdl->status == PS_IDLE) {
115 DPRINTFX("adding breakpoint at 0x%lx", (unsigned long)address);
118 if (phdl->status != PS_STOP) {
119 if (proc_stop(phdl) != 0)
125 * Read the original instruction.
128 piod.piod_op = PIOD_READ_I;
129 piod.piod_offs = (void *)address;
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.
142 instr = BREAKPOINT_INSTR;
143 piod.piod_op = PIOD_WRITE_I;
144 piod.piod_offs = (void *)address;
145 piod.piod_addr = &instr;
146 piod.piod_len = BREAKPOINT_INSTR_SZ;
147 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
148 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
156 /* Restart the process if we had to stop it. */
163 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
166 struct ptrace_io_desc piod;
167 int ret = 0, stopped;
170 if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
171 phdl->status == PS_IDLE) {
176 DPRINTFX("removing breakpoint at 0x%lx", (unsigned long)address);
179 if (phdl->status != PS_STOP) {
180 if (proc_stop(phdl) != 0)
186 * Overwrite the breakpoint instruction that we setup previously.
189 piod.piod_op = PIOD_WRITE_I;
190 piod.piod_offs = (void *)address;
191 piod.piod_addr = &instr;
192 piod.piod_len = BREAKPOINT_INSTR_SZ;
193 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
194 DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
200 /* Restart the process if we had to stop it. */
207 * Decrement pc so that we delete the breakpoint at the correct
208 * address, i.e. at the BREAKPOINT_INSTR address.
210 * This is only needed on some architectures where the pc value
211 * when reading registers points at the instruction after the
212 * breakpoint, e.g. x86.
215 proc_bkptregadj(unsigned long *pc)
219 #ifdef BREAKPOINT_ADJUST_SZ
220 *pc = *pc - BREAKPOINT_ADJUST_SZ;
225 * Step over the breakpoint.
228 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
231 unsigned long samesaved;
234 if (proc_regget(phdl, REG_PC, &pc) < 0) {
235 DPRINTFX("ERROR: couldn't get PC register");
238 proc_bkptregadj(&pc);
239 if (proc_bkptdel(phdl, pc, saved) < 0) {
240 DPRINTFX("ERROR: couldn't delete breakpoint");
244 * Go back in time and step over the new instruction just
245 * set up by proc_bkptdel().
247 proc_regset(phdl, REG_PC, pc);
248 if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
249 DPRINTFX("ERROR: ptrace step failed");
253 status = proc_getwstat(phdl);
254 if (!WIFSTOPPED(status)) {
255 DPRINTFX("ERROR: don't know why process stopped");
259 * Restore the breakpoint. The saved instruction should be
260 * the same as the one that we were passed in.
262 if (proc_bkptset(phdl, pc, &samesaved) < 0) {
263 DPRINTFX("ERROR: couldn't restore breakpoint");
266 assert(samesaved == saved);