Commit | Line | Data |
---|---|---|
91d76f53 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
91d76f53 DG |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
099e26bd | 24 | #include <string.h> |
aba8e916 DG |
25 | #include <sys/stat.h> |
26 | #include <sys/types.h> | |
099e26bd | 27 | #include <unistd.h> |
91d76f53 | 28 | |
1e307fab | 29 | #include <lttngerr.h> |
48842b30 | 30 | #include <lttng-share.h> |
1e307fab | 31 | |
f6a9efaa | 32 | #include "hashtable.h" |
56fff090 | 33 | #include "ust-app.h" |
48842b30 | 34 | #include "ust-consumer.h" |
d80a6244 DG |
35 | #include "ust-ctl.h" |
36 | ||
37 | /* | |
38 | * Delete ust app event safely. RCU read lock must be held before calling | |
39 | * this function. | |
40 | */ | |
41 | static void delete_ust_app_event(int sock, struct ust_app_event *ua_event) | |
42 | { | |
43 | /* TODO : remove context */ | |
44 | //struct ust_app_ctx *ltctx; | |
45 | //cds_lfht_for_each_entry(lte->ctx, &iter, ltctx, node) { | |
46 | // delete_ust_app_ctx(sock, ltctx); | |
47 | //} | |
48 | ||
49 | ustctl_release_object(sock, ua_event->obj); | |
50 | free(ua_event); | |
51 | } | |
52 | ||
53 | /* | |
54 | * Delete ust app stream safely. RCU read lock must be held before calling | |
55 | * this function. | |
56 | */ | |
57 | static void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream) | |
58 | { | |
59 | //TODO | |
60 | //stream is used for passing to consumer. | |
61 | //send_channel_streams is responsible for freeing the streams. | |
62 | //note that this will not play well with flight recorder mode: | |
63 | //we might need a criterion to discard the streams. | |
64 | } | |
65 | ||
66 | /* | |
67 | * Delete ust app channel safely. RCU read lock must be held before calling | |
68 | * this function. | |
69 | */ | |
70 | static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan) | |
71 | { | |
72 | int ret; | |
73 | struct cds_lfht_iter iter; | |
74 | struct ust_app_event *ua_event; | |
75 | struct ltt_ust_stream *stream, *stmp; | |
76 | ||
77 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
78 | delete_ust_app_stream(sock, stream); | |
79 | } | |
80 | ||
81 | /* TODO : remove channel context */ | |
82 | //cds_lfht_for_each_entry(ltc->ctx, &iter, ltctx, node) { | |
83 | // hashtable_del(ltc->ctx, &iter); | |
84 | // delete_ust_app_ctx(sock, ltctx); | |
85 | //} | |
86 | //ret = hashtable_destroy(ltc->ctx); | |
87 | ||
88 | cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) { | |
89 | hashtable_del(ua_chan->events, &iter); | |
90 | delete_ust_app_event(sock, ua_event); | |
91 | } | |
92 | ||
93 | ret = hashtable_destroy(ua_chan->events); | |
94 | if (ret < 0) { | |
95 | ERR("UST app destroy session hashtable failed"); | |
96 | goto error; | |
97 | } | |
98 | ||
99 | error: | |
100 | return; | |
101 | } | |
102 | ||
103 | /* | |
104 | * Delete ust app session safely. RCU read lock must be held before calling | |
105 | * this function. | |
106 | */ | |
107 | static void delete_ust_app_session(int sock, | |
108 | struct ust_app_session *ua_sess) | |
109 | { | |
110 | int ret; | |
111 | struct cds_lfht_iter iter; | |
112 | struct ust_app_channel *ua_chan; | |
113 | ||
114 | if (ua_sess->metadata) { | |
115 | /* | |
116 | * We do NOT release the stream object and metadata object since they | |
117 | * are release when fds are sent to the consumer. | |
118 | */ | |
119 | } | |
120 | ||
121 | cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) { | |
122 | hashtable_del(ua_sess->channels, &iter); | |
123 | delete_ust_app_channel(sock, ua_chan); | |
124 | } | |
125 | ||
126 | ret = hashtable_destroy(ua_sess->channels); | |
127 | if (ret < 0) { | |
128 | ERR("UST app destroy session hashtable failed"); | |
129 | goto error; | |
130 | } | |
131 | ||
132 | error: | |
133 | return; | |
134 | } | |
91d76f53 DG |
135 | |
136 | /* | |
284d8f55 DG |
137 | * Delete a traceable application structure from the global list. Never call |
138 | * this function outside of a call_rcu call. | |
91d76f53 | 139 | */ |
284d8f55 | 140 | static void delete_ust_app(struct ust_app *app) |
91d76f53 | 141 | { |
f6a9efaa DG |
142 | int ret; |
143 | struct cds_lfht_node *node; | |
144 | struct cds_lfht_iter iter; | |
284d8f55 | 145 | struct ust_app_session *ua_sess; |
44d3bd01 | 146 | |
f6a9efaa | 147 | rcu_read_lock(); |
44d3bd01 | 148 | |
f6a9efaa DG |
149 | /* Remove from key hash table */ |
150 | node = hashtable_lookup(ust_app_sock_key_map, | |
284d8f55 | 151 | (void *) ((unsigned long) app->key.sock), sizeof(void *), &iter); |
f6a9efaa | 152 | if (node == NULL) { |
284d8f55 DG |
153 | /* Not suppose to happen */ |
154 | ERR("UST app key %d not found in key hash table", app->key.sock); | |
d80a6244 | 155 | goto end; |
284d8f55 DG |
156 | } |
157 | ||
158 | ret = hashtable_del(ust_app_sock_key_map, &iter); | |
159 | if (ret) { | |
160 | ERR("UST app unable to delete app sock %d from key hash table", | |
161 | app->key.sock); | |
f6a9efaa | 162 | } else { |
284d8f55 DG |
163 | DBG2("UST app pair sock %d key %d deleted", |
164 | app->key.sock, app->key.pid); | |
f6a9efaa DG |
165 | } |
166 | ||
d80a6244 DG |
167 | /* Socket is already closed at this point */ |
168 | ||
169 | /* Delete ust app sessions info */ | |
284d8f55 DG |
170 | if (app->sock_closed) { |
171 | app->key.sock = -1; | |
d80a6244 DG |
172 | } |
173 | ||
284d8f55 DG |
174 | cds_lfht_for_each_entry(app->sessions, &iter, ua_sess, node) { |
175 | hashtable_del(app->sessions, &iter); | |
176 | delete_ust_app_session(app->key.sock, ua_sess); | |
d80a6244 | 177 | } |
f6a9efaa | 178 | |
284d8f55 | 179 | ret = hashtable_destroy(app->sessions); |
d80a6244 DG |
180 | if (ret < 0) { |
181 | ERR("UST app destroy session hashtable failed"); | |
182 | goto end; | |
183 | } | |
184 | ||
284d8f55 DG |
185 | if (!app->sock_closed) { |
186 | close(app->key.sock); | |
d80a6244 DG |
187 | } |
188 | ||
284d8f55 DG |
189 | DBG2("UST app pid %d deleted", app->key.pid); |
190 | free(app); | |
d80a6244 | 191 | end: |
f6a9efaa | 192 | rcu_read_unlock(); |
099e26bd DG |
193 | } |
194 | ||
195 | /* | |
f6a9efaa | 196 | * URCU intermediate call to delete an UST app. |
099e26bd | 197 | */ |
f6a9efaa | 198 | static void delete_ust_app_rcu(struct rcu_head *head) |
099e26bd | 199 | { |
f6a9efaa DG |
200 | struct cds_lfht_node *node = |
201 | caa_container_of(head, struct cds_lfht_node, head); | |
202 | struct ust_app *app = | |
203 | caa_container_of(node, struct ust_app, node); | |
204 | ||
205 | delete_ust_app(app); | |
099e26bd DG |
206 | } |
207 | ||
208 | /* | |
421cb601 DG |
209 | * Find an ust_app using the sock and return it. RCU read side lock must be |
210 | * held before calling this helper function. | |
099e26bd | 211 | */ |
f6a9efaa | 212 | static struct ust_app *find_app_by_sock(int sock) |
099e26bd | 213 | { |
f6a9efaa DG |
214 | struct cds_lfht_node *node; |
215 | struct ust_app_key *key; | |
216 | struct cds_lfht_iter iter; | |
f6a9efaa | 217 | |
f6a9efaa DG |
218 | node = hashtable_lookup(ust_app_sock_key_map, |
219 | (void *)((unsigned long) sock), sizeof(void *), &iter); | |
220 | if (node == NULL) { | |
221 | DBG2("UST app find by sock %d key not found", sock); | |
f6a9efaa DG |
222 | goto error; |
223 | } | |
224 | ||
225 | key = caa_container_of(node, struct ust_app_key, node); | |
226 | ||
227 | node = hashtable_lookup(ust_app_ht, | |
228 | (void *)((unsigned long) key->pid), sizeof(void *), &iter); | |
229 | if (node == NULL) { | |
230 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
231 | goto error; |
232 | } | |
f6a9efaa DG |
233 | return caa_container_of(node, struct ust_app, node); |
234 | ||
235 | error: | |
236 | return NULL; | |
099e26bd DG |
237 | } |
238 | ||
239 | /* | |
5b4a0ec0 | 240 | * Open metadata onto the UST tracer for a UST session. |
0177d773 | 241 | */ |
5b4a0ec0 DG |
242 | static int open_ust_metadata(struct ust_app *app, |
243 | struct ust_app_session *ua_sess) | |
0177d773 | 244 | { |
5b4a0ec0 DG |
245 | int ret; |
246 | struct lttng_ust_channel_attr uattr; | |
0177d773 | 247 | |
5b4a0ec0 DG |
248 | uattr.overwrite = ua_sess->metadata->attr.overwrite; |
249 | uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size; | |
250 | uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf; | |
251 | uattr.switch_timer_interval = | |
252 | ua_sess->metadata->attr.switch_timer_interval; | |
253 | uattr.read_timer_interval = | |
254 | ua_sess->metadata->attr.read_timer_interval; | |
255 | uattr.output = ua_sess->metadata->attr.output; | |
256 | ||
257 | /* UST tracer metadata creation */ | |
258 | ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr, | |
259 | &ua_sess->metadata->obj); | |
260 | if (ret < 0) { | |
261 | ERR("UST app open metadata failed for app pid:%d", | |
262 | app->key.pid); | |
f6a9efaa | 263 | goto error; |
0177d773 | 264 | } |
f6a9efaa DG |
265 | |
266 | error: | |
5b4a0ec0 | 267 | return ret; |
91d76f53 DG |
268 | } |
269 | ||
270 | /* | |
5b4a0ec0 | 271 | * Create stream onto the UST tracer for a UST session. |
91d76f53 | 272 | */ |
5b4a0ec0 DG |
273 | static int create_ust_stream(struct ust_app *app, |
274 | struct ust_app_session *ua_sess) | |
91d76f53 | 275 | { |
5b4a0ec0 | 276 | int ret; |
421cb601 | 277 | |
5b4a0ec0 DG |
278 | ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj, |
279 | &ua_sess->metadata->stream_obj); | |
280 | if (ret < 0) { | |
281 | ERR("UST create metadata stream failed"); | |
421cb601 | 282 | goto error; |
91d76f53 | 283 | } |
421cb601 | 284 | |
421cb601 | 285 | error: |
5b4a0ec0 | 286 | return ret; |
91d76f53 DG |
287 | } |
288 | ||
b551a063 | 289 | /* |
5b4a0ec0 | 290 | * Create the specified channel onto the UST tracer for a UST session. |
b551a063 | 291 | */ |
5b4a0ec0 DG |
292 | static int create_ust_channel(struct ust_app *app, |
293 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
b551a063 | 294 | { |
5b4a0ec0 | 295 | int ret; |
b551a063 | 296 | |
5b4a0ec0 DG |
297 | /* TODO: remove cast and use lttng-ust-abi.h */ |
298 | ret = ustctl_create_channel(app->key.sock, ua_sess->handle, | |
299 | (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj); | |
300 | if (ret < 0) { | |
301 | DBG("Error creating channel %s for app (pid: %d, sock: %d) " | |
302 | "and session handle %d with ret %d", | |
303 | ua_chan->name, app->key.pid, app->key.sock, | |
304 | ua_sess->handle, ret); | |
b551a063 DG |
305 | goto error; |
306 | } | |
307 | ||
5b4a0ec0 DG |
308 | ua_chan->handle = ua_chan->obj->handle; |
309 | ua_chan->attr.shm_fd = ua_chan->obj->shm_fd; | |
310 | ua_chan->attr.wait_fd = ua_chan->obj->wait_fd; | |
311 | ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size; | |
b551a063 | 312 | |
5b4a0ec0 DG |
313 | DBG2("UST app channel %s created successfully for pid:%d and sock:%d", |
314 | ua_chan->name, app->key.pid, app->key.sock); | |
b551a063 | 315 | |
b551a063 DG |
316 | error: |
317 | return ret; | |
318 | } | |
319 | ||
91d76f53 | 320 | /* |
5b4a0ec0 | 321 | * Create the specified event onto the UST tracer for a UST session. |
91d76f53 | 322 | */ |
5b4a0ec0 DG |
323 | static int create_ust_event(struct ust_app *app, |
324 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
325 | struct ust_app_event *ua_event) | |
91d76f53 | 326 | { |
5b4a0ec0 | 327 | int ret = 0; |
284d8f55 | 328 | |
5b4a0ec0 DG |
329 | /* Create UST event on tracer */ |
330 | ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj, | |
331 | &ua_event->obj); | |
332 | if (ret < 0) { | |
333 | ERR("Error ustctl create event %s for app pid: %d with ret %d", | |
334 | ua_event->attr.name, app->key.pid, ret); | |
335 | goto error; | |
91d76f53 | 336 | } |
f6a9efaa | 337 | |
5b4a0ec0 DG |
338 | ua_event->handle = ua_event->obj->handle; |
339 | ua_event->enabled = 1; | |
284d8f55 | 340 | |
5b4a0ec0 DG |
341 | DBG2("UST app event %s created successfully for pid:%d", |
342 | ua_event->attr.name, app->key.pid); | |
f6a9efaa | 343 | |
5b4a0ec0 DG |
344 | error: |
345 | return ret; | |
91d76f53 | 346 | } |
48842b30 DG |
347 | |
348 | /* | |
349 | * Alloc new UST app session. | |
350 | */ | |
421cb601 | 351 | static struct ust_app_session *alloc_ust_app_session(void) |
48842b30 DG |
352 | { |
353 | struct ust_app_session *ua_sess; | |
354 | ||
421cb601 | 355 | /* Init most of the default value by allocating and zeroing */ |
48842b30 DG |
356 | ua_sess = zmalloc(sizeof(struct ust_app_session)); |
357 | if (ua_sess == NULL) { | |
358 | PERROR("malloc"); | |
359 | goto error; | |
360 | } | |
361 | ||
48842b30 DG |
362 | ua_sess->handle = -1; |
363 | ua_sess->channels = hashtable_new_str(0); | |
48842b30 DG |
364 | |
365 | return ua_sess; | |
366 | ||
367 | error: | |
368 | return NULL; | |
369 | } | |
370 | ||
421cb601 DG |
371 | /* |
372 | * Alloc new UST app channel. | |
373 | */ | |
284d8f55 DG |
374 | static struct ust_app_channel *alloc_ust_app_channel(char *name, |
375 | struct lttng_ust_channel *attr) | |
48842b30 DG |
376 | { |
377 | struct ust_app_channel *ua_chan; | |
378 | ||
421cb601 | 379 | /* Init most of the default value by allocating and zeroing */ |
48842b30 DG |
380 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); |
381 | if (ua_chan == NULL) { | |
382 | PERROR("malloc"); | |
383 | goto error; | |
384 | } | |
385 | ||
284d8f55 | 386 | /* Setup channel name */ |
48842b30 DG |
387 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); |
388 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
284d8f55 | 389 | |
48842b30 | 390 | ua_chan->handle = -1; |
48842b30 | 391 | ua_chan->ctx = hashtable_new(0); |
48842b30 DG |
392 | ua_chan->events = hashtable_new_str(0); |
393 | hashtable_node_init(&ua_chan->node, (void *) ua_chan->name, | |
394 | strlen(ua_chan->name)); | |
395 | ||
284d8f55 DG |
396 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); |
397 | ||
398 | /* Copy attributes */ | |
399 | if (attr) { | |
400 | memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr)); | |
401 | } | |
402 | ||
48842b30 DG |
403 | DBG3("UST app channel %s allocated", ua_chan->name); |
404 | ||
405 | return ua_chan; | |
406 | ||
407 | error: | |
408 | return NULL; | |
409 | } | |
410 | ||
421cb601 DG |
411 | /* |
412 | * Alloc new UST app event. | |
413 | */ | |
284d8f55 DG |
414 | static struct ust_app_event *alloc_ust_app_event(char *name, |
415 | struct lttng_ust_event *attr) | |
48842b30 DG |
416 | { |
417 | struct ust_app_event *ua_event; | |
418 | ||
421cb601 | 419 | /* Init most of the default value by allocating and zeroing */ |
48842b30 DG |
420 | ua_event = zmalloc(sizeof(struct ust_app_event)); |
421 | if (ua_event == NULL) { | |
422 | PERROR("malloc"); | |
423 | goto error; | |
424 | } | |
425 | ||
426 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
427 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
428 | ua_event->ctx = hashtable_new(0); | |
429 | hashtable_node_init(&ua_event->node, (void *) ua_event->name, | |
430 | strlen(ua_event->name)); | |
431 | ||
284d8f55 DG |
432 | /* Copy attributes */ |
433 | if (attr) { | |
434 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
435 | } | |
436 | ||
48842b30 DG |
437 | DBG3("UST app event %s allocated", ua_event->name); |
438 | ||
439 | return ua_event; | |
440 | ||
441 | error: | |
442 | return NULL; | |
443 | } | |
444 | ||
5b4a0ec0 DG |
445 | /* |
446 | * Copy data between an UST app event and a LTT event. | |
447 | */ | |
421cb601 | 448 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
449 | struct ltt_ust_event *uevent) |
450 | { | |
451 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); | |
452 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
453 | ||
5b4a0ec0 DG |
454 | /* Copy event attributes */ |
455 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
456 | ||
48842b30 DG |
457 | /* TODO: support copy context */ |
458 | } | |
459 | ||
5b4a0ec0 DG |
460 | /* |
461 | * Copy data between an UST app channel and a LTT channel. | |
462 | */ | |
421cb601 | 463 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
464 | struct ltt_ust_channel *uchan) |
465 | { | |
466 | struct cds_lfht_iter iter; | |
5b4a0ec0 | 467 | struct cds_lfht_node *ua_event_node; |
48842b30 DG |
468 | struct ltt_ust_event *uevent; |
469 | struct ust_app_event *ua_event; | |
470 | ||
421cb601 | 471 | DBG2("Shadow copy of UST app channel %s", ua_chan->name); |
48842b30 DG |
472 | |
473 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
474 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
5b4a0ec0 DG |
475 | /* Copy event attributes */ |
476 | memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr)); | |
48842b30 DG |
477 | |
478 | /* TODO: support copy context */ | |
479 | ||
421cb601 | 480 | /* Copy all events from ltt ust channel to ust app channel */ |
5b4a0ec0 | 481 | cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) { |
ba767faf MD |
482 | struct cds_lfht_iter uiter; |
483 | ||
48842b30 | 484 | ua_event_node = hashtable_lookup(ua_chan->events, |
ba767faf MD |
485 | (void *) uevent->attr.name, strlen(uevent->attr.name), |
486 | &uiter); | |
48842b30 | 487 | if (ua_event_node == NULL) { |
421cb601 | 488 | DBG2("UST event %s not found on shadow copy channel", |
48842b30 | 489 | uevent->attr.name); |
284d8f55 | 490 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
48842b30 | 491 | if (ua_event == NULL) { |
5b4a0ec0 | 492 | continue; |
48842b30 | 493 | } |
421cb601 | 494 | shadow_copy_event(ua_event, uevent); |
48842b30 | 495 | hashtable_add_unique(ua_chan->events, &ua_event->node); |
48842b30 | 496 | } |
48842b30 DG |
497 | } |
498 | ||
421cb601 | 499 | DBG3("Shadow copy channel done"); |
48842b30 DG |
500 | } |
501 | ||
5b4a0ec0 DG |
502 | /* |
503 | * Copy data between a UST app session and a regular LTT session. | |
504 | */ | |
421cb601 | 505 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
477d7741 MD |
506 | struct ltt_ust_session *usess, |
507 | struct ust_app *app) | |
48842b30 | 508 | { |
5b4a0ec0 | 509 | struct cds_lfht_node *ua_chan_node; |
48842b30 DG |
510 | struct cds_lfht_iter iter; |
511 | struct ltt_ust_channel *uchan; | |
512 | struct ust_app_channel *ua_chan; | |
477d7741 MD |
513 | time_t rawtime; |
514 | struct tm *timeinfo; | |
515 | char datetime[16]; | |
516 | int ret; | |
517 | ||
518 | /* Get date and time for unique app path */ | |
519 | time(&rawtime); | |
520 | timeinfo = localtime(&rawtime); | |
521 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
48842b30 | 522 | |
421cb601 | 523 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 DG |
524 | |
525 | ua_sess->uid = usess->uid; | |
526 | ||
477d7741 MD |
527 | ret = snprintf(ua_sess->path, PATH_MAX, |
528 | "%s/%s-%d-%s", | |
529 | usess->pathname, app->name, app->key.pid, | |
530 | datetime); | |
531 | if (ret < 0) { | |
532 | PERROR("asprintf UST shadow copy session"); | |
533 | /* TODO: We cannot return an error from here.. */ | |
534 | assert(0); | |
535 | } | |
536 | ||
48842b30 DG |
537 | /* TODO: support all UST domain */ |
538 | ||
539 | /* Iterate over all channels in global domain. */ | |
5b4a0ec0 DG |
540 | cds_lfht_for_each_entry(usess->domain_global.channels, &iter, |
541 | uchan, node) { | |
ba767faf MD |
542 | struct cds_lfht_iter uiter; |
543 | ||
48842b30 | 544 | ua_chan_node = hashtable_lookup(ua_sess->channels, |
ba767faf MD |
545 | (void *)uchan->name, strlen(uchan->name), |
546 | &uiter); | |
5b4a0ec0 DG |
547 | if (ua_chan_node != NULL) { |
548 | continue; | |
549 | } | |
421cb601 | 550 | |
5b4a0ec0 DG |
551 | DBG2("Channel %s not found on shadow session copy, creating it", |
552 | uchan->name); | |
553 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); | |
554 | if (ua_chan == NULL) { | |
555 | /* malloc failed... continuing */ | |
556 | continue; | |
48842b30 DG |
557 | } |
558 | ||
5b4a0ec0 DG |
559 | shadow_copy_channel(ua_chan, uchan); |
560 | hashtable_add_unique(ua_sess->channels, &ua_chan->node); | |
48842b30 DG |
561 | } |
562 | } | |
563 | ||
421cb601 DG |
564 | /* |
565 | * Return ust app session from the app session hashtable using the UST session | |
566 | * uid. | |
567 | */ | |
48842b30 DG |
568 | static struct ust_app_session *lookup_session_by_app( |
569 | struct ltt_ust_session *usess, struct ust_app *app) | |
570 | { | |
571 | struct cds_lfht_iter iter; | |
572 | struct cds_lfht_node *node; | |
573 | ||
574 | /* Get right UST app session from app */ | |
575 | node = hashtable_lookup(app->sessions, | |
421cb601 | 576 | (void *) ((unsigned long) usess->uid), sizeof(void *), &iter); |
48842b30 DG |
577 | if (node == NULL) { |
578 | goto error; | |
579 | } | |
580 | ||
581 | return caa_container_of(node, struct ust_app_session, node); | |
582 | ||
583 | error: | |
584 | return NULL; | |
585 | } | |
586 | ||
421cb601 DG |
587 | /* |
588 | * Create a UST session onto the tracer of app and add it the session | |
589 | * hashtable. | |
590 | * | |
591 | * Return ust app session or NULL on error. | |
592 | */ | |
593 | static struct ust_app_session *create_ust_app_session( | |
594 | struct ltt_ust_session *usess, struct ust_app *app) | |
595 | { | |
596 | int ret; | |
597 | struct ust_app_session *ua_sess; | |
598 | ||
599 | ua_sess = lookup_session_by_app(usess, app); | |
600 | if (ua_sess == NULL) { | |
601 | DBG2("UST app pid: %d session uid %d not found, creating it", | |
602 | app->key.pid, usess->uid); | |
603 | ua_sess = alloc_ust_app_session(); | |
604 | if (ua_sess == NULL) { | |
605 | /* Only malloc can failed so something is really wrong */ | |
606 | goto error; | |
607 | } | |
477d7741 | 608 | shadow_copy_session(ua_sess, usess, app); |
421cb601 DG |
609 | } |
610 | ||
611 | if (ua_sess->handle == -1) { | |
612 | ret = ustctl_create_session(app->key.sock); | |
613 | if (ret < 0) { | |
614 | ERR("Error creating session for app pid %d, sock %d", | |
615 | app->key.pid, app->key.sock); | |
616 | /* TODO: free() ua_sess */ | |
617 | goto error; | |
618 | } | |
619 | ||
620 | DBG2("UST app ustctl create session handle %d", ret); | |
621 | ua_sess->handle = ret; | |
622 | ||
623 | /* Add ust app session to app's HT */ | |
624 | hashtable_node_init(&ua_sess->node, | |
625 | (void *)((unsigned long) ua_sess->uid), sizeof(void *)); | |
626 | hashtable_add_unique(app->sessions, &ua_sess->node); | |
627 | ||
628 | DBG2("UST app session created successfully with handle %d", ret); | |
629 | } | |
630 | ||
631 | return ua_sess; | |
632 | ||
633 | error: | |
634 | return NULL; | |
635 | } | |
636 | ||
284d8f55 | 637 | /* |
5b4a0ec0 | 638 | * Create UST app channel and create it on the tracer. |
284d8f55 | 639 | */ |
5b4a0ec0 DG |
640 | static struct ust_app_channel *create_ust_app_channel( |
641 | struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan, | |
642 | struct ust_app *app) | |
643 | { | |
644 | int ret = 0; | |
645 | struct cds_lfht_iter iter; | |
646 | struct cds_lfht_node *ua_chan_node; | |
647 | struct ust_app_channel *ua_chan; | |
648 | ||
649 | /* Lookup channel in the ust app session */ | |
650 | ua_chan_node = hashtable_lookup(ua_sess->channels, | |
651 | (void *)uchan->name, strlen(uchan->name), &iter); | |
652 | if (ua_chan_node == NULL) { | |
653 | DBG2("Unable to find channel %s in ust session uid %u", | |
654 | uchan->name, ua_sess->uid); | |
655 | ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr); | |
656 | if (ua_chan == NULL) { | |
657 | goto error; | |
658 | } | |
659 | shadow_copy_channel(ua_chan, uchan); | |
660 | ||
661 | hashtable_add_unique(ua_sess->channels, &ua_chan->node); | |
662 | } else { | |
663 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
664 | } | |
665 | ||
666 | ret = create_ust_channel(app, ua_sess, ua_chan); | |
667 | if (ret < 0) { | |
668 | goto error; | |
669 | } | |
670 | ||
671 | return ua_chan; | |
672 | ||
673 | error: | |
674 | return NULL; | |
675 | } | |
676 | ||
677 | /* | |
678 | * Create UST app event and create it on the tracer side. | |
679 | */ | |
680 | static struct ust_app_event *create_ust_app_event( | |
681 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
682 | struct ltt_ust_event *uevent, struct ust_app *app) | |
284d8f55 DG |
683 | { |
684 | int ret; | |
5b4a0ec0 DG |
685 | struct cds_lfht_iter iter; |
686 | struct cds_lfht_node *ua_event_node; | |
687 | struct ust_app_event *ua_event; | |
284d8f55 | 688 | |
5b4a0ec0 DG |
689 | /* Get event node */ |
690 | ua_event_node = hashtable_lookup(ua_chan->events, | |
691 | (void *)uevent->attr.name, strlen(uevent->attr.name), &iter); | |
692 | if (ua_event_node == NULL) { | |
693 | DBG2("UST app event %s not found, creating it", uevent->attr.name); | |
694 | /* Does not exist so create one */ | |
695 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
696 | if (ua_event == NULL) { | |
697 | /* Only malloc can failed so something is really wrong */ | |
698 | goto error; | |
699 | } | |
700 | shadow_copy_event(ua_event, uevent); | |
701 | ||
702 | hashtable_add_unique(ua_chan->events, &ua_event->node); | |
703 | } else { | |
704 | ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); | |
705 | } | |
706 | ||
707 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); | |
284d8f55 | 708 | if (ret < 0) { |
284d8f55 DG |
709 | goto error; |
710 | } | |
711 | ||
5b4a0ec0 | 712 | return ua_event; |
284d8f55 | 713 | |
5b4a0ec0 DG |
714 | error: |
715 | return NULL; | |
716 | } | |
717 | ||
718 | /* | |
719 | * Create UST metadata and open it on the tracer side. | |
720 | */ | |
721 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
722 | char *pathname, struct ust_app *app) | |
723 | { | |
724 | int ret = 0; | |
725 | ||
726 | if (ua_sess->metadata == NULL) { | |
727 | /* Allocate UST metadata */ | |
728 | ua_sess->metadata = trace_ust_create_metadata(pathname); | |
729 | if (ua_sess->metadata == NULL) { | |
730 | ERR("UST app session %d creating metadata failed", | |
731 | ua_sess->handle); | |
732 | goto error; | |
733 | } | |
734 | ||
735 | ret = open_ust_metadata(app, ua_sess); | |
736 | if (ret < 0) { | |
737 | goto error; | |
738 | } | |
739 | ||
740 | DBG2("UST metadata opened for app pid %d", app->key.pid); | |
741 | } | |
742 | ||
743 | /* Open UST metadata stream */ | |
744 | if (ua_sess->metadata->stream_obj == NULL) { | |
745 | ret = create_ust_stream(app, ua_sess); | |
746 | if (ret < 0) { | |
747 | goto error; | |
748 | } | |
749 | ||
477d7741 | 750 | ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG); |
5b4a0ec0 DG |
751 | if (ret < 0) { |
752 | PERROR("mkdir UST metadata"); | |
753 | goto error; | |
754 | } | |
755 | ||
477d7741 MD |
756 | ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, |
757 | "%s/metadata", ua_sess->path); | |
5b4a0ec0 DG |
758 | if (ret < 0) { |
759 | PERROR("asprintf UST create stream"); | |
760 | goto error; | |
761 | } | |
762 | ||
763 | DBG2("UST metadata stream object created for app pid %d", | |
764 | app->key.pid); | |
765 | } else { | |
766 | ERR("Attempting to create stream without metadata opened"); | |
767 | goto error; | |
768 | } | |
769 | ||
770 | return 0; | |
771 | ||
772 | error: | |
773 | return -1; | |
774 | } | |
775 | ||
776 | /* | |
777 | * Return pointer to traceable apps list. | |
778 | */ | |
779 | struct cds_lfht *ust_app_get_ht(void) | |
780 | { | |
781 | return ust_app_ht; | |
782 | } | |
783 | ||
784 | /* | |
785 | * Return ust app pointer or NULL if not found. | |
786 | */ | |
787 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
788 | { | |
789 | struct cds_lfht_node *node; | |
790 | struct cds_lfht_iter iter; | |
791 | ||
792 | rcu_read_lock(); | |
793 | node = hashtable_lookup(ust_app_ht, | |
794 | (void *)((unsigned long) pid), sizeof(void *), &iter); | |
795 | if (node == NULL) { | |
796 | DBG2("UST app no found with pid %d", pid); | |
797 | goto error; | |
798 | } | |
799 | rcu_read_unlock(); | |
800 | ||
801 | DBG2("Found UST app by pid %d", pid); | |
802 | ||
803 | return caa_container_of(node, struct ust_app, node); | |
804 | ||
805 | error: | |
806 | rcu_read_unlock(); | |
807 | return NULL; | |
808 | } | |
809 | ||
810 | /* | |
811 | * Using pid and uid (of the app), allocate a new ust_app struct and | |
812 | * add it to the global traceable app list. | |
813 | * | |
814 | * On success, return 0, else return malloc ENOMEM. | |
815 | */ | |
816 | int ust_app_register(struct ust_register_msg *msg, int sock) | |
817 | { | |
818 | struct ust_app *lta; | |
819 | ||
820 | lta = zmalloc(sizeof(struct ust_app)); | |
821 | if (lta == NULL) { | |
822 | PERROR("malloc"); | |
823 | return -ENOMEM; | |
824 | } | |
825 | ||
826 | lta->ppid = msg->ppid; | |
827 | lta->uid = msg->uid; | |
828 | lta->gid = msg->gid; | |
829 | lta->v_major = msg->major; | |
830 | lta->v_minor = msg->minor; | |
831 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
832 | lta->name[16] = '\0'; | |
833 | lta->sessions = hashtable_new(0); | |
834 | ||
835 | /* Set key map */ | |
836 | lta->key.pid = msg->pid; | |
837 | hashtable_node_init(<a->node, (void *)((unsigned long)lta->key.pid), | |
838 | sizeof(void *)); | |
839 | lta->key.sock = sock; | |
840 | hashtable_node_init(<a->key.node, (void *)((unsigned long)lta->key.sock), | |
841 | sizeof(void *)); | |
842 | ||
843 | rcu_read_lock(); | |
844 | hashtable_add_unique(ust_app_sock_key_map, <a->key.node); | |
845 | hashtable_add_unique(ust_app_ht, <a->node); | |
846 | rcu_read_unlock(); | |
847 | ||
848 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s" | |
849 | " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid, | |
850 | lta->key.sock, lta->name, lta->v_major, lta->v_minor); | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | /* | |
856 | * Unregister app by removing it from the global traceable app list and freeing | |
857 | * the data struct. | |
858 | * | |
859 | * The socket is already closed at this point so no close to sock. | |
860 | */ | |
861 | void ust_app_unregister(int sock) | |
862 | { | |
863 | struct ust_app *lta; | |
864 | struct cds_lfht_node *node; | |
865 | struct cds_lfht_iter iter; | |
866 | ||
867 | rcu_read_lock(); | |
868 | lta = find_app_by_sock(sock); | |
869 | if (lta == NULL) { | |
870 | ERR("Unregister app sock %d not found!", sock); | |
871 | goto error; | |
872 | } | |
873 | ||
874 | DBG("PID %d unregistering with sock %d", lta->key.pid, sock); | |
875 | ||
876 | /* Get the node reference for a call_rcu */ | |
877 | node = hashtable_lookup(ust_app_ht, | |
878 | (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter); | |
879 | if (node == NULL) { | |
880 | ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid); | |
881 | goto error; | |
882 | } | |
284d8f55 | 883 | |
5b4a0ec0 DG |
884 | /* We got called because the socket was closed on the remote end. */ |
885 | close(sock); | |
886 | /* Using a flag because we still need "sock" as a key. */ | |
887 | lta->sock_closed = 1; | |
888 | hashtable_del(ust_app_ht, &iter); | |
889 | call_rcu(&node->head, delete_ust_app_rcu); | |
284d8f55 | 890 | error: |
5b4a0ec0 DG |
891 | rcu_read_unlock(); |
892 | return; | |
284d8f55 DG |
893 | } |
894 | ||
895 | /* | |
5b4a0ec0 | 896 | * Return traceable_app_count |
284d8f55 | 897 | */ |
5b4a0ec0 | 898 | unsigned long ust_app_list_count(void) |
284d8f55 | 899 | { |
5b4a0ec0 | 900 | unsigned long count; |
284d8f55 | 901 | |
5b4a0ec0 DG |
902 | rcu_read_lock(); |
903 | count = hashtable_get_count(ust_app_ht); | |
904 | rcu_read_unlock(); | |
284d8f55 | 905 | |
5b4a0ec0 | 906 | return count; |
284d8f55 DG |
907 | } |
908 | ||
5b4a0ec0 DG |
909 | /* |
910 | * Fill events array with all events name of all registered apps. | |
911 | */ | |
912 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 913 | { |
5b4a0ec0 DG |
914 | int ret, handle; |
915 | size_t nbmem, count = 0; | |
421cb601 | 916 | struct cds_lfht_iter iter; |
5b4a0ec0 DG |
917 | struct ust_app *app; |
918 | struct lttng_event *tmp; | |
421cb601 | 919 | |
5b4a0ec0 DG |
920 | nbmem = UST_APP_EVENT_LIST_SIZE; |
921 | tmp = zmalloc(nbmem * sizeof(struct lttng_event)); | |
922 | if (tmp == NULL) { | |
923 | PERROR("zmalloc ust app events"); | |
924 | ret = -ENOMEM; | |
421cb601 DG |
925 | goto error; |
926 | } | |
927 | ||
5b4a0ec0 | 928 | rcu_read_lock(); |
421cb601 | 929 | |
5b4a0ec0 DG |
930 | cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) { |
931 | handle = ustctl_tracepoint_list(app->key.sock); | |
932 | if (handle < 0) { | |
933 | ERR("UST app list events getting handle failed for app pid %d", | |
934 | app->key.pid); | |
935 | continue; | |
936 | } | |
421cb601 | 937 | |
5b4a0ec0 DG |
938 | while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle, |
939 | tmp[count].name)) != -ENOENT) { | |
940 | if (count > nbmem) { | |
941 | DBG2("Reallocating event list from %zu to %zu bytes", nbmem, | |
942 | nbmem + UST_APP_EVENT_LIST_SIZE); | |
943 | nbmem += UST_APP_EVENT_LIST_SIZE; | |
944 | tmp = realloc(tmp, nbmem); | |
945 | if (tmp == NULL) { | |
946 | PERROR("realloc ust app events"); | |
947 | ret = -ENOMEM; | |
948 | goto rcu_error; | |
949 | } | |
950 | } | |
421cb601 | 951 | |
5b4a0ec0 DG |
952 | tmp[count].type = LTTNG_UST_TRACEPOINT; |
953 | tmp[count].pid = app->key.pid; | |
954 | tmp[count].enabled = -1; | |
955 | count++; | |
421cb601 | 956 | } |
421cb601 DG |
957 | } |
958 | ||
5b4a0ec0 DG |
959 | ret = count; |
960 | *events = tmp; | |
421cb601 | 961 | |
5b4a0ec0 | 962 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 963 | |
5b4a0ec0 DG |
964 | rcu_error: |
965 | rcu_read_unlock(); | |
421cb601 | 966 | error: |
5b4a0ec0 | 967 | return ret; |
421cb601 DG |
968 | } |
969 | ||
5b4a0ec0 DG |
970 | /* |
971 | * Free and clean all traceable apps of the global list. | |
972 | */ | |
973 | void ust_app_clean_list(void) | |
421cb601 | 974 | { |
5b4a0ec0 DG |
975 | int ret; |
976 | struct cds_lfht_node *node; | |
977 | struct cds_lfht_iter iter; | |
978 | struct ust_app *app; | |
421cb601 | 979 | |
5b4a0ec0 | 980 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 981 | |
5b4a0ec0 | 982 | rcu_read_lock(); |
421cb601 | 983 | |
5b4a0ec0 DG |
984 | cds_lfht_for_each(ust_app_ht, &iter, node) { |
985 | app = caa_container_of(node, struct ust_app, node); | |
986 | close(app->key.sock); | |
987 | app->sock_closed = 1; | |
421cb601 | 988 | |
5b4a0ec0 DG |
989 | ret = hashtable_del(ust_app_ht, &iter); |
990 | if (!ret) { | |
991 | call_rcu(&node->head, delete_ust_app_rcu); | |
421cb601 | 992 | } |
421cb601 DG |
993 | } |
994 | ||
5b4a0ec0 DG |
995 | hashtable_destroy(ust_app_ht); |
996 | hashtable_destroy(ust_app_sock_key_map); | |
421cb601 | 997 | |
5b4a0ec0 DG |
998 | rcu_read_unlock(); |
999 | } | |
1000 | ||
1001 | /* | |
1002 | * Init UST app hash table. | |
1003 | */ | |
1004 | void ust_app_ht_alloc(void) | |
1005 | { | |
1006 | ust_app_ht = hashtable_new(0); | |
1007 | ust_app_sock_key_map = hashtable_new(0); | |
421cb601 DG |
1008 | } |
1009 | ||
1010 | /* | |
5b4a0ec0 | 1011 | * For a specific UST session, create the channel for all registered apps. |
421cb601 | 1012 | */ |
5b4a0ec0 | 1013 | int ust_app_create_channel_all(struct ltt_ust_session *usess, |
48842b30 DG |
1014 | struct ltt_ust_channel *uchan) |
1015 | { | |
1016 | int ret = 0; | |
1017 | struct cds_lfht_iter iter; | |
48842b30 DG |
1018 | struct ust_app *app; |
1019 | struct ust_app_session *ua_sess; | |
1020 | struct ust_app_channel *ua_chan; | |
1021 | ||
421cb601 DG |
1022 | if (usess == NULL || uchan == NULL) { |
1023 | ERR("Adding UST global channel to NULL values"); | |
1024 | ret = -1; | |
1025 | goto error; | |
1026 | } | |
1027 | ||
48842b30 DG |
1028 | DBG2("UST app adding channel %s to global domain for session uid %d", |
1029 | uchan->name, usess->uid); | |
1030 | ||
1031 | rcu_read_lock(); | |
421cb601 | 1032 | |
5b4a0ec0 DG |
1033 | /* For every registered applications */ |
1034 | cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) { | |
421cb601 DG |
1035 | /* Create session on the tracer side and add it to app session HT */ |
1036 | ua_sess = create_ust_app_session(usess, app); | |
509cbaf8 | 1037 | if (ua_sess == NULL) { |
5b4a0ec0 | 1038 | continue; |
48842b30 DG |
1039 | } |
1040 | ||
421cb601 DG |
1041 | /* Create channel onto application */ |
1042 | ua_chan = create_ust_app_channel(ua_sess, uchan, app); | |
1043 | if (ua_chan == NULL) { | |
5b4a0ec0 | 1044 | continue; |
48842b30 | 1045 | } |
48842b30 | 1046 | } |
5b4a0ec0 | 1047 | |
48842b30 DG |
1048 | rcu_read_unlock(); |
1049 | ||
421cb601 | 1050 | error: |
48842b30 DG |
1051 | return ret; |
1052 | } | |
1053 | ||
5b4a0ec0 DG |
1054 | /* |
1055 | * For a specific UST session and UST channel, create the event for all | |
1056 | * registered apps. | |
1057 | */ | |
1058 | int ust_app_create_event_all(struct ltt_ust_session *usess, | |
48842b30 DG |
1059 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
1060 | { | |
1061 | int ret = 0; | |
1062 | struct cds_lfht_iter iter; | |
5b4a0ec0 | 1063 | struct cds_lfht_node *ua_chan_node; |
48842b30 DG |
1064 | struct ust_app *app; |
1065 | struct ust_app_session *ua_sess; | |
1066 | struct ust_app_channel *ua_chan; | |
1067 | struct ust_app_event *ua_event; | |
48842b30 | 1068 | |
284d8f55 | 1069 | DBG("UST app creating event %s for all apps for session uid %d", |
48842b30 DG |
1070 | uevent->attr.name, usess->uid); |
1071 | ||
1072 | rcu_read_lock(); | |
421cb601 DG |
1073 | |
1074 | /* For all registered applications */ | |
5b4a0ec0 | 1075 | cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) { |
ba767faf MD |
1076 | struct cds_lfht_iter uiter; |
1077 | ||
421cb601 DG |
1078 | /* Create session on the tracer side and add it to app session HT */ |
1079 | ua_sess = create_ust_app_session(usess, app); | |
509cbaf8 | 1080 | if (ua_sess == NULL) { |
5b4a0ec0 | 1081 | continue; |
48842b30 DG |
1082 | } |
1083 | ||
1084 | /* Lookup channel in the ust app session */ | |
1085 | ua_chan_node = hashtable_lookup(ua_sess->channels, | |
ba767faf MD |
1086 | (void *)uchan->name, strlen(uchan->name), |
1087 | &uiter); | |
48842b30 | 1088 | if (ua_chan_node == NULL) { |
421cb601 DG |
1089 | ERR("Channel %s not found in session uid %d. Skipping", |
1090 | uchan->name, usess->uid); | |
5b4a0ec0 | 1091 | continue; |
48842b30 | 1092 | } |
48842b30 DG |
1093 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
1094 | ||
421cb601 DG |
1095 | ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
1096 | if (ua_event == NULL) { | |
5b4a0ec0 | 1097 | continue; |
48842b30 | 1098 | } |
48842b30 | 1099 | } |
5b4a0ec0 | 1100 | |
48842b30 DG |
1101 | rcu_read_unlock(); |
1102 | ||
1103 | return ret; | |
1104 | } | |
1105 | ||
5b4a0ec0 DG |
1106 | /* |
1107 | * Start tracing for a specific UST session and app. | |
1108 | */ | |
421cb601 | 1109 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
1110 | { |
1111 | int ret = 0; | |
1112 | struct cds_lfht_iter iter; | |
48842b30 DG |
1113 | struct ust_app_session *ua_sess; |
1114 | struct ust_app_channel *ua_chan; | |
5b4a0ec0 | 1115 | struct ltt_ust_stream *ustream; |
48842b30 | 1116 | |
421cb601 | 1117 | DBG("Starting tracing for ust app pid %d", app->key.pid); |
5cf5d0e7 | 1118 | |
509cbaf8 MD |
1119 | rcu_read_lock(); |
1120 | ||
421cb601 DG |
1121 | ua_sess = lookup_session_by_app(usess, app); |
1122 | if (ua_sess == NULL) { | |
1123 | /* Only malloc can failed so something is really wrong */ | |
509cbaf8 | 1124 | goto error_rcu_unlock; |
421cb601 | 1125 | } |
48842b30 | 1126 | |
421cb601 DG |
1127 | ret = create_ust_app_metadata(ua_sess, usess->pathname, app); |
1128 | if (ret < 0) { | |
509cbaf8 | 1129 | goto error_rcu_unlock; |
421cb601 | 1130 | } |
48842b30 | 1131 | |
421cb601 | 1132 | /* For each channel */ |
5b4a0ec0 | 1133 | cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) { |
421cb601 DG |
1134 | /* Create all streams */ |
1135 | while (1) { | |
5b4a0ec0 | 1136 | /* Create UST stream */ |
421cb601 DG |
1137 | ustream = zmalloc(sizeof(*ustream)); |
1138 | if (ustream == NULL) { | |
1139 | PERROR("zmalloc ust stream"); | |
509cbaf8 | 1140 | goto error_rcu_unlock; |
421cb601 | 1141 | } |
48842b30 | 1142 | |
421cb601 DG |
1143 | ret = ustctl_create_stream(app->key.sock, ua_chan->obj, |
1144 | &ustream->obj); | |
48842b30 | 1145 | if (ret < 0) { |
421cb601 DG |
1146 | /* Got all streams */ |
1147 | break; | |
48842b30 | 1148 | } |
421cb601 | 1149 | ustream->handle = ustream->obj->handle; |
48842b30 | 1150 | |
421cb601 DG |
1151 | /* Order is important */ |
1152 | cds_list_add_tail(&ustream->list, &ua_chan->streams.head); | |
477d7741 MD |
1153 | ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u", |
1154 | ua_sess->path, ua_chan->name, | |
1155 | ua_chan->streams.count++); | |
aba8e916 DG |
1156 | if (ret < 0) { |
1157 | PERROR("asprintf UST create stream"); | |
421cb601 | 1158 | continue; |
aba8e916 | 1159 | } |
421cb601 DG |
1160 | DBG2("UST stream %d ready at %s", ua_chan->streams.count, |
1161 | ustream->pathname); | |
1162 | } | |
421cb601 | 1163 | } |
aba8e916 | 1164 | |
421cb601 | 1165 | /* Setup UST consumer socket and send fds to it */ |
ba5d816e | 1166 | ret = ust_consumer_send_session(ust_consumer_fd, ua_sess); |
421cb601 | 1167 | if (ret < 0) { |
509cbaf8 | 1168 | goto error_rcu_unlock; |
421cb601 | 1169 | } |
48842b30 | 1170 | |
421cb601 DG |
1171 | /* This start the UST tracing */ |
1172 | ret = ustctl_start_session(app->key.sock, ua_sess->handle); | |
1173 | if (ret < 0) { | |
1174 | ERR("Error starting tracing for app pid: %d", app->key.pid); | |
509cbaf8 | 1175 | goto error_rcu_unlock; |
421cb601 | 1176 | } |
5b4a0ec0 | 1177 | |
509cbaf8 | 1178 | rcu_read_unlock(); |
48842b30 | 1179 | |
421cb601 DG |
1180 | /* Quiescent wait after starting trace */ |
1181 | ustctl_wait_quiescent(app->key.sock); | |
48842b30 | 1182 | |
421cb601 | 1183 | return 0; |
48842b30 | 1184 | |
509cbaf8 MD |
1185 | error_rcu_unlock: |
1186 | rcu_read_unlock(); | |
421cb601 DG |
1187 | return -1; |
1188 | } | |
48842b30 | 1189 | |
5b4a0ec0 DG |
1190 | /* |
1191 | * Start tracing for the UST session. | |
1192 | */ | |
421cb601 DG |
1193 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
1194 | { | |
1195 | int ret = 0; | |
1196 | struct cds_lfht_iter iter; | |
421cb601 | 1197 | struct ust_app *app; |
48842b30 | 1198 | |
421cb601 DG |
1199 | DBG("Starting all UST traces"); |
1200 | ||
1201 | rcu_read_lock(); | |
421cb601 | 1202 | |
5b4a0ec0 | 1203 | cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) { |
421cb601 | 1204 | ret = ust_app_start_trace(usess, app); |
48842b30 | 1205 | if (ret < 0) { |
5b4a0ec0 DG |
1206 | /* Continue to next apps even on error */ |
1207 | continue; | |
48842b30 | 1208 | } |
48842b30 | 1209 | } |
5b4a0ec0 | 1210 | |
48842b30 DG |
1211 | rcu_read_unlock(); |
1212 | ||
1213 | return 0; | |
1214 | } | |
487cf67c | 1215 | |
5b4a0ec0 DG |
1216 | /* |
1217 | * Add channels/events from UST global domain to registered apps at sock. | |
1218 | */ | |
487cf67c DG |
1219 | void ust_app_global_update(struct ltt_ust_session *usess, int sock) |
1220 | { | |
1221 | int ret = 0; | |
487cf67c | 1222 | struct cds_lfht_iter iter; |
487cf67c DG |
1223 | struct ust_app *app; |
1224 | struct ust_app_session *ua_sess; | |
1225 | struct ust_app_channel *ua_chan; | |
1226 | struct ust_app_event *ua_event; | |
1f3580c7 DG |
1227 | |
1228 | if (usess == NULL) { | |
5b4a0ec0 | 1229 | ERR("No UST session on global update. Returning"); |
1f3580c7 DG |
1230 | goto error; |
1231 | } | |
1232 | ||
487cf67c DG |
1233 | DBG2("UST app global update for app sock %d for session uid %d", sock, |
1234 | usess->uid); | |
1235 | ||
284d8f55 DG |
1236 | rcu_read_lock(); |
1237 | ||
487cf67c DG |
1238 | app = find_app_by_sock(sock); |
1239 | if (app == NULL) { | |
1240 | ERR("Failed to update app sock %d", sock); | |
1241 | goto error; | |
1242 | } | |
1243 | ||
421cb601 | 1244 | ua_sess = create_ust_app_session(usess, app); |
487cf67c | 1245 | if (ua_sess == NULL) { |
487cf67c DG |
1246 | goto error; |
1247 | } | |
1248 | ||
284d8f55 DG |
1249 | /* |
1250 | * We can iterate safely here over all UST app session sicne the create ust | |
1251 | * app session above made a shadow copy of the UST global domain from the | |
1252 | * ltt ust session. | |
1253 | */ | |
1254 | cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) { | |
1255 | ret = create_ust_channel(app, ua_sess, ua_chan); | |
1256 | if (ret < 0) { | |
1257 | /* FIXME: Should we quit here or continue... */ | |
1258 | continue; | |
487cf67c DG |
1259 | } |
1260 | ||
284d8f55 DG |
1261 | /* For each events */ |
1262 | cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) { | |
1263 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); | |
1264 | if (ret < 0) { | |
1265 | /* FIXME: Should we quit here or continue... */ | |
1266 | continue; | |
487cf67c | 1267 | } |
36dc12cc | 1268 | } |
487cf67c DG |
1269 | } |
1270 | ||
36dc12cc | 1271 | if (usess->start_trace) { |
421cb601 | 1272 | ret = ust_app_start_trace(usess, app); |
36dc12cc | 1273 | if (ret < 0) { |
36dc12cc DG |
1274 | goto error; |
1275 | } | |
1276 | ||
36dc12cc DG |
1277 | DBG2("UST trace started for app pid %d", app->key.pid); |
1278 | } | |
1279 | ||
487cf67c DG |
1280 | error: |
1281 | rcu_read_unlock(); | |
1282 | return; | |
1283 | } |