Commit | Line | Data |
---|---|---|
91d76f53 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
91d76f53 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
91d76f53 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <errno.h> | |
20 | #include <pthread.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
099e26bd | 23 | #include <string.h> |
aba8e916 DG |
24 | #include <sys/stat.h> |
25 | #include <sys/types.h> | |
099e26bd | 26 | #include <unistd.h> |
0df502fd | 27 | #include <urcu/compiler.h> |
bec39940 | 28 | |
990570ed | 29 | #include <common/common.h> |
86acf0da | 30 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 31 | |
86acf0da DG |
32 | #include "fd-limit.h" |
33 | #include "health.h" | |
56fff090 | 34 | #include "ust-app.h" |
48842b30 | 35 | #include "ust-consumer.h" |
d80a6244 DG |
36 | #include "ust-ctl.h" |
37 | ||
55cc08a6 DG |
38 | /* |
39 | * Delete ust context safely. RCU read lock must be held before calling | |
40 | * this function. | |
41 | */ | |
42 | static | |
43 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx) | |
44 | { | |
45 | if (ua_ctx->obj) { | |
46 | ustctl_release_object(sock, ua_ctx->obj); | |
47 | free(ua_ctx->obj); | |
48 | } | |
49 | free(ua_ctx); | |
50 | } | |
51 | ||
d80a6244 DG |
52 | /* |
53 | * Delete ust app event safely. RCU read lock must be held before calling | |
54 | * this function. | |
55 | */ | |
8b366481 DG |
56 | static |
57 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event) | |
d80a6244 | 58 | { |
55cc08a6 | 59 | int ret; |
bec39940 | 60 | struct lttng_ht_iter iter; |
55cc08a6 DG |
61 | struct ust_app_ctx *ua_ctx; |
62 | ||
fc34caaa | 63 | /* Destroy each context of event */ |
bec39940 DG |
64 | cds_lfht_for_each_entry(ua_event->ctx->ht, &iter.iter, ua_ctx, |
65 | node.node) { | |
66 | ret = lttng_ht_del(ua_event->ctx, &iter); | |
55cc08a6 DG |
67 | assert(!ret); |
68 | delete_ust_app_ctx(sock, ua_ctx); | |
69 | } | |
53a80697 | 70 | free(ua_event->filter); |
bec39940 | 71 | lttng_ht_destroy(ua_event->ctx); |
d80a6244 | 72 | |
edb67388 DG |
73 | if (ua_event->obj != NULL) { |
74 | ustctl_release_object(sock, ua_event->obj); | |
75 | free(ua_event->obj); | |
76 | } | |
d80a6244 DG |
77 | free(ua_event); |
78 | } | |
79 | ||
80 | /* | |
81 | * Delete ust app stream safely. RCU read lock must be held before calling | |
82 | * this function. | |
83 | */ | |
8b366481 DG |
84 | static |
85 | void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream) | |
d80a6244 | 86 | { |
8b366481 DG |
87 | if (stream->obj) { |
88 | ustctl_release_object(sock, stream->obj); | |
4063050c | 89 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
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); | |
4063050c | 133 | lttng_fd_put(LTTNG_FD_APPS, 2); |
edb67388 DG |
134 | free(ua_chan->obj); |
135 | } | |
84cd17c6 | 136 | free(ua_chan); |
d80a6244 DG |
137 | } |
138 | ||
139 | /* | |
140 | * Delete ust app session safely. RCU read lock must be held before calling | |
141 | * this function. | |
142 | */ | |
8b366481 DG |
143 | static |
144 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess) | |
d80a6244 DG |
145 | { |
146 | int ret; | |
bec39940 | 147 | struct lttng_ht_iter iter; |
d80a6244 DG |
148 | struct ust_app_channel *ua_chan; |
149 | ||
150 | if (ua_sess->metadata) { | |
8b366481 DG |
151 | if (ua_sess->metadata->stream_obj) { |
152 | ustctl_release_object(sock, ua_sess->metadata->stream_obj); | |
4063050c | 153 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
154 | free(ua_sess->metadata->stream_obj); |
155 | } | |
156 | if (ua_sess->metadata->obj) { | |
157 | ustctl_release_object(sock, ua_sess->metadata->obj); | |
4063050c | 158 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
159 | free(ua_sess->metadata->obj); |
160 | } | |
a2c0da86 | 161 | trace_ust_destroy_metadata(ua_sess->metadata); |
d80a6244 DG |
162 | } |
163 | ||
bec39940 DG |
164 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
165 | node.node) { | |
166 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
525b0740 | 167 | assert(!ret); |
d80a6244 DG |
168 | delete_ust_app_channel(sock, ua_chan); |
169 | } | |
bec39940 | 170 | lttng_ht_destroy(ua_sess->channels); |
d80a6244 | 171 | |
aee6bafd MD |
172 | if (ua_sess->handle != -1) { |
173 | ustctl_release_handle(sock, ua_sess->handle); | |
174 | } | |
8b366481 | 175 | free(ua_sess); |
d80a6244 | 176 | } |
91d76f53 DG |
177 | |
178 | /* | |
284d8f55 DG |
179 | * Delete a traceable application structure from the global list. Never call |
180 | * this function outside of a call_rcu call. | |
91d76f53 | 181 | */ |
8b366481 DG |
182 | static |
183 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 184 | { |
8b366481 | 185 | int ret, sock; |
bec39940 | 186 | struct lttng_ht_iter iter; |
284d8f55 | 187 | struct ust_app_session *ua_sess; |
44d3bd01 | 188 | |
f6a9efaa | 189 | rcu_read_lock(); |
44d3bd01 | 190 | |
d80a6244 | 191 | /* Delete ust app sessions info */ |
852d0037 DG |
192 | sock = app->sock; |
193 | app->sock = -1; | |
d80a6244 | 194 | |
8b366481 | 195 | /* Wipe sessions */ |
bec39940 DG |
196 | cds_lfht_for_each_entry(app->sessions->ht, &iter.iter, ua_sess, |
197 | node.node) { | |
198 | ret = lttng_ht_del(app->sessions, &iter); | |
525b0740 | 199 | assert(!ret); |
852d0037 | 200 | delete_ust_app_session(app->sock, ua_sess); |
d80a6244 | 201 | } |
bec39940 | 202 | lttng_ht_destroy(app->sessions); |
d80a6244 | 203 | |
6414a713 | 204 | /* |
852d0037 DG |
205 | * Wait until we have deleted the application from the sock hash table |
206 | * before closing this socket, otherwise an application could re-use the | |
207 | * socket ID and race with the teardown, using the same hash table entry. | |
208 | * | |
209 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
210 | * all RCU readers that could run concurrently with unregister app, | |
211 | * therefore we _need_ to only close that socket after a grace period. So | |
212 | * it should stay in this RCU callback. | |
213 | * | |
214 | * This close() is a very important step of the synchronization model so | |
215 | * every modification to this function must be carefully reviewed. | |
6414a713 | 216 | */ |
799e2c4f MD |
217 | ret = close(sock); |
218 | if (ret) { | |
219 | PERROR("close"); | |
220 | } | |
4063050c | 221 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 222 | |
852d0037 | 223 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 224 | free(app); |
bec39940 | 225 | |
f6a9efaa | 226 | rcu_read_unlock(); |
099e26bd DG |
227 | } |
228 | ||
229 | /* | |
f6a9efaa | 230 | * URCU intermediate call to delete an UST app. |
099e26bd | 231 | */ |
8b366481 DG |
232 | static |
233 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 234 | { |
bec39940 DG |
235 | struct lttng_ht_node_ulong *node = |
236 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa | 237 | struct ust_app *app = |
852d0037 | 238 | caa_container_of(node, struct ust_app, pid_n); |
f6a9efaa | 239 | |
852d0037 | 240 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 241 | delete_ust_app(app); |
099e26bd DG |
242 | } |
243 | ||
8b366481 DG |
244 | /* |
245 | * Alloc new UST app session. | |
246 | */ | |
247 | static | |
248 | struct ust_app_session *alloc_ust_app_session(void) | |
249 | { | |
250 | struct ust_app_session *ua_sess; | |
251 | ||
252 | /* Init most of the default value by allocating and zeroing */ | |
253 | ua_sess = zmalloc(sizeof(struct ust_app_session)); | |
254 | if (ua_sess == NULL) { | |
255 | PERROR("malloc"); | |
256 | goto error; | |
257 | } | |
258 | ||
259 | ua_sess->handle = -1; | |
bec39940 | 260 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
8b366481 DG |
261 | |
262 | return ua_sess; | |
263 | ||
264 | error: | |
265 | return NULL; | |
266 | } | |
267 | ||
268 | /* | |
269 | * Alloc new UST app channel. | |
270 | */ | |
271 | static | |
272 | struct ust_app_channel *alloc_ust_app_channel(char *name, | |
273 | struct lttng_ust_channel *attr) | |
274 | { | |
275 | struct ust_app_channel *ua_chan; | |
276 | ||
277 | /* Init most of the default value by allocating and zeroing */ | |
278 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); | |
279 | if (ua_chan == NULL) { | |
280 | PERROR("malloc"); | |
281 | goto error; | |
282 | } | |
283 | ||
284 | /* Setup channel name */ | |
285 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
286 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
287 | ||
288 | ua_chan->enabled = 1; | |
289 | ua_chan->handle = -1; | |
bec39940 DG |
290 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
291 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
292 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
293 | |
294 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
295 | ||
296 | /* Copy attributes */ | |
297 | if (attr) { | |
298 | memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr)); | |
299 | } | |
300 | ||
301 | DBG3("UST app channel %s allocated", ua_chan->name); | |
302 | ||
303 | return ua_chan; | |
304 | ||
305 | error: | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | /* | |
310 | * Alloc new UST app event. | |
311 | */ | |
312 | static | |
313 | struct ust_app_event *alloc_ust_app_event(char *name, | |
314 | struct lttng_ust_event *attr) | |
315 | { | |
316 | struct ust_app_event *ua_event; | |
317 | ||
318 | /* Init most of the default value by allocating and zeroing */ | |
319 | ua_event = zmalloc(sizeof(struct ust_app_event)); | |
320 | if (ua_event == NULL) { | |
321 | PERROR("malloc"); | |
322 | goto error; | |
323 | } | |
324 | ||
325 | ua_event->enabled = 1; | |
326 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
327 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 DG |
328 | ua_event->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
329 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); | |
8b366481 DG |
330 | |
331 | /* Copy attributes */ | |
332 | if (attr) { | |
333 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
334 | } | |
335 | ||
336 | DBG3("UST app event %s allocated", ua_event->name); | |
337 | ||
338 | return ua_event; | |
339 | ||
340 | error: | |
341 | return NULL; | |
342 | } | |
343 | ||
344 | /* | |
345 | * Alloc new UST app context. | |
346 | */ | |
347 | static | |
348 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx) | |
349 | { | |
350 | struct ust_app_ctx *ua_ctx; | |
351 | ||
352 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); | |
353 | if (ua_ctx == NULL) { | |
354 | goto error; | |
355 | } | |
356 | ||
357 | if (uctx) { | |
358 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
359 | } | |
360 | ||
361 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
362 | ||
363 | error: | |
364 | return ua_ctx; | |
365 | } | |
366 | ||
099e26bd | 367 | /* |
421cb601 DG |
368 | * Find an ust_app using the sock and return it. RCU read side lock must be |
369 | * held before calling this helper function. | |
099e26bd | 370 | */ |
8b366481 DG |
371 | static |
372 | struct ust_app *find_app_by_sock(int sock) | |
099e26bd | 373 | { |
bec39940 | 374 | struct lttng_ht_node_ulong *node; |
bec39940 | 375 | struct lttng_ht_iter iter; |
f6a9efaa | 376 | |
852d0037 | 377 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 378 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
379 | if (node == NULL) { |
380 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
381 | goto error; |
382 | } | |
852d0037 DG |
383 | |
384 | return caa_container_of(node, struct ust_app, sock_n); | |
f6a9efaa DG |
385 | |
386 | error: | |
387 | return NULL; | |
099e26bd DG |
388 | } |
389 | ||
55cc08a6 DG |
390 | /* |
391 | * Create the channel context on the tracer. | |
392 | */ | |
393 | static | |
394 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
395 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
396 | { | |
397 | int ret; | |
398 | ||
86acf0da DG |
399 | health_code_update(&health_thread_cmd); |
400 | ||
852d0037 | 401 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 DG |
402 | ua_chan->obj, &ua_ctx->obj); |
403 | if (ret < 0) { | |
404 | goto error; | |
405 | } | |
406 | ||
407 | ua_ctx->handle = ua_ctx->obj->handle; | |
408 | ||
727d5404 | 409 | DBG2("UST app context created successfully for channel %s", ua_chan->name); |
55cc08a6 DG |
410 | |
411 | error: | |
86acf0da | 412 | health_code_update(&health_thread_cmd); |
55cc08a6 DG |
413 | return ret; |
414 | } | |
415 | ||
416 | /* | |
417 | * Create the event context on the tracer. | |
418 | */ | |
419 | static | |
420 | int create_ust_event_context(struct ust_app_event *ua_event, | |
421 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
422 | { | |
423 | int ret; | |
424 | ||
86acf0da DG |
425 | health_code_update(&health_thread_cmd); |
426 | ||
852d0037 | 427 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 DG |
428 | ua_event->obj, &ua_ctx->obj); |
429 | if (ret < 0) { | |
430 | goto error; | |
431 | } | |
432 | ||
433 | ua_ctx->handle = ua_ctx->obj->handle; | |
434 | ||
727d5404 | 435 | DBG2("UST app context created successfully for event %s", ua_event->name); |
55cc08a6 DG |
436 | |
437 | error: | |
86acf0da | 438 | health_code_update(&health_thread_cmd); |
55cc08a6 DG |
439 | return ret; |
440 | } | |
441 | ||
53a80697 MD |
442 | /* |
443 | * Set the filter on the tracer. | |
444 | */ | |
445 | static | |
446 | int set_ust_event_filter(struct ust_app_event *ua_event, | |
447 | struct ust_app *app) | |
448 | { | |
449 | int ret; | |
450 | ||
86acf0da DG |
451 | health_code_update(&health_thread_cmd); |
452 | ||
53a80697 | 453 | if (!ua_event->filter) { |
86acf0da DG |
454 | ret = 0; |
455 | goto error; | |
53a80697 MD |
456 | } |
457 | ||
458 | ret = ustctl_set_filter(app->sock, ua_event->filter, | |
459 | ua_event->obj); | |
460 | if (ret < 0) { | |
461 | goto error; | |
462 | } | |
463 | ||
464 | DBG2("UST filter set successfully for event %s", ua_event->name); | |
465 | ||
466 | error: | |
86acf0da | 467 | health_code_update(&health_thread_cmd); |
53a80697 MD |
468 | return ret; |
469 | } | |
470 | ||
9730260e DG |
471 | /* |
472 | * Disable the specified event on to UST tracer for the UST session. | |
473 | */ | |
474 | static int disable_ust_event(struct ust_app *app, | |
475 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
476 | { | |
477 | int ret; | |
478 | ||
86acf0da DG |
479 | health_code_update(&health_thread_cmd); |
480 | ||
852d0037 | 481 | ret = ustctl_disable(app->sock, ua_event->obj); |
9730260e DG |
482 | if (ret < 0) { |
483 | ERR("UST app event %s disable failed for app (pid: %d) " | |
484 | "and session handle %d with ret %d", | |
852d0037 | 485 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
9730260e DG |
486 | goto error; |
487 | } | |
488 | ||
489 | DBG2("UST app event %s disabled successfully for app (pid: %d)", | |
852d0037 | 490 | ua_event->attr.name, app->pid); |
9730260e DG |
491 | |
492 | error: | |
86acf0da | 493 | health_code_update(&health_thread_cmd); |
9730260e DG |
494 | return ret; |
495 | } | |
496 | ||
78f0bacd DG |
497 | /* |
498 | * Disable the specified channel on to UST tracer for the UST session. | |
499 | */ | |
500 | static int disable_ust_channel(struct ust_app *app, | |
501 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
502 | { | |
503 | int ret; | |
504 | ||
86acf0da DG |
505 | health_code_update(&health_thread_cmd); |
506 | ||
852d0037 | 507 | ret = ustctl_disable(app->sock, ua_chan->obj); |
78f0bacd DG |
508 | if (ret < 0) { |
509 | ERR("UST app channel %s disable failed for app (pid: %d) " | |
510 | "and session handle %d with ret %d", | |
852d0037 | 511 | ua_chan->name, app->pid, ua_sess->handle, ret); |
78f0bacd DG |
512 | goto error; |
513 | } | |
514 | ||
78f0bacd | 515 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
852d0037 | 516 | ua_chan->name, app->pid); |
78f0bacd DG |
517 | |
518 | error: | |
86acf0da | 519 | health_code_update(&health_thread_cmd); |
78f0bacd DG |
520 | return ret; |
521 | } | |
522 | ||
523 | /* | |
524 | * Enable the specified channel on to UST tracer for the UST session. | |
525 | */ | |
526 | static int enable_ust_channel(struct ust_app *app, | |
527 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
528 | { | |
529 | int ret; | |
530 | ||
86acf0da DG |
531 | health_code_update(&health_thread_cmd); |
532 | ||
852d0037 | 533 | ret = ustctl_enable(app->sock, ua_chan->obj); |
78f0bacd DG |
534 | if (ret < 0) { |
535 | ERR("UST app channel %s enable failed for app (pid: %d) " | |
536 | "and session handle %d with ret %d", | |
852d0037 | 537 | ua_chan->name, app->pid, ua_sess->handle, ret); |
78f0bacd DG |
538 | goto error; |
539 | } | |
540 | ||
541 | ua_chan->enabled = 1; | |
542 | ||
543 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", | |
852d0037 | 544 | ua_chan->name, app->pid); |
78f0bacd DG |
545 | |
546 | error: | |
86acf0da | 547 | health_code_update(&health_thread_cmd); |
78f0bacd DG |
548 | return ret; |
549 | } | |
550 | ||
edb67388 DG |
551 | /* |
552 | * Enable the specified event on to UST tracer for the UST session. | |
553 | */ | |
554 | static int enable_ust_event(struct ust_app *app, | |
555 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
556 | { | |
557 | int ret; | |
558 | ||
86acf0da DG |
559 | health_code_update(&health_thread_cmd); |
560 | ||
852d0037 | 561 | ret = ustctl_enable(app->sock, ua_event->obj); |
edb67388 DG |
562 | if (ret < 0) { |
563 | ERR("UST app event %s enable failed for app (pid: %d) " | |
564 | "and session handle %d with ret %d", | |
852d0037 | 565 | ua_event->attr.name, app->pid, ua_sess->handle, ret); |
edb67388 DG |
566 | goto error; |
567 | } | |
568 | ||
569 | DBG2("UST app event %s enabled successfully for app (pid: %d)", | |
852d0037 | 570 | ua_event->attr.name, app->pid); |
edb67388 DG |
571 | |
572 | error: | |
86acf0da | 573 | health_code_update(&health_thread_cmd); |
edb67388 DG |
574 | return ret; |
575 | } | |
576 | ||
099e26bd | 577 | /* |
5b4a0ec0 | 578 | * Open metadata onto the UST tracer for a UST session. |
0177d773 | 579 | */ |
5b4a0ec0 DG |
580 | static int open_ust_metadata(struct ust_app *app, |
581 | struct ust_app_session *ua_sess) | |
0177d773 | 582 | { |
5b4a0ec0 DG |
583 | int ret; |
584 | struct lttng_ust_channel_attr uattr; | |
0177d773 | 585 | |
86acf0da DG |
586 | health_code_update(&health_thread_cmd); |
587 | ||
5b4a0ec0 DG |
588 | uattr.overwrite = ua_sess->metadata->attr.overwrite; |
589 | uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size; | |
590 | uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf; | |
591 | uattr.switch_timer_interval = | |
592 | ua_sess->metadata->attr.switch_timer_interval; | |
593 | uattr.read_timer_interval = | |
594 | ua_sess->metadata->attr.read_timer_interval; | |
595 | uattr.output = ua_sess->metadata->attr.output; | |
596 | ||
4063050c MD |
597 | /* We are going to receive 2 fds, we need to reserve them. */ |
598 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
599 | if (ret < 0) { | |
600 | ERR("Exhausted number of available FD upon metadata open"); | |
601 | goto error; | |
602 | } | |
5b4a0ec0 | 603 | /* UST tracer metadata creation */ |
852d0037 | 604 | ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr, |
5b4a0ec0 DG |
605 | &ua_sess->metadata->obj); |
606 | if (ret < 0) { | |
fc34caaa | 607 | ERR("UST app open metadata failed for app pid:%d with ret %d", |
852d0037 | 608 | app->pid, ret); |
f6a9efaa | 609 | goto error; |
0177d773 | 610 | } |
f6a9efaa | 611 | |
6d3686da DG |
612 | ua_sess->metadata->handle = ua_sess->metadata->obj->handle; |
613 | ||
f6a9efaa | 614 | error: |
86acf0da | 615 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 616 | return ret; |
91d76f53 DG |
617 | } |
618 | ||
619 | /* | |
5b4a0ec0 | 620 | * Create stream onto the UST tracer for a UST session. |
91d76f53 | 621 | */ |
5b4a0ec0 DG |
622 | static int create_ust_stream(struct ust_app *app, |
623 | struct ust_app_session *ua_sess) | |
91d76f53 | 624 | { |
5b4a0ec0 | 625 | int ret; |
421cb601 | 626 | |
86acf0da DG |
627 | health_code_update(&health_thread_cmd); |
628 | ||
4063050c MD |
629 | /* We are going to receive 2 fds, we need to reserve them. */ |
630 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
631 | if (ret < 0) { | |
632 | ERR("Exhausted number of available FD upon metadata stream create"); | |
633 | goto error; | |
634 | } | |
852d0037 | 635 | ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj, |
5b4a0ec0 DG |
636 | &ua_sess->metadata->stream_obj); |
637 | if (ret < 0) { | |
638 | ERR("UST create metadata stream failed"); | |
421cb601 | 639 | goto error; |
91d76f53 | 640 | } |
421cb601 | 641 | |
421cb601 | 642 | error: |
86acf0da | 643 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 644 | return ret; |
91d76f53 DG |
645 | } |
646 | ||
b551a063 | 647 | /* |
5b4a0ec0 | 648 | * Create the specified channel onto the UST tracer for a UST session. |
b551a063 | 649 | */ |
5b4a0ec0 DG |
650 | static int create_ust_channel(struct ust_app *app, |
651 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
b551a063 | 652 | { |
5b4a0ec0 | 653 | int ret; |
b551a063 | 654 | |
86acf0da DG |
655 | health_code_update(&health_thread_cmd); |
656 | ||
5b4a0ec0 | 657 | /* TODO: remove cast and use lttng-ust-abi.h */ |
4063050c MD |
658 | |
659 | /* We are going to receive 2 fds, we need to reserve them. */ | |
660 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
661 | if (ret < 0) { | |
662 | ERR("Exhausted number of available FD upon create channel"); | |
663 | goto error; | |
664 | } | |
86acf0da DG |
665 | |
666 | health_code_update(&health_thread_cmd); | |
667 | ||
852d0037 | 668 | ret = ustctl_create_channel(app->sock, ua_sess->handle, |
5b4a0ec0 DG |
669 | (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj); |
670 | if (ret < 0) { | |
58f3ca76 | 671 | ERR("Creating channel %s for app (pid: %d, sock: %d) " |
5b4a0ec0 | 672 | "and session handle %d with ret %d", |
852d0037 | 673 | ua_chan->name, app->pid, app->sock, |
5b4a0ec0 | 674 | ua_sess->handle, ret); |
4063050c | 675 | lttng_fd_put(LTTNG_FD_APPS, 2); |
b551a063 DG |
676 | goto error; |
677 | } | |
678 | ||
5b4a0ec0 | 679 | ua_chan->handle = ua_chan->obj->handle; |
b551a063 | 680 | |
5b4a0ec0 | 681 | DBG2("UST app channel %s created successfully for pid:%d and sock:%d", |
852d0037 | 682 | ua_chan->name, app->pid, app->sock); |
b551a063 | 683 | |
86acf0da DG |
684 | health_code_update(&health_thread_cmd); |
685 | ||
8535a6d9 DG |
686 | /* If channel is not enabled, disable it on the tracer */ |
687 | if (!ua_chan->enabled) { | |
688 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
689 | if (ret < 0) { | |
690 | goto error; | |
691 | } | |
692 | } | |
693 | ||
b551a063 | 694 | error: |
86acf0da | 695 | health_code_update(&health_thread_cmd); |
b551a063 DG |
696 | return ret; |
697 | } | |
698 | ||
91d76f53 | 699 | /* |
5b4a0ec0 | 700 | * Create the specified event onto the UST tracer for a UST session. |
91d76f53 | 701 | */ |
edb67388 DG |
702 | static |
703 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
704 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) | |
91d76f53 | 705 | { |
5b4a0ec0 | 706 | int ret = 0; |
284d8f55 | 707 | |
86acf0da DG |
708 | health_code_update(&health_thread_cmd); |
709 | ||
5b4a0ec0 | 710 | /* Create UST event on tracer */ |
852d0037 | 711 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 DG |
712 | &ua_event->obj); |
713 | if (ret < 0) { | |
a027b2d2 | 714 | if (ret == -EEXIST || ret == -EPERM) { |
fc34caaa DG |
715 | ret = 0; |
716 | goto error; | |
717 | } | |
5b4a0ec0 | 718 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
852d0037 | 719 | ua_event->attr.name, app->pid, ret); |
5b4a0ec0 | 720 | goto error; |
91d76f53 | 721 | } |
f6a9efaa | 722 | |
5b4a0ec0 | 723 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 724 | |
5b4a0ec0 | 725 | DBG2("UST app event %s created successfully for pid:%d", |
852d0037 | 726 | ua_event->attr.name, app->pid); |
f6a9efaa | 727 | |
86acf0da DG |
728 | health_code_update(&health_thread_cmd); |
729 | ||
8535a6d9 | 730 | /* If event not enabled, disable it on the tracer */ |
fc34caaa | 731 | if (ua_event->enabled == 0) { |
8535a6d9 DG |
732 | ret = disable_ust_event(app, ua_sess, ua_event); |
733 | if (ret < 0) { | |
fc34caaa DG |
734 | /* |
735 | * If we hit an EPERM, something is wrong with our disable call. If | |
736 | * we get an EEXIST, there is a problem on the tracer side since we | |
737 | * just created it. | |
738 | */ | |
739 | switch (ret) { | |
740 | case -EPERM: | |
741 | /* Code flow problem */ | |
742 | assert(0); | |
743 | case -EEXIST: | |
744 | /* It's OK for our use case. */ | |
745 | ret = 0; | |
746 | break; | |
747 | default: | |
748 | break; | |
749 | } | |
8535a6d9 DG |
750 | goto error; |
751 | } | |
752 | } | |
753 | ||
5b4a0ec0 | 754 | error: |
86acf0da | 755 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 756 | return ret; |
91d76f53 | 757 | } |
48842b30 | 758 | |
5b4a0ec0 DG |
759 | /* |
760 | * Copy data between an UST app event and a LTT event. | |
761 | */ | |
421cb601 | 762 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
763 | struct ltt_ust_event *uevent) |
764 | { | |
bec39940 | 765 | struct lttng_ht_iter iter; |
55cc08a6 DG |
766 | struct ltt_ust_context *uctx; |
767 | struct ust_app_ctx *ua_ctx; | |
768 | ||
48842b30 DG |
769 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
770 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
771 | ||
fc34caaa DG |
772 | ua_event->enabled = uevent->enabled; |
773 | ||
5b4a0ec0 DG |
774 | /* Copy event attributes */ |
775 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
776 | ||
53a80697 MD |
777 | /* Copy filter bytecode */ |
778 | if (uevent->filter) { | |
779 | ua_event->filter = zmalloc(sizeof(*ua_event->filter) + | |
780 | uevent->filter->len); | |
781 | if (!ua_event->filter) { | |
782 | return; | |
783 | } | |
784 | memcpy(ua_event->filter, uevent->filter, | |
785 | sizeof(*ua_event->filter) + uevent->filter->len); | |
786 | } | |
bec39940 | 787 | cds_lfht_for_each_entry(uevent->ctx->ht, &iter.iter, uctx, node.node) { |
55cc08a6 DG |
788 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
789 | if (ua_ctx == NULL) { | |
fc34caaa DG |
790 | /* malloc() failed. We should simply stop */ |
791 | return; | |
55cc08a6 | 792 | } |
fc34caaa | 793 | |
bec39940 DG |
794 | lttng_ht_node_init_ulong(&ua_ctx->node, |
795 | (unsigned long) ua_ctx->ctx.ctx); | |
796 | lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node); | |
55cc08a6 | 797 | } |
48842b30 DG |
798 | } |
799 | ||
5b4a0ec0 DG |
800 | /* |
801 | * Copy data between an UST app channel and a LTT channel. | |
802 | */ | |
421cb601 | 803 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
804 | struct ltt_ust_channel *uchan) |
805 | { | |
bec39940 DG |
806 | struct lttng_ht_iter iter; |
807 | struct lttng_ht_node_str *ua_event_node; | |
48842b30 | 808 | struct ltt_ust_event *uevent; |
55cc08a6 | 809 | struct ltt_ust_context *uctx; |
48842b30 | 810 | struct ust_app_event *ua_event; |
55cc08a6 | 811 | struct ust_app_ctx *ua_ctx; |
48842b30 | 812 | |
fc34caaa | 813 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
814 | |
815 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
816 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
5b4a0ec0 DG |
817 | /* Copy event attributes */ |
818 | memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr)); | |
48842b30 | 819 | |
fc34caaa DG |
820 | ua_chan->enabled = uchan->enabled; |
821 | ||
bec39940 | 822 | cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) { |
55cc08a6 DG |
823 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
824 | if (ua_ctx == NULL) { | |
825 | continue; | |
826 | } | |
bec39940 DG |
827 | lttng_ht_node_init_ulong(&ua_ctx->node, |
828 | (unsigned long) ua_ctx->ctx.ctx); | |
829 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); | |
55cc08a6 | 830 | } |
48842b30 | 831 | |
421cb601 | 832 | /* Copy all events from ltt ust channel to ust app channel */ |
bec39940 DG |
833 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
834 | struct lttng_ht_iter uiter; | |
ba767faf | 835 | |
bec39940 DG |
836 | lttng_ht_lookup(ua_chan->events, (void *) uevent->attr.name, &uiter); |
837 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
48842b30 | 838 | if (ua_event_node == NULL) { |
421cb601 | 839 | DBG2("UST event %s not found on shadow copy channel", |
48842b30 | 840 | uevent->attr.name); |
284d8f55 | 841 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
48842b30 | 842 | if (ua_event == NULL) { |
5b4a0ec0 | 843 | continue; |
48842b30 | 844 | } |
421cb601 | 845 | shadow_copy_event(ua_event, uevent); |
bec39940 | 846 | lttng_ht_add_unique_str(ua_chan->events, &ua_event->node); |
48842b30 | 847 | } |
48842b30 DG |
848 | } |
849 | ||
fc34caaa | 850 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
851 | } |
852 | ||
5b4a0ec0 DG |
853 | /* |
854 | * Copy data between a UST app session and a regular LTT session. | |
855 | */ | |
421cb601 | 856 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 857 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 858 | { |
bec39940 DG |
859 | struct lttng_ht_node_str *ua_chan_node; |
860 | struct lttng_ht_iter iter; | |
48842b30 DG |
861 | struct ltt_ust_channel *uchan; |
862 | struct ust_app_channel *ua_chan; | |
477d7741 MD |
863 | time_t rawtime; |
864 | struct tm *timeinfo; | |
865 | char datetime[16]; | |
866 | int ret; | |
867 | ||
868 | /* Get date and time for unique app path */ | |
869 | time(&rawtime); | |
870 | timeinfo = localtime(&rawtime); | |
871 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
48842b30 | 872 | |
421cb601 | 873 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 874 | |
a991f516 | 875 | ua_sess->id = usess->id; |
6df2e2c9 MD |
876 | ua_sess->uid = usess->uid; |
877 | ua_sess->gid = usess->gid; | |
48842b30 | 878 | |
00e2e675 DG |
879 | ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid, |
880 | datetime); | |
477d7741 MD |
881 | if (ret < 0) { |
882 | PERROR("asprintf UST shadow copy session"); | |
883 | /* TODO: We cannot return an error from here.. */ | |
884 | assert(0); | |
885 | } | |
886 | ||
48842b30 DG |
887 | /* TODO: support all UST domain */ |
888 | ||
889 | /* Iterate over all channels in global domain. */ | |
bec39940 DG |
890 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
891 | uchan, node.node) { | |
892 | struct lttng_ht_iter uiter; | |
ba767faf | 893 | |
bec39940 DG |
894 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
895 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
5b4a0ec0 | 896 | if (ua_chan_node != NULL) { |
fc34caaa | 897 | /* Session exist. Contiuing. */ |
5b4a0ec0 DG |
898 | continue; |
899 | } | |
421cb601 | 900 | |
5b4a0ec0 DG |
901 | DBG2("Channel %s not found on shadow session copy, creating it", |
902 | uchan->name); | |
903 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); | |
904 | if (ua_chan == NULL) { | |
fc34caaa | 905 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
5b4a0ec0 | 906 | continue; |
48842b30 DG |
907 | } |
908 | ||
5b4a0ec0 | 909 | shadow_copy_channel(ua_chan, uchan); |
bec39940 | 910 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
48842b30 DG |
911 | } |
912 | } | |
913 | ||
78f0bacd DG |
914 | /* |
915 | * Lookup sesison wrapper. | |
916 | */ | |
84cd17c6 MD |
917 | static |
918 | void __lookup_session_by_app(struct ltt_ust_session *usess, | |
bec39940 | 919 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
920 | { |
921 | /* Get right UST app session from app */ | |
2c348c10 | 922 | lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter); |
84cd17c6 MD |
923 | } |
924 | ||
421cb601 DG |
925 | /* |
926 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 927 | * id. |
421cb601 | 928 | */ |
48842b30 DG |
929 | static struct ust_app_session *lookup_session_by_app( |
930 | struct ltt_ust_session *usess, struct ust_app *app) | |
931 | { | |
bec39940 DG |
932 | struct lttng_ht_iter iter; |
933 | struct lttng_ht_node_ulong *node; | |
48842b30 | 934 | |
84cd17c6 | 935 | __lookup_session_by_app(usess, app, &iter); |
bec39940 | 936 | node = lttng_ht_iter_get_node_ulong(&iter); |
48842b30 DG |
937 | if (node == NULL) { |
938 | goto error; | |
939 | } | |
940 | ||
941 | return caa_container_of(node, struct ust_app_session, node); | |
942 | ||
943 | error: | |
944 | return NULL; | |
945 | } | |
946 | ||
421cb601 DG |
947 | /* |
948 | * Create a UST session onto the tracer of app and add it the session | |
949 | * hashtable. | |
950 | * | |
951 | * Return ust app session or NULL on error. | |
952 | */ | |
953 | static struct ust_app_session *create_ust_app_session( | |
954 | struct ltt_ust_session *usess, struct ust_app *app) | |
955 | { | |
956 | int ret; | |
957 | struct ust_app_session *ua_sess; | |
958 | ||
86acf0da DG |
959 | health_code_update(&health_thread_cmd); |
960 | ||
421cb601 DG |
961 | ua_sess = lookup_session_by_app(usess, app); |
962 | if (ua_sess == NULL) { | |
a991f516 | 963 | DBG2("UST app pid: %d session id %d not found, creating it", |
852d0037 | 964 | app->pid, usess->id); |
421cb601 DG |
965 | ua_sess = alloc_ust_app_session(); |
966 | if (ua_sess == NULL) { | |
967 | /* Only malloc can failed so something is really wrong */ | |
fc34caaa | 968 | goto end; |
421cb601 | 969 | } |
477d7741 | 970 | shadow_copy_session(ua_sess, usess, app); |
421cb601 DG |
971 | } |
972 | ||
86acf0da DG |
973 | health_code_update(&health_thread_cmd); |
974 | ||
421cb601 | 975 | if (ua_sess->handle == -1) { |
852d0037 | 976 | ret = ustctl_create_session(app->sock); |
421cb601 | 977 | if (ret < 0) { |
852d0037 | 978 | ERR("Creating session for app pid %d", app->pid); |
0f83395d | 979 | delete_ust_app_session(-1, ua_sess); |
915d047c DG |
980 | /* This means that the tracer is gone... */ |
981 | ua_sess = (void*) -1UL; | |
0f83395d | 982 | goto end; |
421cb601 DG |
983 | } |
984 | ||
421cb601 DG |
985 | ua_sess->handle = ret; |
986 | ||
987 | /* Add ust app session to app's HT */ | |
2c348c10 | 988 | lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id); |
bec39940 | 989 | lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node); |
421cb601 DG |
990 | |
991 | DBG2("UST app session created successfully with handle %d", ret); | |
992 | } | |
993 | ||
fc34caaa | 994 | end: |
86acf0da | 995 | health_code_update(&health_thread_cmd); |
421cb601 | 996 | return ua_sess; |
421cb601 DG |
997 | } |
998 | ||
55cc08a6 DG |
999 | /* |
1000 | * Create a context for the channel on the tracer. | |
1001 | */ | |
1002 | static | |
1003 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, | |
1004 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, | |
1005 | struct ust_app *app) | |
1006 | { | |
1007 | int ret = 0; | |
bec39940 DG |
1008 | struct lttng_ht_iter iter; |
1009 | struct lttng_ht_node_ulong *node; | |
55cc08a6 DG |
1010 | struct ust_app_ctx *ua_ctx; |
1011 | ||
1012 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
1013 | ||
bec39940 DG |
1014 | lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter); |
1015 | node = lttng_ht_iter_get_node_ulong(&iter); | |
55cc08a6 DG |
1016 | if (node != NULL) { |
1017 | ret = -EEXIST; | |
1018 | goto error; | |
1019 | } | |
1020 | ||
1021 | ua_ctx = alloc_ust_app_ctx(uctx); | |
1022 | if (ua_ctx == NULL) { | |
1023 | /* malloc failed */ | |
1024 | ret = -1; | |
1025 | goto error; | |
1026 | } | |
1027 | ||
bec39940 DG |
1028 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
1029 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); | |
55cc08a6 DG |
1030 | |
1031 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
1032 | if (ret < 0) { | |
1033 | goto error; | |
1034 | } | |
1035 | ||
1036 | error: | |
1037 | return ret; | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Create an UST context and enable it for the event on the tracer. | |
1042 | */ | |
1043 | static | |
1044 | int create_ust_app_event_context(struct ust_app_session *ua_sess, | |
1045 | struct ust_app_event *ua_event, struct lttng_ust_context *uctx, | |
1046 | struct ust_app *app) | |
1047 | { | |
1048 | int ret = 0; | |
bec39940 DG |
1049 | struct lttng_ht_iter iter; |
1050 | struct lttng_ht_node_ulong *node; | |
55cc08a6 DG |
1051 | struct ust_app_ctx *ua_ctx; |
1052 | ||
1053 | DBG2("UST app adding context to event %s", ua_event->name); | |
1054 | ||
bec39940 DG |
1055 | lttng_ht_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx), &iter); |
1056 | node = lttng_ht_iter_get_node_ulong(&iter); | |
55cc08a6 DG |
1057 | if (node != NULL) { |
1058 | ret = -EEXIST; | |
1059 | goto error; | |
1060 | } | |
1061 | ||
1062 | ua_ctx = alloc_ust_app_ctx(uctx); | |
1063 | if (ua_ctx == NULL) { | |
1064 | /* malloc failed */ | |
1065 | ret = -1; | |
1066 | goto error; | |
1067 | } | |
1068 | ||
bec39940 DG |
1069 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
1070 | lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node); | |
55cc08a6 DG |
1071 | |
1072 | ret = create_ust_event_context(ua_event, ua_ctx, app); | |
1073 | if (ret < 0) { | |
1074 | goto error; | |
1075 | } | |
1076 | ||
1077 | error: | |
1078 | return ret; | |
1079 | } | |
1080 | ||
53a80697 MD |
1081 | /* |
1082 | * Set UST filter for the event on the tracer. | |
1083 | */ | |
1084 | static | |
1085 | int set_ust_app_event_filter(struct ust_app_session *ua_sess, | |
1086 | struct ust_app_event *ua_event, | |
1087 | struct lttng_filter_bytecode *bytecode, | |
1088 | struct ust_app *app) | |
1089 | { | |
1090 | int ret = 0; | |
1091 | ||
1092 | DBG2("UST app adding context to event %s", ua_event->name); | |
1093 | ||
1094 | /* Copy filter bytecode */ | |
1095 | ua_event->filter = zmalloc(sizeof(*ua_event->filter) + bytecode->len); | |
1096 | if (!ua_event->filter) { | |
1097 | return -ENOMEM; | |
1098 | } | |
1099 | memcpy(ua_event->filter, bytecode, | |
1100 | sizeof(*ua_event->filter) + bytecode->len); | |
1101 | ret = set_ust_event_filter(ua_event, app); | |
1102 | if (ret < 0) { | |
1103 | goto error; | |
1104 | } | |
1105 | ||
1106 | error: | |
1107 | return ret; | |
1108 | } | |
1109 | ||
edb67388 DG |
1110 | /* |
1111 | * Enable on the tracer side a ust app event for the session and channel. | |
1112 | */ | |
1113 | static | |
1114 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
35a9059d | 1115 | struct ust_app_event *ua_event, struct ust_app *app) |
edb67388 DG |
1116 | { |
1117 | int ret; | |
1118 | ||
1119 | ret = enable_ust_event(app, ua_sess, ua_event); | |
1120 | if (ret < 0) { | |
1121 | goto error; | |
1122 | } | |
1123 | ||
1124 | ua_event->enabled = 1; | |
1125 | ||
1126 | error: | |
1127 | return ret; | |
1128 | } | |
1129 | ||
9730260e DG |
1130 | /* |
1131 | * Disable on the tracer side a ust app event for the session and channel. | |
1132 | */ | |
1133 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
7f79d3a1 | 1134 | struct ust_app_event *ua_event, struct ust_app *app) |
9730260e DG |
1135 | { |
1136 | int ret; | |
1137 | ||
1138 | ret = disable_ust_event(app, ua_sess, ua_event); | |
1139 | if (ret < 0) { | |
1140 | goto error; | |
1141 | } | |
1142 | ||
1143 | ua_event->enabled = 0; | |
1144 | ||
1145 | error: | |
1146 | return ret; | |
1147 | } | |
1148 | ||
78f0bacd DG |
1149 | /* |
1150 | * Lookup ust app channel for session and disable it on the tracer side. | |
1151 | */ | |
8535a6d9 DG |
1152 | static |
1153 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
1154 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
78f0bacd | 1155 | { |
8535a6d9 | 1156 | int ret; |
78f0bacd DG |
1157 | |
1158 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
1159 | if (ret < 0) { | |
1160 | goto error; | |
1161 | } | |
1162 | ||
8535a6d9 DG |
1163 | ua_chan->enabled = 0; |
1164 | ||
78f0bacd DG |
1165 | error: |
1166 | return ret; | |
1167 | } | |
1168 | ||
1169 | /* | |
1170 | * Lookup ust app channel for session and enable it on the tracer side. | |
1171 | */ | |
1172 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
1173 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
1174 | { | |
1175 | int ret = 0; | |
bec39940 DG |
1176 | struct lttng_ht_iter iter; |
1177 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
1178 | struct ust_app_channel *ua_chan; |
1179 | ||
bec39940 DG |
1180 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
1181 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
78f0bacd | 1182 | if (ua_chan_node == NULL) { |
a991f516 MD |
1183 | DBG2("Unable to find channel %s in ust session id %u", |
1184 | uchan->name, ua_sess->id); | |
78f0bacd DG |
1185 | goto error; |
1186 | } | |
1187 | ||
1188 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1189 | ||
1190 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
1191 | if (ret < 0) { | |
1192 | goto error; | |
1193 | } | |
1194 | ||
1195 | error: | |
1196 | return ret; | |
1197 | } | |
1198 | ||
284d8f55 | 1199 | /* |
5b4a0ec0 | 1200 | * Create UST app channel and create it on the tracer. |
284d8f55 | 1201 | */ |
5b4a0ec0 DG |
1202 | static struct ust_app_channel *create_ust_app_channel( |
1203 | struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan, | |
1204 | struct ust_app *app) | |
1205 | { | |
1206 | int ret = 0; | |
bec39940 DG |
1207 | struct lttng_ht_iter iter; |
1208 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
1209 | struct ust_app_channel *ua_chan; |
1210 | ||
1211 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
1212 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
1213 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 1214 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 1215 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 1216 | goto end; |
5b4a0ec0 DG |
1217 | } |
1218 | ||
fc34caaa DG |
1219 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); |
1220 | if (ua_chan == NULL) { | |
1221 | /* Only malloc can fail here */ | |
1222 | goto error; | |
1223 | } | |
1224 | shadow_copy_channel(ua_chan, uchan); | |
1225 | ||
5b4a0ec0 DG |
1226 | ret = create_ust_channel(app, ua_sess, ua_chan); |
1227 | if (ret < 0) { | |
fc34caaa DG |
1228 | /* Not found previously means that it does not exist on the tracer */ |
1229 | assert(ret != -EEXIST); | |
5b4a0ec0 DG |
1230 | goto error; |
1231 | } | |
1232 | ||
58f3ca76 DG |
1233 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
1234 | ||
fc34caaa | 1235 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
852d0037 | 1236 | app->pid); |
fc34caaa DG |
1237 | |
1238 | end: | |
5b4a0ec0 DG |
1239 | return ua_chan; |
1240 | ||
1241 | error: | |
fc34caaa | 1242 | delete_ust_app_channel(-1, ua_chan); |
5b4a0ec0 DG |
1243 | return NULL; |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * Create UST app event and create it on the tracer side. | |
1248 | */ | |
edb67388 DG |
1249 | static |
1250 | int create_ust_app_event(struct ust_app_session *ua_sess, | |
1251 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, | |
1252 | struct ust_app *app) | |
284d8f55 | 1253 | { |
edb67388 | 1254 | int ret = 0; |
bec39940 DG |
1255 | struct lttng_ht_iter iter; |
1256 | struct lttng_ht_node_str *ua_event_node; | |
5b4a0ec0 | 1257 | struct ust_app_event *ua_event; |
284d8f55 | 1258 | |
5b4a0ec0 | 1259 | /* Get event node */ |
bec39940 DG |
1260 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter); |
1261 | ua_event_node = lttng_ht_iter_get_node_str(&iter); | |
edb67388 | 1262 | if (ua_event_node != NULL) { |
fc34caaa | 1263 | ret = -EEXIST; |
edb67388 DG |
1264 | goto end; |
1265 | } | |
5b4a0ec0 | 1266 | |
edb67388 DG |
1267 | /* Does not exist so create one */ |
1268 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
1269 | if (ua_event == NULL) { | |
1270 | /* Only malloc can failed so something is really wrong */ | |
1271 | ret = -ENOMEM; | |
fc34caaa | 1272 | goto end; |
5b4a0ec0 | 1273 | } |
edb67388 | 1274 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 1275 | |
edb67388 | 1276 | /* Create it on the tracer side */ |
5b4a0ec0 | 1277 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 1278 | if (ret < 0) { |
fc34caaa DG |
1279 | /* Not found previously means that it does not exist on the tracer */ |
1280 | assert(ret != -EEXIST); | |
284d8f55 DG |
1281 | goto error; |
1282 | } | |
1283 | ||
bec39940 | 1284 | lttng_ht_add_unique_str(ua_chan->events, &ua_event->node); |
284d8f55 | 1285 | |
fc34caaa | 1286 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
852d0037 | 1287 | app->pid); |
7f79d3a1 | 1288 | |
edb67388 | 1289 | end: |
fc34caaa DG |
1290 | return ret; |
1291 | ||
5b4a0ec0 | 1292 | error: |
fc34caaa DG |
1293 | /* Valid. Calling here is already in a read side lock */ |
1294 | delete_ust_app_event(-1, ua_event); | |
edb67388 | 1295 | return ret; |
5b4a0ec0 DG |
1296 | } |
1297 | ||
1298 | /* | |
1299 | * Create UST metadata and open it on the tracer side. | |
1300 | */ | |
1301 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
1302 | char *pathname, struct ust_app *app) | |
1303 | { | |
1304 | int ret = 0; | |
1305 | ||
1306 | if (ua_sess->metadata == NULL) { | |
1307 | /* Allocate UST metadata */ | |
1308 | ua_sess->metadata = trace_ust_create_metadata(pathname); | |
1309 | if (ua_sess->metadata == NULL) { | |
fc34caaa | 1310 | /* malloc() failed */ |
5b4a0ec0 DG |
1311 | goto error; |
1312 | } | |
1313 | ||
1314 | ret = open_ust_metadata(app, ua_sess); | |
1315 | if (ret < 0) { | |
7db205b5 DG |
1316 | DBG3("Opening metadata failed. Cleaning up memory"); |
1317 | ||
fc34caaa DG |
1318 | /* Cleanup failed metadata struct */ |
1319 | free(ua_sess->metadata); | |
7db205b5 DG |
1320 | /* |
1321 | * This is very important because delete_ust_app_session check if | |
1322 | * the pointer is null or not in order to delete the metadata. | |
1323 | */ | |
1324 | ua_sess->metadata = NULL; | |
5b4a0ec0 DG |
1325 | goto error; |
1326 | } | |
1327 | ||
852d0037 | 1328 | DBG2("UST metadata opened for app pid %d", app->pid); |
5b4a0ec0 DG |
1329 | } |
1330 | ||
1331 | /* Open UST metadata stream */ | |
1332 | if (ua_sess->metadata->stream_obj == NULL) { | |
1333 | ret = create_ust_stream(app, ua_sess); | |
1334 | if (ret < 0) { | |
1335 | goto error; | |
1336 | } | |
1337 | ||
477d7741 MD |
1338 | ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, |
1339 | "%s/metadata", ua_sess->path); | |
5b4a0ec0 DG |
1340 | if (ret < 0) { |
1341 | PERROR("asprintf UST create stream"); | |
1342 | goto error; | |
1343 | } | |
1344 | ||
1345 | DBG2("UST metadata stream object created for app pid %d", | |
852d0037 | 1346 | app->pid); |
5b4a0ec0 DG |
1347 | } else { |
1348 | ERR("Attempting to create stream without metadata opened"); | |
1349 | goto error; | |
1350 | } | |
1351 | ||
1352 | return 0; | |
1353 | ||
1354 | error: | |
1355 | return -1; | |
1356 | } | |
1357 | ||
1358 | /* | |
1359 | * Return pointer to traceable apps list. | |
1360 | */ | |
bec39940 | 1361 | struct lttng_ht *ust_app_get_ht(void) |
5b4a0ec0 DG |
1362 | { |
1363 | return ust_app_ht; | |
1364 | } | |
1365 | ||
1366 | /* | |
1367 | * Return ust app pointer or NULL if not found. | |
1368 | */ | |
1369 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
1370 | { | |
bec39940 DG |
1371 | struct lttng_ht_node_ulong *node; |
1372 | struct lttng_ht_iter iter; | |
5b4a0ec0 DG |
1373 | |
1374 | rcu_read_lock(); | |
bec39940 DG |
1375 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
1376 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
1377 | if (node == NULL) { |
1378 | DBG2("UST app no found with pid %d", pid); | |
1379 | goto error; | |
1380 | } | |
1381 | rcu_read_unlock(); | |
1382 | ||
1383 | DBG2("Found UST app by pid %d", pid); | |
1384 | ||
852d0037 | 1385 | return caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
1386 | |
1387 | error: | |
1388 | rcu_read_unlock(); | |
1389 | return NULL; | |
1390 | } | |
1391 | ||
1392 | /* | |
1393 | * Using pid and uid (of the app), allocate a new ust_app struct and | |
1394 | * add it to the global traceable app list. | |
1395 | * | |
0df502fd MD |
1396 | * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app |
1397 | * bitness is not supported. | |
5b4a0ec0 DG |
1398 | */ |
1399 | int ust_app_register(struct ust_register_msg *msg, int sock) | |
1400 | { | |
1401 | struct ust_app *lta; | |
799e2c4f | 1402 | int ret; |
5b4a0ec0 | 1403 | |
173af62f DG |
1404 | if ((msg->bits_per_long == 64 && |
1405 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
1406 | || (msg->bits_per_long == 32 && | |
1407 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 1408 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
7753dea8 MD |
1409 | "%d-bit long, but no consumerd for this long size is available.\n", |
1410 | msg->name, msg->pid, msg->bits_per_long); | |
799e2c4f MD |
1411 | ret = close(sock); |
1412 | if (ret) { | |
1413 | PERROR("close"); | |
1414 | } | |
4063050c | 1415 | lttng_fd_put(LTTNG_FD_APPS, 1); |
0df502fd MD |
1416 | return -EINVAL; |
1417 | } | |
3f2c5fcc MD |
1418 | if (msg->major != LTTNG_UST_COMM_MAJOR) { |
1419 | ERR("Registration failed: application \"%s\" (pid: %d) has " | |
1420 | "communication protocol version %u.%u, but sessiond supports 2.x.\n", | |
1421 | msg->name, msg->pid, msg->major, msg->minor); | |
799e2c4f MD |
1422 | ret = close(sock); |
1423 | if (ret) { | |
1424 | PERROR("close"); | |
1425 | } | |
4063050c | 1426 | lttng_fd_put(LTTNG_FD_APPS, 1); |
3f2c5fcc MD |
1427 | return -EINVAL; |
1428 | } | |
5b4a0ec0 DG |
1429 | lta = zmalloc(sizeof(struct ust_app)); |
1430 | if (lta == NULL) { | |
1431 | PERROR("malloc"); | |
1432 | return -ENOMEM; | |
1433 | } | |
1434 | ||
1435 | lta->ppid = msg->ppid; | |
1436 | lta->uid = msg->uid; | |
1437 | lta->gid = msg->gid; | |
e0c7ec2b | 1438 | lta->compatible = 0; /* Not compatible until proven */ |
7753dea8 | 1439 | lta->bits_per_long = msg->bits_per_long; |
5b4a0ec0 DG |
1440 | lta->v_major = msg->major; |
1441 | lta->v_minor = msg->minor; | |
1442 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
1443 | lta->name[16] = '\0'; | |
bec39940 | 1444 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
5b4a0ec0 | 1445 | |
852d0037 DG |
1446 | lta->pid = msg->pid; |
1447 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long)lta->pid); | |
1448 | lta->sock = sock; | |
1449 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long)lta->sock); | |
5b4a0ec0 DG |
1450 | |
1451 | rcu_read_lock(); | |
852d0037 DG |
1452 | |
1453 | /* | |
1454 | * On a re-registration, we want to kick out the previous registration of | |
1455 | * that pid | |
1456 | */ | |
1457 | lttng_ht_add_replace_ulong(ust_app_ht, <a->pid_n); | |
1458 | ||
1459 | /* | |
1460 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
1461 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
1462 | * already in the table. | |
1463 | */ | |
1464 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, <a->sock_n); | |
1465 | ||
5b4a0ec0 DG |
1466 | rcu_read_unlock(); |
1467 | ||
1468 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s" | |
852d0037 DG |
1469 | " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid, |
1470 | lta->sock, lta->name, lta->v_major, lta->v_minor); | |
5b4a0ec0 DG |
1471 | |
1472 | return 0; | |
1473 | } | |
1474 | ||
1475 | /* | |
1476 | * Unregister app by removing it from the global traceable app list and freeing | |
1477 | * the data struct. | |
1478 | * | |
1479 | * The socket is already closed at this point so no close to sock. | |
1480 | */ | |
1481 | void ust_app_unregister(int sock) | |
1482 | { | |
1483 | struct ust_app *lta; | |
bec39940 DG |
1484 | struct lttng_ht_node_ulong *node; |
1485 | struct lttng_ht_iter iter; | |
525b0740 | 1486 | int ret; |
5b4a0ec0 DG |
1487 | |
1488 | rcu_read_lock(); | |
886459c6 | 1489 | |
5b4a0ec0 | 1490 | /* Get the node reference for a call_rcu */ |
852d0037 | 1491 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1492 | node = lttng_ht_iter_get_node_ulong(&iter); |
5b4a0ec0 | 1493 | if (node == NULL) { |
852d0037 | 1494 | ERR("Unable to find app by sock %d", sock); |
5b4a0ec0 DG |
1495 | goto error; |
1496 | } | |
284d8f55 | 1497 | |
852d0037 DG |
1498 | lta = caa_container_of(node, struct ust_app, sock_n); |
1499 | ||
1500 | DBG("PID %d unregistering with sock %d", lta->pid, sock); | |
1501 | ||
886459c6 | 1502 | /* Remove application from PID hash table */ |
852d0037 DG |
1503 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
1504 | assert(!ret); | |
1505 | ||
1506 | /* Assign second node for deletion */ | |
1507 | iter.iter.node = <a->pid_n.node; | |
1508 | ||
bec39940 | 1509 | ret = lttng_ht_del(ust_app_ht, &iter); |
525b0740 | 1510 | assert(!ret); |
852d0037 DG |
1511 | |
1512 | /* Free memory */ | |
1513 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
1514 | ||
284d8f55 | 1515 | error: |
5b4a0ec0 DG |
1516 | rcu_read_unlock(); |
1517 | return; | |
284d8f55 DG |
1518 | } |
1519 | ||
1520 | /* | |
5b4a0ec0 | 1521 | * Return traceable_app_count |
284d8f55 | 1522 | */ |
5b4a0ec0 | 1523 | unsigned long ust_app_list_count(void) |
284d8f55 | 1524 | { |
5b4a0ec0 | 1525 | unsigned long count; |
284d8f55 | 1526 | |
5b4a0ec0 | 1527 | rcu_read_lock(); |
bec39940 | 1528 | count = lttng_ht_get_count(ust_app_ht); |
5b4a0ec0 | 1529 | rcu_read_unlock(); |
284d8f55 | 1530 | |
5b4a0ec0 | 1531 | return count; |
284d8f55 DG |
1532 | } |
1533 | ||
5b4a0ec0 DG |
1534 | /* |
1535 | * Fill events array with all events name of all registered apps. | |
1536 | */ | |
1537 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 1538 | { |
5b4a0ec0 DG |
1539 | int ret, handle; |
1540 | size_t nbmem, count = 0; | |
bec39940 | 1541 | struct lttng_ht_iter iter; |
5b4a0ec0 DG |
1542 | struct ust_app *app; |
1543 | struct lttng_event *tmp; | |
421cb601 | 1544 | |
5b4a0ec0 DG |
1545 | nbmem = UST_APP_EVENT_LIST_SIZE; |
1546 | tmp = zmalloc(nbmem * sizeof(struct lttng_event)); | |
1547 | if (tmp == NULL) { | |
1548 | PERROR("zmalloc ust app events"); | |
1549 | ret = -ENOMEM; | |
421cb601 DG |
1550 | goto error; |
1551 | } | |
1552 | ||
5b4a0ec0 | 1553 | rcu_read_lock(); |
421cb601 | 1554 | |
852d0037 | 1555 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
90eaa0d2 | 1556 | struct lttng_ust_tracepoint_iter uiter; |
ac3bd9c0 | 1557 | |
86acf0da DG |
1558 | health_code_update(&health_thread_cmd); |
1559 | ||
e0c7ec2b DG |
1560 | if (!app->compatible) { |
1561 | /* | |
1562 | * TODO: In time, we should notice the caller of this error by | |
1563 | * telling him that this is a version error. | |
1564 | */ | |
1565 | continue; | |
1566 | } | |
852d0037 | 1567 | handle = ustctl_tracepoint_list(app->sock); |
5b4a0ec0 DG |
1568 | if (handle < 0) { |
1569 | ERR("UST app list events getting handle failed for app pid %d", | |
852d0037 | 1570 | app->pid); |
5b4a0ec0 DG |
1571 | continue; |
1572 | } | |
421cb601 | 1573 | |
852d0037 | 1574 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
90eaa0d2 | 1575 | &uiter)) != -ENOENT) { |
86acf0da | 1576 | health_code_update(&health_thread_cmd); |
815564d8 | 1577 | if (count >= nbmem) { |
d7b3776f DG |
1578 | /* In case the realloc fails, we free the memory */ |
1579 | void *tmp_ptr = (void *) tmp; | |
815564d8 MD |
1580 | DBG2("Reallocating event list from %zu to %zu entries", nbmem, |
1581 | 2 * nbmem); | |
1582 | nbmem *= 2; | |
2f221590 | 1583 | tmp = realloc(tmp, nbmem * sizeof(struct lttng_event)); |
5b4a0ec0 DG |
1584 | if (tmp == NULL) { |
1585 | PERROR("realloc ust app events"); | |
d7b3776f | 1586 | free(tmp_ptr); |
5b4a0ec0 DG |
1587 | ret = -ENOMEM; |
1588 | goto rcu_error; | |
1589 | } | |
1590 | } | |
90eaa0d2 | 1591 | memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
8005f29a | 1592 | tmp[count].loglevel = uiter.loglevel; |
6775595e | 1593 | tmp[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; |
852d0037 | 1594 | tmp[count].pid = app->pid; |
5b4a0ec0 DG |
1595 | tmp[count].enabled = -1; |
1596 | count++; | |
421cb601 | 1597 | } |
421cb601 DG |
1598 | } |
1599 | ||
5b4a0ec0 DG |
1600 | ret = count; |
1601 | *events = tmp; | |
421cb601 | 1602 | |
5b4a0ec0 | 1603 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 1604 | |
5b4a0ec0 DG |
1605 | rcu_error: |
1606 | rcu_read_unlock(); | |
421cb601 | 1607 | error: |
86acf0da | 1608 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 1609 | return ret; |
421cb601 DG |
1610 | } |
1611 | ||
f37d259d MD |
1612 | /* |
1613 | * Fill events array with all events name of all registered apps. | |
1614 | */ | |
1615 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
1616 | { | |
1617 | int ret, handle; | |
1618 | size_t nbmem, count = 0; | |
1619 | struct lttng_ht_iter iter; | |
1620 | struct ust_app *app; | |
1621 | struct lttng_event_field *tmp; | |
1622 | ||
1623 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
1624 | tmp = zmalloc(nbmem * sizeof(struct lttng_event_field)); | |
1625 | if (tmp == NULL) { | |
1626 | PERROR("zmalloc ust app event fields"); | |
1627 | ret = -ENOMEM; | |
1628 | goto error; | |
1629 | } | |
1630 | ||
1631 | rcu_read_lock(); | |
1632 | ||
1633 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
1634 | struct lttng_ust_field_iter uiter; | |
1635 | ||
86acf0da DG |
1636 | health_code_update(&health_thread_cmd); |
1637 | ||
f37d259d MD |
1638 | if (!app->compatible) { |
1639 | /* | |
1640 | * TODO: In time, we should notice the caller of this error by | |
1641 | * telling him that this is a version error. | |
1642 | */ | |
1643 | continue; | |
1644 | } | |
1645 | handle = ustctl_tracepoint_field_list(app->sock); | |
1646 | if (handle < 0) { | |
1647 | ERR("UST app list event fields getting handle failed for app pid %d", | |
1648 | app->pid); | |
1649 | continue; | |
1650 | } | |
1651 | ||
1652 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, | |
1653 | &uiter)) != -ENOENT) { | |
86acf0da | 1654 | health_code_update(&health_thread_cmd); |
f37d259d | 1655 | if (count >= nbmem) { |
d7b3776f DG |
1656 | /* In case the realloc fails, we free the memory */ |
1657 | void *tmp_ptr = (void *) tmp; | |
f37d259d MD |
1658 | DBG2("Reallocating event field list from %zu to %zu entries", nbmem, |
1659 | 2 * nbmem); | |
1660 | nbmem *= 2; | |
1661 | tmp = realloc(tmp, nbmem * sizeof(struct lttng_event_field)); | |
1662 | if (tmp == NULL) { | |
1663 | PERROR("realloc ust app event fields"); | |
d7b3776f | 1664 | free(tmp_ptr); |
f37d259d MD |
1665 | ret = -ENOMEM; |
1666 | goto rcu_error; | |
1667 | } | |
1668 | } | |
f37d259d MD |
1669 | |
1670 | memcpy(tmp[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); | |
1671 | tmp[count].type = uiter.type; | |
590b9e3c | 1672 | tmp[count].nowrite = uiter.nowrite; |
f37d259d MD |
1673 | |
1674 | memcpy(tmp[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); | |
1675 | tmp[count].event.loglevel = uiter.loglevel; | |
1676 | tmp[count].event.type = LTTNG_UST_TRACEPOINT; | |
1677 | tmp[count].event.pid = app->pid; | |
1678 | tmp[count].event.enabled = -1; | |
1679 | count++; | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | ret = count; | |
1684 | *fields = tmp; | |
1685 | ||
1686 | DBG2("UST app list event fields done (%zu events)", count); | |
1687 | ||
1688 | rcu_error: | |
1689 | rcu_read_unlock(); | |
1690 | error: | |
86acf0da | 1691 | health_code_update(&health_thread_cmd); |
f37d259d MD |
1692 | return ret; |
1693 | } | |
1694 | ||
5b4a0ec0 DG |
1695 | /* |
1696 | * Free and clean all traceable apps of the global list. | |
1697 | */ | |
1698 | void ust_app_clean_list(void) | |
421cb601 | 1699 | { |
5b4a0ec0 | 1700 | int ret; |
659ed79f | 1701 | struct ust_app *app; |
bec39940 | 1702 | struct lttng_ht_iter iter; |
421cb601 | 1703 | |
5b4a0ec0 | 1704 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 1705 | |
5b4a0ec0 | 1706 | rcu_read_lock(); |
421cb601 | 1707 | |
659ed79f | 1708 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 1709 | ret = lttng_ht_del(ust_app_ht, &iter); |
525b0740 | 1710 | assert(!ret); |
659ed79f | 1711 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
421cb601 DG |
1712 | } |
1713 | ||
852d0037 | 1714 | /* Cleanup socket hash table */ |
659ed79f DG |
1715 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, |
1716 | sock_n.node) { | |
852d0037 | 1717 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
bec39940 DG |
1718 | assert(!ret); |
1719 | } | |
852d0037 | 1720 | |
bec39940 | 1721 | /* Destroy is done only when the ht is empty */ |
852d0037 DG |
1722 | lttng_ht_destroy(ust_app_ht); |
1723 | lttng_ht_destroy(ust_app_ht_by_sock); | |
421cb601 | 1724 | |
5b4a0ec0 DG |
1725 | rcu_read_unlock(); |
1726 | } | |
1727 | ||
1728 | /* | |
1729 | * Init UST app hash table. | |
1730 | */ | |
1731 | void ust_app_ht_alloc(void) | |
1732 | { | |
bec39940 | 1733 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
852d0037 | 1734 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
421cb601 DG |
1735 | } |
1736 | ||
78f0bacd DG |
1737 | /* |
1738 | * For a specific UST session, disable the channel for all registered apps. | |
1739 | */ | |
35a9059d | 1740 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
1741 | struct ltt_ust_channel *uchan) |
1742 | { | |
1743 | int ret = 0; | |
bec39940 DG |
1744 | struct lttng_ht_iter iter; |
1745 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
1746 | struct ust_app *app; |
1747 | struct ust_app_session *ua_sess; | |
8535a6d9 | 1748 | struct ust_app_channel *ua_chan; |
78f0bacd DG |
1749 | |
1750 | if (usess == NULL || uchan == NULL) { | |
1751 | ERR("Disabling UST global channel with NULL values"); | |
1752 | ret = -1; | |
1753 | goto error; | |
1754 | } | |
1755 | ||
a991f516 MD |
1756 | DBG2("UST app disabling channel %s from global domain for session id %d", |
1757 | uchan->name, usess->id); | |
78f0bacd DG |
1758 | |
1759 | rcu_read_lock(); | |
1760 | ||
1761 | /* For every registered applications */ | |
852d0037 | 1762 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 1763 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
1764 | if (!app->compatible) { |
1765 | /* | |
1766 | * TODO: In time, we should notice the caller of this error by | |
1767 | * telling him that this is a version error. | |
1768 | */ | |
1769 | continue; | |
1770 | } | |
78f0bacd DG |
1771 | ua_sess = lookup_session_by_app(usess, app); |
1772 | if (ua_sess == NULL) { | |
1773 | continue; | |
1774 | } | |
1775 | ||
8535a6d9 | 1776 | /* Get channel */ |
bec39940 DG |
1777 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1778 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 DG |
1779 | /* If the session if found for the app, the channel must be there */ |
1780 | assert(ua_chan_node); | |
1781 | ||
1782 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1783 | /* The channel must not be already disabled */ | |
1784 | assert(ua_chan->enabled == 1); | |
1785 | ||
1786 | /* Disable channel onto application */ | |
1787 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
1788 | if (ret < 0) { |
1789 | /* XXX: We might want to report this error at some point... */ | |
1790 | continue; | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | rcu_read_unlock(); | |
1795 | ||
1796 | error: | |
1797 | return ret; | |
1798 | } | |
1799 | ||
1800 | /* | |
1801 | * For a specific UST session, enable the channel for all registered apps. | |
1802 | */ | |
35a9059d | 1803 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
1804 | struct ltt_ust_channel *uchan) |
1805 | { | |
1806 | int ret = 0; | |
bec39940 | 1807 | struct lttng_ht_iter iter; |
78f0bacd DG |
1808 | struct ust_app *app; |
1809 | struct ust_app_session *ua_sess; | |
1810 | ||
1811 | if (usess == NULL || uchan == NULL) { | |
1812 | ERR("Adding UST global channel to NULL values"); | |
1813 | ret = -1; | |
1814 | goto error; | |
1815 | } | |
1816 | ||
a991f516 MD |
1817 | DBG2("UST app enabling channel %s to global domain for session id %d", |
1818 | uchan->name, usess->id); | |
78f0bacd DG |
1819 | |
1820 | rcu_read_lock(); | |
1821 | ||
1822 | /* For every registered applications */ | |
852d0037 | 1823 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1824 | if (!app->compatible) { |
1825 | /* | |
1826 | * TODO: In time, we should notice the caller of this error by | |
1827 | * telling him that this is a version error. | |
1828 | */ | |
1829 | continue; | |
1830 | } | |
78f0bacd DG |
1831 | ua_sess = lookup_session_by_app(usess, app); |
1832 | if (ua_sess == NULL) { | |
1833 | continue; | |
1834 | } | |
1835 | ||
1836 | /* Enable channel onto application */ | |
1837 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
1838 | if (ret < 0) { | |
1839 | /* XXX: We might want to report this error at some point... */ | |
1840 | continue; | |
1841 | } | |
1842 | } | |
1843 | ||
1844 | rcu_read_unlock(); | |
1845 | ||
1846 | error: | |
1847 | return ret; | |
1848 | } | |
1849 | ||
b0a40d28 DG |
1850 | /* |
1851 | * Disable an event in a channel and for a specific session. | |
1852 | */ | |
35a9059d DG |
1853 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
1854 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
1855 | { |
1856 | int ret = 0; | |
bec39940 DG |
1857 | struct lttng_ht_iter iter, uiter; |
1858 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
b0a40d28 DG |
1859 | struct ust_app *app; |
1860 | struct ust_app_session *ua_sess; | |
1861 | struct ust_app_channel *ua_chan; | |
1862 | struct ust_app_event *ua_event; | |
1863 | ||
1864 | DBG("UST app disabling event %s for all apps in channel " | |
a991f516 | 1865 | "%s for session id %d", uevent->attr.name, uchan->name, usess->id); |
b0a40d28 DG |
1866 | |
1867 | rcu_read_lock(); | |
1868 | ||
1869 | /* For all registered applications */ | |
852d0037 | 1870 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1871 | if (!app->compatible) { |
1872 | /* | |
1873 | * TODO: In time, we should notice the caller of this error by | |
1874 | * telling him that this is a version error. | |
1875 | */ | |
1876 | continue; | |
1877 | } | |
b0a40d28 DG |
1878 | ua_sess = lookup_session_by_app(usess, app); |
1879 | if (ua_sess == NULL) { | |
1880 | /* Next app */ | |
1881 | continue; | |
1882 | } | |
1883 | ||
1884 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
1885 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1886 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 1887 | if (ua_chan_node == NULL) { |
a991f516 | 1888 | DBG2("Channel %s not found in session id %d for app pid %d." |
852d0037 | 1889 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
1890 | continue; |
1891 | } | |
1892 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1893 | ||
bec39940 DG |
1894 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); |
1895 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 DG |
1896 | if (ua_event_node == NULL) { |
1897 | DBG2("Event %s not found in channel %s for app pid %d." | |
852d0037 | 1898 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
1899 | continue; |
1900 | } | |
1901 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
1902 | ||
7f79d3a1 | 1903 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
b0a40d28 DG |
1904 | if (ret < 0) { |
1905 | /* XXX: Report error someday... */ | |
1906 | continue; | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | rcu_read_unlock(); | |
1911 | ||
1912 | return ret; | |
1913 | } | |
1914 | ||
9730260e | 1915 | /* |
edb67388 | 1916 | * For a specific UST session and UST channel, the event for all |
9730260e DG |
1917 | * registered apps. |
1918 | */ | |
35a9059d | 1919 | int ust_app_disable_all_event_glb(struct ltt_ust_session *usess, |
9730260e DG |
1920 | struct ltt_ust_channel *uchan) |
1921 | { | |
1922 | int ret = 0; | |
bec39940 DG |
1923 | struct lttng_ht_iter iter, uiter; |
1924 | struct lttng_ht_node_str *ua_chan_node; | |
9730260e DG |
1925 | struct ust_app *app; |
1926 | struct ust_app_session *ua_sess; | |
1927 | struct ust_app_channel *ua_chan; | |
1928 | struct ust_app_event *ua_event; | |
1929 | ||
1930 | DBG("UST app disabling all event for all apps in channel " | |
a991f516 | 1931 | "%s for session id %d", uchan->name, usess->id); |
9730260e DG |
1932 | |
1933 | rcu_read_lock(); | |
1934 | ||
1935 | /* For all registered applications */ | |
852d0037 | 1936 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1937 | if (!app->compatible) { |
1938 | /* | |
1939 | * TODO: In time, we should notice the caller of this error by | |
1940 | * telling him that this is a version error. | |
1941 | */ | |
1942 | continue; | |
1943 | } | |
9730260e | 1944 | ua_sess = lookup_session_by_app(usess, app); |
edb67388 DG |
1945 | /* If ua_sess is NULL, there is a code flow error */ |
1946 | assert(ua_sess); | |
9730260e DG |
1947 | |
1948 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
1949 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1950 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
1951 | /* If the channel is not found, there is a code flow error */ |
1952 | assert(ua_chan_node); | |
1953 | ||
9730260e DG |
1954 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
1955 | ||
1956 | /* Disable each events of channel */ | |
bec39940 DG |
1957 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
1958 | node.node) { | |
7f79d3a1 | 1959 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
9730260e DG |
1960 | if (ret < 0) { |
1961 | /* XXX: Report error someday... */ | |
1962 | continue; | |
1963 | } | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | rcu_read_unlock(); | |
1968 | ||
1969 | return ret; | |
1970 | } | |
1971 | ||
421cb601 | 1972 | /* |
5b4a0ec0 | 1973 | * For a specific UST session, create the channel for all registered apps. |
421cb601 | 1974 | */ |
35a9059d | 1975 | int ust_app_create_channel_glb(struct ltt_ust_session *usess, |
48842b30 DG |
1976 | struct ltt_ust_channel *uchan) |
1977 | { | |
bec39940 | 1978 | struct lttng_ht_iter iter; |
48842b30 DG |
1979 | struct ust_app *app; |
1980 | struct ust_app_session *ua_sess; | |
1981 | struct ust_app_channel *ua_chan; | |
1982 | ||
fc34caaa DG |
1983 | /* Very wrong code flow */ |
1984 | assert(usess); | |
1985 | assert(uchan); | |
421cb601 | 1986 | |
a991f516 MD |
1987 | DBG2("UST app adding channel %s to global domain for session id %d", |
1988 | uchan->name, usess->id); | |
48842b30 DG |
1989 | |
1990 | rcu_read_lock(); | |
421cb601 | 1991 | |
5b4a0ec0 | 1992 | /* For every registered applications */ |
852d0037 | 1993 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1994 | if (!app->compatible) { |
1995 | /* | |
1996 | * TODO: In time, we should notice the caller of this error by | |
1997 | * telling him that this is a version error. | |
1998 | */ | |
1999 | continue; | |
2000 | } | |
edb67388 DG |
2001 | /* |
2002 | * Create session on the tracer side and add it to app session HT. Note | |
2003 | * that if session exist, it will simply return a pointer to the ust | |
2004 | * app session. | |
2005 | */ | |
421cb601 | 2006 | ua_sess = create_ust_app_session(usess, app); |
509cbaf8 | 2007 | if (ua_sess == NULL) { |
915d047c | 2008 | /* The malloc() failed. */ |
fc34caaa | 2009 | goto error; |
915d047c DG |
2010 | } else if (ua_sess == (void *) -1UL) { |
2011 | /* The application's socket is not valid. Contiuing */ | |
2012 | continue; | |
48842b30 DG |
2013 | } |
2014 | ||
421cb601 DG |
2015 | /* Create channel onto application */ |
2016 | ua_chan = create_ust_app_channel(ua_sess, uchan, app); | |
2017 | if (ua_chan == NULL) { | |
fc34caaa DG |
2018 | /* Major problem here and it's maybe the tracer or malloc() */ |
2019 | goto error; | |
48842b30 | 2020 | } |
48842b30 | 2021 | } |
5b4a0ec0 | 2022 | |
48842b30 DG |
2023 | rcu_read_unlock(); |
2024 | ||
fc34caaa DG |
2025 | return 0; |
2026 | ||
421cb601 | 2027 | error: |
fc34caaa | 2028 | return -1; |
48842b30 DG |
2029 | } |
2030 | ||
5b4a0ec0 | 2031 | /* |
edb67388 | 2032 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 2033 | */ |
35a9059d | 2034 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
2035 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
2036 | { | |
2037 | int ret = 0; | |
bec39940 DG |
2038 | struct lttng_ht_iter iter, uiter; |
2039 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
48842b30 DG |
2040 | struct ust_app *app; |
2041 | struct ust_app_session *ua_sess; | |
2042 | struct ust_app_channel *ua_chan; | |
2043 | struct ust_app_event *ua_event; | |
48842b30 | 2044 | |
a991f516 MD |
2045 | DBG("UST app enabling event %s for all apps for session id %d", |
2046 | uevent->attr.name, usess->id); | |
48842b30 | 2047 | |
edb67388 DG |
2048 | /* |
2049 | * NOTE: At this point, this function is called only if the session and | |
2050 | * channel passed are already created for all apps. and enabled on the | |
2051 | * tracer also. | |
2052 | */ | |
2053 | ||
48842b30 | 2054 | rcu_read_lock(); |
421cb601 DG |
2055 | |
2056 | /* For all registered applications */ | |
852d0037 | 2057 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2058 | if (!app->compatible) { |
2059 | /* | |
2060 | * TODO: In time, we should notice the caller of this error by | |
2061 | * telling him that this is a version error. | |
2062 | */ | |
2063 | continue; | |
2064 | } | |
edb67388 DG |
2065 | ua_sess = lookup_session_by_app(usess, app); |
2066 | /* If ua_sess is NULL, there is a code flow error */ | |
2067 | assert(ua_sess); | |
ba767faf | 2068 | |
edb67388 | 2069 | /* Lookup channel in the ust app session */ |
bec39940 DG |
2070 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2071 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
2072 | /* If the channel is not found, there is a code flow error */ |
2073 | assert(ua_chan_node); | |
2074 | ||
2075 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2076 | ||
bec39940 DG |
2077 | lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter); |
2078 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
35a9059d | 2079 | if (ua_event_node == NULL) { |
7f79d3a1 | 2080 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 2081 | "Skipping app", uevent->attr.name, app->pid); |
35a9059d DG |
2082 | continue; |
2083 | } | |
2084 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
2085 | ||
2086 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
2087 | if (ret < 0) { | |
7f79d3a1 | 2088 | goto error; |
48842b30 | 2089 | } |
edb67388 DG |
2090 | } |
2091 | ||
7f79d3a1 | 2092 | error: |
edb67388 | 2093 | rcu_read_unlock(); |
edb67388 DG |
2094 | return ret; |
2095 | } | |
2096 | ||
2097 | /* | |
2098 | * For a specific existing UST session and UST channel, creates the event for | |
2099 | * all registered apps. | |
2100 | */ | |
35a9059d | 2101 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
2102 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
2103 | { | |
2104 | int ret = 0; | |
bec39940 DG |
2105 | struct lttng_ht_iter iter, uiter; |
2106 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
2107 | struct ust_app *app; |
2108 | struct ust_app_session *ua_sess; | |
2109 | struct ust_app_channel *ua_chan; | |
2110 | ||
a991f516 MD |
2111 | DBG("UST app creating event %s for all apps for session id %d", |
2112 | uevent->attr.name, usess->id); | |
edb67388 | 2113 | |
edb67388 DG |
2114 | rcu_read_lock(); |
2115 | ||
2116 | /* For all registered applications */ | |
852d0037 | 2117 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2118 | if (!app->compatible) { |
2119 | /* | |
2120 | * TODO: In time, we should notice the caller of this error by | |
2121 | * telling him that this is a version error. | |
2122 | */ | |
2123 | continue; | |
2124 | } | |
edb67388 DG |
2125 | ua_sess = lookup_session_by_app(usess, app); |
2126 | /* If ua_sess is NULL, there is a code flow error */ | |
2127 | assert(ua_sess); | |
48842b30 DG |
2128 | |
2129 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2130 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2131 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
2132 | /* If the channel is not found, there is a code flow error */ |
2133 | assert(ua_chan_node); | |
2134 | ||
48842b30 DG |
2135 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
2136 | ||
edb67388 DG |
2137 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
2138 | if (ret < 0) { | |
fc34caaa DG |
2139 | if (ret != -EEXIST) { |
2140 | /* Possible value at this point: -ENOMEM. If so, we stop! */ | |
2141 | break; | |
2142 | } | |
2143 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 2144 | uevent->attr.name, app->pid); |
5b4a0ec0 | 2145 | continue; |
48842b30 | 2146 | } |
48842b30 | 2147 | } |
5b4a0ec0 | 2148 | |
48842b30 DG |
2149 | rcu_read_unlock(); |
2150 | ||
2151 | return ret; | |
2152 | } | |
2153 | ||
5b4a0ec0 DG |
2154 | /* |
2155 | * Start tracing for a specific UST session and app. | |
2156 | */ | |
421cb601 | 2157 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
2158 | { |
2159 | int ret = 0; | |
bec39940 | 2160 | struct lttng_ht_iter iter; |
48842b30 DG |
2161 | struct ust_app_session *ua_sess; |
2162 | struct ust_app_channel *ua_chan; | |
5b4a0ec0 | 2163 | struct ltt_ust_stream *ustream; |
173af62f | 2164 | struct consumer_socket *socket; |
48842b30 | 2165 | |
852d0037 | 2166 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 2167 | |
509cbaf8 MD |
2168 | rcu_read_lock(); |
2169 | ||
e0c7ec2b DG |
2170 | if (!app->compatible) { |
2171 | goto end; | |
2172 | } | |
2173 | ||
421cb601 DG |
2174 | ua_sess = lookup_session_by_app(usess, app); |
2175 | if (ua_sess == NULL) { | |
509cbaf8 | 2176 | goto error_rcu_unlock; |
421cb601 | 2177 | } |
48842b30 | 2178 | |
aea829b3 DG |
2179 | /* Upon restart, we skip the setup, already done */ |
2180 | if (ua_sess->started) { | |
8be98f9a | 2181 | goto skip_setup; |
aea829b3 | 2182 | } |
8be98f9a | 2183 | |
a4b92340 DG |
2184 | /* Create directories if consumer is LOCAL and has a path defined. */ |
2185 | if (usess->consumer->type == CONSUMER_DST_LOCAL && | |
2186 | strlen(usess->consumer->dst.trace_path) > 0) { | |
2187 | ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, | |
2188 | S_IRWXU | S_IRWXG, usess->uid, usess->gid); | |
2189 | if (ret < 0) { | |
2190 | if (ret != -EEXIST) { | |
2191 | ERR("Trace directory creation error"); | |
2192 | ret = -1; | |
2193 | goto error_rcu_unlock; | |
2194 | } | |
2195 | } | |
2196 | } | |
2197 | ||
f848c3eb DG |
2198 | /* Indicate that the session has been started once */ |
2199 | ua_sess->started = 1; | |
2200 | ||
421cb601 DG |
2201 | ret = create_ust_app_metadata(ua_sess, usess->pathname, app); |
2202 | if (ret < 0) { | |
f73fabfd | 2203 | ret = LTTNG_ERR_UST_META_FAIL; |
509cbaf8 | 2204 | goto error_rcu_unlock; |
421cb601 | 2205 | } |
48842b30 | 2206 | |
421cb601 | 2207 | /* For each channel */ |
bec39940 DG |
2208 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2209 | node.node) { | |
421cb601 DG |
2210 | /* Create all streams */ |
2211 | while (1) { | |
5b4a0ec0 | 2212 | /* Create UST stream */ |
421cb601 DG |
2213 | ustream = zmalloc(sizeof(*ustream)); |
2214 | if (ustream == NULL) { | |
2215 | PERROR("zmalloc ust stream"); | |
509cbaf8 | 2216 | goto error_rcu_unlock; |
421cb601 | 2217 | } |
48842b30 | 2218 | |
4063050c MD |
2219 | /* We are going to receive 2 fds, we need to reserve them. */ |
2220 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
2221 | if (ret < 0) { | |
2222 | ERR("Exhausted number of available FD upon stream create"); | |
2223 | free(ustream); | |
2224 | goto error_rcu_unlock; | |
2225 | } | |
86acf0da DG |
2226 | |
2227 | health_code_update(&health_thread_cmd); | |
2228 | ||
852d0037 | 2229 | ret = ustctl_create_stream(app->sock, ua_chan->obj, |
421cb601 | 2230 | &ustream->obj); |
48842b30 | 2231 | if (ret < 0) { |
421cb601 | 2232 | /* Got all streams */ |
4063050c | 2233 | lttng_fd_put(LTTNG_FD_APPS, 2); |
a2c0da86 | 2234 | free(ustream); |
f73fabfd | 2235 | ret = LTTNG_ERR_UST_STREAM_FAIL; |
421cb601 | 2236 | break; |
48842b30 | 2237 | } |
421cb601 | 2238 | ustream->handle = ustream->obj->handle; |
48842b30 | 2239 | |
86acf0da DG |
2240 | health_code_update(&health_thread_cmd); |
2241 | ||
421cb601 DG |
2242 | /* Order is important */ |
2243 | cds_list_add_tail(&ustream->list, &ua_chan->streams.head); | |
00e2e675 | 2244 | ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u", |
c30aaa51 MD |
2245 | ua_chan->name, ua_chan->streams.count); |
2246 | ua_chan->streams.count++; | |
aba8e916 DG |
2247 | if (ret < 0) { |
2248 | PERROR("asprintf UST create stream"); | |
a2c0da86 MD |
2249 | /* |
2250 | * XXX what should we do here with the | |
2251 | * stream ? | |
2252 | */ | |
421cb601 | 2253 | continue; |
aba8e916 | 2254 | } |
00e2e675 DG |
2255 | DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count, |
2256 | ustream->handle); | |
421cb601 | 2257 | } |
86acf0da DG |
2258 | |
2259 | health_code_update(&health_thread_cmd); | |
421cb601 | 2260 | } |
aba8e916 | 2261 | |
7753dea8 MD |
2262 | switch (app->bits_per_long) { |
2263 | case 64: | |
173af62f DG |
2264 | socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd), |
2265 | usess->consumer); | |
2266 | if (socket == NULL) { | |
2267 | goto skip_setup; | |
2268 | } | |
7753dea8 MD |
2269 | break; |
2270 | case 32: | |
173af62f DG |
2271 | socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd), |
2272 | usess->consumer); | |
2273 | if (socket == NULL) { | |
2274 | goto skip_setup; | |
2275 | } | |
7753dea8 MD |
2276 | break; |
2277 | default: | |
2278 | ret = -EINVAL; | |
2279 | goto error_rcu_unlock; | |
2280 | } | |
aea829b3 | 2281 | |
421cb601 | 2282 | /* Setup UST consumer socket and send fds to it */ |
173af62f | 2283 | ret = ust_consumer_send_session(ua_sess, usess->consumer, socket); |
421cb601 | 2284 | if (ret < 0) { |
509cbaf8 | 2285 | goto error_rcu_unlock; |
421cb601 | 2286 | } |
48842b30 | 2287 | |
86acf0da DG |
2288 | health_code_update(&health_thread_cmd); |
2289 | ||
8be98f9a | 2290 | skip_setup: |
421cb601 | 2291 | /* This start the UST tracing */ |
852d0037 | 2292 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
421cb601 | 2293 | if (ret < 0) { |
852d0037 | 2294 | ERR("Error starting tracing for app pid: %d", app->pid); |
509cbaf8 | 2295 | goto error_rcu_unlock; |
421cb601 | 2296 | } |
5b4a0ec0 | 2297 | |
86acf0da DG |
2298 | health_code_update(&health_thread_cmd); |
2299 | ||
421cb601 | 2300 | /* Quiescent wait after starting trace */ |
852d0037 | 2301 | ustctl_wait_quiescent(app->sock); |
48842b30 | 2302 | |
e0c7ec2b DG |
2303 | end: |
2304 | rcu_read_unlock(); | |
86acf0da | 2305 | health_code_update(&health_thread_cmd); |
421cb601 | 2306 | return 0; |
48842b30 | 2307 | |
509cbaf8 MD |
2308 | error_rcu_unlock: |
2309 | rcu_read_unlock(); | |
86acf0da | 2310 | health_code_update(&health_thread_cmd); |
421cb601 DG |
2311 | return -1; |
2312 | } | |
48842b30 | 2313 | |
8be98f9a MD |
2314 | /* |
2315 | * Stop tracing for a specific UST session and app. | |
2316 | */ | |
2317 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) | |
2318 | { | |
2319 | int ret = 0; | |
bec39940 | 2320 | struct lttng_ht_iter iter; |
8be98f9a | 2321 | struct ust_app_session *ua_sess; |
6d3686da | 2322 | struct ust_app_channel *ua_chan; |
8be98f9a | 2323 | |
852d0037 | 2324 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
2325 | |
2326 | rcu_read_lock(); | |
2327 | ||
e0c7ec2b DG |
2328 | if (!app->compatible) { |
2329 | goto end; | |
2330 | } | |
2331 | ||
8be98f9a MD |
2332 | ua_sess = lookup_session_by_app(usess, app); |
2333 | if (ua_sess == NULL) { | |
2334 | /* Only malloc can failed so something is really wrong */ | |
2335 | goto error_rcu_unlock; | |
2336 | } | |
2337 | ||
9bc07046 DG |
2338 | /* |
2339 | * If started = 0, it means that stop trace has been called for a session | |
2340 | * that was never started. This is a code flow error and should never | |
2341 | * happen. | |
2342 | */ | |
2343 | assert(ua_sess->started == 1); | |
7db205b5 | 2344 | |
86acf0da DG |
2345 | health_code_update(&health_thread_cmd); |
2346 | ||
9d6c7d3f | 2347 | /* This inhibits UST tracing */ |
852d0037 | 2348 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
9d6c7d3f | 2349 | if (ret < 0) { |
852d0037 | 2350 | ERR("Error stopping tracing for app pid: %d", app->pid); |
9d6c7d3f DG |
2351 | goto error_rcu_unlock; |
2352 | } | |
2353 | ||
86acf0da DG |
2354 | health_code_update(&health_thread_cmd); |
2355 | ||
9d6c7d3f | 2356 | /* Quiescent wait after stopping trace */ |
852d0037 | 2357 | ustctl_wait_quiescent(app->sock); |
9d6c7d3f | 2358 | |
86acf0da DG |
2359 | health_code_update(&health_thread_cmd); |
2360 | ||
9d6c7d3f | 2361 | /* Flushing buffers */ |
bec39940 DG |
2362 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2363 | node.node) { | |
86acf0da | 2364 | health_code_update(&health_thread_cmd); |
852d0037 | 2365 | ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj); |
8be98f9a | 2366 | if (ret < 0) { |
7db205b5 | 2367 | ERR("UST app PID %d channel %s flush failed with ret %d", |
852d0037 | 2368 | app->pid, ua_chan->name, ret); |
6d3686da DG |
2369 | /* Continuing flushing all buffers */ |
2370 | continue; | |
8be98f9a MD |
2371 | } |
2372 | } | |
8be98f9a | 2373 | |
86acf0da DG |
2374 | health_code_update(&health_thread_cmd); |
2375 | ||
90d97d10 | 2376 | /* Flush all buffers before stopping */ |
852d0037 | 2377 | ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj); |
90d97d10 | 2378 | if (ret < 0) { |
852d0037 | 2379 | ERR("UST app PID %d metadata flush failed with ret %d", app->pid, |
7db205b5 | 2380 | ret); |
90d97d10 DG |
2381 | } |
2382 | ||
7db205b5 DG |
2383 | end: |
2384 | rcu_read_unlock(); | |
86acf0da | 2385 | health_code_update(&health_thread_cmd); |
8be98f9a MD |
2386 | return 0; |
2387 | ||
2388 | error_rcu_unlock: | |
2389 | rcu_read_unlock(); | |
86acf0da | 2390 | health_code_update(&health_thread_cmd); |
8be98f9a MD |
2391 | return -1; |
2392 | } | |
2393 | ||
84cd17c6 MD |
2394 | /* |
2395 | * Destroy a specific UST session in apps. | |
2396 | */ | |
2397 | int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) | |
2398 | { | |
2399 | struct ust_app_session *ua_sess; | |
2400 | struct lttng_ust_object_data obj; | |
bec39940 DG |
2401 | struct lttng_ht_iter iter; |
2402 | struct lttng_ht_node_ulong *node; | |
525b0740 | 2403 | int ret; |
84cd17c6 | 2404 | |
852d0037 | 2405 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
2406 | |
2407 | rcu_read_lock(); | |
2408 | ||
e0c7ec2b DG |
2409 | if (!app->compatible) { |
2410 | goto end; | |
2411 | } | |
2412 | ||
84cd17c6 | 2413 | __lookup_session_by_app(usess, app, &iter); |
bec39940 | 2414 | node = lttng_ht_iter_get_node_ulong(&iter); |
84cd17c6 MD |
2415 | if (node == NULL) { |
2416 | /* Only malloc can failed so something is really wrong */ | |
2417 | goto error_rcu_unlock; | |
2418 | } | |
2419 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
bec39940 | 2420 | ret = lttng_ht_del(app->sessions, &iter); |
525b0740 | 2421 | assert(!ret); |
84cd17c6 MD |
2422 | obj.handle = ua_sess->handle; |
2423 | obj.shm_fd = -1; | |
2424 | obj.wait_fd = -1; | |
2425 | obj.memory_map_size = 0; | |
86acf0da | 2426 | health_code_update(&health_thread_cmd); |
852d0037 | 2427 | ustctl_release_object(app->sock, &obj); |
84cd17c6 | 2428 | |
86acf0da | 2429 | health_code_update(&health_thread_cmd); |
852d0037 | 2430 | delete_ust_app_session(app->sock, ua_sess); |
7db205b5 | 2431 | |
84cd17c6 | 2432 | /* Quiescent wait after stopping trace */ |
852d0037 | 2433 | ustctl_wait_quiescent(app->sock); |
84cd17c6 | 2434 | |
e0c7ec2b DG |
2435 | end: |
2436 | rcu_read_unlock(); | |
86acf0da | 2437 | health_code_update(&health_thread_cmd); |
84cd17c6 MD |
2438 | return 0; |
2439 | ||
2440 | error_rcu_unlock: | |
2441 | rcu_read_unlock(); | |
86acf0da | 2442 | health_code_update(&health_thread_cmd); |
84cd17c6 MD |
2443 | return -1; |
2444 | } | |
2445 | ||
5b4a0ec0 DG |
2446 | /* |
2447 | * Start tracing for the UST session. | |
2448 | */ | |
421cb601 DG |
2449 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
2450 | { | |
2451 | int ret = 0; | |
bec39940 | 2452 | struct lttng_ht_iter iter; |
421cb601 | 2453 | struct ust_app *app; |
48842b30 | 2454 | |
421cb601 DG |
2455 | DBG("Starting all UST traces"); |
2456 | ||
2457 | rcu_read_lock(); | |
421cb601 | 2458 | |
852d0037 | 2459 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
421cb601 | 2460 | ret = ust_app_start_trace(usess, app); |
48842b30 | 2461 | if (ret < 0) { |
5b4a0ec0 DG |
2462 | /* Continue to next apps even on error */ |
2463 | continue; | |
48842b30 | 2464 | } |
48842b30 | 2465 | } |
5b4a0ec0 | 2466 | |
48842b30 DG |
2467 | rcu_read_unlock(); |
2468 | ||
2469 | return 0; | |
2470 | } | |
487cf67c | 2471 | |
8be98f9a MD |
2472 | /* |
2473 | * Start tracing for the UST session. | |
2474 | */ | |
2475 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
2476 | { | |
2477 | int ret = 0; | |
bec39940 | 2478 | struct lttng_ht_iter iter; |
8be98f9a MD |
2479 | struct ust_app *app; |
2480 | ||
2481 | DBG("Stopping all UST traces"); | |
2482 | ||
2483 | rcu_read_lock(); | |
2484 | ||
852d0037 | 2485 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
8be98f9a MD |
2486 | ret = ust_app_stop_trace(usess, app); |
2487 | if (ret < 0) { | |
2488 | /* Continue to next apps even on error */ | |
2489 | continue; | |
2490 | } | |
2491 | } | |
2492 | ||
2493 | rcu_read_unlock(); | |
2494 | ||
2495 | return 0; | |
2496 | } | |
2497 | ||
84cd17c6 MD |
2498 | /* |
2499 | * Destroy app UST session. | |
2500 | */ | |
2501 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
2502 | { | |
2503 | int ret = 0; | |
bec39940 | 2504 | struct lttng_ht_iter iter; |
84cd17c6 MD |
2505 | struct ust_app *app; |
2506 | ||
2507 | DBG("Destroy all UST traces"); | |
2508 | ||
2509 | rcu_read_lock(); | |
2510 | ||
852d0037 | 2511 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
84cd17c6 MD |
2512 | ret = ust_app_destroy_trace(usess, app); |
2513 | if (ret < 0) { | |
2514 | /* Continue to next apps even on error */ | |
2515 | continue; | |
2516 | } | |
2517 | } | |
2518 | ||
2519 | rcu_read_unlock(); | |
2520 | ||
2521 | return 0; | |
2522 | } | |
2523 | ||
5b4a0ec0 DG |
2524 | /* |
2525 | * Add channels/events from UST global domain to registered apps at sock. | |
2526 | */ | |
487cf67c DG |
2527 | void ust_app_global_update(struct ltt_ust_session *usess, int sock) |
2528 | { | |
55c54cce | 2529 | int ret = 0; |
727d5404 | 2530 | struct lttng_ht_iter iter, uiter, iter_ctx; |
487cf67c DG |
2531 | struct ust_app *app; |
2532 | struct ust_app_session *ua_sess; | |
2533 | struct ust_app_channel *ua_chan; | |
2534 | struct ust_app_event *ua_event; | |
727d5404 | 2535 | struct ust_app_ctx *ua_ctx; |
1f3580c7 DG |
2536 | |
2537 | if (usess == NULL) { | |
5b4a0ec0 | 2538 | ERR("No UST session on global update. Returning"); |
1f3580c7 DG |
2539 | goto error; |
2540 | } | |
2541 | ||
a991f516 MD |
2542 | DBG2("UST app global update for app sock %d for session id %d", sock, |
2543 | usess->id); | |
487cf67c | 2544 | |
284d8f55 DG |
2545 | rcu_read_lock(); |
2546 | ||
487cf67c DG |
2547 | app = find_app_by_sock(sock); |
2548 | if (app == NULL) { | |
2549 | ERR("Failed to update app sock %d", sock); | |
2550 | goto error; | |
2551 | } | |
2552 | ||
e0c7ec2b DG |
2553 | if (!app->compatible) { |
2554 | goto error; | |
2555 | } | |
2556 | ||
421cb601 | 2557 | ua_sess = create_ust_app_session(usess, app); |
487cf67c | 2558 | if (ua_sess == NULL) { |
487cf67c DG |
2559 | goto error; |
2560 | } | |
2561 | ||
284d8f55 DG |
2562 | /* |
2563 | * We can iterate safely here over all UST app session sicne the create ust | |
2564 | * app session above made a shadow copy of the UST global domain from the | |
2565 | * ltt ust session. | |
2566 | */ | |
bec39940 DG |
2567 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2568 | node.node) { | |
284d8f55 DG |
2569 | ret = create_ust_channel(app, ua_sess, ua_chan); |
2570 | if (ret < 0) { | |
2571 | /* FIXME: Should we quit here or continue... */ | |
2572 | continue; | |
487cf67c DG |
2573 | } |
2574 | ||
727d5404 DG |
2575 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx, |
2576 | node.node) { | |
2577 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2578 | if (ret < 0) { | |
2579 | /* FIXME: Should we quit here or continue... */ | |
2580 | continue; | |
2581 | } | |
2582 | } | |
2583 | ||
2584 | ||
284d8f55 | 2585 | /* For each events */ |
bec39940 DG |
2586 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
2587 | node.node) { | |
284d8f55 DG |
2588 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
2589 | if (ret < 0) { | |
2590 | /* FIXME: Should we quit here or continue... */ | |
2591 | continue; | |
487cf67c | 2592 | } |
727d5404 DG |
2593 | |
2594 | /* Add context on events. */ | |
2595 | cds_lfht_for_each_entry(ua_event->ctx->ht, &iter_ctx.iter, | |
2596 | ua_ctx, node.node) { | |
2597 | ret = create_ust_event_context(ua_event, ua_ctx, app); | |
2598 | if (ret < 0) { | |
2599 | /* FIXME: Should we quit here or continue... */ | |
2600 | continue; | |
2601 | } | |
2602 | } | |
53a80697 MD |
2603 | ret = set_ust_event_filter(ua_event, app); |
2604 | if (ret < 0) { | |
2605 | /* FIXME: Should we quit here or continue... */ | |
2606 | continue; | |
2607 | } | |
36dc12cc | 2608 | } |
487cf67c DG |
2609 | } |
2610 | ||
36dc12cc | 2611 | if (usess->start_trace) { |
421cb601 | 2612 | ret = ust_app_start_trace(usess, app); |
36dc12cc | 2613 | if (ret < 0) { |
36dc12cc DG |
2614 | goto error; |
2615 | } | |
2616 | ||
852d0037 | 2617 | DBG2("UST trace started for app pid %d", app->pid); |
36dc12cc DG |
2618 | } |
2619 | ||
487cf67c DG |
2620 | error: |
2621 | rcu_read_unlock(); | |
2622 | return; | |
2623 | } | |
55cc08a6 DG |
2624 | |
2625 | /* | |
2626 | * Add context to a specific channel for global UST domain. | |
2627 | */ | |
2628 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
2629 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
2630 | { | |
2631 | int ret = 0; | |
bec39940 DG |
2632 | struct lttng_ht_node_str *ua_chan_node; |
2633 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
2634 | struct ust_app_channel *ua_chan = NULL; |
2635 | struct ust_app_session *ua_sess; | |
2636 | struct ust_app *app; | |
2637 | ||
2638 | rcu_read_lock(); | |
2639 | ||
852d0037 | 2640 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2641 | if (!app->compatible) { |
2642 | /* | |
2643 | * TODO: In time, we should notice the caller of this error by | |
2644 | * telling him that this is a version error. | |
2645 | */ | |
2646 | continue; | |
2647 | } | |
55cc08a6 DG |
2648 | ua_sess = lookup_session_by_app(usess, app); |
2649 | if (ua_sess == NULL) { | |
2650 | continue; | |
2651 | } | |
2652 | ||
2653 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2654 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2655 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 DG |
2656 | if (ua_chan_node == NULL) { |
2657 | continue; | |
2658 | } | |
2659 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
2660 | node); | |
2661 | ||
2662 | ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app); | |
2663 | if (ret < 0) { | |
2664 | continue; | |
2665 | } | |
2666 | } | |
2667 | ||
55cc08a6 DG |
2668 | rcu_read_unlock(); |
2669 | return ret; | |
2670 | } | |
2671 | ||
2672 | /* | |
2673 | * Add context to a specific event in a channel for global UST domain. | |
2674 | */ | |
2675 | int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess, | |
2676 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, | |
2677 | struct ltt_ust_context *uctx) | |
2678 | { | |
2679 | int ret = 0; | |
bec39940 DG |
2680 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; |
2681 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
2682 | struct ust_app_session *ua_sess; |
2683 | struct ust_app_event *ua_event; | |
2684 | struct ust_app_channel *ua_chan = NULL; | |
2685 | struct ust_app *app; | |
2686 | ||
2687 | rcu_read_lock(); | |
2688 | ||
852d0037 | 2689 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2690 | if (!app->compatible) { |
2691 | /* | |
2692 | * TODO: In time, we should notice the caller of this error by | |
2693 | * telling him that this is a version error. | |
2694 | */ | |
2695 | continue; | |
2696 | } | |
55cc08a6 DG |
2697 | ua_sess = lookup_session_by_app(usess, app); |
2698 | if (ua_sess == NULL) { | |
2699 | continue; | |
2700 | } | |
2701 | ||
2702 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2703 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2704 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 DG |
2705 | if (ua_chan_node == NULL) { |
2706 | continue; | |
2707 | } | |
2708 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
2709 | node); | |
2710 | ||
bec39940 DG |
2711 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); |
2712 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 DG |
2713 | if (ua_event_node == NULL) { |
2714 | continue; | |
2715 | } | |
2716 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, | |
2717 | node); | |
2718 | ||
2719 | ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app); | |
2720 | if (ret < 0) { | |
2721 | continue; | |
2722 | } | |
2723 | } | |
2724 | ||
55cc08a6 DG |
2725 | rcu_read_unlock(); |
2726 | return ret; | |
2727 | } | |
76d45b40 | 2728 | |
53a80697 MD |
2729 | /* |
2730 | * Add context to a specific event in a channel for global UST domain. | |
2731 | */ | |
2732 | int ust_app_set_filter_event_glb(struct ltt_ust_session *usess, | |
2733 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, | |
2734 | struct lttng_filter_bytecode *bytecode) | |
2735 | { | |
2736 | int ret = 0; | |
2737 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
2738 | struct lttng_ht_iter iter, uiter; | |
2739 | struct ust_app_session *ua_sess; | |
2740 | struct ust_app_event *ua_event; | |
2741 | struct ust_app_channel *ua_chan = NULL; | |
2742 | struct ust_app *app; | |
2743 | ||
2744 | rcu_read_lock(); | |
2745 | ||
2746 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
2747 | if (!app->compatible) { | |
2748 | /* | |
2749 | * TODO: In time, we should notice the caller of this error by | |
2750 | * telling him that this is a version error. | |
2751 | */ | |
2752 | continue; | |
2753 | } | |
2754 | ua_sess = lookup_session_by_app(usess, app); | |
2755 | if (ua_sess == NULL) { | |
2756 | continue; | |
2757 | } | |
2758 | ||
2759 | /* Lookup channel in the ust app session */ | |
2760 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); | |
2761 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
2762 | if (ua_chan_node == NULL) { | |
2763 | continue; | |
2764 | } | |
2765 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
2766 | node); | |
2767 | ||
2768 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); | |
2769 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
2770 | if (ua_event_node == NULL) { | |
2771 | continue; | |
2772 | } | |
2773 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, | |
2774 | node); | |
2775 | ||
2776 | ret = set_ust_app_event_filter(ua_sess, ua_event, bytecode, app); | |
2777 | if (ret < 0) { | |
2778 | continue; | |
2779 | } | |
2780 | } | |
2781 | ||
2782 | rcu_read_unlock(); | |
2783 | return ret; | |
2784 | } | |
2785 | ||
76d45b40 DG |
2786 | /* |
2787 | * Enable event for a channel from a UST session for a specific PID. | |
2788 | */ | |
2789 | int ust_app_enable_event_pid(struct ltt_ust_session *usess, | |
2790 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) | |
2791 | { | |
2792 | int ret = 0; | |
bec39940 DG |
2793 | struct lttng_ht_iter iter; |
2794 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
76d45b40 DG |
2795 | struct ust_app *app; |
2796 | struct ust_app_session *ua_sess; | |
2797 | struct ust_app_channel *ua_chan; | |
2798 | struct ust_app_event *ua_event; | |
2799 | ||
2800 | DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid); | |
2801 | ||
2802 | rcu_read_lock(); | |
2803 | ||
2804 | app = ust_app_find_by_pid(pid); | |
2805 | if (app == NULL) { | |
2806 | ERR("UST app enable event per PID %d not found", pid); | |
2807 | ret = -1; | |
2808 | goto error; | |
2809 | } | |
2810 | ||
e0c7ec2b DG |
2811 | if (!app->compatible) { |
2812 | ret = 0; | |
2813 | goto error; | |
2814 | } | |
2815 | ||
76d45b40 DG |
2816 | ua_sess = lookup_session_by_app(usess, app); |
2817 | /* If ua_sess is NULL, there is a code flow error */ | |
2818 | assert(ua_sess); | |
2819 | ||
2820 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2821 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2822 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
76d45b40 DG |
2823 | /* If the channel is not found, there is a code flow error */ |
2824 | assert(ua_chan_node); | |
2825 | ||
2826 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2827 | ||
bec39940 DG |
2828 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter); |
2829 | ua_event_node = lttng_ht_iter_get_node_str(&iter); | |
76d45b40 DG |
2830 | if (ua_event_node == NULL) { |
2831 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); | |
2832 | if (ret < 0) { | |
2833 | goto error; | |
2834 | } | |
2835 | } else { | |
2836 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
2837 | ||
2838 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
2839 | if (ret < 0) { | |
2840 | goto error; | |
2841 | } | |
2842 | } | |
2843 | ||
2844 | error: | |
2845 | rcu_read_unlock(); | |
2846 | return ret; | |
2847 | } | |
7f79d3a1 DG |
2848 | |
2849 | /* | |
2850 | * Disable event for a channel from a UST session for a specific PID. | |
2851 | */ | |
2852 | int ust_app_disable_event_pid(struct ltt_ust_session *usess, | |
2853 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) | |
2854 | { | |
2855 | int ret = 0; | |
bec39940 DG |
2856 | struct lttng_ht_iter iter; |
2857 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
7f79d3a1 DG |
2858 | struct ust_app *app; |
2859 | struct ust_app_session *ua_sess; | |
2860 | struct ust_app_channel *ua_chan; | |
2861 | struct ust_app_event *ua_event; | |
2862 | ||
2863 | DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid); | |
2864 | ||
2865 | rcu_read_lock(); | |
2866 | ||
2867 | app = ust_app_find_by_pid(pid); | |
2868 | if (app == NULL) { | |
2869 | ERR("UST app disable event per PID %d not found", pid); | |
2870 | ret = -1; | |
2871 | goto error; | |
2872 | } | |
2873 | ||
e0c7ec2b DG |
2874 | if (!app->compatible) { |
2875 | ret = 0; | |
2876 | goto error; | |
2877 | } | |
2878 | ||
7f79d3a1 DG |
2879 | ua_sess = lookup_session_by_app(usess, app); |
2880 | /* If ua_sess is NULL, there is a code flow error */ | |
2881 | assert(ua_sess); | |
2882 | ||
2883 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2884 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2885 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
7f79d3a1 DG |
2886 | if (ua_chan_node == NULL) { |
2887 | /* Channel does not exist, skip disabling */ | |
2888 | goto error; | |
2889 | } | |
2890 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2891 | ||
bec39940 DG |
2892 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter); |
2893 | ua_event_node = lttng_ht_iter_get_node_str(&iter); | |
7f79d3a1 DG |
2894 | if (ua_event_node == NULL) { |
2895 | /* Event does not exist, skip disabling */ | |
2896 | goto error; | |
2897 | } | |
2898 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
2899 | ||
2900 | ret = disable_ust_app_event(ua_sess, ua_event, app); | |
2901 | if (ret < 0) { | |
2902 | goto error; | |
2903 | } | |
2904 | ||
2905 | error: | |
2906 | rcu_read_unlock(); | |
2907 | return ret; | |
2908 | } | |
e0c7ec2b DG |
2909 | |
2910 | /* | |
2911 | * Validate version of UST apps and set the compatible bit. | |
2912 | */ | |
2913 | int ust_app_validate_version(int sock) | |
2914 | { | |
2915 | int ret; | |
2916 | struct ust_app *app; | |
2917 | ||
2918 | rcu_read_lock(); | |
2919 | ||
2920 | app = find_app_by_sock(sock); | |
2921 | assert(app); | |
2922 | ||
86acf0da DG |
2923 | health_code_update(&health_thread_cmd); |
2924 | ||
e0c7ec2b DG |
2925 | ret = ustctl_tracer_version(sock, &app->version); |
2926 | if (ret < 0) { | |
2927 | goto error; | |
2928 | } | |
2929 | ||
2930 | /* Validate version */ | |
aee0cea0 | 2931 | if (app->version.major != UST_APP_MAJOR_VERSION) { |
e0c7ec2b DG |
2932 | goto error; |
2933 | } | |
2934 | ||
68264071 | 2935 | DBG2("UST app PID %d is compatible with internal major version %d " |
aee0cea0 | 2936 | "(supporting == %d)", app->pid, app->version.major, |
e0c7ec2b DG |
2937 | UST_APP_MAJOR_VERSION); |
2938 | app->compatible = 1; | |
2939 | rcu_read_unlock(); | |
86acf0da | 2940 | health_code_update(&health_thread_cmd); |
e0c7ec2b DG |
2941 | return 0; |
2942 | ||
2943 | error: | |
68264071 | 2944 | DBG2("UST app PID %d is not compatible with internal major version %d " |
aee0cea0 | 2945 | "(supporting == %d)", app->pid, app->version.major, |
e0c7ec2b DG |
2946 | UST_APP_MAJOR_VERSION); |
2947 | app->compatible = 0; | |
2948 | rcu_read_unlock(); | |
86acf0da | 2949 | health_code_update(&health_thread_cmd); |
e0c7ec2b DG |
2950 | return -1; |
2951 | } | |
4466912f DG |
2952 | |
2953 | /* | |
2954 | * Calibrate registered applications. | |
2955 | */ | |
2956 | int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) | |
2957 | { | |
2958 | int ret = 0; | |
2959 | struct lttng_ht_iter iter; | |
2960 | struct ust_app *app; | |
2961 | ||
2962 | rcu_read_lock(); | |
2963 | ||
852d0037 | 2964 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
4466912f DG |
2965 | if (!app->compatible) { |
2966 | /* | |
2967 | * TODO: In time, we should notice the caller of this error by | |
2968 | * telling him that this is a version error. | |
2969 | */ | |
2970 | continue; | |
2971 | } | |
2972 | ||
86acf0da DG |
2973 | health_code_update(&health_thread_cmd); |
2974 | ||
852d0037 | 2975 | ret = ustctl_calibrate(app->sock, calibrate); |
4466912f DG |
2976 | if (ret < 0) { |
2977 | switch (ret) { | |
2978 | case -ENOSYS: | |
2979 | /* Means that it's not implemented on the tracer side. */ | |
2980 | ret = 0; | |
2981 | break; | |
2982 | default: | |
2983 | /* TODO: Report error to user */ | |
2984 | DBG2("Calibrate app PID %d returned with error %d", | |
852d0037 | 2985 | app->pid, ret); |
4466912f DG |
2986 | break; |
2987 | } | |
2988 | } | |
2989 | } | |
2990 | ||
2991 | DBG("UST app global domain calibration finished"); | |
2992 | ||
2993 | rcu_read_unlock(); | |
2994 | ||
86acf0da DG |
2995 | health_code_update(&health_thread_cmd); |
2996 | ||
4466912f DG |
2997 | return ret; |
2998 | } |