4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
40 #include <dis_tables.h>
44 #include <libproc_compat.h>
47 #define DT_POPL_EBP 0x5d
55 #define DT_MOVL_EBP_ESP 0xe58b
57 #define DT_ISJ32(op16) (((op16) & 0xfff0) == 0x0f80)
58 #define DT_ISJ8(op8) (((op8) & 0xf0) == 0x70)
60 #define DT_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
62 static int dt_instr_size(uchar_t *, dtrace_hdl_t *, pid_t, uintptr_t, char);
66 dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
67 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
69 ftp->ftps_type = DTFTP_ENTRY;
70 ftp->ftps_pc = (uintptr_t)symp->st_value;
71 ftp->ftps_size = (size_t)symp->st_size;
73 ftp->ftps_offs[0] = 0;
75 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
76 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
78 return (dt_set_errno(dtp, errno));
85 dt_pid_has_jump_table(struct ps_prochandle *P, dtrace_hdl_t *dtp,
86 uint8_t *text, fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
91 pid_t pid = Pstatus(P)->pr_pid;
92 char dmodel = Pstatus(P)->pr_dmodel;
94 pid_t pid = proc_getpid(P);
95 char dmodel = proc_getmodel(P);
99 * Take a pass through the function looking for a register-dependant
100 * jmp instruction. This could be a jump table so we have to be
101 * ultra conservative.
103 for (i = 0; i < ftp->ftps_size; i += size) {
104 size = dt_instr_size(&text[i], dtp, pid, symp->st_value + i,
108 * Assume the worst if we hit an illegal instruction.
111 dt_dprintf("error at %#lx (assuming jump table)\n", i);
117 * Register-dependant jmp instructions start with a 0xff byte
118 * and have the modrm.reg field set to 4. They can have an
119 * optional REX prefix on the 64-bit ISA.
121 if ((text[i] == 0xff && DT_MODRM_REG(text[i + 1]) == 4) ||
122 (dmodel == PR_MODEL_LP64 && (text[i] & 0xf0) == 0x40 &&
123 text[i + 1] == 0xff && DT_MODRM_REG(text[i + 2]) == 4)) {
124 dt_dprintf("found a suspected jump table at %s:%lx\n",
136 dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
137 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
143 pid_t pid = Pstatus(P)->pr_pid;
144 char dmodel = Pstatus(P)->pr_dmodel;
146 pid_t pid = proc_getpid(P);
147 char dmodel = proc_getmodel(P);
151 * We allocate a few extra bytes at the end so we don't have to check
152 * for overrunning the buffer.
154 if ((text = calloc(1, symp->st_size + 4)) == NULL) {
155 dt_dprintf("mr sparkle: malloc() failed\n");
156 return (DT_PROC_ERR);
159 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
160 dt_dprintf("mr sparkle: Pread() failed\n");
162 return (DT_PROC_ERR);
165 ftp->ftps_type = DTFTP_RETURN;
166 ftp->ftps_pc = (uintptr_t)symp->st_value;
167 ftp->ftps_size = (size_t)symp->st_size;
171 * If there's a jump table in the function we're only willing to
172 * instrument these specific (and equivalent) instruction sequences:
180 * We do this to avoid accidentally interpreting jump table
181 * offsets as actual instructions.
183 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
184 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
185 size = dt_instr_size(&text[i], dtp, pid,
186 symp->st_value + i, dmodel);
188 /* bail if we hit an invalid opcode */
192 if (text[i] == DT_LEAVE && text[i + 1] == DT_RET) {
193 dt_dprintf("leave/ret at %lx\n", i + 1);
194 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
196 } else if (text[i] == DT_LEAVE &&
197 text[i + 1] == DT_REP && text[i + 2] == DT_RET) {
198 dt_dprintf("leave/rep ret at %lx\n", i + 1);
199 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
201 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
202 text[i + 2] == DT_POPL_EBP &&
203 text[i + 3] == DT_RET) {
204 dt_dprintf("movl/popl/ret at %lx\n", i + 3);
205 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
207 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
208 text[i + 2] == DT_POPL_EBP &&
209 text[i + 3] == DT_REP &&
210 text[i + 4] == DT_RET) {
211 dt_dprintf("movl/popl/rep ret at %lx\n", i + 3);
212 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
217 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
218 size = dt_instr_size(&text[i], dtp, pid,
219 symp->st_value + i, dmodel);
221 /* bail if we hit an invalid opcode */
226 if (size == 1 && text[i] == DT_RET)
230 if (size == 2 && text[i] == DT_REP &&
231 text[i + 1] == DT_RET)
235 if (size == 3 && text[i] == DT_RET16)
238 /* two-byte ret <imm16> */
239 if (size == 4 && text[i] == DT_REP &&
240 text[i + 1] == DT_RET16)
243 /* 32-bit displacement jmp outside of the function */
244 if (size == 5 && text[i] == DT_JMP32 && symp->st_size <=
245 (uintptr_t)(i + size + *(int32_t *)&text[i + 1]))
248 /* 8-bit displacement jmp outside of the function */
249 if (size == 2 && text[i] == DT_JMP8 && symp->st_size <=
250 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
253 /* 32-bit disp. conditional jmp outside of the func. */
254 if (size == 6 && DT_ISJ32(*(uint16_t *)&text[i]) &&
256 (uintptr_t)(i + size + *(int32_t *)&text[i + 2]))
259 /* 8-bit disp. conditional jmp outside of the func. */
260 if (size == 2 && DT_ISJ8(text[i]) && symp->st_size <=
261 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
266 dt_dprintf("return at offset %lx\n", i);
267 ftp->ftps_offs[ftp->ftps_noffs++] = i;
272 if (ftp->ftps_noffs > 0) {
273 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
274 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
276 return (dt_set_errno(dtp, errno));
280 return (ftp->ftps_noffs);
285 dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
286 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
288 ftp->ftps_type = DTFTP_OFFSETS;
289 ftp->ftps_pc = (uintptr_t)symp->st_value;
290 ftp->ftps_size = (size_t)symp->st_size;
293 if (strcmp("-", ftp->ftps_func) == 0) {
294 ftp->ftps_offs[0] = off;
300 pid_t pid = Pstatus(P)->pr_pid;
301 char dmodel = Pstatus(P)->pr_dmodel;
303 pid_t pid = proc_getpid(P);
304 char dmodel = proc_getmodel(P);
307 if ((text = malloc(symp->st_size)) == NULL) {
308 dt_dprintf("mr sparkle: malloc() failed\n");
309 return (DT_PROC_ERR);
312 if (Pread(P, text, symp->st_size, symp->st_value) !=
314 dt_dprintf("mr sparkle: Pread() failed\n");
316 return (DT_PROC_ERR);
320 * We can't instrument offsets in functions with jump tables
321 * as we might interpret a jump table offset as an
324 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
329 for (i = 0; i < symp->st_size; i += size) {
331 ftp->ftps_offs[0] = i;
336 * If we've passed the desired offset without a
337 * match, then the given offset must not lie on a
338 * instruction boundary.
342 return (DT_PROC_ALIGN);
345 size = dt_instr_size(&text[i], dtp, pid,
346 symp->st_value + i, dmodel);
349 * If we hit an invalid instruction, bail as if we
350 * couldn't find the offset.
354 return (DT_PROC_ALIGN);
361 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
362 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
364 return (dt_set_errno(dtp, errno));
367 return (ftp->ftps_noffs);
372 dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
373 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
377 ulong_t i, end = symp->st_size;
379 pid_t pid = Pstatus(P)->pr_pid;
380 char dmodel = Pstatus(P)->pr_dmodel;
382 pid_t pid = proc_getpid(P);
383 char dmodel = proc_getmodel(P);
386 ftp->ftps_type = DTFTP_OFFSETS;
387 ftp->ftps_pc = (uintptr_t)symp->st_value;
388 ftp->ftps_size = (size_t)symp->st_size;
391 if ((text = malloc(symp->st_size)) == NULL) {
392 dt_dprintf("mr sparkle: malloc() failed\n");
393 return (DT_PROC_ERR);
396 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
397 dt_dprintf("mr sparkle: Pread() failed\n");
399 return (DT_PROC_ERR);
403 * We can't instrument offsets in functions with jump tables as
404 * we might interpret a jump table offset as an instruction.
406 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
411 if (strcmp("*", pattern) == 0) {
412 for (i = 0; i < end; i += size) {
413 ftp->ftps_offs[ftp->ftps_noffs++] = i;
415 size = dt_instr_size(&text[i], dtp, pid,
416 symp->st_value + i, dmodel);
418 /* bail if we hit an invalid opcode */
423 char name[sizeof (i) * 2 + 1];
425 for (i = 0; i < end; i += size) {
426 (void) snprintf(name, sizeof (name), "%lx", i);
427 if (gmatch(name, pattern))
428 ftp->ftps_offs[ftp->ftps_noffs++] = i;
430 size = dt_instr_size(&text[i], dtp, pid,
431 symp->st_value + i, dmodel);
433 /* bail if we hit an invalid opcode */
440 if (ftp->ftps_noffs > 0) {
441 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
442 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
444 return (dt_set_errno(dtp, errno));
448 return (ftp->ftps_noffs);
451 typedef struct dtrace_dis {
459 dt_getbyte(void *data)
461 dtrace_dis_t *dis = data;
462 int ret = *dis->instr;
464 if (ret == FASTTRAP_INSTR) {
465 fasttrap_instr_query_t instr;
467 instr.ftiq_pid = dis->pid;
468 instr.ftiq_pc = dis->addr;
471 * If we hit a byte that looks like the fasttrap provider's
472 * trap instruction (which doubles as the breakpoint
473 * instruction for debuggers) we need to query the kernel
474 * for the real value. This may just be part of an immediate
475 * value so there's no need to return an error if the
476 * kernel doesn't know about this address.
478 if (ioctl(dis->dtp->dt_ftfd, FASTTRAPIOC_GETINSTR, &instr) == 0)
479 ret = instr.ftiq_instr;
489 dt_instr_size(uchar_t *instr, dtrace_hdl_t *dtp, pid_t pid, uintptr_t addr,
501 x86dis.d86_data = &data;
502 x86dis.d86_get_byte = dt_getbyte;
503 x86dis.d86_check_func = NULL;
505 cpu_mode = (dmodel == PR_MODEL_ILP32) ? SIZE32 : SIZE64;
507 if (dtrace_disx86(&x86dis, cpu_mode) != 0)
511 * If the instruction was a single-byte breakpoint, there may be
512 * another debugger attached to this process. The original instruction
513 * can't be recovered so this must fail.
515 if (x86dis.d86_len == 1 &&
516 (uchar_t)x86dis.d86_bytes[0] == FASTTRAP_INSTR)
519 return (x86dis.d86_len);