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