xref: /titanic_51/usr/src/lib/libtnfprobe/tnf_trace.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #ifndef DEBUG
29 #define	NDEBUG	1
30 #endif
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <tnf/com.h>
35 #include <tnf/writer.h>
36 #include <tnf/probe.h>
37 #include <assert.h>
38 
39 #include "tnf_types.h"
40 #include "tnf_trace.h"
41 
42 #ifdef TNFWB_DEBUG
43 #ifdef _KERNEL
44 #error TNFWB_DEBUG
45 #else  /* _KERNEL */
46 #include <stdio.h>
47 #include <thread.h>
48 #endif /* _KERNEL */
49 #endif /* TNFW_DEBUG */
50 
51 
52 /*
53  * Defines
54  */
55 
56 #ifdef _KERNEL
57 #define	TNF_ASSERT(expr)	ASSERT(expr)
58 #else
59 #define	TNF_ASSERT(expr)	assert(expr)
60 #endif
61 
62 /*
63  * New properties need for tracing
64  */
65 
66 static tnf_tag_data_t	**derived_tagarg_properties[] = {
67 	&TAG_DATA(tnf_derived),
68 	&TAG_DATA(tnf_tag_arg),
69 	0
70 };
71 
72 static tnf_tag_data_t	***derived_tagarg_properties_ptr =
73 					derived_tagarg_properties;
74 
75 /*
76  * New derived types for probes
77  */
78 
79 TNF_STD_DERIVED_TAG(tnf_probe_event, tnf_tag,
80 		derived_tagarg_properties_ptr, TNF_OPAQUE);
81 
82 TNF_STD_DERIVED_TAG(tnf_time_base, tnf_int64,
83 		tnf_derived_properties, TNF_INT64);
84 
85 TNF_STD_DERIVED_TAG(tnf_time_delta, tnf_uint32,
86 		tnf_derived_properties, TNF_UINT32);
87 
88 TNF_STD_DERIVED_TAG(tnf_pid, tnf_int32,
89 		tnf_derived_properties, TNF_INT32);
90 
91 TNF_STD_DERIVED_TAG(tnf_lwpid, tnf_uint32,
92 		tnf_derived_properties, TNF_UINT32);
93 
94 /*
95  * Schedule records -CAUTION- keep in sync with tnf_internal.h
96  * Note that tnf_schedule_prototype_t has different sizes for
97  * kernel and user.
98  */
99 
100 static char	*user_schedule_slot_names[] = {
101 	TNF_N_TAG,
102 	TNF_N_TID,
103 	TNF_N_LWPID,
104 	TNF_N_PID,
105 	TNF_N_TIME_BASE,
106 	0};
107 
108 static tnf_tag_data_t	**user_schedule_slots[] = {
109 	&TAG_DATA(tnf_tag),		/* tag			*/
110 	&TAG_DATA(tnf_uint32),		/* tid XXX		*/
111 	&TAG_DATA(tnf_lwpid),		/* lwpid 		*/
112 	&TAG_DATA(tnf_pid),		/* pid 			*/
113 	&TAG_DATA(tnf_time_base),	/* time_base 		*/
114 	0};
115 
116 TNF_STD_STRUCT_TAG(tnf_sched_rec,
117 		user_schedule_slots,
118 		user_schedule_slot_names,
119 		sizeof (tnf_schedule_prototype_t));
120 
121 /*
122  * Probe type record (metatag)
123  */
124 
125 static tnf_tag_data_t	**probe_type_slots[] = {
126 	&TAG_DATA(tnf_tag),
127 	&TAG_DATA(tnf_name),
128 	&TAG_DATA(tnf_properties),
129 	&TAG_DATA(tnf_slot_types),
130 	&TAG_DATA(tnf_type_size),
131 	&TAG_DATA(tnf_slot_names),
132 	&TAG_DATA(tnf_string),  	/* detail */
133 	0};
134 
135 TNF_STRUCT_TAG(tnf_probe_type,
136 		tnf_type_properties,
137 		probe_type_slots,
138 		0,
139 		sizeof (tnf_probe_prototype_t));
140 
141 /*
142  * export all tags
143  */
144 
145 TAG_EXPORT(tnf_probe_event);
146 TAG_EXPORT(tnf_time_base);
147 TAG_EXPORT(tnf_time_delta);
148 TAG_EXPORT(tnf_pid);
149 TAG_EXPORT(tnf_lwpid);
150 
151 TAG_EXPORT(tnf_sched_rec);
152 TAG_EXPORT(tnf_probe_type);
153 
154 /*
155  * Write a schedule record
156  * Can only be written in reusable data space.
157  */
158 
159 tnf_record_p
160 tnf_schedule_write(tnf_ops_t *ops, tnf_schedule_t *sched)
161 {
162 	tnf_tag_data_t *metatag_data;
163 	tnf_record_p metatag_index;
164 	tnf_schedule_prototype_t *buffer;
165 
166 #ifdef _TNF_VERBOSE
167 	fprintf(stderr, "tnf_schedule_write: \n");
168 #endif
169 	/* Cannot be called when writing into tag space */
170 	TNF_ASSERT(ops->mode == TNF_ALLOC_REUSABLE);
171 	ALLOC(ops, sizeof (*buffer), buffer, sched->record_p, ops->mode);
172 
173 	metatag_data = TAG_DATA(tnf_sched_rec);
174 
175 	metatag_index = metatag_data->tag_index ?
176 		metatag_data->tag_index :
177 		metatag_data->tag_desc(ops, metatag_data);
178 
179 	ASSIGN(buffer,	tag, 		metatag_index);
180 	/* LINTED - tid is 32 bits */
181 	ASSIGN2(buffer, tid, 		sched->tid,		uint32);
182 	/* LINTED - lwpid is 32 bits */
183 	ASSIGN(buffer,	lwpid, 		sched->lwpid);
184 	/* LINTED - pid is 32 bits */
185 	ASSIGN(buffer,	pid, 		sched->pid);
186 	ASSIGN(buffer,	time_base, 	sched->time_base);
187 
188 	/*
189 	 * Remember schedule record generation number so the distance
190 	 * in virtual space can be calculated from an event record
191 	 */
192 	sched->record_gen = ((tnf_block_header_t *)
193 	    ((uintptr_t)buffer & TNF_BLOCK_MASK))->generation;
194 	/* Cannot have been written into tag space */
195 	TNF_ASSERT(sched->record_gen != TNF_TAG_GENERATION_NUM);
196 
197 	return ((tnf_record_p) buffer);
198 }
199