| 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 <grp.h> |
| 24 | #include <errno.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <common/common.h> |
| 31 | #include <common/defaults.h> |
| 32 | #include <common/sessiond-comm/sessiond-comm.h> |
| 33 | #include <lttng/lttng.h> |
| 34 | |
| 35 | /* Socket to session daemon for communication */ |
| 36 | static int sessiond_socket; |
| 37 | static char sessiond_sock_path[PATH_MAX]; |
| 38 | |
| 39 | /* Variables */ |
| 40 | static char *tracing_group; |
| 41 | static int connected; |
| 42 | |
| 43 | /* Global */ |
| 44 | |
| 45 | /* |
| 46 | * Those two variables are used by error.h to silent or control the verbosity of |
| 47 | * error message. They are global to the library so application linking with it |
| 48 | * are able to compile correctly and also control verbosity of the library. |
| 49 | * |
| 50 | * Note that it is *not* possible to silent ERR() and PERROR() macros. |
| 51 | */ |
| 52 | int lttng_opt_quiet; |
| 53 | int lttng_opt_verbose; |
| 54 | |
| 55 | /* |
| 56 | * Copy string from src to dst and enforce null terminated byte. |
| 57 | */ |
| 58 | static void copy_string(char *dst, const char *src, size_t len) |
| 59 | { |
| 60 | if (src && dst) { |
| 61 | strncpy(dst, src, len); |
| 62 | /* Enforce the NULL terminated byte */ |
| 63 | dst[len - 1] = '\0'; |
| 64 | } else if (dst) { |
| 65 | dst[0] = '\0'; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Copy domain to lttcomm_session_msg domain. |
| 71 | * |
| 72 | * If domain is unknown, default domain will be the kernel. |
| 73 | */ |
| 74 | static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src) |
| 75 | { |
| 76 | if (src && dst) { |
| 77 | switch (src->type) { |
| 78 | case LTTNG_DOMAIN_KERNEL: |
| 79 | case LTTNG_DOMAIN_UST: |
| 80 | /* |
| 81 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 82 | case LTTNG_DOMAIN_UST_PID: |
| 83 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 84 | */ |
| 85 | memcpy(dst, src, sizeof(struct lttng_domain)); |
| 86 | break; |
| 87 | default: |
| 88 | memset(dst, 0, sizeof(struct lttng_domain)); |
| 89 | dst->type = LTTNG_DOMAIN_KERNEL; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Send lttcomm_session_msg to the session daemon. |
| 97 | * |
| 98 | * On success, returns the number of bytes sent (>=0) |
| 99 | * On error, returns -1 |
| 100 | */ |
| 101 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
| 102 | { |
| 103 | int ret; |
| 104 | |
| 105 | if (!connected) { |
| 106 | ret = -ENOTCONN; |
| 107 | goto end; |
| 108 | } |
| 109 | |
| 110 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
| 111 | sizeof(struct lttcomm_session_msg)); |
| 112 | |
| 113 | end: |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Receive data from the sessiond socket. |
| 119 | * |
| 120 | * On success, returns the number of bytes received (>=0) |
| 121 | * On error, returns -1 (recvmsg() error) or -ENOTCONN |
| 122 | */ |
| 123 | static int recv_data_sessiond(void *buf, size_t len) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | if (!connected) { |
| 128 | ret = -ENOTCONN; |
| 129 | goto end; |
| 130 | } |
| 131 | |
| 132 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
| 133 | |
| 134 | end: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Check if we are in the specified group. |
| 140 | * |
| 141 | * If yes return 1, else return -1. |
| 142 | */ |
| 143 | static int check_tracing_group(const char *grp_name) |
| 144 | { |
| 145 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ |
| 146 | gid_t *grp_list; |
| 147 | int grp_list_size, grp_id, i; |
| 148 | int ret = -1; |
| 149 | |
| 150 | /* Get GID of group 'tracing' */ |
| 151 | grp_tracing = getgrnam(grp_name); |
| 152 | if (!grp_tracing) { |
| 153 | /* If grp_tracing is NULL, the group does not exist. */ |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | /* Get number of supplementary group IDs */ |
| 158 | grp_list_size = getgroups(0, NULL); |
| 159 | if (grp_list_size < 0) { |
| 160 | perror("getgroups"); |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | /* Alloc group list of the right size */ |
| 165 | grp_list = malloc(grp_list_size * sizeof(gid_t)); |
| 166 | if (!grp_list) { |
| 167 | perror("malloc"); |
| 168 | goto end; |
| 169 | } |
| 170 | grp_id = getgroups(grp_list_size, grp_list); |
| 171 | if (grp_id < 0) { |
| 172 | perror("getgroups"); |
| 173 | goto free_list; |
| 174 | } |
| 175 | |
| 176 | for (i = 0; i < grp_list_size; i++) { |
| 177 | if (grp_list[i] == grp_tracing->gr_gid) { |
| 178 | ret = 1; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | free_list: |
| 184 | free(grp_list); |
| 185 | |
| 186 | end: |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Try connect to session daemon with sock_path. |
| 192 | * |
| 193 | * Return 0 on success, else -1 |
| 194 | */ |
| 195 | static int try_connect_sessiond(const char *sock_path) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | /* If socket exist, we check if the daemon listens for connect. */ |
| 200 | ret = access(sock_path, F_OK); |
| 201 | if (ret < 0) { |
| 202 | /* Not alive */ |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | ret = lttcomm_connect_unix_sock(sock_path); |
| 207 | if (ret < 0) { |
| 208 | /* Not alive */ |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | ret = lttcomm_close_unix_sock(ret); |
| 213 | if (ret < 0) { |
| 214 | perror("lttcomm_close_unix_sock"); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Set sessiond socket path by putting it in the global |
| 222 | * sessiond_sock_path variable. |
| 223 | * Returns 0 on success, |
| 224 | * -ENOMEM on failure (the sessiond socket path is somehow too long) |
| 225 | */ |
| 226 | static int set_session_daemon_path(void) |
| 227 | { |
| 228 | int ret; |
| 229 | int in_tgroup = 0; /* In tracing group */ |
| 230 | uid_t uid; |
| 231 | |
| 232 | uid = getuid(); |
| 233 | |
| 234 | if (uid != 0) { |
| 235 | /* Are we in the tracing group ? */ |
| 236 | in_tgroup = check_tracing_group(tracing_group); |
| 237 | } |
| 238 | |
| 239 | if ((uid == 0) || in_tgroup) { |
| 240 | copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, |
| 241 | sizeof(sessiond_sock_path)); |
| 242 | } |
| 243 | |
| 244 | if (uid != 0) { |
| 245 | if (in_tgroup) { |
| 246 | /* Tracing group */ |
| 247 | ret = try_connect_sessiond(sessiond_sock_path); |
| 248 | if (ret >= 0) { |
| 249 | goto end; |
| 250 | } |
| 251 | /* Global session daemon not available... */ |
| 252 | } |
| 253 | /* ...or not in tracing group (and not root), default */ |
| 254 | |
| 255 | /* |
| 256 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; |
| 257 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) |
| 258 | */ |
| 259 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), |
| 260 | DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME")); |
| 261 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | } |
| 265 | end: |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Connect to the LTTng session daemon. |
| 271 | * |
| 272 | * On success, return 0. On error, return -1. |
| 273 | */ |
| 274 | static int connect_sessiond(void) |
| 275 | { |
| 276 | int ret; |
| 277 | |
| 278 | ret = set_session_daemon_path(); |
| 279 | if (ret < 0) { |
| 280 | return -1; /* set_session_daemon_path() returns -ENOMEM */ |
| 281 | } |
| 282 | |
| 283 | /* Connect to the sesssion daemon */ |
| 284 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
| 285 | if (ret < 0) { |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | sessiond_socket = ret; |
| 290 | connected = 1; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Clean disconnect from the session daemon. |
| 297 | * On success, return 0. On error, return -1. |
| 298 | */ |
| 299 | static int disconnect_sessiond(void) |
| 300 | { |
| 301 | int ret = 0; |
| 302 | |
| 303 | if (connected) { |
| 304 | ret = lttcomm_close_unix_sock(sessiond_socket); |
| 305 | sessiond_socket = 0; |
| 306 | connected = 0; |
| 307 | } |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Ask the session daemon a specific command and put the data into buf. |
| 314 | * |
| 315 | * Return size of data (only payload, not header) or a negative error code. |
| 316 | */ |
| 317 | static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) |
| 318 | { |
| 319 | int ret; |
| 320 | size_t size; |
| 321 | void *data = NULL; |
| 322 | struct lttcomm_lttng_msg llm; |
| 323 | |
| 324 | ret = connect_sessiond(); |
| 325 | if (ret < 0) { |
| 326 | goto end; |
| 327 | } |
| 328 | |
| 329 | /* Send command to session daemon */ |
| 330 | ret = send_session_msg(lsm); |
| 331 | if (ret < 0) { |
| 332 | goto end; |
| 333 | } |
| 334 | |
| 335 | /* Get header from data transmission */ |
| 336 | ret = recv_data_sessiond(&llm, sizeof(llm)); |
| 337 | if (ret < 0) { |
| 338 | goto end; |
| 339 | } |
| 340 | |
| 341 | /* Check error code if OK */ |
| 342 | if (llm.ret_code != LTTCOMM_OK) { |
| 343 | ret = -llm.ret_code; |
| 344 | goto end; |
| 345 | } |
| 346 | |
| 347 | size = llm.data_size; |
| 348 | if (size == 0) { |
| 349 | /* If client free with size 0 */ |
| 350 | if (buf != NULL) { |
| 351 | *buf = NULL; |
| 352 | } |
| 353 | ret = 0; |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | data = (void*) malloc(size); |
| 358 | |
| 359 | /* Get payload data */ |
| 360 | ret = recv_data_sessiond(data, size); |
| 361 | if (ret < 0) { |
| 362 | free(data); |
| 363 | goto end; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Extra protection not to dereference a NULL pointer. If buf is NULL at |
| 368 | * this point, an error is returned and data is freed. |
| 369 | */ |
| 370 | if (buf == NULL) { |
| 371 | ret = -1; |
| 372 | free(data); |
| 373 | goto end; |
| 374 | } |
| 375 | |
| 376 | *buf = data; |
| 377 | ret = size; |
| 378 | |
| 379 | end: |
| 380 | disconnect_sessiond(); |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Create lttng handle and return pointer. |
| 386 | * The returned pointer will be NULL in case of malloc() error. |
| 387 | */ |
| 388 | struct lttng_handle *lttng_create_handle(const char *session_name, |
| 389 | struct lttng_domain *domain) |
| 390 | { |
| 391 | struct lttng_handle *handle; |
| 392 | |
| 393 | handle = malloc(sizeof(struct lttng_handle)); |
| 394 | if (handle == NULL) { |
| 395 | perror("malloc handle"); |
| 396 | goto end; |
| 397 | } |
| 398 | |
| 399 | /* Copy session name */ |
| 400 | copy_string(handle->session_name, session_name, |
| 401 | sizeof(handle->session_name)); |
| 402 | |
| 403 | /* Copy lttng domain */ |
| 404 | copy_lttng_domain(&handle->domain, domain); |
| 405 | |
| 406 | end: |
| 407 | return handle; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Destroy handle by free(3) the pointer. |
| 412 | */ |
| 413 | void lttng_destroy_handle(struct lttng_handle *handle) |
| 414 | { |
| 415 | if (handle) { |
| 416 | free(handle); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Register an outside consumer. |
| 422 | * Returns size of returned session payload data or a negative error code. |
| 423 | */ |
| 424 | int lttng_register_consumer(struct lttng_handle *handle, |
| 425 | const char *socket_path) |
| 426 | { |
| 427 | struct lttcomm_session_msg lsm; |
| 428 | |
| 429 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
| 430 | copy_string(lsm.session.name, handle->session_name, |
| 431 | sizeof(lsm.session.name)); |
| 432 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 433 | |
| 434 | copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); |
| 435 | |
| 436 | return ask_sessiond(&lsm, NULL); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Start tracing for all traces of the session. |
| 441 | * Returns size of returned session payload data or a negative error code. |
| 442 | */ |
| 443 | int lttng_start_tracing(const char *session_name) |
| 444 | { |
| 445 | struct lttcomm_session_msg lsm; |
| 446 | |
| 447 | if (session_name == NULL) { |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | lsm.cmd_type = LTTNG_START_TRACE; |
| 452 | |
| 453 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
| 454 | |
| 455 | return ask_sessiond(&lsm, NULL); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Stop tracing for all traces of the session. |
| 460 | * Returns size of returned session payload data or a negative error code. |
| 461 | */ |
| 462 | int lttng_stop_tracing(const char *session_name) |
| 463 | { |
| 464 | struct lttcomm_session_msg lsm; |
| 465 | |
| 466 | if (session_name == NULL) { |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | lsm.cmd_type = LTTNG_STOP_TRACE; |
| 471 | |
| 472 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
| 473 | |
| 474 | return ask_sessiond(&lsm, NULL); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Add context to event and/or channel. |
| 479 | * If event_name is NULL, the context is applied to all events of the channel. |
| 480 | * If channel_name is NULL, a lookup of the event's channel is done. |
| 481 | * If both are NULL, the context is applied to all events of all channels. |
| 482 | * |
| 483 | * Returns the size of the returned payload data or a negative error code. |
| 484 | */ |
| 485 | int lttng_add_context(struct lttng_handle *handle, |
| 486 | struct lttng_event_context *ctx, const char *event_name, |
| 487 | const char *channel_name) |
| 488 | { |
| 489 | struct lttcomm_session_msg lsm; |
| 490 | |
| 491 | /* Safety check. Both are mandatory */ |
| 492 | if (handle == NULL || ctx == NULL) { |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | memset(&lsm, 0, sizeof(lsm)); |
| 497 | |
| 498 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
| 499 | |
| 500 | /* Copy channel name */ |
| 501 | copy_string(lsm.u.context.channel_name, channel_name, |
| 502 | sizeof(lsm.u.context.channel_name)); |
| 503 | /* Copy event name */ |
| 504 | copy_string(lsm.u.context.event_name, event_name, |
| 505 | sizeof(lsm.u.context.event_name)); |
| 506 | |
| 507 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 508 | |
| 509 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
| 510 | |
| 511 | copy_string(lsm.session.name, handle->session_name, |
| 512 | sizeof(lsm.session.name)); |
| 513 | |
| 514 | return ask_sessiond(&lsm, NULL); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Enable event(s) for a channel. |
| 519 | * If no event name is specified, all events are enabled. |
| 520 | * If no channel name is specified, the default 'channel0' is used. |
| 521 | * Returns size of returned session payload data or a negative error code. |
| 522 | */ |
| 523 | int lttng_enable_event(struct lttng_handle *handle, |
| 524 | struct lttng_event *ev, const char *channel_name) |
| 525 | { |
| 526 | struct lttcomm_session_msg lsm; |
| 527 | |
| 528 | if (handle == NULL || ev == NULL) { |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | memset(&lsm, 0, sizeof(lsm)); |
| 533 | |
| 534 | /* If no channel name, we put the default name */ |
| 535 | if (channel_name == NULL) { |
| 536 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
| 537 | sizeof(lsm.u.enable.channel_name)); |
| 538 | } else { |
| 539 | copy_string(lsm.u.enable.channel_name, channel_name, |
| 540 | sizeof(lsm.u.enable.channel_name)); |
| 541 | } |
| 542 | |
| 543 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 544 | |
| 545 | if (ev->name[0] != '\0') { |
| 546 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
| 547 | } else { |
| 548 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
| 549 | } |
| 550 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
| 551 | |
| 552 | copy_string(lsm.session.name, handle->session_name, |
| 553 | sizeof(lsm.session.name)); |
| 554 | |
| 555 | return ask_sessiond(&lsm, NULL); |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Disable event(s) of a channel and domain. |
| 560 | * If no event name is specified, all events are disabled. |
| 561 | * If no channel name is specified, the default 'channel0' is used. |
| 562 | * Returns size of returned session payload data or a negative error code. |
| 563 | */ |
| 564 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
| 565 | const char *channel_name) |
| 566 | { |
| 567 | struct lttcomm_session_msg lsm; |
| 568 | |
| 569 | if (handle == NULL) { |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | memset(&lsm, 0, sizeof(lsm)); |
| 574 | |
| 575 | if (channel_name) { |
| 576 | copy_string(lsm.u.disable.channel_name, channel_name, |
| 577 | sizeof(lsm.u.disable.channel_name)); |
| 578 | } else { |
| 579 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
| 580 | sizeof(lsm.u.disable.channel_name)); |
| 581 | } |
| 582 | |
| 583 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 584 | |
| 585 | if (name != NULL) { |
| 586 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
| 587 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
| 588 | } else { |
| 589 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
| 590 | } |
| 591 | |
| 592 | copy_string(lsm.session.name, handle->session_name, |
| 593 | sizeof(lsm.session.name)); |
| 594 | |
| 595 | return ask_sessiond(&lsm, NULL); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Enable channel per domain |
| 600 | * Returns size of returned session payload data or a negative error code. |
| 601 | */ |
| 602 | int lttng_enable_channel(struct lttng_handle *handle, |
| 603 | struct lttng_channel *chan) |
| 604 | { |
| 605 | struct lttcomm_session_msg lsm; |
| 606 | |
| 607 | /* |
| 608 | * NULL arguments are forbidden. No default values. |
| 609 | */ |
| 610 | if (handle == NULL || chan == NULL) { |
| 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | memset(&lsm, 0, sizeof(lsm)); |
| 615 | |
| 616 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
| 617 | |
| 618 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
| 619 | |
| 620 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 621 | |
| 622 | copy_string(lsm.session.name, handle->session_name, |
| 623 | sizeof(lsm.session.name)); |
| 624 | |
| 625 | return ask_sessiond(&lsm, NULL); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * All tracing will be stopped for registered events of the channel. |
| 630 | * Returns size of returned session payload data or a negative error code. |
| 631 | */ |
| 632 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
| 633 | { |
| 634 | struct lttcomm_session_msg lsm; |
| 635 | |
| 636 | /* Safety check. Both are mandatory */ |
| 637 | if (handle == NULL || name == NULL) { |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | memset(&lsm, 0, sizeof(lsm)); |
| 642 | |
| 643 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
| 644 | |
| 645 | copy_string(lsm.u.disable.channel_name, name, |
| 646 | sizeof(lsm.u.disable.channel_name)); |
| 647 | |
| 648 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 649 | |
| 650 | copy_string(lsm.session.name, handle->session_name, |
| 651 | sizeof(lsm.session.name)); |
| 652 | |
| 653 | return ask_sessiond(&lsm, NULL); |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Lists all available tracepoints of domain. |
| 658 | * Sets the contents of the events array. |
| 659 | * Returns the number of lttng_event entries in events; |
| 660 | * on error, returns a negative value. |
| 661 | */ |
| 662 | int lttng_list_tracepoints(struct lttng_handle *handle, |
| 663 | struct lttng_event **events) |
| 664 | { |
| 665 | int ret; |
| 666 | struct lttcomm_session_msg lsm; |
| 667 | |
| 668 | if (handle == NULL) { |
| 669 | return -1; |
| 670 | } |
| 671 | |
| 672 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
| 673 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 674 | |
| 675 | ret = ask_sessiond(&lsm, (void **) events); |
| 676 | if (ret < 0) { |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | return ret / sizeof(struct lttng_event); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Lists all available tracepoint fields of domain. |
| 685 | * Sets the contents of the event field array. |
| 686 | * Returns the number of lttng_event_field entries in events; |
| 687 | * on error, returns a negative value. |
| 688 | */ |
| 689 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, |
| 690 | struct lttng_event_field **fields) |
| 691 | { |
| 692 | int ret; |
| 693 | struct lttcomm_session_msg lsm; |
| 694 | |
| 695 | if (handle == NULL) { |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
| 700 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 701 | |
| 702 | ret = ask_sessiond(&lsm, (void **) fields); |
| 703 | if (ret < 0) { |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | return ret / sizeof(struct lttng_event_field); |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Returns a human readable string describing |
| 712 | * the error code (a negative value). |
| 713 | */ |
| 714 | const char *lttng_strerror(int code) |
| 715 | { |
| 716 | /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */ |
| 717 | if (code > -LTTCOMM_OK) { |
| 718 | return "Ended with errors"; |
| 719 | } |
| 720 | |
| 721 | return lttcomm_get_readable_code(code); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Create a brand new session using name and path. |
| 726 | * Returns size of returned session payload data or a negative error code. |
| 727 | */ |
| 728 | int lttng_create_session(const char *name, const char *path) |
| 729 | { |
| 730 | struct lttcomm_session_msg lsm; |
| 731 | |
| 732 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
| 733 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
| 734 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); |
| 735 | |
| 736 | return ask_sessiond(&lsm, NULL); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Destroy session using name. |
| 741 | * Returns size of returned session payload data or a negative error code. |
| 742 | */ |
| 743 | int lttng_destroy_session(const char *session_name) |
| 744 | { |
| 745 | struct lttcomm_session_msg lsm; |
| 746 | |
| 747 | if (session_name == NULL) { |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | lsm.cmd_type = LTTNG_DESTROY_SESSION; |
| 752 | |
| 753 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
| 754 | |
| 755 | return ask_sessiond(&lsm, NULL); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Ask the session daemon for all available sessions. |
| 760 | * Sets the contents of the sessions array. |
| 761 | * Returns the number of lttng_session entries in sessions; |
| 762 | * on error, returns a negative value. |
| 763 | */ |
| 764 | int lttng_list_sessions(struct lttng_session **sessions) |
| 765 | { |
| 766 | int ret; |
| 767 | struct lttcomm_session_msg lsm; |
| 768 | |
| 769 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
| 770 | ret = ask_sessiond(&lsm, (void**) sessions); |
| 771 | if (ret < 0) { |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | return ret / sizeof(struct lttng_session); |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Ask the session daemon for all available domains of a session. |
| 780 | * Sets the contents of the domains array. |
| 781 | * Returns the number of lttng_domain entries in domains; |
| 782 | * on error, returns a negative value. |
| 783 | */ |
| 784 | int lttng_list_domains(const char *session_name, |
| 785 | struct lttng_domain **domains) |
| 786 | { |
| 787 | int ret; |
| 788 | struct lttcomm_session_msg lsm; |
| 789 | |
| 790 | if (session_name == NULL) { |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
| 795 | |
| 796 | copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); |
| 797 | |
| 798 | ret = ask_sessiond(&lsm, (void**) domains); |
| 799 | if (ret < 0) { |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | return ret / sizeof(struct lttng_domain); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Ask the session daemon for all available channels of a session. |
| 808 | * Sets the contents of the channels array. |
| 809 | * Returns the number of lttng_channel entries in channels; |
| 810 | * on error, returns a negative value. |
| 811 | */ |
| 812 | int lttng_list_channels(struct lttng_handle *handle, |
| 813 | struct lttng_channel **channels) |
| 814 | { |
| 815 | int ret; |
| 816 | struct lttcomm_session_msg lsm; |
| 817 | |
| 818 | if (handle == NULL) { |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
| 823 | copy_string(lsm.session.name, handle->session_name, |
| 824 | sizeof(lsm.session.name)); |
| 825 | |
| 826 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 827 | |
| 828 | ret = ask_sessiond(&lsm, (void**) channels); |
| 829 | if (ret < 0) { |
| 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | return ret / sizeof(struct lttng_channel); |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Ask the session daemon for all available events of a session channel. |
| 838 | * Sets the contents of the events array. |
| 839 | * Returns the number of lttng_event entries in events; |
| 840 | * on error, returns a negative value. |
| 841 | */ |
| 842 | int lttng_list_events(struct lttng_handle *handle, |
| 843 | const char *channel_name, struct lttng_event **events) |
| 844 | { |
| 845 | int ret; |
| 846 | struct lttcomm_session_msg lsm; |
| 847 | |
| 848 | /* Safety check. An handle and channel name are mandatory */ |
| 849 | if (handle == NULL || channel_name == NULL) { |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
| 854 | copy_string(lsm.session.name, handle->session_name, |
| 855 | sizeof(lsm.session.name)); |
| 856 | copy_string(lsm.u.list.channel_name, channel_name, |
| 857 | sizeof(lsm.u.list.channel_name)); |
| 858 | |
| 859 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 860 | |
| 861 | ret = ask_sessiond(&lsm, (void**) events); |
| 862 | if (ret < 0) { |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | return ret / sizeof(struct lttng_event); |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Sets the tracing_group variable with name. |
| 871 | * This function allocates memory pointed to by tracing_group. |
| 872 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. |
| 873 | */ |
| 874 | int lttng_set_tracing_group(const char *name) |
| 875 | { |
| 876 | if (name == NULL) { |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | if (asprintf(&tracing_group, "%s", name) < 0) { |
| 881 | return -ENOMEM; |
| 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Returns size of returned session payload data or a negative error code. |
| 889 | */ |
| 890 | int lttng_calibrate(struct lttng_handle *handle, |
| 891 | struct lttng_calibrate *calibrate) |
| 892 | { |
| 893 | struct lttcomm_session_msg lsm; |
| 894 | |
| 895 | /* Safety check. NULL pointer are forbidden */ |
| 896 | if (handle == NULL || calibrate == NULL) { |
| 897 | return -1; |
| 898 | } |
| 899 | |
| 900 | lsm.cmd_type = LTTNG_CALIBRATE; |
| 901 | copy_lttng_domain(&lsm.domain, &handle->domain); |
| 902 | |
| 903 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
| 904 | |
| 905 | return ask_sessiond(&lsm, NULL); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Set default channel attributes. |
| 910 | * If either or both of the arguments are null, attr content is zeroe'd. |
| 911 | */ |
| 912 | void lttng_channel_set_default_attr(struct lttng_domain *domain, |
| 913 | struct lttng_channel_attr *attr) |
| 914 | { |
| 915 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
| 916 | |
| 917 | /* Safety check */ |
| 918 | if (attr == NULL || domain == NULL) { |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | switch (domain->type) { |
| 923 | case LTTNG_DOMAIN_KERNEL: |
| 924 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 925 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 926 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 927 | |
| 928 | attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; |
| 929 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 930 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 931 | break; |
| 932 | case LTTNG_DOMAIN_UST: |
| 933 | #if 0 |
| 934 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 935 | case LTTNG_DOMAIN_UST_PID: |
| 936 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 937 | #endif |
| 938 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 939 | attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 940 | attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 941 | |
| 942 | attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; |
| 943 | attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; |
| 944 | attr->output = DEFAULT_UST_CHANNEL_OUTPUT; |
| 945 | break; |
| 946 | default: |
| 947 | /* Default behavior: leave set to 0. */ |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Check if session daemon is alive. |
| 954 | * |
| 955 | * Return 1 if alive or 0 if not. |
| 956 | * On error returns a negative value. |
| 957 | */ |
| 958 | int lttng_session_daemon_alive(void) |
| 959 | { |
| 960 | int ret; |
| 961 | |
| 962 | ret = set_session_daemon_path(); |
| 963 | if (ret < 0) { |
| 964 | /* Error */ |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | if (strlen(sessiond_sock_path) == 0) { |
| 969 | /* No socket path set. Weird error */ |
| 970 | return -1; |
| 971 | } |
| 972 | |
| 973 | ret = try_connect_sessiond(sessiond_sock_path); |
| 974 | if (ret < 0) { |
| 975 | /* Not alive */ |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | /* Is alive */ |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * lib constructor |
| 985 | */ |
| 986 | static void __attribute__((constructor)) init() |
| 987 | { |
| 988 | /* Set default session group */ |
| 989 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
| 990 | } |