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