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