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>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
51 #ifdef HAVE_LINUX_VERSION_H
52 #include <linux/version.h>
54 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
55 #define BUG() /* workaround for module.h includes */
56 #include <linux/module.h>
58 #endif /* !HAVE_LINUX_VERSION_H */
60 #include <sys/utsname.h>
62 #include <portals/api-support.h>
63 #include <portals/ptlctl.h>
68 static char rawbuf[8192];
69 static char *buf = rawbuf;
70 static int max = 8192;
71 /*static int g_pfd = -1;*/
72 static int subsystem_mask = ~0;
73 static int debug_mask = ~0;
75 #define MAX_MARK_SIZE 100
77 static const char *portal_debug_subsystems[] =
78 {"undefined", "mdc", "mds", "osc",
79 "ost", "class", "log", "llite",
80 "rpc", "mgmt", "portals", "nal",
81 "pinger", "filter", "ptlbd", "echo",
82 "ldlm", "lov", "router", "cobd",
83 "sm", "asobd", "confobd", "lmv",
85 static const char *portal_debug_masks[] =
86 {"trace", "inode", "super", "ext2",
87 "malloc", "cache", "info", "ioctl",
88 "blocks", "net", "warning", "buffs",
89 "other", "dentry", "portals", "page",
90 "dlmtrace", "error", "emerg", "ha",
91 "rpctrace", "vfstrace", "reada", "mmap",
94 struct debug_daemon_cmd {
99 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
100 {"start", DEBUG_DAEMON_START},
101 {"stop", DEBUG_DAEMON_STOP},
105 static int do_debug_mask(char *name, int enable)
109 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
110 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
111 strcasecmp(name, "all_subs") == 0) {
112 printf("%s output from subsystem \"%s\"\n",
113 enable ? "Enabling" : "Disabling",
114 portal_debug_subsystems[i]);
116 subsystem_mask |= (1 << i);
118 subsystem_mask &= ~(1 << i);
122 for (i = 0; portal_debug_masks[i] != NULL; i++) {
123 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
124 strcasecmp(name, "all_types") == 0) {
125 printf("%s output of type \"%s\"\n",
126 enable ? "Enabling" : "Disabling",
127 portal_debug_masks[i]);
129 debug_mask |= (1 << i);
131 debug_mask &= ~(1 << i);
139 int dbg_initialize(int argc, char **argv)
144 int jt_dbg_filter(int argc, char **argv)
149 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
154 for (i = 1; i < argc; i++)
155 if (!do_debug_mask(argv[i], 0))
156 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
161 int jt_dbg_show(int argc, char **argv)
166 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
171 for (i = 1; i < argc; i++)
172 if (!do_debug_mask(argv[i], 1))
173 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
179 static int applymask(char* procpath, int value)
183 int len = snprintf(buf, 64, "%d", value);
185 int fd = open(procpath, O_WRONLY);
187 fprintf(stderr, "Unable to open %s: %s\n",
188 procpath, strerror(errno));
191 rc = write(fd, buf, len+1);
193 fprintf(stderr, "Write to %s failed: %s\n",
194 procpath, strerror(errno));
201 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
203 if (!dump_filename) {
204 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
205 applymask("/proc/sys/portals/debug", debug_mask);
207 struct portals_debug_ioctl_data data;
209 data.hdr.ioc_len = sizeof(data);
210 data.hdr.ioc_version = 0;
211 data.subs = subs_mask;
212 data.debug = debug_mask;
214 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
216 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
217 subs_mask, debug_mask);
220 int jt_dbg_list(int argc, char **argv)
225 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
229 if (strcasecmp(argv[1], "subs") == 0) {
230 printf("Subsystems: all_subs");
231 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
232 printf(", %s", portal_debug_subsystems[i]);
234 } else if (strcasecmp(argv[1], "types") == 0) {
235 printf("Types: all_types");
236 for (i = 0; portal_debug_masks[i] != NULL; i++)
237 printf(", %s", portal_debug_masks[i]);
239 } else if (strcasecmp(argv[1], "applymasks") == 0) {
240 applymask_all(subsystem_mask, debug_mask);
245 /* all strings nul-terminated; only the struct and hdr need to be freed */
247 struct ptldebug_header *hdr;
251 struct list_head chain;
255 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
257 struct list_head *pos;
258 struct dbg_line *curr;
260 list_for_each(pos, head) {
261 curr = list_entry(pos, struct dbg_line, chain);
263 if (curr->hdr->ph_sec < new->hdr->ph_sec)
265 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
266 curr->hdr->ph_usec < new->hdr->ph_usec)
269 list_add(&new->chain, pos->prev);
272 list_add_tail(&new->chain, head);
275 static void print_saved_records(struct list_head *list, FILE *out)
277 struct list_head *pos, *tmp;
279 list_for_each_safe(pos, tmp, list) {
280 struct dbg_line *line;
281 struct ptldebug_header *hdr;
283 line = list_entry(pos, struct dbg_line, chain);
284 list_del(&line->chain);
287 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
288 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
289 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
290 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
291 line->file, hdr->ph_line_num, line->fn, line->text);
297 static int parse_buffer(FILE *in, FILE *out)
299 struct dbg_line *line;
300 struct ptldebug_header *hdr;
303 unsigned long dropped = 0, kept = 0;
304 struct list_head chunk_list;
306 INIT_LIST_HEAD(&chunk_list);
309 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
314 if (hdr->ph_len == 0)
316 if (hdr->ph_len > 4094) {
317 fprintf(stderr, "unexpected large record: %d bytes. "
323 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
324 print_saved_records(&chunk_list, out);
325 assert(list_empty(&chunk_list));
328 rc = fread(buf + sizeof(hdr->ph_len), 1,
329 hdr->ph_len - sizeof(hdr->ph_len), in);
334 (!(subsystem_mask & hdr->ph_subsys) ||
335 (!(debug_mask & hdr->ph_mask)))) {
340 line = malloc(sizeof(*line));
342 fprintf(stderr, "malloc failed; printing accumulated "
343 "records and exiting.\n");
347 line->hdr = malloc(hdr->ph_len + 1);
348 if (line->hdr == NULL) {
349 fprintf(stderr, "malloc failed; printing accumulated "
350 "records and exiting.\n");
354 p = (void *)line->hdr;
355 memcpy(line->hdr, buf, hdr->ph_len);
356 p[hdr->ph_len] = '\0';
360 p += strlen(line->file) + 1;
362 p += strlen(line->fn) + 1;
365 list_add_ordered(line, &chunk_list);
369 print_saved_records(&chunk_list, out);
371 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
372 dropped + kept, kept, dropped);
376 int jt_dbg_debug_kernel(int argc, char **argv)
381 FILE *in, *out = stdout;
384 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
390 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
395 /* If we are dumping raw (which means no conversion step to ASCII)
396 * then dump directly to any supplied filename, otherwise this is
397 * just a temp file and we dump to the real file at convert time. */
399 strcpy(filename, argv[1]);
401 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
403 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
406 fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
408 fprintf(stderr, "open(dump_kernel) failed: %s\n",
413 rc = write(fd, filename, strlen(filename));
414 if (rc != strlen(filename)) {
415 fprintf(stderr, "write(%s) failed: %s\n", filename,
425 in = fopen(filename, "r");
427 if (errno == ENOENT) /* no dump file created */
430 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
435 out = fopen(argv[1], "w");
437 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
444 rc = parse_buffer(in, out);
449 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
450 "behind.\n", filename);
452 rc = unlink(filename);
454 fprintf(stderr, "dumped successfully, but couldn't "
455 "unlink tmp file %s: %s\n", filename,
461 int jt_dbg_debug_file(int argc, char **argv)
464 FILE *in, *out = stdout;
465 if (argc > 3 || argc < 2) {
466 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
470 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
472 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
476 in = fdopen(fdin, "r");
478 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
484 fdout = open(argv[2], O_CREAT | O_WRONLY | O_LARGEFILE);
486 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
491 out = fdopen(fdout, "w");
493 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
501 return parse_buffer(in, out);
505 dbg_write_cmd(int fd, char *str)
507 int len = strlen(str);
508 int rc = write(fd, str, len);
510 return (rc == len ? 0 : 1);
513 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
514 #define DAEMON_FILE "/proc/sys/portals/daemon_file"
515 int jt_dbg_debug_daemon(int argc, char **argv)
521 fprintf(stderr, debug_daemon_usage, argv[0]);
525 fd = open(DAEMON_FILE, O_WRONLY);
527 fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
533 if (strcasecmp(argv[1], "start") == 0) {
535 fprintf(stderr, debug_daemon_usage);
539 rc = write(fd, argv[2], strlen(argv[2]));
540 if (rc != strlen(argv[2])) {
541 fprintf(stderr, "write(%s) failed: %s\n", argv[2],
548 if (strcasecmp(argv[1], "stop") == 0) {
549 rc = dbg_write_cmd(fd, "stop");
551 fprintf(stderr, "stopping debug_daemon failed: %s\n",
560 fprintf(stderr, debug_daemon_usage, argv[0]);
567 int jt_dbg_clear_debug_buf(int argc, char **argv)
570 struct portal_ioctl_data data;
573 fprintf(stderr, "usage: %s\n", argv[0]);
577 memset(&data, 0, sizeof(data));
578 if (portal_ioctl_pack(&data, &buf, max) != 0) {
579 fprintf(stderr, "portal_ioctl_pack failed.\n");
583 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
585 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
592 int jt_dbg_mark_debug_buf(int argc, char **argv)
594 int rc, max_size = MAX_MARK_SIZE-1;
595 struct portal_ioctl_data data;
597 time_t now = time(NULL);
601 text = malloc(MAX_MARK_SIZE);
602 strncpy(text, argv[1], max_size);
603 max_size-=strlen(argv[1]);
604 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
605 strncat(text, " ", 1);
607 strncat(text, argv[counter], max_size);
608 max_size-=strlen(argv[counter]);
612 text[strlen(text) - 1] = '\0'; /* stupid \n */
615 text[MAX_MARK_SIZE - 1] = '\0';
618 memset(&data, 0, sizeof(data));
619 data.ioc_inllen1 = strlen(text) + 1;
620 data.ioc_inlbuf1 = text;
621 if (portal_ioctl_pack(&data, &buf, max) != 0) {
622 fprintf(stderr, "portal_ioctl_pack failed.\n");
626 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
628 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
635 static struct mod_paths {
638 {"libcfs", "portals/libcfs"},
639 {"portals", "portals/portals"},
640 {"ksocknal", "portals/knals/socknal"},
641 {"kptlrouter", "portals/router"},
642 {"lvfs", "lustre/lvfs"},
643 {"obdclass", "lustre/obdclass"},
644 {"llog_test", "lustre/obdclass"},
645 {"ptlrpc", "lustre/ptlrpc"},
646 {"obdext2", "lustre/obdext2"},
647 {"ost", "lustre/ost"},
648 {"osc", "lustre/osc"},
649 {"mds", "lustre/mds"},
650 {"mdc", "lustre/mdc"},
651 {"llite", "lustre/llite"},
652 {"ldiskfs", "lustre/ldiskfs"},
653 {"smfs", "lustre/smfs"},
654 {"obdecho", "lustre/obdecho"},
655 {"ldlm", "lustre/ldlm"},
656 {"obdfilter", "lustre/obdfilter"},
657 {"extN", "lustre/extN"},
658 {"lov", "lustre/lov"},
659 {"lmv", "lustre/lmv"},
660 {"fsfilt_ext3", "lustre/lvfs"},
661 {"fsfilt_extN", "lustre/lvfs"},
662 {"fsfilt_reiserfs", "lustre/lvfs"},
663 {"fsfilt_smfs", "lustre/lvfs"},
664 {"fsfilt_ldiskfs", "lustre/lvfs"},
665 {"mds_ext2", "lustre/mds"},
666 {"mds_ext3", "lustre/mds"},
667 {"mds_extN", "lustre/mds"},
668 {"ptlbd", "lustre/ptlbd"},
669 {"mgmt_svc", "lustre/mgmt"},
670 {"mgmt_cli", "lustre/mgmt"},
671 {"cobd", "lustre/cobd"},
672 {"cmobd", "lustre/cmobd"},
673 {"confobd", "lustre/obdclass"},
677 static int jt_dbg_modules_2_4(int argc, char **argv)
679 #ifdef HAVE_LINUX_VERSION_H
680 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
681 struct mod_paths *mp;
683 char *kernel = "linux";
690 printf("%s [path] [kernel]\n", argv[0]);
694 for (mp = mod_paths; mp->name != NULL; mp++) {
695 struct module_info info;
698 int query_module(const char *name, int which, void *buf,
699 size_t bufsize, size_t *ret);
701 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
705 printf("query_module(%s) failed: %s\n",
706 mp->name, strerror(errno));
708 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
710 info.addr + sizeof(struct module));
715 #endif /* Headers are 2.6-only */
716 #endif /* !HAVE_LINUX_VERSION_H */
720 static int jt_dbg_modules_2_5(int argc, char **argv)
722 struct mod_paths *mp;
724 char *kernel = "linux";
725 const char *proc = "/proc/modules";
726 char modname[128], others[128];
736 printf("%s [path] [kernel]\n", argv[0]);
740 file = fopen(proc, "r");
742 printf("failed open %s: %s\n", proc, strerror(errno));
746 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
747 modname, others, others, others, others, &modaddr)) == 6) {
748 for (mp = mod_paths; mp->name != NULL; mp++) {
749 if (!strcmp(mp->name, modname))
753 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
754 mp->path, mp->name, modaddr);
761 int jt_dbg_modules(int argc, char **argv)
764 struct utsname sysinfo;
766 rc = uname(&sysinfo);
768 printf("uname() failed: %s\n", strerror(errno));
772 if (sysinfo.release[2] > '4') {
773 return jt_dbg_modules_2_5(argc, argv);
775 return jt_dbg_modules_2_4(argc, argv);
781 int jt_dbg_panic(int argc, char **argv)
784 struct portal_ioctl_data data;
787 fprintf(stderr, "usage: %s\n", argv[0]);
791 memset(&data, 0, sizeof(data));
792 if (portal_ioctl_pack(&data, &buf, max) != 0) {
793 fprintf(stderr, "portal_ioctl_pack failed.\n");
797 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
799 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",