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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 * Some day I'll split all of this functionality into a cfs_debug module
33 * of its own. That day is not today.
36 #define __USE_FILE_OFFSET64
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
54 #include <linux/types.h>
56 #include <libcfs/util/ioctl.h>
57 #include <libcfs/util/param.h>
58 #include <linux/lnet/libcfs_debug.h>
59 #include <linux/lnet/lnetctl.h>
60 #include <libcfs/util/string.h>
62 static char rawbuf[8192];
63 static char *buf = rawbuf;
64 static int max = 8192;
65 /*static int g_pfd = -1;*/
66 static int subsystem_mask = ~0;
67 static int debug_mask = ~0;
69 #define MAX_MARK_SIZE 256
71 static const char *const libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
72 static const char *const libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
74 #define DAEMON_CTL_NAME "daemon_file"
75 #define SUBSYS_DEBUG_CTL_NAME "subsystem_debug"
76 #define DEBUG_CTL_NAME "debug"
77 #define DUMP_KERNEL_CTL_NAME "dump_kernel"
80 * Open the parameter file "debug" which controls the debugging
81 * flags used to determine what information ends up in the lustre
82 * logs collected by lctl dk or the debug daemon.
85 dbg_open_ctlhandle(const char *str)
90 rc = cfs_get_param_paths(&path, "%s", str);
92 fprintf(stderr, "invalid parameter '%s'\n", str);
96 fd = open(path.gl_pathv[0], O_WRONLY);
98 fprintf(stderr, "open '%s' failed: %s\n",
99 path.gl_pathv[0], strerror(errno));
101 cfs_free_param_data(&path);
106 dbg_close_ctlhandle(int fd)
112 dbg_write_cmd(int fd, char *str, int len)
114 int rc = write(fd, str, len);
116 return (rc == len ? 0 : 1);
120 static int do_debug_mask(char *name, int enable)
125 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
126 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
127 strcasecmp(name, "all_subs") == 0) {
128 printf("%s output from subsystem \"%s\"\n",
129 enable ? "Enabling" : "Disabling",
130 libcfs_debug_subsystems[i]);
132 subsystem_mask |= (1 << i);
134 subsystem_mask &= ~(1 << i);
138 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
139 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
140 strcasecmp(name, "all_types") == 0) {
141 printf("%s output of type \"%s\"\n",
142 enable ? "Enabling" : "Disabling",
143 libcfs_debug_masks[i]);
145 debug_mask |= (1 << i);
147 debug_mask &= ~(1 << i);
155 int dbg_initialize(int argc, char **argv)
160 int jt_dbg_filter(int argc, char **argv)
165 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
170 for (i = 1; i < argc; i++)
171 if (!do_debug_mask(argv[i], 0))
172 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
177 int jt_dbg_show(int argc, char **argv)
182 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
187 for (i = 1; i < argc; i++)
188 if (!do_debug_mask(argv[i], 1))
189 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
195 static int applymask(char *param, int value)
199 int len = scnprintf(buf, sizeof(buf), "%d", value);
201 int fd = dbg_open_ctlhandle(param);
205 rc = dbg_write_cmd(fd, buf, len+1);
207 fprintf(stderr, "Write to %s failed: %s\n",
208 param, strerror(errno));
211 dbg_close_ctlhandle(fd);
216 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
218 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
219 applymask(DEBUG_CTL_NAME, debug_mask);
220 printf("Applied subsystem_debug=%d, debug=%d to lnet\n",
221 subs_mask, debug_mask);
224 int jt_dbg_list(int argc, char **argv)
229 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
233 if (strcasecmp(argv[1], "subs") == 0) {
234 printf("Subsystems: all_subs");
235 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
236 if (libcfs_debug_subsystems[i][0])
237 printf(", %s", libcfs_debug_subsystems[i]);
239 } else if (strcasecmp(argv[1], "types") == 0) {
240 printf("Types: all_types");
241 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
242 printf(", %s", libcfs_debug_masks[i]);
244 } else if (strcasecmp(argv[1], "applymasks") == 0) {
245 applymask_all(subsystem_mask, debug_mask);
250 /* all strings nul-terminated; only the struct and hdr need to be freed */
252 struct ptldebug_header *hdr;
258 static int cmp_rec(const void *p1, const void *p2)
260 struct dbg_line *d1 = *(struct dbg_line **)p1;
261 struct dbg_line *d2 = *(struct dbg_line **)p2;
263 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
265 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
266 d1->hdr->ph_usec < d2->hdr->ph_usec)
268 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
269 d1->hdr->ph_usec == d2->hdr->ph_usec)
274 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
276 struct dbg_line **linev = *linevp;
279 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
280 for (i = 0; i < used; i++) {
281 struct dbg_line *line = linev[i];
282 struct ptldebug_header *hdr = line->hdr;
286 ssize_t bytes_written;
288 bytes = scnprintf(out, sizeof(out),
289 "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:"
291 hdr->ph_subsys, hdr->ph_mask,
292 hdr->ph_cpu_id, hdr->ph_type,
293 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
294 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
295 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
296 line->file, hdr->ph_line_num, line->fn,
299 bytes_written = write(fdout, buf, bytes);
300 if (bytes_written <= 0)
302 bytes -= bytes_written;
303 buf += bytes_written;
312 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
315 struct dbg_line **linev = *linevp;
318 int nlen = *lenp + 4096;
319 int nsize = nlen * sizeof(struct dbg_line *);
321 linev = realloc(*linevp, nsize);
333 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
335 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
336 fprintf(stderr, " len = %u\n", hdr->ph_len);
337 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
338 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
339 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
340 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
341 fprintf(stderr, " type = %u\n", hdr->ph_type);
342 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
343 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
344 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
345 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
346 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
347 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
350 #define HDR_SIZE sizeof(*hdr)
352 static int parse_buffer(int fdin, int fdout)
354 struct dbg_line *line;
355 struct ptldebug_header *hdr;
358 unsigned long dropped = 0;
359 unsigned long kept = 0;
360 unsigned long bad = 0;
361 struct dbg_line **linev = NULL;
374 rc = read(fdin, ptr, count);
383 if (hdr->ph_len > 4094 || /* is this header bogus? */
384 hdr->ph_stack > 65536 ||
385 hdr->ph_sec < (1 << 30) ||
386 hdr->ph_usec > 1000000000 ||
387 hdr->ph_line_num > 65536) {
389 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
393 /* try to restart on next line */
394 while (count < HDR_SIZE && buf[count] != '\n')
396 if (buf[count] == '\n')
397 count++; /* move past '\n' */
398 if (HDR_SIZE - count > 0) {
399 int left = HDR_SIZE - count;
401 memmove(buf, buf + count, left);
410 if (hdr->ph_len == 0)
413 count = hdr->ph_len - HDR_SIZE;
415 rc = read(fdin, ptr, count);
426 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
427 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
433 line = malloc(sizeof(*line));
436 fprintf(stderr, "error: line malloc(%u): "
437 "printing accumulated records\n",
438 (unsigned int)sizeof(*line));
439 print_rec(&linev, kept, fdout);
443 fprintf(stderr, "error: line malloc(%u): exiting\n",
444 (unsigned int)sizeof(*line));
448 line->hdr = malloc(hdr->ph_len + 1);
449 if (line->hdr == NULL) {
452 fprintf(stderr, "error: hdr malloc(%u): "
453 "printing accumulated records\n",
455 print_rec(&linev, kept, fdout);
459 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
464 ptr = (void *)line->hdr;
465 memcpy(line->hdr, buf, hdr->ph_len);
466 ptr[hdr->ph_len] = '\0';
470 ptr += strlen(line->file) + 1;
472 ptr += strlen(line->fn) + 1;
476 if (add_rec(line, &linev, &linev_len, kept) < 0) {
478 fprintf(stderr, "error: add_rec[%u] failed; "
479 "print accumulated records\n",
481 print_rec(&linev, kept, fdout);
485 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
493 print_rec(&linev, kept, fdout);
495 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
496 dropped + kept + bad, kept, dropped, bad);
501 int jt_dbg_debug_kernel(int argc, char **argv)
504 char filename[PATH_MAX];
512 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
518 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
523 /* If we are dumping raw (which means no conversion step to ASCII)
524 * then dump directly to any supplied filename, otherwise this is
525 * just a temp file and we dump to the real file at convert time. */
526 if (argc > 1 && raw) {
527 if (strlen(argv[1]) >= sizeof(filename)) {
528 fprintf(stderr, "File name too long: %s\n", argv[1]);
531 strncpy(filename, argv[1], sizeof(filename));
533 if (snprintf(filename, sizeof(filename), "%s%lu.%u",
534 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
536 >= sizeof(filename)) {
537 fprintf(stderr, "File name too long\n");
542 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
545 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
547 fprintf(stderr, "open(dump_kernel) failed: %s\n",
552 rc = dbg_write_cmd(fdin, filename, strlen(filename));
554 dbg_close_ctlhandle(fdin);
556 fprintf(stderr, "write(%s) failed: %s\n", filename,
557 strerror(save_errno));
564 fdin = open(filename, O_RDONLY);
566 if (errno == ENOENT) /* no dump file created */
568 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
573 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
576 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
582 fdout = fileno(stdout);
585 rc = parse_buffer(fdin, fdout);
590 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
591 "behind.\n", filename);
593 rc = unlink(filename);
595 fprintf(stderr, "dumped successfully, but couldn't "
596 "unlink tmp file %s: %s\n", filename,
603 int jt_dbg_debug_file(int argc, char **argv)
609 if (argc > 3 || argc < 2) {
610 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
614 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
616 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
621 fdout = open(argv[2],
622 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
625 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
631 fdout = fileno(stdout);
634 rc = parse_buffer(fdin, fdout);
637 if (fdout != fileno(stdout))
643 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
645 int jt_dbg_debug_daemon(int argc, char **argv)
649 char *resolved_path = NULL;
652 fprintf(stderr, debug_daemon_usage, argv[0]);
656 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
661 if (strcasecmp(argv[1], "start") == 0) {
662 if (argc < 3 || argc > 4 ||
663 (argc == 4 && strlen(argv[3]) > 5)) {
664 fprintf(stderr, debug_daemon_usage, argv[0]);
669 const long min_size = 10;
670 const long max_size = 20480;
674 size = strtoul(argv[3], &end, 0);
675 if (size < min_size ||
678 fprintf(stderr, "size %s invalid, must be in "
679 "the range %ld-%ld MB\n", argv[3],
683 snprintf(buf, sizeof(buf), "size=%ld", size);
684 rc = dbg_write_cmd(fd, buf, strlen(buf));
687 fprintf(stderr, "set %s failed: %s\n",
688 buf, strerror(errno));
693 rc = cfs_abs_path(argv[2], &resolved_path);
696 "%s debug_daemon: cannot resolve path '%s': %s\n",
697 program_invocation_short_name, argv[2],
701 rc = dbg_write_cmd(fd, resolved_path, strlen(resolved_path));
703 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
704 argv[2], strerror(errno));
710 if (strcasecmp(argv[1], "stop") == 0) {
711 rc = dbg_write_cmd(fd, "stop", 4);
713 fprintf(stderr, "stopping debug_daemon failed: %s\n",
722 fprintf(stderr, debug_daemon_usage, argv[0]);
725 dbg_close_ctlhandle(fd);
726 if (resolved_path != NULL)
731 int jt_dbg_clear_debug_buf(int argc, char **argv)
734 struct libcfs_ioctl_data data;
737 fprintf(stderr, "usage: %s\n", argv[0]);
741 memset(&data, 0, sizeof(data));
742 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
743 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
747 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
749 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
756 int jt_dbg_mark_debug_buf(int argc, char **argv)
758 static char scratch[MAX_MARK_SIZE] = "";
759 struct libcfs_ioctl_data data;
763 memset(&data, 0, sizeof(data));
766 int count, max_size = sizeof(scratch) - 1;
768 strncpy(scratch, argv[1], max_size);
769 max_size -= strlen(argv[1]);
770 for (count = 2; (count < argc) && (max_size > 1); count++) {
771 strncat(scratch, " ", max_size);
773 strncat(scratch, argv[count], max_size);
774 max_size -= strlen(argv[count]);
776 scratch[sizeof(scratch) - 1] = '\0';
779 time_t now = time(NULL);
783 data.ioc_inllen1 = strlen(text) + 1;
784 data.ioc_inlbuf1 = text;
786 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
787 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
791 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
793 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
800 static struct mod_paths {
803 { .name = "libcfs", .path = "libcfs/libcfs" },
804 { .name = "lnet", .path = "lnet/lnet" },
805 { .name = "ko2iblnd", .path = "lnet/klnds/o2iblnd" },
806 { .name = "kgnilnd", .path = "lnet/klnds/gnilnd"},
807 { .name = "ksocklnd", .path = "lnet/klnds/socklnd" },
808 { .name = "obdclass", .path = "lustre/obdclass" },
809 { .name = "llog_test", .path = "lustre/obdclass" },
810 { .name = "ptlrpc_gss", .path = "lustre/ptlrpc/gss" },
811 { .name = "ptlrpc", .path = "lustre/ptlrpc" },
812 { .name = "gks", .path = "lustre/sec/gks" },
813 { .name = "gkc", .path = "lustre/sec/gks" },
814 { .name = "ost", .path = "lustre/ost" },
815 { .name = "osc", .path = "lustre/osc" },
816 { .name = "mds", .path = "lustre/mds" },
817 { .name = "mdc", .path = "lustre/mdc" },
818 { .name = "lustre", .path = "lustre/llite" },
819 { .name = "ldiskfs", .path = "ldiskfs" },
820 { .name = "obdecho", .path = "lustre/obdecho" },
821 { .name = "ldlm", .path = "lustre/ldlm" },
822 { .name = "obdfilter", .path = "lustre/obdfilter" },
823 { .name = "lov", .path = "lustre/lov" },
824 { .name = "lmv", .path = "lustre/lmv" },
825 { .name = "lquota", .path = "lustre/quota" },
826 { .name = "mgs", .path = "lustre/mgs" },
827 { .name = "mgc", .path = "lustre/mgc" },
828 { .name = "mdt", .path = "lustre/mdt" },
829 { .name = "mdd", .path = "lustre/mdd" },
830 { .name = "osd", .path = "lustre/osd" },
831 { .name = "cmm", .path = "lustre/cmm" },
832 { .name = "fid", .path = "lustre/fid"},
833 { .name = "fld", .path = "lustre/fld"},
834 { .name = "lod", .path = "lustre/lod"},
835 { .name = "osp", .path = "lustre/osp"},
836 { .name = "lfsck", .path = "lustre/lfsck" },
840 int jt_dbg_modules(int argc, char **argv)
842 struct mod_paths *mp;
844 const char *proc = "/proc/modules";
847 unsigned long modaddr;
853 printf("%s [path] [kernel]\n", argv[0]);
857 file = fopen(proc, "r");
859 printf("failed open %s: %s\n", proc, strerror(errno));
863 while (fgets(buf, sizeof(buf), file) != NULL) {
864 if (sscanf(buf, "%s %*s %*s %*s %*s %lx",
865 modname, &modaddr) == 2) {
866 for (mp = mod_paths; mp->name != NULL; mp++) {
867 if (!strcmp(mp->name, modname))
871 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
872 path, path[0] ? "/" : "",
873 mp->path, mp->name, modaddr);