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/fcntl.h>
38 #include <sys/filio.h>
40 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
52 #include <sys/selinfo.h>
54 #include <sys/syscall.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
58 #include <sys/unistd.h>
59 #include <machine/stdarg.h>
61 #include <sys/dtrace.h>
62 #include <sys/dtrace_bsd.h>
66 MALLOC_DEFINE(M_FBT, "fbt", "Function Boundary Tracing");
68 dtrace_provider_id_t fbt_id;
69 fbt_probe_t **fbt_probetab;
70 int fbt_probetab_mask;
72 static d_open_t fbt_open;
73 static int fbt_unload(void);
74 static void fbt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
75 static void fbt_provide_module(void *, modctl_t *);
76 static void fbt_destroy(void *, dtrace_id_t, void *);
77 static void fbt_enable(void *, dtrace_id_t, void *);
78 static void fbt_disable(void *, dtrace_id_t, void *);
79 static void fbt_load(void *);
80 static void fbt_suspend(void *, dtrace_id_t, void *);
81 static void fbt_resume(void *, dtrace_id_t, void *);
83 static struct cdevsw fbt_cdevsw = {
84 .d_version = D_VERSION,
89 static dtrace_pattr_t fbt_attr = {
90 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
91 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
92 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
93 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
94 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
97 static dtrace_pops_t fbt_pops = {
99 .dtps_provide_module = fbt_provide_module,
100 .dtps_enable = fbt_enable,
101 .dtps_disable = fbt_disable,
102 .dtps_suspend = fbt_suspend,
103 .dtps_resume = fbt_resume,
104 .dtps_getargdesc = fbt_getargdesc,
105 .dtps_getargval = NULL,
106 .dtps_usermode = NULL,
107 .dtps_destroy = fbt_destroy
110 static struct cdev *fbt_cdev;
111 static int fbt_probetab_size;
112 static int fbt_verbose = 0;
115 fbt_excluded(const char *name)
118 if (strncmp(name, "dtrace_", 7) == 0 &&
119 strncmp(name, "dtrace_safe_", 12) != 0) {
121 * Anything beginning with "dtrace_" may be called
122 * from probe context unless it explicitly indicates
123 * that it won't be called from probe context by
124 * using the prefix "dtrace_safe_".
130 * Lock owner methods may be called from probe context.
132 if (strcmp(name, "owner_mtx") == 0 ||
133 strcmp(name, "owner_rm") == 0 ||
134 strcmp(name, "owner_rw") == 0 ||
135 strcmp(name, "owner_sx") == 0)
139 * When DTrace is built into the kernel we need to exclude
140 * the FBT functions from instrumentation.
143 if (strncmp(name, "fbt_", 4) == 0)
156 for (i = 0; i < fbt_probetab_size; i++) {
157 fbt = fbt_probetab[i];
159 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
160 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
165 fbt_provide_module(void *arg, modctl_t *lf)
167 char modname[MAXPATHLEN];
171 strlcpy(modname, lf->filename, sizeof(modname));
172 len = strlen(modname);
173 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
174 modname[len - 3] = '\0';
177 * Employees of dtrace and their families are ineligible. Void
180 if (strcmp(modname, "dtrace") == 0)
184 * To register with DTrace, a module must list 'dtrace' as a
185 * dependency in order for the kernel linker to resolve
186 * symbols like dtrace_register(). All modules with such a
187 * dependency are ineligible for FBT tracing.
189 for (i = 0; i < lf->ndeps; i++)
190 if (strncmp(lf->deps[i]->filename, "dtrace", 6) == 0)
193 if (lf->fbt_nentries) {
195 * This module has some FBT entries allocated; we're afraid
202 * List the functions in the module and the symbol values.
204 (void) linker_file_function_listall(lf, fbt_provide_module_function, modname);
208 fbt_destroy_one(fbt_probe_t *fbt)
210 fbt_probe_t *hash, *hashprev, *next;
213 ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
214 for (hash = fbt_probetab[ndx], hashprev = NULL; hash != NULL;
215 hashprev = hash, hash = hash->fbtp_hashnext) {
217 if ((next = fbt->fbtp_tracenext) != NULL)
218 next->fbtp_hashnext = hash->fbtp_hashnext;
220 next = hash->fbtp_hashnext;
221 if (hashprev != NULL)
222 hashprev->fbtp_hashnext = next;
224 fbt_probetab[ndx] = next;
226 } else if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
227 for (next = hash; next->fbtp_tracenext != NULL;
228 next = next->fbtp_tracenext) {
229 if (fbt == next->fbtp_tracenext) {
230 next->fbtp_tracenext =
237 panic("probe %p not found in hash table", fbt);
243 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
245 fbt_probe_t *fbt = parg, *next;
252 next = fbt->fbtp_probenext;
253 fbt_destroy_one(fbt);
255 } while (fbt != NULL);
259 fbt_enable(void *arg, dtrace_id_t id, void *parg)
261 fbt_probe_t *fbt = parg;
262 modctl_t *ctl = fbt->fbtp_ctl;
267 * Now check that our modctl has the expected load count. If it
268 * doesn't, this module must have been unloaded and reloaded -- and
269 * we're not going to touch it.
271 if (ctl->loadcnt != fbt->fbtp_loadcnt) {
273 printf("fbt is failing for probe %s "
274 "(module %s reloaded)",
275 fbt->fbtp_name, ctl->filename);
281 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
282 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
288 fbt_disable(void *arg, dtrace_id_t id, void *parg)
290 fbt_probe_t *fbt = parg, *hash;
291 modctl_t *ctl = fbt->fbtp_ctl;
293 ASSERT(ctl->nenabled > 0);
296 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
299 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
302 for (hash = fbt_probetab[FBT_ADDR2NDX(fbt->fbtp_patchpoint)];
303 hash != NULL; hash = hash->fbtp_hashnext) {
304 if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
305 for (; hash != NULL; hash = hash->fbtp_tracenext)
306 if (hash->fbtp_enabled > 0)
312 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
317 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
319 fbt_probe_t *fbt = parg;
320 modctl_t *ctl = fbt->fbtp_ctl;
322 ASSERT(ctl->nenabled > 0);
324 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
327 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
328 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
332 fbt_resume(void *arg, dtrace_id_t id, void *parg)
334 fbt_probe_t *fbt = parg;
335 modctl_t *ctl = fbt->fbtp_ctl;
337 ASSERT(ctl->nenabled > 0);
339 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
342 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
343 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
347 fbt_ctfoff_init(modctl_t *lf, linker_ctf_t *lc)
349 const Elf_Sym *symp = lc->symtab;;
350 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
351 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
354 uint32_t objtoff = hp->cth_objtoff;
355 uint32_t funcoff = hp->cth_funcoff;
360 if (hp->cth_magic != CTF_MAGIC) {
361 printf("Bad magic value in CTF data of '%s'\n",lf->pathname);
365 if (lc->symtab == NULL) {
366 printf("No symbol table in '%s'\n",lf->pathname);
370 ctfoff = malloc(sizeof(uint32_t) * lc->nsym, M_LINKER, M_WAITOK);
371 *lc->ctfoffp = ctfoff;
373 for (i = 0; i < lc->nsym; i++, ctfoff++, symp++) {
374 if (symp->st_name == 0 || symp->st_shndx == SHN_UNDEF) {
375 *ctfoff = 0xffffffff;
379 switch (ELF_ST_TYPE(symp->st_info)) {
381 if (objtoff >= hp->cth_funcoff ||
382 (symp->st_shndx == SHN_ABS && symp->st_value == 0)) {
383 *ctfoff = 0xffffffff;
388 objtoff += sizeof (ushort_t);
392 if (funcoff >= hp->cth_typeoff) {
393 *ctfoff = 0xffffffff;
399 info = *((const ushort_t *)(ctfdata + funcoff));
400 vlen = CTF_INFO_VLEN(info);
403 * If we encounter a zero pad at the end, just skip it.
404 * Otherwise skip over the function and its return type
405 * (+2) and the argument list (vlen).
407 if (CTF_INFO_KIND(info) == CTF_K_UNKNOWN && vlen == 0)
408 funcoff += sizeof (ushort_t); /* skip pad */
410 funcoff += sizeof (ushort_t) * (vlen + 2);
414 *ctfoff = 0xffffffff;
423 fbt_get_ctt_size(uint8_t version, const ctf_type_t *tp, ssize_t *sizep,
426 ssize_t size, increment;
428 if (version > CTF_VERSION_1 &&
429 tp->ctt_size == CTF_LSIZE_SENT) {
430 size = CTF_TYPE_LSIZE(tp);
431 increment = sizeof (ctf_type_t);
434 increment = sizeof (ctf_stype_t);
440 *incrementp = increment;
446 fbt_typoff_init(linker_ctf_t *lc)
448 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
449 const ctf_type_t *tbuf;
450 const ctf_type_t *tend;
451 const ctf_type_t *tp;
452 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
455 ulong_t pop[CTF_K_MAX + 1] = { 0 };
459 if (hp->cth_magic != CTF_MAGIC)
462 tbuf = (const ctf_type_t *) (ctfdata + hp->cth_typeoff);
463 tend = (const ctf_type_t *) (ctfdata + hp->cth_stroff);
465 int child = hp->cth_parname != 0;
468 * We make two passes through the entire type section. In this first
469 * pass, we count the number of each type and the total number of types.
471 for (tp = tbuf; tp < tend; ctf_typemax++) {
472 ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
473 ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
474 ssize_t size, increment;
479 (void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);
484 vbytes = sizeof (uint_t);
487 vbytes = sizeof (ctf_array_t);
490 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
494 if (size < CTF_LSTRUCT_THRESH) {
495 ctf_member_t *mp = (ctf_member_t *)
496 ((uintptr_t)tp + increment);
498 vbytes = sizeof (ctf_member_t) * vlen;
499 for (n = vlen; n != 0; n--, mp++)
500 child |= CTF_TYPE_ISCHILD(mp->ctm_type);
502 ctf_lmember_t *lmp = (ctf_lmember_t *)
503 ((uintptr_t)tp + increment);
505 vbytes = sizeof (ctf_lmember_t) * vlen;
506 for (n = vlen; n != 0; n--, lmp++)
508 CTF_TYPE_ISCHILD(lmp->ctlm_type);
512 vbytes = sizeof (ctf_enum_t) * vlen;
516 * For forward declarations, ctt_type is the CTF_K_*
517 * kind for the tag, so bump that population count too.
518 * If ctt_type is unknown, treat the tag as a struct.
520 if (tp->ctt_type == CTF_K_UNKNOWN ||
521 tp->ctt_type >= CTF_K_MAX)
534 child |= CTF_TYPE_ISCHILD(tp->ctt_type);
538 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
541 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
545 /* account for a sentinel value below */
547 *lc->typlenp = ctf_typemax;
549 xp = malloc(sizeof(uint32_t) * ctf_typemax, M_LINKER,
554 /* type id 0 is used as a sentinel value */
558 * In the second pass, fill in the type offset.
560 for (tp = tbuf; tp < tend; xp++) {
561 ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
562 ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
563 ssize_t size, increment;
568 (void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);
573 vbytes = sizeof (uint_t);
576 vbytes = sizeof (ctf_array_t);
579 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
583 if (size < CTF_LSTRUCT_THRESH) {
584 ctf_member_t *mp = (ctf_member_t *)
585 ((uintptr_t)tp + increment);
587 vbytes = sizeof (ctf_member_t) * vlen;
588 for (n = vlen; n != 0; n--, mp++)
589 child |= CTF_TYPE_ISCHILD(mp->ctm_type);
591 ctf_lmember_t *lmp = (ctf_lmember_t *)
592 ((uintptr_t)tp + increment);
594 vbytes = sizeof (ctf_lmember_t) * vlen;
595 for (n = vlen; n != 0; n--, lmp++)
597 CTF_TYPE_ISCHILD(lmp->ctlm_type);
601 vbytes = sizeof (ctf_enum_t) * vlen;
615 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
618 *xp = (uint32_t)((uintptr_t) tp - (uintptr_t) ctfdata);
619 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
626 * CTF Declaration Stack
628 * In order to implement ctf_type_name(), we must convert a type graph back
629 * into a C type declaration. Unfortunately, a type graph represents a storage
630 * class ordering of the type whereas a type declaration must obey the C rules
631 * for operator precedence, and the two orderings are frequently in conflict.
632 * For example, consider these CTF type graphs and their C declarations:
634 * CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
635 * CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
637 * In each case, parentheses are used to raise operator * to higher lexical
638 * precedence, so the string form of the C declaration cannot be constructed by
639 * walking the type graph links and forming the string from left to right.
641 * The functions in this file build a set of stacks from the type graph nodes
642 * corresponding to the C operator precedence levels in the appropriate order.
643 * The code in ctf_type_name() can then iterate over the levels and nodes in
644 * lexical precedence order and construct the final C declaration string.
646 typedef struct ctf_list {
647 struct ctf_list *l_prev; /* previous pointer or tail pointer */
648 struct ctf_list *l_next; /* next pointer or head pointer */
651 #define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev))
652 #define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next))
662 typedef struct ctf_decl_node {
663 ctf_list_t cd_list; /* linked list pointers */
664 ctf_id_t cd_type; /* type identifier */
665 uint_t cd_kind; /* type kind */
666 uint_t cd_n; /* type dimension if array */
669 typedef struct ctf_decl {
670 ctf_list_t cd_nodes[CTF_PREC_MAX]; /* declaration node stacks */
671 int cd_order[CTF_PREC_MAX]; /* storage order of decls */
672 ctf_decl_prec_t cd_qualp; /* qualifier precision */
673 ctf_decl_prec_t cd_ordp; /* ordered precision */
674 char *cd_buf; /* buffer for output */
675 char *cd_ptr; /* buffer location */
676 char *cd_end; /* buffer limit */
677 size_t cd_len; /* buffer space required */
678 int cd_err; /* saved error value */
682 * Simple doubly-linked list append routine. This implementation assumes that
683 * each list element contains an embedded ctf_list_t as the first member.
684 * An additional ctf_list_t is used to store the head (l_next) and tail
685 * (l_prev) pointers. The current head and tail list elements have their
686 * previous and next pointers set to NULL, respectively.
689 ctf_list_append(ctf_list_t *lp, void *new)
691 ctf_list_t *p = lp->l_prev; /* p = tail list element */
692 ctf_list_t *q = new; /* q = new list element */
705 * Prepend the specified existing element to the given ctf_list_t. The
706 * existing pointer should be pointing at a struct with embedded ctf_list_t.
709 ctf_list_prepend(ctf_list_t *lp, void *new)
711 ctf_list_t *p = new; /* p = new list element */
712 ctf_list_t *q = lp->l_next; /* q = head list element */
725 ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)
729 bzero(cd, sizeof (ctf_decl_t));
731 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
732 cd->cd_order[i] = CTF_PREC_BASE - 1;
734 cd->cd_qualp = CTF_PREC_BASE;
735 cd->cd_ordp = CTF_PREC_BASE;
739 cd->cd_end = buf + len;
743 ctf_decl_fini(ctf_decl_t *cd)
745 ctf_decl_node_t *cdp, *ndp;
748 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {
749 for (cdp = ctf_list_next(&cd->cd_nodes[i]);
750 cdp != NULL; cdp = ndp) {
751 ndp = ctf_list_next(cdp);
757 static const ctf_type_t *
758 ctf_lookup_by_id(linker_ctf_t *lc, ctf_id_t type)
760 const ctf_type_t *tp;
762 uint32_t *typoff = *lc->typoffp;
764 if (type >= *lc->typlenp) {
765 printf("%s(%d): type %d exceeds max %ld\n",__func__,__LINE__,(int) type,*lc->typlenp);
769 /* Check if the type isn't cross-referenced. */
770 if ((offset = typoff[type]) == 0) {
771 printf("%s(%d): type %d isn't cross referenced\n",__func__,__LINE__, (int) type);
775 tp = (const ctf_type_t *)(lc->ctftab + offset + sizeof(ctf_header_t));
781 fbt_array_info(linker_ctf_t *lc, ctf_id_t type, ctf_arinfo_t *arp)
783 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
784 const ctf_type_t *tp;
785 const ctf_array_t *ap;
788 bzero(arp, sizeof(*arp));
790 if ((tp = ctf_lookup_by_id(lc, type)) == NULL)
793 if (CTF_INFO_KIND(tp->ctt_info) != CTF_K_ARRAY)
796 (void) fbt_get_ctt_size(hp->cth_version, tp, NULL, &increment);
798 ap = (const ctf_array_t *)((uintptr_t)tp + increment);
799 arp->ctr_contents = ap->cta_contents;
800 arp->ctr_index = ap->cta_index;
801 arp->ctr_nelems = ap->cta_nelems;
805 ctf_strptr(linker_ctf_t *lc, int name)
807 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;;
808 const char *strp = "";
810 if (name < 0 || name >= hp->cth_strlen)
813 strp = (const char *)(lc->ctftab + hp->cth_stroff + name + sizeof(ctf_header_t));
819 ctf_decl_push(ctf_decl_t *cd, linker_ctf_t *lc, ctf_id_t type)
821 ctf_decl_node_t *cdp;
822 ctf_decl_prec_t prec;
826 const ctf_type_t *tp;
829 if ((tp = ctf_lookup_by_id(lc, type)) == NULL) {
834 switch (kind = CTF_INFO_KIND(tp->ctt_info)) {
836 fbt_array_info(lc, type, &ar);
837 ctf_decl_push(cd, lc, ar.ctr_contents);
839 prec = CTF_PREC_ARRAY;
843 if (ctf_strptr(lc, tp->ctt_name)[0] == '\0') {
844 ctf_decl_push(cd, lc, tp->ctt_type);
847 prec = CTF_PREC_BASE;
851 ctf_decl_push(cd, lc, tp->ctt_type);
852 prec = CTF_PREC_FUNCTION;
856 ctf_decl_push(cd, lc, tp->ctt_type);
857 prec = CTF_PREC_POINTER;
863 ctf_decl_push(cd, lc, tp->ctt_type);
869 prec = CTF_PREC_BASE;
872 cdp = malloc(sizeof(*cdp), M_FBT, M_WAITOK);
877 if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)
878 cd->cd_order[prec] = cd->cd_ordp++;
881 * Reset cd_qualp to the highest precedence level that we've seen so
882 * far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).
884 if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
888 * C array declarators are ordered inside out so prepend them. Also by
889 * convention qualifiers of base types precede the type specifier (e.g.
890 * const int vs. int const) even though the two forms are equivalent.
892 if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))
893 ctf_list_prepend(&cd->cd_nodes[prec], cdp);
895 ctf_list_append(&cd->cd_nodes[prec], cdp);
899 ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)
901 size_t len = (size_t)(cd->cd_end - cd->cd_ptr);
905 va_start(ap, format);
906 n = vsnprintf(cd->cd_ptr, len, format, ap);
909 cd->cd_ptr += MIN(n, len);
914 fbt_type_name(linker_ctf_t *lc, ctf_id_t type, char *buf, size_t len)
917 ctf_decl_node_t *cdp;
918 ctf_decl_prec_t prec, lp, rp;
922 if (lc == NULL && type == CTF_ERR)
923 return (-1); /* simplify caller code by permitting CTF_ERR */
925 ctf_decl_init(&cd, buf, len);
926 ctf_decl_push(&cd, lc, type);
928 if (cd.cd_err != 0) {
934 * If the type graph's order conflicts with lexical precedence order
935 * for pointers or arrays, then we need to surround the declarations at
936 * the corresponding lexical precedence with parentheses. This can
937 * result in either a parenthesized pointer (*) as in int (*)() or
938 * int (*)[], or in a parenthesized pointer and array as in int (*[])().
940 ptr = cd.cd_order[CTF_PREC_POINTER] > CTF_PREC_POINTER;
941 arr = cd.cd_order[CTF_PREC_ARRAY] > CTF_PREC_ARRAY;
943 rp = arr ? CTF_PREC_ARRAY : ptr ? CTF_PREC_POINTER : -1;
944 lp = ptr ? CTF_PREC_POINTER : arr ? CTF_PREC_ARRAY : -1;
946 k = CTF_K_POINTER; /* avoid leading whitespace (see below) */
948 for (prec = CTF_PREC_BASE; prec < CTF_PREC_MAX; prec++) {
949 for (cdp = ctf_list_next(&cd.cd_nodes[prec]);
950 cdp != NULL; cdp = ctf_list_next(cdp)) {
952 const ctf_type_t *tp =
953 ctf_lookup_by_id(lc, cdp->cd_type);
954 const char *name = ctf_strptr(lc, tp->ctt_name);
956 if (k != CTF_K_POINTER && k != CTF_K_ARRAY)
957 ctf_decl_sprintf(&cd, " ");
960 ctf_decl_sprintf(&cd, "(");
964 switch (cdp->cd_kind) {
968 ctf_decl_sprintf(&cd, "%s", name);
971 ctf_decl_sprintf(&cd, "*");
974 ctf_decl_sprintf(&cd, "[%u]", cdp->cd_n);
977 ctf_decl_sprintf(&cd, "()");
981 ctf_decl_sprintf(&cd, "struct %s", name);
984 ctf_decl_sprintf(&cd, "union %s", name);
987 ctf_decl_sprintf(&cd, "enum %s", name);
990 ctf_decl_sprintf(&cd, "volatile");
993 ctf_decl_sprintf(&cd, "const");
996 ctf_decl_sprintf(&cd, "restrict");
1004 ctf_decl_sprintf(&cd, ")");
1012 fbt_getargdesc(void *arg __unused, dtrace_id_t id __unused, void *parg, dtrace_argdesc_t *desc)
1015 fbt_probe_t *fbt = parg;
1017 modctl_t *ctl = fbt->fbtp_ctl;
1018 int ndx = desc->dtargd_ndx;
1019 int symindx = fbt->fbtp_symindx;
1022 ushort_t info, kind, n;
1024 if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
1025 (void) strcpy(desc->dtargd_native, "int");
1029 desc->dtargd_ndx = DTRACE_ARGNONE;
1031 /* Get a pointer to the CTF data and it's length. */
1032 if (linker_ctf_get(ctl, &lc) != 0)
1033 /* No CTF data? Something wrong? *shrug* */
1036 /* Check if this module hasn't been initialised yet. */
1037 if (*lc.ctfoffp == NULL) {
1039 * Initialise the CTF object and function symindx to
1040 * byte offset array.
1042 if (fbt_ctfoff_init(ctl, &lc) != 0)
1045 /* Initialise the CTF type to byte offset array. */
1046 if (fbt_typoff_init(&lc) != 0)
1050 ctfoff = *lc.ctfoffp;
1052 if (ctfoff == NULL || *lc.typoffp == NULL)
1055 /* Check if the symbol index is out of range. */
1056 if (symindx >= lc.nsym)
1059 /* Check if the symbol isn't cross-referenced. */
1060 if ((offset = ctfoff[symindx]) == 0xffffffff)
1063 dp = (const ushort_t *)(lc.ctftab + offset + sizeof(ctf_header_t));
1066 kind = CTF_INFO_KIND(info);
1067 n = CTF_INFO_VLEN(info);
1069 if (kind == CTF_K_UNKNOWN && n == 0) {
1070 printf("%s(%d): Unknown function!\n",__func__,__LINE__);
1074 if (kind != CTF_K_FUNCTION) {
1075 printf("%s(%d): Expected a function!\n",__func__,__LINE__);
1079 if (fbt->fbtp_roffset != 0) {
1080 /* Only return type is available for args[1] in return probe. */
1085 /* Check if the requested argument doesn't exist. */
1089 /* Skip the return type and arguments up to the one requested. */
1093 if (fbt_type_name(&lc, *dp, desc->dtargd_native, sizeof(desc->dtargd_native)) > 0)
1094 desc->dtargd_ndx = ndx;
1100 fbt_linker_file_cb(linker_file_t lf, void *arg)
1103 fbt_provide_module(arg, lf);
1109 fbt_load(void *dummy)
1111 /* Create the /dev/dtrace/fbt entry. */
1112 fbt_cdev = make_dev(&fbt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
1115 /* Default the probe table size if not specified. */
1116 if (fbt_probetab_size == 0)
1117 fbt_probetab_size = FBT_PROBETAB_SIZE;
1119 /* Choose the hash mask for the probe table. */
1120 fbt_probetab_mask = fbt_probetab_size - 1;
1122 /* Allocate memory for the probe table. */
1124 malloc(fbt_probetab_size * sizeof (fbt_probe_t *), M_FBT, M_WAITOK | M_ZERO);
1126 dtrace_doubletrap_func = fbt_doubletrap;
1127 dtrace_invop_add(fbt_invop);
1129 if (dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_USER,
1130 NULL, &fbt_pops, NULL, &fbt_id) != 0)
1133 /* Create probes for the kernel and already-loaded modules. */
1134 linker_file_foreach(fbt_linker_file_cb, NULL);
1142 /* De-register the invalid opcode handler. */
1143 dtrace_invop_remove(fbt_invop);
1145 dtrace_doubletrap_func = NULL;
1147 /* De-register this DTrace provider. */
1148 if ((error = dtrace_unregister(fbt_id)) != 0)
1151 /* Free the probe table. */
1152 free(fbt_probetab, M_FBT);
1153 fbt_probetab = NULL;
1154 fbt_probetab_mask = 0;
1156 destroy_dev(fbt_cdev);
1162 fbt_modevent(module_t mod __unused, int type, void *data __unused)
1186 fbt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
1191 SYSINIT(fbt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_load, NULL);
1192 SYSUNINIT(fbt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_unload, NULL);
1194 DEV_MODULE(fbt, fbt_modevent, NULL);
1195 MODULE_VERSION(fbt, 1);
1196 MODULE_DEPEND(fbt, dtrace, 1, 1, 1);
1197 MODULE_DEPEND(fbt, opensolaris, 1, 1, 1);