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