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