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 http://www.opensolaris.org/os/licensing.
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]
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>
30 int zfs_dbgmsg_size
= 0;
31 kmutex_t zfs_dbgmsgs_lock
;
32 int zfs_dbgmsg_maxsize
= 4<<20; /* 4MB */
33 kstat_t
*zfs_dbgmsg_kstat
;
36 * By default only enable the internal ZFS debug messages when running
37 * in userspace (ztest). The kernel log must be manually enabled.
39 * # Enable the kernel debug message log.
40 * echo 1 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
42 * # Clear the kernel debug message log.
43 * echo 0 >/proc/spl/kstat/zfs/dbgmsg
46 int zfs_dbgmsg_enable
= 0;
48 int zfs_dbgmsg_enable
= 1;
52 zfs_dbgmsg_headers(char *buf
, size_t size
)
54 (void) snprintf(buf
, size
, "%-12s %-8s\n", "timestamp", "message");
60 zfs_dbgmsg_data(char *buf
, size_t size
, void *data
)
62 zfs_dbgmsg_t
*zdm
= (zfs_dbgmsg_t
*)data
;
64 (void) snprintf(buf
, size
, "%-12llu %-s\n",
65 (u_longlong_t
) zdm
->zdm_timestamp
, zdm
->zdm_msg
);
71 zfs_dbgmsg_addr(kstat_t
*ksp
, loff_t n
)
73 zfs_dbgmsg_t
*zdm
= (zfs_dbgmsg_t
*)ksp
->ks_private
;
75 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock
));
78 ksp
->ks_private
= list_head(&zfs_dbgmsgs
);
80 ksp
->ks_private
= list_next(&zfs_dbgmsgs
, zdm
);
82 return (ksp
->ks_private
);
86 zfs_dbgmsg_purge(int max_size
)
91 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock
));
93 while (zfs_dbgmsg_size
> max_size
) {
94 zdm
= list_remove_head(&zfs_dbgmsgs
);
100 zfs_dbgmsg_size
-= size
;
105 zfs_dbgmsg_update(kstat_t
*ksp
, int rw
)
107 if (rw
== KSTAT_WRITE
)
114 zfs_dbgmsg_init(void)
116 list_create(&zfs_dbgmsgs
, sizeof (zfs_dbgmsg_t
),
117 offsetof(zfs_dbgmsg_t
, zdm_node
));
118 mutex_init(&zfs_dbgmsgs_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
120 zfs_dbgmsg_kstat
= kstat_create("zfs", 0, "dbgmsg", "misc",
121 KSTAT_TYPE_RAW
, 0, KSTAT_FLAG_VIRTUAL
);
122 if (zfs_dbgmsg_kstat
) {
123 zfs_dbgmsg_kstat
->ks_lock
= &zfs_dbgmsgs_lock
;
124 zfs_dbgmsg_kstat
->ks_ndata
= UINT32_MAX
;
125 zfs_dbgmsg_kstat
->ks_private
= NULL
;
126 zfs_dbgmsg_kstat
->ks_update
= zfs_dbgmsg_update
;
127 kstat_set_raw_ops(zfs_dbgmsg_kstat
, zfs_dbgmsg_headers
,
128 zfs_dbgmsg_data
, zfs_dbgmsg_addr
);
129 kstat_install(zfs_dbgmsg_kstat
);
134 zfs_dbgmsg_fini(void)
136 if (zfs_dbgmsg_kstat
)
137 kstat_delete(zfs_dbgmsg_kstat
);
139 mutex_enter(&zfs_dbgmsgs_lock
);
141 mutex_exit(&zfs_dbgmsgs_lock
);
142 mutex_destroy(&zfs_dbgmsgs_lock
);
146 __zfs_dbgmsg(char *buf
)
151 size
= sizeof (zfs_dbgmsg_t
) + strlen(buf
);
152 zdm
= kmem_zalloc(size
, KM_SLEEP
);
153 zdm
->zdm_size
= size
;
154 zdm
->zdm_timestamp
= gethrestime_sec();
155 strcpy(zdm
->zdm_msg
, buf
);
157 mutex_enter(&zfs_dbgmsgs_lock
);
158 list_insert_tail(&zfs_dbgmsgs
, zdm
);
159 zfs_dbgmsg_size
+= size
;
160 zfs_dbgmsg_purge(MAX(zfs_dbgmsg_maxsize
, 0));
161 mutex_exit(&zfs_dbgmsgs_lock
);
166 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
174 if (!zfs_dbgmsg_enable
&& !(zfs_flags
& ZFS_DEBUG_DPRINTF
))
178 buf
= kmem_alloc(size
, KM_SLEEP
);
181 * Get rid of annoying prefix to filename.
183 newfile
= strrchr(file
, '/');
184 if (newfile
!= NULL
) {
185 newfile
= newfile
+ 1; /* Get rid of leading / */
191 (void) vsnprintf(buf
, size
, fmt
, adx
);
195 * Get rid of trailing newline.
197 nl
= strrchr(buf
, '\n');
202 * To get this data enable the zfs__dprintf trace point as shown:
204 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
205 * $ echo 1 > /sys/module/zfs/parameters/zfs_flags
206 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
207 * $ echo 0 > /sys/kernel/debug/tracing/trace
209 * # Dump the ring buffer.
210 * $ cat /sys/kernel/debug/tracing/trace
212 if (zfs_flags
& ZFS_DEBUG_DPRINTF
)
213 DTRACE_PROBE4(zfs__dprintf
,
214 char *, newfile
, char *, func
, int, line
, char *, buf
);
217 * To get this data enable the zfs debug log as shown:
219 * # Set zfs_dbgmsg enable, clear the log buffer
220 * $ echo 1 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
221 * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
223 * # Dump the log buffer.
224 * $ cat /proc/spl/kstat/zfs/dbgmsg
226 if (zfs_dbgmsg_enable
)
229 kmem_free(buf
, size
);
234 module_param(zfs_dbgmsg_enable
, int, 0644);
235 MODULE_PARM_DESC(zfs_dbgmsg_enable
, "Enable ZFS debug message log");
237 module_param(zfs_dbgmsg_maxsize
, int, 0644);
238 MODULE_PARM_DESC(zfs_dbgmsg_maxsize
, "Maximum ZFS debug log size");