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
= { .cmd_type
= LTTNG_ENABLE_EVENT
};
1109 struct lttng_payload payload
;
1111 unsigned int free_filter_expression
= 0;
1112 struct filter_parser_ctx
*ctx
= NULL
;
1113 size_t bytecode_len
= 0;
1116 * We have either a filter or some exclusions, so we need to set up
1117 * a variable-length payload from where to send the data.
1119 lttng_payload_init(&payload
);
1122 * Cast as non-const since we may replace the filter expression
1123 * by a dynamically allocated string. Otherwise, the original
1124 * string is not modified.
1126 char *filter_expression
= (char *) original_filter_expression
;
1128 if (handle
== NULL
|| ev
== NULL
) {
1129 ret
= -LTTNG_ERR_INVALID
;
1134 * Empty filter string will always be rejected by the parser
1135 * anyway, so treat this corner-case early to eliminate
1136 * lttng_fmemopen error for 0-byte allocation.
1138 if (filter_expression
&& filter_expression
[0] == '\0') {
1139 ret
= -LTTNG_ERR_INVALID
;
1143 if (ev
->name
[0] == '\0') {
1144 /* Enable all events. */
1145 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1149 /* Parse filter expression. */
1150 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1151 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1152 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1153 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1154 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1155 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1158 /* Setup agent filter if needed. */
1159 agent_filter
= set_agent_filter(filter_expression
, ev
);
1160 if (!agent_filter
) {
1161 if (!filter_expression
) {
1163 * No JUL and no filter, just skip
1170 * With an agent filter, the original filter has
1171 * been added to it thus replace the filter
1174 filter_expression
= agent_filter
;
1175 free_filter_expression
= 1;
1179 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1180 LTTNG_FILTER_MAX_LEN
) {
1181 ret
= -LTTNG_ERR_FILTER_INVAL
;
1185 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1190 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1191 sizeof(ctx
->bytecode
->b
);
1192 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1193 ret
= -LTTNG_ERR_FILTER_INVAL
;
1199 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1200 filter_expression
, bytecode_len
,
1201 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1204 ret
= -LTTNG_ERR_INVALID
;
1208 /* If no channel name, send empty string. */
1209 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1210 sizeof(lsm
.u
.enable
.channel_name
));
1212 ret
= -LTTNG_ERR_INVALID
;
1217 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1220 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1221 sizeof(lsm
.session
.name
));
1223 ret
= -LTTNG_ERR_INVALID
;
1227 /* Length of the serialized event. */
1228 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1231 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1233 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1240 assert(fd_count
== 0 || fd_count
== 1);
1241 if (fd_count
== 1) {
1242 struct fd_handle
*h
=
1243 lttng_payload_view_pop_fd_handle(&view
);
1249 fd_to_send
= fd_handle_get_fd(h
);
1253 lsm
.fd_count
= fd_count
;
1255 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1256 fd_count
? &fd_to_send
: NULL
, fd_count
,
1257 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1258 view
.buffer
.size
, NULL
, NULL
, 0);
1262 if (filter_expression
&& ctx
) {
1263 filter_bytecode_free(ctx
);
1264 filter_ir_free(ctx
);
1265 filter_parser_ctx_free(ctx
);
1267 if (free_filter_expression
) {
1269 * The filter expression has been replaced and must be freed as
1270 * it is not the original filter expression received as a
1273 free(filter_expression
);
1276 * Return directly to the caller and don't ask the sessiond since
1277 * something went wrong in the parsing of data above.
1279 lttng_payload_reset(&payload
);
1283 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1284 struct lttng_event
*ev
, const char *channel_name
,
1285 const char *original_filter_expression
)
1287 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_DISABLE_EVENT
};
1288 struct lttng_payload payload
;
1290 unsigned int free_filter_expression
= 0;
1291 struct filter_parser_ctx
*ctx
= NULL
;
1292 size_t bytecode_len
= 0;
1295 * We have either a filter or some exclusions, so we need to set up
1296 * a variable-length payload from where to send the data.
1298 lttng_payload_init(&payload
);
1301 * Cast as non-const since we may replace the filter expression
1302 * by a dynamically allocated string. Otherwise, the original
1303 * string is not modified.
1305 char *filter_expression
= (char *) original_filter_expression
;
1307 if (handle
== NULL
|| ev
== NULL
) {
1308 ret
= -LTTNG_ERR_INVALID
;
1313 * Empty filter string will always be rejected by the parser
1314 * anyway, so treat this corner-case early to eliminate
1315 * lttng_fmemopen error for 0-byte allocation.
1317 if (filter_expression
&& filter_expression
[0] == '\0') {
1318 ret
= -LTTNG_ERR_INVALID
;
1322 /* Parse filter expression. */
1323 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1324 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1325 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1326 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1327 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1328 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1331 /* Setup agent filter if needed. */
1332 agent_filter
= set_agent_filter(filter_expression
, ev
);
1333 if (!agent_filter
) {
1334 if (!filter_expression
) {
1336 * No JUL and no filter, just skip
1343 * With an agent filter, the original filter has
1344 * been added to it thus replace the filter
1347 filter_expression
= agent_filter
;
1348 free_filter_expression
= 1;
1352 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1353 LTTNG_FILTER_MAX_LEN
) {
1354 ret
= -LTTNG_ERR_FILTER_INVAL
;
1358 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1363 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1364 sizeof(ctx
->bytecode
->b
);
1365 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1366 ret
= -LTTNG_ERR_FILTER_INVAL
;
1372 ret
= lttng_event_serialize(ev
, 0, NULL
,
1373 filter_expression
, bytecode_len
,
1374 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1377 ret
= -LTTNG_ERR_INVALID
;
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
;
1390 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1393 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1394 sizeof(lsm
.session
.name
));
1396 ret
= -LTTNG_ERR_INVALID
;
1400 /* Length of the serialized event. */
1401 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1404 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1406 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1413 assert(fd_count
== 0 || fd_count
== 1);
1414 if (fd_count
== 1) {
1415 struct fd_handle
*h
=
1416 lttng_payload_view_pop_fd_handle(&view
);
1422 fd_to_send
= fd_handle_get_fd(h
);
1426 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1427 fd_count
? &fd_to_send
: NULL
, fd_count
,
1428 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1429 view
.buffer
.size
, NULL
, NULL
, 0);
1433 if (filter_expression
&& ctx
) {
1434 filter_bytecode_free(ctx
);
1435 filter_ir_free(ctx
);
1436 filter_parser_ctx_free(ctx
);
1438 if (free_filter_expression
) {
1440 * The filter expression has been replaced and must be freed as
1441 * it is not the original filter expression received as a
1444 free(filter_expression
);
1447 * Return directly to the caller and don't ask the sessiond since
1448 * something went wrong in the parsing of data above.
1450 lttng_payload_reset(&payload
);
1455 * Disable event(s) of a channel and domain.
1456 * If no event name is specified, all events are disabled.
1457 * If no channel name is specified, the default 'channel0' is used.
1458 * Returns size of returned session payload data or a negative error code.
1460 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1461 const char *channel_name
)
1464 struct lttng_event ev
;
1466 memset(&ev
, 0, sizeof(ev
));
1468 ev
.type
= LTTNG_EVENT_ALL
;
1469 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1471 ret
= -LTTNG_ERR_INVALID
;
1475 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1480 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1482 struct lttng_channel
*channel
= NULL
;
1488 /* Validate domain. */
1489 switch (domain
->type
) {
1490 case LTTNG_DOMAIN_UST
:
1491 switch (domain
->buf_type
) {
1492 case LTTNG_BUFFER_PER_UID
:
1493 case LTTNG_BUFFER_PER_PID
:
1499 case LTTNG_DOMAIN_KERNEL
:
1500 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1508 channel
= lttng_channel_create_internal();
1513 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1518 void lttng_channel_destroy(struct lttng_channel
*channel
)
1524 if (channel
->attr
.extended
.ptr
) {
1525 free(channel
->attr
.extended
.ptr
);
1531 * Enable channel per domain
1532 * Returns size of returned session payload data or a negative error code.
1534 int lttng_enable_channel(struct lttng_handle
*handle
,
1535 struct lttng_channel
*in_chan
)
1537 enum lttng_error_code ret_code
;
1539 struct lttng_dynamic_buffer buffer
;
1540 struct lttcomm_session_msg lsm
;
1541 uint64_t total_buffer_size_needed_per_cpu
= 0;
1542 struct lttng_channel
*channel
= NULL
;
1544 lttng_dynamic_buffer_init(&buffer
);
1546 /* NULL arguments are forbidden. No default values. */
1547 if (handle
== NULL
|| in_chan
== NULL
) {
1548 ret
= -LTTNG_ERR_INVALID
;
1553 * Verify that the amount of memory required to create the requested
1554 * buffer is available on the system at the moment.
1556 if (in_chan
->attr
.num_subbuf
>
1557 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1559 ret
= -LTTNG_ERR_OVERFLOW
;
1563 total_buffer_size_needed_per_cpu
=
1564 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1565 ret_code
= check_enough_available_memory(
1566 total_buffer_size_needed_per_cpu
);
1567 if (ret_code
!= LTTNG_OK
) {
1572 /* Copy the channel for easier manipulation. */
1573 channel
= lttng_channel_copy(in_chan
);
1575 ret
= -LTTNG_ERR_NOMEM
;
1579 /* Populate the channel extended attribute if necessary. */
1580 if (!channel
->attr
.extended
.ptr
) {
1581 struct lttng_channel_extended
*extended
=
1582 (struct lttng_channel_extended
*) zmalloc(
1586 ret
= -LTTNG_ERR_NOMEM
;
1589 lttng_channel_set_default_extended_attr(
1590 &handle
->domain
, extended
);
1591 channel
->attr
.extended
.ptr
= extended
;
1594 /* Prepare the payload */
1595 memset(&lsm
, 0, sizeof(lsm
));
1597 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1598 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1600 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1601 sizeof(lsm
.session
.name
));
1603 ret
= -LTTNG_ERR_INVALID
;
1607 ret
= lttng_channel_serialize(channel
, &buffer
);
1609 ret
= -LTTNG_ERR_FATAL
;
1613 lsm
.u
.channel
.length
= buffer
.size
;
1615 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1616 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1618 lttng_channel_destroy(channel
);
1619 lttng_dynamic_buffer_reset(&buffer
);
1624 * All tracing will be stopped for registered events of the channel.
1625 * Returns size of returned session payload data or a negative error code.
1627 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1630 struct lttcomm_session_msg lsm
;
1632 /* Safety check. Both are mandatory. */
1633 if (handle
== NULL
|| name
== NULL
) {
1634 return -LTTNG_ERR_INVALID
;
1637 memset(&lsm
, 0, sizeof(lsm
));
1639 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1641 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1642 sizeof(lsm
.u
.disable
.channel_name
));
1644 ret
= -LTTNG_ERR_INVALID
;
1648 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1650 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1651 sizeof(lsm
.session
.name
));
1653 ret
= -LTTNG_ERR_INVALID
;
1657 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1663 * Lists all available tracepoints of domain.
1664 * Sets the contents of the events array.
1665 * Returns the number of lttng_event entries in events;
1666 * on error, returns a negative value.
1668 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1669 struct lttng_event
**events
)
1671 enum lttng_error_code ret_code
;
1672 int ret
, total_payload_received
;
1673 char *reception_buffer
= NULL
;
1674 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRACEPOINTS
};
1675 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1676 size_t cmd_header_len
;
1677 unsigned int nb_events
= 0;
1679 if (handle
== NULL
) {
1680 ret
= -LTTNG_ERR_INVALID
;
1684 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1686 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1687 (void **) &reception_buffer
, (void **) &cmd_header
,
1693 total_payload_received
= ret
;
1696 ret
= -LTTNG_ERR_UNK
;
1700 if (cmd_header
->count
> INT_MAX
) {
1701 ret
= -LTTNG_ERR_OVERFLOW
;
1705 nb_events
= (unsigned int) cmd_header
->count
;
1708 struct lttng_buffer_view events_view
=
1709 lttng_buffer_view_init(reception_buffer
, 0,
1710 total_payload_received
);
1711 struct lttng_payload_view events_payload_view
=
1712 lttng_payload_view_from_buffer_view(
1713 &events_view
, 0, -1);
1715 ret_code
= lttng_events_create_and_flatten_from_payload(
1716 &events_payload_view
, nb_events
, events
);
1717 if (ret_code
!= LTTNG_OK
) {
1723 ret
= (int) nb_events
;
1727 free(reception_buffer
);
1732 * Lists all available tracepoint fields of domain.
1733 * Sets the contents of the event field array.
1734 * Returns the number of lttng_event_field entries in events;
1735 * on error, returns a negative value.
1737 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1738 struct lttng_event_field
**fields
)
1741 struct lttcomm_session_msg lsm
;
1743 if (handle
== NULL
) {
1744 return -LTTNG_ERR_INVALID
;
1747 memset(&lsm
, 0, sizeof(lsm
));
1748 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1749 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1751 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1756 return ret
/ sizeof(struct lttng_event_field
);
1760 * Lists all available kernel system calls. Allocates and sets the contents of
1763 * Returns the number of lttng_event entries in events; on error, returns a
1766 int lttng_list_syscalls(struct lttng_event
**events
)
1768 enum lttng_error_code ret_code
;
1769 int ret
, total_payload_received
;
1770 char *reception_buffer
= NULL
;
1771 struct lttcomm_session_msg lsm
= {};
1772 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1773 size_t cmd_header_len
;
1774 uint32_t nb_events
= 0;
1777 ret
= -LTTNG_ERR_INVALID
;
1781 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1782 /* Force kernel domain for system calls. */
1783 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1785 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1786 (void **) &reception_buffer
, (void **) &cmd_header
,
1791 total_payload_received
= ret
;
1794 ret
= -LTTNG_ERR_UNK
;
1798 if (cmd_header
->count
> INT_MAX
) {
1799 ret
= -LTTNG_ERR_OVERFLOW
;
1803 nb_events
= (unsigned int) cmd_header
->count
;
1806 const struct lttng_buffer_view events_view
=
1807 lttng_buffer_view_init(reception_buffer
, 0,
1808 total_payload_received
);
1809 struct lttng_payload_view events_payload_view
=
1810 lttng_payload_view_from_buffer_view(
1811 &events_view
, 0, -1);
1813 ret_code
= lttng_events_create_and_flatten_from_payload(
1814 &events_payload_view
, nb_events
, events
);
1815 if (ret_code
!= LTTNG_OK
) {
1821 ret
= (int) nb_events
;
1824 free(reception_buffer
);
1830 * Returns a human readable string describing
1831 * the error code (a negative value).
1833 const char *lttng_strerror(int code
)
1835 return error_get_str(code
);
1838 enum lttng_error_code
lttng_create_session_ext(
1839 struct lttng_session_descriptor
*session_descriptor
)
1841 enum lttng_error_code ret_code
;
1842 struct lttcomm_session_msg lsm
= {
1843 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1846 struct lttng_buffer_view reply_view
;
1848 bool sessiond_must_generate_ouput
;
1849 struct lttng_dynamic_buffer payload
;
1851 size_t descriptor_size
;
1852 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1854 lttng_dynamic_buffer_init(&payload
);
1855 if (!session_descriptor
) {
1856 ret_code
= LTTNG_ERR_INVALID
;
1860 sessiond_must_generate_ouput
=
1861 !lttng_session_descriptor_is_output_destination_initialized(
1862 session_descriptor
);
1863 if (sessiond_must_generate_ouput
) {
1864 const char *home_dir
= utils_get_home_dir();
1865 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1867 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1868 ret_code
= LTTNG_ERR_FATAL
;
1872 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1873 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1876 ret_code
= LTTNG_ERR_NOMEM
;
1881 descriptor_size
= payload
.size
;
1882 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1885 ret_code
= LTTNG_ERR_INVALID
;
1888 descriptor_size
= payload
.size
- descriptor_size
;
1889 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1891 /* Command returns a session descriptor on success. */
1892 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1893 payload
.size
, &reply
);
1894 if (reply_ret
< 0) {
1895 ret_code
= -reply_ret
;
1897 } else if (reply_ret
== 0) {
1898 /* Socket unexpectedly closed by the session daemon. */
1899 ret_code
= LTTNG_ERR_FATAL
;
1903 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1904 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1907 ret_code
= LTTNG_ERR_FATAL
;
1910 ret_code
= LTTNG_OK
;
1911 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1914 lttng_dynamic_buffer_reset(&payload
);
1915 lttng_session_descriptor_destroy(descriptor_reply
);
1920 * Create a new session using name and url for destination.
1922 * Return 0 on success else a negative LTTng error code.
1924 int lttng_create_session(const char *name
, const char *url
)
1928 struct lttng_uri
*uris
= NULL
;
1929 struct lttng_session_descriptor
*descriptor
= NULL
;
1930 enum lttng_error_code ret_code
;
1933 ret
= -LTTNG_ERR_INVALID
;
1937 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1939 ret
= -LTTNG_ERR_INVALID
;
1944 descriptor
= lttng_session_descriptor_create(name
);
1947 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1948 ret
= -LTTNG_ERR_INVALID
;
1951 descriptor
= lttng_session_descriptor_local_create(name
,
1955 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1959 ret
= -LTTNG_ERR_INVALID
;
1963 ret
= -LTTNG_ERR_INVALID
;
1966 ret_code
= lttng_create_session_ext(descriptor
);
1967 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1969 lttng_session_descriptor_destroy(descriptor
);
1975 * Create a session exclusively used for snapshot.
1977 * Return 0 on success else a negative LTTng error code.
1979 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1982 enum lttng_error_code ret_code
;
1984 struct lttng_uri
*uris
= NULL
;
1985 struct lttng_session_descriptor
*descriptor
= NULL
;
1988 ret
= -LTTNG_ERR_INVALID
;
1992 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1994 ret
= -LTTNG_ERR_INVALID
;
1998 * If the user does not specify a custom subdir, use the session name.
2000 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
2001 strlen(uris
[0].subdir
) == 0) {
2002 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2005 PERROR("Failed to set session name as network destination sub-directory");
2006 ret
= -LTTNG_ERR_FATAL
;
2008 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2009 /* Truncated output. */
2010 ret
= -LTTNG_ERR_INVALID
;
2017 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2020 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2021 ret
= -LTTNG_ERR_INVALID
;
2024 descriptor
= lttng_session_descriptor_snapshot_local_create(
2029 descriptor
= lttng_session_descriptor_snapshot_network_create(
2035 ret
= -LTTNG_ERR_INVALID
;
2039 ret
= -LTTNG_ERR_INVALID
;
2042 ret_code
= lttng_create_session_ext(descriptor
);
2043 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2045 lttng_session_descriptor_destroy(descriptor
);
2051 * Create a session exclusively used for live.
2053 * Return 0 on success else a negative LTTng error code.
2055 int lttng_create_session_live(const char *name
, const char *url
,
2056 unsigned int timer_interval
)
2059 enum lttng_error_code ret_code
;
2060 struct lttng_session_descriptor
*descriptor
= NULL
;
2063 ret
= -LTTNG_ERR_INVALID
;
2068 descriptor
= lttng_session_descriptor_live_network_create(
2069 name
, url
, NULL
, timer_interval
);
2071 descriptor
= lttng_session_descriptor_live_create(
2072 name
, timer_interval
);
2075 ret
= -LTTNG_ERR_INVALID
;
2078 ret_code
= lttng_create_session_ext(descriptor
);
2079 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2081 lttng_session_descriptor_destroy(descriptor
);
2086 * Stop the session and wait for the data before destroying it
2088 * Return 0 on success else a negative LTTng error code.
2090 int lttng_destroy_session(const char *session_name
)
2093 enum lttng_error_code ret_code
;
2094 enum lttng_destruction_handle_status status
;
2095 struct lttng_destruction_handle
*handle
= NULL
;
2098 * Stop the tracing and wait for the data to be
2101 ret
= _lttng_stop_tracing(session_name
, 1);
2102 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2106 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2107 if (ret_code
!= LTTNG_OK
) {
2108 ret
= (int) -ret_code
;
2113 /* Block until the completion of the destruction of the session. */
2114 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2115 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2116 ret
= -LTTNG_ERR_UNK
;
2120 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2121 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2122 ret
= -LTTNG_ERR_UNK
;
2125 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2127 lttng_destruction_handle_destroy(handle
);
2132 * Destroy the session without waiting for the data.
2134 int lttng_destroy_session_no_wait(const char *session_name
)
2136 enum lttng_error_code ret_code
;
2138 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2139 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2143 * Ask the session daemon for all available sessions.
2144 * Sets the contents of the sessions array.
2145 * Returns the number of lttng_session entries in sessions;
2146 * on error, returns a negative value.
2148 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2151 struct lttcomm_session_msg lsm
;
2152 const size_t session_size
= sizeof(struct lttng_session
) +
2153 sizeof(struct lttng_session_extended
);
2154 size_t session_count
, i
;
2155 struct lttng_session_extended
*sessions_extended_begin
;
2156 struct lttng_session
*sessions
= NULL
;
2158 memset(&lsm
, 0, sizeof(lsm
));
2159 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2161 * Initialize out_sessions to NULL so it is initialized when
2162 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2163 * be subsequently freed.
2165 *out_sessions
= NULL
;
2166 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2171 ret
= -LTTNG_ERR_FATAL
;
2175 if (ret
% session_size
) {
2176 ret
= -LTTNG_ERR_UNK
;
2180 session_count
= (size_t) ret
/ session_size
;
2181 sessions_extended_begin
= (struct lttng_session_extended
*)
2182 (&sessions
[session_count
]);
2184 /* Set extended session info pointers. */
2185 for (i
= 0; i
< session_count
; i
++) {
2186 struct lttng_session
*session
= &sessions
[i
];
2187 struct lttng_session_extended
*extended
=
2188 &(sessions_extended_begin
[i
]);
2190 session
->extended
.ptr
= extended
;
2193 ret
= (int) session_count
;
2194 *out_sessions
= sessions
;
2199 enum lttng_error_code
lttng_session_get_creation_time(
2200 const struct lttng_session
*session
, uint64_t *creation_time
)
2202 enum lttng_error_code ret
= LTTNG_OK
;
2203 struct lttng_session_extended
*extended
;
2205 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2206 ret
= LTTNG_ERR_INVALID
;
2210 extended
= session
->extended
.ptr
;
2211 if (!extended
->creation_time
.is_set
) {
2212 /* Not created on the session daemon yet. */
2213 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2216 *creation_time
= extended
->creation_time
.value
;
2221 int lttng_set_session_shm_path(const char *session_name
,
2222 const char *shm_path
)
2225 struct lttcomm_session_msg lsm
;
2227 if (session_name
== NULL
) {
2228 return -LTTNG_ERR_INVALID
;
2231 memset(&lsm
, 0, sizeof(lsm
));
2232 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2234 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2235 sizeof(lsm
.session
.name
));
2237 ret
= -LTTNG_ERR_INVALID
;
2241 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2242 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2244 ret
= -LTTNG_ERR_INVALID
;
2248 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2254 * Ask the session daemon for all available domains of a session.
2255 * Sets the contents of the domains array.
2256 * Returns the number of lttng_domain entries in domains;
2257 * on error, returns a negative value.
2259 int lttng_list_domains(const char *session_name
,
2260 struct lttng_domain
**domains
)
2263 struct lttcomm_session_msg lsm
;
2265 if (session_name
== NULL
) {
2266 ret
= -LTTNG_ERR_INVALID
;
2270 memset(&lsm
, 0, sizeof(lsm
));
2271 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2273 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2274 sizeof(lsm
.session
.name
));
2276 ret
= -LTTNG_ERR_INVALID
;
2280 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2285 return ret
/ sizeof(struct lttng_domain
);
2291 * Ask the session daemon for all available channels of a session.
2292 * Sets the contents of the channels array.
2293 * Returns the number of lttng_channel entries in channels;
2294 * on error, returns a negative value.
2296 int lttng_list_channels(struct lttng_handle
*handle
,
2297 struct lttng_channel
**channels
)
2299 int ret
, total_payload_received
;
2300 struct lttcomm_session_msg lsm
;
2301 char *reception_buffer
= NULL
;
2302 size_t cmd_header_len
= 0;
2303 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2304 struct lttng_dynamic_buffer tmp_buffer
;
2306 lttng_dynamic_buffer_init(&tmp_buffer
);
2308 if (handle
== NULL
) {
2309 ret
= -LTTNG_ERR_INVALID
;
2313 memset(&lsm
, 0, sizeof(lsm
));
2314 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2315 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2316 sizeof(lsm
.session
.name
));
2318 ret
= -LTTNG_ERR_INVALID
;
2322 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2324 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2325 (void **) &reception_buffer
, (void **) &cmd_header
,
2331 total_payload_received
= ret
;
2333 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2334 ret
= -LTTNG_ERR_FATAL
;
2339 ret
= LTTNG_ERR_UNK
;
2343 if (cmd_header
->count
> INT_MAX
) {
2344 ret
= -LTTNG_ERR_OVERFLOW
;
2349 enum lttng_error_code ret_code
;
2350 const struct lttng_buffer_view events_view
=
2351 lttng_buffer_view_init(reception_buffer
, 0,
2352 total_payload_received
);
2354 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2355 &events_view
, cmd_header
->count
, channels
);
2356 if (ret_code
!= LTTNG_OK
) {
2362 ret
= (int) cmd_header
->count
;
2365 free(reception_buffer
);
2370 * Ask the session daemon for all available events of a session channel.
2371 * Sets the contents of the events array.
2372 * Returns the number of lttng_event entries in events;
2373 * on error, returns a negative value.
2375 int lttng_list_events(struct lttng_handle
*handle
,
2376 const char *channel_name
, struct lttng_event
**events
)
2379 struct lttcomm_session_msg lsm
= {};
2380 struct lttng_payload reply
;
2381 struct lttng_payload_view lsm_view
=
2382 lttng_payload_view_init_from_buffer(
2383 (const char *) &lsm
, 0, sizeof(lsm
));
2384 unsigned int nb_events
= 0;
2386 /* Safety check. An handle and channel name are mandatory. */
2387 if (handle
== NULL
|| channel_name
== NULL
) {
2388 ret
= -LTTNG_ERR_INVALID
;
2392 lttng_payload_init(&reply
);
2394 /* Initialize command parameters. */
2395 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2396 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2397 sizeof(lsm
.session
.name
));
2399 ret
= -LTTNG_ERR_INVALID
;
2403 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2404 sizeof(lsm
.u
.list
.channel_name
));
2406 ret
= -LTTNG_ERR_INVALID
;
2410 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2412 /* Execute command against the session daemon. */
2413 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2419 const struct lttcomm_list_command_header
*cmd_reply_header
=
2421 const struct lttng_payload_view cmd_reply_header_view
=
2422 lttng_payload_view_from_payload(&reply
, 0,
2423 sizeof(*cmd_reply_header
));
2425 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2426 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2430 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2431 cmd_reply_header_view
.buffer
2433 if (cmd_reply_header
->count
> INT_MAX
) {
2434 ret
= -LTTNG_ERR_OVERFLOW
;
2438 nb_events
= (unsigned int) cmd_reply_header
->count
;
2442 enum lttng_error_code ret_code
;
2443 struct lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2445 sizeof(struct lttcomm_list_command_header
), -1);
2447 ret_code
= lttng_events_create_and_flatten_from_payload(
2448 &cmd_reply_payload
, nb_events
, events
);
2449 if (ret_code
!= LTTNG_OK
) {
2450 ret
= -((int) ret_code
);
2455 ret
= (int) nb_events
;
2457 lttng_payload_reset(&reply
);
2462 * Sets the tracing_group variable with name.
2463 * This function allocates memory pointed to by tracing_group.
2464 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2466 int lttng_set_tracing_group(const char *name
)
2470 return -LTTNG_ERR_INVALID
;
2473 if (asprintf(&new_group
, "%s", name
) < 0) {
2474 return -LTTNG_ERR_FATAL
;
2477 free(tracing_group
);
2478 tracing_group
= new_group
;
2484 int lttng_calibrate(struct lttng_handle
*handle
,
2485 struct lttng_calibrate
*calibrate
)
2488 * This command was removed in LTTng 2.9.
2490 return -LTTNG_ERR_UND
;
2494 * Set default channel attributes.
2495 * If either or both of the arguments are null, attr content is zeroe'd.
2497 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2498 struct lttng_channel_attr
*attr
)
2500 struct lttng_channel_extended
*extended
;
2503 if (attr
== NULL
|| domain
== NULL
) {
2507 /* Save the pointer for later use */
2508 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2509 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2511 /* Same for all domains. */
2512 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2513 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2514 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2516 switch (domain
->type
) {
2517 case LTTNG_DOMAIN_KERNEL
:
2518 attr
->switch_timer_interval
=
2519 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2520 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2521 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2522 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2523 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2525 case LTTNG_DOMAIN_UST
:
2526 switch (domain
->buf_type
) {
2527 case LTTNG_BUFFER_PER_UID
:
2528 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2529 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2530 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2531 attr
->switch_timer_interval
=
2532 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2533 attr
->read_timer_interval
=
2534 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2536 case LTTNG_BUFFER_PER_PID
:
2538 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2539 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2540 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2541 attr
->switch_timer_interval
=
2542 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2543 attr
->read_timer_interval
=
2544 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2548 /* Default behavior: leave set to 0. */
2553 lttng_channel_set_default_extended_attr(domain
, extended
);
2556 /* Reassign the extended pointer. */
2557 attr
->extended
.ptr
= extended
;
2560 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2561 uint64_t *discarded_events
)
2564 struct lttng_channel_extended
*chan_ext
;
2566 if (!channel
|| !discarded_events
) {
2567 ret
= -LTTNG_ERR_INVALID
;
2571 chan_ext
= channel
->attr
.extended
.ptr
;
2574 * This can happen since the lttng_channel structure is
2575 * used for other tasks where this pointer is never set.
2577 *discarded_events
= 0;
2581 *discarded_events
= chan_ext
->discarded_events
;
2586 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2587 uint64_t *lost_packets
)
2590 struct lttng_channel_extended
*chan_ext
;
2592 if (!channel
|| !lost_packets
) {
2593 ret
= -LTTNG_ERR_INVALID
;
2597 chan_ext
= channel
->attr
.extended
.ptr
;
2600 * This can happen since the lttng_channel structure is
2601 * used for other tasks where this pointer is never set.
2607 *lost_packets
= chan_ext
->lost_packets
;
2612 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2613 uint64_t *monitor_timer_interval
)
2617 if (!chan
|| !monitor_timer_interval
) {
2618 ret
= -LTTNG_ERR_INVALID
;
2622 if (!chan
->attr
.extended
.ptr
) {
2623 ret
= -LTTNG_ERR_INVALID
;
2627 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2628 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2633 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2634 uint64_t monitor_timer_interval
)
2638 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2639 ret
= -LTTNG_ERR_INVALID
;
2643 ((struct lttng_channel_extended
*)
2644 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2645 monitor_timer_interval
;
2650 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2651 int64_t *blocking_timeout
)
2655 if (!chan
|| !blocking_timeout
) {
2656 ret
= -LTTNG_ERR_INVALID
;
2660 if (!chan
->attr
.extended
.ptr
) {
2661 ret
= -LTTNG_ERR_INVALID
;
2665 *blocking_timeout
= ((struct lttng_channel_extended
*)
2666 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2671 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2672 int64_t blocking_timeout
)
2675 int64_t msec_timeout
;
2677 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2678 ret
= -LTTNG_ERR_INVALID
;
2682 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2683 ret
= -LTTNG_ERR_INVALID
;
2688 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2689 * us to accept a narrower range of values (msecs expressed as a signed
2692 msec_timeout
= blocking_timeout
/ 1000;
2693 if (msec_timeout
!= (int32_t) msec_timeout
) {
2694 ret
= -LTTNG_ERR_INVALID
;
2698 ((struct lttng_channel_extended
*)
2699 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2706 * Check if session daemon is alive.
2708 * Return 1 if alive or 0 if not.
2709 * On error returns a negative value.
2711 int lttng_session_daemon_alive(void)
2715 ret
= set_session_daemon_path();
2721 if (*sessiond_sock_path
== '\0') {
2723 * No socket path set. Weird error which means the constructor
2729 ret
= try_connect_sessiond(sessiond_sock_path
);
2740 * Set URL for a consumer for a session and domain.
2742 * Return 0 on success, else a negative value.
2744 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2745 const char *control_url
, const char *data_url
)
2749 struct lttcomm_session_msg lsm
;
2750 struct lttng_uri
*uris
= NULL
;
2752 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2753 ret
= -LTTNG_ERR_INVALID
;
2757 memset(&lsm
, 0, sizeof(lsm
));
2759 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2761 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2762 sizeof(lsm
.session
.name
));
2764 ret
= -LTTNG_ERR_INVALID
;
2768 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2770 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2772 ret
= -LTTNG_ERR_INVALID
;
2776 lsm
.u
.uri
.size
= size
;
2778 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2779 sizeof(struct lttng_uri
) * size
, NULL
);
2789 int lttng_enable_consumer(struct lttng_handle
*handle
);
2790 int lttng_enable_consumer(struct lttng_handle
*handle
)
2798 int lttng_disable_consumer(struct lttng_handle
*handle
);
2799 int lttng_disable_consumer(struct lttng_handle
*handle
)
2807 int _lttng_create_session_ext(const char *name
, const char *url
,
2808 const char *datetime
);
2809 int _lttng_create_session_ext(const char *name
, const char *url
,
2810 const char *datetime
)
2816 * For a given session name, this call checks if the data is ready to be read
2817 * or is still being extracted by the consumer(s) hence not ready to be used by
2820 int lttng_data_pending(const char *session_name
)
2823 struct lttcomm_session_msg lsm
;
2824 uint8_t *pending
= NULL
;
2826 if (session_name
== NULL
) {
2827 return -LTTNG_ERR_INVALID
;
2830 memset(&lsm
, 0, sizeof(lsm
));
2831 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2833 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2834 sizeof(lsm
.session
.name
));
2836 ret
= -LTTNG_ERR_INVALID
;
2840 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2843 } else if (ret
!= 1) {
2844 /* Unexpected payload size */
2845 ret
= -LTTNG_ERR_INVALID
;
2847 } else if (!pending
) {
2848 /* Internal error. */
2849 ret
= -LTTNG_ERR_UNK
;
2853 ret
= (int) *pending
;
2860 * Regenerate the metadata for a session.
2861 * Return 0 on success, a negative error code on error.
2863 int lttng_regenerate_metadata(const char *session_name
)
2866 struct lttcomm_session_msg lsm
;
2868 if (!session_name
) {
2869 ret
= -LTTNG_ERR_INVALID
;
2873 memset(&lsm
, 0, sizeof(lsm
));
2874 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2876 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2877 sizeof(lsm
.session
.name
));
2879 ret
= -LTTNG_ERR_INVALID
;
2883 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2894 * Deprecated, replaced by lttng_regenerate_metadata.
2896 int lttng_metadata_regenerate(const char *session_name
)
2898 return lttng_regenerate_metadata(session_name
);
2902 * Regenerate the statedump of a session.
2903 * Return 0 on success, a negative error code on error.
2905 int lttng_regenerate_statedump(const char *session_name
)
2908 struct lttcomm_session_msg lsm
;
2910 if (!session_name
) {
2911 ret
= -LTTNG_ERR_INVALID
;
2915 memset(&lsm
, 0, sizeof(lsm
));
2916 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2918 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2919 sizeof(lsm
.session
.name
));
2921 ret
= -LTTNG_ERR_INVALID
;
2925 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2936 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
2940 struct lttcomm_session_msg lsm
= {
2941 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2942 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
2944 struct lttcomm_session_msg
*message_lsm
;
2945 struct lttng_payload message
;
2946 struct lttng_payload reply
;
2947 struct lttng_trigger
*reply_trigger
= NULL
;
2948 enum lttng_domain_type domain_type
;
2949 const struct lttng_credentials user_creds
= {
2950 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2951 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2953 const char *unused_trigger_name
= NULL
;
2954 enum lttng_trigger_status trigger_status
;
2956 lttng_payload_init(&message
);
2957 lttng_payload_init(&reply
);
2960 ret
= -LTTNG_ERR_INVALID
;
2964 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2965 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2966 /* Re-using already registered trigger. */
2967 ret
= -LTTNG_ERR_INVALID
;
2972 trigger_status
= lttng_trigger_set_name(trigger
, name
);
2973 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2974 ret
= -LTTNG_ERR_NOMEM
;
2979 if (!trigger
->creds
.uid
.is_set
) {
2980 /* Use the client's credentials as the trigger credentials. */
2981 lttng_trigger_set_credentials(trigger
, &user_creds
);
2984 * Validate that either the current trigger credentials and the
2985 * client credentials are identical or that the current user is
2986 * root. The root user can register, unregister triggers for
2987 * himself and other users.
2989 * This check is also present on the sessiond side, using the
2990 * credentials passed on the socket. These check are all
2993 const struct lttng_credentials
*trigger_creds
=
2994 lttng_trigger_get_credentials(trigger
);
2996 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
2997 if (lttng_credentials_get_uid(&user_creds
) != 0) {
2998 ret
= -LTTNG_ERR_EPERM
;
2999 goto end_unset_name
;
3004 if (!lttng_trigger_validate(trigger
)) {
3005 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3006 goto end_unset_name
;
3009 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3012 lsm
.domain
.type
= domain_type
;
3014 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3016 ret
= -LTTNG_ERR_NOMEM
;
3017 goto end_unset_name
;
3020 ret
= lttng_trigger_serialize(trigger
, &message
);
3022 ret
= -LTTNG_ERR_UNK
;
3023 goto end_unset_name
;
3027 * This is needed to populate the trigger object size for the command
3030 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3032 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3035 struct lttng_payload_view message_view
=
3036 lttng_payload_view_from_payload(
3039 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3041 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3043 goto end_unset_name
;
3048 struct lttng_payload_view reply_view
=
3049 lttng_payload_view_from_payload(
3050 &reply
, 0, reply
.buffer
.size
);
3052 ret
= lttng_trigger_create_from_payload(
3053 &reply_view
, &reply_trigger
);
3055 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3056 goto end_unset_name
;
3060 if (name
|| generate_name
) {
3061 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3063 ret
= -LTTNG_ERR_NOMEM
;
3072 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3073 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3074 ret
= -LTTNG_ERR_UNK
;
3077 lttng_payload_reset(&message
);
3078 lttng_payload_reset(&reply
);
3079 lttng_trigger_destroy(reply_trigger
);
3083 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3085 /* Register an anonymous trigger. */
3086 return _lttng_register_trigger(trigger
, NULL
, false);
3089 enum lttng_error_code
lttng_register_trigger_with_name(
3090 struct lttng_trigger
*trigger
, const char *name
)
3092 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3094 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3097 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3098 struct lttng_trigger
*trigger
)
3100 const int ret
= _lttng_register_trigger(trigger
, false, true);
3102 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3105 enum lttng_error_code
lttng_error_query_execute(
3106 const struct lttng_error_query
*query
,
3107 const struct lttng_endpoint
*endpoint
,
3108 struct lttng_error_query_results
**results
)
3111 enum lttng_error_code ret_code
;
3112 struct lttcomm_session_msg lsm
= {
3113 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3115 struct lttng_payload message
;
3116 struct lttng_payload reply
;
3117 struct lttcomm_session_msg
*message_lsm
;
3119 lttng_payload_init(&message
);
3120 lttng_payload_init(&reply
);
3122 if (!query
|| !results
) {
3123 ret_code
= LTTNG_ERR_INVALID
;
3127 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3128 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3132 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3134 ret_code
= LTTNG_ERR_NOMEM
;
3138 ret
= lttng_error_query_serialize(query
, &message
);
3140 ret_code
= LTTNG_ERR_UNK
;
3144 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3145 message_lsm
->u
.error_query
.length
=
3146 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3149 struct lttng_payload_view message_view
=
3150 lttng_payload_view_from_payload(
3153 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3155 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3163 ssize_t reply_create_ret
;
3164 struct lttng_payload_view reply_view
=
3165 lttng_payload_view_from_payload(
3166 &reply
, 0, reply
.buffer
.size
);
3168 reply_create_ret
= lttng_error_query_results_create_from_payload(
3169 &reply_view
, results
);
3170 if (reply_create_ret
< 0) {
3171 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3176 ret_code
= LTTNG_OK
;
3178 lttng_payload_reset(&message
);
3179 lttng_payload_reset(&reply
);
3183 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3186 struct lttcomm_session_msg lsm
;
3187 struct lttcomm_session_msg
*message_lsm
;
3188 struct lttng_payload message
;
3189 struct lttng_payload reply
;
3190 struct lttng_trigger
*copy
= NULL
;
3191 const struct lttng_credentials user_creds
= {
3192 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3193 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3196 lttng_payload_init(&message
);
3197 lttng_payload_init(&reply
);
3200 ret
= -LTTNG_ERR_INVALID
;
3204 copy
= lttng_trigger_copy(trigger
);
3206 ret
= -LTTNG_ERR_UNK
;
3210 if (!copy
->creds
.uid
.is_set
) {
3211 /* Use the client credentials as the trigger credentials */
3212 lttng_trigger_set_credentials(copy
, &user_creds
);
3215 * Validate that either the current trigger credentials and the
3216 * client credentials are identical or that the current user is
3217 * root. The root user can register, unregister triggers for
3218 * himself and other users.
3220 * This check is also present on the sessiond side, using the
3221 * credentials passed on the socket. These check are all
3224 const struct lttng_credentials
*trigger_creds
=
3225 lttng_trigger_get_credentials(copy
);
3226 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3227 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3228 ret
= -LTTNG_ERR_EPERM
;
3234 if (!lttng_trigger_validate(copy
)) {
3235 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3239 memset(&lsm
, 0, sizeof(lsm
));
3240 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3242 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3244 ret
= -LTTNG_ERR_NOMEM
;
3248 ret
= lttng_trigger_serialize(copy
, &message
);
3250 ret
= -LTTNG_ERR_UNK
;
3255 * This is needed to populate the trigger object size for the command
3256 * header and number of fds sent.
3258 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3260 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3263 struct lttng_payload_view message_view
=
3264 lttng_payload_view_from_payload(
3268 * Update the message header with the number of fd that will be
3271 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3274 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3282 lttng_trigger_destroy(copy
);
3283 lttng_payload_reset(&message
);
3284 lttng_payload_reset(&reply
);
3289 * Ask the session daemon for all registered triggers for the current user.
3291 * Allocates and return an lttng_triggers set.
3292 * On error, returns a suitable lttng_error_code.
3294 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3297 enum lttng_error_code ret_code
= LTTNG_OK
;
3298 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3299 struct lttng_triggers
*local_triggers
= NULL
;
3300 struct lttng_payload reply
;
3301 struct lttng_payload_view lsm_view
=
3302 lttng_payload_view_init_from_buffer(
3303 (const char *) &lsm
, 0, sizeof(lsm
));
3305 lttng_payload_init(&reply
);
3307 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3309 ret_code
= (enum lttng_error_code
) -ret
;
3314 struct lttng_payload_view reply_view
=
3315 lttng_payload_view_from_payload(
3316 &reply
, 0, reply
.buffer
.size
);
3318 ret
= lttng_triggers_create_from_payload(
3319 &reply_view
, &local_triggers
);
3321 ret_code
= LTTNG_ERR_FATAL
;
3326 *triggers
= local_triggers
;
3327 local_triggers
= NULL
;
3329 lttng_payload_reset(&reply
);
3330 lttng_triggers_destroy(local_triggers
);
3337 static void __attribute__((constructor
)) init(void)
3339 /* Set default session group */
3340 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3346 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3348 free(tracing_group
);