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
)
94 unsigned int equal
= 1, idx
= 0;
95 /* Add the "file://" size to the URL maximum size */
96 char url
[PATH_MAX
+ 7];
97 ssize_t size_ctrl
= 0, size_data
= 0, size
;
98 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
99 struct lttng_uri
*tmp_uris
= NULL
;
101 /* No URL(s) is allowed. This means that the consumer will be disabled. */
102 if (ctrl_url
== NULL
&& data_url
== NULL
) {
106 /* Check if URLs are equal and if so, only use the control URL */
107 if (ctrl_url
&& data_url
) {
108 equal
= !strcmp(ctrl_url
, data_url
);
112 * Since we allow the str_url to be a full local filesystem path, we are
113 * going to create a valid file:// URL if it's the case.
115 * Check if first character is a '/' or else reject the URL.
117 if (ctrl_url
&& ctrl_url
[0] == '/') {
118 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
120 PERROR("snprintf file url");
126 /* Parse the control URL if there is one */
128 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
130 ERR("Unable to parse the URL %s", ctrl_url
);
134 /* At this point, we know there is at least one URI in the array */
135 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
137 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
138 ERR("Can not have a data URL when destination is file://");
142 /* URL are not equal but the control URL uses a net:// protocol */
143 if (size_ctrl
== 2) {
145 ERR("Control URL uses the net:// protocol and the data URL is "
146 "different. Not allowed.");
149 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
151 * The data_url and ctrl_url are equal and the ctrl_url
152 * contains a net:// protocol so we just skip the data part.
160 /* We have to parse the data URL in this case */
161 size_data
= uri_parse(data_url
, &data_uris
);
163 ERR("Unable to parse the URL %s", data_url
);
165 } else if (size_data
== 2) {
166 ERR("Data URL can not be set with the net[4|6]:// protocol");
170 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
173 /* Compute total size */
174 size
= size_ctrl
+ size_data
;
176 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
177 if (tmp_uris
== NULL
) {
178 PERROR("zmalloc uris");
183 /* It's possible the control URIs array contains more than one URI */
184 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
189 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
205 * Copy string from src to dst and enforce null terminated byte.
207 static void copy_string(char *dst
, const char *src
, size_t len
)
210 strncpy(dst
, src
, len
);
211 /* Enforce the NULL terminated byte */
219 * Copy domain to lttcomm_session_msg domain.
221 * If domain is unknown, default domain will be the kernel.
223 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
227 case LTTNG_DOMAIN_KERNEL
:
228 case LTTNG_DOMAIN_UST
:
230 case LTTNG_DOMAIN_UST_EXEC_NAME:
231 case LTTNG_DOMAIN_UST_PID:
232 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
234 memcpy(dst
, src
, sizeof(struct lttng_domain
));
237 memset(dst
, 0, sizeof(struct lttng_domain
));
244 * Send lttcomm_session_msg to the session daemon.
246 * On success, returns the number of bytes sent (>=0)
247 * On error, returns -1
249 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
254 ret
= -LTTNG_ERR_NO_SESSIOND
;
258 DBG("LSM cmd type : %d", lsm
->cmd_type
);
260 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
261 sizeof(struct lttcomm_session_msg
));
263 ret
= -LTTNG_ERR_FATAL
;
271 * Send var len data to the session daemon.
273 * On success, returns the number of bytes sent (>=0)
274 * On error, returns -1
276 static int send_session_varlen(void *data
, size_t len
)
281 ret
= -LTTNG_ERR_NO_SESSIOND
;
290 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
292 ret
= -LTTNG_ERR_FATAL
;
300 * Receive data from the sessiond socket.
302 * On success, returns the number of bytes received (>=0)
303 * On error, returns -1 (recvmsg() error) or -ENOTCONN
305 static int recv_data_sessiond(void *buf
, size_t len
)
310 ret
= -LTTNG_ERR_NO_SESSIOND
;
314 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
316 ret
= -LTTNG_ERR_FATAL
;
324 * Check if we are in the specified group.
326 * If yes return 1, else return -1.
328 static int check_tracing_group(const char *grp_name
)
330 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
332 int grp_list_size
, grp_id
, i
;
335 /* Get GID of group 'tracing' */
336 grp_tracing
= getgrnam(grp_name
);
338 /* If grp_tracing is NULL, the group does not exist. */
342 /* Get number of supplementary group IDs */
343 grp_list_size
= getgroups(0, NULL
);
344 if (grp_list_size
< 0) {
349 /* Alloc group list of the right size */
350 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
355 grp_id
= getgroups(grp_list_size
, grp_list
);
361 for (i
= 0; i
< grp_list_size
; i
++) {
362 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
376 * Try connect to session daemon with sock_path.
378 * Return 0 on success, else -1
380 static int try_connect_sessiond(const char *sock_path
)
384 /* If socket exist, we check if the daemon listens for connect. */
385 ret
= access(sock_path
, F_OK
);
391 ret
= lttcomm_connect_unix_sock(sock_path
);
397 ret
= lttcomm_close_unix_sock(ret
);
399 perror("lttcomm_close_unix_sock");
409 * Set sessiond socket path by putting it in the global sessiond_sock_path
412 * Returns 0 on success, negative value on failure (the sessiond socket path
413 * is somehow too long or ENOMEM).
415 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
));
436 ret
= try_connect_sessiond(sessiond_sock_path
);
440 /* Global session daemon not available... */
442 /* ...or not in tracing group (and not root), default */
445 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
446 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
448 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
449 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
450 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
462 * Connect to the LTTng session daemon.
464 * On success, return 0. On error, return -1.
466 static int connect_sessiond(void)
470 ret
= set_session_daemon_path();
475 /* Connect to the sesssion daemon */
476 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
481 sessiond_socket
= ret
;
491 * Clean disconnect from the session daemon.
492 * On success, return 0. On error, return -1.
494 static int disconnect_sessiond(void)
499 ret
= lttcomm_close_unix_sock(sessiond_socket
);
508 * Ask the session daemon a specific command and put the data into buf.
509 * Takes extra var. len. data as input to send to the session daemon.
511 * Return size of data (only payload, not header) or a negative error code.
513 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
514 void *vardata
, size_t varlen
, void **buf
)
519 struct lttcomm_lttng_msg llm
;
521 ret
= connect_sessiond();
523 ret
= -LTTNG_ERR_NO_SESSIOND
;
527 /* Send command to session daemon */
528 ret
= send_session_msg(lsm
);
530 /* Ret value is a valid lttng error code. */
533 /* Send var len data */
534 ret
= send_session_varlen(vardata
, varlen
);
536 /* Ret value is a valid lttng error code. */
540 /* Get header from data transmission */
541 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
543 /* Ret value is a valid lttng error code. */
547 /* Check error code if OK */
548 if (llm
.ret_code
!= LTTNG_OK
) {
553 size
= llm
.data_size
;
555 /* If client free with size 0 */
563 data
= (void*) malloc(size
);
565 /* Get payload data */
566 ret
= recv_data_sessiond(data
, size
);
573 * Extra protection not to dereference a NULL pointer. If buf is NULL at
574 * this point, an error is returned and data is freed.
577 ret
= -LTTNG_ERR_INVALID
;
586 disconnect_sessiond();
591 * Ask the session daemon a specific command and put the data into buf.
593 * Return size of data (only payload, not header) or a negative error code.
595 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
597 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
601 * Create lttng handle and return pointer.
602 * The returned pointer will be NULL in case of malloc() error.
604 struct lttng_handle
*lttng_create_handle(const char *session_name
,
605 struct lttng_domain
*domain
)
607 struct lttng_handle
*handle
= NULL
;
609 if (domain
== NULL
) {
613 handle
= malloc(sizeof(struct lttng_handle
));
614 if (handle
== NULL
) {
615 PERROR("malloc handle");
619 /* Copy session name */
620 copy_string(handle
->session_name
, session_name
,
621 sizeof(handle
->session_name
));
623 /* Copy lttng domain */
624 copy_lttng_domain(&handle
->domain
, domain
);
631 * Destroy handle by free(3) the pointer.
633 void lttng_destroy_handle(struct lttng_handle
*handle
)
641 * Register an outside consumer.
642 * Returns size of returned session payload data or a negative error code.
644 int lttng_register_consumer(struct lttng_handle
*handle
,
645 const char *socket_path
)
647 struct lttcomm_session_msg lsm
;
649 if (handle
== NULL
|| socket_path
== NULL
) {
650 return -LTTNG_ERR_INVALID
;
653 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
654 copy_string(lsm
.session
.name
, handle
->session_name
,
655 sizeof(lsm
.session
.name
));
656 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
658 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
660 return ask_sessiond(&lsm
, NULL
);
664 * Start tracing for all traces of the session.
665 * Returns size of returned session payload data or a negative error code.
667 int lttng_start_tracing(const char *session_name
)
669 struct lttcomm_session_msg lsm
;
671 if (session_name
== NULL
) {
672 return -LTTNG_ERR_INVALID
;
675 lsm
.cmd_type
= LTTNG_START_TRACE
;
677 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
679 return ask_sessiond(&lsm
, NULL
);
683 * Stop tracing for all traces of the session.
685 static int _lttng_stop_tracing(const char *session_name
, int wait
)
688 struct lttcomm_session_msg lsm
;
690 if (session_name
== NULL
) {
691 return -LTTNG_ERR_INVALID
;
694 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
696 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
698 ret
= ask_sessiond(&lsm
, NULL
);
699 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
707 _MSG("Waiting for data availability");
709 /* Check for data availability */
711 data_ret
= lttng_data_pending(session_name
);
713 /* Return the data available call error. */
719 * Data sleep time before retrying (in usec). Don't sleep if the call
720 * returned value indicates availability.
723 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
726 } while (data_ret
!= 0);
736 * Stop tracing and wait for data availability.
738 int lttng_stop_tracing(const char *session_name
)
740 return _lttng_stop_tracing(session_name
, 1);
744 * Stop tracing but _don't_ wait for data availability.
746 int lttng_stop_tracing_no_wait(const char *session_name
)
748 return _lttng_stop_tracing(session_name
, 0);
752 * Add context to a channel.
754 * If the given channel is NULL, add the contexts to all channels.
755 * The event_name param is ignored.
757 * Returns the size of the returned payload data or a negative error code.
759 int lttng_add_context(struct lttng_handle
*handle
,
760 struct lttng_event_context
*ctx
, const char *event_name
,
761 const char *channel_name
)
763 struct lttcomm_session_msg lsm
;
765 /* Safety check. Both are mandatory */
766 if (handle
== NULL
|| ctx
== NULL
) {
767 return -LTTNG_ERR_INVALID
;
770 memset(&lsm
, 0, sizeof(lsm
));
772 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
774 /* Copy channel name */
775 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
776 sizeof(lsm
.u
.context
.channel_name
));
778 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
780 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
782 copy_string(lsm
.session
.name
, handle
->session_name
,
783 sizeof(lsm
.session
.name
));
785 return ask_sessiond(&lsm
, NULL
);
789 * Enable event(s) for a channel.
790 * If no event name is specified, all events are enabled.
791 * If no channel name is specified, the default 'channel0' is used.
792 * Returns size of returned session payload data or a negative error code.
794 int lttng_enable_event(struct lttng_handle
*handle
,
795 struct lttng_event
*ev
, const char *channel_name
)
797 struct lttcomm_session_msg lsm
;
799 if (handle
== NULL
|| ev
== NULL
) {
800 return -LTTNG_ERR_INVALID
;
803 memset(&lsm
, 0, sizeof(lsm
));
805 /* If no channel name, we put the default name */
806 if (channel_name
== NULL
) {
807 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
808 sizeof(lsm
.u
.enable
.channel_name
));
810 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
811 sizeof(lsm
.u
.enable
.channel_name
));
814 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
816 if (ev
->name
[0] != '\0') {
817 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
819 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
821 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
823 copy_string(lsm
.session
.name
, handle
->session_name
,
824 sizeof(lsm
.session
.name
));
826 return ask_sessiond(&lsm
, NULL
);
830 * Create or enable an event with a filter expression.
832 * Return negative error value on error.
833 * Return size of returned session payload data if OK.
835 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
836 struct lttng_event
*event
, const char *channel_name
,
837 const char *filter_expression
)
839 struct lttcomm_session_msg lsm
;
840 struct filter_parser_ctx
*ctx
;
844 if (!filter_expression
) {
846 * Fall back to normal event enabling if no filter
849 return lttng_enable_event(handle
, event
, channel_name
);
853 * Empty filter string will always be rejected by the parser
854 * anyway, so treat this corner-case early to eliminate
855 * lttng_fmemopen error for 0-byte allocation.
857 if (handle
== NULL
|| filter_expression
[0] == '\0') {
858 return -LTTNG_ERR_INVALID
;
862 * casting const to non-const, as the underlying function will
863 * use it in read-only mode.
865 fmem
= lttng_fmemopen((void *) filter_expression
,
866 strlen(filter_expression
), "r");
868 fprintf(stderr
, "Error opening memory as stream\n");
869 return -LTTNG_ERR_FILTER_NOMEM
;
871 ctx
= filter_parser_ctx_alloc(fmem
);
873 fprintf(stderr
, "Error allocating parser\n");
874 ret
= -LTTNG_ERR_FILTER_NOMEM
;
877 ret
= filter_parser_ctx_append_ast(ctx
);
879 fprintf(stderr
, "Parse error\n");
880 ret
= -LTTNG_ERR_FILTER_INVAL
;
883 ret
= filter_visitor_set_parent(ctx
);
885 fprintf(stderr
, "Set parent error\n");
886 ret
= -LTTNG_ERR_FILTER_INVAL
;
890 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
893 fprintf(stderr
, "XML print error\n");
894 ret
= -LTTNG_ERR_FILTER_INVAL
;
899 dbg_printf("Generating IR... ");
901 ret
= filter_visitor_ir_generate(ctx
);
903 fprintf(stderr
, "Generate IR error\n");
904 ret
= -LTTNG_ERR_FILTER_INVAL
;
907 dbg_printf("done\n");
909 dbg_printf("Validating IR... ");
911 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
913 ret
= -LTTNG_ERR_FILTER_INVAL
;
916 dbg_printf("done\n");
918 dbg_printf("Generating bytecode... ");
920 ret
= filter_visitor_bytecode_generate(ctx
);
922 fprintf(stderr
, "Generate bytecode error\n");
923 ret
= -LTTNG_ERR_FILTER_INVAL
;
926 dbg_printf("done\n");
927 dbg_printf("Size of bytecode generated: %u bytes.\n",
928 bytecode_get_len(&ctx
->bytecode
->b
));
930 memset(&lsm
, 0, sizeof(lsm
));
932 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
934 /* Copy channel name */
935 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
936 sizeof(lsm
.u
.enable
.channel_name
));
937 /* Copy event name */
939 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
942 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
943 + bytecode_get_len(&ctx
->bytecode
->b
);
945 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
947 copy_string(lsm
.session
.name
, handle
->session_name
,
948 sizeof(lsm
.session
.name
));
950 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
951 lsm
.u
.enable
.bytecode_len
, NULL
);
953 filter_bytecode_free(ctx
);
955 filter_parser_ctx_free(ctx
);
956 if (fclose(fmem
) != 0) {
962 filter_bytecode_free(ctx
);
964 filter_parser_ctx_free(ctx
);
966 if (fclose(fmem
) != 0) {
973 * Disable event(s) of a channel and domain.
974 * If no event name is specified, all events are disabled.
975 * If no channel name is specified, the default 'channel0' is used.
976 * Returns size of returned session payload data or a negative error code.
978 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
979 const char *channel_name
)
981 struct lttcomm_session_msg lsm
;
983 if (handle
== NULL
) {
984 return -LTTNG_ERR_INVALID
;
987 memset(&lsm
, 0, sizeof(lsm
));
990 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
991 sizeof(lsm
.u
.disable
.channel_name
));
993 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
994 sizeof(lsm
.u
.disable
.channel_name
));
997 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1000 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
1001 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1003 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
1006 copy_string(lsm
.session
.name
, handle
->session_name
,
1007 sizeof(lsm
.session
.name
));
1009 return ask_sessiond(&lsm
, NULL
);
1013 * Enable channel per domain
1014 * Returns size of returned session payload data or a negative error code.
1016 int lttng_enable_channel(struct lttng_handle
*handle
,
1017 struct lttng_channel
*chan
)
1019 struct lttcomm_session_msg lsm
;
1022 * NULL arguments are forbidden. No default values.
1024 if (handle
== NULL
|| chan
== NULL
) {
1025 return -LTTNG_ERR_INVALID
;
1028 memset(&lsm
, 0, sizeof(lsm
));
1030 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1032 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1034 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1036 copy_string(lsm
.session
.name
, handle
->session_name
,
1037 sizeof(lsm
.session
.name
));
1039 return ask_sessiond(&lsm
, NULL
);
1043 * All tracing will be stopped for registered events of the channel.
1044 * Returns size of returned session payload data or a negative error code.
1046 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1048 struct lttcomm_session_msg lsm
;
1050 /* Safety check. Both are mandatory */
1051 if (handle
== NULL
|| name
== NULL
) {
1052 return -LTTNG_ERR_INVALID
;
1055 memset(&lsm
, 0, sizeof(lsm
));
1057 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1059 copy_string(lsm
.u
.disable
.channel_name
, name
,
1060 sizeof(lsm
.u
.disable
.channel_name
));
1062 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1064 copy_string(lsm
.session
.name
, handle
->session_name
,
1065 sizeof(lsm
.session
.name
));
1067 return ask_sessiond(&lsm
, NULL
);
1071 * Lists all available tracepoints of domain.
1072 * Sets the contents of the events array.
1073 * Returns the number of lttng_event entries in events;
1074 * on error, returns a negative value.
1076 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1077 struct lttng_event
**events
)
1080 struct lttcomm_session_msg lsm
;
1082 if (handle
== NULL
) {
1083 return -LTTNG_ERR_INVALID
;
1086 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1087 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1089 ret
= ask_sessiond(&lsm
, (void **) events
);
1094 return ret
/ sizeof(struct lttng_event
);
1098 * Lists all available tracepoint fields of domain.
1099 * Sets the contents of the event field array.
1100 * Returns the number of lttng_event_field entries in events;
1101 * on error, returns a negative value.
1103 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1104 struct lttng_event_field
**fields
)
1107 struct lttcomm_session_msg lsm
;
1109 if (handle
== NULL
) {
1110 return -LTTNG_ERR_INVALID
;
1113 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1114 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1116 ret
= ask_sessiond(&lsm
, (void **) fields
);
1121 return ret
/ sizeof(struct lttng_event_field
);
1125 * Returns a human readable string describing
1126 * the error code (a negative value).
1128 const char *lttng_strerror(int code
)
1130 return error_get_str(code
);
1134 * Create a brand new session using name and url for destination.
1136 * Returns LTTNG_OK on success or a negative error code.
1138 int lttng_create_session(const char *name
, const char *url
)
1141 struct lttcomm_session_msg lsm
;
1142 struct lttng_uri
*uris
= NULL
;
1145 return -LTTNG_ERR_INVALID
;
1148 memset(&lsm
, 0, sizeof(lsm
));
1150 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1151 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1153 /* There should never be a data URL */
1154 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1156 return -LTTNG_ERR_INVALID
;
1159 lsm
.u
.uri
.size
= size
;
1161 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1166 * Destroy session using name.
1167 * Returns size of returned session payload data or a negative error code.
1169 int lttng_destroy_session(const char *session_name
)
1171 struct lttcomm_session_msg lsm
;
1173 if (session_name
== NULL
) {
1174 return -LTTNG_ERR_INVALID
;
1177 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1179 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1181 return ask_sessiond(&lsm
, NULL
);
1185 * Ask the session daemon for all available sessions.
1186 * Sets the contents of the sessions array.
1187 * Returns the number of lttng_session entries in sessions;
1188 * on error, returns a negative value.
1190 int lttng_list_sessions(struct lttng_session
**sessions
)
1193 struct lttcomm_session_msg lsm
;
1195 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1196 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1201 return ret
/ sizeof(struct lttng_session
);
1205 * Ask the session daemon for all available domains of a session.
1206 * Sets the contents of the domains array.
1207 * Returns the number of lttng_domain entries in domains;
1208 * on error, returns a negative value.
1210 int lttng_list_domains(const char *session_name
,
1211 struct lttng_domain
**domains
)
1214 struct lttcomm_session_msg lsm
;
1216 if (session_name
== NULL
) {
1217 return -LTTNG_ERR_INVALID
;
1220 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1222 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1224 ret
= ask_sessiond(&lsm
, (void**) domains
);
1229 return ret
/ sizeof(struct lttng_domain
);
1233 * Ask the session daemon for all available channels of a session.
1234 * Sets the contents of the channels array.
1235 * Returns the number of lttng_channel entries in channels;
1236 * on error, returns a negative value.
1238 int lttng_list_channels(struct lttng_handle
*handle
,
1239 struct lttng_channel
**channels
)
1242 struct lttcomm_session_msg lsm
;
1244 if (handle
== NULL
) {
1245 return -LTTNG_ERR_INVALID
;
1248 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1249 copy_string(lsm
.session
.name
, handle
->session_name
,
1250 sizeof(lsm
.session
.name
));
1252 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1254 ret
= ask_sessiond(&lsm
, (void**) channels
);
1259 return ret
/ sizeof(struct lttng_channel
);
1263 * Ask the session daemon for all available events of a session channel.
1264 * Sets the contents of the events array.
1265 * Returns the number of lttng_event entries in events;
1266 * on error, returns a negative value.
1268 int lttng_list_events(struct lttng_handle
*handle
,
1269 const char *channel_name
, struct lttng_event
**events
)
1272 struct lttcomm_session_msg lsm
;
1274 /* Safety check. An handle and channel name are mandatory */
1275 if (handle
== NULL
|| channel_name
== NULL
) {
1276 return -LTTNG_ERR_INVALID
;
1279 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1280 copy_string(lsm
.session
.name
, handle
->session_name
,
1281 sizeof(lsm
.session
.name
));
1282 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1283 sizeof(lsm
.u
.list
.channel_name
));
1285 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1287 ret
= ask_sessiond(&lsm
, (void**) events
);
1292 return ret
/ sizeof(struct lttng_event
);
1296 * Sets the tracing_group variable with name.
1297 * This function allocates memory pointed to by tracing_group.
1298 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1300 int lttng_set_tracing_group(const char *name
)
1303 return -LTTNG_ERR_INVALID
;
1306 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1307 return -LTTNG_ERR_FATAL
;
1314 * Returns size of returned session payload data or a negative error code.
1316 int lttng_calibrate(struct lttng_handle
*handle
,
1317 struct lttng_calibrate
*calibrate
)
1319 struct lttcomm_session_msg lsm
;
1321 /* Safety check. NULL pointer are forbidden */
1322 if (handle
== NULL
|| calibrate
== NULL
) {
1323 return -LTTNG_ERR_INVALID
;
1326 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1327 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1329 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1331 return ask_sessiond(&lsm
, NULL
);
1335 * Set default channel attributes.
1336 * If either or both of the arguments are null, attr content is zeroe'd.
1338 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1339 struct lttng_channel_attr
*attr
)
1342 if (attr
== NULL
|| domain
== NULL
) {
1346 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1348 switch (domain
->type
) {
1349 case LTTNG_DOMAIN_KERNEL
:
1350 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1351 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1352 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1354 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1355 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1356 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1358 case LTTNG_DOMAIN_UST
:
1360 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1361 case LTTNG_DOMAIN_UST_PID
:
1362 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1364 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1365 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1366 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1368 attr
->subbuf_size
= default_get_ust_channel_subbuf_size();
1369 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1370 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1373 /* Default behavior: leave set to 0. */
1379 * Check if session daemon is alive.
1381 * Return 1 if alive or 0 if not.
1382 * On error returns a negative value.
1384 int lttng_session_daemon_alive(void)
1388 ret
= set_session_daemon_path();
1394 if (strlen(sessiond_sock_path
) == 0) {
1396 * No socket path set. Weird error which means the constructor was not
1402 ret
= try_connect_sessiond(sessiond_sock_path
);
1413 * Set URL for a consumer for a session and domain.
1415 * Return 0 on success, else a negative value.
1417 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1418 const char *control_url
, const char *data_url
)
1421 struct lttcomm_session_msg lsm
;
1422 struct lttng_uri
*uris
= NULL
;
1424 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1425 return -LTTNG_ERR_INVALID
;
1428 memset(&lsm
, 0, sizeof(lsm
));
1430 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1432 copy_string(lsm
.session
.name
, handle
->session_name
,
1433 sizeof(lsm
.session
.name
));
1434 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1436 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1438 return -LTTNG_ERR_INVALID
;
1441 lsm
.u
.uri
.size
= size
;
1443 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1448 * Enable consumer for a session and domain.
1450 * Return 0 on success, else a negative value.
1452 int lttng_enable_consumer(struct lttng_handle
*handle
)
1454 struct lttcomm_session_msg lsm
;
1456 if (handle
== NULL
) {
1457 return -LTTNG_ERR_INVALID
;
1460 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1462 copy_string(lsm
.session
.name
, handle
->session_name
,
1463 sizeof(lsm
.session
.name
));
1464 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1466 return ask_sessiond(&lsm
, NULL
);
1470 * Disable consumer for a session and domain.
1472 * Return 0 on success, else a negative value.
1474 int lttng_disable_consumer(struct lttng_handle
*handle
)
1476 struct lttcomm_session_msg lsm
;
1478 if (handle
== NULL
) {
1479 return -LTTNG_ERR_INVALID
;
1482 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1484 copy_string(lsm
.session
.name
, handle
->session_name
,
1485 sizeof(lsm
.session
.name
));
1486 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1488 return ask_sessiond(&lsm
, NULL
);
1492 * Set health socket path by putting it in the global health_sock_path
1495 * Returns 0 on success or assert(0) on ENOMEM.
1497 static int set_health_socket_path(void)
1500 int in_tgroup
= 0; /* In tracing group */
1507 /* Are we in the tracing group ? */
1508 in_tgroup
= check_tracing_group(tracing_group
);
1511 if ((uid
== 0) || in_tgroup
) {
1512 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1513 sizeof(health_sock_path
));
1518 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1519 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1521 home
= getenv("HOME");
1523 /* Fallback in /tmp .. */
1527 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1528 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1529 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1530 /* ENOMEM at this point... just kill the control lib. */
1539 * Check session daemon health for a specific health component.
1541 * Return 0 if health is OK or else 1 if BAD.
1543 * Any other negative value is a lttng error code which can be translated with
1546 int lttng_health_check(enum lttng_health_component c
)
1549 struct lttcomm_health_msg msg
;
1550 struct lttcomm_health_data reply
;
1552 /* Connect to the sesssion daemon */
1553 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1555 ret
= -LTTNG_ERR_NO_SESSIOND
;
1559 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1562 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1564 ret
= -LTTNG_ERR_FATAL
;
1568 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1570 ret
= -LTTNG_ERR_FATAL
;
1574 ret
= reply
.ret_code
;
1584 * This is an extension of create session that is ONLY and SHOULD only be used
1585 * by the lttng command line program. It exists to avoid using URI parsing in
1588 * We need the date and time for the trace path subdirectory for the case where
1589 * the user does NOT define one using either -o or -U. Using the normal
1590 * lttng_create_session API call, we have no clue on the session daemon side if
1591 * the URL was generated automatically by the client or define by the user.
1593 * So this function "wrapper" is hidden from the public API, takes the datetime
1594 * string and appends it if necessary to the URI subdirectory before sending it
1595 * to the session daemon.
1597 * With this extra function, the lttng_create_session call behavior is not
1598 * changed and the timestamp is appended to the URI on the session daemon side
1601 int _lttng_create_session_ext(const char *name
, const char *url
,
1602 const char *datetime
)
1606 struct lttcomm_session_msg lsm
;
1607 struct lttng_uri
*uris
= NULL
;
1609 if (name
== NULL
|| datetime
== NULL
) {
1610 return -LTTNG_ERR_INVALID
;
1613 memset(&lsm
, 0, sizeof(lsm
));
1615 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1616 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1618 /* There should never be a data URL */
1619 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1621 return -LTTNG_ERR_INVALID
;
1624 lsm
.u
.uri
.size
= size
;
1626 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1627 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s", name
,
1630 PERROR("snprintf uri subdir");
1631 return -LTTNG_ERR_FATAL
;
1635 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1640 * For a given session name, this call checks if the data is ready to be read
1641 * or is still being extracted by the consumer(s) hence not ready to be used by
1644 int lttng_data_pending(const char *session_name
)
1647 struct lttcomm_session_msg lsm
;
1649 if (session_name
== NULL
) {
1650 return -LTTNG_ERR_INVALID
;
1653 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1655 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1657 ret
= ask_sessiond(&lsm
, NULL
);
1660 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1661 * getting -1 means that the reply ret_code was 1 thus meaning that the
1662 * data is available. Yes it is hackish but for now this is the only way.
1674 static void __attribute__((constructor
)) init()
1676 /* Set default session group */
1677 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1678 /* Set socket for health check */
1679 (void) set_health_socket_path();
1685 static void __attribute__((destructor
)) lttng_ctl_exit()
1687 free(tracing_group
);