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); \
63 #define COPY_DOMAIN_PACKED(dst, src) \
65 struct lttng_domain _tmp_domain; \
67 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
71 /* Socket to session daemon for communication */
72 static int sessiond_socket
;
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 * Receive data from the sessiond socket.
189 * On success, returns the number of bytes received (>=0)
190 * On error, returns -1 (recvmsg() error) or -ENOTCONN
192 static int recv_data_sessiond(void *buf
, size_t len
)
197 ret
= -LTTNG_ERR_NO_SESSIOND
;
201 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
203 ret
= -LTTNG_ERR_FATAL
;
211 * Check if we are in the specified group.
213 * If yes return 1, else return -1.
216 int lttng_check_tracing_group(void)
218 gid_t
*grp_list
, tracing_gid
;
219 int grp_list_size
, grp_id
, i
;
221 const char *grp_name
= tracing_group
;
223 /* Get GID of group 'tracing' */
224 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
225 /* If grp_tracing is NULL, the group does not exist. */
229 /* Get number of supplementary group IDs */
230 grp_list_size
= getgroups(0, NULL
);
231 if (grp_list_size
< 0) {
236 /* Alloc group list of the right size */
237 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
242 grp_id
= getgroups(grp_list_size
, grp_list
);
248 for (i
= 0; i
< grp_list_size
; i
++) {
249 if (grp_list
[i
] == tracing_gid
) {
263 * Try connect to session daemon with sock_path.
265 * Return 0 on success, else -1
267 static int try_connect_sessiond(const char *sock_path
)
271 /* If socket exist, we check if the daemon listens for connect. */
272 ret
= access(sock_path
, F_OK
);
278 ret
= lttcomm_connect_unix_sock(sock_path
);
284 ret
= lttcomm_close_unix_sock(ret
);
286 PERROR("lttcomm_close_unix_sock");
296 * Set sessiond socket path by putting it in the global sessiond_sock_path
299 * Returns 0 on success, negative value on failure (the sessiond socket path
300 * is somehow too long or ENOMEM).
302 static int set_session_daemon_path(void)
304 int in_tgroup
= 0; /* In tracing group. */
310 /* Are we in the tracing group ? */
311 in_tgroup
= lttng_check_tracing_group();
314 if ((uid
== 0) || in_tgroup
) {
315 lttng_ctl_copy_string(sessiond_sock_path
,
316 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
324 ret
= try_connect_sessiond(sessiond_sock_path
);
328 /* Global session daemon not available... */
330 /* ...or not in tracing group (and not root), default */
333 * With GNU C < 2.1, snprintf returns -1 if the target buffer
335 * With GNU C >= 2.1, snprintf returns the required size
336 * (excluding closing null)
338 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
339 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
340 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
352 * Connect to the LTTng session daemon.
354 * On success, return 0. On error, return -1.
356 static int connect_sessiond(void)
360 /* Don't try to connect if already connected. */
365 ret
= set_session_daemon_path();
370 /* Connect to the sesssion daemon. */
371 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
376 sessiond_socket
= ret
;
386 * Clean disconnect from the session daemon.
388 * On success, return 0. On error, return -1.
390 static int disconnect_sessiond(void)
395 ret
= lttcomm_close_unix_sock(sessiond_socket
);
403 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
411 ret
= -LTTNG_ERR_INVALID
;
421 ret
= recv_data_sessiond(buf
, len
);
427 ret
= -LTTNG_ERR_INVALID
;
431 /* Move ownership of command header buffer to user. */
436 /* No command header. */
452 * Ask the session daemon a specific command and put the data into buf.
453 * Takes extra var. len. data as input to send to the session daemon.
455 * Return size of data (only payload, not header) or a negative error code.
458 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
459 const void *vardata
, size_t vardata_len
,
460 void **user_payload_buf
, void **user_cmd_header_buf
,
461 size_t *user_cmd_header_len
)
465 struct lttcomm_lttng_msg llm
;
467 ret
= connect_sessiond();
469 ret
= -LTTNG_ERR_NO_SESSIOND
;
473 /* Send command to session daemon */
474 ret
= send_session_msg(lsm
);
476 /* Ret value is a valid lttng error code. */
479 /* Send var len data */
480 ret
= send_session_varlen(vardata
, vardata_len
);
482 /* Ret value is a valid lttng error code. */
486 /* Get header from data transmission */
487 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
489 /* Ret value is a valid lttng error code. */
493 /* Check error code if OK */
494 if (llm
.ret_code
!= LTTNG_OK
) {
499 /* Get command header from data transmission */
500 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
501 user_cmd_header_buf
, user_cmd_header_len
);
506 /* Get payload from data transmission */
507 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
516 disconnect_sessiond();
521 * Create lttng handle and return pointer.
523 * The returned pointer will be NULL in case of malloc() error.
525 struct lttng_handle
*lttng_create_handle(const char *session_name
,
526 struct lttng_domain
*domain
)
528 struct lttng_handle
*handle
= NULL
;
530 handle
= zmalloc(sizeof(struct lttng_handle
));
531 if (handle
== NULL
) {
532 PERROR("malloc handle");
536 /* Copy session name */
537 lttng_ctl_copy_string(handle
->session_name
, session_name
,
538 sizeof(handle
->session_name
));
540 /* Copy lttng domain or leave initialized to 0. */
542 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
550 * Destroy handle by free(3) the pointer.
552 void lttng_destroy_handle(struct lttng_handle
*handle
)
558 * Register an outside consumer.
560 * Returns size of returned session payload data or a negative error code.
562 int lttng_register_consumer(struct lttng_handle
*handle
,
563 const char *socket_path
)
565 struct lttcomm_session_msg lsm
;
567 if (handle
== NULL
|| socket_path
== NULL
) {
568 return -LTTNG_ERR_INVALID
;
571 memset(&lsm
, 0, sizeof(lsm
));
572 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
573 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
574 sizeof(lsm
.session
.name
));
575 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
577 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
578 sizeof(lsm
.u
.reg
.path
));
580 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
584 * Start tracing for all traces of the session.
586 * Returns size of returned session payload data or a negative error code.
588 int lttng_start_tracing(const char *session_name
)
590 struct lttcomm_session_msg lsm
;
592 if (session_name
== NULL
) {
593 return -LTTNG_ERR_INVALID
;
596 memset(&lsm
, 0, sizeof(lsm
));
597 lsm
.cmd_type
= LTTNG_START_TRACE
;
599 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
600 sizeof(lsm
.session
.name
));
602 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
606 * Stop tracing for all traces of the session.
608 static int _lttng_stop_tracing(const char *session_name
, int wait
)
611 struct lttcomm_session_msg lsm
;
613 if (session_name
== NULL
) {
614 return -LTTNG_ERR_INVALID
;
617 memset(&lsm
, 0, sizeof(lsm
));
618 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
620 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
621 sizeof(lsm
.session
.name
));
623 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
624 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
632 /* Check for data availability */
634 data_ret
= lttng_data_pending(session_name
);
636 /* Return the data available call error. */
642 * Data sleep time before retrying (in usec). Don't sleep if the
643 * call returned value indicates availability.
646 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
648 } while (data_ret
!= 0);
656 * Stop tracing and wait for data availability.
658 int lttng_stop_tracing(const char *session_name
)
660 return _lttng_stop_tracing(session_name
, 1);
664 * Stop tracing but _don't_ wait for data availability.
666 int lttng_stop_tracing_no_wait(const char *session_name
)
668 return _lttng_stop_tracing(session_name
, 0);
672 * Add context to a channel.
674 * If the given channel is NULL, add the contexts to all channels.
675 * The event_name param is ignored.
677 * Returns the size of the returned payload data or a negative error code.
679 int lttng_add_context(struct lttng_handle
*handle
,
680 struct lttng_event_context
*ctx
, const char *event_name
,
681 const char *channel_name
)
686 struct lttcomm_session_msg lsm
;
688 /* Safety check. Both are mandatory. */
689 if (handle
== NULL
|| ctx
== NULL
) {
690 ret
= -LTTNG_ERR_INVALID
;
694 memset(&lsm
, 0, sizeof(lsm
));
695 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
697 /* If no channel name, send empty string. */
698 if (channel_name
== NULL
) {
699 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
700 sizeof(lsm
.u
.context
.channel_name
));
702 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
703 sizeof(lsm
.u
.context
.channel_name
));
706 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
707 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
708 sizeof(lsm
.session
.name
));
710 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
711 size_t provider_len
, ctx_len
;
712 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
713 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
715 if (!provider_name
|| !ctx_name
) {
716 ret
= -LTTNG_ERR_INVALID
;
720 provider_len
= strlen(provider_name
);
721 if (provider_len
== 0) {
722 ret
= -LTTNG_ERR_INVALID
;
725 lsm
.u
.context
.provider_name_len
= provider_len
;
727 ctx_len
= strlen(ctx_name
);
729 ret
= -LTTNG_ERR_INVALID
;
732 lsm
.u
.context
.context_name_len
= ctx_len
;
734 len
= provider_len
+ ctx_len
;
737 ret
= -LTTNG_ERR_NOMEM
;
741 memcpy(buf
, provider_name
, provider_len
);
742 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
744 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
746 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
748 * Don't leak application addresses to the sessiond.
749 * This is only necessary when ctx is for an app ctx otherwise
750 * the values inside the union (type & config) are overwritten.
752 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
753 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
756 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
763 * Enable event(s) for a channel.
765 * If no event name is specified, all events are enabled.
766 * If no channel name is specified, the default 'channel0' is used.
768 * Returns size of returned session payload data or a negative error code.
770 int lttng_enable_event(struct lttng_handle
*handle
,
771 struct lttng_event
*ev
, const char *channel_name
)
773 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
778 * Create or enable an event with a filter expression.
780 * Return negative error value on error.
781 * Return size of returned session payload data if OK.
783 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
784 struct lttng_event
*event
, const char *channel_name
,
785 const char *filter_expression
)
787 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
788 filter_expression
, 0, NULL
);
792 * Depending on the event, return a newly allocated agent filter expression or
793 * NULL if not applicable.
795 * An event with NO loglevel and the name is * will return NULL.
797 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
800 char *agent_filter
= NULL
;
804 /* Don't add filter for the '*' event. */
805 if (strcmp(ev
->name
, "*") != 0) {
807 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
810 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
818 /* Add loglevel filtering if any for the JUL domain. */
819 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
822 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
828 if (filter
|| agent_filter
) {
831 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
832 agent_filter
? agent_filter
: filter
, op
,
837 agent_filter
= new_filter
;
839 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
855 * Generate the filter bytecode from a given filter expression string. Put the
856 * newly allocated parser context in ctxp and populate the lsm object with the
859 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
861 static int generate_filter(char *filter_expression
,
862 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
865 struct filter_parser_ctx
*ctx
= NULL
;
868 assert(filter_expression
);
873 * Casting const to non-const, as the underlying function will use it in
876 fmem
= lttng_fmemopen((void *) filter_expression
,
877 strlen(filter_expression
), "r");
879 fprintf(stderr
, "Error opening memory as stream\n");
880 ret
= -LTTNG_ERR_FILTER_NOMEM
;
883 ctx
= filter_parser_ctx_alloc(fmem
);
885 fprintf(stderr
, "Error allocating parser\n");
886 ret
= -LTTNG_ERR_FILTER_NOMEM
;
887 goto filter_alloc_error
;
889 ret
= filter_parser_ctx_append_ast(ctx
);
891 fprintf(stderr
, "Parse error\n");
892 ret
= -LTTNG_ERR_FILTER_INVAL
;
895 ret
= filter_visitor_set_parent(ctx
);
897 fprintf(stderr
, "Set parent error\n");
898 ret
= -LTTNG_ERR_FILTER_INVAL
;
902 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
905 fprintf(stderr
, "XML print error\n");
906 ret
= -LTTNG_ERR_FILTER_INVAL
;
911 dbg_printf("Generating IR... ");
913 ret
= filter_visitor_ir_generate(ctx
);
915 fprintf(stderr
, "Generate IR error\n");
916 ret
= -LTTNG_ERR_FILTER_INVAL
;
919 dbg_printf("done\n");
921 dbg_printf("Validating IR... ");
923 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
925 ret
= -LTTNG_ERR_FILTER_INVAL
;
929 /* Normalize globbing patterns in the expression. */
930 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
932 ret
= -LTTNG_ERR_FILTER_INVAL
;
936 /* Validate strings used as literals in the expression. */
937 ret
= filter_visitor_ir_validate_string(ctx
);
939 ret
= -LTTNG_ERR_FILTER_INVAL
;
943 /* Validate globbing patterns in the expression. */
944 ret
= filter_visitor_ir_validate_globbing(ctx
);
946 ret
= -LTTNG_ERR_FILTER_INVAL
;
950 dbg_printf("done\n");
952 dbg_printf("Generating bytecode... ");
954 ret
= filter_visitor_bytecode_generate(ctx
);
956 fprintf(stderr
, "Generate bytecode error\n");
957 ret
= -LTTNG_ERR_FILTER_INVAL
;
960 dbg_printf("done\n");
961 dbg_printf("Size of bytecode generated: %u bytes.\n",
962 bytecode_get_len(&ctx
->bytecode
->b
));
964 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
965 + bytecode_get_len(&ctx
->bytecode
->b
);
966 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
968 /* No need to keep the memory stream. */
969 if (fclose(fmem
) != 0) {
978 filter_parser_ctx_free(ctx
);
980 if (fclose(fmem
) != 0) {
988 * Enable event(s) for a channel, possibly with exclusions and a filter.
989 * If no event name is specified, all events are enabled.
990 * If no channel name is specified, the default name is used.
991 * If filter expression is not NULL, the filter is set for the event.
992 * If exclusion count is not zero, the exclusions are set for the event.
993 * Returns size of returned session payload data or a negative error code.
995 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
996 struct lttng_event
*ev
, const char *channel_name
,
997 const char *original_filter_expression
,
998 int exclusion_count
, char **exclusion_list
)
1000 struct lttcomm_session_msg lsm
;
1003 unsigned int free_filter_expression
= 0;
1004 struct filter_parser_ctx
*ctx
= NULL
;
1006 * Cast as non-const since we may replace the filter expression
1007 * by a dynamically allocated string. Otherwise, the original
1008 * string is not modified.
1010 char *filter_expression
= (char *) original_filter_expression
;
1012 if (handle
== NULL
|| ev
== NULL
) {
1013 ret
= -LTTNG_ERR_INVALID
;
1018 * Empty filter string will always be rejected by the parser
1019 * anyway, so treat this corner-case early to eliminate
1020 * lttng_fmemopen error for 0-byte allocation.
1022 if (filter_expression
&& filter_expression
[0] == '\0') {
1023 ret
= -LTTNG_ERR_INVALID
;
1027 memset(&lsm
, 0, sizeof(lsm
));
1029 /* If no channel name, send empty string. */
1030 if (channel_name
== NULL
) {
1031 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1032 sizeof(lsm
.u
.enable
.channel_name
));
1034 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1035 sizeof(lsm
.u
.enable
.channel_name
));
1038 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1039 if (ev
->name
[0] == '\0') {
1040 /* Enable all events */
1041 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1044 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1045 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1047 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1048 sizeof(lsm
.session
.name
));
1049 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1050 lsm
.u
.enable
.bytecode_len
= 0;
1053 * For the JUL domain, a filter is enforced except for the enable all
1054 * event. This is done to avoid having the event in all sessions thus
1055 * filtering by logger name.
1057 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1058 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1059 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1060 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1065 * We have either a filter or some exclusions, so we need to set up
1066 * a variable-length memory block from where to send the data.
1069 /* Parse filter expression. */
1070 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1071 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1072 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1073 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1074 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1075 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1078 /* Setup JUL filter if needed. */
1079 agent_filter
= set_agent_filter(filter_expression
, ev
);
1080 if (!agent_filter
) {
1081 if (!filter_expression
) {
1083 * No JUL and no filter, just skip
1090 * With an agent filter, the original filter has
1091 * been added to it thus replace the filter
1094 filter_expression
= agent_filter
;
1095 free_filter_expression
= 1;
1099 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1105 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1106 + lsm
.u
.enable
.expression_len
1107 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1109 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1113 /* Put exclusion names first in the data. */
1114 while (exclusion_count
--) {
1115 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1116 *(exclusion_list
+ exclusion_count
),
1117 LTTNG_SYMBOL_NAME_LEN
- 1);
1119 /* Add filter expression next. */
1120 if (lsm
.u
.enable
.expression_len
!= 0) {
1122 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1124 lsm
.u
.enable
.expression_len
);
1126 /* Add filter bytecode next. */
1127 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1129 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1130 + lsm
.u
.enable
.expression_len
,
1132 lsm
.u
.enable
.bytecode_len
);
1135 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1136 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1137 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1142 if (filter_expression
&& ctx
) {
1143 filter_bytecode_free(ctx
);
1144 filter_ir_free(ctx
);
1145 filter_parser_ctx_free(ctx
);
1148 if (free_filter_expression
) {
1150 * The filter expression has been replaced and must be freed as
1151 * it is not the original filter expression received as a
1154 free(filter_expression
);
1158 * Return directly to the caller and don't ask the sessiond since
1159 * something went wrong in the parsing of data above.
1164 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1168 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1169 struct lttng_event
*ev
, const char *channel_name
,
1170 const char *original_filter_expression
)
1172 struct lttcomm_session_msg lsm
;
1175 unsigned int free_filter_expression
= 0;
1176 struct filter_parser_ctx
*ctx
= NULL
;
1178 * Cast as non-const since we may replace the filter expression
1179 * by a dynamically allocated string. Otherwise, the original
1180 * string is not modified.
1182 char *filter_expression
= (char *) original_filter_expression
;
1184 if (handle
== NULL
|| ev
== NULL
) {
1185 ret
= -LTTNG_ERR_INVALID
;
1190 * Empty filter string will always be rejected by the parser
1191 * anyway, so treat this corner-case early to eliminate
1192 * lttng_fmemopen error for 0-byte allocation.
1194 if (filter_expression
&& filter_expression
[0] == '\0') {
1195 ret
= -LTTNG_ERR_INVALID
;
1199 memset(&lsm
, 0, sizeof(lsm
));
1201 /* If no channel name, send empty string. */
1202 if (channel_name
== NULL
) {
1203 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1204 sizeof(lsm
.u
.disable
.channel_name
));
1206 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1207 sizeof(lsm
.u
.disable
.channel_name
));
1210 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1212 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1213 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1215 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1216 sizeof(lsm
.session
.name
));
1217 lsm
.u
.disable
.bytecode_len
= 0;
1220 * For the JUL domain, a filter is enforced except for the
1221 * disable all event. This is done to avoid having the event in
1222 * all sessions thus filtering by logger name.
1224 if (filter_expression
== NULL
&&
1225 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1226 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1227 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1232 * We have a filter, so we need to set up a variable-length
1233 * memory block from where to send the data.
1236 /* Parse filter expression */
1237 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1238 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1239 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1240 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1241 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1242 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1245 /* Setup JUL filter if needed. */
1246 agent_filter
= set_agent_filter(filter_expression
, ev
);
1247 if (!agent_filter
) {
1248 if (!filter_expression
) {
1250 * No JUL and no filter, just skip
1257 * With a JUL filter, the original filter has
1258 * been added to it thus replace the filter
1261 filter_expression
= agent_filter
;
1262 free_filter_expression
= 1;
1266 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1272 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1273 + lsm
.u
.disable
.expression_len
);
1275 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1279 /* Add filter expression. */
1280 if (lsm
.u
.disable
.expression_len
!= 0) {
1283 lsm
.u
.disable
.expression_len
);
1285 /* Add filter bytecode next. */
1286 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1288 + lsm
.u
.disable
.expression_len
,
1290 lsm
.u
.disable
.bytecode_len
);
1293 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1294 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1298 if (filter_expression
&& ctx
) {
1299 filter_bytecode_free(ctx
);
1300 filter_ir_free(ctx
);
1301 filter_parser_ctx_free(ctx
);
1304 if (free_filter_expression
) {
1306 * The filter expression has been replaced and must be freed as
1307 * it is not the original filter expression received as a
1310 free(filter_expression
);
1314 * Return directly to the caller and don't ask the sessiond since
1315 * something went wrong in the parsing of data above.
1320 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1325 * Disable event(s) of a channel and domain.
1326 * If no event name is specified, all events are disabled.
1327 * If no channel name is specified, the default 'channel0' is used.
1328 * Returns size of returned session payload data or a negative error code.
1330 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1331 const char *channel_name
)
1333 struct lttng_event ev
;
1335 memset(&ev
, 0, sizeof(ev
));
1337 ev
.type
= LTTNG_EVENT_ALL
;
1338 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1339 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1342 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1344 struct lttng_channel
*channel
= NULL
;
1345 struct lttng_channel_extended
*extended
= NULL
;
1351 /* Validate domain. */
1352 switch (domain
->type
) {
1353 case LTTNG_DOMAIN_UST
:
1354 switch (domain
->buf_type
) {
1355 case LTTNG_BUFFER_PER_UID
:
1356 case LTTNG_BUFFER_PER_PID
:
1362 case LTTNG_DOMAIN_KERNEL
:
1363 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1371 channel
= zmalloc(sizeof(*channel
));
1376 extended
= zmalloc(sizeof(*extended
));
1381 channel
->attr
.extended
.ptr
= extended
;
1383 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1391 void lttng_channel_destroy(struct lttng_channel
*channel
)
1397 if (channel
->attr
.extended
.ptr
) {
1398 free(channel
->attr
.extended
.ptr
);
1404 * Enable channel per domain
1405 * Returns size of returned session payload data or a negative error code.
1407 int lttng_enable_channel(struct lttng_handle
*handle
,
1408 struct lttng_channel
*in_chan
)
1410 struct lttcomm_session_msg lsm
;
1412 /* NULL arguments are forbidden. No default values. */
1413 if (handle
== NULL
|| in_chan
== NULL
) {
1414 return -LTTNG_ERR_INVALID
;
1417 memset(&lsm
, 0, sizeof(lsm
));
1418 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1419 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1421 if (!in_chan
->attr
.extended
.ptr
) {
1422 struct lttng_channel
*channel
;
1423 struct lttng_channel_extended
*extended
;
1425 channel
= lttng_channel_create(&handle
->domain
);
1427 return -LTTNG_ERR_NOMEM
;
1431 * Create a new channel in order to use default extended
1434 extended
= (struct lttng_channel_extended
*)
1435 channel
->attr
.extended
.ptr
;
1436 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1437 lttng_channel_destroy(channel
);
1439 struct lttng_channel_extended
*extended
;
1441 extended
= (struct lttng_channel_extended
*)
1442 in_chan
->attr
.extended
.ptr
;
1443 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1446 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1447 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1449 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1450 sizeof(lsm
.session
.name
));
1452 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1456 * All tracing will be stopped for registered events of the channel.
1457 * Returns size of returned session payload data or a negative error code.
1459 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1461 struct lttcomm_session_msg lsm
;
1463 /* Safety check. Both are mandatory. */
1464 if (handle
== NULL
|| name
== NULL
) {
1465 return -LTTNG_ERR_INVALID
;
1468 memset(&lsm
, 0, sizeof(lsm
));
1470 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1472 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1473 sizeof(lsm
.u
.disable
.channel_name
));
1475 COPY_DOMAIN_PACKED(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 * Add PID to session tracker.
1485 * Return 0 on success else a negative LTTng error code.
1487 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1489 struct lttcomm_session_msg lsm
;
1491 /* NULL arguments are forbidden. No default values. */
1492 if (handle
== NULL
) {
1493 return -LTTNG_ERR_INVALID
;
1496 memset(&lsm
, 0, sizeof(lsm
));
1498 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1499 lsm
.u
.pid_tracker
.pid
= pid
;
1501 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1503 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1504 sizeof(lsm
.session
.name
));
1506 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1510 * Remove PID from session tracker.
1511 * Return 0 on success else a negative LTTng error code.
1513 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1515 struct lttcomm_session_msg lsm
;
1517 /* NULL arguments are forbidden. No default values. */
1518 if (handle
== NULL
) {
1519 return -LTTNG_ERR_INVALID
;
1522 memset(&lsm
, 0, sizeof(lsm
));
1524 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1525 lsm
.u
.pid_tracker
.pid
= pid
;
1527 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1529 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1530 sizeof(lsm
.session
.name
));
1532 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1536 * Lists all available tracepoints of domain.
1537 * Sets the contents of the events array.
1538 * Returns the number of lttng_event entries in events;
1539 * on error, returns a negative value.
1541 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1542 struct lttng_event
**events
)
1545 struct lttcomm_session_msg lsm
;
1547 if (handle
== NULL
) {
1548 return -LTTNG_ERR_INVALID
;
1551 memset(&lsm
, 0, sizeof(lsm
));
1552 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1553 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1555 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1560 return ret
/ sizeof(struct lttng_event
);
1564 * Lists all available tracepoint fields of domain.
1565 * Sets the contents of the event field array.
1566 * Returns the number of lttng_event_field entries in events;
1567 * on error, returns a negative value.
1569 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1570 struct lttng_event_field
**fields
)
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_TRACEPOINT_FIELDS
;
1581 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1583 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1588 return ret
/ sizeof(struct lttng_event_field
);
1592 * Lists all available kernel system calls. Allocates and sets the contents of
1595 * Returns the number of lttng_event entries in events; on error, returns a
1598 int lttng_list_syscalls(struct lttng_event
**events
)
1601 struct lttcomm_session_msg lsm
;
1604 return -LTTNG_ERR_INVALID
;
1607 memset(&lsm
, 0, sizeof(lsm
));
1608 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1609 /* Force kernel domain for system calls. */
1610 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1612 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1617 return ret
/ sizeof(struct lttng_event
);
1621 * Returns a human readable string describing
1622 * the error code (a negative value).
1624 const char *lttng_strerror(int code
)
1626 return error_get_str(code
);
1630 * Create a brand new session using name and url for destination.
1632 * Returns LTTNG_OK on success or a negative error code.
1634 int lttng_create_session(const char *name
, const char *url
)
1638 struct lttcomm_session_msg lsm
;
1639 struct lttng_uri
*uris
= NULL
;
1642 return -LTTNG_ERR_INVALID
;
1645 memset(&lsm
, 0, sizeof(lsm
));
1647 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1648 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1650 /* There should never be a data URL */
1651 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1653 return -LTTNG_ERR_INVALID
;
1656 lsm
.u
.uri
.size
= size
;
1658 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1659 sizeof(struct lttng_uri
) * size
, NULL
);
1666 * Destroy session using name.
1667 * Returns size of returned session payload data or a negative error code.
1670 int _lttng_destroy_session(const char *session_name
)
1672 struct lttcomm_session_msg lsm
;
1674 if (session_name
== NULL
) {
1675 return -LTTNG_ERR_INVALID
;
1678 memset(&lsm
, 0, sizeof(lsm
));
1679 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1681 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1682 sizeof(lsm
.session
.name
));
1684 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1688 * Stop the session and wait for the data before destroying it
1690 int lttng_destroy_session(const char *session_name
)
1695 * Stop the tracing and wait for the data.
1697 ret
= _lttng_stop_tracing(session_name
, 1);
1698 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1702 ret
= _lttng_destroy_session(session_name
);
1708 * Destroy the session without waiting for the data.
1710 int lttng_destroy_session_no_wait(const char *session_name
)
1715 * Stop the tracing without waiting for the data.
1716 * The session might already have been stopped, so just
1719 ret
= _lttng_stop_tracing(session_name
, 0);
1720 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1724 ret
= _lttng_destroy_session(session_name
);
1730 * Ask the session daemon for all available sessions.
1731 * Sets the contents of the sessions array.
1732 * Returns the number of lttng_session entries in sessions;
1733 * on error, returns a negative value.
1735 int lttng_list_sessions(struct lttng_session
**sessions
)
1738 struct lttcomm_session_msg lsm
;
1740 memset(&lsm
, 0, sizeof(lsm
));
1741 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1742 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1747 return ret
/ sizeof(struct lttng_session
);
1750 int lttng_set_session_shm_path(const char *session_name
,
1751 const char *shm_path
)
1753 struct lttcomm_session_msg lsm
;
1755 if (session_name
== NULL
) {
1756 return -LTTNG_ERR_INVALID
;
1759 memset(&lsm
, 0, sizeof(lsm
));
1760 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1762 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1763 sizeof(lsm
.session
.name
));
1764 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1765 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1767 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1771 * Ask the session daemon for all available domains of a session.
1772 * Sets the contents of the domains array.
1773 * Returns the number of lttng_domain entries in domains;
1774 * on error, returns a negative value.
1776 int lttng_list_domains(const char *session_name
,
1777 struct lttng_domain
**domains
)
1780 struct lttcomm_session_msg lsm
;
1782 if (session_name
== NULL
) {
1783 return -LTTNG_ERR_INVALID
;
1786 memset(&lsm
, 0, sizeof(lsm
));
1787 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1789 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1790 sizeof(lsm
.session
.name
));
1792 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1797 return ret
/ sizeof(struct lttng_domain
);
1801 * Ask the session daemon for all available channels of a session.
1802 * Sets the contents of the channels array.
1803 * Returns the number of lttng_channel entries in channels;
1804 * on error, returns a negative value.
1806 int lttng_list_channels(struct lttng_handle
*handle
,
1807 struct lttng_channel
**channels
)
1810 size_t channel_count
, i
;
1811 const size_t channel_size
= sizeof(struct lttng_channel
) +
1812 sizeof(struct lttng_channel_extended
);
1813 struct lttcomm_session_msg lsm
;
1816 if (handle
== NULL
) {
1817 ret
= -LTTNG_ERR_INVALID
;
1821 memset(&lsm
, 0, sizeof(lsm
));
1822 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1823 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1824 sizeof(lsm
.session
.name
));
1826 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1828 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1833 if (ret
% channel_size
) {
1834 ret
= -LTTNG_ERR_UNK
;
1839 channel_count
= (size_t) ret
/ channel_size
;
1841 /* Set extended info pointers */
1842 extended_at
= ((void *) *channels
) +
1843 channel_count
* sizeof(struct lttng_channel
);
1844 for (i
= 0; i
< channel_count
; i
++) {
1845 struct lttng_channel
*chan
= &(*channels
)[i
];
1847 chan
->attr
.extended
.ptr
= extended_at
;
1848 extended_at
+= sizeof(struct lttng_channel_extended
);
1851 ret
= (int) channel_count
;
1857 * Ask the session daemon for all available events of a session channel.
1858 * Sets the contents of the events array.
1859 * Returns the number of lttng_event entries in events;
1860 * on error, returns a negative value.
1862 int lttng_list_events(struct lttng_handle
*handle
,
1863 const char *channel_name
, struct lttng_event
**events
)
1866 struct lttcomm_session_msg lsm
;
1867 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1868 size_t cmd_header_len
;
1869 uint32_t nb_events
, i
;
1872 /* Safety check. An handle and channel name are mandatory */
1873 if (handle
== NULL
|| channel_name
== NULL
) {
1874 return -LTTNG_ERR_INVALID
;
1877 memset(&lsm
, 0, sizeof(lsm
));
1878 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1879 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1880 sizeof(lsm
.session
.name
));
1881 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1882 sizeof(lsm
.u
.list
.channel_name
));
1883 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1885 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) events
,
1886 (void **) &cmd_header
, &cmd_header_len
);
1891 /* Set number of events and free command header */
1892 nb_events
= cmd_header
->nb_events
;
1893 if (nb_events
> INT_MAX
) {
1897 ret
= (int) nb_events
;
1901 /* Set extended info pointers */
1902 extended_at
= ((void*) (*events
)) +
1903 nb_events
* sizeof(struct lttng_event
);
1905 for (i
= 0; i
< nb_events
; i
++) {
1906 struct lttcomm_event_extended_header
*ext_header
;
1907 struct lttng_event
*event
= &(*events
)[i
];
1909 event
->extended
.ptr
= extended_at
;
1911 (struct lttcomm_event_extended_header
*) extended_at
;
1912 extended_at
+= sizeof(*ext_header
);
1913 extended_at
+= ext_header
->filter_len
;
1915 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1925 int lttng_event_get_filter_expression(struct lttng_event
*event
,
1926 const char **filter_expression
)
1929 struct lttcomm_event_extended_header
*ext_header
;
1931 if (!event
|| !filter_expression
) {
1932 ret
= -LTTNG_ERR_INVALID
;
1936 ext_header
= event
->extended
.ptr
;
1940 * This can happen since the lttng_event structure is
1941 * used for other tasks where this pointer is never set.
1943 *filter_expression
= NULL
;
1947 if (ext_header
->filter_len
) {
1948 *filter_expression
= ((const char *) (ext_header
)) +
1949 sizeof(*ext_header
);
1951 *filter_expression
= NULL
;
1958 int lttng_event_get_exclusion_name_count(struct lttng_event
*event
)
1961 struct lttcomm_event_extended_header
*ext_header
;
1964 ret
= -LTTNG_ERR_INVALID
;
1968 ext_header
= event
->extended
.ptr
;
1971 * This can happen since the lttng_event structure is
1972 * used for other tasks where this pointer is never set.
1978 if (ext_header
->nb_exclusions
> INT_MAX
) {
1979 ret
= -LTTNG_ERR_OVERFLOW
;
1982 ret
= (int) ext_header
->nb_exclusions
;
1987 int lttng_event_get_exclusion_name(struct lttng_event
*event
,
1988 size_t index
, const char **exclusion_name
)
1991 struct lttcomm_event_extended_header
*ext_header
;
1994 if (!event
|| !exclusion_name
) {
1995 ret
= -LTTNG_ERR_INVALID
;
1999 ext_header
= event
->extended
.ptr
;
2001 ret
= -LTTNG_ERR_INVALID
;
2005 if (index
>= ext_header
->nb_exclusions
) {
2006 ret
= -LTTNG_ERR_INVALID
;
2010 at
= (void *) ext_header
+ sizeof(*ext_header
);
2011 at
+= ext_header
->filter_len
;
2012 at
+= index
* LTTNG_SYMBOL_NAME_LEN
;
2013 *exclusion_name
= at
;
2020 * Sets the tracing_group variable with name.
2021 * This function allocates memory pointed to by tracing_group.
2022 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2024 int lttng_set_tracing_group(const char *name
)
2027 return -LTTNG_ERR_INVALID
;
2030 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2031 return -LTTNG_ERR_FATAL
;
2037 int lttng_calibrate(struct lttng_handle
*handle
,
2038 struct lttng_calibrate
*calibrate
)
2041 * This command was removed in LTTng 2.9.
2043 return -LTTNG_ERR_UND
;
2047 * Set default channel attributes.
2048 * If either or both of the arguments are null, attr content is zeroe'd.
2050 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2051 struct lttng_channel_attr
*attr
)
2053 struct lttng_channel_extended
*extended
;
2056 if (attr
== NULL
|| domain
== NULL
) {
2060 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2061 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2063 /* Same for all domains. */
2064 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2065 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2066 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2068 switch (domain
->type
) {
2069 case LTTNG_DOMAIN_KERNEL
:
2070 attr
->switch_timer_interval
=
2071 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2072 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2073 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2074 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2075 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2077 extended
->monitor_timer_interval
=
2078 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2079 extended
->blocking_timeout
=
2080 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2083 case LTTNG_DOMAIN_UST
:
2084 switch (domain
->buf_type
) {
2085 case LTTNG_BUFFER_PER_UID
:
2086 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2087 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2088 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2089 attr
->switch_timer_interval
=
2090 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2091 attr
->read_timer_interval
=
2092 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2094 extended
->monitor_timer_interval
=
2095 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2096 extended
->blocking_timeout
=
2097 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2100 case LTTNG_BUFFER_PER_PID
:
2102 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2103 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2104 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2105 attr
->switch_timer_interval
=
2106 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2107 attr
->read_timer_interval
=
2108 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2110 extended
->monitor_timer_interval
=
2111 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2112 extended
->blocking_timeout
=
2113 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2118 /* Default behavior: leave set to 0. */
2122 attr
->extended
.ptr
= extended
;
2125 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2126 uint64_t *discarded_events
)
2129 struct lttng_channel_extended
*chan_ext
;
2131 if (!channel
|| !discarded_events
) {
2132 ret
= -LTTNG_ERR_INVALID
;
2136 chan_ext
= channel
->attr
.extended
.ptr
;
2139 * This can happen since the lttng_channel structure is
2140 * used for other tasks where this pointer is never set.
2142 *discarded_events
= 0;
2146 *discarded_events
= chan_ext
->discarded_events
;
2151 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2152 uint64_t *lost_packets
)
2155 struct lttng_channel_extended
*chan_ext
;
2157 if (!channel
|| !lost_packets
) {
2158 ret
= -LTTNG_ERR_INVALID
;
2162 chan_ext
= channel
->attr
.extended
.ptr
;
2165 * This can happen since the lttng_channel structure is
2166 * used for other tasks where this pointer is never set.
2172 *lost_packets
= chan_ext
->lost_packets
;
2177 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2178 uint64_t *monitor_timer_interval
)
2182 if (!chan
|| !monitor_timer_interval
) {
2183 ret
= -LTTNG_ERR_INVALID
;
2187 if (!chan
->attr
.extended
.ptr
) {
2188 ret
= -LTTNG_ERR_INVALID
;
2192 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2193 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2198 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2199 uint64_t monitor_timer_interval
)
2203 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2204 ret
= -LTTNG_ERR_INVALID
;
2208 ((struct lttng_channel_extended
*)
2209 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2210 monitor_timer_interval
;
2215 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2216 int64_t *blocking_timeout
)
2220 if (!chan
|| !blocking_timeout
) {
2221 ret
= -LTTNG_ERR_INVALID
;
2225 if (!chan
->attr
.extended
.ptr
) {
2226 ret
= -LTTNG_ERR_INVALID
;
2230 *blocking_timeout
= ((struct lttng_channel_extended
*)
2231 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2236 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2237 int64_t blocking_timeout
)
2240 int64_t msec_timeout
;
2242 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2243 ret
= -LTTNG_ERR_INVALID
;
2247 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2248 ret
= -LTTNG_ERR_INVALID
;
2253 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2254 * us to accept a narrower range of values (msecs expressed as a signed
2257 msec_timeout
= blocking_timeout
/ 1000;
2258 if (msec_timeout
!= (int32_t) msec_timeout
) {
2259 ret
= -LTTNG_ERR_INVALID
;
2263 ((struct lttng_channel_extended
*)
2264 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2271 * Check if session daemon is alive.
2273 * Return 1 if alive or 0 if not.
2274 * On error returns a negative value.
2276 int lttng_session_daemon_alive(void)
2280 ret
= set_session_daemon_path();
2286 if (*sessiond_sock_path
== '\0') {
2288 * No socket path set. Weird error which means the constructor
2294 ret
= try_connect_sessiond(sessiond_sock_path
);
2305 * Set URL for a consumer for a session and domain.
2307 * Return 0 on success, else a negative value.
2309 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2310 const char *control_url
, const char *data_url
)
2314 struct lttcomm_session_msg lsm
;
2315 struct lttng_uri
*uris
= NULL
;
2317 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2318 return -LTTNG_ERR_INVALID
;
2321 memset(&lsm
, 0, sizeof(lsm
));
2323 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2325 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2326 sizeof(lsm
.session
.name
));
2327 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2329 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2331 return -LTTNG_ERR_INVALID
;
2334 lsm
.u
.uri
.size
= size
;
2336 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2337 sizeof(struct lttng_uri
) * size
, NULL
);
2346 int lttng_enable_consumer(struct lttng_handle
*handle
)
2354 int lttng_disable_consumer(struct lttng_handle
*handle
)
2360 * This is an extension of create session that is ONLY and SHOULD only be used
2361 * by the lttng command line program. It exists to avoid using URI parsing in
2364 * We need the date and time for the trace path subdirectory for the case where
2365 * the user does NOT define one using either -o or -U. Using the normal
2366 * lttng_create_session API call, we have no clue on the session daemon side if
2367 * the URL was generated automatically by the client or define by the user.
2369 * So this function "wrapper" is hidden from the public API, takes the datetime
2370 * string and appends it if necessary to the URI subdirectory before sending it
2371 * to the session daemon.
2373 * With this extra function, the lttng_create_session call behavior is not
2374 * changed and the timestamp is appended to the URI on the session daemon side
2377 int _lttng_create_session_ext(const char *name
, const char *url
,
2378 const char *datetime
)
2382 struct lttcomm_session_msg lsm
;
2383 struct lttng_uri
*uris
= NULL
;
2385 if (name
== NULL
|| datetime
== NULL
) {
2386 return -LTTNG_ERR_INVALID
;
2389 memset(&lsm
, 0, sizeof(lsm
));
2391 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2392 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2394 /* There should never be a data URL. */
2395 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2397 ret
= -LTTNG_ERR_INVALID
;
2401 lsm
.u
.uri
.size
= size
;
2403 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2404 /* Don't append datetime if the name was automatically created. */
2405 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2406 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2407 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2410 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2413 PERROR("snprintf uri subdir");
2414 ret
= -LTTNG_ERR_FATAL
;
2419 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2420 sizeof(struct lttng_uri
) * size
, NULL
);
2428 * For a given session name, this call checks if the data is ready to be read
2429 * or is still being extracted by the consumer(s) hence not ready to be used by
2432 int lttng_data_pending(const char *session_name
)
2435 struct lttcomm_session_msg lsm
;
2436 uint8_t *pending
= NULL
;
2438 if (session_name
== NULL
) {
2439 return -LTTNG_ERR_INVALID
;
2442 memset(&lsm
, 0, sizeof(lsm
));
2443 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2445 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2446 sizeof(lsm
.session
.name
));
2448 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2451 } else if (ret
!= 1) {
2452 /* Unexpected payload size */
2453 ret
= -LTTNG_ERR_INVALID
;
2455 } else if (!pending
) {
2456 /* Internal error. */
2457 ret
= -LTTNG_ERR_UNK
;
2461 ret
= (int) *pending
;
2468 * Create a session exclusively used for snapshot.
2470 * Returns LTTNG_OK on success or a negative error code.
2472 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2476 struct lttcomm_session_msg lsm
;
2477 struct lttng_uri
*uris
= NULL
;
2480 return -LTTNG_ERR_INVALID
;
2483 memset(&lsm
, 0, sizeof(lsm
));
2485 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2486 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2488 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2490 return -LTTNG_ERR_INVALID
;
2493 lsm
.u
.uri
.size
= size
;
2495 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2496 sizeof(struct lttng_uri
) * size
, NULL
);
2503 * Create a session exclusively used for live.
2505 * Returns LTTNG_OK on success or a negative error code.
2507 int lttng_create_session_live(const char *name
, const char *url
,
2508 unsigned int timer_interval
)
2512 struct lttcomm_session_msg lsm
;
2513 struct lttng_uri
*uris
= NULL
;
2515 if (name
== NULL
|| timer_interval
== 0) {
2516 return -LTTNG_ERR_INVALID
;
2519 memset(&lsm
, 0, sizeof(lsm
));
2521 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2522 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2525 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2527 ret
= -LTTNG_ERR_INVALID
;
2531 /* file:// is not accepted for live session. */
2532 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2533 ret
= -LTTNG_ERR_INVALID
;
2540 lsm
.u
.session_live
.nb_uri
= size
;
2541 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2543 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2544 sizeof(struct lttng_uri
) * size
, NULL
);
2552 * List PIDs in the tracker.
2554 * enabled is set to whether the PID tracker is enabled.
2555 * pids is set to an allocated array of PIDs currently tracked. On
2556 * success, pids must be freed by the caller.
2557 * nr_pids is set to the number of entries contained by the pids array.
2559 * Returns 0 on success, else a negative LTTng error code.
2561 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2562 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2566 struct lttcomm_session_msg lsm
;
2568 int32_t *pids
= NULL
;
2570 if (handle
== NULL
) {
2571 return -LTTNG_ERR_INVALID
;
2574 memset(&lsm
, 0, sizeof(lsm
));
2575 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2576 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2577 sizeof(lsm
.session
.name
));
2578 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2580 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2584 nr_pids
= ret
/ sizeof(int32_t);
2585 if (nr_pids
> 0 && !pids
) {
2586 return -LTTNG_ERR_UNK
;
2588 if (nr_pids
== 1 && pids
[0] == -1) {
2594 *_enabled
= enabled
;
2596 *_nr_pids
= nr_pids
;
2601 * Regenerate the metadata for a session.
2602 * Return 0 on success, a negative error code on error.
2604 int lttng_regenerate_metadata(const char *session_name
)
2607 struct lttcomm_session_msg lsm
;
2609 if (!session_name
) {
2610 ret
= -LTTNG_ERR_INVALID
;
2614 memset(&lsm
, 0, sizeof(lsm
));
2615 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2617 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2618 sizeof(lsm
.session
.name
));
2620 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2631 * Deprecated, replaced by lttng_regenerate_metadata.
2633 int lttng_metadata_regenerate(const char *session_name
)
2635 return lttng_regenerate_metadata(session_name
);
2639 * Regenerate the statedump of a session.
2640 * Return 0 on success, a negative error code on error.
2642 int lttng_regenerate_statedump(const char *session_name
)
2645 struct lttcomm_session_msg lsm
;
2647 if (!session_name
) {
2648 ret
= -LTTNG_ERR_INVALID
;
2652 memset(&lsm
, 0, sizeof(lsm
));
2653 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2655 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2656 sizeof(lsm
.session
.name
));
2658 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2668 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2671 struct lttcomm_session_msg lsm
;
2672 char *trigger_buf
= NULL
;
2673 ssize_t trigger_size
;
2676 ret
= -LTTNG_ERR_INVALID
;
2680 if (!lttng_trigger_validate(trigger
)) {
2681 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2685 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2686 if (trigger_size
< 0) {
2687 ret
= -LTTNG_ERR_UNK
;
2691 trigger_buf
= zmalloc(trigger_size
);
2693 ret
= -LTTNG_ERR_NOMEM
;
2697 memset(&lsm
, 0, sizeof(lsm
));
2698 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2699 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2700 ret
= -LTTNG_ERR_UNK
;
2704 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2705 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2706 trigger_size
, NULL
);
2712 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2715 struct lttcomm_session_msg lsm
;
2716 char *trigger_buf
= NULL
;
2717 ssize_t trigger_size
;
2720 ret
= -LTTNG_ERR_INVALID
;
2724 if (!lttng_trigger_validate(trigger
)) {
2725 ret
= -LTTNG_ERR_INVALID
;
2729 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2730 if (trigger_size
< 0) {
2731 ret
= -LTTNG_ERR_UNK
;
2735 trigger_buf
= zmalloc(trigger_size
);
2737 ret
= -LTTNG_ERR_NOMEM
;
2741 memset(&lsm
, 0, sizeof(lsm
));
2742 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2743 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2744 ret
= -LTTNG_ERR_UNK
;
2748 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2749 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2750 trigger_size
, NULL
);
2759 static void __attribute__((constructor
)) init(void)
2761 /* Set default session group */
2762 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2768 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2770 free(tracing_group
);