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