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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 * Some day I'll split all of this functionality into a cfs_debug module
40 * of its own. That day is not today.
43 #define __USE_FILE_OFFSET64
48 #include <libcfs/libcfsutil.h>
49 #include <lnet/lnetctl.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 #define MAX_MARK_SIZE 256
61 static const char *libcfs_debug_subsystems[] =
62 {"undefined", "mdc", "mds", "osc",
63 "ost", "class", "log", "llite",
64 "rpc", "mgmt", "lnet", "lnd",
65 "pinger", "filter", "", "echo",
66 "ldlm", "lov", "lquota", "",
69 "mgc", "mgs", "fid", "fld", NULL};
70 static const char *libcfs_debug_masks[] =
71 {"trace", "inode", "super", "ext2",
72 "malloc", "cache", "info", "ioctl",
73 "neterror", "net", "warning", "buffs",
74 "other", "dentry", "nettrace", "page",
75 "dlmtrace", "error", "emerg", "ha",
76 "rpctrace", "vfstrace", "reada", "mmap",
77 "config", "console", "quota", "sec", NULL};
81 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
82 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
83 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
84 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
87 dbg_open_ctlhandle(const char *str)
90 fd = open(str, O_WRONLY);
92 fprintf(stderr, "open %s failed: %s\n", str,
100 dbg_close_ctlhandle(int fd)
106 dbg_write_cmd(int fd, char *str, int len)
108 int rc = write(fd, str, len);
110 return (rc == len ? 0 : 1);
113 #elif defined(__DARWIN__)
115 #define DAEMON_CTL_NAME "lnet.trace_daemon"
116 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
117 #define DEBUG_CTL_NAME "lnet.debug"
118 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
120 static char sysctl_name[128];
122 dbg_open_ctlhandle(const char *str)
125 if (strlen(str)+1 > 128) {
126 fprintf(stderr, "sysctl name is too long: %s.\n", str);
129 strcpy(sysctl_name, str);
135 dbg_close_ctlhandle(int fd)
137 sysctl_name[0] = '\0';
142 dbg_write_cmd(int fd, char *str, int len)
146 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
148 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
149 sysctl_name, str, errno);
151 return (rc == 0 ? 0: 1);
154 #elif defined(__WINNT__)
156 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
157 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
158 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
159 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
162 dbg_open_ctlhandle(const char *str)
165 fd = cfs_proc_open((char *)str, (int)O_WRONLY);
167 fprintf(stderr, "open %s failed: %s\n", str,
175 dbg_close_ctlhandle(int fd)
181 dbg_write_cmd(int fd, char *str, int len)
183 int rc = cfs_proc_write(fd, str, len);
185 return (rc == len ? 0 : 1);
189 #error - Unknown sysctl convention.
192 static int do_debug_mask(char *name, int enable)
196 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
197 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
198 strcasecmp(name, "all_subs") == 0) {
199 printf("%s output from subsystem \"%s\"\n",
200 enable ? "Enabling" : "Disabling",
201 libcfs_debug_subsystems[i]);
203 subsystem_mask |= (1 << i);
205 subsystem_mask &= ~(1 << i);
209 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
210 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
211 strcasecmp(name, "all_types") == 0) {
212 printf("%s output of type \"%s\"\n",
213 enable ? "Enabling" : "Disabling",
214 libcfs_debug_masks[i]);
216 debug_mask |= (1 << i);
218 debug_mask &= ~(1 << i);
226 int dbg_initialize(int argc, char **argv)
231 int jt_dbg_filter(int argc, char **argv)
236 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
241 for (i = 1; i < argc; i++)
242 if (!do_debug_mask(argv[i], 0))
243 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
248 int jt_dbg_show(int argc, char **argv)
253 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
258 for (i = 1; i < argc; i++)
259 if (!do_debug_mask(argv[i], 1))
260 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
266 static int applymask(char* procpath, int value)
270 int len = snprintf(buf, 64, "%d", value);
272 int fd = dbg_open_ctlhandle(procpath);
274 fprintf(stderr, "Unable to open %s: %s\n",
275 procpath, strerror(errno));
278 rc = dbg_write_cmd(fd, buf, len+1);
280 fprintf(stderr, "Write to %s failed: %s\n",
281 procpath, strerror(errno));
284 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 ***linevp, int used, int fdout)
360 struct dbg_line **linev = *linevp;
363 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
364 for (i = 0; i < used; i++) {
365 struct dbg_line *line = linev[i];
366 struct ptldebug_header *hdr = line->hdr;
370 ssize_t bytes_written;
372 bytes = sprintf(out, "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
373 hdr->ph_subsys, hdr->ph_mask,
374 hdr->ph_cpu_id, hdr->ph_type,
375 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
376 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
377 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
378 line->file, hdr->ph_line_num, line->fn, line->text);
380 bytes_written = write(fdout, buf, bytes);
381 if (bytes_written <= 0)
383 bytes -= bytes_written;
384 buf += bytes_written;
393 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
396 struct dbg_line **linev = *linevp;
399 int nlen = *lenp + 4096;
400 int nsize = nlen * sizeof(struct dbg_line *);
402 linev = realloc(*linevp, nsize);
414 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
416 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
417 fprintf(stderr, " len = %u\n", hdr->ph_len);
418 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
419 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
420 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
421 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
422 fprintf(stderr, " type = %u\n", hdr->ph_type);
423 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
424 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
425 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
426 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
427 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
428 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
431 #define HDR_SIZE sizeof(*hdr)
433 static int parse_buffer(int fdin, int fdout)
435 struct dbg_line *line;
436 struct ptldebug_header *hdr;
437 char buf[4097], *ptr;
438 unsigned long dropped = 0, kept = 0, bad = 0;
439 struct dbg_line **linev = NULL;
452 rc = read(fdin, ptr, count);
461 if (hdr->ph_len > 4094 || /* is this header bogus? */
462 hdr->ph_type >= libcfs_tcd_type_max() ||
463 hdr->ph_stack > 65536 ||
464 hdr->ph_sec < (1 << 30) ||
465 hdr->ph_usec > 1000000000 ||
466 hdr->ph_line_num > 65536) {
468 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
472 /* try to restart on next line */
473 while (count < HDR_SIZE && buf[count] != '\n')
475 if (buf[count] == '\n')
476 count++; /* move past '\n' */
477 if (HDR_SIZE - count > 0) {
478 int left = HDR_SIZE - count;
480 memmove(buf, buf + count, left);
489 if (hdr->ph_len == 0)
492 count = hdr->ph_len - HDR_SIZE;
494 rc = read(fdin, ptr, count);
505 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
506 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
512 line = malloc(sizeof(*line));
515 fprintf(stderr, "error: line malloc(%u): "
516 "printing accumulated records\n",
517 (unsigned int)sizeof(*line));
518 print_rec(&linev, kept, fdout);
522 fprintf(stderr, "error: line malloc(%u): exiting\n",
523 (unsigned int)sizeof(*line));
527 line->hdr = malloc(hdr->ph_len + 1);
528 if (line->hdr == NULL) {
531 fprintf(stderr, "error: hdr malloc(%u): "
532 "printing accumulated records\n",
534 print_rec(&linev, kept, fdout);
538 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
543 ptr = (void *)line->hdr;
544 memcpy(line->hdr, buf, hdr->ph_len);
545 ptr[hdr->ph_len] = '\0';
549 ptr += strlen(line->file) + 1;
551 ptr += strlen(line->fn) + 1;
555 if (add_rec(line, &linev, &linev_len, kept) < 0) {
557 fprintf(stderr, "error: add_rec[%u] failed; "
558 "print accumulated records\n",
560 print_rec(&linev, kept, fdout);
564 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
572 print_rec(&linev, kept, fdout);
574 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
575 dropped + kept + bad, kept, dropped, bad);
580 int jt_dbg_debug_kernel(int argc, char **argv)
591 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
597 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
602 /* If we are dumping raw (which means no conversion step to ASCII)
603 * then dump directly to any supplied filename, otherwise this is
604 * just a temp file and we dump to the real file at convert time. */
606 strcpy(filename, argv[1]);
608 sprintf(filename, "%s"CFS_TIME_T".%u",
609 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL), getpid());
611 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
614 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
616 fprintf(stderr, "open(dump_kernel) failed: %s\n",
621 rc = dbg_write_cmd(fdin, filename, strlen(filename));
623 dbg_close_ctlhandle(fdin);
625 fprintf(stderr, "write(%s) failed: %s\n", filename,
626 strerror(save_errno));
633 fdin = open(filename, O_RDONLY);
635 if (errno == ENOENT) /* no dump file created */
637 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
642 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
645 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
651 fdout = fileno(stdout);
654 rc = parse_buffer(fdin, fdout);
659 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
660 "behind.\n", filename);
662 rc = unlink(filename);
664 fprintf(stderr, "dumped successfully, but couldn't "
665 "unlink tmp file %s: %s\n", filename,
672 int jt_dbg_debug_file(int argc, char **argv)
678 if (argc > 3 || argc < 2) {
679 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
683 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
685 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
690 fdout = open(argv[2],
691 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
694 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
700 fdout = fileno(stdout);
703 rc = parse_buffer(fdin, fdout);
706 if (fdout != fileno(stdout))
712 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
714 int jt_dbg_debug_daemon(int argc, char **argv)
720 fprintf(stderr, debug_daemon_usage, argv[0]);
724 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
729 if (strcasecmp(argv[1], "start") == 0) {
730 if (argc < 3 || argc > 4 ||
731 (argc == 4 && strlen(argv[3]) > 5)) {
732 fprintf(stderr, debug_daemon_usage, argv[0]);
737 const long min_size = 10;
738 const long max_size = 20480;
742 size = strtoul(argv[3], &end, 0);
743 if (size < min_size ||
746 fprintf(stderr, "size %s invalid, must be in "
747 "the range %ld-%ld MB\n", argv[3],
751 snprintf(buf, sizeof(buf), "size=%ld", size);
752 rc = dbg_write_cmd(fd, buf, strlen(buf));
755 fprintf(stderr, "set %s failed: %s\n",
756 buf, strerror(errno));
761 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
763 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
764 argv[2], strerror(errno));
770 if (strcasecmp(argv[1], "stop") == 0) {
771 rc = dbg_write_cmd(fd, "stop", 4);
773 fprintf(stderr, "stopping debug_daemon failed: %s\n",
782 fprintf(stderr, debug_daemon_usage, argv[0]);
785 dbg_close_ctlhandle(fd);
789 int jt_dbg_clear_debug_buf(int argc, char **argv)
792 struct libcfs_ioctl_data data;
795 fprintf(stderr, "usage: %s\n", argv[0]);
799 memset(&data, 0, sizeof(data));
800 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
801 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
805 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
807 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
814 int jt_dbg_mark_debug_buf(int argc, char **argv)
816 static char scratch[MAX_MARK_SIZE] = { '\0' };
817 int rc, max_size = MAX_MARK_SIZE-1;
818 struct libcfs_ioctl_data data = { 0 };
820 time_t now = time(NULL);
825 strncpy(text, argv[1], max_size);
826 max_size-=strlen(argv[1]);
827 for (count = 2; (count < argc) && (max_size > 0); count++){
828 strncat(text, " ", max_size);
830 strncat(text, argv[count], max_size);
831 max_size -= strlen(argv[count]);
837 data.ioc_inllen1 = strlen(text) + 1;
838 data.ioc_inlbuf1 = text;
839 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
840 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
844 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
846 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
853 static struct mod_paths {
856 {"libcfs", "libcfs/libcfs"},
857 {"lnet", "lnet/lnet"},
858 {"kmxlnd", "lnet/klnds/mxlnd"},
859 {"ko2iblnd", "lnet/klnds/o2iblnd"},
860 {"kptllnd", "lnet/klnds/ptllnd"},
861 {"kqswlnd", "lnet/klnds/qswlnd"},
862 {"kralnd", "lnet/klnds/ralnd"},
863 {"ksocklnd", "lnet/klnds/socklnd"},
864 {"ktdilnd", "lnet/klnds/tdilnd"},
865 {"lvfs", "lustre/lvfs"},
866 {"obdclass", "lustre/obdclass"},
867 {"llog_test", "lustre/obdclass"},
868 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
869 {"ptlrpc", "lustre/ptlrpc"},
870 {"gks", "lustre/sec/gks"},
871 {"gkc", "lustre/sec/gks"},
872 {"ost", "lustre/ost"},
873 {"osc", "lustre/osc"},
874 {"mds", "lustre/mds"},
875 {"mdc", "lustre/mdc"},
876 {"llite", "lustre/llite"},
877 {"lustre", "lustre/llite"},
878 {"llite_lloop", "lustre/llite"},
879 {"ldiskfs", "ldiskfs/ldiskfs"},
880 {"smfs", "lustre/smfs"},
881 {"obdecho", "lustre/obdecho"},
882 {"ldlm", "lustre/ldlm"},
883 {"obdfilter", "lustre/obdfilter"},
884 {"lov", "lustre/lov"},
885 {"lmv", "lustre/lmv"},
886 {"fsfilt_ext3", "lustre/lvfs"},
887 {"fsfilt_reiserfs", "lustre/lvfs"},
888 {"fsfilt_smfs", "lustre/lvfs"},
889 {"fsfilt_ldiskfs", "lustre/lvfs"},
890 {"mds_ext3", "lustre/mds"},
891 {"cobd", "lustre/cobd"},
892 {"cmobd", "lustre/cmobd"},
893 {"lquota", "lustre/quota"},
894 {"mgs", "lustre/mgs"},
895 {"mgc", "lustre/mgc"},
896 {"mdt", "lustre/mdt"},
897 {"mdd", "lustre/mdd"},
898 {"osd", "lustre/osd"},
899 {"cmm", "lustre/cmm"},
900 {"fid", "lustre/fid"},
901 {"fld", "lustre/fld"},
905 static int jt_dbg_modules_2_4(int argc, char **argv)
910 static int jt_dbg_modules_2_5(int argc, char **argv)
912 struct mod_paths *mp;
914 const char *proc = "/proc/modules";
915 char modname[128], buf[4096];
922 printf("%s [path] [kernel]\n", argv[0]);
926 file = fopen(proc, "r");
928 printf("failed open %s: %s\n", proc, strerror(errno));
932 while (fgets(buf, sizeof(buf), file) != NULL) {
933 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
934 for (mp = mod_paths; mp->name != NULL; mp++) {
935 if (!strcmp(mp->name, modname))
939 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
940 path, path[0] ? "/" : "",
941 mp->path, mp->name, modaddr);
950 int jt_dbg_modules(int argc, char **argv)
953 struct utsname sysinfo;
955 rc = uname(&sysinfo);
957 printf("uname() failed: %s\n", strerror(errno));
961 if (sysinfo.release[2] > '4') {
962 return jt_dbg_modules_2_5(argc, argv);
964 return jt_dbg_modules_2_4(argc, argv);
970 int jt_dbg_panic(int argc, char **argv)
973 struct libcfs_ioctl_data data;
976 fprintf(stderr, "usage: %s\n", argv[0]);
980 memset(&data, 0, sizeof(data));
981 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
982 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
986 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
988 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",