trackers: update liblttng-ctl
[deliverable/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. */
9f449915 800 if (strcmp(ev->name, "*") != 0) {
347c5ab5 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 }
9f449915
PP
923
924 /* Normalize globbing patterns in the expression. */
925 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
926 if (ret) {
927 ret = -LTTNG_ERR_FILTER_INVAL;
928 goto parse_error;
929 }
930
9ae110e2 931 /* Validate strings used as literals in the expression. */
dcd5daf2
JG
932 ret = filter_visitor_ir_validate_string(ctx);
933 if (ret) {
934 ret = -LTTNG_ERR_FILTER_INVAL;
935 goto parse_error;
936 }
9f449915
PP
937
938 /* Validate globbing patterns in the expression. */
939 ret = filter_visitor_ir_validate_globbing(ctx);
940 if (ret) {
941 ret = -LTTNG_ERR_FILTER_INVAL;
942 goto parse_error;
943 }
944
137b9942
DG
945 dbg_printf("done\n");
946
947 dbg_printf("Generating bytecode... ");
948 fflush(stdout);
949 ret = filter_visitor_bytecode_generate(ctx);
950 if (ret) {
951 fprintf(stderr, "Generate bytecode error\n");
952 ret = -LTTNG_ERR_FILTER_INVAL;
953 goto parse_error;
954 }
955 dbg_printf("done\n");
956 dbg_printf("Size of bytecode generated: %u bytes.\n",
957 bytecode_get_len(&ctx->bytecode->b));
958
959 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
960 + bytecode_get_len(&ctx->bytecode->b);
961 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
962
963 /* No need to keep the memory stream. */
964 if (fclose(fmem) != 0) {
6f04ed72 965 PERROR("fclose");
137b9942
DG
966 }
967
968 *ctxp = ctx;
969 return 0;
970
971parse_error:
137b9942
DG
972 filter_ir_free(ctx);
973 filter_parser_ctx_free(ctx);
974filter_alloc_error:
975 if (fclose(fmem) != 0) {
6f04ed72 976 PERROR("fclose");
137b9942
DG
977 }
978error:
979 return ret;
980}
981
93deb080
JI
982/*
983 * Enable event(s) for a channel, possibly with exclusions and a filter.
984 * If no event name is specified, all events are enabled.
985 * If no channel name is specified, the default name is used.
986 * If filter expression is not NULL, the filter is set for the event.
987 * If exclusion count is not zero, the exclusions are set for the event.
988 * Returns size of returned session payload data or a negative error code.
989 */
990int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
991 struct lttng_event *ev, const char *channel_name,
e9efbcd3 992 const char *original_filter_expression,
93deb080
JI
993 int exclusion_count, char **exclusion_list)
994{
995 struct lttcomm_session_msg lsm;
64226865 996 char *varlen_data;
93deb080 997 int ret = 0;
137b9942 998 unsigned int free_filter_expression = 0;
93deb080 999 struct filter_parser_ctx *ctx = NULL;
e9efbcd3
JG
1000 /*
1001 * Cast as non-const since we may replace the filter expression
1002 * by a dynamically allocated string. Otherwise, the original
1003 * string is not modified.
1004 */
1005 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
1006
1007 if (handle == NULL || ev == NULL) {
137b9942
DG
1008 ret = -LTTNG_ERR_INVALID;
1009 goto error;
93deb080
JI
1010 }
1011
9ae110e2
JG
1012 /*
1013 * Empty filter string will always be rejected by the parser
93deb080
JI
1014 * anyway, so treat this corner-case early to eliminate
1015 * lttng_fmemopen error for 0-byte allocation.
1016 */
1017 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
1018 ret = -LTTNG_ERR_INVALID;
1019 goto error;
93deb080
JI
1020 }
1021
1022 memset(&lsm, 0, sizeof(lsm));
1023
1024 /* If no channel name, send empty string. */
1025 if (channel_name == NULL) {
1026 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1027 sizeof(lsm.u.enable.channel_name));
1028 } else {
1029 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1030 sizeof(lsm.u.enable.channel_name));
1031 }
1032
18a720cd
MD
1033 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1034 if (ev->name[0] == '\0') {
1035 /* Enable all events */
1036 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
6565421f 1037 }
93deb080 1038
6565421f 1039 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
6e911cad 1040 /* FIXME: copying non-packed struct to packed struct. */
93deb080
JI
1041 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1042
1043 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1044 sizeof(lsm.session.name));
1045 lsm.u.enable.exclusion_count = exclusion_count;
1046 lsm.u.enable.bytecode_len = 0;
1047
9b21e6d5
DG
1048 /*
1049 * For the JUL domain, a filter is enforced except for the enable all
1050 * event. This is done to avoid having the event in all sessions thus
1051 * filtering by logger name.
1052 */
1053 if (exclusion_count == 0 && filter_expression == NULL &&
5cdb6027 1054 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1055 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1056 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
3e1c9ff7 1057 goto ask_sessiond;
93deb080
JI
1058 }
1059
1060 /*
1061 * We have either a filter or some exclusions, so we need to set up
9ae110e2 1062 * a variable-length memory block from where to send the data.
93deb080
JI
1063 */
1064
9ae110e2 1065 /* Parse filter expression. */
5cdb6027 1066 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1067 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1068 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 1069 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1070 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1071 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1072 char *agent_filter;
64226865 1073
347c5ab5 1074 /* Setup JUL filter if needed. */
0e115563
DG
1075 agent_filter = set_agent_filter(filter_expression, ev);
1076 if (!agent_filter) {
137b9942 1077 if (!filter_expression) {
9ae110e2
JG
1078 /*
1079 * No JUL and no filter, just skip
1080 * everything below.
1081 */
137b9942
DG
1082 goto ask_sessiond;
1083 }
64226865
DG
1084 } else {
1085 /*
9ae110e2
JG
1086 * With an agent filter, the original filter has
1087 * been added to it thus replace the filter
1088 * expression.
64226865 1089 */
0e115563 1090 filter_expression = agent_filter;
e9efbcd3 1091 free_filter_expression = 1;
9b21e6d5 1092 }
9b21e6d5 1093 }
93deb080 1094
137b9942 1095 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 1096 if (ret) {
137b9942 1097 goto filter_error;
93deb080 1098 }
6b453b5e
JG
1099 }
1100
1101 varlen_data = zmalloc(lsm.u.enable.bytecode_len
137b9942
DG
1102 + lsm.u.enable.expression_len
1103 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
6b453b5e
JG
1104 if (!varlen_data) {
1105 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
7ca1dc6f 1106 goto mem_error;
6b453b5e 1107 }
137b9942 1108
9ae110e2 1109 /* Put exclusion names first in the data. */
6b453b5e
JG
1110 while (exclusion_count--) {
1111 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
0c82ac62
PP
1112 *(exclusion_list + exclusion_count),
1113 LTTNG_SYMBOL_NAME_LEN - 1);
6b453b5e 1114 }
9ae110e2 1115 /* Add filter expression next. */
6b453b5e
JG
1116 if (lsm.u.enable.expression_len != 0) {
1117 memcpy(varlen_data
1118 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
1119 filter_expression,
1120 lsm.u.enable.expression_len);
1121 }
9ae110e2 1122 /* Add filter bytecode next. */
137b9942 1123 if (ctx && lsm.u.enable.bytecode_len != 0) {
6b453b5e
JG
1124 memcpy(varlen_data
1125 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
1126 + lsm.u.enable.expression_len,
1127 &ctx->bytecode->b,
1128 lsm.u.enable.bytecode_len);
93deb080
JI
1129 }
1130
795a978d 1131 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
67676bd8 1132 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
9ae110e2
JG
1133 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
1134 NULL);
6b453b5e 1135 free(varlen_data);
93deb080 1136
7ca1dc6f
DG
1137mem_error:
1138 if (filter_expression && ctx) {
93deb080
JI
1139 filter_bytecode_free(ctx);
1140 filter_ir_free(ctx);
1141 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1142 }
1143filter_error:
1144 if (free_filter_expression) {
1145 /*
9ae110e2
JG
1146 * The filter expression has been replaced and must be freed as
1147 * it is not the original filter expression received as a
1148 * parameter.
7ca1dc6f
DG
1149 */
1150 free(filter_expression);
93deb080 1151 }
137b9942
DG
1152error:
1153 /*
9ae110e2
JG
1154 * Return directly to the caller and don't ask the sessiond since
1155 * something went wrong in the parsing of data above.
137b9942 1156 */
93deb080 1157 return ret;
3e1c9ff7
DG
1158
1159ask_sessiond:
1160 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1161 return ret;
93deb080
JI
1162}
1163
6e911cad
MD
1164int lttng_disable_event_ext(struct lttng_handle *handle,
1165 struct lttng_event *ev, const char *channel_name,
1166 const char *original_filter_expression)
1df4dedd 1167{
cd80958d 1168 struct lttcomm_session_msg lsm;
6e911cad
MD
1169 char *varlen_data;
1170 int ret = 0;
1171 unsigned int free_filter_expression = 0;
1172 struct filter_parser_ctx *ctx = NULL;
1173 /*
1174 * Cast as non-const since we may replace the filter expression
1175 * by a dynamically allocated string. Otherwise, the original
1176 * string is not modified.
1177 */
1178 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1179
6e911cad
MD
1180 if (handle == NULL || ev == NULL) {
1181 ret = -LTTNG_ERR_INVALID;
1182 goto error;
1183 }
1184
9ae110e2
JG
1185 /*
1186 * Empty filter string will always be rejected by the parser
6e911cad
MD
1187 * anyway, so treat this corner-case early to eliminate
1188 * lttng_fmemopen error for 0-byte allocation.
1189 */
1190 if (filter_expression && filter_expression[0] == '\0') {
1191 ret = -LTTNG_ERR_INVALID;
1192 goto error;
cd80958d
DG
1193 }
1194
441c16a7
MD
1195 memset(&lsm, 0, sizeof(lsm));
1196
85076754
MD
1197 /* If no channel name, send empty string. */
1198 if (channel_name == NULL) {
1199 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1200 sizeof(lsm.u.disable.channel_name));
f3ed775e 1201 } else {
85076754 1202 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1203 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1204 }
1205
18a720cd 1206 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1207
6e911cad
MD
1208 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1209 /* FIXME: copying non-packed struct to packed struct. */
1210 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1211
cac3069d 1212 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1213 sizeof(lsm.session.name));
6e911cad 1214 lsm.u.disable.bytecode_len = 0;
cd80958d 1215
6e911cad
MD
1216 /*
1217 * For the JUL domain, a filter is enforced except for the
1218 * disable all event. This is done to avoid having the event in
1219 * all sessions thus filtering by logger name.
1220 */
1221 if (filter_expression == NULL &&
1222 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1223 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1224 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1225 goto ask_sessiond;
1226 }
1227
1228 /*
1229 * We have a filter, so we need to set up a variable-length
1230 * memory block from where to send the data.
1231 */
1232
1233 /* Parse filter expression */
1234 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1235 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1236 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1237 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1238 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1239 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1240 char *agent_filter;
6e911cad
MD
1241
1242 /* Setup JUL filter if needed. */
0e115563
DG
1243 agent_filter = set_agent_filter(filter_expression, ev);
1244 if (!agent_filter) {
6e911cad 1245 if (!filter_expression) {
9ae110e2
JG
1246 /*
1247 * No JUL and no filter, just skip
1248 * everything below.
1249 */
6e911cad
MD
1250 goto ask_sessiond;
1251 }
1252 } else {
1253 /*
9ae110e2
JG
1254 * With a JUL filter, the original filter has
1255 * been added to it thus replace the filter
1256 * expression.
6e911cad 1257 */
0e115563 1258 filter_expression = agent_filter;
6e911cad
MD
1259 free_filter_expression = 1;
1260 }
1261 }
1262
1263 ret = generate_filter(filter_expression, &lsm, &ctx);
1264 if (ret) {
1265 goto filter_error;
1266 }
1267 }
1268
1269 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1270 + lsm.u.disable.expression_len);
1271 if (!varlen_data) {
1272 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1273 goto mem_error;
1274 }
1275
9ae110e2 1276 /* Add filter expression. */
6e911cad
MD
1277 if (lsm.u.disable.expression_len != 0) {
1278 memcpy(varlen_data,
1279 filter_expression,
1280 lsm.u.disable.expression_len);
1281 }
9ae110e2 1282 /* Add filter bytecode next. */
6e911cad
MD
1283 if (ctx && lsm.u.disable.bytecode_len != 0) {
1284 memcpy(varlen_data
1285 + lsm.u.disable.expression_len,
1286 &ctx->bytecode->b,
1287 lsm.u.disable.bytecode_len);
1288 }
1289
795a978d 1290 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
6e911cad
MD
1291 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1292 free(varlen_data);
1293
1294mem_error:
1295 if (filter_expression && ctx) {
1296 filter_bytecode_free(ctx);
1297 filter_ir_free(ctx);
1298 filter_parser_ctx_free(ctx);
1299 }
1300filter_error:
1301 if (free_filter_expression) {
1302 /*
9ae110e2
JG
1303 * The filter expression has been replaced and must be freed as
1304 * it is not the original filter expression received as a
1305 * parameter.
6e911cad
MD
1306 */
1307 free(filter_expression);
1308 }
1309error:
1310 /*
9ae110e2
JG
1311 * Return directly to the caller and don't ask the sessiond since
1312 * something went wrong in the parsing of data above.
6e911cad
MD
1313 */
1314 return ret;
1315
1316ask_sessiond:
1317 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1318 return ret;
1319}
1320
1321/*
9ae110e2
JG
1322 * Disable event(s) of a channel and domain.
1323 * If no event name is specified, all events are disabled.
1324 * If no channel name is specified, the default 'channel0' is used.
1325 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1326 */
1327int lttng_disable_event(struct lttng_handle *handle, const char *name,
1328 const char *channel_name)
1329{
1330 struct lttng_event ev;
1331
1332 memset(&ev, 0, sizeof(ev));
9b7431cf 1333 ev.loglevel = -1;
6e911cad
MD
1334 ev.type = LTTNG_EVENT_ALL;
1335 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1336 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1df4dedd
DG
1337}
1338
cf0bcb51
JG
1339struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1340{
1341 struct lttng_channel *channel = NULL;
1342 struct lttng_channel_extended *extended = NULL;
1343
1344 if (!domain) {
1345 goto error;
1346 }
1347
1348 /* Validate domain. */
1349 switch (domain->type) {
1350 case LTTNG_DOMAIN_UST:
1351 switch (domain->buf_type) {
1352 case LTTNG_BUFFER_PER_UID:
1353 case LTTNG_BUFFER_PER_PID:
1354 break;
1355 default:
1356 goto error;
1357 }
1358 break;
1359 case LTTNG_DOMAIN_KERNEL:
1360 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1361 goto error;
1362 }
1363 break;
1364 default:
1365 goto error;
1366 }
1367
1368 channel = zmalloc(sizeof(*channel));
1369 if (!channel) {
1370 goto error;
1371 }
1372
1373 extended = zmalloc(sizeof(*extended));
1374 if (!extended) {
1375 goto error;
1376 }
1377
1378 channel->attr.extended.ptr = extended;
1379
1380 lttng_channel_set_default_attr(domain, &channel->attr);
1381 return channel;
1382error:
1383 free(channel);
1384 free(extended);
1385 return NULL;
1386}
1387
1388void lttng_channel_destroy(struct lttng_channel *channel)
1389{
1390 if (!channel) {
1391 return;
1392 }
1393
1394 if (channel->attr.extended.ptr) {
1395 free(channel->attr.extended.ptr);
1396 }
1397 free(channel);
1398}
1399
1df4dedd 1400/*
9ae110e2
JG
1401 * Enable channel per domain
1402 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1403 */
cd80958d 1404int lttng_enable_channel(struct lttng_handle *handle,
cf0bcb51 1405 struct lttng_channel *in_chan)
a5c5a2bd 1406{
cd80958d
DG
1407 struct lttcomm_session_msg lsm;
1408
9ae110e2 1409 /* NULL arguments are forbidden. No default values. */
cf0bcb51 1410 if (handle == NULL || in_chan == NULL) {
2f70b271 1411 return -LTTNG_ERR_INVALID;
cd80958d
DG
1412 }
1413
441c16a7 1414 memset(&lsm, 0, sizeof(lsm));
cf0bcb51
JG
1415 memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
1416 lsm.u.channel.chan.attr.extended.ptr = NULL;
441c16a7 1417
cf0bcb51
JG
1418 if (!in_chan->attr.extended.ptr) {
1419 struct lttng_channel *channel;
1420 struct lttng_channel_extended *extended;
7d29a247 1421
cf0bcb51
JG
1422 channel = lttng_channel_create(&handle->domain);
1423 if (!channel) {
1424 return -LTTNG_ERR_NOMEM;
1425 }
1426
1427 /*
1428 * Create a new channel in order to use default extended
1429 * attribute values.
1430 */
1431 extended = (struct lttng_channel_extended *)
1432 channel->attr.extended.ptr;
1433 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1434 lttng_channel_destroy(channel);
1435 } else {
1436 struct lttng_channel_extended *extended;
1437
1438 extended = (struct lttng_channel_extended *)
1439 in_chan->attr.extended.ptr;
1440 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1441 }
cd80958d 1442
cf0bcb51 1443 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
cac3069d 1444 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1445
cac3069d 1446 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1447 sizeof(lsm.session.name));
1448
cac3069d 1449 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1450}
1df4dedd 1451
2ef84c95 1452/*
9ae110e2
JG
1453 * All tracing will be stopped for registered events of the channel.
1454 * Returns size of returned session payload data or a negative error code.
2ef84c95 1455 */
cd80958d 1456int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1457{
cd80958d
DG
1458 struct lttcomm_session_msg lsm;
1459
9ae110e2 1460 /* Safety check. Both are mandatory. */
9d697d3d 1461 if (handle == NULL || name == NULL) {
2f70b271 1462 return -LTTNG_ERR_INVALID;
cd80958d
DG
1463 }
1464
441c16a7
MD
1465 memset(&lsm, 0, sizeof(lsm));
1466
cd80958d 1467 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1468
cac3069d 1469 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1470 sizeof(lsm.u.disable.channel_name));
1471
cac3069d 1472 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1473
cac3069d 1474 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1475 sizeof(lsm.session.name));
1476
cac3069d 1477 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1478}
1479
e32d22be
MD
1480static
1481int lttng_track_untrack_id(struct lttng_handle *handle,
1482 enum lttng_tracker_type tracker_type,
1483 struct lttng_tracker_id *id,
1484 enum lttcomm_sessiond_command cmd)
ccf10263
MD
1485{
1486 struct lttcomm_session_msg lsm;
e32d22be
MD
1487 char *var_data = NULL;
1488 size_t var_data_len = 0;
ccf10263 1489
9ae110e2 1490 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1491 if (handle == NULL) {
1492 return -LTTNG_ERR_INVALID;
1493 }
1494
1495 memset(&lsm, 0, sizeof(lsm));
1496
e32d22be
MD
1497 lsm.cmd_type = cmd;
1498 lsm.u.id_tracker.tracker_type = tracker_type;
1499 lsm.u.id_tracker.id_type = id->type;
1500 switch (id->type) {
1501 case LTTNG_ID_ALL:
1502 break;
1503 case LTTNG_ID_VALUE:
1504 lsm.u.id_tracker.u.value = id->value;
1505 break;
1506 case LTTNG_ID_STRING:
1507 var_data = id->string;
1508 var_data_len = strlen(var_data) + 1; /* Includes \0. */
1509 lsm.u.id_tracker.u.var_len = var_data_len;
1510 break;
1511 default:
1512 return -LTTNG_ERR_INVALID;
1513 }
ccf10263
MD
1514
1515 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1516
1517 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1518 sizeof(lsm.session.name));
1519
e32d22be
MD
1520 return lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm,
1521 var_data, var_data_len, NULL);
ccf10263
MD
1522}
1523
e32d22be 1524
ccf10263 1525/*
e32d22be 1526 * Add ID to session tracker.
9ae110e2 1527 * Return 0 on success else a negative LTTng error code.
ccf10263 1528 */
e32d22be
MD
1529int lttng_track_id(struct lttng_handle *handle,
1530 enum lttng_tracker_type tracker_type,
1531 struct lttng_tracker_id *id)
ccf10263 1532{
e32d22be
MD
1533 return lttng_track_untrack_id(handle, tracker_type,
1534 id, LTTNG_TRACK_ID);
1535}
ccf10263 1536
e32d22be
MD
1537/*
1538 * Remove ID from session tracker.
1539 * Return 0 on success else a negative LTTng error code.
1540 */
1541int lttng_untrack_id(struct lttng_handle *handle,
1542 enum lttng_tracker_type tracker_type,
1543 struct lttng_tracker_id *id)
1544{
1545 return lttng_track_untrack_id(handle, tracker_type,
1546 id, LTTNG_UNTRACK_ID);
1547}
ccf10263 1548
e32d22be
MD
1549/*
1550 * Add PID to session tracker.
1551 * Return 0 on success else a negative LTTng error code.
1552 */
1553int lttng_track_pid(struct lttng_handle *handle, int pid)
1554{
1555 struct lttng_tracker_id id;
ccf10263 1556
e32d22be
MD
1557 id.type = LTTNG_TRACKER_PID;
1558 id.value = pid;
1559 return lttng_track_id(handle, LTTNG_TRACKER_PID, &id);
1560}
ccf10263 1561
e32d22be
MD
1562/*
1563 * Remove PID from session tracker.
1564 * Return 0 on success else a negative LTTng error code.
1565 */
1566int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1567{
1568 struct lttng_tracker_id id;
ccf10263 1569
e32d22be
MD
1570 id.type = LTTNG_TRACKER_PID;
1571 id.value = pid;
1572 return lttng_untrack_id(handle, LTTNG_TRACKER_PID, &id);
ccf10263
MD
1573}
1574
fac6795d 1575/*
9ae110e2
JG
1576 * Lists all available tracepoints of domain.
1577 * Sets the contents of the events array.
1578 * Returns the number of lttng_event entries in events;
1579 * on error, returns a negative value.
fac6795d 1580 */
cd80958d 1581int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1582 struct lttng_event **events)
fac6795d 1583{
052da939 1584 int ret;
cd80958d
DG
1585 struct lttcomm_session_msg lsm;
1586
9d697d3d 1587 if (handle == NULL) {
2f70b271 1588 return -LTTNG_ERR_INVALID;
cd80958d 1589 }
fac6795d 1590
53efb85a 1591 memset(&lsm, 0, sizeof(lsm));
cd80958d 1592 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1593 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1594
cac3069d 1595 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1596 if (ret < 0) {
1597 return ret;
eb354453 1598 }
fac6795d 1599
9f19cc17 1600 return ret / sizeof(struct lttng_event);
fac6795d
DG
1601}
1602
f37d259d 1603/*
9ae110e2
JG
1604 * Lists all available tracepoint fields of domain.
1605 * Sets the contents of the event field array.
1606 * Returns the number of lttng_event_field entries in events;
1607 * on error, returns a negative value.
f37d259d
MD
1608 */
1609int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1610 struct lttng_event_field **fields)
1611{
1612 int ret;
1613 struct lttcomm_session_msg lsm;
1614
1615 if (handle == NULL) {
2f70b271 1616 return -LTTNG_ERR_INVALID;
f37d259d
MD
1617 }
1618
53efb85a 1619 memset(&lsm, 0, sizeof(lsm));
f37d259d 1620 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1621 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1622
cac3069d 1623 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1624 if (ret < 0) {
1625 return ret;
1626 }
1627
1628 return ret / sizeof(struct lttng_event_field);
1629}
1630
834978fd 1631/*
9ae110e2
JG
1632 * Lists all available kernel system calls. Allocates and sets the contents of
1633 * the events array.
834978fd 1634 *
9ae110e2
JG
1635 * Returns the number of lttng_event entries in events; on error, returns a
1636 * negative value.
834978fd
DG
1637 */
1638int lttng_list_syscalls(struct lttng_event **events)
1639{
1640 int ret;
1641 struct lttcomm_session_msg lsm;
1642
1643 if (!events) {
1644 return -LTTNG_ERR_INVALID;
1645 }
1646
1647 memset(&lsm, 0, sizeof(lsm));
1648 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1649 /* Force kernel domain for system calls. */
1650 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1651
1652 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1653 if (ret < 0) {
1654 return ret;
1655 }
1656
1657 return ret / sizeof(struct lttng_event);
1658}
1659
1657e9bb 1660/*
9ae110e2
JG
1661 * Returns a human readable string describing
1662 * the error code (a negative value).
1657e9bb 1663 */
9a745bc7 1664const char *lttng_strerror(int code)
1657e9bb 1665{
f73fabfd 1666 return error_get_str(code);
1657e9bb
DG
1667}
1668
aaf97519 1669/*
a4b92340
DG
1670 * Create a brand new session using name and url for destination.
1671 *
f73fabfd 1672 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1673 */
a4b92340 1674int lttng_create_session(const char *name, const char *url)
00e2e675 1675{
3dd05a85 1676 int ret;
a4b92340 1677 ssize_t size;
00e2e675 1678 struct lttcomm_session_msg lsm;
a4b92340 1679 struct lttng_uri *uris = NULL;
00e2e675 1680
a4b92340 1681 if (name == NULL) {
2f70b271 1682 return -LTTNG_ERR_INVALID;
00e2e675
DG
1683 }
1684
a4b92340 1685 memset(&lsm, 0, sizeof(lsm));
00e2e675 1686
a4b92340 1687 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1688 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1689
1690 /* There should never be a data URL */
bc894455 1691 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1692 if (size < 0) {
2f70b271 1693 return -LTTNG_ERR_INVALID;
00e2e675
DG
1694 }
1695
a4b92340
DG
1696 lsm.u.uri.size = size;
1697
795a978d 1698 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 1699 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1700
1701 free(uris);
1702 return ret;
00e2e675
DG
1703}
1704
8028d920 1705/*
9ae110e2
JG
1706 * Destroy session using name.
1707 * Returns size of returned session payload data or a negative error code.
8028d920 1708 */
8fd2c92c 1709static
e20ee7c2 1710int _lttng_destroy_session(const char *session_name)
8028d920 1711{
cd80958d
DG
1712 struct lttcomm_session_msg lsm;
1713
843f5df9 1714 if (session_name == NULL) {
2f70b271 1715 return -LTTNG_ERR_INVALID;
cd80958d
DG
1716 }
1717
53efb85a 1718 memset(&lsm, 0, sizeof(lsm));
cd80958d 1719 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1720
cac3069d
DG
1721 lttng_ctl_copy_string(lsm.session.name, session_name,
1722 sizeof(lsm.session.name));
cd80958d 1723
cac3069d 1724 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1725}
1726
e20ee7c2
JD
1727/*
1728 * Stop the session and wait for the data before destroying it
1729 */
1730int lttng_destroy_session(const char *session_name)
1731{
1732 int ret;
1733
1734 /*
1735 * Stop the tracing and wait for the data.
1736 */
1737 ret = _lttng_stop_tracing(session_name, 1);
1738 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1739 goto end;
1740 }
1741
1742 ret = _lttng_destroy_session(session_name);
1743end:
1744 return ret;
1745}
1746
1747/*
1748 * Destroy the session without waiting for the data.
1749 */
1750int lttng_destroy_session_no_wait(const char *session_name)
1751{
1752 int ret;
1753
1754 /*
1755 * Stop the tracing without waiting for the data.
1756 * The session might already have been stopped, so just
1757 * skip this error.
1758 */
1759 ret = _lttng_stop_tracing(session_name, 0);
1760 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1761 goto end;
1762 }
1763
1764 ret = _lttng_destroy_session(session_name);
1765end:
1766 return ret;
1767}
1768
57167058 1769/*
9ae110e2
JG
1770 * Ask the session daemon for all available sessions.
1771 * Sets the contents of the sessions array.
1772 * Returns the number of lttng_session entries in sessions;
1773 * on error, returns a negative value.
57167058 1774 */
ca95a216 1775int lttng_list_sessions(struct lttng_session **sessions)
57167058 1776{
ca95a216 1777 int ret;
cd80958d 1778 struct lttcomm_session_msg lsm;
57167058 1779
53efb85a 1780 memset(&lsm, 0, sizeof(lsm));
cd80958d 1781 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1782 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1783 if (ret < 0) {
ca95a216 1784 return ret;
57167058
DG
1785 }
1786
ca95a216 1787 return ret / sizeof(struct lttng_session);
57167058
DG
1788}
1789
d7ba1388
MD
1790int lttng_set_session_shm_path(const char *session_name,
1791 const char *shm_path)
1792{
1793 struct lttcomm_session_msg lsm;
1794
1795 if (session_name == NULL) {
1796 return -LTTNG_ERR_INVALID;
1797 }
1798
1799 memset(&lsm, 0, sizeof(lsm));
1800 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1801
1802 lttng_ctl_copy_string(lsm.session.name, session_name,
1803 sizeof(lsm.session.name));
1804 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1805 sizeof(lsm.u.set_shm_path.shm_path));
1806
1807 return lttng_ctl_ask_sessiond(&lsm, NULL);
1808}
1809
9f19cc17 1810/*
9ae110e2
JG
1811 * Ask the session daemon for all available domains of a session.
1812 * Sets the contents of the domains array.
1813 * Returns the number of lttng_domain entries in domains;
1814 * on error, returns a negative value.
9f19cc17 1815 */
330be774 1816int lttng_list_domains(const char *session_name,
cd80958d 1817 struct lttng_domain **domains)
9f19cc17
DG
1818{
1819 int ret;
cd80958d
DG
1820 struct lttcomm_session_msg lsm;
1821
330be774 1822 if (session_name == NULL) {
2f70b271 1823 return -LTTNG_ERR_INVALID;
cd80958d 1824 }
9f19cc17 1825
53efb85a 1826 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
1827 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1828
cac3069d
DG
1829 lttng_ctl_copy_string(lsm.session.name, session_name,
1830 sizeof(lsm.session.name));
cd80958d 1831
cac3069d 1832 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1833 if (ret < 0) {
1834 return ret;
1835 }
1836
1837 return ret / sizeof(struct lttng_domain);
1838}
1839
1840/*
9ae110e2
JG
1841 * Ask the session daemon for all available channels of a session.
1842 * Sets the contents of the channels array.
1843 * Returns the number of lttng_channel entries in channels;
1844 * on error, returns a negative value.
9f19cc17 1845 */
cd80958d
DG
1846int lttng_list_channels(struct lttng_handle *handle,
1847 struct lttng_channel **channels)
9f19cc17
DG
1848{
1849 int ret;
53e367f9
JG
1850 size_t channel_count, i;
1851 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51 1852 sizeof(struct lttng_channel_extended);
cd80958d 1853 struct lttcomm_session_msg lsm;
53e367f9 1854 void *extended_at;
cd80958d 1855
9d697d3d 1856 if (handle == NULL) {
53e367f9
JG
1857 ret = -LTTNG_ERR_INVALID;
1858 goto end;
cd80958d
DG
1859 }
1860
53efb85a 1861 memset(&lsm, 0, sizeof(lsm));
cd80958d 1862 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1863 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1864 sizeof(lsm.session.name));
9f19cc17 1865
cac3069d 1866 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1867
cac3069d 1868 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17 1869 if (ret < 0) {
53e367f9
JG
1870 goto end;
1871 }
1872
1873 if (ret % channel_size) {
1874 ret = -LTTNG_ERR_UNK;
1875 free(*channels);
1876 *channels = NULL;
1877 goto end;
9f19cc17 1878 }
53e367f9 1879 channel_count = (size_t) ret / channel_size;
9f19cc17 1880
53e367f9
JG
1881 /* Set extended info pointers */
1882 extended_at = ((void *) *channels) +
1883 channel_count * sizeof(struct lttng_channel);
1884 for (i = 0; i < channel_count; i++) {
1885 struct lttng_channel *chan = &(*channels)[i];
1886
1887 chan->attr.extended.ptr = extended_at;
cf0bcb51 1888 extended_at += sizeof(struct lttng_channel_extended);
53e367f9
JG
1889 }
1890
1891 ret = (int) channel_count;
1892end:
1893 return ret;
9f19cc17
DG
1894}
1895
1896/*
9ae110e2
JG
1897 * Ask the session daemon for all available events of a session channel.
1898 * Sets the contents of the events array.
1899 * Returns the number of lttng_event entries in events;
1900 * on error, returns a negative value.
9f19cc17 1901 */
cd80958d
DG
1902int lttng_list_events(struct lttng_handle *handle,
1903 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1904{
1905 int ret;
cd80958d 1906 struct lttcomm_session_msg lsm;
b4e3ceb9
PP
1907 struct lttcomm_event_command_header *cmd_header = NULL;
1908 size_t cmd_header_len;
1909 uint32_t nb_events, i;
1910 void *extended_at;
9f19cc17 1911
9d697d3d
DG
1912 /* Safety check. An handle and channel name are mandatory */
1913 if (handle == NULL || channel_name == NULL) {
2f70b271 1914 return -LTTNG_ERR_INVALID;
cd80958d
DG
1915 }
1916
53efb85a 1917 memset(&lsm, 0, sizeof(lsm));
cd80958d 1918 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1919 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1920 sizeof(lsm.session.name));
cac3069d 1921 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d 1922 sizeof(lsm.u.list.channel_name));
cac3069d 1923 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1924
b4e3ceb9
PP
1925 ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) events,
1926 (void **) &cmd_header, &cmd_header_len);
9f19cc17 1927 if (ret < 0) {
b4e3ceb9 1928 goto error;
9f19cc17
DG
1929 }
1930
b4e3ceb9
PP
1931 /* Set number of events and free command header */
1932 nb_events = cmd_header->nb_events;
1933 if (nb_events > INT_MAX) {
e32d22be 1934 ret = -LTTNG_ERR_OVERFLOW;
b4e3ceb9
PP
1935 goto error;
1936 }
1937 ret = (int) nb_events;
1938 free(cmd_header);
1939 cmd_header = NULL;
1940
1941 /* Set extended info pointers */
1942 extended_at = ((void*) (*events)) +
1943 nb_events * sizeof(struct lttng_event);
1944
1945 for (i = 0; i < nb_events; i++) {
1946 struct lttcomm_event_extended_header *ext_header;
1947 struct lttng_event *event = &(*events)[i];
1948
1949 event->extended.ptr = extended_at;
1950 ext_header =
1951 (struct lttcomm_event_extended_header *) extended_at;
1952 extended_at += sizeof(*ext_header);
1953 extended_at += ext_header->filter_len;
795d57ce
PP
1954 extended_at +=
1955 ext_header->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
b4e3ceb9
PP
1956 }
1957
1958 return ret;
1959error:
1960 free(cmd_header);
1961 free(*events);
1962 return ret;
9f19cc17
DG
1963}
1964
134e72ed
JG
1965int lttng_event_get_filter_expression(struct lttng_event *event,
1966 const char **filter_expression)
d31d3e8c
PP
1967{
1968 int ret = 0;
1969 struct lttcomm_event_extended_header *ext_header;
1970
134e72ed 1971 if (!event || !filter_expression) {
d31d3e8c
PP
1972 ret = -LTTNG_ERR_INVALID;
1973 goto end;
1974 }
1975
1976 ext_header = event->extended.ptr;
1977
1978 if (!ext_header) {
1979 /*
1980 * This can happen since the lttng_event structure is
1981 * used for other tasks where this pointer is never set.
1982 */
134e72ed 1983 *filter_expression = NULL;
d31d3e8c
PP
1984 goto end;
1985 }
1986
1987 if (ext_header->filter_len) {
134e72ed
JG
1988 *filter_expression = ((const char *) (ext_header)) +
1989 sizeof(*ext_header);
d31d3e8c 1990 } else {
134e72ed 1991 *filter_expression = NULL;
d31d3e8c
PP
1992 }
1993
1994end:
1995 return ret;
1996}
b4e3ceb9 1997
f086e50e
PP
1998int lttng_event_get_exclusion_name_count(struct lttng_event *event)
1999{
2000 int ret;
2001 struct lttcomm_event_extended_header *ext_header;
2002
2003 if (!event) {
2004 ret = -LTTNG_ERR_INVALID;
2005 goto end;
2006 }
2007
2008 ext_header = event->extended.ptr;
2009 if (!ext_header) {
2010 /*
2011 * This can happen since the lttng_event structure is
2012 * used for other tasks where this pointer is never set.
2013 */
2014 ret = 0;
2015 goto end;
2016 }
2017
2018 if (ext_header->nb_exclusions > INT_MAX) {
2019 ret = -LTTNG_ERR_OVERFLOW;
2020 goto end;
2021 }
2022 ret = (int) ext_header->nb_exclusions;
2023end:
2024 return ret;
2025}
2026
2027int lttng_event_get_exclusion_name(struct lttng_event *event,
2028 size_t index, const char **exclusion_name)
2029{
2030 int ret = 0;
2031 struct lttcomm_event_extended_header *ext_header;
2032 void *at;
2033
2034 if (!event || !exclusion_name) {
2035 ret = -LTTNG_ERR_INVALID;
2036 goto end;
2037 }
2038
2039 ext_header = event->extended.ptr;
2040 if (!ext_header) {
2041 ret = -LTTNG_ERR_INVALID;
2042 goto end;
2043 }
2044
2045 if (index >= ext_header->nb_exclusions) {
2046 ret = -LTTNG_ERR_INVALID;
2047 goto end;
2048 }
2049
2050 at = (void *) ext_header + sizeof(*ext_header);
2051 at += ext_header->filter_len;
2052 at += index * LTTNG_SYMBOL_NAME_LEN;
2053 *exclusion_name = at;
2054
2055end:
2056 return ret;
2057}
2058
fac6795d 2059/*
1c8d13c8
TD
2060 * Sets the tracing_group variable with name.
2061 * This function allocates memory pointed to by tracing_group.
2062 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
2063 */
2064int lttng_set_tracing_group(const char *name)
2065{
9d697d3d 2066 if (name == NULL) {
2f70b271 2067 return -LTTNG_ERR_INVALID;
9d697d3d
DG
2068 }
2069
fac6795d 2070 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 2071 return -LTTNG_ERR_FATAL;
fac6795d
DG
2072 }
2073
2074 return 0;
2075}
2076
cd80958d 2077int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
2078 struct lttng_calibrate *calibrate)
2079{
b812e5ca
PP
2080 /*
2081 * This command was removed in LTTng 2.9.
2082 */
2083 return -LTTNG_ERR_UND;
d0254c7c
MD
2084}
2085
5edd7e09
DG
2086/*
2087 * Set default channel attributes.
441c16a7 2088 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
2089 */
2090void lttng_channel_set_default_attr(struct lttng_domain *domain,
2091 struct lttng_channel_attr *attr)
2092{
294851b0 2093 struct lttng_channel_extended *extended;
cf0bcb51 2094
5edd7e09
DG
2095 /* Safety check */
2096 if (attr == NULL || domain == NULL) {
2097 return;
2098 }
2099
294851b0 2100 extended = (struct lttng_channel_extended *) attr->extended.ptr;
eacaa7a6
DS
2101 memset(attr, 0, sizeof(struct lttng_channel_attr));
2102
0a9c6494
DG
2103 /* Same for all domains. */
2104 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2105 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2106 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2107
5edd7e09
DG
2108 switch (domain->type) {
2109 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
2110 attr->switch_timer_interval =
2111 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 2112 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 2113 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
2114 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2115 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
cf0bcb51
JG
2116 if (extended) {
2117 extended->monitor_timer_interval =
2118 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
491d1539
MD
2119 extended->blocking_timeout =
2120 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2121 }
5edd7e09
DG
2122 break;
2123 case LTTNG_DOMAIN_UST:
0a9c6494
DG
2124 switch (domain->buf_type) {
2125 case LTTNG_BUFFER_PER_UID:
2126 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2127 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2128 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
2129 attr->switch_timer_interval =
2130 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2131 attr->read_timer_interval =
2132 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2133 if (extended) {
2134 extended->monitor_timer_interval =
2135 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2136 extended->blocking_timeout =
2137 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2138 }
0a9c6494
DG
2139 break;
2140 case LTTNG_BUFFER_PER_PID:
2141 default:
2142 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2143 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2144 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
2145 attr->switch_timer_interval =
2146 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2147 attr->read_timer_interval =
2148 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2149 if (extended) {
2150 extended->monitor_timer_interval =
2151 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2152 extended->blocking_timeout =
2153 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2154 }
0a9c6494
DG
2155 break;
2156 }
5edd7e09 2157 default:
441c16a7 2158 /* Default behavior: leave set to 0. */
5edd7e09
DG
2159 break;
2160 }
cf0bcb51
JG
2161
2162 attr->extended.ptr = extended;
5edd7e09
DG
2163}
2164
5ba3702f
JG
2165int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2166 uint64_t *discarded_events)
2167{
2168 int ret = 0;
cf0bcb51 2169 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2170
2171 if (!channel || !discarded_events) {
2172 ret = -LTTNG_ERR_INVALID;
2173 goto end;
2174 }
2175
2176 chan_ext = channel->attr.extended.ptr;
2177 if (!chan_ext) {
2178 /*
2179 * This can happen since the lttng_channel structure is
2180 * used for other tasks where this pointer is never set.
2181 */
2182 *discarded_events = 0;
2183 goto end;
2184 }
2185
2186 *discarded_events = chan_ext->discarded_events;
2187end:
2188 return ret;
2189}
2190
2191int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2192 uint64_t *lost_packets)
2193{
2194 int ret = 0;
cf0bcb51 2195 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2196
2197 if (!channel || !lost_packets) {
2198 ret = -LTTNG_ERR_INVALID;
2199 goto end;
2200 }
2201
2202 chan_ext = channel->attr.extended.ptr;
2203 if (!chan_ext) {
2204 /*
2205 * This can happen since the lttng_channel structure is
2206 * used for other tasks where this pointer is never set.
2207 */
2208 *lost_packets = 0;
2209 goto end;
2210 }
2211
2212 *lost_packets = chan_ext->lost_packets;
2213end:
2214 return ret;
2215}
2216
cf0bcb51
JG
2217int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2218 uint64_t *monitor_timer_interval)
2219{
2220 int ret = 0;
2221
2222 if (!chan || !monitor_timer_interval) {
2223 ret = -LTTNG_ERR_INVALID;
2224 goto end;
2225 }
2226
2227 if (!chan->attr.extended.ptr) {
2228 ret = -LTTNG_ERR_INVALID;
2229 goto end;
2230 }
2231
2232 *monitor_timer_interval = ((struct lttng_channel_extended *)
2233 chan->attr.extended.ptr)->monitor_timer_interval;
2234end:
2235 return ret;
2236}
2237
2238int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2239 uint64_t monitor_timer_interval)
2240{
2241 int ret = 0;
2242
2243 if (!chan || !chan->attr.extended.ptr) {
2244 ret = -LTTNG_ERR_INVALID;
2245 goto end;
2246 }
2247
2248 ((struct lttng_channel_extended *)
2249 chan->attr.extended.ptr)->monitor_timer_interval =
2250 monitor_timer_interval;
2251end:
2252 return ret;
2253}
2254
491d1539
MD
2255int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2256 int64_t *blocking_timeout)
2257{
2258 int ret = 0;
2259
2260 if (!chan || !blocking_timeout) {
2261 ret = -LTTNG_ERR_INVALID;
2262 goto end;
2263 }
2264
2265 if (!chan->attr.extended.ptr) {
2266 ret = -LTTNG_ERR_INVALID;
2267 goto end;
2268 }
2269
2270 *blocking_timeout = ((struct lttng_channel_extended *)
2271 chan->attr.extended.ptr)->blocking_timeout;
2272end:
2273 return ret;
2274}
2275
2276int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2277 int64_t blocking_timeout)
2278{
2279 int ret = 0;
2280 int64_t msec_timeout;
2281
2282 if (!chan || !chan->attr.extended.ptr) {
2283 ret = -LTTNG_ERR_INVALID;
2284 goto end;
2285 }
2286
2287 if (blocking_timeout < 0 && blocking_timeout != -1) {
2288 ret = -LTTNG_ERR_INVALID;
2289 goto end;
2290 }
2291
2292 /*
2293 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2294 * us to accept a narrower range of values (msecs expressed as a signed
2295 * 32-bit integer).
2296 */
2297 msec_timeout = blocking_timeout / 1000;
2298 if (msec_timeout != (int32_t) msec_timeout) {
2299 ret = -LTTNG_ERR_INVALID;
2300 goto end;
2301 }
2302
2303 ((struct lttng_channel_extended *)
2304 chan->attr.extended.ptr)->blocking_timeout =
2305 blocking_timeout;
2306end:
2307 return ret;
2308}
2309
fac6795d 2310/*
2269e89e 2311 * Check if session daemon is alive.
fac6795d 2312 *
2269e89e 2313 * Return 1 if alive or 0 if not.
1c8d13c8 2314 * On error returns a negative value.
fac6795d 2315 */
947308c4 2316int lttng_session_daemon_alive(void)
fac6795d
DG
2317{
2318 int ret;
2319
2320 ret = set_session_daemon_path();
2321 if (ret < 0) {
9ae110e2 2322 /* Error. */
fac6795d
DG
2323 return ret;
2324 }
2325
9d035200 2326 if (*sessiond_sock_path == '\0') {
2f70b271 2327 /*
9ae110e2
JG
2328 * No socket path set. Weird error which means the constructor
2329 * was not called.
2f70b271
DG
2330 */
2331 assert(0);
fac6795d
DG
2332 }
2333
2269e89e 2334 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 2335 if (ret < 0) {
9ae110e2 2336 /* Not alive. */
7d8234d9
MD
2337 return 0;
2338 }
7d8234d9 2339
9ae110e2 2340 /* Is alive. */
947308c4 2341 return 1;
fac6795d
DG
2342}
2343
00e2e675 2344/*
a4b92340 2345 * Set URL for a consumer for a session and domain.
00e2e675
DG
2346 *
2347 * Return 0 on success, else a negative value.
2348 */
a4b92340
DG
2349int lttng_set_consumer_url(struct lttng_handle *handle,
2350 const char *control_url, const char *data_url)
00e2e675 2351{
3dd05a85 2352 int ret;
a4b92340 2353 ssize_t size;
00e2e675 2354 struct lttcomm_session_msg lsm;
a4b92340 2355 struct lttng_uri *uris = NULL;
00e2e675 2356
a4b92340 2357 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 2358 return -LTTNG_ERR_INVALID;
00e2e675
DG
2359 }
2360
a4b92340
DG
2361 memset(&lsm, 0, sizeof(lsm));
2362
00e2e675
DG
2363 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2364
cac3069d 2365 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 2366 sizeof(lsm.session.name));
cac3069d 2367 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 2368
bc894455 2369 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 2370 if (size < 0) {
2f70b271 2371 return -LTTNG_ERR_INVALID;
a4b92340
DG
2372 }
2373
2374 lsm.u.uri.size = size;
00e2e675 2375
795a978d 2376 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2377 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2378
2379 free(uris);
2380 return ret;
00e2e675
DG
2381}
2382
2383/*
9c6bda17 2384 * [OBSOLETE]
00e2e675
DG
2385 */
2386int lttng_enable_consumer(struct lttng_handle *handle)
2387{
785d2d0d 2388 return -ENOSYS;
00e2e675
DG
2389}
2390
2391/*
9c6bda17 2392 * [OBSOLETE]
00e2e675
DG
2393 */
2394int lttng_disable_consumer(struct lttng_handle *handle)
2395{
785d2d0d 2396 return -ENOSYS;
00e2e675
DG
2397}
2398
07424f16
DG
2399/*
2400 * This is an extension of create session that is ONLY and SHOULD only be used
2401 * by the lttng command line program. It exists to avoid using URI parsing in
2402 * the lttng client.
2403 *
2404 * We need the date and time for the trace path subdirectory for the case where
2405 * the user does NOT define one using either -o or -U. Using the normal
2406 * lttng_create_session API call, we have no clue on the session daemon side if
2407 * the URL was generated automatically by the client or define by the user.
2408 *
2409 * So this function "wrapper" is hidden from the public API, takes the datetime
2410 * string and appends it if necessary to the URI subdirectory before sending it
2411 * to the session daemon.
2412 *
2413 * With this extra function, the lttng_create_session call behavior is not
2414 * changed and the timestamp is appended to the URI on the session daemon side
2415 * if necessary.
2416 */
2417int _lttng_create_session_ext(const char *name, const char *url,
2418 const char *datetime)
2419{
3dd05a85 2420 int ret;
07424f16
DG
2421 ssize_t size;
2422 struct lttcomm_session_msg lsm;
2423 struct lttng_uri *uris = NULL;
2424
2bba9e53 2425 if (name == NULL || datetime == NULL) {
2f70b271 2426 return -LTTNG_ERR_INVALID;
07424f16
DG
2427 }
2428
2429 memset(&lsm, 0, sizeof(lsm));
2430
2431 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 2432 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16 2433
9ae110e2 2434 /* There should never be a data URL. */
bc894455 2435 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 2436 if (size < 0) {
3dd05a85
DG
2437 ret = -LTTNG_ERR_INVALID;
2438 goto error;
07424f16
DG
2439 }
2440
2441 lsm.u.uri.size = size;
2442
4b35a6b3 2443 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
2444 /* Don't append datetime if the name was automatically created. */
2445 if (strncmp(name, DEFAULT_SESSION_NAME "-",
2446 strlen(DEFAULT_SESSION_NAME) + 1)) {
2447 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
2448 name, datetime);
2449 } else {
2450 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2451 }
07424f16
DG
2452 if (ret < 0) {
2453 PERROR("snprintf uri subdir");
3dd05a85
DG
2454 ret = -LTTNG_ERR_FATAL;
2455 goto error;
07424f16
DG
2456 }
2457 }
2458
795a978d 2459 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2460 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2461
2462error:
2463 free(uris);
2464 return ret;
07424f16
DG
2465}
2466
806e2684
DG
2467/*
2468 * For a given session name, this call checks if the data is ready to be read
2469 * or is still being extracted by the consumer(s) hence not ready to be used by
2470 * any readers.
2471 */
6d805429 2472int lttng_data_pending(const char *session_name)
806e2684
DG
2473{
2474 int ret;
2475 struct lttcomm_session_msg lsm;
f6151c55 2476 uint8_t *pending = NULL;
806e2684
DG
2477
2478 if (session_name == NULL) {
2479 return -LTTNG_ERR_INVALID;
2480 }
2481
53efb85a 2482 memset(&lsm, 0, sizeof(lsm));
6d805429 2483 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2484
cac3069d
DG
2485 lttng_ctl_copy_string(lsm.session.name, session_name,
2486 sizeof(lsm.session.name));
806e2684 2487
f6151c55
JG
2488 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2489 if (ret < 0) {
2490 goto end;
2491 } else if (ret != 1) {
2492 /* Unexpected payload size */
2493 ret = -LTTNG_ERR_INVALID;
2494 goto end;
806e2684
DG
2495 }
2496
f6151c55
JG
2497 ret = (int) *pending;
2498end:
2499 free(pending);
806e2684
DG
2500 return ret;
2501}
2502
27babd3a
DG
2503/*
2504 * Create a session exclusively used for snapshot.
2505 *
2506 * Returns LTTNG_OK on success or a negative error code.
2507 */
2508int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2509{
2510 int ret;
2511 ssize_t size;
2512 struct lttcomm_session_msg lsm;
2513 struct lttng_uri *uris = NULL;
2514
2515 if (name == NULL) {
2516 return -LTTNG_ERR_INVALID;
2517 }
2518
2519 memset(&lsm, 0, sizeof(lsm));
2520
2521 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
2522 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2523
2524 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2525 if (size < 0) {
2526 return -LTTNG_ERR_INVALID;
2527 }
2528
2529 lsm.u.uri.size = size;
2530
10d65351
JD
2531 /*
2532 * If the user does not specify a custom subdir, use the session name.
2533 */
2534 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
2535 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2536 if (ret < 0) {
2537 PERROR("snprintf uri subdir");
2538 ret = -LTTNG_ERR_FATAL;
2539 goto error;
2540 }
2541 }
2542
795a978d 2543 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
27babd3a
DG
2544 sizeof(struct lttng_uri) * size, NULL);
2545
10d65351 2546error:
27babd3a
DG
2547 free(uris);
2548 return ret;
2549}
2550
ecc48a90
JD
2551/*
2552 * Create a session exclusively used for live.
2553 *
2554 * Returns LTTNG_OK on success or a negative error code.
2555 */
2556int lttng_create_session_live(const char *name, const char *url,
2557 unsigned int timer_interval)
2558{
2559 int ret;
2560 ssize_t size;
2561 struct lttcomm_session_msg lsm;
2562 struct lttng_uri *uris = NULL;
2563
92805ee4 2564 if (name == NULL || timer_interval == 0) {
ecc48a90
JD
2565 return -LTTNG_ERR_INVALID;
2566 }
2567
2568 memset(&lsm, 0, sizeof(lsm));
2569
2570 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
2571 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2572
1a241656
DG
2573 if (url) {
2574 size = uri_parse_str_urls(url, NULL, &uris);
2575 if (size <= 0) {
2576 ret = -LTTNG_ERR_INVALID;
2577 goto end;
2578 }
ecc48a90 2579
1a241656
DG
2580 /* file:// is not accepted for live session. */
2581 if (uris[0].dtype == LTTNG_DST_PATH) {
2582 ret = -LTTNG_ERR_INVALID;
2583 goto end;
2584 }
2585 } else {
2586 size = 0;
ecc48a90
JD
2587 }
2588
2589 lsm.u.session_live.nb_uri = size;
2590 lsm.u.session_live.timer_interval = timer_interval;
2591
795a978d 2592 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
ecc48a90
JD
2593 sizeof(struct lttng_uri) * size, NULL);
2594
2595end:
2596 free(uris);
2597 return ret;
2598}
2599
a5dfbb9d 2600/*
e32d22be 2601 * List IDs in the tracker.
a5dfbb9d 2602 *
e32d22be
MD
2603 * tracker_type is the type of tracker.
2604 * ids is set to an allocated array of IDs currently tracked. On
2605 * success, ids and all the strings it contains must be freed by the caller.
2606 * nr_ids is set to the number of entries contained by the ids array.
a5dfbb9d
MD
2607 *
2608 * Returns 0 on success, else a negative LTTng error code.
2609 */
e32d22be
MD
2610int lttng_list_tracker_ids(struct lttng_handle *handle,
2611 enum lttng_tracker_type tracker_type,
2612 struct lttng_tracker_id **_ids, size_t *_nr_ids)
a5dfbb9d 2613{
e32d22be 2614 int ret, i;
a5dfbb9d 2615 struct lttcomm_session_msg lsm;
e32d22be
MD
2616 struct lttcomm_tracker_command_header *cmd_header = NULL;
2617 char *cmd_payload = NULL, *p;
2618 size_t cmd_header_len;
2619 size_t nr_ids = 0;
2620 struct lttng_tracker_id *ids = NULL;
a5dfbb9d
MD
2621
2622 if (handle == NULL) {
2623 return -LTTNG_ERR_INVALID;
2624 }
2625
2626 memset(&lsm, 0, sizeof(lsm));
e32d22be
MD
2627 lsm.cmd_type = LTTNG_LIST_TRACKER_IDS;
2628 lsm.u.id_tracker_list.tracker_type = tracker_type;
a5dfbb9d
MD
2629 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2630 sizeof(lsm.session.name));
2631 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2632
e32d22be
MD
2633 ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) &cmd_payload,
2634 (void **) &cmd_header, &cmd_header_len);
a5dfbb9d 2635 if (ret < 0) {
e32d22be
MD
2636 goto error;
2637 }
2638
2639 /* Set number of tracker_id and free command header */
2640 nr_ids = cmd_header->nb_tracker_id;
2641 if (nr_ids > INT_MAX) {
2642 ret = -LTTNG_ERR_OVERFLOW;
2643 goto error;
2644 }
2645 free(cmd_header);
2646 cmd_header = NULL;
2647
2648 ids = zmalloc(sizeof(*ids) * nr_ids);
2649 if (!ids) {
2650 ret = -LTTNG_ERR_NOMEM;
2651 goto error;
2652 }
2653
2654 p = cmd_payload;
2655 for (i = 0; i < nr_ids; i++) {
2656 struct lttcomm_tracker_id_header *tracker_id;
2657 struct lttng_tracker_id *id;
2658
2659 tracker_id = (struct lttcomm_tracker_id_header *) p;
2660 p += sizeof(struct lttcomm_tracker_id_header);
2661 id = &ids[i];
2662
2663 id->type = tracker_id->type;
2664 switch (tracker_id->type) {
2665 case LTTNG_ID_ALL:
2666 break;
2667 case LTTNG_ID_VALUE:
2668 id->value = tracker_id->u.value;
2669 break;
2670 case LTTNG_ID_STRING:
2671 id->string = strdup(p);
2672 if (!id->string) {
2673 ret = -LTTNG_ERR_NOMEM;
2674 goto error;
2675 }
2676 p += tracker_id->u.var_data_len;
2677 break;
2678 default:
2679 goto error;
2680 }
2681 }
2682 free(cmd_payload);
2683 *_ids = ids;
2684 *_nr_ids = nr_ids;
2685 return 0;
2686
2687error:
2688 if (ids) {
2689 for (i = 0; i < nr_ids; i++) {
2690 free(ids[i].string);
2691 }
2692 free(ids);
2693 }
2694 free(cmd_payload);
2695 free(cmd_header);
2696 return ret;
2697}
2698
2699/*
2700 * List PIDs in the tracker.
2701 *
2702 * enabled is set to whether the PID tracker is enabled.
2703 * pids is set to an allocated array of PIDs currently tracked. On
2704 * success, pids must be freed by the caller.
2705 * nr_pids is set to the number of entries contained by the pids array.
2706 *
2707 * Returns 0 on success, else a negative LTTng error code.
2708 */
2709int lttng_list_tracker_pids(struct lttng_handle *handle,
2710 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2711{
2712 struct lttng_tracker_id *ids = NULL;
2713 size_t nr_ids = 0;
2714 int *pids = NULL;
2715 int ret = 0, i;
2716
2717 ret = lttng_list_tracker_ids(handle, LTTNG_TRACKER_PID,
2718 &ids, &nr_ids);
2719 if (ret < 0)
a5dfbb9d 2720 return ret;
e32d22be
MD
2721
2722 if (nr_ids == 1 && ids[0].type == LTTNG_ID_ALL) {
2723 *_enabled = 0;
2724 goto end;
a5dfbb9d 2725 }
e32d22be
MD
2726 *_enabled = 1;
2727
2728 pids = zmalloc(nr_ids * sizeof(*pids));
2729 if (!pids) {
2730 ret = -LTTNG_ERR_NOMEM;
2731 goto end;
2732 }
2733 for (i = 0; i < nr_ids; i++) {
2734 struct lttng_tracker_id *id = &ids[i];
2735
2736 if (id->type != LTTNG_ID_VALUE) {
2737 ret = -LTTNG_ERR_UNK;
2738 goto end;
2739 }
2740 pids[i] = id->value;
a5dfbb9d 2741 }
a5dfbb9d 2742 *_pids = pids;
e32d22be
MD
2743 *_nr_pids = nr_ids;
2744end:
2745 for (i = 0; i < nr_ids; i++) {
2746 free(ids[i].string);
2747 }
2748 free(ids);
2749 if (ret < 0) {
2750 free(pids);
2751 }
2752 return ret;
a5dfbb9d
MD
2753}
2754
93ec662e
JD
2755/*
2756 * Regenerate the metadata for a session.
2757 * Return 0 on success, a negative error code on error.
2758 */
eded6438 2759int lttng_regenerate_metadata(const char *session_name)
93ec662e
JD
2760{
2761 int ret;
2762 struct lttcomm_session_msg lsm;
2763
2764 if (!session_name) {
2765 ret = -LTTNG_ERR_INVALID;
2766 goto end;
2767 }
2768
2769 memset(&lsm, 0, sizeof(lsm));
eded6438 2770 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
93ec662e
JD
2771
2772 lttng_ctl_copy_string(lsm.session.name, session_name,
2773 sizeof(lsm.session.name));
2774
2775 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2776 if (ret < 0) {
2777 goto end;
2778 }
2779
2780 ret = 0;
2781end:
2782 return ret;
2783}
2784
eded6438
JD
2785/*
2786 * Deprecated, replaced by lttng_regenerate_metadata.
2787 */
2788int lttng_metadata_regenerate(const char *session_name)
2789{
2790 return lttng_regenerate_metadata(session_name);
2791}
2792
c2561365
JD
2793/*
2794 * Regenerate the statedump of a session.
2795 * Return 0 on success, a negative error code on error.
2796 */
2797int lttng_regenerate_statedump(const char *session_name)
2798{
2799 int ret;
2800 struct lttcomm_session_msg lsm;
2801
2802 if (!session_name) {
2803 ret = -LTTNG_ERR_INVALID;
2804 goto end;
2805 }
2806
2807 memset(&lsm, 0, sizeof(lsm));
2808 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2809
2810 lttng_ctl_copy_string(lsm.session.name, session_name,
2811 sizeof(lsm.session.name));
2812
2813 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2814 if (ret < 0) {
2815 goto end;
2816 }
2817
2818 ret = 0;
2819end:
2820 return ret;
2821}
2822
a58c490f
JG
2823int lttng_register_trigger(struct lttng_trigger *trigger)
2824{
2825 int ret;
2826 struct lttcomm_session_msg lsm;
2827 char *trigger_buf = NULL;
2828 ssize_t trigger_size;
2829
2830 if (!trigger) {
2831 ret = -LTTNG_ERR_INVALID;
2832 goto end;
2833 }
2834
2835 if (!lttng_trigger_validate(trigger)) {
eac4828d 2836 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
2837 goto end;
2838 }
2839
2840 trigger_size = lttng_trigger_serialize(trigger, NULL);
2841 if (trigger_size < 0) {
2842 ret = -LTTNG_ERR_UNK;
2843 goto end;
2844 }
2845
2846 trigger_buf = zmalloc(trigger_size);
2847 if (!trigger_buf) {
2848 ret = -LTTNG_ERR_NOMEM;
2849 goto end;
2850 }
2851
2852 memset(&lsm, 0, sizeof(lsm));
2853 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
2854 if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
2855 ret = -LTTNG_ERR_UNK;
2856 goto end;
2857 }
2858
2859 lsm.u.trigger.length = (uint32_t) trigger_size;
2860 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
2861 trigger_size, NULL);
2862end:
2863 free(trigger_buf);
2864 return ret;
2865}
2866
2867int lttng_unregister_trigger(struct lttng_trigger *trigger)
2868{
2869 int ret;
2870 struct lttcomm_session_msg lsm;
2871 char *trigger_buf = NULL;
2872 ssize_t trigger_size;
2873
2874 if (!trigger) {
2875 ret = -LTTNG_ERR_INVALID;
2876 goto end;
2877 }
2878
2879 if (!lttng_trigger_validate(trigger)) {
2880 ret = -LTTNG_ERR_INVALID;
2881 goto end;
2882 }
2883
2884 trigger_size = lttng_trigger_serialize(trigger, NULL);
2885 if (trigger_size < 0) {
2886 ret = -LTTNG_ERR_UNK;
2887 goto end;
2888 }
2889
2890 trigger_buf = zmalloc(trigger_size);
2891 if (!trigger_buf) {
2892 ret = -LTTNG_ERR_NOMEM;
2893 goto end;
2894 }
2895
2896 memset(&lsm, 0, sizeof(lsm));
2897 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
2898 if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
2899 ret = -LTTNG_ERR_UNK;
2900 goto end;
2901 }
2902
2903 lsm.u.trigger.length = (uint32_t) trigger_size;
2904 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
2905 trigger_size, NULL);
2906end:
2907 free(trigger_buf);
2908 return ret;
2909}
2910
d68c9a04
JD
2911int lttng_session_get_current_archive_location(const char *session_name,
2912 char **chunk_path)
2913{
2914 struct lttcomm_session_msg lsm;
2915 struct lttng_session_get_current_output_return *output_return = NULL;
2916 int ret;
2917 size_t path_len;
2918
2919 memset(&lsm, 0, sizeof(lsm));
2920 lsm.cmd_type = LTTNG_SESSION_GET_CURRENT_OUTPUT;
2921 ret = lttng_strncpy(lsm.session.name, session_name,
2922 sizeof(lsm.session.name));
2923 if (ret) {
2924 ret = -LTTNG_ERR_INVALID;
2925 goto end;
2926 }
2927
2928 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &output_return);
2929 if (ret < 0) {
2930 ret = -1;
2931 goto end;
2932 }
2933
2934 path_len = lttng_strnlen(output_return->path,
2935 sizeof(output_return->path));
2936 if (path_len == 0 || path_len == sizeof(output_return->path)) {
2937 ret = -LTTNG_ERR_NO_SESSION_OUTPUT;
2938 goto end;
2939 }
2940
2941 *chunk_path = zmalloc(path_len + 1);
2942 if (!*chunk_path) {
2943 ret = -1;
2944 goto end;
2945 }
2946 memcpy(*chunk_path, output_return->path, path_len);
2947
2948 ret = 0;
2949
2950end:
2951 free(output_return);
2952 return ret;
2953}
2954
fac6795d 2955/*
9ae110e2 2956 * lib constructor.
fac6795d 2957 */
b2b89e8a 2958static void __attribute__((constructor)) init(void)
fac6795d
DG
2959{
2960 /* Set default session group */
bbccc3d2 2961 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 2962}
49cca668
DG
2963
2964/*
9ae110e2 2965 * lib destructor.
49cca668 2966 */
b2b89e8a 2967static void __attribute__((destructor)) lttng_ctl_exit(void)
49cca668
DG
2968{
2969 free(tracing_group);
2970}
This page took 0.258202 seconds and 5 git commands to generate.