1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 * Some day I'll split all of this functionality into a cfs_debug module
38 * of its own. That day is not today.
41 #define __USE_FILE_OFFSET64
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
67 #include <sys/utsname.h>
69 #include <lnet/api-support.h>
70 #include <lnet/lnetctl.h>
71 #include <libcfs/portals_utils.h>
76 static char rawbuf[8192];
77 static char *buf = rawbuf;
78 static int max = 8192;
79 /*static int g_pfd = -1;*/
80 static int subsystem_mask = ~0;
81 static int debug_mask = ~0;
83 #define MAX_MARK_SIZE 256
85 static const char *libcfs_debug_subsystems[] =
86 {"undefined", "mdc", "mds", "osc",
87 "ost", "class", "log", "llite",
88 "rpc", "mgmt", "lnet", "lnd",
89 "pinger", "filter", "", "echo",
90 "ldlm", "lov", "", "",
93 "mgc", "mgs", "fid", "fld", NULL};
94 static const char *libcfs_debug_masks[] =
95 {"trace", "inode", "super", "ext2",
96 "malloc", "cache", "info", "ioctl",
97 "neterror", "net", "warning", "buffs",
98 "other", "dentry", "nettrace", "page",
99 "dlmtrace", "error", "emerg", "ha",
100 "rpctrace", "vfstrace", "reada", "mmap",
101 "config", "console", "quota", "sec", NULL};
103 struct debug_daemon_cmd {
108 static const struct debug_daemon_cmd libcfs_debug_daemon_cmd[] = {
109 {"start", DEBUG_DAEMON_START},
110 {"stop", DEBUG_DAEMON_STOP},
116 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
117 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
118 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
119 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
122 dbg_open_ctlhandle(const char *str)
125 fd = open(str, O_WRONLY);
127 fprintf(stderr, "open %s failed: %s\n", str,
135 dbg_close_ctlhandle(int fd)
141 dbg_write_cmd(int fd, char *str, int len)
143 int rc = write(fd, str, len);
145 return (rc == len ? 0 : 1);
148 #elif defined(__DARWIN__)
150 #define DAEMON_CTL_NAME "lnet.trace_daemon"
151 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
152 #define DEBUG_CTL_NAME "lnet.debug"
153 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
155 static char sysctl_name[128];
157 dbg_open_ctlhandle(const char *str)
160 if (strlen(str)+1 > 128) {
161 fprintf(stderr, "sysctl name is too long: %s.\n", str);
164 strcpy(sysctl_name, str);
170 dbg_close_ctlhandle(int fd)
172 sysctl_name[0] = '\0';
177 dbg_write_cmd(int fd, char *str, int len)
181 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
183 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
184 sysctl_name, str, errno);
186 return (rc == 0 ? 0: 1);
190 #error - Unknown sysctl convention.
193 static int do_debug_mask(char *name, int enable)
197 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
198 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
199 strcasecmp(name, "all_subs") == 0) {
200 printf("%s output from subsystem \"%s\"\n",
201 enable ? "Enabling" : "Disabling",
202 libcfs_debug_subsystems[i]);
204 subsystem_mask |= (1 << i);
206 subsystem_mask &= ~(1 << i);
210 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
211 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
212 strcasecmp(name, "all_types") == 0) {
213 printf("%s output of type \"%s\"\n",
214 enable ? "Enabling" : "Disabling",
215 libcfs_debug_masks[i]);
217 debug_mask |= (1 << i);
219 debug_mask &= ~(1 << i);
227 int dbg_initialize(int argc, char **argv)
232 int jt_dbg_filter(int argc, char **argv)
237 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
242 for (i = 1; i < argc; i++)
243 if (!do_debug_mask(argv[i], 0))
244 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
249 int jt_dbg_show(int argc, char **argv)
254 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
259 for (i = 1; i < argc; i++)
260 if (!do_debug_mask(argv[i], 1))
261 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
267 static int applymask(char* procpath, int value)
271 int len = snprintf(buf, 64, "%d", value);
273 int fd = dbg_open_ctlhandle(procpath);
275 fprintf(stderr, "Unable to open %s: %s\n",
276 procpath, strerror(errno));
279 rc = dbg_write_cmd(fd, buf, len+1);
281 fprintf(stderr, "Write to %s failed: %s\n",
282 procpath, strerror(errno));
285 dbg_close_ctlhandle(fd);
289 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
291 if (!dump_filename) {
292 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
293 applymask(DEBUG_CTL_NAME, debug_mask);
295 struct libcfs_debug_ioctl_data data;
297 data.hdr.ioc_len = sizeof(data);
298 data.hdr.ioc_version = 0;
299 data.subs = subs_mask;
300 data.debug = debug_mask;
302 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
304 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
305 subs_mask, debug_mask);
308 int jt_dbg_list(int argc, char **argv)
313 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
317 if (strcasecmp(argv[1], "subs") == 0) {
318 printf("Subsystems: all_subs");
319 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
320 if (libcfs_debug_subsystems[i][0])
321 printf(", %s", libcfs_debug_subsystems[i]);
323 } else if (strcasecmp(argv[1], "types") == 0) {
324 printf("Types: all_types");
325 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
326 printf(", %s", libcfs_debug_masks[i]);
328 } else if (strcasecmp(argv[1], "applymasks") == 0) {
329 applymask_all(subsystem_mask, debug_mask);
334 /* all strings nul-terminated; only the struct and hdr need to be freed */
336 struct ptldebug_header *hdr;
342 static int cmp_rec(const void *p1, const void *p2)
344 struct dbg_line *d1 = *(struct dbg_line **)p1;
345 struct dbg_line *d2 = *(struct dbg_line **)p2;
347 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
349 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
350 d1->hdr->ph_usec < d2->hdr->ph_usec)
352 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
353 d1->hdr->ph_usec == d2->hdr->ph_usec)
358 static void print_rec(struct dbg_line **linev, int used, FILE *out)
362 for (i = 0; i < used; i++) {
363 struct dbg_line *line = linev[i];
364 struct ptldebug_header *hdr = line->hdr;
366 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
367 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
368 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
369 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
370 line->file, hdr->ph_line_num, line->fn, line->text);
377 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
380 struct dbg_line **linev = *linevp;
383 int nlen = *lenp + 512;
384 int nsize = nlen * sizeof(struct dbg_line *);
386 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
396 static int parse_buffer(FILE *in, FILE *out)
398 struct dbg_line *line;
399 struct ptldebug_header *hdr;
402 unsigned long dropped = 0, kept = 0;
403 struct dbg_line **linev = NULL;
407 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
412 if (hdr->ph_len == 0)
414 if (hdr->ph_len > 4094) {
415 fprintf(stderr, "unexpected large record: %d bytes. "
421 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
422 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
426 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
427 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
432 line = malloc(sizeof(*line));
434 fprintf(stderr, "malloc failed; printing accumulated "
435 "records and exiting.\n");
439 line->hdr = malloc(hdr->ph_len + 1);
440 if (line->hdr == NULL) {
442 fprintf(stderr, "malloc failed; printing accumulated "
443 "records and exiting.\n");
447 p = (void *)line->hdr;
448 memcpy(line->hdr, buf, hdr->ph_len);
449 p[hdr->ph_len] = '\0';
453 p += strlen(line->file) + 1;
455 p += strlen(line->fn) + 1;
458 if (!add_rec(line, &linev, &linev_len, kept)) {
459 fprintf(stderr, "malloc failed; printing accumulated "
460 "records and exiting.\n");
467 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
468 print_rec(linev, kept, out);
471 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
472 dropped + kept, kept, dropped);
476 int jt_dbg_debug_kernel(int argc, char **argv)
481 FILE *in, *out = stdout;
484 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
490 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
495 /* If we are dumping raw (which means no conversion step to ASCII)
496 * then dump directly to any supplied filename, otherwise this is
497 * just a temp file and we dump to the real file at convert time. */
499 strcpy(filename, argv[1]);
501 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
502 time(NULL),getpid());
504 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
507 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
509 fprintf(stderr, "open(dump_kernel) failed: %s\n",
514 rc = dbg_write_cmd(fd, filename, strlen(filename));
516 fprintf(stderr, "write(%s) failed: %s\n", filename,
521 dbg_close_ctlhandle(fd);
526 in = fopen(filename, "r");
528 if (errno == ENOENT) /* no dump file created */
531 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
536 out = fopen(argv[1], "w");
538 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
545 rc = parse_buffer(in, out);
550 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
551 "behind.\n", filename);
553 rc = unlink(filename);
555 fprintf(stderr, "dumped successfully, but couldn't "
556 "unlink tmp file %s: %s\n", filename,
562 int jt_dbg_debug_file(int argc, char **argv)
570 if (argc > 3 || argc < 2) {
571 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
575 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
577 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
581 in = fdopen(fdin, "r");
583 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
589 fdout = open(argv[2],
590 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
593 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
598 out = fdopen(fdout, "w");
600 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
608 rc = parse_buffer(in, out);
617 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
619 int jt_dbg_debug_daemon(int argc, char **argv)
625 fprintf(stderr, debug_daemon_usage, argv[0]);
629 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
634 if (strcasecmp(argv[1], "start") == 0) {
635 if (argc < 3 || argc > 4 ||
636 (argc == 4 && strlen(argv[3]) > 5)) {
637 fprintf(stderr, debug_daemon_usage, argv[0]);
642 const long min_size = 10;
643 const long max_size = 20480;
647 size = strtoul(argv[3], &end, 0);
648 if (size < min_size ||
651 fprintf(stderr, "size %s invalid, must be in "
652 "the range %ld-%ld MB\n", argv[3],
656 snprintf(buf, sizeof(buf), "size=%ld", size);
657 rc = dbg_write_cmd(fd, buf, strlen(buf));
660 fprintf(stderr, "set %s failed: %s\n",
661 buf, strerror(errno));
666 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
668 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
669 argv[2], strerror(errno));
675 if (strcasecmp(argv[1], "stop") == 0) {
676 rc = dbg_write_cmd(fd, "stop", 4);
678 fprintf(stderr, "stopping debug_daemon failed: %s\n",
687 fprintf(stderr, debug_daemon_usage, argv[0]);
690 dbg_close_ctlhandle(fd);
694 int jt_dbg_clear_debug_buf(int argc, char **argv)
697 struct libcfs_ioctl_data data;
700 fprintf(stderr, "usage: %s\n", argv[0]);
704 memset(&data, 0, sizeof(data));
705 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
706 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
710 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
712 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
719 int jt_dbg_mark_debug_buf(int argc, char **argv)
721 static char scratch[MAX_MARK_SIZE] = { '\0' };
722 int rc, max_size = MAX_MARK_SIZE-1;
723 struct libcfs_ioctl_data data = { 0 };
725 time_t now = time(NULL);
730 strncpy(text, argv[1], max_size);
731 max_size-=strlen(argv[1]);
732 for (count = 2; (count < argc) && (max_size > 0); count++){
733 strncat(text, " ", max_size);
735 strncat(text, argv[count], max_size);
736 max_size -= strlen(argv[count]);
742 data.ioc_inllen1 = strlen(text) + 1;
743 data.ioc_inlbuf1 = text;
744 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
745 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
749 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
751 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
758 static struct mod_paths {
761 {"libcfs", "libcfs/libcfs"},
762 {"lnet", "lnet/lnet"},
763 {"kciblnd", "lnet/klnds/ciblnd"},
764 {"kgmlnd", "lnet/klnds/gmlnd"},
765 {"kmxlnd", "lnet/klnds/mxlnd"},
766 {"kiiblnd", "lnet/klnds/iiblnd"},
767 {"ko2iblnd", "lnet/klnds/o2iblnd"},
768 {"kopeniblnd", "lnet/klnds/openiblnd"},
769 {"kptllnd", "lnet/klnds/ptllnd"},
770 {"kqswlnd", "lnet/klnds/qswlnd"},
771 {"kralnd", "lnet/klnds/ralnd"},
772 {"ksocklnd", "lnet/klnds/socklnd"},
773 {"ktdilnd", "lnet/klnds/tdilnd"},
774 {"kviblnd", "lnet/klnds/viblnd"},
775 {"lvfs", "lustre/lvfs"},
776 {"obdclass", "lustre/obdclass"},
777 {"llog_test", "lustre/obdclass"},
778 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
779 {"ptlrpc", "lustre/ptlrpc"},
780 {"gks", "lustre/sec/gks"},
781 {"gkc", "lustre/sec/gks"},
782 {"ost", "lustre/ost"},
783 {"osc", "lustre/osc"},
784 {"mds", "lustre/mds"},
785 {"mdc", "lustre/mdc"},
786 {"llite", "lustre/llite"},
787 {"lustre", "lustre/llite"},
788 {"llite_lloop", "lustre/llite"},
789 {"ldiskfs", "ldiskfs/ldiskfs"},
790 {"smfs", "lustre/smfs"},
791 {"obdecho", "lustre/obdecho"},
792 {"ldlm", "lustre/ldlm"},
793 {"obdfilter", "lustre/obdfilter"},
794 {"lov", "lustre/lov"},
795 {"lmv", "lustre/lmv"},
796 {"fsfilt_ext3", "lustre/lvfs"},
797 {"fsfilt_reiserfs", "lustre/lvfs"},
798 {"fsfilt_smfs", "lustre/lvfs"},
799 {"fsfilt_ldiskfs", "lustre/lvfs"},
800 {"mds_ext3", "lustre/mds"},
801 {"cobd", "lustre/cobd"},
802 {"cmobd", "lustre/cmobd"},
803 {"lquota", "lustre/quota"},
804 {"mgs", "lustre/mgs"},
805 {"mgc", "lustre/mgc"},
806 {"mdt", "lustre/mdt"},
807 {"mdd", "lustre/mdd"},
808 {"osd", "lustre/osd"},
809 {"cmm", "lustre/cmm"},
810 {"fid", "lustre/fid"},
811 {"fld", "lustre/fld"},
815 static int jt_dbg_modules_2_4(int argc, char **argv)
817 #ifdef HAVE_LINUX_VERSION_H
818 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
819 struct mod_paths *mp;
821 char *kernel = "linux";
828 printf("%s [path] [kernel]\n", argv[0]);
832 for (mp = mod_paths; mp->name != NULL; mp++) {
833 struct module_info info;
836 int query_module(const char *name, int which, void *buf,
837 size_t bufsize, size_t *ret);
839 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
843 printf("query_module(%s) failed: %s\n",
844 mp->name, strerror(errno));
846 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
847 path[0] ? "/" : "", mp->path, mp->name,
848 info.addr + sizeof(struct module));
853 #endif // Headers are 2.6-only
854 #endif // !HAVE_LINUX_VERSION_H
858 static int jt_dbg_modules_2_5(int argc, char **argv)
860 struct mod_paths *mp;
862 char *kernel = "linux";
863 const char *proc = "/proc/modules";
864 char modname[128], others[4096];
874 printf("%s [path] [kernel]\n", argv[0]);
878 file = fopen(proc, "r");
880 printf("failed open %s: %s\n", proc, strerror(errno));
884 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
885 modname, others, others, others, others, &modaddr)) == 6) {
886 for (mp = mod_paths; mp->name != NULL; mp++) {
887 if (!strcmp(mp->name, modname))
891 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
892 path[0] ? "/" : "", mp->path, mp->name, modaddr);
900 int jt_dbg_modules(int argc, char **argv)
903 struct utsname sysinfo;
905 rc = uname(&sysinfo);
907 printf("uname() failed: %s\n", strerror(errno));
911 if (sysinfo.release[2] > '4') {
912 return jt_dbg_modules_2_5(argc, argv);
914 return jt_dbg_modules_2_4(argc, argv);
920 int jt_dbg_panic(int argc, char **argv)
923 struct libcfs_ioctl_data data;
926 fprintf(stderr, "usage: %s\n", argv[0]);
930 memset(&data, 0, sizeof(data));
931 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
932 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
936 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
938 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",