]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zfs_debug.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.git] / module / zfs / zfs_debug.c
CommitLineData
428870ff
BB
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 http://www.opensolaris.org/os/licensing.
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.
ea04106b 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
428870ff
BB
24 */
25
26#include <sys/zfs_context.h>
27
a08ee875
LG
28list_t zfs_dbgmsgs;
29int zfs_dbgmsg_size;
30kmutex_t zfs_dbgmsgs_lock;
ea04106b 31int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
428870ff 32
428870ff 33void
d7e398ce 34zfs_dbgmsg_init(void)
428870ff 35{
a08ee875
LG
36 list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
37 offsetof(zfs_dbgmsg_t, zdm_node));
38 mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
d7e398ce 39}
428870ff 40
d7e398ce
BB
41void
42zfs_dbgmsg_fini(void)
43{
a08ee875
LG
44 zfs_dbgmsg_t *zdm;
45
46 while ((zdm = list_remove_head(&zfs_dbgmsgs)) != NULL) {
47 int size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
48 kmem_free(zdm, size);
49 zfs_dbgmsg_size -= size;
50 }
51 mutex_destroy(&zfs_dbgmsgs_lock);
52 ASSERT0(zfs_dbgmsg_size);
a08ee875
LG
53}
54
a08ee875 55/*
ea04106b 56 * To get this data enable the zfs__dbgmsg tracepoint as shown:
a08ee875 57 *
ea04106b
AX
58 * # Enable zfs__dbgmsg tracepoint, clear the tracepoint ring buffer
59 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
60 * $ echo 0 > /sys/kernel/debug/tracing/trace
61 *
62 * # Dump the ring buffer.
63 * $ cat /sys/kernel/debug/tracing/trace
a08ee875
LG
64 */
65void
66zfs_dbgmsg(const char *fmt, ...)
67{
68 int size;
69 va_list adx;
ea04106b 70 char *nl;
a08ee875
LG
71 zfs_dbgmsg_t *zdm;
72
73 va_start(adx, fmt);
74 size = vsnprintf(NULL, 0, fmt, adx);
75 va_end(adx);
76
77 /*
78 * There is one byte of string in sizeof (zfs_dbgmsg_t), used
79 * for the terminating null.
80 */
81 zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
82 zdm->zdm_timestamp = gethrestime_sec();
83
84 va_start(adx, fmt);
85 (void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
86 va_end(adx);
87
ea04106b
AX
88 /*
89 * Get rid of trailing newline.
90 */
91 nl = strrchr(zdm->zdm_msg, '\n');
92 if (nl != NULL)
93 *nl = '\0';
94
a08ee875
LG
95 DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
96
97 mutex_enter(&zfs_dbgmsgs_lock);
98 list_insert_tail(&zfs_dbgmsgs, zdm);
99 zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
100 while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
101 zdm = list_remove_head(&zfs_dbgmsgs);
102 size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
103 kmem_free(zdm, size);
104 zfs_dbgmsg_size -= size;
105 }
106 mutex_exit(&zfs_dbgmsgs_lock);
d7e398ce 107}
428870ff 108
a08ee875
LG
109void
110zfs_dbgmsg_print(const char *tag)
111{
ea04106b 112#if !defined(_KERNEL)
a08ee875
LG
113 zfs_dbgmsg_t *zdm;
114
115 (void) printf("ZFS_DBGMSG(%s):\n", tag);
116 mutex_enter(&zfs_dbgmsgs_lock);
117 for (zdm = list_head(&zfs_dbgmsgs); zdm;
118 zdm = list_next(&zfs_dbgmsgs, zdm))
119 (void) printf("%s\n", zdm->zdm_msg);
120 mutex_exit(&zfs_dbgmsgs_lock);
ea04106b 121#endif /* !_KERNEL */
a08ee875 122}