ust-app: fix bogus double-use of iterator
[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 * Open metadata onto the UST tracer for a UST session.
241 */
242 static int open_ust_metadata(struct ust_app *app,
243 struct ust_app_session *ua_sess)
244 {
245 int ret;
246 struct lttng_ust_channel_attr uattr;
247
248 uattr.overwrite = ua_sess->metadata->attr.overwrite;
249 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
250 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
251 uattr.switch_timer_interval =
252 ua_sess->metadata->attr.switch_timer_interval;
253 uattr.read_timer_interval =
254 ua_sess->metadata->attr.read_timer_interval;
255 uattr.output = ua_sess->metadata->attr.output;
256
257 /* UST tracer metadata creation */
258 ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
259 &ua_sess->metadata->obj);
260 if (ret < 0) {
261 ERR("UST app open metadata failed for app pid:%d",
262 app->key.pid);
263 goto error;
264 }
265
266 error:
267 return ret;
268 }
269
270 /*
271 * Create stream onto the UST tracer for a UST session.
272 */
273 static int create_ust_stream(struct ust_app *app,
274 struct ust_app_session *ua_sess)
275 {
276 int ret;
277
278 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
279 &ua_sess->metadata->stream_obj);
280 if (ret < 0) {
281 ERR("UST create metadata stream failed");
282 goto error;
283 }
284
285 error:
286 return ret;
287 }
288
289 /*
290 * Create the specified channel onto the UST tracer for a UST session.
291 */
292 static int create_ust_channel(struct ust_app *app,
293 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
294 {
295 int ret;
296
297 /* TODO: remove cast and use lttng-ust-abi.h */
298 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
299 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
300 if (ret < 0) {
301 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
302 "and session handle %d with ret %d",
303 ua_chan->name, app->key.pid, app->key.sock,
304 ua_sess->handle, ret);
305 goto error;
306 }
307
308 ua_chan->handle = ua_chan->obj->handle;
309 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
310 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
311 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
312
313 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
314 ua_chan->name, app->key.pid, app->key.sock);
315
316 error:
317 return ret;
318 }
319
320 /*
321 * Create the specified event onto the UST tracer for a UST session.
322 */
323 static int create_ust_event(struct ust_app *app,
324 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
325 struct ust_app_event *ua_event)
326 {
327 int ret = 0;
328
329 /* Create UST event on tracer */
330 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
331 &ua_event->obj);
332 if (ret < 0) {
333 ERR("Error ustctl create event %s for app pid: %d with ret %d",
334 ua_event->attr.name, app->key.pid, ret);
335 goto error;
336 }
337
338 ua_event->handle = ua_event->obj->handle;
339 ua_event->enabled = 1;
340
341 DBG2("UST app event %s created successfully for pid:%d",
342 ua_event->attr.name, app->key.pid);
343
344 error:
345 return ret;
346 }
347
348 /*
349 * Alloc new UST app session.
350 */
351 static struct ust_app_session *alloc_ust_app_session(void)
352 {
353 struct ust_app_session *ua_sess;
354
355 /* Init most of the default value by allocating and zeroing */
356 ua_sess = zmalloc(sizeof(struct ust_app_session));
357 if (ua_sess == NULL) {
358 PERROR("malloc");
359 goto error;
360 }
361
362 ua_sess->handle = -1;
363 ua_sess->channels = hashtable_new_str(0);
364
365 return ua_sess;
366
367 error:
368 return NULL;
369 }
370
371 /*
372 * Alloc new UST app channel.
373 */
374 static struct ust_app_channel *alloc_ust_app_channel(char *name,
375 struct lttng_ust_channel *attr)
376 {
377 struct ust_app_channel *ua_chan;
378
379 /* Init most of the default value by allocating and zeroing */
380 ua_chan = zmalloc(sizeof(struct ust_app_channel));
381 if (ua_chan == NULL) {
382 PERROR("malloc");
383 goto error;
384 }
385
386 /* Setup channel name */
387 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
388 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
389
390 ua_chan->handle = -1;
391 ua_chan->ctx = hashtable_new(0);
392 ua_chan->events = hashtable_new_str(0);
393 hashtable_node_init(&ua_chan->node, (void *) ua_chan->name,
394 strlen(ua_chan->name));
395
396 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
397
398 /* Copy attributes */
399 if (attr) {
400 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
401 }
402
403 DBG3("UST app channel %s allocated", ua_chan->name);
404
405 return ua_chan;
406
407 error:
408 return NULL;
409 }
410
411 /*
412 * Alloc new UST app event.
413 */
414 static struct ust_app_event *alloc_ust_app_event(char *name,
415 struct lttng_ust_event *attr)
416 {
417 struct ust_app_event *ua_event;
418
419 /* Init most of the default value by allocating and zeroing */
420 ua_event = zmalloc(sizeof(struct ust_app_event));
421 if (ua_event == NULL) {
422 PERROR("malloc");
423 goto error;
424 }
425
426 strncpy(ua_event->name, name, sizeof(ua_event->name));
427 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
428 ua_event->ctx = hashtable_new(0);
429 hashtable_node_init(&ua_event->node, (void *) ua_event->name,
430 strlen(ua_event->name));
431
432 /* Copy attributes */
433 if (attr) {
434 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
435 }
436
437 DBG3("UST app event %s allocated", ua_event->name);
438
439 return ua_event;
440
441 error:
442 return NULL;
443 }
444
445 /*
446 * Copy data between an UST app event and a LTT event.
447 */
448 static void shadow_copy_event(struct ust_app_event *ua_event,
449 struct ltt_ust_event *uevent)
450 {
451 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
452 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
453
454 /* Copy event attributes */
455 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
456
457 /* TODO: support copy context */
458 }
459
460 /*
461 * Copy data between an UST app channel and a LTT channel.
462 */
463 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
464 struct ltt_ust_channel *uchan)
465 {
466 struct cds_lfht_iter iter;
467 struct cds_lfht_node *ua_event_node;
468 struct ltt_ust_event *uevent;
469 struct ust_app_event *ua_event;
470
471 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
472
473 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
474 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
475 /* Copy event attributes */
476 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
477
478 /* TODO: support copy context */
479
480 /* Copy all events from ltt ust channel to ust app channel */
481 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
482 struct cds_lfht_iter uiter;
483
484 ua_event_node = hashtable_lookup(ua_chan->events,
485 (void *) uevent->attr.name, strlen(uevent->attr.name),
486 &uiter);
487 if (ua_event_node == NULL) {
488 DBG2("UST event %s not found on shadow copy channel",
489 uevent->attr.name);
490 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
491 if (ua_event == NULL) {
492 continue;
493 }
494 shadow_copy_event(ua_event, uevent);
495 hashtable_add_unique(ua_chan->events, &ua_event->node);
496 }
497 }
498
499 DBG3("Shadow copy channel done");
500 }
501
502 /*
503 * Copy data between a UST app session and a regular LTT session.
504 */
505 static void shadow_copy_session(struct ust_app_session *ua_sess,
506 struct ltt_ust_session *usess)
507 {
508 struct cds_lfht_node *ua_chan_node;
509 struct cds_lfht_iter iter;
510 struct ltt_ust_channel *uchan;
511 struct ust_app_channel *ua_chan;
512
513 DBG2("Shadow copy of session handle %d", ua_sess->handle);
514
515 ua_sess->uid = usess->uid;
516
517 /* TODO: support all UST domain */
518
519 /* Iterate over all channels in global domain. */
520 cds_lfht_for_each_entry(usess->domain_global.channels, &iter,
521 uchan, node) {
522 struct cds_lfht_iter uiter;
523
524 ua_chan_node = hashtable_lookup(ua_sess->channels,
525 (void *)uchan->name, strlen(uchan->name),
526 &uiter);
527 if (ua_chan_node != NULL) {
528 continue;
529 }
530
531 DBG2("Channel %s not found on shadow session copy, creating it",
532 uchan->name);
533 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
534 if (ua_chan == NULL) {
535 /* malloc failed... continuing */
536 continue;
537 }
538
539 shadow_copy_channel(ua_chan, uchan);
540 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
541 }
542 }
543
544 /*
545 * Return ust app session from the app session hashtable using the UST session
546 * uid.
547 */
548 static struct ust_app_session *lookup_session_by_app(
549 struct ltt_ust_session *usess, struct ust_app *app)
550 {
551 struct cds_lfht_iter iter;
552 struct cds_lfht_node *node;
553
554 /* Get right UST app session from app */
555 node = hashtable_lookup(app->sessions,
556 (void *) ((unsigned long) usess->uid), sizeof(void *), &iter);
557 if (node == NULL) {
558 goto error;
559 }
560
561 return caa_container_of(node, struct ust_app_session, node);
562
563 error:
564 return NULL;
565 }
566
567 /*
568 * Create a UST session onto the tracer of app and add it the session
569 * hashtable.
570 *
571 * Return ust app session or NULL on error.
572 */
573 static struct ust_app_session *create_ust_app_session(
574 struct ltt_ust_session *usess, struct ust_app *app)
575 {
576 int ret;
577 struct ust_app_session *ua_sess;
578
579 ua_sess = lookup_session_by_app(usess, app);
580 if (ua_sess == NULL) {
581 DBG2("UST app pid: %d session uid %d not found, creating it",
582 app->key.pid, usess->uid);
583 ua_sess = alloc_ust_app_session();
584 if (ua_sess == NULL) {
585 /* Only malloc can failed so something is really wrong */
586 goto error;
587 }
588 shadow_copy_session(ua_sess, usess);
589 }
590
591 if (ua_sess->handle == -1) {
592 ret = ustctl_create_session(app->key.sock);
593 if (ret < 0) {
594 ERR("Error creating session for app pid %d, sock %d",
595 app->key.pid, app->key.sock);
596 /* TODO: free() ua_sess */
597 goto error;
598 }
599
600 DBG2("UST app ustctl create session handle %d", ret);
601 ua_sess->handle = ret;
602
603 /* Add ust app session to app's HT */
604 hashtable_node_init(&ua_sess->node,
605 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
606 hashtable_add_unique(app->sessions, &ua_sess->node);
607
608 DBG2("UST app session created successfully with handle %d", ret);
609 }
610
611 return ua_sess;
612
613 error:
614 return NULL;
615 }
616
617 /*
618 * Create UST app channel and create it on the tracer.
619 */
620 static struct ust_app_channel *create_ust_app_channel(
621 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
622 struct ust_app *app)
623 {
624 int ret = 0;
625 struct cds_lfht_iter iter;
626 struct cds_lfht_node *ua_chan_node;
627 struct ust_app_channel *ua_chan;
628
629 /* Lookup channel in the ust app session */
630 ua_chan_node = hashtable_lookup(ua_sess->channels,
631 (void *)uchan->name, strlen(uchan->name), &iter);
632 if (ua_chan_node == NULL) {
633 DBG2("Unable to find channel %s in ust session uid %u",
634 uchan->name, ua_sess->uid);
635 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
636 if (ua_chan == NULL) {
637 goto error;
638 }
639 shadow_copy_channel(ua_chan, uchan);
640
641 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
642 } else {
643 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
644 }
645
646 ret = create_ust_channel(app, ua_sess, ua_chan);
647 if (ret < 0) {
648 goto error;
649 }
650
651 return ua_chan;
652
653 error:
654 return NULL;
655 }
656
657 /*
658 * Create UST app event and create it on the tracer side.
659 */
660 static struct ust_app_event *create_ust_app_event(
661 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
662 struct ltt_ust_event *uevent, struct ust_app *app)
663 {
664 int ret;
665 struct cds_lfht_iter iter;
666 struct cds_lfht_node *ua_event_node;
667 struct ust_app_event *ua_event;
668
669 /* Get event node */
670 ua_event_node = hashtable_lookup(ua_chan->events,
671 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
672 if (ua_event_node == NULL) {
673 DBG2("UST app event %s not found, creating it", uevent->attr.name);
674 /* Does not exist so create one */
675 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
676 if (ua_event == NULL) {
677 /* Only malloc can failed so something is really wrong */
678 goto error;
679 }
680 shadow_copy_event(ua_event, uevent);
681
682 hashtable_add_unique(ua_chan->events, &ua_event->node);
683 } else {
684 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
685 }
686
687 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
688 if (ret < 0) {
689 goto error;
690 }
691
692 return ua_event;
693
694 error:
695 return NULL;
696 }
697
698 /*
699 * Create UST metadata and open it on the tracer side.
700 */
701 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
702 char *pathname, struct ust_app *app)
703 {
704 int ret = 0;
705
706 if (ua_sess->metadata == NULL) {
707 /* Allocate UST metadata */
708 ua_sess->metadata = trace_ust_create_metadata(pathname);
709 if (ua_sess->metadata == NULL) {
710 ERR("UST app session %d creating metadata failed",
711 ua_sess->handle);
712 goto error;
713 }
714
715 ret = open_ust_metadata(app, ua_sess);
716 if (ret < 0) {
717 goto error;
718 }
719
720 DBG2("UST metadata opened for app pid %d", app->key.pid);
721 }
722
723 /* Open UST metadata stream */
724 if (ua_sess->metadata->stream_obj == NULL) {
725 ret = create_ust_stream(app, ua_sess);
726 if (ret < 0) {
727 goto error;
728 }
729
730 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d",
731 pathname, app->name, app->key.pid);
732 if (ret < 0) {
733 PERROR("asprintf UST create stream");
734 goto error;
735 }
736
737 ret = mkdir(ua_sess->metadata->pathname, S_IRWXU | S_IRWXG);
738 if (ret < 0) {
739 PERROR("mkdir UST metadata");
740 goto error;
741 }
742
743 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d/metadata",
744 pathname, app->name, app->key.pid);
745 if (ret < 0) {
746 PERROR("asprintf UST create stream");
747 goto error;
748 }
749
750 DBG2("UST metadata stream object created for app pid %d",
751 app->key.pid);
752 } else {
753 ERR("Attempting to create stream without metadata opened");
754 goto error;
755 }
756
757 return 0;
758
759 error:
760 return -1;
761 }
762
763 /*
764 * Return pointer to traceable apps list.
765 */
766 struct cds_lfht *ust_app_get_ht(void)
767 {
768 return ust_app_ht;
769 }
770
771 /*
772 * Return ust app pointer or NULL if not found.
773 */
774 struct ust_app *ust_app_find_by_pid(pid_t pid)
775 {
776 struct cds_lfht_node *node;
777 struct cds_lfht_iter iter;
778
779 rcu_read_lock();
780 node = hashtable_lookup(ust_app_ht,
781 (void *)((unsigned long) pid), sizeof(void *), &iter);
782 if (node == NULL) {
783 DBG2("UST app no found with pid %d", pid);
784 goto error;
785 }
786 rcu_read_unlock();
787
788 DBG2("Found UST app by pid %d", pid);
789
790 return caa_container_of(node, struct ust_app, node);
791
792 error:
793 rcu_read_unlock();
794 return NULL;
795 }
796
797 /*
798 * Using pid and uid (of the app), allocate a new ust_app struct and
799 * add it to the global traceable app list.
800 *
801 * On success, return 0, else return malloc ENOMEM.
802 */
803 int ust_app_register(struct ust_register_msg *msg, int sock)
804 {
805 struct ust_app *lta;
806
807 lta = zmalloc(sizeof(struct ust_app));
808 if (lta == NULL) {
809 PERROR("malloc");
810 return -ENOMEM;
811 }
812
813 lta->ppid = msg->ppid;
814 lta->uid = msg->uid;
815 lta->gid = msg->gid;
816 lta->v_major = msg->major;
817 lta->v_minor = msg->minor;
818 strncpy(lta->name, msg->name, sizeof(lta->name));
819 lta->name[16] = '\0';
820 lta->sessions = hashtable_new(0);
821
822 /* Set key map */
823 lta->key.pid = msg->pid;
824 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
825 sizeof(void *));
826 lta->key.sock = sock;
827 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
828 sizeof(void *));
829
830 rcu_read_lock();
831 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
832 hashtable_add_unique(ust_app_ht, &lta->node);
833 rcu_read_unlock();
834
835 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
836 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
837 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
838
839 return 0;
840 }
841
842 /*
843 * Unregister app by removing it from the global traceable app list and freeing
844 * the data struct.
845 *
846 * The socket is already closed at this point so no close to sock.
847 */
848 void ust_app_unregister(int sock)
849 {
850 struct ust_app *lta;
851 struct cds_lfht_node *node;
852 struct cds_lfht_iter iter;
853
854 rcu_read_lock();
855 lta = find_app_by_sock(sock);
856 if (lta == NULL) {
857 ERR("Unregister app sock %d not found!", sock);
858 goto error;
859 }
860
861 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
862
863 /* Get the node reference for a call_rcu */
864 node = hashtable_lookup(ust_app_ht,
865 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
866 if (node == NULL) {
867 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
868 goto error;
869 }
870
871 /* We got called because the socket was closed on the remote end. */
872 close(sock);
873 /* Using a flag because we still need "sock" as a key. */
874 lta->sock_closed = 1;
875 hashtable_del(ust_app_ht, &iter);
876 call_rcu(&node->head, delete_ust_app_rcu);
877 error:
878 rcu_read_unlock();
879 return;
880 }
881
882 /*
883 * Return traceable_app_count
884 */
885 unsigned long ust_app_list_count(void)
886 {
887 unsigned long count;
888
889 rcu_read_lock();
890 count = hashtable_get_count(ust_app_ht);
891 rcu_read_unlock();
892
893 return count;
894 }
895
896 /*
897 * Fill events array with all events name of all registered apps.
898 */
899 int ust_app_list_events(struct lttng_event **events)
900 {
901 int ret, handle;
902 size_t nbmem, count = 0;
903 struct cds_lfht_iter iter;
904 struct ust_app *app;
905 struct lttng_event *tmp;
906
907 nbmem = UST_APP_EVENT_LIST_SIZE;
908 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
909 if (tmp == NULL) {
910 PERROR("zmalloc ust app events");
911 ret = -ENOMEM;
912 goto error;
913 }
914
915 rcu_read_lock();
916
917 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
918 handle = ustctl_tracepoint_list(app->key.sock);
919 if (handle < 0) {
920 ERR("UST app list events getting handle failed for app pid %d",
921 app->key.pid);
922 continue;
923 }
924
925 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
926 tmp[count].name)) != -ENOENT) {
927 if (count > nbmem) {
928 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
929 nbmem + UST_APP_EVENT_LIST_SIZE);
930 nbmem += UST_APP_EVENT_LIST_SIZE;
931 tmp = realloc(tmp, nbmem);
932 if (tmp == NULL) {
933 PERROR("realloc ust app events");
934 ret = -ENOMEM;
935 goto rcu_error;
936 }
937 }
938
939 tmp[count].type = LTTNG_UST_TRACEPOINT;
940 tmp[count].pid = app->key.pid;
941 tmp[count].enabled = -1;
942 count++;
943 }
944 }
945
946 ret = count;
947 *events = tmp;
948
949 DBG2("UST app list events done (%zu events)", count);
950
951 rcu_error:
952 rcu_read_unlock();
953 error:
954 return ret;
955 }
956
957 /*
958 * Free and clean all traceable apps of the global list.
959 */
960 void ust_app_clean_list(void)
961 {
962 int ret;
963 struct cds_lfht_node *node;
964 struct cds_lfht_iter iter;
965 struct ust_app *app;
966
967 DBG2("UST app cleaning registered apps hash table");
968
969 rcu_read_lock();
970
971 cds_lfht_for_each(ust_app_ht, &iter, node) {
972 app = caa_container_of(node, struct ust_app, node);
973 close(app->key.sock);
974 app->sock_closed = 1;
975
976 ret = hashtable_del(ust_app_ht, &iter);
977 if (!ret) {
978 call_rcu(&node->head, delete_ust_app_rcu);
979 }
980 }
981
982 hashtable_destroy(ust_app_ht);
983 hashtable_destroy(ust_app_sock_key_map);
984
985 rcu_read_unlock();
986 }
987
988 /*
989 * Init UST app hash table.
990 */
991 void ust_app_ht_alloc(void)
992 {
993 ust_app_ht = hashtable_new(0);
994 ust_app_sock_key_map = hashtable_new(0);
995 }
996
997 /*
998 * For a specific UST session, create the channel for all registered apps.
999 */
1000 int ust_app_create_channel_all(struct ltt_ust_session *usess,
1001 struct ltt_ust_channel *uchan)
1002 {
1003 int ret = 0;
1004 struct cds_lfht_iter iter;
1005 struct ust_app *app;
1006 struct ust_app_session *ua_sess;
1007 struct ust_app_channel *ua_chan;
1008
1009 if (usess == NULL || uchan == NULL) {
1010 ERR("Adding UST global channel to NULL values");
1011 ret = -1;
1012 goto error;
1013 }
1014
1015 DBG2("UST app adding channel %s to global domain for session uid %d",
1016 uchan->name, usess->uid);
1017
1018 rcu_read_lock();
1019
1020 /* For every registered applications */
1021 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1022 /* Create session on the tracer side and add it to app session HT */
1023 ua_sess = create_ust_app_session(usess, app);
1024 if (ua_sess == NULL) {
1025 continue;
1026 }
1027
1028 /* Create channel onto application */
1029 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1030 if (ua_chan == NULL) {
1031 continue;
1032 }
1033 }
1034
1035 rcu_read_unlock();
1036
1037 error:
1038 return ret;
1039 }
1040
1041 /*
1042 * For a specific UST session and UST channel, create the event for all
1043 * registered apps.
1044 */
1045 int ust_app_create_event_all(struct ltt_ust_session *usess,
1046 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1047 {
1048 int ret = 0;
1049 struct cds_lfht_iter iter;
1050 struct cds_lfht_node *ua_chan_node;
1051 struct ust_app *app;
1052 struct ust_app_session *ua_sess;
1053 struct ust_app_channel *ua_chan;
1054 struct ust_app_event *ua_event;
1055
1056 DBG("UST app creating event %s for all apps for session uid %d",
1057 uevent->attr.name, usess->uid);
1058
1059 rcu_read_lock();
1060
1061 /* For all registered applications */
1062 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1063 struct cds_lfht_iter uiter;
1064
1065 /* Create session on the tracer side and add it to app session HT */
1066 ua_sess = create_ust_app_session(usess, app);
1067 if (ua_sess == NULL) {
1068 continue;
1069 }
1070
1071 /* Lookup channel in the ust app session */
1072 ua_chan_node = hashtable_lookup(ua_sess->channels,
1073 (void *)uchan->name, strlen(uchan->name),
1074 &uiter);
1075 if (ua_chan_node == NULL) {
1076 ERR("Channel %s not found in session uid %d. Skipping",
1077 uchan->name, usess->uid);
1078 continue;
1079 }
1080 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1081
1082 ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1083 if (ua_event == NULL) {
1084 continue;
1085 }
1086 }
1087
1088 rcu_read_unlock();
1089
1090 return ret;
1091 }
1092
1093 /*
1094 * Start tracing for a specific UST session and app.
1095 */
1096 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
1097 {
1098 int ret = 0;
1099 struct cds_lfht_iter iter;
1100 struct ust_app_session *ua_sess;
1101 struct ust_app_channel *ua_chan;
1102 struct ltt_ust_stream *ustream;
1103
1104 DBG("Starting tracing for ust app pid %d", app->key.pid);
1105
1106 rcu_read_lock();
1107
1108 ua_sess = lookup_session_by_app(usess, app);
1109 if (ua_sess == NULL) {
1110 /* Only malloc can failed so something is really wrong */
1111 goto error_rcu_unlock;
1112 }
1113
1114 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1115 if (ret < 0) {
1116 goto error_rcu_unlock;
1117 }
1118
1119 /* For each channel */
1120 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1121 /* Create all streams */
1122 while (1) {
1123 /* Create UST stream */
1124 ustream = zmalloc(sizeof(*ustream));
1125 if (ustream == NULL) {
1126 PERROR("zmalloc ust stream");
1127 goto error_rcu_unlock;
1128 }
1129
1130 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1131 &ustream->obj);
1132 if (ret < 0) {
1133 /* Got all streams */
1134 break;
1135 }
1136 ustream->handle = ustream->obj->handle;
1137
1138 /* Order is important */
1139 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
1140 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s-%d/%s_%u",
1141 usess->pathname, app->name, app->key.pid,
1142 ua_chan->name, ua_chan->streams.count++);
1143 if (ret < 0) {
1144 PERROR("asprintf UST create stream");
1145 continue;
1146 }
1147 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1148 ustream->pathname);
1149 }
1150 }
1151
1152 /* Setup UST consumer socket and send fds to it */
1153 ret = ust_consumer_send_session(ust_consumer_fd, ua_sess);
1154 if (ret < 0) {
1155 goto error_rcu_unlock;
1156 }
1157
1158 /* This start the UST tracing */
1159 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1160 if (ret < 0) {
1161 ERR("Error starting tracing for app pid: %d", app->key.pid);
1162 goto error_rcu_unlock;
1163 }
1164
1165 rcu_read_unlock();
1166
1167 /* Quiescent wait after starting trace */
1168 ustctl_wait_quiescent(app->key.sock);
1169
1170 return 0;
1171
1172 error_rcu_unlock:
1173 rcu_read_unlock();
1174 return -1;
1175 }
1176
1177 /*
1178 * Start tracing for the UST session.
1179 */
1180 int ust_app_start_trace_all(struct ltt_ust_session *usess)
1181 {
1182 int ret = 0;
1183 struct cds_lfht_iter iter;
1184 struct ust_app *app;
1185
1186 DBG("Starting all UST traces");
1187
1188 rcu_read_lock();
1189
1190 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1191 ret = ust_app_start_trace(usess, app);
1192 if (ret < 0) {
1193 /* Continue to next apps even on error */
1194 continue;
1195 }
1196 }
1197
1198 rcu_read_unlock();
1199
1200 return 0;
1201 }
1202
1203 /*
1204 * Add channels/events from UST global domain to registered apps at sock.
1205 */
1206 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
1207 {
1208 int ret = 0;
1209 struct cds_lfht_iter iter;
1210 struct ust_app *app;
1211 struct ust_app_session *ua_sess;
1212 struct ust_app_channel *ua_chan;
1213 struct ust_app_event *ua_event;
1214
1215 if (usess == NULL) {
1216 ERR("No UST session on global update. Returning");
1217 goto error;
1218 }
1219
1220 DBG2("UST app global update for app sock %d for session uid %d", sock,
1221 usess->uid);
1222
1223 rcu_read_lock();
1224
1225 app = find_app_by_sock(sock);
1226 if (app == NULL) {
1227 ERR("Failed to update app sock %d", sock);
1228 goto error;
1229 }
1230
1231 ua_sess = create_ust_app_session(usess, app);
1232 if (ua_sess == NULL) {
1233 goto error;
1234 }
1235
1236 /*
1237 * We can iterate safely here over all UST app session sicne the create ust
1238 * app session above made a shadow copy of the UST global domain from the
1239 * ltt ust session.
1240 */
1241 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1242 ret = create_ust_channel(app, ua_sess, ua_chan);
1243 if (ret < 0) {
1244 /* FIXME: Should we quit here or continue... */
1245 continue;
1246 }
1247
1248 /* For each events */
1249 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
1250 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1251 if (ret < 0) {
1252 /* FIXME: Should we quit here or continue... */
1253 continue;
1254 }
1255 }
1256 }
1257
1258 if (usess->start_trace) {
1259 ret = ust_app_start_trace(usess, app);
1260 if (ret < 0) {
1261 goto error;
1262 }
1263
1264 DBG2("UST trace started for app pid %d", app->key.pid);
1265 }
1266
1267 error:
1268 rcu_read_unlock();
1269 return;
1270 }
This page took 0.084285 seconds and 5 git commands to generate.