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
23 #include <common/buffer-view.h>
24 #include <common/common.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/macros.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/tracker.h>
32 #include <common/uri.h>
33 #include <common/utils.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/destruction-handle.h>
36 #include <lttng/endpoint.h>
37 #include <lttng/event-internal.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/lttng.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/trigger/trigger-internal.h>
43 #include <lttng/userspace-probe-internal.h>
45 #include "filter/filter-ast.h"
46 #include "filter/filter-parser.h"
47 #include "filter/filter-bytecode.h"
48 #include "filter/memstream.h"
49 #include "lttng-ctl-helper.h"
52 static const int print_xml
= 1;
53 #define dbg_printf(fmt, args...) \
54 printf("[debug liblttng-ctl] " fmt, ## args)
56 static const int print_xml
= 0;
57 #define dbg_printf(fmt, args...) \
59 /* do nothing but check printf format */ \
61 printf("[debug liblttnctl] " fmt, ## args); \
65 #define COPY_DOMAIN_PACKED(dst, src) \
67 struct lttng_domain _tmp_domain; \
69 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
73 /* Socket to session daemon for communication */
74 static int sessiond_socket
= -1;
75 static char sessiond_sock_path
[PATH_MAX
];
78 static char *tracing_group
;
84 * Those two variables are used by error.h to silent or control the verbosity of
85 * error message. They are global to the library so application linking with it
86 * are able to compile correctly and also control verbosity of the library.
89 int lttng_opt_verbose
;
93 * Copy domain to lttcomm_session_msg domain.
95 * If domain is unknown, default domain will be the kernel.
98 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
99 struct lttng_domain
*src
)
103 case LTTNG_DOMAIN_KERNEL
:
104 case LTTNG_DOMAIN_UST
:
105 case LTTNG_DOMAIN_JUL
:
106 case LTTNG_DOMAIN_LOG4J
:
107 case LTTNG_DOMAIN_PYTHON
:
108 memcpy(dst
, src
, sizeof(struct lttng_domain
));
111 memset(dst
, 0, sizeof(struct lttng_domain
));
118 * Send lttcomm_session_msg to the session daemon.
120 * On success, returns the number of bytes sent (>=0)
121 * On error, returns -1
123 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
128 ret
= -LTTNG_ERR_NO_SESSIOND
;
132 DBG("LSM cmd type : %d", lsm
->cmd_type
);
134 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
135 sizeof(struct lttcomm_session_msg
));
137 ret
= -LTTNG_ERR_FATAL
;
145 * Send var len data to the session daemon.
147 * On success, returns the number of bytes sent (>=0)
148 * On error, returns -1
150 static int send_session_varlen(const void *data
, size_t len
)
155 ret
= -LTTNG_ERR_NO_SESSIOND
;
164 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
166 ret
= -LTTNG_ERR_FATAL
;
174 * Send file descriptors to the session daemon.
176 * On success, returns the number of bytes sent (>=0)
177 * On error, returns -1
179 static int send_session_fds(const int *fds
, size_t nb_fd
)
184 ret
= -LTTNG_ERR_NO_SESSIOND
;
188 if (!fds
|| !nb_fd
) {
193 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
195 ret
= -LTTNG_ERR_FATAL
;
203 * Receive data from the sessiond socket.
205 * On success, returns the number of bytes received (>=0)
206 * On error, returns -1 (recvmsg() error) or -ENOTCONN
208 static int recv_data_sessiond(void *buf
, size_t len
)
213 ret
= -LTTNG_ERR_NO_SESSIOND
;
217 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
219 ret
= -LTTNG_ERR_FATAL
;
227 * Check if we are in the specified group.
229 * If yes return 1, else return -1.
232 int lttng_check_tracing_group(void)
234 gid_t
*grp_list
, tracing_gid
;
235 int grp_list_size
, grp_id
, i
;
237 const char *grp_name
= tracing_group
;
239 /* Get GID of group 'tracing' */
240 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
241 /* If grp_tracing is NULL, the group does not exist. */
245 /* Get number of supplementary group IDs */
246 grp_list_size
= getgroups(0, NULL
);
247 if (grp_list_size
< 0) {
252 /* Alloc group list of the right size */
253 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
258 grp_id
= getgroups(grp_list_size
, grp_list
);
264 for (i
= 0; i
< grp_list_size
; i
++) {
265 if (grp_list
[i
] == tracing_gid
) {
278 static enum lttng_error_code
check_enough_available_memory(
279 uint64_t num_bytes_requested_per_cpu
)
283 uint64_t best_mem_info
;
284 uint64_t num_bytes_requested_total
;
287 * Get the number of CPU currently online to compute the amount of
288 * memory needed to create a buffer for every CPU.
290 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
292 ret
= LTTNG_ERR_FATAL
;
296 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
298 ret
= LTTNG_ERR_OVERFLOW
;
302 num_bytes_requested_total
=
303 num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
306 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
307 * reliable estimate we can get but it is only exposed by the kernel
308 * since 3.14. (See Linux kernel commit:
309 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
311 ret
= utils_get_memory_available(&best_mem_info
);
317 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
318 * is a sanity check for obvious user error.
320 ret
= utils_get_memory_total(&best_mem_info
);
325 /* No valid source of information. */
326 ret
= LTTNG_ERR_NOMEM
;
330 if (best_mem_info
>= num_bytes_requested_total
) {
333 ret
= LTTNG_ERR_NOMEM
;
340 * Try connect to session daemon with sock_path.
342 * Return 0 on success, else -1
344 static int try_connect_sessiond(const char *sock_path
)
348 /* If socket exist, we check if the daemon listens for connect. */
349 ret
= access(sock_path
, F_OK
);
355 ret
= lttcomm_connect_unix_sock(sock_path
);
361 ret
= lttcomm_close_unix_sock(ret
);
363 PERROR("lttcomm_close_unix_sock");
373 * Set sessiond socket path by putting it in the global sessiond_sock_path
376 * Returns 0 on success, negative value on failure (the sessiond socket path
377 * is somehow too long or ENOMEM).
379 static int set_session_daemon_path(void)
381 int in_tgroup
= 0; /* In tracing group. */
387 /* Are we in the tracing group ? */
388 in_tgroup
= lttng_check_tracing_group();
391 if ((uid
== 0) || in_tgroup
== 1) {
392 const int ret
= lttng_strncpy(sessiond_sock_path
,
393 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
394 sizeof(sessiond_sock_path
));
406 ret
= try_connect_sessiond(sessiond_sock_path
);
410 /* Global session daemon not available... */
412 /* ...or not in tracing group (and not root), default */
415 * With GNU C < 2.1, snprintf returns -1 if the target buffer
417 * With GNU C >= 2.1, snprintf returns the required size
418 * (excluding closing null)
420 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
421 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
422 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
434 * Connect to the LTTng session daemon.
436 * On success, return the socket's file descriptor. On error, return -1.
438 LTTNG_HIDDEN
int connect_sessiond(void)
442 ret
= set_session_daemon_path();
447 /* Connect to the sesssion daemon. */
448 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
459 static void reset_global_sessiond_connection_state(void)
461 sessiond_socket
= -1;
466 * Clean disconnect from the session daemon.
468 * On success, return 0. On error, return -1.
470 static int disconnect_sessiond(void)
475 ret
= lttcomm_close_unix_sock(sessiond_socket
);
476 reset_global_sessiond_connection_state();
482 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
490 ret
= -LTTNG_ERR_INVALID
;
500 ret
= recv_data_sessiond(buf
, len
);
506 ret
= -LTTNG_ERR_INVALID
;
510 /* Move ownership of command header buffer to user. */
515 /* No command header. */
531 * Ask the session daemon a specific command and put the data into buf.
532 * Takes extra var. len. data and file descriptors as input to send to the
535 * Return size of data (only payload, not header) or a negative error code.
538 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
539 const int *fds
, size_t nb_fd
, const void *vardata
,
540 size_t vardata_len
, void **user_payload_buf
,
541 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
545 struct lttcomm_lttng_msg llm
;
547 ret
= connect_sessiond();
549 ret
= -LTTNG_ERR_NO_SESSIOND
;
552 sessiond_socket
= ret
;
556 /* Send command to session daemon */
557 ret
= send_session_msg(lsm
);
559 /* Ret value is a valid lttng error code. */
562 /* Send var len data */
563 ret
= send_session_varlen(vardata
, vardata_len
);
565 /* Ret value is a valid lttng error code. */
570 ret
= send_session_fds(fds
, nb_fd
);
572 /* Ret value is a valid lttng error code. */
576 /* Get header from data transmission */
577 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
579 /* Ret value is a valid lttng error code. */
583 /* Check error code if OK */
584 if (llm
.ret_code
!= LTTNG_OK
) {
589 /* Get command header from data transmission */
590 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
591 user_cmd_header_buf
, user_cmd_header_len
);
596 /* Get payload from data transmission */
597 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
606 disconnect_sessiond();
611 * Create lttng handle and return pointer.
613 * The returned pointer will be NULL in case of malloc() error.
615 struct lttng_handle
*lttng_create_handle(const char *session_name
,
616 struct lttng_domain
*domain
)
619 struct lttng_handle
*handle
= NULL
;
621 handle
= zmalloc(sizeof(struct lttng_handle
));
622 if (handle
== NULL
) {
623 PERROR("malloc handle");
627 /* Copy session name */
628 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
629 sizeof(handle
->session_name
));
634 /* Copy lttng domain or leave initialized to 0. */
636 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
647 * Destroy handle by free(3) the pointer.
649 void lttng_destroy_handle(struct lttng_handle
*handle
)
655 * Register an outside consumer.
657 * Returns size of returned session payload data or a negative error code.
659 int lttng_register_consumer(struct lttng_handle
*handle
,
660 const char *socket_path
)
663 struct lttcomm_session_msg lsm
;
665 if (handle
== NULL
|| socket_path
== NULL
) {
666 ret
= -LTTNG_ERR_INVALID
;
670 memset(&lsm
, 0, sizeof(lsm
));
671 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
672 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
673 sizeof(lsm
.session
.name
));
675 ret
= -LTTNG_ERR_INVALID
;
679 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
681 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
682 sizeof(lsm
.u
.reg
.path
));
684 ret
= -LTTNG_ERR_INVALID
;
688 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
694 * Start tracing for all traces of the session.
696 * Returns size of returned session payload data or a negative error code.
698 int lttng_start_tracing(const char *session_name
)
701 struct lttcomm_session_msg lsm
;
703 if (session_name
== NULL
) {
704 ret
= -LTTNG_ERR_INVALID
;
708 memset(&lsm
, 0, sizeof(lsm
));
709 lsm
.cmd_type
= LTTNG_START_TRACE
;
711 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
712 sizeof(lsm
.session
.name
));
714 ret
= -LTTNG_ERR_INVALID
;
718 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
724 * Stop tracing for all traces of the session.
726 static int _lttng_stop_tracing(const char *session_name
, int wait
)
729 struct lttcomm_session_msg lsm
;
731 if (session_name
== NULL
) {
732 ret
= -LTTNG_ERR_INVALID
;
736 memset(&lsm
, 0, sizeof(lsm
));
737 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
739 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
740 sizeof(lsm
.session
.name
));
742 ret
= -LTTNG_ERR_INVALID
;
746 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
747 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
755 /* Check for data availability */
757 data_ret
= lttng_data_pending(session_name
);
759 /* Return the data available call error. */
765 * Data sleep time before retrying (in usec). Don't sleep if the
766 * call returned value indicates availability.
769 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
771 } while (data_ret
!= 0);
779 * Stop tracing and wait for data availability.
781 int lttng_stop_tracing(const char *session_name
)
783 return _lttng_stop_tracing(session_name
, 1);
787 * Stop tracing but _don't_ wait for data availability.
789 int lttng_stop_tracing_no_wait(const char *session_name
)
791 return _lttng_stop_tracing(session_name
, 0);
795 * Add context to a channel.
797 * If the given channel is NULL, add the contexts to all channels.
798 * The event_name param is ignored.
800 * Returns the size of the returned payload data or a negative error code.
802 int lttng_add_context(struct lttng_handle
*handle
,
803 struct lttng_event_context
*ctx
, const char *event_name
,
804 const char *channel_name
)
807 struct lttcomm_session_msg lsm
;
808 struct lttng_dynamic_buffer buffer
;
810 lttng_dynamic_buffer_init(&buffer
);
812 /* Safety check. Both are mandatory. */
813 if (handle
== NULL
|| ctx
== NULL
) {
814 ret
= -LTTNG_ERR_INVALID
;
818 memset(&lsm
, 0, sizeof(lsm
));
819 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
821 /* If no channel name, send empty string. */
822 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
823 sizeof(lsm
.u
.context
.channel_name
));
825 ret
= -LTTNG_ERR_INVALID
;
829 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
830 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
831 sizeof(lsm
.session
.name
));
833 ret
= -LTTNG_ERR_INVALID
;
837 ret
= lttng_event_context_serialize(ctx
, &buffer
);
839 ret
= -LTTNG_ERR_INVALID
;
843 lsm
.u
.context
.length
= buffer
.size
;
845 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
846 &lsm
, buffer
.data
, buffer
.size
, NULL
);
848 lttng_dynamic_buffer_reset(&buffer
);
853 * Enable event(s) for a channel.
855 * If no event name is specified, all events are enabled.
856 * If no channel name is specified, the default 'channel0' is used.
858 * Returns size of returned session payload data or a negative error code.
860 int lttng_enable_event(struct lttng_handle
*handle
,
861 struct lttng_event
*ev
, const char *channel_name
)
863 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
868 * Create or enable an event with a filter expression.
870 * Return negative error value on error.
871 * Return size of returned session payload data if OK.
873 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
874 struct lttng_event
*event
, const char *channel_name
,
875 const char *filter_expression
)
877 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
878 filter_expression
, 0, NULL
);
882 * Depending on the event, return a newly allocated agent filter expression or
883 * NULL if not applicable.
885 * An event with NO loglevel and the name is * will return NULL.
887 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
890 char *agent_filter
= NULL
;
894 /* Don't add filter for the '*' event. */
895 if (strcmp(ev
->name
, "*") != 0) {
897 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
900 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
908 /* Add loglevel filtering if any for the JUL domain. */
909 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
912 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
918 if (filter
|| agent_filter
) {
921 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
922 agent_filter
? agent_filter
: filter
, op
,
927 agent_filter
= new_filter
;
929 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
945 * Generate the filter bytecode from a given filter expression string. Put the
946 * newly allocated parser context in ctxp and populate the lsm object with the
949 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
951 static int generate_filter(char *filter_expression
,
952 size_t *bytecode_len
,
953 struct filter_parser_ctx
**ctxp
)
956 struct filter_parser_ctx
*ctx
= NULL
;
959 assert(filter_expression
);
963 * Casting const to non-const, as the underlying function will use it in
966 fmem
= lttng_fmemopen((void *) filter_expression
,
967 strlen(filter_expression
), "r");
969 fprintf(stderr
, "Error opening memory as stream\n");
970 ret
= -LTTNG_ERR_FILTER_NOMEM
;
973 ctx
= filter_parser_ctx_alloc(fmem
);
975 fprintf(stderr
, "Error allocating parser\n");
976 ret
= -LTTNG_ERR_FILTER_NOMEM
;
977 goto filter_alloc_error
;
979 ret
= filter_parser_ctx_append_ast(ctx
);
981 fprintf(stderr
, "Parse error\n");
982 ret
= -LTTNG_ERR_FILTER_INVAL
;
986 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
989 fprintf(stderr
, "XML print error\n");
990 ret
= -LTTNG_ERR_FILTER_INVAL
;
995 dbg_printf("Generating IR... ");
997 ret
= filter_visitor_ir_generate(ctx
);
999 fprintf(stderr
, "Generate IR error\n");
1000 ret
= -LTTNG_ERR_FILTER_INVAL
;
1003 dbg_printf("done\n");
1005 dbg_printf("Validating IR... ");
1007 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1009 ret
= -LTTNG_ERR_FILTER_INVAL
;
1013 /* Normalize globbing patterns in the expression. */
1014 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1016 ret
= -LTTNG_ERR_FILTER_INVAL
;
1020 /* Validate strings used as literals in the expression. */
1021 ret
= filter_visitor_ir_validate_string(ctx
);
1023 ret
= -LTTNG_ERR_FILTER_INVAL
;
1027 /* Validate globbing patterns in the expression. */
1028 ret
= filter_visitor_ir_validate_globbing(ctx
);
1030 ret
= -LTTNG_ERR_FILTER_INVAL
;
1034 dbg_printf("done\n");
1036 dbg_printf("Generating bytecode... ");
1038 ret
= filter_visitor_bytecode_generate(ctx
);
1040 fprintf(stderr
, "Generate bytecode error\n");
1041 ret
= -LTTNG_ERR_FILTER_INVAL
;
1044 dbg_printf("done\n");
1045 dbg_printf("Size of bytecode generated: %u bytes.\n",
1046 bytecode_get_len(&ctx
->bytecode
->b
));
1048 *bytecode_len
= sizeof(ctx
->bytecode
->b
) +
1049 bytecode_get_len(&ctx
->bytecode
->b
);
1051 /* No need to keep the memory stream. */
1052 if (fclose(fmem
) != 0) {
1060 filter_ir_free(ctx
);
1061 filter_parser_ctx_free(ctx
);
1063 if (fclose(fmem
) != 0) {
1071 * Enable event(s) for a channel, possibly with exclusions and a filter.
1072 * If no event name is specified, all events are enabled.
1073 * If no channel name is specified, the default name is used.
1074 * If filter expression is not NULL, the filter is set for the event.
1075 * If exclusion count is not zero, the exclusions are set for the event.
1076 * Returns size of returned session payload data or a negative error code.
1078 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1079 struct lttng_event
*ev
, const char *channel_name
,
1080 const char *original_filter_expression
,
1081 int exclusion_count
, char **exclusion_list
)
1083 struct lttcomm_session_msg lsm
;
1084 struct lttng_dynamic_buffer send_buffer
;
1085 int ret
= 0, fd_to_send
= -1;
1086 bool send_fd
= false;
1087 unsigned int free_filter_expression
= 0;
1088 struct filter_parser_ctx
*ctx
= NULL
;
1089 size_t bytecode_len
= 0;
1092 * We have either a filter or some exclusions, so we need to set up
1093 * a variable-length memory block from where to send the data.
1095 lttng_dynamic_buffer_init(&send_buffer
);
1098 * Cast as non-const since we may replace the filter expression
1099 * by a dynamically allocated string. Otherwise, the original
1100 * string is not modified.
1102 char *filter_expression
= (char *) original_filter_expression
;
1104 if (handle
== NULL
|| ev
== NULL
) {
1105 ret
= -LTTNG_ERR_INVALID
;
1110 * Empty filter string will always be rejected by the parser
1111 * anyway, so treat this corner-case early to eliminate
1112 * lttng_fmemopen error for 0-byte allocation.
1114 if (filter_expression
&& filter_expression
[0] == '\0') {
1115 ret
= -LTTNG_ERR_INVALID
;
1119 if (ev
->name
[0] == '\0') {
1120 /* Enable all events. */
1121 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1125 /* Parse filter expression. */
1126 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1127 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1128 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1129 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1130 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1131 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1134 /* Setup agent filter if needed. */
1135 agent_filter
= set_agent_filter(filter_expression
, ev
);
1136 if (!agent_filter
) {
1137 if (!filter_expression
) {
1139 * No JUL and no filter, just skip
1146 * With an agent filter, the original filter has
1147 * been added to it thus replace the filter
1150 filter_expression
= agent_filter
;
1151 free_filter_expression
= 1;
1155 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1156 LTTNG_FILTER_MAX_LEN
) {
1157 ret
= -LTTNG_ERR_FILTER_INVAL
;
1161 ret
= generate_filter(filter_expression
, &bytecode_len
, &ctx
);
1166 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1167 ret
= -LTTNG_ERR_FILTER_INVAL
;
1173 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1174 filter_expression
, bytecode_len
,
1175 ctx
&& (bytecode_len
!= 0) ? &ctx
->bytecode
->b
: NULL
,
1176 &send_buffer
, &fd_to_send
);
1181 if (fd_to_send
>= 0) {
1185 /* Prepare the command header */
1186 memset(&lsm
, 0, sizeof(lsm
));
1187 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1189 /* If no channel name, send empty string. */
1190 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1191 sizeof(lsm
.u
.enable
.channel_name
));
1193 ret
= -LTTNG_ERR_INVALID
;
1198 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1201 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1202 sizeof(lsm
.session
.name
));
1204 ret
= -LTTNG_ERR_INVALID
;
1208 /* Length of the serialized event */
1209 lsm
.u
.enable
.length
= (uint32_t) send_buffer
.size
;
1211 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1212 send_fd
? &fd_to_send
: NULL
,
1214 send_buffer
.size
? send_buffer
.data
: NULL
,
1215 send_buffer
.size
, NULL
, NULL
, 0);
1217 if (filter_expression
&& ctx
) {
1218 filter_bytecode_free(ctx
);
1219 filter_ir_free(ctx
);
1220 filter_parser_ctx_free(ctx
);
1223 if (free_filter_expression
) {
1225 * The filter expression has been replaced and must be freed as
1226 * it is not the original filter expression received as a
1229 free(filter_expression
);
1232 lttng_dynamic_buffer_reset(&send_buffer
);
1236 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1237 struct lttng_event
*ev
, const char *channel_name
,
1238 const char *original_filter_expression
)
1240 struct lttcomm_session_msg lsm
;
1241 struct lttng_dynamic_buffer buf
;
1243 unsigned int free_filter_expression
= 0;
1244 struct filter_parser_ctx
*ctx
= NULL
;
1245 size_t bytecode_len
= 0;
1246 int fd_to_send
= -1;
1247 bool send_fd
= false;
1249 * Cast as non-const since we may replace the filter expression
1250 * by a dynamically allocated string. Otherwise, the original
1251 * string is not modified.
1253 char *filter_expression
= (char *) original_filter_expression
;
1255 lttng_dynamic_buffer_init(&buf
);
1257 if (handle
== NULL
|| ev
== NULL
) {
1258 ret
= -LTTNG_ERR_INVALID
;
1263 * Empty filter string will always be rejected by the parser
1264 * anyway, so treat this corner-case early to eliminate
1265 * lttng_fmemopen error for 0-byte allocation.
1267 if (filter_expression
&& filter_expression
[0] == '\0') {
1268 ret
= -LTTNG_ERR_INVALID
;
1272 /* Parse filter expression */
1273 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1274 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1275 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1276 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1277 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1278 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1281 /* Setup agent filter if needed. */
1282 agent_filter
= set_agent_filter(filter_expression
, ev
);
1283 if (!agent_filter
) {
1284 if (!filter_expression
) {
1286 * No JUL and no filter, just skip
1293 * With a JUL filter, the original filter has
1294 * been added to it thus replace the filter
1297 filter_expression
= agent_filter
;
1298 free_filter_expression
= 1;
1302 ret
= generate_filter(filter_expression
, &bytecode_len
, &ctx
);
1310 ret
= lttng_event_serialize(ev
, 0, NULL
, filter_expression
,
1312 ctx
&& (bytecode_len
!= 0) ? &ctx
->bytecode
->b
: NULL
,
1319 if (fd_to_send
>= 0) {
1323 /* Prepare command header */
1324 memset(&lsm
, 0, sizeof(lsm
));
1325 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1327 /* If no channel name, send empty string. */
1328 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1329 sizeof(lsm
.u
.disable
.channel_name
));
1331 ret
= -LTTNG_ERR_INVALID
;
1335 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1338 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1339 sizeof(lsm
.session
.name
));
1341 ret
= -LTTNG_ERR_INVALID
;
1345 /* Length of the serialized event */
1346 lsm
.u
.disable
.length
= (uint32_t) buf
.size
;
1348 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1349 send_fd
? &fd_to_send
: NULL
, send_fd
? 1 : 0,
1350 buf
.size
? buf
.data
: NULL
, buf
.size
, NULL
, NULL
, 0);
1353 if (filter_expression
&& ctx
) {
1354 filter_bytecode_free(ctx
);
1355 filter_ir_free(ctx
);
1356 filter_parser_ctx_free(ctx
);
1359 if (free_filter_expression
) {
1361 * The filter expression has been replaced and must be freed as
1362 * it is not the original filter expression received as a
1365 free(filter_expression
);
1368 lttng_dynamic_buffer_reset(&buf
);
1373 * Disable event(s) of a channel and domain.
1374 * If no event name is specified, all events are disabled.
1375 * If no channel name is specified, the default 'channel0' is used.
1376 * Returns size of returned session payload data or a negative error code.
1378 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1379 const char *channel_name
)
1382 struct lttng_event ev
;
1384 memset(&ev
, 0, sizeof(ev
));
1386 ev
.type
= LTTNG_EVENT_ALL
;
1387 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1389 ret
= -LTTNG_ERR_INVALID
;
1393 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1398 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1400 struct lttng_channel
*channel
= NULL
;
1406 /* Validate domain. */
1407 switch (domain
->type
) {
1408 case LTTNG_DOMAIN_UST
:
1409 switch (domain
->buf_type
) {
1410 case LTTNG_BUFFER_PER_UID
:
1411 case LTTNG_BUFFER_PER_PID
:
1417 case LTTNG_DOMAIN_KERNEL
:
1418 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1426 channel
= lttng_channel_create_internal();
1431 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1436 void lttng_channel_destroy(struct lttng_channel
*channel
)
1442 if (channel
->attr
.extended
.ptr
) {
1443 free(channel
->attr
.extended
.ptr
);
1449 * Enable channel per domain
1450 * Returns size of returned session payload data or a negative error code.
1452 int lttng_enable_channel(struct lttng_handle
*handle
,
1453 struct lttng_channel
*in_chan
)
1455 enum lttng_error_code ret_code
;
1457 struct lttng_dynamic_buffer buffer
;
1458 struct lttcomm_session_msg lsm
;
1459 uint64_t total_buffer_size_needed_per_cpu
= 0;
1460 struct lttng_channel
*channel
= NULL
;
1462 lttng_dynamic_buffer_init(&buffer
);
1464 /* NULL arguments are forbidden. No default values. */
1465 if (handle
== NULL
|| in_chan
== NULL
) {
1466 ret
= -LTTNG_ERR_INVALID
;
1471 * Verify that the amount of memory required to create the requested
1472 * buffer is available on the system at the moment.
1474 if (in_chan
->attr
.num_subbuf
>
1475 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1477 ret
= -LTTNG_ERR_OVERFLOW
;
1481 total_buffer_size_needed_per_cpu
=
1482 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1483 ret_code
= check_enough_available_memory(
1484 total_buffer_size_needed_per_cpu
);
1485 if (ret_code
!= LTTNG_OK
) {
1490 /* Copy the channel for easier manipulation. */
1491 channel
= lttng_channel_copy(in_chan
);
1493 ret
= -LTTNG_ERR_NOMEM
;
1497 /* Populate the channel extended attribute if necessary. */
1498 if (!channel
->attr
.extended
.ptr
) {
1499 struct lttng_channel_extended
*extended
=
1500 zmalloc(sizeof(*extended
));
1503 ret
= -LTTNG_ERR_NOMEM
;
1506 lttng_channel_set_default_extended_attr(
1507 &handle
->domain
, extended
);
1508 channel
->attr
.extended
.ptr
= extended
;
1511 /* Prepare the payload */
1512 memset(&lsm
, 0, sizeof(lsm
));
1514 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1515 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1517 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1518 sizeof(lsm
.session
.name
));
1520 ret
= -LTTNG_ERR_INVALID
;
1524 ret
= lttng_channel_serialize(channel
, &buffer
);
1526 ret
= -LTTNG_ERR_FATAL
;
1530 lsm
.u
.channel
.length
= buffer
.size
;
1532 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1533 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1535 lttng_channel_destroy(channel
);
1536 lttng_dynamic_buffer_reset(&buffer
);
1541 * All tracing will be stopped for registered events of the channel.
1542 * Returns size of returned session payload data or a negative error code.
1544 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1547 struct lttcomm_session_msg lsm
;
1549 /* Safety check. Both are mandatory. */
1550 if (handle
== NULL
|| name
== NULL
) {
1551 return -LTTNG_ERR_INVALID
;
1554 memset(&lsm
, 0, sizeof(lsm
));
1556 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1558 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1559 sizeof(lsm
.u
.disable
.channel_name
));
1561 ret
= -LTTNG_ERR_INVALID
;
1565 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1567 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1568 sizeof(lsm
.session
.name
));
1570 ret
= -LTTNG_ERR_INVALID
;
1574 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1580 * Lists all available tracepoints of domain.
1581 * Sets the contents of the events array.
1582 * Returns the number of lttng_event entries in events;
1583 * on error, returns a negative value.
1585 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1586 struct lttng_event
**events
)
1588 enum lttng_error_code ret_code
;
1589 int ret
, total_payload_received
;
1590 char *reception_buffer
= NULL
;
1591 struct lttcomm_session_msg lsm
;
1592 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1593 size_t cmd_header_len
;
1594 unsigned int nb_events
= 0;
1596 if (handle
== NULL
) {
1597 ret
= -LTTNG_ERR_INVALID
;
1601 memset(&lsm
, 0, sizeof(lsm
));
1602 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1603 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1605 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1606 (void **) &reception_buffer
, (void **) &cmd_header
,
1611 total_payload_received
= ret
;
1614 ret
= -LTTNG_ERR_UNK
;
1618 if (cmd_header
->count
> INT_MAX
) {
1619 ret
= -LTTNG_ERR_OVERFLOW
;
1623 nb_events
= (unsigned int) cmd_header
->count
;
1626 const struct lttng_buffer_view events_view
=
1627 lttng_buffer_view_init(reception_buffer
, 0,
1628 total_payload_received
);
1630 ret_code
= lttng_events_create_and_flatten_from_buffer(
1631 &events_view
, nb_events
, events
);
1632 if (ret_code
!= LTTNG_OK
) {
1638 ret
= (int) nb_events
;
1642 free(reception_buffer
);
1647 * Lists all available tracepoint fields of domain.
1648 * Sets the contents of the event field array.
1649 * Returns the number of lttng_event_field entries in events;
1650 * on error, returns a negative value.
1652 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1653 struct lttng_event_field
**fields
)
1656 struct lttcomm_session_msg lsm
;
1658 if (handle
== NULL
) {
1659 return -LTTNG_ERR_INVALID
;
1662 memset(&lsm
, 0, sizeof(lsm
));
1663 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1664 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1666 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1671 return ret
/ sizeof(struct lttng_event_field
);
1675 * Lists all available kernel system calls. Allocates and sets the contents of
1678 * Returns the number of lttng_event entries in events; on error, returns a
1681 int lttng_list_syscalls(struct lttng_event
**events
)
1683 enum lttng_error_code ret_code
;
1684 int ret
, total_payload_received
;
1685 char *reception_buffer
= NULL
;
1686 struct lttcomm_session_msg lsm
;
1687 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1688 size_t cmd_header_len
;
1689 uint32_t nb_events
= 0;
1692 ret
= -LTTNG_ERR_INVALID
;
1696 memset(&lsm
, 0, sizeof(lsm
));
1697 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1698 /* Force kernel domain for system calls. */
1699 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1701 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1702 (void **) &reception_buffer
, (void **) &cmd_header
,
1707 total_payload_received
= ret
;
1710 ret
= -LTTNG_ERR_UNK
;
1714 if (cmd_header
->count
> INT_MAX
) {
1715 ret
= -LTTNG_ERR_OVERFLOW
;
1719 nb_events
= (unsigned int) cmd_header
->count
;
1722 const struct lttng_buffer_view events_view
=
1723 lttng_buffer_view_init(reception_buffer
, 0,
1724 total_payload_received
);
1726 ret_code
= lttng_events_create_and_flatten_from_buffer(
1727 &events_view
, nb_events
, events
);
1728 if (ret_code
!= LTTNG_OK
) {
1734 ret
= (int) nb_events
;
1737 free(reception_buffer
);
1743 * Returns a human readable string describing
1744 * the error code (a negative value).
1746 const char *lttng_strerror(int code
)
1748 return error_get_str(code
);
1751 enum lttng_error_code
lttng_create_session_ext(
1752 struct lttng_session_descriptor
*session_descriptor
)
1754 enum lttng_error_code ret_code
;
1755 struct lttcomm_session_msg lsm
= {
1756 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1759 struct lttng_buffer_view reply_view
;
1761 bool sessiond_must_generate_ouput
;
1762 struct lttng_dynamic_buffer payload
;
1764 size_t descriptor_size
;
1765 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1767 lttng_dynamic_buffer_init(&payload
);
1768 if (!session_descriptor
) {
1769 ret_code
= LTTNG_ERR_INVALID
;
1773 sessiond_must_generate_ouput
=
1774 !lttng_session_descriptor_is_output_destination_initialized(
1775 session_descriptor
);
1776 if (sessiond_must_generate_ouput
) {
1777 const char *home_dir
= utils_get_home_dir();
1778 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1780 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1781 ret_code
= LTTNG_ERR_FATAL
;
1785 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1786 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1789 ret_code
= LTTNG_ERR_NOMEM
;
1794 descriptor_size
= payload
.size
;
1795 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1798 ret_code
= LTTNG_ERR_INVALID
;
1801 descriptor_size
= payload
.size
- descriptor_size
;
1802 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1804 /* Command returns a session descriptor on success. */
1805 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1806 payload
.size
, &reply
);
1807 if (reply_ret
< 0) {
1808 ret_code
= -reply_ret
;
1810 } else if (reply_ret
== 0) {
1811 /* Socket unexpectedly closed by the session daemon. */
1812 ret_code
= LTTNG_ERR_FATAL
;
1816 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1817 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1820 ret_code
= LTTNG_ERR_FATAL
;
1823 ret_code
= LTTNG_OK
;
1824 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1827 lttng_dynamic_buffer_reset(&payload
);
1828 lttng_session_descriptor_destroy(descriptor_reply
);
1833 * Create a new session using name and url for destination.
1835 * Return 0 on success else a negative LTTng error code.
1837 int lttng_create_session(const char *name
, const char *url
)
1841 struct lttng_uri
*uris
= NULL
;
1842 struct lttng_session_descriptor
*descriptor
= NULL
;
1843 enum lttng_error_code ret_code
;
1846 ret
= -LTTNG_ERR_INVALID
;
1850 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1852 ret
= -LTTNG_ERR_INVALID
;
1857 descriptor
= lttng_session_descriptor_create(name
);
1860 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1861 ret
= -LTTNG_ERR_INVALID
;
1864 descriptor
= lttng_session_descriptor_local_create(name
,
1868 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1872 ret
= -LTTNG_ERR_INVALID
;
1876 ret
= -LTTNG_ERR_INVALID
;
1879 ret_code
= lttng_create_session_ext(descriptor
);
1880 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1882 lttng_session_descriptor_destroy(descriptor
);
1888 * Create a session exclusively used for snapshot.
1890 * Return 0 on success else a negative LTTng error code.
1892 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1895 enum lttng_error_code ret_code
;
1897 struct lttng_uri
*uris
= NULL
;
1898 struct lttng_session_descriptor
*descriptor
= NULL
;
1901 ret
= -LTTNG_ERR_INVALID
;
1905 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1907 ret
= -LTTNG_ERR_INVALID
;
1911 * If the user does not specify a custom subdir, use the session name.
1913 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1914 strlen(uris
[0].subdir
) == 0) {
1915 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1918 PERROR("Failed to set session name as network destination sub-directory");
1919 ret
= -LTTNG_ERR_FATAL
;
1921 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1922 /* Truncated output. */
1923 ret
= -LTTNG_ERR_INVALID
;
1930 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1933 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1934 ret
= -LTTNG_ERR_INVALID
;
1937 descriptor
= lttng_session_descriptor_snapshot_local_create(
1942 descriptor
= lttng_session_descriptor_snapshot_network_create(
1948 ret
= -LTTNG_ERR_INVALID
;
1952 ret
= -LTTNG_ERR_INVALID
;
1955 ret_code
= lttng_create_session_ext(descriptor
);
1956 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1958 lttng_session_descriptor_destroy(descriptor
);
1964 * Create a session exclusively used for live.
1966 * Return 0 on success else a negative LTTng error code.
1968 int lttng_create_session_live(const char *name
, const char *url
,
1969 unsigned int timer_interval
)
1972 enum lttng_error_code ret_code
;
1973 struct lttng_session_descriptor
*descriptor
= NULL
;
1976 ret
= -LTTNG_ERR_INVALID
;
1981 descriptor
= lttng_session_descriptor_live_network_create(
1982 name
, url
, NULL
, timer_interval
);
1984 descriptor
= lttng_session_descriptor_live_create(
1985 name
, timer_interval
);
1988 ret
= -LTTNG_ERR_INVALID
;
1991 ret_code
= lttng_create_session_ext(descriptor
);
1992 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1994 lttng_session_descriptor_destroy(descriptor
);
1999 * Stop the session and wait for the data before destroying it
2001 * Return 0 on success else a negative LTTng error code.
2003 int lttng_destroy_session(const char *session_name
)
2006 enum lttng_error_code ret_code
;
2007 enum lttng_destruction_handle_status status
;
2008 struct lttng_destruction_handle
*handle
= NULL
;
2011 * Stop the tracing and wait for the data to be
2014 ret
= _lttng_stop_tracing(session_name
, 1);
2015 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2019 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2020 if (ret_code
!= LTTNG_OK
) {
2021 ret
= (int) -ret_code
;
2026 /* Block until the completion of the destruction of the session. */
2027 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2028 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2029 ret
= -LTTNG_ERR_UNK
;
2033 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2034 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2035 ret
= -LTTNG_ERR_UNK
;
2038 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2040 lttng_destruction_handle_destroy(handle
);
2045 * Destroy the session without waiting for the data.
2047 int lttng_destroy_session_no_wait(const char *session_name
)
2049 enum lttng_error_code ret_code
;
2051 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2052 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2056 * Ask the session daemon for all available sessions.
2057 * Sets the contents of the sessions array.
2058 * Returns the number of lttng_session entries in sessions;
2059 * on error, returns a negative value.
2061 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2064 struct lttcomm_session_msg lsm
;
2065 const size_t session_size
= sizeof(struct lttng_session
) +
2066 sizeof(struct lttng_session_extended
);
2067 size_t session_count
, i
;
2068 struct lttng_session_extended
*sessions_extended_begin
;
2069 struct lttng_session
*sessions
= NULL
;
2071 memset(&lsm
, 0, sizeof(lsm
));
2072 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2074 * Initialize out_sessions to NULL so it is initialized when
2075 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2076 * be subsequently freed.
2078 *out_sessions
= NULL
;
2079 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2084 ret
= -LTTNG_ERR_FATAL
;
2088 if (ret
% session_size
) {
2089 ret
= -LTTNG_ERR_UNK
;
2093 session_count
= (size_t) ret
/ session_size
;
2094 sessions_extended_begin
= (struct lttng_session_extended
*)
2095 (&sessions
[session_count
]);
2097 /* Set extended session info pointers. */
2098 for (i
= 0; i
< session_count
; i
++) {
2099 struct lttng_session
*session
= &sessions
[i
];
2100 struct lttng_session_extended
*extended
=
2101 &(sessions_extended_begin
[i
]);
2103 session
->extended
.ptr
= extended
;
2106 ret
= (int) session_count
;
2107 *out_sessions
= sessions
;
2112 enum lttng_error_code
lttng_session_get_creation_time(
2113 const struct lttng_session
*session
, uint64_t *creation_time
)
2115 enum lttng_error_code ret
= LTTNG_OK
;
2116 struct lttng_session_extended
*extended
;
2118 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2119 ret
= LTTNG_ERR_INVALID
;
2123 extended
= session
->extended
.ptr
;
2124 if (!extended
->creation_time
.is_set
) {
2125 /* Not created on the session daemon yet. */
2126 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2129 *creation_time
= extended
->creation_time
.value
;
2134 int lttng_set_session_shm_path(const char *session_name
,
2135 const char *shm_path
)
2138 struct lttcomm_session_msg lsm
;
2140 if (session_name
== NULL
) {
2141 return -LTTNG_ERR_INVALID
;
2144 memset(&lsm
, 0, sizeof(lsm
));
2145 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2147 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2148 sizeof(lsm
.session
.name
));
2150 ret
= -LTTNG_ERR_INVALID
;
2154 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2155 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2157 ret
= -LTTNG_ERR_INVALID
;
2161 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2167 * Ask the session daemon for all available domains of a session.
2168 * Sets the contents of the domains array.
2169 * Returns the number of lttng_domain entries in domains;
2170 * on error, returns a negative value.
2172 int lttng_list_domains(const char *session_name
,
2173 struct lttng_domain
**domains
)
2176 struct lttcomm_session_msg lsm
;
2178 if (session_name
== NULL
) {
2179 ret
= -LTTNG_ERR_INVALID
;
2183 memset(&lsm
, 0, sizeof(lsm
));
2184 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2186 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2187 sizeof(lsm
.session
.name
));
2189 ret
= -LTTNG_ERR_INVALID
;
2193 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2198 return ret
/ sizeof(struct lttng_domain
);
2204 * Ask the session daemon for all available channels of a session.
2205 * Sets the contents of the channels array.
2206 * Returns the number of lttng_channel entries in channels;
2207 * on error, returns a negative value.
2209 int lttng_list_channels(struct lttng_handle
*handle
,
2210 struct lttng_channel
**channels
)
2212 int ret
, total_payload_received
;
2213 struct lttcomm_session_msg lsm
;
2214 char *reception_buffer
= NULL
;
2215 size_t cmd_header_len
= 0;
2216 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2217 struct lttng_dynamic_buffer tmp_buffer
;
2219 lttng_dynamic_buffer_init(&tmp_buffer
);
2221 if (handle
== NULL
) {
2222 ret
= -LTTNG_ERR_INVALID
;
2226 memset(&lsm
, 0, sizeof(lsm
));
2227 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2228 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2229 sizeof(lsm
.session
.name
));
2231 ret
= -LTTNG_ERR_INVALID
;
2235 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2237 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2238 (void **) &reception_buffer
, (void **) &cmd_header
,
2244 total_payload_received
= ret
;
2246 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2247 ret
= -LTTNG_ERR_FATAL
;
2252 ret
= LTTNG_ERR_UNK
;
2256 if (cmd_header
->count
> INT_MAX
) {
2257 ret
= -LTTNG_ERR_OVERFLOW
;
2262 enum lttng_error_code ret_code
;
2263 const struct lttng_buffer_view events_view
=
2264 lttng_buffer_view_init(reception_buffer
, 0,
2265 total_payload_received
);
2267 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2268 &events_view
, cmd_header
->count
, channels
);
2269 if (ret_code
!= LTTNG_OK
) {
2275 ret
= (int) cmd_header
->count
;
2278 free(reception_buffer
);
2283 * Ask the session daemon for all available events of a session channel.
2284 * Sets the contents of the events array.
2285 * Returns the number of lttng_event entries in events;
2286 * on error, returns a negative value.
2288 int lttng_list_events(struct lttng_handle
*handle
,
2289 const char *channel_name
, struct lttng_event
**events
)
2291 enum lttng_error_code ret_code
;
2293 struct lttcomm_session_msg lsm
;
2294 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2295 size_t cmd_header_len
;
2296 unsigned int nb_events
;
2297 char *reception_buffer
= NULL
;
2298 struct lttng_dynamic_buffer tmp_buffer
;
2299 ssize_t total_payload_received
;
2301 lttng_dynamic_buffer_init(&tmp_buffer
);
2303 /* Safety check. An handle and channel name are mandatory */
2304 if (handle
== NULL
|| channel_name
== NULL
) {
2305 return -LTTNG_ERR_INVALID
;
2308 memset(&lsm
, 0, sizeof(lsm
));
2309 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2310 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2311 sizeof(lsm
.session
.name
));
2313 ret
= -LTTNG_ERR_INVALID
;
2317 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2318 sizeof(lsm
.u
.list
.channel_name
));
2320 ret
= -LTTNG_ERR_INVALID
;
2324 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2326 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2327 (void **) &reception_buffer
, (void **) &cmd_header
,
2333 total_payload_received
= ret
;
2336 ret
= -LTTNG_ERR_UNK
;
2340 if (cmd_header
->count
> INT_MAX
) {
2341 ret
= -LTTNG_ERR_OVERFLOW
;
2345 nb_events
= (unsigned int) cmd_header
->count
;
2348 const struct lttng_buffer_view events_view
=
2349 lttng_buffer_view_init(reception_buffer
, 0,
2350 total_payload_received
);
2351 ret_code
= lttng_events_create_and_flatten_from_buffer(
2352 &events_view
, nb_events
, events
);
2353 if (ret_code
!= LTTNG_OK
) {
2359 ret
= (int) nb_events
;
2362 free(reception_buffer
);
2367 * Sets the tracing_group variable with name.
2368 * This function allocates memory pointed to by tracing_group.
2369 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2371 int lttng_set_tracing_group(const char *name
)
2375 return -LTTNG_ERR_INVALID
;
2378 if (asprintf(&new_group
, "%s", name
) < 0) {
2379 return -LTTNG_ERR_FATAL
;
2382 free(tracing_group
);
2383 tracing_group
= new_group
;
2389 int lttng_calibrate(struct lttng_handle
*handle
,
2390 struct lttng_calibrate
*calibrate
)
2393 * This command was removed in LTTng 2.9.
2395 return -LTTNG_ERR_UND
;
2399 * Set default channel attributes.
2400 * If either or both of the arguments are null, attr content is zeroe'd.
2402 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2403 struct lttng_channel_attr
*attr
)
2405 struct lttng_channel_extended
*extended
;
2408 if (attr
== NULL
|| domain
== NULL
) {
2412 /* Save the pointer for later use */
2413 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2414 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2416 /* Same for all domains. */
2417 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2418 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2419 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2421 switch (domain
->type
) {
2422 case LTTNG_DOMAIN_KERNEL
:
2423 attr
->switch_timer_interval
=
2424 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2425 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2426 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2427 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2428 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2430 case LTTNG_DOMAIN_UST
:
2431 switch (domain
->buf_type
) {
2432 case LTTNG_BUFFER_PER_UID
:
2433 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2434 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2435 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2436 attr
->switch_timer_interval
=
2437 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2438 attr
->read_timer_interval
=
2439 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2441 case LTTNG_BUFFER_PER_PID
:
2443 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2444 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2445 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2446 attr
->switch_timer_interval
=
2447 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2448 attr
->read_timer_interval
=
2449 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2453 /* Default behavior: leave set to 0. */
2458 lttng_channel_set_default_extended_attr(domain
, extended
);
2461 /* Reassign the extended pointer. */
2462 attr
->extended
.ptr
= extended
;
2465 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2466 uint64_t *discarded_events
)
2469 struct lttng_channel_extended
*chan_ext
;
2471 if (!channel
|| !discarded_events
) {
2472 ret
= -LTTNG_ERR_INVALID
;
2476 chan_ext
= channel
->attr
.extended
.ptr
;
2479 * This can happen since the lttng_channel structure is
2480 * used for other tasks where this pointer is never set.
2482 *discarded_events
= 0;
2486 *discarded_events
= chan_ext
->discarded_events
;
2491 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2492 uint64_t *lost_packets
)
2495 struct lttng_channel_extended
*chan_ext
;
2497 if (!channel
|| !lost_packets
) {
2498 ret
= -LTTNG_ERR_INVALID
;
2502 chan_ext
= channel
->attr
.extended
.ptr
;
2505 * This can happen since the lttng_channel structure is
2506 * used for other tasks where this pointer is never set.
2512 *lost_packets
= chan_ext
->lost_packets
;
2517 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2518 uint64_t *monitor_timer_interval
)
2522 if (!chan
|| !monitor_timer_interval
) {
2523 ret
= -LTTNG_ERR_INVALID
;
2527 if (!chan
->attr
.extended
.ptr
) {
2528 ret
= -LTTNG_ERR_INVALID
;
2532 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2533 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2538 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2539 uint64_t monitor_timer_interval
)
2543 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2544 ret
= -LTTNG_ERR_INVALID
;
2548 ((struct lttng_channel_extended
*)
2549 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2550 monitor_timer_interval
;
2555 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2556 int64_t *blocking_timeout
)
2560 if (!chan
|| !blocking_timeout
) {
2561 ret
= -LTTNG_ERR_INVALID
;
2565 if (!chan
->attr
.extended
.ptr
) {
2566 ret
= -LTTNG_ERR_INVALID
;
2570 *blocking_timeout
= ((struct lttng_channel_extended
*)
2571 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2576 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2577 int64_t blocking_timeout
)
2580 int64_t msec_timeout
;
2582 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2583 ret
= -LTTNG_ERR_INVALID
;
2587 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2588 ret
= -LTTNG_ERR_INVALID
;
2593 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2594 * us to accept a narrower range of values (msecs expressed as a signed
2597 msec_timeout
= blocking_timeout
/ 1000;
2598 if (msec_timeout
!= (int32_t) msec_timeout
) {
2599 ret
= -LTTNG_ERR_INVALID
;
2603 ((struct lttng_channel_extended
*)
2604 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2611 * Check if session daemon is alive.
2613 * Return 1 if alive or 0 if not.
2614 * On error returns a negative value.
2616 int lttng_session_daemon_alive(void)
2620 ret
= set_session_daemon_path();
2626 if (*sessiond_sock_path
== '\0') {
2628 * No socket path set. Weird error which means the constructor
2634 ret
= try_connect_sessiond(sessiond_sock_path
);
2645 * Set URL for a consumer for a session and domain.
2647 * Return 0 on success, else a negative value.
2649 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2650 const char *control_url
, const char *data_url
)
2654 struct lttcomm_session_msg lsm
;
2655 struct lttng_uri
*uris
= NULL
;
2657 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2658 ret
= -LTTNG_ERR_INVALID
;
2662 memset(&lsm
, 0, sizeof(lsm
));
2664 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2666 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2667 sizeof(lsm
.session
.name
));
2669 ret
= -LTTNG_ERR_INVALID
;
2673 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2675 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2677 ret
= -LTTNG_ERR_INVALID
;
2681 lsm
.u
.uri
.size
= size
;
2683 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2684 sizeof(struct lttng_uri
) * size
, NULL
);
2694 int lttng_enable_consumer(struct lttng_handle
*handle
);
2695 int lttng_enable_consumer(struct lttng_handle
*handle
)
2703 int lttng_disable_consumer(struct lttng_handle
*handle
);
2704 int lttng_disable_consumer(struct lttng_handle
*handle
)
2712 int _lttng_create_session_ext(const char *name
, const char *url
,
2713 const char *datetime
);
2714 int _lttng_create_session_ext(const char *name
, const char *url
,
2715 const char *datetime
)
2721 * For a given session name, this call checks if the data is ready to be read
2722 * or is still being extracted by the consumer(s) hence not ready to be used by
2725 int lttng_data_pending(const char *session_name
)
2728 struct lttcomm_session_msg lsm
;
2729 uint8_t *pending
= NULL
;
2731 if (session_name
== NULL
) {
2732 return -LTTNG_ERR_INVALID
;
2735 memset(&lsm
, 0, sizeof(lsm
));
2736 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2738 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2739 sizeof(lsm
.session
.name
));
2741 ret
= -LTTNG_ERR_INVALID
;
2745 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2748 } else if (ret
!= 1) {
2749 /* Unexpected payload size */
2750 ret
= -LTTNG_ERR_INVALID
;
2752 } else if (!pending
) {
2753 /* Internal error. */
2754 ret
= -LTTNG_ERR_UNK
;
2758 ret
= (int) *pending
;
2765 * Regenerate the metadata for a session.
2766 * Return 0 on success, a negative error code on error.
2768 int lttng_regenerate_metadata(const char *session_name
)
2771 struct lttcomm_session_msg lsm
;
2773 if (!session_name
) {
2774 ret
= -LTTNG_ERR_INVALID
;
2778 memset(&lsm
, 0, sizeof(lsm
));
2779 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2781 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2782 sizeof(lsm
.session
.name
));
2784 ret
= -LTTNG_ERR_INVALID
;
2788 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2799 * Deprecated, replaced by lttng_regenerate_metadata.
2801 int lttng_metadata_regenerate(const char *session_name
)
2803 return lttng_regenerate_metadata(session_name
);
2807 * Regenerate the statedump of a session.
2808 * Return 0 on success, a negative error code on error.
2810 int lttng_regenerate_statedump(const char *session_name
)
2813 struct lttcomm_session_msg lsm
;
2815 if (!session_name
) {
2816 ret
= -LTTNG_ERR_INVALID
;
2820 memset(&lsm
, 0, sizeof(lsm
));
2821 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2823 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2824 sizeof(lsm
.session
.name
));
2826 ret
= -LTTNG_ERR_INVALID
;
2830 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2840 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2843 struct lttcomm_session_msg lsm
;
2844 struct lttng_dynamic_buffer buffer
;
2846 lttng_dynamic_buffer_init(&buffer
);
2848 ret
= -LTTNG_ERR_INVALID
;
2852 if (!lttng_trigger_validate(trigger
)) {
2853 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2857 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2859 ret
= -LTTNG_ERR_UNK
;
2863 memset(&lsm
, 0, sizeof(lsm
));
2864 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2865 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2866 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2869 lttng_dynamic_buffer_reset(&buffer
);
2873 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2876 struct lttcomm_session_msg lsm
;
2877 struct lttng_dynamic_buffer buffer
;
2879 lttng_dynamic_buffer_init(&buffer
);
2881 ret
= -LTTNG_ERR_INVALID
;
2885 if (!lttng_trigger_validate(trigger
)) {
2886 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2890 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2892 ret
= -LTTNG_ERR_UNK
;
2896 memset(&lsm
, 0, sizeof(lsm
));
2897 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2898 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2899 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2902 lttng_dynamic_buffer_reset(&buffer
);
2909 static void __attribute__((constructor
)) init(void)
2911 /* Set default session group */
2912 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2918 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2920 free(tracing_group
);