Commit | Line | Data |
---|---|---|
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> |
1f18504e | 50 | #include "error.h" |
08114193 | 51 | #include "compat.h" |
eda498b8 | 52 | #include "lttng-ust-uuid.h" |
44c72f10 | 53 | |
457a6b58 | 54 | #include "tracepoint-internal.h" |
7dd08bec MD |
55 | #include "lttng-tracer.h" |
56 | #include "lttng-tracer-core.h" | |
b728d87e | 57 | #include "wait.h" |
8d8a24c8 | 58 | #include "../libringbuffer/shm.h" |
596c4223 | 59 | #include "jhash.h" |
8165c8da MD |
60 | |
61 | /* | |
62 | * The sessions mutex is the centralized mutex across UST tracing | |
17dfb34b MD |
63 | * control and probe registration. All operations within this file are |
64 | * called by the communication thread, under ust_lock protection. | |
8165c8da | 65 | */ |
2b2d6ff7 | 66 | static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER; |
8165c8da | 67 | |
17dfb34b | 68 | void ust_lock(void) |
8165c8da MD |
69 | { |
70 | pthread_mutex_lock(&sessions_mutex); | |
71 | } | |
72 | ||
17dfb34b | 73 | void ust_unlock(void) |
8165c8da MD |
74 | { |
75 | pthread_mutex_unlock(&sessions_mutex); | |
76 | } | |
77 | ||
b5234c06 | 78 | static CDS_LIST_HEAD(sessions); |
8165c8da | 79 | |
7dd08bec | 80 | static void _lttng_event_destroy(struct lttng_event *event); |
8020ceb5 | 81 | |
e58095ef MD |
82 | static |
83 | void lttng_session_lazy_sync_enablers(struct lttng_session *session); | |
84 | static | |
85 | void lttng_session_sync_enablers(struct lttng_session *session); | |
86 | static | |
87 | void lttng_enabler_destroy(struct lttng_enabler *enabler); | |
88 | ||
6715d7d1 MD |
89 | /* |
90 | * Called with ust lock held. | |
91 | */ | |
92 | int 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 |
103 | static |
104 | int 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 | { | |
457a6b58 MD |
109 | if (req_type == LTTNG_UST_LOGLEVEL_ALL) |
110 | return 1; | |
e58095ef MD |
111 | if (!has_loglevel) |
112 | loglevel = TRACE_DEFAULT; | |
457a6b58 MD |
113 | switch (req_type) { |
114 | case LTTNG_UST_LOGLEVEL_RANGE: | |
e58095ef | 115 | if (loglevel <= req_loglevel || req_loglevel == -1) |
457a6b58 MD |
116 | return 1; |
117 | else | |
118 | return 0; | |
119 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
e58095ef | 120 | if (loglevel == req_loglevel || req_loglevel == -1) |
457a6b58 MD |
121 | return 1; |
122 | else | |
123 | return 0; | |
124 | case LTTNG_UST_LOGLEVEL_ALL: | |
125 | default: | |
126 | return 1; | |
127 | } | |
128 | } | |
129 | ||
8020ceb5 MD |
130 | void synchronize_trace(void) |
131 | { | |
8020ceb5 | 132 | synchronize_rcu(); |
8020ceb5 MD |
133 | } |
134 | ||
7dd08bec | 135 | struct 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; | |
e58095ef MD |
143 | CDS_INIT_LIST_HEAD(&session->chan_head); |
144 | CDS_INIT_LIST_HEAD(&session->events_head); | |
145 | CDS_INIT_LIST_HEAD(&session->enablers_head); | |
d56fa719 MD |
146 | for (i = 0; i < LTTNG_UST_EVENT_HT_SIZE; i++) |
147 | CDS_INIT_HLIST_HEAD(&session->events_ht.table[i]); | |
e58095ef | 148 | cds_list_add(&session->node, &sessions); |
8020ceb5 MD |
149 | return session; |
150 | } | |
151 | ||
74d81a6c MD |
152 | /* |
153 | * Only used internally at session destruction. | |
154 | */ | |
155 | static | |
156 | void _lttng_channel_unmap(struct lttng_channel *lttng_chan) | |
157 | { | |
158 | struct channel *chan; | |
159 | struct lttng_ust_shm_handle *handle; | |
160 | ||
161 | cds_list_del(<tng_chan->node); | |
162 | lttng_destroy_context(lttng_chan->ctx); | |
163 | chan = lttng_chan->chan; | |
164 | handle = lttng_chan->handle; | |
165 | /* | |
166 | * note: lttng_chan is private data contained within handle. It | |
167 | * will be freed along with the handle. | |
168 | */ | |
169 | channel_destroy(chan, handle, 0); | |
170 | } | |
171 | ||
ac6b4ac6 MD |
172 | static |
173 | void register_event(struct lttng_event *event) | |
174 | { | |
175 | int ret; | |
176 | const struct lttng_event_desc *desc; | |
177 | ||
178 | assert(event->registered == 0); | |
179 | desc = event->desc; | |
180 | ret = __tracepoint_probe_register(desc->name, | |
181 | desc->probe_callback, | |
182 | event, desc->signature); | |
183 | WARN_ON_ONCE(ret); | |
184 | if (!ret) | |
185 | event->registered = 1; | |
186 | } | |
187 | ||
188 | static | |
189 | void unregister_event(struct lttng_event *event) | |
190 | { | |
191 | int ret; | |
192 | const struct lttng_event_desc *desc; | |
193 | ||
194 | assert(event->registered == 1); | |
195 | desc = event->desc; | |
196 | ret = __tracepoint_probe_unregister(desc->name, | |
197 | desc->probe_callback, | |
198 | event); | |
199 | WARN_ON_ONCE(ret); | |
200 | if (!ret) | |
201 | event->registered = 0; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Only used internally at session destruction. | |
206 | */ | |
207 | static | |
208 | void _lttng_event_unregister(struct lttng_event *event) | |
209 | { | |
210 | if (event->registered) | |
211 | unregister_event(event); | |
212 | } | |
213 | ||
7dd08bec | 214 | void lttng_session_destroy(struct lttng_session *session) |
8020ceb5 | 215 | { |
7dd08bec MD |
216 | struct lttng_channel *chan, *tmpchan; |
217 | struct lttng_event *event, *tmpevent; | |
e58095ef | 218 | struct lttng_enabler *enabler, *tmpenabler; |
8020ceb5 | 219 | |
b5234c06 | 220 | CMM_ACCESS_ONCE(session->active) = 0; |
e58095ef | 221 | cds_list_for_each_entry(event, &session->events_head, node) { |
ac6b4ac6 | 222 | _lttng_event_unregister(event); |
8020ceb5 MD |
223 | } |
224 | synchronize_trace(); /* Wait for in-flight events to complete */ | |
e58095ef MD |
225 | cds_list_for_each_entry_safe(enabler, tmpenabler, |
226 | &session->enablers_head, node) | |
227 | lttng_enabler_destroy(enabler); | |
228 | cds_list_for_each_entry_safe(event, tmpevent, | |
229 | &session->events_head, node) | |
7dd08bec | 230 | _lttng_event_destroy(event); |
e58095ef | 231 | cds_list_for_each_entry_safe(chan, tmpchan, &session->chan_head, node) |
74d81a6c | 232 | _lttng_channel_unmap(chan); |
e58095ef | 233 | cds_list_del(&session->node); |
b5234c06 | 234 | free(session); |
8020ceb5 MD |
235 | } |
236 | ||
7dd08bec | 237 | int lttng_session_enable(struct lttng_session *session) |
8020ceb5 MD |
238 | { |
239 | int ret = 0; | |
7dd08bec | 240 | struct lttng_channel *chan; |
32ce8569 | 241 | int notify_socket; |
8020ceb5 | 242 | |
8020ceb5 MD |
243 | if (session->active) { |
244 | ret = -EBUSY; | |
245 | goto end; | |
246 | } | |
247 | ||
32ce8569 MD |
248 | notify_socket = lttng_get_notify_socket(session->owner); |
249 | if (notify_socket < 0) | |
250 | return notify_socket; | |
251 | ||
ac6b4ac6 MD |
252 | /* Set transient enabler state to "enabled" */ |
253 | session->tstate = 1; | |
e58095ef MD |
254 | /* We need to sync enablers with session before activation. */ |
255 | lttng_session_sync_enablers(session); | |
256 | ||
8020ceb5 MD |
257 | /* |
258 | * Snapshot the number of events per channel to know the type of header | |
259 | * we need to use. | |
260 | */ | |
e58095ef | 261 | cds_list_for_each_entry(chan, &session->chan_head, node) { |
32ce8569 | 262 | const struct lttng_ctx *ctx; |
83e43212 | 263 | const struct lttng_ctx_field *fields = NULL; |
32ce8569 | 264 | size_t nr_fields = 0; |
6ca18e66 | 265 | uint32_t chan_id; |
32ce8569 MD |
266 | |
267 | /* don't change it if session stop/restart */ | |
8020ceb5 | 268 | if (chan->header_type) |
32ce8569 MD |
269 | continue; |
270 | ctx = chan->ctx; | |
271 | if (ctx) { | |
272 | nr_fields = ctx->nr_fields; | |
83e43212 | 273 | fields = ctx->fields; |
32ce8569 MD |
274 | } |
275 | ret = ustcomm_register_channel(notify_socket, | |
276 | session->objd, | |
277 | chan->objd, | |
278 | nr_fields, | |
279 | fields, | |
6ca18e66 | 280 | &chan_id, |
32ce8569 | 281 | &chan->header_type); |
b869b5ae MD |
282 | if (ret) { |
283 | DBG("Error (%d) registering channel to sessiond", ret); | |
32ce8569 | 284 | return ret; |
b869b5ae | 285 | } |
6ca18e66 MD |
286 | if (chan_id != chan->id) { |
287 | DBG("Error: channel registration id (%u) does not match id assigned at creation (%u)", | |
288 | chan_id, chan->id); | |
289 | return -EINVAL; | |
290 | } | |
8020ceb5 MD |
291 | } |
292 | ||
ac6b4ac6 | 293 | /* Set atomically the state to "active" */ |
b5234c06 MD |
294 | CMM_ACCESS_ONCE(session->active) = 1; |
295 | CMM_ACCESS_ONCE(session->been_active) = 1; | |
8020ceb5 | 296 | end: |
8020ceb5 MD |
297 | return ret; |
298 | } | |
299 | ||
7dd08bec | 300 | int lttng_session_disable(struct lttng_session *session) |
8020ceb5 MD |
301 | { |
302 | int ret = 0; | |
303 | ||
8020ceb5 MD |
304 | if (!session->active) { |
305 | ret = -EBUSY; | |
306 | goto end; | |
307 | } | |
ac6b4ac6 | 308 | /* Set atomically the state to "inactive" */ |
b5234c06 | 309 | CMM_ACCESS_ONCE(session->active) = 0; |
ac6b4ac6 MD |
310 | |
311 | /* Set transient enabler state to "disabled" */ | |
312 | session->tstate = 0; | |
313 | lttng_session_sync_enablers(session); | |
8020ceb5 | 314 | end: |
8020ceb5 MD |
315 | return ret; |
316 | } | |
317 | ||
7dd08bec | 318 | int lttng_channel_enable(struct lttng_channel *channel) |
976fe9ea | 319 | { |
ac6b4ac6 | 320 | int ret = 0; |
976fe9ea | 321 | |
ac6b4ac6 MD |
322 | if (channel->enabled) { |
323 | ret = -EBUSY; | |
324 | goto end; | |
325 | } | |
326 | /* Set transient enabler state to "enabled" */ | |
327 | channel->tstate = 1; | |
328 | lttng_session_sync_enablers(channel->session); | |
329 | /* Set atomically the state to "enabled" */ | |
330 | CMM_ACCESS_ONCE(channel->enabled) = 1; | |
331 | end: | |
332 | return ret; | |
976fe9ea MD |
333 | } |
334 | ||
7dd08bec | 335 | int lttng_channel_disable(struct lttng_channel *channel) |
976fe9ea | 336 | { |
ac6b4ac6 | 337 | int ret = 0; |
976fe9ea | 338 | |
ac6b4ac6 MD |
339 | if (!channel->enabled) { |
340 | ret = -EBUSY; | |
341 | goto end; | |
342 | } | |
343 | /* Set atomically the state to "disabled" */ | |
344 | CMM_ACCESS_ONCE(channel->enabled) = 0; | |
345 | /* Set transient enabler state to "enabled" */ | |
346 | channel->tstate = 0; | |
347 | lttng_session_sync_enablers(channel->session); | |
348 | end: | |
349 | return ret; | |
976fe9ea MD |
350 | } |
351 | ||
8020ceb5 MD |
352 | /* |
353 | * Supports event creation while tracing session is active. | |
354 | */ | |
e58095ef MD |
355 | static |
356 | int lttng_event_create(const struct lttng_event_desc *desc, | |
357 | struct lttng_channel *chan) | |
8020ceb5 | 358 | { |
e58095ef | 359 | const char *event_name = desc->name; |
7dd08bec | 360 | struct lttng_event *event; |
32ce8569 | 361 | struct lttng_session *session = chan->session; |
d56fa719 MD |
362 | struct cds_hlist_head *head; |
363 | struct cds_hlist_node *node; | |
576599a0 | 364 | int ret = 0; |
d56fa719 MD |
365 | size_t name_len = strlen(event_name); |
366 | uint32_t hash; | |
32ce8569 MD |
367 | int notify_socket, loglevel; |
368 | const char *uri; | |
8020ceb5 | 369 | |
d56fa719 MD |
370 | hash = jhash(event_name, name_len, 0); |
371 | head = &chan->session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)]; | |
372 | cds_hlist_for_each_entry(event, node, head, hlist) { | |
e58095ef | 373 | assert(event->desc); |
d970f72e MD |
374 | if (!strncmp(event->desc->name, desc->name, |
375 | LTTNG_UST_SYM_NAME_LEN - 1) | |
376 | && chan == event->chan) { | |
576599a0 | 377 | ret = -EEXIST; |
8020ceb5 | 378 | goto exist; |
576599a0 MD |
379 | } |
380 | } | |
457a6b58 | 381 | |
32ce8569 MD |
382 | notify_socket = lttng_get_notify_socket(session->owner); |
383 | if (notify_socket < 0) { | |
384 | ret = notify_socket; | |
385 | goto socket_error; | |
386 | } | |
387 | ||
457a6b58 MD |
388 | /* |
389 | * Check if loglevel match. Refuse to connect event if not. | |
390 | */ | |
7dd08bec | 391 | event = zmalloc(sizeof(struct lttng_event)); |
576599a0 MD |
392 | if (!event) { |
393 | ret = -ENOMEM; | |
8020ceb5 | 394 | goto cache_error; |
576599a0 | 395 | } |
8020ceb5 | 396 | event->chan = chan; |
e58095ef | 397 | |
ac6b4ac6 MD |
398 | /* Event will be enabled by enabler sync. */ |
399 | event->enabled = 0; | |
400 | event->registered = 0; | |
e58095ef MD |
401 | CDS_INIT_LIST_HEAD(&event->bytecode_runtime_head); |
402 | CDS_INIT_LIST_HEAD(&event->enablers_ref_head); | |
403 | event->desc = desc; | |
32ce8569 MD |
404 | |
405 | if (desc->loglevel) | |
406 | loglevel = *(*event->desc->loglevel); | |
407 | else | |
408 | loglevel = TRACE_DEFAULT; | |
409 | if (desc->u.ext.model_emf_uri) | |
410 | uri = *(desc->u.ext.model_emf_uri); | |
411 | else | |
412 | uri = NULL; | |
413 | ||
13b21cd6 MD |
414 | /* Fetch event ID from sessiond */ |
415 | ret = ustcomm_register_event(notify_socket, | |
416 | session->objd, | |
417 | chan->objd, | |
418 | event_name, | |
419 | loglevel, | |
420 | desc->signature, | |
421 | desc->nr_fields, | |
422 | desc->fields, | |
423 | uri, | |
424 | &event->id); | |
425 | if (ret < 0) { | |
426 | DBG("Error (%d) registering event to sessiond", ret); | |
427 | goto sessiond_register_error; | |
32ce8569 | 428 | } |
2b213b16 | 429 | |
7dd08bec | 430 | /* Populate lttng_event structure before tracepoint registration. */ |
b5234c06 | 431 | cmm_smp_wmb(); |
e58095ef | 432 | cds_list_add(&event->node, &chan->session->events_head); |
d56fa719 | 433 | cds_hlist_add_head(&event->hlist, head); |
576599a0 | 434 | return 0; |
8020ceb5 | 435 | |
32ce8569 | 436 | sessiond_register_error: |
b5234c06 | 437 | free(event); |
8020ceb5 | 438 | cache_error: |
32ce8569 | 439 | socket_error: |
8020ceb5 | 440 | exist: |
576599a0 | 441 | return ret; |
8020ceb5 MD |
442 | } |
443 | ||
e58095ef MD |
444 | static |
445 | int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc *desc, | |
446 | struct lttng_enabler *enabler) | |
447 | { | |
448 | int loglevel = 0; | |
7e2e405c | 449 | unsigned int has_loglevel = 0; |
e58095ef MD |
450 | |
451 | assert(enabler->type == LTTNG_ENABLER_WILDCARD); | |
452 | /* Compare excluding final '*' */ | |
453 | if (strncmp(desc->name, enabler->event_param.name, | |
454 | strlen(enabler->event_param.name) - 1)) | |
455 | return 0; | |
456 | if (desc->loglevel) { | |
457 | loglevel = *(*desc->loglevel); | |
458 | has_loglevel = 1; | |
459 | } | |
460 | if (!lttng_loglevel_match(loglevel, | |
461 | has_loglevel, | |
462 | enabler->event_param.loglevel_type, | |
463 | enabler->event_param.loglevel)) | |
464 | return 0; | |
465 | return 1; | |
466 | } | |
467 | ||
468 | static | |
469 | int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc, | |
470 | struct lttng_enabler *enabler) | |
471 | { | |
472 | int loglevel = 0; | |
473 | unsigned int has_loglevel = 0; | |
474 | ||
475 | assert(enabler->type == LTTNG_ENABLER_EVENT); | |
476 | if (strcmp(desc->name, enabler->event_param.name)) | |
477 | return 0; | |
478 | if (desc->loglevel) { | |
479 | loglevel = *(*desc->loglevel); | |
480 | has_loglevel = 1; | |
481 | } | |
482 | if (!lttng_loglevel_match(loglevel, | |
483 | has_loglevel, | |
484 | enabler->event_param.loglevel_type, | |
485 | enabler->event_param.loglevel)) | |
486 | return 0; | |
487 | return 1; | |
488 | } | |
489 | ||
490 | static | |
491 | int lttng_desc_match_enabler(const struct lttng_event_desc *desc, | |
492 | struct lttng_enabler *enabler) | |
493 | { | |
494 | switch (enabler->type) { | |
495 | case LTTNG_ENABLER_WILDCARD: | |
496 | return lttng_desc_match_wildcard_enabler(desc, enabler); | |
497 | case LTTNG_ENABLER_EVENT: | |
498 | return lttng_desc_match_event_enabler(desc, enabler); | |
499 | default: | |
500 | return -EINVAL; | |
501 | } | |
502 | } | |
503 | ||
504 | static | |
505 | int lttng_event_match_enabler(struct lttng_event *event, | |
506 | struct lttng_enabler *enabler) | |
507 | { | |
d970f72e MD |
508 | if (lttng_desc_match_enabler(event->desc, enabler) |
509 | && event->chan == enabler->chan) | |
510 | return 1; | |
511 | else | |
512 | return 0; | |
e58095ef MD |
513 | } |
514 | ||
515 | static | |
516 | struct lttng_enabler_ref * lttng_event_enabler_ref(struct lttng_event *event, | |
517 | struct lttng_enabler *enabler) | |
518 | { | |
519 | struct lttng_enabler_ref *enabler_ref; | |
520 | ||
521 | cds_list_for_each_entry(enabler_ref, | |
522 | &event->enablers_ref_head, node) { | |
523 | if (enabler_ref->ref == enabler) | |
524 | return enabler_ref; | |
525 | } | |
526 | return NULL; | |
527 | } | |
528 | ||
8020ceb5 | 529 | /* |
e58095ef MD |
530 | * Create struct lttng_event if it is missing and present in the list of |
531 | * tracepoint probes. | |
8020ceb5 | 532 | */ |
e58095ef MD |
533 | static |
534 | void lttng_create_event_if_missing(struct lttng_enabler *enabler) | |
8020ceb5 | 535 | { |
e58095ef MD |
536 | struct lttng_session *session = enabler->chan->session; |
537 | struct lttng_probe_desc *probe_desc; | |
538 | const struct lttng_event_desc *desc; | |
539 | struct lttng_event *event; | |
540 | int i; | |
541 | struct cds_list_head *probe_list; | |
542 | ||
543 | probe_list = lttng_get_probe_list_head(); | |
544 | /* | |
545 | * For each probe event, if we find that a probe event matches | |
546 | * our enabler, create an associated lttng_event if not | |
547 | * already present. | |
548 | */ | |
549 | cds_list_for_each_entry(probe_desc, probe_list, head) { | |
550 | for (i = 0; i < probe_desc->nr_events; i++) { | |
551 | int found = 0, ret; | |
d56fa719 MD |
552 | struct cds_hlist_head *head; |
553 | struct cds_hlist_node *node; | |
554 | const char *event_name; | |
555 | size_t name_len; | |
556 | uint32_t hash; | |
e58095ef MD |
557 | |
558 | desc = probe_desc->event_desc[i]; | |
559 | if (!lttng_desc_match_enabler(desc, enabler)) | |
560 | continue; | |
d56fa719 MD |
561 | event_name = desc->name; |
562 | name_len = strlen(event_name); | |
e58095ef MD |
563 | |
564 | /* | |
d56fa719 | 565 | * Check if already created. |
e58095ef | 566 | */ |
d56fa719 MD |
567 | hash = jhash(event_name, name_len, 0); |
568 | head = &session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)]; | |
569 | cds_hlist_for_each_entry(event, node, head, hlist) { | |
d970f72e MD |
570 | if (event->desc == desc |
571 | && event->chan == enabler->chan) | |
e58095ef MD |
572 | found = 1; |
573 | } | |
574 | if (found) | |
575 | continue; | |
576 | ||
577 | /* | |
578 | * We need to create an event for this | |
579 | * event probe. | |
580 | */ | |
581 | ret = lttng_event_create(probe_desc->event_desc[i], | |
582 | enabler->chan); | |
583 | if (ret) { | |
32ce8569 MD |
584 | DBG("Unable to create event %s, error %d\n", |
585 | probe_desc->event_desc[i]->name, ret); | |
e58095ef | 586 | } |
8165c8da | 587 | } |
8020ceb5 | 588 | } |
8020ceb5 MD |
589 | } |
590 | ||
591 | /* | |
e58095ef MD |
592 | * Create events associated with an enabler (if not already present), |
593 | * and add backward reference from the event to the enabler. | |
8020ceb5 MD |
594 | */ |
595 | static | |
e58095ef | 596 | int lttng_enabler_ref_events(struct lttng_enabler *enabler) |
8020ceb5 | 597 | { |
e58095ef MD |
598 | struct lttng_session *session = enabler->chan->session; |
599 | struct lttng_event *event; | |
600 | ||
601 | /* First ensure that probe events are created for this enabler. */ | |
602 | lttng_create_event_if_missing(enabler); | |
603 | ||
604 | /* For each event matching enabler in session event list. */ | |
605 | cds_list_for_each_entry(event, &session->events_head, node) { | |
606 | struct lttng_enabler_ref *enabler_ref; | |
607 | ||
608 | if (!lttng_event_match_enabler(event, enabler)) | |
609 | continue; | |
610 | ||
611 | enabler_ref = lttng_event_enabler_ref(event, enabler); | |
612 | if (!enabler_ref) { | |
613 | /* | |
614 | * If no backward ref, create it. | |
615 | * Add backward ref from event to enabler. | |
616 | */ | |
617 | enabler_ref = zmalloc(sizeof(*enabler_ref)); | |
618 | if (!enabler_ref) | |
619 | return -ENOMEM; | |
620 | enabler_ref->ref = enabler; | |
621 | cds_list_add(&enabler_ref->node, | |
622 | &event->enablers_ref_head); | |
8165c8da | 623 | } |
e58095ef MD |
624 | |
625 | /* | |
626 | * Link filter bytecodes if not linked yet. | |
627 | */ | |
628 | lttng_enabler_event_link_bytecode(event, enabler); | |
629 | ||
630 | /* TODO: merge event context. */ | |
631 | } | |
632 | return 0; | |
633 | } | |
634 | ||
635 | /* | |
636 | * Called at library load: connect the probe on all enablers matching | |
637 | * this event. | |
5f733922 | 638 | * Called with session mutex held. |
e58095ef | 639 | */ |
5f733922 | 640 | int lttng_fix_pending_events(void) |
e58095ef MD |
641 | { |
642 | struct lttng_session *session; | |
643 | ||
644 | cds_list_for_each_entry(session, &sessions, node) { | |
645 | lttng_session_lazy_sync_enablers(session); | |
8020ceb5 | 646 | } |
e58095ef MD |
647 | return 0; |
648 | } | |
649 | ||
e58095ef MD |
650 | /* |
651 | * Only used internally at session destruction. | |
652 | */ | |
653 | static | |
654 | void _lttng_event_destroy(struct lttng_event *event) | |
655 | { | |
656 | struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref; | |
657 | ||
e58095ef | 658 | cds_list_del(&event->node); |
8020ceb5 | 659 | lttng_destroy_context(event->ctx); |
f488575f | 660 | lttng_free_event_filter_runtime(event); |
e58095ef MD |
661 | /* Free event enabler refs */ |
662 | cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref, | |
663 | &event->enablers_ref_head, node) | |
664 | free(enabler_ref); | |
b5234c06 | 665 | free(event); |
8020ceb5 MD |
666 | } |
667 | ||
003fedf4 | 668 | void lttng_ust_events_exit(void) |
8020ceb5 | 669 | { |
7dd08bec | 670 | struct lttng_session *session, *tmpsession; |
8020ceb5 | 671 | |
e58095ef | 672 | cds_list_for_each_entry_safe(session, tmpsession, &sessions, node) |
7dd08bec | 673 | lttng_session_destroy(session); |
8020ceb5 | 674 | } |
457a6b58 | 675 | |
e58095ef MD |
676 | /* |
677 | * Enabler management. | |
678 | */ | |
679 | struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type, | |
680 | struct lttng_ust_event *event_param, | |
681 | struct lttng_channel *chan) | |
457a6b58 | 682 | { |
e58095ef MD |
683 | struct lttng_enabler *enabler; |
684 | ||
685 | enabler = zmalloc(sizeof(*enabler)); | |
686 | if (!enabler) | |
687 | return NULL; | |
688 | enabler->type = type; | |
689 | CDS_INIT_LIST_HEAD(&enabler->filter_bytecode_head); | |
690 | memcpy(&enabler->event_param, event_param, | |
691 | sizeof(enabler->event_param)); | |
692 | enabler->chan = chan; | |
693 | /* ctx left NULL */ | |
694 | enabler->enabled = 1; | |
695 | cds_list_add(&enabler->node, &enabler->chan->session->enablers_head); | |
696 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
697 | return enabler; | |
457a6b58 MD |
698 | } |
699 | ||
e58095ef | 700 | int lttng_enabler_enable(struct lttng_enabler *enabler) |
457a6b58 | 701 | { |
e58095ef MD |
702 | enabler->enabled = 1; |
703 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
704 | return 0; | |
457a6b58 | 705 | } |
457a6b58 | 706 | |
e58095ef | 707 | int lttng_enabler_disable(struct lttng_enabler *enabler) |
457a6b58 | 708 | { |
e58095ef MD |
709 | enabler->enabled = 0; |
710 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
711 | return 0; | |
712 | } | |
457a6b58 | 713 | |
e58095ef MD |
714 | int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler, |
715 | struct lttng_ust_filter_bytecode_node *bytecode) | |
716 | { | |
717 | bytecode->enabler = enabler; | |
718 | cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head); | |
719 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
720 | return 0; | |
721 | } | |
f488575f | 722 | |
e58095ef MD |
723 | int lttng_attach_context(struct lttng_ust_context *context_param, |
724 | struct lttng_ctx **ctx, struct lttng_session *session) | |
725 | { | |
457a6b58 | 726 | /* |
e58095ef MD |
727 | * We cannot attach a context after trace has been started for a |
728 | * session because the metadata does not allow expressing this | |
729 | * information outside of the original channel scope. | |
457a6b58 | 730 | */ |
e58095ef MD |
731 | if (session->been_active) |
732 | return -EPERM; | |
457a6b58 | 733 | |
e58095ef MD |
734 | switch (context_param->ctx) { |
735 | case LTTNG_UST_CONTEXT_PTHREAD_ID: | |
736 | return lttng_add_pthread_id_to_ctx(ctx); | |
737 | case LTTNG_UST_CONTEXT_VTID: | |
738 | return lttng_add_vtid_to_ctx(ctx); | |
739 | case LTTNG_UST_CONTEXT_VPID: | |
740 | return lttng_add_vpid_to_ctx(ctx); | |
741 | case LTTNG_UST_CONTEXT_PROCNAME: | |
742 | return lttng_add_procname_to_ctx(ctx); | |
96f85541 MD |
743 | case LTTNG_UST_CONTEXT_IP: |
744 | return lttng_add_ip_to_ctx(ctx); | |
e58095ef MD |
745 | default: |
746 | return -EINVAL; | |
457a6b58 | 747 | } |
457a6b58 MD |
748 | } |
749 | ||
e58095ef MD |
750 | int lttng_enabler_attach_context(struct lttng_enabler *enabler, |
751 | struct lttng_ust_context *context_param) | |
457a6b58 | 752 | { |
e58095ef MD |
753 | #if 0 // disabled for now. |
754 | struct lttng_session *session = enabler->chan->session; | |
755 | int ret; | |
457a6b58 | 756 | |
e58095ef MD |
757 | ret = lttng_attach_context(context_param, &enabler->ctx, |
758 | session); | |
759 | if (ret) | |
760 | return ret; | |
761 | lttng_session_lazy_sync_enablers(enabler->chan->session); | |
762 | #endif | |
763 | return -ENOSYS; | |
457a6b58 MD |
764 | } |
765 | ||
e58095ef MD |
766 | static |
767 | void lttng_enabler_destroy(struct lttng_enabler *enabler) | |
457a6b58 | 768 | { |
e58095ef | 769 | struct lttng_ust_filter_bytecode_node *filter_node, *tmp_filter_node; |
457a6b58 | 770 | |
e58095ef MD |
771 | /* Destroy filter bytecode */ |
772 | cds_list_for_each_entry_safe(filter_node, tmp_filter_node, | |
773 | &enabler->filter_bytecode_head, node) { | |
774 | free(filter_node); | |
457a6b58 | 775 | } |
e58095ef MD |
776 | |
777 | /* Destroy contexts */ | |
778 | lttng_destroy_context(enabler->ctx); | |
779 | ||
780 | cds_list_del(&enabler->node); | |
781 | free(enabler); | |
457a6b58 MD |
782 | } |
783 | ||
e58095ef MD |
784 | /* |
785 | * lttng_session_sync_enablers should be called just before starting a | |
786 | * session. | |
787 | */ | |
457a6b58 | 788 | static |
e58095ef | 789 | void lttng_session_sync_enablers(struct lttng_session *session) |
457a6b58 | 790 | { |
e58095ef MD |
791 | struct lttng_enabler *enabler; |
792 | struct lttng_event *event; | |
457a6b58 | 793 | |
e58095ef MD |
794 | cds_list_for_each_entry(enabler, &session->enablers_head, node) |
795 | lttng_enabler_ref_events(enabler); | |
796 | /* | |
797 | * For each event, if at least one of its enablers is enabled, | |
ac6b4ac6 MD |
798 | * and its channel and session transient states are enabled, we |
799 | * enable the event, else we disable it. | |
e58095ef MD |
800 | */ |
801 | cds_list_for_each_entry(event, &session->events_head, node) { | |
802 | struct lttng_enabler_ref *enabler_ref; | |
803 | struct lttng_bytecode_runtime *runtime; | |
dcdeaff0 | 804 | int enabled = 0, has_enablers_without_bytecode = 0; |
e58095ef MD |
805 | |
806 | /* Enable events */ | |
807 | cds_list_for_each_entry(enabler_ref, | |
808 | &event->enablers_ref_head, node) { | |
809 | if (enabler_ref->ref->enabled) { | |
810 | enabled = 1; | |
811 | break; | |
812 | } | |
813 | } | |
ac6b4ac6 MD |
814 | /* |
815 | * Enabled state is based on union of enablers, with | |
816 | * intesection of session and channel transient enable | |
817 | * states. | |
818 | */ | |
819 | enabled = enabled && session->tstate && event->chan->tstate; | |
820 | ||
821 | CMM_STORE_SHARED(event->enabled, enabled); | |
822 | /* | |
823 | * Sync tracepoint registration with event enabled | |
824 | * state. | |
825 | */ | |
826 | if (enabled) { | |
827 | if (!event->registered) | |
828 | register_event(event); | |
829 | } else { | |
830 | if (event->registered) | |
831 | unregister_event(event); | |
832 | } | |
457a6b58 | 833 | |
1f49fc05 | 834 | /* Check if has enablers without bytecode enabled */ |
dcdeaff0 MD |
835 | cds_list_for_each_entry(enabler_ref, |
836 | &event->enablers_ref_head, node) { | |
1f49fc05 MD |
837 | if (enabler_ref->ref->enabled |
838 | && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) { | |
dcdeaff0 MD |
839 | has_enablers_without_bytecode = 1; |
840 | break; | |
841 | } | |
842 | } | |
843 | event->has_enablers_without_bytecode = | |
844 | has_enablers_without_bytecode; | |
845 | ||
e58095ef MD |
846 | /* Enable filters */ |
847 | cds_list_for_each_entry(runtime, | |
848 | &event->bytecode_runtime_head, node) { | |
849 | lttng_filter_sync_state(runtime); | |
457a6b58 MD |
850 | } |
851 | } | |
457a6b58 MD |
852 | } |
853 | ||
e58095ef MD |
854 | /* |
855 | * Apply enablers to session events, adding events to session if need | |
856 | * be. It is required after each modification applied to an active | |
857 | * session, and right before session "start". | |
858 | * "lazy" sync means we only sync if required. | |
859 | */ | |
860 | static | |
861 | void lttng_session_lazy_sync_enablers(struct lttng_session *session) | |
457a6b58 | 862 | { |
e58095ef MD |
863 | /* We can skip if session is not active */ |
864 | if (!session->active) | |
865 | return; | |
866 | lttng_session_sync_enablers(session); | |
457a6b58 | 867 | } |