xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_trace.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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  * Copyright 2004 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 	 * must make it a multiple of 2 if we are compiling 32-bit; otherwise
56 	 * uintptr_t is 8 bytes so any value of tb_frames is acceptable.
57 	 */
58 #ifdef _ILP32
59 	tbp->tb_frames = P2ROUNDUP(tbp->tb_frames, 2);
60 #endif
61 	tbp->tb_size = sizeof (fmd_tracerec_t) +
62 	    sizeof (uintptr_t) * (MAX(tbp->tb_frames, 1) - 1);
63 
64 	bufsize = tbp->tb_size * tbp->tb_recs;
65 
66 	tbp->tb_buf = fmd_zalloc(bufsize, FMD_SLEEP);
67 	tbp->tb_end = (void *)((uintptr_t)tbp->tb_buf + bufsize - tbp->tb_size);
68 	tbp->tb_ptr = tbp->tb_buf;
69 
70 	return (tbp);
71 }
72 
73 void
74 fmd_trace_destroy(fmd_tracebuf_t *tbp)
75 {
76 	fmd_free(tbp->tb_buf, tbp->tb_size * tbp->tb_recs);
77 	fmd_free(tbp, sizeof (fmd_tracebuf_t));
78 }
79 
80 /*
81  * Callback for walkcontext(3C) to store the stack trace.  We use tr_tag below
82  * to store the maximum value of depth that is permitted so we can use it here.
83  */
84 /*ARGSUSED*/
85 static int
86 fmd_trace_frame(uintptr_t pc, int sig, fmd_tracerec_t *trp)
87 {
88 	trp->tr_stack[trp->tr_depth++] = pc;
89 	return (trp->tr_depth >= trp->tr_tag);
90 }
91 
92 /*ARGSUSED*/
93 fmd_tracerec_t *
94 fmd_trace_none(fmd_tracebuf_t *tbp, uint8_t tag, const char *format, va_list ap)
95 {
96 	return (NULL);
97 }
98 
99 fmd_tracerec_t *
100 fmd_trace_lite(fmd_tracebuf_t *tbp, uint8_t tag, const char *format, va_list ap)
101 {
102 	hrtime_t time = gethrtime();
103 	fmd_tracerec_t *trp = tbp->tb_ptr;
104 	char *p;
105 
106 	trp->tr_time = time;
107 	trp->tr_file = NULL;
108 	trp->tr_line = 0;
109 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
110 	trp->tr_tag = tag;
111 
112 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
113 	p = &trp->tr_msg[strlen(trp->tr_msg)];
114 	if (p > trp->tr_msg && p[-1] == '\n')
115 		p[-1] = '\0';
116 
117 	if (tbp->tb_ptr != tbp->tb_end)
118 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
119 	else
120 		tbp->tb_ptr = tbp->tb_buf;
121 
122 	return (trp);
123 }
124 
125 fmd_tracerec_t *
126 fmd_trace_full(fmd_tracebuf_t *tbp, uint8_t tag, const char *format, va_list ap)
127 {
128 	hrtime_t time = gethrtime();
129 	fmd_tracerec_t *trp = tbp->tb_ptr;
130 	ucontext_t uc;
131 	char *p;
132 
133 	(void) getcontext(&uc);
134 	trp->tr_tag = tbp->tb_frames; /* for use by fmd_trace_frame() */
135 	(void) walkcontext(&uc, (int (*)())fmd_trace_frame, trp);
136 
137 	trp->tr_time = time;
138 	trp->tr_file = NULL;
139 	trp->tr_line = 0;
140 	trp->tr_errno = (tag == FMD_DBG_ERR) ? errno : 0;
141 	trp->tr_tag = tag;
142 
143 	(void) vsnprintf(trp->tr_msg, sizeof (trp->tr_msg), format, ap);
144 	p = &trp->tr_msg[strlen(trp->tr_msg)];
145 	if (p > trp->tr_msg && p[-1] == '\n')
146 		p[-1] = '\0';
147 
148 	if (tbp->tb_ptr != tbp->tb_end)
149 		tbp->tb_ptr = (void *)((uintptr_t)tbp->tb_ptr + tbp->tb_size);
150 	else
151 		tbp->tb_ptr = tbp->tb_buf;
152 
153 	return (trp);
154 }
155