Fix regression in dmu_buf_will_fill()
[zfs.git] / module / os / freebsd / zfs / zfs_debug.c
blob6d4ae435586712dfd35054bbb9a897f024dadc93
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/kstat.h>
29 typedef struct zfs_dbgmsg {
30 list_node_t zdm_node;
31 time_t zdm_timestamp;
32 uint_t zdm_size;
33 char zdm_msg[];
34 } zfs_dbgmsg_t;
36 static list_t zfs_dbgmsgs;
37 static uint_t zfs_dbgmsg_size = 0;
38 static kmutex_t zfs_dbgmsgs_lock;
39 uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
40 static kstat_t *zfs_dbgmsg_kstat;
43 * Internal ZFS debug messages are enabled by default.
45 * # Print debug messages as they're logged
46 * dtrace -n 'zfs-dbgmsg { print(stringof(arg0)); }'
48 * # Print all logged dbgmsg entries
49 * sysctl kstat.zfs.misc.dbgmsg
51 * # Disable the kernel debug message log.
52 * sysctl vfs.zfs.dbgmsg_enable=0
54 int zfs_dbgmsg_enable = B_TRUE;
56 static int
57 zfs_dbgmsg_headers(char *buf, size_t size)
59 (void) snprintf(buf, size, "%-12s %-8s\n", "timestamp", "message");
61 return (0);
64 static int
65 zfs_dbgmsg_data(char *buf, size_t size, void *data)
67 zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)data;
69 (void) snprintf(buf, size, "%-12llu %-s\n",
70 (u_longlong_t)zdm->zdm_timestamp, zdm->zdm_msg);
72 return (0);
75 static void *
76 zfs_dbgmsg_addr(kstat_t *ksp, loff_t n)
78 zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)ksp->ks_private;
80 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock));
82 if (n == 0)
83 ksp->ks_private = list_head(&zfs_dbgmsgs);
84 else if (zdm)
85 ksp->ks_private = list_next(&zfs_dbgmsgs, zdm);
87 return (ksp->ks_private);
90 static void
91 zfs_dbgmsg_purge(uint_t max_size)
93 zfs_dbgmsg_t *zdm;
94 uint_t size;
96 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock));
98 while (zfs_dbgmsg_size > max_size) {
99 zdm = list_remove_head(&zfs_dbgmsgs);
100 if (zdm == NULL)
101 return;
103 size = zdm->zdm_size;
104 kmem_free(zdm, size);
105 zfs_dbgmsg_size -= size;
109 static int
110 zfs_dbgmsg_update(kstat_t *ksp, int rw)
112 if (rw == KSTAT_WRITE)
113 zfs_dbgmsg_purge(0);
115 return (0);
118 void
119 zfs_dbgmsg_init(void)
121 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
122 offsetof(zfs_dbgmsg_t, zdm_node));
123 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
125 zfs_dbgmsg_kstat = kstat_create("zfs", 0, "dbgmsg", "misc",
126 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
127 if (zfs_dbgmsg_kstat) {
128 zfs_dbgmsg_kstat->ks_lock = &zfs_dbgmsgs_lock;
129 zfs_dbgmsg_kstat->ks_ndata = UINT32_MAX;
130 zfs_dbgmsg_kstat->ks_private = NULL;
131 zfs_dbgmsg_kstat->ks_update = zfs_dbgmsg_update;
132 kstat_set_raw_ops(zfs_dbgmsg_kstat, zfs_dbgmsg_headers,
133 zfs_dbgmsg_data, zfs_dbgmsg_addr);
134 kstat_install(zfs_dbgmsg_kstat);
138 void
139 zfs_dbgmsg_fini(void)
141 if (zfs_dbgmsg_kstat)
142 kstat_delete(zfs_dbgmsg_kstat);
144 mutex_enter(&zfs_dbgmsgs_lock);
145 zfs_dbgmsg_purge(0);
146 mutex_exit(&zfs_dbgmsgs_lock);
147 mutex_destroy(&zfs_dbgmsgs_lock);
150 void
151 __zfs_dbgmsg(char *buf)
153 zfs_dbgmsg_t *zdm;
154 uint_t size;
156 DTRACE_PROBE1(zfs__dbgmsg, char *, buf);
158 size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
159 zdm = kmem_zalloc(size, KM_SLEEP);
160 zdm->zdm_size = size;
161 zdm->zdm_timestamp = gethrestime_sec();
162 strcpy(zdm->zdm_msg, buf);
164 mutex_enter(&zfs_dbgmsgs_lock);
165 list_insert_tail(&zfs_dbgmsgs, zdm);
166 zfs_dbgmsg_size += size;
167 zfs_dbgmsg_purge(zfs_dbgmsg_maxsize);
168 mutex_exit(&zfs_dbgmsgs_lock);
171 void
172 __set_error(const char *file, const char *func, int line, int err)
175 * To enable this:
177 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
179 if (zfs_flags & ZFS_DEBUG_SET_ERROR)
180 __dprintf(B_FALSE, file, func, line, "error %lu", (ulong_t)err);
183 void
184 __dprintf(boolean_t dprint, const char *file, const char *func,
185 int line, const char *fmt, ...)
187 const char *newfile;
188 va_list adx;
189 size_t size;
190 char *buf;
191 char *nl;
192 int i;
194 size = 1024;
195 buf = kmem_alloc(size, KM_SLEEP);
198 * Get rid of annoying prefix to filename.
200 newfile = strrchr(file, '/');
201 if (newfile != NULL) {
202 newfile = newfile + 1; /* Get rid of leading / */
203 } else {
204 newfile = file;
207 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
209 if (i < size) {
210 va_start(adx, fmt);
211 (void) vsnprintf(buf + i, size - i, fmt, adx);
212 va_end(adx);
216 * Get rid of trailing newline for dprintf logs.
218 if (dprint && buf[0] != '\0') {
219 nl = &buf[strlen(buf) - 1];
220 if (*nl == '\n')
221 *nl = '\0';
225 * To get this data:
227 * $ sysctl -n kstat.zfs.misc.dbgmsg
229 __zfs_dbgmsg(buf);
231 kmem_free(buf, size);
234 ZFS_MODULE_PARAM(zfs, zfs_, dbgmsg_enable, INT, ZMOD_RW,
235 "Enable ZFS debug message log");
237 ZFS_MODULE_PARAM(zfs, zfs_, dbgmsg_maxsize, UINT, ZMOD_RW,
238 "Maximum ZFS debug log size");