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