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/trace_zfs.h>
29 typedef struct zfs_dbgmsg
{
30 procfs_list_node_t zdm_node
;
31 uint64_t zdm_timestamp
;
33 char zdm_msg
[1]; /* variable length allocation */
36 static procfs_list_t zfs_dbgmsgs
;
37 static int zfs_dbgmsg_size
= 0;
38 int zfs_dbgmsg_maxsize
= 4<<20; /* 4MB */
41 * Internal ZFS debug messages are enabled by default.
43 * # Print debug messages
44 * cat /proc/spl/kstat/zfs/dbgmsg
46 * # Disable the kernel debug message log.
47 * echo 0 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
49 * # Clear the kernel debug message log.
50 * echo 0 >/proc/spl/kstat/zfs/dbgmsg
52 int zfs_dbgmsg_enable
= B_TRUE
;
55 zfs_dbgmsg_show_header(struct seq_file
*f
)
57 seq_printf(f
, "%-12s %-8s\n", "timestamp", "message");
62 zfs_dbgmsg_show(struct seq_file
*f
, void *p
)
64 zfs_dbgmsg_t
*zdm
= (zfs_dbgmsg_t
*)p
;
65 seq_printf(f
, "%-12llu %-s\n",
66 (u_longlong_t
)zdm
->zdm_timestamp
, zdm
->zdm_msg
);
71 zfs_dbgmsg_purge(int max_size
)
73 while (zfs_dbgmsg_size
> max_size
) {
74 zfs_dbgmsg_t
*zdm
= list_remove_head(&zfs_dbgmsgs
.pl_list
);
78 int size
= zdm
->zdm_size
;
80 zfs_dbgmsg_size
-= size
;
85 zfs_dbgmsg_clear(procfs_list_t
*procfs_list
)
88 mutex_enter(&zfs_dbgmsgs
.pl_lock
);
90 mutex_exit(&zfs_dbgmsgs
.pl_lock
);
97 procfs_list_install("zfs",
103 zfs_dbgmsg_show_header
,
105 offsetof(zfs_dbgmsg_t
, zdm_node
));
109 zfs_dbgmsg_fini(void)
111 procfs_list_uninstall(&zfs_dbgmsgs
);
115 * TODO - decide how to make this permanent
118 procfs_list_destroy(&zfs_dbgmsgs
);
123 __set_error(const char *file
, const char *func
, int line
, int err
)
128 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
130 if (zfs_flags
& ZFS_DEBUG_SET_ERROR
)
131 __dprintf(B_FALSE
, file
, func
, line
, "error %lu",
136 __zfs_dbgmsg(char *buf
)
138 int size
= sizeof (zfs_dbgmsg_t
) + strlen(buf
);
139 zfs_dbgmsg_t
*zdm
= kmem_zalloc(size
, KM_SLEEP
);
140 zdm
->zdm_size
= size
;
141 zdm
->zdm_timestamp
= gethrestime_sec();
142 strcpy(zdm
->zdm_msg
, buf
);
144 mutex_enter(&zfs_dbgmsgs
.pl_lock
);
145 procfs_list_add(&zfs_dbgmsgs
, zdm
);
146 zfs_dbgmsg_size
+= size
;
147 zfs_dbgmsg_purge(MAX(zfs_dbgmsg_maxsize
, 0));
148 mutex_exit(&zfs_dbgmsgs
.pl_lock
);
154 __dprintf(boolean_t dprint
, const char *file
, const char *func
,
155 int line
, const char *fmt
, ...)
163 char *prefix
= (dprint
) ? "dprintf: " : "";
166 buf
= kmem_alloc(size
, KM_SLEEP
);
169 * Get rid of annoying prefix to filename.
171 newfile
= strrchr(file
, '/');
172 if (newfile
!= NULL
) {
173 newfile
= newfile
+ 1; /* Get rid of leading / */
178 i
= snprintf(buf
, size
, "%s%s:%d:%s(): ", prefix
, newfile
, line
, func
);
182 (void) vsnprintf(buf
+ i
, size
- i
, fmt
, adx
);
187 * Get rid of trailing newline for dprintf logs.
189 if (dprint
&& buf
[0] != '\0') {
190 nl
= &buf
[strlen(buf
) - 1];
196 * To get this data enable the zfs__dprintf trace point as shown:
198 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
199 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
200 * $ echo 0 > /sys/kernel/debug/tracing/trace
202 * # Dump the ring buffer.
203 * $ cat /sys/kernel/debug/tracing/trace
205 DTRACE_PROBE1(zfs__dprintf
, char *, buf
);
210 * $ cat /proc/spl/kstat/zfs/dbgmsg
212 * To clear the buffer:
213 * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
217 kmem_free(buf
, size
);
223 zfs_dbgmsg_print(const char *tag
)
225 ssize_t ret
__attribute__((unused
));
228 * We use write() in this function instead of printf()
229 * so it is safe to call from a signal handler.
231 ret
= write(STDOUT_FILENO
, "ZFS_DBGMSG(", 11);
232 ret
= write(STDOUT_FILENO
, tag
, strlen(tag
));
233 ret
= write(STDOUT_FILENO
, ") START:\n", 9);
235 mutex_enter(&zfs_dbgmsgs
.pl_lock
);
236 for (zfs_dbgmsg_t
*zdm
= list_head(&zfs_dbgmsgs
.pl_list
); zdm
!= NULL
;
237 zdm
= list_next(&zfs_dbgmsgs
.pl_list
, zdm
)) {
238 ret
= write(STDOUT_FILENO
, zdm
->zdm_msg
,
239 strlen(zdm
->zdm_msg
));
240 ret
= write(STDOUT_FILENO
, "\n", 1);
243 ret
= write(STDOUT_FILENO
, "ZFS_DBGMSG(", 11);
244 ret
= write(STDOUT_FILENO
, tag
, strlen(tag
));
245 ret
= write(STDOUT_FILENO
, ") END\n", 6);
247 mutex_exit(&zfs_dbgmsgs
.pl_lock
);
252 module_param(zfs_dbgmsg_enable
, int, 0644);
253 MODULE_PARM_DESC(zfs_dbgmsg_enable
, "Enable ZFS debug message log");
255 module_param(zfs_dbgmsg_maxsize
, int, 0644);
256 MODULE_PARM_DESC(zfs_dbgmsg_maxsize
, "Maximum ZFS debug log size");