Multiple fixes when cleaning app and UST sessions
[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 <lttngerr.h>
30 #include <lttng-share.h>
31
32 #include "hashtable.h"
33 #include "ust-app.h"
34 #include "ust-consumer.h"
35 #include "ust-ctl.h"
36
37 /*
38 * Delete ust app event safely. RCU read lock must be held before calling
39 * this function.
40 */
41 static void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
42 {
43 /* TODO : remove context */
44 //struct ust_app_ctx *ltctx;
45 //cds_lfht_for_each_entry(lte->ctx, &iter, ltctx, node) {
46 // delete_ust_app_ctx(sock, ltctx);
47 //}
48
49 ustctl_release_object(sock, ua_event->obj);
50 free(ua_event);
51 }
52
53 /*
54 * Delete ust app stream safely. RCU read lock must be held before calling
55 * this function.
56 */
57 static void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
58 {
59 //TODO
60 //stream is used for passing to consumer.
61 //send_channel_streams is responsible for freeing the streams.
62 //note that this will not play well with flight recorder mode:
63 //we might need a criterion to discard the streams.
64 }
65
66 /*
67 * Delete ust app channel safely. RCU read lock must be held before calling
68 * this function.
69 */
70 static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
71 {
72 int ret;
73 struct cds_lfht_iter iter;
74 struct ust_app_event *ua_event;
75 struct ltt_ust_stream *stream, *stmp;
76
77 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
78 delete_ust_app_stream(sock, stream);
79 }
80
81 /* TODO : remove channel context */
82 //cds_lfht_for_each_entry(ltc->ctx, &iter, ltctx, node) {
83 // hashtable_del(ltc->ctx, &iter);
84 // delete_ust_app_ctx(sock, ltctx);
85 //}
86 //ret = hashtable_destroy(ltc->ctx);
87
88 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
89 hashtable_del(ua_chan->events, &iter);
90 delete_ust_app_event(sock, ua_event);
91 }
92
93 ret = hashtable_destroy(ua_chan->events);
94 if (ret < 0) {
95 ERR("UST app destroy session hashtable failed");
96 goto error;
97 }
98
99 error:
100 return;
101 }
102
103 /*
104 * Delete ust app session safely. RCU read lock must be held before calling
105 * this function.
106 */
107 static void delete_ust_app_session(int sock,
108 struct ust_app_session *ua_sess)
109 {
110 int ret;
111 struct cds_lfht_iter iter;
112 struct ust_app_channel *ua_chan;
113
114 if (ua_sess->metadata) {
115 /*
116 * We do NOT release the stream object and metadata object since they
117 * are release when fds are sent to the consumer.
118 */
119 }
120
121 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
122 hashtable_del(ua_sess->channels, &iter);
123 delete_ust_app_channel(sock, ua_chan);
124 }
125
126 ret = hashtable_destroy(ua_sess->channels);
127 if (ret < 0) {
128 ERR("UST app destroy session hashtable failed");
129 goto error;
130 }
131
132 error:
133 return;
134 }
135
136 /*
137 * Delete a traceable application structure from the global list. Never call
138 * this function outside of a call_rcu call.
139 */
140 static void delete_ust_app(struct ust_app *app)
141 {
142 int ret;
143 struct cds_lfht_node *node;
144 struct cds_lfht_iter iter;
145 struct ust_app_session *ua_sess;
146
147 rcu_read_lock();
148
149 /* Remove from key hash table */
150 node = hashtable_lookup(ust_app_sock_key_map,
151 (void *) ((unsigned long) app->key.sock), sizeof(void *), &iter);
152 if (node == NULL) {
153 /* Not suppose to happen */
154 ERR("UST app key %d not found in key hash table", app->key.sock);
155 goto end;
156 }
157
158 ret = hashtable_del(ust_app_sock_key_map, &iter);
159 if (ret) {
160 ERR("UST app unable to delete app sock %d from key hash table",
161 app->key.sock);
162 } else {
163 DBG2("UST app pair sock %d key %d deleted",
164 app->key.sock, app->key.pid);
165 }
166
167 /* Socket is already closed at this point */
168
169 /* Delete ust app sessions info */
170 if (app->sock_closed) {
171 app->key.sock = -1;
172 }
173
174 cds_lfht_for_each_entry(app->sessions, &iter, ua_sess, node) {
175 hashtable_del(app->sessions, &iter);
176 delete_ust_app_session(app->key.sock, ua_sess);
177 }
178
179 ret = hashtable_destroy(app->sessions);
180 if (ret < 0) {
181 ERR("UST app destroy session hashtable failed");
182 goto end;
183 }
184
185 if (!app->sock_closed) {
186 close(app->key.sock);
187 }
188
189 DBG2("UST app pid %d deleted", app->key.pid);
190 free(app);
191 end:
192 rcu_read_unlock();
193 }
194
195 /*
196 * URCU intermediate call to delete an UST app.
197 */
198 static void delete_ust_app_rcu(struct rcu_head *head)
199 {
200 struct cds_lfht_node *node =
201 caa_container_of(head, struct cds_lfht_node, head);
202 struct ust_app *app =
203 caa_container_of(node, struct ust_app, node);
204
205 delete_ust_app(app);
206 }
207
208 /*
209 * Find an ust_app using the sock and return it. RCU read side lock must be
210 * held before calling this helper function.
211 */
212 static struct ust_app *find_app_by_sock(int sock)
213 {
214 struct cds_lfht_node *node;
215 struct ust_app_key *key;
216 struct cds_lfht_iter iter;
217
218 node = hashtable_lookup(ust_app_sock_key_map,
219 (void *)((unsigned long) sock), sizeof(void *), &iter);
220 if (node == NULL) {
221 DBG2("UST app find by sock %d key not found", sock);
222 goto error;
223 }
224
225 key = caa_container_of(node, struct ust_app_key, node);
226
227 node = hashtable_lookup(ust_app_ht,
228 (void *)((unsigned long) key->pid), sizeof(void *), &iter);
229 if (node == NULL) {
230 DBG2("UST app find by sock %d not found", sock);
231 goto error;
232 }
233 return caa_container_of(node, struct ust_app, node);
234
235 error:
236 return NULL;
237 }
238
239 /*
240 * Return pointer to traceable apps list.
241 */
242 struct cds_lfht *ust_app_get_ht(void)
243 {
244 return ust_app_ht;
245 }
246
247 /*
248 * Return ust app pointer or NULL if not found.
249 */
250 struct ust_app *ust_app_find_by_pid(pid_t pid)
251 {
252 struct cds_lfht_node *node;
253 struct cds_lfht_iter iter;
254
255 rcu_read_lock();
256 node = hashtable_lookup(ust_app_ht,
257 (void *)((unsigned long) pid), sizeof(void *), &iter);
258 if (node == NULL) {
259 DBG2("UST app no found with pid %d", pid);
260 goto error;
261 }
262 rcu_read_unlock();
263
264 DBG2("Found UST app by pid %d", pid);
265
266 return caa_container_of(node, struct ust_app, node);
267
268 error:
269 rcu_read_unlock();
270 return NULL;
271 }
272
273 /*
274 * Using pid and uid (of the app), allocate a new ust_app struct and
275 * add it to the global traceable app list.
276 *
277 * On success, return 0, else return malloc ENOMEM.
278 */
279 int ust_app_register(struct ust_register_msg *msg, int sock)
280 {
281 struct ust_app *lta;
282
283 lta = zmalloc(sizeof(struct ust_app));
284 if (lta == NULL) {
285 PERROR("malloc");
286 return -ENOMEM;
287 }
288
289 lta->ppid = msg->ppid;
290 lta->uid = msg->uid;
291 lta->gid = msg->gid;
292 lta->v_major = msg->major;
293 lta->v_minor = msg->minor;
294 strncpy(lta->name, msg->name, sizeof(lta->name));
295 lta->name[16] = '\0';
296 lta->sessions = hashtable_new(0);
297
298 /* Set key map */
299 lta->key.pid = msg->pid;
300 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
301 sizeof(void *));
302 lta->key.sock = sock;
303 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
304 sizeof(void *));
305
306 rcu_read_lock();
307 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
308 hashtable_add_unique(ust_app_ht, &lta->node);
309 rcu_read_unlock();
310
311 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
312 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
313 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
314
315 return 0;
316 }
317
318 /*
319 * Unregister app by removing it from the global traceable app list and freeing
320 * the data struct.
321 *
322 * The socket is already closed at this point so no close to sock.
323 */
324 void ust_app_unregister(int sock)
325 {
326 struct ust_app *lta;
327 struct cds_lfht_node *node;
328 struct cds_lfht_iter iter;
329
330 rcu_read_lock();
331 lta = find_app_by_sock(sock);
332 if (lta == NULL) {
333 ERR("Unregister app sock %d not found!", sock);
334 goto error;
335 }
336
337 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
338
339 /* Get the node reference for a call_rcu */
340 node = hashtable_lookup(ust_app_ht,
341 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
342 if (node == NULL) {
343 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
344 goto error;
345 }
346
347 /* We got called because the socket was closed on the remote end. */
348 close(sock);
349 /* Using a flag because we still need "sock" as a key. */
350 lta->sock_closed = 1;
351 hashtable_del(ust_app_ht, &iter);
352 call_rcu(&node->head, delete_ust_app_rcu);
353 error:
354 rcu_read_unlock();
355 return;
356 }
357
358 /*
359 * Return traceable_app_count
360 */
361 unsigned long ust_app_list_count(void)
362 {
363 unsigned long count;
364
365 rcu_read_lock();
366 count = hashtable_get_count(ust_app_ht);
367 rcu_read_unlock();
368
369 return count;
370 }
371
372 /*
373 * Fill events array with all events name of all registered apps.
374 */
375 int ust_app_list_events(struct lttng_event **events)
376 {
377 int ret, handle;
378 size_t nbmem, count = 0;
379 struct cds_lfht_iter iter;
380 struct ust_app *app;
381 struct lttng_event *tmp;
382
383 nbmem = UST_APP_EVENT_LIST_SIZE;
384 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
385 if (tmp == NULL) {
386 PERROR("zmalloc ust app events");
387 ret = -ENOMEM;
388 goto error;
389 }
390
391 rcu_read_lock();
392
393 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
394 handle = ustctl_tracepoint_list(app->key.sock);
395 if (handle < 0) {
396 ERR("UST app list events getting handle failed for app pid %d",
397 app->key.pid);
398 continue;
399 }
400
401 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
402 tmp[count].name)) != -ENOENT) {
403 if (count > nbmem) {
404 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
405 nbmem + UST_APP_EVENT_LIST_SIZE);
406 nbmem += UST_APP_EVENT_LIST_SIZE;
407 tmp = realloc(tmp, nbmem);
408 if (tmp == NULL) {
409 PERROR("realloc ust app events");
410 ret = -ENOMEM;
411 goto rcu_error;
412 }
413 }
414
415 tmp[count].type = LTTNG_UST_TRACEPOINT;
416 tmp[count].pid = app->key.pid;
417 count++;
418 }
419 }
420
421 ret = count;
422 *events = tmp;
423
424 DBG2("UST app list events done (%zu events)", count);
425
426 rcu_error:
427 rcu_read_unlock();
428 error:
429 return ret;
430 }
431
432 /*
433 * Free and clean all traceable apps of the global list.
434 */
435 void ust_app_clean_list(void)
436 {
437 int ret;
438 struct cds_lfht_node *node;
439 struct cds_lfht_iter iter;
440 struct ust_app *app;
441
442 DBG2("UST app cleaning registered apps hash table");
443
444 rcu_read_lock();
445
446 cds_lfht_for_each(ust_app_ht, &iter, node) {
447 app = caa_container_of(node, struct ust_app, node);
448 close(app->key.sock);
449 app->sock_closed = 1;
450
451 ret = hashtable_del(ust_app_ht, &iter);
452 if (!ret) {
453 call_rcu(&node->head, delete_ust_app_rcu);
454 }
455 }
456
457 hashtable_destroy(ust_app_ht);
458 hashtable_destroy(ust_app_sock_key_map);
459
460 rcu_read_unlock();
461 }
462
463 /*
464 * Init UST app hash table.
465 */
466 void ust_app_ht_alloc(void)
467 {
468 ust_app_ht = hashtable_new(0);
469 ust_app_sock_key_map = hashtable_new(0);
470 }
471
472 /*
473 * Alloc new UST app session.
474 */
475 static struct ust_app_session *alloc_ust_app_session(void)
476 {
477 struct ust_app_session *ua_sess;
478
479 /* Init most of the default value by allocating and zeroing */
480 ua_sess = zmalloc(sizeof(struct ust_app_session));
481 if (ua_sess == NULL) {
482 PERROR("malloc");
483 goto error;
484 }
485
486 ua_sess->handle = -1;
487 ua_sess->channels = hashtable_new_str(0);
488
489 return ua_sess;
490
491 error:
492 return NULL;
493 }
494
495 /*
496 * Alloc new UST app channel.
497 */
498 static struct ust_app_channel *alloc_ust_app_channel(char *name,
499 struct lttng_ust_channel *attr)
500 {
501 struct ust_app_channel *ua_chan;
502
503 /* Init most of the default value by allocating and zeroing */
504 ua_chan = zmalloc(sizeof(struct ust_app_channel));
505 if (ua_chan == NULL) {
506 PERROR("malloc");
507 goto error;
508 }
509
510 /* Setup channel name */
511 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
512 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
513
514 ua_chan->handle = -1;
515 ua_chan->ctx = hashtable_new(0);
516 ua_chan->events = hashtable_new_str(0);
517 hashtable_node_init(&ua_chan->node, (void *) ua_chan->name,
518 strlen(ua_chan->name));
519
520 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
521
522 /* Copy attributes */
523 if (attr) {
524 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
525 }
526
527 DBG3("UST app channel %s allocated", ua_chan->name);
528
529 return ua_chan;
530
531 error:
532 return NULL;
533 }
534
535 /*
536 * Alloc new UST app event.
537 */
538 static struct ust_app_event *alloc_ust_app_event(char *name,
539 struct lttng_ust_event *attr)
540 {
541 struct ust_app_event *ua_event;
542
543 /* Init most of the default value by allocating and zeroing */
544 ua_event = zmalloc(sizeof(struct ust_app_event));
545 if (ua_event == NULL) {
546 PERROR("malloc");
547 goto error;
548 }
549
550 strncpy(ua_event->name, name, sizeof(ua_event->name));
551 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
552 ua_event->ctx = hashtable_new(0);
553 hashtable_node_init(&ua_event->node, (void *) ua_event->name,
554 strlen(ua_event->name));
555
556 /* Copy attributes */
557 if (attr) {
558 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
559 }
560
561 DBG3("UST app event %s allocated", ua_event->name);
562
563 return ua_event;
564
565 error:
566 return NULL;
567 }
568
569 static void shadow_copy_event(struct ust_app_event *ua_event,
570 struct ltt_ust_event *uevent)
571 {
572 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
573 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
574
575 /* TODO: support copy context */
576 }
577
578 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
579 struct ltt_ust_channel *uchan)
580 {
581 struct cds_lfht_iter iter;
582 struct cds_lfht_node *node, *ua_event_node;
583 struct ltt_ust_event *uevent;
584 struct ust_app_event *ua_event;
585
586 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
587
588 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
589 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
590
591 /* TODO: support copy context */
592
593 /* Copy all events from ltt ust channel to ust app channel */
594 hashtable_get_first(uchan->events, &iter);
595 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
596 uevent = caa_container_of(node, struct ltt_ust_event, node);
597
598 ua_event_node = hashtable_lookup(ua_chan->events,
599 (void *) uevent->attr.name, strlen(uevent->attr.name), &iter);
600 if (ua_event_node == NULL) {
601 DBG2("UST event %s not found on shadow copy channel",
602 uevent->attr.name);
603 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
604 if (ua_event == NULL) {
605 goto next;
606 }
607 shadow_copy_event(ua_event, uevent);
608 hashtable_add_unique(ua_chan->events, &ua_event->node);
609 }
610
611 next:
612 /* Get next UST events */
613 hashtable_get_next(uchan->events, &iter);
614 }
615
616 DBG3("Shadow copy channel done");
617 }
618
619 static void shadow_copy_session(struct ust_app_session *ua_sess,
620 struct ltt_ust_session *usess)
621 {
622 struct cds_lfht_node *node, *ua_chan_node;
623 struct cds_lfht_iter iter;
624 struct ltt_ust_channel *uchan;
625 struct ust_app_channel *ua_chan;
626
627 DBG2("Shadow copy of session handle %d", ua_sess->handle);
628
629 ua_sess->uid = usess->uid;
630
631 /* TODO: support all UST domain */
632
633 /* Iterate over all channels in global domain. */
634 hashtable_get_first(usess->domain_global.channels, &iter);
635 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
636 uchan = caa_container_of(node, struct ltt_ust_channel, node);
637
638 ua_chan_node = hashtable_lookup(ua_sess->channels,
639 (void *)uchan->name, strlen(uchan->name), &iter);
640 if (ua_chan_node == NULL) {
641 DBG2("Channel %s not found on shadow session copy, creating it",
642 uchan->name);
643 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
644 if (ua_chan == NULL) {
645 /* malloc failed... continuing */
646 goto next;
647 }
648
649 shadow_copy_channel(ua_chan, uchan);
650 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
651 }
652
653 next:
654 /* Next item in hash table */
655 hashtable_get_next(usess->domain_global.channels, &iter);
656 }
657 }
658
659 /*
660 * Return ust app session from the app session hashtable using the UST session
661 * uid.
662 */
663 static struct ust_app_session *lookup_session_by_app(
664 struct ltt_ust_session *usess, struct ust_app *app)
665 {
666 struct cds_lfht_iter iter;
667 struct cds_lfht_node *node;
668
669 /* Get right UST app session from app */
670 node = hashtable_lookup(app->sessions,
671 (void *) ((unsigned long) usess->uid), sizeof(void *), &iter);
672 if (node == NULL) {
673 goto error;
674 }
675
676 return caa_container_of(node, struct ust_app_session, node);
677
678 error:
679 return NULL;
680 }
681
682 /*
683 * Create a UST session onto the tracer of app and add it the session
684 * hashtable.
685 *
686 * Return ust app session or NULL on error.
687 */
688 static struct ust_app_session *create_ust_app_session(
689 struct ltt_ust_session *usess, struct ust_app *app)
690 {
691 int ret;
692 struct ust_app_session *ua_sess;
693
694 ua_sess = lookup_session_by_app(usess, app);
695 if (ua_sess == NULL) {
696 DBG2("UST app pid: %d session uid %d not found, creating it",
697 app->key.pid, usess->uid);
698 ua_sess = alloc_ust_app_session();
699 if (ua_sess == NULL) {
700 /* Only malloc can failed so something is really wrong */
701 goto error;
702 }
703 shadow_copy_session(ua_sess, usess);
704 }
705
706 if (ua_sess->handle == -1) {
707 ret = ustctl_create_session(app->key.sock);
708 if (ret < 0) {
709 ERR("Error creating session for app pid %d, sock %d",
710 app->key.pid, app->key.sock);
711 /* TODO: free() ua_sess */
712 goto error;
713 }
714
715 DBG2("UST app ustctl create session handle %d", ret);
716 ua_sess->handle = ret;
717
718 /* Add ust app session to app's HT */
719 hashtable_node_init(&ua_sess->node,
720 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
721 hashtable_add_unique(app->sessions, &ua_sess->node);
722
723 DBG2("UST app session created successfully with handle %d", ret);
724 }
725
726 return ua_sess;
727
728 error:
729 return NULL;
730 }
731
732 /*
733 * Create the specified channel onto the UST tracer for a UST session.
734 */
735 static int create_ust_channel(struct ust_app *app,
736 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
737 {
738 int ret;
739
740 /* TODO: remove cast and use lttng-ust-abi.h */
741 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
742 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
743 if (ret < 0) {
744 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
745 "and session handle %d with ret %d",
746 ua_chan->name, app->key.pid, app->key.sock,
747 ua_sess->handle, ret);
748 goto error;
749 }
750
751 ua_chan->handle = ua_chan->obj->handle;
752 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
753 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
754 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
755
756 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
757 ua_chan->name, app->key.pid, app->key.sock);
758
759 error:
760 return ret;
761 }
762
763 /*
764 * Create the specified event onto the UST tracer for a UST session.
765 */
766 static int create_ust_event(struct ust_app *app,
767 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
768 struct ust_app_event *ua_event)
769 {
770 int ret = 0;
771
772 /* Create UST event on tracer */
773 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
774 &ua_event->obj);
775 if (ret < 0) {
776 ERR("Error ustctl create event %s for app pid: %d with ret %d",
777 ua_event->attr.name, app->key.pid, ret);
778 goto error;
779 }
780
781 ua_event->handle = ua_event->obj->handle;
782 ua_event->enabled = 1;
783
784 DBG2("UST app event %s created successfully for pid:%d",
785 ua_event->attr.name, app->key.pid);
786
787 error:
788 return ret;
789 }
790
791 static struct ust_app_channel *create_ust_app_channel(
792 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
793 struct ust_app *app)
794 {
795 int ret = 0;
796 struct cds_lfht_iter iter;
797 struct cds_lfht_node *ua_chan_node;
798 struct ust_app_channel *ua_chan;
799
800 /* Lookup channel in the ust app session */
801 ua_chan_node = hashtable_lookup(ua_sess->channels,
802 (void *)uchan->name, strlen(uchan->name), &iter);
803 if (ua_chan_node == NULL) {
804 DBG2("Unable to find channel %s in ust session uid %u",
805 uchan->name, ua_sess->uid);
806 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
807 if (ua_chan == NULL) {
808 goto error;
809 }
810 shadow_copy_channel(ua_chan, uchan);
811
812 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
813 } else {
814 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
815 }
816
817 ret = create_ust_channel(app, ua_sess, ua_chan);
818 if (ret < 0) {
819 goto error;
820 }
821
822 return ua_chan;
823
824 error:
825 return NULL;
826 }
827
828 static struct ust_app_event *create_ust_app_event(
829 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
830 struct ltt_ust_event *uevent, struct ust_app *app)
831 {
832 int ret;
833 struct cds_lfht_iter iter;
834 struct cds_lfht_node *ua_event_node;
835 struct ust_app_event *ua_event;
836
837 /* Get event node */
838 ua_event_node = hashtable_lookup(ua_chan->events,
839 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
840 if (ua_event_node == NULL) {
841 DBG2("UST app event %s not found, creating it", uevent->attr.name);
842 /* Does not exist so create one */
843 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
844 if (ua_event == NULL) {
845 /* Only malloc can failed so something is really wrong */
846 goto error;
847 }
848 shadow_copy_event(ua_event, uevent);
849
850 hashtable_add_unique(ua_chan->events, &ua_event->node);
851 } else {
852 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
853 }
854
855 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
856 if (ret < 0) {
857 goto error;
858 }
859
860 return ua_event;
861
862 error:
863 return NULL;
864 }
865
866 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
867 char *pathname, struct ust_app *app)
868 {
869 int ret = 0;
870 struct lttng_ust_channel_attr uattr;
871
872 if (ua_sess->metadata == NULL) {
873 /* Allocate UST metadata */
874 ua_sess->metadata = trace_ust_create_metadata(pathname);
875 if (ua_sess->metadata == NULL) {
876 ERR("UST app session %d creating metadata failed",
877 ua_sess->handle);
878 goto error;
879 }
880
881 uattr.overwrite = ua_sess->metadata->attr.overwrite;
882 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
883 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
884 uattr.switch_timer_interval =
885 ua_sess->metadata->attr.switch_timer_interval;
886 uattr.read_timer_interval =
887 ua_sess->metadata->attr.read_timer_interval;
888 uattr.output = ua_sess->metadata->attr.output;
889
890 /* UST tracer metadata creation */
891 ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
892 &ua_sess->metadata->obj);
893 if (ret < 0) {
894 ERR("UST app open metadata failed for app pid:%d",
895 app->key.pid);
896 goto error;
897 }
898
899 DBG2("UST metadata opened for app pid %d", app->key.pid);
900 }
901
902 /* Open UST metadata stream */
903 if (ua_sess->metadata->stream_obj == NULL) {
904 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
905 &ua_sess->metadata->stream_obj);
906 if (ret < 0) {
907 ERR("UST create metadata stream failed");
908 goto error;
909 }
910
911 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d",
912 pathname, app->name, app->key.pid);
913 if (ret < 0) {
914 PERROR("asprintf UST create stream");
915 goto error;
916 }
917
918 ret = mkdir(ua_sess->metadata->pathname, S_IRWXU | S_IRWXG);
919 if (ret < 0) {
920 PERROR("mkdir UST metadata");
921 goto error;
922 }
923
924 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d/metadata",
925 pathname, app->name, app->key.pid);
926 if (ret < 0) {
927 PERROR("asprintf UST create stream");
928 goto error;
929 }
930
931 DBG2("UST metadata stream object created for app pid %d",
932 app->key.pid);
933 }
934
935 return 0;
936
937 error:
938 return -1;
939 }
940
941 /*
942 * Add channel to all ust app session.
943 */
944 int ust_app_add_channel_all(struct ltt_ust_session *usess,
945 struct ltt_ust_channel *uchan)
946 {
947 int ret = 0;
948 struct cds_lfht_iter iter;
949 struct cds_lfht_node *node;
950 struct ust_app *app;
951 struct ust_app_session *ua_sess;
952 struct ust_app_channel *ua_chan;
953
954 if (usess == NULL || uchan == NULL) {
955 ERR("Adding UST global channel to NULL values");
956 ret = -1;
957 goto error;
958 }
959
960 DBG2("UST app adding channel %s to global domain for session uid %d",
961 uchan->name, usess->uid);
962
963 rcu_read_lock();
964
965 /* For every UST applications registered */
966 hashtable_get_first(ust_app_ht, &iter);
967 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
968 app = caa_container_of(node, struct ust_app, node);
969
970 /* Create session on the tracer side and add it to app session HT */
971 ua_sess = create_ust_app_session(usess, app);
972 if (ua_sess == NULL) {
973 goto next;
974 }
975
976 /* Create channel onto application */
977 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
978 if (ua_chan == NULL) {
979 goto next;
980 }
981
982 next:
983 /* Next applications */
984 hashtable_get_next(ust_app_ht, &iter);
985 }
986 rcu_read_unlock();
987
988 error:
989 return ret;
990 }
991
992 int ust_app_add_event_all(struct ltt_ust_session *usess,
993 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
994 {
995 int ret = 0;
996 struct cds_lfht_iter iter;
997 struct cds_lfht_node *node, *ua_chan_node;
998 struct ust_app *app;
999 struct ust_app_session *ua_sess;
1000 struct ust_app_channel *ua_chan;
1001 struct ust_app_event *ua_event;
1002
1003 DBG("UST app creating event %s for all apps for session uid %d",
1004 uevent->attr.name, usess->uid);
1005
1006 rcu_read_lock();
1007
1008 /* For all registered applications */
1009 hashtable_get_first(ust_app_ht, &iter);
1010 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
1011 app = caa_container_of(node, struct ust_app, node);
1012
1013 /* Create session on the tracer side and add it to app session HT */
1014 ua_sess = create_ust_app_session(usess, app);
1015 if (ua_sess == NULL) {
1016 goto next;
1017 }
1018
1019 /* Lookup channel in the ust app session */
1020 ua_chan_node = hashtable_lookup(ua_sess->channels,
1021 (void *)uchan->name, strlen(uchan->name), &iter);
1022 if (ua_chan_node == NULL) {
1023 ERR("Channel %s not found in session uid %d. Skipping",
1024 uchan->name, usess->uid);
1025 goto next;
1026 }
1027 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1028
1029 ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1030 if (ua_event == NULL) {
1031 goto next;
1032 }
1033
1034 next:
1035 /* Next applications */
1036 hashtable_get_next(ust_app_ht, &iter);
1037 }
1038 rcu_read_unlock();
1039
1040 return ret;
1041 }
1042
1043 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
1044 {
1045 int ret = 0;
1046 struct cds_lfht_iter iter;
1047 struct cds_lfht_node *node;
1048 struct ust_app_session *ua_sess;
1049 struct ust_app_channel *ua_chan;
1050
1051 DBG("Starting tracing for ust app pid %d", app->key.pid);
1052
1053 rcu_read_lock();
1054
1055 ua_sess = lookup_session_by_app(usess, app);
1056 if (ua_sess == NULL) {
1057 /* Only malloc can failed so something is really wrong */
1058 goto error_rcu_unlock;
1059 }
1060
1061 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1062 if (ret < 0) {
1063 goto error_rcu_unlock;
1064 }
1065
1066 /* For each channel */
1067 hashtable_get_first(ua_sess->channels, &iter);
1068 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
1069 ua_chan = caa_container_of(node, struct ust_app_channel, node);
1070
1071 /* Create all streams */
1072 while (1) {
1073 struct ltt_ust_stream *ustream;
1074
1075 ustream = zmalloc(sizeof(*ustream));
1076 if (ustream == NULL) {
1077 PERROR("zmalloc ust stream");
1078 goto error_rcu_unlock;
1079 }
1080
1081 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1082 &ustream->obj);
1083 if (ret < 0) {
1084 /* Got all streams */
1085 break;
1086 }
1087 ustream->handle = ustream->obj->handle;
1088
1089 /* Order is important */
1090 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
1091 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s-%d/%s_%u",
1092 usess->pathname, app->name, app->key.pid,
1093 ua_chan->name, ua_chan->streams.count++);
1094 if (ret < 0) {
1095 PERROR("asprintf UST create stream");
1096 continue;
1097 }
1098 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1099 ustream->pathname);
1100 }
1101
1102 /* Next applications */
1103 hashtable_get_next(ua_sess->channels, &iter);
1104 }
1105
1106 /* Setup UST consumer socket and send fds to it */
1107 ret = ust_consumer_send_session(usess->consumer_fd, ua_sess);
1108 if (ret < 0) {
1109 goto error_rcu_unlock;
1110 }
1111
1112 /* This start the UST tracing */
1113 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1114 if (ret < 0) {
1115 ERR("Error starting tracing for app pid: %d", app->key.pid);
1116 goto error_rcu_unlock;
1117 }
1118 rcu_read_unlock();
1119
1120 /* Quiescent wait after starting trace */
1121 ustctl_wait_quiescent(app->key.sock);
1122
1123 return 0;
1124
1125 error_rcu_unlock:
1126 rcu_read_unlock();
1127 return -1;
1128 }
1129
1130 int ust_app_start_trace_all(struct ltt_ust_session *usess)
1131 {
1132 int ret = 0;
1133 struct cds_lfht_iter iter;
1134 struct cds_lfht_node *node;
1135 struct ust_app *app;
1136
1137 DBG("Starting all UST traces");
1138
1139 rcu_read_lock();
1140 hashtable_get_first(ust_app_ht, &iter);
1141 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
1142 app = caa_container_of(node, struct ust_app, node);
1143
1144 ret = ust_app_start_trace(usess, app);
1145 if (ret < 0) {
1146 goto next;
1147 }
1148
1149 next:
1150 /* Next applications */
1151 hashtable_get_next(ust_app_ht, &iter);
1152 }
1153 rcu_read_unlock();
1154
1155 return 0;
1156 }
1157
1158 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
1159 {
1160 int ret = 0;
1161 struct cds_lfht_iter iter;
1162 struct ust_app *app;
1163 struct ust_app_session *ua_sess;
1164 struct ust_app_channel *ua_chan;
1165 struct ust_app_event *ua_event;
1166
1167 if (usess == NULL) {
1168 DBG2("No UST session on global update. Returning");
1169 goto error;
1170 }
1171
1172 DBG2("UST app global update for app sock %d for session uid %d", sock,
1173 usess->uid);
1174
1175 rcu_read_lock();
1176
1177 app = find_app_by_sock(sock);
1178 if (app == NULL) {
1179 ERR("Failed to update app sock %d", sock);
1180 goto error;
1181 }
1182
1183 ua_sess = create_ust_app_session(usess, app);
1184 if (ua_sess == NULL) {
1185 goto error;
1186 }
1187
1188 /*
1189 * We can iterate safely here over all UST app session sicne the create ust
1190 * app session above made a shadow copy of the UST global domain from the
1191 * ltt ust session.
1192 */
1193 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1194 ret = create_ust_channel(app, ua_sess, ua_chan);
1195 if (ret < 0) {
1196 /* FIXME: Should we quit here or continue... */
1197 continue;
1198 }
1199
1200 /* For each events */
1201 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
1202 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1203 if (ret < 0) {
1204 /* FIXME: Should we quit here or continue... */
1205 continue;
1206 }
1207 }
1208 }
1209
1210 if (usess->start_trace) {
1211 ret = ust_app_start_trace(usess, app);
1212 if (ret < 0) {
1213 goto error;
1214 }
1215
1216 DBG2("UST trace started for app pid %d", app->key.pid);
1217 }
1218
1219 error:
1220 rcu_read_unlock();
1221 return;
1222 }
This page took 0.09369 seconds and 4 git commands to generate.