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