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 <lttng/lttng.h>
37 #include "filter/filter-ast.h"
38 #include "filter/filter-parser.h"
39 #include "filter/filter-bytecode.h"
40 #include "filter/memstream.h"
43 static const int print_xml
= 1;
44 #define dbg_printf(fmt, args...) \
45 printf("[debug liblttng-ctl] " fmt, ## args)
47 static const int print_xml
= 0;
48 #define dbg_printf(fmt, args...) \
50 /* do nothing but check printf format */ \
52 printf("[debug liblttnctl] " fmt, ## args); \
57 /* Socket to session daemon for communication */
58 static int sessiond_socket
;
59 static char sessiond_sock_path
[PATH_MAX
];
60 static char health_sock_path
[PATH_MAX
];
63 static char *tracing_group
;
69 * Those two variables are used by error.h to silent or control the verbosity of
70 * error message. They are global to the library so application linking with it
71 * are able to compile correctly and also control verbosity of the library.
74 int lttng_opt_verbose
;
76 static void set_default_url_attr(struct lttng_uri
*uri
,
77 enum lttng_stream_type stype
)
80 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
81 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
82 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
87 * Parse a string URL and creates URI(s) returning the size of the populated
90 static ssize_t
parse_str_urls_to_uri(const char *ctrl_url
, const char *data_url
,
91 struct lttng_uri
**uris
)
93 unsigned int equal
= 1, idx
= 0;
94 /* Add the "file://" size to the URL maximum size */
95 char url
[PATH_MAX
+ 7];
96 ssize_t size_ctrl
= 0, size_data
= 0, size
;
97 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
98 struct lttng_uri
*tmp_uris
= NULL
;
100 /* No URL(s) is allowed. This means that the consumer will be disabled. */
101 if (ctrl_url
== NULL
&& data_url
== NULL
) {
105 /* Check if URLs are equal and if so, only use the control URL */
106 if (ctrl_url
&& data_url
) {
107 equal
= !strcmp(ctrl_url
, data_url
);
111 * Since we allow the str_url to be a full local filesystem path, we are
112 * going to create a valid file:// URL if it's the case.
114 * Check if first character is a '/' or else reject the URL.
116 if (ctrl_url
&& ctrl_url
[0] == '/') {
119 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
121 PERROR("snprintf file url");
127 /* Parse the control URL if there is one */
129 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
131 ERR("Unable to parse the URL %s", ctrl_url
);
135 /* At this point, we know there is at least one URI in the array */
136 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
138 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
139 ERR("Can not have a data URL when destination is file://");
143 /* URL are not equal but the control URL uses a net:// protocol */
144 if (size_ctrl
== 2) {
146 ERR("Control URL uses the net:// protocol and the data URL is "
147 "different. Not allowed.");
150 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
152 * The data_url and ctrl_url are equal and the ctrl_url
153 * contains a net:// protocol so we just skip the data part.
161 /* We have to parse the data URL in this case */
162 size_data
= uri_parse(data_url
, &data_uris
);
164 ERR("Unable to parse the URL %s", data_url
);
166 } else if (size_data
== 2) {
167 ERR("Data URL can not be set with the net[4|6]:// protocol");
171 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
174 /* Compute total size */
175 size
= size_ctrl
+ size_data
;
177 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
178 if (tmp_uris
== NULL
) {
179 PERROR("zmalloc uris");
184 /* It's possible the control URIs array contains more than one URI */
185 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
190 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
206 * Copy string from src to dst and enforce null terminated byte.
208 static void copy_string(char *dst
, const char *src
, size_t len
)
211 strncpy(dst
, src
, len
);
212 /* Enforce the NULL terminated byte */
220 * Copy domain to lttcomm_session_msg domain.
222 * If domain is unknown, default domain will be the kernel.
224 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
228 case LTTNG_DOMAIN_KERNEL
:
229 case LTTNG_DOMAIN_UST
:
231 case LTTNG_DOMAIN_UST_EXEC_NAME:
232 case LTTNG_DOMAIN_UST_PID:
233 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
235 memcpy(dst
, src
, sizeof(struct lttng_domain
));
238 memset(dst
, 0, sizeof(struct lttng_domain
));
245 * Send lttcomm_session_msg to the session daemon.
247 * On success, returns the number of bytes sent (>=0)
248 * On error, returns -1
250 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
255 ret
= -LTTNG_ERR_NO_SESSIOND
;
259 DBG("LSM cmd type : %d", lsm
->cmd_type
);
261 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
262 sizeof(struct lttcomm_session_msg
));
264 ret
= -LTTNG_ERR_FATAL
;
272 * Send var len data to the session daemon.
274 * On success, returns the number of bytes sent (>=0)
275 * On error, returns -1
277 static int send_session_varlen(void *data
, size_t len
)
282 ret
= -LTTNG_ERR_NO_SESSIOND
;
291 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
293 ret
= -LTTNG_ERR_FATAL
;
301 * Receive data from the sessiond socket.
303 * On success, returns the number of bytes received (>=0)
304 * On error, returns -1 (recvmsg() error) or -ENOTCONN
306 static int recv_data_sessiond(void *buf
, size_t len
)
311 ret
= -LTTNG_ERR_NO_SESSIOND
;
315 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
317 ret
= -LTTNG_ERR_FATAL
;
325 * Check if we are in the specified group.
327 * If yes return 1, else return -1.
329 static int check_tracing_group(const char *grp_name
)
331 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
333 int grp_list_size
, grp_id
, i
;
336 /* Get GID of group 'tracing' */
337 grp_tracing
= getgrnam(grp_name
);
339 /* If grp_tracing is NULL, the group does not exist. */
343 /* Get number of supplementary group IDs */
344 grp_list_size
= getgroups(0, NULL
);
345 if (grp_list_size
< 0) {
350 /* Alloc group list of the right size */
351 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
356 grp_id
= getgroups(grp_list_size
, grp_list
);
362 for (i
= 0; i
< grp_list_size
; i
++) {
363 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
377 * Try connect to session daemon with sock_path.
379 * Return 0 on success, else -1
381 static int try_connect_sessiond(const char *sock_path
)
385 /* If socket exist, we check if the daemon listens for connect. */
386 ret
= access(sock_path
, F_OK
);
392 ret
= lttcomm_connect_unix_sock(sock_path
);
398 ret
= lttcomm_close_unix_sock(ret
);
400 perror("lttcomm_close_unix_sock");
410 * Set sessiond socket path by putting it in the global sessiond_sock_path
413 * Returns 0 on success, negative value on failure (the sessiond socket path
414 * is somehow too long or ENOMEM).
416 static int set_session_daemon_path(void)
418 int in_tgroup
= 0; /* In tracing group */
424 /* Are we in the tracing group ? */
425 in_tgroup
= check_tracing_group(tracing_group
);
428 if ((uid
== 0) || in_tgroup
) {
429 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
430 sizeof(sessiond_sock_path
));
438 ret
= try_connect_sessiond(sessiond_sock_path
);
442 /* Global session daemon not available... */
444 /* ...or not in tracing group (and not root), default */
447 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
448 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
450 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
451 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
452 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
464 * Connect to the LTTng session daemon.
466 * On success, return 0. On error, return -1.
468 static int connect_sessiond(void)
472 ret
= set_session_daemon_path();
477 /* Connect to the sesssion daemon */
478 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
483 sessiond_socket
= ret
;
493 * Clean disconnect from the session daemon.
494 * On success, return 0. On error, return -1.
496 static int disconnect_sessiond(void)
501 ret
= lttcomm_close_unix_sock(sessiond_socket
);
510 * Ask the session daemon a specific command and put the data into buf.
511 * Takes extra var. len. data as input to send to the session daemon.
513 * Return size of data (only payload, not header) or a negative error code.
515 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
516 void *vardata
, size_t varlen
, void **buf
)
521 struct lttcomm_lttng_msg llm
;
523 ret
= connect_sessiond();
525 ret
= -LTTNG_ERR_NO_SESSIOND
;
529 /* Send command to session daemon */
530 ret
= send_session_msg(lsm
);
532 /* Ret value is a valid lttng error code. */
535 /* Send var len data */
536 ret
= send_session_varlen(vardata
, varlen
);
538 /* Ret value is a valid lttng error code. */
542 /* Get header from data transmission */
543 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
545 /* Ret value is a valid lttng error code. */
549 /* Check error code if OK */
550 if (llm
.ret_code
!= LTTNG_OK
) {
555 size
= llm
.data_size
;
557 /* If client free with size 0 */
565 data
= (void*) malloc(size
);
567 /* Get payload data */
568 ret
= recv_data_sessiond(data
, size
);
575 * Extra protection not to dereference a NULL pointer. If buf is NULL at
576 * this point, an error is returned and data is freed.
579 ret
= -LTTNG_ERR_INVALID
;
588 disconnect_sessiond();
593 * Ask the session daemon a specific command and put the data into buf.
595 * Return size of data (only payload, not header) or a negative error code.
597 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
599 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
603 * Create lttng handle and return pointer.
604 * The returned pointer will be NULL in case of malloc() error.
606 struct lttng_handle
*lttng_create_handle(const char *session_name
,
607 struct lttng_domain
*domain
)
609 struct lttng_handle
*handle
= NULL
;
611 if (domain
== NULL
) {
615 handle
= malloc(sizeof(struct lttng_handle
));
616 if (handle
== NULL
) {
617 PERROR("malloc handle");
621 /* Copy session name */
622 copy_string(handle
->session_name
, session_name
,
623 sizeof(handle
->session_name
));
625 /* Copy lttng domain */
626 copy_lttng_domain(&handle
->domain
, domain
);
633 * Destroy handle by free(3) the pointer.
635 void lttng_destroy_handle(struct lttng_handle
*handle
)
643 * Register an outside consumer.
644 * Returns size of returned session payload data or a negative error code.
646 int lttng_register_consumer(struct lttng_handle
*handle
,
647 const char *socket_path
)
649 struct lttcomm_session_msg lsm
;
651 if (handle
== NULL
|| socket_path
== NULL
) {
652 return -LTTNG_ERR_INVALID
;
655 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
656 copy_string(lsm
.session
.name
, handle
->session_name
,
657 sizeof(lsm
.session
.name
));
658 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
660 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
662 return ask_sessiond(&lsm
, NULL
);
666 * Start tracing for all traces of the session.
667 * Returns size of returned session payload data or a negative error code.
669 int lttng_start_tracing(const char *session_name
)
671 struct lttcomm_session_msg lsm
;
673 if (session_name
== NULL
) {
674 return -LTTNG_ERR_INVALID
;
677 lsm
.cmd_type
= LTTNG_START_TRACE
;
679 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
681 return ask_sessiond(&lsm
, NULL
);
685 * Stop tracing for all traces of the session.
687 static int _lttng_stop_tracing(const char *session_name
, int wait
)
690 struct lttcomm_session_msg lsm
;
692 if (session_name
== NULL
) {
693 return -LTTNG_ERR_INVALID
;
696 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
698 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
700 ret
= ask_sessiond(&lsm
, NULL
);
701 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
709 _MSG("Waiting for data availability");
711 /* Check for data availability */
713 data_ret
= lttng_data_pending(session_name
);
715 /* Return the data available call error. */
721 * Data sleep time before retrying (in usec). Don't sleep if the call
722 * returned value indicates availability.
725 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
728 } while (data_ret
!= 0);
738 * Stop tracing and wait for data availability.
740 int lttng_stop_tracing(const char *session_name
)
742 return _lttng_stop_tracing(session_name
, 1);
746 * Stop tracing but _don't_ wait for data availability.
748 int lttng_stop_tracing_no_wait(const char *session_name
)
750 return _lttng_stop_tracing(session_name
, 0);
754 * Add context to a channel.
756 * If the given channel is NULL, add the contexts to all channels.
757 * The event_name param is ignored.
759 * Returns the size of the returned payload data or a negative error code.
761 int lttng_add_context(struct lttng_handle
*handle
,
762 struct lttng_event_context
*ctx
, const char *event_name
,
763 const char *channel_name
)
765 struct lttcomm_session_msg lsm
;
767 /* Safety check. Both are mandatory */
768 if (handle
== NULL
|| ctx
== NULL
) {
769 return -LTTNG_ERR_INVALID
;
772 memset(&lsm
, 0, sizeof(lsm
));
774 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
776 /* Copy channel name */
777 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
778 sizeof(lsm
.u
.context
.channel_name
));
780 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
782 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
784 copy_string(lsm
.session
.name
, handle
->session_name
,
785 sizeof(lsm
.session
.name
));
787 return ask_sessiond(&lsm
, NULL
);
791 * Enable event(s) for a channel.
792 * If no event name is specified, all events are enabled.
793 * If no channel name is specified, the default 'channel0' is used.
794 * Returns size of returned session payload data or a negative error code.
796 int lttng_enable_event(struct lttng_handle
*handle
,
797 struct lttng_event
*ev
, const char *channel_name
)
799 struct lttcomm_session_msg lsm
;
801 if (handle
== NULL
|| ev
== NULL
) {
802 return -LTTNG_ERR_INVALID
;
805 memset(&lsm
, 0, sizeof(lsm
));
807 /* If no channel name, we put the default name */
808 if (channel_name
== NULL
) {
809 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
810 sizeof(lsm
.u
.enable
.channel_name
));
812 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
813 sizeof(lsm
.u
.enable
.channel_name
));
816 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
818 if (ev
->name
[0] != '\0') {
819 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
821 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
823 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
825 copy_string(lsm
.session
.name
, handle
->session_name
,
826 sizeof(lsm
.session
.name
));
828 return ask_sessiond(&lsm
, NULL
);
832 * Create or enable an event with a filter expression.
834 * Return negative error value on error.
835 * Return size of returned session payload data if OK.
837 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
838 struct lttng_event
*event
, const char *channel_name
,
839 const char *filter_expression
)
841 struct lttcomm_session_msg lsm
;
842 struct filter_parser_ctx
*ctx
;
846 if (!filter_expression
) {
848 * Fall back to normal event enabling if no filter
851 return lttng_enable_event(handle
, event
, channel_name
);
855 * Empty filter string will always be rejected by the parser
856 * anyway, so treat this corner-case early to eliminate
857 * lttng_fmemopen error for 0-byte allocation.
859 if (handle
== NULL
|| filter_expression
[0] == '\0') {
860 return -LTTNG_ERR_INVALID
;
864 * casting const to non-const, as the underlying function will
865 * use it in read-only mode.
867 fmem
= lttng_fmemopen((void *) filter_expression
,
868 strlen(filter_expression
), "r");
870 fprintf(stderr
, "Error opening memory as stream\n");
871 return -LTTNG_ERR_FILTER_NOMEM
;
873 ctx
= filter_parser_ctx_alloc(fmem
);
875 fprintf(stderr
, "Error allocating parser\n");
876 ret
= -LTTNG_ERR_FILTER_NOMEM
;
879 ret
= filter_parser_ctx_append_ast(ctx
);
881 fprintf(stderr
, "Parse error\n");
882 ret
= -LTTNG_ERR_FILTER_INVAL
;
885 ret
= filter_visitor_set_parent(ctx
);
887 fprintf(stderr
, "Set parent error\n");
888 ret
= -LTTNG_ERR_FILTER_INVAL
;
892 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
895 fprintf(stderr
, "XML print error\n");
896 ret
= -LTTNG_ERR_FILTER_INVAL
;
901 dbg_printf("Generating IR... ");
903 ret
= filter_visitor_ir_generate(ctx
);
905 fprintf(stderr
, "Generate IR error\n");
906 ret
= -LTTNG_ERR_FILTER_INVAL
;
909 dbg_printf("done\n");
911 dbg_printf("Validating IR... ");
913 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
915 ret
= -LTTNG_ERR_FILTER_INVAL
;
918 dbg_printf("done\n");
920 dbg_printf("Generating bytecode... ");
922 ret
= filter_visitor_bytecode_generate(ctx
);
924 fprintf(stderr
, "Generate bytecode error\n");
925 ret
= -LTTNG_ERR_FILTER_INVAL
;
928 dbg_printf("done\n");
929 dbg_printf("Size of bytecode generated: %u bytes.\n",
930 bytecode_get_len(&ctx
->bytecode
->b
));
932 memset(&lsm
, 0, sizeof(lsm
));
934 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
936 /* Copy channel name */
937 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
938 sizeof(lsm
.u
.enable
.channel_name
));
939 /* Copy event name */
941 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
944 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
945 + bytecode_get_len(&ctx
->bytecode
->b
);
947 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
949 copy_string(lsm
.session
.name
, handle
->session_name
,
950 sizeof(lsm
.session
.name
));
952 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
953 lsm
.u
.enable
.bytecode_len
, NULL
);
955 filter_bytecode_free(ctx
);
957 filter_parser_ctx_free(ctx
);
958 if (fclose(fmem
) != 0) {
964 filter_bytecode_free(ctx
);
966 filter_parser_ctx_free(ctx
);
968 if (fclose(fmem
) != 0) {
975 * Disable event(s) of a channel and domain.
976 * If no event name is specified, all events are disabled.
977 * If no channel name is specified, the default 'channel0' is used.
978 * Returns size of returned session payload data or a negative error code.
980 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
981 const char *channel_name
)
983 struct lttcomm_session_msg lsm
;
985 if (handle
== NULL
) {
986 return -LTTNG_ERR_INVALID
;
989 memset(&lsm
, 0, sizeof(lsm
));
992 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
993 sizeof(lsm
.u
.disable
.channel_name
));
995 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
996 sizeof(lsm
.u
.disable
.channel_name
));
999 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1002 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
1003 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1005 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
1008 copy_string(lsm
.session
.name
, handle
->session_name
,
1009 sizeof(lsm
.session
.name
));
1011 return ask_sessiond(&lsm
, NULL
);
1015 * Enable channel per domain
1016 * Returns size of returned session payload data or a negative error code.
1018 int lttng_enable_channel(struct lttng_handle
*handle
,
1019 struct lttng_channel
*chan
)
1021 struct lttcomm_session_msg lsm
;
1024 * NULL arguments are forbidden. No default values.
1026 if (handle
== NULL
|| chan
== NULL
) {
1027 return -LTTNG_ERR_INVALID
;
1030 memset(&lsm
, 0, sizeof(lsm
));
1032 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1034 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1036 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1038 copy_string(lsm
.session
.name
, handle
->session_name
,
1039 sizeof(lsm
.session
.name
));
1041 return ask_sessiond(&lsm
, NULL
);
1045 * All tracing will be stopped for registered events of the channel.
1046 * Returns size of returned session payload data or a negative error code.
1048 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1050 struct lttcomm_session_msg lsm
;
1052 /* Safety check. Both are mandatory */
1053 if (handle
== NULL
|| name
== NULL
) {
1054 return -LTTNG_ERR_INVALID
;
1057 memset(&lsm
, 0, sizeof(lsm
));
1059 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1061 copy_string(lsm
.u
.disable
.channel_name
, name
,
1062 sizeof(lsm
.u
.disable
.channel_name
));
1064 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1066 copy_string(lsm
.session
.name
, handle
->session_name
,
1067 sizeof(lsm
.session
.name
));
1069 return ask_sessiond(&lsm
, NULL
);
1073 * Lists all available tracepoints of domain.
1074 * Sets the contents of the events array.
1075 * Returns the number of lttng_event entries in events;
1076 * on error, returns a negative value.
1078 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1079 struct lttng_event
**events
)
1082 struct lttcomm_session_msg lsm
;
1084 if (handle
== NULL
) {
1085 return -LTTNG_ERR_INVALID
;
1088 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1089 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1091 ret
= ask_sessiond(&lsm
, (void **) events
);
1096 return ret
/ sizeof(struct lttng_event
);
1100 * Lists all available tracepoint fields of domain.
1101 * Sets the contents of the event field array.
1102 * Returns the number of lttng_event_field entries in events;
1103 * on error, returns a negative value.
1105 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1106 struct lttng_event_field
**fields
)
1109 struct lttcomm_session_msg lsm
;
1111 if (handle
== NULL
) {
1112 return -LTTNG_ERR_INVALID
;
1115 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1116 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1118 ret
= ask_sessiond(&lsm
, (void **) fields
);
1123 return ret
/ sizeof(struct lttng_event_field
);
1127 * Returns a human readable string describing
1128 * the error code (a negative value).
1130 const char *lttng_strerror(int code
)
1132 return error_get_str(code
);
1136 * Create a brand new session using name and url for destination.
1138 * Returns LTTNG_OK on success or a negative error code.
1140 int lttng_create_session(const char *name
, const char *url
)
1143 struct lttcomm_session_msg lsm
;
1144 struct lttng_uri
*uris
= NULL
;
1147 return -LTTNG_ERR_INVALID
;
1150 memset(&lsm
, 0, sizeof(lsm
));
1152 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1153 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1155 /* There should never be a data URL */
1156 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1158 return -LTTNG_ERR_INVALID
;
1161 lsm
.u
.uri
.size
= size
;
1163 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1168 * Destroy session using name.
1169 * Returns size of returned session payload data or a negative error code.
1171 int lttng_destroy_session(const char *session_name
)
1173 struct lttcomm_session_msg lsm
;
1175 if (session_name
== NULL
) {
1176 return -LTTNG_ERR_INVALID
;
1179 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1181 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1183 return ask_sessiond(&lsm
, NULL
);
1187 * Ask the session daemon for all available sessions.
1188 * Sets the contents of the sessions array.
1189 * Returns the number of lttng_session entries in sessions;
1190 * on error, returns a negative value.
1192 int lttng_list_sessions(struct lttng_session
**sessions
)
1195 struct lttcomm_session_msg lsm
;
1197 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1198 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1203 return ret
/ sizeof(struct lttng_session
);
1207 * Ask the session daemon for all available domains of a session.
1208 * Sets the contents of the domains array.
1209 * Returns the number of lttng_domain entries in domains;
1210 * on error, returns a negative value.
1212 int lttng_list_domains(const char *session_name
,
1213 struct lttng_domain
**domains
)
1216 struct lttcomm_session_msg lsm
;
1218 if (session_name
== NULL
) {
1219 return -LTTNG_ERR_INVALID
;
1222 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1224 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1226 ret
= ask_sessiond(&lsm
, (void**) domains
);
1231 return ret
/ sizeof(struct lttng_domain
);
1235 * Ask the session daemon for all available channels of a session.
1236 * Sets the contents of the channels array.
1237 * Returns the number of lttng_channel entries in channels;
1238 * on error, returns a negative value.
1240 int lttng_list_channels(struct lttng_handle
*handle
,
1241 struct lttng_channel
**channels
)
1244 struct lttcomm_session_msg lsm
;
1246 if (handle
== NULL
) {
1247 return -LTTNG_ERR_INVALID
;
1250 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1251 copy_string(lsm
.session
.name
, handle
->session_name
,
1252 sizeof(lsm
.session
.name
));
1254 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1256 ret
= ask_sessiond(&lsm
, (void**) channels
);
1261 return ret
/ sizeof(struct lttng_channel
);
1265 * Ask the session daemon for all available events of a session channel.
1266 * Sets the contents of the events array.
1267 * Returns the number of lttng_event entries in events;
1268 * on error, returns a negative value.
1270 int lttng_list_events(struct lttng_handle
*handle
,
1271 const char *channel_name
, struct lttng_event
**events
)
1274 struct lttcomm_session_msg lsm
;
1276 /* Safety check. An handle and channel name are mandatory */
1277 if (handle
== NULL
|| channel_name
== NULL
) {
1278 return -LTTNG_ERR_INVALID
;
1281 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1282 copy_string(lsm
.session
.name
, handle
->session_name
,
1283 sizeof(lsm
.session
.name
));
1284 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1285 sizeof(lsm
.u
.list
.channel_name
));
1287 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1289 ret
= ask_sessiond(&lsm
, (void**) events
);
1294 return ret
/ sizeof(struct lttng_event
);
1298 * Sets the tracing_group variable with name.
1299 * This function allocates memory pointed to by tracing_group.
1300 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1302 int lttng_set_tracing_group(const char *name
)
1305 return -LTTNG_ERR_INVALID
;
1308 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1309 return -LTTNG_ERR_FATAL
;
1316 * Returns size of returned session payload data or a negative error code.
1318 int lttng_calibrate(struct lttng_handle
*handle
,
1319 struct lttng_calibrate
*calibrate
)
1321 struct lttcomm_session_msg lsm
;
1323 /* Safety check. NULL pointer are forbidden */
1324 if (handle
== NULL
|| calibrate
== NULL
) {
1325 return -LTTNG_ERR_INVALID
;
1328 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1329 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1331 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1333 return ask_sessiond(&lsm
, NULL
);
1337 * Set default channel attributes.
1338 * If either or both of the arguments are null, attr content is zeroe'd.
1340 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1341 struct lttng_channel_attr
*attr
)
1344 if (attr
== NULL
|| domain
== NULL
) {
1348 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1350 switch (domain
->type
) {
1351 case LTTNG_DOMAIN_KERNEL
:
1352 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1353 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1354 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1356 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1357 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1358 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1360 case LTTNG_DOMAIN_UST
:
1362 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1363 case LTTNG_DOMAIN_UST_PID
:
1364 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1366 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1367 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1368 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1370 attr
->subbuf_size
= default_get_ust_channel_subbuf_size();
1371 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1372 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1375 /* Default behavior: leave set to 0. */
1381 * Check if session daemon is alive.
1383 * Return 1 if alive or 0 if not.
1384 * On error returns a negative value.
1386 int lttng_session_daemon_alive(void)
1390 ret
= set_session_daemon_path();
1396 if (strlen(sessiond_sock_path
) == 0) {
1398 * No socket path set. Weird error which means the constructor was not
1404 ret
= try_connect_sessiond(sessiond_sock_path
);
1415 * Set URL for a consumer for a session and domain.
1417 * Return 0 on success, else a negative value.
1419 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1420 const char *control_url
, const char *data_url
)
1423 struct lttcomm_session_msg lsm
;
1424 struct lttng_uri
*uris
= NULL
;
1426 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1427 return -LTTNG_ERR_INVALID
;
1430 memset(&lsm
, 0, sizeof(lsm
));
1432 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1434 copy_string(lsm
.session
.name
, handle
->session_name
,
1435 sizeof(lsm
.session
.name
));
1436 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1438 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1440 return -LTTNG_ERR_INVALID
;
1443 lsm
.u
.uri
.size
= size
;
1445 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1450 * Enable consumer for a session and domain.
1452 * Return 0 on success, else a negative value.
1454 int lttng_enable_consumer(struct lttng_handle
*handle
)
1456 struct lttcomm_session_msg lsm
;
1458 if (handle
== NULL
) {
1459 return -LTTNG_ERR_INVALID
;
1462 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1464 copy_string(lsm
.session
.name
, handle
->session_name
,
1465 sizeof(lsm
.session
.name
));
1466 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1468 return ask_sessiond(&lsm
, NULL
);
1472 * Disable consumer for a session and domain.
1474 * Return 0 on success, else a negative value.
1476 int lttng_disable_consumer(struct lttng_handle
*handle
)
1478 struct lttcomm_session_msg lsm
;
1480 if (handle
== NULL
) {
1481 return -LTTNG_ERR_INVALID
;
1484 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1486 copy_string(lsm
.session
.name
, handle
->session_name
,
1487 sizeof(lsm
.session
.name
));
1488 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1490 return ask_sessiond(&lsm
, NULL
);
1494 * Set health socket path by putting it in the global health_sock_path
1497 * Returns 0 on success or assert(0) on ENOMEM.
1499 static int set_health_socket_path(void)
1501 int in_tgroup
= 0; /* In tracing group */
1508 /* Are we in the tracing group ? */
1509 in_tgroup
= check_tracing_group(tracing_group
);
1512 if ((uid
== 0) || in_tgroup
) {
1513 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1514 sizeof(health_sock_path
));
1521 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1522 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1524 home
= getenv("HOME");
1526 /* Fallback in /tmp .. */
1530 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1531 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1532 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1533 /* ENOMEM at this point... just kill the control lib. */
1542 * Check session daemon health for a specific health component.
1544 * Return 0 if health is OK or else 1 if BAD.
1546 * Any other negative value is a lttng error code which can be translated with
1549 int lttng_health_check(enum lttng_health_component c
)
1552 struct lttcomm_health_msg msg
;
1553 struct lttcomm_health_data reply
;
1555 /* Connect to the sesssion daemon */
1556 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1558 ret
= -LTTNG_ERR_NO_SESSIOND
;
1562 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1565 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1567 ret
= -LTTNG_ERR_FATAL
;
1571 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1573 ret
= -LTTNG_ERR_FATAL
;
1577 ret
= reply
.ret_code
;
1587 * This is an extension of create session that is ONLY and SHOULD only be used
1588 * by the lttng command line program. It exists to avoid using URI parsing in
1591 * We need the date and time for the trace path subdirectory for the case where
1592 * the user does NOT define one using either -o or -U. Using the normal
1593 * lttng_create_session API call, we have no clue on the session daemon side if
1594 * the URL was generated automatically by the client or define by the user.
1596 * So this function "wrapper" is hidden from the public API, takes the datetime
1597 * string and appends it if necessary to the URI subdirectory before sending it
1598 * to the session daemon.
1600 * With this extra function, the lttng_create_session call behavior is not
1601 * changed and the timestamp is appended to the URI on the session daemon side
1604 int _lttng_create_session_ext(const char *name
, const char *url
,
1605 const char *datetime
)
1608 struct lttcomm_session_msg lsm
;
1609 struct lttng_uri
*uris
= NULL
;
1611 if (name
== NULL
|| datetime
== NULL
) {
1612 return -LTTNG_ERR_INVALID
;
1615 memset(&lsm
, 0, sizeof(lsm
));
1617 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1618 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1620 /* There should never be a data URL */
1621 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1623 return -LTTNG_ERR_INVALID
;
1626 lsm
.u
.uri
.size
= size
;
1628 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1631 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s", name
,
1634 PERROR("snprintf uri subdir");
1635 return -LTTNG_ERR_FATAL
;
1639 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1644 * For a given session name, this call checks if the data is ready to be read
1645 * or is still being extracted by the consumer(s) hence not ready to be used by
1648 int lttng_data_pending(const char *session_name
)
1651 struct lttcomm_session_msg lsm
;
1653 if (session_name
== NULL
) {
1654 return -LTTNG_ERR_INVALID
;
1657 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1659 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1661 ret
= ask_sessiond(&lsm
, NULL
);
1664 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1665 * getting -1 means that the reply ret_code was 1 thus meaning that the
1666 * data is available. Yes it is hackish but for now this is the only way.
1678 static void __attribute__((constructor
)) init()
1680 /* Set default session group */
1681 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1682 /* Set socket for health check */
1683 (void) set_health_socket_path();
1689 static void __attribute__((destructor
)) lttng_ctl_exit()
1691 free(tracing_group
);