8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fm / fmd / common / fmd_trace.c
blobbea6e8d450d87709aed816356780a892cc39748f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <sys/sysmacros.h>
30 #include <ucontext.h>
31 #include <strings.h>
32 #include <stdio.h>
33 #include <errno.h>
35 #include <fmd_trace.h>
36 #include <fmd_alloc.h>
37 #include <fmd_subr.h>
38 #include <fmd_conf.h>
39 #include <fmd.h>
41 fmd_tracebuf_t *
42 fmd_trace_create(void)
44 fmd_tracebuf_t *tbp = fmd_zalloc(sizeof (fmd_tracebuf_t), FMD_SLEEP);
45 size_t bufsize;
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);
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.
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.
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);
74 bufsize = tbp->tb_size * tbp->tb_recs;
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;
80 return (tbp);
83 void
84 fmd_trace_destroy(fmd_tracebuf_t *tbp)
86 fmd_free(tbp->tb_buf, tbp->tb_size * tbp->tb_recs);
87 fmd_free(tbp, sizeof (fmd_tracebuf_t));
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.
94 /*ARGSUSED*/
95 static int
96 fmd_trace_frame(uintptr_t pc, int sig, fmd_tracerec_t *trp)
98 trp->tr_stack[trp->tr_depth++] = pc;
99 return (trp->tr_depth >= trp->tr_tag);
102 /*ARGSUSED*/
103 fmd_tracerec_t *
104 fmd_trace_none(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
106 return (NULL);
109 fmd_tracerec_t *
110 fmd_trace_lite(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
112 hrtime_t time = gethrtime();
113 fmd_tracerec_t *trp = tbp->tb_ptr;
114 char *p;
116 if (tbp->tb_depth++ != 0) {
117 tbp->tb_depth--;
118 return (NULL);
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);
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';
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;
137 tbp->tb_depth--;
138 return (trp);
141 fmd_tracerec_t *
142 fmd_trace_full(fmd_tracebuf_t *tbp, uint_t tag, const char *format, va_list ap)
144 hrtime_t time = gethrtime();
145 fmd_tracerec_t *trp = tbp->tb_ptr;
146 ucontext_t uc;
147 char *p;
149 if (tbp->tb_depth++ != 0) {
150 tbp->tb_depth--;
151 return (NULL);
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);
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);
165 if (fmd.d_fmd_debug & FMD_DBG_TRACE)
166 fmd_vdprintf(tag, format, ap);
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';
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;
178 tbp->tb_depth--;
179 return (trp);