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]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
28 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/cpuvar.h>
37 #include <sys/endian.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
41 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/limits.h>
45 #include <sys/linker.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
53 #include <sys/selinfo.h>
55 #include <sys/syscall.h>
56 #include <sys/sysent.h>
57 #include <sys/sysproto.h>
59 #include <sys/unistd.h>
60 #include <machine/stdarg.h>
62 #include <sys/dtrace.h>
63 #include <sys/dtrace_bsd.h>
67 MALLOC_DEFINE(M_FBT, "fbt", "Function Boundary Tracing");
69 dtrace_provider_id_t fbt_id;
70 fbt_probe_t **fbt_probetab;
71 int fbt_probetab_mask;
73 static d_open_t fbt_open;
74 static int fbt_unload(void);
75 static void fbt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
76 static void fbt_provide_module(void *, modctl_t *);
77 static void fbt_destroy(void *, dtrace_id_t, void *);
78 static void fbt_enable(void *, dtrace_id_t, void *);
79 static void fbt_disable(void *, dtrace_id_t, void *);
80 static void fbt_load(void *);
81 static void fbt_suspend(void *, dtrace_id_t, void *);
82 static void fbt_resume(void *, dtrace_id_t, void *);
84 static struct cdevsw fbt_cdevsw = {
85 .d_version = D_VERSION,
90 static dtrace_pattr_t fbt_attr = {
91 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
92 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
93 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
94 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
95 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
98 static dtrace_pops_t fbt_pops = {
100 .dtps_provide_module = fbt_provide_module,
101 .dtps_enable = fbt_enable,
102 .dtps_disable = fbt_disable,
103 .dtps_suspend = fbt_suspend,
104 .dtps_resume = fbt_resume,
105 .dtps_getargdesc = fbt_getargdesc,
106 .dtps_getargval = NULL,
107 .dtps_usermode = NULL,
108 .dtps_destroy = fbt_destroy
111 static struct cdev *fbt_cdev;
112 static int fbt_probetab_size;
113 static int fbt_verbose = 0;
116 fbt_excluded(const char *name)
119 if (strncmp(name, "dtrace_", 7) == 0 &&
120 strncmp(name, "dtrace_safe_", 12) != 0) {
122 * Anything beginning with "dtrace_" may be called
123 * from probe context unless it explicitly indicates
124 * that it won't be called from probe context by
125 * using the prefix "dtrace_safe_".
131 * Lock owner methods may be called from probe context.
133 if (strcmp(name, "owner_mtx") == 0 ||
134 strcmp(name, "owner_rm") == 0 ||
135 strcmp(name, "owner_rw") == 0 ||
136 strcmp(name, "owner_sx") == 0)
140 * When DTrace is built into the kernel we need to exclude
141 * the FBT functions from instrumentation.
144 if (strncmp(name, "fbt_", 4) == 0)
157 for (i = 0; i < fbt_probetab_size; i++) {
158 fbt = fbt_probetab[i];
160 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
161 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
166 fbt_provide_module(void *arg, modctl_t *lf)
168 char modname[MAXPATHLEN];
172 strlcpy(modname, lf->filename, sizeof(modname));
173 len = strlen(modname);
174 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
175 modname[len - 3] = '\0';
178 * Employees of dtrace and their families are ineligible. Void
181 if (strcmp(modname, "dtrace") == 0)
185 * To register with DTrace, a module must list 'dtrace' as a
186 * dependency in order for the kernel linker to resolve
187 * symbols like dtrace_register(). All modules with such a
188 * dependency are ineligible for FBT tracing.
190 for (i = 0; i < lf->ndeps; i++)
191 if (strncmp(lf->deps[i]->filename, "dtrace", 6) == 0)
194 if (lf->fbt_nentries) {
196 * This module has some FBT entries allocated; we're afraid
203 * List the functions in the module and the symbol values.
205 (void) linker_file_function_listall(lf, fbt_provide_module_function, modname);
209 fbt_destroy_one(fbt_probe_t *fbt)
211 fbt_probe_t *hash, *hashprev, *next;
214 ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
215 for (hash = fbt_probetab[ndx], hashprev = NULL; hash != NULL;
216 hashprev = hash, hash = hash->fbtp_hashnext) {
218 if ((next = fbt->fbtp_tracenext) != NULL)
219 next->fbtp_hashnext = hash->fbtp_hashnext;
221 next = hash->fbtp_hashnext;
222 if (hashprev != NULL)
223 hashprev->fbtp_hashnext = next;
225 fbt_probetab[ndx] = next;
227 } else if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
228 for (next = hash; next->fbtp_tracenext != NULL;
229 next = next->fbtp_tracenext) {
230 if (fbt == next->fbtp_tracenext) {
231 next->fbtp_tracenext =
238 panic("probe %p not found in hash table", fbt);
244 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
246 fbt_probe_t *fbt = parg, *next;
253 next = fbt->fbtp_probenext;
254 fbt_destroy_one(fbt);
256 } while (fbt != NULL);
260 fbt_enable(void *arg, dtrace_id_t id, void *parg)
262 fbt_probe_t *fbt = parg;
263 modctl_t *ctl = fbt->fbtp_ctl;
268 * Now check that our modctl has the expected load count. If it
269 * doesn't, this module must have been unloaded and reloaded -- and
270 * we're not going to touch it.
272 if (ctl->loadcnt != fbt->fbtp_loadcnt) {
274 printf("fbt is failing for probe %s "
275 "(module %s reloaded)",
276 fbt->fbtp_name, ctl->filename);
282 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
283 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
289 fbt_disable(void *arg, dtrace_id_t id, void *parg)
291 fbt_probe_t *fbt = parg, *hash;
292 modctl_t *ctl = fbt->fbtp_ctl;
294 ASSERT(ctl->nenabled > 0);
297 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
300 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
303 for (hash = fbt_probetab[FBT_ADDR2NDX(fbt->fbtp_patchpoint)];
304 hash != NULL; hash = hash->fbtp_hashnext) {
305 if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
306 for (; hash != NULL; hash = hash->fbtp_tracenext)
307 if (hash->fbtp_enabled > 0)
313 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
318 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
320 fbt_probe_t *fbt = parg;
321 modctl_t *ctl = fbt->fbtp_ctl;
323 ASSERT(ctl->nenabled > 0);
325 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
328 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
329 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
333 fbt_resume(void *arg, dtrace_id_t id, void *parg)
335 fbt_probe_t *fbt = parg;
336 modctl_t *ctl = fbt->fbtp_ctl;
338 ASSERT(ctl->nenabled > 0);
340 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
343 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
344 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
348 fbt_ctfoff_init(modctl_t *lf, linker_ctf_t *lc)
350 const Elf_Sym *symp = lc->symtab;;
351 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
352 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
355 uint32_t objtoff = hp->cth_objtoff;
356 uint32_t funcoff = hp->cth_funcoff;
361 if (hp->cth_magic != CTF_MAGIC) {
362 printf("Bad magic value in CTF data of '%s'\n",lf->pathname);
366 if (lc->symtab == NULL) {
367 printf("No symbol table in '%s'\n",lf->pathname);
371 ctfoff = malloc(sizeof(uint32_t) * lc->nsym, M_LINKER, M_WAITOK);
372 *lc->ctfoffp = ctfoff;
374 for (i = 0; i < lc->nsym; i++, ctfoff++, symp++) {
375 if (symp->st_name == 0 || symp->st_shndx == SHN_UNDEF) {
376 *ctfoff = 0xffffffff;
380 switch (ELF_ST_TYPE(symp->st_info)) {
382 if (objtoff >= hp->cth_funcoff ||
383 (symp->st_shndx == SHN_ABS && symp->st_value == 0)) {
384 *ctfoff = 0xffffffff;
389 objtoff += sizeof (ushort_t);
393 if (funcoff >= hp->cth_typeoff) {
394 *ctfoff = 0xffffffff;
400 info = *((const ushort_t *)(ctfdata + funcoff));
401 vlen = CTF_INFO_VLEN(info);
404 * If we encounter a zero pad at the end, just skip it.
405 * Otherwise skip over the function and its return type
406 * (+2) and the argument list (vlen).
408 if (CTF_INFO_KIND(info) == CTF_K_UNKNOWN && vlen == 0)
409 funcoff += sizeof (ushort_t); /* skip pad */
411 funcoff += sizeof (ushort_t) * (vlen + 2);
415 *ctfoff = 0xffffffff;
424 fbt_get_ctt_size(uint8_t version, const ctf_type_t *tp, ssize_t *sizep,
427 ssize_t size, increment;
429 if (version > CTF_VERSION_1 &&
430 tp->ctt_size == CTF_LSIZE_SENT) {
431 size = CTF_TYPE_LSIZE(tp);
432 increment = sizeof (ctf_type_t);
435 increment = sizeof (ctf_stype_t);
441 *incrementp = increment;
447 fbt_typoff_init(linker_ctf_t *lc)
449 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
450 const ctf_type_t *tbuf;
451 const ctf_type_t *tend;
452 const ctf_type_t *tp;
453 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
456 ulong_t pop[CTF_K_MAX + 1] = { 0 };
460 if (hp->cth_magic != CTF_MAGIC)
463 tbuf = (const ctf_type_t *) (ctfdata + hp->cth_typeoff);
464 tend = (const ctf_type_t *) (ctfdata + hp->cth_stroff);
466 int child = hp->cth_parname != 0;
469 * We make two passes through the entire type section. In this first
470 * pass, we count the number of each type and the total number of types.
472 for (tp = tbuf; tp < tend; ctf_typemax++) {
473 ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
474 ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
475 ssize_t size, increment;
480 (void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);
485 vbytes = sizeof (uint_t);
488 vbytes = sizeof (ctf_array_t);
491 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
495 if (size < CTF_LSTRUCT_THRESH) {
496 ctf_member_t *mp = (ctf_member_t *)
497 ((uintptr_t)tp + increment);
499 vbytes = sizeof (ctf_member_t) * vlen;
500 for (n = vlen; n != 0; n--, mp++)
501 child |= CTF_TYPE_ISCHILD(mp->ctm_type);
503 ctf_lmember_t *lmp = (ctf_lmember_t *)
504 ((uintptr_t)tp + increment);
506 vbytes = sizeof (ctf_lmember_t) * vlen;
507 for (n = vlen; n != 0; n--, lmp++)
509 CTF_TYPE_ISCHILD(lmp->ctlm_type);
513 vbytes = sizeof (ctf_enum_t) * vlen;
517 * For forward declarations, ctt_type is the CTF_K_*
518 * kind for the tag, so bump that population count too.
519 * If ctt_type is unknown, treat the tag as a struct.
521 if (tp->ctt_type == CTF_K_UNKNOWN ||
522 tp->ctt_type >= CTF_K_MAX)
535 child |= CTF_TYPE_ISCHILD(tp->ctt_type);
539 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
542 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
546 /* account for a sentinel value below */
548 *lc->typlenp = ctf_typemax;
550 xp = malloc(sizeof(uint32_t) * ctf_typemax, M_LINKER,
555 /* type id 0 is used as a sentinel value */
559 * In the second pass, fill in the type offset.
561 for (tp = tbuf; tp < tend; xp++) {
562 ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
563 ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
564 ssize_t size, increment;
569 (void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);
574 vbytes = sizeof (uint_t);
577 vbytes = sizeof (ctf_array_t);
580 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
584 if (size < CTF_LSTRUCT_THRESH) {
585 ctf_member_t *mp = (ctf_member_t *)
586 ((uintptr_t)tp + increment);
588 vbytes = sizeof (ctf_member_t) * vlen;
589 for (n = vlen; n != 0; n--, mp++)
590 child |= CTF_TYPE_ISCHILD(mp->ctm_type);
592 ctf_lmember_t *lmp = (ctf_lmember_t *)
593 ((uintptr_t)tp + increment);
595 vbytes = sizeof (ctf_lmember_t) * vlen;
596 for (n = vlen; n != 0; n--, lmp++)
598 CTF_TYPE_ISCHILD(lmp->ctlm_type);
602 vbytes = sizeof (ctf_enum_t) * vlen;
616 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
619 *xp = (uint32_t)((uintptr_t) tp - (uintptr_t) ctfdata);
620 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
627 * CTF Declaration Stack
629 * In order to implement ctf_type_name(), we must convert a type graph back
630 * into a C type declaration. Unfortunately, a type graph represents a storage
631 * class ordering of the type whereas a type declaration must obey the C rules
632 * for operator precedence, and the two orderings are frequently in conflict.
633 * For example, consider these CTF type graphs and their C declarations:
635 * CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
636 * CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
638 * In each case, parentheses are used to raise operator * to higher lexical
639 * precedence, so the string form of the C declaration cannot be constructed by
640 * walking the type graph links and forming the string from left to right.
642 * The functions in this file build a set of stacks from the type graph nodes
643 * corresponding to the C operator precedence levels in the appropriate order.
644 * The code in ctf_type_name() can then iterate over the levels and nodes in
645 * lexical precedence order and construct the final C declaration string.
647 typedef struct ctf_list {
648 struct ctf_list *l_prev; /* previous pointer or tail pointer */
649 struct ctf_list *l_next; /* next pointer or head pointer */
652 #define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev))
653 #define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next))
663 typedef struct ctf_decl_node {
664 ctf_list_t cd_list; /* linked list pointers */
665 ctf_id_t cd_type; /* type identifier */
666 uint_t cd_kind; /* type kind */
667 uint_t cd_n; /* type dimension if array */
670 typedef struct ctf_decl {
671 ctf_list_t cd_nodes[CTF_PREC_MAX]; /* declaration node stacks */
672 int cd_order[CTF_PREC_MAX]; /* storage order of decls */
673 ctf_decl_prec_t cd_qualp; /* qualifier precision */
674 ctf_decl_prec_t cd_ordp; /* ordered precision */
675 char *cd_buf; /* buffer for output */
676 char *cd_ptr; /* buffer location */
677 char *cd_end; /* buffer limit */
678 size_t cd_len; /* buffer space required */
679 int cd_err; /* saved error value */
683 * Simple doubly-linked list append routine. This implementation assumes that
684 * each list element contains an embedded ctf_list_t as the first member.
685 * An additional ctf_list_t is used to store the head (l_next) and tail
686 * (l_prev) pointers. The current head and tail list elements have their
687 * previous and next pointers set to NULL, respectively.
690 ctf_list_append(ctf_list_t *lp, void *new)
692 ctf_list_t *p = lp->l_prev; /* p = tail list element */
693 ctf_list_t *q = new; /* q = new list element */
706 * Prepend the specified existing element to the given ctf_list_t. The
707 * existing pointer should be pointing at a struct with embedded ctf_list_t.
710 ctf_list_prepend(ctf_list_t *lp, void *new)
712 ctf_list_t *p = new; /* p = new list element */
713 ctf_list_t *q = lp->l_next; /* q = head list element */
726 ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)
730 bzero(cd, sizeof (ctf_decl_t));
732 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
733 cd->cd_order[i] = CTF_PREC_BASE - 1;
735 cd->cd_qualp = CTF_PREC_BASE;
736 cd->cd_ordp = CTF_PREC_BASE;
740 cd->cd_end = buf + len;
744 ctf_decl_fini(ctf_decl_t *cd)
746 ctf_decl_node_t *cdp, *ndp;
749 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {
750 for (cdp = ctf_list_next(&cd->cd_nodes[i]);
751 cdp != NULL; cdp = ndp) {
752 ndp = ctf_list_next(cdp);
758 static const ctf_type_t *
759 ctf_lookup_by_id(linker_ctf_t *lc, ctf_id_t type)
761 const ctf_type_t *tp;
763 uint32_t *typoff = *lc->typoffp;
765 if (type >= *lc->typlenp) {
766 printf("%s(%d): type %d exceeds max %ld\n",__func__,__LINE__,(int) type,*lc->typlenp);
770 /* Check if the type isn't cross-referenced. */
771 if ((offset = typoff[type]) == 0) {
772 printf("%s(%d): type %d isn't cross referenced\n",__func__,__LINE__, (int) type);
776 tp = (const ctf_type_t *)(lc->ctftab + offset + sizeof(ctf_header_t));
782 fbt_array_info(linker_ctf_t *lc, ctf_id_t type, ctf_arinfo_t *arp)
784 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
785 const ctf_type_t *tp;
786 const ctf_array_t *ap;
789 bzero(arp, sizeof(*arp));
791 if ((tp = ctf_lookup_by_id(lc, type)) == NULL)
794 if (CTF_INFO_KIND(tp->ctt_info) != CTF_K_ARRAY)
797 (void) fbt_get_ctt_size(hp->cth_version, tp, NULL, &increment);
799 ap = (const ctf_array_t *)((uintptr_t)tp + increment);
800 arp->ctr_contents = ap->cta_contents;
801 arp->ctr_index = ap->cta_index;
802 arp->ctr_nelems = ap->cta_nelems;
806 ctf_strptr(linker_ctf_t *lc, int name)
808 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;;
809 const char *strp = "";
811 if (name < 0 || name >= hp->cth_strlen)
814 strp = (const char *)(lc->ctftab + hp->cth_stroff + name + sizeof(ctf_header_t));
820 ctf_decl_push(ctf_decl_t *cd, linker_ctf_t *lc, ctf_id_t type)
822 ctf_decl_node_t *cdp;
823 ctf_decl_prec_t prec;
827 const ctf_type_t *tp;
830 if ((tp = ctf_lookup_by_id(lc, type)) == NULL) {
835 switch (kind = CTF_INFO_KIND(tp->ctt_info)) {
837 fbt_array_info(lc, type, &ar);
838 ctf_decl_push(cd, lc, ar.ctr_contents);
840 prec = CTF_PREC_ARRAY;
844 if (ctf_strptr(lc, tp->ctt_name)[0] == '\0') {
845 ctf_decl_push(cd, lc, tp->ctt_type);
848 prec = CTF_PREC_BASE;
852 ctf_decl_push(cd, lc, tp->ctt_type);
853 prec = CTF_PREC_FUNCTION;
857 ctf_decl_push(cd, lc, tp->ctt_type);
858 prec = CTF_PREC_POINTER;
864 ctf_decl_push(cd, lc, tp->ctt_type);
870 prec = CTF_PREC_BASE;
873 cdp = malloc(sizeof(*cdp), M_FBT, M_WAITOK);
878 if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)
879 cd->cd_order[prec] = cd->cd_ordp++;
882 * Reset cd_qualp to the highest precedence level that we've seen so
883 * far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).
885 if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
889 * C array declarators are ordered inside out so prepend them. Also by
890 * convention qualifiers of base types precede the type specifier (e.g.
891 * const int vs. int const) even though the two forms are equivalent.
893 if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))
894 ctf_list_prepend(&cd->cd_nodes[prec], cdp);
896 ctf_list_append(&cd->cd_nodes[prec], cdp);
900 ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)
902 size_t len = (size_t)(cd->cd_end - cd->cd_ptr);
906 va_start(ap, format);
907 n = vsnprintf(cd->cd_ptr, len, format, ap);
910 cd->cd_ptr += MIN(n, len);
915 fbt_type_name(linker_ctf_t *lc, ctf_id_t type, char *buf, size_t len)
918 ctf_decl_node_t *cdp;
919 ctf_decl_prec_t prec, lp, rp;
923 if (lc == NULL && type == CTF_ERR)
924 return (-1); /* simplify caller code by permitting CTF_ERR */
926 ctf_decl_init(&cd, buf, len);
927 ctf_decl_push(&cd, lc, type);
929 if (cd.cd_err != 0) {
935 * If the type graph's order conflicts with lexical precedence order
936 * for pointers or arrays, then we need to surround the declarations at
937 * the corresponding lexical precedence with parentheses. This can
938 * result in either a parenthesized pointer (*) as in int (*)() or
939 * int (*)[], or in a parenthesized pointer and array as in int (*[])().
941 ptr = cd.cd_order[CTF_PREC_POINTER] > CTF_PREC_POINTER;
942 arr = cd.cd_order[CTF_PREC_ARRAY] > CTF_PREC_ARRAY;
944 rp = arr ? CTF_PREC_ARRAY : ptr ? CTF_PREC_POINTER : -1;
945 lp = ptr ? CTF_PREC_POINTER : arr ? CTF_PREC_ARRAY : -1;
947 k = CTF_K_POINTER; /* avoid leading whitespace (see below) */
949 for (prec = CTF_PREC_BASE; prec < CTF_PREC_MAX; prec++) {
950 for (cdp = ctf_list_next(&cd.cd_nodes[prec]);
951 cdp != NULL; cdp = ctf_list_next(cdp)) {
953 const ctf_type_t *tp =
954 ctf_lookup_by_id(lc, cdp->cd_type);
955 const char *name = ctf_strptr(lc, tp->ctt_name);
957 if (k != CTF_K_POINTER && k != CTF_K_ARRAY)
958 ctf_decl_sprintf(&cd, " ");
961 ctf_decl_sprintf(&cd, "(");
965 switch (cdp->cd_kind) {
969 ctf_decl_sprintf(&cd, "%s", name);
972 ctf_decl_sprintf(&cd, "*");
975 ctf_decl_sprintf(&cd, "[%u]", cdp->cd_n);
978 ctf_decl_sprintf(&cd, "()");
982 ctf_decl_sprintf(&cd, "struct %s", name);
985 ctf_decl_sprintf(&cd, "union %s", name);
988 ctf_decl_sprintf(&cd, "enum %s", name);
991 ctf_decl_sprintf(&cd, "volatile");
994 ctf_decl_sprintf(&cd, "const");
997 ctf_decl_sprintf(&cd, "restrict");
1005 ctf_decl_sprintf(&cd, ")");
1013 fbt_getargdesc(void *arg __unused, dtrace_id_t id __unused, void *parg, dtrace_argdesc_t *desc)
1016 fbt_probe_t *fbt = parg;
1018 modctl_t *ctl = fbt->fbtp_ctl;
1019 int ndx = desc->dtargd_ndx;
1020 int symindx = fbt->fbtp_symindx;
1023 ushort_t info, kind, n;
1025 if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
1026 (void) strcpy(desc->dtargd_native, "int");
1030 desc->dtargd_ndx = DTRACE_ARGNONE;
1032 /* Get a pointer to the CTF data and it's length. */
1033 if (linker_ctf_get(ctl, &lc) != 0)
1034 /* No CTF data? Something wrong? *shrug* */
1037 /* Check if this module hasn't been initialised yet. */
1038 if (*lc.ctfoffp == NULL) {
1040 * Initialise the CTF object and function symindx to
1041 * byte offset array.
1043 if (fbt_ctfoff_init(ctl, &lc) != 0)
1046 /* Initialise the CTF type to byte offset array. */
1047 if (fbt_typoff_init(&lc) != 0)
1051 ctfoff = *lc.ctfoffp;
1053 if (ctfoff == NULL || *lc.typoffp == NULL)
1056 /* Check if the symbol index is out of range. */
1057 if (symindx >= lc.nsym)
1060 /* Check if the symbol isn't cross-referenced. */
1061 if ((offset = ctfoff[symindx]) == 0xffffffff)
1064 dp = (const ushort_t *)(lc.ctftab + offset + sizeof(ctf_header_t));
1067 kind = CTF_INFO_KIND(info);
1068 n = CTF_INFO_VLEN(info);
1070 if (kind == CTF_K_UNKNOWN && n == 0) {
1071 printf("%s(%d): Unknown function!\n",__func__,__LINE__);
1075 if (kind != CTF_K_FUNCTION) {
1076 printf("%s(%d): Expected a function!\n",__func__,__LINE__);
1080 if (fbt->fbtp_roffset != 0) {
1081 /* Only return type is available for args[1] in return probe. */
1086 /* Check if the requested argument doesn't exist. */
1090 /* Skip the return type and arguments up to the one requested. */
1094 if (fbt_type_name(&lc, *dp, desc->dtargd_native, sizeof(desc->dtargd_native)) > 0)
1095 desc->dtargd_ndx = ndx;
1101 fbt_linker_file_cb(linker_file_t lf, void *arg)
1104 fbt_provide_module(arg, lf);
1110 fbt_load(void *dummy)
1112 /* Create the /dev/dtrace/fbt entry. */
1113 fbt_cdev = make_dev(&fbt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
1116 /* Default the probe table size if not specified. */
1117 if (fbt_probetab_size == 0)
1118 fbt_probetab_size = FBT_PROBETAB_SIZE;
1120 /* Choose the hash mask for the probe table. */
1121 fbt_probetab_mask = fbt_probetab_size - 1;
1123 /* Allocate memory for the probe table. */
1125 malloc(fbt_probetab_size * sizeof (fbt_probe_t *), M_FBT, M_WAITOK | M_ZERO);
1127 dtrace_doubletrap_func = fbt_doubletrap;
1128 dtrace_invop_add(fbt_invop);
1130 if (dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_USER,
1131 NULL, &fbt_pops, NULL, &fbt_id) != 0)
1134 /* Create probes for the kernel and already-loaded modules. */
1135 linker_file_foreach(fbt_linker_file_cb, NULL);
1143 /* De-register the invalid opcode handler. */
1144 dtrace_invop_remove(fbt_invop);
1146 dtrace_doubletrap_func = NULL;
1148 /* De-register this DTrace provider. */
1149 if ((error = dtrace_unregister(fbt_id)) != 0)
1152 /* Free the probe table. */
1153 free(fbt_probetab, M_FBT);
1154 fbt_probetab = NULL;
1155 fbt_probetab_mask = 0;
1157 destroy_dev(fbt_cdev);
1163 fbt_modevent(module_t mod __unused, int type, void *data __unused)
1187 fbt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
1192 SYSINIT(fbt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_load, NULL);
1193 SYSUNINIT(fbt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_unload, NULL);
1195 DEV_MODULE(fbt, fbt_modevent, NULL);
1196 MODULE_VERSION(fbt, 1);
1197 MODULE_DEPEND(fbt, dtrace, 1, 1, 1);
1198 MODULE_DEPEND(fbt, opensolaris, 1, 1, 1);