Whamcloud - gitweb
Whitespace cleanup.
[fs/lustre-release.git] / lnet / utils / debug.c
index e4dc15e..8dfc0ff 100644 (file)
@@ -1,61 +1,52 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
+ * GPL HEADER START
  *
- *   This file is part of Lustre Networking, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   LNET is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   LNET is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with LNET; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- * Some day I'll split all of this functionality into a cfs_debug module
- * of its own.  That day is not today.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  *
+ * lnet/utils/debug.c
+ * Some day I'll split all of this functionality into a cfs_debug module
+ * of its own. That day is not today.
  */
 
 #define __USE_FILE_OFFSET64
+#ifndef _GNU_SOURCE
 #define  _GNU_SOURCE
-
-#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/ioctl.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/utsname.h>
 
-#include <lnet/api-support.h>
+#include <libcfs/libcfsutil.h>
 #include <lnet/lnetctl.h>
-#include <libcfs/portals_utils.h>
-#include "parser.h"
 
-#include <time.h>
+#include <sys/utsname.h>
 
 static char rawbuf[8192];
 static char *buf = rawbuf;
@@ -84,17 +75,6 @@ static const char *libcfs_debug_masks[] =
          "rpctrace", "vfstrace", "reada", "mmap",
          "config", "console", "quota", "sec", NULL};
 
-struct debug_daemon_cmd {
-        char *cmd;
-        unsigned int cmdv;
-};
-
-static const struct debug_daemon_cmd libcfs_debug_daemon_cmd[] = {
-        {"start", DEBUG_DAEMON_START},
-        {"stop", DEBUG_DAEMON_STOP},
-        {0, 0}
-};
-
 #ifdef __linux__
 
 #define DAEMON_CTL_NAME         "/proc/sys/lnet/daemon_file"
@@ -321,42 +301,32 @@ struct dbg_line {
         char *file;
         char *fn;
         char *text;
-        struct list_head chain;
 };
 
-/* nurr. */
-static void list_add_ordered(struct dbg_line *new, struct list_head *head)
+static int cmp_rec(const void *p1, const void *p2)
 {
-        struct list_head *pos;
-        struct dbg_line *curr;
-
-        list_for_each(pos, head) {
-                curr = list_entry(pos, struct dbg_line, chain);
+        struct dbg_line *d1 = *(struct dbg_line **)p1;
+        struct dbg_line *d2 = *(struct dbg_line **)p2;
 
-                if (curr->hdr->ph_sec < new->hdr->ph_sec)
-                        continue;
-                if (curr->hdr->ph_sec == new->hdr->ph_sec &&
-                    curr->hdr->ph_usec < new->hdr->ph_usec)
-                        continue;
-
-                list_add(&new->chain, pos->prev);
-                return;
-        }
-        list_add_tail(&new->chain, head);
+        if (d1->hdr->ph_sec < d2->hdr->ph_sec)
+                return -1;
+        if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
+            d1->hdr->ph_usec < d2->hdr->ph_usec)
+                return -1;
+        if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
+            d1->hdr->ph_usec == d2->hdr->ph_usec)
+                return 0;
+        return 1;
 }
 
-static void print_saved_records(struct list_head *list, FILE *out)
+static void print_rec(struct dbg_line **linev, int used, FILE *out)
 {
-        struct list_head *pos, *tmp;
-
-        list_for_each_safe(pos, tmp, list) {
-                struct dbg_line *line;
-                struct ptldebug_header *hdr;
+        int i;
 
-                line = list_entry(pos, struct dbg_line, chain);
-                list_del(&line->chain);
+        for (i = 0; i < used; i++) {
+                struct dbg_line *line = linev[i];
+                struct ptldebug_header *hdr = line->hdr;
 
-                hdr = line->hdr;
                 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,
@@ -365,6 +335,26 @@ static void print_saved_records(struct list_head *list, FILE *out)
                 free(line->hdr);
                 free(line);
         }
+        free(linev);
+}
+
+static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
+                   int used)
+{
+        struct dbg_line **linev = *linevp;
+
+        if (used == *lenp) {
+                int nlen = *lenp + 512;
+                int nsize = nlen * sizeof(struct dbg_line *);
+
+                linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
+                if (!linev)
+                        return 0;
+                *linevp = linev;
+                *lenp = nlen;
+        }
+        linev[used] = line; 
+        return 1;
 }
 
 static int parse_buffer(FILE *in, FILE *out)
@@ -374,9 +364,8 @@ static int parse_buffer(FILE *in, FILE *out)
         char buf[4097], *p;
         int rc;
         unsigned long dropped = 0, kept = 0;
-        struct list_head chunk_list;
-
-        CFS_INIT_LIST_HEAD(&chunk_list);
+        struct dbg_line **linev = NULL;
+        int linev_len = 0;
 
         while (1) {
                 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
@@ -393,19 +382,13 @@ static int parse_buffer(FILE *in, FILE *out)
                         break;
                 }
 
-                if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
-                        print_saved_records(&chunk_list, out);
-                        assert(list_empty(&chunk_list));
-                }
-
                 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
                            hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
                 if (rc <= 0)
                         break;
 
-                if (hdr->ph_mask &&
-                    (!(subsystem_mask & hdr->ph_subsys) ||
-                     (!(debug_mask & hdr->ph_mask)))) {
+                if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
+                    (hdr->ph_mask   && !(debug_mask & hdr->ph_mask))) {
                         dropped++;
                         continue;
                 }
@@ -436,11 +419,18 @@ static int parse_buffer(FILE *in, FILE *out)
                 p += strlen(line->fn) + 1;
                 line->text = p;
 
-                list_add_ordered(line, &chunk_list);
+                if (!add_rec(line, &linev, &linev_len, kept)) {
+                        fprintf(stderr, "malloc failed; printing accumulated " 
+                                "records and exiting.\n");
+                        break;
+                }        
                 kept++;
         }
 
-        print_saved_records(&chunk_list, out);
+        if (linev) {
+                qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
+                print_rec(linev, kept, out);
+        }
 
         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
                 dropped + kept, kept, dropped);
@@ -472,7 +462,8 @@ int jt_dbg_debug_kernel(int argc, char **argv)
         if (argc > 1 && raw)
                 strcpy(filename, argv[1]);
         else
-                sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
+                sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
+                       time(NULL),getpid());
 
         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
                 unlink(filename);
@@ -731,7 +722,7 @@ int jt_dbg_mark_debug_buf(int argc, char **argv)
 static struct mod_paths {
         char *name, *path;
 } mod_paths[] = {
-        {"libcfs", "lnet/libcfs"},
+        {"libcfs", "libcfs/libcfs"},
         {"lnet", "lnet/lnet"},
         {"kciblnd", "lnet/klnds/ciblnd"},
         {"kgmlnd", "lnet/klnds/gmlnd"},
@@ -758,7 +749,8 @@ static struct mod_paths {
         {"mdc", "lustre/mdc"},
         {"llite", "lustre/llite"},
         {"lustre", "lustre/llite"},
-        {"ldiskfs", "lustre/ldiskfs"},
+        {"llite_lloop", "lustre/llite"},
+        {"ldiskfs", "ldiskfs/ldiskfs"},
         {"smfs", "lustre/smfs"},
         {"obdecho", "lustre/obdecho"},
         {"ldlm", "lustre/ldlm"},
@@ -786,44 +778,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;
 }
 
@@ -833,9 +787,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)
@@ -853,15 +806,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);
+                        }
                 }
         }