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