]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/ath/athdebug/athdebug.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / tools / tools / ath / athdebug / athdebug.c
1 /*-
2  * Copyright (c) 2002-2007 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 ath0).
35  */
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <getopt.h>
44
45 #define N(a)    (sizeof(a)/sizeof(a[0]))
46
47 const char *progname;
48
49 enum {
50         ATH_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
51         ATH_DEBUG_XMIT_DESC     = 0x00000002,   /* xmit descriptors */
52         ATH_DEBUG_RECV          = 0x00000004,   /* basic recv operation */
53         ATH_DEBUG_RECV_DESC     = 0x00000008,   /* recv descriptors */
54         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
55         ATH_DEBUG_RESET         = 0x00000020,   /* reset processing */
56         ATH_DEBUG_MODE          = 0x00000040,   /* mode init/setup */
57         ATH_DEBUG_BEACON        = 0x00000080,   /* beacon handling */
58         ATH_DEBUG_WATCHDOG      = 0x00000100,   /* watchdog timeout */
59         ATH_DEBUG_INTR          = 0x00001000,   /* ISR */
60         ATH_DEBUG_TX_PROC       = 0x00002000,   /* tx ISR proc */
61         ATH_DEBUG_RX_PROC       = 0x00004000,   /* rx ISR proc */
62         ATH_DEBUG_BEACON_PROC   = 0x00008000,   /* beacon ISR proc */
63         ATH_DEBUG_CALIBRATE     = 0x00010000,   /* periodic calibration */
64         ATH_DEBUG_KEYCACHE      = 0x00020000,   /* key cache management */
65         ATH_DEBUG_STATE         = 0x00040000,   /* 802.11 state transitions */
66         ATH_DEBUG_NODE          = 0x00080000,   /* node management */
67         ATH_DEBUG_LED           = 0x00100000,   /* led management */
68         ATH_DEBUG_FF            = 0x00200000,   /* fast frames */
69         ATH_DEBUG_DFS           = 0x00400000,   /* DFS processing */
70         ATH_DEBUG_TDMA          = 0x00800000,   /* TDMA processing */
71         ATH_DEBUG_FATAL         = 0x80000000,   /* fatal errors */
72         ATH_DEBUG_ANY           = 0xffffffff
73 };
74
75 static struct {
76         const char      *name;
77         u_int           bit;
78 } flags[] = {
79         { "xmit",       ATH_DEBUG_XMIT },
80         { "xmit_desc",  ATH_DEBUG_XMIT_DESC },
81         { "recv",       ATH_DEBUG_RECV },
82         { "recv_desc",  ATH_DEBUG_RECV_DESC },
83         { "rate",       ATH_DEBUG_RATE },
84         { "reset",      ATH_DEBUG_RESET },
85         { "mode",       ATH_DEBUG_MODE },
86         { "beacon",     ATH_DEBUG_BEACON },
87         { "watchdog",   ATH_DEBUG_WATCHDOG },
88         { "intr",       ATH_DEBUG_INTR },
89         { "xmit_proc",  ATH_DEBUG_TX_PROC },
90         { "recv_proc",  ATH_DEBUG_RX_PROC },
91         { "beacon_proc",ATH_DEBUG_BEACON_PROC },
92         { "calibrate",  ATH_DEBUG_CALIBRATE },
93         { "keycache",   ATH_DEBUG_KEYCACHE },
94         { "state",      ATH_DEBUG_STATE },
95         { "node",       ATH_DEBUG_NODE },
96         { "led",        ATH_DEBUG_LED },
97         { "ff",         ATH_DEBUG_FF },
98         { "dfs",        ATH_DEBUG_DFS },
99         { "tdma",       ATH_DEBUG_TDMA },
100         { "fatal",      ATH_DEBUG_FATAL },
101 };
102
103 static u_int
104 getflag(const char *name, int len)
105 {
106         int i;
107
108         for (i = 0; i < N(flags); i++)
109                 if (strncasecmp(flags[i].name, name, len) == 0)
110                         return flags[i].bit;
111         return 0;
112 }
113
114 static const char *
115 getflagname(u_int flag)
116 {
117         int i;
118
119         for (i = 0; i < N(flags); i++)
120                 if (flags[i].bit == flag)
121                         return flags[i].name;
122         return "???";
123 }
124
125 static void
126 usage(void)
127 {
128         int i;
129
130         fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
131         fprintf(stderr, "where flags are:\n");
132         for (i = 0; i < N(flags); i++)
133                 printf("%s\n", flags[i].name);
134         exit(-1);
135 }
136
137 int
138 main(int argc, char *argv[])
139 {
140         const char *ifname;
141         const char *cp, *tp;
142         const char *sep;
143         int c, op, i;
144         u_int32_t debug, ndebug;
145         size_t debuglen;
146         char oid[256];
147
148         ifname = getenv("ATH");
149         if (ifname == NULL)
150                 ifname = "ath0";
151         progname = argv[0];
152         if (argc > 1) {
153                 if (strcmp(argv[1], "-i") == 0) {
154                         if (argc < 2)
155                                 errx(1, "missing interface name for -i option");
156                         ifname = argv[2];
157                         if (strncmp(ifname, "ath", 3) != 0)
158                                 errx(2, "huh, this is for ath devices?");
159                         argc -= 2, argv += 2;
160                 } else if (strcmp(argv[1], "-?") == 0)
161                         usage();
162         }
163
164 #ifdef __linux__
165         snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
166 #else
167         snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
168 #endif
169         debuglen = sizeof(debug);
170         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
171                 err(1, "sysctl-get(%s)", oid);
172         ndebug = debug;
173         for (; argc > 1; argc--, argv++) {
174                 cp = argv[1];
175                 do {
176                         u_int bit;
177
178                         if (*cp == '-') {
179                                 cp++;
180                                 op = -1;
181                         } else if (*cp == '+') {
182                                 cp++;
183                                 op = 1;
184                         } else
185                                 op = 0;
186                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
187                                 tp++;
188                         bit = getflag(cp, tp-cp);
189                         if (op < 0)
190                                 ndebug &= ~bit;
191                         else if (op > 0)
192                                 ndebug |= bit;
193                         else {
194                                 if (bit == 0) {
195                                         if (isdigit(*cp))
196                                                 bit = strtoul(cp, NULL, 0);
197                                         else
198                                                 errx(1, "unknown flag %.*s",
199                                                         tp-cp, cp);
200                                 }
201                                 ndebug = bit;
202                         }
203                 } while (*(cp = tp) != '\0');
204         }
205         if (debug != ndebug) {
206                 printf("%s: 0x%x => ", oid, debug);
207                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
208                         err(1, "sysctl-set(%s)", oid);
209                 printf("0x%x", ndebug);
210                 debug = ndebug;
211         } else
212                 printf("%s: 0x%x", oid, debug);
213         sep = "<";
214         for (i = 0; i < N(flags); i++)
215                 if (debug & flags[i].bit) {
216                         printf("%s%s", sep, flags[i].name);
217                         sep = ",";
218                 }
219         printf("%s\n", *sep != '<' ? ">" : "");
220         return 0;
221 }