xref: /freebsd/lib/libproc/proc_bkpt.c (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
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 	int ret = 0, stopped;
106 	instr_t instr;
107 
108 	*saved = 0;
109 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
110 	    phdl->status == PS_IDLE) {
111 		errno = ENOENT;
112 		return (-1);
113 	}
114 
115 	DPRINTFX("adding breakpoint at 0x%lx", (unsigned long)address);
116 
117 	stopped = 0;
118 	if (phdl->status != PS_STOP) {
119 		if (proc_stop(phdl) != 0)
120 			return (-1);
121 		stopped = 1;
122 	}
123 
124 	/*
125 	 * Read the original instruction.
126 	 */
127 	instr = 0;
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",
134 		    (uintmax_t)address);
135 		ret = -1;
136 		goto done;
137 	}
138 	*saved = instr;
139 	/*
140 	 * Write a breakpoint instruction to that address.
141 	 */
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",
149 		    (uintmax_t)address);
150 		ret = -1;
151 		goto done;
152 	}
153 
154 done:
155 	if (stopped)
156 		/* Restart the process if we had to stop it. */
157 		proc_continue(phdl);
158 
159 	return (ret);
160 }
161 
162 int
163 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
164     unsigned long saved)
165 {
166 	struct ptrace_io_desc piod;
167 	int ret = 0, stopped;
168 	instr_t instr;
169 
170 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
171 	    phdl->status == PS_IDLE) {
172 		errno = ENOENT;
173 		return (-1);
174 	}
175 
176 	DPRINTFX("removing breakpoint at 0x%lx", (unsigned long)address);
177 
178 	stopped = 0;
179 	if (phdl->status != PS_STOP) {
180 		if (proc_stop(phdl) != 0)
181 			return (-1);
182 		stopped = 1;
183 	}
184 
185 	/*
186 	 * Overwrite the breakpoint instruction that we setup previously.
187 	 */
188 	instr = saved;
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",
195 		    (uintmax_t)address);
196 		ret = -1;
197 	}
198 
199 	if (stopped)
200 		/* Restart the process if we had to stop it. */
201 		proc_continue(phdl);
202 
203 	return (ret);
204 }
205 
206 /*
207  * Decrement pc so that we delete the breakpoint at the correct
208  * address, i.e. at the BREAKPOINT_INSTR address.
209  *
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.
213  */
214 void
215 proc_bkptregadj(unsigned long *pc)
216 {
217 
218 	(void)pc;
219 #ifdef BREAKPOINT_ADJUST_SZ
220 	*pc = *pc - BREAKPOINT_ADJUST_SZ;
221 #endif
222 }
223 
224 /*
225  * Step over the breakpoint.
226  */
227 int
228 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
229 {
230 	unsigned long pc;
231 	unsigned long samesaved;
232 	int status;
233 
234 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
235 		DPRINTFX("ERROR: couldn't get PC register");
236 		return (-1);
237 	}
238 	proc_bkptregadj(&pc);
239 	if (proc_bkptdel(phdl, pc, saved) < 0) {
240 		DPRINTFX("ERROR: couldn't delete breakpoint");
241 		return (-1);
242 	}
243 	/*
244 	 * Go back in time and step over the new instruction just
245 	 * set up by proc_bkptdel().
246 	 */
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");
250 		return (-1);
251 	}
252 	proc_wstatus(phdl);
253 	status = proc_getwstat(phdl);
254 	if (!WIFSTOPPED(status)) {
255 		DPRINTFX("ERROR: don't know why process stopped");
256 		return (-1);
257 	}
258 	/*
259 	 * Restore the breakpoint. The saved instruction should be
260 	 * the same as the one that we were passed in.
261 	 */
262 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
263 		DPRINTFX("ERROR: couldn't restore breakpoint");
264 		return (-1);
265 	}
266 	assert(samesaved == saved);
267 
268 	return (0);
269 }
270