Merge branch 'master' of git://git.lttng.org/lttng-tools
[lttng-tools.git] / 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <urcu/compiler.h>
30 #include <lttngerr.h>
31 #include <lttng-share.h>
32
33 #include "hashtable.h"
34 #include "ust-app.h"
35 #include "ust-consumer.h"
36 #include "ust-ctl.h"
37
38 /*
39 * Delete ust context safely. RCU read lock must be held before calling
40 * this function.
41 */
42 static
43 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
44 {
45 if (ua_ctx->obj) {
46 ustctl_release_object(sock, ua_ctx->obj);
47 free(ua_ctx->obj);
48 }
49 free(ua_ctx);
50 }
51
52 /*
53 * Delete ust app event safely. RCU read lock must be held before calling
54 * this function.
55 */
56 static void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
57 {
58 int ret;
59 struct cds_lfht_iter iter;
60 struct ust_app_ctx *ua_ctx;
61
62 cds_lfht_for_each_entry(ua_event->ctx, &iter, ua_ctx, node) {
63 ret = hashtable_del(ua_event->ctx, &iter);
64 assert(!ret);
65 delete_ust_app_ctx(sock, ua_ctx);
66 }
67 ret = hashtable_destroy(ua_event->ctx);
68 assert(!ret);
69
70 if (ua_event->obj != NULL) {
71 ustctl_release_object(sock, ua_event->obj);
72 free(ua_event->obj);
73 }
74 free(ua_event);
75 }
76
77 /*
78 * Delete ust app stream safely. RCU read lock must be held before calling
79 * this function.
80 */
81 static void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
82 {
83 ustctl_release_object(sock, stream->obj);
84 free(stream->obj);
85 free(stream);
86 }
87
88 /*
89 * Delete ust app channel safely. RCU read lock must be held before calling
90 * this function.
91 */
92 static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
93 {
94 int ret;
95 struct cds_lfht_iter iter;
96 struct ust_app_event *ua_event;
97 struct ust_app_ctx *ua_ctx;
98 struct ltt_ust_stream *stream, *stmp;
99
100 /* Wipe stream */
101 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
102 cds_list_del(&stream->list);
103 delete_ust_app_stream(sock, stream);
104 }
105
106 /* Wipe context */
107 cds_lfht_for_each_entry(ua_chan->ctx, &iter, ua_ctx, node) {
108 ret = hashtable_del(ua_chan->ctx, &iter);
109 assert(!ret);
110 delete_ust_app_ctx(sock, ua_ctx);
111 }
112 ret = hashtable_destroy(ua_chan->ctx);
113 assert(!ret);
114
115 /* Wipe events */
116 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
117 ret = hashtable_del(ua_chan->events, &iter);
118 assert(!ret);
119 delete_ust_app_event(sock, ua_event);
120 }
121 ret = hashtable_destroy(ua_chan->events);
122 assert(!ret);
123
124 if (ua_chan->obj != NULL) {
125 ustctl_release_object(sock, ua_chan->obj);
126 free(ua_chan->obj);
127 }
128 free(ua_chan);
129 }
130
131 /*
132 * Delete ust app session safely. RCU read lock must be held before calling
133 * this function.
134 */
135 static void delete_ust_app_session(int sock,
136 struct ust_app_session *ua_sess)
137 {
138 int ret;
139 struct cds_lfht_iter iter;
140 struct ust_app_channel *ua_chan;
141
142 if (ua_sess->metadata) {
143 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
144 free(ua_sess->metadata->stream_obj);
145 ustctl_release_object(sock, ua_sess->metadata->obj);
146 free(ua_sess->metadata->obj);
147 }
148
149 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
150 ret = hashtable_del(ua_sess->channels, &iter);
151 assert(!ret);
152 delete_ust_app_channel(sock, ua_chan);
153 }
154
155 ret = hashtable_destroy(ua_sess->channels);
156 if (ret < 0) {
157 ERR("UST app destroy session hashtable failed");
158 goto error;
159 }
160
161 error:
162 return;
163 }
164
165 /*
166 * Delete a traceable application structure from the global list. Never call
167 * this function outside of a call_rcu call.
168 */
169 static void delete_ust_app(struct ust_app *app)
170 {
171 int ret;
172 struct cds_lfht_node *node;
173 struct cds_lfht_iter iter;
174 struct ust_app_session *ua_sess;
175 int sock;
176
177 rcu_read_lock();
178
179 /* Remove from key hash table */
180 node = hashtable_lookup(ust_app_sock_key_map,
181 (void *) ((unsigned long) app->key.sock), sizeof(void *), &iter);
182 if (node == NULL) {
183 /* Not suppose to happen */
184 ERR("UST app key %d not found in key hash table", app->key.sock);
185 goto end;
186 }
187
188 ret = hashtable_del(ust_app_sock_key_map, &iter);
189 if (ret) {
190 ERR("UST app unable to delete app sock %d from key hash table",
191 app->key.sock);
192 } else {
193 DBG2("UST app pair sock %d key %d deleted",
194 app->key.sock, app->key.pid);
195 }
196
197 /* Socket is already closed at this point */
198
199 /* Delete ust app sessions info */
200 sock = app->key.sock;
201 app->key.sock = -1;
202
203 cds_lfht_for_each_entry(app->sessions, &iter, ua_sess, node) {
204 ret = hashtable_del(app->sessions, &iter);
205 assert(!ret);
206 delete_ust_app_session(app->key.sock, ua_sess);
207 }
208
209 ret = hashtable_destroy(app->sessions);
210 if (ret < 0) {
211 ERR("UST app destroy session hashtable failed");
212 goto end;
213 }
214
215 /*
216 * Wait until we have removed the key from the sock hash table
217 * before closing this socket, otherwise an application could
218 * re-use the socket ID and race with the teardown, using the
219 * same hash table entry.
220 */
221 close(sock);
222
223 DBG2("UST app pid %d deleted", app->key.pid);
224 free(app);
225 end:
226 rcu_read_unlock();
227 }
228
229 /*
230 * URCU intermediate call to delete an UST app.
231 */
232 static void delete_ust_app_rcu(struct rcu_head *head)
233 {
234 struct cds_lfht_node *node =
235 caa_container_of(head, struct cds_lfht_node, head);
236 struct ust_app *app =
237 caa_container_of(node, struct ust_app, node);
238
239 delete_ust_app(app);
240 }
241
242 /*
243 * Find an ust_app using the sock and return it. RCU read side lock must be
244 * held before calling this helper function.
245 */
246 static struct ust_app *find_app_by_sock(int sock)
247 {
248 struct cds_lfht_node *node;
249 struct ust_app_key *key;
250 struct cds_lfht_iter iter;
251
252 node = hashtable_lookup(ust_app_sock_key_map,
253 (void *)((unsigned long) sock), sizeof(void *), &iter);
254 if (node == NULL) {
255 DBG2("UST app find by sock %d key not found", sock);
256 goto error;
257 }
258
259 key = caa_container_of(node, struct ust_app_key, node);
260
261 node = hashtable_lookup(ust_app_ht,
262 (void *)((unsigned long) key->pid), sizeof(void *), &iter);
263 if (node == NULL) {
264 DBG2("UST app find by sock %d not found", sock);
265 goto error;
266 }
267 return caa_container_of(node, struct ust_app, node);
268
269 error:
270 return NULL;
271 }
272
273 /*
274 * Create the channel context on the tracer.
275 */
276 static
277 int create_ust_channel_context(struct ust_app_channel *ua_chan,
278 struct ust_app_ctx *ua_ctx, struct ust_app *app)
279 {
280 int ret;
281
282 ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx,
283 ua_chan->obj, &ua_ctx->obj);
284 if (ret < 0) {
285 goto error;
286 }
287
288 ua_ctx->handle = ua_ctx->obj->handle;
289
290 DBG2("UST app context added to channel %s successfully", ua_chan->name);
291
292 error:
293 return ret;
294 }
295
296 /*
297 * Create the event context on the tracer.
298 */
299 static
300 int create_ust_event_context(struct ust_app_event *ua_event,
301 struct ust_app_ctx *ua_ctx, struct ust_app *app)
302 {
303 int ret;
304
305 ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx,
306 ua_event->obj, &ua_ctx->obj);
307 if (ret < 0) {
308 goto error;
309 }
310
311 ua_ctx->handle = ua_ctx->obj->handle;
312
313 DBG2("UST app context added to event %s successfully", ua_event->name);
314
315 error:
316 return ret;
317 }
318
319 /*
320 * Disable the specified event on to UST tracer for the UST session.
321 */
322 static int disable_ust_event(struct ust_app *app,
323 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
324 {
325 int ret;
326
327 ret = ustctl_disable(app->key.sock, ua_event->obj);
328 if (ret < 0) {
329 ERR("UST app event %s disable failed for app (pid: %d) "
330 "and session handle %d with ret %d",
331 ua_event->attr.name, app->key.pid, ua_sess->handle, ret);
332 goto error;
333 }
334
335 DBG2("UST app event %s disabled successfully for app (pid: %d)",
336 ua_event->attr.name, app->key.pid);
337
338 error:
339 return ret;
340 }
341
342 /*
343 * Disable the specified channel on to UST tracer for the UST session.
344 */
345 static int disable_ust_channel(struct ust_app *app,
346 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
347 {
348 int ret;
349
350 ret = ustctl_disable(app->key.sock, ua_chan->obj);
351 if (ret < 0) {
352 ERR("UST app channel %s disable failed for app (pid: %d) "
353 "and session handle %d with ret %d",
354 ua_chan->name, app->key.pid, ua_sess->handle, ret);
355 goto error;
356 }
357
358 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
359 ua_chan->name, app->key.pid);
360
361 error:
362 return ret;
363 }
364
365 /*
366 * Enable the specified channel on to UST tracer for the UST session.
367 */
368 static int enable_ust_channel(struct ust_app *app,
369 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
370 {
371 int ret;
372
373 ret = ustctl_enable(app->key.sock, ua_chan->obj);
374 if (ret < 0) {
375 ERR("UST app channel %s enable failed for app (pid: %d) "
376 "and session handle %d with ret %d",
377 ua_chan->name, app->key.pid, ua_sess->handle, ret);
378 goto error;
379 }
380
381 ua_chan->enabled = 1;
382
383 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
384 ua_chan->name, app->key.pid);
385
386 error:
387 return ret;
388 }
389
390 /*
391 * Enable the specified event on to UST tracer for the UST session.
392 */
393 static int enable_ust_event(struct ust_app *app,
394 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
395 {
396 int ret;
397
398 ret = ustctl_enable(app->key.sock, ua_event->obj);
399 if (ret < 0) {
400 ERR("UST app event %s enable failed for app (pid: %d) "
401 "and session handle %d with ret %d",
402 ua_event->attr.name, app->key.pid, ua_sess->handle, ret);
403 goto error;
404 }
405
406 DBG2("UST app event %s enabled successfully for app (pid: %d)",
407 ua_event->attr.name, app->key.pid);
408
409 error:
410 return ret;
411 }
412
413 /*
414 * Open metadata onto the UST tracer for a UST session.
415 */
416 static int open_ust_metadata(struct ust_app *app,
417 struct ust_app_session *ua_sess)
418 {
419 int ret;
420 struct lttng_ust_channel_attr uattr;
421
422 uattr.overwrite = ua_sess->metadata->attr.overwrite;
423 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
424 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
425 uattr.switch_timer_interval =
426 ua_sess->metadata->attr.switch_timer_interval;
427 uattr.read_timer_interval =
428 ua_sess->metadata->attr.read_timer_interval;
429 uattr.output = ua_sess->metadata->attr.output;
430
431 /* UST tracer metadata creation */
432 ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
433 &ua_sess->metadata->obj);
434 if (ret < 0) {
435 ERR("UST app open metadata failed for app pid:%d",
436 app->key.pid);
437 goto error;
438 }
439
440 error:
441 return ret;
442 }
443
444 /*
445 * Create stream onto the UST tracer for a UST session.
446 */
447 static int create_ust_stream(struct ust_app *app,
448 struct ust_app_session *ua_sess)
449 {
450 int ret;
451
452 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
453 &ua_sess->metadata->stream_obj);
454 if (ret < 0) {
455 ERR("UST create metadata stream failed");
456 goto error;
457 }
458
459 error:
460 return ret;
461 }
462
463 /*
464 * Create the specified channel onto the UST tracer for a UST session.
465 */
466 static int create_ust_channel(struct ust_app *app,
467 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
468 {
469 int ret;
470
471 /* TODO: remove cast and use lttng-ust-abi.h */
472 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
473 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
474 if (ret < 0) {
475 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
476 "and session handle %d with ret %d",
477 ua_chan->name, app->key.pid, app->key.sock,
478 ua_sess->handle, ret);
479 goto error;
480 }
481
482 ua_chan->handle = ua_chan->obj->handle;
483 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
484 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
485 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
486
487 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
488 ua_chan->name, app->key.pid, app->key.sock);
489
490 /* If channel is not enabled, disable it on the tracer */
491 if (!ua_chan->enabled) {
492 ret = disable_ust_channel(app, ua_sess, ua_chan);
493 if (ret < 0) {
494 goto error;
495 }
496 }
497
498 error:
499 return ret;
500 }
501
502 /*
503 * Create the specified event onto the UST tracer for a UST session.
504 */
505 static
506 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
507 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
508 {
509 int ret = 0;
510
511 /* Create UST event on tracer */
512 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
513 &ua_event->obj);
514 if (ret < 0) {
515 ERR("Error ustctl create event %s for app pid: %d with ret %d",
516 ua_event->attr.name, app->key.pid, ret);
517 goto error;
518 }
519
520 ua_event->handle = ua_event->obj->handle;
521
522 DBG2("UST app event %s created successfully for pid:%d",
523 ua_event->attr.name, app->key.pid);
524
525 /* If event not enabled, disable it on the tracer */
526 if (!ua_event->enabled) {
527 ret = disable_ust_event(app, ua_sess, ua_event);
528 if (ret < 0) {
529 goto error;
530 }
531 }
532
533 error:
534 return ret;
535 }
536
537 /*
538 * Alloc new UST app session.
539 */
540 static struct ust_app_session *alloc_ust_app_session(void)
541 {
542 struct ust_app_session *ua_sess;
543
544 /* Init most of the default value by allocating and zeroing */
545 ua_sess = zmalloc(sizeof(struct ust_app_session));
546 if (ua_sess == NULL) {
547 PERROR("malloc");
548 goto error;
549 }
550
551 ua_sess->handle = -1;
552 ua_sess->channels = hashtable_new_str(0);
553
554 return ua_sess;
555
556 error:
557 return NULL;
558 }
559
560 /*
561 * Alloc new UST app channel.
562 */
563 static struct ust_app_channel *alloc_ust_app_channel(char *name,
564 struct lttng_ust_channel *attr)
565 {
566 struct ust_app_channel *ua_chan;
567
568 /* Init most of the default value by allocating and zeroing */
569 ua_chan = zmalloc(sizeof(struct ust_app_channel));
570 if (ua_chan == NULL) {
571 PERROR("malloc");
572 goto error;
573 }
574
575 /* Setup channel name */
576 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
577 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
578
579 ua_chan->enabled = 1;
580 ua_chan->handle = -1;
581 ua_chan->ctx = hashtable_new(0);
582 ua_chan->events = hashtable_new_str(0);
583 hashtable_node_init(&ua_chan->node, (void *) ua_chan->name,
584 strlen(ua_chan->name));
585
586 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
587
588 /* Copy attributes */
589 if (attr) {
590 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
591 }
592
593 DBG3("UST app channel %s allocated", ua_chan->name);
594
595 return ua_chan;
596
597 error:
598 return NULL;
599 }
600
601 /*
602 * Alloc new UST app event.
603 */
604 static struct ust_app_event *alloc_ust_app_event(char *name,
605 struct lttng_ust_event *attr)
606 {
607 struct ust_app_event *ua_event;
608
609 /* Init most of the default value by allocating and zeroing */
610 ua_event = zmalloc(sizeof(struct ust_app_event));
611 if (ua_event == NULL) {
612 PERROR("malloc");
613 goto error;
614 }
615
616 ua_event->enabled = 1;
617 strncpy(ua_event->name, name, sizeof(ua_event->name));
618 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
619 ua_event->ctx = hashtable_new(0);
620 hashtable_node_init(&ua_event->node, (void *) ua_event->name,
621 strlen(ua_event->name));
622
623 /* Copy attributes */
624 if (attr) {
625 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
626 }
627
628 DBG3("UST app event %s allocated", ua_event->name);
629
630 return ua_event;
631
632 error:
633 return NULL;
634 }
635
636 /*
637 * Alloc new UST app context.
638 */
639 static
640 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
641 {
642 struct ust_app_ctx *ua_ctx;
643
644 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
645 if (ua_ctx == NULL) {
646 goto error;
647 }
648
649 if (uctx) {
650 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
651 }
652
653 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
654
655 error:
656 return ua_ctx;
657 }
658
659 /*
660 * Copy data between an UST app event and a LTT event.
661 */
662 static void shadow_copy_event(struct ust_app_event *ua_event,
663 struct ltt_ust_event *uevent)
664 {
665 struct cds_lfht_iter iter;
666 struct ltt_ust_context *uctx;
667 struct ust_app_ctx *ua_ctx;
668
669 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
670 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
671
672 /* Copy event attributes */
673 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
674
675 cds_lfht_for_each_entry(uevent->ctx, &iter, uctx, node) {
676 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
677 if (ua_ctx == NULL) {
678 continue;
679 }
680 hashtable_node_init(&ua_ctx->node,
681 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
682 hashtable_add_unique(ua_event->ctx, &ua_ctx->node);
683 }
684 }
685
686 /*
687 * Copy data between an UST app channel and a LTT channel.
688 */
689 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
690 struct ltt_ust_channel *uchan)
691 {
692 struct cds_lfht_iter iter;
693 struct cds_lfht_node *ua_event_node;
694 struct ltt_ust_event *uevent;
695 struct ltt_ust_context *uctx;
696 struct ust_app_event *ua_event;
697 struct ust_app_ctx *ua_ctx;
698
699 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
700
701 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
702 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
703 /* Copy event attributes */
704 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
705
706 cds_lfht_for_each_entry(uchan->ctx, &iter, uctx, node) {
707 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
708 if (ua_ctx == NULL) {
709 continue;
710 }
711 hashtable_node_init(&ua_ctx->node,
712 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
713 hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
714 }
715
716 /* Copy all events from ltt ust channel to ust app channel */
717 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
718 struct cds_lfht_iter uiter;
719
720 ua_event_node = hashtable_lookup(ua_chan->events,
721 (void *) uevent->attr.name, strlen(uevent->attr.name),
722 &uiter);
723 if (ua_event_node == NULL) {
724 DBG2("UST event %s not found on shadow copy channel",
725 uevent->attr.name);
726 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
727 if (ua_event == NULL) {
728 continue;
729 }
730 shadow_copy_event(ua_event, uevent);
731 hashtable_add_unique(ua_chan->events, &ua_event->node);
732 }
733 }
734
735 DBG3("Shadow copy channel done");
736 }
737
738 /*
739 * Copy data between a UST app session and a regular LTT session.
740 */
741 static void shadow_copy_session(struct ust_app_session *ua_sess,
742 struct ltt_ust_session *usess,
743 struct ust_app *app)
744 {
745 struct cds_lfht_node *ua_chan_node;
746 struct cds_lfht_iter iter;
747 struct ltt_ust_channel *uchan;
748 struct ust_app_channel *ua_chan;
749 time_t rawtime;
750 struct tm *timeinfo;
751 char datetime[16];
752 int ret;
753
754 /* Get date and time for unique app path */
755 time(&rawtime);
756 timeinfo = localtime(&rawtime);
757 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
758
759 DBG2("Shadow copy of session handle %d", ua_sess->handle);
760
761 ua_sess->uid = usess->uid;
762
763 ret = snprintf(ua_sess->path, PATH_MAX,
764 "%s/%s-%d-%s",
765 usess->pathname, app->name, app->key.pid,
766 datetime);
767 if (ret < 0) {
768 PERROR("asprintf UST shadow copy session");
769 /* TODO: We cannot return an error from here.. */
770 assert(0);
771 }
772
773 /* TODO: support all UST domain */
774
775 /* Iterate over all channels in global domain. */
776 cds_lfht_for_each_entry(usess->domain_global.channels, &iter,
777 uchan, node) {
778 struct cds_lfht_iter uiter;
779
780 ua_chan_node = hashtable_lookup(ua_sess->channels,
781 (void *)uchan->name, strlen(uchan->name),
782 &uiter);
783 if (ua_chan_node != NULL) {
784 continue;
785 }
786
787 DBG2("Channel %s not found on shadow session copy, creating it",
788 uchan->name);
789 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
790 if (ua_chan == NULL) {
791 /* malloc failed... continuing */
792 continue;
793 }
794
795 shadow_copy_channel(ua_chan, uchan);
796 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
797 }
798 }
799
800 /*
801 * Lookup sesison wrapper.
802 */
803 static
804 void __lookup_session_by_app(struct ltt_ust_session *usess,
805 struct ust_app *app, struct cds_lfht_iter *iter)
806 {
807 /* Get right UST app session from app */
808 (void) hashtable_lookup(app->sessions,
809 (void *) ((unsigned long) usess->uid), sizeof(void *),
810 iter);
811 }
812
813 /*
814 * Return ust app session from the app session hashtable using the UST session
815 * uid.
816 */
817 static struct ust_app_session *lookup_session_by_app(
818 struct ltt_ust_session *usess, struct ust_app *app)
819 {
820 struct cds_lfht_iter iter;
821 struct cds_lfht_node *node;
822
823 __lookup_session_by_app(usess, app, &iter);
824 node = hashtable_iter_get_node(&iter);
825 if (node == NULL) {
826 goto error;
827 }
828
829 return caa_container_of(node, struct ust_app_session, node);
830
831 error:
832 return NULL;
833 }
834
835 /*
836 * Create a UST session onto the tracer of app and add it the session
837 * hashtable.
838 *
839 * Return ust app session or NULL on error.
840 */
841 static struct ust_app_session *create_ust_app_session(
842 struct ltt_ust_session *usess, struct ust_app *app)
843 {
844 int ret;
845 struct ust_app_session *ua_sess;
846
847 ua_sess = lookup_session_by_app(usess, app);
848 if (ua_sess == NULL) {
849 DBG2("UST app pid: %d session uid %d not found, creating it",
850 app->key.pid, usess->uid);
851 ua_sess = alloc_ust_app_session();
852 if (ua_sess == NULL) {
853 /* Only malloc can failed so something is really wrong */
854 goto error;
855 }
856 shadow_copy_session(ua_sess, usess, app);
857 }
858
859 if (ua_sess->handle == -1) {
860 ret = ustctl_create_session(app->key.sock);
861 if (ret < 0) {
862 ERR("Error creating session for app pid %d, sock %d",
863 app->key.pid, app->key.sock);
864 /* TODO: free() ua_sess */
865 goto error;
866 }
867
868 DBG2("UST app ustctl create session handle %d", ret);
869 ua_sess->handle = ret;
870
871 /* Add ust app session to app's HT */
872 hashtable_node_init(&ua_sess->node,
873 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
874 hashtable_add_unique(app->sessions, &ua_sess->node);
875
876 DBG2("UST app session created successfully with handle %d", ret);
877 }
878
879 return ua_sess;
880
881 error:
882 return NULL;
883 }
884
885 /*
886 * Create a context for the channel on the tracer.
887 */
888 static
889 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
890 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
891 struct ust_app *app)
892 {
893 int ret = 0;
894 struct cds_lfht_iter iter;
895 struct cds_lfht_node *node;
896 struct ust_app_ctx *ua_ctx;
897
898 DBG2("UST app adding context to channel %s", ua_chan->name);
899
900 node = hashtable_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx),
901 sizeof(void *), &iter);
902 if (node != NULL) {
903 ret = -EEXIST;
904 goto error;
905 }
906
907 ua_ctx = alloc_ust_app_ctx(uctx);
908 if (ua_ctx == NULL) {
909 /* malloc failed */
910 ret = -1;
911 goto error;
912 }
913
914 hashtable_node_init(&ua_ctx->node,
915 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
916 hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
917
918 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
919 if (ret < 0) {
920 goto error;
921 }
922
923 error:
924 return ret;
925 }
926
927 /*
928 * Create an UST context and enable it for the event on the tracer.
929 */
930 static
931 int create_ust_app_event_context(struct ust_app_session *ua_sess,
932 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
933 struct ust_app *app)
934 {
935 int ret = 0;
936 struct cds_lfht_iter iter;
937 struct cds_lfht_node *node;
938 struct ust_app_ctx *ua_ctx;
939
940 DBG2("UST app adding context to event %s", ua_event->name);
941
942 node = hashtable_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx),
943 sizeof(void *), &iter);
944 if (node != NULL) {
945 ret = -EEXIST;
946 goto error;
947 }
948
949 ua_ctx = alloc_ust_app_ctx(uctx);
950 if (ua_ctx == NULL) {
951 /* malloc failed */
952 ret = -1;
953 goto error;
954 }
955
956 hashtable_node_init(&ua_ctx->node,
957 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
958 hashtable_add_unique(ua_event->ctx, &ua_ctx->node);
959
960 ret = create_ust_event_context(ua_event, ua_ctx, app);
961 if (ret < 0) {
962 goto error;
963 }
964
965 error:
966 return ret;
967 }
968
969 /*
970 * Enable on the tracer side a ust app event for the session and channel.
971 */
972 static
973 int enable_ust_app_event(struct ust_app_session *ua_sess,
974 struct ust_app_event *ua_event, struct ust_app *app)
975 {
976 int ret;
977
978 ret = enable_ust_event(app, ua_sess, ua_event);
979 if (ret < 0) {
980 goto error;
981 }
982
983 ua_event->enabled = 1;
984
985 error:
986 return ret;
987 }
988
989 /*
990 * Disable on the tracer side a ust app event for the session and channel.
991 */
992 static int disable_ust_app_event(struct ust_app_session *ua_sess,
993 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event,
994 struct ust_app *app)
995 {
996 int ret;
997
998 ret = disable_ust_event(app, ua_sess, ua_event);
999 if (ret < 0) {
1000 goto error;
1001 }
1002
1003 ua_event->enabled = 0;
1004
1005 error:
1006 return ret;
1007 }
1008
1009 /*
1010 * Lookup ust app channel for session and disable it on the tracer side.
1011 */
1012 static
1013 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1014 struct ust_app_channel *ua_chan, struct ust_app *app)
1015 {
1016 int ret;
1017
1018 ret = disable_ust_channel(app, ua_sess, ua_chan);
1019 if (ret < 0) {
1020 goto error;
1021 }
1022
1023 ua_chan->enabled = 0;
1024
1025 error:
1026 return ret;
1027 }
1028
1029 /*
1030 * Lookup ust app channel for session and enable it on the tracer side.
1031 */
1032 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1033 struct ltt_ust_channel *uchan, struct ust_app *app)
1034 {
1035 int ret = 0;
1036 struct cds_lfht_iter iter;
1037 struct cds_lfht_node *ua_chan_node;
1038 struct ust_app_channel *ua_chan;
1039
1040 ua_chan_node = hashtable_lookup(ua_sess->channels,
1041 (void *)uchan->name, strlen(uchan->name), &iter);
1042 if (ua_chan_node == NULL) {
1043 DBG2("Unable to find channel %s in ust session uid %u",
1044 uchan->name, ua_sess->uid);
1045 goto error;
1046 }
1047
1048 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1049
1050 ret = enable_ust_channel(app, ua_sess, ua_chan);
1051 if (ret < 0) {
1052 goto error;
1053 }
1054
1055 error:
1056 return ret;
1057 }
1058
1059 /*
1060 * Create UST app channel and create it on the tracer.
1061 */
1062 static struct ust_app_channel *create_ust_app_channel(
1063 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1064 struct ust_app *app)
1065 {
1066 int ret = 0;
1067 struct cds_lfht_iter iter;
1068 struct cds_lfht_node *ua_chan_node;
1069 struct ust_app_channel *ua_chan;
1070
1071 /* Lookup channel in the ust app session */
1072 ua_chan_node = hashtable_lookup(ua_sess->channels,
1073 (void *)uchan->name, strlen(uchan->name), &iter);
1074 if (ua_chan_node == NULL) {
1075 DBG2("Unable to find channel %s in ust session uid %u",
1076 uchan->name, ua_sess->uid);
1077 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1078 if (ua_chan == NULL) {
1079 goto error;
1080 }
1081 shadow_copy_channel(ua_chan, uchan);
1082
1083 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
1084 } else {
1085 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1086 }
1087
1088 ret = create_ust_channel(app, ua_sess, ua_chan);
1089 if (ret < 0) {
1090 goto error;
1091 }
1092
1093 return ua_chan;
1094
1095 error:
1096 return NULL;
1097 }
1098
1099 /*
1100 * Create UST app event and create it on the tracer side.
1101 */
1102 static
1103 int create_ust_app_event(struct ust_app_session *ua_sess,
1104 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1105 struct ust_app *app)
1106 {
1107 int ret = 0;
1108 struct cds_lfht_iter iter;
1109 struct cds_lfht_node *ua_event_node;
1110 struct ust_app_event *ua_event;
1111
1112 /* Get event node */
1113 ua_event_node = hashtable_lookup(ua_chan->events,
1114 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
1115 if (ua_event_node != NULL) {
1116 ERR("UST app event %s already exist. Stopping creation.",
1117 uevent->attr.name);
1118 goto end;
1119 }
1120
1121 /* Does not exist so create one */
1122 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1123 if (ua_event == NULL) {
1124 /* Only malloc can failed so something is really wrong */
1125 ret = -ENOMEM;
1126 goto error;
1127 }
1128 shadow_copy_event(ua_event, uevent);
1129
1130 /* Create it on the tracer side */
1131 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1132 if (ret < 0) {
1133 delete_ust_app_event(app->key.sock, ua_event);
1134 goto error;
1135 }
1136
1137 ua_event->enabled = 1;
1138
1139 hashtable_add_unique(ua_chan->events, &ua_event->node);
1140
1141 end:
1142 error:
1143 return ret;
1144 }
1145
1146 /*
1147 * Create UST metadata and open it on the tracer side.
1148 */
1149 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1150 char *pathname, struct ust_app *app)
1151 {
1152 int ret = 0;
1153
1154 if (ua_sess->metadata == NULL) {
1155 /* Allocate UST metadata */
1156 ua_sess->metadata = trace_ust_create_metadata(pathname);
1157 if (ua_sess->metadata == NULL) {
1158 ERR("UST app session %d creating metadata failed",
1159 ua_sess->handle);
1160 goto error;
1161 }
1162
1163 ret = open_ust_metadata(app, ua_sess);
1164 if (ret < 0) {
1165 goto error;
1166 }
1167
1168 DBG2("UST metadata opened for app pid %d", app->key.pid);
1169 }
1170
1171 /* Open UST metadata stream */
1172 if (ua_sess->metadata->stream_obj == NULL) {
1173 ret = create_ust_stream(app, ua_sess);
1174 if (ret < 0) {
1175 goto error;
1176 }
1177
1178 ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
1179 if (ret < 0) {
1180 PERROR("mkdir UST metadata");
1181 goto error;
1182 }
1183
1184 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1185 "%s/metadata", ua_sess->path);
1186 if (ret < 0) {
1187 PERROR("asprintf UST create stream");
1188 goto error;
1189 }
1190
1191 DBG2("UST metadata stream object created for app pid %d",
1192 app->key.pid);
1193 } else {
1194 ERR("Attempting to create stream without metadata opened");
1195 goto error;
1196 }
1197
1198 return 0;
1199
1200 error:
1201 return -1;
1202 }
1203
1204 /*
1205 * Return pointer to traceable apps list.
1206 */
1207 struct cds_lfht *ust_app_get_ht(void)
1208 {
1209 return ust_app_ht;
1210 }
1211
1212 /*
1213 * Return ust app pointer or NULL if not found.
1214 */
1215 struct ust_app *ust_app_find_by_pid(pid_t pid)
1216 {
1217 struct cds_lfht_node *node;
1218 struct cds_lfht_iter iter;
1219
1220 rcu_read_lock();
1221 node = hashtable_lookup(ust_app_ht,
1222 (void *)((unsigned long) pid), sizeof(void *), &iter);
1223 if (node == NULL) {
1224 DBG2("UST app no found with pid %d", pid);
1225 goto error;
1226 }
1227 rcu_read_unlock();
1228
1229 DBG2("Found UST app by pid %d", pid);
1230
1231 return caa_container_of(node, struct ust_app, node);
1232
1233 error:
1234 rcu_read_unlock();
1235 return NULL;
1236 }
1237
1238 /*
1239 * Using pid and uid (of the app), allocate a new ust_app struct and
1240 * add it to the global traceable app list.
1241 *
1242 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1243 * bitness is not supported.
1244 */
1245 int ust_app_register(struct ust_register_msg *msg, int sock)
1246 {
1247 struct ust_app *lta;
1248
1249 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1250 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
1251 ERR("Registration failed: application \"%s\" (pid: %d) has "
1252 "%d-bit long, but no consumerd for this long size is available.\n",
1253 msg->name, msg->pid, msg->bits_per_long);
1254 close(sock);
1255 return -EINVAL;
1256 }
1257 lta = zmalloc(sizeof(struct ust_app));
1258 if (lta == NULL) {
1259 PERROR("malloc");
1260 return -ENOMEM;
1261 }
1262
1263 lta->ppid = msg->ppid;
1264 lta->uid = msg->uid;
1265 lta->gid = msg->gid;
1266 lta->bits_per_long = msg->bits_per_long;
1267 lta->v_major = msg->major;
1268 lta->v_minor = msg->minor;
1269 strncpy(lta->name, msg->name, sizeof(lta->name));
1270 lta->name[16] = '\0';
1271 lta->sessions = hashtable_new(0);
1272
1273 /* Set key map */
1274 lta->key.pid = msg->pid;
1275 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
1276 sizeof(void *));
1277 lta->key.sock = sock;
1278 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
1279 sizeof(void *));
1280
1281 rcu_read_lock();
1282 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
1283 hashtable_add_unique(ust_app_ht, &lta->node);
1284 rcu_read_unlock();
1285
1286 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1287 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
1288 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
1289
1290 return 0;
1291 }
1292
1293 /*
1294 * Unregister app by removing it from the global traceable app list and freeing
1295 * the data struct.
1296 *
1297 * The socket is already closed at this point so no close to sock.
1298 */
1299 void ust_app_unregister(int sock)
1300 {
1301 struct ust_app *lta;
1302 struct cds_lfht_node *node;
1303 struct cds_lfht_iter iter;
1304 int ret;
1305
1306 rcu_read_lock();
1307 lta = find_app_by_sock(sock);
1308 if (lta == NULL) {
1309 ERR("Unregister app sock %d not found!", sock);
1310 goto error;
1311 }
1312
1313 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
1314
1315 /* Get the node reference for a call_rcu */
1316 node = hashtable_lookup(ust_app_ht,
1317 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
1318 if (node == NULL) {
1319 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
1320 goto error;
1321 }
1322
1323 ret = hashtable_del(ust_app_ht, &iter);
1324 assert(!ret);
1325 call_rcu(&node->head, delete_ust_app_rcu);
1326 error:
1327 rcu_read_unlock();
1328 return;
1329 }
1330
1331 /*
1332 * Return traceable_app_count
1333 */
1334 unsigned long ust_app_list_count(void)
1335 {
1336 unsigned long count;
1337
1338 rcu_read_lock();
1339 count = hashtable_get_count(ust_app_ht);
1340 rcu_read_unlock();
1341
1342 return count;
1343 }
1344
1345 /*
1346 * Fill events array with all events name of all registered apps.
1347 */
1348 int ust_app_list_events(struct lttng_event **events)
1349 {
1350 int ret, handle;
1351 size_t nbmem, count = 0;
1352 struct cds_lfht_iter iter;
1353 struct ust_app *app;
1354 struct lttng_event *tmp;
1355
1356 nbmem = UST_APP_EVENT_LIST_SIZE;
1357 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1358 if (tmp == NULL) {
1359 PERROR("zmalloc ust app events");
1360 ret = -ENOMEM;
1361 goto error;
1362 }
1363
1364 rcu_read_lock();
1365
1366 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1367 handle = ustctl_tracepoint_list(app->key.sock);
1368 if (handle < 0) {
1369 ERR("UST app list events getting handle failed for app pid %d",
1370 app->key.pid);
1371 continue;
1372 }
1373
1374 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
1375 tmp[count].name)) != -ENOENT) {
1376 if (count > nbmem) {
1377 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
1378 nbmem + UST_APP_EVENT_LIST_SIZE);
1379 nbmem += UST_APP_EVENT_LIST_SIZE;
1380 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
1381 if (tmp == NULL) {
1382 PERROR("realloc ust app events");
1383 ret = -ENOMEM;
1384 goto rcu_error;
1385 }
1386 }
1387
1388 tmp[count].type = LTTNG_UST_TRACEPOINT;
1389 tmp[count].pid = app->key.pid;
1390 tmp[count].enabled = -1;
1391 count++;
1392 }
1393 }
1394
1395 ret = count;
1396 *events = tmp;
1397
1398 DBG2("UST app list events done (%zu events)", count);
1399
1400 rcu_error:
1401 rcu_read_unlock();
1402 error:
1403 return ret;
1404 }
1405
1406 /*
1407 * Free and clean all traceable apps of the global list.
1408 */
1409 void ust_app_clean_list(void)
1410 {
1411 int ret;
1412 struct cds_lfht_iter iter;
1413 struct ust_app *app;
1414
1415 DBG2("UST app cleaning registered apps hash table");
1416
1417 rcu_read_lock();
1418
1419 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1420 ret = hashtable_del(ust_app_ht, &iter);
1421 assert(!ret);
1422 call_rcu(&iter.node->head, delete_ust_app_rcu);
1423 }
1424
1425 hashtable_destroy(ust_app_ht);
1426 hashtable_destroy(ust_app_sock_key_map);
1427
1428 rcu_read_unlock();
1429 }
1430
1431 /*
1432 * Init UST app hash table.
1433 */
1434 void ust_app_ht_alloc(void)
1435 {
1436 ust_app_ht = hashtable_new(0);
1437 ust_app_sock_key_map = hashtable_new(0);
1438 }
1439
1440 /*
1441 * For a specific UST session, disable the channel for all registered apps.
1442 */
1443 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1444 struct ltt_ust_channel *uchan)
1445 {
1446 int ret = 0;
1447 struct cds_lfht_iter iter;
1448 struct cds_lfht_node *ua_chan_node;
1449 struct ust_app *app;
1450 struct ust_app_session *ua_sess;
1451 struct ust_app_channel *ua_chan;
1452
1453 if (usess == NULL || uchan == NULL) {
1454 ERR("Disabling UST global channel with NULL values");
1455 ret = -1;
1456 goto error;
1457 }
1458
1459 DBG2("UST app disabling channel %s from global domain for session uid %d",
1460 uchan->name, usess->uid);
1461
1462 rcu_read_lock();
1463
1464 /* For every registered applications */
1465 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1466 ua_sess = lookup_session_by_app(usess, app);
1467 if (ua_sess == NULL) {
1468 continue;
1469 }
1470
1471 /* Get channel */
1472 ua_chan_node = hashtable_lookup(ua_sess->channels,
1473 (void *)uchan->name, strlen(uchan->name), &iter);
1474 /* If the session if found for the app, the channel must be there */
1475 assert(ua_chan_node);
1476
1477 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1478 /* The channel must not be already disabled */
1479 assert(ua_chan->enabled == 1);
1480
1481 /* Disable channel onto application */
1482 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1483 if (ret < 0) {
1484 /* XXX: We might want to report this error at some point... */
1485 continue;
1486 }
1487 }
1488
1489 rcu_read_unlock();
1490
1491 error:
1492 return ret;
1493 }
1494
1495 /*
1496 * For a specific UST session, enable the channel for all registered apps.
1497 */
1498 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1499 struct ltt_ust_channel *uchan)
1500 {
1501 int ret = 0;
1502 struct cds_lfht_iter iter;
1503 struct ust_app *app;
1504 struct ust_app_session *ua_sess;
1505
1506 if (usess == NULL || uchan == NULL) {
1507 ERR("Adding UST global channel to NULL values");
1508 ret = -1;
1509 goto error;
1510 }
1511
1512 DBG2("UST app enabling channel %s to global domain for session uid %d",
1513 uchan->name, usess->uid);
1514
1515 rcu_read_lock();
1516
1517 /* For every registered applications */
1518 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1519 ua_sess = lookup_session_by_app(usess, app);
1520 if (ua_sess == NULL) {
1521 continue;
1522 }
1523
1524 /* Enable channel onto application */
1525 ret = enable_ust_app_channel(ua_sess, uchan, app);
1526 if (ret < 0) {
1527 /* XXX: We might want to report this error at some point... */
1528 continue;
1529 }
1530 }
1531
1532 rcu_read_unlock();
1533
1534 error:
1535 return ret;
1536 }
1537
1538 /*
1539 * Disable an event in a channel and for a specific session.
1540 */
1541 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1542 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1543 {
1544 int ret = 0;
1545 struct cds_lfht_iter iter, uiter;
1546 struct cds_lfht_node *ua_chan_node, *ua_event_node;
1547 struct ust_app *app;
1548 struct ust_app_session *ua_sess;
1549 struct ust_app_channel *ua_chan;
1550 struct ust_app_event *ua_event;
1551
1552 DBG("UST app disabling event %s for all apps in channel "
1553 "%s for session uid %d", uevent->attr.name, uchan->name, usess->uid);
1554
1555 rcu_read_lock();
1556
1557 /* For all registered applications */
1558 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1559 ua_sess = lookup_session_by_app(usess, app);
1560 if (ua_sess == NULL) {
1561 /* Next app */
1562 continue;
1563 }
1564
1565 /* Lookup channel in the ust app session */
1566 ua_chan_node = hashtable_lookup(ua_sess->channels,
1567 (void *)uchan->name, strlen(uchan->name), &uiter);
1568 if (ua_chan_node == NULL) {
1569 DBG2("Channel %s not found in session uid %d for app pid %d."
1570 "Skipping", uchan->name, usess->uid, app->key.pid);
1571 continue;
1572 }
1573 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1574
1575 ua_event_node = hashtable_lookup(ua_chan->events,
1576 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
1577 if (ua_event_node == NULL) {
1578 DBG2("Event %s not found in channel %s for app pid %d."
1579 "Skipping", uevent->attr.name, uchan->name, app->key.pid);
1580 continue;
1581 }
1582 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1583
1584 ret = disable_ust_app_event(ua_sess, ua_chan, ua_event, app);
1585 if (ret < 0) {
1586 /* XXX: Report error someday... */
1587 continue;
1588 }
1589 }
1590
1591 rcu_read_unlock();
1592
1593 return ret;
1594 }
1595
1596 /*
1597 * For a specific UST session and UST channel, the event for all
1598 * registered apps.
1599 */
1600 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1601 struct ltt_ust_channel *uchan)
1602 {
1603 int ret = 0;
1604 struct cds_lfht_iter iter, uiter;
1605 struct cds_lfht_node *ua_chan_node;
1606 struct ust_app *app;
1607 struct ust_app_session *ua_sess;
1608 struct ust_app_channel *ua_chan;
1609 struct ust_app_event *ua_event;
1610
1611 DBG("UST app disabling all event for all apps in channel "
1612 "%s for session uid %d", uchan->name, usess->uid);
1613
1614 rcu_read_lock();
1615
1616 /* For all registered applications */
1617 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1618 ua_sess = lookup_session_by_app(usess, app);
1619 /* If ua_sess is NULL, there is a code flow error */
1620 assert(ua_sess);
1621
1622 /* Lookup channel in the ust app session */
1623 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1624 strlen(uchan->name), &uiter);
1625 /* If the channel is not found, there is a code flow error */
1626 assert(ua_chan_node);
1627
1628 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1629
1630 /* Disable each events of channel */
1631 cds_lfht_for_each_entry(ua_chan->events, &uiter, ua_event, node) {
1632 ret = disable_ust_app_event(ua_sess, ua_chan, ua_event, app);
1633 if (ret < 0) {
1634 /* XXX: Report error someday... */
1635 continue;
1636 }
1637 }
1638 }
1639
1640 rcu_read_unlock();
1641
1642 return ret;
1643 }
1644
1645 /*
1646 * For a specific UST session, create the channel for all registered apps.
1647 */
1648 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
1649 struct ltt_ust_channel *uchan)
1650 {
1651 int ret = 0;
1652 struct cds_lfht_iter iter;
1653 struct ust_app *app;
1654 struct ust_app_session *ua_sess;
1655 struct ust_app_channel *ua_chan;
1656
1657 if (usess == NULL || uchan == NULL) {
1658 ERR("Adding UST global channel to NULL values");
1659 ret = -1;
1660 goto error;
1661 }
1662
1663 DBG2("UST app adding channel %s to global domain for session uid %d",
1664 uchan->name, usess->uid);
1665
1666 rcu_read_lock();
1667
1668 /* For every registered applications */
1669 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1670 /*
1671 * Create session on the tracer side and add it to app session HT. Note
1672 * that if session exist, it will simply return a pointer to the ust
1673 * app session.
1674 */
1675 ua_sess = create_ust_app_session(usess, app);
1676 if (ua_sess == NULL) {
1677 continue;
1678 }
1679
1680 /* Create channel onto application */
1681 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1682 if (ua_chan == NULL) {
1683 continue;
1684 }
1685 }
1686
1687 rcu_read_unlock();
1688
1689 error:
1690 return ret;
1691 }
1692
1693 /*
1694 * Enable event for a specific session and channel on the tracer.
1695 */
1696 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
1697 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1698 {
1699 int ret = 0;
1700 struct cds_lfht_iter iter, uiter;
1701 struct cds_lfht_node *ua_chan_node, *ua_event_node;
1702 struct ust_app *app;
1703 struct ust_app_session *ua_sess;
1704 struct ust_app_channel *ua_chan;
1705 struct ust_app_event *ua_event;
1706
1707 DBG("UST app enabling event %s for all apps for session uid %d",
1708 uevent->attr.name, usess->uid);
1709
1710 /*
1711 * NOTE: At this point, this function is called only if the session and
1712 * channel passed are already created for all apps. and enabled on the
1713 * tracer also.
1714 */
1715
1716 rcu_read_lock();
1717
1718 /* For all registered applications */
1719 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1720 ua_sess = lookup_session_by_app(usess, app);
1721 /* If ua_sess is NULL, there is a code flow error */
1722 assert(ua_sess);
1723
1724 /* Lookup channel in the ust app session */
1725 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1726 strlen(uchan->name), &uiter);
1727 /* If the channel is not found, there is a code flow error */
1728 assert(ua_chan_node);
1729
1730 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1731
1732 ua_event_node = hashtable_lookup(ua_sess->channels,
1733 (void*)uevent->attr.name, strlen(uevent->attr.name), &uiter);
1734 if (ua_event_node == NULL) {
1735 DBG3("UST app enable event %s not found. Skipping app",
1736 uevent->attr.name);
1737 continue;
1738 }
1739 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1740
1741 ret = enable_ust_app_event(ua_sess, ua_event, app);
1742 if (ret < 0) {
1743 /* XXX: Report error someday... */
1744 continue;
1745 }
1746 }
1747
1748 rcu_read_unlock();
1749
1750 return ret;
1751 }
1752
1753 /*
1754 * For a specific existing UST session and UST channel, creates the event for
1755 * all registered apps.
1756 */
1757 int ust_app_create_event_glb(struct ltt_ust_session *usess,
1758 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1759 {
1760 int ret = 0;
1761 struct cds_lfht_iter iter, uiter;
1762 struct cds_lfht_node *ua_chan_node;
1763 struct ust_app *app;
1764 struct ust_app_session *ua_sess;
1765 struct ust_app_channel *ua_chan;
1766
1767 DBG("UST app creating event %s for all apps for session uid %d",
1768 uevent->attr.name, usess->uid);
1769
1770 /*
1771 * NOTE: At this point, this function is called only if the session and
1772 * channel passed are already created for all apps. and enabled on the
1773 * tracer also.
1774 */
1775
1776 rcu_read_lock();
1777
1778 /* For all registered applications */
1779 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1780 ua_sess = lookup_session_by_app(usess, app);
1781 /* If ua_sess is NULL, there is a code flow error */
1782 assert(ua_sess);
1783
1784 /* Lookup channel in the ust app session */
1785 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1786 strlen(uchan->name), &uiter);
1787 /* If the channel is not found, there is a code flow error */
1788 assert(ua_chan_node);
1789
1790 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1791
1792 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1793 if (ret < 0) {
1794 continue;
1795 }
1796 }
1797
1798 rcu_read_unlock();
1799
1800 return ret;
1801 }
1802
1803 /*
1804 * Start tracing for a specific UST session and app.
1805 */
1806 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
1807 {
1808 int ret = 0;
1809 struct cds_lfht_iter iter;
1810 struct ust_app_session *ua_sess;
1811 struct ust_app_channel *ua_chan;
1812 struct ltt_ust_stream *ustream;
1813 int consumerd_fd;
1814
1815 DBG("Starting tracing for ust app pid %d", app->key.pid);
1816
1817 rcu_read_lock();
1818
1819 ua_sess = lookup_session_by_app(usess, app);
1820 if (ua_sess == NULL) {
1821 goto error_rcu_unlock;
1822 }
1823
1824 /* Upon restart, we skip the setup, already done */
1825 if (ua_sess->started) {
1826 goto skip_setup;
1827 }
1828
1829 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1830 if (ret < 0) {
1831 goto error_rcu_unlock;
1832 }
1833
1834 /* For each channel */
1835 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1836 /* Create all streams */
1837 while (1) {
1838 /* Create UST stream */
1839 ustream = zmalloc(sizeof(*ustream));
1840 if (ustream == NULL) {
1841 PERROR("zmalloc ust stream");
1842 goto error_rcu_unlock;
1843 }
1844
1845 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1846 &ustream->obj);
1847 if (ret < 0) {
1848 /* Got all streams */
1849 break;
1850 }
1851 ustream->handle = ustream->obj->handle;
1852
1853 /* Order is important */
1854 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
1855 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1856 ua_sess->path, ua_chan->name,
1857 ua_chan->streams.count++);
1858 if (ret < 0) {
1859 PERROR("asprintf UST create stream");
1860 continue;
1861 }
1862 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1863 ustream->pathname);
1864 }
1865 }
1866
1867 switch (app->bits_per_long) {
1868 case 64:
1869 consumerd_fd = ust_consumerd64_fd;
1870 break;
1871 case 32:
1872 consumerd_fd = ust_consumerd32_fd;
1873 break;
1874 default:
1875 ret = -EINVAL;
1876 goto error_rcu_unlock;
1877 }
1878
1879 /* Setup UST consumer socket and send fds to it */
1880 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
1881 if (ret < 0) {
1882 goto error_rcu_unlock;
1883 }
1884 ua_sess->started = 1;
1885
1886 skip_setup:
1887 /* This start the UST tracing */
1888 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1889 if (ret < 0) {
1890 ERR("Error starting tracing for app pid: %d", app->key.pid);
1891 goto error_rcu_unlock;
1892 }
1893
1894 rcu_read_unlock();
1895
1896 /* Quiescent wait after starting trace */
1897 ustctl_wait_quiescent(app->key.sock);
1898
1899 return 0;
1900
1901 error_rcu_unlock:
1902 rcu_read_unlock();
1903 return -1;
1904 }
1905
1906 /*
1907 * Stop tracing for a specific UST session and app.
1908 */
1909 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1910 {
1911 int ret = 0;
1912 struct ust_app_session *ua_sess;
1913
1914 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1915
1916 rcu_read_lock();
1917
1918 ua_sess = lookup_session_by_app(usess, app);
1919 if (ua_sess == NULL) {
1920 /* Only malloc can failed so something is really wrong */
1921 goto error_rcu_unlock;
1922 }
1923
1924 #if 0 /* only useful when periodical flush will be supported */
1925 /* need to keep a handle on shm in session for this. */
1926 /* Flush all buffers before stopping */
1927 ret = ustctl_flush_buffer(usess->sock, usess->metadata->obj);
1928 if (ret < 0) {
1929 ERR("UST metadata flush failed");
1930 }
1931
1932 cds_list_for_each_entry(ustchan, &usess->channels.head, list) {
1933 ret = ustctl_flush_buffer(usess->sock, ustchan->obj);
1934 if (ret < 0) {
1935 ERR("UST flush buffer error");
1936 }
1937 }
1938 #endif
1939
1940 /* This inhibits UST tracing */
1941 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1942 if (ret < 0) {
1943 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1944 goto error_rcu_unlock;
1945 }
1946
1947 rcu_read_unlock();
1948
1949 /* Quiescent wait after stopping trace */
1950 ustctl_wait_quiescent(app->key.sock);
1951
1952 return 0;
1953
1954 error_rcu_unlock:
1955 rcu_read_unlock();
1956 return -1;
1957 }
1958
1959 /*
1960 * Destroy a specific UST session in apps.
1961 */
1962 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1963 {
1964 struct ust_app_session *ua_sess;
1965 struct lttng_ust_object_data obj;
1966 struct cds_lfht_iter iter;
1967 struct cds_lfht_node *node;
1968 int ret;
1969
1970 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1971
1972 rcu_read_lock();
1973
1974 __lookup_session_by_app(usess, app, &iter);
1975 node = hashtable_iter_get_node(&iter);
1976 if (node == NULL) {
1977 /* Only malloc can failed so something is really wrong */
1978 goto error_rcu_unlock;
1979 }
1980 ua_sess = caa_container_of(node, struct ust_app_session, node);
1981 ret = hashtable_del(app->sessions, &iter);
1982 assert(!ret);
1983 delete_ust_app_session(app->key.sock, ua_sess);
1984 obj.handle = ua_sess->handle;
1985 obj.shm_fd = -1;
1986 obj.wait_fd = -1;
1987 obj.memory_map_size = 0;
1988 ustctl_release_object(app->key.sock, &obj);
1989
1990 rcu_read_unlock();
1991
1992 /* Quiescent wait after stopping trace */
1993 ustctl_wait_quiescent(app->key.sock);
1994
1995 return 0;
1996
1997 error_rcu_unlock:
1998 rcu_read_unlock();
1999 return -1;
2000 }
2001
2002 /*
2003 * Start tracing for the UST session.
2004 */
2005 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2006 {
2007 int ret = 0;
2008 struct cds_lfht_iter iter;
2009 struct ust_app *app;
2010
2011 DBG("Starting all UST traces");
2012
2013 rcu_read_lock();
2014
2015 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2016 ret = ust_app_start_trace(usess, app);
2017 if (ret < 0) {
2018 /* Continue to next apps even on error */
2019 continue;
2020 }
2021 }
2022
2023 rcu_read_unlock();
2024
2025 return 0;
2026 }
2027
2028 /*
2029 * Start tracing for the UST session.
2030 */
2031 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2032 {
2033 int ret = 0;
2034 struct cds_lfht_iter iter;
2035 struct ust_app *app;
2036
2037 DBG("Stopping all UST traces");
2038
2039 rcu_read_lock();
2040
2041 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2042 ret = ust_app_stop_trace(usess, app);
2043 if (ret < 0) {
2044 /* Continue to next apps even on error */
2045 continue;
2046 }
2047 }
2048
2049 rcu_read_unlock();
2050
2051 return 0;
2052 }
2053
2054 /*
2055 * Destroy app UST session.
2056 */
2057 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2058 {
2059 int ret = 0;
2060 struct cds_lfht_iter iter;
2061 struct ust_app *app;
2062
2063 DBG("Destroy all UST traces");
2064
2065 rcu_read_lock();
2066
2067 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2068 ret = ust_app_destroy_trace(usess, app);
2069 if (ret < 0) {
2070 /* Continue to next apps even on error */
2071 continue;
2072 }
2073 }
2074
2075 rcu_read_unlock();
2076
2077 return 0;
2078 }
2079
2080 /*
2081 * Add channels/events from UST global domain to registered apps at sock.
2082 */
2083 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2084 {
2085 int ret = 0;
2086 struct cds_lfht_iter iter;
2087 struct ust_app *app;
2088 struct ust_app_session *ua_sess;
2089 struct ust_app_channel *ua_chan;
2090 struct ust_app_event *ua_event;
2091
2092 if (usess == NULL) {
2093 ERR("No UST session on global update. Returning");
2094 goto error;
2095 }
2096
2097 DBG2("UST app global update for app sock %d for session uid %d", sock,
2098 usess->uid);
2099
2100 rcu_read_lock();
2101
2102 app = find_app_by_sock(sock);
2103 if (app == NULL) {
2104 ERR("Failed to update app sock %d", sock);
2105 goto error;
2106 }
2107
2108 ua_sess = create_ust_app_session(usess, app);
2109 if (ua_sess == NULL) {
2110 goto error;
2111 }
2112
2113 /*
2114 * We can iterate safely here over all UST app session sicne the create ust
2115 * app session above made a shadow copy of the UST global domain from the
2116 * ltt ust session.
2117 */
2118 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
2119 ret = create_ust_channel(app, ua_sess, ua_chan);
2120 if (ret < 0) {
2121 /* FIXME: Should we quit here or continue... */
2122 continue;
2123 }
2124
2125 /* For each events */
2126 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
2127 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2128 if (ret < 0) {
2129 /* FIXME: Should we quit here or continue... */
2130 continue;
2131 }
2132 }
2133 }
2134
2135 if (usess->start_trace) {
2136 ret = ust_app_start_trace(usess, app);
2137 if (ret < 0) {
2138 goto error;
2139 }
2140
2141 DBG2("UST trace started for app pid %d", app->key.pid);
2142 }
2143
2144 error:
2145 rcu_read_unlock();
2146 return;
2147 }
2148
2149 /*
2150 * Add context to a specific channel for global UST domain.
2151 */
2152 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2153 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2154 {
2155 int ret = 0;
2156 struct cds_lfht_node *ua_chan_node;
2157 struct cds_lfht_iter iter, uiter;
2158 struct ust_app_channel *ua_chan = NULL;
2159 struct ust_app_session *ua_sess;
2160 struct ust_app *app;
2161
2162 rcu_read_lock();
2163
2164 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2165 ua_sess = lookup_session_by_app(usess, app);
2166 if (ua_sess == NULL) {
2167 continue;
2168 }
2169
2170 /* Lookup channel in the ust app session */
2171 ua_chan_node = hashtable_lookup(ua_sess->channels,
2172 (void *)uchan->name, strlen(uchan->name), &uiter);
2173 if (ua_chan_node == NULL) {
2174 continue;
2175 }
2176 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2177 node);
2178
2179 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2180 if (ret < 0) {
2181 continue;
2182 }
2183 }
2184
2185 /* Add ltt UST context node to ltt UST channel */
2186 hashtable_add_unique(uchan->ctx, &uctx->node);
2187
2188 rcu_read_unlock();
2189 return ret;
2190 }
2191
2192 /*
2193 * Add context to a specific event in a channel for global UST domain.
2194 */
2195 int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2196 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2197 struct ltt_ust_context *uctx)
2198 {
2199 int ret = 0;
2200 struct cds_lfht_node *ua_chan_node, *ua_event_node;
2201 struct cds_lfht_iter iter, uiter;
2202 struct ust_app_session *ua_sess;
2203 struct ust_app_event *ua_event;
2204 struct ust_app_channel *ua_chan = NULL;
2205 struct ust_app *app;
2206
2207 rcu_read_lock();
2208
2209 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2210 ua_sess = lookup_session_by_app(usess, app);
2211 if (ua_sess == NULL) {
2212 continue;
2213 }
2214
2215 /* Lookup channel in the ust app session */
2216 ua_chan_node = hashtable_lookup(ua_sess->channels,
2217 (void *)uchan->name, strlen(uchan->name), &uiter);
2218 if (ua_chan_node == NULL) {
2219 continue;
2220 }
2221 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2222 node);
2223
2224 ua_event_node = hashtable_lookup(ua_chan->events,
2225 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
2226 if (ua_event_node == NULL) {
2227 continue;
2228 }
2229 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2230 node);
2231
2232 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2233 if (ret < 0) {
2234 continue;
2235 }
2236 }
2237
2238 /* Add ltt UST context node to ltt UST event */
2239 hashtable_add_unique(uevent->ctx, &uctx->node);
2240
2241 rcu_read_unlock();
2242 return ret;
2243 }
This page took 0.110862 seconds and 5 git commands to generate.