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