xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_trace.c (revision 1717:ef845d4a1074)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1717Swesolows  * Common Development and Distribution License (the "License").
6*1717Swesolows  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211193Smws 
220Sstevel@tonic-gate /*
23*1717Swesolows  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <ucontext.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <fmd_trace.h>
380Sstevel@tonic-gate #include <fmd_alloc.h>
390Sstevel@tonic-gate #include <fmd_subr.h>
400Sstevel@tonic-gate #include <fmd_conf.h>
410Sstevel@tonic-gate #include <fmd.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate fmd_tracebuf_t *
fmd_trace_create(void)440Sstevel@tonic-gate fmd_trace_create(void)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	fmd_tracebuf_t *tbp = fmd_zalloc(sizeof (fmd_tracebuf_t), FMD_SLEEP);
470Sstevel@tonic-gate 	size_t bufsize;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "trace.frames", &tbp->tb_frames);
500Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "trace.recs", &tbp->tb_recs);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	/*
530Sstevel@tonic-gate 	 * We require 8-byte alignment of fmd_tracerec_t to store hrtime_t's.
540Sstevel@tonic-gate 	 * Since the trailing flexible array member is of type uintptr_t, we
55*1717Swesolows 	 * may need to allocate an additional element if we are compiling
56*1717Swesolows 	 * 32-bit; otherwise uintptr_t is 8 bytes so any value of tb_frames is
57*1717Swesolows 	 * acceptable.
58*1717Swesolows 	 *
59*1717Swesolows 	 * tb_frames includes the first element, whose size is reflected in
60*1717Swesolows 	 * sizeof (fmd_tracerec_t).  Therefore, if fmd_tracerec_t's size is
61*1717Swesolows 	 * 0 mod 8, we must be sure the total number of frames is odd.
62*1717Swesolows 	 * Otherwise, we need at least one extra frame, so the total count
63*1717Swesolows 	 * must be even.  This will continue to work even if the sizes or
64*1717Swesolows 	 * types of other fmd_tracerec_t members are changed.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate #ifdef _ILP32
67*1717Swesolows 	/*CONSTCOND*/
68*1717Swesolows 	if (sizeof (fmd_tracerec_t) % sizeof (hrtime_t) == 0)
69*1717Swesolows 		tbp->tb_frames = (tbp->tb_frames & ~1UL) + 1;
70*1717Swesolows 	else
71*1717Swesolows 		tbp->tb_frames = P2ROUNDUP(tbp->tb_frames, 2);
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate 	tbp->tb_size = sizeof (fmd_tracerec_t) +
740Sstevel@tonic-gate 	    sizeof (uintptr_t) * (MAX(tbp->tb_frames, 1) - 1);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	bufsize = tbp->tb_size * tbp->tb_recs;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	tbp->tb_buf = fmd_zalloc(bufsize, FMD_SLEEP);
790Sstevel@tonic-gate 	tbp->tb_end = (void *)((uintptr_t)tbp->tb_buf + bufsize - tbp->tb_size);
800Sstevel@tonic-gate 	tbp->tb_ptr = tbp->tb_buf;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	return (tbp);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate void
fmd_trace_destroy(fmd_tracebuf_t * tbp)860Sstevel@tonic-gate fmd_trace_destroy(fmd_tracebuf_t *tbp)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	fmd_free(tbp->tb_buf, tbp->tb_size * tbp->tb_recs);
890Sstevel@tonic-gate 	fmd_free(tbp, sizeof (fmd_tracebuf_t));
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Callback for walkcontext(3C) to store the stack trace.  We use tr_tag below
940Sstevel@tonic-gate  * to store the maximum value of depth that is permitted so we can use it here.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate /*ARGSUSED*/
970Sstevel@tonic-gate static int
fmd_trace_frame(uintptr_t pc,int sig,fmd_tracerec_t * trp)980Sstevel@tonic-gate fmd_trace_frame(uintptr_t pc, int sig, fmd_tracerec_t *trp)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	trp->tr_stack[trp->tr_depth++] = pc;
1010Sstevel@tonic-gate 	return (trp->tr_depth >= trp->tr_tag);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*ARGSUSED*/
1050Sstevel@tonic-gate fmd_tracerec_t *
fmd_trace_none(fmd_tracebuf_t * tbp,uint_t tag,const char * format,va_list ap)1061193Smws fmd_trace_none(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	return (NULL);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate fmd_tracerec_t *
fmd_trace_lite(fmd_tracebuf_t * tbp,uint_t tag,const char * format,va_list ap)1121193Smws fmd_trace_lite(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	hrtime_t time = gethrtime();
1150Sstevel@tonic-gate 	fmd_tracerec_t *trp = tbp->tb_ptr;
1160Sstevel@tonic-gate 	char *p;
1170Sstevel@tonic-gate 
1181193Smws 	if (tbp->tb_depth++ != 0) {
1191193Smws 		tbp->tb_depth--;
1201193Smws 		return (NULL);
1211193Smws 	}
1221193Smws 
1230Sstevel@tonic-gate 	trp->tr_time = time;
1240Sstevel@tonic-gate 	trp->tr_file = NULL;
1250Sstevel@tonic-gate 	trp->tr_line = 0;
1260Sstevel@tonic-gate 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
1271193Smws 	trp->tr_tag = fmd_ntz32(tag);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
1300Sstevel@tonic-gate 	p = &trp->tr_msg[strlen(trp->tr_msg)];
1310Sstevel@tonic-gate 	if (p > trp->tr_msg && p[-1] == '\n')
1320Sstevel@tonic-gate 		p[-1] = '\0';
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (tbp->tb_ptr != tbp->tb_end)
1350Sstevel@tonic-gate 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
1360Sstevel@tonic-gate 	else
1370Sstevel@tonic-gate 		tbp->tb_ptr = tbp->tb_buf;
1380Sstevel@tonic-gate 
1391193Smws 	tbp->tb_depth--;
1400Sstevel@tonic-gate 	return (trp);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate fmd_tracerec_t *
fmd_trace_full(fmd_tracebuf_t * tbp,uint_t tag,const char * format,va_list ap)1441193Smws fmd_trace_full(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	hrtime_t time = gethrtime();
1470Sstevel@tonic-gate 	fmd_tracerec_t *trp = tbp->tb_ptr;
1480Sstevel@tonic-gate 	ucontext_t uc;
1490Sstevel@tonic-gate 	char *p;
1500Sstevel@tonic-gate 
1511193Smws 	if (tbp->tb_depth++ != 0) {
1521193Smws 		tbp->tb_depth--;
1531193Smws 		return (NULL);
1541193Smws 	}
1551193Smws 
1560Sstevel@tonic-gate 	(void) getcontext(&uc);
1570Sstevel@tonic-gate 	trp->tr_tag = tbp->tb_frames; /* for use by fmd_trace_frame() */
1580Sstevel@tonic-gate 	(void) walkcontext(&uc, (int (*)())fmd_trace_frame, trp);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	trp->tr_time = time;
1610Sstevel@tonic-gate 	trp->tr_file = NULL;
1620Sstevel@tonic-gate 	trp->tr_line = 0;
1630Sstevel@tonic-gate 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
1641193Smws 	trp->tr_tag = fmd_ntz32(tag);
1651193Smws 
1661193Smws 	if (fmd.d_fmd_debug & FMD_DBG_TRACE)
1671193Smws 		fmd_vdprintf(tag, format, ap);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
1700Sstevel@tonic-gate 	p = &trp->tr_msg[strlen(trp->tr_msg)];
1710Sstevel@tonic-gate 	if (p > trp->tr_msg && p[-1] == '\n')
1720Sstevel@tonic-gate 		p[-1] = '\0';
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (tbp->tb_ptr != tbp->tb_end)
1750Sstevel@tonic-gate 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
1760Sstevel@tonic-gate 	else
1770Sstevel@tonic-gate 		tbp->tb_ptr = tbp->tb_buf;
1780Sstevel@tonic-gate 
1791193Smws 	tbp->tb_depth--;
1800Sstevel@tonic-gate 	return (trp);
1810Sstevel@tonic-gate }
182