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[] = LIBCFS_DEBUG_SUBSYS_NAMES;
60 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
64 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
65 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
66 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
67 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
70 dbg_open_ctlhandle(const char *str)
73 fd = open(str, O_WRONLY);
75 fprintf(stderr, "open %s failed: %s\n", str,
83 dbg_close_ctlhandle(int fd)
89 dbg_write_cmd(int fd, char *str, int len)
91 int rc = write(fd, str, len);
93 return (rc == len ? 0 : 1);
96 #elif defined(__DARWIN__)
98 #define DAEMON_CTL_NAME "lnet.trace_daemon"
99 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
100 #define DEBUG_CTL_NAME "lnet.debug"
101 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
103 static char sysctl_name[128];
105 dbg_open_ctlhandle(const char *str)
108 if (strlen(str)+1 > 128) {
109 fprintf(stderr, "sysctl name is too long: %s.\n", str);
112 strcpy(sysctl_name, str);
118 dbg_close_ctlhandle(int fd)
120 sysctl_name[0] = '\0';
125 dbg_write_cmd(int fd, char *str, int len)
129 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
131 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
132 sysctl_name, str, errno);
134 return (rc == 0 ? 0: 1);
137 #elif defined(__WINNT__)
139 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
140 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
141 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
142 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
145 dbg_open_ctlhandle(const char *str)
148 fd = cfs_proc_open((char *)str, (int)O_WRONLY);
150 fprintf(stderr, "open %s failed: %s\n", str,
158 dbg_close_ctlhandle(int fd)
164 dbg_write_cmd(int fd, char *str, int len)
166 int rc = cfs_proc_write(fd, str, len);
168 return (rc == len ? 0 : 1);
172 #error - Unknown sysctl convention.
175 static int do_debug_mask(char *name, int enable)
179 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
180 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
181 strcasecmp(name, "all_subs") == 0) {
182 printf("%s output from subsystem \"%s\"\n",
183 enable ? "Enabling" : "Disabling",
184 libcfs_debug_subsystems[i]);
186 subsystem_mask |= (1 << i);
188 subsystem_mask &= ~(1 << i);
192 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
193 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
194 strcasecmp(name, "all_types") == 0) {
195 printf("%s output of type \"%s\"\n",
196 enable ? "Enabling" : "Disabling",
197 libcfs_debug_masks[i]);
199 debug_mask |= (1 << i);
201 debug_mask &= ~(1 << i);
209 int dbg_initialize(int argc, char **argv)
214 int jt_dbg_filter(int argc, char **argv)
219 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
224 for (i = 1; i < argc; i++)
225 if (!do_debug_mask(argv[i], 0))
226 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
231 int jt_dbg_show(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], 1))
243 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
249 static int applymask(char* procpath, int value)
253 int len = snprintf(buf, 64, "%d", value);
255 int fd = dbg_open_ctlhandle(procpath);
257 fprintf(stderr, "Unable to open %s: %s\n",
258 procpath, strerror(errno));
261 rc = dbg_write_cmd(fd, buf, len+1);
263 fprintf(stderr, "Write to %s failed: %s\n",
264 procpath, strerror(errno));
267 dbg_close_ctlhandle(fd);
272 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
274 if (!dump_filename) {
275 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
276 applymask(DEBUG_CTL_NAME, debug_mask);
278 struct libcfs_debug_ioctl_data data;
280 data.hdr.ioc_len = sizeof(data);
281 data.hdr.ioc_version = 0;
282 data.subs = subs_mask;
283 data.debug = debug_mask;
285 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
287 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
288 subs_mask, debug_mask);
291 int jt_dbg_list(int argc, char **argv)
296 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
300 if (strcasecmp(argv[1], "subs") == 0) {
301 printf("Subsystems: all_subs");
302 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
303 if (libcfs_debug_subsystems[i][0])
304 printf(", %s", libcfs_debug_subsystems[i]);
306 } else if (strcasecmp(argv[1], "types") == 0) {
307 printf("Types: all_types");
308 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
309 printf(", %s", libcfs_debug_masks[i]);
311 } else if (strcasecmp(argv[1], "applymasks") == 0) {
312 applymask_all(subsystem_mask, debug_mask);
317 /* all strings nul-terminated; only the struct and hdr need to be freed */
319 struct ptldebug_header *hdr;
325 static int cmp_rec(const void *p1, const void *p2)
327 struct dbg_line *d1 = *(struct dbg_line **)p1;
328 struct dbg_line *d2 = *(struct dbg_line **)p2;
330 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
332 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
333 d1->hdr->ph_usec < d2->hdr->ph_usec)
335 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
336 d1->hdr->ph_usec == d2->hdr->ph_usec)
341 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
343 struct dbg_line **linev = *linevp;
346 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
347 for (i = 0; i < used; i++) {
348 struct dbg_line *line = linev[i];
349 struct ptldebug_header *hdr = line->hdr;
353 ssize_t bytes_written;
355 bytes = sprintf(out, "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
356 hdr->ph_subsys, hdr->ph_mask,
357 hdr->ph_cpu_id, hdr->ph_type,
358 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
359 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
360 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
361 line->file, hdr->ph_line_num, line->fn, line->text);
363 bytes_written = write(fdout, buf, bytes);
364 if (bytes_written <= 0)
366 bytes -= bytes_written;
367 buf += bytes_written;
376 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
379 struct dbg_line **linev = *linevp;
382 int nlen = *lenp + 4096;
383 int nsize = nlen * sizeof(struct dbg_line *);
385 linev = realloc(*linevp, nsize);
397 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
399 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
400 fprintf(stderr, " len = %u\n", hdr->ph_len);
401 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
402 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
403 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
404 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
405 fprintf(stderr, " type = %u\n", hdr->ph_type);
406 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
407 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
408 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
409 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
410 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
411 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
414 #define HDR_SIZE sizeof(*hdr)
416 static int parse_buffer(int fdin, int fdout)
418 struct dbg_line *line;
419 struct ptldebug_header *hdr;
420 char buf[4097], *ptr;
421 unsigned long dropped = 0, kept = 0, bad = 0;
422 struct dbg_line **linev = NULL;
435 rc = read(fdin, ptr, count);
444 if (hdr->ph_len > 4094 || /* is this header bogus? */
445 hdr->ph_type >= libcfs_tcd_type_max() ||
446 hdr->ph_stack > 65536 ||
447 hdr->ph_sec < (1 << 30) ||
448 hdr->ph_usec > 1000000000 ||
449 hdr->ph_line_num > 65536) {
451 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
455 /* try to restart on next line */
456 while (count < HDR_SIZE && buf[count] != '\n')
458 if (buf[count] == '\n')
459 count++; /* move past '\n' */
460 if (HDR_SIZE - count > 0) {
461 int left = HDR_SIZE - count;
463 memmove(buf, buf + count, left);
472 if (hdr->ph_len == 0)
475 count = hdr->ph_len - HDR_SIZE;
477 rc = read(fdin, ptr, count);
488 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
489 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
495 line = malloc(sizeof(*line));
498 fprintf(stderr, "error: line malloc(%u): "
499 "printing accumulated records\n",
500 (unsigned int)sizeof(*line));
501 print_rec(&linev, kept, fdout);
505 fprintf(stderr, "error: line malloc(%u): exiting\n",
506 (unsigned int)sizeof(*line));
510 line->hdr = malloc(hdr->ph_len + 1);
511 if (line->hdr == NULL) {
514 fprintf(stderr, "error: hdr malloc(%u): "
515 "printing accumulated records\n",
517 print_rec(&linev, kept, fdout);
521 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
526 ptr = (void *)line->hdr;
527 memcpy(line->hdr, buf, hdr->ph_len);
528 ptr[hdr->ph_len] = '\0';
532 ptr += strlen(line->file) + 1;
534 ptr += strlen(line->fn) + 1;
538 if (add_rec(line, &linev, &linev_len, kept) < 0) {
540 fprintf(stderr, "error: add_rec[%u] failed; "
541 "print accumulated records\n",
543 print_rec(&linev, kept, fdout);
547 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
555 print_rec(&linev, kept, fdout);
557 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
558 dropped + kept + bad, kept, dropped, bad);
563 int jt_dbg_debug_kernel(int argc, char **argv)
566 char filename[PATH_MAX];
574 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
580 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
585 /* If we are dumping raw (which means no conversion step to ASCII)
586 * then dump directly to any supplied filename, otherwise this is
587 * just a temp file and we dump to the real file at convert time. */
588 if (argc > 1 && raw) {
589 if (strlen(argv[1]) >= sizeof(filename)) {
590 fprintf(stderr, "File name too long: %s\n", argv[1]);
593 strncpy(filename, argv[1], sizeof(filename));
595 if (snprintf(filename, sizeof(filename), "%s"CFS_TIME_T".%u",
596 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
598 >= sizeof(filename)) {
599 fprintf(stderr, "File name too long\n");
604 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
607 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
609 fprintf(stderr, "open(dump_kernel) failed: %s\n",
614 rc = dbg_write_cmd(fdin, filename, strlen(filename));
616 dbg_close_ctlhandle(fdin);
618 fprintf(stderr, "write(%s) failed: %s\n", filename,
619 strerror(save_errno));
626 fdin = open(filename, O_RDONLY);
628 if (errno == ENOENT) /* no dump file created */
630 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
635 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
638 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
644 fdout = fileno(stdout);
647 rc = parse_buffer(fdin, fdout);
652 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
653 "behind.\n", filename);
655 rc = unlink(filename);
657 fprintf(stderr, "dumped successfully, but couldn't "
658 "unlink tmp file %s: %s\n", filename,
665 int jt_dbg_debug_file(int argc, char **argv)
671 if (argc > 3 || argc < 2) {
672 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
676 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
678 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
683 fdout = open(argv[2],
684 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
687 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
693 fdout = fileno(stdout);
696 rc = parse_buffer(fdin, fdout);
699 if (fdout != fileno(stdout))
705 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
707 int jt_dbg_debug_daemon(int argc, char **argv)
713 fprintf(stderr, debug_daemon_usage, argv[0]);
717 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
722 if (strcasecmp(argv[1], "start") == 0) {
723 if (argc < 3 || argc > 4 ||
724 (argc == 4 && strlen(argv[3]) > 5)) {
725 fprintf(stderr, debug_daemon_usage, argv[0]);
730 const long min_size = 10;
731 const long max_size = 20480;
735 size = strtoul(argv[3], &end, 0);
736 if (size < min_size ||
739 fprintf(stderr, "size %s invalid, must be in "
740 "the range %ld-%ld MB\n", argv[3],
744 snprintf(buf, sizeof(buf), "size=%ld", size);
745 rc = dbg_write_cmd(fd, buf, strlen(buf));
748 fprintf(stderr, "set %s failed: %s\n",
749 buf, strerror(errno));
754 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
756 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
757 argv[2], strerror(errno));
763 if (strcasecmp(argv[1], "stop") == 0) {
764 rc = dbg_write_cmd(fd, "stop", 4);
766 fprintf(stderr, "stopping debug_daemon failed: %s\n",
775 fprintf(stderr, debug_daemon_usage, argv[0]);
778 dbg_close_ctlhandle(fd);
782 int jt_dbg_clear_debug_buf(int argc, char **argv)
785 struct libcfs_ioctl_data data;
788 fprintf(stderr, "usage: %s\n", argv[0]);
792 memset(&data, 0, sizeof(data));
793 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
794 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
798 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
800 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
807 int jt_dbg_mark_debug_buf(int argc, char **argv)
809 static char scratch[MAX_MARK_SIZE] = "";
810 struct libcfs_ioctl_data data = { 0 };
815 int count, max_size = sizeof(scratch) - 1;
817 strncpy(scratch, argv[1], max_size);
818 max_size -= strlen(argv[1]);
819 for (count = 2; (count < argc) && (max_size > 1); count++) {
820 strncat(scratch, " ", max_size);
822 strncat(scratch, argv[count], max_size);
823 max_size -= strlen(argv[count]);
825 scratch[sizeof(scratch) - 1] = '\0';
828 time_t now = time(NULL);
832 data.ioc_inllen1 = strlen(text) + 1;
833 data.ioc_inlbuf1 = text;
834 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
835 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
839 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
841 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
848 static struct mod_paths {
851 { "libcfs", "libcfs/libcfs" },
852 { "lnet", "lnet/lnet" },
853 { "kmxlnd", "lnet/klnds/mxlnd" },
854 { "ko2iblnd", "lnet/klnds/o2iblnd" },
855 { "kgnilnd", "lnet/klnds/gnilnd"},
856 { "kqswlnd", "lnet/klnds/qswlnd" },
857 { "kralnd", "lnet/klnds/ralnd" },
858 { "ksocklnd", "lnet/klnds/socklnd" },
859 { "ktdilnd", "lnet/klnds/tdilnd" },
860 { "obdclass", "lustre/obdclass" },
861 { "llog_test", "lustre/obdclass" },
862 { "ptlrpc_gss", "lustre/ptlrpc/gss" },
863 { "ptlrpc", "lustre/ptlrpc" },
864 { "gks", "lustre/sec/gks" },
865 { "gkc", "lustre/sec/gks" },
866 { "ost", "lustre/ost" },
867 { "osc", "lustre/osc" },
868 { "mds", "lustre/mds" },
869 { "mdc", "lustre/mdc" },
870 { "lustre", "lustre/llite" },
871 { "llite_lloop", "lustre/llite" },
872 { "ldiskfs", "ldiskfs" },
873 { "obdecho", "lustre/obdecho" },
874 { "ldlm", "lustre/ldlm" },
875 { "obdfilter", "lustre/obdfilter" },
876 { "lov", "lustre/lov" },
877 { "lmv", "lustre/lmv" },
878 { "lquota", "lustre/quota" },
879 { "mgs", "lustre/mgs" },
880 { "mgc", "lustre/mgc" },
881 { "mdt", "lustre/mdt" },
882 { "mdd", "lustre/mdd" },
883 { "osd", "lustre/osd" },
884 { "cmm", "lustre/cmm" },
885 {"fid", "lustre/fid"},
886 {"fld", "lustre/fld"},
887 {"lod", "lustre/lod"},
888 {"osp", "lustre/osp"},
889 { "lfsck", "lustre/lfsck" },
893 int jt_dbg_modules(int argc, char **argv)
895 struct mod_paths *mp;
897 const char *proc = "/proc/modules";
898 char modname[128], buf[4096];
899 unsigned long modaddr;
905 printf("%s [path] [kernel]\n", argv[0]);
909 file = fopen(proc, "r");
911 printf("failed open %s: %s\n", proc, strerror(errno));
915 while (fgets(buf, sizeof(buf), file) != NULL) {
916 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
917 for (mp = mod_paths; mp->name != NULL; mp++) {
918 if (!strcmp(mp->name, modname))
922 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
923 path, path[0] ? "/" : "",
924 mp->path, mp->name, modaddr);
933 int jt_dbg_panic(int argc, char **argv)
936 struct libcfs_ioctl_data data;
939 fprintf(stderr, "usage: %s\n", argv[0]);
943 memset(&data, 0, sizeof(data));
944 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
945 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
949 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
951 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",