Whamcloud - gitweb
b=20595
[fs/lustre-release.git] / lnet / utils / debug.c
index a4376fd..3ce3013 100644 (file)
 #define  _GNU_SOURCE
 #endif
 
-#include <stdio.h>
-#ifdef HAVE_NETDB_H
-#include <netdb.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_SYS_IOCTL_H
-#include <sys/ioctl.h>
-#endif
-#ifndef _IOWR
-#include "ioctl.h"
-#endif
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <assert.h>
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/utsname.h>
-
 #include <libcfs/libcfsutil.h>
 #include <lnet/lnetctl.h>
-#include <libcfs/portals_utils.h>
 
-#include <time.h>
 
 static char rawbuf[8192];
 static char *buf = rawbuf;
@@ -86,7 +61,7 @@ static const char *libcfs_debug_subsystems[] =
          "ost", "class", "log", "llite",
          "rpc", "mgmt", "lnet", "lnd",
          "pinger", "filter", "", "echo",
-         "ldlm", "lov", "", "",
+         "ldlm", "lov", "lquota", "",
          "", "", "", "lmv",
          "", "sec", "gss", "", 
          "mgc", "mgs", "fid", "fld", NULL};
@@ -174,6 +149,40 @@ dbg_write_cmd(int fd, char *str, int len)
         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
@@ -268,10 +277,11 @@ static int applymask(char* procpath, int value)
         if (rc != 0) {
                 fprintf(stderr, "Write to %s failed: %s\n",
                         procpath, strerror(errno));
-                return rc;
         }
+
         dbg_close_ctlhandle(fd);
-        return 0;
+
+        return rc;
 }
 
 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
@@ -343,23 +353,30 @@ static int cmp_rec(const void *p1, const void *p2)
         return 1;
 }
 
-static void print_rec(struct dbg_line **linev, int used, FILE *out)
+static void print_rec(struct dbg_line ***linevp, int used, int fdout)
 {
+        struct dbg_line **linev = *linevp;
         int i;
 
+        qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
         for (i = 0; i < used; i++) {
                 struct dbg_line *line = linev[i];
                 struct ptldebug_header *hdr = line->hdr;
+                char out[4097];
+                int bytes;
 
-                fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
-                        hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
-                        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);
+                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,
+                                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);
                 free(line->hdr);
                 free(line);
         }
         free(linev);
+        *linevp = NULL;
 }
 
 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
@@ -368,96 +385,183 @@ static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
         struct dbg_line **linev = *linevp;
 
         if (used == *lenp) {
-                int nlen = *lenp + 512;
+                int nlen = *lenp + 4096;
                 int nsize = nlen * sizeof(struct dbg_line *);
 
-                linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
+                linev = realloc(*linevp, nsize);
                 if (!linev)
-                        return 0;
+                        return -ENOMEM;
+
                 *linevp = linev;
                 *lenp = nlen;
         }
-        linev[used] = line; 
-        return 1;
-}
+        linev[used] = line;
 
-static int parse_buffer(FILE *in, FILE *out)
+        return 0;
+}
+  
+static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
+{
+        fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
+        fprintf(stderr, "  len = %u\n", hdr->ph_len);
+        fprintf(stderr, "  flags = %x\n", hdr->ph_flags);
+        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, "  seconds = %u\n", hdr->ph_sec);
+        fprintf(stderr, "  microseconds = %lu\n", (long)hdr->ph_usec);
+        fprintf(stderr, "  stack = %u\n", hdr->ph_stack);
+        fprintf(stderr, "  pid = %u\n", hdr->ph_pid);
+        fprintf(stderr, "  host pid = %u\n", hdr->ph_extern_pid);
+        fprintf(stderr, "  line number = %u\n", hdr->ph_line_num);
+}
+#define HDR_SIZE sizeof(*hdr)
+static int parse_buffer(int fdin, int fdout)
 {
         struct dbg_line *line;
         struct ptldebug_header *hdr;
-        char buf[4097], *p;
-        int rc;
-        unsigned long dropped = 0, kept = 0;
+        char buf[4097], *ptr;
+        unsigned long dropped = 0, kept = 0, bad = 0;
         struct dbg_line **linev = NULL;
         int linev_len = 0;
-
+        int rc;
+        hdr = (void *)buf;
+  
         while (1) {
-                rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
+                int first_bad = 1;
+                int count;
+                count = HDR_SIZE;
+                ptr = buf;
+        readhdr:
+                rc = read(fdin, ptr, count);
                 if (rc <= 0)
-                        break;
+                         goto print;
+  
+                ptr += rc;
+                count -= rc;
+                if (count > 0)
+                        goto readhdr;
+                
+                if (hdr->ph_len > 4094 ||       /* is this header bogus? */
+                    hdr->ph_cpu_id > 65536 ||
+                    hdr->ph_stack > 65536 ||
+                    hdr->ph_sec < (1 << 30) ||
+                    hdr->ph_usec > 1000000000 ||
+                    hdr->ph_line_num > 65536) {
+                        if (first_bad)
+                                dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
+                        bad += first_bad;
+                        first_bad = 0;
+                        /* try to restart on next line */
+                        while (count < HDR_SIZE && buf[count] != '\n')
+                                count++;
+                        if (buf[count] == '\n')
+                                count++; /* move past '\n' */
+                        if (HDR_SIZE - count > 0) {
+                                int left = HDR_SIZE - count;
+
+                                memmove(buf, buf + count, left);
+                                ptr = buf + left;
+
+                                goto readhdr;
+                        }
 
-                hdr = (void *)buf;
-                if (hdr->ph_len == 0)
-                        break;
-                if (hdr->ph_len > 4094) {
-                        fprintf(stderr, "unexpected large record: %d bytes.  "
-                                "aborting.\n",
-                                hdr->ph_len);
-                        break;
+                        continue;
                 }
+  
+                if (hdr->ph_len == 0)
+                        continue;
 
-                rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
-                           hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
+                count = hdr->ph_len - HDR_SIZE;
+        readmore:
+                rc = read(fdin, ptr, count);
                 if (rc <= 0)
                         break;
+  
+                ptr += rc;
+                count -= rc;
+                if (count > 0)
+                        goto readmore;
+                first_bad = 1;
 
                 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
-                    (hdr->ph_mask   && !(debug_mask & hdr->ph_mask))) {
+                    (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
                         dropped++;
                         continue;
                 }
-
+  
+        retry_alloc:
                 line = malloc(sizeof(*line));
                 if (line == NULL) {
-                        fprintf(stderr, "malloc failed; printing accumulated "
-                                "records and exiting.\n");
+                         if (linev) {
+                                fprintf(stderr, "error: line malloc(%u): "
+                                        "printing accumulated records\n",
+                                        (unsigned int)sizeof(*line));
+                                print_rec(&linev, kept, fdout);
+
+                                goto retry_alloc;
+                        }
+                        fprintf(stderr, "error: line malloc(%u): exiting\n",
+                                (unsigned int)sizeof(*line));
                         break;
                 }
 
                 line->hdr = malloc(hdr->ph_len + 1);
                 if (line->hdr == NULL) {
                         free(line);
-                        fprintf(stderr, "malloc failed; printing accumulated "
-                                "records and exiting.\n");
+                        if (linev) {
+                                fprintf(stderr, "error: hdr malloc(%u): "
+                                        "printing accumulated records\n",
+                                        hdr->ph_len + 1);
+                                print_rec(&linev, kept, fdout);
+                                goto retry_alloc;
+                        }
+                        fprintf(stderr, "error: hdr malloc(%u): exiting\n",
+                                        hdr->ph_len + 1);
                         break;
                 }
-
-                p = (void *)line->hdr;
+  
+                ptr = (void *)line->hdr;
                 memcpy(line->hdr, buf, hdr->ph_len);
-                p[hdr->ph_len] = '\0';
-
-                p += sizeof(*hdr);
-                line->file = p;
-                p += strlen(line->file) + 1;
-                line->fn = p;
-                p += strlen(line->fn) + 1;
-                line->text = p;
-
-                if (!add_rec(line, &linev, &linev_len, kept)) {
-                        fprintf(stderr, "malloc failed; printing accumulated " 
-                                "records and exiting.\n");
+                ptr[hdr->ph_len] = '\0';
+
+                ptr += sizeof(*hdr);
+                line->file = ptr;
+                ptr += strlen(line->file) + 1;
+                line->fn = ptr;
+                ptr += strlen(line->fn) + 1;
+                line->text = ptr;
+        retry_add:
+                if (add_rec(line, &linev, &linev_len, kept) < 0) {
+                        if (linev) {
+                                fprintf(stderr, "error: add_rec[%u] failed; "
+                                        "print accumulated records\n",
+                                        linev_len);
+                                print_rec(&linev, kept, fdout);
+
+                                goto retry_add;
+                        }
+                        fprintf(stderr, "error: add_rec[0] failed; exiting\n");
                         break;
-                }        
+                }
                 kept++;
         }
 
-        if (linev) {
-                qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
-                print_rec(linev, kept, out);
-        }
+print:
+        if (linev)
+                print_rec(&linev, kept, fdout);
+
+        printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
+                dropped + kept + bad, kept, dropped, bad);
 
-        printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
-                dropped + kept, kept, dropped);
         return 0;
 }
 
@@ -465,8 +569,11 @@ int jt_dbg_debug_kernel(int argc, char **argv)
 {
         char filename[4096];
         struct stat st;
-        int rc, raw = 0, fd;
-        FILE *in, *out = stdout;
+        int raw = 0;
+        int save_errno;
+        int fdin;
+        int fdout;
+        int rc;
 
         if (argc > 3) {
                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
@@ -486,54 +593,55 @@ int jt_dbg_debug_kernel(int argc, char **argv)
         if (argc > 1 && raw)
                 strcpy(filename, argv[1]);
         else
-                sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
-                       time(NULL),getpid());
+                sprintf(filename, "%s"CFS_TIME_T".%u",
+                       DEBUG_FILE_PATH_DEFAULT, time(NULL), getpid());
 
         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
                 unlink(filename);
 
-        fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
-        if (fd < 0) {
+        fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
+        if (fdin < 0) {
                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
                         strerror(errno));
                 return 1;
         }
 
-        rc = dbg_write_cmd(fd, filename, strlen(filename));
+        rc = dbg_write_cmd(fdin, filename, strlen(filename));
+        save_errno = errno;
+        dbg_close_ctlhandle(fdin);
         if (rc != 0) {
                 fprintf(stderr, "write(%s) failed: %s\n", filename,
-                        strerror(errno));
-                close(fd);
+                        strerror(save_errno));
                 return 1;
         }
-        dbg_close_ctlhandle(fd);
 
         if (raw)
                 return 0;
 
-        in = fopen(filename, "r");
-        if (in == NULL) {
+        fdin = open(filename, O_RDONLY);
+        if (fdin < 0) {
                 if (errno == ENOENT) /* no dump file created */
                         return 0;
-
                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
                         strerror(errno));
                 return 1;
         }
         if (argc > 1) {
-                out = fopen(argv[1], "w");
-                if (out == NULL) {
+                fdout = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC);
+                if (fdout < 0) {
                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
                                 strerror(errno));
-                        fclose(in);
+                        close(fdin);
                         return 1;
                 }
-        }
+        } else {
+               fdout = fileno(stdout);
+       }
 
-        rc = parse_buffer(in, out);
-        fclose(in);
+        rc = parse_buffer(fdin, fdout);
+        close(fdin);
         if (argc > 1)
-                fclose(out);
+                close(fdout);
         if (rc) {
                 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
                         "behind.\n", filename);
@@ -544,6 +652,7 @@ int jt_dbg_debug_kernel(int argc, char **argv)
                                 "unlink tmp file %s: %s\n", filename,
                                 strerror(errno));
         }
+
         return rc;
 }
 
@@ -551,8 +660,6 @@ int jt_dbg_debug_file(int argc, char **argv)
 {
         int    fdin;
         int    fdout;
-        FILE  *in;
-        FILE  *out = stdout;
         int    rc;
 
         if (argc > 3 || argc < 2) {
@@ -561,43 +668,30 @@ int jt_dbg_debug_file(int argc, char **argv)
         }
 
         fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
-        if (fdin == -1) {
+        if (fdin < 0) {
                 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
                         strerror(errno));
                 return 1;
         }
-        in = fdopen(fdin, "r");
-        if (in == NULL) {
-                fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
-                        strerror(errno));
-                close(fdin);
-                return 1;
-        }
         if (argc > 2) {
                 fdout = open(argv[2],
                              O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
                              0600);
-                if (fdout == -1) {
+                if (fdout < 0) {
                         fprintf(stderr, "open(%s) failed: %s\n", argv[2],
                                 strerror(errno));
-                        fclose(in);
-                        return 1;
-                }
-                out = fdopen(fdout, "w");
-                if (out == NULL) {
-                        fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
-                                strerror(errno));
-                        fclose(in);
-                        close(fdout);
+                        close(fdin);
                         return 1;
                 }
+        } else {
+                fdout = fileno(stdout);
         }
 
-        rc = parse_buffer(in, out);
+        rc = parse_buffer(fdin, fdout);
 
-        fclose(in);
-        if (out != stdout)
-                fclose(out);
+        close(fdin);
+        if (fdout != fileno(stdout))
+                close(fdout);
 
         return rc;
 }
@@ -802,44 +896,6 @@ static struct mod_paths {
 
 static int jt_dbg_modules_2_4(int argc, char **argv)
 {
-#ifdef HAVE_LINUX_VERSION_H
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-        struct mod_paths *mp;
-        char *path = "";
-        char *kernel = "linux";
-
-        if (argc >= 2)
-                path = argv[1];
-        if (argc == 3)
-                kernel = argv[2];
-        if (argc > 3) {
-                printf("%s [path] [kernel]\n", argv[0]);
-                return 0;
-        }
-
-        for (mp = mod_paths; mp->name != NULL; mp++) {
-                struct module_info info;
-                int rc;
-                size_t crap;
-                int query_module(const char *name, int which, void *buf,
-                                 size_t bufsize, size_t *ret);
-
-                rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
-                                  &crap);
-                if (rc < 0) {
-                        if (errno != ENOENT)
-                                printf("query_module(%s) failed: %s\n",
-                                       mp->name, strerror(errno));
-                } else {
-                        printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
-                               path[0] ? "/" : "", mp->path, mp->name,
-                               info.addr + sizeof(struct module));
-                }
-        }
-
-        return 0;
-#endif // Headers are 2.6-only
-#endif // !HAVE_LINUX_VERSION_H
         return -EINVAL;
 }
 
@@ -849,9 +905,8 @@ static int jt_dbg_modules_2_5(int argc, char **argv)
         char *path = "";
         char *kernel = "linux";
         const char *proc = "/proc/modules";
-        char modname[128], others[4096];
+        char modname[128], buf[4096];
         long modaddr;
-        int rc;
         FILE *file;
 
         if (argc >= 2)
@@ -869,15 +924,17 @@ static int jt_dbg_modules_2_5(int argc, char **argv)
                 return 0;
         }
 
-        while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
-                modname, others, others, others, others, &modaddr)) == 6) {
-                for (mp = mod_paths; mp->name != NULL; mp++) {
-                        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);
+        while (fgets(buf, sizeof(buf), file) != NULL) {
+                if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
+                        for (mp = mod_paths; mp->name != NULL; mp++) {
+                                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);
+                        }
                 }
         }