xref: /onnv-gate/usr/src/lib/libdtrace/i386/dt_isadep.c (revision 10791:944abfb5b345)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51464Sahl  * Common Development and Distribution License (the "License").
61464Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211464Sahl 
220Sstevel@tonic-gate /*
23*10791SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <assert.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <libgen.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <dt_impl.h>
340Sstevel@tonic-gate #include <dt_pid.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <dis_tables.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	DT_POPL_EBP	0x5d
390Sstevel@tonic-gate #define	DT_RET		0xc3
400Sstevel@tonic-gate #define	DT_RET16	0xc2
410Sstevel@tonic-gate #define	DT_LEAVE	0xc9
420Sstevel@tonic-gate #define	DT_JMP32	0xe9
430Sstevel@tonic-gate #define	DT_JMP8		0xeb
440Sstevel@tonic-gate #define	DT_REP		0xf3
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	DT_MOVL_EBP_ESP	0xe58b
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	DT_ISJ32(op16)	(((op16) & 0xfff0) == 0x0f80)
490Sstevel@tonic-gate #define	DT_ISJ8(op8)	(((op8) & 0xf0) == 0x70)
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	DT_MODRM_REG(modrm)	(((modrm) >> 3) & 0x7)
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static int dt_instr_size(uchar_t *, dtrace_hdl_t *, pid_t, uintptr_t, char);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*ARGSUSED*/
560Sstevel@tonic-gate int
dt_pid_create_entry_probe(struct ps_prochandle * P,dtrace_hdl_t * dtp,fasttrap_probe_spec_t * ftp,const GElf_Sym * symp)570Sstevel@tonic-gate dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
580Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_ENTRY;
610Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
620Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
630Sstevel@tonic-gate 	ftp->ftps_noffs = 1;
640Sstevel@tonic-gate 	ftp->ftps_offs[0] = 0;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
670Sstevel@tonic-gate 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
680Sstevel@tonic-gate 		    strerror(errno));
690Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	return (1);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static int
dt_pid_has_jump_table(struct ps_prochandle * P,dtrace_hdl_t * dtp,uint8_t * text,fasttrap_probe_spec_t * ftp,const GElf_Sym * symp)760Sstevel@tonic-gate dt_pid_has_jump_table(struct ps_prochandle *P, dtrace_hdl_t *dtp,
770Sstevel@tonic-gate     uint8_t *text, fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	ulong_t i;
800Sstevel@tonic-gate 	int size;
810Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
820Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * Take a pass through the function looking for a register-dependant
860Sstevel@tonic-gate 	 * jmp instruction. This could be a jump table so we have to be
870Sstevel@tonic-gate 	 * ultra conservative.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	for (i = 0; i < ftp->ftps_size; i += size) {
900Sstevel@tonic-gate 		size = dt_instr_size(&text[i], dtp, pid, symp->st_value + i,
910Sstevel@tonic-gate 		    dmodel);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		/*
940Sstevel@tonic-gate 		 * Assume the worst if we hit an illegal instruction.
950Sstevel@tonic-gate 		 */
960Sstevel@tonic-gate 		if (size <= 0) {
970Sstevel@tonic-gate 			dt_dprintf("error at %#lx (assuming jump table)\n", i);
980Sstevel@tonic-gate 			return (1);
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 
1011464Sahl 		/*
1021464Sahl 		 * Register-dependant jmp instructions start with a 0xff byte
1031464Sahl 		 * and have the modrm.reg field set to 4. They can have an
1041464Sahl 		 * optional REX prefix on the 64-bit ISA.
1051464Sahl 		 */
1061464Sahl 		if ((text[i] == 0xff && DT_MODRM_REG(text[i + 1]) == 4) ||
1071464Sahl 		    (dmodel == PR_MODEL_LP64 && (text[i] & 0xf0) == 0x40 &&
1081464Sahl 		    text[i + 1] == 0xff && DT_MODRM_REG(text[i + 2]) == 4)) {
1090Sstevel@tonic-gate 			dt_dprintf("found a suspected jump table at %s:%lx\n",
1100Sstevel@tonic-gate 			    ftp->ftps_func, i);
1110Sstevel@tonic-gate 			return (1);
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (0);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*ARGSUSED*/
1190Sstevel@tonic-gate int
dt_pid_create_return_probe(struct ps_prochandle * P,dtrace_hdl_t * dtp,fasttrap_probe_spec_t * ftp,const GElf_Sym * symp,uint64_t * stret)1200Sstevel@tonic-gate dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
1210Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	uint8_t *text;
1240Sstevel@tonic-gate 	ulong_t i, end;
1250Sstevel@tonic-gate 	int size;
1260Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
1270Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/*
1300Sstevel@tonic-gate 	 * We allocate a few extra bytes at the end so we don't have to check
1310Sstevel@tonic-gate 	 * for overrunning the buffer.
1320Sstevel@tonic-gate 	 */
1330Sstevel@tonic-gate 	if ((text = calloc(1, symp->st_size + 4)) == NULL) {
1340Sstevel@tonic-gate 		dt_dprintf("mr sparkle: malloc() failed\n");
1350Sstevel@tonic-gate 		return (DT_PROC_ERR);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
1390Sstevel@tonic-gate 		dt_dprintf("mr sparkle: Pread() failed\n");
1400Sstevel@tonic-gate 		free(text);
1410Sstevel@tonic-gate 		return (DT_PROC_ERR);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_RETURN;
1450Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
1460Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
1470Sstevel@tonic-gate 	ftp->ftps_noffs = 0;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * If there's a jump table in the function we're only willing to
1510Sstevel@tonic-gate 	 * instrument these specific (and equivalent) instruction sequences:
1520Sstevel@tonic-gate 	 *	leave
1530Sstevel@tonic-gate 	 *	[rep] ret
1540Sstevel@tonic-gate 	 * and
1550Sstevel@tonic-gate 	 *	movl	%ebp,%esp
1560Sstevel@tonic-gate 	 *	popl	%ebp
1570Sstevel@tonic-gate 	 *	[rep] ret
1580Sstevel@tonic-gate 	 *
1590Sstevel@tonic-gate 	 * We do this to avoid accidentally interpreting jump table
1600Sstevel@tonic-gate 	 * offsets as actual instructions.
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
1630Sstevel@tonic-gate 		for (i = 0, end = ftp->ftps_size; i < end; i += size) {
1640Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
1650Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
1680Sstevel@tonic-gate 			if (size <= 0)
1690Sstevel@tonic-gate 				break;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			if (text[i] == DT_LEAVE && text[i + 1] == DT_RET) {
1720Sstevel@tonic-gate 				dt_dprintf("leave/ret at %lx\n", i + 1);
1730Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
1740Sstevel@tonic-gate 				size = 2;
1750Sstevel@tonic-gate 			} else if (text[i] == DT_LEAVE &&
1760Sstevel@tonic-gate 			    text[i + 1] == DT_REP && text[i + 2] == DT_RET) {
1770Sstevel@tonic-gate 				dt_dprintf("leave/rep ret at %lx\n", i + 1);
1780Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
1790Sstevel@tonic-gate 				size = 3;
1800Sstevel@tonic-gate 			} else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
1810Sstevel@tonic-gate 			    text[i + 2] == DT_POPL_EBP &&
1820Sstevel@tonic-gate 			    text[i + 3] == DT_RET) {
1830Sstevel@tonic-gate 				dt_dprintf("movl/popl/ret at %lx\n", i + 3);
1840Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
1850Sstevel@tonic-gate 				size = 4;
1860Sstevel@tonic-gate 			} else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
1870Sstevel@tonic-gate 			    text[i + 2] == DT_POPL_EBP &&
1880Sstevel@tonic-gate 			    text[i + 3] == DT_REP &&
1890Sstevel@tonic-gate 			    text[i + 4] == DT_RET) {
1900Sstevel@tonic-gate 				dt_dprintf("movl/popl/rep ret at %lx\n", i + 3);
1910Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
1920Sstevel@tonic-gate 				size = 5;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 	} else {
1960Sstevel@tonic-gate 		for (i = 0, end = ftp->ftps_size; i < end; i += size) {
1970Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
1980Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
2010Sstevel@tonic-gate 			if (size <= 0)
2020Sstevel@tonic-gate 				break;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 			/* ordinary ret */
2050Sstevel@tonic-gate 			if (size == 1 && text[i] == DT_RET)
2060Sstevel@tonic-gate 				goto is_ret;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 			/* two-byte ret */
2090Sstevel@tonic-gate 			if (size == 2 && text[i] == DT_REP &&
2100Sstevel@tonic-gate 			    text[i + 1] == DT_RET)
2110Sstevel@tonic-gate 				goto is_ret;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 			/* ret <imm16> */
2140Sstevel@tonic-gate 			if (size == 3 && text[i] == DT_RET16)
2150Sstevel@tonic-gate 				goto is_ret;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 			/* two-byte ret <imm16> */
2180Sstevel@tonic-gate 			if (size == 4 && text[i] == DT_REP &&
2190Sstevel@tonic-gate 			    text[i + 1] == DT_RET16)
2200Sstevel@tonic-gate 				goto is_ret;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 			/* 32-bit displacement jmp outside of the function */
2230Sstevel@tonic-gate 			if (size == 5 && text[i] == DT_JMP32 && symp->st_size <=
2240Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int32_t *)&text[i + 1]))
2250Sstevel@tonic-gate 				goto is_ret;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 			/* 8-bit displacement jmp outside of the function */
2280Sstevel@tonic-gate 			if (size == 2 && text[i] == DT_JMP8 && symp->st_size <=
2290Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
2300Sstevel@tonic-gate 				goto is_ret;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 			/* 32-bit disp. conditional jmp outside of the func. */
2330Sstevel@tonic-gate 			if (size == 6 && DT_ISJ32(*(uint16_t *)&text[i]) &&
2340Sstevel@tonic-gate 			    symp->st_size <=
2350Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int32_t *)&text[i + 2]))
2360Sstevel@tonic-gate 				goto is_ret;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 			/* 8-bit disp. conditional jmp outside of the func. */
2390Sstevel@tonic-gate 			if (size == 2 && DT_ISJ8(text[i]) && symp->st_size <=
2400Sstevel@tonic-gate 			    (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
2410Sstevel@tonic-gate 				goto is_ret;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 			continue;
2440Sstevel@tonic-gate is_ret:
2450Sstevel@tonic-gate 			dt_dprintf("return at offset %lx\n", i);
2460Sstevel@tonic-gate 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	free(text);
2510Sstevel@tonic-gate 	if (ftp->ftps_noffs > 0) {
2520Sstevel@tonic-gate 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
2530Sstevel@tonic-gate 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
2540Sstevel@tonic-gate 			    strerror(errno));
2550Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (ftp->ftps_noffs);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*ARGSUSED*/
2630Sstevel@tonic-gate int
dt_pid_create_offset_probe(struct ps_prochandle * P,dtrace_hdl_t * dtp,fasttrap_probe_spec_t * ftp,const GElf_Sym * symp,ulong_t off)2640Sstevel@tonic-gate dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
2650Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	ftp->ftps_type = DTFTP_OFFSETS;
2680Sstevel@tonic-gate 	ftp->ftps_pc = (uintptr_t)symp->st_value;
2690Sstevel@tonic-gate 	ftp->ftps_size = (size_t)symp->st_size;
2700Sstevel@tonic-gate 	ftp->ftps_noffs = 1;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (strcmp("-", ftp->ftps_func) == 0) {
2730Sstevel@tonic-gate 		ftp->ftps_offs[0] = off;
2740Sstevel@tonic-gate 	} else {
2750Sstevel@tonic-gate 		uint8_t *text;
2760Sstevel@tonic-gate 		ulong_t i;
2770Sstevel@tonic-gate 		int size;
2780Sstevel@tonic-gate 		pid_t pid = Pstatus(P)->pr_pid;
2790Sstevel@tonic-gate 		char dmodel = Pstatus(P)->pr_dmodel;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		if ((text = malloc(symp->st_size)) == NULL) {
2820Sstevel@tonic-gate 			dt_dprintf("mr sparkle: malloc() failed\n");
2830Sstevel@tonic-gate 			return (DT_PROC_ERR);
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 		if (Pread(P, text, symp->st_size, symp->st_value) !=
2870Sstevel@tonic-gate 		    symp->st_size) {
2880Sstevel@tonic-gate 			dt_dprintf("mr sparkle: Pread() failed\n");
2890Sstevel@tonic-gate 			free(text);
2900Sstevel@tonic-gate 			return (DT_PROC_ERR);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * We can't instrument offsets in functions with jump tables
2950Sstevel@tonic-gate 		 * as we might interpret a jump table offset as an
2960Sstevel@tonic-gate 		 * instruction.
2970Sstevel@tonic-gate 		 */
2980Sstevel@tonic-gate 		if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
2990Sstevel@tonic-gate 			free(text);
3000Sstevel@tonic-gate 			return (0);
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		for (i = 0; i < symp->st_size; i += size) {
3040Sstevel@tonic-gate 			if (i == off) {
3050Sstevel@tonic-gate 				ftp->ftps_offs[0] = i;
3060Sstevel@tonic-gate 				break;
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			/*
3100Sstevel@tonic-gate 			 * If we've passed the desired offset without a
3110Sstevel@tonic-gate 			 * match, then the given offset must not lie on a
3120Sstevel@tonic-gate 			 * instruction boundary.
3130Sstevel@tonic-gate 			 */
3140Sstevel@tonic-gate 			if (i > off) {
3150Sstevel@tonic-gate 				free(text);
3160Sstevel@tonic-gate 				return (DT_PROC_ALIGN);
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
3200Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 			/*
3230Sstevel@tonic-gate 			 * If we hit an invalid instruction, bail as if we
3240Sstevel@tonic-gate 			 * couldn't find the offset.
3250Sstevel@tonic-gate 			 */
3260Sstevel@tonic-gate 			if (size <= 0) {
3270Sstevel@tonic-gate 				free(text);
3280Sstevel@tonic-gate 				return (DT_PROC_ALIGN);
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		free(text);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
3360Sstevel@tonic-gate 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
3370Sstevel@tonic-gate 		    strerror(errno));
3380Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	return (ftp->ftps_noffs);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*ARGSUSED*/
3450Sstevel@tonic-gate int
dt_pid_create_glob_offset_probes(struct ps_prochandle * P,dtrace_hdl_t * dtp,fasttrap_probe_spec_t * ftp,const GElf_Sym * symp,const char * pattern)3460Sstevel@tonic-gate dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
3470Sstevel@tonic-gate     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	uint8_t *text;
3500Sstevel@tonic-gate 	int size;
351*10791SJonathan.Haslam@Sun.COM 	ulong_t i, end = symp->st_size;
3520Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
3530Sstevel@tonic-gate 	char dmodel = Pstatus(P)->pr_dmodel;
3540Sstevel@tonic-gate 
355*10791SJonathan.Haslam@Sun.COM 	ftp->ftps_type = DTFTP_OFFSETS;
356*10791SJonathan.Haslam@Sun.COM 	ftp->ftps_pc = (uintptr_t)symp->st_value;
357*10791SJonathan.Haslam@Sun.COM 	ftp->ftps_size = (size_t)symp->st_size;
358*10791SJonathan.Haslam@Sun.COM 	ftp->ftps_noffs = 0;
359*10791SJonathan.Haslam@Sun.COM 
3600Sstevel@tonic-gate 	if ((text = malloc(symp->st_size)) == NULL) {
3610Sstevel@tonic-gate 		dt_dprintf("mr sparkle: malloc() failed\n");
3620Sstevel@tonic-gate 		return (DT_PROC_ERR);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
3660Sstevel@tonic-gate 		dt_dprintf("mr sparkle: Pread() failed\n");
3670Sstevel@tonic-gate 		free(text);
3680Sstevel@tonic-gate 		return (DT_PROC_ERR);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3720Sstevel@tonic-gate 	 * We can't instrument offsets in functions with jump tables as
3730Sstevel@tonic-gate 	 * we might interpret a jump table offset as an instruction.
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
3760Sstevel@tonic-gate 		free(text);
3770Sstevel@tonic-gate 		return (0);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if (strcmp("*", pattern) == 0) {
3810Sstevel@tonic-gate 		for (i = 0; i < end; i += size) {
3820Sstevel@tonic-gate 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
3850Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
3880Sstevel@tonic-gate 			if (size <= 0)
3890Sstevel@tonic-gate 				break;
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 	} else {
3920Sstevel@tonic-gate 		char name[sizeof (i) * 2 + 1];
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		for (i = 0; i < end; i += size) {
3950Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%x", i);
3960Sstevel@tonic-gate 			if (gmatch(name, pattern))
3970Sstevel@tonic-gate 				ftp->ftps_offs[ftp->ftps_noffs++] = i;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 			size = dt_instr_size(&text[i], dtp, pid,
4000Sstevel@tonic-gate 			    symp->st_value + i, dmodel);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 			/* bail if we hit an invalid opcode */
4030Sstevel@tonic-gate 			if (size <= 0)
4040Sstevel@tonic-gate 				break;
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	free(text);
4090Sstevel@tonic-gate 	if (ftp->ftps_noffs > 0) {
4100Sstevel@tonic-gate 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
4110Sstevel@tonic-gate 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
4120Sstevel@tonic-gate 			    strerror(errno));
4130Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	return (ftp->ftps_noffs);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate typedef struct dtrace_dis {
4210Sstevel@tonic-gate 	uchar_t	*instr;
4220Sstevel@tonic-gate 	dtrace_hdl_t *dtp;
4230Sstevel@tonic-gate 	pid_t pid;
4240Sstevel@tonic-gate 	uintptr_t addr;
4250Sstevel@tonic-gate } dtrace_dis_t;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate static int
dt_getbyte(void * data)4280Sstevel@tonic-gate dt_getbyte(void *data)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	dtrace_dis_t	*dis = data;
4310Sstevel@tonic-gate 	int ret = *dis->instr;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (ret == FASTTRAP_INSTR) {
4340Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		instr.ftiq_pid = dis->pid;
4370Sstevel@tonic-gate 		instr.ftiq_pc = dis->addr;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		/*
4400Sstevel@tonic-gate 		 * If we hit a byte that looks like the fasttrap provider's
4410Sstevel@tonic-gate 		 * trap instruction (which doubles as the breakpoint
4420Sstevel@tonic-gate 		 * instruction for debuggers) we need to query the kernel
4430Sstevel@tonic-gate 		 * for the real value. This may just be part of an immediate
4440Sstevel@tonic-gate 		 * value so there's no need to return an error if the
4450Sstevel@tonic-gate 		 * kernel doesn't know about this address.
4460Sstevel@tonic-gate 		 */
4470Sstevel@tonic-gate 		if (ioctl(dis->dtp->dt_ftfd, FASTTRAPIOC_GETINSTR, &instr) == 0)
4480Sstevel@tonic-gate 			ret = instr.ftiq_instr;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	dis->addr++;
4520Sstevel@tonic-gate 	dis->instr++;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	return (ret);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static int
dt_instr_size(uchar_t * instr,dtrace_hdl_t * dtp,pid_t pid,uintptr_t addr,char dmodel)4580Sstevel@tonic-gate dt_instr_size(uchar_t *instr, dtrace_hdl_t *dtp, pid_t pid, uintptr_t addr,
4590Sstevel@tonic-gate     char dmodel)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	dtrace_dis_t data;
4620Sstevel@tonic-gate 	dis86_t x86dis;
4630Sstevel@tonic-gate 	uint_t cpu_mode;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	data.instr = instr;
4660Sstevel@tonic-gate 	data.dtp = dtp;
4670Sstevel@tonic-gate 	data.pid = pid;
4680Sstevel@tonic-gate 	data.addr = addr;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	x86dis.d86_data = &data;
4710Sstevel@tonic-gate 	x86dis.d86_get_byte = dt_getbyte;
4720Sstevel@tonic-gate 	x86dis.d86_check_func = NULL;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	cpu_mode = (dmodel == PR_MODEL_ILP32) ? SIZE32 : SIZE64;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (dtrace_disx86(&x86dis, cpu_mode) != 0)
4770Sstevel@tonic-gate 		return (-1);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * If the instruction was a single-byte breakpoint, there may be
4810Sstevel@tonic-gate 	 * another debugger attached to this process. The original instruction
4820Sstevel@tonic-gate 	 * can't be recovered so this must fail.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	if (x86dis.d86_len == 1 && instr[0] == FASTTRAP_INSTR)
4850Sstevel@tonic-gate 		return (-1);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	return (x86dis.d86_len);
4880Sstevel@tonic-gate }
489