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.
36 #include <dis_tables.h>
39 #define PR_MODEL_ILP32 1
40 #define PR_MODEL_LP64 2
41 #include <libproc_compat.h>
44 #define DT_POPL_EBP 0x5d
52 #define DT_MOVL_EBP_ESP 0xe58b
54 #define DT_ISJ32(op16) (((op16) & 0xfff0) == 0x0f80)
55 #define DT_ISJ8(op8) (((op8) & 0xf0) == 0x70)
57 #define DT_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
59 static int dt_instr_size(uchar_t *, dtrace_hdl_t *, pid_t, uintptr_t, char);
63 dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
64 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
66 ftp->ftps_type = DTFTP_ENTRY;
67 ftp->ftps_pc = (uintptr_t)symp->st_value;
68 ftp->ftps_size = (size_t)symp->st_size;
70 ftp->ftps_offs[0] = 0;
72 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
73 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
75 return (dt_set_errno(dtp, errno));
82 dt_pid_has_jump_table(struct ps_prochandle *P, dtrace_hdl_t *dtp,
83 uint8_t *text, fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
88 pid_t pid = Pstatus(P)->pr_pid;
89 char dmodel = Pstatus(P)->pr_dmodel;
91 pid_t pid = proc_getpid(P);
93 char dmodel = PR_MODEL_ILP32;
95 char dmodel = PR_MODEL_LP64;
100 * Take a pass through the function looking for a register-dependant
101 * jmp instruction. This could be a jump table so we have to be
102 * ultra conservative.
104 for (i = 0; i < ftp->ftps_size; i += size) {
105 size = dt_instr_size(&text[i], dtp, pid, symp->st_value + i,
109 * Assume the worst if we hit an illegal instruction.
112 dt_dprintf("error at %#lx (assuming jump table)\n", i);
118 * Register-dependant jmp instructions start with a 0xff byte
119 * and have the modrm.reg field set to 4. They can have an
120 * optional REX prefix on the 64-bit ISA.
122 if ((text[i] == 0xff && DT_MODRM_REG(text[i + 1]) == 4) ||
123 (dmodel == PR_MODEL_LP64 && (text[i] & 0xf0) == 0x40 &&
124 text[i + 1] == 0xff && DT_MODRM_REG(text[i + 2]) == 4)) {
125 dt_dprintf("found a suspected jump table at %s:%lx\n",
137 dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
138 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
144 pid_t pid = Pstatus(P)->pr_pid;
145 char dmodel = Pstatus(P)->pr_dmodel;
147 pid_t pid = proc_getpid(P);
149 char dmodel = PR_MODEL_ILP32;
151 char dmodel = PR_MODEL_LP64;
156 * We allocate a few extra bytes at the end so we don't have to check
157 * for overrunning the buffer.
159 if ((text = calloc(1, symp->st_size + 4)) == NULL) {
160 dt_dprintf("mr sparkle: malloc() failed\n");
161 return (DT_PROC_ERR);
164 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
165 dt_dprintf("mr sparkle: Pread() failed\n");
167 return (DT_PROC_ERR);
170 ftp->ftps_type = DTFTP_RETURN;
171 ftp->ftps_pc = (uintptr_t)symp->st_value;
172 ftp->ftps_size = (size_t)symp->st_size;
176 * If there's a jump table in the function we're only willing to
177 * instrument these specific (and equivalent) instruction sequences:
185 * We do this to avoid accidentally interpreting jump table
186 * offsets as actual instructions.
188 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
189 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
190 size = dt_instr_size(&text[i], dtp, pid,
191 symp->st_value + i, dmodel);
193 /* bail if we hit an invalid opcode */
197 if (text[i] == DT_LEAVE && text[i + 1] == DT_RET) {
198 dt_dprintf("leave/ret at %lx\n", i + 1);
199 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
201 } else if (text[i] == DT_LEAVE &&
202 text[i + 1] == DT_REP && text[i + 2] == DT_RET) {
203 dt_dprintf("leave/rep ret at %lx\n", i + 1);
204 ftp->ftps_offs[ftp->ftps_noffs++] = i + 1;
206 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
207 text[i + 2] == DT_POPL_EBP &&
208 text[i + 3] == DT_RET) {
209 dt_dprintf("movl/popl/ret at %lx\n", i + 3);
210 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
212 } else if (*(uint16_t *)&text[i] == DT_MOVL_EBP_ESP &&
213 text[i + 2] == DT_POPL_EBP &&
214 text[i + 3] == DT_REP &&
215 text[i + 4] == DT_RET) {
216 dt_dprintf("movl/popl/rep ret at %lx\n", i + 3);
217 ftp->ftps_offs[ftp->ftps_noffs++] = i + 3;
222 for (i = 0, end = ftp->ftps_size; i < end; i += size) {
223 size = dt_instr_size(&text[i], dtp, pid,
224 symp->st_value + i, dmodel);
226 /* bail if we hit an invalid opcode */
231 if (size == 1 && text[i] == DT_RET)
235 if (size == 2 && text[i] == DT_REP &&
236 text[i + 1] == DT_RET)
240 if (size == 3 && text[i] == DT_RET16)
243 /* two-byte ret <imm16> */
244 if (size == 4 && text[i] == DT_REP &&
245 text[i + 1] == DT_RET16)
248 /* 32-bit displacement jmp outside of the function */
249 if (size == 5 && text[i] == DT_JMP32 && symp->st_size <=
250 (uintptr_t)(i + size + *(int32_t *)&text[i + 1]))
253 /* 8-bit displacement jmp outside of the function */
254 if (size == 2 && text[i] == DT_JMP8 && symp->st_size <=
255 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
258 /* 32-bit disp. conditional jmp outside of the func. */
259 if (size == 6 && DT_ISJ32(*(uint16_t *)&text[i]) &&
261 (uintptr_t)(i + size + *(int32_t *)&text[i + 2]))
264 /* 8-bit disp. conditional jmp outside of the func. */
265 if (size == 2 && DT_ISJ8(text[i]) && symp->st_size <=
266 (uintptr_t)(i + size + *(int8_t *)&text[i + 1]))
271 dt_dprintf("return at offset %lx\n", i);
272 ftp->ftps_offs[ftp->ftps_noffs++] = i;
277 if (ftp->ftps_noffs > 0) {
278 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
279 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
281 return (dt_set_errno(dtp, errno));
285 return (ftp->ftps_noffs);
290 dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
291 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
293 ftp->ftps_type = DTFTP_OFFSETS;
294 ftp->ftps_pc = (uintptr_t)symp->st_value;
295 ftp->ftps_size = (size_t)symp->st_size;
298 if (strcmp("-", ftp->ftps_func) == 0) {
299 ftp->ftps_offs[0] = off;
305 pid_t pid = Pstatus(P)->pr_pid;
306 char dmodel = Pstatus(P)->pr_dmodel;
308 pid_t pid = proc_getpid(P);
310 char dmodel = PR_MODEL_ILP32;
312 char dmodel = PR_MODEL_LP64;
316 if ((text = malloc(symp->st_size)) == NULL) {
317 dt_dprintf("mr sparkle: malloc() failed\n");
318 return (DT_PROC_ERR);
321 if (Pread(P, text, symp->st_size, symp->st_value) !=
323 dt_dprintf("mr sparkle: Pread() failed\n");
325 return (DT_PROC_ERR);
329 * We can't instrument offsets in functions with jump tables
330 * as we might interpret a jump table offset as an
333 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
338 for (i = 0; i < symp->st_size; i += size) {
340 ftp->ftps_offs[0] = i;
345 * If we've passed the desired offset without a
346 * match, then the given offset must not lie on a
347 * instruction boundary.
351 return (DT_PROC_ALIGN);
354 size = dt_instr_size(&text[i], dtp, pid,
355 symp->st_value + i, dmodel);
358 * If we hit an invalid instruction, bail as if we
359 * couldn't find the offset.
363 return (DT_PROC_ALIGN);
370 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
371 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
373 return (dt_set_errno(dtp, errno));
376 return (ftp->ftps_noffs);
381 dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
382 fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
386 ulong_t i, end = symp->st_size;
388 pid_t pid = Pstatus(P)->pr_pid;
389 char dmodel = Pstatus(P)->pr_dmodel;
391 pid_t pid = proc_getpid(P);
393 char dmodel = PR_MODEL_ILP32;
395 char dmodel = PR_MODEL_LP64;
399 ftp->ftps_type = DTFTP_OFFSETS;
400 ftp->ftps_pc = (uintptr_t)symp->st_value;
401 ftp->ftps_size = (size_t)symp->st_size;
404 if ((text = malloc(symp->st_size)) == NULL) {
405 dt_dprintf("mr sparkle: malloc() failed\n");
406 return (DT_PROC_ERR);
409 if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
410 dt_dprintf("mr sparkle: Pread() failed\n");
412 return (DT_PROC_ERR);
416 * We can't instrument offsets in functions with jump tables as
417 * we might interpret a jump table offset as an instruction.
419 if (dt_pid_has_jump_table(P, dtp, text, ftp, symp)) {
424 if (strcmp("*", pattern) == 0) {
425 for (i = 0; i < end; i += size) {
426 ftp->ftps_offs[ftp->ftps_noffs++] = i;
428 size = dt_instr_size(&text[i], dtp, pid,
429 symp->st_value + i, dmodel);
431 /* bail if we hit an invalid opcode */
436 char name[sizeof (i) * 2 + 1];
438 for (i = 0; i < end; i += size) {
439 (void) snprintf(name, sizeof (name), "%lx", i);
440 if (gmatch(name, pattern))
441 ftp->ftps_offs[ftp->ftps_noffs++] = i;
443 size = dt_instr_size(&text[i], dtp, pid,
444 symp->st_value + i, dmodel);
446 /* bail if we hit an invalid opcode */
453 if (ftp->ftps_noffs > 0) {
454 if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
455 dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
457 return (dt_set_errno(dtp, errno));
461 return (ftp->ftps_noffs);
464 typedef struct dtrace_dis {
472 dt_getbyte(void *data)
474 dtrace_dis_t *dis = data;
475 int ret = *dis->instr;
477 if (ret == FASTTRAP_INSTR) {
478 fasttrap_instr_query_t instr;
480 instr.ftiq_pid = dis->pid;
481 instr.ftiq_pc = dis->addr;
484 * If we hit a byte that looks like the fasttrap provider's
485 * trap instruction (which doubles as the breakpoint
486 * instruction for debuggers) we need to query the kernel
487 * for the real value. This may just be part of an immediate
488 * value so there's no need to return an error if the
489 * kernel doesn't know about this address.
491 if (ioctl(dis->dtp->dt_ftfd, FASTTRAPIOC_GETINSTR, &instr) == 0)
492 ret = instr.ftiq_instr;
502 dt_instr_size(uchar_t *instr, dtrace_hdl_t *dtp, pid_t pid, uintptr_t addr,
514 x86dis.d86_data = &data;
515 x86dis.d86_get_byte = dt_getbyte;
516 x86dis.d86_check_func = NULL;
518 cpu_mode = (dmodel == PR_MODEL_ILP32) ? SIZE32 : SIZE64;
520 if (dtrace_disx86(&x86dis, cpu_mode) != 0)
524 * If the instruction was a single-byte breakpoint, there may be
525 * another debugger attached to this process. The original instruction
526 * can't be recovered so this must fail.
528 if (x86dis.d86_len == 1 && instr[0] == FASTTRAP_INSTR)
531 return (x86dis.d86_len);