Update lttng-ust-abi.h for event field listing
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
82a3637f
DG
2 * liblttngctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
826d496d 6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d 7 *
d14d33bf
AM
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
82a3637f
DG
11 *
12 * This library is distributed in the hope that it will be useful,
fac6795d 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
d14d33bf
AM
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
20 */
21
22#define _GNU_SOURCE
fac6795d 23#include <grp.h>
1e307fab 24#include <errno.h>
fac6795d
DG
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
990570ed
DG
30#include <common/common.h>
31#include <common/defaults.h>
db758600 32#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 33#include <lttng/lttng.h>
fac6795d
DG
34
35/* Socket to session daemon for communication */
36static int sessiond_socket;
37static char sessiond_sock_path[PATH_MAX];
38
fac6795d
DG
39/* Variables */
40static char *tracing_group;
41static int connected;
42
97e19046
DG
43/* Global */
44
45/*
46 * Those two variables are used by error.h to silent or control the verbosity of
47 * error message. They are global to the library so application linking with it
48 * are able to compile correctly and also control verbosity of the library.
49 *
50 * Note that it is *not* possible to silent ERR() and PERROR() macros.
51 */
52int lttng_opt_quiet;
53int lttng_opt_verbose;
54
99497cd0
MD
55/*
56 * Copy string from src to dst and enforce null terminated byte.
57 */
58static void copy_string(char *dst, const char *src, size_t len)
59{
e7d6716d 60 if (src && dst) {
99497cd0
MD
61 strncpy(dst, src, len);
62 /* Enforce the NULL terminated byte */
63 dst[len - 1] = '\0';
cd80958d
DG
64 } else if (dst) {
65 dst[0] = '\0';
99497cd0
MD
66 }
67}
68
fac6795d 69/*
cd80958d 70 * Copy domain to lttcomm_session_msg domain.
fac6795d 71 *
cd80958d
DG
72 * If domain is unknown, default domain will be the kernel.
73 */
74static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
75{
76 if (src && dst) {
77 switch (src->type) {
78 case LTTNG_DOMAIN_KERNEL:
79 case LTTNG_DOMAIN_UST:
d78d6610 80 /*
cd80958d
DG
81 case LTTNG_DOMAIN_UST_EXEC_NAME:
82 case LTTNG_DOMAIN_UST_PID:
83 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 84 */
cd80958d
DG
85 memcpy(dst, src, sizeof(struct lttng_domain));
86 break;
87 default:
441c16a7 88 memset(dst, 0, sizeof(struct lttng_domain));
cd80958d
DG
89 dst->type = LTTNG_DOMAIN_KERNEL;
90 break;
91 }
92 }
93}
94
95/*
96 * Send lttcomm_session_msg to the session daemon.
fac6795d 97 *
1c8d13c8
TD
98 * On success, returns the number of bytes sent (>=0)
99 * On error, returns -1
fac6795d 100 */
cd80958d 101static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
102{
103 int ret;
104
105 if (!connected) {
e065084a
DG
106 ret = -ENOTCONN;
107 goto end;
fac6795d
DG
108 }
109
be040666 110 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 111 sizeof(struct lttcomm_session_msg));
e065084a
DG
112
113end:
114 return ret;
115}
116
117/*
cd80958d 118 * Receive data from the sessiond socket.
e065084a 119 *
1c8d13c8
TD
120 * On success, returns the number of bytes received (>=0)
121 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 122 */
ca95a216 123static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
124{
125 int ret;
126
127 if (!connected) {
128 ret = -ENOTCONN;
129 goto end;
fac6795d
DG
130 }
131
ca95a216 132 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 133
e065084a 134end:
fac6795d
DG
135 return ret;
136}
137
138/*
1c8d13c8 139 * Check if we are in the specified group.
65beb5ff 140 *
2269e89e 141 * If yes return 1, else return -1.
947308c4
DG
142 */
143static int check_tracing_group(const char *grp_name)
144{
145 struct group *grp_tracing; /* no free(). See getgrnam(3) */
146 gid_t *grp_list;
147 int grp_list_size, grp_id, i;
148 int ret = -1;
149
150 /* Get GID of group 'tracing' */
151 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
152 if (!grp_tracing) {
153 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
154 goto end;
155 }
156
157 /* Get number of supplementary group IDs */
158 grp_list_size = getgroups(0, NULL);
159 if (grp_list_size < 0) {
160 perror("getgroups");
161 goto end;
162 }
163
164 /* Alloc group list of the right size */
165 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 166 if (!grp_list) {
1c8d13c8 167 perror("malloc");
00795392
MD
168 goto end;
169 }
947308c4 170 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 171 if (grp_id < 0) {
947308c4
DG
172 perror("getgroups");
173 goto free_list;
174 }
175
176 for (i = 0; i < grp_list_size; i++) {
177 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 178 ret = 1;
947308c4
DG
179 break;
180 }
181 }
182
183free_list:
184 free(grp_list);
185
186end:
187 return ret;
188}
189
190/*
2269e89e
DG
191 * Try connect to session daemon with sock_path.
192 *
193 * Return 0 on success, else -1
194 */
195static int try_connect_sessiond(const char *sock_path)
196{
197 int ret;
198
199 /* If socket exist, we check if the daemon listens for connect. */
200 ret = access(sock_path, F_OK);
201 if (ret < 0) {
202 /* Not alive */
203 return -1;
204 }
205
206 ret = lttcomm_connect_unix_sock(sock_path);
207 if (ret < 0) {
208 /* Not alive */
209 return -1;
210 }
211
212 ret = lttcomm_close_unix_sock(ret);
213 if (ret < 0) {
214 perror("lttcomm_close_unix_sock");
215 }
216
217 return 0;
218}
219
220/*
1c8d13c8
TD
221 * Set sessiond socket path by putting it in the global
222 * sessiond_sock_path variable.
223 * Returns 0 on success,
224 * -ENOMEM on failure (the sessiond socket path is somehow too long)
947308c4
DG
225 */
226static int set_session_daemon_path(void)
227{
228 int ret;
2269e89e
DG
229 int in_tgroup = 0; /* In tracing group */
230 uid_t uid;
231
232 uid = getuid();
947308c4 233
2269e89e
DG
234 if (uid != 0) {
235 /* Are we in the tracing group ? */
236 in_tgroup = check_tracing_group(tracing_group);
237 }
238
08a9c49f
TD
239 if ((uid == 0) || in_tgroup) {
240 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 241 sizeof(sessiond_sock_path));
08a9c49f 242 }
2269e89e 243
08a9c49f
TD
244 if (uid != 0) {
245 if (in_tgroup) {
246 /* Tracing group */
247 ret = try_connect_sessiond(sessiond_sock_path);
248 if (ret >= 0) {
249 goto end;
2269e89e 250 }
08a9c49f 251 /* Global session daemon not available... */
2269e89e 252 }
08a9c49f
TD
253 /* ...or not in tracing group (and not root), default */
254
255 /*
256 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
257 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
258 */
259 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
260 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
261 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
947308c4
DG
262 return -ENOMEM;
263 }
947308c4 264 }
08a9c49f 265end:
947308c4
DG
266 return 0;
267}
268
65beb5ff
DG
269/*
270 * Connect to the LTTng session daemon.
271 *
272 * On success, return 0. On error, return -1.
273 */
274static int connect_sessiond(void)
275{
276 int ret;
277
278 ret = set_session_daemon_path();
279 if (ret < 0) {
1c8d13c8 280 return -1; /* set_session_daemon_path() returns -ENOMEM */
65beb5ff
DG
281 }
282
283 /* Connect to the sesssion daemon */
284 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
285 if (ret < 0) {
286 return ret;
287 }
288
289 sessiond_socket = ret;
290 connected = 1;
291
292 return 0;
293}
294
295/*
1c8d13c8
TD
296 * Clean disconnect from the session daemon.
297 * On success, return 0. On error, return -1.
65beb5ff
DG
298 */
299static int disconnect_sessiond(void)
300{
301 int ret = 0;
302
303 if (connected) {
304 ret = lttcomm_close_unix_sock(sessiond_socket);
305 sessiond_socket = 0;
306 connected = 0;
307 }
308
309 return ret;
310}
311
35a6fdb7 312/*
cd80958d 313 * Ask the session daemon a specific command and put the data into buf.
65beb5ff 314 *
af87c45a 315 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 316 */
cd80958d 317static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
65beb5ff
DG
318{
319 int ret;
320 size_t size;
321 void *data = NULL;
cd80958d 322 struct lttcomm_lttng_msg llm;
65beb5ff
DG
323
324 ret = connect_sessiond();
325 if (ret < 0) {
326 goto end;
327 }
328
65beb5ff 329 /* Send command to session daemon */
cd80958d 330 ret = send_session_msg(lsm);
65beb5ff
DG
331 if (ret < 0) {
332 goto end;
333 }
334
335 /* Get header from data transmission */
336 ret = recv_data_sessiond(&llm, sizeof(llm));
337 if (ret < 0) {
338 goto end;
339 }
340
341 /* Check error code if OK */
342 if (llm.ret_code != LTTCOMM_OK) {
343 ret = -llm.ret_code;
344 goto end;
345 }
346
347 size = llm.data_size;
348 if (size == 0) {
874d3f84 349 /* If client free with size 0 */
a45d5536
DG
350 if (buf != NULL) {
351 *buf = NULL;
352 }
7d29a247 353 ret = 0;
65beb5ff
DG
354 goto end;
355 }
356
357 data = (void*) malloc(size);
358
359 /* Get payload data */
360 ret = recv_data_sessiond(data, size);
361 if (ret < 0) {
362 free(data);
363 goto end;
364 }
365
83009e5e
DG
366 /*
367 * Extra protection not to dereference a NULL pointer. If buf is NULL at
368 * this point, an error is returned and data is freed.
369 */
370 if (buf == NULL) {
371 ret = -1;
372 free(data);
373 goto end;
374 }
375
65beb5ff
DG
376 *buf = data;
377 ret = size;
378
379end:
380 disconnect_sessiond();
381 return ret;
382}
383
9f19cc17 384/*
cd80958d 385 * Create lttng handle and return pointer.
1c8d13c8 386 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 387 */
cd80958d
DG
388struct lttng_handle *lttng_create_handle(const char *session_name,
389 struct lttng_domain *domain)
9f19cc17 390{
cd80958d
DG
391 struct lttng_handle *handle;
392
393 handle = malloc(sizeof(struct lttng_handle));
394 if (handle == NULL) {
395 perror("malloc handle");
396 goto end;
397 }
398
399 /* Copy session name */
400 copy_string(handle->session_name, session_name,
401 sizeof(handle->session_name));
402
403 /* Copy lttng domain */
404 copy_lttng_domain(&handle->domain, domain);
405
406end:
407 return handle;
408}
409
410/*
411 * Destroy handle by free(3) the pointer.
412 */
413void lttng_destroy_handle(struct lttng_handle *handle)
414{
415 if (handle) {
416 free(handle);
eb354453
DG
417 }
418}
419
d9800920
DG
420/*
421 * Register an outside consumer.
1c8d13c8 422 * Returns size of returned session payload data or a negative error code.
d9800920
DG
423 */
424int lttng_register_consumer(struct lttng_handle *handle,
425 const char *socket_path)
426{
427 struct lttcomm_session_msg lsm;
428
429 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
430 copy_string(lsm.session.name, handle->session_name,
431 sizeof(lsm.session.name));
432 copy_lttng_domain(&lsm.domain, &handle->domain);
433
434 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
435
436 return ask_sessiond(&lsm, NULL);
437}
438
1df4dedd 439/*
1c8d13c8
TD
440 * Start tracing for all traces of the session.
441 * Returns size of returned session payload data or a negative error code.
1df4dedd 442 */
6a4f824d 443int lttng_start_tracing(const char *session_name)
f3ed775e 444{
cd80958d
DG
445 struct lttcomm_session_msg lsm;
446
6a4f824d 447 if (session_name == NULL) {
cd80958d
DG
448 return -1;
449 }
450
451 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
452
453 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
454
455 return ask_sessiond(&lsm, NULL);
f3ed775e 456}
1df4dedd
DG
457
458/*
1c8d13c8
TD
459 * Stop tracing for all traces of the session.
460 * Returns size of returned session payload data or a negative error code.
f3ed775e 461 */
6a4f824d 462int lttng_stop_tracing(const char *session_name)
f3ed775e 463{
cd80958d
DG
464 struct lttcomm_session_msg lsm;
465
6a4f824d
DG
466 if (session_name == NULL) {
467 return -1;
468 }
469
cd80958d 470 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
471
472 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
473
474 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
475}
476
477/*
1e46a50f
TD
478 * Add context to event and/or channel.
479 * If event_name is NULL, the context is applied to all events of the channel.
480 * If channel_name is NULL, a lookup of the event's channel is done.
481 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
482 *
483 * Returns the size of the returned payload data or a negative error code.
1df4dedd 484 */
cd80958d 485int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
486 struct lttng_event_context *ctx, const char *event_name,
487 const char *channel_name)
d65106b1 488{
cd80958d
DG
489 struct lttcomm_session_msg lsm;
490
9d697d3d
DG
491 /* Safety check. Both are mandatory */
492 if (handle == NULL || ctx == NULL) {
cd80958d
DG
493 return -1;
494 }
495
441c16a7
MD
496 memset(&lsm, 0, sizeof(lsm));
497
cd80958d
DG
498 lsm.cmd_type = LTTNG_ADD_CONTEXT;
499
500 /* Copy channel name */
501 copy_string(lsm.u.context.channel_name, channel_name,
502 sizeof(lsm.u.context.channel_name));
503 /* Copy event name */
504 copy_string(lsm.u.context.event_name, event_name,
505 sizeof(lsm.u.context.event_name));
506
507 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 508
9d697d3d 509 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 510
cd80958d
DG
511 copy_string(lsm.session.name, handle->session_name,
512 sizeof(lsm.session.name));
513
514 return ask_sessiond(&lsm, NULL);
d65106b1
DG
515}
516
f3ed775e 517/*
1c8d13c8
TD
518 * Enable event(s) for a channel.
519 * If no event name is specified, all events are enabled.
520 * If no channel name is specified, the default 'channel0' is used.
521 * Returns size of returned session payload data or a negative error code.
f3ed775e 522 */
cd80958d 523int lttng_enable_event(struct lttng_handle *handle,
38057ed1 524 struct lttng_event *ev, const char *channel_name)
1df4dedd 525{
cd80958d
DG
526 struct lttcomm_session_msg lsm;
527
5117eeec 528 if (handle == NULL || ev == NULL) {
cd80958d
DG
529 return -1;
530 }
33a2b854 531
441c16a7
MD
532 memset(&lsm, 0, sizeof(lsm));
533
5117eeec 534 /* If no channel name, we put the default name */
94cf3c47 535 if (channel_name == NULL) {
cd80958d
DG
536 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
537 sizeof(lsm.u.enable.channel_name));
33a2b854 538 } else {
cd80958d
DG
539 copy_string(lsm.u.enable.channel_name, channel_name,
540 sizeof(lsm.u.enable.channel_name));
eb354453
DG
541 }
542
cd80958d 543 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 544
8c9ae521 545 if (ev->name[0] != '\0') {
cd80958d 546 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 547 } else {
cd80958d 548 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 549 }
8c9ae521 550 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 551
cd80958d
DG
552 copy_string(lsm.session.name, handle->session_name,
553 sizeof(lsm.session.name));
554
555 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
556}
557
558/*
1c8d13c8
TD
559 * Disable event(s) of a channel and domain.
560 * If no event name is specified, all events are disabled.
561 * If no channel name is specified, the default 'channel0' is used.
562 * Returns size of returned session payload data or a negative error code.
1df4dedd 563 */
cd80958d 564int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 565 const char *channel_name)
1df4dedd 566{
cd80958d 567 struct lttcomm_session_msg lsm;
1df4dedd 568
9d697d3d 569 if (handle == NULL) {
cd80958d
DG
570 return -1;
571 }
572
441c16a7
MD
573 memset(&lsm, 0, sizeof(lsm));
574
cd80958d
DG
575 if (channel_name) {
576 copy_string(lsm.u.disable.channel_name, channel_name,
577 sizeof(lsm.u.disable.channel_name));
f3ed775e 578 } else {
cd80958d
DG
579 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
580 sizeof(lsm.u.disable.channel_name));
eb354453
DG
581 }
582
cd80958d 583 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 584
f84efadf 585 if (name != NULL) {
cd80958d
DG
586 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
587 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 588 } else {
cd80958d 589 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
590 }
591
cd80958d
DG
592 copy_string(lsm.session.name, handle->session_name,
593 sizeof(lsm.session.name));
594
595 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
596}
597
598/*
1c8d13c8
TD
599 * Enable channel per domain
600 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 601 */
cd80958d 602int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 603 struct lttng_channel *chan)
a5c5a2bd 604{
cd80958d
DG
605 struct lttcomm_session_msg lsm;
606
5117eeec
DG
607 /*
608 * NULL arguments are forbidden. No default values.
609 */
610 if (handle == NULL || chan == NULL) {
cd80958d
DG
611 return -1;
612 }
613
441c16a7
MD
614 memset(&lsm, 0, sizeof(lsm));
615
5117eeec 616 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 617
cd80958d
DG
618 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
619
620 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 621
cd80958d
DG
622 copy_string(lsm.session.name, handle->session_name,
623 sizeof(lsm.session.name));
624
625 return ask_sessiond(&lsm, NULL);
8c0faa1d 626}
1df4dedd 627
2ef84c95 628/*
1c8d13c8
TD
629 * All tracing will be stopped for registered events of the channel.
630 * Returns size of returned session payload data or a negative error code.
2ef84c95 631 */
cd80958d 632int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 633{
cd80958d
DG
634 struct lttcomm_session_msg lsm;
635
9d697d3d
DG
636 /* Safety check. Both are mandatory */
637 if (handle == NULL || name == NULL) {
cd80958d
DG
638 return -1;
639 }
640
441c16a7
MD
641 memset(&lsm, 0, sizeof(lsm));
642
cd80958d 643 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 644
9d697d3d
DG
645 copy_string(lsm.u.disable.channel_name, name,
646 sizeof(lsm.u.disable.channel_name));
647
cd80958d
DG
648 copy_lttng_domain(&lsm.domain, &handle->domain);
649
650 copy_string(lsm.session.name, handle->session_name,
651 sizeof(lsm.session.name));
652
653 return ask_sessiond(&lsm, NULL);
ca95a216
DG
654}
655
fac6795d 656/*
1c8d13c8
TD
657 * Lists all available tracepoints of domain.
658 * Sets the contents of the events array.
659 * Returns the number of lttng_event entries in events;
660 * on error, returns a negative value.
fac6795d 661 */
cd80958d 662int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 663 struct lttng_event **events)
fac6795d 664{
052da939 665 int ret;
cd80958d
DG
666 struct lttcomm_session_msg lsm;
667
9d697d3d 668 if (handle == NULL) {
cd80958d
DG
669 return -1;
670 }
fac6795d 671
cd80958d
DG
672 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
673 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 674
cd80958d 675 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
676 if (ret < 0) {
677 return ret;
eb354453 678 }
fac6795d 679
9f19cc17 680 return ret / sizeof(struct lttng_event);
fac6795d
DG
681}
682
1657e9bb 683/*
1c8d13c8
TD
684 * Returns a human readable string describing
685 * the error code (a negative value).
1657e9bb 686 */
9a745bc7 687const char *lttng_strerror(int code)
1657e9bb 688{
1c8d13c8 689 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
7d29a247
DG
690 if (code > -LTTCOMM_OK) {
691 return "Ended with errors";
1657e9bb
DG
692 }
693
7d29a247 694 return lttcomm_get_readable_code(code);
1657e9bb
DG
695}
696
aaf97519 697/*
1c8d13c8
TD
698 * Create a brand new session using name and path.
699 * Returns size of returned session payload data or a negative error code.
aaf97519 700 */
38057ed1 701int lttng_create_session(const char *name, const char *path)
aaf97519 702{
cd80958d
DG
703 struct lttcomm_session_msg lsm;
704
705 lsm.cmd_type = LTTNG_CREATE_SESSION;
706 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
707 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
708
709 return ask_sessiond(&lsm, NULL);
8028d920
DG
710}
711
712/*
8028d920 713 * Destroy session using name.
1c8d13c8 714 * Returns size of returned session payload data or a negative error code.
8028d920 715 */
843f5df9 716int lttng_destroy_session(const char *session_name)
8028d920 717{
cd80958d
DG
718 struct lttcomm_session_msg lsm;
719
843f5df9 720 if (session_name == NULL) {
cd80958d
DG
721 return -1;
722 }
723
724 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
725
726 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
727
728 return ask_sessiond(&lsm, NULL);
aaf97519
DG
729}
730
57167058 731/*
57167058 732 * Ask the session daemon for all available sessions.
1c8d13c8
TD
733 * Sets the contents of the sessions array.
734 * Returns the number of lttng_session entries in sessions;
735 * on error, returns a negative value.
57167058 736 */
ca95a216 737int lttng_list_sessions(struct lttng_session **sessions)
57167058 738{
ca95a216 739 int ret;
cd80958d 740 struct lttcomm_session_msg lsm;
57167058 741
cd80958d
DG
742 lsm.cmd_type = LTTNG_LIST_SESSIONS;
743 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 744 if (ret < 0) {
ca95a216 745 return ret;
57167058
DG
746 }
747
ca95a216 748 return ret / sizeof(struct lttng_session);
57167058
DG
749}
750
9f19cc17 751/*
1c8d13c8
TD
752 * Ask the session daemon for all available domains of a session.
753 * Sets the contents of the domains array.
754 * Returns the number of lttng_domain entries in domains;
755 * on error, returns a negative value.
9f19cc17 756 */
330be774 757int lttng_list_domains(const char *session_name,
cd80958d 758 struct lttng_domain **domains)
9f19cc17
DG
759{
760 int ret;
cd80958d
DG
761 struct lttcomm_session_msg lsm;
762
330be774 763 if (session_name == NULL) {
cd80958d
DG
764 return -1;
765 }
9f19cc17 766
cd80958d
DG
767 lsm.cmd_type = LTTNG_LIST_DOMAINS;
768
330be774 769 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
770
771 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
772 if (ret < 0) {
773 return ret;
774 }
775
776 return ret / sizeof(struct lttng_domain);
777}
778
779/*
1c8d13c8
TD
780 * Ask the session daemon for all available channels of a session.
781 * Sets the contents of the channels array.
782 * Returns the number of lttng_channel entries in channels;
783 * on error, returns a negative value.
9f19cc17 784 */
cd80958d
DG
785int lttng_list_channels(struct lttng_handle *handle,
786 struct lttng_channel **channels)
9f19cc17
DG
787{
788 int ret;
cd80958d
DG
789 struct lttcomm_session_msg lsm;
790
9d697d3d 791 if (handle == NULL) {
cd80958d
DG
792 return -1;
793 }
794
795 lsm.cmd_type = LTTNG_LIST_CHANNELS;
796 copy_string(lsm.session.name, handle->session_name,
797 sizeof(lsm.session.name));
9f19cc17 798
cd80958d 799 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 800
cd80958d 801 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
802 if (ret < 0) {
803 return ret;
804 }
805
806 return ret / sizeof(struct lttng_channel);
807}
808
809/*
1c8d13c8
TD
810 * Ask the session daemon for all available events of a session channel.
811 * Sets the contents of the events array.
812 * Returns the number of lttng_event entries in events;
813 * on error, returns a negative value.
9f19cc17 814 */
cd80958d
DG
815int lttng_list_events(struct lttng_handle *handle,
816 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
817{
818 int ret;
cd80958d 819 struct lttcomm_session_msg lsm;
9f19cc17 820
9d697d3d
DG
821 /* Safety check. An handle and channel name are mandatory */
822 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
823 return -1;
824 }
825
826 lsm.cmd_type = LTTNG_LIST_EVENTS;
827 copy_string(lsm.session.name, handle->session_name,
828 sizeof(lsm.session.name));
829 copy_string(lsm.u.list.channel_name, channel_name,
830 sizeof(lsm.u.list.channel_name));
831
832 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 833
cd80958d 834 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
835 if (ret < 0) {
836 return ret;
837 }
838
839 return ret / sizeof(struct lttng_event);
840}
841
fac6795d 842/*
1c8d13c8
TD
843 * Sets the tracing_group variable with name.
844 * This function allocates memory pointed to by tracing_group.
845 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
846 */
847int lttng_set_tracing_group(const char *name)
848{
9d697d3d
DG
849 if (name == NULL) {
850 return -1;
851 }
852
fac6795d
DG
853 if (asprintf(&tracing_group, "%s", name) < 0) {
854 return -ENOMEM;
855 }
856
857 return 0;
858}
859
d0254c7c 860/*
af87c45a 861 * Returns size of returned session payload data or a negative error code.
d0254c7c 862 */
cd80958d 863int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
864 struct lttng_calibrate *calibrate)
865{
cd80958d 866 struct lttcomm_session_msg lsm;
d0254c7c 867
9d697d3d
DG
868 /* Safety check. NULL pointer are forbidden */
869 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
870 return -1;
871 }
d0254c7c 872
cd80958d
DG
873 lsm.cmd_type = LTTNG_CALIBRATE;
874 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 875
cd80958d
DG
876 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
877
878 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
879}
880
5edd7e09
DG
881/*
882 * Set default channel attributes.
441c16a7 883 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
884 */
885void lttng_channel_set_default_attr(struct lttng_domain *domain,
886 struct lttng_channel_attr *attr)
887{
441c16a7
MD
888 memset(attr, 0, sizeof(struct lttng_channel_attr));
889
5edd7e09
DG
890 /* Safety check */
891 if (attr == NULL || domain == NULL) {
892 return;
893 }
894
895 switch (domain->type) {
896 case LTTNG_DOMAIN_KERNEL:
897 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
898 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
899 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
900
901 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
902 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
903 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
904 break;
905 case LTTNG_DOMAIN_UST:
d78d6610 906#if 0
5edd7e09
DG
907 case LTTNG_DOMAIN_UST_EXEC_NAME:
908 case LTTNG_DOMAIN_UST_PID:
909 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 910#endif
5edd7e09
DG
911 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
912 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
913 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
914
915 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
916 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
917 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
918 break;
919 default:
441c16a7 920 /* Default behavior: leave set to 0. */
5edd7e09
DG
921 break;
922 }
923}
924
fac6795d 925/*
2269e89e 926 * Check if session daemon is alive.
fac6795d 927 *
2269e89e 928 * Return 1 if alive or 0 if not.
1c8d13c8 929 * On error returns a negative value.
fac6795d 930 */
947308c4 931int lttng_session_daemon_alive(void)
fac6795d
DG
932{
933 int ret;
934
935 ret = set_session_daemon_path();
936 if (ret < 0) {
947308c4 937 /* Error */
fac6795d
DG
938 return ret;
939 }
940
2269e89e
DG
941 if (strlen(sessiond_sock_path) == 0) {
942 /* No socket path set. Weird error */
943 return -1;
fac6795d
DG
944 }
945
2269e89e 946 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
947 if (ret < 0) {
948 /* Not alive */
949 return 0;
950 }
7d8234d9 951
947308c4
DG
952 /* Is alive */
953 return 1;
fac6795d
DG
954}
955
956/*
957 * lib constructor
958 */
959static void __attribute__((constructor)) init()
960{
961 /* Set default session group */
bbccc3d2 962 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 963}
This page took 0.13533 seconds and 4 git commands to generate.