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", "ost", "class", "log", "llite",
79 "rpc", "mgmt", "portals", "libcfs", "socknal", "qswnal", "pinger",
80 "filter", "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd",
81 "ibnal", "lmv", "cmobd", "smfs", NULL};
82 static const char *portal_debug_masks[] =
83 {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
84 "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
85 "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
86 "reada", "mmap", NULL};
88 struct debug_daemon_cmd {
93 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
94 {"start", DEBUG_DAEMON_START},
95 {"stop", DEBUG_DAEMON_STOP},
99 static int do_debug_mask(char *name, int enable)
103 for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
104 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
105 strcasecmp(name, "all_subs") == 0) {
106 printf("%s output from subsystem \"%s\"\n",
107 enable ? "Enabling" : "Disabling",
108 portal_debug_subsystems[i]);
110 subsystem_mask |= (1 << i);
112 subsystem_mask &= ~(1 << i);
116 for (i = 0; portal_debug_masks[i] != NULL; i++) {
117 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
118 strcasecmp(name, "all_types") == 0) {
119 printf("%s output of type \"%s\"\n",
120 enable ? "Enabling" : "Disabling",
121 portal_debug_masks[i]);
123 debug_mask |= (1 << i);
125 debug_mask &= ~(1 << i);
133 int dbg_initialize(int argc, char **argv)
138 int jt_dbg_filter(int argc, char **argv)
143 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
148 for (i = 1; i < argc; i++)
149 if (!do_debug_mask(argv[i], 0))
150 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
155 int jt_dbg_show(int argc, char **argv)
160 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
165 for (i = 1; i < argc; i++)
166 if (!do_debug_mask(argv[i], 1))
167 fprintf(stderr, "Unknown subsystem or debug type: %s\n",
173 static int applymask(char* procpath, int value)
177 int len = snprintf(buf, 64, "%d", value);
179 int fd = open(procpath, O_WRONLY);
181 fprintf(stderr, "Unable to open %s: %s\n",
182 procpath, strerror(errno));
185 rc = write(fd, buf, len+1);
187 fprintf(stderr, "Write to %s failed: %s\n",
188 procpath, strerror(errno));
195 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
197 if (!dump_filename) {
198 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
199 applymask("/proc/sys/portals/debug", debug_mask);
201 struct portals_debug_ioctl_data data;
203 data.hdr.ioc_len = sizeof(data);
204 data.hdr.ioc_version = 0;
205 data.subs = subs_mask;
206 data.debug = debug_mask;
208 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
210 printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
211 subs_mask, debug_mask);
214 int jt_dbg_list(int argc, char **argv)
219 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
223 if (strcasecmp(argv[1], "subs") == 0) {
224 printf("Subsystems: all_subs");
225 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
226 printf(", %s", portal_debug_subsystems[i]);
228 } else if (strcasecmp(argv[1], "types") == 0) {
229 printf("Types: all_types");
230 for (i = 0; portal_debug_masks[i] != NULL; i++)
231 printf(", %s", portal_debug_masks[i]);
233 } else if (strcasecmp(argv[1], "applymasks") == 0) {
234 applymask_all(subsystem_mask, debug_mask);
239 /* all strings nul-terminated; only the struct and hdr need to be freed */
241 struct ptldebug_header *hdr;
245 struct list_head chain;
249 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
251 struct list_head *pos;
252 struct dbg_line *curr;
254 list_for_each(pos, head) {
255 curr = list_entry(pos, struct dbg_line, chain);
257 if (curr->hdr->ph_sec < new->hdr->ph_sec)
259 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
260 curr->hdr->ph_usec < new->hdr->ph_usec)
263 list_add(&new->chain, pos->prev);
266 list_add_tail(&new->chain, head);
269 static void print_saved_records(struct list_head *list, FILE *out)
271 struct list_head *pos, *tmp;
273 list_for_each_safe(pos, tmp, list) {
274 struct dbg_line *line;
275 struct ptldebug_header *hdr;
277 line = list_entry(pos, struct dbg_line, chain);
278 list_del(&line->chain);
281 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
282 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
283 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
284 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
285 line->file, hdr->ph_line_num, line->fn, line->text);
291 static int parse_buffer(FILE *in, FILE *out)
293 struct dbg_line *line;
294 struct ptldebug_header *hdr;
297 unsigned long dropped = 0, kept = 0;
298 struct list_head chunk_list;
300 INIT_LIST_HEAD(&chunk_list);
303 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
308 if (hdr->ph_len == 0)
310 if (hdr->ph_len > 4094) {
311 fprintf(stderr, "unexpected large record: %d bytes. "
317 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
318 print_saved_records(&chunk_list, out);
319 assert(list_empty(&chunk_list));
322 rc = fread(buf + sizeof(hdr->ph_len), 1,
323 hdr->ph_len - sizeof(hdr->ph_len), in);
328 (!(subsystem_mask & hdr->ph_subsys) ||
329 (!(debug_mask & hdr->ph_mask)))) {
334 line = malloc(sizeof(*line));
336 fprintf(stderr, "malloc failed; printing accumulated "
337 "records and exiting.\n");
341 line->hdr = malloc(hdr->ph_len + 1);
342 if (line->hdr == NULL) {
343 fprintf(stderr, "malloc failed; printing accumulated "
344 "records and exiting.\n");
348 p = (void *)line->hdr;
349 memcpy(line->hdr, buf, hdr->ph_len);
350 p[hdr->ph_len] = '\0';
354 p += strlen(line->file) + 1;
356 p += strlen(line->fn) + 1;
359 list_add_ordered(line, &chunk_list);
363 print_saved_records(&chunk_list, out);
365 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
366 dropped + kept, kept, dropped);
370 int jt_dbg_debug_kernel(int argc, char **argv)
374 FILE *in, *out = stdout;
377 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
383 } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
387 sprintf(filename, "%s.%lu.%u", argc > 1 ? argv[1] :
388 "/tmp/lustre-log", time(NULL), getpid());
393 fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
395 if (errno == ENOENT) /* no dump file created */
398 fprintf(stderr, "open(dump_kernel) failed: %s\n",
403 rc = write(fd, filename, strlen(filename));
404 if (rc != strlen(filename)) {
405 fprintf(stderr, "write(%s) failed: %s\n", filename,
415 in = fopen(filename, "r");
417 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
422 out = fopen(argv[1], "w");
424 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
431 rc = parse_buffer(in, out);
436 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
437 "behind.\n", filename);
439 rc = unlink(filename);
441 fprintf(stderr, "dumped successfully, but couldn't "
442 "unlink tmp file %s: %s\n", filename,
448 int jt_dbg_debug_file(int argc, char **argv)
451 FILE *in, *out = stdout;
452 if (argc > 3 || argc < 2) {
453 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
457 fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
459 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
463 in = fdopen(fdin, "r");
465 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
471 fdout = open(argv[2], O_CREAT | O_WRONLY | O_LARGEFILE);
473 fprintf(stderr, "open(%s) failed: %s\n", argv[2],
478 out = fdopen(fdout, "w");
480 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
488 return parse_buffer(in, out);
491 const char debug_daemon_usage[]="usage: debug_daemon {start file [MB]|stop}\n";
492 int jt_dbg_debug_daemon(int argc, char **argv)
497 fprintf(stderr, debug_daemon_usage);
501 fd = open("/proc/sys/portals/daemon_file", O_WRONLY);
503 fprintf(stderr, "open(daemon_file) failed: %s\n",
508 if (strcasecmp(argv[1], "start") == 0) {
510 fprintf(stderr, debug_daemon_usage);
514 rc = write(fd, argv[2], strlen(argv[2]));
515 if (rc != strlen(argv[2])) {
516 fprintf(stderr, "write(%s) failed: %s\n", argv[2],
521 } else if (strcasecmp(argv[1], "stop") == 0) {
522 rc = write(fd, "stop", 4);
524 fprintf(stderr, "write(stop) failed: %s\n",
530 fprintf(stderr, debug_daemon_usage);
538 int jt_dbg_clear_debug_buf(int argc, char **argv)
541 struct portal_ioctl_data data;
544 fprintf(stderr, "usage: %s\n", argv[0]);
548 memset(&data, 0, sizeof(data));
549 if (portal_ioctl_pack(&data, &buf, max) != 0) {
550 fprintf(stderr, "portal_ioctl_pack failed.\n");
554 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
556 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
563 int jt_dbg_mark_debug_buf(int argc, char **argv)
565 int rc, max_size = MAX_MARK_SIZE-1;
566 struct portal_ioctl_data data;
568 time_t now = time(NULL);
572 text = malloc(MAX_MARK_SIZE);
573 strncpy(text, argv[1], max_size);
574 max_size-=strlen(argv[1]);
575 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
576 strncat(text, " ", 1);
578 strncat(text, argv[counter], max_size);
579 max_size-=strlen(argv[counter]);
583 text[strlen(text) - 1] = '\0'; /* stupid \n */
586 text[MAX_MARK_SIZE - 1] = '\0';
589 memset(&data, 0, sizeof(data));
590 data.ioc_inllen1 = strlen(text) + 1;
591 data.ioc_inlbuf1 = text;
592 if (portal_ioctl_pack(&data, &buf, max) != 0) {
593 fprintf(stderr, "portal_ioctl_pack failed.\n");
597 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
599 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
606 static struct mod_paths {
609 {"libcfs", "lustre/portals/libcfs"},
610 {"portals", "lustre/portals/portals"},
611 {"ksocknal", "lustre/portals/knals/socknal"},
612 {"kptlrouter", "lustre/portals/router"},
613 {"lvfs", "lustre/lvfs"},
614 {"obdclass", "lustre/obdclass"},
615 {"llog_test", "lustre/obdclass"},
616 {"ptlrpc", "lustre/ptlrpc"},
617 {"obdext2", "lustre/obdext2"},
618 {"ost", "lustre/ost"},
619 {"osc", "lustre/osc"},
620 {"mds", "lustre/mds"},
621 {"mdc", "lustre/mdc"},
622 {"llite", "lustre/llite"},
623 {"ldiskfs", "lustre/ldiskfs"},
624 {"smfs", "lustre/smfs"},
625 {"obdecho", "lustre/obdecho"},
626 {"ldlm", "lustre/ldlm"},
627 {"obdfilter", "lustre/obdfilter"},
628 {"extN", "lustre/extN"},
629 {"lov", "lustre/lov"},
630 {"lmv", "lustre/lmv"},
631 {"fsfilt_ext3", "lustre/lvfs"},
632 {"fsfilt_extN", "lustre/lvfs"},
633 {"fsfilt_reiserfs", "lustre/lvfs"},
634 {"fsfilt_smfs", "lustre/lvfs"},
635 {"fsfilt_ldiskfs", "lustre/lvfs"},
636 {"mds_ext2", "lustre/mds"},
637 {"mds_ext3", "lustre/mds"},
638 {"mds_extN", "lustre/mds"},
639 {"ptlbd", "lustre/ptlbd"},
640 {"mgmt_svc", "lustre/mgmt"},
641 {"mgmt_cli", "lustre/mgmt"},
642 {"cobd", "lustre/cobd"},
643 {"cmobd", "lustre/cmobd"},
644 {"conf_obd", "lustre/obdclass"},
648 static int jt_dbg_modules_2_4(int argc, char **argv)
650 #ifdef HAVE_LINUX_VERSION_H
651 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
652 struct mod_paths *mp;
654 char *kernel = "linux";
661 printf("%s [path] [kernel]\n", argv[0]);
665 for (mp = mod_paths; mp->name != NULL; mp++) {
666 struct module_info info;
669 int query_module(const char *name, int which, void *buf,
670 size_t bufsize, size_t *ret);
672 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
676 printf("query_module(%s) failed: %s\n",
677 mp->name, strerror(errno));
679 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
681 info.addr + sizeof(struct module));
686 #endif /* Headers are 2.6-only */
687 #endif /* !HAVE_LINUX_VERSION_H */
691 static int jt_dbg_modules_2_5(int argc, char **argv)
693 struct mod_paths *mp;
695 char *kernel = "linux";
696 const char *proc = "/proc/modules";
697 char modname[128], others[128];
707 printf("%s [path] [kernel]\n", argv[0]);
711 file = fopen(proc, "r");
713 printf("failed open %s: %s\n", proc, strerror(errno));
717 while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
718 modname, others, others, others, others, &modaddr)) == 6) {
719 for (mp = mod_paths; mp->name != NULL; mp++) {
720 if (!strcmp(mp->name, modname))
724 printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
725 mp->path, mp->name, modaddr);
732 int jt_dbg_modules(int argc, char **argv)
735 struct utsname sysinfo;
737 rc = uname(&sysinfo);
739 printf("uname() failed: %s\n", strerror(errno));
743 if (sysinfo.release[2] > '4') {
744 return jt_dbg_modules_2_5(argc, argv);
746 return jt_dbg_modules_2_4(argc, argv);
752 int jt_dbg_panic(int argc, char **argv)
755 struct portal_ioctl_data data;
758 fprintf(stderr, "usage: %s\n", argv[0]);
762 memset(&data, 0, sizeof(data));
763 if (portal_ioctl_pack(&data, &buf, max) != 0) {
764 fprintf(stderr, "portal_ioctl_pack failed.\n");
768 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
770 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",