1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Some day I'll split all of this functionality into a cfs_debug module
22 * of its own. That day is not today.
26 #define __USE_FILE_OFFSET64
29 #include <portals/list.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
51 #ifdef HAVE_LINUX_VERSION_H
52 #include <linux/version.h>
54 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
55 #define BUG() /* workaround for module.h includes */
56 #include <linux/module.h>
58 #endif /* !HAVE_LINUX_VERSION_H */
60 #include <sys/utsname.h>
62 #include <portals/api-support.h>
63 #include <portals/ptlctl.h>
68 static char rawbuf[8192];
69 static char *buf = rawbuf;
70 static int max = 8192;
71 /*static int g_pfd = -1;*/
72 static int subsystem_mask = ~0;
73 static int debug_mask = ~0;
75 #define MAX_MARK_SIZE 100
77 static const char *portal_debug_subsystems[] =
78 {"undefined", "mdc", "mds", "osc",
79 "ost", "class", "log", "llite",
80 "rpc", "mgmt", "portals", "socknal",
81 "qswnal", "pinger", "filter", "ptlbd",
82 "echo", "ldlm", "lov", "gmnal",
83 "router", "cobd", "ibnal", "sm",
84 "asobd", "confobd", "lmv", "cmobd",
86 static const char *portal_debug_masks[] =
87 {"trace", "inode", "super", "ext2",
88 "malloc", "cache", "info", "ioctl",
89 "blocks", "net", "warning", "buffs",
90 "other", "dentry", "portals", "page",
91 "dlmtrace", "error", "emerg", "ha",
92 "rpctrace", "vfstrace", "reada", "mmap",
95 struct debug_daemon_cmd {
100 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
101 {"start", DEBUG_DAEMON_START},
102 {"stop", DEBUG_DAEMON_STOP},
106 static int do_debug_mask(char *name, int enable)
110 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
111 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
112 strcasecmp(name, "all_subs") == 0) {
113 printf("%s output from subsystem \"%s\"\n",
114 enable ? "Enabling" : "Disabling",
115 portal_debug_subsystems[i]);
117 subsystem_mask |= (1 << i);
119 subsystem_mask &= ~(1 << i);
123 for (i = 0; portal_debug_masks[i] != NULL; i++) {
124 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
125 strcasecmp(name, "all_types") == 0) {
126 printf("%s output of type \"%s\"\n",
127 enable ? "Enabling" : "Disabling",
128 portal_debug_masks[i]);
130 debug_mask |= (1 << i);
132 debug_mask &= ~(1 << i);
140 int dbg_initialize(int argc, char **argv)
145 int jt_dbg_filter(int argc, char **argv)
150 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
155 for (i = 1; i < argc; i++)
156 if (!do_debug_mask(argv[i], 0))
157 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
162 int jt_dbg_show(int argc, char **argv)
167 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
172 for (i = 1; i < argc; i++)
173 if (!do_debug_mask(argv[i], 1))
174 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
180 static int applymask(char* procpath, int value)
184 int len = snprintf(buf, 64, "%d", value);
186 int fd = open(procpath, O_WRONLY);
188 fprintf(stderr, "Unable to open %s: %s\n",
189 procpath, strerror(errno));
192 rc = write(fd, buf, len+1);
194 fprintf(stderr, "Write to %s failed: %s\n",
195 procpath, strerror(errno));
202 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
204 if (!dump_filename) {
205 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
206 applymask("/proc/sys/portals/debug", debug_mask);
208 struct portals_debug_ioctl_data data;
210 data.hdr.ioc_len = sizeof(data);
211 data.hdr.ioc_version = 0;
212 data.subs = subs_mask;
213 data.debug = debug_mask;
215 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
217 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
218 subs_mask, debug_mask);
221 int jt_dbg_list(int argc, char **argv)
226 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
230 if (strcasecmp(argv[1], "subs") == 0) {
231 printf("Subsystems: all_subs");
232 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
233 printf(", %s", portal_debug_subsystems[i]);
235 } else if (strcasecmp(argv[1], "types") == 0) {
236 printf("Types: all_types");
237 for (i = 0; portal_debug_masks[i] != NULL; i++)
238 printf(", %s", portal_debug_masks[i]);
240 } else if (strcasecmp(argv[1], "applymasks") == 0) {
241 applymask_all(subsystem_mask, debug_mask);
246 /* all strings nul-terminated; only the struct and hdr need to be freed */
248 struct ptldebug_header *hdr;
252 struct list_head chain;
256 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
258 struct list_head *pos;
259 struct dbg_line *curr;
261 list_for_each(pos, head) {
262 curr = list_entry(pos, struct dbg_line, chain);
264 if (curr->hdr->ph_sec < new->hdr->ph_sec)
266 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
267 curr->hdr->ph_usec < new->hdr->ph_usec)
270 list_add(&new->chain, pos->prev);
273 list_add_tail(&new->chain, head);
276 static void print_saved_records(struct list_head *list, FILE *out)
278 struct list_head *pos, *tmp;
280 list_for_each_safe(pos, tmp, list) {
281 struct dbg_line *line;
282 struct ptldebug_header *hdr;
284 line = list_entry(pos, struct dbg_line, chain);
285 list_del(&line->chain);
288 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
289 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
290 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
291 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
292 line->file, hdr->ph_line_num, line->fn, line->text);
298 static int parse_buffer(FILE *in, FILE *out)
300 struct dbg_line *line;
301 struct ptldebug_header *hdr;
304 unsigned long dropped = 0, kept = 0;
305 struct list_head chunk_list;
307 INIT_LIST_HEAD(&chunk_list);
310 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
315 if (hdr->ph_len == 0)
317 if (hdr->ph_len > 4094) {
318 fprintf(stderr, "unexpected large record: %d bytes. "
324 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
325 print_saved_records(&chunk_list, out);
326 assert(list_empty(&chunk_list));
329 rc = fread(buf + sizeof(hdr->ph_len), 1,
330 hdr->ph_len - sizeof(hdr->ph_len), in);
335 (!(subsystem_mask & hdr->ph_subsys) ||
336 (!(debug_mask & hdr->ph_mask)))) {
341 line = malloc(sizeof(*line));
343 fprintf(stderr, "malloc failed; printing accumulated "
344 "records and exiting.\n");
348 line->hdr = malloc(hdr->ph_len + 1);
349 if (line->hdr == NULL) {
350 fprintf(stderr, "malloc failed; printing accumulated "
351 "records and exiting.\n");
355 p = (void *)line->hdr;
356 memcpy(line->hdr, buf, hdr->ph_len);
357 p[hdr->ph_len] = '\0';
361 p += strlen(line->file) + 1;
363 p += strlen(line->fn) + 1;
366 list_add_ordered(line, &chunk_list);
370 print_saved_records(&chunk_list, out);
372 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
373 dropped + kept, kept, dropped);
377 int jt_dbg_debug_kernel(int argc, char **argv)
381 FILE *in, *out = stdout;
384 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
390 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
394 sprintf(filename, "%s.%lu.%u", argc > 1 ? argv[1] :
395 "/tmp/lustre-log", time(NULL), getpid());
400 fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
402 if (errno == ENOENT) /* no dump file created */
405 fprintf(stderr, "open(dump_kernel) failed: %s\n",
410 rc = write(fd, filename, strlen(filename));
411 if (rc != strlen(filename)) {
412 fprintf(stderr, "write(%s) failed: %s\n", filename,
422 in = fopen(filename, "r");
424 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
429 out = fopen(argv[1], "w");
431 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
438 rc = parse_buffer(in, out);
443 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
444 "behind.\n", filename);
446 rc = unlink(filename);
448 fprintf(stderr, "dumped successfully, but couldn't "
449 "unlink tmp file %s: %s\n", filename,
455 int jt_dbg_debug_file(int argc, char **argv)
458 FILE *in, *out = stdout;
459 if (argc > 3 || argc < 2) {
460 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
464 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
466 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
470 in = fdopen(fdin, "r");
472 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
478 fdout = open(argv[2], O_CREAT | O_WRONLY | O_LARGEFILE);
480 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
485 out = fdopen(fdout, "w");
487 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
495 return parse_buffer(in, out);
499 dbg_write_cmd(int fd, char *str)
501 int len = strlen(str);
502 int rc = write(fd, str, len);
504 return (rc == len ? 0 : 1);
507 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
508 #define DAEMON_FILE "/proc/sys/portals/daemon_file"
509 int jt_dbg_debug_daemon(int argc, char **argv)
515 fprintf(stderr, debug_daemon_usage, argv[0]);
519 fd = open(DAEMON_FILE, O_WRONLY);
521 fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
527 if (strcasecmp(argv[1], "start") == 0) {
528 if (argc < 3 || argc > 4 ||
529 (argc == 4 && strlen(argv[3]) > 5)) {
530 fprintf(stderr, debug_daemon_usage, argv[0]);
536 const long min_size = 10;
537 const long max_size = 20480;
541 size = strtoul(argv[3], &end, 0);
542 if (size < min_size ||
545 fprintf(stderr, "size %s invalid, must be in "
546 "the range %ld-%ld MB\n", argv[3],
551 snprintf(buf, sizeof(buf), "size=%ld", size);
552 rc = dbg_write_cmd(fd, buf);
554 fprintf(stderr, "set %s failed: %s\n",
555 buf, strerror(errno));
560 rc = dbg_write_cmd(fd, "start");
562 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
563 argv[2], strerror(errno));
571 if (strcasecmp(argv[1], "stop") == 0) {
572 rc = dbg_write_cmd(fd, "stop");
574 fprintf(stderr, "stopping debug_daemon failed: %s\n",
583 fprintf(stderr, debug_daemon_usage, argv[0]);
590 int jt_dbg_clear_debug_buf(int argc, char **argv)
593 struct portal_ioctl_data data;
596 fprintf(stderr, "usage: %s\n", argv[0]);
600 memset(&data, 0, sizeof(data));
601 if (portal_ioctl_pack(&data, &buf, max) != 0) {
602 fprintf(stderr, "portal_ioctl_pack failed.\n");
606 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
608 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
615 int jt_dbg_mark_debug_buf(int argc, char **argv)
617 int rc, max_size = MAX_MARK_SIZE-1;
618 struct portal_ioctl_data data;
620 time_t now = time(NULL);
624 text = malloc(MAX_MARK_SIZE);
625 strncpy(text, argv[1], max_size);
626 max_size-=strlen(argv[1]);
627 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
628 strncat(text, " ", 1);
630 strncat(text, argv[counter], max_size);
631 max_size-=strlen(argv[counter]);
635 text[strlen(text) - 1] = '\0'; /* stupid \n */
638 text[MAX_MARK_SIZE - 1] = '\0';
641 memset(&data, 0, sizeof(data));
642 data.ioc_inllen1 = strlen(text) + 1;
643 data.ioc_inlbuf1 = text;
644 if (portal_ioctl_pack(&data, &buf, max) != 0) {
645 fprintf(stderr, "portal_ioctl_pack failed.\n");
649 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
651 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
658 static struct mod_paths {
661 {"libcfs", "lustre/portals/libcfs"},
662 {"portals", "lustre/portals/portals"},
663 {"ksocknal", "lustre/portals/knals/socknal"},
664 {"kptlrouter", "lustre/portals/router"},
665 {"lvfs", "lustre/lvfs"},
666 {"obdclass", "lustre/obdclass"},
667 {"llog_test", "lustre/obdclass"},
668 {"ptlrpc", "lustre/ptlrpc"},
669 {"obdext2", "lustre/obdext2"},
670 {"ost", "lustre/ost"},
671 {"osc", "lustre/osc"},
672 {"mds", "lustre/mds"},
673 {"mdc", "lustre/mdc"},
674 {"llite", "lustre/llite"},
675 {"ldiskfs", "lustre/ldiskfs"},
676 {"smfs", "lustre/smfs"},
677 {"obdecho", "lustre/obdecho"},
678 {"ldlm", "lustre/ldlm"},
679 {"obdfilter", "lustre/obdfilter"},
680 {"extN", "lustre/extN"},
681 {"lov", "lustre/lov"},
682 {"lmv", "lustre/lmv"},
683 {"fsfilt_ext3", "lustre/lvfs"},
684 {"fsfilt_extN", "lustre/lvfs"},
685 {"fsfilt_reiserfs", "lustre/lvfs"},
686 {"fsfilt_smfs", "lustre/lvfs"},
687 {"fsfilt_ldiskfs", "lustre/lvfs"},
688 {"mds_ext2", "lustre/mds"},
689 {"mds_ext3", "lustre/mds"},
690 {"mds_extN", "lustre/mds"},
691 {"ptlbd", "lustre/ptlbd"},
692 {"mgmt_svc", "lustre/mgmt"},
693 {"mgmt_cli", "lustre/mgmt"},
694 {"cobd", "lustre/cobd"},
695 {"cmobd", "lustre/cmobd"},
696 {"conf_obd", "lustre/obdclass"},
700 static int jt_dbg_modules_2_4(int argc, char **argv)
702 #ifdef HAVE_LINUX_VERSION_H
703 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
704 struct mod_paths *mp;
706 char *kernel = "linux";
713 printf("%s [path] [kernel]\n", argv[0]);
717 for (mp = mod_paths; mp->name != NULL; mp++) {
718 struct module_info info;
721 int query_module(const char *name, int which, void *buf,
722 size_t bufsize, size_t *ret);
724 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
728 printf("query_module(%s) failed: %s\n",
729 mp->name, strerror(errno));
731 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
733 info.addr + sizeof(struct module));
738 #endif /* Headers are 2.6-only */
739 #endif /* !HAVE_LINUX_VERSION_H */
743 static int jt_dbg_modules_2_5(int argc, char **argv)
745 struct mod_paths *mp;
747 char *kernel = "linux";
748 const char *proc = "/proc/modules";
749 char modname[128], others[128];
759 printf("%s [path] [kernel]\n", argv[0]);
763 file = fopen(proc, "r");
765 printf("failed open %s: %s\n", proc, strerror(errno));
769 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
770 modname, others, others, others, others, &modaddr)) == 6) {
771 for (mp = mod_paths; mp->name != NULL; mp++) {
772 if (!strcmp(mp->name, modname))
776 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
777 mp->path, mp->name, modaddr);
784 int jt_dbg_modules(int argc, char **argv)
787 struct utsname sysinfo;
789 rc = uname(&sysinfo);
791 printf("uname() failed: %s\n", strerror(errno));
795 if (sysinfo.release[2] > '4') {
796 return jt_dbg_modules_2_5(argc, argv);
798 return jt_dbg_modules_2_4(argc, argv);
804 int jt_dbg_panic(int argc, char **argv)
807 struct portal_ioctl_data data;
810 fprintf(stderr, "usage: %s\n", argv[0]);
814 memset(&data, 0, sizeof(data));
815 if (portal_ioctl_pack(&data, &buf, max) != 0) {
816 fprintf(stderr, "portal_ioctl_pack failed.\n");
820 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
822 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",