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