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