xref: /freebsd/tools/tools/ath/athdebug/athdebug.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * athdebug [-i interface] flags
34  * (default interface is wlan0).
35  */
36 
37 #include <sys/param.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <getopt.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <strings.h>
50 
51 const char *progname;
52 
53 /* XXX TODO: include if_ath_debug.h */
54 enum {
55 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
56 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
57 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
58 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
59 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
60 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
61 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
62 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
63 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
64 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
65 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
66 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
67 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
68 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
69 	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
70 	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
71 	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
72 	ATH_DEBUG_LED		= 0x00100000,	/* led management */
73 	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
74 	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
75 	ATH_DEBUG_TDMA		= 0x00800000,	/* TDMA processing */
76 	ATH_DEBUG_TDMA_TIMER	= 0x01000000,	/* TDMA timer processing */
77 	ATH_DEBUG_REGDOMAIN	= 0x02000000,	/* regulatory processing */
78 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
79 	ATH_DEBUG_ANY		= 0xffffffff
80 };
81 
82 static struct {
83 	const char	*name;
84 	uint64_t	bit;
85 } flags[] = {
86 	{ "xmit",	ATH_DEBUG_XMIT },
87 	{ "xmit_desc",	ATH_DEBUG_XMIT_DESC },
88 	{ "recv",	ATH_DEBUG_RECV },
89 	{ "recv_desc",	ATH_DEBUG_RECV_DESC },
90 	{ "rate",	ATH_DEBUG_RATE },
91 	{ "reset",	ATH_DEBUG_RESET },
92 	{ "mode",	ATH_DEBUG_MODE },
93 	{ "beacon",	ATH_DEBUG_BEACON },
94 	{ "watchdog",	ATH_DEBUG_WATCHDOG },
95 	{ "intr",	ATH_DEBUG_INTR },
96 	{ "xmit_proc",	ATH_DEBUG_TX_PROC },
97 	{ "recv_proc",	ATH_DEBUG_RX_PROC },
98 	{ "beacon_proc",ATH_DEBUG_BEACON_PROC },
99 	{ "calibrate",	ATH_DEBUG_CALIBRATE },
100 	{ "keycache",	ATH_DEBUG_KEYCACHE },
101 	{ "state",	ATH_DEBUG_STATE },
102 	{ "node",	ATH_DEBUG_NODE },
103 	{ "led",	ATH_DEBUG_LED },
104 	{ "ff",		ATH_DEBUG_FF },
105 	{ "dfs",	ATH_DEBUG_DFS },
106 	{ "tdma",	ATH_DEBUG_TDMA },
107 	{ "tdma_timer",	ATH_DEBUG_TDMA_TIMER },
108 	{ "regdomain",	ATH_DEBUG_REGDOMAIN },
109 	{ "fatal",	ATH_DEBUG_FATAL },
110 };
111 
112 static uint64_t
113 getflag(const char *name, int len)
114 {
115 	int i;
116 
117 	for (i = 0; i < nitems(flags); i++)
118 		if (strncasecmp(flags[i].name, name, len) == 0)
119 			return flags[i].bit;
120 	return 0;
121 }
122 
123 static const char *
124 getflagname(u_int flag)
125 {
126 	int i;
127 
128 	for (i = 0; i < nitems(flags); i++)
129 		if (flags[i].bit == flag)
130 			return flags[i].name;
131 	return "???";
132 }
133 
134 static void
135 usage(void)
136 {
137 	int i;
138 
139 	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
140 	fprintf(stderr, "where flags are:\n");
141 	for (i = 0; i < nitems(flags); i++)
142 		printf("%s\n", flags[i].name);
143 	exit(-1);
144 }
145 
146 int
147 main(int argc, char *argv[])
148 {
149 	const char *ifname;
150 	const char *cp, *tp;
151 	const char *sep;
152 	int c, op, i;
153 	uint64_t debug, ndebug;
154 	size_t debuglen;
155 	char oid[256];
156 
157 	ifname = getenv("ATH");
158 	if (ifname == NULL)
159 		ifname = ATH_DEFAULT;
160 	progname = argv[0];
161 	if (argc > 1) {
162 		if (strcmp(argv[1], "-i") == 0) {
163 			if (argc < 2)
164 				errx(1, "missing interface name for -i option");
165 			ifname = argv[2];
166 			if (strncmp(ifname, "ath", 3) != 0)
167 				errx(2, "huh, this is for ath devices?");
168 			argc -= 2, argv += 2;
169 		} else if (strcmp(argv[1], "-?") == 0)
170 			usage();
171 	}
172 
173 #ifdef __linux__
174 	snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
175 #else
176 	snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
177 #endif
178 	debuglen = sizeof(debug);
179 	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
180 		err(1, "sysctl-get(%s)", oid);
181 	ndebug = debug;
182 	for (; argc > 1; argc--, argv++) {
183 		cp = argv[1];
184 		do {
185 			u_int bit;
186 
187 			if (*cp == '-') {
188 				cp++;
189 				op = -1;
190 			} else if (*cp == '+') {
191 				cp++;
192 				op = 1;
193 			} else
194 				op = 0;
195 			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
196 				tp++;
197 			bit = getflag(cp, tp-cp);
198 			if (op < 0)
199 				ndebug &= ~bit;
200 			else if (op > 0)
201 				ndebug |= bit;
202 			else {
203 				if (bit == 0) {
204 					if (isdigit(*cp))
205 						bit = strtoul(cp, NULL, 0);
206 					else
207 						errx(1, "unknown flag %.*s",
208 							(int) (tp-cp), cp);
209 				}
210 				ndebug = bit;
211 			}
212 		} while (*(cp = tp) != '\0');
213 	}
214 	if (debug != ndebug) {
215 		printf("%s: 0x%llx => ", oid, (long long) debug);
216 		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
217 			err(1, "sysctl-set(%s)", oid);
218 		printf("0x%llx", (long long) ndebug);
219 		debug = ndebug;
220 	} else
221 		printf("%s: 0x%llx", oid, (long long) debug);
222 	sep = "<";
223 	for (i = 0; i < nitems(flags); i++)
224 		if (debug & flags[i].bit) {
225 			printf("%s%s", sep, flags[i].name);
226 			sep = ",";
227 		}
228 	printf("%s\n", *sep != '<' ? ">" : "");
229 	return 0;
230 }
231