FreeBSD: ignore some includes when not building kernel
[zfs.git] / lib / libzpool / zfs_debug.c
blobdf49a9a33fe898ae2110fa634720040c320e67e8
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;
39 int zfs_dbgmsg_enable = B_TRUE;
41 void
42 zfs_dbgmsg_init(void)
44 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
45 offsetof(zfs_dbgmsg_t, zdm_node));
46 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
49 void
50 zfs_dbgmsg_fini(void)
52 zfs_dbgmsg_t *zdm;
53 while ((zdm = list_remove_head(&zfs_dbgmsgs)))
54 umem_free(zdm, zdm->zdm_size);
55 mutex_destroy(&zfs_dbgmsgs_lock);
58 void
59 __set_error(const char *file, const char *func, int line, int err)
61 if (zfs_flags & ZFS_DEBUG_SET_ERROR)
62 __dprintf(B_FALSE, file, func, line, "error %lu",
63 (ulong_t)err);
66 void
67 __zfs_dbgmsg(char *buf)
69 uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
70 zfs_dbgmsg_t *zdm = umem_zalloc(size, KM_SLEEP);
71 zdm->zdm_size = size;
72 zdm->zdm_timestamp = gethrestime_sec();
73 strcpy(zdm->zdm_msg, buf);
75 mutex_enter(&zfs_dbgmsgs_lock);
76 list_insert_tail(&zfs_dbgmsgs, zdm);
77 mutex_exit(&zfs_dbgmsgs_lock);
80 void
81 zfs_dbgmsg_print(int fd, const char *tag)
83 ssize_t ret __attribute__((unused));
85 mutex_enter(&zfs_dbgmsgs_lock);
88 * We use write() in this function instead of printf()
89 * so it is safe to call from a signal handler.
91 ret = write(fd, "ZFS_DBGMSG(", 11);
92 ret = write(fd, tag, strlen(tag));
93 ret = write(fd, ") START:\n", 9);
95 for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;
96 zdm = list_next(&zfs_dbgmsgs, zdm)) {
97 ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
98 ret = write(fd, "\n", 1);
101 ret = write(fd, "ZFS_DBGMSG(", 11);
102 ret = write(fd, tag, strlen(tag));
103 ret = write(fd, ") END\n", 6);
105 mutex_exit(&zfs_dbgmsgs_lock);