Add lttng_event_get_filter_string() to liblttng-ctl
[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>
2001793c 7 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 8 *
d14d33bf
AM
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
82a3637f
DG
12 *
13 * This library is distributed in the hope that it will be useful,
fac6795d 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
d14d33bf
AM
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
21 */
22
6c1c0768 23#define _LGPL_SOURCE
44a5e5eb 24#include <assert.h>
fac6795d 25#include <grp.h>
1e307fab 26#include <errno.h>
fac6795d
DG
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
990570ed
DG
32#include <common/common.h>
33#include <common/defaults.h>
db758600 34#include <common/sessiond-comm/sessiond-comm.h>
a4b92340 35#include <common/uri.h>
feb0f3e5 36#include <common/utils.h>
1e307fab 37#include <lttng/lttng.h>
0c89d795 38#include <lttng/health-internal.h>
fac6795d 39
d00c599e
DG
40#include "filter/filter-ast.h"
41#include "filter/filter-parser.h"
42#include "filter/filter-bytecode.h"
43#include "filter/memstream.h"
cac3069d 44#include "lttng-ctl-helper.h"
53a80697
MD
45
46#ifdef DEBUG
d00c599e 47static const int print_xml = 1;
53a80697
MD
48#define dbg_printf(fmt, args...) \
49 printf("[debug liblttng-ctl] " fmt, ## args)
50#else
d00c599e 51static const int print_xml = 0;
53a80697
MD
52#define dbg_printf(fmt, args...) \
53do { \
54 /* do nothing but check printf format */ \
55 if (0) \
56 printf("[debug liblttnctl] " fmt, ## args); \
57} while (0)
58#endif
59
60
fac6795d
DG
61/* Socket to session daemon for communication */
62static int sessiond_socket;
63static char sessiond_sock_path[PATH_MAX];
64
fac6795d
DG
65/* Variables */
66static char *tracing_group;
67static int connected;
68
97e19046
DG
69/* Global */
70
71/*
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
75 */
76int lttng_opt_quiet;
77int lttng_opt_verbose;
c7e35b03 78int lttng_opt_mi;
97e19046 79
99497cd0
MD
80/*
81 * Copy string from src to dst and enforce null terminated byte.
82 */
cac3069d
DG
83LTTNG_HIDDEN
84void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 85{
e7d6716d 86 if (src && dst) {
99497cd0
MD
87 strncpy(dst, src, len);
88 /* Enforce the NULL terminated byte */
89 dst[len - 1] = '\0';
cd80958d
DG
90 } else if (dst) {
91 dst[0] = '\0';
99497cd0
MD
92 }
93}
94
fac6795d 95/*
cd80958d 96 * Copy domain to lttcomm_session_msg domain.
fac6795d 97 *
cd80958d
DG
98 * If domain is unknown, default domain will be the kernel.
99 */
cac3069d
DG
100LTTNG_HIDDEN
101void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
102 struct lttng_domain *src)
cd80958d
DG
103{
104 if (src && dst) {
105 switch (src->type) {
00e2e675
DG
106 case LTTNG_DOMAIN_KERNEL:
107 case LTTNG_DOMAIN_UST:
b9dfb167 108 case LTTNG_DOMAIN_JUL:
5cdb6027 109 case LTTNG_DOMAIN_LOG4J:
0e115563 110 case LTTNG_DOMAIN_PYTHON:
00e2e675
DG
111 memcpy(dst, src, sizeof(struct lttng_domain));
112 break;
113 default:
114 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 115 break;
cd80958d
DG
116 }
117 }
118}
119
120/*
121 * Send lttcomm_session_msg to the session daemon.
fac6795d 122 *
1c8d13c8
TD
123 * On success, returns the number of bytes sent (>=0)
124 * On error, returns -1
fac6795d 125 */
cd80958d 126static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
127{
128 int ret;
129
130 if (!connected) {
2f70b271 131 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 132 goto end;
fac6795d
DG
133 }
134
a4b92340
DG
135 DBG("LSM cmd type : %d", lsm->cmd_type);
136
be040666 137 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 138 sizeof(struct lttcomm_session_msg));
2f70b271
DG
139 if (ret < 0) {
140 ret = -LTTNG_ERR_FATAL;
141 }
e065084a
DG
142
143end:
144 return ret;
145}
146
53a80697
MD
147/*
148 * Send var len data to the session daemon.
149 *
150 * On success, returns the number of bytes sent (>=0)
151 * On error, returns -1
152 */
c2d69327 153static int send_session_varlen(const void *data, size_t len)
53a80697
MD
154{
155 int ret;
156
157 if (!connected) {
2f70b271 158 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
159 goto end;
160 }
a4b92340 161
53a80697
MD
162 if (!data || !len) {
163 ret = 0;
164 goto end;
165 }
166
167 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
168 if (ret < 0) {
169 ret = -LTTNG_ERR_FATAL;
170 }
53a80697
MD
171
172end:
173 return ret;
174}
175
e065084a 176/*
cd80958d 177 * Receive data from the sessiond socket.
e065084a 178 *
1c8d13c8
TD
179 * On success, returns the number of bytes received (>=0)
180 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 181 */
ca95a216 182static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
183{
184 int ret;
185
186 if (!connected) {
2f70b271 187 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 188 goto end;
fac6795d
DG
189 }
190
ca95a216 191 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
192 if (ret < 0) {
193 ret = -LTTNG_ERR_FATAL;
194 }
fac6795d 195
e065084a 196end:
fac6795d
DG
197 return ret;
198}
199
200/*
9ae110e2 201 * Check if we are in the specified group.
65beb5ff 202 *
9ae110e2 203 * If yes return 1, else return -1.
947308c4 204 */
6c71277b
MD
205LTTNG_HIDDEN
206int lttng_check_tracing_group(void)
947308c4
DG
207{
208 struct group *grp_tracing; /* no free(). See getgrnam(3) */
209 gid_t *grp_list;
210 int grp_list_size, grp_id, i;
211 int ret = -1;
6c71277b 212 const char *grp_name = tracing_group;
947308c4
DG
213
214 /* Get GID of group 'tracing' */
215 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
216 if (!grp_tracing) {
217 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
218 goto end;
219 }
220
221 /* Get number of supplementary group IDs */
222 grp_list_size = getgroups(0, NULL);
223 if (grp_list_size < 0) {
6f04ed72 224 PERROR("getgroups");
947308c4
DG
225 goto end;
226 }
227
228 /* Alloc group list of the right size */
3f451dc0 229 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
00795392 230 if (!grp_list) {
6f04ed72 231 PERROR("malloc");
00795392
MD
232 goto end;
233 }
947308c4 234 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 235 if (grp_id < 0) {
6f04ed72 236 PERROR("getgroups");
947308c4
DG
237 goto free_list;
238 }
239
240 for (i = 0; i < grp_list_size; i++) {
241 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 242 ret = 1;
947308c4
DG
243 break;
244 }
245 }
246
247free_list:
248 free(grp_list);
249
250end:
251 return ret;
252}
253
254/*
2269e89e
DG
255 * Try connect to session daemon with sock_path.
256 *
257 * Return 0 on success, else -1
258 */
259static int try_connect_sessiond(const char *sock_path)
260{
261 int ret;
262
263 /* If socket exist, we check if the daemon listens for connect. */
264 ret = access(sock_path, F_OK);
265 if (ret < 0) {
266 /* Not alive */
2f70b271 267 goto error;
2269e89e
DG
268 }
269
270 ret = lttcomm_connect_unix_sock(sock_path);
271 if (ret < 0) {
9ae110e2 272 /* Not alive. */
2f70b271 273 goto error;
2269e89e
DG
274 }
275
276 ret = lttcomm_close_unix_sock(ret);
277 if (ret < 0) {
6f04ed72 278 PERROR("lttcomm_close_unix_sock");
2269e89e
DG
279 }
280
281 return 0;
2f70b271
DG
282
283error:
284 return -1;
2269e89e
DG
285}
286
287/*
2f70b271
DG
288 * Set sessiond socket path by putting it in the global sessiond_sock_path
289 * variable.
290 *
291 * Returns 0 on success, negative value on failure (the sessiond socket path
292 * is somehow too long or ENOMEM).
947308c4
DG
293 */
294static int set_session_daemon_path(void)
295{
9ae110e2 296 int in_tgroup = 0; /* In tracing group. */
2269e89e
DG
297 uid_t uid;
298
299 uid = getuid();
947308c4 300
2269e89e
DG
301 if (uid != 0) {
302 /* Are we in the tracing group ? */
6c71277b 303 in_tgroup = lttng_check_tracing_group();
2269e89e
DG
304 }
305
08a9c49f 306 if ((uid == 0) || in_tgroup) {
cac3069d
DG
307 lttng_ctl_copy_string(sessiond_sock_path,
308 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 309 }
2269e89e 310
08a9c49f 311 if (uid != 0) {
c617c0c6
MD
312 int ret;
313
08a9c49f 314 if (in_tgroup) {
9ae110e2 315 /* Tracing group. */
08a9c49f
TD
316 ret = try_connect_sessiond(sessiond_sock_path);
317 if (ret >= 0) {
318 goto end;
2269e89e 319 }
08a9c49f 320 /* Global session daemon not available... */
2269e89e 321 }
08a9c49f
TD
322 /* ...or not in tracing group (and not root), default */
323
324 /*
9ae110e2
JG
325 * With GNU C < 2.1, snprintf returns -1 if the target buffer
326 * is too small;
327 * With GNU C >= 2.1, snprintf returns the required size
328 * (excluding closing null)
08a9c49f
TD
329 */
330 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 331 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 332 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 333 goto error;
947308c4 334 }
947308c4 335 }
08a9c49f 336end:
947308c4 337 return 0;
2f70b271
DG
338
339error:
340 return -1;
947308c4
DG
341}
342
65beb5ff 343/*
9ae110e2 344 * Connect to the LTTng session daemon.
65beb5ff 345 *
9ae110e2 346 * On success, return 0. On error, return -1.
65beb5ff
DG
347 */
348static int connect_sessiond(void)
349{
350 int ret;
351
da3c9ec1
DG
352 /* Don't try to connect if already connected. */
353 if (connected) {
354 return 0;
355 }
356
65beb5ff
DG
357 ret = set_session_daemon_path();
358 if (ret < 0) {
2f70b271 359 goto error;
65beb5ff
DG
360 }
361
9ae110e2 362 /* Connect to the sesssion daemon. */
65beb5ff
DG
363 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
364 if (ret < 0) {
2f70b271 365 goto error;
65beb5ff
DG
366 }
367
368 sessiond_socket = ret;
369 connected = 1;
370
371 return 0;
2f70b271
DG
372
373error:
374 return -1;
65beb5ff
DG
375}
376
377/*
1c8d13c8 378 * Clean disconnect from the session daemon.
9ae110e2 379 *
1c8d13c8 380 * On success, return 0. On error, return -1.
65beb5ff
DG
381 */
382static int disconnect_sessiond(void)
383{
384 int ret = 0;
385
386 if (connected) {
387 ret = lttcomm_close_unix_sock(sessiond_socket);
388 sessiond_socket = 0;
389 connected = 0;
390 }
391
392 return ret;
393}
394
795a978d
PP
395static int recv_sessiond_optional_data(size_t len, void **user_buf,
396 size_t *user_len)
397{
398 int ret = 0;
399 void *buf = NULL;
400
401 if (len) {
402 if (!user_len) {
403 ret = -LTTNG_ERR_INVALID;
404 goto end;
405 }
406
407 buf = zmalloc(len);
408 if (!buf) {
409 ret = -ENOMEM;
410 goto end;
411 }
412
413 ret = recv_data_sessiond(buf, len);
414 if (ret < 0) {
415 goto end;
416 }
417
418 if (!user_buf) {
419 ret = -LTTNG_ERR_INVALID;
420 goto end;
421 }
422
423 /* Move ownership of command header buffer to user. */
424 *user_buf = buf;
425 buf = NULL;
426 *user_len = len;
427 } else {
428 /* No command header. */
429 if (user_len) {
430 *user_len = 0;
431 }
432
433 if (user_buf) {
434 *user_buf = NULL;
435 }
436 }
437
438end:
439 free(buf);
440 return ret;
441}
442
35a6fdb7 443/*
cd80958d 444 * Ask the session daemon a specific command and put the data into buf.
53a80697 445 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 446 *
af87c45a 447 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 448 */
cac3069d
DG
449LTTNG_HIDDEN
450int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
795a978d
PP
451 const void *vardata, size_t vardata_len,
452 void **user_payload_buf, void **user_cmd_header_buf,
453 size_t *user_cmd_header_len)
65beb5ff
DG
454{
455 int ret;
795a978d 456 size_t payload_len;
cd80958d 457 struct lttcomm_lttng_msg llm;
65beb5ff
DG
458
459 ret = connect_sessiond();
460 if (ret < 0) {
2f70b271 461 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
462 goto end;
463 }
464
65beb5ff 465 /* Send command to session daemon */
cd80958d 466 ret = send_session_msg(lsm);
65beb5ff 467 if (ret < 0) {
2f70b271 468 /* Ret value is a valid lttng error code. */
65beb5ff
DG
469 goto end;
470 }
53a80697 471 /* Send var len data */
795a978d 472 ret = send_session_varlen(vardata, vardata_len);
53a80697 473 if (ret < 0) {
2f70b271 474 /* Ret value is a valid lttng error code. */
53a80697
MD
475 goto end;
476 }
65beb5ff
DG
477
478 /* Get header from data transmission */
479 ret = recv_data_sessiond(&llm, sizeof(llm));
480 if (ret < 0) {
2f70b271 481 /* Ret value is a valid lttng error code. */
65beb5ff
DG
482 goto end;
483 }
484
485 /* Check error code if OK */
f73fabfd 486 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
487 ret = -llm.ret_code;
488 goto end;
489 }
490
795a978d
PP
491 /* Get command header from data transmission */
492 ret = recv_sessiond_optional_data(llm.cmd_header_size,
493 user_cmd_header_buf, user_cmd_header_len);
65beb5ff 494 if (ret < 0) {
65beb5ff
DG
495 goto end;
496 }
497
795a978d
PP
498 /* Get payload from data transmission */
499 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
500 &payload_len);
501 if (ret < 0) {
83009e5e
DG
502 goto end;
503 }
504
795a978d 505 ret = llm.data_size;
65beb5ff
DG
506
507end:
508 disconnect_sessiond();
509 return ret;
510}
511
9f19cc17 512/*
cd80958d 513 * Create lttng handle and return pointer.
9ae110e2 514 *
1c8d13c8 515 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 516 */
cd80958d
DG
517struct lttng_handle *lttng_create_handle(const char *session_name,
518 struct lttng_domain *domain)
9f19cc17 519{
2f70b271
DG
520 struct lttng_handle *handle = NULL;
521
522 if (domain == NULL) {
523 goto end;
524 }
cd80958d 525
3f451dc0 526 handle = zmalloc(sizeof(struct lttng_handle));
cd80958d 527 if (handle == NULL) {
2f70b271 528 PERROR("malloc handle");
cd80958d
DG
529 goto end;
530 }
531
532 /* Copy session name */
cac3069d 533 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
534 sizeof(handle->session_name));
535
536 /* Copy lttng domain */
cac3069d 537 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
cd80958d
DG
538
539end:
540 return handle;
541}
542
543/*
544 * Destroy handle by free(3) the pointer.
545 */
546void lttng_destroy_handle(struct lttng_handle *handle)
547{
0e428499 548 free(handle);
eb354453
DG
549}
550
d9800920
DG
551/*
552 * Register an outside consumer.
9ae110e2 553 *
1c8d13c8 554 * Returns size of returned session payload data or a negative error code.
d9800920
DG
555 */
556int lttng_register_consumer(struct lttng_handle *handle,
557 const char *socket_path)
558{
559 struct lttcomm_session_msg lsm;
560
2f70b271
DG
561 if (handle == NULL || socket_path == NULL) {
562 return -LTTNG_ERR_INVALID;
563 }
564
53efb85a 565 memset(&lsm, 0, sizeof(lsm));
d9800920 566 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 567 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 568 sizeof(lsm.session.name));
cac3069d 569 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 570
9ae110e2
JG
571 lttng_ctl_copy_string(lsm.u.reg.path, socket_path,
572 sizeof(lsm.u.reg.path));
d9800920 573
cac3069d 574 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
575}
576
1df4dedd 577/*
9ae110e2
JG
578 * Start tracing for all traces of the session.
579 *
580 * Returns size of returned session payload data or a negative error code.
1df4dedd 581 */
6a4f824d 582int lttng_start_tracing(const char *session_name)
f3ed775e 583{
cd80958d
DG
584 struct lttcomm_session_msg lsm;
585
6a4f824d 586 if (session_name == NULL) {
2f70b271 587 return -LTTNG_ERR_INVALID;
cd80958d
DG
588 }
589
53efb85a 590 memset(&lsm, 0, sizeof(lsm));
cd80958d 591 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 592
cac3069d
DG
593 lttng_ctl_copy_string(lsm.session.name, session_name,
594 sizeof(lsm.session.name));
cd80958d 595
cac3069d 596 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 597}
1df4dedd
DG
598
599/*
38ee087f 600 * Stop tracing for all traces of the session.
f3ed775e 601 */
38ee087f 602static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 603{
38ee087f 604 int ret, data_ret;
cd80958d
DG
605 struct lttcomm_session_msg lsm;
606
6a4f824d 607 if (session_name == NULL) {
2f70b271 608 return -LTTNG_ERR_INVALID;
6a4f824d
DG
609 }
610
53efb85a 611 memset(&lsm, 0, sizeof(lsm));
cd80958d 612 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 613
cac3069d
DG
614 lttng_ctl_copy_string(lsm.session.name, session_name,
615 sizeof(lsm.session.name));
cd80958d 616
cac3069d 617 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
618 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
619 goto error;
620 }
621
622 if (!wait) {
623 goto end;
624 }
625
38ee087f
DG
626 /* Check for data availability */
627 do {
6d805429 628 data_ret = lttng_data_pending(session_name);
38ee087f
DG
629 if (data_ret < 0) {
630 /* Return the data available call error. */
631 ret = data_ret;
632 goto error;
633 }
634
635 /*
9ae110e2
JG
636 * Data sleep time before retrying (in usec). Don't sleep if the
637 * call returned value indicates availability.
38ee087f 638 */
6d805429 639 if (data_ret) {
38ee087f 640 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
38ee087f 641 }
6d805429 642 } while (data_ret != 0);
38ee087f 643
38ee087f
DG
644end:
645error:
646 return ret;
647}
648
649/*
650 * Stop tracing and wait for data availability.
651 */
652int lttng_stop_tracing(const char *session_name)
653{
654 return _lttng_stop_tracing(session_name, 1);
655}
656
657/*
658 * Stop tracing but _don't_ wait for data availability.
659 */
660int lttng_stop_tracing_no_wait(const char *session_name)
661{
662 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
663}
664
665/*
601d5acf
DG
666 * Add context to a channel.
667 *
668 * If the given channel is NULL, add the contexts to all channels.
669 * The event_name param is ignored.
af87c45a
DG
670 *
671 * Returns the size of the returned payload data or a negative error code.
1df4dedd 672 */
cd80958d 673int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
674 struct lttng_event_context *ctx, const char *event_name,
675 const char *channel_name)
d65106b1 676{
2001793c
JG
677 int ret;
678 size_t len = 0;
679 char *buf = NULL;
cd80958d
DG
680 struct lttcomm_session_msg lsm;
681
9ae110e2 682 /* Safety check. Both are mandatory. */
9d697d3d 683 if (handle == NULL || ctx == NULL) {
2001793c
JG
684 ret = -LTTNG_ERR_INVALID;
685 goto end;
cd80958d
DG
686 }
687
441c16a7 688 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
689 lsm.cmd_type = LTTNG_ADD_CONTEXT;
690
85076754
MD
691 /* If no channel name, send empty string. */
692 if (channel_name == NULL) {
693 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
694 sizeof(lsm.u.context.channel_name));
695 } else {
696 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
697 sizeof(lsm.u.context.channel_name));
698 }
cd80958d 699
cac3069d 700 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cac3069d 701 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
702 sizeof(lsm.session.name));
703
2001793c
JG
704 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
705 size_t provider_len, ctx_len;
706 const char *provider_name = ctx->u.app_ctx.provider_name;
707 const char *ctx_name = ctx->u.app_ctx.ctx_name;
708
709 if (!provider_name || !ctx_name) {
710 ret = -LTTNG_ERR_INVALID;
711 goto end;
712 }
713
714 provider_len = strlen(provider_name);
715 if (provider_len == 0) {
716 ret = -LTTNG_ERR_INVALID;
717 goto end;
718 }
719 lsm.u.context.provider_name_len = provider_len;
720
721 ctx_len = strlen(ctx_name);
722 if (ctx_len == 0) {
723 ret = -LTTNG_ERR_INVALID;
724 goto end;
725 }
726 lsm.u.context.context_name_len = ctx_len;
727
728 len = provider_len + ctx_len;
729 buf = zmalloc(len);
730 if (!buf) {
731 ret = -LTTNG_ERR_NOMEM;
732 goto end;
733 }
734
735 memcpy(buf, provider_name, provider_len);
736 memcpy(buf + provider_len, ctx_name, ctx_len);
737 }
738 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
739 /* Don't leak application addresses to the sessiond. */
740 lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
741 lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
742
795a978d 743 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
2001793c
JG
744end:
745 free(buf);
746 return ret;
d65106b1
DG
747}
748
f3ed775e 749/*
9ae110e2
JG
750 * Enable event(s) for a channel.
751 *
752 * If no event name is specified, all events are enabled.
753 * If no channel name is specified, the default 'channel0' is used.
754 *
755 * Returns size of returned session payload data or a negative error code.
f3ed775e 756 */
cd80958d 757int lttng_enable_event(struct lttng_handle *handle,
38057ed1 758 struct lttng_event *ev, const char *channel_name)
1df4dedd 759{
f8a96544
JI
760 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
761 NULL, 0, NULL);
1df4dedd
DG
762}
763
53a80697 764/*
025faf73 765 * Create or enable an event with a filter expression.
178191b3 766 *
53a80697
MD
767 * Return negative error value on error.
768 * Return size of returned session payload data if OK.
769 */
025faf73 770int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 771 struct lttng_event *event, const char *channel_name,
53a80697
MD
772 const char *filter_expression)
773{
f8a96544
JI
774 return lttng_enable_event_with_exclusions(handle, event, channel_name,
775 filter_expression, 0, NULL);
53a80697
MD
776}
777
347c5ab5 778/*
0e115563 779 * Depending on the event, return a newly allocated agent filter expression or
347c5ab5
DG
780 * NULL if not applicable.
781 *
782 * An event with NO loglevel and the name is * will return NULL.
783 */
0e115563 784static char *set_agent_filter(const char *filter, struct lttng_event *ev)
347c5ab5
DG
785{
786 int err;
0e115563 787 char *agent_filter = NULL;
347c5ab5
DG
788
789 assert(ev);
790
791 /* Don't add filter for the '*' event. */
792 if (ev->name[0] != '*') {
793 if (filter) {
0e115563 794 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
347c5ab5
DG
795 ev->name);
796 } else {
0e115563 797 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
347c5ab5
DG
798 }
799 if (err < 0) {
800 PERROR("asprintf");
6a556f7b 801 goto error;
347c5ab5
DG
802 }
803 }
804
805 /* Add loglevel filtering if any for the JUL domain. */
806 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
807 char *op;
808
809 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
810 op = ">=";
811 } else {
812 op = "==";
813 }
814
0e115563 815 if (filter || agent_filter) {
6a556f7b
JG
816 char *new_filter;
817
fb0edb23 818 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
0e115563 819 agent_filter ? agent_filter : filter, op,
347c5ab5 820 ev->loglevel);
0e115563
DG
821 if (agent_filter) {
822 free(agent_filter);
6a556f7b 823 }
0e115563 824 agent_filter = new_filter;
347c5ab5 825 } else {
0e115563 826 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
347c5ab5
DG
827 ev->loglevel);
828 }
829 if (err < 0) {
830 PERROR("asprintf");
6a556f7b 831 goto error;
347c5ab5
DG
832 }
833 }
834
0e115563 835 return agent_filter;
6a556f7b 836error:
0e115563 837 free(agent_filter);
6a556f7b 838 return NULL;
347c5ab5
DG
839}
840
137b9942 841/*
ec166985 842 * Generate the filter bytecode from a given filter expression string. Put the
137b9942
DG
843 * newly allocated parser context in ctxp and populate the lsm object with the
844 * expression len.
845 *
846 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
847 */
848static int generate_filter(char *filter_expression,
849 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
850{
851 int ret;
852 struct filter_parser_ctx *ctx = NULL;
853 FILE *fmem = NULL;
854
855 assert(filter_expression);
856 assert(lsm);
857 assert(ctxp);
858
859 /*
860 * Casting const to non-const, as the underlying function will use it in
861 * read-only mode.
862 */
863 fmem = lttng_fmemopen((void *) filter_expression,
864 strlen(filter_expression), "r");
865 if (!fmem) {
866 fprintf(stderr, "Error opening memory as stream\n");
867 ret = -LTTNG_ERR_FILTER_NOMEM;
868 goto error;
869 }
870 ctx = filter_parser_ctx_alloc(fmem);
871 if (!ctx) {
872 fprintf(stderr, "Error allocating parser\n");
873 ret = -LTTNG_ERR_FILTER_NOMEM;
874 goto filter_alloc_error;
875 }
876 ret = filter_parser_ctx_append_ast(ctx);
877 if (ret) {
878 fprintf(stderr, "Parse error\n");
879 ret = -LTTNG_ERR_FILTER_INVAL;
880 goto parse_error;
881 }
882 ret = filter_visitor_set_parent(ctx);
883 if (ret) {
884 fprintf(stderr, "Set parent error\n");
885 ret = -LTTNG_ERR_FILTER_INVAL;
886 goto parse_error;
887 }
888 if (print_xml) {
889 ret = filter_visitor_print_xml(ctx, stdout, 0);
890 if (ret) {
891 fflush(stdout);
892 fprintf(stderr, "XML print error\n");
893 ret = -LTTNG_ERR_FILTER_INVAL;
894 goto parse_error;
895 }
896 }
897
898 dbg_printf("Generating IR... ");
899 fflush(stdout);
900 ret = filter_visitor_ir_generate(ctx);
901 if (ret) {
902 fprintf(stderr, "Generate IR error\n");
903 ret = -LTTNG_ERR_FILTER_INVAL;
904 goto parse_error;
905 }
906 dbg_printf("done\n");
907
908 dbg_printf("Validating IR... ");
909 fflush(stdout);
910 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
911 if (ret) {
912 ret = -LTTNG_ERR_FILTER_INVAL;
913 goto parse_error;
914 }
9ae110e2 915 /* Validate strings used as literals in the expression. */
dcd5daf2
JG
916 ret = filter_visitor_ir_validate_string(ctx);
917 if (ret) {
918 ret = -LTTNG_ERR_FILTER_INVAL;
919 goto parse_error;
920 }
137b9942
DG
921 dbg_printf("done\n");
922
923 dbg_printf("Generating bytecode... ");
924 fflush(stdout);
925 ret = filter_visitor_bytecode_generate(ctx);
926 if (ret) {
927 fprintf(stderr, "Generate bytecode error\n");
928 ret = -LTTNG_ERR_FILTER_INVAL;
929 goto parse_error;
930 }
931 dbg_printf("done\n");
932 dbg_printf("Size of bytecode generated: %u bytes.\n",
933 bytecode_get_len(&ctx->bytecode->b));
934
935 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
936 + bytecode_get_len(&ctx->bytecode->b);
937 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
938
939 /* No need to keep the memory stream. */
940 if (fclose(fmem) != 0) {
6f04ed72 941 PERROR("fclose");
137b9942
DG
942 }
943
944 *ctxp = ctx;
945 return 0;
946
947parse_error:
137b9942
DG
948 filter_ir_free(ctx);
949 filter_parser_ctx_free(ctx);
950filter_alloc_error:
951 if (fclose(fmem) != 0) {
6f04ed72 952 PERROR("fclose");
137b9942
DG
953 }
954error:
955 return ret;
956}
957
93deb080
JI
958/*
959 * Enable event(s) for a channel, possibly with exclusions and a filter.
960 * If no event name is specified, all events are enabled.
961 * If no channel name is specified, the default name is used.
962 * If filter expression is not NULL, the filter is set for the event.
963 * If exclusion count is not zero, the exclusions are set for the event.
964 * Returns size of returned session payload data or a negative error code.
965 */
966int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
967 struct lttng_event *ev, const char *channel_name,
e9efbcd3 968 const char *original_filter_expression,
93deb080
JI
969 int exclusion_count, char **exclusion_list)
970{
971 struct lttcomm_session_msg lsm;
64226865 972 char *varlen_data;
93deb080 973 int ret = 0;
137b9942 974 unsigned int free_filter_expression = 0;
93deb080 975 struct filter_parser_ctx *ctx = NULL;
e9efbcd3
JG
976 /*
977 * Cast as non-const since we may replace the filter expression
978 * by a dynamically allocated string. Otherwise, the original
979 * string is not modified.
980 */
981 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
982
983 if (handle == NULL || ev == NULL) {
137b9942
DG
984 ret = -LTTNG_ERR_INVALID;
985 goto error;
93deb080
JI
986 }
987
9ae110e2
JG
988 /*
989 * Empty filter string will always be rejected by the parser
93deb080
JI
990 * anyway, so treat this corner-case early to eliminate
991 * lttng_fmemopen error for 0-byte allocation.
992 */
993 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
994 ret = -LTTNG_ERR_INVALID;
995 goto error;
93deb080
JI
996 }
997
998 memset(&lsm, 0, sizeof(lsm));
999
1000 /* If no channel name, send empty string. */
1001 if (channel_name == NULL) {
1002 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1003 sizeof(lsm.u.enable.channel_name));
1004 } else {
1005 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1006 sizeof(lsm.u.enable.channel_name));
1007 }
1008
18a720cd
MD
1009 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1010 if (ev->name[0] == '\0') {
1011 /* Enable all events */
1012 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
6565421f 1013 }
93deb080 1014
6565421f 1015 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
6e911cad 1016 /* FIXME: copying non-packed struct to packed struct. */
93deb080
JI
1017 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1018
1019 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1020 sizeof(lsm.session.name));
1021 lsm.u.enable.exclusion_count = exclusion_count;
1022 lsm.u.enable.bytecode_len = 0;
1023
9b21e6d5
DG
1024 /*
1025 * For the JUL domain, a filter is enforced except for the enable all
1026 * event. This is done to avoid having the event in all sessions thus
1027 * filtering by logger name.
1028 */
1029 if (exclusion_count == 0 && filter_expression == NULL &&
5cdb6027 1030 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1031 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1032 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
3e1c9ff7 1033 goto ask_sessiond;
93deb080
JI
1034 }
1035
1036 /*
1037 * We have either a filter or some exclusions, so we need to set up
9ae110e2 1038 * a variable-length memory block from where to send the data.
93deb080
JI
1039 */
1040
9ae110e2 1041 /* Parse filter expression. */
5cdb6027 1042 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1043 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1044 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 1045 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1046 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1047 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1048 char *agent_filter;
64226865 1049
347c5ab5 1050 /* Setup JUL filter if needed. */
0e115563
DG
1051 agent_filter = set_agent_filter(filter_expression, ev);
1052 if (!agent_filter) {
137b9942 1053 if (!filter_expression) {
9ae110e2
JG
1054 /*
1055 * No JUL and no filter, just skip
1056 * everything below.
1057 */
137b9942
DG
1058 goto ask_sessiond;
1059 }
64226865
DG
1060 } else {
1061 /*
9ae110e2
JG
1062 * With an agent filter, the original filter has
1063 * been added to it thus replace the filter
1064 * expression.
64226865 1065 */
0e115563 1066 filter_expression = agent_filter;
e9efbcd3 1067 free_filter_expression = 1;
9b21e6d5 1068 }
9b21e6d5 1069 }
93deb080 1070
137b9942 1071 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 1072 if (ret) {
137b9942 1073 goto filter_error;
93deb080 1074 }
6b453b5e
JG
1075 }
1076
1077 varlen_data = zmalloc(lsm.u.enable.bytecode_len
137b9942
DG
1078 + lsm.u.enable.expression_len
1079 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
6b453b5e
JG
1080 if (!varlen_data) {
1081 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
7ca1dc6f 1082 goto mem_error;
6b453b5e 1083 }
137b9942 1084
9ae110e2 1085 /* Put exclusion names first in the data. */
6b453b5e
JG
1086 while (exclusion_count--) {
1087 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
0c82ac62
PP
1088 *(exclusion_list + exclusion_count),
1089 LTTNG_SYMBOL_NAME_LEN - 1);
6b453b5e 1090 }
9ae110e2 1091 /* Add filter expression next. */
6b453b5e
JG
1092 if (lsm.u.enable.expression_len != 0) {
1093 memcpy(varlen_data
1094 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
1095 filter_expression,
1096 lsm.u.enable.expression_len);
1097 }
9ae110e2 1098 /* Add filter bytecode next. */
137b9942 1099 if (ctx && lsm.u.enable.bytecode_len != 0) {
6b453b5e
JG
1100 memcpy(varlen_data
1101 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
1102 + lsm.u.enable.expression_len,
1103 &ctx->bytecode->b,
1104 lsm.u.enable.bytecode_len);
93deb080
JI
1105 }
1106
795a978d 1107 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
67676bd8 1108 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
9ae110e2
JG
1109 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
1110 NULL);
6b453b5e 1111 free(varlen_data);
93deb080 1112
7ca1dc6f
DG
1113mem_error:
1114 if (filter_expression && ctx) {
93deb080
JI
1115 filter_bytecode_free(ctx);
1116 filter_ir_free(ctx);
1117 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1118 }
1119filter_error:
1120 if (free_filter_expression) {
1121 /*
9ae110e2
JG
1122 * The filter expression has been replaced and must be freed as
1123 * it is not the original filter expression received as a
1124 * parameter.
7ca1dc6f
DG
1125 */
1126 free(filter_expression);
93deb080 1127 }
137b9942
DG
1128error:
1129 /*
9ae110e2
JG
1130 * Return directly to the caller and don't ask the sessiond since
1131 * something went wrong in the parsing of data above.
137b9942 1132 */
93deb080 1133 return ret;
3e1c9ff7
DG
1134
1135ask_sessiond:
1136 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1137 return ret;
93deb080
JI
1138}
1139
6e911cad
MD
1140int lttng_disable_event_ext(struct lttng_handle *handle,
1141 struct lttng_event *ev, const char *channel_name,
1142 const char *original_filter_expression)
1df4dedd 1143{
cd80958d 1144 struct lttcomm_session_msg lsm;
6e911cad
MD
1145 char *varlen_data;
1146 int ret = 0;
1147 unsigned int free_filter_expression = 0;
1148 struct filter_parser_ctx *ctx = NULL;
1149 /*
1150 * Cast as non-const since we may replace the filter expression
1151 * by a dynamically allocated string. Otherwise, the original
1152 * string is not modified.
1153 */
1154 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1155
6e911cad
MD
1156 if (handle == NULL || ev == NULL) {
1157 ret = -LTTNG_ERR_INVALID;
1158 goto error;
1159 }
1160
9ae110e2
JG
1161 /*
1162 * Empty filter string will always be rejected by the parser
6e911cad
MD
1163 * anyway, so treat this corner-case early to eliminate
1164 * lttng_fmemopen error for 0-byte allocation.
1165 */
1166 if (filter_expression && filter_expression[0] == '\0') {
1167 ret = -LTTNG_ERR_INVALID;
1168 goto error;
cd80958d
DG
1169 }
1170
441c16a7
MD
1171 memset(&lsm, 0, sizeof(lsm));
1172
85076754
MD
1173 /* If no channel name, send empty string. */
1174 if (channel_name == NULL) {
1175 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1176 sizeof(lsm.u.disable.channel_name));
f3ed775e 1177 } else {
85076754 1178 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1179 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1180 }
1181
18a720cd 1182 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1183
6e911cad
MD
1184 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1185 /* FIXME: copying non-packed struct to packed struct. */
1186 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1187
cac3069d 1188 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1189 sizeof(lsm.session.name));
6e911cad 1190 lsm.u.disable.bytecode_len = 0;
cd80958d 1191
6e911cad
MD
1192 /*
1193 * For the JUL domain, a filter is enforced except for the
1194 * disable all event. This is done to avoid having the event in
1195 * all sessions thus filtering by logger name.
1196 */
1197 if (filter_expression == NULL &&
1198 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1199 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1200 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1201 goto ask_sessiond;
1202 }
1203
1204 /*
1205 * We have a filter, so we need to set up a variable-length
1206 * memory block from where to send the data.
1207 */
1208
1209 /* Parse filter expression */
1210 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1211 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1212 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1213 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1214 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1215 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1216 char *agent_filter;
6e911cad
MD
1217
1218 /* Setup JUL filter if needed. */
0e115563
DG
1219 agent_filter = set_agent_filter(filter_expression, ev);
1220 if (!agent_filter) {
6e911cad 1221 if (!filter_expression) {
9ae110e2
JG
1222 /*
1223 * No JUL and no filter, just skip
1224 * everything below.
1225 */
6e911cad
MD
1226 goto ask_sessiond;
1227 }
1228 } else {
1229 /*
9ae110e2
JG
1230 * With a JUL filter, the original filter has
1231 * been added to it thus replace the filter
1232 * expression.
6e911cad 1233 */
0e115563 1234 filter_expression = agent_filter;
6e911cad
MD
1235 free_filter_expression = 1;
1236 }
1237 }
1238
1239 ret = generate_filter(filter_expression, &lsm, &ctx);
1240 if (ret) {
1241 goto filter_error;
1242 }
1243 }
1244
1245 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1246 + lsm.u.disable.expression_len);
1247 if (!varlen_data) {
1248 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1249 goto mem_error;
1250 }
1251
9ae110e2 1252 /* Add filter expression. */
6e911cad
MD
1253 if (lsm.u.disable.expression_len != 0) {
1254 memcpy(varlen_data,
1255 filter_expression,
1256 lsm.u.disable.expression_len);
1257 }
9ae110e2 1258 /* Add filter bytecode next. */
6e911cad
MD
1259 if (ctx && lsm.u.disable.bytecode_len != 0) {
1260 memcpy(varlen_data
1261 + lsm.u.disable.expression_len,
1262 &ctx->bytecode->b,
1263 lsm.u.disable.bytecode_len);
1264 }
1265
795a978d 1266 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
6e911cad
MD
1267 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1268 free(varlen_data);
1269
1270mem_error:
1271 if (filter_expression && ctx) {
1272 filter_bytecode_free(ctx);
1273 filter_ir_free(ctx);
1274 filter_parser_ctx_free(ctx);
1275 }
1276filter_error:
1277 if (free_filter_expression) {
1278 /*
9ae110e2
JG
1279 * The filter expression has been replaced and must be freed as
1280 * it is not the original filter expression received as a
1281 * parameter.
6e911cad
MD
1282 */
1283 free(filter_expression);
1284 }
1285error:
1286 /*
9ae110e2
JG
1287 * Return directly to the caller and don't ask the sessiond since
1288 * something went wrong in the parsing of data above.
6e911cad
MD
1289 */
1290 return ret;
1291
1292ask_sessiond:
1293 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1294 return ret;
1295}
1296
1297/*
9ae110e2
JG
1298 * Disable event(s) of a channel and domain.
1299 * If no event name is specified, all events are disabled.
1300 * If no channel name is specified, the default 'channel0' is used.
1301 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1302 */
1303int lttng_disable_event(struct lttng_handle *handle, const char *name,
1304 const char *channel_name)
1305{
1306 struct lttng_event ev;
1307
1308 memset(&ev, 0, sizeof(ev));
9b7431cf 1309 ev.loglevel = -1;
6e911cad
MD
1310 ev.type = LTTNG_EVENT_ALL;
1311 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1312 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1df4dedd
DG
1313}
1314
1315/*
9ae110e2
JG
1316 * Enable channel per domain
1317 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1318 */
cd80958d 1319int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 1320 struct lttng_channel *chan)
a5c5a2bd 1321{
cd80958d
DG
1322 struct lttcomm_session_msg lsm;
1323
9ae110e2 1324 /* NULL arguments are forbidden. No default values. */
5117eeec 1325 if (handle == NULL || chan == NULL) {
2f70b271 1326 return -LTTNG_ERR_INVALID;
cd80958d
DG
1327 }
1328
441c16a7
MD
1329 memset(&lsm, 0, sizeof(lsm));
1330
5117eeec 1331 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 1332
cd80958d
DG
1333 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1334
cac3069d 1335 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1336
cac3069d 1337 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1338 sizeof(lsm.session.name));
1339
cac3069d 1340 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1341}
1df4dedd 1342
2ef84c95 1343/*
9ae110e2
JG
1344 * All tracing will be stopped for registered events of the channel.
1345 * Returns size of returned session payload data or a negative error code.
2ef84c95 1346 */
cd80958d 1347int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1348{
cd80958d
DG
1349 struct lttcomm_session_msg lsm;
1350
9ae110e2 1351 /* Safety check. Both are mandatory. */
9d697d3d 1352 if (handle == NULL || name == NULL) {
2f70b271 1353 return -LTTNG_ERR_INVALID;
cd80958d
DG
1354 }
1355
441c16a7
MD
1356 memset(&lsm, 0, sizeof(lsm));
1357
cd80958d 1358 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1359
cac3069d 1360 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1361 sizeof(lsm.u.disable.channel_name));
1362
cac3069d 1363 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1364
cac3069d 1365 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1366 sizeof(lsm.session.name));
1367
cac3069d 1368 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1369}
1370
ccf10263 1371/*
9ae110e2
JG
1372 * Add PID to session tracker.
1373 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1374 */
1375int lttng_track_pid(struct lttng_handle *handle, int pid)
1376{
1377 struct lttcomm_session_msg lsm;
1378
9ae110e2 1379 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1380 if (handle == NULL) {
1381 return -LTTNG_ERR_INVALID;
1382 }
1383
1384 memset(&lsm, 0, sizeof(lsm));
1385
1386 lsm.cmd_type = LTTNG_TRACK_PID;
1387 lsm.u.pid_tracker.pid = pid;
1388
1389 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1390
1391 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1392 sizeof(lsm.session.name));
1393
1394 return lttng_ctl_ask_sessiond(&lsm, NULL);
1395}
1396
1397/*
9ae110e2
JG
1398 * Remove PID from session tracker.
1399 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1400 */
1401int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1402{
1403 struct lttcomm_session_msg lsm;
1404
9ae110e2 1405 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1406 if (handle == NULL) {
1407 return -LTTNG_ERR_INVALID;
1408 }
1409
1410 memset(&lsm, 0, sizeof(lsm));
1411
1412 lsm.cmd_type = LTTNG_UNTRACK_PID;
1413 lsm.u.pid_tracker.pid = pid;
1414
1415 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1416
1417 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1418 sizeof(lsm.session.name));
1419
1420 return lttng_ctl_ask_sessiond(&lsm, NULL);
1421}
1422
fac6795d 1423/*
9ae110e2
JG
1424 * Lists all available tracepoints of domain.
1425 * Sets the contents of the events array.
1426 * Returns the number of lttng_event entries in events;
1427 * on error, returns a negative value.
fac6795d 1428 */
cd80958d 1429int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1430 struct lttng_event **events)
fac6795d 1431{
052da939 1432 int ret;
cd80958d
DG
1433 struct lttcomm_session_msg lsm;
1434
9d697d3d 1435 if (handle == NULL) {
2f70b271 1436 return -LTTNG_ERR_INVALID;
cd80958d 1437 }
fac6795d 1438
53efb85a 1439 memset(&lsm, 0, sizeof(lsm));
cd80958d 1440 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1441 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1442
cac3069d 1443 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1444 if (ret < 0) {
1445 return ret;
eb354453 1446 }
fac6795d 1447
9f19cc17 1448 return ret / sizeof(struct lttng_event);
fac6795d
DG
1449}
1450
f37d259d 1451/*
9ae110e2
JG
1452 * Lists all available tracepoint fields of domain.
1453 * Sets the contents of the event field array.
1454 * Returns the number of lttng_event_field entries in events;
1455 * on error, returns a negative value.
f37d259d
MD
1456 */
1457int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1458 struct lttng_event_field **fields)
1459{
1460 int ret;
1461 struct lttcomm_session_msg lsm;
1462
1463 if (handle == NULL) {
2f70b271 1464 return -LTTNG_ERR_INVALID;
f37d259d
MD
1465 }
1466
53efb85a 1467 memset(&lsm, 0, sizeof(lsm));
f37d259d 1468 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1469 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1470
cac3069d 1471 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1472 if (ret < 0) {
1473 return ret;
1474 }
1475
1476 return ret / sizeof(struct lttng_event_field);
1477}
1478
834978fd 1479/*
9ae110e2
JG
1480 * Lists all available kernel system calls. Allocates and sets the contents of
1481 * the events array.
834978fd 1482 *
9ae110e2
JG
1483 * Returns the number of lttng_event entries in events; on error, returns a
1484 * negative value.
834978fd
DG
1485 */
1486int lttng_list_syscalls(struct lttng_event **events)
1487{
1488 int ret;
1489 struct lttcomm_session_msg lsm;
1490
1491 if (!events) {
1492 return -LTTNG_ERR_INVALID;
1493 }
1494
1495 memset(&lsm, 0, sizeof(lsm));
1496 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1497 /* Force kernel domain for system calls. */
1498 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1499
1500 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1501 if (ret < 0) {
1502 return ret;
1503 }
1504
1505 return ret / sizeof(struct lttng_event);
1506}
1507
1657e9bb 1508/*
9ae110e2
JG
1509 * Returns a human readable string describing
1510 * the error code (a negative value).
1657e9bb 1511 */
9a745bc7 1512const char *lttng_strerror(int code)
1657e9bb 1513{
f73fabfd 1514 return error_get_str(code);
1657e9bb
DG
1515}
1516
aaf97519 1517/*
a4b92340
DG
1518 * Create a brand new session using name and url for destination.
1519 *
f73fabfd 1520 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1521 */
a4b92340 1522int lttng_create_session(const char *name, const char *url)
00e2e675 1523{
3dd05a85 1524 int ret;
a4b92340 1525 ssize_t size;
00e2e675 1526 struct lttcomm_session_msg lsm;
a4b92340 1527 struct lttng_uri *uris = NULL;
00e2e675 1528
a4b92340 1529 if (name == NULL) {
2f70b271 1530 return -LTTNG_ERR_INVALID;
00e2e675
DG
1531 }
1532
a4b92340 1533 memset(&lsm, 0, sizeof(lsm));
00e2e675 1534
a4b92340 1535 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1536 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1537
1538 /* There should never be a data URL */
bc894455 1539 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1540 if (size < 0) {
2f70b271 1541 return -LTTNG_ERR_INVALID;
00e2e675
DG
1542 }
1543
a4b92340
DG
1544 lsm.u.uri.size = size;
1545
795a978d 1546 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 1547 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1548
1549 free(uris);
1550 return ret;
00e2e675
DG
1551}
1552
8028d920 1553/*
9ae110e2
JG
1554 * Destroy session using name.
1555 * Returns size of returned session payload data or a negative error code.
8028d920 1556 */
e20ee7c2 1557int _lttng_destroy_session(const char *session_name)
8028d920 1558{
cd80958d
DG
1559 struct lttcomm_session_msg lsm;
1560
843f5df9 1561 if (session_name == NULL) {
2f70b271 1562 return -LTTNG_ERR_INVALID;
cd80958d
DG
1563 }
1564
53efb85a 1565 memset(&lsm, 0, sizeof(lsm));
cd80958d 1566 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1567
cac3069d
DG
1568 lttng_ctl_copy_string(lsm.session.name, session_name,
1569 sizeof(lsm.session.name));
cd80958d 1570
cac3069d 1571 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1572}
1573
e20ee7c2
JD
1574/*
1575 * Stop the session and wait for the data before destroying it
1576 */
1577int lttng_destroy_session(const char *session_name)
1578{
1579 int ret;
1580
1581 /*
1582 * Stop the tracing and wait for the data.
1583 */
1584 ret = _lttng_stop_tracing(session_name, 1);
1585 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1586 goto end;
1587 }
1588
1589 ret = _lttng_destroy_session(session_name);
1590end:
1591 return ret;
1592}
1593
1594/*
1595 * Destroy the session without waiting for the data.
1596 */
1597int lttng_destroy_session_no_wait(const char *session_name)
1598{
1599 int ret;
1600
1601 /*
1602 * Stop the tracing without waiting for the data.
1603 * The session might already have been stopped, so just
1604 * skip this error.
1605 */
1606 ret = _lttng_stop_tracing(session_name, 0);
1607 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1608 goto end;
1609 }
1610
1611 ret = _lttng_destroy_session(session_name);
1612end:
1613 return ret;
1614}
1615
57167058 1616/*
9ae110e2
JG
1617 * Ask the session daemon for all available sessions.
1618 * Sets the contents of the sessions array.
1619 * Returns the number of lttng_session entries in sessions;
1620 * on error, returns a negative value.
57167058 1621 */
ca95a216 1622int lttng_list_sessions(struct lttng_session **sessions)
57167058 1623{
ca95a216 1624 int ret;
cd80958d 1625 struct lttcomm_session_msg lsm;
57167058 1626
53efb85a 1627 memset(&lsm, 0, sizeof(lsm));
cd80958d 1628 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1629 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1630 if (ret < 0) {
ca95a216 1631 return ret;
57167058
DG
1632 }
1633
ca95a216 1634 return ret / sizeof(struct lttng_session);
57167058
DG
1635}
1636
d7ba1388
MD
1637int lttng_set_session_shm_path(const char *session_name,
1638 const char *shm_path)
1639{
1640 struct lttcomm_session_msg lsm;
1641
1642 if (session_name == NULL) {
1643 return -LTTNG_ERR_INVALID;
1644 }
1645
1646 memset(&lsm, 0, sizeof(lsm));
1647 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1648
1649 lttng_ctl_copy_string(lsm.session.name, session_name,
1650 sizeof(lsm.session.name));
1651 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1652 sizeof(lsm.u.set_shm_path.shm_path));
1653
1654 return lttng_ctl_ask_sessiond(&lsm, NULL);
1655}
1656
9f19cc17 1657/*
9ae110e2
JG
1658 * Ask the session daemon for all available domains of a session.
1659 * Sets the contents of the domains array.
1660 * Returns the number of lttng_domain entries in domains;
1661 * on error, returns a negative value.
9f19cc17 1662 */
330be774 1663int lttng_list_domains(const char *session_name,
cd80958d 1664 struct lttng_domain **domains)
9f19cc17
DG
1665{
1666 int ret;
cd80958d
DG
1667 struct lttcomm_session_msg lsm;
1668
330be774 1669 if (session_name == NULL) {
2f70b271 1670 return -LTTNG_ERR_INVALID;
cd80958d 1671 }
9f19cc17 1672
53efb85a 1673 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
1674 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1675
cac3069d
DG
1676 lttng_ctl_copy_string(lsm.session.name, session_name,
1677 sizeof(lsm.session.name));
cd80958d 1678
cac3069d 1679 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1680 if (ret < 0) {
1681 return ret;
1682 }
1683
1684 return ret / sizeof(struct lttng_domain);
1685}
1686
1687/*
9ae110e2
JG
1688 * Ask the session daemon for all available channels of a session.
1689 * Sets the contents of the channels array.
1690 * Returns the number of lttng_channel entries in channels;
1691 * on error, returns a negative value.
9f19cc17 1692 */
cd80958d
DG
1693int lttng_list_channels(struct lttng_handle *handle,
1694 struct lttng_channel **channels)
9f19cc17
DG
1695{
1696 int ret;
cd80958d
DG
1697 struct lttcomm_session_msg lsm;
1698
9d697d3d 1699 if (handle == NULL) {
2f70b271 1700 return -LTTNG_ERR_INVALID;
cd80958d
DG
1701 }
1702
53efb85a 1703 memset(&lsm, 0, sizeof(lsm));
cd80958d 1704 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1705 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1706 sizeof(lsm.session.name));
9f19cc17 1707
cac3069d 1708 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1709
cac3069d 1710 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1711 if (ret < 0) {
1712 return ret;
1713 }
1714
1715 return ret / sizeof(struct lttng_channel);
1716}
1717
1718/*
9ae110e2
JG
1719 * Ask the session daemon for all available events of a session channel.
1720 * Sets the contents of the events array.
1721 * Returns the number of lttng_event entries in events;
1722 * on error, returns a negative value.
9f19cc17 1723 */
cd80958d
DG
1724int lttng_list_events(struct lttng_handle *handle,
1725 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1726{
1727 int ret;
cd80958d 1728 struct lttcomm_session_msg lsm;
b4e3ceb9
PP
1729 struct lttcomm_event_command_header *cmd_header = NULL;
1730 size_t cmd_header_len;
1731 uint32_t nb_events, i;
1732 void *extended_at;
9f19cc17 1733
9d697d3d
DG
1734 /* Safety check. An handle and channel name are mandatory */
1735 if (handle == NULL || channel_name == NULL) {
2f70b271 1736 return -LTTNG_ERR_INVALID;
cd80958d
DG
1737 }
1738
53efb85a 1739 memset(&lsm, 0, sizeof(lsm));
cd80958d 1740 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1741 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1742 sizeof(lsm.session.name));
cac3069d 1743 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d 1744 sizeof(lsm.u.list.channel_name));
cac3069d 1745 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1746
b4e3ceb9
PP
1747 ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) events,
1748 (void **) &cmd_header, &cmd_header_len);
9f19cc17 1749 if (ret < 0) {
b4e3ceb9 1750 goto error;
9f19cc17
DG
1751 }
1752
b4e3ceb9
PP
1753 /* Set number of events and free command header */
1754 nb_events = cmd_header->nb_events;
1755 if (nb_events > INT_MAX) {
1756 ret = -EOVERFLOW;
1757 goto error;
1758 }
1759 ret = (int) nb_events;
1760 free(cmd_header);
1761 cmd_header = NULL;
1762
1763 /* Set extended info pointers */
1764 extended_at = ((void*) (*events)) +
1765 nb_events * sizeof(struct lttng_event);
1766
1767 for (i = 0; i < nb_events; i++) {
1768 struct lttcomm_event_extended_header *ext_header;
1769 struct lttng_event *event = &(*events)[i];
1770
1771 event->extended.ptr = extended_at;
1772 ext_header =
1773 (struct lttcomm_event_extended_header *) extended_at;
1774 extended_at += sizeof(*ext_header);
1775 extended_at += ext_header->filter_len;
1776 }
1777
1778 return ret;
1779error:
1780 free(cmd_header);
1781 free(*events);
1782 return ret;
9f19cc17
DG
1783}
1784
d31d3e8c
PP
1785int lttng_event_get_filter_string(struct lttng_event *event,
1786 const char **filter_string)
1787{
1788 int ret = 0;
1789 struct lttcomm_event_extended_header *ext_header;
1790
1791 if (!event || !filter_string) {
1792 ret = -LTTNG_ERR_INVALID;
1793 goto end;
1794 }
1795
1796 ext_header = event->extended.ptr;
1797
1798 if (!ext_header) {
1799 /*
1800 * This can happen since the lttng_event structure is
1801 * used for other tasks where this pointer is never set.
1802 */
1803 *filter_string = NULL;
1804 goto end;
1805 }
1806
1807 if (ext_header->filter_len) {
1808 *filter_string = ((const char *) (ext_header)) +
1809 sizeof(*ext_header);
1810 } else {
1811 *filter_string = NULL;
1812 }
1813
1814end:
1815 return ret;
1816}
b4e3ceb9 1817
fac6795d 1818/*
1c8d13c8
TD
1819 * Sets the tracing_group variable with name.
1820 * This function allocates memory pointed to by tracing_group.
1821 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1822 */
1823int lttng_set_tracing_group(const char *name)
1824{
9d697d3d 1825 if (name == NULL) {
2f70b271 1826 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1827 }
1828
fac6795d 1829 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1830 return -LTTNG_ERR_FATAL;
fac6795d
DG
1831 }
1832
1833 return 0;
1834}
1835
d0254c7c 1836/*
af87c45a 1837 * Returns size of returned session payload data or a negative error code.
d0254c7c 1838 */
cd80958d 1839int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1840 struct lttng_calibrate *calibrate)
1841{
cd80958d 1842 struct lttcomm_session_msg lsm;
d0254c7c 1843
9d697d3d
DG
1844 /* Safety check. NULL pointer are forbidden */
1845 if (handle == NULL || calibrate == NULL) {
2f70b271 1846 return -LTTNG_ERR_INVALID;
cd80958d 1847 }
d0254c7c 1848
53efb85a 1849 memset(&lsm, 0, sizeof(lsm));
cd80958d 1850 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1851 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1852
cd80958d
DG
1853 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1854
cac3069d 1855 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1856}
1857
5edd7e09
DG
1858/*
1859 * Set default channel attributes.
441c16a7 1860 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1861 */
1862void lttng_channel_set_default_attr(struct lttng_domain *domain,
1863 struct lttng_channel_attr *attr)
1864{
1865 /* Safety check */
1866 if (attr == NULL || domain == NULL) {
1867 return;
1868 }
1869
eacaa7a6
DS
1870 memset(attr, 0, sizeof(struct lttng_channel_attr));
1871
0a9c6494
DG
1872 /* Same for all domains. */
1873 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1874 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1875 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1876
5edd7e09
DG
1877 switch (domain->type) {
1878 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
1879 attr->switch_timer_interval =
1880 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 1881 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1882 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1883 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1884 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1885 break;
1886 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1887 switch (domain->buf_type) {
1888 case LTTNG_BUFFER_PER_UID:
1889 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1890 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1891 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
1892 attr->switch_timer_interval =
1893 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1894 attr->read_timer_interval =
1895 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
0a9c6494
DG
1896 break;
1897 case LTTNG_BUFFER_PER_PID:
1898 default:
1899 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1900 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1901 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
1902 attr->switch_timer_interval =
1903 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1904 attr->read_timer_interval =
1905 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
0a9c6494
DG
1906 break;
1907 }
5edd7e09 1908 default:
441c16a7 1909 /* Default behavior: leave set to 0. */
5edd7e09
DG
1910 break;
1911 }
1912}
1913
fac6795d 1914/*
2269e89e 1915 * Check if session daemon is alive.
fac6795d 1916 *
2269e89e 1917 * Return 1 if alive or 0 if not.
1c8d13c8 1918 * On error returns a negative value.
fac6795d 1919 */
947308c4 1920int lttng_session_daemon_alive(void)
fac6795d
DG
1921{
1922 int ret;
1923
1924 ret = set_session_daemon_path();
1925 if (ret < 0) {
9ae110e2 1926 /* Error. */
fac6795d
DG
1927 return ret;
1928 }
1929
9d035200 1930 if (*sessiond_sock_path == '\0') {
2f70b271 1931 /*
9ae110e2
JG
1932 * No socket path set. Weird error which means the constructor
1933 * was not called.
2f70b271
DG
1934 */
1935 assert(0);
fac6795d
DG
1936 }
1937
2269e89e 1938 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 1939 if (ret < 0) {
9ae110e2 1940 /* Not alive. */
7d8234d9
MD
1941 return 0;
1942 }
7d8234d9 1943
9ae110e2 1944 /* Is alive. */
947308c4 1945 return 1;
fac6795d
DG
1946}
1947
00e2e675 1948/*
a4b92340 1949 * Set URL for a consumer for a session and domain.
00e2e675
DG
1950 *
1951 * Return 0 on success, else a negative value.
1952 */
a4b92340
DG
1953int lttng_set_consumer_url(struct lttng_handle *handle,
1954 const char *control_url, const char *data_url)
00e2e675 1955{
3dd05a85 1956 int ret;
a4b92340 1957 ssize_t size;
00e2e675 1958 struct lttcomm_session_msg lsm;
a4b92340 1959 struct lttng_uri *uris = NULL;
00e2e675 1960
a4b92340 1961 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1962 return -LTTNG_ERR_INVALID;
00e2e675
DG
1963 }
1964
a4b92340
DG
1965 memset(&lsm, 0, sizeof(lsm));
1966
00e2e675
DG
1967 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1968
cac3069d 1969 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1970 sizeof(lsm.session.name));
cac3069d 1971 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1972
bc894455 1973 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1974 if (size < 0) {
2f70b271 1975 return -LTTNG_ERR_INVALID;
a4b92340
DG
1976 }
1977
1978 lsm.u.uri.size = size;
00e2e675 1979
795a978d 1980 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 1981 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1982
1983 free(uris);
1984 return ret;
00e2e675
DG
1985}
1986
1987/*
9c6bda17 1988 * [OBSOLETE]
00e2e675
DG
1989 */
1990int lttng_enable_consumer(struct lttng_handle *handle)
1991{
785d2d0d 1992 return -ENOSYS;
00e2e675
DG
1993}
1994
1995/*
9c6bda17 1996 * [OBSOLETE]
00e2e675
DG
1997 */
1998int lttng_disable_consumer(struct lttng_handle *handle)
1999{
785d2d0d 2000 return -ENOSYS;
00e2e675
DG
2001}
2002
07424f16
DG
2003/*
2004 * This is an extension of create session that is ONLY and SHOULD only be used
2005 * by the lttng command line program. It exists to avoid using URI parsing in
2006 * the lttng client.
2007 *
2008 * We need the date and time for the trace path subdirectory for the case where
2009 * the user does NOT define one using either -o or -U. Using the normal
2010 * lttng_create_session API call, we have no clue on the session daemon side if
2011 * the URL was generated automatically by the client or define by the user.
2012 *
2013 * So this function "wrapper" is hidden from the public API, takes the datetime
2014 * string and appends it if necessary to the URI subdirectory before sending it
2015 * to the session daemon.
2016 *
2017 * With this extra function, the lttng_create_session call behavior is not
2018 * changed and the timestamp is appended to the URI on the session daemon side
2019 * if necessary.
2020 */
2021int _lttng_create_session_ext(const char *name, const char *url,
2022 const char *datetime)
2023{
3dd05a85 2024 int ret;
07424f16
DG
2025 ssize_t size;
2026 struct lttcomm_session_msg lsm;
2027 struct lttng_uri *uris = NULL;
2028
2bba9e53 2029 if (name == NULL || datetime == NULL) {
2f70b271 2030 return -LTTNG_ERR_INVALID;
07424f16
DG
2031 }
2032
2033 memset(&lsm, 0, sizeof(lsm));
2034
2035 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 2036 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16 2037
9ae110e2 2038 /* There should never be a data URL. */
bc894455 2039 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 2040 if (size < 0) {
3dd05a85
DG
2041 ret = -LTTNG_ERR_INVALID;
2042 goto error;
07424f16
DG
2043 }
2044
2045 lsm.u.uri.size = size;
2046
4b35a6b3 2047 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
2048 /* Don't append datetime if the name was automatically created. */
2049 if (strncmp(name, DEFAULT_SESSION_NAME "-",
2050 strlen(DEFAULT_SESSION_NAME) + 1)) {
2051 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
2052 name, datetime);
2053 } else {
2054 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2055 }
07424f16
DG
2056 if (ret < 0) {
2057 PERROR("snprintf uri subdir");
3dd05a85
DG
2058 ret = -LTTNG_ERR_FATAL;
2059 goto error;
07424f16
DG
2060 }
2061 }
2062
795a978d 2063 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2064 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2065
2066error:
2067 free(uris);
2068 return ret;
07424f16
DG
2069}
2070
806e2684
DG
2071/*
2072 * For a given session name, this call checks if the data is ready to be read
2073 * or is still being extracted by the consumer(s) hence not ready to be used by
2074 * any readers.
2075 */
6d805429 2076int lttng_data_pending(const char *session_name)
806e2684
DG
2077{
2078 int ret;
2079 struct lttcomm_session_msg lsm;
f6151c55 2080 uint8_t *pending = NULL;
806e2684
DG
2081
2082 if (session_name == NULL) {
2083 return -LTTNG_ERR_INVALID;
2084 }
2085
53efb85a 2086 memset(&lsm, 0, sizeof(lsm));
6d805429 2087 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2088
cac3069d
DG
2089 lttng_ctl_copy_string(lsm.session.name, session_name,
2090 sizeof(lsm.session.name));
806e2684 2091
f6151c55
JG
2092 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2093 if (ret < 0) {
2094 goto end;
2095 } else if (ret != 1) {
2096 /* Unexpected payload size */
2097 ret = -LTTNG_ERR_INVALID;
2098 goto end;
806e2684
DG
2099 }
2100
f6151c55
JG
2101 ret = (int) *pending;
2102end:
2103 free(pending);
806e2684
DG
2104 return ret;
2105}
2106
27babd3a
DG
2107/*
2108 * Create a session exclusively used for snapshot.
2109 *
2110 * Returns LTTNG_OK on success or a negative error code.
2111 */
2112int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2113{
2114 int ret;
2115 ssize_t size;
2116 struct lttcomm_session_msg lsm;
2117 struct lttng_uri *uris = NULL;
2118
2119 if (name == NULL) {
2120 return -LTTNG_ERR_INVALID;
2121 }
2122
2123 memset(&lsm, 0, sizeof(lsm));
2124
2125 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
2126 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2127
2128 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2129 if (size < 0) {
2130 return -LTTNG_ERR_INVALID;
2131 }
2132
2133 lsm.u.uri.size = size;
2134
795a978d 2135 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
27babd3a
DG
2136 sizeof(struct lttng_uri) * size, NULL);
2137
2138 free(uris);
2139 return ret;
2140}
2141
ecc48a90
JD
2142/*
2143 * Create a session exclusively used for live.
2144 *
2145 * Returns LTTNG_OK on success or a negative error code.
2146 */
2147int lttng_create_session_live(const char *name, const char *url,
2148 unsigned int timer_interval)
2149{
2150 int ret;
2151 ssize_t size;
2152 struct lttcomm_session_msg lsm;
2153 struct lttng_uri *uris = NULL;
2154
92805ee4 2155 if (name == NULL || timer_interval == 0) {
ecc48a90
JD
2156 return -LTTNG_ERR_INVALID;
2157 }
2158
2159 memset(&lsm, 0, sizeof(lsm));
2160
2161 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
2162 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2163
1a241656
DG
2164 if (url) {
2165 size = uri_parse_str_urls(url, NULL, &uris);
2166 if (size <= 0) {
2167 ret = -LTTNG_ERR_INVALID;
2168 goto end;
2169 }
ecc48a90 2170
1a241656
DG
2171 /* file:// is not accepted for live session. */
2172 if (uris[0].dtype == LTTNG_DST_PATH) {
2173 ret = -LTTNG_ERR_INVALID;
2174 goto end;
2175 }
2176 } else {
2177 size = 0;
ecc48a90
JD
2178 }
2179
2180 lsm.u.session_live.nb_uri = size;
2181 lsm.u.session_live.timer_interval = timer_interval;
2182
795a978d 2183 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
ecc48a90
JD
2184 sizeof(struct lttng_uri) * size, NULL);
2185
2186end:
2187 free(uris);
2188 return ret;
2189}
2190
a5dfbb9d
MD
2191/*
2192 * List PIDs in the tracker.
2193 *
1b18ce93
JG
2194 * enabled is set to whether the PID tracker is enabled.
2195 * pids is set to an allocated array of PIDs currently tracked. On
2196 * success, pids must be freed by the caller.
2197 * nr_pids is set to the number of entries contained by the pids array.
a5dfbb9d
MD
2198 *
2199 * Returns 0 on success, else a negative LTTng error code.
2200 */
2201int lttng_list_tracker_pids(struct lttng_handle *handle,
2202 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2203{
ebbf5ab7
JR
2204 int ret;
2205 int enabled = 1;
a5dfbb9d
MD
2206 struct lttcomm_session_msg lsm;
2207 size_t nr_pids;
2208 int32_t *pids;
2209
2210 if (handle == NULL) {
2211 return -LTTNG_ERR_INVALID;
2212 }
2213
2214 memset(&lsm, 0, sizeof(lsm));
2215 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2216 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2217 sizeof(lsm.session.name));
2218 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2219
2220 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2221 if (ret < 0) {
2222 return ret;
2223 }
2224 nr_pids = ret / sizeof(int32_t);
2225 if (nr_pids == 1 && pids[0] == -1) {
2226 free(pids);
2227 pids = NULL;
2228 enabled = 0;
2229 nr_pids = 0;
2230 }
2231 *_enabled = enabled;
2232 *_pids = pids;
2233 *_nr_pids = nr_pids;
2234 return 0;
2235}
2236
fac6795d 2237/*
9ae110e2 2238 * lib constructor.
fac6795d
DG
2239 */
2240static void __attribute__((constructor)) init()
2241{
2242 /* Set default session group */
bbccc3d2 2243 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 2244}
49cca668
DG
2245
2246/*
9ae110e2 2247 * lib destructor.
49cca668
DG
2248 */
2249static void __attribute__((destructor)) lttng_ctl_exit()
2250{
2251 free(tracing_group);
2252}
This page took 0.164811 seconds and 4 git commands to generate.