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