summaryrefslogtreecommitdiffstats
path: root/tools/rpcdebug/rpcdebug.c
blob: 444616d4759ee57e68796acb92152b59cfb17130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Get or set RPC debug flags.
 *
 * I would have loved to write this without recourse to the sysctl
 * interface, but the only plausible approach (reading and writing
 * /dev/kmem at the offsets indicated by the _debug symbols from
 * /proc/ksyms) didn't work, because /dev/kmem doesn't translate virtual
 * addresses on write. Unfortunately, modules are stuffed into memory
 * allocated via vmalloc.
 *
 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
 *           (C) 2004 <frederic.jolly@bull.ext.net>
 *
 * 06/15/2004: updated for NFSv4
 *
 */

/* #include "config.h" */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
#include <ctype.h>
/* RPC debug flags
   #include <sunrpc/debug.h> */
/* NFS debug flags
   #include <nfs_fs.h> */
/* NFSD and NLM debug flags
   #include <nfsd/debug.h> */
#include <nfs/debug.h>

static int		verbose = 0;
static char*            cdename;

static unsigned int	find_flag(char **module, char *name);
static unsigned int	get_flags(char *);
static unsigned int	set_flags(char *, unsigned int value);
static void		print_flags(FILE *, char *, unsigned int, int);
static char *		strtolower(char *str);
static void		usage(int excode, char *module);

int
main(int argc, char **argv)
{
	int		opt_s = 0,
			opt_c = 0;
	unsigned int	flags = 0, oflags;
	char *		module = NULL;
	int		c;

	cdename = malloc(strlen(basename(argv[0])));
	if (cdename == NULL) {
	  fprintf(stderr, "failed in malloc\n");
	  exit(1);
        }
	strcpy(cdename, basename(argv[0]));

	if (!strcmp(cdename, "nfsdebug")) {
	  module = "nfs";
	}
	else if (!strcmp(cdename, "nfsddebug")) {
	  module = "nfsd";
	}

	while ((c = getopt(argc, argv, "chm:sv")) != EOF) {
		switch (c) {
		case 'c':
			opt_c = 1;
			break;
		case 'h':
			usage(0, module);
		case 'm':
			module = optarg;
			break;
		case 's':
			opt_s = 1;
			break;
		case 'v':
			verbose++;
			break;
		default:
		        fprintf(stderr, "%s: unknown option -%c\n", cdename, optopt);
			usage(1, module);
		}
	}

	if (opt_c + opt_s > 1) {
		fprintf(stderr, "You can use at most one of -c and -s\n");
		usage(1, module);
	}

	if (!module) {
		fprintf(stderr, "%s: no module name specified.\n", cdename);
		usage(1, module);
	}

	if (strcmp(module, "nfsd") &&
	    strcmp(module, "nfs") &&
	    strcmp(module, "nlm") &&
	    strcmp(module, "rpc")) {
	        fprintf(stderr, "%s: unknown module: %s\n", cdename, module);
		usage(1, module);
	}

	if (argc == optind) {
		flags = ~(unsigned int) 0;
	} else {
		for (; optind < argc; optind++)
			flags |= find_flag(&module, argv[optind]);
		if (flags && !opt_c)
			opt_s = 1;
	}

	oflags = get_flags(module);

	if (opt_c) {
		oflags = set_flags(module, oflags & ~flags);
	} else if (opt_s) {
		oflags = set_flags(module, oflags | flags);
	}
	print_flags(stdout, module, oflags, 0);
	if (verbose) {
	        fprintf(stdout, "\nModule     Valid flags\n");
	        print_flags(stdout, module, ~(unsigned int) 0, 1);
	}

	return 0;
}

#define FLAG(mname, fname)	\
      { #mname, #fname, mname##DBG_##fname }

static struct flagmap {
	char *		module;
	char *		name;
	unsigned int	value;
}			flagmap[] = {
	/* rpc */
	FLAG(RPC,	XPRT),
	FLAG(RPC,	CALL),
	FLAG(RPC,	DEBUG),
	FLAG(RPC,	NFS),
	FLAG(RPC,	AUTH),
	FLAG(RPC,	BIND),
	FLAG(RPC,	SCHED),
	FLAG(RPC,	TRANS),
	FLAG(RPC,	SVCSOCK),
	FLAG(RPC,	SVCDSP),
	FLAG(RPC,	MISC),
	FLAG(RPC,	CACHE),
	FLAG(RPC,	ALL),

	/* nfs */
	FLAG(NFS,	VFS),
	FLAG(NFS,	DIRCACHE),
	FLAG(NFS,	LOOKUPCACHE),
	FLAG(NFS,	PAGECACHE),
	FLAG(NFS,	PROC),
	FLAG(NFS,       XDR),
	FLAG(NFS,       FILE),
	FLAG(NFS,       ROOT),
	FLAG(NFS,       CALLBACK),
	FLAG(NFS,       CLIENT),
	FLAG(NFS,       MOUNT),
	FLAG(NFS,       FSCACHE),
	FLAG(NFS,       PNFS),
	FLAG(NFS,       PNFS_LD),
	FLAG(NFS,	ALL),

	/* nfsd */
	FLAG(NFSD,	SOCK),
	FLAG(NFSD,	FH),
	FLAG(NFSD,	EXPORT),
	FLAG(NFSD,	SVC),
	FLAG(NFSD,	PROC),
	FLAG(NFSD,	FILEOP),
	FLAG(NFSD,	AUTH),
	FLAG(NFSD,	REPCACHE),
	FLAG(NFSD,	XDR),
	FLAG(NFSD,	LOCKD),
	FLAG(NFSD,	ALL),

	/* lockd */
	FLAG(NLM,	SVC),
	FLAG(NLM,	CLIENT),
	FLAG(NLM,	CLNTLOCK),
	FLAG(NLM,	SVCLOCK),
	FLAG(NLM,	MONITOR),
	FLAG(NLM,	CLNTSUBS),
	FLAG(NLM,	SVCSUBS),
	FLAG(NLM,       HOSTCACHE),
	FLAG(NLM,       XDR),
	FLAG(NLM,	ALL),

      { NULL,		NULL,		0 }
};

static unsigned int
find_flag(char **module, char *name)
{
	char		*mod = *module;
	unsigned int	value = 0;
	int		i;

	for (i = 0; flagmap[i].module; i++) {
		if ((mod && strcasecmp(mod, flagmap[i].module))
		 || strcasecmp(name, flagmap[i].name))
			continue;
		if (value) {
			fprintf(stderr,
				"%s: ambiguous symbol name %s.\n"
				"This name is used by more than one module, "
				"please specify the module name using\n"
				"the -m option.\n",
				cdename, name);
			exit(1);
		}
		value = flagmap[i].value;
		if (*module)
			return value;
		mod = flagmap[i].module;
	}

	if (!value) {
		if (*module)
			fprintf(stderr,
				"%s: unknown module or flag %s/%s\n",
				cdename, *module, name);
		else
			fprintf(stderr,
				"%s: unknown flag %s\n",
				cdename, name);
		exit(1);
	}

	*module = mod;
	return value;
}

static unsigned int
get_flags(char *module)
{
	char	buffer[256], filename[256];
	int	sysfd, len;

	snprintf(filename, 256, "/proc/sys/sunrpc/%s_debug", module);

	if ((sysfd = open(filename, O_RDONLY)) < 0) {
		perror(filename);
		exit(1);
	}
	if ((len = read(sysfd, buffer, sizeof(buffer))) < 0) {
		perror("read");
		exit(1);
	}
	close(sysfd);
	buffer[len - 1] = '\0';

	return strtoul(buffer, NULL, 0);
}

static unsigned int
set_flags(char *module, unsigned int value)
{
	char	buffer[64], filename[256];
	int	sysfd, len, ret;

	snprintf(filename, 256, "/proc/sys/sunrpc/%s_debug", module);

	len = sprintf(buffer, "%d", value);
	if ((sysfd = open(filename, O_WRONLY)) < 0) {
		perror(filename);
		exit(1);
	}
	if ((ret = write(sysfd, buffer, len)) < 0) {
		perror("write");
		exit(1);
	}
	if (ret < len) {
		fprintf(stderr, "error: short write in set_flags!\n");
		exit(1);
	}
	close(sysfd);
	return value;
}


static char *
strtolower(char *str)
{
	static char	temp[64];
	char		*sp;

	strcpy(temp, str);
	for (sp = temp; *sp; sp++)
		*sp = tolower(*sp);
	return temp;
}

static void
print_flags(FILE *ofp, char *module, unsigned int flags, int show_all)
{
	char		*lastmod = NULL;
	unsigned int	shown = 0;
	int		i;

	if (module) {
		fprintf(ofp, "%-10s", strtolower(module));
		if (!flags) {
			fprintf(ofp, "<no flags set>\n");
			return;
		}
	}

	for (i = 0, shown = 0; flagmap[i].module; i++) {
		if (module) {
			if (strcasecmp(flagmap[i].module, module))
				continue;
		} else if (!lastmod || strcmp(lastmod, flagmap[i].module)) {
			if (lastmod) {
				fprintf(ofp, "\n");
				shown = 0;
			}
			fprintf(ofp, "%-10s", strtolower(flagmap[i].module));
			lastmod = flagmap[i].module;
		}
		if (!(flags & flagmap[i].value)
		 || (!show_all && (shown & flagmap[i].value))
		 || (module && !strcasecmp(flagmap[i].name, "all")))
			continue;
		fprintf(ofp, " %s", strtolower(flagmap[i].name));
		shown |= flagmap[i].value;
	}
	fprintf(ofp, "\n");
}

static void
usage(int excode, char *module)
{
        if (module)
	  fprintf(stderr, "usage: %s [-v] [-h] [-s flags...|-c flags...]\n", cdename);
	else
	  fprintf(stderr, "usage: %s [-v] [-h] [-m module] [-s flags...|-c flags...]\n", cdename);
	fprintf(stderr, "       set or cancel debug flags.\n");
	if (verbose) {
	  fprintf(stderr, "\nModule     Valid flags\n");
	  print_flags(stderr, module, ~(unsigned int) 0, 1);
	} else {
	  if (module)
	    fprintf(stderr, "       (use %s -vh to get a list of valid flags)\n", cdename);
	  else
	    fprintf(stderr, "       (use %s -vh to get a list of modules and valid flags)\n", cdename);
	}
	exit (excode);
}