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 string from src to dst and enforce null terminated byte.
83 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
86 strncpy(dst
, src
, len
);
87 /* Enforce the NULL terminated byte */
95 * Copy domain to lttcomm_session_msg domain.
97 * If domain is unknown, default domain will be the kernel.
100 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
101 struct lttng_domain
*src
)
105 case LTTNG_DOMAIN_KERNEL
:
106 case LTTNG_DOMAIN_UST
:
107 case LTTNG_DOMAIN_JUL
:
108 case LTTNG_DOMAIN_LOG4J
:
109 case LTTNG_DOMAIN_PYTHON
:
110 memcpy(dst
, src
, sizeof(struct lttng_domain
));
113 memset(dst
, 0, sizeof(struct lttng_domain
));
120 * Send lttcomm_session_msg to the session daemon.
122 * On success, returns the number of bytes sent (>=0)
123 * On error, returns -1
125 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
130 ret
= -LTTNG_ERR_NO_SESSIOND
;
134 DBG("LSM cmd type : %d", lsm
->cmd_type
);
136 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
137 sizeof(struct lttcomm_session_msg
));
139 ret
= -LTTNG_ERR_FATAL
;
147 * Send var len data to the session daemon.
149 * On success, returns the number of bytes sent (>=0)
150 * On error, returns -1
152 static int send_session_varlen(const void *data
, size_t len
)
157 ret
= -LTTNG_ERR_NO_SESSIOND
;
166 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
168 ret
= -LTTNG_ERR_FATAL
;
176 * Send file descriptors to the session daemon.
178 * On success, returns the number of bytes sent (>=0)
179 * On error, returns -1
181 static int send_session_fds(const int *fds
, size_t nb_fd
)
186 ret
= -LTTNG_ERR_NO_SESSIOND
;
190 if (!fds
|| !nb_fd
) {
195 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
197 ret
= -LTTNG_ERR_FATAL
;
205 * Receive data from the sessiond socket.
207 * On success, returns the number of bytes received (>=0)
208 * On error, returns a negative lttng_error_code.
210 static int recv_data_sessiond(void *buf
, size_t len
)
217 ret
= -LTTNG_ERR_NO_SESSIOND
;
221 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
223 ret
= -LTTNG_ERR_FATAL
;
224 } else if (ret
== 0) {
225 ret
= -LTTNG_ERR_NO_SESSIOND
;
233 * Receive a payload from the session daemon by appending to an existing
235 * On success, returns the number of bytes received (>=0)
236 * On error, returns a negative lttng_error_code.
238 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
241 const size_t original_payload_size
= payload
->buffer
.size
;
243 ret
= lttng_dynamic_buffer_set_size(
244 &payload
->buffer
, payload
->buffer
.size
+ len
);
246 ret
= -LTTNG_ERR_NOMEM
;
250 ret
= recv_data_sessiond(
251 payload
->buffer
.data
+ original_payload_size
, len
);
257 * Check if we are in the specified group.
259 * If yes return 1, else return -1.
262 int lttng_check_tracing_group(void)
264 gid_t
*grp_list
, tracing_gid
;
265 int grp_list_size
, grp_id
, i
;
267 const char *grp_name
= tracing_group
;
269 /* Get GID of group 'tracing' */
270 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
271 /* If grp_tracing is NULL, the group does not exist. */
275 /* Get number of supplementary group IDs */
276 grp_list_size
= getgroups(0, NULL
);
277 if (grp_list_size
< 0) {
282 /* Alloc group list of the right size */
283 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
288 grp_id
= getgroups(grp_list_size
, grp_list
);
294 for (i
= 0; i
< grp_list_size
; i
++) {
295 if (grp_list
[i
] == tracing_gid
) {
308 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
312 size_t best_mem_info
;
313 size_t num_bytes_requested_total
;
316 * Get the number of CPU currently online to compute the amount of
317 * memory needed to create a buffer for every CPU.
319 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
324 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
327 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
328 * reliable estimate we can get but it is only exposed by the kernel
329 * since 3.14. (See Linux kernel commit:
330 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
332 ret
= utils_get_memory_available(&best_mem_info
);
338 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
339 * is a sanity check for obvious user error.
341 ret
= utils_get_memory_total(&best_mem_info
);
349 return best_mem_info
>= num_bytes_requested_total
;
353 * Try connect to session daemon with sock_path.
355 * Return 0 on success, else -1
357 static int try_connect_sessiond(const char *sock_path
)
361 /* If socket exist, we check if the daemon listens for connect. */
362 ret
= access(sock_path
, F_OK
);
368 ret
= lttcomm_connect_unix_sock(sock_path
);
374 ret
= lttcomm_close_unix_sock(ret
);
376 PERROR("lttcomm_close_unix_sock");
386 * Set sessiond socket path by putting it in the global sessiond_sock_path
389 * Returns 0 on success, negative value on failure (the sessiond socket path
390 * is somehow too long or ENOMEM).
392 static int set_session_daemon_path(void)
394 int in_tgroup
= 0; /* In tracing group. */
400 /* Are we in the tracing group ? */
401 in_tgroup
= lttng_check_tracing_group();
404 if ((uid
== 0) || in_tgroup
) {
405 lttng_ctl_copy_string(sessiond_sock_path
,
406 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
414 ret
= try_connect_sessiond(sessiond_sock_path
);
418 /* Global session daemon not available... */
420 /* ...or not in tracing group (and not root), default */
423 * With GNU C < 2.1, snprintf returns -1 if the target buffer
425 * With GNU C >= 2.1, snprintf returns the required size
426 * (excluding closing null)
428 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
429 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
430 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
442 * Connect to the LTTng session daemon.
444 * On success, return the socket's file descriptor. On error, return -1.
446 LTTNG_HIDDEN
int connect_sessiond(void)
450 ret
= set_session_daemon_path();
455 /* Connect to the sesssion daemon. */
456 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
467 static void reset_global_sessiond_connection_state(void)
469 sessiond_socket
= -1;
474 * Clean disconnect from the session daemon.
476 * On success, return 0. On error, return -1.
478 static int disconnect_sessiond(void)
483 ret
= lttcomm_close_unix_sock(sessiond_socket
);
484 reset_global_sessiond_connection_state();
490 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
498 ret
= -LTTNG_ERR_INVALID
;
508 ret
= recv_data_sessiond(buf
, len
);
514 ret
= -LTTNG_ERR_INVALID
;
518 /* Move ownership of command header buffer to user. */
523 /* No command header. */
539 * Ask the session daemon a specific command and put the data into buf.
540 * Takes extra var. len. data and file descriptors as input to send to the
543 * Return size of data (only payload, not header) or a negative error code.
546 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
547 const int *fds
, size_t nb_fd
, const void *vardata
,
548 size_t vardata_len
, void **user_payload_buf
,
549 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
553 struct lttcomm_lttng_msg llm
;
555 ret
= connect_sessiond();
557 ret
= -LTTNG_ERR_NO_SESSIOND
;
560 sessiond_socket
= ret
;
564 ret
= send_session_msg(lsm
);
566 /* Ret value is a valid lttng error code. */
569 /* Send var len data */
570 ret
= send_session_varlen(vardata
, vardata_len
);
572 /* Ret value is a valid lttng error code. */
577 ret
= send_session_fds(fds
, nb_fd
);
579 /* Ret value is a valid lttng error code. */
583 /* Get header from data transmission */
584 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
586 /* Ret value is a valid lttng error code. */
590 /* Check error code if OK */
591 if (llm
.ret_code
!= LTTNG_OK
) {
596 /* Get command header from data transmission */
597 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
598 user_cmd_header_buf
, user_cmd_header_len
);
603 /* Get payload from data transmission */
604 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
613 disconnect_sessiond();
618 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
619 struct lttng_payload
*reply
)
622 struct lttcomm_lttng_msg llm
;
623 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
625 assert(reply
->buffer
.size
== 0);
626 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
628 ret
= connect_sessiond();
630 ret
= -LTTNG_ERR_NO_SESSIOND
;
633 sessiond_socket
= ret
;
637 /* Send command to session daemon */
638 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
639 message
->buffer
.size
);
641 ret
= -LTTNG_ERR_FATAL
;
646 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
649 ret
= -LTTNG_ERR_FATAL
;
654 /* Get header from data transmission */
655 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
657 /* Ret value is a valid lttng error code. */
661 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
663 /* Check error code if OK */
664 if (llm
.ret_code
!= LTTNG_OK
) {
669 if (llm
.cmd_header_size
> 0) {
670 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
676 /* Get command header from data transmission */
677 if (llm
.data_size
> 0) {
678 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
684 if (llm
.fd_count
> 0) {
685 ret
= lttcomm_recv_payload_fds_unix_sock(
686 sessiond_socket
, llm
.fd_count
, reply
);
692 /* Don't return the llm header to the caller. */
693 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
694 reply
->buffer
.size
- sizeof(llm
));
695 ret
= lttng_dynamic_buffer_set_size(
696 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
698 /* Can't happen as size is reduced. */
702 ret
= reply
->buffer
.size
;
705 disconnect_sessiond();
710 * Create lttng handle and return pointer.
712 * The returned pointer will be NULL in case of malloc() error.
714 struct lttng_handle
*lttng_create_handle(const char *session_name
,
715 struct lttng_domain
*domain
)
717 struct lttng_handle
*handle
= NULL
;
719 handle
= zmalloc(sizeof(struct lttng_handle
));
720 if (handle
== NULL
) {
721 PERROR("malloc handle");
725 /* Copy session name */
726 lttng_ctl_copy_string(handle
->session_name
, session_name
,
727 sizeof(handle
->session_name
));
729 /* Copy lttng domain or leave initialized to 0. */
731 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
739 * Destroy handle by free(3) the pointer.
741 void lttng_destroy_handle(struct lttng_handle
*handle
)
747 * Register an outside consumer.
749 * Returns size of returned session payload data or a negative error code.
751 int lttng_register_consumer(struct lttng_handle
*handle
,
752 const char *socket_path
)
754 struct lttcomm_session_msg lsm
;
756 if (handle
== NULL
|| socket_path
== NULL
) {
757 return -LTTNG_ERR_INVALID
;
760 memset(&lsm
, 0, sizeof(lsm
));
761 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
762 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
763 sizeof(lsm
.session
.name
));
764 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
766 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
767 sizeof(lsm
.u
.reg
.path
));
769 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
773 * Start tracing for all traces of the session.
775 * Returns size of returned session payload data or a negative error code.
777 int lttng_start_tracing(const char *session_name
)
779 struct lttcomm_session_msg lsm
;
781 if (session_name
== NULL
) {
782 return -LTTNG_ERR_INVALID
;
785 memset(&lsm
, 0, sizeof(lsm
));
786 lsm
.cmd_type
= LTTNG_START_TRACE
;
788 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
789 sizeof(lsm
.session
.name
));
791 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
795 * Stop tracing for all traces of the session.
797 static int _lttng_stop_tracing(const char *session_name
, int wait
)
800 struct lttcomm_session_msg lsm
;
802 if (session_name
== NULL
) {
803 return -LTTNG_ERR_INVALID
;
806 memset(&lsm
, 0, sizeof(lsm
));
807 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
809 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
810 sizeof(lsm
.session
.name
));
812 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
813 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
821 /* Check for data availability */
823 data_ret
= lttng_data_pending(session_name
);
825 /* Return the data available call error. */
831 * Data sleep time before retrying (in usec). Don't sleep if the
832 * call returned value indicates availability.
835 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
837 } while (data_ret
!= 0);
845 * Stop tracing and wait for data availability.
847 int lttng_stop_tracing(const char *session_name
)
849 return _lttng_stop_tracing(session_name
, 1);
853 * Stop tracing but _don't_ wait for data availability.
855 int lttng_stop_tracing_no_wait(const char *session_name
)
857 return _lttng_stop_tracing(session_name
, 0);
861 * Add context to a channel.
863 * If the given channel is NULL, add the contexts to all channels.
864 * The event_name param is ignored.
866 * Returns the size of the returned payload data or a negative error code.
868 int lttng_add_context(struct lttng_handle
*handle
,
869 struct lttng_event_context
*ctx
, const char *event_name
,
870 const char *channel_name
)
875 struct lttcomm_session_msg lsm
;
877 /* Safety check. Both are mandatory. */
878 if (handle
== NULL
|| ctx
== NULL
) {
879 ret
= -LTTNG_ERR_INVALID
;
883 memset(&lsm
, 0, sizeof(lsm
));
884 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
886 /* If no channel name, send empty string. */
887 if (channel_name
== NULL
) {
888 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
889 sizeof(lsm
.u
.context
.channel_name
));
891 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
892 sizeof(lsm
.u
.context
.channel_name
));
895 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
896 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
897 sizeof(lsm
.session
.name
));
899 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
900 size_t provider_len
, ctx_len
;
901 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
902 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
904 if (!provider_name
|| !ctx_name
) {
905 ret
= -LTTNG_ERR_INVALID
;
909 provider_len
= strlen(provider_name
);
910 if (provider_len
== 0) {
911 ret
= -LTTNG_ERR_INVALID
;
914 lsm
.u
.context
.provider_name_len
= provider_len
;
916 ctx_len
= strlen(ctx_name
);
918 ret
= -LTTNG_ERR_INVALID
;
921 lsm
.u
.context
.context_name_len
= ctx_len
;
923 len
= provider_len
+ ctx_len
;
926 ret
= -LTTNG_ERR_NOMEM
;
930 memcpy(buf
, provider_name
, provider_len
);
931 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
933 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
935 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
937 * Don't leak application addresses to the sessiond.
938 * This is only necessary when ctx is for an app ctx otherwise
939 * the values inside the union (type & config) are overwritten.
941 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
942 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
945 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
952 * Enable event(s) for a channel.
954 * If no event name is specified, all events are enabled.
955 * If no channel name is specified, the default 'channel0' is used.
957 * Returns size of returned session payload data or a negative error code.
959 int lttng_enable_event(struct lttng_handle
*handle
,
960 struct lttng_event
*ev
, const char *channel_name
)
962 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
967 * Create or enable an event with a filter expression.
969 * Return negative error value on error.
970 * Return size of returned session payload data if OK.
972 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
973 struct lttng_event
*event
, const char *channel_name
,
974 const char *filter_expression
)
976 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
977 filter_expression
, 0, NULL
);
981 * Depending on the event, return a newly allocated agent filter expression or
982 * NULL if not applicable.
984 * An event with NO loglevel and the name is * will return NULL.
986 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
989 char *agent_filter
= NULL
;
993 /* Don't add filter for the '*' event. */
994 if (strcmp(ev
->name
, "*") != 0) {
996 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
999 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1007 /* Add loglevel filtering if any for the JUL domain. */
1008 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1011 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1017 if (filter
|| agent_filter
) {
1020 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1021 agent_filter
? agent_filter
: filter
, op
,
1026 agent_filter
= new_filter
;
1028 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1037 return agent_filter
;
1044 * Enable event(s) for a channel, possibly with exclusions and a filter.
1045 * If no event name is specified, all events are enabled.
1046 * If no channel name is specified, the default name is used.
1047 * If filter expression is not NULL, the filter is set for the event.
1048 * If exclusion count is not zero, the exclusions are set for the event.
1049 * Returns size of returned session payload data or a negative error code.
1051 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1052 struct lttng_event
*ev
, const char *channel_name
,
1053 const char *original_filter_expression
,
1054 int exclusion_count
, char **exclusion_list
)
1056 struct lttcomm_session_msg lsm
;
1057 struct lttng_payload payload
;
1059 unsigned int free_filter_expression
= 0;
1060 struct filter_parser_ctx
*ctx
= NULL
;
1063 * We have either a filter or some exclusions, so we need to set up
1064 * a variable-length payload from where to send the data.
1066 lttng_payload_init(&payload
);
1069 * Cast as non-const since we may replace the filter expression
1070 * by a dynamically allocated string. Otherwise, the original
1071 * string is not modified.
1073 char *filter_expression
= (char *) original_filter_expression
;
1075 if (handle
== NULL
|| ev
== NULL
) {
1076 ret
= -LTTNG_ERR_INVALID
;
1081 * Empty filter string will always be rejected by the parser
1082 * anyway, so treat this corner-case early to eliminate
1083 * lttng_fmemopen error for 0-byte allocation.
1085 if (filter_expression
&& filter_expression
[0] == '\0') {
1086 ret
= -LTTNG_ERR_INVALID
;
1090 memset(&lsm
, 0, sizeof(lsm
));
1092 /* If no channel name, send empty string. */
1093 if (channel_name
== NULL
) {
1094 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1095 sizeof(lsm
.u
.enable
.channel_name
));
1097 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1098 sizeof(lsm
.u
.enable
.channel_name
));
1101 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1102 if (ev
->name
[0] == '\0') {
1103 /* Enable all events */
1104 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1107 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1108 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1110 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1111 sizeof(lsm
.session
.name
));
1112 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1113 lsm
.u
.enable
.bytecode_len
= 0;
1115 /* Parse filter expression. */
1116 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1117 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1118 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1119 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1120 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1121 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1124 /* Setup JUL filter if needed. */
1125 agent_filter
= set_agent_filter(filter_expression
, ev
);
1126 if (!agent_filter
) {
1127 if (!filter_expression
) {
1129 * No JUL and no filter, just skip
1136 * With an agent filter, the original filter has
1137 * been added to it thus replace the filter
1140 filter_expression
= agent_filter
;
1141 free_filter_expression
= 1;
1145 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1150 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1151 + bytecode_get_len(&ctx
->bytecode
->b
);
1152 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1155 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1156 lsm
.u
.enable
.bytecode_len
+
1157 lsm
.u
.enable
.expression_len
+
1158 LTTNG_SYMBOL_NAME_LEN
*
1161 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1165 /* Put exclusion names first in the data. */
1166 for (i
= 0; i
< exclusion_count
; i
++) {
1167 size_t exclusion_len
;
1169 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1170 LTTNG_SYMBOL_NAME_LEN
);
1171 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1172 /* Exclusion is not NULL-terminated. */
1173 ret
= -LTTNG_ERR_INVALID
;
1177 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1178 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1184 /* Add filter expression next. */
1185 if (filter_expression
) {
1186 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1187 filter_expression
, lsm
.u
.enable
.expression_len
);
1192 /* Add filter bytecode next. */
1193 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1194 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1195 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1200 if (ev
->extended
.ptr
) {
1201 struct lttng_event_extended
*ev_ext
=
1202 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1204 if (ev_ext
->probe_location
) {
1206 * lttng_userspace_probe_location_serialize returns the
1207 * number of bytes that was appended to the buffer.
1209 ret
= lttng_userspace_probe_location_serialize(
1210 ev_ext
->probe_location
, &payload
);
1216 * Set the size of the userspace probe location element
1217 * of the buffer so that the receiving side knows where
1220 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1225 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1227 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1234 assert(fd_count
== 0 || fd_count
== 1);
1235 if (fd_count
== 1) {
1236 struct fd_handle
*handle
=
1237 lttng_payload_view_pop_fd_handle(&view
);
1243 fd_to_send
= fd_handle_get_fd(handle
);
1244 fd_handle_put(handle
);
1247 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1248 fd_count
? &fd_to_send
: NULL
, fd_count
,
1249 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1250 view
.buffer
.size
, NULL
, NULL
, 0);
1254 if (filter_expression
&& ctx
) {
1255 filter_bytecode_free(ctx
);
1256 filter_ir_free(ctx
);
1257 filter_parser_ctx_free(ctx
);
1260 if (free_filter_expression
) {
1262 * The filter expression has been replaced and must be freed as
1263 * it is not the original filter expression received as a
1266 free(filter_expression
);
1270 * Return directly to the caller and don't ask the sessiond since
1271 * something went wrong in the parsing of data above.
1273 lttng_payload_reset(&payload
);
1277 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1281 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1282 struct lttng_event
*ev
, const char *channel_name
,
1283 const char *original_filter_expression
)
1285 struct lttcomm_session_msg lsm
;
1288 unsigned int free_filter_expression
= 0;
1289 struct filter_parser_ctx
*ctx
= NULL
;
1291 * Cast as non-const since we may replace the filter expression
1292 * by a dynamically allocated string. Otherwise, the original
1293 * string is not modified.
1295 char *filter_expression
= (char *) original_filter_expression
;
1297 if (handle
== NULL
|| ev
== NULL
) {
1298 ret
= -LTTNG_ERR_INVALID
;
1303 * Empty filter string will always be rejected by the parser
1304 * anyway, so treat this corner-case early to eliminate
1305 * lttng_fmemopen error for 0-byte allocation.
1307 if (filter_expression
&& filter_expression
[0] == '\0') {
1308 ret
= -LTTNG_ERR_INVALID
;
1312 memset(&lsm
, 0, sizeof(lsm
));
1314 /* If no channel name, send empty string. */
1315 if (channel_name
== NULL
) {
1316 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1317 sizeof(lsm
.u
.disable
.channel_name
));
1319 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1320 sizeof(lsm
.u
.disable
.channel_name
));
1323 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1325 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1326 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1328 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1329 sizeof(lsm
.session
.name
));
1330 lsm
.u
.disable
.bytecode_len
= 0;
1333 * For the JUL domain, a filter is enforced except for the
1334 * disable all event. This is done to avoid having the event in
1335 * all sessions thus filtering by logger name.
1337 if (filter_expression
== NULL
&&
1338 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1339 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1340 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1345 * We have a filter, so we need to set up a variable-length
1346 * memory block from where to send the data.
1349 /* Parse filter expression */
1350 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1351 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1352 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1353 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1354 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1355 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1358 /* Setup JUL filter if needed. */
1359 agent_filter
= set_agent_filter(filter_expression
, ev
);
1360 if (!agent_filter
) {
1361 if (!filter_expression
) {
1363 * No JUL and no filter, just skip
1370 * With a JUL filter, the original filter has
1371 * been added to it thus replace the filter
1374 filter_expression
= agent_filter
;
1375 free_filter_expression
= 1;
1379 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1384 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1385 + bytecode_get_len(&ctx
->bytecode
->b
);
1386 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1389 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1390 + lsm
.u
.disable
.expression_len
);
1392 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1396 /* Add filter expression. */
1397 if (lsm
.u
.disable
.expression_len
!= 0) {
1400 lsm
.u
.disable
.expression_len
);
1402 /* Add filter bytecode next. */
1403 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1405 + lsm
.u
.disable
.expression_len
,
1407 lsm
.u
.disable
.bytecode_len
);
1410 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1411 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1415 if (filter_expression
&& ctx
) {
1416 filter_bytecode_free(ctx
);
1417 filter_ir_free(ctx
);
1418 filter_parser_ctx_free(ctx
);
1421 if (free_filter_expression
) {
1423 * The filter expression has been replaced and must be freed as
1424 * it is not the original filter expression received as a
1427 free(filter_expression
);
1431 * Return directly to the caller and don't ask the sessiond since
1432 * something went wrong in the parsing of data above.
1437 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1442 * Disable event(s) of a channel and domain.
1443 * If no event name is specified, all events are disabled.
1444 * If no channel name is specified, the default 'channel0' is used.
1445 * Returns size of returned session payload data or a negative error code.
1447 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1448 const char *channel_name
)
1450 struct lttng_event ev
;
1452 memset(&ev
, 0, sizeof(ev
));
1454 ev
.type
= LTTNG_EVENT_ALL
;
1455 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1456 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1459 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1461 struct lttng_channel
*channel
= NULL
;
1462 struct lttng_channel_extended
*extended
= NULL
;
1468 /* Validate domain. */
1469 switch (domain
->type
) {
1470 case LTTNG_DOMAIN_UST
:
1471 switch (domain
->buf_type
) {
1472 case LTTNG_BUFFER_PER_UID
:
1473 case LTTNG_BUFFER_PER_PID
:
1479 case LTTNG_DOMAIN_KERNEL
:
1480 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1488 channel
= zmalloc(sizeof(*channel
));
1493 extended
= zmalloc(sizeof(*extended
));
1498 channel
->attr
.extended
.ptr
= extended
;
1500 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1508 void lttng_channel_destroy(struct lttng_channel
*channel
)
1514 if (channel
->attr
.extended
.ptr
) {
1515 free(channel
->attr
.extended
.ptr
);
1521 * Enable channel per domain
1522 * Returns size of returned session payload data or a negative error code.
1524 int lttng_enable_channel(struct lttng_handle
*handle
,
1525 struct lttng_channel
*in_chan
)
1527 struct lttcomm_session_msg lsm
;
1528 size_t total_buffer_size_needed_per_cpu
= 0;
1530 /* NULL arguments are forbidden. No default values. */
1531 if (handle
== NULL
|| in_chan
== NULL
) {
1532 return -LTTNG_ERR_INVALID
;
1535 memset(&lsm
, 0, sizeof(lsm
));
1536 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1537 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1539 if (!in_chan
->attr
.extended
.ptr
) {
1540 struct lttng_channel
*channel
;
1541 struct lttng_channel_extended
*extended
;
1543 channel
= lttng_channel_create(&handle
->domain
);
1545 return -LTTNG_ERR_NOMEM
;
1549 * Create a new channel in order to use default extended
1552 extended
= (struct lttng_channel_extended
*)
1553 channel
->attr
.extended
.ptr
;
1554 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1555 lttng_channel_destroy(channel
);
1557 struct lttng_channel_extended
*extended
;
1559 extended
= (struct lttng_channel_extended
*)
1560 in_chan
->attr
.extended
.ptr
;
1561 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1565 * Verify that the amount of memory required to create the requested
1566 * buffer is available on the system at the moment.
1568 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1569 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1570 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1571 return -LTTNG_ERR_NOMEM
;
1574 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1575 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1577 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1578 sizeof(lsm
.session
.name
));
1580 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1584 * All tracing will be stopped for registered events of the channel.
1585 * Returns size of returned session payload data or a negative error code.
1587 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1589 struct lttcomm_session_msg lsm
;
1591 /* Safety check. Both are mandatory. */
1592 if (handle
== NULL
|| name
== NULL
) {
1593 return -LTTNG_ERR_INVALID
;
1596 memset(&lsm
, 0, sizeof(lsm
));
1598 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1600 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1601 sizeof(lsm
.u
.disable
.channel_name
));
1603 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1605 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1606 sizeof(lsm
.session
.name
));
1608 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1612 * Lists all available tracepoints of domain.
1613 * Sets the contents of the events array.
1614 * Returns the number of lttng_event entries in events;
1615 * on error, returns a negative value.
1617 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1618 struct lttng_event
**events
)
1621 struct lttcomm_session_msg lsm
;
1623 if (handle
== NULL
) {
1624 return -LTTNG_ERR_INVALID
;
1627 memset(&lsm
, 0, sizeof(lsm
));
1628 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1629 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1631 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1636 return ret
/ sizeof(struct lttng_event
);
1640 * Lists all available tracepoint fields of domain.
1641 * Sets the contents of the event field array.
1642 * Returns the number of lttng_event_field entries in events;
1643 * on error, returns a negative value.
1645 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1646 struct lttng_event_field
**fields
)
1649 struct lttcomm_session_msg lsm
;
1651 if (handle
== NULL
) {
1652 return -LTTNG_ERR_INVALID
;
1655 memset(&lsm
, 0, sizeof(lsm
));
1656 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1657 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1659 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1664 return ret
/ sizeof(struct lttng_event_field
);
1668 * Lists all available kernel system calls. Allocates and sets the contents of
1671 * Returns the number of lttng_event entries in events; on error, returns a
1674 int lttng_list_syscalls(struct lttng_event
**events
)
1677 struct lttcomm_session_msg lsm
;
1680 return -LTTNG_ERR_INVALID
;
1683 memset(&lsm
, 0, sizeof(lsm
));
1684 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1685 /* Force kernel domain for system calls. */
1686 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1688 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1693 return ret
/ sizeof(struct lttng_event
);
1697 * Returns a human readable string describing
1698 * the error code (a negative value).
1700 const char *lttng_strerror(int code
)
1702 return error_get_str(code
);
1705 enum lttng_error_code
lttng_create_session_ext(
1706 struct lttng_session_descriptor
*session_descriptor
)
1708 enum lttng_error_code ret_code
;
1709 struct lttcomm_session_msg lsm
= {
1710 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1713 struct lttng_buffer_view reply_view
;
1715 bool sessiond_must_generate_ouput
;
1716 struct lttng_dynamic_buffer payload
;
1718 size_t descriptor_size
;
1719 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1721 lttng_dynamic_buffer_init(&payload
);
1722 if (!session_descriptor
) {
1723 ret_code
= LTTNG_ERR_INVALID
;
1727 sessiond_must_generate_ouput
=
1728 !lttng_session_descriptor_is_output_destination_initialized(
1729 session_descriptor
);
1730 if (sessiond_must_generate_ouput
) {
1731 const char *home_dir
= utils_get_home_dir();
1732 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1734 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1735 ret_code
= LTTNG_ERR_FATAL
;
1739 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1740 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1743 ret_code
= LTTNG_ERR_NOMEM
;
1748 descriptor_size
= payload
.size
;
1749 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1752 ret_code
= LTTNG_ERR_INVALID
;
1755 descriptor_size
= payload
.size
- descriptor_size
;
1756 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1758 /* Command returns a session descriptor on success. */
1759 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1760 payload
.size
, &reply
);
1761 if (reply_ret
< 0) {
1762 ret_code
= -reply_ret
;
1764 } else if (reply_ret
== 0) {
1765 /* Socket unexpectedly closed by the session daemon. */
1766 ret_code
= LTTNG_ERR_FATAL
;
1770 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1771 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1774 ret_code
= LTTNG_ERR_FATAL
;
1777 ret_code
= LTTNG_OK
;
1778 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1781 lttng_dynamic_buffer_reset(&payload
);
1782 lttng_session_descriptor_destroy(descriptor_reply
);
1787 * Create a new session using name and url for destination.
1789 * Return 0 on success else a negative LTTng error code.
1791 int lttng_create_session(const char *name
, const char *url
)
1795 struct lttng_uri
*uris
= NULL
;
1796 struct lttng_session_descriptor
*descriptor
= NULL
;
1797 enum lttng_error_code ret_code
;
1800 ret
= -LTTNG_ERR_INVALID
;
1804 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1806 ret
= -LTTNG_ERR_INVALID
;
1811 descriptor
= lttng_session_descriptor_create(name
);
1814 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1815 ret
= -LTTNG_ERR_INVALID
;
1818 descriptor
= lttng_session_descriptor_local_create(name
,
1822 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1826 ret
= -LTTNG_ERR_INVALID
;
1830 ret
= -LTTNG_ERR_INVALID
;
1833 ret_code
= lttng_create_session_ext(descriptor
);
1834 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1836 lttng_session_descriptor_destroy(descriptor
);
1842 * Create a session exclusively used for snapshot.
1844 * Return 0 on success else a negative LTTng error code.
1846 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1849 enum lttng_error_code ret_code
;
1851 struct lttng_uri
*uris
= NULL
;
1852 struct lttng_session_descriptor
*descriptor
= NULL
;
1855 ret
= -LTTNG_ERR_INVALID
;
1859 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1861 ret
= -LTTNG_ERR_INVALID
;
1865 * If the user does not specify a custom subdir, use the session name.
1867 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1868 strlen(uris
[0].subdir
) == 0) {
1869 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1872 PERROR("Failed to set session name as network destination sub-directory");
1873 ret
= -LTTNG_ERR_FATAL
;
1875 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1876 /* Truncated output. */
1877 ret
= -LTTNG_ERR_INVALID
;
1884 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1887 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1888 ret
= -LTTNG_ERR_INVALID
;
1891 descriptor
= lttng_session_descriptor_snapshot_local_create(
1896 descriptor
= lttng_session_descriptor_snapshot_network_create(
1902 ret
= -LTTNG_ERR_INVALID
;
1906 ret
= -LTTNG_ERR_INVALID
;
1909 ret_code
= lttng_create_session_ext(descriptor
);
1910 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1912 lttng_session_descriptor_destroy(descriptor
);
1918 * Create a session exclusively used for live.
1920 * Return 0 on success else a negative LTTng error code.
1922 int lttng_create_session_live(const char *name
, const char *url
,
1923 unsigned int timer_interval
)
1926 enum lttng_error_code ret_code
;
1927 struct lttng_session_descriptor
*descriptor
= NULL
;
1930 ret
= -LTTNG_ERR_INVALID
;
1935 descriptor
= lttng_session_descriptor_live_network_create(
1936 name
, url
, NULL
, timer_interval
);
1938 descriptor
= lttng_session_descriptor_live_create(
1939 name
, timer_interval
);
1942 ret
= -LTTNG_ERR_INVALID
;
1945 ret_code
= lttng_create_session_ext(descriptor
);
1946 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1948 lttng_session_descriptor_destroy(descriptor
);
1953 * Stop the session and wait for the data before destroying it
1955 * Return 0 on success else a negative LTTng error code.
1957 int lttng_destroy_session(const char *session_name
)
1960 enum lttng_error_code ret_code
;
1961 enum lttng_destruction_handle_status status
;
1962 struct lttng_destruction_handle
*handle
= NULL
;
1965 * Stop the tracing and wait for the data to be
1968 ret
= _lttng_stop_tracing(session_name
, 1);
1969 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1973 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
1974 if (ret_code
!= LTTNG_OK
) {
1975 ret
= (int) -ret_code
;
1980 /* Block until the completion of the destruction of the session. */
1981 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
1982 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
1983 ret
= -LTTNG_ERR_UNK
;
1987 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
1988 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
1989 ret
= -LTTNG_ERR_UNK
;
1992 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1994 lttng_destruction_handle_destroy(handle
);
1999 * Destroy the session without waiting for the data.
2001 int lttng_destroy_session_no_wait(const char *session_name
)
2003 enum lttng_error_code ret_code
;
2005 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2006 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2010 * Ask the session daemon for all available sessions.
2011 * Sets the contents of the sessions array.
2012 * Returns the number of lttng_session entries in sessions;
2013 * on error, returns a negative value.
2015 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2018 struct lttcomm_session_msg lsm
;
2019 const size_t session_size
= sizeof(struct lttng_session
) +
2020 sizeof(struct lttng_session_extended
);
2021 size_t session_count
, i
;
2022 struct lttng_session_extended
*sessions_extended_begin
;
2023 struct lttng_session
*sessions
= NULL
;
2025 memset(&lsm
, 0, sizeof(lsm
));
2026 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2027 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2032 ret
= -LTTNG_ERR_FATAL
;
2036 if (ret
% session_size
) {
2037 ret
= -LTTNG_ERR_UNK
;
2039 *out_sessions
= NULL
;
2042 session_count
= (size_t) ret
/ session_size
;
2043 sessions_extended_begin
= (struct lttng_session_extended
*)
2044 (&sessions
[session_count
]);
2046 /* Set extended session info pointers. */
2047 for (i
= 0; i
< session_count
; i
++) {
2048 struct lttng_session
*session
= &sessions
[i
];
2049 struct lttng_session_extended
*extended
=
2050 &(sessions_extended_begin
[i
]);
2052 session
->extended
.ptr
= extended
;
2055 ret
= (int) session_count
;
2056 *out_sessions
= sessions
;
2061 enum lttng_error_code
lttng_session_get_creation_time(
2062 const struct lttng_session
*session
, uint64_t *creation_time
)
2064 enum lttng_error_code ret
= LTTNG_OK
;
2065 struct lttng_session_extended
*extended
;
2067 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2068 ret
= LTTNG_ERR_INVALID
;
2072 extended
= session
->extended
.ptr
;
2073 if (!extended
->creation_time
.is_set
) {
2074 /* Not created on the session daemon yet. */
2075 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2078 *creation_time
= extended
->creation_time
.value
;
2083 int lttng_set_session_shm_path(const char *session_name
,
2084 const char *shm_path
)
2086 struct lttcomm_session_msg lsm
;
2088 if (session_name
== NULL
) {
2089 return -LTTNG_ERR_INVALID
;
2092 memset(&lsm
, 0, sizeof(lsm
));
2093 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2095 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2096 sizeof(lsm
.session
.name
));
2097 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2098 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2100 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2104 * Ask the session daemon for all available domains of a session.
2105 * Sets the contents of the domains array.
2106 * Returns the number of lttng_domain entries in domains;
2107 * on error, returns a negative value.
2109 int lttng_list_domains(const char *session_name
,
2110 struct lttng_domain
**domains
)
2113 struct lttcomm_session_msg lsm
;
2115 if (session_name
== NULL
) {
2116 return -LTTNG_ERR_INVALID
;
2119 memset(&lsm
, 0, sizeof(lsm
));
2120 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2122 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2123 sizeof(lsm
.session
.name
));
2125 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2130 return ret
/ sizeof(struct lttng_domain
);
2134 * Ask the session daemon for all available channels of a session.
2135 * Sets the contents of the channels array.
2136 * Returns the number of lttng_channel entries in channels;
2137 * on error, returns a negative value.
2139 int lttng_list_channels(struct lttng_handle
*handle
,
2140 struct lttng_channel
**channels
)
2143 size_t channel_count
, i
;
2144 const size_t channel_size
= sizeof(struct lttng_channel
) +
2145 sizeof(struct lttng_channel_extended
);
2146 struct lttcomm_session_msg lsm
;
2149 if (handle
== NULL
) {
2150 ret
= -LTTNG_ERR_INVALID
;
2154 memset(&lsm
, 0, sizeof(lsm
));
2155 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2156 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2157 sizeof(lsm
.session
.name
));
2159 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2161 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2166 if (ret
% channel_size
) {
2167 ret
= -LTTNG_ERR_UNK
;
2172 channel_count
= (size_t) ret
/ channel_size
;
2174 /* Set extended info pointers */
2175 extended_at
= ((void *) *channels
) +
2176 channel_count
* sizeof(struct lttng_channel
);
2177 for (i
= 0; i
< channel_count
; i
++) {
2178 struct lttng_channel
*chan
= &(*channels
)[i
];
2180 chan
->attr
.extended
.ptr
= extended_at
;
2181 extended_at
+= sizeof(struct lttng_channel_extended
);
2184 ret
= (int) channel_count
;
2190 * Ask the session daemon for all available events of a session channel.
2191 * Sets the contents of the events array.
2192 * Returns the number of lttng_event entries in events;
2193 * on error, returns a negative value.
2195 int lttng_list_events(struct lttng_handle
*handle
,
2196 const char *channel_name
, struct lttng_event
**events
)
2199 struct lttcomm_session_msg lsm
= {};
2200 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2201 uint32_t nb_events
, i
;
2202 const void *comm_ext_at
;
2203 struct lttng_dynamic_buffer listing
;
2205 struct lttng_payload payload
;
2206 struct lttng_payload payload_copy
;
2207 struct lttng_payload_view lsm_view
=
2208 lttng_payload_view_init_from_buffer(
2209 (const char *) &lsm
, 0, sizeof(lsm
));
2210 struct lttng_buffer_view cmd_header_view
;
2211 struct lttng_buffer_view cmd_payload_view
;
2212 struct lttng_buffer_view flat_events_view
;
2213 struct lttng_buffer_view ext_view
;
2215 /* Safety check. An handle and channel name are mandatory */
2216 if (handle
== NULL
|| channel_name
== NULL
) {
2217 ret
= -LTTNG_ERR_INVALID
;
2221 lttng_payload_init(&payload
);
2222 lttng_payload_init(&payload_copy
);
2224 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2225 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2226 sizeof(lsm
.session
.name
));
2227 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2228 sizeof(lsm
.u
.list
.channel_name
));
2229 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2231 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2237 * A copy of the payload is performed since it will be
2238 * consumed twice. Consuming the same payload twice is invalid since
2239 * it will cause any received file descriptor to become "shared"
2240 * between different instances of the resulting objects.
2242 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2244 ret
= -LTTNG_ERR_NOMEM
;
2248 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2249 &payload
.buffer
, 0, sizeof(*cmd_header
));
2250 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2251 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2255 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2257 /* Set number of events and free command header */
2258 nb_events
= cmd_header
->nb_events
;
2259 if (nb_events
> INT_MAX
) {
2260 ret
= -LTTNG_ERR_OVERFLOW
;
2264 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2265 &payload
.buffer
, sizeof(*cmd_header
), -1);
2268 * The buffer that is returned must contain a "flat" version of
2269 * the events that are returned. In other words, all pointers
2270 * within an lttng_event must point to a location within the returned
2271 * buffer so that the user may free everything by simply calling free()
2272 * on the returned buffer. This is needed in order to maintain API
2275 * A first pass is performed to compute the size of the buffer that
2276 * must be allocated. A second pass is then performed to setup
2277 * the returned events so that their members always point within the
2280 * The layout of the returned buffer is as follows:
2281 * - struct lttng_event[nb_events],
2282 * - nb_events times the following:
2283 * - struct lttng_event_extended,
2284 * - flattened version of userspace_probe_location
2285 * - filter_expression
2287 * - padding to align to 64-bits
2289 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2290 nb_events
* sizeof(struct lttng_event
), -1);
2291 comm_ext_at
= ext_view
.data
;
2292 storage_req
= nb_events
* sizeof(struct lttng_event
);
2294 struct lttng_payload_view payload_view
=
2295 lttng_payload_view_from_payload(&payload
, 0, -1);
2297 for (i
= 0; i
< nb_events
; i
++) {
2298 const struct lttcomm_event_extended_header
*ext_comm
=
2299 (struct lttcomm_event_extended_header
*)
2301 int probe_storage_req
= 0;
2303 comm_ext_at
+= sizeof(*ext_comm
);
2304 comm_ext_at
+= ext_comm
->filter_len
;
2305 comm_ext_at
+= ext_comm
->nb_exclusions
*
2306 LTTNG_SYMBOL_NAME_LEN
;
2308 if (ext_comm
->userspace_probe_location_len
) {
2309 struct lttng_userspace_probe_location
2310 *probe_location
= NULL
;
2311 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2313 (const char *) comm_ext_at
-
2314 payload_view
.buffer
.data
,
2315 ext_comm
->userspace_probe_location_len
);
2317 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2318 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2323 * Create a temporary userspace probe location
2324 * to determine the size needed by a "flattened"
2325 * version of that same probe location.
2327 ret
= lttng_userspace_probe_location_create_from_payload(
2328 &probe_location_view
,
2331 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2335 ret
= lttng_userspace_probe_location_flatten(
2336 probe_location
, NULL
);
2337 lttng_userspace_probe_location_destroy(
2340 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2344 probe_storage_req
= ret
;
2345 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2348 storage_req
+= sizeof(struct lttng_event_extended
);
2349 storage_req
+= ext_comm
->filter_len
;
2350 storage_req
+= ext_comm
->nb_exclusions
*
2351 LTTNG_SYMBOL_NAME_LEN
;
2352 /* Padding to ensure the flat probe is aligned. */
2353 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2354 storage_req
+= probe_storage_req
;
2358 lttng_dynamic_buffer_init(&listing
);
2360 * We must ensure that "listing" is never resized so as to preserve
2361 * the validity of the flattened objects.
2363 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2365 ret
= -LTTNG_ERR_NOMEM
;
2369 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2370 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2371 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2372 nb_events
* sizeof(struct lttng_event
));
2373 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2375 ret
= -LTTNG_ERR_NOMEM
;
2376 goto free_dynamic_buffer
;
2379 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2380 nb_events
* sizeof(struct lttng_event
), -1);
2381 comm_ext_at
= ext_view
.data
;
2384 struct lttng_payload_view payload_copy_view
=
2385 lttng_payload_view_from_payload(
2386 &payload_copy
, 0, -1);
2388 for (i
= 0; i
< nb_events
; i
++) {
2389 struct lttng_event
*event
= (typeof(event
))(
2391 (sizeof(struct lttng_event
) * i
));
2392 const struct lttcomm_event_extended_header
*ext_comm
=
2393 (typeof(ext_comm
)) comm_ext_at
;
2394 struct lttng_event_extended
*event_extended
=
2395 (typeof(event_extended
))(listing
.data
+
2398 /* Insert struct lttng_event_extended. */
2399 ret
= lttng_dynamic_buffer_set_size(&listing
,
2400 listing
.size
+ sizeof(*event_extended
));
2402 ret
= -LTTNG_ERR_NOMEM
;
2403 goto free_dynamic_buffer
;
2405 event
->extended
.ptr
= event_extended
;
2407 comm_ext_at
+= sizeof(*ext_comm
);
2409 /* Insert filter expression. */
2410 if (ext_comm
->filter_len
) {
2411 event_extended
->filter_expression
=
2412 listing
.data
+ listing
.size
;
2413 ret
= lttng_dynamic_buffer_append(&listing
,
2415 ext_comm
->filter_len
);
2417 ret
= -LTTNG_ERR_NOMEM
;
2418 goto free_dynamic_buffer
;
2420 comm_ext_at
+= ext_comm
->filter_len
;
2423 /* Insert exclusions. */
2424 if (ext_comm
->nb_exclusions
) {
2425 event_extended
->exclusions
.count
=
2426 ext_comm
->nb_exclusions
;
2427 event_extended
->exclusions
.strings
=
2428 listing
.data
+ listing
.size
;
2430 ret
= lttng_dynamic_buffer_append(&listing
,
2432 ext_comm
->nb_exclusions
*
2433 LTTNG_SYMBOL_NAME_LEN
);
2435 ret
= -LTTNG_ERR_NOMEM
;
2436 goto free_dynamic_buffer
;
2438 comm_ext_at
+= ext_comm
->nb_exclusions
*
2439 LTTNG_SYMBOL_NAME_LEN
;
2442 /* Insert padding to align to 64-bits. */
2443 ret
= lttng_dynamic_buffer_set_size(&listing
,
2444 ALIGN_TO(listing
.size
,
2447 ret
= -LTTNG_ERR_NOMEM
;
2448 goto free_dynamic_buffer
;
2451 /* Insert flattened userspace probe location. */
2452 if (ext_comm
->userspace_probe_location_len
) {
2453 struct lttng_userspace_probe_location
2454 *probe_location
= NULL
;
2455 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2457 (const char *) comm_ext_at
-
2458 payload_copy_view
.buffer
.data
,
2459 ext_comm
->userspace_probe_location_len
);
2461 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2462 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2463 goto free_dynamic_buffer
;
2466 ret
= lttng_userspace_probe_location_create_from_payload(
2467 &probe_location_view
,
2470 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2471 goto free_dynamic_buffer
;
2474 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2477 ret
= lttng_userspace_probe_location_flatten(
2478 probe_location
, &listing
);
2479 lttng_userspace_probe_location_destroy(
2482 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2483 goto free_dynamic_buffer
;
2486 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2491 /* Don't reset listing buffer as we return its content. */
2492 *events
= (struct lttng_event
*) listing
.data
;
2493 lttng_dynamic_buffer_init(&listing
);
2494 ret
= (int) nb_events
;
2495 free_dynamic_buffer
:
2496 lttng_dynamic_buffer_reset(&listing
);
2498 lttng_payload_reset(&payload
);
2499 lttng_payload_reset(&payload_copy
);
2504 * Sets the tracing_group variable with name.
2505 * This function allocates memory pointed to by tracing_group.
2506 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2508 int lttng_set_tracing_group(const char *name
)
2511 return -LTTNG_ERR_INVALID
;
2514 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2515 return -LTTNG_ERR_FATAL
;
2521 int lttng_calibrate(struct lttng_handle
*handle
,
2522 struct lttng_calibrate
*calibrate
)
2525 * This command was removed in LTTng 2.9.
2527 return -LTTNG_ERR_UND
;
2531 * Set default channel attributes.
2532 * If either or both of the arguments are null, attr content is zeroe'd.
2534 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2535 struct lttng_channel_attr
*attr
)
2537 struct lttng_channel_extended
*extended
;
2540 if (attr
== NULL
|| domain
== NULL
) {
2544 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2545 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2547 /* Same for all domains. */
2548 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2549 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2550 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2552 switch (domain
->type
) {
2553 case LTTNG_DOMAIN_KERNEL
:
2554 attr
->switch_timer_interval
=
2555 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2556 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2557 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2558 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2559 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2561 extended
->monitor_timer_interval
=
2562 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2563 extended
->blocking_timeout
=
2564 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2567 case LTTNG_DOMAIN_UST
:
2568 switch (domain
->buf_type
) {
2569 case LTTNG_BUFFER_PER_UID
:
2570 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2571 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2572 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2573 attr
->switch_timer_interval
=
2574 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2575 attr
->read_timer_interval
=
2576 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2578 extended
->monitor_timer_interval
=
2579 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2580 extended
->blocking_timeout
=
2581 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2584 case LTTNG_BUFFER_PER_PID
:
2586 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2587 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2588 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2589 attr
->switch_timer_interval
=
2590 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2591 attr
->read_timer_interval
=
2592 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2594 extended
->monitor_timer_interval
=
2595 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2596 extended
->blocking_timeout
=
2597 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2602 /* Default behavior: leave set to 0. */
2606 attr
->extended
.ptr
= extended
;
2609 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2610 uint64_t *discarded_events
)
2613 struct lttng_channel_extended
*chan_ext
;
2615 if (!channel
|| !discarded_events
) {
2616 ret
= -LTTNG_ERR_INVALID
;
2620 chan_ext
= channel
->attr
.extended
.ptr
;
2623 * This can happen since the lttng_channel structure is
2624 * used for other tasks where this pointer is never set.
2626 *discarded_events
= 0;
2630 *discarded_events
= chan_ext
->discarded_events
;
2635 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2636 uint64_t *lost_packets
)
2639 struct lttng_channel_extended
*chan_ext
;
2641 if (!channel
|| !lost_packets
) {
2642 ret
= -LTTNG_ERR_INVALID
;
2646 chan_ext
= channel
->attr
.extended
.ptr
;
2649 * This can happen since the lttng_channel structure is
2650 * used for other tasks where this pointer is never set.
2656 *lost_packets
= chan_ext
->lost_packets
;
2661 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2662 uint64_t *monitor_timer_interval
)
2666 if (!chan
|| !monitor_timer_interval
) {
2667 ret
= -LTTNG_ERR_INVALID
;
2671 if (!chan
->attr
.extended
.ptr
) {
2672 ret
= -LTTNG_ERR_INVALID
;
2676 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2677 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2682 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2683 uint64_t monitor_timer_interval
)
2687 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2688 ret
= -LTTNG_ERR_INVALID
;
2692 ((struct lttng_channel_extended
*)
2693 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2694 monitor_timer_interval
;
2699 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2700 int64_t *blocking_timeout
)
2704 if (!chan
|| !blocking_timeout
) {
2705 ret
= -LTTNG_ERR_INVALID
;
2709 if (!chan
->attr
.extended
.ptr
) {
2710 ret
= -LTTNG_ERR_INVALID
;
2714 *blocking_timeout
= ((struct lttng_channel_extended
*)
2715 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2720 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2721 int64_t blocking_timeout
)
2724 int64_t msec_timeout
;
2726 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2727 ret
= -LTTNG_ERR_INVALID
;
2731 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2732 ret
= -LTTNG_ERR_INVALID
;
2737 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2738 * us to accept a narrower range of values (msecs expressed as a signed
2741 msec_timeout
= blocking_timeout
/ 1000;
2742 if (msec_timeout
!= (int32_t) msec_timeout
) {
2743 ret
= -LTTNG_ERR_INVALID
;
2747 ((struct lttng_channel_extended
*)
2748 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2755 * Check if session daemon is alive.
2757 * Return 1 if alive or 0 if not.
2758 * On error returns a negative value.
2760 int lttng_session_daemon_alive(void)
2764 ret
= set_session_daemon_path();
2770 if (*sessiond_sock_path
== '\0') {
2772 * No socket path set. Weird error which means the constructor
2778 ret
= try_connect_sessiond(sessiond_sock_path
);
2789 * Set URL for a consumer for a session and domain.
2791 * Return 0 on success, else a negative value.
2793 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2794 const char *control_url
, const char *data_url
)
2798 struct lttcomm_session_msg lsm
;
2799 struct lttng_uri
*uris
= NULL
;
2801 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2802 return -LTTNG_ERR_INVALID
;
2805 memset(&lsm
, 0, sizeof(lsm
));
2807 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2809 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2810 sizeof(lsm
.session
.name
));
2811 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2813 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2815 return -LTTNG_ERR_INVALID
;
2818 lsm
.u
.uri
.size
= size
;
2820 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2821 sizeof(struct lttng_uri
) * size
, NULL
);
2830 int lttng_enable_consumer(struct lttng_handle
*handle
);
2831 int lttng_enable_consumer(struct lttng_handle
*handle
)
2839 int lttng_disable_consumer(struct lttng_handle
*handle
);
2840 int lttng_disable_consumer(struct lttng_handle
*handle
)
2848 int _lttng_create_session_ext(const char *name
, const char *url
,
2849 const char *datetime
);
2850 int _lttng_create_session_ext(const char *name
, const char *url
,
2851 const char *datetime
)
2857 * For a given session name, this call checks if the data is ready to be read
2858 * or is still being extracted by the consumer(s) hence not ready to be used by
2861 int lttng_data_pending(const char *session_name
)
2864 struct lttcomm_session_msg lsm
;
2865 uint8_t *pending
= NULL
;
2867 if (session_name
== NULL
) {
2868 return -LTTNG_ERR_INVALID
;
2871 memset(&lsm
, 0, sizeof(lsm
));
2872 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2874 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2875 sizeof(lsm
.session
.name
));
2877 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2880 } else if (ret
!= 1) {
2881 /* Unexpected payload size */
2882 ret
= -LTTNG_ERR_INVALID
;
2884 } else if (!pending
) {
2885 /* Internal error. */
2886 ret
= -LTTNG_ERR_UNK
;
2890 ret
= (int) *pending
;
2897 * Regenerate the metadata for a session.
2898 * Return 0 on success, a negative error code on error.
2900 int lttng_regenerate_metadata(const char *session_name
)
2903 struct lttcomm_session_msg lsm
;
2905 if (!session_name
) {
2906 ret
= -LTTNG_ERR_INVALID
;
2910 memset(&lsm
, 0, sizeof(lsm
));
2911 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2913 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2914 sizeof(lsm
.session
.name
));
2916 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2927 * Deprecated, replaced by lttng_regenerate_metadata.
2929 int lttng_metadata_regenerate(const char *session_name
)
2931 return lttng_regenerate_metadata(session_name
);
2935 * Regenerate the statedump of a session.
2936 * Return 0 on success, a negative error code on error.
2938 int lttng_regenerate_statedump(const char *session_name
)
2941 struct lttcomm_session_msg lsm
;
2943 if (!session_name
) {
2944 ret
= -LTTNG_ERR_INVALID
;
2948 memset(&lsm
, 0, sizeof(lsm
));
2949 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2951 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2952 sizeof(lsm
.session
.name
));
2954 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2964 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2967 struct lttcomm_session_msg lsm
= {
2968 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2970 struct lttcomm_session_msg
*message_lsm
;
2971 struct lttng_payload message
;
2972 struct lttng_payload reply
;
2973 struct lttng_trigger
*reply_trigger
= NULL
;
2974 enum lttng_domain_type domain_type
;
2975 const struct lttng_credentials user_creds
= {
2976 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2977 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2981 lttng_payload_init(&message
);
2982 lttng_payload_init(&reply
);
2985 ret
= -LTTNG_ERR_INVALID
;
2989 if (!trigger
->creds
.uid
.is_set
) {
2990 /* Use the client's credentials as the trigger credentials. */
2991 lttng_trigger_set_credentials(trigger
, &user_creds
);
2994 * Validate that either the current trigger credentials and the
2995 * client credentials are identical or that the current user is
2996 * root. The root user can register, unregister triggers for
2997 * himself and other users.
2999 * This check is also present on the sessiond side, using the
3000 * credentials passed on the socket. These check are all
3003 const struct lttng_credentials
*trigger_creds
=
3004 lttng_trigger_get_credentials(trigger
);
3006 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3007 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3008 ret
= -LTTNG_ERR_EPERM
;
3014 if (!lttng_trigger_validate(trigger
)) {
3015 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3019 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3022 lsm
.domain
.type
= domain_type
;
3024 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3026 ret
= -LTTNG_ERR_NOMEM
;
3031 * This is needed to populate the trigger object size for the command
3034 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3036 ret
= lttng_trigger_serialize(trigger
, &message
);
3038 ret
= -LTTNG_ERR_UNK
;
3042 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3045 struct lttng_payload_view message_view
=
3046 lttng_payload_view_from_payload(
3049 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3051 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3058 struct lttng_payload_view reply_view
=
3059 lttng_payload_view_from_payload(
3060 &reply
, 0, reply
.buffer
.size
);
3062 ret
= lttng_trigger_create_from_payload(
3063 &reply_view
, &reply_trigger
);
3065 ret
= -LTTNG_ERR_FATAL
;
3070 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3072 ret
= -LTTNG_ERR_FATAL
;
3078 lttng_payload_reset(&message
);
3079 lttng_payload_reset(&reply
);
3080 lttng_trigger_destroy(reply_trigger
);
3084 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3087 struct lttcomm_session_msg lsm
;
3088 struct lttcomm_session_msg
*message_lsm
;
3089 struct lttng_payload message
;
3090 struct lttng_payload reply
;
3091 const struct lttng_credentials user_creds
= {
3092 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3093 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3096 lttng_payload_init(&message
);
3097 lttng_payload_init(&reply
);
3100 ret
= -LTTNG_ERR_INVALID
;
3104 if (!trigger
->creds
.uid
.is_set
) {
3105 /* Use the client's credentials as the trigger credentials. */
3106 lttng_trigger_set_credentials(trigger
, &user_creds
);
3109 * Validate that either the current trigger credentials and the
3110 * client credentials are identical or that the current user is
3111 * root. The root user can register, unregister triggers for
3112 * himself and other users.
3114 * This check is also present on the sessiond side, using the
3115 * credentials passed on the socket. These check are all
3118 const struct lttng_credentials
*trigger_creds
=
3119 lttng_trigger_get_credentials(trigger
);
3121 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3122 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3123 ret
= -LTTNG_ERR_EPERM
;
3129 if (!lttng_trigger_validate(trigger
)) {
3130 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3134 memset(&lsm
, 0, sizeof(lsm
));
3135 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3137 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3139 ret
= -LTTNG_ERR_NOMEM
;
3144 * This is needed to populate the trigger object size for the command
3145 * header and number of fds sent.
3147 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3149 ret
= lttng_trigger_serialize(trigger
, &message
);
3151 ret
= -LTTNG_ERR_UNK
;
3155 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3158 struct lttng_payload_view message_view
=
3159 lttng_payload_view_from_payload(
3163 * Update the message header with the number of fd that will be
3166 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3169 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3177 lttng_payload_reset(&message
);
3178 lttng_payload_reset(&reply
);
3183 * Ask the session daemon for all registered triggers for the current user.
3185 * Allocates and return an lttng_triggers set.
3186 * On error, returns a suitable lttng_error_code.
3188 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3191 enum lttng_error_code ret_code
= LTTNG_OK
;
3192 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3193 struct lttng_triggers
*local_triggers
= NULL
;
3194 struct lttng_payload reply
;
3195 struct lttng_payload_view lsm_view
=
3196 lttng_payload_view_init_from_buffer(
3197 (const char *) &lsm
, 0, sizeof(lsm
));
3199 lttng_payload_init(&reply
);
3201 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3203 ret_code
= (enum lttng_error_code
) -ret
;
3208 struct lttng_payload_view reply_view
=
3209 lttng_payload_view_from_payload(
3210 &reply
, 0, reply
.buffer
.size
);
3212 ret
= lttng_triggers_create_from_payload(
3213 &reply_view
, &local_triggers
);
3215 ret_code
= LTTNG_ERR_FATAL
;
3220 *triggers
= local_triggers
;
3221 local_triggers
= NULL
;
3223 lttng_payload_reset(&reply
);
3224 lttng_triggers_destroy(local_triggers
);
3231 static void __attribute__((constructor
)) init(void)
3233 /* Set default session group */
3234 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3240 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3242 free(tracing_group
);