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