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