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