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