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