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 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <common/common.h>
33 #include <common/compat/string.h>
34 #include <common/defaults.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/uri.h>
37 #include <common/utils.h>
38 #include <common/dynamic-buffer.h>
39 #include <lttng/lttng.h>
40 #include <lttng/health-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <lttng/endpoint.h>
43 #include <lttng/channel-internal.h>
44 #include <lttng/event-internal.h>
45 #include <lttng/userspace-probe-internal.h>
46 #include <lttng/session-internal.h>
47 #include <lttng/session-descriptor-internal.h>
48 #include <lttng/destruction-handle.h>
50 #include "filter/filter-ast.h"
51 #include "filter/filter-parser.h"
52 #include "filter/filter-bytecode.h"
53 #include "filter/memstream.h"
54 #include "lttng-ctl-helper.h"
57 static const int print_xml
= 1;
58 #define dbg_printf(fmt, args...) \
59 printf("[debug liblttng-ctl] " fmt, ## args)
61 static const int print_xml
= 0;
62 #define dbg_printf(fmt, args...) \
64 /* do nothing but check printf format */ \
66 printf("[debug liblttnctl] " fmt, ## args); \
71 /* Socket to session daemon for communication */
72 static int sessiond_socket
= -1;
73 static char sessiond_sock_path
[PATH_MAX
];
76 static char *tracing_group
;
82 * Those two variables are used by error.h to silent or control the verbosity of
83 * error message. They are global to the library so application linking with it
84 * are able to compile correctly and also control verbosity of the library.
87 int lttng_opt_verbose
;
91 * Copy string from src to dst and enforce null terminated byte.
94 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
97 strncpy(dst
, src
, len
);
98 /* Enforce the NULL terminated byte */
106 * Copy domain to lttcomm_session_msg domain.
108 * If domain is unknown, default domain will be the kernel.
111 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
112 struct lttng_domain
*src
)
116 case LTTNG_DOMAIN_KERNEL
:
117 case LTTNG_DOMAIN_UST
:
118 case LTTNG_DOMAIN_JUL
:
119 case LTTNG_DOMAIN_LOG4J
:
120 case LTTNG_DOMAIN_PYTHON
:
121 memcpy(dst
, src
, sizeof(struct lttng_domain
));
124 memset(dst
, 0, sizeof(struct lttng_domain
));
131 * Send lttcomm_session_msg to the session daemon.
133 * On success, returns the number of bytes sent (>=0)
134 * On error, returns -1
136 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
141 ret
= -LTTNG_ERR_NO_SESSIOND
;
145 DBG("LSM cmd type : %d", lsm
->cmd_type
);
147 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
148 sizeof(struct lttcomm_session_msg
));
150 ret
= -LTTNG_ERR_FATAL
;
158 * Send var len data to the session daemon.
160 * On success, returns the number of bytes sent (>=0)
161 * On error, returns -1
163 static int send_session_varlen(const void *data
, size_t len
)
168 ret
= -LTTNG_ERR_NO_SESSIOND
;
177 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
179 ret
= -LTTNG_ERR_FATAL
;
187 * Send file descriptors to the session daemon.
189 * On success, returns the number of bytes sent (>=0)
190 * On error, returns -1
192 static int send_session_fds(const int *fds
, size_t nb_fd
)
197 ret
= -LTTNG_ERR_NO_SESSIOND
;
201 if (!fds
|| !nb_fd
) {
206 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
208 ret
= -LTTNG_ERR_FATAL
;
216 * Receive data from the sessiond socket.
218 * On success, returns the number of bytes received (>=0)
219 * On error, returns -1 (recvmsg() error) or -ENOTCONN
221 static int recv_data_sessiond(void *buf
, size_t len
)
226 ret
= -LTTNG_ERR_NO_SESSIOND
;
230 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
232 ret
= -LTTNG_ERR_FATAL
;
240 * Check if we are in the specified group.
242 * If yes return 1, else return -1.
245 int lttng_check_tracing_group(void)
247 gid_t
*grp_list
, tracing_gid
;
248 int grp_list_size
, grp_id
, i
;
250 const char *grp_name
= tracing_group
;
252 /* Get GID of group 'tracing' */
253 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
254 /* If grp_tracing is NULL, the group does not exist. */
258 /* Get number of supplementary group IDs */
259 grp_list_size
= getgroups(0, NULL
);
260 if (grp_list_size
< 0) {
265 /* Alloc group list of the right size */
266 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
271 grp_id
= getgroups(grp_list_size
, grp_list
);
277 for (i
= 0; i
< grp_list_size
; i
++) {
278 if (grp_list
[i
] == tracing_gid
) {
291 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
295 size_t best_mem_info
;
296 size_t num_bytes_requested_total
;
299 * Get the number of CPU currently online to compute the amount of
300 * memory needed to create a buffer for every CPU.
302 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
307 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
310 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
311 * reliable estimate we can get but it is only exposed by the kernel
312 * since 3.14. (See Linux kernel commit:
313 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
315 ret
= utils_get_memory_available(&best_mem_info
);
321 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
322 * is a sanity check for obvious user error.
324 ret
= utils_get_memory_total(&best_mem_info
);
332 return best_mem_info
>= num_bytes_requested_total
;
336 * Try connect to session daemon with sock_path.
338 * Return 0 on success, else -1
340 static int try_connect_sessiond(const char *sock_path
)
344 /* If socket exist, we check if the daemon listens for connect. */
345 ret
= access(sock_path
, F_OK
);
351 ret
= lttcomm_connect_unix_sock(sock_path
);
357 ret
= lttcomm_close_unix_sock(ret
);
359 PERROR("lttcomm_close_unix_sock");
369 * Set sessiond socket path by putting it in the global sessiond_sock_path
372 * Returns 0 on success, negative value on failure (the sessiond socket path
373 * is somehow too long or ENOMEM).
375 static int set_session_daemon_path(void)
377 int in_tgroup
= 0; /* In tracing group. */
383 /* Are we in the tracing group ? */
384 in_tgroup
= lttng_check_tracing_group();
387 if ((uid
== 0) || in_tgroup
) {
388 lttng_ctl_copy_string(sessiond_sock_path
,
389 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
397 ret
= try_connect_sessiond(sessiond_sock_path
);
401 /* Global session daemon not available... */
403 /* ...or not in tracing group (and not root), default */
406 * With GNU C < 2.1, snprintf returns -1 if the target buffer
408 * With GNU C >= 2.1, snprintf returns the required size
409 * (excluding closing null)
411 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
412 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
413 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
425 * Connect to the LTTng session daemon.
427 * On success, return the socket's file descriptor. On error, return -1.
429 LTTNG_HIDDEN
int connect_sessiond(void)
433 ret
= set_session_daemon_path();
438 /* Connect to the sesssion daemon. */
439 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
450 static void reset_global_sessiond_connection_state(void)
452 sessiond_socket
= -1;
457 * Clean disconnect from the session daemon.
459 * On success, return 0. On error, return -1.
461 static int disconnect_sessiond(void)
466 ret
= lttcomm_close_unix_sock(sessiond_socket
);
467 reset_global_sessiond_connection_state();
473 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
481 ret
= -LTTNG_ERR_INVALID
;
491 ret
= recv_data_sessiond(buf
, len
);
497 ret
= -LTTNG_ERR_INVALID
;
501 /* Move ownership of command header buffer to user. */
506 /* No command header. */
522 * Ask the session daemon a specific command and put the data into buf.
523 * Takes extra var. len. data and file descriptors as input to send to the
526 * Return size of data (only payload, not header) or a negative error code.
529 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
530 const int *fds
, size_t nb_fd
, const void *vardata
,
531 size_t vardata_len
, void **user_payload_buf
,
532 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
536 struct lttcomm_lttng_msg llm
;
538 ret
= connect_sessiond();
540 ret
= -LTTNG_ERR_NO_SESSIOND
;
543 sessiond_socket
= ret
;
547 /* Send command to session daemon */
548 ret
= send_session_msg(lsm
);
550 /* Ret value is a valid lttng error code. */
553 /* Send var len data */
554 ret
= send_session_varlen(vardata
, vardata_len
);
556 /* Ret value is a valid lttng error code. */
561 ret
= send_session_fds(fds
, nb_fd
);
563 /* Ret value is a valid lttng error code. */
567 /* Get header from data transmission */
568 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
570 /* Ret value is a valid lttng error code. */
574 /* Check error code if OK */
575 if (llm
.ret_code
!= LTTNG_OK
) {
580 /* Get command header from data transmission */
581 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
582 user_cmd_header_buf
, user_cmd_header_len
);
587 /* Get payload from data transmission */
588 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
597 disconnect_sessiond();
602 * Create lttng handle and return pointer.
604 * The returned pointer will be NULL in case of malloc() error.
606 struct lttng_handle
*lttng_create_handle(const char *session_name
,
607 struct lttng_domain
*domain
)
609 struct lttng_handle
*handle
= NULL
;
611 handle
= zmalloc(sizeof(struct lttng_handle
));
612 if (handle
== NULL
) {
613 PERROR("malloc handle");
617 /* Copy session name */
618 lttng_ctl_copy_string(handle
->session_name
, session_name
,
619 sizeof(handle
->session_name
));
621 /* Copy lttng domain or leave initialized to 0. */
623 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
631 * Destroy handle by free(3) the pointer.
633 void lttng_destroy_handle(struct lttng_handle
*handle
)
639 * Register an outside consumer.
641 * Returns size of returned session payload data or a negative error code.
643 int lttng_register_consumer(struct lttng_handle
*handle
,
644 const char *socket_path
)
646 struct lttcomm_session_msg lsm
;
648 if (handle
== NULL
|| socket_path
== NULL
) {
649 return -LTTNG_ERR_INVALID
;
652 memset(&lsm
, 0, sizeof(lsm
));
653 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
654 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
655 sizeof(lsm
.session
.name
));
656 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
658 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
659 sizeof(lsm
.u
.reg
.path
));
661 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
665 * Start tracing for all traces of the session.
667 * Returns size of returned session payload data or a negative error code.
669 int lttng_start_tracing(const char *session_name
)
671 struct lttcomm_session_msg lsm
;
673 if (session_name
== NULL
) {
674 return -LTTNG_ERR_INVALID
;
677 memset(&lsm
, 0, sizeof(lsm
));
678 lsm
.cmd_type
= LTTNG_START_TRACE
;
680 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
681 sizeof(lsm
.session
.name
));
683 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
687 * Stop tracing for all traces of the session.
689 static int _lttng_stop_tracing(const char *session_name
, int wait
)
692 struct lttcomm_session_msg lsm
;
694 if (session_name
== NULL
) {
695 return -LTTNG_ERR_INVALID
;
698 memset(&lsm
, 0, sizeof(lsm
));
699 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
701 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
702 sizeof(lsm
.session
.name
));
704 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
705 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
713 /* Check for data availability */
715 data_ret
= lttng_data_pending(session_name
);
717 /* Return the data available call error. */
723 * Data sleep time before retrying (in usec). Don't sleep if the
724 * call returned value indicates availability.
727 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
729 } while (data_ret
!= 0);
737 * Stop tracing and wait for data availability.
739 int lttng_stop_tracing(const char *session_name
)
741 return _lttng_stop_tracing(session_name
, 1);
745 * Stop tracing but _don't_ wait for data availability.
747 int lttng_stop_tracing_no_wait(const char *session_name
)
749 return _lttng_stop_tracing(session_name
, 0);
753 * Add context to a channel.
755 * If the given channel is NULL, add the contexts to all channels.
756 * The event_name param is ignored.
758 * Returns the size of the returned payload data or a negative error code.
760 int lttng_add_context(struct lttng_handle
*handle
,
761 struct lttng_event_context
*ctx
, const char *event_name
,
762 const char *channel_name
)
767 struct lttcomm_session_msg lsm
;
769 /* Safety check. Both are mandatory. */
770 if (handle
== NULL
|| ctx
== NULL
) {
771 ret
= -LTTNG_ERR_INVALID
;
775 memset(&lsm
, 0, sizeof(lsm
));
776 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
778 /* If no channel name, send empty string. */
779 if (channel_name
== NULL
) {
780 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
781 sizeof(lsm
.u
.context
.channel_name
));
783 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
784 sizeof(lsm
.u
.context
.channel_name
));
787 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
788 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
789 sizeof(lsm
.session
.name
));
791 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
792 size_t provider_len
, ctx_len
;
793 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
794 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
796 if (!provider_name
|| !ctx_name
) {
797 ret
= -LTTNG_ERR_INVALID
;
801 provider_len
= strlen(provider_name
);
802 if (provider_len
== 0) {
803 ret
= -LTTNG_ERR_INVALID
;
806 lsm
.u
.context
.provider_name_len
= provider_len
;
808 ctx_len
= strlen(ctx_name
);
810 ret
= -LTTNG_ERR_INVALID
;
813 lsm
.u
.context
.context_name_len
= ctx_len
;
815 len
= provider_len
+ ctx_len
;
818 ret
= -LTTNG_ERR_NOMEM
;
822 memcpy(buf
, provider_name
, provider_len
);
823 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
825 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
827 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
829 * Don't leak application addresses to the sessiond.
830 * This is only necessary when ctx is for an app ctx otherwise
831 * the values inside the union (type & config) are overwritten.
833 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
834 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
837 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
844 * Enable event(s) for a channel.
846 * If no event name is specified, all events are enabled.
847 * If no channel name is specified, the default 'channel0' is used.
849 * Returns size of returned session payload data or a negative error code.
851 int lttng_enable_event(struct lttng_handle
*handle
,
852 struct lttng_event
*ev
, const char *channel_name
)
854 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
859 * Create or enable an event with a filter expression.
861 * Return negative error value on error.
862 * Return size of returned session payload data if OK.
864 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
865 struct lttng_event
*event
, const char *channel_name
,
866 const char *filter_expression
)
868 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
869 filter_expression
, 0, NULL
);
873 * Depending on the event, return a newly allocated agent filter expression or
874 * NULL if not applicable.
876 * An event with NO loglevel and the name is * will return NULL.
878 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
881 char *agent_filter
= NULL
;
885 /* Don't add filter for the '*' event. */
886 if (strcmp(ev
->name
, "*") != 0) {
888 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
891 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
899 /* Add loglevel filtering if any for the JUL domain. */
900 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
903 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
909 if (filter
|| agent_filter
) {
912 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
913 agent_filter
? agent_filter
: filter
, op
,
918 agent_filter
= new_filter
;
920 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
936 * Generate the filter bytecode from a given filter expression string. Put the
937 * newly allocated parser context in ctxp and populate the lsm object with the
940 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
942 static int generate_filter(char *filter_expression
,
943 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
946 struct filter_parser_ctx
*ctx
= NULL
;
949 assert(filter_expression
);
954 * Casting const to non-const, as the underlying function will use it in
957 fmem
= lttng_fmemopen((void *) filter_expression
,
958 strlen(filter_expression
), "r");
960 fprintf(stderr
, "Error opening memory as stream\n");
961 ret
= -LTTNG_ERR_FILTER_NOMEM
;
964 ctx
= filter_parser_ctx_alloc(fmem
);
966 fprintf(stderr
, "Error allocating parser\n");
967 ret
= -LTTNG_ERR_FILTER_NOMEM
;
968 goto filter_alloc_error
;
970 ret
= filter_parser_ctx_append_ast(ctx
);
972 fprintf(stderr
, "Parse error\n");
973 ret
= -LTTNG_ERR_FILTER_INVAL
;
977 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
980 fprintf(stderr
, "XML print error\n");
981 ret
= -LTTNG_ERR_FILTER_INVAL
;
986 dbg_printf("Generating IR... ");
988 ret
= filter_visitor_ir_generate(ctx
);
990 fprintf(stderr
, "Generate IR error\n");
991 ret
= -LTTNG_ERR_FILTER_INVAL
;
994 dbg_printf("done\n");
996 dbg_printf("Validating IR... ");
998 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1000 ret
= -LTTNG_ERR_FILTER_INVAL
;
1004 /* Normalize globbing patterns in the expression. */
1005 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1007 ret
= -LTTNG_ERR_FILTER_INVAL
;
1011 /* Validate strings used as literals in the expression. */
1012 ret
= filter_visitor_ir_validate_string(ctx
);
1014 ret
= -LTTNG_ERR_FILTER_INVAL
;
1018 /* Validate globbing patterns in the expression. */
1019 ret
= filter_visitor_ir_validate_globbing(ctx
);
1021 ret
= -LTTNG_ERR_FILTER_INVAL
;
1025 dbg_printf("done\n");
1027 dbg_printf("Generating bytecode... ");
1029 ret
= filter_visitor_bytecode_generate(ctx
);
1031 fprintf(stderr
, "Generate bytecode error\n");
1032 ret
= -LTTNG_ERR_FILTER_INVAL
;
1035 dbg_printf("done\n");
1036 dbg_printf("Size of bytecode generated: %u bytes.\n",
1037 bytecode_get_len(&ctx
->bytecode
->b
));
1039 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1040 + bytecode_get_len(&ctx
->bytecode
->b
);
1041 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1043 /* No need to keep the memory stream. */
1044 if (fclose(fmem
) != 0) {
1052 filter_ir_free(ctx
);
1053 filter_parser_ctx_free(ctx
);
1055 if (fclose(fmem
) != 0) {
1063 * Enable event(s) for a channel, possibly with exclusions and a filter.
1064 * If no event name is specified, all events are enabled.
1065 * If no channel name is specified, the default name is used.
1066 * If filter expression is not NULL, the filter is set for the event.
1067 * If exclusion count is not zero, the exclusions are set for the event.
1068 * Returns size of returned session payload data or a negative error code.
1070 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1071 struct lttng_event
*ev
, const char *channel_name
,
1072 const char *original_filter_expression
,
1073 int exclusion_count
, char **exclusion_list
)
1075 struct lttcomm_session_msg lsm
;
1076 struct lttng_dynamic_buffer send_buffer
;
1077 int ret
= 0, i
, fd_to_send
= -1;
1078 bool send_fd
= false;
1079 unsigned int free_filter_expression
= 0;
1080 struct filter_parser_ctx
*ctx
= NULL
;
1083 * We have either a filter or some exclusions, so we need to set up
1084 * a variable-length memory block from where to send the data.
1086 lttng_dynamic_buffer_init(&send_buffer
);
1089 * Cast as non-const since we may replace the filter expression
1090 * by a dynamically allocated string. Otherwise, the original
1091 * string is not modified.
1093 char *filter_expression
= (char *) original_filter_expression
;
1095 if (handle
== NULL
|| ev
== NULL
) {
1096 ret
= -LTTNG_ERR_INVALID
;
1101 * Empty filter string will always be rejected by the parser
1102 * anyway, so treat this corner-case early to eliminate
1103 * lttng_fmemopen error for 0-byte allocation.
1105 if (filter_expression
&& filter_expression
[0] == '\0') {
1106 ret
= -LTTNG_ERR_INVALID
;
1110 memset(&lsm
, 0, sizeof(lsm
));
1112 /* If no channel name, send empty string. */
1113 if (channel_name
== NULL
) {
1114 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1115 sizeof(lsm
.u
.enable
.channel_name
));
1117 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1118 sizeof(lsm
.u
.enable
.channel_name
));
1121 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1122 if (ev
->name
[0] == '\0') {
1123 /* Enable all events */
1124 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1127 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1128 /* FIXME: copying non-packed struct to packed struct. */
1129 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1131 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1132 sizeof(lsm
.session
.name
));
1133 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1134 lsm
.u
.enable
.bytecode_len
= 0;
1136 /* Parse filter expression. */
1137 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1138 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1139 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1140 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1141 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1142 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1145 /* Setup JUL filter if needed. */
1146 agent_filter
= set_agent_filter(filter_expression
, ev
);
1147 if (!agent_filter
) {
1148 if (!filter_expression
) {
1150 * No JUL and no filter, just skip
1157 * With an agent filter, the original filter has
1158 * been added to it thus replace the filter
1161 filter_expression
= agent_filter
;
1162 free_filter_expression
= 1;
1166 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1172 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1173 lsm
.u
.enable
.bytecode_len
1174 + lsm
.u
.enable
.expression_len
1175 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1177 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1181 /* Put exclusion names first in the data. */
1182 for (i
= 0; i
< exclusion_count
; i
++) {
1183 size_t exclusion_len
;
1185 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1186 LTTNG_SYMBOL_NAME_LEN
);
1187 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1188 /* Exclusion is not NULL-terminated. */
1189 ret
= -LTTNG_ERR_INVALID
;
1193 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1194 *(exclusion_list
+ i
),
1195 LTTNG_SYMBOL_NAME_LEN
);
1201 /* Add filter expression next. */
1202 if (filter_expression
) {
1203 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1204 filter_expression
, lsm
.u
.enable
.expression_len
);
1209 /* Add filter bytecode next. */
1210 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1211 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1212 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1217 if (ev
->extended
.ptr
) {
1218 struct lttng_event_extended
*ev_ext
=
1219 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1221 if (ev_ext
->probe_location
) {
1223 * lttng_userspace_probe_location_serialize returns the
1224 * number of bytes that was appended to the buffer.
1226 ret
= lttng_userspace_probe_location_serialize(
1227 ev_ext
->probe_location
, &send_buffer
,
1233 send_fd
= fd_to_send
>= 0;
1235 * Set the size of the userspace probe location element
1236 * of the buffer so that the receiving side knows where
1239 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1243 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1244 send_fd
? &fd_to_send
: NULL
,
1246 send_buffer
.size
? send_buffer
.data
: NULL
,
1247 send_buffer
.size
, NULL
, NULL
, 0);
1250 if (filter_expression
&& ctx
) {
1251 filter_bytecode_free(ctx
);
1252 filter_ir_free(ctx
);
1253 filter_parser_ctx_free(ctx
);
1256 if (free_filter_expression
) {
1258 * The filter expression has been replaced and must be freed as
1259 * it is not the original filter expression received as a
1262 free(filter_expression
);
1266 * Return directly to the caller and don't ask the sessiond since
1267 * something went wrong in the parsing of data above.
1269 lttng_dynamic_buffer_reset(&send_buffer
);
1273 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1277 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1278 struct lttng_event
*ev
, const char *channel_name
,
1279 const char *original_filter_expression
)
1281 struct lttcomm_session_msg lsm
;
1284 unsigned int free_filter_expression
= 0;
1285 struct filter_parser_ctx
*ctx
= NULL
;
1287 * Cast as non-const since we may replace the filter expression
1288 * by a dynamically allocated string. Otherwise, the original
1289 * string is not modified.
1291 char *filter_expression
= (char *) original_filter_expression
;
1293 if (handle
== NULL
|| ev
== NULL
) {
1294 ret
= -LTTNG_ERR_INVALID
;
1299 * Empty filter string will always be rejected by the parser
1300 * anyway, so treat this corner-case early to eliminate
1301 * lttng_fmemopen error for 0-byte allocation.
1303 if (filter_expression
&& filter_expression
[0] == '\0') {
1304 ret
= -LTTNG_ERR_INVALID
;
1308 memset(&lsm
, 0, sizeof(lsm
));
1310 /* If no channel name, send empty string. */
1311 if (channel_name
== NULL
) {
1312 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1313 sizeof(lsm
.u
.disable
.channel_name
));
1315 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1316 sizeof(lsm
.u
.disable
.channel_name
));
1319 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1321 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1322 /* FIXME: copying non-packed struct to packed struct. */
1323 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1325 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1326 sizeof(lsm
.session
.name
));
1327 lsm
.u
.disable
.bytecode_len
= 0;
1330 * For the JUL domain, a filter is enforced except for the
1331 * disable all event. This is done to avoid having the event in
1332 * all sessions thus filtering by logger name.
1334 if (filter_expression
== NULL
&&
1335 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1336 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1337 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1342 * We have a filter, so we need to set up a variable-length
1343 * memory block from where to send the data.
1346 /* Parse filter expression */
1347 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1348 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1349 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1350 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1351 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1352 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1355 /* Setup JUL filter if needed. */
1356 agent_filter
= set_agent_filter(filter_expression
, ev
);
1357 if (!agent_filter
) {
1358 if (!filter_expression
) {
1360 * No JUL and no filter, just skip
1367 * With a JUL filter, the original filter has
1368 * been added to it thus replace the filter
1371 filter_expression
= agent_filter
;
1372 free_filter_expression
= 1;
1376 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1382 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1383 + lsm
.u
.disable
.expression_len
);
1385 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1389 /* Add filter expression. */
1390 if (lsm
.u
.disable
.expression_len
!= 0) {
1393 lsm
.u
.disable
.expression_len
);
1395 /* Add filter bytecode next. */
1396 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1398 + lsm
.u
.disable
.expression_len
,
1400 lsm
.u
.disable
.bytecode_len
);
1403 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1404 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1408 if (filter_expression
&& ctx
) {
1409 filter_bytecode_free(ctx
);
1410 filter_ir_free(ctx
);
1411 filter_parser_ctx_free(ctx
);
1414 if (free_filter_expression
) {
1416 * The filter expression has been replaced and must be freed as
1417 * it is not the original filter expression received as a
1420 free(filter_expression
);
1424 * Return directly to the caller and don't ask the sessiond since
1425 * something went wrong in the parsing of data above.
1430 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1435 * Disable event(s) of a channel and domain.
1436 * If no event name is specified, all events are disabled.
1437 * If no channel name is specified, the default 'channel0' is used.
1438 * Returns size of returned session payload data or a negative error code.
1440 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1441 const char *channel_name
)
1443 struct lttng_event ev
;
1445 memset(&ev
, 0, sizeof(ev
));
1447 ev
.type
= LTTNG_EVENT_ALL
;
1448 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1449 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1452 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1454 struct lttng_channel
*channel
= NULL
;
1455 struct lttng_channel_extended
*extended
= NULL
;
1461 /* Validate domain. */
1462 switch (domain
->type
) {
1463 case LTTNG_DOMAIN_UST
:
1464 switch (domain
->buf_type
) {
1465 case LTTNG_BUFFER_PER_UID
:
1466 case LTTNG_BUFFER_PER_PID
:
1472 case LTTNG_DOMAIN_KERNEL
:
1473 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1481 channel
= zmalloc(sizeof(*channel
));
1486 extended
= zmalloc(sizeof(*extended
));
1491 channel
->attr
.extended
.ptr
= extended
;
1493 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1501 void lttng_channel_destroy(struct lttng_channel
*channel
)
1507 if (channel
->attr
.extended
.ptr
) {
1508 free(channel
->attr
.extended
.ptr
);
1514 * Enable channel per domain
1515 * Returns size of returned session payload data or a negative error code.
1517 int lttng_enable_channel(struct lttng_handle
*handle
,
1518 struct lttng_channel
*in_chan
)
1520 struct lttcomm_session_msg lsm
;
1521 size_t total_buffer_size_needed_per_cpu
= 0;
1523 /* NULL arguments are forbidden. No default values. */
1524 if (handle
== NULL
|| in_chan
== NULL
) {
1525 return -LTTNG_ERR_INVALID
;
1528 memset(&lsm
, 0, sizeof(lsm
));
1529 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1530 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1532 if (!in_chan
->attr
.extended
.ptr
) {
1533 struct lttng_channel
*channel
;
1534 struct lttng_channel_extended
*extended
;
1536 channel
= lttng_channel_create(&handle
->domain
);
1538 return -LTTNG_ERR_NOMEM
;
1542 * Create a new channel in order to use default extended
1545 extended
= (struct lttng_channel_extended
*)
1546 channel
->attr
.extended
.ptr
;
1547 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1548 lttng_channel_destroy(channel
);
1550 struct lttng_channel_extended
*extended
;
1552 extended
= (struct lttng_channel_extended
*)
1553 in_chan
->attr
.extended
.ptr
;
1554 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1558 * Verify that the amount of memory required to create the requested
1559 * buffer is available on the system at the moment.
1561 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1562 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1563 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1564 return -LTTNG_ERR_NOMEM
;
1567 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1568 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1570 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1571 sizeof(lsm
.session
.name
));
1573 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1577 * All tracing will be stopped for registered events of the channel.
1578 * Returns size of returned session payload data or a negative error code.
1580 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1582 struct lttcomm_session_msg lsm
;
1584 /* Safety check. Both are mandatory. */
1585 if (handle
== NULL
|| name
== NULL
) {
1586 return -LTTNG_ERR_INVALID
;
1589 memset(&lsm
, 0, sizeof(lsm
));
1591 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1593 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1594 sizeof(lsm
.u
.disable
.channel_name
));
1596 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1598 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1599 sizeof(lsm
.session
.name
));
1601 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1605 * Add PID to session tracker.
1606 * Return 0 on success else a negative LTTng error code.
1608 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1610 struct lttcomm_session_msg lsm
;
1612 /* NULL arguments are forbidden. No default values. */
1613 if (handle
== NULL
) {
1614 return -LTTNG_ERR_INVALID
;
1617 memset(&lsm
, 0, sizeof(lsm
));
1619 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1620 lsm
.u
.pid_tracker
.pid
= pid
;
1622 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1624 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1625 sizeof(lsm
.session
.name
));
1627 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1631 * Remove PID from session tracker.
1632 * Return 0 on success else a negative LTTng error code.
1634 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1636 struct lttcomm_session_msg lsm
;
1638 /* NULL arguments are forbidden. No default values. */
1639 if (handle
== NULL
) {
1640 return -LTTNG_ERR_INVALID
;
1643 memset(&lsm
, 0, sizeof(lsm
));
1645 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1646 lsm
.u
.pid_tracker
.pid
= pid
;
1648 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1650 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1651 sizeof(lsm
.session
.name
));
1653 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1657 * Lists all available tracepoints of domain.
1658 * Sets the contents of the events array.
1659 * Returns the number of lttng_event entries in events;
1660 * on error, returns a negative value.
1662 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1663 struct lttng_event
**events
)
1666 struct lttcomm_session_msg lsm
;
1668 if (handle
== NULL
) {
1669 return -LTTNG_ERR_INVALID
;
1672 memset(&lsm
, 0, sizeof(lsm
));
1673 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1674 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1676 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1681 return ret
/ sizeof(struct lttng_event
);
1685 * Lists all available tracepoint fields of domain.
1686 * Sets the contents of the event field array.
1687 * Returns the number of lttng_event_field entries in events;
1688 * on error, returns a negative value.
1690 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1691 struct lttng_event_field
**fields
)
1694 struct lttcomm_session_msg lsm
;
1696 if (handle
== NULL
) {
1697 return -LTTNG_ERR_INVALID
;
1700 memset(&lsm
, 0, sizeof(lsm
));
1701 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1702 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1704 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1709 return ret
/ sizeof(struct lttng_event_field
);
1713 * Lists all available kernel system calls. Allocates and sets the contents of
1716 * Returns the number of lttng_event entries in events; on error, returns a
1719 int lttng_list_syscalls(struct lttng_event
**events
)
1722 struct lttcomm_session_msg lsm
;
1725 return -LTTNG_ERR_INVALID
;
1728 memset(&lsm
, 0, sizeof(lsm
));
1729 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1730 /* Force kernel domain for system calls. */
1731 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1733 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1738 return ret
/ sizeof(struct lttng_event
);
1742 * Returns a human readable string describing
1743 * the error code (a negative value).
1745 const char *lttng_strerror(int code
)
1747 return error_get_str(code
);
1750 enum lttng_error_code
lttng_create_session_ext(
1751 struct lttng_session_descriptor
*session_descriptor
)
1753 enum lttng_error_code ret_code
;
1754 struct lttcomm_session_msg lsm
= {
1755 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1758 struct lttng_buffer_view reply_view
;
1760 bool sessiond_must_generate_ouput
;
1761 struct lttng_dynamic_buffer payload
;
1763 size_t descriptor_size
;
1764 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1766 lttng_dynamic_buffer_init(&payload
);
1767 if (!session_descriptor
) {
1768 ret_code
= LTTNG_ERR_INVALID
;
1772 sessiond_must_generate_ouput
=
1773 !lttng_session_descriptor_is_output_destination_initialized(
1774 session_descriptor
);
1775 if (sessiond_must_generate_ouput
) {
1776 const char *home_dir
= utils_get_home_dir();
1777 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1779 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1780 ret_code
= LTTNG_ERR_FATAL
;
1784 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1785 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1788 ret_code
= LTTNG_ERR_NOMEM
;
1793 descriptor_size
= payload
.size
;
1794 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1797 ret_code
= LTTNG_ERR_INVALID
;
1800 descriptor_size
= payload
.size
- descriptor_size
;
1801 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1803 /* Command returns a session descriptor on success. */
1804 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1805 payload
.size
, &reply
);
1806 if (reply_ret
< 0) {
1807 ret_code
= -reply_ret
;
1809 } else if (reply_ret
== 0) {
1810 /* Socket unexpectedly closed by the session daemon. */
1811 ret_code
= LTTNG_ERR_FATAL
;
1815 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1816 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1819 ret_code
= LTTNG_ERR_FATAL
;
1822 ret_code
= LTTNG_OK
;
1823 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1826 lttng_dynamic_buffer_reset(&payload
);
1827 lttng_session_descriptor_destroy(descriptor_reply
);
1832 * Create a new session using name and url for destination.
1834 * Returns LTTNG_OK on success or a negative error code.
1836 int lttng_create_session(const char *name
, const char *url
)
1840 struct lttng_uri
*uris
= NULL
;
1841 struct lttng_session_descriptor
*descriptor
= NULL
;
1842 enum lttng_error_code ret_code
;
1845 ret
= -LTTNG_ERR_INVALID
;
1849 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1851 ret
= -LTTNG_ERR_INVALID
;
1856 descriptor
= lttng_session_descriptor_create(name
);
1859 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1860 ret
= -LTTNG_ERR_INVALID
;
1863 descriptor
= lttng_session_descriptor_local_create(name
,
1867 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1871 ret
= -LTTNG_ERR_INVALID
;
1875 ret
= -LTTNG_ERR_INVALID
;
1878 ret_code
= lttng_create_session_ext(descriptor
);
1879 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1881 lttng_session_descriptor_destroy(descriptor
);
1887 * Create a session exclusively used for snapshot.
1889 * Returns LTTNG_OK on success or a negative error code.
1891 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1894 enum lttng_error_code ret_code
;
1896 struct lttng_uri
*uris
= NULL
;
1897 struct lttng_session_descriptor
*descriptor
= NULL
;
1900 ret
= -LTTNG_ERR_INVALID
;
1904 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1906 ret
= -LTTNG_ERR_INVALID
;
1910 * If the user does not specify a custom subdir, use the session name.
1912 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1913 strlen(uris
[0].subdir
) == 0) {
1914 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1917 PERROR("Failed to set session name as network destination sub-directory");
1918 ret
= -LTTNG_ERR_FATAL
;
1920 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1921 /* Truncated output. */
1922 ret
= -LTTNG_ERR_INVALID
;
1929 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1932 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1933 ret
= -LTTNG_ERR_INVALID
;
1936 descriptor
= lttng_session_descriptor_snapshot_local_create(
1941 descriptor
= lttng_session_descriptor_snapshot_network_create(
1947 ret
= -LTTNG_ERR_INVALID
;
1951 ret
= -LTTNG_ERR_INVALID
;
1954 ret_code
= lttng_create_session_ext(descriptor
);
1955 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1957 lttng_session_descriptor_destroy(descriptor
);
1963 * Create a session exclusively used for live.
1965 * Returns LTTNG_OK on success or a negative error code.
1967 int lttng_create_session_live(const char *name
, const char *url
,
1968 unsigned int timer_interval
)
1971 enum lttng_error_code ret_code
;
1972 struct lttng_session_descriptor
*descriptor
= NULL
;
1975 ret
= -LTTNG_ERR_INVALID
;
1980 descriptor
= lttng_session_descriptor_live_network_create(
1981 name
, url
, NULL
, timer_interval
);
1983 descriptor
= lttng_session_descriptor_live_create(
1984 name
, timer_interval
);
1987 ret
= -LTTNG_ERR_INVALID
;
1990 ret_code
= lttng_create_session_ext(descriptor
);
1991 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1993 lttng_session_descriptor_destroy(descriptor
);
1998 * Stop the session and wait for the data before destroying it
2000 int lttng_destroy_session(const char *session_name
)
2003 enum lttng_error_code ret_code
;
2004 enum lttng_destruction_handle_status status
;
2005 struct lttng_destruction_handle
*handle
= NULL
;
2008 * Stop the tracing and wait for the data to be
2011 ret
= _lttng_stop_tracing(session_name
, 1);
2012 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2016 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2017 if (ret_code
!= LTTNG_OK
) {
2018 ret
= (int) -ret_code
;
2023 /* Block until the completion of the destruction of the session. */
2024 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2025 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2026 ret
= -LTTNG_ERR_UNK
;
2030 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2031 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2032 ret
= -LTTNG_ERR_UNK
;
2035 ret
= ret_code
== LTTNG_OK
? LTTNG_OK
: -ret_code
;
2037 lttng_destruction_handle_destroy(handle
);
2042 * Destroy the session without waiting for the data.
2044 int lttng_destroy_session_no_wait(const char *session_name
)
2046 enum lttng_error_code ret_code
;
2048 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2049 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2053 * Ask the session daemon for all available sessions.
2054 * Sets the contents of the sessions array.
2055 * Returns the number of lttng_session entries in sessions;
2056 * on error, returns a negative value.
2058 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2061 struct lttcomm_session_msg lsm
;
2062 const size_t session_size
= sizeof(struct lttng_session
) +
2063 sizeof(struct lttng_session_extended
);
2064 size_t session_count
, i
;
2065 struct lttng_session_extended
*sessions_extended_begin
;
2066 struct lttng_session
*sessions
= NULL
;
2068 memset(&lsm
, 0, sizeof(lsm
));
2069 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2070 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2075 ret
= -LTTNG_ERR_FATAL
;
2079 if (ret
% session_size
) {
2080 ret
= -LTTNG_ERR_UNK
;
2082 *out_sessions
= NULL
;
2085 session_count
= (size_t) ret
/ session_size
;
2086 sessions_extended_begin
= (struct lttng_session_extended
*)
2087 (&sessions
[session_count
]);
2089 /* Set extended session info pointers. */
2090 for (i
= 0; i
< session_count
; i
++) {
2091 struct lttng_session
*session
= &sessions
[i
];
2092 struct lttng_session_extended
*extended
=
2093 &(sessions_extended_begin
[i
]);
2095 session
->extended
.ptr
= extended
;
2098 ret
= (int) session_count
;
2099 *out_sessions
= sessions
;
2104 enum lttng_error_code
lttng_session_get_creation_time(
2105 const struct lttng_session
*session
, uint64_t *creation_time
)
2107 enum lttng_error_code ret
= LTTNG_OK
;
2108 struct lttng_session_extended
*extended
;
2110 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2111 ret
= LTTNG_ERR_INVALID
;
2115 extended
= session
->extended
.ptr
;
2116 if (!extended
->creation_time
.is_set
) {
2117 /* Not created on the session daemon yet. */
2118 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2121 *creation_time
= extended
->creation_time
.value
;
2126 int lttng_set_session_shm_path(const char *session_name
,
2127 const char *shm_path
)
2129 struct lttcomm_session_msg lsm
;
2131 if (session_name
== NULL
) {
2132 return -LTTNG_ERR_INVALID
;
2135 memset(&lsm
, 0, sizeof(lsm
));
2136 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2138 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2139 sizeof(lsm
.session
.name
));
2140 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2141 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2143 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2147 * Ask the session daemon for all available domains of a session.
2148 * Sets the contents of the domains array.
2149 * Returns the number of lttng_domain entries in domains;
2150 * on error, returns a negative value.
2152 int lttng_list_domains(const char *session_name
,
2153 struct lttng_domain
**domains
)
2156 struct lttcomm_session_msg lsm
;
2158 if (session_name
== NULL
) {
2159 return -LTTNG_ERR_INVALID
;
2162 memset(&lsm
, 0, sizeof(lsm
));
2163 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2165 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2166 sizeof(lsm
.session
.name
));
2168 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2173 return ret
/ sizeof(struct lttng_domain
);
2177 * Ask the session daemon for all available channels of a session.
2178 * Sets the contents of the channels array.
2179 * Returns the number of lttng_channel entries in channels;
2180 * on error, returns a negative value.
2182 int lttng_list_channels(struct lttng_handle
*handle
,
2183 struct lttng_channel
**channels
)
2186 size_t channel_count
, i
;
2187 const size_t channel_size
= sizeof(struct lttng_channel
) +
2188 sizeof(struct lttng_channel_extended
);
2189 struct lttcomm_session_msg lsm
;
2192 if (handle
== NULL
) {
2193 ret
= -LTTNG_ERR_INVALID
;
2197 memset(&lsm
, 0, sizeof(lsm
));
2198 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2199 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2200 sizeof(lsm
.session
.name
));
2202 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2204 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2209 if (ret
% channel_size
) {
2210 ret
= -LTTNG_ERR_UNK
;
2215 channel_count
= (size_t) ret
/ channel_size
;
2217 /* Set extended info pointers */
2218 extended_at
= ((void *) *channels
) +
2219 channel_count
* sizeof(struct lttng_channel
);
2220 for (i
= 0; i
< channel_count
; i
++) {
2221 struct lttng_channel
*chan
= &(*channels
)[i
];
2223 chan
->attr
.extended
.ptr
= extended_at
;
2224 extended_at
+= sizeof(struct lttng_channel_extended
);
2227 ret
= (int) channel_count
;
2233 * Ask the session daemon for all available events of a session channel.
2234 * Sets the contents of the events array.
2235 * Returns the number of lttng_event entries in events;
2236 * on error, returns a negative value.
2238 int lttng_list_events(struct lttng_handle
*handle
,
2239 const char *channel_name
, struct lttng_event
**events
)
2242 struct lttcomm_session_msg lsm
;
2243 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2244 size_t cmd_header_len
;
2245 uint32_t nb_events
, i
;
2247 char *reception_buffer
= NULL
;
2248 struct lttng_dynamic_buffer listing
;
2251 /* Safety check. An handle and channel name are mandatory */
2252 if (handle
== NULL
|| channel_name
== NULL
) {
2253 return -LTTNG_ERR_INVALID
;
2256 memset(&lsm
, 0, sizeof(lsm
));
2257 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2258 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2259 sizeof(lsm
.session
.name
));
2260 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2261 sizeof(lsm
.u
.list
.channel_name
));
2262 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2264 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2265 (void **) &reception_buffer
, (void **) &cmd_header
,
2272 ret
= -LTTNG_ERR_UNK
;
2276 /* Set number of events and free command header */
2277 nb_events
= cmd_header
->nb_events
;
2278 if (nb_events
> INT_MAX
) {
2286 * The buffer that is returned must contain a "flat" version of
2287 * the events that are returned. In other words, all pointers
2288 * within an lttng_event must point to a location within the returned
2289 * buffer so that the user may free everything by simply calling free()
2290 * on the returned buffer. This is needed in order to maintain API
2293 * A first pass is performed to compute the size of the buffer that
2294 * must be allocated. A second pass is then performed to setup
2295 * the returned events so that their members always point within the
2298 * The layout of the returned buffer is as follows:
2299 * - struct lttng_event[nb_events],
2300 * - nb_events times the following:
2301 * - struct lttng_event_extended,
2302 * - flattened version of userspace_probe_location
2303 * - filter_expression
2305 * - padding to align to 64-bits
2307 comm_ext_at
= reception_buffer
+
2308 (nb_events
* sizeof(struct lttng_event
));
2309 storage_req
= nb_events
* sizeof(struct lttng_event
);
2311 for (i
= 0; i
< nb_events
; i
++) {
2312 struct lttcomm_event_extended_header
*ext_comm
=
2313 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2314 int probe_storage_req
= 0;
2316 comm_ext_at
+= sizeof(*ext_comm
);
2317 comm_ext_at
+= ext_comm
->filter_len
;
2319 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2321 if (ext_comm
->userspace_probe_location_len
) {
2322 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2323 struct lttng_buffer_view probe_location_view
;
2325 probe_location_view
= lttng_buffer_view_init(
2327 ext_comm
->userspace_probe_location_len
);
2330 * Create a temporary userspace probe location to
2331 * determine the size needed by a "flattened" version
2332 * of that same probe location.
2334 ret
= lttng_userspace_probe_location_create_from_buffer(
2335 &probe_location_view
, &probe_location
);
2337 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2341 ret
= lttng_userspace_probe_location_flatten(
2342 probe_location
, NULL
);
2343 lttng_userspace_probe_location_destroy(probe_location
);
2345 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2349 probe_storage_req
= ret
;
2350 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2353 storage_req
+= sizeof(struct lttng_event_extended
);
2354 storage_req
+= ext_comm
->filter_len
;
2355 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2356 /* Padding to ensure the flat probe is aligned. */
2357 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2358 storage_req
+= probe_storage_req
;
2361 lttng_dynamic_buffer_init(&listing
);
2363 * We must ensure that "listing" is never resized so as to preserve
2364 * the validity of the flattened objects.
2366 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2368 ret
= -LTTNG_ERR_NOMEM
;
2372 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2373 nb_events
* sizeof(struct lttng_event
));
2375 ret
= -LTTNG_ERR_NOMEM
;
2376 goto free_dynamic_buffer
;
2379 comm_ext_at
= reception_buffer
+
2380 (nb_events
* sizeof(struct lttng_event
));
2381 for (i
= 0; i
< nb_events
; i
++) {
2382 struct lttng_event
*event
= (struct lttng_event
*)
2383 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2384 struct lttcomm_event_extended_header
*ext_comm
=
2385 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2386 struct lttng_event_extended
*event_extended
=
2387 (struct lttng_event_extended
*)
2388 (listing
.data
+ listing
.size
);
2390 /* Insert struct lttng_event_extended. */
2391 ret
= lttng_dynamic_buffer_set_size(&listing
,
2392 listing
.size
+ sizeof(*event_extended
));
2394 ret
= -LTTNG_ERR_NOMEM
;
2395 goto free_dynamic_buffer
;
2397 event
->extended
.ptr
= event_extended
;
2399 comm_ext_at
+= sizeof(*ext_comm
);
2401 /* Insert filter expression. */
2402 if (ext_comm
->filter_len
) {
2403 event_extended
->filter_expression
= listing
.data
+
2405 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2406 ext_comm
->filter_len
);
2408 ret
= -LTTNG_ERR_NOMEM
;
2409 goto free_dynamic_buffer
;
2411 comm_ext_at
+= ext_comm
->filter_len
;
2414 /* Insert exclusions. */
2415 if (ext_comm
->nb_exclusions
) {
2416 event_extended
->exclusions
.count
=
2417 ext_comm
->nb_exclusions
;
2418 event_extended
->exclusions
.strings
=
2419 listing
.data
+ listing
.size
;
2421 ret
= lttng_dynamic_buffer_append(&listing
,
2423 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2425 ret
= -LTTNG_ERR_NOMEM
;
2426 goto free_dynamic_buffer
;
2428 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2431 /* Insert padding to align to 64-bits. */
2432 ret
= lttng_dynamic_buffer_set_size(&listing
,
2433 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2435 ret
= -LTTNG_ERR_NOMEM
;
2436 goto free_dynamic_buffer
;
2439 /* Insert flattened userspace probe location. */
2440 if (ext_comm
->userspace_probe_location_len
) {
2441 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2442 struct lttng_buffer_view probe_location_view
;
2444 probe_location_view
= lttng_buffer_view_init(
2446 ext_comm
->userspace_probe_location_len
);
2448 ret
= lttng_userspace_probe_location_create_from_buffer(
2449 &probe_location_view
, &probe_location
);
2451 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2452 goto free_dynamic_buffer
;
2455 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2456 (listing
.data
+ listing
.size
);
2457 ret
= lttng_userspace_probe_location_flatten(
2458 probe_location
, &listing
);
2459 lttng_userspace_probe_location_destroy(probe_location
);
2461 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2462 goto free_dynamic_buffer
;
2465 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2469 /* Don't reset listing buffer as we return its content. */
2470 *events
= (struct lttng_event
*) listing
.data
;
2471 lttng_dynamic_buffer_init(&listing
);
2472 ret
= (int) nb_events
;
2473 free_dynamic_buffer
:
2474 lttng_dynamic_buffer_reset(&listing
);
2477 free(reception_buffer
);
2482 * Sets the tracing_group variable with name.
2483 * This function allocates memory pointed to by tracing_group.
2484 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2486 int lttng_set_tracing_group(const char *name
)
2489 return -LTTNG_ERR_INVALID
;
2492 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2493 return -LTTNG_ERR_FATAL
;
2499 int lttng_calibrate(struct lttng_handle
*handle
,
2500 struct lttng_calibrate
*calibrate
)
2503 * This command was removed in LTTng 2.9.
2505 return -LTTNG_ERR_UND
;
2509 * Set default channel attributes.
2510 * If either or both of the arguments are null, attr content is zeroe'd.
2512 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2513 struct lttng_channel_attr
*attr
)
2515 struct lttng_channel_extended
*extended
;
2518 if (attr
== NULL
|| domain
== NULL
) {
2522 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2523 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2525 /* Same for all domains. */
2526 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2527 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2528 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2530 switch (domain
->type
) {
2531 case LTTNG_DOMAIN_KERNEL
:
2532 attr
->switch_timer_interval
=
2533 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2534 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2535 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2536 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2537 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2539 extended
->monitor_timer_interval
=
2540 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2541 extended
->blocking_timeout
=
2542 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2545 case LTTNG_DOMAIN_UST
:
2546 switch (domain
->buf_type
) {
2547 case LTTNG_BUFFER_PER_UID
:
2548 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2549 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2550 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2551 attr
->switch_timer_interval
=
2552 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2553 attr
->read_timer_interval
=
2554 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2556 extended
->monitor_timer_interval
=
2557 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2558 extended
->blocking_timeout
=
2559 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2562 case LTTNG_BUFFER_PER_PID
:
2564 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2565 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2566 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2567 attr
->switch_timer_interval
=
2568 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2569 attr
->read_timer_interval
=
2570 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2572 extended
->monitor_timer_interval
=
2573 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2574 extended
->blocking_timeout
=
2575 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2580 /* Default behavior: leave set to 0. */
2584 attr
->extended
.ptr
= extended
;
2587 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2588 uint64_t *discarded_events
)
2591 struct lttng_channel_extended
*chan_ext
;
2593 if (!channel
|| !discarded_events
) {
2594 ret
= -LTTNG_ERR_INVALID
;
2598 chan_ext
= channel
->attr
.extended
.ptr
;
2601 * This can happen since the lttng_channel structure is
2602 * used for other tasks where this pointer is never set.
2604 *discarded_events
= 0;
2608 *discarded_events
= chan_ext
->discarded_events
;
2613 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2614 uint64_t *lost_packets
)
2617 struct lttng_channel_extended
*chan_ext
;
2619 if (!channel
|| !lost_packets
) {
2620 ret
= -LTTNG_ERR_INVALID
;
2624 chan_ext
= channel
->attr
.extended
.ptr
;
2627 * This can happen since the lttng_channel structure is
2628 * used for other tasks where this pointer is never set.
2634 *lost_packets
= chan_ext
->lost_packets
;
2639 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2640 uint64_t *monitor_timer_interval
)
2644 if (!chan
|| !monitor_timer_interval
) {
2645 ret
= -LTTNG_ERR_INVALID
;
2649 if (!chan
->attr
.extended
.ptr
) {
2650 ret
= -LTTNG_ERR_INVALID
;
2654 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2655 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2660 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2661 uint64_t monitor_timer_interval
)
2665 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2666 ret
= -LTTNG_ERR_INVALID
;
2670 ((struct lttng_channel_extended
*)
2671 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2672 monitor_timer_interval
;
2677 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2678 int64_t *blocking_timeout
)
2682 if (!chan
|| !blocking_timeout
) {
2683 ret
= -LTTNG_ERR_INVALID
;
2687 if (!chan
->attr
.extended
.ptr
) {
2688 ret
= -LTTNG_ERR_INVALID
;
2692 *blocking_timeout
= ((struct lttng_channel_extended
*)
2693 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2698 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2699 int64_t blocking_timeout
)
2702 int64_t msec_timeout
;
2704 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2705 ret
= -LTTNG_ERR_INVALID
;
2709 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2710 ret
= -LTTNG_ERR_INVALID
;
2715 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2716 * us to accept a narrower range of values (msecs expressed as a signed
2719 msec_timeout
= blocking_timeout
/ 1000;
2720 if (msec_timeout
!= (int32_t) msec_timeout
) {
2721 ret
= -LTTNG_ERR_INVALID
;
2725 ((struct lttng_channel_extended
*)
2726 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2733 * Check if session daemon is alive.
2735 * Return 1 if alive or 0 if not.
2736 * On error returns a negative value.
2738 int lttng_session_daemon_alive(void)
2742 ret
= set_session_daemon_path();
2748 if (*sessiond_sock_path
== '\0') {
2750 * No socket path set. Weird error which means the constructor
2756 ret
= try_connect_sessiond(sessiond_sock_path
);
2767 * Set URL for a consumer for a session and domain.
2769 * Return 0 on success, else a negative value.
2771 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2772 const char *control_url
, const char *data_url
)
2776 struct lttcomm_session_msg lsm
;
2777 struct lttng_uri
*uris
= NULL
;
2779 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2780 return -LTTNG_ERR_INVALID
;
2783 memset(&lsm
, 0, sizeof(lsm
));
2785 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2787 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2788 sizeof(lsm
.session
.name
));
2789 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2791 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2793 return -LTTNG_ERR_INVALID
;
2796 lsm
.u
.uri
.size
= size
;
2798 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2799 sizeof(struct lttng_uri
) * size
, NULL
);
2808 int lttng_enable_consumer(struct lttng_handle
*handle
)
2816 int lttng_disable_consumer(struct lttng_handle
*handle
)
2824 int _lttng_create_session_ext(const char *name
, const char *url
,
2825 const char *datetime
)
2831 * For a given session name, this call checks if the data is ready to be read
2832 * or is still being extracted by the consumer(s) hence not ready to be used by
2835 int lttng_data_pending(const char *session_name
)
2838 struct lttcomm_session_msg lsm
;
2839 uint8_t *pending
= NULL
;
2841 if (session_name
== NULL
) {
2842 return -LTTNG_ERR_INVALID
;
2845 memset(&lsm
, 0, sizeof(lsm
));
2846 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2848 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2849 sizeof(lsm
.session
.name
));
2851 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2854 } else if (ret
!= 1) {
2855 /* Unexpected payload size */
2856 ret
= -LTTNG_ERR_INVALID
;
2860 ret
= (int) *pending
;
2867 * List PIDs in the tracker.
2869 * enabled is set to whether the PID tracker is enabled.
2870 * pids is set to an allocated array of PIDs currently tracked. On
2871 * success, pids must be freed by the caller.
2872 * nr_pids is set to the number of entries contained by the pids array.
2874 * Returns 0 on success, else a negative LTTng error code.
2876 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2877 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2881 struct lttcomm_session_msg lsm
;
2883 int32_t *pids
= NULL
;
2885 if (handle
== NULL
) {
2886 return -LTTNG_ERR_INVALID
;
2889 memset(&lsm
, 0, sizeof(lsm
));
2890 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2891 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2892 sizeof(lsm
.session
.name
));
2893 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2895 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2899 nr_pids
= ret
/ sizeof(int32_t);
2900 if (nr_pids
> 0 && !pids
) {
2901 return -LTTNG_ERR_UNK
;
2903 if (nr_pids
== 1 && pids
[0] == -1) {
2909 *_enabled
= enabled
;
2911 *_nr_pids
= nr_pids
;
2916 * Regenerate the metadata for a session.
2917 * Return 0 on success, a negative error code on error.
2919 int lttng_regenerate_metadata(const char *session_name
)
2922 struct lttcomm_session_msg lsm
;
2924 if (!session_name
) {
2925 ret
= -LTTNG_ERR_INVALID
;
2929 memset(&lsm
, 0, sizeof(lsm
));
2930 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2932 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2933 sizeof(lsm
.session
.name
));
2935 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2946 * Deprecated, replaced by lttng_regenerate_metadata.
2948 int lttng_metadata_regenerate(const char *session_name
)
2950 return lttng_regenerate_metadata(session_name
);
2954 * Regenerate the statedump of a session.
2955 * Return 0 on success, a negative error code on error.
2957 int lttng_regenerate_statedump(const char *session_name
)
2960 struct lttcomm_session_msg lsm
;
2962 if (!session_name
) {
2963 ret
= -LTTNG_ERR_INVALID
;
2967 memset(&lsm
, 0, sizeof(lsm
));
2968 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2970 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2971 sizeof(lsm
.session
.name
));
2973 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2983 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2986 struct lttcomm_session_msg lsm
;
2987 struct lttng_dynamic_buffer buffer
;
2989 lttng_dynamic_buffer_init(&buffer
);
2991 ret
= -LTTNG_ERR_INVALID
;
2995 if (!lttng_trigger_validate(trigger
)) {
2996 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3000 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3002 ret
= -LTTNG_ERR_UNK
;
3006 memset(&lsm
, 0, sizeof(lsm
));
3007 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3008 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3009 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3012 lttng_dynamic_buffer_reset(&buffer
);
3016 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3019 struct lttcomm_session_msg lsm
;
3020 struct lttng_dynamic_buffer buffer
;
3022 lttng_dynamic_buffer_init(&buffer
);
3024 ret
= -LTTNG_ERR_INVALID
;
3028 if (!lttng_trigger_validate(trigger
)) {
3029 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3033 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3035 ret
= -LTTNG_ERR_UNK
;
3039 memset(&lsm
, 0, sizeof(lsm
));
3040 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3041 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3042 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3045 lttng_dynamic_buffer_reset(&buffer
);
3052 static void __attribute__((constructor
)) init(void)
3054 /* Set default session group */
3055 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3061 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3063 free(tracing_group
);