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