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 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 dtrace_pattr_t fbt_attr = {
84 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
85 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
86 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
87 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
88 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
91 static dtrace_pops_t fbt_pops = {
93 .dtps_provide_module = fbt_provide_module,
94 .dtps_enable = fbt_enable,
95 .dtps_disable = fbt_disable,
96 .dtps_suspend = fbt_suspend,
97 .dtps_resume = fbt_resume,
98 .dtps_getargdesc = fbt_getargdesc,
99 .dtps_getargval = NULL,
100 .dtps_usermode = NULL,
101 .dtps_destroy = fbt_destroy
104 static int fbt_probetab_size;
105 static int fbt_verbose = 0;
108 fbt_excluded(const char *name)
111 if (strncmp(name, "dtrace_", 7) == 0 &&
112 strncmp(name, "dtrace_safe_", 12) != 0) {
114 * Anything beginning with "dtrace_" may be called
115 * from probe context unless it explicitly indicates
116 * that it won't be called from probe context by
117 * using the prefix "dtrace_safe_".
123 * Omit instrumentation of functions that are probably in DDB. It
124 * makes it too hard to debug broken FBT.
126 * NB: kdb_enter() can be excluded, but its call to printf() can't be.
127 * This is generally OK since we're not yet in debugging context.
129 if (strncmp(name, "db_", 3) == 0 ||
130 strncmp(name, "kdb_", 4) == 0)
134 * Lock owner methods may be called from probe context.
136 if (strcmp(name, "owner_mtx") == 0 ||
137 strcmp(name, "owner_rm") == 0 ||
138 strcmp(name, "owner_rw") == 0 ||
139 strcmp(name, "owner_sx") == 0)
143 * Stack unwinders may be called from probe context on some
146 #if defined(__aarch64__) || defined(__riscv)
147 if (strcmp(name, "unwind_frame") == 0)
152 * When DTrace is built into the kernel we need to exclude
153 * the FBT functions from instrumentation.
156 if (strncmp(name, "fbt_", 4) == 0)
169 for (i = 0; i < fbt_probetab_size; i++) {
170 fbt = fbt_probetab[i];
172 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
173 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
178 fbt_provide_module(void *arg, modctl_t *lf)
180 char modname[MAXPATHLEN];
184 strlcpy(modname, lf->filename, sizeof(modname));
185 len = strlen(modname);
186 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
187 modname[len - 3] = '\0';
190 * Employees of dtrace and their families are ineligible. Void
193 if (strcmp(modname, "dtrace") == 0)
197 * To register with DTrace, a module must list 'dtrace' as a
198 * dependency in order for the kernel linker to resolve
199 * symbols like dtrace_register(). All modules with such a
200 * dependency are ineligible for FBT tracing.
202 for (i = 0; i < lf->ndeps; i++)
203 if (strncmp(lf->deps[i]->filename, "dtrace", 6) == 0)
206 if (lf->fbt_nentries) {
208 * This module has some FBT entries allocated; we're afraid
215 * List the functions in the module and the symbol values.
217 (void) linker_file_function_listall(lf, fbt_provide_module_function, modname);
221 fbt_destroy_one(fbt_probe_t *fbt)
223 fbt_probe_t *hash, *hashprev, *next;
226 ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
227 for (hash = fbt_probetab[ndx], hashprev = NULL; hash != NULL;
228 hashprev = hash, hash = hash->fbtp_hashnext) {
230 if ((next = fbt->fbtp_tracenext) != NULL)
231 next->fbtp_hashnext = hash->fbtp_hashnext;
233 next = hash->fbtp_hashnext;
234 if (hashprev != NULL)
235 hashprev->fbtp_hashnext = next;
237 fbt_probetab[ndx] = next;
239 } else if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
240 for (next = hash; next->fbtp_tracenext != NULL;
241 next = next->fbtp_tracenext) {
242 if (fbt == next->fbtp_tracenext) {
243 next->fbtp_tracenext =
250 panic("probe %p not found in hash table", fbt);
256 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
258 fbt_probe_t *fbt = parg, *next;
265 next = fbt->fbtp_probenext;
266 fbt_destroy_one(fbt);
268 } while (fbt != NULL);
272 fbt_enable(void *arg, dtrace_id_t id, void *parg)
274 fbt_probe_t *fbt = parg;
275 modctl_t *ctl = fbt->fbtp_ctl;
280 * Now check that our modctl has the expected load count. If it
281 * doesn't, this module must have been unloaded and reloaded -- and
282 * we're not going to touch it.
284 if (ctl->loadcnt != fbt->fbtp_loadcnt) {
286 printf("fbt is failing for probe %s "
287 "(module %s reloaded)",
288 fbt->fbtp_name, ctl->filename);
294 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
295 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
301 fbt_disable(void *arg, dtrace_id_t id, void *parg)
303 fbt_probe_t *fbt = parg, *hash;
304 modctl_t *ctl = fbt->fbtp_ctl;
306 ASSERT(ctl->nenabled > 0);
309 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
312 for (; fbt != NULL; fbt = fbt->fbtp_probenext) {
315 for (hash = fbt_probetab[FBT_ADDR2NDX(fbt->fbtp_patchpoint)];
316 hash != NULL; hash = hash->fbtp_hashnext) {
317 if (hash->fbtp_patchpoint == fbt->fbtp_patchpoint) {
318 for (; hash != NULL; hash = hash->fbtp_tracenext)
319 if (hash->fbtp_enabled > 0)
325 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
330 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
332 fbt_probe_t *fbt = parg;
333 modctl_t *ctl = fbt->fbtp_ctl;
335 ASSERT(ctl->nenabled > 0);
337 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
340 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
341 fbt_patch_tracepoint(fbt, fbt->fbtp_savedval);
345 fbt_resume(void *arg, dtrace_id_t id, void *parg)
347 fbt_probe_t *fbt = parg;
348 modctl_t *ctl = fbt->fbtp_ctl;
350 ASSERT(ctl->nenabled > 0);
352 if ((ctl->loadcnt != fbt->fbtp_loadcnt))
355 for (; fbt != NULL; fbt = fbt->fbtp_probenext)
356 fbt_patch_tracepoint(fbt, fbt->fbtp_patchval);
360 fbt_ctfoff_init(modctl_t *lf, linker_ctf_t *lc)
362 const Elf_Sym *symp = lc->symtab;
363 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
364 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
368 uint32_t objtoff = hp->cth_objtoff;
369 uint32_t funcoff = hp->cth_funcoff;
370 uint_t kind, info, vlen;
373 if (hp->cth_magic != CTF_MAGIC) {
374 printf("Bad magic value in CTF data of '%s'\n",lf->pathname);
378 if (lc->symtab == NULL) {
379 printf("No symbol table in '%s'\n",lf->pathname);
383 ctfoff = malloc(sizeof(uint32_t) * lc->nsym, M_LINKER, M_WAITOK);
384 *lc->ctfoffp = ctfoff;
386 idwidth = hp->cth_version == CTF_VERSION_2 ? 2 : 4;
388 for (i = 0; i < lc->nsym; i++, ctfoff++, symp++) {
389 if (symp->st_name == 0 || symp->st_shndx == SHN_UNDEF) {
390 *ctfoff = 0xffffffff;
394 switch (ELF_ST_TYPE(symp->st_info)) {
396 if (objtoff >= hp->cth_funcoff ||
397 (symp->st_shndx == SHN_ABS && symp->st_value == 0)) {
398 *ctfoff = 0xffffffff;
407 if (funcoff >= hp->cth_typeoff) {
408 *ctfoff = 0xffffffff;
415 memcpy(&info, ctfdata + funcoff, idwidth);
416 if (hp->cth_version == CTF_VERSION_2) {
417 kind = CTF_V2_INFO_KIND(info);
418 vlen = CTF_V2_INFO_VLEN(info);
420 kind = CTF_V3_INFO_KIND(info);
421 vlen = CTF_V3_INFO_VLEN(info);
425 * If we encounter a zero pad at the end, just skip it.
426 * Otherwise skip over the function and its return type
427 * (+2) and the argument list (vlen).
429 if (kind == CTF_K_UNKNOWN && vlen == 0)
432 funcoff += idwidth * (vlen + 2);
436 *ctfoff = 0xffffffff;
445 fbt_get_ctt_index(uint8_t version, const void *v, uint_t *indexp,
446 uint_t *typep, int *ischildp)
451 if (version == CTF_VERSION_2) {
452 const struct ctf_type_v2 *ctt = v;
454 type = ctt->ctt_type;
455 index = CTF_V2_TYPE_TO_INDEX(ctt->ctt_type);
456 ischild = CTF_V2_TYPE_ISCHILD(ctt->ctt_type);
458 const struct ctf_type_v3 *ctt = v;
460 type = ctt->ctt_type;
461 index = CTF_V3_TYPE_TO_INDEX(ctt->ctt_type);
462 ischild = CTF_V3_TYPE_ISCHILD(ctt->ctt_type);
469 if (ischildp != NULL)
474 fbt_get_ctt_size(uint8_t version, const void *tp, ssize_t *sizep,
477 ssize_t size, increment;
479 if (version == CTF_VERSION_2) {
480 const struct ctf_type_v2 *ctt = tp;
482 if (ctt->ctt_size == CTF_V2_LSIZE_SENT) {
483 size = CTF_TYPE_LSIZE(ctt);
484 increment = sizeof (struct ctf_type_v2);
486 size = ctt->ctt_size;
487 increment = sizeof (struct ctf_stype_v2);
490 const struct ctf_type_v3 *ctt = tp;
492 if (ctt->ctt_size == CTF_V3_LSIZE_SENT) {
493 size = CTF_TYPE_LSIZE(ctt);
494 increment = sizeof (struct ctf_type_v3);
496 size = ctt->ctt_size;
497 increment = sizeof (struct ctf_stype_v3);
504 *incrementp = increment;
510 fbt_get_ctt_info(uint8_t version, const void *tp, uint_t *kindp, uint_t *vlenp,
516 if (version == CTF_VERSION_2) {
517 const struct ctf_type_v2 *ctt = tp;
519 kind = CTF_V2_INFO_KIND(ctt->ctt_info);
520 vlen = CTF_V2_INFO_VLEN(ctt->ctt_info);
521 isroot = CTF_V2_INFO_ISROOT(ctt->ctt_info);
523 const struct ctf_type_v3 *ctt = tp;
525 kind = CTF_V3_INFO_KIND(ctt->ctt_info);
526 vlen = CTF_V3_INFO_VLEN(ctt->ctt_info);
527 isroot = CTF_V3_INFO_ISROOT(ctt->ctt_info);
539 fbt_typoff_init(linker_ctf_t *lc)
541 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
542 const void *tbuf, *tend, *tp;
543 const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
547 ulong_t pop[CTF_K_MAX + 1] = { 0 };
551 if (hp->cth_magic != CTF_MAGIC)
554 version = hp->cth_version;
555 idwidth = version == CTF_VERSION_2 ? 2 : 4;
557 tbuf = (const void *) (ctfdata + hp->cth_typeoff);
558 tend = (const void *) (ctfdata + hp->cth_stroff);
561 * We make two passes through the entire type section. In this first
562 * pass, we count the number of each type and the total number of types.
564 for (tp = tbuf; tp < tend; ctf_typemax++) {
565 uint_t kind, type, vlen;
566 ssize_t size, increment;
569 (void) fbt_get_ctt_size(version, tp, &size, &increment);
570 fbt_get_ctt_info(version, tp, &kind, &vlen, NULL);
571 fbt_get_ctt_index(version, tp, NULL, &type, NULL);
576 vbytes = sizeof (uint_t);
579 if (version == CTF_VERSION_2)
580 vbytes = sizeof (struct ctf_array_v2);
582 vbytes = sizeof (struct ctf_array_v3);
585 vbytes = roundup2(idwidth * vlen, sizeof(uint32_t));
589 if (version == CTF_VERSION_2) {
590 if (size < CTF_V2_LSTRUCT_THRESH)
592 sizeof (struct ctf_member_v2) * vlen;
595 sizeof (struct ctf_lmember_v2) * vlen;
597 if (size < CTF_V3_LSTRUCT_THRESH)
599 sizeof (struct ctf_member_v3) * vlen;
602 sizeof (struct ctf_lmember_v3) * vlen;
606 vbytes = sizeof (ctf_enum_t) * vlen;
610 * For forward declarations, ctt_type is the CTF_K_*
611 * kind for the tag, so bump that population count too.
612 * If ctt_type is unknown, treat the tag as a struct.
614 if (type == CTF_K_UNKNOWN || type >= CTF_K_MAX)
630 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
633 tp = (const void *)((uintptr_t)tp + increment + vbytes);
637 /* account for a sentinel value below */
639 *lc->typlenp = ctf_typemax;
641 xp = malloc(sizeof(uint32_t) * ctf_typemax, M_LINKER,
646 /* type id 0 is used as a sentinel value */
650 * In the second pass, fill in the type offset.
652 for (tp = tbuf; tp < tend; xp++) {
653 ssize_t size, increment;
658 (void) fbt_get_ctt_size(version, tp, &size, &increment);
659 fbt_get_ctt_info(version, tp, &kind, &vlen, NULL);
664 vbytes = sizeof (uint_t);
667 if (version == CTF_VERSION_2)
668 vbytes = sizeof (struct ctf_array_v2);
670 vbytes = sizeof (struct ctf_array_v3);
673 vbytes = roundup2(idwidth * vlen, sizeof(uint32_t));
677 if (version == CTF_VERSION_2) {
678 if (size < CTF_V2_LSTRUCT_THRESH)
680 sizeof (struct ctf_member_v2) * vlen;
683 sizeof (struct ctf_lmember_v2) * vlen;
685 if (size < CTF_V3_LSTRUCT_THRESH)
687 sizeof (struct ctf_member_v3) * vlen;
690 sizeof (struct ctf_lmember_v3) * vlen;
694 vbytes = sizeof (ctf_enum_t) * vlen;
708 printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
711 *xp = (uint32_t)((uintptr_t) tp - (uintptr_t) ctfdata);
712 tp = (const void *)((uintptr_t)tp + increment + vbytes);
719 * CTF Declaration Stack
721 * In order to implement ctf_type_name(), we must convert a type graph back
722 * into a C type declaration. Unfortunately, a type graph represents a storage
723 * class ordering of the type whereas a type declaration must obey the C rules
724 * for operator precedence, and the two orderings are frequently in conflict.
725 * For example, consider these CTF type graphs and their C declarations:
727 * CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
728 * CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
730 * In each case, parentheses are used to raise operator * to higher lexical
731 * precedence, so the string form of the C declaration cannot be constructed by
732 * walking the type graph links and forming the string from left to right.
734 * The functions in this file build a set of stacks from the type graph nodes
735 * corresponding to the C operator precedence levels in the appropriate order.
736 * The code in ctf_type_name() can then iterate over the levels and nodes in
737 * lexical precedence order and construct the final C declaration string.
739 typedef struct ctf_list {
740 struct ctf_list *l_prev; /* previous pointer or tail pointer */
741 struct ctf_list *l_next; /* next pointer or head pointer */
744 #define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev))
745 #define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next))
755 typedef struct ctf_decl_node {
756 ctf_list_t cd_list; /* linked list pointers */
757 ctf_id_t cd_type; /* type identifier */
758 uint_t cd_kind; /* type kind */
759 uint_t cd_n; /* type dimension if array */
762 typedef struct ctf_decl {
763 ctf_list_t cd_nodes[CTF_PREC_MAX]; /* declaration node stacks */
764 int cd_order[CTF_PREC_MAX]; /* storage order of decls */
765 ctf_decl_prec_t cd_qualp; /* qualifier precision */
766 ctf_decl_prec_t cd_ordp; /* ordered precision */
767 char *cd_buf; /* buffer for output */
768 char *cd_ptr; /* buffer location */
769 char *cd_end; /* buffer limit */
770 size_t cd_len; /* buffer space required */
771 int cd_err; /* saved error value */
775 * Simple doubly-linked list append routine. This implementation assumes that
776 * each list element contains an embedded ctf_list_t as the first member.
777 * An additional ctf_list_t is used to store the head (l_next) and tail
778 * (l_prev) pointers. The current head and tail list elements have their
779 * previous and next pointers set to NULL, respectively.
782 ctf_list_append(ctf_list_t *lp, void *new)
784 ctf_list_t *p = lp->l_prev; /* p = tail list element */
785 ctf_list_t *q = new; /* q = new list element */
798 * Prepend the specified existing element to the given ctf_list_t. The
799 * existing pointer should be pointing at a struct with embedded ctf_list_t.
802 ctf_list_prepend(ctf_list_t *lp, void *new)
804 ctf_list_t *p = new; /* p = new list element */
805 ctf_list_t *q = lp->l_next; /* q = head list element */
818 ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)
822 bzero(cd, sizeof (ctf_decl_t));
824 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
825 cd->cd_order[i] = CTF_PREC_BASE - 1;
827 cd->cd_qualp = CTF_PREC_BASE;
828 cd->cd_ordp = CTF_PREC_BASE;
832 cd->cd_end = buf + len;
836 ctf_decl_fini(ctf_decl_t *cd)
838 ctf_decl_node_t *cdp, *ndp;
841 for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {
842 for (cdp = ctf_list_next(&cd->cd_nodes[i]);
843 cdp != NULL; cdp = ndp) {
844 ndp = ctf_list_next(cdp);
851 ctf_lookup_by_id(linker_ctf_t *lc, ctf_id_t type)
855 uint32_t *typoff = *lc->typoffp;
857 if (type >= *lc->typlenp) {
858 printf("%s(%d): type %d exceeds max %ld\n",__func__,__LINE__,(int) type,*lc->typlenp);
862 /* Check if the type isn't cross-referenced. */
863 if ((offset = typoff[type]) == 0) {
864 printf("%s(%d): type %d isn't cross referenced\n",__func__,__LINE__, (int) type);
868 tp = (const void *) (lc->ctftab + offset + sizeof(ctf_header_t));
874 fbt_array_info(linker_ctf_t *lc, ctf_id_t type, ctf_arinfo_t *arp)
876 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
881 bzero(arp, sizeof(*arp));
883 if ((tp = ctf_lookup_by_id(lc, type)) == NULL)
886 fbt_get_ctt_info(hp->cth_version, tp, &kind, NULL, NULL);
887 if (kind != CTF_K_ARRAY)
890 (void) fbt_get_ctt_size(hp->cth_version, tp, NULL, &increment);
892 if (hp->cth_version == CTF_VERSION_2) {
893 const struct ctf_array_v2 *ap;
895 ap = (const struct ctf_array_v2 *)((uintptr_t)tp + increment);
896 arp->ctr_contents = ap->cta_contents;
897 arp->ctr_index = ap->cta_index;
898 arp->ctr_nelems = ap->cta_nelems;
900 const struct ctf_array_v3 *ap;
902 ap = (const struct ctf_array_v3 *)((uintptr_t)tp + increment);
903 arp->ctr_contents = ap->cta_contents;
904 arp->ctr_index = ap->cta_index;
905 arp->ctr_nelems = ap->cta_nelems;
910 ctf_strptr(linker_ctf_t *lc, int name)
912 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
913 const char *strp = "";
915 if (name < 0 || name >= hp->cth_strlen)
918 strp = (const char *)(lc->ctftab + hp->cth_stroff + name + sizeof(ctf_header_t));
924 ctf_type_rname(linker_ctf_t *lc, const void *v)
926 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
929 if (hp->cth_version == CTF_VERSION_2) {
930 const struct ctf_type_v2 *ctt = v;
932 name = ctt->ctt_name;
934 const struct ctf_type_v3 *ctt = v;
936 name = ctt->ctt_name;
939 return (ctf_strptr(lc, name));
943 ctf_decl_push(ctf_decl_t *cd, linker_ctf_t *lc, ctf_id_t type)
945 const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
946 ctf_decl_node_t *cdp;
947 ctf_decl_prec_t prec;
948 uint_t kind, n = 1, t;
954 if ((tp = ctf_lookup_by_id(lc, type)) == NULL) {
959 fbt_get_ctt_info(hp->cth_version, tp, &kind, NULL, NULL);
960 fbt_get_ctt_index(hp->cth_version, tp, NULL, &t, NULL);
964 fbt_array_info(lc, type, &ar);
965 ctf_decl_push(cd, lc, ar.ctr_contents);
967 prec = CTF_PREC_ARRAY;
971 if (ctf_type_rname(lc, tp)[0] == '\0') {
972 ctf_decl_push(cd, lc, t);
975 prec = CTF_PREC_BASE;
979 ctf_decl_push(cd, lc, t);
980 prec = CTF_PREC_FUNCTION;
984 ctf_decl_push(cd, lc, t);
985 prec = CTF_PREC_POINTER;
991 ctf_decl_push(cd, lc, t);
997 prec = CTF_PREC_BASE;
1000 cdp = malloc(sizeof(*cdp), M_FBT, M_WAITOK);
1001 cdp->cd_type = type;
1002 cdp->cd_kind = kind;
1005 if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)
1006 cd->cd_order[prec] = cd->cd_ordp++;
1009 * Reset cd_qualp to the highest precedence level that we've seen so
1010 * far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).
1012 if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
1013 cd->cd_qualp = prec;
1016 * C array declarators are ordered inside out so prepend them. Also by
1017 * convention qualifiers of base types precede the type specifier (e.g.
1018 * const int vs. int const) even though the two forms are equivalent.
1020 if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))
1021 ctf_list_prepend(&cd->cd_nodes[prec], cdp);
1023 ctf_list_append(&cd->cd_nodes[prec], cdp);
1027 ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)
1029 size_t len = (size_t)(cd->cd_end - cd->cd_ptr);
1033 va_start(ap, format);
1034 n = vsnprintf(cd->cd_ptr, len, format, ap);
1037 cd->cd_ptr += MIN(n, len);
1042 fbt_type_name(linker_ctf_t *lc, ctf_id_t type, char *buf, size_t len)
1045 ctf_decl_node_t *cdp;
1046 ctf_decl_prec_t prec, lp, rp;
1050 if (lc == NULL && type == CTF_ERR)
1051 return (-1); /* simplify caller code by permitting CTF_ERR */
1053 ctf_decl_init(&cd, buf, len);
1054 ctf_decl_push(&cd, lc, type);
1056 if (cd.cd_err != 0) {
1062 * If the type graph's order conflicts with lexical precedence order
1063 * for pointers or arrays, then we need to surround the declarations at
1064 * the corresponding lexical precedence with parentheses. This can
1065 * result in either a parenthesized pointer (*) as in int (*)() or
1066 * int (*)[], or in a parenthesized pointer and array as in int (*[])().
1068 ptr = cd.cd_order[CTF_PREC_POINTER] > CTF_PREC_POINTER;
1069 arr = cd.cd_order[CTF_PREC_ARRAY] > CTF_PREC_ARRAY;
1071 rp = arr ? CTF_PREC_ARRAY : ptr ? CTF_PREC_POINTER : -1;
1072 lp = ptr ? CTF_PREC_POINTER : arr ? CTF_PREC_ARRAY : -1;
1074 k = CTF_K_POINTER; /* avoid leading whitespace (see below) */
1076 for (prec = CTF_PREC_BASE; prec < CTF_PREC_MAX; prec++) {
1077 for (cdp = ctf_list_next(&cd.cd_nodes[prec]);
1078 cdp != NULL; cdp = ctf_list_next(cdp)) {
1080 const void *tp = ctf_lookup_by_id(lc, cdp->cd_type);
1081 const char *name = ctf_type_rname(lc, tp);
1083 if (k != CTF_K_POINTER && k != CTF_K_ARRAY)
1084 ctf_decl_sprintf(&cd, " ");
1087 ctf_decl_sprintf(&cd, "(");
1091 switch (cdp->cd_kind) {
1095 ctf_decl_sprintf(&cd, "%s", name);
1098 ctf_decl_sprintf(&cd, "*");
1101 ctf_decl_sprintf(&cd, "[%u]", cdp->cd_n);
1103 case CTF_K_FUNCTION:
1104 ctf_decl_sprintf(&cd, "()");
1108 ctf_decl_sprintf(&cd, "struct %s", name);
1111 ctf_decl_sprintf(&cd, "union %s", name);
1114 ctf_decl_sprintf(&cd, "enum %s", name);
1116 case CTF_K_VOLATILE:
1117 ctf_decl_sprintf(&cd, "volatile");
1120 ctf_decl_sprintf(&cd, "const");
1122 case CTF_K_RESTRICT:
1123 ctf_decl_sprintf(&cd, "restrict");
1131 ctf_decl_sprintf(&cd, ")");
1139 fbt_getargdesc(void *arg __unused, dtrace_id_t id __unused, void *parg, dtrace_argdesc_t *desc)
1141 const ctf_header_t *hp;
1143 fbt_probe_t *fbt = parg;
1145 modctl_t *ctl = fbt->fbtp_ctl;
1147 int ndx = desc->dtargd_ndx;
1148 int symindx = fbt->fbtp_symindx;
1150 uint32_t offset, type;
1154 if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
1155 (void) strcpy(desc->dtargd_native, "int");
1159 desc->dtargd_ndx = DTRACE_ARGNONE;
1161 /* Get a pointer to the CTF data and it's length. */
1162 if (linker_ctf_get(ctl, &lc) != 0)
1163 /* No CTF data? Something wrong? *shrug* */
1166 /* Check if this module hasn't been initialised yet. */
1167 if (*lc.ctfoffp == NULL) {
1169 * Initialise the CTF object and function symindx to
1170 * byte offset array.
1172 if (fbt_ctfoff_init(ctl, &lc) != 0)
1175 /* Initialise the CTF type to byte offset array. */
1176 if (fbt_typoff_init(&lc) != 0)
1180 ctfoff = *lc.ctfoffp;
1182 if (ctfoff == NULL || *lc.typoffp == NULL)
1185 /* Check if the symbol index is out of range. */
1186 if (symindx >= lc.nsym)
1189 /* Check if the symbol isn't cross-referenced. */
1190 if ((offset = ctfoff[symindx]) == 0xffffffff)
1193 hp = (const ctf_header_t *) lc.ctftab;
1194 idwidth = hp->cth_version == CTF_VERSION_2 ? 2 : 4;
1195 dp = (const char *)(lc.ctftab + offset + sizeof(ctf_header_t));
1198 memcpy(&info, dp, idwidth);
1200 if (hp->cth_version == CTF_VERSION_2) {
1201 kind = CTF_V2_INFO_KIND(info);
1202 n = CTF_V2_INFO_VLEN(info);
1204 kind = CTF_V3_INFO_KIND(info);
1205 n = CTF_V3_INFO_VLEN(info);
1208 if (kind == CTF_K_UNKNOWN && n == 0) {
1209 printf("%s(%d): Unknown function!\n",__func__,__LINE__);
1213 if (kind != CTF_K_FUNCTION) {
1214 printf("%s(%d): Expected a function!\n",__func__,__LINE__);
1218 if (fbt->fbtp_roffset != 0) {
1219 /* Only return type is available for args[1] in return probe. */
1224 /* Check if the requested argument doesn't exist. */
1228 /* Skip the return type and arguments up to the one requested. */
1229 dp += idwidth * (ndx + 1);
1233 memcpy(&type, dp, idwidth);
1234 if (fbt_type_name(&lc, type, desc->dtargd_native, sizeof(desc->dtargd_native)) > 0)
1235 desc->dtargd_ndx = ndx;
1239 fbt_linker_file_cb(linker_file_t lf, void *arg)
1242 fbt_provide_module(arg, lf);
1248 fbt_load(void *dummy)
1250 /* Default the probe table size if not specified. */
1251 if (fbt_probetab_size == 0)
1252 fbt_probetab_size = FBT_PROBETAB_SIZE;
1254 /* Choose the hash mask for the probe table. */
1255 fbt_probetab_mask = fbt_probetab_size - 1;
1257 /* Allocate memory for the probe table. */
1259 malloc(fbt_probetab_size * sizeof (fbt_probe_t *), M_FBT, M_WAITOK | M_ZERO);
1261 dtrace_doubletrap_func = fbt_doubletrap;
1262 dtrace_invop_add(fbt_invop);
1264 if (dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_USER,
1265 NULL, &fbt_pops, NULL, &fbt_id) != 0)
1268 /* Create probes for the kernel and already-loaded modules. */
1269 linker_file_foreach(fbt_linker_file_cb, NULL);
1277 /* De-register the invalid opcode handler. */
1278 dtrace_invop_remove(fbt_invop);
1280 dtrace_doubletrap_func = NULL;
1282 /* De-register this DTrace provider. */
1283 if ((error = dtrace_unregister(fbt_id)) != 0)
1286 /* Free the probe table. */
1287 free(fbt_probetab, M_FBT);
1288 fbt_probetab = NULL;
1289 fbt_probetab_mask = 0;
1295 fbt_modevent(module_t mod __unused, int type, void *data __unused)
1318 SYSINIT(fbt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_load, NULL);
1319 SYSUNINIT(fbt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fbt_unload, NULL);
1321 DEV_MODULE(fbt, fbt_modevent, NULL);
1322 MODULE_VERSION(fbt, 1);
1323 MODULE_DEPEND(fbt, dtrace, 1, 1, 1);
1324 MODULE_DEPEND(fbt, opensolaris, 1, 1, 1);