19512fe85Sahl /*
29512fe85Sahl  * CDDL HEADER START
39512fe85Sahl  *
49512fe85Sahl  * The contents of this file are subject to the terms of the
59512fe85Sahl  * Common Development and Distribution License (the "License").
69512fe85Sahl  * You may not use this file except in compliance with the License.
79512fe85Sahl  *
89512fe85Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99512fe85Sahl  * or http://www.opensolaris.org/os/licensing.
109512fe85Sahl  * See the License for the specific language governing permissions
119512fe85Sahl  * and limitations under the License.
129512fe85Sahl  *
139512fe85Sahl  * When distributing Covered Code, include this CDDL HEADER in each
149512fe85Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159512fe85Sahl  * If applicable, add the following below this CDDL HEADER, with the
169512fe85Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
179512fe85Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
189512fe85Sahl  *
199512fe85Sahl  * CDDL HEADER END
209512fe85Sahl  */
219512fe85Sahl 
229512fe85Sahl /*
23*8cb74972SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
249512fe85Sahl  * Use is subject to license terms.
259512fe85Sahl  */
269512fe85Sahl 
279512fe85Sahl #include <strings.h>
289512fe85Sahl #include <unistd.h>
299512fe85Sahl #include <dtrace.h>
309512fe85Sahl 
319512fe85Sahl static int g_count;
329512fe85Sahl static int g_errs;
339512fe85Sahl static int g_fd;
349512fe85Sahl static int g_verbose;
359512fe85Sahl static int g_errexit;
36*8cb74972SJonathan Haslam static char *g_progname;
379512fe85Sahl 
389512fe85Sahl static int
probe(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * data)399512fe85Sahl probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *data)
409512fe85Sahl {
419512fe85Sahl 	dtrace_probeinfo_t p;
429512fe85Sahl 	dtrace_argdesc_t arg;
439512fe85Sahl 	char buf[BUFSIZ];
449512fe85Sahl 	int i;
459512fe85Sahl 
469512fe85Sahl 	(void) printf("\r%6d", ++g_count);
479512fe85Sahl 	(void) fflush(stdout);
489512fe85Sahl 
499512fe85Sahl 	if (dtrace_probe_info(dtp, pdp, &p) != 0) {
509512fe85Sahl 		(void) printf(" failed to get probe info for "
519512fe85Sahl 		    "%s:%s:%s:%s [%d]\n", pdp->dtpd_provider, pdp->dtpd_mod,
529512fe85Sahl 		    pdp->dtpd_func, pdp->dtpd_name, pdp->dtpd_id);
539512fe85Sahl 		g_errs++;
549512fe85Sahl 		return (0);
559512fe85Sahl 	}
569512fe85Sahl 
579512fe85Sahl 	for (i = 0; i < p.dtp_argc; i++) {
589512fe85Sahl 		if (p.dtp_argv[i].dtt_type == CTF_ERR) {
599512fe85Sahl 			bzero(&arg, sizeof (dtrace_argdesc_t));
609512fe85Sahl 			arg.dtargd_id = pdp->dtpd_id;
619512fe85Sahl 			arg.dtargd_ndx = i;
629512fe85Sahl 			(void) ioctl(g_fd, DTRACEIOC_PROBEARG, &arg);
639512fe85Sahl 
649512fe85Sahl 			(void) printf(" failed to get types for args[%d] "
659512fe85Sahl 			    "of %s:%s:%s:%s [%d]: <%s> -> <%s>\n", i,
669512fe85Sahl 			    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func,
679512fe85Sahl 			    pdp->dtpd_name, pdp->dtpd_id,
689512fe85Sahl 			    arg.dtargd_native, arg.dtargd_xlate);
699512fe85Sahl 
709512fe85Sahl 			g_errs++;
719512fe85Sahl 
729512fe85Sahl 			if (g_errexit)
739512fe85Sahl 				return (-1);
749512fe85Sahl 
759512fe85Sahl 		} else if (g_verbose) {
769512fe85Sahl 			(void) printf("%d args[%d] : %s\n", pdp->dtpd_id, i,
779512fe85Sahl 			    ctf_type_name(p.dtp_argv[i].dtt_ctfp,
789512fe85Sahl 			    p.dtp_argv[i].dtt_type, buf, sizeof (buf)));
799512fe85Sahl 		}
809512fe85Sahl 	}
819512fe85Sahl 
829512fe85Sahl 	return (0);
839512fe85Sahl }
849512fe85Sahl 
859512fe85Sahl int
main(int argc,char * argv[])869512fe85Sahl main(int argc, char *argv[])
879512fe85Sahl {
889512fe85Sahl 	dtrace_probedesc_t pd, *pdp = NULL;
899512fe85Sahl 	dtrace_hdl_t *dtp;
909512fe85Sahl 	int err, c;
919512fe85Sahl 	char *p;
929512fe85Sahl 
93*8cb74972SJonathan Haslam 	g_progname = argv[0];
94*8cb74972SJonathan Haslam 
959512fe85Sahl 	if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
969512fe85Sahl 		(void) fprintf(stderr, "%s: failed to open dtrace: %s\n",
97*8cb74972SJonathan Haslam 		    g_progname, dtrace_errmsg(dtp, err));
989512fe85Sahl 		return (1);
999512fe85Sahl 	}
1009512fe85Sahl 
1019512fe85Sahl 	while ((c = getopt(argc, argv, "evx:")) != -1) {
1029512fe85Sahl 		switch (c) {
1039512fe85Sahl 		case 'e':
1049512fe85Sahl 			g_errexit++;
1059512fe85Sahl 			break;
1069512fe85Sahl 		case 'v':
1079512fe85Sahl 			g_verbose++;
1089512fe85Sahl 			break;
1099512fe85Sahl 		case 'x':
1109512fe85Sahl 			if ((p = strchr(optarg, '=')) != NULL)
1119512fe85Sahl 				*p++ = '\0';
1129512fe85Sahl 
1139512fe85Sahl 			if (dtrace_setopt(dtp, optarg, p) != 0) {
1149512fe85Sahl 				(void) fprintf(stderr, "%s: failed to set "
115*8cb74972SJonathan Haslam 				    "option -x %s: %s\n", g_progname, optarg,
1169512fe85Sahl 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1179512fe85Sahl 				return (2);
1189512fe85Sahl 			}
1199512fe85Sahl 			break;
1209512fe85Sahl 
1219512fe85Sahl 		default:
1229512fe85Sahl 			(void) fprintf(stderr, "Usage: %s [-ev] "
123*8cb74972SJonathan Haslam 			    "[-x opt[=arg]] [probedesc]\n", g_progname);
1249512fe85Sahl 			return (2);
1259512fe85Sahl 		}
1269512fe85Sahl 	}
1279512fe85Sahl 
1289512fe85Sahl 	argv += optind;
1299512fe85Sahl 	argc -= optind;
1309512fe85Sahl 
1319512fe85Sahl 	if (argc > 0) {
132*8cb74972SJonathan Haslam 		if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, argv[0], &pd)) {
1339512fe85Sahl 			(void) fprintf(stderr, "%s: invalid probe description "
134*8cb74972SJonathan Haslam 			    "%s: %s\n", g_progname, argv[0],
1359512fe85Sahl 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1369512fe85Sahl 			return (2);
1379512fe85Sahl 		}
1389512fe85Sahl 		pdp = &pd;
1399512fe85Sahl 	}
1409512fe85Sahl 
1419512fe85Sahl 	g_fd = dtrace_ctlfd(dtp);
1429512fe85Sahl 	(void) dtrace_probe_iter(dtp, pdp, probe, NULL);
1439512fe85Sahl 	dtrace_close(dtp);
1449512fe85Sahl 
1459512fe85Sahl 	(void) printf("\nTotal probes: %d\n", g_count);
1469512fe85Sahl 	(void) printf("Total errors: %d\n\n", g_errs);
1479512fe85Sahl 
1489512fe85Sahl 	return (g_errs != 0);
1499512fe85Sahl }
150