xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_trace.c (revision 3db86aab)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 
32 #include <ucontext.h>
33 #include <strings.h>
34 #include <stdio.h>
35 #include <errno.h>
36 
37 #include <fmd_trace.h>
38 #include <fmd_alloc.h>
39 #include <fmd_subr.h>
40 #include <fmd_conf.h>
41 #include <fmd.h>
42 
43 fmd_tracebuf_t *
44 fmd_trace_create(void)
45 {
46 	fmd_tracebuf_t *tbp = fmd_zalloc(sizeof (fmd_tracebuf_t), FMD_SLEEP);
47 	size_t bufsize;
48 
49 	(void) fmd_conf_getprop(fmd.d_conf, "trace.frames", &tbp->tb_frames);
50 	(void) fmd_conf_getprop(fmd.d_conf, "trace.recs", &tbp->tb_recs);
51 
52 	/*
53 	 * We require 8-byte alignment of fmd_tracerec_t to store hrtime_t's.
54 	 * Since the trailing flexible array member is of type uintptr_t, we
55 	 * may need to allocate an additional element if we are compiling
56 	 * 32-bit; otherwise uintptr_t is 8 bytes so any value of tb_frames is
57 	 * acceptable.
58 	 *
59 	 * tb_frames includes the first element, whose size is reflected in
60 	 * sizeof (fmd_tracerec_t).  Therefore, if fmd_tracerec_t's size is
61 	 * 0 mod 8, we must be sure the total number of frames is odd.
62 	 * Otherwise, we need at least one extra frame, so the total count
63 	 * must be even.  This will continue to work even if the sizes or
64 	 * types of other fmd_tracerec_t members are changed.
65 	 */
66 #ifdef _ILP32
67 	/*CONSTCOND*/
68 	if (sizeof (fmd_tracerec_t) % sizeof (hrtime_t) == 0)
69 		tbp->tb_frames = (tbp->tb_frames & ~1UL) + 1;
70 	else
71 		tbp->tb_frames = P2ROUNDUP(tbp->tb_frames, 2);
72 #endif
73 	tbp->tb_size = sizeof (fmd_tracerec_t) +
74 	    sizeof (uintptr_t) * (MAX(tbp->tb_frames, 1) - 1);
75 
76 	bufsize = tbp->tb_size * tbp->tb_recs;
77 
78 	tbp->tb_buf = fmd_zalloc(bufsize, FMD_SLEEP);
79 	tbp->tb_end = (void *)((uintptr_t)tbp->tb_buf + bufsize - tbp->tb_size);
80 	tbp->tb_ptr = tbp->tb_buf;
81 
82 	return (tbp);
83 }
84 
85 void
86 fmd_trace_destroy(fmd_tracebuf_t *tbp)
87 {
88 	fmd_free(tbp->tb_buf, tbp->tb_size * tbp->tb_recs);
89 	fmd_free(tbp, sizeof (fmd_tracebuf_t));
90 }
91 
92 /*
93  * Callback for walkcontext(3C) to store the stack trace.  We use tr_tag below
94  * to store the maximum value of depth that is permitted so we can use it here.
95  */
96 /*ARGSUSED*/
97 static int
98 fmd_trace_frame(uintptr_t pc, int sig, fmd_tracerec_t *trp)
99 {
100 	trp->tr_stack[trp->tr_depth++] = pc;
101 	return (trp->tr_depth >= trp->tr_tag);
102 }
103 
104 /*ARGSUSED*/
105 fmd_tracerec_t *
106 fmd_trace_none(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
107 {
108 	return (NULL);
109 }
110 
111 fmd_tracerec_t *
112 fmd_trace_lite(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
113 {
114 	hrtime_t time = gethrtime();
115 	fmd_tracerec_t *trp = tbp->tb_ptr;
116 	char *p;
117 
118 	if (tbp->tb_depth++ != 0) {
119 		tbp->tb_depth--;
120 		return (NULL);
121 	}
122 
123 	trp->tr_time = time;
124 	trp->tr_file = NULL;
125 	trp->tr_line = 0;
126 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
127 	trp->tr_tag = fmd_ntz32(tag);
128 
129 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
130 	p = &trp->tr_msg[strlen(trp->tr_msg)];
131 	if (p > trp->tr_msg && p[-1] == '\n')
132 		p[-1] = '\0';
133 
134 	if (tbp->tb_ptr != tbp->tb_end)
135 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
136 	else
137 		tbp->tb_ptr = tbp->tb_buf;
138 
139 	tbp->tb_depth--;
140 	return (trp);
141 }
142 
143 fmd_tracerec_t *
144 fmd_trace_full(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
145 {
146 	hrtime_t time = gethrtime();
147 	fmd_tracerec_t *trp = tbp->tb_ptr;
148 	ucontext_t uc;
149 	char *p;
150 
151 	if (tbp->tb_depth++ != 0) {
152 		tbp->tb_depth--;
153 		return (NULL);
154 	}
155 
156 	(void) getcontext(&uc);
157 	trp->tr_tag = tbp->tb_frames; /* for use by fmd_trace_frame() */
158 	(void) walkcontext(&uc, (int (*)())fmd_trace_frame, trp);
159 
160 	trp->tr_time = time;
161 	trp->tr_file = NULL;
162 	trp->tr_line = 0;
163 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
164 	trp->tr_tag = fmd_ntz32(tag);
165 
166 	if (fmd.d_fmd_debug & FMD_DBG_TRACE)
167 		fmd_vdprintf(tag, format, ap);
168 
169 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
170 	p = &trp->tr_msg[strlen(trp->tr_msg)];
171 	if (p > trp->tr_msg && p[-1] == '\n')
172 		p[-1] = '\0';
173 
174 	if (tbp->tb_ptr != tbp->tb_end)
175 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
176 	else
177 		tbp->tb_ptr = tbp->tb_buf;
178 
179 	tbp->tb_depth--;
180 	return (trp);
181 }
182