doc/man: remove duplicate copyrights section
[lttng-ust.git] / liblttng-ust / lttng-events.c
CommitLineData
8020ceb5 1/*
7dd08bec 2 * lttng-events.c
8020ceb5 3 *
8020ceb5
MD
4 * Holds LTTng per-session event registry.
5 *
e92f3e28
MD
6 * Copyright (C) 2010-2012 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
8020ceb5
MD
21 */
22
b5234c06
MD
23#define _GNU_SOURCE
24#include <stdio.h>
25#include <urcu/list.h>
8165c8da 26#include <urcu/hlist.h>
b5234c06 27#include <pthread.h>
b5234c06 28#include <errno.h>
f4681817
MD
29#include <sys/shm.h>
30#include <sys/ipc.h>
44c72f10
MD
31#include <stdint.h>
32#include <stddef.h>
28b12049
MD
33#include <inttypes.h>
34#include <time.h>
2ae57758 35#include <lttng/ust-endian.h>
28b12049 36#include "clock.h"
44c72f10
MD
37
38#include <urcu-bp.h>
39#include <urcu/compiler.h>
40#include <urcu/uatomic.h>
41#include <urcu/arch.h>
42
43#include <lttng/tracepoint.h>
4318ae1b 44#include <lttng/ust-events.h>
44c72f10
MD
45
46#include <usterr-signal-safe.h>
47#include <helper.h>
7d3e35b6 48#include <lttng/ust-ctl.h>
32ce8569 49#include <ust-comm.h>
53569322
MD
50#include <lttng/ust-dynamic-type.h>
51#include <lttng/ust-context-provider.h>
1f18504e 52#include "error.h"
08114193 53#include "compat.h"
eda498b8 54#include "lttng-ust-uuid.h"
44c72f10 55
457a6b58 56#include "tracepoint-internal.h"
7dd08bec
MD
57#include "lttng-tracer.h"
58#include "lttng-tracer-core.h"
cf73e0fe 59#include "lttng-ust-statedump.h"
b728d87e 60#include "wait.h"
8d8a24c8 61#include "../libringbuffer/shm.h"
596c4223 62#include "jhash.h"
8165c8da
MD
63
64/*
3327ac33
MD
65 * All operations within this file are called by the communication
66 * thread, under ust_lock protection.
8165c8da 67 */
8165c8da 68
b5234c06 69static CDS_LIST_HEAD(sessions);
8165c8da 70
37dddb65
MD
71struct cds_list_head *_lttng_get_sessions(void)
72{
73 return &sessions;
74}
75
7dd08bec 76static void _lttng_event_destroy(struct lttng_event *event);
c785c634 77static void _lttng_enum_destroy(struct lttng_enum *_enum);
8020ceb5 78
e58095ef
MD
79static
80void lttng_session_lazy_sync_enablers(struct lttng_session *session);
81static
82void lttng_session_sync_enablers(struct lttng_session *session);
83static
84void lttng_enabler_destroy(struct lttng_enabler *enabler);
85
6715d7d1
MD
86/*
87 * Called with ust lock held.
88 */
89int lttng_session_active(void)
90{
91 struct lttng_session *iter;
92
93 cds_list_for_each_entry(iter, &sessions, node) {
94 if (iter->active)
95 return 1;
96 }
97 return 0;
98}
99
e58095ef
MD
100static
101int lttng_loglevel_match(int loglevel,
102 unsigned int has_loglevel,
457a6b58
MD
103 enum lttng_ust_loglevel_type req_type,
104 int req_loglevel)
105{
e58095ef
MD
106 if (!has_loglevel)
107 loglevel = TRACE_DEFAULT;
457a6b58
MD
108 switch (req_type) {
109 case LTTNG_UST_LOGLEVEL_RANGE:
aacb3774 110 if (loglevel <= req_loglevel
67ada458 111 || (req_loglevel == -1 && loglevel <= TRACE_DEBUG))
457a6b58
MD
112 return 1;
113 else
114 return 0;
115 case LTTNG_UST_LOGLEVEL_SINGLE:
aacb3774 116 if (loglevel == req_loglevel
67ada458 117 || (req_loglevel == -1 && loglevel <= TRACE_DEBUG))
457a6b58
MD
118 return 1;
119 else
120 return 0;
121 case LTTNG_UST_LOGLEVEL_ALL:
122 default:
67ada458 123 if (loglevel <= TRACE_DEBUG)
aacb3774
MD
124 return 1;
125 else
126 return 0;
457a6b58
MD
127 }
128}
129
8020ceb5
MD
130void synchronize_trace(void)
131{
8020ceb5 132 synchronize_rcu();
8020ceb5
MD
133}
134
7dd08bec 135struct lttng_session *lttng_session_create(void)
8020ceb5 136{
7dd08bec 137 struct lttng_session *session;
74d81a6c 138 int i;
8020ceb5 139
7dd08bec 140 session = zmalloc(sizeof(struct lttng_session));
8020ceb5
MD
141 if (!session)
142 return NULL;
53569322
MD
143 if (lttng_session_context_init(&session->ctx)) {
144 free(session);
145 return NULL;
146 }
e58095ef
MD
147 CDS_INIT_LIST_HEAD(&session->chan_head);
148 CDS_INIT_LIST_HEAD(&session->events_head);
c785c634 149 CDS_INIT_LIST_HEAD(&session->enums_head);
e58095ef 150 CDS_INIT_LIST_HEAD(&session->enablers_head);
d56fa719
MD
151 for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++)
152 CDS_INIT_HLIST_HEAD(&session->events_ht.table[i]);
c785c634
MD
153 for (i = 0; i < LTTNG_UST_ENUM_HT_SIZE; i++)
154 CDS_INIT_HLIST_HEAD(&session->enums_ht.table[i]);
e58095ef 155 cds_list_add(&session->node, &sessions);
8020ceb5
MD
156 return session;
157}
158
74d81a6c
MD
159/*
160 * Only used internally at session destruction.
161 */
162static
163void _lttng_channel_unmap(struct lttng_channel *lttng_chan)
164{
165 struct channel *chan;
166 struct lttng_ust_shm_handle *handle;
167
168 cds_list_del(&lttng_chan->node);
169 lttng_destroy_context(lttng_chan->ctx);
170 chan = lttng_chan->chan;
171 handle = lttng_chan->handle;
172 /*
173 * note: lttng_chan is private data contained within handle. It
174 * will be freed along with the handle.
175 */
176 channel_destroy(chan, handle, 0);
177}
178
ac6b4ac6
MD
179static
180void register_event(struct lttng_event *event)
181{
182 int ret;
183 const struct lttng_event_desc *desc;
184
185 assert(event->registered == 0);
186 desc = event->desc;
187 ret = __tracepoint_probe_register(desc->name,
188 desc->probe_callback,
189 event, desc->signature);
190 WARN_ON_ONCE(ret);
191 if (!ret)
192 event->registered = 1;
193}
194
195static
196void unregister_event(struct lttng_event *event)
197{
198 int ret;
199 const struct lttng_event_desc *desc;
200
201 assert(event->registered == 1);
202 desc = event->desc;
203 ret = __tracepoint_probe_unregister(desc->name,
204 desc->probe_callback,
205 event);
206 WARN_ON_ONCE(ret);
207 if (!ret)
208 event->registered = 0;
209}
210
211/*
212 * Only used internally at session destruction.
213 */
214static
215void _lttng_event_unregister(struct lttng_event *event)
216{
217 if (event->registered)
218 unregister_event(event);
219}
220
7dd08bec 221void lttng_session_destroy(struct lttng_session *session)
8020ceb5 222{
7dd08bec
MD
223 struct lttng_channel *chan, *tmpchan;
224 struct lttng_event *event, *tmpevent;
c785c634 225 struct lttng_enum *_enum, *tmp_enum;
e58095ef 226 struct lttng_enabler *enabler, *tmpenabler;
8020ceb5 227
b5234c06 228 CMM_ACCESS_ONCE(session->active) = 0;
e58095ef 229 cds_list_for_each_entry(event, &session->events_head, node) {
ac6b4ac6 230 _lttng_event_unregister(event);
8020ceb5
MD
231 }
232 synchronize_trace(); /* Wait for in-flight events to complete */
e58095ef
MD
233 cds_list_for_each_entry_safe(enabler, tmpenabler,
234 &session->enablers_head, node)
235 lttng_enabler_destroy(enabler);
236 cds_list_for_each_entry_safe(event, tmpevent,
237 &session->events_head, node)
7dd08bec 238 _lttng_event_destroy(event);
c785c634
MD
239 cds_list_for_each_entry_safe(_enum, tmp_enum,
240 &session->enums_head, node)
241 _lttng_enum_destroy(_enum);
e58095ef 242 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan_head, node)
74d81a6c 243 _lttng_channel_unmap(chan);
e58095ef 244 cds_list_del(&session->node);
53569322 245 lttng_destroy_context(session->ctx);
b5234c06 246 free(session);
8020ceb5
MD
247}
248
53569322
MD
249static
250int lttng_enum_create(const struct lttng_enum_desc *desc,
251 struct lttng_session *session)
252{
253 const char *enum_name = desc->name;
254 struct lttng_enum *_enum;
255 struct cds_hlist_head *head;
256 struct cds_hlist_node *node;
257 int ret = 0;
258 size_t name_len = strlen(enum_name);
259 uint32_t hash;
260 int notify_socket;
261
262 hash = jhash(enum_name, name_len, 0);
263 head = &session->enums_ht.table[hash & (LTTNG_UST_ENUM_HT_SIZE - 1)];
264 cds_hlist_for_each_entry(_enum, node, head, hlist) {
265 assert(_enum->desc);
266 if (!strncmp(_enum->desc->name, desc->name,
267 LTTNG_UST_SYM_NAME_LEN - 1)) {
268 ret = -EEXIST;
269 goto exist;
270 }
271 }
272
273 notify_socket = lttng_get_notify_socket(session->owner);
274 if (notify_socket < 0) {
275 ret = notify_socket;
276 goto socket_error;
277 }
278
279 _enum = zmalloc(sizeof(*_enum));
280 if (!_enum) {
281 ret = -ENOMEM;
282 goto cache_error;
283 }
284 _enum->session = session;
285 _enum->desc = desc;
286
287 ret = ustcomm_register_enum(notify_socket,
288 session->objd,
289 enum_name,
290 desc->nr_entries,
291 desc->entries,
292 &_enum->id);
293 if (ret < 0) {
294 DBG("Error (%d) registering enumeration to sessiond", ret);
295 goto sessiond_register_error;
296 }
297 cds_list_add(&_enum->node, &session->enums_head);
298 cds_hlist_add_head(&_enum->hlist, head);
299 return 0;
300
301sessiond_register_error:
302 free(_enum);
303cache_error:
304socket_error:
305exist:
306 return ret;
307}
308
309static
310int lttng_create_enum_check(const struct lttng_type *type,
311 struct lttng_session *session)
312{
313 switch (type->atype) {
314 case atype_enum:
315 {
316 const struct lttng_enum_desc *enum_desc;
317 int ret;
318
319 enum_desc = type->u.basic.enumeration.desc;
320 ret = lttng_enum_create(enum_desc, session);
321 if (ret && ret != -EEXIST) {
322 DBG("Unable to create enum error: (%d)", ret);
323 return ret;
324 }
325 break;
326 }
327 case atype_dynamic:
328 {
329 const struct lttng_event_field *tag_field_generic;
330 const struct lttng_enum_desc *enum_desc;
331 int ret;
332
333 tag_field_generic = lttng_ust_dynamic_type_tag_field();
334 enum_desc = tag_field_generic->type.u.basic.enumeration.desc;
335 ret = lttng_enum_create(enum_desc, session);
336 if (ret && ret != -EEXIST) {
337 DBG("Unable to create enum error: (%d)", ret);
338 return ret;
339 }
340 break;
341 }
342 default:
343 /* TODO: nested types when they become supported. */
344 break;
345 }
346 return 0;
347}
348
349static
350int lttng_create_all_event_enums(size_t nr_fields,
351 const struct lttng_event_field *event_fields,
352 struct lttng_session *session)
353{
354 size_t i;
355 int ret;
356
357 /* For each field, ensure enum is part of the session. */
358 for (i = 0; i < nr_fields; i++) {
359 const struct lttng_type *type = &event_fields[i].type;
360
361 ret = lttng_create_enum_check(type, session);
362 if (ret)
363 return ret;
364 }
365 return 0;
366}
367
368static
369int lttng_create_all_ctx_enums(size_t nr_fields,
370 const struct lttng_ctx_field *ctx_fields,
371 struct lttng_session *session)
372{
373 size_t i;
374 int ret;
375
376 /* For each field, ensure enum is part of the session. */
377 for (i = 0; i < nr_fields; i++) {
378 const struct lttng_type *type = &ctx_fields[i].event_field.type;
379
380 ret = lttng_create_enum_check(type, session);
381 if (ret)
382 return ret;
383 }
384 return 0;
385}
386
387
7dd08bec 388int lttng_session_enable(struct lttng_session *session)
8020ceb5
MD
389{
390 int ret = 0;
7dd08bec 391 struct lttng_channel *chan;
32ce8569 392 int notify_socket;
8020ceb5 393
8020ceb5
MD
394 if (session->active) {
395 ret = -EBUSY;
396 goto end;
397 }
398
32ce8569
MD
399 notify_socket = lttng_get_notify_socket(session->owner);
400 if (notify_socket < 0)
401 return notify_socket;
402
ac6b4ac6
MD
403 /* Set transient enabler state to "enabled" */
404 session->tstate = 1;
e58095ef 405
8020ceb5
MD
406 /*
407 * Snapshot the number of events per channel to know the type of header
408 * we need to use.
409 */
e58095ef 410 cds_list_for_each_entry(chan, &session->chan_head, node) {
32ce8569 411 const struct lttng_ctx *ctx;
83e43212 412 const struct lttng_ctx_field *fields = NULL;
32ce8569 413 size_t nr_fields = 0;
6ca18e66 414 uint32_t chan_id;
32ce8569
MD
415
416 /* don't change it if session stop/restart */
8020ceb5 417 if (chan->header_type)
32ce8569
MD
418 continue;
419 ctx = chan->ctx;
420 if (ctx) {
421 nr_fields = ctx->nr_fields;
83e43212 422 fields = ctx->fields;
53569322
MD
423 ret = lttng_create_all_ctx_enums(nr_fields, fields,
424 session);
425 if (ret < 0) {
426 DBG("Error (%d) adding enum to session", ret);
427 return ret;
428 }
32ce8569
MD
429 }
430 ret = ustcomm_register_channel(notify_socket,
53569322 431 session,
32ce8569
MD
432 session->objd,
433 chan->objd,
434 nr_fields,
435 fields,
6ca18e66 436 &chan_id,
32ce8569 437 &chan->header_type);
b869b5ae
MD
438 if (ret) {
439 DBG("Error (%d) registering channel to sessiond", ret);
32ce8569 440 return ret;
b869b5ae 441 }
6ca18e66
MD
442 if (chan_id != chan->id) {
443 DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)",
444 chan_id, chan->id);
445 return -EINVAL;
446 }
8020ceb5
MD
447 }
448
5c055275
FD
449 /* We need to sync enablers with session before activation. */
450 lttng_session_sync_enablers(session);
451
ac6b4ac6 452 /* Set atomically the state to "active" */
b5234c06
MD
453 CMM_ACCESS_ONCE(session->active) = 1;
454 CMM_ACCESS_ONCE(session->been_active) = 1;
95c25348 455
246be17e
PW
456 session->statedump_pending = 1;
457 lttng_ust_sockinfo_session_enabled(session->owner);
8020ceb5 458end:
8020ceb5
MD
459 return ret;
460}
461
7dd08bec 462int lttng_session_disable(struct lttng_session *session)
8020ceb5
MD
463{
464 int ret = 0;
465
8020ceb5
MD
466 if (!session->active) {
467 ret = -EBUSY;
468 goto end;
469 }
ac6b4ac6 470 /* Set atomically the state to "inactive" */
b5234c06 471 CMM_ACCESS_ONCE(session->active) = 0;
ac6b4ac6
MD
472
473 /* Set transient enabler state to "disabled" */
474 session->tstate = 0;
475 lttng_session_sync_enablers(session);
8020ceb5 476end:
8020ceb5
MD
477 return ret;
478}
479
7dd08bec 480int lttng_channel_enable(struct lttng_channel *channel)
976fe9ea 481{
ac6b4ac6 482 int ret = 0;
976fe9ea 483
ac6b4ac6
MD
484 if (channel->enabled) {
485 ret = -EBUSY;
486 goto end;
487 }
488 /* Set transient enabler state to "enabled" */
489 channel->tstate = 1;
490 lttng_session_sync_enablers(channel->session);
491 /* Set atomically the state to "enabled" */
492 CMM_ACCESS_ONCE(channel->enabled) = 1;
493end:
494 return ret;
976fe9ea
MD
495}
496
7dd08bec 497int lttng_channel_disable(struct lttng_channel *channel)
976fe9ea 498{
ac6b4ac6 499 int ret = 0;
976fe9ea 500
ac6b4ac6
MD
501 if (!channel->enabled) {
502 ret = -EBUSY;
503 goto end;
504 }
505 /* Set atomically the state to "disabled" */
506 CMM_ACCESS_ONCE(channel->enabled) = 0;
507 /* Set transient enabler state to "enabled" */
508 channel->tstate = 0;
509 lttng_session_sync_enablers(channel->session);
510end:
511 return ret;
976fe9ea
MD
512}
513
8020ceb5
MD
514/*
515 * Supports event creation while tracing session is active.
516 */
e58095ef
MD
517static
518int lttng_event_create(const struct lttng_event_desc *desc,
519 struct lttng_channel *chan)
8020ceb5 520{
e58095ef 521 const char *event_name = desc->name;
7dd08bec 522 struct lttng_event *event;
32ce8569 523 struct lttng_session *session = chan->session;
d56fa719
MD
524 struct cds_hlist_head *head;
525 struct cds_hlist_node *node;
576599a0 526 int ret = 0;
d56fa719
MD
527 size_t name_len = strlen(event_name);
528 uint32_t hash;
32ce8569
MD
529 int notify_socket, loglevel;
530 const char *uri;
8020ceb5 531
d56fa719
MD
532 hash = jhash(event_name, name_len, 0);
533 head = &chan->session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
534 cds_hlist_for_each_entry(event, node, head, hlist) {
e58095ef 535 assert(event->desc);
d970f72e
MD
536 if (!strncmp(event->desc->name, desc->name,
537 LTTNG_UST_SYM_NAME_LEN - 1)
538 && chan == event->chan) {
576599a0 539 ret = -EEXIST;
8020ceb5 540 goto exist;
576599a0
MD
541 }
542 }
457a6b58 543
32ce8569
MD
544 notify_socket = lttng_get_notify_socket(session->owner);
545 if (notify_socket < 0) {
546 ret = notify_socket;
547 goto socket_error;
548 }
549
53569322
MD
550 ret = lttng_create_all_event_enums(desc->nr_fields, desc->fields,
551 session);
c785c634
MD
552 if (ret < 0) {
553 DBG("Error (%d) adding enum to session", ret);
554 goto create_enum_error;
555 }
556
457a6b58
MD
557 /*
558 * Check if loglevel match. Refuse to connect event if not.
559 */
7dd08bec 560 event = zmalloc(sizeof(struct lttng_event));
576599a0
MD
561 if (!event) {
562 ret = -ENOMEM;
8020ceb5 563 goto cache_error;
576599a0 564 }
8020ceb5 565 event->chan = chan;
e58095ef 566
ac6b4ac6
MD
567 /* Event will be enabled by enabler sync. */
568 event->enabled = 0;
569 event->registered = 0;
e58095ef
MD
570 CDS_INIT_LIST_HEAD(&event->bytecode_runtime_head);
571 CDS_INIT_LIST_HEAD(&event->enablers_ref_head);
572 event->desc = desc;
32ce8569
MD
573
574 if (desc->loglevel)
575 loglevel = *(*event->desc->loglevel);
576 else
577 loglevel = TRACE_DEFAULT;
578 if (desc->u.ext.model_emf_uri)
579 uri = *(desc->u.ext.model_emf_uri);
580 else
581 uri = NULL;
582
13b21cd6
MD
583 /* Fetch event ID from sessiond */
584 ret = ustcomm_register_event(notify_socket,
c785c634 585 session,
13b21cd6
MD
586 session->objd,
587 chan->objd,
588 event_name,
589 loglevel,
590 desc->signature,
591 desc->nr_fields,
592 desc->fields,
593 uri,
594 &event->id);
595 if (ret < 0) {
596 DBG("Error (%d) registering event to sessiond", ret);
597 goto sessiond_register_error;
32ce8569 598 }
2b213b16 599
7dd08bec 600 /* Populate lttng_event structure before tracepoint registration. */
b5234c06 601 cmm_smp_wmb();
e58095ef 602 cds_list_add(&event->node, &chan->session->events_head);
d56fa719 603 cds_hlist_add_head(&event->hlist, head);
576599a0 604 return 0;
8020ceb5 605
32ce8569 606sessiond_register_error:
b5234c06 607 free(event);
8020ceb5 608cache_error:
c785c634 609create_enum_error:
32ce8569 610socket_error:
8020ceb5 611exist:
576599a0 612 return ret;
8020ceb5
MD
613}
614
e58095ef
MD
615static
616int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc *desc,
617 struct lttng_enabler *enabler)
618{
619 int loglevel = 0;
7e2e405c 620 unsigned int has_loglevel = 0;
e58095ef
MD
621
622 assert(enabler->type == LTTNG_ENABLER_WILDCARD);
623 /* Compare excluding final '*' */
624 if (strncmp(desc->name, enabler->event_param.name,
625 strlen(enabler->event_param.name) - 1))
626 return 0;
627 if (desc->loglevel) {
628 loglevel = *(*desc->loglevel);
629 has_loglevel = 1;
630 }
631 if (!lttng_loglevel_match(loglevel,
632 has_loglevel,
633 enabler->event_param.loglevel_type,
634 enabler->event_param.loglevel))
635 return 0;
636 return 1;
637}
638
639static
640int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc,
641 struct lttng_enabler *enabler)
642{
643 int loglevel = 0;
644 unsigned int has_loglevel = 0;
645
646 assert(enabler->type == LTTNG_ENABLER_EVENT);
647 if (strcmp(desc->name, enabler->event_param.name))
648 return 0;
649 if (desc->loglevel) {
650 loglevel = *(*desc->loglevel);
651 has_loglevel = 1;
652 }
653 if (!lttng_loglevel_match(loglevel,
654 has_loglevel,
655 enabler->event_param.loglevel_type,
656 enabler->event_param.loglevel))
657 return 0;
658 return 1;
659}
660
661static
662int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
663 struct lttng_enabler *enabler)
664{
ed5b5bbd
JI
665 struct lttng_ust_excluder_node *excluder;
666
667 /* If event matches with an excluder, return 'does not match' */
668 cds_list_for_each_entry(excluder, &enabler->excluder_head, node) {
669 int count;
670
671 for (count = 0; count < excluder->excluder.count; count++) {
672 int found, len;
673 char *excluder_name;
674
675 excluder_name = (char *) (excluder->excluder.names)
676 + count * LTTNG_UST_SYM_NAME_LEN;
677 len = strnlen(excluder_name, LTTNG_UST_SYM_NAME_LEN);
678 if (len > 0 && excluder_name[len - 1] == '*') {
679 found = !strncmp(desc->name, excluder_name,
680 len - 1);
681 } else {
682 found = !strncmp(desc->name, excluder_name,
683 LTTNG_UST_SYM_NAME_LEN - 1);
684 }
685 if (found) {
686 return 0;
687 }
688 }
689 }
e58095ef
MD
690 switch (enabler->type) {
691 case LTTNG_ENABLER_WILDCARD:
692 return lttng_desc_match_wildcard_enabler(desc, enabler);
693 case LTTNG_ENABLER_EVENT:
694 return lttng_desc_match_event_enabler(desc, enabler);
695 default:
696 return -EINVAL;
697 }
698}
699
700static
701int lttng_event_match_enabler(struct lttng_event *event,
702 struct lttng_enabler *enabler)
703{
d970f72e
MD
704 if (lttng_desc_match_enabler(event->desc, enabler)
705 && event->chan == enabler->chan)
706 return 1;
707 else
708 return 0;
e58095ef
MD
709}
710
711static
712struct lttng_enabler_ref * lttng_event_enabler_ref(struct lttng_event *event,
713 struct lttng_enabler *enabler)
714{
715 struct lttng_enabler_ref *enabler_ref;
716
717 cds_list_for_each_entry(enabler_ref,
718 &event->enablers_ref_head, node) {
719 if (enabler_ref->ref == enabler)
720 return enabler_ref;
721 }
722 return NULL;
723}
724
8020ceb5 725/*
e58095ef
MD
726 * Create struct lttng_event if it is missing and present in the list of
727 * tracepoint probes.
8020ceb5 728 */
e58095ef
MD
729static
730void lttng_create_event_if_missing(struct lttng_enabler *enabler)
8020ceb5 731{
e58095ef
MD
732 struct lttng_session *session = enabler->chan->session;
733 struct lttng_probe_desc *probe_desc;
734 const struct lttng_event_desc *desc;
735 struct lttng_event *event;
736 int i;
737 struct cds_list_head *probe_list;
738
739 probe_list = lttng_get_probe_list_head();
740 /*
741 * For each probe event, if we find that a probe event matches
742 * our enabler, create an associated lttng_event if not
743 * already present.
744 */
745 cds_list_for_each_entry(probe_desc, probe_list, head) {
746 for (i = 0; i < probe_desc->nr_events; i++) {
747 int found = 0, ret;
d56fa719
MD
748 struct cds_hlist_head *head;
749 struct cds_hlist_node *node;
750 const char *event_name;
751 size_t name_len;
752 uint32_t hash;
e58095ef
MD
753
754 desc = probe_desc->event_desc[i];
755 if (!lttng_desc_match_enabler(desc, enabler))
756 continue;
d56fa719
MD
757 event_name = desc->name;
758 name_len = strlen(event_name);
e58095ef
MD
759
760 /*
d56fa719 761 * Check if already created.
e58095ef 762 */
d56fa719
MD
763 hash = jhash(event_name, name_len, 0);
764 head = &session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
765 cds_hlist_for_each_entry(event, node, head, hlist) {
d970f72e
MD
766 if (event->desc == desc
767 && event->chan == enabler->chan)
e58095ef
MD
768 found = 1;
769 }
770 if (found)
771 continue;
772
773 /*
774 * We need to create an event for this
775 * event probe.
776 */
777 ret = lttng_event_create(probe_desc->event_desc[i],
778 enabler->chan);
779 if (ret) {
32ce8569
MD
780 DBG("Unable to create event %s, error %d\n",
781 probe_desc->event_desc[i]->name, ret);
e58095ef 782 }
8165c8da 783 }
8020ceb5 784 }
8020ceb5
MD
785}
786
787/*
e58095ef
MD
788 * Create events associated with an enabler (if not already present),
789 * and add backward reference from the event to the enabler.
8020ceb5
MD
790 */
791static
e58095ef 792int lttng_enabler_ref_events(struct lttng_enabler *enabler)
8020ceb5 793{
e58095ef
MD
794 struct lttng_session *session = enabler->chan->session;
795 struct lttng_event *event;
796
797 /* First ensure that probe events are created for this enabler. */
798 lttng_create_event_if_missing(enabler);
799
800 /* For each event matching enabler in session event list. */
801 cds_list_for_each_entry(event, &session->events_head, node) {
802 struct lttng_enabler_ref *enabler_ref;
803
804 if (!lttng_event_match_enabler(event, enabler))
805 continue;
806
807 enabler_ref = lttng_event_enabler_ref(event, enabler);
808 if (!enabler_ref) {
809 /*
810 * If no backward ref, create it.
811 * Add backward ref from event to enabler.
812 */
813 enabler_ref = zmalloc(sizeof(*enabler_ref));
814 if (!enabler_ref)
815 return -ENOMEM;
816 enabler_ref->ref = enabler;
817 cds_list_add(&enabler_ref->node,
818 &event->enablers_ref_head);
8165c8da 819 }
e58095ef
MD
820
821 /*
822 * Link filter bytecodes if not linked yet.
823 */
824 lttng_enabler_event_link_bytecode(event, enabler);
825
826 /* TODO: merge event context. */
827 }
828 return 0;
829}
830
831/*
832 * Called at library load: connect the probe on all enablers matching
833 * this event.
5f733922 834 * Called with session mutex held.
e58095ef 835 */
5f733922 836int lttng_fix_pending_events(void)
e58095ef
MD
837{
838 struct lttng_session *session;
839
840 cds_list_for_each_entry(session, &sessions, node) {
841 lttng_session_lazy_sync_enablers(session);
8020ceb5 842 }
e58095ef
MD
843 return 0;
844}
845
246be17e 846/*
37dddb65
MD
847 * For each session of the owner thread, execute pending statedump.
848 * Only dump state for the sessions owned by the caller thread, because
849 * we don't keep ust_lock across the entire iteration.
246be17e 850 */
3327ac33 851void lttng_handle_pending_statedump(void *owner)
246be17e
PW
852{
853 struct lttng_session *session;
854
37dddb65 855 /* Execute state dump */
cf73e0fe 856 do_lttng_ust_statedump(owner);
37dddb65
MD
857
858 /* Clear pending state dump */
3327ac33
MD
859 if (ust_lock()) {
860 goto end;
861 }
246be17e 862 cds_list_for_each_entry(session, &sessions, node) {
37dddb65
MD
863 if (session->owner != owner)
864 continue;
865 if (!session->statedump_pending)
866 continue;
867 session->statedump_pending = 0;
246be17e 868 }
3327ac33 869end:
37dddb65 870 ust_unlock();
3327ac33 871 return;
246be17e
PW
872}
873
e58095ef
MD
874/*
875 * Only used internally at session destruction.
876 */
877static
878void _lttng_event_destroy(struct lttng_event *event)
879{
880 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
881
e58095ef 882 cds_list_del(&event->node);
8020ceb5 883 lttng_destroy_context(event->ctx);
f488575f 884 lttng_free_event_filter_runtime(event);
e58095ef
MD
885 /* Free event enabler refs */
886 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
887 &event->enablers_ref_head, node)
888 free(enabler_ref);
b5234c06 889 free(event);
8020ceb5
MD
890}
891
c785c634
MD
892static
893void _lttng_enum_destroy(struct lttng_enum *_enum)
894{
895 cds_list_del(&_enum->node);
896 free(_enum);
897}
898
003fedf4 899void lttng_ust_events_exit(void)
8020ceb5 900{
7dd08bec 901 struct lttng_session *session, *tmpsession;
8020ceb5 902
e58095ef 903 cds_list_for_each_entry_safe(session, tmpsession, &sessions, node)
7dd08bec 904 lttng_session_destroy(session);
8020ceb5 905}
457a6b58 906
e58095ef
MD
907/*
908 * Enabler management.
909 */
910struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
911 struct lttng_ust_event *event_param,
912 struct lttng_channel *chan)
457a6b58 913{
e58095ef
MD
914 struct lttng_enabler *enabler;
915
916 enabler = zmalloc(sizeof(*enabler));
917 if (!enabler)
918 return NULL;
919 enabler->type = type;
920 CDS_INIT_LIST_HEAD(&enabler->filter_bytecode_head);
0f63324a 921 CDS_INIT_LIST_HEAD(&enabler->excluder_head);
e58095ef
MD
922 memcpy(&enabler->event_param, event_param,
923 sizeof(enabler->event_param));
924 enabler->chan = chan;
925 /* ctx left NULL */
89f26d57 926 enabler->enabled = 0;
e58095ef
MD
927 cds_list_add(&enabler->node, &enabler->chan->session->enablers_head);
928 lttng_session_lazy_sync_enablers(enabler->chan->session);
929 return enabler;
457a6b58
MD
930}
931
e58095ef 932int lttng_enabler_enable(struct lttng_enabler *enabler)
457a6b58 933{
e58095ef
MD
934 enabler->enabled = 1;
935 lttng_session_lazy_sync_enablers(enabler->chan->session);
936 return 0;
457a6b58 937}
457a6b58 938
e58095ef 939int lttng_enabler_disable(struct lttng_enabler *enabler)
457a6b58 940{
e58095ef
MD
941 enabler->enabled = 0;
942 lttng_session_lazy_sync_enablers(enabler->chan->session);
943 return 0;
944}
457a6b58 945
e58095ef
MD
946int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
947 struct lttng_ust_filter_bytecode_node *bytecode)
948{
949 bytecode->enabler = enabler;
950 cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head);
951 lttng_session_lazy_sync_enablers(enabler->chan->session);
952 return 0;
0bfb5cbd
JI
953}
954
955int lttng_enabler_attach_exclusion(struct lttng_enabler *enabler,
956 struct lttng_ust_excluder_node *excluder)
957{
958 excluder->enabler = enabler;
959 cds_list_add_tail(&excluder->node, &enabler->excluder_head);
960 lttng_session_lazy_sync_enablers(enabler->chan->session);
961 return 0;
e58095ef 962}
f488575f 963
e58095ef 964int lttng_attach_context(struct lttng_ust_context *context_param,
8e696cfa 965 union ust_args *uargs,
e58095ef
MD
966 struct lttng_ctx **ctx, struct lttng_session *session)
967{
457a6b58 968 /*
e58095ef
MD
969 * We cannot attach a context after trace has been started for a
970 * session because the metadata does not allow expressing this
971 * information outside of the original channel scope.
457a6b58 972 */
e58095ef
MD
973 if (session->been_active)
974 return -EPERM;
457a6b58 975
e58095ef
MD
976 switch (context_param->ctx) {
977 case LTTNG_UST_CONTEXT_PTHREAD_ID:
978 return lttng_add_pthread_id_to_ctx(ctx);
d58d1454
MD
979 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
980 {
981 struct lttng_ust_perf_counter_ctx *perf_ctx_param;
982
983 perf_ctx_param = &context_param->u.perf_counter;
984 return lttng_add_perf_counter_to_ctx(
985 perf_ctx_param->type,
986 perf_ctx_param->config,
987 perf_ctx_param->name,
988 ctx);
989 }
e58095ef
MD
990 case LTTNG_UST_CONTEXT_VTID:
991 return lttng_add_vtid_to_ctx(ctx);
992 case LTTNG_UST_CONTEXT_VPID:
993 return lttng_add_vpid_to_ctx(ctx);
994 case LTTNG_UST_CONTEXT_PROCNAME:
995 return lttng_add_procname_to_ctx(ctx);
96f85541
MD
996 case LTTNG_UST_CONTEXT_IP:
997 return lttng_add_ip_to_ctx(ctx);
c7ea8487
MD
998 case LTTNG_UST_CONTEXT_CPU_ID:
999 return lttng_add_cpu_id_to_ctx(ctx);
8e696cfa
MD
1000 case LTTNG_UST_CONTEXT_APP_CONTEXT:
1001 return lttng_ust_add_app_context_to_ctx_rcu(uargs->app_context.ctxname,
1002 ctx);
e58095ef
MD
1003 default:
1004 return -EINVAL;
457a6b58 1005 }
457a6b58
MD
1006}
1007
e58095ef
MD
1008int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1009 struct lttng_ust_context *context_param)
457a6b58 1010{
e58095ef
MD
1011#if 0 // disabled for now.
1012 struct lttng_session *session = enabler->chan->session;
1013 int ret;
457a6b58 1014
e58095ef
MD
1015 ret = lttng_attach_context(context_param, &enabler->ctx,
1016 session);
1017 if (ret)
1018 return ret;
1019 lttng_session_lazy_sync_enablers(enabler->chan->session);
1020#endif
1021 return -ENOSYS;
457a6b58
MD
1022}
1023
e58095ef
MD
1024static
1025void lttng_enabler_destroy(struct lttng_enabler *enabler)
457a6b58 1026{
e58095ef 1027 struct lttng_ust_filter_bytecode_node *filter_node, *tmp_filter_node;
0f63324a 1028 struct lttng_ust_excluder_node *excluder_node, *tmp_excluder_node;
457a6b58 1029
e58095ef
MD
1030 /* Destroy filter bytecode */
1031 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
1032 &enabler->filter_bytecode_head, node) {
1033 free(filter_node);
457a6b58 1034 }
e58095ef 1035
0f63324a
JI
1036 /* Destroy excluders */
1037 cds_list_for_each_entry_safe(excluder_node, tmp_excluder_node,
1038 &enabler->excluder_head, node) {
1039 free(excluder_node);
1040 }
1041
e58095ef
MD
1042 /* Destroy contexts */
1043 lttng_destroy_context(enabler->ctx);
1044
1045 cds_list_del(&enabler->node);
1046 free(enabler);
457a6b58
MD
1047}
1048
e58095ef
MD
1049/*
1050 * lttng_session_sync_enablers should be called just before starting a
1051 * session.
1052 */
457a6b58 1053static
e58095ef 1054void lttng_session_sync_enablers(struct lttng_session *session)
457a6b58 1055{
e58095ef
MD
1056 struct lttng_enabler *enabler;
1057 struct lttng_event *event;
457a6b58 1058
e58095ef
MD
1059 cds_list_for_each_entry(enabler, &session->enablers_head, node)
1060 lttng_enabler_ref_events(enabler);
1061 /*
1062 * For each event, if at least one of its enablers is enabled,
ac6b4ac6
MD
1063 * and its channel and session transient states are enabled, we
1064 * enable the event, else we disable it.
e58095ef
MD
1065 */
1066 cds_list_for_each_entry(event, &session->events_head, node) {
1067 struct lttng_enabler_ref *enabler_ref;
1068 struct lttng_bytecode_runtime *runtime;
dcdeaff0 1069 int enabled = 0, has_enablers_without_bytecode = 0;
e58095ef
MD
1070
1071 /* Enable events */
1072 cds_list_for_each_entry(enabler_ref,
1073 &event->enablers_ref_head, node) {
1074 if (enabler_ref->ref->enabled) {
1075 enabled = 1;
1076 break;
1077 }
1078 }
ac6b4ac6
MD
1079 /*
1080 * Enabled state is based on union of enablers, with
1081 * intesection of session and channel transient enable
1082 * states.
1083 */
1084 enabled = enabled && session->tstate && event->chan->tstate;
1085
1086 CMM_STORE_SHARED(event->enabled, enabled);
1087 /*
1088 * Sync tracepoint registration with event enabled
1089 * state.
1090 */
1091 if (enabled) {
1092 if (!event->registered)
1093 register_event(event);
1094 } else {
1095 if (event->registered)
1096 unregister_event(event);
1097 }
457a6b58 1098
1f49fc05 1099 /* Check if has enablers without bytecode enabled */
dcdeaff0
MD
1100 cds_list_for_each_entry(enabler_ref,
1101 &event->enablers_ref_head, node) {
1f49fc05
MD
1102 if (enabler_ref->ref->enabled
1103 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
dcdeaff0
MD
1104 has_enablers_without_bytecode = 1;
1105 break;
1106 }
1107 }
1108 event->has_enablers_without_bytecode =
1109 has_enablers_without_bytecode;
1110
e58095ef
MD
1111 /* Enable filters */
1112 cds_list_for_each_entry(runtime,
1113 &event->bytecode_runtime_head, node) {
1114 lttng_filter_sync_state(runtime);
457a6b58
MD
1115 }
1116 }
457a6b58
MD
1117}
1118
e58095ef
MD
1119/*
1120 * Apply enablers to session events, adding events to session if need
1121 * be. It is required after each modification applied to an active
1122 * session, and right before session "start".
1123 * "lazy" sync means we only sync if required.
1124 */
1125static
1126void lttng_session_lazy_sync_enablers(struct lttng_session *session)
457a6b58 1127{
e58095ef
MD
1128 /* We can skip if session is not active */
1129 if (!session->active)
1130 return;
1131 lttng_session_sync_enablers(session);
457a6b58 1132}
53569322
MD
1133
1134/*
1135 * Update all sessions with the given app context.
1136 * Called with ust lock held.
1137 * This is invoked when an application context gets loaded/unloaded. It
1138 * ensures the context callbacks are in sync with the application
1139 * context (either app context callbacks, or dummy callbacks).
1140 */
1141void lttng_ust_context_set_session_provider(const char *name,
1142 size_t (*get_size)(struct lttng_ctx_field *field, size_t offset),
1143 void (*record)(struct lttng_ctx_field *field,
1144 struct lttng_ust_lib_ring_buffer_ctx *ctx,
1145 struct lttng_channel *chan),
1146 void (*get_value)(struct lttng_ctx_field *field,
1147 struct lttng_ctx_value *value))
1148{
1149 struct lttng_session *session;
1150
1151 cds_list_for_each_entry(session, &sessions, node) {
1152 struct lttng_channel *chan;
1153 struct lttng_event *event;
1154 int ret;
1155
1156 ret = lttng_ust_context_set_provider_rcu(&session->ctx,
1157 name, get_size, record, get_value);
1158 if (ret)
1159 abort();
1160 cds_list_for_each_entry(chan, &session->chan_head, node) {
1161 ret = lttng_ust_context_set_provider_rcu(&chan->ctx,
1162 name, get_size, record, get_value);
1163 if (ret)
1164 abort();
1165 }
1166 cds_list_for_each_entry(event, &session->events_head, node) {
1167 ret = lttng_ust_context_set_provider_rcu(&event->ctx,
1168 name, get_size, record, get_value);
1169 if (ret)
1170 abort();
1171 }
1172 }
1173}
This page took 0.096221 seconds and 4 git commands to generate.