Whamcloud - gitweb
LU-2456 lnet: Dynamic LNet Configuration (DLC) IOCTL changes
[fs/lustre-release.git] / lnet / utils / debug.c
index df15cd0..e1dd37b 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -56,25 +56,9 @@ static int debug_mask = ~0;
 
 #define MAX_MARK_SIZE 256
 
-static const char *libcfs_debug_subsystems[] =
-        {"undefined", "mdc", "mds", "osc",
-         "ost", "class", "log", "llite",
-         "rpc", "mgmt", "lnet", "lnd",
-         "pinger", "filter", "", "echo",
-         "ldlm", "lov", "lquota", "",
-         "", "", "", "lmv",
-         "", "sec", "gss", "", 
-         "mgc", "mgs", "fid", "fld", NULL};
-static const char *libcfs_debug_masks[] =
-        {"trace", "inode", "super", "ext2",
-         "malloc", "cache", "info", "ioctl",
-         "neterror", "net", "warning", "buffs",
-         "other", "dentry", "nettrace", "page",
-         "dlmtrace", "error", "emerg", "ha",
-         "rpctrace", "vfstrace", "reada", "mmap",
-         "config", "console", "quota", "sec", NULL};
-
-#ifdef __linux__
+static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
+static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
+
 
 #define DAEMON_CTL_NAME         "/proc/sys/lnet/daemon_file"
 #define SUBSYS_DEBUG_CTL_NAME   "/proc/sys/lnet/subsystem_debug"
@@ -108,84 +92,6 @@ dbg_write_cmd(int fd, char *str, int len)
         return (rc == len ? 0 : 1);
 }
 
-#elif defined(__DARWIN__)
-
-#define DAEMON_CTL_NAME         "lnet.trace_daemon"
-#define SUBSYS_DEBUG_CTL_NAME   "lnet.subsystem_debug"
-#define DEBUG_CTL_NAME          "lnet.debug"
-#define DUMP_KERNEL_CTL_NAME    "lnet.trace_dumpkernel"
-
-static char     sysctl_name[128];
-static int
-dbg_open_ctlhandle(const char *str)
-{
-
-        if (strlen(str)+1 > 128) {
-                fprintf(stderr, "sysctl name is too long: %s.\n", str);
-                return -1;
-        }
-        strcpy(sysctl_name, str);
-
-        return 0;
-}
-
-static void
-dbg_close_ctlhandle(int fd)
-{
-        sysctl_name[0] = '\0';
-        return;
-}
-
-static int
-dbg_write_cmd(int fd, char *str, int len)
-{
-        int     rc;
-
-        rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
-        if (rc != 0) {
-                fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
-                        sysctl_name, str, errno);
-        }
-        return (rc == 0 ? 0: 1);
-}
-
-#elif defined(__WINNT__)
-
-#define DAEMON_CTL_NAME         "/proc/sys/lnet/daemon_file"
-#define SUBSYS_DEBUG_CTL_NAME   "/proc/sys/lnet/subsystem_debug"
-#define DEBUG_CTL_NAME          "/proc/sys/lnet/debug"
-#define DUMP_KERNEL_CTL_NAME    "/proc/sys/lnet/dump_kernel"
-
-static int
-dbg_open_ctlhandle(const char *str)
-{
-        int fd;
-        fd = cfs_proc_open((char *)str, (int)O_WRONLY);
-        if (fd < 0) {
-                fprintf(stderr, "open %s failed: %s\n", str,
-                        strerror(errno));
-                return -1;
-        }
-        return fd;
-}
-
-static void
-dbg_close_ctlhandle(int fd)
-{
-        cfs_proc_close(fd);
-}
-
-static int
-dbg_write_cmd(int fd, char *str, int len)
-{
-        int    rc  = cfs_proc_write(fd, str, len);
-
-        return (rc == len ? 0 : 1);
-}
-
-#else
-#error - Unknown sysctl convention.
-#endif
 
 static int do_debug_mask(char *name, int enable)
 {
@@ -352,7 +258,7 @@ static int cmp_rec(const void *p1, const void *p2)
                 return 0;
         return 1;
 }
-  
+
 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
 {
         struct dbg_line **linev = *linevp;
@@ -363,15 +269,24 @@ static void print_rec(struct dbg_line ***linevp, int used, int fdout)
                 struct dbg_line *line = linev[i];
                 struct ptldebug_header *hdr = line->hdr;
                 char out[4097];
+                char *buf = out;
                 int bytes;
+                ssize_t bytes_written;
 
-                bytes = sprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
-                                hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
+                bytes = sprintf(out, "%08x:%08x:%u.%u%s:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
+                                hdr->ph_subsys, hdr->ph_mask,
+                                hdr->ph_cpu_id, hdr->ph_type,
+                                hdr->ph_flags & PH_FLAG_FIRST_RECORD ? "F" : "",
                                 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
                                 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
                                 line->file, hdr->ph_line_num, line->fn, line->text);
-
-                write(fdout, out, bytes);
+                while (bytes > 0) {
+                        bytes_written = write(fdout, buf, bytes);
+                        if (bytes_written <= 0)
+                                break;
+                        bytes -= bytes_written;
+                        buf += bytes_written;
+                }
                 free(line->hdr);
                 free(line);
         }
@@ -408,6 +323,7 @@ static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
         fprintf(stderr, "  subsystem = %x\n", hdr->ph_subsys);
         fprintf(stderr, "  mask = %x\n", hdr->ph_mask);
         fprintf(stderr, "  cpu_id = %u\n", hdr->ph_cpu_id);
+        fprintf(stderr, "  type = %u\n", hdr->ph_type);
         fprintf(stderr, "  seconds = %u\n", hdr->ph_sec);
         fprintf(stderr, "  microseconds = %lu\n", (long)hdr->ph_usec);
         fprintf(stderr, "  stack = %u\n", hdr->ph_stack);
@@ -447,7 +363,7 @@ static int parse_buffer(int fdin, int fdout)
                         goto readhdr;
                 
                 if (hdr->ph_len > 4094 ||       /* is this header bogus? */
-                    hdr->ph_cpu_id > 65536 ||
+                    hdr->ph_type >= libcfs_tcd_type_max() ||
                     hdr->ph_stack > 65536 ||
                     hdr->ph_sec < (1 << 30) ||
                     hdr->ph_usec > 1000000000 ||
@@ -561,14 +477,14 @@ print:
 
         printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
                 dropped + kept + bad, kept, dropped, bad);
-  
+
         return 0;
 }
 
 int jt_dbg_debug_kernel(int argc, char **argv)
 {
-        char filename[4096];
-        struct stat st;
+       struct stat st;
+       char filename[PATH_MAX];
         int raw = 0;
         int save_errno;
         int fdin;
@@ -590,15 +506,25 @@ int jt_dbg_debug_kernel(int argc, char **argv)
         /* 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, "%s"CFS_TIME_T".%u",
-                       DEBUG_FILE_PATH_DEFAULT, time(NULL), getpid());
+       if (argc > 1 && raw) {
+               if (strlen(argv[1]) >= sizeof(filename)) {
+                       fprintf(stderr, "File name too long: %s\n", argv[1]);
+                       return 1;
+               }
+               strncpy(filename, argv[1], sizeof(filename));
+       } else {
+               if (snprintf(filename, sizeof(filename), "%s"CFS_TIME_T".%u",
+                            LIBCFS_DEBUG_FILE_PATH_DEFAULT, time(NULL),
+                            getpid())
+                   >= sizeof(filename)) {
+                       fprintf(stderr, "File name too long\n");
+                       return 1;
+               }
+       }
 
         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
                 unlink(filename);
-  
+
         fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
         if (fdin < 0) {
                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
@@ -617,7 +543,7 @@ int jt_dbg_debug_kernel(int argc, char **argv)
 
         if (raw)
                 return 0;
-  
+
         fdin = open(filename, O_RDONLY);
         if (fdin < 0) {
                 if (errno == ENOENT) /* no dump file created */
@@ -627,7 +553,8 @@ int jt_dbg_debug_kernel(int argc, char **argv)
                 return 1;
         }
         if (argc > 1) {
-                fdout = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC);
+                fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
+                             S_IRUSR | S_IWUSR);
                 if (fdout < 0) {
                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
                                 strerror(errno));
@@ -800,119 +727,102 @@ int jt_dbg_clear_debug_buf(int argc, char **argv)
 
 int jt_dbg_mark_debug_buf(int argc, char **argv)
 {
-        static char scratch[MAX_MARK_SIZE] = { '\0' };
-        int rc, max_size = MAX_MARK_SIZE-1;
-        struct libcfs_ioctl_data data = { 0 };
-        char *text;
-        time_t now = time(NULL);
+       static char scratch[MAX_MARK_SIZE] = "";
+       struct libcfs_ioctl_data data = { 0 };
+       char *text;
+       int rc;
+
+       if (argc > 1) {
+               int count, max_size = sizeof(scratch) - 1;
+
+               strncpy(scratch, argv[1], max_size);
+               max_size -= strlen(argv[1]);
+               for (count = 2; (count < argc) && (max_size > 1); count++) {
+                       strncat(scratch, " ", max_size);
+                       max_size -= 1;
+                       strncat(scratch, argv[count], max_size);
+                       max_size -= strlen(argv[count]);
+               }
+               scratch[sizeof(scratch) - 1] = '\0';
+               text = scratch;
+       } else {
+               time_t now = time(NULL);
+               text = ctime(&now);
+       }
 
-        if (argc > 1) {
-                int count;
-                text = scratch;
-                strncpy(text, argv[1], max_size);
-                max_size-=strlen(argv[1]);
-                for (count = 2; (count < argc) && (max_size > 0); count++){
-                        strncat(text, " ", max_size);
-                        max_size -= 1;
-                        strncat(text, argv[count], max_size);
-                        max_size -= strlen(argv[count]);
-                }
-        } else {
-                text = ctime(&now);
-        }
+       data.ioc_inllen1 = strlen(text) + 1;
+       data.ioc_inlbuf1 = text;
 
-        data.ioc_inllen1 = strlen(text) + 1;
-        data.ioc_inlbuf1 = text;
-        if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
-                fprintf(stderr, "libcfs_ioctl_pack failed.\n");
-                return -1;
-        }
+       if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
+               fprintf(stderr, "libcfs_ioctl_pack failed.\n");
+               return -1;
+       }
 
-        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
-        if (rc) {
-                fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
-                        strerror(errno));
-                return -1;
-        }
-        return 0;
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
+       if (rc) {
+               fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+       return 0;
 }
 
 static struct mod_paths {
-        char *name, *path;
+       char *name, *path;
 } mod_paths[] = {
-        {"libcfs", "libcfs/libcfs"},
-        {"lnet", "lnet/lnet"},
-        {"kciblnd", "lnet/klnds/ciblnd"},
-        {"kgmlnd", "lnet/klnds/gmlnd"},
-        {"kmxlnd", "lnet/klnds/mxlnd"},
-        {"kiiblnd", "lnet/klnds/iiblnd"},
-        {"ko2iblnd", "lnet/klnds/o2iblnd"},
-        {"kopeniblnd", "lnet/klnds/openiblnd"},
-        {"kptllnd", "lnet/klnds/ptllnd"},
-        {"kqswlnd", "lnet/klnds/qswlnd"},
-        {"kralnd", "lnet/klnds/ralnd"},
-        {"ksocklnd", "lnet/klnds/socklnd"},
-        {"ktdilnd", "lnet/klnds/tdilnd"},
-        {"kviblnd", "lnet/klnds/viblnd"},
-        {"lvfs", "lustre/lvfs"},
-        {"obdclass", "lustre/obdclass"},
-        {"llog_test", "lustre/obdclass"},
-        {"ptlrpc_gss", "lustre/ptlrpc/gss"},
-        {"ptlrpc", "lustre/ptlrpc"},
-        {"gks", "lustre/sec/gks"},
-        {"gkc", "lustre/sec/gks"},
-        {"ost", "lustre/ost"},
-        {"osc", "lustre/osc"},
-        {"mds", "lustre/mds"},
-        {"mdc", "lustre/mdc"},
-        {"llite", "lustre/llite"},
-        {"lustre", "lustre/llite"},
-        {"llite_lloop", "lustre/llite"},
-        {"ldiskfs", "ldiskfs/ldiskfs"},
-        {"smfs", "lustre/smfs"},
-        {"obdecho", "lustre/obdecho"},
-        {"ldlm", "lustre/ldlm"},
-        {"obdfilter", "lustre/obdfilter"},
-        {"lov", "lustre/lov"},
-        {"lmv", "lustre/lmv"},
-        {"fsfilt_ext3", "lustre/lvfs"},
-        {"fsfilt_reiserfs", "lustre/lvfs"},
-        {"fsfilt_smfs", "lustre/lvfs"},
-        {"fsfilt_ldiskfs", "lustre/lvfs"},
-        {"mds_ext3", "lustre/mds"},
-        {"cobd", "lustre/cobd"},
-        {"cmobd", "lustre/cmobd"},
-        {"lquota", "lustre/quota"},
-        {"mgs", "lustre/mgs"},
-        {"mgc", "lustre/mgc"},
-        {"mdt", "lustre/mdt"},
-        {"mdd", "lustre/mdd"},
-        {"osd", "lustre/osd"},
-        {"cmm", "lustre/cmm"},
+       { "libcfs", "libcfs/libcfs" },
+       { "lnet", "lnet/lnet" },
+       { "kmxlnd", "lnet/klnds/mxlnd" },
+       { "ko2iblnd", "lnet/klnds/o2iblnd" },
+       { "kgnilnd", "lnet/klnds/gnilnd"},
+       { "kqswlnd", "lnet/klnds/qswlnd" },
+       { "kralnd", "lnet/klnds/ralnd" },
+       { "ksocklnd", "lnet/klnds/socklnd" },
+       { "ktdilnd", "lnet/klnds/tdilnd" },
+       { "obdclass", "lustre/obdclass" },
+       { "llog_test", "lustre/obdclass" },
+       { "ptlrpc_gss", "lustre/ptlrpc/gss" },
+       { "ptlrpc", "lustre/ptlrpc" },
+       { "gks", "lustre/sec/gks" },
+       { "gkc", "lustre/sec/gks" },
+       { "ost", "lustre/ost" },
+       { "osc", "lustre/osc" },
+       { "mds", "lustre/mds" },
+       { "mdc", "lustre/mdc" },
+       { "lustre", "lustre/llite" },
+       { "llite_lloop", "lustre/llite" },
+       { "ldiskfs", "ldiskfs" },
+       { "obdecho", "lustre/obdecho" },
+       { "ldlm", "lustre/ldlm" },
+       { "obdfilter", "lustre/obdfilter" },
+       { "lov", "lustre/lov" },
+       { "lmv", "lustre/lmv" },
+       { "lquota", "lustre/quota" },
+       { "mgs", "lustre/mgs" },
+       { "mgc", "lustre/mgc" },
+       { "mdt", "lustre/mdt" },
+       { "mdd", "lustre/mdd" },
+       { "osd", "lustre/osd" },
+       { "cmm", "lustre/cmm" },
         {"fid", "lustre/fid"},
         {"fld", "lustre/fld"},
+       {"lod", "lustre/lod"},
+       {"osp", "lustre/osp"},
+       { "lfsck", "lustre/lfsck" },
         {NULL, NULL}
 };
 
-static int jt_dbg_modules_2_4(int argc, char **argv)
-{
-        return -EINVAL;
-}
-
-static int jt_dbg_modules_2_5(int argc, char **argv)
+int jt_dbg_modules(int argc, char **argv)
 {
         struct mod_paths *mp;
         char *path = "";
-        char *kernel = "linux";
         const char *proc = "/proc/modules";
         char modname[128], buf[4096];
-        long modaddr;
+       unsigned long modaddr;
         FILE *file;
 
         if (argc >= 2)
                 path = argv[1];
-        if (argc == 3)
-                kernel = argv[2];
         if (argc > 3) {
                 printf("%s [path] [kernel]\n", argv[0]);
                 return 0;
@@ -930,11 +840,11 @@ static int jt_dbg_modules_2_5(int argc, char **argv)
                                 if (!strcmp(mp->name, modname))
                                         break;
                         }
-                        if (mp->name) {
-                                printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", 
-                                       path, path[0] ? "/" : "", 
-                                       mp->path, mp->name, modaddr);
-                        }
+                       if (mp->name) {
+                               printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
+                                       path, path[0] ? "/" : "",
+                                       mp->path, mp->name, modaddr);
+                       }
                 }
         }
 
@@ -942,26 +852,6 @@ static int jt_dbg_modules_2_5(int argc, char **argv)
         return 0;
 }
 
-int jt_dbg_modules(int argc, char **argv)
-{
-        int rc = 0;
-        struct utsname sysinfo;
-
-        rc = uname(&sysinfo);
-        if (rc) {
-                printf("uname() failed: %s\n", strerror(errno));
-                return 0;
-        }
-
-        if (sysinfo.release[2] > '4') {
-                return jt_dbg_modules_2_5(argc, argv);
-        } else {
-                return jt_dbg_modules_2_4(argc, argv);
-        }
-
-        return 0;
-}
-
 int jt_dbg_panic(int argc, char **argv)
 {
         int rc;