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 <lttng/lttng.h>
36 #include "filter-parser.h"
37 #include "filter-ast.h"
38 #include "filter-bytecode.h"
39 #include "memstream.h"
42 const int print_xml
= 1;
43 #define dbg_printf(fmt, args...) \
44 printf("[debug liblttng-ctl] " fmt, ## args)
46 const int print_xml
= 0;
47 #define dbg_printf(fmt, args...) \
49 /* do nothing but check printf format */ \
51 printf("[debug liblttnctl] " fmt, ## args); \
56 /* Socket to session daemon for communication */
57 static int sessiond_socket
;
58 static char sessiond_sock_path
[PATH_MAX
];
59 static char health_sock_path
[PATH_MAX
];
62 static char *tracing_group
;
68 * Those two variables are used by error.h to silent or control the verbosity of
69 * error message. They are global to the library so application linking with it
70 * are able to compile correctly and also control verbosity of the library.
72 * Note that it is *not* possible to silent ERR() and PERROR() macros.
75 int lttng_opt_verbose
;
78 * Copy string from src to dst and enforce null terminated byte.
80 static void copy_string(char *dst
, const char *src
, size_t len
)
83 strncpy(dst
, src
, len
);
84 /* Enforce the NULL terminated byte */
92 * Copy domain to lttcomm_session_msg domain.
94 * If domain is unknown, default domain will be the kernel.
96 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
100 case LTTNG_DOMAIN_KERNEL
:
101 case LTTNG_DOMAIN_UST
:
103 case LTTNG_DOMAIN_UST_EXEC_NAME:
104 case LTTNG_DOMAIN_UST_PID:
105 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
107 memcpy(dst
, src
, sizeof(struct lttng_domain
));
110 memset(dst
, 0, sizeof(struct lttng_domain
));
111 dst
->type
= LTTNG_DOMAIN_KERNEL
;
118 * Send lttcomm_session_msg to the session daemon.
120 * On success, returns the number of bytes sent (>=0)
121 * On error, returns -1
123 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
132 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
133 sizeof(struct lttcomm_session_msg
));
140 * Send var len data to the session daemon.
142 * On success, returns the number of bytes sent (>=0)
143 * On error, returns -1
145 static int send_session_varlen(void *data
, size_t len
)
158 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
165 * Receive data from the sessiond socket.
167 * On success, returns the number of bytes received (>=0)
168 * On error, returns -1 (recvmsg() error) or -ENOTCONN
170 static int recv_data_sessiond(void *buf
, size_t len
)
179 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
186 * Check if we are in the specified group.
188 * If yes return 1, else return -1.
190 static int check_tracing_group(const char *grp_name
)
192 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
194 int grp_list_size
, grp_id
, i
;
197 /* Get GID of group 'tracing' */
198 grp_tracing
= getgrnam(grp_name
);
200 /* If grp_tracing is NULL, the group does not exist. */
204 /* Get number of supplementary group IDs */
205 grp_list_size
= getgroups(0, NULL
);
206 if (grp_list_size
< 0) {
211 /* Alloc group list of the right size */
212 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
217 grp_id
= getgroups(grp_list_size
, grp_list
);
223 for (i
= 0; i
< grp_list_size
; i
++) {
224 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
238 * Try connect to session daemon with sock_path.
240 * Return 0 on success, else -1
242 static int try_connect_sessiond(const char *sock_path
)
246 /* If socket exist, we check if the daemon listens for connect. */
247 ret
= access(sock_path
, F_OK
);
253 ret
= lttcomm_connect_unix_sock(sock_path
);
259 ret
= lttcomm_close_unix_sock(ret
);
261 perror("lttcomm_close_unix_sock");
268 * Set sessiond socket path by putting it in the global
269 * sessiond_sock_path variable.
270 * Returns 0 on success,
271 * -ENOMEM on failure (the sessiond socket path is somehow too long)
273 static int set_session_daemon_path(void)
276 int in_tgroup
= 0; /* In tracing group */
282 /* Are we in the tracing group ? */
283 in_tgroup
= check_tracing_group(tracing_group
);
286 if ((uid
== 0) || in_tgroup
) {
287 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
288 sizeof(sessiond_sock_path
));
294 ret
= try_connect_sessiond(sessiond_sock_path
);
298 /* Global session daemon not available... */
300 /* ...or not in tracing group (and not root), default */
303 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
304 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
306 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
307 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
308 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
317 * Connect to the LTTng session daemon.
319 * On success, return 0. On error, return -1.
321 static int connect_sessiond(void)
325 ret
= set_session_daemon_path();
327 return -1; /* set_session_daemon_path() returns -ENOMEM */
330 /* Connect to the sesssion daemon */
331 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
336 sessiond_socket
= ret
;
343 * Clean disconnect from the session daemon.
344 * On success, return 0. On error, return -1.
346 static int disconnect_sessiond(void)
351 ret
= lttcomm_close_unix_sock(sessiond_socket
);
360 * Ask the session daemon a specific command and put the data into buf.
361 * Takes extra var. len. data as input to send to the session daemon.
363 * Return size of data (only payload, not header) or a negative error code.
365 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
373 struct lttcomm_lttng_msg llm
;
375 ret
= connect_sessiond();
380 /* Send command to session daemon */
381 ret
= send_session_msg(lsm
);
385 /* Send var len data */
386 ret
= send_session_varlen(vardata
, varlen
);
391 /* Get header from data transmission */
392 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
397 /* Check error code if OK */
398 if (llm
.ret_code
!= LTTCOMM_OK
) {
403 size
= llm
.data_size
;
405 /* If client free with size 0 */
413 data
= (void*) malloc(size
);
415 /* Get payload data */
416 ret
= recv_data_sessiond(data
, size
);
423 * Extra protection not to dereference a NULL pointer. If buf is NULL at
424 * this point, an error is returned and data is freed.
436 disconnect_sessiond();
441 * Ask the session daemon a specific command and put the data into buf.
443 * Return size of data (only payload, not header) or a negative error code.
445 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
447 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
451 * Create lttng handle and return pointer.
452 * The returned pointer will be NULL in case of malloc() error.
454 struct lttng_handle
*lttng_create_handle(const char *session_name
,
455 struct lttng_domain
*domain
)
457 struct lttng_handle
*handle
;
459 handle
= malloc(sizeof(struct lttng_handle
));
460 if (handle
== NULL
) {
461 perror("malloc handle");
465 /* Copy session name */
466 copy_string(handle
->session_name
, session_name
,
467 sizeof(handle
->session_name
));
469 /* Copy lttng domain */
470 copy_lttng_domain(&handle
->domain
, domain
);
477 * Destroy handle by free(3) the pointer.
479 void lttng_destroy_handle(struct lttng_handle
*handle
)
487 * Register an outside consumer.
488 * Returns size of returned session payload data or a negative error code.
490 int lttng_register_consumer(struct lttng_handle
*handle
,
491 const char *socket_path
)
493 struct lttcomm_session_msg lsm
;
495 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
496 copy_string(lsm
.session
.name
, handle
->session_name
,
497 sizeof(lsm
.session
.name
));
498 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
500 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
502 return ask_sessiond(&lsm
, NULL
);
506 * Start tracing for all traces of the session.
507 * Returns size of returned session payload data or a negative error code.
509 int lttng_start_tracing(const char *session_name
)
511 struct lttcomm_session_msg lsm
;
513 if (session_name
== NULL
) {
517 lsm
.cmd_type
= LTTNG_START_TRACE
;
519 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
521 return ask_sessiond(&lsm
, NULL
);
525 * Stop tracing for all traces of the session.
526 * Returns size of returned session payload data or a negative error code.
528 int lttng_stop_tracing(const char *session_name
)
530 struct lttcomm_session_msg lsm
;
532 if (session_name
== NULL
) {
536 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
538 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
540 return ask_sessiond(&lsm
, NULL
);
544 * Add context to event and/or channel.
545 * If event_name is NULL, the context is applied to all events of the channel.
546 * If channel_name is NULL, a lookup of the event's channel is done.
547 * If both are NULL, the context is applied to all events of all channels.
549 * Returns the size of the returned payload data or a negative error code.
551 int lttng_add_context(struct lttng_handle
*handle
,
552 struct lttng_event_context
*ctx
, const char *event_name
,
553 const char *channel_name
)
555 struct lttcomm_session_msg lsm
;
557 /* Safety check. Both are mandatory */
558 if (handle
== NULL
|| ctx
== NULL
) {
562 memset(&lsm
, 0, sizeof(lsm
));
564 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
566 /* Copy channel name */
567 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
568 sizeof(lsm
.u
.context
.channel_name
));
569 /* Copy event name */
570 copy_string(lsm
.u
.context
.event_name
, event_name
,
571 sizeof(lsm
.u
.context
.event_name
));
573 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
575 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
577 copy_string(lsm
.session
.name
, handle
->session_name
,
578 sizeof(lsm
.session
.name
));
580 return ask_sessiond(&lsm
, NULL
);
584 * Enable event(s) for a channel.
585 * If no event name is specified, all events are enabled.
586 * If no channel name is specified, the default 'channel0' is used.
587 * Returns size of returned session payload data or a negative error code.
589 int lttng_enable_event(struct lttng_handle
*handle
,
590 struct lttng_event
*ev
, const char *channel_name
)
592 struct lttcomm_session_msg lsm
;
594 if (handle
== NULL
|| ev
== NULL
) {
598 memset(&lsm
, 0, sizeof(lsm
));
600 /* If no channel name, we put the default name */
601 if (channel_name
== NULL
) {
602 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
603 sizeof(lsm
.u
.enable
.channel_name
));
605 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
606 sizeof(lsm
.u
.enable
.channel_name
));
609 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
611 if (ev
->name
[0] != '\0') {
612 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
614 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
616 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
618 copy_string(lsm
.session
.name
, handle
->session_name
,
619 sizeof(lsm
.session
.name
));
621 return ask_sessiond(&lsm
, NULL
);
625 * set filter for an event
626 * Return negative error value on error.
627 * Return size of returned session payload data if OK.
630 int lttng_set_event_filter(struct lttng_handle
*handle
,
631 const char *event_name
, const char *channel_name
,
632 const char *filter_expression
)
634 struct lttcomm_session_msg lsm
;
635 struct filter_parser_ctx
*ctx
;
640 if (handle
== NULL
) {
644 if (!filter_expression
) {
649 * casting const to non-const, as the underlying function will
650 * use it in read-only mode.
652 fmem
= lttng_fmemopen((void *) filter_expression
,
653 strlen(filter_expression
), "r");
655 fprintf(stderr
, "Error opening memory as stream\n");
658 ctx
= filter_parser_ctx_alloc(fmem
);
660 fprintf(stderr
, "Error allocating parser\n");
664 ret
= filter_parser_ctx_append_ast(ctx
);
666 fprintf(stderr
, "Parse error\n");
670 ret
= filter_visitor_set_parent(ctx
);
672 fprintf(stderr
, "Set parent error\n");
677 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
680 fprintf(stderr
, "XML print error\n");
686 dbg_printf("Generating IR... ");
688 ret
= filter_visitor_ir_generate(ctx
);
690 fprintf(stderr
, "Generate IR error\n");
694 dbg_printf("done\n");
696 dbg_printf("Validating IR... ");
698 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
703 dbg_printf("done\n");
705 dbg_printf("Generating bytecode... ");
707 ret
= filter_visitor_bytecode_generate(ctx
);
709 fprintf(stderr
, "Generate bytecode error\n");
713 dbg_printf("done\n");
714 dbg_printf("Size of bytecode generated: %u bytes.\n",
715 bytecode_get_len(&ctx
->bytecode
->b
));
717 memset(&lsm
, 0, sizeof(lsm
));
719 lsm
.cmd_type
= LTTNG_SET_FILTER
;
721 /* Copy channel name */
722 copy_string(lsm
.u
.filter
.channel_name
, channel_name
,
723 sizeof(lsm
.u
.filter
.channel_name
));
724 /* Copy event name */
725 copy_string(lsm
.u
.filter
.event_name
, event_name
,
726 sizeof(lsm
.u
.filter
.event_name
));
727 lsm
.u
.filter
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
728 + bytecode_get_len(&ctx
->bytecode
->b
);
730 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
732 copy_string(lsm
.session
.name
, handle
->session_name
,
733 sizeof(lsm
.session
.name
));
735 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
736 lsm
.u
.filter
.bytecode_len
, NULL
);
738 filter_bytecode_free(ctx
);
740 filter_parser_ctx_free(ctx
);
741 if (fclose(fmem
) != 0) {
747 filter_bytecode_free(ctx
);
749 filter_parser_ctx_free(ctx
);
751 if (fclose(fmem
) != 0) {
758 * Disable event(s) of a channel and domain.
759 * If no event name is specified, all events are disabled.
760 * If no channel name is specified, the default 'channel0' is used.
761 * Returns size of returned session payload data or a negative error code.
763 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
764 const char *channel_name
)
766 struct lttcomm_session_msg lsm
;
768 if (handle
== NULL
) {
772 memset(&lsm
, 0, sizeof(lsm
));
775 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
776 sizeof(lsm
.u
.disable
.channel_name
));
778 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
779 sizeof(lsm
.u
.disable
.channel_name
));
782 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
785 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
786 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
788 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
791 copy_string(lsm
.session
.name
, handle
->session_name
,
792 sizeof(lsm
.session
.name
));
794 return ask_sessiond(&lsm
, NULL
);
798 * Enable channel per domain
799 * Returns size of returned session payload data or a negative error code.
801 int lttng_enable_channel(struct lttng_handle
*handle
,
802 struct lttng_channel
*chan
)
804 struct lttcomm_session_msg lsm
;
807 * NULL arguments are forbidden. No default values.
809 if (handle
== NULL
|| chan
== NULL
) {
813 memset(&lsm
, 0, sizeof(lsm
));
815 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
817 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
819 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
821 copy_string(lsm
.session
.name
, handle
->session_name
,
822 sizeof(lsm
.session
.name
));
824 return ask_sessiond(&lsm
, NULL
);
828 * All tracing will be stopped for registered events of the channel.
829 * Returns size of returned session payload data or a negative error code.
831 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
833 struct lttcomm_session_msg lsm
;
835 /* Safety check. Both are mandatory */
836 if (handle
== NULL
|| name
== NULL
) {
840 memset(&lsm
, 0, sizeof(lsm
));
842 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
844 copy_string(lsm
.u
.disable
.channel_name
, name
,
845 sizeof(lsm
.u
.disable
.channel_name
));
847 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
849 copy_string(lsm
.session
.name
, handle
->session_name
,
850 sizeof(lsm
.session
.name
));
852 return ask_sessiond(&lsm
, NULL
);
856 * Lists all available tracepoints of domain.
857 * Sets the contents of the events array.
858 * Returns the number of lttng_event entries in events;
859 * on error, returns a negative value.
861 int lttng_list_tracepoints(struct lttng_handle
*handle
,
862 struct lttng_event
**events
)
865 struct lttcomm_session_msg lsm
;
867 if (handle
== NULL
) {
871 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
872 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
874 ret
= ask_sessiond(&lsm
, (void **) events
);
879 return ret
/ sizeof(struct lttng_event
);
883 * Lists all available tracepoint fields of domain.
884 * Sets the contents of the event field array.
885 * Returns the number of lttng_event_field entries in events;
886 * on error, returns a negative value.
888 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
889 struct lttng_event_field
**fields
)
892 struct lttcomm_session_msg lsm
;
894 if (handle
== NULL
) {
898 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
899 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
901 ret
= ask_sessiond(&lsm
, (void **) fields
);
906 return ret
/ sizeof(struct lttng_event_field
);
910 * Returns a human readable string describing
911 * the error code (a negative value).
913 const char *lttng_strerror(int code
)
915 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
916 if (code
> -LTTCOMM_OK
) {
917 return "Ended with errors";
920 return lttcomm_get_readable_code(code
);
924 * Create a brand new session using name and path.
925 * Returns size of returned session payload data or a negative error code.
927 int lttng_create_session(const char *name
, const char *path
)
929 struct lttcomm_session_msg lsm
;
931 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
932 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
933 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
935 return ask_sessiond(&lsm
, NULL
);
939 * Create a new tracing session using a name, URIs and a consumer enable flag.
941 int lttng_create_session_uri(const char *name
, struct lttng_uri
*ctrl_uri
,
942 struct lttng_uri
*data_uri
, unsigned int enable_consumer
)
944 struct lttcomm_session_msg lsm
;
946 /* Name and ctrl_uri are mandatory */
947 if (name
== NULL
|| ctrl_uri
== NULL
) {
951 lsm
.cmd_type
= LTTNG_CREATE_SESSION_URI
;
953 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
954 /* Anything bigger than zero, the consumer(s) will be enabled */
955 lsm
.u
.create_uri
.enable_consumer
= enable_consumer
;
956 memcpy(&lsm
.u
.create_uri
.ctrl_uri
, ctrl_uri
,
957 sizeof(lsm
.u
.create_uri
.ctrl_uri
));
960 * The only possible scenario where data_uri is NULL is for a local
961 * consumer where the output is at a specified path name on the
964 memcpy(&lsm
.u
.create_uri
.data_uri
, data_uri
,
965 sizeof(lsm
.u
.create_uri
.data_uri
));
968 return ask_sessiond(&lsm
, NULL
);
972 * Destroy session using name.
973 * Returns size of returned session payload data or a negative error code.
975 int lttng_destroy_session(const char *session_name
)
977 struct lttcomm_session_msg lsm
;
979 if (session_name
== NULL
) {
983 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
985 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
987 return ask_sessiond(&lsm
, NULL
);
991 * Ask the session daemon for all available sessions.
992 * Sets the contents of the sessions array.
993 * Returns the number of lttng_session entries in sessions;
994 * on error, returns a negative value.
996 int lttng_list_sessions(struct lttng_session
**sessions
)
999 struct lttcomm_session_msg lsm
;
1001 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1002 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1007 return ret
/ sizeof(struct lttng_session
);
1011 * Ask the session daemon for all available domains of a session.
1012 * Sets the contents of the domains array.
1013 * Returns the number of lttng_domain entries in domains;
1014 * on error, returns a negative value.
1016 int lttng_list_domains(const char *session_name
,
1017 struct lttng_domain
**domains
)
1020 struct lttcomm_session_msg lsm
;
1022 if (session_name
== NULL
) {
1026 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1028 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1030 ret
= ask_sessiond(&lsm
, (void**) domains
);
1035 return ret
/ sizeof(struct lttng_domain
);
1039 * Ask the session daemon for all available channels of a session.
1040 * Sets the contents of the channels array.
1041 * Returns the number of lttng_channel entries in channels;
1042 * on error, returns a negative value.
1044 int lttng_list_channels(struct lttng_handle
*handle
,
1045 struct lttng_channel
**channels
)
1048 struct lttcomm_session_msg lsm
;
1050 if (handle
== NULL
) {
1054 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1055 copy_string(lsm
.session
.name
, handle
->session_name
,
1056 sizeof(lsm
.session
.name
));
1058 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1060 ret
= ask_sessiond(&lsm
, (void**) channels
);
1065 return ret
/ sizeof(struct lttng_channel
);
1069 * Ask the session daemon for all available events of a session channel.
1070 * Sets the contents of the events array.
1071 * Returns the number of lttng_event entries in events;
1072 * on error, returns a negative value.
1074 int lttng_list_events(struct lttng_handle
*handle
,
1075 const char *channel_name
, struct lttng_event
**events
)
1078 struct lttcomm_session_msg lsm
;
1080 /* Safety check. An handle and channel name are mandatory */
1081 if (handle
== NULL
|| channel_name
== NULL
) {
1085 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1086 copy_string(lsm
.session
.name
, handle
->session_name
,
1087 sizeof(lsm
.session
.name
));
1088 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1089 sizeof(lsm
.u
.list
.channel_name
));
1091 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1093 ret
= ask_sessiond(&lsm
, (void**) events
);
1098 return ret
/ sizeof(struct lttng_event
);
1102 * Sets the tracing_group variable with name.
1103 * This function allocates memory pointed to by tracing_group.
1104 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1106 int lttng_set_tracing_group(const char *name
)
1112 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1120 * Returns size of returned session payload data or a negative error code.
1122 int lttng_calibrate(struct lttng_handle
*handle
,
1123 struct lttng_calibrate
*calibrate
)
1125 struct lttcomm_session_msg lsm
;
1127 /* Safety check. NULL pointer are forbidden */
1128 if (handle
== NULL
|| calibrate
== NULL
) {
1132 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1133 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1135 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1137 return ask_sessiond(&lsm
, NULL
);
1141 * Set default channel attributes.
1142 * If either or both of the arguments are null, attr content is zeroe'd.
1144 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1145 struct lttng_channel_attr
*attr
)
1148 if (attr
== NULL
|| domain
== NULL
) {
1152 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1154 switch (domain
->type
) {
1155 case LTTNG_DOMAIN_KERNEL
:
1156 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1157 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1158 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1160 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1161 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1162 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1164 case LTTNG_DOMAIN_UST
:
1166 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1167 case LTTNG_DOMAIN_UST_PID
:
1168 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1170 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1171 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1172 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1174 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
1175 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1176 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1179 /* Default behavior: leave set to 0. */
1185 * Check if session daemon is alive.
1187 * Return 1 if alive or 0 if not.
1188 * On error returns a negative value.
1190 int lttng_session_daemon_alive(void)
1194 ret
= set_session_daemon_path();
1200 if (strlen(sessiond_sock_path
) == 0) {
1201 /* No socket path set. Weird error */
1205 ret
= try_connect_sessiond(sessiond_sock_path
);
1216 * Set URI for a consumer for a session and domain.
1218 * Return 0 on success, else a negative value.
1220 int lttng_set_consumer_uri(struct lttng_handle
*handle
, struct lttng_uri
*uri
)
1222 struct lttcomm_session_msg lsm
;
1224 if (handle
== NULL
|| uri
== NULL
) {
1228 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1230 copy_string(lsm
.session
.name
, handle
->session_name
,
1231 sizeof(lsm
.session
.name
));
1232 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1234 memcpy(&lsm
.u
.uri
, uri
, sizeof(lsm
.u
.uri
));
1236 return ask_sessiond(&lsm
, NULL
);
1240 * Enable consumer for a session and domain.
1242 * Return 0 on success, else a negative value.
1244 int lttng_enable_consumer(struct lttng_handle
*handle
)
1246 struct lttcomm_session_msg lsm
;
1248 if (handle
== NULL
) {
1252 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1254 copy_string(lsm
.session
.name
, handle
->session_name
,
1255 sizeof(lsm
.session
.name
));
1256 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1258 return ask_sessiond(&lsm
, NULL
);
1262 * Disable consumer for a session and domain.
1264 * Return 0 on success, else a negative value.
1266 int lttng_disable_consumer(struct lttng_handle
*handle
)
1268 struct lttcomm_session_msg lsm
;
1270 if (handle
== NULL
) {
1274 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1276 copy_string(lsm
.session
.name
, handle
->session_name
,
1277 sizeof(lsm
.session
.name
));
1278 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1280 return ask_sessiond(&lsm
, NULL
);
1284 * Set health socket path by putting it in the global health_sock_path
1287 * Returns 0 on success or assert(0) on ENOMEM.
1289 static int set_health_socket_path(void)
1292 int in_tgroup
= 0; /* In tracing group */
1299 /* Are we in the tracing group ? */
1300 in_tgroup
= check_tracing_group(tracing_group
);
1303 if ((uid
== 0) || in_tgroup
) {
1304 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1305 sizeof(health_sock_path
));
1310 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1311 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1313 home
= getenv("HOME");
1315 /* Fallback in /tmp .. */
1319 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1320 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1321 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1322 /* ENOMEM at this point... just kill the control lib. */
1331 * Check session daemon health for a specific health component.
1333 * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate
1334 * that the control library was not able to connect to the session daemon
1337 * Any other positive value is an lttcomm error which can be translate with
1340 int lttng_health_check(enum lttng_health_component c
)
1343 struct lttcomm_health_msg msg
;
1344 struct lttcomm_health_data reply
;
1346 /* Connect to the sesssion daemon */
1347 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1353 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1356 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1361 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1366 ret
= reply
.ret_code
;
1378 static void __attribute__((constructor
)) init()
1380 /* Set default session group */
1381 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1382 /* Set socket for health check */
1383 (void) set_health_socket_path();