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