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