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