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