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.
26 #define __USE_FILE_OFFSET64
29 #include <portals/list.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
48 #include <linux/version.h>
50 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
51 #define BUG() /* workaround for module.h includes */
52 #include <linux/module.h>
54 #include <sys/utsname.h>
56 #include <portals/api-support.h>
57 #include <portals/ptlctl.h>
62 static char rawbuf[8192];
63 static char *buf = rawbuf;
64 static int max = 8192;
65 //static int g_pfd = -1;
66 static int subsystem_mask = ~0;
67 static int debug_mask = ~0;
69 #define MAX_MARK_SIZE 100
71 static const char *portal_debug_subsystems[] =
72 {"undefined", "mdc", "mds", "osc", "ost", "class", "log", "llite",
73 "rpc", "mgmt", "portals", "libcfs", "socknal", "qswnal", "pinger",
74 "filter", "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd",
75 "openibnal", "lmv", "smfs", "cmobd", NULL};
76 static const char *portal_debug_masks[] =
77 {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
78 "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
79 "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
80 "reada", "mmap", NULL};
82 struct debug_daemon_cmd {
87 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
88 {"start", DEBUG_DAEMON_START},
89 {"stop", DEBUG_DAEMON_STOP},
93 static int do_debug_mask(char *name, int enable)
97 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
98 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
99 strcasecmp(name, "all_subs") == 0) {
100 printf("%s output from subsystem \"%s\"\n",
101 enable ? "Enabling" : "Disabling",
102 portal_debug_subsystems[i]);
104 subsystem_mask |= (1 << i);
106 subsystem_mask &= ~(1 << i);
110 for (i = 0; portal_debug_masks[i] != NULL; i++) {
111 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
112 strcasecmp(name, "all_types") == 0) {
113 printf("%s output of type \"%s\"\n",
114 enable ? "Enabling" : "Disabling",
115 portal_debug_masks[i]);
117 debug_mask |= (1 << i);
119 debug_mask &= ~(1 << i);
127 int dbg_initialize(int argc, char **argv)
132 int jt_dbg_filter(int argc, char **argv)
137 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
142 for (i = 1; i < argc; i++)
143 if (!do_debug_mask(argv[i], 0))
144 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
149 int jt_dbg_show(int argc, char **argv)
154 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
159 for (i = 1; i < argc; i++)
160 if (!do_debug_mask(argv[i], 1))
161 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
167 static int applymask(char* procpath, int value)
171 int len = snprintf(buf, 64, "%d", value);
173 int fd = open(procpath, O_WRONLY);
175 fprintf(stderr, "Unable to open %s: %s\n",
176 procpath, strerror(errno));
179 rc = write(fd, buf, len+1);
181 fprintf(stderr, "Write to %s failed: %s\n",
182 procpath, strerror(errno));
189 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
191 if (!dump_filename) {
192 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
193 applymask("/proc/sys/portals/debug", debug_mask);
195 struct portals_debug_ioctl_data data;
197 data.hdr.ioc_len = sizeof(data);
198 data.hdr.ioc_version = 0;
199 data.subs = subs_mask;
200 data.debug = debug_mask;
202 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
204 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
205 subs_mask, debug_mask);
208 int jt_dbg_list(int argc, char **argv)
213 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
217 if (strcasecmp(argv[1], "subs") == 0) {
218 printf("Subsystems: all_subs");
219 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
220 printf(", %s", portal_debug_subsystems[i]);
222 } else if (strcasecmp(argv[1], "types") == 0) {
223 printf("Types: all_types");
224 for (i = 0; portal_debug_masks[i] != NULL; i++)
225 printf(", %s", portal_debug_masks[i]);
227 } else if (strcasecmp(argv[1], "applymasks") == 0) {
228 applymask_all(subsystem_mask, debug_mask);
233 /* all strings nul-terminated; only the struct and hdr need to be freed */
235 struct ptldebug_header *hdr;
239 struct list_head chain;
243 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
245 struct list_head *pos;
246 struct dbg_line *curr;
248 list_for_each(pos, head) {
249 curr = list_entry(pos, struct dbg_line, chain);
251 if (curr->hdr->ph_sec < new->hdr->ph_sec)
253 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
254 curr->hdr->ph_usec < new->hdr->ph_usec)
257 list_add(&new->chain, pos->prev);
260 list_add_tail(&new->chain, head);
263 static void print_saved_records(struct list_head *list, FILE *out)
265 struct list_head *pos, *tmp;
267 list_for_each_safe(pos, tmp, list) {
268 struct dbg_line *line;
269 struct ptldebug_header *hdr;
271 line = list_entry(pos, struct dbg_line, chain);
272 list_del(&line->chain);
275 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
276 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
277 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
278 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
279 line->file, hdr->ph_line_num, line->fn, line->text);
285 static int parse_buffer(FILE *in, FILE *out)
287 struct dbg_line *line;
288 struct ptldebug_header *hdr;
291 unsigned long dropped = 0, kept = 0;
292 struct list_head chunk_list;
294 INIT_LIST_HEAD(&chunk_list);
297 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
302 if (hdr->ph_len == 0)
304 if (hdr->ph_len > 4094) {
305 fprintf(stderr, "unexpected large record: %d bytes. "
311 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
312 print_saved_records(&chunk_list, out);
313 assert(list_empty(&chunk_list));
316 rc = fread(buf + sizeof(hdr->ph_len), 1,
317 hdr->ph_len - sizeof(hdr->ph_len), in);
322 (!(subsystem_mask & hdr->ph_subsys) ||
323 (!(debug_mask & hdr->ph_mask)))) {
328 line = malloc(sizeof(*line));
330 fprintf(stderr, "malloc failed; printing accumulated "
331 "records and exiting.\n");
335 line->hdr = malloc(hdr->ph_len + 1);
336 if (line->hdr == NULL) {
337 fprintf(stderr, "malloc failed; printing accumulated "
338 "records and exiting.\n");
342 p = (void *)line->hdr;
343 memcpy(line->hdr, buf, hdr->ph_len);
344 p[hdr->ph_len] = '\0';
348 p += strlen(line->file) + 1;
350 p += strlen(line->fn) + 1;
353 list_add_ordered(line, &chunk_list);
357 print_saved_records(&chunk_list, out);
359 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
360 dropped + kept, kept, dropped);
364 int jt_dbg_debug_kernel(int argc, char **argv)
368 FILE *in, *out = stdout;
371 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
374 sprintf(filename, "%s.%lu.%u", argc > 1 ? argv[1] : "/tmp/lustre-log",
375 time(NULL), getpid());
381 fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
383 fprintf(stderr, "open(dump_kernel) failed: %s\n",
388 rc = write(fd, filename, strlen(filename));
389 if (rc != strlen(filename)) {
390 fprintf(stderr, "write(%s) failed: %s\n", filename,
400 in = fopen(filename, "r");
402 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
407 out = fopen(argv[1], "w");
409 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
416 rc = parse_buffer(in, out);
421 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
422 "behind.\n", filename);
424 rc = unlink(filename);
426 fprintf(stderr, "dumped successfully, but couldn't "
427 "unlink tmp file %s: %s\n", filename,
433 int jt_dbg_debug_file(int argc, char **argv)
436 FILE *in, *out = stdout;
437 if (argc > 3 || argc < 2) {
438 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
442 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
444 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
448 in = fdopen(fdin, "r");
450 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
456 fdout = open(argv[2], O_CREAT | O_WRONLY | O_LARGEFILE);
458 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
463 out = fdopen(fdout, "w");
465 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
473 return parse_buffer(in, out);
476 const char debug_daemon_usage[]="usage: debug_daemon {start file [MB]|stop}\n";
477 int jt_dbg_debug_daemon(int argc, char **argv)
482 fprintf(stderr, debug_daemon_usage);
486 fd = open("/proc/sys/portals/daemon_file", O_WRONLY);
488 fprintf(stderr, "open(daemon_file) failed: %s\n",
493 if (strcasecmp(argv[1], "start") == 0) {
495 fprintf(stderr, debug_daemon_usage);
499 rc = write(fd, argv[2], strlen(argv[2]));
500 if (rc != strlen(argv[2])) {
501 fprintf(stderr, "write(%s) failed: %s\n", argv[2],
506 } else if (strcasecmp(argv[1], "stop") == 0) {
507 rc = write(fd, "stop", 4);
509 fprintf(stderr, "write(stop) failed: %s\n",
515 fprintf(stderr, debug_daemon_usage);
523 int jt_dbg_clear_debug_buf(int argc, char **argv)
526 struct portal_ioctl_data data;
529 fprintf(stderr, "usage: %s\n", argv[0]);
533 memset(&data, 0, sizeof(data));
534 if (portal_ioctl_pack(&data, &buf, max) != 0) {
535 fprintf(stderr, "portal_ioctl_pack failed.\n");
539 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
541 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
548 int jt_dbg_mark_debug_buf(int argc, char **argv)
550 int rc, max_size = MAX_MARK_SIZE-1;
551 struct portal_ioctl_data data;
553 time_t now = time(NULL);
557 text = malloc(MAX_MARK_SIZE);
558 strncpy(text, argv[1], max_size);
559 max_size-=strlen(argv[1]);
560 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
561 strncat(text, " ", 1);
563 strncat(text, argv[counter], max_size);
564 max_size-=strlen(argv[counter]);
568 text[strlen(text) - 1] = '\0'; /* stupid \n */
571 text[MAX_MARK_SIZE - 1] = '\0';
574 memset(&data, 0, sizeof(data));
575 data.ioc_inllen1 = strlen(text) + 1;
576 data.ioc_inlbuf1 = text;
577 if (portal_ioctl_pack(&data, &buf, max) != 0) {
578 fprintf(stderr, "portal_ioctl_pack failed.\n");
582 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
584 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
591 static struct mod_paths {
594 {"libcfs", "lustre/portals/libcfs"},
595 {"portals", "lustre/portals/portals"},
596 {"ksocknal", "lustre/portals/knals/socknal"},
597 {"kptlrouter", "lustre/portals/router"},
598 {"lvfs", "lustre/lvfs"},
599 {"obdclass", "lustre/obdclass"},
600 {"llog_test", "lustre/obdclass"},
601 {"ptlrpc", "lustre/ptlrpc"},
602 {"obdext2", "lustre/obdext2"},
603 {"ost", "lustre/ost"},
604 {"osc", "lustre/osc"},
605 {"mds", "lustre/mds"},
606 {"mdc", "lustre/mdc"},
607 {"llite", "lustre/llite"},
608 {"smfs", "lustre/smfs"},
609 {"obdecho", "lustre/obdecho"},
610 {"ldlm", "lustre/ldlm"},
611 {"obdfilter", "lustre/obdfilter"},
612 {"extN", "lustre/extN"},
613 {"lov", "lustre/lov"},
614 {"lmv", "lustre/lmv"},
615 {"fsfilt_ext3", "lustre/lvfs"},
616 {"fsfilt_extN", "lustre/lvfs"},
617 {"fsfilt_reiserfs", "lustre/lvfs"},
618 {"fsfilt_smfs", "lustre/lvfs"},
619 {"fsfilt_ldiskfs", "lustre/lvfs"},
620 {"mds_ext2", "lustre/mds"},
621 {"mds_ext3", "lustre/mds"},
622 {"mds_extN", "lustre/mds"},
623 {"ptlbd", "lustre/ptlbd"},
624 {"mgmt_svc", "lustre/mgmt"},
625 {"mgmt_cli", "lustre/mgmt"},
626 {"cobd", "lustre/cobd"},
627 {"cmobd", "lustre/cmobd"},
631 static int jt_dbg_modules_2_4(int argc, char **argv)
633 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
634 struct mod_paths *mp;
636 char *kernel = "linux";
643 printf("%s [path] [kernel]\n", argv[0]);
647 for (mp = mod_paths; mp->name != NULL; mp++) {
648 struct module_info info;
651 int query_module(const char *name, int which, void *buf,
652 size_t bufsize, size_t *ret);
654 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
658 printf("query_module(%s) failed: %s\n",
659 mp->name, strerror(errno));
661 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
663 info.addr + sizeof(struct module));
668 #else /* Headers are 2.6-only */
673 static int jt_dbg_modules_2_5(int argc, char **argv)
675 struct mod_paths *mp;
677 char *kernel = "linux";
678 const char *proc = "/proc/modules";
679 char modname[128], others[128];
689 printf("%s [path] [kernel]\n", argv[0]);
693 file = fopen(proc, "r");
695 printf("failed open %s: %s\n", proc, strerror(errno));
699 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
700 modname, others, others, others, others, &modaddr)) == 6) {
701 for (mp = mod_paths; mp->name != NULL; mp++) {
702 if (!strcmp(mp->name, modname))
706 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
707 mp->path, mp->name, modaddr);
714 int jt_dbg_modules(int argc, char **argv)
717 struct utsname sysinfo;
719 rc = uname(&sysinfo);
721 printf("uname() failed: %s\n", strerror(errno));
725 if (sysinfo.release[2] > '4') {
726 return jt_dbg_modules_2_5(argc, argv);
728 return jt_dbg_modules_2_4(argc, argv);
734 int jt_dbg_panic(int argc, char **argv)
737 struct portal_ioctl_data data;
740 fprintf(stderr, "usage: %s\n", argv[0]);
744 memset(&data, 0, sizeof(data));
745 if (portal_ioctl_pack(&data, &buf, max) != 0) {
746 fprintf(stderr, "portal_ioctl_pack failed.\n");
750 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
752 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",