Whamcloud - gitweb
b=5684
[fs/lustre-release.git] / lnet / utils / debug.c
index d5ff09a..7b0ca54 100644 (file)
 #define __USE_FILE_OFFSET64
 #define  _GNU_SOURCE
 
-#include <portals/list.h>
-
 #include <stdio.h>
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
 #include <stdlib.h>
 #include <string.h>
-#include "ioctl.h"
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
-#ifndef __CYGWIN__
-# include <syscall.h>
-#endif
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#ifdef HAVE_LINUX_VERSION_H
-#include <linux/version.h>
-
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-#define BUG()                            /* workaround for module.h includes */
-#include <linux/module.h>
-#endif
-#endif /* !HAVE_LINUX_VERSION_H */
-
 #include <sys/utsname.h>
 
 #include <portals/api-support.h>
 #include <portals/ptlctl.h>
+#include <libcfs/portals_utils.h>
 #include "parser.h"
 
 #include <time.h>
@@ -75,15 +61,21 @@ static int debug_mask = ~0;
 #define MAX_MARK_SIZE 100
 
 static const char *portal_debug_subsystems[] =
-        {"undefined", "mdc", "mds", "osc", "ost", "class", "log", "llite",
-         "rpc", "mgmt", "portals", "libcfs", "socknal", "qswnal", "pinger",
-         "filter", "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd",
-         "ibnal", "lmv", "cmobd", "smfs", NULL};
+        {"undefined", "mdc", "mds", "osc", 
+         "ost", "class", "log", "llite",
+         "rpc", "mgmt", "portals", "nal", 
+         "pinger", "filter", "ptlbd", "echo", 
+         "ldlm", "lov", "router", "cobd", 
+         "sm", "asobd", "confobd", "lmv", 
+         "cmobd", NULL};
 static const char *portal_debug_masks[] =
-        {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
-         "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
-         "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
-         "reada", "mmap", NULL};
+        {"trace", "inode", "super", "ext2", 
+         "malloc", "cache", "info", "ioctl",
+         "blocks", "net", "warning", "buffs", 
+         "other", "dentry", "portals", "page", 
+         "dlmtrace", "error", "emerg", "ha", 
+         "rpctrace", "vfstrace", "reada", "mmap",
+         "config", "console", NULL};
 
 struct debug_daemon_cmd {
         char *cmd;
@@ -297,7 +289,7 @@ static int parse_buffer(FILE *in, FILE *out)
         unsigned long dropped = 0, kept = 0;
         struct list_head chunk_list;
 
-        INIT_LIST_HEAD(&chunk_list);
+        CFS_INIT_LIST_HEAD(&chunk_list);
 
         while (1) {
                 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
@@ -370,6 +362,7 @@ static int parse_buffer(FILE *in, FILE *out)
 int jt_dbg_debug_kernel(int argc, char **argv)
 {
         char filename[4096];
+        struct stat st;
         int rc, raw = 0, fd;
         FILE *in, *out = stdout;
 
@@ -383,18 +376,21 @@ int jt_dbg_debug_kernel(int argc, char **argv)
         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
                 raw = atoi(argv[1]);
                 argc--;
-        } else {
-                sprintf(filename, "%s.%lu.%u", argc > 1 ? argv[1] :
-                        "/tmp/lustre-log", time(NULL), getpid());
         }
 
-        unlink(filename);
+        /* If we are dumping raw (which means no conversion step to ASCII)
+         * then dump directly to any supplied filename, otherwise this is
+         * just a temp file and we dump to the real file at convert time. */
+        if (argc > 1 && raw)
+                strcpy(filename, argv[1]);
+        else
+                sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
+
+        if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
+                unlink(filename);
 
         fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
         if (fd < 0) {
-                if (errno == ENOENT) /* no dump file created */
-                        return 0;
-
                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
                         strerror(errno));
                 return 1;
@@ -414,6 +410,9 @@ int jt_dbg_debug_kernel(int argc, char **argv)
 
         in = fopen(filename, "r");
         if (in == NULL) {
+                if (errno == ENOENT) /* no dump file created */
+                        return 0;
+
                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
                         strerror(errno));
                 return 1;
@@ -488,51 +487,93 @@ int jt_dbg_debug_file(int argc, char **argv)
         return parse_buffer(in, out);
 }
 
-const char debug_daemon_usage[]="usage: debug_daemon {start file [MB]|stop}\n";
+static int
+dbg_write_cmd(int fd, char *str)
+{
+        int    len = strlen(str);
+        int    rc  = write(fd, str, len);
+        
+        return (rc == len ? 0 : 1);
+}
+
+const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
+#define DAEMON_FILE "/proc/sys/portals/daemon_file"
 int jt_dbg_debug_daemon(int argc, char **argv)
 {
-        int rc, fd;
+        int  rc;
+        int  fd;
 
         if (argc <= 1) {
-                fprintf(stderr, debug_daemon_usage);
-                return 0;
+                fprintf(stderr, debug_daemon_usage, argv[0]);
+                return 1;
         }
 
-        fd = open("/proc/sys/portals/daemon_file", O_WRONLY);
+        fd = open(DAEMON_FILE, O_WRONLY);
         if (fd < 0) {
-                fprintf(stderr, "open(daemon_file) failed: %s\n",
+                fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
                         strerror(errno));
-                return 1;
+                return -1;
         }
-
+        
+        rc = -1;
         if (strcasecmp(argv[1], "start") == 0) {
-                if (argc != 3) {
-                        fprintf(stderr, debug_daemon_usage);
-                        return 1;
+             if (argc < 3 || argc > 4 ||
+                    (argc == 4 && strlen(argv[3]) > 5)) {
+                        fprintf(stderr, debug_daemon_usage, argv[0]);
+                        goto out;
+                }
+                if (argc == 4) {
+                        char       buf[12];
+                        const long min_size = 10;
+                        const long max_size = 20480;
+                        long       size;
+                        char      *end;
+
+                        size = strtoul(argv[3], &end, 0);
+                        if (size < min_size ||
+                            size > max_size ||
+                            *end != 0) {
+                                fprintf(stderr, "size %s invalid, must be in "
+                                        "the range %ld-%ld MB\n", argv[3],
+                                        min_size, max_size);
+                                goto out;
+                        }
+                        snprintf(buf, sizeof(buf), "size=%ld", size);
+                        rc = dbg_write_cmd(fd, buf);
+
+                        if (rc != 0) {
+                                fprintf(stderr, "set %s failed: %s\n",
+                                        buf, strerror(errno));
+                                goto out;
+                        }
                 }
 
-                rc = write(fd, argv[2], strlen(argv[2]));
-                if (rc != strlen(argv[2])) {
-                        fprintf(stderr, "write(%s) failed: %s\n", argv[2],
-                                strerror(errno));
-                        close(fd);
-                        return 1;
+                rc = dbg_write_cmd(fd, argv[2]);
+                if (rc != 0) {
+                        fprintf(stderr, "start debug_daemon on %s failed: %s\n",
+                                argv[2], strerror(errno));
+                        goto out;
                 }
-        } else if (strcasecmp(argv[1], "stop") == 0) {
-                rc = write(fd, "stop", 4);
-                if (rc != 4) {
-                        fprintf(stderr, "write(stop) failed: %s\n",
+                rc = 0;
+                goto out;
+        }
+        if (strcasecmp(argv[1], "stop") == 0) {
+                rc = dbg_write_cmd(fd, "stop");
+                if (rc != 0) {
+                        fprintf(stderr, "stopping debug_daemon failed: %s\n",
                                 strerror(errno));
-                        close(fd);
-                        return 1;
+                        goto out;
                 }
-        } else {
-                fprintf(stderr, debug_daemon_usage);
-                return 1;
+
+                rc = 0;
+                goto out;
         }
 
+        fprintf(stderr, debug_daemon_usage, argv[0]);
+        rc = -1;
+out:
         close(fd);
-        return 0;
+        return rc;
 }
 
 int jt_dbg_clear_debug_buf(int argc, char **argv)
@@ -606,10 +647,10 @@ int jt_dbg_mark_debug_buf(int argc, char **argv)
 static struct mod_paths {
         char *name, *path;
 } mod_paths[] = {
-        {"libcfs", "lustre/portals/libcfs"},
-        {"portals", "lustre/portals/portals"},
-        {"ksocknal", "lustre/portals/knals/socknal"},
-        {"kptlrouter", "lustre/portals/router"},
+        {"libcfs", "portals/libcfs"},
+        {"portals", "portals/portals"},
+        {"ksocknal", "portals/knals/socknal"},
+        {"kptlrouter", "portals/router"},
         {"lvfs", "lustre/lvfs"},
         {"obdclass", "lustre/obdclass"},
         {"llog_test", "lustre/obdclass"},
@@ -641,7 +682,7 @@ static struct mod_paths {
         {"mgmt_cli", "lustre/mgmt"},
         {"cobd", "lustre/cobd"},
         {"cmobd", "lustre/cmobd"},
-        {"conf_obd", "lustre/obdclass"},
+        {"confobd", "lustre/obdclass"},
         {NULL, NULL}
 };