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;
324 struct list_head chain;
328 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
330 struct list_head *pos;
331 struct dbg_line *curr;
333 list_for_each(pos, head) {
334 curr = list_entry(pos, struct dbg_line, chain);
336 if (curr->hdr->ph_sec < new->hdr->ph_sec)
338 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
339 curr->hdr->ph_usec < new->hdr->ph_usec)
342 list_add(&new->chain, pos->prev);
345 list_add_tail(&new->chain, head);
348 static void print_saved_records(struct list_head *list, FILE *out)
350 struct list_head *pos, *tmp;
352 list_for_each_safe(pos, tmp, list) {
353 struct dbg_line *line;
354 struct ptldebug_header *hdr;
356 line = list_entry(pos, struct dbg_line, chain);
357 list_del(&line->chain);
360 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
361 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
362 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
363 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
364 line->file, hdr->ph_line_num, line->fn, line->text);
370 static int parse_buffer(FILE *in, FILE *out)
372 struct dbg_line *line;
373 struct ptldebug_header *hdr;
376 unsigned long dropped = 0, kept = 0;
377 struct list_head chunk_list;
379 CFS_INIT_LIST_HEAD(&chunk_list);
382 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
387 if (hdr->ph_len == 0)
389 if (hdr->ph_len > 4094) {
390 fprintf(stderr, "unexpected large record: %d bytes. "
396 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
397 print_saved_records(&chunk_list, out);
398 assert(list_empty(&chunk_list));
401 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
402 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
407 (!(subsystem_mask & hdr->ph_subsys) ||
408 (!(debug_mask & hdr->ph_mask)))) {
413 line = malloc(sizeof(*line));
415 fprintf(stderr, "malloc failed; printing accumulated "
416 "records and exiting.\n");
420 line->hdr = malloc(hdr->ph_len + 1);
421 if (line->hdr == NULL) {
423 fprintf(stderr, "malloc failed; printing accumulated "
424 "records and exiting.\n");
428 p = (void *)line->hdr;
429 memcpy(line->hdr, buf, hdr->ph_len);
430 p[hdr->ph_len] = '\0';
434 p += strlen(line->file) + 1;
436 p += strlen(line->fn) + 1;
439 list_add_ordered(line, &chunk_list);
443 print_saved_records(&chunk_list, out);
445 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
446 dropped + kept, kept, dropped);
450 int jt_dbg_debug_kernel(int argc, char **argv)
455 FILE *in, *out = stdout;
458 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
464 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
469 /* If we are dumping raw (which means no conversion step to ASCII)
470 * then dump directly to any supplied filename, otherwise this is
471 * just a temp file and we dump to the real file at convert time. */
473 strcpy(filename, argv[1]);
475 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
477 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
480 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
482 fprintf(stderr, "open(dump_kernel) failed: %s\n",
487 rc = dbg_write_cmd(fd, filename, strlen(filename));
489 fprintf(stderr, "write(%s) failed: %s\n", filename,
494 dbg_close_ctlhandle(fd);
499 in = fopen(filename, "r");
501 if (errno == ENOENT) /* no dump file created */
504 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
509 out = fopen(argv[1], "w");
511 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
518 rc = parse_buffer(in, out);
523 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
524 "behind.\n", filename);
526 rc = unlink(filename);
528 fprintf(stderr, "dumped successfully, but couldn't "
529 "unlink tmp file %s: %s\n", filename,
535 int jt_dbg_debug_file(int argc, char **argv)
543 if (argc > 3 || argc < 2) {
544 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
548 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
550 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
554 in = fdopen(fdin, "r");
556 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
562 fdout = open(argv[2],
563 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
566 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
571 out = fdopen(fdout, "w");
573 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
581 rc = parse_buffer(in, out);
590 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
592 int jt_dbg_debug_daemon(int argc, char **argv)
598 fprintf(stderr, debug_daemon_usage, argv[0]);
602 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
607 if (strcasecmp(argv[1], "start") == 0) {
608 if (argc < 3 || argc > 4 ||
609 (argc == 4 && strlen(argv[3]) > 5)) {
610 fprintf(stderr, debug_daemon_usage, argv[0]);
615 const long min_size = 10;
616 const long max_size = 20480;
620 size = strtoul(argv[3], &end, 0);
621 if (size < min_size ||
624 fprintf(stderr, "size %s invalid, must be in "
625 "the range %ld-%ld MB\n", argv[3],
629 snprintf(buf, sizeof(buf), "size=%ld", size);
630 rc = dbg_write_cmd(fd, buf, strlen(buf));
633 fprintf(stderr, "set %s failed: %s\n",
634 buf, strerror(errno));
639 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
641 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
642 argv[2], strerror(errno));
648 if (strcasecmp(argv[1], "stop") == 0) {
649 rc = dbg_write_cmd(fd, "stop", 4);
651 fprintf(stderr, "stopping debug_daemon failed: %s\n",
660 fprintf(stderr, debug_daemon_usage, argv[0]);
663 dbg_close_ctlhandle(fd);
667 int jt_dbg_clear_debug_buf(int argc, char **argv)
670 struct libcfs_ioctl_data data;
673 fprintf(stderr, "usage: %s\n", argv[0]);
677 memset(&data, 0, sizeof(data));
678 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
679 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
683 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
685 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
692 int jt_dbg_mark_debug_buf(int argc, char **argv)
694 static char scratch[MAX_MARK_SIZE] = { '\0' };
695 int rc, max_size = MAX_MARK_SIZE-1;
696 struct libcfs_ioctl_data data = { 0 };
698 time_t now = time(NULL);
703 strncpy(text, argv[1], max_size);
704 max_size-=strlen(argv[1]);
705 for (count = 2; (count < argc) && (max_size > 0); count++){
706 strncat(text, " ", max_size);
708 strncat(text, argv[count], max_size);
709 max_size -= strlen(argv[count]);
715 data.ioc_inllen1 = strlen(text) + 1;
716 data.ioc_inlbuf1 = text;
717 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
718 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
722 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
724 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
731 static struct mod_paths {
734 {"libcfs", "lnet/libcfs"},
735 {"lnet", "lnet/lnet"},
736 {"kciblnd", "lnet/klnds/ciblnd"},
737 {"kgmlnd", "lnet/klnds/gmlnd"},
738 {"kmxlnd", "lnet/klnds/mxlnd"},
739 {"kiiblnd", "lnet/klnds/iiblnd"},
740 {"ko2iblnd", "lnet/klnds/o2iblnd"},
741 {"kopeniblnd", "lnet/klnds/openiblnd"},
742 {"kptllnd", "lnet/klnds/ptllnd"},
743 {"kqswlnd", "lnet/klnds/qswlnd"},
744 {"kralnd", "lnet/klnds/ralnd"},
745 {"ksocklnd", "lnet/klnds/socklnd"},
746 {"ktdilnd", "lnet/klnds/tdilnd"},
747 {"kviblnd", "lnet/klnds/viblnd"},
748 {"lvfs", "lustre/lvfs"},
749 {"obdclass", "lustre/obdclass"},
750 {"llog_test", "lustre/obdclass"},
751 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
752 {"ptlrpc", "lustre/ptlrpc"},
753 {"gks", "lustre/sec/gks"},
754 {"gkc", "lustre/sec/gks"},
755 {"ost", "lustre/ost"},
756 {"osc", "lustre/osc"},
757 {"mds", "lustre/mds"},
758 {"mdc", "lustre/mdc"},
759 {"llite", "lustre/llite"},
760 {"lustre", "lustre/llite"},
761 {"ldiskfs", "lustre/ldiskfs"},
762 {"smfs", "lustre/smfs"},
763 {"obdecho", "lustre/obdecho"},
764 {"ldlm", "lustre/ldlm"},
765 {"obdfilter", "lustre/obdfilter"},
766 {"lov", "lustre/lov"},
767 {"lmv", "lustre/lmv"},
768 {"fsfilt_ext3", "lustre/lvfs"},
769 {"fsfilt_reiserfs", "lustre/lvfs"},
770 {"fsfilt_smfs", "lustre/lvfs"},
771 {"fsfilt_ldiskfs", "lustre/lvfs"},
772 {"mds_ext3", "lustre/mds"},
773 {"cobd", "lustre/cobd"},
774 {"cmobd", "lustre/cmobd"},
775 {"lquota", "lustre/quota"},
776 {"mgs", "lustre/mgs"},
777 {"mgc", "lustre/mgc"},
778 {"mdt", "lustre/mdt"},
779 {"mdd", "lustre/mdd"},
780 {"osd", "lustre/osd"},
781 {"cmm", "lustre/cmm"},
782 {"fid", "lustre/fid"},
783 {"fld", "lustre/fld"},
787 static int jt_dbg_modules_2_4(int argc, char **argv)
789 #ifdef HAVE_LINUX_VERSION_H
790 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
791 struct mod_paths *mp;
793 char *kernel = "linux";
800 printf("%s [path] [kernel]\n", argv[0]);
804 for (mp = mod_paths; mp->name != NULL; mp++) {
805 struct module_info info;
808 int query_module(const char *name, int which, void *buf,
809 size_t bufsize, size_t *ret);
811 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
815 printf("query_module(%s) failed: %s\n",
816 mp->name, strerror(errno));
818 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
819 path[0] ? "/" : "", mp->path, mp->name,
820 info.addr + sizeof(struct module));
825 #endif // Headers are 2.6-only
826 #endif // !HAVE_LINUX_VERSION_H
830 static int jt_dbg_modules_2_5(int argc, char **argv)
832 struct mod_paths *mp;
834 char *kernel = "linux";
835 const char *proc = "/proc/modules";
836 char modname[128], others[4096];
846 printf("%s [path] [kernel]\n", argv[0]);
850 file = fopen(proc, "r");
852 printf("failed open %s: %s\n", proc, strerror(errno));
856 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
857 modname, others, others, others, others, &modaddr)) == 6) {
858 for (mp = mod_paths; mp->name != NULL; mp++) {
859 if (!strcmp(mp->name, modname))
863 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
864 path[0] ? "/" : "", mp->path, mp->name, modaddr);
872 int jt_dbg_modules(int argc, char **argv)
875 struct utsname sysinfo;
877 rc = uname(&sysinfo);
879 printf("uname() failed: %s\n", strerror(errno));
883 if (sysinfo.release[2] > '4') {
884 return jt_dbg_modules_2_5(argc, argv);
886 return jt_dbg_modules_2_4(argc, argv);
892 int jt_dbg_panic(int argc, char **argv)
895 struct libcfs_ioctl_data data;
898 fprintf(stderr, "usage: %s\n", argv[0]);
902 memset(&data, 0, sizeof(data));
903 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
904 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
908 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
910 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",