xref: /freebsd/lib/libproc/proc_bkpt.c (revision 119b75925c562202145d7bac7b676b98029c6cb9)
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 #include <machine/_inttypes.h>
37 
38 #include <assert.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdio.h>
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 #elif defined(__arm__)
55 #define	BREAKPOINT_INSTR	0xe7ffffff	/* bkpt */
56 #define	BREAKPOINT_INSTR_SZ	4
57 #elif defined(__mips__)
58 #define	BREAKPOINT_INSTR	0xd	/* break */
59 #define	BREAKPOINT_INSTR_SZ	4
60 #elif defined(__powerpc__)
61 #define	BREAKPOINT_INSTR	0x7fe00008	/* trap */
62 #define	BREAKPOINT_INSTR_SZ	4
63 #else
64 #error "Add support for your architecture"
65 #endif
66 
67 static int
68 proc_stop(struct proc_handle *phdl)
69 {
70 	int status;
71 
72 	if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
73 		DPRINTF("kill %d", proc_getpid(phdl));
74 		return (-1);
75 	} else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
76 		DPRINTF("waitpid %d", proc_getpid(phdl));
77 		return (-1);
78 	} else if (!WIFSTOPPED(status)) {
79 		DPRINTFX("waitpid: unexpected status 0x%x", status);
80 		return (-1);
81 	}
82 
83 	return (0);
84 }
85 
86 int
87 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
88     unsigned long *saved)
89 {
90 	struct ptrace_io_desc piod;
91 	unsigned long paddr, caddr;
92 	int ret = 0, stopped;
93 
94 	*saved = 0;
95 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
96 	    phdl->status == PS_IDLE) {
97 		errno = ENOENT;
98 		return (-1);
99 	}
100 
101 	DPRINTFX("adding breakpoint at 0x%lx", address);
102 
103 	stopped = 0;
104 	if (phdl->status != PS_STOP) {
105 		if (proc_stop(phdl) != 0)
106 			return (-1);
107 		stopped = 1;
108 	}
109 
110 	/*
111 	 * Read the original instruction.
112 	 */
113 	caddr = address;
114 	paddr = 0;
115 	piod.piod_op = PIOD_READ_I;
116 	piod.piod_offs = (void *)caddr;
117 	piod.piod_addr = &paddr;
118 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
119 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
120 		DPRINTF("ERROR: couldn't read instruction at address 0x%"
121 		    PRIuPTR, address);
122 		ret = -1;
123 		goto done;
124 	}
125 	*saved = paddr;
126 	/*
127 	 * Write a breakpoint instruction to that address.
128 	 */
129 	caddr = address;
130 	paddr = BREAKPOINT_INSTR;
131 	piod.piod_op = PIOD_WRITE_I;
132 	piod.piod_offs = (void *)caddr;
133 	piod.piod_addr = &paddr;
134 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
135 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
136 		DPRINTF("ERROR: couldn't write instruction at address 0x%"
137 		    PRIuPTR, address);
138 		ret = -1;
139 		goto done;
140 	}
141 
142 done:
143 	if (stopped)
144 		/* Restart the process if we had to stop it. */
145 		proc_continue(phdl);
146 
147 	return (ret);
148 }
149 
150 int
151 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
152     unsigned long saved)
153 {
154 	struct ptrace_io_desc piod;
155 	unsigned long paddr, caddr;
156 	int ret = 0, stopped;
157 
158 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
159 	    phdl->status == PS_IDLE) {
160 		errno = ENOENT;
161 		return (-1);
162 	}
163 
164 	DPRINTFX("removing breakpoint at 0x%lx", address);
165 
166 	stopped = 0;
167 	if (phdl->status != PS_STOP) {
168 		if (proc_stop(phdl) != 0)
169 			return (-1);
170 		stopped = 1;
171 	}
172 
173 	/*
174 	 * Overwrite the breakpoint instruction that we setup previously.
175 	 */
176 	caddr = address;
177 	paddr = saved;
178 	piod.piod_op = PIOD_WRITE_I;
179 	piod.piod_offs = (void *)caddr;
180 	piod.piod_addr = &paddr;
181 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
182 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
183 		DPRINTF("ERROR: couldn't write instruction at address 0x%"
184 		    PRIuPTR, address);
185 		ret = -1;
186 	}
187 
188 	if (stopped)
189 		/* Restart the process if we had to stop it. */
190 		proc_continue(phdl);
191 
192 	return (ret);
193 }
194 
195 /*
196  * Decrement pc so that we delete the breakpoint at the correct
197  * address, i.e. at the BREAKPOINT_INSTR address.
198  */
199 void
200 proc_bkptregadj(unsigned long *pc)
201 {
202 	*pc = *pc - BREAKPOINT_INSTR_SZ;
203 }
204 
205 /*
206  * Step over the breakpoint.
207  */
208 int
209 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
210 {
211 	unsigned long pc;
212 	unsigned long samesaved;
213 	int status;
214 
215 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
216 		DPRINTFX("ERROR: couldn't get PC register");
217 		return (-1);
218 	}
219 	proc_bkptregadj(&pc);
220 	if (proc_bkptdel(phdl, pc, saved) < 0) {
221 		DPRINTFX("ERROR: couldn't delete breakpoint");
222 		return (-1);
223 	}
224 	/*
225 	 * Go back in time and step over the new instruction just
226 	 * set up by proc_bkptdel().
227 	 */
228 	proc_regset(phdl, REG_PC, pc);
229 	if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
230 		DPRINTFX("ERROR: ptrace step failed");
231 		return (-1);
232 	}
233 	proc_wstatus(phdl);
234 	status = proc_getwstat(phdl);
235 	if (!WIFSTOPPED(status)) {
236 		DPRINTFX("ERROR: don't know why process stopped");
237 		return (-1);
238 	}
239 	/*
240 	 * Restore the breakpoint. The saved instruction should be
241 	 * the same as the one that we were passed in.
242 	 */
243 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
244 		DPRINTFX("ERROR: couldn't restore breakpoint");
245 		return (-1);
246 	}
247 	assert(samesaved == saved);
248 
249 	return (0);
250 }
251