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"
32 /* Socket to session daemon for communication */
33 static int sessiond_socket
;
34 static char sessiond_sock_path
[PATH_MAX
];
36 /* Communication structure to ltt-sessiond */
37 static struct lttcomm_session_msg lsm
;
38 static struct lttcomm_lttng_msg llm
;
41 static char *tracing_group
;
47 * Send lttcomm_session_msg to the session daemon.
49 * On success, return 0
50 * On error, return error code
52 static int send_data_sessiond(void)
61 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
70 * Receive data from the sessiond socket.
72 * On success, return 0
73 * On error, return recv() error code
75 static int recv_data_sessiond(void *buf
, size_t len
)
84 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
93 * Ask the session daemon a specific command and put the data into buf.
95 * Return size of data (only payload, not header).
97 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
103 ret
= lttng_connect_sessiond();
110 /* Send command to session daemon */
111 ret
= send_data_sessiond();
116 /* Get header from data transmission */
117 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
122 /* Check error code if OK */
123 if (llm
.ret_code
!= LTTCOMM_OK
) {
128 size
= llm
.trace_name_offset
+ llm
.data_size
;
133 data
= (void*) malloc(size
);
135 /* Get payload data */
136 ret
= recv_data_sessiond(data
, size
);
146 lttng_disconnect_sessiond();
151 * check_tracing_group
153 * Check if the specified group name exist.
156 static int check_tracing_group(const char *grp_name
)
158 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
160 int grp_list_size
, grp_id
, i
;
163 /* Get GID of group 'tracing' */
164 grp_tracing
= getgrnam(grp_name
);
165 if (grp_tracing
== NULL
) {
166 /* NULL means not found also. getgrnam(3) */
173 /* Get number of supplementary group IDs */
174 grp_list_size
= getgroups(0, NULL
);
175 if (grp_list_size
< 0) {
180 /* Alloc group list of the right size */
181 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
182 grp_id
= getgroups(grp_list_size
, grp_list
);
188 for (i
= 0; i
< grp_list_size
; i
++) {
189 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
203 * set_session_daemon_path
205 * Set sessiond socket path by putting it in
206 * the global sessiond_sock_path variable.
208 static int set_session_daemon_path(void)
212 /* Are we in the tracing group ? */
213 ret
= check_tracing_group(tracing_group
);
214 if (ret
< 0 && getuid() != 0) {
215 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
216 getenv("HOME")) < 0) {
220 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
221 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
228 * BEGIN KERNEL CONTROL
232 * lttng_kernel_enable_event
234 * Enable an event in the kernel tracer.
236 int lttng_kernel_enable_event(char *event_name
)
240 if (event_name
== NULL
) {
241 ret
= ask_sessiond(KERNEL_ENABLE_ALL_EVENT
, NULL
);
243 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
244 ret
= ask_sessiond(KERNEL_ENABLE_EVENT
, NULL
);
251 * lttng_kernel_disable_event
253 * Disable an event in the kernel tracer.
255 int lttng_kernel_disable_event(char *event_name
)
257 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
258 return ask_sessiond(KERNEL_DISABLE_EVENT
, NULL
);
262 * lttng_kernel_create_session
264 * Create a session in the kernel tracer.
266 int lttng_kernel_create_session(void)
268 return ask_sessiond(KERNEL_CREATE_SESSION
, NULL
);
272 * lttng_kernel_create_channel
274 * Create a channel in the kernel tracer.
276 int lttng_kernel_create_channel(void)
278 return ask_sessiond(KERNEL_CREATE_CHANNEL
, NULL
);
282 * lttng_kernel_open_metadata
284 * Open metadata in the kernel tracer.
286 int lttng_kernel_open_metadata(void)
288 return ask_sessiond(KERNEL_OPEN_METADATA
, NULL
);
292 * lttng_kernel_create_stream
294 * Create stream in the kernel tracer.
296 int lttng_kernel_create_stream(void)
298 return ask_sessiond(KERNEL_CREATE_STREAM
, NULL
);
302 * lttng_kernel_list_events
304 * List all available events in the kernel.
306 * Return the size (bytes) of the list and set the event_list array.
307 * On error, return negative value.
309 int lttng_kernel_list_events(char **event_list
)
311 return ask_sessiond(KERNEL_LIST_EVENTS
, (void **) event_list
);
315 * lttng_kernel_start_tracing
317 * Start kernel tracing.
319 int lttng_kernel_start_tracing(void)
321 return ask_sessiond(KERNEL_START_TRACE
, NULL
);
325 * lttng_kernel_stop_tracing
327 * Stop kernel tracing.
329 int lttng_kernel_stop_tracing(void)
331 return ask_sessiond(KERNEL_STOP_TRACE
, NULL
);
339 * lttng_get_readable_code
341 * Return a human readable string of code
343 const char *lttng_get_readable_code(int code
)
345 if (code
> -LTTCOMM_OK
) {
346 return "Ended with errors";
349 return lttcomm_get_readable_code(code
);
353 * lttng_ust_start_trace
355 * Request a trace start for pid.
357 int lttng_ust_start_trace(pid_t pid
)
360 return ask_sessiond(UST_START_TRACE
, NULL
);
364 * lttng_ust_stop_trace
366 * Request a trace stop for pid.
368 int lttng_ust_stop_trace(pid_t pid
)
371 return ask_sessiond(UST_STOP_TRACE
, NULL
);
375 * lttng_ust_create_trace
377 * Request a trace creation for pid.
379 int lttng_ust_create_trace(pid_t pid
)
382 return ask_sessiond(UST_CREATE_TRACE
, NULL
);
386 * lttng_ust_list_apps
388 * Ask the session daemon for all UST traceable applications.
390 * Return the number of pids.
391 * On error, return negative value.
393 int lttng_ust_list_apps(pid_t
**pids
)
397 ret
= ask_sessiond(UST_LIST_APPS
, (void**) pids
);
402 return ret
/ sizeof(pid_t
);
408 * Ask the session daemon for all traces (kernel and ust) for the session
409 * identified by uuid.
411 * Return the number of traces.
412 * On error, return negative value.
414 int lttng_list_traces(uuid_t
*uuid
, struct lttng_trace
**traces
)
418 uuid_copy(lsm
.session_uuid
, *uuid
);
420 ret
= ask_sessiond(LTTNG_LIST_TRACES
, (void **) traces
);
425 return ret
/ sizeof(struct lttng_trace
);
429 * lttng_create_session
431 * Create a brand new session using name.
433 int lttng_create_session(char *name
)
435 strncpy(lsm
.session_name
, name
, NAME_MAX
);
436 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
440 * lttng_destroy_session
442 * Destroy session using name.
444 int lttng_destroy_session(uuid_t
*uuid
)
446 uuid_copy(lsm
.session_uuid
, *uuid
);
447 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
451 * lttng_list_sessions
453 * Ask the session daemon for all available sessions.
455 * Return number of session.
456 * On error, return negative value.
458 int lttng_list_sessions(struct lttng_session
**sessions
)
462 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
467 return ret
/ sizeof(struct lttng_session
);
471 * lttng_connect_sessiond
473 * Connect to the LTTng session daemon.
474 * On success, return 0
475 * On error, return a negative value
477 int lttng_connect_sessiond(void)
481 ret
= set_session_daemon_path();
486 /* Connect to the sesssion daemon */
487 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
492 sessiond_socket
= ret
;
499 * lttng_disconnect_sessiond
501 * Clean disconnect the session daemon.
503 int lttng_disconnect_sessiond(void)
508 ret
= lttcomm_close_unix_sock(sessiond_socket
);
517 * lttng_set_current_session_uuid
519 * Set the session uuid for current lsm.
521 void lttng_set_current_session_uuid(uuid_t
*uuid
)
523 uuid_copy(lsm
.session_uuid
, *uuid
);
527 * lttng_set_tracing_group
529 * Set tracing group variable with name. This function
530 * allocate memory pointed by tracing_group.
532 int lttng_set_tracing_group(const char *name
)
534 if (asprintf(&tracing_group
, "%s", name
) < 0) {
542 * lttng_check_session_daemon
546 * Error, return negative value
548 int lttng_session_daemon_alive(void)
552 ret
= set_session_daemon_path();
558 /* If socket exist, we consider the daemon started */
559 ret
= access(sessiond_sock_path
, F_OK
);
572 static void __attribute__((constructor
)) init()
574 /* Set default session group */
575 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);