f028ccab8cadd967971cb3f673b883f622b03f7e
[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 /*
44 * List of probes registered by not yet processed.
45 */
46 static CDS_LIST_HEAD(lazy_probe_init);
47
48 /*
49 * lazy_nesting counter ensures we don't trigger lazy probe registration
50 * fixup while we are performing the fixup. It is protected by the ust
51 * mutex.
52 */
53 static int lazy_nesting;
54
55 /*
56 * Called under ust lock.
57 */
58 static
59 int check_event_provider(struct lttng_probe_desc *desc)
60 {
61 int i;
62 size_t provider_name_len;
63
64 provider_name_len = strnlen(desc->provider,
65 LTTNG_UST_SYM_NAME_LEN - 1);
66 for (i = 0; i < desc->nr_events; i++) {
67 if (strncmp(desc->event_desc[i]->name,
68 desc->provider,
69 provider_name_len))
70 return 0; /* provider mismatch */
71 }
72 return 1;
73 }
74
75 /*
76 * Called under ust lock.
77 */
78 static
79 void lttng_lazy_probe_register(struct lttng_probe_desc *desc)
80 {
81 struct lttng_probe_desc *iter;
82 struct cds_list_head *probe_list;
83
84 /*
85 * Each provider enforce that every event name begins with the
86 * provider name. Check this in an assertion for extra
87 * carefulness. This ensures we cannot have duplicate event
88 * names across providers.
89 */
90 assert(check_event_provider(desc));
91
92 /*
93 * The provider ensures there are no duplicate event names.
94 * Duplicated TRACEPOINT_EVENT event names would generate a
95 * compile-time error due to duplicated symbol names.
96 */
97
98 /*
99 * We sort the providers by struct lttng_probe_desc pointer
100 * address.
101 */
102 probe_list = &_probe_list;
103 cds_list_for_each_entry_reverse(iter, probe_list, head) {
104 BUG_ON(iter == desc); /* Should never be in the list twice */
105 if (iter < desc) {
106 /* We belong to the location right after iter. */
107 cds_list_add(&desc->head, &iter->head);
108 goto desc_added;
109 }
110 }
111 /* We should be added at the head of the list */
112 cds_list_add(&desc->head, probe_list);
113 desc_added:
114 DBG("just registered probe %s containing %u events",
115 desc->provider, desc->nr_events);
116 }
117
118 /*
119 * Called under ust lock.
120 */
121 static
122 void fixup_lazy_probes(void)
123 {
124 struct lttng_probe_desc *iter, *tmp;
125 int ret;
126
127 lazy_nesting++;
128 cds_list_for_each_entry_safe(iter, tmp,
129 &lazy_probe_init, lazy_init_head) {
130 lttng_lazy_probe_register(iter);
131 iter->lazy = 0;
132 cds_list_del(&iter->lazy_init_head);
133 }
134 ret = lttng_fix_pending_events();
135 assert(!ret);
136 lazy_nesting--;
137 }
138
139 /*
140 * Called under ust lock.
141 */
142 struct cds_list_head *lttng_get_probe_list_head(void)
143 {
144 if (!lazy_nesting && !cds_list_empty(&lazy_probe_init))
145 fixup_lazy_probes();
146 return &_probe_list;
147 }
148
149 static
150 const struct lttng_probe_desc *find_provider(const char *provider)
151 {
152 struct lttng_probe_desc *iter;
153 struct cds_list_head *probe_list;
154
155 probe_list = lttng_get_probe_list_head();
156 cds_list_for_each_entry(iter, probe_list, head) {
157 if (!strcmp(iter->provider, provider))
158 return iter;
159 }
160 return NULL;
161 }
162
163 int lttng_probe_register(struct lttng_probe_desc *desc)
164 {
165 int ret = 0;
166
167 ust_lock();
168
169 /*
170 * Check if the provider has already been registered.
171 */
172 if (find_provider(desc->provider)) {
173 ret = -EEXIST;
174 goto end;
175 }
176 cds_list_add(&desc->lazy_init_head, &lazy_probe_init);
177 desc->lazy = 1;
178 DBG("adding probe %s containing %u events to lazy registration list",
179 desc->provider, desc->nr_events);
180 /*
181 * If there is at least one active session, we need to register
182 * the probe immediately, since we cannot delay event
183 * registration because they are needed ASAP.
184 */
185 if (lttng_session_active())
186 fixup_lazy_probes();
187 end:
188 ust_unlock();
189 return ret;
190 }
191
192 /* Backward compatibility with UST 2.0 */
193 int ltt_probe_register(struct lttng_probe_desc *desc)
194 {
195 return lttng_probe_register(desc);
196 }
197
198 void lttng_probe_unregister(struct lttng_probe_desc *desc)
199 {
200 ust_lock();
201 if (!desc->lazy)
202 cds_list_del(&desc->head);
203 else
204 cds_list_del(&desc->lazy_init_head);
205 DBG("just unregistered probe %s", desc->provider);
206 ust_unlock();
207 }
208
209 /* Backward compatibility with UST 2.0 */
210 void ltt_probe_unregister(struct lttng_probe_desc *desc)
211 {
212 lttng_probe_unregister(desc);
213 }
214
215 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
216 {
217 struct tp_list_entry *list_entry, *tmp;
218
219 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
220 cds_list_del(&list_entry->head);
221 free(list_entry);
222 }
223 }
224
225 /*
226 * called with UST lock held.
227 */
228 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
229 {
230 struct lttng_probe_desc *probe_desc;
231 int i;
232 struct cds_list_head *probe_list;
233
234 probe_list = lttng_get_probe_list_head();
235 CDS_INIT_LIST_HEAD(&list->head);
236 cds_list_for_each_entry(probe_desc, probe_list, head) {
237 for (i = 0; i < probe_desc->nr_events; i++) {
238 struct tp_list_entry *list_entry;
239
240 list_entry = zmalloc(sizeof(*list_entry));
241 if (!list_entry)
242 goto err_nomem;
243 cds_list_add(&list_entry->head, &list->head);
244 strncpy(list_entry->tp.name,
245 probe_desc->event_desc[i]->name,
246 LTTNG_UST_SYM_NAME_LEN);
247 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
248 if (!probe_desc->event_desc[i]->loglevel) {
249 list_entry->tp.loglevel = TRACE_DEFAULT;
250 } else {
251 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
252 }
253 }
254 }
255 if (cds_list_empty(&list->head))
256 list->iter = NULL;
257 else
258 list->iter =
259 cds_list_first_entry(&list->head, struct tp_list_entry, head);
260 return 0;
261
262 err_nomem:
263 lttng_probes_prune_event_list(list);
264 return -ENOMEM;
265 }
266
267 /*
268 * Return current iteration position, advance internal iterator to next.
269 * Return NULL if end of list.
270 */
271 struct lttng_ust_tracepoint_iter *
272 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
273 {
274 struct tp_list_entry *entry;
275
276 if (!list->iter)
277 return NULL;
278 entry = list->iter;
279 if (entry->head.next == &list->head)
280 list->iter = NULL;
281 else
282 list->iter = cds_list_entry(entry->head.next,
283 struct tp_list_entry, head);
284 return &entry->tp;
285 }
286
287 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
288 {
289 struct tp_field_list_entry *list_entry, *tmp;
290
291 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
292 cds_list_del(&list_entry->head);
293 free(list_entry);
294 }
295 }
296
297 /*
298 * called with UST lock held.
299 */
300 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
301 {
302 struct lttng_probe_desc *probe_desc;
303 int i;
304 struct cds_list_head *probe_list;
305
306 probe_list = lttng_get_probe_list_head();
307 CDS_INIT_LIST_HEAD(&list->head);
308 cds_list_for_each_entry(probe_desc, probe_list, head) {
309 for (i = 0; i < probe_desc->nr_events; i++) {
310 const struct lttng_event_desc *event_desc =
311 probe_desc->event_desc[i];
312 int j;
313
314 if (event_desc->nr_fields == 0) {
315 /* Events without fields. */
316 struct tp_field_list_entry *list_entry;
317
318 list_entry = zmalloc(sizeof(*list_entry));
319 if (!list_entry)
320 goto err_nomem;
321 cds_list_add(&list_entry->head, &list->head);
322 strncpy(list_entry->field.event_name,
323 event_desc->name,
324 LTTNG_UST_SYM_NAME_LEN);
325 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
326 list_entry->field.field_name[0] = '\0';
327 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
328 if (!event_desc->loglevel) {
329 list_entry->field.loglevel = TRACE_DEFAULT;
330 } else {
331 list_entry->field.loglevel = *(*event_desc->loglevel);
332 }
333 list_entry->field.nowrite = 1;
334 }
335
336 for (j = 0; j < event_desc->nr_fields; j++) {
337 const struct lttng_event_field *event_field =
338 &event_desc->fields[j];
339 struct tp_field_list_entry *list_entry;
340
341 list_entry = zmalloc(sizeof(*list_entry));
342 if (!list_entry)
343 goto err_nomem;
344 cds_list_add(&list_entry->head, &list->head);
345 strncpy(list_entry->field.event_name,
346 event_desc->name,
347 LTTNG_UST_SYM_NAME_LEN);
348 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
349 strncpy(list_entry->field.field_name,
350 event_field->name,
351 LTTNG_UST_SYM_NAME_LEN);
352 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
353 switch (event_field->type.atype) {
354 case atype_integer:
355 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
356 break;
357 case atype_string:
358 list_entry->field.type = LTTNG_UST_FIELD_STRING;
359 break;
360 case atype_array:
361 if (event_field->type.u.array.elem_type.atype != atype_integer
362 || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
363 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
364 else
365 list_entry->field.type = LTTNG_UST_FIELD_STRING;
366 break;
367 case atype_sequence:
368 if (event_field->type.u.sequence.elem_type.atype != atype_integer
369 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
370 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
371 else
372 list_entry->field.type = LTTNG_UST_FIELD_STRING;
373 break;
374 case atype_float:
375 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
376 break;
377 case atype_enum:
378 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
379 break;
380 default:
381 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
382 }
383 if (!event_desc->loglevel) {
384 list_entry->field.loglevel = TRACE_DEFAULT;
385 } else {
386 list_entry->field.loglevel = *(*event_desc->loglevel);
387 }
388 list_entry->field.nowrite = event_field->nowrite;
389 }
390 }
391 }
392 if (cds_list_empty(&list->head))
393 list->iter = NULL;
394 else
395 list->iter =
396 cds_list_first_entry(&list->head,
397 struct tp_field_list_entry, head);
398 return 0;
399
400 err_nomem:
401 lttng_probes_prune_field_list(list);
402 return -ENOMEM;
403 }
404
405 /*
406 * Return current iteration position, advance internal iterator to next.
407 * Return NULL if end of list.
408 */
409 struct lttng_ust_field_iter *
410 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
411 {
412 struct tp_field_list_entry *entry;
413
414 if (!list->iter)
415 return NULL;
416 entry = list->iter;
417 if (entry->head.next == &list->head)
418 list->iter = NULL;
419 else
420 list->iter = cds_list_entry(entry->head.next,
421 struct tp_field_list_entry, head);
422 return &entry->field;
423 }
This page took 0.040158 seconds and 3 git commands to generate.