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
30 #include <common/common.h>
31 #include <common/defaults.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <lttng/lttng.h>
35 #include "filter-parser.h"
36 #include "filter-ast.h"
37 #include "filter-bytecode.h"
38 #include "memstream.h"
41 const int print_xml
= 1;
42 #define dbg_printf(fmt, args...) \
43 printf("[debug liblttng-ctl] " fmt, ## args)
45 const int print_xml
= 0;
46 #define dbg_printf(fmt, args...) \
48 /* do nothing but check printf format */ \
50 printf("[debug liblttnctl] " fmt, ## args); \
55 /* Socket to session daemon for communication */
56 static int sessiond_socket
;
57 static char sessiond_sock_path
[PATH_MAX
];
60 static char *tracing_group
;
66 * Those two variables are used by error.h to silent or control the verbosity of
67 * error message. They are global to the library so application linking with it
68 * are able to compile correctly and also control verbosity of the library.
70 * Note that it is *not* possible to silent ERR() and PERROR() macros.
73 int lttng_opt_verbose
;
76 * Copy string from src to dst and enforce null terminated byte.
78 static void copy_string(char *dst
, const char *src
, size_t len
)
81 strncpy(dst
, src
, len
);
82 /* Enforce the NULL terminated byte */
90 * Copy domain to lttcomm_session_msg domain.
92 * If domain is unknown, default domain will be the kernel.
94 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
98 case LTTNG_DOMAIN_KERNEL
:
99 case LTTNG_DOMAIN_UST
:
101 case LTTNG_DOMAIN_UST_EXEC_NAME:
102 case LTTNG_DOMAIN_UST_PID:
103 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
105 memcpy(dst
, src
, sizeof(struct lttng_domain
));
108 memset(dst
, 0, sizeof(struct lttng_domain
));
109 dst
->type
= LTTNG_DOMAIN_KERNEL
;
116 * Send lttcomm_session_msg to the session daemon.
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
121 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
130 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
131 sizeof(struct lttcomm_session_msg
));
138 * Send var len data to the session daemon.
140 * On success, returns the number of bytes sent (>=0)
141 * On error, returns -1
143 static int send_session_varlen(void *data
, size_t len
)
156 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
163 * Receive data from the sessiond socket.
165 * On success, returns the number of bytes received (>=0)
166 * On error, returns -1 (recvmsg() error) or -ENOTCONN
168 static int recv_data_sessiond(void *buf
, size_t len
)
177 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
184 * Check if we are in the specified group.
186 * If yes return 1, else return -1.
188 static int check_tracing_group(const char *grp_name
)
190 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
192 int grp_list_size
, grp_id
, i
;
195 /* Get GID of group 'tracing' */
196 grp_tracing
= getgrnam(grp_name
);
198 /* If grp_tracing is NULL, the group does not exist. */
202 /* Get number of supplementary group IDs */
203 grp_list_size
= getgroups(0, NULL
);
204 if (grp_list_size
< 0) {
209 /* Alloc group list of the right size */
210 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
215 grp_id
= getgroups(grp_list_size
, grp_list
);
221 for (i
= 0; i
< grp_list_size
; i
++) {
222 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
236 * Try connect to session daemon with sock_path.
238 * Return 0 on success, else -1
240 static int try_connect_sessiond(const char *sock_path
)
244 /* If socket exist, we check if the daemon listens for connect. */
245 ret
= access(sock_path
, F_OK
);
251 ret
= lttcomm_connect_unix_sock(sock_path
);
257 ret
= lttcomm_close_unix_sock(ret
);
259 perror("lttcomm_close_unix_sock");
266 * Set sessiond socket path by putting it in the global
267 * sessiond_sock_path variable.
268 * Returns 0 on success,
269 * -ENOMEM on failure (the sessiond socket path is somehow too long)
271 static int set_session_daemon_path(void)
274 int in_tgroup
= 0; /* In tracing group */
280 /* Are we in the tracing group ? */
281 in_tgroup
= check_tracing_group(tracing_group
);
284 if ((uid
== 0) || in_tgroup
) {
285 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
286 sizeof(sessiond_sock_path
));
292 ret
= try_connect_sessiond(sessiond_sock_path
);
296 /* Global session daemon not available... */
298 /* ...or not in tracing group (and not root), default */
301 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
302 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
304 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
305 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
306 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
315 * Connect to the LTTng session daemon.
317 * On success, return 0. On error, return -1.
319 static int connect_sessiond(void)
323 ret
= set_session_daemon_path();
325 return -1; /* set_session_daemon_path() returns -ENOMEM */
328 /* Connect to the sesssion daemon */
329 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
334 sessiond_socket
= ret
;
341 * Clean disconnect from the session daemon.
342 * On success, return 0. On error, return -1.
344 static int disconnect_sessiond(void)
349 ret
= lttcomm_close_unix_sock(sessiond_socket
);
358 * Ask the session daemon a specific command and put the data into buf.
359 * Takes extra var. len. data as input to send to the session daemon.
361 * Return size of data (only payload, not header) or a negative error code.
363 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
371 struct lttcomm_lttng_msg llm
;
373 ret
= connect_sessiond();
378 /* Send command to session daemon */
379 ret
= send_session_msg(lsm
);
383 /* Send var len data */
384 ret
= send_session_varlen(vardata
, varlen
);
389 /* Get header from data transmission */
390 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
395 /* Check error code if OK */
396 if (llm
.ret_code
!= LTTCOMM_OK
) {
401 size
= llm
.data_size
;
403 /* If client free with size 0 */
411 data
= (void*) malloc(size
);
413 /* Get payload data */
414 ret
= recv_data_sessiond(data
, size
);
421 * Extra protection not to dereference a NULL pointer. If buf is NULL at
422 * this point, an error is returned and data is freed.
434 disconnect_sessiond();
439 * Ask the session daemon a specific command and put the data into buf.
441 * Return size of data (only payload, not header) or a negative error code.
443 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
445 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
449 * Create lttng handle and return pointer.
450 * The returned pointer will be NULL in case of malloc() error.
452 struct lttng_handle
*lttng_create_handle(const char *session_name
,
453 struct lttng_domain
*domain
)
455 struct lttng_handle
*handle
;
457 handle
= malloc(sizeof(struct lttng_handle
));
458 if (handle
== NULL
) {
459 perror("malloc handle");
463 /* Copy session name */
464 copy_string(handle
->session_name
, session_name
,
465 sizeof(handle
->session_name
));
467 /* Copy lttng domain */
468 copy_lttng_domain(&handle
->domain
, domain
);
475 * Destroy handle by free(3) the pointer.
477 void lttng_destroy_handle(struct lttng_handle
*handle
)
485 * Register an outside consumer.
486 * Returns size of returned session payload data or a negative error code.
488 int lttng_register_consumer(struct lttng_handle
*handle
,
489 const char *socket_path
)
491 struct lttcomm_session_msg lsm
;
493 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
494 copy_string(lsm
.session
.name
, handle
->session_name
,
495 sizeof(lsm
.session
.name
));
496 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
498 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
500 return ask_sessiond(&lsm
, NULL
);
504 * Start tracing for all traces of the session.
505 * Returns size of returned session payload data or a negative error code.
507 int lttng_start_tracing(const char *session_name
)
509 struct lttcomm_session_msg lsm
;
511 if (session_name
== NULL
) {
515 lsm
.cmd_type
= LTTNG_START_TRACE
;
517 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
519 return ask_sessiond(&lsm
, NULL
);
523 * Stop tracing for all traces of the session.
524 * Returns size of returned session payload data or a negative error code.
526 int lttng_stop_tracing(const char *session_name
)
528 struct lttcomm_session_msg lsm
;
530 if (session_name
== NULL
) {
534 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
536 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
538 return ask_sessiond(&lsm
, NULL
);
542 * Add context to event and/or channel.
543 * If event_name is NULL, the context is applied to all events of the channel.
544 * If channel_name is NULL, a lookup of the event's channel is done.
545 * If both are NULL, the context is applied to all events of all channels.
547 * Returns the size of the returned payload data or a negative error code.
549 int lttng_add_context(struct lttng_handle
*handle
,
550 struct lttng_event_context
*ctx
, const char *event_name
,
551 const char *channel_name
)
553 struct lttcomm_session_msg lsm
;
555 /* Safety check. Both are mandatory */
556 if (handle
== NULL
|| ctx
== NULL
) {
560 memset(&lsm
, 0, sizeof(lsm
));
562 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
564 /* Copy channel name */
565 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
566 sizeof(lsm
.u
.context
.channel_name
));
567 /* Copy event name */
568 copy_string(lsm
.u
.context
.event_name
, event_name
,
569 sizeof(lsm
.u
.context
.event_name
));
571 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
573 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
575 copy_string(lsm
.session
.name
, handle
->session_name
,
576 sizeof(lsm
.session
.name
));
578 return ask_sessiond(&lsm
, NULL
);
582 * Enable event(s) for a channel.
583 * If no event name is specified, all events are enabled.
584 * If no channel name is specified, the default 'channel0' is used.
585 * Returns size of returned session payload data or a negative error code.
587 int lttng_enable_event(struct lttng_handle
*handle
,
588 struct lttng_event
*ev
, const char *channel_name
)
590 struct lttcomm_session_msg lsm
;
592 if (handle
== NULL
|| ev
== NULL
) {
596 memset(&lsm
, 0, sizeof(lsm
));
598 /* If no channel name, we put the default name */
599 if (channel_name
== NULL
) {
600 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
601 sizeof(lsm
.u
.enable
.channel_name
));
603 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
604 sizeof(lsm
.u
.enable
.channel_name
));
607 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
609 if (ev
->name
[0] != '\0') {
610 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
612 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
614 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
616 copy_string(lsm
.session
.name
, handle
->session_name
,
617 sizeof(lsm
.session
.name
));
619 return ask_sessiond(&lsm
, NULL
);
623 * set filter for an event
624 * Return negative error value on error.
625 * Return size of returned session payload data if OK.
628 int lttng_set_event_filter(struct lttng_handle
*handle
,
629 const char *event_name
, const char *channel_name
,
630 const char *filter_expression
)
632 struct lttcomm_session_msg lsm
;
633 struct filter_parser_ctx
*ctx
;
638 if (handle
== NULL
) {
642 if (!filter_expression
) {
647 * casting const to non-const, as the underlying function will
648 * use it in read-only mode.
650 fmem
= lttng_fmemopen((void *) filter_expression
,
651 strlen(filter_expression
), "r");
653 fprintf(stderr
, "Error opening memory as stream\n");
656 ctx
= filter_parser_ctx_alloc(fmem
);
658 fprintf(stderr
, "Error allocating parser\n");
662 ret
= filter_parser_ctx_append_ast(ctx
);
664 fprintf(stderr
, "Parse error\n");
668 ret
= filter_visitor_set_parent(ctx
);
670 fprintf(stderr
, "Set parent error\n");
675 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
678 fprintf(stderr
, "XML print error\n");
684 dbg_printf("Generating IR... ");
686 ret
= filter_visitor_ir_generate(ctx
);
688 fprintf(stderr
, "Generate IR error\n");
692 dbg_printf("done\n");
694 dbg_printf("Validating IR... ");
696 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
701 dbg_printf("done\n");
703 dbg_printf("Generating bytecode... ");
705 ret
= filter_visitor_bytecode_generate(ctx
);
707 fprintf(stderr
, "Generate bytecode error\n");
711 dbg_printf("done\n");
712 dbg_printf("Size of bytecode generated: %u bytes.\n",
713 bytecode_get_len(&ctx
->bytecode
->b
));
715 memset(&lsm
, 0, sizeof(lsm
));
717 lsm
.cmd_type
= LTTNG_SET_FILTER
;
719 /* Copy channel name */
720 copy_string(lsm
.u
.filter
.channel_name
, channel_name
,
721 sizeof(lsm
.u
.filter
.channel_name
));
722 /* Copy event name */
723 copy_string(lsm
.u
.filter
.event_name
, event_name
,
724 sizeof(lsm
.u
.filter
.event_name
));
725 lsm
.u
.filter
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
726 + bytecode_get_len(&ctx
->bytecode
->b
);
728 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
730 copy_string(lsm
.session
.name
, handle
->session_name
,
731 sizeof(lsm
.session
.name
));
733 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
734 lsm
.u
.filter
.bytecode_len
, NULL
);
736 filter_bytecode_free(ctx
);
738 filter_parser_ctx_free(ctx
);
739 if (fclose(fmem
) != 0) {
745 filter_bytecode_free(ctx
);
747 filter_parser_ctx_free(ctx
);
749 if (fclose(fmem
) != 0) {
756 * Disable event(s) of a channel and domain.
757 * If no event name is specified, all events are disabled.
758 * If no channel name is specified, the default 'channel0' is used.
759 * Returns size of returned session payload data or a negative error code.
761 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
762 const char *channel_name
)
764 struct lttcomm_session_msg lsm
;
766 if (handle
== NULL
) {
770 memset(&lsm
, 0, sizeof(lsm
));
773 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
774 sizeof(lsm
.u
.disable
.channel_name
));
776 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
777 sizeof(lsm
.u
.disable
.channel_name
));
780 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
783 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
784 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
786 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
789 copy_string(lsm
.session
.name
, handle
->session_name
,
790 sizeof(lsm
.session
.name
));
792 return ask_sessiond(&lsm
, NULL
);
796 * Enable channel per domain
797 * Returns size of returned session payload data or a negative error code.
799 int lttng_enable_channel(struct lttng_handle
*handle
,
800 struct lttng_channel
*chan
)
802 struct lttcomm_session_msg lsm
;
805 * NULL arguments are forbidden. No default values.
807 if (handle
== NULL
|| chan
== NULL
) {
811 memset(&lsm
, 0, sizeof(lsm
));
813 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
815 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
817 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
819 copy_string(lsm
.session
.name
, handle
->session_name
,
820 sizeof(lsm
.session
.name
));
822 return ask_sessiond(&lsm
, NULL
);
826 * All tracing will be stopped for registered events of the channel.
827 * Returns size of returned session payload data or a negative error code.
829 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
831 struct lttcomm_session_msg lsm
;
833 /* Safety check. Both are mandatory */
834 if (handle
== NULL
|| name
== NULL
) {
838 memset(&lsm
, 0, sizeof(lsm
));
840 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
842 copy_string(lsm
.u
.disable
.channel_name
, name
,
843 sizeof(lsm
.u
.disable
.channel_name
));
845 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
847 copy_string(lsm
.session
.name
, handle
->session_name
,
848 sizeof(lsm
.session
.name
));
850 return ask_sessiond(&lsm
, NULL
);
854 * Lists all available tracepoints of domain.
855 * Sets the contents of the events array.
856 * Returns the number of lttng_event entries in events;
857 * on error, returns a negative value.
859 int lttng_list_tracepoints(struct lttng_handle
*handle
,
860 struct lttng_event
**events
)
863 struct lttcomm_session_msg lsm
;
865 if (handle
== NULL
) {
869 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
870 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
872 ret
= ask_sessiond(&lsm
, (void **) events
);
877 return ret
/ sizeof(struct lttng_event
);
881 * Lists all available tracepoint fields of domain.
882 * Sets the contents of the event field array.
883 * Returns the number of lttng_event_field entries in events;
884 * on error, returns a negative value.
886 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
887 struct lttng_event_field
**fields
)
890 struct lttcomm_session_msg lsm
;
892 if (handle
== NULL
) {
896 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
897 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
899 ret
= ask_sessiond(&lsm
, (void **) fields
);
904 return ret
/ sizeof(struct lttng_event_field
);
908 * Returns a human readable string describing
909 * the error code (a negative value).
911 const char *lttng_strerror(int code
)
913 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
914 if (code
> -LTTCOMM_OK
) {
915 return "Ended with errors";
918 return lttcomm_get_readable_code(code
);
922 * Create a brand new session using name and path.
923 * Returns size of returned session payload data or a negative error code.
925 int lttng_create_session(const char *name
, const char *path
)
927 struct lttcomm_session_msg lsm
;
929 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
930 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
931 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
933 return ask_sessiond(&lsm
, NULL
);
937 * Create a new tracing session using a name, URIs and a consumer enable flag.
939 int lttng_create_session_uri(const char *name
, struct lttng_uri
*ctrl_uri
,
940 struct lttng_uri
*data_uri
, unsigned int enable_consumer
)
942 struct lttcomm_session_msg lsm
;
944 /* Name and ctrl_uri are mandatory */
945 if (name
== NULL
|| ctrl_uri
== NULL
) {
949 lsm
.cmd_type
= LTTNG_CREATE_SESSION_URI
;
951 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
952 /* Anything bigger than zero, the consumer(s) will be enabled */
953 lsm
.u
.create_uri
.enable_consumer
= enable_consumer
;
954 memcpy(&lsm
.u
.create_uri
.ctrl_uri
, ctrl_uri
,
955 sizeof(lsm
.u
.create_uri
.ctrl_uri
));
958 * The only possible scenario where data_uri is NULL is for a local
959 * consumer where the output is at a specified path name on the
962 memcpy(&lsm
.u
.create_uri
.data_uri
, data_uri
,
963 sizeof(lsm
.u
.create_uri
.data_uri
));
966 return ask_sessiond(&lsm
, NULL
);
970 * Destroy session using name.
971 * Returns size of returned session payload data or a negative error code.
973 int lttng_destroy_session(const char *session_name
)
975 struct lttcomm_session_msg lsm
;
977 if (session_name
== NULL
) {
981 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
983 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
985 return ask_sessiond(&lsm
, NULL
);
989 * Ask the session daemon for all available sessions.
990 * Sets the contents of the sessions array.
991 * Returns the number of lttng_session entries in sessions;
992 * on error, returns a negative value.
994 int lttng_list_sessions(struct lttng_session
**sessions
)
997 struct lttcomm_session_msg lsm
;
999 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1000 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1005 return ret
/ sizeof(struct lttng_session
);
1009 * Ask the session daemon for all available domains of a session.
1010 * Sets the contents of the domains array.
1011 * Returns the number of lttng_domain entries in domains;
1012 * on error, returns a negative value.
1014 int lttng_list_domains(const char *session_name
,
1015 struct lttng_domain
**domains
)
1018 struct lttcomm_session_msg lsm
;
1020 if (session_name
== NULL
) {
1024 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1026 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1028 ret
= ask_sessiond(&lsm
, (void**) domains
);
1033 return ret
/ sizeof(struct lttng_domain
);
1037 * Ask the session daemon for all available channels of a session.
1038 * Sets the contents of the channels array.
1039 * Returns the number of lttng_channel entries in channels;
1040 * on error, returns a negative value.
1042 int lttng_list_channels(struct lttng_handle
*handle
,
1043 struct lttng_channel
**channels
)
1046 struct lttcomm_session_msg lsm
;
1048 if (handle
== NULL
) {
1052 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1053 copy_string(lsm
.session
.name
, handle
->session_name
,
1054 sizeof(lsm
.session
.name
));
1056 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1058 ret
= ask_sessiond(&lsm
, (void**) channels
);
1063 return ret
/ sizeof(struct lttng_channel
);
1067 * Ask the session daemon for all available events of a session channel.
1068 * Sets the contents of the events array.
1069 * Returns the number of lttng_event entries in events;
1070 * on error, returns a negative value.
1072 int lttng_list_events(struct lttng_handle
*handle
,
1073 const char *channel_name
, struct lttng_event
**events
)
1076 struct lttcomm_session_msg lsm
;
1078 /* Safety check. An handle and channel name are mandatory */
1079 if (handle
== NULL
|| channel_name
== NULL
) {
1083 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1084 copy_string(lsm
.session
.name
, handle
->session_name
,
1085 sizeof(lsm
.session
.name
));
1086 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1087 sizeof(lsm
.u
.list
.channel_name
));
1089 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1091 ret
= ask_sessiond(&lsm
, (void**) events
);
1096 return ret
/ sizeof(struct lttng_event
);
1100 * Sets the tracing_group variable with name.
1101 * This function allocates memory pointed to by tracing_group.
1102 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1104 int lttng_set_tracing_group(const char *name
)
1110 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1118 * Returns size of returned session payload data or a negative error code.
1120 int lttng_calibrate(struct lttng_handle
*handle
,
1121 struct lttng_calibrate
*calibrate
)
1123 struct lttcomm_session_msg lsm
;
1125 /* Safety check. NULL pointer are forbidden */
1126 if (handle
== NULL
|| calibrate
== NULL
) {
1130 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1131 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1133 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1135 return ask_sessiond(&lsm
, NULL
);
1139 * Set default channel attributes.
1140 * If either or both of the arguments are null, attr content is zeroe'd.
1142 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1143 struct lttng_channel_attr
*attr
)
1146 if (attr
== NULL
|| domain
== NULL
) {
1150 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1152 switch (domain
->type
) {
1153 case LTTNG_DOMAIN_KERNEL
:
1154 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1155 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1156 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1158 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1159 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1160 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1162 case LTTNG_DOMAIN_UST
:
1164 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1165 case LTTNG_DOMAIN_UST_PID
:
1166 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1168 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1169 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1170 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1172 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
1173 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1174 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1177 /* Default behavior: leave set to 0. */
1183 * Check if session daemon is alive.
1185 * Return 1 if alive or 0 if not.
1186 * On error returns a negative value.
1188 int lttng_session_daemon_alive(void)
1192 ret
= set_session_daemon_path();
1198 if (strlen(sessiond_sock_path
) == 0) {
1199 /* No socket path set. Weird error */
1203 ret
= try_connect_sessiond(sessiond_sock_path
);
1214 * Set URI for a consumer for a session and domain.
1216 * Return 0 on success, else a negative value.
1218 int lttng_set_consumer_uri(struct lttng_handle
*handle
, struct lttng_uri
*uri
)
1220 struct lttcomm_session_msg lsm
;
1222 if (handle
== NULL
|| uri
== NULL
) {
1226 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1228 copy_string(lsm
.session
.name
, handle
->session_name
,
1229 sizeof(lsm
.session
.name
));
1230 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1232 memcpy(&lsm
.u
.uri
, uri
, sizeof(lsm
.u
.uri
));
1234 return ask_sessiond(&lsm
, NULL
);
1238 * Enable consumer for a session and domain.
1240 * Return 0 on success, else a negative value.
1242 int lttng_enable_consumer(struct lttng_handle
*handle
)
1244 struct lttcomm_session_msg lsm
;
1246 if (handle
== NULL
) {
1250 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1252 copy_string(lsm
.session
.name
, handle
->session_name
,
1253 sizeof(lsm
.session
.name
));
1254 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1256 return ask_sessiond(&lsm
, NULL
);
1260 * Disable consumer for a session and domain.
1262 * Return 0 on success, else a negative value.
1264 int lttng_disable_consumer(struct lttng_handle
*handle
)
1266 struct lttcomm_session_msg lsm
;
1268 if (handle
== NULL
) {
1272 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1274 copy_string(lsm
.session
.name
, handle
->session_name
,
1275 sizeof(lsm
.session
.name
));
1276 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1278 return ask_sessiond(&lsm
, NULL
);
1284 static void __attribute__((constructor
)) init()
1286 /* Set default session group */
1287 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);