d8d5db6c7ff6838d087309c421c83cb919e0758c
[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 *
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #define _GNU_SOURCE
23 #include <assert.h>
24 #include <grp.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/lttng.h>
37
38 #include "filter/filter-ast.h"
39 #include "filter/filter-parser.h"
40 #include "filter/filter-bytecode.h"
41 #include "filter/memstream.h"
42 #include "lttng-ctl-helper.h"
43
44 #ifdef DEBUG
45 static const int print_xml = 1;
46 #define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
48 #else
49 static const int print_xml = 0;
50 #define dbg_printf(fmt, args...) \
51 do { \
52 /* do nothing but check printf format */ \
53 if (0) \
54 printf("[debug liblttnctl] " fmt, ## args); \
55 } while (0)
56 #endif
57
58
59 /* Socket to session daemon for communication */
60 static int sessiond_socket;
61 static char sessiond_sock_path[PATH_MAX];
62 static char health_sock_path[PATH_MAX];
63
64 /* Variables */
65 static char *tracing_group;
66 static int connected;
67
68 /* Global */
69
70 /*
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
74 */
75 int lttng_opt_quiet;
76 int lttng_opt_verbose;
77
78 /*
79 * Copy string from src to dst and enforce null terminated byte.
80 */
81 LTTNG_HIDDEN
82 void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
83 {
84 if (src && dst) {
85 strncpy(dst, src, len);
86 /* Enforce the NULL terminated byte */
87 dst[len - 1] = '\0';
88 } else if (dst) {
89 dst[0] = '\0';
90 }
91 }
92
93 /*
94 * Copy domain to lttcomm_session_msg domain.
95 *
96 * If domain is unknown, default domain will be the kernel.
97 */
98 LTTNG_HIDDEN
99 void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
100 struct lttng_domain *src)
101 {
102 if (src && dst) {
103 switch (src->type) {
104 case LTTNG_DOMAIN_KERNEL:
105 case LTTNG_DOMAIN_UST:
106 memcpy(dst, src, sizeof(struct lttng_domain));
107 break;
108 default:
109 memset(dst, 0, sizeof(struct lttng_domain));
110 break;
111 }
112 }
113 }
114
115 /*
116 * Send lttcomm_session_msg to the session daemon.
117 *
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
120 */
121 static int send_session_msg(struct lttcomm_session_msg *lsm)
122 {
123 int ret;
124
125 if (!connected) {
126 ret = -LTTNG_ERR_NO_SESSIOND;
127 goto end;
128 }
129
130 DBG("LSM cmd type : %d", lsm->cmd_type);
131
132 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
133 sizeof(struct lttcomm_session_msg));
134 if (ret < 0) {
135 ret = -LTTNG_ERR_FATAL;
136 }
137
138 end:
139 return ret;
140 }
141
142 /*
143 * Send var len data to the session daemon.
144 *
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
147 */
148 static int send_session_varlen(void *data, size_t len)
149 {
150 int ret;
151
152 if (!connected) {
153 ret = -LTTNG_ERR_NO_SESSIOND;
154 goto end;
155 }
156
157 if (!data || !len) {
158 ret = 0;
159 goto end;
160 }
161
162 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
163 if (ret < 0) {
164 ret = -LTTNG_ERR_FATAL;
165 }
166
167 end:
168 return ret;
169 }
170
171 /*
172 * Receive data from the sessiond socket.
173 *
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
176 */
177 static int recv_data_sessiond(void *buf, size_t len)
178 {
179 int ret;
180
181 if (!connected) {
182 ret = -LTTNG_ERR_NO_SESSIOND;
183 goto end;
184 }
185
186 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
187 if (ret < 0) {
188 ret = -LTTNG_ERR_FATAL;
189 }
190
191 end:
192 return ret;
193 }
194
195 /*
196 * Check if we are in the specified group.
197 *
198 * If yes return 1, else return -1.
199 */
200 static int check_tracing_group(const char *grp_name)
201 {
202 struct group *grp_tracing; /* no free(). See getgrnam(3) */
203 gid_t *grp_list;
204 int grp_list_size, grp_id, i;
205 int ret = -1;
206
207 /* Get GID of group 'tracing' */
208 grp_tracing = getgrnam(grp_name);
209 if (!grp_tracing) {
210 /* If grp_tracing is NULL, the group does not exist. */
211 goto end;
212 }
213
214 /* Get number of supplementary group IDs */
215 grp_list_size = getgroups(0, NULL);
216 if (grp_list_size < 0) {
217 perror("getgroups");
218 goto end;
219 }
220
221 /* Alloc group list of the right size */
222 grp_list = malloc(grp_list_size * sizeof(gid_t));
223 if (!grp_list) {
224 perror("malloc");
225 goto end;
226 }
227 grp_id = getgroups(grp_list_size, grp_list);
228 if (grp_id < 0) {
229 perror("getgroups");
230 goto free_list;
231 }
232
233 for (i = 0; i < grp_list_size; i++) {
234 if (grp_list[i] == grp_tracing->gr_gid) {
235 ret = 1;
236 break;
237 }
238 }
239
240 free_list:
241 free(grp_list);
242
243 end:
244 return ret;
245 }
246
247 /*
248 * Try connect to session daemon with sock_path.
249 *
250 * Return 0 on success, else -1
251 */
252 static int try_connect_sessiond(const char *sock_path)
253 {
254 int ret;
255
256 /* If socket exist, we check if the daemon listens for connect. */
257 ret = access(sock_path, F_OK);
258 if (ret < 0) {
259 /* Not alive */
260 goto error;
261 }
262
263 ret = lttcomm_connect_unix_sock(sock_path);
264 if (ret < 0) {
265 /* Not alive */
266 goto error;
267 }
268
269 ret = lttcomm_close_unix_sock(ret);
270 if (ret < 0) {
271 perror("lttcomm_close_unix_sock");
272 }
273
274 return 0;
275
276 error:
277 return -1;
278 }
279
280 /*
281 * Set sessiond socket path by putting it in the global sessiond_sock_path
282 * variable.
283 *
284 * Returns 0 on success, negative value on failure (the sessiond socket path
285 * is somehow too long or ENOMEM).
286 */
287 static int set_session_daemon_path(void)
288 {
289 int in_tgroup = 0; /* In tracing group */
290 uid_t uid;
291
292 uid = getuid();
293
294 if (uid != 0) {
295 /* Are we in the tracing group ? */
296 in_tgroup = check_tracing_group(tracing_group);
297 }
298
299 if ((uid == 0) || in_tgroup) {
300 lttng_ctl_copy_string(sessiond_sock_path,
301 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
302 }
303
304 if (uid != 0) {
305 int ret;
306
307 if (in_tgroup) {
308 /* Tracing group */
309 ret = try_connect_sessiond(sessiond_sock_path);
310 if (ret >= 0) {
311 goto end;
312 }
313 /* Global session daemon not available... */
314 }
315 /* ...or not in tracing group (and not root), default */
316
317 /*
318 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
319 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
320 */
321 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
322 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
323 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
324 goto error;
325 }
326 }
327 end:
328 return 0;
329
330 error:
331 return -1;
332 }
333
334 /*
335 * Connect to the LTTng session daemon.
336 *
337 * On success, return 0. On error, return -1.
338 */
339 static int connect_sessiond(void)
340 {
341 int ret;
342
343 /* Don't try to connect if already connected. */
344 if (connected) {
345 return 0;
346 }
347
348 ret = set_session_daemon_path();
349 if (ret < 0) {
350 goto error;
351 }
352
353 /* Connect to the sesssion daemon */
354 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
355 if (ret < 0) {
356 goto error;
357 }
358
359 sessiond_socket = ret;
360 connected = 1;
361
362 return 0;
363
364 error:
365 return -1;
366 }
367
368 /*
369 * Clean disconnect from the session daemon.
370 * On success, return 0. On error, return -1.
371 */
372 static int disconnect_sessiond(void)
373 {
374 int ret = 0;
375
376 if (connected) {
377 ret = lttcomm_close_unix_sock(sessiond_socket);
378 sessiond_socket = 0;
379 connected = 0;
380 }
381
382 return ret;
383 }
384
385 /*
386 * Ask the session daemon a specific command and put the data into buf.
387 * Takes extra var. len. data as input to send to the session daemon.
388 *
389 * Return size of data (only payload, not header) or a negative error code.
390 */
391 LTTNG_HIDDEN
392 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
393 void *vardata, size_t varlen, void **buf)
394 {
395 int ret;
396 size_t size;
397 void *data = NULL;
398 struct lttcomm_lttng_msg llm;
399
400 ret = connect_sessiond();
401 if (ret < 0) {
402 ret = -LTTNG_ERR_NO_SESSIOND;
403 goto end;
404 }
405
406 /* Send command to session daemon */
407 ret = send_session_msg(lsm);
408 if (ret < 0) {
409 /* Ret value is a valid lttng error code. */
410 goto end;
411 }
412 /* Send var len data */
413 ret = send_session_varlen(vardata, varlen);
414 if (ret < 0) {
415 /* Ret value is a valid lttng error code. */
416 goto end;
417 }
418
419 /* Get header from data transmission */
420 ret = recv_data_sessiond(&llm, sizeof(llm));
421 if (ret < 0) {
422 /* Ret value is a valid lttng error code. */
423 goto end;
424 }
425
426 /* Check error code if OK */
427 if (llm.ret_code != LTTNG_OK) {
428 ret = -llm.ret_code;
429 goto end;
430 }
431
432 size = llm.data_size;
433 if (size == 0) {
434 /* If client free with size 0 */
435 if (buf != NULL) {
436 *buf = NULL;
437 }
438 ret = 0;
439 goto end;
440 }
441
442 data = (void*) malloc(size);
443
444 /* Get payload data */
445 ret = recv_data_sessiond(data, size);
446 if (ret < 0) {
447 free(data);
448 goto end;
449 }
450
451 /*
452 * Extra protection not to dereference a NULL pointer. If buf is NULL at
453 * this point, an error is returned and data is freed.
454 */
455 if (buf == NULL) {
456 ret = -LTTNG_ERR_INVALID;
457 free(data);
458 goto end;
459 }
460
461 *buf = data;
462 ret = size;
463
464 end:
465 disconnect_sessiond();
466 return ret;
467 }
468
469 /*
470 * Create lttng handle and return pointer.
471 * The returned pointer will be NULL in case of malloc() error.
472 */
473 struct lttng_handle *lttng_create_handle(const char *session_name,
474 struct lttng_domain *domain)
475 {
476 struct lttng_handle *handle = NULL;
477
478 if (domain == NULL) {
479 goto end;
480 }
481
482 handle = malloc(sizeof(struct lttng_handle));
483 if (handle == NULL) {
484 PERROR("malloc handle");
485 goto end;
486 }
487
488 /* Copy session name */
489 lttng_ctl_copy_string(handle->session_name, session_name,
490 sizeof(handle->session_name));
491
492 /* Copy lttng domain */
493 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
494
495 end:
496 return handle;
497 }
498
499 /*
500 * Destroy handle by free(3) the pointer.
501 */
502 void lttng_destroy_handle(struct lttng_handle *handle)
503 {
504 free(handle);
505 }
506
507 /*
508 * Register an outside consumer.
509 * Returns size of returned session payload data or a negative error code.
510 */
511 int lttng_register_consumer(struct lttng_handle *handle,
512 const char *socket_path)
513 {
514 struct lttcomm_session_msg lsm;
515
516 if (handle == NULL || socket_path == NULL) {
517 return -LTTNG_ERR_INVALID;
518 }
519
520 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
521 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
522 sizeof(lsm.session.name));
523 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
524
525 lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
526
527 return lttng_ctl_ask_sessiond(&lsm, NULL);
528 }
529
530 /*
531 * Start tracing for all traces of the session.
532 * Returns size of returned session payload data or a negative error code.
533 */
534 int lttng_start_tracing(const char *session_name)
535 {
536 struct lttcomm_session_msg lsm;
537
538 if (session_name == NULL) {
539 return -LTTNG_ERR_INVALID;
540 }
541
542 lsm.cmd_type = LTTNG_START_TRACE;
543
544 lttng_ctl_copy_string(lsm.session.name, session_name,
545 sizeof(lsm.session.name));
546
547 return lttng_ctl_ask_sessiond(&lsm, NULL);
548 }
549
550 /*
551 * Stop tracing for all traces of the session.
552 */
553 static int _lttng_stop_tracing(const char *session_name, int wait)
554 {
555 int ret, data_ret;
556 struct lttcomm_session_msg lsm;
557
558 if (session_name == NULL) {
559 return -LTTNG_ERR_INVALID;
560 }
561
562 lsm.cmd_type = LTTNG_STOP_TRACE;
563
564 lttng_ctl_copy_string(lsm.session.name, session_name,
565 sizeof(lsm.session.name));
566
567 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
568 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
569 goto error;
570 }
571
572 if (!wait) {
573 goto end;
574 }
575
576 _MSG("Waiting for data availability");
577
578 /* Check for data availability */
579 do {
580 data_ret = lttng_data_pending(session_name);
581 if (data_ret < 0) {
582 /* Return the data available call error. */
583 ret = data_ret;
584 goto error;
585 }
586
587 /*
588 * Data sleep time before retrying (in usec). Don't sleep if the call
589 * returned value indicates availability.
590 */
591 if (data_ret) {
592 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
593 _MSG(".");
594 }
595 } while (data_ret != 0);
596
597 MSG("");
598
599 end:
600 error:
601 return ret;
602 }
603
604 /*
605 * Stop tracing and wait for data availability.
606 */
607 int lttng_stop_tracing(const char *session_name)
608 {
609 return _lttng_stop_tracing(session_name, 1);
610 }
611
612 /*
613 * Stop tracing but _don't_ wait for data availability.
614 */
615 int lttng_stop_tracing_no_wait(const char *session_name)
616 {
617 return _lttng_stop_tracing(session_name, 0);
618 }
619
620 /*
621 * Add context to a channel.
622 *
623 * If the given channel is NULL, add the contexts to all channels.
624 * The event_name param is ignored.
625 *
626 * Returns the size of the returned payload data or a negative error code.
627 */
628 int lttng_add_context(struct lttng_handle *handle,
629 struct lttng_event_context *ctx, const char *event_name,
630 const char *channel_name)
631 {
632 struct lttcomm_session_msg lsm;
633
634 /* Safety check. Both are mandatory */
635 if (handle == NULL || ctx == NULL) {
636 return -LTTNG_ERR_INVALID;
637 }
638
639 memset(&lsm, 0, sizeof(lsm));
640
641 lsm.cmd_type = LTTNG_ADD_CONTEXT;
642
643 /* Copy channel name */
644 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
645 sizeof(lsm.u.context.channel_name));
646
647 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
648
649 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
650
651 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
652 sizeof(lsm.session.name));
653
654 return lttng_ctl_ask_sessiond(&lsm, NULL);
655 }
656
657 /*
658 * Enable event(s) for a channel.
659 * If no event name is specified, all events are enabled.
660 * If no channel name is specified, the default 'channel0' is used.
661 * Returns size of returned session payload data or a negative error code.
662 */
663 int lttng_enable_event(struct lttng_handle *handle,
664 struct lttng_event *ev, const char *channel_name)
665 {
666 struct lttcomm_session_msg lsm;
667
668 if (handle == NULL || ev == NULL) {
669 return -LTTNG_ERR_INVALID;
670 }
671
672 memset(&lsm, 0, sizeof(lsm));
673
674 /* If no channel name, we put the default name */
675 if (channel_name == NULL) {
676 lttng_ctl_copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
677 sizeof(lsm.u.enable.channel_name));
678 } else {
679 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
680 sizeof(lsm.u.enable.channel_name));
681 }
682
683 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
684
685 if (ev->name[0] != '\0') {
686 lsm.cmd_type = LTTNG_ENABLE_EVENT;
687 } else {
688 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
689 }
690 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
691
692 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
693 sizeof(lsm.session.name));
694
695 return lttng_ctl_ask_sessiond(&lsm, NULL);
696 }
697
698 /*
699 * Create or enable an event with a filter expression.
700 *
701 * Return negative error value on error.
702 * Return size of returned session payload data if OK.
703 */
704 int lttng_enable_event_with_filter(struct lttng_handle *handle,
705 struct lttng_event *event, const char *channel_name,
706 const char *filter_expression)
707 {
708 struct lttcomm_session_msg lsm;
709 struct filter_parser_ctx *ctx;
710 FILE *fmem;
711 int ret = 0;
712
713 if (!filter_expression) {
714 /*
715 * Fall back to normal event enabling if no filter
716 * specified.
717 */
718 return lttng_enable_event(handle, event, channel_name);
719 }
720
721 /*
722 * Empty filter string will always be rejected by the parser
723 * anyway, so treat this corner-case early to eliminate
724 * lttng_fmemopen error for 0-byte allocation.
725 */
726 if (handle == NULL || filter_expression[0] == '\0') {
727 return -LTTNG_ERR_INVALID;
728 }
729
730 /*
731 * casting const to non-const, as the underlying function will
732 * use it in read-only mode.
733 */
734 fmem = lttng_fmemopen((void *) filter_expression,
735 strlen(filter_expression), "r");
736 if (!fmem) {
737 fprintf(stderr, "Error opening memory as stream\n");
738 return -LTTNG_ERR_FILTER_NOMEM;
739 }
740 ctx = filter_parser_ctx_alloc(fmem);
741 if (!ctx) {
742 fprintf(stderr, "Error allocating parser\n");
743 ret = -LTTNG_ERR_FILTER_NOMEM;
744 goto alloc_error;
745 }
746 ret = filter_parser_ctx_append_ast(ctx);
747 if (ret) {
748 fprintf(stderr, "Parse error\n");
749 ret = -LTTNG_ERR_FILTER_INVAL;
750 goto parse_error;
751 }
752 ret = filter_visitor_set_parent(ctx);
753 if (ret) {
754 fprintf(stderr, "Set parent error\n");
755 ret = -LTTNG_ERR_FILTER_INVAL;
756 goto parse_error;
757 }
758 if (print_xml) {
759 ret = filter_visitor_print_xml(ctx, stdout, 0);
760 if (ret) {
761 fflush(stdout);
762 fprintf(stderr, "XML print error\n");
763 ret = -LTTNG_ERR_FILTER_INVAL;
764 goto parse_error;
765 }
766 }
767
768 dbg_printf("Generating IR... ");
769 fflush(stdout);
770 ret = filter_visitor_ir_generate(ctx);
771 if (ret) {
772 fprintf(stderr, "Generate IR error\n");
773 ret = -LTTNG_ERR_FILTER_INVAL;
774 goto parse_error;
775 }
776 dbg_printf("done\n");
777
778 dbg_printf("Validating IR... ");
779 fflush(stdout);
780 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
781 if (ret) {
782 ret = -LTTNG_ERR_FILTER_INVAL;
783 goto parse_error;
784 }
785 dbg_printf("done\n");
786
787 dbg_printf("Generating bytecode... ");
788 fflush(stdout);
789 ret = filter_visitor_bytecode_generate(ctx);
790 if (ret) {
791 fprintf(stderr, "Generate bytecode error\n");
792 ret = -LTTNG_ERR_FILTER_INVAL;
793 goto parse_error;
794 }
795 dbg_printf("done\n");
796 dbg_printf("Size of bytecode generated: %u bytes.\n",
797 bytecode_get_len(&ctx->bytecode->b));
798
799 memset(&lsm, 0, sizeof(lsm));
800
801 lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER;
802
803 /* Copy channel name */
804 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
805 sizeof(lsm.u.enable.channel_name));
806 /* Copy event name */
807 if (event) {
808 memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event));
809 }
810
811 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
812 + bytecode_get_len(&ctx->bytecode->b);
813
814 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
815
816 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
817 sizeof(lsm.session.name));
818
819 ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
820 lsm.u.enable.bytecode_len, NULL);
821
822 filter_bytecode_free(ctx);
823 filter_ir_free(ctx);
824 filter_parser_ctx_free(ctx);
825 if (fclose(fmem) != 0) {
826 perror("fclose");
827 }
828 return ret;
829
830 parse_error:
831 filter_bytecode_free(ctx);
832 filter_ir_free(ctx);
833 filter_parser_ctx_free(ctx);
834 alloc_error:
835 if (fclose(fmem) != 0) {
836 perror("fclose");
837 }
838 return ret;
839 }
840
841 /*
842 * Disable event(s) of a channel and domain.
843 * If no event name is specified, all events are disabled.
844 * If no channel name is specified, the default 'channel0' is used.
845 * Returns size of returned session payload data or a negative error code.
846 */
847 int lttng_disable_event(struct lttng_handle *handle, const char *name,
848 const char *channel_name)
849 {
850 struct lttcomm_session_msg lsm;
851
852 if (handle == NULL) {
853 return -LTTNG_ERR_INVALID;
854 }
855
856 memset(&lsm, 0, sizeof(lsm));
857
858 if (channel_name) {
859 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
860 sizeof(lsm.u.disable.channel_name));
861 } else {
862 lttng_ctl_copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
863 sizeof(lsm.u.disable.channel_name));
864 }
865
866 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
867
868 if (name != NULL) {
869 lttng_ctl_copy_string(lsm.u.disable.name, name,
870 sizeof(lsm.u.disable.name));
871 lsm.cmd_type = LTTNG_DISABLE_EVENT;
872 } else {
873 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
874 }
875
876 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
877 sizeof(lsm.session.name));
878
879 return lttng_ctl_ask_sessiond(&lsm, NULL);
880 }
881
882 /*
883 * Enable channel per domain
884 * Returns size of returned session payload data or a negative error code.
885 */
886 int lttng_enable_channel(struct lttng_handle *handle,
887 struct lttng_channel *chan)
888 {
889 struct lttcomm_session_msg lsm;
890
891 /*
892 * NULL arguments are forbidden. No default values.
893 */
894 if (handle == NULL || chan == NULL) {
895 return -LTTNG_ERR_INVALID;
896 }
897
898 memset(&lsm, 0, sizeof(lsm));
899
900 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
901
902 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
903
904 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
905
906 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
907 sizeof(lsm.session.name));
908
909 return lttng_ctl_ask_sessiond(&lsm, NULL);
910 }
911
912 /*
913 * All tracing will be stopped for registered events of the channel.
914 * Returns size of returned session payload data or a negative error code.
915 */
916 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
917 {
918 struct lttcomm_session_msg lsm;
919
920 /* Safety check. Both are mandatory */
921 if (handle == NULL || name == NULL) {
922 return -LTTNG_ERR_INVALID;
923 }
924
925 memset(&lsm, 0, sizeof(lsm));
926
927 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
928
929 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
930 sizeof(lsm.u.disable.channel_name));
931
932 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
933
934 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
935 sizeof(lsm.session.name));
936
937 return lttng_ctl_ask_sessiond(&lsm, NULL);
938 }
939
940 /*
941 * Lists all available tracepoints of domain.
942 * Sets the contents of the events array.
943 * Returns the number of lttng_event entries in events;
944 * on error, returns a negative value.
945 */
946 int lttng_list_tracepoints(struct lttng_handle *handle,
947 struct lttng_event **events)
948 {
949 int ret;
950 struct lttcomm_session_msg lsm;
951
952 if (handle == NULL) {
953 return -LTTNG_ERR_INVALID;
954 }
955
956 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
957 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
958
959 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
960 if (ret < 0) {
961 return ret;
962 }
963
964 return ret / sizeof(struct lttng_event);
965 }
966
967 /*
968 * Lists all available tracepoint fields of domain.
969 * Sets the contents of the event field array.
970 * Returns the number of lttng_event_field entries in events;
971 * on error, returns a negative value.
972 */
973 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
974 struct lttng_event_field **fields)
975 {
976 int ret;
977 struct lttcomm_session_msg lsm;
978
979 if (handle == NULL) {
980 return -LTTNG_ERR_INVALID;
981 }
982
983 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
984 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
985
986 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
987 if (ret < 0) {
988 return ret;
989 }
990
991 return ret / sizeof(struct lttng_event_field);
992 }
993
994 /*
995 * Returns a human readable string describing
996 * the error code (a negative value).
997 */
998 const char *lttng_strerror(int code)
999 {
1000 return error_get_str(code);
1001 }
1002
1003 /*
1004 * Create a brand new session using name and url for destination.
1005 *
1006 * Returns LTTNG_OK on success or a negative error code.
1007 */
1008 int lttng_create_session(const char *name, const char *url)
1009 {
1010 int ret;
1011 ssize_t size;
1012 struct lttcomm_session_msg lsm;
1013 struct lttng_uri *uris = NULL;
1014
1015 if (name == NULL) {
1016 return -LTTNG_ERR_INVALID;
1017 }
1018
1019 memset(&lsm, 0, sizeof(lsm));
1020
1021 lsm.cmd_type = LTTNG_CREATE_SESSION;
1022 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1023
1024 /* There should never be a data URL */
1025 size = uri_parse_str_urls(url, NULL, &uris);
1026 if (size < 0) {
1027 return -LTTNG_ERR_INVALID;
1028 }
1029
1030 lsm.u.uri.size = size;
1031
1032 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1033 sizeof(struct lttng_uri) * size, NULL);
1034
1035 free(uris);
1036 return ret;
1037 }
1038
1039 /*
1040 * Destroy session using name.
1041 * Returns size of returned session payload data or a negative error code.
1042 */
1043 int lttng_destroy_session(const char *session_name)
1044 {
1045 struct lttcomm_session_msg lsm;
1046
1047 if (session_name == NULL) {
1048 return -LTTNG_ERR_INVALID;
1049 }
1050
1051 lsm.cmd_type = LTTNG_DESTROY_SESSION;
1052
1053 lttng_ctl_copy_string(lsm.session.name, session_name,
1054 sizeof(lsm.session.name));
1055
1056 return lttng_ctl_ask_sessiond(&lsm, NULL);
1057 }
1058
1059 /*
1060 * Ask the session daemon for all available sessions.
1061 * Sets the contents of the sessions array.
1062 * Returns the number of lttng_session entries in sessions;
1063 * on error, returns a negative value.
1064 */
1065 int lttng_list_sessions(struct lttng_session **sessions)
1066 {
1067 int ret;
1068 struct lttcomm_session_msg lsm;
1069
1070 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1071 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
1072 if (ret < 0) {
1073 return ret;
1074 }
1075
1076 return ret / sizeof(struct lttng_session);
1077 }
1078
1079 /*
1080 * Ask the session daemon for all available domains of a session.
1081 * Sets the contents of the domains array.
1082 * Returns the number of lttng_domain entries in domains;
1083 * on error, returns a negative value.
1084 */
1085 int lttng_list_domains(const char *session_name,
1086 struct lttng_domain **domains)
1087 {
1088 int ret;
1089 struct lttcomm_session_msg lsm;
1090
1091 if (session_name == NULL) {
1092 return -LTTNG_ERR_INVALID;
1093 }
1094
1095 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1096
1097 lttng_ctl_copy_string(lsm.session.name, session_name,
1098 sizeof(lsm.session.name));
1099
1100 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
1101 if (ret < 0) {
1102 return ret;
1103 }
1104
1105 return ret / sizeof(struct lttng_domain);
1106 }
1107
1108 /*
1109 * Ask the session daemon for all available channels of a session.
1110 * Sets the contents of the channels array.
1111 * Returns the number of lttng_channel entries in channels;
1112 * on error, returns a negative value.
1113 */
1114 int lttng_list_channels(struct lttng_handle *handle,
1115 struct lttng_channel **channels)
1116 {
1117 int ret;
1118 struct lttcomm_session_msg lsm;
1119
1120 if (handle == NULL) {
1121 return -LTTNG_ERR_INVALID;
1122 }
1123
1124 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1125 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1126 sizeof(lsm.session.name));
1127
1128 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1129
1130 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
1131 if (ret < 0) {
1132 return ret;
1133 }
1134
1135 return ret / sizeof(struct lttng_channel);
1136 }
1137
1138 /*
1139 * Ask the session daemon for all available events of a session channel.
1140 * Sets the contents of the events array.
1141 * Returns the number of lttng_event entries in events;
1142 * on error, returns a negative value.
1143 */
1144 int lttng_list_events(struct lttng_handle *handle,
1145 const char *channel_name, struct lttng_event **events)
1146 {
1147 int ret;
1148 struct lttcomm_session_msg lsm;
1149
1150 /* Safety check. An handle and channel name are mandatory */
1151 if (handle == NULL || channel_name == NULL) {
1152 return -LTTNG_ERR_INVALID;
1153 }
1154
1155 lsm.cmd_type = LTTNG_LIST_EVENTS;
1156 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1157 sizeof(lsm.session.name));
1158 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
1159 sizeof(lsm.u.list.channel_name));
1160
1161 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1162
1163 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
1164 if (ret < 0) {
1165 return ret;
1166 }
1167
1168 return ret / sizeof(struct lttng_event);
1169 }
1170
1171 /*
1172 * Sets the tracing_group variable with name.
1173 * This function allocates memory pointed to by tracing_group.
1174 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1175 */
1176 int lttng_set_tracing_group(const char *name)
1177 {
1178 if (name == NULL) {
1179 return -LTTNG_ERR_INVALID;
1180 }
1181
1182 if (asprintf(&tracing_group, "%s", name) < 0) {
1183 return -LTTNG_ERR_FATAL;
1184 }
1185
1186 return 0;
1187 }
1188
1189 /*
1190 * Returns size of returned session payload data or a negative error code.
1191 */
1192 int lttng_calibrate(struct lttng_handle *handle,
1193 struct lttng_calibrate *calibrate)
1194 {
1195 struct lttcomm_session_msg lsm;
1196
1197 /* Safety check. NULL pointer are forbidden */
1198 if (handle == NULL || calibrate == NULL) {
1199 return -LTTNG_ERR_INVALID;
1200 }
1201
1202 lsm.cmd_type = LTTNG_CALIBRATE;
1203 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1204
1205 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1206
1207 return lttng_ctl_ask_sessiond(&lsm, NULL);
1208 }
1209
1210 /*
1211 * Set default channel attributes.
1212 * If either or both of the arguments are null, attr content is zeroe'd.
1213 */
1214 void lttng_channel_set_default_attr(struct lttng_domain *domain,
1215 struct lttng_channel_attr *attr)
1216 {
1217 /* Safety check */
1218 if (attr == NULL || domain == NULL) {
1219 return;
1220 }
1221
1222 memset(attr, 0, sizeof(struct lttng_channel_attr));
1223
1224 /* Same for all domains. */
1225 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1226 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1227 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1228
1229 switch (domain->type) {
1230 case LTTNG_DOMAIN_KERNEL:
1231 attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1232 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
1233 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
1234 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1235 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1236 break;
1237 case LTTNG_DOMAIN_UST:
1238 switch (domain->buf_type) {
1239 case LTTNG_BUFFER_PER_UID:
1240 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1241 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1242 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1243 attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1244 attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1245 break;
1246 case LTTNG_BUFFER_PER_PID:
1247 default:
1248 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1249 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1250 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1251 attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1252 attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1253 break;
1254 }
1255 default:
1256 /* Default behavior: leave set to 0. */
1257 break;
1258 }
1259 }
1260
1261 /*
1262 * Check if session daemon is alive.
1263 *
1264 * Return 1 if alive or 0 if not.
1265 * On error returns a negative value.
1266 */
1267 int lttng_session_daemon_alive(void)
1268 {
1269 int ret;
1270
1271 ret = set_session_daemon_path();
1272 if (ret < 0) {
1273 /* Error */
1274 return ret;
1275 }
1276
1277 if (*sessiond_sock_path == '\0') {
1278 /*
1279 * No socket path set. Weird error which means the constructor was not
1280 * called.
1281 */
1282 assert(0);
1283 }
1284
1285 ret = try_connect_sessiond(sessiond_sock_path);
1286 if (ret < 0) {
1287 /* Not alive */
1288 return 0;
1289 }
1290
1291 /* Is alive */
1292 return 1;
1293 }
1294
1295 /*
1296 * Set URL for a consumer for a session and domain.
1297 *
1298 * Return 0 on success, else a negative value.
1299 */
1300 int lttng_set_consumer_url(struct lttng_handle *handle,
1301 const char *control_url, const char *data_url)
1302 {
1303 int ret;
1304 ssize_t size;
1305 struct lttcomm_session_msg lsm;
1306 struct lttng_uri *uris = NULL;
1307
1308 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
1309 return -LTTNG_ERR_INVALID;
1310 }
1311
1312 memset(&lsm, 0, sizeof(lsm));
1313
1314 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1315
1316 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1317 sizeof(lsm.session.name));
1318 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1319
1320 size = uri_parse_str_urls(control_url, data_url, &uris);
1321 if (size < 0) {
1322 return -LTTNG_ERR_INVALID;
1323 }
1324
1325 lsm.u.uri.size = size;
1326
1327 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1328 sizeof(struct lttng_uri) * size, NULL);
1329
1330 free(uris);
1331 return ret;
1332 }
1333
1334 /*
1335 * [OBSOLETE]
1336 */
1337 int lttng_enable_consumer(struct lttng_handle *handle)
1338 {
1339 return -ENOSYS;
1340 }
1341
1342 /*
1343 * [OBSOLETE]
1344 */
1345 int lttng_disable_consumer(struct lttng_handle *handle)
1346 {
1347 return -ENOSYS;
1348 }
1349
1350 /*
1351 * Set health socket path by putting it in the global health_sock_path
1352 * variable.
1353 *
1354 * Returns 0 on success or assert(0) on ENOMEM.
1355 */
1356 static int set_health_socket_path(void)
1357 {
1358 int in_tgroup = 0; /* In tracing group */
1359 uid_t uid;
1360 const char *home;
1361
1362 uid = getuid();
1363
1364 if (uid != 0) {
1365 /* Are we in the tracing group ? */
1366 in_tgroup = check_tracing_group(tracing_group);
1367 }
1368
1369 if ((uid == 0) || in_tgroup) {
1370 lttng_ctl_copy_string(health_sock_path,
1371 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, sizeof(health_sock_path));
1372 }
1373
1374 if (uid != 0) {
1375 int ret;
1376
1377 /*
1378 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1379 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1380 */
1381 home = utils_get_home_dir();
1382 if (home == NULL) {
1383 /* Fallback in /tmp .. */
1384 home = "/tmp";
1385 }
1386
1387 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1388 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1389 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1390 /* ENOMEM at this point... just kill the control lib. */
1391 assert(0);
1392 }
1393 }
1394
1395 return 0;
1396 }
1397
1398 /*
1399 * Check session daemon health for a specific health component.
1400 *
1401 * Return 0 if health is OK or else 1 if BAD.
1402 *
1403 * Any other negative value is a lttng error code which can be translated with
1404 * lttng_strerror().
1405 */
1406 int lttng_health_check(enum lttng_health_component c)
1407 {
1408 int sock, ret;
1409 struct lttcomm_health_msg msg;
1410 struct lttcomm_health_data reply;
1411
1412 /* Connect to the sesssion daemon */
1413 sock = lttcomm_connect_unix_sock(health_sock_path);
1414 if (sock < 0) {
1415 ret = -LTTNG_ERR_NO_SESSIOND;
1416 goto error;
1417 }
1418
1419 msg.cmd = LTTNG_HEALTH_CHECK;
1420 msg.component = c;
1421
1422 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1423 if (ret < 0) {
1424 ret = -LTTNG_ERR_FATAL;
1425 goto close_error;
1426 }
1427
1428 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1429 if (ret < 0) {
1430 ret = -LTTNG_ERR_FATAL;
1431 goto close_error;
1432 }
1433
1434 ret = reply.ret_code;
1435
1436 close_error:
1437 {
1438 int closeret;
1439
1440 closeret = close(sock);
1441 assert(!closeret);
1442 }
1443
1444 error:
1445 return ret;
1446 }
1447
1448 /*
1449 * This is an extension of create session that is ONLY and SHOULD only be used
1450 * by the lttng command line program. It exists to avoid using URI parsing in
1451 * the lttng client.
1452 *
1453 * We need the date and time for the trace path subdirectory for the case where
1454 * the user does NOT define one using either -o or -U. Using the normal
1455 * lttng_create_session API call, we have no clue on the session daemon side if
1456 * the URL was generated automatically by the client or define by the user.
1457 *
1458 * So this function "wrapper" is hidden from the public API, takes the datetime
1459 * string and appends it if necessary to the URI subdirectory before sending it
1460 * to the session daemon.
1461 *
1462 * With this extra function, the lttng_create_session call behavior is not
1463 * changed and the timestamp is appended to the URI on the session daemon side
1464 * if necessary.
1465 */
1466 int _lttng_create_session_ext(const char *name, const char *url,
1467 const char *datetime)
1468 {
1469 int ret;
1470 ssize_t size;
1471 struct lttcomm_session_msg lsm;
1472 struct lttng_uri *uris = NULL;
1473
1474 if (name == NULL || datetime == NULL) {
1475 return -LTTNG_ERR_INVALID;
1476 }
1477
1478 memset(&lsm, 0, sizeof(lsm));
1479
1480 lsm.cmd_type = LTTNG_CREATE_SESSION;
1481 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1482
1483 /* There should never be a data URL */
1484 size = uri_parse_str_urls(url, NULL, &uris);
1485 if (size < 0) {
1486 ret = -LTTNG_ERR_INVALID;
1487 goto error;
1488 }
1489
1490 lsm.u.uri.size = size;
1491
1492 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
1493 /* Don't append datetime if the name was automatically created. */
1494 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1495 strlen(DEFAULT_SESSION_NAME) + 1)) {
1496 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1497 name, datetime);
1498 } else {
1499 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1500 }
1501 if (ret < 0) {
1502 PERROR("snprintf uri subdir");
1503 ret = -LTTNG_ERR_FATAL;
1504 goto error;
1505 }
1506 }
1507
1508 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1509 sizeof(struct lttng_uri) * size, NULL);
1510
1511 error:
1512 free(uris);
1513 return ret;
1514 }
1515
1516 /*
1517 * For a given session name, this call checks if the data is ready to be read
1518 * or is still being extracted by the consumer(s) hence not ready to be used by
1519 * any readers.
1520 */
1521 int lttng_data_pending(const char *session_name)
1522 {
1523 int ret;
1524 struct lttcomm_session_msg lsm;
1525
1526 if (session_name == NULL) {
1527 return -LTTNG_ERR_INVALID;
1528 }
1529
1530 lsm.cmd_type = LTTNG_DATA_PENDING;
1531
1532 lttng_ctl_copy_string(lsm.session.name, session_name,
1533 sizeof(lsm.session.name));
1534
1535 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1536
1537 /*
1538 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1539 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1540 * that the data is available. Yes it is hackish but for now this is the
1541 * only way.
1542 */
1543 if (ret == -1) {
1544 ret = 1;
1545 }
1546
1547 return ret;
1548 }
1549
1550 /*
1551 * Create a session exclusively used for snapshot.
1552 *
1553 * Returns LTTNG_OK on success or a negative error code.
1554 */
1555 int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1556 {
1557 int ret;
1558 ssize_t size;
1559 struct lttcomm_session_msg lsm;
1560 struct lttng_uri *uris = NULL;
1561
1562 if (name == NULL) {
1563 return -LTTNG_ERR_INVALID;
1564 }
1565
1566 memset(&lsm, 0, sizeof(lsm));
1567
1568 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
1569 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1570
1571 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1572 if (size < 0) {
1573 return -LTTNG_ERR_INVALID;
1574 }
1575
1576 lsm.u.uri.size = size;
1577
1578 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1579 sizeof(struct lttng_uri) * size, NULL);
1580
1581 free(uris);
1582 return ret;
1583 }
1584
1585 /*
1586 * lib constructor
1587 */
1588 static void __attribute__((constructor)) init()
1589 {
1590 /* Set default session group */
1591 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
1592 /* Set socket for health check */
1593 (void) set_health_socket_path();
1594 }
1595
1596 /*
1597 * lib destructor
1598 */
1599 static void __attribute__((destructor)) lttng_ctl_exit()
1600 {
1601 free(tracing_group);
1602 }
This page took 0.063245 seconds and 3 git commands to generate.