xref: /freebsd/sys/contrib/openzfs/lib/libzpool/zfs_debug.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25  * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
26  */
27 
28 #include <sys/zfs_context.h>
29 
30 typedef struct zfs_dbgmsg {
31 	list_node_t		zdm_node;
32 	uint64_t		zdm_timestamp;
33 	uint_t			zdm_size;
34 	char			zdm_msg[]; /* variable length allocation */
35 } zfs_dbgmsg_t;
36 
37 static list_t zfs_dbgmsgs;
38 static kmutex_t zfs_dbgmsgs_lock;
39 static uint_t zfs_dbgmsg_size = 0;
40 static uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
41 
42 int zfs_dbgmsg_enable = B_TRUE;
43 
44 static void
zfs_dbgmsg_purge(uint_t max_size)45 zfs_dbgmsg_purge(uint_t max_size)
46 {
47 	while (zfs_dbgmsg_size > max_size) {
48 		zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs);
49 		if (zdm == NULL)
50 			return;
51 
52 		uint_t size = zdm->zdm_size;
53 		kmem_free(zdm, size);
54 		zfs_dbgmsg_size -= size;
55 	}
56 }
57 
58 void
zfs_dbgmsg_init(void)59 zfs_dbgmsg_init(void)
60 {
61 	list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
62 	    offsetof(zfs_dbgmsg_t, zdm_node));
63 	mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
64 }
65 
66 void
zfs_dbgmsg_fini(void)67 zfs_dbgmsg_fini(void)
68 {
69 	zfs_dbgmsg_t *zdm;
70 	while ((zdm = list_remove_head(&zfs_dbgmsgs)))
71 		umem_free(zdm, zdm->zdm_size);
72 	mutex_destroy(&zfs_dbgmsgs_lock);
73 }
74 
75 void
__set_error(const char * file,const char * func,int line,int err)76 __set_error(const char *file, const char *func, int line, int err)
77 {
78 	if (zfs_flags & ZFS_DEBUG_SET_ERROR)
79 		__dprintf(B_FALSE, file, func, line, "error %lu",
80 		    (ulong_t)err);
81 }
82 
83 void
__zfs_dbgmsg(char * buf)84 __zfs_dbgmsg(char *buf)
85 {
86 	uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
87 	zfs_dbgmsg_t *zdm = umem_zalloc(size, KM_SLEEP);
88 	zdm->zdm_size = size;
89 	zdm->zdm_timestamp = gethrestime_sec();
90 	strcpy(zdm->zdm_msg, buf);
91 
92 	mutex_enter(&zfs_dbgmsgs_lock);
93 	list_insert_tail(&zfs_dbgmsgs, zdm);
94 	zfs_dbgmsg_size += size;
95 	zfs_dbgmsg_purge(zfs_dbgmsg_maxsize);
96 	mutex_exit(&zfs_dbgmsgs_lock);
97 }
98 
99 void
zfs_dbgmsg_print(int fd,const char * tag)100 zfs_dbgmsg_print(int fd, const char *tag)
101 {
102 	ssize_t ret __attribute__((unused));
103 
104 	mutex_enter(&zfs_dbgmsgs_lock);
105 
106 	/*
107 	 * We use write() in this function instead of printf()
108 	 * so it is safe to call from a signal handler.
109 	 */
110 	ret = write(fd, "ZFS_DBGMSG(", 11);
111 	ret = write(fd, tag, strlen(tag));
112 	ret = write(fd, ") START:\n", 9);
113 
114 	for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;
115 	    zdm = list_next(&zfs_dbgmsgs, zdm)) {
116 		ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
117 		ret = write(fd, "\n", 1);
118 	}
119 
120 	ret = write(fd, "ZFS_DBGMSG(", 11);
121 	ret = write(fd, tag, strlen(tag));
122 	ret = write(fd, ") END\n", 6);
123 
124 	mutex_exit(&zfs_dbgmsgs_lock);
125 }
126