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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * Some day I'll split all of this functionality into a cfs_debug module
34 * of its own. That day is not today.
37 #define __USE_FILE_OFFSET64
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
55 #include <linux/types.h>
57 #include <libcfs/util/ioctl.h>
58 #include <libcfs/util/param.h>
59 #include <linux/lnet/libcfs_debug.h>
60 #include <linux/lnet/lnetctl.h>
61 #include <libcfs/util/string.h>
63 static char rawbuf[8192];
64 static char *buf = rawbuf;
65 static int max = 8192;
66 /*static int g_pfd = -1;*/
67 static int subsystem_mask = ~0;
68 static int debug_mask = ~0;
70 #define MAX_MARK_SIZE 256
72 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
73 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
75 #define DAEMON_CTL_NAME "daemon_file"
76 #define SUBSYS_DEBUG_CTL_NAME "subsystem_debug"
77 #define DEBUG_CTL_NAME "debug"
78 #define DUMP_KERNEL_CTL_NAME "dump_kernel"
81 * Open the parameter file "debug" which controls the debugging
82 * flags used to determine what information ends up in the lustre
83 * logs collected by lctl dk or the debug daemon.
86 dbg_open_ctlhandle(const char *str)
91 rc = cfs_get_param_paths(&path, "%s", str);
93 fprintf(stderr, "invalid parameter '%s'\n", str);
97 fd = open(path.gl_pathv[0], O_WRONLY);
99 fprintf(stderr, "open '%s' failed: %s\n",
100 path.gl_pathv[0], strerror(errno));
102 cfs_free_param_data(&path);
107 dbg_close_ctlhandle(int fd)
113 dbg_write_cmd(int fd, char *str, int len)
115 int rc = write(fd, str, len);
117 return (rc == len ? 0 : 1);
121 static int do_debug_mask(char *name, int enable)
126 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
127 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
128 strcasecmp(name, "all_subs") == 0) {
129 printf("%s output from subsystem \"%s\"\n",
130 enable ? "Enabling" : "Disabling",
131 libcfs_debug_subsystems[i]);
133 subsystem_mask |= (1 << i);
135 subsystem_mask &= ~(1 << i);
139 for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
140 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
141 strcasecmp(name, "all_types") == 0) {
142 printf("%s output of type \"%s\"\n",
143 enable ? "Enabling" : "Disabling",
144 libcfs_debug_masks[i]);
146 debug_mask |= (1 << i);
148 debug_mask &= ~(1 << i);
156 int dbg_initialize(int argc, char **argv)
161 int jt_dbg_filter(int argc, char **argv)
166 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
171 for (i = 1; i < argc; i++)
172 if (!do_debug_mask(argv[i], 0))
173 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
178 int jt_dbg_show(int argc, char **argv)
183 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
188 for (i = 1; i < argc; i++)
189 if (!do_debug_mask(argv[i], 1))
190 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
196 static int applymask(char *param, int value)
200 int len = snprintf(buf, 64, "%d", value);
202 int fd = dbg_open_ctlhandle(param);
206 rc = dbg_write_cmd(fd, buf, len+1);
208 fprintf(stderr, "Write to %s failed: %s\n",
209 param, strerror(errno));
212 dbg_close_ctlhandle(fd);
217 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
219 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
220 applymask(DEBUG_CTL_NAME, debug_mask);
221 printf("Applied subsystem_debug=%d, debug=%d to lnet\n",
222 subs_mask, debug_mask);
225 int jt_dbg_list(int argc, char **argv)
230 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
234 if (strcasecmp(argv[1], "subs") == 0) {
235 printf("Subsystems: all_subs");
236 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
237 if (libcfs_debug_subsystems[i][0])
238 printf(", %s", libcfs_debug_subsystems[i]);
240 } else if (strcasecmp(argv[1], "types") == 0) {
241 printf("Types: all_types");
242 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
243 printf(", %s", libcfs_debug_masks[i]);
245 } else if (strcasecmp(argv[1], "applymasks") == 0) {
246 applymask_all(subsystem_mask, debug_mask);
251 /* all strings nul-terminated; only the struct and hdr need to be freed */
253 struct ptldebug_header *hdr;
259 static int cmp_rec(const void *p1, const void *p2)
261 struct dbg_line *d1 = *(struct dbg_line **)p1;
262 struct dbg_line *d2 = *(struct dbg_line **)p2;
264 if (d1->hdr->ph_sec < d2->hdr->ph_sec)
266 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
267 d1->hdr->ph_usec < d2->hdr->ph_usec)
269 if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
270 d1->hdr->ph_usec == d2->hdr->ph_usec)
275 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
277 struct dbg_line **linev = *linevp;
280 qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
281 for (i = 0; i < used; i++) {
282 struct dbg_line *line = linev[i];
283 struct ptldebug_header *hdr = line->hdr;
287 ssize_t bytes_written;
289 bytes = snprintf(out, sizeof(out),
290 "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:"
292 hdr->ph_subsys, hdr->ph_mask,
293 hdr->ph_cpu_id, hdr->ph_type,
294 hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
295 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
296 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
297 line->file, hdr->ph_line_num, line->fn,
300 bytes_written = write(fdout, buf, bytes);
301 if (bytes_written <= 0)
303 bytes -= bytes_written;
304 buf += bytes_written;
313 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
316 struct dbg_line **linev = *linevp;
319 int nlen = *lenp + 4096;
320 int nsize = nlen * sizeof(struct dbg_line *);
322 linev = realloc(*linevp, nsize);
334 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
336 fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
337 fprintf(stderr, " len = %u\n", hdr->ph_len);
338 fprintf(stderr, " flags = %x\n", hdr->ph_flags);
339 fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
340 fprintf(stderr, " mask = %x\n", hdr->ph_mask);
341 fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
342 fprintf(stderr, " type = %u\n", hdr->ph_type);
343 fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
344 fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
345 fprintf(stderr, " stack = %u\n", hdr->ph_stack);
346 fprintf(stderr, " pid = %u\n", hdr->ph_pid);
347 fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
348 fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
351 #define HDR_SIZE sizeof(*hdr)
353 static int parse_buffer(int fdin, int fdout)
355 struct dbg_line *line;
356 struct ptldebug_header *hdr;
359 unsigned long dropped = 0;
360 unsigned long kept = 0;
361 unsigned long bad = 0;
362 struct dbg_line **linev = NULL;
375 rc = read(fdin, ptr, count);
384 if (hdr->ph_len > 4094 || /* is this header bogus? */
385 hdr->ph_stack > 65536 ||
386 hdr->ph_sec < (1 << 30) ||
387 hdr->ph_usec > 1000000000 ||
388 hdr->ph_line_num > 65536) {
390 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
394 /* try to restart on next line */
395 while (count < HDR_SIZE && buf[count] != '\n')
397 if (buf[count] == '\n')
398 count++; /* move past '\n' */
399 if (HDR_SIZE - count > 0) {
400 int left = HDR_SIZE - count;
402 memmove(buf, buf + count, left);
411 if (hdr->ph_len == 0)
414 count = hdr->ph_len - HDR_SIZE;
416 rc = read(fdin, ptr, count);
427 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
428 (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
434 line = malloc(sizeof(*line));
437 fprintf(stderr, "error: line malloc(%u): "
438 "printing accumulated records\n",
439 (unsigned int)sizeof(*line));
440 print_rec(&linev, kept, fdout);
444 fprintf(stderr, "error: line malloc(%u): exiting\n",
445 (unsigned int)sizeof(*line));
449 line->hdr = malloc(hdr->ph_len + 1);
450 if (line->hdr == NULL) {
453 fprintf(stderr, "error: hdr malloc(%u): "
454 "printing accumulated records\n",
456 print_rec(&linev, kept, fdout);
460 fprintf(stderr, "error: hdr malloc(%u): exiting\n",
465 ptr = (void *)line->hdr;
466 memcpy(line->hdr, buf, hdr->ph_len);
467 ptr[hdr->ph_len] = '\0';
471 ptr += strlen(line->file) + 1;
473 ptr += strlen(line->fn) + 1;
477 if (add_rec(line, &linev, &linev_len, kept) < 0) {
479 fprintf(stderr, "error: add_rec[%u] failed; "
480 "print accumulated records\n",
482 print_rec(&linev, kept, fdout);
486 fprintf(stderr, "error: add_rec[0] failed; exiting\n");
494 print_rec(&linev, kept, fdout);
496 printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
497 dropped + kept + bad, kept, dropped, bad);
502 int jt_dbg_debug_kernel(int argc, char **argv)
505 char filename[PATH_MAX];
513 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
519 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
524 /* If we are dumping raw (which means no conversion step to ASCII)
525 * then dump directly to any supplied filename, otherwise this is
526 * just a temp file and we dump to the real file at convert time. */
527 if (argc > 1 && raw) {
528 if (strlen(argv[1]) >= sizeof(filename)) {
529 fprintf(stderr, "File name too long: %s\n", argv[1]);
532 strncpy(filename, argv[1], sizeof(filename));
534 if (snprintf(filename, sizeof(filename), "%s%lu.%u",
535 LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
537 >= sizeof(filename)) {
538 fprintf(stderr, "File name too long\n");
543 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
546 fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
548 fprintf(stderr, "open(dump_kernel) failed: %s\n",
553 rc = dbg_write_cmd(fdin, filename, strlen(filename));
555 dbg_close_ctlhandle(fdin);
557 fprintf(stderr, "write(%s) failed: %s\n", filename,
558 strerror(save_errno));
565 fdin = open(filename, O_RDONLY);
567 if (errno == ENOENT) /* no dump file created */
569 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
574 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
577 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
583 fdout = fileno(stdout);
586 rc = parse_buffer(fdin, fdout);
591 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
592 "behind.\n", filename);
594 rc = unlink(filename);
596 fprintf(stderr, "dumped successfully, but couldn't "
597 "unlink tmp file %s: %s\n", filename,
604 int jt_dbg_debug_file(int argc, char **argv)
610 if (argc > 3 || argc < 2) {
611 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
615 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
617 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
622 fdout = open(argv[2],
623 O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
626 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
632 fdout = fileno(stdout);
635 rc = parse_buffer(fdin, fdout);
638 if (fdout != fileno(stdout))
644 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
646 int jt_dbg_debug_daemon(int argc, char **argv)
650 char *resolved_path = NULL;
653 fprintf(stderr, debug_daemon_usage, argv[0]);
657 fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
662 if (strcasecmp(argv[1], "start") == 0) {
663 if (argc < 3 || argc > 4 ||
664 (argc == 4 && strlen(argv[3]) > 5)) {
665 fprintf(stderr, debug_daemon_usage, argv[0]);
670 const long min_size = 10;
671 const long max_size = 20480;
675 size = strtoul(argv[3], &end, 0);
676 if (size < min_size ||
679 fprintf(stderr, "size %s invalid, must be in "
680 "the range %ld-%ld MB\n", argv[3],
684 snprintf(buf, sizeof(buf), "size=%ld", size);
685 rc = dbg_write_cmd(fd, buf, strlen(buf));
688 fprintf(stderr, "set %s failed: %s\n",
689 buf, strerror(errno));
694 rc = cfs_abs_path(argv[2], &resolved_path);
697 "%s debug_daemon: cannot resolve path '%s': %s\n",
698 program_invocation_short_name, argv[2],
702 rc = dbg_write_cmd(fd, resolved_path, strlen(resolved_path));
704 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
705 argv[2], strerror(errno));
711 if (strcasecmp(argv[1], "stop") == 0) {
712 rc = dbg_write_cmd(fd, "stop", 4);
714 fprintf(stderr, "stopping debug_daemon failed: %s\n",
723 fprintf(stderr, debug_daemon_usage, argv[0]);
726 dbg_close_ctlhandle(fd);
727 if (resolved_path != NULL)
732 int jt_dbg_clear_debug_buf(int argc, char **argv)
735 struct libcfs_ioctl_data data;
738 fprintf(stderr, "usage: %s\n", argv[0]);
742 memset(&data, 0, sizeof(data));
743 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
744 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
748 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
750 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
757 int jt_dbg_mark_debug_buf(int argc, char **argv)
759 static char scratch[MAX_MARK_SIZE] = "";
760 struct libcfs_ioctl_data data;
764 memset(&data, 0, sizeof(data));
767 int count, max_size = sizeof(scratch) - 1;
769 strncpy(scratch, argv[1], max_size);
770 max_size -= strlen(argv[1]);
771 for (count = 2; (count < argc) && (max_size > 1); count++) {
772 strncat(scratch, " ", max_size);
774 strncat(scratch, argv[count], max_size);
775 max_size -= strlen(argv[count]);
777 scratch[sizeof(scratch) - 1] = '\0';
780 time_t now = time(NULL);
784 data.ioc_inllen1 = strlen(text) + 1;
785 data.ioc_inlbuf1 = text;
787 if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
788 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
792 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
794 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
801 static struct mod_paths {
804 { .name = "libcfs", .path = "libcfs/libcfs" },
805 { .name = "lnet", .path = "lnet/lnet" },
806 { .name = "ko2iblnd", .path = "lnet/klnds/o2iblnd" },
807 { .name = "kgnilnd", .path = "lnet/klnds/gnilnd"},
808 { .name = "ksocklnd", .path = "lnet/klnds/socklnd" },
809 { .name = "obdclass", .path = "lustre/obdclass" },
810 { .name = "llog_test", .path = "lustre/obdclass" },
811 { .name = "ptlrpc_gss", .path = "lustre/ptlrpc/gss" },
812 { .name = "ptlrpc", .path = "lustre/ptlrpc" },
813 { .name = "gks", .path = "lustre/sec/gks" },
814 { .name = "gkc", .path = "lustre/sec/gks" },
815 { .name = "ost", .path = "lustre/ost" },
816 { .name = "osc", .path = "lustre/osc" },
817 { .name = "mds", .path = "lustre/mds" },
818 { .name = "mdc", .path = "lustre/mdc" },
819 { .name = "lustre", .path = "lustre/llite" },
820 { .name = "ldiskfs", .path = "ldiskfs" },
821 { .name = "obdecho", .path = "lustre/obdecho" },
822 { .name = "ldlm", .path = "lustre/ldlm" },
823 { .name = "obdfilter", .path = "lustre/obdfilter" },
824 { .name = "lov", .path = "lustre/lov" },
825 { .name = "lmv", .path = "lustre/lmv" },
826 { .name = "lquota", .path = "lustre/quota" },
827 { .name = "mgs", .path = "lustre/mgs" },
828 { .name = "mgc", .path = "lustre/mgc" },
829 { .name = "mdt", .path = "lustre/mdt" },
830 { .name = "mdd", .path = "lustre/mdd" },
831 { .name = "osd", .path = "lustre/osd" },
832 { .name = "cmm", .path = "lustre/cmm" },
833 { .name = "fid", .path = "lustre/fid"},
834 { .name = "fld", .path = "lustre/fld"},
835 { .name = "lod", .path = "lustre/lod"},
836 { .name = "osp", .path = "lustre/osp"},
837 { .name = "lfsck", .path = "lustre/lfsck" },
841 int jt_dbg_modules(int argc, char **argv)
843 struct mod_paths *mp;
845 const char *proc = "/proc/modules";
848 unsigned long modaddr;
854 printf("%s [path] [kernel]\n", argv[0]);
858 file = fopen(proc, "r");
860 printf("failed open %s: %s\n", proc, strerror(errno));
864 while (fgets(buf, sizeof(buf), file) != NULL) {
865 if (sscanf(buf, "%s %*s %*s %*s %*s %lx",
866 modname, &modaddr) == 2) {
867 for (mp = mod_paths; mp->name != NULL; mp++) {
868 if (!strcmp(mp->name, modname))
872 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
873 path, path[0] ? "/" : "",
874 mp->path, mp->name, modaddr);