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