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