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