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