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