]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_debug.c
Fixes for procfs files backed by linked lists
[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 &zfs_dbgmsgs,
98 zfs_dbgmsg_show,
99 zfs_dbgmsg_show_header,
100 zfs_dbgmsg_clear,
101 offsetof(zfs_dbgmsg_t, zdm_node));
102 }
103
104 void
105 zfs_dbgmsg_fini(void)
106 {
107 procfs_list_uninstall(&zfs_dbgmsgs);
108 zfs_dbgmsg_purge(0);
109
110 /*
111 * TODO - decide how to make this permanent
112 */
113 #ifdef _KERNEL
114 procfs_list_destroy(&zfs_dbgmsgs);
115 #endif
116 }
117
118 void
119 __set_error(const char *file, const char *func, int line, int err)
120 {
121 /*
122 * To enable this:
123 *
124 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
125 */
126 if (zfs_flags & ZFS_DEBUG_SET_ERROR)
127 __dprintf(file, func, line, "error %lu", err);
128 }
129
130 #ifdef _KERNEL
131 static 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 void
148 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
149 {
150 const char *newfile;
151 va_list adx;
152 size_t size;
153 char *buf;
154 char *nl;
155 int i;
156
157 size = 1024;
158 buf = kmem_alloc(size, KM_SLEEP);
159
160 /*
161 * Get rid of annoying prefix to filename.
162 */
163 newfile = strrchr(file, '/');
164 if (newfile != NULL) {
165 newfile = newfile + 1; /* Get rid of leading / */
166 } else {
167 newfile = file;
168 }
169
170 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
171
172 if (i < size) {
173 va_start(adx, fmt);
174 (void) vsnprintf(buf + i, size - i, fmt, adx);
175 va_end(adx);
176 }
177
178 /*
179 * Get rid of trailing newline.
180 */
181 nl = strrchr(buf, '\n');
182 if (nl != NULL)
183 *nl = '\0';
184
185 /*
186 * To get this data enable the zfs__dprintf trace point as shown:
187 *
188 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
189 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
190 * $ echo 0 > /sys/kernel/debug/tracing/trace
191 *
192 * # Dump the ring buffer.
193 * $ cat /sys/kernel/debug/tracing/trace
194 */
195 DTRACE_PROBE1(zfs__dprintf, char *, buf);
196
197 /*
198 * To get this data:
199 *
200 * $ cat /proc/spl/kstat/zfs/dbgmsg
201 *
202 * To clear the buffer:
203 * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
204 */
205 __zfs_dbgmsg(buf);
206
207 kmem_free(buf, size);
208 }
209
210 #else
211
212 void
213 zfs_dbgmsg_print(const char *tag)
214 {
215 (void) printf("ZFS_DBGMSG(%s):\n", tag);
216 mutex_enter(&zfs_dbgmsgs.pl_lock);
217 for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs.pl_list); zdm != NULL;
218 zdm = list_next(&zfs_dbgmsgs.pl_list, zdm))
219 (void) printf("%s\n", zdm->zdm_msg);
220 mutex_exit(&zfs_dbgmsgs.pl_lock);
221 }
222 #endif /* _KERNEL */
223
224 #ifdef _KERNEL
225 module_param(zfs_dbgmsg_enable, int, 0644);
226 MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log");
227
228 module_param(zfs_dbgmsg_maxsize, int, 0644);
229 MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size");
230 #endif