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