]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zfs_debug.c
Fixes for procfs files backed by linked lists
[mirror_zfs.git] / module / zfs / zfs_debug.c
index 4f612e16ba8b0ffbbaf19efa17477a1012dacdf8..b5f93fd9bebb4cb31136e89c7470de8850b6bc5d 100644 (file)
  */
 /*
  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
 
-#if !defined(_KERNEL) || !defined(__linux__)
-list_t zfs_dbgmsgs;
-int zfs_dbgmsg_size;
-kmutex_t zfs_dbgmsgs_lock;
-int zfs_dbgmsg_maxsize = 1<<20; /* 1MB */
-#endif
+typedef struct zfs_dbgmsg {
+       procfs_list_node_t      zdm_node;
+       time_t                  zdm_timestamp;
+       int                     zdm_size;
+       char                    zdm_msg[1]; /* variable length allocation */
+} zfs_dbgmsg_t;
 
-/*
- * Enable various debugging features.
- */
-int zfs_flags = 0;
+procfs_list_t zfs_dbgmsgs;
+int zfs_dbgmsg_size = 0;
+int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
 
 /*
- * zfs_recover can be set to nonzero to attempt to recover from
- * otherwise-fatal errors, typically caused by on-disk corruption.  When
- * set, calls to zfs_panic_recover() will turn into warning messages.
- * This should only be used as a last resort, as it typically results
- * in leaked space, or worse.
+ * Internal ZFS debug messages are enabled by default.
+ *
+ * # Print debug messages
+ * cat /proc/spl/kstat/zfs/dbgmsg
+ *
+ * # Disable the kernel debug message log.
+ * echo 0 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
+ *
+ * # Clear the kernel debug message log.
+ * echo 0 >/proc/spl/kstat/zfs/dbgmsg
  */
-int zfs_recover = 0;
+int zfs_dbgmsg_enable = 1;
 
+static int
+zfs_dbgmsg_show_header(struct seq_file *f)
+{
+       seq_printf(f, "%-12s %-8s\n", "timestamp", "message");
+       return (0);
+}
 
-void
-zfs_panic_recover(const char *fmt, ...)
+static int
+zfs_dbgmsg_show(struct seq_file *f, void *p)
 {
-       va_list adx;
+       zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)p;
+       seq_printf(f, "%-12llu %-s\n",
+           (u_longlong_t)zdm->zdm_timestamp, zdm->zdm_msg);
+       return (0);
+}
+
+static void
+zfs_dbgmsg_purge(int max_size)
+{
+       while (zfs_dbgmsg_size > max_size) {
+               zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs.pl_list);
+               if (zdm == NULL)
+                       return;
 
-       va_start(adx, fmt);
-       vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
-       va_end(adx);
+               int size = zdm->zdm_size;
+               kmem_free(zdm, size);
+               zfs_dbgmsg_size -= size;
+       }
+}
+
+static int
+zfs_dbgmsg_clear(procfs_list_t *procfs_list)
+{
+       mutex_enter(&zfs_dbgmsgs.pl_lock);
+       zfs_dbgmsg_purge(0);
+       mutex_exit(&zfs_dbgmsgs.pl_lock);
+       return (0);
 }
 
-/*
- * Debug logging is enabled by default for production kernel builds.
- * The overhead for this is negligible and the logs can be valuable when
- * debugging.  For non-production user space builds all debugging except
- * logging is enabled since performance is no longer a concern.
- */
 void
 zfs_dbgmsg_init(void)
 {
-#if !defined(_KERNEL) || !defined(__linux__)
-       list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
+       procfs_list_install("zfs",
+           "dbgmsg",
+           &zfs_dbgmsgs,
+           zfs_dbgmsg_show,
+           zfs_dbgmsg_show_header,
+           zfs_dbgmsg_clear,
            offsetof(zfs_dbgmsg_t, zdm_node));
-       mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
-#endif
-
-       if (zfs_flags == 0) {
-#if defined(_KERNEL)
-               zfs_flags = ZFS_DEBUG_DPRINTF;
-               spl_debug_set_mask(spl_debug_get_mask() | SD_DPRINTF);
-               spl_debug_set_subsys(spl_debug_get_subsys() | SS_USER1);
-#else
-               zfs_flags = ~ZFS_DEBUG_DPRINTF;
-#endif /* _KERNEL */
-       }
 }
 
 void
 zfs_dbgmsg_fini(void)
 {
-#if !defined(_KERNEL) || !defined(__linux__)
-       zfs_dbgmsg_t *zdm;
+       procfs_list_uninstall(&zfs_dbgmsgs);
+       zfs_dbgmsg_purge(0);
 
-       while ((zdm = list_remove_head(&zfs_dbgmsgs)) != NULL) {
-               int size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
-               kmem_free(zdm, size);
-               zfs_dbgmsg_size -= size;
-       }
-       mutex_destroy(&zfs_dbgmsgs_lock);
-       ASSERT0(zfs_dbgmsg_size);
+       /*
+        * TODO - decide how to make this permanent
+        */
+#ifdef _KERNEL
+       procfs_list_destroy(&zfs_dbgmsgs);
 #endif
 }
 
-#if !defined(_KERNEL) || !defined(__linux__)
-/*
- * Print these messages by running:
- * echo ::zfs_dbgmsg | mdb -k
- *
- * Monitor these messages by running:
- *     dtrace -q -n 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
- */
 void
-zfs_dbgmsg(const char *fmt, ...)
+__set_error(const char *file, const char *func, int line, int err)
 {
-       int size;
+       /*
+        * To enable this:
+        *
+        * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
+        */
+       if (zfs_flags & ZFS_DEBUG_SET_ERROR)
+               __dprintf(file, func, line, "error %lu", err);
+}
+
+#ifdef _KERNEL
+static void
+__zfs_dbgmsg(char *buf)
+{
+       int size = sizeof (zfs_dbgmsg_t) + strlen(buf);
+       zfs_dbgmsg_t *zdm = kmem_zalloc(size, KM_SLEEP);
+       zdm->zdm_size = size;
+       zdm->zdm_timestamp = gethrestime_sec();
+       strcpy(zdm->zdm_msg, buf);
+
+       mutex_enter(&zfs_dbgmsgs.pl_lock);
+       procfs_list_add(&zfs_dbgmsgs, zdm);
+       zfs_dbgmsg_size += size;
+       zfs_dbgmsg_purge(MAX(zfs_dbgmsg_maxsize, 0));
+       mutex_exit(&zfs_dbgmsgs.pl_lock);
+}
+
+void
+__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
+{
+       const char *newfile;
        va_list adx;
-       zfs_dbgmsg_t *zdm;
+       size_t size;
+       char *buf;
+       char *nl;
+       int i;
 
-       va_start(adx, fmt);
-       size = vsnprintf(NULL, 0, fmt, adx);
-       va_end(adx);
+       size = 1024;
+       buf = kmem_alloc(size, KM_SLEEP);
 
        /*
-        * There is one byte of string in sizeof (zfs_dbgmsg_t), used
-        * for the terminating null.
+        * Get rid of annoying prefix to filename.
         */
-       zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
-       zdm->zdm_timestamp = gethrestime_sec();
-
-       va_start(adx, fmt);
-       (void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
-       va_end(adx);
+       newfile = strrchr(file, '/');
+       if (newfile != NULL) {
+               newfile = newfile + 1; /* Get rid of leading / */
+       } else {
+               newfile = file;
+       }
 
-       DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
+       i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
 
-       mutex_enter(&zfs_dbgmsgs_lock);
-       list_insert_tail(&zfs_dbgmsgs, zdm);
-       zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
-       while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
-               zdm = list_remove_head(&zfs_dbgmsgs);
-               size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
-               kmem_free(zdm, size);
-               zfs_dbgmsg_size -= size;
+       if (i < size) {
+               va_start(adx, fmt);
+               (void) vsnprintf(buf + i, size - i, fmt, adx);
+               va_end(adx);
        }
-       mutex_exit(&zfs_dbgmsgs_lock);
+
+       /*
+        * Get rid of trailing newline.
+        */
+       nl = strrchr(buf, '\n');
+       if (nl != NULL)
+               *nl = '\0';
+
+       /*
+        * To get this data enable the zfs__dprintf trace point as shown:
+        *
+        * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
+        * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
+        * $ echo 0 > /sys/kernel/debug/tracing/trace
+        *
+        * # Dump the ring buffer.
+        * $ cat /sys/kernel/debug/tracing/trace
+        */
+       DTRACE_PROBE1(zfs__dprintf, char *, buf);
+
+       /*
+        * To get this data:
+        *
+        * $ cat /proc/spl/kstat/zfs/dbgmsg
+        *
+        * To clear the buffer:
+        * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
+        */
+       __zfs_dbgmsg(buf);
+
+       kmem_free(buf, size);
 }
 
+#else
+
 void
 zfs_dbgmsg_print(const char *tag)
 {
-       zfs_dbgmsg_t *zdm;
-
        (void) printf("ZFS_DBGMSG(%s):\n", tag);
-       mutex_enter(&zfs_dbgmsgs_lock);
-       for (zdm = list_head(&zfs_dbgmsgs); zdm;
-           zdm = list_next(&zfs_dbgmsgs, zdm))
+       mutex_enter(&zfs_dbgmsgs.pl_lock);
+       for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs.pl_list); zdm != NULL;
+           zdm = list_next(&zfs_dbgmsgs.pl_list, zdm))
                (void) printf("%s\n", zdm->zdm_msg);
-       mutex_exit(&zfs_dbgmsgs_lock);
+       mutex_exit(&zfs_dbgmsgs.pl_lock);
 }
-#endif
+#endif /* _KERNEL */
 
-#if defined(_KERNEL)
-module_param(zfs_flags, int, 0644);
-MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
+#ifdef _KERNEL
+module_param(zfs_dbgmsg_enable, int, 0644);
+MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log");
 
-module_param(zfs_recover, int, 0644);
-MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
-#endif /* _KERNEL */
+module_param(zfs_dbgmsg_maxsize, int, 0644);
+MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size");
+#endif