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>
38 #include "filter/filter-ast.h"
39 #include "filter/filter-parser.h"
40 #include "filter/filter-bytecode.h"
41 #include "filter/memstream.h"
42 #include "lttng-ctl-helper.h"
45 static const int print_xml
= 1;
46 #define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
49 static const int print_xml
= 0;
50 #define dbg_printf(fmt, args...) \
52 /* do nothing but check printf format */ \
54 printf("[debug liblttnctl] " fmt, ## args); \
59 /* Socket to session daemon for communication */
60 static int sessiond_socket
;
61 static char sessiond_sock_path
[PATH_MAX
];
62 static char health_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
79 * Compare two URL destination.
81 * Return 0 is equal else is not equal.
83 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
90 switch (ctrl
->dtype
) {
92 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
95 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
105 static void set_default_url_attr(struct lttng_uri
*uri
,
106 enum lttng_stream_type stype
)
109 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
110 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
111 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
116 * Parse a string URL and creates URI(s) returning the size of the populated
119 static ssize_t
parse_str_urls_to_uri(const char *ctrl_url
, const char *data_url
,
120 struct lttng_uri
**uris
)
122 unsigned int equal
= 1, idx
= 0;
123 /* Add the "file://" size to the URL maximum size */
124 char url
[PATH_MAX
+ 7];
125 ssize_t size_ctrl
= 0, size_data
= 0, size
;
126 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
127 struct lttng_uri
*tmp_uris
= NULL
;
129 /* No URL(s) is allowed. This means that the consumer will be disabled. */
130 if (ctrl_url
== NULL
&& data_url
== NULL
) {
134 /* Check if URLs are equal and if so, only use the control URL */
135 if (ctrl_url
&& data_url
) {
136 equal
= !strcmp(ctrl_url
, data_url
);
140 * Since we allow the str_url to be a full local filesystem path, we are
141 * going to create a valid file:// URL if it's the case.
143 * Check if first character is a '/' or else reject the URL.
145 if (ctrl_url
&& ctrl_url
[0] == '/') {
148 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
150 PERROR("snprintf file url");
156 /* Parse the control URL if there is one */
158 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
160 ERR("Unable to parse the URL %s", ctrl_url
);
164 /* At this point, we know there is at least one URI in the array */
165 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
167 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
168 ERR("Can not have a data URL when destination is file://");
172 /* URL are not equal but the control URL uses a net:// protocol */
173 if (size_ctrl
== 2) {
175 ERR("Control URL uses the net:// protocol and the data URL is "
176 "different. Not allowed.");
179 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
181 * The data_url and ctrl_url are equal and the ctrl_url
182 * contains a net:// protocol so we just skip the data part.
192 /* We have to parse the data URL in this case */
193 size_data
= uri_parse(data_url
, &data_uris
);
195 ERR("Unable to parse the URL %s", data_url
);
197 } else if (size_data
== 2) {
198 ERR("Data URL can not be set with the net[4|6]:// protocol");
202 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
204 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
206 ERR("Control and data destination mismatch");
211 /* Compute total size */
212 size
= size_ctrl
+ size_data
;
214 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
215 if (tmp_uris
== NULL
) {
216 PERROR("zmalloc uris");
221 /* It's possible the control URIs array contains more than one URI */
222 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
228 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
245 * Copy string from src to dst and enforce null terminated byte.
248 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
251 strncpy(dst
, src
, len
);
252 /* Enforce the NULL terminated byte */
260 * Copy domain to lttcomm_session_msg domain.
262 * If domain is unknown, default domain will be the kernel.
265 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
266 struct lttng_domain
*src
)
270 case LTTNG_DOMAIN_KERNEL
:
271 case LTTNG_DOMAIN_UST
:
272 memcpy(dst
, src
, sizeof(struct lttng_domain
));
275 memset(dst
, 0, sizeof(struct lttng_domain
));
282 * Send lttcomm_session_msg to the session daemon.
284 * On success, returns the number of bytes sent (>=0)
285 * On error, returns -1
287 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
292 ret
= -LTTNG_ERR_NO_SESSIOND
;
296 DBG("LSM cmd type : %d", lsm
->cmd_type
);
298 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
299 sizeof(struct lttcomm_session_msg
));
301 ret
= -LTTNG_ERR_FATAL
;
309 * Send var len data to the session daemon.
311 * On success, returns the number of bytes sent (>=0)
312 * On error, returns -1
314 static int send_session_varlen(void *data
, size_t len
)
319 ret
= -LTTNG_ERR_NO_SESSIOND
;
328 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
330 ret
= -LTTNG_ERR_FATAL
;
338 * Receive data from the sessiond socket.
340 * On success, returns the number of bytes received (>=0)
341 * On error, returns -1 (recvmsg() error) or -ENOTCONN
343 static int recv_data_sessiond(void *buf
, size_t len
)
348 ret
= -LTTNG_ERR_NO_SESSIOND
;
352 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
354 ret
= -LTTNG_ERR_FATAL
;
362 * Check if we are in the specified group.
364 * If yes return 1, else return -1.
366 static int check_tracing_group(const char *grp_name
)
368 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
370 int grp_list_size
, grp_id
, i
;
373 /* Get GID of group 'tracing' */
374 grp_tracing
= getgrnam(grp_name
);
376 /* If grp_tracing is NULL, the group does not exist. */
380 /* Get number of supplementary group IDs */
381 grp_list_size
= getgroups(0, NULL
);
382 if (grp_list_size
< 0) {
387 /* Alloc group list of the right size */
388 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
393 grp_id
= getgroups(grp_list_size
, grp_list
);
399 for (i
= 0; i
< grp_list_size
; i
++) {
400 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
414 * Try connect to session daemon with sock_path.
416 * Return 0 on success, else -1
418 static int try_connect_sessiond(const char *sock_path
)
422 /* If socket exist, we check if the daemon listens for connect. */
423 ret
= access(sock_path
, F_OK
);
429 ret
= lttcomm_connect_unix_sock(sock_path
);
435 ret
= lttcomm_close_unix_sock(ret
);
437 perror("lttcomm_close_unix_sock");
447 * Set sessiond socket path by putting it in the global sessiond_sock_path
450 * Returns 0 on success, negative value on failure (the sessiond socket path
451 * is somehow too long or ENOMEM).
453 static int set_session_daemon_path(void)
455 int in_tgroup
= 0; /* In tracing group */
461 /* Are we in the tracing group ? */
462 in_tgroup
= check_tracing_group(tracing_group
);
465 if ((uid
== 0) || in_tgroup
) {
466 lttng_ctl_copy_string(sessiond_sock_path
,
467 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
475 ret
= try_connect_sessiond(sessiond_sock_path
);
479 /* Global session daemon not available... */
481 /* ...or not in tracing group (and not root), default */
484 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
485 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
487 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
488 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
489 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
501 * Connect to the LTTng session daemon.
503 * On success, return 0. On error, return -1.
505 static int connect_sessiond(void)
509 ret
= set_session_daemon_path();
514 /* Connect to the sesssion daemon */
515 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
520 sessiond_socket
= ret
;
530 * Clean disconnect from the session daemon.
531 * On success, return 0. On error, return -1.
533 static int disconnect_sessiond(void)
538 ret
= lttcomm_close_unix_sock(sessiond_socket
);
547 * Ask the session daemon a specific command and put the data into buf.
548 * Takes extra var. len. data as input to send to the session daemon.
550 * Return size of data (only payload, not header) or a negative error code.
553 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
554 void *vardata
, size_t varlen
, void **buf
)
559 struct lttcomm_lttng_msg llm
;
561 ret
= connect_sessiond();
563 ret
= -LTTNG_ERR_NO_SESSIOND
;
567 /* Send command to session daemon */
568 ret
= send_session_msg(lsm
);
570 /* Ret value is a valid lttng error code. */
573 /* Send var len data */
574 ret
= send_session_varlen(vardata
, varlen
);
576 /* Ret value is a valid lttng error code. */
580 /* Get header from data transmission */
581 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
583 /* Ret value is a valid lttng error code. */
587 /* Check error code if OK */
588 if (llm
.ret_code
!= LTTNG_OK
) {
593 size
= llm
.data_size
;
595 /* If client free with size 0 */
603 data
= (void*) malloc(size
);
605 /* Get payload data */
606 ret
= recv_data_sessiond(data
, size
);
613 * Extra protection not to dereference a NULL pointer. If buf is NULL at
614 * this point, an error is returned and data is freed.
617 ret
= -LTTNG_ERR_INVALID
;
626 disconnect_sessiond();
631 * Create lttng handle and return pointer.
632 * The returned pointer will be NULL in case of malloc() error.
634 struct lttng_handle
*lttng_create_handle(const char *session_name
,
635 struct lttng_domain
*domain
)
637 struct lttng_handle
*handle
= NULL
;
639 if (domain
== NULL
) {
643 handle
= malloc(sizeof(struct lttng_handle
));
644 if (handle
== NULL
) {
645 PERROR("malloc handle");
649 /* Copy session name */
650 lttng_ctl_copy_string(handle
->session_name
, session_name
,
651 sizeof(handle
->session_name
));
653 /* Copy lttng domain */
654 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
661 * Destroy handle by free(3) the pointer.
663 void lttng_destroy_handle(struct lttng_handle
*handle
)
669 * Register an outside consumer.
670 * Returns size of returned session payload data or a negative error code.
672 int lttng_register_consumer(struct lttng_handle
*handle
,
673 const char *socket_path
)
675 struct lttcomm_session_msg lsm
;
677 if (handle
== NULL
|| socket_path
== NULL
) {
678 return -LTTNG_ERR_INVALID
;
681 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
682 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
683 sizeof(lsm
.session
.name
));
684 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
686 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
688 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
692 * Start tracing for all traces of the session.
693 * Returns size of returned session payload data or a negative error code.
695 int lttng_start_tracing(const char *session_name
)
697 struct lttcomm_session_msg lsm
;
699 if (session_name
== NULL
) {
700 return -LTTNG_ERR_INVALID
;
703 lsm
.cmd_type
= LTTNG_START_TRACE
;
705 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
706 sizeof(lsm
.session
.name
));
708 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
712 * Stop tracing for all traces of the session.
714 static int _lttng_stop_tracing(const char *session_name
, int wait
)
717 struct lttcomm_session_msg lsm
;
719 if (session_name
== NULL
) {
720 return -LTTNG_ERR_INVALID
;
723 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
725 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
726 sizeof(lsm
.session
.name
));
728 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
729 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
737 _MSG("Waiting for data availability");
739 /* Check for data availability */
741 data_ret
= lttng_data_pending(session_name
);
743 /* Return the data available call error. */
749 * Data sleep time before retrying (in usec). Don't sleep if the call
750 * returned value indicates availability.
753 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
756 } while (data_ret
!= 0);
766 * Stop tracing and wait for data availability.
768 int lttng_stop_tracing(const char *session_name
)
770 return _lttng_stop_tracing(session_name
, 1);
774 * Stop tracing but _don't_ wait for data availability.
776 int lttng_stop_tracing_no_wait(const char *session_name
)
778 return _lttng_stop_tracing(session_name
, 0);
782 * Add context to a channel.
784 * If the given channel is NULL, add the contexts to all channels.
785 * The event_name param is ignored.
787 * Returns the size of the returned payload data or a negative error code.
789 int lttng_add_context(struct lttng_handle
*handle
,
790 struct lttng_event_context
*ctx
, const char *event_name
,
791 const char *channel_name
)
793 struct lttcomm_session_msg lsm
;
795 /* Safety check. Both are mandatory */
796 if (handle
== NULL
|| ctx
== NULL
) {
797 return -LTTNG_ERR_INVALID
;
800 memset(&lsm
, 0, sizeof(lsm
));
802 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
804 /* Copy channel name */
805 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
806 sizeof(lsm
.u
.context
.channel_name
));
808 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
810 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
812 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
813 sizeof(lsm
.session
.name
));
815 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
819 * Enable event(s) for a channel.
820 * If no event name is specified, all events are enabled.
821 * If no channel name is specified, the default 'channel0' is used.
822 * Returns size of returned session payload data or a negative error code.
824 int lttng_enable_event(struct lttng_handle
*handle
,
825 struct lttng_event
*ev
, const char *channel_name
)
827 struct lttcomm_session_msg lsm
;
829 if (handle
== NULL
|| ev
== NULL
) {
830 return -LTTNG_ERR_INVALID
;
833 memset(&lsm
, 0, sizeof(lsm
));
835 /* If no channel name, we put the default name */
836 if (channel_name
== NULL
) {
837 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
838 sizeof(lsm
.u
.enable
.channel_name
));
840 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
841 sizeof(lsm
.u
.enable
.channel_name
));
844 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
846 if (ev
->name
[0] != '\0') {
847 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
849 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
851 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
853 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
854 sizeof(lsm
.session
.name
));
856 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
860 * Create or enable an event with a filter expression.
862 * Return negative error value on error.
863 * Return size of returned session payload data if OK.
865 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
866 struct lttng_event
*event
, const char *channel_name
,
867 const char *filter_expression
)
869 struct lttcomm_session_msg lsm
;
870 struct filter_parser_ctx
*ctx
;
874 if (!filter_expression
) {
876 * Fall back to normal event enabling if no filter
879 return lttng_enable_event(handle
, event
, channel_name
);
883 * Empty filter string will always be rejected by the parser
884 * anyway, so treat this corner-case early to eliminate
885 * lttng_fmemopen error for 0-byte allocation.
887 if (handle
== NULL
|| filter_expression
[0] == '\0') {
888 return -LTTNG_ERR_INVALID
;
892 * casting const to non-const, as the underlying function will
893 * use it in read-only mode.
895 fmem
= lttng_fmemopen((void *) filter_expression
,
896 strlen(filter_expression
), "r");
898 fprintf(stderr
, "Error opening memory as stream\n");
899 return -LTTNG_ERR_FILTER_NOMEM
;
901 ctx
= filter_parser_ctx_alloc(fmem
);
903 fprintf(stderr
, "Error allocating parser\n");
904 ret
= -LTTNG_ERR_FILTER_NOMEM
;
907 ret
= filter_parser_ctx_append_ast(ctx
);
909 fprintf(stderr
, "Parse error\n");
910 ret
= -LTTNG_ERR_FILTER_INVAL
;
913 ret
= filter_visitor_set_parent(ctx
);
915 fprintf(stderr
, "Set parent error\n");
916 ret
= -LTTNG_ERR_FILTER_INVAL
;
920 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
923 fprintf(stderr
, "XML print error\n");
924 ret
= -LTTNG_ERR_FILTER_INVAL
;
929 dbg_printf("Generating IR... ");
931 ret
= filter_visitor_ir_generate(ctx
);
933 fprintf(stderr
, "Generate IR error\n");
934 ret
= -LTTNG_ERR_FILTER_INVAL
;
937 dbg_printf("done\n");
939 dbg_printf("Validating IR... ");
941 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
943 ret
= -LTTNG_ERR_FILTER_INVAL
;
946 dbg_printf("done\n");
948 dbg_printf("Generating bytecode... ");
950 ret
= filter_visitor_bytecode_generate(ctx
);
952 fprintf(stderr
, "Generate bytecode error\n");
953 ret
= -LTTNG_ERR_FILTER_INVAL
;
956 dbg_printf("done\n");
957 dbg_printf("Size of bytecode generated: %u bytes.\n",
958 bytecode_get_len(&ctx
->bytecode
->b
));
960 memset(&lsm
, 0, sizeof(lsm
));
962 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
964 /* Copy channel name */
965 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
966 sizeof(lsm
.u
.enable
.channel_name
));
967 /* Copy event name */
969 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
972 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
973 + bytecode_get_len(&ctx
->bytecode
->b
);
975 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
977 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
978 sizeof(lsm
.session
.name
));
980 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
981 lsm
.u
.enable
.bytecode_len
, NULL
);
983 filter_bytecode_free(ctx
);
985 filter_parser_ctx_free(ctx
);
986 if (fclose(fmem
) != 0) {
992 filter_bytecode_free(ctx
);
994 filter_parser_ctx_free(ctx
);
996 if (fclose(fmem
) != 0) {
1003 * Disable event(s) of a channel and domain.
1004 * If no event name is specified, all events are disabled.
1005 * If no channel name is specified, the default 'channel0' is used.
1006 * Returns size of returned session payload data or a negative error code.
1008 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1009 const char *channel_name
)
1011 struct lttcomm_session_msg lsm
;
1013 if (handle
== NULL
) {
1014 return -LTTNG_ERR_INVALID
;
1017 memset(&lsm
, 0, sizeof(lsm
));
1020 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1021 sizeof(lsm
.u
.disable
.channel_name
));
1023 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
1024 sizeof(lsm
.u
.disable
.channel_name
));
1027 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1030 lttng_ctl_copy_string(lsm
.u
.disable
.name
, name
,
1031 sizeof(lsm
.u
.disable
.name
));
1032 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1034 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
1037 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1038 sizeof(lsm
.session
.name
));
1040 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1044 * Enable channel per domain
1045 * Returns size of returned session payload data or a negative error code.
1047 int lttng_enable_channel(struct lttng_handle
*handle
,
1048 struct lttng_channel
*chan
)
1050 struct lttcomm_session_msg lsm
;
1053 * NULL arguments are forbidden. No default values.
1055 if (handle
== NULL
|| chan
== NULL
) {
1056 return -LTTNG_ERR_INVALID
;
1059 memset(&lsm
, 0, sizeof(lsm
));
1061 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1063 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1065 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1067 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1068 sizeof(lsm
.session
.name
));
1070 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1074 * All tracing will be stopped for registered events of the channel.
1075 * Returns size of returned session payload data or a negative error code.
1077 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1079 struct lttcomm_session_msg lsm
;
1081 /* Safety check. Both are mandatory */
1082 if (handle
== NULL
|| name
== NULL
) {
1083 return -LTTNG_ERR_INVALID
;
1086 memset(&lsm
, 0, sizeof(lsm
));
1088 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1090 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1091 sizeof(lsm
.u
.disable
.channel_name
));
1093 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1095 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1096 sizeof(lsm
.session
.name
));
1098 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1102 * Lists all available tracepoints of domain.
1103 * Sets the contents of the events array.
1104 * Returns the number of lttng_event entries in events;
1105 * on error, returns a negative value.
1107 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1108 struct lttng_event
**events
)
1111 struct lttcomm_session_msg lsm
;
1113 if (handle
== NULL
) {
1114 return -LTTNG_ERR_INVALID
;
1117 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1118 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1120 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1125 return ret
/ sizeof(struct lttng_event
);
1129 * Lists all available tracepoint fields of domain.
1130 * Sets the contents of the event field array.
1131 * Returns the number of lttng_event_field entries in events;
1132 * on error, returns a negative value.
1134 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1135 struct lttng_event_field
**fields
)
1138 struct lttcomm_session_msg lsm
;
1140 if (handle
== NULL
) {
1141 return -LTTNG_ERR_INVALID
;
1144 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1145 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1147 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1152 return ret
/ sizeof(struct lttng_event_field
);
1156 * Returns a human readable string describing
1157 * the error code (a negative value).
1159 const char *lttng_strerror(int code
)
1161 return error_get_str(code
);
1165 * Create a brand new session using name and url for destination.
1167 * Returns LTTNG_OK on success or a negative error code.
1169 int lttng_create_session(const char *name
, const char *url
)
1173 struct lttcomm_session_msg lsm
;
1174 struct lttng_uri
*uris
= NULL
;
1177 return -LTTNG_ERR_INVALID
;
1180 memset(&lsm
, 0, sizeof(lsm
));
1182 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1183 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1185 /* There should never be a data URL */
1186 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1188 return -LTTNG_ERR_INVALID
;
1191 lsm
.u
.uri
.size
= size
;
1193 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1194 sizeof(struct lttng_uri
) * size
, NULL
);
1201 * Destroy session using name.
1202 * Returns size of returned session payload data or a negative error code.
1204 int lttng_destroy_session(const char *session_name
)
1206 struct lttcomm_session_msg lsm
;
1208 if (session_name
== NULL
) {
1209 return -LTTNG_ERR_INVALID
;
1212 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1214 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1215 sizeof(lsm
.session
.name
));
1217 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1221 * Ask the session daemon for all available sessions.
1222 * Sets the contents of the sessions array.
1223 * Returns the number of lttng_session entries in sessions;
1224 * on error, returns a negative value.
1226 int lttng_list_sessions(struct lttng_session
**sessions
)
1229 struct lttcomm_session_msg lsm
;
1231 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1232 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1237 return ret
/ sizeof(struct lttng_session
);
1241 * Ask the session daemon for all available domains of a session.
1242 * Sets the contents of the domains array.
1243 * Returns the number of lttng_domain entries in domains;
1244 * on error, returns a negative value.
1246 int lttng_list_domains(const char *session_name
,
1247 struct lttng_domain
**domains
)
1250 struct lttcomm_session_msg lsm
;
1252 if (session_name
== NULL
) {
1253 return -LTTNG_ERR_INVALID
;
1256 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1258 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1259 sizeof(lsm
.session
.name
));
1261 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1266 return ret
/ sizeof(struct lttng_domain
);
1270 * Ask the session daemon for all available channels of a session.
1271 * Sets the contents of the channels array.
1272 * Returns the number of lttng_channel entries in channels;
1273 * on error, returns a negative value.
1275 int lttng_list_channels(struct lttng_handle
*handle
,
1276 struct lttng_channel
**channels
)
1279 struct lttcomm_session_msg lsm
;
1281 if (handle
== NULL
) {
1282 return -LTTNG_ERR_INVALID
;
1285 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1286 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1287 sizeof(lsm
.session
.name
));
1289 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1291 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1296 return ret
/ sizeof(struct lttng_channel
);
1300 * Ask the session daemon for all available events of a session channel.
1301 * Sets the contents of the events array.
1302 * Returns the number of lttng_event entries in events;
1303 * on error, returns a negative value.
1305 int lttng_list_events(struct lttng_handle
*handle
,
1306 const char *channel_name
, struct lttng_event
**events
)
1309 struct lttcomm_session_msg lsm
;
1311 /* Safety check. An handle and channel name are mandatory */
1312 if (handle
== NULL
|| channel_name
== NULL
) {
1313 return -LTTNG_ERR_INVALID
;
1316 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1317 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1318 sizeof(lsm
.session
.name
));
1319 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1320 sizeof(lsm
.u
.list
.channel_name
));
1322 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1324 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1329 return ret
/ sizeof(struct lttng_event
);
1333 * Sets the tracing_group variable with name.
1334 * This function allocates memory pointed to by tracing_group.
1335 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1337 int lttng_set_tracing_group(const char *name
)
1340 return -LTTNG_ERR_INVALID
;
1343 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1344 return -LTTNG_ERR_FATAL
;
1351 * Returns size of returned session payload data or a negative error code.
1353 int lttng_calibrate(struct lttng_handle
*handle
,
1354 struct lttng_calibrate
*calibrate
)
1356 struct lttcomm_session_msg lsm
;
1358 /* Safety check. NULL pointer are forbidden */
1359 if (handle
== NULL
|| calibrate
== NULL
) {
1360 return -LTTNG_ERR_INVALID
;
1363 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1364 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1366 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1368 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1372 * Set default channel attributes.
1373 * If either or both of the arguments are null, attr content is zeroe'd.
1375 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1376 struct lttng_channel_attr
*attr
)
1379 if (attr
== NULL
|| domain
== NULL
) {
1383 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1385 /* Same for all domains. */
1386 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1387 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1388 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1390 switch (domain
->type
) {
1391 case LTTNG_DOMAIN_KERNEL
:
1392 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1393 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1394 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1395 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1396 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1398 case LTTNG_DOMAIN_UST
:
1399 switch (domain
->buf_type
) {
1400 case LTTNG_BUFFER_PER_UID
:
1401 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1402 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1403 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1404 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1405 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1407 case LTTNG_BUFFER_PER_PID
:
1409 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1410 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1411 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1412 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1413 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1417 /* Default behavior: leave set to 0. */
1423 * Check if session daemon is alive.
1425 * Return 1 if alive or 0 if not.
1426 * On error returns a negative value.
1428 int lttng_session_daemon_alive(void)
1432 ret
= set_session_daemon_path();
1438 if (*sessiond_sock_path
== '\0') {
1440 * No socket path set. Weird error which means the constructor was not
1446 ret
= try_connect_sessiond(sessiond_sock_path
);
1457 * Set URL for a consumer for a session and domain.
1459 * Return 0 on success, else a negative value.
1461 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1462 const char *control_url
, const char *data_url
)
1466 struct lttcomm_session_msg lsm
;
1467 struct lttng_uri
*uris
= NULL
;
1469 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1470 return -LTTNG_ERR_INVALID
;
1473 memset(&lsm
, 0, sizeof(lsm
));
1475 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1477 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1478 sizeof(lsm
.session
.name
));
1479 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1481 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1483 return -LTTNG_ERR_INVALID
;
1486 lsm
.u
.uri
.size
= size
;
1488 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1489 sizeof(struct lttng_uri
) * size
, NULL
);
1498 int lttng_enable_consumer(struct lttng_handle
*handle
)
1506 int lttng_disable_consumer(struct lttng_handle
*handle
)
1512 * Set health socket path by putting it in the global health_sock_path
1515 * Returns 0 on success or assert(0) on ENOMEM.
1517 static int set_health_socket_path(void)
1519 int in_tgroup
= 0; /* In tracing group */
1526 /* Are we in the tracing group ? */
1527 in_tgroup
= check_tracing_group(tracing_group
);
1530 if ((uid
== 0) || in_tgroup
) {
1531 lttng_ctl_copy_string(health_sock_path
,
1532 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
, sizeof(health_sock_path
));
1539 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1540 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1542 home
= utils_get_home_dir();
1544 /* Fallback in /tmp .. */
1548 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1549 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1550 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1551 /* ENOMEM at this point... just kill the control lib. */
1560 * Check session daemon health for a specific health component.
1562 * Return 0 if health is OK or else 1 if BAD.
1564 * Any other negative value is a lttng error code which can be translated with
1567 int lttng_health_check(enum lttng_health_component c
)
1570 struct lttcomm_health_msg msg
;
1571 struct lttcomm_health_data reply
;
1573 /* Connect to the sesssion daemon */
1574 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1576 ret
= -LTTNG_ERR_NO_SESSIOND
;
1580 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1583 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1585 ret
= -LTTNG_ERR_FATAL
;
1589 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1591 ret
= -LTTNG_ERR_FATAL
;
1595 ret
= reply
.ret_code
;
1601 closeret
= close(sock
);
1610 * This is an extension of create session that is ONLY and SHOULD only be used
1611 * by the lttng command line program. It exists to avoid using URI parsing in
1614 * We need the date and time for the trace path subdirectory for the case where
1615 * the user does NOT define one using either -o or -U. Using the normal
1616 * lttng_create_session API call, we have no clue on the session daemon side if
1617 * the URL was generated automatically by the client or define by the user.
1619 * So this function "wrapper" is hidden from the public API, takes the datetime
1620 * string and appends it if necessary to the URI subdirectory before sending it
1621 * to the session daemon.
1623 * With this extra function, the lttng_create_session call behavior is not
1624 * changed and the timestamp is appended to the URI on the session daemon side
1627 int _lttng_create_session_ext(const char *name
, const char *url
,
1628 const char *datetime
)
1632 struct lttcomm_session_msg lsm
;
1633 struct lttng_uri
*uris
= NULL
;
1635 if (name
== NULL
|| datetime
== NULL
|| url
== NULL
) {
1636 return -LTTNG_ERR_INVALID
;
1639 memset(&lsm
, 0, sizeof(lsm
));
1641 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1642 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1644 /* There should never be a data URL */
1645 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1647 ret
= -LTTNG_ERR_INVALID
;
1651 lsm
.u
.uri
.size
= size
;
1653 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1654 /* Don't append datetime if the name was automatically created. */
1655 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1656 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1657 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1660 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1663 PERROR("snprintf uri subdir");
1664 ret
= -LTTNG_ERR_FATAL
;
1669 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1670 sizeof(struct lttng_uri
) * size
, NULL
);
1678 * For a given session name, this call checks if the data is ready to be read
1679 * or is still being extracted by the consumer(s) hence not ready to be used by
1682 int lttng_data_pending(const char *session_name
)
1685 struct lttcomm_session_msg lsm
;
1687 if (session_name
== NULL
) {
1688 return -LTTNG_ERR_INVALID
;
1691 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1693 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1694 sizeof(lsm
.session
.name
));
1696 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1699 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1700 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1701 * that the data is available. Yes it is hackish but for now this is the
1714 static void __attribute__((constructor
)) init()
1716 /* Set default session group */
1717 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1718 /* Set socket for health check */
1719 (void) set_health_socket_path();
1725 static void __attribute__((destructor
)) lttng_ctl_exit()
1727 free(tracing_group
);