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>
43 #define PR_MODEL_ILP32 1
44 #define PR_MODEL_LP64 2
45 #include <libproc_compat.h>
48 #define DT_POPL_EBP 0x5d
56 #define DT_MOVL_EBP_ESP 0xe58b
58 #define DT_ISJ32(op16) (((op16) & 0xfff0) == 0x0f80)
59 #define DT_ISJ8(op8) (((op8) & 0xf0) == 0x70)
61 #define DT_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
63 static int dt_instr_size(uchar_t *, dtrace_hdl_t *, pid_t, uintptr_t, char);
67 dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
68 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
70 ftp->ftps_type = DTFTP_ENTRY;
71 ftp->ftps_pc = (uintptr_t)symp->st_value;
72 ftp->ftps_size = (size_t)symp->st_size;
74 ftp->ftps_offs[0] = 0;
76 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
77 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
79 return (dt_set_errno(dtp, errno));
86 dt_pid_has_jump_table(struct ps_prochandle *P, dtrace_hdl_t *dtp,
87 uint8_t *text, fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
92 pid_t pid = Pstatus(P)->pr_pid;
93 char dmodel = Pstatus(P)->pr_dmodel;
95 pid_t pid = proc_getpid(P);
97 char dmodel = PR_MODEL_ILP32;
99 char dmodel = PR_MODEL_LP64;
104 * Take a pass through the function looking for a register-dependant
105 * jmp instruction. This could be a jump table so we have to be
106 * ultra conservative.
108 for (i = 0; i < ftp->ftps_size; i += size) {
109 size = dt_instr_size(&text[i], dtp, pid, symp->st_value + i,
113 * Assume the worst if we hit an illegal instruction.
116 dt_dprintf("error at %#lx (assuming jump table)\n", i);
122 * Register-dependant jmp instructions start with a 0xff byte
123 * and have the modrm.reg field set to 4. They can have an
124 * optional REX prefix on the 64-bit ISA.
126 if ((text[i] == 0xff && DT_MODRM_REG(text[i + 1]) == 4) ||
127 (dmodel == PR_MODEL_LP64 && (text[i] & 0xf0) == 0x40 &&
128 text[i + 1] == 0xff && DT_MODRM_REG(text[i + 2]) == 4)) {
129 dt_dprintf("found a suspected jump table at %s:%lx\n",
141 dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
142 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
148 pid_t pid = Pstatus(P)->pr_pid;
149 char dmodel = Pstatus(P)->pr_dmodel;
151 pid_t pid = proc_getpid(P);
153 char dmodel = PR_MODEL_ILP32;
155 char dmodel = PR_MODEL_LP64;
160 * We allocate a few extra bytes at the end so we don't have to check
161 * for overrunning the buffer.
163 if ((text = calloc(1, symp->st_size + 4)) == NULL) {
164 dt_dprintf("mr sparkle: malloc() failed\n");
165 return (DT_PROC_ERR);
168 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
169 dt_dprintf("mr sparkle: Pread() failed\n");
171 return (DT_PROC_ERR);
174 ftp->ftps_type = DTFTP_RETURN;
175 ftp->ftps_pc = (uintptr_t)symp->st_value;
176 ftp->ftps_size = (size_t)symp->st_size;
180 * If there's a jump table in the function we're only willing to
181 * instrument these specific (and equivalent) instruction sequences:
189 * We do this to avoid accidentally interpreting jump table
190 * offsets as actual instructions.
192 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
193 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
194 size = dt_instr_size(&text[i], dtp, pid,
195 symp->st_value + i, dmodel);
197 /* bail if we hit an invalid opcode */
201 if (text[i] == DT_LEAVE && text[i + 1] == DT_RET) {
202 dt_dprintf("leave/ret at %lx\n", i + 1);
203 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
205 } else if (text[i] == DT_LEAVE &&
206 text[i + 1] == DT_REP && text[i + 2] == DT_RET) {
207 dt_dprintf("leave/rep ret at %lx\n", i + 1);
208 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
210 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
211 text[i + 2] == DT_POPL_EBP &&
212 text[i + 3] == DT_RET) {
213 dt_dprintf("movl/popl/ret at %lx\n", i + 3);
214 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
216 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
217 text[i + 2] == DT_POPL_EBP &&
218 text[i + 3] == DT_REP &&
219 text[i + 4] == DT_RET) {
220 dt_dprintf("movl/popl/rep ret at %lx\n", i + 3);
221 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
226 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
227 size = dt_instr_size(&text[i], dtp, pid,
228 symp->st_value + i, dmodel);
230 /* bail if we hit an invalid opcode */
235 if (size == 1 && text[i] == DT_RET)
239 if (size == 2 && text[i] == DT_REP &&
240 text[i + 1] == DT_RET)
244 if (size == 3 && text[i] == DT_RET16)
247 /* two-byte ret <imm16> */
248 if (size == 4 && text[i] == DT_REP &&
249 text[i + 1] == DT_RET16)
252 /* 32-bit displacement jmp outside of the function */
253 if (size == 5 && text[i] == DT_JMP32 && symp->st_size <=
254 (uintptr_t)(i + size + *(int32_t *)&text[i + 1]))
257 /* 8-bit displacement jmp outside of the function */
258 if (size == 2 && text[i] == DT_JMP8 && symp->st_size <=
259 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
262 /* 32-bit disp. conditional jmp outside of the func. */
263 if (size == 6 && DT_ISJ32(*(uint16_t *)&text[i]) &&
265 (uintptr_t)(i + size + *(int32_t *)&text[i + 2]))
268 /* 8-bit disp. conditional jmp outside of the func. */
269 if (size == 2 && DT_ISJ8(text[i]) && symp->st_size <=
270 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
275 dt_dprintf("return at offset %lx\n", i);
276 ftp->ftps_offs[ftp->ftps_noffs++] = i;
281 if (ftp->ftps_noffs > 0) {
282 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
283 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
285 return (dt_set_errno(dtp, errno));
289 return (ftp->ftps_noffs);
294 dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
295 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
297 ftp->ftps_type = DTFTP_OFFSETS;
298 ftp->ftps_pc = (uintptr_t)symp->st_value;
299 ftp->ftps_size = (size_t)symp->st_size;
302 if (strcmp("-", ftp->ftps_func) == 0) {
303 ftp->ftps_offs[0] = off;
309 pid_t pid = Pstatus(P)->pr_pid;
310 char dmodel = Pstatus(P)->pr_dmodel;
312 pid_t pid = proc_getpid(P);
314 char dmodel = PR_MODEL_ILP32;
316 char dmodel = PR_MODEL_LP64;
320 if ((text = malloc(symp->st_size)) == NULL) {
321 dt_dprintf("mr sparkle: malloc() failed\n");
322 return (DT_PROC_ERR);
325 if (Pread(P, text, symp->st_size, symp->st_value) !=
327 dt_dprintf("mr sparkle: Pread() failed\n");
329 return (DT_PROC_ERR);
333 * We can't instrument offsets in functions with jump tables
334 * as we might interpret a jump table offset as an
337 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
342 for (i = 0; i < symp->st_size; i += size) {
344 ftp->ftps_offs[0] = i;
349 * If we've passed the desired offset without a
350 * match, then the given offset must not lie on a
351 * instruction boundary.
355 return (DT_PROC_ALIGN);
358 size = dt_instr_size(&text[i], dtp, pid,
359 symp->st_value + i, dmodel);
362 * If we hit an invalid instruction, bail as if we
363 * couldn't find the offset.
367 return (DT_PROC_ALIGN);
374 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
375 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
377 return (dt_set_errno(dtp, errno));
380 return (ftp->ftps_noffs);
385 dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
386 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
390 ulong_t i, end = symp->st_size;
392 pid_t pid = Pstatus(P)->pr_pid;
393 char dmodel = Pstatus(P)->pr_dmodel;
395 pid_t pid = proc_getpid(P);
397 char dmodel = PR_MODEL_ILP32;
399 char dmodel = PR_MODEL_LP64;
403 ftp->ftps_type = DTFTP_OFFSETS;
404 ftp->ftps_pc = (uintptr_t)symp->st_value;
405 ftp->ftps_size = (size_t)symp->st_size;
408 if ((text = malloc(symp->st_size)) == NULL) {
409 dt_dprintf("mr sparkle: malloc() failed\n");
410 return (DT_PROC_ERR);
413 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
414 dt_dprintf("mr sparkle: Pread() failed\n");
416 return (DT_PROC_ERR);
420 * We can't instrument offsets in functions with jump tables as
421 * we might interpret a jump table offset as an instruction.
423 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
428 if (strcmp("*", pattern) == 0) {
429 for (i = 0; i < end; i += size) {
430 ftp->ftps_offs[ftp->ftps_noffs++] = i;
432 size = dt_instr_size(&text[i], dtp, pid,
433 symp->st_value + i, dmodel);
435 /* bail if we hit an invalid opcode */
440 char name[sizeof (i) * 2 + 1];
442 for (i = 0; i < end; i += size) {
443 (void) snprintf(name, sizeof (name), "%lx", i);
444 if (gmatch(name, pattern))
445 ftp->ftps_offs[ftp->ftps_noffs++] = i;
447 size = dt_instr_size(&text[i], dtp, pid,
448 symp->st_value + i, dmodel);
450 /* bail if we hit an invalid opcode */
457 if (ftp->ftps_noffs > 0) {
458 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
459 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
461 return (dt_set_errno(dtp, errno));
465 return (ftp->ftps_noffs);
468 typedef struct dtrace_dis {
476 dt_getbyte(void *data)
478 dtrace_dis_t *dis = data;
479 int ret = *dis->instr;
481 if (ret == FASTTRAP_INSTR) {
482 fasttrap_instr_query_t instr;
484 instr.ftiq_pid = dis->pid;
485 instr.ftiq_pc = dis->addr;
488 * If we hit a byte that looks like the fasttrap provider's
489 * trap instruction (which doubles as the breakpoint
490 * instruction for debuggers) we need to query the kernel
491 * for the real value. This may just be part of an immediate
492 * value so there's no need to return an error if the
493 * kernel doesn't know about this address.
495 if (ioctl(dis->dtp->dt_ftfd, FASTTRAPIOC_GETINSTR, &instr) == 0)
496 ret = instr.ftiq_instr;
506 dt_instr_size(uchar_t *instr, dtrace_hdl_t *dtp, pid_t pid, uintptr_t addr,
518 x86dis.d86_data = &data;
519 x86dis.d86_get_byte = dt_getbyte;
520 x86dis.d86_check_func = NULL;
522 cpu_mode = (dmodel == PR_MODEL_ILP32) ? SIZE32 : SIZE64;
524 if (dtrace_disx86(&x86dis, cpu_mode) != 0)
528 * If the instruction was a single-byte breakpoint, there may be
529 * another debugger attached to this process. The original instruction
530 * can't be recovered so this must fail.
532 if (x86dis.d86_len == 1 &&
533 (uchar_t)x86dis.d86_bytes[0] == FASTTRAP_INSTR)
536 return (x86dis.d86_len);