4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
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
53 #include <sys/ioctl.h>
54 #include <sys/types.h>
59 #include <linux/types.h>
61 #include <libcfs/util/ioctl.h>
62 #include <libcfs/util/param.h>
63 #include <libcfs/user-time.h>
64 #include <libcfs/libcfs_debug.h>
65 #include <lnet/lnetctl.h>
67 static char rawbuf[8192];
68 static char *buf = rawbuf;
69 static int max = 8192;
70 /*static int g_pfd = -1;*/
71 static int subsystem_mask = ~0;
72 static int debug_mask = ~0;
74 #define MAX_MARK_SIZE 256
76 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
77 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
79 #define DAEMON_CTL_NAME "daemon_file"
80 #define SUBSYS_DEBUG_CTL_NAME "subsystem_debug"
81 #define DEBUG_CTL_NAME "debug"
82 #define DUMP_KERNEL_CTL_NAME "dump_kernel"
85 * Open the parameter file "debug" which controls the debugging
86 * flags used to determine what information ends up in the lustre
87 * logs collected by lctl dk or the debug daemon.
90 dbg_open_ctlhandle(const char *str)
95 rc = cfs_get_param_paths(&path, str);
97 fprintf(stderr, "invalid parameter '%s'\n", str);
101 fd = open(path.gl_pathv[0], O_WRONLY);
103 fprintf(stderr, "open '%s' failed: %s\n",
104 path.gl_pathv[0], strerror(errno));
106 cfs_free_param_data(&path);
111 dbg_close_ctlhandle(int fd)
117 dbg_write_cmd(int fd, char *str, int len)
119 int rc = write(fd, str, len);
121 return (rc == len ? 0 : 1);
125 static int do_debug_mask(char *name, int enable)
130 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
131 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
132 strcasecmp(name, "all_subs") == 0) {
133 printf("%s output from subsystem \"%s\"\n",
134 enable ? "Enabling" : "Disabling",
135 libcfs_debug_subsystems[i]);
137 subsystem_mask |= (1 << i);
139 subsystem_mask &= ~(1 << i);
143 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
144 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
145 strcasecmp(name, "all_types") == 0) {
146 printf("%s output of type \"%s\"\n",
147 enable ? "Enabling" : "Disabling",
148 libcfs_debug_masks[i]);
150 debug_mask |= (1 << i);
152 debug_mask &= ~(1 << i);
160 int dbg_initialize(int argc, char **argv)
165 int jt_dbg_filter(int argc, char **argv)
170 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
175 for (i = 1; i < argc; i++)
176 if (!do_debug_mask(argv[i], 0))
177 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
182 int jt_dbg_show(int argc, char **argv)
187 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
192 for (i = 1; i < argc; i++)
193 if (!do_debug_mask(argv[i], 1))
194 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
200 static int applymask(char *param, int value)
204 int len = snprintf(buf, 64, "%d", value);
206 int fd = dbg_open_ctlhandle(param);
210 rc = dbg_write_cmd(fd, buf, len+1);
212 fprintf(stderr, "Write to %s failed: %s\n",
213 param, strerror(errno));
216 dbg_close_ctlhandle(fd);
221 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
223 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
224 applymask(DEBUG_CTL_NAME, debug_mask);
225 printf("Applied subsystem_debug=%d, debug=%d to lnet\n",
226 subs_mask, debug_mask);
229 int jt_dbg_list(int argc, char **argv)
234 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
238 if (strcasecmp(argv[1], "subs") == 0) {
239 printf("Subsystems: all_subs");
240 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
241 if (libcfs_debug_subsystems[i][0])
242 printf(", %s", libcfs_debug_subsystems[i]);
244 } else if (strcasecmp(argv[1], "types") == 0) {
245 printf("Types: all_types");
246 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
247 printf(", %s", libcfs_debug_masks[i]);
249 } else if (strcasecmp(argv[1], "applymasks") == 0) {
250 applymask_all(subsystem_mask, debug_mask);
255 /* all strings nul-terminated; only the struct and hdr need to be freed */
257 struct ptldebug_header *hdr;
263 static int cmp_rec(const void *p1, const void *p2)
265 struct dbg_line *d1 = *(struct dbg_line **)p1;
266 struct dbg_line *d2 = *(struct dbg_line **)p2;
268 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
270 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
271 d1->hdr->ph_usec < d2->hdr->ph_usec)
273 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
274 d1->hdr->ph_usec == d2->hdr->ph_usec)
279 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
281 struct dbg_line **linev = *linevp;
284 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
285 for (i = 0; i < used; i++) {
286 struct dbg_line *line = linev[i];
287 struct ptldebug_header *hdr = line->hdr;
291 ssize_t bytes_written;
293 bytes = snprintf(out, sizeof(out),
294 "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:"
296 hdr->ph_subsys, hdr->ph_mask,
297 hdr->ph_cpu_id, hdr->ph_type,
298 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
299 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
300 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
301 line->file, hdr->ph_line_num, line->fn,
304 bytes_written = write(fdout, buf, bytes);
305 if (bytes_written <= 0)
307 bytes -= bytes_written;
308 buf += bytes_written;
317 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
320 struct dbg_line **linev = *linevp;
323 int nlen = *lenp + 4096;
324 int nsize = nlen * sizeof(struct dbg_line *);
326 linev = realloc(*linevp, nsize);
338 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
340 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
341 fprintf(stderr, " len = %u\n", hdr->ph_len);
342 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
343 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
344 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
345 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
346 fprintf(stderr, " type = %u\n", hdr->ph_type);
347 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
348 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
349 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
350 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
351 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
352 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
355 #define HDR_SIZE sizeof(*hdr)
357 static int parse_buffer(int fdin, int fdout)
359 struct dbg_line *line;
360 struct ptldebug_header *hdr;
363 unsigned long dropped = 0;
364 unsigned long kept = 0;
365 unsigned long bad = 0;
366 struct dbg_line **linev = NULL;
379 rc = read(fdin, ptr, count);
388 if (hdr->ph_len > 4094 || /* is this header bogus? */
389 hdr->ph_stack > 65536 ||
390 hdr->ph_sec < (1 << 30) ||
391 hdr->ph_usec > 1000000000 ||
392 hdr->ph_line_num > 65536) {
394 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
398 /* try to restart on next line */
399 while (count < HDR_SIZE && buf[count] != '\n')
401 if (buf[count] == '\n')
402 count++; /* move past '\n' */
403 if (HDR_SIZE - count > 0) {
404 int left = HDR_SIZE - count;
406 memmove(buf, buf + count, left);
415 if (hdr->ph_len == 0)
418 count = hdr->ph_len - HDR_SIZE;
420 rc = read(fdin, ptr, count);
431 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
432 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
438 line = malloc(sizeof(*line));
441 fprintf(stderr, "error: line malloc(%u): "
442 "printing accumulated records\n",
443 (unsigned int)sizeof(*line));
444 print_rec(&linev, kept, fdout);
448 fprintf(stderr, "error: line malloc(%u): exiting\n",
449 (unsigned int)sizeof(*line));
453 line->hdr = malloc(hdr->ph_len + 1);
454 if (line->hdr == NULL) {
457 fprintf(stderr, "error: hdr malloc(%u): "
458 "printing accumulated records\n",
460 print_rec(&linev, kept, fdout);
464 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
469 ptr = (void *)line->hdr;
470 memcpy(line->hdr, buf, hdr->ph_len);
471 ptr[hdr->ph_len] = '\0';
475 ptr += strlen(line->file) + 1;
477 ptr += strlen(line->fn) + 1;
481 if (add_rec(line, &linev, &linev_len, kept) < 0) {
483 fprintf(stderr, "error: add_rec[%u] failed; "
484 "print accumulated records\n",
486 print_rec(&linev, kept, fdout);
490 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
498 print_rec(&linev, kept, fdout);
500 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
501 dropped + kept + bad, kept, dropped, bad);
506 int jt_dbg_debug_kernel(int argc, char **argv)
509 char filename[PATH_MAX];
517 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
523 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
528 /* If we are dumping raw (which means no conversion step to ASCII)
529 * then dump directly to any supplied filename, otherwise this is
530 * just a temp file and we dump to the real file at convert time. */
531 if (argc > 1 && raw) {
532 if (strlen(argv[1]) >= sizeof(filename)) {
533 fprintf(stderr, "File name too long: %s\n", argv[1]);
536 strncpy(filename, argv[1], sizeof(filename));
538 if (snprintf(filename, sizeof(filename), "%s"CFS_TIME_T".%u",
539 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
541 >= sizeof(filename)) {
542 fprintf(stderr, "File name too long\n");
547 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
550 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
552 fprintf(stderr, "open(dump_kernel) failed: %s\n",
557 rc = dbg_write_cmd(fdin, filename, strlen(filename));
559 dbg_close_ctlhandle(fdin);
561 fprintf(stderr, "write(%s) failed: %s\n", filename,
562 strerror(save_errno));
569 fdin = open(filename, O_RDONLY);
571 if (errno == ENOENT) /* no dump file created */
573 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
578 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
581 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
587 fdout = fileno(stdout);
590 rc = parse_buffer(fdin, fdout);
595 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
596 "behind.\n", filename);
598 rc = unlink(filename);
600 fprintf(stderr, "dumped successfully, but couldn't "
601 "unlink tmp file %s: %s\n", filename,
608 int jt_dbg_debug_file(int argc, char **argv)
614 if (argc > 3 || argc < 2) {
615 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
619 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
621 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
626 fdout = open(argv[2],
627 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
630 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
636 fdout = fileno(stdout);
639 rc = parse_buffer(fdin, fdout);
642 if (fdout != fileno(stdout))
648 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
650 int jt_dbg_debug_daemon(int argc, char **argv)
656 fprintf(stderr, debug_daemon_usage, argv[0]);
660 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
665 if (strcasecmp(argv[1], "start") == 0) {
666 if (argc < 3 || argc > 4 ||
667 (argc == 4 && strlen(argv[3]) > 5)) {
668 fprintf(stderr, debug_daemon_usage, argv[0]);
673 const long min_size = 10;
674 const long max_size = 20480;
678 size = strtoul(argv[3], &end, 0);
679 if (size < min_size ||
682 fprintf(stderr, "size %s invalid, must be in "
683 "the range %ld-%ld MB\n", argv[3],
687 snprintf(buf, sizeof(buf), "size=%ld", size);
688 rc = dbg_write_cmd(fd, buf, strlen(buf));
691 fprintf(stderr, "set %s failed: %s\n",
692 buf, strerror(errno));
697 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
699 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
700 argv[2], strerror(errno));
706 if (strcasecmp(argv[1], "stop") == 0) {
707 rc = dbg_write_cmd(fd, "stop", 4);
709 fprintf(stderr, "stopping debug_daemon failed: %s\n",
718 fprintf(stderr, debug_daemon_usage, argv[0]);
721 dbg_close_ctlhandle(fd);
725 int jt_dbg_clear_debug_buf(int argc, char **argv)
728 struct libcfs_ioctl_data data;
731 fprintf(stderr, "usage: %s\n", argv[0]);
735 memset(&data, 0, sizeof(data));
736 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
737 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
741 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
743 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
750 int jt_dbg_mark_debug_buf(int argc, char **argv)
752 static char scratch[MAX_MARK_SIZE] = "";
753 struct libcfs_ioctl_data data;
757 memset(&data, 0, sizeof(data));
760 int count, max_size = sizeof(scratch) - 1;
762 strncpy(scratch, argv[1], max_size);
763 max_size -= strlen(argv[1]);
764 for (count = 2; (count < argc) && (max_size > 1); count++) {
765 strncat(scratch, " ", max_size);
767 strncat(scratch, argv[count], max_size);
768 max_size -= strlen(argv[count]);
770 scratch[sizeof(scratch) - 1] = '\0';
773 time_t now = time(NULL);
777 data.ioc_inllen1 = strlen(text) + 1;
778 data.ioc_inlbuf1 = text;
780 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
781 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
785 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
787 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
794 static struct mod_paths {
797 { "libcfs", "libcfs/libcfs" },
798 { "lnet", "lnet/lnet" },
799 { "ko2iblnd", "lnet/klnds/o2iblnd" },
800 { "kgnilnd", "lnet/klnds/gnilnd"},
801 { "ksocklnd", "lnet/klnds/socklnd" },
802 { "obdclass", "lustre/obdclass" },
803 { "llog_test", "lustre/obdclass" },
804 { "ptlrpc_gss", "lustre/ptlrpc/gss" },
805 { "ptlrpc", "lustre/ptlrpc" },
806 { "gks", "lustre/sec/gks" },
807 { "gkc", "lustre/sec/gks" },
808 { "ost", "lustre/ost" },
809 { "osc", "lustre/osc" },
810 { "mds", "lustre/mds" },
811 { "mdc", "lustre/mdc" },
812 { "lustre", "lustre/llite" },
813 { "llite_lloop", "lustre/llite" },
814 { "ldiskfs", "ldiskfs" },
815 { "obdecho", "lustre/obdecho" },
816 { "ldlm", "lustre/ldlm" },
817 { "obdfilter", "lustre/obdfilter" },
818 { "lov", "lustre/lov" },
819 { "lmv", "lustre/lmv" },
820 { "lquota", "lustre/quota" },
821 { "mgs", "lustre/mgs" },
822 { "mgc", "lustre/mgc" },
823 { "mdt", "lustre/mdt" },
824 { "mdd", "lustre/mdd" },
825 { "osd", "lustre/osd" },
826 { "cmm", "lustre/cmm" },
827 {"fid", "lustre/fid"},
828 {"fld", "lustre/fld"},
829 {"lod", "lustre/lod"},
830 {"osp", "lustre/osp"},
831 { "lfsck", "lustre/lfsck" },
835 int jt_dbg_modules(int argc, char **argv)
837 struct mod_paths *mp;
839 const char *proc = "/proc/modules";
842 unsigned long modaddr;
848 printf("%s [path] [kernel]\n", argv[0]);
852 file = fopen(proc, "r");
854 printf("failed open %s: %s\n", proc, strerror(errno));
858 while (fgets(buf, sizeof(buf), file) != NULL) {
859 if (sscanf(buf, "%s %*s %*s %*s %*s %lx",
860 modname, &modaddr) == 2) {
861 for (mp = mod_paths; mp->name != NULL; mp++) {
862 if (!strcmp(mp->name, modname))
866 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
867 path, path[0] ? "/" : "",
868 mp->path, mp->name, modaddr);