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