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
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/lttng.h>
37 #include <lttng/health-internal.h>
39 #include "filter/filter-ast.h"
40 #include "filter/filter-parser.h"
41 #include "filter/filter-bytecode.h"
42 #include "filter/memstream.h"
43 #include "lttng-ctl-helper.h"
46 static const int print_xml
= 1;
47 #define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
50 static const int print_xml
= 0;
51 #define dbg_printf(fmt, args...) \
53 /* do nothing but check printf format */ \
55 printf("[debug liblttnctl] " fmt, ## args); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
;
62 static char sessiond_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
80 * Copy string from src to dst and enforce null terminated byte.
83 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
86 strncpy(dst
, src
, len
);
87 /* Enforce the NULL terminated byte */
95 * Copy domain to lttcomm_session_msg domain.
97 * If domain is unknown, default domain will be the kernel.
100 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
101 struct lttng_domain
*src
)
105 case LTTNG_DOMAIN_KERNEL
:
106 case LTTNG_DOMAIN_UST
:
107 case LTTNG_DOMAIN_JUL
:
108 case LTTNG_DOMAIN_LOG4J
:
109 case LTTNG_DOMAIN_PYTHON
:
110 memcpy(dst
, src
, sizeof(struct lttng_domain
));
113 memset(dst
, 0, sizeof(struct lttng_domain
));
120 * Send lttcomm_session_msg to the session daemon.
122 * On success, returns the number of bytes sent (>=0)
123 * On error, returns -1
125 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
130 ret
= -LTTNG_ERR_NO_SESSIOND
;
134 DBG("LSM cmd type : %d", lsm
->cmd_type
);
136 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
137 sizeof(struct lttcomm_session_msg
));
139 ret
= -LTTNG_ERR_FATAL
;
147 * Send var len data to the session daemon.
149 * On success, returns the number of bytes sent (>=0)
150 * On error, returns -1
152 static int send_session_varlen(const void *data
, size_t len
)
157 ret
= -LTTNG_ERR_NO_SESSIOND
;
166 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
168 ret
= -LTTNG_ERR_FATAL
;
176 * Receive data from the sessiond socket.
178 * On success, returns the number of bytes received (>=0)
179 * On error, returns -1 (recvmsg() error) or -ENOTCONN
181 static int recv_data_sessiond(void *buf
, size_t len
)
186 ret
= -LTTNG_ERR_NO_SESSIOND
;
190 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
192 ret
= -LTTNG_ERR_FATAL
;
200 * Check if we are in the specified group.
202 * If yes return 1, else return -1.
205 int lttng_check_tracing_group(void)
207 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
209 int grp_list_size
, grp_id
, i
;
211 const char *grp_name
= tracing_group
;
213 /* Get GID of group 'tracing' */
214 grp_tracing
= getgrnam(grp_name
);
216 /* If grp_tracing is NULL, the group does not exist. */
220 /* Get number of supplementary group IDs */
221 grp_list_size
= getgroups(0, NULL
);
222 if (grp_list_size
< 0) {
227 /* Alloc group list of the right size */
228 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
233 grp_id
= getgroups(grp_list_size
, grp_list
);
239 for (i
= 0; i
< grp_list_size
; i
++) {
240 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
254 * Try connect to session daemon with sock_path.
256 * Return 0 on success, else -1
258 static int try_connect_sessiond(const char *sock_path
)
262 /* If socket exist, we check if the daemon listens for connect. */
263 ret
= access(sock_path
, F_OK
);
269 ret
= lttcomm_connect_unix_sock(sock_path
);
275 ret
= lttcomm_close_unix_sock(ret
);
277 PERROR("lttcomm_close_unix_sock");
287 * Set sessiond socket path by putting it in the global sessiond_sock_path
290 * Returns 0 on success, negative value on failure (the sessiond socket path
291 * is somehow too long or ENOMEM).
293 static int set_session_daemon_path(void)
295 int in_tgroup
= 0; /* In tracing group. */
301 /* Are we in the tracing group ? */
302 in_tgroup
= lttng_check_tracing_group();
305 if ((uid
== 0) || in_tgroup
) {
306 lttng_ctl_copy_string(sessiond_sock_path
,
307 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
315 ret
= try_connect_sessiond(sessiond_sock_path
);
319 /* Global session daemon not available... */
321 /* ...or not in tracing group (and not root), default */
324 * With GNU C < 2.1, snprintf returns -1 if the target buffer
326 * With GNU C >= 2.1, snprintf returns the required size
327 * (excluding closing null)
329 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
330 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
331 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
343 * Connect to the LTTng session daemon.
345 * On success, return 0. On error, return -1.
347 static int connect_sessiond(void)
351 /* Don't try to connect if already connected. */
356 ret
= set_session_daemon_path();
361 /* Connect to the sesssion daemon. */
362 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
367 sessiond_socket
= ret
;
377 * Clean disconnect from the session daemon.
379 * On success, return 0. On error, return -1.
381 static int disconnect_sessiond(void)
386 ret
= lttcomm_close_unix_sock(sessiond_socket
);
395 * Ask the session daemon a specific command and put the data into buf.
396 * Takes extra var. len. data as input to send to the session daemon.
398 * Return size of data (only payload, not header) or a negative error code.
401 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
402 const void *vardata
, size_t varlen
, void **buf
)
407 struct lttcomm_lttng_msg llm
;
409 ret
= connect_sessiond();
411 ret
= -LTTNG_ERR_NO_SESSIOND
;
415 /* Send command to session daemon */
416 ret
= send_session_msg(lsm
);
418 /* Ret value is a valid lttng error code. */
421 /* Send var len data */
422 ret
= send_session_varlen(vardata
, varlen
);
424 /* Ret value is a valid lttng error code. */
428 /* Get header from data transmission */
429 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
431 /* Ret value is a valid lttng error code. */
435 /* Check error code if OK */
436 if (llm
.ret_code
!= LTTNG_OK
) {
441 size
= llm
.data_size
;
443 /* If client free with size 0 */
451 data
= zmalloc(size
);
457 /* Get payload data */
458 ret
= recv_data_sessiond(data
, size
);
465 * Extra protection not to dereference a NULL pointer. If buf is NULL at
466 * this point, an error is returned and data is freed.
469 ret
= -LTTNG_ERR_INVALID
;
478 disconnect_sessiond();
483 * Create lttng handle and return pointer.
485 * The returned pointer will be NULL in case of malloc() error.
487 struct lttng_handle
*lttng_create_handle(const char *session_name
,
488 struct lttng_domain
*domain
)
490 struct lttng_handle
*handle
= NULL
;
492 if (domain
== NULL
) {
496 handle
= zmalloc(sizeof(struct lttng_handle
));
497 if (handle
== NULL
) {
498 PERROR("malloc handle");
502 /* Copy session name */
503 lttng_ctl_copy_string(handle
->session_name
, session_name
,
504 sizeof(handle
->session_name
));
506 /* Copy lttng domain */
507 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
514 * Destroy handle by free(3) the pointer.
516 void lttng_destroy_handle(struct lttng_handle
*handle
)
522 * Register an outside consumer.
524 * Returns size of returned session payload data or a negative error code.
526 int lttng_register_consumer(struct lttng_handle
*handle
,
527 const char *socket_path
)
529 struct lttcomm_session_msg lsm
;
531 if (handle
== NULL
|| socket_path
== NULL
) {
532 return -LTTNG_ERR_INVALID
;
535 memset(&lsm
, 0, sizeof(lsm
));
536 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
537 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
538 sizeof(lsm
.session
.name
));
539 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
541 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
542 sizeof(lsm
.u
.reg
.path
));
544 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
548 * Start tracing for all traces of the session.
550 * Returns size of returned session payload data or a negative error code.
552 int lttng_start_tracing(const char *session_name
)
554 struct lttcomm_session_msg lsm
;
556 if (session_name
== NULL
) {
557 return -LTTNG_ERR_INVALID
;
560 memset(&lsm
, 0, sizeof(lsm
));
561 lsm
.cmd_type
= LTTNG_START_TRACE
;
563 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
564 sizeof(lsm
.session
.name
));
566 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
570 * Stop tracing for all traces of the session.
572 static int _lttng_stop_tracing(const char *session_name
, int wait
)
575 struct lttcomm_session_msg lsm
;
577 if (session_name
== NULL
) {
578 return -LTTNG_ERR_INVALID
;
581 memset(&lsm
, 0, sizeof(lsm
));
582 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
584 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
585 sizeof(lsm
.session
.name
));
587 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
588 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
596 /* Check for data availability */
598 data_ret
= lttng_data_pending(session_name
);
600 /* Return the data available call error. */
606 * Data sleep time before retrying (in usec). Don't sleep if the
607 * call returned value indicates availability.
610 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
612 } while (data_ret
!= 0);
620 * Stop tracing and wait for data availability.
622 int lttng_stop_tracing(const char *session_name
)
624 return _lttng_stop_tracing(session_name
, 1);
628 * Stop tracing but _don't_ wait for data availability.
630 int lttng_stop_tracing_no_wait(const char *session_name
)
632 return _lttng_stop_tracing(session_name
, 0);
636 * Add context to a channel.
638 * If the given channel is NULL, add the contexts to all channels.
639 * The event_name param is ignored.
641 * Returns the size of the returned payload data or a negative error code.
643 int lttng_add_context(struct lttng_handle
*handle
,
644 struct lttng_event_context
*ctx
, const char *event_name
,
645 const char *channel_name
)
650 struct lttcomm_session_msg lsm
;
652 /* Safety check. Both are mandatory. */
653 if (handle
== NULL
|| ctx
== NULL
) {
654 ret
= -LTTNG_ERR_INVALID
;
658 memset(&lsm
, 0, sizeof(lsm
));
659 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
661 /* If no channel name, send empty string. */
662 if (channel_name
== NULL
) {
663 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
664 sizeof(lsm
.u
.context
.channel_name
));
666 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
667 sizeof(lsm
.u
.context
.channel_name
));
670 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
671 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
672 sizeof(lsm
.session
.name
));
674 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
675 size_t provider_len
, ctx_len
;
676 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
677 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
679 if (!provider_name
|| !ctx_name
) {
680 ret
= -LTTNG_ERR_INVALID
;
684 provider_len
= strlen(provider_name
);
685 if (provider_len
== 0) {
686 ret
= -LTTNG_ERR_INVALID
;
689 lsm
.u
.context
.provider_name_len
= provider_len
;
691 ctx_len
= strlen(ctx_name
);
693 ret
= -LTTNG_ERR_INVALID
;
696 lsm
.u
.context
.context_name_len
= ctx_len
;
698 len
= provider_len
+ ctx_len
;
701 ret
= -LTTNG_ERR_NOMEM
;
705 memcpy(buf
, provider_name
, provider_len
);
706 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
708 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
709 /* Don't leak application addresses to the sessiond. */
710 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
711 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
713 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, buf
, len
, NULL
);
720 * Enable event(s) for a channel.
722 * If no event name is specified, all events are enabled.
723 * If no channel name is specified, the default 'channel0' is used.
725 * Returns size of returned session payload data or a negative error code.
727 int lttng_enable_event(struct lttng_handle
*handle
,
728 struct lttng_event
*ev
, const char *channel_name
)
730 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
735 * Create or enable an event with a filter expression.
737 * Return negative error value on error.
738 * Return size of returned session payload data if OK.
740 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
741 struct lttng_event
*event
, const char *channel_name
,
742 const char *filter_expression
)
744 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
745 filter_expression
, 0, NULL
);
749 * Depending on the event, return a newly allocated agent filter expression or
750 * NULL if not applicable.
752 * An event with NO loglevel and the name is * will return NULL.
754 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
757 char *agent_filter
= NULL
;
761 /* Don't add filter for the '*' event. */
762 if (ev
->name
[0] != '*') {
764 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
767 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
775 /* Add loglevel filtering if any for the JUL domain. */
776 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
779 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
785 if (filter
|| agent_filter
) {
788 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
789 agent_filter
? agent_filter
: filter
, op
,
794 agent_filter
= new_filter
;
796 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
812 * Generate the filter bytecode from a given filter expression string. Put the
813 * newly allocated parser context in ctxp and populate the lsm object with the
816 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
818 static int generate_filter(char *filter_expression
,
819 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
822 struct filter_parser_ctx
*ctx
= NULL
;
825 assert(filter_expression
);
830 * Casting const to non-const, as the underlying function will use it in
833 fmem
= lttng_fmemopen((void *) filter_expression
,
834 strlen(filter_expression
), "r");
836 fprintf(stderr
, "Error opening memory as stream\n");
837 ret
= -LTTNG_ERR_FILTER_NOMEM
;
840 ctx
= filter_parser_ctx_alloc(fmem
);
842 fprintf(stderr
, "Error allocating parser\n");
843 ret
= -LTTNG_ERR_FILTER_NOMEM
;
844 goto filter_alloc_error
;
846 ret
= filter_parser_ctx_append_ast(ctx
);
848 fprintf(stderr
, "Parse error\n");
849 ret
= -LTTNG_ERR_FILTER_INVAL
;
852 ret
= filter_visitor_set_parent(ctx
);
854 fprintf(stderr
, "Set parent error\n");
855 ret
= -LTTNG_ERR_FILTER_INVAL
;
859 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
862 fprintf(stderr
, "XML print error\n");
863 ret
= -LTTNG_ERR_FILTER_INVAL
;
868 dbg_printf("Generating IR... ");
870 ret
= filter_visitor_ir_generate(ctx
);
872 fprintf(stderr
, "Generate IR error\n");
873 ret
= -LTTNG_ERR_FILTER_INVAL
;
876 dbg_printf("done\n");
878 dbg_printf("Validating IR... ");
880 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
882 ret
= -LTTNG_ERR_FILTER_INVAL
;
885 /* Validate strings used as literals in the expression. */
886 ret
= filter_visitor_ir_validate_string(ctx
);
888 ret
= -LTTNG_ERR_FILTER_INVAL
;
891 dbg_printf("done\n");
893 dbg_printf("Generating bytecode... ");
895 ret
= filter_visitor_bytecode_generate(ctx
);
897 fprintf(stderr
, "Generate bytecode error\n");
898 ret
= -LTTNG_ERR_FILTER_INVAL
;
901 dbg_printf("done\n");
902 dbg_printf("Size of bytecode generated: %u bytes.\n",
903 bytecode_get_len(&ctx
->bytecode
->b
));
905 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
906 + bytecode_get_len(&ctx
->bytecode
->b
);
907 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
909 /* No need to keep the memory stream. */
910 if (fclose(fmem
) != 0) {
919 filter_parser_ctx_free(ctx
);
921 if (fclose(fmem
) != 0) {
929 * Enable event(s) for a channel, possibly with exclusions and a filter.
930 * If no event name is specified, all events are enabled.
931 * If no channel name is specified, the default name is used.
932 * If filter expression is not NULL, the filter is set for the event.
933 * If exclusion count is not zero, the exclusions are set for the event.
934 * Returns size of returned session payload data or a negative error code.
936 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
937 struct lttng_event
*ev
, const char *channel_name
,
938 const char *original_filter_expression
,
939 int exclusion_count
, char **exclusion_list
)
941 struct lttcomm_session_msg lsm
;
944 unsigned int free_filter_expression
= 0;
945 struct filter_parser_ctx
*ctx
= NULL
;
947 * Cast as non-const since we may replace the filter expression
948 * by a dynamically allocated string. Otherwise, the original
949 * string is not modified.
951 char *filter_expression
= (char *) original_filter_expression
;
953 if (handle
== NULL
|| ev
== NULL
) {
954 ret
= -LTTNG_ERR_INVALID
;
959 * Empty filter string will always be rejected by the parser
960 * anyway, so treat this corner-case early to eliminate
961 * lttng_fmemopen error for 0-byte allocation.
963 if (filter_expression
&& filter_expression
[0] == '\0') {
964 ret
= -LTTNG_ERR_INVALID
;
968 memset(&lsm
, 0, sizeof(lsm
));
970 /* If no channel name, send empty string. */
971 if (channel_name
== NULL
) {
972 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
973 sizeof(lsm
.u
.enable
.channel_name
));
975 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
976 sizeof(lsm
.u
.enable
.channel_name
));
979 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
980 if (ev
->name
[0] == '\0') {
981 /* Enable all events */
982 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
985 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
986 /* FIXME: copying non-packed struct to packed struct. */
987 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
989 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
990 sizeof(lsm
.session
.name
));
991 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
992 lsm
.u
.enable
.bytecode_len
= 0;
995 * For the JUL domain, a filter is enforced except for the enable all
996 * event. This is done to avoid having the event in all sessions thus
997 * filtering by logger name.
999 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1000 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1001 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1002 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1007 * We have either a filter or some exclusions, so we need to set up
1008 * a variable-length memory block from where to send the data.
1011 /* Parse filter expression. */
1012 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1013 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1014 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1015 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1016 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1017 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1020 /* Setup JUL filter if needed. */
1021 agent_filter
= set_agent_filter(filter_expression
, ev
);
1022 if (!agent_filter
) {
1023 if (!filter_expression
) {
1025 * No JUL and no filter, just skip
1032 * With an agent filter, the original filter has
1033 * been added to it thus replace the filter
1036 filter_expression
= agent_filter
;
1037 free_filter_expression
= 1;
1041 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1047 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1048 + lsm
.u
.enable
.expression_len
1049 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1051 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1055 /* Put exclusion names first in the data. */
1056 while (exclusion_count
--) {
1057 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1058 *(exclusion_list
+ exclusion_count
),
1059 LTTNG_SYMBOL_NAME_LEN
- 1);
1061 /* Add filter expression next. */
1062 if (lsm
.u
.enable
.expression_len
!= 0) {
1064 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1066 lsm
.u
.enable
.expression_len
);
1068 /* Add filter bytecode next. */
1069 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1071 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1072 + lsm
.u
.enable
.expression_len
,
1074 lsm
.u
.enable
.bytecode_len
);
1077 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1078 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1079 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1084 if (filter_expression
&& ctx
) {
1085 filter_bytecode_free(ctx
);
1086 filter_ir_free(ctx
);
1087 filter_parser_ctx_free(ctx
);
1090 if (free_filter_expression
) {
1092 * The filter expression has been replaced and must be freed as
1093 * it is not the original filter expression received as a
1096 free(filter_expression
);
1100 * Return directly to the caller and don't ask the sessiond since
1101 * something went wrong in the parsing of data above.
1106 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1110 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1111 struct lttng_event
*ev
, const char *channel_name
,
1112 const char *original_filter_expression
)
1114 struct lttcomm_session_msg lsm
;
1117 unsigned int free_filter_expression
= 0;
1118 struct filter_parser_ctx
*ctx
= NULL
;
1120 * Cast as non-const since we may replace the filter expression
1121 * by a dynamically allocated string. Otherwise, the original
1122 * string is not modified.
1124 char *filter_expression
= (char *) original_filter_expression
;
1126 if (handle
== NULL
|| ev
== NULL
) {
1127 ret
= -LTTNG_ERR_INVALID
;
1132 * Empty filter string will always be rejected by the parser
1133 * anyway, so treat this corner-case early to eliminate
1134 * lttng_fmemopen error for 0-byte allocation.
1136 if (filter_expression
&& filter_expression
[0] == '\0') {
1137 ret
= -LTTNG_ERR_INVALID
;
1141 memset(&lsm
, 0, sizeof(lsm
));
1143 /* If no channel name, send empty string. */
1144 if (channel_name
== NULL
) {
1145 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1146 sizeof(lsm
.u
.disable
.channel_name
));
1148 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1149 sizeof(lsm
.u
.disable
.channel_name
));
1152 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1154 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1155 /* FIXME: copying non-packed struct to packed struct. */
1156 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1158 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1159 sizeof(lsm
.session
.name
));
1160 lsm
.u
.disable
.bytecode_len
= 0;
1163 * For the JUL domain, a filter is enforced except for the
1164 * disable all event. This is done to avoid having the event in
1165 * all sessions thus filtering by logger name.
1167 if (filter_expression
== NULL
&&
1168 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1169 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1170 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1175 * We have a filter, so we need to set up a variable-length
1176 * memory block from where to send the data.
1179 /* Parse filter expression */
1180 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1181 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1182 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1183 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1184 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1185 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1188 /* Setup JUL filter if needed. */
1189 agent_filter
= set_agent_filter(filter_expression
, ev
);
1190 if (!agent_filter
) {
1191 if (!filter_expression
) {
1193 * No JUL and no filter, just skip
1200 * With a JUL filter, the original filter has
1201 * been added to it thus replace the filter
1204 filter_expression
= agent_filter
;
1205 free_filter_expression
= 1;
1209 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1215 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1216 + lsm
.u
.disable
.expression_len
);
1218 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1222 /* Add filter expression. */
1223 if (lsm
.u
.disable
.expression_len
!= 0) {
1226 lsm
.u
.disable
.expression_len
);
1228 /* Add filter bytecode next. */
1229 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1231 + lsm
.u
.disable
.expression_len
,
1233 lsm
.u
.disable
.bytecode_len
);
1236 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1237 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1241 if (filter_expression
&& ctx
) {
1242 filter_bytecode_free(ctx
);
1243 filter_ir_free(ctx
);
1244 filter_parser_ctx_free(ctx
);
1247 if (free_filter_expression
) {
1249 * The filter expression has been replaced and must be freed as
1250 * it is not the original filter expression received as a
1253 free(filter_expression
);
1257 * Return directly to the caller and don't ask the sessiond since
1258 * something went wrong in the parsing of data above.
1263 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1268 * Disable event(s) of a channel and domain.
1269 * If no event name is specified, all events are disabled.
1270 * If no channel name is specified, the default 'channel0' is used.
1271 * Returns size of returned session payload data or a negative error code.
1273 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1274 const char *channel_name
)
1276 struct lttng_event ev
;
1278 memset(&ev
, 0, sizeof(ev
));
1280 ev
.type
= LTTNG_EVENT_ALL
;
1281 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1282 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1286 * Enable channel per domain
1287 * Returns size of returned session payload data or a negative error code.
1289 int lttng_enable_channel(struct lttng_handle
*handle
,
1290 struct lttng_channel
*chan
)
1292 struct lttcomm_session_msg lsm
;
1294 /* NULL arguments are forbidden. No default values. */
1295 if (handle
== NULL
|| chan
== NULL
) {
1296 return -LTTNG_ERR_INVALID
;
1299 memset(&lsm
, 0, sizeof(lsm
));
1301 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1303 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1305 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1307 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1308 sizeof(lsm
.session
.name
));
1310 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1314 * All tracing will be stopped for registered events of the channel.
1315 * Returns size of returned session payload data or a negative error code.
1317 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1319 struct lttcomm_session_msg lsm
;
1321 /* Safety check. Both are mandatory. */
1322 if (handle
== NULL
|| name
== NULL
) {
1323 return -LTTNG_ERR_INVALID
;
1326 memset(&lsm
, 0, sizeof(lsm
));
1328 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1330 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1331 sizeof(lsm
.u
.disable
.channel_name
));
1333 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1335 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1336 sizeof(lsm
.session
.name
));
1338 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1342 * Add PID to session tracker.
1343 * Return 0 on success else a negative LTTng error code.
1345 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1347 struct lttcomm_session_msg lsm
;
1349 /* NULL arguments are forbidden. No default values. */
1350 if (handle
== NULL
) {
1351 return -LTTNG_ERR_INVALID
;
1354 memset(&lsm
, 0, sizeof(lsm
));
1356 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1357 lsm
.u
.pid_tracker
.pid
= pid
;
1359 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1361 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1362 sizeof(lsm
.session
.name
));
1364 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1368 * Remove PID from session tracker.
1369 * Return 0 on success else a negative LTTng error code.
1371 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1373 struct lttcomm_session_msg lsm
;
1375 /* NULL arguments are forbidden. No default values. */
1376 if (handle
== NULL
) {
1377 return -LTTNG_ERR_INVALID
;
1380 memset(&lsm
, 0, sizeof(lsm
));
1382 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1383 lsm
.u
.pid_tracker
.pid
= pid
;
1385 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1387 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1388 sizeof(lsm
.session
.name
));
1390 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1394 * Lists all available tracepoints of domain.
1395 * Sets the contents of the events array.
1396 * Returns the number of lttng_event entries in events;
1397 * on error, returns a negative value.
1399 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1400 struct lttng_event
**events
)
1403 struct lttcomm_session_msg lsm
;
1405 if (handle
== NULL
) {
1406 return -LTTNG_ERR_INVALID
;
1409 memset(&lsm
, 0, sizeof(lsm
));
1410 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1411 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1413 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1418 return ret
/ sizeof(struct lttng_event
);
1422 * Lists all available tracepoint fields of domain.
1423 * Sets the contents of the event field array.
1424 * Returns the number of lttng_event_field entries in events;
1425 * on error, returns a negative value.
1427 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1428 struct lttng_event_field
**fields
)
1431 struct lttcomm_session_msg lsm
;
1433 if (handle
== NULL
) {
1434 return -LTTNG_ERR_INVALID
;
1437 memset(&lsm
, 0, sizeof(lsm
));
1438 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1439 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1441 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1446 return ret
/ sizeof(struct lttng_event_field
);
1450 * Lists all available kernel system calls. Allocates and sets the contents of
1453 * Returns the number of lttng_event entries in events; on error, returns a
1456 int lttng_list_syscalls(struct lttng_event
**events
)
1459 struct lttcomm_session_msg lsm
;
1462 return -LTTNG_ERR_INVALID
;
1465 memset(&lsm
, 0, sizeof(lsm
));
1466 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1467 /* Force kernel domain for system calls. */
1468 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1470 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1475 return ret
/ sizeof(struct lttng_event
);
1479 * Returns a human readable string describing
1480 * the error code (a negative value).
1482 const char *lttng_strerror(int code
)
1484 return error_get_str(code
);
1488 * Create a brand new session using name and url for destination.
1490 * Returns LTTNG_OK on success or a negative error code.
1492 int lttng_create_session(const char *name
, const char *url
)
1496 struct lttcomm_session_msg lsm
;
1497 struct lttng_uri
*uris
= NULL
;
1500 return -LTTNG_ERR_INVALID
;
1503 memset(&lsm
, 0, sizeof(lsm
));
1505 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1506 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1508 /* There should never be a data URL */
1509 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1511 return -LTTNG_ERR_INVALID
;
1514 lsm
.u
.uri
.size
= size
;
1516 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1517 sizeof(struct lttng_uri
) * size
, NULL
);
1524 * Destroy session using name.
1525 * Returns size of returned session payload data or a negative error code.
1527 int lttng_destroy_session(const char *session_name
)
1529 struct lttcomm_session_msg lsm
;
1531 if (session_name
== NULL
) {
1532 return -LTTNG_ERR_INVALID
;
1535 memset(&lsm
, 0, sizeof(lsm
));
1536 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1538 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1539 sizeof(lsm
.session
.name
));
1541 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1545 * Ask the session daemon for all available sessions.
1546 * Sets the contents of the sessions array.
1547 * Returns the number of lttng_session entries in sessions;
1548 * on error, returns a negative value.
1550 int lttng_list_sessions(struct lttng_session
**sessions
)
1553 struct lttcomm_session_msg lsm
;
1555 memset(&lsm
, 0, sizeof(lsm
));
1556 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1557 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1562 return ret
/ sizeof(struct lttng_session
);
1565 int lttng_set_session_shm_path(const char *session_name
,
1566 const char *shm_path
)
1568 struct lttcomm_session_msg lsm
;
1570 if (session_name
== NULL
) {
1571 return -LTTNG_ERR_INVALID
;
1574 memset(&lsm
, 0, sizeof(lsm
));
1575 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1577 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1578 sizeof(lsm
.session
.name
));
1579 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1580 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1582 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1586 * Ask the session daemon for all available domains of a session.
1587 * Sets the contents of the domains array.
1588 * Returns the number of lttng_domain entries in domains;
1589 * on error, returns a negative value.
1591 int lttng_list_domains(const char *session_name
,
1592 struct lttng_domain
**domains
)
1595 struct lttcomm_session_msg lsm
;
1597 if (session_name
== NULL
) {
1598 return -LTTNG_ERR_INVALID
;
1601 memset(&lsm
, 0, sizeof(lsm
));
1602 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1604 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1605 sizeof(lsm
.session
.name
));
1607 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1612 return ret
/ sizeof(struct lttng_domain
);
1616 * Ask the session daemon for all available channels of a session.
1617 * Sets the contents of the channels array.
1618 * Returns the number of lttng_channel entries in channels;
1619 * on error, returns a negative value.
1621 int lttng_list_channels(struct lttng_handle
*handle
,
1622 struct lttng_channel
**channels
)
1625 struct lttcomm_session_msg lsm
;
1627 if (handle
== NULL
) {
1628 return -LTTNG_ERR_INVALID
;
1631 memset(&lsm
, 0, sizeof(lsm
));
1632 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1633 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1634 sizeof(lsm
.session
.name
));
1636 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1638 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1643 return ret
/ sizeof(struct lttng_channel
);
1647 * Ask the session daemon for all available events of a session channel.
1648 * Sets the contents of the events array.
1649 * Returns the number of lttng_event entries in events;
1650 * on error, returns a negative value.
1652 int lttng_list_events(struct lttng_handle
*handle
,
1653 const char *channel_name
, struct lttng_event
**events
)
1656 struct lttcomm_session_msg lsm
;
1658 /* Safety check. An handle and channel name are mandatory */
1659 if (handle
== NULL
|| channel_name
== NULL
) {
1660 return -LTTNG_ERR_INVALID
;
1663 memset(&lsm
, 0, sizeof(lsm
));
1664 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1665 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1666 sizeof(lsm
.session
.name
));
1667 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1668 sizeof(lsm
.u
.list
.channel_name
));
1670 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1672 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1677 return ret
/ sizeof(struct lttng_event
);
1681 * Sets the tracing_group variable with name.
1682 * This function allocates memory pointed to by tracing_group.
1683 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1685 int lttng_set_tracing_group(const char *name
)
1688 return -LTTNG_ERR_INVALID
;
1691 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1692 return -LTTNG_ERR_FATAL
;
1699 * Returns size of returned session payload data or a negative error code.
1701 int lttng_calibrate(struct lttng_handle
*handle
,
1702 struct lttng_calibrate
*calibrate
)
1704 struct lttcomm_session_msg lsm
;
1706 /* Safety check. NULL pointer are forbidden */
1707 if (handle
== NULL
|| calibrate
== NULL
) {
1708 return -LTTNG_ERR_INVALID
;
1711 memset(&lsm
, 0, sizeof(lsm
));
1712 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1713 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1715 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1717 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1721 * Set default channel attributes.
1722 * If either or both of the arguments are null, attr content is zeroe'd.
1724 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1725 struct lttng_channel_attr
*attr
)
1728 if (attr
== NULL
|| domain
== NULL
) {
1732 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1734 /* Same for all domains. */
1735 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1736 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1737 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1739 switch (domain
->type
) {
1740 case LTTNG_DOMAIN_KERNEL
:
1741 attr
->switch_timer_interval
=
1742 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1743 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1744 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1745 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1746 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1748 case LTTNG_DOMAIN_UST
:
1749 switch (domain
->buf_type
) {
1750 case LTTNG_BUFFER_PER_UID
:
1751 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1752 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1753 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1754 attr
->switch_timer_interval
=
1755 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1756 attr
->read_timer_interval
=
1757 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1759 case LTTNG_BUFFER_PER_PID
:
1761 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1762 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1763 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1764 attr
->switch_timer_interval
=
1765 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1766 attr
->read_timer_interval
=
1767 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1771 /* Default behavior: leave set to 0. */
1777 * Check if session daemon is alive.
1779 * Return 1 if alive or 0 if not.
1780 * On error returns a negative value.
1782 int lttng_session_daemon_alive(void)
1786 ret
= set_session_daemon_path();
1792 if (*sessiond_sock_path
== '\0') {
1794 * No socket path set. Weird error which means the constructor
1800 ret
= try_connect_sessiond(sessiond_sock_path
);
1811 * Set URL for a consumer for a session and domain.
1813 * Return 0 on success, else a negative value.
1815 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1816 const char *control_url
, const char *data_url
)
1820 struct lttcomm_session_msg lsm
;
1821 struct lttng_uri
*uris
= NULL
;
1823 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1824 return -LTTNG_ERR_INVALID
;
1827 memset(&lsm
, 0, sizeof(lsm
));
1829 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1831 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1832 sizeof(lsm
.session
.name
));
1833 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1835 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1837 return -LTTNG_ERR_INVALID
;
1840 lsm
.u
.uri
.size
= size
;
1842 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1843 sizeof(struct lttng_uri
) * size
, NULL
);
1852 int lttng_enable_consumer(struct lttng_handle
*handle
)
1860 int lttng_disable_consumer(struct lttng_handle
*handle
)
1866 * This is an extension of create session that is ONLY and SHOULD only be used
1867 * by the lttng command line program. It exists to avoid using URI parsing in
1870 * We need the date and time for the trace path subdirectory for the case where
1871 * the user does NOT define one using either -o or -U. Using the normal
1872 * lttng_create_session API call, we have no clue on the session daemon side if
1873 * the URL was generated automatically by the client or define by the user.
1875 * So this function "wrapper" is hidden from the public API, takes the datetime
1876 * string and appends it if necessary to the URI subdirectory before sending it
1877 * to the session daemon.
1879 * With this extra function, the lttng_create_session call behavior is not
1880 * changed and the timestamp is appended to the URI on the session daemon side
1883 int _lttng_create_session_ext(const char *name
, const char *url
,
1884 const char *datetime
)
1888 struct lttcomm_session_msg lsm
;
1889 struct lttng_uri
*uris
= NULL
;
1891 if (name
== NULL
|| datetime
== NULL
) {
1892 return -LTTNG_ERR_INVALID
;
1895 memset(&lsm
, 0, sizeof(lsm
));
1897 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1898 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1900 /* There should never be a data URL. */
1901 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1903 ret
= -LTTNG_ERR_INVALID
;
1907 lsm
.u
.uri
.size
= size
;
1909 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1910 /* Don't append datetime if the name was automatically created. */
1911 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1912 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1913 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1916 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1919 PERROR("snprintf uri subdir");
1920 ret
= -LTTNG_ERR_FATAL
;
1925 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1926 sizeof(struct lttng_uri
) * size
, NULL
);
1934 * For a given session name, this call checks if the data is ready to be read
1935 * or is still being extracted by the consumer(s) hence not ready to be used by
1938 int lttng_data_pending(const char *session_name
)
1941 struct lttcomm_session_msg lsm
;
1942 uint8_t *pending
= NULL
;
1944 if (session_name
== NULL
) {
1945 return -LTTNG_ERR_INVALID
;
1948 memset(&lsm
, 0, sizeof(lsm
));
1949 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1951 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1952 sizeof(lsm
.session
.name
));
1954 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
1957 } else if (ret
!= 1) {
1958 /* Unexpected payload size */
1959 ret
= -LTTNG_ERR_INVALID
;
1963 ret
= (int) *pending
;
1970 * Create a session exclusively used for snapshot.
1972 * Returns LTTNG_OK on success or a negative error code.
1974 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1978 struct lttcomm_session_msg lsm
;
1979 struct lttng_uri
*uris
= NULL
;
1982 return -LTTNG_ERR_INVALID
;
1985 memset(&lsm
, 0, sizeof(lsm
));
1987 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1988 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1990 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1992 return -LTTNG_ERR_INVALID
;
1995 lsm
.u
.uri
.size
= size
;
1997 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1998 sizeof(struct lttng_uri
) * size
, NULL
);
2005 * Create a session exclusively used for live.
2007 * Returns LTTNG_OK on success or a negative error code.
2009 int lttng_create_session_live(const char *name
, const char *url
,
2010 unsigned int timer_interval
)
2014 struct lttcomm_session_msg lsm
;
2015 struct lttng_uri
*uris
= NULL
;
2017 if (name
== NULL
|| timer_interval
== 0) {
2018 return -LTTNG_ERR_INVALID
;
2021 memset(&lsm
, 0, sizeof(lsm
));
2023 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2024 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2027 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2029 ret
= -LTTNG_ERR_INVALID
;
2033 /* file:// is not accepted for live session. */
2034 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2035 ret
= -LTTNG_ERR_INVALID
;
2042 lsm
.u
.session_live
.nb_uri
= size
;
2043 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2045 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
2046 sizeof(struct lttng_uri
) * size
, NULL
);
2054 * List PIDs in the tracker.
2056 * enabled is set to whether the PID tracker is enabled.
2057 * pids is set to an allocated array of PIDs currently tracked. On
2058 * success, pids must be freed by the caller.
2059 * nr_pids is set to the number of entries contained by the pids array.
2061 * Returns 0 on success, else a negative LTTng error code.
2063 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2064 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2068 struct lttcomm_session_msg lsm
;
2072 if (handle
== NULL
) {
2073 return -LTTNG_ERR_INVALID
;
2076 memset(&lsm
, 0, sizeof(lsm
));
2077 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2078 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2079 sizeof(lsm
.session
.name
));
2080 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2082 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2086 nr_pids
= ret
/ sizeof(int32_t);
2087 if (nr_pids
== 1 && pids
[0] == -1) {
2093 *_enabled
= enabled
;
2095 *_nr_pids
= nr_pids
;
2102 static void __attribute__((constructor
)) init()
2104 /* Set default session group */
2105 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2111 static void __attribute__((destructor
)) lttng_ctl_exit()
2113 free(tracing_group
);