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