4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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
46 #include <libcfs/libcfsutil.h>
47 #include <lnet/lnetctl.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_mask = ~0;
55 static int debug_mask = ~0;
57 #define MAX_MARK_SIZE 256
59 static const char *libcfs_debug_subsystems[] =
60 {"undefined", "mdc", "mds", "osc",
61 "ost", "class", "log", "llite",
62 "rpc", "mgmt", "lnet", "lnd",
63 "pinger", "filter", "", "echo",
64 "ldlm", "lov", "lquota", "",
67 "mgc", "mgs", "fid", "fld", NULL};
68 static const char *libcfs_debug_masks[] =
69 {"trace", "inode", "super", "ext2",
70 "malloc", "cache", "info", "ioctl",
71 "neterror", "net", "warning", "buffs",
72 "other", "dentry", "nettrace", "page",
73 "dlmtrace", "error", "emerg", "ha",
74 "rpctrace", "vfstrace", "reada", "mmap",
75 "config", "console", "quota", "sec", NULL};
79 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
80 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
81 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
82 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
85 dbg_open_ctlhandle(const char *str)
88 fd = open(str, O_WRONLY);
90 fprintf(stderr, "open %s failed: %s\n", str,
98 dbg_close_ctlhandle(int fd)
104 dbg_write_cmd(int fd, char *str, int len)
106 int rc = write(fd, str, len);
108 return (rc == len ? 0 : 1);
111 #elif defined(__DARWIN__)
113 #define DAEMON_CTL_NAME "lnet.trace_daemon"
114 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
115 #define DEBUG_CTL_NAME "lnet.debug"
116 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
118 static char sysctl_name[128];
120 dbg_open_ctlhandle(const char *str)
123 if (strlen(str)+1 > 128) {
124 fprintf(stderr, "sysctl name is too long: %s.\n", str);
127 strcpy(sysctl_name, str);
133 dbg_close_ctlhandle(int fd)
135 sysctl_name[0] = '\0';
140 dbg_write_cmd(int fd, char *str, int len)
144 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
146 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
147 sysctl_name, str, errno);
149 return (rc == 0 ? 0: 1);
152 #elif defined(__WINNT__)
154 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
155 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
156 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
157 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
160 dbg_open_ctlhandle(const char *str)
163 fd = cfs_proc_open((char *)str, (int)O_WRONLY);
165 fprintf(stderr, "open %s failed: %s\n", str,
173 dbg_close_ctlhandle(int fd)
179 dbg_write_cmd(int fd, char *str, int len)
181 int rc = cfs_proc_write(fd, str, len);
183 return (rc == len ? 0 : 1);
187 #error - Unknown sysctl convention.
190 static int do_debug_mask(char *name, int enable)
194 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
195 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
196 strcasecmp(name, "all_subs") == 0) {
197 printf("%s output from subsystem \"%s\"\n",
198 enable ? "Enabling" : "Disabling",
199 libcfs_debug_subsystems[i]);
201 subsystem_mask |= (1 << i);
203 subsystem_mask &= ~(1 << i);
207 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
208 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
209 strcasecmp(name, "all_types") == 0) {
210 printf("%s output of type \"%s\"\n",
211 enable ? "Enabling" : "Disabling",
212 libcfs_debug_masks[i]);
214 debug_mask |= (1 << i);
216 debug_mask &= ~(1 << i);
224 int dbg_initialize(int argc, char **argv)
229 int jt_dbg_filter(int argc, char **argv)
234 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
239 for (i = 1; i < argc; i++)
240 if (!do_debug_mask(argv[i], 0))
241 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
246 int jt_dbg_show(int argc, char **argv)
251 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
256 for (i = 1; i < argc; i++)
257 if (!do_debug_mask(argv[i], 1))
258 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
264 static int applymask(char* procpath, int value)
268 int len = snprintf(buf, 64, "%d", value);
270 int fd = dbg_open_ctlhandle(procpath);
272 fprintf(stderr, "Unable to open %s: %s\n",
273 procpath, strerror(errno));
276 rc = dbg_write_cmd(fd, buf, len+1);
278 fprintf(stderr, "Write to %s failed: %s\n",
279 procpath, strerror(errno));
282 dbg_close_ctlhandle(fd);
287 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
289 if (!dump_filename) {
290 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
291 applymask(DEBUG_CTL_NAME, debug_mask);
293 struct libcfs_debug_ioctl_data data;
295 data.hdr.ioc_len = sizeof(data);
296 data.hdr.ioc_version = 0;
297 data.subs = subs_mask;
298 data.debug = debug_mask;
300 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
302 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
303 subs_mask, debug_mask);
306 int jt_dbg_list(int argc, char **argv)
311 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
315 if (strcasecmp(argv[1], "subs") == 0) {
316 printf("Subsystems: all_subs");
317 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
318 if (libcfs_debug_subsystems[i][0])
319 printf(", %s", libcfs_debug_subsystems[i]);
321 } else if (strcasecmp(argv[1], "types") == 0) {
322 printf("Types: all_types");
323 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
324 printf(", %s", libcfs_debug_masks[i]);
326 } else if (strcasecmp(argv[1], "applymasks") == 0) {
327 applymask_all(subsystem_mask, debug_mask);
332 /* all strings nul-terminated; only the struct and hdr need to be freed */
334 struct ptldebug_header *hdr;
340 static int cmp_rec(const void *p1, const void *p2)
342 struct dbg_line *d1 = *(struct dbg_line **)p1;
343 struct dbg_line *d2 = *(struct dbg_line **)p2;
345 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
347 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
348 d1->hdr->ph_usec < d2->hdr->ph_usec)
350 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
351 d1->hdr->ph_usec == d2->hdr->ph_usec)
356 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
358 struct dbg_line **linev = *linevp;
361 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
362 for (i = 0; i < used; i++) {
363 struct dbg_line *line = linev[i];
364 struct ptldebug_header *hdr = line->hdr;
368 ssize_t bytes_written;
370 bytes = sprintf(out, "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
371 hdr->ph_subsys, hdr->ph_mask,
372 hdr->ph_cpu_id, hdr->ph_type,
373 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
374 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
375 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
376 line->file, hdr->ph_line_num, line->fn, line->text);
378 bytes_written = write(fdout, buf, bytes);
379 if (bytes_written <= 0)
381 bytes -= bytes_written;
382 buf += bytes_written;
391 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
394 struct dbg_line **linev = *linevp;
397 int nlen = *lenp + 4096;
398 int nsize = nlen * sizeof(struct dbg_line *);
400 linev = realloc(*linevp, nsize);
412 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
414 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
415 fprintf(stderr, " len = %u\n", hdr->ph_len);
416 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
417 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
418 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
419 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
420 fprintf(stderr, " type = %u\n", hdr->ph_type);
421 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
422 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
423 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
424 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
425 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
426 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
429 #define HDR_SIZE sizeof(*hdr)
431 static int parse_buffer(int fdin, int fdout)
433 struct dbg_line *line;
434 struct ptldebug_header *hdr;
435 char buf[4097], *ptr;
436 unsigned long dropped = 0, kept = 0, bad = 0;
437 struct dbg_line **linev = NULL;
450 rc = read(fdin, ptr, count);
459 if (hdr->ph_len > 4094 || /* is this header bogus? */
460 hdr->ph_type >= libcfs_tcd_type_max() ||
461 hdr->ph_stack > 65536 ||
462 hdr->ph_sec < (1 << 30) ||
463 hdr->ph_usec > 1000000000 ||
464 hdr->ph_line_num > 65536) {
466 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
470 /* try to restart on next line */
471 while (count < HDR_SIZE && buf[count] != '\n')
473 if (buf[count] == '\n')
474 count++; /* move past '\n' */
475 if (HDR_SIZE - count > 0) {
476 int left = HDR_SIZE - count;
478 memmove(buf, buf + count, left);
487 if (hdr->ph_len == 0)
490 count = hdr->ph_len - HDR_SIZE;
492 rc = read(fdin, ptr, count);
503 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
504 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
510 line = malloc(sizeof(*line));
513 fprintf(stderr, "error: line malloc(%u): "
514 "printing accumulated records\n",
515 (unsigned int)sizeof(*line));
516 print_rec(&linev, kept, fdout);
520 fprintf(stderr, "error: line malloc(%u): exiting\n",
521 (unsigned int)sizeof(*line));
525 line->hdr = malloc(hdr->ph_len + 1);
526 if (line->hdr == NULL) {
529 fprintf(stderr, "error: hdr malloc(%u): "
530 "printing accumulated records\n",
532 print_rec(&linev, kept, fdout);
536 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
541 ptr = (void *)line->hdr;
542 memcpy(line->hdr, buf, hdr->ph_len);
543 ptr[hdr->ph_len] = '\0';
547 ptr += strlen(line->file) + 1;
549 ptr += strlen(line->fn) + 1;
553 if (add_rec(line, &linev, &linev_len, kept) < 0) {
555 fprintf(stderr, "error: add_rec[%u] failed; "
556 "print accumulated records\n",
558 print_rec(&linev, kept, fdout);
562 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
570 print_rec(&linev, kept, fdout);
572 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
573 dropped + kept + bad, kept, dropped, bad);
578 int jt_dbg_debug_kernel(int argc, char **argv)
589 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
595 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
600 /* If we are dumping raw (which means no conversion step to ASCII)
601 * then dump directly to any supplied filename, otherwise this is
602 * just a temp file and we dump to the real file at convert time. */
604 strcpy(filename, argv[1]);
606 sprintf(filename, "%s"CFS_TIME_T".%u",
607 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL), getpid());
609 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
612 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
614 fprintf(stderr, "open(dump_kernel) failed: %s\n",
619 rc = dbg_write_cmd(fdin, filename, strlen(filename));
621 dbg_close_ctlhandle(fdin);
623 fprintf(stderr, "write(%s) failed: %s\n", filename,
624 strerror(save_errno));
631 fdin = open(filename, O_RDONLY);
633 if (errno == ENOENT) /* no dump file created */
635 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
640 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
643 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
649 fdout = fileno(stdout);
652 rc = parse_buffer(fdin, fdout);
657 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
658 "behind.\n", filename);
660 rc = unlink(filename);
662 fprintf(stderr, "dumped successfully, but couldn't "
663 "unlink tmp file %s: %s\n", filename,
670 int jt_dbg_debug_file(int argc, char **argv)
676 if (argc > 3 || argc < 2) {
677 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
681 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
683 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
688 fdout = open(argv[2],
689 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
692 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
698 fdout = fileno(stdout);
701 rc = parse_buffer(fdin, fdout);
704 if (fdout != fileno(stdout))
710 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
712 int jt_dbg_debug_daemon(int argc, char **argv)
718 fprintf(stderr, debug_daemon_usage, argv[0]);
722 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
727 if (strcasecmp(argv[1], "start") == 0) {
728 if (argc < 3 || argc > 4 ||
729 (argc == 4 && strlen(argv[3]) > 5)) {
730 fprintf(stderr, debug_daemon_usage, argv[0]);
735 const long min_size = 10;
736 const long max_size = 20480;
740 size = strtoul(argv[3], &end, 0);
741 if (size < min_size ||
744 fprintf(stderr, "size %s invalid, must be in "
745 "the range %ld-%ld MB\n", argv[3],
749 snprintf(buf, sizeof(buf), "size=%ld", size);
750 rc = dbg_write_cmd(fd, buf, strlen(buf));
753 fprintf(stderr, "set %s failed: %s\n",
754 buf, strerror(errno));
759 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
761 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
762 argv[2], strerror(errno));
768 if (strcasecmp(argv[1], "stop") == 0) {
769 rc = dbg_write_cmd(fd, "stop", 4);
771 fprintf(stderr, "stopping debug_daemon failed: %s\n",
780 fprintf(stderr, debug_daemon_usage, argv[0]);
783 dbg_close_ctlhandle(fd);
787 int jt_dbg_clear_debug_buf(int argc, char **argv)
790 struct libcfs_ioctl_data data;
793 fprintf(stderr, "usage: %s\n", argv[0]);
797 memset(&data, 0, sizeof(data));
798 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
799 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
803 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
805 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
812 int jt_dbg_mark_debug_buf(int argc, char **argv)
814 static char scratch[MAX_MARK_SIZE] = { '\0' };
815 int rc, max_size = MAX_MARK_SIZE-1;
816 struct libcfs_ioctl_data data = { 0 };
818 time_t now = time(NULL);
823 strncpy(text, argv[1], max_size);
824 max_size-=strlen(argv[1]);
825 for (count = 2; (count < argc) && (max_size > 0); count++){
826 strncat(text, " ", max_size);
828 strncat(text, argv[count], max_size);
829 max_size -= strlen(argv[count]);
835 data.ioc_inllen1 = strlen(text) + 1;
836 data.ioc_inlbuf1 = text;
837 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
838 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
842 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
844 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
851 static struct mod_paths {
854 { "libcfs", "libcfs/libcfs" },
855 { "lnet", "lnet/lnet" },
856 { "kmxlnd", "lnet/klnds/mxlnd" },
857 { "ko2iblnd", "lnet/klnds/o2iblnd" },
858 { "kptllnd", "lnet/klnds/ptllnd" },
859 { "kgnilnd", "lnet/klnds/gnilnd"},
860 { "kqswlnd", "lnet/klnds/qswlnd" },
861 { "kralnd", "lnet/klnds/ralnd" },
862 { "ksocklnd", "lnet/klnds/socklnd" },
863 { "ktdilnd", "lnet/klnds/tdilnd" },
864 { "lvfs", "lustre/lvfs" },
865 { "obdclass", "lustre/obdclass" },
866 { "llog_test", "lustre/obdclass" },
867 { "ptlrpc_gss", "lustre/ptlrpc/gss" },
868 { "ptlrpc", "lustre/ptlrpc" },
869 { "gks", "lustre/sec/gks" },
870 { "gkc", "lustre/sec/gks" },
871 { "ost", "lustre/ost" },
872 { "osc", "lustre/osc" },
873 { "mds", "lustre/mds" },
874 { "mdc", "lustre/mdc" },
875 { "lustre", "lustre/llite" },
876 { "llite_lloop", "lustre/llite" },
877 { "ldiskfs", "ldiskfs/ldiskfs" },
878 { "obdecho", "lustre/obdecho" },
879 { "ldlm", "lustre/ldlm" },
880 { "obdfilter", "lustre/obdfilter" },
881 { "lov", "lustre/lov" },
882 { "lmv", "lustre/lmv" },
883 { "fsfilt_ldiskfs", "lustre/lvfs" },
884 { "lquota", "lustre/quota" },
885 { "mgs", "lustre/mgs" },
886 { "mgc", "lustre/mgc" },
887 { "mdt", "lustre/mdt" },
888 { "mdd", "lustre/mdd" },
889 { "osd", "lustre/osd" },
890 { "cmm", "lustre/cmm" },
891 {"fid", "lustre/fid"},
892 {"fld", "lustre/fld"},
893 {"lod", "lustre/lod"},
894 {"osp", "lustre/osp"},
898 int jt_dbg_modules(int argc, char **argv)
900 struct mod_paths *mp;
902 const char *proc = "/proc/modules";
903 char modname[128], buf[4096];
910 printf("%s [path] [kernel]\n", argv[0]);
914 file = fopen(proc, "r");
916 printf("failed open %s: %s\n", proc, strerror(errno));
920 while (fgets(buf, sizeof(buf), file) != NULL) {
921 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
922 for (mp = mod_paths; mp->name != NULL; mp++) {
923 if (!strcmp(mp->name, modname))
927 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
928 path, path[0] ? "/" : "",
929 mp->path, mp->name, modaddr);
938 int jt_dbg_panic(int argc, char **argv)
941 struct libcfs_ioctl_data data;
944 fprintf(stderr, "usage: %s\n", argv[0]);
948 memset(&data, 0, sizeof(data));
949 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
950 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
954 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
956 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",