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);
254 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
256 if (!dump_filename) {
257 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
258 applymask(DEBUG_CTL_NAME, debug_mask);
260 struct libcfs_debug_ioctl_data data;
262 data.hdr.ioc_len = sizeof(data);
263 data.hdr.ioc_version = 0;
264 data.subs = subs_mask;
265 data.debug = debug_mask;
267 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
269 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
270 subs_mask, debug_mask);
273 int jt_dbg_list(int argc, char **argv)
278 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
282 if (strcasecmp(argv[1], "subs") == 0) {
283 printf("Subsystems: all_subs");
284 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
285 if (libcfs_debug_subsystems[i][0])
286 printf(", %s", libcfs_debug_subsystems[i]);
288 } else if (strcasecmp(argv[1], "types") == 0) {
289 printf("Types: all_types");
290 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
291 printf(", %s", libcfs_debug_masks[i]);
293 } else if (strcasecmp(argv[1], "applymasks") == 0) {
294 applymask_all(subsystem_mask, debug_mask);
299 /* all strings nul-terminated; only the struct and hdr need to be freed */
301 struct ptldebug_header *hdr;
307 static int cmp_rec(const void *p1, const void *p2)
309 struct dbg_line *d1 = *(struct dbg_line **)p1;
310 struct dbg_line *d2 = *(struct dbg_line **)p2;
312 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
314 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
315 d1->hdr->ph_usec < d2->hdr->ph_usec)
317 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
318 d1->hdr->ph_usec == d2->hdr->ph_usec)
323 static void print_rec(struct dbg_line **linev, int used, FILE *out)
327 for (i = 0; i < used; i++) {
328 struct dbg_line *line = linev[i];
329 struct ptldebug_header *hdr = line->hdr;
331 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
332 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
333 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
334 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
335 line->file, hdr->ph_line_num, line->fn, line->text);
342 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
345 struct dbg_line **linev = *linevp;
348 int nlen = *lenp + 512;
349 int nsize = nlen * sizeof(struct dbg_line *);
351 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
361 static int parse_buffer(FILE *in, FILE *out)
363 struct dbg_line *line;
364 struct ptldebug_header *hdr;
367 unsigned long dropped = 0, kept = 0;
368 struct dbg_line **linev = NULL;
372 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
377 if (hdr->ph_len == 0)
379 if (hdr->ph_len > 4094) {
380 fprintf(stderr, "unexpected large record: %d bytes. "
386 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
387 hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
391 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
392 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
397 line = malloc(sizeof(*line));
399 fprintf(stderr, "malloc failed; printing accumulated "
400 "records and exiting.\n");
404 line->hdr = malloc(hdr->ph_len + 1);
405 if (line->hdr == NULL) {
407 fprintf(stderr, "malloc failed; printing accumulated "
408 "records and exiting.\n");
412 p = (void *)line->hdr;
413 memcpy(line->hdr, buf, hdr->ph_len);
414 p[hdr->ph_len] = '\0';
418 p += strlen(line->file) + 1;
420 p += strlen(line->fn) + 1;
423 if (!add_rec(line, &linev, &linev_len, kept)) {
424 fprintf(stderr, "malloc failed; printing accumulated "
425 "records and exiting.\n");
432 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
433 print_rec(linev, kept, out);
436 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
437 dropped + kept, kept, dropped);
441 int jt_dbg_debug_kernel(int argc, char **argv)
446 FILE *in, *out = stdout;
449 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
455 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
460 /* If we are dumping raw (which means no conversion step to ASCII)
461 * then dump directly to any supplied filename, otherwise this is
462 * just a temp file and we dump to the real file at convert time. */
464 strcpy(filename, argv[1]);
466 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
467 time(NULL),getpid());
469 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
472 fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
474 fprintf(stderr, "open(dump_kernel) failed: %s\n",
479 rc = dbg_write_cmd(fd, filename, strlen(filename));
481 fprintf(stderr, "write(%s) failed: %s\n", filename,
483 dbg_close_ctlhandle(fd);
486 dbg_close_ctlhandle(fd);
491 in = fopen(filename, "r");
493 if (errno == ENOENT) /* no dump file created */
496 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
501 out = fopen(argv[1], "w");
503 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
510 rc = parse_buffer(in, out);
515 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
516 "behind.\n", filename);
518 rc = unlink(filename);
520 fprintf(stderr, "dumped successfully, but couldn't "
521 "unlink tmp file %s: %s\n", filename,
527 int jt_dbg_debug_file(int argc, char **argv)
535 if (argc > 3 || argc < 2) {
536 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
540 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
542 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
546 in = fdopen(fdin, "r");
548 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
554 fdout = open(argv[2],
555 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
558 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
563 out = fdopen(fdout, "w");
565 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
573 rc = parse_buffer(in, out);
582 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
584 int jt_dbg_debug_daemon(int argc, char **argv)
590 fprintf(stderr, debug_daemon_usage, argv[0]);
594 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
599 if (strcasecmp(argv[1], "start") == 0) {
600 if (argc < 3 || argc > 4 ||
601 (argc == 4 && strlen(argv[3]) > 5)) {
602 fprintf(stderr, debug_daemon_usage, argv[0]);
607 const long min_size = 10;
608 const long max_size = 20480;
612 size = strtoul(argv[3], &end, 0);
613 if (size < min_size ||
616 fprintf(stderr, "size %s invalid, must be in "
617 "the range %ld-%ld MB\n", argv[3],
621 snprintf(buf, sizeof(buf), "size=%ld", size);
622 rc = dbg_write_cmd(fd, buf, strlen(buf));
625 fprintf(stderr, "set %s failed: %s\n",
626 buf, strerror(errno));
631 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
633 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
634 argv[2], strerror(errno));
640 if (strcasecmp(argv[1], "stop") == 0) {
641 rc = dbg_write_cmd(fd, "stop", 4);
643 fprintf(stderr, "stopping debug_daemon failed: %s\n",
652 fprintf(stderr, debug_daemon_usage, argv[0]);
655 dbg_close_ctlhandle(fd);
659 int jt_dbg_clear_debug_buf(int argc, char **argv)
662 struct libcfs_ioctl_data data;
665 fprintf(stderr, "usage: %s\n", argv[0]);
669 memset(&data, 0, sizeof(data));
670 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
671 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
675 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
677 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
684 int jt_dbg_mark_debug_buf(int argc, char **argv)
686 static char scratch[MAX_MARK_SIZE] = { '\0' };
687 int rc, max_size = MAX_MARK_SIZE-1;
688 struct libcfs_ioctl_data data = { 0 };
690 time_t now = time(NULL);
695 strncpy(text, argv[1], max_size);
696 max_size-=strlen(argv[1]);
697 for (count = 2; (count < argc) && (max_size > 0); count++){
698 strncat(text, " ", max_size);
700 strncat(text, argv[count], max_size);
701 max_size -= strlen(argv[count]);
707 data.ioc_inllen1 = strlen(text) + 1;
708 data.ioc_inlbuf1 = text;
709 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
710 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
714 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
716 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
723 static struct mod_paths {
726 {"libcfs", "libcfs/libcfs"},
727 {"lnet", "lnet/lnet"},
728 {"kciblnd", "lnet/klnds/ciblnd"},
729 {"kgmlnd", "lnet/klnds/gmlnd"},
730 {"kmxlnd", "lnet/klnds/mxlnd"},
731 {"kiiblnd", "lnet/klnds/iiblnd"},
732 {"ko2iblnd", "lnet/klnds/o2iblnd"},
733 {"kopeniblnd", "lnet/klnds/openiblnd"},
734 {"kptllnd", "lnet/klnds/ptllnd"},
735 {"kqswlnd", "lnet/klnds/qswlnd"},
736 {"kralnd", "lnet/klnds/ralnd"},
737 {"ksocklnd", "lnet/klnds/socklnd"},
738 {"ktdilnd", "lnet/klnds/tdilnd"},
739 {"kviblnd", "lnet/klnds/viblnd"},
740 {"lvfs", "lustre/lvfs"},
741 {"obdclass", "lustre/obdclass"},
742 {"llog_test", "lustre/obdclass"},
743 {"ptlrpc_gss", "lustre/ptlrpc/gss"},
744 {"ptlrpc", "lustre/ptlrpc"},
745 {"gks", "lustre/sec/gks"},
746 {"gkc", "lustre/sec/gks"},
747 {"ost", "lustre/ost"},
748 {"osc", "lustre/osc"},
749 {"mds", "lustre/mds"},
750 {"mdc", "lustre/mdc"},
751 {"llite", "lustre/llite"},
752 {"lustre", "lustre/llite"},
753 {"llite_lloop", "lustre/llite"},
754 {"ldiskfs", "ldiskfs/ldiskfs"},
755 {"smfs", "lustre/smfs"},
756 {"obdecho", "lustre/obdecho"},
757 {"ldlm", "lustre/ldlm"},
758 {"obdfilter", "lustre/obdfilter"},
759 {"lov", "lustre/lov"},
760 {"lmv", "lustre/lmv"},
761 {"fsfilt_ext3", "lustre/lvfs"},
762 {"fsfilt_reiserfs", "lustre/lvfs"},
763 {"fsfilt_smfs", "lustre/lvfs"},
764 {"fsfilt_ldiskfs", "lustre/lvfs"},
765 {"mds_ext3", "lustre/mds"},
766 {"cobd", "lustre/cobd"},
767 {"cmobd", "lustre/cmobd"},
768 {"lquota", "lustre/quota"},
769 {"mgs", "lustre/mgs"},
770 {"mgc", "lustre/mgc"},
771 {"mdt", "lustre/mdt"},
772 {"mdd", "lustre/mdd"},
773 {"osd", "lustre/osd"},
774 {"cmm", "lustre/cmm"},
775 {"fid", "lustre/fid"},
776 {"fld", "lustre/fld"},
780 static int jt_dbg_modules_2_4(int argc, char **argv)
785 static int jt_dbg_modules_2_5(int argc, char **argv)
787 struct mod_paths *mp;
789 char *kernel = "linux";
790 const char *proc = "/proc/modules";
791 char modname[128], buf[4096];
800 printf("%s [path] [kernel]\n", argv[0]);
804 file = fopen(proc, "r");
806 printf("failed open %s: %s\n", proc, strerror(errno));
810 while (fgets(buf, sizeof(buf), file) != NULL) {
811 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
812 for (mp = mod_paths; mp->name != NULL; mp++) {
813 if (!strcmp(mp->name, modname))
817 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
818 path, path[0] ? "/" : "",
819 mp->path, mp->name, modaddr);
828 int jt_dbg_modules(int argc, char **argv)
831 struct utsname sysinfo;
833 rc = uname(&sysinfo);
835 printf("uname() failed: %s\n", strerror(errno));
839 if (sysinfo.release[2] > '4') {
840 return jt_dbg_modules_2_5(argc, argv);
842 return jt_dbg_modules_2_4(argc, argv);
848 int jt_dbg_panic(int argc, char **argv)
851 struct libcfs_ioctl_data data;
854 fprintf(stderr, "usage: %s\n", argv[0]);
858 memset(&data, 0, sizeof(data));
859 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
860 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
864 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
866 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",