xref: /freebsd/lib/libproc/proc_bkpt.c (revision ec965063070e5753c166cf592c9336444b74720a)
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(__powerpc__)
61 #define	BREAKPOINT_INSTR	0x7fe00008	/* trap */
62 #define	BREAKPOINT_INSTR_SZ	4
63 #elif defined(__riscv)
64 #define	BREAKPOINT_INSTR	0x00100073	/* sbreak */
65 #define	BREAKPOINT_INSTR_SZ	4
66 #else
67 #error "Add support for your architecture"
68 #endif
69 
70 /*
71  * Use 4-bytes holder for breakpoint instruction on all the platforms.
72  * Works for x86 as well until it is endian-little platform.
73  * (We are coping one byte only on x86 from this 4-bytes piece of
74  * memory).
75  */
76 typedef uint32_t instr_t;
77 
78 static int
79 proc_stop(struct proc_handle *phdl)
80 {
81 	int status;
82 
83 	if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
84 		DPRINTF("kill %d", proc_getpid(phdl));
85 		return (-1);
86 	} else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
87 		DPRINTF("waitpid %d", proc_getpid(phdl));
88 		return (-1);
89 	} else if (!WIFSTOPPED(status)) {
90 		DPRINTFX("waitpid: unexpected status 0x%x", status);
91 		return (-1);
92 	}
93 
94 	return (0);
95 }
96 
97 int
98 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
99     unsigned long *saved)
100 {
101 	struct ptrace_io_desc piod;
102 	int ret = 0, stopped;
103 	instr_t instr;
104 
105 	*saved = 0;
106 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
107 	    phdl->status == PS_IDLE) {
108 		errno = ENOENT;
109 		return (-1);
110 	}
111 
112 	DPRINTFX("adding breakpoint at 0x%lx", (unsigned long)address);
113 
114 	stopped = 0;
115 	if (phdl->status != PS_STOP) {
116 		if (proc_stop(phdl) != 0)
117 			return (-1);
118 		stopped = 1;
119 	}
120 
121 	/*
122 	 * Read the original instruction.
123 	 */
124 	instr = 0;
125 	piod.piod_op = PIOD_READ_I;
126 	piod.piod_offs = (void *)address;
127 	piod.piod_addr = &instr;
128 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
129 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
130 		DPRINTF("ERROR: couldn't read instruction at address 0x%jx",
131 		    (uintmax_t)address);
132 		ret = -1;
133 		goto done;
134 	}
135 	*saved = instr;
136 	/*
137 	 * Write a breakpoint instruction to that address.
138 	 */
139 	instr = BREAKPOINT_INSTR;
140 	piod.piod_op = PIOD_WRITE_I;
141 	piod.piod_offs = (void *)address;
142 	piod.piod_addr = &instr;
143 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
144 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
145 		DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
146 		    (uintmax_t)address);
147 		ret = -1;
148 		goto done;
149 	}
150 
151 done:
152 	if (stopped)
153 		/* Restart the process if we had to stop it. */
154 		proc_continue(phdl);
155 
156 	return (ret);
157 }
158 
159 int
160 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
161     unsigned long saved)
162 {
163 	struct ptrace_io_desc piod;
164 	int ret = 0, stopped;
165 	instr_t instr;
166 
167 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
168 	    phdl->status == PS_IDLE) {
169 		errno = ENOENT;
170 		return (-1);
171 	}
172 
173 	DPRINTFX("removing breakpoint at 0x%lx", (unsigned long)address);
174 
175 	stopped = 0;
176 	if (phdl->status != PS_STOP) {
177 		if (proc_stop(phdl) != 0)
178 			return (-1);
179 		stopped = 1;
180 	}
181 
182 	/*
183 	 * Overwrite the breakpoint instruction that we setup previously.
184 	 */
185 	instr = saved;
186 	piod.piod_op = PIOD_WRITE_I;
187 	piod.piod_offs = (void *)address;
188 	piod.piod_addr = &instr;
189 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
190 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
191 		DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
192 		    (uintmax_t)address);
193 		ret = -1;
194 	}
195 
196 	if (stopped)
197 		/* Restart the process if we had to stop it. */
198 		proc_continue(phdl);
199 
200 	return (ret);
201 }
202 
203 /*
204  * Decrement pc so that we delete the breakpoint at the correct
205  * address, i.e. at the BREAKPOINT_INSTR address.
206  *
207  * This is only needed on some architectures where the pc value
208  * when reading registers points at the instruction after the
209  * breakpoint, e.g. x86.
210  */
211 void
212 proc_bkptregadj(unsigned long *pc)
213 {
214 
215 	(void)pc;
216 #ifdef BREAKPOINT_ADJUST_SZ
217 	*pc = *pc - BREAKPOINT_ADJUST_SZ;
218 #endif
219 }
220 
221 /*
222  * Step over the breakpoint.
223  */
224 int
225 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
226 {
227 	unsigned long pc;
228 	unsigned long samesaved;
229 	int status;
230 
231 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
232 		DPRINTFX("ERROR: couldn't get PC register");
233 		return (-1);
234 	}
235 	proc_bkptregadj(&pc);
236 	if (proc_bkptdel(phdl, pc, saved) < 0) {
237 		DPRINTFX("ERROR: couldn't delete breakpoint");
238 		return (-1);
239 	}
240 	/*
241 	 * Go back in time and step over the new instruction just
242 	 * set up by proc_bkptdel().
243 	 */
244 	proc_regset(phdl, REG_PC, pc);
245 	if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
246 		DPRINTFX("ERROR: ptrace step failed");
247 		return (-1);
248 	}
249 	proc_wstatus(phdl);
250 	status = proc_getwstat(phdl);
251 	if (!WIFSTOPPED(status)) {
252 		DPRINTFX("ERROR: don't know why process stopped");
253 		return (-1);
254 	}
255 	/*
256 	 * Restore the breakpoint. The saved instruction should be
257 	 * the same as the one that we were passed in.
258 	 */
259 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
260 		DPRINTFX("ERROR: couldn't restore breakpoint");
261 		return (-1);
262 	}
263 	assert(samesaved == saved);
264 
265 	return (0);
266 }
267