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.
84 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
85 struct lttng_domain
*src
)
89 case LTTNG_DOMAIN_KERNEL
:
90 case LTTNG_DOMAIN_UST
:
91 case LTTNG_DOMAIN_JUL
:
92 case LTTNG_DOMAIN_LOG4J
:
93 case LTTNG_DOMAIN_PYTHON
:
94 memcpy(dst
, src
, sizeof(struct lttng_domain
));
97 memset(dst
, 0, sizeof(struct lttng_domain
));
104 * Send lttcomm_session_msg to the session daemon.
106 * On success, returns the number of bytes sent (>=0)
107 * On error, returns -1
109 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
114 ret
= -LTTNG_ERR_NO_SESSIOND
;
118 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(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
)
199 LTTNG_ASSERT(len
> 0);
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.
246 int lttng_check_tracing_group(void)
248 gid_t
*grp_list
, tracing_gid
;
249 int grp_list_size
, grp_id
, i
;
251 const char *grp_name
= tracing_group
;
253 /* Get GID of group 'tracing' */
254 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
255 /* If grp_tracing is NULL, the group does not exist. */
259 /* Get number of supplementary group IDs */
260 grp_list_size
= getgroups(0, NULL
);
261 if (grp_list_size
< 0) {
266 /* Alloc group list of the right size */
267 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
272 grp_id
= getgroups(grp_list_size
, grp_list
);
278 for (i
= 0; i
< grp_list_size
; i
++) {
279 if (grp_list
[i
] == tracing_gid
) {
292 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
296 size_t best_mem_info
;
297 size_t num_bytes_requested_total
;
300 * Get the number of CPU currently online to compute the amount of
301 * memory needed to create a buffer for every CPU.
303 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
308 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
311 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
312 * reliable estimate we can get but it is only exposed by the kernel
313 * since 3.14. (See Linux kernel commit:
314 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
316 ret
= utils_get_memory_available(&best_mem_info
);
322 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
323 * is a sanity check for obvious user error.
325 ret
= utils_get_memory_total(&best_mem_info
);
333 return best_mem_info
>= num_bytes_requested_total
;
337 * Try connect to session daemon with sock_path.
339 * Return 0 on success, else -1
341 static int try_connect_sessiond(const char *sock_path
)
345 /* If socket exist, we check if the daemon listens for connect. */
346 ret
= access(sock_path
, F_OK
);
352 ret
= lttcomm_connect_unix_sock(sock_path
);
358 ret
= lttcomm_close_unix_sock(ret
);
360 PERROR("lttcomm_close_unix_sock");
370 * Set sessiond socket path by putting it in the global sessiond_sock_path
373 * Returns 0 on success, negative value on failure (the sessiond socket path
374 * is somehow too long or ENOMEM).
376 static int set_session_daemon_path(void)
378 int in_tgroup
= 0; /* In tracing group. */
384 /* Are we in the tracing group ? */
385 in_tgroup
= lttng_check_tracing_group();
388 if ((uid
== 0) || in_tgroup
== 1) {
389 const int ret
= lttng_strncpy(sessiond_sock_path
,
390 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
391 sizeof(sessiond_sock_path
));
403 ret
= try_connect_sessiond(sessiond_sock_path
);
407 /* Global session daemon not available... */
409 /* ...or not in tracing group (and not root), default */
412 * With GNU C < 2.1, snprintf returns -1 if the target buffer
414 * With GNU C >= 2.1, snprintf returns the required size
415 * (excluding closing null)
417 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
418 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
419 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
431 * Connect to the LTTng session daemon.
433 * On success, return the socket's file descriptor. On error, return -1.
435 int connect_sessiond(void)
439 ret
= set_session_daemon_path();
444 /* Connect to the sesssion daemon. */
445 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
456 static void reset_global_sessiond_connection_state(void)
458 sessiond_socket
= -1;
463 * Clean disconnect from the session daemon.
465 * On success, return 0. On error, return -1.
467 static int disconnect_sessiond(void)
472 ret
= lttcomm_close_unix_sock(sessiond_socket
);
473 reset_global_sessiond_connection_state();
479 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
487 ret
= -LTTNG_ERR_INVALID
;
497 ret
= recv_data_sessiond(buf
, len
);
503 ret
= -LTTNG_ERR_INVALID
;
507 /* Move ownership of command header buffer to user. */
512 /* No command header. */
528 * Ask the session daemon a specific command and put the data into buf.
529 * Takes extra var. len. data and file descriptors as input to send to the
532 * Return size of data (only payload, not header) or a negative error code.
534 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
535 const int *fds
, size_t nb_fd
, const void *vardata
,
536 size_t vardata_len
, void **user_payload_buf
,
537 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
541 struct lttcomm_lttng_msg llm
;
543 ret
= connect_sessiond();
545 ret
= -LTTNG_ERR_NO_SESSIOND
;
548 sessiond_socket
= ret
;
552 ret
= send_session_msg(lsm
);
554 /* Ret value is a valid lttng error code. */
557 /* Send var len data */
558 ret
= send_session_varlen(vardata
, vardata_len
);
560 /* Ret value is a valid lttng error code. */
565 ret
= send_session_fds(fds
, nb_fd
);
567 /* Ret value is a valid lttng error code. */
571 /* Get header from data transmission */
572 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
574 /* Ret value is a valid lttng error code. */
578 /* Check error code if OK */
579 if (llm
.ret_code
!= LTTNG_OK
) {
584 /* Get command header from data transmission */
585 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
586 user_cmd_header_buf
, user_cmd_header_len
);
591 /* Get payload from data transmission */
592 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
601 disconnect_sessiond();
605 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
606 struct lttng_payload
*reply
)
609 struct lttcomm_lttng_msg llm
;
610 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
612 LTTNG_ASSERT(reply
->buffer
.size
== 0);
613 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
615 ret
= connect_sessiond();
617 ret
= -LTTNG_ERR_NO_SESSIOND
;
620 sessiond_socket
= ret
;
624 /* Send command to session daemon */
625 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
626 message
->buffer
.size
);
628 ret
= -LTTNG_ERR_FATAL
;
633 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
636 ret
= -LTTNG_ERR_FATAL
;
641 /* Get header from data transmission */
642 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
644 /* Ret value is a valid lttng error code. */
648 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
650 /* Check error code if OK */
651 if (llm
.ret_code
!= LTTNG_OK
) {
652 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
653 /* Invalid error code received. */
654 ret
= -LTTNG_ERR_UNK
;
661 if (llm
.cmd_header_size
> 0) {
662 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
668 /* Get command header from data transmission */
669 if (llm
.data_size
> 0) {
670 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
676 if (llm
.fd_count
> 0) {
677 ret
= lttcomm_recv_payload_fds_unix_sock(
678 sessiond_socket
, llm
.fd_count
, reply
);
684 /* Don't return the llm header to the caller. */
685 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
686 reply
->buffer
.size
- sizeof(llm
));
687 ret
= lttng_dynamic_buffer_set_size(
688 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
690 /* Can't happen as size is reduced. */
694 ret
= reply
->buffer
.size
;
697 disconnect_sessiond();
702 * Create lttng handle and return pointer.
704 * The returned pointer will be NULL in case of malloc() error.
706 struct lttng_handle
*lttng_create_handle(const char *session_name
,
707 struct lttng_domain
*domain
)
710 struct lttng_handle
*handle
= NULL
;
712 handle
= zmalloc(sizeof(struct lttng_handle
));
713 if (handle
== NULL
) {
714 PERROR("malloc handle");
718 /* Copy session name */
719 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
720 sizeof(handle
->session_name
));
725 /* Copy lttng domain or leave initialized to 0. */
727 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
738 * Destroy handle by free(3) the pointer.
740 void lttng_destroy_handle(struct lttng_handle
*handle
)
746 * Register an outside consumer.
748 * Returns size of returned session payload data or a negative error code.
750 int lttng_register_consumer(struct lttng_handle
*handle
,
751 const char *socket_path
)
754 struct lttcomm_session_msg lsm
;
756 if (handle
== NULL
|| socket_path
== NULL
) {
757 ret
= -LTTNG_ERR_INVALID
;
761 memset(&lsm
, 0, sizeof(lsm
));
762 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
763 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
764 sizeof(lsm
.session
.name
));
766 ret
= -LTTNG_ERR_INVALID
;
770 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
772 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
773 sizeof(lsm
.u
.reg
.path
));
775 ret
= -LTTNG_ERR_INVALID
;
779 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
785 * Start tracing for all traces of the session.
787 * Returns size of returned session payload data or a negative error code.
789 int lttng_start_tracing(const char *session_name
)
792 struct lttcomm_session_msg lsm
;
794 if (session_name
== NULL
) {
795 ret
= -LTTNG_ERR_INVALID
;
799 memset(&lsm
, 0, sizeof(lsm
));
800 lsm
.cmd_type
= LTTNG_START_TRACE
;
802 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
803 sizeof(lsm
.session
.name
));
805 ret
= -LTTNG_ERR_INVALID
;
809 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
815 * Stop tracing for all traces of the session.
817 static int _lttng_stop_tracing(const char *session_name
, int wait
)
820 struct lttcomm_session_msg lsm
;
822 if (session_name
== NULL
) {
823 ret
= -LTTNG_ERR_INVALID
;
827 memset(&lsm
, 0, sizeof(lsm
));
828 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
830 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
831 sizeof(lsm
.session
.name
));
833 ret
= -LTTNG_ERR_INVALID
;
837 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
838 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
846 /* Check for data availability */
848 data_ret
= lttng_data_pending(session_name
);
850 /* Return the data available call error. */
856 * Data sleep time before retrying (in usec). Don't sleep if the
857 * call returned value indicates availability.
860 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
862 } while (data_ret
!= 0);
870 * Stop tracing and wait for data availability.
872 int lttng_stop_tracing(const char *session_name
)
874 return _lttng_stop_tracing(session_name
, 1);
878 * Stop tracing but _don't_ wait for data availability.
880 int lttng_stop_tracing_no_wait(const char *session_name
)
882 return _lttng_stop_tracing(session_name
, 0);
886 * Add context to a channel.
888 * If the given channel is NULL, add the contexts to all channels.
889 * The event_name param is ignored.
891 * Returns the size of the returned payload data or a negative error code.
893 int lttng_add_context(struct lttng_handle
*handle
,
894 struct lttng_event_context
*ctx
, const char *event_name
,
895 const char *channel_name
)
900 struct lttcomm_session_msg lsm
;
902 /* Safety check. Both are mandatory. */
903 if (handle
== NULL
|| ctx
== NULL
) {
904 ret
= -LTTNG_ERR_INVALID
;
908 memset(&lsm
, 0, sizeof(lsm
));
909 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
911 /* If no channel name, send empty string. */
912 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
913 sizeof(lsm
.u
.context
.channel_name
));
915 ret
= -LTTNG_ERR_INVALID
;
919 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
920 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
921 sizeof(lsm
.session
.name
));
923 ret
= -LTTNG_ERR_INVALID
;
927 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
928 size_t provider_len
, ctx_len
;
929 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
930 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
932 if (!provider_name
|| !ctx_name
) {
933 ret
= -LTTNG_ERR_INVALID
;
937 provider_len
= strlen(provider_name
);
938 if (provider_len
== 0) {
939 ret
= -LTTNG_ERR_INVALID
;
942 lsm
.u
.context
.provider_name_len
= provider_len
;
944 ctx_len
= strlen(ctx_name
);
946 ret
= -LTTNG_ERR_INVALID
;
949 lsm
.u
.context
.context_name_len
= ctx_len
;
951 len
= provider_len
+ ctx_len
;
954 ret
= -LTTNG_ERR_NOMEM
;
958 memcpy(buf
, provider_name
, provider_len
);
959 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
961 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
963 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
965 * Don't leak application addresses to the sessiond.
966 * This is only necessary when ctx is for an app ctx otherwise
967 * the values inside the union (type & config) are overwritten.
969 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
970 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
973 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
980 * Enable event(s) for a channel.
982 * If no event name is specified, all events are enabled.
983 * If no channel name is specified, the default 'channel0' is used.
985 * Returns size of returned session payload data or a negative error code.
987 int lttng_enable_event(struct lttng_handle
*handle
,
988 struct lttng_event
*ev
, const char *channel_name
)
990 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
995 * Create or enable an event with a filter expression.
997 * Return negative error value on error.
998 * Return size of returned session payload data if OK.
1000 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1001 struct lttng_event
*event
, const char *channel_name
,
1002 const char *filter_expression
)
1004 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1005 filter_expression
, 0, NULL
);
1009 * Depending on the event, return a newly allocated agent filter expression or
1010 * NULL if not applicable.
1012 * An event with NO loglevel and the name is * will return NULL.
1014 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1017 char *agent_filter
= NULL
;
1021 /* Don't add filter for the '*' event. */
1022 if (strcmp(ev
->name
, "*") != 0) {
1024 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1027 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1035 /* Add loglevel filtering if any for the JUL domain. */
1036 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1039 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1045 if (filter
|| agent_filter
) {
1048 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1049 agent_filter
? agent_filter
: filter
, op
,
1054 agent_filter
= new_filter
;
1056 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1065 return agent_filter
;
1072 * Enable event(s) for a channel, possibly with exclusions and a filter.
1073 * If no event name is specified, all events are enabled.
1074 * If no channel name is specified, the default name is used.
1075 * If filter expression is not NULL, the filter is set for the event.
1076 * If exclusion count is not zero, the exclusions are set for the event.
1077 * Returns size of returned session payload data or a negative error code.
1079 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1080 struct lttng_event
*ev
, const char *channel_name
,
1081 const char *original_filter_expression
,
1082 int exclusion_count
, char **exclusion_list
)
1084 struct lttcomm_session_msg lsm
;
1085 struct lttng_payload payload
;
1087 unsigned int free_filter_expression
= 0;
1088 struct filter_parser_ctx
*ctx
= NULL
;
1091 * We have either a filter or some exclusions, so we need to set up
1092 * a variable-length payload from where to send the data.
1094 lttng_payload_init(&payload
);
1097 * Cast as non-const since we may replace the filter expression
1098 * by a dynamically allocated string. Otherwise, the original
1099 * string is not modified.
1101 char *filter_expression
= (char *) original_filter_expression
;
1103 if (handle
== NULL
|| ev
== NULL
) {
1104 ret
= -LTTNG_ERR_INVALID
;
1109 * Empty filter string will always be rejected by the parser
1110 * anyway, so treat this corner-case early to eliminate
1111 * lttng_fmemopen error for 0-byte allocation.
1113 if (filter_expression
&& filter_expression
[0] == '\0') {
1114 ret
= -LTTNG_ERR_INVALID
;
1118 memset(&lsm
, 0, sizeof(lsm
));
1120 /* If no channel name, send empty string. */
1121 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1122 sizeof(lsm
.u
.enable
.channel_name
));
1124 ret
= -LTTNG_ERR_INVALID
;
1128 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1129 if (ev
->name
[0] == '\0') {
1130 /* Enable all events. */
1131 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1132 LTTNG_ASSERT(ret
== 0);
1135 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1136 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1138 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1139 sizeof(lsm
.session
.name
));
1141 ret
= -LTTNG_ERR_INVALID
;
1145 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1146 lsm
.u
.enable
.bytecode_len
= 0;
1148 /* Parse filter expression. */
1149 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1150 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1151 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1152 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1153 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1154 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1157 /* Setup JUL filter if needed. */
1158 agent_filter
= set_agent_filter(filter_expression
, ev
);
1159 if (!agent_filter
) {
1160 if (!filter_expression
) {
1162 * No JUL and no filter, just skip
1169 * With an agent filter, the original filter has
1170 * been added to it thus replace the filter
1173 filter_expression
= agent_filter
;
1174 free_filter_expression
= 1;
1178 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1183 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1184 + bytecode_get_len(&ctx
->bytecode
->b
);
1185 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1188 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1189 lsm
.u
.enable
.bytecode_len
+
1190 lsm
.u
.enable
.expression_len
+
1191 LTTNG_SYMBOL_NAME_LEN
*
1194 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1198 /* Put exclusion names first in the data. */
1199 for (i
= 0; i
< exclusion_count
; i
++) {
1200 size_t exclusion_len
;
1202 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1203 LTTNG_SYMBOL_NAME_LEN
);
1204 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1205 /* Exclusion is not NULL-terminated. */
1206 ret
= -LTTNG_ERR_INVALID
;
1210 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1211 exclusion_list
[i
], exclusion_len
);
1217 * Padding the rest of the entry with zeros. Every exclusion
1218 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1220 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1221 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1227 /* Add filter expression next. */
1228 if (filter_expression
) {
1229 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1230 filter_expression
, lsm
.u
.enable
.expression_len
);
1235 /* Add filter bytecode next. */
1236 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1237 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1238 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1243 if (ev
->extended
.ptr
) {
1244 struct lttng_event_extended
*ev_ext
=
1245 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1247 if (ev_ext
->probe_location
) {
1249 * lttng_userspace_probe_location_serialize returns the
1250 * number of bytes that was appended to the buffer.
1252 ret
= lttng_userspace_probe_location_serialize(
1253 ev_ext
->probe_location
, &payload
);
1259 * Set the size of the userspace probe location element
1260 * of the buffer so that the receiving side knows where
1263 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1268 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1270 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1277 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1278 if (fd_count
== 1) {
1279 struct fd_handle
*h
=
1280 lttng_payload_view_pop_fd_handle(&view
);
1286 fd_to_send
= fd_handle_get_fd(h
);
1290 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1291 fd_count
? &fd_to_send
: NULL
, fd_count
,
1292 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1293 view
.buffer
.size
, NULL
, NULL
, 0);
1297 if (filter_expression
&& ctx
) {
1298 filter_bytecode_free(ctx
);
1299 filter_ir_free(ctx
);
1300 filter_parser_ctx_free(ctx
);
1303 if (free_filter_expression
) {
1305 * The filter expression has been replaced and must be freed as
1306 * it is not the original filter expression received as a
1309 free(filter_expression
);
1313 * Return directly to the caller and don't ask the sessiond since
1314 * something went wrong in the parsing of data above.
1316 lttng_payload_reset(&payload
);
1320 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1324 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1325 struct lttng_event
*ev
, const char *channel_name
,
1326 const char *original_filter_expression
)
1328 struct lttcomm_session_msg lsm
;
1331 unsigned int free_filter_expression
= 0;
1332 struct filter_parser_ctx
*ctx
= NULL
;
1334 * Cast as non-const since we may replace the filter expression
1335 * by a dynamically allocated string. Otherwise, the original
1336 * string is not modified.
1338 char *filter_expression
= (char *) original_filter_expression
;
1340 if (handle
== NULL
|| ev
== NULL
) {
1341 ret
= -LTTNG_ERR_INVALID
;
1346 * Empty filter string will always be rejected by the parser
1347 * anyway, so treat this corner-case early to eliminate
1348 * lttng_fmemopen error for 0-byte allocation.
1350 if (filter_expression
&& filter_expression
[0] == '\0') {
1351 ret
= -LTTNG_ERR_INVALID
;
1355 memset(&lsm
, 0, sizeof(lsm
));
1357 /* If no channel name, send empty string. */
1358 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1359 sizeof(lsm
.u
.disable
.channel_name
));
1361 ret
= -LTTNG_ERR_INVALID
;
1365 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1367 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1368 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1370 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1371 sizeof(lsm
.session
.name
));
1373 ret
= -LTTNG_ERR_INVALID
;
1377 lsm
.u
.disable
.bytecode_len
= 0;
1380 * For the JUL domain, a filter is enforced except for the
1381 * disable all event. This is done to avoid having the event in
1382 * all sessions thus filtering by logger name.
1384 if (filter_expression
== NULL
&&
1385 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1386 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1387 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1392 * We have a filter, so we need to set up a variable-length
1393 * memory block from where to send the data.
1396 /* Parse filter expression */
1397 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1398 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1399 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1400 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1401 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1402 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1405 /* Setup JUL filter if needed. */
1406 agent_filter
= set_agent_filter(filter_expression
, ev
);
1407 if (!agent_filter
) {
1408 if (!filter_expression
) {
1410 * No JUL and no filter, just skip
1417 * With a JUL filter, the original filter has
1418 * been added to it thus replace the filter
1421 filter_expression
= agent_filter
;
1422 free_filter_expression
= 1;
1426 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1431 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1432 + bytecode_get_len(&ctx
->bytecode
->b
);
1433 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1436 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1437 + lsm
.u
.disable
.expression_len
);
1439 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1443 /* Add filter expression. */
1444 if (lsm
.u
.disable
.expression_len
!= 0) {
1447 lsm
.u
.disable
.expression_len
);
1449 /* Add filter bytecode next. */
1450 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1452 + lsm
.u
.disable
.expression_len
,
1454 lsm
.u
.disable
.bytecode_len
);
1457 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1458 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1462 if (filter_expression
&& ctx
) {
1463 filter_bytecode_free(ctx
);
1464 filter_ir_free(ctx
);
1465 filter_parser_ctx_free(ctx
);
1468 if (free_filter_expression
) {
1470 * The filter expression has been replaced and must be freed as
1471 * it is not the original filter expression received as a
1474 free(filter_expression
);
1478 * Return directly to the caller and don't ask the sessiond since
1479 * something went wrong in the parsing of data above.
1484 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1489 * Disable event(s) of a channel and domain.
1490 * If no event name is specified, all events are disabled.
1491 * If no channel name is specified, the default 'channel0' is used.
1492 * Returns size of returned session payload data or a negative error code.
1494 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1495 const char *channel_name
)
1498 struct lttng_event ev
;
1500 memset(&ev
, 0, sizeof(ev
));
1502 ev
.type
= LTTNG_EVENT_ALL
;
1503 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1505 ret
= -LTTNG_ERR_INVALID
;
1509 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1514 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1516 struct lttng_channel
*channel
= NULL
;
1517 struct lttng_channel_extended
*extended
= NULL
;
1523 /* Validate domain. */
1524 switch (domain
->type
) {
1525 case LTTNG_DOMAIN_UST
:
1526 switch (domain
->buf_type
) {
1527 case LTTNG_BUFFER_PER_UID
:
1528 case LTTNG_BUFFER_PER_PID
:
1534 case LTTNG_DOMAIN_KERNEL
:
1535 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1543 channel
= zmalloc(sizeof(*channel
));
1548 extended
= zmalloc(sizeof(*extended
));
1553 channel
->attr
.extended
.ptr
= extended
;
1555 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1563 void lttng_channel_destroy(struct lttng_channel
*channel
)
1569 if (channel
->attr
.extended
.ptr
) {
1570 free(channel
->attr
.extended
.ptr
);
1576 * Enable channel per domain
1577 * Returns size of returned session payload data or a negative error code.
1579 int lttng_enable_channel(struct lttng_handle
*handle
,
1580 struct lttng_channel
*in_chan
)
1583 struct lttcomm_session_msg lsm
;
1584 size_t total_buffer_size_needed_per_cpu
= 0;
1586 /* NULL arguments are forbidden. No default values. */
1587 if (handle
== NULL
|| in_chan
== NULL
) {
1588 return -LTTNG_ERR_INVALID
;
1591 memset(&lsm
, 0, sizeof(lsm
));
1592 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1593 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1595 if (!in_chan
->attr
.extended
.ptr
) {
1596 struct lttng_channel
*channel
;
1597 struct lttng_channel_extended
*extended
;
1599 channel
= lttng_channel_create(&handle
->domain
);
1601 return -LTTNG_ERR_NOMEM
;
1605 * Create a new channel in order to use default extended
1608 extended
= (struct lttng_channel_extended
*)
1609 channel
->attr
.extended
.ptr
;
1610 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1611 lttng_channel_destroy(channel
);
1613 struct lttng_channel_extended
*extended
;
1615 extended
= (struct lttng_channel_extended
*)
1616 in_chan
->attr
.extended
.ptr
;
1617 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1621 * Verify that the amount of memory required to create the requested
1622 * buffer is available on the system at the moment.
1624 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1625 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1626 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1627 return -LTTNG_ERR_NOMEM
;
1630 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1631 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1633 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1634 sizeof(lsm
.session
.name
));
1636 ret
= -LTTNG_ERR_INVALID
;
1640 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1646 * All tracing will be stopped for registered events of the channel.
1647 * Returns size of returned session payload data or a negative error code.
1649 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1652 struct lttcomm_session_msg lsm
;
1654 /* Safety check. Both are mandatory. */
1655 if (handle
== NULL
|| name
== NULL
) {
1656 return -LTTNG_ERR_INVALID
;
1659 memset(&lsm
, 0, sizeof(lsm
));
1661 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1663 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1664 sizeof(lsm
.u
.disable
.channel_name
));
1666 ret
= -LTTNG_ERR_INVALID
;
1670 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1672 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1673 sizeof(lsm
.session
.name
));
1675 ret
= -LTTNG_ERR_INVALID
;
1679 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1685 * Lists all available tracepoints of domain.
1686 * Sets the contents of the events array.
1687 * Returns the number of lttng_event entries in events;
1688 * on error, returns a negative value.
1690 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1691 struct lttng_event
**events
)
1694 struct lttcomm_session_msg lsm
;
1696 if (handle
== NULL
) {
1697 return -LTTNG_ERR_INVALID
;
1700 memset(&lsm
, 0, sizeof(lsm
));
1701 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1702 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1704 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1709 return ret
/ sizeof(struct lttng_event
);
1713 * Lists all available tracepoint fields of domain.
1714 * Sets the contents of the event field array.
1715 * Returns the number of lttng_event_field entries in events;
1716 * on error, returns a negative value.
1718 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1719 struct lttng_event_field
**fields
)
1722 struct lttcomm_session_msg lsm
;
1724 if (handle
== NULL
) {
1725 return -LTTNG_ERR_INVALID
;
1728 memset(&lsm
, 0, sizeof(lsm
));
1729 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1730 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1732 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1737 return ret
/ sizeof(struct lttng_event_field
);
1741 * Lists all available kernel system calls. Allocates and sets the contents of
1744 * Returns the number of lttng_event entries in events; on error, returns a
1747 int lttng_list_syscalls(struct lttng_event
**events
)
1750 struct lttcomm_session_msg lsm
;
1753 return -LTTNG_ERR_INVALID
;
1756 memset(&lsm
, 0, sizeof(lsm
));
1757 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1758 /* Force kernel domain for system calls. */
1759 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1761 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1766 return ret
/ sizeof(struct lttng_event
);
1770 * Returns a human readable string describing
1771 * the error code (a negative value).
1773 const char *lttng_strerror(int code
)
1775 return error_get_str(code
);
1778 enum lttng_error_code
lttng_create_session_ext(
1779 struct lttng_session_descriptor
*session_descriptor
)
1781 enum lttng_error_code ret_code
;
1782 struct lttcomm_session_msg lsm
= {
1783 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1786 struct lttng_buffer_view reply_view
;
1788 bool sessiond_must_generate_ouput
;
1789 struct lttng_dynamic_buffer payload
;
1791 size_t descriptor_size
;
1792 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1794 lttng_dynamic_buffer_init(&payload
);
1795 if (!session_descriptor
) {
1796 ret_code
= LTTNG_ERR_INVALID
;
1800 sessiond_must_generate_ouput
=
1801 !lttng_session_descriptor_is_output_destination_initialized(
1802 session_descriptor
);
1803 if (sessiond_must_generate_ouput
) {
1804 const char *home_dir
= utils_get_home_dir();
1805 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1807 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1808 ret_code
= LTTNG_ERR_FATAL
;
1812 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1813 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1816 ret_code
= LTTNG_ERR_NOMEM
;
1821 descriptor_size
= payload
.size
;
1822 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1825 ret_code
= LTTNG_ERR_INVALID
;
1828 descriptor_size
= payload
.size
- descriptor_size
;
1829 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1831 /* Command returns a session descriptor on success. */
1832 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1833 payload
.size
, &reply
);
1834 if (reply_ret
< 0) {
1835 ret_code
= -reply_ret
;
1837 } else if (reply_ret
== 0) {
1838 /* Socket unexpectedly closed by the session daemon. */
1839 ret_code
= LTTNG_ERR_FATAL
;
1843 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1844 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1847 ret_code
= LTTNG_ERR_FATAL
;
1850 ret_code
= LTTNG_OK
;
1851 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1854 lttng_dynamic_buffer_reset(&payload
);
1855 lttng_session_descriptor_destroy(descriptor_reply
);
1860 * Create a new session using name and url for destination.
1862 * Return 0 on success else a negative LTTng error code.
1864 int lttng_create_session(const char *name
, const char *url
)
1868 struct lttng_uri
*uris
= NULL
;
1869 struct lttng_session_descriptor
*descriptor
= NULL
;
1870 enum lttng_error_code ret_code
;
1873 ret
= -LTTNG_ERR_INVALID
;
1877 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1879 ret
= -LTTNG_ERR_INVALID
;
1884 descriptor
= lttng_session_descriptor_create(name
);
1887 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1888 ret
= -LTTNG_ERR_INVALID
;
1891 descriptor
= lttng_session_descriptor_local_create(name
,
1895 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1899 ret
= -LTTNG_ERR_INVALID
;
1903 ret
= -LTTNG_ERR_INVALID
;
1906 ret_code
= lttng_create_session_ext(descriptor
);
1907 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1909 lttng_session_descriptor_destroy(descriptor
);
1915 * Create a session exclusively used for snapshot.
1917 * Return 0 on success else a negative LTTng error code.
1919 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1922 enum lttng_error_code ret_code
;
1924 struct lttng_uri
*uris
= NULL
;
1925 struct lttng_session_descriptor
*descriptor
= NULL
;
1928 ret
= -LTTNG_ERR_INVALID
;
1932 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1934 ret
= -LTTNG_ERR_INVALID
;
1938 * If the user does not specify a custom subdir, use the session name.
1940 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1941 strlen(uris
[0].subdir
) == 0) {
1942 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1945 PERROR("Failed to set session name as network destination sub-directory");
1946 ret
= -LTTNG_ERR_FATAL
;
1948 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1949 /* Truncated output. */
1950 ret
= -LTTNG_ERR_INVALID
;
1957 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1960 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1961 ret
= -LTTNG_ERR_INVALID
;
1964 descriptor
= lttng_session_descriptor_snapshot_local_create(
1969 descriptor
= lttng_session_descriptor_snapshot_network_create(
1975 ret
= -LTTNG_ERR_INVALID
;
1979 ret
= -LTTNG_ERR_INVALID
;
1982 ret_code
= lttng_create_session_ext(descriptor
);
1983 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1985 lttng_session_descriptor_destroy(descriptor
);
1991 * Create a session exclusively used for live.
1993 * Return 0 on success else a negative LTTng error code.
1995 int lttng_create_session_live(const char *name
, const char *url
,
1996 unsigned int timer_interval
)
1999 enum lttng_error_code ret_code
;
2000 struct lttng_session_descriptor
*descriptor
= NULL
;
2003 ret
= -LTTNG_ERR_INVALID
;
2008 descriptor
= lttng_session_descriptor_live_network_create(
2009 name
, url
, NULL
, timer_interval
);
2011 descriptor
= lttng_session_descriptor_live_create(
2012 name
, timer_interval
);
2015 ret
= -LTTNG_ERR_INVALID
;
2018 ret_code
= lttng_create_session_ext(descriptor
);
2019 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2021 lttng_session_descriptor_destroy(descriptor
);
2026 * Stop the session and wait for the data before destroying it
2028 * Return 0 on success else a negative LTTng error code.
2030 int lttng_destroy_session(const char *session_name
)
2033 enum lttng_error_code ret_code
;
2034 enum lttng_destruction_handle_status status
;
2035 struct lttng_destruction_handle
*handle
= NULL
;
2038 * Stop the tracing and wait for the data to be
2041 ret
= _lttng_stop_tracing(session_name
, 1);
2042 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2046 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2047 if (ret_code
!= LTTNG_OK
) {
2048 ret
= (int) -ret_code
;
2051 LTTNG_ASSERT(handle
);
2053 /* Block until the completion of the destruction of the session. */
2054 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2055 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2056 ret
= -LTTNG_ERR_UNK
;
2060 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2061 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2062 ret
= -LTTNG_ERR_UNK
;
2065 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2067 lttng_destruction_handle_destroy(handle
);
2072 * Destroy the session without waiting for the data.
2074 int lttng_destroy_session_no_wait(const char *session_name
)
2076 enum lttng_error_code ret_code
;
2078 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2079 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2083 * Ask the session daemon for all available sessions.
2084 * Sets the contents of the sessions array.
2085 * Returns the number of lttng_session entries in sessions;
2086 * on error, returns a negative value.
2088 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2091 struct lttcomm_session_msg lsm
;
2092 const size_t session_size
= sizeof(struct lttng_session
) +
2093 sizeof(struct lttng_session_extended
);
2094 size_t session_count
, i
;
2095 struct lttng_session_extended
*sessions_extended_begin
;
2096 struct lttng_session
*sessions
= NULL
;
2098 memset(&lsm
, 0, sizeof(lsm
));
2099 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2100 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2105 ret
= -LTTNG_ERR_FATAL
;
2109 if (ret
% session_size
) {
2110 ret
= -LTTNG_ERR_UNK
;
2112 *out_sessions
= NULL
;
2115 session_count
= (size_t) ret
/ session_size
;
2116 sessions_extended_begin
= (struct lttng_session_extended
*)
2117 (&sessions
[session_count
]);
2119 /* Set extended session info pointers. */
2120 for (i
= 0; i
< session_count
; i
++) {
2121 struct lttng_session
*session
= &sessions
[i
];
2122 struct lttng_session_extended
*extended
=
2123 &(sessions_extended_begin
[i
]);
2125 session
->extended
.ptr
= extended
;
2128 ret
= (int) session_count
;
2129 *out_sessions
= sessions
;
2134 enum lttng_error_code
lttng_session_get_creation_time(
2135 const struct lttng_session
*session
, uint64_t *creation_time
)
2137 enum lttng_error_code ret
= LTTNG_OK
;
2138 struct lttng_session_extended
*extended
;
2140 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2141 ret
= LTTNG_ERR_INVALID
;
2145 extended
= session
->extended
.ptr
;
2146 if (!extended
->creation_time
.is_set
) {
2147 /* Not created on the session daemon yet. */
2148 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2151 *creation_time
= extended
->creation_time
.value
;
2156 int lttng_set_session_shm_path(const char *session_name
,
2157 const char *shm_path
)
2160 struct lttcomm_session_msg lsm
;
2162 if (session_name
== NULL
) {
2163 return -LTTNG_ERR_INVALID
;
2166 memset(&lsm
, 0, sizeof(lsm
));
2167 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2169 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2170 sizeof(lsm
.session
.name
));
2172 ret
= -LTTNG_ERR_INVALID
;
2176 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2177 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2179 ret
= -LTTNG_ERR_INVALID
;
2183 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2189 * Ask the session daemon for all available domains of a session.
2190 * Sets the contents of the domains array.
2191 * Returns the number of lttng_domain entries in domains;
2192 * on error, returns a negative value.
2194 int lttng_list_domains(const char *session_name
,
2195 struct lttng_domain
**domains
)
2198 struct lttcomm_session_msg lsm
;
2200 if (session_name
== NULL
) {
2201 ret
= -LTTNG_ERR_INVALID
;
2205 memset(&lsm
, 0, sizeof(lsm
));
2206 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2208 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2209 sizeof(lsm
.session
.name
));
2211 ret
= -LTTNG_ERR_INVALID
;
2215 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2220 return ret
/ sizeof(struct lttng_domain
);
2226 * Ask the session daemon for all available channels of a session.
2227 * Sets the contents of the channels array.
2228 * Returns the number of lttng_channel entries in channels;
2229 * on error, returns a negative value.
2231 int lttng_list_channels(struct lttng_handle
*handle
,
2232 struct lttng_channel
**channels
)
2235 size_t channel_count
, i
;
2236 const size_t channel_size
= sizeof(struct lttng_channel
) +
2237 sizeof(struct lttng_channel_extended
);
2238 struct lttcomm_session_msg lsm
;
2241 if (handle
== NULL
) {
2242 ret
= -LTTNG_ERR_INVALID
;
2246 memset(&lsm
, 0, sizeof(lsm
));
2247 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2248 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2249 sizeof(lsm
.session
.name
));
2251 ret
= -LTTNG_ERR_INVALID
;
2255 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2257 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2262 if (ret
% channel_size
) {
2263 ret
= -LTTNG_ERR_UNK
;
2268 channel_count
= (size_t) ret
/ channel_size
;
2270 /* Set extended info pointers */
2271 extended_at
= ((void *) *channels
) +
2272 channel_count
* sizeof(struct lttng_channel
);
2273 for (i
= 0; i
< channel_count
; i
++) {
2274 struct lttng_channel
*chan
= &(*channels
)[i
];
2276 chan
->attr
.extended
.ptr
= extended_at
;
2277 extended_at
+= sizeof(struct lttng_channel_extended
);
2280 ret
= (int) channel_count
;
2286 * Ask the session daemon for all available events of a session channel.
2287 * Sets the contents of the events array.
2288 * Returns the number of lttng_event entries in events;
2289 * on error, returns a negative value.
2291 int lttng_list_events(struct lttng_handle
*handle
,
2292 const char *channel_name
, struct lttng_event
**events
)
2295 struct lttcomm_session_msg lsm
= {};
2296 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2297 uint32_t nb_events
, i
;
2298 const void *comm_ext_at
;
2299 struct lttng_dynamic_buffer listing
;
2301 struct lttng_payload payload
;
2302 struct lttng_payload payload_copy
;
2303 struct lttng_payload_view lsm_view
=
2304 lttng_payload_view_init_from_buffer(
2305 (const char *) &lsm
, 0, sizeof(lsm
));
2306 struct lttng_buffer_view cmd_header_view
;
2307 struct lttng_buffer_view cmd_payload_view
;
2308 struct lttng_buffer_view flat_events_view
;
2309 struct lttng_buffer_view ext_view
;
2311 /* Safety check. An handle and channel name are mandatory */
2312 if (handle
== NULL
|| channel_name
== NULL
) {
2313 ret
= -LTTNG_ERR_INVALID
;
2317 lttng_payload_init(&payload
);
2318 lttng_payload_init(&payload_copy
);
2320 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2321 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2322 sizeof(lsm
.session
.name
));
2324 ret
= -LTTNG_ERR_INVALID
;
2328 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2329 sizeof(lsm
.u
.list
.channel_name
));
2331 ret
= -LTTNG_ERR_INVALID
;
2335 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2337 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2343 * A copy of the payload is performed since it will be
2344 * consumed twice. Consuming the same payload twice is invalid since
2345 * it will cause any received file descriptor to become "shared"
2346 * between different instances of the resulting objects.
2348 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2350 ret
= -LTTNG_ERR_NOMEM
;
2354 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2355 &payload
.buffer
, 0, sizeof(*cmd_header
));
2356 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2357 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2361 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2363 /* Set number of events and free command header */
2364 nb_events
= cmd_header
->nb_events
;
2365 if (nb_events
> INT_MAX
) {
2366 ret
= -LTTNG_ERR_OVERFLOW
;
2370 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2371 &payload
.buffer
, sizeof(*cmd_header
), -1);
2374 * The buffer that is returned must contain a "flat" version of
2375 * the events that are returned. In other words, all pointers
2376 * within an lttng_event must point to a location within the returned
2377 * buffer so that the user may free everything by simply calling free()
2378 * on the returned buffer. This is needed in order to maintain API
2381 * A first pass is performed to compute the size of the buffer that
2382 * must be allocated. A second pass is then performed to setup
2383 * the returned events so that their members always point within the
2386 * The layout of the returned buffer is as follows:
2387 * - struct lttng_event[nb_events],
2388 * - nb_events times the following:
2389 * - struct lttng_event_extended,
2390 * - flattened version of userspace_probe_location
2391 * - filter_expression
2393 * - padding to align to 64-bits
2395 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2396 nb_events
* sizeof(struct lttng_event
), -1);
2397 comm_ext_at
= ext_view
.data
;
2398 storage_req
= nb_events
* sizeof(struct lttng_event
);
2400 struct lttng_payload_view payload_view
=
2401 lttng_payload_view_from_payload(&payload
, 0, -1);
2403 for (i
= 0; i
< nb_events
; i
++) {
2404 const struct lttcomm_event_extended_header
*ext_comm
=
2405 (struct lttcomm_event_extended_header
*)
2407 int probe_storage_req
= 0;
2409 comm_ext_at
+= sizeof(*ext_comm
);
2410 comm_ext_at
+= ext_comm
->filter_len
;
2411 comm_ext_at
+= ext_comm
->nb_exclusions
*
2412 LTTNG_SYMBOL_NAME_LEN
;
2414 if (ext_comm
->userspace_probe_location_len
) {
2415 struct lttng_userspace_probe_location
2416 *probe_location
= NULL
;
2417 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2419 (const char *) comm_ext_at
-
2420 payload_view
.buffer
.data
,
2421 ext_comm
->userspace_probe_location_len
);
2423 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2424 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2429 * Create a temporary userspace probe location
2430 * to determine the size needed by a "flattened"
2431 * version of that same probe location.
2433 ret
= lttng_userspace_probe_location_create_from_payload(
2434 &probe_location_view
,
2437 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2441 ret
= lttng_userspace_probe_location_flatten(
2442 probe_location
, NULL
);
2443 lttng_userspace_probe_location_destroy(
2446 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2450 probe_storage_req
= ret
;
2451 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2454 storage_req
+= sizeof(struct lttng_event_extended
);
2455 storage_req
+= ext_comm
->filter_len
;
2456 storage_req
+= ext_comm
->nb_exclusions
*
2457 LTTNG_SYMBOL_NAME_LEN
;
2458 /* Padding to ensure the flat probe is aligned. */
2459 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2460 storage_req
+= probe_storage_req
;
2464 lttng_dynamic_buffer_init(&listing
);
2466 * We must ensure that "listing" is never resized so as to preserve
2467 * the validity of the flattened objects.
2469 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2471 ret
= -LTTNG_ERR_NOMEM
;
2475 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2476 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2477 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2478 nb_events
* sizeof(struct lttng_event
));
2479 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2481 ret
= -LTTNG_ERR_NOMEM
;
2482 goto free_dynamic_buffer
;
2485 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2486 nb_events
* sizeof(struct lttng_event
), -1);
2487 comm_ext_at
= ext_view
.data
;
2490 struct lttng_payload_view payload_copy_view
=
2491 lttng_payload_view_from_payload(
2492 &payload_copy
, 0, -1);
2494 for (i
= 0; i
< nb_events
; i
++) {
2495 struct lttng_event
*event
= (typeof(event
))(
2497 (sizeof(struct lttng_event
) * i
));
2498 const struct lttcomm_event_extended_header
*ext_comm
=
2499 (typeof(ext_comm
)) comm_ext_at
;
2500 struct lttng_event_extended
*event_extended
=
2501 (typeof(event_extended
))(listing
.data
+
2504 /* Insert struct lttng_event_extended. */
2505 ret
= lttng_dynamic_buffer_set_size(&listing
,
2506 listing
.size
+ sizeof(*event_extended
));
2508 ret
= -LTTNG_ERR_NOMEM
;
2509 goto free_dynamic_buffer
;
2511 event
->extended
.ptr
= event_extended
;
2513 comm_ext_at
+= sizeof(*ext_comm
);
2515 /* Insert filter expression. */
2516 if (ext_comm
->filter_len
) {
2517 event_extended
->filter_expression
=
2518 listing
.data
+ listing
.size
;
2519 ret
= lttng_dynamic_buffer_append(&listing
,
2521 ext_comm
->filter_len
);
2523 ret
= -LTTNG_ERR_NOMEM
;
2524 goto free_dynamic_buffer
;
2526 comm_ext_at
+= ext_comm
->filter_len
;
2529 /* Insert exclusions. */
2530 if (ext_comm
->nb_exclusions
) {
2531 event_extended
->exclusions
.count
=
2532 ext_comm
->nb_exclusions
;
2533 event_extended
->exclusions
.strings
=
2534 listing
.data
+ listing
.size
;
2536 ret
= lttng_dynamic_buffer_append(&listing
,
2538 ext_comm
->nb_exclusions
*
2539 LTTNG_SYMBOL_NAME_LEN
);
2541 ret
= -LTTNG_ERR_NOMEM
;
2542 goto free_dynamic_buffer
;
2544 comm_ext_at
+= ext_comm
->nb_exclusions
*
2545 LTTNG_SYMBOL_NAME_LEN
;
2548 /* Insert padding to align to 64-bits. */
2549 ret
= lttng_dynamic_buffer_set_size(&listing
,
2550 ALIGN_TO(listing
.size
,
2553 ret
= -LTTNG_ERR_NOMEM
;
2554 goto free_dynamic_buffer
;
2557 /* Insert flattened userspace probe location. */
2558 if (ext_comm
->userspace_probe_location_len
) {
2559 struct lttng_userspace_probe_location
2560 *probe_location
= NULL
;
2561 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2563 (const char *) comm_ext_at
-
2564 payload_copy_view
.buffer
.data
,
2565 ext_comm
->userspace_probe_location_len
);
2567 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2568 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2569 goto free_dynamic_buffer
;
2572 ret
= lttng_userspace_probe_location_create_from_payload(
2573 &probe_location_view
,
2576 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2577 goto free_dynamic_buffer
;
2580 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2583 ret
= lttng_userspace_probe_location_flatten(
2584 probe_location
, &listing
);
2585 lttng_userspace_probe_location_destroy(
2588 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2589 goto free_dynamic_buffer
;
2592 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2597 /* Don't reset listing buffer as we return its content. */
2598 *events
= (struct lttng_event
*) listing
.data
;
2599 lttng_dynamic_buffer_init(&listing
);
2600 ret
= (int) nb_events
;
2601 free_dynamic_buffer
:
2602 lttng_dynamic_buffer_reset(&listing
);
2604 lttng_payload_reset(&payload
);
2605 lttng_payload_reset(&payload_copy
);
2610 * Sets the tracing_group variable with name.
2611 * This function allocates memory pointed to by tracing_group.
2612 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2614 int lttng_set_tracing_group(const char *name
)
2617 return -LTTNG_ERR_INVALID
;
2620 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2621 return -LTTNG_ERR_FATAL
;
2627 int lttng_calibrate(struct lttng_handle
*handle
,
2628 struct lttng_calibrate
*calibrate
)
2631 * This command was removed in LTTng 2.9.
2633 return -LTTNG_ERR_UND
;
2637 * Set default channel attributes.
2638 * If either or both of the arguments are null, attr content is zeroe'd.
2640 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2641 struct lttng_channel_attr
*attr
)
2643 struct lttng_channel_extended
*extended
;
2646 if (attr
== NULL
|| domain
== NULL
) {
2650 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2651 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2653 /* Same for all domains. */
2654 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2655 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2656 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2658 switch (domain
->type
) {
2659 case LTTNG_DOMAIN_KERNEL
:
2660 attr
->switch_timer_interval
=
2661 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2662 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2663 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2664 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2665 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2667 extended
->monitor_timer_interval
=
2668 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2669 extended
->blocking_timeout
=
2670 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2673 case LTTNG_DOMAIN_UST
:
2674 switch (domain
->buf_type
) {
2675 case LTTNG_BUFFER_PER_UID
:
2676 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2677 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2678 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2679 attr
->switch_timer_interval
=
2680 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2681 attr
->read_timer_interval
=
2682 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2684 extended
->monitor_timer_interval
=
2685 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2686 extended
->blocking_timeout
=
2687 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2690 case LTTNG_BUFFER_PER_PID
:
2692 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2693 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2694 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2695 attr
->switch_timer_interval
=
2696 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2697 attr
->read_timer_interval
=
2698 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2700 extended
->monitor_timer_interval
=
2701 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2702 extended
->blocking_timeout
=
2703 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2708 /* Default behavior: leave set to 0. */
2712 attr
->extended
.ptr
= extended
;
2715 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2716 uint64_t *discarded_events
)
2719 struct lttng_channel_extended
*chan_ext
;
2721 if (!channel
|| !discarded_events
) {
2722 ret
= -LTTNG_ERR_INVALID
;
2726 chan_ext
= channel
->attr
.extended
.ptr
;
2729 * This can happen since the lttng_channel structure is
2730 * used for other tasks where this pointer is never set.
2732 *discarded_events
= 0;
2736 *discarded_events
= chan_ext
->discarded_events
;
2741 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2742 uint64_t *lost_packets
)
2745 struct lttng_channel_extended
*chan_ext
;
2747 if (!channel
|| !lost_packets
) {
2748 ret
= -LTTNG_ERR_INVALID
;
2752 chan_ext
= channel
->attr
.extended
.ptr
;
2755 * This can happen since the lttng_channel structure is
2756 * used for other tasks where this pointer is never set.
2762 *lost_packets
= chan_ext
->lost_packets
;
2767 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2768 uint64_t *monitor_timer_interval
)
2772 if (!chan
|| !monitor_timer_interval
) {
2773 ret
= -LTTNG_ERR_INVALID
;
2777 if (!chan
->attr
.extended
.ptr
) {
2778 ret
= -LTTNG_ERR_INVALID
;
2782 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2783 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2788 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2789 uint64_t monitor_timer_interval
)
2793 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2794 ret
= -LTTNG_ERR_INVALID
;
2798 ((struct lttng_channel_extended
*)
2799 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2800 monitor_timer_interval
;
2805 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2806 int64_t *blocking_timeout
)
2810 if (!chan
|| !blocking_timeout
) {
2811 ret
= -LTTNG_ERR_INVALID
;
2815 if (!chan
->attr
.extended
.ptr
) {
2816 ret
= -LTTNG_ERR_INVALID
;
2820 *blocking_timeout
= ((struct lttng_channel_extended
*)
2821 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2826 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2827 int64_t blocking_timeout
)
2830 int64_t msec_timeout
;
2832 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2833 ret
= -LTTNG_ERR_INVALID
;
2837 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2838 ret
= -LTTNG_ERR_INVALID
;
2843 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2844 * us to accept a narrower range of values (msecs expressed as a signed
2847 msec_timeout
= blocking_timeout
/ 1000;
2848 if (msec_timeout
!= (int32_t) msec_timeout
) {
2849 ret
= -LTTNG_ERR_INVALID
;
2853 ((struct lttng_channel_extended
*)
2854 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2861 * Check if session daemon is alive.
2863 * Return 1 if alive or 0 if not.
2864 * On error returns a negative value.
2866 int lttng_session_daemon_alive(void)
2870 ret
= set_session_daemon_path();
2876 if (*sessiond_sock_path
== '\0') {
2878 * No socket path set. Weird error which means the constructor
2884 ret
= try_connect_sessiond(sessiond_sock_path
);
2895 * Set URL for a consumer for a session and domain.
2897 * Return 0 on success, else a negative value.
2899 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2900 const char *control_url
, const char *data_url
)
2904 struct lttcomm_session_msg lsm
;
2905 struct lttng_uri
*uris
= NULL
;
2907 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2908 ret
= -LTTNG_ERR_INVALID
;
2912 memset(&lsm
, 0, sizeof(lsm
));
2914 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2916 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2917 sizeof(lsm
.session
.name
));
2919 ret
= -LTTNG_ERR_INVALID
;
2923 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2925 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2927 ret
= -LTTNG_ERR_INVALID
;
2931 lsm
.u
.uri
.size
= size
;
2933 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2934 sizeof(struct lttng_uri
) * size
, NULL
);
2944 int lttng_enable_consumer(struct lttng_handle
*handle
);
2945 int lttng_enable_consumer(struct lttng_handle
*handle
)
2953 int lttng_disable_consumer(struct lttng_handle
*handle
);
2954 int lttng_disable_consumer(struct lttng_handle
*handle
)
2962 int _lttng_create_session_ext(const char *name
, const char *url
,
2963 const char *datetime
);
2964 int _lttng_create_session_ext(const char *name
, const char *url
,
2965 const char *datetime
)
2971 * For a given session name, this call checks if the data is ready to be read
2972 * or is still being extracted by the consumer(s) hence not ready to be used by
2975 int lttng_data_pending(const char *session_name
)
2978 struct lttcomm_session_msg lsm
;
2979 uint8_t *pending
= NULL
;
2981 if (session_name
== NULL
) {
2982 return -LTTNG_ERR_INVALID
;
2985 memset(&lsm
, 0, sizeof(lsm
));
2986 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2988 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2989 sizeof(lsm
.session
.name
));
2991 ret
= -LTTNG_ERR_INVALID
;
2995 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2998 } else if (ret
!= 1) {
2999 /* Unexpected payload size */
3000 ret
= -LTTNG_ERR_INVALID
;
3002 } else if (!pending
) {
3003 /* Internal error. */
3004 ret
= -LTTNG_ERR_UNK
;
3008 ret
= (int) *pending
;
3015 * Regenerate the metadata for a session.
3016 * Return 0 on success, a negative error code on error.
3018 int lttng_regenerate_metadata(const char *session_name
)
3021 struct lttcomm_session_msg lsm
;
3023 if (!session_name
) {
3024 ret
= -LTTNG_ERR_INVALID
;
3028 memset(&lsm
, 0, sizeof(lsm
));
3029 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3031 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3032 sizeof(lsm
.session
.name
));
3034 ret
= -LTTNG_ERR_INVALID
;
3038 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3049 * Deprecated, replaced by lttng_regenerate_metadata.
3051 int lttng_metadata_regenerate(const char *session_name
)
3053 return lttng_regenerate_metadata(session_name
);
3057 * Regenerate the statedump of a session.
3058 * Return 0 on success, a negative error code on error.
3060 int lttng_regenerate_statedump(const char *session_name
)
3063 struct lttcomm_session_msg lsm
;
3065 if (!session_name
) {
3066 ret
= -LTTNG_ERR_INVALID
;
3070 memset(&lsm
, 0, sizeof(lsm
));
3071 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3073 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3074 sizeof(lsm
.session
.name
));
3076 ret
= -LTTNG_ERR_INVALID
;
3080 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3091 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3095 struct lttcomm_session_msg lsm
= {
3096 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3097 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
3099 struct lttcomm_session_msg
*message_lsm
;
3100 struct lttng_payload message
;
3101 struct lttng_payload reply
;
3102 struct lttng_trigger
*reply_trigger
= NULL
;
3103 enum lttng_domain_type domain_type
;
3104 const struct lttng_credentials user_creds
= {
3105 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3106 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3108 const char *unused_trigger_name
= NULL
;
3109 enum lttng_trigger_status trigger_status
;
3111 lttng_payload_init(&message
);
3112 lttng_payload_init(&reply
);
3115 ret
= -LTTNG_ERR_INVALID
;
3119 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3120 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3121 /* Re-using already registered trigger. */
3122 ret
= -LTTNG_ERR_INVALID
;
3127 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3128 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3129 ret
= -LTTNG_ERR_NOMEM
;
3134 if (!trigger
->creds
.uid
.is_set
) {
3135 /* Use the client's credentials as the trigger credentials. */
3136 lttng_trigger_set_credentials(trigger
, &user_creds
);
3139 * Validate that either the current trigger credentials and the
3140 * client credentials are identical or that the current user is
3141 * root. The root user can register, unregister triggers for
3142 * himself and other users.
3144 * This check is also present on the sessiond side, using the
3145 * credentials passed on the socket. These check are all
3148 const struct lttng_credentials
*trigger_creds
=
3149 lttng_trigger_get_credentials(trigger
);
3151 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3152 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3153 ret
= -LTTNG_ERR_EPERM
;
3154 goto end_unset_name
;
3159 if (!lttng_trigger_validate(trigger
)) {
3160 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3161 goto end_unset_name
;
3164 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3167 lsm
.domain
.type
= domain_type
;
3169 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3171 ret
= -LTTNG_ERR_NOMEM
;
3172 goto end_unset_name
;
3175 ret
= lttng_trigger_serialize(trigger
, &message
);
3177 ret
= -LTTNG_ERR_UNK
;
3178 goto end_unset_name
;
3182 * This is needed to populate the trigger object size for the command
3185 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3187 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3190 struct lttng_payload_view message_view
=
3191 lttng_payload_view_from_payload(
3194 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3196 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3198 goto end_unset_name
;
3203 struct lttng_payload_view reply_view
=
3204 lttng_payload_view_from_payload(
3205 &reply
, 0, reply
.buffer
.size
);
3207 ret
= lttng_trigger_create_from_payload(
3208 &reply_view
, &reply_trigger
);
3210 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3211 goto end_unset_name
;
3215 if (name
|| generate_name
) {
3216 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3218 ret
= -LTTNG_ERR_NOMEM
;
3227 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3228 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3229 ret
= -LTTNG_ERR_UNK
;
3232 lttng_payload_reset(&message
);
3233 lttng_payload_reset(&reply
);
3234 lttng_trigger_destroy(reply_trigger
);
3238 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3240 /* Register an anonymous trigger. */
3241 return _lttng_register_trigger(trigger
, NULL
, false);
3244 enum lttng_error_code
lttng_register_trigger_with_name(
3245 struct lttng_trigger
*trigger
, const char *name
)
3247 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3249 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3252 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3253 struct lttng_trigger
*trigger
)
3255 const int ret
= _lttng_register_trigger(trigger
, false, true);
3257 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3260 enum lttng_error_code
lttng_error_query_execute(
3261 const struct lttng_error_query
*query
,
3262 const struct lttng_endpoint
*endpoint
,
3263 struct lttng_error_query_results
**results
)
3266 enum lttng_error_code ret_code
;
3267 struct lttcomm_session_msg lsm
= {
3268 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3270 struct lttng_payload message
;
3271 struct lttng_payload reply
;
3272 struct lttcomm_session_msg
*message_lsm
;
3274 lttng_payload_init(&message
);
3275 lttng_payload_init(&reply
);
3277 if (!query
|| !results
) {
3278 ret_code
= LTTNG_ERR_INVALID
;
3282 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3283 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3287 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3289 ret_code
= LTTNG_ERR_NOMEM
;
3293 ret
= lttng_error_query_serialize(query
, &message
);
3295 ret_code
= LTTNG_ERR_UNK
;
3299 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3300 message_lsm
->u
.error_query
.length
=
3301 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3304 struct lttng_payload_view message_view
=
3305 lttng_payload_view_from_payload(
3308 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3310 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3318 ssize_t reply_create_ret
;
3319 struct lttng_payload_view reply_view
=
3320 lttng_payload_view_from_payload(
3321 &reply
, 0, reply
.buffer
.size
);
3323 reply_create_ret
= lttng_error_query_results_create_from_payload(
3324 &reply_view
, results
);
3325 if (reply_create_ret
< 0) {
3326 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3331 ret_code
= LTTNG_OK
;
3333 lttng_payload_reset(&message
);
3334 lttng_payload_reset(&reply
);
3338 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3341 struct lttcomm_session_msg lsm
;
3342 struct lttcomm_session_msg
*message_lsm
;
3343 struct lttng_payload message
;
3344 struct lttng_payload reply
;
3345 struct lttng_trigger
*copy
= NULL
;
3346 const struct lttng_credentials user_creds
= {
3347 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3348 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3351 lttng_payload_init(&message
);
3352 lttng_payload_init(&reply
);
3355 ret
= -LTTNG_ERR_INVALID
;
3359 copy
= lttng_trigger_copy(trigger
);
3361 ret
= -LTTNG_ERR_UNK
;
3365 if (!copy
->creds
.uid
.is_set
) {
3366 /* Use the client credentials as the trigger credentials */
3367 lttng_trigger_set_credentials(copy
, &user_creds
);
3370 * Validate that either the current trigger credentials and the
3371 * client credentials are identical or that the current user is
3372 * root. The root user can register, unregister triggers for
3373 * himself and other users.
3375 * This check is also present on the sessiond side, using the
3376 * credentials passed on the socket. These check are all
3379 const struct lttng_credentials
*trigger_creds
=
3380 lttng_trigger_get_credentials(copy
);
3381 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3382 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3383 ret
= -LTTNG_ERR_EPERM
;
3389 if (!lttng_trigger_validate(copy
)) {
3390 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3394 memset(&lsm
, 0, sizeof(lsm
));
3395 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3397 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3399 ret
= -LTTNG_ERR_NOMEM
;
3403 ret
= lttng_trigger_serialize(copy
, &message
);
3405 ret
= -LTTNG_ERR_UNK
;
3410 * This is needed to populate the trigger object size for the command
3411 * header and number of fds sent.
3413 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3415 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3418 struct lttng_payload_view message_view
=
3419 lttng_payload_view_from_payload(
3423 * Update the message header with the number of fd that will be
3426 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3429 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3437 lttng_trigger_destroy(copy
);
3438 lttng_payload_reset(&message
);
3439 lttng_payload_reset(&reply
);
3444 * Ask the session daemon for all registered triggers for the current user.
3446 * Allocates and return an lttng_triggers set.
3447 * On error, returns a suitable lttng_error_code.
3449 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3452 enum lttng_error_code ret_code
= LTTNG_OK
;
3453 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3454 struct lttng_triggers
*local_triggers
= NULL
;
3455 struct lttng_payload reply
;
3456 struct lttng_payload_view lsm_view
=
3457 lttng_payload_view_init_from_buffer(
3458 (const char *) &lsm
, 0, sizeof(lsm
));
3460 lttng_payload_init(&reply
);
3462 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3464 ret_code
= (enum lttng_error_code
) -ret
;
3469 struct lttng_payload_view reply_view
=
3470 lttng_payload_view_from_payload(
3471 &reply
, 0, reply
.buffer
.size
);
3473 ret
= lttng_triggers_create_from_payload(
3474 &reply_view
, &local_triggers
);
3476 ret_code
= LTTNG_ERR_FATAL
;
3481 *triggers
= local_triggers
;
3482 local_triggers
= NULL
;
3484 lttng_payload_reset(&reply
);
3485 lttng_triggers_destroy(local_triggers
);
3492 static void __attribute__((constructor
)) init(void)
3494 /* Set default session group */
3495 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3501 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3503 free(tracing_group
);