Path: blob/main/sys/contrib/openzfs/lib/libzpool/zfs_debug.c
48375 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/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 <[email protected]>25*/2627#include <sys/zfs_context.h>2829typedef struct zfs_dbgmsg {30list_node_t zdm_node;31uint64_t zdm_timestamp;32uint_t zdm_size;33char zdm_msg[]; /* variable length allocation */34} zfs_dbgmsg_t;3536static list_t zfs_dbgmsgs;37static kmutex_t zfs_dbgmsgs_lock;38static uint_t zfs_dbgmsg_size = 0;39static uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */4041int zfs_dbgmsg_enable = B_TRUE;4243static void44zfs_dbgmsg_purge(uint_t max_size)45{46while (zfs_dbgmsg_size > max_size) {47zfs_dbgmsg_t *zdm = list_remove_head(&zfs_dbgmsgs);48if (zdm == NULL)49return;5051uint_t size = zdm->zdm_size;52kmem_free(zdm, size);53zfs_dbgmsg_size -= size;54}55}5657void58zfs_dbgmsg_init(void)59{60list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),61offsetof(zfs_dbgmsg_t, zdm_node));62mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);63}6465void66zfs_dbgmsg_fini(void)67{68zfs_dbgmsg_t *zdm;69while ((zdm = list_remove_head(&zfs_dbgmsgs)))70umem_free(zdm, zdm->zdm_size);71mutex_destroy(&zfs_dbgmsgs_lock);72}7374void75__set_error(const char *file, const char *func, int line, int err)76{77if (zfs_flags & ZFS_DEBUG_SET_ERROR)78__dprintf(B_FALSE, file, func, line, "error %lu",79(ulong_t)err);80}8182void83__zfs_dbgmsg(char *buf)84{85uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;86zfs_dbgmsg_t *zdm = umem_zalloc(size, KM_SLEEP);87zdm->zdm_size = size;88zdm->zdm_timestamp = gethrestime_sec();89strcpy(zdm->zdm_msg, buf);9091mutex_enter(&zfs_dbgmsgs_lock);92list_insert_tail(&zfs_dbgmsgs, zdm);93zfs_dbgmsg_size += size;94zfs_dbgmsg_purge(zfs_dbgmsg_maxsize);95mutex_exit(&zfs_dbgmsgs_lock);96}9798void99zfs_dbgmsg_print(int fd, const char *tag)100{101ssize_t ret __attribute__((unused));102103mutex_enter(&zfs_dbgmsgs_lock);104105/*106* We use write() in this function instead of printf()107* so it is safe to call from a signal handler.108*/109ret = write(fd, "ZFS_DBGMSG(", 11);110ret = write(fd, tag, strlen(tag));111ret = write(fd, ") START:\n", 9);112113for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;114zdm = list_next(&zfs_dbgmsgs, zdm)) {115ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));116ret = write(fd, "\n", 1);117}118119ret = write(fd, "ZFS_DBGMSG(", 11);120ret = write(fd, tag, strlen(tag));121ret = write(fd, ") END\n", 6);122123mutex_exit(&zfs_dbgmsgs_lock);124}125126127