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