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