xref: /illumos-gate/usr/src/cmd/dtrace/test/cmd/chkargs/chkargs.c (revision 168c213023b7f347f11abfc72f448b0c621ab718)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <strings.h>
30 #include <unistd.h>
31 #include <dtrace.h>
32 
33 static int g_count;
34 static int g_errs;
35 static int g_fd;
36 static int g_verbose;
37 static int g_errexit;
38 
39 static int
40 probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *data)
41 {
42 	dtrace_probeinfo_t p;
43 	dtrace_argdesc_t arg;
44 	char buf[BUFSIZ];
45 	int i;
46 
47 	(void) printf("\r%6d", ++g_count);
48 	(void) fflush(stdout);
49 
50 	if (dtrace_probe_info(dtp, pdp, &p) != 0) {
51 		(void) printf(" failed to get probe info for "
52 		    "%s:%s:%s:%s [%d]\n", pdp->dtpd_provider, pdp->dtpd_mod,
53 		    pdp->dtpd_func, pdp->dtpd_name, pdp->dtpd_id);
54 		g_errs++;
55 		return (0);
56 	}
57 
58 	for (i = 0; i < p.dtp_argc; i++) {
59 		if (p.dtp_argv[i].dtt_type == CTF_ERR) {
60 			bzero(&arg, sizeof (dtrace_argdesc_t));
61 			arg.dtargd_id = pdp->dtpd_id;
62 			arg.dtargd_ndx = i;
63 			(void) ioctl(g_fd, DTRACEIOC_PROBEARG, &arg);
64 
65 			(void) printf(" failed to get types for args[%d] "
66 			    "of %s:%s:%s:%s [%d]: <%s> -> <%s>\n", i,
67 			    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func,
68 			    pdp->dtpd_name, pdp->dtpd_id,
69 			    arg.dtargd_native, arg.dtargd_xlate);
70 
71 			g_errs++;
72 
73 			if (g_errexit)
74 				return (-1);
75 
76 		} else if (g_verbose) {
77 			(void) printf("%d args[%d] : %s\n", pdp->dtpd_id, i,
78 			    ctf_type_name(p.dtp_argv[i].dtt_ctfp,
79 			    p.dtp_argv[i].dtt_type, buf, sizeof (buf)));
80 		}
81 	}
82 
83 	return (0);
84 }
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	dtrace_probedesc_t pd, *pdp = NULL;
90 	dtrace_hdl_t *dtp;
91 	int err, c;
92 	char *p;
93 
94 	if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
95 		(void) fprintf(stderr, "%s: failed to open dtrace: %s\n",
96 		    argv[0], dtrace_errmsg(dtp, err));
97 		return (1);
98 	}
99 
100 	while ((c = getopt(argc, argv, "evx:")) != -1) {
101 		switch (c) {
102 		case 'e':
103 			g_errexit++;
104 			break;
105 		case 'v':
106 			g_verbose++;
107 			break;
108 		case 'x':
109 			if ((p = strchr(optarg, '=')) != NULL)
110 				*p++ = '\0';
111 
112 			if (dtrace_setopt(dtp, optarg, p) != 0) {
113 				(void) fprintf(stderr, "%s: failed to set "
114 				    "option -x %s: %s\n", argv[0], optarg,
115 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
116 				return (2);
117 			}
118 			break;
119 
120 		default:
121 			(void) fprintf(stderr, "Usage: %s [-ev] "
122 			    "[-x opt[=arg]] [probedesc]\n", argv[0]);
123 			return (2);
124 		}
125 	}
126 
127 	argv += optind;
128 	argc -= optind;
129 
130 	if (argc > 0) {
131 		if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, argv[1], &pd)) {
132 			(void) fprintf(stderr, "%s: invalid probe description "
133 			    "%s: %s\n", argv[0], argv[1],
134 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
135 			return (2);
136 		}
137 		pdp = &pd;
138 	}
139 
140 	g_fd = dtrace_ctlfd(dtp);
141 	(void) dtrace_probe_iter(dtp, pdp, probe, NULL);
142 	dtrace_close(dtp);
143 
144 	(void) printf("\nTotal probes: %d\n", g_count);
145 	(void) printf("Total errors: %d\n\n", g_errs);
146 
147 	return (g_errs != 0);
148 }
149