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