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.
73 * Note that it is *not* possible to silent ERR() and PERROR() macros.
76 int lttng_opt_verbose
;
78 static void set_default_url_attr(struct lttng_uri
*uri
,
79 enum lttng_stream_type stype
)
82 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
83 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
84 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
89 * Parse a string URL and creates URI(s) returning the size of the populated
92 static ssize_t
parse_str_urls_to_uri(const char *ctrl_url
, const char *data_url
,
93 struct lttng_uri
**uris
)
96 unsigned int equal
= 1, idx
= 0;
97 /* Add the "file://" size to the URL maximum size */
98 char url
[PATH_MAX
+ 7];
99 ssize_t size_ctrl
= 0, size_data
= 0, size
;
100 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
101 struct lttng_uri
*tmp_uris
= NULL
;
103 /* No URL(s) is allowed. This means that the consumer will be disabled. */
104 if (ctrl_url
== NULL
&& data_url
== NULL
) {
108 /* Check if URLs are equal and if so, only use the control URL */
109 if (ctrl_url
&& data_url
) {
110 equal
= !strcmp(ctrl_url
, data_url
);
114 * Since we allow the str_url to be a full local filesystem path, we are
115 * going to create a valid file:// URL if it's the case.
117 * Check if first character is a '/' or else reject the URL.
119 if (ctrl_url
&& ctrl_url
[0] == '/') {
120 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
122 PERROR("snprintf file url");
128 /* Parse the control URL if there is one */
130 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
132 ERR("Unable to parse the URL %s", ctrl_url
);
136 /* At this point, we know there is at least one URI in the array */
137 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
139 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
140 ERR("Can not have a data URL when destination is file://");
144 /* URL are not equal but the control URL uses a net:// protocol */
145 if (size_ctrl
== 2) {
147 ERR("Control URL uses the net:// protocol and the data URL is "
148 "different. Not allowed.");
151 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
153 * The data_url and ctrl_url are equal and the ctrl_url
154 * contains a net:// protocol so we just skip the data part.
162 /* We have to parse the data URL in this case */
163 size_data
= uri_parse(data_url
, &data_uris
);
165 ERR("Unable to parse the URL %s", data_url
);
167 } else if (size_data
== 2) {
168 ERR("Data URL can not be set with the net[4|6]:// protocol");
172 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
175 /* Compute total size */
176 size
= size_ctrl
+ size_data
;
178 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
179 if (tmp_uris
== NULL
) {
180 PERROR("zmalloc uris");
185 /* It's possible the control URIs array contains more than one URI */
186 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
191 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
207 * Copy string from src to dst and enforce null terminated byte.
209 static void copy_string(char *dst
, const char *src
, size_t len
)
212 strncpy(dst
, src
, len
);
213 /* Enforce the NULL terminated byte */
221 * Copy domain to lttcomm_session_msg domain.
223 * If domain is unknown, default domain will be the kernel.
225 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
229 case LTTNG_DOMAIN_KERNEL
:
230 case LTTNG_DOMAIN_UST
:
232 case LTTNG_DOMAIN_UST_EXEC_NAME:
233 case LTTNG_DOMAIN_UST_PID:
234 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
236 memcpy(dst
, src
, sizeof(struct lttng_domain
));
239 memset(dst
, 0, sizeof(struct lttng_domain
));
246 * Send lttcomm_session_msg to the session daemon.
248 * On success, returns the number of bytes sent (>=0)
249 * On error, returns -1
251 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
256 ret
= -LTTNG_ERR_NO_SESSIOND
;
260 DBG("LSM cmd type : %d", lsm
->cmd_type
);
262 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
263 sizeof(struct lttcomm_session_msg
));
265 ret
= -LTTNG_ERR_FATAL
;
273 * Send var len data to the session daemon.
275 * On success, returns the number of bytes sent (>=0)
276 * On error, returns -1
278 static int send_session_varlen(void *data
, size_t len
)
283 ret
= -LTTNG_ERR_NO_SESSIOND
;
292 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
294 ret
= -LTTNG_ERR_FATAL
;
302 * Receive data from the sessiond socket.
304 * On success, returns the number of bytes received (>=0)
305 * On error, returns -1 (recvmsg() error) or -ENOTCONN
307 static int recv_data_sessiond(void *buf
, size_t len
)
312 ret
= -LTTNG_ERR_NO_SESSIOND
;
316 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
318 ret
= -LTTNG_ERR_FATAL
;
326 * Check if we are in the specified group.
328 * If yes return 1, else return -1.
330 static int check_tracing_group(const char *grp_name
)
332 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
334 int grp_list_size
, grp_id
, i
;
337 /* Get GID of group 'tracing' */
338 grp_tracing
= getgrnam(grp_name
);
340 /* If grp_tracing is NULL, the group does not exist. */
344 /* Get number of supplementary group IDs */
345 grp_list_size
= getgroups(0, NULL
);
346 if (grp_list_size
< 0) {
351 /* Alloc group list of the right size */
352 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
357 grp_id
= getgroups(grp_list_size
, grp_list
);
363 for (i
= 0; i
< grp_list_size
; i
++) {
364 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
378 * Try connect to session daemon with sock_path.
380 * Return 0 on success, else -1
382 static int try_connect_sessiond(const char *sock_path
)
386 /* If socket exist, we check if the daemon listens for connect. */
387 ret
= access(sock_path
, F_OK
);
393 ret
= lttcomm_connect_unix_sock(sock_path
);
399 ret
= lttcomm_close_unix_sock(ret
);
401 perror("lttcomm_close_unix_sock");
411 * Set sessiond socket path by putting it in the global sessiond_sock_path
414 * Returns 0 on success, negative value on failure (the sessiond socket path
415 * is somehow too long or ENOMEM).
417 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
));
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.
686 * Returns size of returned session payload data or a negative error code.
688 int lttng_stop_tracing(const char *session_name
)
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 return ask_sessiond(&lsm
, NULL
);
704 * Add context to event and/or channel.
705 * If event_name is NULL, the context is applied to all events of the channel.
706 * If channel_name is NULL, a lookup of the event's channel is done.
707 * If both are NULL, the context is applied to all events of all channels.
709 * Returns the size of the returned payload data or a negative error code.
711 int lttng_add_context(struct lttng_handle
*handle
,
712 struct lttng_event_context
*ctx
, const char *event_name
,
713 const char *channel_name
)
715 struct lttcomm_session_msg lsm
;
717 /* Safety check. Both are mandatory */
718 if (handle
== NULL
|| ctx
== NULL
) {
719 return -LTTNG_ERR_INVALID
;
722 memset(&lsm
, 0, sizeof(lsm
));
724 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
726 /* Copy channel name */
727 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
728 sizeof(lsm
.u
.context
.channel_name
));
729 /* Copy event name */
730 copy_string(lsm
.u
.context
.event_name
, event_name
,
731 sizeof(lsm
.u
.context
.event_name
));
733 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
735 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
737 copy_string(lsm
.session
.name
, handle
->session_name
,
738 sizeof(lsm
.session
.name
));
740 return ask_sessiond(&lsm
, NULL
);
744 * Enable event(s) for a channel.
745 * If no event name is specified, all events are enabled.
746 * If no channel name is specified, the default 'channel0' is used.
747 * Returns size of returned session payload data or a negative error code.
749 int lttng_enable_event(struct lttng_handle
*handle
,
750 struct lttng_event
*ev
, const char *channel_name
)
752 struct lttcomm_session_msg lsm
;
754 if (handle
== NULL
|| ev
== NULL
) {
755 return -LTTNG_ERR_INVALID
;
758 memset(&lsm
, 0, sizeof(lsm
));
760 /* If no channel name, we put the default name */
761 if (channel_name
== NULL
) {
762 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
763 sizeof(lsm
.u
.enable
.channel_name
));
765 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
766 sizeof(lsm
.u
.enable
.channel_name
));
769 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
771 if (ev
->name
[0] != '\0') {
772 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
774 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
776 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
778 copy_string(lsm
.session
.name
, handle
->session_name
,
779 sizeof(lsm
.session
.name
));
781 return ask_sessiond(&lsm
, NULL
);
785 * set filter for an event
786 * Return negative error value on error.
787 * Return size of returned session payload data if OK.
790 int lttng_set_event_filter(struct lttng_handle
*handle
,
791 const char *event_name
, const char *channel_name
,
792 const char *filter_expression
)
794 struct lttcomm_session_msg lsm
;
795 struct filter_parser_ctx
*ctx
;
800 if (handle
== NULL
) {
801 return -LTTNG_ERR_INVALID
;
804 if (!filter_expression
) {
809 * casting const to non-const, as the underlying function will
810 * use it in read-only mode.
812 fmem
= lttng_fmemopen((void *) filter_expression
,
813 strlen(filter_expression
), "r");
815 fprintf(stderr
, "Error opening memory as stream\n");
816 return -LTTNG_ERR_FILTER_NOMEM
;
818 ctx
= filter_parser_ctx_alloc(fmem
);
820 fprintf(stderr
, "Error allocating parser\n");
821 ret
= -LTTNG_ERR_FILTER_NOMEM
;
824 ret
= filter_parser_ctx_append_ast(ctx
);
826 fprintf(stderr
, "Parse error\n");
827 ret
= -LTTNG_ERR_FILTER_INVAL
;
830 ret
= filter_visitor_set_parent(ctx
);
832 fprintf(stderr
, "Set parent error\n");
833 ret
= -LTTNG_ERR_FILTER_INVAL
;
837 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
840 fprintf(stderr
, "XML print error\n");
841 ret
= -LTTNG_ERR_FILTER_INVAL
;
846 dbg_printf("Generating IR... ");
848 ret
= filter_visitor_ir_generate(ctx
);
850 fprintf(stderr
, "Generate IR error\n");
851 ret
= -LTTNG_ERR_FILTER_INVAL
;
854 dbg_printf("done\n");
856 dbg_printf("Validating IR... ");
858 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
860 ret
= -LTTNG_ERR_FILTER_INVAL
;
863 dbg_printf("done\n");
865 dbg_printf("Generating bytecode... ");
867 ret
= filter_visitor_bytecode_generate(ctx
);
869 fprintf(stderr
, "Generate bytecode error\n");
870 ret
= -LTTNG_ERR_FILTER_INVAL
;
873 dbg_printf("done\n");
874 dbg_printf("Size of bytecode generated: %u bytes.\n",
875 bytecode_get_len(&ctx
->bytecode
->b
));
877 memset(&lsm
, 0, sizeof(lsm
));
879 lsm
.cmd_type
= LTTNG_SET_FILTER
;
881 /* Copy channel name */
882 copy_string(lsm
.u
.filter
.channel_name
, channel_name
,
883 sizeof(lsm
.u
.filter
.channel_name
));
884 /* Copy event name */
885 copy_string(lsm
.u
.filter
.event_name
, event_name
,
886 sizeof(lsm
.u
.filter
.event_name
));
887 lsm
.u
.filter
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
888 + bytecode_get_len(&ctx
->bytecode
->b
);
890 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
892 copy_string(lsm
.session
.name
, handle
->session_name
,
893 sizeof(lsm
.session
.name
));
895 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
896 lsm
.u
.filter
.bytecode_len
, NULL
);
898 filter_bytecode_free(ctx
);
900 filter_parser_ctx_free(ctx
);
901 if (fclose(fmem
) != 0) {
907 filter_bytecode_free(ctx
);
909 filter_parser_ctx_free(ctx
);
911 if (fclose(fmem
) != 0) {
918 * Disable event(s) of a channel and domain.
919 * If no event name is specified, all events are disabled.
920 * If no channel name is specified, the default 'channel0' is used.
921 * Returns size of returned session payload data or a negative error code.
923 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
924 const char *channel_name
)
926 struct lttcomm_session_msg lsm
;
928 if (handle
== NULL
) {
929 return -LTTNG_ERR_INVALID
;
932 memset(&lsm
, 0, sizeof(lsm
));
935 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
936 sizeof(lsm
.u
.disable
.channel_name
));
938 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
939 sizeof(lsm
.u
.disable
.channel_name
));
942 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
945 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
946 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
948 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
951 copy_string(lsm
.session
.name
, handle
->session_name
,
952 sizeof(lsm
.session
.name
));
954 return ask_sessiond(&lsm
, NULL
);
958 * Enable channel per domain
959 * Returns size of returned session payload data or a negative error code.
961 int lttng_enable_channel(struct lttng_handle
*handle
,
962 struct lttng_channel
*chan
)
964 struct lttcomm_session_msg lsm
;
967 * NULL arguments are forbidden. No default values.
969 if (handle
== NULL
|| chan
== NULL
) {
970 return -LTTNG_ERR_INVALID
;
973 memset(&lsm
, 0, sizeof(lsm
));
975 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
977 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
979 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
981 copy_string(lsm
.session
.name
, handle
->session_name
,
982 sizeof(lsm
.session
.name
));
984 return ask_sessiond(&lsm
, NULL
);
988 * All tracing will be stopped for registered events of the channel.
989 * Returns size of returned session payload data or a negative error code.
991 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
993 struct lttcomm_session_msg lsm
;
995 /* Safety check. Both are mandatory */
996 if (handle
== NULL
|| name
== NULL
) {
997 return -LTTNG_ERR_INVALID
;
1000 memset(&lsm
, 0, sizeof(lsm
));
1002 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1004 copy_string(lsm
.u
.disable
.channel_name
, name
,
1005 sizeof(lsm
.u
.disable
.channel_name
));
1007 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1009 copy_string(lsm
.session
.name
, handle
->session_name
,
1010 sizeof(lsm
.session
.name
));
1012 return ask_sessiond(&lsm
, NULL
);
1016 * Lists all available tracepoints of domain.
1017 * Sets the contents of the events array.
1018 * Returns the number of lttng_event entries in events;
1019 * on error, returns a negative value.
1021 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1022 struct lttng_event
**events
)
1025 struct lttcomm_session_msg lsm
;
1027 if (handle
== NULL
) {
1028 return -LTTNG_ERR_INVALID
;
1031 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1032 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1034 ret
= ask_sessiond(&lsm
, (void **) events
);
1039 return ret
/ sizeof(struct lttng_event
);
1043 * Lists all available tracepoint fields of domain.
1044 * Sets the contents of the event field array.
1045 * Returns the number of lttng_event_field entries in events;
1046 * on error, returns a negative value.
1048 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1049 struct lttng_event_field
**fields
)
1052 struct lttcomm_session_msg lsm
;
1054 if (handle
== NULL
) {
1055 return -LTTNG_ERR_INVALID
;
1058 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1059 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1061 ret
= ask_sessiond(&lsm
, (void **) fields
);
1066 return ret
/ sizeof(struct lttng_event_field
);
1070 * Returns a human readable string describing
1071 * the error code (a negative value).
1073 const char *lttng_strerror(int code
)
1075 return error_get_str(code
);
1079 * Create a brand new session using name and url for destination.
1081 * Returns LTTNG_OK on success or a negative error code.
1083 int lttng_create_session(const char *name
, const char *url
)
1086 struct lttcomm_session_msg lsm
;
1087 struct lttng_uri
*uris
= NULL
;
1090 return -LTTNG_ERR_INVALID
;
1093 memset(&lsm
, 0, sizeof(lsm
));
1095 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1096 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1098 /* There should never be a data URL */
1099 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1101 return -LTTNG_ERR_INVALID
;
1104 lsm
.u
.uri
.size
= size
;
1106 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1111 * Destroy session using name.
1112 * Returns size of returned session payload data or a negative error code.
1114 int lttng_destroy_session(const char *session_name
)
1116 struct lttcomm_session_msg lsm
;
1118 if (session_name
== NULL
) {
1119 return -LTTNG_ERR_INVALID
;
1122 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1124 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1126 return ask_sessiond(&lsm
, NULL
);
1130 * Ask the session daemon for all available sessions.
1131 * Sets the contents of the sessions array.
1132 * Returns the number of lttng_session entries in sessions;
1133 * on error, returns a negative value.
1135 int lttng_list_sessions(struct lttng_session
**sessions
)
1138 struct lttcomm_session_msg lsm
;
1140 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1141 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1146 return ret
/ sizeof(struct lttng_session
);
1150 * Ask the session daemon for all available domains of a session.
1151 * Sets the contents of the domains array.
1152 * Returns the number of lttng_domain entries in domains;
1153 * on error, returns a negative value.
1155 int lttng_list_domains(const char *session_name
,
1156 struct lttng_domain
**domains
)
1159 struct lttcomm_session_msg lsm
;
1161 if (session_name
== NULL
) {
1162 return -LTTNG_ERR_INVALID
;
1165 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1167 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1169 ret
= ask_sessiond(&lsm
, (void**) domains
);
1174 return ret
/ sizeof(struct lttng_domain
);
1178 * Ask the session daemon for all available channels of a session.
1179 * Sets the contents of the channels array.
1180 * Returns the number of lttng_channel entries in channels;
1181 * on error, returns a negative value.
1183 int lttng_list_channels(struct lttng_handle
*handle
,
1184 struct lttng_channel
**channels
)
1187 struct lttcomm_session_msg lsm
;
1189 if (handle
== NULL
) {
1190 return -LTTNG_ERR_INVALID
;
1193 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1194 copy_string(lsm
.session
.name
, handle
->session_name
,
1195 sizeof(lsm
.session
.name
));
1197 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1199 ret
= ask_sessiond(&lsm
, (void**) channels
);
1204 return ret
/ sizeof(struct lttng_channel
);
1208 * Ask the session daemon for all available events of a session channel.
1209 * Sets the contents of the events array.
1210 * Returns the number of lttng_event entries in events;
1211 * on error, returns a negative value.
1213 int lttng_list_events(struct lttng_handle
*handle
,
1214 const char *channel_name
, struct lttng_event
**events
)
1217 struct lttcomm_session_msg lsm
;
1219 /* Safety check. An handle and channel name are mandatory */
1220 if (handle
== NULL
|| channel_name
== NULL
) {
1221 return -LTTNG_ERR_INVALID
;
1224 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1225 copy_string(lsm
.session
.name
, handle
->session_name
,
1226 sizeof(lsm
.session
.name
));
1227 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1228 sizeof(lsm
.u
.list
.channel_name
));
1230 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1232 ret
= ask_sessiond(&lsm
, (void**) events
);
1237 return ret
/ sizeof(struct lttng_event
);
1241 * Sets the tracing_group variable with name.
1242 * This function allocates memory pointed to by tracing_group.
1243 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1245 int lttng_set_tracing_group(const char *name
)
1248 return -LTTNG_ERR_INVALID
;
1251 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1252 return -LTTNG_ERR_FATAL
;
1259 * Returns size of returned session payload data or a negative error code.
1261 int lttng_calibrate(struct lttng_handle
*handle
,
1262 struct lttng_calibrate
*calibrate
)
1264 struct lttcomm_session_msg lsm
;
1266 /* Safety check. NULL pointer are forbidden */
1267 if (handle
== NULL
|| calibrate
== NULL
) {
1268 return -LTTNG_ERR_INVALID
;
1271 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1272 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1274 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1276 return ask_sessiond(&lsm
, NULL
);
1280 * Set default channel attributes.
1281 * If either or both of the arguments are null, attr content is zeroe'd.
1283 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1284 struct lttng_channel_attr
*attr
)
1287 if (attr
== NULL
|| domain
== NULL
) {
1291 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1293 switch (domain
->type
) {
1294 case LTTNG_DOMAIN_KERNEL
:
1295 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1296 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1297 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1299 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1300 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1301 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1303 case LTTNG_DOMAIN_UST
:
1305 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1306 case LTTNG_DOMAIN_UST_PID
:
1307 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1309 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1310 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1311 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1313 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
1314 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1315 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1318 /* Default behavior: leave set to 0. */
1324 * Check if session daemon is alive.
1326 * Return 1 if alive or 0 if not.
1327 * On error returns a negative value.
1329 int lttng_session_daemon_alive(void)
1333 ret
= set_session_daemon_path();
1339 if (strlen(sessiond_sock_path
) == 0) {
1341 * No socket path set. Weird error which means the constructor was not
1347 ret
= try_connect_sessiond(sessiond_sock_path
);
1358 * Set URL for a consumer for a session and domain.
1360 * Return 0 on success, else a negative value.
1362 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1363 const char *control_url
, const char *data_url
)
1366 struct lttcomm_session_msg lsm
;
1367 struct lttng_uri
*uris
= NULL
;
1369 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1370 return -LTTNG_ERR_INVALID
;
1373 memset(&lsm
, 0, sizeof(lsm
));
1375 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1377 copy_string(lsm
.session
.name
, handle
->session_name
,
1378 sizeof(lsm
.session
.name
));
1379 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1381 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1383 return -LTTNG_ERR_INVALID
;
1386 lsm
.u
.uri
.size
= size
;
1388 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1393 * Enable consumer for a session and domain.
1395 * Return 0 on success, else a negative value.
1397 int lttng_enable_consumer(struct lttng_handle
*handle
)
1399 struct lttcomm_session_msg lsm
;
1401 if (handle
== NULL
) {
1402 return -LTTNG_ERR_INVALID
;
1405 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1407 copy_string(lsm
.session
.name
, handle
->session_name
,
1408 sizeof(lsm
.session
.name
));
1409 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1411 return ask_sessiond(&lsm
, NULL
);
1415 * Disable consumer for a session and domain.
1417 * Return 0 on success, else a negative value.
1419 int lttng_disable_consumer(struct lttng_handle
*handle
)
1421 struct lttcomm_session_msg lsm
;
1423 if (handle
== NULL
) {
1424 return -LTTNG_ERR_INVALID
;
1427 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1429 copy_string(lsm
.session
.name
, handle
->session_name
,
1430 sizeof(lsm
.session
.name
));
1431 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1433 return ask_sessiond(&lsm
, NULL
);
1437 * Set health socket path by putting it in the global health_sock_path
1440 * Returns 0 on success or assert(0) on ENOMEM.
1442 static int set_health_socket_path(void)
1445 int in_tgroup
= 0; /* In tracing group */
1452 /* Are we in the tracing group ? */
1453 in_tgroup
= check_tracing_group(tracing_group
);
1456 if ((uid
== 0) || in_tgroup
) {
1457 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1458 sizeof(health_sock_path
));
1463 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1464 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1466 home
= getenv("HOME");
1468 /* Fallback in /tmp .. */
1472 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1473 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1474 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1475 /* ENOMEM at this point... just kill the control lib. */
1484 * Check session daemon health for a specific health component.
1486 * Return 0 if health is OK or else 1 if BAD.
1488 * Any other negative value is a lttng error code which can be translated with
1491 int lttng_health_check(enum lttng_health_component c
)
1494 struct lttcomm_health_msg msg
;
1495 struct lttcomm_health_data reply
;
1497 /* Connect to the sesssion daemon */
1498 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1500 ret
= -LTTNG_ERR_NO_SESSIOND
;
1504 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1507 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1509 ret
= -LTTNG_ERR_FATAL
;
1513 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1515 ret
= -LTTNG_ERR_FATAL
;
1519 ret
= reply
.ret_code
;
1529 * This is an extension of create session that is ONLY and SHOULD only be used
1530 * by the lttng command line program. It exists to avoid using URI parsing in
1533 * We need the date and time for the trace path subdirectory for the case where
1534 * the user does NOT define one using either -o or -U. Using the normal
1535 * lttng_create_session API call, we have no clue on the session daemon side if
1536 * the URL was generated automatically by the client or define by the user.
1538 * So this function "wrapper" is hidden from the public API, takes the datetime
1539 * string and appends it if necessary to the URI subdirectory before sending it
1540 * to the session daemon.
1542 * With this extra function, the lttng_create_session call behavior is not
1543 * changed and the timestamp is appended to the URI on the session daemon side
1546 int _lttng_create_session_ext(const char *name
, const char *url
,
1547 const char *datetime
)
1551 struct lttcomm_session_msg lsm
;
1552 struct lttng_uri
*uris
= NULL
;
1554 if (name
== NULL
|| datetime
== NULL
) {
1555 return -LTTNG_ERR_INVALID
;
1558 memset(&lsm
, 0, sizeof(lsm
));
1560 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1561 if (!strncmp(name
, DEFAULT_SESSION_NAME
, strlen(DEFAULT_SESSION_NAME
))) {
1562 ret
= snprintf(lsm
.session
.name
, sizeof(lsm
.session
.name
), "%s-%s",
1565 PERROR("snprintf session name datetime");
1566 return -LTTNG_ERR_FATAL
;
1569 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1572 /* There should never be a data URL */
1573 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1575 return -LTTNG_ERR_INVALID
;
1578 lsm
.u
.uri
.size
= size
;
1580 if (uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1581 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s", name
,
1584 PERROR("snprintf uri subdir");
1585 return -LTTNG_ERR_FATAL
;
1589 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1594 * For a given session name, this call checks if the data is ready to be read
1595 * or is still being extracted by the consumer(s) hence not ready to be used by
1598 int lttng_data_available(const char *session_name
)
1601 struct lttcomm_session_msg lsm
;
1603 if (session_name
== NULL
) {
1604 return -LTTNG_ERR_INVALID
;
1607 lsm
.cmd_type
= LTTNG_DATA_AVAILABLE
;
1609 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1611 ret
= ask_sessiond(&lsm
, NULL
);
1614 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1615 * getting -1 means that the reply ret_code was 1 thus meaning that the
1616 * data is available. Yes it is hackish but for now this is the only way.
1628 static void __attribute__((constructor
)) init()
1630 /* Set default session group */
1631 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1632 /* Set socket for health check */
1633 (void) set_health_socket_path();