xref: /freebsd/lib/libproc/proc_bkpt.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Rui Paulo under sponsorship from the
8  * FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/types.h>
36 #include <sys/ptrace.h>
37 #include <sys/wait.h>
38 
39 #include <assert.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <stdio.h>
44 
45 #include "_libproc.h"
46 
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
69 #else
70 #error "Add support for your architecture"
71 #endif
72 
73 /*
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
77  * memory).
78  */
79 typedef uint32_t instr_t;
80 
81 static int
82 proc_stop(struct proc_handle *phdl)
83 {
84 	int status;
85 
86 	if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
87 		DPRINTF("kill %d", proc_getpid(phdl));
88 		return (-1);
89 	} else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
90 		DPRINTF("waitpid %d", proc_getpid(phdl));
91 		return (-1);
92 	} else if (!WIFSTOPPED(status)) {
93 		DPRINTFX("waitpid: unexpected status 0x%x", status);
94 		return (-1);
95 	}
96 
97 	return (0);
98 }
99 
100 int
101 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
102     unsigned long *saved)
103 {
104 	struct ptrace_io_desc piod;
105 	unsigned long caddr;
106 	int ret = 0, stopped;
107 	instr_t instr;
108 
109 	*saved = 0;
110 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
111 	    phdl->status == PS_IDLE) {
112 		errno = ENOENT;
113 		return (-1);
114 	}
115 
116 	DPRINTFX("adding breakpoint at 0x%lx", address);
117 
118 	stopped = 0;
119 	if (phdl->status != PS_STOP) {
120 		if (proc_stop(phdl) != 0)
121 			return (-1);
122 		stopped = 1;
123 	}
124 
125 	/*
126 	 * Read the original instruction.
127 	 */
128 	caddr = address;
129 	instr = 0;
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",
136 		    (uintmax_t)address);
137 		ret = -1;
138 		goto done;
139 	}
140 	*saved = instr;
141 	/*
142 	 * Write a breakpoint instruction to that address.
143 	 */
144 	caddr = 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",
152 		    (uintmax_t)address);
153 		ret = -1;
154 		goto done;
155 	}
156 
157 done:
158 	if (stopped)
159 		/* Restart the process if we had to stop it. */
160 		proc_continue(phdl);
161 
162 	return (ret);
163 }
164 
165 int
166 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
167     unsigned long saved)
168 {
169 	struct ptrace_io_desc piod;
170 	unsigned long caddr;
171 	int ret = 0, stopped;
172 	instr_t instr;
173 
174 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
175 	    phdl->status == PS_IDLE) {
176 		errno = ENOENT;
177 		return (-1);
178 	}
179 
180 	DPRINTFX("removing breakpoint at 0x%lx", address);
181 
182 	stopped = 0;
183 	if (phdl->status != PS_STOP) {
184 		if (proc_stop(phdl) != 0)
185 			return (-1);
186 		stopped = 1;
187 	}
188 
189 	/*
190 	 * Overwrite the breakpoint instruction that we setup previously.
191 	 */
192 	caddr = address;
193 	instr = saved;
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",
200 		    (uintmax_t)address);
201 		ret = -1;
202 	}
203 
204 	if (stopped)
205 		/* Restart the process if we had to stop it. */
206 		proc_continue(phdl);
207 
208 	return (ret);
209 }
210 
211 /*
212  * Decrement pc so that we delete the breakpoint at the correct
213  * address, i.e. at the BREAKPOINT_INSTR address.
214  *
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.
218  */
219 void
220 proc_bkptregadj(unsigned long *pc)
221 {
222 
223 	(void)pc;
224 #ifdef BREAKPOINT_ADJUST_SZ
225 	*pc = *pc - BREAKPOINT_ADJUST_SZ;
226 #endif
227 }
228 
229 /*
230  * Step over the breakpoint.
231  */
232 int
233 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
234 {
235 	unsigned long pc;
236 	unsigned long samesaved;
237 	int status;
238 
239 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
240 		DPRINTFX("ERROR: couldn't get PC register");
241 		return (-1);
242 	}
243 	proc_bkptregadj(&pc);
244 	if (proc_bkptdel(phdl, pc, saved) < 0) {
245 		DPRINTFX("ERROR: couldn't delete breakpoint");
246 		return (-1);
247 	}
248 	/*
249 	 * Go back in time and step over the new instruction just
250 	 * set up by proc_bkptdel().
251 	 */
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");
255 		return (-1);
256 	}
257 	proc_wstatus(phdl);
258 	status = proc_getwstat(phdl);
259 	if (!WIFSTOPPED(status)) {
260 		DPRINTFX("ERROR: don't know why process stopped");
261 		return (-1);
262 	}
263 	/*
264 	 * Restore the breakpoint. The saved instruction should be
265 	 * the same as the one that we were passed in.
266 	 */
267 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
268 		DPRINTFX("ERROR: couldn't restore breakpoint");
269 		return (-1);
270 	}
271 	assert(samesaved == saved);
272 
273 	return (0);
274 }
275