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"
44 static const int print_xml
= 1;
45 #define dbg_printf(fmt, args...) \
46 printf("[debug liblttng-ctl] " fmt, ## args)
48 static const int print_xml
= 0;
49 #define dbg_printf(fmt, args...) \
51 /* do nothing but check printf format */ \
53 printf("[debug liblttnctl] " fmt, ## args); \
58 /* Socket to session daemon for communication */
59 static int sessiond_socket
;
60 static char sessiond_sock_path
[PATH_MAX
];
61 static char health_sock_path
[PATH_MAX
];
64 static char *tracing_group
;
70 * Those two variables are used by error.h to silent or control the verbosity of
71 * error message. They are global to the library so application linking with it
72 * are able to compile correctly and also control verbosity of the library.
75 int lttng_opt_verbose
;
78 * Compare two URL destination.
80 * Return 0 is equal else is not equal.
82 static int compare_destination(struct lttng_uri
*ctrl
, struct lttng_uri
*data
)
89 switch (ctrl
->dtype
) {
91 ret
= strncmp(ctrl
->dst
.ipv4
, data
->dst
.ipv4
, sizeof(ctrl
->dst
.ipv4
));
94 ret
= strncmp(ctrl
->dst
.ipv6
, data
->dst
.ipv6
, sizeof(ctrl
->dst
.ipv6
));
104 static void set_default_url_attr(struct lttng_uri
*uri
,
105 enum lttng_stream_type stype
)
108 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
109 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
110 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
115 * Parse a string URL and creates URI(s) returning the size of the populated
118 static ssize_t
parse_str_urls_to_uri(const char *ctrl_url
, const char *data_url
,
119 struct lttng_uri
**uris
)
121 unsigned int equal
= 1, idx
= 0;
122 /* Add the "file://" size to the URL maximum size */
123 char url
[PATH_MAX
+ 7];
124 ssize_t size_ctrl
= 0, size_data
= 0, size
;
125 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
126 struct lttng_uri
*tmp_uris
= NULL
;
128 /* No URL(s) is allowed. This means that the consumer will be disabled. */
129 if (ctrl_url
== NULL
&& data_url
== NULL
) {
133 /* Check if URLs are equal and if so, only use the control URL */
134 if (ctrl_url
&& data_url
) {
135 equal
= !strcmp(ctrl_url
, data_url
);
139 * Since we allow the str_url to be a full local filesystem path, we are
140 * going to create a valid file:// URL if it's the case.
142 * Check if first character is a '/' or else reject the URL.
144 if (ctrl_url
&& ctrl_url
[0] == '/') {
147 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
149 PERROR("snprintf file url");
155 /* Parse the control URL if there is one */
157 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
159 ERR("Unable to parse the URL %s", ctrl_url
);
163 /* At this point, we know there is at least one URI in the array */
164 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
166 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
167 ERR("Can not have a data URL when destination is file://");
171 /* URL are not equal but the control URL uses a net:// protocol */
172 if (size_ctrl
== 2) {
174 ERR("Control URL uses the net:// protocol and the data URL is "
175 "different. Not allowed.");
178 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
180 * The data_url and ctrl_url are equal and the ctrl_url
181 * contains a net:// protocol so we just skip the data part.
191 /* We have to parse the data URL in this case */
192 size_data
= uri_parse(data_url
, &data_uris
);
194 ERR("Unable to parse the URL %s", data_url
);
196 } else if (size_data
== 2) {
197 ERR("Data URL can not be set with the net[4|6]:// protocol");
201 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
203 ret
= compare_destination(&ctrl_uris
[0], &data_uris
[0]);
205 ERR("Control and data destination mismatch");
210 /* Compute total size */
211 size
= size_ctrl
+ size_data
;
213 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
214 if (tmp_uris
== NULL
) {
215 PERROR("zmalloc uris");
220 /* It's possible the control URIs array contains more than one URI */
221 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
227 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
244 * Copy string from src to dst and enforce null terminated byte.
246 static void copy_string(char *dst
, const char *src
, size_t len
)
249 strncpy(dst
, src
, len
);
250 /* Enforce the NULL terminated byte */
258 * Copy domain to lttcomm_session_msg domain.
260 * If domain is unknown, default domain will be the kernel.
262 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
266 case LTTNG_DOMAIN_KERNEL
:
267 case LTTNG_DOMAIN_UST
:
269 case LTTNG_DOMAIN_UST_EXEC_NAME:
270 case LTTNG_DOMAIN_UST_PID:
271 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
273 memcpy(dst
, src
, sizeof(struct lttng_domain
));
276 memset(dst
, 0, sizeof(struct lttng_domain
));
283 * Send lttcomm_session_msg to the session daemon.
285 * On success, returns the number of bytes sent (>=0)
286 * On error, returns -1
288 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
293 ret
= -LTTNG_ERR_NO_SESSIOND
;
297 DBG("LSM cmd type : %d", lsm
->cmd_type
);
299 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
300 sizeof(struct lttcomm_session_msg
));
302 ret
= -LTTNG_ERR_FATAL
;
310 * Send var len data to the session daemon.
312 * On success, returns the number of bytes sent (>=0)
313 * On error, returns -1
315 static int send_session_varlen(void *data
, size_t len
)
320 ret
= -LTTNG_ERR_NO_SESSIOND
;
329 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
331 ret
= -LTTNG_ERR_FATAL
;
339 * Receive data from the sessiond socket.
341 * On success, returns the number of bytes received (>=0)
342 * On error, returns -1 (recvmsg() error) or -ENOTCONN
344 static int recv_data_sessiond(void *buf
, size_t len
)
349 ret
= -LTTNG_ERR_NO_SESSIOND
;
353 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
355 ret
= -LTTNG_ERR_FATAL
;
363 * Check if we are in the specified group.
365 * If yes return 1, else return -1.
367 static int check_tracing_group(const char *grp_name
)
369 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
371 int grp_list_size
, grp_id
, i
;
374 /* Get GID of group 'tracing' */
375 grp_tracing
= getgrnam(grp_name
);
377 /* If grp_tracing is NULL, the group does not exist. */
381 /* Get number of supplementary group IDs */
382 grp_list_size
= getgroups(0, NULL
);
383 if (grp_list_size
< 0) {
388 /* Alloc group list of the right size */
389 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
394 grp_id
= getgroups(grp_list_size
, grp_list
);
400 for (i
= 0; i
< grp_list_size
; i
++) {
401 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
415 * Try connect to session daemon with sock_path.
417 * Return 0 on success, else -1
419 static int try_connect_sessiond(const char *sock_path
)
423 /* If socket exist, we check if the daemon listens for connect. */
424 ret
= access(sock_path
, F_OK
);
430 ret
= lttcomm_connect_unix_sock(sock_path
);
436 ret
= lttcomm_close_unix_sock(ret
);
438 perror("lttcomm_close_unix_sock");
448 * Set sessiond socket path by putting it in the global sessiond_sock_path
451 * Returns 0 on success, negative value on failure (the sessiond socket path
452 * is somehow too long or ENOMEM).
454 static int set_session_daemon_path(void)
456 int in_tgroup
= 0; /* In tracing group */
462 /* Are we in the tracing group ? */
463 in_tgroup
= check_tracing_group(tracing_group
);
466 if ((uid
== 0) || in_tgroup
) {
467 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
468 sizeof(sessiond_sock_path
));
476 ret
= try_connect_sessiond(sessiond_sock_path
);
480 /* Global session daemon not available... */
482 /* ...or not in tracing group (and not root), default */
485 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
486 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
488 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
489 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
490 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
502 * Connect to the LTTng session daemon.
504 * On success, return 0. On error, return -1.
506 static int connect_sessiond(void)
510 ret
= set_session_daemon_path();
515 /* Connect to the sesssion daemon */
516 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
521 sessiond_socket
= ret
;
531 * Clean disconnect from the session daemon.
532 * On success, return 0. On error, return -1.
534 static int disconnect_sessiond(void)
539 ret
= lttcomm_close_unix_sock(sessiond_socket
);
548 * Ask the session daemon a specific command and put the data into buf.
549 * Takes extra var. len. data as input to send to the session daemon.
551 * Return size of data (only payload, not header) or a negative error code.
553 static int 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 * Ask the session daemon a specific command and put the data into buf.
633 * Return size of data (only payload, not header) or a negative error code.
635 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
637 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
641 * Create lttng handle and return pointer.
642 * The returned pointer will be NULL in case of malloc() error.
644 struct lttng_handle
*lttng_create_handle(const char *session_name
,
645 struct lttng_domain
*domain
)
647 struct lttng_handle
*handle
= NULL
;
649 if (domain
== NULL
) {
653 handle
= malloc(sizeof(struct lttng_handle
));
654 if (handle
== NULL
) {
655 PERROR("malloc handle");
659 /* Copy session name */
660 copy_string(handle
->session_name
, session_name
,
661 sizeof(handle
->session_name
));
663 /* Copy lttng domain */
664 copy_lttng_domain(&handle
->domain
, domain
);
671 * Destroy handle by free(3) the pointer.
673 void lttng_destroy_handle(struct lttng_handle
*handle
)
679 * Register an outside consumer.
680 * Returns size of returned session payload data or a negative error code.
682 int lttng_register_consumer(struct lttng_handle
*handle
,
683 const char *socket_path
)
685 struct lttcomm_session_msg lsm
;
687 if (handle
== NULL
|| socket_path
== NULL
) {
688 return -LTTNG_ERR_INVALID
;
691 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
692 copy_string(lsm
.session
.name
, handle
->session_name
,
693 sizeof(lsm
.session
.name
));
694 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
696 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
698 return ask_sessiond(&lsm
, NULL
);
702 * Start tracing for all traces of the session.
703 * Returns size of returned session payload data or a negative error code.
705 int lttng_start_tracing(const char *session_name
)
707 struct lttcomm_session_msg lsm
;
709 if (session_name
== NULL
) {
710 return -LTTNG_ERR_INVALID
;
713 lsm
.cmd_type
= LTTNG_START_TRACE
;
715 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
717 return ask_sessiond(&lsm
, NULL
);
721 * Stop tracing for all traces of the session.
723 static int _lttng_stop_tracing(const char *session_name
, int wait
)
726 struct lttcomm_session_msg lsm
;
728 if (session_name
== NULL
) {
729 return -LTTNG_ERR_INVALID
;
732 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
734 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
736 ret
= ask_sessiond(&lsm
, NULL
);
737 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
745 _MSG("Waiting for data availability");
748 /* Check for data availability */
750 data_ret
= lttng_data_pending(session_name
);
752 /* Return the data available call error. */
758 * Data sleep time before retrying (in usec). Don't sleep if the call
759 * returned value indicates availability.
762 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
766 } while (data_ret
!= 0);
776 * Stop tracing and wait for data availability.
778 int lttng_stop_tracing(const char *session_name
)
780 return _lttng_stop_tracing(session_name
, 1);
784 * Stop tracing but _don't_ wait for data availability.
786 int lttng_stop_tracing_no_wait(const char *session_name
)
788 return _lttng_stop_tracing(session_name
, 0);
792 * Add context to a channel.
794 * If the given channel is NULL, add the contexts to all channels.
795 * The event_name param is ignored.
797 * Returns the size of the returned payload data or a negative error code.
799 int lttng_add_context(struct lttng_handle
*handle
,
800 struct lttng_event_context
*ctx
, const char *event_name
,
801 const char *channel_name
)
803 struct lttcomm_session_msg lsm
;
805 /* Safety check. Both are mandatory */
806 if (handle
== NULL
|| ctx
== NULL
) {
807 return -LTTNG_ERR_INVALID
;
810 memset(&lsm
, 0, sizeof(lsm
));
812 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
814 /* Copy channel name */
815 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
816 sizeof(lsm
.u
.context
.channel_name
));
818 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
820 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
822 copy_string(lsm
.session
.name
, handle
->session_name
,
823 sizeof(lsm
.session
.name
));
825 return ask_sessiond(&lsm
, NULL
);
829 * Enable event(s) for a channel.
830 * If no event name is specified, all events are enabled.
831 * If no channel name is specified, the default 'channel0' is used.
832 * Returns size of returned session payload data or a negative error code.
834 int lttng_enable_event(struct lttng_handle
*handle
,
835 struct lttng_event
*ev
, const char *channel_name
)
837 struct lttcomm_session_msg lsm
;
839 if (handle
== NULL
|| ev
== NULL
) {
840 return -LTTNG_ERR_INVALID
;
843 memset(&lsm
, 0, sizeof(lsm
));
845 /* If no channel name, we put the default name */
846 if (channel_name
== NULL
) {
847 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
848 sizeof(lsm
.u
.enable
.channel_name
));
850 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
851 sizeof(lsm
.u
.enable
.channel_name
));
854 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
856 if (ev
->name
[0] != '\0') {
857 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
859 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
861 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
863 copy_string(lsm
.session
.name
, handle
->session_name
,
864 sizeof(lsm
.session
.name
));
866 return ask_sessiond(&lsm
, NULL
);
870 * Create or enable an event with a filter expression.
872 * Return negative error value on error.
873 * Return size of returned session payload data if OK.
875 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
876 struct lttng_event
*event
, const char *channel_name
,
877 const char *filter_expression
)
879 struct lttcomm_session_msg lsm
;
880 struct filter_parser_ctx
*ctx
;
884 if (!filter_expression
) {
886 * Fall back to normal event enabling if no filter
889 return lttng_enable_event(handle
, event
, channel_name
);
893 * Empty filter string will always be rejected by the parser
894 * anyway, so treat this corner-case early to eliminate
895 * lttng_fmemopen error for 0-byte allocation.
897 if (handle
== NULL
|| filter_expression
[0] == '\0') {
898 return -LTTNG_ERR_INVALID
;
902 * casting const to non-const, as the underlying function will
903 * use it in read-only mode.
905 fmem
= lttng_fmemopen((void *) filter_expression
,
906 strlen(filter_expression
), "r");
908 fprintf(stderr
, "Error opening memory as stream\n");
909 return -LTTNG_ERR_FILTER_NOMEM
;
911 ctx
= filter_parser_ctx_alloc(fmem
);
913 fprintf(stderr
, "Error allocating parser\n");
914 ret
= -LTTNG_ERR_FILTER_NOMEM
;
917 ret
= filter_parser_ctx_append_ast(ctx
);
919 fprintf(stderr
, "Parse error\n");
920 ret
= -LTTNG_ERR_FILTER_INVAL
;
923 ret
= filter_visitor_set_parent(ctx
);
925 fprintf(stderr
, "Set parent error\n");
926 ret
= -LTTNG_ERR_FILTER_INVAL
;
930 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
933 fprintf(stderr
, "XML print error\n");
934 ret
= -LTTNG_ERR_FILTER_INVAL
;
939 dbg_printf("Generating IR... ");
941 ret
= filter_visitor_ir_generate(ctx
);
943 fprintf(stderr
, "Generate IR error\n");
944 ret
= -LTTNG_ERR_FILTER_INVAL
;
947 dbg_printf("done\n");
949 dbg_printf("Validating IR... ");
951 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
953 ret
= -LTTNG_ERR_FILTER_INVAL
;
956 dbg_printf("done\n");
958 dbg_printf("Generating bytecode... ");
960 ret
= filter_visitor_bytecode_generate(ctx
);
962 fprintf(stderr
, "Generate bytecode error\n");
963 ret
= -LTTNG_ERR_FILTER_INVAL
;
966 dbg_printf("done\n");
967 dbg_printf("Size of bytecode generated: %u bytes.\n",
968 bytecode_get_len(&ctx
->bytecode
->b
));
970 memset(&lsm
, 0, sizeof(lsm
));
972 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
974 /* Copy channel name */
975 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
976 sizeof(lsm
.u
.enable
.channel_name
));
977 /* Copy event name */
979 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
982 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
983 + bytecode_get_len(&ctx
->bytecode
->b
);
985 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
987 copy_string(lsm
.session
.name
, handle
->session_name
,
988 sizeof(lsm
.session
.name
));
990 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
991 lsm
.u
.enable
.bytecode_len
, NULL
);
993 filter_bytecode_free(ctx
);
995 filter_parser_ctx_free(ctx
);
996 if (fclose(fmem
) != 0) {
1002 filter_bytecode_free(ctx
);
1003 filter_ir_free(ctx
);
1004 filter_parser_ctx_free(ctx
);
1006 if (fclose(fmem
) != 0) {
1013 * Disable event(s) of a channel and domain.
1014 * If no event name is specified, all events are disabled.
1015 * If no channel name is specified, the default 'channel0' is used.
1016 * Returns size of returned session payload data or a negative error code.
1018 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1019 const char *channel_name
)
1021 struct lttcomm_session_msg lsm
;
1023 if (handle
== NULL
) {
1024 return -LTTNG_ERR_INVALID
;
1027 memset(&lsm
, 0, sizeof(lsm
));
1030 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1031 sizeof(lsm
.u
.disable
.channel_name
));
1033 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
1034 sizeof(lsm
.u
.disable
.channel_name
));
1037 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1040 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
1041 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1043 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
1046 copy_string(lsm
.session
.name
, handle
->session_name
,
1047 sizeof(lsm
.session
.name
));
1049 return ask_sessiond(&lsm
, NULL
);
1053 * Enable channel per domain
1054 * Returns size of returned session payload data or a negative error code.
1056 int lttng_enable_channel(struct lttng_handle
*handle
,
1057 struct lttng_channel
*chan
)
1059 struct lttcomm_session_msg lsm
;
1062 * NULL arguments are forbidden. No default values.
1064 if (handle
== NULL
|| chan
== NULL
) {
1065 return -LTTNG_ERR_INVALID
;
1068 memset(&lsm
, 0, sizeof(lsm
));
1070 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1072 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1074 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1076 copy_string(lsm
.session
.name
, handle
->session_name
,
1077 sizeof(lsm
.session
.name
));
1079 return ask_sessiond(&lsm
, NULL
);
1083 * All tracing will be stopped for registered events of the channel.
1084 * Returns size of returned session payload data or a negative error code.
1086 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1088 struct lttcomm_session_msg lsm
;
1090 /* Safety check. Both are mandatory */
1091 if (handle
== NULL
|| name
== NULL
) {
1092 return -LTTNG_ERR_INVALID
;
1095 memset(&lsm
, 0, sizeof(lsm
));
1097 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1099 copy_string(lsm
.u
.disable
.channel_name
, name
,
1100 sizeof(lsm
.u
.disable
.channel_name
));
1102 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1104 copy_string(lsm
.session
.name
, handle
->session_name
,
1105 sizeof(lsm
.session
.name
));
1107 return ask_sessiond(&lsm
, NULL
);
1111 * Lists all available tracepoints of domain.
1112 * Sets the contents of the events array.
1113 * Returns the number of lttng_event entries in events;
1114 * on error, returns a negative value.
1116 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1117 struct lttng_event
**events
)
1120 struct lttcomm_session_msg lsm
;
1122 if (handle
== NULL
) {
1123 return -LTTNG_ERR_INVALID
;
1126 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1127 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1129 ret
= ask_sessiond(&lsm
, (void **) events
);
1134 return ret
/ sizeof(struct lttng_event
);
1138 * Lists all available tracepoint fields of domain.
1139 * Sets the contents of the event field array.
1140 * Returns the number of lttng_event_field entries in events;
1141 * on error, returns a negative value.
1143 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1144 struct lttng_event_field
**fields
)
1147 struct lttcomm_session_msg lsm
;
1149 if (handle
== NULL
) {
1150 return -LTTNG_ERR_INVALID
;
1153 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1154 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1156 ret
= ask_sessiond(&lsm
, (void **) fields
);
1161 return ret
/ sizeof(struct lttng_event_field
);
1165 * Returns a human readable string describing
1166 * the error code (a negative value).
1168 const char *lttng_strerror(int code
)
1170 return error_get_str(code
);
1174 * Create a brand new session using name and url for destination.
1176 * Returns LTTNG_OK on success or a negative error code.
1178 int lttng_create_session(const char *name
, const char *url
)
1182 struct lttcomm_session_msg lsm
;
1183 struct lttng_uri
*uris
= NULL
;
1186 return -LTTNG_ERR_INVALID
;
1189 memset(&lsm
, 0, sizeof(lsm
));
1191 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1192 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1194 /* There should never be a data URL */
1195 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1197 return -LTTNG_ERR_INVALID
;
1200 lsm
.u
.uri
.size
= size
;
1202 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1210 * Destroy session using name.
1211 * Returns size of returned session payload data or a negative error code.
1213 int lttng_destroy_session(const char *session_name
)
1215 struct lttcomm_session_msg lsm
;
1217 if (session_name
== NULL
) {
1218 return -LTTNG_ERR_INVALID
;
1221 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1223 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1225 return ask_sessiond(&lsm
, NULL
);
1229 * Ask the session daemon for all available sessions.
1230 * Sets the contents of the sessions array.
1231 * Returns the number of lttng_session entries in sessions;
1232 * on error, returns a negative value.
1234 int lttng_list_sessions(struct lttng_session
**sessions
)
1237 struct lttcomm_session_msg lsm
;
1239 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1240 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1245 return ret
/ sizeof(struct lttng_session
);
1249 * Ask the session daemon for all available domains of a session.
1250 * Sets the contents of the domains array.
1251 * Returns the number of lttng_domain entries in domains;
1252 * on error, returns a negative value.
1254 int lttng_list_domains(const char *session_name
,
1255 struct lttng_domain
**domains
)
1258 struct lttcomm_session_msg lsm
;
1260 if (session_name
== NULL
) {
1261 return -LTTNG_ERR_INVALID
;
1264 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1266 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1268 ret
= ask_sessiond(&lsm
, (void**) domains
);
1273 return ret
/ sizeof(struct lttng_domain
);
1277 * Ask the session daemon for all available channels of a session.
1278 * Sets the contents of the channels array.
1279 * Returns the number of lttng_channel entries in channels;
1280 * on error, returns a negative value.
1282 int lttng_list_channels(struct lttng_handle
*handle
,
1283 struct lttng_channel
**channels
)
1286 struct lttcomm_session_msg lsm
;
1288 if (handle
== NULL
) {
1289 return -LTTNG_ERR_INVALID
;
1292 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1293 copy_string(lsm
.session
.name
, handle
->session_name
,
1294 sizeof(lsm
.session
.name
));
1296 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1298 ret
= ask_sessiond(&lsm
, (void**) channels
);
1303 return ret
/ sizeof(struct lttng_channel
);
1307 * Ask the session daemon for all available events of a session channel.
1308 * Sets the contents of the events array.
1309 * Returns the number of lttng_event entries in events;
1310 * on error, returns a negative value.
1312 int lttng_list_events(struct lttng_handle
*handle
,
1313 const char *channel_name
, struct lttng_event
**events
)
1316 struct lttcomm_session_msg lsm
;
1318 /* Safety check. An handle and channel name are mandatory */
1319 if (handle
== NULL
|| channel_name
== NULL
) {
1320 return -LTTNG_ERR_INVALID
;
1323 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1324 copy_string(lsm
.session
.name
, handle
->session_name
,
1325 sizeof(lsm
.session
.name
));
1326 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1327 sizeof(lsm
.u
.list
.channel_name
));
1329 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1331 ret
= ask_sessiond(&lsm
, (void**) events
);
1336 return ret
/ sizeof(struct lttng_event
);
1340 * Sets the tracing_group variable with name.
1341 * This function allocates memory pointed to by tracing_group.
1342 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1344 int lttng_set_tracing_group(const char *name
)
1347 return -LTTNG_ERR_INVALID
;
1350 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1351 return -LTTNG_ERR_FATAL
;
1358 * Returns size of returned session payload data or a negative error code.
1360 int lttng_calibrate(struct lttng_handle
*handle
,
1361 struct lttng_calibrate
*calibrate
)
1363 struct lttcomm_session_msg lsm
;
1365 /* Safety check. NULL pointer are forbidden */
1366 if (handle
== NULL
|| calibrate
== NULL
) {
1367 return -LTTNG_ERR_INVALID
;
1370 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1371 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1373 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1375 return ask_sessiond(&lsm
, NULL
);
1379 * Set default channel attributes.
1380 * If either or both of the arguments are null, attr content is zeroe'd.
1382 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1383 struct lttng_channel_attr
*attr
)
1386 if (attr
== NULL
|| domain
== NULL
) {
1390 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1392 /* Same for all domains. */
1393 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1394 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1395 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1397 switch (domain
->type
) {
1398 case LTTNG_DOMAIN_KERNEL
:
1399 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1400 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1401 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1402 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1403 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1405 case LTTNG_DOMAIN_UST
:
1406 switch (domain
->buf_type
) {
1407 case LTTNG_BUFFER_PER_UID
:
1408 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1409 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1410 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1411 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1412 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1414 case LTTNG_BUFFER_PER_PID
:
1416 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1417 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1418 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1419 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1420 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1424 /* Default behavior: leave set to 0. */
1430 * Check if session daemon is alive.
1432 * Return 1 if alive or 0 if not.
1433 * On error returns a negative value.
1435 int lttng_session_daemon_alive(void)
1439 ret
= set_session_daemon_path();
1445 if (*sessiond_sock_path
== '\0') {
1447 * No socket path set. Weird error which means the constructor was not
1453 ret
= try_connect_sessiond(sessiond_sock_path
);
1464 * Set URL for a consumer for a session and domain.
1466 * Return 0 on success, else a negative value.
1468 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1469 const char *control_url
, const char *data_url
)
1473 struct lttcomm_session_msg lsm
;
1474 struct lttng_uri
*uris
= NULL
;
1476 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1477 return -LTTNG_ERR_INVALID
;
1480 memset(&lsm
, 0, sizeof(lsm
));
1482 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1484 copy_string(lsm
.session
.name
, handle
->session_name
,
1485 sizeof(lsm
.session
.name
));
1486 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1488 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1490 return -LTTNG_ERR_INVALID
;
1493 lsm
.u
.uri
.size
= size
;
1495 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1505 int lttng_enable_consumer(struct lttng_handle
*handle
)
1513 int lttng_disable_consumer(struct lttng_handle
*handle
)
1519 * Set health socket path by putting it in the global health_sock_path
1522 * Returns 0 on success or assert(0) on ENOMEM.
1524 static int set_health_socket_path(void)
1526 int in_tgroup
= 0; /* In tracing group */
1533 /* Are we in the tracing group ? */
1534 in_tgroup
= check_tracing_group(tracing_group
);
1537 if ((uid
== 0) || in_tgroup
) {
1538 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1539 sizeof(health_sock_path
));
1546 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1547 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1549 home
= utils_get_home_dir();
1551 /* Fallback in /tmp .. */
1555 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1556 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1557 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1558 /* ENOMEM at this point... just kill the control lib. */
1567 * Check session daemon health for a specific health component.
1569 * Return 0 if health is OK or else 1 if BAD.
1571 * Any other negative value is a lttng error code which can be translated with
1574 int lttng_health_check(enum lttng_health_component c
)
1577 struct lttcomm_health_msg msg
;
1578 struct lttcomm_health_data reply
;
1580 /* Connect to the sesssion daemon */
1581 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1583 ret
= -LTTNG_ERR_NO_SESSIOND
;
1587 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1590 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1592 ret
= -LTTNG_ERR_FATAL
;
1596 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1598 ret
= -LTTNG_ERR_FATAL
;
1602 ret
= reply
.ret_code
;
1608 closeret
= close(sock
);
1617 * This is an extension of create session that is ONLY and SHOULD only be used
1618 * by the lttng command line program. It exists to avoid using URI parsing in
1621 * We need the date and time for the trace path subdirectory for the case where
1622 * the user does NOT define one using either -o or -U. Using the normal
1623 * lttng_create_session API call, we have no clue on the session daemon side if
1624 * the URL was generated automatically by the client or define by the user.
1626 * So this function "wrapper" is hidden from the public API, takes the datetime
1627 * string and appends it if necessary to the URI subdirectory before sending it
1628 * to the session daemon.
1630 * With this extra function, the lttng_create_session call behavior is not
1631 * changed and the timestamp is appended to the URI on the session daemon side
1634 int _lttng_create_session_ext(const char *name
, const char *url
,
1635 const char *datetime
)
1639 struct lttcomm_session_msg lsm
;
1640 struct lttng_uri
*uris
= NULL
;
1642 if (name
== NULL
|| datetime
== NULL
|| url
== NULL
) {
1643 return -LTTNG_ERR_INVALID
;
1646 memset(&lsm
, 0, sizeof(lsm
));
1648 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1649 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1651 /* There should never be a data URL */
1652 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1654 ret
= -LTTNG_ERR_INVALID
;
1658 lsm
.u
.uri
.size
= size
;
1660 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1661 /* Don't append datetime if the name was automatically created. */
1662 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1663 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1664 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1667 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1670 PERROR("snprintf uri subdir");
1671 ret
= -LTTNG_ERR_FATAL
;
1676 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1685 * For a given session name, this call checks if the data is ready to be read
1686 * or is still being extracted by the consumer(s) hence not ready to be used by
1689 int lttng_data_pending(const char *session_name
)
1692 struct lttcomm_session_msg lsm
;
1694 if (session_name
== NULL
) {
1695 return -LTTNG_ERR_INVALID
;
1698 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1700 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1702 ret
= ask_sessiond(&lsm
, NULL
);
1705 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1706 * getting -1 means that the reply ret_code was 1 thus meaning that the
1707 * data is available. Yes it is hackish but for now this is the only way.
1719 static void __attribute__((constructor
)) init()
1721 /* Set default session group */
1722 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1723 /* Set socket for health check */
1724 (void) set_health_socket_path();
1730 static void __attribute__((destructor
)) lttng_ctl_exit()
1732 free(tracing_group
);