Fix: (slight UI change) refuse missing -c if non-default channel exists
[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
44a5e5eb 23#include <assert.h>
fac6795d 24#include <grp.h>
1e307fab 25#include <errno.h>
fac6795d
DG
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
990570ed
DG
31#include <common/common.h>
32#include <common/defaults.h>
db758600 33#include <common/sessiond-comm/sessiond-comm.h>
a4b92340 34#include <common/uri.h>
feb0f3e5 35#include <common/utils.h>
1e307fab 36#include <lttng/lttng.h>
fac6795d 37
d00c599e
DG
38#include "filter/filter-ast.h"
39#include "filter/filter-parser.h"
40#include "filter/filter-bytecode.h"
41#include "filter/memstream.h"
cac3069d 42#include "lttng-ctl-helper.h"
53a80697
MD
43
44#ifdef DEBUG
d00c599e 45static const int print_xml = 1;
53a80697
MD
46#define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
48#else
d00c599e 49static const int print_xml = 0;
53a80697
MD
50#define dbg_printf(fmt, args...) \
51do { \
52 /* do nothing but check printf format */ \
53 if (0) \
54 printf("[debug liblttnctl] " fmt, ## args); \
55} while (0)
56#endif
57
58
fac6795d
DG
59/* Socket to session daemon for communication */
60static int sessiond_socket;
61static char sessiond_sock_path[PATH_MAX];
44a5e5eb 62static char health_sock_path[PATH_MAX];
fac6795d 63
fac6795d
DG
64/* Variables */
65static char *tracing_group;
66static int connected;
67
97e19046
DG
68/* Global */
69
70/*
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
74 */
75int lttng_opt_quiet;
76int lttng_opt_verbose;
77
99497cd0
MD
78/*
79 * Copy string from src to dst and enforce null terminated byte.
80 */
cac3069d
DG
81LTTNG_HIDDEN
82void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 83{
e7d6716d 84 if (src && dst) {
99497cd0
MD
85 strncpy(dst, src, len);
86 /* Enforce the NULL terminated byte */
87 dst[len - 1] = '\0';
cd80958d
DG
88 } else if (dst) {
89 dst[0] = '\0';
99497cd0
MD
90 }
91}
92
fac6795d 93/*
cd80958d 94 * Copy domain to lttcomm_session_msg domain.
fac6795d 95 *
cd80958d
DG
96 * If domain is unknown, default domain will be the kernel.
97 */
cac3069d
DG
98LTTNG_HIDDEN
99void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
100 struct lttng_domain *src)
cd80958d
DG
101{
102 if (src && dst) {
103 switch (src->type) {
00e2e675
DG
104 case LTTNG_DOMAIN_KERNEL:
105 case LTTNG_DOMAIN_UST:
00e2e675
DG
106 memcpy(dst, src, sizeof(struct lttng_domain));
107 break;
108 default:
109 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 110 break;
cd80958d
DG
111 }
112 }
113}
114
115/*
116 * Send lttcomm_session_msg to the session daemon.
fac6795d 117 *
1c8d13c8
TD
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
fac6795d 120 */
cd80958d 121static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
122{
123 int ret;
124
125 if (!connected) {
2f70b271 126 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 127 goto end;
fac6795d
DG
128 }
129
a4b92340
DG
130 DBG("LSM cmd type : %d", lsm->cmd_type);
131
be040666 132 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 133 sizeof(struct lttcomm_session_msg));
2f70b271
DG
134 if (ret < 0) {
135 ret = -LTTNG_ERR_FATAL;
136 }
e065084a
DG
137
138end:
139 return ret;
140}
141
53a80697
MD
142/*
143 * Send var len data to the session daemon.
144 *
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
147 */
148static int send_session_varlen(void *data, size_t len)
149{
150 int ret;
151
152 if (!connected) {
2f70b271 153 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
154 goto end;
155 }
a4b92340 156
53a80697
MD
157 if (!data || !len) {
158 ret = 0;
159 goto end;
160 }
161
162 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
163 if (ret < 0) {
164 ret = -LTTNG_ERR_FATAL;
165 }
53a80697
MD
166
167end:
168 return ret;
169}
170
e065084a 171/*
cd80958d 172 * Receive data from the sessiond socket.
e065084a 173 *
1c8d13c8
TD
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 176 */
ca95a216 177static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
178{
179 int ret;
180
181 if (!connected) {
2f70b271 182 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 183 goto end;
fac6795d
DG
184 }
185
ca95a216 186 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
187 if (ret < 0) {
188 ret = -LTTNG_ERR_FATAL;
189 }
fac6795d 190
e065084a 191end:
fac6795d
DG
192 return ret;
193}
194
195/*
1c8d13c8 196 * Check if we are in the specified group.
65beb5ff 197 *
2269e89e 198 * If yes return 1, else return -1.
947308c4
DG
199 */
200static int check_tracing_group(const char *grp_name)
201{
202 struct group *grp_tracing; /* no free(). See getgrnam(3) */
203 gid_t *grp_list;
204 int grp_list_size, grp_id, i;
205 int ret = -1;
206
207 /* Get GID of group 'tracing' */
208 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
209 if (!grp_tracing) {
210 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
211 goto end;
212 }
213
214 /* Get number of supplementary group IDs */
215 grp_list_size = getgroups(0, NULL);
216 if (grp_list_size < 0) {
217 perror("getgroups");
218 goto end;
219 }
220
221 /* Alloc group list of the right size */
222 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 223 if (!grp_list) {
1c8d13c8 224 perror("malloc");
00795392
MD
225 goto end;
226 }
947308c4 227 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 228 if (grp_id < 0) {
947308c4
DG
229 perror("getgroups");
230 goto free_list;
231 }
232
233 for (i = 0; i < grp_list_size; i++) {
234 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 235 ret = 1;
947308c4
DG
236 break;
237 }
238 }
239
240free_list:
241 free(grp_list);
242
243end:
244 return ret;
245}
246
247/*
2269e89e
DG
248 * Try connect to session daemon with sock_path.
249 *
250 * Return 0 on success, else -1
251 */
252static int try_connect_sessiond(const char *sock_path)
253{
254 int ret;
255
256 /* If socket exist, we check if the daemon listens for connect. */
257 ret = access(sock_path, F_OK);
258 if (ret < 0) {
259 /* Not alive */
2f70b271 260 goto error;
2269e89e
DG
261 }
262
263 ret = lttcomm_connect_unix_sock(sock_path);
264 if (ret < 0) {
265 /* Not alive */
2f70b271 266 goto error;
2269e89e
DG
267 }
268
269 ret = lttcomm_close_unix_sock(ret);
270 if (ret < 0) {
271 perror("lttcomm_close_unix_sock");
272 }
273
274 return 0;
2f70b271
DG
275
276error:
277 return -1;
2269e89e
DG
278}
279
280/*
2f70b271
DG
281 * Set sessiond socket path by putting it in the global sessiond_sock_path
282 * variable.
283 *
284 * Returns 0 on success, negative value on failure (the sessiond socket path
285 * is somehow too long or ENOMEM).
947308c4
DG
286 */
287static int set_session_daemon_path(void)
288{
2269e89e
DG
289 int in_tgroup = 0; /* In tracing group */
290 uid_t uid;
291
292 uid = getuid();
947308c4 293
2269e89e
DG
294 if (uid != 0) {
295 /* Are we in the tracing group ? */
296 in_tgroup = check_tracing_group(tracing_group);
297 }
298
08a9c49f 299 if ((uid == 0) || in_tgroup) {
cac3069d
DG
300 lttng_ctl_copy_string(sessiond_sock_path,
301 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 302 }
2269e89e 303
08a9c49f 304 if (uid != 0) {
c617c0c6
MD
305 int ret;
306
08a9c49f
TD
307 if (in_tgroup) {
308 /* Tracing group */
309 ret = try_connect_sessiond(sessiond_sock_path);
310 if (ret >= 0) {
311 goto end;
2269e89e 312 }
08a9c49f 313 /* Global session daemon not available... */
2269e89e 314 }
08a9c49f
TD
315 /* ...or not in tracing group (and not root), default */
316
317 /*
318 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
319 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
320 */
321 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 322 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 323 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 324 goto error;
947308c4 325 }
947308c4 326 }
08a9c49f 327end:
947308c4 328 return 0;
2f70b271
DG
329
330error:
331 return -1;
947308c4
DG
332}
333
65beb5ff
DG
334/*
335 * Connect to the LTTng session daemon.
336 *
337 * On success, return 0. On error, return -1.
338 */
339static int connect_sessiond(void)
340{
341 int ret;
342
da3c9ec1
DG
343 /* Don't try to connect if already connected. */
344 if (connected) {
345 return 0;
346 }
347
65beb5ff
DG
348 ret = set_session_daemon_path();
349 if (ret < 0) {
2f70b271 350 goto error;
65beb5ff
DG
351 }
352
353 /* Connect to the sesssion daemon */
354 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
355 if (ret < 0) {
2f70b271 356 goto error;
65beb5ff
DG
357 }
358
359 sessiond_socket = ret;
360 connected = 1;
361
362 return 0;
2f70b271
DG
363
364error:
365 return -1;
65beb5ff
DG
366}
367
368/*
1c8d13c8
TD
369 * Clean disconnect from the session daemon.
370 * On success, return 0. On error, return -1.
65beb5ff
DG
371 */
372static int disconnect_sessiond(void)
373{
374 int ret = 0;
375
376 if (connected) {
377 ret = lttcomm_close_unix_sock(sessiond_socket);
378 sessiond_socket = 0;
379 connected = 0;
380 }
381
382 return ret;
383}
384
35a6fdb7 385/*
cd80958d 386 * Ask the session daemon a specific command and put the data into buf.
53a80697 387 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 388 *
af87c45a 389 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 390 */
cac3069d
DG
391LTTNG_HIDDEN
392int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 393 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
394{
395 int ret;
396 size_t size;
397 void *data = NULL;
cd80958d 398 struct lttcomm_lttng_msg llm;
65beb5ff
DG
399
400 ret = connect_sessiond();
401 if (ret < 0) {
2f70b271 402 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
403 goto end;
404 }
405
65beb5ff 406 /* Send command to session daemon */
cd80958d 407 ret = send_session_msg(lsm);
65beb5ff 408 if (ret < 0) {
2f70b271 409 /* Ret value is a valid lttng error code. */
65beb5ff
DG
410 goto end;
411 }
53a80697
MD
412 /* Send var len data */
413 ret = send_session_varlen(vardata, varlen);
414 if (ret < 0) {
2f70b271 415 /* Ret value is a valid lttng error code. */
53a80697
MD
416 goto end;
417 }
65beb5ff
DG
418
419 /* Get header from data transmission */
420 ret = recv_data_sessiond(&llm, sizeof(llm));
421 if (ret < 0) {
2f70b271 422 /* Ret value is a valid lttng error code. */
65beb5ff
DG
423 goto end;
424 }
425
426 /* Check error code if OK */
f73fabfd 427 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
428 ret = -llm.ret_code;
429 goto end;
430 }
431
432 size = llm.data_size;
433 if (size == 0) {
874d3f84 434 /* If client free with size 0 */
a45d5536
DG
435 if (buf != NULL) {
436 *buf = NULL;
437 }
7d29a247 438 ret = 0;
65beb5ff
DG
439 goto end;
440 }
441
442 data = (void*) malloc(size);
443
444 /* Get payload data */
445 ret = recv_data_sessiond(data, size);
446 if (ret < 0) {
447 free(data);
448 goto end;
449 }
450
83009e5e
DG
451 /*
452 * Extra protection not to dereference a NULL pointer. If buf is NULL at
453 * this point, an error is returned and data is freed.
454 */
455 if (buf == NULL) {
2f70b271 456 ret = -LTTNG_ERR_INVALID;
83009e5e
DG
457 free(data);
458 goto end;
459 }
460
65beb5ff
DG
461 *buf = data;
462 ret = size;
463
464end:
465 disconnect_sessiond();
466 return ret;
467}
468
9f19cc17 469/*
cd80958d 470 * Create lttng handle and return pointer.
1c8d13c8 471 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 472 */
cd80958d
DG
473struct lttng_handle *lttng_create_handle(const char *session_name,
474 struct lttng_domain *domain)
9f19cc17 475{
2f70b271
DG
476 struct lttng_handle *handle = NULL;
477
478 if (domain == NULL) {
479 goto end;
480 }
cd80958d
DG
481
482 handle = malloc(sizeof(struct lttng_handle));
483 if (handle == NULL) {
2f70b271 484 PERROR("malloc handle");
cd80958d
DG
485 goto end;
486 }
487
488 /* Copy session name */
cac3069d 489 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
490 sizeof(handle->session_name));
491
492 /* Copy lttng domain */
cac3069d 493 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
cd80958d
DG
494
495end:
496 return handle;
497}
498
499/*
500 * Destroy handle by free(3) the pointer.
501 */
502void lttng_destroy_handle(struct lttng_handle *handle)
503{
0e428499 504 free(handle);
eb354453
DG
505}
506
d9800920
DG
507/*
508 * Register an outside consumer.
1c8d13c8 509 * Returns size of returned session payload data or a negative error code.
d9800920
DG
510 */
511int lttng_register_consumer(struct lttng_handle *handle,
512 const char *socket_path)
513{
514 struct lttcomm_session_msg lsm;
515
2f70b271
DG
516 if (handle == NULL || socket_path == NULL) {
517 return -LTTNG_ERR_INVALID;
518 }
519
d9800920 520 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 521 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 522 sizeof(lsm.session.name));
cac3069d 523 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 524
cac3069d 525 lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
d9800920 526
cac3069d 527 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
528}
529
1df4dedd 530/*
1c8d13c8
TD
531 * Start tracing for all traces of the session.
532 * Returns size of returned session payload data or a negative error code.
1df4dedd 533 */
6a4f824d 534int lttng_start_tracing(const char *session_name)
f3ed775e 535{
cd80958d
DG
536 struct lttcomm_session_msg lsm;
537
6a4f824d 538 if (session_name == NULL) {
2f70b271 539 return -LTTNG_ERR_INVALID;
cd80958d
DG
540 }
541
542 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 543
cac3069d
DG
544 lttng_ctl_copy_string(lsm.session.name, session_name,
545 sizeof(lsm.session.name));
cd80958d 546
cac3069d 547 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 548}
1df4dedd
DG
549
550/*
38ee087f 551 * Stop tracing for all traces of the session.
f3ed775e 552 */
38ee087f 553static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 554{
38ee087f 555 int ret, data_ret;
cd80958d
DG
556 struct lttcomm_session_msg lsm;
557
6a4f824d 558 if (session_name == NULL) {
2f70b271 559 return -LTTNG_ERR_INVALID;
6a4f824d
DG
560 }
561
cd80958d 562 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 563
cac3069d
DG
564 lttng_ctl_copy_string(lsm.session.name, session_name,
565 sizeof(lsm.session.name));
cd80958d 566
cac3069d 567 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
568 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
569 goto error;
570 }
571
572 if (!wait) {
573 goto end;
574 }
575
576 _MSG("Waiting for data availability");
577
578 /* Check for data availability */
579 do {
6d805429 580 data_ret = lttng_data_pending(session_name);
38ee087f
DG
581 if (data_ret < 0) {
582 /* Return the data available call error. */
583 ret = data_ret;
584 goto error;
585 }
586
587 /*
588 * Data sleep time before retrying (in usec). Don't sleep if the call
589 * returned value indicates availability.
590 */
6d805429 591 if (data_ret) {
38ee087f
DG
592 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
593 _MSG(".");
594 }
6d805429 595 } while (data_ret != 0);
38ee087f
DG
596
597 MSG("");
598
599end:
600error:
601 return ret;
602}
603
604/*
605 * Stop tracing and wait for data availability.
606 */
607int lttng_stop_tracing(const char *session_name)
608{
609 return _lttng_stop_tracing(session_name, 1);
610}
611
612/*
613 * Stop tracing but _don't_ wait for data availability.
614 */
615int lttng_stop_tracing_no_wait(const char *session_name)
616{
617 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
618}
619
620/*
601d5acf
DG
621 * Add context to a channel.
622 *
623 * If the given channel is NULL, add the contexts to all channels.
624 * The event_name param is ignored.
af87c45a
DG
625 *
626 * Returns the size of the returned payload data or a negative error code.
1df4dedd 627 */
cd80958d 628int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
629 struct lttng_event_context *ctx, const char *event_name,
630 const char *channel_name)
d65106b1 631{
cd80958d
DG
632 struct lttcomm_session_msg lsm;
633
9d697d3d
DG
634 /* Safety check. Both are mandatory */
635 if (handle == NULL || ctx == NULL) {
2f70b271 636 return -LTTNG_ERR_INVALID;
cd80958d
DG
637 }
638
441c16a7
MD
639 memset(&lsm, 0, sizeof(lsm));
640
cd80958d
DG
641 lsm.cmd_type = LTTNG_ADD_CONTEXT;
642
85076754
MD
643 /* If no channel name, send empty string. */
644 if (channel_name == NULL) {
645 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
646 sizeof(lsm.u.context.channel_name));
647 } else {
648 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
649 sizeof(lsm.u.context.channel_name));
650 }
cd80958d 651
cac3069d 652 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 653
9d697d3d 654 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 655
cac3069d 656 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
657 sizeof(lsm.session.name));
658
cac3069d 659 return lttng_ctl_ask_sessiond(&lsm, NULL);
d65106b1
DG
660}
661
f3ed775e 662/*
1c8d13c8
TD
663 * Enable event(s) for a channel.
664 * If no event name is specified, all events are enabled.
665 * If no channel name is specified, the default 'channel0' is used.
666 * Returns size of returned session payload data or a negative error code.
f3ed775e 667 */
cd80958d 668int lttng_enable_event(struct lttng_handle *handle,
38057ed1 669 struct lttng_event *ev, const char *channel_name)
1df4dedd 670{
cd80958d
DG
671 struct lttcomm_session_msg lsm;
672
5117eeec 673 if (handle == NULL || ev == NULL) {
2f70b271 674 return -LTTNG_ERR_INVALID;
cd80958d 675 }
33a2b854 676
441c16a7
MD
677 memset(&lsm, 0, sizeof(lsm));
678
85076754 679 /* If no channel name, send empty string. */
94cf3c47 680 if (channel_name == NULL) {
85076754 681 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
cd80958d 682 sizeof(lsm.u.enable.channel_name));
33a2b854 683 } else {
cac3069d 684 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
cd80958d 685 sizeof(lsm.u.enable.channel_name));
eb354453
DG
686 }
687
cac3069d 688 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 689
8c9ae521 690 if (ev->name[0] != '\0') {
cd80958d 691 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 692 } else {
cd80958d 693 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 694 }
8c9ae521 695 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 696
cac3069d 697 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
698 sizeof(lsm.session.name));
699
cac3069d 700 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
701}
702
53a80697 703/*
025faf73 704 * Create or enable an event with a filter expression.
178191b3 705 *
53a80697
MD
706 * Return negative error value on error.
707 * Return size of returned session payload data if OK.
708 */
025faf73 709int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 710 struct lttng_event *event, const char *channel_name,
53a80697
MD
711 const char *filter_expression)
712{
713 struct lttcomm_session_msg lsm;
714 struct filter_parser_ctx *ctx;
715 FILE *fmem;
716 int ret = 0;
717
52df2401
MD
718 if (!filter_expression) {
719 /*
720 * Fall back to normal event enabling if no filter
721 * specified.
722 */
723 return lttng_enable_event(handle, event, channel_name);
724 }
725
726 /*
727 * Empty filter string will always be rejected by the parser
728 * anyway, so treat this corner-case early to eliminate
729 * lttng_fmemopen error for 0-byte allocation.
730 */
731 if (handle == NULL || filter_expression[0] == '\0') {
2f70b271 732 return -LTTNG_ERR_INVALID;
53a80697
MD
733 }
734
53a80697
MD
735 /*
736 * casting const to non-const, as the underlying function will
737 * use it in read-only mode.
738 */
739 fmem = lttng_fmemopen((void *) filter_expression,
740 strlen(filter_expression), "r");
741 if (!fmem) {
742 fprintf(stderr, "Error opening memory as stream\n");
ff7ef7b8 743 return -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
744 }
745 ctx = filter_parser_ctx_alloc(fmem);
746 if (!ctx) {
747 fprintf(stderr, "Error allocating parser\n");
ff7ef7b8 748 ret = -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
749 goto alloc_error;
750 }
751 ret = filter_parser_ctx_append_ast(ctx);
752 if (ret) {
753 fprintf(stderr, "Parse error\n");
ff7ef7b8 754 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
755 goto parse_error;
756 }
757 ret = filter_visitor_set_parent(ctx);
758 if (ret) {
759 fprintf(stderr, "Set parent error\n");
ff7ef7b8 760 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
761 goto parse_error;
762 }
763 if (print_xml) {
764 ret = filter_visitor_print_xml(ctx, stdout, 0);
765 if (ret) {
766 fflush(stdout);
767 fprintf(stderr, "XML print error\n");
ff7ef7b8 768 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
769 goto parse_error;
770 }
771 }
772
773 dbg_printf("Generating IR... ");
774 fflush(stdout);
775 ret = filter_visitor_ir_generate(ctx);
776 if (ret) {
777 fprintf(stderr, "Generate IR error\n");
ff7ef7b8 778 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
779 goto parse_error;
780 }
781 dbg_printf("done\n");
782
783 dbg_printf("Validating IR... ");
784 fflush(stdout);
785 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
786 if (ret) {
ff7ef7b8 787 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
788 goto parse_error;
789 }
790 dbg_printf("done\n");
791
792 dbg_printf("Generating bytecode... ");
793 fflush(stdout);
794 ret = filter_visitor_bytecode_generate(ctx);
795 if (ret) {
796 fprintf(stderr, "Generate bytecode error\n");
ff7ef7b8 797 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
798 goto parse_error;
799 }
800 dbg_printf("done\n");
801 dbg_printf("Size of bytecode generated: %u bytes.\n",
802 bytecode_get_len(&ctx->bytecode->b));
803
804 memset(&lsm, 0, sizeof(lsm));
805
025faf73 806 lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER;
53a80697 807
85076754
MD
808 /* If no channel name, send empty string. */
809 if (channel_name == NULL) {
810 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
811 sizeof(lsm.u.enable.channel_name));
812 } else {
813 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
814 sizeof(lsm.u.enable.channel_name));
815 }
816
53a80697 817 /* Copy event name */
178191b3
DG
818 if (event) {
819 memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event));
820 }
821
025faf73 822 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
53a80697
MD
823 + bytecode_get_len(&ctx->bytecode->b);
824
cac3069d 825 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
53a80697 826
cac3069d 827 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
53a80697
MD
828 sizeof(lsm.session.name));
829
cac3069d 830 ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
025faf73 831 lsm.u.enable.bytecode_len, NULL);
53a80697
MD
832
833 filter_bytecode_free(ctx);
834 filter_ir_free(ctx);
835 filter_parser_ctx_free(ctx);
836 if (fclose(fmem) != 0) {
837 perror("fclose");
838 }
839 return ret;
840
841parse_error:
842 filter_bytecode_free(ctx);
843 filter_ir_free(ctx);
844 filter_parser_ctx_free(ctx);
845alloc_error:
846 if (fclose(fmem) != 0) {
847 perror("fclose");
848 }
849 return ret;
850}
851
1df4dedd 852/*
1c8d13c8
TD
853 * Disable event(s) of a channel and domain.
854 * If no event name is specified, all events are disabled.
855 * If no channel name is specified, the default 'channel0' is used.
856 * Returns size of returned session payload data or a negative error code.
1df4dedd 857 */
cd80958d 858int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 859 const char *channel_name)
1df4dedd 860{
cd80958d 861 struct lttcomm_session_msg lsm;
1df4dedd 862
9d697d3d 863 if (handle == NULL) {
2f70b271 864 return -LTTNG_ERR_INVALID;
cd80958d
DG
865 }
866
441c16a7
MD
867 memset(&lsm, 0, sizeof(lsm));
868
85076754
MD
869 /* If no channel name, send empty string. */
870 if (channel_name == NULL) {
871 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 872 sizeof(lsm.u.disable.channel_name));
f3ed775e 873 } else {
85076754 874 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 875 sizeof(lsm.u.disable.channel_name));
eb354453
DG
876 }
877
cac3069d 878 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 879
f84efadf 880 if (name != NULL) {
cac3069d
DG
881 lttng_ctl_copy_string(lsm.u.disable.name, name,
882 sizeof(lsm.u.disable.name));
cd80958d 883 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 884 } else {
cd80958d 885 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
886 }
887
cac3069d 888 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
889 sizeof(lsm.session.name));
890
cac3069d 891 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
892}
893
894/*
1c8d13c8
TD
895 * Enable channel per domain
896 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 897 */
cd80958d 898int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 899 struct lttng_channel *chan)
a5c5a2bd 900{
cd80958d
DG
901 struct lttcomm_session_msg lsm;
902
5117eeec
DG
903 /*
904 * NULL arguments are forbidden. No default values.
905 */
906 if (handle == NULL || chan == NULL) {
2f70b271 907 return -LTTNG_ERR_INVALID;
cd80958d
DG
908 }
909
441c16a7
MD
910 memset(&lsm, 0, sizeof(lsm));
911
5117eeec 912 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 913
cd80958d
DG
914 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
915
cac3069d 916 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 917
cac3069d 918 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
919 sizeof(lsm.session.name));
920
cac3069d 921 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 922}
1df4dedd 923
2ef84c95 924/*
1c8d13c8
TD
925 * All tracing will be stopped for registered events of the channel.
926 * Returns size of returned session payload data or a negative error code.
2ef84c95 927 */
cd80958d 928int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 929{
cd80958d
DG
930 struct lttcomm_session_msg lsm;
931
9d697d3d
DG
932 /* Safety check. Both are mandatory */
933 if (handle == NULL || name == NULL) {
2f70b271 934 return -LTTNG_ERR_INVALID;
cd80958d
DG
935 }
936
441c16a7
MD
937 memset(&lsm, 0, sizeof(lsm));
938
cd80958d 939 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 940
cac3069d 941 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
942 sizeof(lsm.u.disable.channel_name));
943
cac3069d 944 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 945
cac3069d 946 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
947 sizeof(lsm.session.name));
948
cac3069d 949 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
950}
951
fac6795d 952/*
1c8d13c8
TD
953 * Lists all available tracepoints of domain.
954 * Sets the contents of the events array.
955 * Returns the number of lttng_event entries in events;
956 * on error, returns a negative value.
fac6795d 957 */
cd80958d 958int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 959 struct lttng_event **events)
fac6795d 960{
052da939 961 int ret;
cd80958d
DG
962 struct lttcomm_session_msg lsm;
963
9d697d3d 964 if (handle == NULL) {
2f70b271 965 return -LTTNG_ERR_INVALID;
cd80958d 966 }
fac6795d 967
cd80958d 968 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 969 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 970
cac3069d 971 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
972 if (ret < 0) {
973 return ret;
eb354453 974 }
fac6795d 975
9f19cc17 976 return ret / sizeof(struct lttng_event);
fac6795d
DG
977}
978
f37d259d
MD
979/*
980 * Lists all available tracepoint fields of domain.
981 * Sets the contents of the event field array.
982 * Returns the number of lttng_event_field entries in events;
983 * on error, returns a negative value.
984 */
985int lttng_list_tracepoint_fields(struct lttng_handle *handle,
986 struct lttng_event_field **fields)
987{
988 int ret;
989 struct lttcomm_session_msg lsm;
990
991 if (handle == NULL) {
2f70b271 992 return -LTTNG_ERR_INVALID;
f37d259d
MD
993 }
994
995 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 996 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 997
cac3069d 998 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
999 if (ret < 0) {
1000 return ret;
1001 }
1002
1003 return ret / sizeof(struct lttng_event_field);
1004}
1005
1657e9bb 1006/*
1c8d13c8
TD
1007 * Returns a human readable string describing
1008 * the error code (a negative value).
1657e9bb 1009 */
9a745bc7 1010const char *lttng_strerror(int code)
1657e9bb 1011{
f73fabfd 1012 return error_get_str(code);
1657e9bb
DG
1013}
1014
aaf97519 1015/*
a4b92340
DG
1016 * Create a brand new session using name and url for destination.
1017 *
f73fabfd 1018 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1019 */
a4b92340 1020int lttng_create_session(const char *name, const char *url)
00e2e675 1021{
3dd05a85 1022 int ret;
a4b92340 1023 ssize_t size;
00e2e675 1024 struct lttcomm_session_msg lsm;
a4b92340 1025 struct lttng_uri *uris = NULL;
00e2e675 1026
a4b92340 1027 if (name == NULL) {
2f70b271 1028 return -LTTNG_ERR_INVALID;
00e2e675
DG
1029 }
1030
a4b92340 1031 memset(&lsm, 0, sizeof(lsm));
00e2e675 1032
a4b92340 1033 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1034 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1035
1036 /* There should never be a data URL */
bc894455 1037 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1038 if (size < 0) {
2f70b271 1039 return -LTTNG_ERR_INVALID;
00e2e675
DG
1040 }
1041
a4b92340
DG
1042 lsm.u.uri.size = size;
1043
cac3069d
DG
1044 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1045 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1046
1047 free(uris);
1048 return ret;
00e2e675
DG
1049}
1050
8028d920 1051/*
8028d920 1052 * Destroy session using name.
1c8d13c8 1053 * Returns size of returned session payload data or a negative error code.
8028d920 1054 */
843f5df9 1055int lttng_destroy_session(const char *session_name)
8028d920 1056{
cd80958d
DG
1057 struct lttcomm_session_msg lsm;
1058
843f5df9 1059 if (session_name == NULL) {
2f70b271 1060 return -LTTNG_ERR_INVALID;
cd80958d
DG
1061 }
1062
1063 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1064
cac3069d
DG
1065 lttng_ctl_copy_string(lsm.session.name, session_name,
1066 sizeof(lsm.session.name));
cd80958d 1067
cac3069d 1068 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1069}
1070
57167058 1071/*
57167058 1072 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1073 * Sets the contents of the sessions array.
1074 * Returns the number of lttng_session entries in sessions;
1075 * on error, returns a negative value.
57167058 1076 */
ca95a216 1077int lttng_list_sessions(struct lttng_session **sessions)
57167058 1078{
ca95a216 1079 int ret;
cd80958d 1080 struct lttcomm_session_msg lsm;
57167058 1081
cd80958d 1082 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1083 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1084 if (ret < 0) {
ca95a216 1085 return ret;
57167058
DG
1086 }
1087
ca95a216 1088 return ret / sizeof(struct lttng_session);
57167058
DG
1089}
1090
9f19cc17 1091/*
1c8d13c8
TD
1092 * Ask the session daemon for all available domains of a session.
1093 * Sets the contents of the domains array.
1094 * Returns the number of lttng_domain entries in domains;
1095 * on error, returns a negative value.
9f19cc17 1096 */
330be774 1097int lttng_list_domains(const char *session_name,
cd80958d 1098 struct lttng_domain **domains)
9f19cc17
DG
1099{
1100 int ret;
cd80958d
DG
1101 struct lttcomm_session_msg lsm;
1102
330be774 1103 if (session_name == NULL) {
2f70b271 1104 return -LTTNG_ERR_INVALID;
cd80958d 1105 }
9f19cc17 1106
cd80958d
DG
1107 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1108
cac3069d
DG
1109 lttng_ctl_copy_string(lsm.session.name, session_name,
1110 sizeof(lsm.session.name));
cd80958d 1111
cac3069d 1112 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1113 if (ret < 0) {
1114 return ret;
1115 }
1116
1117 return ret / sizeof(struct lttng_domain);
1118}
1119
1120/*
1c8d13c8
TD
1121 * Ask the session daemon for all available channels of a session.
1122 * Sets the contents of the channels array.
1123 * Returns the number of lttng_channel entries in channels;
1124 * on error, returns a negative value.
9f19cc17 1125 */
cd80958d
DG
1126int lttng_list_channels(struct lttng_handle *handle,
1127 struct lttng_channel **channels)
9f19cc17
DG
1128{
1129 int ret;
cd80958d
DG
1130 struct lttcomm_session_msg lsm;
1131
9d697d3d 1132 if (handle == NULL) {
2f70b271 1133 return -LTTNG_ERR_INVALID;
cd80958d
DG
1134 }
1135
1136 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1137 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1138 sizeof(lsm.session.name));
9f19cc17 1139
cac3069d 1140 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1141
cac3069d 1142 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1143 if (ret < 0) {
1144 return ret;
1145 }
1146
1147 return ret / sizeof(struct lttng_channel);
1148}
1149
1150/*
1c8d13c8
TD
1151 * Ask the session daemon for all available events of a session channel.
1152 * Sets the contents of the events array.
1153 * Returns the number of lttng_event entries in events;
1154 * on error, returns a negative value.
9f19cc17 1155 */
cd80958d
DG
1156int lttng_list_events(struct lttng_handle *handle,
1157 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1158{
1159 int ret;
cd80958d 1160 struct lttcomm_session_msg lsm;
9f19cc17 1161
9d697d3d
DG
1162 /* Safety check. An handle and channel name are mandatory */
1163 if (handle == NULL || channel_name == NULL) {
2f70b271 1164 return -LTTNG_ERR_INVALID;
cd80958d
DG
1165 }
1166
1167 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1168 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1169 sizeof(lsm.session.name));
cac3069d 1170 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d
DG
1171 sizeof(lsm.u.list.channel_name));
1172
cac3069d 1173 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1174
cac3069d 1175 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1176 if (ret < 0) {
1177 return ret;
1178 }
1179
1180 return ret / sizeof(struct lttng_event);
1181}
1182
fac6795d 1183/*
1c8d13c8
TD
1184 * Sets the tracing_group variable with name.
1185 * This function allocates memory pointed to by tracing_group.
1186 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1187 */
1188int lttng_set_tracing_group(const char *name)
1189{
9d697d3d 1190 if (name == NULL) {
2f70b271 1191 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1192 }
1193
fac6795d 1194 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1195 return -LTTNG_ERR_FATAL;
fac6795d
DG
1196 }
1197
1198 return 0;
1199}
1200
d0254c7c 1201/*
af87c45a 1202 * Returns size of returned session payload data or a negative error code.
d0254c7c 1203 */
cd80958d 1204int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1205 struct lttng_calibrate *calibrate)
1206{
cd80958d 1207 struct lttcomm_session_msg lsm;
d0254c7c 1208
9d697d3d
DG
1209 /* Safety check. NULL pointer are forbidden */
1210 if (handle == NULL || calibrate == NULL) {
2f70b271 1211 return -LTTNG_ERR_INVALID;
cd80958d 1212 }
d0254c7c 1213
cd80958d 1214 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1215 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1216
cd80958d
DG
1217 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1218
cac3069d 1219 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1220}
1221
5edd7e09
DG
1222/*
1223 * Set default channel attributes.
441c16a7 1224 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1225 */
1226void lttng_channel_set_default_attr(struct lttng_domain *domain,
1227 struct lttng_channel_attr *attr)
1228{
1229 /* Safety check */
1230 if (attr == NULL || domain == NULL) {
1231 return;
1232 }
1233
eacaa7a6
DS
1234 memset(attr, 0, sizeof(struct lttng_channel_attr));
1235
0a9c6494
DG
1236 /* Same for all domains. */
1237 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1238 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1239 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1240
5edd7e09
DG
1241 switch (domain->type) {
1242 case LTTNG_DOMAIN_KERNEL:
d92ff3ef
DG
1243 attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1244 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1245 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1246 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1247 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1248 break;
1249 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1250 switch (domain->buf_type) {
1251 case LTTNG_BUFFER_PER_UID:
1252 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1253 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1254 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1255 attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1256 attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1257 break;
1258 case LTTNG_BUFFER_PER_PID:
1259 default:
1260 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1261 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1262 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1263 attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1264 attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1265 break;
1266 }
5edd7e09 1267 default:
441c16a7 1268 /* Default behavior: leave set to 0. */
5edd7e09
DG
1269 break;
1270 }
1271}
1272
fac6795d 1273/*
2269e89e 1274 * Check if session daemon is alive.
fac6795d 1275 *
2269e89e 1276 * Return 1 if alive or 0 if not.
1c8d13c8 1277 * On error returns a negative value.
fac6795d 1278 */
947308c4 1279int lttng_session_daemon_alive(void)
fac6795d
DG
1280{
1281 int ret;
1282
1283 ret = set_session_daemon_path();
1284 if (ret < 0) {
947308c4 1285 /* Error */
fac6795d
DG
1286 return ret;
1287 }
1288
9d035200 1289 if (*sessiond_sock_path == '\0') {
2f70b271
DG
1290 /*
1291 * No socket path set. Weird error which means the constructor was not
1292 * called.
1293 */
1294 assert(0);
fac6795d
DG
1295 }
1296
2269e89e 1297 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1298 if (ret < 0) {
1299 /* Not alive */
1300 return 0;
1301 }
7d8234d9 1302
947308c4
DG
1303 /* Is alive */
1304 return 1;
fac6795d
DG
1305}
1306
00e2e675 1307/*
a4b92340 1308 * Set URL for a consumer for a session and domain.
00e2e675
DG
1309 *
1310 * Return 0 on success, else a negative value.
1311 */
a4b92340
DG
1312int lttng_set_consumer_url(struct lttng_handle *handle,
1313 const char *control_url, const char *data_url)
00e2e675 1314{
3dd05a85 1315 int ret;
a4b92340 1316 ssize_t size;
00e2e675 1317 struct lttcomm_session_msg lsm;
a4b92340 1318 struct lttng_uri *uris = NULL;
00e2e675 1319
a4b92340 1320 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1321 return -LTTNG_ERR_INVALID;
00e2e675
DG
1322 }
1323
a4b92340
DG
1324 memset(&lsm, 0, sizeof(lsm));
1325
00e2e675
DG
1326 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1327
cac3069d 1328 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1329 sizeof(lsm.session.name));
cac3069d 1330 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1331
bc894455 1332 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1333 if (size < 0) {
2f70b271 1334 return -LTTNG_ERR_INVALID;
a4b92340
DG
1335 }
1336
1337 lsm.u.uri.size = size;
00e2e675 1338
cac3069d
DG
1339 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1340 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1341
1342 free(uris);
1343 return ret;
00e2e675
DG
1344}
1345
1346/*
9c6bda17 1347 * [OBSOLETE]
00e2e675
DG
1348 */
1349int lttng_enable_consumer(struct lttng_handle *handle)
1350{
785d2d0d 1351 return -ENOSYS;
00e2e675
DG
1352}
1353
1354/*
9c6bda17 1355 * [OBSOLETE]
00e2e675
DG
1356 */
1357int lttng_disable_consumer(struct lttng_handle *handle)
1358{
785d2d0d 1359 return -ENOSYS;
00e2e675
DG
1360}
1361
44a5e5eb
DG
1362/*
1363 * Set health socket path by putting it in the global health_sock_path
1364 * variable.
1365 *
1366 * Returns 0 on success or assert(0) on ENOMEM.
1367 */
1368static int set_health_socket_path(void)
1369{
44a5e5eb
DG
1370 int in_tgroup = 0; /* In tracing group */
1371 uid_t uid;
1372 const char *home;
1373
1374 uid = getuid();
1375
1376 if (uid != 0) {
1377 /* Are we in the tracing group ? */
1378 in_tgroup = check_tracing_group(tracing_group);
1379 }
1380
1381 if ((uid == 0) || in_tgroup) {
cac3069d
DG
1382 lttng_ctl_copy_string(health_sock_path,
1383 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, sizeof(health_sock_path));
44a5e5eb
DG
1384 }
1385
1386 if (uid != 0) {
c617c0c6
MD
1387 int ret;
1388
44a5e5eb
DG
1389 /*
1390 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1391 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1392 */
feb0f3e5 1393 home = utils_get_home_dir();
44a5e5eb
DG
1394 if (home == NULL) {
1395 /* Fallback in /tmp .. */
1396 home = "/tmp";
1397 }
1398
1399 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1400 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1401 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1402 /* ENOMEM at this point... just kill the control lib. */
1403 assert(0);
1404 }
1405 }
1406
1407 return 0;
1408}
1409
1410/*
1411 * Check session daemon health for a specific health component.
1412 *
2f70b271 1413 * Return 0 if health is OK or else 1 if BAD.
44a5e5eb 1414 *
2f70b271 1415 * Any other negative value is a lttng error code which can be translated with
44a5e5eb
DG
1416 * lttng_strerror().
1417 */
1418int lttng_health_check(enum lttng_health_component c)
1419{
1420 int sock, ret;
1421 struct lttcomm_health_msg msg;
1422 struct lttcomm_health_data reply;
1423
1424 /* Connect to the sesssion daemon */
1425 sock = lttcomm_connect_unix_sock(health_sock_path);
1426 if (sock < 0) {
2f70b271 1427 ret = -LTTNG_ERR_NO_SESSIOND;
44a5e5eb
DG
1428 goto error;
1429 }
1430
1431 msg.cmd = LTTNG_HEALTH_CHECK;
1432 msg.component = c;
1433
1434 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1435 if (ret < 0) {
2f70b271 1436 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1437 goto close_error;
1438 }
1439
1440 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1441 if (ret < 0) {
2f70b271 1442 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1443 goto close_error;
1444 }
1445
1446 ret = reply.ret_code;
1447
1448close_error:
67ef15b7
MD
1449 {
1450 int closeret;
1451
1452 closeret = close(sock);
1453 assert(!closeret);
1454 }
44a5e5eb
DG
1455
1456error:
1457 return ret;
1458}
1459
07424f16
DG
1460/*
1461 * This is an extension of create session that is ONLY and SHOULD only be used
1462 * by the lttng command line program. It exists to avoid using URI parsing in
1463 * the lttng client.
1464 *
1465 * We need the date and time for the trace path subdirectory for the case where
1466 * the user does NOT define one using either -o or -U. Using the normal
1467 * lttng_create_session API call, we have no clue on the session daemon side if
1468 * the URL was generated automatically by the client or define by the user.
1469 *
1470 * So this function "wrapper" is hidden from the public API, takes the datetime
1471 * string and appends it if necessary to the URI subdirectory before sending it
1472 * to the session daemon.
1473 *
1474 * With this extra function, the lttng_create_session call behavior is not
1475 * changed and the timestamp is appended to the URI on the session daemon side
1476 * if necessary.
1477 */
1478int _lttng_create_session_ext(const char *name, const char *url,
1479 const char *datetime)
1480{
3dd05a85 1481 int ret;
07424f16
DG
1482 ssize_t size;
1483 struct lttcomm_session_msg lsm;
1484 struct lttng_uri *uris = NULL;
1485
2bba9e53 1486 if (name == NULL || datetime == NULL) {
2f70b271 1487 return -LTTNG_ERR_INVALID;
07424f16
DG
1488 }
1489
1490 memset(&lsm, 0, sizeof(lsm));
1491
1492 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1493 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16
DG
1494
1495 /* There should never be a data URL */
bc894455 1496 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 1497 if (size < 0) {
3dd05a85
DG
1498 ret = -LTTNG_ERR_INVALID;
1499 goto error;
07424f16
DG
1500 }
1501
1502 lsm.u.uri.size = size;
1503
4b35a6b3 1504 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
1505 /* Don't append datetime if the name was automatically created. */
1506 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1507 strlen(DEFAULT_SESSION_NAME) + 1)) {
1508 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1509 name, datetime);
1510 } else {
1511 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1512 }
07424f16
DG
1513 if (ret < 0) {
1514 PERROR("snprintf uri subdir");
3dd05a85
DG
1515 ret = -LTTNG_ERR_FATAL;
1516 goto error;
07424f16
DG
1517 }
1518 }
1519
cac3069d
DG
1520 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1521 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1522
1523error:
1524 free(uris);
1525 return ret;
07424f16
DG
1526}
1527
806e2684
DG
1528/*
1529 * For a given session name, this call checks if the data is ready to be read
1530 * or is still being extracted by the consumer(s) hence not ready to be used by
1531 * any readers.
1532 */
6d805429 1533int lttng_data_pending(const char *session_name)
806e2684
DG
1534{
1535 int ret;
1536 struct lttcomm_session_msg lsm;
1537
1538 if (session_name == NULL) {
1539 return -LTTNG_ERR_INVALID;
1540 }
1541
6d805429 1542 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 1543
cac3069d
DG
1544 lttng_ctl_copy_string(lsm.session.name, session_name,
1545 sizeof(lsm.session.name));
806e2684 1546
cac3069d 1547 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
806e2684
DG
1548
1549 /*
cac3069d
DG
1550 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1551 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1552 * that the data is available. Yes it is hackish but for now this is the
1553 * only way.
806e2684
DG
1554 */
1555 if (ret == -1) {
1556 ret = 1;
1557 }
1558
1559 return ret;
1560}
1561
27babd3a
DG
1562/*
1563 * Create a session exclusively used for snapshot.
1564 *
1565 * Returns LTTNG_OK on success or a negative error code.
1566 */
1567int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1568{
1569 int ret;
1570 ssize_t size;
1571 struct lttcomm_session_msg lsm;
1572 struct lttng_uri *uris = NULL;
1573
1574 if (name == NULL) {
1575 return -LTTNG_ERR_INVALID;
1576 }
1577
1578 memset(&lsm, 0, sizeof(lsm));
1579
1580 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
1581 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1582
1583 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1584 if (size < 0) {
1585 return -LTTNG_ERR_INVALID;
1586 }
1587
1588 lsm.u.uri.size = size;
1589
1590 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1591 sizeof(struct lttng_uri) * size, NULL);
1592
1593 free(uris);
1594 return ret;
1595}
1596
fac6795d
DG
1597/*
1598 * lib constructor
1599 */
1600static void __attribute__((constructor)) init()
1601{
1602 /* Set default session group */
bbccc3d2 1603 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
44a5e5eb
DG
1604 /* Set socket for health check */
1605 (void) set_health_socket_path();
fac6795d 1606}
49cca668
DG
1607
1608/*
1609 * lib destructor
1610 */
1611static void __attribute__((destructor)) lttng_ctl_exit()
1612{
1613 free(tracing_group);
1614}
This page took 0.1311 seconds and 5 git commands to generate.