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
21 #include <common/common.h>
22 #include <common/compat/errno.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/dynamic-array.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/tracker.h>
31 #include <common/unix.h>
32 #include <common/uri.h>
33 #include <common/utils.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/destruction-handle.h>
36 #include <lttng/endpoint.h>
37 #include <lttng/event-internal.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/lttng.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/trigger/trigger-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/lttng-error.h>
46 #include <common/filter/filter-ast.h>
47 #include <common/filter/filter-parser.h>
48 #include <common/filter/filter-bytecode.h>
49 #include <common/filter/memstream.h>
50 #include "lttng-ctl-helper.h"
52 #define COPY_DOMAIN_PACKED(dst, src) \
54 struct lttng_domain _tmp_domain; \
56 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
= -1;
62 static char sessiond_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
80 * Copy domain to lttcomm_session_msg domain.
82 * If domain is unknown, default domain will be the kernel.
85 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
86 struct lttng_domain
*src
)
90 case LTTNG_DOMAIN_KERNEL
:
91 case LTTNG_DOMAIN_UST
:
92 case LTTNG_DOMAIN_JUL
:
93 case LTTNG_DOMAIN_LOG4J
:
94 case LTTNG_DOMAIN_PYTHON
:
95 memcpy(dst
, src
, sizeof(struct lttng_domain
));
98 memset(dst
, 0, sizeof(struct lttng_domain
));
105 * Send lttcomm_session_msg to the session daemon.
107 * On success, returns the number of bytes sent (>=0)
108 * On error, returns -1
110 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
115 ret
= -LTTNG_ERR_NO_SESSIOND
;
119 DBG("LSM cmd type : %d", lsm
->cmd_type
);
121 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
122 sizeof(struct lttcomm_session_msg
));
124 ret
= -LTTNG_ERR_FATAL
;
132 * Send var len data to the session daemon.
134 * On success, returns the number of bytes sent (>=0)
135 * On error, returns -1
137 static int send_session_varlen(const void *data
, size_t len
)
142 ret
= -LTTNG_ERR_NO_SESSIOND
;
151 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
153 ret
= -LTTNG_ERR_FATAL
;
161 * Send file descriptors to the session daemon.
163 * On success, returns the number of bytes sent (>=0)
164 * On error, returns -1
166 static int send_session_fds(const int *fds
, size_t nb_fd
)
171 ret
= -LTTNG_ERR_NO_SESSIOND
;
175 if (!fds
|| !nb_fd
) {
180 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
182 ret
= -LTTNG_ERR_FATAL
;
190 * Receive data from the sessiond socket.
192 * On success, returns the number of bytes received (>=0)
193 * On error, returns a negative lttng_error_code.
195 static int recv_data_sessiond(void *buf
, size_t len
)
202 ret
= -LTTNG_ERR_NO_SESSIOND
;
206 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
208 ret
= -LTTNG_ERR_FATAL
;
209 } else if (ret
== 0) {
210 ret
= -LTTNG_ERR_NO_SESSIOND
;
218 * Receive a payload from the session daemon by appending to an existing
220 * On success, returns the number of bytes received (>=0)
221 * On error, returns a negative lttng_error_code.
223 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
226 const size_t original_payload_size
= payload
->buffer
.size
;
228 ret
= lttng_dynamic_buffer_set_size(
229 &payload
->buffer
, payload
->buffer
.size
+ len
);
231 ret
= -LTTNG_ERR_NOMEM
;
235 ret
= recv_data_sessiond(
236 payload
->buffer
.data
+ original_payload_size
, len
);
242 * Check if we are in the specified group.
244 * If yes return 1, else return -1.
247 int lttng_check_tracing_group(void)
249 gid_t
*grp_list
, tracing_gid
;
250 int grp_list_size
, grp_id
, i
;
252 const char *grp_name
= tracing_group
;
254 /* Get GID of group 'tracing' */
255 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
256 /* If grp_tracing is NULL, the group does not exist. */
260 /* Get number of supplementary group IDs */
261 grp_list_size
= getgroups(0, NULL
);
262 if (grp_list_size
< 0) {
267 /* Alloc group list of the right size */
268 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
273 grp_id
= getgroups(grp_list_size
, grp_list
);
279 for (i
= 0; i
< grp_list_size
; i
++) {
280 if (grp_list
[i
] == tracing_gid
) {
293 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
297 size_t best_mem_info
;
298 size_t num_bytes_requested_total
;
301 * Get the number of CPU currently online to compute the amount of
302 * memory needed to create a buffer for every CPU.
304 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
309 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
312 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
313 * reliable estimate we can get but it is only exposed by the kernel
314 * since 3.14. (See Linux kernel commit:
315 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
317 ret
= utils_get_memory_available(&best_mem_info
);
323 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
324 * is a sanity check for obvious user error.
326 ret
= utils_get_memory_total(&best_mem_info
);
334 return best_mem_info
>= num_bytes_requested_total
;
338 * Try connect to session daemon with sock_path.
340 * Return 0 on success, else -1
342 static int try_connect_sessiond(const char *sock_path
)
346 /* If socket exist, we check if the daemon listens for connect. */
347 ret
= access(sock_path
, F_OK
);
353 ret
= lttcomm_connect_unix_sock(sock_path
);
359 ret
= lttcomm_close_unix_sock(ret
);
361 PERROR("lttcomm_close_unix_sock");
371 * Set sessiond socket path by putting it in the global sessiond_sock_path
374 * Returns 0 on success, negative value on failure (the sessiond socket path
375 * is somehow too long or ENOMEM).
377 static int set_session_daemon_path(void)
379 int in_tgroup
= 0; /* In tracing group. */
385 /* Are we in the tracing group ? */
386 in_tgroup
= lttng_check_tracing_group();
389 if ((uid
== 0) || in_tgroup
) {
390 const int ret
= lttng_strncpy(sessiond_sock_path
,
391 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
392 sizeof(sessiond_sock_path
));
404 ret
= try_connect_sessiond(sessiond_sock_path
);
408 /* Global session daemon not available... */
410 /* ...or not in tracing group (and not root), default */
413 * With GNU C < 2.1, snprintf returns -1 if the target buffer
415 * With GNU C >= 2.1, snprintf returns the required size
416 * (excluding closing null)
418 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
419 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
420 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
432 * Connect to the LTTng session daemon.
434 * On success, return the socket's file descriptor. On error, return -1.
436 LTTNG_HIDDEN
int connect_sessiond(void)
440 ret
= set_session_daemon_path();
445 /* Connect to the sesssion daemon. */
446 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
457 static void reset_global_sessiond_connection_state(void)
459 sessiond_socket
= -1;
464 * Clean disconnect from the session daemon.
466 * On success, return 0. On error, return -1.
468 static int disconnect_sessiond(void)
473 ret
= lttcomm_close_unix_sock(sessiond_socket
);
474 reset_global_sessiond_connection_state();
480 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
488 ret
= -LTTNG_ERR_INVALID
;
498 ret
= recv_data_sessiond(buf
, len
);
504 ret
= -LTTNG_ERR_INVALID
;
508 /* Move ownership of command header buffer to user. */
513 /* No command header. */
529 * Ask the session daemon a specific command and put the data into buf.
530 * Takes extra var. len. data and file descriptors as input to send to the
533 * Return size of data (only payload, not header) or a negative error code.
536 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
537 const int *fds
, size_t nb_fd
, const void *vardata
,
538 size_t vardata_len
, void **user_payload_buf
,
539 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
543 struct lttcomm_lttng_msg llm
;
545 ret
= connect_sessiond();
547 ret
= -LTTNG_ERR_NO_SESSIOND
;
550 sessiond_socket
= ret
;
554 ret
= send_session_msg(lsm
);
556 /* Ret value is a valid lttng error code. */
559 /* Send var len data */
560 ret
= send_session_varlen(vardata
, vardata_len
);
562 /* Ret value is a valid lttng error code. */
567 ret
= send_session_fds(fds
, nb_fd
);
569 /* Ret value is a valid lttng error code. */
573 /* Get header from data transmission */
574 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
576 /* Ret value is a valid lttng error code. */
580 /* Check error code if OK */
581 if (llm
.ret_code
!= LTTNG_OK
) {
586 /* Get command header from data transmission */
587 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
588 user_cmd_header_buf
, user_cmd_header_len
);
593 /* Get payload from data transmission */
594 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
603 disconnect_sessiond();
608 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
609 struct lttng_payload
*reply
)
612 struct lttcomm_lttng_msg llm
;
613 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
615 assert(reply
->buffer
.size
== 0);
616 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
618 ret
= connect_sessiond();
620 ret
= -LTTNG_ERR_NO_SESSIOND
;
623 sessiond_socket
= ret
;
627 /* Send command to session daemon */
628 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
629 message
->buffer
.size
);
631 ret
= -LTTNG_ERR_FATAL
;
636 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
639 ret
= -LTTNG_ERR_FATAL
;
644 /* Get header from data transmission */
645 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
647 /* Ret value is a valid lttng error code. */
651 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
653 /* Check error code if OK */
654 if (llm
.ret_code
!= LTTNG_OK
) {
659 if (llm
.cmd_header_size
> 0) {
660 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
666 /* Get command header from data transmission */
667 if (llm
.data_size
> 0) {
668 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
674 if (llm
.fd_count
> 0) {
675 ret
= lttcomm_recv_payload_fds_unix_sock(
676 sessiond_socket
, llm
.fd_count
, reply
);
682 /* Don't return the llm header to the caller. */
683 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
684 reply
->buffer
.size
- sizeof(llm
));
685 ret
= lttng_dynamic_buffer_set_size(
686 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
688 /* Can't happen as size is reduced. */
692 ret
= reply
->buffer
.size
;
695 disconnect_sessiond();
700 * Create lttng handle and return pointer.
702 * The returned pointer will be NULL in case of malloc() error.
704 struct lttng_handle
*lttng_create_handle(const char *session_name
,
705 struct lttng_domain
*domain
)
708 struct lttng_handle
*handle
= NULL
;
710 handle
= zmalloc(sizeof(struct lttng_handle
));
711 if (handle
== NULL
) {
712 PERROR("malloc handle");
716 /* Copy session name */
717 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
718 sizeof(handle
->session_name
));
723 /* Copy lttng domain or leave initialized to 0. */
725 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
736 * Destroy handle by free(3) the pointer.
738 void lttng_destroy_handle(struct lttng_handle
*handle
)
744 * Register an outside consumer.
746 * Returns size of returned session payload data or a negative error code.
748 int lttng_register_consumer(struct lttng_handle
*handle
,
749 const char *socket_path
)
752 struct lttcomm_session_msg lsm
;
754 if (handle
== NULL
|| socket_path
== NULL
) {
755 ret
= -LTTNG_ERR_INVALID
;
759 memset(&lsm
, 0, sizeof(lsm
));
760 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
761 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
762 sizeof(lsm
.session
.name
));
764 ret
= -LTTNG_ERR_INVALID
;
768 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
770 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
771 sizeof(lsm
.u
.reg
.path
));
773 ret
= -LTTNG_ERR_INVALID
;
777 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
783 * Start tracing for all traces of the session.
785 * Returns size of returned session payload data or a negative error code.
787 int lttng_start_tracing(const char *session_name
)
790 struct lttcomm_session_msg lsm
;
792 if (session_name
== NULL
) {
793 ret
= -LTTNG_ERR_INVALID
;
797 memset(&lsm
, 0, sizeof(lsm
));
798 lsm
.cmd_type
= LTTNG_START_TRACE
;
800 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
801 sizeof(lsm
.session
.name
));
803 ret
= -LTTNG_ERR_INVALID
;
807 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
813 * Stop tracing for all traces of the session.
815 static int _lttng_stop_tracing(const char *session_name
, int wait
)
818 struct lttcomm_session_msg lsm
;
820 if (session_name
== NULL
) {
821 ret
= -LTTNG_ERR_INVALID
;
825 memset(&lsm
, 0, sizeof(lsm
));
826 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
828 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
829 sizeof(lsm
.session
.name
));
831 ret
= -LTTNG_ERR_INVALID
;
835 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
836 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
844 /* Check for data availability */
846 data_ret
= lttng_data_pending(session_name
);
848 /* Return the data available call error. */
854 * Data sleep time before retrying (in usec). Don't sleep if the
855 * call returned value indicates availability.
858 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
860 } while (data_ret
!= 0);
868 * Stop tracing and wait for data availability.
870 int lttng_stop_tracing(const char *session_name
)
872 return _lttng_stop_tracing(session_name
, 1);
876 * Stop tracing but _don't_ wait for data availability.
878 int lttng_stop_tracing_no_wait(const char *session_name
)
880 return _lttng_stop_tracing(session_name
, 0);
884 * Add context to a channel.
886 * If the given channel is NULL, add the contexts to all channels.
887 * The event_name param is ignored.
889 * Returns the size of the returned payload data or a negative error code.
891 int lttng_add_context(struct lttng_handle
*handle
,
892 struct lttng_event_context
*ctx
, const char *event_name
,
893 const char *channel_name
)
898 struct lttcomm_session_msg lsm
;
900 /* Safety check. Both are mandatory. */
901 if (handle
== NULL
|| ctx
== NULL
) {
902 ret
= -LTTNG_ERR_INVALID
;
906 memset(&lsm
, 0, sizeof(lsm
));
907 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
909 /* If no channel name, send empty string. */
910 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
911 sizeof(lsm
.u
.context
.channel_name
));
913 ret
= -LTTNG_ERR_INVALID
;
917 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
918 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
919 sizeof(lsm
.session
.name
));
921 ret
= -LTTNG_ERR_INVALID
;
925 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
926 size_t provider_len
, ctx_len
;
927 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
928 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
930 if (!provider_name
|| !ctx_name
) {
931 ret
= -LTTNG_ERR_INVALID
;
935 provider_len
= strlen(provider_name
);
936 if (provider_len
== 0) {
937 ret
= -LTTNG_ERR_INVALID
;
940 lsm
.u
.context
.provider_name_len
= provider_len
;
942 ctx_len
= strlen(ctx_name
);
944 ret
= -LTTNG_ERR_INVALID
;
947 lsm
.u
.context
.context_name_len
= ctx_len
;
949 len
= provider_len
+ ctx_len
;
952 ret
= -LTTNG_ERR_NOMEM
;
956 memcpy(buf
, provider_name
, provider_len
);
957 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
959 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
961 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
963 * Don't leak application addresses to the sessiond.
964 * This is only necessary when ctx is for an app ctx otherwise
965 * the values inside the union (type & config) are overwritten.
967 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
968 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
971 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
978 * Enable event(s) for a channel.
980 * If no event name is specified, all events are enabled.
981 * If no channel name is specified, the default 'channel0' is used.
983 * Returns size of returned session payload data or a negative error code.
985 int lttng_enable_event(struct lttng_handle
*handle
,
986 struct lttng_event
*ev
, const char *channel_name
)
988 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
993 * Create or enable an event with a filter expression.
995 * Return negative error value on error.
996 * Return size of returned session payload data if OK.
998 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
999 struct lttng_event
*event
, const char *channel_name
,
1000 const char *filter_expression
)
1002 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1003 filter_expression
, 0, NULL
);
1007 * Depending on the event, return a newly allocated agent filter expression or
1008 * NULL if not applicable.
1010 * An event with NO loglevel and the name is * will return NULL.
1012 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1015 char *agent_filter
= NULL
;
1019 /* Don't add filter for the '*' event. */
1020 if (strcmp(ev
->name
, "*") != 0) {
1022 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1025 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1033 /* Add loglevel filtering if any for the JUL domain. */
1034 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1037 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1043 if (filter
|| agent_filter
) {
1046 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1047 agent_filter
? agent_filter
: filter
, op
,
1052 agent_filter
= new_filter
;
1054 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1063 return agent_filter
;
1070 * Enable event(s) for a channel, possibly with exclusions and a filter.
1071 * If no event name is specified, all events are enabled.
1072 * If no channel name is specified, the default name is used.
1073 * If filter expression is not NULL, the filter is set for the event.
1074 * If exclusion count is not zero, the exclusions are set for the event.
1075 * Returns size of returned session payload data or a negative error code.
1077 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1078 struct lttng_event
*ev
, const char *channel_name
,
1079 const char *original_filter_expression
,
1080 int exclusion_count
, char **exclusion_list
)
1082 struct lttcomm_session_msg lsm
;
1083 struct lttng_payload payload
;
1085 unsigned int free_filter_expression
= 0;
1086 struct filter_parser_ctx
*ctx
= NULL
;
1089 * We have either a filter or some exclusions, so we need to set up
1090 * a variable-length payload from where to send the data.
1092 lttng_payload_init(&payload
);
1095 * Cast as non-const since we may replace the filter expression
1096 * by a dynamically allocated string. Otherwise, the original
1097 * string is not modified.
1099 char *filter_expression
= (char *) original_filter_expression
;
1101 if (handle
== NULL
|| ev
== NULL
) {
1102 ret
= -LTTNG_ERR_INVALID
;
1107 * Empty filter string will always be rejected by the parser
1108 * anyway, so treat this corner-case early to eliminate
1109 * lttng_fmemopen error for 0-byte allocation.
1111 if (filter_expression
&& filter_expression
[0] == '\0') {
1112 ret
= -LTTNG_ERR_INVALID
;
1116 memset(&lsm
, 0, sizeof(lsm
));
1118 /* If no channel name, send empty string. */
1119 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1120 sizeof(lsm
.u
.enable
.channel_name
));
1122 ret
= -LTTNG_ERR_INVALID
;
1126 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1127 if (ev
->name
[0] == '\0') {
1128 /* Enable all events. */
1129 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1133 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1134 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1136 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1137 sizeof(lsm
.session
.name
));
1139 ret
= -LTTNG_ERR_INVALID
;
1143 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1144 lsm
.u
.enable
.bytecode_len
= 0;
1146 /* Parse filter expression. */
1147 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1148 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1149 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1150 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1151 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1152 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1155 /* Setup JUL filter if needed. */
1156 agent_filter
= set_agent_filter(filter_expression
, ev
);
1157 if (!agent_filter
) {
1158 if (!filter_expression
) {
1160 * No JUL and no filter, just skip
1167 * With an agent filter, the original filter has
1168 * been added to it thus replace the filter
1171 filter_expression
= agent_filter
;
1172 free_filter_expression
= 1;
1176 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1181 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1182 + bytecode_get_len(&ctx
->bytecode
->b
);
1183 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1186 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1187 lsm
.u
.enable
.bytecode_len
+
1188 lsm
.u
.enable
.expression_len
+
1189 LTTNG_SYMBOL_NAME_LEN
*
1192 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1196 /* Put exclusion names first in the data. */
1197 for (i
= 0; i
< exclusion_count
; i
++) {
1198 size_t exclusion_len
;
1200 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1201 LTTNG_SYMBOL_NAME_LEN
);
1202 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1203 /* Exclusion is not NULL-terminated. */
1204 ret
= -LTTNG_ERR_INVALID
;
1208 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1209 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1215 /* Add filter expression next. */
1216 if (filter_expression
) {
1217 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1218 filter_expression
, lsm
.u
.enable
.expression_len
);
1223 /* Add filter bytecode next. */
1224 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1225 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1226 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1231 if (ev
->extended
.ptr
) {
1232 struct lttng_event_extended
*ev_ext
=
1233 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1235 if (ev_ext
->probe_location
) {
1237 * lttng_userspace_probe_location_serialize returns the
1238 * number of bytes that was appended to the buffer.
1240 ret
= lttng_userspace_probe_location_serialize(
1241 ev_ext
->probe_location
, &payload
);
1247 * Set the size of the userspace probe location element
1248 * of the buffer so that the receiving side knows where
1251 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1256 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1258 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1265 assert(fd_count
== 0 || fd_count
== 1);
1266 if (fd_count
== 1) {
1267 struct fd_handle
*handle
=
1268 lttng_payload_view_pop_fd_handle(&view
);
1274 fd_to_send
= fd_handle_get_fd(handle
);
1275 fd_handle_put(handle
);
1278 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1279 fd_count
? &fd_to_send
: NULL
, fd_count
,
1280 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1281 view
.buffer
.size
, NULL
, NULL
, 0);
1285 if (filter_expression
&& ctx
) {
1286 filter_bytecode_free(ctx
);
1287 filter_ir_free(ctx
);
1288 filter_parser_ctx_free(ctx
);
1291 if (free_filter_expression
) {
1293 * The filter expression has been replaced and must be freed as
1294 * it is not the original filter expression received as a
1297 free(filter_expression
);
1301 * Return directly to the caller and don't ask the sessiond since
1302 * something went wrong in the parsing of data above.
1304 lttng_payload_reset(&payload
);
1308 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1312 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1313 struct lttng_event
*ev
, const char *channel_name
,
1314 const char *original_filter_expression
)
1316 struct lttcomm_session_msg lsm
;
1319 unsigned int free_filter_expression
= 0;
1320 struct filter_parser_ctx
*ctx
= NULL
;
1322 * Cast as non-const since we may replace the filter expression
1323 * by a dynamically allocated string. Otherwise, the original
1324 * string is not modified.
1326 char *filter_expression
= (char *) original_filter_expression
;
1328 if (handle
== NULL
|| ev
== NULL
) {
1329 ret
= -LTTNG_ERR_INVALID
;
1334 * Empty filter string will always be rejected by the parser
1335 * anyway, so treat this corner-case early to eliminate
1336 * lttng_fmemopen error for 0-byte allocation.
1338 if (filter_expression
&& filter_expression
[0] == '\0') {
1339 ret
= -LTTNG_ERR_INVALID
;
1343 memset(&lsm
, 0, sizeof(lsm
));
1345 /* If no channel name, send empty string. */
1346 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1347 sizeof(lsm
.u
.disable
.channel_name
));
1349 ret
= -LTTNG_ERR_INVALID
;
1353 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1355 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1356 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1358 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1359 sizeof(lsm
.session
.name
));
1361 ret
= -LTTNG_ERR_INVALID
;
1365 lsm
.u
.disable
.bytecode_len
= 0;
1368 * For the JUL domain, a filter is enforced except for the
1369 * disable all event. This is done to avoid having the event in
1370 * all sessions thus filtering by logger name.
1372 if (filter_expression
== NULL
&&
1373 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1374 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1375 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1380 * We have a filter, so we need to set up a variable-length
1381 * memory block from where to send the data.
1384 /* Parse filter expression */
1385 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1386 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1387 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1388 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1389 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1390 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1393 /* Setup JUL filter if needed. */
1394 agent_filter
= set_agent_filter(filter_expression
, ev
);
1395 if (!agent_filter
) {
1396 if (!filter_expression
) {
1398 * No JUL and no filter, just skip
1405 * With a JUL filter, the original filter has
1406 * been added to it thus replace the filter
1409 filter_expression
= agent_filter
;
1410 free_filter_expression
= 1;
1414 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1419 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1420 + bytecode_get_len(&ctx
->bytecode
->b
);
1421 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1424 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1425 + lsm
.u
.disable
.expression_len
);
1427 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1431 /* Add filter expression. */
1432 if (lsm
.u
.disable
.expression_len
!= 0) {
1435 lsm
.u
.disable
.expression_len
);
1437 /* Add filter bytecode next. */
1438 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1440 + lsm
.u
.disable
.expression_len
,
1442 lsm
.u
.disable
.bytecode_len
);
1445 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1446 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1450 if (filter_expression
&& ctx
) {
1451 filter_bytecode_free(ctx
);
1452 filter_ir_free(ctx
);
1453 filter_parser_ctx_free(ctx
);
1456 if (free_filter_expression
) {
1458 * The filter expression has been replaced and must be freed as
1459 * it is not the original filter expression received as a
1462 free(filter_expression
);
1466 * Return directly to the caller and don't ask the sessiond since
1467 * something went wrong in the parsing of data above.
1472 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1477 * Disable event(s) of a channel and domain.
1478 * If no event name is specified, all events are disabled.
1479 * If no channel name is specified, the default 'channel0' is used.
1480 * Returns size of returned session payload data or a negative error code.
1482 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1483 const char *channel_name
)
1486 struct lttng_event ev
;
1488 memset(&ev
, 0, sizeof(ev
));
1490 ev
.type
= LTTNG_EVENT_ALL
;
1491 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1493 ret
= -LTTNG_ERR_INVALID
;
1497 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1502 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1504 struct lttng_channel
*channel
= NULL
;
1505 struct lttng_channel_extended
*extended
= NULL
;
1511 /* Validate domain. */
1512 switch (domain
->type
) {
1513 case LTTNG_DOMAIN_UST
:
1514 switch (domain
->buf_type
) {
1515 case LTTNG_BUFFER_PER_UID
:
1516 case LTTNG_BUFFER_PER_PID
:
1522 case LTTNG_DOMAIN_KERNEL
:
1523 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1531 channel
= zmalloc(sizeof(*channel
));
1536 extended
= zmalloc(sizeof(*extended
));
1541 channel
->attr
.extended
.ptr
= extended
;
1543 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1551 void lttng_channel_destroy(struct lttng_channel
*channel
)
1557 if (channel
->attr
.extended
.ptr
) {
1558 free(channel
->attr
.extended
.ptr
);
1564 * Enable channel per domain
1565 * Returns size of returned session payload data or a negative error code.
1567 int lttng_enable_channel(struct lttng_handle
*handle
,
1568 struct lttng_channel
*in_chan
)
1571 struct lttcomm_session_msg lsm
;
1572 size_t total_buffer_size_needed_per_cpu
= 0;
1574 /* NULL arguments are forbidden. No default values. */
1575 if (handle
== NULL
|| in_chan
== NULL
) {
1576 return -LTTNG_ERR_INVALID
;
1579 memset(&lsm
, 0, sizeof(lsm
));
1580 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1581 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1583 if (!in_chan
->attr
.extended
.ptr
) {
1584 struct lttng_channel
*channel
;
1585 struct lttng_channel_extended
*extended
;
1587 channel
= lttng_channel_create(&handle
->domain
);
1589 return -LTTNG_ERR_NOMEM
;
1593 * Create a new channel in order to use default extended
1596 extended
= (struct lttng_channel_extended
*)
1597 channel
->attr
.extended
.ptr
;
1598 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1599 lttng_channel_destroy(channel
);
1601 struct lttng_channel_extended
*extended
;
1603 extended
= (struct lttng_channel_extended
*)
1604 in_chan
->attr
.extended
.ptr
;
1605 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1609 * Verify that the amount of memory required to create the requested
1610 * buffer is available on the system at the moment.
1612 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1613 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1614 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1615 return -LTTNG_ERR_NOMEM
;
1618 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1619 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1621 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1622 sizeof(lsm
.session
.name
));
1624 ret
= -LTTNG_ERR_INVALID
;
1628 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1634 * All tracing will be stopped for registered events of the channel.
1635 * Returns size of returned session payload data or a negative error code.
1637 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1640 struct lttcomm_session_msg lsm
;
1642 /* Safety check. Both are mandatory. */
1643 if (handle
== NULL
|| name
== NULL
) {
1644 return -LTTNG_ERR_INVALID
;
1647 memset(&lsm
, 0, sizeof(lsm
));
1649 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1651 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1652 sizeof(lsm
.u
.disable
.channel_name
));
1654 ret
= -LTTNG_ERR_INVALID
;
1658 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1660 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1661 sizeof(lsm
.session
.name
));
1663 ret
= -LTTNG_ERR_INVALID
;
1667 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1673 * Lists all available tracepoints of domain.
1674 * Sets the contents of the events array.
1675 * Returns the number of lttng_event entries in events;
1676 * on error, returns a negative value.
1678 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1679 struct lttng_event
**events
)
1682 struct lttcomm_session_msg lsm
;
1684 if (handle
== NULL
) {
1685 return -LTTNG_ERR_INVALID
;
1688 memset(&lsm
, 0, sizeof(lsm
));
1689 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1690 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1692 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1697 return ret
/ sizeof(struct lttng_event
);
1701 * Lists all available tracepoint fields of domain.
1702 * Sets the contents of the event field array.
1703 * Returns the number of lttng_event_field entries in events;
1704 * on error, returns a negative value.
1706 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1707 struct lttng_event_field
**fields
)
1710 struct lttcomm_session_msg lsm
;
1712 if (handle
== NULL
) {
1713 return -LTTNG_ERR_INVALID
;
1716 memset(&lsm
, 0, sizeof(lsm
));
1717 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1718 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1720 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1725 return ret
/ sizeof(struct lttng_event_field
);
1729 * Lists all available kernel system calls. Allocates and sets the contents of
1732 * Returns the number of lttng_event entries in events; on error, returns a
1735 int lttng_list_syscalls(struct lttng_event
**events
)
1738 struct lttcomm_session_msg lsm
;
1741 return -LTTNG_ERR_INVALID
;
1744 memset(&lsm
, 0, sizeof(lsm
));
1745 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1746 /* Force kernel domain for system calls. */
1747 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1749 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1754 return ret
/ sizeof(struct lttng_event
);
1758 * Returns a human readable string describing
1759 * the error code (a negative value).
1761 const char *lttng_strerror(int code
)
1763 return error_get_str(code
);
1766 enum lttng_error_code
lttng_create_session_ext(
1767 struct lttng_session_descriptor
*session_descriptor
)
1769 enum lttng_error_code ret_code
;
1770 struct lttcomm_session_msg lsm
= {
1771 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1774 struct lttng_buffer_view reply_view
;
1776 bool sessiond_must_generate_ouput
;
1777 struct lttng_dynamic_buffer payload
;
1779 size_t descriptor_size
;
1780 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1782 lttng_dynamic_buffer_init(&payload
);
1783 if (!session_descriptor
) {
1784 ret_code
= LTTNG_ERR_INVALID
;
1788 sessiond_must_generate_ouput
=
1789 !lttng_session_descriptor_is_output_destination_initialized(
1790 session_descriptor
);
1791 if (sessiond_must_generate_ouput
) {
1792 const char *home_dir
= utils_get_home_dir();
1793 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1795 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1796 ret_code
= LTTNG_ERR_FATAL
;
1800 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1801 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1804 ret_code
= LTTNG_ERR_NOMEM
;
1809 descriptor_size
= payload
.size
;
1810 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1813 ret_code
= LTTNG_ERR_INVALID
;
1816 descriptor_size
= payload
.size
- descriptor_size
;
1817 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1819 /* Command returns a session descriptor on success. */
1820 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1821 payload
.size
, &reply
);
1822 if (reply_ret
< 0) {
1823 ret_code
= -reply_ret
;
1825 } else if (reply_ret
== 0) {
1826 /* Socket unexpectedly closed by the session daemon. */
1827 ret_code
= LTTNG_ERR_FATAL
;
1831 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1832 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1835 ret_code
= LTTNG_ERR_FATAL
;
1838 ret_code
= LTTNG_OK
;
1839 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1842 lttng_dynamic_buffer_reset(&payload
);
1843 lttng_session_descriptor_destroy(descriptor_reply
);
1848 * Create a new session using name and url for destination.
1850 * Return 0 on success else a negative LTTng error code.
1852 int lttng_create_session(const char *name
, const char *url
)
1856 struct lttng_uri
*uris
= NULL
;
1857 struct lttng_session_descriptor
*descriptor
= NULL
;
1858 enum lttng_error_code ret_code
;
1861 ret
= -LTTNG_ERR_INVALID
;
1865 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1867 ret
= -LTTNG_ERR_INVALID
;
1872 descriptor
= lttng_session_descriptor_create(name
);
1875 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1876 ret
= -LTTNG_ERR_INVALID
;
1879 descriptor
= lttng_session_descriptor_local_create(name
,
1883 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1887 ret
= -LTTNG_ERR_INVALID
;
1891 ret
= -LTTNG_ERR_INVALID
;
1894 ret_code
= lttng_create_session_ext(descriptor
);
1895 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1897 lttng_session_descriptor_destroy(descriptor
);
1903 * Create a session exclusively used for snapshot.
1905 * Return 0 on success else a negative LTTng error code.
1907 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1910 enum lttng_error_code ret_code
;
1912 struct lttng_uri
*uris
= NULL
;
1913 struct lttng_session_descriptor
*descriptor
= NULL
;
1916 ret
= -LTTNG_ERR_INVALID
;
1920 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1922 ret
= -LTTNG_ERR_INVALID
;
1926 * If the user does not specify a custom subdir, use the session name.
1928 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1929 strlen(uris
[0].subdir
) == 0) {
1930 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1933 PERROR("Failed to set session name as network destination sub-directory");
1934 ret
= -LTTNG_ERR_FATAL
;
1936 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1937 /* Truncated output. */
1938 ret
= -LTTNG_ERR_INVALID
;
1945 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1948 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1949 ret
= -LTTNG_ERR_INVALID
;
1952 descriptor
= lttng_session_descriptor_snapshot_local_create(
1957 descriptor
= lttng_session_descriptor_snapshot_network_create(
1963 ret
= -LTTNG_ERR_INVALID
;
1967 ret
= -LTTNG_ERR_INVALID
;
1970 ret_code
= lttng_create_session_ext(descriptor
);
1971 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1973 lttng_session_descriptor_destroy(descriptor
);
1979 * Create a session exclusively used for live.
1981 * Return 0 on success else a negative LTTng error code.
1983 int lttng_create_session_live(const char *name
, const char *url
,
1984 unsigned int timer_interval
)
1987 enum lttng_error_code ret_code
;
1988 struct lttng_session_descriptor
*descriptor
= NULL
;
1991 ret
= -LTTNG_ERR_INVALID
;
1996 descriptor
= lttng_session_descriptor_live_network_create(
1997 name
, url
, NULL
, timer_interval
);
1999 descriptor
= lttng_session_descriptor_live_create(
2000 name
, timer_interval
);
2003 ret
= -LTTNG_ERR_INVALID
;
2006 ret_code
= lttng_create_session_ext(descriptor
);
2007 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2009 lttng_session_descriptor_destroy(descriptor
);
2014 * Stop the session and wait for the data before destroying it
2016 * Return 0 on success else a negative LTTng error code.
2018 int lttng_destroy_session(const char *session_name
)
2021 enum lttng_error_code ret_code
;
2022 enum lttng_destruction_handle_status status
;
2023 struct lttng_destruction_handle
*handle
= NULL
;
2026 * Stop the tracing and wait for the data to be
2029 ret
= _lttng_stop_tracing(session_name
, 1);
2030 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2034 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2035 if (ret_code
!= LTTNG_OK
) {
2036 ret
= (int) -ret_code
;
2041 /* Block until the completion of the destruction of the session. */
2042 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2043 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2044 ret
= -LTTNG_ERR_UNK
;
2048 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2049 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2050 ret
= -LTTNG_ERR_UNK
;
2053 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2055 lttng_destruction_handle_destroy(handle
);
2060 * Destroy the session without waiting for the data.
2062 int lttng_destroy_session_no_wait(const char *session_name
)
2064 enum lttng_error_code ret_code
;
2066 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2067 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2071 * Ask the session daemon for all available sessions.
2072 * Sets the contents of the sessions array.
2073 * Returns the number of lttng_session entries in sessions;
2074 * on error, returns a negative value.
2076 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2079 struct lttcomm_session_msg lsm
;
2080 const size_t session_size
= sizeof(struct lttng_session
) +
2081 sizeof(struct lttng_session_extended
);
2082 size_t session_count
, i
;
2083 struct lttng_session_extended
*sessions_extended_begin
;
2084 struct lttng_session
*sessions
= NULL
;
2086 memset(&lsm
, 0, sizeof(lsm
));
2087 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2088 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2093 ret
= -LTTNG_ERR_FATAL
;
2097 if (ret
% session_size
) {
2098 ret
= -LTTNG_ERR_UNK
;
2100 *out_sessions
= NULL
;
2103 session_count
= (size_t) ret
/ session_size
;
2104 sessions_extended_begin
= (struct lttng_session_extended
*)
2105 (&sessions
[session_count
]);
2107 /* Set extended session info pointers. */
2108 for (i
= 0; i
< session_count
; i
++) {
2109 struct lttng_session
*session
= &sessions
[i
];
2110 struct lttng_session_extended
*extended
=
2111 &(sessions_extended_begin
[i
]);
2113 session
->extended
.ptr
= extended
;
2116 ret
= (int) session_count
;
2117 *out_sessions
= sessions
;
2122 enum lttng_error_code
lttng_session_get_creation_time(
2123 const struct lttng_session
*session
, uint64_t *creation_time
)
2125 enum lttng_error_code ret
= LTTNG_OK
;
2126 struct lttng_session_extended
*extended
;
2128 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2129 ret
= LTTNG_ERR_INVALID
;
2133 extended
= session
->extended
.ptr
;
2134 if (!extended
->creation_time
.is_set
) {
2135 /* Not created on the session daemon yet. */
2136 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2139 *creation_time
= extended
->creation_time
.value
;
2144 int lttng_set_session_shm_path(const char *session_name
,
2145 const char *shm_path
)
2148 struct lttcomm_session_msg lsm
;
2150 if (session_name
== NULL
) {
2151 return -LTTNG_ERR_INVALID
;
2154 memset(&lsm
, 0, sizeof(lsm
));
2155 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2157 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2158 sizeof(lsm
.session
.name
));
2160 ret
= -LTTNG_ERR_INVALID
;
2164 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2165 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2167 ret
= -LTTNG_ERR_INVALID
;
2171 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2177 * Ask the session daemon for all available domains of a session.
2178 * Sets the contents of the domains array.
2179 * Returns the number of lttng_domain entries in domains;
2180 * on error, returns a negative value.
2182 int lttng_list_domains(const char *session_name
,
2183 struct lttng_domain
**domains
)
2186 struct lttcomm_session_msg lsm
;
2188 if (session_name
== NULL
) {
2189 ret
= -LTTNG_ERR_INVALID
;
2193 memset(&lsm
, 0, sizeof(lsm
));
2194 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2196 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2197 sizeof(lsm
.session
.name
));
2199 ret
= -LTTNG_ERR_INVALID
;
2203 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2208 return ret
/ sizeof(struct lttng_domain
);
2214 * Ask the session daemon for all available channels of a session.
2215 * Sets the contents of the channels array.
2216 * Returns the number of lttng_channel entries in channels;
2217 * on error, returns a negative value.
2219 int lttng_list_channels(struct lttng_handle
*handle
,
2220 struct lttng_channel
**channels
)
2223 size_t channel_count
, i
;
2224 const size_t channel_size
= sizeof(struct lttng_channel
) +
2225 sizeof(struct lttng_channel_extended
);
2226 struct lttcomm_session_msg lsm
;
2229 if (handle
== NULL
) {
2230 ret
= -LTTNG_ERR_INVALID
;
2234 memset(&lsm
, 0, sizeof(lsm
));
2235 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2236 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2237 sizeof(lsm
.session
.name
));
2239 ret
= -LTTNG_ERR_INVALID
;
2243 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2245 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2250 if (ret
% channel_size
) {
2251 ret
= -LTTNG_ERR_UNK
;
2256 channel_count
= (size_t) ret
/ channel_size
;
2258 /* Set extended info pointers */
2259 extended_at
= ((void *) *channels
) +
2260 channel_count
* sizeof(struct lttng_channel
);
2261 for (i
= 0; i
< channel_count
; i
++) {
2262 struct lttng_channel
*chan
= &(*channels
)[i
];
2264 chan
->attr
.extended
.ptr
= extended_at
;
2265 extended_at
+= sizeof(struct lttng_channel_extended
);
2268 ret
= (int) channel_count
;
2274 * Ask the session daemon for all available events of a session channel.
2275 * Sets the contents of the events array.
2276 * Returns the number of lttng_event entries in events;
2277 * on error, returns a negative value.
2279 int lttng_list_events(struct lttng_handle
*handle
,
2280 const char *channel_name
, struct lttng_event
**events
)
2283 struct lttcomm_session_msg lsm
= {};
2284 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2285 uint32_t nb_events
, i
;
2286 const void *comm_ext_at
;
2287 struct lttng_dynamic_buffer listing
;
2289 struct lttng_payload payload
;
2290 struct lttng_payload payload_copy
;
2291 struct lttng_payload_view lsm_view
=
2292 lttng_payload_view_init_from_buffer(
2293 (const char *) &lsm
, 0, sizeof(lsm
));
2294 struct lttng_buffer_view cmd_header_view
;
2295 struct lttng_buffer_view cmd_payload_view
;
2296 struct lttng_buffer_view flat_events_view
;
2297 struct lttng_buffer_view ext_view
;
2299 /* Safety check. An handle and channel name are mandatory */
2300 if (handle
== NULL
|| channel_name
== NULL
) {
2301 ret
= -LTTNG_ERR_INVALID
;
2305 lttng_payload_init(&payload
);
2306 lttng_payload_init(&payload_copy
);
2308 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2309 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2310 sizeof(lsm
.session
.name
));
2312 ret
= -LTTNG_ERR_INVALID
;
2316 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2317 sizeof(lsm
.u
.list
.channel_name
));
2319 ret
= -LTTNG_ERR_INVALID
;
2323 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2325 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2331 * A copy of the payload is performed since it will be
2332 * consumed twice. Consuming the same payload twice is invalid since
2333 * it will cause any received file descriptor to become "shared"
2334 * between different instances of the resulting objects.
2336 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2338 ret
= -LTTNG_ERR_NOMEM
;
2342 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2343 &payload
.buffer
, 0, sizeof(*cmd_header
));
2344 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2345 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2349 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2351 /* Set number of events and free command header */
2352 nb_events
= cmd_header
->nb_events
;
2353 if (nb_events
> INT_MAX
) {
2354 ret
= -LTTNG_ERR_OVERFLOW
;
2358 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2359 &payload
.buffer
, sizeof(*cmd_header
), -1);
2362 * The buffer that is returned must contain a "flat" version of
2363 * the events that are returned. In other words, all pointers
2364 * within an lttng_event must point to a location within the returned
2365 * buffer so that the user may free everything by simply calling free()
2366 * on the returned buffer. This is needed in order to maintain API
2369 * A first pass is performed to compute the size of the buffer that
2370 * must be allocated. A second pass is then performed to setup
2371 * the returned events so that their members always point within the
2374 * The layout of the returned buffer is as follows:
2375 * - struct lttng_event[nb_events],
2376 * - nb_events times the following:
2377 * - struct lttng_event_extended,
2378 * - flattened version of userspace_probe_location
2379 * - filter_expression
2381 * - padding to align to 64-bits
2383 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2384 nb_events
* sizeof(struct lttng_event
), -1);
2385 comm_ext_at
= ext_view
.data
;
2386 storage_req
= nb_events
* sizeof(struct lttng_event
);
2388 struct lttng_payload_view payload_view
=
2389 lttng_payload_view_from_payload(&payload
, 0, -1);
2391 for (i
= 0; i
< nb_events
; i
++) {
2392 const struct lttcomm_event_extended_header
*ext_comm
=
2393 (struct lttcomm_event_extended_header
*)
2395 int probe_storage_req
= 0;
2397 comm_ext_at
+= sizeof(*ext_comm
);
2398 comm_ext_at
+= ext_comm
->filter_len
;
2399 comm_ext_at
+= ext_comm
->nb_exclusions
*
2400 LTTNG_SYMBOL_NAME_LEN
;
2402 if (ext_comm
->userspace_probe_location_len
) {
2403 struct lttng_userspace_probe_location
2404 *probe_location
= NULL
;
2405 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2407 (const char *) comm_ext_at
-
2408 payload_view
.buffer
.data
,
2409 ext_comm
->userspace_probe_location_len
);
2411 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2412 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2417 * Create a temporary userspace probe location
2418 * to determine the size needed by a "flattened"
2419 * version of that same probe location.
2421 ret
= lttng_userspace_probe_location_create_from_payload(
2422 &probe_location_view
,
2425 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2429 ret
= lttng_userspace_probe_location_flatten(
2430 probe_location
, NULL
);
2431 lttng_userspace_probe_location_destroy(
2434 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2438 probe_storage_req
= ret
;
2439 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2442 storage_req
+= sizeof(struct lttng_event_extended
);
2443 storage_req
+= ext_comm
->filter_len
;
2444 storage_req
+= ext_comm
->nb_exclusions
*
2445 LTTNG_SYMBOL_NAME_LEN
;
2446 /* Padding to ensure the flat probe is aligned. */
2447 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2448 storage_req
+= probe_storage_req
;
2452 lttng_dynamic_buffer_init(&listing
);
2454 * We must ensure that "listing" is never resized so as to preserve
2455 * the validity of the flattened objects.
2457 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2459 ret
= -LTTNG_ERR_NOMEM
;
2463 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2464 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2465 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2466 nb_events
* sizeof(struct lttng_event
));
2467 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2469 ret
= -LTTNG_ERR_NOMEM
;
2470 goto free_dynamic_buffer
;
2473 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2474 nb_events
* sizeof(struct lttng_event
), -1);
2475 comm_ext_at
= ext_view
.data
;
2478 struct lttng_payload_view payload_copy_view
=
2479 lttng_payload_view_from_payload(
2480 &payload_copy
, 0, -1);
2482 for (i
= 0; i
< nb_events
; i
++) {
2483 struct lttng_event
*event
= (typeof(event
))(
2485 (sizeof(struct lttng_event
) * i
));
2486 const struct lttcomm_event_extended_header
*ext_comm
=
2487 (typeof(ext_comm
)) comm_ext_at
;
2488 struct lttng_event_extended
*event_extended
=
2489 (typeof(event_extended
))(listing
.data
+
2492 /* Insert struct lttng_event_extended. */
2493 ret
= lttng_dynamic_buffer_set_size(&listing
,
2494 listing
.size
+ sizeof(*event_extended
));
2496 ret
= -LTTNG_ERR_NOMEM
;
2497 goto free_dynamic_buffer
;
2499 event
->extended
.ptr
= event_extended
;
2501 comm_ext_at
+= sizeof(*ext_comm
);
2503 /* Insert filter expression. */
2504 if (ext_comm
->filter_len
) {
2505 event_extended
->filter_expression
=
2506 listing
.data
+ listing
.size
;
2507 ret
= lttng_dynamic_buffer_append(&listing
,
2509 ext_comm
->filter_len
);
2511 ret
= -LTTNG_ERR_NOMEM
;
2512 goto free_dynamic_buffer
;
2514 comm_ext_at
+= ext_comm
->filter_len
;
2517 /* Insert exclusions. */
2518 if (ext_comm
->nb_exclusions
) {
2519 event_extended
->exclusions
.count
=
2520 ext_comm
->nb_exclusions
;
2521 event_extended
->exclusions
.strings
=
2522 listing
.data
+ listing
.size
;
2524 ret
= lttng_dynamic_buffer_append(&listing
,
2526 ext_comm
->nb_exclusions
*
2527 LTTNG_SYMBOL_NAME_LEN
);
2529 ret
= -LTTNG_ERR_NOMEM
;
2530 goto free_dynamic_buffer
;
2532 comm_ext_at
+= ext_comm
->nb_exclusions
*
2533 LTTNG_SYMBOL_NAME_LEN
;
2536 /* Insert padding to align to 64-bits. */
2537 ret
= lttng_dynamic_buffer_set_size(&listing
,
2538 ALIGN_TO(listing
.size
,
2541 ret
= -LTTNG_ERR_NOMEM
;
2542 goto free_dynamic_buffer
;
2545 /* Insert flattened userspace probe location. */
2546 if (ext_comm
->userspace_probe_location_len
) {
2547 struct lttng_userspace_probe_location
2548 *probe_location
= NULL
;
2549 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2551 (const char *) comm_ext_at
-
2552 payload_copy_view
.buffer
.data
,
2553 ext_comm
->userspace_probe_location_len
);
2555 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2556 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2557 goto free_dynamic_buffer
;
2560 ret
= lttng_userspace_probe_location_create_from_payload(
2561 &probe_location_view
,
2564 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2565 goto free_dynamic_buffer
;
2568 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2571 ret
= lttng_userspace_probe_location_flatten(
2572 probe_location
, &listing
);
2573 lttng_userspace_probe_location_destroy(
2576 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2577 goto free_dynamic_buffer
;
2580 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2585 /* Don't reset listing buffer as we return its content. */
2586 *events
= (struct lttng_event
*) listing
.data
;
2587 lttng_dynamic_buffer_init(&listing
);
2588 ret
= (int) nb_events
;
2589 free_dynamic_buffer
:
2590 lttng_dynamic_buffer_reset(&listing
);
2592 lttng_payload_reset(&payload
);
2593 lttng_payload_reset(&payload_copy
);
2598 * Sets the tracing_group variable with name.
2599 * This function allocates memory pointed to by tracing_group.
2600 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2602 int lttng_set_tracing_group(const char *name
)
2605 return -LTTNG_ERR_INVALID
;
2608 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2609 return -LTTNG_ERR_FATAL
;
2615 int lttng_calibrate(struct lttng_handle
*handle
,
2616 struct lttng_calibrate
*calibrate
)
2619 * This command was removed in LTTng 2.9.
2621 return -LTTNG_ERR_UND
;
2625 * Set default channel attributes.
2626 * If either or both of the arguments are null, attr content is zeroe'd.
2628 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2629 struct lttng_channel_attr
*attr
)
2631 struct lttng_channel_extended
*extended
;
2634 if (attr
== NULL
|| domain
== NULL
) {
2638 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2639 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2641 /* Same for all domains. */
2642 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2643 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2644 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2646 switch (domain
->type
) {
2647 case LTTNG_DOMAIN_KERNEL
:
2648 attr
->switch_timer_interval
=
2649 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2650 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2651 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2652 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2653 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2655 extended
->monitor_timer_interval
=
2656 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2657 extended
->blocking_timeout
=
2658 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2661 case LTTNG_DOMAIN_UST
:
2662 switch (domain
->buf_type
) {
2663 case LTTNG_BUFFER_PER_UID
:
2664 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2665 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2666 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2667 attr
->switch_timer_interval
=
2668 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2669 attr
->read_timer_interval
=
2670 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2672 extended
->monitor_timer_interval
=
2673 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2674 extended
->blocking_timeout
=
2675 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2678 case LTTNG_BUFFER_PER_PID
:
2680 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2681 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2682 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2683 attr
->switch_timer_interval
=
2684 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2685 attr
->read_timer_interval
=
2686 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2688 extended
->monitor_timer_interval
=
2689 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2690 extended
->blocking_timeout
=
2691 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2696 /* Default behavior: leave set to 0. */
2700 attr
->extended
.ptr
= extended
;
2703 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2704 uint64_t *discarded_events
)
2707 struct lttng_channel_extended
*chan_ext
;
2709 if (!channel
|| !discarded_events
) {
2710 ret
= -LTTNG_ERR_INVALID
;
2714 chan_ext
= channel
->attr
.extended
.ptr
;
2717 * This can happen since the lttng_channel structure is
2718 * used for other tasks where this pointer is never set.
2720 *discarded_events
= 0;
2724 *discarded_events
= chan_ext
->discarded_events
;
2729 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2730 uint64_t *lost_packets
)
2733 struct lttng_channel_extended
*chan_ext
;
2735 if (!channel
|| !lost_packets
) {
2736 ret
= -LTTNG_ERR_INVALID
;
2740 chan_ext
= channel
->attr
.extended
.ptr
;
2743 * This can happen since the lttng_channel structure is
2744 * used for other tasks where this pointer is never set.
2750 *lost_packets
= chan_ext
->lost_packets
;
2755 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2756 uint64_t *monitor_timer_interval
)
2760 if (!chan
|| !monitor_timer_interval
) {
2761 ret
= -LTTNG_ERR_INVALID
;
2765 if (!chan
->attr
.extended
.ptr
) {
2766 ret
= -LTTNG_ERR_INVALID
;
2770 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2771 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2776 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2777 uint64_t monitor_timer_interval
)
2781 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2782 ret
= -LTTNG_ERR_INVALID
;
2786 ((struct lttng_channel_extended
*)
2787 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2788 monitor_timer_interval
;
2793 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2794 int64_t *blocking_timeout
)
2798 if (!chan
|| !blocking_timeout
) {
2799 ret
= -LTTNG_ERR_INVALID
;
2803 if (!chan
->attr
.extended
.ptr
) {
2804 ret
= -LTTNG_ERR_INVALID
;
2808 *blocking_timeout
= ((struct lttng_channel_extended
*)
2809 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2814 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2815 int64_t blocking_timeout
)
2818 int64_t msec_timeout
;
2820 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2821 ret
= -LTTNG_ERR_INVALID
;
2825 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2826 ret
= -LTTNG_ERR_INVALID
;
2831 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2832 * us to accept a narrower range of values (msecs expressed as a signed
2835 msec_timeout
= blocking_timeout
/ 1000;
2836 if (msec_timeout
!= (int32_t) msec_timeout
) {
2837 ret
= -LTTNG_ERR_INVALID
;
2841 ((struct lttng_channel_extended
*)
2842 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2849 * Check if session daemon is alive.
2851 * Return 1 if alive or 0 if not.
2852 * On error returns a negative value.
2854 int lttng_session_daemon_alive(void)
2858 ret
= set_session_daemon_path();
2864 if (*sessiond_sock_path
== '\0') {
2866 * No socket path set. Weird error which means the constructor
2872 ret
= try_connect_sessiond(sessiond_sock_path
);
2883 * Set URL for a consumer for a session and domain.
2885 * Return 0 on success, else a negative value.
2887 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2888 const char *control_url
, const char *data_url
)
2892 struct lttcomm_session_msg lsm
;
2893 struct lttng_uri
*uris
= NULL
;
2895 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2896 ret
= -LTTNG_ERR_INVALID
;
2900 memset(&lsm
, 0, sizeof(lsm
));
2902 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2904 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2905 sizeof(lsm
.session
.name
));
2907 ret
= -LTTNG_ERR_INVALID
;
2911 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2913 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2915 ret
= -LTTNG_ERR_INVALID
;
2919 lsm
.u
.uri
.size
= size
;
2921 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2922 sizeof(struct lttng_uri
) * size
, NULL
);
2932 int lttng_enable_consumer(struct lttng_handle
*handle
);
2933 int lttng_enable_consumer(struct lttng_handle
*handle
)
2941 int lttng_disable_consumer(struct lttng_handle
*handle
);
2942 int lttng_disable_consumer(struct lttng_handle
*handle
)
2950 int _lttng_create_session_ext(const char *name
, const char *url
,
2951 const char *datetime
);
2952 int _lttng_create_session_ext(const char *name
, const char *url
,
2953 const char *datetime
)
2959 * For a given session name, this call checks if the data is ready to be read
2960 * or is still being extracted by the consumer(s) hence not ready to be used by
2963 int lttng_data_pending(const char *session_name
)
2966 struct lttcomm_session_msg lsm
;
2967 uint8_t *pending
= NULL
;
2969 if (session_name
== NULL
) {
2970 return -LTTNG_ERR_INVALID
;
2973 memset(&lsm
, 0, sizeof(lsm
));
2974 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2976 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2977 sizeof(lsm
.session
.name
));
2979 ret
= -LTTNG_ERR_INVALID
;
2983 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2986 } else if (ret
!= 1) {
2987 /* Unexpected payload size */
2988 ret
= -LTTNG_ERR_INVALID
;
2990 } else if (!pending
) {
2991 /* Internal error. */
2992 ret
= -LTTNG_ERR_UNK
;
2996 ret
= (int) *pending
;
3003 * Regenerate the metadata for a session.
3004 * Return 0 on success, a negative error code on error.
3006 int lttng_regenerate_metadata(const char *session_name
)
3009 struct lttcomm_session_msg lsm
;
3011 if (!session_name
) {
3012 ret
= -LTTNG_ERR_INVALID
;
3016 memset(&lsm
, 0, sizeof(lsm
));
3017 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3019 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3020 sizeof(lsm
.session
.name
));
3022 ret
= -LTTNG_ERR_INVALID
;
3026 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3037 * Deprecated, replaced by lttng_regenerate_metadata.
3039 int lttng_metadata_regenerate(const char *session_name
)
3041 return lttng_regenerate_metadata(session_name
);
3045 * Regenerate the statedump of a session.
3046 * Return 0 on success, a negative error code on error.
3048 int lttng_regenerate_statedump(const char *session_name
)
3051 struct lttcomm_session_msg lsm
;
3053 if (!session_name
) {
3054 ret
= -LTTNG_ERR_INVALID
;
3058 memset(&lsm
, 0, sizeof(lsm
));
3059 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3061 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3062 sizeof(lsm
.session
.name
));
3064 ret
= -LTTNG_ERR_INVALID
;
3068 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3078 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3081 struct lttcomm_session_msg lsm
= {
3082 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3084 struct lttcomm_session_msg
*message_lsm
;
3085 struct lttng_payload message
;
3086 struct lttng_payload reply
;
3087 struct lttng_trigger
*reply_trigger
= NULL
;
3088 enum lttng_domain_type domain_type
;
3089 const struct lttng_credentials user_creds
= {
3090 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3091 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3095 lttng_payload_init(&message
);
3096 lttng_payload_init(&reply
);
3099 ret
= -LTTNG_ERR_INVALID
;
3103 if (!trigger
->creds
.uid
.is_set
) {
3104 /* Use the client's credentials as the trigger credentials. */
3105 lttng_trigger_set_credentials(trigger
, &user_creds
);
3108 * Validate that either the current trigger credentials and the
3109 * client credentials are identical or that the current user is
3110 * root. The root user can register, unregister triggers for
3111 * himself and other users.
3113 * This check is also present on the sessiond side, using the
3114 * credentials passed on the socket. These check are all
3117 const struct lttng_credentials
*trigger_creds
=
3118 lttng_trigger_get_credentials(trigger
);
3120 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3121 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3122 ret
= -LTTNG_ERR_EPERM
;
3128 if (!lttng_trigger_validate(trigger
)) {
3129 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3133 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3136 lsm
.domain
.type
= domain_type
;
3138 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3140 ret
= -LTTNG_ERR_NOMEM
;
3145 * This is needed to populate the trigger object size for the command
3148 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3150 ret
= lttng_trigger_serialize(trigger
, &message
);
3152 ret
= -LTTNG_ERR_UNK
;
3156 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3159 struct lttng_payload_view message_view
=
3160 lttng_payload_view_from_payload(
3163 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3165 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3172 struct lttng_payload_view reply_view
=
3173 lttng_payload_view_from_payload(
3174 &reply
, 0, reply
.buffer
.size
);
3176 ret
= lttng_trigger_create_from_payload(
3177 &reply_view
, &reply_trigger
);
3179 ret
= -LTTNG_ERR_FATAL
;
3184 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3186 ret
= -LTTNG_ERR_FATAL
;
3192 lttng_payload_reset(&message
);
3193 lttng_payload_reset(&reply
);
3194 lttng_trigger_destroy(reply_trigger
);
3198 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3201 struct lttcomm_session_msg lsm
;
3202 struct lttcomm_session_msg
*message_lsm
;
3203 struct lttng_payload message
;
3204 struct lttng_payload reply
;
3205 const struct lttng_credentials user_creds
= {
3206 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3207 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3210 lttng_payload_init(&message
);
3211 lttng_payload_init(&reply
);
3214 ret
= -LTTNG_ERR_INVALID
;
3218 if (!trigger
->creds
.uid
.is_set
) {
3219 /* Use the client's credentials as the trigger credentials. */
3220 lttng_trigger_set_credentials(trigger
, &user_creds
);
3223 * Validate that either the current trigger credentials and the
3224 * client credentials are identical or that the current user is
3225 * root. The root user can register, unregister triggers for
3226 * himself and other users.
3228 * This check is also present on the sessiond side, using the
3229 * credentials passed on the socket. These check are all
3232 const struct lttng_credentials
*trigger_creds
=
3233 lttng_trigger_get_credentials(trigger
);
3235 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3236 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3237 ret
= -LTTNG_ERR_EPERM
;
3243 if (!lttng_trigger_validate(trigger
)) {
3244 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3248 memset(&lsm
, 0, sizeof(lsm
));
3249 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3251 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3253 ret
= -LTTNG_ERR_NOMEM
;
3258 * This is needed to populate the trigger object size for the command
3259 * header and number of fds sent.
3261 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3263 ret
= lttng_trigger_serialize(trigger
, &message
);
3265 ret
= -LTTNG_ERR_UNK
;
3269 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3272 struct lttng_payload_view message_view
=
3273 lttng_payload_view_from_payload(
3277 * Update the message header with the number of fd that will be
3280 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3283 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3291 lttng_payload_reset(&message
);
3292 lttng_payload_reset(&reply
);
3297 * Ask the session daemon for all registered triggers for the current user.
3299 * Allocates and return an lttng_triggers set.
3300 * On error, returns a suitable lttng_error_code.
3302 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3305 enum lttng_error_code ret_code
= LTTNG_OK
;
3306 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3307 struct lttng_triggers
*local_triggers
= NULL
;
3308 struct lttng_payload reply
;
3309 struct lttng_payload_view lsm_view
=
3310 lttng_payload_view_init_from_buffer(
3311 (const char *) &lsm
, 0, sizeof(lsm
));
3313 lttng_payload_init(&reply
);
3315 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3317 ret_code
= (enum lttng_error_code
) -ret
;
3322 struct lttng_payload_view reply_view
=
3323 lttng_payload_view_from_payload(
3324 &reply
, 0, reply
.buffer
.size
);
3326 ret
= lttng_triggers_create_from_payload(
3327 &reply_view
, &local_triggers
);
3329 ret_code
= LTTNG_ERR_FATAL
;
3334 *triggers
= local_triggers
;
3335 local_triggers
= NULL
;
3337 lttng_payload_reset(&reply
);
3338 lttng_triggers_destroy(local_triggers
);
3345 static void __attribute__((constructor
)) init(void)
3347 /* Set default session group */
3348 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3354 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3356 free(tracing_group
);