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