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 <libcfs/libcfsutil.h>
70 #include <lnet/lnetctl.h>
71 #include <libcfs/portals_utils.h>
75 static char rawbuf[8192];
76 static char *buf = rawbuf;
77 static int max = 8192;
78 /*static int g_pfd = -1;*/
79 static int subsystem_mask = ~0;
80 static int debug_mask = ~0;
82 #define MAX_MARK_SIZE 256
84 static const char *libcfs_debug_subsystems[] =
85 {"undefined", "mdc", "mds", "osc",
86 "ost", "class", "log", "llite",
87 "rpc", "mgmt", "lnet", "lnd",
88 "pinger", "filter", "", "echo",
89 "ldlm", "lov", "", "",
92 "mgc", "mgs", "fid", "fld", NULL};
93 static const char *libcfs_debug_masks[] =
94 {"trace", "inode", "super", "ext2",
95 "malloc", "cache", "info", "ioctl",
96 "neterror", "net", "warning", "buffs",
97 "other", "dentry", "nettrace", "page",
98 "dlmtrace", "error", "emerg", "ha",
99 "rpctrace", "vfstrace", "reada", "mmap",
100 "config", "console", "quota", "sec", NULL};
104 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
105 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
106 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
107 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
110 dbg_open_ctlhandle(const char *str)
113 fd = open(str, O_WRONLY);
115 fprintf(stderr, "open %s failed: %s\n", str,
123 dbg_close_ctlhandle(int fd)
129 dbg_write_cmd(int fd, char *str, int len)
131 int rc = write(fd, str, len);
133 return (rc == len ? 0 : 1);
136 #elif defined(__DARWIN__)
138 #define DAEMON_CTL_NAME "lnet.trace_daemon"
139 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
140 #define DEBUG_CTL_NAME "lnet.debug"
141 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
143 static char sysctl_name[128];
145 dbg_open_ctlhandle(const char *str)
148 if (strlen(str)+1 > 128) {
149 fprintf(stderr, "sysctl name is too long: %s.\n", str);
152 strcpy(sysctl_name, str);
158 dbg_close_ctlhandle(int fd)
160 sysctl_name[0] = '\0';
165 dbg_write_cmd(int fd, char *str, int len)
169 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
171 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
172 sysctl_name, str, errno);
174 return (rc == 0 ? 0: 1);
178 #error - Unknown sysctl convention.
181 static int do_debug_mask(char *name, int enable)
185 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
186 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
187 strcasecmp(name, "all_subs") == 0) {
188 printf("%s output from subsystem \"%s\"\n",
189 enable ? "Enabling" : "Disabling",
190 libcfs_debug_subsystems[i]);
192 subsystem_mask |= (1 << i);
194 subsystem_mask &= ~(1 << i);
198 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
199 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
200 strcasecmp(name, "all_types") == 0) {
201 printf("%s output of type \"%s\"\n",
202 enable ? "Enabling" : "Disabling",
203 libcfs_debug_masks[i]);
205 debug_mask |= (1 << i);
207 debug_mask &= ~(1 << i);
215 int dbg_initialize(int argc, char **argv)
220 int jt_dbg_filter(int argc, char **argv)
225 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
230 for (i = 1; i < argc; i++)
231 if (!do_debug_mask(argv[i], 0))
232 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
237 int jt_dbg_show(int argc, char **argv)
242 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
247 for (i = 1; i < argc; i++)
248 if (!do_debug_mask(argv[i], 1))
249 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
255 static int applymask(char* procpath, int value)
259 int len = snprintf(buf, 64, "%d", value);
261 int fd = dbg_open_ctlhandle(procpath);
263 fprintf(stderr, "Unable to open %s: %s\n",
264 procpath, strerror(errno));
267 rc = dbg_write_cmd(fd, buf, len+1);
269 fprintf(stderr, "Write to %s failed: %s\n",
270 procpath, strerror(errno));
273 dbg_close_ctlhandle(fd);
277 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
279 if (!dump_filename) {
280 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
281 applymask(DEBUG_CTL_NAME, debug_mask);
283 struct libcfs_debug_ioctl_data data;
285 data.hdr.ioc_len = sizeof(data);
286 data.hdr.ioc_version = 0;
287 data.subs = subs_mask;
288 data.debug = debug_mask;
290 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
292 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
293 subs_mask, debug_mask);
296 int jt_dbg_list(int argc, char **argv)
301 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
305 if (strcasecmp(argv[1], "subs") == 0) {
306 printf("Subsystems: all_subs");
307 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
308 if (libcfs_debug_subsystems[i][0])
309 printf(", %s", libcfs_debug_subsystems[i]);
311 } else if (strcasecmp(argv[1], "types") == 0) {
312 printf("Types: all_types");
313 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
314 printf(", %s", libcfs_debug_masks[i]);
316 } else if (strcasecmp(argv[1], "applymasks") == 0) {
317 applymask_all(subsystem_mask, debug_mask);
322 /* all strings nul-terminated; only the struct and hdr need to be freed */
324 struct ptldebug_header *hdr;
330 static int cmp_rec(const void *p1, const void *p2)
332 struct dbg_line *d1 = *(struct dbg_line **)p1;
333 struct dbg_line *d2 = *(struct dbg_line **)p2;
335 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
337 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
338 d1->hdr->ph_usec < d2->hdr->ph_usec)
340 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
341 d1->hdr->ph_usec == d2->hdr->ph_usec)
346 static void print_rec(struct dbg_line **linev, int used, FILE *out)
350 for (i = 0; i < used; i++) {
351 struct dbg_line *line = linev[i];
352 struct ptldebug_header *hdr = line->hdr;
354 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
355 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
356 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
357 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
358 line->file, hdr->ph_line_num, line->fn, line->text);
365 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
368 struct dbg_line **linev = *linevp;
371 int nlen = *lenp + 512;
372 int nsize = nlen * sizeof(struct dbg_line *);
374 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
384 static int parse_buffer(FILE *in, FILE *out)
386 struct dbg_line *line;
387 struct ptldebug_header *hdr;
390 unsigned long dropped = 0, kept = 0;
391 struct dbg_line **linev = NULL;
395 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
400 if (hdr->ph_len == 0)
402 if (hdr->ph_len > 4094) {
403 fprintf(stderr, "unexpected large record: %d bytes. "
409 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
410 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
414 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
415 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
420 line = malloc(sizeof(*line));
422 fprintf(stderr, "malloc failed; printing accumulated "
423 "records and exiting.\n");
427 line->hdr = malloc(hdr->ph_len + 1);
428 if (line->hdr == NULL) {
430 fprintf(stderr, "malloc failed; printing accumulated "
431 "records and exiting.\n");
435 p = (void *)line->hdr;
436 memcpy(line->hdr, buf, hdr->ph_len);
437 p[hdr->ph_len] = '\0';
441 p += strlen(line->file) + 1;
443 p += strlen(line->fn) + 1;
446 if (!add_rec(line, &linev, &linev_len, kept)) {
447 fprintf(stderr, "malloc failed; printing accumulated "
448 "records and exiting.\n");
455 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
456 print_rec(linev, kept, out);
459 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
460 dropped + kept, kept, dropped);
464 int jt_dbg_debug_kernel(int argc, char **argv)
469 FILE *in, *out = stdout;
472 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
478 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
483 /* If we are dumping raw (which means no conversion step to ASCII)
484 * then dump directly to any supplied filename, otherwise this is
485 * just a temp file and we dump to the real file at convert time. */
487 strcpy(filename, argv[1]);
489 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
490 time(NULL),getpid());
492 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
495 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
497 fprintf(stderr, "open(dump_kernel) failed: %s\n",
502 rc = dbg_write_cmd(fd, filename, strlen(filename));
504 fprintf(stderr, "write(%s) failed: %s\n", filename,
509 dbg_close_ctlhandle(fd);
514 in = fopen(filename, "r");
516 if (errno == ENOENT) /* no dump file created */
519 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
524 out = fopen(argv[1], "w");
526 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
533 rc = parse_buffer(in, out);
538 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
539 "behind.\n", filename);
541 rc = unlink(filename);
543 fprintf(stderr, "dumped successfully, but couldn't "
544 "unlink tmp file %s: %s\n", filename,
550 int jt_dbg_debug_file(int argc, char **argv)
558 if (argc > 3 || argc < 2) {
559 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
563 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
565 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
569 in = fdopen(fdin, "r");
571 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
577 fdout = open(argv[2],
578 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
581 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
586 out = fdopen(fdout, "w");
588 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
596 rc = parse_buffer(in, out);
605 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
607 int jt_dbg_debug_daemon(int argc, char **argv)
613 fprintf(stderr, debug_daemon_usage, argv[0]);
617 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
622 if (strcasecmp(argv[1], "start") == 0) {
623 if (argc < 3 || argc > 4 ||
624 (argc == 4 && strlen(argv[3]) > 5)) {
625 fprintf(stderr, debug_daemon_usage, argv[0]);
630 const long min_size = 10;
631 const long max_size = 20480;
635 size = strtoul(argv[3], &end, 0);
636 if (size < min_size ||
639 fprintf(stderr, "size %s invalid, must be in "
640 "the range %ld-%ld MB\n", argv[3],
644 snprintf(buf, sizeof(buf), "size=%ld", size);
645 rc = dbg_write_cmd(fd, buf, strlen(buf));
648 fprintf(stderr, "set %s failed: %s\n",
649 buf, strerror(errno));
654 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
656 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
657 argv[2], strerror(errno));
663 if (strcasecmp(argv[1], "stop") == 0) {
664 rc = dbg_write_cmd(fd, "stop", 4);
666 fprintf(stderr, "stopping debug_daemon failed: %s\n",
675 fprintf(stderr, debug_daemon_usage, argv[0]);
678 dbg_close_ctlhandle(fd);
682 int jt_dbg_clear_debug_buf(int argc, char **argv)
685 struct libcfs_ioctl_data data;
688 fprintf(stderr, "usage: %s\n", argv[0]);
692 memset(&data, 0, sizeof(data));
693 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
694 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
698 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
700 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
707 int jt_dbg_mark_debug_buf(int argc, char **argv)
709 static char scratch[MAX_MARK_SIZE] = { '\0' };
710 int rc, max_size = MAX_MARK_SIZE-1;
711 struct libcfs_ioctl_data data = { 0 };
713 time_t now = time(NULL);
718 strncpy(text, argv[1], max_size);
719 max_size-=strlen(argv[1]);
720 for (count = 2; (count < argc) && (max_size > 0); count++){
721 strncat(text, " ", max_size);
723 strncat(text, argv[count], max_size);
724 max_size -= strlen(argv[count]);
730 data.ioc_inllen1 = strlen(text) + 1;
731 data.ioc_inlbuf1 = text;
732 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
733 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
737 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
739 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
746 static struct mod_paths {
749 {"libcfs", "libcfs/libcfs"},
750 {"lnet", "lnet/lnet"},
751 {"kciblnd", "lnet/klnds/ciblnd"},
752 {"kgmlnd", "lnet/klnds/gmlnd"},
753 {"kmxlnd", "lnet/klnds/mxlnd"},
754 {"kiiblnd", "lnet/klnds/iiblnd"},
755 {"ko2iblnd", "lnet/klnds/o2iblnd"},
756 {"kopeniblnd", "lnet/klnds/openiblnd"},
757 {"kptllnd", "lnet/klnds/ptllnd"},
758 {"kqswlnd", "lnet/klnds/qswlnd"},
759 {"kralnd", "lnet/klnds/ralnd"},
760 {"ksocklnd", "lnet/klnds/socklnd"},
761 {"ktdilnd", "lnet/klnds/tdilnd"},
762 {"kviblnd", "lnet/klnds/viblnd"},
763 {"lvfs", "lustre/lvfs"},
764 {"obdclass", "lustre/obdclass"},
765 {"llog_test", "lustre/obdclass"},
766 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
767 {"ptlrpc", "lustre/ptlrpc"},
768 {"gks", "lustre/sec/gks"},
769 {"gkc", "lustre/sec/gks"},
770 {"ost", "lustre/ost"},
771 {"osc", "lustre/osc"},
772 {"mds", "lustre/mds"},
773 {"mdc", "lustre/mdc"},
774 {"llite", "lustre/llite"},
775 {"lustre", "lustre/llite"},
776 {"llite_lloop", "lustre/llite"},
777 {"ldiskfs", "ldiskfs/ldiskfs"},
778 {"smfs", "lustre/smfs"},
779 {"obdecho", "lustre/obdecho"},
780 {"ldlm", "lustre/ldlm"},
781 {"obdfilter", "lustre/obdfilter"},
782 {"lov", "lustre/lov"},
783 {"lmv", "lustre/lmv"},
784 {"fsfilt_ext3", "lustre/lvfs"},
785 {"fsfilt_reiserfs", "lustre/lvfs"},
786 {"fsfilt_smfs", "lustre/lvfs"},
787 {"fsfilt_ldiskfs", "lustre/lvfs"},
788 {"mds_ext3", "lustre/mds"},
789 {"cobd", "lustre/cobd"},
790 {"cmobd", "lustre/cmobd"},
791 {"lquota", "lustre/quota"},
792 {"mgs", "lustre/mgs"},
793 {"mgc", "lustre/mgc"},
794 {"mdt", "lustre/mdt"},
795 {"mdd", "lustre/mdd"},
796 {"osd", "lustre/osd"},
797 {"cmm", "lustre/cmm"},
798 {"fid", "lustre/fid"},
799 {"fld", "lustre/fld"},
803 static int jt_dbg_modules_2_4(int argc, char **argv)
805 #ifdef HAVE_LINUX_VERSION_H
806 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
807 struct mod_paths *mp;
809 char *kernel = "linux";
816 printf("%s [path] [kernel]\n", argv[0]);
820 for (mp = mod_paths; mp->name != NULL; mp++) {
821 struct module_info info;
824 int query_module(const char *name, int which, void *buf,
825 size_t bufsize, size_t *ret);
827 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
831 printf("query_module(%s) failed: %s\n",
832 mp->name, strerror(errno));
834 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
835 path[0] ? "/" : "", mp->path, mp->name,
836 info.addr + sizeof(struct module));
841 #endif // Headers are 2.6-only
842 #endif // !HAVE_LINUX_VERSION_H
846 static int jt_dbg_modules_2_5(int argc, char **argv)
848 struct mod_paths *mp;
850 char *kernel = "linux";
851 const char *proc = "/proc/modules";
852 char modname[128], others[4096];
862 printf("%s [path] [kernel]\n", argv[0]);
866 file = fopen(proc, "r");
868 printf("failed open %s: %s\n", proc, strerror(errno));
872 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
873 modname, others, others, others, others, &modaddr)) == 6) {
874 for (mp = mod_paths; mp->name != NULL; mp++) {
875 if (!strcmp(mp->name, modname))
879 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
880 path[0] ? "/" : "", mp->path, mp->name, modaddr);
888 int jt_dbg_modules(int argc, char **argv)
891 struct utsname sysinfo;
893 rc = uname(&sysinfo);
895 printf("uname() failed: %s\n", strerror(errno));
899 if (sysinfo.release[2] > '4') {
900 return jt_dbg_modules_2_5(argc, argv);
902 return jt_dbg_modules_2_4(argc, argv);
908 int jt_dbg_panic(int argc, char **argv)
911 struct libcfs_ioctl_data data;
914 fprintf(stderr, "usage: %s\n", argv[0]);
918 memset(&data, 0, sizeof(data));
919 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
920 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
924 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
926 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",