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