xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_trace.c (revision 7f7322febbcfe774b7270abc3b191c094bfcc517)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 
33 #include <ucontext.h>
34 #include <strings.h>
35 #include <stdio.h>
36 #include <errno.h>
37 
38 #include <fmd_trace.h>
39 #include <fmd_alloc.h>
40 #include <fmd_subr.h>
41 #include <fmd_conf.h>
42 #include <fmd.h>
43 
44 fmd_tracebuf_t *
45 fmd_trace_create(void)
46 {
47 	fmd_tracebuf_t *tbp = fmd_zalloc(sizeof (fmd_tracebuf_t), FMD_SLEEP);
48 	size_t bufsize;
49 
50 	(void) fmd_conf_getprop(fmd.d_conf, "trace.frames", &tbp->tb_frames);
51 	(void) fmd_conf_getprop(fmd.d_conf, "trace.recs", &tbp->tb_recs);
52 
53 	/*
54 	 * We require 8-byte alignment of fmd_tracerec_t to store hrtime_t's.
55 	 * Since the trailing flexible array member is of type uintptr_t, we
56 	 * must make it a multiple of 2 if we are compiling 32-bit; otherwise
57 	 * uintptr_t is 8 bytes so any value of tb_frames is acceptable.
58 	 */
59 #ifdef _ILP32
60 	tbp->tb_frames = P2ROUNDUP(tbp->tb_frames, 2);
61 #endif
62 	tbp->tb_size = sizeof (fmd_tracerec_t) +
63 	    sizeof (uintptr_t) * (MAX(tbp->tb_frames, 1) - 1);
64 
65 	bufsize = tbp->tb_size * tbp->tb_recs;
66 
67 	tbp->tb_buf = fmd_zalloc(bufsize, FMD_SLEEP);
68 	tbp->tb_end = (void *)((uintptr_t)tbp->tb_buf + bufsize - tbp->tb_size);
69 	tbp->tb_ptr = tbp->tb_buf;
70 
71 	return (tbp);
72 }
73 
74 void
75 fmd_trace_destroy(fmd_tracebuf_t *tbp)
76 {
77 	fmd_free(tbp->tb_buf, tbp->tb_size * tbp->tb_recs);
78 	fmd_free(tbp, sizeof (fmd_tracebuf_t));
79 }
80 
81 /*
82  * Callback for walkcontext(3C) to store the stack trace.  We use tr_tag below
83  * to store the maximum value of depth that is permitted so we can use it here.
84  */
85 /*ARGSUSED*/
86 static int
87 fmd_trace_frame(uintptr_t pc, int sig, fmd_tracerec_t *trp)
88 {
89 	trp->tr_stack[trp->tr_depth++] = pc;
90 	return (trp->tr_depth >= trp->tr_tag);
91 }
92 
93 /*ARGSUSED*/
94 fmd_tracerec_t *
95 fmd_trace_none(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
96 {
97 	return (NULL);
98 }
99 
100 fmd_tracerec_t *
101 fmd_trace_lite(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
102 {
103 	hrtime_t time = gethrtime();
104 	fmd_tracerec_t *trp = tbp->tb_ptr;
105 	char *p;
106 
107 	if (tbp->tb_depth++ != 0) {
108 		tbp->tb_depth--;
109 		return (NULL);
110 	}
111 
112 	trp->tr_time = time;
113 	trp->tr_file = NULL;
114 	trp->tr_line = 0;
115 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
116 	trp->tr_tag = fmd_ntz32(tag);
117 
118 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
119 	p = &trp->tr_msg[strlen(trp->tr_msg)];
120 	if (p > trp->tr_msg && p[-1] == '\n')
121 		p[-1] = '\0';
122 
123 	if (tbp->tb_ptr != tbp->tb_end)
124 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
125 	else
126 		tbp->tb_ptr = tbp->tb_buf;
127 
128 	tbp->tb_depth--;
129 	return (trp);
130 }
131 
132 fmd_tracerec_t *
133 fmd_trace_full(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
134 {
135 	hrtime_t time = gethrtime();
136 	fmd_tracerec_t *trp = tbp->tb_ptr;
137 	ucontext_t uc;
138 	char *p;
139 
140 	if (tbp->tb_depth++ != 0) {
141 		tbp->tb_depth--;
142 		return (NULL);
143 	}
144 
145 	(void) getcontext(&uc);
146 	trp->tr_tag = tbp->tb_frames; /* for use by fmd_trace_frame() */
147 	(void) walkcontext(&uc, (int (*)())fmd_trace_frame, trp);
148 
149 	trp->tr_time = time;
150 	trp->tr_file = NULL;
151 	trp->tr_line = 0;
152 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
153 	trp->tr_tag = fmd_ntz32(tag);
154 
155 	if (fmd.d_fmd_debug & FMD_DBG_TRACE)
156 		fmd_vdprintf(tag, format, ap);
157 
158 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
159 	p = &trp->tr_msg[strlen(trp->tr_msg)];
160 	if (p > trp->tr_msg && p[-1] == '\n')
161 		p[-1] = '\0';
162 
163 	if (tbp->tb_ptr != tbp->tb_end)
164 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
165 	else
166 		tbp->tb_ptr = tbp->tb_buf;
167 
168 	tbp->tb_depth--;
169 	return (trp);
170 }
171