Home | History | Annotate | Line # | Download | only in zfs
      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.
     23  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
     24  */
     25 
     26 #include <sys/zfs_context.h>
     27 
     28 list_t zfs_dbgmsgs;
     29 int zfs_dbgmsg_size;
     30 kmutex_t zfs_dbgmsgs_lock;
     31 int zfs_dbgmsg_maxsize = 4<<20; /* 4MB */
     32 
     33 void
     34 zfs_dbgmsg_init(void)
     35 {
     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);
     39 }
     40 
     41 void
     42 zfs_dbgmsg_fini(void)
     43 {
     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);
     53 }
     54 
     55 /*
     56  * Print these messages by running:
     57  * echo ::zfs_dbgmsg | mdb -k
     58  *
     59  * Monitor these messages by running:
     60  * dtrace -qn 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
     61  *
     62  * When used with libzpool, monitor with:
     63  * dtrace -qn 'zfs$pid::zfs_dbgmsg:probe1{printf("%s\n", copyinstr(arg1))}'
     64  */
     65 void
     66 zfs_dbgmsg(const char *fmt, ...)
     67 {
     68 	int size;
     69 	va_list adx;
     70 	zfs_dbgmsg_t *zdm;
     71 
     72 	va_start(adx, fmt);
     73 	size = vsnprintf(NULL, 0, fmt, adx);
     74 	va_end(adx);
     75 
     76 	/*
     77 	 * There is one byte of string in sizeof (zfs_dbgmsg_t), used
     78 	 * for the terminating null.
     79 	 */
     80 	zdm = kmem_alloc(sizeof (zfs_dbgmsg_t) + size, KM_SLEEP);
     81 	zdm->zdm_timestamp = gethrestime_sec();
     82 
     83 	va_start(adx, fmt);
     84 	(void) vsnprintf(zdm->zdm_msg, size + 1, fmt, adx);
     85 	va_end(adx);
     86 
     87 	DTRACE_PROBE1(zfs__dbgmsg, char *, zdm->zdm_msg);
     88 
     89 	mutex_enter(&zfs_dbgmsgs_lock);
     90 	list_insert_tail(&zfs_dbgmsgs, zdm);
     91 	zfs_dbgmsg_size += sizeof (zfs_dbgmsg_t) + size;
     92 	while (zfs_dbgmsg_size > zfs_dbgmsg_maxsize) {
     93 		zdm = list_remove_head(&zfs_dbgmsgs);
     94 		size = sizeof (zfs_dbgmsg_t) + strlen(zdm->zdm_msg);
     95 		kmem_free(zdm, size);
     96 		zfs_dbgmsg_size -= size;
     97 	}
     98 	mutex_exit(&zfs_dbgmsgs_lock);
     99 }
    100 
    101 void
    102 zfs_dbgmsg_print(const char *tag)
    103 {
    104 	zfs_dbgmsg_t *zdm;
    105 
    106 	(void) printf("ZFS_DBGMSG(%s):\n", tag);
    107 	mutex_enter(&zfs_dbgmsgs_lock);
    108 	for (zdm = list_head(&zfs_dbgmsgs); zdm;
    109 	    zdm = list_next(&zfs_dbgmsgs, zdm))
    110 		(void) printf("%s\n", zdm->zdm_msg);
    111 	mutex_exit(&zfs_dbgmsgs_lock);
    112 }
    113