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