]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_debug.c
Illumos #3956, #3957, #3958, #3959, #3960, #3961, #3962
[mirror_zfs.git] / module / zfs / zfs_debug.c
CommitLineData
428870ff
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5d1f7fb6 23 * Copyright (c) 2013 by Delphix. All rights reserved.
428870ff
BB
24 */
25
26#include <sys/zfs_context.h>
27
495b25a9
RY
28#if !defined(_KERNEL) || !defined(__linux__)
29list_t zfs_dbgmsgs;
30int zfs_dbgmsg_size;
31kmutex_t zfs_dbgmsgs_lock;
32int zfs_dbgmsg_maxsize = 1<<20; /* 1MB */
33#endif
34
d7e398ce
BB
35/*
36 * Enable various debugging features.
37 */
38int zfs_flags = 0;
39
40/*
41 * zfs_recover can be set to nonzero to attempt to recover from
42 * otherwise-fatal errors, typically caused by on-disk corruption. When
43 * set, calls to zfs_panic_recover() will turn into warning messages.
44 */
45int zfs_recover = 0;
428870ff 46
428870ff
BB
47
48void
d7e398ce 49zfs_panic_recover(const char *fmt, ...)
428870ff 50{
d7e398ce 51 va_list adx;
428870ff 52
d7e398ce
BB
53 va_start(adx, fmt);
54 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
55 va_end(adx);
428870ff
BB
56}
57
58/*
d7e398ce
BB
59 * Debug logging is enabled by default for production kernel builds.
60 * The overhead for this is negligible and the logs can be valuable when
61 * debugging. For non-production user space builds all debugging except
62 * logging is enabled since performance is no longer a concern.
428870ff
BB
63 */
64void
d7e398ce 65zfs_dbgmsg_init(void)
428870ff 66{
495b25a9
RY
67#if !defined(_KERNEL) || !defined(__linux__)
68 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
69 offsetof(zfs_dbgmsg_t, zdm_node));
70 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
71#endif
72
d7e398ce
BB
73 if (zfs_flags == 0) {
74#if defined(_KERNEL)
75 zfs_flags = ZFS_DEBUG_DPRINTF;
a31acb46
BB
76 spl_debug_set_mask(spl_debug_get_mask() | SD_DPRINTF);
77 spl_debug_set_subsys(spl_debug_get_subsys() | SS_USER1);
d7e398ce
BB
78#else
79 zfs_flags = ~ZFS_DEBUG_DPRINTF;
80#endif /* _KERNEL */
81 }
82}
428870ff 83
d7e398ce
BB
84void
85zfs_dbgmsg_fini(void)
86{
495b25a9
RY
87#if !defined(_KERNEL) || !defined(__linux__)
88 zfs_dbgmsg_t *zdm;
89
90 while ((zdm = list_remove_head(&zfs_dbgmsgs)) != NULL) {
91 int size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
92 kmem_free(zdm, size);
93 zfs_dbgmsg_size -= size;
94 }
95 mutex_destroy(&zfs_dbgmsgs_lock);
96 ASSERT0(zfs_dbgmsg_size);
97#endif
d7e398ce
BB
98 return;
99}
428870ff 100
495b25a9
RY
101#if !defined(_KERNEL) || !defined(__linux__)
102/*
103 * Print these messages by running:
104 * echo ::zfs_dbgmsg | mdb -k
105 *
106 * Monitor these messages by running:
107 * dtrace -q -n 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
108 */
109void
110zfs_dbgmsg(const char *fmt, ...)
111{
112 int size;
113 va_list adx;
114 zfs_dbgmsg_t *zdm;
115
116 va_start(adx, fmt);
117 size = vsnprintf(NULL, 0, fmt, adx);
118 va_end(adx);
119
120 /*
121 * There is one byte of string in sizeof (zfs_dbgmsg_t), used
122 * for the terminating null.
123 */
124 zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
125 zdm->zdm_timestamp = gethrestime_sec();
126
127 va_start(adx, fmt);
128 (void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
129 va_end(adx);
130
131 DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
132
133 mutex_enter(&zfs_dbgmsgs_lock);
134 list_insert_tail(&zfs_dbgmsgs, zdm);
135 zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
136 while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
137 zdm = list_remove_head(&zfs_dbgmsgs);
138 size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
139 kmem_free(zdm, size);
140 zfs_dbgmsg_size -= size;
141 }
142 mutex_exit(&zfs_dbgmsgs_lock);
143}
5d1f7fb6
GW
144
145void
146zfs_dbgmsg_print(const char *tag)
147{
148 zfs_dbgmsg_t *zdm;
149
150 (void) printf("ZFS_DBGMSG(%s):\n", tag);
151 mutex_enter(&zfs_dbgmsgs_lock);
152 for (zdm = list_head(&zfs_dbgmsgs); zdm;
153 zdm = list_next(&zfs_dbgmsgs, zdm))
154 (void) printf("%s\n", zdm->zdm_msg);
155 mutex_exit(&zfs_dbgmsgs_lock);
156}
495b25a9 157#endif
428870ff 158
d7e398ce
BB
159#if defined(_KERNEL)
160module_param(zfs_flags, int, 0644);
161MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
428870ff 162
d7e398ce
BB
163module_param(zfs_recover, int, 0644);
164MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
165#endif /* _KERNEL */