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