2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <lttng/lttng.h>
29 #include "liblttsessiondcomm.h"
31 #include "lttng-share.h"
33 /* Socket to session daemon for communication */
34 static int sessiond_socket
;
35 static char sessiond_sock_path
[PATH_MAX
];
37 /* Communication structure to ltt-sessiond */
38 static struct lttcomm_session_msg lsm
;
39 static struct lttcomm_lttng_msg llm
;
42 static char *tracing_group
;
48 * Send lttcomm_session_msg to the session daemon.
50 * On success, return 0
51 * On error, return error code
53 static int send_data_sessiond(void)
62 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
71 * Receive data from the sessiond socket.
73 * On success, return 0
74 * On error, return recv() error code
76 static int recv_data_sessiond(void *buf
, size_t len
)
85 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
92 * Check if the specified group name exist.
94 * If yes return 0, else return -1.
96 static int check_tracing_group(const char *grp_name
)
98 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
100 int grp_list_size
, grp_id
, i
;
103 /* Get GID of group 'tracing' */
104 grp_tracing
= getgrnam(grp_name
);
105 if (grp_tracing
== NULL
) {
106 /* NULL means not found also. getgrnam(3) */
113 /* Get number of supplementary group IDs */
114 grp_list_size
= getgroups(0, NULL
);
115 if (grp_list_size
< 0) {
120 /* Alloc group list of the right size */
121 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
122 grp_id
= getgroups(grp_list_size
, grp_list
);
128 for (i
= 0; i
< grp_list_size
; i
++) {
129 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
143 * Set sessiond socket path by putting it in the global sessiond_sock_path
146 static int set_session_daemon_path(void)
150 /* Are we in the tracing group ? */
151 ret
= check_tracing_group(tracing_group
);
152 if (ret
< 0 && getuid() != 0) {
153 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
154 getenv("HOME")) < 0) {
158 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
159 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
166 * Connect to the LTTng session daemon.
168 * On success, return 0. On error, return -1.
170 static int connect_sessiond(void)
174 ret
= set_session_daemon_path();
179 /* Connect to the sesssion daemon */
180 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
185 sessiond_socket
= ret
;
192 * Clean disconnect the session daemon.
194 static int disconnect_sessiond(void)
199 ret
= lttcomm_close_unix_sock(sessiond_socket
);
210 * Ask the session daemon a specific command and put the data into buf.
212 * Return size of data (only payload, not header).
214 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
220 ret
= connect_sessiond();
227 /* Send command to session daemon */
228 ret
= send_data_sessiond();
233 /* Get header from data transmission */
234 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
239 /* Check error code if OK */
240 if (llm
.ret_code
!= LTTCOMM_OK
) {
245 size
= llm
.data_size
;
250 data
= (void*) malloc(size
);
252 /* Get payload data */
253 ret
= recv_data_sessiond(data
, size
);
263 disconnect_sessiond();
268 * lttng_start_tracing
270 * Start tracing for all trace of the session.
272 int lttng_start_tracing(char *session_name
)
274 strncpy(lsm
.session_name
, session_name
, NAME_MAX
);
275 return ask_sessiond(LTTNG_START_TRACE
, NULL
);
281 * Stop tracing for all trace of the session.
283 int lttng_stop_tracing(char *session_name
)
285 strncpy(lsm
.session_name
, session_name
, NAME_MAX
);
286 return ask_sessiond(LTTNG_STOP_TRACE
, NULL
);
290 * BEGIN Kernel control API
294 * lttng_kernel_add_context
296 int lttng_kernel_add_context(struct lttng_kernel_context
*ctx
,
297 char *event_name
, char *channel_name
)
299 if (channel_name
!= NULL
) {
300 strncpy(lsm
.u
.context
.channel_name
, channel_name
, NAME_MAX
);
303 if (event_name
!= NULL
) {
304 strncpy(lsm
.u
.context
.event_name
, event_name
, NAME_MAX
);
307 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_kernel_context
));
308 return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT
, NULL
);
312 * lttng_kernel_enable_event
314 int lttng_kernel_enable_event(struct lttng_event
*ev
, char *channel_name
)
318 if (channel_name
== NULL
) {
319 strncpy(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
321 strncpy(lsm
.u
.enable
.channel_name
, channel_name
, NAME_MAX
);
325 ret
= ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT
, NULL
);
327 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(struct lttng_event
));
328 ret
= ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT
, NULL
);
335 * lttng_kernel_disable_event
337 * Disable an event in the kernel tracer.
339 int lttng_kernel_disable_event(char *name
, char *channel_name
)
343 if (channel_name
== NULL
) {
344 strncpy(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
346 strncpy(lsm
.u
.disable
.channel_name
, channel_name
, NAME_MAX
);
350 ret
= ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT
, NULL
);
352 strncpy(lsm
.u
.disable
.name
, name
, NAME_MAX
);
353 ret
= ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT
, NULL
);
360 * lttng_kernel_enable_channel
362 * Enable recording for a channel for the kernel tracer.
364 int lttng_kernel_enable_channel(char *name
)
366 strncpy(lsm
.u
.enable
.channel_name
, name
, NAME_MAX
);
367 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL
, NULL
);
371 * lttng_kernel_disable_channel
373 * Disable recording for the channel for the kernel tracer.
375 int lttng_kernel_disable_channel(char *name
)
377 strncpy(lsm
.u
.disable
.channel_name
, name
, NAME_MAX
);
378 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL
, NULL
);
382 * lttng_kernel_create_channel
384 * Create a channel in the kernel tracer.
386 int lttng_kernel_create_channel(struct lttng_channel
*chan
)
388 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(struct lttng_channel
));
389 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL
, NULL
);
395 * List all available events in the kernel.
397 * Return the size (bytes) of the list and set the event_list array.
398 * On error, return negative value.
400 int lttng_kernel_list_events(char **event_list
)
402 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS
, (void **) event_list
);
406 * END Kernel control API
410 * lttng_get_readable_code
412 * Return a human readable string of code
414 const char *lttng_get_readable_code(int code
)
416 if (code
> -LTTCOMM_OK
) {
417 return "Ended with errors";
420 return lttcomm_get_readable_code(code
);
424 * lttng_ust_list_apps
426 * Ask the session daemon for all UST traceable applications.
428 * Return the number of pids.
429 * On error, return negative value.
431 int lttng_ust_list_traceable_apps(pid_t
**pids
)
435 ret
= ask_sessiond(LTTNG_LIST_TRACEABLE_APPS
, (void**) pids
);
440 return ret
/ sizeof(pid_t
);
446 * Ask the session daemon for all traces (kernel and ust) for the session
447 * identified by name.
449 * Return the number of traces.
450 * On error, return negative value.
453 int lttng_list_traces(char *session_name, struct lttng_trace **traces)
457 strncpy(lsm.session_name, session_name, NAME_MAX);
459 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
464 return ret / sizeof(struct lttng_trace);
469 * lttng_create_session
471 * Create a brand new session using name.
473 int lttng_create_session(char *name
, char *path
)
475 strncpy(lsm
.session_name
, name
, NAME_MAX
);
476 strncpy(lsm
.path
, path
, PATH_MAX
);
477 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
481 * lttng_destroy_session
483 * Destroy session using name.
485 int lttng_destroy_session(char *name
)
487 strncpy(lsm
.session_name
, name
, NAME_MAX
);
488 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
492 * lttng_list_sessions
494 * Ask the session daemon for all available sessions.
496 * Return number of session.
497 * On error, return negative value.
499 int lttng_list_sessions(struct lttng_session
**sessions
)
503 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
508 return ret
/ sizeof(struct lttng_session
);
511 void lttng_set_session_name(char *name
)
513 strncpy(lsm
.session_name
, name
, NAME_MAX
);
517 * lttng_set_tracing_group
519 * Set tracing group variable with name. This function
520 * allocate memory pointed by tracing_group.
522 int lttng_set_tracing_group(const char *name
)
524 if (asprintf(&tracing_group
, "%s", name
) < 0) {
532 * lttng_check_session_daemon
536 * Error, return negative value
538 int lttng_session_daemon_alive(void)
542 ret
= set_session_daemon_path();
548 /* If socket exist, we consider the daemon started */
549 ret
= access(sessiond_sock_path
, F_OK
);
562 static void __attribute__((constructor
)) init()
564 /* Set default session group */
565 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);