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