zfs_debug: Restore log size limit for userspace
[zfs.git] / lib / libzpool / zfs_debug.c
blob82c7229932f059b6e16c5fe40f89bfca91282ee7
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.
24 * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
27 #include <sys/zfs_context.h>
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;
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 */
41 int zfs_dbgmsg_enable = B_TRUE;
43 static void
44 zfs_dbgmsg_purge(uint_t max_size)
46 while (zfs_dbgmsg_size > max_size) {
47 zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs);
48 if (zdm == NULL)
49 return;
51 uint_t size = zdm->zdm_size;
52 kmem_free(zdm, size);
53 zfs_dbgmsg_size -= size;
57 void
58 zfs_dbgmsg_init(void)
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);
65 void
66 zfs_dbgmsg_fini(void)
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);
74 void
75 __set_error(const char *file, const char *func, int line, int err)
77 if (zfs_flags & ZFS_DEBUG_SET_ERROR)
78 __dprintf(B_FALSE, file, func, line, "error %lu",
79 (ulong_t)err);
82 void
83 __zfs_dbgmsg(char *buf)
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);
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);
98 void
99 zfs_dbgmsg_print(int fd, const char *tag)
101 ssize_t ret __attribute__((unused));
103 mutex_enter(&zfs_dbgmsgs_lock);
106 * We use write() in this function instead of printf()
107 * so it is safe to call from a signal handler.
109 ret = write(fd, "ZFS_DBGMSG(", 11);
110 ret = write(fd, tag, strlen(tag));
111 ret = write(fd, ") START:\n", 9);
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);
119 ret = write(fd, "ZFS_DBGMSG(", 11);
120 ret = write(fd, tag, strlen(tag));
121 ret = write(fd, ") END\n", 6);
123 mutex_exit(&zfs_dbgmsgs_lock);