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