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