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 /* Socket to session daemon for communication */
36 static int sessiond_socket
;
37 static char sessiond_sock_path
[PATH_MAX
];
40 static char *tracing_group
;
46 * Those two variables are used by error.h to silent or control the verbosity of
47 * error message. They are global to the library so application linking with it
48 * are able to compile correctly and also control verbosity of the library.
50 * Note that it is *not* possible to silent ERR() and PERROR() macros.
53 int lttng_opt_verbose
;
56 * Copy string from src to dst and enforce null terminated byte.
58 static void copy_string(char *dst
, const char *src
, size_t len
)
61 strncpy(dst
, src
, len
);
62 /* Enforce the NULL terminated byte */
70 * Copy domain to lttcomm_session_msg domain.
72 * If domain is unknown, default domain will be the kernel.
74 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
78 case LTTNG_DOMAIN_KERNEL
:
79 case LTTNG_DOMAIN_UST
:
81 case LTTNG_DOMAIN_UST_EXEC_NAME:
82 case LTTNG_DOMAIN_UST_PID:
83 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
85 memcpy(dst
, src
, sizeof(struct lttng_domain
));
88 memset(dst
, 0, sizeof(struct lttng_domain
));
89 dst
->type
= LTTNG_DOMAIN_KERNEL
;
96 * Send lttcomm_session_msg to the session daemon.
98 * On success, returns the number of bytes sent (>=0)
99 * On error, returns -1
101 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
110 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
111 sizeof(struct lttcomm_session_msg
));
118 * Receive data from the sessiond socket.
120 * On success, returns the number of bytes received (>=0)
121 * On error, returns -1 (recvmsg() error) or -ENOTCONN
123 static int recv_data_sessiond(void *buf
, size_t len
)
132 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
139 * Check if we are in the specified group.
141 * If yes return 1, else return -1.
143 static int check_tracing_group(const char *grp_name
)
145 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
147 int grp_list_size
, grp_id
, i
;
150 /* Get GID of group 'tracing' */
151 grp_tracing
= getgrnam(grp_name
);
153 /* If grp_tracing is NULL, the group does not exist. */
157 /* Get number of supplementary group IDs */
158 grp_list_size
= getgroups(0, NULL
);
159 if (grp_list_size
< 0) {
164 /* Alloc group list of the right size */
165 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
170 grp_id
= getgroups(grp_list_size
, grp_list
);
176 for (i
= 0; i
< grp_list_size
; i
++) {
177 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
191 * Try connect to session daemon with sock_path.
193 * Return 0 on success, else -1
195 static int try_connect_sessiond(const char *sock_path
)
199 /* If socket exist, we check if the daemon listens for connect. */
200 ret
= access(sock_path
, F_OK
);
206 ret
= lttcomm_connect_unix_sock(sock_path
);
212 ret
= lttcomm_close_unix_sock(ret
);
214 perror("lttcomm_close_unix_sock");
221 * Set sessiond socket path by putting it in the global
222 * sessiond_sock_path variable.
223 * Returns 0 on success,
224 * -ENOMEM on failure (the sessiond socket path is somehow too long)
226 static int set_session_daemon_path(void)
229 int in_tgroup
= 0; /* In tracing group */
235 /* Are we in the tracing group ? */
236 in_tgroup
= check_tracing_group(tracing_group
);
239 if ((uid
== 0) || in_tgroup
) {
240 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
241 sizeof(sessiond_sock_path
));
247 ret
= try_connect_sessiond(sessiond_sock_path
);
251 /* Global session daemon not available... */
253 /* ...or not in tracing group (and not root), default */
256 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
257 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
259 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
260 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
261 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
270 * Connect to the LTTng session daemon.
272 * On success, return 0. On error, return -1.
274 static int connect_sessiond(void)
278 ret
= set_session_daemon_path();
280 return -1; /* set_session_daemon_path() returns -ENOMEM */
283 /* Connect to the sesssion daemon */
284 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
289 sessiond_socket
= ret
;
296 * Clean disconnect from the session daemon.
297 * On success, return 0. On error, return -1.
299 static int disconnect_sessiond(void)
304 ret
= lttcomm_close_unix_sock(sessiond_socket
);
313 * Ask the session daemon a specific command and put the data into buf.
315 * Return size of data (only payload, not header) or a negative error code.
317 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
322 struct lttcomm_lttng_msg llm
;
324 ret
= connect_sessiond();
329 /* Send command to session daemon */
330 ret
= send_session_msg(lsm
);
335 /* Get header from data transmission */
336 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
341 /* Check error code if OK */
342 if (llm
.ret_code
!= LTTCOMM_OK
) {
347 size
= llm
.data_size
;
349 /* If client free with size 0 */
357 data
= (void*) malloc(size
);
359 /* Get payload data */
360 ret
= recv_data_sessiond(data
, size
);
367 * Extra protection not to dereference a NULL pointer. If buf is NULL at
368 * this point, an error is returned and data is freed.
380 disconnect_sessiond();
385 * Create lttng handle and return pointer.
386 * The returned pointer will be NULL in case of malloc() error.
388 struct lttng_handle
*lttng_create_handle(const char *session_name
,
389 struct lttng_domain
*domain
)
391 struct lttng_handle
*handle
;
393 handle
= malloc(sizeof(struct lttng_handle
));
394 if (handle
== NULL
) {
395 perror("malloc handle");
399 /* Copy session name */
400 copy_string(handle
->session_name
, session_name
,
401 sizeof(handle
->session_name
));
403 /* Copy lttng domain */
404 copy_lttng_domain(&handle
->domain
, domain
);
411 * Destroy handle by free(3) the pointer.
413 void lttng_destroy_handle(struct lttng_handle
*handle
)
421 * Register an outside consumer.
422 * Returns size of returned session payload data or a negative error code.
424 int lttng_register_consumer(struct lttng_handle
*handle
,
425 const char *socket_path
)
427 struct lttcomm_session_msg lsm
;
429 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
430 copy_string(lsm
.session
.name
, handle
->session_name
,
431 sizeof(lsm
.session
.name
));
432 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
434 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
436 return ask_sessiond(&lsm
, NULL
);
440 * Start tracing for all traces of the session.
441 * Returns size of returned session payload data or a negative error code.
443 int lttng_start_tracing(const char *session_name
)
445 struct lttcomm_session_msg lsm
;
447 if (session_name
== NULL
) {
451 lsm
.cmd_type
= LTTNG_START_TRACE
;
453 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
455 return ask_sessiond(&lsm
, NULL
);
459 * Stop tracing for all traces of the session.
460 * Returns size of returned session payload data or a negative error code.
462 int lttng_stop_tracing(const char *session_name
)
464 struct lttcomm_session_msg lsm
;
466 if (session_name
== NULL
) {
470 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
472 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
474 return ask_sessiond(&lsm
, NULL
);
478 * Add context to event and/or channel.
479 * If event_name is NULL, the context is applied to all events of the channel.
480 * If channel_name is NULL, a lookup of the event's channel is done.
481 * If both are NULL, the context is applied to all events of all channels.
483 * Returns the size of the returned payload data or a negative error code.
485 int lttng_add_context(struct lttng_handle
*handle
,
486 struct lttng_event_context
*ctx
, const char *event_name
,
487 const char *channel_name
)
489 struct lttcomm_session_msg lsm
;
491 /* Safety check. Both are mandatory */
492 if (handle
== NULL
|| ctx
== NULL
) {
496 memset(&lsm
, 0, sizeof(lsm
));
498 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
500 /* Copy channel name */
501 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
502 sizeof(lsm
.u
.context
.channel_name
));
503 /* Copy event name */
504 copy_string(lsm
.u
.context
.event_name
, event_name
,
505 sizeof(lsm
.u
.context
.event_name
));
507 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
509 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
511 copy_string(lsm
.session
.name
, handle
->session_name
,
512 sizeof(lsm
.session
.name
));
514 return ask_sessiond(&lsm
, NULL
);
518 * Enable event(s) for a channel.
519 * If no event name is specified, all events are enabled.
520 * If no channel name is specified, the default 'channel0' is used.
521 * Returns size of returned session payload data or a negative error code.
523 int lttng_enable_event(struct lttng_handle
*handle
,
524 struct lttng_event
*ev
, const char *channel_name
)
526 struct lttcomm_session_msg lsm
;
528 if (handle
== NULL
|| ev
== NULL
) {
532 memset(&lsm
, 0, sizeof(lsm
));
534 /* If no channel name, we put the default name */
535 if (channel_name
== NULL
) {
536 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
537 sizeof(lsm
.u
.enable
.channel_name
));
539 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
540 sizeof(lsm
.u
.enable
.channel_name
));
543 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
545 if (ev
->name
[0] != '\0') {
546 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
548 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
550 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
552 copy_string(lsm
.session
.name
, handle
->session_name
,
553 sizeof(lsm
.session
.name
));
555 return ask_sessiond(&lsm
, NULL
);
559 * Disable event(s) of a channel and domain.
560 * If no event name is specified, all events are disabled.
561 * If no channel name is specified, the default 'channel0' is used.
562 * Returns size of returned session payload data or a negative error code.
564 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
565 const char *channel_name
)
567 struct lttcomm_session_msg lsm
;
569 if (handle
== NULL
) {
573 memset(&lsm
, 0, sizeof(lsm
));
576 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
577 sizeof(lsm
.u
.disable
.channel_name
));
579 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
580 sizeof(lsm
.u
.disable
.channel_name
));
583 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
586 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
587 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
589 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
592 copy_string(lsm
.session
.name
, handle
->session_name
,
593 sizeof(lsm
.session
.name
));
595 return ask_sessiond(&lsm
, NULL
);
599 * Enable channel per domain
600 * Returns size of returned session payload data or a negative error code.
602 int lttng_enable_channel(struct lttng_handle
*handle
,
603 struct lttng_channel
*chan
)
605 struct lttcomm_session_msg lsm
;
608 * NULL arguments are forbidden. No default values.
610 if (handle
== NULL
|| chan
== NULL
) {
614 memset(&lsm
, 0, sizeof(lsm
));
616 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
618 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
620 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
622 copy_string(lsm
.session
.name
, handle
->session_name
,
623 sizeof(lsm
.session
.name
));
625 return ask_sessiond(&lsm
, NULL
);
629 * All tracing will be stopped for registered events of the channel.
630 * Returns size of returned session payload data or a negative error code.
632 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
634 struct lttcomm_session_msg lsm
;
636 /* Safety check. Both are mandatory */
637 if (handle
== NULL
|| name
== NULL
) {
641 memset(&lsm
, 0, sizeof(lsm
));
643 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
645 copy_string(lsm
.u
.disable
.channel_name
, name
,
646 sizeof(lsm
.u
.disable
.channel_name
));
648 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
650 copy_string(lsm
.session
.name
, handle
->session_name
,
651 sizeof(lsm
.session
.name
));
653 return ask_sessiond(&lsm
, NULL
);
657 * Lists all available tracepoints of domain.
658 * Sets the contents of the events array.
659 * Returns the number of lttng_event entries in events;
660 * on error, returns a negative value.
662 int lttng_list_tracepoints(struct lttng_handle
*handle
,
663 struct lttng_event
**events
)
666 struct lttcomm_session_msg lsm
;
668 if (handle
== NULL
) {
672 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
673 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
675 ret
= ask_sessiond(&lsm
, (void **) events
);
680 return ret
/ sizeof(struct lttng_event
);
684 * Returns a human readable string describing
685 * the error code (a negative value).
687 const char *lttng_strerror(int code
)
689 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
690 if (code
> -LTTCOMM_OK
) {
691 return "Ended with errors";
694 return lttcomm_get_readable_code(code
);
698 * Create a brand new session using name and path.
699 * Returns size of returned session payload data or a negative error code.
701 int lttng_create_session(const char *name
, const char *path
)
703 struct lttcomm_session_msg lsm
;
705 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
706 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
707 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
709 return ask_sessiond(&lsm
, NULL
);
713 * Destroy session using name.
714 * Returns size of returned session payload data or a negative error code.
716 int lttng_destroy_session(const char *session_name
)
718 struct lttcomm_session_msg lsm
;
720 if (session_name
== NULL
) {
724 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
726 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
728 return ask_sessiond(&lsm
, NULL
);
732 * Ask the session daemon for all available sessions.
733 * Sets the contents of the sessions array.
734 * Returns the number of lttng_session entries in sessions;
735 * on error, returns a negative value.
737 int lttng_list_sessions(struct lttng_session
**sessions
)
740 struct lttcomm_session_msg lsm
;
742 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
743 ret
= ask_sessiond(&lsm
, (void**) sessions
);
748 return ret
/ sizeof(struct lttng_session
);
752 * Ask the session daemon for all available domains of a session.
753 * Sets the contents of the domains array.
754 * Returns the number of lttng_domain entries in domains;
755 * on error, returns a negative value.
757 int lttng_list_domains(const char *session_name
,
758 struct lttng_domain
**domains
)
761 struct lttcomm_session_msg lsm
;
763 if (session_name
== NULL
) {
767 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
769 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
771 ret
= ask_sessiond(&lsm
, (void**) domains
);
776 return ret
/ sizeof(struct lttng_domain
);
780 * Ask the session daemon for all available channels of a session.
781 * Sets the contents of the channels array.
782 * Returns the number of lttng_channel entries in channels;
783 * on error, returns a negative value.
785 int lttng_list_channels(struct lttng_handle
*handle
,
786 struct lttng_channel
**channels
)
789 struct lttcomm_session_msg lsm
;
791 if (handle
== NULL
) {
795 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
796 copy_string(lsm
.session
.name
, handle
->session_name
,
797 sizeof(lsm
.session
.name
));
799 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
801 ret
= ask_sessiond(&lsm
, (void**) channels
);
806 return ret
/ sizeof(struct lttng_channel
);
810 * Ask the session daemon for all available events of a session channel.
811 * Sets the contents of the events array.
812 * Returns the number of lttng_event entries in events;
813 * on error, returns a negative value.
815 int lttng_list_events(struct lttng_handle
*handle
,
816 const char *channel_name
, struct lttng_event
**events
)
819 struct lttcomm_session_msg lsm
;
821 /* Safety check. An handle and channel name are mandatory */
822 if (handle
== NULL
|| channel_name
== NULL
) {
826 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
827 copy_string(lsm
.session
.name
, handle
->session_name
,
828 sizeof(lsm
.session
.name
));
829 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
830 sizeof(lsm
.u
.list
.channel_name
));
832 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
834 ret
= ask_sessiond(&lsm
, (void**) events
);
839 return ret
/ sizeof(struct lttng_event
);
843 * Sets the tracing_group variable with name.
844 * This function allocates memory pointed to by tracing_group.
845 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
847 int lttng_set_tracing_group(const char *name
)
853 if (asprintf(&tracing_group
, "%s", name
) < 0) {
861 * Returns size of returned session payload data or a negative error code.
863 int lttng_calibrate(struct lttng_handle
*handle
,
864 struct lttng_calibrate
*calibrate
)
866 struct lttcomm_session_msg lsm
;
868 /* Safety check. NULL pointer are forbidden */
869 if (handle
== NULL
|| calibrate
== NULL
) {
873 lsm
.cmd_type
= LTTNG_CALIBRATE
;
874 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
876 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
878 return ask_sessiond(&lsm
, NULL
);
882 * Set default channel attributes.
883 * If either or both of the arguments are null, attr content is zeroe'd.
885 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
886 struct lttng_channel_attr
*attr
)
888 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
891 if (attr
== NULL
|| domain
== NULL
) {
895 switch (domain
->type
) {
896 case LTTNG_DOMAIN_KERNEL
:
897 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
898 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
899 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
901 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
902 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
903 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
905 case LTTNG_DOMAIN_UST
:
907 case LTTNG_DOMAIN_UST_EXEC_NAME
:
908 case LTTNG_DOMAIN_UST_PID
:
909 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
911 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
912 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
913 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
915 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
916 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
917 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
920 /* Default behavior: leave set to 0. */
926 * Check if session daemon is alive.
928 * Return 1 if alive or 0 if not.
929 * On error returns a negative value.
931 int lttng_session_daemon_alive(void)
935 ret
= set_session_daemon_path();
941 if (strlen(sessiond_sock_path
) == 0) {
942 /* No socket path set. Weird error */
946 ret
= try_connect_sessiond(sessiond_sock_path
);
959 static void __attribute__((constructor
)) init()
961 /* Set default session group */
962 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);