]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_debug.c
Illumos 4757, 4913
[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.
78e2739d
MA
44 * This should only be used as a last resort, as it typically results
45 * in leaked space, or worse.
d7e398ce
BB
46 */
47int zfs_recover = 0;
428870ff 48
428870ff
BB
49
50void
d7e398ce 51zfs_panic_recover(const char *fmt, ...)
428870ff 52{
d7e398ce 53 va_list adx;
428870ff 54
d7e398ce
BB
55 va_start(adx, fmt);
56 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
57 va_end(adx);
428870ff
BB
58}
59
60/*
d7e398ce
BB
61 * Debug logging is enabled by default for production kernel builds.
62 * The overhead for this is negligible and the logs can be valuable when
63 * debugging. For non-production user space builds all debugging except
64 * logging is enabled since performance is no longer a concern.
428870ff
BB
65 */
66void
d7e398ce 67zfs_dbgmsg_init(void)
428870ff 68{
495b25a9
RY
69#if !defined(_KERNEL) || !defined(__linux__)
70 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
71 offsetof(zfs_dbgmsg_t, zdm_node));
72 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
73#endif
74
d7e398ce
BB
75 if (zfs_flags == 0) {
76#if defined(_KERNEL)
77 zfs_flags = ZFS_DEBUG_DPRINTF;
a31acb46
BB
78 spl_debug_set_mask(spl_debug_get_mask() | SD_DPRINTF);
79 spl_debug_set_subsys(spl_debug_get_subsys() | SS_USER1);
d7e398ce
BB
80#else
81 zfs_flags = ~ZFS_DEBUG_DPRINTF;
82#endif /* _KERNEL */
83 }
84}
428870ff 85
d7e398ce
BB
86void
87zfs_dbgmsg_fini(void)
88{
495b25a9
RY
89#if !defined(_KERNEL) || !defined(__linux__)
90 zfs_dbgmsg_t *zdm;
91
92 while ((zdm = list_remove_head(&zfs_dbgmsgs)) != NULL) {
93 int size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
94 kmem_free(zdm, size);
95 zfs_dbgmsg_size -= size;
96 }
97 mutex_destroy(&zfs_dbgmsgs_lock);
98 ASSERT0(zfs_dbgmsg_size);
99#endif
d7e398ce 100}
428870ff 101
495b25a9
RY
102#if !defined(_KERNEL) || !defined(__linux__)
103/*
104 * Print these messages by running:
105 * echo ::zfs_dbgmsg | mdb -k
106 *
107 * Monitor these messages by running:
108 * dtrace -q -n 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
109 */
110void
111zfs_dbgmsg(const char *fmt, ...)
112{
113 int size;
114 va_list adx;
115 zfs_dbgmsg_t *zdm;
116
117 va_start(adx, fmt);
118 size = vsnprintf(NULL, 0, fmt, adx);
119 va_end(adx);
120
121 /*
122 * There is one byte of string in sizeof (zfs_dbgmsg_t), used
123 * for the terminating null.
124 */
125 zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
126 zdm->zdm_timestamp = gethrestime_sec();
127
128 va_start(adx, fmt);
129 (void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
130 va_end(adx);
131
132 DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
133
134 mutex_enter(&zfs_dbgmsgs_lock);
135 list_insert_tail(&zfs_dbgmsgs, zdm);
136 zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
137 while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
138 zdm = list_remove_head(&zfs_dbgmsgs);
139 size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
140 kmem_free(zdm, size);
141 zfs_dbgmsg_size -= size;
142 }
143 mutex_exit(&zfs_dbgmsgs_lock);
144}
5d1f7fb6
GW
145
146void
147zfs_dbgmsg_print(const char *tag)
148{
149 zfs_dbgmsg_t *zdm;
150
151 (void) printf("ZFS_DBGMSG(%s):\n", tag);
152 mutex_enter(&zfs_dbgmsgs_lock);
153 for (zdm = list_head(&zfs_dbgmsgs); zdm;
154 zdm = list_next(&zfs_dbgmsgs, zdm))
155 (void) printf("%s\n", zdm->zdm_msg);
156 mutex_exit(&zfs_dbgmsgs_lock);
157}
495b25a9 158#endif
428870ff 159
d7e398ce
BB
160#if defined(_KERNEL)
161module_param(zfs_flags, int, 0644);
162MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
428870ff 163
d7e398ce
BB
164module_param(zfs_recover, int, 0644);
165MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
166#endif /* _KERNEL */