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
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <lttng/lttng.h>
33 #include <lttng-sessiond-comm.h>
35 #include "lttng-share.h"
37 /* Socket to session daemon for communication */
38 static int sessiond_socket
;
39 static char sessiond_sock_path
[PATH_MAX
];
42 static char *tracing_group
;
46 * Copy string from src to dst and enforce null terminated byte.
48 static void copy_string(char *dst
, const char *src
, size_t len
)
51 strncpy(dst
, src
, len
);
52 /* Enforce the NULL terminated byte */
60 * Copy domain to lttcomm_session_msg domain.
62 * If domain is unknown, default domain will be the kernel.
64 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
68 case LTTNG_DOMAIN_KERNEL
:
69 case LTTNG_DOMAIN_UST
:
70 case LTTNG_DOMAIN_UST_EXEC_NAME
:
71 case LTTNG_DOMAIN_UST_PID
:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
73 memcpy(dst
, src
, sizeof(struct lttng_domain
));
76 dst
->type
= LTTNG_DOMAIN_KERNEL
;
83 * Send lttcomm_session_msg to the session daemon.
85 * On success, return 0
86 * On error, return error code
88 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
97 ret
= lttcomm_send_unix_sock(sessiond_socket
, lsm
,
98 sizeof(struct lttcomm_session_msg
));
105 * Receive data from the sessiond socket.
107 * On success, return 0
108 * On error, return recv() error code
110 static int recv_data_sessiond(void *buf
, size_t len
)
119 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
126 * Check if the specified group name exist.
128 * If yes return 1, else return -1.
130 static int check_tracing_group(const char *grp_name
)
132 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
134 int grp_list_size
, grp_id
, i
;
137 /* Get GID of group 'tracing' */
138 grp_tracing
= getgrnam(grp_name
);
139 if (grp_tracing
== NULL
) {
140 /* NULL means not found also. getgrnam(3) */
147 /* Get number of supplementary group IDs */
148 grp_list_size
= getgroups(0, NULL
);
149 if (grp_list_size
< 0) {
154 /* Alloc group list of the right size */
155 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
160 grp_id
= getgroups(grp_list_size
, grp_list
);
166 for (i
= 0; i
< grp_list_size
; i
++) {
167 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
181 * Try connect to session daemon with sock_path.
183 * Return 0 on success, else -1
185 static int try_connect_sessiond(const char *sock_path
)
189 /* If socket exist, we check if the daemon listens for connect. */
190 ret
= access(sock_path
, F_OK
);
196 ret
= lttcomm_connect_unix_sock(sock_path
);
202 ret
= lttcomm_close_unix_sock(ret
);
204 perror("lttcomm_close_unix_sock");
211 * Set sessiond socket path by putting it in the global sessiond_sock_path
214 static int set_session_daemon_path(void)
217 int in_tgroup
= 0; /* In tracing group */
223 /* Are we in the tracing group ? */
224 in_tgroup
= check_tracing_group(tracing_group
);
229 copy_string(sessiond_sock_path
,
230 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
231 sizeof(sessiond_sock_path
));
232 } else if (in_tgroup
) {
234 copy_string(sessiond_sock_path
,
235 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
236 sizeof(sessiond_sock_path
));
238 ret
= try_connect_sessiond(sessiond_sock_path
);
240 /* Global session daemon not available */
241 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
242 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
243 getenv("HOME")) < 0) {
248 /* Not in tracing group and not root, default */
249 if (snprintf(sessiond_sock_path
, PATH_MAX
,
250 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
251 getenv("HOME")) < 0) {
260 * Connect to the LTTng session daemon.
262 * On success, return 0. On error, return -1.
264 static int connect_sessiond(void)
268 ret
= set_session_daemon_path();
273 /* Connect to the sesssion daemon */
274 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
279 sessiond_socket
= ret
;
286 * Clean disconnect the session daemon.
288 static int disconnect_sessiond(void)
293 ret
= lttcomm_close_unix_sock(sessiond_socket
);
302 * Ask the session daemon a specific command and put the data into buf.
304 * Return size of data (only payload, not header).
306 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
311 struct lttcomm_lttng_msg llm
;
313 ret
= connect_sessiond();
318 /* Send command to session daemon */
319 ret
= send_session_msg(lsm
);
324 /* Get header from data transmission */
325 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
330 /* Check error code if OK */
331 if (llm
.ret_code
!= LTTCOMM_OK
) {
336 size
= llm
.data_size
;
342 data
= (void*) malloc(size
);
344 /* Get payload data */
345 ret
= recv_data_sessiond(data
, size
);
355 disconnect_sessiond();
360 * Create lttng handle and return pointer.
362 struct lttng_handle
*lttng_create_handle(const char *session_name
,
363 struct lttng_domain
*domain
)
365 struct lttng_handle
*handle
;
367 handle
= malloc(sizeof(struct lttng_handle
));
368 if (handle
== NULL
) {
369 perror("malloc handle");
373 /* Copy session name */
374 copy_string(handle
->session_name
, session_name
,
375 sizeof(handle
->session_name
));
377 /* Copy lttng domain */
378 copy_lttng_domain(&handle
->domain
, domain
);
385 * Destroy handle by free(3) the pointer.
387 void lttng_destroy_handle(struct lttng_handle
*handle
)
395 * Register an outside consumer.
397 int lttng_register_consumer(struct lttng_handle
*handle
,
398 const char *socket_path
)
400 struct lttcomm_session_msg lsm
;
402 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
403 copy_string(lsm
.session
.name
, handle
->session_name
,
404 sizeof(lsm
.session
.name
));
405 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
407 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
409 return ask_sessiond(&lsm
, NULL
);
413 * Start tracing for all trace of the session.
415 int lttng_start_tracing(struct lttng_handle
*handle
)
417 struct lttcomm_session_msg lsm
;
423 lsm
.cmd_type
= LTTNG_START_TRACE
;
424 copy_string(lsm
.session
.name
, handle
->session_name
,
425 sizeof(lsm
.session
.name
));
427 return ask_sessiond(&lsm
, NULL
);
431 * Stop tracing for all trace of the session.
433 int lttng_stop_tracing(struct lttng_handle
*handle
)
435 struct lttcomm_session_msg lsm
;
437 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
438 copy_string(lsm
.session
.name
, handle
->session_name
,
439 sizeof(lsm
.session
.name
));
441 return ask_sessiond(&lsm
, NULL
);
445 * Add context to event or/and channel.
447 int lttng_add_context(struct lttng_handle
*handle
,
448 struct lttng_event_context
*ctx
, const char *event_name
,
449 const char *channel_name
)
451 struct lttcomm_session_msg lsm
;
457 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
459 /* Copy channel name */
460 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
461 sizeof(lsm
.u
.context
.channel_name
));
462 /* Copy event name */
463 copy_string(lsm
.u
.context
.event_name
, event_name
,
464 sizeof(lsm
.u
.context
.event_name
));
466 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
469 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
472 copy_string(lsm
.session
.name
, handle
->session_name
,
473 sizeof(lsm
.session
.name
));
475 return ask_sessiond(&lsm
, NULL
);
481 int lttng_enable_event(struct lttng_handle
*handle
,
482 struct lttng_event
*ev
, const char *channel_name
)
484 struct lttcomm_session_msg lsm
;
490 if (channel_name
== NULL
) {
491 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
492 sizeof(lsm
.u
.enable
.channel_name
));
494 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
495 sizeof(lsm
.u
.enable
.channel_name
));
498 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
501 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
502 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
504 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
507 copy_string(lsm
.session
.name
, handle
->session_name
,
508 sizeof(lsm
.session
.name
));
510 return ask_sessiond(&lsm
, NULL
);
514 * Disable event of a channel and domain.
516 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
517 const char *channel_name
)
519 struct lttcomm_session_msg lsm
;
526 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
527 sizeof(lsm
.u
.disable
.channel_name
));
529 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
530 sizeof(lsm
.u
.disable
.channel_name
));
533 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
536 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
537 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
539 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
542 copy_string(lsm
.session
.name
, handle
->session_name
,
543 sizeof(lsm
.session
.name
));
545 return ask_sessiond(&lsm
, NULL
);
549 * Enable channel per domain
551 int lttng_enable_channel(struct lttng_handle
*handle
,
552 struct lttng_channel
*chan
)
554 struct lttcomm_session_msg lsm
;
561 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
564 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
566 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
568 copy_string(lsm
.session
.name
, handle
->session_name
,
569 sizeof(lsm
.session
.name
));
571 return ask_sessiond(&lsm
, NULL
);
575 * All tracing will be stopped for registered events of the channel.
577 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
579 struct lttcomm_session_msg lsm
;
586 copy_string(lsm
.u
.disable
.channel_name
, name
,
587 sizeof(lsm
.u
.disable
.channel_name
));
590 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
592 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
594 copy_string(lsm
.session
.name
, handle
->session_name
,
595 sizeof(lsm
.session
.name
));
597 return ask_sessiond(&lsm
, NULL
);
601 * List all available tracepoints of domain.
603 * Return the size (bytes) of the list and set the events array.
604 * On error, return negative value.
606 int lttng_list_tracepoints(struct lttng_handle
*handle
,
607 struct lttng_event
**events
)
610 struct lttcomm_session_msg lsm
;
616 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
617 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
619 ret
= ask_sessiond(&lsm
, (void **) events
);
624 return ret
/ sizeof(struct lttng_event
);
628 * Return a human readable string of code
630 const char *lttng_get_readable_code(int code
)
632 if (code
> -LTTCOMM_OK
) {
633 return "Ended with errors";
636 return lttcomm_get_readable_code(code
);
640 * Create a brand new session using name.
642 int lttng_create_session(const char *name
, const char *path
)
644 struct lttcomm_session_msg lsm
;
646 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
647 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
648 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
650 return ask_sessiond(&lsm
, NULL
);
654 * Destroy session using name.
656 int lttng_destroy_session(struct lttng_handle
*handle
)
658 struct lttcomm_session_msg lsm
;
664 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
665 copy_string(lsm
.session
.name
, handle
->session_name
,
666 sizeof(lsm
.session
.name
));
668 return ask_sessiond(&lsm
, NULL
);
672 * Ask the session daemon for all available sessions.
674 * Return number of session.
675 * On error, return negative value.
677 int lttng_list_sessions(struct lttng_session
**sessions
)
680 struct lttcomm_session_msg lsm
;
682 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
683 ret
= ask_sessiond(&lsm
, (void**) sessions
);
688 return ret
/ sizeof(struct lttng_session
);
692 * List domain of a session.
694 int lttng_list_domains(struct lttng_handle
*handle
,
695 struct lttng_domain
**domains
)
698 struct lttcomm_session_msg lsm
;
704 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
706 copy_string(lsm
.session
.name
, handle
->session_name
,
707 sizeof(lsm
.session
.name
));
709 ret
= ask_sessiond(&lsm
, (void**) domains
);
714 return ret
/ sizeof(struct lttng_domain
);
718 * List channels of a session
720 int lttng_list_channels(struct lttng_handle
*handle
,
721 struct lttng_channel
**channels
)
724 struct lttcomm_session_msg lsm
;
730 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
731 copy_string(lsm
.session
.name
, handle
->session_name
,
732 sizeof(lsm
.session
.name
));
734 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
736 ret
= ask_sessiond(&lsm
, (void**) channels
);
741 return ret
/ sizeof(struct lttng_channel
);
745 * List events of a session channel.
747 int lttng_list_events(struct lttng_handle
*handle
,
748 const char *channel_name
, struct lttng_event
**events
)
751 struct lttcomm_session_msg lsm
;
757 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
758 copy_string(lsm
.session
.name
, handle
->session_name
,
759 sizeof(lsm
.session
.name
));
760 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
761 sizeof(lsm
.u
.list
.channel_name
));
763 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
765 ret
= ask_sessiond(&lsm
, (void**) events
);
770 return ret
/ sizeof(struct lttng_event
);
774 * lttng_set_tracing_group
776 * Set tracing group variable with name. This function
777 * allocate memory pointed by tracing_group.
779 int lttng_set_tracing_group(const char *name
)
781 if (asprintf(&tracing_group
, "%s", name
) < 0) {
791 int lttng_calibrate(struct lttng_handle
*handle
,
792 struct lttng_calibrate
*calibrate
)
794 struct lttcomm_session_msg lsm
;
800 lsm
.cmd_type
= LTTNG_CALIBRATE
;
801 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
803 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
805 return ask_sessiond(&lsm
, NULL
);
809 * Check if session daemon is alive.
811 * Return 1 if alive or 0 if not.
814 int lttng_session_daemon_alive(void)
818 ret
= set_session_daemon_path();
824 if (strlen(sessiond_sock_path
) == 0) {
825 /* No socket path set. Weird error */
829 ret
= try_connect_sessiond(sessiond_sock_path
);
842 static void __attribute__((constructor
)) init()
844 /* Set default session group */
845 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);