xref: /onnv-gate/usr/src/lib/libdtrace/i386/dt_isadep.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <assert.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <libgen.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <dt_impl.h>
36*0Sstevel@tonic-gate #include <dt_pid.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <dis_tables.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #define	DT_POPL_EBP	0x5d
41*0Sstevel@tonic-gate #define	DT_RET		0xc3
42*0Sstevel@tonic-gate #define	DT_RET16	0xc2
43*0Sstevel@tonic-gate #define	DT_LEAVE	0xc9
44*0Sstevel@tonic-gate #define	DT_JMP32	0xe9
45*0Sstevel@tonic-gate #define	DT_JMP8		0xeb
46*0Sstevel@tonic-gate #define	DT_REP		0xf3
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	DT_MOVL_EBP_ESP	0xe58b
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	DT_ISJ32(op16)	(((op16) & 0xfff0) == 0x0f80)
51*0Sstevel@tonic-gate #define	DT_ISJ8(op8)	(((op8) & 0xf0) == 0x70)
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #define	DT_MODRM_REG(modrm)	(((modrm) >> 3) & 0x7)
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static int dt_instr_size(uchar_t *, dtrace_hdl_t *, pid_t, uintptr_t, char);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*ARGSUSED*/
58*0Sstevel@tonic-gate int
59*0Sstevel@tonic-gate dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
60*0Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_ENTRY;
63*0Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
64*0Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
65*0Sstevel@tonic-gate 	ftp->ftps_noffs = 1;
66*0Sstevel@tonic-gate 	ftp->ftps_offs[0] = 0;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
69*0Sstevel@tonic-gate 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
70*0Sstevel@tonic-gate 		    strerror(errno));
71*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	return (1);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int
78*0Sstevel@tonic-gate dt_pid_has_jump_table(struct ps_prochandle *P, dtrace_hdl_t *dtp,
79*0Sstevel@tonic-gate     uint8_t *text, fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	ulong_t i;
82*0Sstevel@tonic-gate 	int size;
83*0Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
84*0Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/*
87*0Sstevel@tonic-gate 	 * Take a pass through the function looking for a register-dependant
88*0Sstevel@tonic-gate 	 * jmp instruction. This could be a jump table so we have to be
89*0Sstevel@tonic-gate 	 * ultra conservative.
90*0Sstevel@tonic-gate 	 */
91*0Sstevel@tonic-gate 	for (i = 0; i < ftp->ftps_size; i += size) {
92*0Sstevel@tonic-gate 		size = dt_instr_size(&text[i], dtp, pid, symp->st_value + i,
93*0Sstevel@tonic-gate 		    dmodel);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 		/*
96*0Sstevel@tonic-gate 		 * Assume the worst if we hit an illegal instruction.
97*0Sstevel@tonic-gate 		 */
98*0Sstevel@tonic-gate 		if (size <= 0) {
99*0Sstevel@tonic-gate 			dt_dprintf("error at %#lx (assuming jump table)\n", i);
100*0Sstevel@tonic-gate 			return (1);
101*0Sstevel@tonic-gate 		}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		if (text[i] == 0xff && DT_MODRM_REG(text[i + 1]) == 4) {
104*0Sstevel@tonic-gate 			dt_dprintf("found a suspected jump table at %s:%lx\n",
105*0Sstevel@tonic-gate 			    ftp->ftps_func, i);
106*0Sstevel@tonic-gate 			return (1);
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	return (0);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*ARGSUSED*/
114*0Sstevel@tonic-gate int
115*0Sstevel@tonic-gate dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
116*0Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	uint8_t *text;
119*0Sstevel@tonic-gate 	ulong_t i, end;
120*0Sstevel@tonic-gate 	int size;
121*0Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
122*0Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	/*
125*0Sstevel@tonic-gate 	 * We allocate a few extra bytes at the end so we don't have to check
126*0Sstevel@tonic-gate 	 * for overrunning the buffer.
127*0Sstevel@tonic-gate 	 */
128*0Sstevel@tonic-gate 	if ((text = calloc(1, symp->st_size + 4)) == NULL) {
129*0Sstevel@tonic-gate 		dt_dprintf("mr sparkle: malloc() failed\n");
130*0Sstevel@tonic-gate 		return (DT_PROC_ERR);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
134*0Sstevel@tonic-gate 		dt_dprintf("mr sparkle: Pread() failed\n");
135*0Sstevel@tonic-gate 		free(text);
136*0Sstevel@tonic-gate 		return (DT_PROC_ERR);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_RETURN;
140*0Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
141*0Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
142*0Sstevel@tonic-gate 	ftp->ftps_noffs = 0;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * If there's a jump table in the function we're only willing to
146*0Sstevel@tonic-gate 	 * instrument these specific (and equivalent) instruction sequences:
147*0Sstevel@tonic-gate 	 *	leave
148*0Sstevel@tonic-gate 	 *	[rep] ret
149*0Sstevel@tonic-gate 	 * and
150*0Sstevel@tonic-gate 	 *	movl	%ebp,%esp
151*0Sstevel@tonic-gate 	 *	popl	%ebp
152*0Sstevel@tonic-gate 	 *	[rep] ret
153*0Sstevel@tonic-gate 	 *
154*0Sstevel@tonic-gate 	 * We do this to avoid accidentally interpreting jump table
155*0Sstevel@tonic-gate 	 * offsets as actual instructions.
156*0Sstevel@tonic-gate 	 */
157*0Sstevel@tonic-gate 	if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
158*0Sstevel@tonic-gate 		for (i = 0, end = ftp->ftps_size; i < end; i += size) {
159*0Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
160*0Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
163*0Sstevel@tonic-gate 			if (size <= 0)
164*0Sstevel@tonic-gate 				break;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 			if (text[i] == DT_LEAVE && text[i + 1] == DT_RET) {
167*0Sstevel@tonic-gate 				dt_dprintf("leave/ret at %lx\n", i + 1);
168*0Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
169*0Sstevel@tonic-gate 				size = 2;
170*0Sstevel@tonic-gate 			} else if (text[i] == DT_LEAVE &&
171*0Sstevel@tonic-gate 			    text[i + 1] == DT_REP && text[i + 2] == DT_RET) {
172*0Sstevel@tonic-gate 				dt_dprintf("leave/rep ret at %lx\n", i + 1);
173*0Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
174*0Sstevel@tonic-gate 				size = 3;
175*0Sstevel@tonic-gate 			} else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
176*0Sstevel@tonic-gate 			    text[i + 2] == DT_POPL_EBP &&
177*0Sstevel@tonic-gate 			    text[i + 3] == DT_RET) {
178*0Sstevel@tonic-gate 				dt_dprintf("movl/popl/ret at %lx\n", i + 3);
179*0Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
180*0Sstevel@tonic-gate 				size = 4;
181*0Sstevel@tonic-gate 			} else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
182*0Sstevel@tonic-gate 			    text[i + 2] == DT_POPL_EBP &&
183*0Sstevel@tonic-gate 			    text[i + 3] == DT_REP &&
184*0Sstevel@tonic-gate 			    text[i + 4] == DT_RET) {
185*0Sstevel@tonic-gate 				dt_dprintf("movl/popl/rep ret at %lx\n", i + 3);
186*0Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
187*0Sstevel@tonic-gate 				size = 5;
188*0Sstevel@tonic-gate 			}
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 	} else {
191*0Sstevel@tonic-gate 		for (i = 0, end = ftp->ftps_size; i < end; i += size) {
192*0Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
193*0Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
196*0Sstevel@tonic-gate 			if (size <= 0)
197*0Sstevel@tonic-gate 				break;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 			/* ordinary ret */
200*0Sstevel@tonic-gate 			if (size == 1 && text[i] == DT_RET)
201*0Sstevel@tonic-gate 				goto is_ret;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 			/* two-byte ret */
204*0Sstevel@tonic-gate 			if (size == 2 && text[i] == DT_REP &&
205*0Sstevel@tonic-gate 			    text[i + 1] == DT_RET)
206*0Sstevel@tonic-gate 				goto is_ret;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 			/* ret <imm16> */
209*0Sstevel@tonic-gate 			if (size == 3 && text[i] == DT_RET16)
210*0Sstevel@tonic-gate 				goto is_ret;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 			/* two-byte ret <imm16> */
213*0Sstevel@tonic-gate 			if (size == 4 && text[i] == DT_REP &&
214*0Sstevel@tonic-gate 			    text[i + 1] == DT_RET16)
215*0Sstevel@tonic-gate 				goto is_ret;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 			/* 32-bit displacement jmp outside of the function */
218*0Sstevel@tonic-gate 			if (size == 5 && text[i] == DT_JMP32 && symp->st_size <=
219*0Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int32_t *)&text[i + 1]))
220*0Sstevel@tonic-gate 				goto is_ret;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 			/* 8-bit displacement jmp outside of the function */
223*0Sstevel@tonic-gate 			if (size == 2 && text[i] == DT_JMP8 && symp->st_size <=
224*0Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
225*0Sstevel@tonic-gate 				goto is_ret;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 			/* 32-bit disp. conditional jmp outside of the func. */
228*0Sstevel@tonic-gate 			if (size == 6 && DT_ISJ32(*(uint16_t *)&text[i]) &&
229*0Sstevel@tonic-gate 			    symp->st_size <=
230*0Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int32_t *)&text[i + 2]))
231*0Sstevel@tonic-gate 				goto is_ret;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 			/* 8-bit disp. conditional jmp outside of the func. */
234*0Sstevel@tonic-gate 			if (size == 2 && DT_ISJ8(text[i]) && symp->st_size <=
235*0Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
236*0Sstevel@tonic-gate 				goto is_ret;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 			continue;
239*0Sstevel@tonic-gate is_ret:
240*0Sstevel@tonic-gate 			dt_dprintf("return at offset %lx\n", i);
241*0Sstevel@tonic-gate 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	free(text);
246*0Sstevel@tonic-gate 	if (ftp->ftps_noffs > 0) {
247*0Sstevel@tonic-gate 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
248*0Sstevel@tonic-gate 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
249*0Sstevel@tonic-gate 			    strerror(errno));
250*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	return (ftp->ftps_noffs);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate /*ARGSUSED*/
258*0Sstevel@tonic-gate int
259*0Sstevel@tonic-gate dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
260*0Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_OFFSETS;
263*0Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
264*0Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
265*0Sstevel@tonic-gate 	ftp->ftps_noffs = 1;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	if (strcmp("-", ftp->ftps_func) == 0) {
268*0Sstevel@tonic-gate 		ftp->ftps_offs[0] = off;
269*0Sstevel@tonic-gate 	} else {
270*0Sstevel@tonic-gate 		uint8_t *text;
271*0Sstevel@tonic-gate 		ulong_t i;
272*0Sstevel@tonic-gate 		int size;
273*0Sstevel@tonic-gate 		pid_t pid = Pstatus(P)->pr_pid;
274*0Sstevel@tonic-gate 		char dmodel = Pstatus(P)->pr_dmodel;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 		if ((text = malloc(symp->st_size)) == NULL) {
277*0Sstevel@tonic-gate 			dt_dprintf("mr sparkle: malloc() failed\n");
278*0Sstevel@tonic-gate 			return (DT_PROC_ERR);
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 		if (Pread(P, text, symp->st_size, symp->st_value) !=
282*0Sstevel@tonic-gate 		    symp->st_size) {
283*0Sstevel@tonic-gate 			dt_dprintf("mr sparkle: Pread() failed\n");
284*0Sstevel@tonic-gate 			free(text);
285*0Sstevel@tonic-gate 			return (DT_PROC_ERR);
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 		/*
289*0Sstevel@tonic-gate 		 * We can't instrument offsets in functions with jump tables
290*0Sstevel@tonic-gate 		 * as we might interpret a jump table offset as an
291*0Sstevel@tonic-gate 		 * instruction.
292*0Sstevel@tonic-gate 		 */
293*0Sstevel@tonic-gate 		if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
294*0Sstevel@tonic-gate 			free(text);
295*0Sstevel@tonic-gate 			return (0);
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 		for (i = 0; i < symp->st_size; i += size) {
299*0Sstevel@tonic-gate 			if (i == off) {
300*0Sstevel@tonic-gate 				ftp->ftps_offs[0] = i;
301*0Sstevel@tonic-gate 				break;
302*0Sstevel@tonic-gate 			}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 			/*
305*0Sstevel@tonic-gate 			 * If we've passed the desired offset without a
306*0Sstevel@tonic-gate 			 * match, then the given offset must not lie on a
307*0Sstevel@tonic-gate 			 * instruction boundary.
308*0Sstevel@tonic-gate 			 */
309*0Sstevel@tonic-gate 			if (i > off) {
310*0Sstevel@tonic-gate 				free(text);
311*0Sstevel@tonic-gate 				return (DT_PROC_ALIGN);
312*0Sstevel@tonic-gate 			}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
315*0Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 			/*
318*0Sstevel@tonic-gate 			 * If we hit an invalid instruction, bail as if we
319*0Sstevel@tonic-gate 			 * couldn't find the offset.
320*0Sstevel@tonic-gate 			 */
321*0Sstevel@tonic-gate 			if (size <= 0) {
322*0Sstevel@tonic-gate 				free(text);
323*0Sstevel@tonic-gate 				return (DT_PROC_ALIGN);
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 		}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 		free(text);
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
331*0Sstevel@tonic-gate 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
332*0Sstevel@tonic-gate 		    strerror(errno));
333*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	return (ftp->ftps_noffs);
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate /*ARGSUSED*/
340*0Sstevel@tonic-gate int
341*0Sstevel@tonic-gate dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
342*0Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 	uint8_t *text;
345*0Sstevel@tonic-gate 	ulong_t i, end;
346*0Sstevel@tonic-gate 	int size;
347*0Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
348*0Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	if ((text = malloc(symp->st_size)) == NULL) {
351*0Sstevel@tonic-gate 		dt_dprintf("mr sparkle: malloc() failed\n");
352*0Sstevel@tonic-gate 		return (DT_PROC_ERR);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
356*0Sstevel@tonic-gate 		dt_dprintf("mr sparkle: Pread() failed\n");
357*0Sstevel@tonic-gate 		free(text);
358*0Sstevel@tonic-gate 		return (DT_PROC_ERR);
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	/*
362*0Sstevel@tonic-gate 	 * We can't instrument offsets in functions with jump tables as
363*0Sstevel@tonic-gate 	 * we might interpret a jump table offset as an instruction.
364*0Sstevel@tonic-gate 	 */
365*0Sstevel@tonic-gate 	if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
366*0Sstevel@tonic-gate 		free(text);
367*0Sstevel@tonic-gate 		return (0);
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_OFFSETS;
371*0Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
372*0Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
373*0Sstevel@tonic-gate 	ftp->ftps_noffs = 0;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	end = ftp->ftps_size;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (strcmp("*", pattern) == 0) {
378*0Sstevel@tonic-gate 		for (i = 0; i < end; i += size) {
379*0Sstevel@tonic-gate 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
382*0Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
385*0Sstevel@tonic-gate 			if (size <= 0)
386*0Sstevel@tonic-gate 				break;
387*0Sstevel@tonic-gate 		}
388*0Sstevel@tonic-gate 	} else {
389*0Sstevel@tonic-gate 		char name[sizeof (i) * 2 + 1];
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 		for (i = 0; i < end; i += size) {
392*0Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%x", i);
393*0Sstevel@tonic-gate 			if (gmatch(name, pattern))
394*0Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
397*0Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
400*0Sstevel@tonic-gate 			if (size <= 0)
401*0Sstevel@tonic-gate 				break;
402*0Sstevel@tonic-gate 		}
403*0Sstevel@tonic-gate 	}
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	free(text);
406*0Sstevel@tonic-gate 	if (ftp->ftps_noffs > 0) {
407*0Sstevel@tonic-gate 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
408*0Sstevel@tonic-gate 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
409*0Sstevel@tonic-gate 			    strerror(errno));
410*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 	}
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	return (ftp->ftps_noffs);
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate typedef struct dtrace_dis {
418*0Sstevel@tonic-gate 	uchar_t	*instr;
419*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp;
420*0Sstevel@tonic-gate 	pid_t pid;
421*0Sstevel@tonic-gate 	uintptr_t addr;
422*0Sstevel@tonic-gate } dtrace_dis_t;
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate static int
425*0Sstevel@tonic-gate dt_getbyte(void *data)
426*0Sstevel@tonic-gate {
427*0Sstevel@tonic-gate 	dtrace_dis_t	*dis = data;
428*0Sstevel@tonic-gate 	int ret = *dis->instr;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	if (ret == FASTTRAP_INSTR) {
431*0Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 		instr.ftiq_pid = dis->pid;
434*0Sstevel@tonic-gate 		instr.ftiq_pc = dis->addr;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		/*
437*0Sstevel@tonic-gate 		 * If we hit a byte that looks like the fasttrap provider's
438*0Sstevel@tonic-gate 		 * trap instruction (which doubles as the breakpoint
439*0Sstevel@tonic-gate 		 * instruction for debuggers) we need to query the kernel
440*0Sstevel@tonic-gate 		 * for the real value. This may just be part of an immediate
441*0Sstevel@tonic-gate 		 * value so there's no need to return an error if the
442*0Sstevel@tonic-gate 		 * kernel doesn't know about this address.
443*0Sstevel@tonic-gate 		 */
444*0Sstevel@tonic-gate 		if (ioctl(dis->dtp->dt_ftfd, FASTTRAPIOC_GETINSTR, &instr) == 0)
445*0Sstevel@tonic-gate 			ret = instr.ftiq_instr;
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	dis->addr++;
449*0Sstevel@tonic-gate 	dis->instr++;
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	return (ret);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate static int
455*0Sstevel@tonic-gate dt_instr_size(uchar_t *instr, dtrace_hdl_t *dtp, pid_t pid, uintptr_t addr,
456*0Sstevel@tonic-gate     char dmodel)
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	dtrace_dis_t data;
459*0Sstevel@tonic-gate 	dis86_t x86dis;
460*0Sstevel@tonic-gate 	uint_t cpu_mode;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	data.instr = instr;
463*0Sstevel@tonic-gate 	data.dtp = dtp;
464*0Sstevel@tonic-gate 	data.pid = pid;
465*0Sstevel@tonic-gate 	data.addr = addr;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	x86dis.d86_data = &data;
468*0Sstevel@tonic-gate 	x86dis.d86_get_byte = dt_getbyte;
469*0Sstevel@tonic-gate 	x86dis.d86_check_func = NULL;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	cpu_mode = (dmodel == PR_MODEL_ILP32) ? SIZE32 : SIZE64;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	if (dtrace_disx86(&x86dis, cpu_mode) != 0)
474*0Sstevel@tonic-gate 		return (-1);
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	/*
477*0Sstevel@tonic-gate 	 * If the instruction was a single-byte breakpoint, there may be
478*0Sstevel@tonic-gate 	 * another debugger attached to this process. The original instruction
479*0Sstevel@tonic-gate 	 * can't be recovered so this must fail.
480*0Sstevel@tonic-gate 	 */
481*0Sstevel@tonic-gate 	if (x86dis.d86_len == 1 && instr[0] == FASTTRAP_INSTR)
482*0Sstevel@tonic-gate 		return (-1);
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	return (x86dis.d86_len);
485*0Sstevel@tonic-gate }
486