xref: /spdk/lib/trace/trace_flags.c (revision 9c2aea2ad581e78982146835ecab3b12107d25d6)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/env.h"
37 #include "spdk/trace.h"
38 #include "spdk/log.h"
39 
40 struct spdk_trace_flags *g_trace_flags = NULL;
41 static struct spdk_trace_register_fn *g_reg_fn_head = NULL;
42 
43 uint64_t
44 spdk_trace_get_tpoint_mask(uint32_t group_id)
45 {
46 	if (group_id >= SPDK_TRACE_MAX_GROUP_ID) {
47 		SPDK_ERRLOG("%s: invalid group ID %d\n", __func__, group_id);
48 		return 0ULL;
49 	}
50 
51 	return g_trace_flags->tpoint_mask[group_id];
52 }
53 
54 void
55 spdk_trace_set_tpoints(uint32_t group_id, uint64_t tpoint_mask)
56 {
57 	if (group_id >= SPDK_TRACE_MAX_GROUP_ID) {
58 		SPDK_ERRLOG("%s: invalid group ID %d\n", __func__, group_id);
59 		return;
60 	}
61 
62 	g_trace_flags->tpoint_mask[group_id] |= tpoint_mask;
63 }
64 
65 void
66 spdk_trace_clear_tpoints(uint32_t group_id, uint64_t tpoint_mask)
67 {
68 	if (group_id >= SPDK_TRACE_MAX_GROUP_ID) {
69 		SPDK_ERRLOG("%s: invalid group ID %d\n", __func__, group_id);
70 		return;
71 	}
72 
73 	g_trace_flags->tpoint_mask[group_id] &= ~tpoint_mask;
74 }
75 
76 uint64_t
77 spdk_trace_get_tpoint_group_mask(void)
78 {
79 	uint64_t mask = 0x0;
80 	int i;
81 
82 	for (i = 0; i < SPDK_TRACE_MAX_GROUP_ID; i++) {
83 		if (spdk_trace_get_tpoint_mask(i) != 0) {
84 			mask |= (1ULL << i);
85 		}
86 	}
87 
88 	return mask;
89 }
90 
91 void
92 spdk_trace_set_tpoint_group_mask(uint64_t tpoint_group_mask)
93 {
94 	int i;
95 
96 	for (i = 0; i < SPDK_TRACE_MAX_GROUP_ID; i++) {
97 		if (tpoint_group_mask & (1ULL << i)) {
98 			spdk_trace_set_tpoints(i, -1ULL);
99 		}
100 	}
101 }
102 
103 void
104 spdk_trace_mask_usage(FILE *f, const char *tmask_arg)
105 {
106 	struct spdk_trace_register_fn *register_fn;
107 
108 	fprintf(f, " %s, --tpoint-group-mask <mask>\n", tmask_arg);
109 	fprintf(f, "                           tracepoint group mask for spdk trace buffers (default 0x0");
110 
111 	register_fn = g_reg_fn_head;
112 	while (register_fn) {
113 		fprintf(f, ", %s 0x%x", register_fn->name, 1 << register_fn->tgroup_id);
114 		register_fn = register_fn->next;
115 	}
116 
117 	fprintf(f, ", all 0xffff)\n");
118 }
119 
120 void
121 spdk_trace_register_owner(uint8_t type, char id_prefix)
122 {
123 	struct spdk_trace_owner *owner;
124 
125 	assert(type != OWNER_NONE);
126 
127 	/* 'owner' has 256 entries and since 'type' is a uint8_t, it
128 	 * can't overrun the array.
129 	 */
130 	owner = &g_trace_flags->owner[type];
131 	assert(owner->type == 0);
132 
133 	owner->type = type;
134 	owner->id_prefix = id_prefix;
135 }
136 
137 void
138 spdk_trace_register_object(uint8_t type, char id_prefix)
139 {
140 	struct spdk_trace_object *object;
141 
142 	assert(type != OBJECT_NONE);
143 
144 	/* 'object' has 256 entries and since 'type' is a uint8_t, it
145 	 * can't overrun the array.
146 	 */
147 	object = &g_trace_flags->object[type];
148 	assert(object->type == 0);
149 
150 	object->type = type;
151 	object->id_prefix = id_prefix;
152 }
153 
154 void
155 spdk_trace_register_description(const char *name, const char *short_name,
156 				uint16_t tpoint_id, uint8_t owner_type,
157 				uint8_t object_type, uint8_t new_object,
158 				uint8_t arg1_is_ptr, const char *arg1_name)
159 {
160 	struct spdk_trace_tpoint *tpoint;
161 
162 	assert(tpoint_id != 0);
163 	assert(tpoint_id < SPDK_TRACE_MAX_TPOINT_ID);
164 
165 	tpoint = &g_trace_flags->tpoint[tpoint_id];
166 	assert(tpoint->tpoint_id == 0);
167 
168 	snprintf(tpoint->name, sizeof(tpoint->name), "%s", name);
169 	snprintf(tpoint->short_name, sizeof(tpoint->short_name), "%s", short_name);
170 	tpoint->tpoint_id = tpoint_id;
171 	tpoint->object_type = object_type;
172 	tpoint->owner_type = owner_type;
173 	tpoint->new_object = new_object;
174 	tpoint->arg1_is_ptr = arg1_is_ptr;
175 	snprintf(tpoint->arg1_name, sizeof(tpoint->arg1_name), "%s", arg1_name);
176 }
177 
178 void
179 spdk_trace_add_register_fn(struct spdk_trace_register_fn *reg_fn)
180 {
181 	struct spdk_trace_register_fn *_reg_fn;
182 
183 	if (reg_fn->name == NULL) {
184 		SPDK_ERRLOG("missing name for registering spdk trace tpoint group\n");
185 		assert(false);
186 		return;
187 	}
188 
189 	/* Ensure that no trace point group IDs and names are ever duplicated */
190 	for (_reg_fn = g_reg_fn_head; _reg_fn; _reg_fn = _reg_fn->next) {
191 		if (reg_fn->tgroup_id == _reg_fn->tgroup_id) {
192 			SPDK_ERRLOG("duplicate tgroup_id (%d) with %s\n", _reg_fn->tgroup_id, _reg_fn->name);
193 			assert(false);
194 			return;
195 		}
196 
197 		if (strcmp(reg_fn->name, _reg_fn->name) == 0) {
198 			SPDK_ERRLOG("duplicate name with %s\n", _reg_fn->name);
199 			assert(false);
200 			return;
201 		}
202 	}
203 
204 	/* Arrange trace registration in order on tgroup_id */
205 	if (g_reg_fn_head == NULL || reg_fn->tgroup_id < g_reg_fn_head->tgroup_id) {
206 		reg_fn->next = g_reg_fn_head;
207 		g_reg_fn_head = reg_fn;
208 		return;
209 	}
210 
211 	for (_reg_fn = g_reg_fn_head; _reg_fn; _reg_fn = _reg_fn->next) {
212 		if (_reg_fn->next == NULL || reg_fn->tgroup_id < _reg_fn->next->tgroup_id) {
213 			reg_fn->next = _reg_fn->next;
214 			_reg_fn->next = reg_fn;
215 			return;
216 		}
217 	}
218 }
219 
220 void
221 spdk_trace_flags_init(void)
222 {
223 	struct spdk_trace_register_fn *reg_fn;
224 
225 	reg_fn = g_reg_fn_head;
226 	while (reg_fn) {
227 		reg_fn->reg_fn();
228 		reg_fn = reg_fn->next;
229 	}
230 }
231