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/libcfs_debug.h>
64 #include <lnet/lnetctl.h>
66 static char rawbuf[8192];
67 static char *buf = rawbuf;
68 static int max = 8192;
69 /*static int g_pfd = -1;*/
70 static int subsystem_mask = ~0;
71 static int debug_mask = ~0;
73 #define MAX_MARK_SIZE 256
75 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
76 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
78 #define DAEMON_CTL_NAME "daemon_file"
79 #define SUBSYS_DEBUG_CTL_NAME "subsystem_debug"
80 #define DEBUG_CTL_NAME "debug"
81 #define DUMP_KERNEL_CTL_NAME "dump_kernel"
84 * Open the parameter file "debug" which controls the debugging
85 * flags used to determine what information ends up in the lustre
86 * logs collected by lctl dk or the debug daemon.
89 dbg_open_ctlhandle(const char *str)
94 rc = cfs_get_param_paths(&path, "%s", str);
96 fprintf(stderr, "invalid parameter '%s'\n", str);
100 fd = open(path.gl_pathv[0], O_WRONLY);
102 fprintf(stderr, "open '%s' failed: %s\n",
103 path.gl_pathv[0], strerror(errno));
105 cfs_free_param_data(&path);
110 dbg_close_ctlhandle(int fd)
116 dbg_write_cmd(int fd, char *str, int len)
118 int rc = write(fd, str, len);
120 return (rc == len ? 0 : 1);
124 static int do_debug_mask(char *name, int enable)
129 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
130 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
131 strcasecmp(name, "all_subs") == 0) {
132 printf("%s output from subsystem \"%s\"\n",
133 enable ? "Enabling" : "Disabling",
134 libcfs_debug_subsystems[i]);
136 subsystem_mask |= (1 << i);
138 subsystem_mask &= ~(1 << i);
142 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
143 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
144 strcasecmp(name, "all_types") == 0) {
145 printf("%s output of type \"%s\"\n",
146 enable ? "Enabling" : "Disabling",
147 libcfs_debug_masks[i]);
149 debug_mask |= (1 << i);
151 debug_mask &= ~(1 << i);
159 int dbg_initialize(int argc, char **argv)
164 int jt_dbg_filter(int argc, char **argv)
169 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
174 for (i = 1; i < argc; i++)
175 if (!do_debug_mask(argv[i], 0))
176 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
181 int jt_dbg_show(int argc, char **argv)
186 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
191 for (i = 1; i < argc; i++)
192 if (!do_debug_mask(argv[i], 1))
193 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
199 static int applymask(char *param, int value)
203 int len = snprintf(buf, 64, "%d", value);
205 int fd = dbg_open_ctlhandle(param);
209 rc = dbg_write_cmd(fd, buf, len+1);
211 fprintf(stderr, "Write to %s failed: %s\n",
212 param, strerror(errno));
215 dbg_close_ctlhandle(fd);
220 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
222 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
223 applymask(DEBUG_CTL_NAME, debug_mask);
224 printf("Applied subsystem_debug=%d, debug=%d to lnet\n",
225 subs_mask, debug_mask);
228 int jt_dbg_list(int argc, char **argv)
233 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
237 if (strcasecmp(argv[1], "subs") == 0) {
238 printf("Subsystems: all_subs");
239 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
240 if (libcfs_debug_subsystems[i][0])
241 printf(", %s", libcfs_debug_subsystems[i]);
243 } else if (strcasecmp(argv[1], "types") == 0) {
244 printf("Types: all_types");
245 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
246 printf(", %s", libcfs_debug_masks[i]);
248 } else if (strcasecmp(argv[1], "applymasks") == 0) {
249 applymask_all(subsystem_mask, debug_mask);
254 /* all strings nul-terminated; only the struct and hdr need to be freed */
256 struct ptldebug_header *hdr;
262 static int cmp_rec(const void *p1, const void *p2)
264 struct dbg_line *d1 = *(struct dbg_line **)p1;
265 struct dbg_line *d2 = *(struct dbg_line **)p2;
267 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
269 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
270 d1->hdr->ph_usec < d2->hdr->ph_usec)
272 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
273 d1->hdr->ph_usec == d2->hdr->ph_usec)
278 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
280 struct dbg_line **linev = *linevp;
283 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
284 for (i = 0; i < used; i++) {
285 struct dbg_line *line = linev[i];
286 struct ptldebug_header *hdr = line->hdr;
290 ssize_t bytes_written;
292 bytes = snprintf(out, sizeof(out),
293 "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:"
295 hdr->ph_subsys, hdr->ph_mask,
296 hdr->ph_cpu_id, hdr->ph_type,
297 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
298 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
299 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
300 line->file, hdr->ph_line_num, line->fn,
303 bytes_written = write(fdout, buf, bytes);
304 if (bytes_written <= 0)
306 bytes -= bytes_written;
307 buf += bytes_written;
316 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
319 struct dbg_line **linev = *linevp;
322 int nlen = *lenp + 4096;
323 int nsize = nlen * sizeof(struct dbg_line *);
325 linev = realloc(*linevp, nsize);
337 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
339 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
340 fprintf(stderr, " len = %u\n", hdr->ph_len);
341 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
342 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
343 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
344 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
345 fprintf(stderr, " type = %u\n", hdr->ph_type);
346 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
347 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
348 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
349 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
350 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
351 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
354 #define HDR_SIZE sizeof(*hdr)
356 static int parse_buffer(int fdin, int fdout)
358 struct dbg_line *line;
359 struct ptldebug_header *hdr;
362 unsigned long dropped = 0;
363 unsigned long kept = 0;
364 unsigned long bad = 0;
365 struct dbg_line **linev = NULL;
378 rc = read(fdin, ptr, count);
387 if (hdr->ph_len > 4094 || /* is this header bogus? */
388 hdr->ph_stack > 65536 ||
389 hdr->ph_sec < (1 << 30) ||
390 hdr->ph_usec > 1000000000 ||
391 hdr->ph_line_num > 65536) {
393 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
397 /* try to restart on next line */
398 while (count < HDR_SIZE && buf[count] != '\n')
400 if (buf[count] == '\n')
401 count++; /* move past '\n' */
402 if (HDR_SIZE - count > 0) {
403 int left = HDR_SIZE - count;
405 memmove(buf, buf + count, left);
414 if (hdr->ph_len == 0)
417 count = hdr->ph_len - HDR_SIZE;
419 rc = read(fdin, ptr, count);
430 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
431 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
437 line = malloc(sizeof(*line));
440 fprintf(stderr, "error: line malloc(%u): "
441 "printing accumulated records\n",
442 (unsigned int)sizeof(*line));
443 print_rec(&linev, kept, fdout);
447 fprintf(stderr, "error: line malloc(%u): exiting\n",
448 (unsigned int)sizeof(*line));
452 line->hdr = malloc(hdr->ph_len + 1);
453 if (line->hdr == NULL) {
456 fprintf(stderr, "error: hdr malloc(%u): "
457 "printing accumulated records\n",
459 print_rec(&linev, kept, fdout);
463 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
468 ptr = (void *)line->hdr;
469 memcpy(line->hdr, buf, hdr->ph_len);
470 ptr[hdr->ph_len] = '\0';
474 ptr += strlen(line->file) + 1;
476 ptr += strlen(line->fn) + 1;
480 if (add_rec(line, &linev, &linev_len, kept) < 0) {
482 fprintf(stderr, "error: add_rec[%u] failed; "
483 "print accumulated records\n",
485 print_rec(&linev, kept, fdout);
489 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
497 print_rec(&linev, kept, fdout);
499 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
500 dropped + kept + bad, kept, dropped, bad);
505 int jt_dbg_debug_kernel(int argc, char **argv)
508 char filename[PATH_MAX];
516 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
522 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
527 /* If we are dumping raw (which means no conversion step to ASCII)
528 * then dump directly to any supplied filename, otherwise this is
529 * just a temp file and we dump to the real file at convert time. */
530 if (argc > 1 && raw) {
531 if (strlen(argv[1]) >= sizeof(filename)) {
532 fprintf(stderr, "File name too long: %s\n", argv[1]);
535 strncpy(filename, argv[1], sizeof(filename));
537 if (snprintf(filename, sizeof(filename), "%s%lu.%u",
538 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
540 >= sizeof(filename)) {
541 fprintf(stderr, "File name too long\n");
546 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
549 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
551 fprintf(stderr, "open(dump_kernel) failed: %s\n",
556 rc = dbg_write_cmd(fdin, filename, strlen(filename));
558 dbg_close_ctlhandle(fdin);
560 fprintf(stderr, "write(%s) failed: %s\n", filename,
561 strerror(save_errno));
568 fdin = open(filename, O_RDONLY);
570 if (errno == ENOENT) /* no dump file created */
572 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
577 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
580 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
586 fdout = fileno(stdout);
589 rc = parse_buffer(fdin, fdout);
594 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
595 "behind.\n", filename);
597 rc = unlink(filename);
599 fprintf(stderr, "dumped successfully, but couldn't "
600 "unlink tmp file %s: %s\n", filename,
607 int jt_dbg_debug_file(int argc, char **argv)
613 if (argc > 3 || argc < 2) {
614 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
618 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
620 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
625 fdout = open(argv[2],
626 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
629 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
635 fdout = fileno(stdout);
638 rc = parse_buffer(fdin, fdout);
641 if (fdout != fileno(stdout))
647 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
649 int jt_dbg_debug_daemon(int argc, char **argv)
655 fprintf(stderr, debug_daemon_usage, argv[0]);
659 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
664 if (strcasecmp(argv[1], "start") == 0) {
665 if (argc < 3 || argc > 4 ||
666 (argc == 4 && strlen(argv[3]) > 5)) {
667 fprintf(stderr, debug_daemon_usage, argv[0]);
672 const long min_size = 10;
673 const long max_size = 20480;
677 size = strtoul(argv[3], &end, 0);
678 if (size < min_size ||
681 fprintf(stderr, "size %s invalid, must be in "
682 "the range %ld-%ld MB\n", argv[3],
686 snprintf(buf, sizeof(buf), "size=%ld", size);
687 rc = dbg_write_cmd(fd, buf, strlen(buf));
690 fprintf(stderr, "set %s failed: %s\n",
691 buf, strerror(errno));
696 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
698 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
699 argv[2], strerror(errno));
705 if (strcasecmp(argv[1], "stop") == 0) {
706 rc = dbg_write_cmd(fd, "stop", 4);
708 fprintf(stderr, "stopping debug_daemon failed: %s\n",
717 fprintf(stderr, debug_daemon_usage, argv[0]);
720 dbg_close_ctlhandle(fd);
724 int jt_dbg_clear_debug_buf(int argc, char **argv)
727 struct libcfs_ioctl_data data;
730 fprintf(stderr, "usage: %s\n", argv[0]);
734 memset(&data, 0, sizeof(data));
735 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
736 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
740 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
742 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
749 int jt_dbg_mark_debug_buf(int argc, char **argv)
751 static char scratch[MAX_MARK_SIZE] = "";
752 struct libcfs_ioctl_data data;
756 memset(&data, 0, sizeof(data));
759 int count, max_size = sizeof(scratch) - 1;
761 strncpy(scratch, argv[1], max_size);
762 max_size -= strlen(argv[1]);
763 for (count = 2; (count < argc) && (max_size > 1); count++) {
764 strncat(scratch, " ", max_size);
766 strncat(scratch, argv[count], max_size);
767 max_size -= strlen(argv[count]);
769 scratch[sizeof(scratch) - 1] = '\0';
772 time_t now = time(NULL);
776 data.ioc_inllen1 = strlen(text) + 1;
777 data.ioc_inlbuf1 = text;
779 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
780 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
784 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
786 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
793 static struct mod_paths {
796 { "libcfs", "libcfs/libcfs" },
797 { "lnet", "lnet/lnet" },
798 { "ko2iblnd", "lnet/klnds/o2iblnd" },
799 { "kgnilnd", "lnet/klnds/gnilnd"},
800 { "ksocklnd", "lnet/klnds/socklnd" },
801 { "obdclass", "lustre/obdclass" },
802 { "llog_test", "lustre/obdclass" },
803 { "ptlrpc_gss", "lustre/ptlrpc/gss" },
804 { "ptlrpc", "lustre/ptlrpc" },
805 { "gks", "lustre/sec/gks" },
806 { "gkc", "lustre/sec/gks" },
807 { "ost", "lustre/ost" },
808 { "osc", "lustre/osc" },
809 { "mds", "lustre/mds" },
810 { "mdc", "lustre/mdc" },
811 { "lustre", "lustre/llite" },
812 { "llite_lloop", "lustre/llite" },
813 { "ldiskfs", "ldiskfs" },
814 { "obdecho", "lustre/obdecho" },
815 { "ldlm", "lustre/ldlm" },
816 { "obdfilter", "lustre/obdfilter" },
817 { "lov", "lustre/lov" },
818 { "lmv", "lustre/lmv" },
819 { "lquota", "lustre/quota" },
820 { "mgs", "lustre/mgs" },
821 { "mgc", "lustre/mgc" },
822 { "mdt", "lustre/mdt" },
823 { "mdd", "lustre/mdd" },
824 { "osd", "lustre/osd" },
825 { "cmm", "lustre/cmm" },
826 {"fid", "lustre/fid"},
827 {"fld", "lustre/fld"},
828 {"lod", "lustre/lod"},
829 {"osp", "lustre/osp"},
830 { "lfsck", "lustre/lfsck" },
834 int jt_dbg_modules(int argc, char **argv)
836 struct mod_paths *mp;
838 const char *proc = "/proc/modules";
841 unsigned long modaddr;
847 printf("%s [path] [kernel]\n", argv[0]);
851 file = fopen(proc, "r");
853 printf("failed open %s: %s\n", proc, strerror(errno));
857 while (fgets(buf, sizeof(buf), file) != NULL) {
858 if (sscanf(buf, "%s %*s %*s %*s %*s %lx",
859 modname, &modaddr) == 2) {
860 for (mp = mod_paths; mp->name != NULL; mp++) {
861 if (!strcmp(mp->name, modname))
865 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
866 path, path[0] ? "/" : "",
867 mp->path, mp->name, modaddr);