1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Some day I'll split all of this functionality into a cfs_debug module
22 * of its own. That day is not today.
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
40 #include <asm/atomic.h>
41 #include <linux/module.h>
42 #include <portals/api-support.h>
50 static char rawbuf[8192];
51 static char *buf = rawbuf;
52 static int max = 8192;
53 static int g_pfd = -1;
54 static int subsystem_array[1 << 8];
55 static int debug_mask = ~0;
57 static const char *portal_debug_subsystems[] =
58 {"undefined", "mdc", "mds", "osc", "ost", "class", "obdfs", "llite",
59 "rpc", "ext2obd", "portals", "socknal", "qswnal", "pinger", "filter",
60 "obdtrace", "echo", "ldlm", "lov", "gmnal", "router", NULL};
61 static const char *portal_debug_masks[] =
62 {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
63 "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
64 "page", "dlmtrace", NULL};
66 int debug_setup(int argc, char **argv) {
67 memset(subsystem_array, 1, sizeof(subsystem_array));
71 static int do_debug_mask(char *name, int enable) {
74 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
75 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
76 strcasecmp(name, "all_subs") == 0) {
77 fprintf(stderr, "%s output from subsystem \"%s\"\n",
78 enable ? "Enabling" : "Disabling",
79 portal_debug_subsystems[i]);
80 subsystem_array[i] = enable;
84 for (i = 0; portal_debug_masks[i] != NULL; i++) {
85 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
86 strcasecmp(name, "all_types") == 0) {
87 fprintf(stderr, "%s output of type \"%s\"\n",
88 enable ? "Enabling" : "Disabling",
89 portal_debug_masks[i]);
91 debug_mask |= (1 << i);
93 debug_mask &= ~(1 << i);
101 /* if 'raw' is true, don't strip the debug information from the
102 * front of the lines */
103 static void dump_buffer(FILE *fd, char *buf, int size, int raw) {
105 unsigned long subsystem, debug, dropped = 0, kept = 0;
106 int max_sub, max_type;
108 for (max_sub = 0; portal_debug_subsystems[max_sub] != NULL; max_sub++)
110 for (max_type = 0; portal_debug_masks[max_type] != NULL; max_type++)
114 p = memchr(buf, '\n', size);
117 subsystem = strtoul(buf, &z, 16);
118 debug = strtoul(z + 1, &z, 16);
121 /* for some reason %*s isn't working. */
123 if (subsystem < max_sub &&
124 subsystem_array[subsystem] &&
125 (!debug || (debug_mask & debug))) {
127 fprintf(fd, "%s\n", buf);
129 fprintf(fd, "%s\n", z);
130 //printf("%s\n", buf);
133 //fprintf(stderr, "dropping line (%lx:%lx): %s\n", subsystem, debug, buf);
142 fprintf(stderr, "Debug log: %lu lines, %lu kept, %lu dropped.\n",
143 dropped + kept, kept, dropped);
146 int jt_debug_kernel(int argc, char **argv) {
149 const int databuf_size = (6 << 20);
150 struct portal_ioctl_data data, *newdata;
151 char *databuf = NULL;
159 fd = fopen(argv[1], "w");
161 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
169 databuf = malloc(databuf_size);
171 fprintf(stderr, "No memory for buffer.\n");
175 memset(&data, 0, sizeof(data));
176 data.ioc_plen1 = databuf_size;
177 data.ioc_pbuf1 = databuf;
179 if (portal_ioctl_pack(&data, &buf, max) != 0) {
180 fprintf(stderr, "portal_ioctl_pack failed.\n");
184 rc = ioctl(g_pfd, IOC_PORTAL_GET_DEBUG, buf);
186 fprintf(stderr, "IOC_PORTAL_GET_DEBUG failed: %s\n",
191 newdata = (struct portal_ioctl_data *)buf;
192 if (newdata->ioc_size > 0)
193 dump_buffer(fd, databuf, newdata->ioc_size, raw);
195 fprintf(stderr, "No data in the debug buffer.\n");
205 int jt_debug_file(int argc, char **argv) {
206 int rc, fd = -1, raw = 0;
207 FILE *output = stdout;
208 char *databuf = NULL;
211 if (argc > 4 || argc < 2)
214 fd = open(argv[1], O_RDONLY);
216 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
220 rc = fstat(fd, &statbuf);
222 fprintf(stderr, "fstat failed: %s\n", strerror(errno));
227 output = fopen(argv[2], "w");
228 if (output == NULL) {
229 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
238 databuf = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE,
240 if (databuf == NULL) {
241 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
245 dump_buffer(output, databuf, statbuf.st_size, raw);
249 munmap(databuf, statbuf.st_size);
250 if (output != stdout)
257 int jt_debug_clear(int argc, char **argv) {
259 struct portal_ioctl_data data;
265 memset(&data, 0, sizeof(data));
266 if (portal_ioctl_pack(&data, &buf, max) != 0) {
267 fprintf(stderr, "portal_ioctl_pack failed.\n");
271 rc = ioctl(g_pfd, IOC_PORTAL_CLEAR_DEBUG, buf);
273 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
280 int jt_debug_mark(int argc, char **argv) {
282 struct portal_ioctl_data data;
284 time_t now = time(NULL);
294 text[strlen(text) - 1] = '\0'; /* stupid \n */
297 memset(&data, 0, sizeof(data));
298 data.ioc_inllen1 = strlen(text) + 1;
299 data.ioc_inlbuf1 = text;
300 if (portal_ioctl_pack(&data, &buf, max) != 0) {
301 fprintf(stderr, "portal_ioctl_pack failed.\n");
305 rc = ioctl(g_pfd, IOC_PORTAL_MARK_DEBUG, buf);
307 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
314 int jt_debug_filter(int argc, char **argv) {
320 for (i = 1; i < argc; i++)
321 if (!do_debug_mask(argv[i], 0))
322 fprintf(stderr, "Unknown subsystem or "
323 "debug type: %s\n", argv[i]);
327 int jt_debug_show(int argc, char **argv) {
333 for (i = 1; i < argc; i++)
334 if (!do_debug_mask(argv[i], 1))
335 fprintf(stderr, "Unknown subsystem or "
336 "debug type: %s\n", argv[i]);
341 int jt_debug_list(int argc, char **argv) {
347 if (strcasecmp(argv[1], "subs") == 0) {
348 printf("Subsystems: all_subs");
349 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
350 printf(", %s", portal_debug_subsystems[i]);
352 } else if (strcasecmp(argv[1], "types") == 0) {
353 printf("Types: all_types");
354 for (i = 0; portal_debug_masks[i] != NULL; i++)
355 printf(", %s", portal_debug_masks[i]);
361 int jt_debug_modules(int argc, char **argv) {
362 char *modules[] = {"portals", "ksocknal", "obdclass", "ptlrpc",
363 "obdext2", "ost", "osc", "mds", "mdc", "llite",
364 "obdecho", "ldlm", "obdfilter", "extN", "lov",
365 "mds_ext2", "mds_ext3", "mds_extN", NULL};
366 char *paths[] = {"portals/linux/oslib", "portals/linux/socknal",
367 "lustre/obdclass", "lustre/ptlrpc", "lustre/obdext2",
368 "lustre/ost", "lustre/osc", "lustre/mds", "lustre/mdc",
369 "lustre/llite", "lustre/obdecho", "lustre/ldlm",
370 "lustre/obdfilter", "lustre/extN", "lustre/lov",
371 "lustre/mds", "lustre/mds", "lustre/mds", NULL};
373 char *kernel = "linux";
381 printf("%s [path] [kernel]\n", argv[0]);
385 printf("set height 1000\n"
392 for (i = 0; modules[i] != NULL; i++) {
393 struct module_info info;
396 int query_module(const char *name, int which, void *buf,
397 size_t bufsize, size_t *ret);
399 rc = query_module(modules[i], QM_INFO, &info, sizeof(info),
403 printf("query_module(%s) failed: %s\n",
404 modules[i], strerror(errno));
406 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
407 paths[i], modules[i],
408 info.addr + sizeof(struct module));
411 printf("set height 24\n");
416 int jt_debug_panic(int argc, char **argv) {
418 struct portal_ioctl_data data;
424 memset(&data, 0, sizeof(data));
425 if (portal_ioctl_pack(&data, &buf, max) != 0) {
426 fprintf(stderr, "portal_ioctl_pack failed.\n");
430 rc = ioctl(g_pfd, IOC_PORTAL_PANIC, buf);
432 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",
439 int jt_debug_lctl(int argc, char **argv) {
441 lctl_debug = strtoul(argv[1], NULL, 0);
443 printf("current lctl_debug: 0x%x\n", lctl_debug);