Commit | Line | Data |
---|---|---|
91d76f53 | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
91d76f53 | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
91d76f53 | 6 | * |
91d76f53 DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
7972aab2 | 10 | #include <inttypes.h> |
91d76f53 DG |
11 | #include <pthread.h> |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
099e26bd | 14 | #include <string.h> |
aba8e916 DG |
15 | #include <sys/stat.h> |
16 | #include <sys/types.h> | |
099e26bd | 17 | #include <unistd.h> |
0df502fd | 18 | #include <urcu/compiler.h> |
331744e3 | 19 | #include <signal.h> |
bec39940 | 20 | |
763f0d4c | 21 | #include <common/bytecode/bytecode.h> |
edf4b93e | 22 | #include <common/compat/errno.h> |
990570ed | 23 | #include <common/common.h> |
993578ff JR |
24 | #include <common/hashtable/utils.h> |
25 | #include <lttng/event-rule/event-rule.h> | |
26 | #include <lttng/event-rule/event-rule-internal.h> | |
27 | #include <lttng/event-rule/tracepoint.h> | |
28 | #include <lttng/condition/condition.h> | |
29 | #include <lttng/condition/event-rule-internal.h> | |
30 | #include <lttng/condition/event-rule.h> | |
86acf0da | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 32 | |
7972aab2 | 33 | #include "buffer-registry.h" |
86acf0da | 34 | #include "fd-limit.h" |
8782cc74 | 35 | #include "health-sessiond.h" |
56fff090 | 36 | #include "ust-app.h" |
48842b30 | 37 | #include "ust-consumer.h" |
75018ab6 JG |
38 | #include "lttng-ust-ctl.h" |
39 | #include "lttng-ust-error.h" | |
0b2dc8df | 40 | #include "utils.h" |
fb83fe64 | 41 | #include "session.h" |
e9404c27 JG |
42 | #include "lttng-sessiond.h" |
43 | #include "notification-thread-commands.h" | |
5c408ad8 | 44 | #include "rotate.h" |
44760c20 | 45 | #include "event.h" |
d80a6244 | 46 | |
44cdb3a2 MJ |
47 | struct lttng_ht *ust_app_ht; |
48 | struct lttng_ht *ust_app_ht_by_sock; | |
49 | struct lttng_ht *ust_app_ht_by_notify_sock; | |
50 | ||
c4b88406 MD |
51 | static |
52 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); | |
53 | ||
d9bf3ca4 MD |
54 | /* Next available channel key. Access under next_channel_key_lock. */ |
55 | static uint64_t _next_channel_key; | |
56 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
57 | ||
58 | /* Next available session ID. Access under next_session_id_lock. */ | |
59 | static uint64_t _next_session_id; | |
60 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
ffe60014 DG |
61 | |
62 | /* | |
d9bf3ca4 | 63 | * Return the incremented value of next_channel_key. |
ffe60014 | 64 | */ |
d9bf3ca4 | 65 | static uint64_t get_next_channel_key(void) |
ffe60014 | 66 | { |
d9bf3ca4 MD |
67 | uint64_t ret; |
68 | ||
69 | pthread_mutex_lock(&next_channel_key_lock); | |
70 | ret = ++_next_channel_key; | |
71 | pthread_mutex_unlock(&next_channel_key_lock); | |
72 | return ret; | |
ffe60014 DG |
73 | } |
74 | ||
75 | /* | |
7972aab2 | 76 | * Return the atomically incremented value of next_session_id. |
ffe60014 | 77 | */ |
d9bf3ca4 | 78 | static uint64_t get_next_session_id(void) |
ffe60014 | 79 | { |
d9bf3ca4 MD |
80 | uint64_t ret; |
81 | ||
82 | pthread_mutex_lock(&next_session_id_lock); | |
83 | ret = ++_next_session_id; | |
84 | pthread_mutex_unlock(&next_session_id_lock); | |
85 | return ret; | |
ffe60014 DG |
86 | } |
87 | ||
d65d2de8 DG |
88 | static void copy_channel_attr_to_ustctl( |
89 | struct ustctl_consumer_channel_attr *attr, | |
90 | struct lttng_ust_channel_attr *uattr) | |
91 | { | |
92 | /* Copy event attributes since the layout is different. */ | |
93 | attr->subbuf_size = uattr->subbuf_size; | |
94 | attr->num_subbuf = uattr->num_subbuf; | |
95 | attr->overwrite = uattr->overwrite; | |
96 | attr->switch_timer_interval = uattr->switch_timer_interval; | |
97 | attr->read_timer_interval = uattr->read_timer_interval; | |
98 | attr->output = uattr->output; | |
491d1539 | 99 | attr->blocking_timeout = uattr->u.s.blocking_timeout; |
d65d2de8 DG |
100 | } |
101 | ||
025faf73 DG |
102 | /* |
103 | * Match function for the hash table lookup. | |
104 | * | |
105 | * It matches an ust app event based on three attributes which are the event | |
106 | * name, the filter bytecode and the loglevel. | |
107 | */ | |
18eace3b DG |
108 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
109 | { | |
110 | struct ust_app_event *event; | |
111 | const struct ust_app_ht_key *key; | |
2106efa0 | 112 | int ev_loglevel_value; |
18eace3b DG |
113 | |
114 | assert(node); | |
115 | assert(_key); | |
116 | ||
117 | event = caa_container_of(node, struct ust_app_event, node.node); | |
118 | key = _key; | |
2106efa0 | 119 | ev_loglevel_value = event->attr.loglevel; |
18eace3b | 120 | |
1af53eb5 | 121 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
18eace3b DG |
122 | |
123 | /* Event name */ | |
124 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
125 | goto no_match; | |
126 | } | |
127 | ||
128 | /* Event loglevel. */ | |
2106efa0 | 129 | if (ev_loglevel_value != key->loglevel_type) { |
025faf73 | 130 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
2106efa0 PP |
131 | && key->loglevel_type == 0 && |
132 | ev_loglevel_value == -1) { | |
025faf73 DG |
133 | /* |
134 | * Match is accepted. This is because on event creation, the | |
135 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and | |
136 | * -1 are accepted for this loglevel type since 0 is the one set by | |
137 | * the API when receiving an enable event. | |
138 | */ | |
139 | } else { | |
140 | goto no_match; | |
141 | } | |
18eace3b DG |
142 | } |
143 | ||
144 | /* One of the filters is NULL, fail. */ | |
145 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
146 | goto no_match; | |
147 | } | |
148 | ||
025faf73 DG |
149 | if (key->filter && event->filter) { |
150 | /* Both filters exists, check length followed by the bytecode. */ | |
151 | if (event->filter->len != key->filter->len || | |
152 | memcmp(event->filter->data, key->filter->data, | |
153 | event->filter->len) != 0) { | |
154 | goto no_match; | |
155 | } | |
18eace3b DG |
156 | } |
157 | ||
1af53eb5 JI |
158 | /* One of the exclusions is NULL, fail. */ |
159 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
160 | goto no_match; | |
161 | } | |
162 | ||
163 | if (key->exclusion && event->exclusion) { | |
164 | /* Both exclusions exists, check count followed by the names. */ | |
165 | if (event->exclusion->count != key->exclusion->count || | |
166 | memcmp(event->exclusion->names, key->exclusion->names, | |
167 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { | |
168 | goto no_match; | |
169 | } | |
170 | } | |
171 | ||
172 | ||
025faf73 | 173 | /* Match. */ |
18eace3b DG |
174 | return 1; |
175 | ||
176 | no_match: | |
177 | return 0; | |
18eace3b DG |
178 | } |
179 | ||
025faf73 DG |
180 | /* |
181 | * Unique add of an ust app event in the given ht. This uses the custom | |
182 | * ht_match_ust_app_event match function and the event name as hash. | |
183 | */ | |
d0b96690 | 184 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
18eace3b DG |
185 | struct ust_app_event *event) |
186 | { | |
187 | struct cds_lfht_node *node_ptr; | |
188 | struct ust_app_ht_key key; | |
d0b96690 | 189 | struct lttng_ht *ht; |
18eace3b | 190 | |
d0b96690 DG |
191 | assert(ua_chan); |
192 | assert(ua_chan->events); | |
18eace3b DG |
193 | assert(event); |
194 | ||
d0b96690 | 195 | ht = ua_chan->events; |
18eace3b DG |
196 | key.name = event->attr.name; |
197 | key.filter = event->filter; | |
2106efa0 | 198 | key.loglevel_type = event->attr.loglevel; |
91c89f23 | 199 | key.exclusion = event->exclusion; |
18eace3b DG |
200 | |
201 | node_ptr = cds_lfht_add_unique(ht->ht, | |
202 | ht->hash_fct(event->node.key, lttng_ht_seed), | |
203 | ht_match_ust_app_event, &key, &event->node.node); | |
204 | assert(node_ptr == &event->node.node); | |
205 | } | |
206 | ||
d88aee68 DG |
207 | /* |
208 | * Close the notify socket from the given RCU head object. This MUST be called | |
209 | * through a call_rcu(). | |
210 | */ | |
211 | static void close_notify_sock_rcu(struct rcu_head *head) | |
212 | { | |
213 | int ret; | |
214 | struct ust_app_notify_sock_obj *obj = | |
215 | caa_container_of(head, struct ust_app_notify_sock_obj, head); | |
216 | ||
217 | /* Must have a valid fd here. */ | |
218 | assert(obj->fd >= 0); | |
219 | ||
220 | ret = close(obj->fd); | |
221 | if (ret) { | |
222 | ERR("close notify sock %d RCU", obj->fd); | |
223 | } | |
224 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
225 | ||
226 | free(obj); | |
227 | } | |
228 | ||
7972aab2 DG |
229 | /* |
230 | * Return the session registry according to the buffer type of the given | |
231 | * session. | |
232 | * | |
233 | * A registry per UID object MUST exists before calling this function or else | |
234 | * it assert() if not found. RCU read side lock must be acquired. | |
235 | */ | |
236 | static struct ust_registry_session *get_session_registry( | |
237 | struct ust_app_session *ua_sess) | |
238 | { | |
239 | struct ust_registry_session *registry = NULL; | |
240 | ||
241 | assert(ua_sess); | |
242 | ||
243 | switch (ua_sess->buffer_type) { | |
244 | case LTTNG_BUFFER_PER_PID: | |
245 | { | |
246 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
247 | if (!reg_pid) { | |
248 | goto error; | |
249 | } | |
250 | registry = reg_pid->registry->reg.ust; | |
251 | break; | |
252 | } | |
253 | case LTTNG_BUFFER_PER_UID: | |
254 | { | |
255 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( | |
470cc211 | 256 | ua_sess->tracing_id, ua_sess->bits_per_long, |
ff588497 | 257 | lttng_credentials_get_uid(&ua_sess->real_credentials)); |
7972aab2 DG |
258 | if (!reg_uid) { |
259 | goto error; | |
260 | } | |
261 | registry = reg_uid->registry->reg.ust; | |
262 | break; | |
263 | } | |
264 | default: | |
265 | assert(0); | |
266 | }; | |
267 | ||
268 | error: | |
269 | return registry; | |
270 | } | |
271 | ||
55cc08a6 DG |
272 | /* |
273 | * Delete ust context safely. RCU read lock must be held before calling | |
274 | * this function. | |
275 | */ | |
276 | static | |
fb45065e MD |
277 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, |
278 | struct ust_app *app) | |
55cc08a6 | 279 | { |
ffe60014 DG |
280 | int ret; |
281 | ||
282 | assert(ua_ctx); | |
283 | ||
55cc08a6 | 284 | if (ua_ctx->obj) { |
fb45065e | 285 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 286 | ret = ustctl_release_object(sock, ua_ctx->obj); |
fb45065e | 287 | pthread_mutex_unlock(&app->sock_lock); |
d0b96690 DG |
288 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
289 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", | |
290 | sock, ua_ctx->obj->handle, ret); | |
ffe60014 | 291 | } |
55cc08a6 DG |
292 | free(ua_ctx->obj); |
293 | } | |
294 | free(ua_ctx); | |
295 | } | |
296 | ||
d80a6244 DG |
297 | /* |
298 | * Delete ust app event safely. RCU read lock must be held before calling | |
299 | * this function. | |
300 | */ | |
8b366481 | 301 | static |
fb45065e MD |
302 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event, |
303 | struct ust_app *app) | |
d80a6244 | 304 | { |
ffe60014 DG |
305 | int ret; |
306 | ||
307 | assert(ua_event); | |
308 | ||
53a80697 | 309 | free(ua_event->filter); |
951f0b71 JI |
310 | if (ua_event->exclusion != NULL) |
311 | free(ua_event->exclusion); | |
edb67388 | 312 | if (ua_event->obj != NULL) { |
fb45065e | 313 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 314 | ret = ustctl_release_object(sock, ua_event->obj); |
fb45065e | 315 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
316 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
317 | ERR("UST app sock %d release event obj failed with ret %d", | |
318 | sock, ret); | |
319 | } | |
edb67388 DG |
320 | free(ua_event->obj); |
321 | } | |
d80a6244 DG |
322 | free(ua_event); |
323 | } | |
324 | ||
993578ff JR |
325 | /* |
326 | * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called | |
327 | * through a call_rcu(). | |
328 | */ | |
329 | static | |
330 | void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head) | |
331 | { | |
332 | struct ust_app_event_notifier_rule *obj = caa_container_of( | |
333 | head, struct ust_app_event_notifier_rule, rcu_head); | |
334 | ||
335 | free(obj); | |
336 | } | |
337 | ||
338 | /* | |
339 | * Delete ust app event notifier rule safely. | |
340 | */ | |
341 | static void delete_ust_app_event_notifier_rule(int sock, | |
342 | struct ust_app_event_notifier_rule *ua_event_notifier_rule, | |
343 | struct ust_app *app) | |
344 | { | |
345 | int ret; | |
346 | ||
347 | assert(ua_event_notifier_rule); | |
348 | ||
349 | if (ua_event_notifier_rule->exclusion != NULL) { | |
350 | free(ua_event_notifier_rule->exclusion); | |
351 | } | |
352 | ||
353 | if (ua_event_notifier_rule->obj != NULL) { | |
354 | pthread_mutex_lock(&app->sock_lock); | |
355 | ret = ustctl_release_object(sock, ua_event_notifier_rule->obj); | |
356 | pthread_mutex_unlock(&app->sock_lock); | |
357 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
358 | ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d", | |
359 | app->name, (int) app->ppid, ret); | |
360 | } | |
361 | ||
362 | free(ua_event_notifier_rule->obj); | |
363 | } | |
364 | ||
267d66aa | 365 | lttng_trigger_put(ua_event_notifier_rule->trigger); |
993578ff JR |
366 | call_rcu(&ua_event_notifier_rule->rcu_head, |
367 | free_ust_app_event_notifier_rule_rcu); | |
368 | } | |
369 | ||
d80a6244 | 370 | /* |
7972aab2 DG |
371 | * Release ust data object of the given stream. |
372 | * | |
373 | * Return 0 on success or else a negative value. | |
d80a6244 | 374 | */ |
fb45065e MD |
375 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, |
376 | struct ust_app *app) | |
d80a6244 | 377 | { |
7972aab2 | 378 | int ret = 0; |
ffe60014 DG |
379 | |
380 | assert(stream); | |
381 | ||
8b366481 | 382 | if (stream->obj) { |
fb45065e | 383 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 384 | ret = ustctl_release_object(sock, stream->obj); |
fb45065e | 385 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
386 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
387 | ERR("UST app sock %d release stream obj failed with ret %d", | |
388 | sock, ret); | |
389 | } | |
4063050c | 390 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
391 | free(stream->obj); |
392 | } | |
7972aab2 DG |
393 | |
394 | return ret; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Delete ust app stream safely. RCU read lock must be held before calling | |
399 | * this function. | |
400 | */ | |
401 | static | |
fb45065e MD |
402 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream, |
403 | struct ust_app *app) | |
7972aab2 DG |
404 | { |
405 | assert(stream); | |
406 | ||
fb45065e | 407 | (void) release_ust_app_stream(sock, stream, app); |
84cd17c6 | 408 | free(stream); |
d80a6244 DG |
409 | } |
410 | ||
36b588ed MD |
411 | /* |
412 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
413 | * section and outside of call_rcu thread, so we postpone its execution |
414 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
415 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
416 | */ |
417 | static | |
418 | void delete_ust_app_channel_rcu(struct rcu_head *head) | |
419 | { | |
420 | struct ust_app_channel *ua_chan = | |
421 | caa_container_of(head, struct ust_app_channel, rcu_head); | |
422 | ||
0b2dc8df MD |
423 | ht_cleanup_push(ua_chan->ctx); |
424 | ht_cleanup_push(ua_chan->events); | |
36b588ed MD |
425 | free(ua_chan); |
426 | } | |
427 | ||
fb83fe64 JD |
428 | /* |
429 | * Extract the lost packet or discarded events counter when the channel is | |
430 | * being deleted and store the value in the parent channel so we can | |
431 | * access it from lttng list and at stop/destroy. | |
82cac6d2 JG |
432 | * |
433 | * The session list lock must be held by the caller. | |
fb83fe64 JD |
434 | */ |
435 | static | |
436 | void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan) | |
437 | { | |
438 | uint64_t discarded = 0, lost = 0; | |
439 | struct ltt_session *session; | |
440 | struct ltt_ust_channel *uchan; | |
441 | ||
442 | if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) { | |
443 | return; | |
444 | } | |
445 | ||
446 | rcu_read_lock(); | |
447 | session = session_find_by_id(ua_chan->session->tracing_id); | |
d68ec974 JG |
448 | if (!session || !session->ust_session) { |
449 | /* | |
450 | * Not finding the session is not an error because there are | |
451 | * multiple ways the channels can be torn down. | |
452 | * | |
453 | * 1) The session daemon can initiate the destruction of the | |
454 | * ust app session after receiving a destroy command or | |
455 | * during its shutdown/teardown. | |
456 | * 2) The application, since we are in per-pid tracing, is | |
457 | * unregistering and tearing down its ust app session. | |
458 | * | |
459 | * Both paths are protected by the session list lock which | |
460 | * ensures that the accounting of lost packets and discarded | |
461 | * events is done exactly once. The session is then unpublished | |
462 | * from the session list, resulting in this condition. | |
463 | */ | |
fb83fe64 JD |
464 | goto end; |
465 | } | |
466 | ||
467 | if (ua_chan->attr.overwrite) { | |
468 | consumer_get_lost_packets(ua_chan->session->tracing_id, | |
469 | ua_chan->key, session->ust_session->consumer, | |
470 | &lost); | |
471 | } else { | |
472 | consumer_get_discarded_events(ua_chan->session->tracing_id, | |
473 | ua_chan->key, session->ust_session->consumer, | |
474 | &discarded); | |
475 | } | |
476 | uchan = trace_ust_find_channel_by_name( | |
477 | session->ust_session->domain_global.channels, | |
478 | ua_chan->name); | |
479 | if (!uchan) { | |
480 | ERR("Missing UST channel to store discarded counters"); | |
481 | goto end; | |
482 | } | |
483 | ||
484 | uchan->per_pid_closed_app_discarded += discarded; | |
485 | uchan->per_pid_closed_app_lost += lost; | |
486 | ||
487 | end: | |
488 | rcu_read_unlock(); | |
e32d7f27 JG |
489 | if (session) { |
490 | session_put(session); | |
491 | } | |
fb83fe64 JD |
492 | } |
493 | ||
d80a6244 DG |
494 | /* |
495 | * Delete ust app channel safely. RCU read lock must be held before calling | |
496 | * this function. | |
82cac6d2 JG |
497 | * |
498 | * The session list lock must be held by the caller. | |
d80a6244 | 499 | */ |
8b366481 | 500 | static |
d0b96690 DG |
501 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
502 | struct ust_app *app) | |
d80a6244 DG |
503 | { |
504 | int ret; | |
bec39940 | 505 | struct lttng_ht_iter iter; |
d80a6244 | 506 | struct ust_app_event *ua_event; |
55cc08a6 | 507 | struct ust_app_ctx *ua_ctx; |
030a66fa | 508 | struct ust_app_stream *stream, *stmp; |
7972aab2 | 509 | struct ust_registry_session *registry; |
d80a6244 | 510 | |
ffe60014 DG |
511 | assert(ua_chan); |
512 | ||
513 | DBG3("UST app deleting channel %s", ua_chan->name); | |
514 | ||
55cc08a6 | 515 | /* Wipe stream */ |
d80a6244 | 516 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
84cd17c6 | 517 | cds_list_del(&stream->list); |
fb45065e | 518 | delete_ust_app_stream(sock, stream, app); |
d80a6244 DG |
519 | } |
520 | ||
55cc08a6 | 521 | /* Wipe context */ |
bec39940 | 522 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
31746f93 | 523 | cds_list_del(&ua_ctx->list); |
bec39940 | 524 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
55cc08a6 | 525 | assert(!ret); |
fb45065e | 526 | delete_ust_app_ctx(sock, ua_ctx, app); |
55cc08a6 | 527 | } |
d80a6244 | 528 | |
55cc08a6 | 529 | /* Wipe events */ |
bec39940 DG |
530 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
531 | node.node) { | |
532 | ret = lttng_ht_del(ua_chan->events, &iter); | |
525b0740 | 533 | assert(!ret); |
fb45065e | 534 | delete_ust_app_event(sock, ua_event, app); |
d80a6244 | 535 | } |
edb67388 | 536 | |
c8335706 MD |
537 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
538 | /* Wipe and free registry from session registry. */ | |
539 | registry = get_session_registry(ua_chan->session); | |
540 | if (registry) { | |
e9404c27 | 541 | ust_registry_channel_del_free(registry, ua_chan->key, |
e38d96f9 MD |
542 | sock >= 0); |
543 | } | |
45798a31 JG |
544 | /* |
545 | * A negative socket can be used by the caller when | |
546 | * cleaning-up a ua_chan in an error path. Skip the | |
547 | * accounting in this case. | |
548 | */ | |
e38d96f9 MD |
549 | if (sock >= 0) { |
550 | save_per_pid_lost_discarded_counters(ua_chan); | |
c8335706 | 551 | } |
7972aab2 | 552 | } |
d0b96690 | 553 | |
edb67388 | 554 | if (ua_chan->obj != NULL) { |
d0b96690 DG |
555 | /* Remove channel from application UST object descriptor. */ |
556 | iter.iter.node = &ua_chan->ust_objd_node.node; | |
c6e62271 DG |
557 | ret = lttng_ht_del(app->ust_objd, &iter); |
558 | assert(!ret); | |
fb45065e | 559 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 560 | ret = ustctl_release_object(sock, ua_chan->obj); |
fb45065e | 561 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
562 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
563 | ERR("UST app sock %d release channel obj failed with ret %d", | |
564 | sock, ret); | |
565 | } | |
7972aab2 | 566 | lttng_fd_put(LTTNG_FD_APPS, 1); |
edb67388 DG |
567 | free(ua_chan->obj); |
568 | } | |
36b588ed | 569 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
d80a6244 DG |
570 | } |
571 | ||
fb45065e MD |
572 | int ust_app_register_done(struct ust_app *app) |
573 | { | |
574 | int ret; | |
575 | ||
576 | pthread_mutex_lock(&app->sock_lock); | |
577 | ret = ustctl_register_done(app->sock); | |
578 | pthread_mutex_unlock(&app->sock_lock); | |
579 | return ret; | |
580 | } | |
581 | ||
582 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data) | |
583 | { | |
584 | int ret, sock; | |
585 | ||
586 | if (app) { | |
587 | pthread_mutex_lock(&app->sock_lock); | |
588 | sock = app->sock; | |
589 | } else { | |
590 | sock = -1; | |
591 | } | |
592 | ret = ustctl_release_object(sock, data); | |
593 | if (app) { | |
594 | pthread_mutex_unlock(&app->sock_lock); | |
595 | } | |
596 | return ret; | |
597 | } | |
598 | ||
331744e3 | 599 | /* |
1b532a60 DG |
600 | * Push metadata to consumer socket. |
601 | * | |
dc2bbdae MD |
602 | * RCU read-side lock must be held to guarantee existance of socket. |
603 | * Must be called with the ust app session lock held. | |
604 | * Must be called with the registry lock held. | |
331744e3 JD |
605 | * |
606 | * On success, return the len of metadata pushed or else a negative value. | |
2c57e06d MD |
607 | * Returning a -EPIPE return value means we could not send the metadata, |
608 | * but it can be caused by recoverable errors (e.g. the application has | |
609 | * terminated concurrently). | |
331744e3 JD |
610 | */ |
611 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, | |
612 | struct consumer_socket *socket, int send_zero_data) | |
613 | { | |
614 | int ret; | |
615 | char *metadata_str = NULL; | |
c585821b | 616 | size_t len, offset, new_metadata_len_sent; |
331744e3 | 617 | ssize_t ret_val; |
93ec662e | 618 | uint64_t metadata_key, metadata_version; |
331744e3 JD |
619 | |
620 | assert(registry); | |
621 | assert(socket); | |
1b532a60 | 622 | |
c585821b MD |
623 | metadata_key = registry->metadata_key; |
624 | ||
ce34fcd0 | 625 | /* |
dc2bbdae MD |
626 | * Means that no metadata was assigned to the session. This can |
627 | * happens if no start has been done previously. | |
ce34fcd0 | 628 | */ |
c585821b | 629 | if (!metadata_key) { |
ce34fcd0 MD |
630 | return 0; |
631 | } | |
632 | ||
331744e3 JD |
633 | offset = registry->metadata_len_sent; |
634 | len = registry->metadata_len - registry->metadata_len_sent; | |
c585821b | 635 | new_metadata_len_sent = registry->metadata_len; |
93ec662e | 636 | metadata_version = registry->metadata_version; |
331744e3 JD |
637 | if (len == 0) { |
638 | DBG3("No metadata to push for metadata key %" PRIu64, | |
639 | registry->metadata_key); | |
640 | ret_val = len; | |
641 | if (send_zero_data) { | |
642 | DBG("No metadata to push"); | |
643 | goto push_data; | |
644 | } | |
645 | goto end; | |
646 | } | |
647 | ||
648 | /* Allocate only what we have to send. */ | |
649 | metadata_str = zmalloc(len); | |
650 | if (!metadata_str) { | |
651 | PERROR("zmalloc ust app metadata string"); | |
652 | ret_val = -ENOMEM; | |
653 | goto error; | |
654 | } | |
c585821b | 655 | /* Copy what we haven't sent out. */ |
331744e3 | 656 | memcpy(metadata_str, registry->metadata + offset, len); |
331744e3 JD |
657 | |
658 | push_data: | |
c585821b MD |
659 | pthread_mutex_unlock(®istry->lock); |
660 | /* | |
661 | * We need to unlock the registry while we push metadata to | |
662 | * break a circular dependency between the consumerd metadata | |
663 | * lock and the sessiond registry lock. Indeed, pushing metadata | |
664 | * to the consumerd awaits that it gets pushed all the way to | |
665 | * relayd, but doing so requires grabbing the metadata lock. If | |
666 | * a concurrent metadata request is being performed by | |
667 | * consumerd, this can try to grab the registry lock on the | |
668 | * sessiond while holding the metadata lock on the consumer | |
669 | * daemon. Those push and pull schemes are performed on two | |
670 | * different bidirectionnal communication sockets. | |
671 | */ | |
672 | ret = consumer_push_metadata(socket, metadata_key, | |
93ec662e | 673 | metadata_str, len, offset, metadata_version); |
c585821b | 674 | pthread_mutex_lock(®istry->lock); |
331744e3 | 675 | if (ret < 0) { |
000baf6a | 676 | /* |
dc2bbdae MD |
677 | * There is an acceptable race here between the registry |
678 | * metadata key assignment and the creation on the | |
679 | * consumer. The session daemon can concurrently push | |
680 | * metadata for this registry while being created on the | |
681 | * consumer since the metadata key of the registry is | |
682 | * assigned *before* it is setup to avoid the consumer | |
683 | * to ask for metadata that could possibly be not found | |
684 | * in the session daemon. | |
000baf6a | 685 | * |
dc2bbdae MD |
686 | * The metadata will get pushed either by the session |
687 | * being stopped or the consumer requesting metadata if | |
688 | * that race is triggered. | |
000baf6a DG |
689 | */ |
690 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { | |
691 | ret = 0; | |
c585821b MD |
692 | } else { |
693 | ERR("Error pushing metadata to consumer"); | |
000baf6a | 694 | } |
331744e3 JD |
695 | ret_val = ret; |
696 | goto error_push; | |
c585821b MD |
697 | } else { |
698 | /* | |
699 | * Metadata may have been concurrently pushed, since | |
700 | * we're not holding the registry lock while pushing to | |
701 | * consumer. This is handled by the fact that we send | |
702 | * the metadata content, size, and the offset at which | |
703 | * that metadata belongs. This may arrive out of order | |
704 | * on the consumer side, and the consumer is able to | |
705 | * deal with overlapping fragments. The consumer | |
706 | * supports overlapping fragments, which must be | |
707 | * contiguous starting from offset 0. We keep the | |
708 | * largest metadata_len_sent value of the concurrent | |
709 | * send. | |
710 | */ | |
711 | registry->metadata_len_sent = | |
712 | max_t(size_t, registry->metadata_len_sent, | |
713 | new_metadata_len_sent); | |
331744e3 | 714 | } |
331744e3 JD |
715 | free(metadata_str); |
716 | return len; | |
717 | ||
718 | end: | |
719 | error: | |
ce34fcd0 MD |
720 | if (ret_val) { |
721 | /* | |
dc2bbdae MD |
722 | * On error, flag the registry that the metadata is |
723 | * closed. We were unable to push anything and this | |
724 | * means that either the consumer is not responding or | |
725 | * the metadata cache has been destroyed on the | |
726 | * consumer. | |
ce34fcd0 MD |
727 | */ |
728 | registry->metadata_closed = 1; | |
729 | } | |
331744e3 JD |
730 | error_push: |
731 | free(metadata_str); | |
732 | return ret_val; | |
733 | } | |
734 | ||
d88aee68 | 735 | /* |
ce34fcd0 | 736 | * For a given application and session, push metadata to consumer. |
331744e3 JD |
737 | * Either sock or consumer is required : if sock is NULL, the default |
738 | * socket to send the metadata is retrieved from consumer, if sock | |
739 | * is not NULL we use it to send the metadata. | |
ce34fcd0 | 740 | * RCU read-side lock must be held while calling this function, |
dc2bbdae MD |
741 | * therefore ensuring existance of registry. It also ensures existance |
742 | * of socket throughout this function. | |
d88aee68 DG |
743 | * |
744 | * Return 0 on success else a negative error. | |
2c57e06d MD |
745 | * Returning a -EPIPE return value means we could not send the metadata, |
746 | * but it can be caused by recoverable errors (e.g. the application has | |
747 | * terminated concurrently). | |
d88aee68 | 748 | */ |
7972aab2 DG |
749 | static int push_metadata(struct ust_registry_session *registry, |
750 | struct consumer_output *consumer) | |
d88aee68 | 751 | { |
331744e3 JD |
752 | int ret_val; |
753 | ssize_t ret; | |
d88aee68 DG |
754 | struct consumer_socket *socket; |
755 | ||
7972aab2 DG |
756 | assert(registry); |
757 | assert(consumer); | |
758 | ||
ce34fcd0 | 759 | pthread_mutex_lock(®istry->lock); |
ce34fcd0 | 760 | if (registry->metadata_closed) { |
dc2bbdae MD |
761 | ret_val = -EPIPE; |
762 | goto error; | |
d88aee68 DG |
763 | } |
764 | ||
d88aee68 | 765 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
766 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
767 | consumer); | |
d88aee68 | 768 | if (!socket) { |
331744e3 | 769 | ret_val = -1; |
ce34fcd0 | 770 | goto error; |
d88aee68 DG |
771 | } |
772 | ||
331744e3 | 773 | ret = ust_app_push_metadata(registry, socket, 0); |
d88aee68 | 774 | if (ret < 0) { |
331744e3 | 775 | ret_val = ret; |
ce34fcd0 | 776 | goto error; |
d88aee68 | 777 | } |
dc2bbdae | 778 | pthread_mutex_unlock(®istry->lock); |
d88aee68 DG |
779 | return 0; |
780 | ||
ce34fcd0 | 781 | error: |
dc2bbdae | 782 | pthread_mutex_unlock(®istry->lock); |
331744e3 | 783 | return ret_val; |
d88aee68 DG |
784 | } |
785 | ||
786 | /* | |
787 | * Send to the consumer a close metadata command for the given session. Once | |
788 | * done, the metadata channel is deleted and the session metadata pointer is | |
dc2bbdae | 789 | * nullified. The session lock MUST be held unless the application is |
d88aee68 DG |
790 | * in the destroy path. |
791 | * | |
a70ac2f4 MD |
792 | * Do not hold the registry lock while communicating with the consumerd, because |
793 | * doing so causes inter-process deadlocks between consumerd and sessiond with | |
794 | * the metadata request notification. | |
795 | * | |
d88aee68 DG |
796 | * Return 0 on success else a negative value. |
797 | */ | |
7972aab2 DG |
798 | static int close_metadata(struct ust_registry_session *registry, |
799 | struct consumer_output *consumer) | |
d88aee68 DG |
800 | { |
801 | int ret; | |
802 | struct consumer_socket *socket; | |
a70ac2f4 MD |
803 | uint64_t metadata_key; |
804 | bool registry_was_already_closed; | |
d88aee68 | 805 | |
7972aab2 DG |
806 | assert(registry); |
807 | assert(consumer); | |
d88aee68 | 808 | |
7972aab2 DG |
809 | rcu_read_lock(); |
810 | ||
ce34fcd0 | 811 | pthread_mutex_lock(®istry->lock); |
a70ac2f4 MD |
812 | metadata_key = registry->metadata_key; |
813 | registry_was_already_closed = registry->metadata_closed; | |
814 | if (metadata_key != 0) { | |
815 | /* | |
816 | * Metadata closed. Even on error this means that the consumer | |
817 | * is not responding or not found so either way a second close | |
818 | * should NOT be emit for this registry. | |
819 | */ | |
820 | registry->metadata_closed = 1; | |
821 | } | |
822 | pthread_mutex_unlock(®istry->lock); | |
ce34fcd0 | 823 | |
a70ac2f4 | 824 | if (metadata_key == 0 || registry_was_already_closed) { |
d88aee68 | 825 | ret = 0; |
1b532a60 | 826 | goto end; |
d88aee68 DG |
827 | } |
828 | ||
d88aee68 | 829 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
830 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
831 | consumer); | |
d88aee68 DG |
832 | if (!socket) { |
833 | ret = -1; | |
a70ac2f4 | 834 | goto end; |
d88aee68 DG |
835 | } |
836 | ||
a70ac2f4 | 837 | ret = consumer_close_metadata(socket, metadata_key); |
d88aee68 | 838 | if (ret < 0) { |
a70ac2f4 | 839 | goto end; |
d88aee68 DG |
840 | } |
841 | ||
1b532a60 | 842 | end: |
7972aab2 | 843 | rcu_read_unlock(); |
d88aee68 DG |
844 | return ret; |
845 | } | |
846 | ||
36b588ed MD |
847 | /* |
848 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
849 | * section and outside of call_rcu thread, so we postpone its execution |
850 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
851 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
852 | */ |
853 | static | |
854 | void delete_ust_app_session_rcu(struct rcu_head *head) | |
855 | { | |
856 | struct ust_app_session *ua_sess = | |
857 | caa_container_of(head, struct ust_app_session, rcu_head); | |
858 | ||
0b2dc8df | 859 | ht_cleanup_push(ua_sess->channels); |
36b588ed MD |
860 | free(ua_sess); |
861 | } | |
862 | ||
d80a6244 DG |
863 | /* |
864 | * Delete ust app session safely. RCU read lock must be held before calling | |
865 | * this function. | |
82cac6d2 JG |
866 | * |
867 | * The session list lock must be held by the caller. | |
d80a6244 | 868 | */ |
8b366481 | 869 | static |
d0b96690 DG |
870 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
871 | struct ust_app *app) | |
d80a6244 DG |
872 | { |
873 | int ret; | |
bec39940 | 874 | struct lttng_ht_iter iter; |
d80a6244 | 875 | struct ust_app_channel *ua_chan; |
7972aab2 | 876 | struct ust_registry_session *registry; |
d80a6244 | 877 | |
d88aee68 DG |
878 | assert(ua_sess); |
879 | ||
1b532a60 DG |
880 | pthread_mutex_lock(&ua_sess->lock); |
881 | ||
b161602a MD |
882 | assert(!ua_sess->deleted); |
883 | ua_sess->deleted = true; | |
884 | ||
7972aab2 | 885 | registry = get_session_registry(ua_sess); |
fad1ed2f | 886 | /* Registry can be null on error path during initialization. */ |
ce34fcd0 | 887 | if (registry) { |
d88aee68 | 888 | /* Push metadata for application before freeing the application. */ |
7972aab2 | 889 | (void) push_metadata(registry, ua_sess->consumer); |
d88aee68 | 890 | |
7972aab2 DG |
891 | /* |
892 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
893 | * metadata only on destroy trace session in this case. Also, the |
894 | * previous push metadata could have flag the metadata registry to | |
895 | * close so don't send a close command if closed. | |
7972aab2 | 896 | */ |
ce34fcd0 | 897 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
898 | /* And ask to close it for this session registry. */ |
899 | (void) close_metadata(registry, ua_sess->consumer); | |
900 | } | |
d80a6244 DG |
901 | } |
902 | ||
bec39940 DG |
903 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
904 | node.node) { | |
905 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
525b0740 | 906 | assert(!ret); |
d0b96690 | 907 | delete_ust_app_channel(sock, ua_chan, app); |
d80a6244 | 908 | } |
d80a6244 | 909 | |
7972aab2 DG |
910 | /* In case of per PID, the registry is kept in the session. */ |
911 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
912 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
913 | if (reg_pid) { | |
fad1ed2f JR |
914 | /* |
915 | * Registry can be null on error path during | |
916 | * initialization. | |
917 | */ | |
7972aab2 DG |
918 | buffer_reg_pid_remove(reg_pid); |
919 | buffer_reg_pid_destroy(reg_pid); | |
920 | } | |
921 | } | |
d0b96690 | 922 | |
aee6bafd | 923 | if (ua_sess->handle != -1) { |
fb45065e | 924 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 925 | ret = ustctl_release_handle(sock, ua_sess->handle); |
fb45065e | 926 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
927 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
928 | ERR("UST app sock %d release session handle failed with ret %d", | |
929 | sock, ret); | |
930 | } | |
10b56aef MD |
931 | /* Remove session from application UST object descriptor. */ |
932 | iter.iter.node = &ua_sess->ust_objd_node.node; | |
933 | ret = lttng_ht_del(app->ust_sessions_objd, &iter); | |
934 | assert(!ret); | |
aee6bafd | 935 | } |
10b56aef | 936 | |
1b532a60 DG |
937 | pthread_mutex_unlock(&ua_sess->lock); |
938 | ||
6addfa37 MD |
939 | consumer_output_put(ua_sess->consumer); |
940 | ||
36b588ed | 941 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
d80a6244 | 942 | } |
91d76f53 DG |
943 | |
944 | /* | |
284d8f55 DG |
945 | * Delete a traceable application structure from the global list. Never call |
946 | * this function outside of a call_rcu call. | |
36b588ed MD |
947 | * |
948 | * RCU read side lock should _NOT_ be held when calling this function. | |
91d76f53 | 949 | */ |
8b366481 DG |
950 | static |
951 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 952 | { |
8b366481 | 953 | int ret, sock; |
d42f20df | 954 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
993578ff JR |
955 | struct lttng_ht_iter iter; |
956 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5e2abfaf | 957 | bool event_notifier_write_fd_is_open; |
44d3bd01 | 958 | |
82cac6d2 JG |
959 | /* |
960 | * The session list lock must be held during this function to guarantee | |
961 | * the existence of ua_sess. | |
962 | */ | |
963 | session_lock_list(); | |
d80a6244 | 964 | /* Delete ust app sessions info */ |
852d0037 DG |
965 | sock = app->sock; |
966 | app->sock = -1; | |
d80a6244 | 967 | |
8b366481 | 968 | /* Wipe sessions */ |
d42f20df DG |
969 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
970 | teardown_node) { | |
971 | /* Free every object in the session and the session. */ | |
36b588ed | 972 | rcu_read_lock(); |
d0b96690 | 973 | delete_ust_app_session(sock, ua_sess, app); |
36b588ed | 974 | rcu_read_unlock(); |
d80a6244 | 975 | } |
36b588ed | 976 | |
993578ff JR |
977 | /* Remove the event notifier rules associated with this app. */ |
978 | rcu_read_lock(); | |
979 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
980 | &iter.iter, event_notifier_rule, node.node) { | |
981 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter); | |
982 | assert(!ret); | |
983 | ||
984 | delete_ust_app_event_notifier_rule( | |
985 | app->sock, event_notifier_rule, app); | |
986 | } | |
987 | ||
988 | rcu_read_unlock(); | |
989 | ||
0b2dc8df | 990 | ht_cleanup_push(app->sessions); |
10b56aef | 991 | ht_cleanup_push(app->ust_sessions_objd); |
0b2dc8df | 992 | ht_cleanup_push(app->ust_objd); |
993578ff | 993 | ht_cleanup_push(app->token_to_event_notifier_rule_ht); |
d80a6244 | 994 | |
da873412 JR |
995 | /* |
996 | * This could be NULL if the event notifier setup failed (e.g the app | |
997 | * was killed or the tracer does not support this feature). | |
998 | */ | |
999 | if (app->event_notifier_group.object) { | |
1000 | enum lttng_error_code ret_code; | |
1001 | const int event_notifier_read_fd = lttng_pipe_get_readfd( | |
1002 | app->event_notifier_group.event_pipe); | |
1003 | ||
1004 | ret_code = notification_thread_command_remove_tracer_event_source( | |
1005 | notification_thread_handle, | |
1006 | event_notifier_read_fd); | |
1007 | if (ret_code != LTTNG_OK) { | |
1008 | ERR("Failed to remove application tracer event source from notification thread"); | |
1009 | } | |
1010 | ||
1011 | ustctl_release_object(sock, app->event_notifier_group.object); | |
1012 | free(app->event_notifier_group.object); | |
1013 | } | |
1014 | ||
5e2abfaf JG |
1015 | event_notifier_write_fd_is_open = lttng_pipe_is_write_open( |
1016 | app->event_notifier_group.event_pipe); | |
da873412 | 1017 | lttng_pipe_destroy(app->event_notifier_group.event_pipe); |
5e2abfaf JG |
1018 | /* |
1019 | * Release the file descriptors reserved for the event notifier pipe. | |
1020 | * The app could be destroyed before the write end of the pipe could be | |
1021 | * passed to the application (and closed). In that case, both file | |
1022 | * descriptors must be released. | |
1023 | */ | |
1024 | lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1); | |
da873412 | 1025 | |
6414a713 | 1026 | /* |
852d0037 DG |
1027 | * Wait until we have deleted the application from the sock hash table |
1028 | * before closing this socket, otherwise an application could re-use the | |
1029 | * socket ID and race with the teardown, using the same hash table entry. | |
1030 | * | |
1031 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
1032 | * all RCU readers that could run concurrently with unregister app, | |
1033 | * therefore we _need_ to only close that socket after a grace period. So | |
1034 | * it should stay in this RCU callback. | |
1035 | * | |
1036 | * This close() is a very important step of the synchronization model so | |
1037 | * every modification to this function must be carefully reviewed. | |
6414a713 | 1038 | */ |
799e2c4f MD |
1039 | ret = close(sock); |
1040 | if (ret) { | |
1041 | PERROR("close"); | |
1042 | } | |
4063050c | 1043 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 1044 | |
852d0037 | 1045 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 1046 | free(app); |
82cac6d2 | 1047 | session_unlock_list(); |
099e26bd DG |
1048 | } |
1049 | ||
1050 | /* | |
f6a9efaa | 1051 | * URCU intermediate call to delete an UST app. |
099e26bd | 1052 | */ |
8b366481 DG |
1053 | static |
1054 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 1055 | { |
bec39940 DG |
1056 | struct lttng_ht_node_ulong *node = |
1057 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa | 1058 | struct ust_app *app = |
852d0037 | 1059 | caa_container_of(node, struct ust_app, pid_n); |
f6a9efaa | 1060 | |
852d0037 | 1061 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 1062 | delete_ust_app(app); |
099e26bd DG |
1063 | } |
1064 | ||
ffe60014 DG |
1065 | /* |
1066 | * Delete the session from the application ht and delete the data structure by | |
1067 | * freeing every object inside and releasing them. | |
82cac6d2 JG |
1068 | * |
1069 | * The session list lock must be held by the caller. | |
ffe60014 | 1070 | */ |
d0b96690 | 1071 | static void destroy_app_session(struct ust_app *app, |
ffe60014 DG |
1072 | struct ust_app_session *ua_sess) |
1073 | { | |
1074 | int ret; | |
1075 | struct lttng_ht_iter iter; | |
1076 | ||
1077 | assert(app); | |
1078 | assert(ua_sess); | |
1079 | ||
1080 | iter.iter.node = &ua_sess->node.node; | |
1081 | ret = lttng_ht_del(app->sessions, &iter); | |
1082 | if (ret) { | |
1083 | /* Already scheduled for teardown. */ | |
1084 | goto end; | |
1085 | } | |
1086 | ||
1087 | /* Once deleted, free the data structure. */ | |
d0b96690 | 1088 | delete_ust_app_session(app->sock, ua_sess, app); |
ffe60014 DG |
1089 | |
1090 | end: | |
1091 | return; | |
1092 | } | |
1093 | ||
8b366481 DG |
1094 | /* |
1095 | * Alloc new UST app session. | |
1096 | */ | |
1097 | static | |
40bbd087 | 1098 | struct ust_app_session *alloc_ust_app_session(void) |
8b366481 DG |
1099 | { |
1100 | struct ust_app_session *ua_sess; | |
1101 | ||
1102 | /* Init most of the default value by allocating and zeroing */ | |
1103 | ua_sess = zmalloc(sizeof(struct ust_app_session)); | |
1104 | if (ua_sess == NULL) { | |
1105 | PERROR("malloc"); | |
ffe60014 | 1106 | goto error_free; |
8b366481 DG |
1107 | } |
1108 | ||
1109 | ua_sess->handle = -1; | |
bec39940 | 1110 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
ad7a9107 | 1111 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
84ad93e8 | 1112 | pthread_mutex_init(&ua_sess->lock, NULL); |
ad7a9107 | 1113 | |
8b366481 DG |
1114 | return ua_sess; |
1115 | ||
ffe60014 | 1116 | error_free: |
8b366481 DG |
1117 | return NULL; |
1118 | } | |
1119 | ||
1120 | /* | |
1121 | * Alloc new UST app channel. | |
1122 | */ | |
1123 | static | |
b53d4e59 | 1124 | struct ust_app_channel *alloc_ust_app_channel(const char *name, |
d0b96690 | 1125 | struct ust_app_session *ua_sess, |
ffe60014 | 1126 | struct lttng_ust_channel_attr *attr) |
8b366481 DG |
1127 | { |
1128 | struct ust_app_channel *ua_chan; | |
1129 | ||
1130 | /* Init most of the default value by allocating and zeroing */ | |
1131 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); | |
1132 | if (ua_chan == NULL) { | |
1133 | PERROR("malloc"); | |
1134 | goto error; | |
1135 | } | |
1136 | ||
1137 | /* Setup channel name */ | |
1138 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
1139 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
1140 | ||
1141 | ua_chan->enabled = 1; | |
1142 | ua_chan->handle = -1; | |
45893984 | 1143 | ua_chan->session = ua_sess; |
ffe60014 | 1144 | ua_chan->key = get_next_channel_key(); |
bec39940 DG |
1145 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
1146 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
1147 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
1148 | |
1149 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
31746f93 | 1150 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
8b366481 DG |
1151 | |
1152 | /* Copy attributes */ | |
1153 | if (attr) { | |
ffe60014 | 1154 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
2fe6e7f5 DG |
1155 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
1156 | ua_chan->attr.num_subbuf = attr->num_subbuf; | |
1157 | ua_chan->attr.overwrite = attr->overwrite; | |
1158 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; | |
1159 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; | |
1160 | ua_chan->attr.output = attr->output; | |
491d1539 | 1161 | ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout; |
8b366481 | 1162 | } |
ffe60014 DG |
1163 | /* By default, the channel is a per cpu channel. */ |
1164 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8b366481 DG |
1165 | |
1166 | DBG3("UST app channel %s allocated", ua_chan->name); | |
1167 | ||
1168 | return ua_chan; | |
1169 | ||
1170 | error: | |
1171 | return NULL; | |
1172 | } | |
1173 | ||
37f1c236 DG |
1174 | /* |
1175 | * Allocate and initialize a UST app stream. | |
1176 | * | |
1177 | * Return newly allocated stream pointer or NULL on error. | |
1178 | */ | |
ffe60014 | 1179 | struct ust_app_stream *ust_app_alloc_stream(void) |
37f1c236 DG |
1180 | { |
1181 | struct ust_app_stream *stream = NULL; | |
1182 | ||
1183 | stream = zmalloc(sizeof(*stream)); | |
1184 | if (stream == NULL) { | |
1185 | PERROR("zmalloc ust app stream"); | |
1186 | goto error; | |
1187 | } | |
1188 | ||
1189 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
1190 | stream->handle = -1; | |
1191 | ||
1192 | error: | |
1193 | return stream; | |
1194 | } | |
1195 | ||
8b366481 DG |
1196 | /* |
1197 | * Alloc new UST app event. | |
1198 | */ | |
1199 | static | |
1200 | struct ust_app_event *alloc_ust_app_event(char *name, | |
1201 | struct lttng_ust_event *attr) | |
1202 | { | |
1203 | struct ust_app_event *ua_event; | |
1204 | ||
1205 | /* Init most of the default value by allocating and zeroing */ | |
1206 | ua_event = zmalloc(sizeof(struct ust_app_event)); | |
1207 | if (ua_event == NULL) { | |
20533947 | 1208 | PERROR("Failed to allocate ust_app_event structure"); |
8b366481 DG |
1209 | goto error; |
1210 | } | |
1211 | ||
1212 | ua_event->enabled = 1; | |
1213 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
1214 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 | 1215 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
8b366481 DG |
1216 | |
1217 | /* Copy attributes */ | |
1218 | if (attr) { | |
1219 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
1220 | } | |
1221 | ||
1222 | DBG3("UST app event %s allocated", ua_event->name); | |
1223 | ||
1224 | return ua_event; | |
1225 | ||
1226 | error: | |
1227 | return NULL; | |
1228 | } | |
1229 | ||
993578ff JR |
1230 | /* |
1231 | * Allocate a new UST app event notifier rule. | |
1232 | */ | |
1233 | static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule( | |
267d66aa | 1234 | struct lttng_trigger *trigger) |
993578ff JR |
1235 | { |
1236 | enum lttng_event_rule_generate_exclusions_status | |
1237 | generate_exclusion_status; | |
1238 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
267d66aa JR |
1239 | struct lttng_condition *condition = NULL; |
1240 | const struct lttng_event_rule *event_rule = NULL; | |
993578ff JR |
1241 | |
1242 | ua_event_notifier_rule = zmalloc(sizeof(struct ust_app_event_notifier_rule)); | |
1243 | if (ua_event_notifier_rule == NULL) { | |
1244 | PERROR("Failed to allocate ust_app_event_notifier_rule structure"); | |
1245 | goto error; | |
1246 | } | |
1247 | ||
1248 | ua_event_notifier_rule->enabled = 1; | |
267d66aa JR |
1249 | ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger); |
1250 | lttng_ht_node_init_u64(&ua_event_notifier_rule->node, | |
1251 | ua_event_notifier_rule->token); | |
993578ff | 1252 | |
267d66aa JR |
1253 | condition = lttng_trigger_get_condition(trigger); |
1254 | assert(condition); | |
1255 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT); | |
1256 | ||
1257 | assert(LTTNG_CONDITION_STATUS_OK == lttng_condition_event_rule_get_rule(condition, &event_rule)); | |
1258 | assert(event_rule); | |
993578ff | 1259 | |
267d66aa JR |
1260 | /* Acquire the event notifier's reference to the trigger. */ |
1261 | lttng_trigger_get(trigger); | |
1262 | ||
1263 | ua_event_notifier_rule->trigger = trigger; | |
993578ff JR |
1264 | ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
1265 | generate_exclusion_status = lttng_event_rule_generate_exclusions( | |
1266 | event_rule, &ua_event_notifier_rule->exclusion); | |
1267 | switch (generate_exclusion_status) { | |
1268 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK: | |
1269 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE: | |
1270 | break; | |
1271 | default: | |
1272 | /* Error occured. */ | |
267d66aa JR |
1273 | ERR("Failed to generate exclusions from trigger while allocating an event notifier rule"); |
1274 | goto error_put_trigger; | |
993578ff JR |
1275 | } |
1276 | ||
1277 | DBG3("UST app event notifier rule allocated: token = %" PRIu64, | |
1278 | ua_event_notifier_rule->token); | |
1279 | ||
1280 | return ua_event_notifier_rule; | |
1281 | ||
267d66aa JR |
1282 | error_put_trigger: |
1283 | lttng_trigger_put(trigger); | |
993578ff JR |
1284 | error: |
1285 | free(ua_event_notifier_rule); | |
1286 | return NULL; | |
1287 | } | |
1288 | ||
8b366481 DG |
1289 | /* |
1290 | * Alloc new UST app context. | |
1291 | */ | |
1292 | static | |
bdf64013 | 1293 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) |
8b366481 DG |
1294 | { |
1295 | struct ust_app_ctx *ua_ctx; | |
1296 | ||
1297 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); | |
1298 | if (ua_ctx == NULL) { | |
1299 | goto error; | |
1300 | } | |
1301 | ||
31746f93 DG |
1302 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
1303 | ||
8b366481 DG |
1304 | if (uctx) { |
1305 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
bdf64013 | 1306 | if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { |
f3db82be | 1307 | char *provider_name = NULL, *ctx_name = NULL; |
bdf64013 JG |
1308 | |
1309 | provider_name = strdup(uctx->u.app_ctx.provider_name); | |
1310 | ctx_name = strdup(uctx->u.app_ctx.ctx_name); | |
1311 | if (!provider_name || !ctx_name) { | |
1312 | free(provider_name); | |
1313 | free(ctx_name); | |
1314 | goto error; | |
1315 | } | |
1316 | ||
1317 | ua_ctx->ctx.u.app_ctx.provider_name = provider_name; | |
1318 | ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name; | |
1319 | } | |
8b366481 DG |
1320 | } |
1321 | ||
1322 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
8b366481 | 1323 | return ua_ctx; |
bdf64013 JG |
1324 | error: |
1325 | free(ua_ctx); | |
1326 | return NULL; | |
8b366481 DG |
1327 | } |
1328 | ||
51755dc8 JG |
1329 | /* |
1330 | * Create a liblttng-ust filter bytecode from given bytecode. | |
1331 | * | |
1332 | * Return allocated filter or NULL on error. | |
1333 | */ | |
1334 | static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode( | |
2b00d462 | 1335 | const struct lttng_bytecode *orig_f) |
51755dc8 JG |
1336 | { |
1337 | struct lttng_ust_filter_bytecode *filter = NULL; | |
1338 | ||
1339 | /* Copy filter bytecode */ | |
1340 | filter = zmalloc(sizeof(*filter) + orig_f->len); | |
1341 | if (!filter) { | |
1342 | PERROR("zmalloc alloc ust filter bytecode"); | |
1343 | goto error; | |
1344 | } | |
1345 | ||
2b00d462 | 1346 | assert(sizeof(struct lttng_bytecode) == |
51755dc8 JG |
1347 | sizeof(struct lttng_ust_filter_bytecode)); |
1348 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1349 | error: | |
1350 | return filter; | |
1351 | } | |
1352 | ||
099e26bd | 1353 | /* |
421cb601 DG |
1354 | * Find an ust_app using the sock and return it. RCU read side lock must be |
1355 | * held before calling this helper function. | |
099e26bd | 1356 | */ |
f20baf8e | 1357 | struct ust_app *ust_app_find_by_sock(int sock) |
099e26bd | 1358 | { |
bec39940 | 1359 | struct lttng_ht_node_ulong *node; |
bec39940 | 1360 | struct lttng_ht_iter iter; |
f6a9efaa | 1361 | |
852d0037 | 1362 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1363 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
1364 | if (node == NULL) { |
1365 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
1366 | goto error; |
1367 | } | |
852d0037 DG |
1368 | |
1369 | return caa_container_of(node, struct ust_app, sock_n); | |
f6a9efaa DG |
1370 | |
1371 | error: | |
1372 | return NULL; | |
099e26bd DG |
1373 | } |
1374 | ||
d0b96690 DG |
1375 | /* |
1376 | * Find an ust_app using the notify sock and return it. RCU read side lock must | |
1377 | * be held before calling this helper function. | |
1378 | */ | |
1379 | static struct ust_app *find_app_by_notify_sock(int sock) | |
1380 | { | |
1381 | struct lttng_ht_node_ulong *node; | |
1382 | struct lttng_ht_iter iter; | |
1383 | ||
1384 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), | |
1385 | &iter); | |
1386 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1387 | if (node == NULL) { | |
1388 | DBG2("UST app find by notify sock %d not found", sock); | |
1389 | goto error; | |
1390 | } | |
1391 | ||
1392 | return caa_container_of(node, struct ust_app, notify_sock_n); | |
1393 | ||
1394 | error: | |
1395 | return NULL; | |
1396 | } | |
1397 | ||
025faf73 DG |
1398 | /* |
1399 | * Lookup for an ust app event based on event name, filter bytecode and the | |
1400 | * event loglevel. | |
1401 | * | |
1402 | * Return an ust_app_event object or NULL on error. | |
1403 | */ | |
18eace3b | 1404 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
2b00d462 | 1405 | const char *name, const struct lttng_bytecode *filter, |
2106efa0 | 1406 | int loglevel_value, |
39c5a3a7 | 1407 | const struct lttng_event_exclusion *exclusion) |
18eace3b DG |
1408 | { |
1409 | struct lttng_ht_iter iter; | |
1410 | struct lttng_ht_node_str *node; | |
1411 | struct ust_app_event *event = NULL; | |
1412 | struct ust_app_ht_key key; | |
18eace3b DG |
1413 | |
1414 | assert(name); | |
1415 | assert(ht); | |
1416 | ||
1417 | /* Setup key for event lookup. */ | |
1418 | key.name = name; | |
1419 | key.filter = filter; | |
2106efa0 | 1420 | key.loglevel_type = loglevel_value; |
39c5a3a7 | 1421 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
51755dc8 | 1422 | key.exclusion = exclusion; |
18eace3b | 1423 | |
025faf73 DG |
1424 | /* Lookup using the event name as hash and a custom match fct. */ |
1425 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), | |
1426 | ht_match_ust_app_event, &key, &iter.iter); | |
18eace3b DG |
1427 | node = lttng_ht_iter_get_node_str(&iter); |
1428 | if (node == NULL) { | |
1429 | goto end; | |
1430 | } | |
1431 | ||
1432 | event = caa_container_of(node, struct ust_app_event, node); | |
1433 | ||
1434 | end: | |
18eace3b DG |
1435 | return event; |
1436 | } | |
1437 | ||
993578ff JR |
1438 | /* |
1439 | * Look-up an event notifier rule based on its token id. | |
1440 | * | |
1441 | * Must be called with the RCU read lock held. | |
1442 | * Return an ust_app_event_notifier_rule object or NULL on error. | |
1443 | */ | |
1444 | static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule( | |
1445 | struct lttng_ht *ht, uint64_t token) | |
1446 | { | |
1447 | struct lttng_ht_iter iter; | |
1448 | struct lttng_ht_node_u64 *node; | |
1449 | struct ust_app_event_notifier_rule *event_notifier_rule = NULL; | |
1450 | ||
1451 | assert(ht); | |
1452 | ||
1453 | lttng_ht_lookup(ht, &token, &iter); | |
1454 | node = lttng_ht_iter_get_node_u64(&iter); | |
1455 | if (node == NULL) { | |
1456 | DBG2("UST app event notifier rule token not found: token = %" PRIu64, | |
1457 | token); | |
1458 | goto end; | |
1459 | } | |
1460 | ||
1461 | event_notifier_rule = caa_container_of( | |
1462 | node, struct ust_app_event_notifier_rule, node); | |
1463 | end: | |
1464 | return event_notifier_rule; | |
1465 | } | |
1466 | ||
55cc08a6 DG |
1467 | /* |
1468 | * Create the channel context on the tracer. | |
d0b96690 DG |
1469 | * |
1470 | * Called with UST app session lock held. | |
55cc08a6 DG |
1471 | */ |
1472 | static | |
1473 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
1474 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
1475 | { | |
1476 | int ret; | |
1477 | ||
840cb59c | 1478 | health_code_update(); |
86acf0da | 1479 | |
fb45065e | 1480 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1481 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 | 1482 | ua_chan->obj, &ua_ctx->obj); |
fb45065e | 1483 | pthread_mutex_unlock(&app->sock_lock); |
55cc08a6 | 1484 | if (ret < 0) { |
ffe60014 DG |
1485 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1486 | ERR("UST app create channel context failed for app (pid: %d) " | |
1487 | "with ret %d", app->pid, ret); | |
1488 | } else { | |
3757b385 DG |
1489 | /* |
1490 | * This is normal behavior, an application can die during the | |
1491 | * creation process. Don't report an error so the execution can | |
1492 | * continue normally. | |
1493 | */ | |
1494 | ret = 0; | |
88e3c2f5 | 1495 | DBG3("UST app add context failed. Application is dead."); |
ffe60014 | 1496 | } |
55cc08a6 DG |
1497 | goto error; |
1498 | } | |
1499 | ||
1500 | ua_ctx->handle = ua_ctx->obj->handle; | |
1501 | ||
d0b96690 DG |
1502 | DBG2("UST app context handle %d created successfully for channel %s", |
1503 | ua_ctx->handle, ua_chan->name); | |
55cc08a6 DG |
1504 | |
1505 | error: | |
840cb59c | 1506 | health_code_update(); |
55cc08a6 DG |
1507 | return ret; |
1508 | } | |
1509 | ||
53a80697 MD |
1510 | /* |
1511 | * Set the filter on the tracer. | |
1512 | */ | |
a154c7b8 | 1513 | static int set_ust_object_filter(struct ust_app *app, |
2b00d462 | 1514 | const struct lttng_bytecode *bytecode, |
a154c7b8 | 1515 | struct lttng_ust_object_data *ust_object) |
53a80697 MD |
1516 | { |
1517 | int ret; | |
51755dc8 | 1518 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
53a80697 | 1519 | |
840cb59c | 1520 | health_code_update(); |
86acf0da | 1521 | |
a154c7b8 | 1522 | ust_bytecode = create_ust_bytecode_from_bytecode(bytecode); |
51755dc8 JG |
1523 | if (!ust_bytecode) { |
1524 | ret = -LTTNG_ERR_NOMEM; | |
1525 | goto error; | |
1526 | } | |
fb45065e | 1527 | pthread_mutex_lock(&app->sock_lock); |
51755dc8 | 1528 | ret = ustctl_set_filter(app->sock, ust_bytecode, |
a154c7b8 | 1529 | ust_object); |
fb45065e | 1530 | pthread_mutex_unlock(&app->sock_lock); |
53a80697 | 1531 | if (ret < 0) { |
ffe60014 | 1532 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
a154c7b8 JR |
1533 | ERR("UST app set object filter failed for object %p of app (pid: %d) " |
1534 | "with ret %d", ust_object, app->pid, ret); | |
ffe60014 | 1535 | } else { |
3757b385 DG |
1536 | /* |
1537 | * This is normal behavior, an application can die during the | |
1538 | * creation process. Don't report an error so the execution can | |
1539 | * continue normally. | |
1540 | */ | |
1541 | ret = 0; | |
a154c7b8 | 1542 | DBG3("Failed to set UST app object filter. Application is dead."); |
ffe60014 | 1543 | } |
53a80697 MD |
1544 | goto error; |
1545 | } | |
1546 | ||
a154c7b8 | 1547 | DBG2("UST filter successfully set for object %p", ust_object); |
53a80697 MD |
1548 | |
1549 | error: | |
840cb59c | 1550 | health_code_update(); |
51755dc8 | 1551 | free(ust_bytecode); |
53a80697 MD |
1552 | return ret; |
1553 | } | |
1554 | ||
51755dc8 JG |
1555 | static |
1556 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( | |
c0901ffa | 1557 | const struct lttng_event_exclusion *exclusion) |
51755dc8 JG |
1558 | { |
1559 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; | |
1560 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + | |
1561 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; | |
1562 | ||
1563 | ust_exclusion = zmalloc(exclusion_alloc_size); | |
1564 | if (!ust_exclusion) { | |
1565 | PERROR("malloc"); | |
1566 | goto end; | |
1567 | } | |
1568 | ||
1569 | assert(sizeof(struct lttng_event_exclusion) == | |
1570 | sizeof(struct lttng_ust_event_exclusion)); | |
1571 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); | |
1572 | end: | |
1573 | return ust_exclusion; | |
1574 | } | |
1575 | ||
7cc9a73c JI |
1576 | /* |
1577 | * Set event exclusions on the tracer. | |
1578 | */ | |
c0901ffa JR |
1579 | static int set_ust_object_exclusions(struct ust_app *app, |
1580 | const struct lttng_event_exclusion *exclusions, | |
1581 | struct lttng_ust_object_data *ust_object) | |
7cc9a73c JI |
1582 | { |
1583 | int ret; | |
c0901ffa | 1584 | struct lttng_ust_event_exclusion *ust_exclusions = NULL; |
7cc9a73c | 1585 | |
c0901ffa | 1586 | assert(exclusions && exclusions->count > 0); |
7cc9a73c | 1587 | |
c0901ffa | 1588 | health_code_update(); |
7cc9a73c | 1589 | |
c0901ffa JR |
1590 | ust_exclusions = create_ust_exclusion_from_exclusion( |
1591 | exclusions); | |
1592 | if (!ust_exclusions) { | |
51755dc8 JG |
1593 | ret = -LTTNG_ERR_NOMEM; |
1594 | goto error; | |
1595 | } | |
fb45065e | 1596 | pthread_mutex_lock(&app->sock_lock); |
c0901ffa | 1597 | ret = ustctl_set_exclusion(app->sock, ust_exclusions, ust_object); |
fb45065e | 1598 | pthread_mutex_unlock(&app->sock_lock); |
7cc9a73c JI |
1599 | if (ret < 0) { |
1600 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
c0901ffa JR |
1601 | ERR("Failed to set UST app exclusions for object %p of app (pid: %d) " |
1602 | "with ret %d", ust_object, app->pid, ret); | |
7cc9a73c JI |
1603 | } else { |
1604 | /* | |
1605 | * This is normal behavior, an application can die during the | |
1606 | * creation process. Don't report an error so the execution can | |
1607 | * continue normally. | |
1608 | */ | |
1609 | ret = 0; | |
c0901ffa | 1610 | DBG3("Failed to set UST app object exclusions. Application is dead."); |
7cc9a73c JI |
1611 | } |
1612 | goto error; | |
1613 | } | |
1614 | ||
c0901ffa | 1615 | DBG2("UST exclusions set successfully for object %p", ust_object); |
7cc9a73c JI |
1616 | |
1617 | error: | |
1618 | health_code_update(); | |
c0901ffa | 1619 | free(ust_exclusions); |
7cc9a73c JI |
1620 | return ret; |
1621 | } | |
1622 | ||
9730260e DG |
1623 | /* |
1624 | * Disable the specified event on to UST tracer for the UST session. | |
1625 | */ | |
e2456d0a JR |
1626 | static int disable_ust_object(struct ust_app *app, |
1627 | struct lttng_ust_object_data *object) | |
9730260e DG |
1628 | { |
1629 | int ret; | |
1630 | ||
840cb59c | 1631 | health_code_update(); |
86acf0da | 1632 | |
fb45065e | 1633 | pthread_mutex_lock(&app->sock_lock); |
e2456d0a | 1634 | ret = ustctl_disable(app->sock, object); |
fb45065e | 1635 | pthread_mutex_unlock(&app->sock_lock); |
9730260e | 1636 | if (ret < 0) { |
ffe60014 | 1637 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
e2456d0a JR |
1638 | ERR("Failed to disable UST app object %p app (pid: %d) with ret %d", |
1639 | object, app->pid, ret); | |
ffe60014 | 1640 | } else { |
3757b385 DG |
1641 | /* |
1642 | * This is normal behavior, an application can die during the | |
1643 | * creation process. Don't report an error so the execution can | |
1644 | * continue normally. | |
1645 | */ | |
1646 | ret = 0; | |
e2456d0a | 1647 | DBG3("Failed to disable UST app object. Application is dead."); |
ffe60014 | 1648 | } |
9730260e DG |
1649 | goto error; |
1650 | } | |
1651 | ||
e2456d0a JR |
1652 | DBG2("UST app object %p disabled successfully for app (pid: %d)", |
1653 | object, app->pid); | |
9730260e DG |
1654 | |
1655 | error: | |
840cb59c | 1656 | health_code_update(); |
9730260e DG |
1657 | return ret; |
1658 | } | |
1659 | ||
78f0bacd DG |
1660 | /* |
1661 | * Disable the specified channel on to UST tracer for the UST session. | |
1662 | */ | |
1663 | static int disable_ust_channel(struct ust_app *app, | |
1664 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1665 | { | |
1666 | int ret; | |
1667 | ||
840cb59c | 1668 | health_code_update(); |
86acf0da | 1669 | |
fb45065e | 1670 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1671 | ret = ustctl_disable(app->sock, ua_chan->obj); |
fb45065e | 1672 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1673 | if (ret < 0) { |
ffe60014 DG |
1674 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1675 | ERR("UST app channel %s disable failed for app (pid: %d) " | |
1676 | "and session handle %d with ret %d", | |
1677 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1678 | } else { | |
3757b385 DG |
1679 | /* |
1680 | * This is normal behavior, an application can die during the | |
1681 | * creation process. Don't report an error so the execution can | |
1682 | * continue normally. | |
1683 | */ | |
1684 | ret = 0; | |
ffe60014 DG |
1685 | DBG3("UST app disable channel failed. Application is dead."); |
1686 | } | |
78f0bacd DG |
1687 | goto error; |
1688 | } | |
1689 | ||
78f0bacd | 1690 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
852d0037 | 1691 | ua_chan->name, app->pid); |
78f0bacd DG |
1692 | |
1693 | error: | |
840cb59c | 1694 | health_code_update(); |
78f0bacd DG |
1695 | return ret; |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * Enable the specified channel on to UST tracer for the UST session. | |
1700 | */ | |
1701 | static int enable_ust_channel(struct ust_app *app, | |
1702 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1703 | { | |
1704 | int ret; | |
1705 | ||
840cb59c | 1706 | health_code_update(); |
86acf0da | 1707 | |
fb45065e | 1708 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1709 | ret = ustctl_enable(app->sock, ua_chan->obj); |
fb45065e | 1710 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1711 | if (ret < 0) { |
ffe60014 DG |
1712 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1713 | ERR("UST app channel %s enable failed for app (pid: %d) " | |
1714 | "and session handle %d with ret %d", | |
1715 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1716 | } else { | |
3757b385 DG |
1717 | /* |
1718 | * This is normal behavior, an application can die during the | |
1719 | * creation process. Don't report an error so the execution can | |
1720 | * continue normally. | |
1721 | */ | |
1722 | ret = 0; | |
ffe60014 DG |
1723 | DBG3("UST app enable channel failed. Application is dead."); |
1724 | } | |
78f0bacd DG |
1725 | goto error; |
1726 | } | |
1727 | ||
1728 | ua_chan->enabled = 1; | |
1729 | ||
1730 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", | |
852d0037 | 1731 | ua_chan->name, app->pid); |
78f0bacd DG |
1732 | |
1733 | error: | |
840cb59c | 1734 | health_code_update(); |
78f0bacd DG |
1735 | return ret; |
1736 | } | |
1737 | ||
edb67388 DG |
1738 | /* |
1739 | * Enable the specified event on to UST tracer for the UST session. | |
1740 | */ | |
3428a1b7 JR |
1741 | static int enable_ust_object( |
1742 | struct ust_app *app, struct lttng_ust_object_data *ust_object) | |
edb67388 DG |
1743 | { |
1744 | int ret; | |
1745 | ||
840cb59c | 1746 | health_code_update(); |
86acf0da | 1747 | |
fb45065e | 1748 | pthread_mutex_lock(&app->sock_lock); |
3428a1b7 | 1749 | ret = ustctl_enable(app->sock, ust_object); |
fb45065e | 1750 | pthread_mutex_unlock(&app->sock_lock); |
edb67388 | 1751 | if (ret < 0) { |
ffe60014 | 1752 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
3428a1b7 JR |
1753 | ERR("UST app enable failed for object %p app (pid: %d) with ret %d", |
1754 | ust_object, app->pid, ret); | |
ffe60014 | 1755 | } else { |
3757b385 DG |
1756 | /* |
1757 | * This is normal behavior, an application can die during the | |
1758 | * creation process. Don't report an error so the execution can | |
1759 | * continue normally. | |
1760 | */ | |
1761 | ret = 0; | |
3428a1b7 | 1762 | DBG3("Failed to enable UST app object. Application is dead."); |
ffe60014 | 1763 | } |
edb67388 DG |
1764 | goto error; |
1765 | } | |
1766 | ||
3428a1b7 JR |
1767 | DBG2("UST app object %p enabled successfully for app (pid: %d)", |
1768 | ust_object, app->pid); | |
edb67388 DG |
1769 | |
1770 | error: | |
840cb59c | 1771 | health_code_update(); |
edb67388 DG |
1772 | return ret; |
1773 | } | |
1774 | ||
099e26bd | 1775 | /* |
7972aab2 | 1776 | * Send channel and stream buffer to application. |
4f3ab6ee | 1777 | * |
ffe60014 | 1778 | * Return 0 on success. On error, a negative value is returned. |
4f3ab6ee | 1779 | */ |
7972aab2 DG |
1780 | static int send_channel_pid_to_ust(struct ust_app *app, |
1781 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
4f3ab6ee DG |
1782 | { |
1783 | int ret; | |
ffe60014 | 1784 | struct ust_app_stream *stream, *stmp; |
4f3ab6ee DG |
1785 | |
1786 | assert(app); | |
ffe60014 | 1787 | assert(ua_sess); |
4f3ab6ee | 1788 | assert(ua_chan); |
4f3ab6ee | 1789 | |
840cb59c | 1790 | health_code_update(); |
4f3ab6ee | 1791 | |
7972aab2 DG |
1792 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
1793 | app->sock); | |
86acf0da | 1794 | |
ffe60014 DG |
1795 | /* Send channel to the application. */ |
1796 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
1797 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1798 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1799 | goto error; | |
1800 | } else if (ret < 0) { | |
b551a063 DG |
1801 | goto error; |
1802 | } | |
1803 | ||
d88aee68 DG |
1804 | health_code_update(); |
1805 | ||
ffe60014 DG |
1806 | /* Send all streams to application. */ |
1807 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
1808 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); | |
a7169585 MD |
1809 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1810 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1811 | goto error; | |
1812 | } else if (ret < 0) { | |
ffe60014 DG |
1813 | goto error; |
1814 | } | |
1815 | /* We don't need the stream anymore once sent to the tracer. */ | |
1816 | cds_list_del(&stream->list); | |
fb45065e | 1817 | delete_ust_app_stream(-1, stream, app); |
ffe60014 | 1818 | } |
ffe60014 DG |
1819 | /* Flag the channel that it is sent to the application. */ |
1820 | ua_chan->is_sent = 1; | |
ffe60014 | 1821 | |
b551a063 | 1822 | error: |
840cb59c | 1823 | health_code_update(); |
b551a063 DG |
1824 | return ret; |
1825 | } | |
1826 | ||
91d76f53 | 1827 | /* |
5b4a0ec0 | 1828 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
1829 | * |
1830 | * Should be called with session mutex held. | |
91d76f53 | 1831 | */ |
edb67388 DG |
1832 | static |
1833 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
1834 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) | |
91d76f53 | 1835 | { |
5b4a0ec0 | 1836 | int ret = 0; |
284d8f55 | 1837 | |
840cb59c | 1838 | health_code_update(); |
86acf0da | 1839 | |
5b4a0ec0 | 1840 | /* Create UST event on tracer */ |
fb45065e | 1841 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1842 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 | 1843 | &ua_event->obj); |
fb45065e | 1844 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 | 1845 | if (ret < 0) { |
ffe60014 | 1846 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
88e3c2f5 | 1847 | abort(); |
ffe60014 DG |
1848 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
1849 | ua_event->attr.name, app->pid, ret); | |
1850 | } else { | |
3757b385 DG |
1851 | /* |
1852 | * This is normal behavior, an application can die during the | |
1853 | * creation process. Don't report an error so the execution can | |
1854 | * continue normally. | |
1855 | */ | |
1856 | ret = 0; | |
ffe60014 DG |
1857 | DBG3("UST app create event failed. Application is dead."); |
1858 | } | |
5b4a0ec0 | 1859 | goto error; |
91d76f53 | 1860 | } |
f6a9efaa | 1861 | |
5b4a0ec0 | 1862 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 1863 | |
3428a1b7 JR |
1864 | DBG2("UST app event %s created successfully for pid:%d object: %p", |
1865 | ua_event->attr.name, app->pid, ua_event->obj); | |
f6a9efaa | 1866 | |
840cb59c | 1867 | health_code_update(); |
86acf0da | 1868 | |
025faf73 DG |
1869 | /* Set filter if one is present. */ |
1870 | if (ua_event->filter) { | |
a154c7b8 | 1871 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
025faf73 DG |
1872 | if (ret < 0) { |
1873 | goto error; | |
1874 | } | |
1875 | } | |
1876 | ||
7cc9a73c JI |
1877 | /* Set exclusions for the event */ |
1878 | if (ua_event->exclusion) { | |
c0901ffa | 1879 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
7cc9a73c JI |
1880 | if (ret < 0) { |
1881 | goto error; | |
1882 | } | |
1883 | } | |
1884 | ||
8535a6d9 | 1885 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
1886 | if (ua_event->enabled) { |
1887 | /* | |
1888 | * We now need to explicitly enable the event, since it | |
1889 | * is now disabled at creation. | |
1890 | */ | |
3428a1b7 | 1891 | ret = enable_ust_object(app, ua_event->obj); |
40113787 MD |
1892 | if (ret < 0) { |
1893 | /* | |
1894 | * If we hit an EPERM, something is wrong with our enable call. If | |
1895 | * we get an EEXIST, there is a problem on the tracer side since we | |
1896 | * just created it. | |
1897 | */ | |
1898 | switch (ret) { | |
1899 | case -LTTNG_UST_ERR_PERM: | |
1900 | /* Code flow problem */ | |
1901 | assert(0); | |
1902 | case -LTTNG_UST_ERR_EXIST: | |
1903 | /* It's OK for our use case. */ | |
1904 | ret = 0; | |
1905 | break; | |
1906 | default: | |
1907 | break; | |
1908 | } | |
1909 | goto error; | |
1910 | } | |
8535a6d9 DG |
1911 | } |
1912 | ||
5b4a0ec0 | 1913 | error: |
840cb59c | 1914 | health_code_update(); |
5b4a0ec0 | 1915 | return ret; |
91d76f53 | 1916 | } |
48842b30 | 1917 | |
993578ff JR |
1918 | static int init_ust_event_notifier_from_event_rule( |
1919 | const struct lttng_event_rule *rule, | |
1920 | struct lttng_ust_event_notifier *event_notifier) | |
1921 | { | |
1922 | enum lttng_event_rule_status status; | |
1923 | enum lttng_loglevel_type loglevel_type; | |
1924 | enum lttng_ust_loglevel_type ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; | |
1925 | int loglevel = -1, ret = 0; | |
1926 | const char *pattern; | |
1927 | ||
1928 | /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */ | |
1929 | assert(lttng_event_rule_get_type(rule) == | |
1930 | LTTNG_EVENT_RULE_TYPE_TRACEPOINT); | |
1931 | ||
1932 | memset(event_notifier, 0, sizeof(*event_notifier)); | |
1933 | ||
44760c20 JR |
1934 | if (lttng_event_rule_targets_agent_domain(rule)) { |
1935 | /* | |
1936 | * Special event for agents | |
1937 | * The actual meat of the event is in the filter that will be | |
1938 | * attached later on. | |
1939 | * Set the default values for the agent event. | |
1940 | */ | |
1941 | pattern = event_get_default_agent_ust_name( | |
1942 | lttng_event_rule_get_domain_type(rule)); | |
1943 | loglevel = 0; | |
1944 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; | |
1945 | } else { | |
1946 | status = lttng_event_rule_tracepoint_get_pattern( | |
1947 | rule, &pattern); | |
1948 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1949 | /* At this point, this is a fatal error. */ | |
1950 | abort(); | |
1951 | } | |
993578ff | 1952 | |
44760c20 JR |
1953 | status = lttng_event_rule_tracepoint_get_log_level_type( |
1954 | rule, &loglevel_type); | |
1955 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1956 | /* At this point, this is a fatal error. */ | |
1957 | abort(); | |
1958 | } | |
993578ff | 1959 | |
44760c20 JR |
1960 | switch (loglevel_type) { |
1961 | case LTTNG_EVENT_LOGLEVEL_ALL: | |
1962 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; | |
1963 | break; | |
1964 | case LTTNG_EVENT_LOGLEVEL_RANGE: | |
1965 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; | |
1966 | break; | |
1967 | case LTTNG_EVENT_LOGLEVEL_SINGLE: | |
1968 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; | |
1969 | break; | |
1970 | default: | |
1971 | /* Unknown log level specification type. */ | |
1972 | abort(); | |
1973 | } | |
993578ff | 1974 | |
44760c20 JR |
1975 | if (loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { |
1976 | status = lttng_event_rule_tracepoint_get_log_level( | |
1977 | rule, &loglevel); | |
1978 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); | |
1979 | } | |
993578ff JR |
1980 | } |
1981 | ||
1982 | event_notifier->event.instrumentation = LTTNG_UST_TRACEPOINT; | |
1983 | ret = lttng_strncpy(event_notifier->event.name, pattern, | |
44760c20 | 1984 | LTTNG_UST_SYM_NAME_LEN - 1); |
993578ff JR |
1985 | if (ret) { |
1986 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", | |
1987 | pattern); | |
1988 | goto end; | |
1989 | } | |
1990 | ||
1991 | event_notifier->event.loglevel_type = ust_loglevel_type; | |
1992 | event_notifier->event.loglevel = loglevel; | |
1993 | end: | |
1994 | return ret; | |
1995 | } | |
1996 | ||
1997 | /* | |
1998 | * Create the specified event notifier against the user space tracer of a | |
1999 | * given application. | |
2000 | */ | |
2001 | static int create_ust_event_notifier(struct ust_app *app, | |
2002 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) | |
2003 | { | |
2004 | int ret = 0; | |
267d66aa JR |
2005 | enum lttng_condition_status condition_status; |
2006 | const struct lttng_condition *condition = NULL; | |
993578ff | 2007 | struct lttng_ust_event_notifier event_notifier; |
267d66aa | 2008 | const struct lttng_event_rule *event_rule = NULL; |
993578ff JR |
2009 | |
2010 | health_code_update(); | |
2011 | assert(app->event_notifier_group.object); | |
2012 | ||
267d66aa JR |
2013 | condition = lttng_trigger_get_const_condition( |
2014 | ua_event_notifier_rule->trigger); | |
2015 | assert(condition); | |
2016 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT); | |
993578ff | 2017 | |
267d66aa JR |
2018 | condition_status = lttng_condition_event_rule_get_rule(condition, &event_rule); |
2019 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
2020 | assert(event_rule); | |
2021 | assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT); | |
2022 | ||
2023 | init_ust_event_notifier_from_event_rule(event_rule, &event_notifier); | |
993578ff JR |
2024 | event_notifier.event.token = ua_event_notifier_rule->token; |
2025 | ||
2026 | /* Create UST event notifier against the tracer. */ | |
2027 | pthread_mutex_lock(&app->sock_lock); | |
2028 | ret = ustctl_create_event_notifier(app->sock, &event_notifier, | |
2029 | app->event_notifier_group.object, | |
2030 | &ua_event_notifier_rule->obj); | |
2031 | pthread_mutex_unlock(&app->sock_lock); | |
2032 | if (ret < 0) { | |
2033 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2034 | ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d", | |
2035 | event_notifier.event.name, app->name, | |
2036 | app->ppid, ret); | |
2037 | } else { | |
2038 | /* | |
2039 | * This is normal behavior, an application can die | |
2040 | * during the creation process. Don't report an error so | |
2041 | * the execution can continue normally. | |
2042 | */ | |
2043 | ret = 0; | |
2044 | DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)", | |
2045 | app->name, app->ppid); | |
2046 | } | |
2047 | ||
2048 | goto error; | |
2049 | } | |
2050 | ||
2051 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; | |
2052 | ||
2053 | DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p", | |
2054 | event_notifier.event.name, app->name, app->ppid, | |
2055 | ua_event_notifier_rule->obj); | |
2056 | ||
2057 | health_code_update(); | |
2058 | ||
2059 | /* Set filter if one is present. */ | |
2060 | if (ua_event_notifier_rule->filter) { | |
2061 | ret = set_ust_object_filter(app, ua_event_notifier_rule->filter, | |
2062 | ua_event_notifier_rule->obj); | |
2063 | if (ret < 0) { | |
2064 | goto error; | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | /* Set exclusions for the event. */ | |
2069 | if (ua_event_notifier_rule->exclusion) { | |
2070 | ret = set_ust_object_exclusions(app, | |
2071 | ua_event_notifier_rule->exclusion, | |
2072 | ua_event_notifier_rule->obj); | |
2073 | if (ret < 0) { | |
2074 | goto error; | |
2075 | } | |
2076 | } | |
2077 | ||
2078 | /* | |
2079 | * We now need to explicitly enable the event, since it | |
2080 | * is disabled at creation. | |
2081 | */ | |
2082 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); | |
2083 | if (ret < 0) { | |
2084 | /* | |
2085 | * If we hit an EPERM, something is wrong with our enable call. | |
2086 | * If we get an EEXIST, there is a problem on the tracer side | |
2087 | * since we just created it. | |
2088 | */ | |
2089 | switch (ret) { | |
2090 | case -LTTNG_UST_ERR_PERM: | |
2091 | /* Code flow problem. */ | |
2092 | abort(); | |
2093 | case -LTTNG_UST_ERR_EXIST: | |
2094 | /* It's OK for our use case. */ | |
2095 | ret = 0; | |
2096 | break; | |
2097 | default: | |
2098 | break; | |
2099 | } | |
2100 | ||
2101 | goto error; | |
2102 | } | |
2103 | ||
2104 | ua_event_notifier_rule->enabled = true; | |
2105 | ||
2106 | error: | |
2107 | health_code_update(); | |
2108 | return ret; | |
2109 | } | |
2110 | ||
5b4a0ec0 DG |
2111 | /* |
2112 | * Copy data between an UST app event and a LTT event. | |
2113 | */ | |
421cb601 | 2114 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
2115 | struct ltt_ust_event *uevent) |
2116 | { | |
b4ffad32 JI |
2117 | size_t exclusion_alloc_size; |
2118 | ||
48842b30 DG |
2119 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
2120 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
2121 | ||
fc34caaa DG |
2122 | ua_event->enabled = uevent->enabled; |
2123 | ||
5b4a0ec0 DG |
2124 | /* Copy event attributes */ |
2125 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
2126 | ||
53a80697 MD |
2127 | /* Copy filter bytecode */ |
2128 | if (uevent->filter) { | |
2b00d462 | 2129 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
025faf73 | 2130 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 2131 | } |
b4ffad32 JI |
2132 | |
2133 | /* Copy exclusion data */ | |
2134 | if (uevent->exclusion) { | |
51755dc8 | 2135 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
b4ffad32 JI |
2136 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
2137 | ua_event->exclusion = zmalloc(exclusion_alloc_size); | |
5f8df26c JI |
2138 | if (ua_event->exclusion == NULL) { |
2139 | PERROR("malloc"); | |
2140 | } else { | |
2141 | memcpy(ua_event->exclusion, uevent->exclusion, | |
2142 | exclusion_alloc_size); | |
b4ffad32 JI |
2143 | } |
2144 | } | |
48842b30 DG |
2145 | } |
2146 | ||
5b4a0ec0 DG |
2147 | /* |
2148 | * Copy data between an UST app channel and a LTT channel. | |
2149 | */ | |
421cb601 | 2150 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
2151 | struct ltt_ust_channel *uchan) |
2152 | { | |
fc34caaa | 2153 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
2154 | |
2155 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
2156 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 2157 | |
1624d5b7 JD |
2158 | ua_chan->tracefile_size = uchan->tracefile_size; |
2159 | ua_chan->tracefile_count = uchan->tracefile_count; | |
2160 | ||
ffe60014 DG |
2161 | /* Copy event attributes since the layout is different. */ |
2162 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
2163 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
2164 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
2165 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
2166 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
e9404c27 | 2167 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
ffe60014 | 2168 | ua_chan->attr.output = uchan->attr.output; |
491d1539 MD |
2169 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
2170 | ||
ffe60014 DG |
2171 | /* |
2172 | * Note that the attribute channel type is not set since the channel on the | |
2173 | * tracing registry side does not have this information. | |
2174 | */ | |
48842b30 | 2175 | |
fc34caaa | 2176 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 2177 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 2178 | |
fc34caaa | 2179 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
2180 | } |
2181 | ||
5b4a0ec0 DG |
2182 | /* |
2183 | * Copy data between a UST app session and a regular LTT session. | |
2184 | */ | |
421cb601 | 2185 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 2186 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2187 | { |
477d7741 MD |
2188 | struct tm *timeinfo; |
2189 | char datetime[16]; | |
2190 | int ret; | |
d7ba1388 | 2191 | char tmp_shm_path[PATH_MAX]; |
477d7741 | 2192 | |
940c4592 | 2193 | timeinfo = localtime(&app->registration_time); |
477d7741 | 2194 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
48842b30 | 2195 | |
421cb601 | 2196 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 2197 | |
7972aab2 DG |
2198 | ua_sess->tracing_id = usess->id; |
2199 | ua_sess->id = get_next_session_id(); | |
ff588497 JR |
2200 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
2201 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); | |
2202 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); | |
2203 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); | |
7972aab2 DG |
2204 | ua_sess->buffer_type = usess->buffer_type; |
2205 | ua_sess->bits_per_long = app->bits_per_long; | |
6addfa37 | 2206 | |
7972aab2 | 2207 | /* There is only one consumer object per session possible. */ |
6addfa37 | 2208 | consumer_output_get(usess->consumer); |
7972aab2 | 2209 | ua_sess->consumer = usess->consumer; |
6addfa37 | 2210 | |
2bba9e53 | 2211 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 2212 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
2213 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
2214 | &usess->metadata_attr); | |
7972aab2 DG |
2215 | |
2216 | switch (ua_sess->buffer_type) { | |
2217 | case LTTNG_BUFFER_PER_PID: | |
2218 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 2219 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
2220 | datetime); |
2221 | break; | |
2222 | case LTTNG_BUFFER_PER_UID: | |
2223 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
470cc211 | 2224 | DEFAULT_UST_TRACE_UID_PATH, |
ff588497 | 2225 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
470cc211 | 2226 | app->bits_per_long); |
7972aab2 DG |
2227 | break; |
2228 | default: | |
2229 | assert(0); | |
2230 | goto error; | |
2231 | } | |
477d7741 MD |
2232 | if (ret < 0) { |
2233 | PERROR("asprintf UST shadow copy session"); | |
477d7741 | 2234 | assert(0); |
7972aab2 | 2235 | goto error; |
477d7741 MD |
2236 | } |
2237 | ||
3d071855 MD |
2238 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
2239 | sizeof(ua_sess->root_shm_path)); | |
2240 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2241 | strncpy(ua_sess->shm_path, usess->shm_path, |
2242 | sizeof(ua_sess->shm_path)); | |
2243 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2244 | if (ua_sess->shm_path[0]) { | |
2245 | switch (ua_sess->buffer_type) { | |
2246 | case LTTNG_BUFFER_PER_PID: | |
2247 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2248 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
d7ba1388 MD |
2249 | app->name, app->pid, datetime); |
2250 | break; | |
2251 | case LTTNG_BUFFER_PER_UID: | |
2252 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2253 | "/" DEFAULT_UST_TRACE_UID_PATH, |
d7ba1388 MD |
2254 | app->uid, app->bits_per_long); |
2255 | break; | |
2256 | default: | |
2257 | assert(0); | |
2258 | goto error; | |
2259 | } | |
2260 | if (ret < 0) { | |
2261 | PERROR("sprintf UST shadow copy session"); | |
2262 | assert(0); | |
2263 | goto error; | |
2264 | } | |
2265 | strncat(ua_sess->shm_path, tmp_shm_path, | |
2266 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
2267 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2268 | } | |
6addfa37 | 2269 | return; |
7972aab2 DG |
2270 | |
2271 | error: | |
6addfa37 | 2272 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
2273 | } |
2274 | ||
78f0bacd DG |
2275 | /* |
2276 | * Lookup sesison wrapper. | |
2277 | */ | |
84cd17c6 | 2278 | static |
fb9a95c4 | 2279 | void __lookup_session_by_app(const struct ltt_ust_session *usess, |
bec39940 | 2280 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
2281 | { |
2282 | /* Get right UST app session from app */ | |
d9bf3ca4 | 2283 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
2284 | } |
2285 | ||
421cb601 DG |
2286 | /* |
2287 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 2288 | * id. |
421cb601 | 2289 | */ |
48842b30 | 2290 | static struct ust_app_session *lookup_session_by_app( |
fb9a95c4 | 2291 | const struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2292 | { |
bec39940 | 2293 | struct lttng_ht_iter iter; |
d9bf3ca4 | 2294 | struct lttng_ht_node_u64 *node; |
48842b30 | 2295 | |
84cd17c6 | 2296 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 2297 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
2298 | if (node == NULL) { |
2299 | goto error; | |
2300 | } | |
2301 | ||
2302 | return caa_container_of(node, struct ust_app_session, node); | |
2303 | ||
2304 | error: | |
2305 | return NULL; | |
2306 | } | |
2307 | ||
7972aab2 DG |
2308 | /* |
2309 | * Setup buffer registry per PID for the given session and application. If none | |
2310 | * is found, a new one is created, added to the global registry and | |
2311 | * initialized. If regp is valid, it's set with the newly created object. | |
2312 | * | |
2313 | * Return 0 on success or else a negative value. | |
2314 | */ | |
2315 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
2316 | struct ust_app *app, struct buffer_reg_pid **regp) | |
2317 | { | |
2318 | int ret = 0; | |
2319 | struct buffer_reg_pid *reg_pid; | |
2320 | ||
2321 | assert(ua_sess); | |
2322 | assert(app); | |
2323 | ||
2324 | rcu_read_lock(); | |
2325 | ||
2326 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
2327 | if (!reg_pid) { | |
2328 | /* | |
2329 | * This is the create channel path meaning that if there is NO | |
2330 | * registry available, we have to create one for this session. | |
2331 | */ | |
d7ba1388 | 2332 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 2333 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
2334 | if (ret < 0) { |
2335 | goto error; | |
2336 | } | |
7972aab2 DG |
2337 | } else { |
2338 | goto end; | |
2339 | } | |
2340 | ||
2341 | /* Initialize registry. */ | |
2342 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, | |
2343 | app->bits_per_long, app->uint8_t_alignment, | |
2344 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf | 2345 | app->uint64_t_alignment, app->long_alignment, |
470cc211 JG |
2346 | app->byte_order, app->version.major, app->version.minor, |
2347 | reg_pid->root_shm_path, reg_pid->shm_path, | |
ff588497 JR |
2348 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
2349 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
2350 | ua_sess->tracing_id, | |
8de88061 | 2351 | app->uid); |
7972aab2 | 2352 | if (ret < 0) { |
286c991a MD |
2353 | /* |
2354 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
2355 | * destroy the buffer registry, because it is always expected | |
2356 | * that if the buffer registry can be found, its ust registry is | |
2357 | * non-NULL. | |
2358 | */ | |
2359 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
2360 | goto error; |
2361 | } | |
2362 | ||
286c991a MD |
2363 | buffer_reg_pid_add(reg_pid); |
2364 | ||
7972aab2 DG |
2365 | DBG3("UST app buffer registry per PID created successfully"); |
2366 | ||
2367 | end: | |
2368 | if (regp) { | |
2369 | *regp = reg_pid; | |
2370 | } | |
2371 | error: | |
2372 | rcu_read_unlock(); | |
2373 | return ret; | |
2374 | } | |
2375 | ||
2376 | /* | |
2377 | * Setup buffer registry per UID for the given session and application. If none | |
2378 | * is found, a new one is created, added to the global registry and | |
2379 | * initialized. If regp is valid, it's set with the newly created object. | |
2380 | * | |
2381 | * Return 0 on success or else a negative value. | |
2382 | */ | |
2383 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 2384 | struct ust_app_session *ua_sess, |
7972aab2 DG |
2385 | struct ust_app *app, struct buffer_reg_uid **regp) |
2386 | { | |
2387 | int ret = 0; | |
2388 | struct buffer_reg_uid *reg_uid; | |
2389 | ||
2390 | assert(usess); | |
2391 | assert(app); | |
2392 | ||
2393 | rcu_read_lock(); | |
2394 | ||
2395 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
2396 | if (!reg_uid) { | |
2397 | /* | |
2398 | * This is the create channel path meaning that if there is NO | |
2399 | * registry available, we have to create one for this session. | |
2400 | */ | |
2401 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, | |
3d071855 MD |
2402 | LTTNG_DOMAIN_UST, ®_uid, |
2403 | ua_sess->root_shm_path, ua_sess->shm_path); | |
7972aab2 DG |
2404 | if (ret < 0) { |
2405 | goto error; | |
2406 | } | |
7972aab2 DG |
2407 | } else { |
2408 | goto end; | |
2409 | } | |
2410 | ||
2411 | /* Initialize registry. */ | |
af6142cf | 2412 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
7972aab2 DG |
2413 | app->bits_per_long, app->uint8_t_alignment, |
2414 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
2415 | app->uint64_t_alignment, app->long_alignment, |
2416 | app->byte_order, app->version.major, | |
3d071855 | 2417 | app->version.minor, reg_uid->root_shm_path, |
8de88061 JR |
2418 | reg_uid->shm_path, usess->uid, usess->gid, |
2419 | ua_sess->tracing_id, app->uid); | |
7972aab2 | 2420 | if (ret < 0) { |
286c991a MD |
2421 | /* |
2422 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
2423 | * destroy the buffer registry, because it is always expected | |
2424 | * that if the buffer registry can be found, its ust registry is | |
2425 | * non-NULL. | |
2426 | */ | |
2427 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
2428 | goto error; |
2429 | } | |
2430 | /* Add node to teardown list of the session. */ | |
2431 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
2432 | ||
286c991a | 2433 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 2434 | |
286c991a | 2435 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
2436 | end: |
2437 | if (regp) { | |
2438 | *regp = reg_uid; | |
2439 | } | |
2440 | error: | |
2441 | rcu_read_unlock(); | |
2442 | return ret; | |
2443 | } | |
2444 | ||
421cb601 | 2445 | /* |
3d8ca23b | 2446 | * Create a session on the tracer side for the given app. |
421cb601 | 2447 | * |
3d8ca23b DG |
2448 | * On success, ua_sess_ptr is populated with the session pointer or else left |
2449 | * untouched. If the session was created, is_created is set to 1. On error, | |
2450 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
2451 | * be NULL. | |
2452 | * | |
2453 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
2454 | * -ENOTCONN which is the default code if the ustctl_create_session fails. | |
421cb601 | 2455 | */ |
03f91eaa | 2456 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
3d8ca23b DG |
2457 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
2458 | int *is_created) | |
421cb601 | 2459 | { |
3d8ca23b | 2460 | int ret, created = 0; |
421cb601 DG |
2461 | struct ust_app_session *ua_sess; |
2462 | ||
3d8ca23b DG |
2463 | assert(usess); |
2464 | assert(app); | |
2465 | assert(ua_sess_ptr); | |
2466 | ||
840cb59c | 2467 | health_code_update(); |
86acf0da | 2468 | |
421cb601 DG |
2469 | ua_sess = lookup_session_by_app(usess, app); |
2470 | if (ua_sess == NULL) { | |
d9bf3ca4 | 2471 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 2472 | app->pid, usess->id); |
40bbd087 | 2473 | ua_sess = alloc_ust_app_session(); |
421cb601 DG |
2474 | if (ua_sess == NULL) { |
2475 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
2476 | ret = -ENOMEM; |
2477 | goto error; | |
421cb601 | 2478 | } |
477d7741 | 2479 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 2480 | created = 1; |
421cb601 DG |
2481 | } |
2482 | ||
7972aab2 DG |
2483 | switch (usess->buffer_type) { |
2484 | case LTTNG_BUFFER_PER_PID: | |
2485 | /* Init local registry. */ | |
2486 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 2487 | if (ret < 0) { |
e64207cf | 2488 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2489 | goto error; |
2490 | } | |
2491 | break; | |
2492 | case LTTNG_BUFFER_PER_UID: | |
2493 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 2494 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 2495 | if (ret < 0) { |
e64207cf | 2496 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2497 | goto error; |
2498 | } | |
2499 | break; | |
2500 | default: | |
2501 | assert(0); | |
2502 | ret = -EINVAL; | |
2503 | goto error; | |
2504 | } | |
2505 | ||
2506 | health_code_update(); | |
2507 | ||
2508 | if (ua_sess->handle == -1) { | |
fb45065e | 2509 | pthread_mutex_lock(&app->sock_lock); |
7972aab2 | 2510 | ret = ustctl_create_session(app->sock); |
fb45065e | 2511 | pthread_mutex_unlock(&app->sock_lock); |
7972aab2 DG |
2512 | if (ret < 0) { |
2513 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2514 | ERR("Creating session for app pid %d with ret %d", | |
ffe60014 DG |
2515 | app->pid, ret); |
2516 | } else { | |
2517 | DBG("UST app creating session failed. Application is dead"); | |
3757b385 DG |
2518 | /* |
2519 | * This is normal behavior, an application can die during the | |
2520 | * creation process. Don't report an error so the execution can | |
2521 | * continue normally. This will get flagged ENOTCONN and the | |
2522 | * caller will handle it. | |
2523 | */ | |
2524 | ret = 0; | |
ffe60014 | 2525 | } |
d0b96690 | 2526 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
2527 | if (ret != -ENOMEM) { |
2528 | /* | |
2529 | * Tracer is probably gone or got an internal error so let's | |
2530 | * behave like it will soon unregister or not usable. | |
2531 | */ | |
2532 | ret = -ENOTCONN; | |
2533 | } | |
2534 | goto error; | |
421cb601 DG |
2535 | } |
2536 | ||
7972aab2 DG |
2537 | ua_sess->handle = ret; |
2538 | ||
2539 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
2540 | lttng_ht_node_init_u64(&ua_sess->node, |
2541 | ua_sess->tracing_id); | |
2542 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
10b56aef MD |
2543 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
2544 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, | |
2545 | &ua_sess->ust_objd_node); | |
7972aab2 DG |
2546 | |
2547 | DBG2("UST app session created successfully with handle %d", ret); | |
2548 | } | |
2549 | ||
2550 | *ua_sess_ptr = ua_sess; | |
2551 | if (is_created) { | |
2552 | *is_created = created; | |
2553 | } | |
2554 | ||
2555 | /* Everything went well. */ | |
2556 | ret = 0; | |
2557 | ||
2558 | error: | |
2559 | health_code_update(); | |
2560 | return ret; | |
2561 | } | |
2562 | ||
6a6b2068 JG |
2563 | /* |
2564 | * Match function for a hash table lookup of ust_app_ctx. | |
2565 | * | |
2566 | * It matches an ust app context based on the context type and, in the case | |
2567 | * of perf counters, their name. | |
2568 | */ | |
2569 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
2570 | { | |
2571 | struct ust_app_ctx *ctx; | |
bdf64013 | 2572 | const struct lttng_ust_context_attr *key; |
6a6b2068 JG |
2573 | |
2574 | assert(node); | |
2575 | assert(_key); | |
2576 | ||
2577 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
2578 | key = _key; | |
2579 | ||
2580 | /* Context type */ | |
2581 | if (ctx->ctx.ctx != key->ctx) { | |
2582 | goto no_match; | |
2583 | } | |
2584 | ||
bdf64013 JG |
2585 | switch(key->ctx) { |
2586 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: | |
6a6b2068 | 2587 | if (strncmp(key->u.perf_counter.name, |
bdf64013 JG |
2588 | ctx->ctx.u.perf_counter.name, |
2589 | sizeof(key->u.perf_counter.name))) { | |
2590 | goto no_match; | |
2591 | } | |
2592 | break; | |
2593 | case LTTNG_UST_CONTEXT_APP_CONTEXT: | |
2594 | if (strcmp(key->u.app_ctx.provider_name, | |
2595 | ctx->ctx.u.app_ctx.provider_name) || | |
2596 | strcmp(key->u.app_ctx.ctx_name, | |
2597 | ctx->ctx.u.app_ctx.ctx_name)) { | |
6a6b2068 JG |
2598 | goto no_match; |
2599 | } | |
bdf64013 JG |
2600 | break; |
2601 | default: | |
2602 | break; | |
6a6b2068 JG |
2603 | } |
2604 | ||
2605 | /* Match. */ | |
2606 | return 1; | |
2607 | ||
2608 | no_match: | |
2609 | return 0; | |
2610 | } | |
2611 | ||
2612 | /* | |
2613 | * Lookup for an ust app context from an lttng_ust_context. | |
2614 | * | |
be184a0f | 2615 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
2616 | * Return an ust_app_ctx object or NULL on error. |
2617 | */ | |
2618 | static | |
2619 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
bdf64013 | 2620 | struct lttng_ust_context_attr *uctx) |
6a6b2068 JG |
2621 | { |
2622 | struct lttng_ht_iter iter; | |
2623 | struct lttng_ht_node_ulong *node; | |
2624 | struct ust_app_ctx *app_ctx = NULL; | |
2625 | ||
2626 | assert(uctx); | |
2627 | assert(ht); | |
2628 | ||
2629 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
2630 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
2631 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
2632 | node = lttng_ht_iter_get_node_ulong(&iter); | |
2633 | if (!node) { | |
2634 | goto end; | |
2635 | } | |
2636 | ||
2637 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); | |
2638 | ||
2639 | end: | |
2640 | return app_ctx; | |
2641 | } | |
2642 | ||
7972aab2 DG |
2643 | /* |
2644 | * Create a context for the channel on the tracer. | |
2645 | * | |
2646 | * Called with UST app session lock held and a RCU read side lock. | |
2647 | */ | |
2648 | static | |
c9edf082 | 2649 | int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
f3db82be | 2650 | struct lttng_ust_context_attr *uctx, |
7972aab2 DG |
2651 | struct ust_app *app) |
2652 | { | |
2653 | int ret = 0; | |
7972aab2 DG |
2654 | struct ust_app_ctx *ua_ctx; |
2655 | ||
2656 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
2657 | ||
6a6b2068 JG |
2658 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
2659 | if (ua_ctx) { | |
7972aab2 DG |
2660 | ret = -EEXIST; |
2661 | goto error; | |
2662 | } | |
2663 | ||
2664 | ua_ctx = alloc_ust_app_ctx(uctx); | |
2665 | if (ua_ctx == NULL) { | |
2666 | /* malloc failed */ | |
7682f304 | 2667 | ret = -ENOMEM; |
7972aab2 DG |
2668 | goto error; |
2669 | } | |
2670 | ||
2671 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 2672 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 2673 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
2674 | |
2675 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2676 | if (ret < 0) { | |
2677 | goto error; | |
2678 | } | |
2679 | ||
2680 | error: | |
2681 | return ret; | |
2682 | } | |
2683 | ||
2684 | /* | |
2685 | * Enable on the tracer side a ust app event for the session and channel. | |
2686 | * | |
2687 | * Called with UST app session lock held. | |
2688 | */ | |
2689 | static | |
2690 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
2691 | struct ust_app_event *ua_event, struct ust_app *app) | |
2692 | { | |
2693 | int ret; | |
2694 | ||
3428a1b7 | 2695 | ret = enable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2696 | if (ret < 0) { |
2697 | goto error; | |
2698 | } | |
2699 | ||
2700 | ua_event->enabled = 1; | |
2701 | ||
2702 | error: | |
2703 | return ret; | |
2704 | } | |
2705 | ||
2706 | /* | |
2707 | * Disable on the tracer side a ust app event for the session and channel. | |
2708 | */ | |
2709 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
2710 | struct ust_app_event *ua_event, struct ust_app *app) | |
2711 | { | |
2712 | int ret; | |
2713 | ||
e2456d0a | 2714 | ret = disable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2715 | if (ret < 0) { |
2716 | goto error; | |
2717 | } | |
2718 | ||
2719 | ua_event->enabled = 0; | |
2720 | ||
2721 | error: | |
2722 | return ret; | |
2723 | } | |
2724 | ||
2725 | /* | |
2726 | * Lookup ust app channel for session and disable it on the tracer side. | |
2727 | */ | |
2728 | static | |
2729 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
2730 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
2731 | { | |
2732 | int ret; | |
2733 | ||
2734 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2735 | if (ret < 0) { | |
2736 | goto error; | |
2737 | } | |
2738 | ||
2739 | ua_chan->enabled = 0; | |
2740 | ||
2741 | error: | |
2742 | return ret; | |
2743 | } | |
2744 | ||
2745 | /* | |
2746 | * Lookup ust app channel for session and enable it on the tracer side. This | |
2747 | * MUST be called with a RCU read side lock acquired. | |
2748 | */ | |
2749 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
2750 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
2751 | { | |
2752 | int ret = 0; | |
2753 | struct lttng_ht_iter iter; | |
2754 | struct lttng_ht_node_str *ua_chan_node; | |
2755 | struct ust_app_channel *ua_chan; | |
2756 | ||
2757 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); | |
2758 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
2759 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 2760 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
2761 | uchan->name, ua_sess->tracing_id); |
2762 | goto error; | |
2763 | } | |
2764 | ||
2765 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2766 | ||
2767 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
2768 | if (ret < 0) { | |
2769 | goto error; | |
2770 | } | |
2771 | ||
2772 | error: | |
2773 | return ret; | |
2774 | } | |
2775 | ||
2776 | /* | |
2777 | * Ask the consumer to create a channel and get it if successful. | |
2778 | * | |
fad1ed2f JR |
2779 | * Called with UST app session lock held. |
2780 | * | |
7972aab2 DG |
2781 | * Return 0 on success or else a negative value. |
2782 | */ | |
2783 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
2784 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
e098433c JG |
2785 | int bitness, struct ust_registry_session *registry, |
2786 | uint64_t trace_archive_id) | |
7972aab2 DG |
2787 | { |
2788 | int ret; | |
2789 | unsigned int nb_fd = 0; | |
2790 | struct consumer_socket *socket; | |
2791 | ||
2792 | assert(usess); | |
2793 | assert(ua_sess); | |
2794 | assert(ua_chan); | |
2795 | assert(registry); | |
2796 | ||
2797 | rcu_read_lock(); | |
2798 | health_code_update(); | |
2799 | ||
2800 | /* Get the right consumer socket for the application. */ | |
2801 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
2802 | if (!socket) { | |
2803 | ret = -EINVAL; | |
2804 | goto error; | |
2805 | } | |
2806 | ||
2807 | health_code_update(); | |
2808 | ||
2809 | /* Need one fd for the channel. */ | |
2810 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2811 | if (ret < 0) { | |
2812 | ERR("Exhausted number of available FD upon create channel"); | |
2813 | goto error; | |
2814 | } | |
2815 | ||
2816 | /* | |
2817 | * Ask consumer to create channel. The consumer will return the number of | |
2818 | * stream we have to expect. | |
2819 | */ | |
2820 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
d2956687 | 2821 | registry, usess->current_trace_chunk); |
7972aab2 DG |
2822 | if (ret < 0) { |
2823 | goto error_ask; | |
2824 | } | |
2825 | ||
2826 | /* | |
2827 | * Compute the number of fd needed before receiving them. It must be 2 per | |
2828 | * stream (2 being the default value here). | |
2829 | */ | |
2830 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
2831 | ||
2832 | /* Reserve the amount of file descriptor we need. */ | |
2833 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
2834 | if (ret < 0) { | |
2835 | ERR("Exhausted number of available FD upon create channel"); | |
2836 | goto error_fd_get_stream; | |
2837 | } | |
2838 | ||
2839 | health_code_update(); | |
2840 | ||
2841 | /* | |
2842 | * Now get the channel from the consumer. This call wil populate the stream | |
2843 | * list of that channel and set the ust objects. | |
2844 | */ | |
d9078d0c DG |
2845 | if (usess->consumer->enabled) { |
2846 | ret = ust_consumer_get_channel(socket, ua_chan); | |
2847 | if (ret < 0) { | |
2848 | goto error_destroy; | |
2849 | } | |
7972aab2 DG |
2850 | } |
2851 | ||
2852 | rcu_read_unlock(); | |
2853 | return 0; | |
2854 | ||
2855 | error_destroy: | |
2856 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
2857 | error_fd_get_stream: | |
2858 | /* | |
2859 | * Initiate a destroy channel on the consumer since we had an error | |
2860 | * handling it on our side. The return value is of no importance since we | |
2861 | * already have a ret value set by the previous error that we need to | |
2862 | * return. | |
2863 | */ | |
2864 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
2865 | error_ask: | |
2866 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2867 | error: | |
2868 | health_code_update(); | |
2869 | rcu_read_unlock(); | |
2870 | return ret; | |
2871 | } | |
2872 | ||
2873 | /* | |
2874 | * Duplicate the ust data object of the ust app stream and save it in the | |
2875 | * buffer registry stream. | |
2876 | * | |
2877 | * Return 0 on success or else a negative value. | |
2878 | */ | |
2879 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
2880 | struct ust_app_stream *stream) | |
2881 | { | |
2882 | int ret; | |
2883 | ||
2884 | assert(reg_stream); | |
2885 | assert(stream); | |
2886 | ||
2887 | /* Reserve the amount of file descriptor we need. */ | |
2888 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
2889 | if (ret < 0) { | |
2890 | ERR("Exhausted number of available FD upon duplicate stream"); | |
2891 | goto error; | |
2892 | } | |
2893 | ||
2894 | /* Duplicate object for stream once the original is in the registry. */ | |
2895 | ret = ustctl_duplicate_ust_object_data(&stream->obj, | |
2896 | reg_stream->obj.ust); | |
2897 | if (ret < 0) { | |
2898 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
2899 | reg_stream->obj.ust, stream->obj, ret); | |
2900 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
2901 | goto error; | |
2902 | } | |
2903 | stream->handle = stream->obj->handle; | |
2904 | ||
2905 | error: | |
2906 | return ret; | |
2907 | } | |
2908 | ||
2909 | /* | |
2910 | * Duplicate the ust data object of the ust app. channel and save it in the | |
2911 | * buffer registry channel. | |
2912 | * | |
2913 | * Return 0 on success or else a negative value. | |
2914 | */ | |
2915 | static int duplicate_channel_object(struct buffer_reg_channel *reg_chan, | |
2916 | struct ust_app_channel *ua_chan) | |
2917 | { | |
2918 | int ret; | |
2919 | ||
2920 | assert(reg_chan); | |
2921 | assert(ua_chan); | |
2922 | ||
2923 | /* Need two fds for the channel. */ | |
2924 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2925 | if (ret < 0) { | |
2926 | ERR("Exhausted number of available FD upon duplicate channel"); | |
2927 | goto error_fd_get; | |
2928 | } | |
2929 | ||
2930 | /* Duplicate object for stream once the original is in the registry. */ | |
2931 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust); | |
2932 | if (ret < 0) { | |
2933 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
2934 | reg_chan->obj.ust, ua_chan->obj, ret); | |
2935 | goto error; | |
2936 | } | |
2937 | ua_chan->handle = ua_chan->obj->handle; | |
2938 | ||
2939 | return 0; | |
2940 | ||
2941 | error: | |
2942 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2943 | error_fd_get: | |
2944 | return ret; | |
2945 | } | |
2946 | ||
2947 | /* | |
2948 | * For a given channel buffer registry, setup all streams of the given ust | |
2949 | * application channel. | |
2950 | * | |
2951 | * Return 0 on success or else a negative value. | |
2952 | */ | |
2953 | static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan, | |
fb45065e MD |
2954 | struct ust_app_channel *ua_chan, |
2955 | struct ust_app *app) | |
7972aab2 DG |
2956 | { |
2957 | int ret = 0; | |
2958 | struct ust_app_stream *stream, *stmp; | |
2959 | ||
2960 | assert(reg_chan); | |
2961 | assert(ua_chan); | |
2962 | ||
2963 | DBG2("UST app setup buffer registry stream"); | |
2964 | ||
2965 | /* Send all streams to application. */ | |
2966 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
2967 | struct buffer_reg_stream *reg_stream; | |
2968 | ||
2969 | ret = buffer_reg_stream_create(®_stream); | |
2970 | if (ret < 0) { | |
2971 | goto error; | |
2972 | } | |
2973 | ||
2974 | /* | |
2975 | * Keep original pointer and nullify it in the stream so the delete | |
2976 | * stream call does not release the object. | |
2977 | */ | |
2978 | reg_stream->obj.ust = stream->obj; | |
2979 | stream->obj = NULL; | |
2980 | buffer_reg_stream_add(reg_stream, reg_chan); | |
421cb601 | 2981 | |
7972aab2 DG |
2982 | /* We don't need the streams anymore. */ |
2983 | cds_list_del(&stream->list); | |
fb45065e | 2984 | delete_ust_app_stream(-1, stream, app); |
7972aab2 | 2985 | } |
421cb601 | 2986 | |
7972aab2 DG |
2987 | error: |
2988 | return ret; | |
2989 | } | |
2990 | ||
2991 | /* | |
2992 | * Create a buffer registry channel for the given session registry and | |
2993 | * application channel object. If regp pointer is valid, it's set with the | |
2994 | * created object. Important, the created object is NOT added to the session | |
2995 | * registry hash table. | |
2996 | * | |
2997 | * Return 0 on success else a negative value. | |
2998 | */ | |
2999 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
3000 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
3001 | { | |
3002 | int ret; | |
3003 | struct buffer_reg_channel *reg_chan = NULL; | |
3004 | ||
3005 | assert(reg_sess); | |
3006 | assert(ua_chan); | |
3007 | ||
3008 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
3009 | ||
3010 | /* Create buffer registry channel. */ | |
3011 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, ®_chan); | |
3012 | if (ret < 0) { | |
3013 | goto error_create; | |
421cb601 | 3014 | } |
7972aab2 DG |
3015 | assert(reg_chan); |
3016 | reg_chan->consumer_key = ua_chan->key; | |
8c924c7b | 3017 | reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
d07ceecd | 3018 | reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
421cb601 | 3019 | |
7972aab2 DG |
3020 | /* Create and add a channel registry to session. */ |
3021 | ret = ust_registry_channel_add(reg_sess->reg.ust, | |
3022 | ua_chan->tracing_channel_id); | |
3023 | if (ret < 0) { | |
3024 | goto error; | |
d88aee68 | 3025 | } |
7972aab2 | 3026 | buffer_reg_channel_add(reg_sess, reg_chan); |
d88aee68 | 3027 | |
7972aab2 DG |
3028 | if (regp) { |
3029 | *regp = reg_chan; | |
3d8ca23b | 3030 | } |
d88aee68 | 3031 | |
7972aab2 | 3032 | return 0; |
3d8ca23b DG |
3033 | |
3034 | error: | |
7972aab2 DG |
3035 | /* Safe because the registry channel object was not added to any HT. */ |
3036 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
3037 | error_create: | |
3d8ca23b | 3038 | return ret; |
421cb601 DG |
3039 | } |
3040 | ||
55cc08a6 | 3041 | /* |
7972aab2 DG |
3042 | * Setup buffer registry channel for the given session registry and application |
3043 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 3044 | * |
7972aab2 | 3045 | * Return 0 on success else a negative value. |
55cc08a6 | 3046 | */ |
7972aab2 | 3047 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
fb45065e MD |
3048 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan, |
3049 | struct ust_app *app) | |
55cc08a6 | 3050 | { |
7972aab2 | 3051 | int ret; |
55cc08a6 | 3052 | |
7972aab2 DG |
3053 | assert(reg_sess); |
3054 | assert(reg_chan); | |
3055 | assert(ua_chan); | |
3056 | assert(ua_chan->obj); | |
55cc08a6 | 3057 | |
7972aab2 | 3058 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 3059 | |
7972aab2 | 3060 | /* Setup all streams for the registry. */ |
fb45065e | 3061 | ret = setup_buffer_reg_streams(reg_chan, ua_chan, app); |
7972aab2 | 3062 | if (ret < 0) { |
55cc08a6 DG |
3063 | goto error; |
3064 | } | |
3065 | ||
7972aab2 DG |
3066 | reg_chan->obj.ust = ua_chan->obj; |
3067 | ua_chan->obj = NULL; | |
55cc08a6 | 3068 | |
7972aab2 | 3069 | return 0; |
55cc08a6 DG |
3070 | |
3071 | error: | |
7972aab2 DG |
3072 | buffer_reg_channel_remove(reg_sess, reg_chan); |
3073 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
3074 | return ret; |
3075 | } | |
3076 | ||
edb67388 | 3077 | /* |
7972aab2 | 3078 | * Send buffer registry channel to the application. |
d0b96690 | 3079 | * |
7972aab2 | 3080 | * Return 0 on success else a negative value. |
edb67388 | 3081 | */ |
7972aab2 DG |
3082 | static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan, |
3083 | struct ust_app *app, struct ust_app_session *ua_sess, | |
3084 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
3085 | { |
3086 | int ret; | |
7972aab2 | 3087 | struct buffer_reg_stream *reg_stream; |
edb67388 | 3088 | |
7972aab2 DG |
3089 | assert(reg_chan); |
3090 | assert(app); | |
3091 | assert(ua_sess); | |
3092 | assert(ua_chan); | |
3093 | ||
3094 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
3095 | ||
3096 | ret = duplicate_channel_object(reg_chan, ua_chan); | |
edb67388 DG |
3097 | if (ret < 0) { |
3098 | goto error; | |
3099 | } | |
3100 | ||
7972aab2 DG |
3101 | /* Send channel to the application. */ |
3102 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
3103 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3104 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
3105 | goto error; | |
3106 | } else if (ret < 0) { | |
7972aab2 DG |
3107 | goto error; |
3108 | } | |
3109 | ||
3110 | health_code_update(); | |
3111 | ||
3112 | /* Send all streams to application. */ | |
3113 | pthread_mutex_lock(®_chan->stream_list_lock); | |
3114 | cds_list_for_each_entry(reg_stream, ®_chan->streams, lnode) { | |
3115 | struct ust_app_stream stream; | |
3116 | ||
3117 | ret = duplicate_stream_object(reg_stream, &stream); | |
3118 | if (ret < 0) { | |
3119 | goto error_stream_unlock; | |
3120 | } | |
3121 | ||
3122 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
3123 | if (ret < 0) { | |
fb45065e | 3124 | (void) release_ust_app_stream(-1, &stream, app); |
a7169585 MD |
3125 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3126 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
a7169585 | 3127 | } |
7972aab2 DG |
3128 | goto error_stream_unlock; |
3129 | } | |
edb67388 | 3130 | |
7972aab2 DG |
3131 | /* |
3132 | * The return value is not important here. This function will output an | |
3133 | * error if needed. | |
3134 | */ | |
fb45065e | 3135 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
3136 | } |
3137 | ua_chan->is_sent = 1; | |
3138 | ||
3139 | error_stream_unlock: | |
3140 | pthread_mutex_unlock(®_chan->stream_list_lock); | |
edb67388 DG |
3141 | error: |
3142 | return ret; | |
3143 | } | |
3144 | ||
9730260e | 3145 | /* |
7972aab2 DG |
3146 | * Create and send to the application the created buffers with per UID buffers. |
3147 | * | |
9acdc1d6 | 3148 | * This MUST be called with a RCU read side lock acquired. |
71e0a100 | 3149 | * The session list lock and the session's lock must be acquired. |
9acdc1d6 | 3150 | * |
7972aab2 | 3151 | * Return 0 on success else a negative value. |
9730260e | 3152 | */ |
7972aab2 DG |
3153 | static int create_channel_per_uid(struct ust_app *app, |
3154 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3155 | struct ust_app_channel *ua_chan) | |
9730260e DG |
3156 | { |
3157 | int ret; | |
7972aab2 DG |
3158 | struct buffer_reg_uid *reg_uid; |
3159 | struct buffer_reg_channel *reg_chan; | |
e32d7f27 | 3160 | struct ltt_session *session = NULL; |
e098433c JG |
3161 | enum lttng_error_code notification_ret; |
3162 | struct ust_registry_channel *chan_reg; | |
9730260e | 3163 | |
7972aab2 DG |
3164 | assert(app); |
3165 | assert(usess); | |
3166 | assert(ua_sess); | |
3167 | assert(ua_chan); | |
3168 | ||
3169 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
3170 | ||
3171 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
3172 | /* | |
3173 | * The session creation handles the creation of this global registry | |
3174 | * object. If none can be find, there is a code flow problem or a | |
3175 | * teardown race. | |
3176 | */ | |
3177 | assert(reg_uid); | |
3178 | ||
3179 | reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, | |
3180 | reg_uid); | |
2721f7ea JG |
3181 | if (reg_chan) { |
3182 | goto send_channel; | |
3183 | } | |
7972aab2 | 3184 | |
2721f7ea JG |
3185 | /* Create the buffer registry channel object. */ |
3186 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, ®_chan); | |
3187 | if (ret < 0) { | |
3188 | ERR("Error creating the UST channel \"%s\" registry instance", | |
f14256d6 | 3189 | ua_chan->name); |
2721f7ea JG |
3190 | goto error; |
3191 | } | |
f14256d6 | 3192 | |
e098433c JG |
3193 | session = session_find_by_id(ua_sess->tracing_id); |
3194 | assert(session); | |
3195 | assert(pthread_mutex_trylock(&session->lock)); | |
3196 | assert(session_trylock_list()); | |
3197 | ||
2721f7ea JG |
3198 | /* |
3199 | * Create the buffers on the consumer side. This call populates the | |
3200 | * ust app channel object with all streams and data object. | |
3201 | */ | |
3202 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
e098433c | 3203 | app->bits_per_long, reg_uid->registry->reg.ust, |
d2956687 | 3204 | session->most_recent_chunk_id.value); |
2721f7ea JG |
3205 | if (ret < 0) { |
3206 | ERR("Error creating UST channel \"%s\" on the consumer daemon", | |
3207 | ua_chan->name); | |
7972aab2 DG |
3208 | |
3209 | /* | |
2721f7ea JG |
3210 | * Let's remove the previously created buffer registry channel so |
3211 | * it's not visible anymore in the session registry. | |
7972aab2 | 3212 | */ |
2721f7ea JG |
3213 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, |
3214 | ua_chan->tracing_channel_id, false); | |
3215 | buffer_reg_channel_remove(reg_uid->registry, reg_chan); | |
3216 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
3217 | goto error; | |
7972aab2 DG |
3218 | } |
3219 | ||
2721f7ea JG |
3220 | /* |
3221 | * Setup the streams and add it to the session registry. | |
3222 | */ | |
3223 | ret = setup_buffer_reg_channel(reg_uid->registry, | |
3224 | ua_chan, reg_chan, app); | |
3225 | if (ret < 0) { | |
3226 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); | |
3227 | goto error; | |
3228 | } | |
3229 | ||
e098433c JG |
3230 | /* Notify the notification subsystem of the channel's creation. */ |
3231 | pthread_mutex_lock(®_uid->registry->reg.ust->lock); | |
3232 | chan_reg = ust_registry_channel_find(reg_uid->registry->reg.ust, | |
3233 | ua_chan->tracing_channel_id); | |
3234 | assert(chan_reg); | |
3235 | chan_reg->consumer_key = ua_chan->key; | |
3236 | chan_reg = NULL; | |
3237 | pthread_mutex_unlock(®_uid->registry->reg.ust->lock); | |
e9404c27 | 3238 | |
e098433c JG |
3239 | notification_ret = notification_thread_command_add_channel( |
3240 | notification_thread_handle, session->name, | |
ff588497 JR |
3241 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
3242 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
3243 | ua_chan->name, | |
470cc211 | 3244 | ua_chan->key, LTTNG_DOMAIN_UST, |
e098433c JG |
3245 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3246 | if (notification_ret != LTTNG_OK) { | |
3247 | ret = - (int) notification_ret; | |
3248 | ERR("Failed to add channel to notification thread"); | |
3249 | goto error; | |
e9404c27 JG |
3250 | } |
3251 | ||
2721f7ea | 3252 | send_channel: |
66ff8e3f JG |
3253 | /* Send buffers to the application. */ |
3254 | ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan); | |
3255 | if (ret < 0) { | |
3256 | if (ret != -ENOTCONN) { | |
3257 | ERR("Error sending channel to application"); | |
3258 | } | |
3259 | goto error; | |
3260 | } | |
3261 | ||
9730260e | 3262 | error: |
e32d7f27 JG |
3263 | if (session) { |
3264 | session_put(session); | |
3265 | } | |
9730260e DG |
3266 | return ret; |
3267 | } | |
3268 | ||
78f0bacd | 3269 | /* |
7972aab2 DG |
3270 | * Create and send to the application the created buffers with per PID buffers. |
3271 | * | |
fad1ed2f | 3272 | * Called with UST app session lock held. |
71e0a100 | 3273 | * The session list lock and the session's lock must be acquired. |
fad1ed2f | 3274 | * |
7972aab2 | 3275 | * Return 0 on success else a negative value. |
78f0bacd | 3276 | */ |
7972aab2 DG |
3277 | static int create_channel_per_pid(struct ust_app *app, |
3278 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3279 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3280 | { |
8535a6d9 | 3281 | int ret; |
7972aab2 | 3282 | struct ust_registry_session *registry; |
e9404c27 | 3283 | enum lttng_error_code cmd_ret; |
e32d7f27 | 3284 | struct ltt_session *session = NULL; |
e9404c27 JG |
3285 | uint64_t chan_reg_key; |
3286 | struct ust_registry_channel *chan_reg; | |
78f0bacd | 3287 | |
7972aab2 DG |
3288 | assert(app); |
3289 | assert(usess); | |
3290 | assert(ua_sess); | |
3291 | assert(ua_chan); | |
3292 | ||
3293 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
3294 | ||
3295 | rcu_read_lock(); | |
3296 | ||
3297 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 3298 | /* The UST app session lock is held, registry shall not be null. */ |
7972aab2 DG |
3299 | assert(registry); |
3300 | ||
3301 | /* Create and add a new channel registry to session. */ | |
3302 | ret = ust_registry_channel_add(registry, ua_chan->key); | |
78f0bacd | 3303 | if (ret < 0) { |
f14256d6 MD |
3304 | ERR("Error creating the UST channel \"%s\" registry instance", |
3305 | ua_chan->name); | |
78f0bacd DG |
3306 | goto error; |
3307 | } | |
3308 | ||
e098433c JG |
3309 | session = session_find_by_id(ua_sess->tracing_id); |
3310 | assert(session); | |
3311 | ||
3312 | assert(pthread_mutex_trylock(&session->lock)); | |
3313 | assert(session_trylock_list()); | |
3314 | ||
7972aab2 DG |
3315 | /* Create and get channel on the consumer side. */ |
3316 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
e098433c | 3317 | app->bits_per_long, registry, |
d2956687 | 3318 | session->most_recent_chunk_id.value); |
7972aab2 | 3319 | if (ret < 0) { |
f14256d6 MD |
3320 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
3321 | ua_chan->name); | |
5b951542 | 3322 | goto error_remove_from_registry; |
7972aab2 DG |
3323 | } |
3324 | ||
3325 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
3326 | if (ret < 0) { | |
a7169585 MD |
3327 | if (ret != -ENOTCONN) { |
3328 | ERR("Error sending channel to application"); | |
3329 | } | |
5b951542 | 3330 | goto error_remove_from_registry; |
7972aab2 | 3331 | } |
8535a6d9 | 3332 | |
e9404c27 JG |
3333 | chan_reg_key = ua_chan->key; |
3334 | pthread_mutex_lock(®istry->lock); | |
3335 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); | |
3336 | assert(chan_reg); | |
3337 | chan_reg->consumer_key = ua_chan->key; | |
3338 | pthread_mutex_unlock(®istry->lock); | |
3339 | ||
3340 | cmd_ret = notification_thread_command_add_channel( | |
3341 | notification_thread_handle, session->name, | |
ff588497 JR |
3342 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
3343 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
3344 | ua_chan->name, | |
470cc211 | 3345 | ua_chan->key, LTTNG_DOMAIN_UST, |
e9404c27 JG |
3346 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3347 | if (cmd_ret != LTTNG_OK) { | |
3348 | ret = - (int) cmd_ret; | |
3349 | ERR("Failed to add channel to notification thread"); | |
5b951542 | 3350 | goto error_remove_from_registry; |
e9404c27 JG |
3351 | } |
3352 | ||
5b951542 MD |
3353 | error_remove_from_registry: |
3354 | if (ret) { | |
3355 | ust_registry_channel_del_free(registry, ua_chan->key, false); | |
3356 | } | |
78f0bacd | 3357 | error: |
7972aab2 | 3358 | rcu_read_unlock(); |
e32d7f27 JG |
3359 | if (session) { |
3360 | session_put(session); | |
3361 | } | |
78f0bacd DG |
3362 | return ret; |
3363 | } | |
3364 | ||
3365 | /* | |
7972aab2 | 3366 | * From an already allocated ust app channel, create the channel buffers if |
88e3c2f5 | 3367 | * needed and send them to the application. This MUST be called with a RCU read |
7972aab2 DG |
3368 | * side lock acquired. |
3369 | * | |
fad1ed2f JR |
3370 | * Called with UST app session lock held. |
3371 | * | |
a7169585 MD |
3372 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
3373 | * the application exited concurrently. | |
78f0bacd | 3374 | */ |
88e3c2f5 | 3375 | static int ust_app_channel_send(struct ust_app *app, |
7972aab2 DG |
3376 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
3377 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3378 | { |
7972aab2 | 3379 | int ret; |
78f0bacd | 3380 | |
7972aab2 DG |
3381 | assert(app); |
3382 | assert(usess); | |
88e3c2f5 | 3383 | assert(usess->active); |
7972aab2 DG |
3384 | assert(ua_sess); |
3385 | assert(ua_chan); | |
3386 | ||
3387 | /* Handle buffer type before sending the channel to the application. */ | |
3388 | switch (usess->buffer_type) { | |
3389 | case LTTNG_BUFFER_PER_UID: | |
3390 | { | |
3391 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
3392 | if (ret < 0) { | |
3393 | goto error; | |
3394 | } | |
3395 | break; | |
3396 | } | |
3397 | case LTTNG_BUFFER_PER_PID: | |
3398 | { | |
3399 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
3400 | if (ret < 0) { | |
3401 | goto error; | |
3402 | } | |
3403 | break; | |
3404 | } | |
3405 | default: | |
3406 | assert(0); | |
3407 | ret = -EINVAL; | |
78f0bacd DG |
3408 | goto error; |
3409 | } | |
3410 | ||
7972aab2 DG |
3411 | /* Initialize ust objd object using the received handle and add it. */ |
3412 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
3413 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 3414 | |
7972aab2 DG |
3415 | /* If channel is not enabled, disable it on the tracer */ |
3416 | if (!ua_chan->enabled) { | |
3417 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
3418 | if (ret < 0) { | |
3419 | goto error; | |
3420 | } | |
78f0bacd DG |
3421 | } |
3422 | ||
3423 | error: | |
3424 | return ret; | |
3425 | } | |
3426 | ||
284d8f55 | 3427 | /* |
88e3c2f5 | 3428 | * Create UST app channel and return it through ua_chanp if not NULL. |
d0b96690 | 3429 | * |
36b588ed | 3430 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 3431 | * |
88e3c2f5 | 3432 | * Return 0 on success or else a negative value. |
284d8f55 | 3433 | */ |
88e3c2f5 JG |
3434 | static int ust_app_channel_allocate(struct ust_app_session *ua_sess, |
3435 | struct ltt_ust_channel *uchan, | |
7972aab2 | 3436 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
4d710ac2 | 3437 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
3438 | { |
3439 | int ret = 0; | |
bec39940 DG |
3440 | struct lttng_ht_iter iter; |
3441 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
3442 | struct ust_app_channel *ua_chan; |
3443 | ||
3444 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
3445 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
3446 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 3447 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 3448 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 3449 | goto end; |
5b4a0ec0 DG |
3450 | } |
3451 | ||
d0b96690 | 3452 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
3453 | if (ua_chan == NULL) { |
3454 | /* Only malloc can fail here */ | |
4d710ac2 | 3455 | ret = -ENOMEM; |
88e3c2f5 | 3456 | goto error; |
fc34caaa DG |
3457 | } |
3458 | shadow_copy_channel(ua_chan, uchan); | |
3459 | ||
ffe60014 DG |
3460 | /* Set channel type. */ |
3461 | ua_chan->attr.type = type; | |
3462 | ||
d0b96690 DG |
3463 | /* Only add the channel if successful on the tracer side. */ |
3464 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
fc34caaa | 3465 | end: |
4d710ac2 DG |
3466 | if (ua_chanp) { |
3467 | *ua_chanp = ua_chan; | |
3468 | } | |
3469 | ||
3470 | /* Everything went well. */ | |
3471 | return 0; | |
5b4a0ec0 DG |
3472 | |
3473 | error: | |
4d710ac2 | 3474 | return ret; |
5b4a0ec0 DG |
3475 | } |
3476 | ||
3477 | /* | |
3478 | * Create UST app event and create it on the tracer side. | |
d0b96690 | 3479 | * |
993578ff | 3480 | * Must be called with the RCU read side lock held. |
d0b96690 | 3481 | * Called with ust app session mutex held. |
5b4a0ec0 | 3482 | */ |
edb67388 DG |
3483 | static |
3484 | int create_ust_app_event(struct ust_app_session *ua_sess, | |
3485 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, | |
3486 | struct ust_app *app) | |
284d8f55 | 3487 | { |
edb67388 | 3488 | int ret = 0; |
5b4a0ec0 | 3489 | struct ust_app_event *ua_event; |
284d8f55 | 3490 | |
edb67388 DG |
3491 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
3492 | if (ua_event == NULL) { | |
20533947 | 3493 | /* Only failure mode of alloc_ust_app_event(). */ |
edb67388 | 3494 | ret = -ENOMEM; |
fc34caaa | 3495 | goto end; |
5b4a0ec0 | 3496 | } |
edb67388 | 3497 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 3498 | |
edb67388 | 3499 | /* Create it on the tracer side */ |
5b4a0ec0 | 3500 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 3501 | if (ret < 0) { |
e9f11505 JG |
3502 | /* |
3503 | * Not found previously means that it does not exist on the | |
3504 | * tracer. If the application reports that the event existed, | |
3505 | * it means there is a bug in the sessiond or lttng-ust | |
3506 | * (or corruption, etc.) | |
3507 | */ | |
3508 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3509 | ERR("Tracer for application reported that an event being created already existed: " | |
3510 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
3511 | uevent->attr.name, | |
3512 | app->pid, app->ppid, app->uid, | |
3513 | app->gid); | |
3514 | } | |
284d8f55 DG |
3515 | goto error; |
3516 | } | |
3517 | ||
d0b96690 | 3518 | add_unique_ust_app_event(ua_chan, ua_event); |
284d8f55 | 3519 | |
993578ff JR |
3520 | DBG2("UST app create event completed: app = '%s' (ppid: %d)", |
3521 | app->name, app->ppid); | |
7f79d3a1 | 3522 | |
edb67388 | 3523 | end: |
fc34caaa DG |
3524 | return ret; |
3525 | ||
5b4a0ec0 | 3526 | error: |
fc34caaa | 3527 | /* Valid. Calling here is already in a read side lock */ |
fb45065e | 3528 | delete_ust_app_event(-1, ua_event, app); |
edb67388 | 3529 | return ret; |
5b4a0ec0 DG |
3530 | } |
3531 | ||
993578ff JR |
3532 | /* |
3533 | * Create UST app event notifier rule and create it on the tracer side. | |
3534 | * | |
3535 | * Must be called with the RCU read side lock held. | |
3536 | * Called with ust app session mutex held. | |
3537 | */ | |
3538 | static | |
267d66aa JR |
3539 | int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, |
3540 | struct ust_app *app) | |
993578ff JR |
3541 | { |
3542 | int ret = 0; | |
3543 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
3544 | ||
267d66aa | 3545 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
993578ff JR |
3546 | if (ua_event_notifier_rule == NULL) { |
3547 | ret = -ENOMEM; | |
3548 | goto end; | |
3549 | } | |
3550 | ||
3551 | /* Create it on the tracer side. */ | |
3552 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); | |
3553 | if (ret < 0) { | |
3554 | /* | |
3555 | * Not found previously means that it does not exist on the | |
3556 | * tracer. If the application reports that the event existed, | |
3557 | * it means there is a bug in the sessiond or lttng-ust | |
3558 | * (or corruption, etc.) | |
3559 | */ | |
3560 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3561 | ERR("Tracer for application reported that an event notifier being created already exists: " | |
3562 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
267d66aa | 3563 | lttng_trigger_get_tracer_token(trigger), |
993578ff JR |
3564 | app->pid, app->ppid, app->uid, |
3565 | app->gid); | |
3566 | } | |
3567 | goto error; | |
3568 | } | |
3569 | ||
3570 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, | |
3571 | &ua_event_notifier_rule->node); | |
3572 | ||
3573 | DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64, | |
267d66aa | 3574 | app->name, app->ppid, lttng_trigger_get_tracer_token(trigger)); |
993578ff JR |
3575 | |
3576 | end: | |
3577 | return ret; | |
3578 | ||
3579 | error: | |
3580 | /* The RCU read side lock is already being held by the caller. */ | |
3581 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); | |
3582 | return ret; | |
3583 | } | |
3584 | ||
5b4a0ec0 DG |
3585 | /* |
3586 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 3587 | * |
7972aab2 | 3588 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
3589 | */ |
3590 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 3591 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
3592 | { |
3593 | int ret = 0; | |
ffe60014 | 3594 | struct ust_app_channel *metadata; |
d88aee68 | 3595 | struct consumer_socket *socket; |
7972aab2 | 3596 | struct ust_registry_session *registry; |
e32d7f27 | 3597 | struct ltt_session *session = NULL; |
5b4a0ec0 | 3598 | |
ffe60014 DG |
3599 | assert(ua_sess); |
3600 | assert(app); | |
d88aee68 | 3601 | assert(consumer); |
5b4a0ec0 | 3602 | |
7972aab2 | 3603 | registry = get_session_registry(ua_sess); |
fad1ed2f | 3604 | /* The UST app session is held registry shall not be null. */ |
7972aab2 DG |
3605 | assert(registry); |
3606 | ||
ce34fcd0 MD |
3607 | pthread_mutex_lock(®istry->lock); |
3608 | ||
1b532a60 DG |
3609 | /* Metadata already exists for this registry or it was closed previously */ |
3610 | if (registry->metadata_key || registry->metadata_closed) { | |
7972aab2 DG |
3611 | ret = 0; |
3612 | goto error; | |
5b4a0ec0 DG |
3613 | } |
3614 | ||
ffe60014 | 3615 | /* Allocate UST metadata */ |
d0b96690 | 3616 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
3617 | if (!metadata) { |
3618 | /* malloc() failed */ | |
3619 | ret = -ENOMEM; | |
3620 | goto error; | |
3621 | } | |
5b4a0ec0 | 3622 | |
ad7a9107 | 3623 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 3624 | |
7972aab2 DG |
3625 | /* Need one fd for the channel. */ |
3626 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3627 | if (ret < 0) { | |
3628 | ERR("Exhausted number of available FD upon create metadata"); | |
3629 | goto error; | |
3630 | } | |
3631 | ||
4dc3dfc5 DG |
3632 | /* Get the right consumer socket for the application. */ |
3633 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); | |
3634 | if (!socket) { | |
3635 | ret = -EINVAL; | |
3636 | goto error_consumer; | |
3637 | } | |
3638 | ||
331744e3 JD |
3639 | /* |
3640 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
3641 | * to the registry *before* we ask the consumer so we avoid the race of the | |
3642 | * consumer requesting the metadata and the ask_channel call on our side | |
3643 | * did not returned yet. | |
3644 | */ | |
3645 | registry->metadata_key = metadata->key; | |
3646 | ||
e098433c JG |
3647 | session = session_find_by_id(ua_sess->tracing_id); |
3648 | assert(session); | |
3649 | ||
3650 | assert(pthread_mutex_trylock(&session->lock)); | |
3651 | assert(session_trylock_list()); | |
3652 | ||
d88aee68 DG |
3653 | /* |
3654 | * Ask the metadata channel creation to the consumer. The metadata object | |
3655 | * will be created by the consumer and kept their. However, the stream is | |
3656 | * never added or monitored until we do a first push metadata to the | |
3657 | * consumer. | |
3658 | */ | |
7972aab2 | 3659 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
d2956687 | 3660 | registry, session->current_trace_chunk); |
d88aee68 | 3661 | if (ret < 0) { |
f2a444f1 DG |
3662 | /* Nullify the metadata key so we don't try to close it later on. */ |
3663 | registry->metadata_key = 0; | |
d88aee68 DG |
3664 | goto error_consumer; |
3665 | } | |
3666 | ||
3667 | /* | |
3668 | * The setup command will make the metadata stream be sent to the relayd, | |
3669 | * if applicable, and the thread managing the metadatas. This is important | |
3670 | * because after this point, if an error occurs, the only way the stream | |
3671 | * can be deleted is to be monitored in the consumer. | |
3672 | */ | |
7972aab2 | 3673 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 3674 | if (ret < 0) { |
f2a444f1 DG |
3675 | /* Nullify the metadata key so we don't try to close it later on. */ |
3676 | registry->metadata_key = 0; | |
d88aee68 | 3677 | goto error_consumer; |
5b4a0ec0 DG |
3678 | } |
3679 | ||
7972aab2 DG |
3680 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
3681 | metadata->key, app->pid); | |
5b4a0ec0 | 3682 | |
d88aee68 | 3683 | error_consumer: |
b80f0b6c | 3684 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 3685 | delete_ust_app_channel(-1, metadata, app); |
5b4a0ec0 | 3686 | error: |
ce34fcd0 | 3687 | pthread_mutex_unlock(®istry->lock); |
e32d7f27 JG |
3688 | if (session) { |
3689 | session_put(session); | |
3690 | } | |
ffe60014 | 3691 | return ret; |
5b4a0ec0 DG |
3692 | } |
3693 | ||
5b4a0ec0 | 3694 | /* |
d88aee68 DG |
3695 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
3696 | * acquired before calling this function. | |
5b4a0ec0 DG |
3697 | */ |
3698 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
3699 | { | |
d88aee68 | 3700 | struct ust_app *app = NULL; |
bec39940 DG |
3701 | struct lttng_ht_node_ulong *node; |
3702 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 3703 | |
bec39940 DG |
3704 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
3705 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
3706 | if (node == NULL) { |
3707 | DBG2("UST app no found with pid %d", pid); | |
3708 | goto error; | |
3709 | } | |
5b4a0ec0 DG |
3710 | |
3711 | DBG2("Found UST app by pid %d", pid); | |
3712 | ||
d88aee68 | 3713 | app = caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
3714 | |
3715 | error: | |
d88aee68 | 3716 | return app; |
5b4a0ec0 DG |
3717 | } |
3718 | ||
d88aee68 DG |
3719 | /* |
3720 | * Allocate and init an UST app object using the registration information and | |
3721 | * the command socket. This is called when the command socket connects to the | |
3722 | * session daemon. | |
3723 | * | |
3724 | * The object is returned on success or else NULL. | |
3725 | */ | |
d0b96690 | 3726 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 3727 | { |
5e2abfaf | 3728 | int ret; |
d0b96690 | 3729 | struct ust_app *lta = NULL; |
da873412 | 3730 | struct lttng_pipe *event_notifier_event_source_pipe = NULL; |
d0b96690 DG |
3731 | |
3732 | assert(msg); | |
3733 | assert(sock >= 0); | |
3734 | ||
3735 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 3736 | |
173af62f DG |
3737 | if ((msg->bits_per_long == 64 && |
3738 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
3739 | || (msg->bits_per_long == 32 && | |
3740 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 3741 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
3742 | "%d-bit long, but no consumerd for this size is available.\n", |
3743 | msg->name, msg->pid, msg->bits_per_long); | |
3744 | goto error; | |
3f2c5fcc | 3745 | } |
d0b96690 | 3746 | |
5e2abfaf JG |
3747 | /* |
3748 | * Reserve the two file descriptors of the event source pipe. The write | |
3749 | * end will be closed once it is passed to the application, at which | |
3750 | * point a single 'put' will be performed. | |
3751 | */ | |
3752 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
3753 | if (ret) { | |
3754 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)", | |
3755 | msg->name, (int) msg->ppid); | |
3756 | goto error; | |
3757 | } | |
3758 | ||
da873412 JR |
3759 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
3760 | if (!event_notifier_event_source_pipe) { | |
3761 | PERROR("Failed to open application event source pipe: '%s' (ppid = %d)", | |
3762 | msg->name, msg->ppid); | |
3763 | goto error; | |
3764 | } | |
3765 | ||
5b4a0ec0 DG |
3766 | lta = zmalloc(sizeof(struct ust_app)); |
3767 | if (lta == NULL) { | |
3768 | PERROR("malloc"); | |
da873412 | 3769 | goto error_free_pipe; |
5b4a0ec0 DG |
3770 | } |
3771 | ||
da873412 JR |
3772 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
3773 | ||
5b4a0ec0 DG |
3774 | lta->ppid = msg->ppid; |
3775 | lta->uid = msg->uid; | |
3776 | lta->gid = msg->gid; | |
d0b96690 | 3777 | |
7753dea8 | 3778 | lta->bits_per_long = msg->bits_per_long; |
d0b96690 DG |
3779 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
3780 | lta->uint16_t_alignment = msg->uint16_t_alignment; | |
3781 | lta->uint32_t_alignment = msg->uint32_t_alignment; | |
3782 | lta->uint64_t_alignment = msg->uint64_t_alignment; | |
3783 | lta->long_alignment = msg->long_alignment; | |
3784 | lta->byte_order = msg->byte_order; | |
3785 | ||
5b4a0ec0 DG |
3786 | lta->v_major = msg->major; |
3787 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 3788 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d0b96690 | 3789 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
10b56aef | 3790 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
d0b96690 | 3791 | lta->notify_sock = -1; |
993578ff | 3792 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d88aee68 DG |
3793 | |
3794 | /* Copy name and make sure it's NULL terminated. */ | |
3795 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
3796 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
3797 | ||
3798 | /* | |
3799 | * Before this can be called, when receiving the registration information, | |
3800 | * the application compatibility is checked. So, at this point, the | |
3801 | * application can work with this session daemon. | |
3802 | */ | |
d0b96690 | 3803 | lta->compatible = 1; |
5b4a0ec0 | 3804 | |
852d0037 | 3805 | lta->pid = msg->pid; |
d0b96690 | 3806 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 3807 | lta->sock = sock; |
fb45065e | 3808 | pthread_mutex_init(<a->sock_lock, NULL); |
d0b96690 | 3809 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 3810 | |
d42f20df | 3811 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
d0b96690 | 3812 | return lta; |
da873412 JR |
3813 | |
3814 | error_free_pipe: | |
3815 | lttng_pipe_destroy(event_notifier_event_source_pipe); | |
5e2abfaf | 3816 | lttng_fd_put(LTTNG_FD_APPS, 2); |
da873412 JR |
3817 | error: |
3818 | return NULL; | |
d0b96690 DG |
3819 | } |
3820 | ||
d88aee68 DG |
3821 | /* |
3822 | * For a given application object, add it to every hash table. | |
3823 | */ | |
d0b96690 DG |
3824 | void ust_app_add(struct ust_app *app) |
3825 | { | |
3826 | assert(app); | |
3827 | assert(app->notify_sock >= 0); | |
3828 | ||
940c4592 JR |
3829 | app->registration_time = time(NULL); |
3830 | ||
5b4a0ec0 | 3831 | rcu_read_lock(); |
852d0037 DG |
3832 | |
3833 | /* | |
3834 | * On a re-registration, we want to kick out the previous registration of | |
3835 | * that pid | |
3836 | */ | |
d0b96690 | 3837 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
3838 | |
3839 | /* | |
3840 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
3841 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
3842 | * already in the table. | |
3843 | */ | |
d0b96690 | 3844 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 3845 | |
d0b96690 DG |
3846 | /* Add application to the notify socket hash table. */ |
3847 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
3848 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 3849 | |
d0b96690 | 3850 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
d88aee68 DG |
3851 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
3852 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, | |
3853 | app->v_minor); | |
5b4a0ec0 | 3854 | |
d0b96690 DG |
3855 | rcu_read_unlock(); |
3856 | } | |
3857 | ||
d88aee68 DG |
3858 | /* |
3859 | * Set the application version into the object. | |
3860 | * | |
3861 | * Return 0 on success else a negative value either an errno code or a | |
3862 | * LTTng-UST error code. | |
3863 | */ | |
d0b96690 DG |
3864 | int ust_app_version(struct ust_app *app) |
3865 | { | |
d88aee68 DG |
3866 | int ret; |
3867 | ||
d0b96690 | 3868 | assert(app); |
d88aee68 | 3869 | |
fb45065e | 3870 | pthread_mutex_lock(&app->sock_lock); |
d88aee68 | 3871 | ret = ustctl_tracer_version(app->sock, &app->version); |
fb45065e | 3872 | pthread_mutex_unlock(&app->sock_lock); |
d88aee68 DG |
3873 | if (ret < 0) { |
3874 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
5368d366 | 3875 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
d88aee68 | 3876 | } else { |
5368d366 | 3877 | DBG3("UST app %d version failed. Application is dead", app->sock); |
d88aee68 DG |
3878 | } |
3879 | } | |
3880 | ||
3881 | return ret; | |
5b4a0ec0 DG |
3882 | } |
3883 | ||
da873412 JR |
3884 | /* |
3885 | * Setup the base event notifier group. | |
3886 | * | |
3887 | * Return 0 on success else a negative value either an errno code or a | |
3888 | * LTTng-UST error code. | |
3889 | */ | |
3890 | int ust_app_setup_event_notifier_group(struct ust_app *app) | |
3891 | { | |
3892 | int ret; | |
3893 | int event_pipe_write_fd; | |
3894 | struct lttng_ust_object_data *event_notifier_group = NULL; | |
3895 | enum lttng_error_code lttng_ret; | |
3896 | ||
3897 | assert(app); | |
3898 | ||
3899 | /* Get the write side of the pipe. */ | |
3900 | event_pipe_write_fd = lttng_pipe_get_writefd( | |
3901 | app->event_notifier_group.event_pipe); | |
3902 | ||
3903 | pthread_mutex_lock(&app->sock_lock); | |
3904 | ret = ustctl_create_event_notifier_group(app->sock, | |
3905 | event_pipe_write_fd, &event_notifier_group); | |
3906 | pthread_mutex_unlock(&app->sock_lock); | |
3907 | if (ret < 0) { | |
3908 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
3909 | ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d", | |
3910 | ret, app->sock, event_pipe_write_fd); | |
3911 | } else { | |
3912 | DBG("Failed to create application event notifier group (application is dead): app socket fd = %d", | |
3913 | app->sock); | |
3914 | } | |
3915 | ||
3916 | goto error; | |
3917 | } | |
3918 | ||
5d4193fd JG |
3919 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
3920 | if (ret) { | |
3921 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)", | |
3922 | app->name, app->ppid); | |
3923 | goto error; | |
3924 | } | |
3925 | ||
5e2abfaf JG |
3926 | /* |
3927 | * Release the file descriptor that was reserved for the write-end of | |
3928 | * the pipe. | |
3929 | */ | |
3930 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3931 | ||
da873412 JR |
3932 | lttng_ret = notification_thread_command_add_tracer_event_source( |
3933 | notification_thread_handle, | |
3934 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe), | |
3935 | LTTNG_DOMAIN_UST); | |
3936 | if (lttng_ret != LTTNG_OK) { | |
3937 | ERR("Failed to add tracer event source to notification thread"); | |
3938 | ret = - 1; | |
3939 | goto error; | |
3940 | } | |
3941 | ||
3942 | /* Assign handle only when the complete setup is valid. */ | |
3943 | app->event_notifier_group.object = event_notifier_group; | |
3944 | return ret; | |
3945 | ||
3946 | error: | |
3947 | ustctl_release_object(app->sock, app->event_notifier_group.object); | |
3948 | free(app->event_notifier_group.object); | |
3949 | return ret; | |
3950 | } | |
3951 | ||
5b4a0ec0 DG |
3952 | /* |
3953 | * Unregister app by removing it from the global traceable app list and freeing | |
3954 | * the data struct. | |
3955 | * | |
3956 | * The socket is already closed at this point so no close to sock. | |
3957 | */ | |
3958 | void ust_app_unregister(int sock) | |
3959 | { | |
3960 | struct ust_app *lta; | |
bec39940 | 3961 | struct lttng_ht_node_ulong *node; |
c4b88406 | 3962 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 3963 | struct lttng_ht_iter iter; |
d42f20df | 3964 | struct ust_app_session *ua_sess; |
525b0740 | 3965 | int ret; |
5b4a0ec0 DG |
3966 | |
3967 | rcu_read_lock(); | |
886459c6 | 3968 | |
5b4a0ec0 | 3969 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
3970 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
3971 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
d0b96690 | 3972 | assert(node); |
284d8f55 | 3973 | |
852d0037 | 3974 | lta = caa_container_of(node, struct ust_app, sock_n); |
852d0037 DG |
3975 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
3976 | ||
d88aee68 | 3977 | /* |
ce34fcd0 MD |
3978 | * For per-PID buffers, perform "push metadata" and flush all |
3979 | * application streams before removing app from hash tables, | |
3980 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 3981 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 3982 | */ |
d42f20df DG |
3983 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
3984 | node.node) { | |
7972aab2 DG |
3985 | struct ust_registry_session *registry; |
3986 | ||
d42f20df DG |
3987 | ret = lttng_ht_del(lta->sessions, &iter); |
3988 | if (ret) { | |
3989 | /* The session was already removed so scheduled for teardown. */ | |
3990 | continue; | |
3991 | } | |
3992 | ||
ce34fcd0 MD |
3993 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
3994 | (void) ust_app_flush_app_session(lta, ua_sess); | |
3995 | } | |
c4b88406 | 3996 | |
d42f20df DG |
3997 | /* |
3998 | * Add session to list for teardown. This is safe since at this point we | |
3999 | * are the only one using this list. | |
4000 | */ | |
d88aee68 DG |
4001 | pthread_mutex_lock(&ua_sess->lock); |
4002 | ||
b161602a MD |
4003 | if (ua_sess->deleted) { |
4004 | pthread_mutex_unlock(&ua_sess->lock); | |
4005 | continue; | |
4006 | } | |
4007 | ||
d88aee68 DG |
4008 | /* |
4009 | * Normally, this is done in the delete session process which is | |
4010 | * executed in the call rcu below. However, upon registration we can't | |
4011 | * afford to wait for the grace period before pushing data or else the | |
4012 | * data pending feature can race between the unregistration and stop | |
4013 | * command where the data pending command is sent *before* the grace | |
4014 | * period ended. | |
4015 | * | |
4016 | * The close metadata below nullifies the metadata pointer in the | |
4017 | * session so the delete session will NOT push/close a second time. | |
4018 | */ | |
7972aab2 | 4019 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 4020 | if (registry) { |
7972aab2 DG |
4021 | /* Push metadata for application before freeing the application. */ |
4022 | (void) push_metadata(registry, ua_sess->consumer); | |
4023 | ||
4024 | /* | |
4025 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
4026 | * metadata only on destroy trace session in this case. Also, the |
4027 | * previous push metadata could have flag the metadata registry to | |
4028 | * close so don't send a close command if closed. | |
7972aab2 | 4029 | */ |
ce34fcd0 | 4030 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
4031 | /* And ask to close it for this session registry. */ |
4032 | (void) close_metadata(registry, ua_sess->consumer); | |
4033 | } | |
4034 | } | |
d42f20df | 4035 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
c4b88406 | 4036 | |
d88aee68 | 4037 | pthread_mutex_unlock(&ua_sess->lock); |
d42f20df DG |
4038 | } |
4039 | ||
c4b88406 MD |
4040 | /* Remove application from PID hash table */ |
4041 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); | |
4042 | assert(!ret); | |
4043 | ||
4044 | /* | |
4045 | * Remove application from notify hash table. The thread handling the | |
4046 | * notify socket could have deleted the node so ignore on error because | |
c48239ca JG |
4047 | * either way it's valid. The close of that socket is handled by the |
4048 | * apps_notify_thread. | |
c4b88406 MD |
4049 | */ |
4050 | iter.iter.node = <a->notify_sock_n.node; | |
4051 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
4052 | ||
4053 | /* | |
4054 | * Ignore return value since the node might have been removed before by an | |
4055 | * add replace during app registration because the PID can be reassigned by | |
4056 | * the OS. | |
4057 | */ | |
4058 | iter.iter.node = <a->pid_n.node; | |
4059 | ret = lttng_ht_del(ust_app_ht, &iter); | |
4060 | if (ret) { | |
4061 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
4062 | lta->pid); | |
4063 | } | |
4064 | ||
852d0037 DG |
4065 | /* Free memory */ |
4066 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
4067 | ||
5b4a0ec0 DG |
4068 | rcu_read_unlock(); |
4069 | return; | |
284d8f55 DG |
4070 | } |
4071 | ||
5b4a0ec0 DG |
4072 | /* |
4073 | * Fill events array with all events name of all registered apps. | |
4074 | */ | |
4075 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 4076 | { |
5b4a0ec0 DG |
4077 | int ret, handle; |
4078 | size_t nbmem, count = 0; | |
bec39940 | 4079 | struct lttng_ht_iter iter; |
5b4a0ec0 | 4080 | struct ust_app *app; |
c617c0c6 | 4081 | struct lttng_event *tmp_event; |
421cb601 | 4082 | |
5b4a0ec0 | 4083 | nbmem = UST_APP_EVENT_LIST_SIZE; |
c617c0c6 MD |
4084 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
4085 | if (tmp_event == NULL) { | |
5b4a0ec0 DG |
4086 | PERROR("zmalloc ust app events"); |
4087 | ret = -ENOMEM; | |
421cb601 DG |
4088 | goto error; |
4089 | } | |
4090 | ||
5b4a0ec0 | 4091 | rcu_read_lock(); |
421cb601 | 4092 | |
852d0037 | 4093 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
90eaa0d2 | 4094 | struct lttng_ust_tracepoint_iter uiter; |
ac3bd9c0 | 4095 | |
840cb59c | 4096 | health_code_update(); |
86acf0da | 4097 | |
e0c7ec2b DG |
4098 | if (!app->compatible) { |
4099 | /* | |
4100 | * TODO: In time, we should notice the caller of this error by | |
4101 | * telling him that this is a version error. | |
4102 | */ | |
4103 | continue; | |
4104 | } | |
fb45065e | 4105 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4106 | handle = ustctl_tracepoint_list(app->sock); |
5b4a0ec0 | 4107 | if (handle < 0) { |
ffe60014 DG |
4108 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4109 | ERR("UST app list events getting handle failed for app pid %d", | |
4110 | app->pid); | |
4111 | } | |
fb45065e | 4112 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 DG |
4113 | continue; |
4114 | } | |
421cb601 | 4115 | |
852d0037 | 4116 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 4117 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4118 | /* Handle ustctl error. */ |
4119 | if (ret < 0) { | |
fb45065e MD |
4120 | int release_ret; |
4121 | ||
a2ba1ab0 | 4122 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4123 | ERR("UST app tp list get failed for app %d with ret %d", |
4124 | app->sock, ret); | |
4125 | } else { | |
4126 | DBG3("UST app tp list get failed. Application is dead"); | |
3757b385 DG |
4127 | /* |
4128 | * This is normal behavior, an application can die during the | |
4129 | * creation process. Don't report an error so the execution can | |
4130 | * continue normally. Continue normal execution. | |
4131 | */ | |
4132 | break; | |
ffe60014 | 4133 | } |
98f595d4 | 4134 | free(tmp_event); |
fb45065e | 4135 | release_ret = ustctl_release_handle(app->sock, handle); |
68313703 JG |
4136 | if (release_ret < 0 && |
4137 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4138 | release_ret != -EPIPE) { | |
fb45065e MD |
4139 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4140 | } | |
4141 | pthread_mutex_unlock(&app->sock_lock); | |
ffe60014 DG |
4142 | goto rcu_error; |
4143 | } | |
4144 | ||
840cb59c | 4145 | health_code_update(); |
815564d8 | 4146 | if (count >= nbmem) { |
d7b3776f | 4147 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4148 | struct lttng_event *new_tmp_event; |
4149 | size_t new_nbmem; | |
4150 | ||
4151 | new_nbmem = nbmem << 1; | |
4152 | DBG2("Reallocating event list from %zu to %zu entries", | |
4153 | nbmem, new_nbmem); | |
4154 | new_tmp_event = realloc(tmp_event, | |
4155 | new_nbmem * sizeof(struct lttng_event)); | |
4156 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4157 | int release_ret; |
4158 | ||
5b4a0ec0 | 4159 | PERROR("realloc ust app events"); |
c617c0c6 | 4160 | free(tmp_event); |
5b4a0ec0 | 4161 | ret = -ENOMEM; |
fb45065e | 4162 | release_ret = ustctl_release_handle(app->sock, handle); |
68313703 JG |
4163 | if (release_ret < 0 && |
4164 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4165 | release_ret != -EPIPE) { | |
fb45065e MD |
4166 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4167 | } | |
4168 | pthread_mutex_unlock(&app->sock_lock); | |
5b4a0ec0 DG |
4169 | goto rcu_error; |
4170 | } | |
53efb85a MD |
4171 | /* Zero the new memory */ |
4172 | memset(new_tmp_event + nbmem, 0, | |
4173 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
4174 | nbmem = new_nbmem; | |
4175 | tmp_event = new_tmp_event; | |
5b4a0ec0 | 4176 | } |
c617c0c6 MD |
4177 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
4178 | tmp_event[count].loglevel = uiter.loglevel; | |
4179 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; | |
4180 | tmp_event[count].pid = app->pid; | |
4181 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 4182 | count++; |
421cb601 | 4183 | } |
fb45065e MD |
4184 | ret = ustctl_release_handle(app->sock, handle); |
4185 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 | 4186 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
fb45065e MD |
4187 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
4188 | } | |
421cb601 DG |
4189 | } |
4190 | ||
5b4a0ec0 | 4191 | ret = count; |
c617c0c6 | 4192 | *events = tmp_event; |
421cb601 | 4193 | |
5b4a0ec0 | 4194 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 4195 | |
5b4a0ec0 DG |
4196 | rcu_error: |
4197 | rcu_read_unlock(); | |
421cb601 | 4198 | error: |
840cb59c | 4199 | health_code_update(); |
5b4a0ec0 | 4200 | return ret; |
421cb601 DG |
4201 | } |
4202 | ||
f37d259d MD |
4203 | /* |
4204 | * Fill events array with all events name of all registered apps. | |
4205 | */ | |
4206 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
4207 | { | |
4208 | int ret, handle; | |
4209 | size_t nbmem, count = 0; | |
4210 | struct lttng_ht_iter iter; | |
4211 | struct ust_app *app; | |
c617c0c6 | 4212 | struct lttng_event_field *tmp_event; |
f37d259d MD |
4213 | |
4214 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
c617c0c6 MD |
4215 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
4216 | if (tmp_event == NULL) { | |
f37d259d MD |
4217 | PERROR("zmalloc ust app event fields"); |
4218 | ret = -ENOMEM; | |
4219 | goto error; | |
4220 | } | |
4221 | ||
4222 | rcu_read_lock(); | |
4223 | ||
4224 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4225 | struct lttng_ust_field_iter uiter; | |
4226 | ||
840cb59c | 4227 | health_code_update(); |
86acf0da | 4228 | |
f37d259d MD |
4229 | if (!app->compatible) { |
4230 | /* | |
4231 | * TODO: In time, we should notice the caller of this error by | |
4232 | * telling him that this is a version error. | |
4233 | */ | |
4234 | continue; | |
4235 | } | |
fb45065e | 4236 | pthread_mutex_lock(&app->sock_lock); |
f37d259d MD |
4237 | handle = ustctl_tracepoint_field_list(app->sock); |
4238 | if (handle < 0) { | |
ffe60014 DG |
4239 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4240 | ERR("UST app list field getting handle failed for app pid %d", | |
4241 | app->pid); | |
4242 | } | |
fb45065e | 4243 | pthread_mutex_unlock(&app->sock_lock); |
f37d259d MD |
4244 | continue; |
4245 | } | |
4246 | ||
4247 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, | |
fb54cdbf | 4248 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4249 | /* Handle ustctl error. */ |
4250 | if (ret < 0) { | |
fb45065e MD |
4251 | int release_ret; |
4252 | ||
a2ba1ab0 | 4253 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4254 | ERR("UST app tp list field failed for app %d with ret %d", |
4255 | app->sock, ret); | |
4256 | } else { | |
4257 | DBG3("UST app tp list field failed. Application is dead"); | |
3757b385 DG |
4258 | /* |
4259 | * This is normal behavior, an application can die during the | |
4260 | * creation process. Don't report an error so the execution can | |
98f595d4 | 4261 | * continue normally. Reset list and count for next app. |
3757b385 DG |
4262 | */ |
4263 | break; | |
ffe60014 | 4264 | } |
98f595d4 | 4265 | free(tmp_event); |
fb45065e MD |
4266 | release_ret = ustctl_release_handle(app->sock, handle); |
4267 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
4268 | if (release_ret < 0 && |
4269 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4270 | release_ret != -EPIPE) { | |
fb45065e MD |
4271 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4272 | } | |
ffe60014 DG |
4273 | goto rcu_error; |
4274 | } | |
4275 | ||
840cb59c | 4276 | health_code_update(); |
f37d259d | 4277 | if (count >= nbmem) { |
d7b3776f | 4278 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4279 | struct lttng_event_field *new_tmp_event; |
4280 | size_t new_nbmem; | |
4281 | ||
4282 | new_nbmem = nbmem << 1; | |
4283 | DBG2("Reallocating event field list from %zu to %zu entries", | |
4284 | nbmem, new_nbmem); | |
4285 | new_tmp_event = realloc(tmp_event, | |
4286 | new_nbmem * sizeof(struct lttng_event_field)); | |
4287 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4288 | int release_ret; |
4289 | ||
f37d259d | 4290 | PERROR("realloc ust app event fields"); |
c617c0c6 | 4291 | free(tmp_event); |
f37d259d | 4292 | ret = -ENOMEM; |
fb45065e MD |
4293 | release_ret = ustctl_release_handle(app->sock, handle); |
4294 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
4295 | if (release_ret && |
4296 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4297 | release_ret != -EPIPE) { | |
fb45065e MD |
4298 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4299 | } | |
f37d259d MD |
4300 | goto rcu_error; |
4301 | } | |
53efb85a MD |
4302 | /* Zero the new memory */ |
4303 | memset(new_tmp_event + nbmem, 0, | |
4304 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); | |
4305 | nbmem = new_nbmem; | |
4306 | tmp_event = new_tmp_event; | |
f37d259d | 4307 | } |
f37d259d | 4308 | |
c617c0c6 | 4309 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
2e84128e DG |
4310 | /* Mapping between these enums matches 1 to 1. */ |
4311 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; | |
c617c0c6 | 4312 | tmp_event[count].nowrite = uiter.nowrite; |
f37d259d | 4313 | |
c617c0c6 MD |
4314 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
4315 | tmp_event[count].event.loglevel = uiter.loglevel; | |
2e84128e | 4316 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
c617c0c6 MD |
4317 | tmp_event[count].event.pid = app->pid; |
4318 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
4319 | count++; |
4320 | } | |
fb45065e MD |
4321 | ret = ustctl_release_handle(app->sock, handle); |
4322 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
4323 | if (ret < 0 && |
4324 | ret != -LTTNG_UST_ERR_EXITING && | |
4325 | ret != -EPIPE) { | |
fb45065e MD |
4326 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
4327 | } | |
f37d259d MD |
4328 | } |
4329 | ||
4330 | ret = count; | |
c617c0c6 | 4331 | *fields = tmp_event; |
f37d259d MD |
4332 | |
4333 | DBG2("UST app list event fields done (%zu events)", count); | |
4334 | ||
4335 | rcu_error: | |
4336 | rcu_read_unlock(); | |
4337 | error: | |
840cb59c | 4338 | health_code_update(); |
f37d259d MD |
4339 | return ret; |
4340 | } | |
4341 | ||
5b4a0ec0 DG |
4342 | /* |
4343 | * Free and clean all traceable apps of the global list. | |
36b588ed MD |
4344 | * |
4345 | * Should _NOT_ be called with RCU read-side lock held. | |
5b4a0ec0 DG |
4346 | */ |
4347 | void ust_app_clean_list(void) | |
421cb601 | 4348 | { |
5b4a0ec0 | 4349 | int ret; |
659ed79f | 4350 | struct ust_app *app; |
bec39940 | 4351 | struct lttng_ht_iter iter; |
421cb601 | 4352 | |
5b4a0ec0 | 4353 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 4354 | |
5b4a0ec0 | 4355 | rcu_read_lock(); |
421cb601 | 4356 | |
faadaa3a JG |
4357 | /* Cleanup notify socket hash table */ |
4358 | if (ust_app_ht_by_notify_sock) { | |
4359 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, | |
4360 | notify_sock_n.node) { | |
b69a1b40 JG |
4361 | /* |
4362 | * Assert that all notifiers are gone as all triggers | |
4363 | * are unregistered prior to this clean-up. | |
4364 | */ | |
4365 | assert(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); | |
faadaa3a | 4366 | |
faadaa3a JG |
4367 | ust_app_notify_sock_unregister(app->notify_sock); |
4368 | } | |
4369 | } | |
4370 | ||
f1b711c4 MD |
4371 | if (ust_app_ht) { |
4372 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4373 | ret = lttng_ht_del(ust_app_ht, &iter); | |
4374 | assert(!ret); | |
4375 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
4376 | } | |
421cb601 DG |
4377 | } |
4378 | ||
852d0037 | 4379 | /* Cleanup socket hash table */ |
f1b711c4 MD |
4380 | if (ust_app_ht_by_sock) { |
4381 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, | |
4382 | sock_n.node) { | |
4383 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); | |
4384 | assert(!ret); | |
4385 | } | |
bec39940 | 4386 | } |
852d0037 | 4387 | |
36b588ed | 4388 | rcu_read_unlock(); |
d88aee68 | 4389 | |
bec39940 | 4390 | /* Destroy is done only when the ht is empty */ |
f1b711c4 MD |
4391 | if (ust_app_ht) { |
4392 | ht_cleanup_push(ust_app_ht); | |
4393 | } | |
4394 | if (ust_app_ht_by_sock) { | |
4395 | ht_cleanup_push(ust_app_ht_by_sock); | |
4396 | } | |
4397 | if (ust_app_ht_by_notify_sock) { | |
4398 | ht_cleanup_push(ust_app_ht_by_notify_sock); | |
4399 | } | |
5b4a0ec0 DG |
4400 | } |
4401 | ||
4402 | /* | |
4403 | * Init UST app hash table. | |
4404 | */ | |
57703f6e | 4405 | int ust_app_ht_alloc(void) |
5b4a0ec0 | 4406 | { |
bec39940 | 4407 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4408 | if (!ust_app_ht) { |
4409 | return -1; | |
4410 | } | |
852d0037 | 4411 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4412 | if (!ust_app_ht_by_sock) { |
4413 | return -1; | |
4414 | } | |
d0b96690 | 4415 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4416 | if (!ust_app_ht_by_notify_sock) { |
4417 | return -1; | |
4418 | } | |
4419 | return 0; | |
421cb601 DG |
4420 | } |
4421 | ||
78f0bacd DG |
4422 | /* |
4423 | * For a specific UST session, disable the channel for all registered apps. | |
4424 | */ | |
35a9059d | 4425 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4426 | struct ltt_ust_channel *uchan) |
4427 | { | |
4428 | int ret = 0; | |
bec39940 DG |
4429 | struct lttng_ht_iter iter; |
4430 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
4431 | struct ust_app *app; |
4432 | struct ust_app_session *ua_sess; | |
8535a6d9 | 4433 | struct ust_app_channel *ua_chan; |
78f0bacd | 4434 | |
88e3c2f5 | 4435 | assert(usess->active); |
d9bf3ca4 | 4436 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
a991f516 | 4437 | uchan->name, usess->id); |
78f0bacd DG |
4438 | |
4439 | rcu_read_lock(); | |
4440 | ||
4441 | /* For every registered applications */ | |
852d0037 | 4442 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 4443 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
4444 | if (!app->compatible) { |
4445 | /* | |
4446 | * TODO: In time, we should notice the caller of this error by | |
4447 | * telling him that this is a version error. | |
4448 | */ | |
4449 | continue; | |
4450 | } | |
78f0bacd DG |
4451 | ua_sess = lookup_session_by_app(usess, app); |
4452 | if (ua_sess == NULL) { | |
4453 | continue; | |
4454 | } | |
4455 | ||
8535a6d9 | 4456 | /* Get channel */ |
bec39940 DG |
4457 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4458 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 DG |
4459 | /* If the session if found for the app, the channel must be there */ |
4460 | assert(ua_chan_node); | |
4461 | ||
4462 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4463 | /* The channel must not be already disabled */ | |
4464 | assert(ua_chan->enabled == 1); | |
4465 | ||
4466 | /* Disable channel onto application */ | |
4467 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
4468 | if (ret < 0) { |
4469 | /* XXX: We might want to report this error at some point... */ | |
4470 | continue; | |
4471 | } | |
4472 | } | |
4473 | ||
4474 | rcu_read_unlock(); | |
78f0bacd DG |
4475 | return ret; |
4476 | } | |
4477 | ||
4478 | /* | |
4479 | * For a specific UST session, enable the channel for all registered apps. | |
4480 | */ | |
35a9059d | 4481 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4482 | struct ltt_ust_channel *uchan) |
4483 | { | |
4484 | int ret = 0; | |
bec39940 | 4485 | struct lttng_ht_iter iter; |
78f0bacd DG |
4486 | struct ust_app *app; |
4487 | struct ust_app_session *ua_sess; | |
4488 | ||
88e3c2f5 | 4489 | assert(usess->active); |
d9bf3ca4 | 4490 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
a991f516 | 4491 | uchan->name, usess->id); |
78f0bacd DG |
4492 | |
4493 | rcu_read_lock(); | |
4494 | ||
4495 | /* For every registered applications */ | |
852d0037 | 4496 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4497 | if (!app->compatible) { |
4498 | /* | |
4499 | * TODO: In time, we should notice the caller of this error by | |
4500 | * telling him that this is a version error. | |
4501 | */ | |
4502 | continue; | |
4503 | } | |
78f0bacd DG |
4504 | ua_sess = lookup_session_by_app(usess, app); |
4505 | if (ua_sess == NULL) { | |
4506 | continue; | |
4507 | } | |
4508 | ||
4509 | /* Enable channel onto application */ | |
4510 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
4511 | if (ret < 0) { | |
4512 | /* XXX: We might want to report this error at some point... */ | |
4513 | continue; | |
4514 | } | |
4515 | } | |
4516 | ||
4517 | rcu_read_unlock(); | |
78f0bacd DG |
4518 | return ret; |
4519 | } | |
4520 | ||
b0a40d28 DG |
4521 | /* |
4522 | * Disable an event in a channel and for a specific session. | |
4523 | */ | |
35a9059d DG |
4524 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
4525 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
4526 | { |
4527 | int ret = 0; | |
bec39940 | 4528 | struct lttng_ht_iter iter, uiter; |
700c5a9d | 4529 | struct lttng_ht_node_str *ua_chan_node; |
b0a40d28 DG |
4530 | struct ust_app *app; |
4531 | struct ust_app_session *ua_sess; | |
4532 | struct ust_app_channel *ua_chan; | |
4533 | struct ust_app_event *ua_event; | |
4534 | ||
88e3c2f5 | 4535 | assert(usess->active); |
b0a40d28 | 4536 | DBG("UST app disabling event %s for all apps in channel " |
d9bf3ca4 MD |
4537 | "%s for session id %" PRIu64, |
4538 | uevent->attr.name, uchan->name, usess->id); | |
b0a40d28 DG |
4539 | |
4540 | rcu_read_lock(); | |
4541 | ||
4542 | /* For all registered applications */ | |
852d0037 | 4543 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4544 | if (!app->compatible) { |
4545 | /* | |
4546 | * TODO: In time, we should notice the caller of this error by | |
4547 | * telling him that this is a version error. | |
4548 | */ | |
4549 | continue; | |
4550 | } | |
b0a40d28 DG |
4551 | ua_sess = lookup_session_by_app(usess, app); |
4552 | if (ua_sess == NULL) { | |
4553 | /* Next app */ | |
4554 | continue; | |
4555 | } | |
4556 | ||
4557 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
4558 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4559 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 4560 | if (ua_chan_node == NULL) { |
d9bf3ca4 | 4561 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
852d0037 | 4562 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
4563 | continue; |
4564 | } | |
4565 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4566 | ||
700c5a9d JR |
4567 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
4568 | uevent->filter, uevent->attr.loglevel, | |
4569 | uevent->exclusion); | |
4570 | if (ua_event == NULL) { | |
b0a40d28 | 4571 | DBG2("Event %s not found in channel %s for app pid %d." |
852d0037 | 4572 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
4573 | continue; |
4574 | } | |
b0a40d28 | 4575 | |
7f79d3a1 | 4576 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
b0a40d28 DG |
4577 | if (ret < 0) { |
4578 | /* XXX: Report error someday... */ | |
4579 | continue; | |
4580 | } | |
4581 | } | |
4582 | ||
4583 | rcu_read_unlock(); | |
88e3c2f5 JG |
4584 | return ret; |
4585 | } | |
4586 | ||
4587 | /* The ua_sess lock must be held by the caller. */ | |
4588 | static | |
4589 | int ust_app_channel_create(struct ltt_ust_session *usess, | |
4590 | struct ust_app_session *ua_sess, | |
4591 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
4592 | struct ust_app_channel **_ua_chan) | |
4593 | { | |
4594 | int ret = 0; | |
4595 | struct ust_app_channel *ua_chan = NULL; | |
4596 | ||
4597 | assert(ua_sess); | |
4598 | ASSERT_LOCKED(ua_sess->lock); | |
4599 | ||
4600 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, | |
4601 | sizeof(uchan->name))) { | |
4602 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, | |
4603 | &uchan->attr); | |
4604 | ret = 0; | |
4605 | } else { | |
4606 | struct ltt_ust_context *uctx = NULL; | |
4607 | ||
4608 | /* | |
4609 | * Create channel onto application and synchronize its | |
4610 | * configuration. | |
4611 | */ | |
4612 | ret = ust_app_channel_allocate(ua_sess, uchan, | |
4613 | LTTNG_UST_CHAN_PER_CPU, usess, | |
4614 | &ua_chan); | |
88ebf5a7 JR |
4615 | if (ret < 0) { |
4616 | goto error; | |
4617 | } | |
4618 | ||
4619 | ret = ust_app_channel_send(app, usess, | |
4620 | ua_sess, ua_chan); | |
4621 | if (ret) { | |
4622 | goto error; | |
88e3c2f5 JG |
4623 | } |
4624 | ||
4625 | /* Add contexts. */ | |
4626 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
4627 | ret = create_ust_app_channel_context(ua_chan, | |
4628 | &uctx->ctx, app); | |
4629 | if (ret) { | |
88ebf5a7 | 4630 | goto error; |
88e3c2f5 JG |
4631 | } |
4632 | } | |
4633 | } | |
88ebf5a7 JR |
4634 | |
4635 | error: | |
88e3c2f5 JG |
4636 | if (ret < 0) { |
4637 | switch (ret) { | |
4638 | case -ENOTCONN: | |
4639 | /* | |
4640 | * The application's socket is not valid. Either a bad socket | |
4641 | * or a timeout on it. We can't inform the caller that for a | |
4642 | * specific app, the session failed so lets continue here. | |
4643 | */ | |
4644 | ret = 0; /* Not an error. */ | |
4645 | break; | |
4646 | case -ENOMEM: | |
4647 | default: | |
4648 | break; | |
4649 | } | |
4650 | } | |
88ebf5a7 | 4651 | |
88e3c2f5 JG |
4652 | if (ret == 0 && _ua_chan) { |
4653 | /* | |
4654 | * Only return the application's channel on success. Note | |
4655 | * that the channel can still be part of the application's | |
4656 | * channel hashtable on error. | |
4657 | */ | |
4658 | *_ua_chan = ua_chan; | |
4659 | } | |
b0a40d28 DG |
4660 | return ret; |
4661 | } | |
4662 | ||
5b4a0ec0 | 4663 | /* |
edb67388 | 4664 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 4665 | */ |
35a9059d | 4666 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
4667 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4668 | { | |
4669 | int ret = 0; | |
bec39940 | 4670 | struct lttng_ht_iter iter, uiter; |
18eace3b | 4671 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
4672 | struct ust_app *app; |
4673 | struct ust_app_session *ua_sess; | |
4674 | struct ust_app_channel *ua_chan; | |
4675 | struct ust_app_event *ua_event; | |
48842b30 | 4676 | |
88e3c2f5 | 4677 | assert(usess->active); |
d9bf3ca4 | 4678 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
a991f516 | 4679 | uevent->attr.name, usess->id); |
48842b30 | 4680 | |
edb67388 DG |
4681 | /* |
4682 | * NOTE: At this point, this function is called only if the session and | |
4683 | * channel passed are already created for all apps. and enabled on the | |
4684 | * tracer also. | |
4685 | */ | |
4686 | ||
48842b30 | 4687 | rcu_read_lock(); |
421cb601 DG |
4688 | |
4689 | /* For all registered applications */ | |
852d0037 | 4690 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4691 | if (!app->compatible) { |
4692 | /* | |
4693 | * TODO: In time, we should notice the caller of this error by | |
4694 | * telling him that this is a version error. | |
4695 | */ | |
4696 | continue; | |
4697 | } | |
edb67388 | 4698 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4699 | if (!ua_sess) { |
4700 | /* The application has problem or is probably dead. */ | |
4701 | continue; | |
4702 | } | |
ba767faf | 4703 | |
d0b96690 DG |
4704 | pthread_mutex_lock(&ua_sess->lock); |
4705 | ||
b161602a MD |
4706 | if (ua_sess->deleted) { |
4707 | pthread_mutex_unlock(&ua_sess->lock); | |
4708 | continue; | |
4709 | } | |
4710 | ||
edb67388 | 4711 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4712 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4713 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
a7169585 MD |
4714 | /* |
4715 | * It is possible that the channel cannot be found is | |
4716 | * the channel/event creation occurs concurrently with | |
4717 | * an application exit. | |
4718 | */ | |
4719 | if (!ua_chan_node) { | |
4720 | pthread_mutex_unlock(&ua_sess->lock); | |
4721 | continue; | |
4722 | } | |
edb67388 DG |
4723 | |
4724 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4725 | ||
18eace3b DG |
4726 | /* Get event node */ |
4727 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
39c5a3a7 | 4728 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 4729 | if (ua_event == NULL) { |
7f79d3a1 | 4730 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 4731 | "Skipping app", uevent->attr.name, app->pid); |
d0b96690 | 4732 | goto next_app; |
35a9059d | 4733 | } |
35a9059d DG |
4734 | |
4735 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
4736 | if (ret < 0) { | |
d0b96690 | 4737 | pthread_mutex_unlock(&ua_sess->lock); |
7f79d3a1 | 4738 | goto error; |
48842b30 | 4739 | } |
d0b96690 DG |
4740 | next_app: |
4741 | pthread_mutex_unlock(&ua_sess->lock); | |
edb67388 DG |
4742 | } |
4743 | ||
7f79d3a1 | 4744 | error: |
edb67388 | 4745 | rcu_read_unlock(); |
edb67388 DG |
4746 | return ret; |
4747 | } | |
4748 | ||
4749 | /* | |
4750 | * For a specific existing UST session and UST channel, creates the event for | |
4751 | * all registered apps. | |
4752 | */ | |
35a9059d | 4753 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
4754 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4755 | { | |
4756 | int ret = 0; | |
bec39940 DG |
4757 | struct lttng_ht_iter iter, uiter; |
4758 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
4759 | struct ust_app *app; |
4760 | struct ust_app_session *ua_sess; | |
4761 | struct ust_app_channel *ua_chan; | |
4762 | ||
88e3c2f5 | 4763 | assert(usess->active); |
d9bf3ca4 | 4764 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
a991f516 | 4765 | uevent->attr.name, usess->id); |
edb67388 | 4766 | |
edb67388 DG |
4767 | rcu_read_lock(); |
4768 | ||
4769 | /* For all registered applications */ | |
852d0037 | 4770 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4771 | if (!app->compatible) { |
4772 | /* | |
4773 | * TODO: In time, we should notice the caller of this error by | |
4774 | * telling him that this is a version error. | |
4775 | */ | |
4776 | continue; | |
4777 | } | |
edb67388 | 4778 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4779 | if (!ua_sess) { |
4780 | /* The application has problem or is probably dead. */ | |
4781 | continue; | |
4782 | } | |
48842b30 | 4783 | |
d0b96690 | 4784 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
4785 | |
4786 | if (ua_sess->deleted) { | |
4787 | pthread_mutex_unlock(&ua_sess->lock); | |
4788 | continue; | |
4789 | } | |
4790 | ||
48842b30 | 4791 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4792 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4793 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
4794 | /* If the channel is not found, there is a code flow error */ |
4795 | assert(ua_chan_node); | |
4796 | ||
48842b30 DG |
4797 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
4798 | ||
edb67388 | 4799 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
d0b96690 | 4800 | pthread_mutex_unlock(&ua_sess->lock); |
edb67388 | 4801 | if (ret < 0) { |
49c336c1 | 4802 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
4803 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
4804 | break; | |
4805 | } | |
4806 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 4807 | uevent->attr.name, app->pid); |
5b4a0ec0 | 4808 | continue; |
48842b30 | 4809 | } |
48842b30 | 4810 | } |
5b4a0ec0 | 4811 | |
48842b30 | 4812 | rcu_read_unlock(); |
48842b30 DG |
4813 | return ret; |
4814 | } | |
4815 | ||
5b4a0ec0 DG |
4816 | /* |
4817 | * Start tracing for a specific UST session and app. | |
fad1ed2f JR |
4818 | * |
4819 | * Called with UST app session lock held. | |
4820 | * | |
5b4a0ec0 | 4821 | */ |
b34cbebf | 4822 | static |
421cb601 | 4823 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
4824 | { |
4825 | int ret = 0; | |
48842b30 | 4826 | struct ust_app_session *ua_sess; |
48842b30 | 4827 | |
852d0037 | 4828 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 4829 | |
509cbaf8 MD |
4830 | rcu_read_lock(); |
4831 | ||
e0c7ec2b DG |
4832 | if (!app->compatible) { |
4833 | goto end; | |
4834 | } | |
4835 | ||
421cb601 DG |
4836 | ua_sess = lookup_session_by_app(usess, app); |
4837 | if (ua_sess == NULL) { | |
d42f20df DG |
4838 | /* The session is in teardown process. Ignore and continue. */ |
4839 | goto end; | |
421cb601 | 4840 | } |
48842b30 | 4841 | |
d0b96690 DG |
4842 | pthread_mutex_lock(&ua_sess->lock); |
4843 | ||
b161602a MD |
4844 | if (ua_sess->deleted) { |
4845 | pthread_mutex_unlock(&ua_sess->lock); | |
4846 | goto end; | |
4847 | } | |
4848 | ||
b0a1c741 JR |
4849 | if (ua_sess->enabled) { |
4850 | pthread_mutex_unlock(&ua_sess->lock); | |
4851 | goto end; | |
4852 | } | |
4853 | ||
aea829b3 DG |
4854 | /* Upon restart, we skip the setup, already done */ |
4855 | if (ua_sess->started) { | |
8be98f9a | 4856 | goto skip_setup; |
aea829b3 | 4857 | } |
8be98f9a | 4858 | |
840cb59c | 4859 | health_code_update(); |
86acf0da | 4860 | |
8be98f9a | 4861 | skip_setup: |
a945cdc7 | 4862 | /* This starts the UST tracing */ |
fb45065e | 4863 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4864 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
fb45065e | 4865 | pthread_mutex_unlock(&app->sock_lock); |
421cb601 | 4866 | if (ret < 0) { |
ffe60014 DG |
4867 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4868 | ERR("Error starting tracing for app pid: %d (ret: %d)", | |
4869 | app->pid, ret); | |
4870 | } else { | |
4871 | DBG("UST app start session failed. Application is dead."); | |
3757b385 DG |
4872 | /* |
4873 | * This is normal behavior, an application can die during the | |
4874 | * creation process. Don't report an error so the execution can | |
4875 | * continue normally. | |
4876 | */ | |
4877 | pthread_mutex_unlock(&ua_sess->lock); | |
4878 | goto end; | |
ffe60014 | 4879 | } |
d0b96690 | 4880 | goto error_unlock; |
421cb601 | 4881 | } |
5b4a0ec0 | 4882 | |
55c3953d DG |
4883 | /* Indicate that the session has been started once */ |
4884 | ua_sess->started = 1; | |
b0a1c741 | 4885 | ua_sess->enabled = 1; |
55c3953d | 4886 | |
d0b96690 DG |
4887 | pthread_mutex_unlock(&ua_sess->lock); |
4888 | ||
840cb59c | 4889 | health_code_update(); |
86acf0da | 4890 | |
421cb601 | 4891 | /* Quiescent wait after starting trace */ |
fb45065e | 4892 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 4893 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 4894 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
4895 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4896 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
4897 | app->pid, ret); | |
4898 | } | |
48842b30 | 4899 | |
e0c7ec2b DG |
4900 | end: |
4901 | rcu_read_unlock(); | |
840cb59c | 4902 | health_code_update(); |
421cb601 | 4903 | return 0; |
48842b30 | 4904 | |
d0b96690 DG |
4905 | error_unlock: |
4906 | pthread_mutex_unlock(&ua_sess->lock); | |
509cbaf8 | 4907 | rcu_read_unlock(); |
840cb59c | 4908 | health_code_update(); |
421cb601 DG |
4909 | return -1; |
4910 | } | |
48842b30 | 4911 | |
8be98f9a MD |
4912 | /* |
4913 | * Stop tracing for a specific UST session and app. | |
4914 | */ | |
b34cbebf | 4915 | static |
8be98f9a MD |
4916 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
4917 | { | |
4918 | int ret = 0; | |
4919 | struct ust_app_session *ua_sess; | |
7972aab2 | 4920 | struct ust_registry_session *registry; |
8be98f9a | 4921 | |
852d0037 | 4922 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
4923 | |
4924 | rcu_read_lock(); | |
4925 | ||
e0c7ec2b | 4926 | if (!app->compatible) { |
d88aee68 | 4927 | goto end_no_session; |
e0c7ec2b DG |
4928 | } |
4929 | ||
8be98f9a MD |
4930 | ua_sess = lookup_session_by_app(usess, app); |
4931 | if (ua_sess == NULL) { | |
d88aee68 | 4932 | goto end_no_session; |
8be98f9a MD |
4933 | } |
4934 | ||
d88aee68 DG |
4935 | pthread_mutex_lock(&ua_sess->lock); |
4936 | ||
b161602a MD |
4937 | if (ua_sess->deleted) { |
4938 | pthread_mutex_unlock(&ua_sess->lock); | |
4939 | goto end_no_session; | |
4940 | } | |
4941 | ||
9bc07046 DG |
4942 | /* |
4943 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
4944 | * that was never started. It's possible since we can have a fail start |
4945 | * from either the application manager thread or the command thread. Simply | |
4946 | * indicate that this is a stop error. | |
9bc07046 | 4947 | */ |
f9dfc3d9 | 4948 | if (!ua_sess->started) { |
c45536e1 DG |
4949 | goto error_rcu_unlock; |
4950 | } | |
7db205b5 | 4951 | |
840cb59c | 4952 | health_code_update(); |
86acf0da | 4953 | |
9d6c7d3f | 4954 | /* This inhibits UST tracing */ |
fb45065e | 4955 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4956 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
fb45065e | 4957 | pthread_mutex_unlock(&app->sock_lock); |
9d6c7d3f | 4958 | if (ret < 0) { |
ffe60014 DG |
4959 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4960 | ERR("Error stopping tracing for app pid: %d (ret: %d)", | |
4961 | app->pid, ret); | |
4962 | } else { | |
4963 | DBG("UST app stop session failed. Application is dead."); | |
3757b385 DG |
4964 | /* |
4965 | * This is normal behavior, an application can die during the | |
4966 | * creation process. Don't report an error so the execution can | |
4967 | * continue normally. | |
4968 | */ | |
4969 | goto end_unlock; | |
ffe60014 | 4970 | } |
9d6c7d3f DG |
4971 | goto error_rcu_unlock; |
4972 | } | |
4973 | ||
840cb59c | 4974 | health_code_update(); |
b0a1c741 | 4975 | ua_sess->enabled = 0; |
86acf0da | 4976 | |
9d6c7d3f | 4977 | /* Quiescent wait after stopping trace */ |
fb45065e | 4978 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 4979 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 4980 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
4981 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4982 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
4983 | app->pid, ret); | |
4984 | } | |
9d6c7d3f | 4985 | |
840cb59c | 4986 | health_code_update(); |
86acf0da | 4987 | |
b34cbebf | 4988 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
4989 | |
4990 | /* The UST app session is held registry shall not be null. */ | |
b34cbebf | 4991 | assert(registry); |
1b532a60 | 4992 | |
ce34fcd0 MD |
4993 | /* Push metadata for application before freeing the application. */ |
4994 | (void) push_metadata(registry, ua_sess->consumer); | |
b34cbebf | 4995 | |
3757b385 | 4996 | end_unlock: |
b34cbebf MD |
4997 | pthread_mutex_unlock(&ua_sess->lock); |
4998 | end_no_session: | |
4999 | rcu_read_unlock(); | |
5000 | health_code_update(); | |
5001 | return 0; | |
5002 | ||
5003 | error_rcu_unlock: | |
5004 | pthread_mutex_unlock(&ua_sess->lock); | |
5005 | rcu_read_unlock(); | |
5006 | health_code_update(); | |
5007 | return -1; | |
5008 | } | |
5009 | ||
b34cbebf | 5010 | static |
c4b88406 MD |
5011 | int ust_app_flush_app_session(struct ust_app *app, |
5012 | struct ust_app_session *ua_sess) | |
b34cbebf | 5013 | { |
c4b88406 | 5014 | int ret, retval = 0; |
b34cbebf | 5015 | struct lttng_ht_iter iter; |
b34cbebf | 5016 | struct ust_app_channel *ua_chan; |
c4b88406 | 5017 | struct consumer_socket *socket; |
b34cbebf | 5018 | |
c4b88406 | 5019 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
b34cbebf MD |
5020 | |
5021 | rcu_read_lock(); | |
5022 | ||
5023 | if (!app->compatible) { | |
c4b88406 | 5024 | goto end_not_compatible; |
b34cbebf MD |
5025 | } |
5026 | ||
5027 | pthread_mutex_lock(&ua_sess->lock); | |
5028 | ||
b161602a MD |
5029 | if (ua_sess->deleted) { |
5030 | goto end_deleted; | |
5031 | } | |
5032 | ||
b34cbebf MD |
5033 | health_code_update(); |
5034 | ||
9d6c7d3f | 5035 | /* Flushing buffers */ |
c4b88406 MD |
5036 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
5037 | ua_sess->consumer); | |
ce34fcd0 MD |
5038 | |
5039 | /* Flush buffers and push metadata. */ | |
5040 | switch (ua_sess->buffer_type) { | |
5041 | case LTTNG_BUFFER_PER_PID: | |
5042 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, | |
5043 | node.node) { | |
5044 | health_code_update(); | |
ce34fcd0 MD |
5045 | ret = consumer_flush_channel(socket, ua_chan->key); |
5046 | if (ret) { | |
5047 | ERR("Error flushing consumer channel"); | |
5048 | retval = -1; | |
5049 | continue; | |
5050 | } | |
8be98f9a | 5051 | } |
ce34fcd0 MD |
5052 | break; |
5053 | case LTTNG_BUFFER_PER_UID: | |
5054 | default: | |
5055 | assert(0); | |
5056 | break; | |
8be98f9a | 5057 | } |
8be98f9a | 5058 | |
840cb59c | 5059 | health_code_update(); |
86acf0da | 5060 | |
b161602a | 5061 | end_deleted: |
d88aee68 | 5062 | pthread_mutex_unlock(&ua_sess->lock); |
ce34fcd0 | 5063 | |
c4b88406 MD |
5064 | end_not_compatible: |
5065 | rcu_read_unlock(); | |
5066 | health_code_update(); | |
5067 | return retval; | |
5068 | } | |
5069 | ||
5070 | /* | |
ce34fcd0 MD |
5071 | * Flush buffers for all applications for a specific UST session. |
5072 | * Called with UST session lock held. | |
c4b88406 MD |
5073 | */ |
5074 | static | |
ce34fcd0 | 5075 | int ust_app_flush_session(struct ltt_ust_session *usess) |
c4b88406 MD |
5076 | |
5077 | { | |
99b1411c | 5078 | int ret = 0; |
c4b88406 | 5079 | |
ce34fcd0 | 5080 | DBG("Flushing session buffers for all ust apps"); |
c4b88406 MD |
5081 | |
5082 | rcu_read_lock(); | |
5083 | ||
ce34fcd0 MD |
5084 | /* Flush buffers and push metadata. */ |
5085 | switch (usess->buffer_type) { | |
5086 | case LTTNG_BUFFER_PER_UID: | |
5087 | { | |
5088 | struct buffer_reg_uid *reg; | |
5089 | struct lttng_ht_iter iter; | |
5090 | ||
5091 | /* Flush all per UID buffers associated to that session. */ | |
5092 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
5093 | struct ust_registry_session *ust_session_reg; | |
5094 | struct buffer_reg_channel *reg_chan; | |
5095 | struct consumer_socket *socket; | |
5096 | ||
5097 | /* Get consumer socket to use to push the metadata.*/ | |
5098 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
5099 | usess->consumer); | |
5100 | if (!socket) { | |
5101 | /* Ignore request if no consumer is found for the session. */ | |
5102 | continue; | |
5103 | } | |
5104 | ||
5105 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
5106 | reg_chan, node.node) { | |
5107 | /* | |
5108 | * The following call will print error values so the return | |
5109 | * code is of little importance because whatever happens, we | |
5110 | * have to try them all. | |
5111 | */ | |
5112 | (void) consumer_flush_channel(socket, reg_chan->consumer_key); | |
5113 | } | |
5114 | ||
5115 | ust_session_reg = reg->registry->reg.ust; | |
5116 | /* Push metadata. */ | |
5117 | (void) push_metadata(ust_session_reg, usess->consumer); | |
5118 | } | |
ce34fcd0 MD |
5119 | break; |
5120 | } | |
5121 | case LTTNG_BUFFER_PER_PID: | |
5122 | { | |
5123 | struct ust_app_session *ua_sess; | |
5124 | struct lttng_ht_iter iter; | |
5125 | struct ust_app *app; | |
5126 | ||
5127 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5128 | ua_sess = lookup_session_by_app(usess, app); | |
5129 | if (ua_sess == NULL) { | |
5130 | continue; | |
5131 | } | |
5132 | (void) ust_app_flush_app_session(app, ua_sess); | |
5133 | } | |
5134 | break; | |
5135 | } | |
5136 | default: | |
99b1411c | 5137 | ret = -1; |
ce34fcd0 MD |
5138 | assert(0); |
5139 | break; | |
c4b88406 | 5140 | } |
c4b88406 | 5141 | |
7db205b5 | 5142 | rcu_read_unlock(); |
840cb59c | 5143 | health_code_update(); |
c4b88406 | 5144 | return ret; |
8be98f9a MD |
5145 | } |
5146 | ||
0dd01979 MD |
5147 | static |
5148 | int ust_app_clear_quiescent_app_session(struct ust_app *app, | |
5149 | struct ust_app_session *ua_sess) | |
5150 | { | |
5151 | int ret = 0; | |
5152 | struct lttng_ht_iter iter; | |
5153 | struct ust_app_channel *ua_chan; | |
5154 | struct consumer_socket *socket; | |
5155 | ||
5156 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); | |
5157 | ||
5158 | rcu_read_lock(); | |
5159 | ||
5160 | if (!app->compatible) { | |
5161 | goto end_not_compatible; | |
5162 | } | |
5163 | ||
5164 | pthread_mutex_lock(&ua_sess->lock); | |
5165 | ||
5166 | if (ua_sess->deleted) { | |
5167 | goto end_unlock; | |
5168 | } | |
5169 | ||
5170 | health_code_update(); | |
5171 | ||
5172 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
5173 | ua_sess->consumer); | |
5174 | if (!socket) { | |
5175 | ERR("Failed to find consumer (%" PRIu32 ") socket", | |
5176 | app->bits_per_long); | |
5177 | ret = -1; | |
5178 | goto end_unlock; | |
5179 | } | |
5180 | ||
5181 | /* Clear quiescent state. */ | |
5182 | switch (ua_sess->buffer_type) { | |
5183 | case LTTNG_BUFFER_PER_PID: | |
5184 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, | |
5185 | ua_chan, node.node) { | |
5186 | health_code_update(); | |
5187 | ret = consumer_clear_quiescent_channel(socket, | |
5188 | ua_chan->key); | |
5189 | if (ret) { | |
5190 | ERR("Error clearing quiescent state for consumer channel"); | |
5191 | ret = -1; | |
5192 | continue; | |
5193 | } | |
5194 | } | |
5195 | break; | |
5196 | case LTTNG_BUFFER_PER_UID: | |
5197 | default: | |
5198 | assert(0); | |
5199 | ret = -1; | |
5200 | break; | |
5201 | } | |
5202 | ||
5203 | health_code_update(); | |
5204 | ||
5205 | end_unlock: | |
5206 | pthread_mutex_unlock(&ua_sess->lock); | |
5207 | ||
5208 | end_not_compatible: | |
5209 | rcu_read_unlock(); | |
5210 | health_code_update(); | |
5211 | return ret; | |
5212 | } | |
5213 | ||
5214 | /* | |
5215 | * Clear quiescent state in each stream for all applications for a | |
5216 | * specific UST session. | |
5217 | * Called with UST session lock held. | |
5218 | */ | |
5219 | static | |
5220 | int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) | |
5221 | ||
5222 | { | |
5223 | int ret = 0; | |
5224 | ||
5225 | DBG("Clearing stream quiescent state for all ust apps"); | |
5226 | ||
5227 | rcu_read_lock(); | |
5228 | ||
5229 | switch (usess->buffer_type) { | |
5230 | case LTTNG_BUFFER_PER_UID: | |
5231 | { | |
5232 | struct lttng_ht_iter iter; | |
5233 | struct buffer_reg_uid *reg; | |
5234 | ||
5235 | /* | |
5236 | * Clear quiescent for all per UID buffers associated to | |
5237 | * that session. | |
5238 | */ | |
5239 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
5240 | struct consumer_socket *socket; | |
5241 | struct buffer_reg_channel *reg_chan; | |
5242 | ||
5243 | /* Get associated consumer socket.*/ | |
5244 | socket = consumer_find_socket_by_bitness( | |
5245 | reg->bits_per_long, usess->consumer); | |
5246 | if (!socket) { | |
5247 | /* | |
5248 | * Ignore request if no consumer is found for | |
5249 | * the session. | |
5250 | */ | |
5251 | continue; | |
5252 | } | |
5253 | ||
5254 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
5255 | &iter.iter, reg_chan, node.node) { | |
5256 | /* | |
5257 | * The following call will print error values so | |
5258 | * the return code is of little importance | |
5259 | * because whatever happens, we have to try them | |
5260 | * all. | |
5261 | */ | |
5262 | (void) consumer_clear_quiescent_channel(socket, | |
5263 | reg_chan->consumer_key); | |
5264 | } | |
5265 | } | |
5266 | break; | |
5267 | } | |
5268 | case LTTNG_BUFFER_PER_PID: | |
5269 | { | |
5270 | struct ust_app_session *ua_sess; | |
5271 | struct lttng_ht_iter iter; | |
5272 | struct ust_app *app; | |
5273 | ||
5274 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, | |
5275 | pid_n.node) { | |
5276 | ua_sess = lookup_session_by_app(usess, app); | |
5277 | if (ua_sess == NULL) { | |
5278 | continue; | |
5279 | } | |
5280 | (void) ust_app_clear_quiescent_app_session(app, | |
5281 | ua_sess); | |
5282 | } | |
5283 | break; | |
5284 | } | |
5285 | default: | |
5286 | ret = -1; | |
5287 | assert(0); | |
5288 | break; | |
5289 | } | |
5290 | ||
5291 | rcu_read_unlock(); | |
5292 | health_code_update(); | |
5293 | return ret; | |
5294 | } | |
5295 | ||
84cd17c6 MD |
5296 | /* |
5297 | * Destroy a specific UST session in apps. | |
5298 | */ | |
3353de95 | 5299 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 | 5300 | { |
ffe60014 | 5301 | int ret; |
84cd17c6 | 5302 | struct ust_app_session *ua_sess; |
bec39940 | 5303 | struct lttng_ht_iter iter; |
d9bf3ca4 | 5304 | struct lttng_ht_node_u64 *node; |
84cd17c6 | 5305 | |
852d0037 | 5306 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
5307 | |
5308 | rcu_read_lock(); | |
5309 | ||
e0c7ec2b DG |
5310 | if (!app->compatible) { |
5311 | goto end; | |
5312 | } | |
5313 | ||
84cd17c6 | 5314 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 5315 | node = lttng_ht_iter_get_node_u64(&iter); |
84cd17c6 | 5316 | if (node == NULL) { |
d42f20df DG |
5317 | /* Session is being or is deleted. */ |
5318 | goto end; | |
84cd17c6 MD |
5319 | } |
5320 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
c4a1715b | 5321 | |
840cb59c | 5322 | health_code_update(); |
d0b96690 | 5323 | destroy_app_session(app, ua_sess); |
84cd17c6 | 5324 | |
840cb59c | 5325 | health_code_update(); |
7db205b5 | 5326 | |
84cd17c6 | 5327 | /* Quiescent wait after stopping trace */ |
fb45065e | 5328 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 5329 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 5330 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
5331 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
5332 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
5333 | app->pid, ret); | |
5334 | } | |
e0c7ec2b DG |
5335 | end: |
5336 | rcu_read_unlock(); | |
840cb59c | 5337 | health_code_update(); |
84cd17c6 | 5338 | return 0; |
84cd17c6 MD |
5339 | } |
5340 | ||
5b4a0ec0 DG |
5341 | /* |
5342 | * Start tracing for the UST session. | |
5343 | */ | |
421cb601 DG |
5344 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
5345 | { | |
bec39940 | 5346 | struct lttng_ht_iter iter; |
421cb601 | 5347 | struct ust_app *app; |
48842b30 | 5348 | |
421cb601 DG |
5349 | DBG("Starting all UST traces"); |
5350 | ||
bb2452c8 MD |
5351 | /* |
5352 | * Even though the start trace might fail, flag this session active so | |
5353 | * other application coming in are started by default. | |
5354 | */ | |
5355 | usess->active = 1; | |
5356 | ||
421cb601 | 5357 | rcu_read_lock(); |
421cb601 | 5358 | |
0dd01979 MD |
5359 | /* |
5360 | * In a start-stop-start use-case, we need to clear the quiescent state | |
5361 | * of each channel set by the prior stop command, thus ensuring that a | |
5362 | * following stop or destroy is sure to grab a timestamp_end near those | |
5363 | * operations, even if the packet is empty. | |
5364 | */ | |
5365 | (void) ust_app_clear_quiescent_session(usess); | |
5366 | ||
0498a00c MD |
5367 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5368 | ust_app_global_update(usess, app); | |
5369 | } | |
5370 | ||
48842b30 DG |
5371 | rcu_read_unlock(); |
5372 | ||
5373 | return 0; | |
5374 | } | |
487cf67c | 5375 | |
8be98f9a MD |
5376 | /* |
5377 | * Start tracing for the UST session. | |
ce34fcd0 | 5378 | * Called with UST session lock held. |
8be98f9a MD |
5379 | */ |
5380 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
5381 | { | |
5382 | int ret = 0; | |
bec39940 | 5383 | struct lttng_ht_iter iter; |
8be98f9a MD |
5384 | struct ust_app *app; |
5385 | ||
5386 | DBG("Stopping all UST traces"); | |
5387 | ||
bb2452c8 MD |
5388 | /* |
5389 | * Even though the stop trace might fail, flag this session inactive so | |
5390 | * other application coming in are not started by default. | |
5391 | */ | |
5392 | usess->active = 0; | |
5393 | ||
8be98f9a MD |
5394 | rcu_read_lock(); |
5395 | ||
b34cbebf MD |
5396 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5397 | ret = ust_app_stop_trace(usess, app); | |
5398 | if (ret < 0) { | |
5399 | /* Continue to next apps even on error */ | |
5400 | continue; | |
5401 | } | |
5402 | } | |
5403 | ||
ce34fcd0 | 5404 | (void) ust_app_flush_session(usess); |
8be98f9a MD |
5405 | |
5406 | rcu_read_unlock(); | |
5407 | ||
5408 | return 0; | |
5409 | } | |
5410 | ||
84cd17c6 MD |
5411 | /* |
5412 | * Destroy app UST session. | |
5413 | */ | |
5414 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
5415 | { | |
5416 | int ret = 0; | |
bec39940 | 5417 | struct lttng_ht_iter iter; |
84cd17c6 MD |
5418 | struct ust_app *app; |
5419 | ||
5420 | DBG("Destroy all UST traces"); | |
5421 | ||
5422 | rcu_read_lock(); | |
5423 | ||
852d0037 | 5424 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 5425 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
5426 | if (ret < 0) { |
5427 | /* Continue to next apps even on error */ | |
5428 | continue; | |
5429 | } | |
5430 | } | |
5431 | ||
5432 | rcu_read_unlock(); | |
5433 | ||
5434 | return 0; | |
5435 | } | |
5436 | ||
88e3c2f5 | 5437 | /* The ua_sess lock must be held by the caller. */ |
a9ad0c8f | 5438 | static |
88e3c2f5 JG |
5439 | int find_or_create_ust_app_channel( |
5440 | struct ltt_ust_session *usess, | |
5441 | struct ust_app_session *ua_sess, | |
5442 | struct ust_app *app, | |
5443 | struct ltt_ust_channel *uchan, | |
5444 | struct ust_app_channel **ua_chan) | |
487cf67c | 5445 | { |
55c54cce | 5446 | int ret = 0; |
88e3c2f5 JG |
5447 | struct lttng_ht_iter iter; |
5448 | struct lttng_ht_node_str *ua_chan_node; | |
5449 | ||
5450 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); | |
5451 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
5452 | if (ua_chan_node) { | |
5453 | *ua_chan = caa_container_of(ua_chan_node, | |
5454 | struct ust_app_channel, node); | |
5455 | goto end; | |
5456 | } | |
5457 | ||
5458 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); | |
5459 | if (ret) { | |
5460 | goto end; | |
5461 | } | |
5462 | end: | |
5463 | return ret; | |
5464 | } | |
5465 | ||
5466 | static | |
5467 | int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, | |
5468 | struct ltt_ust_event *uevent, struct ust_app_session *ua_sess, | |
5469 | struct ust_app *app) | |
5470 | { | |
5471 | int ret = 0; | |
5472 | struct ust_app_event *ua_event = NULL; | |
5473 | ||
5474 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
5475 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); | |
5476 | if (!ua_event) { | |
5477 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); | |
5478 | if (ret < 0) { | |
5479 | goto end; | |
5480 | } | |
5481 | } else { | |
5482 | if (ua_event->enabled != uevent->enabled) { | |
5483 | ret = uevent->enabled ? | |
5484 | enable_ust_app_event(ua_sess, ua_event, app) : | |
5485 | disable_ust_app_event(ua_sess, ua_event, app); | |
5486 | } | |
5487 | } | |
5488 | ||
5489 | end: | |
5490 | return ret; | |
5491 | } | |
5492 | ||
993578ff JR |
5493 | /* Called with RCU read-side lock held. */ |
5494 | static | |
5495 | void ust_app_synchronize_event_notifier_rules(struct ust_app *app) | |
5496 | { | |
5497 | int ret = 0; | |
5498 | enum lttng_error_code ret_code; | |
5499 | enum lttng_trigger_status t_status; | |
5500 | struct lttng_ht_iter app_trigger_iter; | |
5501 | struct lttng_triggers *triggers = NULL; | |
5502 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5503 | unsigned int count, i; | |
5504 | ||
5505 | /* | |
5506 | * Currrently, registering or unregistering a trigger with an | |
5507 | * event rule condition causes a full synchronization of the event | |
5508 | * notifiers. | |
5509 | * | |
5510 | * The first step attempts to add an event notifier for all registered | |
5511 | * triggers that apply to the user space tracers. Then, the | |
5512 | * application's event notifiers rules are all checked against the list | |
5513 | * of registered triggers. Any event notifier that doesn't have a | |
5514 | * matching trigger can be assumed to have been disabled. | |
5515 | * | |
5516 | * All of this is inefficient, but is put in place to get the feature | |
5517 | * rolling as it is simpler at this moment. It will be optimized Soon™ | |
5518 | * to allow the state of enabled | |
5519 | * event notifiers to be synchronized in a piece-wise way. | |
5520 | */ | |
5521 | ||
5522 | /* Get all triggers using uid 0 (root) */ | |
5523 | ret_code = notification_thread_command_list_triggers( | |
5524 | notification_thread_handle, 0, &triggers); | |
5525 | if (ret_code != LTTNG_OK) { | |
5526 | ret = -1; | |
5527 | goto end; | |
5528 | } | |
5529 | ||
5530 | assert(triggers); | |
5531 | ||
5532 | t_status = lttng_triggers_get_count(triggers, &count); | |
5533 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
5534 | ret = -1; | |
5535 | goto end; | |
5536 | } | |
5537 | ||
5538 | for (i = 0; i < count; i++) { | |
5539 | struct lttng_condition *condition; | |
5540 | struct lttng_event_rule *event_rule; | |
5541 | struct lttng_trigger *trigger; | |
5542 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; | |
5543 | enum lttng_condition_status condition_status; | |
5544 | uint64_t token; | |
5545 | ||
5546 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); | |
5547 | assert(trigger); | |
5548 | ||
5549 | token = lttng_trigger_get_tracer_token(trigger); | |
5550 | condition = lttng_trigger_get_condition(trigger); | |
5551 | ||
5552 | if (lttng_condition_get_type(condition) != LTTNG_CONDITION_TYPE_EVENT_RULE_HIT) { | |
5553 | /* Does not apply */ | |
5554 | continue; | |
5555 | } | |
5556 | ||
5557 | condition_status = lttng_condition_event_rule_borrow_rule_mutable(condition, &event_rule); | |
5558 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
5559 | ||
5560 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { | |
5561 | /* Skip kernel related triggers. */ | |
5562 | continue; | |
5563 | } | |
5564 | ||
5565 | /* | |
5566 | * Find or create the associated token event rule. The caller | |
5567 | * holds the RCU read lock, so this is safe to call without | |
5568 | * explicitly acquiring it here. | |
5569 | */ | |
5570 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( | |
5571 | app->token_to_event_notifier_rule_ht, token); | |
5572 | if (!looked_up_event_notifier_rule) { | |
267d66aa | 5573 | ret = create_ust_app_event_notifier_rule(trigger, app); |
993578ff JR |
5574 | if (ret < 0) { |
5575 | goto end; | |
5576 | } | |
5577 | } | |
5578 | } | |
5579 | ||
5580 | rcu_read_lock(); | |
5581 | /* Remove all unknown event sources from the app. */ | |
5582 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
5583 | &app_trigger_iter.iter, event_notifier_rule, | |
5584 | node.node) { | |
5585 | const uint64_t app_token = event_notifier_rule->token; | |
5586 | bool found = false; | |
5587 | ||
5588 | /* | |
5589 | * Check if the app event trigger still exists on the | |
5590 | * notification side. | |
5591 | */ | |
5592 | for (i = 0; i < count; i++) { | |
5593 | uint64_t notification_thread_token; | |
5594 | const struct lttng_trigger *trigger = | |
5595 | lttng_triggers_get_at_index( | |
5596 | triggers, i); | |
5597 | ||
5598 | assert(trigger); | |
5599 | ||
5600 | notification_thread_token = | |
5601 | lttng_trigger_get_tracer_token(trigger); | |
5602 | ||
5603 | if (notification_thread_token == app_token) { | |
5604 | found = true; | |
5605 | break; | |
5606 | } | |
5607 | } | |
5608 | ||
5609 | if (found) { | |
5610 | /* Still valid. */ | |
5611 | continue; | |
5612 | } | |
5613 | ||
5614 | /* | |
5615 | * This trigger was unregistered, disable it on the tracer's | |
5616 | * side. | |
5617 | */ | |
5618 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, | |
5619 | &app_trigger_iter); | |
5620 | assert(ret == 0); | |
5621 | ||
5622 | /* Callee logs errors. */ | |
5623 | (void) disable_ust_object(app, event_notifier_rule->obj); | |
5624 | ||
5625 | delete_ust_app_event_notifier_rule( | |
5626 | app->sock, event_notifier_rule, app); | |
5627 | } | |
5628 | ||
5629 | rcu_read_unlock(); | |
5630 | ||
5631 | end: | |
5632 | lttng_triggers_destroy(triggers); | |
5633 | return; | |
5634 | } | |
5635 | ||
88e3c2f5 JG |
5636 | /* |
5637 | * The caller must ensure that the application is compatible and is tracked | |
2a3a1a2b | 5638 | * by the process attribute trackers. |
88e3c2f5 JG |
5639 | */ |
5640 | static | |
5641 | void ust_app_synchronize(struct ltt_ust_session *usess, | |
5642 | struct ust_app *app) | |
5643 | { | |
5644 | int ret = 0; | |
5645 | struct cds_lfht_iter uchan_iter; | |
5646 | struct ltt_ust_channel *uchan; | |
3d8ca23b | 5647 | struct ust_app_session *ua_sess = NULL; |
1f3580c7 | 5648 | |
88e3c2f5 JG |
5649 | /* |
5650 | * The application's configuration should only be synchronized for | |
5651 | * active sessions. | |
5652 | */ | |
5653 | assert(usess->active); | |
5654 | ||
5655 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL); | |
3d8ca23b DG |
5656 | if (ret < 0) { |
5657 | /* Tracer is probably gone or ENOMEM. */ | |
487cf67c DG |
5658 | goto error; |
5659 | } | |
3d8ca23b | 5660 | assert(ua_sess); |
487cf67c | 5661 | |
d0b96690 | 5662 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
5663 | if (ua_sess->deleted) { |
5664 | pthread_mutex_unlock(&ua_sess->lock); | |
5665 | goto end; | |
5666 | } | |
5667 | ||
88e3c2f5 | 5668 | rcu_read_lock(); |
ef67c072 | 5669 | |
88e3c2f5 JG |
5670 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, |
5671 | uchan, node.node) { | |
5672 | struct ust_app_channel *ua_chan; | |
5673 | struct cds_lfht_iter uevent_iter; | |
5674 | struct ltt_ust_event *uevent; | |
487cf67c | 5675 | |
31746f93 | 5676 | /* |
88e3c2f5 JG |
5677 | * Search for a matching ust_app_channel. If none is found, |
5678 | * create it. Creating the channel will cause the ua_chan | |
5679 | * structure to be allocated, the channel buffers to be | |
5680 | * allocated (if necessary) and sent to the application, and | |
5681 | * all enabled contexts will be added to the channel. | |
31746f93 | 5682 | */ |
f3db82be | 5683 | ret = find_or_create_ust_app_channel(usess, ua_sess, |
88e3c2f5 JG |
5684 | app, uchan, &ua_chan); |
5685 | if (ret) { | |
5686 | /* Tracer is probably gone or ENOMEM. */ | |
5687 | goto error_unlock; | |
727d5404 DG |
5688 | } |
5689 | ||
88e3c2f5 JG |
5690 | if (!ua_chan) { |
5691 | /* ua_chan will be NULL for the metadata channel */ | |
5692 | continue; | |
5693 | } | |
727d5404 | 5694 | |
88e3c2f5 | 5695 | cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent, |
bec39940 | 5696 | node.node) { |
88e3c2f5 JG |
5697 | ret = ust_app_channel_synchronize_event(ua_chan, |
5698 | uevent, ua_sess, app); | |
5699 | if (ret) { | |
d0b96690 | 5700 | goto error_unlock; |
487cf67c | 5701 | } |
36dc12cc | 5702 | } |
d0b96690 | 5703 | |
88e3c2f5 JG |
5704 | if (ua_chan->enabled != uchan->enabled) { |
5705 | ret = uchan->enabled ? | |
5706 | enable_ust_app_channel(ua_sess, uchan, app) : | |
5707 | disable_ust_app_channel(ua_sess, ua_chan, app); | |
5708 | if (ret) { | |
5709 | goto error_unlock; | |
5710 | } | |
5711 | } | |
36dc12cc | 5712 | } |
ef67c072 JG |
5713 | |
5714 | /* | |
5715 | * Create the metadata for the application. This returns gracefully if a | |
5716 | * metadata was already set for the session. | |
5717 | * | |
5718 | * The metadata channel must be created after the data channels as the | |
5719 | * consumer daemon assumes this ordering. When interacting with a relay | |
5720 | * daemon, the consumer will use this assumption to send the | |
5721 | * "STREAMS_SENT" message to the relay daemon. | |
5722 | */ | |
5723 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); | |
5724 | if (ret < 0) { | |
5725 | goto error_unlock; | |
5726 | } | |
5727 | ||
88e3c2f5 | 5728 | rcu_read_unlock(); |
0498a00c | 5729 | |
a9ad0c8f | 5730 | end: |
88e3c2f5 | 5731 | pthread_mutex_unlock(&ua_sess->lock); |
ffe60014 | 5732 | /* Everything went well at this point. */ |
ffe60014 DG |
5733 | return; |
5734 | ||
d0b96690 | 5735 | error_unlock: |
88e3c2f5 | 5736 | rcu_read_unlock(); |
d0b96690 | 5737 | pthread_mutex_unlock(&ua_sess->lock); |
487cf67c | 5738 | error: |
ffe60014 | 5739 | if (ua_sess) { |
d0b96690 | 5740 | destroy_app_session(app, ua_sess); |
ffe60014 | 5741 | } |
487cf67c DG |
5742 | return; |
5743 | } | |
55cc08a6 | 5744 | |
a9ad0c8f MD |
5745 | static |
5746 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) | |
5747 | { | |
5748 | struct ust_app_session *ua_sess; | |
5749 | ||
5750 | ua_sess = lookup_session_by_app(usess, app); | |
5751 | if (ua_sess == NULL) { | |
5752 | return; | |
5753 | } | |
5754 | destroy_app_session(app, ua_sess); | |
5755 | } | |
5756 | ||
5757 | /* | |
5758 | * Add channels/events from UST global domain to registered apps at sock. | |
5759 | * | |
5760 | * Called with session lock held. | |
5761 | * Called with RCU read-side lock held. | |
5762 | */ | |
5763 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) | |
5764 | { | |
5765 | assert(usess); | |
88e3c2f5 | 5766 | assert(usess->active); |
a9ad0c8f MD |
5767 | |
5768 | DBG2("UST app global update for app sock %d for session id %" PRIu64, | |
5769 | app->sock, usess->id); | |
5770 | ||
5771 | if (!app->compatible) { | |
5772 | return; | |
5773 | } | |
159b042f JG |
5774 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, |
5775 | usess, app->pid) && | |
55c9e7ca | 5776 | trace_ust_id_tracker_lookup( |
159b042f JG |
5777 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, |
5778 | usess, app->uid) && | |
55c9e7ca | 5779 | trace_ust_id_tracker_lookup( |
159b042f JG |
5780 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, |
5781 | usess, app->gid)) { | |
88e3c2f5 JG |
5782 | /* |
5783 | * Synchronize the application's internal tracing configuration | |
5784 | * and start tracing. | |
5785 | */ | |
5786 | ust_app_synchronize(usess, app); | |
5787 | ust_app_start_trace(usess, app); | |
a9ad0c8f MD |
5788 | } else { |
5789 | ust_app_global_destroy(usess, app); | |
5790 | } | |
5791 | } | |
5792 | ||
993578ff JR |
5793 | /* |
5794 | * Add all event notifiers to an application. | |
5795 | * | |
5796 | * Called with session lock held. | |
5797 | * Called with RCU read-side lock held. | |
5798 | */ | |
5799 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) | |
5800 | { | |
5801 | DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)", | |
5802 | app->name, app->ppid); | |
5803 | ||
5804 | if (!app->compatible) { | |
5805 | return; | |
5806 | } | |
5807 | ||
5808 | if (app->event_notifier_group.object == NULL) { | |
5809 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)", | |
5810 | app->name, app->ppid); | |
5811 | return; | |
5812 | } | |
5813 | ||
5814 | ust_app_synchronize_event_notifier_rules(app); | |
5815 | } | |
5816 | ||
a9ad0c8f MD |
5817 | /* |
5818 | * Called with session lock held. | |
5819 | */ | |
5820 | void ust_app_global_update_all(struct ltt_ust_session *usess) | |
5821 | { | |
5822 | struct lttng_ht_iter iter; | |
5823 | struct ust_app *app; | |
5824 | ||
5825 | rcu_read_lock(); | |
5826 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5827 | ust_app_global_update(usess, app); | |
5828 | } | |
5829 | rcu_read_unlock(); | |
5830 | } | |
5831 | ||
993578ff JR |
5832 | void ust_app_global_update_all_event_notifier_rules(void) |
5833 | { | |
5834 | struct lttng_ht_iter iter; | |
5835 | struct ust_app *app; | |
5836 | ||
5837 | rcu_read_lock(); | |
5838 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5839 | ust_app_global_update_event_notifier_rules(app); | |
5840 | } | |
5841 | ||
5842 | rcu_read_unlock(); | |
5843 | } | |
5844 | ||
55cc08a6 DG |
5845 | /* |
5846 | * Add context to a specific channel for global UST domain. | |
5847 | */ | |
5848 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
5849 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
5850 | { | |
5851 | int ret = 0; | |
bec39940 DG |
5852 | struct lttng_ht_node_str *ua_chan_node; |
5853 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
5854 | struct ust_app_channel *ua_chan = NULL; |
5855 | struct ust_app_session *ua_sess; | |
5856 | struct ust_app *app; | |
5857 | ||
88e3c2f5 | 5858 | assert(usess->active); |
0498a00c | 5859 | |
55cc08a6 | 5860 | rcu_read_lock(); |
852d0037 | 5861 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
5862 | if (!app->compatible) { |
5863 | /* | |
5864 | * TODO: In time, we should notice the caller of this error by | |
5865 | * telling him that this is a version error. | |
5866 | */ | |
5867 | continue; | |
5868 | } | |
55cc08a6 DG |
5869 | ua_sess = lookup_session_by_app(usess, app); |
5870 | if (ua_sess == NULL) { | |
5871 | continue; | |
5872 | } | |
5873 | ||
d0b96690 | 5874 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
5875 | |
5876 | if (ua_sess->deleted) { | |
5877 | pthread_mutex_unlock(&ua_sess->lock); | |
5878 | continue; | |
5879 | } | |
5880 | ||
55cc08a6 | 5881 | /* Lookup channel in the ust app session */ |
bec39940 DG |
5882 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
5883 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 | 5884 | if (ua_chan_node == NULL) { |
d0b96690 | 5885 | goto next_app; |
55cc08a6 DG |
5886 | } |
5887 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
5888 | node); | |
c9edf082 | 5889 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
55cc08a6 | 5890 | if (ret < 0) { |
d0b96690 | 5891 | goto next_app; |
55cc08a6 | 5892 | } |
d0b96690 DG |
5893 | next_app: |
5894 | pthread_mutex_unlock(&ua_sess->lock); | |
55cc08a6 DG |
5895 | } |
5896 | ||
55cc08a6 | 5897 | rcu_read_unlock(); |
76d45b40 DG |
5898 | return ret; |
5899 | } | |
7f79d3a1 | 5900 | |
d0b96690 DG |
5901 | /* |
5902 | * Receive registration and populate the given msg structure. | |
5903 | * | |
5904 | * On success return 0 else a negative value returned by the ustctl call. | |
5905 | */ | |
5906 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) | |
5907 | { | |
5908 | int ret; | |
5909 | uint32_t pid, ppid, uid, gid; | |
5910 | ||
5911 | assert(msg); | |
5912 | ||
5913 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, | |
5914 | &pid, &ppid, &uid, &gid, | |
5915 | &msg->bits_per_long, | |
5916 | &msg->uint8_t_alignment, | |
5917 | &msg->uint16_t_alignment, | |
5918 | &msg->uint32_t_alignment, | |
5919 | &msg->uint64_t_alignment, | |
5920 | &msg->long_alignment, | |
5921 | &msg->byte_order, | |
5922 | msg->name); | |
5923 | if (ret < 0) { | |
5924 | switch (-ret) { | |
5925 | case EPIPE: | |
5926 | case ECONNRESET: | |
5927 | case LTTNG_UST_ERR_EXITING: | |
5928 | DBG3("UST app recv reg message failed. Application died"); | |
5929 | break; | |
5930 | case LTTNG_UST_ERR_UNSUP_MAJOR: | |
5931 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", | |
5932 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, | |
5933 | LTTNG_UST_ABI_MINOR_VERSION); | |
5934 | break; | |
5935 | default: | |
5936 | ERR("UST app recv reg message failed with ret %d", ret); | |
5937 | break; | |
5938 | } | |
5939 | goto error; | |
5940 | } | |
5941 | msg->pid = (pid_t) pid; | |
5942 | msg->ppid = (pid_t) ppid; | |
5943 | msg->uid = (uid_t) uid; | |
5944 | msg->gid = (gid_t) gid; | |
5945 | ||
5946 | error: | |
5947 | return ret; | |
5948 | } | |
5949 | ||
10b56aef MD |
5950 | /* |
5951 | * Return a ust app session object using the application object and the | |
5952 | * session object descriptor has a key. If not found, NULL is returned. | |
5953 | * A RCU read side lock MUST be acquired when calling this function. | |
5954 | */ | |
5955 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, | |
5956 | int objd) | |
5957 | { | |
5958 | struct lttng_ht_node_ulong *node; | |
5959 | struct lttng_ht_iter iter; | |
5960 | struct ust_app_session *ua_sess = NULL; | |
5961 | ||
5962 | assert(app); | |
5963 | ||
5964 | lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter); | |
5965 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5966 | if (node == NULL) { | |
5967 | DBG2("UST app session find by objd %d not found", objd); | |
5968 | goto error; | |
5969 | } | |
5970 | ||
5971 | ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node); | |
5972 | ||
5973 | error: | |
5974 | return ua_sess; | |
5975 | } | |
5976 | ||
d88aee68 DG |
5977 | /* |
5978 | * Return a ust app channel object using the application object and the channel | |
5979 | * object descriptor has a key. If not found, NULL is returned. A RCU read side | |
5980 | * lock MUST be acquired before calling this function. | |
5981 | */ | |
d0b96690 DG |
5982 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
5983 | int objd) | |
5984 | { | |
5985 | struct lttng_ht_node_ulong *node; | |
5986 | struct lttng_ht_iter iter; | |
5987 | struct ust_app_channel *ua_chan = NULL; | |
5988 | ||
5989 | assert(app); | |
5990 | ||
5991 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); | |
5992 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5993 | if (node == NULL) { | |
5994 | DBG2("UST app channel find by objd %d not found", objd); | |
5995 | goto error; | |
5996 | } | |
5997 | ||
5998 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); | |
5999 | ||
6000 | error: | |
6001 | return ua_chan; | |
6002 | } | |
6003 | ||
d88aee68 DG |
6004 | /* |
6005 | * Reply to a register channel notification from an application on the notify | |
6006 | * socket. The channel metadata is also created. | |
6007 | * | |
6008 | * The session UST registry lock is acquired in this function. | |
6009 | * | |
6010 | * On success 0 is returned else a negative value. | |
6011 | */ | |
8eede835 | 6012 | static int reply_ust_register_channel(int sock, int cobjd, |
d0b96690 DG |
6013 | size_t nr_fields, struct ustctl_field *fields) |
6014 | { | |
6015 | int ret, ret_code = 0; | |
294e218e | 6016 | uint32_t chan_id; |
7972aab2 | 6017 | uint64_t chan_reg_key; |
d0b96690 DG |
6018 | enum ustctl_channel_header type; |
6019 | struct ust_app *app; | |
6020 | struct ust_app_channel *ua_chan; | |
6021 | struct ust_app_session *ua_sess; | |
7972aab2 | 6022 | struct ust_registry_session *registry; |
45893984 | 6023 | struct ust_registry_channel *chan_reg; |
d0b96690 DG |
6024 | |
6025 | rcu_read_lock(); | |
6026 | ||
6027 | /* Lookup application. If not found, there is a code flow error. */ | |
6028 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6029 | if (!app) { |
fad1ed2f | 6030 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 DG |
6031 | sock); |
6032 | ret = 0; | |
6033 | goto error_rcu_unlock; | |
6034 | } | |
d0b96690 | 6035 | |
4950b860 | 6036 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6037 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6038 | if (!ua_chan) { |
fad1ed2f | 6039 | DBG("Application channel is being torn down. Abort event notify"); |
4950b860 MD |
6040 | ret = 0; |
6041 | goto error_rcu_unlock; | |
6042 | } | |
6043 | ||
d0b96690 DG |
6044 | assert(ua_chan->session); |
6045 | ua_sess = ua_chan->session; | |
d0b96690 | 6046 | |
7972aab2 DG |
6047 | /* Get right session registry depending on the session buffer type. */ |
6048 | registry = get_session_registry(ua_sess); | |
fad1ed2f JR |
6049 | if (!registry) { |
6050 | DBG("Application session is being torn down. Abort event notify"); | |
6051 | ret = 0; | |
6052 | goto error_rcu_unlock; | |
6053 | }; | |
45893984 | 6054 | |
7972aab2 DG |
6055 | /* Depending on the buffer type, a different channel key is used. */ |
6056 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
6057 | chan_reg_key = ua_chan->tracing_channel_id; | |
d0b96690 | 6058 | } else { |
7972aab2 | 6059 | chan_reg_key = ua_chan->key; |
d0b96690 DG |
6060 | } |
6061 | ||
7972aab2 DG |
6062 | pthread_mutex_lock(®istry->lock); |
6063 | ||
6064 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); | |
6065 | assert(chan_reg); | |
6066 | ||
6067 | if (!chan_reg->register_done) { | |
294e218e MD |
6068 | /* |
6069 | * TODO: eventually use the registry event count for | |
6070 | * this channel to better guess header type for per-pid | |
6071 | * buffers. | |
6072 | */ | |
6073 | type = USTCTL_CHANNEL_HEADER_LARGE; | |
7972aab2 DG |
6074 | chan_reg->nr_ctx_fields = nr_fields; |
6075 | chan_reg->ctx_fields = fields; | |
fad1ed2f | 6076 | fields = NULL; |
7972aab2 | 6077 | chan_reg->header_type = type; |
d0b96690 | 6078 | } else { |
7972aab2 DG |
6079 | /* Get current already assigned values. */ |
6080 | type = chan_reg->header_type; | |
d0b96690 | 6081 | } |
7972aab2 DG |
6082 | /* Channel id is set during the object creation. */ |
6083 | chan_id = chan_reg->chan_id; | |
d0b96690 DG |
6084 | |
6085 | /* Append to metadata */ | |
7972aab2 DG |
6086 | if (!chan_reg->metadata_dumped) { |
6087 | ret_code = ust_metadata_channel_statedump(registry, chan_reg); | |
d0b96690 DG |
6088 | if (ret_code) { |
6089 | ERR("Error appending channel metadata (errno = %d)", ret_code); | |
6090 | goto reply; | |
6091 | } | |
6092 | } | |
6093 | ||
6094 | reply: | |
7972aab2 DG |
6095 | DBG3("UST app replying to register channel key %" PRIu64 |
6096 | " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type, | |
6097 | ret_code); | |
d0b96690 DG |
6098 | |
6099 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); | |
6100 | if (ret < 0) { | |
6101 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6102 | ERR("UST app reply channel failed with ret %d", ret); | |
6103 | } else { | |
6104 | DBG3("UST app reply channel failed. Application died"); | |
6105 | } | |
6106 | goto error; | |
6107 | } | |
6108 | ||
7972aab2 DG |
6109 | /* This channel registry registration is completed. */ |
6110 | chan_reg->register_done = 1; | |
6111 | ||
d0b96690 | 6112 | error: |
7972aab2 | 6113 | pthread_mutex_unlock(®istry->lock); |
d88aee68 | 6114 | error_rcu_unlock: |
d0b96690 | 6115 | rcu_read_unlock(); |
fad1ed2f | 6116 | free(fields); |
d0b96690 DG |
6117 | return ret; |
6118 | } | |
6119 | ||
d88aee68 DG |
6120 | /* |
6121 | * Add event to the UST channel registry. When the event is added to the | |
6122 | * registry, the metadata is also created. Once done, this replies to the | |
6123 | * application with the appropriate error code. | |
6124 | * | |
6125 | * The session UST registry lock is acquired in the function. | |
6126 | * | |
6127 | * On success 0 is returned else a negative value. | |
6128 | */ | |
d0b96690 | 6129 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
2106efa0 PP |
6130 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
6131 | int loglevel_value, char *model_emf_uri) | |
d0b96690 DG |
6132 | { |
6133 | int ret, ret_code; | |
6134 | uint32_t event_id = 0; | |
7972aab2 | 6135 | uint64_t chan_reg_key; |
d0b96690 DG |
6136 | struct ust_app *app; |
6137 | struct ust_app_channel *ua_chan; | |
6138 | struct ust_app_session *ua_sess; | |
7972aab2 | 6139 | struct ust_registry_session *registry; |
d0b96690 DG |
6140 | |
6141 | rcu_read_lock(); | |
6142 | ||
6143 | /* Lookup application. If not found, there is a code flow error. */ | |
6144 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6145 | if (!app) { |
fad1ed2f | 6146 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 DG |
6147 | sock); |
6148 | ret = 0; | |
6149 | goto error_rcu_unlock; | |
6150 | } | |
d0b96690 | 6151 | |
4950b860 | 6152 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6153 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6154 | if (!ua_chan) { |
fad1ed2f | 6155 | DBG("Application channel is being torn down. Abort event notify"); |
4950b860 MD |
6156 | ret = 0; |
6157 | goto error_rcu_unlock; | |
6158 | } | |
6159 | ||
d0b96690 DG |
6160 | assert(ua_chan->session); |
6161 | ua_sess = ua_chan->session; | |
6162 | ||
7972aab2 | 6163 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
6164 | if (!registry) { |
6165 | DBG("Application session is being torn down. Abort event notify"); | |
6166 | ret = 0; | |
6167 | goto error_rcu_unlock; | |
6168 | } | |
7972aab2 DG |
6169 | |
6170 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
6171 | chan_reg_key = ua_chan->tracing_channel_id; | |
6172 | } else { | |
6173 | chan_reg_key = ua_chan->key; | |
6174 | } | |
6175 | ||
6176 | pthread_mutex_lock(®istry->lock); | |
d0b96690 | 6177 | |
d5d629b5 DG |
6178 | /* |
6179 | * From this point on, this call acquires the ownership of the sig, fields | |
6180 | * and model_emf_uri meaning any free are done inside it if needed. These | |
6181 | * three variables MUST NOT be read/write after this. | |
6182 | */ | |
7972aab2 | 6183 | ret_code = ust_registry_create_event(registry, chan_reg_key, |
2106efa0 PP |
6184 | sobjd, cobjd, name, sig, nr_fields, fields, |
6185 | loglevel_value, model_emf_uri, ua_sess->buffer_type, | |
6186 | &event_id, app); | |
fad1ed2f JR |
6187 | sig = NULL; |
6188 | fields = NULL; | |
6189 | model_emf_uri = NULL; | |
d0b96690 DG |
6190 | |
6191 | /* | |
6192 | * The return value is returned to ustctl so in case of an error, the | |
6193 | * application can be notified. In case of an error, it's important not to | |
6194 | * return a negative error or else the application will get closed. | |
6195 | */ | |
6196 | ret = ustctl_reply_register_event(sock, event_id, ret_code); | |
6197 | if (ret < 0) { | |
6198 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6199 | ERR("UST app reply event failed with ret %d", ret); | |
6200 | } else { | |
6201 | DBG3("UST app reply event failed. Application died"); | |
6202 | } | |
6203 | /* | |
6204 | * No need to wipe the create event since the application socket will | |
6205 | * get close on error hence cleaning up everything by itself. | |
6206 | */ | |
6207 | goto error; | |
6208 | } | |
6209 | ||
7972aab2 DG |
6210 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
6211 | name, event_id); | |
d88aee68 | 6212 | |
d0b96690 | 6213 | error: |
7972aab2 | 6214 | pthread_mutex_unlock(®istry->lock); |
d88aee68 | 6215 | error_rcu_unlock: |
d0b96690 | 6216 | rcu_read_unlock(); |
fad1ed2f JR |
6217 | free(sig); |
6218 | free(fields); | |
6219 | free(model_emf_uri); | |
d0b96690 DG |
6220 | return ret; |
6221 | } | |
6222 | ||
10b56aef MD |
6223 | /* |
6224 | * Add enum to the UST session registry. Once done, this replies to the | |
6225 | * application with the appropriate error code. | |
6226 | * | |
6227 | * The session UST registry lock is acquired within this function. | |
6228 | * | |
6229 | * On success 0 is returned else a negative value. | |
6230 | */ | |
6231 | static int add_enum_ust_registry(int sock, int sobjd, char *name, | |
6232 | struct ustctl_enum_entry *entries, size_t nr_entries) | |
6233 | { | |
6234 | int ret = 0, ret_code; | |
6235 | struct ust_app *app; | |
6236 | struct ust_app_session *ua_sess; | |
6237 | struct ust_registry_session *registry; | |
6238 | uint64_t enum_id = -1ULL; | |
6239 | ||
6240 | rcu_read_lock(); | |
6241 | ||
6242 | /* Lookup application. If not found, there is a code flow error. */ | |
6243 | app = find_app_by_notify_sock(sock); | |
6244 | if (!app) { | |
6245 | /* Return an error since this is not an error */ | |
6246 | DBG("Application socket %d is being torn down. Aborting enum registration", | |
6247 | sock); | |
6248 | free(entries); | |
6249 | goto error_rcu_unlock; | |
6250 | } | |
6251 | ||
6252 | /* Lookup session by UST object descriptor. */ | |
6253 | ua_sess = find_session_by_objd(app, sobjd); | |
6254 | if (!ua_sess) { | |
6255 | /* Return an error since this is not an error */ | |
fad1ed2f | 6256 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
10b56aef MD |
6257 | free(entries); |
6258 | goto error_rcu_unlock; | |
6259 | } | |
6260 | ||
6261 | registry = get_session_registry(ua_sess); | |
fad1ed2f JR |
6262 | if (!registry) { |
6263 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); | |
6264 | free(entries); | |
6265 | goto error_rcu_unlock; | |
6266 | } | |
10b56aef MD |
6267 | |
6268 | pthread_mutex_lock(®istry->lock); | |
6269 | ||
6270 | /* | |
6271 | * From this point on, the callee acquires the ownership of | |
6272 | * entries. The variable entries MUST NOT be read/written after | |
6273 | * call. | |
6274 | */ | |
6275 | ret_code = ust_registry_create_or_find_enum(registry, sobjd, name, | |
6276 | entries, nr_entries, &enum_id); | |
6277 | entries = NULL; | |
6278 | ||
6279 | /* | |
6280 | * The return value is returned to ustctl so in case of an error, the | |
6281 | * application can be notified. In case of an error, it's important not to | |
6282 | * return a negative error or else the application will get closed. | |
6283 | */ | |
6284 | ret = ustctl_reply_register_enum(sock, enum_id, ret_code); | |
6285 | if (ret < 0) { | |
6286 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6287 | ERR("UST app reply enum failed with ret %d", ret); | |
6288 | } else { | |
6289 | DBG3("UST app reply enum failed. Application died"); | |
6290 | } | |
6291 | /* | |
6292 | * No need to wipe the create enum since the application socket will | |
6293 | * get close on error hence cleaning up everything by itself. | |
6294 | */ | |
6295 | goto error; | |
6296 | } | |
6297 | ||
6298 | DBG3("UST registry enum %s added successfully or already found", name); | |
6299 | ||
6300 | error: | |
6301 | pthread_mutex_unlock(®istry->lock); | |
6302 | error_rcu_unlock: | |
6303 | rcu_read_unlock(); | |
6304 | return ret; | |
6305 | } | |
6306 | ||
d88aee68 DG |
6307 | /* |
6308 | * Handle application notification through the given notify socket. | |
6309 | * | |
6310 | * Return 0 on success or else a negative value. | |
6311 | */ | |
d0b96690 DG |
6312 | int ust_app_recv_notify(int sock) |
6313 | { | |
6314 | int ret; | |
6315 | enum ustctl_notify_cmd cmd; | |
6316 | ||
6317 | DBG3("UST app receiving notify from sock %d", sock); | |
6318 | ||
6319 | ret = ustctl_recv_notify(sock, &cmd); | |
6320 | if (ret < 0) { | |
6321 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6322 | ERR("UST app recv notify failed with ret %d", ret); | |
6323 | } else { | |
6324 | DBG3("UST app recv notify failed. Application died"); | |
6325 | } | |
6326 | goto error; | |
6327 | } | |
6328 | ||
6329 | switch (cmd) { | |
6330 | case USTCTL_NOTIFY_CMD_EVENT: | |
6331 | { | |
2106efa0 | 6332 | int sobjd, cobjd, loglevel_value; |
d0b96690 DG |
6333 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
6334 | size_t nr_fields; | |
6335 | struct ustctl_field *fields; | |
6336 | ||
6337 | DBG2("UST app ustctl register event received"); | |
6338 | ||
2106efa0 PP |
6339 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, |
6340 | &loglevel_value, &sig, &nr_fields, &fields, | |
6341 | &model_emf_uri); | |
d0b96690 DG |
6342 | if (ret < 0) { |
6343 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6344 | ERR("UST app recv event failed with ret %d", ret); | |
6345 | } else { | |
6346 | DBG3("UST app recv event failed. Application died"); | |
6347 | } | |
6348 | goto error; | |
6349 | } | |
6350 | ||
d5d629b5 DG |
6351 | /* |
6352 | * Add event to the UST registry coming from the notify socket. This | |
6353 | * call will free if needed the sig, fields and model_emf_uri. This | |
6354 | * code path loses the ownsership of these variables and transfer them | |
6355 | * to the this function. | |
6356 | */ | |
d0b96690 | 6357 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
2106efa0 | 6358 | fields, loglevel_value, model_emf_uri); |
d0b96690 DG |
6359 | if (ret < 0) { |
6360 | goto error; | |
6361 | } | |
6362 | ||
6363 | break; | |
6364 | } | |
6365 | case USTCTL_NOTIFY_CMD_CHANNEL: | |
6366 | { | |
6367 | int sobjd, cobjd; | |
6368 | size_t nr_fields; | |
6369 | struct ustctl_field *fields; | |
6370 | ||
6371 | DBG2("UST app ustctl register channel received"); | |
6372 | ||
6373 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, | |
6374 | &fields); | |
6375 | if (ret < 0) { | |
6376 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6377 | ERR("UST app recv channel failed with ret %d", ret); | |
6378 | } else { | |
6379 | DBG3("UST app recv channel failed. Application died"); | |
6380 | } | |
6381 | goto error; | |
6382 | } | |
6383 | ||
d5d629b5 DG |
6384 | /* |
6385 | * The fields ownership are transfered to this function call meaning | |
6386 | * that if needed it will be freed. After this, it's invalid to access | |
6387 | * fields or clean it up. | |
6388 | */ | |
8eede835 | 6389 | ret = reply_ust_register_channel(sock, cobjd, nr_fields, |
d0b96690 DG |
6390 | fields); |
6391 | if (ret < 0) { | |
6392 | goto error; | |
6393 | } | |
6394 | ||
6395 | break; | |
6396 | } | |
10b56aef MD |
6397 | case USTCTL_NOTIFY_CMD_ENUM: |
6398 | { | |
6399 | int sobjd; | |
6400 | char name[LTTNG_UST_SYM_NAME_LEN]; | |
6401 | size_t nr_entries; | |
6402 | struct ustctl_enum_entry *entries; | |
6403 | ||
6404 | DBG2("UST app ustctl register enum received"); | |
6405 | ||
6406 | ret = ustctl_recv_register_enum(sock, &sobjd, name, | |
6407 | &entries, &nr_entries); | |
6408 | if (ret < 0) { | |
6409 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
6410 | ERR("UST app recv enum failed with ret %d", ret); | |
6411 | } else { | |
6412 | DBG3("UST app recv enum failed. Application died"); | |
6413 | } | |
6414 | goto error; | |
6415 | } | |
6416 | ||
6417 | /* Callee assumes ownership of entries */ | |
6418 | ret = add_enum_ust_registry(sock, sobjd, name, | |
6419 | entries, nr_entries); | |
6420 | if (ret < 0) { | |
6421 | goto error; | |
6422 | } | |
6423 | ||
6424 | break; | |
6425 | } | |
d0b96690 DG |
6426 | default: |
6427 | /* Should NEVER happen. */ | |
6428 | assert(0); | |
6429 | } | |
6430 | ||
6431 | error: | |
6432 | return ret; | |
6433 | } | |
d88aee68 DG |
6434 | |
6435 | /* | |
6436 | * Once the notify socket hangs up, this is called. First, it tries to find the | |
6437 | * corresponding application. On failure, the call_rcu to close the socket is | |
6438 | * executed. If an application is found, it tries to delete it from the notify | |
6439 | * socket hash table. Whathever the result, it proceeds to the call_rcu. | |
6440 | * | |
6441 | * Note that an object needs to be allocated here so on ENOMEM failure, the | |
6442 | * call RCU is not done but the rest of the cleanup is. | |
6443 | */ | |
6444 | void ust_app_notify_sock_unregister(int sock) | |
6445 | { | |
6446 | int err_enomem = 0; | |
6447 | struct lttng_ht_iter iter; | |
6448 | struct ust_app *app; | |
6449 | struct ust_app_notify_sock_obj *obj; | |
6450 | ||
6451 | assert(sock >= 0); | |
6452 | ||
6453 | rcu_read_lock(); | |
6454 | ||
6455 | obj = zmalloc(sizeof(*obj)); | |
6456 | if (!obj) { | |
6457 | /* | |
6458 | * An ENOMEM is kind of uncool. If this strikes we continue the | |
6459 | * procedure but the call_rcu will not be called. In this case, we | |
6460 | * accept the fd leak rather than possibly creating an unsynchronized | |
6461 | * state between threads. | |
6462 | * | |
6463 | * TODO: The notify object should be created once the notify socket is | |
6464 | * registered and stored independantely from the ust app object. The | |
6465 | * tricky part is to synchronize the teardown of the application and | |
6466 | * this notify object. Let's keep that in mind so we can avoid this | |
6467 | * kind of shenanigans with ENOMEM in the teardown path. | |
6468 | */ | |
6469 | err_enomem = 1; | |
6470 | } else { | |
6471 | obj->fd = sock; | |
6472 | } | |
6473 | ||
6474 | DBG("UST app notify socket unregister %d", sock); | |
6475 | ||
6476 | /* | |
6477 | * Lookup application by notify socket. If this fails, this means that the | |
6478 | * hash table delete has already been done by the application | |
6479 | * unregistration process so we can safely close the notify socket in a | |
6480 | * call RCU. | |
6481 | */ | |
6482 | app = find_app_by_notify_sock(sock); | |
6483 | if (!app) { | |
6484 | goto close_socket; | |
6485 | } | |
6486 | ||
6487 | iter.iter.node = &app->notify_sock_n.node; | |
6488 | ||
6489 | /* | |
6490 | * Whatever happens here either we fail or succeed, in both cases we have | |
6491 | * to close the socket after a grace period to continue to the call RCU | |
6492 | * here. If the deletion is successful, the application is not visible | |
6493 | * anymore by other threads and is it fails it means that it was already | |
6494 | * deleted from the hash table so either way we just have to close the | |
6495 | * socket. | |
6496 | */ | |
6497 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
6498 | ||
6499 | close_socket: | |
6500 | rcu_read_unlock(); | |
6501 | ||
6502 | /* | |
6503 | * Close socket after a grace period to avoid for the socket to be reused | |
6504 | * before the application object is freed creating potential race between | |
6505 | * threads trying to add unique in the global hash table. | |
6506 | */ | |
6507 | if (!err_enomem) { | |
6508 | call_rcu(&obj->head, close_notify_sock_rcu); | |
6509 | } | |
6510 | } | |
f45e313d DG |
6511 | |
6512 | /* | |
6513 | * Destroy a ust app data structure and free its memory. | |
6514 | */ | |
6515 | void ust_app_destroy(struct ust_app *app) | |
6516 | { | |
6517 | if (!app) { | |
6518 | return; | |
6519 | } | |
6520 | ||
6521 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
6522 | } | |
6dc3064a DG |
6523 | |
6524 | /* | |
6525 | * Take a snapshot for a given UST session. The snapshot is sent to the given | |
6526 | * output. | |
6527 | * | |
9a654598 | 6528 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 6529 | */ |
fb9a95c4 JG |
6530 | enum lttng_error_code ust_app_snapshot_record( |
6531 | const struct ltt_ust_session *usess, | |
348a81dc | 6532 | const struct consumer_output *output, int wait, |
d07ceecd | 6533 | uint64_t nb_packets_per_stream) |
6dc3064a DG |
6534 | { |
6535 | int ret = 0; | |
9a654598 | 6536 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
6537 | struct lttng_ht_iter iter; |
6538 | struct ust_app *app; | |
affce97e | 6539 | char *trace_path = NULL; |
6dc3064a DG |
6540 | |
6541 | assert(usess); | |
6542 | assert(output); | |
6543 | ||
6544 | rcu_read_lock(); | |
6545 | ||
8c924c7b MD |
6546 | switch (usess->buffer_type) { |
6547 | case LTTNG_BUFFER_PER_UID: | |
6548 | { | |
6549 | struct buffer_reg_uid *reg; | |
6dc3064a | 6550 | |
8c924c7b MD |
6551 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
6552 | struct buffer_reg_channel *reg_chan; | |
6553 | struct consumer_socket *socket; | |
3b967712 | 6554 | char pathname[PATH_MAX]; |
5da88b0f | 6555 | size_t consumer_path_offset = 0; |
6dc3064a | 6556 | |
2b269489 JR |
6557 | if (!reg->registry->reg.ust->metadata_key) { |
6558 | /* Skip since no metadata is present */ | |
6559 | continue; | |
6560 | } | |
6561 | ||
8c924c7b MD |
6562 | /* Get consumer socket to use to push the metadata.*/ |
6563 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
6564 | usess->consumer); | |
6565 | if (!socket) { | |
9a654598 | 6566 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
6567 | goto error; |
6568 | } | |
6dc3064a | 6569 | |
8c924c7b MD |
6570 | memset(pathname, 0, sizeof(pathname)); |
6571 | ret = snprintf(pathname, sizeof(pathname), | |
5da88b0f | 6572 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, |
8c924c7b MD |
6573 | reg->uid, reg->bits_per_long); |
6574 | if (ret < 0) { | |
6575 | PERROR("snprintf snapshot path"); | |
9a654598 | 6576 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
6577 | goto error; |
6578 | } | |
affce97e JG |
6579 | /* Free path allowed on previous iteration. */ |
6580 | free(trace_path); | |
5da88b0f MD |
6581 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
6582 | &consumer_path_offset); | |
3b967712 MD |
6583 | if (!trace_path) { |
6584 | status = LTTNG_ERR_INVALID; | |
6585 | goto error; | |
6586 | } | |
f3db82be | 6587 | /* Add the UST default trace dir to path. */ |
8c924c7b MD |
6588 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
6589 | reg_chan, node.node) { | |
9a654598 | 6590 | status = consumer_snapshot_channel(socket, |
e098433c JG |
6591 | reg_chan->consumer_key, |
6592 | output, 0, usess->uid, | |
5da88b0f | 6593 | usess->gid, &trace_path[consumer_path_offset], wait, |
d2956687 | 6594 | nb_packets_per_stream); |
9a654598 | 6595 | if (status != LTTNG_OK) { |
8c924c7b MD |
6596 | goto error; |
6597 | } | |
6598 | } | |
9a654598 | 6599 | status = consumer_snapshot_channel(socket, |
68808f4e | 6600 | reg->registry->reg.ust->metadata_key, output, 1, |
5da88b0f MD |
6601 | usess->uid, usess->gid, &trace_path[consumer_path_offset], |
6602 | wait, 0); | |
9a654598 | 6603 | if (status != LTTNG_OK) { |
8c924c7b MD |
6604 | goto error; |
6605 | } | |
af706bb7 | 6606 | } |
8c924c7b MD |
6607 | break; |
6608 | } | |
6609 | case LTTNG_BUFFER_PER_PID: | |
6610 | { | |
6611 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6612 | struct consumer_socket *socket; | |
6613 | struct lttng_ht_iter chan_iter; | |
6614 | struct ust_app_channel *ua_chan; | |
6615 | struct ust_app_session *ua_sess; | |
6616 | struct ust_registry_session *registry; | |
3b967712 | 6617 | char pathname[PATH_MAX]; |
5da88b0f | 6618 | size_t consumer_path_offset = 0; |
8c924c7b MD |
6619 | |
6620 | ua_sess = lookup_session_by_app(usess, app); | |
6621 | if (!ua_sess) { | |
6622 | /* Session not associated with this app. */ | |
6623 | continue; | |
6624 | } | |
af706bb7 | 6625 | |
8c924c7b MD |
6626 | /* Get the right consumer socket for the application. */ |
6627 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
348a81dc | 6628 | output); |
8c924c7b | 6629 | if (!socket) { |
9a654598 | 6630 | status = LTTNG_ERR_INVALID; |
5c786ded JD |
6631 | goto error; |
6632 | } | |
6633 | ||
8c924c7b MD |
6634 | /* Add the UST default trace dir to path. */ |
6635 | memset(pathname, 0, sizeof(pathname)); | |
5da88b0f | 6636 | ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", |
8c924c7b | 6637 | ua_sess->path); |
6dc3064a | 6638 | if (ret < 0) { |
9a654598 | 6639 | status = LTTNG_ERR_INVALID; |
8c924c7b | 6640 | PERROR("snprintf snapshot path"); |
6dc3064a DG |
6641 | goto error; |
6642 | } | |
affce97e JG |
6643 | /* Free path allowed on previous iteration. */ |
6644 | free(trace_path); | |
5da88b0f MD |
6645 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
6646 | &consumer_path_offset); | |
3b967712 MD |
6647 | if (!trace_path) { |
6648 | status = LTTNG_ERR_INVALID; | |
6649 | goto error; | |
6650 | } | |
f3db82be | 6651 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
8c924c7b | 6652 | ua_chan, node.node) { |
9a654598 | 6653 | status = consumer_snapshot_channel(socket, |
470cc211 | 6654 | ua_chan->key, output, 0, |
ff588497 JR |
6655 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
6656 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
5da88b0f | 6657 | &trace_path[consumer_path_offset], wait, |
d2956687 | 6658 | nb_packets_per_stream); |
9a654598 JG |
6659 | switch (status) { |
6660 | case LTTNG_OK: | |
6661 | break; | |
6662 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
6663 | continue; | |
6664 | default: | |
8c924c7b MD |
6665 | goto error; |
6666 | } | |
6667 | } | |
6668 | ||
6669 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 6670 | if (!registry) { |
9bbfb88c MD |
6671 | DBG("Application session is being torn down. Skip application."); |
6672 | continue; | |
fad1ed2f | 6673 | } |
9a654598 | 6674 | status = consumer_snapshot_channel(socket, |
470cc211 | 6675 | registry->metadata_key, output, 1, |
ff588497 JR |
6676 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
6677 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
5da88b0f | 6678 | &trace_path[consumer_path_offset], wait, 0); |
9a654598 JG |
6679 | switch (status) { |
6680 | case LTTNG_OK: | |
6681 | break; | |
6682 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
6683 | continue; | |
6684 | default: | |
8c924c7b MD |
6685 | goto error; |
6686 | } | |
6687 | } | |
6688 | break; | |
6689 | } | |
6690 | default: | |
6691 | assert(0); | |
6692 | break; | |
6dc3064a DG |
6693 | } |
6694 | ||
6695 | error: | |
affce97e | 6696 | free(trace_path); |
6dc3064a | 6697 | rcu_read_unlock(); |
9a654598 | 6698 | return status; |
6dc3064a | 6699 | } |
5c786ded JD |
6700 | |
6701 | /* | |
d07ceecd | 6702 | * Return the size taken by one more packet per stream. |
5c786ded | 6703 | */ |
fb9a95c4 JG |
6704 | uint64_t ust_app_get_size_one_more_packet_per_stream( |
6705 | const struct ltt_ust_session *usess, uint64_t cur_nr_packets) | |
5c786ded | 6706 | { |
d07ceecd | 6707 | uint64_t tot_size = 0; |
5c786ded JD |
6708 | struct ust_app *app; |
6709 | struct lttng_ht_iter iter; | |
6710 | ||
6711 | assert(usess); | |
6712 | ||
6713 | switch (usess->buffer_type) { | |
6714 | case LTTNG_BUFFER_PER_UID: | |
6715 | { | |
6716 | struct buffer_reg_uid *reg; | |
6717 | ||
6718 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
6719 | struct buffer_reg_channel *reg_chan; | |
6720 | ||
b7064eaa | 6721 | rcu_read_lock(); |
5c786ded JD |
6722 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
6723 | reg_chan, node.node) { | |
d07ceecd MD |
6724 | if (cur_nr_packets >= reg_chan->num_subbuf) { |
6725 | /* | |
6726 | * Don't take channel into account if we | |
6727 | * already grab all its packets. | |
6728 | */ | |
6729 | continue; | |
6730 | } | |
6731 | tot_size += reg_chan->subbuf_size * reg_chan->stream_count; | |
5c786ded | 6732 | } |
b7064eaa | 6733 | rcu_read_unlock(); |
5c786ded JD |
6734 | } |
6735 | break; | |
6736 | } | |
6737 | case LTTNG_BUFFER_PER_PID: | |
6738 | { | |
6739 | rcu_read_lock(); | |
6740 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6741 | struct ust_app_channel *ua_chan; | |
6742 | struct ust_app_session *ua_sess; | |
6743 | struct lttng_ht_iter chan_iter; | |
6744 | ||
6745 | ua_sess = lookup_session_by_app(usess, app); | |
6746 | if (!ua_sess) { | |
6747 | /* Session not associated with this app. */ | |
6748 | continue; | |
6749 | } | |
6750 | ||
6751 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
6752 | ua_chan, node.node) { | |
d07ceecd MD |
6753 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
6754 | /* | |
6755 | * Don't take channel into account if we | |
6756 | * already grab all its packets. | |
6757 | */ | |
6758 | continue; | |
6759 | } | |
6760 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; | |
5c786ded JD |
6761 | } |
6762 | } | |
6763 | rcu_read_unlock(); | |
6764 | break; | |
6765 | } | |
6766 | default: | |
6767 | assert(0); | |
6768 | break; | |
6769 | } | |
6770 | ||
d07ceecd | 6771 | return tot_size; |
5c786ded | 6772 | } |
fb83fe64 JD |
6773 | |
6774 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, | |
6775 | struct cds_list_head *buffer_reg_uid_list, | |
6776 | struct consumer_output *consumer, uint64_t uchan_id, | |
6777 | int overwrite, uint64_t *discarded, uint64_t *lost) | |
6778 | { | |
6779 | int ret; | |
6780 | uint64_t consumer_chan_key; | |
6781 | ||
70dd8162 MD |
6782 | *discarded = 0; |
6783 | *lost = 0; | |
6784 | ||
fb83fe64 | 6785 | ret = buffer_reg_uid_consumer_channel_key( |
76604852 | 6786 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
fb83fe64 | 6787 | if (ret < 0) { |
70dd8162 MD |
6788 | /* Not found */ |
6789 | ret = 0; | |
fb83fe64 JD |
6790 | goto end; |
6791 | } | |
6792 | ||
6793 | if (overwrite) { | |
6794 | ret = consumer_get_lost_packets(ust_session_id, | |
6795 | consumer_chan_key, consumer, lost); | |
6796 | } else { | |
6797 | ret = consumer_get_discarded_events(ust_session_id, | |
6798 | consumer_chan_key, consumer, discarded); | |
6799 | } | |
6800 | ||
6801 | end: | |
6802 | return ret; | |
6803 | } | |
6804 | ||
6805 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, | |
6806 | struct ltt_ust_channel *uchan, | |
6807 | struct consumer_output *consumer, int overwrite, | |
6808 | uint64_t *discarded, uint64_t *lost) | |
6809 | { | |
6810 | int ret = 0; | |
6811 | struct lttng_ht_iter iter; | |
6812 | struct lttng_ht_node_str *ua_chan_node; | |
6813 | struct ust_app *app; | |
6814 | struct ust_app_session *ua_sess; | |
6815 | struct ust_app_channel *ua_chan; | |
6816 | ||
70dd8162 MD |
6817 | *discarded = 0; |
6818 | *lost = 0; | |
6819 | ||
fb83fe64 JD |
6820 | rcu_read_lock(); |
6821 | /* | |
70dd8162 MD |
6822 | * Iterate over every registered applications. Sum counters for |
6823 | * all applications containing requested session and channel. | |
fb83fe64 JD |
6824 | */ |
6825 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6826 | struct lttng_ht_iter uiter; | |
6827 | ||
6828 | ua_sess = lookup_session_by_app(usess, app); | |
6829 | if (ua_sess == NULL) { | |
6830 | continue; | |
6831 | } | |
6832 | ||
6833 | /* Get channel */ | |
ee022399 | 6834 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
fb83fe64 JD |
6835 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
6836 | /* If the session is found for the app, the channel must be there */ | |
6837 | assert(ua_chan_node); | |
6838 | ||
6839 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
6840 | ||
6841 | if (overwrite) { | |
70dd8162 MD |
6842 | uint64_t _lost; |
6843 | ||
fb83fe64 | 6844 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, |
70dd8162 MD |
6845 | consumer, &_lost); |
6846 | if (ret < 0) { | |
6847 | break; | |
6848 | } | |
6849 | (*lost) += _lost; | |
fb83fe64 | 6850 | } else { |
70dd8162 MD |
6851 | uint64_t _discarded; |
6852 | ||
fb83fe64 | 6853 | ret = consumer_get_discarded_events(usess->id, |
70dd8162 MD |
6854 | ua_chan->key, consumer, &_discarded); |
6855 | if (ret < 0) { | |
6856 | break; | |
6857 | } | |
6858 | (*discarded) += _discarded; | |
fb83fe64 | 6859 | } |
fb83fe64 JD |
6860 | } |
6861 | ||
fb83fe64 JD |
6862 | rcu_read_unlock(); |
6863 | return ret; | |
6864 | } | |
c2561365 JD |
6865 | |
6866 | static | |
6867 | int ust_app_regenerate_statedump(struct ltt_ust_session *usess, | |
6868 | struct ust_app *app) | |
6869 | { | |
6870 | int ret = 0; | |
6871 | struct ust_app_session *ua_sess; | |
6872 | ||
6873 | DBG("Regenerating the metadata for ust app pid %d", app->pid); | |
6874 | ||
6875 | rcu_read_lock(); | |
6876 | ||
6877 | ua_sess = lookup_session_by_app(usess, app); | |
6878 | if (ua_sess == NULL) { | |
6879 | /* The session is in teardown process. Ignore and continue. */ | |
6880 | goto end; | |
6881 | } | |
6882 | ||
6883 | pthread_mutex_lock(&ua_sess->lock); | |
6884 | ||
6885 | if (ua_sess->deleted) { | |
6886 | goto end_unlock; | |
6887 | } | |
6888 | ||
6889 | pthread_mutex_lock(&app->sock_lock); | |
6890 | ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle); | |
6891 | pthread_mutex_unlock(&app->sock_lock); | |
6892 | ||
6893 | end_unlock: | |
6894 | pthread_mutex_unlock(&ua_sess->lock); | |
6895 | ||
6896 | end: | |
6897 | rcu_read_unlock(); | |
6898 | health_code_update(); | |
6899 | return ret; | |
6900 | } | |
6901 | ||
6902 | /* | |
6903 | * Regenerate the statedump for each app in the session. | |
6904 | */ | |
6905 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) | |
6906 | { | |
6907 | int ret = 0; | |
6908 | struct lttng_ht_iter iter; | |
6909 | struct ust_app *app; | |
6910 | ||
6911 | DBG("Regenerating the metadata for all UST apps"); | |
6912 | ||
6913 | rcu_read_lock(); | |
6914 | ||
6915 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6916 | if (!app->compatible) { | |
6917 | continue; | |
6918 | } | |
6919 | ||
6920 | ret = ust_app_regenerate_statedump(usess, app); | |
6921 | if (ret < 0) { | |
6922 | /* Continue to the next app even on error */ | |
6923 | continue; | |
6924 | } | |
6925 | } | |
6926 | ||
6927 | rcu_read_unlock(); | |
6928 | ||
6929 | return 0; | |
6930 | } | |
5c408ad8 JD |
6931 | |
6932 | /* | |
6933 | * Rotate all the channels of a session. | |
6934 | * | |
6f6d3b69 | 6935 | * Return LTTNG_OK on success or else an LTTng error code. |
5c408ad8 | 6936 | */ |
6f6d3b69 | 6937 | enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) |
5c408ad8 | 6938 | { |
6f6d3b69 MD |
6939 | int ret; |
6940 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
5c408ad8 JD |
6941 | struct lttng_ht_iter iter; |
6942 | struct ust_app *app; | |
6943 | struct ltt_ust_session *usess = session->ust_session; | |
5c408ad8 JD |
6944 | |
6945 | assert(usess); | |
6946 | ||
6947 | rcu_read_lock(); | |
6948 | ||
6949 | switch (usess->buffer_type) { | |
6950 | case LTTNG_BUFFER_PER_UID: | |
6951 | { | |
6952 | struct buffer_reg_uid *reg; | |
6953 | ||
6954 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
6955 | struct buffer_reg_channel *reg_chan; | |
6956 | struct consumer_socket *socket; | |
6957 | ||
14d3fca9 JR |
6958 | if (!reg->registry->reg.ust->metadata_key) { |
6959 | /* Skip since no metadata is present */ | |
6960 | continue; | |
6961 | } | |
6962 | ||
5c408ad8 JD |
6963 | /* Get consumer socket to use to push the metadata.*/ |
6964 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
6965 | usess->consumer); | |
6966 | if (!socket) { | |
6f6d3b69 | 6967 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
6968 | goto error; |
6969 | } | |
6970 | ||
5c408ad8 JD |
6971 | /* Rotate the data channels. */ |
6972 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
6973 | reg_chan, node.node) { | |
5c408ad8 JD |
6974 | ret = consumer_rotate_channel(socket, |
6975 | reg_chan->consumer_key, | |
6976 | usess->uid, usess->gid, | |
d2956687 JG |
6977 | usess->consumer, |
6978 | /* is_metadata_channel */ false); | |
5c408ad8 | 6979 | if (ret < 0) { |
6f6d3b69 | 6980 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
6981 | goto error; |
6982 | } | |
6983 | } | |
6984 | ||
6985 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); | |
6986 | ||
6987 | ret = consumer_rotate_channel(socket, | |
6988 | reg->registry->reg.ust->metadata_key, | |
6989 | usess->uid, usess->gid, | |
d2956687 JG |
6990 | usess->consumer, |
6991 | /* is_metadata_channel */ true); | |
5c408ad8 | 6992 | if (ret < 0) { |
6f6d3b69 | 6993 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
6994 | goto error; |
6995 | } | |
5c408ad8 JD |
6996 | } |
6997 | break; | |
6998 | } | |
6999 | case LTTNG_BUFFER_PER_PID: | |
7000 | { | |
7001 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7002 | struct consumer_socket *socket; | |
7003 | struct lttng_ht_iter chan_iter; | |
7004 | struct ust_app_channel *ua_chan; | |
7005 | struct ust_app_session *ua_sess; | |
7006 | struct ust_registry_session *registry; | |
7007 | ||
7008 | ua_sess = lookup_session_by_app(usess, app); | |
7009 | if (!ua_sess) { | |
7010 | /* Session not associated with this app. */ | |
7011 | continue; | |
7012 | } | |
5c408ad8 JD |
7013 | |
7014 | /* Get the right consumer socket for the application. */ | |
7015 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
7016 | usess->consumer); | |
7017 | if (!socket) { | |
6f6d3b69 | 7018 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
7019 | goto error; |
7020 | } | |
7021 | ||
7022 | registry = get_session_registry(ua_sess); | |
7023 | if (!registry) { | |
6f6d3b69 MD |
7024 | DBG("Application session is being torn down. Skip application."); |
7025 | continue; | |
5c408ad8 JD |
7026 | } |
7027 | ||
5c408ad8 JD |
7028 | /* Rotate the data channels. */ |
7029 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7030 | ua_chan, node.node) { | |
470cc211 JG |
7031 | ret = consumer_rotate_channel(socket, |
7032 | ua_chan->key, | |
ff588497 JR |
7033 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
7034 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
d2956687 JG |
7035 | ua_sess->consumer, |
7036 | /* is_metadata_channel */ false); | |
5c408ad8 | 7037 | if (ret < 0) { |
6f6d3b69 MD |
7038 | /* Per-PID buffer and application going away. */ |
7039 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7040 | continue; | |
7041 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7042 | goto error; |
7043 | } | |
7044 | } | |
7045 | ||
7046 | /* Rotate the metadata channel. */ | |
7047 | (void) push_metadata(registry, usess->consumer); | |
470cc211 JG |
7048 | ret = consumer_rotate_channel(socket, |
7049 | registry->metadata_key, | |
ff588497 JR |
7050 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
7051 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
d2956687 JG |
7052 | ua_sess->consumer, |
7053 | /* is_metadata_channel */ true); | |
5c408ad8 | 7054 | if (ret < 0) { |
6f6d3b69 MD |
7055 | /* Per-PID buffer and application going away. */ |
7056 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7057 | continue; | |
7058 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7059 | goto error; |
7060 | } | |
5c408ad8 JD |
7061 | } |
7062 | break; | |
7063 | } | |
7064 | default: | |
7065 | assert(0); | |
7066 | break; | |
7067 | } | |
7068 | ||
6f6d3b69 | 7069 | cmd_ret = LTTNG_OK; |
5c408ad8 JD |
7070 | |
7071 | error: | |
7072 | rcu_read_unlock(); | |
6f6d3b69 | 7073 | return cmd_ret; |
5c408ad8 | 7074 | } |
d2956687 JG |
7075 | |
7076 | enum lttng_error_code ust_app_create_channel_subdirectories( | |
7077 | const struct ltt_ust_session *usess) | |
7078 | { | |
7079 | enum lttng_error_code ret = LTTNG_OK; | |
7080 | struct lttng_ht_iter iter; | |
7081 | enum lttng_trace_chunk_status chunk_status; | |
7082 | char *pathname_index; | |
7083 | int fmt_ret; | |
7084 | ||
7085 | assert(usess->current_trace_chunk); | |
7086 | rcu_read_lock(); | |
7087 | ||
7088 | switch (usess->buffer_type) { | |
7089 | case LTTNG_BUFFER_PER_UID: | |
7090 | { | |
7091 | struct buffer_reg_uid *reg; | |
7092 | ||
7093 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7094 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7095 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7096 | reg->uid, reg->bits_per_long); |
7097 | if (fmt_ret < 0) { | |
7098 | ERR("Failed to format channel index directory"); | |
7099 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7100 | goto error; | |
7101 | } | |
7102 | ||
7103 | /* | |
7104 | * Create the index subdirectory which will take care | |
7105 | * of implicitly creating the channel's path. | |
7106 | */ | |
7107 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7108 | usess->current_trace_chunk, | |
7109 | pathname_index); | |
7110 | free(pathname_index); | |
7111 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7112 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7113 | goto error; | |
7114 | } | |
7115 | } | |
7116 | break; | |
7117 | } | |
7118 | case LTTNG_BUFFER_PER_PID: | |
7119 | { | |
7120 | struct ust_app *app; | |
7121 | ||
495dece5 MD |
7122 | /* |
7123 | * Create the toplevel ust/ directory in case no apps are running. | |
7124 | */ | |
7125 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7126 | usess->current_trace_chunk, | |
7127 | DEFAULT_UST_TRACE_DIR); | |
7128 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7129 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7130 | goto error; | |
7131 | } | |
7132 | ||
d2956687 JG |
7133 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
7134 | pid_n.node) { | |
7135 | struct ust_app_session *ua_sess; | |
7136 | struct ust_registry_session *registry; | |
7137 | ||
7138 | ua_sess = lookup_session_by_app(usess, app); | |
7139 | if (!ua_sess) { | |
7140 | /* Session not associated with this app. */ | |
7141 | continue; | |
7142 | } | |
7143 | ||
7144 | registry = get_session_registry(ua_sess); | |
7145 | if (!registry) { | |
7146 | DBG("Application session is being torn down. Skip application."); | |
7147 | continue; | |
7148 | } | |
7149 | ||
7150 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7151 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7152 | ua_sess->path); |
7153 | if (fmt_ret < 0) { | |
7154 | ERR("Failed to format channel index directory"); | |
7155 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7156 | goto error; | |
7157 | } | |
7158 | /* | |
7159 | * Create the index subdirectory which will take care | |
7160 | * of implicitly creating the channel's path. | |
7161 | */ | |
7162 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7163 | usess->current_trace_chunk, | |
7164 | pathname_index); | |
7165 | free(pathname_index); | |
7166 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7167 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7168 | goto error; | |
7169 | } | |
7170 | } | |
7171 | break; | |
7172 | } | |
7173 | default: | |
7174 | abort(); | |
7175 | } | |
7176 | ||
7177 | ret = LTTNG_OK; | |
7178 | error: | |
7179 | rcu_read_unlock(); | |
7180 | return ret; | |
7181 | } | |
4a9b9759 MD |
7182 | |
7183 | /* | |
7184 | * Clear all the channels of a session. | |
7185 | * | |
7186 | * Return LTTNG_OK on success or else an LTTng error code. | |
7187 | */ | |
7188 | enum lttng_error_code ust_app_clear_session(struct ltt_session *session) | |
7189 | { | |
7190 | int ret; | |
7191 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
7192 | struct lttng_ht_iter iter; | |
7193 | struct ust_app *app; | |
7194 | struct ltt_ust_session *usess = session->ust_session; | |
7195 | ||
7196 | assert(usess); | |
7197 | ||
7198 | rcu_read_lock(); | |
7199 | ||
7200 | if (usess->active) { | |
7201 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); | |
7202 | cmd_ret = LTTNG_ERR_FATAL; | |
7203 | goto end; | |
7204 | } | |
7205 | ||
7206 | switch (usess->buffer_type) { | |
7207 | case LTTNG_BUFFER_PER_UID: | |
7208 | { | |
7209 | struct buffer_reg_uid *reg; | |
7210 | ||
7211 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7212 | struct buffer_reg_channel *reg_chan; | |
7213 | struct consumer_socket *socket; | |
7214 | ||
7215 | /* Get consumer socket to use to push the metadata.*/ | |
7216 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7217 | usess->consumer); | |
7218 | if (!socket) { | |
7219 | cmd_ret = LTTNG_ERR_INVALID; | |
7220 | goto error_socket; | |
7221 | } | |
7222 | ||
7223 | /* Clear the data channels. */ | |
7224 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
7225 | reg_chan, node.node) { | |
7226 | ret = consumer_clear_channel(socket, | |
7227 | reg_chan->consumer_key); | |
7228 | if (ret < 0) { | |
7229 | goto error; | |
7230 | } | |
7231 | } | |
7232 | ||
7233 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); | |
7234 | ||
7235 | /* | |
7236 | * Clear the metadata channel. | |
7237 | * Metadata channel is not cleared per se but we still need to | |
7238 | * perform a rotation operation on it behind the scene. | |
7239 | */ | |
7240 | ret = consumer_clear_channel(socket, | |
7241 | reg->registry->reg.ust->metadata_key); | |
7242 | if (ret < 0) { | |
7243 | goto error; | |
7244 | } | |
7245 | } | |
7246 | break; | |
7247 | } | |
7248 | case LTTNG_BUFFER_PER_PID: | |
7249 | { | |
7250 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7251 | struct consumer_socket *socket; | |
7252 | struct lttng_ht_iter chan_iter; | |
7253 | struct ust_app_channel *ua_chan; | |
7254 | struct ust_app_session *ua_sess; | |
7255 | struct ust_registry_session *registry; | |
7256 | ||
7257 | ua_sess = lookup_session_by_app(usess, app); | |
7258 | if (!ua_sess) { | |
7259 | /* Session not associated with this app. */ | |
7260 | continue; | |
7261 | } | |
7262 | ||
7263 | /* Get the right consumer socket for the application. */ | |
7264 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
7265 | usess->consumer); | |
7266 | if (!socket) { | |
7267 | cmd_ret = LTTNG_ERR_INVALID; | |
7268 | goto error_socket; | |
7269 | } | |
7270 | ||
7271 | registry = get_session_registry(ua_sess); | |
7272 | if (!registry) { | |
7273 | DBG("Application session is being torn down. Skip application."); | |
7274 | continue; | |
7275 | } | |
7276 | ||
7277 | /* Clear the data channels. */ | |
7278 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7279 | ua_chan, node.node) { | |
7280 | ret = consumer_clear_channel(socket, ua_chan->key); | |
7281 | if (ret < 0) { | |
7282 | /* Per-PID buffer and application going away. */ | |
7283 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7284 | continue; | |
7285 | } | |
7286 | goto error; | |
7287 | } | |
7288 | } | |
7289 | ||
7290 | (void) push_metadata(registry, usess->consumer); | |
7291 | ||
7292 | /* | |
7293 | * Clear the metadata channel. | |
7294 | * Metadata channel is not cleared per se but we still need to | |
7295 | * perform rotation operation on it behind the scene. | |
7296 | */ | |
7297 | ret = consumer_clear_channel(socket, registry->metadata_key); | |
7298 | if (ret < 0) { | |
7299 | /* Per-PID buffer and application going away. */ | |
7300 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7301 | continue; | |
7302 | } | |
7303 | goto error; | |
7304 | } | |
7305 | } | |
7306 | break; | |
7307 | } | |
7308 | default: | |
7309 | assert(0); | |
7310 | break; | |
7311 | } | |
7312 | ||
7313 | cmd_ret = LTTNG_OK; | |
7314 | goto end; | |
7315 | ||
7316 | error: | |
7317 | switch (-ret) { | |
7318 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
7319 | cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; | |
7320 | break; | |
7321 | default: | |
7322 | cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; | |
7323 | } | |
7324 | ||
7325 | error_socket: | |
7326 | end: | |
7327 | rcu_read_unlock(); | |
7328 | return cmd_ret; | |
7329 | } | |
04ed9e10 JG |
7330 | |
7331 | /* | |
7332 | * This function skips the metadata channel as the begin/end timestamps of a | |
7333 | * metadata packet are useless. | |
7334 | * | |
7335 | * Moreover, opening a packet after a "clear" will cause problems for live | |
7336 | * sessions as it will introduce padding that was not part of the first trace | |
7337 | * chunk. The relay daemon expects the content of the metadata stream of | |
7338 | * successive metadata trace chunks to be strict supersets of one another. | |
7339 | * | |
7340 | * For example, flushing a packet at the beginning of the metadata stream of | |
7341 | * a trace chunk resulting from a "clear" session command will cause the | |
7342 | * size of the metadata stream of the new trace chunk to not match the size of | |
7343 | * the metadata stream of the original chunk. This will confuse the relay | |
7344 | * daemon as the same "offset" in a metadata stream will no longer point | |
7345 | * to the same content. | |
7346 | */ | |
7347 | enum lttng_error_code ust_app_open_packets(struct ltt_session *session) | |
7348 | { | |
7349 | enum lttng_error_code ret = LTTNG_OK; | |
7350 | struct lttng_ht_iter iter; | |
7351 | struct ltt_ust_session *usess = session->ust_session; | |
7352 | ||
7353 | assert(usess); | |
7354 | ||
7355 | rcu_read_lock(); | |
7356 | ||
7357 | switch (usess->buffer_type) { | |
7358 | case LTTNG_BUFFER_PER_UID: | |
7359 | { | |
7360 | struct buffer_reg_uid *reg; | |
7361 | ||
7362 | cds_list_for_each_entry ( | |
7363 | reg, &usess->buffer_reg_uid_list, lnode) { | |
7364 | struct buffer_reg_channel *reg_chan; | |
7365 | struct consumer_socket *socket; | |
7366 | ||
7367 | socket = consumer_find_socket_by_bitness( | |
7368 | reg->bits_per_long, usess->consumer); | |
7369 | if (!socket) { | |
7370 | ret = LTTNG_ERR_FATAL; | |
7371 | goto error; | |
7372 | } | |
7373 | ||
7374 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
7375 | &iter.iter, reg_chan, node.node) { | |
7376 | const int open_ret = | |
7377 | consumer_open_channel_packets( | |
7378 | socket, | |
7379 | reg_chan->consumer_key); | |
7380 | ||
7381 | if (open_ret < 0) { | |
7382 | ret = LTTNG_ERR_UNK; | |
7383 | goto error; | |
7384 | } | |
7385 | } | |
7386 | } | |
7387 | break; | |
7388 | } | |
7389 | case LTTNG_BUFFER_PER_PID: | |
7390 | { | |
7391 | struct ust_app *app; | |
7392 | ||
7393 | cds_lfht_for_each_entry ( | |
7394 | ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7395 | struct consumer_socket *socket; | |
7396 | struct lttng_ht_iter chan_iter; | |
7397 | struct ust_app_channel *ua_chan; | |
7398 | struct ust_app_session *ua_sess; | |
7399 | struct ust_registry_session *registry; | |
7400 | ||
7401 | ua_sess = lookup_session_by_app(usess, app); | |
7402 | if (!ua_sess) { | |
7403 | /* Session not associated with this app. */ | |
7404 | continue; | |
7405 | } | |
7406 | ||
7407 | /* Get the right consumer socket for the application. */ | |
7408 | socket = consumer_find_socket_by_bitness( | |
7409 | app->bits_per_long, usess->consumer); | |
7410 | if (!socket) { | |
7411 | ret = LTTNG_ERR_FATAL; | |
7412 | goto error; | |
7413 | } | |
7414 | ||
7415 | registry = get_session_registry(ua_sess); | |
7416 | if (!registry) { | |
7417 | DBG("Application session is being torn down. Skip application."); | |
7418 | continue; | |
7419 | } | |
7420 | ||
7421 | cds_lfht_for_each_entry(ua_sess->channels->ht, | |
7422 | &chan_iter.iter, ua_chan, node.node) { | |
7423 | const int open_ret = | |
7424 | consumer_open_channel_packets( | |
7425 | socket, | |
7426 | ua_chan->key); | |
7427 | ||
7428 | if (open_ret < 0) { | |
7429 | /* | |
7430 | * Per-PID buffer and application going | |
7431 | * away. | |
7432 | */ | |
97a171e1 | 7433 | if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
04ed9e10 JG |
7434 | continue; |
7435 | } | |
7436 | ||
7437 | ret = LTTNG_ERR_UNK; | |
7438 | goto error; | |
7439 | } | |
7440 | } | |
7441 | } | |
7442 | break; | |
7443 | } | |
7444 | default: | |
7445 | abort(); | |
7446 | break; | |
7447 | } | |
7448 | ||
7449 | error: | |
7450 | rcu_read_unlock(); | |
7451 | return ret; | |
7452 | } |