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
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
52 #include <sys/utsname.h>
54 #include <lnet/api-support.h>
55 #include <lnet/lnetctl.h>
56 #include <libcfs/portals_utils.h>
61 static char rawbuf[8192];
62 static char *buf = rawbuf;
63 static int max = 8192;
64 /*static int g_pfd = -1;*/
65 static int subsystem_mask = ~0;
66 static int debug_mask = ~0;
68 #define MAX_MARK_SIZE 256
70 static const char *libcfs_debug_subsystems[] =
71 {"undefined", "mdc", "mds", "osc",
72 "ost", "class", "log", "llite",
73 "rpc", "mgmt", "lnet", "lnd",
74 "pinger", "filter", "", "echo",
75 "ldlm", "lov", "", "",
78 "mgc", "mgs", "fid", "fld", NULL};
79 static const char *libcfs_debug_masks[] =
80 {"trace", "inode", "super", "ext2",
81 "malloc", "cache", "info", "ioctl",
82 "neterror", "net", "warning", "buffs",
83 "other", "dentry", "nettrace", "page",
84 "dlmtrace", "error", "emerg", "ha",
85 "rpctrace", "vfstrace", "reada", "mmap",
86 "config", "console", "quota", "sec", NULL};
88 struct debug_daemon_cmd {
93 static const struct debug_daemon_cmd libcfs_debug_daemon_cmd[] = {
94 {"start", DEBUG_DAEMON_START},
95 {"stop", DEBUG_DAEMON_STOP},
101 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
102 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
103 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
104 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
107 dbg_open_ctlhandle(const char *str)
110 fd = open(str, O_WRONLY);
112 fprintf(stderr, "open %s failed: %s\n", str,
120 dbg_close_ctlhandle(int fd)
126 dbg_write_cmd(int fd, char *str, int len)
128 int rc = write(fd, str, len);
130 return (rc == len ? 0 : 1);
133 #elif defined(__DARWIN__)
135 #define DAEMON_CTL_NAME "lnet.trace_daemon"
136 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
137 #define DEBUG_CTL_NAME "lnet.debug"
138 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
140 static char sysctl_name[128];
142 dbg_open_ctlhandle(const char *str)
145 if (strlen(str)+1 > 128) {
146 fprintf(stderr, "sysctl name is too long: %s.\n", str);
149 strcpy(sysctl_name, str);
155 dbg_close_ctlhandle(int fd)
157 sysctl_name[0] = '\0';
162 dbg_write_cmd(int fd, char *str, int len)
166 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
168 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
169 sysctl_name, str, errno);
171 return (rc == 0 ? 0: 1);
175 #error - Unknown sysctl convention.
178 static int do_debug_mask(char *name, int enable)
182 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
183 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
184 strcasecmp(name, "all_subs") == 0) {
185 printf("%s output from subsystem \"%s\"\n",
186 enable ? "Enabling" : "Disabling",
187 libcfs_debug_subsystems[i]);
189 subsystem_mask |= (1 << i);
191 subsystem_mask &= ~(1 << i);
195 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
196 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
197 strcasecmp(name, "all_types") == 0) {
198 printf("%s output of type \"%s\"\n",
199 enable ? "Enabling" : "Disabling",
200 libcfs_debug_masks[i]);
202 debug_mask |= (1 << i);
204 debug_mask &= ~(1 << i);
212 int dbg_initialize(int argc, char **argv)
217 int jt_dbg_filter(int argc, char **argv)
222 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
227 for (i = 1; i < argc; i++)
228 if (!do_debug_mask(argv[i], 0))
229 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
234 int jt_dbg_show(int argc, char **argv)
239 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
244 for (i = 1; i < argc; i++)
245 if (!do_debug_mask(argv[i], 1))
246 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
252 static int applymask(char* procpath, int value)
256 int len = snprintf(buf, 64, "%d", value);
258 int fd = dbg_open_ctlhandle(procpath);
260 fprintf(stderr, "Unable to open %s: %s\n",
261 procpath, strerror(errno));
264 rc = dbg_write_cmd(fd, buf, len+1);
266 fprintf(stderr, "Write to %s failed: %s\n",
267 procpath, strerror(errno));
270 dbg_close_ctlhandle(fd);
274 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
276 if (!dump_filename) {
277 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
278 applymask(DEBUG_CTL_NAME, debug_mask);
280 struct libcfs_debug_ioctl_data data;
282 data.hdr.ioc_len = sizeof(data);
283 data.hdr.ioc_version = 0;
284 data.subs = subs_mask;
285 data.debug = debug_mask;
287 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
289 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
290 subs_mask, debug_mask);
293 int jt_dbg_list(int argc, char **argv)
298 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
302 if (strcasecmp(argv[1], "subs") == 0) {
303 printf("Subsystems: all_subs");
304 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
305 if (libcfs_debug_subsystems[i][0])
306 printf(", %s", libcfs_debug_subsystems[i]);
308 } else if (strcasecmp(argv[1], "types") == 0) {
309 printf("Types: all_types");
310 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
311 printf(", %s", libcfs_debug_masks[i]);
313 } else if (strcasecmp(argv[1], "applymasks") == 0) {
314 applymask_all(subsystem_mask, debug_mask);
319 /* all strings nul-terminated; only the struct and hdr need to be freed */
321 struct ptldebug_header *hdr;
327 static int cmp_rec(const void *p1, const void *p2)
329 struct dbg_line *d1 = *(struct dbg_line **)p1;
330 struct dbg_line *d2 = *(struct dbg_line **)p2;
332 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
334 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
335 d1->hdr->ph_usec < d2->hdr->ph_usec)
337 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
338 d1->hdr->ph_usec == d2->hdr->ph_usec)
343 static void print_rec(struct dbg_line **linev, int used, FILE *out)
347 for (i = 0; i < used; i++) {
348 struct dbg_line *line = linev[i];
349 struct ptldebug_header *hdr = line->hdr;
351 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
352 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
353 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
354 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
355 line->file, hdr->ph_line_num, line->fn, line->text);
362 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
365 struct dbg_line **linev = *linevp;
368 int nlen = *lenp + 512;
369 int nsize = nlen * sizeof(struct dbg_line *);
371 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
381 static int parse_buffer(FILE *in, FILE *out)
383 struct dbg_line *line;
384 struct ptldebug_header *hdr;
387 unsigned long dropped = 0, kept = 0;
388 struct dbg_line **linev = NULL;
392 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
397 if (hdr->ph_len == 0)
399 if (hdr->ph_len > 4094) {
400 fprintf(stderr, "unexpected large record: %d bytes. "
406 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
407 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
411 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
412 (hdr->ph_mask && !(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."CFS_TIME_T".%u",
487 time(NULL),getpid());
489 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
492 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
494 fprintf(stderr, "open(dump_kernel) failed: %s\n",
499 rc = dbg_write_cmd(fd, filename, strlen(filename));
501 fprintf(stderr, "write(%s) failed: %s\n", filename,
506 dbg_close_ctlhandle(fd);
511 in = fopen(filename, "r");
513 if (errno == ENOENT) /* no dump file created */
516 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
521 out = fopen(argv[1], "w");
523 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
530 rc = parse_buffer(in, out);
535 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
536 "behind.\n", filename);
538 rc = unlink(filename);
540 fprintf(stderr, "dumped successfully, but couldn't "
541 "unlink tmp file %s: %s\n", filename,
547 int jt_dbg_debug_file(int argc, char **argv)
555 if (argc > 3 || argc < 2) {
556 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
560 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
562 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
566 in = fdopen(fdin, "r");
568 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
574 fdout = open(argv[2],
575 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
578 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
583 out = fdopen(fdout, "w");
585 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
593 rc = parse_buffer(in, out);
602 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
604 int jt_dbg_debug_daemon(int argc, char **argv)
610 fprintf(stderr, debug_daemon_usage, argv[0]);
614 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
619 if (strcasecmp(argv[1], "start") == 0) {
620 if (argc < 3 || argc > 4 ||
621 (argc == 4 && strlen(argv[3]) > 5)) {
622 fprintf(stderr, debug_daemon_usage, argv[0]);
627 const long min_size = 10;
628 const long max_size = 20480;
632 size = strtoul(argv[3], &end, 0);
633 if (size < min_size ||
636 fprintf(stderr, "size %s invalid, must be in "
637 "the range %ld-%ld MB\n", argv[3],
641 snprintf(buf, sizeof(buf), "size=%ld", size);
642 rc = dbg_write_cmd(fd, buf, strlen(buf));
645 fprintf(stderr, "set %s failed: %s\n",
646 buf, strerror(errno));
651 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
653 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
654 argv[2], strerror(errno));
660 if (strcasecmp(argv[1], "stop") == 0) {
661 rc = dbg_write_cmd(fd, "stop", 4);
663 fprintf(stderr, "stopping debug_daemon failed: %s\n",
672 fprintf(stderr, debug_daemon_usage, argv[0]);
675 dbg_close_ctlhandle(fd);
679 int jt_dbg_clear_debug_buf(int argc, char **argv)
682 struct libcfs_ioctl_data data;
685 fprintf(stderr, "usage: %s\n", argv[0]);
689 memset(&data, 0, sizeof(data));
690 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
691 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
695 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
697 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
704 int jt_dbg_mark_debug_buf(int argc, char **argv)
706 static char scratch[MAX_MARK_SIZE] = { '\0' };
707 int rc, max_size = MAX_MARK_SIZE-1;
708 struct libcfs_ioctl_data data = { 0 };
710 time_t now = time(NULL);
715 strncpy(text, argv[1], max_size);
716 max_size-=strlen(argv[1]);
717 for (count = 2; (count < argc) && (max_size > 0); count++){
718 strncat(text, " ", max_size);
720 strncat(text, argv[count], max_size);
721 max_size -= strlen(argv[count]);
727 data.ioc_inllen1 = strlen(text) + 1;
728 data.ioc_inlbuf1 = text;
729 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
730 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
734 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
736 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
743 static struct mod_paths {
746 {"libcfs", "libcfs/libcfs"},
747 {"lnet", "lnet/lnet"},
748 {"kciblnd", "lnet/klnds/ciblnd"},
749 {"kgmlnd", "lnet/klnds/gmlnd"},
750 {"kmxlnd", "lnet/klnds/mxlnd"},
751 {"kiiblnd", "lnet/klnds/iiblnd"},
752 {"ko2iblnd", "lnet/klnds/o2iblnd"},
753 {"kopeniblnd", "lnet/klnds/openiblnd"},
754 {"kptllnd", "lnet/klnds/ptllnd"},
755 {"kqswlnd", "lnet/klnds/qswlnd"},
756 {"kralnd", "lnet/klnds/ralnd"},
757 {"ksocklnd", "lnet/klnds/socklnd"},
758 {"ktdilnd", "lnet/klnds/tdilnd"},
759 {"kviblnd", "lnet/klnds/viblnd"},
760 {"lvfs", "lustre/lvfs"},
761 {"obdclass", "lustre/obdclass"},
762 {"llog_test", "lustre/obdclass"},
763 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
764 {"ptlrpc", "lustre/ptlrpc"},
765 {"gks", "lustre/sec/gks"},
766 {"gkc", "lustre/sec/gks"},
767 {"ost", "lustre/ost"},
768 {"osc", "lustre/osc"},
769 {"mds", "lustre/mds"},
770 {"mdc", "lustre/mdc"},
771 {"llite", "lustre/llite"},
772 {"lustre", "lustre/llite"},
773 {"llite_lloop", "lustre/llite"},
774 {"ldiskfs", "ldiskfs/ldiskfs"},
775 {"smfs", "lustre/smfs"},
776 {"obdecho", "lustre/obdecho"},
777 {"ldlm", "lustre/ldlm"},
778 {"obdfilter", "lustre/obdfilter"},
779 {"lov", "lustre/lov"},
780 {"lmv", "lustre/lmv"},
781 {"fsfilt_ext3", "lustre/lvfs"},
782 {"fsfilt_reiserfs", "lustre/lvfs"},
783 {"fsfilt_smfs", "lustre/lvfs"},
784 {"fsfilt_ldiskfs", "lustre/lvfs"},
785 {"mds_ext3", "lustre/mds"},
786 {"cobd", "lustre/cobd"},
787 {"cmobd", "lustre/cmobd"},
788 {"lquota", "lustre/quota"},
789 {"mgs", "lustre/mgs"},
790 {"mgc", "lustre/mgc"},
791 {"mdt", "lustre/mdt"},
792 {"mdd", "lustre/mdd"},
793 {"osd", "lustre/osd"},
794 {"cmm", "lustre/cmm"},
795 {"fid", "lustre/fid"},
796 {"fld", "lustre/fld"},
800 static int jt_dbg_modules_2_4(int argc, char **argv)
802 #ifdef HAVE_LINUX_VERSION_H
803 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
804 struct mod_paths *mp;
806 char *kernel = "linux";
813 printf("%s [path] [kernel]\n", argv[0]);
817 for (mp = mod_paths; mp->name != NULL; mp++) {
818 struct module_info info;
821 int query_module(const char *name, int which, void *buf,
822 size_t bufsize, size_t *ret);
824 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
828 printf("query_module(%s) failed: %s\n",
829 mp->name, strerror(errno));
831 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
832 path[0] ? "/" : "", mp->path, mp->name,
833 info.addr + sizeof(struct module));
838 #endif // Headers are 2.6-only
839 #endif // !HAVE_LINUX_VERSION_H
843 static int jt_dbg_modules_2_5(int argc, char **argv)
845 struct mod_paths *mp;
847 char *kernel = "linux";
848 const char *proc = "/proc/modules";
849 char modname[128], others[4096];
859 printf("%s [path] [kernel]\n", argv[0]);
863 file = fopen(proc, "r");
865 printf("failed open %s: %s\n", proc, strerror(errno));
869 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
870 modname, others, others, others, others, &modaddr)) == 6) {
871 for (mp = mod_paths; mp->name != NULL; mp++) {
872 if (!strcmp(mp->name, modname))
876 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
877 path[0] ? "/" : "", mp->path, mp->name, modaddr);
885 int jt_dbg_modules(int argc, char **argv)
888 struct utsname sysinfo;
890 rc = uname(&sysinfo);
892 printf("uname() failed: %s\n", strerror(errno));
896 if (sysinfo.release[2] > '4') {
897 return jt_dbg_modules_2_5(argc, argv);
899 return jt_dbg_modules_2_4(argc, argv);
905 int jt_dbg_panic(int argc, char **argv)
908 struct libcfs_ioctl_data data;
911 fprintf(stderr, "usage: %s\n", argv[0]);
915 memset(&data, 0, sizeof(data));
916 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
917 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
921 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
923 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",