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