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