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