c81f6e8f5597e1531292cfee2e4f5b5f57faf0dd
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <urcu/compiler.h>
28 #include <lttng/ust-error.h>
29
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32
33 #include "fd-limit.h"
34 #include "health.h"
35 #include "ust-app.h"
36 #include "ust-consumer.h"
37 #include "ust-ctl.h"
38
39 /*
40 * Match function for the hash table lookup.
41 *
42 * It matches an ust app event based on three attributes which are the event
43 * name, the filter bytecode and the loglevel.
44 */
45 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
46 {
47 struct ust_app_event *event;
48 const struct ust_app_ht_key *key;
49
50 assert(node);
51 assert(_key);
52
53 event = caa_container_of(node, struct ust_app_event, node.node);
54 key = _key;
55
56 /* Match the 3 elements of the key: name, filter and loglevel. */
57
58 /* Event name */
59 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
60 goto no_match;
61 }
62
63 /* Event loglevel. */
64 if (event->attr.loglevel != key->loglevel) {
65 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
66 && key->loglevel == 0 && event->attr.loglevel == -1) {
67 /*
68 * Match is accepted. This is because on event creation, the
69 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
70 * -1 are accepted for this loglevel type since 0 is the one set by
71 * the API when receiving an enable event.
72 */
73 } else {
74 goto no_match;
75 }
76 }
77
78 /* One of the filters is NULL, fail. */
79 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
80 goto no_match;
81 }
82
83 if (key->filter && event->filter) {
84 /* Both filters exists, check length followed by the bytecode. */
85 if (event->filter->len != key->filter->len ||
86 memcmp(event->filter->data, key->filter->data,
87 event->filter->len) != 0) {
88 goto no_match;
89 }
90 }
91
92 /* Match. */
93 return 1;
94
95 no_match:
96 return 0;
97 }
98
99 /*
100 * Unique add of an ust app event in the given ht. This uses the custom
101 * ht_match_ust_app_event match function and the event name as hash.
102 */
103 static void add_unique_ust_app_event(struct lttng_ht *ht,
104 struct ust_app_event *event)
105 {
106 struct cds_lfht_node *node_ptr;
107 struct ust_app_ht_key key;
108
109 assert(ht);
110 assert(ht->ht);
111 assert(event);
112
113 key.name = event->attr.name;
114 key.filter = event->filter;
115 key.loglevel = event->attr.loglevel;
116
117 node_ptr = cds_lfht_add_unique(ht->ht,
118 ht->hash_fct(event->node.key, lttng_ht_seed),
119 ht_match_ust_app_event, &key, &event->node.node);
120 assert(node_ptr == &event->node.node);
121 }
122
123 /*
124 * Delete ust context safely. RCU read lock must be held before calling
125 * this function.
126 */
127 static
128 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
129 {
130 if (ua_ctx->obj) {
131 ustctl_release_object(sock, ua_ctx->obj);
132 free(ua_ctx->obj);
133 }
134 free(ua_ctx);
135 }
136
137 /*
138 * Delete ust app event safely. RCU read lock must be held before calling
139 * this function.
140 */
141 static
142 void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
143 {
144 free(ua_event->filter);
145
146 if (ua_event->obj != NULL) {
147 ustctl_release_object(sock, ua_event->obj);
148 free(ua_event->obj);
149 }
150 free(ua_event);
151 }
152
153 /*
154 * Delete ust app stream safely. RCU read lock must be held before calling
155 * this function.
156 */
157 static
158 void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
159 {
160 if (stream->obj) {
161 ustctl_release_object(sock, stream->obj);
162 lttng_fd_put(LTTNG_FD_APPS, 2);
163 free(stream->obj);
164 }
165 free(stream);
166 }
167
168 /*
169 * Delete ust app channel safely. RCU read lock must be held before calling
170 * this function.
171 */
172 static
173 void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
174 {
175 int ret;
176 struct lttng_ht_iter iter;
177 struct ust_app_event *ua_event;
178 struct ust_app_ctx *ua_ctx;
179 struct ltt_ust_stream *stream, *stmp;
180
181 /* Wipe stream */
182 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
183 cds_list_del(&stream->list);
184 delete_ust_app_stream(sock, stream);
185 }
186
187 /* Wipe context */
188 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
189 ret = lttng_ht_del(ua_chan->ctx, &iter);
190 assert(!ret);
191 delete_ust_app_ctx(sock, ua_ctx);
192 }
193 lttng_ht_destroy(ua_chan->ctx);
194
195 /* Wipe events */
196 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
197 node.node) {
198 ret = lttng_ht_del(ua_chan->events, &iter);
199 assert(!ret);
200 delete_ust_app_event(sock, ua_event);
201 }
202 lttng_ht_destroy(ua_chan->events);
203
204 if (ua_chan->obj != NULL) {
205 ustctl_release_object(sock, ua_chan->obj);
206 lttng_fd_put(LTTNG_FD_APPS, 2);
207 free(ua_chan->obj);
208 }
209 free(ua_chan);
210 }
211
212 /*
213 * Delete ust app session safely. RCU read lock must be held before calling
214 * this function.
215 */
216 static
217 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess)
218 {
219 int ret;
220 struct lttng_ht_iter iter;
221 struct ust_app_channel *ua_chan;
222
223 if (ua_sess->metadata) {
224 if (ua_sess->metadata->stream_obj) {
225 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
226 lttng_fd_put(LTTNG_FD_APPS, 2);
227 free(ua_sess->metadata->stream_obj);
228 }
229 if (ua_sess->metadata->obj) {
230 ustctl_release_object(sock, ua_sess->metadata->obj);
231 lttng_fd_put(LTTNG_FD_APPS, 2);
232 free(ua_sess->metadata->obj);
233 }
234 trace_ust_destroy_metadata(ua_sess->metadata);
235 }
236
237 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
238 node.node) {
239 ret = lttng_ht_del(ua_sess->channels, &iter);
240 assert(!ret);
241 delete_ust_app_channel(sock, ua_chan);
242 }
243 lttng_ht_destroy(ua_sess->channels);
244
245 if (ua_sess->handle != -1) {
246 ustctl_release_handle(sock, ua_sess->handle);
247 }
248 free(ua_sess);
249 }
250
251 /*
252 * Delete a traceable application structure from the global list. Never call
253 * this function outside of a call_rcu call.
254 */
255 static
256 void delete_ust_app(struct ust_app *app)
257 {
258 int ret, sock;
259 struct lttng_ht_iter iter;
260 struct ust_app_session *ua_sess;
261
262 rcu_read_lock();
263
264 /* Delete ust app sessions info */
265 sock = app->sock;
266 app->sock = -1;
267
268 /* Wipe sessions */
269 cds_lfht_for_each_entry(app->sessions->ht, &iter.iter, ua_sess,
270 node.node) {
271 ret = lttng_ht_del(app->sessions, &iter);
272 assert(!ret);
273 delete_ust_app_session(app->sock, ua_sess);
274 }
275 lttng_ht_destroy(app->sessions);
276
277 /*
278 * Wait until we have deleted the application from the sock hash table
279 * before closing this socket, otherwise an application could re-use the
280 * socket ID and race with the teardown, using the same hash table entry.
281 *
282 * It's OK to leave the close in call_rcu. We want it to stay unique for
283 * all RCU readers that could run concurrently with unregister app,
284 * therefore we _need_ to only close that socket after a grace period. So
285 * it should stay in this RCU callback.
286 *
287 * This close() is a very important step of the synchronization model so
288 * every modification to this function must be carefully reviewed.
289 */
290 ret = close(sock);
291 if (ret) {
292 PERROR("close");
293 }
294 lttng_fd_put(LTTNG_FD_APPS, 1);
295
296 DBG2("UST app pid %d deleted", app->pid);
297 free(app);
298
299 rcu_read_unlock();
300 }
301
302 /*
303 * URCU intermediate call to delete an UST app.
304 */
305 static
306 void delete_ust_app_rcu(struct rcu_head *head)
307 {
308 struct lttng_ht_node_ulong *node =
309 caa_container_of(head, struct lttng_ht_node_ulong, head);
310 struct ust_app *app =
311 caa_container_of(node, struct ust_app, pid_n);
312
313 DBG3("Call RCU deleting app PID %d", app->pid);
314 delete_ust_app(app);
315 }
316
317 /*
318 * Alloc new UST app session.
319 */
320 static
321 struct ust_app_session *alloc_ust_app_session(void)
322 {
323 struct ust_app_session *ua_sess;
324
325 /* Init most of the default value by allocating and zeroing */
326 ua_sess = zmalloc(sizeof(struct ust_app_session));
327 if (ua_sess == NULL) {
328 PERROR("malloc");
329 goto error;
330 }
331
332 ua_sess->handle = -1;
333 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
334
335 return ua_sess;
336
337 error:
338 return NULL;
339 }
340
341 /*
342 * Alloc new UST app channel.
343 */
344 static
345 struct ust_app_channel *alloc_ust_app_channel(char *name,
346 struct lttng_ust_channel *attr)
347 {
348 struct ust_app_channel *ua_chan;
349
350 /* Init most of the default value by allocating and zeroing */
351 ua_chan = zmalloc(sizeof(struct ust_app_channel));
352 if (ua_chan == NULL) {
353 PERROR("malloc");
354 goto error;
355 }
356
357 /* Setup channel name */
358 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
359 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
360
361 ua_chan->enabled = 1;
362 ua_chan->handle = -1;
363 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
364 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
365 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
366
367 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
368
369 /* Copy attributes */
370 if (attr) {
371 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
372 }
373
374 DBG3("UST app channel %s allocated", ua_chan->name);
375
376 return ua_chan;
377
378 error:
379 return NULL;
380 }
381
382 /*
383 * Alloc new UST app event.
384 */
385 static
386 struct ust_app_event *alloc_ust_app_event(char *name,
387 struct lttng_ust_event *attr)
388 {
389 struct ust_app_event *ua_event;
390
391 /* Init most of the default value by allocating and zeroing */
392 ua_event = zmalloc(sizeof(struct ust_app_event));
393 if (ua_event == NULL) {
394 PERROR("malloc");
395 goto error;
396 }
397
398 ua_event->enabled = 1;
399 strncpy(ua_event->name, name, sizeof(ua_event->name));
400 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
401 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
402
403 /* Copy attributes */
404 if (attr) {
405 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
406 }
407
408 DBG3("UST app event %s allocated", ua_event->name);
409
410 return ua_event;
411
412 error:
413 return NULL;
414 }
415
416 /*
417 * Alloc new UST app context.
418 */
419 static
420 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
421 {
422 struct ust_app_ctx *ua_ctx;
423
424 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
425 if (ua_ctx == NULL) {
426 goto error;
427 }
428
429 if (uctx) {
430 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
431 }
432
433 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
434
435 error:
436 return ua_ctx;
437 }
438
439 /*
440 * Allocate a filter and copy the given original filter.
441 *
442 * Return allocated filter or NULL on error.
443 */
444 static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
445 struct lttng_ust_filter_bytecode *orig_f)
446 {
447 struct lttng_ust_filter_bytecode *filter = NULL;
448
449 /* Copy filter bytecode */
450 filter = zmalloc(sizeof(*filter) + orig_f->len);
451 if (!filter) {
452 PERROR("zmalloc alloc ust app filter");
453 goto error;
454 }
455
456 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
457
458 error:
459 return filter;
460 }
461
462 /*
463 * Find an ust_app using the sock and return it. RCU read side lock must be
464 * held before calling this helper function.
465 */
466 static
467 struct ust_app *find_app_by_sock(int sock)
468 {
469 struct lttng_ht_node_ulong *node;
470 struct lttng_ht_iter iter;
471
472 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
473 node = lttng_ht_iter_get_node_ulong(&iter);
474 if (node == NULL) {
475 DBG2("UST app find by sock %d not found", sock);
476 goto error;
477 }
478
479 return caa_container_of(node, struct ust_app, sock_n);
480
481 error:
482 return NULL;
483 }
484
485 /*
486 * Lookup for an ust app event based on event name, filter bytecode and the
487 * event loglevel.
488 *
489 * Return an ust_app_event object or NULL on error.
490 */
491 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
492 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel)
493 {
494 struct lttng_ht_iter iter;
495 struct lttng_ht_node_str *node;
496 struct ust_app_event *event = NULL;
497 struct ust_app_ht_key key;
498
499 assert(name);
500 assert(ht);
501
502 /* Setup key for event lookup. */
503 key.name = name;
504 key.filter = filter;
505 key.loglevel = loglevel;
506
507 /* Lookup using the event name as hash and a custom match fct. */
508 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
509 ht_match_ust_app_event, &key, &iter.iter);
510 node = lttng_ht_iter_get_node_str(&iter);
511 if (node == NULL) {
512 goto end;
513 }
514
515 event = caa_container_of(node, struct ust_app_event, node);
516
517 end:
518 return event;
519 }
520
521 /*
522 * Create the channel context on the tracer.
523 */
524 static
525 int create_ust_channel_context(struct ust_app_channel *ua_chan,
526 struct ust_app_ctx *ua_ctx, struct ust_app *app)
527 {
528 int ret;
529
530 health_code_update(&health_thread_cmd);
531
532 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
533 ua_chan->obj, &ua_ctx->obj);
534 if (ret < 0) {
535 goto error;
536 }
537
538 ua_ctx->handle = ua_ctx->obj->handle;
539
540 DBG2("UST app context created successfully for channel %s", ua_chan->name);
541
542 error:
543 health_code_update(&health_thread_cmd);
544 return ret;
545 }
546
547 /*
548 * Set the filter on the tracer.
549 */
550 static
551 int set_ust_event_filter(struct ust_app_event *ua_event,
552 struct ust_app *app)
553 {
554 int ret;
555
556 health_code_update(&health_thread_cmd);
557
558 if (!ua_event->filter) {
559 ret = 0;
560 goto error;
561 }
562
563 ret = ustctl_set_filter(app->sock, ua_event->filter,
564 ua_event->obj);
565 if (ret < 0) {
566 goto error;
567 }
568
569 DBG2("UST filter set successfully for event %s", ua_event->name);
570
571 error:
572 health_code_update(&health_thread_cmd);
573 return ret;
574 }
575
576 /*
577 * Disable the specified event on to UST tracer for the UST session.
578 */
579 static int disable_ust_event(struct ust_app *app,
580 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
581 {
582 int ret;
583
584 health_code_update(&health_thread_cmd);
585
586 ret = ustctl_disable(app->sock, ua_event->obj);
587 if (ret < 0) {
588 ERR("UST app event %s disable failed for app (pid: %d) "
589 "and session handle %d with ret %d",
590 ua_event->attr.name, app->pid, ua_sess->handle, ret);
591 goto error;
592 }
593
594 DBG2("UST app event %s disabled successfully for app (pid: %d)",
595 ua_event->attr.name, app->pid);
596
597 error:
598 health_code_update(&health_thread_cmd);
599 return ret;
600 }
601
602 /*
603 * Disable the specified channel on to UST tracer for the UST session.
604 */
605 static int disable_ust_channel(struct ust_app *app,
606 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
607 {
608 int ret;
609
610 health_code_update(&health_thread_cmd);
611
612 ret = ustctl_disable(app->sock, ua_chan->obj);
613 if (ret < 0) {
614 ERR("UST app channel %s disable failed for app (pid: %d) "
615 "and session handle %d with ret %d",
616 ua_chan->name, app->pid, ua_sess->handle, ret);
617 goto error;
618 }
619
620 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
621 ua_chan->name, app->pid);
622
623 error:
624 health_code_update(&health_thread_cmd);
625 return ret;
626 }
627
628 /*
629 * Enable the specified channel on to UST tracer for the UST session.
630 */
631 static int enable_ust_channel(struct ust_app *app,
632 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
633 {
634 int ret;
635
636 health_code_update(&health_thread_cmd);
637
638 ret = ustctl_enable(app->sock, ua_chan->obj);
639 if (ret < 0) {
640 ERR("UST app channel %s enable failed for app (pid: %d) "
641 "and session handle %d with ret %d",
642 ua_chan->name, app->pid, ua_sess->handle, ret);
643 goto error;
644 }
645
646 ua_chan->enabled = 1;
647
648 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
649 ua_chan->name, app->pid);
650
651 error:
652 health_code_update(&health_thread_cmd);
653 return ret;
654 }
655
656 /*
657 * Enable the specified event on to UST tracer for the UST session.
658 */
659 static int enable_ust_event(struct ust_app *app,
660 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
661 {
662 int ret;
663
664 health_code_update(&health_thread_cmd);
665
666 ret = ustctl_enable(app->sock, ua_event->obj);
667 if (ret < 0) {
668 ERR("UST app event %s enable failed for app (pid: %d) "
669 "and session handle %d with ret %d",
670 ua_event->attr.name, app->pid, ua_sess->handle, ret);
671 goto error;
672 }
673
674 DBG2("UST app event %s enabled successfully for app (pid: %d)",
675 ua_event->attr.name, app->pid);
676
677 error:
678 health_code_update(&health_thread_cmd);
679 return ret;
680 }
681
682 /*
683 * Open metadata onto the UST tracer for a UST session.
684 */
685 static int open_ust_metadata(struct ust_app *app,
686 struct ust_app_session *ua_sess)
687 {
688 int ret;
689 struct lttng_ust_channel_attr uattr;
690
691 health_code_update(&health_thread_cmd);
692
693 uattr.overwrite = ua_sess->metadata->attr.overwrite;
694 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
695 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
696 uattr.switch_timer_interval =
697 ua_sess->metadata->attr.switch_timer_interval;
698 uattr.read_timer_interval =
699 ua_sess->metadata->attr.read_timer_interval;
700 uattr.output = ua_sess->metadata->attr.output;
701
702 /* We are going to receive 2 fds, we need to reserve them. */
703 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
704 if (ret < 0) {
705 ERR("Exhausted number of available FD upon metadata open");
706 goto error;
707 }
708 /* UST tracer metadata creation */
709 ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr,
710 &ua_sess->metadata->obj);
711 if (ret < 0) {
712 ERR("UST app open metadata failed for app pid:%d with ret %d",
713 app->pid, ret);
714 goto error;
715 }
716
717 ua_sess->metadata->handle = ua_sess->metadata->obj->handle;
718
719 error:
720 health_code_update(&health_thread_cmd);
721 return ret;
722 }
723
724 /*
725 * Create stream onto the UST tracer for a UST session.
726 */
727 static int create_ust_stream(struct ust_app *app,
728 struct ust_app_session *ua_sess)
729 {
730 int ret;
731
732 health_code_update(&health_thread_cmd);
733
734 /* We are going to receive 2 fds, we need to reserve them. */
735 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
736 if (ret < 0) {
737 ERR("Exhausted number of available FD upon metadata stream create");
738 goto error;
739 }
740 ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj,
741 &ua_sess->metadata->stream_obj);
742 if (ret < 0) {
743 ERR("UST create metadata stream failed");
744 goto error;
745 }
746
747 error:
748 health_code_update(&health_thread_cmd);
749 return ret;
750 }
751
752 /*
753 * Create the specified channel onto the UST tracer for a UST session.
754 */
755 static int create_ust_channel(struct ust_app *app,
756 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
757 {
758 int ret;
759
760 health_code_update(&health_thread_cmd);
761
762 /* TODO: remove cast and use lttng-ust-abi.h */
763
764 /* We are going to receive 2 fds, we need to reserve them. */
765 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
766 if (ret < 0) {
767 ERR("Exhausted number of available FD upon create channel");
768 goto error;
769 }
770
771 health_code_update(&health_thread_cmd);
772
773 ret = ustctl_create_channel(app->sock, ua_sess->handle,
774 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
775 if (ret < 0) {
776 ERR("Creating channel %s for app (pid: %d, sock: %d) "
777 "and session handle %d with ret %d",
778 ua_chan->name, app->pid, app->sock,
779 ua_sess->handle, ret);
780 lttng_fd_put(LTTNG_FD_APPS, 2);
781 goto error;
782 }
783
784 ua_chan->handle = ua_chan->obj->handle;
785
786 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
787 ua_chan->name, app->pid, app->sock);
788
789 health_code_update(&health_thread_cmd);
790
791 /* If channel is not enabled, disable it on the tracer */
792 if (!ua_chan->enabled) {
793 ret = disable_ust_channel(app, ua_sess, ua_chan);
794 if (ret < 0) {
795 goto error;
796 }
797 }
798
799 error:
800 health_code_update(&health_thread_cmd);
801 return ret;
802 }
803
804 /*
805 * Create the specified event onto the UST tracer for a UST session.
806 */
807 static
808 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
809 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
810 {
811 int ret = 0;
812
813 health_code_update(&health_thread_cmd);
814
815 /* Create UST event on tracer */
816 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
817 &ua_event->obj);
818 if (ret < 0) {
819 ERR("Error ustctl create event %s for app pid: %d with ret %d",
820 ua_event->attr.name, app->pid, ret);
821 goto error;
822 }
823
824 ua_event->handle = ua_event->obj->handle;
825
826 DBG2("UST app event %s created successfully for pid:%d",
827 ua_event->attr.name, app->pid);
828
829 health_code_update(&health_thread_cmd);
830
831 /* Set filter if one is present. */
832 if (ua_event->filter) {
833 ret = set_ust_event_filter(ua_event, app);
834 if (ret < 0) {
835 goto error;
836 }
837 }
838
839 /* If event not enabled, disable it on the tracer */
840 if (ua_event->enabled == 0) {
841 ret = disable_ust_event(app, ua_sess, ua_event);
842 if (ret < 0) {
843 /*
844 * If we hit an EPERM, something is wrong with our disable call. If
845 * we get an EEXIST, there is a problem on the tracer side since we
846 * just created it.
847 */
848 switch (ret) {
849 case -LTTNG_UST_ERR_PERM:
850 /* Code flow problem */
851 assert(0);
852 case -LTTNG_UST_ERR_EXIST:
853 /* It's OK for our use case. */
854 ret = 0;
855 break;
856 default:
857 break;
858 }
859 goto error;
860 }
861 }
862
863 error:
864 health_code_update(&health_thread_cmd);
865 return ret;
866 }
867
868 /*
869 * Copy data between an UST app event and a LTT event.
870 */
871 static void shadow_copy_event(struct ust_app_event *ua_event,
872 struct ltt_ust_event *uevent)
873 {
874 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
875 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
876
877 ua_event->enabled = uevent->enabled;
878
879 /* Copy event attributes */
880 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
881
882 /* Copy filter bytecode */
883 if (uevent->filter) {
884 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
885 /* Filter might be NULL here in case of ENONEM. */
886 }
887 }
888
889 /*
890 * Copy data between an UST app channel and a LTT channel.
891 */
892 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
893 struct ltt_ust_channel *uchan)
894 {
895 struct lttng_ht_iter iter;
896 struct ltt_ust_event *uevent;
897 struct ltt_ust_context *uctx;
898 struct ust_app_event *ua_event;
899 struct ust_app_ctx *ua_ctx;
900
901 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
902
903 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
904 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
905 /* Copy event attributes */
906 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
907
908 ua_chan->enabled = uchan->enabled;
909
910 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
911 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
912 if (ua_ctx == NULL) {
913 continue;
914 }
915 lttng_ht_node_init_ulong(&ua_ctx->node,
916 (unsigned long) ua_ctx->ctx.ctx);
917 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
918 }
919
920 /* Copy all events from ltt ust channel to ust app channel */
921 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
922 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
923 uevent->filter, uevent->attr.loglevel);
924 if (ua_event == NULL) {
925 DBG2("UST event %s not found on shadow copy channel",
926 uevent->attr.name);
927 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
928 if (ua_event == NULL) {
929 continue;
930 }
931 shadow_copy_event(ua_event, uevent);
932 add_unique_ust_app_event(ua_chan->events, ua_event);
933 }
934 }
935
936 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
937 }
938
939 /*
940 * Copy data between a UST app session and a regular LTT session.
941 */
942 static void shadow_copy_session(struct ust_app_session *ua_sess,
943 struct ltt_ust_session *usess, struct ust_app *app)
944 {
945 struct lttng_ht_node_str *ua_chan_node;
946 struct lttng_ht_iter iter;
947 struct ltt_ust_channel *uchan;
948 struct ust_app_channel *ua_chan;
949 time_t rawtime;
950 struct tm *timeinfo;
951 char datetime[16];
952 int ret;
953
954 /* Get date and time for unique app path */
955 time(&rawtime);
956 timeinfo = localtime(&rawtime);
957 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
958
959 DBG2("Shadow copy of session handle %d", ua_sess->handle);
960
961 ua_sess->id = usess->id;
962 ua_sess->uid = usess->uid;
963 ua_sess->gid = usess->gid;
964
965 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
966 datetime);
967 if (ret < 0) {
968 PERROR("asprintf UST shadow copy session");
969 /* TODO: We cannot return an error from here.. */
970 assert(0);
971 }
972
973 /* TODO: support all UST domain */
974
975 /* Iterate over all channels in global domain. */
976 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
977 uchan, node.node) {
978 struct lttng_ht_iter uiter;
979
980 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
981 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
982 if (ua_chan_node != NULL) {
983 /* Session exist. Contiuing. */
984 continue;
985 }
986
987 DBG2("Channel %s not found on shadow session copy, creating it",
988 uchan->name);
989 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
990 if (ua_chan == NULL) {
991 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
992 continue;
993 }
994
995 shadow_copy_channel(ua_chan, uchan);
996 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
997 }
998 }
999
1000 /*
1001 * Lookup sesison wrapper.
1002 */
1003 static
1004 void __lookup_session_by_app(struct ltt_ust_session *usess,
1005 struct ust_app *app, struct lttng_ht_iter *iter)
1006 {
1007 /* Get right UST app session from app */
1008 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
1009 }
1010
1011 /*
1012 * Return ust app session from the app session hashtable using the UST session
1013 * id.
1014 */
1015 static struct ust_app_session *lookup_session_by_app(
1016 struct ltt_ust_session *usess, struct ust_app *app)
1017 {
1018 struct lttng_ht_iter iter;
1019 struct lttng_ht_node_ulong *node;
1020
1021 __lookup_session_by_app(usess, app, &iter);
1022 node = lttng_ht_iter_get_node_ulong(&iter);
1023 if (node == NULL) {
1024 goto error;
1025 }
1026
1027 return caa_container_of(node, struct ust_app_session, node);
1028
1029 error:
1030 return NULL;
1031 }
1032
1033 /*
1034 * Create a UST session onto the tracer of app and add it the session
1035 * hashtable.
1036 *
1037 * Return ust app session or NULL on error.
1038 */
1039 static struct ust_app_session *create_ust_app_session(
1040 struct ltt_ust_session *usess, struct ust_app *app)
1041 {
1042 int ret;
1043 struct ust_app_session *ua_sess;
1044
1045 health_code_update(&health_thread_cmd);
1046
1047 ua_sess = lookup_session_by_app(usess, app);
1048 if (ua_sess == NULL) {
1049 DBG2("UST app pid: %d session id %d not found, creating it",
1050 app->pid, usess->id);
1051 ua_sess = alloc_ust_app_session();
1052 if (ua_sess == NULL) {
1053 /* Only malloc can failed so something is really wrong */
1054 goto end;
1055 }
1056 shadow_copy_session(ua_sess, usess, app);
1057 }
1058
1059 health_code_update(&health_thread_cmd);
1060
1061 if (ua_sess->handle == -1) {
1062 ret = ustctl_create_session(app->sock);
1063 if (ret < 0) {
1064 ERR("Creating session for app pid %d", app->pid);
1065 delete_ust_app_session(-1, ua_sess);
1066 /* This means that the tracer is gone... */
1067 ua_sess = (void*) -1UL;
1068 goto end;
1069 }
1070
1071 ua_sess->handle = ret;
1072
1073 /* Add ust app session to app's HT */
1074 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
1075 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
1076
1077 DBG2("UST app session created successfully with handle %d", ret);
1078 }
1079
1080 end:
1081 health_code_update(&health_thread_cmd);
1082 return ua_sess;
1083 }
1084
1085 /*
1086 * Create a context for the channel on the tracer.
1087 */
1088 static
1089 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1090 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1091 struct ust_app *app)
1092 {
1093 int ret = 0;
1094 struct lttng_ht_iter iter;
1095 struct lttng_ht_node_ulong *node;
1096 struct ust_app_ctx *ua_ctx;
1097
1098 DBG2("UST app adding context to channel %s", ua_chan->name);
1099
1100 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1101 node = lttng_ht_iter_get_node_ulong(&iter);
1102 if (node != NULL) {
1103 ret = -EEXIST;
1104 goto error;
1105 }
1106
1107 ua_ctx = alloc_ust_app_ctx(uctx);
1108 if (ua_ctx == NULL) {
1109 /* malloc failed */
1110 ret = -1;
1111 goto error;
1112 }
1113
1114 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1115 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
1116
1117 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1118 if (ret < 0) {
1119 goto error;
1120 }
1121
1122 error:
1123 return ret;
1124 }
1125
1126 /*
1127 * Enable on the tracer side a ust app event for the session and channel.
1128 */
1129 static
1130 int enable_ust_app_event(struct ust_app_session *ua_sess,
1131 struct ust_app_event *ua_event, struct ust_app *app)
1132 {
1133 int ret;
1134
1135 ret = enable_ust_event(app, ua_sess, ua_event);
1136 if (ret < 0) {
1137 goto error;
1138 }
1139
1140 ua_event->enabled = 1;
1141
1142 error:
1143 return ret;
1144 }
1145
1146 /*
1147 * Disable on the tracer side a ust app event for the session and channel.
1148 */
1149 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1150 struct ust_app_event *ua_event, struct ust_app *app)
1151 {
1152 int ret;
1153
1154 ret = disable_ust_event(app, ua_sess, ua_event);
1155 if (ret < 0) {
1156 goto error;
1157 }
1158
1159 ua_event->enabled = 0;
1160
1161 error:
1162 return ret;
1163 }
1164
1165 /*
1166 * Lookup ust app channel for session and disable it on the tracer side.
1167 */
1168 static
1169 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1170 struct ust_app_channel *ua_chan, struct ust_app *app)
1171 {
1172 int ret;
1173
1174 ret = disable_ust_channel(app, ua_sess, ua_chan);
1175 if (ret < 0) {
1176 goto error;
1177 }
1178
1179 ua_chan->enabled = 0;
1180
1181 error:
1182 return ret;
1183 }
1184
1185 /*
1186 * Lookup ust app channel for session and enable it on the tracer side.
1187 */
1188 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1189 struct ltt_ust_channel *uchan, struct ust_app *app)
1190 {
1191 int ret = 0;
1192 struct lttng_ht_iter iter;
1193 struct lttng_ht_node_str *ua_chan_node;
1194 struct ust_app_channel *ua_chan;
1195
1196 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1197 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1198 if (ua_chan_node == NULL) {
1199 DBG2("Unable to find channel %s in ust session id %u",
1200 uchan->name, ua_sess->id);
1201 goto error;
1202 }
1203
1204 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1205
1206 ret = enable_ust_channel(app, ua_sess, ua_chan);
1207 if (ret < 0) {
1208 goto error;
1209 }
1210
1211 error:
1212 return ret;
1213 }
1214
1215 /*
1216 * Create UST app channel and create it on the tracer.
1217 */
1218 static struct ust_app_channel *create_ust_app_channel(
1219 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1220 struct ust_app *app)
1221 {
1222 int ret = 0;
1223 struct lttng_ht_iter iter;
1224 struct lttng_ht_node_str *ua_chan_node;
1225 struct ust_app_channel *ua_chan;
1226
1227 /* Lookup channel in the ust app session */
1228 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1229 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1230 if (ua_chan_node != NULL) {
1231 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1232 goto end;
1233 }
1234
1235 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1236 if (ua_chan == NULL) {
1237 /* Only malloc can fail here */
1238 goto error;
1239 }
1240 shadow_copy_channel(ua_chan, uchan);
1241
1242 ret = create_ust_channel(app, ua_sess, ua_chan);
1243 if (ret < 0) {
1244 /* Not found previously means that it does not exist on the tracer */
1245 assert(ret != -LTTNG_UST_ERR_EXIST);
1246 goto error;
1247 }
1248
1249 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1250
1251 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
1252 app->pid);
1253
1254 end:
1255 return ua_chan;
1256
1257 error:
1258 delete_ust_app_channel(-1, ua_chan);
1259 return NULL;
1260 }
1261
1262 /*
1263 * Create UST app event and create it on the tracer side.
1264 */
1265 static
1266 int create_ust_app_event(struct ust_app_session *ua_sess,
1267 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1268 struct ust_app *app)
1269 {
1270 int ret = 0;
1271 struct ust_app_event *ua_event;
1272
1273 /* Get event node */
1274 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1275 uevent->filter, uevent->attr.loglevel);
1276 if (ua_event != NULL) {
1277 ret = -EEXIST;
1278 goto end;
1279 }
1280
1281 /* Does not exist so create one */
1282 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1283 if (ua_event == NULL) {
1284 /* Only malloc can failed so something is really wrong */
1285 ret = -ENOMEM;
1286 goto end;
1287 }
1288 shadow_copy_event(ua_event, uevent);
1289
1290 /* Create it on the tracer side */
1291 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1292 if (ret < 0) {
1293 /* Not found previously means that it does not exist on the tracer */
1294 assert(ret != -LTTNG_UST_ERR_EXIST);
1295 goto error;
1296 }
1297
1298 add_unique_ust_app_event(ua_chan->events, ua_event);
1299
1300 DBG2("UST app create event %s for PID %d completed", ua_event->name,
1301 app->pid);
1302
1303 end:
1304 return ret;
1305
1306 error:
1307 /* Valid. Calling here is already in a read side lock */
1308 delete_ust_app_event(-1, ua_event);
1309 return ret;
1310 }
1311
1312 /*
1313 * Create UST metadata and open it on the tracer side.
1314 */
1315 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1316 char *pathname, struct ust_app *app)
1317 {
1318 int ret = 0;
1319
1320 if (ua_sess->metadata == NULL) {
1321 /* Allocate UST metadata */
1322 ua_sess->metadata = trace_ust_create_metadata(pathname);
1323 if (ua_sess->metadata == NULL) {
1324 /* malloc() failed */
1325 goto error;
1326 }
1327
1328 ret = open_ust_metadata(app, ua_sess);
1329 if (ret < 0) {
1330 DBG3("Opening metadata failed. Cleaning up memory");
1331
1332 /* Cleanup failed metadata struct */
1333 free(ua_sess->metadata);
1334 /*
1335 * This is very important because delete_ust_app_session check if
1336 * the pointer is null or not in order to delete the metadata.
1337 */
1338 ua_sess->metadata = NULL;
1339 goto error;
1340 }
1341
1342 DBG2("UST metadata opened for app pid %d", app->pid);
1343 }
1344
1345 /* Open UST metadata stream */
1346 if (ua_sess->metadata->stream_obj == NULL) {
1347 ret = create_ust_stream(app, ua_sess);
1348 if (ret < 0) {
1349 goto error;
1350 }
1351
1352 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1353 "%s/metadata", ua_sess->path);
1354 if (ret < 0) {
1355 PERROR("asprintf UST create stream");
1356 goto error;
1357 }
1358
1359 DBG2("UST metadata stream object created for app pid %d",
1360 app->pid);
1361 } else {
1362 ERR("Attempting to create stream without metadata opened");
1363 goto error;
1364 }
1365
1366 return 0;
1367
1368 error:
1369 return -1;
1370 }
1371
1372 /*
1373 * Return pointer to traceable apps list.
1374 */
1375 struct lttng_ht *ust_app_get_ht(void)
1376 {
1377 return ust_app_ht;
1378 }
1379
1380 /*
1381 * Return ust app pointer or NULL if not found.
1382 */
1383 struct ust_app *ust_app_find_by_pid(pid_t pid)
1384 {
1385 struct lttng_ht_node_ulong *node;
1386 struct lttng_ht_iter iter;
1387
1388 rcu_read_lock();
1389 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1390 node = lttng_ht_iter_get_node_ulong(&iter);
1391 if (node == NULL) {
1392 DBG2("UST app no found with pid %d", pid);
1393 goto error;
1394 }
1395 rcu_read_unlock();
1396
1397 DBG2("Found UST app by pid %d", pid);
1398
1399 return caa_container_of(node, struct ust_app, pid_n);
1400
1401 error:
1402 rcu_read_unlock();
1403 return NULL;
1404 }
1405
1406 /*
1407 * Using pid and uid (of the app), allocate a new ust_app struct and
1408 * add it to the global traceable app list.
1409 *
1410 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1411 * bitness is not supported.
1412 */
1413 int ust_app_register(struct ust_register_msg *msg, int sock)
1414 {
1415 struct ust_app *lta;
1416 int ret;
1417
1418 if ((msg->bits_per_long == 64 &&
1419 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1420 || (msg->bits_per_long == 32 &&
1421 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
1422 ERR("Registration failed: application \"%s\" (pid: %d) has "
1423 "%d-bit long, but no consumerd for this long size is available.\n",
1424 msg->name, msg->pid, msg->bits_per_long);
1425 ret = close(sock);
1426 if (ret) {
1427 PERROR("close");
1428 }
1429 lttng_fd_put(LTTNG_FD_APPS, 1);
1430 return -EINVAL;
1431 }
1432 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1433 ERR("Registration failed: application \"%s\" (pid: %d) has "
1434 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1435 msg->name, msg->pid, msg->major, msg->minor);
1436 ret = close(sock);
1437 if (ret) {
1438 PERROR("close");
1439 }
1440 lttng_fd_put(LTTNG_FD_APPS, 1);
1441 return -EINVAL;
1442 }
1443 lta = zmalloc(sizeof(struct ust_app));
1444 if (lta == NULL) {
1445 PERROR("malloc");
1446 return -ENOMEM;
1447 }
1448
1449 lta->ppid = msg->ppid;
1450 lta->uid = msg->uid;
1451 lta->gid = msg->gid;
1452 lta->compatible = 0; /* Not compatible until proven */
1453 lta->bits_per_long = msg->bits_per_long;
1454 lta->v_major = msg->major;
1455 lta->v_minor = msg->minor;
1456 strncpy(lta->name, msg->name, sizeof(lta->name));
1457 lta->name[16] = '\0';
1458 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1459
1460 lta->pid = msg->pid;
1461 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1462 lta->sock = sock;
1463 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
1464
1465 rcu_read_lock();
1466
1467 /*
1468 * On a re-registration, we want to kick out the previous registration of
1469 * that pid
1470 */
1471 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1472
1473 /*
1474 * The socket _should_ be unique until _we_ call close. So, a add_unique
1475 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1476 * already in the table.
1477 */
1478 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1479
1480 rcu_read_unlock();
1481
1482 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1483 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1484 lta->sock, lta->name, lta->v_major, lta->v_minor);
1485
1486 return 0;
1487 }
1488
1489 /*
1490 * Unregister app by removing it from the global traceable app list and freeing
1491 * the data struct.
1492 *
1493 * The socket is already closed at this point so no close to sock.
1494 */
1495 void ust_app_unregister(int sock)
1496 {
1497 struct ust_app *lta;
1498 struct lttng_ht_node_ulong *node;
1499 struct lttng_ht_iter iter;
1500 int ret;
1501
1502 rcu_read_lock();
1503
1504 /* Get the node reference for a call_rcu */
1505 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1506 node = lttng_ht_iter_get_node_ulong(&iter);
1507 if (node == NULL) {
1508 ERR("Unable to find app by sock %d", sock);
1509 goto error;
1510 }
1511
1512 lta = caa_container_of(node, struct ust_app, sock_n);
1513
1514 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1515
1516 /* Remove application from PID hash table */
1517 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1518 assert(!ret);
1519
1520 /* Assign second node for deletion */
1521 iter.iter.node = &lta->pid_n.node;
1522
1523 /*
1524 * Ignore return value since the node might have been removed before by an
1525 * add replace during app registration because the PID can be reassigned by
1526 * the OS.
1527 */
1528 ret = lttng_ht_del(ust_app_ht, &iter);
1529 if (ret) {
1530 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1531 lta->pid);
1532 }
1533
1534 /* Free memory */
1535 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1536
1537 error:
1538 rcu_read_unlock();
1539 return;
1540 }
1541
1542 /*
1543 * Return traceable_app_count
1544 */
1545 unsigned long ust_app_list_count(void)
1546 {
1547 unsigned long count;
1548
1549 rcu_read_lock();
1550 count = lttng_ht_get_count(ust_app_ht);
1551 rcu_read_unlock();
1552
1553 return count;
1554 }
1555
1556 /*
1557 * Fill events array with all events name of all registered apps.
1558 */
1559 int ust_app_list_events(struct lttng_event **events)
1560 {
1561 int ret, handle;
1562 size_t nbmem, count = 0;
1563 struct lttng_ht_iter iter;
1564 struct ust_app *app;
1565 struct lttng_event *tmp;
1566
1567 nbmem = UST_APP_EVENT_LIST_SIZE;
1568 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1569 if (tmp == NULL) {
1570 PERROR("zmalloc ust app events");
1571 ret = -ENOMEM;
1572 goto error;
1573 }
1574
1575 rcu_read_lock();
1576
1577 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1578 struct lttng_ust_tracepoint_iter uiter;
1579
1580 health_code_update(&health_thread_cmd);
1581
1582 if (!app->compatible) {
1583 /*
1584 * TODO: In time, we should notice the caller of this error by
1585 * telling him that this is a version error.
1586 */
1587 continue;
1588 }
1589 handle = ustctl_tracepoint_list(app->sock);
1590 if (handle < 0) {
1591 ERR("UST app list events getting handle failed for app pid %d",
1592 app->pid);
1593 continue;
1594 }
1595
1596 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
1597 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1598 health_code_update(&health_thread_cmd);
1599 if (count >= nbmem) {
1600 /* In case the realloc fails, we free the memory */
1601 void *tmp_ptr = (void *) tmp;
1602 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1603 2 * nbmem);
1604 nbmem *= 2;
1605 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
1606 if (tmp == NULL) {
1607 PERROR("realloc ust app events");
1608 free(tmp_ptr);
1609 ret = -ENOMEM;
1610 goto rcu_error;
1611 }
1612 }
1613 memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1614 tmp[count].loglevel = uiter.loglevel;
1615 tmp[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1616 tmp[count].pid = app->pid;
1617 tmp[count].enabled = -1;
1618 count++;
1619 }
1620 }
1621
1622 ret = count;
1623 *events = tmp;
1624
1625 DBG2("UST app list events done (%zu events)", count);
1626
1627 rcu_error:
1628 rcu_read_unlock();
1629 error:
1630 health_code_update(&health_thread_cmd);
1631 return ret;
1632 }
1633
1634 /*
1635 * Fill events array with all events name of all registered apps.
1636 */
1637 int ust_app_list_event_fields(struct lttng_event_field **fields)
1638 {
1639 int ret, handle;
1640 size_t nbmem, count = 0;
1641 struct lttng_ht_iter iter;
1642 struct ust_app *app;
1643 struct lttng_event_field *tmp;
1644
1645 nbmem = UST_APP_EVENT_LIST_SIZE;
1646 tmp = zmalloc(nbmem * sizeof(struct lttng_event_field));
1647 if (tmp == NULL) {
1648 PERROR("zmalloc ust app event fields");
1649 ret = -ENOMEM;
1650 goto error;
1651 }
1652
1653 rcu_read_lock();
1654
1655 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1656 struct lttng_ust_field_iter uiter;
1657
1658 health_code_update(&health_thread_cmd);
1659
1660 if (!app->compatible) {
1661 /*
1662 * TODO: In time, we should notice the caller of this error by
1663 * telling him that this is a version error.
1664 */
1665 continue;
1666 }
1667 handle = ustctl_tracepoint_field_list(app->sock);
1668 if (handle < 0) {
1669 ERR("UST app list event fields getting handle failed for app pid %d",
1670 app->pid);
1671 continue;
1672 }
1673
1674 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
1675 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1676 health_code_update(&health_thread_cmd);
1677 if (count >= nbmem) {
1678 /* In case the realloc fails, we free the memory */
1679 void *tmp_ptr = (void *) tmp;
1680 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
1681 2 * nbmem);
1682 nbmem *= 2;
1683 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event_field));
1684 if (tmp == NULL) {
1685 PERROR("realloc ust app event fields");
1686 free(tmp_ptr);
1687 ret = -ENOMEM;
1688 goto rcu_error;
1689 }
1690 }
1691
1692 memcpy(tmp[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
1693 tmp[count].type = uiter.type;
1694 tmp[count].nowrite = uiter.nowrite;
1695
1696 memcpy(tmp[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
1697 tmp[count].event.loglevel = uiter.loglevel;
1698 tmp[count].event.type = LTTNG_UST_TRACEPOINT;
1699 tmp[count].event.pid = app->pid;
1700 tmp[count].event.enabled = -1;
1701 count++;
1702 }
1703 }
1704
1705 ret = count;
1706 *fields = tmp;
1707
1708 DBG2("UST app list event fields done (%zu events)", count);
1709
1710 rcu_error:
1711 rcu_read_unlock();
1712 error:
1713 health_code_update(&health_thread_cmd);
1714 return ret;
1715 }
1716
1717 /*
1718 * Free and clean all traceable apps of the global list.
1719 */
1720 void ust_app_clean_list(void)
1721 {
1722 int ret;
1723 struct ust_app *app;
1724 struct lttng_ht_iter iter;
1725
1726 DBG2("UST app cleaning registered apps hash table");
1727
1728 rcu_read_lock();
1729
1730 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1731 ret = lttng_ht_del(ust_app_ht, &iter);
1732 assert(!ret);
1733 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
1734 }
1735
1736 /* Cleanup socket hash table */
1737 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
1738 sock_n.node) {
1739 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1740 assert(!ret);
1741 }
1742
1743 /* Destroy is done only when the ht is empty */
1744 lttng_ht_destroy(ust_app_ht);
1745 lttng_ht_destroy(ust_app_ht_by_sock);
1746
1747 rcu_read_unlock();
1748 }
1749
1750 /*
1751 * Init UST app hash table.
1752 */
1753 void ust_app_ht_alloc(void)
1754 {
1755 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1756 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1757 }
1758
1759 /*
1760 * For a specific UST session, disable the channel for all registered apps.
1761 */
1762 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1763 struct ltt_ust_channel *uchan)
1764 {
1765 int ret = 0;
1766 struct lttng_ht_iter iter;
1767 struct lttng_ht_node_str *ua_chan_node;
1768 struct ust_app *app;
1769 struct ust_app_session *ua_sess;
1770 struct ust_app_channel *ua_chan;
1771
1772 if (usess == NULL || uchan == NULL) {
1773 ERR("Disabling UST global channel with NULL values");
1774 ret = -1;
1775 goto error;
1776 }
1777
1778 DBG2("UST app disabling channel %s from global domain for session id %d",
1779 uchan->name, usess->id);
1780
1781 rcu_read_lock();
1782
1783 /* For every registered applications */
1784 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1785 struct lttng_ht_iter uiter;
1786 if (!app->compatible) {
1787 /*
1788 * TODO: In time, we should notice the caller of this error by
1789 * telling him that this is a version error.
1790 */
1791 continue;
1792 }
1793 ua_sess = lookup_session_by_app(usess, app);
1794 if (ua_sess == NULL) {
1795 continue;
1796 }
1797
1798 /* Get channel */
1799 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1800 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1801 /* If the session if found for the app, the channel must be there */
1802 assert(ua_chan_node);
1803
1804 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1805 /* The channel must not be already disabled */
1806 assert(ua_chan->enabled == 1);
1807
1808 /* Disable channel onto application */
1809 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1810 if (ret < 0) {
1811 /* XXX: We might want to report this error at some point... */
1812 continue;
1813 }
1814 }
1815
1816 rcu_read_unlock();
1817
1818 error:
1819 return ret;
1820 }
1821
1822 /*
1823 * For a specific UST session, enable the channel for all registered apps.
1824 */
1825 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1826 struct ltt_ust_channel *uchan)
1827 {
1828 int ret = 0;
1829 struct lttng_ht_iter iter;
1830 struct ust_app *app;
1831 struct ust_app_session *ua_sess;
1832
1833 if (usess == NULL || uchan == NULL) {
1834 ERR("Adding UST global channel to NULL values");
1835 ret = -1;
1836 goto error;
1837 }
1838
1839 DBG2("UST app enabling channel %s to global domain for session id %d",
1840 uchan->name, usess->id);
1841
1842 rcu_read_lock();
1843
1844 /* For every registered applications */
1845 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1846 if (!app->compatible) {
1847 /*
1848 * TODO: In time, we should notice the caller of this error by
1849 * telling him that this is a version error.
1850 */
1851 continue;
1852 }
1853 ua_sess = lookup_session_by_app(usess, app);
1854 if (ua_sess == NULL) {
1855 continue;
1856 }
1857
1858 /* Enable channel onto application */
1859 ret = enable_ust_app_channel(ua_sess, uchan, app);
1860 if (ret < 0) {
1861 /* XXX: We might want to report this error at some point... */
1862 continue;
1863 }
1864 }
1865
1866 rcu_read_unlock();
1867
1868 error:
1869 return ret;
1870 }
1871
1872 /*
1873 * Disable an event in a channel and for a specific session.
1874 */
1875 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1876 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1877 {
1878 int ret = 0;
1879 struct lttng_ht_iter iter, uiter;
1880 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
1881 struct ust_app *app;
1882 struct ust_app_session *ua_sess;
1883 struct ust_app_channel *ua_chan;
1884 struct ust_app_event *ua_event;
1885
1886 DBG("UST app disabling event %s for all apps in channel "
1887 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
1888
1889 rcu_read_lock();
1890
1891 /* For all registered applications */
1892 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1893 if (!app->compatible) {
1894 /*
1895 * TODO: In time, we should notice the caller of this error by
1896 * telling him that this is a version error.
1897 */
1898 continue;
1899 }
1900 ua_sess = lookup_session_by_app(usess, app);
1901 if (ua_sess == NULL) {
1902 /* Next app */
1903 continue;
1904 }
1905
1906 /* Lookup channel in the ust app session */
1907 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1908 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1909 if (ua_chan_node == NULL) {
1910 DBG2("Channel %s not found in session id %d for app pid %d."
1911 "Skipping", uchan->name, usess->id, app->pid);
1912 continue;
1913 }
1914 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1915
1916 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1917 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
1918 if (ua_event_node == NULL) {
1919 DBG2("Event %s not found in channel %s for app pid %d."
1920 "Skipping", uevent->attr.name, uchan->name, app->pid);
1921 continue;
1922 }
1923 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1924
1925 ret = disable_ust_app_event(ua_sess, ua_event, app);
1926 if (ret < 0) {
1927 /* XXX: Report error someday... */
1928 continue;
1929 }
1930 }
1931
1932 rcu_read_unlock();
1933
1934 return ret;
1935 }
1936
1937 /*
1938 * For a specific UST session and UST channel, the event for all
1939 * registered apps.
1940 */
1941 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1942 struct ltt_ust_channel *uchan)
1943 {
1944 int ret = 0;
1945 struct lttng_ht_iter iter, uiter;
1946 struct lttng_ht_node_str *ua_chan_node;
1947 struct ust_app *app;
1948 struct ust_app_session *ua_sess;
1949 struct ust_app_channel *ua_chan;
1950 struct ust_app_event *ua_event;
1951
1952 DBG("UST app disabling all event for all apps in channel "
1953 "%s for session id %d", uchan->name, usess->id);
1954
1955 rcu_read_lock();
1956
1957 /* For all registered applications */
1958 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1959 if (!app->compatible) {
1960 /*
1961 * TODO: In time, we should notice the caller of this error by
1962 * telling him that this is a version error.
1963 */
1964 continue;
1965 }
1966 ua_sess = lookup_session_by_app(usess, app);
1967 /* If ua_sess is NULL, there is a code flow error */
1968 assert(ua_sess);
1969
1970 /* Lookup channel in the ust app session */
1971 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1972 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1973 /* If the channel is not found, there is a code flow error */
1974 assert(ua_chan_node);
1975
1976 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1977
1978 /* Disable each events of channel */
1979 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
1980 node.node) {
1981 ret = disable_ust_app_event(ua_sess, ua_event, app);
1982 if (ret < 0) {
1983 /* XXX: Report error someday... */
1984 continue;
1985 }
1986 }
1987 }
1988
1989 rcu_read_unlock();
1990
1991 return ret;
1992 }
1993
1994 /*
1995 * For a specific UST session, create the channel for all registered apps.
1996 */
1997 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
1998 struct ltt_ust_channel *uchan)
1999 {
2000 int ret = 0;
2001 struct lttng_ht_iter iter;
2002 struct ust_app *app;
2003 struct ust_app_session *ua_sess;
2004 struct ust_app_channel *ua_chan;
2005
2006 /* Very wrong code flow */
2007 assert(usess);
2008 assert(uchan);
2009
2010 DBG2("UST app adding channel %s to global domain for session id %d",
2011 uchan->name, usess->id);
2012
2013 rcu_read_lock();
2014
2015 /* For every registered applications */
2016 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2017 if (!app->compatible) {
2018 /*
2019 * TODO: In time, we should notice the caller of this error by
2020 * telling him that this is a version error.
2021 */
2022 continue;
2023 }
2024 /*
2025 * Create session on the tracer side and add it to app session HT. Note
2026 * that if session exist, it will simply return a pointer to the ust
2027 * app session.
2028 */
2029 ua_sess = create_ust_app_session(usess, app);
2030 if (ua_sess == NULL) {
2031 /* The malloc() failed. */
2032 ret = -1;
2033 goto error;
2034 } else if (ua_sess == (void *) -1UL) {
2035 /* The application's socket is not valid. Contiuing */
2036 ret = -1;
2037 continue;
2038 }
2039
2040 /* Create channel onto application */
2041 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
2042 if (ua_chan == NULL) {
2043 /* Major problem here and it's maybe the tracer or malloc() */
2044 ret = -1;
2045 goto error;
2046 }
2047 }
2048
2049 rcu_read_unlock();
2050
2051 error:
2052 return ret;
2053 }
2054
2055 /*
2056 * Enable event for a specific session and channel on the tracer.
2057 */
2058 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
2059 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2060 {
2061 int ret = 0;
2062 struct lttng_ht_iter iter, uiter;
2063 struct lttng_ht_node_str *ua_chan_node;
2064 struct ust_app *app;
2065 struct ust_app_session *ua_sess;
2066 struct ust_app_channel *ua_chan;
2067 struct ust_app_event *ua_event;
2068
2069 DBG("UST app enabling event %s for all apps for session id %d",
2070 uevent->attr.name, usess->id);
2071
2072 /*
2073 * NOTE: At this point, this function is called only if the session and
2074 * channel passed are already created for all apps. and enabled on the
2075 * tracer also.
2076 */
2077
2078 rcu_read_lock();
2079
2080 /* For all registered applications */
2081 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2082 if (!app->compatible) {
2083 /*
2084 * TODO: In time, we should notice the caller of this error by
2085 * telling him that this is a version error.
2086 */
2087 continue;
2088 }
2089 ua_sess = lookup_session_by_app(usess, app);
2090 /* If ua_sess is NULL, there is a code flow error */
2091 assert(ua_sess);
2092
2093 /* Lookup channel in the ust app session */
2094 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2095 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2096 /* If the channel is not found, there is a code flow error */
2097 assert(ua_chan_node);
2098
2099 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2100
2101 /* Get event node */
2102 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2103 uevent->filter, uevent->attr.loglevel);
2104 if (ua_event == NULL) {
2105 DBG3("UST app enable event %s not found for app PID %d."
2106 "Skipping app", uevent->attr.name, app->pid);
2107 continue;
2108 }
2109
2110 ret = enable_ust_app_event(ua_sess, ua_event, app);
2111 if (ret < 0) {
2112 goto error;
2113 }
2114 }
2115
2116 error:
2117 rcu_read_unlock();
2118 return ret;
2119 }
2120
2121 /*
2122 * For a specific existing UST session and UST channel, creates the event for
2123 * all registered apps.
2124 */
2125 int ust_app_create_event_glb(struct ltt_ust_session *usess,
2126 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2127 {
2128 int ret = 0;
2129 struct lttng_ht_iter iter, uiter;
2130 struct lttng_ht_node_str *ua_chan_node;
2131 struct ust_app *app;
2132 struct ust_app_session *ua_sess;
2133 struct ust_app_channel *ua_chan;
2134
2135 DBG("UST app creating event %s for all apps for session id %d",
2136 uevent->attr.name, usess->id);
2137
2138 rcu_read_lock();
2139
2140 /* For all registered applications */
2141 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2142 if (!app->compatible) {
2143 /*
2144 * TODO: In time, we should notice the caller of this error by
2145 * telling him that this is a version error.
2146 */
2147 continue;
2148 }
2149 ua_sess = lookup_session_by_app(usess, app);
2150 /* If ua_sess is NULL, there is a code flow error */
2151 assert(ua_sess);
2152
2153 /* Lookup channel in the ust app session */
2154 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2155 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2156 /* If the channel is not found, there is a code flow error */
2157 assert(ua_chan_node);
2158
2159 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2160
2161 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2162 if (ret < 0) {
2163 if (ret != -LTTNG_UST_ERR_EXIST) {
2164 /* Possible value at this point: -ENOMEM. If so, we stop! */
2165 break;
2166 }
2167 DBG2("UST app event %s already exist on app PID %d",
2168 uevent->attr.name, app->pid);
2169 continue;
2170 }
2171 }
2172
2173 rcu_read_unlock();
2174
2175 return ret;
2176 }
2177
2178 /*
2179 * Start tracing for a specific UST session and app.
2180 */
2181 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
2182 {
2183 int ret = 0;
2184 struct lttng_ht_iter iter;
2185 struct ust_app_session *ua_sess;
2186 struct ust_app_channel *ua_chan;
2187 struct ltt_ust_stream *ustream;
2188 struct consumer_socket *socket;
2189
2190 DBG("Starting tracing for ust app pid %d", app->pid);
2191
2192 rcu_read_lock();
2193
2194 if (!app->compatible) {
2195 goto end;
2196 }
2197
2198 ua_sess = lookup_session_by_app(usess, app);
2199 if (ua_sess == NULL) {
2200 goto error_rcu_unlock;
2201 }
2202
2203 /* Upon restart, we skip the setup, already done */
2204 if (ua_sess->started) {
2205 goto skip_setup;
2206 }
2207
2208 /* Create directories if consumer is LOCAL and has a path defined. */
2209 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2210 strlen(usess->consumer->dst.trace_path) > 0) {
2211 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2212 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2213 if (ret < 0) {
2214 if (ret != -EEXIST) {
2215 ERR("Trace directory creation error");
2216 ret = -1;
2217 goto error_rcu_unlock;
2218 }
2219 }
2220 }
2221
2222 /* Indicate that the session has been started once */
2223 ua_sess->started = 1;
2224
2225 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
2226 if (ret < 0) {
2227 ret = LTTNG_ERR_UST_META_FAIL;
2228 goto error_rcu_unlock;
2229 }
2230
2231 /* For each channel */
2232 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2233 node.node) {
2234 /* Create all streams */
2235 while (1) {
2236 /* Create UST stream */
2237 ustream = zmalloc(sizeof(*ustream));
2238 if (ustream == NULL) {
2239 PERROR("zmalloc ust stream");
2240 goto error_rcu_unlock;
2241 }
2242
2243 /* We are going to receive 2 fds, we need to reserve them. */
2244 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2245 if (ret < 0) {
2246 ERR("Exhausted number of available FD upon stream create");
2247 free(ustream);
2248 goto error_rcu_unlock;
2249 }
2250
2251 health_code_update(&health_thread_cmd);
2252
2253 ret = ustctl_create_stream(app->sock, ua_chan->obj,
2254 &ustream->obj);
2255 if (ret < 0) {
2256 /* Got all streams */
2257 lttng_fd_put(LTTNG_FD_APPS, 2);
2258 free(ustream);
2259 ret = LTTNG_ERR_UST_STREAM_FAIL;
2260 break;
2261 }
2262 ustream->handle = ustream->obj->handle;
2263
2264 health_code_update(&health_thread_cmd);
2265
2266 /* Order is important */
2267 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
2268 ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u",
2269 ua_chan->name, ua_chan->streams.count);
2270 ua_chan->streams.count++;
2271 if (ret < 0) {
2272 PERROR("asprintf UST create stream");
2273 /*
2274 * XXX what should we do here with the
2275 * stream ?
2276 */
2277 continue;
2278 }
2279 DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count,
2280 ustream->handle);
2281 }
2282
2283 health_code_update(&health_thread_cmd);
2284 }
2285
2286 switch (app->bits_per_long) {
2287 case 64:
2288 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
2289 usess->consumer);
2290 if (socket == NULL) {
2291 goto skip_setup;
2292 }
2293 break;
2294 case 32:
2295 socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd),
2296 usess->consumer);
2297 if (socket == NULL) {
2298 goto skip_setup;
2299 }
2300 break;
2301 default:
2302 ret = -EINVAL;
2303 goto error_rcu_unlock;
2304 }
2305
2306 /* Setup UST consumer socket and send fds to it */
2307 ret = ust_consumer_send_session(ua_sess, usess->consumer, socket);
2308 if (ret < 0) {
2309 goto error_rcu_unlock;
2310 }
2311
2312 health_code_update(&health_thread_cmd);
2313
2314 skip_setup:
2315 /* This start the UST tracing */
2316 ret = ustctl_start_session(app->sock, ua_sess->handle);
2317 if (ret < 0) {
2318 ERR("Error starting tracing for app pid: %d", app->pid);
2319 goto error_rcu_unlock;
2320 }
2321
2322 health_code_update(&health_thread_cmd);
2323
2324 /* Quiescent wait after starting trace */
2325 ustctl_wait_quiescent(app->sock);
2326
2327 end:
2328 rcu_read_unlock();
2329 health_code_update(&health_thread_cmd);
2330 return 0;
2331
2332 error_rcu_unlock:
2333 rcu_read_unlock();
2334 health_code_update(&health_thread_cmd);
2335 return -1;
2336 }
2337
2338 /*
2339 * Stop tracing for a specific UST session and app.
2340 */
2341 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2342 {
2343 int ret = 0;
2344 struct lttng_ht_iter iter;
2345 struct ust_app_session *ua_sess;
2346 struct ust_app_channel *ua_chan;
2347
2348 DBG("Stopping tracing for ust app pid %d", app->pid);
2349
2350 rcu_read_lock();
2351
2352 if (!app->compatible) {
2353 goto end;
2354 }
2355
2356 ua_sess = lookup_session_by_app(usess, app);
2357 if (ua_sess == NULL) {
2358 /* Only malloc can failed so something is really wrong */
2359 goto error_rcu_unlock;
2360 }
2361
2362 /*
2363 * If started = 0, it means that stop trace has been called for a session
2364 * that was never started. This is a code flow error and should never
2365 * happen.
2366 */
2367 assert(ua_sess->started == 1);
2368
2369 health_code_update(&health_thread_cmd);
2370
2371 /* This inhibits UST tracing */
2372 ret = ustctl_stop_session(app->sock, ua_sess->handle);
2373 if (ret < 0) {
2374 ERR("Error stopping tracing for app pid: %d", app->pid);
2375 goto error_rcu_unlock;
2376 }
2377
2378 health_code_update(&health_thread_cmd);
2379
2380 /* Quiescent wait after stopping trace */
2381 ustctl_wait_quiescent(app->sock);
2382
2383 health_code_update(&health_thread_cmd);
2384
2385 /* Flushing buffers */
2386 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2387 node.node) {
2388 health_code_update(&health_thread_cmd);
2389 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
2390 if (ret < 0) {
2391 ERR("UST app PID %d channel %s flush failed with ret %d",
2392 app->pid, ua_chan->name, ret);
2393 /* Continuing flushing all buffers */
2394 continue;
2395 }
2396 }
2397
2398 health_code_update(&health_thread_cmd);
2399
2400 /* Flush all buffers before stopping */
2401 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
2402 if (ret < 0) {
2403 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
2404 ret);
2405 }
2406
2407 end:
2408 rcu_read_unlock();
2409 health_code_update(&health_thread_cmd);
2410 return 0;
2411
2412 error_rcu_unlock:
2413 rcu_read_unlock();
2414 health_code_update(&health_thread_cmd);
2415 return -1;
2416 }
2417
2418 /*
2419 * Destroy a specific UST session in apps.
2420 */
2421 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2422 {
2423 struct ust_app_session *ua_sess;
2424 struct lttng_ust_object_data obj;
2425 struct lttng_ht_iter iter;
2426 struct lttng_ht_node_ulong *node;
2427 int ret;
2428
2429 DBG("Destroy tracing for ust app pid %d", app->pid);
2430
2431 rcu_read_lock();
2432
2433 if (!app->compatible) {
2434 goto end;
2435 }
2436
2437 __lookup_session_by_app(usess, app, &iter);
2438 node = lttng_ht_iter_get_node_ulong(&iter);
2439 if (node == NULL) {
2440 /* Only malloc can failed so something is really wrong */
2441 goto error_rcu_unlock;
2442 }
2443 ua_sess = caa_container_of(node, struct ust_app_session, node);
2444 ret = lttng_ht_del(app->sessions, &iter);
2445 assert(!ret);
2446 obj.handle = ua_sess->handle;
2447 obj.shm_fd = -1;
2448 obj.wait_fd = -1;
2449 obj.memory_map_size = 0;
2450 health_code_update(&health_thread_cmd);
2451 ustctl_release_object(app->sock, &obj);
2452
2453 health_code_update(&health_thread_cmd);
2454 delete_ust_app_session(app->sock, ua_sess);
2455
2456 /* Quiescent wait after stopping trace */
2457 ustctl_wait_quiescent(app->sock);
2458
2459 end:
2460 rcu_read_unlock();
2461 health_code_update(&health_thread_cmd);
2462 return 0;
2463
2464 error_rcu_unlock:
2465 rcu_read_unlock();
2466 health_code_update(&health_thread_cmd);
2467 return -1;
2468 }
2469
2470 /*
2471 * Start tracing for the UST session.
2472 */
2473 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2474 {
2475 int ret = 0;
2476 struct lttng_ht_iter iter;
2477 struct ust_app *app;
2478
2479 DBG("Starting all UST traces");
2480
2481 rcu_read_lock();
2482
2483 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2484 ret = ust_app_start_trace(usess, app);
2485 if (ret < 0) {
2486 /* Continue to next apps even on error */
2487 continue;
2488 }
2489 }
2490
2491 rcu_read_unlock();
2492
2493 return 0;
2494 }
2495
2496 /*
2497 * Start tracing for the UST session.
2498 */
2499 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2500 {
2501 int ret = 0;
2502 struct lttng_ht_iter iter;
2503 struct ust_app *app;
2504
2505 DBG("Stopping all UST traces");
2506
2507 rcu_read_lock();
2508
2509 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2510 ret = ust_app_stop_trace(usess, app);
2511 if (ret < 0) {
2512 /* Continue to next apps even on error */
2513 continue;
2514 }
2515 }
2516
2517 rcu_read_unlock();
2518
2519 return 0;
2520 }
2521
2522 /*
2523 * Destroy app UST session.
2524 */
2525 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2526 {
2527 int ret = 0;
2528 struct lttng_ht_iter iter;
2529 struct ust_app *app;
2530
2531 DBG("Destroy all UST traces");
2532
2533 rcu_read_lock();
2534
2535 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2536 ret = ust_app_destroy_trace(usess, app);
2537 if (ret < 0) {
2538 /* Continue to next apps even on error */
2539 continue;
2540 }
2541 }
2542
2543 rcu_read_unlock();
2544
2545 return 0;
2546 }
2547
2548 /*
2549 * Add channels/events from UST global domain to registered apps at sock.
2550 */
2551 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2552 {
2553 int ret = 0;
2554 struct lttng_ht_iter iter, uiter, iter_ctx;
2555 struct ust_app *app;
2556 struct ust_app_session *ua_sess;
2557 struct ust_app_channel *ua_chan;
2558 struct ust_app_event *ua_event;
2559 struct ust_app_ctx *ua_ctx;
2560
2561 if (usess == NULL) {
2562 ERR("No UST session on global update. Returning");
2563 goto error;
2564 }
2565
2566 DBG2("UST app global update for app sock %d for session id %d", sock,
2567 usess->id);
2568
2569 rcu_read_lock();
2570
2571 app = find_app_by_sock(sock);
2572 if (app == NULL) {
2573 ERR("Failed to update app sock %d", sock);
2574 goto error;
2575 }
2576
2577 if (!app->compatible) {
2578 goto error;
2579 }
2580
2581 ua_sess = create_ust_app_session(usess, app);
2582 if (ua_sess == NULL || ua_sess == (void *) -1UL) {
2583 /* Tracer is gone for this session and has been freed */
2584 goto error;
2585 }
2586
2587 /*
2588 * We can iterate safely here over all UST app session sicne the create ust
2589 * app session above made a shadow copy of the UST global domain from the
2590 * ltt ust session.
2591 */
2592 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2593 node.node) {
2594 ret = create_ust_channel(app, ua_sess, ua_chan);
2595 if (ret < 0) {
2596 /* FIXME: Should we quit here or continue... */
2597 continue;
2598 }
2599
2600 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2601 node.node) {
2602 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2603 if (ret < 0) {
2604 /* FIXME: Should we quit here or continue... */
2605 continue;
2606 }
2607 }
2608
2609
2610 /* For each events */
2611 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2612 node.node) {
2613 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2614 if (ret < 0) {
2615 /* FIXME: Should we quit here or continue... */
2616 continue;
2617 }
2618
2619 ret = set_ust_event_filter(ua_event, app);
2620 if (ret < 0) {
2621 /* FIXME: Should we quit here or continue... */
2622 continue;
2623 }
2624 }
2625 }
2626
2627 if (usess->start_trace) {
2628 ret = ust_app_start_trace(usess, app);
2629 if (ret < 0) {
2630 goto error;
2631 }
2632
2633 DBG2("UST trace started for app pid %d", app->pid);
2634 }
2635
2636 error:
2637 rcu_read_unlock();
2638 return;
2639 }
2640
2641 /*
2642 * Add context to a specific channel for global UST domain.
2643 */
2644 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2645 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2646 {
2647 int ret = 0;
2648 struct lttng_ht_node_str *ua_chan_node;
2649 struct lttng_ht_iter iter, uiter;
2650 struct ust_app_channel *ua_chan = NULL;
2651 struct ust_app_session *ua_sess;
2652 struct ust_app *app;
2653
2654 rcu_read_lock();
2655
2656 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2657 if (!app->compatible) {
2658 /*
2659 * TODO: In time, we should notice the caller of this error by
2660 * telling him that this is a version error.
2661 */
2662 continue;
2663 }
2664 ua_sess = lookup_session_by_app(usess, app);
2665 if (ua_sess == NULL) {
2666 continue;
2667 }
2668
2669 /* Lookup channel in the ust app session */
2670 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2671 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2672 if (ua_chan_node == NULL) {
2673 continue;
2674 }
2675 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2676 node);
2677
2678 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2679 if (ret < 0) {
2680 continue;
2681 }
2682 }
2683
2684 rcu_read_unlock();
2685 return ret;
2686 }
2687
2688 /*
2689 * Enable event for a channel from a UST session for a specific PID.
2690 */
2691 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2692 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2693 {
2694 int ret = 0;
2695 struct lttng_ht_iter iter;
2696 struct lttng_ht_node_str *ua_chan_node;
2697 struct ust_app *app;
2698 struct ust_app_session *ua_sess;
2699 struct ust_app_channel *ua_chan;
2700 struct ust_app_event *ua_event;
2701
2702 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2703
2704 rcu_read_lock();
2705
2706 app = ust_app_find_by_pid(pid);
2707 if (app == NULL) {
2708 ERR("UST app enable event per PID %d not found", pid);
2709 ret = -1;
2710 goto error;
2711 }
2712
2713 if (!app->compatible) {
2714 ret = 0;
2715 goto error;
2716 }
2717
2718 ua_sess = lookup_session_by_app(usess, app);
2719 /* If ua_sess is NULL, there is a code flow error */
2720 assert(ua_sess);
2721
2722 /* Lookup channel in the ust app session */
2723 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2724 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2725 /* If the channel is not found, there is a code flow error */
2726 assert(ua_chan_node);
2727
2728 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2729
2730 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2731 uevent->filter, uevent->attr.loglevel);
2732 if (ua_event == NULL) {
2733 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2734 if (ret < 0) {
2735 goto error;
2736 }
2737 } else {
2738 ret = enable_ust_app_event(ua_sess, ua_event, app);
2739 if (ret < 0) {
2740 goto error;
2741 }
2742 }
2743
2744 error:
2745 rcu_read_unlock();
2746 return ret;
2747 }
2748
2749 /*
2750 * Disable event for a channel from a UST session for a specific PID.
2751 */
2752 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2753 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2754 {
2755 int ret = 0;
2756 struct lttng_ht_iter iter;
2757 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2758 struct ust_app *app;
2759 struct ust_app_session *ua_sess;
2760 struct ust_app_channel *ua_chan;
2761 struct ust_app_event *ua_event;
2762
2763 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2764
2765 rcu_read_lock();
2766
2767 app = ust_app_find_by_pid(pid);
2768 if (app == NULL) {
2769 ERR("UST app disable event per PID %d not found", pid);
2770 ret = -1;
2771 goto error;
2772 }
2773
2774 if (!app->compatible) {
2775 ret = 0;
2776 goto error;
2777 }
2778
2779 ua_sess = lookup_session_by_app(usess, app);
2780 /* If ua_sess is NULL, there is a code flow error */
2781 assert(ua_sess);
2782
2783 /* Lookup channel in the ust app session */
2784 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2785 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2786 if (ua_chan_node == NULL) {
2787 /* Channel does not exist, skip disabling */
2788 goto error;
2789 }
2790 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2791
2792 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2793 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2794 if (ua_event_node == NULL) {
2795 /* Event does not exist, skip disabling */
2796 goto error;
2797 }
2798 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2799
2800 ret = disable_ust_app_event(ua_sess, ua_event, app);
2801 if (ret < 0) {
2802 goto error;
2803 }
2804
2805 error:
2806 rcu_read_unlock();
2807 return ret;
2808 }
2809
2810 /*
2811 * Validate version of UST apps and set the compatible bit.
2812 */
2813 int ust_app_validate_version(int sock)
2814 {
2815 int ret;
2816 struct ust_app *app;
2817
2818 rcu_read_lock();
2819
2820 app = find_app_by_sock(sock);
2821 assert(app);
2822
2823 health_code_update(&health_thread_cmd);
2824
2825 ret = ustctl_tracer_version(sock, &app->version);
2826 if (ret < 0) {
2827 goto error;
2828 }
2829
2830 /* Validate version */
2831 if (app->version.major != UST_APP_MAJOR_VERSION) {
2832 goto error;
2833 }
2834
2835 DBG2("UST app PID %d is compatible with internal major version %d "
2836 "(supporting == %d)", app->pid, app->version.major,
2837 UST_APP_MAJOR_VERSION);
2838 app->compatible = 1;
2839 rcu_read_unlock();
2840 health_code_update(&health_thread_cmd);
2841 return 0;
2842
2843 error:
2844 DBG2("UST app PID %d is not compatible with internal major version %d "
2845 "(supporting == %d)", app->pid, app->version.major,
2846 UST_APP_MAJOR_VERSION);
2847 app->compatible = 0;
2848 rcu_read_unlock();
2849 health_code_update(&health_thread_cmd);
2850 return -1;
2851 }
2852
2853 /*
2854 * Calibrate registered applications.
2855 */
2856 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2857 {
2858 int ret = 0;
2859 struct lttng_ht_iter iter;
2860 struct ust_app *app;
2861
2862 rcu_read_lock();
2863
2864 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2865 if (!app->compatible) {
2866 /*
2867 * TODO: In time, we should notice the caller of this error by
2868 * telling him that this is a version error.
2869 */
2870 continue;
2871 }
2872
2873 health_code_update(&health_thread_cmd);
2874
2875 ret = ustctl_calibrate(app->sock, calibrate);
2876 if (ret < 0) {
2877 switch (ret) {
2878 case -ENOSYS:
2879 /* Means that it's not implemented on the tracer side. */
2880 ret = 0;
2881 break;
2882 default:
2883 /* TODO: Report error to user */
2884 DBG2("Calibrate app PID %d returned with error %d",
2885 app->pid, ret);
2886 break;
2887 }
2888 }
2889 }
2890
2891 DBG("UST app global domain calibration finished");
2892
2893 rcu_read_unlock();
2894
2895 health_code_update(&health_thread_cmd);
2896
2897 return ret;
2898 }
This page took 0.098994 seconds and 3 git commands to generate.