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
)
922 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_ADD_CONTEXT
};
923 struct lttng_payload payload
;
925 lttng_payload_init(&payload
);
927 /* Safety check. Both are mandatory. */
928 if (handle
== NULL
|| ctx
== NULL
) {
929 ret
= -LTTNG_ERR_INVALID
;
933 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
935 ret
= -LTTNG_ERR_NOMEM
;
939 /* If no channel name, send empty string. */
940 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
941 sizeof(lsm
.u
.context
.channel_name
));
943 ret
= -LTTNG_ERR_INVALID
;
947 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
948 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
949 sizeof(lsm
.session
.name
));
951 ret
= -LTTNG_ERR_INVALID
;
955 ret
= lttng_event_context_serialize(ctx
, &payload
);
957 ret
= -LTTNG_ERR_INVALID
;
961 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
963 /* Update message header. */
964 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
967 struct lttng_payload reply
;
968 struct lttng_payload_view payload_view
=
969 lttng_payload_view_from_payload(&payload
, 0,
972 lttng_payload_init(&reply
);
973 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
974 lttng_payload_reset(&reply
);
981 lttng_payload_reset(&payload
);
986 * Enable event(s) for a channel.
988 * If no event name is specified, all events are enabled.
989 * If no channel name is specified, the default 'channel0' is used.
991 * Returns size of returned session payload data or a negative error code.
993 int lttng_enable_event(struct lttng_handle
*handle
,
994 struct lttng_event
*ev
, const char *channel_name
)
996 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1001 * Create or enable an event with a filter expression.
1003 * Return negative error value on error.
1004 * Return size of returned session payload data if OK.
1006 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1007 struct lttng_event
*event
, const char *channel_name
,
1008 const char *filter_expression
)
1010 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1011 filter_expression
, 0, NULL
);
1015 * Depending on the event, return a newly allocated agent filter expression or
1016 * NULL if not applicable.
1018 * An event with NO loglevel and the name is * will return NULL.
1020 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1023 char *agent_filter
= NULL
;
1027 /* Don't add filter for the '*' event. */
1028 if (strcmp(ev
->name
, "*") != 0) {
1030 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1033 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1041 /* Add loglevel filtering if any for the JUL domain. */
1042 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1045 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1051 if (filter
|| agent_filter
) {
1054 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1055 agent_filter
? agent_filter
: filter
, op
,
1060 agent_filter
= new_filter
;
1062 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1071 return agent_filter
;
1078 * Enable event(s) for a channel, possibly with exclusions and a filter.
1079 * If no event name is specified, all events are enabled.
1080 * If no channel name is specified, the default name is used.
1081 * If filter expression is not NULL, the filter is set for the event.
1082 * If exclusion count is not zero, the exclusions are set for the event.
1083 * Returns size of returned session payload data or a negative error code.
1085 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1086 struct lttng_event
*ev
, const char *channel_name
,
1087 const char *original_filter_expression
,
1088 int exclusion_count
, char **exclusion_list
)
1090 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_ENABLE_EVENT
};
1091 struct lttng_payload payload
;
1093 unsigned int free_filter_expression
= 0;
1094 struct filter_parser_ctx
*ctx
= NULL
;
1095 size_t bytecode_len
= 0;
1098 * We have either a filter or some exclusions, so we need to set up
1099 * a variable-length payload from where to send the data.
1101 lttng_payload_init(&payload
);
1104 * Cast as non-const since we may replace the filter expression
1105 * by a dynamically allocated string. Otherwise, the original
1106 * string is not modified.
1108 char *filter_expression
= (char *) original_filter_expression
;
1110 if (handle
== NULL
|| ev
== NULL
) {
1111 ret
= -LTTNG_ERR_INVALID
;
1116 * Empty filter string will always be rejected by the parser
1117 * anyway, so treat this corner-case early to eliminate
1118 * lttng_fmemopen error for 0-byte allocation.
1120 if (filter_expression
&& filter_expression
[0] == '\0') {
1121 ret
= -LTTNG_ERR_INVALID
;
1125 if (ev
->name
[0] == '\0') {
1126 /* Enable all events. */
1127 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1131 /* Parse filter expression. */
1132 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1133 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1134 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1135 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1136 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1137 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1140 /* Setup agent filter if needed. */
1141 agent_filter
= set_agent_filter(filter_expression
, ev
);
1142 if (!agent_filter
) {
1143 if (!filter_expression
) {
1145 * No JUL and no filter, just skip
1152 * With an agent filter, the original filter has
1153 * been added to it thus replace the filter
1156 filter_expression
= agent_filter
;
1157 free_filter_expression
= 1;
1161 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1162 LTTNG_FILTER_MAX_LEN
) {
1163 ret
= -LTTNG_ERR_FILTER_INVAL
;
1167 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1172 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1173 sizeof(ctx
->bytecode
->b
);
1174 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1175 ret
= -LTTNG_ERR_FILTER_INVAL
;
1181 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1182 filter_expression
, bytecode_len
,
1183 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1186 ret
= -LTTNG_ERR_INVALID
;
1190 /* If no channel name, send empty string. */
1191 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1192 sizeof(lsm
.u
.enable
.channel_name
));
1194 ret
= -LTTNG_ERR_INVALID
;
1199 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1202 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1203 sizeof(lsm
.session
.name
));
1205 ret
= -LTTNG_ERR_INVALID
;
1209 /* Length of the serialized event. */
1210 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1213 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1215 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1222 assert(fd_count
== 0 || fd_count
== 1);
1223 if (fd_count
== 1) {
1224 struct fd_handle
*h
=
1225 lttng_payload_view_pop_fd_handle(&view
);
1231 fd_to_send
= fd_handle_get_fd(h
);
1235 lsm
.fd_count
= fd_count
;
1237 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1238 fd_count
? &fd_to_send
: NULL
, fd_count
,
1239 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1240 view
.buffer
.size
, NULL
, NULL
, 0);
1244 if (filter_expression
&& ctx
) {
1245 filter_bytecode_free(ctx
);
1246 filter_ir_free(ctx
);
1247 filter_parser_ctx_free(ctx
);
1249 if (free_filter_expression
) {
1251 * The filter expression has been replaced and must be freed as
1252 * it is not the original filter expression received as a
1255 free(filter_expression
);
1258 * Return directly to the caller and don't ask the sessiond since
1259 * something went wrong in the parsing of data above.
1261 lttng_payload_reset(&payload
);
1265 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1266 struct lttng_event
*ev
, const char *channel_name
,
1267 const char *original_filter_expression
)
1269 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_DISABLE_EVENT
};
1270 struct lttng_payload payload
;
1272 unsigned int free_filter_expression
= 0;
1273 struct filter_parser_ctx
*ctx
= NULL
;
1274 size_t bytecode_len
= 0;
1277 * We have either a filter or some exclusions, so we need to set up
1278 * a variable-length payload from where to send the data.
1280 lttng_payload_init(&payload
);
1283 * Cast as non-const since we may replace the filter expression
1284 * by a dynamically allocated string. Otherwise, the original
1285 * string is not modified.
1287 char *filter_expression
= (char *) original_filter_expression
;
1289 if (handle
== NULL
|| ev
== NULL
) {
1290 ret
= -LTTNG_ERR_INVALID
;
1295 * Empty filter string will always be rejected by the parser
1296 * anyway, so treat this corner-case early to eliminate
1297 * lttng_fmemopen error for 0-byte allocation.
1299 if (filter_expression
&& filter_expression
[0] == '\0') {
1300 ret
= -LTTNG_ERR_INVALID
;
1304 /* Parse filter expression. */
1305 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1306 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1307 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1308 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1309 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1310 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1313 /* Setup agent filter if needed. */
1314 agent_filter
= set_agent_filter(filter_expression
, ev
);
1315 if (!agent_filter
) {
1316 if (!filter_expression
) {
1318 * No JUL and no filter, just skip
1325 * With an agent filter, the original filter has
1326 * been added to it thus replace the filter
1329 filter_expression
= agent_filter
;
1330 free_filter_expression
= 1;
1334 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1335 LTTNG_FILTER_MAX_LEN
) {
1336 ret
= -LTTNG_ERR_FILTER_INVAL
;
1340 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1345 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1346 sizeof(ctx
->bytecode
->b
);
1347 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1348 ret
= -LTTNG_ERR_FILTER_INVAL
;
1354 ret
= lttng_event_serialize(ev
, 0, NULL
,
1355 filter_expression
, bytecode_len
,
1356 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1359 ret
= -LTTNG_ERR_INVALID
;
1363 /* If no channel name, send empty string. */
1364 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1365 sizeof(lsm
.u
.disable
.channel_name
));
1367 ret
= -LTTNG_ERR_INVALID
;
1372 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1375 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1376 sizeof(lsm
.session
.name
));
1378 ret
= -LTTNG_ERR_INVALID
;
1382 /* Length of the serialized event. */
1383 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1386 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1388 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1395 assert(fd_count
== 0 || fd_count
== 1);
1396 if (fd_count
== 1) {
1397 struct fd_handle
*h
=
1398 lttng_payload_view_pop_fd_handle(&view
);
1404 fd_to_send
= fd_handle_get_fd(h
);
1408 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1409 fd_count
? &fd_to_send
: NULL
, fd_count
,
1410 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1411 view
.buffer
.size
, NULL
, NULL
, 0);
1415 if (filter_expression
&& ctx
) {
1416 filter_bytecode_free(ctx
);
1417 filter_ir_free(ctx
);
1418 filter_parser_ctx_free(ctx
);
1420 if (free_filter_expression
) {
1422 * The filter expression has been replaced and must be freed as
1423 * it is not the original filter expression received as a
1426 free(filter_expression
);
1429 * Return directly to the caller and don't ask the sessiond since
1430 * something went wrong in the parsing of data above.
1432 lttng_payload_reset(&payload
);
1437 * Disable event(s) of a channel and domain.
1438 * If no event name is specified, all events are disabled.
1439 * If no channel name is specified, the default 'channel0' is used.
1440 * Returns size of returned session payload data or a negative error code.
1442 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1443 const char *channel_name
)
1446 struct lttng_event ev
;
1448 memset(&ev
, 0, sizeof(ev
));
1450 ev
.type
= LTTNG_EVENT_ALL
;
1451 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1453 ret
= -LTTNG_ERR_INVALID
;
1457 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1462 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1464 struct lttng_channel
*channel
= NULL
;
1470 /* Validate domain. */
1471 switch (domain
->type
) {
1472 case LTTNG_DOMAIN_UST
:
1473 switch (domain
->buf_type
) {
1474 case LTTNG_BUFFER_PER_UID
:
1475 case LTTNG_BUFFER_PER_PID
:
1481 case LTTNG_DOMAIN_KERNEL
:
1482 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1490 channel
= lttng_channel_create_internal();
1495 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1500 void lttng_channel_destroy(struct lttng_channel
*channel
)
1506 if (channel
->attr
.extended
.ptr
) {
1507 free(channel
->attr
.extended
.ptr
);
1513 * Enable channel per domain
1514 * Returns size of returned session payload data or a negative error code.
1516 int lttng_enable_channel(struct lttng_handle
*handle
,
1517 struct lttng_channel
*in_chan
)
1519 enum lttng_error_code ret_code
;
1521 struct lttng_dynamic_buffer buffer
;
1522 struct lttcomm_session_msg lsm
;
1523 uint64_t total_buffer_size_needed_per_cpu
= 0;
1524 struct lttng_channel
*channel
= NULL
;
1526 lttng_dynamic_buffer_init(&buffer
);
1528 /* NULL arguments are forbidden. No default values. */
1529 if (handle
== NULL
|| in_chan
== NULL
) {
1530 ret
= -LTTNG_ERR_INVALID
;
1535 * Verify that the amount of memory required to create the requested
1536 * buffer is available on the system at the moment.
1538 if (in_chan
->attr
.num_subbuf
>
1539 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1541 ret
= -LTTNG_ERR_OVERFLOW
;
1545 total_buffer_size_needed_per_cpu
=
1546 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1547 ret_code
= check_enough_available_memory(
1548 total_buffer_size_needed_per_cpu
);
1549 if (ret_code
!= LTTNG_OK
) {
1554 /* Copy the channel for easier manipulation. */
1555 channel
= lttng_channel_copy(in_chan
);
1557 ret
= -LTTNG_ERR_NOMEM
;
1561 /* Populate the channel extended attribute if necessary. */
1562 if (!channel
->attr
.extended
.ptr
) {
1563 struct lttng_channel_extended
*extended
=
1564 (struct lttng_channel_extended
*) zmalloc(
1568 ret
= -LTTNG_ERR_NOMEM
;
1571 lttng_channel_set_default_extended_attr(
1572 &handle
->domain
, extended
);
1573 channel
->attr
.extended
.ptr
= extended
;
1576 /* Prepare the payload */
1577 memset(&lsm
, 0, sizeof(lsm
));
1579 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1580 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1582 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1583 sizeof(lsm
.session
.name
));
1585 ret
= -LTTNG_ERR_INVALID
;
1589 ret
= lttng_channel_serialize(channel
, &buffer
);
1591 ret
= -LTTNG_ERR_FATAL
;
1595 lsm
.u
.channel
.length
= buffer
.size
;
1597 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1598 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1600 lttng_channel_destroy(channel
);
1601 lttng_dynamic_buffer_reset(&buffer
);
1606 * All tracing will be stopped for registered events of the channel.
1607 * Returns size of returned session payload data or a negative error code.
1609 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1612 struct lttcomm_session_msg lsm
;
1614 /* Safety check. Both are mandatory. */
1615 if (handle
== NULL
|| name
== NULL
) {
1616 return -LTTNG_ERR_INVALID
;
1619 memset(&lsm
, 0, sizeof(lsm
));
1621 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1623 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1624 sizeof(lsm
.u
.disable
.channel_name
));
1626 ret
= -LTTNG_ERR_INVALID
;
1630 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1632 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1633 sizeof(lsm
.session
.name
));
1635 ret
= -LTTNG_ERR_INVALID
;
1639 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1645 * Lists all available tracepoints of domain.
1646 * Sets the contents of the events array.
1647 * Returns the number of lttng_event entries in events;
1648 * on error, returns a negative value.
1650 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1651 struct lttng_event
**events
)
1653 enum lttng_error_code ret_code
;
1654 int ret
, total_payload_received
;
1655 char *reception_buffer
= NULL
;
1656 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRACEPOINTS
};
1657 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1658 size_t cmd_header_len
;
1659 unsigned int nb_events
= 0;
1661 if (handle
== NULL
) {
1662 ret
= -LTTNG_ERR_INVALID
;
1666 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1668 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1669 (void **) &reception_buffer
, (void **) &cmd_header
,
1675 total_payload_received
= ret
;
1678 ret
= -LTTNG_ERR_UNK
;
1682 if (cmd_header
->count
> INT_MAX
) {
1683 ret
= -LTTNG_ERR_OVERFLOW
;
1687 nb_events
= (unsigned int) cmd_header
->count
;
1690 struct lttng_buffer_view events_view
=
1691 lttng_buffer_view_init(reception_buffer
, 0,
1692 total_payload_received
);
1693 struct lttng_payload_view events_payload_view
=
1694 lttng_payload_view_from_buffer_view(
1695 &events_view
, 0, -1);
1697 ret_code
= lttng_events_create_and_flatten_from_payload(
1698 &events_payload_view
, nb_events
, events
);
1699 if (ret_code
!= LTTNG_OK
) {
1705 ret
= (int) nb_events
;
1709 free(reception_buffer
);
1714 * Lists all available tracepoint fields of domain.
1715 * Sets the contents of the event field array.
1716 * Returns the number of lttng_event_field entries in events;
1717 * on error, returns a negative value.
1719 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1720 struct lttng_event_field
**fields
)
1722 enum lttng_error_code ret_code
;
1724 struct lttcomm_session_msg lsm
;
1725 const struct lttcomm_list_command_header
*cmd_header
= NULL
;
1726 unsigned int nb_event_fields
= 0;
1727 struct lttng_payload reply
;
1729 if (handle
== NULL
) {
1730 ret
= -LTTNG_ERR_INVALID
;
1734 lttng_payload_init(&reply
);
1736 memset(&lsm
, 0, sizeof(lsm
));
1737 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1738 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1741 struct lttng_payload_view message_view
=
1742 lttng_payload_view_init_from_buffer(
1743 (const char *) &lsm
, 0,
1746 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1753 const struct lttng_buffer_view cmd_header_view
=
1754 lttng_buffer_view_from_dynamic_buffer(
1755 &reply
.buffer
, 0, sizeof(*cmd_header
));
1757 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1758 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1762 cmd_header
= (struct lttcomm_list_command_header
*)
1763 cmd_header_view
.data
;
1766 if (cmd_header
->count
> INT_MAX
) {
1767 ret
= -LTTNG_ERR_OVERFLOW
;
1771 nb_event_fields
= cmd_header
->count
;
1774 struct lttng_payload_view reply_view
=
1775 lttng_payload_view_from_payload(&reply
,
1776 sizeof(*cmd_header
), -1);
1778 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1779 &reply_view
, nb_event_fields
, fields
);
1780 if (ret_code
!= LTTNG_OK
) {
1786 ret
= nb_event_fields
;
1793 * Lists all available kernel system calls. Allocates and sets the contents of
1796 * Returns the number of lttng_event entries in events; on error, returns a
1799 int lttng_list_syscalls(struct lttng_event
**events
)
1801 enum lttng_error_code ret_code
;
1802 int ret
, total_payload_received
;
1803 char *reception_buffer
= NULL
;
1804 struct lttcomm_session_msg lsm
= {};
1805 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1806 size_t cmd_header_len
;
1807 uint32_t nb_events
= 0;
1810 ret
= -LTTNG_ERR_INVALID
;
1814 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1815 /* Force kernel domain for system calls. */
1816 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1818 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1819 (void **) &reception_buffer
, (void **) &cmd_header
,
1824 total_payload_received
= ret
;
1827 ret
= -LTTNG_ERR_UNK
;
1831 if (cmd_header
->count
> INT_MAX
) {
1832 ret
= -LTTNG_ERR_OVERFLOW
;
1836 nb_events
= (unsigned int) cmd_header
->count
;
1839 const struct lttng_buffer_view events_view
=
1840 lttng_buffer_view_init(reception_buffer
, 0,
1841 total_payload_received
);
1842 struct lttng_payload_view events_payload_view
=
1843 lttng_payload_view_from_buffer_view(
1844 &events_view
, 0, -1);
1846 ret_code
= lttng_events_create_and_flatten_from_payload(
1847 &events_payload_view
, nb_events
, events
);
1848 if (ret_code
!= LTTNG_OK
) {
1854 ret
= (int) nb_events
;
1857 free(reception_buffer
);
1863 * Returns a human readable string describing
1864 * the error code (a negative value).
1866 const char *lttng_strerror(int code
)
1868 return error_get_str(code
);
1871 enum lttng_error_code
lttng_create_session_ext(
1872 struct lttng_session_descriptor
*session_descriptor
)
1874 enum lttng_error_code ret_code
;
1875 struct lttcomm_session_msg lsm
= {
1876 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1879 struct lttng_buffer_view reply_view
;
1881 bool sessiond_must_generate_ouput
;
1882 struct lttng_dynamic_buffer payload
;
1884 size_t descriptor_size
;
1885 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1887 lttng_dynamic_buffer_init(&payload
);
1888 if (!session_descriptor
) {
1889 ret_code
= LTTNG_ERR_INVALID
;
1893 sessiond_must_generate_ouput
=
1894 !lttng_session_descriptor_is_output_destination_initialized(
1895 session_descriptor
);
1896 if (sessiond_must_generate_ouput
) {
1897 const char *home_dir
= utils_get_home_dir();
1898 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1900 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1901 ret_code
= LTTNG_ERR_FATAL
;
1905 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1906 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1909 ret_code
= LTTNG_ERR_NOMEM
;
1914 descriptor_size
= payload
.size
;
1915 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1918 ret_code
= LTTNG_ERR_INVALID
;
1921 descriptor_size
= payload
.size
- descriptor_size
;
1922 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1924 /* Command returns a session descriptor on success. */
1925 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1926 payload
.size
, &reply
);
1927 if (reply_ret
< 0) {
1928 ret_code
= -reply_ret
;
1930 } else if (reply_ret
== 0) {
1931 /* Socket unexpectedly closed by the session daemon. */
1932 ret_code
= LTTNG_ERR_FATAL
;
1936 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1937 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1940 ret_code
= LTTNG_ERR_FATAL
;
1943 ret_code
= LTTNG_OK
;
1944 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1947 lttng_dynamic_buffer_reset(&payload
);
1948 lttng_session_descriptor_destroy(descriptor_reply
);
1953 * Create a new session using name and url for destination.
1955 * Return 0 on success else a negative LTTng error code.
1957 int lttng_create_session(const char *name
, const char *url
)
1961 struct lttng_uri
*uris
= NULL
;
1962 struct lttng_session_descriptor
*descriptor
= NULL
;
1963 enum lttng_error_code ret_code
;
1966 ret
= -LTTNG_ERR_INVALID
;
1970 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1972 ret
= -LTTNG_ERR_INVALID
;
1977 descriptor
= lttng_session_descriptor_create(name
);
1980 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1981 ret
= -LTTNG_ERR_INVALID
;
1984 descriptor
= lttng_session_descriptor_local_create(name
,
1988 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1992 ret
= -LTTNG_ERR_INVALID
;
1996 ret
= -LTTNG_ERR_INVALID
;
1999 ret_code
= lttng_create_session_ext(descriptor
);
2000 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2002 lttng_session_descriptor_destroy(descriptor
);
2008 * Create a session exclusively used for snapshot.
2010 * Return 0 on success else a negative LTTng error code.
2012 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2015 enum lttng_error_code ret_code
;
2017 struct lttng_uri
*uris
= NULL
;
2018 struct lttng_session_descriptor
*descriptor
= NULL
;
2021 ret
= -LTTNG_ERR_INVALID
;
2025 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2027 ret
= -LTTNG_ERR_INVALID
;
2031 * If the user does not specify a custom subdir, use the session name.
2033 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
2034 strlen(uris
[0].subdir
) == 0) {
2035 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2038 PERROR("Failed to set session name as network destination sub-directory");
2039 ret
= -LTTNG_ERR_FATAL
;
2041 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2042 /* Truncated output. */
2043 ret
= -LTTNG_ERR_INVALID
;
2050 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2053 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2054 ret
= -LTTNG_ERR_INVALID
;
2057 descriptor
= lttng_session_descriptor_snapshot_local_create(
2062 descriptor
= lttng_session_descriptor_snapshot_network_create(
2068 ret
= -LTTNG_ERR_INVALID
;
2072 ret
= -LTTNG_ERR_INVALID
;
2075 ret_code
= lttng_create_session_ext(descriptor
);
2076 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2078 lttng_session_descriptor_destroy(descriptor
);
2084 * Create a session exclusively used for live.
2086 * Return 0 on success else a negative LTTng error code.
2088 int lttng_create_session_live(const char *name
, const char *url
,
2089 unsigned int timer_interval
)
2092 enum lttng_error_code ret_code
;
2093 struct lttng_session_descriptor
*descriptor
= NULL
;
2096 ret
= -LTTNG_ERR_INVALID
;
2101 descriptor
= lttng_session_descriptor_live_network_create(
2102 name
, url
, NULL
, timer_interval
);
2104 descriptor
= lttng_session_descriptor_live_create(
2105 name
, timer_interval
);
2108 ret
= -LTTNG_ERR_INVALID
;
2111 ret_code
= lttng_create_session_ext(descriptor
);
2112 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2114 lttng_session_descriptor_destroy(descriptor
);
2119 * Stop the session and wait for the data before destroying it
2121 * Return 0 on success else a negative LTTng error code.
2123 int lttng_destroy_session(const char *session_name
)
2126 enum lttng_error_code ret_code
;
2127 enum lttng_destruction_handle_status status
;
2128 struct lttng_destruction_handle
*handle
= NULL
;
2131 * Stop the tracing and wait for the data to be
2134 ret
= _lttng_stop_tracing(session_name
, 1);
2135 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2139 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2140 if (ret_code
!= LTTNG_OK
) {
2141 ret
= (int) -ret_code
;
2146 /* Block until the completion of the destruction of the session. */
2147 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2148 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2149 ret
= -LTTNG_ERR_UNK
;
2153 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2154 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2155 ret
= -LTTNG_ERR_UNK
;
2158 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2160 lttng_destruction_handle_destroy(handle
);
2165 * Destroy the session without waiting for the data.
2167 int lttng_destroy_session_no_wait(const char *session_name
)
2169 enum lttng_error_code ret_code
;
2171 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2172 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2176 * Ask the session daemon for all available sessions.
2177 * Sets the contents of the sessions array.
2178 * Returns the number of lttng_session entries in sessions;
2179 * on error, returns a negative value.
2181 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2184 struct lttcomm_session_msg lsm
;
2185 const size_t session_size
= sizeof(struct lttng_session
) +
2186 sizeof(struct lttng_session_extended
);
2187 size_t session_count
, i
;
2188 struct lttng_session_extended
*sessions_extended_begin
;
2189 struct lttng_session
*sessions
= NULL
;
2191 memset(&lsm
, 0, sizeof(lsm
));
2192 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2194 * Initialize out_sessions to NULL so it is initialized when
2195 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2196 * be subsequently freed.
2198 *out_sessions
= NULL
;
2199 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2204 ret
= -LTTNG_ERR_FATAL
;
2208 if (ret
% session_size
) {
2209 ret
= -LTTNG_ERR_UNK
;
2213 session_count
= (size_t) ret
/ session_size
;
2214 sessions_extended_begin
= (struct lttng_session_extended
*)
2215 (&sessions
[session_count
]);
2217 /* Set extended session info pointers. */
2218 for (i
= 0; i
< session_count
; i
++) {
2219 struct lttng_session
*session
= &sessions
[i
];
2220 struct lttng_session_extended
*extended
=
2221 &(sessions_extended_begin
[i
]);
2223 session
->extended
.ptr
= extended
;
2226 ret
= (int) session_count
;
2227 *out_sessions
= sessions
;
2232 enum lttng_error_code
lttng_session_get_creation_time(
2233 const struct lttng_session
*session
, uint64_t *creation_time
)
2235 enum lttng_error_code ret
= LTTNG_OK
;
2236 struct lttng_session_extended
*extended
;
2238 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2239 ret
= LTTNG_ERR_INVALID
;
2243 extended
= session
->extended
.ptr
;
2244 if (!extended
->creation_time
.is_set
) {
2245 /* Not created on the session daemon yet. */
2246 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2249 *creation_time
= extended
->creation_time
.value
;
2254 int lttng_set_session_shm_path(const char *session_name
,
2255 const char *shm_path
)
2258 struct lttcomm_session_msg lsm
;
2260 if (session_name
== NULL
) {
2261 return -LTTNG_ERR_INVALID
;
2264 memset(&lsm
, 0, sizeof(lsm
));
2265 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2267 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2268 sizeof(lsm
.session
.name
));
2270 ret
= -LTTNG_ERR_INVALID
;
2274 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2275 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2277 ret
= -LTTNG_ERR_INVALID
;
2281 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2287 * Ask the session daemon for all available domains of a session.
2288 * Sets the contents of the domains array.
2289 * Returns the number of lttng_domain entries in domains;
2290 * on error, returns a negative value.
2292 int lttng_list_domains(const char *session_name
,
2293 struct lttng_domain
**domains
)
2296 struct lttcomm_session_msg lsm
;
2298 if (session_name
== NULL
) {
2299 ret
= -LTTNG_ERR_INVALID
;
2303 memset(&lsm
, 0, sizeof(lsm
));
2304 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2306 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2307 sizeof(lsm
.session
.name
));
2309 ret
= -LTTNG_ERR_INVALID
;
2313 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2318 return ret
/ sizeof(struct lttng_domain
);
2324 * Ask the session daemon for all available channels of a session.
2325 * Sets the contents of the channels array.
2326 * Returns the number of lttng_channel entries in channels;
2327 * on error, returns a negative value.
2329 int lttng_list_channels(struct lttng_handle
*handle
,
2330 struct lttng_channel
**channels
)
2332 int ret
, total_payload_received
;
2333 struct lttcomm_session_msg lsm
;
2334 char *reception_buffer
= NULL
;
2335 size_t cmd_header_len
= 0;
2336 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2337 struct lttng_dynamic_buffer tmp_buffer
;
2339 lttng_dynamic_buffer_init(&tmp_buffer
);
2341 if (handle
== NULL
) {
2342 ret
= -LTTNG_ERR_INVALID
;
2346 memset(&lsm
, 0, sizeof(lsm
));
2347 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2348 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2349 sizeof(lsm
.session
.name
));
2351 ret
= -LTTNG_ERR_INVALID
;
2355 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2357 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2358 (void **) &reception_buffer
, (void **) &cmd_header
,
2364 total_payload_received
= ret
;
2366 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2367 ret
= -LTTNG_ERR_FATAL
;
2372 ret
= LTTNG_ERR_UNK
;
2376 if (cmd_header
->count
> INT_MAX
) {
2377 ret
= -LTTNG_ERR_OVERFLOW
;
2382 enum lttng_error_code ret_code
;
2383 const struct lttng_buffer_view events_view
=
2384 lttng_buffer_view_init(reception_buffer
, 0,
2385 total_payload_received
);
2387 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2388 &events_view
, cmd_header
->count
, channels
);
2389 if (ret_code
!= LTTNG_OK
) {
2395 ret
= (int) cmd_header
->count
;
2398 free(reception_buffer
);
2403 * Ask the session daemon for all available events of a session channel.
2404 * Sets the contents of the events array.
2405 * Returns the number of lttng_event entries in events;
2406 * on error, returns a negative value.
2408 int lttng_list_events(struct lttng_handle
*handle
,
2409 const char *channel_name
, struct lttng_event
**events
)
2412 struct lttcomm_session_msg lsm
= {};
2413 struct lttng_payload reply
;
2414 struct lttng_payload_view lsm_view
=
2415 lttng_payload_view_init_from_buffer(
2416 (const char *) &lsm
, 0, sizeof(lsm
));
2417 unsigned int nb_events
= 0;
2419 lttng_payload_init(&reply
);
2421 /* Safety check. An handle and channel name are mandatory. */
2422 if (handle
== NULL
|| channel_name
== NULL
) {
2423 ret
= -LTTNG_ERR_INVALID
;
2427 /* Initialize command parameters. */
2428 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2429 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2430 sizeof(lsm
.session
.name
));
2432 ret
= -LTTNG_ERR_INVALID
;
2436 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2437 sizeof(lsm
.u
.list
.channel_name
));
2439 ret
= -LTTNG_ERR_INVALID
;
2443 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2445 /* Execute command against the session daemon. */
2446 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2452 const struct lttcomm_list_command_header
*cmd_reply_header
=
2454 const struct lttng_payload_view cmd_reply_header_view
=
2455 lttng_payload_view_from_payload(&reply
, 0,
2456 sizeof(*cmd_reply_header
));
2458 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2459 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2463 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2464 cmd_reply_header_view
.buffer
2466 if (cmd_reply_header
->count
> INT_MAX
) {
2467 ret
= -LTTNG_ERR_OVERFLOW
;
2471 nb_events
= (unsigned int) cmd_reply_header
->count
;
2475 enum lttng_error_code ret_code
;
2476 struct lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2478 sizeof(struct lttcomm_list_command_header
), -1);
2480 ret_code
= lttng_events_create_and_flatten_from_payload(
2481 &cmd_reply_payload
, nb_events
, events
);
2482 if (ret_code
!= LTTNG_OK
) {
2483 ret
= -((int) ret_code
);
2488 ret
= (int) nb_events
;
2490 lttng_payload_reset(&reply
);
2495 * Sets the tracing_group variable with name.
2496 * This function allocates memory pointed to by tracing_group.
2497 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2499 int lttng_set_tracing_group(const char *name
)
2503 return -LTTNG_ERR_INVALID
;
2506 if (asprintf(&new_group
, "%s", name
) < 0) {
2507 return -LTTNG_ERR_FATAL
;
2510 free(tracing_group
);
2511 tracing_group
= new_group
;
2517 int lttng_calibrate(struct lttng_handle
*handle
,
2518 struct lttng_calibrate
*calibrate
)
2521 * This command was removed in LTTng 2.9.
2523 return -LTTNG_ERR_UND
;
2527 * Set default channel attributes.
2528 * If either or both of the arguments are null, attr content is zeroe'd.
2530 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2531 struct lttng_channel_attr
*attr
)
2533 struct lttng_channel_extended
*extended
;
2536 if (attr
== NULL
|| domain
== NULL
) {
2540 /* Save the pointer for later use */
2541 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2542 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2544 /* Same for all domains. */
2545 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2546 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2547 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2549 switch (domain
->type
) {
2550 case LTTNG_DOMAIN_KERNEL
:
2551 attr
->switch_timer_interval
=
2552 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2553 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2554 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2555 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2556 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2558 case LTTNG_DOMAIN_UST
:
2559 switch (domain
->buf_type
) {
2560 case LTTNG_BUFFER_PER_UID
:
2561 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2562 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2563 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2564 attr
->switch_timer_interval
=
2565 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2566 attr
->read_timer_interval
=
2567 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2569 case LTTNG_BUFFER_PER_PID
:
2571 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2572 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2573 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2574 attr
->switch_timer_interval
=
2575 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2576 attr
->read_timer_interval
=
2577 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2581 /* Default behavior: leave set to 0. */
2586 lttng_channel_set_default_extended_attr(domain
, extended
);
2589 /* Reassign the extended pointer. */
2590 attr
->extended
.ptr
= extended
;
2593 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2594 uint64_t *discarded_events
)
2597 struct lttng_channel_extended
*chan_ext
;
2599 if (!channel
|| !discarded_events
) {
2600 ret
= -LTTNG_ERR_INVALID
;
2604 chan_ext
= channel
->attr
.extended
.ptr
;
2607 * This can happen since the lttng_channel structure is
2608 * used for other tasks where this pointer is never set.
2610 *discarded_events
= 0;
2614 *discarded_events
= chan_ext
->discarded_events
;
2619 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2620 uint64_t *lost_packets
)
2623 struct lttng_channel_extended
*chan_ext
;
2625 if (!channel
|| !lost_packets
) {
2626 ret
= -LTTNG_ERR_INVALID
;
2630 chan_ext
= channel
->attr
.extended
.ptr
;
2633 * This can happen since the lttng_channel structure is
2634 * used for other tasks where this pointer is never set.
2640 *lost_packets
= chan_ext
->lost_packets
;
2645 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2646 uint64_t *monitor_timer_interval
)
2650 if (!chan
|| !monitor_timer_interval
) {
2651 ret
= -LTTNG_ERR_INVALID
;
2655 if (!chan
->attr
.extended
.ptr
) {
2656 ret
= -LTTNG_ERR_INVALID
;
2660 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2661 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2666 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2667 uint64_t monitor_timer_interval
)
2671 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2672 ret
= -LTTNG_ERR_INVALID
;
2676 ((struct lttng_channel_extended
*)
2677 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2678 monitor_timer_interval
;
2683 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2684 int64_t *blocking_timeout
)
2688 if (!chan
|| !blocking_timeout
) {
2689 ret
= -LTTNG_ERR_INVALID
;
2693 if (!chan
->attr
.extended
.ptr
) {
2694 ret
= -LTTNG_ERR_INVALID
;
2698 *blocking_timeout
= ((struct lttng_channel_extended
*)
2699 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2704 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2705 int64_t blocking_timeout
)
2708 int64_t msec_timeout
;
2710 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2711 ret
= -LTTNG_ERR_INVALID
;
2715 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2716 ret
= -LTTNG_ERR_INVALID
;
2721 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2722 * us to accept a narrower range of values (msecs expressed as a signed
2725 msec_timeout
= blocking_timeout
/ 1000;
2726 if (msec_timeout
!= (int32_t) msec_timeout
) {
2727 ret
= -LTTNG_ERR_INVALID
;
2731 ((struct lttng_channel_extended
*)
2732 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2739 * Check if session daemon is alive.
2741 * Return 1 if alive or 0 if not.
2742 * On error returns a negative value.
2744 int lttng_session_daemon_alive(void)
2748 ret
= set_session_daemon_path();
2754 if (*sessiond_sock_path
== '\0') {
2756 * No socket path set. Weird error which means the constructor
2762 ret
= try_connect_sessiond(sessiond_sock_path
);
2773 * Set URL for a consumer for a session and domain.
2775 * Return 0 on success, else a negative value.
2777 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2778 const char *control_url
, const char *data_url
)
2782 struct lttcomm_session_msg lsm
;
2783 struct lttng_uri
*uris
= NULL
;
2785 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2786 ret
= -LTTNG_ERR_INVALID
;
2790 memset(&lsm
, 0, sizeof(lsm
));
2792 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2794 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2795 sizeof(lsm
.session
.name
));
2797 ret
= -LTTNG_ERR_INVALID
;
2801 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2803 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2805 ret
= -LTTNG_ERR_INVALID
;
2809 lsm
.u
.uri
.size
= size
;
2811 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2812 sizeof(struct lttng_uri
) * size
, NULL
);
2822 int lttng_enable_consumer(struct lttng_handle
*handle
);
2823 int lttng_enable_consumer(struct lttng_handle
*handle
)
2831 int lttng_disable_consumer(struct lttng_handle
*handle
);
2832 int lttng_disable_consumer(struct lttng_handle
*handle
)
2840 int _lttng_create_session_ext(const char *name
, const char *url
,
2841 const char *datetime
);
2842 int _lttng_create_session_ext(const char *name
, const char *url
,
2843 const char *datetime
)
2849 * For a given session name, this call checks if the data is ready to be read
2850 * or is still being extracted by the consumer(s) hence not ready to be used by
2853 int lttng_data_pending(const char *session_name
)
2856 struct lttcomm_session_msg lsm
;
2857 uint8_t *pending
= NULL
;
2859 if (session_name
== NULL
) {
2860 return -LTTNG_ERR_INVALID
;
2863 memset(&lsm
, 0, sizeof(lsm
));
2864 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2866 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2867 sizeof(lsm
.session
.name
));
2869 ret
= -LTTNG_ERR_INVALID
;
2873 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2876 } else if (ret
!= 1) {
2877 /* Unexpected payload size */
2878 ret
= -LTTNG_ERR_INVALID
;
2880 } else if (!pending
) {
2881 /* Internal error. */
2882 ret
= -LTTNG_ERR_UNK
;
2886 ret
= (int) *pending
;
2893 * Regenerate the metadata for a session.
2894 * Return 0 on success, a negative error code on error.
2896 int lttng_regenerate_metadata(const char *session_name
)
2899 struct lttcomm_session_msg lsm
;
2901 if (!session_name
) {
2902 ret
= -LTTNG_ERR_INVALID
;
2906 memset(&lsm
, 0, sizeof(lsm
));
2907 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2909 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2910 sizeof(lsm
.session
.name
));
2912 ret
= -LTTNG_ERR_INVALID
;
2916 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2927 * Deprecated, replaced by lttng_regenerate_metadata.
2929 int lttng_metadata_regenerate(const char *session_name
)
2931 return lttng_regenerate_metadata(session_name
);
2935 * Regenerate the statedump of a session.
2936 * Return 0 on success, a negative error code on error.
2938 int lttng_regenerate_statedump(const char *session_name
)
2941 struct lttcomm_session_msg lsm
;
2943 if (!session_name
) {
2944 ret
= -LTTNG_ERR_INVALID
;
2948 memset(&lsm
, 0, sizeof(lsm
));
2949 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2951 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2952 sizeof(lsm
.session
.name
));
2954 ret
= -LTTNG_ERR_INVALID
;
2958 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2969 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
2973 struct lttcomm_session_msg lsm
= {
2974 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2975 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
2977 struct lttcomm_session_msg
*message_lsm
;
2978 struct lttng_payload message
;
2979 struct lttng_payload reply
;
2980 struct lttng_trigger
*reply_trigger
= NULL
;
2981 enum lttng_domain_type domain_type
;
2982 const struct lttng_credentials user_creds
= {
2983 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2984 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2986 const char *unused_trigger_name
= NULL
;
2987 enum lttng_trigger_status trigger_status
;
2989 lttng_payload_init(&message
);
2990 lttng_payload_init(&reply
);
2993 ret
= -LTTNG_ERR_INVALID
;
2997 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2998 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2999 /* Re-using already registered trigger. */
3000 ret
= -LTTNG_ERR_INVALID
;
3005 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3006 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3007 ret
= -LTTNG_ERR_NOMEM
;
3012 if (!trigger
->creds
.uid
.is_set
) {
3013 /* Use the client's credentials as the trigger credentials. */
3014 lttng_trigger_set_credentials(trigger
, &user_creds
);
3017 * Validate that either the current trigger credentials and the
3018 * client credentials are identical or that the current user is
3019 * root. The root user can register, unregister triggers for
3020 * himself and other users.
3022 * This check is also present on the sessiond side, using the
3023 * credentials passed on the socket. These check are all
3026 const struct lttng_credentials
*trigger_creds
=
3027 lttng_trigger_get_credentials(trigger
);
3029 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3030 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3031 ret
= -LTTNG_ERR_EPERM
;
3032 goto end_unset_name
;
3037 if (!lttng_trigger_validate(trigger
)) {
3038 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3039 goto end_unset_name
;
3042 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3045 lsm
.domain
.type
= domain_type
;
3047 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3049 ret
= -LTTNG_ERR_NOMEM
;
3050 goto end_unset_name
;
3053 ret
= lttng_trigger_serialize(trigger
, &message
);
3055 ret
= -LTTNG_ERR_UNK
;
3056 goto end_unset_name
;
3060 * This is needed to populate the trigger object size for the command
3063 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3065 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3068 struct lttng_payload_view message_view
=
3069 lttng_payload_view_from_payload(
3072 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3074 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3076 goto end_unset_name
;
3081 struct lttng_payload_view reply_view
=
3082 lttng_payload_view_from_payload(
3083 &reply
, 0, reply
.buffer
.size
);
3085 ret
= lttng_trigger_create_from_payload(
3086 &reply_view
, &reply_trigger
);
3088 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3089 goto end_unset_name
;
3093 if (name
|| generate_name
) {
3094 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3096 ret
= -LTTNG_ERR_NOMEM
;
3105 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3106 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3107 ret
= -LTTNG_ERR_UNK
;
3110 lttng_payload_reset(&message
);
3111 lttng_payload_reset(&reply
);
3112 lttng_trigger_destroy(reply_trigger
);
3116 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3118 /* Register an anonymous trigger. */
3119 return _lttng_register_trigger(trigger
, NULL
, false);
3122 enum lttng_error_code
lttng_register_trigger_with_name(
3123 struct lttng_trigger
*trigger
, const char *name
)
3125 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3127 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3130 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3131 struct lttng_trigger
*trigger
)
3133 const int ret
= _lttng_register_trigger(trigger
, false, true);
3135 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3138 enum lttng_error_code
lttng_error_query_execute(
3139 const struct lttng_error_query
*query
,
3140 const struct lttng_endpoint
*endpoint
,
3141 struct lttng_error_query_results
**results
)
3144 enum lttng_error_code ret_code
;
3145 struct lttcomm_session_msg lsm
= {
3146 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3148 struct lttng_payload message
;
3149 struct lttng_payload reply
;
3150 struct lttcomm_session_msg
*message_lsm
;
3152 lttng_payload_init(&message
);
3153 lttng_payload_init(&reply
);
3155 if (!query
|| !results
) {
3156 ret_code
= LTTNG_ERR_INVALID
;
3160 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3161 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3165 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3167 ret_code
= LTTNG_ERR_NOMEM
;
3171 ret
= lttng_error_query_serialize(query
, &message
);
3173 ret_code
= LTTNG_ERR_UNK
;
3177 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3178 message_lsm
->u
.error_query
.length
=
3179 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3182 struct lttng_payload_view message_view
=
3183 lttng_payload_view_from_payload(
3186 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3188 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3196 ssize_t reply_create_ret
;
3197 struct lttng_payload_view reply_view
=
3198 lttng_payload_view_from_payload(
3199 &reply
, 0, reply
.buffer
.size
);
3201 reply_create_ret
= lttng_error_query_results_create_from_payload(
3202 &reply_view
, results
);
3203 if (reply_create_ret
< 0) {
3204 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3209 ret_code
= LTTNG_OK
;
3211 lttng_payload_reset(&message
);
3212 lttng_payload_reset(&reply
);
3216 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3219 struct lttcomm_session_msg lsm
;
3220 struct lttcomm_session_msg
*message_lsm
;
3221 struct lttng_payload message
;
3222 struct lttng_payload reply
;
3223 struct lttng_trigger
*copy
= NULL
;
3224 const struct lttng_credentials user_creds
= {
3225 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3226 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3229 lttng_payload_init(&message
);
3230 lttng_payload_init(&reply
);
3233 ret
= -LTTNG_ERR_INVALID
;
3237 copy
= lttng_trigger_copy(trigger
);
3239 ret
= -LTTNG_ERR_UNK
;
3243 if (!copy
->creds
.uid
.is_set
) {
3244 /* Use the client credentials as the trigger credentials */
3245 lttng_trigger_set_credentials(copy
, &user_creds
);
3248 * Validate that either the current trigger credentials and the
3249 * client credentials are identical or that the current user is
3250 * root. The root user can register, unregister triggers for
3251 * himself and other users.
3253 * This check is also present on the sessiond side, using the
3254 * credentials passed on the socket. These check are all
3257 const struct lttng_credentials
*trigger_creds
=
3258 lttng_trigger_get_credentials(copy
);
3259 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3260 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3261 ret
= -LTTNG_ERR_EPERM
;
3267 if (!lttng_trigger_validate(copy
)) {
3268 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3272 memset(&lsm
, 0, sizeof(lsm
));
3273 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3275 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3277 ret
= -LTTNG_ERR_NOMEM
;
3281 ret
= lttng_trigger_serialize(copy
, &message
);
3283 ret
= -LTTNG_ERR_UNK
;
3288 * This is needed to populate the trigger object size for the command
3289 * header and number of fds sent.
3291 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3293 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3296 struct lttng_payload_view message_view
=
3297 lttng_payload_view_from_payload(
3301 * Update the message header with the number of fd that will be
3304 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3307 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3315 lttng_trigger_destroy(copy
);
3316 lttng_payload_reset(&message
);
3317 lttng_payload_reset(&reply
);
3322 * Ask the session daemon for all registered triggers for the current user.
3324 * Allocates and return an lttng_triggers set.
3325 * On error, returns a suitable lttng_error_code.
3327 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3330 enum lttng_error_code ret_code
= LTTNG_OK
;
3331 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3332 struct lttng_triggers
*local_triggers
= NULL
;
3333 struct lttng_payload reply
;
3334 struct lttng_payload_view lsm_view
=
3335 lttng_payload_view_init_from_buffer(
3336 (const char *) &lsm
, 0, sizeof(lsm
));
3338 lttng_payload_init(&reply
);
3340 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3342 ret_code
= (enum lttng_error_code
) -ret
;
3347 struct lttng_payload_view reply_view
=
3348 lttng_payload_view_from_payload(
3349 &reply
, 0, reply
.buffer
.size
);
3351 ret
= lttng_triggers_create_from_payload(
3352 &reply_view
, &local_triggers
);
3354 ret_code
= LTTNG_ERR_FATAL
;
3359 *triggers
= local_triggers
;
3360 local_triggers
= NULL
;
3362 lttng_payload_reset(&reply
);
3363 lttng_triggers_destroy(local_triggers
);
3370 static void __attribute__((constructor
)) init(void)
3372 /* Set default session group */
3373 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3379 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3381 free(tracing_group
);