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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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
46 #include <libcfs/libcfsutil.h>
47 #include <lnet/lnetctl.h>
49 #include <sys/utsname.h>
51 static char rawbuf[8192];
52 static char *buf = rawbuf;
53 static int max = 8192;
54 /*static int g_pfd = -1;*/
55 static int subsystem_mask = ~0;
56 static int debug_mask = ~0;
58 #define MAX_MARK_SIZE 256
60 static const char *libcfs_debug_subsystems[] =
61 {"undefined", "mdc", "mds", "osc",
62 "ost", "class", "log", "llite",
63 "rpc", "mgmt", "lnet", "lnd",
64 "pinger", "filter", "", "echo",
65 "ldlm", "lov", "", "",
68 "mgc", "mgs", "fid", "fld", NULL};
69 static const char *libcfs_debug_masks[] =
70 {"trace", "inode", "super", "ext2",
71 "malloc", "cache", "info", "ioctl",
72 "neterror", "net", "warning", "buffs",
73 "other", "dentry", "nettrace", "page",
74 "dlmtrace", "error", "emerg", "ha",
75 "rpctrace", "vfstrace", "reada", "mmap",
76 "config", "console", "quota", "sec", NULL};
80 #define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
81 #define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
82 #define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
83 #define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
86 dbg_open_ctlhandle(const char *str)
89 fd = open(str, O_WRONLY);
91 fprintf(stderr, "open %s failed: %s\n", str,
99 dbg_close_ctlhandle(int fd)
105 dbg_write_cmd(int fd, char *str, int len)
107 int rc = write(fd, str, len);
109 return (rc == len ? 0 : 1);
112 #elif defined(__DARWIN__)
114 #define DAEMON_CTL_NAME "lnet.trace_daemon"
115 #define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
116 #define DEBUG_CTL_NAME "lnet.debug"
117 #define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
119 static char sysctl_name[128];
121 dbg_open_ctlhandle(const char *str)
124 if (strlen(str)+1 > 128) {
125 fprintf(stderr, "sysctl name is too long: %s.\n", str);
128 strcpy(sysctl_name, str);
134 dbg_close_ctlhandle(int fd)
136 sysctl_name[0] = '\0';
141 dbg_write_cmd(int fd, char *str, int len)
145 rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
147 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
148 sysctl_name, str, errno);
150 return (rc == 0 ? 0: 1);
154 #error - Unknown sysctl convention.
157 static int do_debug_mask(char *name, int enable)
161 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
162 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
163 strcasecmp(name, "all_subs") == 0) {
164 printf("%s output from subsystem \"%s\"\n",
165 enable ? "Enabling" : "Disabling",
166 libcfs_debug_subsystems[i]);
168 subsystem_mask |= (1 << i);
170 subsystem_mask &= ~(1 << i);
174 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
175 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
176 strcasecmp(name, "all_types") == 0) {
177 printf("%s output of type \"%s\"\n",
178 enable ? "Enabling" : "Disabling",
179 libcfs_debug_masks[i]);
181 debug_mask |= (1 << i);
183 debug_mask &= ~(1 << i);
191 int dbg_initialize(int argc, char **argv)
196 int jt_dbg_filter(int argc, char **argv)
201 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
206 for (i = 1; i < argc; i++)
207 if (!do_debug_mask(argv[i], 0))
208 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
213 int jt_dbg_show(int argc, char **argv)
218 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
223 for (i = 1; i < argc; i++)
224 if (!do_debug_mask(argv[i], 1))
225 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
231 static int applymask(char* procpath, int value)
235 int len = snprintf(buf, 64, "%d", value);
237 int fd = dbg_open_ctlhandle(procpath);
239 fprintf(stderr, "Unable to open %s: %s\n",
240 procpath, strerror(errno));
243 rc = dbg_write_cmd(fd, buf, len+1);
245 fprintf(stderr, "Write to %s failed: %s\n",
246 procpath, strerror(errno));
249 dbg_close_ctlhandle(fd);
253 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
255 if (!dump_filename) {
256 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
257 applymask(DEBUG_CTL_NAME, debug_mask);
259 struct libcfs_debug_ioctl_data data;
261 data.hdr.ioc_len = sizeof(data);
262 data.hdr.ioc_version = 0;
263 data.subs = subs_mask;
264 data.debug = debug_mask;
266 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
268 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
269 subs_mask, debug_mask);
272 int jt_dbg_list(int argc, char **argv)
277 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
281 if (strcasecmp(argv[1], "subs") == 0) {
282 printf("Subsystems: all_subs");
283 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
284 if (libcfs_debug_subsystems[i][0])
285 printf(", %s", libcfs_debug_subsystems[i]);
287 } else if (strcasecmp(argv[1], "types") == 0) {
288 printf("Types: all_types");
289 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
290 printf(", %s", libcfs_debug_masks[i]);
292 } else if (strcasecmp(argv[1], "applymasks") == 0) {
293 applymask_all(subsystem_mask, debug_mask);
298 /* all strings nul-terminated; only the struct and hdr need to be freed */
300 struct ptldebug_header *hdr;
306 static int cmp_rec(const void *p1, const void *p2)
308 struct dbg_line *d1 = *(struct dbg_line **)p1;
309 struct dbg_line *d2 = *(struct dbg_line **)p2;
311 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
313 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
314 d1->hdr->ph_usec < d2->hdr->ph_usec)
316 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
317 d1->hdr->ph_usec == d2->hdr->ph_usec)
322 static void print_rec(struct dbg_line **linev, int used, FILE *out)
326 for (i = 0; i < used; i++) {
327 struct dbg_line *line = linev[i];
328 struct ptldebug_header *hdr = line->hdr;
330 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
331 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
332 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
333 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
334 line->file, hdr->ph_line_num, line->fn, line->text);
341 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
344 struct dbg_line **linev = *linevp;
347 int nlen = *lenp + 512;
348 int nsize = nlen * sizeof(struct dbg_line *);
350 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
360 static int parse_buffer(FILE *in, FILE *out)
362 struct dbg_line *line;
363 struct ptldebug_header *hdr;
366 unsigned long dropped = 0, kept = 0;
367 struct dbg_line **linev = NULL;
371 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
376 if (hdr->ph_len == 0)
378 if (hdr->ph_len > 4094) {
379 fprintf(stderr, "unexpected large record: %d bytes. "
385 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
386 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
390 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
391 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
396 line = malloc(sizeof(*line));
398 fprintf(stderr, "malloc failed; printing accumulated "
399 "records and exiting.\n");
403 line->hdr = malloc(hdr->ph_len + 1);
404 if (line->hdr == NULL) {
406 fprintf(stderr, "malloc failed; printing accumulated "
407 "records and exiting.\n");
411 p = (void *)line->hdr;
412 memcpy(line->hdr, buf, hdr->ph_len);
413 p[hdr->ph_len] = '\0';
417 p += strlen(line->file) + 1;
419 p += strlen(line->fn) + 1;
422 if (!add_rec(line, &linev, &linev_len, kept)) {
423 fprintf(stderr, "malloc failed; printing accumulated "
424 "records and exiting.\n");
431 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
432 print_rec(linev, kept, out);
435 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
436 dropped + kept, kept, dropped);
440 int jt_dbg_debug_kernel(int argc, char **argv)
445 FILE *in, *out = stdout;
448 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
454 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
459 /* If we are dumping raw (which means no conversion step to ASCII)
460 * then dump directly to any supplied filename, otherwise this is
461 * just a temp file and we dump to the real file at convert time. */
463 strcpy(filename, argv[1]);
465 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
466 time(NULL),getpid());
468 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
471 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
473 fprintf(stderr, "open(dump_kernel) failed: %s\n",
478 rc = dbg_write_cmd(fd, filename, strlen(filename));
480 fprintf(stderr, "write(%s) failed: %s\n", filename,
485 dbg_close_ctlhandle(fd);
490 in = fopen(filename, "r");
492 if (errno == ENOENT) /* no dump file created */
495 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
500 out = fopen(argv[1], "w");
502 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
509 rc = parse_buffer(in, out);
514 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
515 "behind.\n", filename);
517 rc = unlink(filename);
519 fprintf(stderr, "dumped successfully, but couldn't "
520 "unlink tmp file %s: %s\n", filename,
526 int jt_dbg_debug_file(int argc, char **argv)
534 if (argc > 3 || argc < 2) {
535 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
539 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
541 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
545 in = fdopen(fdin, "r");
547 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
553 fdout = open(argv[2],
554 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
557 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
562 out = fdopen(fdout, "w");
564 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
572 rc = parse_buffer(in, out);
581 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
583 int jt_dbg_debug_daemon(int argc, char **argv)
589 fprintf(stderr, debug_daemon_usage, argv[0]);
593 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
598 if (strcasecmp(argv[1], "start") == 0) {
599 if (argc < 3 || argc > 4 ||
600 (argc == 4 && strlen(argv[3]) > 5)) {
601 fprintf(stderr, debug_daemon_usage, argv[0]);
606 const long min_size = 10;
607 const long max_size = 20480;
611 size = strtoul(argv[3], &end, 0);
612 if (size < min_size ||
615 fprintf(stderr, "size %s invalid, must be in "
616 "the range %ld-%ld MB\n", argv[3],
620 snprintf(buf, sizeof(buf), "size=%ld", size);
621 rc = dbg_write_cmd(fd, buf, strlen(buf));
624 fprintf(stderr, "set %s failed: %s\n",
625 buf, strerror(errno));
630 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
632 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
633 argv[2], strerror(errno));
639 if (strcasecmp(argv[1], "stop") == 0) {
640 rc = dbg_write_cmd(fd, "stop", 4);
642 fprintf(stderr, "stopping debug_daemon failed: %s\n",
651 fprintf(stderr, debug_daemon_usage, argv[0]);
654 dbg_close_ctlhandle(fd);
658 int jt_dbg_clear_debug_buf(int argc, char **argv)
661 struct libcfs_ioctl_data data;
664 fprintf(stderr, "usage: %s\n", argv[0]);
668 memset(&data, 0, sizeof(data));
669 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
670 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
674 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
676 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
683 int jt_dbg_mark_debug_buf(int argc, char **argv)
685 static char scratch[MAX_MARK_SIZE] = { '\0' };
686 int rc, max_size = MAX_MARK_SIZE-1;
687 struct libcfs_ioctl_data data = { 0 };
689 time_t now = time(NULL);
694 strncpy(text, argv[1], max_size);
695 max_size-=strlen(argv[1]);
696 for (count = 2; (count < argc) && (max_size > 0); count++){
697 strncat(text, " ", max_size);
699 strncat(text, argv[count], max_size);
700 max_size -= strlen(argv[count]);
706 data.ioc_inllen1 = strlen(text) + 1;
707 data.ioc_inlbuf1 = text;
708 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
709 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
713 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
715 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
722 static struct mod_paths {
725 {"libcfs", "libcfs/libcfs"},
726 {"lnet", "lnet/lnet"},
727 {"kciblnd", "lnet/klnds/ciblnd"},
728 {"kgmlnd", "lnet/klnds/gmlnd"},
729 {"kmxlnd", "lnet/klnds/mxlnd"},
730 {"kiiblnd", "lnet/klnds/iiblnd"},
731 {"ko2iblnd", "lnet/klnds/o2iblnd"},
732 {"kopeniblnd", "lnet/klnds/openiblnd"},
733 {"kptllnd", "lnet/klnds/ptllnd"},
734 {"kqswlnd", "lnet/klnds/qswlnd"},
735 {"kralnd", "lnet/klnds/ralnd"},
736 {"ksocklnd", "lnet/klnds/socklnd"},
737 {"ktdilnd", "lnet/klnds/tdilnd"},
738 {"kviblnd", "lnet/klnds/viblnd"},
739 {"lvfs", "lustre/lvfs"},
740 {"obdclass", "lustre/obdclass"},
741 {"llog_test", "lustre/obdclass"},
742 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
743 {"ptlrpc", "lustre/ptlrpc"},
744 {"gks", "lustre/sec/gks"},
745 {"gkc", "lustre/sec/gks"},
746 {"ost", "lustre/ost"},
747 {"osc", "lustre/osc"},
748 {"mds", "lustre/mds"},
749 {"mdc", "lustre/mdc"},
750 {"llite", "lustre/llite"},
751 {"lustre", "lustre/llite"},
752 {"llite_lloop", "lustre/llite"},
753 {"ldiskfs", "ldiskfs/ldiskfs"},
754 {"smfs", "lustre/smfs"},
755 {"obdecho", "lustre/obdecho"},
756 {"ldlm", "lustre/ldlm"},
757 {"obdfilter", "lustre/obdfilter"},
758 {"lov", "lustre/lov"},
759 {"lmv", "lustre/lmv"},
760 {"fsfilt_ext3", "lustre/lvfs"},
761 {"fsfilt_reiserfs", "lustre/lvfs"},
762 {"fsfilt_smfs", "lustre/lvfs"},
763 {"fsfilt_ldiskfs", "lustre/lvfs"},
764 {"mds_ext3", "lustre/mds"},
765 {"cobd", "lustre/cobd"},
766 {"cmobd", "lustre/cmobd"},
767 {"lquota", "lustre/quota"},
768 {"mgs", "lustre/mgs"},
769 {"mgc", "lustre/mgc"},
770 {"mdt", "lustre/mdt"},
771 {"mdd", "lustre/mdd"},
772 {"osd", "lustre/osd"},
773 {"cmm", "lustre/cmm"},
774 {"fid", "lustre/fid"},
775 {"fld", "lustre/fld"},
779 static int jt_dbg_modules_2_4(int argc, char **argv)
784 static int jt_dbg_modules_2_5(int argc, char **argv)
786 struct mod_paths *mp;
788 char *kernel = "linux";
789 const char *proc = "/proc/modules";
790 char modname[128], buf[4096];
799 printf("%s [path] [kernel]\n", argv[0]);
803 file = fopen(proc, "r");
805 printf("failed open %s: %s\n", proc, strerror(errno));
809 while (fgets(buf, sizeof(buf), file) != NULL) {
810 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
811 for (mp = mod_paths; mp->name != NULL; mp++) {
812 if (!strcmp(mp->name, modname))
816 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
817 path, path[0] ? "/" : "",
818 mp->path, mp->name, modaddr);
827 int jt_dbg_modules(int argc, char **argv)
830 struct utsname sysinfo;
832 rc = uname(&sysinfo);
834 printf("uname() failed: %s\n", strerror(errno));
838 if (sysinfo.release[2] > '4') {
839 return jt_dbg_modules_2_5(argc, argv);
841 return jt_dbg_modules_2_4(argc, argv);
847 int jt_dbg_panic(int argc, char **argv)
850 struct libcfs_ioctl_data data;
853 fprintf(stderr, "usage: %s\n", argv[0]);
857 memset(&data, 0, sizeof(data));
858 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
859 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
863 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
865 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",