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