CI: Automate some GitHub PR status labels manipulations
[zfs.git] / module / os / linux / zfs / zfs_debug.c
bloba017900d55388a37873b7fcd7d687e85c6f0e5c3
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/trace_zfs.h>
29 typedef struct zfs_dbgmsg {
30 procfs_list_node_t zdm_node;
31 uint64_t zdm_timestamp;
32 uint_t zdm_size;
33 char zdm_msg[]; /* variable length allocation */
34 } zfs_dbgmsg_t;
36 static procfs_list_t zfs_dbgmsgs;
37 static uint_t zfs_dbgmsg_size = 0;
38 static uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
41 * Internal ZFS debug messages are enabled by default.
43 * # Print debug messages
44 * cat /proc/spl/kstat/zfs/dbgmsg
46 * # Disable the kernel debug message log.
47 * echo 0 > /sys/module/zfs/parameters/zfs_dbgmsg_enable
49 * # Clear the kernel debug message log.
50 * echo 0 >/proc/spl/kstat/zfs/dbgmsg
52 int zfs_dbgmsg_enable = B_TRUE;
54 static int
55 zfs_dbgmsg_show_header(struct seq_file *f)
57 seq_printf(f, "%-12s %-8s\n", "timestamp", "message");
58 return (0);
61 static int
62 zfs_dbgmsg_show(struct seq_file *f, void *p)
64 zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)p;
65 seq_printf(f, "%-12llu %-s\n",
66 (u_longlong_t)zdm->zdm_timestamp, zdm->zdm_msg);
67 return (0);
70 static void
71 zfs_dbgmsg_purge(uint_t max_size)
73 while (zfs_dbgmsg_size > max_size) {
74 zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs.pl_list);
75 if (zdm == NULL)
76 return;
78 uint_t size = zdm->zdm_size;
79 kmem_free(zdm, size);
80 zfs_dbgmsg_size -= size;
84 static int
85 zfs_dbgmsg_clear(procfs_list_t *procfs_list)
87 (void) procfs_list;
88 mutex_enter(&zfs_dbgmsgs.pl_lock);
89 zfs_dbgmsg_purge(0);
90 mutex_exit(&zfs_dbgmsgs.pl_lock);
91 return (0);
94 void
95 zfs_dbgmsg_init(void)
97 procfs_list_install("zfs",
98 NULL,
99 "dbgmsg",
100 0600,
101 &zfs_dbgmsgs,
102 zfs_dbgmsg_show,
103 zfs_dbgmsg_show_header,
104 zfs_dbgmsg_clear,
105 offsetof(zfs_dbgmsg_t, zdm_node));
108 void
109 zfs_dbgmsg_fini(void)
111 procfs_list_uninstall(&zfs_dbgmsgs);
112 zfs_dbgmsg_purge(0);
114 procfs_list_destroy(&zfs_dbgmsgs);
117 void
118 __set_error(const char *file, const char *func, int line, int err)
121 * To enable this:
123 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags
125 if (zfs_flags & ZFS_DEBUG_SET_ERROR)
126 __dprintf(B_FALSE, file, func, line, "error %lu",
127 (ulong_t)err);
130 void
131 __zfs_dbgmsg(char *buf)
133 uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
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);
139 mutex_enter(&zfs_dbgmsgs.pl_lock);
140 procfs_list_add(&zfs_dbgmsgs, zdm);
141 zfs_dbgmsg_size += size;
142 zfs_dbgmsg_purge(zfs_dbgmsg_maxsize);
143 mutex_exit(&zfs_dbgmsgs.pl_lock);
146 void
147 __dprintf(boolean_t dprint, const char *file, const char *func,
148 int line, const char *fmt, ...)
150 const char *newfile;
151 va_list adx;
152 size_t size;
153 char *buf;
154 char *nl;
155 int i;
156 char *prefix = (dprint) ? "dprintf: " : "";
158 size = 1024;
159 buf = kmem_alloc(size, KM_SLEEP);
162 * Get rid of annoying prefix to filename.
164 newfile = strrchr(file, '/');
165 if (newfile != NULL) {
166 newfile = newfile + 1; /* Get rid of leading / */
167 } else {
168 newfile = file;
171 i = snprintf(buf, size, "%px %s%s:%d:%s(): ",
172 curthread, prefix, newfile, line, func);
174 if (i < size) {
175 va_start(adx, fmt);
176 (void) vsnprintf(buf + i, size - i, fmt, adx);
177 va_end(adx);
181 * Get rid of trailing newline for dprintf logs.
183 if (dprint && buf[0] != '\0') {
184 nl = &buf[strlen(buf) - 1];
185 if (*nl == '\n')
186 *nl = '\0';
190 * To get this data enable the zfs__dprintf trace point as shown:
192 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
193 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
194 * $ echo 0 > /sys/kernel/debug/tracing/trace
196 * # Dump the ring buffer.
197 * $ cat /sys/kernel/debug/tracing/trace
199 DTRACE_PROBE1(zfs__dprintf, char *, buf);
202 * To get this data:
204 * $ cat /proc/spl/kstat/zfs/dbgmsg
206 * To clear the buffer:
207 * $ echo 0 > /proc/spl/kstat/zfs/dbgmsg
209 __zfs_dbgmsg(buf);
211 kmem_free(buf, size);
214 module_param(zfs_dbgmsg_enable, int, 0644);
215 MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log");
217 /* BEGIN CSTYLED */
218 module_param(zfs_dbgmsg_maxsize, uint, 0644);
219 /* END CSTYLED */
220 MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size");