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]
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
{
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
;
57 zfs_dbgmsg_headers(char *buf
, size_t size
)
59 (void) snprintf(buf
, size
, "%-12s %-8s\n", "timestamp", "message");
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
);
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
));
83 ksp
->ks_private
= list_head(&zfs_dbgmsgs
);
85 ksp
->ks_private
= list_next(&zfs_dbgmsgs
, zdm
);
87 return (ksp
->ks_private
);
91 zfs_dbgmsg_purge(uint_t max_size
)
96 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock
));
98 while (zfs_dbgmsg_size
> max_size
) {
99 zdm
= list_remove_head(&zfs_dbgmsgs
);
103 size
= zdm
->zdm_size
;
104 kmem_free(zdm
, size
);
105 zfs_dbgmsg_size
-= size
;
110 zfs_dbgmsg_update(kstat_t
*ksp
, int rw
)
112 if (rw
== KSTAT_WRITE
)
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
);
139 zfs_dbgmsg_fini(void)
141 if (zfs_dbgmsg_kstat
)
142 kstat_delete(zfs_dbgmsg_kstat
);
144 mutex_enter(&zfs_dbgmsgs_lock
);
146 mutex_exit(&zfs_dbgmsgs_lock
);
147 mutex_destroy(&zfs_dbgmsgs_lock
);
151 __zfs_dbgmsg(char *buf
)
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
);
172 __set_error(const char *file
, const char *func
, int line
, int err
)
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
);
184 __dprintf(boolean_t dprint
, const char *file
, const char *func
,
185 int line
, const char *fmt
, ...)
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 / */
207 i
= snprintf(buf
, size
, "%s:%d:%s(): ", newfile
, line
, func
);
211 (void) vsnprintf(buf
+ i
, size
- i
, fmt
, adx
);
216 * Get rid of trailing newline for dprintf logs.
218 if (dprint
&& buf
[0] != '\0') {
219 nl
= &buf
[strlen(buf
) - 1];
227 * $ sysctl -n kstat.zfs.misc.dbgmsg
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");