4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
21 #include <common/common.h>
22 #include <common/compat/errno.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/dynamic-array.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/tracker.h>
31 #include <common/unix.h>
32 #include <common/uri.h>
33 #include <common/utils.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/destruction-handle.h>
36 #include <lttng/endpoint.h>
37 #include <lttng/event-internal.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/lttng.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/trigger/trigger-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/lttng-error.h>
46 #include <common/filter/filter-ast.h>
47 #include <common/filter/filter-parser.h>
48 #include <common/filter/filter-bytecode.h>
49 #include <common/filter/memstream.h>
50 #include "lttng-ctl-helper.h"
52 #define COPY_DOMAIN_PACKED(dst, src) \
54 struct lttng_domain _tmp_domain; \
56 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
= -1;
62 static char sessiond_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
80 * Copy domain to lttcomm_session_msg domain.
82 * If domain is unknown, default domain will be the kernel.
85 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
86 struct lttng_domain
*src
)
90 case LTTNG_DOMAIN_KERNEL
:
91 case LTTNG_DOMAIN_UST
:
92 case LTTNG_DOMAIN_JUL
:
93 case LTTNG_DOMAIN_LOG4J
:
94 case LTTNG_DOMAIN_PYTHON
:
95 memcpy(dst
, src
, sizeof(struct lttng_domain
));
98 memset(dst
, 0, sizeof(struct lttng_domain
));
105 * Send lttcomm_session_msg to the session daemon.
107 * On success, returns the number of bytes sent (>=0)
108 * On error, returns -1
110 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
115 ret
= -LTTNG_ERR_NO_SESSIOND
;
119 DBG("LSM cmd type: '%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
)
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
) {
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 assert(reply
->buffer
.size
== 0);
617 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
));
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
), LTTNG_SYMBOL_NAME_LEN
);
1221 /* Add filter expression next. */
1222 if (filter_expression
) {
1223 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1224 filter_expression
, lsm
.u
.enable
.expression_len
);
1229 /* Add filter bytecode next. */
1230 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1231 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1232 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1237 if (ev
->extended
.ptr
) {
1238 struct lttng_event_extended
*ev_ext
=
1239 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1241 if (ev_ext
->probe_location
) {
1243 * lttng_userspace_probe_location_serialize returns the
1244 * number of bytes that was appended to the buffer.
1246 ret
= lttng_userspace_probe_location_serialize(
1247 ev_ext
->probe_location
, &payload
);
1253 * Set the size of the userspace probe location element
1254 * of the buffer so that the receiving side knows where
1257 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1262 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1264 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1271 assert(fd_count
== 0 || fd_count
== 1);
1272 if (fd_count
== 1) {
1273 struct fd_handle
*handle
=
1274 lttng_payload_view_pop_fd_handle(&view
);
1280 fd_to_send
= fd_handle_get_fd(handle
);
1281 fd_handle_put(handle
);
1284 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1285 fd_count
? &fd_to_send
: NULL
, fd_count
,
1286 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1287 view
.buffer
.size
, NULL
, NULL
, 0);
1291 if (filter_expression
&& ctx
) {
1292 filter_bytecode_free(ctx
);
1293 filter_ir_free(ctx
);
1294 filter_parser_ctx_free(ctx
);
1297 if (free_filter_expression
) {
1299 * The filter expression has been replaced and must be freed as
1300 * it is not the original filter expression received as a
1303 free(filter_expression
);
1307 * Return directly to the caller and don't ask the sessiond since
1308 * something went wrong in the parsing of data above.
1310 lttng_payload_reset(&payload
);
1314 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1318 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1319 struct lttng_event
*ev
, const char *channel_name
,
1320 const char *original_filter_expression
)
1322 struct lttcomm_session_msg lsm
;
1325 unsigned int free_filter_expression
= 0;
1326 struct filter_parser_ctx
*ctx
= NULL
;
1328 * Cast as non-const since we may replace the filter expression
1329 * by a dynamically allocated string. Otherwise, the original
1330 * string is not modified.
1332 char *filter_expression
= (char *) original_filter_expression
;
1334 if (handle
== NULL
|| ev
== NULL
) {
1335 ret
= -LTTNG_ERR_INVALID
;
1340 * Empty filter string will always be rejected by the parser
1341 * anyway, so treat this corner-case early to eliminate
1342 * lttng_fmemopen error for 0-byte allocation.
1344 if (filter_expression
&& filter_expression
[0] == '\0') {
1345 ret
= -LTTNG_ERR_INVALID
;
1349 memset(&lsm
, 0, sizeof(lsm
));
1351 /* If no channel name, send empty string. */
1352 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1353 sizeof(lsm
.u
.disable
.channel_name
));
1355 ret
= -LTTNG_ERR_INVALID
;
1359 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1361 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1362 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1364 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1365 sizeof(lsm
.session
.name
));
1367 ret
= -LTTNG_ERR_INVALID
;
1371 lsm
.u
.disable
.bytecode_len
= 0;
1374 * For the JUL domain, a filter is enforced except for the
1375 * disable all event. This is done to avoid having the event in
1376 * all sessions thus filtering by logger name.
1378 if (filter_expression
== NULL
&&
1379 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1380 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1381 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1386 * We have a filter, so we need to set up a variable-length
1387 * memory block from where to send the data.
1390 /* Parse filter expression */
1391 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1392 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1393 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1394 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1395 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1396 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1399 /* Setup JUL filter if needed. */
1400 agent_filter
= set_agent_filter(filter_expression
, ev
);
1401 if (!agent_filter
) {
1402 if (!filter_expression
) {
1404 * No JUL and no filter, just skip
1411 * With a JUL filter, the original filter has
1412 * been added to it thus replace the filter
1415 filter_expression
= agent_filter
;
1416 free_filter_expression
= 1;
1420 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1425 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1426 + bytecode_get_len(&ctx
->bytecode
->b
);
1427 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1430 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1431 + lsm
.u
.disable
.expression_len
);
1433 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1437 /* Add filter expression. */
1438 if (lsm
.u
.disable
.expression_len
!= 0) {
1441 lsm
.u
.disable
.expression_len
);
1443 /* Add filter bytecode next. */
1444 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1446 + lsm
.u
.disable
.expression_len
,
1448 lsm
.u
.disable
.bytecode_len
);
1451 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1452 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1456 if (filter_expression
&& ctx
) {
1457 filter_bytecode_free(ctx
);
1458 filter_ir_free(ctx
);
1459 filter_parser_ctx_free(ctx
);
1462 if (free_filter_expression
) {
1464 * The filter expression has been replaced and must be freed as
1465 * it is not the original filter expression received as a
1468 free(filter_expression
);
1472 * Return directly to the caller and don't ask the sessiond since
1473 * something went wrong in the parsing of data above.
1478 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1483 * Disable event(s) of a channel and domain.
1484 * If no event name is specified, all events are disabled.
1485 * If no channel name is specified, the default 'channel0' is used.
1486 * Returns size of returned session payload data or a negative error code.
1488 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1489 const char *channel_name
)
1492 struct lttng_event ev
;
1494 memset(&ev
, 0, sizeof(ev
));
1496 ev
.type
= LTTNG_EVENT_ALL
;
1497 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1499 ret
= -LTTNG_ERR_INVALID
;
1503 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1508 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1510 struct lttng_channel
*channel
= NULL
;
1511 struct lttng_channel_extended
*extended
= NULL
;
1517 /* Validate domain. */
1518 switch (domain
->type
) {
1519 case LTTNG_DOMAIN_UST
:
1520 switch (domain
->buf_type
) {
1521 case LTTNG_BUFFER_PER_UID
:
1522 case LTTNG_BUFFER_PER_PID
:
1528 case LTTNG_DOMAIN_KERNEL
:
1529 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1537 channel
= zmalloc(sizeof(*channel
));
1542 extended
= zmalloc(sizeof(*extended
));
1547 channel
->attr
.extended
.ptr
= extended
;
1549 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1557 void lttng_channel_destroy(struct lttng_channel
*channel
)
1563 if (channel
->attr
.extended
.ptr
) {
1564 free(channel
->attr
.extended
.ptr
);
1570 * Enable channel per domain
1571 * Returns size of returned session payload data or a negative error code.
1573 int lttng_enable_channel(struct lttng_handle
*handle
,
1574 struct lttng_channel
*in_chan
)
1577 struct lttcomm_session_msg lsm
;
1578 size_t total_buffer_size_needed_per_cpu
= 0;
1580 /* NULL arguments are forbidden. No default values. */
1581 if (handle
== NULL
|| in_chan
== NULL
) {
1582 return -LTTNG_ERR_INVALID
;
1585 memset(&lsm
, 0, sizeof(lsm
));
1586 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1587 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1589 if (!in_chan
->attr
.extended
.ptr
) {
1590 struct lttng_channel
*channel
;
1591 struct lttng_channel_extended
*extended
;
1593 channel
= lttng_channel_create(&handle
->domain
);
1595 return -LTTNG_ERR_NOMEM
;
1599 * Create a new channel in order to use default extended
1602 extended
= (struct lttng_channel_extended
*)
1603 channel
->attr
.extended
.ptr
;
1604 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1605 lttng_channel_destroy(channel
);
1607 struct lttng_channel_extended
*extended
;
1609 extended
= (struct lttng_channel_extended
*)
1610 in_chan
->attr
.extended
.ptr
;
1611 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1615 * Verify that the amount of memory required to create the requested
1616 * buffer is available on the system at the moment.
1618 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1619 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1620 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1621 return -LTTNG_ERR_NOMEM
;
1624 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1625 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1627 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1628 sizeof(lsm
.session
.name
));
1630 ret
= -LTTNG_ERR_INVALID
;
1634 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1640 * All tracing will be stopped for registered events of the channel.
1641 * Returns size of returned session payload data or a negative error code.
1643 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1646 struct lttcomm_session_msg lsm
;
1648 /* Safety check. Both are mandatory. */
1649 if (handle
== NULL
|| name
== NULL
) {
1650 return -LTTNG_ERR_INVALID
;
1653 memset(&lsm
, 0, sizeof(lsm
));
1655 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1657 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1658 sizeof(lsm
.u
.disable
.channel_name
));
1660 ret
= -LTTNG_ERR_INVALID
;
1664 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1666 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1667 sizeof(lsm
.session
.name
));
1669 ret
= -LTTNG_ERR_INVALID
;
1673 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1679 * Lists all available tracepoints of domain.
1680 * Sets the contents of the events array.
1681 * Returns the number of lttng_event entries in events;
1682 * on error, returns a negative value.
1684 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1685 struct lttng_event
**events
)
1688 struct lttcomm_session_msg lsm
;
1690 if (handle
== NULL
) {
1691 return -LTTNG_ERR_INVALID
;
1694 memset(&lsm
, 0, sizeof(lsm
));
1695 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1696 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1698 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1703 return ret
/ sizeof(struct lttng_event
);
1707 * Lists all available tracepoint fields of domain.
1708 * Sets the contents of the event field array.
1709 * Returns the number of lttng_event_field entries in events;
1710 * on error, returns a negative value.
1712 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1713 struct lttng_event_field
**fields
)
1716 struct lttcomm_session_msg lsm
;
1718 if (handle
== NULL
) {
1719 return -LTTNG_ERR_INVALID
;
1722 memset(&lsm
, 0, sizeof(lsm
));
1723 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1724 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1726 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1731 return ret
/ sizeof(struct lttng_event_field
);
1735 * Lists all available kernel system calls. Allocates and sets the contents of
1738 * Returns the number of lttng_event entries in events; on error, returns a
1741 int lttng_list_syscalls(struct lttng_event
**events
)
1744 struct lttcomm_session_msg lsm
;
1747 return -LTTNG_ERR_INVALID
;
1750 memset(&lsm
, 0, sizeof(lsm
));
1751 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1752 /* Force kernel domain for system calls. */
1753 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1755 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1760 return ret
/ sizeof(struct lttng_event
);
1764 * Returns a human readable string describing
1765 * the error code (a negative value).
1767 const char *lttng_strerror(int code
)
1769 return error_get_str(code
);
1772 enum lttng_error_code
lttng_create_session_ext(
1773 struct lttng_session_descriptor
*session_descriptor
)
1775 enum lttng_error_code ret_code
;
1776 struct lttcomm_session_msg lsm
= {
1777 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1780 struct lttng_buffer_view reply_view
;
1782 bool sessiond_must_generate_ouput
;
1783 struct lttng_dynamic_buffer payload
;
1785 size_t descriptor_size
;
1786 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1788 lttng_dynamic_buffer_init(&payload
);
1789 if (!session_descriptor
) {
1790 ret_code
= LTTNG_ERR_INVALID
;
1794 sessiond_must_generate_ouput
=
1795 !lttng_session_descriptor_is_output_destination_initialized(
1796 session_descriptor
);
1797 if (sessiond_must_generate_ouput
) {
1798 const char *home_dir
= utils_get_home_dir();
1799 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1801 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1802 ret_code
= LTTNG_ERR_FATAL
;
1806 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1807 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1810 ret_code
= LTTNG_ERR_NOMEM
;
1815 descriptor_size
= payload
.size
;
1816 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1819 ret_code
= LTTNG_ERR_INVALID
;
1822 descriptor_size
= payload
.size
- descriptor_size
;
1823 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1825 /* Command returns a session descriptor on success. */
1826 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1827 payload
.size
, &reply
);
1828 if (reply_ret
< 0) {
1829 ret_code
= -reply_ret
;
1831 } else if (reply_ret
== 0) {
1832 /* Socket unexpectedly closed by the session daemon. */
1833 ret_code
= LTTNG_ERR_FATAL
;
1837 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1838 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1841 ret_code
= LTTNG_ERR_FATAL
;
1844 ret_code
= LTTNG_OK
;
1845 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1848 lttng_dynamic_buffer_reset(&payload
);
1849 lttng_session_descriptor_destroy(descriptor_reply
);
1854 * Create a new session using name and url for destination.
1856 * Return 0 on success else a negative LTTng error code.
1858 int lttng_create_session(const char *name
, const char *url
)
1862 struct lttng_uri
*uris
= NULL
;
1863 struct lttng_session_descriptor
*descriptor
= NULL
;
1864 enum lttng_error_code ret_code
;
1867 ret
= -LTTNG_ERR_INVALID
;
1871 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1873 ret
= -LTTNG_ERR_INVALID
;
1878 descriptor
= lttng_session_descriptor_create(name
);
1881 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1882 ret
= -LTTNG_ERR_INVALID
;
1885 descriptor
= lttng_session_descriptor_local_create(name
,
1889 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1893 ret
= -LTTNG_ERR_INVALID
;
1897 ret
= -LTTNG_ERR_INVALID
;
1900 ret_code
= lttng_create_session_ext(descriptor
);
1901 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1903 lttng_session_descriptor_destroy(descriptor
);
1909 * Create a session exclusively used for snapshot.
1911 * Return 0 on success else a negative LTTng error code.
1913 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1916 enum lttng_error_code ret_code
;
1918 struct lttng_uri
*uris
= NULL
;
1919 struct lttng_session_descriptor
*descriptor
= NULL
;
1922 ret
= -LTTNG_ERR_INVALID
;
1926 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1928 ret
= -LTTNG_ERR_INVALID
;
1932 * If the user does not specify a custom subdir, use the session name.
1934 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1935 strlen(uris
[0].subdir
) == 0) {
1936 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1939 PERROR("Failed to set session name as network destination sub-directory");
1940 ret
= -LTTNG_ERR_FATAL
;
1942 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1943 /* Truncated output. */
1944 ret
= -LTTNG_ERR_INVALID
;
1951 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1954 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1955 ret
= -LTTNG_ERR_INVALID
;
1958 descriptor
= lttng_session_descriptor_snapshot_local_create(
1963 descriptor
= lttng_session_descriptor_snapshot_network_create(
1969 ret
= -LTTNG_ERR_INVALID
;
1973 ret
= -LTTNG_ERR_INVALID
;
1976 ret_code
= lttng_create_session_ext(descriptor
);
1977 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1979 lttng_session_descriptor_destroy(descriptor
);
1985 * Create a session exclusively used for live.
1987 * Return 0 on success else a negative LTTng error code.
1989 int lttng_create_session_live(const char *name
, const char *url
,
1990 unsigned int timer_interval
)
1993 enum lttng_error_code ret_code
;
1994 struct lttng_session_descriptor
*descriptor
= NULL
;
1997 ret
= -LTTNG_ERR_INVALID
;
2002 descriptor
= lttng_session_descriptor_live_network_create(
2003 name
, url
, NULL
, timer_interval
);
2005 descriptor
= lttng_session_descriptor_live_create(
2006 name
, timer_interval
);
2009 ret
= -LTTNG_ERR_INVALID
;
2012 ret_code
= lttng_create_session_ext(descriptor
);
2013 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2015 lttng_session_descriptor_destroy(descriptor
);
2020 * Stop the session and wait for the data before destroying it
2022 * Return 0 on success else a negative LTTng error code.
2024 int lttng_destroy_session(const char *session_name
)
2027 enum lttng_error_code ret_code
;
2028 enum lttng_destruction_handle_status status
;
2029 struct lttng_destruction_handle
*handle
= NULL
;
2032 * Stop the tracing and wait for the data to be
2035 ret
= _lttng_stop_tracing(session_name
, 1);
2036 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2040 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2041 if (ret_code
!= LTTNG_OK
) {
2042 ret
= (int) -ret_code
;
2047 /* Block until the completion of the destruction of the session. */
2048 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2049 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2050 ret
= -LTTNG_ERR_UNK
;
2054 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2055 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2056 ret
= -LTTNG_ERR_UNK
;
2059 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2061 lttng_destruction_handle_destroy(handle
);
2066 * Destroy the session without waiting for the data.
2068 int lttng_destroy_session_no_wait(const char *session_name
)
2070 enum lttng_error_code ret_code
;
2072 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2073 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2077 * Ask the session daemon for all available sessions.
2078 * Sets the contents of the sessions array.
2079 * Returns the number of lttng_session entries in sessions;
2080 * on error, returns a negative value.
2082 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2085 struct lttcomm_session_msg lsm
;
2086 const size_t session_size
= sizeof(struct lttng_session
) +
2087 sizeof(struct lttng_session_extended
);
2088 size_t session_count
, i
;
2089 struct lttng_session_extended
*sessions_extended_begin
;
2090 struct lttng_session
*sessions
= NULL
;
2092 memset(&lsm
, 0, sizeof(lsm
));
2093 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2094 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2099 ret
= -LTTNG_ERR_FATAL
;
2103 if (ret
% session_size
) {
2104 ret
= -LTTNG_ERR_UNK
;
2106 *out_sessions
= NULL
;
2109 session_count
= (size_t) ret
/ session_size
;
2110 sessions_extended_begin
= (struct lttng_session_extended
*)
2111 (&sessions
[session_count
]);
2113 /* Set extended session info pointers. */
2114 for (i
= 0; i
< session_count
; i
++) {
2115 struct lttng_session
*session
= &sessions
[i
];
2116 struct lttng_session_extended
*extended
=
2117 &(sessions_extended_begin
[i
]);
2119 session
->extended
.ptr
= extended
;
2122 ret
= (int) session_count
;
2123 *out_sessions
= sessions
;
2128 enum lttng_error_code
lttng_session_get_creation_time(
2129 const struct lttng_session
*session
, uint64_t *creation_time
)
2131 enum lttng_error_code ret
= LTTNG_OK
;
2132 struct lttng_session_extended
*extended
;
2134 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2135 ret
= LTTNG_ERR_INVALID
;
2139 extended
= session
->extended
.ptr
;
2140 if (!extended
->creation_time
.is_set
) {
2141 /* Not created on the session daemon yet. */
2142 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2145 *creation_time
= extended
->creation_time
.value
;
2150 int lttng_set_session_shm_path(const char *session_name
,
2151 const char *shm_path
)
2154 struct lttcomm_session_msg lsm
;
2156 if (session_name
== NULL
) {
2157 return -LTTNG_ERR_INVALID
;
2160 memset(&lsm
, 0, sizeof(lsm
));
2161 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2163 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2164 sizeof(lsm
.session
.name
));
2166 ret
= -LTTNG_ERR_INVALID
;
2170 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2171 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2173 ret
= -LTTNG_ERR_INVALID
;
2177 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2183 * Ask the session daemon for all available domains of a session.
2184 * Sets the contents of the domains array.
2185 * Returns the number of lttng_domain entries in domains;
2186 * on error, returns a negative value.
2188 int lttng_list_domains(const char *session_name
,
2189 struct lttng_domain
**domains
)
2192 struct lttcomm_session_msg lsm
;
2194 if (session_name
== NULL
) {
2195 ret
= -LTTNG_ERR_INVALID
;
2199 memset(&lsm
, 0, sizeof(lsm
));
2200 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2202 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2203 sizeof(lsm
.session
.name
));
2205 ret
= -LTTNG_ERR_INVALID
;
2209 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2214 return ret
/ sizeof(struct lttng_domain
);
2220 * Ask the session daemon for all available channels of a session.
2221 * Sets the contents of the channels array.
2222 * Returns the number of lttng_channel entries in channels;
2223 * on error, returns a negative value.
2225 int lttng_list_channels(struct lttng_handle
*handle
,
2226 struct lttng_channel
**channels
)
2229 size_t channel_count
, i
;
2230 const size_t channel_size
= sizeof(struct lttng_channel
) +
2231 sizeof(struct lttng_channel_extended
);
2232 struct lttcomm_session_msg lsm
;
2235 if (handle
== NULL
) {
2236 ret
= -LTTNG_ERR_INVALID
;
2240 memset(&lsm
, 0, sizeof(lsm
));
2241 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2242 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2243 sizeof(lsm
.session
.name
));
2245 ret
= -LTTNG_ERR_INVALID
;
2249 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2251 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2256 if (ret
% channel_size
) {
2257 ret
= -LTTNG_ERR_UNK
;
2262 channel_count
= (size_t) ret
/ channel_size
;
2264 /* Set extended info pointers */
2265 extended_at
= ((void *) *channels
) +
2266 channel_count
* sizeof(struct lttng_channel
);
2267 for (i
= 0; i
< channel_count
; i
++) {
2268 struct lttng_channel
*chan
= &(*channels
)[i
];
2270 chan
->attr
.extended
.ptr
= extended_at
;
2271 extended_at
+= sizeof(struct lttng_channel_extended
);
2274 ret
= (int) channel_count
;
2280 * Ask the session daemon for all available events of a session channel.
2281 * Sets the contents of the events array.
2282 * Returns the number of lttng_event entries in events;
2283 * on error, returns a negative value.
2285 int lttng_list_events(struct lttng_handle
*handle
,
2286 const char *channel_name
, struct lttng_event
**events
)
2289 struct lttcomm_session_msg lsm
= {};
2290 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2291 uint32_t nb_events
, i
;
2292 const void *comm_ext_at
;
2293 struct lttng_dynamic_buffer listing
;
2295 struct lttng_payload payload
;
2296 struct lttng_payload payload_copy
;
2297 struct lttng_payload_view lsm_view
=
2298 lttng_payload_view_init_from_buffer(
2299 (const char *) &lsm
, 0, sizeof(lsm
));
2300 struct lttng_buffer_view cmd_header_view
;
2301 struct lttng_buffer_view cmd_payload_view
;
2302 struct lttng_buffer_view flat_events_view
;
2303 struct lttng_buffer_view ext_view
;
2305 /* Safety check. An handle and channel name are mandatory */
2306 if (handle
== NULL
|| channel_name
== NULL
) {
2307 ret
= -LTTNG_ERR_INVALID
;
2311 lttng_payload_init(&payload
);
2312 lttng_payload_init(&payload_copy
);
2314 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2315 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2316 sizeof(lsm
.session
.name
));
2318 ret
= -LTTNG_ERR_INVALID
;
2322 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2323 sizeof(lsm
.u
.list
.channel_name
));
2325 ret
= -LTTNG_ERR_INVALID
;
2329 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2331 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2337 * A copy of the payload is performed since it will be
2338 * consumed twice. Consuming the same payload twice is invalid since
2339 * it will cause any received file descriptor to become "shared"
2340 * between different instances of the resulting objects.
2342 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2344 ret
= -LTTNG_ERR_NOMEM
;
2348 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2349 &payload
.buffer
, 0, sizeof(*cmd_header
));
2350 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2351 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2355 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2357 /* Set number of events and free command header */
2358 nb_events
= cmd_header
->nb_events
;
2359 if (nb_events
> INT_MAX
) {
2360 ret
= -LTTNG_ERR_OVERFLOW
;
2364 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2365 &payload
.buffer
, sizeof(*cmd_header
), -1);
2368 * The buffer that is returned must contain a "flat" version of
2369 * the events that are returned. In other words, all pointers
2370 * within an lttng_event must point to a location within the returned
2371 * buffer so that the user may free everything by simply calling free()
2372 * on the returned buffer. This is needed in order to maintain API
2375 * A first pass is performed to compute the size of the buffer that
2376 * must be allocated. A second pass is then performed to setup
2377 * the returned events so that their members always point within the
2380 * The layout of the returned buffer is as follows:
2381 * - struct lttng_event[nb_events],
2382 * - nb_events times the following:
2383 * - struct lttng_event_extended,
2384 * - flattened version of userspace_probe_location
2385 * - filter_expression
2387 * - padding to align to 64-bits
2389 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2390 nb_events
* sizeof(struct lttng_event
), -1);
2391 comm_ext_at
= ext_view
.data
;
2392 storage_req
= nb_events
* sizeof(struct lttng_event
);
2394 struct lttng_payload_view payload_view
=
2395 lttng_payload_view_from_payload(&payload
, 0, -1);
2397 for (i
= 0; i
< nb_events
; i
++) {
2398 const struct lttcomm_event_extended_header
*ext_comm
=
2399 (struct lttcomm_event_extended_header
*)
2401 int probe_storage_req
= 0;
2403 comm_ext_at
+= sizeof(*ext_comm
);
2404 comm_ext_at
+= ext_comm
->filter_len
;
2405 comm_ext_at
+= ext_comm
->nb_exclusions
*
2406 LTTNG_SYMBOL_NAME_LEN
;
2408 if (ext_comm
->userspace_probe_location_len
) {
2409 struct lttng_userspace_probe_location
2410 *probe_location
= NULL
;
2411 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2413 (const char *) comm_ext_at
-
2414 payload_view
.buffer
.data
,
2415 ext_comm
->userspace_probe_location_len
);
2417 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2418 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2423 * Create a temporary userspace probe location
2424 * to determine the size needed by a "flattened"
2425 * version of that same probe location.
2427 ret
= lttng_userspace_probe_location_create_from_payload(
2428 &probe_location_view
,
2431 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2435 ret
= lttng_userspace_probe_location_flatten(
2436 probe_location
, NULL
);
2437 lttng_userspace_probe_location_destroy(
2440 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2444 probe_storage_req
= ret
;
2445 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2448 storage_req
+= sizeof(struct lttng_event_extended
);
2449 storage_req
+= ext_comm
->filter_len
;
2450 storage_req
+= ext_comm
->nb_exclusions
*
2451 LTTNG_SYMBOL_NAME_LEN
;
2452 /* Padding to ensure the flat probe is aligned. */
2453 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2454 storage_req
+= probe_storage_req
;
2458 lttng_dynamic_buffer_init(&listing
);
2460 * We must ensure that "listing" is never resized so as to preserve
2461 * the validity of the flattened objects.
2463 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2465 ret
= -LTTNG_ERR_NOMEM
;
2469 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2470 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2471 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2472 nb_events
* sizeof(struct lttng_event
));
2473 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2475 ret
= -LTTNG_ERR_NOMEM
;
2476 goto free_dynamic_buffer
;
2479 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2480 nb_events
* sizeof(struct lttng_event
), -1);
2481 comm_ext_at
= ext_view
.data
;
2484 struct lttng_payload_view payload_copy_view
=
2485 lttng_payload_view_from_payload(
2486 &payload_copy
, 0, -1);
2488 for (i
= 0; i
< nb_events
; i
++) {
2489 struct lttng_event
*event
= (typeof(event
))(
2491 (sizeof(struct lttng_event
) * i
));
2492 const struct lttcomm_event_extended_header
*ext_comm
=
2493 (typeof(ext_comm
)) comm_ext_at
;
2494 struct lttng_event_extended
*event_extended
=
2495 (typeof(event_extended
))(listing
.data
+
2498 /* Insert struct lttng_event_extended. */
2499 ret
= lttng_dynamic_buffer_set_size(&listing
,
2500 listing
.size
+ sizeof(*event_extended
));
2502 ret
= -LTTNG_ERR_NOMEM
;
2503 goto free_dynamic_buffer
;
2505 event
->extended
.ptr
= event_extended
;
2507 comm_ext_at
+= sizeof(*ext_comm
);
2509 /* Insert filter expression. */
2510 if (ext_comm
->filter_len
) {
2511 event_extended
->filter_expression
=
2512 listing
.data
+ listing
.size
;
2513 ret
= lttng_dynamic_buffer_append(&listing
,
2515 ext_comm
->filter_len
);
2517 ret
= -LTTNG_ERR_NOMEM
;
2518 goto free_dynamic_buffer
;
2520 comm_ext_at
+= ext_comm
->filter_len
;
2523 /* Insert exclusions. */
2524 if (ext_comm
->nb_exclusions
) {
2525 event_extended
->exclusions
.count
=
2526 ext_comm
->nb_exclusions
;
2527 event_extended
->exclusions
.strings
=
2528 listing
.data
+ listing
.size
;
2530 ret
= lttng_dynamic_buffer_append(&listing
,
2532 ext_comm
->nb_exclusions
*
2533 LTTNG_SYMBOL_NAME_LEN
);
2535 ret
= -LTTNG_ERR_NOMEM
;
2536 goto free_dynamic_buffer
;
2538 comm_ext_at
+= ext_comm
->nb_exclusions
*
2539 LTTNG_SYMBOL_NAME_LEN
;
2542 /* Insert padding to align to 64-bits. */
2543 ret
= lttng_dynamic_buffer_set_size(&listing
,
2544 ALIGN_TO(listing
.size
,
2547 ret
= -LTTNG_ERR_NOMEM
;
2548 goto free_dynamic_buffer
;
2551 /* Insert flattened userspace probe location. */
2552 if (ext_comm
->userspace_probe_location_len
) {
2553 struct lttng_userspace_probe_location
2554 *probe_location
= NULL
;
2555 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2557 (const char *) comm_ext_at
-
2558 payload_copy_view
.buffer
.data
,
2559 ext_comm
->userspace_probe_location_len
);
2561 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2562 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2563 goto free_dynamic_buffer
;
2566 ret
= lttng_userspace_probe_location_create_from_payload(
2567 &probe_location_view
,
2570 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2571 goto free_dynamic_buffer
;
2574 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2577 ret
= lttng_userspace_probe_location_flatten(
2578 probe_location
, &listing
);
2579 lttng_userspace_probe_location_destroy(
2582 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2583 goto free_dynamic_buffer
;
2586 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2591 /* Don't reset listing buffer as we return its content. */
2592 *events
= (struct lttng_event
*) listing
.data
;
2593 lttng_dynamic_buffer_init(&listing
);
2594 ret
= (int) nb_events
;
2595 free_dynamic_buffer
:
2596 lttng_dynamic_buffer_reset(&listing
);
2598 lttng_payload_reset(&payload
);
2599 lttng_payload_reset(&payload_copy
);
2604 * Sets the tracing_group variable with name.
2605 * This function allocates memory pointed to by tracing_group.
2606 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2608 int lttng_set_tracing_group(const char *name
)
2611 return -LTTNG_ERR_INVALID
;
2614 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2615 return -LTTNG_ERR_FATAL
;
2621 int lttng_calibrate(struct lttng_handle
*handle
,
2622 struct lttng_calibrate
*calibrate
)
2625 * This command was removed in LTTng 2.9.
2627 return -LTTNG_ERR_UND
;
2631 * Set default channel attributes.
2632 * If either or both of the arguments are null, attr content is zeroe'd.
2634 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2635 struct lttng_channel_attr
*attr
)
2637 struct lttng_channel_extended
*extended
;
2640 if (attr
== NULL
|| domain
== NULL
) {
2644 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2645 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2647 /* Same for all domains. */
2648 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2649 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2650 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2652 switch (domain
->type
) {
2653 case LTTNG_DOMAIN_KERNEL
:
2654 attr
->switch_timer_interval
=
2655 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2656 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2657 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2658 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2659 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2661 extended
->monitor_timer_interval
=
2662 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2663 extended
->blocking_timeout
=
2664 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2667 case LTTNG_DOMAIN_UST
:
2668 switch (domain
->buf_type
) {
2669 case LTTNG_BUFFER_PER_UID
:
2670 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2671 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2672 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2673 attr
->switch_timer_interval
=
2674 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2675 attr
->read_timer_interval
=
2676 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2678 extended
->monitor_timer_interval
=
2679 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2680 extended
->blocking_timeout
=
2681 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2684 case LTTNG_BUFFER_PER_PID
:
2686 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2687 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2688 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2689 attr
->switch_timer_interval
=
2690 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2691 attr
->read_timer_interval
=
2692 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2694 extended
->monitor_timer_interval
=
2695 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2696 extended
->blocking_timeout
=
2697 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2702 /* Default behavior: leave set to 0. */
2706 attr
->extended
.ptr
= extended
;
2709 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2710 uint64_t *discarded_events
)
2713 struct lttng_channel_extended
*chan_ext
;
2715 if (!channel
|| !discarded_events
) {
2716 ret
= -LTTNG_ERR_INVALID
;
2720 chan_ext
= channel
->attr
.extended
.ptr
;
2723 * This can happen since the lttng_channel structure is
2724 * used for other tasks where this pointer is never set.
2726 *discarded_events
= 0;
2730 *discarded_events
= chan_ext
->discarded_events
;
2735 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2736 uint64_t *lost_packets
)
2739 struct lttng_channel_extended
*chan_ext
;
2741 if (!channel
|| !lost_packets
) {
2742 ret
= -LTTNG_ERR_INVALID
;
2746 chan_ext
= channel
->attr
.extended
.ptr
;
2749 * This can happen since the lttng_channel structure is
2750 * used for other tasks where this pointer is never set.
2756 *lost_packets
= chan_ext
->lost_packets
;
2761 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2762 uint64_t *monitor_timer_interval
)
2766 if (!chan
|| !monitor_timer_interval
) {
2767 ret
= -LTTNG_ERR_INVALID
;
2771 if (!chan
->attr
.extended
.ptr
) {
2772 ret
= -LTTNG_ERR_INVALID
;
2776 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2777 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2782 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2783 uint64_t monitor_timer_interval
)
2787 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2788 ret
= -LTTNG_ERR_INVALID
;
2792 ((struct lttng_channel_extended
*)
2793 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2794 monitor_timer_interval
;
2799 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2800 int64_t *blocking_timeout
)
2804 if (!chan
|| !blocking_timeout
) {
2805 ret
= -LTTNG_ERR_INVALID
;
2809 if (!chan
->attr
.extended
.ptr
) {
2810 ret
= -LTTNG_ERR_INVALID
;
2814 *blocking_timeout
= ((struct lttng_channel_extended
*)
2815 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2820 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2821 int64_t blocking_timeout
)
2824 int64_t msec_timeout
;
2826 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2827 ret
= -LTTNG_ERR_INVALID
;
2831 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2832 ret
= -LTTNG_ERR_INVALID
;
2837 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2838 * us to accept a narrower range of values (msecs expressed as a signed
2841 msec_timeout
= blocking_timeout
/ 1000;
2842 if (msec_timeout
!= (int32_t) msec_timeout
) {
2843 ret
= -LTTNG_ERR_INVALID
;
2847 ((struct lttng_channel_extended
*)
2848 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2855 * Check if session daemon is alive.
2857 * Return 1 if alive or 0 if not.
2858 * On error returns a negative value.
2860 int lttng_session_daemon_alive(void)
2864 ret
= set_session_daemon_path();
2870 if (*sessiond_sock_path
== '\0') {
2872 * No socket path set. Weird error which means the constructor
2878 ret
= try_connect_sessiond(sessiond_sock_path
);
2889 * Set URL for a consumer for a session and domain.
2891 * Return 0 on success, else a negative value.
2893 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2894 const char *control_url
, const char *data_url
)
2898 struct lttcomm_session_msg lsm
;
2899 struct lttng_uri
*uris
= NULL
;
2901 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2902 ret
= -LTTNG_ERR_INVALID
;
2906 memset(&lsm
, 0, sizeof(lsm
));
2908 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2910 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2911 sizeof(lsm
.session
.name
));
2913 ret
= -LTTNG_ERR_INVALID
;
2917 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2919 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2921 ret
= -LTTNG_ERR_INVALID
;
2925 lsm
.u
.uri
.size
= size
;
2927 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2928 sizeof(struct lttng_uri
) * size
, NULL
);
2938 int lttng_enable_consumer(struct lttng_handle
*handle
);
2939 int lttng_enable_consumer(struct lttng_handle
*handle
)
2947 int lttng_disable_consumer(struct lttng_handle
*handle
);
2948 int lttng_disable_consumer(struct lttng_handle
*handle
)
2956 int _lttng_create_session_ext(const char *name
, const char *url
,
2957 const char *datetime
);
2958 int _lttng_create_session_ext(const char *name
, const char *url
,
2959 const char *datetime
)
2965 * For a given session name, this call checks if the data is ready to be read
2966 * or is still being extracted by the consumer(s) hence not ready to be used by
2969 int lttng_data_pending(const char *session_name
)
2972 struct lttcomm_session_msg lsm
;
2973 uint8_t *pending
= NULL
;
2975 if (session_name
== NULL
) {
2976 return -LTTNG_ERR_INVALID
;
2979 memset(&lsm
, 0, sizeof(lsm
));
2980 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2982 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2983 sizeof(lsm
.session
.name
));
2985 ret
= -LTTNG_ERR_INVALID
;
2989 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2992 } else if (ret
!= 1) {
2993 /* Unexpected payload size */
2994 ret
= -LTTNG_ERR_INVALID
;
2996 } else if (!pending
) {
2997 /* Internal error. */
2998 ret
= -LTTNG_ERR_UNK
;
3002 ret
= (int) *pending
;
3009 * Regenerate the metadata for a session.
3010 * Return 0 on success, a negative error code on error.
3012 int lttng_regenerate_metadata(const char *session_name
)
3015 struct lttcomm_session_msg lsm
;
3017 if (!session_name
) {
3018 ret
= -LTTNG_ERR_INVALID
;
3022 memset(&lsm
, 0, sizeof(lsm
));
3023 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3025 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3026 sizeof(lsm
.session
.name
));
3028 ret
= -LTTNG_ERR_INVALID
;
3032 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3043 * Deprecated, replaced by lttng_regenerate_metadata.
3045 int lttng_metadata_regenerate(const char *session_name
)
3047 return lttng_regenerate_metadata(session_name
);
3051 * Regenerate the statedump of a session.
3052 * Return 0 on success, a negative error code on error.
3054 int lttng_regenerate_statedump(const char *session_name
)
3057 struct lttcomm_session_msg lsm
;
3059 if (!session_name
) {
3060 ret
= -LTTNG_ERR_INVALID
;
3064 memset(&lsm
, 0, sizeof(lsm
));
3065 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3067 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3068 sizeof(lsm
.session
.name
));
3070 ret
= -LTTNG_ERR_INVALID
;
3074 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3084 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3087 struct lttcomm_session_msg lsm
= {
3088 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3090 struct lttcomm_session_msg
*message_lsm
;
3091 struct lttng_payload message
;
3092 struct lttng_payload reply
;
3093 struct lttng_trigger
*reply_trigger
= NULL
;
3094 enum lttng_domain_type domain_type
;
3095 const struct lttng_credentials user_creds
= {
3096 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3097 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3101 lttng_payload_init(&message
);
3102 lttng_payload_init(&reply
);
3105 ret
= -LTTNG_ERR_INVALID
;
3109 if (!trigger
->creds
.uid
.is_set
) {
3110 /* Use the client's credentials as the trigger credentials. */
3111 lttng_trigger_set_credentials(trigger
, &user_creds
);
3114 * Validate that either the current trigger credentials and the
3115 * client credentials are identical or that the current user is
3116 * root. The root user can register, unregister triggers for
3117 * himself and other users.
3119 * This check is also present on the sessiond side, using the
3120 * credentials passed on the socket. These check are all
3123 const struct lttng_credentials
*trigger_creds
=
3124 lttng_trigger_get_credentials(trigger
);
3126 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3127 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3128 ret
= -LTTNG_ERR_EPERM
;
3134 if (!lttng_trigger_validate(trigger
)) {
3135 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3139 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3142 lsm
.domain
.type
= domain_type
;
3144 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3146 ret
= -LTTNG_ERR_NOMEM
;
3150 ret
= lttng_trigger_serialize(trigger
, &message
);
3152 ret
= -LTTNG_ERR_UNK
;
3157 * This is needed to populate the trigger object size for the command
3160 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3162 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3165 struct lttng_payload_view message_view
=
3166 lttng_payload_view_from_payload(
3169 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3171 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3178 struct lttng_payload_view reply_view
=
3179 lttng_payload_view_from_payload(
3180 &reply
, 0, reply
.buffer
.size
);
3182 ret
= lttng_trigger_create_from_payload(
3183 &reply_view
, &reply_trigger
);
3185 ret
= -LTTNG_ERR_FATAL
;
3190 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3192 ret
= -LTTNG_ERR_FATAL
;
3198 lttng_payload_reset(&message
);
3199 lttng_payload_reset(&reply
);
3200 lttng_trigger_destroy(reply_trigger
);
3204 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3207 struct lttcomm_session_msg lsm
;
3208 struct lttcomm_session_msg
*message_lsm
;
3209 struct lttng_payload message
;
3210 struct lttng_payload reply
;
3211 struct lttng_trigger
*copy
= NULL
;
3212 const struct lttng_credentials user_creds
= {
3213 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3214 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3217 lttng_payload_init(&message
);
3218 lttng_payload_init(&reply
);
3221 ret
= -LTTNG_ERR_INVALID
;
3225 copy
= lttng_trigger_copy(trigger
);
3227 ret
= -LTTNG_ERR_UNK
;
3231 if (!copy
->creds
.uid
.is_set
) {
3232 /* Use the client credentials as the trigger credentials */
3233 lttng_trigger_set_credentials(copy
, &user_creds
);
3236 * Validate that either the current trigger credentials and the
3237 * client credentials are identical or that the current user is
3238 * root. The root user can register, unregister triggers for
3239 * himself and other users.
3241 * This check is also present on the sessiond side, using the
3242 * credentials passed on the socket. These check are all
3245 const struct lttng_credentials
*trigger_creds
=
3246 lttng_trigger_get_credentials(copy
);
3247 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3248 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3249 ret
= -LTTNG_ERR_EPERM
;
3255 if (!lttng_trigger_validate(copy
)) {
3256 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3260 memset(&lsm
, 0, sizeof(lsm
));
3261 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3263 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3265 ret
= -LTTNG_ERR_NOMEM
;
3270 * This is needed to populate the trigger object size for the command
3271 * header and number of fds sent.
3273 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3275 ret
= lttng_trigger_serialize(copy
, &message
);
3277 ret
= -LTTNG_ERR_UNK
;
3281 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3284 struct lttng_payload_view message_view
=
3285 lttng_payload_view_from_payload(
3289 * Update the message header with the number of fd that will be
3292 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3295 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3303 lttng_trigger_destroy(copy
);
3304 lttng_payload_reset(&message
);
3305 lttng_payload_reset(&reply
);
3310 * Ask the session daemon for all registered triggers for the current user.
3312 * Allocates and return an lttng_triggers set.
3313 * On error, returns a suitable lttng_error_code.
3315 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3318 enum lttng_error_code ret_code
= LTTNG_OK
;
3319 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3320 struct lttng_triggers
*local_triggers
= NULL
;
3321 struct lttng_payload reply
;
3322 struct lttng_payload_view lsm_view
=
3323 lttng_payload_view_init_from_buffer(
3324 (const char *) &lsm
, 0, sizeof(lsm
));
3326 lttng_payload_init(&reply
);
3328 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3330 ret_code
= (enum lttng_error_code
) -ret
;
3335 struct lttng_payload_view reply_view
=
3336 lttng_payload_view_from_payload(
3337 &reply
, 0, reply
.buffer
.size
);
3339 ret
= lttng_triggers_create_from_payload(
3340 &reply_view
, &local_triggers
);
3342 ret_code
= LTTNG_ERR_FATAL
;
3347 *triggers
= local_triggers
;
3348 local_triggers
= NULL
;
3350 lttng_payload_reset(&reply
);
3351 lttng_triggers_destroy(local_triggers
);
3358 static void __attribute__((constructor
)) init(void)
3360 /* Set default session group */
3361 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3367 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3369 free(tracing_group
);