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