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.
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
41 #define BUG() /* workaround for module.h includes */
42 #include <linux/version.h>
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 #include <linux/module.h>
48 #include <portals/api-support.h>
49 #include <portals/ptlctl.h>
52 static char rawbuf[8192];
53 static char *buf = rawbuf;
54 static int max = 8192;
55 //static int g_pfd = -1;
56 static int subsystem_mask = ~0;
57 static int debug_mask = ~0;
59 static const char *portal_debug_subsystems[] =
60 {"undefined", "mdc", "mds", "osc", "ost", "class", "log", "llite",
61 "rpc", "mgmt", "portals", "socknal", "qswnal", "pinger", "filter",
62 "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd", NULL};
63 static const char *portal_debug_masks[] =
64 {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
65 "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
66 "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
69 struct debug_daemon_cmd {
74 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
75 {"start", DEBUG_DAEMON_START},
76 {"stop", DEBUG_DAEMON_STOP},
77 {"pause", DEBUG_DAEMON_PAUSE},
78 {"continue", DEBUG_DAEMON_CONTINUE},
82 static int do_debug_mask(char *name, int enable)
86 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
87 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
88 strcasecmp(name, "all_subs") == 0) {
89 printf("%s output from subsystem \"%s\"\n",
90 enable ? "Enabling" : "Disabling",
91 portal_debug_subsystems[i]);
93 subsystem_mask |= (1 << i);
95 subsystem_mask &= ~(1 << i);
99 for (i = 0; portal_debug_masks[i] != NULL; i++) {
100 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
101 strcasecmp(name, "all_types") == 0) {
102 printf("%s output of type \"%s\"\n",
103 enable ? "Enabling" : "Disabling",
104 portal_debug_masks[i]);
106 debug_mask |= (1 << i);
108 debug_mask &= ~(1 << i);
116 int dbg_initialize(int argc, char **argv)
121 int jt_dbg_filter(int argc, char **argv)
126 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
131 for (i = 1; i < argc; i++)
132 if (!do_debug_mask(argv[i], 0))
133 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
138 int jt_dbg_show(int argc, char **argv)
143 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
148 for (i = 1; i < argc; i++)
149 if (!do_debug_mask(argv[i], 1))
150 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
156 static int applymask(char* procpath, int value)
160 int len = snprintf(buf, 64, "%d", value);
162 int fd = open(procpath, O_WRONLY);
164 fprintf(stderr, "Unable to open %s: %s\n",
165 procpath, strerror(errno));
168 rc = write(fd, buf, len+1);
170 fprintf(stderr, "Write to %s failed: %s\n",
171 procpath, strerror(errno));
178 extern char *dump_filename;
179 extern int dump(int dev_id, int opc, void *buf);
181 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
183 if (!dump_filename) {
184 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
185 applymask("/proc/sys/portals/debug", debug_mask);
187 struct portals_debug_ioctl_data data;
189 data.hdr.ioc_len = sizeof(data);
190 data.hdr.ioc_version = 0;
191 data.subs = subs_mask;
192 data.debug = debug_mask;
194 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
196 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
197 subs_mask, debug_mask);
200 int jt_dbg_list(int argc, char **argv)
205 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
209 if (strcasecmp(argv[1], "subs") == 0) {
210 printf("Subsystems: all_subs");
211 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
212 printf(", %s", portal_debug_subsystems[i]);
214 } else if (strcasecmp(argv[1], "types") == 0) {
215 printf("Types: all_types");
216 for (i = 0; portal_debug_masks[i] != NULL; i++)
217 printf(", %s", portal_debug_masks[i]);
219 } else if (strcasecmp(argv[1], "applymasks") == 0) {
220 applymask_all(subsystem_mask, debug_mask);
225 /* if 'raw' is true, don't strip the debug information from the front of the
227 static void dump_buffer(FILE *fd, char *buf, int size, int raw)
230 unsigned long subsystem, debug, dropped = 0, kept = 0;
233 p = memchr(buf, '\n', size);
236 subsystem = strtoul(buf, &z, 16);
237 debug = strtoul(z + 1, &z, 16);
240 /* for some reason %*s isn't working. */
242 if ((subsystem_mask & subsystem) &&
243 (!debug || (debug_mask & debug))) {
245 fprintf(fd, "%s\n", buf);
247 fprintf(fd, "%s\n", z);
248 //printf("%s\n", buf);
251 //fprintf(stderr, "dropping line (%lx:%lx): %s\n", subsystem, debug, buf);
260 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
261 dropped + kept, kept, dropped);
264 int jt_dbg_debug_kernel(int argc, char **argv)
268 const int databuf_size = (6 << 20);
269 struct portal_ioctl_data data, *newdata;
270 char *databuf = NULL;
273 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
278 fd = fopen(argv[1], "w");
280 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
288 databuf = malloc(databuf_size);
290 fprintf(stderr, "No memory for buffer.\n");
294 memset(&data, 0, sizeof(data));
295 data.ioc_plen1 = databuf_size;
296 data.ioc_pbuf1 = databuf;
298 if (portal_ioctl_pack(&data, &buf, max) != 0) {
299 fprintf(stderr, "portal_ioctl_pack failed.\n");
303 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_DEBUG, buf);
305 fprintf(stderr, "IOC_PORTAL_GET_DEBUG failed: %s\n",
310 newdata = (struct portal_ioctl_data *)buf;
311 if (newdata->ioc_size > 0)
312 dump_buffer(fd, databuf, newdata->ioc_size, raw);
322 int jt_dbg_debug_daemon(int argc, char **argv)
325 unsigned int cmd = 0;
327 struct portal_ioctl_data data;
330 fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
331 "continue]\n", argv[0]);
334 for (i = 0; portal_debug_daemon_cmd[i].cmd != NULL; i++) {
335 if (strcasecmp(argv[1], portal_debug_daemon_cmd[i].cmd) == 0) {
336 cmd = portal_debug_daemon_cmd[i].cmdv;
340 if (portal_debug_daemon_cmd[i].cmd == NULL) {
341 fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
342 "continue]\n", argv[0]);
345 memset(&data, 0, sizeof(data));
346 if (cmd == DEBUG_DAEMON_START) {
348 fprintf(stderr, "usage: %s [start file <#MB>|stop|"
349 "pause|continue]\n", argv[0]);
352 if (access(argv[2], F_OK) != 0) {
353 fd = fopen(argv[2], "w");
360 if (access(argv[2], W_OK) == 0)
362 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
366 data.ioc_inllen1 = strlen(argv[2]) + 1;
367 data.ioc_inlbuf1 = argv[2];
372 size = strtoul(argv[3], NULL, 0);
374 fprintf(stderr, "file size(%s): error %s\n",
375 argv[3], strerror(errno));
378 data.ioc_misc = size;
381 data.ioc_count = cmd;
382 if (portal_ioctl_pack(&data, &buf, max) != 0) {
383 fprintf(stderr, "portal_ioctl_pack failed.\n");
386 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_SET_DAEMON, buf);
388 fprintf(stderr, "IOC_PORTAL_SET_DEMON failed: %s\n",
395 int jt_dbg_debug_file(int argc, char **argv)
397 int rc, fd = -1, raw = 1;
398 FILE *output = stdout;
399 char *databuf = NULL;
402 if (argc > 4 || argc < 2) {
403 fprintf(stderr, "usage: %s <input> [output] [raw]\n", argv[0]);
407 fd = open(argv[1], O_RDONLY);
409 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
413 #warning FIXME: cleanup fstat issue here
415 #define __SYS_fstat__ SYS_fstat
417 #define __SYS_fstat__ SYS_fstat64
419 rc = syscall(__SYS_fstat__, fd, &statbuf);
421 fprintf(stderr, "fstat failed: %s\n", strerror(errno));
426 output = fopen(argv[2], "w");
427 if (output == NULL) {
428 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
437 databuf = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE,
439 if (databuf == NULL) {
440 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
444 dump_buffer(output, databuf, statbuf.st_size, raw);
448 munmap(databuf, statbuf.st_size);
449 if (output != stdout)
456 int jt_dbg_clear_debug_buf(int argc, char **argv)
459 struct portal_ioctl_data data;
462 fprintf(stderr, "usage: %s\n", argv[0]);
466 memset(&data, 0, sizeof(data));
467 if (portal_ioctl_pack(&data, &buf, max) != 0) {
468 fprintf(stderr, "portal_ioctl_pack failed.\n");
472 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
474 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
481 int jt_dbg_mark_debug_buf(int argc, char **argv)
484 struct portal_ioctl_data data;
486 time_t now = time(NULL);
489 fprintf(stderr, "usage: %s [marker text]\n", argv[0]);
497 text[strlen(text) - 1] = '\0'; /* stupid \n */
500 memset(&data, 0, sizeof(data));
501 data.ioc_inllen1 = strlen(text) + 1;
502 data.ioc_inlbuf1 = text;
503 if (portal_ioctl_pack(&data, &buf, max) != 0) {
504 fprintf(stderr, "portal_ioctl_pack failed.\n");
508 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
510 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
518 int jt_dbg_modules(int argc, char **argv)
520 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
523 } *mp, mod_paths[] = {
524 {"portals", "lustre/portals/libcfs"},
525 {"ksocknal", "lustre/portals/knals/socknal"},
526 {"obdclass", "lustre/obdclass"},
527 {"ptlrpc", "lustre/ptlrpc"},
528 {"obdext2", "lustre/obdext2"},
529 {"ost", "lustre/ost"},
530 {"osc", "lustre/osc"},
531 {"mds", "lustre/mds"},
532 {"mdc", "lustre/mdc"},
533 {"llite", "lustre/llite"},
534 {"obdecho", "lustre/obdecho"},
535 {"ldlm", "lustre/ldlm"},
536 {"obdfilter", "lustre/obdfilter"},
537 {"extN", "lustre/extN"},
538 {"lov", "lustre/lov"},
539 {"fsfilt_ext3", "lustre/obdclass"},
540 {"fsfilt_extN", "lustre/obdclass"},
541 {"mds_ext2", "lustre/mds"},
542 {"mds_ext3", "lustre/mds"},
543 {"mds_extN", "lustre/mds"},
544 {"ptlbd", "lustre/ptlbd"},
545 {"mgmt_svc", "lustre/mgmt"},
546 {"mgmt_cli", "lustre/mgmt"},
550 char *kernel = "linux";
557 printf("%s [path] [kernel]\n", argv[0]);
561 for (mp = mod_paths; mp->name != NULL; mp++) {
562 struct module_info info;
565 int query_module(const char *name, int which, void *buf,
566 size_t bufsize, size_t *ret);
568 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
572 printf("query_module(%s) failed: %s\n",
573 mp->name, strerror(errno));
575 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
577 info.addr + sizeof(struct module));
583 printf("jt_dbg_module is not yet implemented for Linux 2.5\n");
585 #endif /* linux 2.5 */
588 int jt_dbg_panic(int argc, char **argv)
591 struct portal_ioctl_data data;
594 fprintf(stderr, "usage: %s\n", argv[0]);
598 memset(&data, 0, sizeof(data));
599 if (portal_ioctl_pack(&data, &buf, max) != 0) {
600 fprintf(stderr, "portal_ioctl_pack failed.\n");
604 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
606 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",