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>
42 #include "filter/filter-ast.h"
43 #include "filter/filter-parser.h"
44 #include "filter/filter-bytecode.h"
45 #include "filter/memstream.h"
46 #include "lttng-ctl-helper.h"
49 static const int print_xml
= 1;
50 #define dbg_printf(fmt, args...) \
51 printf("[debug liblttng-ctl] " fmt, ## args)
53 static const int print_xml
= 0;
54 #define dbg_printf(fmt, args...) \
56 /* do nothing but check printf format */ \
58 printf("[debug liblttnctl] " fmt, ## args); \
63 /* Socket to session daemon for communication */
64 static int sessiond_socket
;
65 static char sessiond_sock_path
[PATH_MAX
];
68 static char *tracing_group
;
74 * Those two variables are used by error.h to silent or control the verbosity of
75 * error message. They are global to the library so application linking with it
76 * are able to compile correctly and also control verbosity of the library.
79 int lttng_opt_verbose
;
83 * Copy string from src to dst and enforce null terminated byte.
86 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
89 strncpy(dst
, src
, len
);
90 /* Enforce the NULL terminated byte */
98 * Copy domain to lttcomm_session_msg domain.
100 * If domain is unknown, default domain will be the kernel.
103 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
104 struct lttng_domain
*src
)
108 case LTTNG_DOMAIN_KERNEL
:
109 case LTTNG_DOMAIN_UST
:
110 case LTTNG_DOMAIN_JUL
:
111 case LTTNG_DOMAIN_LOG4J
:
112 case LTTNG_DOMAIN_PYTHON
:
113 memcpy(dst
, src
, sizeof(struct lttng_domain
));
116 memset(dst
, 0, sizeof(struct lttng_domain
));
123 * Send lttcomm_session_msg to the session daemon.
125 * On success, returns the number of bytes sent (>=0)
126 * On error, returns -1
128 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
133 ret
= -LTTNG_ERR_NO_SESSIOND
;
137 DBG("LSM cmd type : %d", lsm
->cmd_type
);
139 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
140 sizeof(struct lttcomm_session_msg
));
142 ret
= -LTTNG_ERR_FATAL
;
150 * Send var len data to the session daemon.
152 * On success, returns the number of bytes sent (>=0)
153 * On error, returns -1
155 static int send_session_varlen(const void *data
, size_t len
)
160 ret
= -LTTNG_ERR_NO_SESSIOND
;
169 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
171 ret
= -LTTNG_ERR_FATAL
;
179 * Receive data from the sessiond socket.
181 * On success, returns the number of bytes received (>=0)
182 * On error, returns -1 (recvmsg() error) or -ENOTCONN
184 static int recv_data_sessiond(void *buf
, size_t len
)
189 ret
= -LTTNG_ERR_NO_SESSIOND
;
193 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
195 ret
= -LTTNG_ERR_FATAL
;
203 * Check if we are in the specified group.
205 * If yes return 1, else return -1.
208 int lttng_check_tracing_group(void)
210 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
212 int grp_list_size
, grp_id
, i
;
214 const char *grp_name
= tracing_group
;
216 /* Get GID of group 'tracing' */
217 grp_tracing
= getgrnam(grp_name
);
219 /* If grp_tracing is NULL, the group does not exist. */
223 /* Get number of supplementary group IDs */
224 grp_list_size
= getgroups(0, NULL
);
225 if (grp_list_size
< 0) {
230 /* Alloc group list of the right size */
231 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
236 grp_id
= getgroups(grp_list_size
, grp_list
);
242 for (i
= 0; i
< grp_list_size
; i
++) {
243 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
257 * Try connect to session daemon with sock_path.
259 * Return 0 on success, else -1
261 static int try_connect_sessiond(const char *sock_path
)
265 /* If socket exist, we check if the daemon listens for connect. */
266 ret
= access(sock_path
, F_OK
);
272 ret
= lttcomm_connect_unix_sock(sock_path
);
278 ret
= lttcomm_close_unix_sock(ret
);
280 PERROR("lttcomm_close_unix_sock");
290 * Set sessiond socket path by putting it in the global sessiond_sock_path
293 * Returns 0 on success, negative value on failure (the sessiond socket path
294 * is somehow too long or ENOMEM).
296 static int set_session_daemon_path(void)
298 int in_tgroup
= 0; /* In tracing group. */
304 /* Are we in the tracing group ? */
305 in_tgroup
= lttng_check_tracing_group();
308 if ((uid
== 0) || in_tgroup
) {
309 lttng_ctl_copy_string(sessiond_sock_path
,
310 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
318 ret
= try_connect_sessiond(sessiond_sock_path
);
322 /* Global session daemon not available... */
324 /* ...or not in tracing group (and not root), default */
327 * With GNU C < 2.1, snprintf returns -1 if the target buffer
329 * With GNU C >= 2.1, snprintf returns the required size
330 * (excluding closing null)
332 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
333 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
334 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
346 * Connect to the LTTng session daemon.
348 * On success, return 0. On error, return -1.
350 static int connect_sessiond(void)
354 /* Don't try to connect if already connected. */
359 ret
= set_session_daemon_path();
364 /* Connect to the sesssion daemon. */
365 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
370 sessiond_socket
= ret
;
380 * Clean disconnect from the session daemon.
382 * On success, return 0. On error, return -1.
384 static int disconnect_sessiond(void)
389 ret
= lttcomm_close_unix_sock(sessiond_socket
);
397 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
405 ret
= -LTTNG_ERR_INVALID
;
415 ret
= recv_data_sessiond(buf
, len
);
421 ret
= -LTTNG_ERR_INVALID
;
425 /* Move ownership of command header buffer to user. */
430 /* No command header. */
446 * Ask the session daemon a specific command and put the data into buf.
447 * Takes extra var. len. data as input to send to the session daemon.
449 * Return size of data (only payload, not header) or a negative error code.
452 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
453 const void *vardata
, size_t vardata_len
,
454 void **user_payload_buf
, void **user_cmd_header_buf
,
455 size_t *user_cmd_header_len
)
459 struct lttcomm_lttng_msg llm
;
461 ret
= connect_sessiond();
463 ret
= -LTTNG_ERR_NO_SESSIOND
;
467 /* Send command to session daemon */
468 ret
= send_session_msg(lsm
);
470 /* Ret value is a valid lttng error code. */
473 /* Send var len data */
474 ret
= send_session_varlen(vardata
, vardata_len
);
476 /* Ret value is a valid lttng error code. */
480 /* Get header from data transmission */
481 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
483 /* Ret value is a valid lttng error code. */
487 /* Check error code if OK */
488 if (llm
.ret_code
!= LTTNG_OK
) {
493 /* Get command header from data transmission */
494 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
495 user_cmd_header_buf
, user_cmd_header_len
);
500 /* Get payload from data transmission */
501 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
510 disconnect_sessiond();
515 * Create lttng handle and return pointer.
517 * The returned pointer will be NULL in case of malloc() error.
519 struct lttng_handle
*lttng_create_handle(const char *session_name
,
520 struct lttng_domain
*domain
)
522 struct lttng_handle
*handle
= NULL
;
524 handle
= zmalloc(sizeof(struct lttng_handle
));
525 if (handle
== NULL
) {
526 PERROR("malloc handle");
530 /* Copy session name */
531 lttng_ctl_copy_string(handle
->session_name
, session_name
,
532 sizeof(handle
->session_name
));
534 /* Copy lttng domain or leave initialized to 0. */
536 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
544 * Destroy handle by free(3) the pointer.
546 void lttng_destroy_handle(struct lttng_handle
*handle
)
552 * Register an outside consumer.
554 * Returns size of returned session payload data or a negative error code.
556 int lttng_register_consumer(struct lttng_handle
*handle
,
557 const char *socket_path
)
559 struct lttcomm_session_msg lsm
;
561 if (handle
== NULL
|| socket_path
== NULL
) {
562 return -LTTNG_ERR_INVALID
;
565 memset(&lsm
, 0, sizeof(lsm
));
566 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
567 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
568 sizeof(lsm
.session
.name
));
569 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
571 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
572 sizeof(lsm
.u
.reg
.path
));
574 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
578 * Start tracing for all traces of the session.
580 * Returns size of returned session payload data or a negative error code.
582 int lttng_start_tracing(const char *session_name
)
584 struct lttcomm_session_msg lsm
;
586 if (session_name
== NULL
) {
587 return -LTTNG_ERR_INVALID
;
590 memset(&lsm
, 0, sizeof(lsm
));
591 lsm
.cmd_type
= LTTNG_START_TRACE
;
593 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
594 sizeof(lsm
.session
.name
));
596 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
600 * Stop tracing for all traces of the session.
602 static int _lttng_stop_tracing(const char *session_name
, int wait
)
605 struct lttcomm_session_msg lsm
;
607 if (session_name
== NULL
) {
608 return -LTTNG_ERR_INVALID
;
611 memset(&lsm
, 0, sizeof(lsm
));
612 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
614 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
615 sizeof(lsm
.session
.name
));
617 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
618 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
626 /* Check for data availability */
628 data_ret
= lttng_data_pending(session_name
);
630 /* Return the data available call error. */
636 * Data sleep time before retrying (in usec). Don't sleep if the
637 * call returned value indicates availability.
640 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
642 } while (data_ret
!= 0);
650 * Stop tracing and wait for data availability.
652 int lttng_stop_tracing(const char *session_name
)
654 return _lttng_stop_tracing(session_name
, 1);
658 * Stop tracing but _don't_ wait for data availability.
660 int lttng_stop_tracing_no_wait(const char *session_name
)
662 return _lttng_stop_tracing(session_name
, 0);
666 * Add context to a channel.
668 * If the given channel is NULL, add the contexts to all channels.
669 * The event_name param is ignored.
671 * Returns the size of the returned payload data or a negative error code.
673 int lttng_add_context(struct lttng_handle
*handle
,
674 struct lttng_event_context
*ctx
, const char *event_name
,
675 const char *channel_name
)
680 struct lttcomm_session_msg lsm
;
682 /* Safety check. Both are mandatory. */
683 if (handle
== NULL
|| ctx
== NULL
) {
684 ret
= -LTTNG_ERR_INVALID
;
688 memset(&lsm
, 0, sizeof(lsm
));
689 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
691 /* If no channel name, send empty string. */
692 if (channel_name
== NULL
) {
693 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
694 sizeof(lsm
.u
.context
.channel_name
));
696 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
697 sizeof(lsm
.u
.context
.channel_name
));
700 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
701 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
702 sizeof(lsm
.session
.name
));
704 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
705 size_t provider_len
, ctx_len
;
706 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
707 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
709 if (!provider_name
|| !ctx_name
) {
710 ret
= -LTTNG_ERR_INVALID
;
714 provider_len
= strlen(provider_name
);
715 if (provider_len
== 0) {
716 ret
= -LTTNG_ERR_INVALID
;
719 lsm
.u
.context
.provider_name_len
= provider_len
;
721 ctx_len
= strlen(ctx_name
);
723 ret
= -LTTNG_ERR_INVALID
;
726 lsm
.u
.context
.context_name_len
= ctx_len
;
728 len
= provider_len
+ ctx_len
;
731 ret
= -LTTNG_ERR_NOMEM
;
735 memcpy(buf
, provider_name
, provider_len
);
736 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
738 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
740 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
742 * Don't leak application addresses to the sessiond.
743 * This is only necessary when ctx is for an app ctx otherwise
744 * the values inside the union (type & config) are overwritten.
746 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
747 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
750 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
757 * Enable event(s) for a channel.
759 * If no event name is specified, all events are enabled.
760 * If no channel name is specified, the default 'channel0' is used.
762 * Returns size of returned session payload data or a negative error code.
764 int lttng_enable_event(struct lttng_handle
*handle
,
765 struct lttng_event
*ev
, const char *channel_name
)
767 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
772 * Create or enable an event with a filter expression.
774 * Return negative error value on error.
775 * Return size of returned session payload data if OK.
777 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
778 struct lttng_event
*event
, const char *channel_name
,
779 const char *filter_expression
)
781 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
782 filter_expression
, 0, NULL
);
786 * Depending on the event, return a newly allocated agent filter expression or
787 * NULL if not applicable.
789 * An event with NO loglevel and the name is * will return NULL.
791 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
794 char *agent_filter
= NULL
;
798 /* Don't add filter for the '*' event. */
799 if (ev
->name
[0] != '*') {
801 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
804 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
812 /* Add loglevel filtering if any for the JUL domain. */
813 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
816 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
822 if (filter
|| agent_filter
) {
825 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
826 agent_filter
? agent_filter
: filter
, op
,
831 agent_filter
= new_filter
;
833 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
849 * Generate the filter bytecode from a given filter expression string. Put the
850 * newly allocated parser context in ctxp and populate the lsm object with the
853 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
855 static int generate_filter(char *filter_expression
,
856 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
859 struct filter_parser_ctx
*ctx
= NULL
;
862 assert(filter_expression
);
867 * Casting const to non-const, as the underlying function will use it in
870 fmem
= lttng_fmemopen((void *) filter_expression
,
871 strlen(filter_expression
), "r");
873 fprintf(stderr
, "Error opening memory as stream\n");
874 ret
= -LTTNG_ERR_FILTER_NOMEM
;
877 ctx
= filter_parser_ctx_alloc(fmem
);
879 fprintf(stderr
, "Error allocating parser\n");
880 ret
= -LTTNG_ERR_FILTER_NOMEM
;
881 goto filter_alloc_error
;
883 ret
= filter_parser_ctx_append_ast(ctx
);
885 fprintf(stderr
, "Parse error\n");
886 ret
= -LTTNG_ERR_FILTER_INVAL
;
889 ret
= filter_visitor_set_parent(ctx
);
891 fprintf(stderr
, "Set parent error\n");
892 ret
= -LTTNG_ERR_FILTER_INVAL
;
896 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
899 fprintf(stderr
, "XML print error\n");
900 ret
= -LTTNG_ERR_FILTER_INVAL
;
905 dbg_printf("Generating IR... ");
907 ret
= filter_visitor_ir_generate(ctx
);
909 fprintf(stderr
, "Generate IR error\n");
910 ret
= -LTTNG_ERR_FILTER_INVAL
;
913 dbg_printf("done\n");
915 dbg_printf("Validating IR... ");
917 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
919 ret
= -LTTNG_ERR_FILTER_INVAL
;
922 /* Validate strings used as literals in the expression. */
923 ret
= filter_visitor_ir_validate_string(ctx
);
925 ret
= -LTTNG_ERR_FILTER_INVAL
;
928 dbg_printf("done\n");
930 dbg_printf("Generating bytecode... ");
932 ret
= filter_visitor_bytecode_generate(ctx
);
934 fprintf(stderr
, "Generate bytecode error\n");
935 ret
= -LTTNG_ERR_FILTER_INVAL
;
938 dbg_printf("done\n");
939 dbg_printf("Size of bytecode generated: %u bytes.\n",
940 bytecode_get_len(&ctx
->bytecode
->b
));
942 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
943 + bytecode_get_len(&ctx
->bytecode
->b
);
944 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
946 /* No need to keep the memory stream. */
947 if (fclose(fmem
) != 0) {
956 filter_parser_ctx_free(ctx
);
958 if (fclose(fmem
) != 0) {
966 * Enable event(s) for a channel, possibly with exclusions and a filter.
967 * If no event name is specified, all events are enabled.
968 * If no channel name is specified, the default name is used.
969 * If filter expression is not NULL, the filter is set for the event.
970 * If exclusion count is not zero, the exclusions are set for the event.
971 * Returns size of returned session payload data or a negative error code.
973 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
974 struct lttng_event
*ev
, const char *channel_name
,
975 const char *original_filter_expression
,
976 int exclusion_count
, char **exclusion_list
)
978 struct lttcomm_session_msg lsm
;
981 unsigned int free_filter_expression
= 0;
982 struct filter_parser_ctx
*ctx
= NULL
;
984 * Cast as non-const since we may replace the filter expression
985 * by a dynamically allocated string. Otherwise, the original
986 * string is not modified.
988 char *filter_expression
= (char *) original_filter_expression
;
990 if (handle
== NULL
|| ev
== NULL
) {
991 ret
= -LTTNG_ERR_INVALID
;
996 * Empty filter string will always be rejected by the parser
997 * anyway, so treat this corner-case early to eliminate
998 * lttng_fmemopen error for 0-byte allocation.
1000 if (filter_expression
&& filter_expression
[0] == '\0') {
1001 ret
= -LTTNG_ERR_INVALID
;
1005 memset(&lsm
, 0, sizeof(lsm
));
1007 /* If no channel name, send empty string. */
1008 if (channel_name
== NULL
) {
1009 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1010 sizeof(lsm
.u
.enable
.channel_name
));
1012 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1013 sizeof(lsm
.u
.enable
.channel_name
));
1016 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1017 if (ev
->name
[0] == '\0') {
1018 /* Enable all events */
1019 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1022 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1023 /* FIXME: copying non-packed struct to packed struct. */
1024 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1026 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1027 sizeof(lsm
.session
.name
));
1028 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1029 lsm
.u
.enable
.bytecode_len
= 0;
1032 * For the JUL domain, a filter is enforced except for the enable all
1033 * event. This is done to avoid having the event in all sessions thus
1034 * filtering by logger name.
1036 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1037 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1038 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1039 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1044 * We have either a filter or some exclusions, so we need to set up
1045 * a variable-length memory block from where to send the data.
1048 /* Parse filter expression. */
1049 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1050 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1051 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1052 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1053 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1054 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1057 /* Setup JUL filter if needed. */
1058 agent_filter
= set_agent_filter(filter_expression
, ev
);
1059 if (!agent_filter
) {
1060 if (!filter_expression
) {
1062 * No JUL and no filter, just skip
1069 * With an agent filter, the original filter has
1070 * been added to it thus replace the filter
1073 filter_expression
= agent_filter
;
1074 free_filter_expression
= 1;
1078 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1084 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1085 + lsm
.u
.enable
.expression_len
1086 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1088 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1092 /* Put exclusion names first in the data. */
1093 while (exclusion_count
--) {
1094 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1095 *(exclusion_list
+ exclusion_count
),
1096 LTTNG_SYMBOL_NAME_LEN
- 1);
1098 /* Add filter expression next. */
1099 if (lsm
.u
.enable
.expression_len
!= 0) {
1101 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1103 lsm
.u
.enable
.expression_len
);
1105 /* Add filter bytecode next. */
1106 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1108 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1109 + lsm
.u
.enable
.expression_len
,
1111 lsm
.u
.enable
.bytecode_len
);
1114 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1115 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1116 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1121 if (filter_expression
&& ctx
) {
1122 filter_bytecode_free(ctx
);
1123 filter_ir_free(ctx
);
1124 filter_parser_ctx_free(ctx
);
1127 if (free_filter_expression
) {
1129 * The filter expression has been replaced and must be freed as
1130 * it is not the original filter expression received as a
1133 free(filter_expression
);
1137 * Return directly to the caller and don't ask the sessiond since
1138 * something went wrong in the parsing of data above.
1143 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1147 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1148 struct lttng_event
*ev
, const char *channel_name
,
1149 const char *original_filter_expression
)
1151 struct lttcomm_session_msg lsm
;
1154 unsigned int free_filter_expression
= 0;
1155 struct filter_parser_ctx
*ctx
= NULL
;
1157 * Cast as non-const since we may replace the filter expression
1158 * by a dynamically allocated string. Otherwise, the original
1159 * string is not modified.
1161 char *filter_expression
= (char *) original_filter_expression
;
1163 if (handle
== NULL
|| ev
== NULL
) {
1164 ret
= -LTTNG_ERR_INVALID
;
1169 * Empty filter string will always be rejected by the parser
1170 * anyway, so treat this corner-case early to eliminate
1171 * lttng_fmemopen error for 0-byte allocation.
1173 if (filter_expression
&& filter_expression
[0] == '\0') {
1174 ret
= -LTTNG_ERR_INVALID
;
1178 memset(&lsm
, 0, sizeof(lsm
));
1180 /* If no channel name, send empty string. */
1181 if (channel_name
== NULL
) {
1182 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1183 sizeof(lsm
.u
.disable
.channel_name
));
1185 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1186 sizeof(lsm
.u
.disable
.channel_name
));
1189 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1191 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1192 /* FIXME: copying non-packed struct to packed struct. */
1193 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1195 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1196 sizeof(lsm
.session
.name
));
1197 lsm
.u
.disable
.bytecode_len
= 0;
1200 * For the JUL domain, a filter is enforced except for the
1201 * disable all event. This is done to avoid having the event in
1202 * all sessions thus filtering by logger name.
1204 if (filter_expression
== NULL
&&
1205 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1206 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1207 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1212 * We have a filter, so we need to set up a variable-length
1213 * memory block from where to send the data.
1216 /* Parse filter expression */
1217 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1218 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1219 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1220 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1221 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1222 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1225 /* Setup JUL filter if needed. */
1226 agent_filter
= set_agent_filter(filter_expression
, ev
);
1227 if (!agent_filter
) {
1228 if (!filter_expression
) {
1230 * No JUL and no filter, just skip
1237 * With a JUL filter, the original filter has
1238 * been added to it thus replace the filter
1241 filter_expression
= agent_filter
;
1242 free_filter_expression
= 1;
1246 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1252 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1253 + lsm
.u
.disable
.expression_len
);
1255 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1259 /* Add filter expression. */
1260 if (lsm
.u
.disable
.expression_len
!= 0) {
1263 lsm
.u
.disable
.expression_len
);
1265 /* Add filter bytecode next. */
1266 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1268 + lsm
.u
.disable
.expression_len
,
1270 lsm
.u
.disable
.bytecode_len
);
1273 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1274 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1278 if (filter_expression
&& ctx
) {
1279 filter_bytecode_free(ctx
);
1280 filter_ir_free(ctx
);
1281 filter_parser_ctx_free(ctx
);
1284 if (free_filter_expression
) {
1286 * The filter expression has been replaced and must be freed as
1287 * it is not the original filter expression received as a
1290 free(filter_expression
);
1294 * Return directly to the caller and don't ask the sessiond since
1295 * something went wrong in the parsing of data above.
1300 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1305 * Disable event(s) of a channel and domain.
1306 * If no event name is specified, all events are disabled.
1307 * If no channel name is specified, the default 'channel0' is used.
1308 * Returns size of returned session payload data or a negative error code.
1310 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1311 const char *channel_name
)
1313 struct lttng_event ev
;
1315 memset(&ev
, 0, sizeof(ev
));
1317 ev
.type
= LTTNG_EVENT_ALL
;
1318 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1319 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1323 * Enable channel per domain
1324 * Returns size of returned session payload data or a negative error code.
1326 int lttng_enable_channel(struct lttng_handle
*handle
,
1327 struct lttng_channel
*chan
)
1329 struct lttcomm_session_msg lsm
;
1331 /* NULL arguments are forbidden. No default values. */
1332 if (handle
== NULL
|| chan
== NULL
) {
1333 return -LTTNG_ERR_INVALID
;
1336 memset(&lsm
, 0, sizeof(lsm
));
1338 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1340 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1342 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1344 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1345 sizeof(lsm
.session
.name
));
1347 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1351 * All tracing will be stopped for registered events of the channel.
1352 * Returns size of returned session payload data or a negative error code.
1354 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1356 struct lttcomm_session_msg lsm
;
1358 /* Safety check. Both are mandatory. */
1359 if (handle
== NULL
|| name
== NULL
) {
1360 return -LTTNG_ERR_INVALID
;
1363 memset(&lsm
, 0, sizeof(lsm
));
1365 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1367 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1368 sizeof(lsm
.u
.disable
.channel_name
));
1370 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1372 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1373 sizeof(lsm
.session
.name
));
1375 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1379 * Add PID to session tracker.
1380 * Return 0 on success else a negative LTTng error code.
1382 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1384 struct lttcomm_session_msg lsm
;
1386 /* NULL arguments are forbidden. No default values. */
1387 if (handle
== NULL
) {
1388 return -LTTNG_ERR_INVALID
;
1391 memset(&lsm
, 0, sizeof(lsm
));
1393 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1394 lsm
.u
.pid_tracker
.pid
= pid
;
1396 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1398 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1399 sizeof(lsm
.session
.name
));
1401 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1405 * Remove PID from session tracker.
1406 * Return 0 on success else a negative LTTng error code.
1408 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1410 struct lttcomm_session_msg lsm
;
1412 /* NULL arguments are forbidden. No default values. */
1413 if (handle
== NULL
) {
1414 return -LTTNG_ERR_INVALID
;
1417 memset(&lsm
, 0, sizeof(lsm
));
1419 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1420 lsm
.u
.pid_tracker
.pid
= pid
;
1422 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1424 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1425 sizeof(lsm
.session
.name
));
1427 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1431 * Lists all available tracepoints of domain.
1432 * Sets the contents of the events array.
1433 * Returns the number of lttng_event entries in events;
1434 * on error, returns a negative value.
1436 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1437 struct lttng_event
**events
)
1440 struct lttcomm_session_msg lsm
;
1442 if (handle
== NULL
) {
1443 return -LTTNG_ERR_INVALID
;
1446 memset(&lsm
, 0, sizeof(lsm
));
1447 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1448 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1450 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1455 return ret
/ sizeof(struct lttng_event
);
1459 * Lists all available tracepoint fields of domain.
1460 * Sets the contents of the event field array.
1461 * Returns the number of lttng_event_field entries in events;
1462 * on error, returns a negative value.
1464 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1465 struct lttng_event_field
**fields
)
1468 struct lttcomm_session_msg lsm
;
1470 if (handle
== NULL
) {
1471 return -LTTNG_ERR_INVALID
;
1474 memset(&lsm
, 0, sizeof(lsm
));
1475 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1476 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1478 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1483 return ret
/ sizeof(struct lttng_event_field
);
1487 * Lists all available kernel system calls. Allocates and sets the contents of
1490 * Returns the number of lttng_event entries in events; on error, returns a
1493 int lttng_list_syscalls(struct lttng_event
**events
)
1496 struct lttcomm_session_msg lsm
;
1499 return -LTTNG_ERR_INVALID
;
1502 memset(&lsm
, 0, sizeof(lsm
));
1503 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1504 /* Force kernel domain for system calls. */
1505 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1507 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1512 return ret
/ sizeof(struct lttng_event
);
1516 * Returns a human readable string describing
1517 * the error code (a negative value).
1519 const char *lttng_strerror(int code
)
1521 return error_get_str(code
);
1525 * Create a brand new session using name and url for destination.
1527 * Returns LTTNG_OK on success or a negative error code.
1529 int lttng_create_session(const char *name
, const char *url
)
1533 struct lttcomm_session_msg lsm
;
1534 struct lttng_uri
*uris
= NULL
;
1537 return -LTTNG_ERR_INVALID
;
1540 memset(&lsm
, 0, sizeof(lsm
));
1542 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1543 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1545 /* There should never be a data URL */
1546 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1548 return -LTTNG_ERR_INVALID
;
1551 lsm
.u
.uri
.size
= size
;
1553 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1554 sizeof(struct lttng_uri
) * size
, NULL
);
1561 * Destroy session using name.
1562 * Returns size of returned session payload data or a negative error code.
1565 int _lttng_destroy_session(const char *session_name
)
1567 struct lttcomm_session_msg lsm
;
1569 if (session_name
== NULL
) {
1570 return -LTTNG_ERR_INVALID
;
1573 memset(&lsm
, 0, sizeof(lsm
));
1574 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1576 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1577 sizeof(lsm
.session
.name
));
1579 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1583 * Stop the session and wait for the data before destroying it
1585 int lttng_destroy_session(const char *session_name
)
1590 * Stop the tracing and wait for the data.
1592 ret
= _lttng_stop_tracing(session_name
, 1);
1593 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1597 ret
= _lttng_destroy_session(session_name
);
1603 * Destroy the session without waiting for the data.
1605 int lttng_destroy_session_no_wait(const char *session_name
)
1610 * Stop the tracing without waiting for the data.
1611 * The session might already have been stopped, so just
1614 ret
= _lttng_stop_tracing(session_name
, 0);
1615 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1619 ret
= _lttng_destroy_session(session_name
);
1625 * Ask the session daemon for all available sessions.
1626 * Sets the contents of the sessions array.
1627 * Returns the number of lttng_session entries in sessions;
1628 * on error, returns a negative value.
1630 int lttng_list_sessions(struct lttng_session
**sessions
)
1633 struct lttcomm_session_msg lsm
;
1635 memset(&lsm
, 0, sizeof(lsm
));
1636 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1637 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1642 return ret
/ sizeof(struct lttng_session
);
1645 int lttng_set_session_shm_path(const char *session_name
,
1646 const char *shm_path
)
1648 struct lttcomm_session_msg lsm
;
1650 if (session_name
== NULL
) {
1651 return -LTTNG_ERR_INVALID
;
1654 memset(&lsm
, 0, sizeof(lsm
));
1655 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1657 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1658 sizeof(lsm
.session
.name
));
1659 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1660 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1662 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1666 * Ask the session daemon for all available domains of a session.
1667 * Sets the contents of the domains array.
1668 * Returns the number of lttng_domain entries in domains;
1669 * on error, returns a negative value.
1671 int lttng_list_domains(const char *session_name
,
1672 struct lttng_domain
**domains
)
1675 struct lttcomm_session_msg lsm
;
1677 if (session_name
== NULL
) {
1678 return -LTTNG_ERR_INVALID
;
1681 memset(&lsm
, 0, sizeof(lsm
));
1682 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1684 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1685 sizeof(lsm
.session
.name
));
1687 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1692 return ret
/ sizeof(struct lttng_domain
);
1696 * Ask the session daemon for all available channels of a session.
1697 * Sets the contents of the channels array.
1698 * Returns the number of lttng_channel entries in channels;
1699 * on error, returns a negative value.
1701 int lttng_list_channels(struct lttng_handle
*handle
,
1702 struct lttng_channel
**channels
)
1705 size_t channel_count
, i
;
1706 const size_t channel_size
= sizeof(struct lttng_channel
) +
1707 sizeof(struct lttcomm_channel_extended
);
1708 struct lttcomm_session_msg lsm
;
1711 if (handle
== NULL
) {
1712 ret
= -LTTNG_ERR_INVALID
;
1716 memset(&lsm
, 0, sizeof(lsm
));
1717 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1718 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1719 sizeof(lsm
.session
.name
));
1721 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1723 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1728 if (ret
% channel_size
) {
1729 ret
= -LTTNG_ERR_UNK
;
1734 channel_count
= (size_t) ret
/ channel_size
;
1736 /* Set extended info pointers */
1737 extended_at
= ((void *) *channels
) +
1738 channel_count
* sizeof(struct lttng_channel
);
1739 for (i
= 0; i
< channel_count
; i
++) {
1740 struct lttng_channel
*chan
= &(*channels
)[i
];
1742 chan
->attr
.extended
.ptr
= extended_at
;
1743 extended_at
+= sizeof(struct lttcomm_channel_extended
);
1746 ret
= (int) channel_count
;
1752 * Ask the session daemon for all available events of a session channel.
1753 * Sets the contents of the events array.
1754 * Returns the number of lttng_event entries in events;
1755 * on error, returns a negative value.
1757 int lttng_list_events(struct lttng_handle
*handle
,
1758 const char *channel_name
, struct lttng_event
**events
)
1761 struct lttcomm_session_msg lsm
;
1762 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1763 size_t cmd_header_len
;
1764 uint32_t nb_events
, i
;
1767 /* Safety check. An handle and channel name are mandatory */
1768 if (handle
== NULL
|| channel_name
== NULL
) {
1769 return -LTTNG_ERR_INVALID
;
1772 memset(&lsm
, 0, sizeof(lsm
));
1773 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1774 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1775 sizeof(lsm
.session
.name
));
1776 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1777 sizeof(lsm
.u
.list
.channel_name
));
1778 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1780 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) events
,
1781 (void **) &cmd_header
, &cmd_header_len
);
1786 /* Set number of events and free command header */
1787 nb_events
= cmd_header
->nb_events
;
1788 if (nb_events
> INT_MAX
) {
1792 ret
= (int) nb_events
;
1796 /* Set extended info pointers */
1797 extended_at
= ((void*) (*events
)) +
1798 nb_events
* sizeof(struct lttng_event
);
1800 for (i
= 0; i
< nb_events
; i
++) {
1801 struct lttcomm_event_extended_header
*ext_header
;
1802 struct lttng_event
*event
= &(*events
)[i
];
1804 event
->extended
.ptr
= extended_at
;
1806 (struct lttcomm_event_extended_header
*) extended_at
;
1807 extended_at
+= sizeof(*ext_header
);
1808 extended_at
+= ext_header
->filter_len
;
1810 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1820 int lttng_event_get_filter_expression(struct lttng_event
*event
,
1821 const char **filter_expression
)
1824 struct lttcomm_event_extended_header
*ext_header
;
1826 if (!event
|| !filter_expression
) {
1827 ret
= -LTTNG_ERR_INVALID
;
1831 ext_header
= event
->extended
.ptr
;
1835 * This can happen since the lttng_event structure is
1836 * used for other tasks where this pointer is never set.
1838 *filter_expression
= NULL
;
1842 if (ext_header
->filter_len
) {
1843 *filter_expression
= ((const char *) (ext_header
)) +
1844 sizeof(*ext_header
);
1846 *filter_expression
= NULL
;
1853 int lttng_event_get_exclusion_name_count(struct lttng_event
*event
)
1856 struct lttcomm_event_extended_header
*ext_header
;
1859 ret
= -LTTNG_ERR_INVALID
;
1863 ext_header
= event
->extended
.ptr
;
1866 * This can happen since the lttng_event structure is
1867 * used for other tasks where this pointer is never set.
1873 if (ext_header
->nb_exclusions
> INT_MAX
) {
1874 ret
= -LTTNG_ERR_OVERFLOW
;
1877 ret
= (int) ext_header
->nb_exclusions
;
1882 int lttng_event_get_exclusion_name(struct lttng_event
*event
,
1883 size_t index
, const char **exclusion_name
)
1886 struct lttcomm_event_extended_header
*ext_header
;
1889 if (!event
|| !exclusion_name
) {
1890 ret
= -LTTNG_ERR_INVALID
;
1894 ext_header
= event
->extended
.ptr
;
1896 ret
= -LTTNG_ERR_INVALID
;
1900 if (index
>= ext_header
->nb_exclusions
) {
1901 ret
= -LTTNG_ERR_INVALID
;
1905 at
= (void *) ext_header
+ sizeof(*ext_header
);
1906 at
+= ext_header
->filter_len
;
1907 at
+= index
* LTTNG_SYMBOL_NAME_LEN
;
1908 *exclusion_name
= at
;
1915 * Sets the tracing_group variable with name.
1916 * This function allocates memory pointed to by tracing_group.
1917 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1919 int lttng_set_tracing_group(const char *name
)
1922 return -LTTNG_ERR_INVALID
;
1925 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1926 return -LTTNG_ERR_FATAL
;
1932 int lttng_calibrate(struct lttng_handle
*handle
,
1933 struct lttng_calibrate
*calibrate
)
1936 * This command was removed in LTTng 2.9.
1938 return -LTTNG_ERR_UND
;
1942 * Set default channel attributes.
1943 * If either or both of the arguments are null, attr content is zeroe'd.
1945 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1946 struct lttng_channel_attr
*attr
)
1949 if (attr
== NULL
|| domain
== NULL
) {
1953 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1955 /* Same for all domains. */
1956 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1957 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1958 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1960 switch (domain
->type
) {
1961 case LTTNG_DOMAIN_KERNEL
:
1962 attr
->switch_timer_interval
=
1963 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1964 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1965 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1966 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1967 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1969 case LTTNG_DOMAIN_UST
:
1970 switch (domain
->buf_type
) {
1971 case LTTNG_BUFFER_PER_UID
:
1972 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1973 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1974 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1975 attr
->switch_timer_interval
=
1976 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1977 attr
->read_timer_interval
=
1978 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1980 case LTTNG_BUFFER_PER_PID
:
1982 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1983 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1984 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1985 attr
->switch_timer_interval
=
1986 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1987 attr
->read_timer_interval
=
1988 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1992 /* Default behavior: leave set to 0. */
1997 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
1998 uint64_t *discarded_events
)
2001 struct lttcomm_channel_extended
*chan_ext
;
2003 if (!channel
|| !discarded_events
) {
2004 ret
= -LTTNG_ERR_INVALID
;
2008 chan_ext
= channel
->attr
.extended
.ptr
;
2011 * This can happen since the lttng_channel structure is
2012 * used for other tasks where this pointer is never set.
2014 *discarded_events
= 0;
2018 *discarded_events
= chan_ext
->discarded_events
;
2023 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2024 uint64_t *lost_packets
)
2027 struct lttcomm_channel_extended
*chan_ext
;
2029 if (!channel
|| !lost_packets
) {
2030 ret
= -LTTNG_ERR_INVALID
;
2034 chan_ext
= channel
->attr
.extended
.ptr
;
2037 * This can happen since the lttng_channel structure is
2038 * used for other tasks where this pointer is never set.
2044 *lost_packets
= chan_ext
->lost_packets
;
2050 * Check if session daemon is alive.
2052 * Return 1 if alive or 0 if not.
2053 * On error returns a negative value.
2055 int lttng_session_daemon_alive(void)
2059 ret
= set_session_daemon_path();
2065 if (*sessiond_sock_path
== '\0') {
2067 * No socket path set. Weird error which means the constructor
2073 ret
= try_connect_sessiond(sessiond_sock_path
);
2084 * Set URL for a consumer for a session and domain.
2086 * Return 0 on success, else a negative value.
2088 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2089 const char *control_url
, const char *data_url
)
2093 struct lttcomm_session_msg lsm
;
2094 struct lttng_uri
*uris
= NULL
;
2096 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2097 return -LTTNG_ERR_INVALID
;
2100 memset(&lsm
, 0, sizeof(lsm
));
2102 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2104 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2105 sizeof(lsm
.session
.name
));
2106 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2108 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2110 return -LTTNG_ERR_INVALID
;
2113 lsm
.u
.uri
.size
= size
;
2115 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2116 sizeof(struct lttng_uri
) * size
, NULL
);
2125 int lttng_enable_consumer(struct lttng_handle
*handle
)
2133 int lttng_disable_consumer(struct lttng_handle
*handle
)
2139 * This is an extension of create session that is ONLY and SHOULD only be used
2140 * by the lttng command line program. It exists to avoid using URI parsing in
2143 * We need the date and time for the trace path subdirectory for the case where
2144 * the user does NOT define one using either -o or -U. Using the normal
2145 * lttng_create_session API call, we have no clue on the session daemon side if
2146 * the URL was generated automatically by the client or define by the user.
2148 * So this function "wrapper" is hidden from the public API, takes the datetime
2149 * string and appends it if necessary to the URI subdirectory before sending it
2150 * to the session daemon.
2152 * With this extra function, the lttng_create_session call behavior is not
2153 * changed and the timestamp is appended to the URI on the session daemon side
2156 int _lttng_create_session_ext(const char *name
, const char *url
,
2157 const char *datetime
)
2161 struct lttcomm_session_msg lsm
;
2162 struct lttng_uri
*uris
= NULL
;
2164 if (name
== NULL
|| datetime
== NULL
) {
2165 return -LTTNG_ERR_INVALID
;
2168 memset(&lsm
, 0, sizeof(lsm
));
2170 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2171 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2173 /* There should never be a data URL. */
2174 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2176 ret
= -LTTNG_ERR_INVALID
;
2180 lsm
.u
.uri
.size
= size
;
2182 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2183 /* Don't append datetime if the name was automatically created. */
2184 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2185 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2186 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2189 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2192 PERROR("snprintf uri subdir");
2193 ret
= -LTTNG_ERR_FATAL
;
2198 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2199 sizeof(struct lttng_uri
) * size
, NULL
);
2207 * For a given session name, this call checks if the data is ready to be read
2208 * or is still being extracted by the consumer(s) hence not ready to be used by
2211 int lttng_data_pending(const char *session_name
)
2214 struct lttcomm_session_msg lsm
;
2215 uint8_t *pending
= NULL
;
2217 if (session_name
== NULL
) {
2218 return -LTTNG_ERR_INVALID
;
2221 memset(&lsm
, 0, sizeof(lsm
));
2222 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2224 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2225 sizeof(lsm
.session
.name
));
2227 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2230 } else if (ret
!= 1) {
2231 /* Unexpected payload size */
2232 ret
= -LTTNG_ERR_INVALID
;
2236 ret
= (int) *pending
;
2243 * Create a session exclusively used for snapshot.
2245 * Returns LTTNG_OK on success or a negative error code.
2247 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2251 struct lttcomm_session_msg lsm
;
2252 struct lttng_uri
*uris
= NULL
;
2255 return -LTTNG_ERR_INVALID
;
2258 memset(&lsm
, 0, sizeof(lsm
));
2260 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2261 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2263 size
= uri_parse_str_urls(snapshot_url
, NULL
, &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
);
2278 * Create a session exclusively used for live.
2280 * Returns LTTNG_OK on success or a negative error code.
2282 int lttng_create_session_live(const char *name
, const char *url
,
2283 unsigned int timer_interval
)
2287 struct lttcomm_session_msg lsm
;
2288 struct lttng_uri
*uris
= NULL
;
2290 if (name
== NULL
|| timer_interval
== 0) {
2291 return -LTTNG_ERR_INVALID
;
2294 memset(&lsm
, 0, sizeof(lsm
));
2296 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2297 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2300 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2302 ret
= -LTTNG_ERR_INVALID
;
2306 /* file:// is not accepted for live session. */
2307 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2308 ret
= -LTTNG_ERR_INVALID
;
2315 lsm
.u
.session_live
.nb_uri
= size
;
2316 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2318 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2319 sizeof(struct lttng_uri
) * size
, NULL
);
2327 * List PIDs in the tracker.
2329 * enabled is set to whether the PID tracker is enabled.
2330 * pids is set to an allocated array of PIDs currently tracked. On
2331 * success, pids must be freed by the caller.
2332 * nr_pids is set to the number of entries contained by the pids array.
2334 * Returns 0 on success, else a negative LTTng error code.
2336 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2337 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2341 struct lttcomm_session_msg lsm
;
2345 if (handle
== NULL
) {
2346 return -LTTNG_ERR_INVALID
;
2349 memset(&lsm
, 0, sizeof(lsm
));
2350 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2351 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2352 sizeof(lsm
.session
.name
));
2353 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2355 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2359 nr_pids
= ret
/ sizeof(int32_t);
2360 if (nr_pids
== 1 && pids
[0] == -1) {
2366 *_enabled
= enabled
;
2368 *_nr_pids
= nr_pids
;
2373 * Regenerate the metadata for a session.
2374 * Return 0 on success, a negative error code on error.
2376 int lttng_regenerate_metadata(const char *session_name
)
2379 struct lttcomm_session_msg lsm
;
2381 if (!session_name
) {
2382 ret
= -LTTNG_ERR_INVALID
;
2386 memset(&lsm
, 0, sizeof(lsm
));
2387 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2389 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2390 sizeof(lsm
.session
.name
));
2392 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2403 * Deprecated, replaced by lttng_regenerate_metadata.
2405 int lttng_metadata_regenerate(const char *session_name
)
2407 return lttng_regenerate_metadata(session_name
);
2411 * Regenerate the statedump of a session.
2412 * Return 0 on success, a negative error code on error.
2414 int lttng_regenerate_statedump(const char *session_name
)
2417 struct lttcomm_session_msg lsm
;
2419 if (!session_name
) {
2420 ret
= -LTTNG_ERR_INVALID
;
2424 memset(&lsm
, 0, sizeof(lsm
));
2425 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2427 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2428 sizeof(lsm
.session
.name
));
2430 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2440 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2443 struct lttcomm_session_msg lsm
;
2444 char *trigger_buf
= NULL
;
2445 ssize_t trigger_size
;
2448 ret
= -LTTNG_ERR_INVALID
;
2452 if (!lttng_trigger_validate(trigger
)) {
2453 ret
= -LTTNG_ERR_INVALID
;
2457 trigger_size
= lttng_trigger_serialize(trigger
, NULL
);
2458 if (trigger_size
< 0) {
2459 ret
= -LTTNG_ERR_UNK
;
2463 trigger_buf
= zmalloc(trigger_size
);
2465 ret
= -LTTNG_ERR_NOMEM
;
2469 memset(&lsm
, 0, sizeof(lsm
));
2470 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2471 if (lttng_trigger_serialize(trigger
, trigger_buf
) < 0) {
2472 ret
= -LTTNG_ERR_UNK
;
2476 lsm
.u
.trigger
.length
= (uint32_t) trigger_size
;
2477 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, trigger_buf
,
2478 trigger_size
, NULL
);
2487 static void __attribute__((constructor
)) init(void)
2489 /* Set default session group */
2490 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2496 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2498 free(tracing_group
);