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.
26 /* #pragma ident "@(#)sdt.c 1.9 08/07/01 SMI" */
30 #define _KERNEL /* Solaris vs. Darwin */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
38 #include <sys/ioctl.h>
40 #include <sys/fcntl.h>
41 #include <miscfs/devfs/devfs.h>
44 #include <sys/dtrace.h>
45 #include <sys/dtrace_impl.h>
47 #include <sys/dtrace_glue.h>
49 #include <sys/sdt_impl.h>
50 extern int dtrace_kernel_symbol_mode
;
52 /* #include <machine/trap.h */
53 struct savearea_t
; /* Used anonymously */
55 #if defined(__x86_64__)
56 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, uintptr_t *, int);
57 extern perfCallback tempDTraceTrapHook
;
58 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, uintptr_t *, int);
59 #define SDT_PATCHVAL 0xf0
62 #error Unknown architecture
65 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
67 #define DTRACE_PROBE_PREFIX "_dtrace_probe$"
69 static dev_info_t
*sdt_devi
;
70 static int sdt_verbose
= 0;
71 sdt_probe_t
**sdt_probetab
;
72 int sdt_probetab_size
;
73 int sdt_probetab_mask
;
77 __sdt_provide_module(void *arg
, struct modctl
*ctl
)
80 struct module *mp
= (struct module *)ctl
->mod_address
;
81 char *modname
= ctl
->mod_modname
;
82 sdt_probedesc_t
*sdpd
;
83 sdt_probe_t
*sdp
, *old
;
88 * One for all, and all for one: if we haven't yet registered all of
89 * our providers, we'll refuse to provide anything.
91 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
92 if (prov
->sdtp_id
== DTRACE_PROVNONE
)
96 if (!mp
|| mp
->sdt_nprobes
!= 0 || (sdpd
= mp
->sdt_probes
) == NULL
)
99 for (sdpd
= mp
->sdt_probes
; sdpd
!= NULL
; sdpd
= sdpd
->sdpd_next
) {
100 const char *name
= sdpd
->sdpd_name
, *func
;
105 for (prov
= sdt_providers
; prov
->sdtp_prefix
!= NULL
; prov
++) {
106 const char *prefpart
, *prefix
= prov
->sdtp_prefix
;
108 if ((prefpart
= strstr(name
, prefix
))) {
109 name
= prefpart
+ strlen(prefix
);
114 nname
= kmem_alloc(len
= strlen(name
) + 1, KM_SLEEP
);
116 for (i
= 0, j
= 0; name
[j
] != '\0'; i
++) {
117 if (name
[j
] == '_' && name
[j
+ 1] == '_') {
121 nname
[i
] = name
[j
++];
127 sdp
= kmem_zalloc(sizeof (sdt_probe_t
), KM_SLEEP
);
128 sdp
->sdp_loadcnt
= ctl
->mod_loadcnt
;
130 sdp
->sdp_name
= nname
;
131 sdp
->sdp_namelen
= len
;
132 sdp
->sdp_provider
= prov
;
134 func
= sdpd
->sdpd_func
;
140 * We have our provider. Now create the probe.
142 if ((id
= dtrace_probe_lookup(prov
->sdtp_id
, modname
,
143 func
, nname
)) != DTRACE_IDNONE
) {
144 old
= dtrace_probe_arg(prov
->sdtp_id
, id
);
147 sdp
->sdp_next
= old
->sdp_next
;
151 sdp
->sdp_id
= dtrace_probe_create(prov
->sdtp_id
,
152 modname
, func
, nname
, SDT_AFRAMES
, sdp
);
158 printf ("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd
, sdp
, nname
, sdp
->sdp_id
);
162 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)];
163 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)] = sdp
;
165 sdp
->sdp_patchval
= SDT_PATCHVAL
;
166 sdp
->sdp_patchpoint
= (sdt_instr_t
*)sdpd
->sdpd_offset
;
167 sdp
->sdp_savedval
= *sdp
->sdp_patchpoint
;
173 sdt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
175 #pragma unused(arg,id)
176 sdt_probe_t
*sdp
= parg
, *old
, *last
, *hash
;
179 #if !defined(__APPLE__)
181 * APPLE NOTE: sdt probes for kexts not yet implemented
183 struct modctl
*ctl
= sdp
->sdp_ctl
;
185 if (ctl
!= NULL
&& ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
) {
186 if ((ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
&&
188 ((struct module *)(ctl
->mod_mp
))->sdt_nprobes
--;
191 #endif /* __APPLE__ */
193 while (sdp
!= NULL
) {
197 * Now we need to remove this probe from the sdt_probetab.
199 ndx
= SDT_ADDR2NDX(sdp
->sdp_patchpoint
);
201 hash
= sdt_probetab
[ndx
];
203 while (hash
!= sdp
) {
204 ASSERT(hash
!= NULL
);
206 hash
= hash
->sdp_hashnext
;
210 last
->sdp_hashnext
= sdp
->sdp_hashnext
;
212 sdt_probetab
[ndx
] = sdp
->sdp_hashnext
;
215 kmem_free(sdp
->sdp_name
, sdp
->sdp_namelen
);
217 kmem_free(old
, sizeof (sdt_probe_t
));
223 sdt_enable(void *arg
, dtrace_id_t id
, void *parg
)
225 #pragma unused(arg,id)
226 sdt_probe_t
*sdp
= parg
;
227 struct modctl
*ctl
= sdp
->sdp_ctl
;
232 * If this module has disappeared since we discovered its probes,
233 * refuse to enable it.
235 if (!ctl
->mod_loaded
) {
237 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
238 "(module %s unloaded)",
239 sdp
->sdp_name
, ctl
->mod_modname
);
245 * Now check that our modctl has the expected load count. If it
246 * doesn't, this module must have been unloaded and reloaded -- and
247 * we're not going to touch it.
249 if (ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
251 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
252 "(module %s reloaded)",
253 sdp
->sdp_name
, ctl
->mod_modname
);
258 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
259 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
261 cmn_err(CE_NOTE
, "sdt_enable is failing for probe %s "
262 "in module %s: tempDTraceTrapHook already occupied.",
263 sdp
->sdp_name
, ctl
->mod_modname
);
268 while (sdp
!= NULL
) {
269 (void)ml_nofault_copy( (vm_offset_t
)&sdp
->sdp_patchval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
270 (vm_size_t
)sizeof(sdp
->sdp_patchval
));
273 * Make the patched instruction visible via a data + instruction
274 * cache fush on platforms that need it
276 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
277 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
288 sdt_disable(void *arg
, dtrace_id_t id
, void *parg
)
290 #pragma unused(arg,id)
291 sdt_probe_t
*sdp
= parg
;
292 struct modctl
*ctl
= sdp
->sdp_ctl
;
296 if (!ctl
->mod_loaded
|| ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
)
299 while (sdp
!= NULL
) {
300 (void)ml_nofault_copy( (vm_offset_t
)&sdp
->sdp_savedval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
301 (vm_size_t
)sizeof(sdp
->sdp_savedval
));
303 * Make the patched instruction visible via a data + instruction
304 * cache flush on platforms that need it
306 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
307 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
315 static dtrace_pops_t sdt_pops
= {
330 sdt_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
333 sdt_provider_t
*prov
;
335 if (ddi_create_minor_node(devi
, "sdt", S_IFCHR
,
336 0, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
337 cmn_err(CE_NOTE
, "/dev/sdt couldn't create minor node");
338 ddi_remove_minor_node(devi
, NULL
);
339 return (DDI_FAILURE
);
342 ddi_report_dev(devi
);
345 if (sdt_probetab_size
== 0)
346 sdt_probetab_size
= SDT_PROBETAB_SIZE
;
348 sdt_probetab_mask
= sdt_probetab_size
- 1;
350 kmem_zalloc(sdt_probetab_size
* sizeof (sdt_probe_t
*), KM_SLEEP
);
351 dtrace_invop_add(sdt_invop
);
353 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
354 if (dtrace_register(prov
->sdtp_name
, prov
->sdtp_attr
,
355 DTRACE_PRIV_KERNEL
, NULL
,
356 &sdt_pops
, prov
, &prov
->sdtp_id
) != 0) {
357 cmn_err(CE_WARN
, "failed to register sdt provider %s",
362 return (DDI_SUCCESS
);
366 * APPLE NOTE: sdt_detach not implemented
368 #if !defined(__APPLE__)
371 sdt_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
373 sdt_provider_t
*prov
;
380 return (DDI_SUCCESS
);
383 return (DDI_FAILURE
);
386 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
387 if (prov
->sdtp_id
!= DTRACE_PROVNONE
) {
388 if (dtrace_unregister(prov
->sdtp_id
) != 0)
389 return (DDI_FAILURE
);
391 prov
->sdtp_id
= DTRACE_PROVNONE
;
395 dtrace_invop_remove(sdt_invop
);
396 kmem_free(sdt_probetab
, sdt_probetab_size
* sizeof (sdt_probe_t
*));
398 return (DDI_SUCCESS
);
400 #endif /* __APPLE__ */
404 int _sdt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
406 #pragma unused(dev,flags,devtype,p)
410 #define SDT_MAJOR -24 /* let the kernel pick the device number */
413 * A struct describing which functions will get invoked for certain
416 static struct cdevsw sdt_cdevsw
=
418 _sdt_open
, /* open */
419 eno_opcl
, /* close */
420 eno_rdwrt
, /* read */
421 eno_rdwrt
, /* write */
422 eno_ioctl
, /* ioctl */
423 (stop_fcn_t
*)nulldev
, /* stop */
424 (reset_fcn_t
*)nulldev
, /* reset */
426 eno_select
, /* select */
428 eno_strat
, /* strategy */
434 static int gSDTInited
= 0;
435 static struct modctl g_sdt_kernctl
;
436 static struct module g_sdt_mach_module
;
438 #include <mach-o/nlist.h>
439 #include <libkern/kernel_mach_header.h>
441 void sdt_init( void )
445 int majdevno
= cdevsw_add(SDT_MAJOR
, &sdt_cdevsw
);
448 printf("sdt_init: failed to allocate a major number!\n");
453 if (dtrace_sdt_probes_restricted()) {
457 if (MH_MAGIC_KERNEL
!= _mh_execute_header
.magic
) {
458 g_sdt_kernctl
.mod_address
= (vm_address_t
)NULL
;
459 g_sdt_kernctl
.mod_size
= 0;
461 kernel_mach_header_t
*mh
;
462 struct load_command
*cmd
;
463 kernel_segment_command_t
*orig_ts
= NULL
, *orig_le
= NULL
;
464 struct symtab_command
*orig_st
= NULL
;
465 kernel_nlist_t
*sym
= NULL
;
469 g_sdt_mach_module
.sdt_nprobes
= 0;
470 g_sdt_mach_module
.sdt_probes
= NULL
;
472 g_sdt_kernctl
.mod_address
= (vm_address_t
)&g_sdt_mach_module
;
473 g_sdt_kernctl
.mod_size
= 0;
474 strncpy((char *)&(g_sdt_kernctl
.mod_modname
), "mach_kernel", KMOD_MAX_NAME
);
476 g_sdt_kernctl
.mod_next
= NULL
;
477 g_sdt_kernctl
.mod_stale
= NULL
;
478 g_sdt_kernctl
.mod_id
= 0;
479 g_sdt_kernctl
.mod_loadcnt
= 1;
480 g_sdt_kernctl
.mod_loaded
= 1;
481 g_sdt_kernctl
.mod_flags
= 0;
482 g_sdt_kernctl
.mod_nenabled
= 0;
484 mh
= &_mh_execute_header
;
485 cmd
= (struct load_command
*) &mh
[1];
486 for (i
= 0; i
< mh
->ncmds
; i
++) {
487 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
488 kernel_segment_command_t
*orig_sg
= (kernel_segment_command_t
*) cmd
;
490 if (LIT_STRNEQL(orig_sg
->segname
, SEG_TEXT
))
492 else if (LIT_STRNEQL(orig_sg
->segname
, SEG_LINKEDIT
))
494 else if (LIT_STRNEQL(orig_sg
->segname
, ""))
495 orig_ts
= orig_sg
; /* kexts have a single unnamed segment */
497 else if (cmd
->cmd
== LC_SYMTAB
)
498 orig_st
= (struct symtab_command
*) cmd
;
500 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
503 if ((orig_ts
== NULL
) || (orig_st
== NULL
) || (orig_le
== NULL
))
506 sym
= (kernel_nlist_t
*)(orig_le
->vmaddr
+ orig_st
->symoff
- orig_le
->fileoff
);
507 strings
= (char *)(orig_le
->vmaddr
+ orig_st
->stroff
- orig_le
->fileoff
);
509 for (i
= 0; i
< orig_st
->nsyms
; i
++) {
510 uint8_t n_type
= sym
[i
].n_type
& (N_TYPE
| N_EXT
);
511 char *name
= strings
+ sym
[i
].n_un
.n_strx
;
512 const char *prev_name
;
516 /* Check that the symbol is a global and that it has a name. */
517 if (((N_SECT
| N_EXT
) != n_type
&& (N_ABS
| N_EXT
) != n_type
))
520 if (0 == sym
[i
].n_un
.n_strx
) /* iff a null, "", name. */
523 /* Lop off omnipresent leading underscore. */
527 if (strncmp(name
, DTRACE_PROBE_PREFIX
, sizeof(DTRACE_PROBE_PREFIX
) - 1) == 0) {
528 sdt_probedesc_t
*sdpd
= kmem_alloc(sizeof(sdt_probedesc_t
), KM_SLEEP
);
529 int len
= strlen(name
) + 1;
531 sdpd
->sdpd_name
= kmem_alloc(len
, KM_SLEEP
);
532 strncpy(sdpd
->sdpd_name
, name
, len
); /* NUL termination is ensured. */
534 prev_name
= "<unknown>";
538 * Find the symbol immediately preceding the sdt probe site just discovered,
539 * that symbol names the function containing the sdt probe.
541 for (j
= 0; j
< orig_st
->nsyms
; j
++) {
542 uint8_t jn_type
= sym
[j
].n_type
& (N_TYPE
| N_EXT
);
543 char *jname
= strings
+ sym
[j
].n_un
.n_strx
;
545 if (((N_SECT
| N_EXT
) != jn_type
&& (N_ABS
| N_EXT
) != jn_type
))
548 if (0 == sym
[j
].n_un
.n_strx
) /* iff a null, "", name. */
554 if (*(unsigned long *)sym
[i
].n_value
<= (unsigned long)sym
[j
].n_value
)
557 if ((unsigned long)sym
[j
].n_value
> best
) {
558 best
= (unsigned long)sym
[j
].n_value
;
563 sdpd
->sdpd_func
= kmem_alloc((len
= strlen(prev_name
) + 1), KM_SLEEP
);
564 strncpy(sdpd
->sdpd_func
, prev_name
, len
); /* NUL termination is ensured. */
566 sdpd
->sdpd_offset
= *(unsigned long *)sym
[i
].n_value
;
569 printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
570 sdpd
->sdpd_offset
, *(unsigned long *)sym
[i
].n_value
, name
);
573 sdpd
->sdpd_next
= g_sdt_mach_module
.sdt_probes
;
574 g_sdt_mach_module
.sdt_probes
= sdpd
;
581 sdt_attach( (dev_info_t
*)(uintptr_t)majdevno
, DDI_ATTACH
);
585 panic("sdt_init: called twice!\n");
592 sdt_provide_module(void *arg
, struct modctl
*ctl
)
596 ASSERT(dtrace_kernel_symbol_mode
!= DTRACE_KERNEL_SYMBOLS_NEVER
);
597 lck_mtx_assert(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
599 if (MOD_SDT_DONE(ctl
))
602 if (MOD_IS_MACH_KERNEL(ctl
)) {
603 __sdt_provide_module(arg
, &g_sdt_kernctl
);
605 sdt_probedesc_t
*sdpd
= g_sdt_mach_module
.sdt_probes
;
607 sdt_probedesc_t
*this_sdpd
= sdpd
;
608 kmem_free((void *)sdpd
->sdpd_name
, strlen(sdpd
->sdpd_name
) + 1);
609 kmem_free((void *)sdpd
->sdpd_func
, strlen(sdpd
->sdpd_func
) + 1);
610 sdpd
= sdpd
->sdpd_next
;
611 kmem_free((void *)this_sdpd
, sizeof(sdt_probedesc_t
));
613 g_sdt_mach_module
.sdt_probes
= NULL
;
616 * APPLE NOTE: sdt probes for kexts not yet implemented
620 /* Need to mark this module as completed */
621 ctl
->mod_flags
|= MODCTL_SDT_PROBES_PROVIDED
;