Implement 32/64 bit consumer support
[lttng-tools.git] / lttng-sessiond / ust-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
91d76f53
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
099e26bd 24#include <string.h>
aba8e916
DG
25#include <sys/stat.h>
26#include <sys/types.h>
099e26bd 27#include <unistd.h>
91d76f53 28
0df502fd 29#include <urcu/compiler.h>
1e307fab 30#include <lttngerr.h>
48842b30 31#include <lttng-share.h>
1e307fab 32
f6a9efaa 33#include "hashtable.h"
56fff090 34#include "ust-app.h"
48842b30 35#include "ust-consumer.h"
d80a6244
DG
36#include "ust-ctl.h"
37
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
247/*
5b4a0ec0 248 * Open metadata onto the UST tracer for a UST session.
0177d773 249 */
5b4a0ec0
DG
250static int open_ust_metadata(struct ust_app *app,
251 struct ust_app_session *ua_sess)
0177d773 252{
5b4a0ec0
DG
253 int ret;
254 struct lttng_ust_channel_attr uattr;
0177d773 255
5b4a0ec0
DG
256 uattr.overwrite = ua_sess->metadata->attr.overwrite;
257 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
258 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
259 uattr.switch_timer_interval =
260 ua_sess->metadata->attr.switch_timer_interval;
261 uattr.read_timer_interval =
262 ua_sess->metadata->attr.read_timer_interval;
263 uattr.output = ua_sess->metadata->attr.output;
264
265 /* UST tracer metadata creation */
266 ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
267 &ua_sess->metadata->obj);
268 if (ret < 0) {
269 ERR("UST app open metadata failed for app pid:%d",
270 app->key.pid);
f6a9efaa 271 goto error;
0177d773 272 }
f6a9efaa
DG
273
274error:
5b4a0ec0 275 return ret;
91d76f53
DG
276}
277
278/*
5b4a0ec0 279 * Create stream onto the UST tracer for a UST session.
91d76f53 280 */
5b4a0ec0
DG
281static int create_ust_stream(struct ust_app *app,
282 struct ust_app_session *ua_sess)
91d76f53 283{
5b4a0ec0 284 int ret;
421cb601 285
5b4a0ec0
DG
286 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
287 &ua_sess->metadata->stream_obj);
288 if (ret < 0) {
289 ERR("UST create metadata stream failed");
421cb601 290 goto error;
91d76f53 291 }
421cb601 292
421cb601 293error:
5b4a0ec0 294 return ret;
91d76f53
DG
295}
296
b551a063 297/*
5b4a0ec0 298 * Create the specified channel onto the UST tracer for a UST session.
b551a063 299 */
5b4a0ec0
DG
300static int create_ust_channel(struct ust_app *app,
301 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
b551a063 302{
5b4a0ec0 303 int ret;
b551a063 304
5b4a0ec0
DG
305 /* TODO: remove cast and use lttng-ust-abi.h */
306 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
307 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
308 if (ret < 0) {
309 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
310 "and session handle %d with ret %d",
311 ua_chan->name, app->key.pid, app->key.sock,
312 ua_sess->handle, ret);
b551a063
DG
313 goto error;
314 }
315
5b4a0ec0
DG
316 ua_chan->handle = ua_chan->obj->handle;
317 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
318 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
319 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
b551a063 320
5b4a0ec0
DG
321 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
322 ua_chan->name, app->key.pid, app->key.sock);
b551a063 323
b551a063
DG
324error:
325 return ret;
326}
327
91d76f53 328/*
5b4a0ec0 329 * Create the specified event onto the UST tracer for a UST session.
91d76f53 330 */
5b4a0ec0
DG
331static int create_ust_event(struct ust_app *app,
332 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
333 struct ust_app_event *ua_event)
91d76f53 334{
5b4a0ec0 335 int ret = 0;
284d8f55 336
5b4a0ec0
DG
337 /* Create UST event on tracer */
338 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
339 &ua_event->obj);
340 if (ret < 0) {
341 ERR("Error ustctl create event %s for app pid: %d with ret %d",
342 ua_event->attr.name, app->key.pid, ret);
343 goto error;
91d76f53 344 }
f6a9efaa 345
5b4a0ec0
DG
346 ua_event->handle = ua_event->obj->handle;
347 ua_event->enabled = 1;
284d8f55 348
5b4a0ec0
DG
349 DBG2("UST app event %s created successfully for pid:%d",
350 ua_event->attr.name, app->key.pid);
f6a9efaa 351
5b4a0ec0
DG
352error:
353 return ret;
91d76f53 354}
48842b30
DG
355
356/*
357 * Alloc new UST app session.
358 */
421cb601 359static struct ust_app_session *alloc_ust_app_session(void)
48842b30
DG
360{
361 struct ust_app_session *ua_sess;
362
421cb601 363 /* Init most of the default value by allocating and zeroing */
48842b30
DG
364 ua_sess = zmalloc(sizeof(struct ust_app_session));
365 if (ua_sess == NULL) {
366 PERROR("malloc");
367 goto error;
368 }
369
48842b30
DG
370 ua_sess->handle = -1;
371 ua_sess->channels = hashtable_new_str(0);
48842b30
DG
372
373 return ua_sess;
374
375error:
376 return NULL;
377}
378
421cb601
DG
379/*
380 * Alloc new UST app channel.
381 */
284d8f55
DG
382static struct ust_app_channel *alloc_ust_app_channel(char *name,
383 struct lttng_ust_channel *attr)
48842b30
DG
384{
385 struct ust_app_channel *ua_chan;
386
421cb601 387 /* Init most of the default value by allocating and zeroing */
48842b30
DG
388 ua_chan = zmalloc(sizeof(struct ust_app_channel));
389 if (ua_chan == NULL) {
390 PERROR("malloc");
391 goto error;
392 }
393
284d8f55 394 /* Setup channel name */
48842b30
DG
395 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
396 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
284d8f55 397
48842b30 398 ua_chan->handle = -1;
48842b30 399 ua_chan->ctx = hashtable_new(0);
48842b30
DG
400 ua_chan->events = hashtable_new_str(0);
401 hashtable_node_init(&ua_chan->node, (void *) ua_chan->name,
402 strlen(ua_chan->name));
403
284d8f55
DG
404 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
405
406 /* Copy attributes */
407 if (attr) {
408 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
409 }
410
48842b30
DG
411 DBG3("UST app channel %s allocated", ua_chan->name);
412
413 return ua_chan;
414
415error:
416 return NULL;
417}
418
421cb601
DG
419/*
420 * Alloc new UST app event.
421 */
284d8f55
DG
422static struct ust_app_event *alloc_ust_app_event(char *name,
423 struct lttng_ust_event *attr)
48842b30
DG
424{
425 struct ust_app_event *ua_event;
426
421cb601 427 /* Init most of the default value by allocating and zeroing */
48842b30
DG
428 ua_event = zmalloc(sizeof(struct ust_app_event));
429 if (ua_event == NULL) {
430 PERROR("malloc");
431 goto error;
432 }
433
434 strncpy(ua_event->name, name, sizeof(ua_event->name));
435 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
436 ua_event->ctx = hashtable_new(0);
437 hashtable_node_init(&ua_event->node, (void *) ua_event->name,
438 strlen(ua_event->name));
439
284d8f55
DG
440 /* Copy attributes */
441 if (attr) {
442 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
443 }
444
48842b30
DG
445 DBG3("UST app event %s allocated", ua_event->name);
446
447 return ua_event;
448
449error:
450 return NULL;
451}
452
5b4a0ec0
DG
453/*
454 * Copy data between an UST app event and a LTT event.
455 */
421cb601 456static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
457 struct ltt_ust_event *uevent)
458{
459 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
460 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
461
5b4a0ec0
DG
462 /* Copy event attributes */
463 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
464
48842b30
DG
465 /* TODO: support copy context */
466}
467
5b4a0ec0
DG
468/*
469 * Copy data between an UST app channel and a LTT channel.
470 */
421cb601 471static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
472 struct ltt_ust_channel *uchan)
473{
474 struct cds_lfht_iter iter;
5b4a0ec0 475 struct cds_lfht_node *ua_event_node;
48842b30
DG
476 struct ltt_ust_event *uevent;
477 struct ust_app_event *ua_event;
478
421cb601 479 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
48842b30
DG
480
481 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
482 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
5b4a0ec0
DG
483 /* Copy event attributes */
484 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
48842b30
DG
485
486 /* TODO: support copy context */
487
421cb601 488 /* Copy all events from ltt ust channel to ust app channel */
5b4a0ec0 489 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
ba767faf
MD
490 struct cds_lfht_iter uiter;
491
48842b30 492 ua_event_node = hashtable_lookup(ua_chan->events,
ba767faf
MD
493 (void *) uevent->attr.name, strlen(uevent->attr.name),
494 &uiter);
48842b30 495 if (ua_event_node == NULL) {
421cb601 496 DBG2("UST event %s not found on shadow copy channel",
48842b30 497 uevent->attr.name);
284d8f55 498 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 499 if (ua_event == NULL) {
5b4a0ec0 500 continue;
48842b30 501 }
421cb601 502 shadow_copy_event(ua_event, uevent);
48842b30 503 hashtable_add_unique(ua_chan->events, &ua_event->node);
48842b30 504 }
48842b30
DG
505 }
506
421cb601 507 DBG3("Shadow copy channel done");
48842b30
DG
508}
509
5b4a0ec0
DG
510/*
511 * Copy data between a UST app session and a regular LTT session.
512 */
421cb601 513static void shadow_copy_session(struct ust_app_session *ua_sess,
477d7741
MD
514 struct ltt_ust_session *usess,
515 struct ust_app *app)
48842b30 516{
5b4a0ec0 517 struct cds_lfht_node *ua_chan_node;
48842b30
DG
518 struct cds_lfht_iter iter;
519 struct ltt_ust_channel *uchan;
520 struct ust_app_channel *ua_chan;
477d7741
MD
521 time_t rawtime;
522 struct tm *timeinfo;
523 char datetime[16];
524 int ret;
525
526 /* Get date and time for unique app path */
527 time(&rawtime);
528 timeinfo = localtime(&rawtime);
529 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 530
421cb601 531 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30
DG
532
533 ua_sess->uid = usess->uid;
534
477d7741
MD
535 ret = snprintf(ua_sess->path, PATH_MAX,
536 "%s/%s-%d-%s",
537 usess->pathname, app->name, app->key.pid,
538 datetime);
539 if (ret < 0) {
540 PERROR("asprintf UST shadow copy session");
541 /* TODO: We cannot return an error from here.. */
542 assert(0);
543 }
544
48842b30
DG
545 /* TODO: support all UST domain */
546
547 /* Iterate over all channels in global domain. */
5b4a0ec0
DG
548 cds_lfht_for_each_entry(usess->domain_global.channels, &iter,
549 uchan, node) {
ba767faf
MD
550 struct cds_lfht_iter uiter;
551
48842b30 552 ua_chan_node = hashtable_lookup(ua_sess->channels,
ba767faf
MD
553 (void *)uchan->name, strlen(uchan->name),
554 &uiter);
5b4a0ec0
DG
555 if (ua_chan_node != NULL) {
556 continue;
557 }
421cb601 558
5b4a0ec0
DG
559 DBG2("Channel %s not found on shadow session copy, creating it",
560 uchan->name);
561 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
562 if (ua_chan == NULL) {
563 /* malloc failed... continuing */
564 continue;
48842b30
DG
565 }
566
5b4a0ec0
DG
567 shadow_copy_channel(ua_chan, uchan);
568 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
48842b30
DG
569 }
570}
571
84cd17c6
MD
572static
573void __lookup_session_by_app(struct ltt_ust_session *usess,
574 struct ust_app *app, struct cds_lfht_iter *iter)
575{
576 /* Get right UST app session from app */
577 (void) hashtable_lookup(app->sessions,
578 (void *) ((unsigned long) usess->uid), sizeof(void *),
579 iter);
580}
581
421cb601
DG
582/*
583 * Return ust app session from the app session hashtable using the UST session
584 * uid.
585 */
48842b30
DG
586static struct ust_app_session *lookup_session_by_app(
587 struct ltt_ust_session *usess, struct ust_app *app)
588{
589 struct cds_lfht_iter iter;
590 struct cds_lfht_node *node;
591
84cd17c6
MD
592 __lookup_session_by_app(usess, app, &iter);
593 node = hashtable_iter_get_node(&iter);
48842b30
DG
594 if (node == NULL) {
595 goto error;
596 }
597
598 return caa_container_of(node, struct ust_app_session, node);
599
600error:
601 return NULL;
602}
603
421cb601
DG
604/*
605 * Create a UST session onto the tracer of app and add it the session
606 * hashtable.
607 *
608 * Return ust app session or NULL on error.
609 */
610static struct ust_app_session *create_ust_app_session(
611 struct ltt_ust_session *usess, struct ust_app *app)
612{
613 int ret;
614 struct ust_app_session *ua_sess;
615
616 ua_sess = lookup_session_by_app(usess, app);
617 if (ua_sess == NULL) {
618 DBG2("UST app pid: %d session uid %d not found, creating it",
619 app->key.pid, usess->uid);
620 ua_sess = alloc_ust_app_session();
621 if (ua_sess == NULL) {
622 /* Only malloc can failed so something is really wrong */
623 goto error;
624 }
477d7741 625 shadow_copy_session(ua_sess, usess, app);
421cb601
DG
626 }
627
628 if (ua_sess->handle == -1) {
629 ret = ustctl_create_session(app->key.sock);
630 if (ret < 0) {
631 ERR("Error creating session for app pid %d, sock %d",
632 app->key.pid, app->key.sock);
633 /* TODO: free() ua_sess */
634 goto error;
635 }
636
637 DBG2("UST app ustctl create session handle %d", ret);
638 ua_sess->handle = ret;
639
640 /* Add ust app session to app's HT */
641 hashtable_node_init(&ua_sess->node,
642 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
643 hashtable_add_unique(app->sessions, &ua_sess->node);
644
645 DBG2("UST app session created successfully with handle %d", ret);
646 }
647
648 return ua_sess;
649
650error:
651 return NULL;
652}
653
284d8f55 654/*
5b4a0ec0 655 * Create UST app channel and create it on the tracer.
284d8f55 656 */
5b4a0ec0
DG
657static struct ust_app_channel *create_ust_app_channel(
658 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
659 struct ust_app *app)
660{
661 int ret = 0;
662 struct cds_lfht_iter iter;
663 struct cds_lfht_node *ua_chan_node;
664 struct ust_app_channel *ua_chan;
665
666 /* Lookup channel in the ust app session */
667 ua_chan_node = hashtable_lookup(ua_sess->channels,
668 (void *)uchan->name, strlen(uchan->name), &iter);
669 if (ua_chan_node == NULL) {
670 DBG2("Unable to find channel %s in ust session uid %u",
671 uchan->name, ua_sess->uid);
672 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
673 if (ua_chan == NULL) {
674 goto error;
675 }
676 shadow_copy_channel(ua_chan, uchan);
677
678 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
679 } else {
680 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
681 }
682
683 ret = create_ust_channel(app, ua_sess, ua_chan);
684 if (ret < 0) {
685 goto error;
686 }
687
688 return ua_chan;
689
690error:
691 return NULL;
692}
693
694/*
695 * Create UST app event and create it on the tracer side.
696 */
697static struct ust_app_event *create_ust_app_event(
698 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
699 struct ltt_ust_event *uevent, struct ust_app *app)
284d8f55
DG
700{
701 int ret;
5b4a0ec0
DG
702 struct cds_lfht_iter iter;
703 struct cds_lfht_node *ua_event_node;
704 struct ust_app_event *ua_event;
284d8f55 705
5b4a0ec0
DG
706 /* Get event node */
707 ua_event_node = hashtable_lookup(ua_chan->events,
708 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
709 if (ua_event_node == NULL) {
710 DBG2("UST app event %s not found, creating it", uevent->attr.name);
711 /* Does not exist so create one */
712 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
713 if (ua_event == NULL) {
714 /* Only malloc can failed so something is really wrong */
715 goto error;
716 }
717 shadow_copy_event(ua_event, uevent);
718
719 hashtable_add_unique(ua_chan->events, &ua_event->node);
720 } else {
721 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
722 }
723
724 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 725 if (ret < 0) {
284d8f55
DG
726 goto error;
727 }
728
5b4a0ec0 729 return ua_event;
284d8f55 730
5b4a0ec0
DG
731error:
732 return NULL;
733}
734
735/*
736 * Create UST metadata and open it on the tracer side.
737 */
738static int create_ust_app_metadata(struct ust_app_session *ua_sess,
739 char *pathname, struct ust_app *app)
740{
741 int ret = 0;
742
743 if (ua_sess->metadata == NULL) {
744 /* Allocate UST metadata */
745 ua_sess->metadata = trace_ust_create_metadata(pathname);
746 if (ua_sess->metadata == NULL) {
747 ERR("UST app session %d creating metadata failed",
748 ua_sess->handle);
749 goto error;
750 }
751
752 ret = open_ust_metadata(app, ua_sess);
753 if (ret < 0) {
754 goto error;
755 }
756
757 DBG2("UST metadata opened for app pid %d", app->key.pid);
758 }
759
760 /* Open UST metadata stream */
761 if (ua_sess->metadata->stream_obj == NULL) {
762 ret = create_ust_stream(app, ua_sess);
763 if (ret < 0) {
764 goto error;
765 }
766
477d7741 767 ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
5b4a0ec0
DG
768 if (ret < 0) {
769 PERROR("mkdir UST metadata");
770 goto error;
771 }
772
477d7741
MD
773 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
774 "%s/metadata", ua_sess->path);
5b4a0ec0
DG
775 if (ret < 0) {
776 PERROR("asprintf UST create stream");
777 goto error;
778 }
779
780 DBG2("UST metadata stream object created for app pid %d",
781 app->key.pid);
782 } else {
783 ERR("Attempting to create stream without metadata opened");
784 goto error;
785 }
786
787 return 0;
788
789error:
790 return -1;
791}
792
793/*
794 * Return pointer to traceable apps list.
795 */
796struct cds_lfht *ust_app_get_ht(void)
797{
798 return ust_app_ht;
799}
800
801/*
802 * Return ust app pointer or NULL if not found.
803 */
804struct ust_app *ust_app_find_by_pid(pid_t pid)
805{
806 struct cds_lfht_node *node;
807 struct cds_lfht_iter iter;
808
809 rcu_read_lock();
810 node = hashtable_lookup(ust_app_ht,
811 (void *)((unsigned long) pid), sizeof(void *), &iter);
812 if (node == NULL) {
813 DBG2("UST app no found with pid %d", pid);
814 goto error;
815 }
816 rcu_read_unlock();
817
818 DBG2("Found UST app by pid %d", pid);
819
820 return caa_container_of(node, struct ust_app, node);
821
822error:
823 rcu_read_unlock();
824 return NULL;
825}
826
827/*
828 * Using pid and uid (of the app), allocate a new ust_app struct and
829 * add it to the global traceable app list.
830 *
0df502fd
MD
831 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
832 * bitness is not supported.
5b4a0ec0
DG
833 */
834int ust_app_register(struct ust_register_msg *msg, int sock)
835{
836 struct ust_app *lta;
837
7753dea8
MD
838 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
839 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
f943b0fb 840 ERR("Registration failed: application \"%s\" (pid: %d) has "
7753dea8
MD
841 "%d-bit long, but no consumerd for this long size is available.\n",
842 msg->name, msg->pid, msg->bits_per_long);
88ff5b7f 843 close(sock);
0df502fd
MD
844 return -EINVAL;
845 }
5b4a0ec0
DG
846 lta = zmalloc(sizeof(struct ust_app));
847 if (lta == NULL) {
848 PERROR("malloc");
849 return -ENOMEM;
850 }
851
852 lta->ppid = msg->ppid;
853 lta->uid = msg->uid;
854 lta->gid = msg->gid;
7753dea8 855 lta->bits_per_long = msg->bits_per_long;
5b4a0ec0
DG
856 lta->v_major = msg->major;
857 lta->v_minor = msg->minor;
858 strncpy(lta->name, msg->name, sizeof(lta->name));
859 lta->name[16] = '\0';
860 lta->sessions = hashtable_new(0);
861
862 /* Set key map */
863 lta->key.pid = msg->pid;
864 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
865 sizeof(void *));
866 lta->key.sock = sock;
867 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
868 sizeof(void *));
869
870 rcu_read_lock();
871 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
872 hashtable_add_unique(ust_app_ht, &lta->node);
873 rcu_read_unlock();
874
875 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
876 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
877 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
878
879 return 0;
880}
881
882/*
883 * Unregister app by removing it from the global traceable app list and freeing
884 * the data struct.
885 *
886 * The socket is already closed at this point so no close to sock.
887 */
888void ust_app_unregister(int sock)
889{
890 struct ust_app *lta;
891 struct cds_lfht_node *node;
892 struct cds_lfht_iter iter;
893
894 rcu_read_lock();
895 lta = find_app_by_sock(sock);
896 if (lta == NULL) {
897 ERR("Unregister app sock %d not found!", sock);
898 goto error;
899 }
900
901 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
902
903 /* Get the node reference for a call_rcu */
904 node = hashtable_lookup(ust_app_ht,
905 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
906 if (node == NULL) {
907 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
908 goto error;
909 }
284d8f55 910
5b4a0ec0
DG
911 hashtable_del(ust_app_ht, &iter);
912 call_rcu(&node->head, delete_ust_app_rcu);
284d8f55 913error:
5b4a0ec0
DG
914 rcu_read_unlock();
915 return;
284d8f55
DG
916}
917
918/*
5b4a0ec0 919 * Return traceable_app_count
284d8f55 920 */
5b4a0ec0 921unsigned long ust_app_list_count(void)
284d8f55 922{
5b4a0ec0 923 unsigned long count;
284d8f55 924
5b4a0ec0
DG
925 rcu_read_lock();
926 count = hashtable_get_count(ust_app_ht);
927 rcu_read_unlock();
284d8f55 928
5b4a0ec0 929 return count;
284d8f55
DG
930}
931
5b4a0ec0
DG
932/*
933 * Fill events array with all events name of all registered apps.
934 */
935int ust_app_list_events(struct lttng_event **events)
421cb601 936{
5b4a0ec0
DG
937 int ret, handle;
938 size_t nbmem, count = 0;
421cb601 939 struct cds_lfht_iter iter;
5b4a0ec0
DG
940 struct ust_app *app;
941 struct lttng_event *tmp;
421cb601 942
5b4a0ec0
DG
943 nbmem = UST_APP_EVENT_LIST_SIZE;
944 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
945 if (tmp == NULL) {
946 PERROR("zmalloc ust app events");
947 ret = -ENOMEM;
421cb601
DG
948 goto error;
949 }
950
5b4a0ec0 951 rcu_read_lock();
421cb601 952
5b4a0ec0
DG
953 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
954 handle = ustctl_tracepoint_list(app->key.sock);
955 if (handle < 0) {
956 ERR("UST app list events getting handle failed for app pid %d",
957 app->key.pid);
958 continue;
959 }
421cb601 960
5b4a0ec0
DG
961 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
962 tmp[count].name)) != -ENOENT) {
963 if (count > nbmem) {
964 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
965 nbmem + UST_APP_EVENT_LIST_SIZE);
966 nbmem += UST_APP_EVENT_LIST_SIZE;
967 tmp = realloc(tmp, nbmem);
968 if (tmp == NULL) {
969 PERROR("realloc ust app events");
970 ret = -ENOMEM;
971 goto rcu_error;
972 }
973 }
421cb601 974
5b4a0ec0
DG
975 tmp[count].type = LTTNG_UST_TRACEPOINT;
976 tmp[count].pid = app->key.pid;
977 tmp[count].enabled = -1;
978 count++;
421cb601 979 }
421cb601
DG
980 }
981
5b4a0ec0
DG
982 ret = count;
983 *events = tmp;
421cb601 984
5b4a0ec0 985 DBG2("UST app list events done (%zu events)", count);
421cb601 986
5b4a0ec0
DG
987rcu_error:
988 rcu_read_unlock();
421cb601 989error:
5b4a0ec0 990 return ret;
421cb601
DG
991}
992
5b4a0ec0
DG
993/*
994 * Free and clean all traceable apps of the global list.
995 */
996void ust_app_clean_list(void)
421cb601 997{
5b4a0ec0
DG
998 int ret;
999 struct cds_lfht_node *node;
1000 struct cds_lfht_iter iter;
1001 struct ust_app *app;
421cb601 1002
5b4a0ec0 1003 DBG2("UST app cleaning registered apps hash table");
421cb601 1004
5b4a0ec0 1005 rcu_read_lock();
421cb601 1006
5b4a0ec0
DG
1007 cds_lfht_for_each(ust_app_ht, &iter, node) {
1008 app = caa_container_of(node, struct ust_app, node);
421cb601 1009
5b4a0ec0
DG
1010 ret = hashtable_del(ust_app_ht, &iter);
1011 if (!ret) {
1012 call_rcu(&node->head, delete_ust_app_rcu);
421cb601 1013 }
421cb601
DG
1014 }
1015
5b4a0ec0
DG
1016 hashtable_destroy(ust_app_ht);
1017 hashtable_destroy(ust_app_sock_key_map);
421cb601 1018
5b4a0ec0
DG
1019 rcu_read_unlock();
1020}
1021
1022/*
1023 * Init UST app hash table.
1024 */
1025void ust_app_ht_alloc(void)
1026{
1027 ust_app_ht = hashtable_new(0);
1028 ust_app_sock_key_map = hashtable_new(0);
421cb601
DG
1029}
1030
1031/*
5b4a0ec0 1032 * For a specific UST session, create the channel for all registered apps.
421cb601 1033 */
5b4a0ec0 1034int ust_app_create_channel_all(struct ltt_ust_session *usess,
48842b30
DG
1035 struct ltt_ust_channel *uchan)
1036{
1037 int ret = 0;
1038 struct cds_lfht_iter iter;
48842b30
DG
1039 struct ust_app *app;
1040 struct ust_app_session *ua_sess;
1041 struct ust_app_channel *ua_chan;
1042
421cb601
DG
1043 if (usess == NULL || uchan == NULL) {
1044 ERR("Adding UST global channel to NULL values");
1045 ret = -1;
1046 goto error;
1047 }
1048
48842b30
DG
1049 DBG2("UST app adding channel %s to global domain for session uid %d",
1050 uchan->name, usess->uid);
1051
1052 rcu_read_lock();
421cb601 1053
5b4a0ec0
DG
1054 /* For every registered applications */
1055 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
421cb601
DG
1056 /* Create session on the tracer side and add it to app session HT */
1057 ua_sess = create_ust_app_session(usess, app);
509cbaf8 1058 if (ua_sess == NULL) {
5b4a0ec0 1059 continue;
48842b30
DG
1060 }
1061
421cb601
DG
1062 /* Create channel onto application */
1063 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1064 if (ua_chan == NULL) {
5b4a0ec0 1065 continue;
48842b30 1066 }
48842b30 1067 }
5b4a0ec0 1068
48842b30
DG
1069 rcu_read_unlock();
1070
421cb601 1071error:
48842b30
DG
1072 return ret;
1073}
1074
5b4a0ec0
DG
1075/*
1076 * For a specific UST session and UST channel, create the event for all
1077 * registered apps.
1078 */
1079int ust_app_create_event_all(struct ltt_ust_session *usess,
48842b30
DG
1080 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1081{
1082 int ret = 0;
1083 struct cds_lfht_iter iter;
5b4a0ec0 1084 struct cds_lfht_node *ua_chan_node;
48842b30
DG
1085 struct ust_app *app;
1086 struct ust_app_session *ua_sess;
1087 struct ust_app_channel *ua_chan;
1088 struct ust_app_event *ua_event;
48842b30 1089
284d8f55 1090 DBG("UST app creating event %s for all apps for session uid %d",
48842b30
DG
1091 uevent->attr.name, usess->uid);
1092
1093 rcu_read_lock();
421cb601
DG
1094
1095 /* For all registered applications */
5b4a0ec0 1096 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
ba767faf
MD
1097 struct cds_lfht_iter uiter;
1098
421cb601
DG
1099 /* Create session on the tracer side and add it to app session HT */
1100 ua_sess = create_ust_app_session(usess, app);
509cbaf8 1101 if (ua_sess == NULL) {
5b4a0ec0 1102 continue;
48842b30
DG
1103 }
1104
1105 /* Lookup channel in the ust app session */
1106 ua_chan_node = hashtable_lookup(ua_sess->channels,
ba767faf
MD
1107 (void *)uchan->name, strlen(uchan->name),
1108 &uiter);
48842b30 1109 if (ua_chan_node == NULL) {
421cb601
DG
1110 ERR("Channel %s not found in session uid %d. Skipping",
1111 uchan->name, usess->uid);
5b4a0ec0 1112 continue;
48842b30 1113 }
48842b30
DG
1114 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1115
421cb601
DG
1116 ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1117 if (ua_event == NULL) {
5b4a0ec0 1118 continue;
48842b30 1119 }
48842b30 1120 }
5b4a0ec0 1121
48842b30
DG
1122 rcu_read_unlock();
1123
1124 return ret;
1125}
1126
5b4a0ec0
DG
1127/*
1128 * Start tracing for a specific UST session and app.
1129 */
421cb601 1130int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
1131{
1132 int ret = 0;
1133 struct cds_lfht_iter iter;
48842b30
DG
1134 struct ust_app_session *ua_sess;
1135 struct ust_app_channel *ua_chan;
5b4a0ec0 1136 struct ltt_ust_stream *ustream;
7753dea8 1137 int consumerd_fd;
48842b30 1138
421cb601 1139 DBG("Starting tracing for ust app pid %d", app->key.pid);
5cf5d0e7 1140
509cbaf8
MD
1141 rcu_read_lock();
1142
421cb601
DG
1143 ua_sess = lookup_session_by_app(usess, app);
1144 if (ua_sess == NULL) {
1145 /* Only malloc can failed so something is really wrong */
509cbaf8 1146 goto error_rcu_unlock;
421cb601 1147 }
48842b30 1148
8be98f9a
MD
1149 /* upon restart, we skip the setup, already done */
1150 if (ua_sess->started)
1151 goto skip_setup;
1152
421cb601
DG
1153 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1154 if (ret < 0) {
509cbaf8 1155 goto error_rcu_unlock;
421cb601 1156 }
48842b30 1157
421cb601 1158 /* For each channel */
5b4a0ec0 1159 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
421cb601
DG
1160 /* Create all streams */
1161 while (1) {
5b4a0ec0 1162 /* Create UST stream */
421cb601
DG
1163 ustream = zmalloc(sizeof(*ustream));
1164 if (ustream == NULL) {
1165 PERROR("zmalloc ust stream");
509cbaf8 1166 goto error_rcu_unlock;
421cb601 1167 }
48842b30 1168
421cb601
DG
1169 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1170 &ustream->obj);
48842b30 1171 if (ret < 0) {
421cb601
DG
1172 /* Got all streams */
1173 break;
48842b30 1174 }
421cb601 1175 ustream->handle = ustream->obj->handle;
48842b30 1176
421cb601
DG
1177 /* Order is important */
1178 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
477d7741
MD
1179 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1180 ua_sess->path, ua_chan->name,
1181 ua_chan->streams.count++);
aba8e916
DG
1182 if (ret < 0) {
1183 PERROR("asprintf UST create stream");
421cb601 1184 continue;
aba8e916 1185 }
421cb601
DG
1186 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1187 ustream->pathname);
1188 }
421cb601 1189 }
aba8e916 1190
7753dea8
MD
1191 switch (app->bits_per_long) {
1192 case 64:
1193 consumerd_fd = ust_consumerd64_fd;
1194 break;
1195 case 32:
1196 consumerd_fd = ust_consumerd32_fd;
1197 break;
1198 default:
1199 ret = -EINVAL;
1200 goto error_rcu_unlock;
1201 }
421cb601 1202 /* Setup UST consumer socket and send fds to it */
7753dea8 1203 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
421cb601 1204 if (ret < 0) {
509cbaf8 1205 goto error_rcu_unlock;
421cb601 1206 }
8be98f9a 1207 ua_sess->started = 1;
48842b30 1208
8be98f9a 1209skip_setup:
421cb601
DG
1210 /* This start the UST tracing */
1211 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1212 if (ret < 0) {
1213 ERR("Error starting tracing for app pid: %d", app->key.pid);
509cbaf8 1214 goto error_rcu_unlock;
421cb601 1215 }
5b4a0ec0 1216
509cbaf8 1217 rcu_read_unlock();
48842b30 1218
421cb601
DG
1219 /* Quiescent wait after starting trace */
1220 ustctl_wait_quiescent(app->key.sock);
48842b30 1221
421cb601 1222 return 0;
48842b30 1223
509cbaf8
MD
1224error_rcu_unlock:
1225 rcu_read_unlock();
421cb601
DG
1226 return -1;
1227}
48842b30 1228
8be98f9a
MD
1229/*
1230 * Stop tracing for a specific UST session and app.
1231 */
1232int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1233{
1234 int ret = 0;
1235 struct ust_app_session *ua_sess;
1236
1237 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1238
1239 rcu_read_lock();
1240
1241 ua_sess = lookup_session_by_app(usess, app);
1242 if (ua_sess == NULL) {
1243 /* Only malloc can failed so something is really wrong */
1244 goto error_rcu_unlock;
1245 }
1246
1247#if 0 /* only useful when periodical flush will be supported */
1248 /* need to keep a handle on shm in session for this. */
1249 /* Flush all buffers before stopping */
1250 ret = ustctl_flush_buffer(usess->sock, usess->metadata->obj);
1251 if (ret < 0) {
1252 ERR("UST metadata flush failed");
1253 }
1254
1255 cds_list_for_each_entry(ustchan, &usess->channels.head, list) {
1256 ret = ustctl_flush_buffer(usess->sock, ustchan->obj);
1257 if (ret < 0) {
1258 ERR("UST flush buffer error");
1259 }
1260 }
1261#endif
1262
1263 /* This inhibits UST tracing */
1264 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1265 if (ret < 0) {
1266 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1267 goto error_rcu_unlock;
1268 }
1269
1270 rcu_read_unlock();
1271
1272 /* Quiescent wait after stopping trace */
1273 ustctl_wait_quiescent(app->key.sock);
1274
1275 return 0;
1276
1277error_rcu_unlock:
1278 rcu_read_unlock();
1279 return -1;
1280}
1281
84cd17c6
MD
1282/*
1283 * Destroy a specific UST session in apps.
1284 */
1285int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1286{
1287 struct ust_app_session *ua_sess;
1288 struct lttng_ust_object_data obj;
1289 struct cds_lfht_iter iter;
1290 struct cds_lfht_node *node;
1291
1292 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1293
1294 rcu_read_lock();
1295
1296 __lookup_session_by_app(usess, app, &iter);
1297 node = hashtable_iter_get_node(&iter);
1298 if (node == NULL) {
1299 /* Only malloc can failed so something is really wrong */
1300 goto error_rcu_unlock;
1301 }
1302 ua_sess = caa_container_of(node, struct ust_app_session, node);
1303 hashtable_del(app->sessions, &iter);
1304 delete_ust_app_session(app->key.sock, ua_sess);
1305 obj.handle = ua_sess->handle;
1306 obj.shm_fd = -1;
1307 obj.wait_fd = -1;
1308 obj.memory_map_size = 0;
1309 ustctl_release_object(app->key.sock, &obj);
1310
1311 rcu_read_unlock();
1312
1313 /* Quiescent wait after stopping trace */
1314 ustctl_wait_quiescent(app->key.sock);
1315
1316 return 0;
1317
1318error_rcu_unlock:
1319 rcu_read_unlock();
1320 return -1;
1321}
1322
5b4a0ec0
DG
1323/*
1324 * Start tracing for the UST session.
1325 */
421cb601
DG
1326int ust_app_start_trace_all(struct ltt_ust_session *usess)
1327{
1328 int ret = 0;
1329 struct cds_lfht_iter iter;
421cb601 1330 struct ust_app *app;
48842b30 1331
421cb601
DG
1332 DBG("Starting all UST traces");
1333
1334 rcu_read_lock();
421cb601 1335
5b4a0ec0 1336 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
421cb601 1337 ret = ust_app_start_trace(usess, app);
48842b30 1338 if (ret < 0) {
5b4a0ec0
DG
1339 /* Continue to next apps even on error */
1340 continue;
48842b30 1341 }
48842b30 1342 }
5b4a0ec0 1343
48842b30
DG
1344 rcu_read_unlock();
1345
1346 return 0;
1347}
487cf67c 1348
8be98f9a
MD
1349/*
1350 * Start tracing for the UST session.
1351 */
1352int ust_app_stop_trace_all(struct ltt_ust_session *usess)
1353{
1354 int ret = 0;
1355 struct cds_lfht_iter iter;
1356 struct ust_app *app;
1357
1358 DBG("Stopping all UST traces");
1359
1360 rcu_read_lock();
1361
1362 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1363 ret = ust_app_stop_trace(usess, app);
1364 if (ret < 0) {
1365 /* Continue to next apps even on error */
1366 continue;
1367 }
1368 }
1369
1370 rcu_read_unlock();
1371
1372 return 0;
1373}
1374
84cd17c6
MD
1375/*
1376 * Destroy app UST session.
1377 */
1378int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
1379{
1380 int ret = 0;
1381 struct cds_lfht_iter iter;
1382 struct ust_app *app;
1383
1384 DBG("Destroy all UST traces");
1385
1386 rcu_read_lock();
1387
1388 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1389 ret = ust_app_destroy_trace(usess, app);
1390 if (ret < 0) {
1391 /* Continue to next apps even on error */
1392 continue;
1393 }
1394 }
1395
1396 rcu_read_unlock();
1397
1398 return 0;
1399}
1400
5b4a0ec0
DG
1401/*
1402 * Add channels/events from UST global domain to registered apps at sock.
1403 */
487cf67c
DG
1404void ust_app_global_update(struct ltt_ust_session *usess, int sock)
1405{
1406 int ret = 0;
487cf67c 1407 struct cds_lfht_iter iter;
487cf67c
DG
1408 struct ust_app *app;
1409 struct ust_app_session *ua_sess;
1410 struct ust_app_channel *ua_chan;
1411 struct ust_app_event *ua_event;
1f3580c7
DG
1412
1413 if (usess == NULL) {
5b4a0ec0 1414 ERR("No UST session on global update. Returning");
1f3580c7
DG
1415 goto error;
1416 }
1417
487cf67c
DG
1418 DBG2("UST app global update for app sock %d for session uid %d", sock,
1419 usess->uid);
1420
284d8f55
DG
1421 rcu_read_lock();
1422
487cf67c
DG
1423 app = find_app_by_sock(sock);
1424 if (app == NULL) {
1425 ERR("Failed to update app sock %d", sock);
1426 goto error;
1427 }
1428
421cb601 1429 ua_sess = create_ust_app_session(usess, app);
487cf67c 1430 if (ua_sess == NULL) {
487cf67c
DG
1431 goto error;
1432 }
1433
284d8f55
DG
1434 /*
1435 * We can iterate safely here over all UST app session sicne the create ust
1436 * app session above made a shadow copy of the UST global domain from the
1437 * ltt ust session.
1438 */
1439 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1440 ret = create_ust_channel(app, ua_sess, ua_chan);
1441 if (ret < 0) {
1442 /* FIXME: Should we quit here or continue... */
1443 continue;
487cf67c
DG
1444 }
1445
284d8f55
DG
1446 /* For each events */
1447 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
1448 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1449 if (ret < 0) {
1450 /* FIXME: Should we quit here or continue... */
1451 continue;
487cf67c 1452 }
36dc12cc 1453 }
487cf67c
DG
1454 }
1455
36dc12cc 1456 if (usess->start_trace) {
421cb601 1457 ret = ust_app_start_trace(usess, app);
36dc12cc 1458 if (ret < 0) {
36dc12cc
DG
1459 goto error;
1460 }
1461
36dc12cc
DG
1462 DBG2("UST trace started for app pid %d", app->key.pid);
1463 }
1464
487cf67c
DG
1465error:
1466 rcu_read_unlock();
1467 return;
1468}
This page took 0.095853 seconds and 4 git commands to generate.