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