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