]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/sdt.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / sdt.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /* #pragma ident "@(#)sdt.c 1.9 08/07/01 SMI" */
27
28 #ifdef KERNEL
29 #ifndef _KERNEL
30 #define _KERNEL /* Solaris vs. Darwin */
31 #endif
32 #endif
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <miscfs/devfs/devfs.h>
42
43
44 #include <sys/dtrace.h>
45 #include <sys/dtrace_impl.h>
46
47 #include <sys/dtrace_glue.h>
48
49 #include <sys/sdt_impl.h>
50 extern int dtrace_kernel_symbol_mode;
51
52 /* #include <machine/trap.h */
53 struct savearea_t; /* Used anonymously */
54
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
60 #define SDT_AFRAMES 6
61 #else
62 #error Unknown architecture
63 #endif
64
65 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
66
67 #define DTRACE_PROBE_PREFIX "_dtrace_probe$"
68
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;
74
75 /*ARGSUSED*/
76 static void
77 __sdt_provide_module(void *arg, struct modctl *ctl)
78 {
79 #pragma unused(arg)
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;
84 sdt_provider_t *prov;
85 int len;
86
87 /*
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.
90 */
91 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
92 if (prov->sdtp_id == DTRACE_PROVNONE)
93 return;
94 }
95
96 if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
97 return;
98
99 for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
100 const char *name = sdpd->sdpd_name, *func;
101 char *nname;
102 int i, j;
103 dtrace_id_t id;
104
105 for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
106 const char *prefpart, *prefix = prov->sdtp_prefix;
107
108 if ((prefpart = strstr(name, prefix))) {
109 name = prefpart + strlen(prefix);
110 break;
111 }
112 }
113
114 nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
115
116 for (i = 0, j = 0; name[j] != '\0'; i++) {
117 if (name[j] == '_' && name[j + 1] == '_') {
118 nname[i] = '-';
119 j += 2;
120 } else {
121 nname[i] = name[j++];
122 }
123 }
124
125 nname[i] = '\0';
126
127 sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
128 sdp->sdp_loadcnt = ctl->mod_loadcnt;
129 sdp->sdp_ctl = ctl;
130 sdp->sdp_name = nname;
131 sdp->sdp_namelen = len;
132 sdp->sdp_provider = prov;
133
134 func = sdpd->sdpd_func;
135
136 if (func == NULL)
137 func = "<unknown>";
138
139 /*
140 * We have our provider. Now create the probe.
141 */
142 if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
143 func, nname)) != DTRACE_IDNONE) {
144 old = dtrace_probe_arg(prov->sdtp_id, id);
145 ASSERT(old != NULL);
146
147 sdp->sdp_next = old->sdp_next;
148 sdp->sdp_id = id;
149 old->sdp_next = sdp;
150 } else {
151 sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
152 modname, func, nname, SDT_AFRAMES, sdp);
153
154 mp->sdt_nprobes++;
155 }
156
157 #if 0
158 printf ("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd, sdp, nname, sdp->sdp_id);
159 #endif
160
161 sdp->sdp_hashnext =
162 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
163 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
164
165 sdp->sdp_patchval = SDT_PATCHVAL;
166 sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset;
167 sdp->sdp_savedval = *sdp->sdp_patchpoint;
168 }
169 }
170
171 /*ARGSUSED*/
172 static void
173 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
174 {
175 #pragma unused(arg,id)
176 sdt_probe_t *sdp = parg, *old, *last, *hash;
177 int ndx;
178
179 #if !defined(__APPLE__)
180 /*
181 * APPLE NOTE: sdt probes for kexts not yet implemented
182 */
183 struct modctl *ctl = sdp->sdp_ctl;
184
185 if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
186 if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
187 ctl->mod_loaded)) {
188 ((struct module *)(ctl->mod_mp))->sdt_nprobes--;
189 }
190 }
191 #endif /* __APPLE__ */
192
193 while (sdp != NULL) {
194 old = sdp;
195
196 /*
197 * Now we need to remove this probe from the sdt_probetab.
198 */
199 ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
200 last = NULL;
201 hash = sdt_probetab[ndx];
202
203 while (hash != sdp) {
204 ASSERT(hash != NULL);
205 last = hash;
206 hash = hash->sdp_hashnext;
207 }
208
209 if (last != NULL) {
210 last->sdp_hashnext = sdp->sdp_hashnext;
211 } else {
212 sdt_probetab[ndx] = sdp->sdp_hashnext;
213 }
214
215 kmem_free(sdp->sdp_name, sdp->sdp_namelen);
216 sdp = sdp->sdp_next;
217 kmem_free(old, sizeof (sdt_probe_t));
218 }
219 }
220
221 /*ARGSUSED*/
222 static int
223 sdt_enable(void *arg, dtrace_id_t id, void *parg)
224 {
225 #pragma unused(arg,id)
226 sdt_probe_t *sdp = parg;
227 struct modctl *ctl = sdp->sdp_ctl;
228
229 ctl->mod_nenabled++;
230
231 /*
232 * If this module has disappeared since we discovered its probes,
233 * refuse to enable it.
234 */
235 if (!ctl->mod_loaded) {
236 if (sdt_verbose) {
237 cmn_err(CE_NOTE, "sdt is failing for probe %s "
238 "(module %s unloaded)",
239 sdp->sdp_name, ctl->mod_modname);
240 }
241 goto err;
242 }
243
244 /*
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.
248 */
249 if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
250 if (sdt_verbose) {
251 cmn_err(CE_NOTE, "sdt is failing for probe %s "
252 "(module %s reloaded)",
253 sdp->sdp_name, ctl->mod_modname);
254 }
255 goto err;
256 }
257
258 dtrace_casptr(&tempDTraceTrapHook, NULL, fbt_perfCallback);
259 if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) {
260 if (sdt_verbose) {
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);
264 }
265 return (0);
266 }
267
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));
271 sdp = sdp->sdp_next;
272 }
273
274 err:
275 return (0);
276 }
277
278 /*ARGSUSED*/
279 static void
280 sdt_disable(void *arg, dtrace_id_t id, void *parg)
281 {
282 #pragma unused(arg,id)
283 sdt_probe_t *sdp = parg;
284 struct modctl *ctl = sdp->sdp_ctl;
285
286 ctl->mod_nenabled--;
287
288 if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt)
289 goto err;
290
291 while (sdp != NULL) {
292 (void)ml_nofault_copy( (vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint,
293 (vm_size_t)sizeof(sdp->sdp_savedval));
294 sdp = sdp->sdp_next;
295 }
296
297 err:
298 ;
299 }
300
301 static dtrace_pops_t sdt_pops = {
302 NULL,
303 sdt_provide_module,
304 sdt_enable,
305 sdt_disable,
306 NULL,
307 NULL,
308 sdt_getargdesc,
309 sdt_getarg,
310 NULL,
311 sdt_destroy
312 };
313
314 /*ARGSUSED*/
315 static int
316 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
317 {
318 #pragma unused(cmd)
319 sdt_provider_t *prov;
320
321 if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
322 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
323 cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
324 ddi_remove_minor_node(devi, NULL);
325 return (DDI_FAILURE);
326 }
327
328 ddi_report_dev(devi);
329 sdt_devi = devi;
330
331 if (sdt_probetab_size == 0)
332 sdt_probetab_size = SDT_PROBETAB_SIZE;
333
334 sdt_probetab_mask = sdt_probetab_size - 1;
335 sdt_probetab =
336 kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
337 dtrace_invop_add(sdt_invop);
338
339 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
340 if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
341 DTRACE_PRIV_KERNEL, NULL,
342 &sdt_pops, prov, &prov->sdtp_id) != 0) {
343 cmn_err(CE_WARN, "failed to register sdt provider %s",
344 prov->sdtp_name);
345 }
346 }
347
348 return (DDI_SUCCESS);
349 }
350
351 /*
352 * APPLE NOTE: sdt_detach not implemented
353 */
354 #if !defined(__APPLE__)
355 /*ARGSUSED*/
356 static int
357 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
358 {
359 sdt_provider_t *prov;
360
361 switch (cmd) {
362 case DDI_DETACH:
363 break;
364
365 case DDI_SUSPEND:
366 return (DDI_SUCCESS);
367
368 default:
369 return (DDI_FAILURE);
370 }
371
372 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
373 if (prov->sdtp_id != DTRACE_PROVNONE) {
374 if (dtrace_unregister(prov->sdtp_id) != 0)
375 return (DDI_FAILURE);
376
377 prov->sdtp_id = DTRACE_PROVNONE;
378 }
379 }
380
381 dtrace_invop_remove(sdt_invop);
382 kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
383
384 return (DDI_SUCCESS);
385 }
386 #endif /* __APPLE__ */
387
388 d_open_t _sdt_open;
389
390 int _sdt_open(dev_t dev, int flags, int devtype, struct proc *p)
391 {
392 #pragma unused(dev,flags,devtype,p)
393 return 0;
394 }
395
396 #define SDT_MAJOR -24 /* let the kernel pick the device number */
397
398 /*
399 * A struct describing which functions will get invoked for certain
400 * actions.
401 */
402 static struct cdevsw sdt_cdevsw =
403 {
404 _sdt_open, /* open */
405 eno_opcl, /* close */
406 eno_rdwrt, /* read */
407 eno_rdwrt, /* write */
408 eno_ioctl, /* ioctl */
409 (stop_fcn_t *)nulldev, /* stop */
410 (reset_fcn_t *)nulldev, /* reset */
411 NULL, /* tty's */
412 eno_select, /* select */
413 eno_mmap, /* mmap */
414 eno_strat, /* strategy */
415 eno_getc, /* getc */
416 eno_putc, /* putc */
417 0 /* type */
418 };
419
420 static int gSDTInited = 0;
421 static struct modctl g_sdt_kernctl;
422 static struct module g_sdt_mach_module;
423
424 #include <mach-o/nlist.h>
425 #include <libkern/kernel_mach_header.h>
426
427 void sdt_init( void )
428 {
429 if (0 == gSDTInited)
430 {
431 int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw);
432
433 if (majdevno < 0) {
434 printf("sdt_init: failed to allocate a major number!\n");
435 gSDTInited = 0;
436 return;
437 }
438
439 if (dtrace_fbt_probes_restricted()) {
440 return;
441 }
442
443 if (MH_MAGIC_KERNEL != _mh_execute_header.magic) {
444 g_sdt_kernctl.mod_address = (vm_address_t)NULL;
445 g_sdt_kernctl.mod_size = 0;
446 } else {
447 kernel_mach_header_t *mh;
448 struct load_command *cmd;
449 kernel_segment_command_t *orig_ts = NULL, *orig_le = NULL;
450 struct symtab_command *orig_st = NULL;
451 kernel_nlist_t *sym = NULL;
452 char *strings;
453 unsigned int i;
454
455 g_sdt_mach_module.sdt_nprobes = 0;
456 g_sdt_mach_module.sdt_probes = NULL;
457
458 g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module;
459 g_sdt_kernctl.mod_size = 0;
460 strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME);
461
462 g_sdt_kernctl.mod_next = NULL;
463 g_sdt_kernctl.mod_stale = NULL;
464 g_sdt_kernctl.mod_id = 0;
465 g_sdt_kernctl.mod_loadcnt = 1;
466 g_sdt_kernctl.mod_loaded = 1;
467 g_sdt_kernctl.mod_flags = 0;
468 g_sdt_kernctl.mod_nenabled = 0;
469
470 mh = &_mh_execute_header;
471 cmd = (struct load_command*) &mh[1];
472 for (i = 0; i < mh->ncmds; i++) {
473 if (cmd->cmd == LC_SEGMENT_KERNEL) {
474 kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd;
475
476 if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT))
477 orig_ts = orig_sg;
478 else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT))
479 orig_le = orig_sg;
480 else if (LIT_STRNEQL(orig_sg->segname, ""))
481 orig_ts = orig_sg; /* kexts have a single unnamed segment */
482 }
483 else if (cmd->cmd == LC_SYMTAB)
484 orig_st = (struct symtab_command *) cmd;
485
486 cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize);
487 }
488
489 if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL))
490 return;
491
492 sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff);
493 strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff);
494
495 for (i = 0; i < orig_st->nsyms; i++) {
496 uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
497 char *name = strings + sym[i].n_un.n_strx;
498 const char *prev_name;
499 unsigned long best;
500 unsigned int j;
501
502 /* Check that the symbol is a global and that it has a name. */
503 if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type))
504 continue;
505
506 if (0 == sym[i].n_un.n_strx) /* iff a null, "", name. */
507 continue;
508
509 /* Lop off omnipresent leading underscore. */
510 if (*name == '_')
511 name += 1;
512
513 if (strncmp(name, DTRACE_PROBE_PREFIX, sizeof(DTRACE_PROBE_PREFIX) - 1) == 0) {
514 sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP);
515 int len = strlen(name) + 1;
516
517 sdpd->sdpd_name = kmem_alloc(len, KM_SLEEP);
518 strncpy(sdpd->sdpd_name, name, len); /* NUL termination is ensured. */
519
520 prev_name = "<unknown>";
521 best = 0;
522
523 /*
524 * Find the symbol immediately preceding the sdt probe site just discovered,
525 * that symbol names the function containing the sdt probe.
526 */
527 for (j = 0; j < orig_st->nsyms; j++) {
528 uint8_t jn_type = sym[j].n_type & (N_TYPE | N_EXT);
529 char *jname = strings + sym[j].n_un.n_strx;
530
531 if (((N_SECT | N_EXT) != jn_type && (N_ABS | N_EXT) != jn_type))
532 continue;
533
534 if (0 == sym[j].n_un.n_strx) /* iff a null, "", name. */
535 continue;
536
537 if (*jname == '_')
538 jname += 1;
539
540 if (*(unsigned long *)sym[i].n_value <= (unsigned long)sym[j].n_value)
541 continue;
542
543 if ((unsigned long)sym[j].n_value > best) {
544 best = (unsigned long)sym[j].n_value;
545 prev_name = jname;
546 }
547 }
548
549 sdpd->sdpd_func = kmem_alloc((len = strlen(prev_name) + 1), KM_SLEEP);
550 strncpy(sdpd->sdpd_func, prev_name, len); /* NUL termination is ensured. */
551
552 sdpd->sdpd_offset = *(unsigned long *)sym[i].n_value;
553
554 #if 0
555 printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
556 sdpd->sdpd_offset, *(unsigned long *)sym[i].n_value, name);
557 #endif
558
559 sdpd->sdpd_next = g_sdt_mach_module.sdt_probes;
560 g_sdt_mach_module.sdt_probes = sdpd;
561 } else {
562 prev_name = name;
563 }
564 }
565 }
566
567 sdt_attach( (dev_info_t *)(uintptr_t)majdevno, DDI_ATTACH );
568
569 gSDTInited = 1;
570 } else
571 panic("sdt_init: called twice!\n");
572 }
573
574 #undef SDT_MAJOR
575
576 /*ARGSUSED*/
577 void
578 sdt_provide_module(void *arg, struct modctl *ctl)
579 {
580 #pragma unused(arg)
581 ASSERT(ctl != NULL);
582 ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER);
583 lck_mtx_assert(&mod_lock, LCK_MTX_ASSERT_OWNED);
584
585 if (MOD_SDT_DONE(ctl))
586 return;
587
588 if (MOD_IS_MACH_KERNEL(ctl)) {
589 __sdt_provide_module(arg, &g_sdt_kernctl);
590
591 sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes;
592 while (sdpd) {
593 sdt_probedesc_t *this_sdpd = sdpd;
594 kmem_free((void *)sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1);
595 kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1);
596 sdpd = sdpd->sdpd_next;
597 kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t));
598 }
599 g_sdt_mach_module.sdt_probes = NULL;
600 } else {
601 /*
602 * APPLE NOTE: sdt probes for kexts not yet implemented
603 */
604 }
605
606 /* Need to mark this module as completed */
607 ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED;
608 }