xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zfs_debug.c (revision 179219ea046f46927d6478d43431e8b541703539)
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.
23  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/trace_zfs.h>
28 
29 typedef struct zfs_dbgmsg {
30 	procfs_list_node_t	zdm_node;
31 	uint64_t		zdm_timestamp;
32 	int			zdm_size;
33 	char			zdm_msg[1]; /* variable length allocation */
34 } zfs_dbgmsg_t;
35 
36 procfs_list_t zfs_dbgmsgs;
37 int zfs_dbgmsg_size = 0;
38 int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
39 
40 /*
41  * Internal ZFS debug messages are enabled by default.
42  *
43  * # Print debug messages
44  * cat /proc/spl/kstat/zfs/dbgmsg
45  *
46  * # Disable the kernel debug message log.
47  * echo 0 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
48  *
49  * # Clear the kernel debug message log.
50  * echo 0 >/proc/spl/kstat/zfs/dbgmsg
51  */
52 int zfs_dbgmsg_enable = 1;
53 
54 static int
55 zfs_dbgmsg_show_header(struct seq_file *f)
56 {
57 	seq_printf(f, "%-12s %-8s\n", "timestamp", "message");
58 	return (0);
59 }
60 
61 static int
62 zfs_dbgmsg_show(struct seq_file *f, void *p)
63 {
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);
67 	return (0);
68 }
69 
70 static void
71 zfs_dbgmsg_purge(int max_size)
72 {
73 	while (zfs_dbgmsg_size > max_size) {
74 		zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs.pl_list);
75 		if (zdm == NULL)
76 			return;
77 
78 		int size = zdm->zdm_size;
79 		kmem_free(zdm, size);
80 		zfs_dbgmsg_size -= size;
81 	}
82 }
83 
84 static int
85 zfs_dbgmsg_clear(procfs_list_t *procfs_list)
86 {
87 	mutex_enter(&zfs_dbgmsgs.pl_lock);
88 	zfs_dbgmsg_purge(0);
89 	mutex_exit(&zfs_dbgmsgs.pl_lock);
90 	return (0);
91 }
92 
93 void
94 zfs_dbgmsg_init(void)
95 {
96 	procfs_list_install("zfs",
97 	    NULL,
98 	    "dbgmsg",
99 	    0600,
100 	    &zfs_dbgmsgs,
101 	    zfs_dbgmsg_show,
102 	    zfs_dbgmsg_show_header,
103 	    zfs_dbgmsg_clear,
104 	    offsetof(zfs_dbgmsg_t, zdm_node));
105 }
106 
107 void
108 zfs_dbgmsg_fini(void)
109 {
110 	procfs_list_uninstall(&zfs_dbgmsgs);
111 	zfs_dbgmsg_purge(0);
112 
113 	/*
114 	 * TODO - decide how to make this permanent
115 	 */
116 #ifdef _KERNEL
117 	procfs_list_destroy(&zfs_dbgmsgs);
118 #endif
119 }
120 
121 void
122 __set_error(const char *file, const char *func, int line, int err)
123 {
124 	/*
125 	 * To enable this:
126 	 *
127 	 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
128 	 */
129 	if (zfs_flags & ZFS_DEBUG_SET_ERROR)
130 		__dprintf(B_FALSE, file, func, line, "error %lu",
131 		    (ulong_t)err);
132 }
133 
134 void
135 __zfs_dbgmsg(char *buf)
136 {
137 	int size = sizeof (zfs_dbgmsg_t) + strlen(buf);
138 	zfs_dbgmsg_t *zdm = kmem_zalloc(size, KM_SLEEP);
139 	zdm->zdm_size = size;
140 	zdm->zdm_timestamp = gethrestime_sec();
141 	strcpy(zdm->zdm_msg, buf);
142 
143 	mutex_enter(&zfs_dbgmsgs.pl_lock);
144 	procfs_list_add(&zfs_dbgmsgs, zdm);
145 	zfs_dbgmsg_size += size;
146 	zfs_dbgmsg_purge(MAX(zfs_dbgmsg_maxsize, 0));
147 	mutex_exit(&zfs_dbgmsgs.pl_lock);
148 }
149 
150 #ifdef _KERNEL
151 
152 void
153 __dprintf(boolean_t dprint, const char *file, const char *func,
154     int line, const char *fmt, ...)
155 {
156 	const char *newfile;
157 	va_list adx;
158 	size_t size;
159 	char *buf;
160 	char *nl;
161 	int i;
162 	char *prefix = (dprint) ? "dprintf: " : "";
163 
164 	size = 1024;
165 	buf = kmem_alloc(size, KM_SLEEP);
166 
167 	/*
168 	 * Get rid of annoying prefix to filename.
169 	 */
170 	newfile = strrchr(file, '/');
171 	if (newfile != NULL) {
172 		newfile = newfile + 1; /* Get rid of leading / */
173 	} else {
174 		newfile = file;
175 	}
176 
177 	i = snprintf(buf, size, "%s%s:%d:%s(): ", prefix, newfile, line, func);
178 
179 	if (i < size) {
180 		va_start(adx, fmt);
181 		(void) vsnprintf(buf + i, size - i, fmt, adx);
182 		va_end(adx);
183 	}
184 
185 	/*
186 	 * Get rid of trailing newline for dprintf logs.
187 	 */
188 	if (dprint && buf[0] != '\0') {
189 		nl = &buf[strlen(buf) - 1];
190 		if (*nl == '\n')
191 			*nl = '\0';
192 	}
193 
194 	/*
195 	 * To get this data enable the zfs__dprintf trace point as shown:
196 	 *
197 	 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
198 	 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
199 	 * $ echo 0 > /sys/kernel/debug/tracing/trace
200 	 *
201 	 * # Dump the ring buffer.
202 	 * $ cat /sys/kernel/debug/tracing/trace
203 	 */
204 	DTRACE_PROBE1(zfs__dprintf, char *, buf);
205 
206 	/*
207 	 * To get this data:
208 	 *
209 	 * $ cat /proc/spl/kstat/zfs/dbgmsg
210 	 *
211 	 * To clear the buffer:
212 	 * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
213 	 */
214 	__zfs_dbgmsg(buf);
215 
216 	kmem_free(buf, size);
217 }
218 
219 #else
220 
221 void
222 zfs_dbgmsg_print(const char *tag)
223 {
224 	ssize_t ret __attribute__((unused));
225 
226 	/*
227 	 * We use write() in this function instead of printf()
228 	 * so it is safe to call from a signal handler.
229 	 */
230 	ret = write(STDOUT_FILENO, "ZFS_DBGMSG(", 11);
231 	ret = write(STDOUT_FILENO, tag, strlen(tag));
232 	ret = write(STDOUT_FILENO, ") START:\n", 9);
233 
234 	mutex_enter(&zfs_dbgmsgs.pl_lock);
235 	for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs.pl_list); zdm != NULL;
236 	    zdm = list_next(&zfs_dbgmsgs.pl_list, zdm)) {
237 		ret = write(STDOUT_FILENO, zdm->zdm_msg,
238 		    strlen(zdm->zdm_msg));
239 		ret = write(STDOUT_FILENO, "\n", 1);
240 	}
241 
242 	ret = write(STDOUT_FILENO, "ZFS_DBGMSG(", 11);
243 	ret = write(STDOUT_FILENO, tag, strlen(tag));
244 	ret = write(STDOUT_FILENO, ") END\n", 6);
245 
246 	mutex_exit(&zfs_dbgmsgs.pl_lock);
247 }
248 #endif /* _KERNEL */
249 
250 #ifdef _KERNEL
251 module_param(zfs_dbgmsg_enable, int, 0644);
252 MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log");
253 
254 module_param(zfs_dbgmsg_maxsize, int, 0644);
255 MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size");
256 #endif
257