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
20 #include <common/bytecode/bytecode.h>
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-array.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/payload-view.h>
28 #include <common/payload.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/error-query-internal.h>
38 #include <lttng/event-internal.h>
39 #include <lttng/health-internal.h>
40 #include <lttng/lttng-error.h>
41 #include <lttng/lttng.h>
42 #include <lttng/session-descriptor-internal.h>
43 #include <lttng/session-internal.h>
44 #include <lttng/trigger/trigger-internal.h>
45 #include <lttng/userspace-probe-internal.h>
47 #include "lttng-ctl-helper.h"
48 #include <common/filter/filter-ast.h>
49 #include <common/filter/filter-parser.h>
50 #include <common/filter/memstream.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: '%s' (%d)", lttcomm_sessiond_command_str(lsm
->cmd_type
),
122 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
123 sizeof(struct lttcomm_session_msg
));
125 ret
= -LTTNG_ERR_FATAL
;
133 * Send var len data to the session daemon.
135 * On success, returns the number of bytes sent (>=0)
136 * On error, returns -1
138 static int send_session_varlen(const void *data
, size_t len
)
143 ret
= -LTTNG_ERR_NO_SESSIOND
;
152 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
154 ret
= -LTTNG_ERR_FATAL
;
162 * Send file descriptors to the session daemon.
164 * On success, returns the number of bytes sent (>=0)
165 * On error, returns -1
167 static int send_session_fds(const int *fds
, size_t nb_fd
)
172 ret
= -LTTNG_ERR_NO_SESSIOND
;
176 if (!fds
|| !nb_fd
) {
181 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
183 ret
= -LTTNG_ERR_FATAL
;
191 * Receive data from the sessiond socket.
193 * On success, returns the number of bytes received (>=0)
194 * On error, returns a negative lttng_error_code.
196 static int recv_data_sessiond(void *buf
, size_t len
)
200 LTTNG_ASSERT(len
> 0);
203 ret
= -LTTNG_ERR_NO_SESSIOND
;
207 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
209 ret
= -LTTNG_ERR_FATAL
;
210 } else if (ret
== 0) {
211 ret
= -LTTNG_ERR_NO_SESSIOND
;
219 * Receive a payload from the session daemon by appending to an existing
221 * On success, returns the number of bytes received (>=0)
222 * On error, returns a negative lttng_error_code.
224 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
227 const size_t original_payload_size
= payload
->buffer
.size
;
229 ret
= lttng_dynamic_buffer_set_size(
230 &payload
->buffer
, payload
->buffer
.size
+ len
);
232 ret
= -LTTNG_ERR_NOMEM
;
236 ret
= recv_data_sessiond(
237 payload
->buffer
.data
+ original_payload_size
, len
);
243 * Check if we are in the specified group.
245 * If yes return 1, else return -1.
248 int lttng_check_tracing_group(void)
250 gid_t
*grp_list
, tracing_gid
;
251 int grp_list_size
, grp_id
, i
;
253 const char *grp_name
= tracing_group
;
255 /* Get GID of group 'tracing' */
256 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
257 /* If grp_tracing is NULL, the group does not exist. */
261 /* Get number of supplementary group IDs */
262 grp_list_size
= getgroups(0, NULL
);
263 if (grp_list_size
< 0) {
268 /* Alloc group list of the right size */
269 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
274 grp_id
= getgroups(grp_list_size
, grp_list
);
280 for (i
= 0; i
< grp_list_size
; i
++) {
281 if (grp_list
[i
] == tracing_gid
) {
294 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
298 size_t best_mem_info
;
299 size_t num_bytes_requested_total
;
302 * Get the number of CPU currently online to compute the amount of
303 * memory needed to create a buffer for every CPU.
305 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
310 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
313 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
314 * reliable estimate we can get but it is only exposed by the kernel
315 * since 3.14. (See Linux kernel commit:
316 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
318 ret
= utils_get_memory_available(&best_mem_info
);
324 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
325 * is a sanity check for obvious user error.
327 ret
= utils_get_memory_total(&best_mem_info
);
335 return best_mem_info
>= num_bytes_requested_total
;
339 * Try connect to session daemon with sock_path.
341 * Return 0 on success, else -1
343 static int try_connect_sessiond(const char *sock_path
)
347 /* If socket exist, we check if the daemon listens for connect. */
348 ret
= access(sock_path
, F_OK
);
354 ret
= lttcomm_connect_unix_sock(sock_path
);
360 ret
= lttcomm_close_unix_sock(ret
);
362 PERROR("lttcomm_close_unix_sock");
372 * Set sessiond socket path by putting it in the global sessiond_sock_path
375 * Returns 0 on success, negative value on failure (the sessiond socket path
376 * is somehow too long or ENOMEM).
378 static int set_session_daemon_path(void)
380 int in_tgroup
= 0; /* In tracing group. */
386 /* Are we in the tracing group ? */
387 in_tgroup
= lttng_check_tracing_group();
390 if ((uid
== 0) || in_tgroup
== 1) {
391 const int ret
= lttng_strncpy(sessiond_sock_path
,
392 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
393 sizeof(sessiond_sock_path
));
405 ret
= try_connect_sessiond(sessiond_sock_path
);
409 /* Global session daemon not available... */
411 /* ...or not in tracing group (and not root), default */
414 * With GNU C < 2.1, snprintf returns -1 if the target buffer
416 * With GNU C >= 2.1, snprintf returns the required size
417 * (excluding closing null)
419 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
420 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
421 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
433 * Connect to the LTTng session daemon.
435 * On success, return the socket's file descriptor. On error, return -1.
437 LTTNG_HIDDEN
int connect_sessiond(void)
441 ret
= set_session_daemon_path();
446 /* Connect to the sesssion daemon. */
447 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
458 static void reset_global_sessiond_connection_state(void)
460 sessiond_socket
= -1;
465 * Clean disconnect from the session daemon.
467 * On success, return 0. On error, return -1.
469 static int disconnect_sessiond(void)
474 ret
= lttcomm_close_unix_sock(sessiond_socket
);
475 reset_global_sessiond_connection_state();
481 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
489 ret
= -LTTNG_ERR_INVALID
;
499 ret
= recv_data_sessiond(buf
, len
);
505 ret
= -LTTNG_ERR_INVALID
;
509 /* Move ownership of command header buffer to user. */
514 /* No command header. */
530 * Ask the session daemon a specific command and put the data into buf.
531 * Takes extra var. len. data and file descriptors as input to send to the
534 * Return size of data (only payload, not header) or a negative error code.
537 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
538 const int *fds
, size_t nb_fd
, const void *vardata
,
539 size_t vardata_len
, void **user_payload_buf
,
540 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
544 struct lttcomm_lttng_msg llm
;
546 ret
= connect_sessiond();
548 ret
= -LTTNG_ERR_NO_SESSIOND
;
551 sessiond_socket
= ret
;
555 ret
= send_session_msg(lsm
);
557 /* Ret value is a valid lttng error code. */
560 /* Send var len data */
561 ret
= send_session_varlen(vardata
, vardata_len
);
563 /* Ret value is a valid lttng error code. */
568 ret
= send_session_fds(fds
, nb_fd
);
570 /* Ret value is a valid lttng error code. */
574 /* Get header from data transmission */
575 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
577 /* Ret value is a valid lttng error code. */
581 /* Check error code if OK */
582 if (llm
.ret_code
!= LTTNG_OK
) {
587 /* Get command header from data transmission */
588 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
589 user_cmd_header_buf
, user_cmd_header_len
);
594 /* Get payload from data transmission */
595 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
604 disconnect_sessiond();
609 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
610 struct lttng_payload
*reply
)
613 struct lttcomm_lttng_msg llm
;
614 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
616 LTTNG_ASSERT(reply
->buffer
.size
== 0);
617 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
619 ret
= connect_sessiond();
621 ret
= -LTTNG_ERR_NO_SESSIOND
;
624 sessiond_socket
= ret
;
628 /* Send command to session daemon */
629 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
630 message
->buffer
.size
);
632 ret
= -LTTNG_ERR_FATAL
;
637 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
640 ret
= -LTTNG_ERR_FATAL
;
645 /* Get header from data transmission */
646 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
648 /* Ret value is a valid lttng error code. */
652 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
654 /* Check error code if OK */
655 if (llm
.ret_code
!= LTTNG_OK
) {
656 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
657 /* Invalid error code received. */
658 ret
= -LTTNG_ERR_UNK
;
665 if (llm
.cmd_header_size
> 0) {
666 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
672 /* Get command header from data transmission */
673 if (llm
.data_size
> 0) {
674 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
680 if (llm
.fd_count
> 0) {
681 ret
= lttcomm_recv_payload_fds_unix_sock(
682 sessiond_socket
, llm
.fd_count
, reply
);
688 /* Don't return the llm header to the caller. */
689 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
690 reply
->buffer
.size
- sizeof(llm
));
691 ret
= lttng_dynamic_buffer_set_size(
692 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
694 /* Can't happen as size is reduced. */
698 ret
= reply
->buffer
.size
;
701 disconnect_sessiond();
706 * Create lttng handle and return pointer.
708 * The returned pointer will be NULL in case of malloc() error.
710 struct lttng_handle
*lttng_create_handle(const char *session_name
,
711 struct lttng_domain
*domain
)
714 struct lttng_handle
*handle
= NULL
;
716 handle
= zmalloc(sizeof(struct lttng_handle
));
717 if (handle
== NULL
) {
718 PERROR("malloc handle");
722 /* Copy session name */
723 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
724 sizeof(handle
->session_name
));
729 /* Copy lttng domain or leave initialized to 0. */
731 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
742 * Destroy handle by free(3) the pointer.
744 void lttng_destroy_handle(struct lttng_handle
*handle
)
750 * Register an outside consumer.
752 * Returns size of returned session payload data or a negative error code.
754 int lttng_register_consumer(struct lttng_handle
*handle
,
755 const char *socket_path
)
758 struct lttcomm_session_msg lsm
;
760 if (handle
== NULL
|| socket_path
== NULL
) {
761 ret
= -LTTNG_ERR_INVALID
;
765 memset(&lsm
, 0, sizeof(lsm
));
766 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
767 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
768 sizeof(lsm
.session
.name
));
770 ret
= -LTTNG_ERR_INVALID
;
774 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
776 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
777 sizeof(lsm
.u
.reg
.path
));
779 ret
= -LTTNG_ERR_INVALID
;
783 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
789 * Start tracing for all traces of the session.
791 * Returns size of returned session payload data or a negative error code.
793 int lttng_start_tracing(const char *session_name
)
796 struct lttcomm_session_msg lsm
;
798 if (session_name
== NULL
) {
799 ret
= -LTTNG_ERR_INVALID
;
803 memset(&lsm
, 0, sizeof(lsm
));
804 lsm
.cmd_type
= LTTNG_START_TRACE
;
806 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
807 sizeof(lsm
.session
.name
));
809 ret
= -LTTNG_ERR_INVALID
;
813 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
819 * Stop tracing for all traces of the session.
821 static int _lttng_stop_tracing(const char *session_name
, int wait
)
824 struct lttcomm_session_msg lsm
;
826 if (session_name
== NULL
) {
827 ret
= -LTTNG_ERR_INVALID
;
831 memset(&lsm
, 0, sizeof(lsm
));
832 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
834 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
835 sizeof(lsm
.session
.name
));
837 ret
= -LTTNG_ERR_INVALID
;
841 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
842 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
850 /* Check for data availability */
852 data_ret
= lttng_data_pending(session_name
);
854 /* Return the data available call error. */
860 * Data sleep time before retrying (in usec). Don't sleep if the
861 * call returned value indicates availability.
864 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
866 } while (data_ret
!= 0);
874 * Stop tracing and wait for data availability.
876 int lttng_stop_tracing(const char *session_name
)
878 return _lttng_stop_tracing(session_name
, 1);
882 * Stop tracing but _don't_ wait for data availability.
884 int lttng_stop_tracing_no_wait(const char *session_name
)
886 return _lttng_stop_tracing(session_name
, 0);
890 * Add context to a channel.
892 * If the given channel is NULL, add the contexts to all channels.
893 * The event_name param is ignored.
895 * Returns the size of the returned payload data or a negative error code.
897 int lttng_add_context(struct lttng_handle
*handle
,
898 struct lttng_event_context
*ctx
, const char *event_name
,
899 const char *channel_name
)
904 struct lttcomm_session_msg lsm
;
906 /* Safety check. Both are mandatory. */
907 if (handle
== NULL
|| ctx
== NULL
) {
908 ret
= -LTTNG_ERR_INVALID
;
912 memset(&lsm
, 0, sizeof(lsm
));
913 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
915 /* If no channel name, send empty string. */
916 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
917 sizeof(lsm
.u
.context
.channel_name
));
919 ret
= -LTTNG_ERR_INVALID
;
923 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
924 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
925 sizeof(lsm
.session
.name
));
927 ret
= -LTTNG_ERR_INVALID
;
931 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
932 size_t provider_len
, ctx_len
;
933 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
934 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
936 if (!provider_name
|| !ctx_name
) {
937 ret
= -LTTNG_ERR_INVALID
;
941 provider_len
= strlen(provider_name
);
942 if (provider_len
== 0) {
943 ret
= -LTTNG_ERR_INVALID
;
946 lsm
.u
.context
.provider_name_len
= provider_len
;
948 ctx_len
= strlen(ctx_name
);
950 ret
= -LTTNG_ERR_INVALID
;
953 lsm
.u
.context
.context_name_len
= ctx_len
;
955 len
= provider_len
+ ctx_len
;
958 ret
= -LTTNG_ERR_NOMEM
;
962 memcpy(buf
, provider_name
, provider_len
);
963 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
965 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
967 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
969 * Don't leak application addresses to the sessiond.
970 * This is only necessary when ctx is for an app ctx otherwise
971 * the values inside the union (type & config) are overwritten.
973 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
974 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
977 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
984 * Enable event(s) for a channel.
986 * If no event name is specified, all events are enabled.
987 * If no channel name is specified, the default 'channel0' is used.
989 * Returns size of returned session payload data or a negative error code.
991 int lttng_enable_event(struct lttng_handle
*handle
,
992 struct lttng_event
*ev
, const char *channel_name
)
994 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
999 * Create or enable an event with a filter expression.
1001 * Return negative error value on error.
1002 * Return size of returned session payload data if OK.
1004 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1005 struct lttng_event
*event
, const char *channel_name
,
1006 const char *filter_expression
)
1008 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1009 filter_expression
, 0, NULL
);
1013 * Depending on the event, return a newly allocated agent filter expression or
1014 * NULL if not applicable.
1016 * An event with NO loglevel and the name is * will return NULL.
1018 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1021 char *agent_filter
= NULL
;
1025 /* Don't add filter for the '*' event. */
1026 if (strcmp(ev
->name
, "*") != 0) {
1028 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1031 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1039 /* Add loglevel filtering if any for the JUL domain. */
1040 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1043 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1049 if (filter
|| agent_filter
) {
1052 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1053 agent_filter
? agent_filter
: filter
, op
,
1058 agent_filter
= new_filter
;
1060 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1069 return agent_filter
;
1076 * Enable event(s) for a channel, possibly with exclusions and a filter.
1077 * If no event name is specified, all events are enabled.
1078 * If no channel name is specified, the default name is used.
1079 * If filter expression is not NULL, the filter is set for the event.
1080 * If exclusion count is not zero, the exclusions are set for the event.
1081 * Returns size of returned session payload data or a negative error code.
1083 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1084 struct lttng_event
*ev
, const char *channel_name
,
1085 const char *original_filter_expression
,
1086 int exclusion_count
, char **exclusion_list
)
1088 struct lttcomm_session_msg lsm
;
1089 struct lttng_payload payload
;
1091 unsigned int free_filter_expression
= 0;
1092 struct filter_parser_ctx
*ctx
= NULL
;
1095 * We have either a filter or some exclusions, so we need to set up
1096 * a variable-length payload from where to send the data.
1098 lttng_payload_init(&payload
);
1101 * Cast as non-const since we may replace the filter expression
1102 * by a dynamically allocated string. Otherwise, the original
1103 * string is not modified.
1105 char *filter_expression
= (char *) original_filter_expression
;
1107 if (handle
== NULL
|| ev
== NULL
) {
1108 ret
= -LTTNG_ERR_INVALID
;
1113 * Empty filter string will always be rejected by the parser
1114 * anyway, so treat this corner-case early to eliminate
1115 * lttng_fmemopen error for 0-byte allocation.
1117 if (filter_expression
&& filter_expression
[0] == '\0') {
1118 ret
= -LTTNG_ERR_INVALID
;
1122 memset(&lsm
, 0, sizeof(lsm
));
1124 /* If no channel name, send empty string. */
1125 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1126 sizeof(lsm
.u
.enable
.channel_name
));
1128 ret
= -LTTNG_ERR_INVALID
;
1132 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1133 if (ev
->name
[0] == '\0') {
1134 /* Enable all events. */
1135 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1136 LTTNG_ASSERT(ret
== 0);
1139 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1140 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1142 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1143 sizeof(lsm
.session
.name
));
1145 ret
= -LTTNG_ERR_INVALID
;
1149 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1150 lsm
.u
.enable
.bytecode_len
= 0;
1152 /* Parse filter expression. */
1153 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1154 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1155 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1156 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1157 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1158 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1161 /* Setup JUL filter if needed. */
1162 agent_filter
= set_agent_filter(filter_expression
, ev
);
1163 if (!agent_filter
) {
1164 if (!filter_expression
) {
1166 * No JUL and no filter, just skip
1173 * With an agent filter, the original filter has
1174 * been added to it thus replace the filter
1177 filter_expression
= agent_filter
;
1178 free_filter_expression
= 1;
1182 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1187 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1188 + bytecode_get_len(&ctx
->bytecode
->b
);
1189 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1192 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1193 lsm
.u
.enable
.bytecode_len
+
1194 lsm
.u
.enable
.expression_len
+
1195 LTTNG_SYMBOL_NAME_LEN
*
1198 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1202 /* Put exclusion names first in the data. */
1203 for (i
= 0; i
< exclusion_count
; i
++) {
1204 size_t exclusion_len
;
1206 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1207 LTTNG_SYMBOL_NAME_LEN
);
1208 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1209 /* Exclusion is not NULL-terminated. */
1210 ret
= -LTTNG_ERR_INVALID
;
1214 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1215 exclusion_list
[i
], exclusion_len
);
1221 * Padding the rest of the entry with zeros. Every exclusion
1222 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1224 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1225 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1231 /* Add filter expression next. */
1232 if (filter_expression
) {
1233 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1234 filter_expression
, lsm
.u
.enable
.expression_len
);
1239 /* Add filter bytecode next. */
1240 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1241 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1242 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1247 if (ev
->extended
.ptr
) {
1248 struct lttng_event_extended
*ev_ext
=
1249 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1251 if (ev_ext
->probe_location
) {
1253 * lttng_userspace_probe_location_serialize returns the
1254 * number of bytes that was appended to the buffer.
1256 ret
= lttng_userspace_probe_location_serialize(
1257 ev_ext
->probe_location
, &payload
);
1263 * Set the size of the userspace probe location element
1264 * of the buffer so that the receiving side knows where
1267 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1272 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1274 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1281 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1282 if (fd_count
== 1) {
1283 struct fd_handle
*h
=
1284 lttng_payload_view_pop_fd_handle(&view
);
1290 fd_to_send
= fd_handle_get_fd(h
);
1294 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1295 fd_count
? &fd_to_send
: NULL
, fd_count
,
1296 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1297 view
.buffer
.size
, NULL
, NULL
, 0);
1301 if (filter_expression
&& ctx
) {
1302 filter_bytecode_free(ctx
);
1303 filter_ir_free(ctx
);
1304 filter_parser_ctx_free(ctx
);
1307 if (free_filter_expression
) {
1309 * The filter expression has been replaced and must be freed as
1310 * it is not the original filter expression received as a
1313 free(filter_expression
);
1317 * Return directly to the caller and don't ask the sessiond since
1318 * something went wrong in the parsing of data above.
1320 lttng_payload_reset(&payload
);
1324 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1328 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1329 struct lttng_event
*ev
, const char *channel_name
,
1330 const char *original_filter_expression
)
1332 struct lttcomm_session_msg lsm
;
1335 unsigned int free_filter_expression
= 0;
1336 struct filter_parser_ctx
*ctx
= NULL
;
1338 * Cast as non-const since we may replace the filter expression
1339 * by a dynamically allocated string. Otherwise, the original
1340 * string is not modified.
1342 char *filter_expression
= (char *) original_filter_expression
;
1344 if (handle
== NULL
|| ev
== NULL
) {
1345 ret
= -LTTNG_ERR_INVALID
;
1350 * Empty filter string will always be rejected by the parser
1351 * anyway, so treat this corner-case early to eliminate
1352 * lttng_fmemopen error for 0-byte allocation.
1354 if (filter_expression
&& filter_expression
[0] == '\0') {
1355 ret
= -LTTNG_ERR_INVALID
;
1359 memset(&lsm
, 0, sizeof(lsm
));
1361 /* If no channel name, send empty string. */
1362 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1363 sizeof(lsm
.u
.disable
.channel_name
));
1365 ret
= -LTTNG_ERR_INVALID
;
1369 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1371 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1372 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1374 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1375 sizeof(lsm
.session
.name
));
1377 ret
= -LTTNG_ERR_INVALID
;
1381 lsm
.u
.disable
.bytecode_len
= 0;
1384 * For the JUL domain, a filter is enforced except for the
1385 * disable all event. This is done to avoid having the event in
1386 * all sessions thus filtering by logger name.
1388 if (filter_expression
== NULL
&&
1389 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1390 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1391 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1396 * We have a filter, so we need to set up a variable-length
1397 * memory block from where to send the data.
1400 /* Parse filter expression */
1401 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1402 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1403 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1404 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1405 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1406 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1409 /* Setup JUL filter if needed. */
1410 agent_filter
= set_agent_filter(filter_expression
, ev
);
1411 if (!agent_filter
) {
1412 if (!filter_expression
) {
1414 * No JUL and no filter, just skip
1421 * With a JUL filter, the original filter has
1422 * been added to it thus replace the filter
1425 filter_expression
= agent_filter
;
1426 free_filter_expression
= 1;
1430 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1435 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1436 + bytecode_get_len(&ctx
->bytecode
->b
);
1437 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1440 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1441 + lsm
.u
.disable
.expression_len
);
1443 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1447 /* Add filter expression. */
1448 if (lsm
.u
.disable
.expression_len
!= 0) {
1451 lsm
.u
.disable
.expression_len
);
1453 /* Add filter bytecode next. */
1454 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1456 + lsm
.u
.disable
.expression_len
,
1458 lsm
.u
.disable
.bytecode_len
);
1461 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1462 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1466 if (filter_expression
&& ctx
) {
1467 filter_bytecode_free(ctx
);
1468 filter_ir_free(ctx
);
1469 filter_parser_ctx_free(ctx
);
1472 if (free_filter_expression
) {
1474 * The filter expression has been replaced and must be freed as
1475 * it is not the original filter expression received as a
1478 free(filter_expression
);
1482 * Return directly to the caller and don't ask the sessiond since
1483 * something went wrong in the parsing of data above.
1488 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1493 * Disable event(s) of a channel and domain.
1494 * If no event name is specified, all events are disabled.
1495 * If no channel name is specified, the default 'channel0' is used.
1496 * Returns size of returned session payload data or a negative error code.
1498 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1499 const char *channel_name
)
1502 struct lttng_event ev
;
1504 memset(&ev
, 0, sizeof(ev
));
1506 ev
.type
= LTTNG_EVENT_ALL
;
1507 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1509 ret
= -LTTNG_ERR_INVALID
;
1513 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1518 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1520 struct lttng_channel
*channel
= NULL
;
1521 struct lttng_channel_extended
*extended
= NULL
;
1527 /* Validate domain. */
1528 switch (domain
->type
) {
1529 case LTTNG_DOMAIN_UST
:
1530 switch (domain
->buf_type
) {
1531 case LTTNG_BUFFER_PER_UID
:
1532 case LTTNG_BUFFER_PER_PID
:
1538 case LTTNG_DOMAIN_KERNEL
:
1539 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1547 channel
= zmalloc(sizeof(*channel
));
1552 extended
= zmalloc(sizeof(*extended
));
1557 channel
->attr
.extended
.ptr
= extended
;
1559 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1567 void lttng_channel_destroy(struct lttng_channel
*channel
)
1573 if (channel
->attr
.extended
.ptr
) {
1574 free(channel
->attr
.extended
.ptr
);
1580 * Enable channel per domain
1581 * Returns size of returned session payload data or a negative error code.
1583 int lttng_enable_channel(struct lttng_handle
*handle
,
1584 struct lttng_channel
*in_chan
)
1587 struct lttcomm_session_msg lsm
;
1588 size_t total_buffer_size_needed_per_cpu
= 0;
1590 /* NULL arguments are forbidden. No default values. */
1591 if (handle
== NULL
|| in_chan
== NULL
) {
1592 return -LTTNG_ERR_INVALID
;
1595 memset(&lsm
, 0, sizeof(lsm
));
1596 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1597 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1599 if (!in_chan
->attr
.extended
.ptr
) {
1600 struct lttng_channel
*channel
;
1601 struct lttng_channel_extended
*extended
;
1603 channel
= lttng_channel_create(&handle
->domain
);
1605 return -LTTNG_ERR_NOMEM
;
1609 * Create a new channel in order to use default extended
1612 extended
= (struct lttng_channel_extended
*)
1613 channel
->attr
.extended
.ptr
;
1614 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1615 lttng_channel_destroy(channel
);
1617 struct lttng_channel_extended
*extended
;
1619 extended
= (struct lttng_channel_extended
*)
1620 in_chan
->attr
.extended
.ptr
;
1621 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1625 * Verify that the amount of memory required to create the requested
1626 * buffer is available on the system at the moment.
1628 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1629 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1630 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1631 return -LTTNG_ERR_NOMEM
;
1634 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1635 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1637 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1638 sizeof(lsm
.session
.name
));
1640 ret
= -LTTNG_ERR_INVALID
;
1644 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1650 * All tracing will be stopped for registered events of the channel.
1651 * Returns size of returned session payload data or a negative error code.
1653 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1656 struct lttcomm_session_msg lsm
;
1658 /* Safety check. Both are mandatory. */
1659 if (handle
== NULL
|| name
== NULL
) {
1660 return -LTTNG_ERR_INVALID
;
1663 memset(&lsm
, 0, sizeof(lsm
));
1665 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1667 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1668 sizeof(lsm
.u
.disable
.channel_name
));
1670 ret
= -LTTNG_ERR_INVALID
;
1674 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1676 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1677 sizeof(lsm
.session
.name
));
1679 ret
= -LTTNG_ERR_INVALID
;
1683 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1689 * Lists all available tracepoints of domain.
1690 * Sets the contents of the events array.
1691 * Returns the number of lttng_event entries in events;
1692 * on error, returns a negative value.
1694 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1695 struct lttng_event
**events
)
1698 struct lttcomm_session_msg lsm
;
1700 if (handle
== NULL
) {
1701 return -LTTNG_ERR_INVALID
;
1704 memset(&lsm
, 0, sizeof(lsm
));
1705 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1706 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1708 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1713 return ret
/ sizeof(struct lttng_event
);
1717 * Lists all available tracepoint fields of domain.
1718 * Sets the contents of the event field array.
1719 * Returns the number of lttng_event_field entries in events;
1720 * on error, returns a negative value.
1722 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1723 struct lttng_event_field
**fields
)
1726 struct lttcomm_session_msg lsm
;
1728 if (handle
== NULL
) {
1729 return -LTTNG_ERR_INVALID
;
1732 memset(&lsm
, 0, sizeof(lsm
));
1733 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1734 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1736 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1741 return ret
/ sizeof(struct lttng_event_field
);
1745 * Lists all available kernel system calls. Allocates and sets the contents of
1748 * Returns the number of lttng_event entries in events; on error, returns a
1751 int lttng_list_syscalls(struct lttng_event
**events
)
1754 struct lttcomm_session_msg lsm
;
1757 return -LTTNG_ERR_INVALID
;
1760 memset(&lsm
, 0, sizeof(lsm
));
1761 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1762 /* Force kernel domain for system calls. */
1763 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1765 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1770 return ret
/ sizeof(struct lttng_event
);
1774 * Returns a human readable string describing
1775 * the error code (a negative value).
1777 const char *lttng_strerror(int code
)
1779 return error_get_str(code
);
1782 enum lttng_error_code
lttng_create_session_ext(
1783 struct lttng_session_descriptor
*session_descriptor
)
1785 enum lttng_error_code ret_code
;
1786 struct lttcomm_session_msg lsm
= {
1787 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1790 struct lttng_buffer_view reply_view
;
1792 bool sessiond_must_generate_ouput
;
1793 struct lttng_dynamic_buffer payload
;
1795 size_t descriptor_size
;
1796 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1798 lttng_dynamic_buffer_init(&payload
);
1799 if (!session_descriptor
) {
1800 ret_code
= LTTNG_ERR_INVALID
;
1804 sessiond_must_generate_ouput
=
1805 !lttng_session_descriptor_is_output_destination_initialized(
1806 session_descriptor
);
1807 if (sessiond_must_generate_ouput
) {
1808 const char *home_dir
= utils_get_home_dir();
1809 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1811 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1812 ret_code
= LTTNG_ERR_FATAL
;
1816 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1817 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1820 ret_code
= LTTNG_ERR_NOMEM
;
1825 descriptor_size
= payload
.size
;
1826 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1829 ret_code
= LTTNG_ERR_INVALID
;
1832 descriptor_size
= payload
.size
- descriptor_size
;
1833 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1835 /* Command returns a session descriptor on success. */
1836 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1837 payload
.size
, &reply
);
1838 if (reply_ret
< 0) {
1839 ret_code
= -reply_ret
;
1841 } else if (reply_ret
== 0) {
1842 /* Socket unexpectedly closed by the session daemon. */
1843 ret_code
= LTTNG_ERR_FATAL
;
1847 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1848 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1851 ret_code
= LTTNG_ERR_FATAL
;
1854 ret_code
= LTTNG_OK
;
1855 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1858 lttng_dynamic_buffer_reset(&payload
);
1859 lttng_session_descriptor_destroy(descriptor_reply
);
1864 * Create a new session using name and url for destination.
1866 * Return 0 on success else a negative LTTng error code.
1868 int lttng_create_session(const char *name
, const char *url
)
1872 struct lttng_uri
*uris
= NULL
;
1873 struct lttng_session_descriptor
*descriptor
= NULL
;
1874 enum lttng_error_code ret_code
;
1877 ret
= -LTTNG_ERR_INVALID
;
1881 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1883 ret
= -LTTNG_ERR_INVALID
;
1888 descriptor
= lttng_session_descriptor_create(name
);
1891 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1892 ret
= -LTTNG_ERR_INVALID
;
1895 descriptor
= lttng_session_descriptor_local_create(name
,
1899 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1903 ret
= -LTTNG_ERR_INVALID
;
1907 ret
= -LTTNG_ERR_INVALID
;
1910 ret_code
= lttng_create_session_ext(descriptor
);
1911 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1913 lttng_session_descriptor_destroy(descriptor
);
1919 * Create a session exclusively used for snapshot.
1921 * Return 0 on success else a negative LTTng error code.
1923 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1926 enum lttng_error_code ret_code
;
1928 struct lttng_uri
*uris
= NULL
;
1929 struct lttng_session_descriptor
*descriptor
= NULL
;
1932 ret
= -LTTNG_ERR_INVALID
;
1936 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1938 ret
= -LTTNG_ERR_INVALID
;
1942 * If the user does not specify a custom subdir, use the session name.
1944 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1945 strlen(uris
[0].subdir
) == 0) {
1946 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1949 PERROR("Failed to set session name as network destination sub-directory");
1950 ret
= -LTTNG_ERR_FATAL
;
1952 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1953 /* Truncated output. */
1954 ret
= -LTTNG_ERR_INVALID
;
1961 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1964 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1965 ret
= -LTTNG_ERR_INVALID
;
1968 descriptor
= lttng_session_descriptor_snapshot_local_create(
1973 descriptor
= lttng_session_descriptor_snapshot_network_create(
1979 ret
= -LTTNG_ERR_INVALID
;
1983 ret
= -LTTNG_ERR_INVALID
;
1986 ret_code
= lttng_create_session_ext(descriptor
);
1987 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1989 lttng_session_descriptor_destroy(descriptor
);
1995 * Create a session exclusively used for live.
1997 * Return 0 on success else a negative LTTng error code.
1999 int lttng_create_session_live(const char *name
, const char *url
,
2000 unsigned int timer_interval
)
2003 enum lttng_error_code ret_code
;
2004 struct lttng_session_descriptor
*descriptor
= NULL
;
2007 ret
= -LTTNG_ERR_INVALID
;
2012 descriptor
= lttng_session_descriptor_live_network_create(
2013 name
, url
, NULL
, timer_interval
);
2015 descriptor
= lttng_session_descriptor_live_create(
2016 name
, timer_interval
);
2019 ret
= -LTTNG_ERR_INVALID
;
2022 ret_code
= lttng_create_session_ext(descriptor
);
2023 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2025 lttng_session_descriptor_destroy(descriptor
);
2030 * Stop the session and wait for the data before destroying it
2032 * Return 0 on success else a negative LTTng error code.
2034 int lttng_destroy_session(const char *session_name
)
2037 enum lttng_error_code ret_code
;
2038 enum lttng_destruction_handle_status status
;
2039 struct lttng_destruction_handle
*handle
= NULL
;
2042 * Stop the tracing and wait for the data to be
2045 ret
= _lttng_stop_tracing(session_name
, 1);
2046 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2050 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2051 if (ret_code
!= LTTNG_OK
) {
2052 ret
= (int) -ret_code
;
2055 LTTNG_ASSERT(handle
);
2057 /* Block until the completion of the destruction of the session. */
2058 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2059 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2060 ret
= -LTTNG_ERR_UNK
;
2064 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2065 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2066 ret
= -LTTNG_ERR_UNK
;
2069 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2071 lttng_destruction_handle_destroy(handle
);
2076 * Destroy the session without waiting for the data.
2078 int lttng_destroy_session_no_wait(const char *session_name
)
2080 enum lttng_error_code ret_code
;
2082 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2083 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2087 * Ask the session daemon for all available sessions.
2088 * Sets the contents of the sessions array.
2089 * Returns the number of lttng_session entries in sessions;
2090 * on error, returns a negative value.
2092 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2095 struct lttcomm_session_msg lsm
;
2096 const size_t session_size
= sizeof(struct lttng_session
) +
2097 sizeof(struct lttng_session_extended
);
2098 size_t session_count
, i
;
2099 struct lttng_session_extended
*sessions_extended_begin
;
2100 struct lttng_session
*sessions
= NULL
;
2102 memset(&lsm
, 0, sizeof(lsm
));
2103 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2104 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2109 ret
= -LTTNG_ERR_FATAL
;
2113 if (ret
% session_size
) {
2114 ret
= -LTTNG_ERR_UNK
;
2116 *out_sessions
= NULL
;
2119 session_count
= (size_t) ret
/ session_size
;
2120 sessions_extended_begin
= (struct lttng_session_extended
*)
2121 (&sessions
[session_count
]);
2123 /* Set extended session info pointers. */
2124 for (i
= 0; i
< session_count
; i
++) {
2125 struct lttng_session
*session
= &sessions
[i
];
2126 struct lttng_session_extended
*extended
=
2127 &(sessions_extended_begin
[i
]);
2129 session
->extended
.ptr
= extended
;
2132 ret
= (int) session_count
;
2133 *out_sessions
= sessions
;
2138 enum lttng_error_code
lttng_session_get_creation_time(
2139 const struct lttng_session
*session
, uint64_t *creation_time
)
2141 enum lttng_error_code ret
= LTTNG_OK
;
2142 struct lttng_session_extended
*extended
;
2144 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2145 ret
= LTTNG_ERR_INVALID
;
2149 extended
= session
->extended
.ptr
;
2150 if (!extended
->creation_time
.is_set
) {
2151 /* Not created on the session daemon yet. */
2152 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2155 *creation_time
= extended
->creation_time
.value
;
2160 int lttng_set_session_shm_path(const char *session_name
,
2161 const char *shm_path
)
2164 struct lttcomm_session_msg lsm
;
2166 if (session_name
== NULL
) {
2167 return -LTTNG_ERR_INVALID
;
2170 memset(&lsm
, 0, sizeof(lsm
));
2171 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2173 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2174 sizeof(lsm
.session
.name
));
2176 ret
= -LTTNG_ERR_INVALID
;
2180 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2181 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2183 ret
= -LTTNG_ERR_INVALID
;
2187 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2193 * Ask the session daemon for all available domains of a session.
2194 * Sets the contents of the domains array.
2195 * Returns the number of lttng_domain entries in domains;
2196 * on error, returns a negative value.
2198 int lttng_list_domains(const char *session_name
,
2199 struct lttng_domain
**domains
)
2202 struct lttcomm_session_msg lsm
;
2204 if (session_name
== NULL
) {
2205 ret
= -LTTNG_ERR_INVALID
;
2209 memset(&lsm
, 0, sizeof(lsm
));
2210 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2212 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2213 sizeof(lsm
.session
.name
));
2215 ret
= -LTTNG_ERR_INVALID
;
2219 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2224 return ret
/ sizeof(struct lttng_domain
);
2230 * Ask the session daemon for all available channels of a session.
2231 * Sets the contents of the channels array.
2232 * Returns the number of lttng_channel entries in channels;
2233 * on error, returns a negative value.
2235 int lttng_list_channels(struct lttng_handle
*handle
,
2236 struct lttng_channel
**channels
)
2239 size_t channel_count
, i
;
2240 const size_t channel_size
= sizeof(struct lttng_channel
) +
2241 sizeof(struct lttng_channel_extended
);
2242 struct lttcomm_session_msg lsm
;
2245 if (handle
== NULL
) {
2246 ret
= -LTTNG_ERR_INVALID
;
2250 memset(&lsm
, 0, sizeof(lsm
));
2251 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2252 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2253 sizeof(lsm
.session
.name
));
2255 ret
= -LTTNG_ERR_INVALID
;
2259 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2261 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2266 if (ret
% channel_size
) {
2267 ret
= -LTTNG_ERR_UNK
;
2272 channel_count
= (size_t) ret
/ channel_size
;
2274 /* Set extended info pointers */
2275 extended_at
= ((void *) *channels
) +
2276 channel_count
* sizeof(struct lttng_channel
);
2277 for (i
= 0; i
< channel_count
; i
++) {
2278 struct lttng_channel
*chan
= &(*channels
)[i
];
2280 chan
->attr
.extended
.ptr
= extended_at
;
2281 extended_at
+= sizeof(struct lttng_channel_extended
);
2284 ret
= (int) channel_count
;
2290 * Ask the session daemon for all available events of a session channel.
2291 * Sets the contents of the events array.
2292 * Returns the number of lttng_event entries in events;
2293 * on error, returns a negative value.
2295 int lttng_list_events(struct lttng_handle
*handle
,
2296 const char *channel_name
, struct lttng_event
**events
)
2299 struct lttcomm_session_msg lsm
= {};
2300 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2301 uint32_t nb_events
, i
;
2302 const void *comm_ext_at
;
2303 struct lttng_dynamic_buffer listing
;
2305 struct lttng_payload payload
;
2306 struct lttng_payload payload_copy
;
2307 struct lttng_payload_view lsm_view
=
2308 lttng_payload_view_init_from_buffer(
2309 (const char *) &lsm
, 0, sizeof(lsm
));
2310 struct lttng_buffer_view cmd_header_view
;
2311 struct lttng_buffer_view cmd_payload_view
;
2312 struct lttng_buffer_view flat_events_view
;
2313 struct lttng_buffer_view ext_view
;
2315 /* Safety check. An handle and channel name are mandatory */
2316 if (handle
== NULL
|| channel_name
== NULL
) {
2317 ret
= -LTTNG_ERR_INVALID
;
2321 lttng_payload_init(&payload
);
2322 lttng_payload_init(&payload_copy
);
2324 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2325 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2326 sizeof(lsm
.session
.name
));
2328 ret
= -LTTNG_ERR_INVALID
;
2332 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2333 sizeof(lsm
.u
.list
.channel_name
));
2335 ret
= -LTTNG_ERR_INVALID
;
2339 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2341 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2347 * A copy of the payload is performed since it will be
2348 * consumed twice. Consuming the same payload twice is invalid since
2349 * it will cause any received file descriptor to become "shared"
2350 * between different instances of the resulting objects.
2352 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2354 ret
= -LTTNG_ERR_NOMEM
;
2358 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2359 &payload
.buffer
, 0, sizeof(*cmd_header
));
2360 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2361 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2365 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2367 /* Set number of events and free command header */
2368 nb_events
= cmd_header
->nb_events
;
2369 if (nb_events
> INT_MAX
) {
2370 ret
= -LTTNG_ERR_OVERFLOW
;
2374 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2375 &payload
.buffer
, sizeof(*cmd_header
), -1);
2378 * The buffer that is returned must contain a "flat" version of
2379 * the events that are returned. In other words, all pointers
2380 * within an lttng_event must point to a location within the returned
2381 * buffer so that the user may free everything by simply calling free()
2382 * on the returned buffer. This is needed in order to maintain API
2385 * A first pass is performed to compute the size of the buffer that
2386 * must be allocated. A second pass is then performed to setup
2387 * the returned events so that their members always point within the
2390 * The layout of the returned buffer is as follows:
2391 * - struct lttng_event[nb_events],
2392 * - nb_events times the following:
2393 * - struct lttng_event_extended,
2394 * - flattened version of userspace_probe_location
2395 * - filter_expression
2397 * - padding to align to 64-bits
2399 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2400 nb_events
* sizeof(struct lttng_event
), -1);
2401 comm_ext_at
= ext_view
.data
;
2402 storage_req
= nb_events
* sizeof(struct lttng_event
);
2404 struct lttng_payload_view payload_view
=
2405 lttng_payload_view_from_payload(&payload
, 0, -1);
2407 for (i
= 0; i
< nb_events
; i
++) {
2408 const struct lttcomm_event_extended_header
*ext_comm
=
2409 (struct lttcomm_event_extended_header
*)
2411 int probe_storage_req
= 0;
2413 comm_ext_at
+= sizeof(*ext_comm
);
2414 comm_ext_at
+= ext_comm
->filter_len
;
2415 comm_ext_at
+= ext_comm
->nb_exclusions
*
2416 LTTNG_SYMBOL_NAME_LEN
;
2418 if (ext_comm
->userspace_probe_location_len
) {
2419 struct lttng_userspace_probe_location
2420 *probe_location
= NULL
;
2421 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2423 (const char *) comm_ext_at
-
2424 payload_view
.buffer
.data
,
2425 ext_comm
->userspace_probe_location_len
);
2427 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2428 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2433 * Create a temporary userspace probe location
2434 * to determine the size needed by a "flattened"
2435 * version of that same probe location.
2437 ret
= lttng_userspace_probe_location_create_from_payload(
2438 &probe_location_view
,
2441 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2445 ret
= lttng_userspace_probe_location_flatten(
2446 probe_location
, NULL
);
2447 lttng_userspace_probe_location_destroy(
2450 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2454 probe_storage_req
= ret
;
2455 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2458 storage_req
+= sizeof(struct lttng_event_extended
);
2459 storage_req
+= ext_comm
->filter_len
;
2460 storage_req
+= ext_comm
->nb_exclusions
*
2461 LTTNG_SYMBOL_NAME_LEN
;
2462 /* Padding to ensure the flat probe is aligned. */
2463 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2464 storage_req
+= probe_storage_req
;
2468 lttng_dynamic_buffer_init(&listing
);
2470 * We must ensure that "listing" is never resized so as to preserve
2471 * the validity of the flattened objects.
2473 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2475 ret
= -LTTNG_ERR_NOMEM
;
2479 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2480 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2481 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2482 nb_events
* sizeof(struct lttng_event
));
2483 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2485 ret
= -LTTNG_ERR_NOMEM
;
2486 goto free_dynamic_buffer
;
2489 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2490 nb_events
* sizeof(struct lttng_event
), -1);
2491 comm_ext_at
= ext_view
.data
;
2494 struct lttng_payload_view payload_copy_view
=
2495 lttng_payload_view_from_payload(
2496 &payload_copy
, 0, -1);
2498 for (i
= 0; i
< nb_events
; i
++) {
2499 struct lttng_event
*event
= (typeof(event
))(
2501 (sizeof(struct lttng_event
) * i
));
2502 const struct lttcomm_event_extended_header
*ext_comm
=
2503 (typeof(ext_comm
)) comm_ext_at
;
2504 struct lttng_event_extended
*event_extended
=
2505 (typeof(event_extended
))(listing
.data
+
2508 /* Insert struct lttng_event_extended. */
2509 ret
= lttng_dynamic_buffer_set_size(&listing
,
2510 listing
.size
+ sizeof(*event_extended
));
2512 ret
= -LTTNG_ERR_NOMEM
;
2513 goto free_dynamic_buffer
;
2515 event
->extended
.ptr
= event_extended
;
2517 comm_ext_at
+= sizeof(*ext_comm
);
2519 /* Insert filter expression. */
2520 if (ext_comm
->filter_len
) {
2521 event_extended
->filter_expression
=
2522 listing
.data
+ listing
.size
;
2523 ret
= lttng_dynamic_buffer_append(&listing
,
2525 ext_comm
->filter_len
);
2527 ret
= -LTTNG_ERR_NOMEM
;
2528 goto free_dynamic_buffer
;
2530 comm_ext_at
+= ext_comm
->filter_len
;
2533 /* Insert exclusions. */
2534 if (ext_comm
->nb_exclusions
) {
2535 event_extended
->exclusions
.count
=
2536 ext_comm
->nb_exclusions
;
2537 event_extended
->exclusions
.strings
=
2538 listing
.data
+ listing
.size
;
2540 ret
= lttng_dynamic_buffer_append(&listing
,
2542 ext_comm
->nb_exclusions
*
2543 LTTNG_SYMBOL_NAME_LEN
);
2545 ret
= -LTTNG_ERR_NOMEM
;
2546 goto free_dynamic_buffer
;
2548 comm_ext_at
+= ext_comm
->nb_exclusions
*
2549 LTTNG_SYMBOL_NAME_LEN
;
2552 /* Insert padding to align to 64-bits. */
2553 ret
= lttng_dynamic_buffer_set_size(&listing
,
2554 ALIGN_TO(listing
.size
,
2557 ret
= -LTTNG_ERR_NOMEM
;
2558 goto free_dynamic_buffer
;
2561 /* Insert flattened userspace probe location. */
2562 if (ext_comm
->userspace_probe_location_len
) {
2563 struct lttng_userspace_probe_location
2564 *probe_location
= NULL
;
2565 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2567 (const char *) comm_ext_at
-
2568 payload_copy_view
.buffer
.data
,
2569 ext_comm
->userspace_probe_location_len
);
2571 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2572 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2573 goto free_dynamic_buffer
;
2576 ret
= lttng_userspace_probe_location_create_from_payload(
2577 &probe_location_view
,
2580 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2581 goto free_dynamic_buffer
;
2584 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2587 ret
= lttng_userspace_probe_location_flatten(
2588 probe_location
, &listing
);
2589 lttng_userspace_probe_location_destroy(
2592 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2593 goto free_dynamic_buffer
;
2596 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2601 /* Don't reset listing buffer as we return its content. */
2602 *events
= (struct lttng_event
*) listing
.data
;
2603 lttng_dynamic_buffer_init(&listing
);
2604 ret
= (int) nb_events
;
2605 free_dynamic_buffer
:
2606 lttng_dynamic_buffer_reset(&listing
);
2608 lttng_payload_reset(&payload
);
2609 lttng_payload_reset(&payload_copy
);
2614 * Sets the tracing_group variable with name.
2615 * This function allocates memory pointed to by tracing_group.
2616 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2618 int lttng_set_tracing_group(const char *name
)
2621 return -LTTNG_ERR_INVALID
;
2624 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2625 return -LTTNG_ERR_FATAL
;
2631 int lttng_calibrate(struct lttng_handle
*handle
,
2632 struct lttng_calibrate
*calibrate
)
2635 * This command was removed in LTTng 2.9.
2637 return -LTTNG_ERR_UND
;
2641 * Set default channel attributes.
2642 * If either or both of the arguments are null, attr content is zeroe'd.
2644 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2645 struct lttng_channel_attr
*attr
)
2647 struct lttng_channel_extended
*extended
;
2650 if (attr
== NULL
|| domain
== NULL
) {
2654 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2655 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2657 /* Same for all domains. */
2658 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2659 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2660 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2662 switch (domain
->type
) {
2663 case LTTNG_DOMAIN_KERNEL
:
2664 attr
->switch_timer_interval
=
2665 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2666 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2667 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2668 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2669 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2671 extended
->monitor_timer_interval
=
2672 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2673 extended
->blocking_timeout
=
2674 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2677 case LTTNG_DOMAIN_UST
:
2678 switch (domain
->buf_type
) {
2679 case LTTNG_BUFFER_PER_UID
:
2680 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2681 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2682 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2683 attr
->switch_timer_interval
=
2684 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2685 attr
->read_timer_interval
=
2686 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2688 extended
->monitor_timer_interval
=
2689 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2690 extended
->blocking_timeout
=
2691 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2694 case LTTNG_BUFFER_PER_PID
:
2696 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2697 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2698 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2699 attr
->switch_timer_interval
=
2700 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2701 attr
->read_timer_interval
=
2702 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2704 extended
->monitor_timer_interval
=
2705 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2706 extended
->blocking_timeout
=
2707 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2712 /* Default behavior: leave set to 0. */
2716 attr
->extended
.ptr
= extended
;
2719 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2720 uint64_t *discarded_events
)
2723 struct lttng_channel_extended
*chan_ext
;
2725 if (!channel
|| !discarded_events
) {
2726 ret
= -LTTNG_ERR_INVALID
;
2730 chan_ext
= channel
->attr
.extended
.ptr
;
2733 * This can happen since the lttng_channel structure is
2734 * used for other tasks where this pointer is never set.
2736 *discarded_events
= 0;
2740 *discarded_events
= chan_ext
->discarded_events
;
2745 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2746 uint64_t *lost_packets
)
2749 struct lttng_channel_extended
*chan_ext
;
2751 if (!channel
|| !lost_packets
) {
2752 ret
= -LTTNG_ERR_INVALID
;
2756 chan_ext
= channel
->attr
.extended
.ptr
;
2759 * This can happen since the lttng_channel structure is
2760 * used for other tasks where this pointer is never set.
2766 *lost_packets
= chan_ext
->lost_packets
;
2771 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2772 uint64_t *monitor_timer_interval
)
2776 if (!chan
|| !monitor_timer_interval
) {
2777 ret
= -LTTNG_ERR_INVALID
;
2781 if (!chan
->attr
.extended
.ptr
) {
2782 ret
= -LTTNG_ERR_INVALID
;
2786 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2787 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2792 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2793 uint64_t monitor_timer_interval
)
2797 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2798 ret
= -LTTNG_ERR_INVALID
;
2802 ((struct lttng_channel_extended
*)
2803 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2804 monitor_timer_interval
;
2809 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2810 int64_t *blocking_timeout
)
2814 if (!chan
|| !blocking_timeout
) {
2815 ret
= -LTTNG_ERR_INVALID
;
2819 if (!chan
->attr
.extended
.ptr
) {
2820 ret
= -LTTNG_ERR_INVALID
;
2824 *blocking_timeout
= ((struct lttng_channel_extended
*)
2825 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2830 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2831 int64_t blocking_timeout
)
2834 int64_t msec_timeout
;
2836 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2837 ret
= -LTTNG_ERR_INVALID
;
2841 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2842 ret
= -LTTNG_ERR_INVALID
;
2847 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2848 * us to accept a narrower range of values (msecs expressed as a signed
2851 msec_timeout
= blocking_timeout
/ 1000;
2852 if (msec_timeout
!= (int32_t) msec_timeout
) {
2853 ret
= -LTTNG_ERR_INVALID
;
2857 ((struct lttng_channel_extended
*)
2858 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2865 * Check if session daemon is alive.
2867 * Return 1 if alive or 0 if not.
2868 * On error returns a negative value.
2870 int lttng_session_daemon_alive(void)
2874 ret
= set_session_daemon_path();
2880 if (*sessiond_sock_path
== '\0') {
2882 * No socket path set. Weird error which means the constructor
2888 ret
= try_connect_sessiond(sessiond_sock_path
);
2899 * Set URL for a consumer for a session and domain.
2901 * Return 0 on success, else a negative value.
2903 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2904 const char *control_url
, const char *data_url
)
2908 struct lttcomm_session_msg lsm
;
2909 struct lttng_uri
*uris
= NULL
;
2911 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2912 ret
= -LTTNG_ERR_INVALID
;
2916 memset(&lsm
, 0, sizeof(lsm
));
2918 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2920 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2921 sizeof(lsm
.session
.name
));
2923 ret
= -LTTNG_ERR_INVALID
;
2927 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2929 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2931 ret
= -LTTNG_ERR_INVALID
;
2935 lsm
.u
.uri
.size
= size
;
2937 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2938 sizeof(struct lttng_uri
) * size
, NULL
);
2948 int lttng_enable_consumer(struct lttng_handle
*handle
);
2949 int lttng_enable_consumer(struct lttng_handle
*handle
)
2957 int lttng_disable_consumer(struct lttng_handle
*handle
);
2958 int lttng_disable_consumer(struct lttng_handle
*handle
)
2966 int _lttng_create_session_ext(const char *name
, const char *url
,
2967 const char *datetime
);
2968 int _lttng_create_session_ext(const char *name
, const char *url
,
2969 const char *datetime
)
2975 * For a given session name, this call checks if the data is ready to be read
2976 * or is still being extracted by the consumer(s) hence not ready to be used by
2979 int lttng_data_pending(const char *session_name
)
2982 struct lttcomm_session_msg lsm
;
2983 uint8_t *pending
= NULL
;
2985 if (session_name
== NULL
) {
2986 return -LTTNG_ERR_INVALID
;
2989 memset(&lsm
, 0, sizeof(lsm
));
2990 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2992 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2993 sizeof(lsm
.session
.name
));
2995 ret
= -LTTNG_ERR_INVALID
;
2999 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
3002 } else if (ret
!= 1) {
3003 /* Unexpected payload size */
3004 ret
= -LTTNG_ERR_INVALID
;
3006 } else if (!pending
) {
3007 /* Internal error. */
3008 ret
= -LTTNG_ERR_UNK
;
3012 ret
= (int) *pending
;
3019 * Regenerate the metadata for a session.
3020 * Return 0 on success, a negative error code on error.
3022 int lttng_regenerate_metadata(const char *session_name
)
3025 struct lttcomm_session_msg lsm
;
3027 if (!session_name
) {
3028 ret
= -LTTNG_ERR_INVALID
;
3032 memset(&lsm
, 0, sizeof(lsm
));
3033 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3035 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3036 sizeof(lsm
.session
.name
));
3038 ret
= -LTTNG_ERR_INVALID
;
3042 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3053 * Deprecated, replaced by lttng_regenerate_metadata.
3055 int lttng_metadata_regenerate(const char *session_name
)
3057 return lttng_regenerate_metadata(session_name
);
3061 * Regenerate the statedump of a session.
3062 * Return 0 on success, a negative error code on error.
3064 int lttng_regenerate_statedump(const char *session_name
)
3067 struct lttcomm_session_msg lsm
;
3069 if (!session_name
) {
3070 ret
= -LTTNG_ERR_INVALID
;
3074 memset(&lsm
, 0, sizeof(lsm
));
3075 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3077 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3078 sizeof(lsm
.session
.name
));
3080 ret
= -LTTNG_ERR_INVALID
;
3084 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3095 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3099 struct lttcomm_session_msg lsm
= {
3100 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3101 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
3103 struct lttcomm_session_msg
*message_lsm
;
3104 struct lttng_payload message
;
3105 struct lttng_payload reply
;
3106 struct lttng_trigger
*reply_trigger
= NULL
;
3107 enum lttng_domain_type domain_type
;
3108 const struct lttng_credentials user_creds
= {
3109 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3110 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3112 const char *unused_trigger_name
= NULL
;
3113 enum lttng_trigger_status trigger_status
;
3115 lttng_payload_init(&message
);
3116 lttng_payload_init(&reply
);
3119 ret
= -LTTNG_ERR_INVALID
;
3123 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3124 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3125 /* Re-using already registered trigger. */
3126 ret
= -LTTNG_ERR_INVALID
;
3131 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3132 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3133 ret
= -LTTNG_ERR_NOMEM
;
3138 if (!trigger
->creds
.uid
.is_set
) {
3139 /* Use the client's credentials as the trigger credentials. */
3140 lttng_trigger_set_credentials(trigger
, &user_creds
);
3143 * Validate that either the current trigger credentials and the
3144 * client credentials are identical or that the current user is
3145 * root. The root user can register, unregister triggers for
3146 * himself and other users.
3148 * This check is also present on the sessiond side, using the
3149 * credentials passed on the socket. These check are all
3152 const struct lttng_credentials
*trigger_creds
=
3153 lttng_trigger_get_credentials(trigger
);
3155 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3156 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3157 ret
= -LTTNG_ERR_EPERM
;
3158 goto end_unset_name
;
3163 if (!lttng_trigger_validate(trigger
)) {
3164 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3165 goto end_unset_name
;
3168 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3171 lsm
.domain
.type
= domain_type
;
3173 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3175 ret
= -LTTNG_ERR_NOMEM
;
3176 goto end_unset_name
;
3179 ret
= lttng_trigger_serialize(trigger
, &message
);
3181 ret
= -LTTNG_ERR_UNK
;
3182 goto end_unset_name
;
3186 * This is needed to populate the trigger object size for the command
3189 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3191 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3194 struct lttng_payload_view message_view
=
3195 lttng_payload_view_from_payload(
3198 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3200 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3202 goto end_unset_name
;
3207 struct lttng_payload_view reply_view
=
3208 lttng_payload_view_from_payload(
3209 &reply
, 0, reply
.buffer
.size
);
3211 ret
= lttng_trigger_create_from_payload(
3212 &reply_view
, &reply_trigger
);
3214 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3215 goto end_unset_name
;
3219 if (name
|| generate_name
) {
3220 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3222 ret
= -LTTNG_ERR_NOMEM
;
3231 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3232 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3233 ret
= -LTTNG_ERR_UNK
;
3236 lttng_payload_reset(&message
);
3237 lttng_payload_reset(&reply
);
3238 lttng_trigger_destroy(reply_trigger
);
3242 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3244 /* Register an anonymous trigger. */
3245 return _lttng_register_trigger(trigger
, NULL
, false);
3248 enum lttng_error_code
lttng_register_trigger_with_name(
3249 struct lttng_trigger
*trigger
, const char *name
)
3251 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3253 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3256 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3257 struct lttng_trigger
*trigger
)
3259 const int ret
= _lttng_register_trigger(trigger
, false, true);
3261 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3264 enum lttng_error_code
lttng_error_query_execute(
3265 const struct lttng_error_query
*query
,
3266 const struct lttng_endpoint
*endpoint
,
3267 struct lttng_error_query_results
**results
)
3270 enum lttng_error_code ret_code
;
3271 struct lttcomm_session_msg lsm
= {
3272 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3274 struct lttng_payload message
;
3275 struct lttng_payload reply
;
3276 struct lttcomm_session_msg
*message_lsm
;
3278 lttng_payload_init(&message
);
3279 lttng_payload_init(&reply
);
3281 if (!query
|| !results
) {
3282 ret_code
= LTTNG_ERR_INVALID
;
3286 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3287 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3291 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3293 ret_code
= LTTNG_ERR_NOMEM
;
3297 ret
= lttng_error_query_serialize(query
, &message
);
3299 ret_code
= LTTNG_ERR_UNK
;
3303 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3304 message_lsm
->u
.error_query
.length
=
3305 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3308 struct lttng_payload_view message_view
=
3309 lttng_payload_view_from_payload(
3312 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3314 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3322 ssize_t reply_create_ret
;
3323 struct lttng_payload_view reply_view
=
3324 lttng_payload_view_from_payload(
3325 &reply
, 0, reply
.buffer
.size
);
3327 reply_create_ret
= lttng_error_query_results_create_from_payload(
3328 &reply_view
, results
);
3329 if (reply_create_ret
< 0) {
3330 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3335 ret_code
= LTTNG_OK
;
3337 lttng_payload_reset(&message
);
3338 lttng_payload_reset(&reply
);
3342 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3345 struct lttcomm_session_msg lsm
;
3346 struct lttcomm_session_msg
*message_lsm
;
3347 struct lttng_payload message
;
3348 struct lttng_payload reply
;
3349 struct lttng_trigger
*copy
= NULL
;
3350 const struct lttng_credentials user_creds
= {
3351 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3352 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3355 lttng_payload_init(&message
);
3356 lttng_payload_init(&reply
);
3359 ret
= -LTTNG_ERR_INVALID
;
3363 copy
= lttng_trigger_copy(trigger
);
3365 ret
= -LTTNG_ERR_UNK
;
3369 if (!copy
->creds
.uid
.is_set
) {
3370 /* Use the client credentials as the trigger credentials */
3371 lttng_trigger_set_credentials(copy
, &user_creds
);
3374 * Validate that either the current trigger credentials and the
3375 * client credentials are identical or that the current user is
3376 * root. The root user can register, unregister triggers for
3377 * himself and other users.
3379 * This check is also present on the sessiond side, using the
3380 * credentials passed on the socket. These check are all
3383 const struct lttng_credentials
*trigger_creds
=
3384 lttng_trigger_get_credentials(copy
);
3385 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3386 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3387 ret
= -LTTNG_ERR_EPERM
;
3393 if (!lttng_trigger_validate(copy
)) {
3394 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3398 memset(&lsm
, 0, sizeof(lsm
));
3399 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3401 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3403 ret
= -LTTNG_ERR_NOMEM
;
3407 ret
= lttng_trigger_serialize(copy
, &message
);
3409 ret
= -LTTNG_ERR_UNK
;
3414 * This is needed to populate the trigger object size for the command
3415 * header and number of fds sent.
3417 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3419 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3422 struct lttng_payload_view message_view
=
3423 lttng_payload_view_from_payload(
3427 * Update the message header with the number of fd that will be
3430 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3433 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3441 lttng_trigger_destroy(copy
);
3442 lttng_payload_reset(&message
);
3443 lttng_payload_reset(&reply
);
3448 * Ask the session daemon for all registered triggers for the current user.
3450 * Allocates and return an lttng_triggers set.
3451 * On error, returns a suitable lttng_error_code.
3453 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3456 enum lttng_error_code ret_code
= LTTNG_OK
;
3457 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3458 struct lttng_triggers
*local_triggers
= NULL
;
3459 struct lttng_payload reply
;
3460 struct lttng_payload_view lsm_view
=
3461 lttng_payload_view_init_from_buffer(
3462 (const char *) &lsm
, 0, sizeof(lsm
));
3464 lttng_payload_init(&reply
);
3466 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3468 ret_code
= (enum lttng_error_code
) -ret
;
3473 struct lttng_payload_view reply_view
=
3474 lttng_payload_view_from_payload(
3475 &reply
, 0, reply
.buffer
.size
);
3477 ret
= lttng_triggers_create_from_payload(
3478 &reply_view
, &local_triggers
);
3480 ret_code
= LTTNG_ERR_FATAL
;
3485 *triggers
= local_triggers
;
3486 local_triggers
= NULL
;
3488 lttng_payload_reset(&reply
);
3489 lttng_triggers_destroy(local_triggers
);
3496 static void __attribute__((constructor
)) init(void)
3498 /* Set default session group */
3499 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3505 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3507 free(tracing_group
);