]>
Commit | Line | Data |
---|---|---|
428870ff BB |
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. | |
b02fe35d | 23 | * Copyright (c) 2012, 2014 by Delphix. All rights reserved. |
428870ff BB |
24 | */ |
25 | ||
26 | #include <sys/zfs_context.h> | |
3b36f831 | 27 | #include <sys/kstat.h> |
428870ff | 28 | |
495b25a9 | 29 | list_t zfs_dbgmsgs; |
3b36f831 | 30 | int zfs_dbgmsg_size = 0; |
495b25a9 | 31 | kmutex_t zfs_dbgmsgs_lock; |
fbeddd60 | 32 | int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */ |
3b36f831 BB |
33 | kstat_t *zfs_dbgmsg_kstat; |
34 | ||
35 | /* | |
2fd92c3d | 36 | * Internal ZFS debug messages are enabled by default. |
3b36f831 | 37 | * |
2fd92c3d MA |
38 | * # Print debug messages |
39 | * cat /proc/spl/kstat/zfs/dbgmsg | |
40 | * | |
41 | * # Disable the kernel debug message log. | |
42 | * echo 0 > /sys/module/zfs/parameters/zfs_dbgmsg_enable | |
3b36f831 BB |
43 | * |
44 | * # Clear the kernel debug message log. | |
45 | * echo 0 >/proc/spl/kstat/zfs/dbgmsg | |
46 | */ | |
3b36f831 | 47 | int zfs_dbgmsg_enable = 1; |
3b36f831 BB |
48 | |
49 | static int | |
50 | zfs_dbgmsg_headers(char *buf, size_t size) | |
51 | { | |
52 | (void) snprintf(buf, size, "%-12s %-8s\n", "timestamp", "message"); | |
53 | ||
54 | return (0); | |
55 | } | |
56 | ||
57 | static int | |
58 | zfs_dbgmsg_data(char *buf, size_t size, void *data) | |
59 | { | |
60 | zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)data; | |
61 | ||
62 | (void) snprintf(buf, size, "%-12llu %-s\n", | |
02730c33 | 63 | (u_longlong_t)zdm->zdm_timestamp, zdm->zdm_msg); |
3b36f831 BB |
64 | |
65 | return (0); | |
66 | } | |
67 | ||
68 | static void * | |
69 | zfs_dbgmsg_addr(kstat_t *ksp, loff_t n) | |
70 | { | |
71 | zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)ksp->ks_private; | |
72 | ||
73 | ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock)); | |
74 | ||
75 | if (n == 0) | |
76 | ksp->ks_private = list_head(&zfs_dbgmsgs); | |
77 | else if (zdm) | |
78 | ksp->ks_private = list_next(&zfs_dbgmsgs, zdm); | |
79 | ||
80 | return (ksp->ks_private); | |
81 | } | |
82 | ||
83 | static void | |
84 | zfs_dbgmsg_purge(int max_size) | |
85 | { | |
86 | zfs_dbgmsg_t *zdm; | |
87 | int size; | |
88 | ||
89 | ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock)); | |
90 | ||
91 | while (zfs_dbgmsg_size > max_size) { | |
92 | zdm = list_remove_head(&zfs_dbgmsgs); | |
93 | if (zdm == NULL) | |
94 | return; | |
95 | ||
96 | size = zdm->zdm_size; | |
97 | kmem_free(zdm, size); | |
98 | zfs_dbgmsg_size -= size; | |
99 | } | |
100 | } | |
101 | ||
102 | static int | |
103 | zfs_dbgmsg_update(kstat_t *ksp, int rw) | |
104 | { | |
105 | if (rw == KSTAT_WRITE) | |
106 | zfs_dbgmsg_purge(0); | |
107 | ||
108 | return (0); | |
109 | } | |
495b25a9 | 110 | |
428870ff | 111 | void |
d7e398ce | 112 | zfs_dbgmsg_init(void) |
428870ff | 113 | { |
495b25a9 RY |
114 | list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t), |
115 | offsetof(zfs_dbgmsg_t, zdm_node)); | |
116 | mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL); | |
3b36f831 BB |
117 | |
118 | zfs_dbgmsg_kstat = kstat_create("zfs", 0, "dbgmsg", "misc", | |
119 | KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL); | |
120 | if (zfs_dbgmsg_kstat) { | |
121 | zfs_dbgmsg_kstat->ks_lock = &zfs_dbgmsgs_lock; | |
122 | zfs_dbgmsg_kstat->ks_ndata = UINT32_MAX; | |
123 | zfs_dbgmsg_kstat->ks_private = NULL; | |
124 | zfs_dbgmsg_kstat->ks_update = zfs_dbgmsg_update; | |
125 | kstat_set_raw_ops(zfs_dbgmsg_kstat, zfs_dbgmsg_headers, | |
126 | zfs_dbgmsg_data, zfs_dbgmsg_addr); | |
127 | kstat_install(zfs_dbgmsg_kstat); | |
128 | } | |
d7e398ce | 129 | } |
428870ff | 130 | |
d7e398ce BB |
131 | void |
132 | zfs_dbgmsg_fini(void) | |
133 | { | |
3b36f831 BB |
134 | if (zfs_dbgmsg_kstat) |
135 | kstat_delete(zfs_dbgmsg_kstat); | |
495b25a9 | 136 | |
3b36f831 BB |
137 | mutex_enter(&zfs_dbgmsgs_lock); |
138 | zfs_dbgmsg_purge(0); | |
139 | mutex_exit(&zfs_dbgmsgs_lock); | |
495b25a9 | 140 | mutex_destroy(&zfs_dbgmsgs_lock); |
d7e398ce | 141 | } |
428870ff | 142 | |
495b25a9 | 143 | void |
3b36f831 | 144 | __zfs_dbgmsg(char *buf) |
495b25a9 | 145 | { |
3b36f831 | 146 | zfs_dbgmsg_t *zdm; |
495b25a9 | 147 | int size; |
3b36f831 BB |
148 | |
149 | size = sizeof (zfs_dbgmsg_t) + strlen(buf); | |
150 | zdm = kmem_zalloc(size, KM_SLEEP); | |
151 | zdm->zdm_size = size; | |
152 | zdm->zdm_timestamp = gethrestime_sec(); | |
153 | strcpy(zdm->zdm_msg, buf); | |
154 | ||
155 | mutex_enter(&zfs_dbgmsgs_lock); | |
156 | list_insert_tail(&zfs_dbgmsgs, zdm); | |
157 | zfs_dbgmsg_size += size; | |
158 | zfs_dbgmsg_purge(MAX(zfs_dbgmsg_maxsize, 0)); | |
159 | mutex_exit(&zfs_dbgmsgs_lock); | |
160 | } | |
161 | ||
8740cf4a NB |
162 | void |
163 | __set_error(const char *file, const char *func, int line, int err) | |
164 | { | |
2fd92c3d MA |
165 | /* |
166 | * To enable this: | |
167 | * | |
168 | * $ echo 512 >/sys/module/zfs/parameters/zfs_flags | |
169 | */ | |
8740cf4a NB |
170 | if (zfs_flags & ZFS_DEBUG_SET_ERROR) |
171 | __dprintf(file, func, line, "error %lu", err); | |
172 | } | |
173 | ||
3b36f831 BB |
174 | #ifdef _KERNEL |
175 | void | |
176 | __dprintf(const char *file, const char *func, int line, const char *fmt, ...) | |
177 | { | |
178 | const char *newfile; | |
495b25a9 | 179 | va_list adx; |
3b36f831 BB |
180 | size_t size; |
181 | char *buf; | |
0b39b9f9 | 182 | char *nl; |
8740cf4a | 183 | int i; |
495b25a9 | 184 | |
3b36f831 BB |
185 | size = 1024; |
186 | buf = kmem_alloc(size, KM_SLEEP); | |
495b25a9 RY |
187 | |
188 | /* | |
3b36f831 | 189 | * Get rid of annoying prefix to filename. |
495b25a9 | 190 | */ |
3b36f831 BB |
191 | newfile = strrchr(file, '/'); |
192 | if (newfile != NULL) { | |
193 | newfile = newfile + 1; /* Get rid of leading / */ | |
194 | } else { | |
195 | newfile = file; | |
196 | } | |
495b25a9 | 197 | |
8740cf4a NB |
198 | i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func); |
199 | ||
200 | if (i < size) { | |
201 | va_start(adx, fmt); | |
202 | (void) vsnprintf(buf + i, size - i, fmt, adx); | |
203 | va_end(adx); | |
204 | } | |
495b25a9 | 205 | |
0b39b9f9 PS |
206 | /* |
207 | * Get rid of trailing newline. | |
208 | */ | |
3b36f831 | 209 | nl = strrchr(buf, '\n'); |
0b39b9f9 PS |
210 | if (nl != NULL) |
211 | *nl = '\0'; | |
212 | ||
3b36f831 BB |
213 | /* |
214 | * To get this data enable the zfs__dprintf trace point as shown: | |
215 | * | |
216 | * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer | |
3b36f831 BB |
217 | * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable |
218 | * $ echo 0 > /sys/kernel/debug/tracing/trace | |
219 | * | |
220 | * # Dump the ring buffer. | |
221 | * $ cat /sys/kernel/debug/tracing/trace | |
222 | */ | |
2fd92c3d | 223 | DTRACE_PROBE1(zfs__dprintf, char *, buf); |
495b25a9 | 224 | |
3b36f831 | 225 | /* |
2fd92c3d | 226 | * To get this data: |
3b36f831 | 227 | * |
3b36f831 | 228 | * $ cat /proc/spl/kstat/zfs/dbgmsg |
2fd92c3d MA |
229 | * |
230 | * To clear the buffer: | |
231 | * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg | |
3b36f831 | 232 | */ |
2fd92c3d | 233 | __zfs_dbgmsg(buf); |
3b36f831 BB |
234 | |
235 | kmem_free(buf, size); | |
495b25a9 | 236 | } |
fa603f82 GM |
237 | |
238 | #else | |
239 | ||
240 | void | |
241 | zfs_dbgmsg_print(const char *tag) | |
242 | { | |
243 | zfs_dbgmsg_t *zdm; | |
244 | ||
245 | (void) printf("ZFS_DBGMSG(%s):\n", tag); | |
246 | mutex_enter(&zfs_dbgmsgs_lock); | |
247 | for (zdm = list_head(&zfs_dbgmsgs); zdm; | |
248 | zdm = list_next(&zfs_dbgmsgs, zdm)) | |
249 | (void) printf("%s\n", zdm->zdm_msg); | |
250 | mutex_exit(&zfs_dbgmsgs_lock); | |
251 | } | |
3b36f831 | 252 | #endif /* _KERNEL */ |
5d1f7fb6 | 253 | |
3b36f831 BB |
254 | #ifdef _KERNEL |
255 | module_param(zfs_dbgmsg_enable, int, 0644); | |
256 | MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log"); | |
5d1f7fb6 | 257 | |
3b36f831 BB |
258 | module_param(zfs_dbgmsg_maxsize, int, 0644); |
259 | MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size"); | |
260 | #endif |