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