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 39 int zfs_dbgmsg_enable = B_TRUE; 40 41 void 42 zfs_dbgmsg_init(void) 43 { 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); 47 } 48 49 void 50 zfs_dbgmsg_fini(void) 51 { 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); 56 } 57 58 void 59 __set_error(const char *file, const char *func, int line, int err) 60 { 61 if (zfs_flags & ZFS_DEBUG_SET_ERROR) 62 __dprintf(B_FALSE, file, func, line, "error %lu", 63 (ulong_t)err); 64 } 65 66 void 67 __zfs_dbgmsg(char *buf) 68 { 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); 74 75 mutex_enter(&zfs_dbgmsgs_lock); 76 list_insert_tail(&zfs_dbgmsgs, zdm); 77 mutex_exit(&zfs_dbgmsgs_lock); 78 } 79 80 void 81 zfs_dbgmsg_print(int fd, const char *tag) 82 { 83 ssize_t ret __attribute__((unused)); 84 85 mutex_enter(&zfs_dbgmsgs_lock); 86 87 /* 88 * We use write() in this function instead of printf() 89 * so it is safe to call from a signal handler. 90 */ 91 ret = write(fd, "ZFS_DBGMSG(", 11); 92 ret = write(fd, tag, strlen(tag)); 93 ret = write(fd, ") START:\n", 9); 94 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); 99 } 100 101 ret = write(fd, "ZFS_DBGMSG(", 11); 102 ret = write(fd, tag, strlen(tag)); 103 ret = write(fd, ") END\n", 6); 104 105 mutex_exit(&zfs_dbgmsgs_lock); 106 } 107