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