xref: /freebsd/sys/contrib/openzfs/lib/libzpool/zfs_debug.c (revision b1879975794772ee51f0b4865753364c7d7626c3)
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 https://opensource.org/licenses/CDDL-1.0.
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  * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
25  */
26 
27 #include <sys/zfs_context.h>
28 
29 typedef struct zfs_dbgmsg {
30 	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;
35 
36 static list_t zfs_dbgmsgs;
37 static kmutex_t zfs_dbgmsgs_lock;
38 static uint_t zfs_dbgmsg_size = 0;
39 static uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
40 
41 int zfs_dbgmsg_enable = B_TRUE;
42 
43 static void
44 zfs_dbgmsg_purge(uint_t max_size)
45 {
46 	while (zfs_dbgmsg_size > max_size) {
47 		zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs);
48 		if (zdm == NULL)
49 			return;
50 
51 		uint_t size = zdm->zdm_size;
52 		kmem_free(zdm, size);
53 		zfs_dbgmsg_size -= size;
54 	}
55 }
56 
57 void
58 zfs_dbgmsg_init(void)
59 {
60 	list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
61 	    offsetof(zfs_dbgmsg_t, zdm_node));
62 	mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
63 }
64 
65 void
66 zfs_dbgmsg_fini(void)
67 {
68 	zfs_dbgmsg_t *zdm;
69 	while ((zdm = list_remove_head(&zfs_dbgmsgs)))
70 		umem_free(zdm, zdm->zdm_size);
71 	mutex_destroy(&zfs_dbgmsgs_lock);
72 }
73 
74 void
75 __set_error(const char *file, const char *func, int line, int err)
76 {
77 	if (zfs_flags & ZFS_DEBUG_SET_ERROR)
78 		__dprintf(B_FALSE, file, func, line, "error %lu",
79 		    (ulong_t)err);
80 }
81 
82 void
83 __zfs_dbgmsg(char *buf)
84 {
85 	uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
86 	zfs_dbgmsg_t *zdm = umem_zalloc(size, KM_SLEEP);
87 	zdm->zdm_size = size;
88 	zdm->zdm_timestamp = gethrestime_sec();
89 	strcpy(zdm->zdm_msg, buf);
90 
91 	mutex_enter(&zfs_dbgmsgs_lock);
92 	list_insert_tail(&zfs_dbgmsgs, zdm);
93 	zfs_dbgmsg_size += size;
94 	zfs_dbgmsg_purge(zfs_dbgmsg_maxsize);
95 	mutex_exit(&zfs_dbgmsgs_lock);
96 }
97 
98 void
99 zfs_dbgmsg_print(int fd, const char *tag)
100 {
101 	ssize_t ret __attribute__((unused));
102 
103 	mutex_enter(&zfs_dbgmsgs_lock);
104 
105 	/*
106 	 * We use write() in this function instead of printf()
107 	 * so it is safe to call from a signal handler.
108 	 */
109 	ret = write(fd, "ZFS_DBGMSG(", 11);
110 	ret = write(fd, tag, strlen(tag));
111 	ret = write(fd, ") START:\n", 9);
112 
113 	for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;
114 	    zdm = list_next(&zfs_dbgmsgs, zdm)) {
115 		ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
116 		ret = write(fd, "\n", 1);
117 	}
118 
119 	ret = write(fd, "ZFS_DBGMSG(", 11);
120 	ret = write(fd, tag, strlen(tag));
121 	ret = write(fd, ") END\n", 6);
122 
123 	mutex_exit(&zfs_dbgmsgs_lock);
124 }
125