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