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