xref: /freebsd/lib/libproc/proc_bkpt.c (revision ea825d02749f382c3f7e17f28247f20a48733eab)
1 /*-
2  * Copyright (c) 2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Rui Paulo under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
35 #include <sys/wait.h>
36 
37 #include <assert.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <stdio.h>
42 
43 #include "_libproc.h"
44 
45 #if defined(__aarch64__)
46 #define	AARCH64_BRK		0xd4200000
47 #define	AARCH64_BRK_IMM16_SHIFT	5
48 #define	AARCH64_BRK_IMM16_VAL	(0xd << AARCH64_BRK_IMM16_SHIFT)
49 #define	BREAKPOINT_INSTR	(AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
50 #define	BREAKPOINT_INSTR_SZ	4
51 #elif defined(__amd64__) || defined(__i386__)
52 #define	BREAKPOINT_INSTR	0xcc	/* int 0x3 */
53 #define	BREAKPOINT_INSTR_SZ	1
54 #define	BREAKPOINT_ADJUST_SZ	BREAKPOINT_INSTR_SZ
55 #elif defined(__arm__)
56 #define	BREAKPOINT_INSTR	0xe7ffffff	/* bkpt */
57 #define	BREAKPOINT_INSTR_SZ	4
58 #elif defined(__mips__)
59 #define	BREAKPOINT_INSTR	0xd	/* break */
60 #define	BREAKPOINT_INSTR_SZ	4
61 #elif defined(__powerpc__)
62 #define	BREAKPOINT_INSTR	0x7fe00008	/* trap */
63 #define	BREAKPOINT_INSTR_SZ	4
64 #elif defined(__riscv)
65 #define	BREAKPOINT_INSTR	0x00100073	/* sbreak */
66 #define	BREAKPOINT_INSTR_SZ	4
67 #else
68 #error "Add support for your architecture"
69 #endif
70 
71 /*
72  * Use 4-bytes holder for breakpoint instruction on all the platforms.
73  * Works for x86 as well until it is endian-little platform.
74  * (We are coping one byte only on x86 from this 4-bytes piece of
75  * memory).
76  */
77 typedef uint32_t instr_t;
78 
79 static int
80 proc_stop(struct proc_handle *phdl)
81 {
82 	int status;
83 
84 	if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
85 		DPRINTF("kill %d", proc_getpid(phdl));
86 		return (-1);
87 	} else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
88 		DPRINTF("waitpid %d", proc_getpid(phdl));
89 		return (-1);
90 	} else if (!WIFSTOPPED(status)) {
91 		DPRINTFX("waitpid: unexpected status 0x%x", status);
92 		return (-1);
93 	}
94 
95 	return (0);
96 }
97 
98 int
99 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
100     unsigned long *saved)
101 {
102 	struct ptrace_io_desc piod;
103 	unsigned long caddr;
104 	int ret = 0, stopped;
105 	instr_t instr;
106 
107 	*saved = 0;
108 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
109 	    phdl->status == PS_IDLE) {
110 		errno = ENOENT;
111 		return (-1);
112 	}
113 
114 	DPRINTFX("adding breakpoint at 0x%lx", address);
115 
116 	stopped = 0;
117 	if (phdl->status != PS_STOP) {
118 		if (proc_stop(phdl) != 0)
119 			return (-1);
120 		stopped = 1;
121 	}
122 
123 	/*
124 	 * Read the original instruction.
125 	 */
126 	caddr = address;
127 	instr = 0;
128 	piod.piod_op = PIOD_READ_I;
129 	piod.piod_offs = (void *)caddr;
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 	caddr = address;
143 	instr = BREAKPOINT_INSTR;
144 	piod.piod_op = PIOD_WRITE_I;
145 	piod.piod_offs = (void *)caddr;
146 	piod.piod_addr = &instr;
147 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
148 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
149 		DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
150 		    (uintmax_t)address);
151 		ret = -1;
152 		goto done;
153 	}
154 
155 done:
156 	if (stopped)
157 		/* Restart the process if we had to stop it. */
158 		proc_continue(phdl);
159 
160 	return (ret);
161 }
162 
163 int
164 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
165     unsigned long saved)
166 {
167 	struct ptrace_io_desc piod;
168 	unsigned long caddr;
169 	int ret = 0, stopped;
170 	instr_t instr;
171 
172 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
173 	    phdl->status == PS_IDLE) {
174 		errno = ENOENT;
175 		return (-1);
176 	}
177 
178 	DPRINTFX("removing breakpoint at 0x%lx", address);
179 
180 	stopped = 0;
181 	if (phdl->status != PS_STOP) {
182 		if (proc_stop(phdl) != 0)
183 			return (-1);
184 		stopped = 1;
185 	}
186 
187 	/*
188 	 * Overwrite the breakpoint instruction that we setup previously.
189 	 */
190 	caddr = address;
191 	instr = saved;
192 	piod.piod_op = PIOD_WRITE_I;
193 	piod.piod_offs = (void *)caddr;
194 	piod.piod_addr = &instr;
195 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
196 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
197 		DPRINTF("ERROR: couldn't write instruction at address 0x%jx",
198 		    (uintmax_t)address);
199 		ret = -1;
200 	}
201 
202 	if (stopped)
203 		/* Restart the process if we had to stop it. */
204 		proc_continue(phdl);
205 
206 	return (ret);
207 }
208 
209 /*
210  * Decrement pc so that we delete the breakpoint at the correct
211  * address, i.e. at the BREAKPOINT_INSTR address.
212  *
213  * This is only needed on some architectures where the pc value
214  * when reading registers points at the instruction after the
215  * breakpoint, e.g. x86.
216  */
217 void
218 proc_bkptregadj(unsigned long *pc)
219 {
220 
221 	(void)pc;
222 #ifdef BREAKPOINT_ADJUST_SZ
223 	*pc = *pc - BREAKPOINT_ADJUST_SZ;
224 #endif
225 }
226 
227 /*
228  * Step over the breakpoint.
229  */
230 int
231 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
232 {
233 	unsigned long pc;
234 	unsigned long samesaved;
235 	int status;
236 
237 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
238 		DPRINTFX("ERROR: couldn't get PC register");
239 		return (-1);
240 	}
241 	proc_bkptregadj(&pc);
242 	if (proc_bkptdel(phdl, pc, saved) < 0) {
243 		DPRINTFX("ERROR: couldn't delete breakpoint");
244 		return (-1);
245 	}
246 	/*
247 	 * Go back in time and step over the new instruction just
248 	 * set up by proc_bkptdel().
249 	 */
250 	proc_regset(phdl, REG_PC, pc);
251 	if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
252 		DPRINTFX("ERROR: ptrace step failed");
253 		return (-1);
254 	}
255 	proc_wstatus(phdl);
256 	status = proc_getwstat(phdl);
257 	if (!WIFSTOPPED(status)) {
258 		DPRINTFX("ERROR: don't know why process stopped");
259 		return (-1);
260 	}
261 	/*
262 	 * Restore the breakpoint. The saved instruction should be
263 	 * the same as the one that we were passed in.
264 	 */
265 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
266 		DPRINTFX("ERROR: couldn't restore breakpoint");
267 		return (-1);
268 	}
269 	assert(samesaved == saved);
270 
271 	return (0);
272 }
273