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
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
45 #include <sys/utsname.h>
47 #include <portals/api-support.h>
48 #include <portals/ptlctl.h>
49 #include <libcfs/portals_utils.h>
54 static char rawbuf[8192];
55 static char *buf = rawbuf;
56 static int max = 8192;
57 /*static int g_pfd = -1;*/
58 static int subsystem_mask = ~0;
59 static int debug_mask = ~0;
61 #define MAX_MARK_SIZE 100
63 static const char *portal_debug_subsystems[] =
64 {"undefined", "mdc", "mds", "osc",
65 "ost", "class", "log", "llite",
66 "rpc", "mgmt", "portals", "nal",
67 "pinger", "filter", "ptlbd", "echo",
68 "ldlm", "lov", "router", "cobd",
69 "sm", "asobd", "confobd", "lmv",
71 static const char *portal_debug_masks[] =
72 {"trace", "inode", "super", "ext2",
73 "malloc", "cache", "info", "ioctl",
74 "blocks", "net", "warning", "buffs",
75 "other", "dentry", "portals", "page",
76 "dlmtrace", "error", "emerg", "ha",
77 "rpctrace", "vfstrace", "reada", "mmap",
78 "config", "console", NULL};
80 struct debug_daemon_cmd {
85 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
86 {"start", DEBUG_DAEMON_START},
87 {"stop", DEBUG_DAEMON_STOP},
91 static int do_debug_mask(char *name, int enable)
95 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
96 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
97 strcasecmp(name, "all_subs") == 0) {
98 printf("%s output from subsystem \"%s\"\n",
99 enable ? "Enabling" : "Disabling",
100 portal_debug_subsystems[i]);
102 subsystem_mask |= (1 << i);
104 subsystem_mask &= ~(1 << i);
108 for (i = 0; portal_debug_masks[i] != NULL; i++) {
109 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
110 strcasecmp(name, "all_types") == 0) {
111 printf("%s output of type \"%s\"\n",
112 enable ? "Enabling" : "Disabling",
113 portal_debug_masks[i]);
115 debug_mask |= (1 << i);
117 debug_mask &= ~(1 << i);
125 int dbg_initialize(int argc, char **argv)
130 int jt_dbg_filter(int argc, char **argv)
135 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
140 for (i = 1; i < argc; i++)
141 if (!do_debug_mask(argv[i], 0))
142 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
147 int jt_dbg_show(int argc, char **argv)
152 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
157 for (i = 1; i < argc; i++)
158 if (!do_debug_mask(argv[i], 1))
159 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
165 static int applymask(char* procpath, int value)
169 int len = snprintf(buf, 64, "%d", value);
171 int fd = open(procpath, O_WRONLY);
173 fprintf(stderr, "Unable to open %s: %s\n",
174 procpath, strerror(errno));
177 rc = write(fd, buf, len+1);
179 fprintf(stderr, "Write to %s failed: %s\n",
180 procpath, strerror(errno));
187 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
189 if (!dump_filename) {
190 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
191 applymask("/proc/sys/portals/debug", debug_mask);
193 struct portals_debug_ioctl_data data;
195 data.hdr.ioc_len = sizeof(data);
196 data.hdr.ioc_version = 0;
197 data.subs = subs_mask;
198 data.debug = debug_mask;
200 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
202 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
203 subs_mask, debug_mask);
206 int jt_dbg_list(int argc, char **argv)
211 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
215 if (strcasecmp(argv[1], "subs") == 0) {
216 printf("Subsystems: all_subs");
217 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
218 printf(", %s", portal_debug_subsystems[i]);
220 } else if (strcasecmp(argv[1], "types") == 0) {
221 printf("Types: all_types");
222 for (i = 0; portal_debug_masks[i] != NULL; i++)
223 printf(", %s", portal_debug_masks[i]);
225 } else if (strcasecmp(argv[1], "applymasks") == 0) {
226 applymask_all(subsystem_mask, debug_mask);
231 /* all strings nul-terminated; only the struct and hdr need to be freed */
233 struct ptldebug_header *hdr;
237 struct list_head chain;
241 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
243 struct list_head *pos;
244 struct dbg_line *curr;
246 list_for_each(pos, head) {
247 curr = list_entry(pos, struct dbg_line, chain);
249 if (curr->hdr->ph_sec < new->hdr->ph_sec)
251 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
252 curr->hdr->ph_usec < new->hdr->ph_usec)
255 list_add(&new->chain, pos->prev);
258 list_add_tail(&new->chain, head);
261 static void print_saved_records(struct list_head *list, FILE *out)
263 struct list_head *pos, *tmp;
265 list_for_each_safe(pos, tmp, list) {
266 struct dbg_line *line;
267 struct ptldebug_header *hdr;
269 line = list_entry(pos, struct dbg_line, chain);
270 list_del(&line->chain);
273 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
274 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
275 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
276 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
277 line->file, hdr->ph_line_num, line->fn, line->text);
283 static int parse_buffer(FILE *in, FILE *out)
285 struct dbg_line *line;
286 struct ptldebug_header *hdr;
289 unsigned long dropped = 0, kept = 0;
290 struct list_head chunk_list;
292 CFS_INIT_LIST_HEAD(&chunk_list);
295 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
300 if (hdr->ph_len == 0)
302 if (hdr->ph_len > 4094) {
303 fprintf(stderr, "unexpected large record: %d bytes. "
309 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
310 print_saved_records(&chunk_list, out);
311 assert(list_empty(&chunk_list));
314 rc = fread(buf + sizeof(hdr->ph_len), 1,
315 hdr->ph_len - sizeof(hdr->ph_len), in);
320 (!(subsystem_mask & hdr->ph_subsys) ||
321 (!(debug_mask & hdr->ph_mask)))) {
326 line = malloc(sizeof(*line));
328 fprintf(stderr, "malloc failed; printing accumulated "
329 "records and exiting.\n");
333 line->hdr = malloc(hdr->ph_len + 1);
334 if (line->hdr == NULL) {
335 fprintf(stderr, "malloc failed; printing accumulated "
336 "records and exiting.\n");
340 p = (void *)line->hdr;
341 memcpy(line->hdr, buf, hdr->ph_len);
342 p[hdr->ph_len] = '\0';
346 p += strlen(line->file) + 1;
348 p += strlen(line->fn) + 1;
351 list_add_ordered(line, &chunk_list);
355 print_saved_records(&chunk_list, out);
357 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
358 dropped + kept, kept, dropped);
362 int jt_dbg_debug_kernel(int argc, char **argv)
367 FILE *in, *out = stdout;
370 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
376 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
381 /* If we are dumping raw (which means no conversion step to ASCII)
382 * then dump directly to any supplied filename, otherwise this is
383 * just a temp file and we dump to the real file at convert time. */
385 strcpy(filename, argv[1]);
387 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
389 if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
392 fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
394 fprintf(stderr, "open(dump_kernel) failed: %s\n",
399 rc = write(fd, filename, strlen(filename));
400 if (rc != strlen(filename)) {
401 fprintf(stderr, "write(%s) failed: %s\n", filename,
411 in = fopen(filename, "r");
413 if (errno == ENOENT) /* no dump file created */
416 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
421 out = fopen(argv[1], "w");
423 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
430 rc = parse_buffer(in, out);
435 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
436 "behind.\n", filename);
438 rc = unlink(filename);
440 fprintf(stderr, "dumped successfully, but couldn't "
441 "unlink tmp file %s: %s\n", filename,
447 int jt_dbg_debug_file(int argc, char **argv)
450 FILE *in, *out = stdout;
451 if (argc > 3 || argc < 2) {
452 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
456 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
458 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
462 in = fdopen(fdin, "r");
464 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
470 fdout = open(argv[2], O_CREAT | O_WRONLY | O_LARGEFILE, 0600);
472 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
477 out = fdopen(fdout, "w");
479 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
487 return parse_buffer(in, out);
491 dbg_write_cmd(int fd, char *str)
493 int len = strlen(str);
494 int rc = write(fd, str, len);
496 return (rc == len ? 0 : 1);
499 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
500 #define DAEMON_FILE "/proc/sys/portals/daemon_file"
501 int jt_dbg_debug_daemon(int argc, char **argv)
507 fprintf(stderr, debug_daemon_usage, argv[0]);
511 fd = open(DAEMON_FILE, O_WRONLY);
513 fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
519 if (strcasecmp(argv[1], "start") == 0) {
520 if (argc < 3 || argc > 4 ||
521 (argc == 4 && strlen(argv[3]) > 5)) {
522 fprintf(stderr, debug_daemon_usage, argv[0]);
527 const long min_size = 10;
528 const long max_size = 20480;
532 size = strtoul(argv[3], &end, 0);
533 if (size < min_size ||
536 fprintf(stderr, "size %s invalid, must be in "
537 "the range %ld-%ld MB\n", argv[3],
541 snprintf(buf, sizeof(buf), "size=%ld", size);
542 rc = dbg_write_cmd(fd, buf);
545 fprintf(stderr, "set %s failed: %s\n",
546 buf, strerror(errno));
551 rc = dbg_write_cmd(fd, argv[2]);
553 fprintf(stderr, "start debug_daemon on %s failed: %s\n",
554 argv[2], strerror(errno));
560 if (strcasecmp(argv[1], "stop") == 0) {
561 rc = dbg_write_cmd(fd, "stop");
563 fprintf(stderr, "stopping debug_daemon failed: %s\n",
572 fprintf(stderr, debug_daemon_usage, argv[0]);
579 int jt_dbg_clear_debug_buf(int argc, char **argv)
582 struct portal_ioctl_data data;
585 fprintf(stderr, "usage: %s\n", argv[0]);
589 memset(&data, 0, sizeof(data));
590 if (portal_ioctl_pack(&data, &buf, max) != 0) {
591 fprintf(stderr, "portal_ioctl_pack failed.\n");
595 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
597 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
604 int jt_dbg_mark_debug_buf(int argc, char **argv)
606 int rc, max_size = MAX_MARK_SIZE-1;
607 struct portal_ioctl_data data;
609 time_t now = time(NULL);
613 text = malloc(MAX_MARK_SIZE);
614 strncpy(text, argv[1], max_size);
615 max_size-=strlen(argv[1]);
616 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
617 strncat(text, " ", 1);
619 strncat(text, argv[counter], max_size);
620 max_size-=strlen(argv[counter]);
624 text[strlen(text) - 1] = '\0'; /* stupid \n */
627 text[MAX_MARK_SIZE - 1] = '\0';
630 memset(&data, 0, sizeof(data));
631 data.ioc_inllen1 = strlen(text) + 1;
632 data.ioc_inlbuf1 = text;
633 if (portal_ioctl_pack(&data, &buf, max) != 0) {
634 fprintf(stderr, "portal_ioctl_pack failed.\n");
638 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
640 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
647 static struct mod_paths {
650 {"libcfs", "portals/libcfs"},
651 {"portals", "portals/portals"},
652 {"ksocknal", "portals/knals/socknal"},
653 {"kptlrouter", "portals/router"},
654 {"lvfs", "lustre/lvfs"},
655 {"obdclass", "lustre/obdclass"},
656 {"llog_test", "lustre/obdclass"},
657 {"ptlrpc", "lustre/ptlrpc"},
658 {"obdext2", "lustre/obdext2"},
659 {"ost", "lustre/ost"},
660 {"osc", "lustre/osc"},
661 {"mds", "lustre/mds"},
662 {"mdc", "lustre/mdc"},
663 {"llite", "lustre/llite"},
664 {"ldiskfs", "lustre/ldiskfs"},
665 {"smfs", "lustre/smfs"},
666 {"obdecho", "lustre/obdecho"},
667 {"ldlm", "lustre/ldlm"},
668 {"obdfilter", "lustre/obdfilter"},
669 {"extN", "lustre/extN"},
670 {"lov", "lustre/lov"},
671 {"lmv", "lustre/lmv"},
672 {"fsfilt_ext3", "lustre/lvfs"},
673 {"fsfilt_extN", "lustre/lvfs"},
674 {"fsfilt_reiserfs", "lustre/lvfs"},
675 {"fsfilt_smfs", "lustre/lvfs"},
676 {"fsfilt_ldiskfs", "lustre/lvfs"},
677 {"mds_ext2", "lustre/mds"},
678 {"mds_ext3", "lustre/mds"},
679 {"mds_extN", "lustre/mds"},
680 {"ptlbd", "lustre/ptlbd"},
681 {"mgmt_svc", "lustre/mgmt"},
682 {"mgmt_cli", "lustre/mgmt"},
683 {"cobd", "lustre/cobd"},
684 {"cmobd", "lustre/cmobd"},
685 {"confobd", "lustre/obdclass"},
689 static int jt_dbg_modules_2_4(int argc, char **argv)
691 #ifdef HAVE_LINUX_VERSION_H
692 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
693 struct mod_paths *mp;
695 char *kernel = "linux";
702 printf("%s [path] [kernel]\n", argv[0]);
706 for (mp = mod_paths; mp->name != NULL; mp++) {
707 struct module_info info;
710 int query_module(const char *name, int which, void *buf,
711 size_t bufsize, size_t *ret);
713 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
717 printf("query_module(%s) failed: %s\n",
718 mp->name, strerror(errno));
720 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
722 info.addr + sizeof(struct module));
727 #endif /* Headers are 2.6-only */
728 #endif /* !HAVE_LINUX_VERSION_H */
732 static int jt_dbg_modules_2_5(int argc, char **argv)
734 struct mod_paths *mp;
736 char *kernel = "linux";
737 const char *proc = "/proc/modules";
738 char modname[128], others[128];
748 printf("%s [path] [kernel]\n", argv[0]);
752 file = fopen(proc, "r");
754 printf("failed open %s: %s\n", proc, strerror(errno));
758 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
759 modname, others, others, others, others, &modaddr)) == 6) {
760 for (mp = mod_paths; mp->name != NULL; mp++) {
761 if (!strcmp(mp->name, modname))
765 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
766 mp->path, mp->name, modaddr);
773 int jt_dbg_modules(int argc, char **argv)
776 struct utsname sysinfo;
778 rc = uname(&sysinfo);
780 printf("uname() failed: %s\n", strerror(errno));
784 if (sysinfo.release[2] > '4') {
785 return jt_dbg_modules_2_5(argc, argv);
787 return jt_dbg_modules_2_4(argc, argv);
793 int jt_dbg_panic(int argc, char **argv)
796 struct portal_ioctl_data data;
799 fprintf(stderr, "usage: %s\n", argv[0]);
803 memset(&data, 0, sizeof(data));
804 if (portal_ioctl_pack(&data, &buf, max) != 0) {
805 fprintf(stderr, "portal_ioctl_pack failed.\n");
809 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
811 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",