4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/common.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/payload.h>
27 #include <common/payload-view.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/tracker.h>
30 #include <common/uri.h>
31 #include <common/utils.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/destruction-handle.h>
34 #include <lttng/endpoint.h>
35 #include <lttng/event-internal.h>
36 #include <lttng/health-internal.h>
37 #include <lttng/lttng.h>
38 #include <lttng/session-descriptor-internal.h>
39 #include <lttng/session-internal.h>
40 #include <lttng/trigger/trigger-internal.h>
41 #include <lttng/userspace-probe-internal.h>
43 #include "filter/filter-ast.h"
44 #include "filter/filter-parser.h"
45 #include "filter/filter-bytecode.h"
46 #include "filter/memstream.h"
47 #include "lttng-ctl-helper.h"
50 static const int print_xml
= 1;
51 #define dbg_printf(fmt, args...) \
52 printf("[debug liblttng-ctl] " fmt, ## args)
54 static const int print_xml
= 0;
55 #define dbg_printf(fmt, args...) \
57 /* do nothing but check printf format */ \
59 printf("[debug liblttnctl] " fmt, ## args); \
63 #define COPY_DOMAIN_PACKED(dst, src) \
65 struct lttng_domain _tmp_domain; \
67 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
71 /* Socket to session daemon for communication */
72 static int sessiond_socket
= -1;
73 static char sessiond_sock_path
[PATH_MAX
];
76 static char *tracing_group
;
82 * Those two variables are used by error.h to silent or control the verbosity of
83 * error message. They are global to the library so application linking with it
84 * are able to compile correctly and also control verbosity of the library.
87 int lttng_opt_verbose
;
91 * Copy string from src to dst and enforce null terminated byte.
94 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
97 strncpy(dst
, src
, len
);
98 /* Enforce the NULL terminated byte */
106 * Copy domain to lttcomm_session_msg domain.
108 * If domain is unknown, default domain will be the kernel.
111 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
112 struct lttng_domain
*src
)
116 case LTTNG_DOMAIN_KERNEL
:
117 case LTTNG_DOMAIN_UST
:
118 case LTTNG_DOMAIN_JUL
:
119 case LTTNG_DOMAIN_LOG4J
:
120 case LTTNG_DOMAIN_PYTHON
:
121 memcpy(dst
, src
, sizeof(struct lttng_domain
));
124 memset(dst
, 0, sizeof(struct lttng_domain
));
131 * Send lttcomm_session_msg to the session daemon.
133 * On success, returns the number of bytes sent (>=0)
134 * On error, returns -1
136 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
141 ret
= -LTTNG_ERR_NO_SESSIOND
;
145 DBG("LSM cmd type : %d", lsm
->cmd_type
);
147 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
148 sizeof(struct lttcomm_session_msg
));
150 ret
= -LTTNG_ERR_FATAL
;
158 * Send var len data to the session daemon.
160 * On success, returns the number of bytes sent (>=0)
161 * On error, returns -1
163 static int send_session_varlen(const void *data
, size_t len
)
168 ret
= -LTTNG_ERR_NO_SESSIOND
;
177 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
179 ret
= -LTTNG_ERR_FATAL
;
187 * Send file descriptors to the session daemon.
189 * On success, returns the number of bytes sent (>=0)
190 * On error, returns -1
192 static int send_session_fds(const int *fds
, size_t nb_fd
)
197 ret
= -LTTNG_ERR_NO_SESSIOND
;
201 if (!fds
|| !nb_fd
) {
206 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
208 ret
= -LTTNG_ERR_FATAL
;
216 * Receive data from the sessiond socket.
218 * On success, returns the number of bytes received (>=0)
219 * On error, returns a negative lttng_error_code.
221 static int recv_data_sessiond(void *buf
, size_t len
)
226 ret
= -LTTNG_ERR_NO_SESSIOND
;
230 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
232 ret
= -LTTNG_ERR_FATAL
;
240 * Receive a payload from the session daemon by appending to an existing
242 * On success, returns the number of bytes received (>=0)
243 * On error, returns a negative lttng_error_code.
245 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
248 const size_t original_payload_size
= payload
->buffer
.size
;
250 ret
= lttng_dynamic_buffer_set_size(
251 &payload
->buffer
, payload
->buffer
.size
+ len
);
253 ret
= -LTTNG_ERR_NOMEM
;
257 ret
= recv_data_sessiond(
258 payload
->buffer
.data
+ original_payload_size
, len
);
264 * Check if we are in the specified group.
266 * If yes return 1, else return -1.
269 int lttng_check_tracing_group(void)
271 gid_t
*grp_list
, tracing_gid
;
272 int grp_list_size
, grp_id
, i
;
274 const char *grp_name
= tracing_group
;
276 /* Get GID of group 'tracing' */
277 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
278 /* If grp_tracing is NULL, the group does not exist. */
282 /* Get number of supplementary group IDs */
283 grp_list_size
= getgroups(0, NULL
);
284 if (grp_list_size
< 0) {
289 /* Alloc group list of the right size */
290 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
295 grp_id
= getgroups(grp_list_size
, grp_list
);
301 for (i
= 0; i
< grp_list_size
; i
++) {
302 if (grp_list
[i
] == tracing_gid
) {
315 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
319 size_t best_mem_info
;
320 size_t num_bytes_requested_total
;
323 * Get the number of CPU currently online to compute the amount of
324 * memory needed to create a buffer for every CPU.
326 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
331 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
334 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
335 * reliable estimate we can get but it is only exposed by the kernel
336 * since 3.14. (See Linux kernel commit:
337 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
339 ret
= utils_get_memory_available(&best_mem_info
);
345 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
346 * is a sanity check for obvious user error.
348 ret
= utils_get_memory_total(&best_mem_info
);
356 return best_mem_info
>= num_bytes_requested_total
;
360 * Try connect to session daemon with sock_path.
362 * Return 0 on success, else -1
364 static int try_connect_sessiond(const char *sock_path
)
368 /* If socket exist, we check if the daemon listens for connect. */
369 ret
= access(sock_path
, F_OK
);
375 ret
= lttcomm_connect_unix_sock(sock_path
);
381 ret
= lttcomm_close_unix_sock(ret
);
383 PERROR("lttcomm_close_unix_sock");
393 * Set sessiond socket path by putting it in the global sessiond_sock_path
396 * Returns 0 on success, negative value on failure (the sessiond socket path
397 * is somehow too long or ENOMEM).
399 static int set_session_daemon_path(void)
401 int in_tgroup
= 0; /* In tracing group. */
407 /* Are we in the tracing group ? */
408 in_tgroup
= lttng_check_tracing_group();
411 if ((uid
== 0) || in_tgroup
) {
412 lttng_ctl_copy_string(sessiond_sock_path
,
413 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
421 ret
= try_connect_sessiond(sessiond_sock_path
);
425 /* Global session daemon not available... */
427 /* ...or not in tracing group (and not root), default */
430 * With GNU C < 2.1, snprintf returns -1 if the target buffer
432 * With GNU C >= 2.1, snprintf returns the required size
433 * (excluding closing null)
435 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
436 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
437 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
449 * Connect to the LTTng session daemon.
451 * On success, return the socket's file descriptor. On error, return -1.
453 LTTNG_HIDDEN
int connect_sessiond(void)
457 ret
= set_session_daemon_path();
462 /* Connect to the sesssion daemon. */
463 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
474 static void reset_global_sessiond_connection_state(void)
476 sessiond_socket
= -1;
481 * Clean disconnect from the session daemon.
483 * On success, return 0. On error, return -1.
485 static int disconnect_sessiond(void)
490 ret
= lttcomm_close_unix_sock(sessiond_socket
);
491 reset_global_sessiond_connection_state();
497 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
505 ret
= -LTTNG_ERR_INVALID
;
515 ret
= recv_data_sessiond(buf
, len
);
521 ret
= -LTTNG_ERR_INVALID
;
525 /* Move ownership of command header buffer to user. */
530 /* No command header. */
546 * Ask the session daemon a specific command and put the data into buf.
547 * Takes extra var. len. data and file descriptors as input to send to the
550 * Return size of data (only payload, not header) or a negative error code.
553 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
554 const int *fds
, size_t nb_fd
, const void *vardata
,
555 size_t vardata_len
, void **user_payload_buf
,
556 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
560 struct lttcomm_lttng_msg llm
;
562 ret
= connect_sessiond();
564 ret
= -LTTNG_ERR_NO_SESSIOND
;
567 sessiond_socket
= ret
;
571 ret
= send_session_msg(lsm
);
573 /* Ret value is a valid lttng error code. */
576 /* Send var len data */
577 ret
= send_session_varlen(vardata
, vardata_len
);
579 /* Ret value is a valid lttng error code. */
584 ret
= send_session_fds(fds
, nb_fd
);
586 /* Ret value is a valid lttng error code. */
590 /* Get header from data transmission */
591 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
593 /* Ret value is a valid lttng error code. */
597 /* Check error code if OK */
598 if (llm
.ret_code
!= LTTNG_OK
) {
603 /* Get command header from data transmission */
604 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
605 user_cmd_header_buf
, user_cmd_header_len
);
610 /* Get payload from data transmission */
611 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
620 disconnect_sessiond();
625 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
626 struct lttng_payload
*reply
)
629 struct lttcomm_lttng_msg llm
;
631 assert(reply
->buffer
.size
== 0);
632 assert(reply
->_fds
.size
== 0);
634 ret
= connect_sessiond();
636 ret
= -LTTNG_ERR_NO_SESSIOND
;
639 sessiond_socket
= ret
;
643 /* Send command to session daemon */
644 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
645 message
->buffer
.size
);
647 ret
= -LTTNG_ERR_FATAL
;
651 if (lttng_payload_view_get_fd_count(message
) > 0) {
652 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
,
653 (const int *) message
->_fds
.buffer
.data
,
654 lttng_dynamic_array_get_count(&message
->_fds
));
657 /* Get header from data transmission */
658 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
660 /* Ret value is a valid lttng error code. */
664 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
666 /* Check error code if OK */
667 if (llm
.ret_code
!= LTTNG_OK
) {
672 if (llm
.cmd_header_size
> 0) {
673 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
679 /* Get command header from data transmission */
680 if (llm
.data_size
> 0) {
681 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
687 if (llm
.fd_count
> 0) {
688 ret
= lttng_dynamic_array_set_count(&reply
->_fds
, llm
.fd_count
);
690 ret
= -LTTNG_ERR_NOMEM
;
694 ret
= lttcomm_recv_fds_unix_sock(sessiond_socket
,
695 (int *) reply
->_fds
.buffer
.data
, llm
.fd_count
);
696 if (ret
> 0 && ret
!= llm
.fd_count
* sizeof(int)) {
697 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
699 } else if (ret
<= 0) {
700 ret
= -LTTNG_ERR_FATAL
;
705 /* Don't return the llm header to the caller. */
706 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
707 reply
->buffer
.size
- sizeof(llm
));
708 ret
= lttng_dynamic_buffer_set_size(
709 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
711 /* Can't happen as size is reduced. */
715 ret
= reply
->buffer
.size
;
718 disconnect_sessiond();
723 * Create lttng handle and return pointer.
725 * The returned pointer will be NULL in case of malloc() error.
727 struct lttng_handle
*lttng_create_handle(const char *session_name
,
728 struct lttng_domain
*domain
)
730 struct lttng_handle
*handle
= NULL
;
732 handle
= zmalloc(sizeof(struct lttng_handle
));
733 if (handle
== NULL
) {
734 PERROR("malloc handle");
738 /* Copy session name */
739 lttng_ctl_copy_string(handle
->session_name
, session_name
,
740 sizeof(handle
->session_name
));
742 /* Copy lttng domain or leave initialized to 0. */
744 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
752 * Destroy handle by free(3) the pointer.
754 void lttng_destroy_handle(struct lttng_handle
*handle
)
760 * Register an outside consumer.
762 * Returns size of returned session payload data or a negative error code.
764 int lttng_register_consumer(struct lttng_handle
*handle
,
765 const char *socket_path
)
767 struct lttcomm_session_msg lsm
;
769 if (handle
== NULL
|| socket_path
== NULL
) {
770 return -LTTNG_ERR_INVALID
;
773 memset(&lsm
, 0, sizeof(lsm
));
774 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
775 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
776 sizeof(lsm
.session
.name
));
777 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
779 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
780 sizeof(lsm
.u
.reg
.path
));
782 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
786 * Start tracing for all traces of the session.
788 * Returns size of returned session payload data or a negative error code.
790 int lttng_start_tracing(const char *session_name
)
792 struct lttcomm_session_msg lsm
;
794 if (session_name
== NULL
) {
795 return -LTTNG_ERR_INVALID
;
798 memset(&lsm
, 0, sizeof(lsm
));
799 lsm
.cmd_type
= LTTNG_START_TRACE
;
801 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
802 sizeof(lsm
.session
.name
));
804 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
808 * Stop tracing for all traces of the session.
810 static int _lttng_stop_tracing(const char *session_name
, int wait
)
813 struct lttcomm_session_msg lsm
;
815 if (session_name
== NULL
) {
816 return -LTTNG_ERR_INVALID
;
819 memset(&lsm
, 0, sizeof(lsm
));
820 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
822 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
823 sizeof(lsm
.session
.name
));
825 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
826 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
834 /* Check for data availability */
836 data_ret
= lttng_data_pending(session_name
);
838 /* Return the data available call error. */
844 * Data sleep time before retrying (in usec). Don't sleep if the
845 * call returned value indicates availability.
848 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
850 } while (data_ret
!= 0);
858 * Stop tracing and wait for data availability.
860 int lttng_stop_tracing(const char *session_name
)
862 return _lttng_stop_tracing(session_name
, 1);
866 * Stop tracing but _don't_ wait for data availability.
868 int lttng_stop_tracing_no_wait(const char *session_name
)
870 return _lttng_stop_tracing(session_name
, 0);
874 * Add context to a channel.
876 * If the given channel is NULL, add the contexts to all channels.
877 * The event_name param is ignored.
879 * Returns the size of the returned payload data or a negative error code.
881 int lttng_add_context(struct lttng_handle
*handle
,
882 struct lttng_event_context
*ctx
, const char *event_name
,
883 const char *channel_name
)
888 struct lttcomm_session_msg lsm
;
890 /* Safety check. Both are mandatory. */
891 if (handle
== NULL
|| ctx
== NULL
) {
892 ret
= -LTTNG_ERR_INVALID
;
896 memset(&lsm
, 0, sizeof(lsm
));
897 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
899 /* If no channel name, send empty string. */
900 if (channel_name
== NULL
) {
901 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
902 sizeof(lsm
.u
.context
.channel_name
));
904 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
905 sizeof(lsm
.u
.context
.channel_name
));
908 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
909 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
910 sizeof(lsm
.session
.name
));
912 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
913 size_t provider_len
, ctx_len
;
914 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
915 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
917 if (!provider_name
|| !ctx_name
) {
918 ret
= -LTTNG_ERR_INVALID
;
922 provider_len
= strlen(provider_name
);
923 if (provider_len
== 0) {
924 ret
= -LTTNG_ERR_INVALID
;
927 lsm
.u
.context
.provider_name_len
= provider_len
;
929 ctx_len
= strlen(ctx_name
);
931 ret
= -LTTNG_ERR_INVALID
;
934 lsm
.u
.context
.context_name_len
= ctx_len
;
936 len
= provider_len
+ ctx_len
;
939 ret
= -LTTNG_ERR_NOMEM
;
943 memcpy(buf
, provider_name
, provider_len
);
944 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
946 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
948 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
950 * Don't leak application addresses to the sessiond.
951 * This is only necessary when ctx is for an app ctx otherwise
952 * the values inside the union (type & config) are overwritten.
954 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
955 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
958 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
965 * Enable event(s) for a channel.
967 * If no event name is specified, all events are enabled.
968 * If no channel name is specified, the default 'channel0' is used.
970 * Returns size of returned session payload data or a negative error code.
972 int lttng_enable_event(struct lttng_handle
*handle
,
973 struct lttng_event
*ev
, const char *channel_name
)
975 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
980 * Create or enable an event with a filter expression.
982 * Return negative error value on error.
983 * Return size of returned session payload data if OK.
985 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
986 struct lttng_event
*event
, const char *channel_name
,
987 const char *filter_expression
)
989 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
990 filter_expression
, 0, NULL
);
994 * Depending on the event, return a newly allocated agent filter expression or
995 * NULL if not applicable.
997 * An event with NO loglevel and the name is * will return NULL.
999 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1002 char *agent_filter
= NULL
;
1006 /* Don't add filter for the '*' event. */
1007 if (strcmp(ev
->name
, "*") != 0) {
1009 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1012 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1020 /* Add loglevel filtering if any for the JUL domain. */
1021 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1024 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1030 if (filter
|| agent_filter
) {
1033 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1034 agent_filter
? agent_filter
: filter
, op
,
1039 agent_filter
= new_filter
;
1041 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1050 return agent_filter
;
1057 * Generate the filter bytecode from a given filter expression string. Put the
1058 * newly allocated parser context in ctxp and populate the lsm object with the
1061 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
1063 static int generate_filter(char *filter_expression
,
1064 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
1067 struct filter_parser_ctx
*ctx
= NULL
;
1070 assert(filter_expression
);
1075 * Casting const to non-const, as the underlying function will use it in
1078 fmem
= lttng_fmemopen((void *) filter_expression
,
1079 strlen(filter_expression
), "r");
1081 fprintf(stderr
, "Error opening memory as stream\n");
1082 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1085 ctx
= filter_parser_ctx_alloc(fmem
);
1087 fprintf(stderr
, "Error allocating parser\n");
1088 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1089 goto filter_alloc_error
;
1091 ret
= filter_parser_ctx_append_ast(ctx
);
1093 fprintf(stderr
, "Parse error\n");
1094 ret
= -LTTNG_ERR_FILTER_INVAL
;
1098 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
1101 fprintf(stderr
, "XML print error\n");
1102 ret
= -LTTNG_ERR_FILTER_INVAL
;
1107 dbg_printf("Generating IR... ");
1109 ret
= filter_visitor_ir_generate(ctx
);
1111 fprintf(stderr
, "Generate IR error\n");
1112 ret
= -LTTNG_ERR_FILTER_INVAL
;
1115 dbg_printf("done\n");
1117 dbg_printf("Validating IR... ");
1119 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1121 ret
= -LTTNG_ERR_FILTER_INVAL
;
1125 /* Normalize globbing patterns in the expression. */
1126 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1128 ret
= -LTTNG_ERR_FILTER_INVAL
;
1132 /* Validate strings used as literals in the expression. */
1133 ret
= filter_visitor_ir_validate_string(ctx
);
1135 ret
= -LTTNG_ERR_FILTER_INVAL
;
1139 /* Validate globbing patterns in the expression. */
1140 ret
= filter_visitor_ir_validate_globbing(ctx
);
1142 ret
= -LTTNG_ERR_FILTER_INVAL
;
1146 dbg_printf("done\n");
1148 dbg_printf("Generating bytecode... ");
1150 ret
= filter_visitor_bytecode_generate(ctx
);
1152 fprintf(stderr
, "Generate bytecode error\n");
1153 ret
= -LTTNG_ERR_FILTER_INVAL
;
1156 dbg_printf("done\n");
1157 dbg_printf("Size of bytecode generated: %u bytes.\n",
1158 bytecode_get_len(&ctx
->bytecode
->b
));
1160 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1161 + bytecode_get_len(&ctx
->bytecode
->b
);
1162 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1164 /* No need to keep the memory stream. */
1165 if (fclose(fmem
) != 0) {
1173 filter_ir_free(ctx
);
1174 filter_parser_ctx_free(ctx
);
1176 if (fclose(fmem
) != 0) {
1184 * Enable event(s) for a channel, possibly with exclusions and a filter.
1185 * If no event name is specified, all events are enabled.
1186 * If no channel name is specified, the default name is used.
1187 * If filter expression is not NULL, the filter is set for the event.
1188 * If exclusion count is not zero, the exclusions are set for the event.
1189 * Returns size of returned session payload data or a negative error code.
1191 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1192 struct lttng_event
*ev
, const char *channel_name
,
1193 const char *original_filter_expression
,
1194 int exclusion_count
, char **exclusion_list
)
1196 struct lttcomm_session_msg lsm
;
1197 struct lttng_payload payload
;
1199 unsigned int free_filter_expression
= 0;
1200 struct filter_parser_ctx
*ctx
= NULL
;
1203 * We have either a filter or some exclusions, so we need to set up
1204 * a variable-length payload from where to send the data.
1206 lttng_payload_init(&payload
);
1209 * Cast as non-const since we may replace the filter expression
1210 * by a dynamically allocated string. Otherwise, the original
1211 * string is not modified.
1213 char *filter_expression
= (char *) original_filter_expression
;
1215 if (handle
== NULL
|| ev
== NULL
) {
1216 ret
= -LTTNG_ERR_INVALID
;
1221 * Empty filter string will always be rejected by the parser
1222 * anyway, so treat this corner-case early to eliminate
1223 * lttng_fmemopen error for 0-byte allocation.
1225 if (filter_expression
&& filter_expression
[0] == '\0') {
1226 ret
= -LTTNG_ERR_INVALID
;
1230 memset(&lsm
, 0, sizeof(lsm
));
1232 /* If no channel name, send empty string. */
1233 if (channel_name
== NULL
) {
1234 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1235 sizeof(lsm
.u
.enable
.channel_name
));
1237 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1238 sizeof(lsm
.u
.enable
.channel_name
));
1241 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1242 if (ev
->name
[0] == '\0') {
1243 /* Enable all events */
1244 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1247 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1248 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1250 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1251 sizeof(lsm
.session
.name
));
1252 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1253 lsm
.u
.enable
.bytecode_len
= 0;
1255 /* Parse filter expression. */
1256 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1257 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1258 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1259 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1260 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1261 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1264 /* Setup JUL filter if needed. */
1265 agent_filter
= set_agent_filter(filter_expression
, ev
);
1266 if (!agent_filter
) {
1267 if (!filter_expression
) {
1269 * No JUL and no filter, just skip
1276 * With an agent filter, the original filter has
1277 * been added to it thus replace the filter
1280 filter_expression
= agent_filter
;
1281 free_filter_expression
= 1;
1285 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1291 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1292 lsm
.u
.enable
.bytecode_len
+
1293 lsm
.u
.enable
.expression_len
+
1294 LTTNG_SYMBOL_NAME_LEN
*
1297 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1301 /* Put exclusion names first in the data. */
1302 for (i
= 0; i
< exclusion_count
; i
++) {
1303 size_t exclusion_len
;
1305 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1306 LTTNG_SYMBOL_NAME_LEN
);
1307 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1308 /* Exclusion is not NULL-terminated. */
1309 ret
= -LTTNG_ERR_INVALID
;
1313 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1314 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1320 /* Add filter expression next. */
1321 if (filter_expression
) {
1322 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1323 filter_expression
, lsm
.u
.enable
.expression_len
);
1328 /* Add filter bytecode next. */
1329 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1330 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1331 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1336 if (ev
->extended
.ptr
) {
1337 struct lttng_event_extended
*ev_ext
=
1338 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1340 if (ev_ext
->probe_location
) {
1342 * lttng_userspace_probe_location_serialize returns the
1343 * number of bytes that was appended to the buffer.
1345 ret
= lttng_userspace_probe_location_serialize(
1346 ev_ext
->probe_location
, &payload
);
1352 * Set the size of the userspace probe location element
1353 * of the buffer so that the receiving side knows where
1356 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1361 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1363 int fd_count
= lttng_payload_view_get_fd_count(&view
);
1370 assert(fd_count
== 0 || fd_count
== 1);
1371 if (fd_count
== 1) {
1372 ret
= lttng_payload_view_pop_fd(&view
);
1380 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1381 fd_count
? &fd_to_send
: NULL
, fd_count
,
1382 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1383 view
.buffer
.size
, NULL
, NULL
, 0);
1387 if (filter_expression
&& ctx
) {
1388 filter_bytecode_free(ctx
);
1389 filter_ir_free(ctx
);
1390 filter_parser_ctx_free(ctx
);
1393 if (free_filter_expression
) {
1395 * The filter expression has been replaced and must be freed as
1396 * it is not the original filter expression received as a
1399 free(filter_expression
);
1403 * Return directly to the caller and don't ask the sessiond since
1404 * something went wrong in the parsing of data above.
1406 lttng_payload_reset(&payload
);
1410 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1414 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1415 struct lttng_event
*ev
, const char *channel_name
,
1416 const char *original_filter_expression
)
1418 struct lttcomm_session_msg lsm
;
1421 unsigned int free_filter_expression
= 0;
1422 struct filter_parser_ctx
*ctx
= NULL
;
1424 * Cast as non-const since we may replace the filter expression
1425 * by a dynamically allocated string. Otherwise, the original
1426 * string is not modified.
1428 char *filter_expression
= (char *) original_filter_expression
;
1430 if (handle
== NULL
|| ev
== NULL
) {
1431 ret
= -LTTNG_ERR_INVALID
;
1436 * Empty filter string will always be rejected by the parser
1437 * anyway, so treat this corner-case early to eliminate
1438 * lttng_fmemopen error for 0-byte allocation.
1440 if (filter_expression
&& filter_expression
[0] == '\0') {
1441 ret
= -LTTNG_ERR_INVALID
;
1445 memset(&lsm
, 0, sizeof(lsm
));
1447 /* If no channel name, send empty string. */
1448 if (channel_name
== NULL
) {
1449 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1450 sizeof(lsm
.u
.disable
.channel_name
));
1452 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1453 sizeof(lsm
.u
.disable
.channel_name
));
1456 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1458 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1459 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1461 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1462 sizeof(lsm
.session
.name
));
1463 lsm
.u
.disable
.bytecode_len
= 0;
1466 * For the JUL domain, a filter is enforced except for the
1467 * disable all event. This is done to avoid having the event in
1468 * all sessions thus filtering by logger name.
1470 if (filter_expression
== NULL
&&
1471 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1472 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1473 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1478 * We have a filter, so we need to set up a variable-length
1479 * memory block from where to send the data.
1482 /* Parse filter expression */
1483 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1484 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1485 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1486 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1487 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1488 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1491 /* Setup JUL filter if needed. */
1492 agent_filter
= set_agent_filter(filter_expression
, ev
);
1493 if (!agent_filter
) {
1494 if (!filter_expression
) {
1496 * No JUL and no filter, just skip
1503 * With a JUL filter, the original filter has
1504 * been added to it thus replace the filter
1507 filter_expression
= agent_filter
;
1508 free_filter_expression
= 1;
1512 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1518 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1519 + lsm
.u
.disable
.expression_len
);
1521 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1525 /* Add filter expression. */
1526 if (lsm
.u
.disable
.expression_len
!= 0) {
1529 lsm
.u
.disable
.expression_len
);
1531 /* Add filter bytecode next. */
1532 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1534 + lsm
.u
.disable
.expression_len
,
1536 lsm
.u
.disable
.bytecode_len
);
1539 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1540 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1544 if (filter_expression
&& ctx
) {
1545 filter_bytecode_free(ctx
);
1546 filter_ir_free(ctx
);
1547 filter_parser_ctx_free(ctx
);
1550 if (free_filter_expression
) {
1552 * The filter expression has been replaced and must be freed as
1553 * it is not the original filter expression received as a
1556 free(filter_expression
);
1560 * Return directly to the caller and don't ask the sessiond since
1561 * something went wrong in the parsing of data above.
1566 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1571 * Disable event(s) of a channel and domain.
1572 * If no event name is specified, all events are disabled.
1573 * If no channel name is specified, the default 'channel0' is used.
1574 * Returns size of returned session payload data or a negative error code.
1576 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1577 const char *channel_name
)
1579 struct lttng_event ev
;
1581 memset(&ev
, 0, sizeof(ev
));
1583 ev
.type
= LTTNG_EVENT_ALL
;
1584 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1585 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1588 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1590 struct lttng_channel
*channel
= NULL
;
1591 struct lttng_channel_extended
*extended
= NULL
;
1597 /* Validate domain. */
1598 switch (domain
->type
) {
1599 case LTTNG_DOMAIN_UST
:
1600 switch (domain
->buf_type
) {
1601 case LTTNG_BUFFER_PER_UID
:
1602 case LTTNG_BUFFER_PER_PID
:
1608 case LTTNG_DOMAIN_KERNEL
:
1609 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1617 channel
= zmalloc(sizeof(*channel
));
1622 extended
= zmalloc(sizeof(*extended
));
1627 channel
->attr
.extended
.ptr
= extended
;
1629 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1637 void lttng_channel_destroy(struct lttng_channel
*channel
)
1643 if (channel
->attr
.extended
.ptr
) {
1644 free(channel
->attr
.extended
.ptr
);
1650 * Enable channel per domain
1651 * Returns size of returned session payload data or a negative error code.
1653 int lttng_enable_channel(struct lttng_handle
*handle
,
1654 struct lttng_channel
*in_chan
)
1656 struct lttcomm_session_msg lsm
;
1657 size_t total_buffer_size_needed_per_cpu
= 0;
1659 /* NULL arguments are forbidden. No default values. */
1660 if (handle
== NULL
|| in_chan
== NULL
) {
1661 return -LTTNG_ERR_INVALID
;
1664 memset(&lsm
, 0, sizeof(lsm
));
1665 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1666 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1668 if (!in_chan
->attr
.extended
.ptr
) {
1669 struct lttng_channel
*channel
;
1670 struct lttng_channel_extended
*extended
;
1672 channel
= lttng_channel_create(&handle
->domain
);
1674 return -LTTNG_ERR_NOMEM
;
1678 * Create a new channel in order to use default extended
1681 extended
= (struct lttng_channel_extended
*)
1682 channel
->attr
.extended
.ptr
;
1683 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1684 lttng_channel_destroy(channel
);
1686 struct lttng_channel_extended
*extended
;
1688 extended
= (struct lttng_channel_extended
*)
1689 in_chan
->attr
.extended
.ptr
;
1690 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1694 * Verify that the amount of memory required to create the requested
1695 * buffer is available on the system at the moment.
1697 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1698 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1699 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1700 return -LTTNG_ERR_NOMEM
;
1703 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1704 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1706 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1707 sizeof(lsm
.session
.name
));
1709 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1713 * All tracing will be stopped for registered events of the channel.
1714 * Returns size of returned session payload data or a negative error code.
1716 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1718 struct lttcomm_session_msg lsm
;
1720 /* Safety check. Both are mandatory. */
1721 if (handle
== NULL
|| name
== NULL
) {
1722 return -LTTNG_ERR_INVALID
;
1725 memset(&lsm
, 0, sizeof(lsm
));
1727 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1729 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1730 sizeof(lsm
.u
.disable
.channel_name
));
1732 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1734 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1735 sizeof(lsm
.session
.name
));
1737 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1741 * Lists all available tracepoints of domain.
1742 * Sets the contents of the events array.
1743 * Returns the number of lttng_event entries in events;
1744 * on error, returns a negative value.
1746 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1747 struct lttng_event
**events
)
1750 struct lttcomm_session_msg lsm
;
1752 if (handle
== NULL
) {
1753 return -LTTNG_ERR_INVALID
;
1756 memset(&lsm
, 0, sizeof(lsm
));
1757 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1758 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1760 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1765 return ret
/ sizeof(struct lttng_event
);
1769 * Lists all available tracepoint fields of domain.
1770 * Sets the contents of the event field array.
1771 * Returns the number of lttng_event_field entries in events;
1772 * on error, returns a negative value.
1774 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1775 struct lttng_event_field
**fields
)
1778 struct lttcomm_session_msg lsm
;
1780 if (handle
== NULL
) {
1781 return -LTTNG_ERR_INVALID
;
1784 memset(&lsm
, 0, sizeof(lsm
));
1785 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1786 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1788 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1793 return ret
/ sizeof(struct lttng_event_field
);
1797 * Lists all available kernel system calls. Allocates and sets the contents of
1800 * Returns the number of lttng_event entries in events; on error, returns a
1803 int lttng_list_syscalls(struct lttng_event
**events
)
1806 struct lttcomm_session_msg lsm
;
1809 return -LTTNG_ERR_INVALID
;
1812 memset(&lsm
, 0, sizeof(lsm
));
1813 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1814 /* Force kernel domain for system calls. */
1815 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1817 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1822 return ret
/ sizeof(struct lttng_event
);
1826 * Returns a human readable string describing
1827 * the error code (a negative value).
1829 const char *lttng_strerror(int code
)
1831 return error_get_str(code
);
1834 enum lttng_error_code
lttng_create_session_ext(
1835 struct lttng_session_descriptor
*session_descriptor
)
1837 enum lttng_error_code ret_code
;
1838 struct lttcomm_session_msg lsm
= {
1839 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1842 struct lttng_buffer_view reply_view
;
1844 bool sessiond_must_generate_ouput
;
1845 struct lttng_dynamic_buffer payload
;
1847 size_t descriptor_size
;
1848 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1850 lttng_dynamic_buffer_init(&payload
);
1851 if (!session_descriptor
) {
1852 ret_code
= LTTNG_ERR_INVALID
;
1856 sessiond_must_generate_ouput
=
1857 !lttng_session_descriptor_is_output_destination_initialized(
1858 session_descriptor
);
1859 if (sessiond_must_generate_ouput
) {
1860 const char *home_dir
= utils_get_home_dir();
1861 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1863 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1864 ret_code
= LTTNG_ERR_FATAL
;
1868 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1869 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1872 ret_code
= LTTNG_ERR_NOMEM
;
1877 descriptor_size
= payload
.size
;
1878 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1881 ret_code
= LTTNG_ERR_INVALID
;
1884 descriptor_size
= payload
.size
- descriptor_size
;
1885 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1887 /* Command returns a session descriptor on success. */
1888 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1889 payload
.size
, &reply
);
1890 if (reply_ret
< 0) {
1891 ret_code
= -reply_ret
;
1893 } else if (reply_ret
== 0) {
1894 /* Socket unexpectedly closed by the session daemon. */
1895 ret_code
= LTTNG_ERR_FATAL
;
1899 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1900 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1903 ret_code
= LTTNG_ERR_FATAL
;
1906 ret_code
= LTTNG_OK
;
1907 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1910 lttng_dynamic_buffer_reset(&payload
);
1911 lttng_session_descriptor_destroy(descriptor_reply
);
1916 * Create a new session using name and url for destination.
1918 * Return 0 on success else a negative LTTng error code.
1920 int lttng_create_session(const char *name
, const char *url
)
1924 struct lttng_uri
*uris
= NULL
;
1925 struct lttng_session_descriptor
*descriptor
= NULL
;
1926 enum lttng_error_code ret_code
;
1929 ret
= -LTTNG_ERR_INVALID
;
1933 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1935 ret
= -LTTNG_ERR_INVALID
;
1940 descriptor
= lttng_session_descriptor_create(name
);
1943 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1944 ret
= -LTTNG_ERR_INVALID
;
1947 descriptor
= lttng_session_descriptor_local_create(name
,
1951 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1955 ret
= -LTTNG_ERR_INVALID
;
1959 ret
= -LTTNG_ERR_INVALID
;
1962 ret_code
= lttng_create_session_ext(descriptor
);
1963 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1965 lttng_session_descriptor_destroy(descriptor
);
1971 * Create a session exclusively used for snapshot.
1973 * Return 0 on success else a negative LTTng error code.
1975 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1978 enum lttng_error_code ret_code
;
1980 struct lttng_uri
*uris
= NULL
;
1981 struct lttng_session_descriptor
*descriptor
= NULL
;
1984 ret
= -LTTNG_ERR_INVALID
;
1988 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1990 ret
= -LTTNG_ERR_INVALID
;
1994 * If the user does not specify a custom subdir, use the session name.
1996 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1997 strlen(uris
[0].subdir
) == 0) {
1998 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2001 PERROR("Failed to set session name as network destination sub-directory");
2002 ret
= -LTTNG_ERR_FATAL
;
2004 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2005 /* Truncated output. */
2006 ret
= -LTTNG_ERR_INVALID
;
2013 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2016 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2017 ret
= -LTTNG_ERR_INVALID
;
2020 descriptor
= lttng_session_descriptor_snapshot_local_create(
2025 descriptor
= lttng_session_descriptor_snapshot_network_create(
2031 ret
= -LTTNG_ERR_INVALID
;
2035 ret
= -LTTNG_ERR_INVALID
;
2038 ret_code
= lttng_create_session_ext(descriptor
);
2039 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2041 lttng_session_descriptor_destroy(descriptor
);
2047 * Create a session exclusively used for live.
2049 * Return 0 on success else a negative LTTng error code.
2051 int lttng_create_session_live(const char *name
, const char *url
,
2052 unsigned int timer_interval
)
2055 enum lttng_error_code ret_code
;
2056 struct lttng_session_descriptor
*descriptor
= NULL
;
2059 ret
= -LTTNG_ERR_INVALID
;
2064 descriptor
= lttng_session_descriptor_live_network_create(
2065 name
, url
, NULL
, timer_interval
);
2067 descriptor
= lttng_session_descriptor_live_create(
2068 name
, timer_interval
);
2071 ret
= -LTTNG_ERR_INVALID
;
2074 ret_code
= lttng_create_session_ext(descriptor
);
2075 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2077 lttng_session_descriptor_destroy(descriptor
);
2082 * Stop the session and wait for the data before destroying it
2084 * Return 0 on success else a negative LTTng error code.
2086 int lttng_destroy_session(const char *session_name
)
2089 enum lttng_error_code ret_code
;
2090 enum lttng_destruction_handle_status status
;
2091 struct lttng_destruction_handle
*handle
= NULL
;
2094 * Stop the tracing and wait for the data to be
2097 ret
= _lttng_stop_tracing(session_name
, 1);
2098 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2102 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2103 if (ret_code
!= LTTNG_OK
) {
2104 ret
= (int) -ret_code
;
2109 /* Block until the completion of the destruction of the session. */
2110 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2111 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2112 ret
= -LTTNG_ERR_UNK
;
2116 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2117 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2118 ret
= -LTTNG_ERR_UNK
;
2121 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2123 lttng_destruction_handle_destroy(handle
);
2128 * Destroy the session without waiting for the data.
2130 int lttng_destroy_session_no_wait(const char *session_name
)
2132 enum lttng_error_code ret_code
;
2134 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2135 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2139 * Ask the session daemon for all available sessions.
2140 * Sets the contents of the sessions array.
2141 * Returns the number of lttng_session entries in sessions;
2142 * on error, returns a negative value.
2144 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2147 struct lttcomm_session_msg lsm
;
2148 const size_t session_size
= sizeof(struct lttng_session
) +
2149 sizeof(struct lttng_session_extended
);
2150 size_t session_count
, i
;
2151 struct lttng_session_extended
*sessions_extended_begin
;
2152 struct lttng_session
*sessions
= NULL
;
2154 memset(&lsm
, 0, sizeof(lsm
));
2155 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2156 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2161 ret
= -LTTNG_ERR_FATAL
;
2165 if (ret
% session_size
) {
2166 ret
= -LTTNG_ERR_UNK
;
2168 *out_sessions
= NULL
;
2171 session_count
= (size_t) ret
/ session_size
;
2172 sessions_extended_begin
= (struct lttng_session_extended
*)
2173 (&sessions
[session_count
]);
2175 /* Set extended session info pointers. */
2176 for (i
= 0; i
< session_count
; i
++) {
2177 struct lttng_session
*session
= &sessions
[i
];
2178 struct lttng_session_extended
*extended
=
2179 &(sessions_extended_begin
[i
]);
2181 session
->extended
.ptr
= extended
;
2184 ret
= (int) session_count
;
2185 *out_sessions
= sessions
;
2190 enum lttng_error_code
lttng_session_get_creation_time(
2191 const struct lttng_session
*session
, uint64_t *creation_time
)
2193 enum lttng_error_code ret
= LTTNG_OK
;
2194 struct lttng_session_extended
*extended
;
2196 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2197 ret
= LTTNG_ERR_INVALID
;
2201 extended
= session
->extended
.ptr
;
2202 if (!extended
->creation_time
.is_set
) {
2203 /* Not created on the session daemon yet. */
2204 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2207 *creation_time
= extended
->creation_time
.value
;
2212 int lttng_set_session_shm_path(const char *session_name
,
2213 const char *shm_path
)
2215 struct lttcomm_session_msg lsm
;
2217 if (session_name
== NULL
) {
2218 return -LTTNG_ERR_INVALID
;
2221 memset(&lsm
, 0, sizeof(lsm
));
2222 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2224 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2225 sizeof(lsm
.session
.name
));
2226 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2227 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2229 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2233 * Ask the session daemon for all available domains of a session.
2234 * Sets the contents of the domains array.
2235 * Returns the number of lttng_domain entries in domains;
2236 * on error, returns a negative value.
2238 int lttng_list_domains(const char *session_name
,
2239 struct lttng_domain
**domains
)
2242 struct lttcomm_session_msg lsm
;
2244 if (session_name
== NULL
) {
2245 return -LTTNG_ERR_INVALID
;
2248 memset(&lsm
, 0, sizeof(lsm
));
2249 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2251 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2252 sizeof(lsm
.session
.name
));
2254 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2259 return ret
/ sizeof(struct lttng_domain
);
2263 * Ask the session daemon for all available channels of a session.
2264 * Sets the contents of the channels array.
2265 * Returns the number of lttng_channel entries in channels;
2266 * on error, returns a negative value.
2268 int lttng_list_channels(struct lttng_handle
*handle
,
2269 struct lttng_channel
**channels
)
2272 size_t channel_count
, i
;
2273 const size_t channel_size
= sizeof(struct lttng_channel
) +
2274 sizeof(struct lttng_channel_extended
);
2275 struct lttcomm_session_msg lsm
;
2278 if (handle
== NULL
) {
2279 ret
= -LTTNG_ERR_INVALID
;
2283 memset(&lsm
, 0, sizeof(lsm
));
2284 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2285 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2286 sizeof(lsm
.session
.name
));
2288 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2290 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2295 if (ret
% channel_size
) {
2296 ret
= -LTTNG_ERR_UNK
;
2301 channel_count
= (size_t) ret
/ channel_size
;
2303 /* Set extended info pointers */
2304 extended_at
= ((void *) *channels
) +
2305 channel_count
* sizeof(struct lttng_channel
);
2306 for (i
= 0; i
< channel_count
; i
++) {
2307 struct lttng_channel
*chan
= &(*channels
)[i
];
2309 chan
->attr
.extended
.ptr
= extended_at
;
2310 extended_at
+= sizeof(struct lttng_channel_extended
);
2313 ret
= (int) channel_count
;
2319 * Ask the session daemon for all available events of a session channel.
2320 * Sets the contents of the events array.
2321 * Returns the number of lttng_event entries in events;
2322 * on error, returns a negative value.
2324 int lttng_list_events(struct lttng_handle
*handle
,
2325 const char *channel_name
, struct lttng_event
**events
)
2328 struct lttcomm_session_msg lsm
= {};
2329 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2330 uint32_t nb_events
, i
;
2331 const void *comm_ext_at
;
2332 struct lttng_dynamic_buffer listing
;
2334 struct lttng_payload payload
;
2335 struct lttng_payload payload_copy
;
2336 struct lttng_payload_view lsm_view
=
2337 lttng_payload_view_init_from_buffer(
2338 (const char *) &lsm
, 0, sizeof(lsm
));
2339 struct lttng_buffer_view cmd_header_view
;
2340 struct lttng_buffer_view cmd_payload_view
;
2341 struct lttng_buffer_view flat_events_view
;
2342 struct lttng_buffer_view ext_view
;
2344 /* Safety check. An handle and channel name are mandatory */
2345 if (handle
== NULL
|| channel_name
== NULL
) {
2346 ret
= -LTTNG_ERR_INVALID
;
2350 lttng_payload_init(&payload
);
2351 lttng_payload_init(&payload_copy
);
2353 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2354 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2355 sizeof(lsm
.session
.name
));
2356 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2357 sizeof(lsm
.u
.list
.channel_name
));
2358 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2360 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2366 * A copy of the payload is performed since it will be
2367 * consumed twice. Consuming the same payload twice is invalid since
2368 * it will cause any received file descriptor to become "shared"
2369 * between different instances of the resulting objects.
2371 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2373 ret
= -LTTNG_ERR_NOMEM
;
2377 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2378 &payload
.buffer
, 0, sizeof(*cmd_header
));
2379 if (!cmd_header_view
.data
) {
2380 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2384 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2386 /* Set number of events and free command header */
2387 nb_events
= cmd_header
->nb_events
;
2388 if (nb_events
> INT_MAX
) {
2389 ret
= -LTTNG_ERR_OVERFLOW
;
2393 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2394 &payload
.buffer
, sizeof(*cmd_header
), -1);
2397 * The buffer that is returned must contain a "flat" version of
2398 * the events that are returned. In other words, all pointers
2399 * within an lttng_event must point to a location within the returned
2400 * buffer so that the user may free everything by simply calling free()
2401 * on the returned buffer. This is needed in order to maintain API
2404 * A first pass is performed to compute the size of the buffer that
2405 * must be allocated. A second pass is then performed to setup
2406 * the returned events so that their members always point within the
2409 * The layout of the returned buffer is as follows:
2410 * - struct lttng_event[nb_events],
2411 * - nb_events times the following:
2412 * - struct lttng_event_extended,
2413 * - flattened version of userspace_probe_location
2414 * - filter_expression
2416 * - padding to align to 64-bits
2418 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2419 nb_events
* sizeof(struct lttng_event
), -1);
2420 comm_ext_at
= ext_view
.data
;
2421 storage_req
= nb_events
* sizeof(struct lttng_event
);
2423 struct lttng_payload_view payload_view
=
2424 lttng_payload_view_from_payload(&payload
, 0, -1);
2426 for (i
= 0; i
< nb_events
; i
++) {
2427 const struct lttcomm_event_extended_header
*ext_comm
=
2428 (struct lttcomm_event_extended_header
*)
2430 int probe_storage_req
= 0;
2432 comm_ext_at
+= sizeof(*ext_comm
);
2433 comm_ext_at
+= ext_comm
->filter_len
;
2434 comm_ext_at
+= ext_comm
->nb_exclusions
*
2435 LTTNG_SYMBOL_NAME_LEN
;
2437 if (ext_comm
->userspace_probe_location_len
) {
2438 struct lttng_userspace_probe_location
2439 *probe_location
= NULL
;
2440 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2442 (const char *) comm_ext_at
-
2443 payload_view
.buffer
.data
,
2444 ext_comm
->userspace_probe_location_len
);
2447 * Create a temporary userspace probe location
2448 * to determine the size needed by a "flattened"
2449 * version of that same probe location.
2451 ret
= lttng_userspace_probe_location_create_from_payload(
2452 &probe_location_view
,
2455 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2459 ret
= lttng_userspace_probe_location_flatten(
2460 probe_location
, NULL
);
2461 lttng_userspace_probe_location_destroy(
2464 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2468 probe_storage_req
= ret
;
2469 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2472 storage_req
+= sizeof(struct lttng_event_extended
);
2473 storage_req
+= ext_comm
->filter_len
;
2474 storage_req
+= ext_comm
->nb_exclusions
*
2475 LTTNG_SYMBOL_NAME_LEN
;
2476 /* Padding to ensure the flat probe is aligned. */
2477 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2478 storage_req
+= probe_storage_req
;
2482 lttng_dynamic_buffer_init(&listing
);
2484 * We must ensure that "listing" is never resized so as to preserve
2485 * the validity of the flattened objects.
2487 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2489 ret
= -LTTNG_ERR_NOMEM
;
2493 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2494 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2495 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2496 nb_events
* sizeof(struct lttng_event
));
2497 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2499 ret
= -LTTNG_ERR_NOMEM
;
2500 goto free_dynamic_buffer
;
2503 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2504 nb_events
* sizeof(struct lttng_event
), -1);
2505 comm_ext_at
= ext_view
.data
;
2508 struct lttng_payload_view payload_copy_view
=
2509 lttng_payload_view_from_payload(
2510 &payload_copy
, 0, -1);
2512 for (i
= 0; i
< nb_events
; i
++) {
2513 struct lttng_event
*event
= (typeof(event
))(
2515 (sizeof(struct lttng_event
) * i
));
2516 const struct lttcomm_event_extended_header
*ext_comm
=
2517 (typeof(ext_comm
)) comm_ext_at
;
2518 struct lttng_event_extended
*event_extended
=
2519 (typeof(event_extended
))(listing
.data
+
2522 /* Insert struct lttng_event_extended. */
2523 ret
= lttng_dynamic_buffer_set_size(&listing
,
2524 listing
.size
+ sizeof(*event_extended
));
2526 ret
= -LTTNG_ERR_NOMEM
;
2527 goto free_dynamic_buffer
;
2529 event
->extended
.ptr
= event_extended
;
2531 comm_ext_at
+= sizeof(*ext_comm
);
2533 /* Insert filter expression. */
2534 if (ext_comm
->filter_len
) {
2535 event_extended
->filter_expression
=
2536 listing
.data
+ listing
.size
;
2537 ret
= lttng_dynamic_buffer_append(&listing
,
2539 ext_comm
->filter_len
);
2541 ret
= -LTTNG_ERR_NOMEM
;
2542 goto free_dynamic_buffer
;
2544 comm_ext_at
+= ext_comm
->filter_len
;
2547 /* Insert exclusions. */
2548 if (ext_comm
->nb_exclusions
) {
2549 event_extended
->exclusions
.count
=
2550 ext_comm
->nb_exclusions
;
2551 event_extended
->exclusions
.strings
=
2552 listing
.data
+ listing
.size
;
2554 ret
= lttng_dynamic_buffer_append(&listing
,
2556 ext_comm
->nb_exclusions
*
2557 LTTNG_SYMBOL_NAME_LEN
);
2559 ret
= -LTTNG_ERR_NOMEM
;
2560 goto free_dynamic_buffer
;
2562 comm_ext_at
+= ext_comm
->nb_exclusions
*
2563 LTTNG_SYMBOL_NAME_LEN
;
2566 /* Insert padding to align to 64-bits. */
2567 ret
= lttng_dynamic_buffer_set_size(&listing
,
2568 ALIGN_TO(listing
.size
,
2571 ret
= -LTTNG_ERR_NOMEM
;
2572 goto free_dynamic_buffer
;
2575 /* Insert flattened userspace probe location. */
2576 if (ext_comm
->userspace_probe_location_len
) {
2577 struct lttng_userspace_probe_location
2578 *probe_location
= NULL
;
2579 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2581 (const char *) comm_ext_at
-
2582 payload_copy_view
.buffer
.data
,
2583 ext_comm
->userspace_probe_location_len
);
2585 ret
= lttng_userspace_probe_location_create_from_payload(
2586 &probe_location_view
,
2589 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2590 goto free_dynamic_buffer
;
2593 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2596 ret
= lttng_userspace_probe_location_flatten(
2597 probe_location
, &listing
);
2598 lttng_userspace_probe_location_destroy(
2601 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2602 goto free_dynamic_buffer
;
2605 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2610 /* Don't reset listing buffer as we return its content. */
2611 *events
= (struct lttng_event
*) listing
.data
;
2612 lttng_dynamic_buffer_init(&listing
);
2613 ret
= (int) nb_events
;
2614 free_dynamic_buffer
:
2615 lttng_dynamic_buffer_reset(&listing
);
2617 lttng_payload_reset(&payload
);
2618 lttng_payload_reset(&payload_copy
);
2623 * Sets the tracing_group variable with name.
2624 * This function allocates memory pointed to by tracing_group.
2625 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2627 int lttng_set_tracing_group(const char *name
)
2630 return -LTTNG_ERR_INVALID
;
2633 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2634 return -LTTNG_ERR_FATAL
;
2640 int lttng_calibrate(struct lttng_handle
*handle
,
2641 struct lttng_calibrate
*calibrate
)
2644 * This command was removed in LTTng 2.9.
2646 return -LTTNG_ERR_UND
;
2650 * Set default channel attributes.
2651 * If either or both of the arguments are null, attr content is zeroe'd.
2653 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2654 struct lttng_channel_attr
*attr
)
2656 struct lttng_channel_extended
*extended
;
2659 if (attr
== NULL
|| domain
== NULL
) {
2663 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2664 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2666 /* Same for all domains. */
2667 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2668 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2669 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2671 switch (domain
->type
) {
2672 case LTTNG_DOMAIN_KERNEL
:
2673 attr
->switch_timer_interval
=
2674 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2675 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2676 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2677 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2678 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2680 extended
->monitor_timer_interval
=
2681 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2682 extended
->blocking_timeout
=
2683 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2686 case LTTNG_DOMAIN_UST
:
2687 switch (domain
->buf_type
) {
2688 case LTTNG_BUFFER_PER_UID
:
2689 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2690 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2691 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2692 attr
->switch_timer_interval
=
2693 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2694 attr
->read_timer_interval
=
2695 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2697 extended
->monitor_timer_interval
=
2698 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2699 extended
->blocking_timeout
=
2700 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2703 case LTTNG_BUFFER_PER_PID
:
2705 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2706 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2707 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2708 attr
->switch_timer_interval
=
2709 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2710 attr
->read_timer_interval
=
2711 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2713 extended
->monitor_timer_interval
=
2714 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2715 extended
->blocking_timeout
=
2716 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2721 /* Default behavior: leave set to 0. */
2725 attr
->extended
.ptr
= extended
;
2728 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2729 uint64_t *discarded_events
)
2732 struct lttng_channel_extended
*chan_ext
;
2734 if (!channel
|| !discarded_events
) {
2735 ret
= -LTTNG_ERR_INVALID
;
2739 chan_ext
= channel
->attr
.extended
.ptr
;
2742 * This can happen since the lttng_channel structure is
2743 * used for other tasks where this pointer is never set.
2745 *discarded_events
= 0;
2749 *discarded_events
= chan_ext
->discarded_events
;
2754 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2755 uint64_t *lost_packets
)
2758 struct lttng_channel_extended
*chan_ext
;
2760 if (!channel
|| !lost_packets
) {
2761 ret
= -LTTNG_ERR_INVALID
;
2765 chan_ext
= channel
->attr
.extended
.ptr
;
2768 * This can happen since the lttng_channel structure is
2769 * used for other tasks where this pointer is never set.
2775 *lost_packets
= chan_ext
->lost_packets
;
2780 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2781 uint64_t *monitor_timer_interval
)
2785 if (!chan
|| !monitor_timer_interval
) {
2786 ret
= -LTTNG_ERR_INVALID
;
2790 if (!chan
->attr
.extended
.ptr
) {
2791 ret
= -LTTNG_ERR_INVALID
;
2795 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2796 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2801 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2802 uint64_t monitor_timer_interval
)
2806 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2807 ret
= -LTTNG_ERR_INVALID
;
2811 ((struct lttng_channel_extended
*)
2812 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2813 monitor_timer_interval
;
2818 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2819 int64_t *blocking_timeout
)
2823 if (!chan
|| !blocking_timeout
) {
2824 ret
= -LTTNG_ERR_INVALID
;
2828 if (!chan
->attr
.extended
.ptr
) {
2829 ret
= -LTTNG_ERR_INVALID
;
2833 *blocking_timeout
= ((struct lttng_channel_extended
*)
2834 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2839 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2840 int64_t blocking_timeout
)
2843 int64_t msec_timeout
;
2845 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2846 ret
= -LTTNG_ERR_INVALID
;
2850 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2851 ret
= -LTTNG_ERR_INVALID
;
2856 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2857 * us to accept a narrower range of values (msecs expressed as a signed
2860 msec_timeout
= blocking_timeout
/ 1000;
2861 if (msec_timeout
!= (int32_t) msec_timeout
) {
2862 ret
= -LTTNG_ERR_INVALID
;
2866 ((struct lttng_channel_extended
*)
2867 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2874 * Check if session daemon is alive.
2876 * Return 1 if alive or 0 if not.
2877 * On error returns a negative value.
2879 int lttng_session_daemon_alive(void)
2883 ret
= set_session_daemon_path();
2889 if (*sessiond_sock_path
== '\0') {
2891 * No socket path set. Weird error which means the constructor
2897 ret
= try_connect_sessiond(sessiond_sock_path
);
2908 * Set URL for a consumer for a session and domain.
2910 * Return 0 on success, else a negative value.
2912 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2913 const char *control_url
, const char *data_url
)
2917 struct lttcomm_session_msg lsm
;
2918 struct lttng_uri
*uris
= NULL
;
2920 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2921 return -LTTNG_ERR_INVALID
;
2924 memset(&lsm
, 0, sizeof(lsm
));
2926 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2928 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2929 sizeof(lsm
.session
.name
));
2930 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2932 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2934 return -LTTNG_ERR_INVALID
;
2937 lsm
.u
.uri
.size
= size
;
2939 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2940 sizeof(struct lttng_uri
) * size
, NULL
);
2949 int lttng_enable_consumer(struct lttng_handle
*handle
);
2950 int lttng_enable_consumer(struct lttng_handle
*handle
)
2958 int lttng_disable_consumer(struct lttng_handle
*handle
);
2959 int lttng_disable_consumer(struct lttng_handle
*handle
)
2967 int _lttng_create_session_ext(const char *name
, const char *url
,
2968 const char *datetime
);
2969 int _lttng_create_session_ext(const char *name
, const char *url
,
2970 const char *datetime
)
2976 * For a given session name, this call checks if the data is ready to be read
2977 * or is still being extracted by the consumer(s) hence not ready to be used by
2980 int lttng_data_pending(const char *session_name
)
2983 struct lttcomm_session_msg lsm
;
2984 uint8_t *pending
= NULL
;
2986 if (session_name
== NULL
) {
2987 return -LTTNG_ERR_INVALID
;
2990 memset(&lsm
, 0, sizeof(lsm
));
2991 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2993 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2994 sizeof(lsm
.session
.name
));
2996 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2999 } else if (ret
!= 1) {
3000 /* Unexpected payload size */
3001 ret
= -LTTNG_ERR_INVALID
;
3003 } else if (!pending
) {
3004 /* Internal error. */
3005 ret
= -LTTNG_ERR_UNK
;
3009 ret
= (int) *pending
;
3016 * Regenerate the metadata for a session.
3017 * Return 0 on success, a negative error code on error.
3019 int lttng_regenerate_metadata(const char *session_name
)
3022 struct lttcomm_session_msg lsm
;
3024 if (!session_name
) {
3025 ret
= -LTTNG_ERR_INVALID
;
3029 memset(&lsm
, 0, sizeof(lsm
));
3030 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3032 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3033 sizeof(lsm
.session
.name
));
3035 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3046 * Deprecated, replaced by lttng_regenerate_metadata.
3048 int lttng_metadata_regenerate(const char *session_name
)
3050 return lttng_regenerate_metadata(session_name
);
3054 * Regenerate the statedump of a session.
3055 * Return 0 on success, a negative error code on error.
3057 int lttng_regenerate_statedump(const char *session_name
)
3060 struct lttcomm_session_msg lsm
;
3062 if (!session_name
) {
3063 ret
= -LTTNG_ERR_INVALID
;
3067 memset(&lsm
, 0, sizeof(lsm
));
3068 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3070 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3071 sizeof(lsm
.session
.name
));
3073 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3083 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3086 struct lttcomm_session_msg lsm
;
3087 struct lttng_payload payload
;
3089 lttng_payload_init(&payload
);
3091 ret
= -LTTNG_ERR_INVALID
;
3095 if (!lttng_trigger_validate(trigger
)) {
3096 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3100 ret
= lttng_trigger_serialize(trigger
, &payload
);
3102 ret
= -LTTNG_ERR_UNK
;
3106 memset(&lsm
, 0, sizeof(lsm
));
3107 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3108 lsm
.u
.trigger
.length
= (uint32_t) payload
.buffer
.size
;
3109 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3110 &lsm
, payload
.buffer
.data
, payload
.buffer
.size
, NULL
);
3112 lttng_payload_reset(&payload
);
3116 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3119 struct lttcomm_session_msg lsm
;
3120 struct lttng_payload payload
;
3122 lttng_payload_init(&payload
);
3124 ret
= -LTTNG_ERR_INVALID
;
3128 if (!lttng_trigger_validate(trigger
)) {
3129 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3133 ret
= lttng_trigger_serialize(trigger
, &payload
);
3135 ret
= -LTTNG_ERR_UNK
;
3139 memset(&lsm
, 0, sizeof(lsm
));
3140 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3141 lsm
.u
.trigger
.length
= (uint32_t) payload
.buffer
.size
;
3142 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3143 &lsm
, payload
.buffer
.data
, payload
.buffer
.size
, NULL
);
3145 lttng_payload_reset(&payload
);
3152 static void __attribute__((constructor
)) init(void)
3154 /* Set default session group */
3155 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3161 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3163 free(tracing_group
);