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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
31 #include <sys/modctl.h>
32 #include <sys/sunddi.h>
33 #include <sys/dtrace.h>
37 #include <vm/seg_kmem.h>
38 #include <sys/stack.h>
39 #include <sys/frame.h>
40 #include <sys/dtrace_impl.h>
41 #include <sys/cmn_err.h>
42 #include <sys/sysmacros.h>
43 #include <sys/privregs.h>
44 #include <sys/sdt_impl.h>
46 #define SDT_PATCHVAL 0xf0
47 #define SDT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & sdt_probetab_mask)
48 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
50 static dev_info_t
*sdt_devi
;
51 static int sdt_verbose
= 0;
52 static sdt_probe_t
**sdt_probetab
;
53 static int sdt_probetab_size
;
54 static int sdt_probetab_mask
;
58 sdt_invop(uintptr_t addr
, uintptr_t *stack
, uintptr_t eax
)
60 uintptr_t stack0
, stack1
, stack2
, stack3
, stack4
;
62 sdt_probe_t
*sdt
= sdt_probetab
[SDT_ADDR2NDX(addr
)];
66 * On amd64, stack[0] contains the dereferenced stack pointer,
67 * stack[1] contains savfp, stack[2] contains savpc. We want
68 * to step over these entries.
73 for (; sdt
!= NULL
; sdt
= sdt
->sdp_hashnext
) {
74 if ((uintptr_t)sdt
->sdp_patchpoint
== addr
) {
76 * When accessing the arguments on the stack, we must
77 * protect against accessing beyond the stack. We can
78 * safely set NOFAULT here -- we know that interrupts
79 * are already disabled.
81 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
87 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
|
90 dtrace_probe(sdt
->sdp_id
, stack0
, stack1
,
91 stack2
, stack3
, stack4
);
93 return (DTRACE_INVOP_NOP
);
102 sdt_provide_module(void *arg
, struct modctl
*ctl
)
104 struct module
*mp
= ctl
->mod_mp
;
105 char *modname
= ctl
->mod_modname
;
106 sdt_probedesc_t
*sdpd
;
107 sdt_probe_t
*sdp
, *old
;
108 sdt_provider_t
*prov
;
112 * One for all, and all for one: if we haven't yet registered all of
113 * our providers, we'll refuse to provide anything.
115 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
116 if (prov
->sdtp_id
== DTRACE_PROVNONE
)
120 if (mp
->sdt_nprobes
!= 0 || (sdpd
= mp
->sdt_probes
) == NULL
)
123 for (sdpd
= mp
->sdt_probes
; sdpd
!= NULL
; sdpd
= sdpd
->sdpd_next
) {
124 char *name
= sdpd
->sdpd_name
, *func
, *nname
;
126 sdt_provider_t
*prov
;
130 for (prov
= sdt_providers
; prov
->sdtp_prefix
!= NULL
; prov
++) {
131 char *prefix
= prov
->sdtp_prefix
;
133 if (strncmp(name
, prefix
, strlen(prefix
)) == 0) {
134 name
+= strlen(prefix
);
139 nname
= kmem_alloc(len
= strlen(name
) + 1, KM_SLEEP
);
141 for (i
= 0, j
= 0; name
[j
] != '\0'; i
++) {
142 if (name
[j
] == '_' && name
[j
+ 1] == '_') {
146 nname
[i
] = name
[j
++];
152 sdp
= kmem_zalloc(sizeof (sdt_probe_t
), KM_SLEEP
);
153 sdp
->sdp_loadcnt
= ctl
->mod_loadcnt
;
155 sdp
->sdp_name
= nname
;
156 sdp
->sdp_namelen
= len
;
157 sdp
->sdp_provider
= prov
;
159 func
= kobj_searchsym(mp
, sdpd
->sdpd_offset
, &offs
);
165 * We have our provider. Now create the probe.
167 if ((id
= dtrace_probe_lookup(prov
->sdtp_id
, modname
,
168 func
, nname
)) != DTRACE_IDNONE
) {
169 old
= dtrace_probe_arg(prov
->sdtp_id
, id
);
172 sdp
->sdp_next
= old
->sdp_next
;
176 sdp
->sdp_id
= dtrace_probe_create(prov
->sdtp_id
,
177 modname
, func
, nname
, 3, sdp
);
183 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)];
184 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)] = sdp
;
186 sdp
->sdp_patchval
= SDT_PATCHVAL
;
187 sdp
->sdp_patchpoint
= (uint8_t *)sdpd
->sdpd_offset
;
188 sdp
->sdp_savedval
= *sdp
->sdp_patchpoint
;
194 sdt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
196 sdt_probe_t
*sdp
= parg
, *old
, *last
, *hash
;
197 struct modctl
*ctl
= sdp
->sdp_ctl
;
200 if (ctl
!= NULL
&& ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
) {
201 if ((ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
&&
203 ((struct module
*)(ctl
->mod_mp
))->sdt_nprobes
--;
207 while (sdp
!= NULL
) {
211 * Now we need to remove this probe from the sdt_probetab.
213 ndx
= SDT_ADDR2NDX(sdp
->sdp_patchpoint
);
215 hash
= sdt_probetab
[ndx
];
217 while (hash
!= sdp
) {
218 ASSERT(hash
!= NULL
);
220 hash
= hash
->sdp_hashnext
;
224 last
->sdp_hashnext
= sdp
->sdp_hashnext
;
226 sdt_probetab
[ndx
] = sdp
->sdp_hashnext
;
229 kmem_free(sdp
->sdp_name
, sdp
->sdp_namelen
);
231 kmem_free(old
, sizeof (sdt_probe_t
));
237 sdt_enable(void *arg
, dtrace_id_t id
, void *parg
)
239 sdt_probe_t
*sdp
= parg
;
240 struct modctl
*ctl
= sdp
->sdp_ctl
;
245 * If this module has disappeared since we discovered its probes,
246 * refuse to enable it.
248 if (!ctl
->mod_loaded
) {
250 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
251 "(module %s unloaded)",
252 sdp
->sdp_name
, ctl
->mod_modname
);
258 * Now check that our modctl has the expected load count. If it
259 * doesn't, this module must have been unloaded and reloaded -- and
260 * we're not going to touch it.
262 if (ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
264 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
265 "(module %s reloaded)",
266 sdp
->sdp_name
, ctl
->mod_modname
);
271 while (sdp
!= NULL
) {
272 *sdp
->sdp_patchpoint
= sdp
->sdp_patchval
;
281 sdt_disable(void *arg
, dtrace_id_t id
, void *parg
)
283 sdt_probe_t
*sdp
= parg
;
284 struct modctl
*ctl
= sdp
->sdp_ctl
;
288 if (!ctl
->mod_loaded
|| ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
)
291 while (sdp
!= NULL
) {
292 *sdp
->sdp_patchpoint
= sdp
->sdp_savedval
;
302 sdt_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
305 struct frame
*fp
= (struct frame
*)dtrace_getfp();
310 * A total of 6 arguments are passed via registers; any argument with
311 * index of 5 or lower is therefore in a register.
316 for (i
= 1; i
<= aframes
; i
++) {
317 fp
= (struct frame
*)(fp
->fr_savfp
);
319 if (fp
->fr_savpc
== (pc_t
)dtrace_invop_callsite
) {
320 #if !defined(__amd64)
322 * If we pass through the invalid op handler, we will
323 * use the pointer that it passed to the stack as the
324 * second argument to dtrace_invop() as the pointer to
327 stack
= ((uintptr_t **)&fp
[1])[1];
330 * In the case of amd64, we will use the pointer to the
331 * regs structure that was pushed when we took the
332 * trap. To get this structure, we must increment
333 * beyond the frame structure, the calling RIP, and
334 * padding stored in dtrace_invop(). If the argument
335 * that we're seeking is passed on the stack, we'll
336 * pull the true stack pointer out of the saved
337 * registers and decrement our argument by the number
338 * of arguments passed in registers; if the argument
339 * we're seeking is passed in regsiters, we can just
342 struct regs
*rp
= (struct regs
*)((uintptr_t)&fp
[1] +
343 sizeof (uintptr_t) * 2);
345 if (argno
<= inreg
) {
346 stack
= (uintptr_t *)&rp
->r_rdi
;
348 stack
= (uintptr_t *)(rp
->r_rsp
);
349 argno
-= (inreg
+ 1);
357 * We know that we did not come through a trap to get into
358 * dtrace_probe() -- the provider simply called dtrace_probe()
359 * directly. As this is the case, we need to shift the argument
360 * that we're looking for: the probe ID is the first argument to
361 * dtrace_probe(), so the argument n will actually be found where
362 * one would expect to find argument (n + 1).
367 if (argno
<= inreg
) {
369 * This shouldn't happen. If the argument is passed in a
370 * register then it should have been, well, passed in a
373 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
377 argno
-= (inreg
+ 1);
379 stack
= (uintptr_t *)&fp
[1];
382 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
384 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
389 static dtrace_pops_t sdt_pops
= {
404 sdt_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
406 sdt_provider_t
*prov
;
408 if (ddi_create_minor_node(devi
, "sdt", S_IFCHR
,
409 0, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
410 cmn_err(CE_NOTE
, "/dev/sdt couldn't create minor node");
411 ddi_remove_minor_node(devi
, NULL
);
412 return (DDI_FAILURE
);
415 ddi_report_dev(devi
);
418 if (sdt_probetab_size
== 0)
419 sdt_probetab_size
= SDT_PROBETAB_SIZE
;
421 sdt_probetab_mask
= sdt_probetab_size
- 1;
423 kmem_zalloc(sdt_probetab_size
* sizeof (sdt_probe_t
*), KM_SLEEP
);
424 dtrace_invop_add(sdt_invop
);
426 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
429 if (prov
->sdtp_priv
== DTRACE_PRIV_NONE
) {
430 priv
= DTRACE_PRIV_KERNEL
;
431 sdt_pops
.dtps_mode
= NULL
;
433 priv
= prov
->sdtp_priv
;
434 ASSERT(priv
== DTRACE_PRIV_USER
);
435 sdt_pops
.dtps_mode
= sdt_mode
;
438 if (dtrace_register(prov
->sdtp_name
, prov
->sdtp_attr
,
439 priv
, NULL
, &sdt_pops
, prov
, &prov
->sdtp_id
) != 0) {
440 cmn_err(CE_WARN
, "failed to register sdt provider %s",
445 return (DDI_SUCCESS
);
450 sdt_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
452 sdt_provider_t
*prov
;
459 return (DDI_SUCCESS
);
462 return (DDI_FAILURE
);
465 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
466 if (prov
->sdtp_id
!= DTRACE_PROVNONE
) {
467 if (dtrace_unregister(prov
->sdtp_id
) != 0)
468 return (DDI_FAILURE
);
470 prov
->sdtp_id
= DTRACE_PROVNONE
;
474 dtrace_invop_remove(sdt_invop
);
475 kmem_free(sdt_probetab
, sdt_probetab_size
* sizeof (sdt_probe_t
*));
477 return (DDI_SUCCESS
);
482 sdt_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
487 case DDI_INFO_DEVT2DEVINFO
:
488 *result
= (void *)sdt_devi
;
491 case DDI_INFO_DEVT2INSTANCE
:
503 sdt_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cred_p
)
508 static struct cb_ops sdt_cb_ops
= {
511 nulldev
, /* strategy */
521 ddi_prop_op
, /* cb_prop_op */
523 D_NEW
| D_MP
/* Driver compatibility flag */
526 static struct dev_ops sdt_ops
= {
527 DEVO_REV
, /* devo_rev, */
529 sdt_info
, /* get_dev_info */
530 nulldev
, /* identify */
532 sdt_attach
, /* attach */
533 sdt_detach
, /* detach */
535 &sdt_cb_ops
, /* driver operations */
536 NULL
, /* bus operations */
537 nodev
, /* dev power */
538 ddi_quiesce_not_needed
, /* quiesce */
542 * Module linkage information for the kernel.
544 static struct modldrv modldrv
= {
545 &mod_driverops
, /* module type (this is a pseudo driver) */
546 "Statically Defined Tracing", /* name of module */
547 &sdt_ops
, /* driver ops */
550 static struct modlinkage modlinkage
= {
559 return (mod_install(&modlinkage
));
563 _info(struct modinfo
*modinfop
)
565 return (mod_info(&modlinkage
, modinfop
));
571 return (mod_remove(&modlinkage
));