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