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 | /* | |
5b4a0ec0 | 724 | * Create stream onto the UST tracer for a UST session. |
91d76f53 | 725 | */ |
5b4a0ec0 DG |
726 | static int create_ust_stream(struct ust_app *app, |
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) { | |
742 | ERR("UST create metadata stream failed"); | |
421cb601 | 743 | goto error; |
91d76f53 | 744 | } |
421cb601 | 745 | |
421cb601 | 746 | error: |
86acf0da | 747 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 748 | return ret; |
91d76f53 DG |
749 | } |
750 | ||
b551a063 | 751 | /* |
5b4a0ec0 | 752 | * Create the specified channel onto the UST tracer for a UST session. |
b551a063 | 753 | */ |
5b4a0ec0 DG |
754 | static int create_ust_channel(struct ust_app *app, |
755 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
b551a063 | 756 | { |
5b4a0ec0 | 757 | int ret; |
b551a063 | 758 | |
86acf0da DG |
759 | health_code_update(&health_thread_cmd); |
760 | ||
5b4a0ec0 | 761 | /* TODO: remove cast and use lttng-ust-abi.h */ |
4063050c MD |
762 | |
763 | /* We are going to receive 2 fds, we need to reserve them. */ | |
764 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
765 | if (ret < 0) { | |
766 | ERR("Exhausted number of available FD upon create channel"); | |
767 | goto error; | |
768 | } | |
86acf0da DG |
769 | |
770 | health_code_update(&health_thread_cmd); | |
771 | ||
852d0037 | 772 | ret = ustctl_create_channel(app->sock, ua_sess->handle, |
5b4a0ec0 DG |
773 | (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj); |
774 | if (ret < 0) { | |
58f3ca76 | 775 | ERR("Creating channel %s for app (pid: %d, sock: %d) " |
5b4a0ec0 | 776 | "and session handle %d with ret %d", |
852d0037 | 777 | ua_chan->name, app->pid, app->sock, |
5b4a0ec0 | 778 | ua_sess->handle, ret); |
4063050c | 779 | lttng_fd_put(LTTNG_FD_APPS, 2); |
b551a063 DG |
780 | goto error; |
781 | } | |
782 | ||
5b4a0ec0 | 783 | ua_chan->handle = ua_chan->obj->handle; |
b551a063 | 784 | |
5b4a0ec0 | 785 | DBG2("UST app channel %s created successfully for pid:%d and sock:%d", |
852d0037 | 786 | ua_chan->name, app->pid, app->sock); |
b551a063 | 787 | |
86acf0da DG |
788 | health_code_update(&health_thread_cmd); |
789 | ||
8535a6d9 DG |
790 | /* If channel is not enabled, disable it on the tracer */ |
791 | if (!ua_chan->enabled) { | |
792 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
793 | if (ret < 0) { | |
794 | goto error; | |
795 | } | |
796 | } | |
797 | ||
b551a063 | 798 | error: |
86acf0da | 799 | health_code_update(&health_thread_cmd); |
b551a063 DG |
800 | return ret; |
801 | } | |
802 | ||
91d76f53 | 803 | /* |
5b4a0ec0 | 804 | * Create the specified event onto the UST tracer for a UST session. |
91d76f53 | 805 | */ |
edb67388 DG |
806 | static |
807 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
808 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) | |
91d76f53 | 809 | { |
5b4a0ec0 | 810 | int ret = 0; |
284d8f55 | 811 | |
86acf0da DG |
812 | health_code_update(&health_thread_cmd); |
813 | ||
5b4a0ec0 | 814 | /* Create UST event on tracer */ |
852d0037 | 815 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 DG |
816 | &ua_event->obj); |
817 | if (ret < 0) { | |
818 | ERR("Error ustctl create event %s for app pid: %d with ret %d", | |
852d0037 | 819 | ua_event->attr.name, app->pid, ret); |
5b4a0ec0 | 820 | goto error; |
91d76f53 | 821 | } |
f6a9efaa | 822 | |
5b4a0ec0 | 823 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 824 | |
5b4a0ec0 | 825 | DBG2("UST app event %s created successfully for pid:%d", |
852d0037 | 826 | ua_event->attr.name, app->pid); |
f6a9efaa | 827 | |
86acf0da DG |
828 | health_code_update(&health_thread_cmd); |
829 | ||
025faf73 DG |
830 | /* Set filter if one is present. */ |
831 | if (ua_event->filter) { | |
832 | ret = set_ust_event_filter(ua_event, app); | |
833 | if (ret < 0) { | |
834 | goto error; | |
835 | } | |
836 | } | |
837 | ||
8535a6d9 | 838 | /* If event not enabled, disable it on the tracer */ |
fc34caaa | 839 | if (ua_event->enabled == 0) { |
8535a6d9 DG |
840 | ret = disable_ust_event(app, ua_sess, ua_event); |
841 | if (ret < 0) { | |
fc34caaa DG |
842 | /* |
843 | * If we hit an EPERM, something is wrong with our disable call. If | |
844 | * we get an EEXIST, there is a problem on the tracer side since we | |
845 | * just created it. | |
846 | */ | |
847 | switch (ret) { | |
49c336c1 | 848 | case -LTTNG_UST_ERR_PERM: |
fc34caaa DG |
849 | /* Code flow problem */ |
850 | assert(0); | |
49c336c1 | 851 | case -LTTNG_UST_ERR_EXIST: |
fc34caaa DG |
852 | /* It's OK for our use case. */ |
853 | ret = 0; | |
854 | break; | |
855 | default: | |
856 | break; | |
857 | } | |
8535a6d9 DG |
858 | goto error; |
859 | } | |
860 | } | |
861 | ||
5b4a0ec0 | 862 | error: |
86acf0da | 863 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 864 | return ret; |
91d76f53 | 865 | } |
48842b30 | 866 | |
5b4a0ec0 DG |
867 | /* |
868 | * Copy data between an UST app event and a LTT event. | |
869 | */ | |
421cb601 | 870 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
871 | struct ltt_ust_event *uevent) |
872 | { | |
873 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); | |
874 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
875 | ||
fc34caaa DG |
876 | ua_event->enabled = uevent->enabled; |
877 | ||
5b4a0ec0 DG |
878 | /* Copy event attributes */ |
879 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
880 | ||
53a80697 MD |
881 | /* Copy filter bytecode */ |
882 | if (uevent->filter) { | |
025faf73 DG |
883 | ua_event->filter = alloc_copy_ust_app_filter(uevent->filter); |
884 | /* Filter might be NULL here in case of ENONEM. */ | |
53a80697 | 885 | } |
48842b30 DG |
886 | } |
887 | ||
5b4a0ec0 DG |
888 | /* |
889 | * Copy data between an UST app channel and a LTT channel. | |
890 | */ | |
421cb601 | 891 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
892 | struct ltt_ust_channel *uchan) |
893 | { | |
bec39940 | 894 | struct lttng_ht_iter iter; |
48842b30 | 895 | struct ltt_ust_event *uevent; |
55cc08a6 | 896 | struct ltt_ust_context *uctx; |
48842b30 | 897 | struct ust_app_event *ua_event; |
55cc08a6 | 898 | struct ust_app_ctx *ua_ctx; |
48842b30 | 899 | |
fc34caaa | 900 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
901 | |
902 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
903 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
5b4a0ec0 DG |
904 | /* Copy event attributes */ |
905 | memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr)); | |
48842b30 | 906 | |
fc34caaa DG |
907 | ua_chan->enabled = uchan->enabled; |
908 | ||
bec39940 | 909 | cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) { |
55cc08a6 DG |
910 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
911 | if (ua_ctx == NULL) { | |
912 | continue; | |
913 | } | |
bec39940 DG |
914 | lttng_ht_node_init_ulong(&ua_ctx->node, |
915 | (unsigned long) ua_ctx->ctx.ctx); | |
916 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); | |
55cc08a6 | 917 | } |
48842b30 | 918 | |
421cb601 | 919 | /* Copy all events from ltt ust channel to ust app channel */ |
bec39940 | 920 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
18eace3b DG |
921 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
922 | uevent->filter, uevent->attr.loglevel); | |
923 | if (ua_event == NULL) { | |
421cb601 | 924 | DBG2("UST event %s not found on shadow copy channel", |
48842b30 | 925 | uevent->attr.name); |
284d8f55 | 926 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
48842b30 | 927 | if (ua_event == NULL) { |
5b4a0ec0 | 928 | continue; |
48842b30 | 929 | } |
421cb601 | 930 | shadow_copy_event(ua_event, uevent); |
18eace3b | 931 | add_unique_ust_app_event(ua_chan->events, ua_event); |
48842b30 | 932 | } |
48842b30 DG |
933 | } |
934 | ||
fc34caaa | 935 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
936 | } |
937 | ||
5b4a0ec0 DG |
938 | /* |
939 | * Copy data between a UST app session and a regular LTT session. | |
940 | */ | |
421cb601 | 941 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 942 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 943 | { |
bec39940 DG |
944 | struct lttng_ht_node_str *ua_chan_node; |
945 | struct lttng_ht_iter iter; | |
48842b30 DG |
946 | struct ltt_ust_channel *uchan; |
947 | struct ust_app_channel *ua_chan; | |
477d7741 MD |
948 | time_t rawtime; |
949 | struct tm *timeinfo; | |
950 | char datetime[16]; | |
951 | int ret; | |
952 | ||
953 | /* Get date and time for unique app path */ | |
954 | time(&rawtime); | |
955 | timeinfo = localtime(&rawtime); | |
956 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
48842b30 | 957 | |
421cb601 | 958 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 959 | |
a991f516 | 960 | ua_sess->id = usess->id; |
6df2e2c9 MD |
961 | ua_sess->uid = usess->uid; |
962 | ua_sess->gid = usess->gid; | |
48842b30 | 963 | |
00e2e675 DG |
964 | ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid, |
965 | datetime); | |
477d7741 MD |
966 | if (ret < 0) { |
967 | PERROR("asprintf UST shadow copy session"); | |
968 | /* TODO: We cannot return an error from here.. */ | |
969 | assert(0); | |
970 | } | |
971 | ||
48842b30 DG |
972 | /* TODO: support all UST domain */ |
973 | ||
974 | /* Iterate over all channels in global domain. */ | |
bec39940 DG |
975 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
976 | uchan, node.node) { | |
977 | struct lttng_ht_iter uiter; | |
ba767faf | 978 | |
bec39940 DG |
979 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
980 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
5b4a0ec0 | 981 | if (ua_chan_node != NULL) { |
fc34caaa | 982 | /* Session exist. Contiuing. */ |
5b4a0ec0 DG |
983 | continue; |
984 | } | |
421cb601 | 985 | |
5b4a0ec0 DG |
986 | DBG2("Channel %s not found on shadow session copy, creating it", |
987 | uchan->name); | |
988 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); | |
989 | if (ua_chan == NULL) { | |
fc34caaa | 990 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
5b4a0ec0 | 991 | continue; |
48842b30 DG |
992 | } |
993 | ||
5b4a0ec0 | 994 | shadow_copy_channel(ua_chan, uchan); |
bec39940 | 995 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
48842b30 DG |
996 | } |
997 | } | |
998 | ||
78f0bacd DG |
999 | /* |
1000 | * Lookup sesison wrapper. | |
1001 | */ | |
84cd17c6 MD |
1002 | static |
1003 | void __lookup_session_by_app(struct ltt_ust_session *usess, | |
bec39940 | 1004 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
1005 | { |
1006 | /* Get right UST app session from app */ | |
2c348c10 | 1007 | lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter); |
84cd17c6 MD |
1008 | } |
1009 | ||
421cb601 DG |
1010 | /* |
1011 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 1012 | * id. |
421cb601 | 1013 | */ |
48842b30 DG |
1014 | static struct ust_app_session *lookup_session_by_app( |
1015 | struct ltt_ust_session *usess, struct ust_app *app) | |
1016 | { | |
bec39940 DG |
1017 | struct lttng_ht_iter iter; |
1018 | struct lttng_ht_node_ulong *node; | |
48842b30 | 1019 | |
84cd17c6 | 1020 | __lookup_session_by_app(usess, app, &iter); |
bec39940 | 1021 | node = lttng_ht_iter_get_node_ulong(&iter); |
48842b30 DG |
1022 | if (node == NULL) { |
1023 | goto error; | |
1024 | } | |
1025 | ||
1026 | return caa_container_of(node, struct ust_app_session, node); | |
1027 | ||
1028 | error: | |
1029 | return NULL; | |
1030 | } | |
1031 | ||
421cb601 DG |
1032 | /* |
1033 | * Create a UST session onto the tracer of app and add it the session | |
1034 | * hashtable. | |
1035 | * | |
1036 | * Return ust app session or NULL on error. | |
1037 | */ | |
1038 | static struct ust_app_session *create_ust_app_session( | |
1039 | struct ltt_ust_session *usess, struct ust_app *app) | |
1040 | { | |
421cb601 DG |
1041 | struct ust_app_session *ua_sess; |
1042 | ||
86acf0da DG |
1043 | health_code_update(&health_thread_cmd); |
1044 | ||
421cb601 DG |
1045 | ua_sess = lookup_session_by_app(usess, app); |
1046 | if (ua_sess == NULL) { | |
a991f516 | 1047 | DBG2("UST app pid: %d session id %d not found, creating it", |
852d0037 | 1048 | app->pid, usess->id); |
421cb601 DG |
1049 | ua_sess = alloc_ust_app_session(); |
1050 | if (ua_sess == NULL) { | |
1051 | /* Only malloc can failed so something is really wrong */ | |
fc34caaa | 1052 | goto end; |
421cb601 | 1053 | } |
477d7741 | 1054 | shadow_copy_session(ua_sess, usess, app); |
421cb601 DG |
1055 | } |
1056 | ||
86acf0da DG |
1057 | health_code_update(&health_thread_cmd); |
1058 | ||
421cb601 | 1059 | if (ua_sess->handle == -1) { |
c617c0c6 MD |
1060 | int ret; |
1061 | ||
852d0037 | 1062 | ret = ustctl_create_session(app->sock); |
421cb601 | 1063 | if (ret < 0) { |
852d0037 | 1064 | ERR("Creating session for app pid %d", app->pid); |
0f83395d | 1065 | delete_ust_app_session(-1, ua_sess); |
915d047c DG |
1066 | /* This means that the tracer is gone... */ |
1067 | ua_sess = (void*) -1UL; | |
0f83395d | 1068 | goto end; |
421cb601 DG |
1069 | } |
1070 | ||
421cb601 DG |
1071 | ua_sess->handle = ret; |
1072 | ||
1073 | /* Add ust app session to app's HT */ | |
2c348c10 | 1074 | lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id); |
bec39940 | 1075 | lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node); |
421cb601 DG |
1076 | |
1077 | DBG2("UST app session created successfully with handle %d", ret); | |
1078 | } | |
1079 | ||
fc34caaa | 1080 | end: |
86acf0da | 1081 | health_code_update(&health_thread_cmd); |
421cb601 | 1082 | return ua_sess; |
421cb601 DG |
1083 | } |
1084 | ||
55cc08a6 DG |
1085 | /* |
1086 | * Create a context for the channel on the tracer. | |
1087 | */ | |
1088 | static | |
1089 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, | |
1090 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, | |
1091 | struct ust_app *app) | |
1092 | { | |
1093 | int ret = 0; | |
bec39940 DG |
1094 | struct lttng_ht_iter iter; |
1095 | struct lttng_ht_node_ulong *node; | |
55cc08a6 DG |
1096 | struct ust_app_ctx *ua_ctx; |
1097 | ||
1098 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
1099 | ||
bec39940 DG |
1100 | lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter); |
1101 | node = lttng_ht_iter_get_node_ulong(&iter); | |
55cc08a6 DG |
1102 | if (node != NULL) { |
1103 | ret = -EEXIST; | |
1104 | goto error; | |
1105 | } | |
1106 | ||
1107 | ua_ctx = alloc_ust_app_ctx(uctx); | |
1108 | if (ua_ctx == NULL) { | |
1109 | /* malloc failed */ | |
1110 | ret = -1; | |
1111 | goto error; | |
1112 | } | |
1113 | ||
bec39940 DG |
1114 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); |
1115 | lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node); | |
55cc08a6 DG |
1116 | |
1117 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
1118 | if (ret < 0) { | |
1119 | goto error; | |
1120 | } | |
1121 | ||
1122 | error: | |
1123 | return ret; | |
1124 | } | |
1125 | ||
edb67388 DG |
1126 | /* |
1127 | * Enable on the tracer side a ust app event for the session and channel. | |
1128 | */ | |
1129 | static | |
1130 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
35a9059d | 1131 | struct ust_app_event *ua_event, struct ust_app *app) |
edb67388 DG |
1132 | { |
1133 | int ret; | |
1134 | ||
1135 | ret = enable_ust_event(app, ua_sess, ua_event); | |
1136 | if (ret < 0) { | |
1137 | goto error; | |
1138 | } | |
1139 | ||
1140 | ua_event->enabled = 1; | |
1141 | ||
1142 | error: | |
1143 | return ret; | |
1144 | } | |
1145 | ||
9730260e DG |
1146 | /* |
1147 | * Disable on the tracer side a ust app event for the session and channel. | |
1148 | */ | |
1149 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
7f79d3a1 | 1150 | struct ust_app_event *ua_event, struct ust_app *app) |
9730260e DG |
1151 | { |
1152 | int ret; | |
1153 | ||
1154 | ret = disable_ust_event(app, ua_sess, ua_event); | |
1155 | if (ret < 0) { | |
1156 | goto error; | |
1157 | } | |
1158 | ||
1159 | ua_event->enabled = 0; | |
1160 | ||
1161 | error: | |
1162 | return ret; | |
1163 | } | |
1164 | ||
78f0bacd DG |
1165 | /* |
1166 | * Lookup ust app channel for session and disable it on the tracer side. | |
1167 | */ | |
8535a6d9 DG |
1168 | static |
1169 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
1170 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
78f0bacd | 1171 | { |
8535a6d9 | 1172 | int ret; |
78f0bacd DG |
1173 | |
1174 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
1175 | if (ret < 0) { | |
1176 | goto error; | |
1177 | } | |
1178 | ||
8535a6d9 DG |
1179 | ua_chan->enabled = 0; |
1180 | ||
78f0bacd DG |
1181 | error: |
1182 | return ret; | |
1183 | } | |
1184 | ||
1185 | /* | |
1186 | * Lookup ust app channel for session and enable it on the tracer side. | |
1187 | */ | |
1188 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
1189 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
1190 | { | |
1191 | int ret = 0; | |
bec39940 DG |
1192 | struct lttng_ht_iter iter; |
1193 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
1194 | struct ust_app_channel *ua_chan; |
1195 | ||
bec39940 DG |
1196 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
1197 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
78f0bacd | 1198 | if (ua_chan_node == NULL) { |
a991f516 MD |
1199 | DBG2("Unable to find channel %s in ust session id %u", |
1200 | uchan->name, ua_sess->id); | |
78f0bacd DG |
1201 | goto error; |
1202 | } | |
1203 | ||
1204 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1205 | ||
1206 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
1207 | if (ret < 0) { | |
1208 | goto error; | |
1209 | } | |
1210 | ||
1211 | error: | |
1212 | return ret; | |
1213 | } | |
1214 | ||
284d8f55 | 1215 | /* |
4d710ac2 DG |
1216 | * Create UST app channel and create it on the tracer. Set ua_chanp of the |
1217 | * newly created channel if not NULL. | |
284d8f55 | 1218 | */ |
4d710ac2 DG |
1219 | static int create_ust_app_channel(struct ust_app_session *ua_sess, |
1220 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
1221 | struct ust_app_channel **ua_chanp) | |
5b4a0ec0 DG |
1222 | { |
1223 | int ret = 0; | |
bec39940 DG |
1224 | struct lttng_ht_iter iter; |
1225 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
1226 | struct ust_app_channel *ua_chan; |
1227 | ||
1228 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
1229 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
1230 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 1231 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 1232 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 1233 | goto end; |
5b4a0ec0 DG |
1234 | } |
1235 | ||
fc34caaa DG |
1236 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); |
1237 | if (ua_chan == NULL) { | |
1238 | /* Only malloc can fail here */ | |
4d710ac2 | 1239 | ret = -ENOMEM; |
fc34caaa DG |
1240 | goto error; |
1241 | } | |
1242 | shadow_copy_channel(ua_chan, uchan); | |
1243 | ||
5b4a0ec0 DG |
1244 | ret = create_ust_channel(app, ua_sess, ua_chan); |
1245 | if (ret < 0) { | |
fc34caaa | 1246 | /* Not found previously means that it does not exist on the tracer */ |
49c336c1 | 1247 | assert(ret != -LTTNG_UST_ERR_EXIST); |
5b4a0ec0 DG |
1248 | goto error; |
1249 | } | |
1250 | ||
4d710ac2 | 1251 | /* Only add the channel if successful on the tracer side. */ |
58f3ca76 DG |
1252 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
1253 | ||
fc34caaa | 1254 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
852d0037 | 1255 | app->pid); |
fc34caaa DG |
1256 | |
1257 | end: | |
4d710ac2 DG |
1258 | if (ua_chanp) { |
1259 | *ua_chanp = ua_chan; | |
1260 | } | |
1261 | ||
1262 | /* Everything went well. */ | |
1263 | return 0; | |
5b4a0ec0 DG |
1264 | |
1265 | error: | |
fc34caaa | 1266 | delete_ust_app_channel(-1, ua_chan); |
4d710ac2 | 1267 | return ret; |
5b4a0ec0 DG |
1268 | } |
1269 | ||
1270 | /* | |
1271 | * Create UST app event and create it on the tracer side. | |
1272 | */ | |
edb67388 DG |
1273 | static |
1274 | int create_ust_app_event(struct ust_app_session *ua_sess, | |
1275 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, | |
1276 | struct ust_app *app) | |
284d8f55 | 1277 | { |
edb67388 | 1278 | int ret = 0; |
5b4a0ec0 | 1279 | struct ust_app_event *ua_event; |
284d8f55 | 1280 | |
5b4a0ec0 | 1281 | /* Get event node */ |
18eace3b DG |
1282 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
1283 | uevent->filter, uevent->attr.loglevel); | |
1284 | if (ua_event != NULL) { | |
fc34caaa | 1285 | ret = -EEXIST; |
edb67388 DG |
1286 | goto end; |
1287 | } | |
5b4a0ec0 | 1288 | |
edb67388 DG |
1289 | /* Does not exist so create one */ |
1290 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
1291 | if (ua_event == NULL) { | |
1292 | /* Only malloc can failed so something is really wrong */ | |
1293 | ret = -ENOMEM; | |
fc34caaa | 1294 | goto end; |
5b4a0ec0 | 1295 | } |
edb67388 | 1296 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 1297 | |
edb67388 | 1298 | /* Create it on the tracer side */ |
5b4a0ec0 | 1299 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 1300 | if (ret < 0) { |
fc34caaa | 1301 | /* Not found previously means that it does not exist on the tracer */ |
76f66f63 | 1302 | assert(ret != -LTTNG_UST_ERR_EXIST); |
284d8f55 DG |
1303 | goto error; |
1304 | } | |
1305 | ||
18eace3b | 1306 | add_unique_ust_app_event(ua_chan->events, ua_event); |
284d8f55 | 1307 | |
fc34caaa | 1308 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
852d0037 | 1309 | app->pid); |
7f79d3a1 | 1310 | |
edb67388 | 1311 | end: |
fc34caaa DG |
1312 | return ret; |
1313 | ||
5b4a0ec0 | 1314 | error: |
fc34caaa DG |
1315 | /* Valid. Calling here is already in a read side lock */ |
1316 | delete_ust_app_event(-1, ua_event); | |
edb67388 | 1317 | return ret; |
5b4a0ec0 DG |
1318 | } |
1319 | ||
1320 | /* | |
1321 | * Create UST metadata and open it on the tracer side. | |
1322 | */ | |
1323 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
1324 | char *pathname, struct ust_app *app) | |
1325 | { | |
1326 | int ret = 0; | |
1327 | ||
1328 | if (ua_sess->metadata == NULL) { | |
1329 | /* Allocate UST metadata */ | |
1330 | ua_sess->metadata = trace_ust_create_metadata(pathname); | |
1331 | if (ua_sess->metadata == NULL) { | |
fc34caaa | 1332 | /* malloc() failed */ |
5b4a0ec0 DG |
1333 | goto error; |
1334 | } | |
1335 | ||
1336 | ret = open_ust_metadata(app, ua_sess); | |
1337 | if (ret < 0) { | |
7db205b5 DG |
1338 | DBG3("Opening metadata failed. Cleaning up memory"); |
1339 | ||
fc34caaa DG |
1340 | /* Cleanup failed metadata struct */ |
1341 | free(ua_sess->metadata); | |
7db205b5 DG |
1342 | /* |
1343 | * This is very important because delete_ust_app_session check if | |
1344 | * the pointer is null or not in order to delete the metadata. | |
1345 | */ | |
1346 | ua_sess->metadata = NULL; | |
5b4a0ec0 DG |
1347 | goto error; |
1348 | } | |
1349 | ||
852d0037 | 1350 | DBG2("UST metadata opened for app pid %d", app->pid); |
5b4a0ec0 DG |
1351 | } |
1352 | ||
1353 | /* Open UST metadata stream */ | |
1354 | if (ua_sess->metadata->stream_obj == NULL) { | |
1355 | ret = create_ust_stream(app, ua_sess); | |
1356 | if (ret < 0) { | |
1357 | goto error; | |
1358 | } | |
1359 | ||
477d7741 MD |
1360 | ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, |
1361 | "%s/metadata", ua_sess->path); | |
5b4a0ec0 DG |
1362 | if (ret < 0) { |
1363 | PERROR("asprintf UST create stream"); | |
1364 | goto error; | |
1365 | } | |
1366 | ||
1367 | DBG2("UST metadata stream object created for app pid %d", | |
852d0037 | 1368 | app->pid); |
5b4a0ec0 DG |
1369 | } else { |
1370 | ERR("Attempting to create stream without metadata opened"); | |
1371 | goto error; | |
1372 | } | |
1373 | ||
1374 | return 0; | |
1375 | ||
1376 | error: | |
1377 | return -1; | |
1378 | } | |
1379 | ||
1380 | /* | |
1381 | * Return pointer to traceable apps list. | |
1382 | */ | |
bec39940 | 1383 | struct lttng_ht *ust_app_get_ht(void) |
5b4a0ec0 DG |
1384 | { |
1385 | return ust_app_ht; | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * Return ust app pointer or NULL if not found. | |
1390 | */ | |
1391 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
1392 | { | |
bec39940 DG |
1393 | struct lttng_ht_node_ulong *node; |
1394 | struct lttng_ht_iter iter; | |
5b4a0ec0 DG |
1395 | |
1396 | rcu_read_lock(); | |
bec39940 DG |
1397 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
1398 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
1399 | if (node == NULL) { |
1400 | DBG2("UST app no found with pid %d", pid); | |
1401 | goto error; | |
1402 | } | |
1403 | rcu_read_unlock(); | |
1404 | ||
1405 | DBG2("Found UST app by pid %d", pid); | |
1406 | ||
852d0037 | 1407 | return caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
1408 | |
1409 | error: | |
1410 | rcu_read_unlock(); | |
1411 | return NULL; | |
1412 | } | |
1413 | ||
1414 | /* | |
1415 | * Using pid and uid (of the app), allocate a new ust_app struct and | |
1416 | * add it to the global traceable app list. | |
1417 | * | |
0df502fd MD |
1418 | * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app |
1419 | * bitness is not supported. | |
5b4a0ec0 DG |
1420 | */ |
1421 | int ust_app_register(struct ust_register_msg *msg, int sock) | |
1422 | { | |
1423 | struct ust_app *lta; | |
799e2c4f | 1424 | int ret; |
5b4a0ec0 | 1425 | |
173af62f DG |
1426 | if ((msg->bits_per_long == 64 && |
1427 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
1428 | || (msg->bits_per_long == 32 && | |
1429 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 1430 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
7753dea8 MD |
1431 | "%d-bit long, but no consumerd for this long size is available.\n", |
1432 | msg->name, msg->pid, msg->bits_per_long); | |
799e2c4f MD |
1433 | ret = close(sock); |
1434 | if (ret) { | |
1435 | PERROR("close"); | |
1436 | } | |
4063050c | 1437 | lttng_fd_put(LTTNG_FD_APPS, 1); |
0df502fd MD |
1438 | return -EINVAL; |
1439 | } | |
3f2c5fcc MD |
1440 | if (msg->major != LTTNG_UST_COMM_MAJOR) { |
1441 | ERR("Registration failed: application \"%s\" (pid: %d) has " | |
1442 | "communication protocol version %u.%u, but sessiond supports 2.x.\n", | |
1443 | msg->name, msg->pid, msg->major, msg->minor); | |
799e2c4f MD |
1444 | ret = close(sock); |
1445 | if (ret) { | |
1446 | PERROR("close"); | |
1447 | } | |
4063050c | 1448 | lttng_fd_put(LTTNG_FD_APPS, 1); |
3f2c5fcc MD |
1449 | return -EINVAL; |
1450 | } | |
5b4a0ec0 DG |
1451 | lta = zmalloc(sizeof(struct ust_app)); |
1452 | if (lta == NULL) { | |
1453 | PERROR("malloc"); | |
1454 | return -ENOMEM; | |
1455 | } | |
1456 | ||
1457 | lta->ppid = msg->ppid; | |
1458 | lta->uid = msg->uid; | |
1459 | lta->gid = msg->gid; | |
e0c7ec2b | 1460 | lta->compatible = 0; /* Not compatible until proven */ |
7753dea8 | 1461 | lta->bits_per_long = msg->bits_per_long; |
5b4a0ec0 DG |
1462 | lta->v_major = msg->major; |
1463 | lta->v_minor = msg->minor; | |
1464 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
1465 | lta->name[16] = '\0'; | |
bec39940 | 1466 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
5b4a0ec0 | 1467 | |
852d0037 DG |
1468 | lta->pid = msg->pid; |
1469 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long)lta->pid); | |
1470 | lta->sock = sock; | |
1471 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long)lta->sock); | |
5b4a0ec0 | 1472 | |
d42f20df DG |
1473 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
1474 | ||
5b4a0ec0 | 1475 | rcu_read_lock(); |
852d0037 DG |
1476 | |
1477 | /* | |
1478 | * On a re-registration, we want to kick out the previous registration of | |
1479 | * that pid | |
1480 | */ | |
1481 | lttng_ht_add_replace_ulong(ust_app_ht, <a->pid_n); | |
1482 | ||
1483 | /* | |
1484 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
1485 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
1486 | * already in the table. | |
1487 | */ | |
1488 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, <a->sock_n); | |
1489 | ||
5b4a0ec0 DG |
1490 | rcu_read_unlock(); |
1491 | ||
1492 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s" | |
852d0037 DG |
1493 | " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid, |
1494 | lta->sock, lta->name, lta->v_major, lta->v_minor); | |
5b4a0ec0 DG |
1495 | |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * Unregister app by removing it from the global traceable app list and freeing | |
1501 | * the data struct. | |
1502 | * | |
1503 | * The socket is already closed at this point so no close to sock. | |
1504 | */ | |
1505 | void ust_app_unregister(int sock) | |
1506 | { | |
1507 | struct ust_app *lta; | |
bec39940 DG |
1508 | struct lttng_ht_node_ulong *node; |
1509 | struct lttng_ht_iter iter; | |
d42f20df | 1510 | struct ust_app_session *ua_sess; |
525b0740 | 1511 | int ret; |
5b4a0ec0 DG |
1512 | |
1513 | rcu_read_lock(); | |
886459c6 | 1514 | |
5b4a0ec0 | 1515 | /* Get the node reference for a call_rcu */ |
852d0037 | 1516 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1517 | node = lttng_ht_iter_get_node_ulong(&iter); |
5b4a0ec0 | 1518 | if (node == NULL) { |
852d0037 | 1519 | ERR("Unable to find app by sock %d", sock); |
5b4a0ec0 DG |
1520 | goto error; |
1521 | } | |
284d8f55 | 1522 | |
852d0037 DG |
1523 | lta = caa_container_of(node, struct ust_app, sock_n); |
1524 | ||
1525 | DBG("PID %d unregistering with sock %d", lta->pid, sock); | |
1526 | ||
886459c6 | 1527 | /* Remove application from PID hash table */ |
852d0037 DG |
1528 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
1529 | assert(!ret); | |
1530 | ||
1531 | /* Assign second node for deletion */ | |
1532 | iter.iter.node = <a->pid_n.node; | |
1533 | ||
5b98a774 DG |
1534 | /* |
1535 | * Ignore return value since the node might have been removed before by an | |
1536 | * add replace during app registration because the PID can be reassigned by | |
1537 | * the OS. | |
1538 | */ | |
bec39940 | 1539 | ret = lttng_ht_del(ust_app_ht, &iter); |
5b98a774 DG |
1540 | if (ret) { |
1541 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
1542 | lta->pid); | |
1543 | } | |
852d0037 | 1544 | |
d42f20df DG |
1545 | /* Remove sessions so they are not visible during deletion.*/ |
1546 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, | |
1547 | node.node) { | |
1548 | ret = lttng_ht_del(lta->sessions, &iter); | |
1549 | if (ret) { | |
1550 | /* The session was already removed so scheduled for teardown. */ | |
1551 | continue; | |
1552 | } | |
1553 | ||
1554 | /* | |
1555 | * Add session to list for teardown. This is safe since at this point we | |
1556 | * are the only one using this list. | |
1557 | */ | |
1558 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); | |
1559 | } | |
1560 | ||
852d0037 DG |
1561 | /* Free memory */ |
1562 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
1563 | ||
284d8f55 | 1564 | error: |
5b4a0ec0 DG |
1565 | rcu_read_unlock(); |
1566 | return; | |
284d8f55 DG |
1567 | } |
1568 | ||
1569 | /* | |
5b4a0ec0 | 1570 | * Return traceable_app_count |
284d8f55 | 1571 | */ |
5b4a0ec0 | 1572 | unsigned long ust_app_list_count(void) |
284d8f55 | 1573 | { |
5b4a0ec0 | 1574 | unsigned long count; |
284d8f55 | 1575 | |
5b4a0ec0 | 1576 | rcu_read_lock(); |
bec39940 | 1577 | count = lttng_ht_get_count(ust_app_ht); |
5b4a0ec0 | 1578 | rcu_read_unlock(); |
284d8f55 | 1579 | |
5b4a0ec0 | 1580 | return count; |
284d8f55 DG |
1581 | } |
1582 | ||
5b4a0ec0 DG |
1583 | /* |
1584 | * Fill events array with all events name of all registered apps. | |
1585 | */ | |
1586 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 1587 | { |
5b4a0ec0 DG |
1588 | int ret, handle; |
1589 | size_t nbmem, count = 0; | |
bec39940 | 1590 | struct lttng_ht_iter iter; |
5b4a0ec0 | 1591 | struct ust_app *app; |
c617c0c6 | 1592 | struct lttng_event *tmp_event; |
421cb601 | 1593 | |
5b4a0ec0 | 1594 | nbmem = UST_APP_EVENT_LIST_SIZE; |
c617c0c6 MD |
1595 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
1596 | if (tmp_event == NULL) { | |
5b4a0ec0 DG |
1597 | PERROR("zmalloc ust app events"); |
1598 | ret = -ENOMEM; | |
421cb601 DG |
1599 | goto error; |
1600 | } | |
1601 | ||
5b4a0ec0 | 1602 | rcu_read_lock(); |
421cb601 | 1603 | |
852d0037 | 1604 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
90eaa0d2 | 1605 | struct lttng_ust_tracepoint_iter uiter; |
ac3bd9c0 | 1606 | |
86acf0da DG |
1607 | health_code_update(&health_thread_cmd); |
1608 | ||
e0c7ec2b DG |
1609 | if (!app->compatible) { |
1610 | /* | |
1611 | * TODO: In time, we should notice the caller of this error by | |
1612 | * telling him that this is a version error. | |
1613 | */ | |
1614 | continue; | |
1615 | } | |
852d0037 | 1616 | handle = ustctl_tracepoint_list(app->sock); |
5b4a0ec0 DG |
1617 | if (handle < 0) { |
1618 | ERR("UST app list events getting handle failed for app pid %d", | |
852d0037 | 1619 | app->pid); |
5b4a0ec0 DG |
1620 | continue; |
1621 | } | |
421cb601 | 1622 | |
852d0037 | 1623 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 1624 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
86acf0da | 1625 | health_code_update(&health_thread_cmd); |
815564d8 | 1626 | if (count >= nbmem) { |
d7b3776f | 1627 | /* In case the realloc fails, we free the memory */ |
c617c0c6 MD |
1628 | void *ptr; |
1629 | ||
815564d8 MD |
1630 | DBG2("Reallocating event list from %zu to %zu entries", nbmem, |
1631 | 2 * nbmem); | |
1632 | nbmem *= 2; | |
c617c0c6 MD |
1633 | ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event)); |
1634 | if (ptr == NULL) { | |
5b4a0ec0 | 1635 | PERROR("realloc ust app events"); |
c617c0c6 | 1636 | free(tmp_event); |
5b4a0ec0 DG |
1637 | ret = -ENOMEM; |
1638 | goto rcu_error; | |
1639 | } | |
c617c0c6 | 1640 | tmp_event = ptr; |
5b4a0ec0 | 1641 | } |
c617c0c6 MD |
1642 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
1643 | tmp_event[count].loglevel = uiter.loglevel; | |
1644 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; | |
1645 | tmp_event[count].pid = app->pid; | |
1646 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 1647 | count++; |
421cb601 | 1648 | } |
421cb601 DG |
1649 | } |
1650 | ||
5b4a0ec0 | 1651 | ret = count; |
c617c0c6 | 1652 | *events = tmp_event; |
421cb601 | 1653 | |
5b4a0ec0 | 1654 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 1655 | |
5b4a0ec0 DG |
1656 | rcu_error: |
1657 | rcu_read_unlock(); | |
421cb601 | 1658 | error: |
86acf0da | 1659 | health_code_update(&health_thread_cmd); |
5b4a0ec0 | 1660 | return ret; |
421cb601 DG |
1661 | } |
1662 | ||
f37d259d MD |
1663 | /* |
1664 | * Fill events array with all events name of all registered apps. | |
1665 | */ | |
1666 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
1667 | { | |
1668 | int ret, handle; | |
1669 | size_t nbmem, count = 0; | |
1670 | struct lttng_ht_iter iter; | |
1671 | struct ust_app *app; | |
c617c0c6 | 1672 | struct lttng_event_field *tmp_event; |
f37d259d MD |
1673 | |
1674 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
c617c0c6 MD |
1675 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
1676 | if (tmp_event == NULL) { | |
f37d259d MD |
1677 | PERROR("zmalloc ust app event fields"); |
1678 | ret = -ENOMEM; | |
1679 | goto error; | |
1680 | } | |
1681 | ||
1682 | rcu_read_lock(); | |
1683 | ||
1684 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
1685 | struct lttng_ust_field_iter uiter; | |
1686 | ||
86acf0da DG |
1687 | health_code_update(&health_thread_cmd); |
1688 | ||
f37d259d MD |
1689 | if (!app->compatible) { |
1690 | /* | |
1691 | * TODO: In time, we should notice the caller of this error by | |
1692 | * telling him that this is a version error. | |
1693 | */ | |
1694 | continue; | |
1695 | } | |
1696 | handle = ustctl_tracepoint_field_list(app->sock); | |
1697 | if (handle < 0) { | |
1698 | ERR("UST app list event fields getting handle failed for app pid %d", | |
1699 | app->pid); | |
1700 | continue; | |
1701 | } | |
1702 | ||
1703 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, | |
fb54cdbf | 1704 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
86acf0da | 1705 | health_code_update(&health_thread_cmd); |
f37d259d | 1706 | if (count >= nbmem) { |
d7b3776f | 1707 | /* In case the realloc fails, we free the memory */ |
c617c0c6 MD |
1708 | void *ptr; |
1709 | ||
f37d259d MD |
1710 | DBG2("Reallocating event field list from %zu to %zu entries", nbmem, |
1711 | 2 * nbmem); | |
1712 | nbmem *= 2; | |
c617c0c6 MD |
1713 | ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field)); |
1714 | if (ptr == NULL) { | |
f37d259d | 1715 | PERROR("realloc ust app event fields"); |
c617c0c6 | 1716 | free(tmp_event); |
f37d259d MD |
1717 | ret = -ENOMEM; |
1718 | goto rcu_error; | |
1719 | } | |
c617c0c6 | 1720 | tmp_event = ptr; |
f37d259d | 1721 | } |
f37d259d | 1722 | |
c617c0c6 MD |
1723 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
1724 | tmp_event[count].type = uiter.type; | |
1725 | tmp_event[count].nowrite = uiter.nowrite; | |
f37d259d | 1726 | |
c617c0c6 MD |
1727 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
1728 | tmp_event[count].event.loglevel = uiter.loglevel; | |
1729 | tmp_event[count].event.type = LTTNG_UST_TRACEPOINT; | |
1730 | tmp_event[count].event.pid = app->pid; | |
1731 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
1732 | count++; |
1733 | } | |
1734 | } | |
1735 | ||
1736 | ret = count; | |
c617c0c6 | 1737 | *fields = tmp_event; |
f37d259d MD |
1738 | |
1739 | DBG2("UST app list event fields done (%zu events)", count); | |
1740 | ||
1741 | rcu_error: | |
1742 | rcu_read_unlock(); | |
1743 | error: | |
86acf0da | 1744 | health_code_update(&health_thread_cmd); |
f37d259d MD |
1745 | return ret; |
1746 | } | |
1747 | ||
5b4a0ec0 DG |
1748 | /* |
1749 | * Free and clean all traceable apps of the global list. | |
1750 | */ | |
1751 | void ust_app_clean_list(void) | |
421cb601 | 1752 | { |
5b4a0ec0 | 1753 | int ret; |
659ed79f | 1754 | struct ust_app *app; |
bec39940 | 1755 | struct lttng_ht_iter iter; |
421cb601 | 1756 | |
5b4a0ec0 | 1757 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 1758 | |
5b4a0ec0 | 1759 | rcu_read_lock(); |
421cb601 | 1760 | |
659ed79f | 1761 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 1762 | ret = lttng_ht_del(ust_app_ht, &iter); |
525b0740 | 1763 | assert(!ret); |
659ed79f | 1764 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
421cb601 DG |
1765 | } |
1766 | ||
852d0037 | 1767 | /* Cleanup socket hash table */ |
659ed79f DG |
1768 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, |
1769 | sock_n.node) { | |
852d0037 | 1770 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); |
bec39940 DG |
1771 | assert(!ret); |
1772 | } | |
852d0037 | 1773 | |
bec39940 | 1774 | /* Destroy is done only when the ht is empty */ |
852d0037 DG |
1775 | lttng_ht_destroy(ust_app_ht); |
1776 | lttng_ht_destroy(ust_app_ht_by_sock); | |
421cb601 | 1777 | |
5b4a0ec0 DG |
1778 | rcu_read_unlock(); |
1779 | } | |
1780 | ||
1781 | /* | |
1782 | * Init UST app hash table. | |
1783 | */ | |
1784 | void ust_app_ht_alloc(void) | |
1785 | { | |
bec39940 | 1786 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
852d0037 | 1787 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
421cb601 DG |
1788 | } |
1789 | ||
78f0bacd DG |
1790 | /* |
1791 | * For a specific UST session, disable the channel for all registered apps. | |
1792 | */ | |
35a9059d | 1793 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
1794 | struct ltt_ust_channel *uchan) |
1795 | { | |
1796 | int ret = 0; | |
bec39940 DG |
1797 | struct lttng_ht_iter iter; |
1798 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
1799 | struct ust_app *app; |
1800 | struct ust_app_session *ua_sess; | |
8535a6d9 | 1801 | struct ust_app_channel *ua_chan; |
78f0bacd DG |
1802 | |
1803 | if (usess == NULL || uchan == NULL) { | |
1804 | ERR("Disabling UST global channel with NULL values"); | |
1805 | ret = -1; | |
1806 | goto error; | |
1807 | } | |
1808 | ||
a991f516 MD |
1809 | DBG2("UST app disabling channel %s from global domain for session id %d", |
1810 | uchan->name, usess->id); | |
78f0bacd DG |
1811 | |
1812 | rcu_read_lock(); | |
1813 | ||
1814 | /* For every registered applications */ | |
852d0037 | 1815 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 1816 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
1817 | if (!app->compatible) { |
1818 | /* | |
1819 | * TODO: In time, we should notice the caller of this error by | |
1820 | * telling him that this is a version error. | |
1821 | */ | |
1822 | continue; | |
1823 | } | |
78f0bacd DG |
1824 | ua_sess = lookup_session_by_app(usess, app); |
1825 | if (ua_sess == NULL) { | |
1826 | continue; | |
1827 | } | |
1828 | ||
8535a6d9 | 1829 | /* Get channel */ |
bec39940 DG |
1830 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1831 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 DG |
1832 | /* If the session if found for the app, the channel must be there */ |
1833 | assert(ua_chan_node); | |
1834 | ||
1835 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1836 | /* The channel must not be already disabled */ | |
1837 | assert(ua_chan->enabled == 1); | |
1838 | ||
1839 | /* Disable channel onto application */ | |
1840 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
1841 | if (ret < 0) { |
1842 | /* XXX: We might want to report this error at some point... */ | |
1843 | continue; | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | rcu_read_unlock(); | |
1848 | ||
1849 | error: | |
1850 | return ret; | |
1851 | } | |
1852 | ||
1853 | /* | |
1854 | * For a specific UST session, enable the channel for all registered apps. | |
1855 | */ | |
35a9059d | 1856 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
1857 | struct ltt_ust_channel *uchan) |
1858 | { | |
1859 | int ret = 0; | |
bec39940 | 1860 | struct lttng_ht_iter iter; |
78f0bacd DG |
1861 | struct ust_app *app; |
1862 | struct ust_app_session *ua_sess; | |
1863 | ||
1864 | if (usess == NULL || uchan == NULL) { | |
1865 | ERR("Adding UST global channel to NULL values"); | |
1866 | ret = -1; | |
1867 | goto error; | |
1868 | } | |
1869 | ||
a991f516 MD |
1870 | DBG2("UST app enabling channel %s to global domain for session id %d", |
1871 | uchan->name, usess->id); | |
78f0bacd DG |
1872 | |
1873 | rcu_read_lock(); | |
1874 | ||
1875 | /* For every registered applications */ | |
852d0037 | 1876 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1877 | if (!app->compatible) { |
1878 | /* | |
1879 | * TODO: In time, we should notice the caller of this error by | |
1880 | * telling him that this is a version error. | |
1881 | */ | |
1882 | continue; | |
1883 | } | |
78f0bacd DG |
1884 | ua_sess = lookup_session_by_app(usess, app); |
1885 | if (ua_sess == NULL) { | |
1886 | continue; | |
1887 | } | |
1888 | ||
1889 | /* Enable channel onto application */ | |
1890 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
1891 | if (ret < 0) { | |
1892 | /* XXX: We might want to report this error at some point... */ | |
1893 | continue; | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | rcu_read_unlock(); | |
1898 | ||
1899 | error: | |
1900 | return ret; | |
1901 | } | |
1902 | ||
b0a40d28 DG |
1903 | /* |
1904 | * Disable an event in a channel and for a specific session. | |
1905 | */ | |
35a9059d DG |
1906 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
1907 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
1908 | { |
1909 | int ret = 0; | |
bec39940 DG |
1910 | struct lttng_ht_iter iter, uiter; |
1911 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
b0a40d28 DG |
1912 | struct ust_app *app; |
1913 | struct ust_app_session *ua_sess; | |
1914 | struct ust_app_channel *ua_chan; | |
1915 | struct ust_app_event *ua_event; | |
1916 | ||
1917 | DBG("UST app disabling event %s for all apps in channel " | |
a991f516 | 1918 | "%s for session id %d", uevent->attr.name, uchan->name, usess->id); |
b0a40d28 DG |
1919 | |
1920 | rcu_read_lock(); | |
1921 | ||
1922 | /* For all registered applications */ | |
852d0037 | 1923 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1924 | if (!app->compatible) { |
1925 | /* | |
1926 | * TODO: In time, we should notice the caller of this error by | |
1927 | * telling him that this is a version error. | |
1928 | */ | |
1929 | continue; | |
1930 | } | |
b0a40d28 DG |
1931 | ua_sess = lookup_session_by_app(usess, app); |
1932 | if (ua_sess == NULL) { | |
1933 | /* Next app */ | |
1934 | continue; | |
1935 | } | |
1936 | ||
1937 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
1938 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1939 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 1940 | if (ua_chan_node == NULL) { |
a991f516 | 1941 | DBG2("Channel %s not found in session id %d for app pid %d." |
852d0037 | 1942 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
1943 | continue; |
1944 | } | |
1945 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
1946 | ||
bec39940 DG |
1947 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter); |
1948 | ua_event_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 DG |
1949 | if (ua_event_node == NULL) { |
1950 | DBG2("Event %s not found in channel %s for app pid %d." | |
852d0037 | 1951 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
1952 | continue; |
1953 | } | |
1954 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
1955 | ||
7f79d3a1 | 1956 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
b0a40d28 DG |
1957 | if (ret < 0) { |
1958 | /* XXX: Report error someday... */ | |
1959 | continue; | |
1960 | } | |
1961 | } | |
1962 | ||
1963 | rcu_read_unlock(); | |
1964 | ||
1965 | return ret; | |
1966 | } | |
1967 | ||
9730260e | 1968 | /* |
edb67388 | 1969 | * For a specific UST session and UST channel, the event for all |
9730260e DG |
1970 | * registered apps. |
1971 | */ | |
35a9059d | 1972 | int ust_app_disable_all_event_glb(struct ltt_ust_session *usess, |
9730260e DG |
1973 | struct ltt_ust_channel *uchan) |
1974 | { | |
1975 | int ret = 0; | |
bec39940 DG |
1976 | struct lttng_ht_iter iter, uiter; |
1977 | struct lttng_ht_node_str *ua_chan_node; | |
9730260e DG |
1978 | struct ust_app *app; |
1979 | struct ust_app_session *ua_sess; | |
1980 | struct ust_app_channel *ua_chan; | |
1981 | struct ust_app_event *ua_event; | |
1982 | ||
1983 | DBG("UST app disabling all event for all apps in channel " | |
a991f516 | 1984 | "%s for session id %d", uchan->name, usess->id); |
9730260e DG |
1985 | |
1986 | rcu_read_lock(); | |
1987 | ||
1988 | /* For all registered applications */ | |
852d0037 | 1989 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
1990 | if (!app->compatible) { |
1991 | /* | |
1992 | * TODO: In time, we should notice the caller of this error by | |
1993 | * telling him that this is a version error. | |
1994 | */ | |
1995 | continue; | |
1996 | } | |
9730260e | 1997 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
1998 | if (!ua_sess) { |
1999 | /* The application has problem or is probably dead. */ | |
2000 | continue; | |
2001 | } | |
9730260e DG |
2002 | |
2003 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2004 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2005 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
2006 | /* If the channel is not found, there is a code flow error */ |
2007 | assert(ua_chan_node); | |
2008 | ||
9730260e DG |
2009 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
2010 | ||
2011 | /* Disable each events of channel */ | |
bec39940 DG |
2012 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
2013 | node.node) { | |
7f79d3a1 | 2014 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
9730260e DG |
2015 | if (ret < 0) { |
2016 | /* XXX: Report error someday... */ | |
2017 | continue; | |
2018 | } | |
2019 | } | |
2020 | } | |
2021 | ||
2022 | rcu_read_unlock(); | |
2023 | ||
2024 | return ret; | |
2025 | } | |
2026 | ||
421cb601 | 2027 | /* |
5b4a0ec0 | 2028 | * For a specific UST session, create the channel for all registered apps. |
421cb601 | 2029 | */ |
35a9059d | 2030 | int ust_app_create_channel_glb(struct ltt_ust_session *usess, |
48842b30 DG |
2031 | struct ltt_ust_channel *uchan) |
2032 | { | |
3c14c33f | 2033 | int ret = 0; |
bec39940 | 2034 | struct lttng_ht_iter iter; |
48842b30 DG |
2035 | struct ust_app *app; |
2036 | struct ust_app_session *ua_sess; | |
48842b30 | 2037 | |
fc34caaa DG |
2038 | /* Very wrong code flow */ |
2039 | assert(usess); | |
2040 | assert(uchan); | |
421cb601 | 2041 | |
a991f516 MD |
2042 | DBG2("UST app adding channel %s to global domain for session id %d", |
2043 | uchan->name, usess->id); | |
48842b30 DG |
2044 | |
2045 | rcu_read_lock(); | |
421cb601 | 2046 | |
5b4a0ec0 | 2047 | /* For every registered applications */ |
852d0037 | 2048 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2049 | if (!app->compatible) { |
2050 | /* | |
2051 | * TODO: In time, we should notice the caller of this error by | |
2052 | * telling him that this is a version error. | |
2053 | */ | |
2054 | continue; | |
2055 | } | |
edb67388 DG |
2056 | /* |
2057 | * Create session on the tracer side and add it to app session HT. Note | |
2058 | * that if session exist, it will simply return a pointer to the ust | |
2059 | * app session. | |
2060 | */ | |
421cb601 | 2061 | ua_sess = create_ust_app_session(usess, app); |
509cbaf8 | 2062 | if (ua_sess == NULL) { |
915d047c | 2063 | /* The malloc() failed. */ |
4d710ac2 | 2064 | ret = -ENOMEM; |
95e047ff | 2065 | goto error_rcu_unlock; |
915d047c | 2066 | } else if (ua_sess == (void *) -1UL) { |
4d710ac2 DG |
2067 | /* |
2068 | * The application's socket is not valid. Either a bad socket or a | |
2069 | * timeout on it. We can't inform yet the caller that for a | |
2070 | * specific app, the session failed so we continue here. | |
2071 | */ | |
915d047c | 2072 | continue; |
48842b30 DG |
2073 | } |
2074 | ||
4d710ac2 DG |
2075 | /* Create channel onto application. We don't need the chan ref. */ |
2076 | ret = create_ust_app_channel(ua_sess, uchan, app, NULL); | |
2077 | if (ret < 0 && ret == -ENOMEM) { | |
2078 | /* No more memory is a fatal error. Stop right now. */ | |
95e047ff | 2079 | goto error_rcu_unlock; |
48842b30 | 2080 | } |
48842b30 | 2081 | } |
5b4a0ec0 | 2082 | |
95e047ff | 2083 | error_rcu_unlock: |
48842b30 | 2084 | rcu_read_unlock(); |
3c14c33f | 2085 | return ret; |
48842b30 DG |
2086 | } |
2087 | ||
5b4a0ec0 | 2088 | /* |
edb67388 | 2089 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 2090 | */ |
35a9059d | 2091 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
2092 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
2093 | { | |
2094 | int ret = 0; | |
bec39940 | 2095 | struct lttng_ht_iter iter, uiter; |
18eace3b | 2096 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
2097 | struct ust_app *app; |
2098 | struct ust_app_session *ua_sess; | |
2099 | struct ust_app_channel *ua_chan; | |
2100 | struct ust_app_event *ua_event; | |
48842b30 | 2101 | |
a991f516 MD |
2102 | DBG("UST app enabling event %s for all apps for session id %d", |
2103 | uevent->attr.name, usess->id); | |
48842b30 | 2104 | |
edb67388 DG |
2105 | /* |
2106 | * NOTE: At this point, this function is called only if the session and | |
2107 | * channel passed are already created for all apps. and enabled on the | |
2108 | * tracer also. | |
2109 | */ | |
2110 | ||
48842b30 | 2111 | rcu_read_lock(); |
421cb601 DG |
2112 | |
2113 | /* For all registered applications */ | |
852d0037 | 2114 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2115 | if (!app->compatible) { |
2116 | /* | |
2117 | * TODO: In time, we should notice the caller of this error by | |
2118 | * telling him that this is a version error. | |
2119 | */ | |
2120 | continue; | |
2121 | } | |
edb67388 | 2122 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
2123 | if (!ua_sess) { |
2124 | /* The application has problem or is probably dead. */ | |
2125 | continue; | |
2126 | } | |
ba767faf | 2127 | |
edb67388 | 2128 | /* Lookup channel in the ust app session */ |
bec39940 DG |
2129 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2130 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
2131 | /* If the channel is not found, there is a code flow error */ |
2132 | assert(ua_chan_node); | |
2133 | ||
2134 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2135 | ||
18eace3b DG |
2136 | /* Get event node */ |
2137 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
2138 | uevent->filter, uevent->attr.loglevel); | |
2139 | if (ua_event == NULL) { | |
7f79d3a1 | 2140 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 2141 | "Skipping app", uevent->attr.name, app->pid); |
35a9059d DG |
2142 | continue; |
2143 | } | |
35a9059d DG |
2144 | |
2145 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
2146 | if (ret < 0) { | |
7f79d3a1 | 2147 | goto error; |
48842b30 | 2148 | } |
edb67388 DG |
2149 | } |
2150 | ||
7f79d3a1 | 2151 | error: |
edb67388 | 2152 | rcu_read_unlock(); |
edb67388 DG |
2153 | return ret; |
2154 | } | |
2155 | ||
2156 | /* | |
2157 | * For a specific existing UST session and UST channel, creates the event for | |
2158 | * all registered apps. | |
2159 | */ | |
35a9059d | 2160 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
2161 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
2162 | { | |
2163 | int ret = 0; | |
bec39940 DG |
2164 | struct lttng_ht_iter iter, uiter; |
2165 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
2166 | struct ust_app *app; |
2167 | struct ust_app_session *ua_sess; | |
2168 | struct ust_app_channel *ua_chan; | |
2169 | ||
a991f516 MD |
2170 | DBG("UST app creating event %s for all apps for session id %d", |
2171 | uevent->attr.name, usess->id); | |
edb67388 | 2172 | |
edb67388 DG |
2173 | rcu_read_lock(); |
2174 | ||
2175 | /* For all registered applications */ | |
852d0037 | 2176 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2177 | if (!app->compatible) { |
2178 | /* | |
2179 | * TODO: In time, we should notice the caller of this error by | |
2180 | * telling him that this is a version error. | |
2181 | */ | |
2182 | continue; | |
2183 | } | |
edb67388 | 2184 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
2185 | if (!ua_sess) { |
2186 | /* The application has problem or is probably dead. */ | |
2187 | continue; | |
2188 | } | |
48842b30 DG |
2189 | |
2190 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2191 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2192 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
2193 | /* If the channel is not found, there is a code flow error */ |
2194 | assert(ua_chan_node); | |
2195 | ||
48842b30 DG |
2196 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
2197 | ||
edb67388 DG |
2198 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
2199 | if (ret < 0) { | |
49c336c1 | 2200 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
2201 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
2202 | break; | |
2203 | } | |
2204 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 2205 | uevent->attr.name, app->pid); |
5b4a0ec0 | 2206 | continue; |
48842b30 | 2207 | } |
48842b30 | 2208 | } |
5b4a0ec0 | 2209 | |
48842b30 DG |
2210 | rcu_read_unlock(); |
2211 | ||
2212 | return ret; | |
2213 | } | |
2214 | ||
5b4a0ec0 DG |
2215 | /* |
2216 | * Start tracing for a specific UST session and app. | |
2217 | */ | |
421cb601 | 2218 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
2219 | { |
2220 | int ret = 0; | |
bec39940 | 2221 | struct lttng_ht_iter iter; |
48842b30 DG |
2222 | struct ust_app_session *ua_sess; |
2223 | struct ust_app_channel *ua_chan; | |
5b4a0ec0 | 2224 | struct ltt_ust_stream *ustream; |
173af62f | 2225 | struct consumer_socket *socket; |
48842b30 | 2226 | |
852d0037 | 2227 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 2228 | |
509cbaf8 MD |
2229 | rcu_read_lock(); |
2230 | ||
e0c7ec2b DG |
2231 | if (!app->compatible) { |
2232 | goto end; | |
2233 | } | |
2234 | ||
421cb601 DG |
2235 | ua_sess = lookup_session_by_app(usess, app); |
2236 | if (ua_sess == NULL) { | |
d42f20df DG |
2237 | /* The session is in teardown process. Ignore and continue. */ |
2238 | goto end; | |
421cb601 | 2239 | } |
48842b30 | 2240 | |
aea829b3 DG |
2241 | /* Upon restart, we skip the setup, already done */ |
2242 | if (ua_sess->started) { | |
8be98f9a | 2243 | goto skip_setup; |
aea829b3 | 2244 | } |
8be98f9a | 2245 | |
a4b92340 DG |
2246 | /* Create directories if consumer is LOCAL and has a path defined. */ |
2247 | if (usess->consumer->type == CONSUMER_DST_LOCAL && | |
2248 | strlen(usess->consumer->dst.trace_path) > 0) { | |
2249 | ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, | |
2250 | S_IRWXU | S_IRWXG, usess->uid, usess->gid); | |
2251 | if (ret < 0) { | |
2252 | if (ret != -EEXIST) { | |
2253 | ERR("Trace directory creation error"); | |
2254 | ret = -1; | |
2255 | goto error_rcu_unlock; | |
2256 | } | |
2257 | } | |
2258 | } | |
2259 | ||
421cb601 DG |
2260 | ret = create_ust_app_metadata(ua_sess, usess->pathname, app); |
2261 | if (ret < 0) { | |
f73fabfd | 2262 | ret = LTTNG_ERR_UST_META_FAIL; |
509cbaf8 | 2263 | goto error_rcu_unlock; |
421cb601 | 2264 | } |
48842b30 | 2265 | |
421cb601 | 2266 | /* For each channel */ |
bec39940 DG |
2267 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2268 | node.node) { | |
421cb601 DG |
2269 | /* Create all streams */ |
2270 | while (1) { | |
5b4a0ec0 | 2271 | /* Create UST stream */ |
421cb601 DG |
2272 | ustream = zmalloc(sizeof(*ustream)); |
2273 | if (ustream == NULL) { | |
2274 | PERROR("zmalloc ust stream"); | |
509cbaf8 | 2275 | goto error_rcu_unlock; |
421cb601 | 2276 | } |
48842b30 | 2277 | |
4063050c MD |
2278 | /* We are going to receive 2 fds, we need to reserve them. */ |
2279 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
2280 | if (ret < 0) { | |
2281 | ERR("Exhausted number of available FD upon stream create"); | |
2282 | free(ustream); | |
2283 | goto error_rcu_unlock; | |
2284 | } | |
86acf0da DG |
2285 | |
2286 | health_code_update(&health_thread_cmd); | |
2287 | ||
852d0037 | 2288 | ret = ustctl_create_stream(app->sock, ua_chan->obj, |
421cb601 | 2289 | &ustream->obj); |
48842b30 | 2290 | if (ret < 0) { |
421cb601 | 2291 | /* Got all streams */ |
4063050c | 2292 | lttng_fd_put(LTTNG_FD_APPS, 2); |
a2c0da86 | 2293 | free(ustream); |
f73fabfd | 2294 | ret = LTTNG_ERR_UST_STREAM_FAIL; |
421cb601 | 2295 | break; |
48842b30 | 2296 | } |
421cb601 | 2297 | ustream->handle = ustream->obj->handle; |
48842b30 | 2298 | |
86acf0da DG |
2299 | health_code_update(&health_thread_cmd); |
2300 | ||
421cb601 DG |
2301 | /* Order is important */ |
2302 | cds_list_add_tail(&ustream->list, &ua_chan->streams.head); | |
00e2e675 | 2303 | ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u", |
c30aaa51 MD |
2304 | ua_chan->name, ua_chan->streams.count); |
2305 | ua_chan->streams.count++; | |
aba8e916 DG |
2306 | if (ret < 0) { |
2307 | PERROR("asprintf UST create stream"); | |
a2c0da86 MD |
2308 | /* |
2309 | * XXX what should we do here with the | |
2310 | * stream ? | |
2311 | */ | |
421cb601 | 2312 | continue; |
aba8e916 | 2313 | } |
00e2e675 DG |
2314 | DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count, |
2315 | ustream->handle); | |
421cb601 | 2316 | } |
86acf0da DG |
2317 | |
2318 | health_code_update(&health_thread_cmd); | |
421cb601 | 2319 | } |
aba8e916 | 2320 | |
7753dea8 MD |
2321 | switch (app->bits_per_long) { |
2322 | case 64: | |
173af62f DG |
2323 | socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd), |
2324 | usess->consumer); | |
2325 | if (socket == NULL) { | |
2326 | goto skip_setup; | |
2327 | } | |
7753dea8 MD |
2328 | break; |
2329 | case 32: | |
173af62f DG |
2330 | socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd), |
2331 | usess->consumer); | |
2332 | if (socket == NULL) { | |
2333 | goto skip_setup; | |
2334 | } | |
7753dea8 MD |
2335 | break; |
2336 | default: | |
2337 | ret = -EINVAL; | |
2338 | goto error_rcu_unlock; | |
2339 | } | |
aea829b3 | 2340 | |
421cb601 | 2341 | /* Setup UST consumer socket and send fds to it */ |
173af62f | 2342 | ret = ust_consumer_send_session(ua_sess, usess->consumer, socket); |
421cb601 | 2343 | if (ret < 0) { |
509cbaf8 | 2344 | goto error_rcu_unlock; |
421cb601 | 2345 | } |
48842b30 | 2346 | |
86acf0da DG |
2347 | health_code_update(&health_thread_cmd); |
2348 | ||
8be98f9a | 2349 | skip_setup: |
421cb601 | 2350 | /* This start the UST tracing */ |
852d0037 | 2351 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
421cb601 | 2352 | if (ret < 0) { |
5c3c99d7 | 2353 | ERR("Error starting tracing for app pid: %d (ret: %d)", app->pid, ret); |
509cbaf8 | 2354 | goto error_rcu_unlock; |
421cb601 | 2355 | } |
5b4a0ec0 | 2356 | |
55c3953d DG |
2357 | /* Indicate that the session has been started once */ |
2358 | ua_sess->started = 1; | |
2359 | ||
86acf0da DG |
2360 | health_code_update(&health_thread_cmd); |
2361 | ||
421cb601 | 2362 | /* Quiescent wait after starting trace */ |
852d0037 | 2363 | ustctl_wait_quiescent(app->sock); |
48842b30 | 2364 | |
e0c7ec2b DG |
2365 | end: |
2366 | rcu_read_unlock(); | |
86acf0da | 2367 | health_code_update(&health_thread_cmd); |
421cb601 | 2368 | return 0; |
48842b30 | 2369 | |
509cbaf8 MD |
2370 | error_rcu_unlock: |
2371 | rcu_read_unlock(); | |
86acf0da | 2372 | health_code_update(&health_thread_cmd); |
421cb601 DG |
2373 | return -1; |
2374 | } | |
48842b30 | 2375 | |
8be98f9a MD |
2376 | /* |
2377 | * Stop tracing for a specific UST session and app. | |
2378 | */ | |
2379 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) | |
2380 | { | |
2381 | int ret = 0; | |
bec39940 | 2382 | struct lttng_ht_iter iter; |
8be98f9a | 2383 | struct ust_app_session *ua_sess; |
6d3686da | 2384 | struct ust_app_channel *ua_chan; |
8be98f9a | 2385 | |
852d0037 | 2386 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
2387 | |
2388 | rcu_read_lock(); | |
2389 | ||
e0c7ec2b DG |
2390 | if (!app->compatible) { |
2391 | goto end; | |
2392 | } | |
2393 | ||
8be98f9a MD |
2394 | ua_sess = lookup_session_by_app(usess, app); |
2395 | if (ua_sess == NULL) { | |
d42f20df | 2396 | goto end; |
8be98f9a MD |
2397 | } |
2398 | ||
9bc07046 DG |
2399 | /* |
2400 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
2401 | * that was never started. It's possible since we can have a fail start |
2402 | * from either the application manager thread or the command thread. Simply | |
2403 | * indicate that this is a stop error. | |
9bc07046 | 2404 | */ |
f9dfc3d9 | 2405 | if (!ua_sess->started) { |
c45536e1 DG |
2406 | goto error_rcu_unlock; |
2407 | } | |
7db205b5 | 2408 | |
86acf0da DG |
2409 | health_code_update(&health_thread_cmd); |
2410 | ||
9d6c7d3f | 2411 | /* This inhibits UST tracing */ |
852d0037 | 2412 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
9d6c7d3f | 2413 | if (ret < 0) { |
5c3c99d7 | 2414 | ERR("Error stopping tracing for app pid: %d (ret: %d)", app->pid, ret); |
9d6c7d3f DG |
2415 | goto error_rcu_unlock; |
2416 | } | |
2417 | ||
86acf0da DG |
2418 | health_code_update(&health_thread_cmd); |
2419 | ||
9d6c7d3f | 2420 | /* Quiescent wait after stopping trace */ |
852d0037 | 2421 | ustctl_wait_quiescent(app->sock); |
9d6c7d3f | 2422 | |
86acf0da DG |
2423 | health_code_update(&health_thread_cmd); |
2424 | ||
9d6c7d3f | 2425 | /* Flushing buffers */ |
bec39940 DG |
2426 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2427 | node.node) { | |
86acf0da | 2428 | health_code_update(&health_thread_cmd); |
852d0037 | 2429 | ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj); |
8be98f9a | 2430 | if (ret < 0) { |
7db205b5 | 2431 | ERR("UST app PID %d channel %s flush failed with ret %d", |
852d0037 | 2432 | app->pid, ua_chan->name, ret); |
6d3686da DG |
2433 | /* Continuing flushing all buffers */ |
2434 | continue; | |
8be98f9a MD |
2435 | } |
2436 | } | |
8be98f9a | 2437 | |
86acf0da DG |
2438 | health_code_update(&health_thread_cmd); |
2439 | ||
90d97d10 | 2440 | /* Flush all buffers before stopping */ |
852d0037 | 2441 | ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj); |
90d97d10 | 2442 | if (ret < 0) { |
852d0037 | 2443 | ERR("UST app PID %d metadata flush failed with ret %d", app->pid, |
7db205b5 | 2444 | ret); |
90d97d10 DG |
2445 | } |
2446 | ||
7db205b5 DG |
2447 | end: |
2448 | rcu_read_unlock(); | |
86acf0da | 2449 | health_code_update(&health_thread_cmd); |
8be98f9a MD |
2450 | return 0; |
2451 | ||
2452 | error_rcu_unlock: | |
2453 | rcu_read_unlock(); | |
86acf0da | 2454 | health_code_update(&health_thread_cmd); |
8be98f9a MD |
2455 | return -1; |
2456 | } | |
2457 | ||
84cd17c6 MD |
2458 | /* |
2459 | * Destroy a specific UST session in apps. | |
2460 | */ | |
3353de95 | 2461 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 MD |
2462 | { |
2463 | struct ust_app_session *ua_sess; | |
2464 | struct lttng_ust_object_data obj; | |
bec39940 DG |
2465 | struct lttng_ht_iter iter; |
2466 | struct lttng_ht_node_ulong *node; | |
525b0740 | 2467 | int ret; |
84cd17c6 | 2468 | |
852d0037 | 2469 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
2470 | |
2471 | rcu_read_lock(); | |
2472 | ||
e0c7ec2b DG |
2473 | if (!app->compatible) { |
2474 | goto end; | |
2475 | } | |
2476 | ||
84cd17c6 | 2477 | __lookup_session_by_app(usess, app, &iter); |
bec39940 | 2478 | node = lttng_ht_iter_get_node_ulong(&iter); |
84cd17c6 | 2479 | if (node == NULL) { |
d42f20df DG |
2480 | /* Session is being or is deleted. */ |
2481 | goto end; | |
84cd17c6 MD |
2482 | } |
2483 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
bec39940 | 2484 | ret = lttng_ht_del(app->sessions, &iter); |
c4a1715b DG |
2485 | if (ret) { |
2486 | /* Already scheduled for teardown. */ | |
2487 | goto end; | |
2488 | } | |
2489 | ||
84cd17c6 MD |
2490 | obj.handle = ua_sess->handle; |
2491 | obj.shm_fd = -1; | |
2492 | obj.wait_fd = -1; | |
2493 | obj.memory_map_size = 0; | |
86acf0da | 2494 | health_code_update(&health_thread_cmd); |
852d0037 | 2495 | ustctl_release_object(app->sock, &obj); |
84cd17c6 | 2496 | |
86acf0da | 2497 | health_code_update(&health_thread_cmd); |
852d0037 | 2498 | delete_ust_app_session(app->sock, ua_sess); |
7db205b5 | 2499 | |
84cd17c6 | 2500 | /* Quiescent wait after stopping trace */ |
852d0037 | 2501 | ustctl_wait_quiescent(app->sock); |
84cd17c6 | 2502 | |
e0c7ec2b DG |
2503 | end: |
2504 | rcu_read_unlock(); | |
86acf0da | 2505 | health_code_update(&health_thread_cmd); |
84cd17c6 | 2506 | return 0; |
84cd17c6 MD |
2507 | } |
2508 | ||
5b4a0ec0 DG |
2509 | /* |
2510 | * Start tracing for the UST session. | |
2511 | */ | |
421cb601 DG |
2512 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
2513 | { | |
2514 | int ret = 0; | |
bec39940 | 2515 | struct lttng_ht_iter iter; |
421cb601 | 2516 | struct ust_app *app; |
48842b30 | 2517 | |
421cb601 DG |
2518 | DBG("Starting all UST traces"); |
2519 | ||
2520 | rcu_read_lock(); | |
421cb601 | 2521 | |
852d0037 | 2522 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
421cb601 | 2523 | ret = ust_app_start_trace(usess, app); |
48842b30 | 2524 | if (ret < 0) { |
5b4a0ec0 DG |
2525 | /* Continue to next apps even on error */ |
2526 | continue; | |
48842b30 | 2527 | } |
48842b30 | 2528 | } |
5b4a0ec0 | 2529 | |
48842b30 DG |
2530 | rcu_read_unlock(); |
2531 | ||
2532 | return 0; | |
2533 | } | |
487cf67c | 2534 | |
8be98f9a MD |
2535 | /* |
2536 | * Start tracing for the UST session. | |
2537 | */ | |
2538 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
2539 | { | |
2540 | int ret = 0; | |
bec39940 | 2541 | struct lttng_ht_iter iter; |
8be98f9a MD |
2542 | struct ust_app *app; |
2543 | ||
2544 | DBG("Stopping all UST traces"); | |
2545 | ||
2546 | rcu_read_lock(); | |
2547 | ||
852d0037 | 2548 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
8be98f9a MD |
2549 | ret = ust_app_stop_trace(usess, app); |
2550 | if (ret < 0) { | |
2551 | /* Continue to next apps even on error */ | |
2552 | continue; | |
2553 | } | |
2554 | } | |
2555 | ||
2556 | rcu_read_unlock(); | |
2557 | ||
2558 | return 0; | |
2559 | } | |
2560 | ||
84cd17c6 MD |
2561 | /* |
2562 | * Destroy app UST session. | |
2563 | */ | |
2564 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
2565 | { | |
2566 | int ret = 0; | |
bec39940 | 2567 | struct lttng_ht_iter iter; |
84cd17c6 MD |
2568 | struct ust_app *app; |
2569 | ||
2570 | DBG("Destroy all UST traces"); | |
2571 | ||
2572 | rcu_read_lock(); | |
2573 | ||
852d0037 | 2574 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 2575 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
2576 | if (ret < 0) { |
2577 | /* Continue to next apps even on error */ | |
2578 | continue; | |
2579 | } | |
2580 | } | |
2581 | ||
2582 | rcu_read_unlock(); | |
2583 | ||
2584 | return 0; | |
2585 | } | |
2586 | ||
5b4a0ec0 DG |
2587 | /* |
2588 | * Add channels/events from UST global domain to registered apps at sock. | |
2589 | */ | |
487cf67c DG |
2590 | void ust_app_global_update(struct ltt_ust_session *usess, int sock) |
2591 | { | |
55c54cce | 2592 | int ret = 0; |
727d5404 | 2593 | struct lttng_ht_iter iter, uiter, iter_ctx; |
487cf67c DG |
2594 | struct ust_app *app; |
2595 | struct ust_app_session *ua_sess; | |
2596 | struct ust_app_channel *ua_chan; | |
2597 | struct ust_app_event *ua_event; | |
727d5404 | 2598 | struct ust_app_ctx *ua_ctx; |
1f3580c7 | 2599 | |
95e047ff | 2600 | assert(usess); |
1f3580c7 | 2601 | |
a991f516 MD |
2602 | DBG2("UST app global update for app sock %d for session id %d", sock, |
2603 | usess->id); | |
487cf67c | 2604 | |
284d8f55 DG |
2605 | rcu_read_lock(); |
2606 | ||
487cf67c DG |
2607 | app = find_app_by_sock(sock); |
2608 | if (app == NULL) { | |
2609 | ERR("Failed to update app sock %d", sock); | |
2610 | goto error; | |
2611 | } | |
2612 | ||
e0c7ec2b DG |
2613 | if (!app->compatible) { |
2614 | goto error; | |
2615 | } | |
2616 | ||
421cb601 | 2617 | ua_sess = create_ust_app_session(usess, app); |
5b98a774 DG |
2618 | if (ua_sess == NULL || ua_sess == (void *) -1UL) { |
2619 | /* Tracer is gone for this session and has been freed */ | |
487cf67c DG |
2620 | goto error; |
2621 | } | |
2622 | ||
284d8f55 DG |
2623 | /* |
2624 | * We can iterate safely here over all UST app session sicne the create ust | |
2625 | * app session above made a shadow copy of the UST global domain from the | |
2626 | * ltt ust session. | |
2627 | */ | |
bec39940 DG |
2628 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
2629 | node.node) { | |
284d8f55 DG |
2630 | ret = create_ust_channel(app, ua_sess, ua_chan); |
2631 | if (ret < 0) { | |
2632 | /* FIXME: Should we quit here or continue... */ | |
2633 | continue; | |
487cf67c DG |
2634 | } |
2635 | ||
727d5404 DG |
2636 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx, |
2637 | node.node) { | |
2638 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2639 | if (ret < 0) { | |
2640 | /* FIXME: Should we quit here or continue... */ | |
2641 | continue; | |
2642 | } | |
2643 | } | |
2644 | ||
2645 | ||
284d8f55 | 2646 | /* For each events */ |
bec39940 DG |
2647 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
2648 | node.node) { | |
284d8f55 DG |
2649 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
2650 | if (ret < 0) { | |
2651 | /* FIXME: Should we quit here or continue... */ | |
2652 | continue; | |
487cf67c | 2653 | } |
727d5404 | 2654 | |
53a80697 MD |
2655 | ret = set_ust_event_filter(ua_event, app); |
2656 | if (ret < 0) { | |
2657 | /* FIXME: Should we quit here or continue... */ | |
2658 | continue; | |
2659 | } | |
36dc12cc | 2660 | } |
487cf67c DG |
2661 | } |
2662 | ||
36dc12cc | 2663 | if (usess->start_trace) { |
421cb601 | 2664 | ret = ust_app_start_trace(usess, app); |
36dc12cc | 2665 | if (ret < 0) { |
36dc12cc DG |
2666 | goto error; |
2667 | } | |
2668 | ||
852d0037 | 2669 | DBG2("UST trace started for app pid %d", app->pid); |
36dc12cc DG |
2670 | } |
2671 | ||
487cf67c DG |
2672 | error: |
2673 | rcu_read_unlock(); | |
2674 | return; | |
2675 | } | |
55cc08a6 DG |
2676 | |
2677 | /* | |
2678 | * Add context to a specific channel for global UST domain. | |
2679 | */ | |
2680 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
2681 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
2682 | { | |
2683 | int ret = 0; | |
bec39940 DG |
2684 | struct lttng_ht_node_str *ua_chan_node; |
2685 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
2686 | struct ust_app_channel *ua_chan = NULL; |
2687 | struct ust_app_session *ua_sess; | |
2688 | struct ust_app *app; | |
2689 | ||
2690 | rcu_read_lock(); | |
2691 | ||
852d0037 | 2692 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
2693 | if (!app->compatible) { |
2694 | /* | |
2695 | * TODO: In time, we should notice the caller of this error by | |
2696 | * telling him that this is a version error. | |
2697 | */ | |
2698 | continue; | |
2699 | } | |
55cc08a6 DG |
2700 | ua_sess = lookup_session_by_app(usess, app); |
2701 | if (ua_sess == NULL) { | |
2702 | continue; | |
2703 | } | |
2704 | ||
2705 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2706 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
2707 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 DG |
2708 | if (ua_chan_node == NULL) { |
2709 | continue; | |
2710 | } | |
2711 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
2712 | node); | |
2713 | ||
2714 | ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app); | |
2715 | if (ret < 0) { | |
2716 | continue; | |
2717 | } | |
2718 | } | |
2719 | ||
55cc08a6 DG |
2720 | rcu_read_unlock(); |
2721 | return ret; | |
2722 | } | |
2723 | ||
76d45b40 DG |
2724 | /* |
2725 | * Enable event for a channel from a UST session for a specific PID. | |
2726 | */ | |
2727 | int ust_app_enable_event_pid(struct ltt_ust_session *usess, | |
2728 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) | |
2729 | { | |
2730 | int ret = 0; | |
bec39940 | 2731 | struct lttng_ht_iter iter; |
18eace3b | 2732 | struct lttng_ht_node_str *ua_chan_node; |
76d45b40 DG |
2733 | struct ust_app *app; |
2734 | struct ust_app_session *ua_sess; | |
2735 | struct ust_app_channel *ua_chan; | |
2736 | struct ust_app_event *ua_event; | |
2737 | ||
2738 | DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid); | |
2739 | ||
2740 | rcu_read_lock(); | |
2741 | ||
2742 | app = ust_app_find_by_pid(pid); | |
2743 | if (app == NULL) { | |
2744 | ERR("UST app enable event per PID %d not found", pid); | |
2745 | ret = -1; | |
2746 | goto error; | |
2747 | } | |
2748 | ||
e0c7ec2b DG |
2749 | if (!app->compatible) { |
2750 | ret = 0; | |
2751 | goto error; | |
2752 | } | |
2753 | ||
76d45b40 | 2754 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
2755 | if (!ua_sess) { |
2756 | /* The application has problem or is probably dead. */ | |
2757 | goto error; | |
2758 | } | |
76d45b40 DG |
2759 | |
2760 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2761 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2762 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
76d45b40 DG |
2763 | /* If the channel is not found, there is a code flow error */ |
2764 | assert(ua_chan_node); | |
2765 | ||
2766 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2767 | ||
18eace3b DG |
2768 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
2769 | uevent->filter, uevent->attr.loglevel); | |
2770 | if (ua_event == NULL) { | |
76d45b40 DG |
2771 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
2772 | if (ret < 0) { | |
2773 | goto error; | |
2774 | } | |
2775 | } else { | |
76d45b40 DG |
2776 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
2777 | if (ret < 0) { | |
2778 | goto error; | |
2779 | } | |
2780 | } | |
2781 | ||
2782 | error: | |
2783 | rcu_read_unlock(); | |
2784 | return ret; | |
2785 | } | |
7f79d3a1 DG |
2786 | |
2787 | /* | |
2788 | * Disable event for a channel from a UST session for a specific PID. | |
2789 | */ | |
2790 | int ust_app_disable_event_pid(struct ltt_ust_session *usess, | |
2791 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) | |
2792 | { | |
2793 | int ret = 0; | |
bec39940 DG |
2794 | struct lttng_ht_iter iter; |
2795 | struct lttng_ht_node_str *ua_chan_node, *ua_event_node; | |
7f79d3a1 DG |
2796 | struct ust_app *app; |
2797 | struct ust_app_session *ua_sess; | |
2798 | struct ust_app_channel *ua_chan; | |
2799 | struct ust_app_event *ua_event; | |
2800 | ||
2801 | DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid); | |
2802 | ||
2803 | rcu_read_lock(); | |
2804 | ||
2805 | app = ust_app_find_by_pid(pid); | |
2806 | if (app == NULL) { | |
2807 | ERR("UST app disable event per PID %d not found", pid); | |
2808 | ret = -1; | |
2809 | goto error; | |
2810 | } | |
2811 | ||
e0c7ec2b DG |
2812 | if (!app->compatible) { |
2813 | ret = 0; | |
2814 | goto error; | |
2815 | } | |
2816 | ||
7f79d3a1 | 2817 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
2818 | if (!ua_sess) { |
2819 | /* The application has problem or is probably dead. */ | |
2820 | goto error; | |
2821 | } | |
7f79d3a1 DG |
2822 | |
2823 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2824 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2825 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
7f79d3a1 DG |
2826 | if (ua_chan_node == NULL) { |
2827 | /* Channel does not exist, skip disabling */ | |
2828 | goto error; | |
2829 | } | |
2830 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2831 | ||
bec39940 DG |
2832 | lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter); |
2833 | ua_event_node = lttng_ht_iter_get_node_str(&iter); | |
7f79d3a1 DG |
2834 | if (ua_event_node == NULL) { |
2835 | /* Event does not exist, skip disabling */ | |
2836 | goto error; | |
2837 | } | |
2838 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
2839 | ||
2840 | ret = disable_ust_app_event(ua_sess, ua_event, app); | |
2841 | if (ret < 0) { | |
2842 | goto error; | |
2843 | } | |
2844 | ||
2845 | error: | |
2846 | rcu_read_unlock(); | |
2847 | return ret; | |
2848 | } | |
e0c7ec2b DG |
2849 | |
2850 | /* | |
2851 | * Validate version of UST apps and set the compatible bit. | |
2852 | */ | |
2853 | int ust_app_validate_version(int sock) | |
2854 | { | |
2855 | int ret; | |
2856 | struct ust_app *app; | |
2857 | ||
2858 | rcu_read_lock(); | |
2859 | ||
2860 | app = find_app_by_sock(sock); | |
2861 | assert(app); | |
2862 | ||
86acf0da DG |
2863 | health_code_update(&health_thread_cmd); |
2864 | ||
e0c7ec2b DG |
2865 | ret = ustctl_tracer_version(sock, &app->version); |
2866 | if (ret < 0) { | |
2867 | goto error; | |
2868 | } | |
2869 | ||
2870 | /* Validate version */ | |
aee0cea0 | 2871 | if (app->version.major != UST_APP_MAJOR_VERSION) { |
e0c7ec2b DG |
2872 | goto error; |
2873 | } | |
2874 | ||
68264071 | 2875 | DBG2("UST app PID %d is compatible with internal major version %d " |
aee0cea0 | 2876 | "(supporting == %d)", app->pid, app->version.major, |
e0c7ec2b DG |
2877 | UST_APP_MAJOR_VERSION); |
2878 | app->compatible = 1; | |
2879 | rcu_read_unlock(); | |
86acf0da | 2880 | health_code_update(&health_thread_cmd); |
e0c7ec2b DG |
2881 | return 0; |
2882 | ||
2883 | error: | |
68264071 | 2884 | DBG2("UST app PID %d is not compatible with internal major version %d " |
aee0cea0 | 2885 | "(supporting == %d)", app->pid, app->version.major, |
e0c7ec2b DG |
2886 | UST_APP_MAJOR_VERSION); |
2887 | app->compatible = 0; | |
2888 | rcu_read_unlock(); | |
86acf0da | 2889 | health_code_update(&health_thread_cmd); |
e0c7ec2b DG |
2890 | return -1; |
2891 | } | |
4466912f DG |
2892 | |
2893 | /* | |
2894 | * Calibrate registered applications. | |
2895 | */ | |
2896 | int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) | |
2897 | { | |
2898 | int ret = 0; | |
2899 | struct lttng_ht_iter iter; | |
2900 | struct ust_app *app; | |
2901 | ||
2902 | rcu_read_lock(); | |
2903 | ||
852d0037 | 2904 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
4466912f DG |
2905 | if (!app->compatible) { |
2906 | /* | |
2907 | * TODO: In time, we should notice the caller of this error by | |
2908 | * telling him that this is a version error. | |
2909 | */ | |
2910 | continue; | |
2911 | } | |
2912 | ||
86acf0da DG |
2913 | health_code_update(&health_thread_cmd); |
2914 | ||
852d0037 | 2915 | ret = ustctl_calibrate(app->sock, calibrate); |
4466912f DG |
2916 | if (ret < 0) { |
2917 | switch (ret) { | |
2918 | case -ENOSYS: | |
2919 | /* Means that it's not implemented on the tracer side. */ | |
2920 | ret = 0; | |
2921 | break; | |
2922 | default: | |
2923 | /* TODO: Report error to user */ | |
2924 | DBG2("Calibrate app PID %d returned with error %d", | |
852d0037 | 2925 | app->pid, ret); |
4466912f DG |
2926 | break; |
2927 | } | |
2928 | } | |
2929 | } | |
2930 | ||
2931 | DBG("UST app global domain calibration finished"); | |
2932 | ||
2933 | rcu_read_unlock(); | |
2934 | ||
86acf0da DG |
2935 | health_code_update(&health_thread_cmd); |
2936 | ||
4466912f DG |
2937 | return ret; |
2938 | } |