4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * 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>
40 #include "filter/filter-ast.h"
41 #include "filter/filter-parser.h"
42 #include "filter/filter-bytecode.h"
43 #include "filter/memstream.h"
44 #include "lttng-ctl-helper.h"
47 static const int print_xml
= 1;
48 #define dbg_printf(fmt, args...) \
49 printf("[debug liblttng-ctl] " fmt, ## args)
51 static const int print_xml
= 0;
52 #define dbg_printf(fmt, args...) \
54 /* do nothing but check printf format */ \
56 printf("[debug liblttnctl] " fmt, ## args); \
61 /* Socket to session daemon for communication */
62 static int sessiond_socket
;
63 static char sessiond_sock_path
[PATH_MAX
];
66 static char *tracing_group
;
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
77 int lttng_opt_verbose
;
81 * Copy string from src to dst and enforce null terminated byte.
84 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
87 strncpy(dst
, src
, len
);
88 /* Enforce the NULL terminated byte */
96 * Copy domain to lttcomm_session_msg domain.
98 * If domain is unknown, default domain will be the kernel.
101 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
102 struct lttng_domain
*src
)
106 case LTTNG_DOMAIN_KERNEL
:
107 case LTTNG_DOMAIN_UST
:
108 case LTTNG_DOMAIN_JUL
:
109 case LTTNG_DOMAIN_LOG4J
:
110 case LTTNG_DOMAIN_PYTHON
:
111 memcpy(dst
, src
, sizeof(struct lttng_domain
));
114 memset(dst
, 0, sizeof(struct lttng_domain
));
121 * Send lttcomm_session_msg to the session daemon.
123 * On success, returns the number of bytes sent (>=0)
124 * On error, returns -1
126 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
131 ret
= -LTTNG_ERR_NO_SESSIOND
;
135 DBG("LSM cmd type : %d", lsm
->cmd_type
);
137 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
138 sizeof(struct lttcomm_session_msg
));
140 ret
= -LTTNG_ERR_FATAL
;
148 * Send var len data to the session daemon.
150 * On success, returns the number of bytes sent (>=0)
151 * On error, returns -1
153 static int send_session_varlen(void *data
, size_t len
)
158 ret
= -LTTNG_ERR_NO_SESSIOND
;
167 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
169 ret
= -LTTNG_ERR_FATAL
;
177 * Receive data from the sessiond socket.
179 * On success, returns the number of bytes received (>=0)
180 * On error, returns -1 (recvmsg() error) or -ENOTCONN
182 static int recv_data_sessiond(void *buf
, size_t len
)
187 ret
= -LTTNG_ERR_NO_SESSIOND
;
191 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
193 ret
= -LTTNG_ERR_FATAL
;
201 * Check if we are in the specified group.
203 * If yes return 1, else return -1.
206 int lttng_check_tracing_group(void)
208 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
210 int grp_list_size
, grp_id
, i
;
212 const char *grp_name
= tracing_group
;
214 /* Get GID of group 'tracing' */
215 grp_tracing
= getgrnam(grp_name
);
217 /* If grp_tracing is NULL, the group does not exist. */
221 /* Get number of supplementary group IDs */
222 grp_list_size
= getgroups(0, NULL
);
223 if (grp_list_size
< 0) {
228 /* Alloc group list of the right size */
229 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
234 grp_id
= getgroups(grp_list_size
, grp_list
);
240 for (i
= 0; i
< grp_list_size
; i
++) {
241 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
255 * Try connect to session daemon with sock_path.
257 * Return 0 on success, else -1
259 static int try_connect_sessiond(const char *sock_path
)
263 /* If socket exist, we check if the daemon listens for connect. */
264 ret
= access(sock_path
, F_OK
);
270 ret
= lttcomm_connect_unix_sock(sock_path
);
276 ret
= lttcomm_close_unix_sock(ret
);
278 PERROR("lttcomm_close_unix_sock");
288 * Set sessiond socket path by putting it in the global sessiond_sock_path
291 * Returns 0 on success, negative value on failure (the sessiond socket path
292 * is somehow too long or ENOMEM).
294 static int set_session_daemon_path(void)
296 int in_tgroup
= 0; /* In tracing group */
302 /* Are we in the tracing group ? */
303 in_tgroup
= lttng_check_tracing_group();
306 if ((uid
== 0) || in_tgroup
) {
307 lttng_ctl_copy_string(sessiond_sock_path
,
308 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
316 ret
= try_connect_sessiond(sessiond_sock_path
);
320 /* Global session daemon not available... */
322 /* ...or not in tracing group (and not root), default */
325 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
326 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
328 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
329 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
330 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
342 * Connect to the LTTng session daemon.
344 * On success, return 0. On error, return -1.
346 static int connect_sessiond(void)
350 /* Don't try to connect if already connected. */
355 ret
= set_session_daemon_path();
360 /* Connect to the sesssion daemon */
361 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
366 sessiond_socket
= ret
;
376 * Clean disconnect from the session daemon.
377 * On success, return 0. On error, return -1.
379 static int disconnect_sessiond(void)
384 ret
= lttcomm_close_unix_sock(sessiond_socket
);
393 * Ask the session daemon a specific command and put the data into buf.
394 * Takes extra var. len. data as input to send to the session daemon.
396 * Return size of data (only payload, not header) or a negative error code.
399 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
400 void *vardata
, size_t varlen
, void **buf
)
405 struct lttcomm_lttng_msg llm
;
407 ret
= connect_sessiond();
409 ret
= -LTTNG_ERR_NO_SESSIOND
;
413 /* Send command to session daemon */
414 ret
= send_session_msg(lsm
);
416 /* Ret value is a valid lttng error code. */
419 /* Send var len data */
420 ret
= send_session_varlen(vardata
, varlen
);
422 /* Ret value is a valid lttng error code. */
426 /* Get header from data transmission */
427 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
429 /* Ret value is a valid lttng error code. */
433 /* Check error code if OK */
434 if (llm
.ret_code
!= LTTNG_OK
) {
439 size
= llm
.data_size
;
441 /* If client free with size 0 */
449 data
= zmalloc(size
);
455 /* Get payload data */
456 ret
= recv_data_sessiond(data
, size
);
463 * Extra protection not to dereference a NULL pointer. If buf is NULL at
464 * this point, an error is returned and data is freed.
467 ret
= -LTTNG_ERR_INVALID
;
476 disconnect_sessiond();
481 * Create lttng handle and return pointer.
482 * The returned pointer will be NULL in case of malloc() error.
484 struct lttng_handle
*lttng_create_handle(const char *session_name
,
485 struct lttng_domain
*domain
)
487 struct lttng_handle
*handle
= NULL
;
489 if (domain
== NULL
) {
493 handle
= zmalloc(sizeof(struct lttng_handle
));
494 if (handle
== NULL
) {
495 PERROR("malloc handle");
499 /* Copy session name */
500 lttng_ctl_copy_string(handle
->session_name
, session_name
,
501 sizeof(handle
->session_name
));
503 /* Copy lttng domain */
504 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
511 * Destroy handle by free(3) the pointer.
513 void lttng_destroy_handle(struct lttng_handle
*handle
)
519 * Register an outside consumer.
520 * Returns size of returned session payload data or a negative error code.
522 int lttng_register_consumer(struct lttng_handle
*handle
,
523 const char *socket_path
)
525 struct lttcomm_session_msg lsm
;
527 if (handle
== NULL
|| socket_path
== NULL
) {
528 return -LTTNG_ERR_INVALID
;
531 memset(&lsm
, 0, sizeof(lsm
));
532 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
533 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
534 sizeof(lsm
.session
.name
));
535 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
537 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
539 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
543 * Start tracing for all traces of the session.
544 * Returns size of returned session payload data or a negative error code.
546 int lttng_start_tracing(const char *session_name
)
548 struct lttcomm_session_msg lsm
;
550 if (session_name
== NULL
) {
551 return -LTTNG_ERR_INVALID
;
554 memset(&lsm
, 0, sizeof(lsm
));
555 lsm
.cmd_type
= LTTNG_START_TRACE
;
557 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
558 sizeof(lsm
.session
.name
));
560 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
564 * Stop tracing for all traces of the session.
566 static int _lttng_stop_tracing(const char *session_name
, int wait
)
569 struct lttcomm_session_msg lsm
;
571 if (session_name
== NULL
) {
572 return -LTTNG_ERR_INVALID
;
575 memset(&lsm
, 0, sizeof(lsm
));
576 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
578 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
579 sizeof(lsm
.session
.name
));
581 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
582 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
590 /* Check for data availability */
592 data_ret
= lttng_data_pending(session_name
);
594 /* Return the data available call error. */
600 * Data sleep time before retrying (in usec). Don't sleep if the call
601 * returned value indicates availability.
604 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
606 } while (data_ret
!= 0);
614 * Stop tracing and wait for data availability.
616 int lttng_stop_tracing(const char *session_name
)
618 return _lttng_stop_tracing(session_name
, 1);
622 * Stop tracing but _don't_ wait for data availability.
624 int lttng_stop_tracing_no_wait(const char *session_name
)
626 return _lttng_stop_tracing(session_name
, 0);
630 * Add context to a channel.
632 * If the given channel is NULL, add the contexts to all channels.
633 * The event_name param is ignored.
635 * Returns the size of the returned payload data or a negative error code.
637 int lttng_add_context(struct lttng_handle
*handle
,
638 struct lttng_event_context
*ctx
, const char *event_name
,
639 const char *channel_name
)
641 struct lttcomm_session_msg lsm
;
643 /* Safety check. Both are mandatory */
644 if (handle
== NULL
|| ctx
== NULL
) {
645 return -LTTNG_ERR_INVALID
;
648 memset(&lsm
, 0, sizeof(lsm
));
650 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
652 /* If no channel name, send empty string. */
653 if (channel_name
== NULL
) {
654 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
655 sizeof(lsm
.u
.context
.channel_name
));
657 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
658 sizeof(lsm
.u
.context
.channel_name
));
661 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
663 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
665 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
666 sizeof(lsm
.session
.name
));
668 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
672 * Enable event(s) for a channel.
673 * If no event name is specified, all events are enabled.
674 * If no channel name is specified, the default 'channel0' is used.
675 * Returns size of returned session payload data or a negative error code.
677 int lttng_enable_event(struct lttng_handle
*handle
,
678 struct lttng_event
*ev
, const char *channel_name
)
680 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
685 * Create or enable an event with a filter expression.
687 * Return negative error value on error.
688 * Return size of returned session payload data if OK.
690 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
691 struct lttng_event
*event
, const char *channel_name
,
692 const char *filter_expression
)
694 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
695 filter_expression
, 0, NULL
);
699 * Depending on the event, return a newly allocated agent filter expression or
700 * NULL if not applicable.
702 * An event with NO loglevel and the name is * will return NULL.
704 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
707 char *agent_filter
= NULL
;
711 /* Don't add filter for the '*' event. */
712 if (ev
->name
[0] != '*') {
714 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
717 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
725 /* Add loglevel filtering if any for the JUL domain. */
726 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
729 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
735 if (filter
|| agent_filter
) {
738 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
739 agent_filter
? agent_filter
: filter
, op
,
744 agent_filter
= new_filter
;
746 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
762 * Generate the filter bytecode from a give filter expression string. Put the
763 * newly allocated parser context in ctxp and populate the lsm object with the
766 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
768 static int generate_filter(char *filter_expression
,
769 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
772 struct filter_parser_ctx
*ctx
= NULL
;
775 assert(filter_expression
);
780 * Casting const to non-const, as the underlying function will use it in
783 fmem
= lttng_fmemopen((void *) filter_expression
,
784 strlen(filter_expression
), "r");
786 fprintf(stderr
, "Error opening memory as stream\n");
787 ret
= -LTTNG_ERR_FILTER_NOMEM
;
790 ctx
= filter_parser_ctx_alloc(fmem
);
792 fprintf(stderr
, "Error allocating parser\n");
793 ret
= -LTTNG_ERR_FILTER_NOMEM
;
794 goto filter_alloc_error
;
796 ret
= filter_parser_ctx_append_ast(ctx
);
798 fprintf(stderr
, "Parse error\n");
799 ret
= -LTTNG_ERR_FILTER_INVAL
;
802 ret
= filter_visitor_set_parent(ctx
);
804 fprintf(stderr
, "Set parent error\n");
805 ret
= -LTTNG_ERR_FILTER_INVAL
;
809 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
812 fprintf(stderr
, "XML print error\n");
813 ret
= -LTTNG_ERR_FILTER_INVAL
;
818 dbg_printf("Generating IR... ");
820 ret
= filter_visitor_ir_generate(ctx
);
822 fprintf(stderr
, "Generate IR error\n");
823 ret
= -LTTNG_ERR_FILTER_INVAL
;
826 dbg_printf("done\n");
828 dbg_printf("Validating IR... ");
830 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
832 ret
= -LTTNG_ERR_FILTER_INVAL
;
835 /* Validate strings used as literals in the expression */
836 ret
= filter_visitor_ir_validate_string(ctx
);
838 ret
= -LTTNG_ERR_FILTER_INVAL
;
841 dbg_printf("done\n");
843 dbg_printf("Generating bytecode... ");
845 ret
= filter_visitor_bytecode_generate(ctx
);
847 fprintf(stderr
, "Generate bytecode error\n");
848 ret
= -LTTNG_ERR_FILTER_INVAL
;
851 dbg_printf("done\n");
852 dbg_printf("Size of bytecode generated: %u bytes.\n",
853 bytecode_get_len(&ctx
->bytecode
->b
));
855 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
856 + bytecode_get_len(&ctx
->bytecode
->b
);
857 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
859 /* No need to keep the memory stream. */
860 if (fclose(fmem
) != 0) {
869 filter_parser_ctx_free(ctx
);
871 if (fclose(fmem
) != 0) {
879 * Enable event(s) for a channel, possibly with exclusions and a filter.
880 * If no event name is specified, all events are enabled.
881 * If no channel name is specified, the default name is used.
882 * If filter expression is not NULL, the filter is set for the event.
883 * If exclusion count is not zero, the exclusions are set for the event.
884 * Returns size of returned session payload data or a negative error code.
886 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
887 struct lttng_event
*ev
, const char *channel_name
,
888 const char *original_filter_expression
,
889 int exclusion_count
, char **exclusion_list
)
891 struct lttcomm_session_msg lsm
;
894 unsigned int free_filter_expression
= 0;
895 struct filter_parser_ctx
*ctx
= NULL
;
897 * Cast as non-const since we may replace the filter expression
898 * by a dynamically allocated string. Otherwise, the original
899 * string is not modified.
901 char *filter_expression
= (char *) original_filter_expression
;
903 if (handle
== NULL
|| ev
== NULL
) {
904 ret
= -LTTNG_ERR_INVALID
;
908 /* Empty filter string will always be rejected by the parser
909 * anyway, so treat this corner-case early to eliminate
910 * lttng_fmemopen error for 0-byte allocation.
912 if (filter_expression
&& filter_expression
[0] == '\0') {
913 ret
= -LTTNG_ERR_INVALID
;
917 memset(&lsm
, 0, sizeof(lsm
));
919 /* If no channel name, send empty string. */
920 if (channel_name
== NULL
) {
921 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
922 sizeof(lsm
.u
.enable
.channel_name
));
924 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
925 sizeof(lsm
.u
.enable
.channel_name
));
928 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
929 if (ev
->name
[0] == '\0') {
930 /* Enable all events */
931 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
934 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
935 /* FIXME: copying non-packed struct to packed struct. */
936 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
938 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
939 sizeof(lsm
.session
.name
));
940 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
941 lsm
.u
.enable
.bytecode_len
= 0;
944 * For the JUL domain, a filter is enforced except for the enable all
945 * event. This is done to avoid having the event in all sessions thus
946 * filtering by logger name.
948 if (exclusion_count
== 0 && filter_expression
== NULL
&&
949 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
950 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
951 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
956 * We have either a filter or some exclusions, so we need to set up
957 * a variable-length memory block from where to send the data
960 /* Parse filter expression */
961 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
962 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
963 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
964 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
965 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
966 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
969 /* Setup JUL filter if needed. */
970 agent_filter
= set_agent_filter(filter_expression
, ev
);
972 if (!filter_expression
) {
973 /* No JUL and no filter, just skip everything below. */
978 * With an agent filter, the original filter has been added to
979 * it thus replace the filter expression.
981 filter_expression
= agent_filter
;
982 free_filter_expression
= 1;
986 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
992 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
993 + lsm
.u
.enable
.expression_len
994 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
996 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1000 /* Put exclusion names first in the data */
1001 while (exclusion_count
--) {
1002 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1003 *(exclusion_list
+ exclusion_count
), LTTNG_SYMBOL_NAME_LEN
);
1005 /* Add filter expression next */
1006 if (lsm
.u
.enable
.expression_len
!= 0) {
1008 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1010 lsm
.u
.enable
.expression_len
);
1012 /* Add filter bytecode next */
1013 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1015 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1016 + lsm
.u
.enable
.expression_len
,
1018 lsm
.u
.enable
.bytecode_len
);
1021 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1022 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1023 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
, NULL
);
1027 if (filter_expression
&& ctx
) {
1028 filter_bytecode_free(ctx
);
1029 filter_ir_free(ctx
);
1030 filter_parser_ctx_free(ctx
);
1033 if (free_filter_expression
) {
1035 * The filter expression has been replaced and must be freed as it is
1036 * not the original filter expression received as a parameter.
1038 free(filter_expression
);
1042 * Return directly to the caller and don't ask the sessiond since something
1043 * went wrong in the parsing of data above.
1048 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1052 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1053 struct lttng_event
*ev
, const char *channel_name
,
1054 const char *original_filter_expression
)
1056 struct lttcomm_session_msg lsm
;
1059 unsigned int free_filter_expression
= 0;
1060 struct filter_parser_ctx
*ctx
= NULL
;
1062 * Cast as non-const since we may replace the filter expression
1063 * by a dynamically allocated string. Otherwise, the original
1064 * string is not modified.
1066 char *filter_expression
= (char *) original_filter_expression
;
1068 if (handle
== NULL
|| ev
== NULL
) {
1069 ret
= -LTTNG_ERR_INVALID
;
1073 /* Empty filter string will always be rejected by the parser
1074 * anyway, so treat this corner-case early to eliminate
1075 * lttng_fmemopen error for 0-byte allocation.
1077 if (filter_expression
&& filter_expression
[0] == '\0') {
1078 ret
= -LTTNG_ERR_INVALID
;
1082 memset(&lsm
, 0, sizeof(lsm
));
1084 /* If no channel name, send empty string. */
1085 if (channel_name
== NULL
) {
1086 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1087 sizeof(lsm
.u
.disable
.channel_name
));
1089 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1090 sizeof(lsm
.u
.disable
.channel_name
));
1093 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1094 if (ev
->name
[0] == '\0') {
1095 /* Disable all events */
1096 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1099 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1100 /* FIXME: copying non-packed struct to packed struct. */
1101 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1103 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1104 sizeof(lsm
.session
.name
));
1105 lsm
.u
.disable
.bytecode_len
= 0;
1108 * For the JUL domain, a filter is enforced except for the
1109 * disable all event. This is done to avoid having the event in
1110 * all sessions thus filtering by logger name.
1112 if (filter_expression
== NULL
&&
1113 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1114 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1115 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1120 * We have a filter, so we need to set up a variable-length
1121 * memory block from where to send the data.
1124 /* Parse filter expression */
1125 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1126 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1127 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1128 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1129 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1130 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1133 /* Setup JUL filter if needed. */
1134 agent_filter
= set_agent_filter(filter_expression
, ev
);
1135 if (!agent_filter
) {
1136 if (!filter_expression
) {
1137 /* No JUL and no filter, just skip everything below. */
1142 * With a JUL filter, the original filter has been added to it
1143 * thus replace the filter expression.
1145 filter_expression
= agent_filter
;
1146 free_filter_expression
= 1;
1150 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1156 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1157 + lsm
.u
.disable
.expression_len
);
1159 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1163 /* Add filter expression */
1164 if (lsm
.u
.disable
.expression_len
!= 0) {
1167 lsm
.u
.disable
.expression_len
);
1169 /* Add filter bytecode next */
1170 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1172 + lsm
.u
.disable
.expression_len
,
1174 lsm
.u
.disable
.bytecode_len
);
1177 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1178 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1182 if (filter_expression
&& ctx
) {
1183 filter_bytecode_free(ctx
);
1184 filter_ir_free(ctx
);
1185 filter_parser_ctx_free(ctx
);
1188 if (free_filter_expression
) {
1190 * The filter expression has been replaced and must be freed as it is
1191 * not the original filter expression received as a parameter.
1193 free(filter_expression
);
1197 * Return directly to the caller and don't ask the sessiond since something
1198 * went wrong in the parsing of data above.
1203 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1208 * Disable event(s) of a channel and domain.
1209 * If no event name is specified, all events are disabled.
1210 * If no channel name is specified, the default 'channel0' is used.
1211 * Returns size of returned session payload data or a negative error code.
1213 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1214 const char *channel_name
)
1216 struct lttng_event ev
;
1218 memset(&ev
, 0, sizeof(ev
));
1220 ev
.type
= LTTNG_EVENT_ALL
;
1221 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1222 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1226 * Enable channel per domain
1227 * Returns size of returned session payload data or a negative error code.
1229 int lttng_enable_channel(struct lttng_handle
*handle
,
1230 struct lttng_channel
*chan
)
1232 struct lttcomm_session_msg lsm
;
1235 * NULL arguments are forbidden. No default values.
1237 if (handle
== NULL
|| chan
== NULL
) {
1238 return -LTTNG_ERR_INVALID
;
1241 memset(&lsm
, 0, sizeof(lsm
));
1243 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1245 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1247 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1249 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1250 sizeof(lsm
.session
.name
));
1252 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1256 * All tracing will be stopped for registered events of the channel.
1257 * Returns size of returned session payload data or a negative error code.
1259 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1261 struct lttcomm_session_msg lsm
;
1263 /* Safety check. Both are mandatory */
1264 if (handle
== NULL
|| name
== NULL
) {
1265 return -LTTNG_ERR_INVALID
;
1268 memset(&lsm
, 0, sizeof(lsm
));
1270 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1272 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1273 sizeof(lsm
.u
.disable
.channel_name
));
1275 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1277 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1278 sizeof(lsm
.session
.name
));
1280 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1284 * Add PID to session tracker.
1285 * Return 0 on success else a negative LTTng error code.
1287 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1289 struct lttcomm_session_msg lsm
;
1292 * NULL arguments are forbidden. No default values.
1294 if (handle
== NULL
) {
1295 return -LTTNG_ERR_INVALID
;
1298 memset(&lsm
, 0, sizeof(lsm
));
1300 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1301 lsm
.u
.pid_tracker
.pid
= pid
;
1303 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1305 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1306 sizeof(lsm
.session
.name
));
1308 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1312 * Remove PID from session tracker.
1313 * Return 0 on success else a negative LTTng error code.
1315 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1317 struct lttcomm_session_msg lsm
;
1320 * NULL arguments are forbidden. No default values.
1322 if (handle
== NULL
) {
1323 return -LTTNG_ERR_INVALID
;
1326 memset(&lsm
, 0, sizeof(lsm
));
1328 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1329 lsm
.u
.pid_tracker
.pid
= pid
;
1331 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1333 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1334 sizeof(lsm
.session
.name
));
1336 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1340 * Lists all available tracepoints of domain.
1341 * Sets the contents of the events array.
1342 * Returns the number of lttng_event entries in events;
1343 * on error, returns a negative value.
1345 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1346 struct lttng_event
**events
)
1349 struct lttcomm_session_msg lsm
;
1351 if (handle
== NULL
) {
1352 return -LTTNG_ERR_INVALID
;
1355 memset(&lsm
, 0, sizeof(lsm
));
1356 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1357 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1359 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1364 return ret
/ sizeof(struct lttng_event
);
1368 * Lists all available tracepoint fields of domain.
1369 * Sets the contents of the event field array.
1370 * Returns the number of lttng_event_field entries in events;
1371 * on error, returns a negative value.
1373 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1374 struct lttng_event_field
**fields
)
1377 struct lttcomm_session_msg lsm
;
1379 if (handle
== NULL
) {
1380 return -LTTNG_ERR_INVALID
;
1383 memset(&lsm
, 0, sizeof(lsm
));
1384 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1385 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1387 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1392 return ret
/ sizeof(struct lttng_event_field
);
1396 * Lists all available kernel system calls. Allocates and sets the contents of
1399 * Returns the number of lttng_event entries in events; on error, returns a
1402 int lttng_list_syscalls(struct lttng_event
**events
)
1405 struct lttcomm_session_msg lsm
;
1408 return -LTTNG_ERR_INVALID
;
1411 memset(&lsm
, 0, sizeof(lsm
));
1412 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1413 /* Force kernel domain for system calls. */
1414 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1416 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1421 return ret
/ sizeof(struct lttng_event
);
1425 * Returns a human readable string describing
1426 * the error code (a negative value).
1428 const char *lttng_strerror(int code
)
1430 return error_get_str(code
);
1434 * Create a brand new session using name and url for destination.
1436 * Returns LTTNG_OK on success or a negative error code.
1438 int lttng_create_session(const char *name
, const char *url
)
1442 struct lttcomm_session_msg lsm
;
1443 struct lttng_uri
*uris
= NULL
;
1446 return -LTTNG_ERR_INVALID
;
1449 memset(&lsm
, 0, sizeof(lsm
));
1451 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1452 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1454 /* There should never be a data URL */
1455 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1457 return -LTTNG_ERR_INVALID
;
1460 lsm
.u
.uri
.size
= size
;
1462 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1463 sizeof(struct lttng_uri
) * size
, NULL
);
1470 * Destroy session using name.
1471 * Returns size of returned session payload data or a negative error code.
1473 int lttng_destroy_session(const char *session_name
)
1475 struct lttcomm_session_msg lsm
;
1477 if (session_name
== NULL
) {
1478 return -LTTNG_ERR_INVALID
;
1481 memset(&lsm
, 0, sizeof(lsm
));
1482 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1484 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1485 sizeof(lsm
.session
.name
));
1487 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1491 * Ask the session daemon for all available sessions.
1492 * Sets the contents of the sessions array.
1493 * Returns the number of lttng_session entries in sessions;
1494 * on error, returns a negative value.
1496 int lttng_list_sessions(struct lttng_session
**sessions
)
1499 struct lttcomm_session_msg lsm
;
1501 memset(&lsm
, 0, sizeof(lsm
));
1502 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1503 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1508 return ret
/ sizeof(struct lttng_session
);
1511 int lttng_set_session_shm_path(const char *session_name
,
1512 const char *shm_path
)
1514 struct lttcomm_session_msg lsm
;
1516 if (session_name
== NULL
) {
1517 return -LTTNG_ERR_INVALID
;
1520 memset(&lsm
, 0, sizeof(lsm
));
1521 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1523 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1524 sizeof(lsm
.session
.name
));
1525 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1526 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1528 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1532 * Ask the session daemon for all available domains of a session.
1533 * Sets the contents of the domains array.
1534 * Returns the number of lttng_domain entries in domains;
1535 * on error, returns a negative value.
1537 int lttng_list_domains(const char *session_name
,
1538 struct lttng_domain
**domains
)
1541 struct lttcomm_session_msg lsm
;
1543 if (session_name
== NULL
) {
1544 return -LTTNG_ERR_INVALID
;
1547 memset(&lsm
, 0, sizeof(lsm
));
1548 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1550 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1551 sizeof(lsm
.session
.name
));
1553 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1558 return ret
/ sizeof(struct lttng_domain
);
1562 * Ask the session daemon for all available channels of a session.
1563 * Sets the contents of the channels array.
1564 * Returns the number of lttng_channel entries in channels;
1565 * on error, returns a negative value.
1567 int lttng_list_channels(struct lttng_handle
*handle
,
1568 struct lttng_channel
**channels
)
1571 struct lttcomm_session_msg lsm
;
1573 if (handle
== NULL
) {
1574 return -LTTNG_ERR_INVALID
;
1577 memset(&lsm
, 0, sizeof(lsm
));
1578 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1579 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1580 sizeof(lsm
.session
.name
));
1582 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1584 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1589 return ret
/ sizeof(struct lttng_channel
);
1593 * Ask the session daemon for all available events of a session channel.
1594 * Sets the contents of the events array.
1595 * Returns the number of lttng_event entries in events;
1596 * on error, returns a negative value.
1598 int lttng_list_events(struct lttng_handle
*handle
,
1599 const char *channel_name
, struct lttng_event
**events
)
1602 struct lttcomm_session_msg lsm
;
1604 /* Safety check. An handle and channel name are mandatory */
1605 if (handle
== NULL
|| channel_name
== NULL
) {
1606 return -LTTNG_ERR_INVALID
;
1609 memset(&lsm
, 0, sizeof(lsm
));
1610 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1611 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1612 sizeof(lsm
.session
.name
));
1613 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1614 sizeof(lsm
.u
.list
.channel_name
));
1616 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1618 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1623 return ret
/ sizeof(struct lttng_event
);
1627 * Sets the tracing_group variable with name.
1628 * This function allocates memory pointed to by tracing_group.
1629 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1631 int lttng_set_tracing_group(const char *name
)
1634 return -LTTNG_ERR_INVALID
;
1637 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1638 return -LTTNG_ERR_FATAL
;
1645 * Returns size of returned session payload data or a negative error code.
1647 int lttng_calibrate(struct lttng_handle
*handle
,
1648 struct lttng_calibrate
*calibrate
)
1650 struct lttcomm_session_msg lsm
;
1652 /* Safety check. NULL pointer are forbidden */
1653 if (handle
== NULL
|| calibrate
== NULL
) {
1654 return -LTTNG_ERR_INVALID
;
1657 memset(&lsm
, 0, sizeof(lsm
));
1658 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1659 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1661 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1663 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1667 * Set default channel attributes.
1668 * If either or both of the arguments are null, attr content is zeroe'd.
1670 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1671 struct lttng_channel_attr
*attr
)
1674 if (attr
== NULL
|| domain
== NULL
) {
1678 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1680 /* Same for all domains. */
1681 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1682 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1683 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1685 switch (domain
->type
) {
1686 case LTTNG_DOMAIN_KERNEL
:
1687 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1688 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1689 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1690 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1691 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1693 case LTTNG_DOMAIN_UST
:
1694 switch (domain
->buf_type
) {
1695 case LTTNG_BUFFER_PER_UID
:
1696 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1697 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1698 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1699 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1700 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1702 case LTTNG_BUFFER_PER_PID
:
1704 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1705 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1706 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1707 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1708 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1712 /* Default behavior: leave set to 0. */
1718 * Check if session daemon is alive.
1720 * Return 1 if alive or 0 if not.
1721 * On error returns a negative value.
1723 int lttng_session_daemon_alive(void)
1727 ret
= set_session_daemon_path();
1733 if (*sessiond_sock_path
== '\0') {
1735 * No socket path set. Weird error which means the constructor was not
1741 ret
= try_connect_sessiond(sessiond_sock_path
);
1752 * Set URL for a consumer for a session and domain.
1754 * Return 0 on success, else a negative value.
1756 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1757 const char *control_url
, const char *data_url
)
1761 struct lttcomm_session_msg lsm
;
1762 struct lttng_uri
*uris
= NULL
;
1764 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1765 return -LTTNG_ERR_INVALID
;
1768 memset(&lsm
, 0, sizeof(lsm
));
1770 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1772 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1773 sizeof(lsm
.session
.name
));
1774 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1776 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1778 return -LTTNG_ERR_INVALID
;
1781 lsm
.u
.uri
.size
= size
;
1783 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1784 sizeof(struct lttng_uri
) * size
, NULL
);
1793 int lttng_enable_consumer(struct lttng_handle
*handle
)
1801 int lttng_disable_consumer(struct lttng_handle
*handle
)
1807 * This is an extension of create session that is ONLY and SHOULD only be used
1808 * by the lttng command line program. It exists to avoid using URI parsing in
1811 * We need the date and time for the trace path subdirectory for the case where
1812 * the user does NOT define one using either -o or -U. Using the normal
1813 * lttng_create_session API call, we have no clue on the session daemon side if
1814 * the URL was generated automatically by the client or define by the user.
1816 * So this function "wrapper" is hidden from the public API, takes the datetime
1817 * string and appends it if necessary to the URI subdirectory before sending it
1818 * to the session daemon.
1820 * With this extra function, the lttng_create_session call behavior is not
1821 * changed and the timestamp is appended to the URI on the session daemon side
1824 int _lttng_create_session_ext(const char *name
, const char *url
,
1825 const char *datetime
)
1829 struct lttcomm_session_msg lsm
;
1830 struct lttng_uri
*uris
= NULL
;
1832 if (name
== NULL
|| datetime
== NULL
) {
1833 return -LTTNG_ERR_INVALID
;
1836 memset(&lsm
, 0, sizeof(lsm
));
1838 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1839 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1841 /* There should never be a data URL */
1842 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1844 ret
= -LTTNG_ERR_INVALID
;
1848 lsm
.u
.uri
.size
= size
;
1850 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1851 /* Don't append datetime if the name was automatically created. */
1852 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1853 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1854 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1857 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1860 PERROR("snprintf uri subdir");
1861 ret
= -LTTNG_ERR_FATAL
;
1866 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1867 sizeof(struct lttng_uri
) * size
, NULL
);
1875 * For a given session name, this call checks if the data is ready to be read
1876 * or is still being extracted by the consumer(s) hence not ready to be used by
1879 int lttng_data_pending(const char *session_name
)
1882 struct lttcomm_session_msg lsm
;
1883 uint8_t *pending
= NULL
;
1885 if (session_name
== NULL
) {
1886 return -LTTNG_ERR_INVALID
;
1889 memset(&lsm
, 0, sizeof(lsm
));
1890 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1892 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1893 sizeof(lsm
.session
.name
));
1895 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
1898 } else if (ret
!= 1) {
1899 /* Unexpected payload size */
1900 ret
= -LTTNG_ERR_INVALID
;
1904 ret
= (int) *pending
;
1911 * Create a session exclusively used for snapshot.
1913 * Returns LTTNG_OK on success or a negative error code.
1915 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1919 struct lttcomm_session_msg lsm
;
1920 struct lttng_uri
*uris
= NULL
;
1923 return -LTTNG_ERR_INVALID
;
1926 memset(&lsm
, 0, sizeof(lsm
));
1928 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1929 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1931 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1933 return -LTTNG_ERR_INVALID
;
1936 lsm
.u
.uri
.size
= size
;
1938 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1939 sizeof(struct lttng_uri
) * size
, NULL
);
1946 * Create a session exclusively used for live.
1948 * Returns LTTNG_OK on success or a negative error code.
1950 int lttng_create_session_live(const char *name
, const char *url
,
1951 unsigned int timer_interval
)
1955 struct lttcomm_session_msg lsm
;
1956 struct lttng_uri
*uris
= NULL
;
1958 if (name
== NULL
|| timer_interval
== 0) {
1959 return -LTTNG_ERR_INVALID
;
1962 memset(&lsm
, 0, sizeof(lsm
));
1964 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
1965 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1968 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1970 ret
= -LTTNG_ERR_INVALID
;
1974 /* file:// is not accepted for live session. */
1975 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1976 ret
= -LTTNG_ERR_INVALID
;
1983 lsm
.u
.session_live
.nb_uri
= size
;
1984 lsm
.u
.session_live
.timer_interval
= timer_interval
;
1986 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1987 sizeof(struct lttng_uri
) * size
, NULL
);
1995 * List PIDs in the tracker.
1997 * enabled is set to whether the PID tracker is enabled.
1998 * pids is set to an allocated array of PIDs currently tracked. On
1999 * success, pids must be freed by the caller.
2000 * nr_pids is set to the number of entries contained by the pids array.
2002 * Returns 0 on success, else a negative LTTng error code.
2004 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2005 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2009 struct lttcomm_session_msg lsm
;
2013 if (handle
== NULL
) {
2014 return -LTTNG_ERR_INVALID
;
2017 memset(&lsm
, 0, sizeof(lsm
));
2018 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2019 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2020 sizeof(lsm
.session
.name
));
2021 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2023 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2027 nr_pids
= ret
/ sizeof(int32_t);
2028 if (nr_pids
== 1 && pids
[0] == -1) {
2034 *_enabled
= enabled
;
2036 *_nr_pids
= nr_pids
;
2043 static void __attribute__((constructor
)) init()
2045 /* Set default session group */
2046 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2052 static void __attribute__((destructor
)) lttng_ctl_exit()
2054 free(tracing_group
);