4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/lttng.h>
37 #include <lttng/health-internal.h>
39 #include "filter/filter-ast.h"
40 #include "filter/filter-parser.h"
41 #include "filter/filter-bytecode.h"
42 #include "filter/memstream.h"
43 #include "lttng-ctl-helper.h"
46 static const int print_xml
= 1;
47 #define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
50 static const int print_xml
= 0;
51 #define dbg_printf(fmt, args...) \
53 /* do nothing but check printf format */ \
55 printf("[debug liblttnctl] " fmt, ## args); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
;
62 static char sessiond_sock_path
[PATH_MAX
];
63 static char health_sock_path
[PATH_MAX
];
66 static char *tracing_group
;
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
77 int lttng_opt_verbose
;
80 * Copy string from src to dst and enforce null terminated byte.
83 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
86 strncpy(dst
, src
, len
);
87 /* Enforce the NULL terminated byte */
95 * Copy domain to lttcomm_session_msg domain.
97 * If domain is unknown, default domain will be the kernel.
100 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
101 struct lttng_domain
*src
)
105 case LTTNG_DOMAIN_KERNEL
:
106 case LTTNG_DOMAIN_UST
:
107 memcpy(dst
, src
, sizeof(struct lttng_domain
));
110 memset(dst
, 0, sizeof(struct lttng_domain
));
117 * Send lttcomm_session_msg to the session daemon.
119 * On success, returns the number of bytes sent (>=0)
120 * On error, returns -1
122 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
127 ret
= -LTTNG_ERR_NO_SESSIOND
;
131 DBG("LSM cmd type : %d", lsm
->cmd_type
);
133 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
134 sizeof(struct lttcomm_session_msg
));
136 ret
= -LTTNG_ERR_FATAL
;
144 * Send var len data to the session daemon.
146 * On success, returns the number of bytes sent (>=0)
147 * On error, returns -1
149 static int send_session_varlen(void *data
, size_t len
)
154 ret
= -LTTNG_ERR_NO_SESSIOND
;
163 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
165 ret
= -LTTNG_ERR_FATAL
;
173 * Receive data from the sessiond socket.
175 * On success, returns the number of bytes received (>=0)
176 * On error, returns -1 (recvmsg() error) or -ENOTCONN
178 static int recv_data_sessiond(void *buf
, size_t len
)
183 ret
= -LTTNG_ERR_NO_SESSIOND
;
187 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
189 ret
= -LTTNG_ERR_FATAL
;
197 * Check if we are in the specified group.
199 * If yes return 1, else return -1.
201 static int check_tracing_group(const char *grp_name
)
203 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
205 int grp_list_size
, grp_id
, i
;
208 /* Get GID of group 'tracing' */
209 grp_tracing
= getgrnam(grp_name
);
211 /* If grp_tracing is NULL, the group does not exist. */
215 /* Get number of supplementary group IDs */
216 grp_list_size
= getgroups(0, NULL
);
217 if (grp_list_size
< 0) {
222 /* Alloc group list of the right size */
223 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
228 grp_id
= getgroups(grp_list_size
, grp_list
);
234 for (i
= 0; i
< grp_list_size
; i
++) {
235 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
249 * Try connect to session daemon with sock_path.
251 * Return 0 on success, else -1
253 static int try_connect_sessiond(const char *sock_path
)
257 /* If socket exist, we check if the daemon listens for connect. */
258 ret
= access(sock_path
, F_OK
);
264 ret
= lttcomm_connect_unix_sock(sock_path
);
270 ret
= lttcomm_close_unix_sock(ret
);
272 perror("lttcomm_close_unix_sock");
282 * Set sessiond socket path by putting it in the global sessiond_sock_path
285 * Returns 0 on success, negative value on failure (the sessiond socket path
286 * is somehow too long or ENOMEM).
288 static int set_session_daemon_path(void)
290 int in_tgroup
= 0; /* In tracing group */
296 /* Are we in the tracing group ? */
297 in_tgroup
= check_tracing_group(tracing_group
);
300 if ((uid
== 0) || in_tgroup
) {
301 lttng_ctl_copy_string(sessiond_sock_path
,
302 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
310 ret
= try_connect_sessiond(sessiond_sock_path
);
314 /* Global session daemon not available... */
316 /* ...or not in tracing group (and not root), default */
319 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
320 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
322 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
323 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
324 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
336 * Connect to the LTTng session daemon.
338 * On success, return 0. On error, return -1.
340 static int connect_sessiond(void)
344 /* Don't try to connect if already connected. */
349 ret
= set_session_daemon_path();
354 /* Connect to the sesssion daemon */
355 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
360 sessiond_socket
= ret
;
370 * Clean disconnect from the session daemon.
371 * On success, return 0. On error, return -1.
373 static int disconnect_sessiond(void)
378 ret
= lttcomm_close_unix_sock(sessiond_socket
);
387 * Ask the session daemon a specific command and put the data into buf.
388 * Takes extra var. len. data as input to send to the session daemon.
390 * Return size of data (only payload, not header) or a negative error code.
393 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
394 void *vardata
, size_t varlen
, void **buf
)
399 struct lttcomm_lttng_msg llm
;
401 ret
= connect_sessiond();
403 ret
= -LTTNG_ERR_NO_SESSIOND
;
407 /* Send command to session daemon */
408 ret
= send_session_msg(lsm
);
410 /* Ret value is a valid lttng error code. */
413 /* Send var len data */
414 ret
= send_session_varlen(vardata
, varlen
);
416 /* Ret value is a valid lttng error code. */
420 /* Get header from data transmission */
421 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
423 /* Ret value is a valid lttng error code. */
427 /* Check error code if OK */
428 if (llm
.ret_code
!= LTTNG_OK
) {
433 size
= llm
.data_size
;
435 /* If client free with size 0 */
443 data
= (void*) malloc(size
);
445 /* Get payload data */
446 ret
= recv_data_sessiond(data
, size
);
453 * Extra protection not to dereference a NULL pointer. If buf is NULL at
454 * this point, an error is returned and data is freed.
457 ret
= -LTTNG_ERR_INVALID
;
466 disconnect_sessiond();
471 * Create lttng handle and return pointer.
472 * The returned pointer will be NULL in case of malloc() error.
474 struct lttng_handle
*lttng_create_handle(const char *session_name
,
475 struct lttng_domain
*domain
)
477 struct lttng_handle
*handle
= NULL
;
479 if (domain
== NULL
) {
483 handle
= malloc(sizeof(struct lttng_handle
));
484 if (handle
== NULL
) {
485 PERROR("malloc handle");
489 /* Copy session name */
490 lttng_ctl_copy_string(handle
->session_name
, session_name
,
491 sizeof(handle
->session_name
));
493 /* Copy lttng domain */
494 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
501 * Destroy handle by free(3) the pointer.
503 void lttng_destroy_handle(struct lttng_handle
*handle
)
509 * Register an outside consumer.
510 * Returns size of returned session payload data or a negative error code.
512 int lttng_register_consumer(struct lttng_handle
*handle
,
513 const char *socket_path
)
515 struct lttcomm_session_msg lsm
;
517 if (handle
== NULL
|| socket_path
== NULL
) {
518 return -LTTNG_ERR_INVALID
;
521 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
522 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
523 sizeof(lsm
.session
.name
));
524 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
526 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
528 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
532 * Start tracing for all traces of the session.
533 * Returns size of returned session payload data or a negative error code.
535 int lttng_start_tracing(const char *session_name
)
537 struct lttcomm_session_msg lsm
;
539 if (session_name
== NULL
) {
540 return -LTTNG_ERR_INVALID
;
543 lsm
.cmd_type
= LTTNG_START_TRACE
;
545 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
546 sizeof(lsm
.session
.name
));
548 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
552 * Stop tracing for all traces of the session.
554 static int _lttng_stop_tracing(const char *session_name
, int wait
)
557 struct lttcomm_session_msg lsm
;
559 if (session_name
== NULL
) {
560 return -LTTNG_ERR_INVALID
;
563 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
565 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
566 sizeof(lsm
.session
.name
));
568 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
569 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
577 _MSG("Waiting for data availability");
580 /* Check for data availability */
582 data_ret
= lttng_data_pending(session_name
);
584 /* Return the data available call error. */
590 * Data sleep time before retrying (in usec). Don't sleep if the call
591 * returned value indicates availability.
594 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
598 } while (data_ret
!= 0);
608 * Stop tracing and wait for data availability.
610 int lttng_stop_tracing(const char *session_name
)
612 return _lttng_stop_tracing(session_name
, 1);
616 * Stop tracing but _don't_ wait for data availability.
618 int lttng_stop_tracing_no_wait(const char *session_name
)
620 return _lttng_stop_tracing(session_name
, 0);
624 * Add context to a channel.
626 * If the given channel is NULL, add the contexts to all channels.
627 * The event_name param is ignored.
629 * Returns the size of the returned payload data or a negative error code.
631 int lttng_add_context(struct lttng_handle
*handle
,
632 struct lttng_event_context
*ctx
, const char *event_name
,
633 const char *channel_name
)
635 struct lttcomm_session_msg lsm
;
637 /* Safety check. Both are mandatory */
638 if (handle
== NULL
|| ctx
== NULL
) {
639 return -LTTNG_ERR_INVALID
;
642 memset(&lsm
, 0, sizeof(lsm
));
644 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
646 /* If no channel name, send empty string. */
647 if (channel_name
== NULL
) {
648 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
649 sizeof(lsm
.u
.context
.channel_name
));
651 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
652 sizeof(lsm
.u
.context
.channel_name
));
655 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
657 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
659 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
660 sizeof(lsm
.session
.name
));
662 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
666 * Enable event(s) for a channel.
667 * If no event name is specified, all events are enabled.
668 * If no channel name is specified, the default 'channel0' is used.
669 * Returns size of returned session payload data or a negative error code.
671 int lttng_enable_event(struct lttng_handle
*handle
,
672 struct lttng_event
*ev
, const char *channel_name
)
674 struct lttcomm_session_msg lsm
;
676 if (handle
== NULL
|| ev
== NULL
) {
677 return -LTTNG_ERR_INVALID
;
680 memset(&lsm
, 0, sizeof(lsm
));
682 /* If no channel name, send empty string. */
683 if (channel_name
== NULL
) {
684 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
685 sizeof(lsm
.u
.enable
.channel_name
));
687 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
688 sizeof(lsm
.u
.enable
.channel_name
));
691 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
693 if (ev
->name
[0] != '\0') {
694 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
696 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
698 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
700 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
701 sizeof(lsm
.session
.name
));
703 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
707 * Create or enable an event with a filter expression.
709 * Return negative error value on error.
710 * Return size of returned session payload data if OK.
712 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
713 struct lttng_event
*event
, const char *channel_name
,
714 const char *filter_expression
)
716 struct lttcomm_session_msg lsm
;
717 struct filter_parser_ctx
*ctx
;
721 if (!filter_expression
) {
723 * Fall back to normal event enabling if no filter
726 return lttng_enable_event(handle
, event
, channel_name
);
730 * Empty filter string will always be rejected by the parser
731 * anyway, so treat this corner-case early to eliminate
732 * lttng_fmemopen error for 0-byte allocation.
734 if (handle
== NULL
|| filter_expression
[0] == '\0') {
735 return -LTTNG_ERR_INVALID
;
739 * casting const to non-const, as the underlying function will
740 * use it in read-only mode.
742 fmem
= lttng_fmemopen((void *) filter_expression
,
743 strlen(filter_expression
), "r");
745 fprintf(stderr
, "Error opening memory as stream\n");
746 return -LTTNG_ERR_FILTER_NOMEM
;
748 ctx
= filter_parser_ctx_alloc(fmem
);
750 fprintf(stderr
, "Error allocating parser\n");
751 ret
= -LTTNG_ERR_FILTER_NOMEM
;
754 ret
= filter_parser_ctx_append_ast(ctx
);
756 fprintf(stderr
, "Parse error\n");
757 ret
= -LTTNG_ERR_FILTER_INVAL
;
760 ret
= filter_visitor_set_parent(ctx
);
762 fprintf(stderr
, "Set parent error\n");
763 ret
= -LTTNG_ERR_FILTER_INVAL
;
767 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
770 fprintf(stderr
, "XML print error\n");
771 ret
= -LTTNG_ERR_FILTER_INVAL
;
776 dbg_printf("Generating IR... ");
778 ret
= filter_visitor_ir_generate(ctx
);
780 fprintf(stderr
, "Generate IR error\n");
781 ret
= -LTTNG_ERR_FILTER_INVAL
;
784 dbg_printf("done\n");
786 dbg_printf("Validating IR... ");
788 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
790 ret
= -LTTNG_ERR_FILTER_INVAL
;
793 dbg_printf("done\n");
795 dbg_printf("Generating bytecode... ");
797 ret
= filter_visitor_bytecode_generate(ctx
);
799 fprintf(stderr
, "Generate bytecode error\n");
800 ret
= -LTTNG_ERR_FILTER_INVAL
;
803 dbg_printf("done\n");
804 dbg_printf("Size of bytecode generated: %u bytes.\n",
805 bytecode_get_len(&ctx
->bytecode
->b
));
807 memset(&lsm
, 0, sizeof(lsm
));
809 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
811 /* If no channel name, send empty string. */
812 if (channel_name
== NULL
) {
813 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
814 sizeof(lsm
.u
.enable
.channel_name
));
816 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
817 sizeof(lsm
.u
.enable
.channel_name
));
820 /* Copy event name */
822 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
825 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
826 + bytecode_get_len(&ctx
->bytecode
->b
);
828 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
830 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
831 sizeof(lsm
.session
.name
));
833 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
834 lsm
.u
.enable
.bytecode_len
, NULL
);
836 filter_bytecode_free(ctx
);
838 filter_parser_ctx_free(ctx
);
839 if (fclose(fmem
) != 0) {
845 filter_bytecode_free(ctx
);
847 filter_parser_ctx_free(ctx
);
849 if (fclose(fmem
) != 0) {
856 * Disable event(s) of a channel and domain.
857 * If no event name is specified, all events are disabled.
858 * If no channel name is specified, the default 'channel0' is used.
859 * Returns size of returned session payload data or a negative error code.
861 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
862 const char *channel_name
)
864 struct lttcomm_session_msg lsm
;
866 if (handle
== NULL
) {
867 return -LTTNG_ERR_INVALID
;
870 memset(&lsm
, 0, sizeof(lsm
));
872 /* If no channel name, send empty string. */
873 if (channel_name
== NULL
) {
874 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
875 sizeof(lsm
.u
.disable
.channel_name
));
877 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
878 sizeof(lsm
.u
.disable
.channel_name
));
881 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
884 lttng_ctl_copy_string(lsm
.u
.disable
.name
, name
,
885 sizeof(lsm
.u
.disable
.name
));
886 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
888 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
891 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
892 sizeof(lsm
.session
.name
));
894 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
898 * Enable channel per domain
899 * Returns size of returned session payload data or a negative error code.
901 int lttng_enable_channel(struct lttng_handle
*handle
,
902 struct lttng_channel
*chan
)
904 struct lttcomm_session_msg lsm
;
907 * NULL arguments are forbidden. No default values.
909 if (handle
== NULL
|| chan
== NULL
) {
910 return -LTTNG_ERR_INVALID
;
913 memset(&lsm
, 0, sizeof(lsm
));
915 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
917 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
919 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
921 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
922 sizeof(lsm
.session
.name
));
924 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
928 * All tracing will be stopped for registered events of the channel.
929 * Returns size of returned session payload data or a negative error code.
931 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
933 struct lttcomm_session_msg lsm
;
935 /* Safety check. Both are mandatory */
936 if (handle
== NULL
|| name
== NULL
) {
937 return -LTTNG_ERR_INVALID
;
940 memset(&lsm
, 0, sizeof(lsm
));
942 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
944 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
945 sizeof(lsm
.u
.disable
.channel_name
));
947 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
949 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
950 sizeof(lsm
.session
.name
));
952 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
956 * Lists all available tracepoints of domain.
957 * Sets the contents of the events array.
958 * Returns the number of lttng_event entries in events;
959 * on error, returns a negative value.
961 int lttng_list_tracepoints(struct lttng_handle
*handle
,
962 struct lttng_event
**events
)
965 struct lttcomm_session_msg lsm
;
967 if (handle
== NULL
) {
968 return -LTTNG_ERR_INVALID
;
971 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
972 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
974 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
979 return ret
/ sizeof(struct lttng_event
);
983 * Lists all available tracepoint fields of domain.
984 * Sets the contents of the event field array.
985 * Returns the number of lttng_event_field entries in events;
986 * on error, returns a negative value.
988 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
989 struct lttng_event_field
**fields
)
992 struct lttcomm_session_msg lsm
;
994 if (handle
== NULL
) {
995 return -LTTNG_ERR_INVALID
;
998 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
999 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1001 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1006 return ret
/ sizeof(struct lttng_event_field
);
1010 * Returns a human readable string describing
1011 * the error code (a negative value).
1013 const char *lttng_strerror(int code
)
1015 return error_get_str(code
);
1019 * Create a brand new session using name and url for destination.
1021 * Returns LTTNG_OK on success or a negative error code.
1023 int lttng_create_session(const char *name
, const char *url
)
1027 struct lttcomm_session_msg lsm
;
1028 struct lttng_uri
*uris
= NULL
;
1031 return -LTTNG_ERR_INVALID
;
1034 memset(&lsm
, 0, sizeof(lsm
));
1036 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1037 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1039 /* There should never be a data URL */
1040 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1042 return -LTTNG_ERR_INVALID
;
1045 lsm
.u
.uri
.size
= size
;
1047 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1048 sizeof(struct lttng_uri
) * size
, NULL
);
1055 * Destroy session using name.
1056 * Returns size of returned session payload data or a negative error code.
1058 int lttng_destroy_session(const char *session_name
)
1060 struct lttcomm_session_msg lsm
;
1062 if (session_name
== NULL
) {
1063 return -LTTNG_ERR_INVALID
;
1066 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1068 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1069 sizeof(lsm
.session
.name
));
1071 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1075 * Ask the session daemon for all available sessions.
1076 * Sets the contents of the sessions array.
1077 * Returns the number of lttng_session entries in sessions;
1078 * on error, returns a negative value.
1080 int lttng_list_sessions(struct lttng_session
**sessions
)
1083 struct lttcomm_session_msg lsm
;
1085 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1086 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1091 return ret
/ sizeof(struct lttng_session
);
1095 * Ask the session daemon for all available domains of a session.
1096 * Sets the contents of the domains array.
1097 * Returns the number of lttng_domain entries in domains;
1098 * on error, returns a negative value.
1100 int lttng_list_domains(const char *session_name
,
1101 struct lttng_domain
**domains
)
1104 struct lttcomm_session_msg lsm
;
1106 if (session_name
== NULL
) {
1107 return -LTTNG_ERR_INVALID
;
1110 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1112 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1113 sizeof(lsm
.session
.name
));
1115 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1120 return ret
/ sizeof(struct lttng_domain
);
1124 * Ask the session daemon for all available channels of a session.
1125 * Sets the contents of the channels array.
1126 * Returns the number of lttng_channel entries in channels;
1127 * on error, returns a negative value.
1129 int lttng_list_channels(struct lttng_handle
*handle
,
1130 struct lttng_channel
**channels
)
1133 struct lttcomm_session_msg lsm
;
1135 if (handle
== NULL
) {
1136 return -LTTNG_ERR_INVALID
;
1139 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1140 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1141 sizeof(lsm
.session
.name
));
1143 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1145 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1150 return ret
/ sizeof(struct lttng_channel
);
1154 * Ask the session daemon for all available events of a session channel.
1155 * Sets the contents of the events array.
1156 * Returns the number of lttng_event entries in events;
1157 * on error, returns a negative value.
1159 int lttng_list_events(struct lttng_handle
*handle
,
1160 const char *channel_name
, struct lttng_event
**events
)
1163 struct lttcomm_session_msg lsm
;
1165 /* Safety check. An handle and channel name are mandatory */
1166 if (handle
== NULL
|| channel_name
== NULL
) {
1167 return -LTTNG_ERR_INVALID
;
1170 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1171 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1172 sizeof(lsm
.session
.name
));
1173 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1174 sizeof(lsm
.u
.list
.channel_name
));
1176 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1178 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1183 return ret
/ sizeof(struct lttng_event
);
1187 * Sets the tracing_group variable with name.
1188 * This function allocates memory pointed to by tracing_group.
1189 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1191 int lttng_set_tracing_group(const char *name
)
1194 return -LTTNG_ERR_INVALID
;
1197 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1198 return -LTTNG_ERR_FATAL
;
1205 * Returns size of returned session payload data or a negative error code.
1207 int lttng_calibrate(struct lttng_handle
*handle
,
1208 struct lttng_calibrate
*calibrate
)
1210 struct lttcomm_session_msg lsm
;
1212 /* Safety check. NULL pointer are forbidden */
1213 if (handle
== NULL
|| calibrate
== NULL
) {
1214 return -LTTNG_ERR_INVALID
;
1217 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1218 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1220 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1222 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1226 * Set default channel attributes.
1227 * If either or both of the arguments are null, attr content is zeroe'd.
1229 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1230 struct lttng_channel_attr
*attr
)
1233 if (attr
== NULL
|| domain
== NULL
) {
1237 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1239 /* Same for all domains. */
1240 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1241 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1242 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1244 switch (domain
->type
) {
1245 case LTTNG_DOMAIN_KERNEL
:
1246 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1247 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1248 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1249 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1250 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1252 case LTTNG_DOMAIN_UST
:
1253 switch (domain
->buf_type
) {
1254 case LTTNG_BUFFER_PER_UID
:
1255 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1256 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1257 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1258 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1259 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1261 case LTTNG_BUFFER_PER_PID
:
1263 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1264 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1265 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1266 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1267 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1271 /* Default behavior: leave set to 0. */
1277 * Check if session daemon is alive.
1279 * Return 1 if alive or 0 if not.
1280 * On error returns a negative value.
1282 int lttng_session_daemon_alive(void)
1286 ret
= set_session_daemon_path();
1292 if (*sessiond_sock_path
== '\0') {
1294 * No socket path set. Weird error which means the constructor was not
1300 ret
= try_connect_sessiond(sessiond_sock_path
);
1311 * Set URL for a consumer for a session and domain.
1313 * Return 0 on success, else a negative value.
1315 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1316 const char *control_url
, const char *data_url
)
1320 struct lttcomm_session_msg lsm
;
1321 struct lttng_uri
*uris
= NULL
;
1323 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1324 return -LTTNG_ERR_INVALID
;
1327 memset(&lsm
, 0, sizeof(lsm
));
1329 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1331 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1332 sizeof(lsm
.session
.name
));
1333 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1335 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1337 return -LTTNG_ERR_INVALID
;
1340 lsm
.u
.uri
.size
= size
;
1342 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1343 sizeof(struct lttng_uri
) * size
, NULL
);
1352 int lttng_enable_consumer(struct lttng_handle
*handle
)
1360 int lttng_disable_consumer(struct lttng_handle
*handle
)
1366 * Set health socket path by putting it in the global health_sock_path
1369 * Returns 0 on success or -ENOMEM.
1371 static int set_health_socket_path(void)
1379 if (uid
== 0 || check_tracing_group(tracing_group
)) {
1380 lttng_ctl_copy_string(health_sock_path
,
1381 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
, sizeof(health_sock_path
));
1386 * With GNU C < 2.1, snprintf returns -1 if the target buffer
1387 * is too small; With GNU C >= 2.1, snprintf returns the
1388 * required size (excluding closing null).
1390 home
= utils_get_home_dir();
1392 /* Fallback in /tmp */
1396 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1397 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1398 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1406 * Check session daemon health for a specific health component.
1408 * Return 0 if health is OK or else 1 if BAD.
1410 * Any other negative value is a lttng error code which can be translated with
1413 int lttng_health_check(enum lttng_health_component c
)
1416 struct health_comm_msg msg
;
1417 struct health_comm_reply reply
;
1419 /* Connect to the sesssion daemon */
1420 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1422 ret
= -LTTNG_ERR_NO_SESSIOND
;
1426 msg
.cmd
= HEALTH_CMD_CHECK
;
1429 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1431 ret
= -LTTNG_ERR_FATAL
;
1435 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1437 ret
= -LTTNG_ERR_FATAL
;
1441 ret
= reply
.ret_code
;
1447 closeret
= close(sock
);
1456 * This is an extension of create session that is ONLY and SHOULD only be used
1457 * by the lttng command line program. It exists to avoid using URI parsing in
1460 * We need the date and time for the trace path subdirectory for the case where
1461 * the user does NOT define one using either -o or -U. Using the normal
1462 * lttng_create_session API call, we have no clue on the session daemon side if
1463 * the URL was generated automatically by the client or define by the user.
1465 * So this function "wrapper" is hidden from the public API, takes the datetime
1466 * string and appends it if necessary to the URI subdirectory before sending it
1467 * to the session daemon.
1469 * With this extra function, the lttng_create_session call behavior is not
1470 * changed and the timestamp is appended to the URI on the session daemon side
1473 int _lttng_create_session_ext(const char *name
, const char *url
,
1474 const char *datetime
)
1478 struct lttcomm_session_msg lsm
;
1479 struct lttng_uri
*uris
= NULL
;
1481 if (name
== NULL
|| datetime
== NULL
) {
1482 return -LTTNG_ERR_INVALID
;
1485 memset(&lsm
, 0, sizeof(lsm
));
1487 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1488 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1490 /* There should never be a data URL */
1491 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1493 ret
= -LTTNG_ERR_INVALID
;
1497 lsm
.u
.uri
.size
= size
;
1499 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1500 /* Don't append datetime if the name was automatically created. */
1501 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1502 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1503 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1506 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1509 PERROR("snprintf uri subdir");
1510 ret
= -LTTNG_ERR_FATAL
;
1515 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1516 sizeof(struct lttng_uri
) * size
, NULL
);
1524 * For a given session name, this call checks if the data is ready to be read
1525 * or is still being extracted by the consumer(s) hence not ready to be used by
1528 int lttng_data_pending(const char *session_name
)
1531 struct lttcomm_session_msg lsm
;
1533 if (session_name
== NULL
) {
1534 return -LTTNG_ERR_INVALID
;
1537 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1539 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1540 sizeof(lsm
.session
.name
));
1542 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1545 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1546 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1547 * that the data is available. Yes it is hackish but for now this is the
1558 * Create a session exclusively used for snapshot.
1560 * Returns LTTNG_OK on success or a negative error code.
1562 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1566 struct lttcomm_session_msg lsm
;
1567 struct lttng_uri
*uris
= NULL
;
1570 return -LTTNG_ERR_INVALID
;
1573 memset(&lsm
, 0, sizeof(lsm
));
1575 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1576 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1578 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1580 return -LTTNG_ERR_INVALID
;
1583 lsm
.u
.uri
.size
= size
;
1585 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1586 sizeof(struct lttng_uri
) * size
, NULL
);
1593 * Create a session exclusively used for live.
1595 * Returns LTTNG_OK on success or a negative error code.
1597 int lttng_create_session_live(const char *name
, const char *url
,
1598 unsigned int timer_interval
)
1602 struct lttcomm_session_msg lsm
;
1603 struct lttng_uri
*uris
= NULL
;
1606 return -LTTNG_ERR_INVALID
;
1609 memset(&lsm
, 0, sizeof(lsm
));
1611 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
1612 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1614 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1616 ret
= -LTTNG_ERR_INVALID
;
1620 /* file:// is not accepted for live session. */
1621 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1622 ret
= -LTTNG_ERR_INVALID
;
1626 lsm
.u
.session_live
.nb_uri
= size
;
1627 lsm
.u
.session_live
.timer_interval
= timer_interval
;
1629 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1630 sizeof(struct lttng_uri
) * size
, NULL
);
1640 static void __attribute__((constructor
)) init()
1642 /* Set default session group */
1643 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1644 /* Set socket for health check */
1645 if (set_health_socket_path()) {
1653 static void __attribute__((destructor
)) lttng_ctl_exit()
1655 free(tracing_group
);