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