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
);
191 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
208 * Copy string from src to dst and enforce null terminated byte.
210 static void copy_string(char *dst
, const char *src
, size_t len
)
213 strncpy(dst
, src
, len
);
214 /* Enforce the NULL terminated byte */
222 * Copy domain to lttcomm_session_msg domain.
224 * If domain is unknown, default domain will be the kernel.
226 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
230 case LTTNG_DOMAIN_KERNEL
:
231 case LTTNG_DOMAIN_UST
:
233 case LTTNG_DOMAIN_UST_EXEC_NAME:
234 case LTTNG_DOMAIN_UST_PID:
235 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
237 memcpy(dst
, src
, sizeof(struct lttng_domain
));
240 memset(dst
, 0, sizeof(struct lttng_domain
));
247 * Send lttcomm_session_msg to the session daemon.
249 * On success, returns the number of bytes sent (>=0)
250 * On error, returns -1
252 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
257 ret
= -LTTNG_ERR_NO_SESSIOND
;
261 DBG("LSM cmd type : %d", lsm
->cmd_type
);
263 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
264 sizeof(struct lttcomm_session_msg
));
266 ret
= -LTTNG_ERR_FATAL
;
274 * Send var len data to the session daemon.
276 * On success, returns the number of bytes sent (>=0)
277 * On error, returns -1
279 static int send_session_varlen(void *data
, size_t len
)
284 ret
= -LTTNG_ERR_NO_SESSIOND
;
293 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
295 ret
= -LTTNG_ERR_FATAL
;
303 * Receive data from the sessiond socket.
305 * On success, returns the number of bytes received (>=0)
306 * On error, returns -1 (recvmsg() error) or -ENOTCONN
308 static int recv_data_sessiond(void *buf
, size_t len
)
313 ret
= -LTTNG_ERR_NO_SESSIOND
;
317 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
319 ret
= -LTTNG_ERR_FATAL
;
327 * Check if we are in the specified group.
329 * If yes return 1, else return -1.
331 static int check_tracing_group(const char *grp_name
)
333 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
335 int grp_list_size
, grp_id
, i
;
338 /* Get GID of group 'tracing' */
339 grp_tracing
= getgrnam(grp_name
);
341 /* If grp_tracing is NULL, the group does not exist. */
345 /* Get number of supplementary group IDs */
346 grp_list_size
= getgroups(0, NULL
);
347 if (grp_list_size
< 0) {
352 /* Alloc group list of the right size */
353 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
358 grp_id
= getgroups(grp_list_size
, grp_list
);
364 for (i
= 0; i
< grp_list_size
; i
++) {
365 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
379 * Try connect to session daemon with sock_path.
381 * Return 0 on success, else -1
383 static int try_connect_sessiond(const char *sock_path
)
387 /* If socket exist, we check if the daemon listens for connect. */
388 ret
= access(sock_path
, F_OK
);
394 ret
= lttcomm_connect_unix_sock(sock_path
);
400 ret
= lttcomm_close_unix_sock(ret
);
402 perror("lttcomm_close_unix_sock");
412 * Set sessiond socket path by putting it in the global sessiond_sock_path
415 * Returns 0 on success, negative value on failure (the sessiond socket path
416 * is somehow too long or ENOMEM).
418 static int set_session_daemon_path(void)
420 int in_tgroup
= 0; /* In tracing group */
426 /* Are we in the tracing group ? */
427 in_tgroup
= check_tracing_group(tracing_group
);
430 if ((uid
== 0) || in_tgroup
) {
431 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
432 sizeof(sessiond_sock_path
));
440 ret
= try_connect_sessiond(sessiond_sock_path
);
444 /* Global session daemon not available... */
446 /* ...or not in tracing group (and not root), default */
449 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
450 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
452 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
453 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
454 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
466 * Connect to the LTTng session daemon.
468 * On success, return 0. On error, return -1.
470 static int connect_sessiond(void)
474 ret
= set_session_daemon_path();
479 /* Connect to the sesssion daemon */
480 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
485 sessiond_socket
= ret
;
495 * Clean disconnect from the session daemon.
496 * On success, return 0. On error, return -1.
498 static int disconnect_sessiond(void)
503 ret
= lttcomm_close_unix_sock(sessiond_socket
);
512 * Ask the session daemon a specific command and put the data into buf.
513 * Takes extra var. len. data as input to send to the session daemon.
515 * Return size of data (only payload, not header) or a negative error code.
517 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
518 void *vardata
, size_t varlen
, void **buf
)
523 struct lttcomm_lttng_msg llm
;
525 ret
= connect_sessiond();
527 ret
= -LTTNG_ERR_NO_SESSIOND
;
531 /* Send command to session daemon */
532 ret
= send_session_msg(lsm
);
534 /* Ret value is a valid lttng error code. */
537 /* Send var len data */
538 ret
= send_session_varlen(vardata
, varlen
);
540 /* Ret value is a valid lttng error code. */
544 /* Get header from data transmission */
545 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
547 /* Ret value is a valid lttng error code. */
551 /* Check error code if OK */
552 if (llm
.ret_code
!= LTTNG_OK
) {
557 size
= llm
.data_size
;
559 /* If client free with size 0 */
567 data
= (void*) malloc(size
);
569 /* Get payload data */
570 ret
= recv_data_sessiond(data
, size
);
577 * Extra protection not to dereference a NULL pointer. If buf is NULL at
578 * this point, an error is returned and data is freed.
581 ret
= -LTTNG_ERR_INVALID
;
590 disconnect_sessiond();
595 * Ask the session daemon a specific command and put the data into buf.
597 * Return size of data (only payload, not header) or a negative error code.
599 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
601 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
605 * Create lttng handle and return pointer.
606 * The returned pointer will be NULL in case of malloc() error.
608 struct lttng_handle
*lttng_create_handle(const char *session_name
,
609 struct lttng_domain
*domain
)
611 struct lttng_handle
*handle
= NULL
;
613 if (domain
== NULL
) {
617 handle
= malloc(sizeof(struct lttng_handle
));
618 if (handle
== NULL
) {
619 PERROR("malloc handle");
623 /* Copy session name */
624 copy_string(handle
->session_name
, session_name
,
625 sizeof(handle
->session_name
));
627 /* Copy lttng domain */
628 copy_lttng_domain(&handle
->domain
, domain
);
635 * Destroy handle by free(3) the pointer.
637 void lttng_destroy_handle(struct lttng_handle
*handle
)
645 * Register an outside consumer.
646 * Returns size of returned session payload data or a negative error code.
648 int lttng_register_consumer(struct lttng_handle
*handle
,
649 const char *socket_path
)
651 struct lttcomm_session_msg lsm
;
653 if (handle
== NULL
|| socket_path
== NULL
) {
654 return -LTTNG_ERR_INVALID
;
657 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
658 copy_string(lsm
.session
.name
, handle
->session_name
,
659 sizeof(lsm
.session
.name
));
660 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
662 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
664 return ask_sessiond(&lsm
, NULL
);
668 * Start tracing for all traces of the session.
669 * Returns size of returned session payload data or a negative error code.
671 int lttng_start_tracing(const char *session_name
)
673 struct lttcomm_session_msg lsm
;
675 if (session_name
== NULL
) {
676 return -LTTNG_ERR_INVALID
;
679 lsm
.cmd_type
= LTTNG_START_TRACE
;
681 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
683 return ask_sessiond(&lsm
, NULL
);
687 * Stop tracing for all traces of the session.
689 static int _lttng_stop_tracing(const char *session_name
, int wait
)
692 struct lttcomm_session_msg lsm
;
694 if (session_name
== NULL
) {
695 return -LTTNG_ERR_INVALID
;
698 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
700 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
702 ret
= ask_sessiond(&lsm
, NULL
);
703 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
711 _MSG("Waiting for data availability");
713 /* Check for data availability */
715 data_ret
= lttng_data_pending(session_name
);
717 /* Return the data available call error. */
723 * Data sleep time before retrying (in usec). Don't sleep if the call
724 * returned value indicates availability.
727 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
730 } while (data_ret
!= 0);
740 * Stop tracing and wait for data availability.
742 int lttng_stop_tracing(const char *session_name
)
744 return _lttng_stop_tracing(session_name
, 1);
748 * Stop tracing but _don't_ wait for data availability.
750 int lttng_stop_tracing_no_wait(const char *session_name
)
752 return _lttng_stop_tracing(session_name
, 0);
756 * Add context to a channel.
758 * If the given channel is NULL, add the contexts to all channels.
759 * The event_name param is ignored.
761 * Returns the size of the returned payload data or a negative error code.
763 int lttng_add_context(struct lttng_handle
*handle
,
764 struct lttng_event_context
*ctx
, const char *event_name
,
765 const char *channel_name
)
767 struct lttcomm_session_msg lsm
;
769 /* Safety check. Both are mandatory */
770 if (handle
== NULL
|| ctx
== NULL
) {
771 return -LTTNG_ERR_INVALID
;
774 memset(&lsm
, 0, sizeof(lsm
));
776 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
778 /* Copy channel name */
779 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
780 sizeof(lsm
.u
.context
.channel_name
));
782 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
784 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
786 copy_string(lsm
.session
.name
, handle
->session_name
,
787 sizeof(lsm
.session
.name
));
789 return ask_sessiond(&lsm
, NULL
);
793 * Enable event(s) for a channel.
794 * If no event name is specified, all events are enabled.
795 * If no channel name is specified, the default 'channel0' is used.
796 * Returns size of returned session payload data or a negative error code.
798 int lttng_enable_event(struct lttng_handle
*handle
,
799 struct lttng_event
*ev
, const char *channel_name
)
801 struct lttcomm_session_msg lsm
;
803 if (handle
== NULL
|| ev
== NULL
) {
804 return -LTTNG_ERR_INVALID
;
807 memset(&lsm
, 0, sizeof(lsm
));
809 /* If no channel name, we put the default name */
810 if (channel_name
== NULL
) {
811 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
812 sizeof(lsm
.u
.enable
.channel_name
));
814 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
815 sizeof(lsm
.u
.enable
.channel_name
));
818 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
820 if (ev
->name
[0] != '\0') {
821 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
823 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
825 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
827 copy_string(lsm
.session
.name
, handle
->session_name
,
828 sizeof(lsm
.session
.name
));
830 return ask_sessiond(&lsm
, NULL
);
834 * Create or enable an event with a filter expression.
836 * Return negative error value on error.
837 * Return size of returned session payload data if OK.
839 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
840 struct lttng_event
*event
, const char *channel_name
,
841 const char *filter_expression
)
843 struct lttcomm_session_msg lsm
;
844 struct filter_parser_ctx
*ctx
;
848 if (!filter_expression
) {
850 * Fall back to normal event enabling if no filter
853 return lttng_enable_event(handle
, event
, channel_name
);
857 * Empty filter string will always be rejected by the parser
858 * anyway, so treat this corner-case early to eliminate
859 * lttng_fmemopen error for 0-byte allocation.
861 if (handle
== NULL
|| filter_expression
[0] == '\0') {
862 return -LTTNG_ERR_INVALID
;
866 * casting const to non-const, as the underlying function will
867 * use it in read-only mode.
869 fmem
= lttng_fmemopen((void *) filter_expression
,
870 strlen(filter_expression
), "r");
872 fprintf(stderr
, "Error opening memory as stream\n");
873 return -LTTNG_ERR_FILTER_NOMEM
;
875 ctx
= filter_parser_ctx_alloc(fmem
);
877 fprintf(stderr
, "Error allocating parser\n");
878 ret
= -LTTNG_ERR_FILTER_NOMEM
;
881 ret
= filter_parser_ctx_append_ast(ctx
);
883 fprintf(stderr
, "Parse error\n");
884 ret
= -LTTNG_ERR_FILTER_INVAL
;
887 ret
= filter_visitor_set_parent(ctx
);
889 fprintf(stderr
, "Set parent error\n");
890 ret
= -LTTNG_ERR_FILTER_INVAL
;
894 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
897 fprintf(stderr
, "XML print error\n");
898 ret
= -LTTNG_ERR_FILTER_INVAL
;
903 dbg_printf("Generating IR... ");
905 ret
= filter_visitor_ir_generate(ctx
);
907 fprintf(stderr
, "Generate IR error\n");
908 ret
= -LTTNG_ERR_FILTER_INVAL
;
911 dbg_printf("done\n");
913 dbg_printf("Validating IR... ");
915 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
917 ret
= -LTTNG_ERR_FILTER_INVAL
;
920 dbg_printf("done\n");
922 dbg_printf("Generating bytecode... ");
924 ret
= filter_visitor_bytecode_generate(ctx
);
926 fprintf(stderr
, "Generate bytecode error\n");
927 ret
= -LTTNG_ERR_FILTER_INVAL
;
930 dbg_printf("done\n");
931 dbg_printf("Size of bytecode generated: %u bytes.\n",
932 bytecode_get_len(&ctx
->bytecode
->b
));
934 memset(&lsm
, 0, sizeof(lsm
));
936 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
938 /* Copy channel name */
939 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
940 sizeof(lsm
.u
.enable
.channel_name
));
941 /* Copy event name */
943 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
946 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
947 + bytecode_get_len(&ctx
->bytecode
->b
);
949 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
951 copy_string(lsm
.session
.name
, handle
->session_name
,
952 sizeof(lsm
.session
.name
));
954 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
955 lsm
.u
.enable
.bytecode_len
, NULL
);
957 filter_bytecode_free(ctx
);
959 filter_parser_ctx_free(ctx
);
960 if (fclose(fmem
) != 0) {
966 filter_bytecode_free(ctx
);
968 filter_parser_ctx_free(ctx
);
970 if (fclose(fmem
) != 0) {
977 * Disable event(s) of a channel and domain.
978 * If no event name is specified, all events are disabled.
979 * If no channel name is specified, the default 'channel0' is used.
980 * Returns size of returned session payload data or a negative error code.
982 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
983 const char *channel_name
)
985 struct lttcomm_session_msg lsm
;
987 if (handle
== NULL
) {
988 return -LTTNG_ERR_INVALID
;
991 memset(&lsm
, 0, sizeof(lsm
));
994 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
995 sizeof(lsm
.u
.disable
.channel_name
));
997 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
998 sizeof(lsm
.u
.disable
.channel_name
));
1001 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1004 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
1005 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1007 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
1010 copy_string(lsm
.session
.name
, handle
->session_name
,
1011 sizeof(lsm
.session
.name
));
1013 return ask_sessiond(&lsm
, NULL
);
1017 * Enable channel per domain
1018 * Returns size of returned session payload data or a negative error code.
1020 int lttng_enable_channel(struct lttng_handle
*handle
,
1021 struct lttng_channel
*chan
)
1023 struct lttcomm_session_msg lsm
;
1026 * NULL arguments are forbidden. No default values.
1028 if (handle
== NULL
|| chan
== NULL
) {
1029 return -LTTNG_ERR_INVALID
;
1032 memset(&lsm
, 0, sizeof(lsm
));
1034 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1036 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1038 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1040 copy_string(lsm
.session
.name
, handle
->session_name
,
1041 sizeof(lsm
.session
.name
));
1043 return ask_sessiond(&lsm
, NULL
);
1047 * All tracing will be stopped for registered events of the channel.
1048 * Returns size of returned session payload data or a negative error code.
1050 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1052 struct lttcomm_session_msg lsm
;
1054 /* Safety check. Both are mandatory */
1055 if (handle
== NULL
|| name
== NULL
) {
1056 return -LTTNG_ERR_INVALID
;
1059 memset(&lsm
, 0, sizeof(lsm
));
1061 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1063 copy_string(lsm
.u
.disable
.channel_name
, name
,
1064 sizeof(lsm
.u
.disable
.channel_name
));
1066 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1068 copy_string(lsm
.session
.name
, handle
->session_name
,
1069 sizeof(lsm
.session
.name
));
1071 return ask_sessiond(&lsm
, NULL
);
1075 * Lists all available tracepoints of domain.
1076 * Sets the contents of the events array.
1077 * Returns the number of lttng_event entries in events;
1078 * on error, returns a negative value.
1080 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1081 struct lttng_event
**events
)
1084 struct lttcomm_session_msg lsm
;
1086 if (handle
== NULL
) {
1087 return -LTTNG_ERR_INVALID
;
1090 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1091 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1093 ret
= ask_sessiond(&lsm
, (void **) events
);
1098 return ret
/ sizeof(struct lttng_event
);
1102 * Lists all available tracepoint fields of domain.
1103 * Sets the contents of the event field array.
1104 * Returns the number of lttng_event_field entries in events;
1105 * on error, returns a negative value.
1107 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1108 struct lttng_event_field
**fields
)
1111 struct lttcomm_session_msg lsm
;
1113 if (handle
== NULL
) {
1114 return -LTTNG_ERR_INVALID
;
1117 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1118 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1120 ret
= ask_sessiond(&lsm
, (void **) fields
);
1125 return ret
/ sizeof(struct lttng_event_field
);
1129 * Returns a human readable string describing
1130 * the error code (a negative value).
1132 const char *lttng_strerror(int code
)
1134 return error_get_str(code
);
1138 * Create a brand new session using name and url for destination.
1140 * Returns LTTNG_OK on success or a negative error code.
1142 int lttng_create_session(const char *name
, const char *url
)
1146 struct lttcomm_session_msg lsm
;
1147 struct lttng_uri
*uris
= NULL
;
1150 return -LTTNG_ERR_INVALID
;
1153 memset(&lsm
, 0, sizeof(lsm
));
1155 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1156 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1158 /* There should never be a data URL */
1159 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1161 return -LTTNG_ERR_INVALID
;
1164 lsm
.u
.uri
.size
= size
;
1166 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1174 * Destroy session using name.
1175 * Returns size of returned session payload data or a negative error code.
1177 int lttng_destroy_session(const char *session_name
)
1179 struct lttcomm_session_msg lsm
;
1181 if (session_name
== NULL
) {
1182 return -LTTNG_ERR_INVALID
;
1185 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1187 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1189 return ask_sessiond(&lsm
, NULL
);
1193 * Ask the session daemon for all available sessions.
1194 * Sets the contents of the sessions array.
1195 * Returns the number of lttng_session entries in sessions;
1196 * on error, returns a negative value.
1198 int lttng_list_sessions(struct lttng_session
**sessions
)
1201 struct lttcomm_session_msg lsm
;
1203 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1204 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1209 return ret
/ sizeof(struct lttng_session
);
1213 * Ask the session daemon for all available domains of a session.
1214 * Sets the contents of the domains array.
1215 * Returns the number of lttng_domain entries in domains;
1216 * on error, returns a negative value.
1218 int lttng_list_domains(const char *session_name
,
1219 struct lttng_domain
**domains
)
1222 struct lttcomm_session_msg lsm
;
1224 if (session_name
== NULL
) {
1225 return -LTTNG_ERR_INVALID
;
1228 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1230 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1232 ret
= ask_sessiond(&lsm
, (void**) domains
);
1237 return ret
/ sizeof(struct lttng_domain
);
1241 * Ask the session daemon for all available channels of a session.
1242 * Sets the contents of the channels array.
1243 * Returns the number of lttng_channel entries in channels;
1244 * on error, returns a negative value.
1246 int lttng_list_channels(struct lttng_handle
*handle
,
1247 struct lttng_channel
**channels
)
1250 struct lttcomm_session_msg lsm
;
1252 if (handle
== NULL
) {
1253 return -LTTNG_ERR_INVALID
;
1256 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1257 copy_string(lsm
.session
.name
, handle
->session_name
,
1258 sizeof(lsm
.session
.name
));
1260 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1262 ret
= ask_sessiond(&lsm
, (void**) channels
);
1267 return ret
/ sizeof(struct lttng_channel
);
1271 * Ask the session daemon for all available events of a session channel.
1272 * Sets the contents of the events array.
1273 * Returns the number of lttng_event entries in events;
1274 * on error, returns a negative value.
1276 int lttng_list_events(struct lttng_handle
*handle
,
1277 const char *channel_name
, struct lttng_event
**events
)
1280 struct lttcomm_session_msg lsm
;
1282 /* Safety check. An handle and channel name are mandatory */
1283 if (handle
== NULL
|| channel_name
== NULL
) {
1284 return -LTTNG_ERR_INVALID
;
1287 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1288 copy_string(lsm
.session
.name
, handle
->session_name
,
1289 sizeof(lsm
.session
.name
));
1290 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1291 sizeof(lsm
.u
.list
.channel_name
));
1293 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1295 ret
= ask_sessiond(&lsm
, (void**) events
);
1300 return ret
/ sizeof(struct lttng_event
);
1304 * Sets the tracing_group variable with name.
1305 * This function allocates memory pointed to by tracing_group.
1306 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1308 int lttng_set_tracing_group(const char *name
)
1311 return -LTTNG_ERR_INVALID
;
1314 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1315 return -LTTNG_ERR_FATAL
;
1322 * Returns size of returned session payload data or a negative error code.
1324 int lttng_calibrate(struct lttng_handle
*handle
,
1325 struct lttng_calibrate
*calibrate
)
1327 struct lttcomm_session_msg lsm
;
1329 /* Safety check. NULL pointer are forbidden */
1330 if (handle
== NULL
|| calibrate
== NULL
) {
1331 return -LTTNG_ERR_INVALID
;
1334 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1335 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1337 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1339 return ask_sessiond(&lsm
, NULL
);
1343 * Set default channel attributes.
1344 * If either or both of the arguments are null, attr content is zeroe'd.
1346 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1347 struct lttng_channel_attr
*attr
)
1350 if (attr
== NULL
|| domain
== NULL
) {
1354 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1356 switch (domain
->type
) {
1357 case LTTNG_DOMAIN_KERNEL
:
1358 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1359 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1360 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1362 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1363 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1364 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1366 case LTTNG_DOMAIN_UST
:
1368 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1369 case LTTNG_DOMAIN_UST_PID
:
1370 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1372 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1373 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1374 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1376 attr
->subbuf_size
= default_get_ust_channel_subbuf_size();
1377 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1378 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1381 /* Default behavior: leave set to 0. */
1387 * Check if session daemon is alive.
1389 * Return 1 if alive or 0 if not.
1390 * On error returns a negative value.
1392 int lttng_session_daemon_alive(void)
1396 ret
= set_session_daemon_path();
1402 if (*sessiond_sock_path
== '\0') {
1404 * No socket path set. Weird error which means the constructor was not
1410 ret
= try_connect_sessiond(sessiond_sock_path
);
1421 * Set URL for a consumer for a session and domain.
1423 * Return 0 on success, else a negative value.
1425 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1426 const char *control_url
, const char *data_url
)
1430 struct lttcomm_session_msg lsm
;
1431 struct lttng_uri
*uris
= NULL
;
1433 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1434 return -LTTNG_ERR_INVALID
;
1437 memset(&lsm
, 0, sizeof(lsm
));
1439 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1441 copy_string(lsm
.session
.name
, handle
->session_name
,
1442 sizeof(lsm
.session
.name
));
1443 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1445 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1447 return -LTTNG_ERR_INVALID
;
1450 lsm
.u
.uri
.size
= size
;
1452 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1460 * Enable consumer for a session and domain.
1462 * Return 0 on success, else a negative value.
1464 int lttng_enable_consumer(struct lttng_handle
*handle
)
1466 struct lttcomm_session_msg lsm
;
1468 if (handle
== NULL
) {
1469 return -LTTNG_ERR_INVALID
;
1472 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1474 copy_string(lsm
.session
.name
, handle
->session_name
,
1475 sizeof(lsm
.session
.name
));
1476 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1478 return ask_sessiond(&lsm
, NULL
);
1482 * Disable consumer for a session and domain.
1484 * Return 0 on success, else a negative value.
1486 int lttng_disable_consumer(struct lttng_handle
*handle
)
1488 struct lttcomm_session_msg lsm
;
1490 if (handle
== NULL
) {
1491 return -LTTNG_ERR_INVALID
;
1494 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1496 copy_string(lsm
.session
.name
, handle
->session_name
,
1497 sizeof(lsm
.session
.name
));
1498 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1500 return ask_sessiond(&lsm
, NULL
);
1504 * Set health socket path by putting it in the global health_sock_path
1507 * Returns 0 on success or assert(0) on ENOMEM.
1509 static int set_health_socket_path(void)
1511 int in_tgroup
= 0; /* In tracing group */
1518 /* Are we in the tracing group ? */
1519 in_tgroup
= check_tracing_group(tracing_group
);
1522 if ((uid
== 0) || in_tgroup
) {
1523 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1524 sizeof(health_sock_path
));
1531 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1532 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1534 home
= getenv("HOME");
1536 /* Fallback in /tmp .. */
1540 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1541 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1542 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1543 /* ENOMEM at this point... just kill the control lib. */
1552 * Check session daemon health for a specific health component.
1554 * Return 0 if health is OK or else 1 if BAD.
1556 * Any other negative value is a lttng error code which can be translated with
1559 int lttng_health_check(enum lttng_health_component c
)
1562 struct lttcomm_health_msg msg
;
1563 struct lttcomm_health_data reply
;
1565 /* Connect to the sesssion daemon */
1566 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1568 ret
= -LTTNG_ERR_NO_SESSIOND
;
1572 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1575 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1577 ret
= -LTTNG_ERR_FATAL
;
1581 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1583 ret
= -LTTNG_ERR_FATAL
;
1587 ret
= reply
.ret_code
;
1597 * This is an extension of create session that is ONLY and SHOULD only be used
1598 * by the lttng command line program. It exists to avoid using URI parsing in
1601 * We need the date and time for the trace path subdirectory for the case where
1602 * the user does NOT define one using either -o or -U. Using the normal
1603 * lttng_create_session API call, we have no clue on the session daemon side if
1604 * the URL was generated automatically by the client or define by the user.
1606 * So this function "wrapper" is hidden from the public API, takes the datetime
1607 * string and appends it if necessary to the URI subdirectory before sending it
1608 * to the session daemon.
1610 * With this extra function, the lttng_create_session call behavior is not
1611 * changed and the timestamp is appended to the URI on the session daemon side
1614 int _lttng_create_session_ext(const char *name
, const char *url
,
1615 const char *datetime
)
1619 struct lttcomm_session_msg lsm
;
1620 struct lttng_uri
*uris
= NULL
;
1622 if (name
== NULL
|| datetime
== NULL
) {
1623 return -LTTNG_ERR_INVALID
;
1626 memset(&lsm
, 0, sizeof(lsm
));
1628 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1629 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1631 /* There should never be a data URL */
1632 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1634 ret
= -LTTNG_ERR_INVALID
;
1638 lsm
.u
.uri
.size
= size
;
1640 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1641 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s", name
,
1644 PERROR("snprintf uri subdir");
1645 ret
= -LTTNG_ERR_FATAL
;
1650 ret
= ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1659 * For a given session name, this call checks if the data is ready to be read
1660 * or is still being extracted by the consumer(s) hence not ready to be used by
1663 int lttng_data_pending(const char *session_name
)
1666 struct lttcomm_session_msg lsm
;
1668 if (session_name
== NULL
) {
1669 return -LTTNG_ERR_INVALID
;
1672 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1674 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1676 ret
= ask_sessiond(&lsm
, NULL
);
1679 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1680 * getting -1 means that the reply ret_code was 1 thus meaning that the
1681 * data is available. Yes it is hackish but for now this is the only way.
1693 static void __attribute__((constructor
)) init()
1695 /* Set default session group */
1696 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1697 /* Set socket for health check */
1698 (void) set_health_socket_path();
1704 static void __attribute__((destructor
)) lttng_ctl_exit()
1706 free(tracing_group
);