| 1 | /* |
| 2 | * lttng-ctl.c |
| 3 | * |
| 4 | * Linux Trace Toolkit Control Library |
| 5 | * |
| 6 | * Copyright (C) 2011 EfficiOS Inc. |
| 7 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: LGPL-2.1-only |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _LGPL_SOURCE |
| 14 | #include <assert.h> |
| 15 | #include <grp.h> |
| 16 | #include <errno.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <common/common.h> |
| 24 | #include <common/compat/string.h> |
| 25 | #include <common/defaults.h> |
| 26 | #include <common/dynamic-buffer.h> |
| 27 | #include <common/sessiond-comm/sessiond-comm.h> |
| 28 | #include <common/tracker.h> |
| 29 | #include <common/uri.h> |
| 30 | #include <common/utils.h> |
| 31 | #include <lttng/channel-internal.h> |
| 32 | #include <lttng/destruction-handle.h> |
| 33 | #include <lttng/endpoint.h> |
| 34 | #include <lttng/event-internal.h> |
| 35 | #include <lttng/health-internal.h> |
| 36 | #include <lttng/lttng.h> |
| 37 | #include <lttng/session-descriptor-internal.h> |
| 38 | #include <lttng/session-internal.h> |
| 39 | #include <lttng/trigger/trigger-internal.h> |
| 40 | #include <lttng/userspace-probe-internal.h> |
| 41 | |
| 42 | #include "filter/filter-ast.h" |
| 43 | #include "filter/filter-parser.h" |
| 44 | #include "filter/filter-bytecode.h" |
| 45 | #include "filter/memstream.h" |
| 46 | #include "lttng-ctl-helper.h" |
| 47 | |
| 48 | #ifdef DEBUG |
| 49 | static const int print_xml = 1; |
| 50 | #define dbg_printf(fmt, args...) \ |
| 51 | printf("[debug liblttng-ctl] " fmt, ## args) |
| 52 | #else |
| 53 | static const int print_xml = 0; |
| 54 | #define dbg_printf(fmt, args...) \ |
| 55 | do { \ |
| 56 | /* do nothing but check printf format */ \ |
| 57 | if (0) \ |
| 58 | printf("[debug liblttnctl] " fmt, ## args); \ |
| 59 | } while (0) |
| 60 | #endif |
| 61 | |
| 62 | #define COPY_DOMAIN_PACKED(dst, src) \ |
| 63 | do { \ |
| 64 | struct lttng_domain _tmp_domain; \ |
| 65 | \ |
| 66 | lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \ |
| 67 | dst = _tmp_domain; \ |
| 68 | } while (0) |
| 69 | |
| 70 | /* Socket to session daemon for communication */ |
| 71 | static int sessiond_socket = -1; |
| 72 | static char sessiond_sock_path[PATH_MAX]; |
| 73 | |
| 74 | /* Variables */ |
| 75 | static char *tracing_group; |
| 76 | static int connected; |
| 77 | |
| 78 | /* Global */ |
| 79 | |
| 80 | /* |
| 81 | * Those two variables are used by error.h to silent or control the verbosity of |
| 82 | * error message. They are global to the library so application linking with it |
| 83 | * are able to compile correctly and also control verbosity of the library. |
| 84 | */ |
| 85 | int lttng_opt_quiet; |
| 86 | int lttng_opt_verbose; |
| 87 | int lttng_opt_mi; |
| 88 | |
| 89 | /* |
| 90 | * Copy domain to lttcomm_session_msg domain. |
| 91 | * |
| 92 | * If domain is unknown, default domain will be the kernel. |
| 93 | */ |
| 94 | LTTNG_HIDDEN |
| 95 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, |
| 96 | struct lttng_domain *src) |
| 97 | { |
| 98 | if (src && dst) { |
| 99 | switch (src->type) { |
| 100 | case LTTNG_DOMAIN_KERNEL: |
| 101 | case LTTNG_DOMAIN_UST: |
| 102 | case LTTNG_DOMAIN_JUL: |
| 103 | case LTTNG_DOMAIN_LOG4J: |
| 104 | case LTTNG_DOMAIN_PYTHON: |
| 105 | memcpy(dst, src, sizeof(struct lttng_domain)); |
| 106 | break; |
| 107 | default: |
| 108 | memset(dst, 0, sizeof(struct lttng_domain)); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Send lttcomm_session_msg to the session daemon. |
| 116 | * |
| 117 | * On success, returns the number of bytes sent (>=0) |
| 118 | * On error, returns -1 |
| 119 | */ |
| 120 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
| 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | if (!connected) { |
| 125 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 126 | goto end; |
| 127 | } |
| 128 | |
| 129 | DBG("LSM cmd type : %d", lsm->cmd_type); |
| 130 | |
| 131 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
| 132 | sizeof(struct lttcomm_session_msg)); |
| 133 | if (ret < 0) { |
| 134 | ret = -LTTNG_ERR_FATAL; |
| 135 | } |
| 136 | |
| 137 | end: |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Send var len data to the session daemon. |
| 143 | * |
| 144 | * On success, returns the number of bytes sent (>=0) |
| 145 | * On error, returns -1 |
| 146 | */ |
| 147 | static int send_session_varlen(const void *data, size_t len) |
| 148 | { |
| 149 | int ret; |
| 150 | |
| 151 | if (!connected) { |
| 152 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | if (!data || !len) { |
| 157 | ret = 0; |
| 158 | goto end; |
| 159 | } |
| 160 | |
| 161 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); |
| 162 | if (ret < 0) { |
| 163 | ret = -LTTNG_ERR_FATAL; |
| 164 | } |
| 165 | |
| 166 | end: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Send file descriptors to the session daemon. |
| 172 | * |
| 173 | * On success, returns the number of bytes sent (>=0) |
| 174 | * On error, returns -1 |
| 175 | */ |
| 176 | static int send_session_fds(const int *fds, size_t nb_fd) |
| 177 | { |
| 178 | int ret; |
| 179 | |
| 180 | if (!connected) { |
| 181 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 182 | goto end; |
| 183 | } |
| 184 | |
| 185 | if (!fds || !nb_fd) { |
| 186 | ret = 0; |
| 187 | goto end; |
| 188 | } |
| 189 | |
| 190 | ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd); |
| 191 | if (ret < 0) { |
| 192 | ret = -LTTNG_ERR_FATAL; |
| 193 | } |
| 194 | |
| 195 | end: |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Receive data from the sessiond socket. |
| 201 | * |
| 202 | * On success, returns the number of bytes received (>=0) |
| 203 | * On error, returns -1 (recvmsg() error) or -ENOTCONN |
| 204 | */ |
| 205 | static int recv_data_sessiond(void *buf, size_t len) |
| 206 | { |
| 207 | int ret; |
| 208 | |
| 209 | if (!connected) { |
| 210 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
| 215 | if (ret < 0) { |
| 216 | ret = -LTTNG_ERR_FATAL; |
| 217 | } |
| 218 | |
| 219 | end: |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Check if we are in the specified group. |
| 225 | * |
| 226 | * If yes return 1, else return -1. |
| 227 | */ |
| 228 | LTTNG_HIDDEN |
| 229 | int lttng_check_tracing_group(void) |
| 230 | { |
| 231 | gid_t *grp_list, tracing_gid; |
| 232 | int grp_list_size, grp_id, i; |
| 233 | int ret = -1; |
| 234 | const char *grp_name = tracing_group; |
| 235 | |
| 236 | /* Get GID of group 'tracing' */ |
| 237 | if (utils_get_group_id(grp_name, false, &tracing_gid)) { |
| 238 | /* If grp_tracing is NULL, the group does not exist. */ |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | /* Get number of supplementary group IDs */ |
| 243 | grp_list_size = getgroups(0, NULL); |
| 244 | if (grp_list_size < 0) { |
| 245 | PERROR("getgroups"); |
| 246 | goto end; |
| 247 | } |
| 248 | |
| 249 | /* Alloc group list of the right size */ |
| 250 | grp_list = zmalloc(grp_list_size * sizeof(gid_t)); |
| 251 | if (!grp_list) { |
| 252 | PERROR("malloc"); |
| 253 | goto end; |
| 254 | } |
| 255 | grp_id = getgroups(grp_list_size, grp_list); |
| 256 | if (grp_id < 0) { |
| 257 | PERROR("getgroups"); |
| 258 | goto free_list; |
| 259 | } |
| 260 | |
| 261 | for (i = 0; i < grp_list_size; i++) { |
| 262 | if (grp_list[i] == tracing_gid) { |
| 263 | ret = 1; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | free_list: |
| 269 | free(grp_list); |
| 270 | |
| 271 | end: |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | static enum lttng_error_code check_enough_available_memory( |
| 276 | uint64_t num_bytes_requested_per_cpu) |
| 277 | { |
| 278 | int ret; |
| 279 | long num_cpu; |
| 280 | uint64_t best_mem_info; |
| 281 | uint64_t num_bytes_requested_total; |
| 282 | |
| 283 | /* |
| 284 | * Get the number of CPU currently online to compute the amount of |
| 285 | * memory needed to create a buffer for every CPU. |
| 286 | */ |
| 287 | num_cpu = sysconf(_SC_NPROCESSORS_ONLN); |
| 288 | if (num_cpu == -1) { |
| 289 | ret = LTTNG_ERR_FATAL; |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | if (num_bytes_requested_per_cpu > UINT64_MAX / (uint64_t) num_cpu) { |
| 294 | /* Overflow */ |
| 295 | ret = LTTNG_ERR_OVERFLOW; |
| 296 | goto end; |
| 297 | } |
| 298 | |
| 299 | num_bytes_requested_total = |
| 300 | num_bytes_requested_per_cpu * (uint64_t) num_cpu; |
| 301 | |
| 302 | /* |
| 303 | * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most |
| 304 | * reliable estimate we can get but it is only exposed by the kernel |
| 305 | * since 3.14. (See Linux kernel commit: |
| 306 | * 34e431b0ae398fc54ea69ff85ec700722c9da773) |
| 307 | */ |
| 308 | ret = utils_get_memory_available(&best_mem_info); |
| 309 | if (ret >= 0) { |
| 310 | goto success; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This |
| 315 | * is a sanity check for obvious user error. |
| 316 | */ |
| 317 | ret = utils_get_memory_total(&best_mem_info); |
| 318 | if (ret >= 0) { |
| 319 | goto success; |
| 320 | } |
| 321 | |
| 322 | /* No valid source of information. */ |
| 323 | ret = LTTNG_ERR_NOMEM; |
| 324 | goto end; |
| 325 | |
| 326 | success: |
| 327 | if (best_mem_info >= num_bytes_requested_total) { |
| 328 | ret = LTTNG_OK; |
| 329 | } else { |
| 330 | ret = LTTNG_ERR_NOMEM; |
| 331 | } |
| 332 | end: |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Try connect to session daemon with sock_path. |
| 338 | * |
| 339 | * Return 0 on success, else -1 |
| 340 | */ |
| 341 | static int try_connect_sessiond(const char *sock_path) |
| 342 | { |
| 343 | int ret; |
| 344 | |
| 345 | /* If socket exist, we check if the daemon listens for connect. */ |
| 346 | ret = access(sock_path, F_OK); |
| 347 | if (ret < 0) { |
| 348 | /* Not alive */ |
| 349 | goto error; |
| 350 | } |
| 351 | |
| 352 | ret = lttcomm_connect_unix_sock(sock_path); |
| 353 | if (ret < 0) { |
| 354 | /* Not alive. */ |
| 355 | goto error; |
| 356 | } |
| 357 | |
| 358 | ret = lttcomm_close_unix_sock(ret); |
| 359 | if (ret < 0) { |
| 360 | PERROR("lttcomm_close_unix_sock"); |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | |
| 365 | error: |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
| 371 | * variable. |
| 372 | * |
| 373 | * Returns 0 on success, negative value on failure (the sessiond socket path |
| 374 | * is somehow too long or ENOMEM). |
| 375 | */ |
| 376 | static int set_session_daemon_path(void) |
| 377 | { |
| 378 | int in_tgroup = 0; /* In tracing group. */ |
| 379 | uid_t uid; |
| 380 | |
| 381 | uid = getuid(); |
| 382 | |
| 383 | if (uid != 0) { |
| 384 | /* Are we in the tracing group ? */ |
| 385 | in_tgroup = lttng_check_tracing_group(); |
| 386 | } |
| 387 | |
| 388 | if ((uid == 0) || in_tgroup == 1) { |
| 389 | const int ret = lttng_strncpy(sessiond_sock_path, |
| 390 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, |
| 391 | sizeof(sessiond_sock_path)); |
| 392 | |
| 393 | if (ret) { |
| 394 | goto error; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (uid != 0) { |
| 399 | int ret; |
| 400 | |
| 401 | if (in_tgroup) { |
| 402 | /* Tracing group. */ |
| 403 | ret = try_connect_sessiond(sessiond_sock_path); |
| 404 | if (ret >= 0) { |
| 405 | goto end; |
| 406 | } |
| 407 | /* Global session daemon not available... */ |
| 408 | } |
| 409 | /* ...or not in tracing group (and not root), default */ |
| 410 | |
| 411 | /* |
| 412 | * With GNU C < 2.1, snprintf returns -1 if the target buffer |
| 413 | * is too small; |
| 414 | * With GNU C >= 2.1, snprintf returns the required size |
| 415 | * (excluding closing null) |
| 416 | */ |
| 417 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), |
| 418 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
| 419 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
| 420 | goto error; |
| 421 | } |
| 422 | } |
| 423 | end: |
| 424 | return 0; |
| 425 | |
| 426 | error: |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Connect to the LTTng session daemon. |
| 432 | * |
| 433 | * On success, return the socket's file descriptor. On error, return -1. |
| 434 | */ |
| 435 | LTTNG_HIDDEN int connect_sessiond(void) |
| 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | ret = set_session_daemon_path(); |
| 440 | if (ret < 0) { |
| 441 | goto error; |
| 442 | } |
| 443 | |
| 444 | /* Connect to the sesssion daemon. */ |
| 445 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
| 446 | if (ret < 0) { |
| 447 | goto error; |
| 448 | } |
| 449 | |
| 450 | return ret; |
| 451 | |
| 452 | error: |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | static void reset_global_sessiond_connection_state(void) |
| 457 | { |
| 458 | sessiond_socket = -1; |
| 459 | connected = 0; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Clean disconnect from the session daemon. |
| 464 | * |
| 465 | * On success, return 0. On error, return -1. |
| 466 | */ |
| 467 | static int disconnect_sessiond(void) |
| 468 | { |
| 469 | int ret = 0; |
| 470 | |
| 471 | if (connected) { |
| 472 | ret = lttcomm_close_unix_sock(sessiond_socket); |
| 473 | reset_global_sessiond_connection_state(); |
| 474 | } |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | static int recv_sessiond_optional_data(size_t len, void **user_buf, |
| 480 | size_t *user_len) |
| 481 | { |
| 482 | int ret = 0; |
| 483 | void *buf = NULL; |
| 484 | |
| 485 | if (len) { |
| 486 | if (!user_len) { |
| 487 | ret = -LTTNG_ERR_INVALID; |
| 488 | goto end; |
| 489 | } |
| 490 | |
| 491 | buf = zmalloc(len); |
| 492 | if (!buf) { |
| 493 | ret = -ENOMEM; |
| 494 | goto end; |
| 495 | } |
| 496 | |
| 497 | ret = recv_data_sessiond(buf, len); |
| 498 | if (ret < 0) { |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | if (!user_buf) { |
| 503 | ret = -LTTNG_ERR_INVALID; |
| 504 | goto end; |
| 505 | } |
| 506 | |
| 507 | /* Move ownership of command header buffer to user. */ |
| 508 | *user_buf = buf; |
| 509 | buf = NULL; |
| 510 | *user_len = len; |
| 511 | } else { |
| 512 | /* No command header. */ |
| 513 | if (user_len) { |
| 514 | *user_len = 0; |
| 515 | } |
| 516 | |
| 517 | if (user_buf) { |
| 518 | *user_buf = NULL; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | end: |
| 523 | free(buf); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Ask the session daemon a specific command and put the data into buf. |
| 529 | * Takes extra var. len. data and file descriptors as input to send to the |
| 530 | * session daemon. |
| 531 | * |
| 532 | * Return size of data (only payload, not header) or a negative error code. |
| 533 | */ |
| 534 | LTTNG_HIDDEN |
| 535 | int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, |
| 536 | const int *fds, size_t nb_fd, const void *vardata, |
| 537 | size_t vardata_len, void **user_payload_buf, |
| 538 | void **user_cmd_header_buf, size_t *user_cmd_header_len) |
| 539 | { |
| 540 | int ret; |
| 541 | size_t payload_len; |
| 542 | struct lttcomm_lttng_msg llm; |
| 543 | |
| 544 | ret = connect_sessiond(); |
| 545 | if (ret < 0) { |
| 546 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 547 | goto end; |
| 548 | } else { |
| 549 | sessiond_socket = ret; |
| 550 | connected = 1; |
| 551 | } |
| 552 | |
| 553 | /* Send command to session daemon */ |
| 554 | ret = send_session_msg(lsm); |
| 555 | if (ret < 0) { |
| 556 | /* Ret value is a valid lttng error code. */ |
| 557 | goto end; |
| 558 | } |
| 559 | /* Send var len data */ |
| 560 | ret = send_session_varlen(vardata, vardata_len); |
| 561 | if (ret < 0) { |
| 562 | /* Ret value is a valid lttng error code. */ |
| 563 | goto end; |
| 564 | } |
| 565 | |
| 566 | /* Send fds */ |
| 567 | ret = send_session_fds(fds, nb_fd); |
| 568 | if (ret < 0) { |
| 569 | /* Ret value is a valid lttng error code. */ |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | /* Get header from data transmission */ |
| 574 | ret = recv_data_sessiond(&llm, sizeof(llm)); |
| 575 | if (ret < 0) { |
| 576 | /* Ret value is a valid lttng error code. */ |
| 577 | goto end; |
| 578 | } |
| 579 | |
| 580 | /* Check error code if OK */ |
| 581 | if (llm.ret_code != LTTNG_OK) { |
| 582 | ret = -llm.ret_code; |
| 583 | goto end; |
| 584 | } |
| 585 | |
| 586 | /* Get command header from data transmission */ |
| 587 | ret = recv_sessiond_optional_data(llm.cmd_header_size, |
| 588 | user_cmd_header_buf, user_cmd_header_len); |
| 589 | if (ret < 0) { |
| 590 | goto end; |
| 591 | } |
| 592 | |
| 593 | /* Get payload from data transmission */ |
| 594 | ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, |
| 595 | &payload_len); |
| 596 | if (ret < 0) { |
| 597 | goto end; |
| 598 | } |
| 599 | |
| 600 | ret = llm.data_size; |
| 601 | |
| 602 | end: |
| 603 | disconnect_sessiond(); |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Create lttng handle and return pointer. |
| 609 | * |
| 610 | * The returned pointer will be NULL in case of malloc() error. |
| 611 | */ |
| 612 | struct lttng_handle *lttng_create_handle(const char *session_name, |
| 613 | struct lttng_domain *domain) |
| 614 | { |
| 615 | int ret; |
| 616 | struct lttng_handle *handle = NULL; |
| 617 | |
| 618 | handle = zmalloc(sizeof(struct lttng_handle)); |
| 619 | if (handle == NULL) { |
| 620 | PERROR("malloc handle"); |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | /* Copy session name */ |
| 625 | ret = lttng_strncpy(handle->session_name, session_name ? : "", |
| 626 | sizeof(handle->session_name)); |
| 627 | if (ret) { |
| 628 | goto error; |
| 629 | } |
| 630 | |
| 631 | /* Copy lttng domain or leave initialized to 0. */ |
| 632 | if (domain) { |
| 633 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); |
| 634 | } |
| 635 | |
| 636 | end: |
| 637 | return handle; |
| 638 | error: |
| 639 | free(handle); |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Destroy handle by free(3) the pointer. |
| 645 | */ |
| 646 | void lttng_destroy_handle(struct lttng_handle *handle) |
| 647 | { |
| 648 | free(handle); |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Register an outside consumer. |
| 653 | * |
| 654 | * Returns size of returned session payload data or a negative error code. |
| 655 | */ |
| 656 | int lttng_register_consumer(struct lttng_handle *handle, |
| 657 | const char *socket_path) |
| 658 | { |
| 659 | int ret; |
| 660 | struct lttcomm_session_msg lsm; |
| 661 | |
| 662 | if (handle == NULL || socket_path == NULL) { |
| 663 | ret = -LTTNG_ERR_INVALID; |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | memset(&lsm, 0, sizeof(lsm)); |
| 668 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
| 669 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 670 | sizeof(lsm.session.name)); |
| 671 | if (ret) { |
| 672 | ret = -LTTNG_ERR_INVALID; |
| 673 | goto end; |
| 674 | } |
| 675 | |
| 676 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 677 | |
| 678 | ret = lttng_strncpy(lsm.u.reg.path, socket_path, |
| 679 | sizeof(lsm.u.reg.path)); |
| 680 | if (ret) { |
| 681 | ret = -LTTNG_ERR_INVALID; |
| 682 | goto end; |
| 683 | } |
| 684 | |
| 685 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 686 | end: |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Start tracing for all traces of the session. |
| 692 | * |
| 693 | * Returns size of returned session payload data or a negative error code. |
| 694 | */ |
| 695 | int lttng_start_tracing(const char *session_name) |
| 696 | { |
| 697 | int ret; |
| 698 | struct lttcomm_session_msg lsm; |
| 699 | |
| 700 | if (session_name == NULL) { |
| 701 | ret = -LTTNG_ERR_INVALID; |
| 702 | goto end; |
| 703 | } |
| 704 | |
| 705 | memset(&lsm, 0, sizeof(lsm)); |
| 706 | lsm.cmd_type = LTTNG_START_TRACE; |
| 707 | |
| 708 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 709 | sizeof(lsm.session.name)); |
| 710 | if (ret) { |
| 711 | ret = -LTTNG_ERR_INVALID; |
| 712 | goto end; |
| 713 | } |
| 714 | |
| 715 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 716 | end: |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Stop tracing for all traces of the session. |
| 722 | */ |
| 723 | static int _lttng_stop_tracing(const char *session_name, int wait) |
| 724 | { |
| 725 | int ret, data_ret; |
| 726 | struct lttcomm_session_msg lsm; |
| 727 | |
| 728 | if (session_name == NULL) { |
| 729 | ret = -LTTNG_ERR_INVALID; |
| 730 | goto error; |
| 731 | } |
| 732 | |
| 733 | memset(&lsm, 0, sizeof(lsm)); |
| 734 | lsm.cmd_type = LTTNG_STOP_TRACE; |
| 735 | |
| 736 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 737 | sizeof(lsm.session.name)); |
| 738 | if (ret) { |
| 739 | ret = -LTTNG_ERR_INVALID; |
| 740 | goto error; |
| 741 | } |
| 742 | |
| 743 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 744 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
| 745 | goto error; |
| 746 | } |
| 747 | |
| 748 | if (!wait) { |
| 749 | goto end; |
| 750 | } |
| 751 | |
| 752 | /* Check for data availability */ |
| 753 | do { |
| 754 | data_ret = lttng_data_pending(session_name); |
| 755 | if (data_ret < 0) { |
| 756 | /* Return the data available call error. */ |
| 757 | ret = data_ret; |
| 758 | goto error; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Data sleep time before retrying (in usec). Don't sleep if the |
| 763 | * call returned value indicates availability. |
| 764 | */ |
| 765 | if (data_ret) { |
| 766 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
| 767 | } |
| 768 | } while (data_ret != 0); |
| 769 | |
| 770 | end: |
| 771 | error: |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Stop tracing and wait for data availability. |
| 777 | */ |
| 778 | int lttng_stop_tracing(const char *session_name) |
| 779 | { |
| 780 | return _lttng_stop_tracing(session_name, 1); |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Stop tracing but _don't_ wait for data availability. |
| 785 | */ |
| 786 | int lttng_stop_tracing_no_wait(const char *session_name) |
| 787 | { |
| 788 | return _lttng_stop_tracing(session_name, 0); |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Add context to a channel. |
| 793 | * |
| 794 | * If the given channel is NULL, add the contexts to all channels. |
| 795 | * The event_name param is ignored. |
| 796 | * |
| 797 | * Returns the size of the returned payload data or a negative error code. |
| 798 | */ |
| 799 | int lttng_add_context(struct lttng_handle *handle, |
| 800 | struct lttng_event_context *ctx, const char *event_name, |
| 801 | const char *channel_name) |
| 802 | { |
| 803 | int ret; |
| 804 | size_t len = 0; |
| 805 | char *buf = NULL; |
| 806 | struct lttcomm_session_msg lsm; |
| 807 | |
| 808 | /* Safety check. Both are mandatory. */ |
| 809 | if (handle == NULL || ctx == NULL) { |
| 810 | ret = -LTTNG_ERR_INVALID; |
| 811 | goto end; |
| 812 | } |
| 813 | |
| 814 | memset(&lsm, 0, sizeof(lsm)); |
| 815 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
| 816 | |
| 817 | /* If no channel name, send empty string. */ |
| 818 | ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "", |
| 819 | sizeof(lsm.u.context.channel_name)); |
| 820 | if (ret) { |
| 821 | ret = -LTTNG_ERR_INVALID; |
| 822 | goto end; |
| 823 | } |
| 824 | |
| 825 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 826 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 827 | sizeof(lsm.session.name)); |
| 828 | if (ret) { |
| 829 | ret = -LTTNG_ERR_INVALID; |
| 830 | goto end; |
| 831 | } |
| 832 | |
| 833 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
| 834 | size_t provider_len, ctx_len; |
| 835 | const char *provider_name = ctx->u.app_ctx.provider_name; |
| 836 | const char *ctx_name = ctx->u.app_ctx.ctx_name; |
| 837 | |
| 838 | if (!provider_name || !ctx_name) { |
| 839 | ret = -LTTNG_ERR_INVALID; |
| 840 | goto end; |
| 841 | } |
| 842 | |
| 843 | provider_len = strlen(provider_name); |
| 844 | if (provider_len == 0) { |
| 845 | ret = -LTTNG_ERR_INVALID; |
| 846 | goto end; |
| 847 | } |
| 848 | lsm.u.context.provider_name_len = provider_len; |
| 849 | |
| 850 | ctx_len = strlen(ctx_name); |
| 851 | if (ctx_len == 0) { |
| 852 | ret = -LTTNG_ERR_INVALID; |
| 853 | goto end; |
| 854 | } |
| 855 | lsm.u.context.context_name_len = ctx_len; |
| 856 | |
| 857 | len = provider_len + ctx_len; |
| 858 | buf = zmalloc(len); |
| 859 | if (!buf) { |
| 860 | ret = -LTTNG_ERR_NOMEM; |
| 861 | goto end; |
| 862 | } |
| 863 | |
| 864 | memcpy(buf, provider_name, provider_len); |
| 865 | memcpy(buf + provider_len, ctx_name, ctx_len); |
| 866 | } |
| 867 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
| 868 | |
| 869 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
| 870 | /* |
| 871 | * Don't leak application addresses to the sessiond. |
| 872 | * This is only necessary when ctx is for an app ctx otherwise |
| 873 | * the values inside the union (type & config) are overwritten. |
| 874 | */ |
| 875 | lsm.u.context.ctx.u.app_ctx.provider_name = NULL; |
| 876 | lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; |
| 877 | } |
| 878 | |
| 879 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); |
| 880 | end: |
| 881 | free(buf); |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Enable event(s) for a channel. |
| 887 | * |
| 888 | * If no event name is specified, all events are enabled. |
| 889 | * If no channel name is specified, the default 'channel0' is used. |
| 890 | * |
| 891 | * Returns size of returned session payload data or a negative error code. |
| 892 | */ |
| 893 | int lttng_enable_event(struct lttng_handle *handle, |
| 894 | struct lttng_event *ev, const char *channel_name) |
| 895 | { |
| 896 | return lttng_enable_event_with_exclusions(handle, ev, channel_name, |
| 897 | NULL, 0, NULL); |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Create or enable an event with a filter expression. |
| 902 | * |
| 903 | * Return negative error value on error. |
| 904 | * Return size of returned session payload data if OK. |
| 905 | */ |
| 906 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
| 907 | struct lttng_event *event, const char *channel_name, |
| 908 | const char *filter_expression) |
| 909 | { |
| 910 | return lttng_enable_event_with_exclusions(handle, event, channel_name, |
| 911 | filter_expression, 0, NULL); |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Depending on the event, return a newly allocated agent filter expression or |
| 916 | * NULL if not applicable. |
| 917 | * |
| 918 | * An event with NO loglevel and the name is * will return NULL. |
| 919 | */ |
| 920 | static char *set_agent_filter(const char *filter, struct lttng_event *ev) |
| 921 | { |
| 922 | int err; |
| 923 | char *agent_filter = NULL; |
| 924 | |
| 925 | assert(ev); |
| 926 | |
| 927 | /* Don't add filter for the '*' event. */ |
| 928 | if (strcmp(ev->name, "*") != 0) { |
| 929 | if (filter) { |
| 930 | err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, |
| 931 | ev->name); |
| 932 | } else { |
| 933 | err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); |
| 934 | } |
| 935 | if (err < 0) { |
| 936 | PERROR("asprintf"); |
| 937 | goto error; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* Add loglevel filtering if any for the JUL domain. */ |
| 942 | if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { |
| 943 | const char *op; |
| 944 | |
| 945 | if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) { |
| 946 | op = ">="; |
| 947 | } else { |
| 948 | op = "=="; |
| 949 | } |
| 950 | |
| 951 | if (filter || agent_filter) { |
| 952 | char *new_filter; |
| 953 | |
| 954 | err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", |
| 955 | agent_filter ? agent_filter : filter, op, |
| 956 | ev->loglevel); |
| 957 | if (agent_filter) { |
| 958 | free(agent_filter); |
| 959 | } |
| 960 | agent_filter = new_filter; |
| 961 | } else { |
| 962 | err = asprintf(&agent_filter, "int_loglevel %s %d", op, |
| 963 | ev->loglevel); |
| 964 | } |
| 965 | if (err < 0) { |
| 966 | PERROR("asprintf"); |
| 967 | goto error; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return agent_filter; |
| 972 | error: |
| 973 | free(agent_filter); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Generate the filter bytecode from a given filter expression string. Put the |
| 979 | * newly allocated parser context in ctxp and populate the lsm object with the |
| 980 | * expression len. |
| 981 | * |
| 982 | * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched. |
| 983 | */ |
| 984 | static int generate_filter(char *filter_expression, |
| 985 | struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp) |
| 986 | { |
| 987 | int ret; |
| 988 | struct filter_parser_ctx *ctx = NULL; |
| 989 | FILE *fmem = NULL; |
| 990 | |
| 991 | assert(filter_expression); |
| 992 | assert(lsm); |
| 993 | assert(ctxp); |
| 994 | |
| 995 | /* |
| 996 | * Casting const to non-const, as the underlying function will use it in |
| 997 | * read-only mode. |
| 998 | */ |
| 999 | fmem = lttng_fmemopen((void *) filter_expression, |
| 1000 | strlen(filter_expression), "r"); |
| 1001 | if (!fmem) { |
| 1002 | fprintf(stderr, "Error opening memory as stream\n"); |
| 1003 | ret = -LTTNG_ERR_FILTER_NOMEM; |
| 1004 | goto error; |
| 1005 | } |
| 1006 | ctx = filter_parser_ctx_alloc(fmem); |
| 1007 | if (!ctx) { |
| 1008 | fprintf(stderr, "Error allocating parser\n"); |
| 1009 | ret = -LTTNG_ERR_FILTER_NOMEM; |
| 1010 | goto filter_alloc_error; |
| 1011 | } |
| 1012 | ret = filter_parser_ctx_append_ast(ctx); |
| 1013 | if (ret) { |
| 1014 | fprintf(stderr, "Parse error\n"); |
| 1015 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1016 | goto parse_error; |
| 1017 | } |
| 1018 | if (print_xml) { |
| 1019 | ret = filter_visitor_print_xml(ctx, stdout, 0); |
| 1020 | if (ret) { |
| 1021 | fflush(stdout); |
| 1022 | fprintf(stderr, "XML print error\n"); |
| 1023 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1024 | goto parse_error; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | dbg_printf("Generating IR... "); |
| 1029 | fflush(stdout); |
| 1030 | ret = filter_visitor_ir_generate(ctx); |
| 1031 | if (ret) { |
| 1032 | fprintf(stderr, "Generate IR error\n"); |
| 1033 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1034 | goto parse_error; |
| 1035 | } |
| 1036 | dbg_printf("done\n"); |
| 1037 | |
| 1038 | dbg_printf("Validating IR... "); |
| 1039 | fflush(stdout); |
| 1040 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); |
| 1041 | if (ret) { |
| 1042 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1043 | goto parse_error; |
| 1044 | } |
| 1045 | |
| 1046 | /* Normalize globbing patterns in the expression. */ |
| 1047 | ret = filter_visitor_ir_normalize_glob_patterns(ctx); |
| 1048 | if (ret) { |
| 1049 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1050 | goto parse_error; |
| 1051 | } |
| 1052 | |
| 1053 | /* Validate strings used as literals in the expression. */ |
| 1054 | ret = filter_visitor_ir_validate_string(ctx); |
| 1055 | if (ret) { |
| 1056 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1057 | goto parse_error; |
| 1058 | } |
| 1059 | |
| 1060 | /* Validate globbing patterns in the expression. */ |
| 1061 | ret = filter_visitor_ir_validate_globbing(ctx); |
| 1062 | if (ret) { |
| 1063 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1064 | goto parse_error; |
| 1065 | } |
| 1066 | |
| 1067 | dbg_printf("done\n"); |
| 1068 | |
| 1069 | dbg_printf("Generating bytecode... "); |
| 1070 | fflush(stdout); |
| 1071 | ret = filter_visitor_bytecode_generate(ctx); |
| 1072 | if (ret) { |
| 1073 | fprintf(stderr, "Generate bytecode error\n"); |
| 1074 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 1075 | goto parse_error; |
| 1076 | } |
| 1077 | dbg_printf("done\n"); |
| 1078 | dbg_printf("Size of bytecode generated: %u bytes.\n", |
| 1079 | bytecode_get_len(&ctx->bytecode->b)); |
| 1080 | |
| 1081 | lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b) |
| 1082 | + bytecode_get_len(&ctx->bytecode->b); |
| 1083 | lsm->u.enable.expression_len = strlen(filter_expression) + 1; |
| 1084 | |
| 1085 | /* No need to keep the memory stream. */ |
| 1086 | if (fclose(fmem) != 0) { |
| 1087 | PERROR("fclose"); |
| 1088 | } |
| 1089 | |
| 1090 | *ctxp = ctx; |
| 1091 | return 0; |
| 1092 | |
| 1093 | parse_error: |
| 1094 | filter_ir_free(ctx); |
| 1095 | filter_parser_ctx_free(ctx); |
| 1096 | filter_alloc_error: |
| 1097 | if (fclose(fmem) != 0) { |
| 1098 | PERROR("fclose"); |
| 1099 | } |
| 1100 | error: |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Enable event(s) for a channel, possibly with exclusions and a filter. |
| 1106 | * If no event name is specified, all events are enabled. |
| 1107 | * If no channel name is specified, the default name is used. |
| 1108 | * If filter expression is not NULL, the filter is set for the event. |
| 1109 | * If exclusion count is not zero, the exclusions are set for the event. |
| 1110 | * Returns size of returned session payload data or a negative error code. |
| 1111 | */ |
| 1112 | int lttng_enable_event_with_exclusions(struct lttng_handle *handle, |
| 1113 | struct lttng_event *ev, const char *channel_name, |
| 1114 | const char *original_filter_expression, |
| 1115 | int exclusion_count, char **exclusion_list) |
| 1116 | { |
| 1117 | struct lttcomm_session_msg lsm; |
| 1118 | struct lttng_dynamic_buffer send_buffer; |
| 1119 | int ret = 0, i, fd_to_send = -1; |
| 1120 | bool send_fd = false; |
| 1121 | unsigned int free_filter_expression = 0; |
| 1122 | struct filter_parser_ctx *ctx = NULL; |
| 1123 | |
| 1124 | /* |
| 1125 | * We have either a filter or some exclusions, so we need to set up |
| 1126 | * a variable-length memory block from where to send the data. |
| 1127 | */ |
| 1128 | lttng_dynamic_buffer_init(&send_buffer); |
| 1129 | |
| 1130 | /* |
| 1131 | * Cast as non-const since we may replace the filter expression |
| 1132 | * by a dynamically allocated string. Otherwise, the original |
| 1133 | * string is not modified. |
| 1134 | */ |
| 1135 | char *filter_expression = (char *) original_filter_expression; |
| 1136 | |
| 1137 | if (handle == NULL || ev == NULL) { |
| 1138 | ret = -LTTNG_ERR_INVALID; |
| 1139 | goto error; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Empty filter string will always be rejected by the parser |
| 1144 | * anyway, so treat this corner-case early to eliminate |
| 1145 | * lttng_fmemopen error for 0-byte allocation. |
| 1146 | */ |
| 1147 | if (filter_expression && filter_expression[0] == '\0') { |
| 1148 | ret = -LTTNG_ERR_INVALID; |
| 1149 | goto error; |
| 1150 | } |
| 1151 | |
| 1152 | memset(&lsm, 0, sizeof(lsm)); |
| 1153 | |
| 1154 | /* If no channel name, send empty string. */ |
| 1155 | ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "", |
| 1156 | sizeof(lsm.u.enable.channel_name)); |
| 1157 | if (ret) { |
| 1158 | ret = -LTTNG_ERR_INVALID; |
| 1159 | goto error; |
| 1160 | } |
| 1161 | |
| 1162 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
| 1163 | if (ev->name[0] == '\0') { |
| 1164 | /* Enable all events. */ |
| 1165 | ret = lttng_strncpy(ev->name, "*", sizeof(ev->name)); |
| 1166 | assert(ret == 0); |
| 1167 | } |
| 1168 | |
| 1169 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1170 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
| 1171 | |
| 1172 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 1173 | sizeof(lsm.session.name)); |
| 1174 | if (ret) { |
| 1175 | ret = -LTTNG_ERR_INVALID; |
| 1176 | goto error; |
| 1177 | } |
| 1178 | |
| 1179 | lsm.u.enable.exclusion_count = exclusion_count; |
| 1180 | lsm.u.enable.bytecode_len = 0; |
| 1181 | |
| 1182 | /* Parse filter expression. */ |
| 1183 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
| 1184 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
| 1185 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { |
| 1186 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
| 1187 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
| 1188 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { |
| 1189 | char *agent_filter; |
| 1190 | |
| 1191 | /* Setup JUL filter if needed. */ |
| 1192 | agent_filter = set_agent_filter(filter_expression, ev); |
| 1193 | if (!agent_filter) { |
| 1194 | if (!filter_expression) { |
| 1195 | /* |
| 1196 | * No JUL and no filter, just skip |
| 1197 | * everything below. |
| 1198 | */ |
| 1199 | goto ask_sessiond; |
| 1200 | } |
| 1201 | } else { |
| 1202 | /* |
| 1203 | * With an agent filter, the original filter has |
| 1204 | * been added to it thus replace the filter |
| 1205 | * expression. |
| 1206 | */ |
| 1207 | filter_expression = agent_filter; |
| 1208 | free_filter_expression = 1; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | ret = generate_filter(filter_expression, &lsm, &ctx); |
| 1213 | if (ret) { |
| 1214 | goto filter_error; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | ret = lttng_dynamic_buffer_set_capacity(&send_buffer, |
| 1219 | lsm.u.enable.bytecode_len |
| 1220 | + lsm.u.enable.expression_len |
| 1221 | + LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
| 1222 | if (ret) { |
| 1223 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; |
| 1224 | goto mem_error; |
| 1225 | } |
| 1226 | |
| 1227 | /* Put exclusion names first in the data. */ |
| 1228 | for (i = 0; i < exclusion_count; i++) { |
| 1229 | size_t exclusion_len; |
| 1230 | |
| 1231 | exclusion_len = lttng_strnlen(*(exclusion_list + i), |
| 1232 | LTTNG_SYMBOL_NAME_LEN); |
| 1233 | if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) { |
| 1234 | /* Exclusion is not NULL-terminated. */ |
| 1235 | ret = -LTTNG_ERR_INVALID; |
| 1236 | goto mem_error; |
| 1237 | } |
| 1238 | |
| 1239 | ret = lttng_dynamic_buffer_append(&send_buffer, |
| 1240 | *(exclusion_list + i), |
| 1241 | LTTNG_SYMBOL_NAME_LEN); |
| 1242 | if (ret) { |
| 1243 | goto mem_error; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* Add filter expression next. */ |
| 1248 | if (filter_expression) { |
| 1249 | ret = lttng_dynamic_buffer_append(&send_buffer, |
| 1250 | filter_expression, lsm.u.enable.expression_len); |
| 1251 | if (ret) { |
| 1252 | goto mem_error; |
| 1253 | } |
| 1254 | } |
| 1255 | /* Add filter bytecode next. */ |
| 1256 | if (ctx && lsm.u.enable.bytecode_len != 0) { |
| 1257 | ret = lttng_dynamic_buffer_append(&send_buffer, |
| 1258 | &ctx->bytecode->b, lsm.u.enable.bytecode_len); |
| 1259 | if (ret) { |
| 1260 | goto mem_error; |
| 1261 | } |
| 1262 | } |
| 1263 | if (ev->extended.ptr) { |
| 1264 | struct lttng_event_extended *ev_ext = |
| 1265 | (struct lttng_event_extended *) ev->extended.ptr; |
| 1266 | |
| 1267 | if (ev_ext->probe_location) { |
| 1268 | /* |
| 1269 | * lttng_userspace_probe_location_serialize returns the |
| 1270 | * number of bytes that was appended to the buffer. |
| 1271 | */ |
| 1272 | ret = lttng_userspace_probe_location_serialize( |
| 1273 | ev_ext->probe_location, &send_buffer, |
| 1274 | &fd_to_send); |
| 1275 | if (ret < 0) { |
| 1276 | goto mem_error; |
| 1277 | } |
| 1278 | |
| 1279 | send_fd = fd_to_send >= 0; |
| 1280 | /* |
| 1281 | * Set the size of the userspace probe location element |
| 1282 | * of the buffer so that the receiving side knows where |
| 1283 | * to split it. |
| 1284 | */ |
| 1285 | lsm.u.enable.userspace_probe_location_len = ret; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, |
| 1290 | send_fd ? &fd_to_send : NULL, |
| 1291 | send_fd ? 1 : 0, |
| 1292 | send_buffer.size ? send_buffer.data : NULL, |
| 1293 | send_buffer.size, NULL, NULL, 0); |
| 1294 | |
| 1295 | mem_error: |
| 1296 | if (filter_expression && ctx) { |
| 1297 | filter_bytecode_free(ctx); |
| 1298 | filter_ir_free(ctx); |
| 1299 | filter_parser_ctx_free(ctx); |
| 1300 | } |
| 1301 | filter_error: |
| 1302 | if (free_filter_expression) { |
| 1303 | /* |
| 1304 | * The filter expression has been replaced and must be freed as |
| 1305 | * it is not the original filter expression received as a |
| 1306 | * parameter. |
| 1307 | */ |
| 1308 | free(filter_expression); |
| 1309 | } |
| 1310 | error: |
| 1311 | /* |
| 1312 | * Return directly to the caller and don't ask the sessiond since |
| 1313 | * something went wrong in the parsing of data above. |
| 1314 | */ |
| 1315 | lttng_dynamic_buffer_reset(&send_buffer); |
| 1316 | return ret; |
| 1317 | |
| 1318 | ask_sessiond: |
| 1319 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | int lttng_disable_event_ext(struct lttng_handle *handle, |
| 1324 | struct lttng_event *ev, const char *channel_name, |
| 1325 | const char *original_filter_expression) |
| 1326 | { |
| 1327 | struct lttcomm_session_msg lsm; |
| 1328 | char *varlen_data; |
| 1329 | int ret = 0; |
| 1330 | unsigned int free_filter_expression = 0; |
| 1331 | struct filter_parser_ctx *ctx = NULL; |
| 1332 | /* |
| 1333 | * Cast as non-const since we may replace the filter expression |
| 1334 | * by a dynamically allocated string. Otherwise, the original |
| 1335 | * string is not modified. |
| 1336 | */ |
| 1337 | char *filter_expression = (char *) original_filter_expression; |
| 1338 | |
| 1339 | if (handle == NULL || ev == NULL) { |
| 1340 | ret = -LTTNG_ERR_INVALID; |
| 1341 | goto error; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Empty filter string will always be rejected by the parser |
| 1346 | * anyway, so treat this corner-case early to eliminate |
| 1347 | * lttng_fmemopen error for 0-byte allocation. |
| 1348 | */ |
| 1349 | if (filter_expression && filter_expression[0] == '\0') { |
| 1350 | ret = -LTTNG_ERR_INVALID; |
| 1351 | goto error; |
| 1352 | } |
| 1353 | |
| 1354 | memset(&lsm, 0, sizeof(lsm)); |
| 1355 | |
| 1356 | /* If no channel name, send empty string. */ |
| 1357 | ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "", |
| 1358 | sizeof(lsm.u.disable.channel_name)); |
| 1359 | if (ret) { |
| 1360 | ret = -LTTNG_ERR_INVALID; |
| 1361 | goto error; |
| 1362 | } |
| 1363 | |
| 1364 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
| 1365 | |
| 1366 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1367 | memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event)); |
| 1368 | |
| 1369 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 1370 | sizeof(lsm.session.name)); |
| 1371 | if (ret) { |
| 1372 | ret = -LTTNG_ERR_INVALID; |
| 1373 | goto error; |
| 1374 | } |
| 1375 | |
| 1376 | lsm.u.disable.bytecode_len = 0; |
| 1377 | |
| 1378 | /* |
| 1379 | * For the JUL domain, a filter is enforced except for the |
| 1380 | * disable all event. This is done to avoid having the event in |
| 1381 | * all sessions thus filtering by logger name. |
| 1382 | */ |
| 1383 | if (filter_expression == NULL && |
| 1384 | (handle->domain.type != LTTNG_DOMAIN_JUL && |
| 1385 | handle->domain.type != LTTNG_DOMAIN_LOG4J && |
| 1386 | handle->domain.type != LTTNG_DOMAIN_PYTHON)) { |
| 1387 | goto ask_sessiond; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * We have a filter, so we need to set up a variable-length |
| 1392 | * memory block from where to send the data. |
| 1393 | */ |
| 1394 | |
| 1395 | /* Parse filter expression */ |
| 1396 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
| 1397 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
| 1398 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { |
| 1399 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
| 1400 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
| 1401 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { |
| 1402 | char *agent_filter; |
| 1403 | |
| 1404 | /* Setup JUL filter if needed. */ |
| 1405 | agent_filter = set_agent_filter(filter_expression, ev); |
| 1406 | if (!agent_filter) { |
| 1407 | if (!filter_expression) { |
| 1408 | /* |
| 1409 | * No JUL and no filter, just skip |
| 1410 | * everything below. |
| 1411 | */ |
| 1412 | goto ask_sessiond; |
| 1413 | } |
| 1414 | } else { |
| 1415 | /* |
| 1416 | * With a JUL filter, the original filter has |
| 1417 | * been added to it thus replace the filter |
| 1418 | * expression. |
| 1419 | */ |
| 1420 | filter_expression = agent_filter; |
| 1421 | free_filter_expression = 1; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | ret = generate_filter(filter_expression, &lsm, &ctx); |
| 1426 | if (ret) { |
| 1427 | goto filter_error; |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | varlen_data = zmalloc(lsm.u.disable.bytecode_len |
| 1432 | + lsm.u.disable.expression_len); |
| 1433 | if (!varlen_data) { |
| 1434 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; |
| 1435 | goto mem_error; |
| 1436 | } |
| 1437 | |
| 1438 | /* Add filter expression. */ |
| 1439 | if (lsm.u.disable.expression_len != 0) { |
| 1440 | memcpy(varlen_data, |
| 1441 | filter_expression, |
| 1442 | lsm.u.disable.expression_len); |
| 1443 | } |
| 1444 | /* Add filter bytecode next. */ |
| 1445 | if (ctx && lsm.u.disable.bytecode_len != 0) { |
| 1446 | memcpy(varlen_data |
| 1447 | + lsm.u.disable.expression_len, |
| 1448 | &ctx->bytecode->b, |
| 1449 | lsm.u.disable.bytecode_len); |
| 1450 | } |
| 1451 | |
| 1452 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, |
| 1453 | lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL); |
| 1454 | free(varlen_data); |
| 1455 | |
| 1456 | mem_error: |
| 1457 | if (filter_expression && ctx) { |
| 1458 | filter_bytecode_free(ctx); |
| 1459 | filter_ir_free(ctx); |
| 1460 | filter_parser_ctx_free(ctx); |
| 1461 | } |
| 1462 | filter_error: |
| 1463 | if (free_filter_expression) { |
| 1464 | /* |
| 1465 | * The filter expression has been replaced and must be freed as |
| 1466 | * it is not the original filter expression received as a |
| 1467 | * parameter. |
| 1468 | */ |
| 1469 | free(filter_expression); |
| 1470 | } |
| 1471 | error: |
| 1472 | /* |
| 1473 | * Return directly to the caller and don't ask the sessiond since |
| 1474 | * something went wrong in the parsing of data above. |
| 1475 | */ |
| 1476 | return ret; |
| 1477 | |
| 1478 | ask_sessiond: |
| 1479 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * Disable event(s) of a channel and domain. |
| 1485 | * If no event name is specified, all events are disabled. |
| 1486 | * If no channel name is specified, the default 'channel0' is used. |
| 1487 | * Returns size of returned session payload data or a negative error code. |
| 1488 | */ |
| 1489 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
| 1490 | const char *channel_name) |
| 1491 | { |
| 1492 | int ret; |
| 1493 | struct lttng_event ev; |
| 1494 | |
| 1495 | memset(&ev, 0, sizeof(ev)); |
| 1496 | ev.loglevel = -1; |
| 1497 | ev.type = LTTNG_EVENT_ALL; |
| 1498 | ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name)); |
| 1499 | if (ret) { |
| 1500 | ret = -LTTNG_ERR_INVALID; |
| 1501 | goto end; |
| 1502 | } |
| 1503 | |
| 1504 | ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL); |
| 1505 | end: |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) |
| 1510 | { |
| 1511 | struct lttng_channel *channel = NULL; |
| 1512 | |
| 1513 | if (!domain) { |
| 1514 | goto end; |
| 1515 | } |
| 1516 | |
| 1517 | /* Validate domain. */ |
| 1518 | switch (domain->type) { |
| 1519 | case LTTNG_DOMAIN_UST: |
| 1520 | switch (domain->buf_type) { |
| 1521 | case LTTNG_BUFFER_PER_UID: |
| 1522 | case LTTNG_BUFFER_PER_PID: |
| 1523 | break; |
| 1524 | default: |
| 1525 | goto end; |
| 1526 | } |
| 1527 | break; |
| 1528 | case LTTNG_DOMAIN_KERNEL: |
| 1529 | if (domain->buf_type != LTTNG_BUFFER_GLOBAL) { |
| 1530 | goto end; |
| 1531 | } |
| 1532 | break; |
| 1533 | default: |
| 1534 | goto end; |
| 1535 | } |
| 1536 | |
| 1537 | channel = lttng_channel_create_internal(); |
| 1538 | if (!channel) { |
| 1539 | goto end; |
| 1540 | } |
| 1541 | |
| 1542 | lttng_channel_set_default_attr(domain, &channel->attr); |
| 1543 | end: |
| 1544 | return channel; |
| 1545 | } |
| 1546 | |
| 1547 | void lttng_channel_destroy(struct lttng_channel *channel) |
| 1548 | { |
| 1549 | if (!channel) { |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | if (channel->attr.extended.ptr) { |
| 1554 | free(channel->attr.extended.ptr); |
| 1555 | } |
| 1556 | free(channel); |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Enable channel per domain |
| 1561 | * Returns size of returned session payload data or a negative error code. |
| 1562 | */ |
| 1563 | int lttng_enable_channel(struct lttng_handle *handle, |
| 1564 | struct lttng_channel *in_chan) |
| 1565 | { |
| 1566 | enum lttng_error_code ret_code; |
| 1567 | int ret; |
| 1568 | struct lttng_dynamic_buffer buffer; |
| 1569 | struct lttcomm_session_msg lsm; |
| 1570 | uint64_t total_buffer_size_needed_per_cpu = 0; |
| 1571 | struct lttng_channel *channel = NULL; |
| 1572 | |
| 1573 | lttng_dynamic_buffer_init(&buffer); |
| 1574 | |
| 1575 | /* NULL arguments are forbidden. No default values. */ |
| 1576 | if (handle == NULL || in_chan == NULL) { |
| 1577 | ret = -LTTNG_ERR_INVALID; |
| 1578 | goto end; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Verify that the amount of memory required to create the requested |
| 1583 | * buffer is available on the system at the moment. |
| 1584 | */ |
| 1585 | if (in_chan->attr.num_subbuf > |
| 1586 | UINT64_MAX / in_chan->attr.subbuf_size) { |
| 1587 | /* Overflow */ |
| 1588 | ret = -LTTNG_ERR_OVERFLOW; |
| 1589 | goto end; |
| 1590 | } |
| 1591 | |
| 1592 | total_buffer_size_needed_per_cpu = |
| 1593 | in_chan->attr.num_subbuf * in_chan->attr.subbuf_size; |
| 1594 | ret_code = check_enough_available_memory( |
| 1595 | total_buffer_size_needed_per_cpu); |
| 1596 | if (ret_code != LTTNG_OK) { |
| 1597 | ret = -ret_code; |
| 1598 | goto end; |
| 1599 | } |
| 1600 | |
| 1601 | /* Copy the channel for easier manipulation. */ |
| 1602 | channel = lttng_channel_copy(in_chan); |
| 1603 | if (!channel) { |
| 1604 | ret = -LTTNG_ERR_NOMEM; |
| 1605 | goto end; |
| 1606 | } |
| 1607 | |
| 1608 | /* Populate the channel extended attribute if necessary. */ |
| 1609 | if (!channel->attr.extended.ptr) { |
| 1610 | struct lttng_channel_extended *extended = |
| 1611 | zmalloc(sizeof(*extended)); |
| 1612 | |
| 1613 | if (!extended) { |
| 1614 | ret = -LTTNG_ERR_NOMEM; |
| 1615 | goto end; |
| 1616 | } |
| 1617 | lttng_channel_set_default_extended_attr( |
| 1618 | &handle->domain, extended); |
| 1619 | channel->attr.extended.ptr = extended; |
| 1620 | } |
| 1621 | |
| 1622 | /* Prepare the payload */ |
| 1623 | memset(&lsm, 0, sizeof(lsm)); |
| 1624 | |
| 1625 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
| 1626 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1627 | |
| 1628 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 1629 | sizeof(lsm.session.name)); |
| 1630 | if (ret) { |
| 1631 | ret = -LTTNG_ERR_INVALID; |
| 1632 | goto end; |
| 1633 | } |
| 1634 | |
| 1635 | ret = lttng_channel_serialize(channel, &buffer); |
| 1636 | if (ret) { |
| 1637 | ret = -LTTNG_ERR_FATAL; |
| 1638 | goto end; |
| 1639 | } |
| 1640 | |
| 1641 | lsm.u.channel.length = buffer.size; |
| 1642 | |
| 1643 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( |
| 1644 | &lsm, buffer.data, buffer.size, NULL); |
| 1645 | end: |
| 1646 | lttng_channel_destroy(channel); |
| 1647 | lttng_dynamic_buffer_reset(&buffer); |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * All tracing will be stopped for registered events of the channel. |
| 1653 | * Returns size of returned session payload data or a negative error code. |
| 1654 | */ |
| 1655 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
| 1656 | { |
| 1657 | int ret; |
| 1658 | struct lttcomm_session_msg lsm; |
| 1659 | |
| 1660 | /* Safety check. Both are mandatory. */ |
| 1661 | if (handle == NULL || name == NULL) { |
| 1662 | return -LTTNG_ERR_INVALID; |
| 1663 | } |
| 1664 | |
| 1665 | memset(&lsm, 0, sizeof(lsm)); |
| 1666 | |
| 1667 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
| 1668 | |
| 1669 | ret = lttng_strncpy(lsm.u.disable.channel_name, name, |
| 1670 | sizeof(lsm.u.disable.channel_name)); |
| 1671 | if (ret) { |
| 1672 | ret = -LTTNG_ERR_INVALID; |
| 1673 | goto end; |
| 1674 | } |
| 1675 | |
| 1676 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1677 | |
| 1678 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 1679 | sizeof(lsm.session.name)); |
| 1680 | if (ret) { |
| 1681 | ret = -LTTNG_ERR_INVALID; |
| 1682 | goto end; |
| 1683 | } |
| 1684 | |
| 1685 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1686 | end: |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Lists all available tracepoints of domain. |
| 1692 | * Sets the contents of the events array. |
| 1693 | * Returns the number of lttng_event entries in events; |
| 1694 | * on error, returns a negative value. |
| 1695 | */ |
| 1696 | int lttng_list_tracepoints(struct lttng_handle *handle, |
| 1697 | struct lttng_event **events) |
| 1698 | { |
| 1699 | int ret; |
| 1700 | struct lttcomm_session_msg lsm; |
| 1701 | |
| 1702 | if (handle == NULL) { |
| 1703 | return -LTTNG_ERR_INVALID; |
| 1704 | } |
| 1705 | |
| 1706 | memset(&lsm, 0, sizeof(lsm)); |
| 1707 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
| 1708 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1709 | |
| 1710 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
| 1711 | if (ret < 0) { |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
| 1715 | return ret / sizeof(struct lttng_event); |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * Lists all available tracepoint fields of domain. |
| 1720 | * Sets the contents of the event field array. |
| 1721 | * Returns the number of lttng_event_field entries in events; |
| 1722 | * on error, returns a negative value. |
| 1723 | */ |
| 1724 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, |
| 1725 | struct lttng_event_field **fields) |
| 1726 | { |
| 1727 | int ret; |
| 1728 | struct lttcomm_session_msg lsm; |
| 1729 | |
| 1730 | if (handle == NULL) { |
| 1731 | return -LTTNG_ERR_INVALID; |
| 1732 | } |
| 1733 | |
| 1734 | memset(&lsm, 0, sizeof(lsm)); |
| 1735 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
| 1736 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 1737 | |
| 1738 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
| 1739 | if (ret < 0) { |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | return ret / sizeof(struct lttng_event_field); |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Lists all available kernel system calls. Allocates and sets the contents of |
| 1748 | * the events array. |
| 1749 | * |
| 1750 | * Returns the number of lttng_event entries in events; on error, returns a |
| 1751 | * negative value. |
| 1752 | */ |
| 1753 | int lttng_list_syscalls(struct lttng_event **events) |
| 1754 | { |
| 1755 | int ret; |
| 1756 | struct lttcomm_session_msg lsm; |
| 1757 | |
| 1758 | if (!events) { |
| 1759 | return -LTTNG_ERR_INVALID; |
| 1760 | } |
| 1761 | |
| 1762 | memset(&lsm, 0, sizeof(lsm)); |
| 1763 | lsm.cmd_type = LTTNG_LIST_SYSCALLS; |
| 1764 | /* Force kernel domain for system calls. */ |
| 1765 | lsm.domain.type = LTTNG_DOMAIN_KERNEL; |
| 1766 | |
| 1767 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
| 1768 | if (ret < 0) { |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | return ret / sizeof(struct lttng_event); |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Returns a human readable string describing |
| 1777 | * the error code (a negative value). |
| 1778 | */ |
| 1779 | const char *lttng_strerror(int code) |
| 1780 | { |
| 1781 | return error_get_str(code); |
| 1782 | } |
| 1783 | |
| 1784 | enum lttng_error_code lttng_create_session_ext( |
| 1785 | struct lttng_session_descriptor *session_descriptor) |
| 1786 | { |
| 1787 | enum lttng_error_code ret_code; |
| 1788 | struct lttcomm_session_msg lsm = { |
| 1789 | .cmd_type = LTTNG_CREATE_SESSION_EXT, |
| 1790 | }; |
| 1791 | void *reply = NULL; |
| 1792 | struct lttng_buffer_view reply_view; |
| 1793 | int reply_ret; |
| 1794 | bool sessiond_must_generate_ouput; |
| 1795 | struct lttng_dynamic_buffer payload; |
| 1796 | int ret; |
| 1797 | size_t descriptor_size; |
| 1798 | struct lttng_session_descriptor *descriptor_reply = NULL; |
| 1799 | |
| 1800 | lttng_dynamic_buffer_init(&payload); |
| 1801 | if (!session_descriptor) { |
| 1802 | ret_code = LTTNG_ERR_INVALID; |
| 1803 | goto end; |
| 1804 | } |
| 1805 | |
| 1806 | sessiond_must_generate_ouput = |
| 1807 | !lttng_session_descriptor_is_output_destination_initialized( |
| 1808 | session_descriptor); |
| 1809 | if (sessiond_must_generate_ouput) { |
| 1810 | const char *home_dir = utils_get_home_dir(); |
| 1811 | size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0; |
| 1812 | |
| 1813 | if (!home_dir || home_dir_len > LTTNG_PATH_MAX) { |
| 1814 | ret_code = LTTNG_ERR_FATAL; |
| 1815 | goto end; |
| 1816 | } |
| 1817 | |
| 1818 | lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; |
| 1819 | ret = lttng_dynamic_buffer_append(&payload, home_dir, |
| 1820 | home_dir_len); |
| 1821 | if (ret) { |
| 1822 | ret_code = LTTNG_ERR_NOMEM; |
| 1823 | goto end; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | descriptor_size = payload.size; |
| 1828 | ret = lttng_session_descriptor_serialize(session_descriptor, |
| 1829 | &payload); |
| 1830 | if (ret) { |
| 1831 | ret_code = LTTNG_ERR_INVALID; |
| 1832 | goto end; |
| 1833 | } |
| 1834 | descriptor_size = payload.size - descriptor_size; |
| 1835 | lsm.u.create_session.session_descriptor_size = descriptor_size; |
| 1836 | |
| 1837 | /* Command returns a session descriptor on success. */ |
| 1838 | reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, |
| 1839 | payload.size, &reply); |
| 1840 | if (reply_ret < 0) { |
| 1841 | ret_code = -reply_ret; |
| 1842 | goto end; |
| 1843 | } else if (reply_ret == 0) { |
| 1844 | /* Socket unexpectedly closed by the session daemon. */ |
| 1845 | ret_code = LTTNG_ERR_FATAL; |
| 1846 | goto end; |
| 1847 | } |
| 1848 | |
| 1849 | reply_view = lttng_buffer_view_init(reply, 0, reply_ret); |
| 1850 | ret = lttng_session_descriptor_create_from_buffer(&reply_view, |
| 1851 | &descriptor_reply); |
| 1852 | if (ret < 0) { |
| 1853 | ret_code = LTTNG_ERR_FATAL; |
| 1854 | goto end; |
| 1855 | } |
| 1856 | ret_code = LTTNG_OK; |
| 1857 | lttng_session_descriptor_assign(session_descriptor, descriptor_reply); |
| 1858 | end: |
| 1859 | free(reply); |
| 1860 | lttng_dynamic_buffer_reset(&payload); |
| 1861 | lttng_session_descriptor_destroy(descriptor_reply); |
| 1862 | return ret_code; |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Create a new session using name and url for destination. |
| 1867 | * |
| 1868 | * Return 0 on success else a negative LTTng error code. |
| 1869 | */ |
| 1870 | int lttng_create_session(const char *name, const char *url) |
| 1871 | { |
| 1872 | int ret; |
| 1873 | ssize_t size; |
| 1874 | struct lttng_uri *uris = NULL; |
| 1875 | struct lttng_session_descriptor *descriptor = NULL; |
| 1876 | enum lttng_error_code ret_code; |
| 1877 | |
| 1878 | if (!name) { |
| 1879 | ret = -LTTNG_ERR_INVALID; |
| 1880 | goto end; |
| 1881 | } |
| 1882 | |
| 1883 | size = uri_parse_str_urls(url, NULL, &uris); |
| 1884 | if (size < 0) { |
| 1885 | ret = -LTTNG_ERR_INVALID; |
| 1886 | goto end; |
| 1887 | } |
| 1888 | switch (size) { |
| 1889 | case 0: |
| 1890 | descriptor = lttng_session_descriptor_create(name); |
| 1891 | break; |
| 1892 | case 1: |
| 1893 | if (uris[0].dtype != LTTNG_DST_PATH) { |
| 1894 | ret = -LTTNG_ERR_INVALID; |
| 1895 | goto end; |
| 1896 | } |
| 1897 | descriptor = lttng_session_descriptor_local_create(name, |
| 1898 | uris[0].dst.path); |
| 1899 | break; |
| 1900 | case 2: |
| 1901 | descriptor = lttng_session_descriptor_network_create(name, url, |
| 1902 | NULL); |
| 1903 | break; |
| 1904 | default: |
| 1905 | ret = -LTTNG_ERR_INVALID; |
| 1906 | goto end; |
| 1907 | } |
| 1908 | if (!descriptor) { |
| 1909 | ret = -LTTNG_ERR_INVALID; |
| 1910 | goto end; |
| 1911 | } |
| 1912 | ret_code = lttng_create_session_ext(descriptor); |
| 1913 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
| 1914 | end: |
| 1915 | lttng_session_descriptor_destroy(descriptor); |
| 1916 | free(uris); |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * Create a session exclusively used for snapshot. |
| 1922 | * |
| 1923 | * Return 0 on success else a negative LTTng error code. |
| 1924 | */ |
| 1925 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) |
| 1926 | { |
| 1927 | int ret; |
| 1928 | enum lttng_error_code ret_code; |
| 1929 | ssize_t size; |
| 1930 | struct lttng_uri *uris = NULL; |
| 1931 | struct lttng_session_descriptor *descriptor = NULL; |
| 1932 | |
| 1933 | if (!name) { |
| 1934 | ret = -LTTNG_ERR_INVALID; |
| 1935 | goto end; |
| 1936 | } |
| 1937 | |
| 1938 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); |
| 1939 | if (size < 0) { |
| 1940 | ret = -LTTNG_ERR_INVALID; |
| 1941 | goto end; |
| 1942 | } |
| 1943 | /* |
| 1944 | * If the user does not specify a custom subdir, use the session name. |
| 1945 | */ |
| 1946 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && |
| 1947 | strlen(uris[0].subdir) == 0) { |
| 1948 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", |
| 1949 | name); |
| 1950 | if (ret < 0) { |
| 1951 | PERROR("Failed to set session name as network destination sub-directory"); |
| 1952 | ret = -LTTNG_ERR_FATAL; |
| 1953 | goto end; |
| 1954 | } else if (ret >= sizeof(uris[0].subdir)) { |
| 1955 | /* Truncated output. */ |
| 1956 | ret = -LTTNG_ERR_INVALID; |
| 1957 | goto end; |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | switch (size) { |
| 1962 | case 0: |
| 1963 | descriptor = lttng_session_descriptor_snapshot_create(name); |
| 1964 | break; |
| 1965 | case 1: |
| 1966 | if (uris[0].dtype != LTTNG_DST_PATH) { |
| 1967 | ret = -LTTNG_ERR_INVALID; |
| 1968 | goto end; |
| 1969 | } |
| 1970 | descriptor = lttng_session_descriptor_snapshot_local_create( |
| 1971 | name, |
| 1972 | uris[0].dst.path); |
| 1973 | break; |
| 1974 | case 2: |
| 1975 | descriptor = lttng_session_descriptor_snapshot_network_create( |
| 1976 | name, |
| 1977 | snapshot_url, |
| 1978 | NULL); |
| 1979 | break; |
| 1980 | default: |
| 1981 | ret = -LTTNG_ERR_INVALID; |
| 1982 | goto end; |
| 1983 | } |
| 1984 | if (!descriptor) { |
| 1985 | ret = -LTTNG_ERR_INVALID; |
| 1986 | goto end; |
| 1987 | } |
| 1988 | ret_code = lttng_create_session_ext(descriptor); |
| 1989 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
| 1990 | end: |
| 1991 | lttng_session_descriptor_destroy(descriptor); |
| 1992 | free(uris); |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * Create a session exclusively used for live. |
| 1998 | * |
| 1999 | * Return 0 on success else a negative LTTng error code. |
| 2000 | */ |
| 2001 | int lttng_create_session_live(const char *name, const char *url, |
| 2002 | unsigned int timer_interval) |
| 2003 | { |
| 2004 | int ret; |
| 2005 | enum lttng_error_code ret_code; |
| 2006 | struct lttng_session_descriptor *descriptor = NULL; |
| 2007 | |
| 2008 | if (!name) { |
| 2009 | ret = -LTTNG_ERR_INVALID; |
| 2010 | goto end; |
| 2011 | } |
| 2012 | |
| 2013 | if (url) { |
| 2014 | descriptor = lttng_session_descriptor_live_network_create( |
| 2015 | name, url, NULL, timer_interval); |
| 2016 | } else { |
| 2017 | descriptor = lttng_session_descriptor_live_create( |
| 2018 | name, timer_interval); |
| 2019 | } |
| 2020 | if (!descriptor) { |
| 2021 | ret = -LTTNG_ERR_INVALID; |
| 2022 | goto end; |
| 2023 | } |
| 2024 | ret_code = lttng_create_session_ext(descriptor); |
| 2025 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
| 2026 | end: |
| 2027 | lttng_session_descriptor_destroy(descriptor); |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * Stop the session and wait for the data before destroying it |
| 2033 | * |
| 2034 | * Return 0 on success else a negative LTTng error code. |
| 2035 | */ |
| 2036 | int lttng_destroy_session(const char *session_name) |
| 2037 | { |
| 2038 | int ret; |
| 2039 | enum lttng_error_code ret_code; |
| 2040 | enum lttng_destruction_handle_status status; |
| 2041 | struct lttng_destruction_handle *handle = NULL; |
| 2042 | |
| 2043 | /* |
| 2044 | * Stop the tracing and wait for the data to be |
| 2045 | * consumed. |
| 2046 | */ |
| 2047 | ret = _lttng_stop_tracing(session_name, 1); |
| 2048 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
| 2049 | goto end; |
| 2050 | } |
| 2051 | |
| 2052 | ret_code = lttng_destroy_session_ext(session_name, &handle); |
| 2053 | if (ret_code != LTTNG_OK) { |
| 2054 | ret = (int) -ret_code; |
| 2055 | goto end; |
| 2056 | } |
| 2057 | assert(handle); |
| 2058 | |
| 2059 | /* Block until the completion of the destruction of the session. */ |
| 2060 | status = lttng_destruction_handle_wait_for_completion(handle, -1); |
| 2061 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) { |
| 2062 | ret = -LTTNG_ERR_UNK; |
| 2063 | goto end; |
| 2064 | } |
| 2065 | |
| 2066 | status = lttng_destruction_handle_get_result(handle, &ret_code); |
| 2067 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { |
| 2068 | ret = -LTTNG_ERR_UNK; |
| 2069 | goto end; |
| 2070 | } |
| 2071 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
| 2072 | end: |
| 2073 | lttng_destruction_handle_destroy(handle); |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * Destroy the session without waiting for the data. |
| 2079 | */ |
| 2080 | int lttng_destroy_session_no_wait(const char *session_name) |
| 2081 | { |
| 2082 | enum lttng_error_code ret_code; |
| 2083 | |
| 2084 | ret_code = lttng_destroy_session_ext(session_name, NULL); |
| 2085 | return ret_code == LTTNG_OK ? 0 : -ret_code; |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * Ask the session daemon for all available sessions. |
| 2090 | * Sets the contents of the sessions array. |
| 2091 | * Returns the number of lttng_session entries in sessions; |
| 2092 | * on error, returns a negative value. |
| 2093 | */ |
| 2094 | int lttng_list_sessions(struct lttng_session **out_sessions) |
| 2095 | { |
| 2096 | int ret; |
| 2097 | struct lttcomm_session_msg lsm; |
| 2098 | const size_t session_size = sizeof(struct lttng_session) + |
| 2099 | sizeof(struct lttng_session_extended); |
| 2100 | size_t session_count, i; |
| 2101 | struct lttng_session_extended *sessions_extended_begin; |
| 2102 | struct lttng_session *sessions = NULL; |
| 2103 | |
| 2104 | memset(&lsm, 0, sizeof(lsm)); |
| 2105 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
| 2106 | /* |
| 2107 | * Initialize out_sessions to NULL so it is initialized when |
| 2108 | * lttng_list_sessions returns 0, thus allowing *out_sessions to |
| 2109 | * be subsequently freed. |
| 2110 | */ |
| 2111 | *out_sessions = NULL; |
| 2112 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); |
| 2113 | if (ret <= 0) { |
| 2114 | goto end; |
| 2115 | } |
| 2116 | if (!sessions) { |
| 2117 | ret = -LTTNG_ERR_FATAL; |
| 2118 | goto end; |
| 2119 | } |
| 2120 | |
| 2121 | if (ret % session_size) { |
| 2122 | ret = -LTTNG_ERR_UNK; |
| 2123 | free(sessions); |
| 2124 | goto end; |
| 2125 | } |
| 2126 | session_count = (size_t) ret / session_size; |
| 2127 | sessions_extended_begin = (struct lttng_session_extended *) |
| 2128 | (&sessions[session_count]); |
| 2129 | |
| 2130 | /* Set extended session info pointers. */ |
| 2131 | for (i = 0; i < session_count; i++) { |
| 2132 | struct lttng_session *session = &sessions[i]; |
| 2133 | struct lttng_session_extended *extended = |
| 2134 | &(sessions_extended_begin[i]); |
| 2135 | |
| 2136 | session->extended.ptr = extended; |
| 2137 | } |
| 2138 | |
| 2139 | ret = (int) session_count; |
| 2140 | *out_sessions = sessions; |
| 2141 | end: |
| 2142 | return ret; |
| 2143 | } |
| 2144 | |
| 2145 | enum lttng_error_code lttng_session_get_creation_time( |
| 2146 | const struct lttng_session *session, uint64_t *creation_time) |
| 2147 | { |
| 2148 | enum lttng_error_code ret = LTTNG_OK; |
| 2149 | struct lttng_session_extended *extended; |
| 2150 | |
| 2151 | if (!session || !creation_time || !session->extended.ptr) { |
| 2152 | ret = LTTNG_ERR_INVALID; |
| 2153 | goto end; |
| 2154 | } |
| 2155 | |
| 2156 | extended = session->extended.ptr; |
| 2157 | if (!extended->creation_time.is_set) { |
| 2158 | /* Not created on the session daemon yet. */ |
| 2159 | ret = LTTNG_ERR_SESSION_NOT_EXIST; |
| 2160 | goto end; |
| 2161 | } |
| 2162 | *creation_time = extended->creation_time.value; |
| 2163 | end: |
| 2164 | return ret; |
| 2165 | } |
| 2166 | |
| 2167 | int lttng_set_session_shm_path(const char *session_name, |
| 2168 | const char *shm_path) |
| 2169 | { |
| 2170 | int ret; |
| 2171 | struct lttcomm_session_msg lsm; |
| 2172 | |
| 2173 | if (session_name == NULL) { |
| 2174 | return -LTTNG_ERR_INVALID; |
| 2175 | } |
| 2176 | |
| 2177 | memset(&lsm, 0, sizeof(lsm)); |
| 2178 | lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; |
| 2179 | |
| 2180 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 2181 | sizeof(lsm.session.name)); |
| 2182 | if (ret) { |
| 2183 | ret = -LTTNG_ERR_INVALID; |
| 2184 | goto end; |
| 2185 | } |
| 2186 | |
| 2187 | ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "", |
| 2188 | sizeof(lsm.u.set_shm_path.shm_path)); |
| 2189 | if (ret) { |
| 2190 | ret = -LTTNG_ERR_INVALID; |
| 2191 | goto end; |
| 2192 | } |
| 2193 | |
| 2194 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 2195 | end: |
| 2196 | return ret; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Ask the session daemon for all available domains of a session. |
| 2201 | * Sets the contents of the domains array. |
| 2202 | * Returns the number of lttng_domain entries in domains; |
| 2203 | * on error, returns a negative value. |
| 2204 | */ |
| 2205 | int lttng_list_domains(const char *session_name, |
| 2206 | struct lttng_domain **domains) |
| 2207 | { |
| 2208 | int ret; |
| 2209 | struct lttcomm_session_msg lsm; |
| 2210 | |
| 2211 | if (session_name == NULL) { |
| 2212 | ret = -LTTNG_ERR_INVALID; |
| 2213 | goto error; |
| 2214 | } |
| 2215 | |
| 2216 | memset(&lsm, 0, sizeof(lsm)); |
| 2217 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
| 2218 | |
| 2219 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 2220 | sizeof(lsm.session.name)); |
| 2221 | if (ret) { |
| 2222 | ret = -LTTNG_ERR_INVALID; |
| 2223 | goto error; |
| 2224 | } |
| 2225 | |
| 2226 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
| 2227 | if (ret < 0) { |
| 2228 | goto error; |
| 2229 | } |
| 2230 | |
| 2231 | return ret / sizeof(struct lttng_domain); |
| 2232 | error: |
| 2233 | return ret; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Ask the session daemon for all available channels of a session. |
| 2238 | * Sets the contents of the channels array. |
| 2239 | * Returns the number of lttng_channel entries in channels; |
| 2240 | * on error, returns a negative value. |
| 2241 | */ |
| 2242 | int lttng_list_channels(struct lttng_handle *handle, |
| 2243 | struct lttng_channel **channels) |
| 2244 | { |
| 2245 | int ret, total_payload_received; |
| 2246 | struct lttcomm_session_msg lsm; |
| 2247 | char *reception_buffer = NULL; |
| 2248 | size_t cmd_header_len = 0; |
| 2249 | struct lttcomm_list_command_header *cmd_header = NULL; |
| 2250 | struct lttng_dynamic_buffer tmp_buffer; |
| 2251 | |
| 2252 | lttng_dynamic_buffer_init(&tmp_buffer); |
| 2253 | |
| 2254 | if (handle == NULL) { |
| 2255 | ret = -LTTNG_ERR_INVALID; |
| 2256 | goto end; |
| 2257 | } |
| 2258 | |
| 2259 | memset(&lsm, 0, sizeof(lsm)); |
| 2260 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
| 2261 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 2262 | sizeof(lsm.session.name)); |
| 2263 | if (ret) { |
| 2264 | ret = -LTTNG_ERR_INVALID; |
| 2265 | goto end; |
| 2266 | } |
| 2267 | |
| 2268 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 2269 | |
| 2270 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, |
| 2271 | (void **) &reception_buffer, (void **) &cmd_header, |
| 2272 | &cmd_header_len); |
| 2273 | if (ret < 0) { |
| 2274 | goto end; |
| 2275 | } |
| 2276 | |
| 2277 | total_payload_received = ret; |
| 2278 | |
| 2279 | if (cmd_header_len != sizeof(*cmd_header)) { |
| 2280 | ret = -LTTNG_ERR_FATAL; |
| 2281 | goto end; |
| 2282 | } |
| 2283 | |
| 2284 | if (!cmd_header) { |
| 2285 | ret = LTTNG_ERR_UNK; |
| 2286 | goto end; |
| 2287 | } |
| 2288 | |
| 2289 | if (cmd_header->count > INT_MAX) { |
| 2290 | ret = -LTTNG_ERR_OVERFLOW; |
| 2291 | goto end; |
| 2292 | } |
| 2293 | |
| 2294 | { |
| 2295 | enum lttng_error_code ret_code; |
| 2296 | const struct lttng_buffer_view events_view = |
| 2297 | lttng_buffer_view_init(reception_buffer, 0, |
| 2298 | total_payload_received); |
| 2299 | |
| 2300 | ret_code = lttng_channels_create_and_flatten_from_buffer( |
| 2301 | &events_view, cmd_header->count, channels); |
| 2302 | if (ret_code != LTTNG_OK) { |
| 2303 | ret = -ret_code; |
| 2304 | goto end; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | ret = (int) cmd_header->count; |
| 2309 | end: |
| 2310 | free(cmd_header); |
| 2311 | free(reception_buffer); |
| 2312 | return ret; |
| 2313 | } |
| 2314 | |
| 2315 | /* |
| 2316 | * Ask the session daemon for all available events of a session channel. |
| 2317 | * Sets the contents of the events array. |
| 2318 | * Returns the number of lttng_event entries in events; |
| 2319 | * on error, returns a negative value. |
| 2320 | */ |
| 2321 | int lttng_list_events(struct lttng_handle *handle, |
| 2322 | const char *channel_name, struct lttng_event **events) |
| 2323 | { |
| 2324 | int ret; |
| 2325 | struct lttcomm_session_msg lsm; |
| 2326 | struct lttcomm_event_command_header *cmd_header = NULL; |
| 2327 | size_t cmd_header_len; |
| 2328 | uint32_t nb_events, i; |
| 2329 | void *comm_ext_at; |
| 2330 | char *reception_buffer = NULL; |
| 2331 | struct lttng_dynamic_buffer listing; |
| 2332 | size_t storage_req; |
| 2333 | |
| 2334 | /* Safety check. An handle and channel name are mandatory */ |
| 2335 | if (handle == NULL || channel_name == NULL) { |
| 2336 | return -LTTNG_ERR_INVALID; |
| 2337 | } |
| 2338 | |
| 2339 | memset(&lsm, 0, sizeof(lsm)); |
| 2340 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
| 2341 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 2342 | sizeof(lsm.session.name)); |
| 2343 | if (ret) { |
| 2344 | ret = -LTTNG_ERR_INVALID; |
| 2345 | goto end; |
| 2346 | } |
| 2347 | |
| 2348 | ret = lttng_strncpy(lsm.u.list.channel_name, channel_name, |
| 2349 | sizeof(lsm.u.list.channel_name)); |
| 2350 | if (ret) { |
| 2351 | ret = -LTTNG_ERR_INVALID; |
| 2352 | goto end; |
| 2353 | } |
| 2354 | |
| 2355 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 2356 | |
| 2357 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, |
| 2358 | (void **) &reception_buffer, (void **) &cmd_header, |
| 2359 | &cmd_header_len); |
| 2360 | if (ret < 0) { |
| 2361 | goto end; |
| 2362 | } |
| 2363 | |
| 2364 | if (!cmd_header) { |
| 2365 | ret = -LTTNG_ERR_UNK; |
| 2366 | goto end; |
| 2367 | } |
| 2368 | |
| 2369 | /* Set number of events and free command header */ |
| 2370 | nb_events = cmd_header->nb_events; |
| 2371 | if (nb_events > INT_MAX) { |
| 2372 | ret = -LTTNG_ERR_OVERFLOW; |
| 2373 | goto end; |
| 2374 | } |
| 2375 | free(cmd_header); |
| 2376 | cmd_header = NULL; |
| 2377 | |
| 2378 | /* |
| 2379 | * The buffer that is returned must contain a "flat" version of |
| 2380 | * the events that are returned. In other words, all pointers |
| 2381 | * within an lttng_event must point to a location within the returned |
| 2382 | * buffer so that the user may free everything by simply calling free() |
| 2383 | * on the returned buffer. This is needed in order to maintain API |
| 2384 | * compatibility. |
| 2385 | * |
| 2386 | * A first pass is performed to compute the size of the buffer that |
| 2387 | * must be allocated. A second pass is then performed to setup |
| 2388 | * the returned events so that their members always point within the |
| 2389 | * buffer. |
| 2390 | * |
| 2391 | * The layout of the returned buffer is as follows: |
| 2392 | * - struct lttng_event[nb_events], |
| 2393 | * - nb_events times the following: |
| 2394 | * - struct lttng_event_extended, |
| 2395 | * - flattened version of userspace_probe_location |
| 2396 | * - filter_expression |
| 2397 | * - exclusions |
| 2398 | * - padding to align to 64-bits |
| 2399 | */ |
| 2400 | comm_ext_at = reception_buffer + |
| 2401 | (nb_events * sizeof(struct lttng_event)); |
| 2402 | storage_req = nb_events * sizeof(struct lttng_event); |
| 2403 | |
| 2404 | for (i = 0; i < nb_events; i++) { |
| 2405 | struct lttcomm_event_extended_header *ext_comm = |
| 2406 | (struct lttcomm_event_extended_header *) comm_ext_at; |
| 2407 | int probe_storage_req = 0; |
| 2408 | |
| 2409 | comm_ext_at += sizeof(*ext_comm); |
| 2410 | comm_ext_at += ext_comm->filter_len; |
| 2411 | comm_ext_at += |
| 2412 | ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; |
| 2413 | |
| 2414 | if (ext_comm->userspace_probe_location_len) { |
| 2415 | struct lttng_userspace_probe_location *probe_location = NULL; |
| 2416 | struct lttng_buffer_view probe_location_view; |
| 2417 | |
| 2418 | probe_location_view = lttng_buffer_view_init( |
| 2419 | comm_ext_at, 0, |
| 2420 | ext_comm->userspace_probe_location_len); |
| 2421 | |
| 2422 | /* |
| 2423 | * Create a temporary userspace probe location to |
| 2424 | * determine the size needed by a "flattened" version |
| 2425 | * of that same probe location. |
| 2426 | */ |
| 2427 | ret = lttng_userspace_probe_location_create_from_buffer( |
| 2428 | &probe_location_view, &probe_location); |
| 2429 | if (ret < 0) { |
| 2430 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 2431 | goto end; |
| 2432 | } |
| 2433 | |
| 2434 | ret = lttng_userspace_probe_location_flatten( |
| 2435 | probe_location, NULL); |
| 2436 | lttng_userspace_probe_location_destroy(probe_location); |
| 2437 | if (ret < 0) { |
| 2438 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 2439 | goto end; |
| 2440 | } |
| 2441 | |
| 2442 | probe_storage_req = ret; |
| 2443 | comm_ext_at += ext_comm->userspace_probe_location_len; |
| 2444 | } |
| 2445 | |
| 2446 | storage_req += sizeof(struct lttng_event_extended); |
| 2447 | storage_req += ext_comm->filter_len; |
| 2448 | storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; |
| 2449 | /* Padding to ensure the flat probe is aligned. */ |
| 2450 | storage_req = ALIGN_TO(storage_req, sizeof(uint64_t)); |
| 2451 | storage_req += probe_storage_req; |
| 2452 | } |
| 2453 | |
| 2454 | lttng_dynamic_buffer_init(&listing); |
| 2455 | /* |
| 2456 | * We must ensure that "listing" is never resized so as to preserve |
| 2457 | * the validity of the flattened objects. |
| 2458 | */ |
| 2459 | ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req); |
| 2460 | if (ret) { |
| 2461 | ret = -LTTNG_ERR_NOMEM; |
| 2462 | goto end; |
| 2463 | } |
| 2464 | |
| 2465 | ret = lttng_dynamic_buffer_append(&listing, reception_buffer, |
| 2466 | nb_events * sizeof(struct lttng_event)); |
| 2467 | if (ret) { |
| 2468 | ret = -LTTNG_ERR_NOMEM; |
| 2469 | goto free_dynamic_buffer; |
| 2470 | } |
| 2471 | |
| 2472 | comm_ext_at = reception_buffer + |
| 2473 | (nb_events * sizeof(struct lttng_event)); |
| 2474 | for (i = 0; i < nb_events; i++) { |
| 2475 | struct lttng_event *event = (struct lttng_event *) |
| 2476 | (listing.data + (sizeof(struct lttng_event) * i)); |
| 2477 | struct lttcomm_event_extended_header *ext_comm = |
| 2478 | (struct lttcomm_event_extended_header *) comm_ext_at; |
| 2479 | struct lttng_event_extended *event_extended = |
| 2480 | (struct lttng_event_extended *) |
| 2481 | (listing.data + listing.size); |
| 2482 | |
| 2483 | /* Insert struct lttng_event_extended. */ |
| 2484 | ret = lttng_dynamic_buffer_set_size(&listing, |
| 2485 | listing.size + sizeof(*event_extended)); |
| 2486 | if (ret) { |
| 2487 | ret = -LTTNG_ERR_NOMEM; |
| 2488 | goto free_dynamic_buffer; |
| 2489 | } |
| 2490 | event->extended.ptr = event_extended; |
| 2491 | |
| 2492 | comm_ext_at += sizeof(*ext_comm); |
| 2493 | |
| 2494 | /* Insert filter expression. */ |
| 2495 | if (ext_comm->filter_len) { |
| 2496 | event_extended->filter_expression = listing.data + |
| 2497 | listing.size; |
| 2498 | ret = lttng_dynamic_buffer_append(&listing, comm_ext_at, |
| 2499 | ext_comm->filter_len); |
| 2500 | if (ret) { |
| 2501 | ret = -LTTNG_ERR_NOMEM; |
| 2502 | goto free_dynamic_buffer; |
| 2503 | } |
| 2504 | comm_ext_at += ext_comm->filter_len; |
| 2505 | } |
| 2506 | |
| 2507 | /* Insert exclusions. */ |
| 2508 | if (ext_comm->nb_exclusions) { |
| 2509 | event_extended->exclusions.count = |
| 2510 | ext_comm->nb_exclusions; |
| 2511 | event_extended->exclusions.strings = |
| 2512 | listing.data + listing.size; |
| 2513 | |
| 2514 | ret = lttng_dynamic_buffer_append(&listing, |
| 2515 | comm_ext_at, |
| 2516 | ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN); |
| 2517 | if (ret) { |
| 2518 | ret = -LTTNG_ERR_NOMEM; |
| 2519 | goto free_dynamic_buffer; |
| 2520 | } |
| 2521 | comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; |
| 2522 | } |
| 2523 | |
| 2524 | /* Insert padding to align to 64-bits. */ |
| 2525 | ret = lttng_dynamic_buffer_set_size(&listing, |
| 2526 | ALIGN_TO(listing.size, sizeof(uint64_t))); |
| 2527 | if (ret) { |
| 2528 | ret = -LTTNG_ERR_NOMEM; |
| 2529 | goto free_dynamic_buffer; |
| 2530 | } |
| 2531 | |
| 2532 | /* Insert flattened userspace probe location. */ |
| 2533 | if (ext_comm->userspace_probe_location_len) { |
| 2534 | struct lttng_userspace_probe_location *probe_location = NULL; |
| 2535 | struct lttng_buffer_view probe_location_view; |
| 2536 | |
| 2537 | probe_location_view = lttng_buffer_view_init( |
| 2538 | comm_ext_at, 0, |
| 2539 | ext_comm->userspace_probe_location_len); |
| 2540 | |
| 2541 | ret = lttng_userspace_probe_location_create_from_buffer( |
| 2542 | &probe_location_view, &probe_location); |
| 2543 | if (ret < 0) { |
| 2544 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 2545 | goto free_dynamic_buffer; |
| 2546 | } |
| 2547 | |
| 2548 | event_extended->probe_location = (struct lttng_userspace_probe_location *) |
| 2549 | (listing.data + listing.size); |
| 2550 | ret = lttng_userspace_probe_location_flatten( |
| 2551 | probe_location, &listing); |
| 2552 | lttng_userspace_probe_location_destroy(probe_location); |
| 2553 | if (ret < 0) { |
| 2554 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 2555 | goto free_dynamic_buffer; |
| 2556 | } |
| 2557 | |
| 2558 | comm_ext_at += ext_comm->userspace_probe_location_len; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | /* Don't reset listing buffer as we return its content. */ |
| 2563 | *events = (struct lttng_event *) listing.data; |
| 2564 | lttng_dynamic_buffer_init(&listing); |
| 2565 | ret = (int) nb_events; |
| 2566 | free_dynamic_buffer: |
| 2567 | lttng_dynamic_buffer_reset(&listing); |
| 2568 | end: |
| 2569 | free(cmd_header); |
| 2570 | free(reception_buffer); |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | * Sets the tracing_group variable with name. |
| 2576 | * This function allocates memory pointed to by tracing_group. |
| 2577 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. |
| 2578 | */ |
| 2579 | int lttng_set_tracing_group(const char *name) |
| 2580 | { |
| 2581 | char *new_group; |
| 2582 | if (name == NULL) { |
| 2583 | return -LTTNG_ERR_INVALID; |
| 2584 | } |
| 2585 | |
| 2586 | if (asprintf(&new_group, "%s", name) < 0) { |
| 2587 | return -LTTNG_ERR_FATAL; |
| 2588 | } |
| 2589 | |
| 2590 | free(tracing_group); |
| 2591 | tracing_group = new_group; |
| 2592 | new_group = NULL; |
| 2593 | |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
| 2597 | int lttng_calibrate(struct lttng_handle *handle, |
| 2598 | struct lttng_calibrate *calibrate) |
| 2599 | { |
| 2600 | /* |
| 2601 | * This command was removed in LTTng 2.9. |
| 2602 | */ |
| 2603 | return -LTTNG_ERR_UND; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
| 2607 | * Set default channel attributes. |
| 2608 | * If either or both of the arguments are null, attr content is zeroe'd. |
| 2609 | */ |
| 2610 | void lttng_channel_set_default_attr(struct lttng_domain *domain, |
| 2611 | struct lttng_channel_attr *attr) |
| 2612 | { |
| 2613 | struct lttng_channel_extended *extended; |
| 2614 | |
| 2615 | /* Safety check */ |
| 2616 | if (attr == NULL || domain == NULL) { |
| 2617 | return; |
| 2618 | } |
| 2619 | |
| 2620 | /* Save the pointer for later use */ |
| 2621 | extended = (struct lttng_channel_extended *) attr->extended.ptr; |
| 2622 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
| 2623 | |
| 2624 | /* Same for all domains. */ |
| 2625 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 2626 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; |
| 2627 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; |
| 2628 | |
| 2629 | switch (domain->type) { |
| 2630 | case LTTNG_DOMAIN_KERNEL: |
| 2631 | attr->switch_timer_interval = |
| 2632 | DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
| 2633 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
| 2634 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
| 2635 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 2636 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 2637 | break; |
| 2638 | case LTTNG_DOMAIN_UST: |
| 2639 | switch (domain->buf_type) { |
| 2640 | case LTTNG_BUFFER_PER_UID: |
| 2641 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); |
| 2642 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; |
| 2643 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; |
| 2644 | attr->switch_timer_interval = |
| 2645 | DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; |
| 2646 | attr->read_timer_interval = |
| 2647 | DEFAULT_UST_UID_CHANNEL_READ_TIMER; |
| 2648 | break; |
| 2649 | case LTTNG_BUFFER_PER_PID: |
| 2650 | default: |
| 2651 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); |
| 2652 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; |
| 2653 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; |
| 2654 | attr->switch_timer_interval = |
| 2655 | DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; |
| 2656 | attr->read_timer_interval = |
| 2657 | DEFAULT_UST_PID_CHANNEL_READ_TIMER; |
| 2658 | break; |
| 2659 | } |
| 2660 | default: |
| 2661 | /* Default behavior: leave set to 0. */ |
| 2662 | break; |
| 2663 | } |
| 2664 | |
| 2665 | if (extended) { |
| 2666 | lttng_channel_set_default_extended_attr(domain, extended); |
| 2667 | } |
| 2668 | |
| 2669 | /* Reassign the extended pointer. */ |
| 2670 | attr->extended.ptr = extended; |
| 2671 | } |
| 2672 | |
| 2673 | int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, |
| 2674 | uint64_t *discarded_events) |
| 2675 | { |
| 2676 | int ret = 0; |
| 2677 | struct lttng_channel_extended *chan_ext; |
| 2678 | |
| 2679 | if (!channel || !discarded_events) { |
| 2680 | ret = -LTTNG_ERR_INVALID; |
| 2681 | goto end; |
| 2682 | } |
| 2683 | |
| 2684 | chan_ext = channel->attr.extended.ptr; |
| 2685 | if (!chan_ext) { |
| 2686 | /* |
| 2687 | * This can happen since the lttng_channel structure is |
| 2688 | * used for other tasks where this pointer is never set. |
| 2689 | */ |
| 2690 | *discarded_events = 0; |
| 2691 | goto end; |
| 2692 | } |
| 2693 | |
| 2694 | *discarded_events = chan_ext->discarded_events; |
| 2695 | end: |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, |
| 2700 | uint64_t *lost_packets) |
| 2701 | { |
| 2702 | int ret = 0; |
| 2703 | struct lttng_channel_extended *chan_ext; |
| 2704 | |
| 2705 | if (!channel || !lost_packets) { |
| 2706 | ret = -LTTNG_ERR_INVALID; |
| 2707 | goto end; |
| 2708 | } |
| 2709 | |
| 2710 | chan_ext = channel->attr.extended.ptr; |
| 2711 | if (!chan_ext) { |
| 2712 | /* |
| 2713 | * This can happen since the lttng_channel structure is |
| 2714 | * used for other tasks where this pointer is never set. |
| 2715 | */ |
| 2716 | *lost_packets = 0; |
| 2717 | goto end; |
| 2718 | } |
| 2719 | |
| 2720 | *lost_packets = chan_ext->lost_packets; |
| 2721 | end: |
| 2722 | return ret; |
| 2723 | } |
| 2724 | |
| 2725 | int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, |
| 2726 | uint64_t *monitor_timer_interval) |
| 2727 | { |
| 2728 | int ret = 0; |
| 2729 | |
| 2730 | if (!chan || !monitor_timer_interval) { |
| 2731 | ret = -LTTNG_ERR_INVALID; |
| 2732 | goto end; |
| 2733 | } |
| 2734 | |
| 2735 | if (!chan->attr.extended.ptr) { |
| 2736 | ret = -LTTNG_ERR_INVALID; |
| 2737 | goto end; |
| 2738 | } |
| 2739 | |
| 2740 | *monitor_timer_interval = ((struct lttng_channel_extended *) |
| 2741 | chan->attr.extended.ptr)->monitor_timer_interval; |
| 2742 | end: |
| 2743 | return ret; |
| 2744 | } |
| 2745 | |
| 2746 | int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, |
| 2747 | uint64_t monitor_timer_interval) |
| 2748 | { |
| 2749 | int ret = 0; |
| 2750 | |
| 2751 | if (!chan || !chan->attr.extended.ptr) { |
| 2752 | ret = -LTTNG_ERR_INVALID; |
| 2753 | goto end; |
| 2754 | } |
| 2755 | |
| 2756 | ((struct lttng_channel_extended *) |
| 2757 | chan->attr.extended.ptr)->monitor_timer_interval = |
| 2758 | monitor_timer_interval; |
| 2759 | end: |
| 2760 | return ret; |
| 2761 | } |
| 2762 | |
| 2763 | int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, |
| 2764 | int64_t *blocking_timeout) |
| 2765 | { |
| 2766 | int ret = 0; |
| 2767 | |
| 2768 | if (!chan || !blocking_timeout) { |
| 2769 | ret = -LTTNG_ERR_INVALID; |
| 2770 | goto end; |
| 2771 | } |
| 2772 | |
| 2773 | if (!chan->attr.extended.ptr) { |
| 2774 | ret = -LTTNG_ERR_INVALID; |
| 2775 | goto end; |
| 2776 | } |
| 2777 | |
| 2778 | *blocking_timeout = ((struct lttng_channel_extended *) |
| 2779 | chan->attr.extended.ptr)->blocking_timeout; |
| 2780 | end: |
| 2781 | return ret; |
| 2782 | } |
| 2783 | |
| 2784 | int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, |
| 2785 | int64_t blocking_timeout) |
| 2786 | { |
| 2787 | int ret = 0; |
| 2788 | int64_t msec_timeout; |
| 2789 | |
| 2790 | if (!chan || !chan->attr.extended.ptr) { |
| 2791 | ret = -LTTNG_ERR_INVALID; |
| 2792 | goto end; |
| 2793 | } |
| 2794 | |
| 2795 | if (blocking_timeout < 0 && blocking_timeout != -1) { |
| 2796 | ret = -LTTNG_ERR_INVALID; |
| 2797 | goto end; |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * LTTng-ust's use of poll() to implement this timeout mechanism forces |
| 2802 | * us to accept a narrower range of values (msecs expressed as a signed |
| 2803 | * 32-bit integer). |
| 2804 | */ |
| 2805 | msec_timeout = blocking_timeout / 1000; |
| 2806 | if (msec_timeout != (int32_t) msec_timeout) { |
| 2807 | ret = -LTTNG_ERR_INVALID; |
| 2808 | goto end; |
| 2809 | } |
| 2810 | |
| 2811 | ((struct lttng_channel_extended *) |
| 2812 | chan->attr.extended.ptr)->blocking_timeout = |
| 2813 | blocking_timeout; |
| 2814 | end: |
| 2815 | return ret; |
| 2816 | } |
| 2817 | |
| 2818 | /* |
| 2819 | * Check if session daemon is alive. |
| 2820 | * |
| 2821 | * Return 1 if alive or 0 if not. |
| 2822 | * On error returns a negative value. |
| 2823 | */ |
| 2824 | int lttng_session_daemon_alive(void) |
| 2825 | { |
| 2826 | int ret; |
| 2827 | |
| 2828 | ret = set_session_daemon_path(); |
| 2829 | if (ret < 0) { |
| 2830 | /* Error. */ |
| 2831 | return ret; |
| 2832 | } |
| 2833 | |
| 2834 | if (*sessiond_sock_path == '\0') { |
| 2835 | /* |
| 2836 | * No socket path set. Weird error which means the constructor |
| 2837 | * was not called. |
| 2838 | */ |
| 2839 | assert(0); |
| 2840 | } |
| 2841 | |
| 2842 | ret = try_connect_sessiond(sessiond_sock_path); |
| 2843 | if (ret < 0) { |
| 2844 | /* Not alive. */ |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | /* Is alive. */ |
| 2849 | return 1; |
| 2850 | } |
| 2851 | |
| 2852 | /* |
| 2853 | * Set URL for a consumer for a session and domain. |
| 2854 | * |
| 2855 | * Return 0 on success, else a negative value. |
| 2856 | */ |
| 2857 | int lttng_set_consumer_url(struct lttng_handle *handle, |
| 2858 | const char *control_url, const char *data_url) |
| 2859 | { |
| 2860 | int ret; |
| 2861 | ssize_t size; |
| 2862 | struct lttcomm_session_msg lsm; |
| 2863 | struct lttng_uri *uris = NULL; |
| 2864 | |
| 2865 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
| 2866 | ret = -LTTNG_ERR_INVALID; |
| 2867 | goto error; |
| 2868 | } |
| 2869 | |
| 2870 | memset(&lsm, 0, sizeof(lsm)); |
| 2871 | |
| 2872 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
| 2873 | |
| 2874 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
| 2875 | sizeof(lsm.session.name)); |
| 2876 | if (ret) { |
| 2877 | ret = -LTTNG_ERR_INVALID; |
| 2878 | goto error; |
| 2879 | } |
| 2880 | |
| 2881 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
| 2882 | |
| 2883 | size = uri_parse_str_urls(control_url, data_url, &uris); |
| 2884 | if (size < 0) { |
| 2885 | ret = -LTTNG_ERR_INVALID; |
| 2886 | goto error; |
| 2887 | } |
| 2888 | |
| 2889 | lsm.u.uri.size = size; |
| 2890 | |
| 2891 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
| 2892 | sizeof(struct lttng_uri) * size, NULL); |
| 2893 | |
| 2894 | free(uris); |
| 2895 | error: |
| 2896 | return ret; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * [OBSOLETE] |
| 2901 | */ |
| 2902 | int lttng_enable_consumer(struct lttng_handle *handle); |
| 2903 | int lttng_enable_consumer(struct lttng_handle *handle) |
| 2904 | { |
| 2905 | return -ENOSYS; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * [OBSOLETE] |
| 2910 | */ |
| 2911 | int lttng_disable_consumer(struct lttng_handle *handle); |
| 2912 | int lttng_disable_consumer(struct lttng_handle *handle) |
| 2913 | { |
| 2914 | return -ENOSYS; |
| 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * [OBSOLETE] |
| 2919 | */ |
| 2920 | int _lttng_create_session_ext(const char *name, const char *url, |
| 2921 | const char *datetime); |
| 2922 | int _lttng_create_session_ext(const char *name, const char *url, |
| 2923 | const char *datetime) |
| 2924 | { |
| 2925 | return -ENOSYS; |
| 2926 | } |
| 2927 | |
| 2928 | /* |
| 2929 | * For a given session name, this call checks if the data is ready to be read |
| 2930 | * or is still being extracted by the consumer(s) hence not ready to be used by |
| 2931 | * any readers. |
| 2932 | */ |
| 2933 | int lttng_data_pending(const char *session_name) |
| 2934 | { |
| 2935 | int ret; |
| 2936 | struct lttcomm_session_msg lsm; |
| 2937 | uint8_t *pending = NULL; |
| 2938 | |
| 2939 | if (session_name == NULL) { |
| 2940 | return -LTTNG_ERR_INVALID; |
| 2941 | } |
| 2942 | |
| 2943 | memset(&lsm, 0, sizeof(lsm)); |
| 2944 | lsm.cmd_type = LTTNG_DATA_PENDING; |
| 2945 | |
| 2946 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 2947 | sizeof(lsm.session.name)); |
| 2948 | if (ret) { |
| 2949 | ret = -LTTNG_ERR_INVALID; |
| 2950 | goto end; |
| 2951 | } |
| 2952 | |
| 2953 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending); |
| 2954 | if (ret < 0) { |
| 2955 | goto end; |
| 2956 | } else if (ret != 1) { |
| 2957 | /* Unexpected payload size */ |
| 2958 | ret = -LTTNG_ERR_INVALID; |
| 2959 | goto end; |
| 2960 | } else if (!pending) { |
| 2961 | /* Internal error. */ |
| 2962 | ret = -LTTNG_ERR_UNK; |
| 2963 | goto end; |
| 2964 | } |
| 2965 | |
| 2966 | ret = (int) *pending; |
| 2967 | end: |
| 2968 | free(pending); |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
| 2972 | /* |
| 2973 | * Regenerate the metadata for a session. |
| 2974 | * Return 0 on success, a negative error code on error. |
| 2975 | */ |
| 2976 | int lttng_regenerate_metadata(const char *session_name) |
| 2977 | { |
| 2978 | int ret; |
| 2979 | struct lttcomm_session_msg lsm; |
| 2980 | |
| 2981 | if (!session_name) { |
| 2982 | ret = -LTTNG_ERR_INVALID; |
| 2983 | goto end; |
| 2984 | } |
| 2985 | |
| 2986 | memset(&lsm, 0, sizeof(lsm)); |
| 2987 | lsm.cmd_type = LTTNG_REGENERATE_METADATA; |
| 2988 | |
| 2989 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 2990 | sizeof(lsm.session.name)); |
| 2991 | if (ret) { |
| 2992 | ret = -LTTNG_ERR_INVALID; |
| 2993 | goto end; |
| 2994 | } |
| 2995 | |
| 2996 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 2997 | if (ret < 0) { |
| 2998 | goto end; |
| 2999 | } |
| 3000 | |
| 3001 | ret = 0; |
| 3002 | end: |
| 3003 | return ret; |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * Deprecated, replaced by lttng_regenerate_metadata. |
| 3008 | */ |
| 3009 | int lttng_metadata_regenerate(const char *session_name) |
| 3010 | { |
| 3011 | return lttng_regenerate_metadata(session_name); |
| 3012 | } |
| 3013 | |
| 3014 | /* |
| 3015 | * Regenerate the statedump of a session. |
| 3016 | * Return 0 on success, a negative error code on error. |
| 3017 | */ |
| 3018 | int lttng_regenerate_statedump(const char *session_name) |
| 3019 | { |
| 3020 | int ret; |
| 3021 | struct lttcomm_session_msg lsm; |
| 3022 | |
| 3023 | if (!session_name) { |
| 3024 | ret = -LTTNG_ERR_INVALID; |
| 3025 | goto end; |
| 3026 | } |
| 3027 | |
| 3028 | memset(&lsm, 0, sizeof(lsm)); |
| 3029 | lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP; |
| 3030 | |
| 3031 | ret = lttng_strncpy(lsm.session.name, session_name, |
| 3032 | sizeof(lsm.session.name)); |
| 3033 | if (ret) { |
| 3034 | ret = -LTTNG_ERR_INVALID; |
| 3035 | goto end; |
| 3036 | } |
| 3037 | |
| 3038 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 3039 | if (ret < 0) { |
| 3040 | goto end; |
| 3041 | } |
| 3042 | |
| 3043 | ret = 0; |
| 3044 | end: |
| 3045 | return ret; |
| 3046 | } |
| 3047 | |
| 3048 | int lttng_register_trigger(struct lttng_trigger *trigger) |
| 3049 | { |
| 3050 | int ret; |
| 3051 | struct lttcomm_session_msg lsm; |
| 3052 | struct lttng_dynamic_buffer buffer; |
| 3053 | |
| 3054 | lttng_dynamic_buffer_init(&buffer); |
| 3055 | if (!trigger) { |
| 3056 | ret = -LTTNG_ERR_INVALID; |
| 3057 | goto end; |
| 3058 | } |
| 3059 | |
| 3060 | if (!lttng_trigger_validate(trigger)) { |
| 3061 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
| 3062 | goto end; |
| 3063 | } |
| 3064 | |
| 3065 | ret = lttng_trigger_serialize(trigger, &buffer); |
| 3066 | if (ret < 0) { |
| 3067 | ret = -LTTNG_ERR_UNK; |
| 3068 | goto end; |
| 3069 | } |
| 3070 | |
| 3071 | memset(&lsm, 0, sizeof(lsm)); |
| 3072 | lsm.cmd_type = LTTNG_REGISTER_TRIGGER; |
| 3073 | lsm.u.trigger.length = (uint32_t) buffer.size; |
| 3074 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, |
| 3075 | buffer.size, NULL); |
| 3076 | end: |
| 3077 | lttng_dynamic_buffer_reset(&buffer); |
| 3078 | return ret; |
| 3079 | } |
| 3080 | |
| 3081 | int lttng_unregister_trigger(struct lttng_trigger *trigger) |
| 3082 | { |
| 3083 | int ret; |
| 3084 | struct lttcomm_session_msg lsm; |
| 3085 | struct lttng_dynamic_buffer buffer; |
| 3086 | |
| 3087 | lttng_dynamic_buffer_init(&buffer); |
| 3088 | if (!trigger) { |
| 3089 | ret = -LTTNG_ERR_INVALID; |
| 3090 | goto end; |
| 3091 | } |
| 3092 | |
| 3093 | if (!lttng_trigger_validate(trigger)) { |
| 3094 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
| 3095 | goto end; |
| 3096 | } |
| 3097 | |
| 3098 | ret = lttng_trigger_serialize(trigger, &buffer); |
| 3099 | if (ret < 0) { |
| 3100 | ret = -LTTNG_ERR_UNK; |
| 3101 | goto end; |
| 3102 | } |
| 3103 | |
| 3104 | memset(&lsm, 0, sizeof(lsm)); |
| 3105 | lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; |
| 3106 | lsm.u.trigger.length = (uint32_t) buffer.size; |
| 3107 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, |
| 3108 | buffer.size, NULL); |
| 3109 | end: |
| 3110 | lttng_dynamic_buffer_reset(&buffer); |
| 3111 | return ret; |
| 3112 | } |
| 3113 | |
| 3114 | /* |
| 3115 | * lib constructor. |
| 3116 | */ |
| 3117 | static void __attribute__((constructor)) init(void) |
| 3118 | { |
| 3119 | /* Set default session group */ |
| 3120 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
| 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | * lib destructor. |
| 3125 | */ |
| 3126 | static void __attribute__((destructor)) lttng_ctl_exit(void) |
| 3127 | { |
| 3128 | free(tracing_group); |
| 3129 | } |