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 */ 25 26 #include <sys/zfs_context.h> 27 #include <sys/kstat.h> 28 29 typedef struct zfs_dbgmsg { 30 list_node_t zdm_node; 31 time_t zdm_timestamp; 32 uint_t zdm_size; 33 char zdm_msg[]; 34 } zfs_dbgmsg_t; 35 36 static list_t zfs_dbgmsgs; 37 static uint_t zfs_dbgmsg_size = 0; 38 static kmutex_t zfs_dbgmsgs_lock; 39 uint_t zfs_dbgmsg_maxsize = 4<<20; /* 4MB */ 40 static kstat_t *zfs_dbgmsg_kstat; 41 42 /* 43 * Internal ZFS debug messages are enabled by default. 44 * 45 * # Print debug messages as they're logged 46 * dtrace -n 'zfs-dbgmsg { print(stringof(arg0)); }' 47 * 48 * # Print all logged dbgmsg entries 49 * sysctl kstat.zfs.misc.dbgmsg 50 * 51 * # Disable the kernel debug message log. 52 * sysctl vfs.zfs.dbgmsg_enable=0 53 */ 54 int zfs_dbgmsg_enable = B_TRUE; 55 56 static int 57 zfs_dbgmsg_headers(char *buf, size_t size) 58 { 59 (void) snprintf(buf, size, "%-12s %-8s\n", "timestamp", "message"); 60 61 return (0); 62 } 63 64 static int 65 zfs_dbgmsg_data(char *buf, size_t size, void *data) 66 { 67 zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)data; 68 69 (void) snprintf(buf, size, "%-12llu %-s\n", 70 (u_longlong_t)zdm->zdm_timestamp, zdm->zdm_msg); 71 72 return (0); 73 } 74 75 static void * 76 zfs_dbgmsg_addr(kstat_t *ksp, loff_t n) 77 { 78 zfs_dbgmsg_t *zdm = (zfs_dbgmsg_t *)ksp->ks_private; 79 80 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock)); 81 82 if (n == 0) 83 ksp->ks_private = list_head(&zfs_dbgmsgs); 84 else if (zdm) 85 ksp->ks_private = list_next(&zfs_dbgmsgs, zdm); 86 87 return (ksp->ks_private); 88 } 89 90 static void 91 zfs_dbgmsg_purge(uint_t max_size) 92 { 93 zfs_dbgmsg_t *zdm; 94 uint_t size; 95 96 ASSERT(MUTEX_HELD(&zfs_dbgmsgs_lock)); 97 98 while (zfs_dbgmsg_size > max_size) { 99 zdm = list_remove_head(&zfs_dbgmsgs); 100 if (zdm == NULL) 101 return; 102 103 size = zdm->zdm_size; 104 kmem_free(zdm, size); 105 zfs_dbgmsg_size -= size; 106 } 107 } 108 109 static int 110 zfs_dbgmsg_update(kstat_t *ksp, int rw) 111 { 112 if (rw == KSTAT_WRITE) 113 zfs_dbgmsg_purge(0); 114 115 return (0); 116 } 117 118 void 119 zfs_dbgmsg_init(void) 120 { 121 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t), 122 offsetof(zfs_dbgmsg_t, zdm_node)); 123 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL); 124 125 zfs_dbgmsg_kstat = kstat_create("zfs", 0, "dbgmsg", "misc", 126 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL); 127 if (zfs_dbgmsg_kstat) { 128 zfs_dbgmsg_kstat->ks_lock = &zfs_dbgmsgs_lock; 129 zfs_dbgmsg_kstat->ks_ndata = UINT32_MAX; 130 zfs_dbgmsg_kstat->ks_private = NULL; 131 zfs_dbgmsg_kstat->ks_update = zfs_dbgmsg_update; 132 kstat_set_raw_ops(zfs_dbgmsg_kstat, zfs_dbgmsg_headers, 133 zfs_dbgmsg_data, zfs_dbgmsg_addr); 134 kstat_install(zfs_dbgmsg_kstat); 135 } 136 } 137 138 void 139 zfs_dbgmsg_fini(void) 140 { 141 if (zfs_dbgmsg_kstat) 142 kstat_delete(zfs_dbgmsg_kstat); 143 144 mutex_enter(&zfs_dbgmsgs_lock); 145 zfs_dbgmsg_purge(0); 146 mutex_exit(&zfs_dbgmsgs_lock); 147 mutex_destroy(&zfs_dbgmsgs_lock); 148 } 149 150 void 151 __zfs_dbgmsg(char *buf) 152 { 153 zfs_dbgmsg_t *zdm; 154 uint_t size; 155 156 DTRACE_PROBE1(zfs__dbgmsg, char *, buf); 157 158 size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1; 159 zdm = kmem_zalloc(size, KM_SLEEP); 160 zdm->zdm_size = size; 161 zdm->zdm_timestamp = gethrestime_sec(); 162 strcpy(zdm->zdm_msg, buf); 163 164 mutex_enter(&zfs_dbgmsgs_lock); 165 list_insert_tail(&zfs_dbgmsgs, zdm); 166 zfs_dbgmsg_size += size; 167 zfs_dbgmsg_purge(zfs_dbgmsg_maxsize); 168 mutex_exit(&zfs_dbgmsgs_lock); 169 } 170 171 void 172 __set_error(const char *file, const char *func, int line, int err) 173 { 174 /* 175 * To enable this: 176 * 177 * $ echo 512 >/sys/module/zfs/parameters/zfs_flags 178 */ 179 if (zfs_flags & ZFS_DEBUG_SET_ERROR) 180 __dprintf(B_FALSE, file, func, line, "error %lu", (ulong_t)err); 181 } 182 183 void 184 __dprintf(boolean_t dprint, const char *file, const char *func, 185 int line, const char *fmt, ...) 186 { 187 const char *newfile; 188 va_list adx; 189 size_t size; 190 char *buf; 191 char *nl; 192 int i; 193 194 size = 1024; 195 buf = kmem_alloc(size, KM_SLEEP); 196 197 /* 198 * Get rid of annoying prefix to filename. 199 */ 200 newfile = strrchr(file, '/'); 201 if (newfile != NULL) { 202 newfile = newfile + 1; /* Get rid of leading / */ 203 } else { 204 newfile = file; 205 } 206 207 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func); 208 209 if (i < size) { 210 va_start(adx, fmt); 211 (void) vsnprintf(buf + i, size - i, fmt, adx); 212 va_end(adx); 213 } 214 215 /* 216 * Get rid of trailing newline for dprintf logs. 217 */ 218 if (dprint && buf[0] != '\0') { 219 nl = &buf[strlen(buf) - 1]; 220 if (*nl == '\n') 221 *nl = '\0'; 222 } 223 224 /* 225 * To get this data: 226 * 227 * $ sysctl -n kstat.zfs.misc.dbgmsg 228 */ 229 __zfs_dbgmsg(buf); 230 231 kmem_free(buf, size); 232 } 233 234 ZFS_MODULE_PARAM(zfs, zfs_, dbgmsg_enable, INT, ZMOD_RW, 235 "Enable ZFS debug message log"); 236 237 ZFS_MODULE_PARAM(zfs, zfs_, dbgmsg_maxsize, UINT, ZMOD_RW, 238 "Maximum ZFS debug log size"); 239