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]
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>
35 #include <fmd_trace.h>
36 #include <fmd_alloc.h>
42 fmd_trace_create(void)
44 fmd_tracebuf_t
*tbp
= fmd_zalloc(sizeof (fmd_tracebuf_t
), FMD_SLEEP
);
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
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.
66 if (sizeof (fmd_tracerec_t
) % sizeof (hrtime_t
) == 0)
67 tbp
->tb_frames
= (tbp
->tb_frames
& ~1UL) + 1;
69 tbp
->tb_frames
= P2ROUNDUP(tbp
->tb_frames
, 2);
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
;
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.
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
);
104 fmd_trace_none(fmd_tracebuf_t
*tbp
, uint_t tag
, const char *format
, va_list ap
)
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
;
116 if (tbp
->tb_depth
++ != 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')
132 if (tbp
->tb_ptr
!= tbp
->tb_end
)
133 tbp
->tb_ptr
= (void *)((uintptr_t)tbp
->tb_ptr
+ tbp
->tb_size
);
135 tbp
->tb_ptr
= tbp
->tb_buf
;
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
;
149 if (tbp
->tb_depth
++ != 0) {
154 (void) getcontext(&uc
);
156 trp
->tr_tag
= tbp
->tb_frames
; /* for use by fmd_trace_frame() */
157 (void) walkcontext(&uc
, (int (*)())fmd_trace_frame
, trp
);
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')
173 if (tbp
->tb_ptr
!= tbp
->tb_end
)
174 tbp
->tb_ptr
= (void *)((uintptr_t)tbp
->tb_ptr
+ tbp
->tb_size
);
176 tbp
->tb_ptr
= tbp
->tb_buf
;