| 1 | /* |
| 2 | * liblttngctl.c |
| 3 | * |
| 4 | * Linux Trace Toolkit Control Library |
| 5 | * |
| 6 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #include <assert.h> |
| 24 | #include <grp.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <common/common.h> |
| 32 | #include <common/defaults.h> |
| 33 | #include <common/sessiond-comm/sessiond-comm.h> |
| 34 | #include <common/uri.h> |
| 35 | #include <common/utils.h> |
| 36 | #include <lttng/lttng.h> |
| 37 | |
| 38 | #include "filter/filter-ast.h" |
| 39 | #include "filter/filter-parser.h" |
| 40 | #include "filter/filter-bytecode.h" |
| 41 | #include "filter/memstream.h" |
| 42 | #include "lttng-ctl-helper.h" |
| 43 | |
| 44 | #ifdef DEBUG |
| 45 | static const int print_xml = 1; |
| 46 | #define dbg_printf(fmt, args...) \ |
| 47 | printf("[debug liblttng-ctl] " fmt, ## args) |
| 48 | #else |
| 49 | static const int print_xml = 0; |
| 50 | #define dbg_printf(fmt, args...) \ |
| 51 | do { \ |
| 52 | /* do nothing but check printf format */ \ |
| 53 | if (0) \ |
| 54 | printf("[debug liblttnctl] " fmt, ## args); \ |
| 55 | } while (0) |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | /* Socket to session daemon for communication */ |
| 60 | static int sessiond_socket; |
| 61 | static char sessiond_sock_path[PATH_MAX]; |
| 62 | static char health_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 | |
| 78 | /* |
| 79 | * Copy string from src to dst and enforce null terminated byte. |
| 80 | */ |
| 81 | LTTNG_HIDDEN |
| 82 | void lttng_ctl_copy_string(char *dst, const char *src, size_t len) |
| 83 | { |
| 84 | if (src && dst) { |
| 85 | strncpy(dst, src, len); |
| 86 | /* Enforce the NULL terminated byte */ |
| 87 | dst[len - 1] = '\0'; |
| 88 | } else if (dst) { |
| 89 | dst[0] = '\0'; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Copy domain to lttcomm_session_msg domain. |
| 95 | * |
| 96 | * If domain is unknown, default domain will be the kernel. |
| 97 | */ |
| 98 | LTTNG_HIDDEN |
| 99 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, |
| 100 | struct lttng_domain *src) |
| 101 | { |
| 102 | if (src && dst) { |
| 103 | switch (src->type) { |
| 104 | case LTTNG_DOMAIN_KERNEL: |
| 105 | case LTTNG_DOMAIN_UST: |
| 106 | memcpy(dst, src, sizeof(struct lttng_domain)); |
| 107 | break; |
| 108 | default: |
| 109 | memset(dst, 0, sizeof(struct lttng_domain)); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Send lttcomm_session_msg to the session daemon. |
| 117 | * |
| 118 | * On success, returns the number of bytes sent (>=0) |
| 119 | * On error, returns -1 |
| 120 | */ |
| 121 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
| 122 | { |
| 123 | int ret; |
| 124 | |
| 125 | if (!connected) { |
| 126 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 127 | goto end; |
| 128 | } |
| 129 | |
| 130 | DBG("LSM cmd type : %d", lsm->cmd_type); |
| 131 | |
| 132 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
| 133 | sizeof(struct lttcomm_session_msg)); |
| 134 | if (ret < 0) { |
| 135 | ret = -LTTNG_ERR_FATAL; |
| 136 | } |
| 137 | |
| 138 | end: |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Send var len data to the session daemon. |
| 144 | * |
| 145 | * On success, returns the number of bytes sent (>=0) |
| 146 | * On error, returns -1 |
| 147 | */ |
| 148 | static int send_session_varlen(void *data, size_t len) |
| 149 | { |
| 150 | int ret; |
| 151 | |
| 152 | if (!connected) { |
| 153 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | if (!data || !len) { |
| 158 | ret = 0; |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 162 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); |
| 163 | if (ret < 0) { |
| 164 | ret = -LTTNG_ERR_FATAL; |
| 165 | } |
| 166 | |
| 167 | end: |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Receive data from the sessiond socket. |
| 173 | * |
| 174 | * On success, returns the number of bytes received (>=0) |
| 175 | * On error, returns -1 (recvmsg() error) or -ENOTCONN |
| 176 | */ |
| 177 | static int recv_data_sessiond(void *buf, size_t len) |
| 178 | { |
| 179 | int ret; |
| 180 | |
| 181 | if (!connected) { |
| 182 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
| 187 | if (ret < 0) { |
| 188 | ret = -LTTNG_ERR_FATAL; |
| 189 | } |
| 190 | |
| 191 | end: |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Check if we are in the specified group. |
| 197 | * |
| 198 | * If yes return 1, else return -1. |
| 199 | */ |
| 200 | static int check_tracing_group(const char *grp_name) |
| 201 | { |
| 202 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ |
| 203 | gid_t *grp_list; |
| 204 | int grp_list_size, grp_id, i; |
| 205 | int ret = -1; |
| 206 | |
| 207 | /* Get GID of group 'tracing' */ |
| 208 | grp_tracing = getgrnam(grp_name); |
| 209 | if (!grp_tracing) { |
| 210 | /* If grp_tracing is NULL, the group does not exist. */ |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | /* Get number of supplementary group IDs */ |
| 215 | grp_list_size = getgroups(0, NULL); |
| 216 | if (grp_list_size < 0) { |
| 217 | perror("getgroups"); |
| 218 | goto end; |
| 219 | } |
| 220 | |
| 221 | /* Alloc group list of the right size */ |
| 222 | grp_list = malloc(grp_list_size * sizeof(gid_t)); |
| 223 | if (!grp_list) { |
| 224 | perror("malloc"); |
| 225 | goto end; |
| 226 | } |
| 227 | grp_id = getgroups(grp_list_size, grp_list); |
| 228 | if (grp_id < 0) { |
| 229 | perror("getgroups"); |
| 230 | goto free_list; |
| 231 | } |
| 232 | |
| 233 | for (i = 0; i < grp_list_size; i++) { |
| 234 | if (grp_list[i] == grp_tracing->gr_gid) { |
| 235 | ret = 1; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | free_list: |
| 241 | free(grp_list); |
| 242 | |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Try connect to session daemon with sock_path. |
| 249 | * |
| 250 | * Return 0 on success, else -1 |
| 251 | */ |
| 252 | static int try_connect_sessiond(const char *sock_path) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 256 | /* If socket exist, we check if the daemon listens for connect. */ |
| 257 | ret = access(sock_path, F_OK); |
| 258 | if (ret < 0) { |
| 259 | /* Not alive */ |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | ret = lttcomm_connect_unix_sock(sock_path); |
| 264 | if (ret < 0) { |
| 265 | /* Not alive */ |
| 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | ret = lttcomm_close_unix_sock(ret); |
| 270 | if (ret < 0) { |
| 271 | perror("lttcomm_close_unix_sock"); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | |
| 276 | error: |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
| 282 | * variable. |
| 283 | * |
| 284 | * Returns 0 on success, negative value on failure (the sessiond socket path |
| 285 | * is somehow too long or ENOMEM). |
| 286 | */ |
| 287 | static int set_session_daemon_path(void) |
| 288 | { |
| 289 | int in_tgroup = 0; /* In tracing group */ |
| 290 | uid_t uid; |
| 291 | |
| 292 | uid = getuid(); |
| 293 | |
| 294 | if (uid != 0) { |
| 295 | /* Are we in the tracing group ? */ |
| 296 | in_tgroup = check_tracing_group(tracing_group); |
| 297 | } |
| 298 | |
| 299 | if ((uid == 0) || in_tgroup) { |
| 300 | lttng_ctl_copy_string(sessiond_sock_path, |
| 301 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path)); |
| 302 | } |
| 303 | |
| 304 | if (uid != 0) { |
| 305 | int ret; |
| 306 | |
| 307 | if (in_tgroup) { |
| 308 | /* Tracing group */ |
| 309 | ret = try_connect_sessiond(sessiond_sock_path); |
| 310 | if (ret >= 0) { |
| 311 | goto end; |
| 312 | } |
| 313 | /* Global session daemon not available... */ |
| 314 | } |
| 315 | /* ...or not in tracing group (and not root), default */ |
| 316 | |
| 317 | /* |
| 318 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; |
| 319 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) |
| 320 | */ |
| 321 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), |
| 322 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
| 323 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
| 324 | goto error; |
| 325 | } |
| 326 | } |
| 327 | end: |
| 328 | return 0; |
| 329 | |
| 330 | error: |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Connect to the LTTng session daemon. |
| 336 | * |
| 337 | * On success, return 0. On error, return -1. |
| 338 | */ |
| 339 | static int connect_sessiond(void) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | /* Don't try to connect if already connected. */ |
| 344 | if (connected) { |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | ret = set_session_daemon_path(); |
| 349 | if (ret < 0) { |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | /* Connect to the sesssion daemon */ |
| 354 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
| 355 | if (ret < 0) { |
| 356 | goto error; |
| 357 | } |
| 358 | |
| 359 | sessiond_socket = ret; |
| 360 | connected = 1; |
| 361 | |
| 362 | return 0; |
| 363 | |
| 364 | error: |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Clean disconnect from the session daemon. |
| 370 | * On success, return 0. On error, return -1. |
| 371 | */ |
| 372 | static int disconnect_sessiond(void) |
| 373 | { |
| 374 | int ret = 0; |
| 375 | |
| 376 | if (connected) { |
| 377 | ret = lttcomm_close_unix_sock(sessiond_socket); |
| 378 | sessiond_socket = 0; |
| 379 | connected = 0; |
| 380 | } |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Ask the session daemon a specific command and put the data into buf. |
| 387 | * Takes extra var. len. data as input to send to the session daemon. |
| 388 | * |
| 389 | * Return size of data (only payload, not header) or a negative error code. |
| 390 | */ |
| 391 | LTTNG_HIDDEN |
| 392 | int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm, |
| 393 | void *vardata, size_t varlen, void **buf) |
| 394 | { |
| 395 | int ret; |
| 396 | size_t size; |
| 397 | void *data = NULL; |
| 398 | struct lttcomm_lttng_msg llm; |
| 399 | |
| 400 | ret = connect_sessiond(); |
| 401 | if (ret < 0) { |
| 402 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 403 | goto end; |
| 404 | } |
| 405 | |
| 406 | /* Send command to session daemon */ |
| 407 | ret = send_session_msg(lsm); |
| 408 | if (ret < 0) { |
| 409 | /* Ret value is a valid lttng error code. */ |
| 410 | goto end; |
| 411 | } |
| 412 | /* Send var len data */ |
| 413 | ret = send_session_varlen(vardata, varlen); |
| 414 | if (ret < 0) { |
| 415 | /* Ret value is a valid lttng error code. */ |
| 416 | goto end; |
| 417 | } |
| 418 | |
| 419 | /* Get header from data transmission */ |
| 420 | ret = recv_data_sessiond(&llm, sizeof(llm)); |
| 421 | if (ret < 0) { |
| 422 | /* Ret value is a valid lttng error code. */ |
| 423 | goto end; |
| 424 | } |
| 425 | |
| 426 | /* Check error code if OK */ |
| 427 | if (llm.ret_code != LTTNG_OK) { |
| 428 | ret = -llm.ret_code; |
| 429 | goto end; |
| 430 | } |
| 431 | |
| 432 | size = llm.data_size; |
| 433 | if (size == 0) { |
| 434 | /* If client free with size 0 */ |
| 435 | if (buf != NULL) { |
| 436 | *buf = NULL; |
| 437 | } |
| 438 | ret = 0; |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | data = (void*) malloc(size); |
| 443 | |
| 444 | /* Get payload data */ |
| 445 | ret = recv_data_sessiond(data, size); |
| 446 | if (ret < 0) { |
| 447 | free(data); |
| 448 | goto end; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Extra protection not to dereference a NULL pointer. If buf is NULL at |
| 453 | * this point, an error is returned and data is freed. |
| 454 | */ |
| 455 | if (buf == NULL) { |
| 456 | ret = -LTTNG_ERR_INVALID; |
| 457 | free(data); |
| 458 | goto end; |
| 459 | } |
| 460 | |
| 461 | *buf = data; |
| 462 | ret = size; |
| 463 | |
| 464 | end: |
| 465 | disconnect_sessiond(); |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Create lttng handle and return pointer. |
| 471 | * The returned pointer will be NULL in case of malloc() error. |
| 472 | */ |
| 473 | struct lttng_handle *lttng_create_handle(const char *session_name, |
| 474 | struct lttng_domain *domain) |
| 475 | { |
| 476 | struct lttng_handle *handle = NULL; |
| 477 | |
| 478 | if (domain == NULL) { |
| 479 | goto end; |
| 480 | } |
| 481 | |
| 482 | handle = malloc(sizeof(struct lttng_handle)); |
| 483 | if (handle == NULL) { |
| 484 | PERROR("malloc handle"); |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | /* Copy session name */ |
| 489 | lttng_ctl_copy_string(handle->session_name, session_name, |
| 490 | sizeof(handle->session_name)); |
| 491 | |
| 492 | /* Copy lttng domain */ |
| 493 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); |
| 494 | |
| 495 | end: |
| 496 | return handle; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Destroy handle by free(3) the pointer. |
| 501 | */ |
| 502 | void lttng_destroy_handle(struct lttng_handle *handle) |
| 503 | { |
| 504 | free(handle); |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Register an outside consumer. |
| 509 | * Returns size of returned session payload data or a negative error code. |
| 510 | */ |
| 511 | int lttng_register_consumer(struct lttng_handle *handle, |
| 512 | const char *socket_path) |
| 513 | { |
| 514 | struct lttcomm_session_msg lsm; |
| 515 | |
| 516 | if (handle == NULL || socket_path == NULL) { |
| 517 | return -LTTNG_ERR_INVALID; |
| 518 | } |
| 519 | |
| 520 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
| 521 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 522 | sizeof(lsm.session.name)); |
| 523 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 524 | |
| 525 | lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); |
| 526 | |
| 527 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Start tracing for all traces of the session. |
| 532 | * Returns size of returned session payload data or a negative error code. |
| 533 | */ |
| 534 | int lttng_start_tracing(const char *session_name) |
| 535 | { |
| 536 | struct lttcomm_session_msg lsm; |
| 537 | |
| 538 | if (session_name == NULL) { |
| 539 | return -LTTNG_ERR_INVALID; |
| 540 | } |
| 541 | |
| 542 | lsm.cmd_type = LTTNG_START_TRACE; |
| 543 | |
| 544 | lttng_ctl_copy_string(lsm.session.name, session_name, |
| 545 | sizeof(lsm.session.name)); |
| 546 | |
| 547 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Stop tracing for all traces of the session. |
| 552 | */ |
| 553 | static int _lttng_stop_tracing(const char *session_name, int wait) |
| 554 | { |
| 555 | int ret, data_ret; |
| 556 | struct lttcomm_session_msg lsm; |
| 557 | |
| 558 | if (session_name == NULL) { |
| 559 | return -LTTNG_ERR_INVALID; |
| 560 | } |
| 561 | |
| 562 | lsm.cmd_type = LTTNG_STOP_TRACE; |
| 563 | |
| 564 | lttng_ctl_copy_string(lsm.session.name, session_name, |
| 565 | sizeof(lsm.session.name)); |
| 566 | |
| 567 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 568 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
| 569 | goto error; |
| 570 | } |
| 571 | |
| 572 | if (!wait) { |
| 573 | goto end; |
| 574 | } |
| 575 | |
| 576 | _MSG("Waiting for data availability"); |
| 577 | fflush(stdout); |
| 578 | |
| 579 | /* Check for data availability */ |
| 580 | do { |
| 581 | data_ret = lttng_data_pending(session_name); |
| 582 | if (data_ret < 0) { |
| 583 | /* Return the data available call error. */ |
| 584 | ret = data_ret; |
| 585 | goto error; |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Data sleep time before retrying (in usec). Don't sleep if the call |
| 590 | * returned value indicates availability. |
| 591 | */ |
| 592 | if (data_ret) { |
| 593 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); |
| 594 | _MSG("."); |
| 595 | fflush(stdout); |
| 596 | } |
| 597 | } while (data_ret != 0); |
| 598 | |
| 599 | MSG(""); |
| 600 | |
| 601 | end: |
| 602 | error: |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Stop tracing and wait for data availability. |
| 608 | */ |
| 609 | int lttng_stop_tracing(const char *session_name) |
| 610 | { |
| 611 | return _lttng_stop_tracing(session_name, 1); |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Stop tracing but _don't_ wait for data availability. |
| 616 | */ |
| 617 | int lttng_stop_tracing_no_wait(const char *session_name) |
| 618 | { |
| 619 | return _lttng_stop_tracing(session_name, 0); |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Add context to a channel. |
| 624 | * |
| 625 | * If the given channel is NULL, add the contexts to all channels. |
| 626 | * The event_name param is ignored. |
| 627 | * |
| 628 | * Returns the size of the returned payload data or a negative error code. |
| 629 | */ |
| 630 | int lttng_add_context(struct lttng_handle *handle, |
| 631 | struct lttng_event_context *ctx, const char *event_name, |
| 632 | const char *channel_name) |
| 633 | { |
| 634 | struct lttcomm_session_msg lsm; |
| 635 | |
| 636 | /* Safety check. Both are mandatory */ |
| 637 | if (handle == NULL || ctx == NULL) { |
| 638 | return -LTTNG_ERR_INVALID; |
| 639 | } |
| 640 | |
| 641 | memset(&lsm, 0, sizeof(lsm)); |
| 642 | |
| 643 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
| 644 | |
| 645 | /* If no channel name, send empty string. */ |
| 646 | if (channel_name == NULL) { |
| 647 | lttng_ctl_copy_string(lsm.u.context.channel_name, "", |
| 648 | sizeof(lsm.u.context.channel_name)); |
| 649 | } else { |
| 650 | lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name, |
| 651 | sizeof(lsm.u.context.channel_name)); |
| 652 | } |
| 653 | |
| 654 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 655 | |
| 656 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
| 657 | |
| 658 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 659 | sizeof(lsm.session.name)); |
| 660 | |
| 661 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Enable event(s) for a channel. |
| 666 | * If no event name is specified, all events are enabled. |
| 667 | * If no channel name is specified, the default 'channel0' is used. |
| 668 | * Returns size of returned session payload data or a negative error code. |
| 669 | */ |
| 670 | int lttng_enable_event(struct lttng_handle *handle, |
| 671 | struct lttng_event *ev, const char *channel_name) |
| 672 | { |
| 673 | struct lttcomm_session_msg lsm; |
| 674 | |
| 675 | if (handle == NULL || ev == NULL) { |
| 676 | return -LTTNG_ERR_INVALID; |
| 677 | } |
| 678 | |
| 679 | memset(&lsm, 0, sizeof(lsm)); |
| 680 | |
| 681 | /* If no channel name, send empty string. */ |
| 682 | if (channel_name == NULL) { |
| 683 | lttng_ctl_copy_string(lsm.u.enable.channel_name, "", |
| 684 | sizeof(lsm.u.enable.channel_name)); |
| 685 | } else { |
| 686 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, |
| 687 | sizeof(lsm.u.enable.channel_name)); |
| 688 | } |
| 689 | |
| 690 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 691 | |
| 692 | if (ev->name[0] != '\0') { |
| 693 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
| 694 | } else { |
| 695 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
| 696 | } |
| 697 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
| 698 | |
| 699 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 700 | sizeof(lsm.session.name)); |
| 701 | |
| 702 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Create or enable an event with a filter expression. |
| 707 | * |
| 708 | * Return negative error value on error. |
| 709 | * Return size of returned session payload data if OK. |
| 710 | */ |
| 711 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
| 712 | struct lttng_event *event, const char *channel_name, |
| 713 | const char *filter_expression) |
| 714 | { |
| 715 | struct lttcomm_session_msg lsm; |
| 716 | struct filter_parser_ctx *ctx; |
| 717 | FILE *fmem; |
| 718 | int ret = 0; |
| 719 | |
| 720 | if (!filter_expression) { |
| 721 | /* |
| 722 | * Fall back to normal event enabling if no filter |
| 723 | * specified. |
| 724 | */ |
| 725 | return lttng_enable_event(handle, event, channel_name); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Empty filter string will always be rejected by the parser |
| 730 | * anyway, so treat this corner-case early to eliminate |
| 731 | * lttng_fmemopen error for 0-byte allocation. |
| 732 | */ |
| 733 | if (handle == NULL || filter_expression[0] == '\0') { |
| 734 | return -LTTNG_ERR_INVALID; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * casting const to non-const, as the underlying function will |
| 739 | * use it in read-only mode. |
| 740 | */ |
| 741 | fmem = lttng_fmemopen((void *) filter_expression, |
| 742 | strlen(filter_expression), "r"); |
| 743 | if (!fmem) { |
| 744 | fprintf(stderr, "Error opening memory as stream\n"); |
| 745 | return -LTTNG_ERR_FILTER_NOMEM; |
| 746 | } |
| 747 | ctx = filter_parser_ctx_alloc(fmem); |
| 748 | if (!ctx) { |
| 749 | fprintf(stderr, "Error allocating parser\n"); |
| 750 | ret = -LTTNG_ERR_FILTER_NOMEM; |
| 751 | goto alloc_error; |
| 752 | } |
| 753 | ret = filter_parser_ctx_append_ast(ctx); |
| 754 | if (ret) { |
| 755 | fprintf(stderr, "Parse error\n"); |
| 756 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 757 | goto parse_error; |
| 758 | } |
| 759 | ret = filter_visitor_set_parent(ctx); |
| 760 | if (ret) { |
| 761 | fprintf(stderr, "Set parent error\n"); |
| 762 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 763 | goto parse_error; |
| 764 | } |
| 765 | if (print_xml) { |
| 766 | ret = filter_visitor_print_xml(ctx, stdout, 0); |
| 767 | if (ret) { |
| 768 | fflush(stdout); |
| 769 | fprintf(stderr, "XML print error\n"); |
| 770 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 771 | goto parse_error; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | dbg_printf("Generating IR... "); |
| 776 | fflush(stdout); |
| 777 | ret = filter_visitor_ir_generate(ctx); |
| 778 | if (ret) { |
| 779 | fprintf(stderr, "Generate IR error\n"); |
| 780 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 781 | goto parse_error; |
| 782 | } |
| 783 | dbg_printf("done\n"); |
| 784 | |
| 785 | dbg_printf("Validating IR... "); |
| 786 | fflush(stdout); |
| 787 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); |
| 788 | if (ret) { |
| 789 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 790 | goto parse_error; |
| 791 | } |
| 792 | dbg_printf("done\n"); |
| 793 | |
| 794 | dbg_printf("Generating bytecode... "); |
| 795 | fflush(stdout); |
| 796 | ret = filter_visitor_bytecode_generate(ctx); |
| 797 | if (ret) { |
| 798 | fprintf(stderr, "Generate bytecode error\n"); |
| 799 | ret = -LTTNG_ERR_FILTER_INVAL; |
| 800 | goto parse_error; |
| 801 | } |
| 802 | dbg_printf("done\n"); |
| 803 | dbg_printf("Size of bytecode generated: %u bytes.\n", |
| 804 | bytecode_get_len(&ctx->bytecode->b)); |
| 805 | |
| 806 | memset(&lsm, 0, sizeof(lsm)); |
| 807 | |
| 808 | lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER; |
| 809 | |
| 810 | /* If no channel name, send empty string. */ |
| 811 | if (channel_name == NULL) { |
| 812 | lttng_ctl_copy_string(lsm.u.enable.channel_name, "", |
| 813 | sizeof(lsm.u.enable.channel_name)); |
| 814 | } else { |
| 815 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, |
| 816 | sizeof(lsm.u.enable.channel_name)); |
| 817 | } |
| 818 | |
| 819 | /* Copy event name */ |
| 820 | if (event) { |
| 821 | memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event)); |
| 822 | } |
| 823 | |
| 824 | lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) |
| 825 | + bytecode_get_len(&ctx->bytecode->b); |
| 826 | |
| 827 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 828 | |
| 829 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 830 | sizeof(lsm.session.name)); |
| 831 | |
| 832 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b, |
| 833 | lsm.u.enable.bytecode_len, NULL); |
| 834 | |
| 835 | filter_bytecode_free(ctx); |
| 836 | filter_ir_free(ctx); |
| 837 | filter_parser_ctx_free(ctx); |
| 838 | if (fclose(fmem) != 0) { |
| 839 | perror("fclose"); |
| 840 | } |
| 841 | return ret; |
| 842 | |
| 843 | parse_error: |
| 844 | filter_bytecode_free(ctx); |
| 845 | filter_ir_free(ctx); |
| 846 | filter_parser_ctx_free(ctx); |
| 847 | alloc_error: |
| 848 | if (fclose(fmem) != 0) { |
| 849 | perror("fclose"); |
| 850 | } |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Disable event(s) of a channel and domain. |
| 856 | * If no event name is specified, all events are disabled. |
| 857 | * If no channel name is specified, the default 'channel0' is used. |
| 858 | * Returns size of returned session payload data or a negative error code. |
| 859 | */ |
| 860 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
| 861 | const char *channel_name) |
| 862 | { |
| 863 | struct lttcomm_session_msg lsm; |
| 864 | |
| 865 | if (handle == NULL) { |
| 866 | return -LTTNG_ERR_INVALID; |
| 867 | } |
| 868 | |
| 869 | memset(&lsm, 0, sizeof(lsm)); |
| 870 | |
| 871 | /* If no channel name, send empty string. */ |
| 872 | if (channel_name == NULL) { |
| 873 | lttng_ctl_copy_string(lsm.u.disable.channel_name, "", |
| 874 | sizeof(lsm.u.disable.channel_name)); |
| 875 | } else { |
| 876 | lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name, |
| 877 | sizeof(lsm.u.disable.channel_name)); |
| 878 | } |
| 879 | |
| 880 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 881 | |
| 882 | if (name != NULL) { |
| 883 | lttng_ctl_copy_string(lsm.u.disable.name, name, |
| 884 | sizeof(lsm.u.disable.name)); |
| 885 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
| 886 | } else { |
| 887 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
| 888 | } |
| 889 | |
| 890 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 891 | sizeof(lsm.session.name)); |
| 892 | |
| 893 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Enable channel per domain |
| 898 | * Returns size of returned session payload data or a negative error code. |
| 899 | */ |
| 900 | int lttng_enable_channel(struct lttng_handle *handle, |
| 901 | struct lttng_channel *chan) |
| 902 | { |
| 903 | struct lttcomm_session_msg lsm; |
| 904 | |
| 905 | /* |
| 906 | * NULL arguments are forbidden. No default values. |
| 907 | */ |
| 908 | if (handle == NULL || chan == NULL) { |
| 909 | return -LTTNG_ERR_INVALID; |
| 910 | } |
| 911 | |
| 912 | memset(&lsm, 0, sizeof(lsm)); |
| 913 | |
| 914 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
| 915 | |
| 916 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
| 917 | |
| 918 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 919 | |
| 920 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 921 | sizeof(lsm.session.name)); |
| 922 | |
| 923 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * All tracing will be stopped for registered events of the channel. |
| 928 | * Returns size of returned session payload data or a negative error code. |
| 929 | */ |
| 930 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
| 931 | { |
| 932 | struct lttcomm_session_msg lsm; |
| 933 | |
| 934 | /* Safety check. Both are mandatory */ |
| 935 | if (handle == NULL || name == NULL) { |
| 936 | return -LTTNG_ERR_INVALID; |
| 937 | } |
| 938 | |
| 939 | memset(&lsm, 0, sizeof(lsm)); |
| 940 | |
| 941 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
| 942 | |
| 943 | lttng_ctl_copy_string(lsm.u.disable.channel_name, name, |
| 944 | sizeof(lsm.u.disable.channel_name)); |
| 945 | |
| 946 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 947 | |
| 948 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 949 | sizeof(lsm.session.name)); |
| 950 | |
| 951 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Lists all available tracepoints of domain. |
| 956 | * Sets the contents of the events array. |
| 957 | * Returns the number of lttng_event entries in events; |
| 958 | * on error, returns a negative value. |
| 959 | */ |
| 960 | int lttng_list_tracepoints(struct lttng_handle *handle, |
| 961 | struct lttng_event **events) |
| 962 | { |
| 963 | int ret; |
| 964 | struct lttcomm_session_msg lsm; |
| 965 | |
| 966 | if (handle == NULL) { |
| 967 | return -LTTNG_ERR_INVALID; |
| 968 | } |
| 969 | |
| 970 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
| 971 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 972 | |
| 973 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
| 974 | if (ret < 0) { |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | return ret / sizeof(struct lttng_event); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Lists all available tracepoint fields of domain. |
| 983 | * Sets the contents of the event field array. |
| 984 | * Returns the number of lttng_event_field entries in events; |
| 985 | * on error, returns a negative value. |
| 986 | */ |
| 987 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, |
| 988 | struct lttng_event_field **fields) |
| 989 | { |
| 990 | int ret; |
| 991 | struct lttcomm_session_msg lsm; |
| 992 | |
| 993 | if (handle == NULL) { |
| 994 | return -LTTNG_ERR_INVALID; |
| 995 | } |
| 996 | |
| 997 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
| 998 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 999 | |
| 1000 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
| 1001 | if (ret < 0) { |
| 1002 | return ret; |
| 1003 | } |
| 1004 | |
| 1005 | return ret / sizeof(struct lttng_event_field); |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Returns a human readable string describing |
| 1010 | * the error code (a negative value). |
| 1011 | */ |
| 1012 | const char *lttng_strerror(int code) |
| 1013 | { |
| 1014 | return error_get_str(code); |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Create a brand new session using name and url for destination. |
| 1019 | * |
| 1020 | * Returns LTTNG_OK on success or a negative error code. |
| 1021 | */ |
| 1022 | int lttng_create_session(const char *name, const char *url) |
| 1023 | { |
| 1024 | int ret; |
| 1025 | ssize_t size; |
| 1026 | struct lttcomm_session_msg lsm; |
| 1027 | struct lttng_uri *uris = NULL; |
| 1028 | |
| 1029 | if (name == NULL) { |
| 1030 | return -LTTNG_ERR_INVALID; |
| 1031 | } |
| 1032 | |
| 1033 | memset(&lsm, 0, sizeof(lsm)); |
| 1034 | |
| 1035 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
| 1036 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
| 1037 | |
| 1038 | /* There should never be a data URL */ |
| 1039 | size = uri_parse_str_urls(url, NULL, &uris); |
| 1040 | if (size < 0) { |
| 1041 | return -LTTNG_ERR_INVALID; |
| 1042 | } |
| 1043 | |
| 1044 | lsm.u.uri.size = size; |
| 1045 | |
| 1046 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
| 1047 | sizeof(struct lttng_uri) * size, NULL); |
| 1048 | |
| 1049 | free(uris); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Destroy session using name. |
| 1055 | * Returns size of returned session payload data or a negative error code. |
| 1056 | */ |
| 1057 | int lttng_destroy_session(const char *session_name) |
| 1058 | { |
| 1059 | struct lttcomm_session_msg lsm; |
| 1060 | |
| 1061 | if (session_name == NULL) { |
| 1062 | return -LTTNG_ERR_INVALID; |
| 1063 | } |
| 1064 | |
| 1065 | lsm.cmd_type = LTTNG_DESTROY_SESSION; |
| 1066 | |
| 1067 | lttng_ctl_copy_string(lsm.session.name, session_name, |
| 1068 | sizeof(lsm.session.name)); |
| 1069 | |
| 1070 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * Ask the session daemon for all available sessions. |
| 1075 | * Sets the contents of the sessions array. |
| 1076 | * Returns the number of lttng_session entries in sessions; |
| 1077 | * on error, returns a negative value. |
| 1078 | */ |
| 1079 | int lttng_list_sessions(struct lttng_session **sessions) |
| 1080 | { |
| 1081 | int ret; |
| 1082 | struct lttcomm_session_msg lsm; |
| 1083 | |
| 1084 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
| 1085 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions); |
| 1086 | if (ret < 0) { |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
| 1090 | return ret / sizeof(struct lttng_session); |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Ask the session daemon for all available domains of a session. |
| 1095 | * Sets the contents of the domains array. |
| 1096 | * Returns the number of lttng_domain entries in domains; |
| 1097 | * on error, returns a negative value. |
| 1098 | */ |
| 1099 | int lttng_list_domains(const char *session_name, |
| 1100 | struct lttng_domain **domains) |
| 1101 | { |
| 1102 | int ret; |
| 1103 | struct lttcomm_session_msg lsm; |
| 1104 | |
| 1105 | if (session_name == NULL) { |
| 1106 | return -LTTNG_ERR_INVALID; |
| 1107 | } |
| 1108 | |
| 1109 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
| 1110 | |
| 1111 | lttng_ctl_copy_string(lsm.session.name, session_name, |
| 1112 | sizeof(lsm.session.name)); |
| 1113 | |
| 1114 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
| 1115 | if (ret < 0) { |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | return ret / sizeof(struct lttng_domain); |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Ask the session daemon for all available channels of a session. |
| 1124 | * Sets the contents of the channels array. |
| 1125 | * Returns the number of lttng_channel entries in channels; |
| 1126 | * on error, returns a negative value. |
| 1127 | */ |
| 1128 | int lttng_list_channels(struct lttng_handle *handle, |
| 1129 | struct lttng_channel **channels) |
| 1130 | { |
| 1131 | int ret; |
| 1132 | struct lttcomm_session_msg lsm; |
| 1133 | |
| 1134 | if (handle == NULL) { |
| 1135 | return -LTTNG_ERR_INVALID; |
| 1136 | } |
| 1137 | |
| 1138 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
| 1139 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 1140 | sizeof(lsm.session.name)); |
| 1141 | |
| 1142 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 1143 | |
| 1144 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); |
| 1145 | if (ret < 0) { |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | return ret / sizeof(struct lttng_channel); |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Ask the session daemon for all available events of a session channel. |
| 1154 | * Sets the contents of the events array. |
| 1155 | * Returns the number of lttng_event entries in events; |
| 1156 | * on error, returns a negative value. |
| 1157 | */ |
| 1158 | int lttng_list_events(struct lttng_handle *handle, |
| 1159 | const char *channel_name, struct lttng_event **events) |
| 1160 | { |
| 1161 | int ret; |
| 1162 | struct lttcomm_session_msg lsm; |
| 1163 | |
| 1164 | /* Safety check. An handle and channel name are mandatory */ |
| 1165 | if (handle == NULL || channel_name == NULL) { |
| 1166 | return -LTTNG_ERR_INVALID; |
| 1167 | } |
| 1168 | |
| 1169 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
| 1170 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 1171 | sizeof(lsm.session.name)); |
| 1172 | lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name, |
| 1173 | sizeof(lsm.u.list.channel_name)); |
| 1174 | |
| 1175 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 1176 | |
| 1177 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) events); |
| 1178 | if (ret < 0) { |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | return ret / sizeof(struct lttng_event); |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Sets the tracing_group variable with name. |
| 1187 | * This function allocates memory pointed to by tracing_group. |
| 1188 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. |
| 1189 | */ |
| 1190 | int lttng_set_tracing_group(const char *name) |
| 1191 | { |
| 1192 | if (name == NULL) { |
| 1193 | return -LTTNG_ERR_INVALID; |
| 1194 | } |
| 1195 | |
| 1196 | if (asprintf(&tracing_group, "%s", name) < 0) { |
| 1197 | return -LTTNG_ERR_FATAL; |
| 1198 | } |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * Returns size of returned session payload data or a negative error code. |
| 1205 | */ |
| 1206 | int lttng_calibrate(struct lttng_handle *handle, |
| 1207 | struct lttng_calibrate *calibrate) |
| 1208 | { |
| 1209 | struct lttcomm_session_msg lsm; |
| 1210 | |
| 1211 | /* Safety check. NULL pointer are forbidden */ |
| 1212 | if (handle == NULL || calibrate == NULL) { |
| 1213 | return -LTTNG_ERR_INVALID; |
| 1214 | } |
| 1215 | |
| 1216 | lsm.cmd_type = LTTNG_CALIBRATE; |
| 1217 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 1218 | |
| 1219 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
| 1220 | |
| 1221 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Set default channel attributes. |
| 1226 | * If either or both of the arguments are null, attr content is zeroe'd. |
| 1227 | */ |
| 1228 | void lttng_channel_set_default_attr(struct lttng_domain *domain, |
| 1229 | struct lttng_channel_attr *attr) |
| 1230 | { |
| 1231 | /* Safety check */ |
| 1232 | if (attr == NULL || domain == NULL) { |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
| 1237 | |
| 1238 | /* Same for all domains. */ |
| 1239 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 1240 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; |
| 1241 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; |
| 1242 | |
| 1243 | switch (domain->type) { |
| 1244 | case LTTNG_DOMAIN_KERNEL: |
| 1245 | attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
| 1246 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
| 1247 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
| 1248 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 1249 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 1250 | break; |
| 1251 | case LTTNG_DOMAIN_UST: |
| 1252 | switch (domain->buf_type) { |
| 1253 | case LTTNG_BUFFER_PER_UID: |
| 1254 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); |
| 1255 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; |
| 1256 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; |
| 1257 | attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; |
| 1258 | attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER; |
| 1259 | break; |
| 1260 | case LTTNG_BUFFER_PER_PID: |
| 1261 | default: |
| 1262 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); |
| 1263 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; |
| 1264 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; |
| 1265 | attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; |
| 1266 | attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER; |
| 1267 | break; |
| 1268 | } |
| 1269 | default: |
| 1270 | /* Default behavior: leave set to 0. */ |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Check if session daemon is alive. |
| 1277 | * |
| 1278 | * Return 1 if alive or 0 if not. |
| 1279 | * On error returns a negative value. |
| 1280 | */ |
| 1281 | int lttng_session_daemon_alive(void) |
| 1282 | { |
| 1283 | int ret; |
| 1284 | |
| 1285 | ret = set_session_daemon_path(); |
| 1286 | if (ret < 0) { |
| 1287 | /* Error */ |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | if (*sessiond_sock_path == '\0') { |
| 1292 | /* |
| 1293 | * No socket path set. Weird error which means the constructor was not |
| 1294 | * called. |
| 1295 | */ |
| 1296 | assert(0); |
| 1297 | } |
| 1298 | |
| 1299 | ret = try_connect_sessiond(sessiond_sock_path); |
| 1300 | if (ret < 0) { |
| 1301 | /* Not alive */ |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /* Is alive */ |
| 1306 | return 1; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Set URL for a consumer for a session and domain. |
| 1311 | * |
| 1312 | * Return 0 on success, else a negative value. |
| 1313 | */ |
| 1314 | int lttng_set_consumer_url(struct lttng_handle *handle, |
| 1315 | const char *control_url, const char *data_url) |
| 1316 | { |
| 1317 | int ret; |
| 1318 | ssize_t size; |
| 1319 | struct lttcomm_session_msg lsm; |
| 1320 | struct lttng_uri *uris = NULL; |
| 1321 | |
| 1322 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
| 1323 | return -LTTNG_ERR_INVALID; |
| 1324 | } |
| 1325 | |
| 1326 | memset(&lsm, 0, sizeof(lsm)); |
| 1327 | |
| 1328 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
| 1329 | |
| 1330 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
| 1331 | sizeof(lsm.session.name)); |
| 1332 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
| 1333 | |
| 1334 | size = uri_parse_str_urls(control_url, data_url, &uris); |
| 1335 | if (size < 0) { |
| 1336 | return -LTTNG_ERR_INVALID; |
| 1337 | } |
| 1338 | |
| 1339 | lsm.u.uri.size = size; |
| 1340 | |
| 1341 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
| 1342 | sizeof(struct lttng_uri) * size, NULL); |
| 1343 | |
| 1344 | free(uris); |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * [OBSOLETE] |
| 1350 | */ |
| 1351 | int lttng_enable_consumer(struct lttng_handle *handle) |
| 1352 | { |
| 1353 | return -ENOSYS; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * [OBSOLETE] |
| 1358 | */ |
| 1359 | int lttng_disable_consumer(struct lttng_handle *handle) |
| 1360 | { |
| 1361 | return -ENOSYS; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * Set health socket path by putting it in the global health_sock_path |
| 1366 | * variable. |
| 1367 | * |
| 1368 | * Returns 0 on success or assert(0) on ENOMEM. |
| 1369 | */ |
| 1370 | static int set_health_socket_path(void) |
| 1371 | { |
| 1372 | int in_tgroup = 0; /* In tracing group */ |
| 1373 | uid_t uid; |
| 1374 | const char *home; |
| 1375 | |
| 1376 | uid = getuid(); |
| 1377 | |
| 1378 | if (uid != 0) { |
| 1379 | /* Are we in the tracing group ? */ |
| 1380 | in_tgroup = check_tracing_group(tracing_group); |
| 1381 | } |
| 1382 | |
| 1383 | if ((uid == 0) || in_tgroup) { |
| 1384 | lttng_ctl_copy_string(health_sock_path, |
| 1385 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, sizeof(health_sock_path)); |
| 1386 | } |
| 1387 | |
| 1388 | if (uid != 0) { |
| 1389 | int ret; |
| 1390 | |
| 1391 | /* |
| 1392 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; |
| 1393 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) |
| 1394 | */ |
| 1395 | home = utils_get_home_dir(); |
| 1396 | if (home == NULL) { |
| 1397 | /* Fallback in /tmp .. */ |
| 1398 | home = "/tmp"; |
| 1399 | } |
| 1400 | |
| 1401 | ret = snprintf(health_sock_path, sizeof(health_sock_path), |
| 1402 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home); |
| 1403 | if ((ret < 0) || (ret >= sizeof(health_sock_path))) { |
| 1404 | /* ENOMEM at this point... just kill the control lib. */ |
| 1405 | assert(0); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * Check session daemon health for a specific health component. |
| 1414 | * |
| 1415 | * Return 0 if health is OK or else 1 if BAD. |
| 1416 | * |
| 1417 | * Any other negative value is a lttng error code which can be translated with |
| 1418 | * lttng_strerror(). |
| 1419 | */ |
| 1420 | int lttng_health_check(enum lttng_health_component c) |
| 1421 | { |
| 1422 | int sock, ret; |
| 1423 | struct lttcomm_health_msg msg; |
| 1424 | struct lttcomm_health_data reply; |
| 1425 | |
| 1426 | /* Connect to the sesssion daemon */ |
| 1427 | sock = lttcomm_connect_unix_sock(health_sock_path); |
| 1428 | if (sock < 0) { |
| 1429 | ret = -LTTNG_ERR_NO_SESSIOND; |
| 1430 | goto error; |
| 1431 | } |
| 1432 | |
| 1433 | msg.cmd = LTTNG_HEALTH_CHECK; |
| 1434 | msg.component = c; |
| 1435 | |
| 1436 | ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg)); |
| 1437 | if (ret < 0) { |
| 1438 | ret = -LTTNG_ERR_FATAL; |
| 1439 | goto close_error; |
| 1440 | } |
| 1441 | |
| 1442 | ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply)); |
| 1443 | if (ret < 0) { |
| 1444 | ret = -LTTNG_ERR_FATAL; |
| 1445 | goto close_error; |
| 1446 | } |
| 1447 | |
| 1448 | ret = reply.ret_code; |
| 1449 | |
| 1450 | close_error: |
| 1451 | { |
| 1452 | int closeret; |
| 1453 | |
| 1454 | closeret = close(sock); |
| 1455 | assert(!closeret); |
| 1456 | } |
| 1457 | |
| 1458 | error: |
| 1459 | return ret; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * This is an extension of create session that is ONLY and SHOULD only be used |
| 1464 | * by the lttng command line program. It exists to avoid using URI parsing in |
| 1465 | * the lttng client. |
| 1466 | * |
| 1467 | * We need the date and time for the trace path subdirectory for the case where |
| 1468 | * the user does NOT define one using either -o or -U. Using the normal |
| 1469 | * lttng_create_session API call, we have no clue on the session daemon side if |
| 1470 | * the URL was generated automatically by the client or define by the user. |
| 1471 | * |
| 1472 | * So this function "wrapper" is hidden from the public API, takes the datetime |
| 1473 | * string and appends it if necessary to the URI subdirectory before sending it |
| 1474 | * to the session daemon. |
| 1475 | * |
| 1476 | * With this extra function, the lttng_create_session call behavior is not |
| 1477 | * changed and the timestamp is appended to the URI on the session daemon side |
| 1478 | * if necessary. |
| 1479 | */ |
| 1480 | int _lttng_create_session_ext(const char *name, const char *url, |
| 1481 | const char *datetime) |
| 1482 | { |
| 1483 | int ret; |
| 1484 | ssize_t size; |
| 1485 | struct lttcomm_session_msg lsm; |
| 1486 | struct lttng_uri *uris = NULL; |
| 1487 | |
| 1488 | if (name == NULL || datetime == NULL) { |
| 1489 | return -LTTNG_ERR_INVALID; |
| 1490 | } |
| 1491 | |
| 1492 | memset(&lsm, 0, sizeof(lsm)); |
| 1493 | |
| 1494 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
| 1495 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
| 1496 | |
| 1497 | /* There should never be a data URL */ |
| 1498 | size = uri_parse_str_urls(url, NULL, &uris); |
| 1499 | if (size < 0) { |
| 1500 | ret = -LTTNG_ERR_INVALID; |
| 1501 | goto error; |
| 1502 | } |
| 1503 | |
| 1504 | lsm.u.uri.size = size; |
| 1505 | |
| 1506 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) { |
| 1507 | /* Don't append datetime if the name was automatically created. */ |
| 1508 | if (strncmp(name, DEFAULT_SESSION_NAME "-", |
| 1509 | strlen(DEFAULT_SESSION_NAME) + 1)) { |
| 1510 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", |
| 1511 | name, datetime); |
| 1512 | } else { |
| 1513 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name); |
| 1514 | } |
| 1515 | if (ret < 0) { |
| 1516 | PERROR("snprintf uri subdir"); |
| 1517 | ret = -LTTNG_ERR_FATAL; |
| 1518 | goto error; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
| 1523 | sizeof(struct lttng_uri) * size, NULL); |
| 1524 | |
| 1525 | error: |
| 1526 | free(uris); |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * For a given session name, this call checks if the data is ready to be read |
| 1532 | * or is still being extracted by the consumer(s) hence not ready to be used by |
| 1533 | * any readers. |
| 1534 | */ |
| 1535 | int lttng_data_pending(const char *session_name) |
| 1536 | { |
| 1537 | int ret; |
| 1538 | struct lttcomm_session_msg lsm; |
| 1539 | |
| 1540 | if (session_name == NULL) { |
| 1541 | return -LTTNG_ERR_INVALID; |
| 1542 | } |
| 1543 | |
| 1544 | lsm.cmd_type = LTTNG_DATA_PENDING; |
| 1545 | |
| 1546 | lttng_ctl_copy_string(lsm.session.name, session_name, |
| 1547 | sizeof(lsm.session.name)); |
| 1548 | |
| 1549 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
| 1550 | |
| 1551 | /* |
| 1552 | * The lttng_ctl_ask_sessiond function negate the return code if it's not |
| 1553 | * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning |
| 1554 | * that the data is available. Yes it is hackish but for now this is the |
| 1555 | * only way. |
| 1556 | */ |
| 1557 | if (ret == -1) { |
| 1558 | ret = 1; |
| 1559 | } |
| 1560 | |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * Create a session exclusively used for snapshot. |
| 1566 | * |
| 1567 | * Returns LTTNG_OK on success or a negative error code. |
| 1568 | */ |
| 1569 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) |
| 1570 | { |
| 1571 | int ret; |
| 1572 | ssize_t size; |
| 1573 | struct lttcomm_session_msg lsm; |
| 1574 | struct lttng_uri *uris = NULL; |
| 1575 | |
| 1576 | if (name == NULL) { |
| 1577 | return -LTTNG_ERR_INVALID; |
| 1578 | } |
| 1579 | |
| 1580 | memset(&lsm, 0, sizeof(lsm)); |
| 1581 | |
| 1582 | lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT; |
| 1583 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
| 1584 | |
| 1585 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); |
| 1586 | if (size < 0) { |
| 1587 | return -LTTNG_ERR_INVALID; |
| 1588 | } |
| 1589 | |
| 1590 | lsm.u.uri.size = size; |
| 1591 | |
| 1592 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
| 1593 | sizeof(struct lttng_uri) * size, NULL); |
| 1594 | |
| 1595 | free(uris); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Create a session exclusively used for live. |
| 1601 | * |
| 1602 | * Returns LTTNG_OK on success or a negative error code. |
| 1603 | */ |
| 1604 | int lttng_create_session_live(const char *name, const char *url, |
| 1605 | unsigned int timer_interval) |
| 1606 | { |
| 1607 | int ret; |
| 1608 | ssize_t size; |
| 1609 | struct lttcomm_session_msg lsm; |
| 1610 | struct lttng_uri *uris = NULL; |
| 1611 | |
| 1612 | if (name == NULL) { |
| 1613 | return -LTTNG_ERR_INVALID; |
| 1614 | } |
| 1615 | |
| 1616 | memset(&lsm, 0, sizeof(lsm)); |
| 1617 | |
| 1618 | lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE; |
| 1619 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
| 1620 | |
| 1621 | size = uri_parse_str_urls(url, NULL, &uris); |
| 1622 | if (size < 0) { |
| 1623 | ret = -LTTNG_ERR_INVALID; |
| 1624 | goto end; |
| 1625 | } |
| 1626 | |
| 1627 | /* file:// is not accepted for live session. */ |
| 1628 | if (uris[0].dtype == LTTNG_DST_PATH) { |
| 1629 | ret = -LTTNG_ERR_INVALID; |
| 1630 | goto end; |
| 1631 | } |
| 1632 | |
| 1633 | lsm.u.session_live.nb_uri = size; |
| 1634 | lsm.u.session_live.timer_interval = timer_interval; |
| 1635 | |
| 1636 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
| 1637 | sizeof(struct lttng_uri) * size, NULL); |
| 1638 | |
| 1639 | end: |
| 1640 | free(uris); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * lib constructor |
| 1646 | */ |
| 1647 | static void __attribute__((constructor)) init() |
| 1648 | { |
| 1649 | /* Set default session group */ |
| 1650 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
| 1651 | /* Set socket for health check */ |
| 1652 | (void) set_health_socket_path(); |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * lib destructor |
| 1657 | */ |
| 1658 | static void __attribute__((destructor)) lttng_ctl_exit() |
| 1659 | { |
| 1660 | free(tracing_group); |
| 1661 | } |