1*3227e6cfSchs /*
2*3227e6cfSchs * CDDL HEADER START
3*3227e6cfSchs *
4*3227e6cfSchs * The contents of this file are subject to the terms of the
5*3227e6cfSchs * Common Development and Distribution License (the "License").
6*3227e6cfSchs * You may not use this file except in compliance with the License.
7*3227e6cfSchs *
8*3227e6cfSchs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3227e6cfSchs * or http://www.opensolaris.org/os/licensing.
10*3227e6cfSchs * See the License for the specific language governing permissions
11*3227e6cfSchs * and limitations under the License.
12*3227e6cfSchs *
13*3227e6cfSchs * When distributing Covered Code, include this CDDL HEADER in each
14*3227e6cfSchs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3227e6cfSchs * If applicable, add the following below this CDDL HEADER, with the
16*3227e6cfSchs * fields enclosed by brackets "[]" replaced with your own identifying
17*3227e6cfSchs * information: Portions Copyright [yyyy] [name of copyright owner]
18*3227e6cfSchs *
19*3227e6cfSchs * CDDL HEADER END
20*3227e6cfSchs */
21*3227e6cfSchs /*
22*3227e6cfSchs * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*3227e6cfSchs * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24*3227e6cfSchs */
25*3227e6cfSchs
26*3227e6cfSchs #include <sys/zfs_context.h>
27*3227e6cfSchs
28*3227e6cfSchs list_t zfs_dbgmsgs;
29*3227e6cfSchs int zfs_dbgmsg_size;
30*3227e6cfSchs kmutex_t zfs_dbgmsgs_lock;
31*3227e6cfSchs int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
32*3227e6cfSchs
33*3227e6cfSchs void
zfs_dbgmsg_init(void)34*3227e6cfSchs zfs_dbgmsg_init(void)
35*3227e6cfSchs {
36*3227e6cfSchs list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
37*3227e6cfSchs offsetof(zfs_dbgmsg_t, zdm_node));
38*3227e6cfSchs mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
39*3227e6cfSchs }
40*3227e6cfSchs
41*3227e6cfSchs void
zfs_dbgmsg_fini(void)42*3227e6cfSchs zfs_dbgmsg_fini(void)
43*3227e6cfSchs {
44*3227e6cfSchs zfs_dbgmsg_t *zdm;
45*3227e6cfSchs
46*3227e6cfSchs while ((zdm = list_remove_head(&zfs_dbgmsgs)) != NULL) {
47*3227e6cfSchs int size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
48*3227e6cfSchs kmem_free(zdm, size);
49*3227e6cfSchs zfs_dbgmsg_size -= size;
50*3227e6cfSchs }
51*3227e6cfSchs mutex_destroy(&zfs_dbgmsgs_lock);
52*3227e6cfSchs ASSERT0(zfs_dbgmsg_size);
53*3227e6cfSchs }
54*3227e6cfSchs
55*3227e6cfSchs /*
56*3227e6cfSchs * Print these messages by running:
57*3227e6cfSchs * echo ::zfs_dbgmsg | mdb -k
58*3227e6cfSchs *
59*3227e6cfSchs * Monitor these messages by running:
60*3227e6cfSchs * dtrace -qn 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
61*3227e6cfSchs *
62*3227e6cfSchs * When used with libzpool, monitor with:
63*3227e6cfSchs * dtrace -qn 'zfs$pid::zfs_dbgmsg:probe1{printf("%s\n", copyinstr(arg1))}'
64*3227e6cfSchs */
65*3227e6cfSchs void
zfs_dbgmsg(const char * fmt,...)66*3227e6cfSchs zfs_dbgmsg(const char *fmt, ...)
67*3227e6cfSchs {
68*3227e6cfSchs int size;
69*3227e6cfSchs va_list adx;
70*3227e6cfSchs zfs_dbgmsg_t *zdm;
71*3227e6cfSchs
72*3227e6cfSchs va_start(adx, fmt);
73*3227e6cfSchs size = vsnprintf(NULL, 0, fmt, adx);
74*3227e6cfSchs va_end(adx);
75*3227e6cfSchs
76*3227e6cfSchs /*
77*3227e6cfSchs * There is one byte of string in sizeof (zfs_dbgmsg_t), used
78*3227e6cfSchs * for the terminating null.
79*3227e6cfSchs */
80*3227e6cfSchs zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
81*3227e6cfSchs zdm->zdm_timestamp = gethrestime_sec();
82*3227e6cfSchs
83*3227e6cfSchs va_start(adx, fmt);
84*3227e6cfSchs (void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
85*3227e6cfSchs va_end(adx);
86*3227e6cfSchs
87*3227e6cfSchs DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
88*3227e6cfSchs
89*3227e6cfSchs mutex_enter(&zfs_dbgmsgs_lock);
90*3227e6cfSchs list_insert_tail(&zfs_dbgmsgs, zdm);
91*3227e6cfSchs zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
92*3227e6cfSchs while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
93*3227e6cfSchs zdm = list_remove_head(&zfs_dbgmsgs);
94*3227e6cfSchs size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
95*3227e6cfSchs kmem_free(zdm, size);
96*3227e6cfSchs zfs_dbgmsg_size -= size;
97*3227e6cfSchs }
98*3227e6cfSchs mutex_exit(&zfs_dbgmsgs_lock);
99*3227e6cfSchs }
100*3227e6cfSchs
101*3227e6cfSchs void
zfs_dbgmsg_print(const char * tag)102*3227e6cfSchs zfs_dbgmsg_print(const char *tag)
103*3227e6cfSchs {
104*3227e6cfSchs zfs_dbgmsg_t *zdm;
105*3227e6cfSchs
106*3227e6cfSchs (void) printf("ZFS_DBGMSG(%s):\n", tag);
107*3227e6cfSchs mutex_enter(&zfs_dbgmsgs_lock);
108*3227e6cfSchs for (zdm = list_head(&zfs_dbgmsgs); zdm;
109*3227e6cfSchs zdm = list_next(&zfs_dbgmsgs, zdm))
110*3227e6cfSchs (void) printf("%s\n", zdm->zdm_msg);
111*3227e6cfSchs mutex_exit(&zfs_dbgmsgs_lock);
112*3227e6cfSchs }
113