Add client notification API
[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 (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 /* Validate strings used as literals in the expression. */
924 ret = filter_visitor_ir_validate_string(ctx);
925 if (ret) {
926 ret = -LTTNG_ERR_FILTER_INVAL;
927 goto parse_error;
928 }
929 dbg_printf("done\n");
930
931 dbg_printf("Generating bytecode... ");
932 fflush(stdout);
933 ret = filter_visitor_bytecode_generate(ctx);
934 if (ret) {
935 fprintf(stderr, "Generate bytecode error\n");
936 ret = -LTTNG_ERR_FILTER_INVAL;
937 goto parse_error;
938 }
939 dbg_printf("done\n");
940 dbg_printf("Size of bytecode generated: %u bytes.\n",
941 bytecode_get_len(&ctx->bytecode->b));
942
943 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
944 + bytecode_get_len(&ctx->bytecode->b);
945 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
946
947 /* No need to keep the memory stream. */
948 if (fclose(fmem) != 0) {
949 PERROR("fclose");
950 }
951
952 *ctxp = ctx;
953 return 0;
954
955 parse_error:
956 filter_ir_free(ctx);
957 filter_parser_ctx_free(ctx);
958 filter_alloc_error:
959 if (fclose(fmem) != 0) {
960 PERROR("fclose");
961 }
962 error:
963 return ret;
964 }
965
966 /*
967 * Enable event(s) for a channel, possibly with exclusions and a filter.
968 * If no event name is specified, all events are enabled.
969 * If no channel name is specified, the default name is used.
970 * If filter expression is not NULL, the filter is set for the event.
971 * If exclusion count is not zero, the exclusions are set for the event.
972 * Returns size of returned session payload data or a negative error code.
973 */
974 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
975 struct lttng_event *ev, const char *channel_name,
976 const char *original_filter_expression,
977 int exclusion_count, char **exclusion_list)
978 {
979 struct lttcomm_session_msg lsm;
980 char *varlen_data;
981 int ret = 0;
982 unsigned int free_filter_expression = 0;
983 struct filter_parser_ctx *ctx = NULL;
984 /*
985 * Cast as non-const since we may replace the filter expression
986 * by a dynamically allocated string. Otherwise, the original
987 * string is not modified.
988 */
989 char *filter_expression = (char *) original_filter_expression;
990
991 if (handle == NULL || ev == NULL) {
992 ret = -LTTNG_ERR_INVALID;
993 goto error;
994 }
995
996 /*
997 * Empty filter string will always be rejected by the parser
998 * anyway, so treat this corner-case early to eliminate
999 * lttng_fmemopen error for 0-byte allocation.
1000 */
1001 if (filter_expression && filter_expression[0] == '\0') {
1002 ret = -LTTNG_ERR_INVALID;
1003 goto error;
1004 }
1005
1006 memset(&lsm, 0, sizeof(lsm));
1007
1008 /* If no channel name, send empty string. */
1009 if (channel_name == NULL) {
1010 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1011 sizeof(lsm.u.enable.channel_name));
1012 } else {
1013 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1014 sizeof(lsm.u.enable.channel_name));
1015 }
1016
1017 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1018 if (ev->name[0] == '\0') {
1019 /* Enable all events */
1020 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
1021 }
1022
1023 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1024 /* FIXME: copying non-packed struct to packed struct. */
1025 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1026
1027 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1028 sizeof(lsm.session.name));
1029 lsm.u.enable.exclusion_count = exclusion_count;
1030 lsm.u.enable.bytecode_len = 0;
1031
1032 /*
1033 * For the JUL domain, a filter is enforced except for the enable all
1034 * event. This is done to avoid having the event in all sessions thus
1035 * filtering by logger name.
1036 */
1037 if (exclusion_count == 0 && filter_expression == NULL &&
1038 (handle->domain.type != LTTNG_DOMAIN_JUL &&
1039 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1040 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
1041 goto ask_sessiond;
1042 }
1043
1044 /*
1045 * We have either a filter or some exclusions, so we need to set up
1046 * a variable-length memory block from where to send the data.
1047 */
1048
1049 /* Parse filter expression. */
1050 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1051 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1052 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1053 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1054 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1055 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1056 char *agent_filter;
1057
1058 /* Setup JUL filter if needed. */
1059 agent_filter = set_agent_filter(filter_expression, ev);
1060 if (!agent_filter) {
1061 if (!filter_expression) {
1062 /*
1063 * No JUL and no filter, just skip
1064 * everything below.
1065 */
1066 goto ask_sessiond;
1067 }
1068 } else {
1069 /*
1070 * With an agent filter, the original filter has
1071 * been added to it thus replace the filter
1072 * expression.
1073 */
1074 filter_expression = agent_filter;
1075 free_filter_expression = 1;
1076 }
1077 }
1078
1079 ret = generate_filter(filter_expression, &lsm, &ctx);
1080 if (ret) {
1081 goto filter_error;
1082 }
1083 }
1084
1085 varlen_data = zmalloc(lsm.u.enable.bytecode_len
1086 + lsm.u.enable.expression_len
1087 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
1088 if (!varlen_data) {
1089 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1090 goto mem_error;
1091 }
1092
1093 /* Put exclusion names first in the data. */
1094 while (exclusion_count--) {
1095 strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count,
1096 *(exclusion_list + exclusion_count),
1097 LTTNG_SYMBOL_NAME_LEN - 1);
1098 }
1099 /* Add filter expression next. */
1100 if (lsm.u.enable.expression_len != 0) {
1101 memcpy(varlen_data
1102 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count,
1103 filter_expression,
1104 lsm.u.enable.expression_len);
1105 }
1106 /* Add filter bytecode next. */
1107 if (ctx && lsm.u.enable.bytecode_len != 0) {
1108 memcpy(varlen_data
1109 + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count
1110 + lsm.u.enable.expression_len,
1111 &ctx->bytecode->b,
1112 lsm.u.enable.bytecode_len);
1113 }
1114
1115 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
1116 (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
1117 lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
1118 NULL);
1119 free(varlen_data);
1120
1121 mem_error:
1122 if (filter_expression && ctx) {
1123 filter_bytecode_free(ctx);
1124 filter_ir_free(ctx);
1125 filter_parser_ctx_free(ctx);
1126 }
1127 filter_error:
1128 if (free_filter_expression) {
1129 /*
1130 * The filter expression has been replaced and must be freed as
1131 * it is not the original filter expression received as a
1132 * parameter.
1133 */
1134 free(filter_expression);
1135 }
1136 error:
1137 /*
1138 * Return directly to the caller and don't ask the sessiond since
1139 * something went wrong in the parsing of data above.
1140 */
1141 return ret;
1142
1143 ask_sessiond:
1144 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1145 return ret;
1146 }
1147
1148 int lttng_disable_event_ext(struct lttng_handle *handle,
1149 struct lttng_event *ev, const char *channel_name,
1150 const char *original_filter_expression)
1151 {
1152 struct lttcomm_session_msg lsm;
1153 char *varlen_data;
1154 int ret = 0;
1155 unsigned int free_filter_expression = 0;
1156 struct filter_parser_ctx *ctx = NULL;
1157 /*
1158 * Cast as non-const since we may replace the filter expression
1159 * by a dynamically allocated string. Otherwise, the original
1160 * string is not modified.
1161 */
1162 char *filter_expression = (char *) original_filter_expression;
1163
1164 if (handle == NULL || ev == NULL) {
1165 ret = -LTTNG_ERR_INVALID;
1166 goto error;
1167 }
1168
1169 /*
1170 * Empty filter string will always be rejected by the parser
1171 * anyway, so treat this corner-case early to eliminate
1172 * lttng_fmemopen error for 0-byte allocation.
1173 */
1174 if (filter_expression && filter_expression[0] == '\0') {
1175 ret = -LTTNG_ERR_INVALID;
1176 goto error;
1177 }
1178
1179 memset(&lsm, 0, sizeof(lsm));
1180
1181 /* If no channel name, send empty string. */
1182 if (channel_name == NULL) {
1183 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
1184 sizeof(lsm.u.disable.channel_name));
1185 } else {
1186 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
1187 sizeof(lsm.u.disable.channel_name));
1188 }
1189
1190 lsm.cmd_type = LTTNG_DISABLE_EVENT;
1191
1192 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1193 /* FIXME: copying non-packed struct to packed struct. */
1194 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1195
1196 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1197 sizeof(lsm.session.name));
1198 lsm.u.disable.bytecode_len = 0;
1199
1200 /*
1201 * For the JUL domain, a filter is enforced except for the
1202 * disable all event. This is done to avoid having the event in
1203 * all sessions thus filtering by logger name.
1204 */
1205 if (filter_expression == NULL &&
1206 (handle->domain.type != LTTNG_DOMAIN_JUL &&
1207 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1208 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
1209 goto ask_sessiond;
1210 }
1211
1212 /*
1213 * We have a filter, so we need to set up a variable-length
1214 * memory block from where to send the data.
1215 */
1216
1217 /* Parse filter expression */
1218 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1219 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1220 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1221 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1222 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1223 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1224 char *agent_filter;
1225
1226 /* Setup JUL filter if needed. */
1227 agent_filter = set_agent_filter(filter_expression, ev);
1228 if (!agent_filter) {
1229 if (!filter_expression) {
1230 /*
1231 * No JUL and no filter, just skip
1232 * everything below.
1233 */
1234 goto ask_sessiond;
1235 }
1236 } else {
1237 /*
1238 * With a JUL filter, the original filter has
1239 * been added to it thus replace the filter
1240 * expression.
1241 */
1242 filter_expression = agent_filter;
1243 free_filter_expression = 1;
1244 }
1245 }
1246
1247 ret = generate_filter(filter_expression, &lsm, &ctx);
1248 if (ret) {
1249 goto filter_error;
1250 }
1251 }
1252
1253 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1254 + lsm.u.disable.expression_len);
1255 if (!varlen_data) {
1256 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1257 goto mem_error;
1258 }
1259
1260 /* Add filter expression. */
1261 if (lsm.u.disable.expression_len != 0) {
1262 memcpy(varlen_data,
1263 filter_expression,
1264 lsm.u.disable.expression_len);
1265 }
1266 /* Add filter bytecode next. */
1267 if (ctx && lsm.u.disable.bytecode_len != 0) {
1268 memcpy(varlen_data
1269 + lsm.u.disable.expression_len,
1270 &ctx->bytecode->b,
1271 lsm.u.disable.bytecode_len);
1272 }
1273
1274 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
1275 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1276 free(varlen_data);
1277
1278 mem_error:
1279 if (filter_expression && ctx) {
1280 filter_bytecode_free(ctx);
1281 filter_ir_free(ctx);
1282 filter_parser_ctx_free(ctx);
1283 }
1284 filter_error:
1285 if (free_filter_expression) {
1286 /*
1287 * The filter expression has been replaced and must be freed as
1288 * it is not the original filter expression received as a
1289 * parameter.
1290 */
1291 free(filter_expression);
1292 }
1293 error:
1294 /*
1295 * Return directly to the caller and don't ask the sessiond since
1296 * something went wrong in the parsing of data above.
1297 */
1298 return ret;
1299
1300 ask_sessiond:
1301 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1302 return ret;
1303 }
1304
1305 /*
1306 * Disable event(s) of a channel and domain.
1307 * If no event name is specified, all events are disabled.
1308 * If no channel name is specified, the default 'channel0' is used.
1309 * Returns size of returned session payload data or a negative error code.
1310 */
1311 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1312 const char *channel_name)
1313 {
1314 struct lttng_event ev;
1315
1316 memset(&ev, 0, sizeof(ev));
1317 ev.loglevel = -1;
1318 ev.type = LTTNG_EVENT_ALL;
1319 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1320 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1321 }
1322
1323 /*
1324 * Enable channel per domain
1325 * Returns size of returned session payload data or a negative error code.
1326 */
1327 int lttng_enable_channel(struct lttng_handle *handle,
1328 struct lttng_channel *chan)
1329 {
1330 struct lttcomm_session_msg lsm;
1331
1332 /* NULL arguments are forbidden. No default values. */
1333 if (handle == NULL || chan == NULL) {
1334 return -LTTNG_ERR_INVALID;
1335 }
1336
1337 memset(&lsm, 0, sizeof(lsm));
1338
1339 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
1340
1341 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1342
1343 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1344
1345 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1346 sizeof(lsm.session.name));
1347
1348 return lttng_ctl_ask_sessiond(&lsm, NULL);
1349 }
1350
1351 /*
1352 * All tracing will be stopped for registered events of the channel.
1353 * Returns size of returned session payload data or a negative error code.
1354 */
1355 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1356 {
1357 struct lttcomm_session_msg lsm;
1358
1359 /* Safety check. Both are mandatory. */
1360 if (handle == NULL || name == NULL) {
1361 return -LTTNG_ERR_INVALID;
1362 }
1363
1364 memset(&lsm, 0, sizeof(lsm));
1365
1366 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1367
1368 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
1369 sizeof(lsm.u.disable.channel_name));
1370
1371 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1372
1373 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1374 sizeof(lsm.session.name));
1375
1376 return lttng_ctl_ask_sessiond(&lsm, NULL);
1377 }
1378
1379 /*
1380 * Add PID to session tracker.
1381 * Return 0 on success else a negative LTTng error code.
1382 */
1383 int lttng_track_pid(struct lttng_handle *handle, int pid)
1384 {
1385 struct lttcomm_session_msg lsm;
1386
1387 /* NULL arguments are forbidden. No default values. */
1388 if (handle == NULL) {
1389 return -LTTNG_ERR_INVALID;
1390 }
1391
1392 memset(&lsm, 0, sizeof(lsm));
1393
1394 lsm.cmd_type = LTTNG_TRACK_PID;
1395 lsm.u.pid_tracker.pid = pid;
1396
1397 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1398
1399 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1400 sizeof(lsm.session.name));
1401
1402 return lttng_ctl_ask_sessiond(&lsm, NULL);
1403 }
1404
1405 /*
1406 * Remove PID from session tracker.
1407 * Return 0 on success else a negative LTTng error code.
1408 */
1409 int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1410 {
1411 struct lttcomm_session_msg lsm;
1412
1413 /* NULL arguments are forbidden. No default values. */
1414 if (handle == NULL) {
1415 return -LTTNG_ERR_INVALID;
1416 }
1417
1418 memset(&lsm, 0, sizeof(lsm));
1419
1420 lsm.cmd_type = LTTNG_UNTRACK_PID;
1421 lsm.u.pid_tracker.pid = pid;
1422
1423 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1424
1425 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1426 sizeof(lsm.session.name));
1427
1428 return lttng_ctl_ask_sessiond(&lsm, NULL);
1429 }
1430
1431 /*
1432 * Lists all available tracepoints of domain.
1433 * Sets the contents of the events array.
1434 * Returns the number of lttng_event entries in events;
1435 * on error, returns a negative value.
1436 */
1437 int lttng_list_tracepoints(struct lttng_handle *handle,
1438 struct lttng_event **events)
1439 {
1440 int ret;
1441 struct lttcomm_session_msg lsm;
1442
1443 if (handle == NULL) {
1444 return -LTTNG_ERR_INVALID;
1445 }
1446
1447 memset(&lsm, 0, sizeof(lsm));
1448 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1449 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1450
1451 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1452 if (ret < 0) {
1453 return ret;
1454 }
1455
1456 return ret / sizeof(struct lttng_event);
1457 }
1458
1459 /*
1460 * Lists all available tracepoint fields of domain.
1461 * Sets the contents of the event field array.
1462 * Returns the number of lttng_event_field entries in events;
1463 * on error, returns a negative value.
1464 */
1465 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1466 struct lttng_event_field **fields)
1467 {
1468 int ret;
1469 struct lttcomm_session_msg lsm;
1470
1471 if (handle == NULL) {
1472 return -LTTNG_ERR_INVALID;
1473 }
1474
1475 memset(&lsm, 0, sizeof(lsm));
1476 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1477 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1478
1479 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
1480 if (ret < 0) {
1481 return ret;
1482 }
1483
1484 return ret / sizeof(struct lttng_event_field);
1485 }
1486
1487 /*
1488 * Lists all available kernel system calls. Allocates and sets the contents of
1489 * the events array.
1490 *
1491 * Returns the number of lttng_event entries in events; on error, returns a
1492 * negative value.
1493 */
1494 int lttng_list_syscalls(struct lttng_event **events)
1495 {
1496 int ret;
1497 struct lttcomm_session_msg lsm;
1498
1499 if (!events) {
1500 return -LTTNG_ERR_INVALID;
1501 }
1502
1503 memset(&lsm, 0, sizeof(lsm));
1504 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1505 /* Force kernel domain for system calls. */
1506 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1507
1508 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1509 if (ret < 0) {
1510 return ret;
1511 }
1512
1513 return ret / sizeof(struct lttng_event);
1514 }
1515
1516 /*
1517 * Returns a human readable string describing
1518 * the error code (a negative value).
1519 */
1520 const char *lttng_strerror(int code)
1521 {
1522 return error_get_str(code);
1523 }
1524
1525 /*
1526 * Create a brand new session using name and url for destination.
1527 *
1528 * Returns LTTNG_OK on success or a negative error code.
1529 */
1530 int lttng_create_session(const char *name, const char *url)
1531 {
1532 int ret;
1533 ssize_t size;
1534 struct lttcomm_session_msg lsm;
1535 struct lttng_uri *uris = NULL;
1536
1537 if (name == NULL) {
1538 return -LTTNG_ERR_INVALID;
1539 }
1540
1541 memset(&lsm, 0, sizeof(lsm));
1542
1543 lsm.cmd_type = LTTNG_CREATE_SESSION;
1544 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1545
1546 /* There should never be a data URL */
1547 size = uri_parse_str_urls(url, NULL, &uris);
1548 if (size < 0) {
1549 return -LTTNG_ERR_INVALID;
1550 }
1551
1552 lsm.u.uri.size = size;
1553
1554 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
1555 sizeof(struct lttng_uri) * size, NULL);
1556
1557 free(uris);
1558 return ret;
1559 }
1560
1561 /*
1562 * Destroy session using name.
1563 * Returns size of returned session payload data or a negative error code.
1564 */
1565 static
1566 int _lttng_destroy_session(const char *session_name)
1567 {
1568 struct lttcomm_session_msg lsm;
1569
1570 if (session_name == NULL) {
1571 return -LTTNG_ERR_INVALID;
1572 }
1573
1574 memset(&lsm, 0, sizeof(lsm));
1575 lsm.cmd_type = LTTNG_DESTROY_SESSION;
1576
1577 lttng_ctl_copy_string(lsm.session.name, session_name,
1578 sizeof(lsm.session.name));
1579
1580 return lttng_ctl_ask_sessiond(&lsm, NULL);
1581 }
1582
1583 /*
1584 * Stop the session and wait for the data before destroying it
1585 */
1586 int lttng_destroy_session(const char *session_name)
1587 {
1588 int ret;
1589
1590 /*
1591 * Stop the tracing and wait for the data.
1592 */
1593 ret = _lttng_stop_tracing(session_name, 1);
1594 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1595 goto end;
1596 }
1597
1598 ret = _lttng_destroy_session(session_name);
1599 end:
1600 return ret;
1601 }
1602
1603 /*
1604 * Destroy the session without waiting for the data.
1605 */
1606 int lttng_destroy_session_no_wait(const char *session_name)
1607 {
1608 int ret;
1609
1610 /*
1611 * Stop the tracing without waiting for the data.
1612 * The session might already have been stopped, so just
1613 * skip this error.
1614 */
1615 ret = _lttng_stop_tracing(session_name, 0);
1616 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1617 goto end;
1618 }
1619
1620 ret = _lttng_destroy_session(session_name);
1621 end:
1622 return ret;
1623 }
1624
1625 /*
1626 * Ask the session daemon for all available sessions.
1627 * Sets the contents of the sessions array.
1628 * Returns the number of lttng_session entries in sessions;
1629 * on error, returns a negative value.
1630 */
1631 int lttng_list_sessions(struct lttng_session **sessions)
1632 {
1633 int ret;
1634 struct lttcomm_session_msg lsm;
1635
1636 memset(&lsm, 0, sizeof(lsm));
1637 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1638 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
1639 if (ret < 0) {
1640 return ret;
1641 }
1642
1643 return ret / sizeof(struct lttng_session);
1644 }
1645
1646 int lttng_set_session_shm_path(const char *session_name,
1647 const char *shm_path)
1648 {
1649 struct lttcomm_session_msg lsm;
1650
1651 if (session_name == NULL) {
1652 return -LTTNG_ERR_INVALID;
1653 }
1654
1655 memset(&lsm, 0, sizeof(lsm));
1656 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1657
1658 lttng_ctl_copy_string(lsm.session.name, session_name,
1659 sizeof(lsm.session.name));
1660 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1661 sizeof(lsm.u.set_shm_path.shm_path));
1662
1663 return lttng_ctl_ask_sessiond(&lsm, NULL);
1664 }
1665
1666 /*
1667 * Ask the session daemon for all available domains of a session.
1668 * Sets the contents of the domains array.
1669 * Returns the number of lttng_domain entries in domains;
1670 * on error, returns a negative value.
1671 */
1672 int lttng_list_domains(const char *session_name,
1673 struct lttng_domain **domains)
1674 {
1675 int ret;
1676 struct lttcomm_session_msg lsm;
1677
1678 if (session_name == NULL) {
1679 return -LTTNG_ERR_INVALID;
1680 }
1681
1682 memset(&lsm, 0, sizeof(lsm));
1683 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1684
1685 lttng_ctl_copy_string(lsm.session.name, session_name,
1686 sizeof(lsm.session.name));
1687
1688 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
1689 if (ret < 0) {
1690 return ret;
1691 }
1692
1693 return ret / sizeof(struct lttng_domain);
1694 }
1695
1696 /*
1697 * Ask the session daemon for all available channels of a session.
1698 * Sets the contents of the channels array.
1699 * Returns the number of lttng_channel entries in channels;
1700 * on error, returns a negative value.
1701 */
1702 int lttng_list_channels(struct lttng_handle *handle,
1703 struct lttng_channel **channels)
1704 {
1705 int ret;
1706 size_t channel_count, i;
1707 const size_t channel_size = sizeof(struct lttng_channel) +
1708 sizeof(struct lttcomm_channel_extended);
1709 struct lttcomm_session_msg lsm;
1710 void *extended_at;
1711
1712 if (handle == NULL) {
1713 ret = -LTTNG_ERR_INVALID;
1714 goto end;
1715 }
1716
1717 memset(&lsm, 0, sizeof(lsm));
1718 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1719 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1720 sizeof(lsm.session.name));
1721
1722 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1723
1724 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
1725 if (ret < 0) {
1726 goto end;
1727 }
1728
1729 if (ret % channel_size) {
1730 ret = -LTTNG_ERR_UNK;
1731 free(*channels);
1732 *channels = NULL;
1733 goto end;
1734 }
1735 channel_count = (size_t) ret / channel_size;
1736
1737 /* Set extended info pointers */
1738 extended_at = ((void *) *channels) +
1739 channel_count * sizeof(struct lttng_channel);
1740 for (i = 0; i < channel_count; i++) {
1741 struct lttng_channel *chan = &(*channels)[i];
1742
1743 chan->attr.extended.ptr = extended_at;
1744 extended_at += sizeof(struct lttcomm_channel_extended);
1745 }
1746
1747 ret = (int) channel_count;
1748 end:
1749 return ret;
1750 }
1751
1752 /*
1753 * Ask the session daemon for all available events of a session channel.
1754 * Sets the contents of the events array.
1755 * Returns the number of lttng_event entries in events;
1756 * on error, returns a negative value.
1757 */
1758 int lttng_list_events(struct lttng_handle *handle,
1759 const char *channel_name, struct lttng_event **events)
1760 {
1761 int ret;
1762 struct lttcomm_session_msg lsm;
1763 struct lttcomm_event_command_header *cmd_header = NULL;
1764 size_t cmd_header_len;
1765 uint32_t nb_events, i;
1766 void *extended_at;
1767
1768 /* Safety check. An handle and channel name are mandatory */
1769 if (handle == NULL || channel_name == NULL) {
1770 return -LTTNG_ERR_INVALID;
1771 }
1772
1773 memset(&lsm, 0, sizeof(lsm));
1774 lsm.cmd_type = LTTNG_LIST_EVENTS;
1775 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1776 sizeof(lsm.session.name));
1777 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
1778 sizeof(lsm.u.list.channel_name));
1779 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1780
1781 ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) events,
1782 (void **) &cmd_header, &cmd_header_len);
1783 if (ret < 0) {
1784 goto error;
1785 }
1786
1787 /* Set number of events and free command header */
1788 nb_events = cmd_header->nb_events;
1789 if (nb_events > INT_MAX) {
1790 ret = -EOVERFLOW;
1791 goto error;
1792 }
1793 ret = (int) nb_events;
1794 free(cmd_header);
1795 cmd_header = NULL;
1796
1797 /* Set extended info pointers */
1798 extended_at = ((void*) (*events)) +
1799 nb_events * sizeof(struct lttng_event);
1800
1801 for (i = 0; i < nb_events; i++) {
1802 struct lttcomm_event_extended_header *ext_header;
1803 struct lttng_event *event = &(*events)[i];
1804
1805 event->extended.ptr = extended_at;
1806 ext_header =
1807 (struct lttcomm_event_extended_header *) extended_at;
1808 extended_at += sizeof(*ext_header);
1809 extended_at += ext_header->filter_len;
1810 extended_at +=
1811 ext_header->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
1812 }
1813
1814 return ret;
1815 error:
1816 free(cmd_header);
1817 free(*events);
1818 return ret;
1819 }
1820
1821 int lttng_event_get_filter_expression(struct lttng_event *event,
1822 const char **filter_expression)
1823 {
1824 int ret = 0;
1825 struct lttcomm_event_extended_header *ext_header;
1826
1827 if (!event || !filter_expression) {
1828 ret = -LTTNG_ERR_INVALID;
1829 goto end;
1830 }
1831
1832 ext_header = event->extended.ptr;
1833
1834 if (!ext_header) {
1835 /*
1836 * This can happen since the lttng_event structure is
1837 * used for other tasks where this pointer is never set.
1838 */
1839 *filter_expression = NULL;
1840 goto end;
1841 }
1842
1843 if (ext_header->filter_len) {
1844 *filter_expression = ((const char *) (ext_header)) +
1845 sizeof(*ext_header);
1846 } else {
1847 *filter_expression = NULL;
1848 }
1849
1850 end:
1851 return ret;
1852 }
1853
1854 int lttng_event_get_exclusion_name_count(struct lttng_event *event)
1855 {
1856 int ret;
1857 struct lttcomm_event_extended_header *ext_header;
1858
1859 if (!event) {
1860 ret = -LTTNG_ERR_INVALID;
1861 goto end;
1862 }
1863
1864 ext_header = event->extended.ptr;
1865 if (!ext_header) {
1866 /*
1867 * This can happen since the lttng_event structure is
1868 * used for other tasks where this pointer is never set.
1869 */
1870 ret = 0;
1871 goto end;
1872 }
1873
1874 if (ext_header->nb_exclusions > INT_MAX) {
1875 ret = -LTTNG_ERR_OVERFLOW;
1876 goto end;
1877 }
1878 ret = (int) ext_header->nb_exclusions;
1879 end:
1880 return ret;
1881 }
1882
1883 int lttng_event_get_exclusion_name(struct lttng_event *event,
1884 size_t index, const char **exclusion_name)
1885 {
1886 int ret = 0;
1887 struct lttcomm_event_extended_header *ext_header;
1888 void *at;
1889
1890 if (!event || !exclusion_name) {
1891 ret = -LTTNG_ERR_INVALID;
1892 goto end;
1893 }
1894
1895 ext_header = event->extended.ptr;
1896 if (!ext_header) {
1897 ret = -LTTNG_ERR_INVALID;
1898 goto end;
1899 }
1900
1901 if (index >= ext_header->nb_exclusions) {
1902 ret = -LTTNG_ERR_INVALID;
1903 goto end;
1904 }
1905
1906 at = (void *) ext_header + sizeof(*ext_header);
1907 at += ext_header->filter_len;
1908 at += index * LTTNG_SYMBOL_NAME_LEN;
1909 *exclusion_name = at;
1910
1911 end:
1912 return ret;
1913 }
1914
1915 /*
1916 * Sets the tracing_group variable with name.
1917 * This function allocates memory pointed to by tracing_group.
1918 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1919 */
1920 int lttng_set_tracing_group(const char *name)
1921 {
1922 if (name == NULL) {
1923 return -LTTNG_ERR_INVALID;
1924 }
1925
1926 if (asprintf(&tracing_group, "%s", name) < 0) {
1927 return -LTTNG_ERR_FATAL;
1928 }
1929
1930 return 0;
1931 }
1932
1933 int lttng_calibrate(struct lttng_handle *handle,
1934 struct lttng_calibrate *calibrate)
1935 {
1936 /*
1937 * This command was removed in LTTng 2.9.
1938 */
1939 return -LTTNG_ERR_UND;
1940 }
1941
1942 /*
1943 * Set default channel attributes.
1944 * If either or both of the arguments are null, attr content is zeroe'd.
1945 */
1946 void lttng_channel_set_default_attr(struct lttng_domain *domain,
1947 struct lttng_channel_attr *attr)
1948 {
1949 /* Safety check */
1950 if (attr == NULL || domain == NULL) {
1951 return;
1952 }
1953
1954 memset(attr, 0, sizeof(struct lttng_channel_attr));
1955
1956 /* Same for all domains. */
1957 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1958 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1959 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1960
1961 switch (domain->type) {
1962 case LTTNG_DOMAIN_KERNEL:
1963 attr->switch_timer_interval =
1964 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1965 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
1966 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
1967 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1968 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1969 break;
1970 case LTTNG_DOMAIN_UST:
1971 switch (domain->buf_type) {
1972 case LTTNG_BUFFER_PER_UID:
1973 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1974 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1975 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1976 attr->switch_timer_interval =
1977 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1978 attr->read_timer_interval =
1979 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1980 break;
1981 case LTTNG_BUFFER_PER_PID:
1982 default:
1983 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1984 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1985 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1986 attr->switch_timer_interval =
1987 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1988 attr->read_timer_interval =
1989 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1990 break;
1991 }
1992 default:
1993 /* Default behavior: leave set to 0. */
1994 break;
1995 }
1996 }
1997
1998 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
1999 uint64_t *discarded_events)
2000 {
2001 int ret = 0;
2002 struct lttcomm_channel_extended *chan_ext;
2003
2004 if (!channel || !discarded_events) {
2005 ret = -LTTNG_ERR_INVALID;
2006 goto end;
2007 }
2008
2009 chan_ext = channel->attr.extended.ptr;
2010 if (!chan_ext) {
2011 /*
2012 * This can happen since the lttng_channel structure is
2013 * used for other tasks where this pointer is never set.
2014 */
2015 *discarded_events = 0;
2016 goto end;
2017 }
2018
2019 *discarded_events = chan_ext->discarded_events;
2020 end:
2021 return ret;
2022 }
2023
2024 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2025 uint64_t *lost_packets)
2026 {
2027 int ret = 0;
2028 struct lttcomm_channel_extended *chan_ext;
2029
2030 if (!channel || !lost_packets) {
2031 ret = -LTTNG_ERR_INVALID;
2032 goto end;
2033 }
2034
2035 chan_ext = channel->attr.extended.ptr;
2036 if (!chan_ext) {
2037 /*
2038 * This can happen since the lttng_channel structure is
2039 * used for other tasks where this pointer is never set.
2040 */
2041 *lost_packets = 0;
2042 goto end;
2043 }
2044
2045 *lost_packets = chan_ext->lost_packets;
2046 end:
2047 return ret;
2048 }
2049
2050 /*
2051 * Check if session daemon is alive.
2052 *
2053 * Return 1 if alive or 0 if not.
2054 * On error returns a negative value.
2055 */
2056 int lttng_session_daemon_alive(void)
2057 {
2058 int ret;
2059
2060 ret = set_session_daemon_path();
2061 if (ret < 0) {
2062 /* Error. */
2063 return ret;
2064 }
2065
2066 if (*sessiond_sock_path == '\0') {
2067 /*
2068 * No socket path set. Weird error which means the constructor
2069 * was not called.
2070 */
2071 assert(0);
2072 }
2073
2074 ret = try_connect_sessiond(sessiond_sock_path);
2075 if (ret < 0) {
2076 /* Not alive. */
2077 return 0;
2078 }
2079
2080 /* Is alive. */
2081 return 1;
2082 }
2083
2084 /*
2085 * Set URL for a consumer for a session and domain.
2086 *
2087 * Return 0 on success, else a negative value.
2088 */
2089 int lttng_set_consumer_url(struct lttng_handle *handle,
2090 const char *control_url, const char *data_url)
2091 {
2092 int ret;
2093 ssize_t size;
2094 struct lttcomm_session_msg lsm;
2095 struct lttng_uri *uris = NULL;
2096
2097 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2098 return -LTTNG_ERR_INVALID;
2099 }
2100
2101 memset(&lsm, 0, sizeof(lsm));
2102
2103 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2104
2105 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2106 sizeof(lsm.session.name));
2107 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2108
2109 size = uri_parse_str_urls(control_url, data_url, &uris);
2110 if (size < 0) {
2111 return -LTTNG_ERR_INVALID;
2112 }
2113
2114 lsm.u.uri.size = size;
2115
2116 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2117 sizeof(struct lttng_uri) * size, NULL);
2118
2119 free(uris);
2120 return ret;
2121 }
2122
2123 /*
2124 * [OBSOLETE]
2125 */
2126 int lttng_enable_consumer(struct lttng_handle *handle)
2127 {
2128 return -ENOSYS;
2129 }
2130
2131 /*
2132 * [OBSOLETE]
2133 */
2134 int lttng_disable_consumer(struct lttng_handle *handle)
2135 {
2136 return -ENOSYS;
2137 }
2138
2139 /*
2140 * This is an extension of create session that is ONLY and SHOULD only be used
2141 * by the lttng command line program. It exists to avoid using URI parsing in
2142 * the lttng client.
2143 *
2144 * We need the date and time for the trace path subdirectory for the case where
2145 * the user does NOT define one using either -o or -U. Using the normal
2146 * lttng_create_session API call, we have no clue on the session daemon side if
2147 * the URL was generated automatically by the client or define by the user.
2148 *
2149 * So this function "wrapper" is hidden from the public API, takes the datetime
2150 * string and appends it if necessary to the URI subdirectory before sending it
2151 * to the session daemon.
2152 *
2153 * With this extra function, the lttng_create_session call behavior is not
2154 * changed and the timestamp is appended to the URI on the session daemon side
2155 * if necessary.
2156 */
2157 int _lttng_create_session_ext(const char *name, const char *url,
2158 const char *datetime)
2159 {
2160 int ret;
2161 ssize_t size;
2162 struct lttcomm_session_msg lsm;
2163 struct lttng_uri *uris = NULL;
2164
2165 if (name == NULL || datetime == NULL) {
2166 return -LTTNG_ERR_INVALID;
2167 }
2168
2169 memset(&lsm, 0, sizeof(lsm));
2170
2171 lsm.cmd_type = LTTNG_CREATE_SESSION;
2172 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2173
2174 /* There should never be a data URL. */
2175 size = uri_parse_str_urls(url, NULL, &uris);
2176 if (size < 0) {
2177 ret = -LTTNG_ERR_INVALID;
2178 goto error;
2179 }
2180
2181 lsm.u.uri.size = size;
2182
2183 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
2184 /* Don't append datetime if the name was automatically created. */
2185 if (strncmp(name, DEFAULT_SESSION_NAME "-",
2186 strlen(DEFAULT_SESSION_NAME) + 1)) {
2187 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
2188 name, datetime);
2189 } else {
2190 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2191 }
2192 if (ret < 0) {
2193 PERROR("snprintf uri subdir");
2194 ret = -LTTNG_ERR_FATAL;
2195 goto error;
2196 }
2197 }
2198
2199 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2200 sizeof(struct lttng_uri) * size, NULL);
2201
2202 error:
2203 free(uris);
2204 return ret;
2205 }
2206
2207 /*
2208 * For a given session name, this call checks if the data is ready to be read
2209 * or is still being extracted by the consumer(s) hence not ready to be used by
2210 * any readers.
2211 */
2212 int lttng_data_pending(const char *session_name)
2213 {
2214 int ret;
2215 struct lttcomm_session_msg lsm;
2216 uint8_t *pending = NULL;
2217
2218 if (session_name == NULL) {
2219 return -LTTNG_ERR_INVALID;
2220 }
2221
2222 memset(&lsm, 0, sizeof(lsm));
2223 lsm.cmd_type = LTTNG_DATA_PENDING;
2224
2225 lttng_ctl_copy_string(lsm.session.name, session_name,
2226 sizeof(lsm.session.name));
2227
2228 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2229 if (ret < 0) {
2230 goto end;
2231 } else if (ret != 1) {
2232 /* Unexpected payload size */
2233 ret = -LTTNG_ERR_INVALID;
2234 goto end;
2235 }
2236
2237 ret = (int) *pending;
2238 end:
2239 free(pending);
2240 return ret;
2241 }
2242
2243 /*
2244 * Create a session exclusively used for snapshot.
2245 *
2246 * Returns LTTNG_OK on success or a negative error code.
2247 */
2248 int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2249 {
2250 int ret;
2251 ssize_t size;
2252 struct lttcomm_session_msg lsm;
2253 struct lttng_uri *uris = NULL;
2254
2255 if (name == NULL) {
2256 return -LTTNG_ERR_INVALID;
2257 }
2258
2259 memset(&lsm, 0, sizeof(lsm));
2260
2261 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
2262 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2263
2264 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2265 if (size < 0) {
2266 return -LTTNG_ERR_INVALID;
2267 }
2268
2269 lsm.u.uri.size = size;
2270
2271 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2272 sizeof(struct lttng_uri) * size, NULL);
2273
2274 free(uris);
2275 return ret;
2276 }
2277
2278 /*
2279 * Create a session exclusively used for live.
2280 *
2281 * Returns LTTNG_OK on success or a negative error code.
2282 */
2283 int lttng_create_session_live(const char *name, const char *url,
2284 unsigned int timer_interval)
2285 {
2286 int ret;
2287 ssize_t size;
2288 struct lttcomm_session_msg lsm;
2289 struct lttng_uri *uris = NULL;
2290
2291 if (name == NULL || timer_interval == 0) {
2292 return -LTTNG_ERR_INVALID;
2293 }
2294
2295 memset(&lsm, 0, sizeof(lsm));
2296
2297 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
2298 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2299
2300 if (url) {
2301 size = uri_parse_str_urls(url, NULL, &uris);
2302 if (size <= 0) {
2303 ret = -LTTNG_ERR_INVALID;
2304 goto end;
2305 }
2306
2307 /* file:// is not accepted for live session. */
2308 if (uris[0].dtype == LTTNG_DST_PATH) {
2309 ret = -LTTNG_ERR_INVALID;
2310 goto end;
2311 }
2312 } else {
2313 size = 0;
2314 }
2315
2316 lsm.u.session_live.nb_uri = size;
2317 lsm.u.session_live.timer_interval = timer_interval;
2318
2319 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2320 sizeof(struct lttng_uri) * size, NULL);
2321
2322 end:
2323 free(uris);
2324 return ret;
2325 }
2326
2327 /*
2328 * List PIDs in the tracker.
2329 *
2330 * enabled is set to whether the PID tracker is enabled.
2331 * pids is set to an allocated array of PIDs currently tracked. On
2332 * success, pids must be freed by the caller.
2333 * nr_pids is set to the number of entries contained by the pids array.
2334 *
2335 * Returns 0 on success, else a negative LTTng error code.
2336 */
2337 int lttng_list_tracker_pids(struct lttng_handle *handle,
2338 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2339 {
2340 int ret;
2341 int enabled = 1;
2342 struct lttcomm_session_msg lsm;
2343 size_t nr_pids;
2344 int32_t *pids;
2345
2346 if (handle == NULL) {
2347 return -LTTNG_ERR_INVALID;
2348 }
2349
2350 memset(&lsm, 0, sizeof(lsm));
2351 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2352 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2353 sizeof(lsm.session.name));
2354 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2355
2356 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2357 if (ret < 0) {
2358 return ret;
2359 }
2360 nr_pids = ret / sizeof(int32_t);
2361 if (nr_pids == 1 && pids[0] == -1) {
2362 free(pids);
2363 pids = NULL;
2364 enabled = 0;
2365 nr_pids = 0;
2366 }
2367 *_enabled = enabled;
2368 *_pids = pids;
2369 *_nr_pids = nr_pids;
2370 return 0;
2371 }
2372
2373 /*
2374 * Regenerate the metadata for a session.
2375 * Return 0 on success, a negative error code on error.
2376 */
2377 int lttng_regenerate_metadata(const char *session_name)
2378 {
2379 int ret;
2380 struct lttcomm_session_msg lsm;
2381
2382 if (!session_name) {
2383 ret = -LTTNG_ERR_INVALID;
2384 goto end;
2385 }
2386
2387 memset(&lsm, 0, sizeof(lsm));
2388 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2389
2390 lttng_ctl_copy_string(lsm.session.name, session_name,
2391 sizeof(lsm.session.name));
2392
2393 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2394 if (ret < 0) {
2395 goto end;
2396 }
2397
2398 ret = 0;
2399 end:
2400 return ret;
2401 }
2402
2403 /*
2404 * Deprecated, replaced by lttng_regenerate_metadata.
2405 */
2406 int lttng_metadata_regenerate(const char *session_name)
2407 {
2408 return lttng_regenerate_metadata(session_name);
2409 }
2410
2411 /*
2412 * Regenerate the statedump of a session.
2413 * Return 0 on success, a negative error code on error.
2414 */
2415 int lttng_regenerate_statedump(const char *session_name)
2416 {
2417 int ret;
2418 struct lttcomm_session_msg lsm;
2419
2420 if (!session_name) {
2421 ret = -LTTNG_ERR_INVALID;
2422 goto end;
2423 }
2424
2425 memset(&lsm, 0, sizeof(lsm));
2426 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2427
2428 lttng_ctl_copy_string(lsm.session.name, session_name,
2429 sizeof(lsm.session.name));
2430
2431 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2432 if (ret < 0) {
2433 goto end;
2434 }
2435
2436 ret = 0;
2437 end:
2438 return ret;
2439 }
2440
2441 int lttng_register_trigger(struct lttng_trigger *trigger)
2442 {
2443 int ret;
2444 struct lttcomm_session_msg lsm;
2445 char *trigger_buf = NULL;
2446 ssize_t trigger_size;
2447
2448 if (!trigger) {
2449 ret = -LTTNG_ERR_INVALID;
2450 goto end;
2451 }
2452
2453 if (!lttng_trigger_validate(trigger)) {
2454 ret = -LTTNG_ERR_INVALID;
2455 goto end;
2456 }
2457
2458 trigger_size = lttng_trigger_serialize(trigger, NULL);
2459 if (trigger_size < 0) {
2460 ret = -LTTNG_ERR_UNK;
2461 goto end;
2462 }
2463
2464 trigger_buf = zmalloc(trigger_size);
2465 if (!trigger_buf) {
2466 ret = -LTTNG_ERR_NOMEM;
2467 goto end;
2468 }
2469
2470 memset(&lsm, 0, sizeof(lsm));
2471 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
2472 if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
2473 ret = -LTTNG_ERR_UNK;
2474 goto end;
2475 }
2476
2477 lsm.u.trigger.length = (uint32_t) trigger_size;
2478 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
2479 trigger_size, NULL);
2480 end:
2481 free(trigger_buf);
2482 return ret;
2483 }
2484
2485 int lttng_unregister_trigger(struct lttng_trigger *trigger)
2486 {
2487 int ret;
2488 struct lttcomm_session_msg lsm;
2489 char *trigger_buf = NULL;
2490 ssize_t trigger_size;
2491
2492 if (!trigger) {
2493 ret = -LTTNG_ERR_INVALID;
2494 goto end;
2495 }
2496
2497 if (!lttng_trigger_validate(trigger)) {
2498 ret = -LTTNG_ERR_INVALID;
2499 goto end;
2500 }
2501
2502 trigger_size = lttng_trigger_serialize(trigger, NULL);
2503 if (trigger_size < 0) {
2504 ret = -LTTNG_ERR_UNK;
2505 goto end;
2506 }
2507
2508 trigger_buf = zmalloc(trigger_size);
2509 if (!trigger_buf) {
2510 ret = -LTTNG_ERR_NOMEM;
2511 goto end;
2512 }
2513
2514 memset(&lsm, 0, sizeof(lsm));
2515 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
2516 if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
2517 ret = -LTTNG_ERR_UNK;
2518 goto end;
2519 }
2520
2521 lsm.u.trigger.length = (uint32_t) trigger_size;
2522 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
2523 trigger_size, NULL);
2524 end:
2525 free(trigger_buf);
2526 return ret;
2527 }
2528
2529 /*
2530 * lib constructor.
2531 */
2532 static void __attribute__((constructor)) init(void)
2533 {
2534 /* Set default session group */
2535 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
2536 }
2537
2538 /*
2539 * lib destructor.
2540 */
2541 static void __attribute__((destructor)) lttng_ctl_exit(void)
2542 {
2543 free(tracing_group);
2544 }
This page took 0.123243 seconds and 6 git commands to generate.