xref: /freebsd/lib/libproc/proc_bkpt.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
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 <stdio.h>
41 #include <errno.h>
42 #include "_libproc.h"
43 
44 #if defined(__i386__) || defined(__amd64__)
45 #define BREAKPOINT_INSTR	0xcc	/* int 0x3 */
46 #define	BREAKPOINT_INSTR_SZ	1
47 #elif defined(__mips__)
48 #define BREAKPOINT_INSTR	0xd	/* break */
49 #define	BREAKPOINT_INSTR_SZ	4
50 #elif defined(__powerpc__)
51 #define BREAKPOINT_INSTR	0x7fe00008	/* trap */
52 #define BREAKPOINT_INSTR_SZ 4
53 #else
54 #error "Add support for your architecture"
55 #endif
56 
57 int
58 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
59     unsigned long *saved)
60 {
61 	struct ptrace_io_desc piod;
62 	unsigned long paddr, caddr;
63 
64 	*saved = 0;
65 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
66 	    phdl->status == PS_IDLE) {
67 		errno = ENOENT;
68 		return (-1);
69 	}
70 
71 	/*
72 	 * Read the original instruction.
73 	 */
74 	caddr = address;
75 	paddr = 0;
76 	piod.piod_op = PIOD_READ_I;
77 	piod.piod_offs = (void *)caddr;
78 	piod.piod_addr = &paddr;
79 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
80 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
81 		DPRINTF("ERROR: couldn't read instruction at address 0x%" PRIuPTR,
82 		    address);
83 		return (-1);
84 	}
85 	*saved = paddr;
86 	/*
87 	 * Write a breakpoint instruction to that address.
88 	 */
89 	caddr = address;
90 	paddr = BREAKPOINT_INSTR;
91 	piod.piod_op = PIOD_WRITE_I;
92 	piod.piod_offs = (void *)caddr;
93 	piod.piod_addr = &paddr;
94 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
95 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
96 		warn("ERROR: couldn't write instruction at address 0x%" PRIuPTR,
97 		    address);
98 		return (-1);
99 	}
100 
101 	return (0);
102 }
103 
104 int
105 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
106     unsigned long saved)
107 {
108 	struct ptrace_io_desc piod;
109 	unsigned long paddr, caddr;
110 
111 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
112 	    phdl->status == PS_IDLE) {
113 		errno = ENOENT;
114 		return (-1);
115 	}
116 	DPRINTF("removing breakpoint at 0x%lx\n", address);
117 	/*
118 	 * Overwrite the breakpoint instruction that we setup previously.
119 	 */
120 	caddr = address;
121 	paddr = saved;
122 	piod.piod_op = PIOD_WRITE_I;
123 	piod.piod_offs = (void *)caddr;
124 	piod.piod_addr = &paddr;
125 	piod.piod_len  = BREAKPOINT_INSTR_SZ;
126 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
127 		DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR,
128 		    address);
129 		return (-1);
130 	}
131 
132 	return (0);
133 }
134 
135 /*
136  * Decrement pc so that we delete the breakpoint at the correct
137  * address, i.e. at the BREAKPOINT_INSTR address.
138  */
139 void
140 proc_bkptregadj(unsigned long *pc)
141 {
142 	*pc = *pc - BREAKPOINT_INSTR_SZ;
143 }
144 
145 /*
146  * Step over the breakpoint.
147  */
148 int
149 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
150 {
151 	unsigned long pc;
152 	unsigned long samesaved;
153 	int status;
154 
155 	if (proc_regget(phdl, REG_PC, &pc) < 0) {
156 		warn("ERROR: couldn't get PC register");
157 		return (-1);
158 	}
159 	proc_bkptregadj(&pc);
160 	if (proc_bkptdel(phdl, pc, saved) < 0) {
161 		warn("ERROR: couldn't delete breakpoint");
162 		return (-1);
163 	}
164 	/*
165 	 * Go back in time and step over the new instruction just
166 	 * set up by proc_bkptdel().
167 	 */
168 	proc_regset(phdl, REG_PC, pc);
169 	if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
170 		warn("ERROR: ptrace step failed");
171 		return (-1);
172 	}
173 	proc_wstatus(phdl);
174 	status = proc_getwstat(phdl);
175 	if (!WIFSTOPPED(status)) {
176 		warn("ERROR: don't know why process stopped");
177 		return (-1);
178 	}
179 	/*
180 	 * Restore the breakpoint. The saved instruction should be
181 	 * the same as the one that we were passed in.
182 	 */
183 	if (proc_bkptset(phdl, pc, &samesaved) < 0) {
184 		warn("ERROR: couldn't restore breakpoint");
185 		return (-1);
186 	}
187 	assert(samesaved == saved);
188 
189 	return (0);
190 }
191