1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre Networking, http://www.lustre.org.
8 * LNET is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * LNET is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with LNET; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Some day I'll split all of this functionality into a cfs_debug module
22 * of its own. That day is not today.
26 #define __USE_FILE_OFFSET64
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
51 #include <sys/utsname.h>
53 #include <lnet/api-support.h>
54 #include <lnet/lnetctl.h>
55 #include <libcfs/portals_utils.h>
60 static char rawbuf[8192];
61 static char *buf = rawbuf;
62 static int max = 8192;
63 /*static int g_pfd = -1;*/
64 static int subsystem_mask = ~0;
65 static int debug_mask = ~0;
67 #define MAX_MARK_SIZE 256
69 static const char *libcfs_debug_subsystems[] =
70 {"undefined", "mdc", "mds", "osc",
71 "ost", "class", "log", "llite",
72 "rpc", "mgmt", "lnet", "lnd",
73 "pinger", "filter", "", "echo",
74 "ldlm", "lov", "", "",
77 "mgc", "mgs", "fid", "fld", NULL};
78 static const char *libcfs_debug_masks[] =
79 {"trace", "inode", "super", "ext2",
80 "malloc", "cache", "info", "ioctl",
81 "neterror", "net", "warning", "buffs",
82 "other", "dentry", "nettrace", "page",
83 "dlmtrace", "error", "emerg", "ha",
84 "rpctrace", "vfstrace", "reada", "mmap",
85 "config", "console", "quota", "sec", NULL};
87 struct debug_daemon_cmd {
92 static const struct debug_daemon_cmd libcfs_debug_daemon_cmd[] = {
93 {"start", DEBUG_DAEMON_START},
94 {"stop", DEBUG_DAEMON_STOP},
100 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
101 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
102 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
103 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
106 dbg_open_ctlhandle(const char *str)
109 fd = open(str, O_WRONLY);
111 fprintf(stderr, "open %s failed: %s\n", str,
119 dbg_close_ctlhandle(int fd)
125 dbg_write_cmd(int fd, char *str, int len)
127 int rc = write(fd, str, len);
129 return (rc == len ? 0 : 1);
132 #elif defined(__DARWIN__)
134 #define DAEMON_CTL_NAME "lnet.trace_daemon"
135 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
136 #define DEBUG_CTL_NAME "lnet.debug"
137 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
139 static char sysctl_name[128];
141 dbg_open_ctlhandle(const char *str)
144 if (strlen(str)+1 > 128) {
145 fprintf(stderr, "sysctl name is too long: %s.\n", str);
148 strcpy(sysctl_name, str);
154 dbg_close_ctlhandle(int fd)
156 sysctl_name[0] = '\0';
161 dbg_write_cmd(int fd, char *str, int len)
165 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
167 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
168 sysctl_name, str, errno);
170 return (rc == 0 ? 0: 1);
174 #error - Unknown sysctl convention.
177 static int do_debug_mask(char *name, int enable)
181 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
182 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
183 strcasecmp(name, "all_subs") == 0) {
184 printf("%s output from subsystem \"%s\"\n",
185 enable ? "Enabling" : "Disabling",
186 libcfs_debug_subsystems[i]);
188 subsystem_mask |= (1 << i);
190 subsystem_mask &= ~(1 << i);
194 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
195 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
196 strcasecmp(name, "all_types") == 0) {
197 printf("%s output of type \"%s\"\n",
198 enable ? "Enabling" : "Disabling",
199 libcfs_debug_masks[i]);
201 debug_mask |= (1 << i);
203 debug_mask &= ~(1 << i);
211 int dbg_initialize(int argc, char **argv)
216 int jt_dbg_filter(int argc, char **argv)
221 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
226 for (i = 1; i < argc; i++)
227 if (!do_debug_mask(argv[i], 0))
228 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
233 int jt_dbg_show(int argc, char **argv)
238 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
243 for (i = 1; i < argc; i++)
244 if (!do_debug_mask(argv[i], 1))
245 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
251 static int applymask(char* procpath, int value)
255 int len = snprintf(buf, 64, "%d", value);
257 int fd = dbg_open_ctlhandle(procpath);
259 fprintf(stderr, "Unable to open %s: %s\n",
260 procpath, strerror(errno));
263 rc = dbg_write_cmd(fd, buf, len+1);
265 fprintf(stderr, "Write to %s failed: %s\n",
266 procpath, strerror(errno));
269 dbg_close_ctlhandle(fd);
273 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
275 if (!dump_filename) {
276 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
277 applymask(DEBUG_CTL_NAME, debug_mask);
279 struct libcfs_debug_ioctl_data data;
281 data.hdr.ioc_len = sizeof(data);
282 data.hdr.ioc_version = 0;
283 data.subs = subs_mask;
284 data.debug = debug_mask;
286 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
288 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
289 subs_mask, debug_mask);
292 int jt_dbg_list(int argc, char **argv)
297 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
301 if (strcasecmp(argv[1], "subs") == 0) {
302 printf("Subsystems: all_subs");
303 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
304 if (libcfs_debug_subsystems[i][0])
305 printf(", %s", libcfs_debug_subsystems[i]);
307 } else if (strcasecmp(argv[1], "types") == 0) {
308 printf("Types: all_types");
309 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
310 printf(", %s", libcfs_debug_masks[i]);
312 } else if (strcasecmp(argv[1], "applymasks") == 0) {
313 applymask_all(subsystem_mask, debug_mask);
318 /* all strings nul-terminated; only the struct and hdr need to be freed */
320 struct ptldebug_header *hdr;
326 static int cmp_rec(const void *p1, const void *p2)
328 struct dbg_line *d1 = *(struct dbg_line **)p1;
329 struct dbg_line *d2 = *(struct dbg_line **)p2;
331 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
333 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
334 d1->hdr->ph_usec < d2->hdr->ph_usec)
336 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
337 d1->hdr->ph_usec == d2->hdr->ph_usec)
342 static void print_rec(struct dbg_line **linev, int used, FILE *out)
346 for (i = 0; i < used; i++) {
347 struct dbg_line *line = linev[i];
348 struct ptldebug_header *hdr = line->hdr;
350 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
351 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
352 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
353 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
354 line->file, hdr->ph_line_num, line->fn, line->text);
361 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
364 struct dbg_line **linev = *linevp;
367 int nlen = *lenp + 512;
368 int nsize = nlen * sizeof(struct dbg_line *);
370 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
380 static int parse_buffer(FILE *in, FILE *out)
382 struct dbg_line *line;
383 struct ptldebug_header *hdr;
386 unsigned long dropped = 0, kept = 0;
387 struct dbg_line **linev = NULL;
391 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
396 if (hdr->ph_len == 0)
398 if (hdr->ph_len > 4094) {
399 fprintf(stderr, "unexpected large record: %d bytes. "
405 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
406 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
411 (!(subsystem_mask & hdr->ph_subsys) ||
412 (!(debug_mask & hdr->ph_mask)))) {
417 line = malloc(sizeof(*line));
419 fprintf(stderr, "malloc failed; printing accumulated "
420 "records and exiting.\n");
424 line->hdr = malloc(hdr->ph_len + 1);
425 if (line->hdr == NULL) {
427 fprintf(stderr, "malloc failed; printing accumulated "
428 "records and exiting.\n");
432 p = (void *)line->hdr;
433 memcpy(line->hdr, buf, hdr->ph_len);
434 p[hdr->ph_len] = '\0';
438 p += strlen(line->file) + 1;
440 p += strlen(line->fn) + 1;
443 if (!add_rec(line, &linev, &linev_len, kept)) {
444 fprintf(stderr, "malloc failed; printing accumulated "
445 "records and exiting.\n");
452 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
453 print_rec(linev, kept, out);
456 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
457 dropped + kept, kept, dropped);
461 int jt_dbg_debug_kernel(int argc, char **argv)
466 FILE *in, *out = stdout;
469 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
475 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
480 /* If we are dumping raw (which means no conversion step to ASCII)
481 * then dump directly to any supplied filename, otherwise this is
482 * just a temp file and we dump to the real file at convert time. */
484 strcpy(filename, argv[1]);
486 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
488 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
491 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
493 fprintf(stderr, "open(dump_kernel) failed: %s\n",
498 rc = dbg_write_cmd(fd, filename, strlen(filename));
500 fprintf(stderr, "write(%s) failed: %s\n", filename,
505 dbg_close_ctlhandle(fd);
510 in = fopen(filename, "r");
512 if (errno == ENOENT) /* no dump file created */
515 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
520 out = fopen(argv[1], "w");
522 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
529 rc = parse_buffer(in, out);
534 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
535 "behind.\n", filename);
537 rc = unlink(filename);
539 fprintf(stderr, "dumped successfully, but couldn't "
540 "unlink tmp file %s: %s\n", filename,
546 int jt_dbg_debug_file(int argc, char **argv)
554 if (argc > 3 || argc < 2) {
555 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
559 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
561 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
565 in = fdopen(fdin, "r");
567 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
573 fdout = open(argv[2],
574 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
577 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
582 out = fdopen(fdout, "w");
584 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
592 rc = parse_buffer(in, out);
601 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
603 int jt_dbg_debug_daemon(int argc, char **argv)
609 fprintf(stderr, debug_daemon_usage, argv[0]);
613 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
618 if (strcasecmp(argv[1], "start") == 0) {
619 if (argc < 3 || argc > 4 ||
620 (argc == 4 && strlen(argv[3]) > 5)) {
621 fprintf(stderr, debug_daemon_usage, argv[0]);
626 const long min_size = 10;
627 const long max_size = 20480;
631 size = strtoul(argv[3], &end, 0);
632 if (size < min_size ||
635 fprintf(stderr, "size %s invalid, must be in "
636 "the range %ld-%ld MB\n", argv[3],
640 snprintf(buf, sizeof(buf), "size=%ld", size);
641 rc = dbg_write_cmd(fd, buf, strlen(buf));
644 fprintf(stderr, "set %s failed: %s\n",
645 buf, strerror(errno));
650 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
652 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
653 argv[2], strerror(errno));
659 if (strcasecmp(argv[1], "stop") == 0) {
660 rc = dbg_write_cmd(fd, "stop", 4);
662 fprintf(stderr, "stopping debug_daemon failed: %s\n",
671 fprintf(stderr, debug_daemon_usage, argv[0]);
674 dbg_close_ctlhandle(fd);
678 int jt_dbg_clear_debug_buf(int argc, char **argv)
681 struct libcfs_ioctl_data data;
684 fprintf(stderr, "usage: %s\n", argv[0]);
688 memset(&data, 0, sizeof(data));
689 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
690 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
694 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
696 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
703 int jt_dbg_mark_debug_buf(int argc, char **argv)
705 static char scratch[MAX_MARK_SIZE] = { '\0' };
706 int rc, max_size = MAX_MARK_SIZE-1;
707 struct libcfs_ioctl_data data = { 0 };
709 time_t now = time(NULL);
714 strncpy(text, argv[1], max_size);
715 max_size-=strlen(argv[1]);
716 for (count = 2; (count < argc) && (max_size > 0); count++){
717 strncat(text, " ", max_size);
719 strncat(text, argv[count], max_size);
720 max_size -= strlen(argv[count]);
726 data.ioc_inllen1 = strlen(text) + 1;
727 data.ioc_inlbuf1 = text;
728 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
729 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
733 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
735 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
742 static struct mod_paths {
745 {"libcfs", "lnet/libcfs"},
746 {"lnet", "lnet/lnet"},
747 {"kciblnd", "lnet/klnds/ciblnd"},
748 {"kgmlnd", "lnet/klnds/gmlnd"},
749 {"kmxlnd", "lnet/klnds/mxlnd"},
750 {"kiiblnd", "lnet/klnds/iiblnd"},
751 {"ko2iblnd", "lnet/klnds/o2iblnd"},
752 {"kopeniblnd", "lnet/klnds/openiblnd"},
753 {"kptllnd", "lnet/klnds/ptllnd"},
754 {"kqswlnd", "lnet/klnds/qswlnd"},
755 {"kralnd", "lnet/klnds/ralnd"},
756 {"ksocklnd", "lnet/klnds/socklnd"},
757 {"ktdilnd", "lnet/klnds/tdilnd"},
758 {"kviblnd", "lnet/klnds/viblnd"},
759 {"lvfs", "lustre/lvfs"},
760 {"obdclass", "lustre/obdclass"},
761 {"llog_test", "lustre/obdclass"},
762 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
763 {"ptlrpc", "lustre/ptlrpc"},
764 {"gks", "lustre/sec/gks"},
765 {"gkc", "lustre/sec/gks"},
766 {"ost", "lustre/ost"},
767 {"osc", "lustre/osc"},
768 {"mds", "lustre/mds"},
769 {"mdc", "lustre/mdc"},
770 {"llite", "lustre/llite"},
771 {"lustre", "lustre/llite"},
772 {"llite_lloop", "lustre/llite"},
773 {"ldiskfs", "ldiskfs/ldiskfs"},
774 {"smfs", "lustre/smfs"},
775 {"obdecho", "lustre/obdecho"},
776 {"ldlm", "lustre/ldlm"},
777 {"obdfilter", "lustre/obdfilter"},
778 {"lov", "lustre/lov"},
779 {"lmv", "lustre/lmv"},
780 {"fsfilt_ext3", "lustre/lvfs"},
781 {"fsfilt_reiserfs", "lustre/lvfs"},
782 {"fsfilt_smfs", "lustre/lvfs"},
783 {"fsfilt_ldiskfs", "lustre/lvfs"},
784 {"mds_ext3", "lustre/mds"},
785 {"cobd", "lustre/cobd"},
786 {"cmobd", "lustre/cmobd"},
787 {"lquota", "lustre/quota"},
788 {"mgs", "lustre/mgs"},
789 {"mgc", "lustre/mgc"},
790 {"mdt", "lustre/mdt"},
791 {"mdd", "lustre/mdd"},
792 {"osd", "lustre/osd"},
793 {"cmm", "lustre/cmm"},
794 {"fid", "lustre/fid"},
795 {"fld", "lustre/fld"},
799 static int jt_dbg_modules_2_4(int argc, char **argv)
801 #ifdef HAVE_LINUX_VERSION_H
802 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
803 struct mod_paths *mp;
805 char *kernel = "linux";
812 printf("%s [path] [kernel]\n", argv[0]);
816 for (mp = mod_paths; mp->name != NULL; mp++) {
817 struct module_info info;
820 int query_module(const char *name, int which, void *buf,
821 size_t bufsize, size_t *ret);
823 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
827 printf("query_module(%s) failed: %s\n",
828 mp->name, strerror(errno));
830 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
831 path[0] ? "/" : "", mp->path, mp->name,
832 info.addr + sizeof(struct module));
837 #endif // Headers are 2.6-only
838 #endif // !HAVE_LINUX_VERSION_H
842 static int jt_dbg_modules_2_5(int argc, char **argv)
844 struct mod_paths *mp;
846 char *kernel = "linux";
847 const char *proc = "/proc/modules";
848 char modname[128], others[4096];
858 printf("%s [path] [kernel]\n", argv[0]);
862 file = fopen(proc, "r");
864 printf("failed open %s: %s\n", proc, strerror(errno));
868 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
869 modname, others, others, others, others, &modaddr)) == 6) {
870 for (mp = mod_paths; mp->name != NULL; mp++) {
871 if (!strcmp(mp->name, modname))
875 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
876 path[0] ? "/" : "", mp->path, mp->name, modaddr);
884 int jt_dbg_modules(int argc, char **argv)
887 struct utsname sysinfo;
889 rc = uname(&sysinfo);
891 printf("uname() failed: %s\n", strerror(errno));
895 if (sysinfo.release[2] > '4') {
896 return jt_dbg_modules_2_5(argc, argv);
898 return jt_dbg_modules_2_4(argc, argv);
904 int jt_dbg_panic(int argc, char **argv)
907 struct libcfs_ioctl_data data;
910 fprintf(stderr, "usage: %s\n", argv[0]);
914 memset(&data, 0, sizeof(data));
915 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
916 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
920 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
922 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",