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 0, 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 * Set sessiond socket path by putting it in the global sessiond_sock_path
180 static int set_session_daemon_path(void)
184 /* Are we in the tracing group ? */
185 ret
= check_tracing_group(tracing_group
);
186 if (ret
< 0 && getuid() != 0) {
187 if (snprintf(sessiond_sock_path
, PATH_MAX
,
188 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
189 getenv("HOME")) < 0) {
193 copy_string(sessiond_sock_path
,
194 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
202 * Connect to the LTTng session daemon.
204 * On success, return 0. On error, return -1.
206 static int connect_sessiond(void)
210 ret
= set_session_daemon_path();
215 /* Connect to the sesssion daemon */
216 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
221 sessiond_socket
= ret
;
228 * Clean disconnect the session daemon.
230 static int disconnect_sessiond(void)
235 ret
= lttcomm_close_unix_sock(sessiond_socket
);
244 * Ask the session daemon a specific command and put the data into buf.
246 * Return size of data (only payload, not header).
248 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
253 struct lttcomm_lttng_msg llm
;
255 ret
= connect_sessiond();
260 /* Send command to session daemon */
261 ret
= send_session_msg(lsm
);
266 /* Get header from data transmission */
267 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
272 /* Check error code if OK */
273 if (llm
.ret_code
!= LTTCOMM_OK
) {
278 size
= llm
.data_size
;
284 data
= (void*) malloc(size
);
286 /* Get payload data */
287 ret
= recv_data_sessiond(data
, size
);
297 disconnect_sessiond();
302 * Create lttng handle and return pointer.
304 struct lttng_handle
*lttng_create_handle(const char *session_name
,
305 struct lttng_domain
*domain
)
307 struct lttng_handle
*handle
;
309 handle
= malloc(sizeof(struct lttng_handle
));
310 if (handle
== NULL
) {
311 perror("malloc handle");
315 /* Copy session name */
316 copy_string(handle
->session_name
, session_name
,
317 sizeof(handle
->session_name
));
319 /* Copy lttng domain */
320 copy_lttng_domain(&handle
->domain
, domain
);
327 * Destroy handle by free(3) the pointer.
329 void lttng_destroy_handle(struct lttng_handle
*handle
)
337 * Register an outside consumer.
339 int lttng_register_consumer(struct lttng_handle
*handle
,
340 const char *socket_path
)
342 struct lttcomm_session_msg lsm
;
344 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
345 copy_string(lsm
.session
.name
, handle
->session_name
,
346 sizeof(lsm
.session
.name
));
347 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
349 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
351 return ask_sessiond(&lsm
, NULL
);
355 * Start tracing for all trace of the session.
357 int lttng_start_tracing(struct lttng_handle
*handle
)
359 struct lttcomm_session_msg lsm
;
365 lsm
.cmd_type
= LTTNG_START_TRACE
;
366 copy_string(lsm
.session
.name
, handle
->session_name
,
367 sizeof(lsm
.session
.name
));
369 return ask_sessiond(&lsm
, NULL
);
373 * Stop tracing for all trace of the session.
375 int lttng_stop_tracing(struct lttng_handle
*handle
)
377 struct lttcomm_session_msg lsm
;
379 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
380 copy_string(lsm
.session
.name
, handle
->session_name
,
381 sizeof(lsm
.session
.name
));
383 return ask_sessiond(&lsm
, NULL
);
387 * Add context to event or/and channel.
389 int lttng_add_context(struct lttng_handle
*handle
,
390 struct lttng_event_context
*ctx
, const char *event_name
,
391 const char *channel_name
)
393 struct lttcomm_session_msg lsm
;
399 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
401 /* Copy channel name */
402 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
403 sizeof(lsm
.u
.context
.channel_name
));
404 /* Copy event name */
405 copy_string(lsm
.u
.context
.event_name
, event_name
,
406 sizeof(lsm
.u
.context
.event_name
));
408 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
411 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
414 copy_string(lsm
.session
.name
, handle
->session_name
,
415 sizeof(lsm
.session
.name
));
417 return ask_sessiond(&lsm
, NULL
);
423 int lttng_enable_event(struct lttng_handle
*handle
,
424 struct lttng_event
*ev
, const char *channel_name
)
426 struct lttcomm_session_msg lsm
;
432 if (channel_name
== NULL
) {
433 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
434 sizeof(lsm
.u
.enable
.channel_name
));
436 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
437 sizeof(lsm
.u
.enable
.channel_name
));
440 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
443 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
444 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
446 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
449 copy_string(lsm
.session
.name
, handle
->session_name
,
450 sizeof(lsm
.session
.name
));
452 return ask_sessiond(&lsm
, NULL
);
456 * Disable event of a channel and domain.
458 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
459 const char *channel_name
)
461 struct lttcomm_session_msg lsm
;
468 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
469 sizeof(lsm
.u
.disable
.channel_name
));
471 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
472 sizeof(lsm
.u
.disable
.channel_name
));
475 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
478 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
479 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
481 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
484 copy_string(lsm
.session
.name
, handle
->session_name
,
485 sizeof(lsm
.session
.name
));
487 return ask_sessiond(&lsm
, NULL
);
491 * Enable channel per domain
493 int lttng_enable_channel(struct lttng_handle
*handle
,
494 struct lttng_channel
*chan
)
496 struct lttcomm_session_msg lsm
;
503 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
506 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
508 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
510 copy_string(lsm
.session
.name
, handle
->session_name
,
511 sizeof(lsm
.session
.name
));
513 return ask_sessiond(&lsm
, NULL
);
517 * All tracing will be stopped for registered events of the channel.
519 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
521 struct lttcomm_session_msg lsm
;
528 copy_string(lsm
.u
.disable
.channel_name
, name
,
529 sizeof(lsm
.u
.disable
.channel_name
));
532 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
534 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
536 copy_string(lsm
.session
.name
, handle
->session_name
,
537 sizeof(lsm
.session
.name
));
539 return ask_sessiond(&lsm
, NULL
);
543 * List all available tracepoints of domain.
545 * Return the size (bytes) of the list and set the events array.
546 * On error, return negative value.
548 int lttng_list_tracepoints(struct lttng_handle
*handle
,
549 struct lttng_event
**events
)
552 struct lttcomm_session_msg lsm
;
558 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
559 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
561 ret
= ask_sessiond(&lsm
, (void **) events
);
566 return ret
/ sizeof(struct lttng_event
);
570 * Return a human readable string of code
572 const char *lttng_get_readable_code(int code
)
574 if (code
> -LTTCOMM_OK
) {
575 return "Ended with errors";
578 return lttcomm_get_readable_code(code
);
582 * Create a brand new session using name.
584 int lttng_create_session(const char *name
, const char *path
)
586 struct lttcomm_session_msg lsm
;
588 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
589 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
590 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
592 return ask_sessiond(&lsm
, NULL
);
596 * Destroy session using name.
598 int lttng_destroy_session(struct lttng_handle
*handle
)
600 struct lttcomm_session_msg lsm
;
606 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
607 copy_string(lsm
.session
.name
, handle
->session_name
,
608 sizeof(lsm
.session
.name
));
610 return ask_sessiond(&lsm
, NULL
);
614 * Ask the session daemon for all available sessions.
616 * Return number of session.
617 * On error, return negative value.
619 int lttng_list_sessions(struct lttng_session
**sessions
)
622 struct lttcomm_session_msg lsm
;
624 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
625 ret
= ask_sessiond(&lsm
, (void**) sessions
);
630 return ret
/ sizeof(struct lttng_session
);
634 * List domain of a session.
636 int lttng_list_domains(struct lttng_handle
*handle
,
637 struct lttng_domain
**domains
)
640 struct lttcomm_session_msg lsm
;
646 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
648 copy_string(lsm
.session
.name
, handle
->session_name
,
649 sizeof(lsm
.session
.name
));
651 ret
= ask_sessiond(&lsm
, (void**) domains
);
656 return ret
/ sizeof(struct lttng_domain
);
660 * List channels of a session
662 int lttng_list_channels(struct lttng_handle
*handle
,
663 struct lttng_channel
**channels
)
666 struct lttcomm_session_msg lsm
;
672 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
673 copy_string(lsm
.session
.name
, handle
->session_name
,
674 sizeof(lsm
.session
.name
));
676 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
678 ret
= ask_sessiond(&lsm
, (void**) channels
);
683 return ret
/ sizeof(struct lttng_channel
);
687 * List events of a session channel.
689 int lttng_list_events(struct lttng_handle
*handle
,
690 const char *channel_name
, struct lttng_event
**events
)
693 struct lttcomm_session_msg lsm
;
699 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
700 copy_string(lsm
.session
.name
, handle
->session_name
,
701 sizeof(lsm
.session
.name
));
702 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
703 sizeof(lsm
.u
.list
.channel_name
));
705 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
707 ret
= ask_sessiond(&lsm
, (void**) events
);
712 return ret
/ sizeof(struct lttng_event
);
716 * lttng_set_tracing_group
718 * Set tracing group variable with name. This function
719 * allocate memory pointed by tracing_group.
721 int lttng_set_tracing_group(const char *name
)
723 if (asprintf(&tracing_group
, "%s", name
) < 0) {
733 int lttng_calibrate(struct lttng_handle
*handle
,
734 struct lttng_calibrate
*calibrate
)
736 struct lttcomm_session_msg lsm
;
742 lsm
.cmd_type
= LTTNG_CALIBRATE
;
743 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
745 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
747 return ask_sessiond(&lsm
, NULL
);
751 * lttng_check_session_daemon
755 * Error, return negative value
757 int lttng_session_daemon_alive(void)
761 ret
= set_session_daemon_path();
767 /* If socket exist, we check if the daemon listens to connect. */
768 ret
= access(sessiond_sock_path
, F_OK
);
774 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
779 ret
= lttcomm_close_unix_sock(ret
);
781 perror("lttcomm_close_unix_sock");
790 static void __attribute__((constructor
)) init()
792 /* Set default session group */
793 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);