4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 EfficiOS Inc.
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/bytecode/bytecode.h>
23 #include <common/common.h>
24 #include <common/compat/errno.h>
25 #include <common/compat/string.h>
26 #include <common/defaults.h>
27 #include <common/dynamic-array.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/payload-view.h>
30 #include <common/payload.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/tracker.h>
33 #include <common/unix.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/channel-internal.h>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-internal.h>
41 #include <lttng/health-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/trigger/trigger-internal.h>
47 #include <lttng/userspace-probe-internal.h>
49 #include "lttng-ctl-helper.h"
50 #include <common/filter/filter-ast.h>
51 #include <common/filter/filter-parser.h>
52 #include <common/filter/memstream.h>
54 #define COPY_DOMAIN_PACKED(dst, src) \
56 struct lttng_domain _tmp_domain; \
58 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
62 /* Socket to session daemon for communication */
63 static int sessiond_socket
= -1;
64 static char sessiond_sock_path
[PATH_MAX
];
67 static char *tracing_group
;
73 * Those two variables are used by error.h to silent or control the verbosity of
74 * error message. They are global to the library so application linking with it
75 * are able to compile correctly and also control verbosity of the library.
78 int lttng_opt_verbose
;
82 * Copy domain to lttcomm_session_msg domain.
84 * If domain is unknown, default domain will be the kernel.
87 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
88 struct lttng_domain
*src
)
92 case LTTNG_DOMAIN_KERNEL
:
93 case LTTNG_DOMAIN_UST
:
94 case LTTNG_DOMAIN_JUL
:
95 case LTTNG_DOMAIN_LOG4J
:
96 case LTTNG_DOMAIN_PYTHON
:
97 memcpy(dst
, src
, sizeof(struct lttng_domain
));
100 memset(dst
, 0, sizeof(struct lttng_domain
));
107 * Send lttcomm_session_msg to the session daemon.
109 * On success, returns the number of bytes sent (>=0)
110 * On error, returns -1
112 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
117 ret
= -LTTNG_ERR_NO_SESSIOND
;
121 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm
->cmd_type
),
124 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
125 sizeof(struct lttcomm_session_msg
));
127 ret
= -LTTNG_ERR_FATAL
;
135 * Send var len data to the session daemon.
137 * On success, returns the number of bytes sent (>=0)
138 * On error, returns -1
140 static int send_session_varlen(const void *data
, size_t len
)
145 ret
= -LTTNG_ERR_NO_SESSIOND
;
154 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
156 ret
= -LTTNG_ERR_FATAL
;
164 * Send file descriptors to the session daemon.
166 * On success, returns the number of bytes sent (>=0)
167 * On error, returns -1
169 static int send_session_fds(const int *fds
, size_t nb_fd
)
174 ret
= -LTTNG_ERR_NO_SESSIOND
;
178 if (!fds
|| !nb_fd
) {
183 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
185 ret
= -LTTNG_ERR_FATAL
;
193 * Receive data from the sessiond socket.
195 * On success, returns the number of bytes received (>=0)
196 * On error, returns a negative lttng_error_code.
198 static int recv_data_sessiond(void *buf
, size_t len
)
205 ret
= -LTTNG_ERR_NO_SESSIOND
;
209 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
211 ret
= -LTTNG_ERR_FATAL
;
212 } else if (ret
== 0) {
213 ret
= -LTTNG_ERR_NO_SESSIOND
;
221 * Receive a payload from the session daemon by appending to an existing
223 * On success, returns the number of bytes received (>=0)
224 * On error, returns a negative lttng_error_code.
226 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
229 const size_t original_payload_size
= payload
->buffer
.size
;
231 ret
= lttng_dynamic_buffer_set_size(
232 &payload
->buffer
, payload
->buffer
.size
+ len
);
234 ret
= -LTTNG_ERR_NOMEM
;
238 ret
= recv_data_sessiond(
239 payload
->buffer
.data
+ original_payload_size
, len
);
245 * Check if we are in the specified group.
247 * If yes return 1, else return -1.
250 int lttng_check_tracing_group(void)
252 gid_t
*grp_list
, tracing_gid
;
253 int grp_list_size
, grp_id
, i
;
255 const char *grp_name
= tracing_group
;
257 /* Get GID of group 'tracing' */
258 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
259 /* If grp_tracing is NULL, the group does not exist. */
263 /* Get number of supplementary group IDs */
264 grp_list_size
= getgroups(0, NULL
);
265 if (grp_list_size
< 0) {
270 /* Alloc group list of the right size */
271 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
276 grp_id
= getgroups(grp_list_size
, grp_list
);
282 for (i
= 0; i
< grp_list_size
; i
++) {
283 if (grp_list
[i
] == tracing_gid
) {
296 static enum lttng_error_code
check_enough_available_memory(
297 uint64_t num_bytes_requested_per_cpu
)
300 enum lttng_error_code ret_code
;
302 uint64_t best_mem_info
;
303 uint64_t num_bytes_requested_total
;
306 * Get the number of CPU currently online to compute the amount of
307 * memory needed to create a buffer for every CPU.
309 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
311 ret_code
= LTTNG_ERR_FATAL
;
315 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
317 ret_code
= LTTNG_ERR_OVERFLOW
;
321 num_bytes_requested_total
=
322 num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
325 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
326 * reliable estimate we can get but it is only exposed by the kernel
327 * since 3.14. (See Linux kernel commit:
328 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
330 ret
= utils_get_memory_available(&best_mem_info
);
336 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
337 * is a sanity check for obvious user error.
339 ret
= utils_get_memory_total(&best_mem_info
);
344 /* No valid source of information. */
345 ret_code
= LTTNG_ERR_NOMEM
;
349 if (best_mem_info
>= num_bytes_requested_total
) {
352 ret_code
= LTTNG_ERR_NOMEM
;
359 * Try connect to session daemon with sock_path.
361 * Return 0 on success, else -1
363 static int try_connect_sessiond(const char *sock_path
)
367 /* If socket exist, we check if the daemon listens for connect. */
368 ret
= access(sock_path
, F_OK
);
374 ret
= lttcomm_connect_unix_sock(sock_path
);
380 ret
= lttcomm_close_unix_sock(ret
);
382 PERROR("lttcomm_close_unix_sock");
392 * Set sessiond socket path by putting it in the global sessiond_sock_path
395 * Returns 0 on success, negative value on failure (the sessiond socket path
396 * is somehow too long or ENOMEM).
398 static int set_session_daemon_path(void)
400 int in_tgroup
= 0; /* In tracing group. */
406 /* Are we in the tracing group ? */
407 in_tgroup
= lttng_check_tracing_group();
410 if ((uid
== 0) || in_tgroup
== 1) {
411 const int ret
= lttng_strncpy(sessiond_sock_path
,
412 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
413 sizeof(sessiond_sock_path
));
425 ret
= try_connect_sessiond(sessiond_sock_path
);
429 /* Global session daemon not available... */
431 /* ...or not in tracing group (and not root), default */
434 * With GNU C < 2.1, snprintf returns -1 if the target buffer
436 * With GNU C >= 2.1, snprintf returns the required size
437 * (excluding closing null)
439 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
440 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
441 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
453 * Connect to the LTTng session daemon.
455 * On success, return the socket's file descriptor. On error, return -1.
457 LTTNG_HIDDEN
int connect_sessiond(void)
461 ret
= set_session_daemon_path();
466 /* Connect to the sesssion daemon. */
467 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
478 static void reset_global_sessiond_connection_state(void)
480 sessiond_socket
= -1;
485 * Clean disconnect from the session daemon.
487 * On success, return 0. On error, return -1.
489 static int disconnect_sessiond(void)
494 ret
= lttcomm_close_unix_sock(sessiond_socket
);
495 reset_global_sessiond_connection_state();
501 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
509 ret
= -LTTNG_ERR_INVALID
;
519 ret
= recv_data_sessiond(buf
, len
);
525 ret
= -LTTNG_ERR_INVALID
;
529 /* Move ownership of command header buffer to user. */
534 /* No command header. */
550 * Ask the session daemon a specific command and put the data into buf.
551 * Takes extra var. len. data and file descriptors as input to send to the
554 * Return size of data (only payload, not header) or a negative error code.
557 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
558 const int *fds
, size_t nb_fd
, const void *vardata
,
559 size_t vardata_len
, void **user_payload_buf
,
560 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
564 struct lttcomm_lttng_msg llm
;
566 ret
= connect_sessiond();
568 ret
= -LTTNG_ERR_NO_SESSIOND
;
571 sessiond_socket
= ret
;
575 ret
= send_session_msg(lsm
);
577 /* Ret value is a valid lttng error code. */
580 /* Send var len data */
581 ret
= send_session_varlen(vardata
, vardata_len
);
583 /* Ret value is a valid lttng error code. */
588 ret
= send_session_fds(fds
, nb_fd
);
590 /* Ret value is a valid lttng error code. */
594 /* Get header from data transmission */
595 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
597 /* Ret value is a valid lttng error code. */
601 /* Check error code if OK */
602 if (llm
.ret_code
!= LTTNG_OK
) {
607 /* Get command header from data transmission */
608 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
609 user_cmd_header_buf
, user_cmd_header_len
);
614 /* Get payload from data transmission */
615 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
624 disconnect_sessiond();
629 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
630 struct lttng_payload
*reply
)
633 struct lttcomm_lttng_msg llm
;
634 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
636 assert(reply
->buffer
.size
== 0);
637 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
639 ret
= connect_sessiond();
641 ret
= -LTTNG_ERR_NO_SESSIOND
;
644 sessiond_socket
= ret
;
648 /* Send command to session daemon */
649 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
650 message
->buffer
.size
);
652 ret
= -LTTNG_ERR_FATAL
;
657 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
660 ret
= -LTTNG_ERR_FATAL
;
665 /* Get header from data transmission */
666 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
668 /* Ret value is a valid lttng error code. */
672 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
674 /* Check error code if OK */
675 if (llm
.ret_code
!= LTTNG_OK
) {
676 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
677 /* Invalid error code received. */
678 ret
= -LTTNG_ERR_UNK
;
685 if (llm
.cmd_header_size
> 0) {
686 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
692 /* Get command header from data transmission */
693 if (llm
.data_size
> 0) {
694 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
700 if (llm
.fd_count
> 0) {
701 ret
= lttcomm_recv_payload_fds_unix_sock(
702 sessiond_socket
, llm
.fd_count
, reply
);
708 /* Don't return the llm header to the caller. */
709 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
710 reply
->buffer
.size
- sizeof(llm
));
711 ret
= lttng_dynamic_buffer_set_size(
712 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
714 /* Can't happen as size is reduced. */
718 ret
= reply
->buffer
.size
;
721 disconnect_sessiond();
726 * Create lttng handle and return pointer.
728 * The returned pointer will be NULL in case of malloc() error.
730 struct lttng_handle
*lttng_create_handle(const char *session_name
,
731 struct lttng_domain
*domain
)
734 struct lttng_handle
*handle
= NULL
;
736 handle
= zmalloc(sizeof(struct lttng_handle
));
737 if (handle
== NULL
) {
738 PERROR("malloc handle");
742 /* Copy session name */
743 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
744 sizeof(handle
->session_name
));
749 /* Copy lttng domain or leave initialized to 0. */
751 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
762 * Destroy handle by free(3) the pointer.
764 void lttng_destroy_handle(struct lttng_handle
*handle
)
770 * Register an outside consumer.
772 * Returns size of returned session payload data or a negative error code.
774 int lttng_register_consumer(struct lttng_handle
*handle
,
775 const char *socket_path
)
778 struct lttcomm_session_msg lsm
;
780 if (handle
== NULL
|| socket_path
== NULL
) {
781 ret
= -LTTNG_ERR_INVALID
;
785 memset(&lsm
, 0, sizeof(lsm
));
786 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
787 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
788 sizeof(lsm
.session
.name
));
790 ret
= -LTTNG_ERR_INVALID
;
794 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
796 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
797 sizeof(lsm
.u
.reg
.path
));
799 ret
= -LTTNG_ERR_INVALID
;
803 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
809 * Start tracing for all traces of the session.
811 * Returns size of returned session payload data or a negative error code.
813 int lttng_start_tracing(const char *session_name
)
816 struct lttcomm_session_msg lsm
;
818 if (session_name
== NULL
) {
819 ret
= -LTTNG_ERR_INVALID
;
823 memset(&lsm
, 0, sizeof(lsm
));
824 lsm
.cmd_type
= LTTNG_START_TRACE
;
826 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
827 sizeof(lsm
.session
.name
));
829 ret
= -LTTNG_ERR_INVALID
;
833 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
839 * Stop tracing for all traces of the session.
841 static int _lttng_stop_tracing(const char *session_name
, int wait
)
844 struct lttcomm_session_msg lsm
;
846 if (session_name
== NULL
) {
847 ret
= -LTTNG_ERR_INVALID
;
851 memset(&lsm
, 0, sizeof(lsm
));
852 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
854 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
855 sizeof(lsm
.session
.name
));
857 ret
= -LTTNG_ERR_INVALID
;
861 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
862 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
870 /* Check for data availability */
872 data_ret
= lttng_data_pending(session_name
);
874 /* Return the data available call error. */
880 * Data sleep time before retrying (in usec). Don't sleep if the
881 * call returned value indicates availability.
884 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
886 } while (data_ret
!= 0);
894 * Stop tracing and wait for data availability.
896 int lttng_stop_tracing(const char *session_name
)
898 return _lttng_stop_tracing(session_name
, 1);
902 * Stop tracing but _don't_ wait for data availability.
904 int lttng_stop_tracing_no_wait(const char *session_name
)
906 return _lttng_stop_tracing(session_name
, 0);
910 * Add context to a channel.
912 * If the given channel is NULL, add the contexts to all channels.
913 * The event_name param is ignored.
915 * Returns the size of the returned payload data or a negative error code.
917 int lttng_add_context(struct lttng_handle
*handle
,
918 struct lttng_event_context
*ctx
, const char *event_name
,
919 const char *channel_name
)
924 struct lttcomm_session_msg lsm
;
926 /* Safety check. Both are mandatory. */
927 if (handle
== NULL
|| ctx
== NULL
) {
928 ret
= -LTTNG_ERR_INVALID
;
932 memset(&lsm
, 0, sizeof(lsm
));
933 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
935 /* If no channel name, send empty string. */
936 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
937 sizeof(lsm
.u
.context
.channel_name
));
939 ret
= -LTTNG_ERR_INVALID
;
943 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
944 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
945 sizeof(lsm
.session
.name
));
947 ret
= -LTTNG_ERR_INVALID
;
951 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
952 size_t provider_len
, ctx_len
;
953 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
954 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
956 if (!provider_name
|| !ctx_name
) {
957 ret
= -LTTNG_ERR_INVALID
;
961 provider_len
= strlen(provider_name
);
962 if (provider_len
== 0) {
963 ret
= -LTTNG_ERR_INVALID
;
966 lsm
.u
.context
.provider_name_len
= provider_len
;
968 ctx_len
= strlen(ctx_name
);
970 ret
= -LTTNG_ERR_INVALID
;
973 lsm
.u
.context
.context_name_len
= ctx_len
;
975 len
= provider_len
+ ctx_len
;
978 ret
= -LTTNG_ERR_NOMEM
;
982 memcpy(buf
, provider_name
, provider_len
);
983 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
985 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
987 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
989 * Don't leak application addresses to the sessiond.
990 * This is only necessary when ctx is for an app ctx otherwise
991 * the values inside the union (type & config) are overwritten.
993 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
994 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
997 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
1004 * Enable event(s) for a channel.
1006 * If no event name is specified, all events are enabled.
1007 * If no channel name is specified, the default 'channel0' is used.
1009 * Returns size of returned session payload data or a negative error code.
1011 int lttng_enable_event(struct lttng_handle
*handle
,
1012 struct lttng_event
*ev
, const char *channel_name
)
1014 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1019 * Create or enable an event with a filter expression.
1021 * Return negative error value on error.
1022 * Return size of returned session payload data if OK.
1024 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1025 struct lttng_event
*event
, const char *channel_name
,
1026 const char *filter_expression
)
1028 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1029 filter_expression
, 0, NULL
);
1033 * Depending on the event, return a newly allocated agent filter expression or
1034 * NULL if not applicable.
1036 * An event with NO loglevel and the name is * will return NULL.
1038 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1041 char *agent_filter
= NULL
;
1045 /* Don't add filter for the '*' event. */
1046 if (strcmp(ev
->name
, "*") != 0) {
1048 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1051 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1059 /* Add loglevel filtering if any for the JUL domain. */
1060 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1063 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1069 if (filter
|| agent_filter
) {
1072 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1073 agent_filter
? agent_filter
: filter
, op
,
1078 agent_filter
= new_filter
;
1080 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1089 return agent_filter
;
1096 * Enable event(s) for a channel, possibly with exclusions and a filter.
1097 * If no event name is specified, all events are enabled.
1098 * If no channel name is specified, the default name is used.
1099 * If filter expression is not NULL, the filter is set for the event.
1100 * If exclusion count is not zero, the exclusions are set for the event.
1101 * Returns size of returned session payload data or a negative error code.
1103 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1104 struct lttng_event
*ev
, const char *channel_name
,
1105 const char *original_filter_expression
,
1106 int exclusion_count
, char **exclusion_list
)
1108 struct lttcomm_session_msg lsm
;
1109 struct lttng_payload payload
;
1111 unsigned int free_filter_expression
= 0;
1112 struct filter_parser_ctx
*ctx
= NULL
;
1115 * We have either a filter or some exclusions, so we need to set up
1116 * a variable-length payload from where to send the data.
1118 lttng_payload_init(&payload
);
1121 * Cast as non-const since we may replace the filter expression
1122 * by a dynamically allocated string. Otherwise, the original
1123 * string is not modified.
1125 char *filter_expression
= (char *) original_filter_expression
;
1127 if (handle
== NULL
|| ev
== NULL
) {
1128 ret
= -LTTNG_ERR_INVALID
;
1133 * Empty filter string will always be rejected by the parser
1134 * anyway, so treat this corner-case early to eliminate
1135 * lttng_fmemopen error for 0-byte allocation.
1137 if (filter_expression
&& filter_expression
[0] == '\0') {
1138 ret
= -LTTNG_ERR_INVALID
;
1142 memset(&lsm
, 0, sizeof(lsm
));
1144 /* If no channel name, send empty string. */
1145 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1146 sizeof(lsm
.u
.enable
.channel_name
));
1148 ret
= -LTTNG_ERR_INVALID
;
1152 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1153 if (ev
->name
[0] == '\0') {
1154 /* Enable all events. */
1155 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1159 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1160 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1162 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1163 sizeof(lsm
.session
.name
));
1165 ret
= -LTTNG_ERR_INVALID
;
1169 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1170 lsm
.u
.enable
.bytecode_len
= 0;
1172 /* Parse filter expression. */
1173 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1174 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1175 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1176 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1177 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1178 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1181 /* Setup JUL filter if needed. */
1182 agent_filter
= set_agent_filter(filter_expression
, ev
);
1183 if (!agent_filter
) {
1184 if (!filter_expression
) {
1186 * No JUL and no filter, just skip
1193 * With an agent filter, the original filter has
1194 * been added to it thus replace the filter
1197 filter_expression
= agent_filter
;
1198 free_filter_expression
= 1;
1202 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1207 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1208 + bytecode_get_len(&ctx
->bytecode
->b
);
1209 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1212 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1213 lsm
.u
.enable
.bytecode_len
+
1214 lsm
.u
.enable
.expression_len
+
1215 LTTNG_SYMBOL_NAME_LEN
*
1218 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1222 /* Put exclusion names first in the data. */
1223 for (i
= 0; i
< exclusion_count
; i
++) {
1224 size_t exclusion_len
;
1226 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1227 LTTNG_SYMBOL_NAME_LEN
);
1228 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1229 /* Exclusion is not NULL-terminated. */
1230 ret
= -LTTNG_ERR_INVALID
;
1234 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1235 exclusion_list
[i
], exclusion_len
);
1241 * Padding the rest of the entry with zeros. Every exclusion
1242 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1244 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1245 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1251 /* Add filter expression next. */
1252 if (filter_expression
) {
1253 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1254 filter_expression
, lsm
.u
.enable
.expression_len
);
1259 /* Add filter bytecode next. */
1260 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1261 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1262 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1267 if (ev
->extended
.ptr
) {
1268 struct lttng_event_extended
*ev_ext
=
1269 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1271 if (ev_ext
->probe_location
) {
1273 * lttng_userspace_probe_location_serialize returns the
1274 * number of bytes that was appended to the buffer.
1276 ret
= lttng_userspace_probe_location_serialize(
1277 ev_ext
->probe_location
, &payload
);
1283 * Set the size of the userspace probe location element
1284 * of the buffer so that the receiving side knows where
1287 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1292 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1294 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1301 assert(fd_count
== 0 || fd_count
== 1);
1302 if (fd_count
== 1) {
1303 struct fd_handle
*h
=
1304 lttng_payload_view_pop_fd_handle(&view
);
1310 fd_to_send
= fd_handle_get_fd(h
);
1314 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1315 fd_count
? &fd_to_send
: NULL
, fd_count
,
1316 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1317 view
.buffer
.size
, NULL
, NULL
, 0);
1321 if (filter_expression
&& ctx
) {
1322 filter_bytecode_free(ctx
);
1323 filter_ir_free(ctx
);
1324 filter_parser_ctx_free(ctx
);
1327 if (free_filter_expression
) {
1329 * The filter expression has been replaced and must be freed as
1330 * it is not the original filter expression received as a
1333 free(filter_expression
);
1337 * Return directly to the caller and don't ask the sessiond since
1338 * something went wrong in the parsing of data above.
1340 lttng_payload_reset(&payload
);
1344 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1348 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1349 struct lttng_event
*ev
, const char *channel_name
,
1350 const char *original_filter_expression
)
1352 struct lttcomm_session_msg lsm
;
1355 unsigned int free_filter_expression
= 0;
1356 struct filter_parser_ctx
*ctx
= NULL
;
1358 * Cast as non-const since we may replace the filter expression
1359 * by a dynamically allocated string. Otherwise, the original
1360 * string is not modified.
1362 char *filter_expression
= (char *) original_filter_expression
;
1364 if (handle
== NULL
|| ev
== NULL
) {
1365 ret
= -LTTNG_ERR_INVALID
;
1370 * Empty filter string will always be rejected by the parser
1371 * anyway, so treat this corner-case early to eliminate
1372 * lttng_fmemopen error for 0-byte allocation.
1374 if (filter_expression
&& filter_expression
[0] == '\0') {
1375 ret
= -LTTNG_ERR_INVALID
;
1379 memset(&lsm
, 0, sizeof(lsm
));
1381 /* If no channel name, send empty string. */
1382 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1383 sizeof(lsm
.u
.disable
.channel_name
));
1385 ret
= -LTTNG_ERR_INVALID
;
1389 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1391 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1392 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1394 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1395 sizeof(lsm
.session
.name
));
1397 ret
= -LTTNG_ERR_INVALID
;
1401 lsm
.u
.disable
.bytecode_len
= 0;
1404 * For the JUL domain, a filter is enforced except for the
1405 * disable all event. This is done to avoid having the event in
1406 * all sessions thus filtering by logger name.
1408 if (filter_expression
== NULL
&&
1409 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1410 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1411 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1416 * We have a filter, so we need to set up a variable-length
1417 * memory block from where to send the data.
1420 /* Parse filter expression */
1421 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1422 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1423 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1424 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1425 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1426 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1429 /* Setup JUL filter if needed. */
1430 agent_filter
= set_agent_filter(filter_expression
, ev
);
1431 if (!agent_filter
) {
1432 if (!filter_expression
) {
1434 * No JUL and no filter, just skip
1441 * With a JUL filter, the original filter has
1442 * been added to it thus replace the filter
1445 filter_expression
= agent_filter
;
1446 free_filter_expression
= 1;
1450 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1455 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1456 + bytecode_get_len(&ctx
->bytecode
->b
);
1457 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1460 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1461 + lsm
.u
.disable
.expression_len
);
1463 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1467 /* Add filter expression. */
1468 if (lsm
.u
.disable
.expression_len
!= 0) {
1471 lsm
.u
.disable
.expression_len
);
1473 /* Add filter bytecode next. */
1474 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1476 + lsm
.u
.disable
.expression_len
,
1478 lsm
.u
.disable
.bytecode_len
);
1481 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1482 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1486 if (filter_expression
&& ctx
) {
1487 filter_bytecode_free(ctx
);
1488 filter_ir_free(ctx
);
1489 filter_parser_ctx_free(ctx
);
1492 if (free_filter_expression
) {
1494 * The filter expression has been replaced and must be freed as
1495 * it is not the original filter expression received as a
1498 free(filter_expression
);
1502 * Return directly to the caller and don't ask the sessiond since
1503 * something went wrong in the parsing of data above.
1508 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1513 * Disable event(s) of a channel and domain.
1514 * If no event name is specified, all events are disabled.
1515 * If no channel name is specified, the default 'channel0' is used.
1516 * Returns size of returned session payload data or a negative error code.
1518 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1519 const char *channel_name
)
1522 struct lttng_event ev
;
1524 memset(&ev
, 0, sizeof(ev
));
1526 ev
.type
= LTTNG_EVENT_ALL
;
1527 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1529 ret
= -LTTNG_ERR_INVALID
;
1533 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1538 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1540 struct lttng_channel
*channel
= NULL
;
1541 struct lttng_channel_extended
*extended
= NULL
;
1547 /* Validate domain. */
1548 switch (domain
->type
) {
1549 case LTTNG_DOMAIN_UST
:
1550 switch (domain
->buf_type
) {
1551 case LTTNG_BUFFER_PER_UID
:
1552 case LTTNG_BUFFER_PER_PID
:
1558 case LTTNG_DOMAIN_KERNEL
:
1559 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1567 channel
= zmalloc(sizeof(*channel
));
1572 extended
= zmalloc(sizeof(*extended
));
1577 channel
->attr
.extended
.ptr
= extended
;
1579 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1587 void lttng_channel_destroy(struct lttng_channel
*channel
)
1593 if (channel
->attr
.extended
.ptr
) {
1594 free(channel
->attr
.extended
.ptr
);
1600 * Enable channel per domain
1601 * Returns size of returned session payload data or a negative error code.
1603 int lttng_enable_channel(struct lttng_handle
*handle
,
1604 struct lttng_channel
*in_chan
)
1606 enum lttng_error_code ret_code
;
1608 struct lttcomm_session_msg lsm
;
1609 uint64_t total_buffer_size_needed_per_cpu
= 0;
1611 /* NULL arguments are forbidden. No default values. */
1612 if (handle
== NULL
|| in_chan
== NULL
) {
1613 return -LTTNG_ERR_INVALID
;
1616 memset(&lsm
, 0, sizeof(lsm
));
1617 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1618 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1620 if (!in_chan
->attr
.extended
.ptr
) {
1621 struct lttng_channel
*channel
;
1622 struct lttng_channel_extended
*extended
;
1624 channel
= lttng_channel_create(&handle
->domain
);
1626 return -LTTNG_ERR_NOMEM
;
1630 * Create a new channel in order to use default extended
1633 extended
= (struct lttng_channel_extended
*)
1634 channel
->attr
.extended
.ptr
;
1635 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1636 lttng_channel_destroy(channel
);
1638 struct lttng_channel_extended
*extended
;
1640 extended
= (struct lttng_channel_extended
*)
1641 in_chan
->attr
.extended
.ptr
;
1642 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1646 * Verify that the amount of memory required to create the requested
1647 * buffer is available on the system at the moment.
1649 if (lsm
.u
.channel
.chan
.attr
.num_subbuf
>
1650 UINT64_MAX
/ lsm
.u
.channel
.chan
.attr
.subbuf_size
) {
1652 ret
= -LTTNG_ERR_OVERFLOW
;
1656 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1657 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1658 ret_code
= check_enough_available_memory(
1659 total_buffer_size_needed_per_cpu
);
1660 if (ret_code
!= LTTNG_OK
) {
1665 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1666 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1668 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1669 sizeof(lsm
.session
.name
));
1671 ret
= -LTTNG_ERR_INVALID
;
1675 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1681 * All tracing will be stopped for registered events of the channel.
1682 * Returns size of returned session payload data or a negative error code.
1684 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1687 struct lttcomm_session_msg lsm
;
1689 /* Safety check. Both are mandatory. */
1690 if (handle
== NULL
|| name
== NULL
) {
1691 return -LTTNG_ERR_INVALID
;
1694 memset(&lsm
, 0, sizeof(lsm
));
1696 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1698 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1699 sizeof(lsm
.u
.disable
.channel_name
));
1701 ret
= -LTTNG_ERR_INVALID
;
1705 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1707 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1708 sizeof(lsm
.session
.name
));
1710 ret
= -LTTNG_ERR_INVALID
;
1714 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1720 * Lists all available tracepoints of domain.
1721 * Sets the contents of the events array.
1722 * Returns the number of lttng_event entries in events;
1723 * on error, returns a negative value.
1725 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1726 struct lttng_event
**events
)
1729 struct lttcomm_session_msg lsm
;
1731 if (handle
== NULL
) {
1732 return -LTTNG_ERR_INVALID
;
1735 memset(&lsm
, 0, sizeof(lsm
));
1736 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1737 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1739 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1744 return ret
/ sizeof(struct lttng_event
);
1748 * Lists all available tracepoint fields of domain.
1749 * Sets the contents of the event field array.
1750 * Returns the number of lttng_event_field entries in events;
1751 * on error, returns a negative value.
1753 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1754 struct lttng_event_field
**fields
)
1757 struct lttcomm_session_msg lsm
;
1759 if (handle
== NULL
) {
1760 return -LTTNG_ERR_INVALID
;
1763 memset(&lsm
, 0, sizeof(lsm
));
1764 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1765 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1767 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1772 return ret
/ sizeof(struct lttng_event_field
);
1776 * Lists all available kernel system calls. Allocates and sets the contents of
1779 * Returns the number of lttng_event entries in events; on error, returns a
1782 int lttng_list_syscalls(struct lttng_event
**events
)
1785 struct lttcomm_session_msg lsm
;
1788 return -LTTNG_ERR_INVALID
;
1791 memset(&lsm
, 0, sizeof(lsm
));
1792 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1793 /* Force kernel domain for system calls. */
1794 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1796 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1801 return ret
/ sizeof(struct lttng_event
);
1805 * Returns a human readable string describing
1806 * the error code (a negative value).
1808 const char *lttng_strerror(int code
)
1810 return error_get_str(code
);
1813 enum lttng_error_code
lttng_create_session_ext(
1814 struct lttng_session_descriptor
*session_descriptor
)
1816 enum lttng_error_code ret_code
;
1817 struct lttcomm_session_msg lsm
= {
1818 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1821 struct lttng_buffer_view reply_view
;
1823 bool sessiond_must_generate_ouput
;
1824 struct lttng_dynamic_buffer payload
;
1826 size_t descriptor_size
;
1827 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1829 lttng_dynamic_buffer_init(&payload
);
1830 if (!session_descriptor
) {
1831 ret_code
= LTTNG_ERR_INVALID
;
1835 sessiond_must_generate_ouput
=
1836 !lttng_session_descriptor_is_output_destination_initialized(
1837 session_descriptor
);
1838 if (sessiond_must_generate_ouput
) {
1839 const char *home_dir
= utils_get_home_dir();
1840 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1842 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1843 ret_code
= LTTNG_ERR_FATAL
;
1847 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1848 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1851 ret_code
= LTTNG_ERR_NOMEM
;
1856 descriptor_size
= payload
.size
;
1857 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1860 ret_code
= LTTNG_ERR_INVALID
;
1863 descriptor_size
= payload
.size
- descriptor_size
;
1864 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1866 /* Command returns a session descriptor on success. */
1867 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1868 payload
.size
, &reply
);
1869 if (reply_ret
< 0) {
1870 ret_code
= -reply_ret
;
1872 } else if (reply_ret
== 0) {
1873 /* Socket unexpectedly closed by the session daemon. */
1874 ret_code
= LTTNG_ERR_FATAL
;
1878 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1879 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1882 ret_code
= LTTNG_ERR_FATAL
;
1885 ret_code
= LTTNG_OK
;
1886 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1889 lttng_dynamic_buffer_reset(&payload
);
1890 lttng_session_descriptor_destroy(descriptor_reply
);
1895 * Create a new session using name and url for destination.
1897 * Return 0 on success else a negative LTTng error code.
1899 int lttng_create_session(const char *name
, const char *url
)
1903 struct lttng_uri
*uris
= NULL
;
1904 struct lttng_session_descriptor
*descriptor
= NULL
;
1905 enum lttng_error_code ret_code
;
1908 ret
= -LTTNG_ERR_INVALID
;
1912 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1914 ret
= -LTTNG_ERR_INVALID
;
1919 descriptor
= lttng_session_descriptor_create(name
);
1922 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1923 ret
= -LTTNG_ERR_INVALID
;
1926 descriptor
= lttng_session_descriptor_local_create(name
,
1930 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1934 ret
= -LTTNG_ERR_INVALID
;
1938 ret
= -LTTNG_ERR_INVALID
;
1941 ret_code
= lttng_create_session_ext(descriptor
);
1942 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1944 lttng_session_descriptor_destroy(descriptor
);
1950 * Create a session exclusively used for snapshot.
1952 * Return 0 on success else a negative LTTng error code.
1954 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1957 enum lttng_error_code ret_code
;
1959 struct lttng_uri
*uris
= NULL
;
1960 struct lttng_session_descriptor
*descriptor
= NULL
;
1963 ret
= -LTTNG_ERR_INVALID
;
1967 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1969 ret
= -LTTNG_ERR_INVALID
;
1973 * If the user does not specify a custom subdir, use the session name.
1975 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1976 strlen(uris
[0].subdir
) == 0) {
1977 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1980 PERROR("Failed to set session name as network destination sub-directory");
1981 ret
= -LTTNG_ERR_FATAL
;
1983 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1984 /* Truncated output. */
1985 ret
= -LTTNG_ERR_INVALID
;
1992 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1995 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1996 ret
= -LTTNG_ERR_INVALID
;
1999 descriptor
= lttng_session_descriptor_snapshot_local_create(
2004 descriptor
= lttng_session_descriptor_snapshot_network_create(
2010 ret
= -LTTNG_ERR_INVALID
;
2014 ret
= -LTTNG_ERR_INVALID
;
2017 ret_code
= lttng_create_session_ext(descriptor
);
2018 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2020 lttng_session_descriptor_destroy(descriptor
);
2026 * Create a session exclusively used for live.
2028 * Return 0 on success else a negative LTTng error code.
2030 int lttng_create_session_live(const char *name
, const char *url
,
2031 unsigned int timer_interval
)
2034 enum lttng_error_code ret_code
;
2035 struct lttng_session_descriptor
*descriptor
= NULL
;
2038 ret
= -LTTNG_ERR_INVALID
;
2043 descriptor
= lttng_session_descriptor_live_network_create(
2044 name
, url
, NULL
, timer_interval
);
2046 descriptor
= lttng_session_descriptor_live_create(
2047 name
, timer_interval
);
2050 ret
= -LTTNG_ERR_INVALID
;
2053 ret_code
= lttng_create_session_ext(descriptor
);
2054 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2056 lttng_session_descriptor_destroy(descriptor
);
2061 * Stop the session and wait for the data before destroying it
2063 * Return 0 on success else a negative LTTng error code.
2065 int lttng_destroy_session(const char *session_name
)
2068 enum lttng_error_code ret_code
;
2069 enum lttng_destruction_handle_status status
;
2070 struct lttng_destruction_handle
*handle
= NULL
;
2073 * Stop the tracing and wait for the data to be
2076 ret
= _lttng_stop_tracing(session_name
, 1);
2077 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2081 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2082 if (ret_code
!= LTTNG_OK
) {
2083 ret
= (int) -ret_code
;
2088 /* Block until the completion of the destruction of the session. */
2089 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2090 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2091 ret
= -LTTNG_ERR_UNK
;
2095 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2096 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2097 ret
= -LTTNG_ERR_UNK
;
2100 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2102 lttng_destruction_handle_destroy(handle
);
2107 * Destroy the session without waiting for the data.
2109 int lttng_destroy_session_no_wait(const char *session_name
)
2111 enum lttng_error_code ret_code
;
2113 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2114 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2118 * Ask the session daemon for all available sessions.
2119 * Sets the contents of the sessions array.
2120 * Returns the number of lttng_session entries in sessions;
2121 * on error, returns a negative value.
2123 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2126 struct lttcomm_session_msg lsm
;
2127 const size_t session_size
= sizeof(struct lttng_session
) +
2128 sizeof(struct lttng_session_extended
);
2129 size_t session_count
, i
;
2130 struct lttng_session_extended
*sessions_extended_begin
;
2131 struct lttng_session
*sessions
= NULL
;
2133 memset(&lsm
, 0, sizeof(lsm
));
2134 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2136 * Initialize out_sessions to NULL so it is initialized when
2137 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2138 * be subsequently freed.
2140 *out_sessions
= NULL
;
2141 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2146 ret
= -LTTNG_ERR_FATAL
;
2150 if (ret
% session_size
) {
2151 ret
= -LTTNG_ERR_UNK
;
2155 session_count
= (size_t) ret
/ session_size
;
2156 sessions_extended_begin
= (struct lttng_session_extended
*)
2157 (&sessions
[session_count
]);
2159 /* Set extended session info pointers. */
2160 for (i
= 0; i
< session_count
; i
++) {
2161 struct lttng_session
*session
= &sessions
[i
];
2162 struct lttng_session_extended
*extended
=
2163 &(sessions_extended_begin
[i
]);
2165 session
->extended
.ptr
= extended
;
2168 ret
= (int) session_count
;
2169 *out_sessions
= sessions
;
2174 enum lttng_error_code
lttng_session_get_creation_time(
2175 const struct lttng_session
*session
, uint64_t *creation_time
)
2177 enum lttng_error_code ret
= LTTNG_OK
;
2178 struct lttng_session_extended
*extended
;
2180 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2181 ret
= LTTNG_ERR_INVALID
;
2185 extended
= session
->extended
.ptr
;
2186 if (!extended
->creation_time
.is_set
) {
2187 /* Not created on the session daemon yet. */
2188 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2191 *creation_time
= extended
->creation_time
.value
;
2196 int lttng_set_session_shm_path(const char *session_name
,
2197 const char *shm_path
)
2200 struct lttcomm_session_msg lsm
;
2202 if (session_name
== NULL
) {
2203 return -LTTNG_ERR_INVALID
;
2206 memset(&lsm
, 0, sizeof(lsm
));
2207 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2209 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2210 sizeof(lsm
.session
.name
));
2212 ret
= -LTTNG_ERR_INVALID
;
2216 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2217 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2219 ret
= -LTTNG_ERR_INVALID
;
2223 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2229 * Ask the session daemon for all available domains of a session.
2230 * Sets the contents of the domains array.
2231 * Returns the number of lttng_domain entries in domains;
2232 * on error, returns a negative value.
2234 int lttng_list_domains(const char *session_name
,
2235 struct lttng_domain
**domains
)
2238 struct lttcomm_session_msg lsm
;
2240 if (session_name
== NULL
) {
2241 ret
= -LTTNG_ERR_INVALID
;
2245 memset(&lsm
, 0, sizeof(lsm
));
2246 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2248 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2249 sizeof(lsm
.session
.name
));
2251 ret
= -LTTNG_ERR_INVALID
;
2255 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2260 return ret
/ sizeof(struct lttng_domain
);
2266 * Ask the session daemon for all available channels of a session.
2267 * Sets the contents of the channels array.
2268 * Returns the number of lttng_channel entries in channels;
2269 * on error, returns a negative value.
2271 int lttng_list_channels(struct lttng_handle
*handle
,
2272 struct lttng_channel
**channels
)
2275 size_t channel_count
, i
;
2276 const size_t channel_size
= sizeof(struct lttng_channel
) +
2277 sizeof(struct lttng_channel_extended
);
2278 struct lttcomm_session_msg lsm
;
2281 if (handle
== NULL
) {
2282 ret
= -LTTNG_ERR_INVALID
;
2286 memset(&lsm
, 0, sizeof(lsm
));
2287 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2288 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2289 sizeof(lsm
.session
.name
));
2291 ret
= -LTTNG_ERR_INVALID
;
2295 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2297 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2302 if (ret
% channel_size
) {
2303 ret
= -LTTNG_ERR_UNK
;
2308 channel_count
= (size_t) ret
/ channel_size
;
2310 /* Set extended info pointers */
2311 extended_at
= ((void *) *channels
) +
2312 channel_count
* sizeof(struct lttng_channel
);
2313 for (i
= 0; i
< channel_count
; i
++) {
2314 struct lttng_channel
*chan
= &(*channels
)[i
];
2316 chan
->attr
.extended
.ptr
= extended_at
;
2317 extended_at
+= sizeof(struct lttng_channel_extended
);
2320 ret
= (int) channel_count
;
2326 * Ask the session daemon for all available events of a session channel.
2327 * Sets the contents of the events array.
2328 * Returns the number of lttng_event entries in events;
2329 * on error, returns a negative value.
2331 int lttng_list_events(struct lttng_handle
*handle
,
2332 const char *channel_name
, struct lttng_event
**events
)
2335 struct lttcomm_session_msg lsm
= {};
2336 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2337 uint32_t nb_events
, i
;
2338 const void *comm_ext_at
;
2339 struct lttng_dynamic_buffer listing
;
2341 struct lttng_payload payload
;
2342 struct lttng_payload payload_copy
;
2343 struct lttng_payload_view lsm_view
=
2344 lttng_payload_view_init_from_buffer(
2345 (const char *) &lsm
, 0, sizeof(lsm
));
2346 struct lttng_buffer_view cmd_header_view
;
2347 struct lttng_buffer_view cmd_payload_view
;
2348 struct lttng_buffer_view flat_events_view
;
2349 struct lttng_buffer_view ext_view
;
2351 /* Safety check. An handle and channel name are mandatory */
2352 if (handle
== NULL
|| channel_name
== NULL
) {
2353 ret
= -LTTNG_ERR_INVALID
;
2357 lttng_payload_init(&payload
);
2358 lttng_payload_init(&payload_copy
);
2360 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2361 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2362 sizeof(lsm
.session
.name
));
2364 ret
= -LTTNG_ERR_INVALID
;
2368 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2369 sizeof(lsm
.u
.list
.channel_name
));
2371 ret
= -LTTNG_ERR_INVALID
;
2375 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2377 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2383 * A copy of the payload is performed since it will be
2384 * consumed twice. Consuming the same payload twice is invalid since
2385 * it will cause any received file descriptor to become "shared"
2386 * between different instances of the resulting objects.
2388 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2390 ret
= -LTTNG_ERR_NOMEM
;
2394 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2395 &payload
.buffer
, 0, sizeof(*cmd_header
));
2396 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2397 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2401 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2403 /* Set number of events and free command header */
2404 nb_events
= cmd_header
->nb_events
;
2405 if (nb_events
> INT_MAX
) {
2406 ret
= -LTTNG_ERR_OVERFLOW
;
2410 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2411 &payload
.buffer
, sizeof(*cmd_header
), -1);
2414 * The buffer that is returned must contain a "flat" version of
2415 * the events that are returned. In other words, all pointers
2416 * within an lttng_event must point to a location within the returned
2417 * buffer so that the user may free everything by simply calling free()
2418 * on the returned buffer. This is needed in order to maintain API
2421 * A first pass is performed to compute the size of the buffer that
2422 * must be allocated. A second pass is then performed to setup
2423 * the returned events so that their members always point within the
2426 * The layout of the returned buffer is as follows:
2427 * - struct lttng_event[nb_events],
2428 * - nb_events times the following:
2429 * - struct lttng_event_extended,
2430 * - flattened version of userspace_probe_location
2431 * - filter_expression
2433 * - padding to align to 64-bits
2435 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2436 nb_events
* sizeof(struct lttng_event
), -1);
2437 comm_ext_at
= ext_view
.data
;
2438 storage_req
= nb_events
* sizeof(struct lttng_event
);
2440 struct lttng_payload_view payload_view
=
2441 lttng_payload_view_from_payload(&payload
, 0, -1);
2443 for (i
= 0; i
< nb_events
; i
++) {
2444 const struct lttcomm_event_extended_header
*ext_comm
=
2445 (struct lttcomm_event_extended_header
*)
2447 int probe_storage_req
= 0;
2449 comm_ext_at
+= sizeof(*ext_comm
);
2450 comm_ext_at
+= ext_comm
->filter_len
;
2451 comm_ext_at
+= ext_comm
->nb_exclusions
*
2452 LTTNG_SYMBOL_NAME_LEN
;
2454 if (ext_comm
->userspace_probe_location_len
) {
2455 struct lttng_userspace_probe_location
2456 *probe_location
= NULL
;
2457 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2459 (const char *) comm_ext_at
-
2460 payload_view
.buffer
.data
,
2461 ext_comm
->userspace_probe_location_len
);
2463 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2464 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2469 * Create a temporary userspace probe location
2470 * to determine the size needed by a "flattened"
2471 * version of that same probe location.
2473 ret
= lttng_userspace_probe_location_create_from_payload(
2474 &probe_location_view
,
2477 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2481 ret
= lttng_userspace_probe_location_flatten(
2482 probe_location
, NULL
);
2483 lttng_userspace_probe_location_destroy(
2486 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2490 probe_storage_req
= ret
;
2491 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2494 storage_req
+= sizeof(struct lttng_event_extended
);
2495 storage_req
+= ext_comm
->filter_len
;
2496 storage_req
+= ext_comm
->nb_exclusions
*
2497 LTTNG_SYMBOL_NAME_LEN
;
2498 /* Padding to ensure the flat probe is aligned. */
2499 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2500 storage_req
+= probe_storage_req
;
2504 lttng_dynamic_buffer_init(&listing
);
2506 * We must ensure that "listing" is never resized so as to preserve
2507 * the validity of the flattened objects.
2509 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2511 ret
= -LTTNG_ERR_NOMEM
;
2515 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2516 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2517 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2518 nb_events
* sizeof(struct lttng_event
));
2519 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2521 ret
= -LTTNG_ERR_NOMEM
;
2522 goto free_dynamic_buffer
;
2525 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2526 nb_events
* sizeof(struct lttng_event
), -1);
2527 comm_ext_at
= ext_view
.data
;
2530 struct lttng_payload_view payload_copy_view
=
2531 lttng_payload_view_from_payload(
2532 &payload_copy
, 0, -1);
2534 for (i
= 0; i
< nb_events
; i
++) {
2535 struct lttng_event
*event
= (typeof(event
))(
2537 (sizeof(struct lttng_event
) * i
));
2538 const struct lttcomm_event_extended_header
*ext_comm
=
2539 (typeof(ext_comm
)) comm_ext_at
;
2540 struct lttng_event_extended
*event_extended
=
2541 (typeof(event_extended
))(listing
.data
+
2544 /* Insert struct lttng_event_extended. */
2545 ret
= lttng_dynamic_buffer_set_size(&listing
,
2546 listing
.size
+ sizeof(*event_extended
));
2548 ret
= -LTTNG_ERR_NOMEM
;
2549 goto free_dynamic_buffer
;
2551 event
->extended
.ptr
= event_extended
;
2553 comm_ext_at
+= sizeof(*ext_comm
);
2555 /* Insert filter expression. */
2556 if (ext_comm
->filter_len
) {
2557 event_extended
->filter_expression
=
2558 listing
.data
+ listing
.size
;
2559 ret
= lttng_dynamic_buffer_append(&listing
,
2561 ext_comm
->filter_len
);
2563 ret
= -LTTNG_ERR_NOMEM
;
2564 goto free_dynamic_buffer
;
2566 comm_ext_at
+= ext_comm
->filter_len
;
2569 /* Insert exclusions. */
2570 if (ext_comm
->nb_exclusions
) {
2571 event_extended
->exclusions
.count
=
2572 ext_comm
->nb_exclusions
;
2573 event_extended
->exclusions
.strings
=
2574 listing
.data
+ listing
.size
;
2576 ret
= lttng_dynamic_buffer_append(&listing
,
2578 ext_comm
->nb_exclusions
*
2579 LTTNG_SYMBOL_NAME_LEN
);
2581 ret
= -LTTNG_ERR_NOMEM
;
2582 goto free_dynamic_buffer
;
2584 comm_ext_at
+= ext_comm
->nb_exclusions
*
2585 LTTNG_SYMBOL_NAME_LEN
;
2588 /* Insert padding to align to 64-bits. */
2589 ret
= lttng_dynamic_buffer_set_size(&listing
,
2590 ALIGN_TO(listing
.size
,
2593 ret
= -LTTNG_ERR_NOMEM
;
2594 goto free_dynamic_buffer
;
2597 /* Insert flattened userspace probe location. */
2598 if (ext_comm
->userspace_probe_location_len
) {
2599 struct lttng_userspace_probe_location
2600 *probe_location
= NULL
;
2601 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2603 (const char *) comm_ext_at
-
2604 payload_copy_view
.buffer
.data
,
2605 ext_comm
->userspace_probe_location_len
);
2607 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2608 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2609 goto free_dynamic_buffer
;
2612 ret
= lttng_userspace_probe_location_create_from_payload(
2613 &probe_location_view
,
2616 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2617 goto free_dynamic_buffer
;
2620 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2623 ret
= lttng_userspace_probe_location_flatten(
2624 probe_location
, &listing
);
2625 lttng_userspace_probe_location_destroy(
2628 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2629 goto free_dynamic_buffer
;
2632 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2637 /* Don't reset listing buffer as we return its content. */
2638 *events
= (struct lttng_event
*) listing
.data
;
2639 lttng_dynamic_buffer_init(&listing
);
2640 ret
= (int) nb_events
;
2641 free_dynamic_buffer
:
2642 lttng_dynamic_buffer_reset(&listing
);
2644 lttng_payload_reset(&payload
);
2645 lttng_payload_reset(&payload_copy
);
2650 * Sets the tracing_group variable with name.
2651 * This function allocates memory pointed to by tracing_group.
2652 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2654 int lttng_set_tracing_group(const char *name
)
2658 return -LTTNG_ERR_INVALID
;
2661 if (asprintf(&new_group
, "%s", name
) < 0) {
2662 return -LTTNG_ERR_FATAL
;
2665 free(tracing_group
);
2666 tracing_group
= new_group
;
2672 int lttng_calibrate(struct lttng_handle
*handle
,
2673 struct lttng_calibrate
*calibrate
)
2676 * This command was removed in LTTng 2.9.
2678 return -LTTNG_ERR_UND
;
2682 * Set default channel attributes.
2683 * If either or both of the arguments are null, attr content is zeroe'd.
2685 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2686 struct lttng_channel_attr
*attr
)
2688 struct lttng_channel_extended
*extended
;
2691 if (attr
== NULL
|| domain
== NULL
) {
2695 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2696 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2698 /* Same for all domains. */
2699 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2700 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2701 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2703 switch (domain
->type
) {
2704 case LTTNG_DOMAIN_KERNEL
:
2705 attr
->switch_timer_interval
=
2706 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2707 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2708 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2709 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2710 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2712 extended
->monitor_timer_interval
=
2713 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2714 extended
->blocking_timeout
=
2715 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2718 case LTTNG_DOMAIN_UST
:
2719 switch (domain
->buf_type
) {
2720 case LTTNG_BUFFER_PER_UID
:
2721 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2722 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2723 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2724 attr
->switch_timer_interval
=
2725 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2726 attr
->read_timer_interval
=
2727 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2729 extended
->monitor_timer_interval
=
2730 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2731 extended
->blocking_timeout
=
2732 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2735 case LTTNG_BUFFER_PER_PID
:
2737 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2738 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2739 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2740 attr
->switch_timer_interval
=
2741 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2742 attr
->read_timer_interval
=
2743 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2745 extended
->monitor_timer_interval
=
2746 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2747 extended
->blocking_timeout
=
2748 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2753 /* Default behavior: leave set to 0. */
2757 attr
->extended
.ptr
= extended
;
2760 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2761 uint64_t *discarded_events
)
2764 struct lttng_channel_extended
*chan_ext
;
2766 if (!channel
|| !discarded_events
) {
2767 ret
= -LTTNG_ERR_INVALID
;
2771 chan_ext
= channel
->attr
.extended
.ptr
;
2774 * This can happen since the lttng_channel structure is
2775 * used for other tasks where this pointer is never set.
2777 *discarded_events
= 0;
2781 *discarded_events
= chan_ext
->discarded_events
;
2786 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2787 uint64_t *lost_packets
)
2790 struct lttng_channel_extended
*chan_ext
;
2792 if (!channel
|| !lost_packets
) {
2793 ret
= -LTTNG_ERR_INVALID
;
2797 chan_ext
= channel
->attr
.extended
.ptr
;
2800 * This can happen since the lttng_channel structure is
2801 * used for other tasks where this pointer is never set.
2807 *lost_packets
= chan_ext
->lost_packets
;
2812 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2813 uint64_t *monitor_timer_interval
)
2817 if (!chan
|| !monitor_timer_interval
) {
2818 ret
= -LTTNG_ERR_INVALID
;
2822 if (!chan
->attr
.extended
.ptr
) {
2823 ret
= -LTTNG_ERR_INVALID
;
2827 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2828 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2833 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2834 uint64_t monitor_timer_interval
)
2838 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2839 ret
= -LTTNG_ERR_INVALID
;
2843 ((struct lttng_channel_extended
*)
2844 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2845 monitor_timer_interval
;
2850 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2851 int64_t *blocking_timeout
)
2855 if (!chan
|| !blocking_timeout
) {
2856 ret
= -LTTNG_ERR_INVALID
;
2860 if (!chan
->attr
.extended
.ptr
) {
2861 ret
= -LTTNG_ERR_INVALID
;
2865 *blocking_timeout
= ((struct lttng_channel_extended
*)
2866 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2871 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2872 int64_t blocking_timeout
)
2875 int64_t msec_timeout
;
2877 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2878 ret
= -LTTNG_ERR_INVALID
;
2882 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2883 ret
= -LTTNG_ERR_INVALID
;
2888 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2889 * us to accept a narrower range of values (msecs expressed as a signed
2892 msec_timeout
= blocking_timeout
/ 1000;
2893 if (msec_timeout
!= (int32_t) msec_timeout
) {
2894 ret
= -LTTNG_ERR_INVALID
;
2898 ((struct lttng_channel_extended
*)
2899 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2906 * Check if session daemon is alive.
2908 * Return 1 if alive or 0 if not.
2909 * On error returns a negative value.
2911 int lttng_session_daemon_alive(void)
2915 ret
= set_session_daemon_path();
2921 if (*sessiond_sock_path
== '\0') {
2923 * No socket path set. Weird error which means the constructor
2929 ret
= try_connect_sessiond(sessiond_sock_path
);
2940 * Set URL for a consumer for a session and domain.
2942 * Return 0 on success, else a negative value.
2944 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2945 const char *control_url
, const char *data_url
)
2949 struct lttcomm_session_msg lsm
;
2950 struct lttng_uri
*uris
= NULL
;
2952 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2953 ret
= -LTTNG_ERR_INVALID
;
2957 memset(&lsm
, 0, sizeof(lsm
));
2959 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2961 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2962 sizeof(lsm
.session
.name
));
2964 ret
= -LTTNG_ERR_INVALID
;
2968 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2970 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2972 ret
= -LTTNG_ERR_INVALID
;
2976 lsm
.u
.uri
.size
= size
;
2978 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2979 sizeof(struct lttng_uri
) * size
, NULL
);
2989 int lttng_enable_consumer(struct lttng_handle
*handle
);
2990 int lttng_enable_consumer(struct lttng_handle
*handle
)
2998 int lttng_disable_consumer(struct lttng_handle
*handle
);
2999 int lttng_disable_consumer(struct lttng_handle
*handle
)
3007 int _lttng_create_session_ext(const char *name
, const char *url
,
3008 const char *datetime
);
3009 int _lttng_create_session_ext(const char *name
, const char *url
,
3010 const char *datetime
)
3016 * For a given session name, this call checks if the data is ready to be read
3017 * or is still being extracted by the consumer(s) hence not ready to be used by
3020 int lttng_data_pending(const char *session_name
)
3023 struct lttcomm_session_msg lsm
;
3024 uint8_t *pending
= NULL
;
3026 if (session_name
== NULL
) {
3027 return -LTTNG_ERR_INVALID
;
3030 memset(&lsm
, 0, sizeof(lsm
));
3031 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
3033 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3034 sizeof(lsm
.session
.name
));
3036 ret
= -LTTNG_ERR_INVALID
;
3040 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
3043 } else if (ret
!= 1) {
3044 /* Unexpected payload size */
3045 ret
= -LTTNG_ERR_INVALID
;
3047 } else if (!pending
) {
3048 /* Internal error. */
3049 ret
= -LTTNG_ERR_UNK
;
3053 ret
= (int) *pending
;
3060 * Regenerate the metadata for a session.
3061 * Return 0 on success, a negative error code on error.
3063 int lttng_regenerate_metadata(const char *session_name
)
3066 struct lttcomm_session_msg lsm
;
3068 if (!session_name
) {
3069 ret
= -LTTNG_ERR_INVALID
;
3073 memset(&lsm
, 0, sizeof(lsm
));
3074 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3076 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3077 sizeof(lsm
.session
.name
));
3079 ret
= -LTTNG_ERR_INVALID
;
3083 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3094 * Deprecated, replaced by lttng_regenerate_metadata.
3096 int lttng_metadata_regenerate(const char *session_name
)
3098 return lttng_regenerate_metadata(session_name
);
3102 * Regenerate the statedump of a session.
3103 * Return 0 on success, a negative error code on error.
3105 int lttng_regenerate_statedump(const char *session_name
)
3108 struct lttcomm_session_msg lsm
;
3110 if (!session_name
) {
3111 ret
= -LTTNG_ERR_INVALID
;
3115 memset(&lsm
, 0, sizeof(lsm
));
3116 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3118 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3119 sizeof(lsm
.session
.name
));
3121 ret
= -LTTNG_ERR_INVALID
;
3125 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3136 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3140 struct lttcomm_session_msg lsm
= {
3141 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3142 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
3144 struct lttcomm_session_msg
*message_lsm
;
3145 struct lttng_payload message
;
3146 struct lttng_payload reply
;
3147 struct lttng_trigger
*reply_trigger
= NULL
;
3148 enum lttng_domain_type domain_type
;
3149 const struct lttng_credentials user_creds
= {
3150 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3151 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3153 const char *unused_trigger_name
= NULL
;
3154 enum lttng_trigger_status trigger_status
;
3156 lttng_payload_init(&message
);
3157 lttng_payload_init(&reply
);
3160 ret
= -LTTNG_ERR_INVALID
;
3164 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3165 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3166 /* Re-using already registered trigger. */
3167 ret
= -LTTNG_ERR_INVALID
;
3172 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3173 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3174 ret
= -LTTNG_ERR_NOMEM
;
3179 if (!trigger
->creds
.uid
.is_set
) {
3180 /* Use the client's credentials as the trigger credentials. */
3181 lttng_trigger_set_credentials(trigger
, &user_creds
);
3184 * Validate that either the current trigger credentials and the
3185 * client credentials are identical or that the current user is
3186 * root. The root user can register, unregister triggers for
3187 * himself and other users.
3189 * This check is also present on the sessiond side, using the
3190 * credentials passed on the socket. These check are all
3193 const struct lttng_credentials
*trigger_creds
=
3194 lttng_trigger_get_credentials(trigger
);
3196 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3197 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3198 ret
= -LTTNG_ERR_EPERM
;
3199 goto end_unset_name
;
3204 if (!lttng_trigger_validate(trigger
)) {
3205 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3206 goto end_unset_name
;
3209 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3212 lsm
.domain
.type
= domain_type
;
3214 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3216 ret
= -LTTNG_ERR_NOMEM
;
3217 goto end_unset_name
;
3220 ret
= lttng_trigger_serialize(trigger
, &message
);
3222 ret
= -LTTNG_ERR_UNK
;
3223 goto end_unset_name
;
3227 * This is needed to populate the trigger object size for the command
3230 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3232 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3235 struct lttng_payload_view message_view
=
3236 lttng_payload_view_from_payload(
3239 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3241 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3243 goto end_unset_name
;
3248 struct lttng_payload_view reply_view
=
3249 lttng_payload_view_from_payload(
3250 &reply
, 0, reply
.buffer
.size
);
3252 ret
= lttng_trigger_create_from_payload(
3253 &reply_view
, &reply_trigger
);
3255 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3256 goto end_unset_name
;
3260 if (name
|| generate_name
) {
3261 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3263 ret
= -LTTNG_ERR_NOMEM
;
3272 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3273 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3274 ret
= -LTTNG_ERR_UNK
;
3277 lttng_payload_reset(&message
);
3278 lttng_payload_reset(&reply
);
3279 lttng_trigger_destroy(reply_trigger
);
3283 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3285 /* Register an anonymous trigger. */
3286 return _lttng_register_trigger(trigger
, NULL
, false);
3289 enum lttng_error_code
lttng_register_trigger_with_name(
3290 struct lttng_trigger
*trigger
, const char *name
)
3292 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3294 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3297 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3298 struct lttng_trigger
*trigger
)
3300 const int ret
= _lttng_register_trigger(trigger
, false, true);
3302 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3305 enum lttng_error_code
lttng_error_query_execute(
3306 const struct lttng_error_query
*query
,
3307 const struct lttng_endpoint
*endpoint
,
3308 struct lttng_error_query_results
**results
)
3311 enum lttng_error_code ret_code
;
3312 struct lttcomm_session_msg lsm
= {
3313 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3315 struct lttng_payload message
;
3316 struct lttng_payload reply
;
3317 struct lttcomm_session_msg
*message_lsm
;
3319 lttng_payload_init(&message
);
3320 lttng_payload_init(&reply
);
3322 if (!query
|| !results
) {
3323 ret_code
= LTTNG_ERR_INVALID
;
3327 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3328 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3332 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3334 ret_code
= LTTNG_ERR_NOMEM
;
3338 ret
= lttng_error_query_serialize(query
, &message
);
3340 ret_code
= LTTNG_ERR_UNK
;
3344 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3345 message_lsm
->u
.error_query
.length
=
3346 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3349 struct lttng_payload_view message_view
=
3350 lttng_payload_view_from_payload(
3353 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3355 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3363 ssize_t reply_create_ret
;
3364 struct lttng_payload_view reply_view
=
3365 lttng_payload_view_from_payload(
3366 &reply
, 0, reply
.buffer
.size
);
3368 reply_create_ret
= lttng_error_query_results_create_from_payload(
3369 &reply_view
, results
);
3370 if (reply_create_ret
< 0) {
3371 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3376 ret_code
= LTTNG_OK
;
3378 lttng_payload_reset(&message
);
3379 lttng_payload_reset(&reply
);
3383 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3386 struct lttcomm_session_msg lsm
;
3387 struct lttcomm_session_msg
*message_lsm
;
3388 struct lttng_payload message
;
3389 struct lttng_payload reply
;
3390 struct lttng_trigger
*copy
= NULL
;
3391 const struct lttng_credentials user_creds
= {
3392 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3393 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3396 lttng_payload_init(&message
);
3397 lttng_payload_init(&reply
);
3400 ret
= -LTTNG_ERR_INVALID
;
3404 copy
= lttng_trigger_copy(trigger
);
3406 ret
= -LTTNG_ERR_UNK
;
3410 if (!copy
->creds
.uid
.is_set
) {
3411 /* Use the client credentials as the trigger credentials */
3412 lttng_trigger_set_credentials(copy
, &user_creds
);
3415 * Validate that either the current trigger credentials and the
3416 * client credentials are identical or that the current user is
3417 * root. The root user can register, unregister triggers for
3418 * himself and other users.
3420 * This check is also present on the sessiond side, using the
3421 * credentials passed on the socket. These check are all
3424 const struct lttng_credentials
*trigger_creds
=
3425 lttng_trigger_get_credentials(copy
);
3426 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3427 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3428 ret
= -LTTNG_ERR_EPERM
;
3434 if (!lttng_trigger_validate(copy
)) {
3435 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3439 memset(&lsm
, 0, sizeof(lsm
));
3440 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3442 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3444 ret
= -LTTNG_ERR_NOMEM
;
3448 ret
= lttng_trigger_serialize(copy
, &message
);
3450 ret
= -LTTNG_ERR_UNK
;
3455 * This is needed to populate the trigger object size for the command
3456 * header and number of fds sent.
3458 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3460 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3463 struct lttng_payload_view message_view
=
3464 lttng_payload_view_from_payload(
3468 * Update the message header with the number of fd that will be
3471 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3474 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3482 lttng_trigger_destroy(copy
);
3483 lttng_payload_reset(&message
);
3484 lttng_payload_reset(&reply
);
3489 * Ask the session daemon for all registered triggers for the current user.
3491 * Allocates and return an lttng_triggers set.
3492 * On error, returns a suitable lttng_error_code.
3494 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3497 enum lttng_error_code ret_code
= LTTNG_OK
;
3498 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3499 struct lttng_triggers
*local_triggers
= NULL
;
3500 struct lttng_payload reply
;
3501 struct lttng_payload_view lsm_view
=
3502 lttng_payload_view_init_from_buffer(
3503 (const char *) &lsm
, 0, sizeof(lsm
));
3505 lttng_payload_init(&reply
);
3507 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3509 ret_code
= (enum lttng_error_code
) -ret
;
3514 struct lttng_payload_view reply_view
=
3515 lttng_payload_view_from_payload(
3516 &reply
, 0, reply
.buffer
.size
);
3518 ret
= lttng_triggers_create_from_payload(
3519 &reply_view
, &local_triggers
);
3521 ret_code
= LTTNG_ERR_FATAL
;
3526 *triggers
= local_triggers
;
3527 local_triggers
= NULL
;
3529 lttng_payload_reset(&reply
);
3530 lttng_triggers_destroy(local_triggers
);
3537 static void __attribute__((constructor
)) init(void)
3539 /* Set default session group */
3540 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3546 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3548 free(tracing_group
);