Always use lttng_get_probe_list_head to get probe list
[lttng-ust.git] / liblttng-ust / lttng-probes.c
1 /*
2 * lttng-probes.c
3 *
4 * Holds LTTng probes registry.
5 *
6 * Copyright 2010-2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <string.h>
24 #include <errno.h>
25 #include <urcu/list.h>
26 #include <urcu/hlist.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/tracepoint.h>
29 #include "tracepoint-internal.h"
30 #include <assert.h>
31 #include <helper.h>
32 #include <ctype.h>
33
34 #include "lttng-tracer-core.h"
35 #include "jhash.h"
36 #include "error.h"
37
38 /*
39 * probe list is protected by ust_lock()/ust_unlock().
40 */
41 static CDS_LIST_HEAD(_probe_list);
42
43 struct cds_list_head *lttng_get_probe_list_head(void)
44 {
45 return &_probe_list;
46 }
47
48 static
49 const struct lttng_probe_desc *find_provider(const char *provider)
50 {
51 struct lttng_probe_desc *iter;
52 struct cds_list_head *probe_list;
53
54 probe_list = lttng_get_probe_list_head();
55 cds_list_for_each_entry(iter, probe_list, head) {
56 if (!strcmp(iter->provider, provider))
57 return iter;
58 }
59 return NULL;
60 }
61
62 static
63 int check_event_provider(struct lttng_probe_desc *desc)
64 {
65 int i;
66 size_t provider_name_len;
67
68 provider_name_len = strnlen(desc->provider,
69 LTTNG_UST_SYM_NAME_LEN - 1);
70 for (i = 0; i < desc->nr_events; i++) {
71 if (strncmp(desc->event_desc[i]->name,
72 desc->provider,
73 provider_name_len))
74 return 0; /* provider mismatch */
75 }
76 return 1;
77 }
78
79 int lttng_probe_register(struct lttng_probe_desc *desc)
80 {
81 struct lttng_probe_desc *iter;
82 int ret = 0;
83 int i;
84 struct cds_list_head *probe_list;
85
86 ust_lock();
87
88 /*
89 * Check if the provider has already been registered.
90 */
91 if (find_provider(desc->provider)) {
92 ret = -EEXIST;
93 goto end;
94 }
95
96 /*
97 * Each provider enforce that every event name begins with the
98 * provider name. Check this in an assertion for extra
99 * carefulness. This ensures we cannot have duplicate event
100 * names across providers.
101 */
102 assert(check_event_provider(desc));
103
104 /*
105 * The provider ensures there are no duplicate event names.
106 * Duplicated TRACEPOINT_EVENT event names would generate a
107 * compile-time error due to duplicated symbol names.
108 */
109
110 /*
111 * We sort the providers by struct lttng_probe_desc pointer
112 * address.
113 */
114 probe_list = lttng_get_probe_list_head();
115 cds_list_for_each_entry_reverse(iter, probe_list, head) {
116 BUG_ON(iter == desc); /* Should never be in the list twice */
117 if (iter < desc) {
118 /* We belong to the location right after iter. */
119 cds_list_add(&desc->head, &iter->head);
120 goto desc_added;
121 }
122 }
123 /* We should be added at the head of the list */
124 cds_list_add(&desc->head, probe_list);
125 desc_added:
126 DBG("just registered probe %s containing %u events",
127 desc->provider, desc->nr_events);
128 /*
129 * fix the events awaiting probe load.
130 */
131 for (i = 0; i < desc->nr_events; i++) {
132 const struct lttng_event_desc *ed;
133
134 ed = desc->event_desc[i];
135 DBG("Registered event probe \"%s\" with signature \"%s\"",
136 ed->name, ed->signature);
137 ret = lttng_fix_pending_event_desc(ed);
138 assert(!ret);
139 }
140 end:
141 ust_unlock();
142 return ret;
143 }
144
145 /* Backward compatibility with UST 2.0 */
146 int ltt_probe_register(struct lttng_probe_desc *desc)
147 {
148 return lttng_probe_register(desc);
149 }
150
151 void lttng_probe_unregister(struct lttng_probe_desc *desc)
152 {
153 ust_lock();
154 cds_list_del(&desc->head);
155 DBG("just unregistered probe %s", desc->provider);
156 ust_unlock();
157 }
158
159 /* Backward compatibility with UST 2.0 */
160 void ltt_probe_unregister(struct lttng_probe_desc *desc)
161 {
162 lttng_probe_unregister(desc);
163 }
164
165 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
166 {
167 struct tp_list_entry *list_entry, *tmp;
168
169 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
170 cds_list_del(&list_entry->head);
171 free(list_entry);
172 }
173 }
174
175 /*
176 * called with UST lock held.
177 */
178 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
179 {
180 struct lttng_probe_desc *probe_desc;
181 int i;
182 struct cds_list_head *probe_list;
183
184 probe_list = lttng_get_probe_list_head();
185 CDS_INIT_LIST_HEAD(&list->head);
186 cds_list_for_each_entry(probe_desc, probe_list, head) {
187 for (i = 0; i < probe_desc->nr_events; i++) {
188 struct tp_list_entry *list_entry;
189
190 list_entry = zmalloc(sizeof(*list_entry));
191 if (!list_entry)
192 goto err_nomem;
193 cds_list_add(&list_entry->head, &list->head);
194 strncpy(list_entry->tp.name,
195 probe_desc->event_desc[i]->name,
196 LTTNG_UST_SYM_NAME_LEN);
197 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
198 if (!probe_desc->event_desc[i]->loglevel) {
199 list_entry->tp.loglevel = TRACE_DEFAULT;
200 } else {
201 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
202 }
203 }
204 }
205 if (cds_list_empty(&list->head))
206 list->iter = NULL;
207 else
208 list->iter =
209 cds_list_first_entry(&list->head, struct tp_list_entry, head);
210 return 0;
211
212 err_nomem:
213 lttng_probes_prune_event_list(list);
214 return -ENOMEM;
215 }
216
217 /*
218 * Return current iteration position, advance internal iterator to next.
219 * Return NULL if end of list.
220 */
221 struct lttng_ust_tracepoint_iter *
222 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
223 {
224 struct tp_list_entry *entry;
225
226 if (!list->iter)
227 return NULL;
228 entry = list->iter;
229 if (entry->head.next == &list->head)
230 list->iter = NULL;
231 else
232 list->iter = cds_list_entry(entry->head.next,
233 struct tp_list_entry, head);
234 return &entry->tp;
235 }
236
237 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
238 {
239 struct tp_field_list_entry *list_entry, *tmp;
240
241 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
242 cds_list_del(&list_entry->head);
243 free(list_entry);
244 }
245 }
246
247 /*
248 * called with UST lock held.
249 */
250 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
251 {
252 struct lttng_probe_desc *probe_desc;
253 int i;
254 struct cds_list_head *probe_list;
255
256 probe_list = lttng_get_probe_list_head();
257 CDS_INIT_LIST_HEAD(&list->head);
258 cds_list_for_each_entry(probe_desc, probe_list, head) {
259 for (i = 0; i < probe_desc->nr_events; i++) {
260 const struct lttng_event_desc *event_desc =
261 probe_desc->event_desc[i];
262 int j;
263
264 if (event_desc->nr_fields == 0) {
265 /* Events without fields. */
266 struct tp_field_list_entry *list_entry;
267
268 list_entry = zmalloc(sizeof(*list_entry));
269 if (!list_entry)
270 goto err_nomem;
271 cds_list_add(&list_entry->head, &list->head);
272 strncpy(list_entry->field.event_name,
273 event_desc->name,
274 LTTNG_UST_SYM_NAME_LEN);
275 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
276 list_entry->field.field_name[0] = '\0';
277 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
278 if (!event_desc->loglevel) {
279 list_entry->field.loglevel = TRACE_DEFAULT;
280 } else {
281 list_entry->field.loglevel = *(*event_desc->loglevel);
282 }
283 list_entry->field.nowrite = 1;
284 }
285
286 for (j = 0; j < event_desc->nr_fields; j++) {
287 const struct lttng_event_field *event_field =
288 &event_desc->fields[j];
289 struct tp_field_list_entry *list_entry;
290
291 list_entry = zmalloc(sizeof(*list_entry));
292 if (!list_entry)
293 goto err_nomem;
294 cds_list_add(&list_entry->head, &list->head);
295 strncpy(list_entry->field.event_name,
296 event_desc->name,
297 LTTNG_UST_SYM_NAME_LEN);
298 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
299 strncpy(list_entry->field.field_name,
300 event_field->name,
301 LTTNG_UST_SYM_NAME_LEN);
302 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
303 switch (event_field->type.atype) {
304 case atype_integer:
305 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
306 break;
307 case atype_string:
308 list_entry->field.type = LTTNG_UST_FIELD_STRING;
309 break;
310 case atype_array:
311 if (event_field->type.u.array.elem_type.atype != atype_integer
312 || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
313 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
314 else
315 list_entry->field.type = LTTNG_UST_FIELD_STRING;
316 break;
317 case atype_sequence:
318 if (event_field->type.u.sequence.elem_type.atype != atype_integer
319 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
320 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
321 else
322 list_entry->field.type = LTTNG_UST_FIELD_STRING;
323 break;
324 case atype_float:
325 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
326 break;
327 case atype_enum:
328 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
329 break;
330 default:
331 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
332 }
333 if (!event_desc->loglevel) {
334 list_entry->field.loglevel = TRACE_DEFAULT;
335 } else {
336 list_entry->field.loglevel = *(*event_desc->loglevel);
337 }
338 list_entry->field.nowrite = event_field->nowrite;
339 }
340 }
341 }
342 if (cds_list_empty(&list->head))
343 list->iter = NULL;
344 else
345 list->iter =
346 cds_list_first_entry(&list->head,
347 struct tp_field_list_entry, head);
348 return 0;
349
350 err_nomem:
351 lttng_probes_prune_field_list(list);
352 return -ENOMEM;
353 }
354
355 /*
356 * Return current iteration position, advance internal iterator to next.
357 * Return NULL if end of list.
358 */
359 struct lttng_ust_field_iter *
360 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
361 {
362 struct tp_field_list_entry *entry;
363
364 if (!list->iter)
365 return NULL;
366 entry = list->iter;
367 if (entry->head.next == &list->head)
368 list->iter = NULL;
369 else
370 list->iter = cds_list_entry(entry->head.next,
371 struct tp_field_list_entry, head);
372 return &entry->field;
373 }
This page took 0.037293 seconds and 5 git commands to generate.