4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/common.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/tracker.h>
28 #include <common/uri.h>
29 #include <common/utils.h>
30 #include <lttng/channel-internal.h>
31 #include <lttng/destruction-handle.h>
32 #include <lttng/endpoint.h>
33 #include <lttng/event-internal.h>
34 #include <lttng/health-internal.h>
35 #include <lttng/lttng.h>
36 #include <lttng/session-descriptor-internal.h>
37 #include <lttng/session-internal.h>
38 #include <lttng/trigger/trigger-internal.h>
39 #include <lttng/userspace-probe-internal.h>
41 #include "filter/filter-ast.h"
42 #include "filter/filter-parser.h"
43 #include "filter/filter-bytecode.h"
44 #include "filter/memstream.h"
45 #include "lttng-ctl-helper.h"
48 static const int print_xml
= 1;
49 #define dbg_printf(fmt, args...) \
50 printf("[debug liblttng-ctl] " fmt, ## args)
52 static const int print_xml
= 0;
53 #define dbg_printf(fmt, args...) \
55 /* do nothing but check printf format */ \
57 printf("[debug liblttnctl] " fmt, ## args); \
61 #define COPY_DOMAIN_PACKED(dst, src) \
63 struct lttng_domain _tmp_domain; \
65 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
69 /* Socket to session daemon for communication */
70 static int sessiond_socket
= -1;
71 static char sessiond_sock_path
[PATH_MAX
];
74 static char *tracing_group
;
80 * Those two variables are used by error.h to silent or control the verbosity of
81 * error message. They are global to the library so application linking with it
82 * are able to compile correctly and also control verbosity of the library.
85 int lttng_opt_verbose
;
89 * Copy domain to lttcomm_session_msg domain.
91 * If domain is unknown, default domain will be the kernel.
94 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
95 struct lttng_domain
*src
)
99 case LTTNG_DOMAIN_KERNEL
:
100 case LTTNG_DOMAIN_UST
:
101 case LTTNG_DOMAIN_JUL
:
102 case LTTNG_DOMAIN_LOG4J
:
103 case LTTNG_DOMAIN_PYTHON
:
104 memcpy(dst
, src
, sizeof(struct lttng_domain
));
107 memset(dst
, 0, sizeof(struct lttng_domain
));
114 * Send lttcomm_session_msg to the session daemon.
116 * On success, returns the number of bytes sent (>=0)
117 * On error, returns -1
119 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
124 ret
= -LTTNG_ERR_NO_SESSIOND
;
128 DBG("LSM cmd type : %d", lsm
->cmd_type
);
130 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
131 sizeof(struct lttcomm_session_msg
));
133 ret
= -LTTNG_ERR_FATAL
;
141 * Send var len data to the session daemon.
143 * On success, returns the number of bytes sent (>=0)
144 * On error, returns -1
146 static int send_session_varlen(const void *data
, size_t len
)
151 ret
= -LTTNG_ERR_NO_SESSIOND
;
160 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
162 ret
= -LTTNG_ERR_FATAL
;
170 * Send file descriptors to the session daemon.
172 * On success, returns the number of bytes sent (>=0)
173 * On error, returns -1
175 static int send_session_fds(const int *fds
, size_t nb_fd
)
180 ret
= -LTTNG_ERR_NO_SESSIOND
;
184 if (!fds
|| !nb_fd
) {
189 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
191 ret
= -LTTNG_ERR_FATAL
;
199 * Receive data from the sessiond socket.
201 * On success, returns the number of bytes received (>=0)
202 * On error, returns -1 (recvmsg() error) or -ENOTCONN
204 static int recv_data_sessiond(void *buf
, size_t len
)
209 ret
= -LTTNG_ERR_NO_SESSIOND
;
213 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
215 ret
= -LTTNG_ERR_FATAL
;
223 * Check if we are in the specified group.
225 * If yes return 1, else return -1.
228 int lttng_check_tracing_group(void)
230 gid_t
*grp_list
, tracing_gid
;
231 int grp_list_size
, grp_id
, i
;
233 const char *grp_name
= tracing_group
;
235 /* Get GID of group 'tracing' */
236 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
237 /* If grp_tracing is NULL, the group does not exist. */
241 /* Get number of supplementary group IDs */
242 grp_list_size
= getgroups(0, NULL
);
243 if (grp_list_size
< 0) {
248 /* Alloc group list of the right size */
249 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
254 grp_id
= getgroups(grp_list_size
, grp_list
);
260 for (i
= 0; i
< grp_list_size
; i
++) {
261 if (grp_list
[i
] == tracing_gid
) {
274 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
278 size_t best_mem_info
;
279 size_t num_bytes_requested_total
;
282 * Get the number of CPU currently online to compute the amount of
283 * memory needed to create a buffer for every CPU.
285 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
290 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
293 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
294 * reliable estimate we can get but it is only exposed by the kernel
295 * since 3.14. (See Linux kernel commit:
296 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
298 ret
= utils_get_memory_available(&best_mem_info
);
304 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
305 * is a sanity check for obvious user error.
307 ret
= utils_get_memory_total(&best_mem_info
);
315 return best_mem_info
>= num_bytes_requested_total
;
319 * Try connect to session daemon with sock_path.
321 * Return 0 on success, else -1
323 static int try_connect_sessiond(const char *sock_path
)
327 /* If socket exist, we check if the daemon listens for connect. */
328 ret
= access(sock_path
, F_OK
);
334 ret
= lttcomm_connect_unix_sock(sock_path
);
340 ret
= lttcomm_close_unix_sock(ret
);
342 PERROR("lttcomm_close_unix_sock");
352 * Set sessiond socket path by putting it in the global sessiond_sock_path
355 * Returns 0 on success, negative value on failure (the sessiond socket path
356 * is somehow too long or ENOMEM).
358 static int set_session_daemon_path(void)
360 int in_tgroup
= 0; /* In tracing group. */
366 /* Are we in the tracing group ? */
367 in_tgroup
= lttng_check_tracing_group();
370 if ((uid
== 0) || in_tgroup
== 1) {
371 const int ret
= lttng_strncpy(sessiond_sock_path
,
372 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
373 sizeof(sessiond_sock_path
));
385 ret
= try_connect_sessiond(sessiond_sock_path
);
389 /* Global session daemon not available... */
391 /* ...or not in tracing group (and not root), default */
394 * With GNU C < 2.1, snprintf returns -1 if the target buffer
396 * With GNU C >= 2.1, snprintf returns the required size
397 * (excluding closing null)
399 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
400 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
401 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
413 * Connect to the LTTng session daemon.
415 * On success, return the socket's file descriptor. On error, return -1.
417 LTTNG_HIDDEN
int connect_sessiond(void)
421 ret
= set_session_daemon_path();
426 /* Connect to the sesssion daemon. */
427 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
438 static void reset_global_sessiond_connection_state(void)
440 sessiond_socket
= -1;
445 * Clean disconnect from the session daemon.
447 * On success, return 0. On error, return -1.
449 static int disconnect_sessiond(void)
454 ret
= lttcomm_close_unix_sock(sessiond_socket
);
455 reset_global_sessiond_connection_state();
461 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
469 ret
= -LTTNG_ERR_INVALID
;
479 ret
= recv_data_sessiond(buf
, len
);
485 ret
= -LTTNG_ERR_INVALID
;
489 /* Move ownership of command header buffer to user. */
494 /* No command header. */
510 * Ask the session daemon a specific command and put the data into buf.
511 * Takes extra var. len. data and file descriptors as input to send to the
514 * Return size of data (only payload, not header) or a negative error code.
517 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
518 const int *fds
, size_t nb_fd
, const void *vardata
,
519 size_t vardata_len
, void **user_payload_buf
,
520 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
524 struct lttcomm_lttng_msg llm
;
526 ret
= connect_sessiond();
528 ret
= -LTTNG_ERR_NO_SESSIOND
;
531 sessiond_socket
= ret
;
535 /* Send command to session daemon */
536 ret
= send_session_msg(lsm
);
538 /* Ret value is a valid lttng error code. */
541 /* Send var len data */
542 ret
= send_session_varlen(vardata
, vardata_len
);
544 /* Ret value is a valid lttng error code. */
549 ret
= send_session_fds(fds
, nb_fd
);
551 /* Ret value is a valid lttng error code. */
555 /* Get header from data transmission */
556 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
558 /* Ret value is a valid lttng error code. */
562 /* Check error code if OK */
563 if (llm
.ret_code
!= LTTNG_OK
) {
568 /* Get command header from data transmission */
569 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
570 user_cmd_header_buf
, user_cmd_header_len
);
575 /* Get payload from data transmission */
576 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
585 disconnect_sessiond();
590 * Create lttng handle and return pointer.
592 * The returned pointer will be NULL in case of malloc() error.
594 struct lttng_handle
*lttng_create_handle(const char *session_name
,
595 struct lttng_domain
*domain
)
598 struct lttng_handle
*handle
= NULL
;
600 handle
= zmalloc(sizeof(struct lttng_handle
));
601 if (handle
== NULL
) {
602 PERROR("malloc handle");
606 /* Copy session name */
607 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
608 sizeof(handle
->session_name
));
613 /* Copy lttng domain or leave initialized to 0. */
615 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
626 * Destroy handle by free(3) the pointer.
628 void lttng_destroy_handle(struct lttng_handle
*handle
)
634 * Register an outside consumer.
636 * Returns size of returned session payload data or a negative error code.
638 int lttng_register_consumer(struct lttng_handle
*handle
,
639 const char *socket_path
)
642 struct lttcomm_session_msg lsm
;
644 if (handle
== NULL
|| socket_path
== NULL
) {
645 ret
= -LTTNG_ERR_INVALID
;
649 memset(&lsm
, 0, sizeof(lsm
));
650 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
651 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
652 sizeof(lsm
.session
.name
));
654 ret
= -LTTNG_ERR_INVALID
;
658 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
660 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
661 sizeof(lsm
.u
.reg
.path
));
663 ret
= -LTTNG_ERR_INVALID
;
667 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
673 * Start tracing for all traces of the session.
675 * Returns size of returned session payload data or a negative error code.
677 int lttng_start_tracing(const char *session_name
)
680 struct lttcomm_session_msg lsm
;
682 if (session_name
== NULL
) {
683 ret
= -LTTNG_ERR_INVALID
;
687 memset(&lsm
, 0, sizeof(lsm
));
688 lsm
.cmd_type
= LTTNG_START_TRACE
;
690 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
691 sizeof(lsm
.session
.name
));
693 ret
= -LTTNG_ERR_INVALID
;
697 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
703 * Stop tracing for all traces of the session.
705 static int _lttng_stop_tracing(const char *session_name
, int wait
)
708 struct lttcomm_session_msg lsm
;
710 if (session_name
== NULL
) {
711 ret
= -LTTNG_ERR_INVALID
;
715 memset(&lsm
, 0, sizeof(lsm
));
716 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
718 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
719 sizeof(lsm
.session
.name
));
721 ret
= -LTTNG_ERR_INVALID
;
725 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
726 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
734 /* Check for data availability */
736 data_ret
= lttng_data_pending(session_name
);
738 /* Return the data available call error. */
744 * Data sleep time before retrying (in usec). Don't sleep if the
745 * call returned value indicates availability.
748 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
750 } while (data_ret
!= 0);
758 * Stop tracing and wait for data availability.
760 int lttng_stop_tracing(const char *session_name
)
762 return _lttng_stop_tracing(session_name
, 1);
766 * Stop tracing but _don't_ wait for data availability.
768 int lttng_stop_tracing_no_wait(const char *session_name
)
770 return _lttng_stop_tracing(session_name
, 0);
774 * Add context to a channel.
776 * If the given channel is NULL, add the contexts to all channels.
777 * The event_name param is ignored.
779 * Returns the size of the returned payload data or a negative error code.
781 int lttng_add_context(struct lttng_handle
*handle
,
782 struct lttng_event_context
*ctx
, const char *event_name
,
783 const char *channel_name
)
788 struct lttcomm_session_msg lsm
;
790 /* Safety check. Both are mandatory. */
791 if (handle
== NULL
|| ctx
== NULL
) {
792 ret
= -LTTNG_ERR_INVALID
;
796 memset(&lsm
, 0, sizeof(lsm
));
797 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
799 /* If no channel name, send empty string. */
800 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
801 sizeof(lsm
.u
.context
.channel_name
));
803 ret
= -LTTNG_ERR_INVALID
;
807 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
808 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
809 sizeof(lsm
.session
.name
));
811 ret
= -LTTNG_ERR_INVALID
;
815 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
816 size_t provider_len
, ctx_len
;
817 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
818 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
820 if (!provider_name
|| !ctx_name
) {
821 ret
= -LTTNG_ERR_INVALID
;
825 provider_len
= strlen(provider_name
);
826 if (provider_len
== 0) {
827 ret
= -LTTNG_ERR_INVALID
;
830 lsm
.u
.context
.provider_name_len
= provider_len
;
832 ctx_len
= strlen(ctx_name
);
834 ret
= -LTTNG_ERR_INVALID
;
837 lsm
.u
.context
.context_name_len
= ctx_len
;
839 len
= provider_len
+ ctx_len
;
842 ret
= -LTTNG_ERR_NOMEM
;
846 memcpy(buf
, provider_name
, provider_len
);
847 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
849 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
851 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
853 * Don't leak application addresses to the sessiond.
854 * This is only necessary when ctx is for an app ctx otherwise
855 * the values inside the union (type & config) are overwritten.
857 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
858 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
861 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
868 * Enable event(s) for a channel.
870 * If no event name is specified, all events are enabled.
871 * If no channel name is specified, the default 'channel0' is used.
873 * Returns size of returned session payload data or a negative error code.
875 int lttng_enable_event(struct lttng_handle
*handle
,
876 struct lttng_event
*ev
, const char *channel_name
)
878 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
883 * Create or enable an event with a filter expression.
885 * Return negative error value on error.
886 * Return size of returned session payload data if OK.
888 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
889 struct lttng_event
*event
, const char *channel_name
,
890 const char *filter_expression
)
892 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
893 filter_expression
, 0, NULL
);
897 * Depending on the event, return a newly allocated agent filter expression or
898 * NULL if not applicable.
900 * An event with NO loglevel and the name is * will return NULL.
902 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
905 char *agent_filter
= NULL
;
909 /* Don't add filter for the '*' event. */
910 if (strcmp(ev
->name
, "*") != 0) {
912 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
915 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
923 /* Add loglevel filtering if any for the JUL domain. */
924 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
927 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
933 if (filter
|| agent_filter
) {
936 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
937 agent_filter
? agent_filter
: filter
, op
,
942 agent_filter
= new_filter
;
944 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
960 * Generate the filter bytecode from a given filter expression string. Put the
961 * newly allocated parser context in ctxp and populate the lsm object with the
964 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
966 static int generate_filter(char *filter_expression
,
967 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
970 struct filter_parser_ctx
*ctx
= NULL
;
973 assert(filter_expression
);
978 * Casting const to non-const, as the underlying function will use it in
981 fmem
= lttng_fmemopen((void *) filter_expression
,
982 strlen(filter_expression
), "r");
984 fprintf(stderr
, "Error opening memory as stream\n");
985 ret
= -LTTNG_ERR_FILTER_NOMEM
;
988 ctx
= filter_parser_ctx_alloc(fmem
);
990 fprintf(stderr
, "Error allocating parser\n");
991 ret
= -LTTNG_ERR_FILTER_NOMEM
;
992 goto filter_alloc_error
;
994 ret
= filter_parser_ctx_append_ast(ctx
);
996 fprintf(stderr
, "Parse error\n");
997 ret
= -LTTNG_ERR_FILTER_INVAL
;
1001 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
1004 fprintf(stderr
, "XML print error\n");
1005 ret
= -LTTNG_ERR_FILTER_INVAL
;
1010 dbg_printf("Generating IR... ");
1012 ret
= filter_visitor_ir_generate(ctx
);
1014 fprintf(stderr
, "Generate IR error\n");
1015 ret
= -LTTNG_ERR_FILTER_INVAL
;
1018 dbg_printf("done\n");
1020 dbg_printf("Validating IR... ");
1022 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1024 ret
= -LTTNG_ERR_FILTER_INVAL
;
1028 /* Normalize globbing patterns in the expression. */
1029 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1031 ret
= -LTTNG_ERR_FILTER_INVAL
;
1035 /* Validate strings used as literals in the expression. */
1036 ret
= filter_visitor_ir_validate_string(ctx
);
1038 ret
= -LTTNG_ERR_FILTER_INVAL
;
1042 /* Validate globbing patterns in the expression. */
1043 ret
= filter_visitor_ir_validate_globbing(ctx
);
1045 ret
= -LTTNG_ERR_FILTER_INVAL
;
1049 dbg_printf("done\n");
1051 dbg_printf("Generating bytecode... ");
1053 ret
= filter_visitor_bytecode_generate(ctx
);
1055 fprintf(stderr
, "Generate bytecode error\n");
1056 ret
= -LTTNG_ERR_FILTER_INVAL
;
1059 dbg_printf("done\n");
1060 dbg_printf("Size of bytecode generated: %u bytes.\n",
1061 bytecode_get_len(&ctx
->bytecode
->b
));
1063 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1064 + bytecode_get_len(&ctx
->bytecode
->b
);
1065 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1067 /* No need to keep the memory stream. */
1068 if (fclose(fmem
) != 0) {
1076 filter_ir_free(ctx
);
1077 filter_parser_ctx_free(ctx
);
1079 if (fclose(fmem
) != 0) {
1087 * Enable event(s) for a channel, possibly with exclusions and a filter.
1088 * If no event name is specified, all events are enabled.
1089 * If no channel name is specified, the default name is used.
1090 * If filter expression is not NULL, the filter is set for the event.
1091 * If exclusion count is not zero, the exclusions are set for the event.
1092 * Returns size of returned session payload data or a negative error code.
1094 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1095 struct lttng_event
*ev
, const char *channel_name
,
1096 const char *original_filter_expression
,
1097 int exclusion_count
, char **exclusion_list
)
1099 struct lttcomm_session_msg lsm
;
1100 struct lttng_dynamic_buffer send_buffer
;
1101 int ret
= 0, i
, fd_to_send
= -1;
1102 bool send_fd
= false;
1103 unsigned int free_filter_expression
= 0;
1104 struct filter_parser_ctx
*ctx
= NULL
;
1107 * We have either a filter or some exclusions, so we need to set up
1108 * a variable-length memory block from where to send the data.
1110 lttng_dynamic_buffer_init(&send_buffer
);
1113 * Cast as non-const since we may replace the filter expression
1114 * by a dynamically allocated string. Otherwise, the original
1115 * string is not modified.
1117 char *filter_expression
= (char *) original_filter_expression
;
1119 if (handle
== NULL
|| ev
== NULL
) {
1120 ret
= -LTTNG_ERR_INVALID
;
1125 * Empty filter string will always be rejected by the parser
1126 * anyway, so treat this corner-case early to eliminate
1127 * lttng_fmemopen error for 0-byte allocation.
1129 if (filter_expression
&& filter_expression
[0] == '\0') {
1130 ret
= -LTTNG_ERR_INVALID
;
1134 memset(&lsm
, 0, sizeof(lsm
));
1136 /* If no channel name, send empty string. */
1137 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1138 sizeof(lsm
.u
.enable
.channel_name
));
1140 ret
= -LTTNG_ERR_INVALID
;
1144 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1145 if (ev
->name
[0] == '\0') {
1146 /* Enable all events. */
1147 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1151 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1152 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1154 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1155 sizeof(lsm
.session
.name
));
1157 ret
= -LTTNG_ERR_INVALID
;
1161 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1162 lsm
.u
.enable
.bytecode_len
= 0;
1164 /* Parse filter expression. */
1165 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1166 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1167 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1168 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1169 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1170 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1173 /* Setup JUL filter if needed. */
1174 agent_filter
= set_agent_filter(filter_expression
, ev
);
1175 if (!agent_filter
) {
1176 if (!filter_expression
) {
1178 * No JUL and no filter, just skip
1185 * With an agent filter, the original filter has
1186 * been added to it thus replace the filter
1189 filter_expression
= agent_filter
;
1190 free_filter_expression
= 1;
1194 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1200 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1201 lsm
.u
.enable
.bytecode_len
1202 + lsm
.u
.enable
.expression_len
1203 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1205 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1209 /* Put exclusion names first in the data. */
1210 for (i
= 0; i
< exclusion_count
; i
++) {
1211 size_t exclusion_len
;
1213 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1214 LTTNG_SYMBOL_NAME_LEN
);
1215 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1216 /* Exclusion is not NULL-terminated. */
1217 ret
= -LTTNG_ERR_INVALID
;
1221 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1222 *(exclusion_list
+ i
),
1223 LTTNG_SYMBOL_NAME_LEN
);
1229 /* Add filter expression next. */
1230 if (filter_expression
) {
1231 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1232 filter_expression
, lsm
.u
.enable
.expression_len
);
1237 /* Add filter bytecode next. */
1238 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1239 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1240 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1245 if (ev
->extended
.ptr
) {
1246 struct lttng_event_extended
*ev_ext
=
1247 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1249 if (ev_ext
->probe_location
) {
1251 * lttng_userspace_probe_location_serialize returns the
1252 * number of bytes that was appended to the buffer.
1254 ret
= lttng_userspace_probe_location_serialize(
1255 ev_ext
->probe_location
, &send_buffer
,
1261 send_fd
= fd_to_send
>= 0;
1263 * Set the size of the userspace probe location element
1264 * of the buffer so that the receiving side knows where
1267 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1271 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1272 send_fd
? &fd_to_send
: NULL
,
1274 send_buffer
.size
? send_buffer
.data
: NULL
,
1275 send_buffer
.size
, NULL
, NULL
, 0);
1278 if (filter_expression
&& ctx
) {
1279 filter_bytecode_free(ctx
);
1280 filter_ir_free(ctx
);
1281 filter_parser_ctx_free(ctx
);
1284 if (free_filter_expression
) {
1286 * The filter expression has been replaced and must be freed as
1287 * it is not the original filter expression received as a
1290 free(filter_expression
);
1294 * Return directly to the caller and don't ask the sessiond since
1295 * something went wrong in the parsing of data above.
1297 lttng_dynamic_buffer_reset(&send_buffer
);
1301 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1305 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1306 struct lttng_event
*ev
, const char *channel_name
,
1307 const char *original_filter_expression
)
1309 struct lttcomm_session_msg lsm
;
1312 unsigned int free_filter_expression
= 0;
1313 struct filter_parser_ctx
*ctx
= NULL
;
1315 * Cast as non-const since we may replace the filter expression
1316 * by a dynamically allocated string. Otherwise, the original
1317 * string is not modified.
1319 char *filter_expression
= (char *) original_filter_expression
;
1321 if (handle
== NULL
|| ev
== NULL
) {
1322 ret
= -LTTNG_ERR_INVALID
;
1327 * Empty filter string will always be rejected by the parser
1328 * anyway, so treat this corner-case early to eliminate
1329 * lttng_fmemopen error for 0-byte allocation.
1331 if (filter_expression
&& filter_expression
[0] == '\0') {
1332 ret
= -LTTNG_ERR_INVALID
;
1336 memset(&lsm
, 0, sizeof(lsm
));
1338 /* If no channel name, send empty string. */
1339 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1340 sizeof(lsm
.u
.disable
.channel_name
));
1342 ret
= -LTTNG_ERR_INVALID
;
1346 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1348 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1349 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1351 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1352 sizeof(lsm
.session
.name
));
1354 ret
= -LTTNG_ERR_INVALID
;
1358 lsm
.u
.disable
.bytecode_len
= 0;
1361 * For the JUL domain, a filter is enforced except for the
1362 * disable all event. This is done to avoid having the event in
1363 * all sessions thus filtering by logger name.
1365 if (filter_expression
== NULL
&&
1366 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1367 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1368 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1373 * We have a filter, so we need to set up a variable-length
1374 * memory block from where to send the data.
1377 /* Parse filter expression */
1378 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1379 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1380 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1381 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1382 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1383 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1386 /* Setup JUL filter if needed. */
1387 agent_filter
= set_agent_filter(filter_expression
, ev
);
1388 if (!agent_filter
) {
1389 if (!filter_expression
) {
1391 * No JUL and no filter, just skip
1398 * With a JUL filter, the original filter has
1399 * been added to it thus replace the filter
1402 filter_expression
= agent_filter
;
1403 free_filter_expression
= 1;
1407 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1413 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1414 + lsm
.u
.disable
.expression_len
);
1416 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1420 /* Add filter expression. */
1421 if (lsm
.u
.disable
.expression_len
!= 0) {
1424 lsm
.u
.disable
.expression_len
);
1426 /* Add filter bytecode next. */
1427 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1429 + lsm
.u
.disable
.expression_len
,
1431 lsm
.u
.disable
.bytecode_len
);
1434 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1435 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1439 if (filter_expression
&& ctx
) {
1440 filter_bytecode_free(ctx
);
1441 filter_ir_free(ctx
);
1442 filter_parser_ctx_free(ctx
);
1445 if (free_filter_expression
) {
1447 * The filter expression has been replaced and must be freed as
1448 * it is not the original filter expression received as a
1451 free(filter_expression
);
1455 * Return directly to the caller and don't ask the sessiond since
1456 * something went wrong in the parsing of data above.
1461 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1466 * Disable event(s) of a channel and domain.
1467 * If no event name is specified, all events are disabled.
1468 * If no channel name is specified, the default 'channel0' is used.
1469 * Returns size of returned session payload data or a negative error code.
1471 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1472 const char *channel_name
)
1475 struct lttng_event ev
;
1477 memset(&ev
, 0, sizeof(ev
));
1479 ev
.type
= LTTNG_EVENT_ALL
;
1480 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1482 ret
= -LTTNG_ERR_INVALID
;
1486 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1491 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1493 struct lttng_channel
*channel
= NULL
;
1494 struct lttng_channel_extended
*extended
= NULL
;
1500 /* Validate domain. */
1501 switch (domain
->type
) {
1502 case LTTNG_DOMAIN_UST
:
1503 switch (domain
->buf_type
) {
1504 case LTTNG_BUFFER_PER_UID
:
1505 case LTTNG_BUFFER_PER_PID
:
1511 case LTTNG_DOMAIN_KERNEL
:
1512 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1520 channel
= zmalloc(sizeof(*channel
));
1525 extended
= zmalloc(sizeof(*extended
));
1530 channel
->attr
.extended
.ptr
= extended
;
1532 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1540 void lttng_channel_destroy(struct lttng_channel
*channel
)
1546 if (channel
->attr
.extended
.ptr
) {
1547 free(channel
->attr
.extended
.ptr
);
1553 * Enable channel per domain
1554 * Returns size of returned session payload data or a negative error code.
1556 int lttng_enable_channel(struct lttng_handle
*handle
,
1557 struct lttng_channel
*in_chan
)
1560 struct lttcomm_session_msg lsm
;
1561 size_t total_buffer_size_needed_per_cpu
= 0;
1563 /* NULL arguments are forbidden. No default values. */
1564 if (handle
== NULL
|| in_chan
== NULL
) {
1565 return -LTTNG_ERR_INVALID
;
1568 memset(&lsm
, 0, sizeof(lsm
));
1569 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1570 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1572 if (!in_chan
->attr
.extended
.ptr
) {
1573 struct lttng_channel
*channel
;
1574 struct lttng_channel_extended
*extended
;
1576 channel
= lttng_channel_create(&handle
->domain
);
1578 return -LTTNG_ERR_NOMEM
;
1582 * Create a new channel in order to use default extended
1585 extended
= (struct lttng_channel_extended
*)
1586 channel
->attr
.extended
.ptr
;
1587 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1588 lttng_channel_destroy(channel
);
1590 struct lttng_channel_extended
*extended
;
1592 extended
= (struct lttng_channel_extended
*)
1593 in_chan
->attr
.extended
.ptr
;
1594 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1598 * Verify that the amount of memory required to create the requested
1599 * buffer is available on the system at the moment.
1601 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1602 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1603 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1604 return -LTTNG_ERR_NOMEM
;
1607 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1608 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1610 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1611 sizeof(lsm
.session
.name
));
1613 ret
= -LTTNG_ERR_INVALID
;
1617 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1623 * All tracing will be stopped for registered events of the channel.
1624 * Returns size of returned session payload data or a negative error code.
1626 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1629 struct lttcomm_session_msg lsm
;
1631 /* Safety check. Both are mandatory. */
1632 if (handle
== NULL
|| name
== NULL
) {
1633 return -LTTNG_ERR_INVALID
;
1636 memset(&lsm
, 0, sizeof(lsm
));
1638 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1640 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1641 sizeof(lsm
.u
.disable
.channel_name
));
1643 ret
= -LTTNG_ERR_INVALID
;
1647 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1649 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1650 sizeof(lsm
.session
.name
));
1652 ret
= -LTTNG_ERR_INVALID
;
1656 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1662 * Lists all available tracepoints of domain.
1663 * Sets the contents of the events array.
1664 * Returns the number of lttng_event entries in events;
1665 * on error, returns a negative value.
1667 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1668 struct lttng_event
**events
)
1671 struct lttcomm_session_msg lsm
;
1673 if (handle
== NULL
) {
1674 return -LTTNG_ERR_INVALID
;
1677 memset(&lsm
, 0, sizeof(lsm
));
1678 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1679 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1681 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1686 return ret
/ sizeof(struct lttng_event
);
1690 * Lists all available tracepoint fields of domain.
1691 * Sets the contents of the event field array.
1692 * Returns the number of lttng_event_field entries in events;
1693 * on error, returns a negative value.
1695 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1696 struct lttng_event_field
**fields
)
1699 struct lttcomm_session_msg lsm
;
1701 if (handle
== NULL
) {
1702 return -LTTNG_ERR_INVALID
;
1705 memset(&lsm
, 0, sizeof(lsm
));
1706 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1707 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1709 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1714 return ret
/ sizeof(struct lttng_event_field
);
1718 * Lists all available kernel system calls. Allocates and sets the contents of
1721 * Returns the number of lttng_event entries in events; on error, returns a
1724 int lttng_list_syscalls(struct lttng_event
**events
)
1727 struct lttcomm_session_msg lsm
;
1730 return -LTTNG_ERR_INVALID
;
1733 memset(&lsm
, 0, sizeof(lsm
));
1734 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1735 /* Force kernel domain for system calls. */
1736 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1738 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1743 return ret
/ sizeof(struct lttng_event
);
1747 * Returns a human readable string describing
1748 * the error code (a negative value).
1750 const char *lttng_strerror(int code
)
1752 return error_get_str(code
);
1755 enum lttng_error_code
lttng_create_session_ext(
1756 struct lttng_session_descriptor
*session_descriptor
)
1758 enum lttng_error_code ret_code
;
1759 struct lttcomm_session_msg lsm
= {
1760 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1763 struct lttng_buffer_view reply_view
;
1765 bool sessiond_must_generate_ouput
;
1766 struct lttng_dynamic_buffer payload
;
1768 size_t descriptor_size
;
1769 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1771 lttng_dynamic_buffer_init(&payload
);
1772 if (!session_descriptor
) {
1773 ret_code
= LTTNG_ERR_INVALID
;
1777 sessiond_must_generate_ouput
=
1778 !lttng_session_descriptor_is_output_destination_initialized(
1779 session_descriptor
);
1780 if (sessiond_must_generate_ouput
) {
1781 const char *home_dir
= utils_get_home_dir();
1782 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1784 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1785 ret_code
= LTTNG_ERR_FATAL
;
1789 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1790 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1793 ret_code
= LTTNG_ERR_NOMEM
;
1798 descriptor_size
= payload
.size
;
1799 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1802 ret_code
= LTTNG_ERR_INVALID
;
1805 descriptor_size
= payload
.size
- descriptor_size
;
1806 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1808 /* Command returns a session descriptor on success. */
1809 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1810 payload
.size
, &reply
);
1811 if (reply_ret
< 0) {
1812 ret_code
= -reply_ret
;
1814 } else if (reply_ret
== 0) {
1815 /* Socket unexpectedly closed by the session daemon. */
1816 ret_code
= LTTNG_ERR_FATAL
;
1820 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1821 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1824 ret_code
= LTTNG_ERR_FATAL
;
1827 ret_code
= LTTNG_OK
;
1828 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1831 lttng_dynamic_buffer_reset(&payload
);
1832 lttng_session_descriptor_destroy(descriptor_reply
);
1837 * Create a new session using name and url for destination.
1839 * Return 0 on success else a negative LTTng error code.
1841 int lttng_create_session(const char *name
, const char *url
)
1845 struct lttng_uri
*uris
= NULL
;
1846 struct lttng_session_descriptor
*descriptor
= NULL
;
1847 enum lttng_error_code ret_code
;
1850 ret
= -LTTNG_ERR_INVALID
;
1854 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1856 ret
= -LTTNG_ERR_INVALID
;
1861 descriptor
= lttng_session_descriptor_create(name
);
1864 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1865 ret
= -LTTNG_ERR_INVALID
;
1868 descriptor
= lttng_session_descriptor_local_create(name
,
1872 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1876 ret
= -LTTNG_ERR_INVALID
;
1880 ret
= -LTTNG_ERR_INVALID
;
1883 ret_code
= lttng_create_session_ext(descriptor
);
1884 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1886 lttng_session_descriptor_destroy(descriptor
);
1892 * Create a session exclusively used for snapshot.
1894 * Return 0 on success else a negative LTTng error code.
1896 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1899 enum lttng_error_code ret_code
;
1901 struct lttng_uri
*uris
= NULL
;
1902 struct lttng_session_descriptor
*descriptor
= NULL
;
1905 ret
= -LTTNG_ERR_INVALID
;
1909 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1911 ret
= -LTTNG_ERR_INVALID
;
1915 * If the user does not specify a custom subdir, use the session name.
1917 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1918 strlen(uris
[0].subdir
) == 0) {
1919 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1922 PERROR("Failed to set session name as network destination sub-directory");
1923 ret
= -LTTNG_ERR_FATAL
;
1925 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1926 /* Truncated output. */
1927 ret
= -LTTNG_ERR_INVALID
;
1934 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1937 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1938 ret
= -LTTNG_ERR_INVALID
;
1941 descriptor
= lttng_session_descriptor_snapshot_local_create(
1946 descriptor
= lttng_session_descriptor_snapshot_network_create(
1952 ret
= -LTTNG_ERR_INVALID
;
1956 ret
= -LTTNG_ERR_INVALID
;
1959 ret_code
= lttng_create_session_ext(descriptor
);
1960 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1962 lttng_session_descriptor_destroy(descriptor
);
1968 * Create a session exclusively used for live.
1970 * Return 0 on success else a negative LTTng error code.
1972 int lttng_create_session_live(const char *name
, const char *url
,
1973 unsigned int timer_interval
)
1976 enum lttng_error_code ret_code
;
1977 struct lttng_session_descriptor
*descriptor
= NULL
;
1980 ret
= -LTTNG_ERR_INVALID
;
1985 descriptor
= lttng_session_descriptor_live_network_create(
1986 name
, url
, NULL
, timer_interval
);
1988 descriptor
= lttng_session_descriptor_live_create(
1989 name
, timer_interval
);
1992 ret
= -LTTNG_ERR_INVALID
;
1995 ret_code
= lttng_create_session_ext(descriptor
);
1996 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1998 lttng_session_descriptor_destroy(descriptor
);
2003 * Stop the session and wait for the data before destroying it
2005 * Return 0 on success else a negative LTTng error code.
2007 int lttng_destroy_session(const char *session_name
)
2010 enum lttng_error_code ret_code
;
2011 enum lttng_destruction_handle_status status
;
2012 struct lttng_destruction_handle
*handle
= NULL
;
2015 * Stop the tracing and wait for the data to be
2018 ret
= _lttng_stop_tracing(session_name
, 1);
2019 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2023 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2024 if (ret_code
!= LTTNG_OK
) {
2025 ret
= (int) -ret_code
;
2030 /* Block until the completion of the destruction of the session. */
2031 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2032 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2033 ret
= -LTTNG_ERR_UNK
;
2037 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2038 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2039 ret
= -LTTNG_ERR_UNK
;
2042 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2044 lttng_destruction_handle_destroy(handle
);
2049 * Destroy the session without waiting for the data.
2051 int lttng_destroy_session_no_wait(const char *session_name
)
2053 enum lttng_error_code ret_code
;
2055 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2056 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2060 * Ask the session daemon for all available sessions.
2061 * Sets the contents of the sessions array.
2062 * Returns the number of lttng_session entries in sessions;
2063 * on error, returns a negative value.
2065 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2068 struct lttcomm_session_msg lsm
;
2069 const size_t session_size
= sizeof(struct lttng_session
) +
2070 sizeof(struct lttng_session_extended
);
2071 size_t session_count
, i
;
2072 struct lttng_session_extended
*sessions_extended_begin
;
2073 struct lttng_session
*sessions
= NULL
;
2075 memset(&lsm
, 0, sizeof(lsm
));
2076 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2078 * Initialize out_sessions to NULL so it is initialized when
2079 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2080 * be subsequently freed.
2082 *out_sessions
= NULL
;
2083 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2088 ret
= -LTTNG_ERR_FATAL
;
2092 if (ret
% session_size
) {
2093 ret
= -LTTNG_ERR_UNK
;
2097 session_count
= (size_t) ret
/ session_size
;
2098 sessions_extended_begin
= (struct lttng_session_extended
*)
2099 (&sessions
[session_count
]);
2101 /* Set extended session info pointers. */
2102 for (i
= 0; i
< session_count
; i
++) {
2103 struct lttng_session
*session
= &sessions
[i
];
2104 struct lttng_session_extended
*extended
=
2105 &(sessions_extended_begin
[i
]);
2107 session
->extended
.ptr
= extended
;
2110 ret
= (int) session_count
;
2111 *out_sessions
= sessions
;
2116 enum lttng_error_code
lttng_session_get_creation_time(
2117 const struct lttng_session
*session
, uint64_t *creation_time
)
2119 enum lttng_error_code ret
= LTTNG_OK
;
2120 struct lttng_session_extended
*extended
;
2122 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2123 ret
= LTTNG_ERR_INVALID
;
2127 extended
= session
->extended
.ptr
;
2128 if (!extended
->creation_time
.is_set
) {
2129 /* Not created on the session daemon yet. */
2130 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2133 *creation_time
= extended
->creation_time
.value
;
2138 int lttng_set_session_shm_path(const char *session_name
,
2139 const char *shm_path
)
2142 struct lttcomm_session_msg lsm
;
2144 if (session_name
== NULL
) {
2145 return -LTTNG_ERR_INVALID
;
2148 memset(&lsm
, 0, sizeof(lsm
));
2149 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2151 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2152 sizeof(lsm
.session
.name
));
2154 ret
= -LTTNG_ERR_INVALID
;
2158 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2159 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2161 ret
= -LTTNG_ERR_INVALID
;
2165 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2171 * Ask the session daemon for all available domains of a session.
2172 * Sets the contents of the domains array.
2173 * Returns the number of lttng_domain entries in domains;
2174 * on error, returns a negative value.
2176 int lttng_list_domains(const char *session_name
,
2177 struct lttng_domain
**domains
)
2180 struct lttcomm_session_msg lsm
;
2182 if (session_name
== NULL
) {
2183 ret
= -LTTNG_ERR_INVALID
;
2187 memset(&lsm
, 0, sizeof(lsm
));
2188 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2190 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2191 sizeof(lsm
.session
.name
));
2193 ret
= -LTTNG_ERR_INVALID
;
2197 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2202 return ret
/ sizeof(struct lttng_domain
);
2208 * Ask the session daemon for all available channels of a session.
2209 * Sets the contents of the channels array.
2210 * Returns the number of lttng_channel entries in channels;
2211 * on error, returns a negative value.
2213 int lttng_list_channels(struct lttng_handle
*handle
,
2214 struct lttng_channel
**channels
)
2217 size_t channel_count
, i
;
2218 const size_t channel_size
= sizeof(struct lttng_channel
) +
2219 sizeof(struct lttng_channel_extended
);
2220 struct lttcomm_session_msg lsm
;
2223 if (handle
== NULL
) {
2224 ret
= -LTTNG_ERR_INVALID
;
2228 memset(&lsm
, 0, sizeof(lsm
));
2229 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2230 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2231 sizeof(lsm
.session
.name
));
2233 ret
= -LTTNG_ERR_INVALID
;
2237 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2239 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2244 if (ret
% channel_size
) {
2245 ret
= -LTTNG_ERR_UNK
;
2250 channel_count
= (size_t) ret
/ channel_size
;
2252 /* Set extended info pointers */
2253 extended_at
= ((void *) *channels
) +
2254 channel_count
* sizeof(struct lttng_channel
);
2255 for (i
= 0; i
< channel_count
; i
++) {
2256 struct lttng_channel
*chan
= &(*channels
)[i
];
2258 chan
->attr
.extended
.ptr
= extended_at
;
2259 extended_at
+= sizeof(struct lttng_channel_extended
);
2262 ret
= (int) channel_count
;
2268 * Ask the session daemon for all available events of a session channel.
2269 * Sets the contents of the events array.
2270 * Returns the number of lttng_event entries in events;
2271 * on error, returns a negative value.
2273 int lttng_list_events(struct lttng_handle
*handle
,
2274 const char *channel_name
, struct lttng_event
**events
)
2277 struct lttcomm_session_msg lsm
;
2278 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2279 size_t cmd_header_len
;
2280 uint32_t nb_events
, i
;
2282 char *reception_buffer
= NULL
;
2283 struct lttng_dynamic_buffer listing
;
2286 /* Safety check. An handle and channel name are mandatory */
2287 if (handle
== NULL
|| channel_name
== NULL
) {
2288 return -LTTNG_ERR_INVALID
;
2291 memset(&lsm
, 0, sizeof(lsm
));
2292 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2293 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2294 sizeof(lsm
.session
.name
));
2296 ret
= -LTTNG_ERR_INVALID
;
2300 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2301 sizeof(lsm
.u
.list
.channel_name
));
2303 ret
= -LTTNG_ERR_INVALID
;
2307 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2309 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2310 (void **) &reception_buffer
, (void **) &cmd_header
,
2317 ret
= -LTTNG_ERR_UNK
;
2321 /* Set number of events and free command header */
2322 nb_events
= cmd_header
->nb_events
;
2323 if (nb_events
> INT_MAX
) {
2324 ret
= -LTTNG_ERR_OVERFLOW
;
2331 * The buffer that is returned must contain a "flat" version of
2332 * the events that are returned. In other words, all pointers
2333 * within an lttng_event must point to a location within the returned
2334 * buffer so that the user may free everything by simply calling free()
2335 * on the returned buffer. This is needed in order to maintain API
2338 * A first pass is performed to compute the size of the buffer that
2339 * must be allocated. A second pass is then performed to setup
2340 * the returned events so that their members always point within the
2343 * The layout of the returned buffer is as follows:
2344 * - struct lttng_event[nb_events],
2345 * - nb_events times the following:
2346 * - struct lttng_event_extended,
2347 * - flattened version of userspace_probe_location
2348 * - filter_expression
2350 * - padding to align to 64-bits
2352 comm_ext_at
= reception_buffer
+
2353 (nb_events
* sizeof(struct lttng_event
));
2354 storage_req
= nb_events
* sizeof(struct lttng_event
);
2356 for (i
= 0; i
< nb_events
; i
++) {
2357 struct lttcomm_event_extended_header
*ext_comm
=
2358 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2359 int probe_storage_req
= 0;
2361 comm_ext_at
+= sizeof(*ext_comm
);
2362 comm_ext_at
+= ext_comm
->filter_len
;
2364 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2366 if (ext_comm
->userspace_probe_location_len
) {
2367 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2368 struct lttng_buffer_view probe_location_view
;
2370 probe_location_view
= lttng_buffer_view_init(
2372 ext_comm
->userspace_probe_location_len
);
2375 * Create a temporary userspace probe location to
2376 * determine the size needed by a "flattened" version
2377 * of that same probe location.
2379 ret
= lttng_userspace_probe_location_create_from_buffer(
2380 &probe_location_view
, &probe_location
);
2382 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2386 ret
= lttng_userspace_probe_location_flatten(
2387 probe_location
, NULL
);
2388 lttng_userspace_probe_location_destroy(probe_location
);
2390 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2394 probe_storage_req
= ret
;
2395 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2398 storage_req
+= sizeof(struct lttng_event_extended
);
2399 storage_req
+= ext_comm
->filter_len
;
2400 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2401 /* Padding to ensure the flat probe is aligned. */
2402 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2403 storage_req
+= probe_storage_req
;
2406 lttng_dynamic_buffer_init(&listing
);
2408 * We must ensure that "listing" is never resized so as to preserve
2409 * the validity of the flattened objects.
2411 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2413 ret
= -LTTNG_ERR_NOMEM
;
2417 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2418 nb_events
* sizeof(struct lttng_event
));
2420 ret
= -LTTNG_ERR_NOMEM
;
2421 goto free_dynamic_buffer
;
2424 comm_ext_at
= reception_buffer
+
2425 (nb_events
* sizeof(struct lttng_event
));
2426 for (i
= 0; i
< nb_events
; i
++) {
2427 struct lttng_event
*event
= (struct lttng_event
*)
2428 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2429 struct lttcomm_event_extended_header
*ext_comm
=
2430 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2431 struct lttng_event_extended
*event_extended
=
2432 (struct lttng_event_extended
*)
2433 (listing
.data
+ listing
.size
);
2435 /* Insert struct lttng_event_extended. */
2436 ret
= lttng_dynamic_buffer_set_size(&listing
,
2437 listing
.size
+ sizeof(*event_extended
));
2439 ret
= -LTTNG_ERR_NOMEM
;
2440 goto free_dynamic_buffer
;
2442 event
->extended
.ptr
= event_extended
;
2444 comm_ext_at
+= sizeof(*ext_comm
);
2446 /* Insert filter expression. */
2447 if (ext_comm
->filter_len
) {
2448 event_extended
->filter_expression
= listing
.data
+
2450 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2451 ext_comm
->filter_len
);
2453 ret
= -LTTNG_ERR_NOMEM
;
2454 goto free_dynamic_buffer
;
2456 comm_ext_at
+= ext_comm
->filter_len
;
2459 /* Insert exclusions. */
2460 if (ext_comm
->nb_exclusions
) {
2461 event_extended
->exclusions
.count
=
2462 ext_comm
->nb_exclusions
;
2463 event_extended
->exclusions
.strings
=
2464 listing
.data
+ listing
.size
;
2466 ret
= lttng_dynamic_buffer_append(&listing
,
2468 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2470 ret
= -LTTNG_ERR_NOMEM
;
2471 goto free_dynamic_buffer
;
2473 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2476 /* Insert padding to align to 64-bits. */
2477 ret
= lttng_dynamic_buffer_set_size(&listing
,
2478 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2480 ret
= -LTTNG_ERR_NOMEM
;
2481 goto free_dynamic_buffer
;
2484 /* Insert flattened userspace probe location. */
2485 if (ext_comm
->userspace_probe_location_len
) {
2486 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2487 struct lttng_buffer_view probe_location_view
;
2489 probe_location_view
= lttng_buffer_view_init(
2491 ext_comm
->userspace_probe_location_len
);
2493 ret
= lttng_userspace_probe_location_create_from_buffer(
2494 &probe_location_view
, &probe_location
);
2496 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2497 goto free_dynamic_buffer
;
2500 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2501 (listing
.data
+ listing
.size
);
2502 ret
= lttng_userspace_probe_location_flatten(
2503 probe_location
, &listing
);
2504 lttng_userspace_probe_location_destroy(probe_location
);
2506 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2507 goto free_dynamic_buffer
;
2510 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2514 /* Don't reset listing buffer as we return its content. */
2515 *events
= (struct lttng_event
*) listing
.data
;
2516 lttng_dynamic_buffer_init(&listing
);
2517 ret
= (int) nb_events
;
2518 free_dynamic_buffer
:
2519 lttng_dynamic_buffer_reset(&listing
);
2522 free(reception_buffer
);
2527 * Sets the tracing_group variable with name.
2528 * This function allocates memory pointed to by tracing_group.
2529 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2531 int lttng_set_tracing_group(const char *name
)
2535 return -LTTNG_ERR_INVALID
;
2538 if (asprintf(&new_group
, "%s", name
) < 0) {
2539 return -LTTNG_ERR_FATAL
;
2542 free(tracing_group
);
2543 tracing_group
= new_group
;
2549 int lttng_calibrate(struct lttng_handle
*handle
,
2550 struct lttng_calibrate
*calibrate
)
2553 * This command was removed in LTTng 2.9.
2555 return -LTTNG_ERR_UND
;
2559 * Set default channel attributes.
2560 * If either or both of the arguments are null, attr content is zeroe'd.
2562 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2563 struct lttng_channel_attr
*attr
)
2565 struct lttng_channel_extended
*extended
;
2568 if (attr
== NULL
|| domain
== NULL
) {
2572 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2573 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2575 /* Same for all domains. */
2576 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2577 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2578 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2580 switch (domain
->type
) {
2581 case LTTNG_DOMAIN_KERNEL
:
2582 attr
->switch_timer_interval
=
2583 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2584 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2585 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2586 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2587 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2589 extended
->monitor_timer_interval
=
2590 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2591 extended
->blocking_timeout
=
2592 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2595 case LTTNG_DOMAIN_UST
:
2596 switch (domain
->buf_type
) {
2597 case LTTNG_BUFFER_PER_UID
:
2598 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2599 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2600 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2601 attr
->switch_timer_interval
=
2602 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2603 attr
->read_timer_interval
=
2604 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2606 extended
->monitor_timer_interval
=
2607 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2608 extended
->blocking_timeout
=
2609 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2612 case LTTNG_BUFFER_PER_PID
:
2614 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2615 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2616 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2617 attr
->switch_timer_interval
=
2618 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2619 attr
->read_timer_interval
=
2620 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2622 extended
->monitor_timer_interval
=
2623 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2624 extended
->blocking_timeout
=
2625 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2630 /* Default behavior: leave set to 0. */
2634 attr
->extended
.ptr
= extended
;
2637 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2638 uint64_t *discarded_events
)
2641 struct lttng_channel_extended
*chan_ext
;
2643 if (!channel
|| !discarded_events
) {
2644 ret
= -LTTNG_ERR_INVALID
;
2648 chan_ext
= channel
->attr
.extended
.ptr
;
2651 * This can happen since the lttng_channel structure is
2652 * used for other tasks where this pointer is never set.
2654 *discarded_events
= 0;
2658 *discarded_events
= chan_ext
->discarded_events
;
2663 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2664 uint64_t *lost_packets
)
2667 struct lttng_channel_extended
*chan_ext
;
2669 if (!channel
|| !lost_packets
) {
2670 ret
= -LTTNG_ERR_INVALID
;
2674 chan_ext
= channel
->attr
.extended
.ptr
;
2677 * This can happen since the lttng_channel structure is
2678 * used for other tasks where this pointer is never set.
2684 *lost_packets
= chan_ext
->lost_packets
;
2689 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2690 uint64_t *monitor_timer_interval
)
2694 if (!chan
|| !monitor_timer_interval
) {
2695 ret
= -LTTNG_ERR_INVALID
;
2699 if (!chan
->attr
.extended
.ptr
) {
2700 ret
= -LTTNG_ERR_INVALID
;
2704 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2705 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2710 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2711 uint64_t monitor_timer_interval
)
2715 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2716 ret
= -LTTNG_ERR_INVALID
;
2720 ((struct lttng_channel_extended
*)
2721 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2722 monitor_timer_interval
;
2727 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2728 int64_t *blocking_timeout
)
2732 if (!chan
|| !blocking_timeout
) {
2733 ret
= -LTTNG_ERR_INVALID
;
2737 if (!chan
->attr
.extended
.ptr
) {
2738 ret
= -LTTNG_ERR_INVALID
;
2742 *blocking_timeout
= ((struct lttng_channel_extended
*)
2743 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2748 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2749 int64_t blocking_timeout
)
2752 int64_t msec_timeout
;
2754 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2755 ret
= -LTTNG_ERR_INVALID
;
2759 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2760 ret
= -LTTNG_ERR_INVALID
;
2765 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2766 * us to accept a narrower range of values (msecs expressed as a signed
2769 msec_timeout
= blocking_timeout
/ 1000;
2770 if (msec_timeout
!= (int32_t) msec_timeout
) {
2771 ret
= -LTTNG_ERR_INVALID
;
2775 ((struct lttng_channel_extended
*)
2776 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2783 * Check if session daemon is alive.
2785 * Return 1 if alive or 0 if not.
2786 * On error returns a negative value.
2788 int lttng_session_daemon_alive(void)
2792 ret
= set_session_daemon_path();
2798 if (*sessiond_sock_path
== '\0') {
2800 * No socket path set. Weird error which means the constructor
2806 ret
= try_connect_sessiond(sessiond_sock_path
);
2817 * Set URL for a consumer for a session and domain.
2819 * Return 0 on success, else a negative value.
2821 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2822 const char *control_url
, const char *data_url
)
2826 struct lttcomm_session_msg lsm
;
2827 struct lttng_uri
*uris
= NULL
;
2829 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2830 ret
= -LTTNG_ERR_INVALID
;
2834 memset(&lsm
, 0, sizeof(lsm
));
2836 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2838 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2839 sizeof(lsm
.session
.name
));
2841 ret
= -LTTNG_ERR_INVALID
;
2845 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2847 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2849 ret
= -LTTNG_ERR_INVALID
;
2853 lsm
.u
.uri
.size
= size
;
2855 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2856 sizeof(struct lttng_uri
) * size
, NULL
);
2866 int lttng_enable_consumer(struct lttng_handle
*handle
);
2867 int lttng_enable_consumer(struct lttng_handle
*handle
)
2875 int lttng_disable_consumer(struct lttng_handle
*handle
);
2876 int lttng_disable_consumer(struct lttng_handle
*handle
)
2884 int _lttng_create_session_ext(const char *name
, const char *url
,
2885 const char *datetime
);
2886 int _lttng_create_session_ext(const char *name
, const char *url
,
2887 const char *datetime
)
2893 * For a given session name, this call checks if the data is ready to be read
2894 * or is still being extracted by the consumer(s) hence not ready to be used by
2897 int lttng_data_pending(const char *session_name
)
2900 struct lttcomm_session_msg lsm
;
2901 uint8_t *pending
= NULL
;
2903 if (session_name
== NULL
) {
2904 return -LTTNG_ERR_INVALID
;
2907 memset(&lsm
, 0, sizeof(lsm
));
2908 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2910 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2911 sizeof(lsm
.session
.name
));
2913 ret
= -LTTNG_ERR_INVALID
;
2917 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2920 } else if (ret
!= 1) {
2921 /* Unexpected payload size */
2922 ret
= -LTTNG_ERR_INVALID
;
2924 } else if (!pending
) {
2925 /* Internal error. */
2926 ret
= -LTTNG_ERR_UNK
;
2930 ret
= (int) *pending
;
2937 * Regenerate the metadata for a session.
2938 * Return 0 on success, a negative error code on error.
2940 int lttng_regenerate_metadata(const char *session_name
)
2943 struct lttcomm_session_msg lsm
;
2945 if (!session_name
) {
2946 ret
= -LTTNG_ERR_INVALID
;
2950 memset(&lsm
, 0, sizeof(lsm
));
2951 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2953 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2954 sizeof(lsm
.session
.name
));
2956 ret
= -LTTNG_ERR_INVALID
;
2960 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2971 * Deprecated, replaced by lttng_regenerate_metadata.
2973 int lttng_metadata_regenerate(const char *session_name
)
2975 return lttng_regenerate_metadata(session_name
);
2979 * Regenerate the statedump of a session.
2980 * Return 0 on success, a negative error code on error.
2982 int lttng_regenerate_statedump(const char *session_name
)
2985 struct lttcomm_session_msg lsm
;
2987 if (!session_name
) {
2988 ret
= -LTTNG_ERR_INVALID
;
2992 memset(&lsm
, 0, sizeof(lsm
));
2993 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2995 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2996 sizeof(lsm
.session
.name
));
2998 ret
= -LTTNG_ERR_INVALID
;
3002 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3012 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3015 struct lttcomm_session_msg lsm
;
3016 struct lttng_dynamic_buffer buffer
;
3018 lttng_dynamic_buffer_init(&buffer
);
3020 ret
= -LTTNG_ERR_INVALID
;
3024 if (!lttng_trigger_validate(trigger
)) {
3025 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3029 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3031 ret
= -LTTNG_ERR_UNK
;
3035 memset(&lsm
, 0, sizeof(lsm
));
3036 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3037 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3038 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3041 lttng_dynamic_buffer_reset(&buffer
);
3045 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3048 struct lttcomm_session_msg lsm
;
3049 struct lttng_dynamic_buffer buffer
;
3051 lttng_dynamic_buffer_init(&buffer
);
3053 ret
= -LTTNG_ERR_INVALID
;
3057 if (!lttng_trigger_validate(trigger
)) {
3058 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3062 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3064 ret
= -LTTNG_ERR_UNK
;
3068 memset(&lsm
, 0, sizeof(lsm
));
3069 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3070 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3071 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3074 lttng_dynamic_buffer_reset(&buffer
);
3081 static void __attribute__((constructor
)) init(void)
3083 /* Set default session group */
3084 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3090 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3092 free(tracing_group
);