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
));
156 grp_id
= getgroups(grp_list_size
, grp_list
);
162 for (i
= 0; i
< grp_list_size
; i
++) {
163 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
177 * Try connect to session daemon with sock_path.
179 * Return 0 on success, else -1
181 static int try_connect_sessiond(const char *sock_path
)
185 /* If socket exist, we check if the daemon listens for connect. */
186 ret
= access(sock_path
, F_OK
);
192 ret
= lttcomm_connect_unix_sock(sock_path
);
198 ret
= lttcomm_close_unix_sock(ret
);
200 perror("lttcomm_close_unix_sock");
207 * Set sessiond socket path by putting it in the global sessiond_sock_path
210 static int set_session_daemon_path(void)
213 int in_tgroup
= 0; /* In tracing group */
219 /* Are we in the tracing group ? */
220 in_tgroup
= check_tracing_group(tracing_group
);
225 copy_string(sessiond_sock_path
,
226 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
227 sizeof(sessiond_sock_path
));
228 } else if (in_tgroup
) {
230 copy_string(sessiond_sock_path
,
231 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
232 sizeof(sessiond_sock_path
));
234 ret
= try_connect_sessiond(sessiond_sock_path
);
236 /* Global session daemon not available */
237 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
238 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
239 getenv("HOME")) < 0) {
244 /* Not in tracing group and not root, default */
245 if (snprintf(sessiond_sock_path
, PATH_MAX
,
246 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
247 getenv("HOME")) < 0) {
256 * Connect to the LTTng session daemon.
258 * On success, return 0. On error, return -1.
260 static int connect_sessiond(void)
264 ret
= set_session_daemon_path();
269 /* Connect to the sesssion daemon */
270 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
275 sessiond_socket
= ret
;
282 * Clean disconnect the session daemon.
284 static int disconnect_sessiond(void)
289 ret
= lttcomm_close_unix_sock(sessiond_socket
);
298 * Ask the session daemon a specific command and put the data into buf.
300 * Return size of data (only payload, not header).
302 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
307 struct lttcomm_lttng_msg llm
;
309 ret
= connect_sessiond();
314 /* Send command to session daemon */
315 ret
= send_session_msg(lsm
);
320 /* Get header from data transmission */
321 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
326 /* Check error code if OK */
327 if (llm
.ret_code
!= LTTCOMM_OK
) {
332 size
= llm
.data_size
;
338 data
= (void*) malloc(size
);
340 /* Get payload data */
341 ret
= recv_data_sessiond(data
, size
);
351 disconnect_sessiond();
356 * Create lttng handle and return pointer.
358 struct lttng_handle
*lttng_create_handle(const char *session_name
,
359 struct lttng_domain
*domain
)
361 struct lttng_handle
*handle
;
363 handle
= malloc(sizeof(struct lttng_handle
));
364 if (handle
== NULL
) {
365 perror("malloc handle");
369 /* Copy session name */
370 copy_string(handle
->session_name
, session_name
,
371 sizeof(handle
->session_name
));
373 /* Copy lttng domain */
374 copy_lttng_domain(&handle
->domain
, domain
);
381 * Destroy handle by free(3) the pointer.
383 void lttng_destroy_handle(struct lttng_handle
*handle
)
391 * Register an outside consumer.
393 int lttng_register_consumer(struct lttng_handle
*handle
,
394 const char *socket_path
)
396 struct lttcomm_session_msg lsm
;
398 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
399 copy_string(lsm
.session
.name
, handle
->session_name
,
400 sizeof(lsm
.session
.name
));
401 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
403 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
405 return ask_sessiond(&lsm
, NULL
);
409 * Start tracing for all trace of the session.
411 int lttng_start_tracing(struct lttng_handle
*handle
)
413 struct lttcomm_session_msg lsm
;
419 lsm
.cmd_type
= LTTNG_START_TRACE
;
420 copy_string(lsm
.session
.name
, handle
->session_name
,
421 sizeof(lsm
.session
.name
));
423 return ask_sessiond(&lsm
, NULL
);
427 * Stop tracing for all trace of the session.
429 int lttng_stop_tracing(struct lttng_handle
*handle
)
431 struct lttcomm_session_msg lsm
;
433 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
434 copy_string(lsm
.session
.name
, handle
->session_name
,
435 sizeof(lsm
.session
.name
));
437 return ask_sessiond(&lsm
, NULL
);
441 * Add context to event or/and channel.
443 int lttng_add_context(struct lttng_handle
*handle
,
444 struct lttng_event_context
*ctx
, const char *event_name
,
445 const char *channel_name
)
447 struct lttcomm_session_msg lsm
;
453 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
455 /* Copy channel name */
456 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
457 sizeof(lsm
.u
.context
.channel_name
));
458 /* Copy event name */
459 copy_string(lsm
.u
.context
.event_name
, event_name
,
460 sizeof(lsm
.u
.context
.event_name
));
462 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
465 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
468 copy_string(lsm
.session
.name
, handle
->session_name
,
469 sizeof(lsm
.session
.name
));
471 return ask_sessiond(&lsm
, NULL
);
477 int lttng_enable_event(struct lttng_handle
*handle
,
478 struct lttng_event
*ev
, const char *channel_name
)
480 struct lttcomm_session_msg lsm
;
486 if (channel_name
== NULL
) {
487 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
488 sizeof(lsm
.u
.enable
.channel_name
));
490 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
491 sizeof(lsm
.u
.enable
.channel_name
));
494 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
497 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
498 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
500 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
503 copy_string(lsm
.session
.name
, handle
->session_name
,
504 sizeof(lsm
.session
.name
));
506 return ask_sessiond(&lsm
, NULL
);
510 * Disable event of a channel and domain.
512 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
513 const char *channel_name
)
515 struct lttcomm_session_msg lsm
;
522 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
523 sizeof(lsm
.u
.disable
.channel_name
));
525 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
526 sizeof(lsm
.u
.disable
.channel_name
));
529 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
532 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
533 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
535 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
538 copy_string(lsm
.session
.name
, handle
->session_name
,
539 sizeof(lsm
.session
.name
));
541 return ask_sessiond(&lsm
, NULL
);
545 * Enable channel per domain
547 int lttng_enable_channel(struct lttng_handle
*handle
,
548 struct lttng_channel
*chan
)
550 struct lttcomm_session_msg lsm
;
557 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
560 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
562 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
564 copy_string(lsm
.session
.name
, handle
->session_name
,
565 sizeof(lsm
.session
.name
));
567 return ask_sessiond(&lsm
, NULL
);
571 * All tracing will be stopped for registered events of the channel.
573 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
575 struct lttcomm_session_msg lsm
;
582 copy_string(lsm
.u
.disable
.channel_name
, name
,
583 sizeof(lsm
.u
.disable
.channel_name
));
586 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
588 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
590 copy_string(lsm
.session
.name
, handle
->session_name
,
591 sizeof(lsm
.session
.name
));
593 return ask_sessiond(&lsm
, NULL
);
597 * List all available tracepoints of domain.
599 * Return the size (bytes) of the list and set the events array.
600 * On error, return negative value.
602 int lttng_list_tracepoints(struct lttng_handle
*handle
,
603 struct lttng_event
**events
)
606 struct lttcomm_session_msg lsm
;
612 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
613 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
615 ret
= ask_sessiond(&lsm
, (void **) events
);
620 return ret
/ sizeof(struct lttng_event
);
624 * Return a human readable string of code
626 const char *lttng_get_readable_code(int code
)
628 if (code
> -LTTCOMM_OK
) {
629 return "Ended with errors";
632 return lttcomm_get_readable_code(code
);
636 * Create a brand new session using name.
638 int lttng_create_session(const char *name
, const char *path
)
640 struct lttcomm_session_msg lsm
;
642 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
643 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
644 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
646 return ask_sessiond(&lsm
, NULL
);
650 * Destroy session using name.
652 int lttng_destroy_session(struct lttng_handle
*handle
)
654 struct lttcomm_session_msg lsm
;
660 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
661 copy_string(lsm
.session
.name
, handle
->session_name
,
662 sizeof(lsm
.session
.name
));
664 return ask_sessiond(&lsm
, NULL
);
668 * Ask the session daemon for all available sessions.
670 * Return number of session.
671 * On error, return negative value.
673 int lttng_list_sessions(struct lttng_session
**sessions
)
676 struct lttcomm_session_msg lsm
;
678 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
679 ret
= ask_sessiond(&lsm
, (void**) sessions
);
684 return ret
/ sizeof(struct lttng_session
);
688 * List domain of a session.
690 int lttng_list_domains(struct lttng_handle
*handle
,
691 struct lttng_domain
**domains
)
694 struct lttcomm_session_msg lsm
;
700 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
702 copy_string(lsm
.session
.name
, handle
->session_name
,
703 sizeof(lsm
.session
.name
));
705 ret
= ask_sessiond(&lsm
, (void**) domains
);
710 return ret
/ sizeof(struct lttng_domain
);
714 * List channels of a session
716 int lttng_list_channels(struct lttng_handle
*handle
,
717 struct lttng_channel
**channels
)
720 struct lttcomm_session_msg lsm
;
726 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
727 copy_string(lsm
.session
.name
, handle
->session_name
,
728 sizeof(lsm
.session
.name
));
730 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
732 ret
= ask_sessiond(&lsm
, (void**) channels
);
737 return ret
/ sizeof(struct lttng_channel
);
741 * List events of a session channel.
743 int lttng_list_events(struct lttng_handle
*handle
,
744 const char *channel_name
, struct lttng_event
**events
)
747 struct lttcomm_session_msg lsm
;
753 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
754 copy_string(lsm
.session
.name
, handle
->session_name
,
755 sizeof(lsm
.session
.name
));
756 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
757 sizeof(lsm
.u
.list
.channel_name
));
759 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
761 ret
= ask_sessiond(&lsm
, (void**) events
);
766 return ret
/ sizeof(struct lttng_event
);
770 * lttng_set_tracing_group
772 * Set tracing group variable with name. This function
773 * allocate memory pointed by tracing_group.
775 int lttng_set_tracing_group(const char *name
)
777 if (asprintf(&tracing_group
, "%s", name
) < 0) {
787 int lttng_calibrate(struct lttng_handle
*handle
,
788 struct lttng_calibrate
*calibrate
)
790 struct lttcomm_session_msg lsm
;
796 lsm
.cmd_type
= LTTNG_CALIBRATE
;
797 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
799 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
801 return ask_sessiond(&lsm
, NULL
);
805 * Check if session daemon is alive.
807 * Return 1 if alive or 0 if not.
810 int lttng_session_daemon_alive(void)
814 ret
= set_session_daemon_path();
820 if (strlen(sessiond_sock_path
) == 0) {
821 /* No socket path set. Weird error */
825 ret
= try_connect_sessiond(sessiond_sock_path
);
838 static void __attribute__((constructor
)) init()
840 /* Set default session group */
841 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);