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/defaults.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/uri.h>
36 #include <common/utils.h>
37 #include <lttng/lttng.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/trigger/trigger-internal.h>
40 #include <lttng/endpoint.h>
41 #include <lttng/channel-internal.h>
43 #include "filter/filter-ast.h"
44 #include "filter/filter-parser.h"
45 #include "filter/filter-bytecode.h"
46 #include "filter/memstream.h"
47 #include "lttng-ctl-helper.h"
50 static const int print_xml
= 1;
51 #define dbg_printf(fmt, args...) \
52 printf("[debug liblttng-ctl] " fmt, ## args)
54 static const int print_xml
= 0;
55 #define dbg_printf(fmt, args...) \
57 /* do nothing but check printf format */ \
59 printf("[debug liblttnctl] " fmt, ## args); \
64 /* Socket to session daemon for communication */
65 static int sessiond_socket
;
66 static char sessiond_sock_path
[PATH_MAX
];
69 static char *tracing_group
;
75 * Those two variables are used by error.h to silent or control the verbosity of
76 * error message. They are global to the library so application linking with it
77 * are able to compile correctly and also control verbosity of the library.
80 int lttng_opt_verbose
;
84 * Copy string from src to dst and enforce null terminated byte.
87 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
90 strncpy(dst
, src
, len
);
91 /* Enforce the NULL terminated byte */
99 * Copy domain to lttcomm_session_msg domain.
101 * If domain is unknown, default domain will be the kernel.
104 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
105 struct lttng_domain
*src
)
109 case LTTNG_DOMAIN_KERNEL
:
110 case LTTNG_DOMAIN_UST
:
111 case LTTNG_DOMAIN_JUL
:
112 case LTTNG_DOMAIN_LOG4J
:
113 case LTTNG_DOMAIN_PYTHON
:
114 memcpy(dst
, src
, sizeof(struct lttng_domain
));
117 memset(dst
, 0, sizeof(struct lttng_domain
));
124 * Send lttcomm_session_msg to the session daemon.
126 * On success, returns the number of bytes sent (>=0)
127 * On error, returns -1
129 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
134 ret
= -LTTNG_ERR_NO_SESSIOND
;
138 DBG("LSM cmd type : %d", lsm
->cmd_type
);
140 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
141 sizeof(struct lttcomm_session_msg
));
143 ret
= -LTTNG_ERR_FATAL
;
151 * Send var len data to the session daemon.
153 * On success, returns the number of bytes sent (>=0)
154 * On error, returns -1
156 static int send_session_varlen(const void *data
, size_t len
)
161 ret
= -LTTNG_ERR_NO_SESSIOND
;
170 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
172 ret
= -LTTNG_ERR_FATAL
;
180 * Send file descriptors to the session daemon.
182 * On success, returns the number of bytes sent (>=0)
183 * On error, returns -1
185 static int send_session_fds(const int *fds
, size_t nb_fd
)
190 ret
= -LTTNG_ERR_NO_SESSIOND
;
194 if (!fds
|| !nb_fd
) {
199 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
201 ret
= -LTTNG_ERR_FATAL
;
209 * Receive data from the sessiond socket.
211 * On success, returns the number of bytes received (>=0)
212 * On error, returns -1 (recvmsg() error) or -ENOTCONN
214 static int recv_data_sessiond(void *buf
, size_t len
)
219 ret
= -LTTNG_ERR_NO_SESSIOND
;
223 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
225 ret
= -LTTNG_ERR_FATAL
;
233 * Check if we are in the specified group.
235 * If yes return 1, else return -1.
238 int lttng_check_tracing_group(void)
240 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
242 int grp_list_size
, grp_id
, i
;
244 const char *grp_name
= tracing_group
;
246 /* Get GID of group 'tracing' */
247 grp_tracing
= getgrnam(grp_name
);
249 /* If grp_tracing is NULL, the group does not exist. */
253 /* Get number of supplementary group IDs */
254 grp_list_size
= getgroups(0, NULL
);
255 if (grp_list_size
< 0) {
260 /* Alloc group list of the right size */
261 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
266 grp_id
= getgroups(grp_list_size
, grp_list
);
272 for (i
= 0; i
< grp_list_size
; i
++) {
273 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
287 * Try connect to session daemon with sock_path.
289 * Return 0 on success, else -1
291 static int try_connect_sessiond(const char *sock_path
)
295 /* If socket exist, we check if the daemon listens for connect. */
296 ret
= access(sock_path
, F_OK
);
302 ret
= lttcomm_connect_unix_sock(sock_path
);
308 ret
= lttcomm_close_unix_sock(ret
);
310 PERROR("lttcomm_close_unix_sock");
320 * Set sessiond socket path by putting it in the global sessiond_sock_path
323 * Returns 0 on success, negative value on failure (the sessiond socket path
324 * is somehow too long or ENOMEM).
326 static int set_session_daemon_path(void)
328 int in_tgroup
= 0; /* In tracing group. */
334 /* Are we in the tracing group ? */
335 in_tgroup
= lttng_check_tracing_group();
338 if ((uid
== 0) || in_tgroup
) {
339 lttng_ctl_copy_string(sessiond_sock_path
,
340 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
348 ret
= try_connect_sessiond(sessiond_sock_path
);
352 /* Global session daemon not available... */
354 /* ...or not in tracing group (and not root), default */
357 * With GNU C < 2.1, snprintf returns -1 if the target buffer
359 * With GNU C >= 2.1, snprintf returns the required size
360 * (excluding closing null)
362 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
363 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
364 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
376 * Connect to the LTTng session daemon.
378 * On success, return 0. On error, return -1.
380 static int connect_sessiond(void)
384 /* Don't try to connect if already connected. */
389 ret
= set_session_daemon_path();
394 /* Connect to the sesssion daemon. */
395 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
400 sessiond_socket
= ret
;
410 * Clean disconnect from the session daemon.
412 * On success, return 0. On error, return -1.
414 static int disconnect_sessiond(void)
419 ret
= lttcomm_close_unix_sock(sessiond_socket
);
427 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
435 ret
= -LTTNG_ERR_INVALID
;
445 ret
= recv_data_sessiond(buf
, len
);
451 ret
= -LTTNG_ERR_INVALID
;
455 /* Move ownership of command header buffer to user. */
460 /* No command header. */
476 * Ask the session daemon a specific command and put the data into buf.
477 * Takes extra var. len. data and file descriptors as input to send to the
480 * Return size of data (only payload, not header) or a negative error code.
483 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
484 const int *fds
, size_t nb_fd
, const void *vardata
,
485 size_t vardata_len
, void **user_payload_buf
,
486 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
490 struct lttcomm_lttng_msg llm
;
492 ret
= connect_sessiond();
494 ret
= -LTTNG_ERR_NO_SESSIOND
;
498 /* Send command to session daemon */
499 ret
= send_session_msg(lsm
);
501 /* Ret value is a valid lttng error code. */
504 /* Send var len data */
505 ret
= send_session_varlen(vardata
, vardata_len
);
507 /* Ret value is a valid lttng error code. */
512 ret
= send_session_fds(fds
, nb_fd
);
514 /* Ret value is a valid lttng error code. */
518 /* Get header from data transmission */
519 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
521 /* Ret value is a valid lttng error code. */
525 /* Check error code if OK */
526 if (llm
.ret_code
!= LTTNG_OK
) {
531 /* Get command header from data transmission */
532 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
533 user_cmd_header_buf
, user_cmd_header_len
);
538 /* Get payload from data transmission */
539 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
548 disconnect_sessiond();
553 * Create lttng handle and return pointer.
555 * The returned pointer will be NULL in case of malloc() error.
557 struct lttng_handle
*lttng_create_handle(const char *session_name
,
558 struct lttng_domain
*domain
)
560 struct lttng_handle
*handle
= NULL
;
562 handle
= zmalloc(sizeof(struct lttng_handle
));
563 if (handle
== NULL
) {
564 PERROR("malloc handle");
568 /* Copy session name */
569 lttng_ctl_copy_string(handle
->session_name
, session_name
,
570 sizeof(handle
->session_name
));
572 /* Copy lttng domain or leave initialized to 0. */
574 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
582 * Destroy handle by free(3) the pointer.
584 void lttng_destroy_handle(struct lttng_handle
*handle
)
590 * Register an outside consumer.
592 * Returns size of returned session payload data or a negative error code.
594 int lttng_register_consumer(struct lttng_handle
*handle
,
595 const char *socket_path
)
597 struct lttcomm_session_msg lsm
;
599 if (handle
== NULL
|| socket_path
== NULL
) {
600 return -LTTNG_ERR_INVALID
;
603 memset(&lsm
, 0, sizeof(lsm
));
604 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
605 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
606 sizeof(lsm
.session
.name
));
607 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
609 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
610 sizeof(lsm
.u
.reg
.path
));
612 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
616 * Start tracing for all traces of the session.
618 * Returns size of returned session payload data or a negative error code.
620 int lttng_start_tracing(const char *session_name
)
622 struct lttcomm_session_msg lsm
;
624 if (session_name
== NULL
) {
625 return -LTTNG_ERR_INVALID
;
628 memset(&lsm
, 0, sizeof(lsm
));
629 lsm
.cmd_type
= LTTNG_START_TRACE
;
631 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
632 sizeof(lsm
.session
.name
));
634 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
638 * Stop tracing for all traces of the session.
640 static int _lttng_stop_tracing(const char *session_name
, int wait
)
643 struct lttcomm_session_msg lsm
;
645 if (session_name
== NULL
) {
646 return -LTTNG_ERR_INVALID
;
649 memset(&lsm
, 0, sizeof(lsm
));
650 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
652 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
653 sizeof(lsm
.session
.name
));
655 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
656 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
664 /* Check for data availability */
666 data_ret
= lttng_data_pending(session_name
);
668 /* Return the data available call error. */
674 * Data sleep time before retrying (in usec). Don't sleep if the
675 * call returned value indicates availability.
678 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
680 } while (data_ret
!= 0);
688 * Stop tracing and wait for data availability.
690 int lttng_stop_tracing(const char *session_name
)
692 return _lttng_stop_tracing(session_name
, 1);
696 * Stop tracing but _don't_ wait for data availability.
698 int lttng_stop_tracing_no_wait(const char *session_name
)
700 return _lttng_stop_tracing(session_name
, 0);
704 * Add context to a channel.
706 * If the given channel is NULL, add the contexts to all channels.
707 * The event_name param is ignored.
709 * Returns the size of the returned payload data or a negative error code.
711 int lttng_add_context(struct lttng_handle
*handle
,
712 struct lttng_event_context
*ctx
, const char *event_name
,
713 const char *channel_name
)
718 struct lttcomm_session_msg lsm
;
720 /* Safety check. Both are mandatory. */
721 if (handle
== NULL
|| ctx
== NULL
) {
722 ret
= -LTTNG_ERR_INVALID
;
726 memset(&lsm
, 0, sizeof(lsm
));
727 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
729 /* If no channel name, send empty string. */
730 if (channel_name
== NULL
) {
731 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
732 sizeof(lsm
.u
.context
.channel_name
));
734 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
735 sizeof(lsm
.u
.context
.channel_name
));
738 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
739 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
740 sizeof(lsm
.session
.name
));
742 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
743 size_t provider_len
, ctx_len
;
744 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
745 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
747 if (!provider_name
|| !ctx_name
) {
748 ret
= -LTTNG_ERR_INVALID
;
752 provider_len
= strlen(provider_name
);
753 if (provider_len
== 0) {
754 ret
= -LTTNG_ERR_INVALID
;
757 lsm
.u
.context
.provider_name_len
= provider_len
;
759 ctx_len
= strlen(ctx_name
);
761 ret
= -LTTNG_ERR_INVALID
;
764 lsm
.u
.context
.context_name_len
= ctx_len
;
766 len
= provider_len
+ ctx_len
;
769 ret
= -LTTNG_ERR_NOMEM
;
773 memcpy(buf
, provider_name
, provider_len
);
774 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
776 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
778 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
780 * Don't leak application addresses to the sessiond.
781 * This is only necessary when ctx is for an app ctx otherwise
782 * the values inside the union (type & config) are overwritten.
784 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
785 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
788 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
795 * Enable event(s) for a channel.
797 * If no event name is specified, all events are enabled.
798 * If no channel name is specified, the default 'channel0' is used.
800 * Returns size of returned session payload data or a negative error code.
802 int lttng_enable_event(struct lttng_handle
*handle
,
803 struct lttng_event
*ev
, const char *channel_name
)
805 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
810 * Create or enable an event with a filter expression.
812 * Return negative error value on error.
813 * Return size of returned session payload data if OK.
815 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
816 struct lttng_event
*event
, const char *channel_name
,
817 const char *filter_expression
)
819 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
820 filter_expression
, 0, NULL
);
824 * Depending on the event, return a newly allocated agent filter expression or
825 * NULL if not applicable.
827 * An event with NO loglevel and the name is * will return NULL.
829 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
832 char *agent_filter
= NULL
;
836 /* Don't add filter for the '*' event. */
837 if (strcmp(ev
->name
, "*") != 0) {
839 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
842 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
850 /* Add loglevel filtering if any for the JUL domain. */
851 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
854 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
860 if (filter
|| agent_filter
) {
863 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
864 agent_filter
? agent_filter
: filter
, op
,
869 agent_filter
= new_filter
;
871 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
887 * Generate the filter bytecode from a given filter expression string. Put the
888 * newly allocated parser context in ctxp and populate the lsm object with the
891 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
893 static int generate_filter(char *filter_expression
,
894 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
897 struct filter_parser_ctx
*ctx
= NULL
;
900 assert(filter_expression
);
905 * Casting const to non-const, as the underlying function will use it in
908 fmem
= lttng_fmemopen((void *) filter_expression
,
909 strlen(filter_expression
), "r");
911 fprintf(stderr
, "Error opening memory as stream\n");
912 ret
= -LTTNG_ERR_FILTER_NOMEM
;
915 ctx
= filter_parser_ctx_alloc(fmem
);
917 fprintf(stderr
, "Error allocating parser\n");
918 ret
= -LTTNG_ERR_FILTER_NOMEM
;
919 goto filter_alloc_error
;
921 ret
= filter_parser_ctx_append_ast(ctx
);
923 fprintf(stderr
, "Parse error\n");
924 ret
= -LTTNG_ERR_FILTER_INVAL
;
928 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
931 fprintf(stderr
, "XML print error\n");
932 ret
= -LTTNG_ERR_FILTER_INVAL
;
937 dbg_printf("Generating IR... ");
939 ret
= filter_visitor_ir_generate(ctx
);
941 fprintf(stderr
, "Generate IR error\n");
942 ret
= -LTTNG_ERR_FILTER_INVAL
;
945 dbg_printf("done\n");
947 dbg_printf("Validating IR... ");
949 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
951 ret
= -LTTNG_ERR_FILTER_INVAL
;
955 /* Normalize globbing patterns in the expression. */
956 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
958 ret
= -LTTNG_ERR_FILTER_INVAL
;
962 /* Validate strings used as literals in the expression. */
963 ret
= filter_visitor_ir_validate_string(ctx
);
965 ret
= -LTTNG_ERR_FILTER_INVAL
;
969 /* Validate globbing patterns in the expression. */
970 ret
= filter_visitor_ir_validate_globbing(ctx
);
972 ret
= -LTTNG_ERR_FILTER_INVAL
;
976 dbg_printf("done\n");
978 dbg_printf("Generating bytecode... ");
980 ret
= filter_visitor_bytecode_generate(ctx
);
982 fprintf(stderr
, "Generate bytecode error\n");
983 ret
= -LTTNG_ERR_FILTER_INVAL
;
986 dbg_printf("done\n");
987 dbg_printf("Size of bytecode generated: %u bytes.\n",
988 bytecode_get_len(&ctx
->bytecode
->b
));
990 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
991 + bytecode_get_len(&ctx
->bytecode
->b
);
992 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
994 /* No need to keep the memory stream. */
995 if (fclose(fmem
) != 0) {
1003 filter_ir_free(ctx
);
1004 filter_parser_ctx_free(ctx
);
1006 if (fclose(fmem
) != 0) {
1014 * Enable event(s) for a channel, possibly with exclusions and a filter.
1015 * If no event name is specified, all events are enabled.
1016 * If no channel name is specified, the default name is used.
1017 * If filter expression is not NULL, the filter is set for the event.
1018 * If exclusion count is not zero, the exclusions are set for the event.
1019 * Returns size of returned session payload data or a negative error code.
1021 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1022 struct lttng_event
*ev
, const char *channel_name
,
1023 const char *original_filter_expression
,
1024 int exclusion_count
, char **exclusion_list
)
1026 struct lttcomm_session_msg lsm
;
1029 unsigned int free_filter_expression
= 0;
1030 struct filter_parser_ctx
*ctx
= NULL
;
1032 * Cast as non-const since we may replace the filter expression
1033 * by a dynamically allocated string. Otherwise, the original
1034 * string is not modified.
1036 char *filter_expression
= (char *) original_filter_expression
;
1038 if (handle
== NULL
|| ev
== NULL
) {
1039 ret
= -LTTNG_ERR_INVALID
;
1044 * Empty filter string will always be rejected by the parser
1045 * anyway, so treat this corner-case early to eliminate
1046 * lttng_fmemopen error for 0-byte allocation.
1048 if (filter_expression
&& filter_expression
[0] == '\0') {
1049 ret
= -LTTNG_ERR_INVALID
;
1053 memset(&lsm
, 0, sizeof(lsm
));
1055 /* If no channel name, send empty string. */
1056 if (channel_name
== NULL
) {
1057 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1058 sizeof(lsm
.u
.enable
.channel_name
));
1060 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1061 sizeof(lsm
.u
.enable
.channel_name
));
1064 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1065 if (ev
->name
[0] == '\0') {
1066 /* Enable all events */
1067 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1070 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1071 /* FIXME: copying non-packed struct to packed struct. */
1072 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1074 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1075 sizeof(lsm
.session
.name
));
1076 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1077 lsm
.u
.enable
.bytecode_len
= 0;
1080 * For the JUL domain, a filter is enforced except for the enable all
1081 * event. This is done to avoid having the event in all sessions thus
1082 * filtering by logger name.
1084 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1085 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1086 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1087 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1092 * We have either a filter or some exclusions, so we need to set up
1093 * a variable-length memory block from where to send the data.
1096 /* Parse filter expression. */
1097 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1098 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1099 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1100 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1101 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1102 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1105 /* Setup JUL filter if needed. */
1106 agent_filter
= set_agent_filter(filter_expression
, ev
);
1107 if (!agent_filter
) {
1108 if (!filter_expression
) {
1110 * No JUL and no filter, just skip
1117 * With an agent filter, the original filter has
1118 * been added to it thus replace the filter
1121 filter_expression
= agent_filter
;
1122 free_filter_expression
= 1;
1126 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1132 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1133 + lsm
.u
.enable
.expression_len
1134 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1136 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1140 /* Put exclusion names first in the data. */
1141 while (exclusion_count
--) {
1142 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1143 *(exclusion_list
+ exclusion_count
),
1144 LTTNG_SYMBOL_NAME_LEN
- 1);
1146 /* Add filter expression next. */
1147 if (lsm
.u
.enable
.expression_len
!= 0) {
1149 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1151 lsm
.u
.enable
.expression_len
);
1153 /* Add filter bytecode next. */
1154 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1156 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1157 + lsm
.u
.enable
.expression_len
,
1159 lsm
.u
.enable
.bytecode_len
);
1162 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1163 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1164 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1169 if (filter_expression
&& ctx
) {
1170 filter_bytecode_free(ctx
);
1171 filter_ir_free(ctx
);
1172 filter_parser_ctx_free(ctx
);
1175 if (free_filter_expression
) {
1177 * The filter expression has been replaced and must be freed as
1178 * it is not the original filter expression received as a
1181 free(filter_expression
);
1185 * Return directly to the caller and don't ask the sessiond since
1186 * something went wrong in the parsing of data above.
1191 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1195 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1196 struct lttng_event
*ev
, const char *channel_name
,
1197 const char *original_filter_expression
)
1199 struct lttcomm_session_msg lsm
;
1202 unsigned int free_filter_expression
= 0;
1203 struct filter_parser_ctx
*ctx
= NULL
;
1205 * Cast as non-const since we may replace the filter expression
1206 * by a dynamically allocated string. Otherwise, the original
1207 * string is not modified.
1209 char *filter_expression
= (char *) original_filter_expression
;
1211 if (handle
== NULL
|| ev
== NULL
) {
1212 ret
= -LTTNG_ERR_INVALID
;
1217 * Empty filter string will always be rejected by the parser
1218 * anyway, so treat this corner-case early to eliminate
1219 * lttng_fmemopen error for 0-byte allocation.
1221 if (filter_expression
&& filter_expression
[0] == '\0') {
1222 ret
= -LTTNG_ERR_INVALID
;
1226 memset(&lsm
, 0, sizeof(lsm
));
1228 /* If no channel name, send empty string. */
1229 if (channel_name
== NULL
) {
1230 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1231 sizeof(lsm
.u
.disable
.channel_name
));
1233 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1234 sizeof(lsm
.u
.disable
.channel_name
));
1237 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1239 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1240 /* FIXME: copying non-packed struct to packed struct. */
1241 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1243 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1244 sizeof(lsm
.session
.name
));
1245 lsm
.u
.disable
.bytecode_len
= 0;
1248 * For the JUL domain, a filter is enforced except for the
1249 * disable all event. This is done to avoid having the event in
1250 * all sessions thus filtering by logger name.
1252 if (filter_expression
== NULL
&&
1253 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1254 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1255 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1260 * We have a filter, so we need to set up a variable-length
1261 * memory block from where to send the data.
1264 /* Parse filter expression */
1265 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1266 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1267 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1268 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1269 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1270 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1273 /* Setup JUL filter if needed. */
1274 agent_filter
= set_agent_filter(filter_expression
, ev
);
1275 if (!agent_filter
) {
1276 if (!filter_expression
) {
1278 * No JUL and no filter, just skip
1285 * With a JUL filter, the original filter has
1286 * been added to it thus replace the filter
1289 filter_expression
= agent_filter
;
1290 free_filter_expression
= 1;
1294 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1300 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1301 + lsm
.u
.disable
.expression_len
);
1303 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1307 /* Add filter expression. */
1308 if (lsm
.u
.disable
.expression_len
!= 0) {
1311 lsm
.u
.disable
.expression_len
);
1313 /* Add filter bytecode next. */
1314 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1316 + lsm
.u
.disable
.expression_len
,
1318 lsm
.u
.disable
.bytecode_len
);
1321 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1322 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1326 if (filter_expression
&& ctx
) {
1327 filter_bytecode_free(ctx
);
1328 filter_ir_free(ctx
);
1329 filter_parser_ctx_free(ctx
);
1332 if (free_filter_expression
) {
1334 * The filter expression has been replaced and must be freed as
1335 * it is not the original filter expression received as a
1338 free(filter_expression
);
1342 * Return directly to the caller and don't ask the sessiond since
1343 * something went wrong in the parsing of data above.
1348 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1353 * Disable event(s) of a channel and domain.
1354 * If no event name is specified, all events are disabled.
1355 * If no channel name is specified, the default 'channel0' is used.
1356 * Returns size of returned session payload data or a negative error code.
1358 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1359 const char *channel_name
)
1361 struct lttng_event ev
;
1363 memset(&ev
, 0, sizeof(ev
));
1365 ev
.type
= LTTNG_EVENT_ALL
;
1366 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1367 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1370 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1372 struct lttng_channel
*channel
= NULL
;
1373 struct lttng_channel_extended
*extended
= NULL
;
1379 /* Validate domain. */
1380 switch (domain
->type
) {
1381 case LTTNG_DOMAIN_UST
:
1382 switch (domain
->buf_type
) {
1383 case LTTNG_BUFFER_PER_UID
:
1384 case LTTNG_BUFFER_PER_PID
:
1390 case LTTNG_DOMAIN_KERNEL
:
1391 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1399 channel
= zmalloc(sizeof(*channel
));
1404 extended
= zmalloc(sizeof(*extended
));
1409 channel
->attr
.extended
.ptr
= extended
;
1411 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1419 void lttng_channel_destroy(struct lttng_channel
*channel
)
1425 if (channel
->attr
.extended
.ptr
) {
1426 free(channel
->attr
.extended
.ptr
);
1432 * Enable channel per domain
1433 * Returns size of returned session payload data or a negative error code.
1435 int lttng_enable_channel(struct lttng_handle
*handle
,
1436 struct lttng_channel
*in_chan
)
1438 struct lttcomm_session_msg lsm
;
1440 /* NULL arguments are forbidden. No default values. */
1441 if (handle
== NULL
|| in_chan
== NULL
) {
1442 return -LTTNG_ERR_INVALID
;
1445 memset(&lsm
, 0, sizeof(lsm
));
1446 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1447 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1449 if (!in_chan
->attr
.extended
.ptr
) {
1450 struct lttng_channel
*channel
;
1451 struct lttng_channel_extended
*extended
;
1453 channel
= lttng_channel_create(&handle
->domain
);
1455 return -LTTNG_ERR_NOMEM
;
1459 * Create a new channel in order to use default extended
1462 extended
= (struct lttng_channel_extended
*)
1463 channel
->attr
.extended
.ptr
;
1464 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1465 lttng_channel_destroy(channel
);
1467 struct lttng_channel_extended
*extended
;
1469 extended
= (struct lttng_channel_extended
*)
1470 in_chan
->attr
.extended
.ptr
;
1471 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1474 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1475 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1477 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1478 sizeof(lsm
.session
.name
));
1480 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1484 * All tracing will be stopped for registered events of the channel.
1485 * Returns size of returned session payload data or a negative error code.
1487 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1489 struct lttcomm_session_msg lsm
;
1491 /* Safety check. Both are mandatory. */
1492 if (handle
== NULL
|| name
== NULL
) {
1493 return -LTTNG_ERR_INVALID
;
1496 memset(&lsm
, 0, sizeof(lsm
));
1498 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1500 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1501 sizeof(lsm
.u
.disable
.channel_name
));
1503 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1505 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1506 sizeof(lsm
.session
.name
));
1508 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1512 * Add PID to session tracker.
1513 * Return 0 on success else a negative LTTng error code.
1515 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1517 struct lttcomm_session_msg lsm
;
1519 /* NULL arguments are forbidden. No default values. */
1520 if (handle
== NULL
) {
1521 return -LTTNG_ERR_INVALID
;
1524 memset(&lsm
, 0, sizeof(lsm
));
1526 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1527 lsm
.u
.pid_tracker
.pid
= pid
;
1529 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1531 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1532 sizeof(lsm
.session
.name
));
1534 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1538 * Remove PID from session tracker.
1539 * Return 0 on success else a negative LTTng error code.
1541 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1543 struct lttcomm_session_msg lsm
;
1545 /* NULL arguments are forbidden. No default values. */
1546 if (handle
== NULL
) {
1547 return -LTTNG_ERR_INVALID
;
1550 memset(&lsm
, 0, sizeof(lsm
));
1552 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1553 lsm
.u
.pid_tracker
.pid
= pid
;
1555 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1557 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1558 sizeof(lsm
.session
.name
));
1560 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1564 * Lists all available tracepoints of domain.
1565 * Sets the contents of the events array.
1566 * Returns the number of lttng_event entries in events;
1567 * on error, returns a negative value.
1569 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1570 struct lttng_event
**events
)
1573 struct lttcomm_session_msg lsm
;
1575 if (handle
== NULL
) {
1576 return -LTTNG_ERR_INVALID
;
1579 memset(&lsm
, 0, sizeof(lsm
));
1580 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1581 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1583 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1588 return ret
/ sizeof(struct lttng_event
);
1592 * Lists all available tracepoint fields of domain.
1593 * Sets the contents of the event field array.
1594 * Returns the number of lttng_event_field entries in events;
1595 * on error, returns a negative value.
1597 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1598 struct lttng_event_field
**fields
)
1601 struct lttcomm_session_msg lsm
;
1603 if (handle
== NULL
) {
1604 return -LTTNG_ERR_INVALID
;
1607 memset(&lsm
, 0, sizeof(lsm
));
1608 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1609 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1611 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1616 return ret
/ sizeof(struct lttng_event_field
);
1620 * Lists all available kernel system calls. Allocates and sets the contents of
1623 * Returns the number of lttng_event entries in events; on error, returns a
1626 int lttng_list_syscalls(struct lttng_event
**events
)
1629 struct lttcomm_session_msg lsm
;
1632 return -LTTNG_ERR_INVALID
;
1635 memset(&lsm
, 0, sizeof(lsm
));
1636 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1637 /* Force kernel domain for system calls. */
1638 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1640 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1645 return ret
/ sizeof(struct lttng_event
);
1649 * Returns a human readable string describing
1650 * the error code (a negative value).
1652 const char *lttng_strerror(int code
)
1654 return error_get_str(code
);
1658 * Create a brand new session using name and url for destination.
1660 * Returns LTTNG_OK on success or a negative error code.
1662 int lttng_create_session(const char *name
, const char *url
)
1666 struct lttcomm_session_msg lsm
;
1667 struct lttng_uri
*uris
= NULL
;
1670 return -LTTNG_ERR_INVALID
;
1673 memset(&lsm
, 0, sizeof(lsm
));
1675 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1676 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1678 /* There should never be a data URL */
1679 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1681 return -LTTNG_ERR_INVALID
;
1684 lsm
.u
.uri
.size
= size
;
1686 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1687 sizeof(struct lttng_uri
) * size
, NULL
);
1694 * Destroy session using name.
1695 * Returns size of returned session payload data or a negative error code.
1698 int _lttng_destroy_session(const char *session_name
)
1700 struct lttcomm_session_msg lsm
;
1702 if (session_name
== NULL
) {
1703 return -LTTNG_ERR_INVALID
;
1706 memset(&lsm
, 0, sizeof(lsm
));
1707 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1709 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1710 sizeof(lsm
.session
.name
));
1712 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1716 * Stop the session and wait for the data before destroying it
1718 int lttng_destroy_session(const char *session_name
)
1723 * Stop the tracing and wait for the data.
1725 ret
= _lttng_stop_tracing(session_name
, 1);
1726 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1730 ret
= _lttng_destroy_session(session_name
);
1736 * Destroy the session without waiting for the data.
1738 int lttng_destroy_session_no_wait(const char *session_name
)
1743 * Stop the tracing without waiting for the data.
1744 * The session might already have been stopped, so just
1747 ret
= _lttng_stop_tracing(session_name
, 0);
1748 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1752 ret
= _lttng_destroy_session(session_name
);
1758 * Ask the session daemon for all available sessions.
1759 * Sets the contents of the sessions array.
1760 * Returns the number of lttng_session entries in sessions;
1761 * on error, returns a negative value.
1763 int lttng_list_sessions(struct lttng_session
**sessions
)
1766 struct lttcomm_session_msg lsm
;
1768 memset(&lsm
, 0, sizeof(lsm
));
1769 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1770 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1775 return ret
/ sizeof(struct lttng_session
);
1778 int lttng_set_session_shm_path(const char *session_name
,
1779 const char *shm_path
)
1781 struct lttcomm_session_msg lsm
;
1783 if (session_name
== NULL
) {
1784 return -LTTNG_ERR_INVALID
;
1787 memset(&lsm
, 0, sizeof(lsm
));
1788 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1790 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1791 sizeof(lsm
.session
.name
));
1792 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1793 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1795 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1799 * Ask the session daemon for all available domains of a session.
1800 * Sets the contents of the domains array.
1801 * Returns the number of lttng_domain entries in domains;
1802 * on error, returns a negative value.
1804 int lttng_list_domains(const char *session_name
,
1805 struct lttng_domain
**domains
)
1808 struct lttcomm_session_msg lsm
;
1810 if (session_name
== NULL
) {
1811 return -LTTNG_ERR_INVALID
;
1814 memset(&lsm
, 0, sizeof(lsm
));
1815 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1817 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1818 sizeof(lsm
.session
.name
));
1820 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1825 return ret
/ sizeof(struct lttng_domain
);
1829 * Ask the session daemon for all available channels of a session.
1830 * Sets the contents of the channels array.
1831 * Returns the number of lttng_channel entries in channels;
1832 * on error, returns a negative value.
1834 int lttng_list_channels(struct lttng_handle
*handle
,
1835 struct lttng_channel
**channels
)
1838 size_t channel_count
, i
;
1839 const size_t channel_size
= sizeof(struct lttng_channel
) +
1840 sizeof(struct lttng_channel_extended
);
1841 struct lttcomm_session_msg lsm
;
1844 if (handle
== NULL
) {
1845 ret
= -LTTNG_ERR_INVALID
;
1849 memset(&lsm
, 0, sizeof(lsm
));
1850 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1851 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1852 sizeof(lsm
.session
.name
));
1854 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1856 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1861 if (ret
% channel_size
) {
1862 ret
= -LTTNG_ERR_UNK
;
1867 channel_count
= (size_t) ret
/ channel_size
;
1869 /* Set extended info pointers */
1870 extended_at
= ((void *) *channels
) +
1871 channel_count
* sizeof(struct lttng_channel
);
1872 for (i
= 0; i
< channel_count
; i
++) {
1873 struct lttng_channel
*chan
= &(*channels
)[i
];
1875 chan
->attr
.extended
.ptr
= extended_at
;
1876 extended_at
+= sizeof(struct lttng_channel_extended
);
1879 ret
= (int) channel_count
;
1885 * Ask the session daemon for all available events of a session channel.
1886 * Sets the contents of the events array.
1887 * Returns the number of lttng_event entries in events;
1888 * on error, returns a negative value.
1890 int lttng_list_events(struct lttng_handle
*handle
,
1891 const char *channel_name
, struct lttng_event
**events
)
1894 struct lttcomm_session_msg lsm
;
1895 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1896 size_t cmd_header_len
;
1897 uint32_t nb_events
, i
;
1900 /* Safety check. An handle and channel name are mandatory */
1901 if (handle
== NULL
|| channel_name
== NULL
) {
1902 return -LTTNG_ERR_INVALID
;
1905 memset(&lsm
, 0, sizeof(lsm
));
1906 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1907 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1908 sizeof(lsm
.session
.name
));
1909 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1910 sizeof(lsm
.u
.list
.channel_name
));
1911 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1913 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1914 (void **) events
, (void **) &cmd_header
, &cmd_header_len
);
1919 /* Set number of events and free command header */
1920 nb_events
= cmd_header
->nb_events
;
1921 if (nb_events
> INT_MAX
) {
1925 ret
= (int) nb_events
;
1929 /* Set extended info pointers */
1930 extended_at
= ((void*) (*events
)) +
1931 nb_events
* sizeof(struct lttng_event
);
1933 for (i
= 0; i
< nb_events
; i
++) {
1934 struct lttcomm_event_extended_header
*ext_header
;
1935 struct lttng_event
*event
= &(*events
)[i
];
1937 event
->extended
.ptr
= extended_at
;
1939 (struct lttcomm_event_extended_header
*) extended_at
;
1940 extended_at
+= sizeof(*ext_header
);
1941 extended_at
+= ext_header
->filter_len
;
1943 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1954 * Sets the tracing_group variable with name.
1955 * This function allocates memory pointed to by tracing_group.
1956 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1958 int lttng_set_tracing_group(const char *name
)
1961 return -LTTNG_ERR_INVALID
;
1964 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1965 return -LTTNG_ERR_FATAL
;
1971 int lttng_calibrate(struct lttng_handle
*handle
,
1972 struct lttng_calibrate
*calibrate
)
1975 * This command was removed in LTTng 2.9.
1977 return -LTTNG_ERR_UND
;
1981 * Set default channel attributes.
1982 * If either or both of the arguments are null, attr content is zeroe'd.
1984 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1985 struct lttng_channel_attr
*attr
)
1987 struct lttng_channel_extended
*extended
;
1990 if (attr
== NULL
|| domain
== NULL
) {
1994 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
1995 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1997 /* Same for all domains. */
1998 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1999 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2000 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2002 switch (domain
->type
) {
2003 case LTTNG_DOMAIN_KERNEL
:
2004 attr
->switch_timer_interval
=
2005 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2006 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2007 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2008 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2009 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2011 extended
->monitor_timer_interval
=
2012 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2013 extended
->blocking_timeout
=
2014 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2017 case LTTNG_DOMAIN_UST
:
2018 switch (domain
->buf_type
) {
2019 case LTTNG_BUFFER_PER_UID
:
2020 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2021 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2022 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2023 attr
->switch_timer_interval
=
2024 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2025 attr
->read_timer_interval
=
2026 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2028 extended
->monitor_timer_interval
=
2029 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2030 extended
->blocking_timeout
=
2031 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2034 case LTTNG_BUFFER_PER_PID
:
2036 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2037 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2038 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2039 attr
->switch_timer_interval
=
2040 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2041 attr
->read_timer_interval
=
2042 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2044 extended
->monitor_timer_interval
=
2045 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2046 extended
->blocking_timeout
=
2047 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2052 /* Default behavior: leave set to 0. */
2056 attr
->extended
.ptr
= extended
;
2059 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2060 uint64_t *discarded_events
)
2063 struct lttng_channel_extended
*chan_ext
;
2065 if (!channel
|| !discarded_events
) {
2066 ret
= -LTTNG_ERR_INVALID
;
2070 chan_ext
= channel
->attr
.extended
.ptr
;
2073 * This can happen since the lttng_channel structure is
2074 * used for other tasks where this pointer is never set.
2076 *discarded_events
= 0;
2080 *discarded_events
= chan_ext
->discarded_events
;
2085 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2086 uint64_t *lost_packets
)
2089 struct lttng_channel_extended
*chan_ext
;
2091 if (!channel
|| !lost_packets
) {
2092 ret
= -LTTNG_ERR_INVALID
;
2096 chan_ext
= channel
->attr
.extended
.ptr
;
2099 * This can happen since the lttng_channel structure is
2100 * used for other tasks where this pointer is never set.
2106 *lost_packets
= chan_ext
->lost_packets
;
2111 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2112 uint64_t *monitor_timer_interval
)
2116 if (!chan
|| !monitor_timer_interval
) {
2117 ret
= -LTTNG_ERR_INVALID
;
2121 if (!chan
->attr
.extended
.ptr
) {
2122 ret
= -LTTNG_ERR_INVALID
;
2126 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2127 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2132 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2133 uint64_t monitor_timer_interval
)
2137 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2138 ret
= -LTTNG_ERR_INVALID
;
2142 ((struct lttng_channel_extended
*)
2143 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2144 monitor_timer_interval
;
2149 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2150 int64_t *blocking_timeout
)
2154 if (!chan
|| !blocking_timeout
) {
2155 ret
= -LTTNG_ERR_INVALID
;
2159 if (!chan
->attr
.extended
.ptr
) {
2160 ret
= -LTTNG_ERR_INVALID
;
2164 *blocking_timeout
= ((struct lttng_channel_extended
*)
2165 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2170 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2171 int64_t blocking_timeout
)
2174 int64_t msec_timeout
;
2176 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2177 ret
= -LTTNG_ERR_INVALID
;
2181 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2182 ret
= -LTTNG_ERR_INVALID
;
2187 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2188 * us to accept a narrower range of values (msecs expressed as a signed
2191 msec_timeout
= blocking_timeout
/ 1000;
2192 if (msec_timeout
!= (int32_t) msec_timeout
) {
2193 ret
= -LTTNG_ERR_INVALID
;
2197 ((struct lttng_channel_extended
*)
2198 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2205 * Check if session daemon is alive.
2207 * Return 1 if alive or 0 if not.
2208 * On error returns a negative value.
2210 int lttng_session_daemon_alive(void)
2214 ret
= set_session_daemon_path();
2220 if (*sessiond_sock_path
== '\0') {
2222 * No socket path set. Weird error which means the constructor
2228 ret
= try_connect_sessiond(sessiond_sock_path
);
2239 * Set URL for a consumer for a session and domain.
2241 * Return 0 on success, else a negative value.
2243 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2244 const char *control_url
, const char *data_url
)
2248 struct lttcomm_session_msg lsm
;
2249 struct lttng_uri
*uris
= NULL
;
2251 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2252 return -LTTNG_ERR_INVALID
;
2255 memset(&lsm
, 0, sizeof(lsm
));
2257 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2259 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2260 sizeof(lsm
.session
.name
));
2261 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2263 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2265 return -LTTNG_ERR_INVALID
;
2268 lsm
.u
.uri
.size
= size
;
2270 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2271 sizeof(struct lttng_uri
) * size
, NULL
);
2280 int lttng_enable_consumer(struct lttng_handle
*handle
)
2288 int lttng_disable_consumer(struct lttng_handle
*handle
)
2294 * This is an extension of create session that is ONLY and SHOULD only be used
2295 * by the lttng command line program. It exists to avoid using URI parsing in
2298 * We need the date and time for the trace path subdirectory for the case where
2299 * the user does NOT define one using either -o or -U. Using the normal
2300 * lttng_create_session API call, we have no clue on the session daemon side if
2301 * the URL was generated automatically by the client or define by the user.
2303 * So this function "wrapper" is hidden from the public API, takes the datetime
2304 * string and appends it if necessary to the URI subdirectory before sending it
2305 * to the session daemon.
2307 * With this extra function, the lttng_create_session call behavior is not
2308 * changed and the timestamp is appended to the URI on the session daemon side
2311 int _lttng_create_session_ext(const char *name
, const char *url
,
2312 const char *datetime
)
2316 struct lttcomm_session_msg lsm
;
2317 struct lttng_uri
*uris
= NULL
;
2319 if (name
== NULL
|| datetime
== NULL
) {
2320 return -LTTNG_ERR_INVALID
;
2323 memset(&lsm
, 0, sizeof(lsm
));
2325 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2326 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2328 /* There should never be a data URL. */
2329 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2331 ret
= -LTTNG_ERR_INVALID
;
2335 lsm
.u
.uri
.size
= size
;
2337 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2338 /* Don't append datetime if the name was automatically created. */
2339 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2340 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2341 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2344 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2347 PERROR("snprintf uri subdir");
2348 ret
= -LTTNG_ERR_FATAL
;
2353 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2354 sizeof(struct lttng_uri
) * size
, NULL
);
2362 * For a given session name, this call checks if the data is ready to be read
2363 * or is still being extracted by the consumer(s) hence not ready to be used by
2366 int lttng_data_pending(const char *session_name
)
2369 struct lttcomm_session_msg lsm
;
2370 uint8_t *pending
= NULL
;
2372 if (session_name
== NULL
) {
2373 return -LTTNG_ERR_INVALID
;
2376 memset(&lsm
, 0, sizeof(lsm
));
2377 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2379 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2380 sizeof(lsm
.session
.name
));
2382 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2385 } else if (ret
!= 1) {
2386 /* Unexpected payload size */
2387 ret
= -LTTNG_ERR_INVALID
;
2391 ret
= (int) *pending
;
2398 * Create a session exclusively used for snapshot.
2400 * Returns LTTNG_OK on success or a negative error code.
2402 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2406 struct lttcomm_session_msg lsm
;
2407 struct lttng_uri
*uris
= NULL
;
2410 return -LTTNG_ERR_INVALID
;
2413 memset(&lsm
, 0, sizeof(lsm
));
2415 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2416 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2418 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2420 return -LTTNG_ERR_INVALID
;
2423 lsm
.u
.uri
.size
= size
;
2426 * If the user does not specify a custom subdir, use the session name.
2428 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2429 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2431 PERROR("snprintf uri subdir");
2432 ret
= -LTTNG_ERR_FATAL
;
2437 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2438 sizeof(struct lttng_uri
) * size
, NULL
);
2446 * Create a session exclusively used for live.
2448 * Returns LTTNG_OK on success or a negative error code.
2450 int lttng_create_session_live(const char *name
, const char *url
,
2451 unsigned int timer_interval
)
2455 struct lttcomm_session_msg lsm
;
2456 struct lttng_uri
*uris
= NULL
;
2458 if (name
== NULL
|| timer_interval
== 0) {
2459 return -LTTNG_ERR_INVALID
;
2462 memset(&lsm
, 0, sizeof(lsm
));
2464 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2465 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2468 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2470 ret
= -LTTNG_ERR_INVALID
;
2474 /* file:// is not accepted for live session. */
2475 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2476 ret
= -LTTNG_ERR_INVALID
;
2483 lsm
.u
.session_live
.nb_uri
= size
;
2484 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2486 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2487 sizeof(struct lttng_uri
) * size
, NULL
);
2495 * List PIDs in the tracker.
2497 * enabled is set to whether the PID tracker is enabled.
2498 * pids is set to an allocated array of PIDs currently tracked. On
2499 * success, pids must be freed by the caller.
2500 * nr_pids is set to the number of entries contained by the pids array.
2502 * Returns 0 on success, else a negative LTTng error code.
2504 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2505 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2509 struct lttcomm_session_msg lsm
;
2513 if (handle
== NULL
) {
2514 return -LTTNG_ERR_INVALID
;
2517 memset(&lsm
, 0, sizeof(lsm
));
2518 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2519 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2520 sizeof(lsm
.session
.name
));
2521 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2523 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2527 nr_pids
= ret
/ sizeof(int32_t);
2528 if (nr_pids
== 1 && pids
[0] == -1) {
2534 *_enabled
= enabled
;
2536 *_nr_pids
= nr_pids
;
2541 * Regenerate the metadata for a session.
2542 * Return 0 on success, a negative error code on error.
2544 int lttng_regenerate_metadata(const char *session_name
)
2547 struct lttcomm_session_msg lsm
;
2549 if (!session_name
) {
2550 ret
= -LTTNG_ERR_INVALID
;
2554 memset(&lsm
, 0, sizeof(lsm
));
2555 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2557 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2558 sizeof(lsm
.session
.name
));
2560 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2571 * Deprecated, replaced by lttng_regenerate_metadata.
2573 int lttng_metadata_regenerate(const char *session_name
)
2575 return lttng_regenerate_metadata(session_name
);
2579 * Regenerate the statedump of a session.
2580 * Return 0 on success, a negative error code on error.
2582 int lttng_regenerate_statedump(const char *session_name
)
2585 struct lttcomm_session_msg lsm
;
2587 if (!session_name
) {
2588 ret
= -LTTNG_ERR_INVALID
;
2592 memset(&lsm
, 0, sizeof(lsm
));
2593 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2595 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2596 sizeof(lsm
.session
.name
));
2598 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2608 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2611 struct lttcomm_session_msg lsm
;
2612 struct lttng_dynamic_buffer buffer
;
2614 lttng_dynamic_buffer_init(&buffer
);
2616 ret
= -LTTNG_ERR_INVALID
;
2620 if (!lttng_trigger_validate(trigger
)) {
2621 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2625 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2627 ret
= -LTTNG_ERR_UNK
;
2631 memset(&lsm
, 0, sizeof(lsm
));
2632 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2633 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2634 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2637 lttng_dynamic_buffer_reset(&buffer
);
2641 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2644 struct lttcomm_session_msg lsm
;
2645 struct lttng_dynamic_buffer buffer
;
2647 lttng_dynamic_buffer_init(&buffer
);
2649 ret
= -LTTNG_ERR_INVALID
;
2653 if (!lttng_trigger_validate(trigger
)) {
2654 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2658 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2660 ret
= -LTTNG_ERR_UNK
;
2664 memset(&lsm
, 0, sizeof(lsm
));
2665 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2666 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2667 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2670 lttng_dynamic_buffer_reset(&buffer
);
2674 int lttng_session_get_current_archive_location(const char *session_name
,
2677 struct lttcomm_session_msg lsm
;
2678 struct lttng_session_get_current_output_return
*output_return
= NULL
;
2682 memset(&lsm
, 0, sizeof(lsm
));
2683 lsm
.cmd_type
= LTTNG_SESSION_GET_CURRENT_OUTPUT
;
2684 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2685 sizeof(lsm
.session
.name
));
2687 ret
= -LTTNG_ERR_INVALID
;
2691 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &output_return
);
2697 path_len
= lttng_strnlen(output_return
->path
,
2698 sizeof(output_return
->path
));
2699 if (path_len
== 0 || path_len
== sizeof(output_return
->path
)) {
2700 ret
= -LTTNG_ERR_NO_SESSION_OUTPUT
;
2704 *chunk_path
= zmalloc(path_len
+ 1);
2709 memcpy(*chunk_path
, output_return
->path
, path_len
);
2714 free(output_return
);
2721 static void __attribute__((constructor
)) init(void)
2723 /* Set default session group */
2724 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2730 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2732 free(tracing_group
);