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