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