Fix: Save jul event filter bytecode
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <string.h>
21 #include <inttypes.h>
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
24
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
30
31 #include "channel.h"
32 #include "consumer.h"
33 #include "event.h"
34 #include "health-sessiond.h"
35 #include "kernel.h"
36 #include "kernel-consumer.h"
37 #include "lttng-sessiond.h"
38 #include "utils.h"
39
40 #include "cmd.h"
41
42 /*
43 * Used to keep a unique index for each relayd socket created where this value
44 * is associated with streams on the consumer so it can match the right relayd
45 * to send to. It must be accessed with the relayd_net_seq_idx_lock
46 * held.
47 */
48 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
49 static uint64_t relayd_net_seq_idx;
50
51 /*
52 * Create a session path used by list_lttng_sessions for the case that the
53 * session consumer is on the network.
54 */
55 static int build_network_session_path(char *dst, size_t size,
56 struct ltt_session *session)
57 {
58 int ret, kdata_port, udata_port;
59 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
60 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
61
62 assert(session);
63 assert(dst);
64
65 memset(tmp_urls, 0, sizeof(tmp_urls));
66 memset(tmp_uurl, 0, sizeof(tmp_uurl));
67
68 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
69
70 if (session->kernel_session && session->kernel_session->consumer) {
71 kuri = &session->kernel_session->consumer->dst.net.control;
72 kdata_port = session->kernel_session->consumer->dst.net.data.port;
73 }
74
75 if (session->ust_session && session->ust_session->consumer) {
76 uuri = &session->ust_session->consumer->dst.net.control;
77 udata_port = session->ust_session->consumer->dst.net.data.port;
78 }
79
80 if (uuri == NULL && kuri == NULL) {
81 uri = &session->consumer->dst.net.control;
82 kdata_port = session->consumer->dst.net.data.port;
83 } else if (kuri && uuri) {
84 ret = uri_compare(kuri, uuri);
85 if (ret) {
86 /* Not Equal */
87 uri = kuri;
88 /* Build uuri URL string */
89 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
90 if (ret < 0) {
91 goto error;
92 }
93 } else {
94 uri = kuri;
95 }
96 } else if (kuri && uuri == NULL) {
97 uri = kuri;
98 } else if (uuri && kuri == NULL) {
99 uri = uuri;
100 }
101
102 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
103 if (ret < 0) {
104 goto error;
105 }
106
107 /*
108 * Do we have a UST url set. If yes, this means we have both kernel and UST
109 * to print.
110 */
111 if (*tmp_uurl != '\0') {
112 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
113 tmp_urls, kdata_port, tmp_uurl, udata_port);
114 } else {
115 int dport;
116 if (kuri || (!kuri && !uuri)) {
117 dport = kdata_port;
118 } else {
119 /* No kernel URI, use the UST port. */
120 dport = udata_port;
121 }
122 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
123 }
124
125 error:
126 return ret;
127 }
128
129 /*
130 * Fill lttng_channel array of all channels.
131 */
132 static void list_lttng_channels(int domain, struct ltt_session *session,
133 struct lttng_channel *channels)
134 {
135 int i = 0;
136 struct ltt_kernel_channel *kchan;
137
138 DBG("Listing channels for session %s", session->name);
139
140 switch (domain) {
141 case LTTNG_DOMAIN_KERNEL:
142 /* Kernel channels */
143 if (session->kernel_session != NULL) {
144 cds_list_for_each_entry(kchan,
145 &session->kernel_session->channel_list.head, list) {
146 /* Copy lttng_channel struct to array */
147 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
148 channels[i].enabled = kchan->enabled;
149 i++;
150 }
151 }
152 break;
153 case LTTNG_DOMAIN_UST:
154 {
155 struct lttng_ht_iter iter;
156 struct ltt_ust_channel *uchan;
157
158 rcu_read_lock();
159 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
160 &iter.iter, uchan, node.node) {
161 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
162 channels[i].attr.overwrite = uchan->attr.overwrite;
163 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
164 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
165 channels[i].attr.switch_timer_interval =
166 uchan->attr.switch_timer_interval;
167 channels[i].attr.read_timer_interval =
168 uchan->attr.read_timer_interval;
169 channels[i].enabled = uchan->enabled;
170 channels[i].attr.tracefile_size = uchan->tracefile_size;
171 channels[i].attr.tracefile_count = uchan->tracefile_count;
172 switch (uchan->attr.output) {
173 case LTTNG_UST_MMAP:
174 default:
175 channels[i].attr.output = LTTNG_EVENT_MMAP;
176 break;
177 }
178 i++;
179 }
180 rcu_read_unlock();
181 break;
182 }
183 default:
184 break;
185 }
186 }
187
188 /*
189 * Create a list of JUL domain events.
190 *
191 * Return number of events in list on success or else a negative value.
192 */
193 static int list_lttng_jul_events(struct jul_domain *dom,
194 struct lttng_event **events)
195 {
196 int i = 0, ret = 0;
197 unsigned int nb_event = 0;
198 struct jul_event *event;
199 struct lttng_event *tmp_events;
200 struct lttng_ht_iter iter;
201
202 assert(dom);
203 assert(events);
204
205 DBG3("Listing JUL events");
206
207 nb_event = lttng_ht_get_count(dom->events);
208 if (nb_event == 0) {
209 ret = nb_event;
210 goto error;
211 }
212
213 tmp_events = zmalloc(nb_event * sizeof(*tmp_events));
214 if (!tmp_events) {
215 PERROR("zmalloc JUL events session");
216 ret = -LTTNG_ERR_FATAL;
217 goto error;
218 }
219
220 rcu_read_lock();
221 cds_lfht_for_each_entry(dom->events->ht, &iter.iter, event, node.node) {
222 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
223 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
224 tmp_events[i].enabled = event->enabled;
225 tmp_events[i].loglevel = event->loglevel;
226 tmp_events[i].loglevel_type = event->loglevel_type;
227 i++;
228 }
229 rcu_read_unlock();
230
231 *events = tmp_events;
232 ret = nb_event;
233
234 error:
235 assert(nb_event == i);
236 return ret;
237 }
238
239 /*
240 * Create a list of ust global domain events.
241 */
242 static int list_lttng_ust_global_events(char *channel_name,
243 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
244 {
245 int i = 0, ret = 0;
246 unsigned int nb_event = 0;
247 struct lttng_ht_iter iter;
248 struct lttng_ht_node_str *node;
249 struct ltt_ust_channel *uchan;
250 struct ltt_ust_event *uevent;
251 struct lttng_event *tmp;
252
253 DBG("Listing UST global events for channel %s", channel_name);
254
255 rcu_read_lock();
256
257 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
258 node = lttng_ht_iter_get_node_str(&iter);
259 if (node == NULL) {
260 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
261 goto error;
262 }
263
264 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
265
266 nb_event += lttng_ht_get_count(uchan->events);
267
268 if (nb_event == 0) {
269 ret = nb_event;
270 goto error;
271 }
272
273 DBG3("Listing UST global %d events", nb_event);
274
275 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
276 if (tmp == NULL) {
277 ret = LTTNG_ERR_FATAL;
278 goto error;
279 }
280
281 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
282 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
283 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
284 tmp[i].enabled = uevent->enabled;
285
286 switch (uevent->attr.instrumentation) {
287 case LTTNG_UST_TRACEPOINT:
288 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
289 break;
290 case LTTNG_UST_PROBE:
291 tmp[i].type = LTTNG_EVENT_PROBE;
292 break;
293 case LTTNG_UST_FUNCTION:
294 tmp[i].type = LTTNG_EVENT_FUNCTION;
295 break;
296 }
297
298 tmp[i].loglevel = uevent->attr.loglevel;
299 switch (uevent->attr.loglevel_type) {
300 case LTTNG_UST_LOGLEVEL_ALL:
301 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
302 break;
303 case LTTNG_UST_LOGLEVEL_RANGE:
304 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
305 break;
306 case LTTNG_UST_LOGLEVEL_SINGLE:
307 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
308 break;
309 }
310 if (uevent->filter) {
311 tmp[i].filter = 1;
312 }
313 if (uevent->exclusion) {
314 tmp[i].exclusion = 1;
315 }
316 i++;
317 }
318
319 ret = nb_event;
320 *events = tmp;
321
322 error:
323 rcu_read_unlock();
324 return ret;
325 }
326
327 /*
328 * Fill lttng_event array of all kernel events in the channel.
329 */
330 static int list_lttng_kernel_events(char *channel_name,
331 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
332 {
333 int i = 0, ret;
334 unsigned int nb_event;
335 struct ltt_kernel_event *event;
336 struct ltt_kernel_channel *kchan;
337
338 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
339 if (kchan == NULL) {
340 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
341 goto error;
342 }
343
344 nb_event = kchan->event_count;
345
346 DBG("Listing events for channel %s", kchan->channel->name);
347
348 if (nb_event == 0) {
349 goto end;
350 }
351
352 *events = zmalloc(nb_event * sizeof(struct lttng_event));
353 if (*events == NULL) {
354 ret = LTTNG_ERR_FATAL;
355 goto error;
356 }
357
358 /* Kernel channels */
359 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
360 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
361 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
362 (*events)[i].enabled = event->enabled;
363
364 switch (event->event->instrumentation) {
365 case LTTNG_KERNEL_TRACEPOINT:
366 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
367 break;
368 case LTTNG_KERNEL_KRETPROBE:
369 (*events)[i].type = LTTNG_EVENT_FUNCTION;
370 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
371 sizeof(struct lttng_kernel_kprobe));
372 break;
373 case LTTNG_KERNEL_KPROBE:
374 (*events)[i].type = LTTNG_EVENT_PROBE;
375 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
376 sizeof(struct lttng_kernel_kprobe));
377 break;
378 case LTTNG_KERNEL_FUNCTION:
379 (*events)[i].type = LTTNG_EVENT_FUNCTION;
380 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
381 sizeof(struct lttng_kernel_function));
382 break;
383 case LTTNG_KERNEL_NOOP:
384 (*events)[i].type = LTTNG_EVENT_NOOP;
385 break;
386 case LTTNG_KERNEL_SYSCALL:
387 (*events)[i].type = LTTNG_EVENT_SYSCALL;
388 break;
389 case LTTNG_KERNEL_ALL:
390 assert(0);
391 break;
392 }
393 i++;
394 }
395
396 end:
397 return nb_event;
398
399 error:
400 /* Negate the error code to differentiate the size from an error */
401 return -ret;
402 }
403
404 /*
405 * Add URI so the consumer output object. Set the correct path depending on the
406 * domain adding the default trace directory.
407 */
408 static int add_uri_to_consumer(struct consumer_output *consumer,
409 struct lttng_uri *uri, int domain, const char *session_name)
410 {
411 int ret = LTTNG_OK;
412 const char *default_trace_dir;
413
414 assert(uri);
415
416 if (consumer == NULL) {
417 DBG("No consumer detected. Don't add URI. Stopping.");
418 ret = LTTNG_ERR_NO_CONSUMER;
419 goto error;
420 }
421
422 switch (domain) {
423 case LTTNG_DOMAIN_KERNEL:
424 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
425 break;
426 case LTTNG_DOMAIN_UST:
427 default_trace_dir = DEFAULT_UST_TRACE_DIR;
428 break;
429 default:
430 /*
431 * This case is possible is we try to add the URI to the global tracing
432 * session consumer object which in this case there is no subdir.
433 */
434 default_trace_dir = "";
435 }
436
437 switch (uri->dtype) {
438 case LTTNG_DST_IPV4:
439 case LTTNG_DST_IPV6:
440 DBG2("Setting network URI to consumer");
441
442 if (consumer->type == CONSUMER_DST_NET) {
443 if ((uri->stype == LTTNG_STREAM_CONTROL &&
444 consumer->dst.net.control_isset) ||
445 (uri->stype == LTTNG_STREAM_DATA &&
446 consumer->dst.net.data_isset)) {
447 ret = LTTNG_ERR_URL_EXIST;
448 goto error;
449 }
450 } else {
451 memset(&consumer->dst.net, 0, sizeof(consumer->dst.net));
452 }
453
454 consumer->type = CONSUMER_DST_NET;
455
456 /* Set URI into consumer output object */
457 ret = consumer_set_network_uri(consumer, uri);
458 if (ret < 0) {
459 ret = -ret;
460 goto error;
461 } else if (ret == 1) {
462 /*
463 * URI was the same in the consumer so we do not append the subdir
464 * again so to not duplicate output dir.
465 */
466 ret = LTTNG_OK;
467 goto error;
468 }
469
470 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
471 ret = consumer_set_subdir(consumer, session_name);
472 if (ret < 0) {
473 ret = LTTNG_ERR_FATAL;
474 goto error;
475 }
476 }
477
478 if (uri->stype == LTTNG_STREAM_CONTROL) {
479 /* On a new subdir, reappend the default trace dir. */
480 strncat(consumer->subdir, default_trace_dir,
481 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
482 DBG3("Append domain trace name to subdir %s", consumer->subdir);
483 }
484
485 break;
486 case LTTNG_DST_PATH:
487 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
488 memset(consumer->dst.trace_path, 0,
489 sizeof(consumer->dst.trace_path));
490 strncpy(consumer->dst.trace_path, uri->dst.path,
491 sizeof(consumer->dst.trace_path));
492 /* Append default trace dir */
493 strncat(consumer->dst.trace_path, default_trace_dir,
494 sizeof(consumer->dst.trace_path) -
495 strlen(consumer->dst.trace_path) - 1);
496 /* Flag consumer as local. */
497 consumer->type = CONSUMER_DST_LOCAL;
498 break;
499 }
500
501 ret = LTTNG_OK;
502
503 error:
504 return ret;
505 }
506
507 /*
508 * Init tracing by creating trace directory and sending fds kernel consumer.
509 */
510 static int init_kernel_tracing(struct ltt_kernel_session *session)
511 {
512 int ret = 0;
513 struct lttng_ht_iter iter;
514 struct consumer_socket *socket;
515
516 assert(session);
517
518 rcu_read_lock();
519
520 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
521 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
522 socket, node.node) {
523 pthread_mutex_lock(socket->lock);
524 ret = kernel_consumer_send_session(socket, session);
525 pthread_mutex_unlock(socket->lock);
526 if (ret < 0) {
527 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
528 goto error;
529 }
530 }
531 }
532
533 error:
534 rcu_read_unlock();
535 return ret;
536 }
537
538 /*
539 * Create a socket to the relayd using the URI.
540 *
541 * On success, the relayd_sock pointer is set to the created socket.
542 * Else, it's stays untouched and a lttcomm error code is returned.
543 */
544 static int create_connect_relayd(struct lttng_uri *uri,
545 struct lttcomm_relayd_sock **relayd_sock)
546 {
547 int ret;
548 struct lttcomm_relayd_sock *rsock;
549
550 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
551 RELAYD_VERSION_COMM_MINOR);
552 if (!rsock) {
553 ret = LTTNG_ERR_FATAL;
554 goto error;
555 }
556
557 /*
558 * Connect to relayd so we can proceed with a session creation. This call
559 * can possibly block for an arbitrary amount of time to set the health
560 * state to be in poll execution.
561 */
562 health_poll_entry();
563 ret = relayd_connect(rsock);
564 health_poll_exit();
565 if (ret < 0) {
566 ERR("Unable to reach lttng-relayd");
567 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
568 goto free_sock;
569 }
570
571 /* Create socket for control stream. */
572 if (uri->stype == LTTNG_STREAM_CONTROL) {
573 DBG3("Creating relayd stream socket from URI");
574
575 /* Check relayd version */
576 ret = relayd_version_check(rsock);
577 if (ret < 0) {
578 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
579 goto close_sock;
580 }
581 } else if (uri->stype == LTTNG_STREAM_DATA) {
582 DBG3("Creating relayd data socket from URI");
583 } else {
584 /* Command is not valid */
585 ERR("Relayd invalid stream type: %d", uri->stype);
586 ret = LTTNG_ERR_INVALID;
587 goto close_sock;
588 }
589
590 *relayd_sock = rsock;
591
592 return LTTNG_OK;
593
594 close_sock:
595 /* The returned value is not useful since we are on an error path. */
596 (void) relayd_close(rsock);
597 free_sock:
598 free(rsock);
599 error:
600 return ret;
601 }
602
603 /*
604 * Connect to the relayd using URI and send the socket to the right consumer.
605 */
606 static int send_consumer_relayd_socket(int domain, unsigned int session_id,
607 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
608 struct consumer_socket *consumer_sock,
609 char *session_name, char *hostname, int session_live_timer)
610 {
611 int ret;
612 struct lttcomm_relayd_sock *rsock = NULL;
613
614 /* Connect to relayd and make version check if uri is the control. */
615 ret = create_connect_relayd(relayd_uri, &rsock);
616 if (ret != LTTNG_OK) {
617 goto error;
618 }
619 assert(rsock);
620
621 /* Set the network sequence index if not set. */
622 if (consumer->net_seq_index == (uint64_t) -1ULL) {
623 pthread_mutex_lock(&relayd_net_seq_idx_lock);
624 /*
625 * Increment net_seq_idx because we are about to transfer the
626 * new relayd socket to the consumer.
627 * Assign unique key so the consumer can match streams.
628 */
629 consumer->net_seq_index = ++relayd_net_seq_idx;
630 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
631 }
632
633 /* Send relayd socket to consumer. */
634 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
635 relayd_uri->stype, session_id,
636 session_name, hostname, session_live_timer);
637 if (ret < 0) {
638 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
639 goto close_sock;
640 }
641
642 /* Flag that the corresponding socket was sent. */
643 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
644 consumer_sock->control_sock_sent = 1;
645 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
646 consumer_sock->data_sock_sent = 1;
647 }
648
649 ret = LTTNG_OK;
650
651 /*
652 * Close socket which was dup on the consumer side. The session daemon does
653 * NOT keep track of the relayd socket(s) once transfer to the consumer.
654 */
655
656 close_sock:
657 (void) relayd_close(rsock);
658 free(rsock);
659
660 error:
661 if (ret != LTTNG_OK) {
662 /*
663 * The consumer output for this session should not be used anymore
664 * since the relayd connection failed thus making any tracing or/and
665 * streaming not usable.
666 */
667 consumer->enabled = 0;
668 }
669 return ret;
670 }
671
672 /*
673 * Send both relayd sockets to a specific consumer and domain. This is a
674 * helper function to facilitate sending the information to the consumer for a
675 * session.
676 */
677 static int send_consumer_relayd_sockets(int domain, unsigned int session_id,
678 struct consumer_output *consumer, struct consumer_socket *sock,
679 char *session_name, char *hostname, int session_live_timer)
680 {
681 int ret = LTTNG_OK;
682
683 assert(consumer);
684 assert(sock);
685
686 /* Sending control relayd socket. */
687 if (!sock->control_sock_sent) {
688 ret = send_consumer_relayd_socket(domain, session_id,
689 &consumer->dst.net.control, consumer, sock,
690 session_name, hostname, session_live_timer);
691 if (ret != LTTNG_OK) {
692 goto error;
693 }
694 }
695
696 /* Sending data relayd socket. */
697 if (!sock->data_sock_sent) {
698 ret = send_consumer_relayd_socket(domain, session_id,
699 &consumer->dst.net.data, consumer, sock,
700 session_name, hostname, session_live_timer);
701 if (ret != LTTNG_OK) {
702 goto error;
703 }
704 }
705
706 error:
707 return ret;
708 }
709
710 /*
711 * Setup relayd connections for a tracing session. First creates the socket to
712 * the relayd and send them to the right domain consumer. Consumer type MUST be
713 * network.
714 */
715 int cmd_setup_relayd(struct ltt_session *session)
716 {
717 int ret = LTTNG_OK;
718 struct ltt_ust_session *usess;
719 struct ltt_kernel_session *ksess;
720 struct consumer_socket *socket;
721 struct lttng_ht_iter iter;
722
723 assert(session);
724
725 usess = session->ust_session;
726 ksess = session->kernel_session;
727
728 DBG("Setting relayd for session %s", session->name);
729
730 rcu_read_lock();
731
732 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
733 && usess->consumer->enabled) {
734 /* For each consumer socket, send relayd sockets */
735 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
736 socket, node.node) {
737 pthread_mutex_lock(socket->lock);
738 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
739 usess->consumer, socket,
740 session->name, session->hostname,
741 session->live_timer);
742 pthread_mutex_unlock(socket->lock);
743 if (ret != LTTNG_OK) {
744 goto error;
745 }
746 /* Session is now ready for network streaming. */
747 session->net_handle = 1;
748 }
749 }
750
751 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
752 && ksess->consumer->enabled) {
753 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
754 socket, node.node) {
755 pthread_mutex_lock(socket->lock);
756 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
757 ksess->consumer, socket,
758 session->name, session->hostname,
759 session->live_timer);
760 pthread_mutex_unlock(socket->lock);
761 if (ret != LTTNG_OK) {
762 goto error;
763 }
764 /* Session is now ready for network streaming. */
765 session->net_handle = 1;
766 }
767 }
768
769 error:
770 rcu_read_unlock();
771 return ret;
772 }
773
774 /*
775 * Start a kernel session by opening all necessary streams.
776 */
777 static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
778 {
779 int ret;
780 struct ltt_kernel_channel *kchan;
781
782 /* Open kernel metadata */
783 if (ksess->metadata == NULL && ksess->output_traces) {
784 ret = kernel_open_metadata(ksess);
785 if (ret < 0) {
786 ret = LTTNG_ERR_KERN_META_FAIL;
787 goto error;
788 }
789 }
790
791 /* Open kernel metadata stream */
792 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
793 ret = kernel_open_metadata_stream(ksess);
794 if (ret < 0) {
795 ERR("Kernel create metadata stream failed");
796 ret = LTTNG_ERR_KERN_STREAM_FAIL;
797 goto error;
798 }
799 }
800
801 /* For each channel */
802 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
803 if (kchan->stream_count == 0) {
804 ret = kernel_open_channel_stream(kchan);
805 if (ret < 0) {
806 ret = LTTNG_ERR_KERN_STREAM_FAIL;
807 goto error;
808 }
809 /* Update the stream global counter */
810 ksess->stream_count_global += ret;
811 }
812 }
813
814 /* Setup kernel consumer socket and send fds to it */
815 ret = init_kernel_tracing(ksess);
816 if (ret != 0) {
817 ret = LTTNG_ERR_KERN_START_FAIL;
818 goto error;
819 }
820
821 /* This start the kernel tracing */
822 ret = kernel_start_session(ksess);
823 if (ret < 0) {
824 ret = LTTNG_ERR_KERN_START_FAIL;
825 goto error;
826 }
827
828 /* Quiescent wait after starting trace */
829 kernel_wait_quiescent(kernel_tracer_fd);
830
831 ksess->active = 1;
832
833 ret = LTTNG_OK;
834
835 error:
836 return ret;
837 }
838
839 /*
840 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
841 */
842 int cmd_disable_channel(struct ltt_session *session, int domain,
843 char *channel_name)
844 {
845 int ret;
846 struct ltt_ust_session *usess;
847
848 usess = session->ust_session;
849
850 rcu_read_lock();
851
852 switch (domain) {
853 case LTTNG_DOMAIN_KERNEL:
854 {
855 ret = channel_kernel_disable(session->kernel_session,
856 channel_name);
857 if (ret != LTTNG_OK) {
858 goto error;
859 }
860
861 kernel_wait_quiescent(kernel_tracer_fd);
862 break;
863 }
864 case LTTNG_DOMAIN_UST:
865 {
866 struct ltt_ust_channel *uchan;
867 struct lttng_ht *chan_ht;
868
869 chan_ht = usess->domain_global.channels;
870
871 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
872 if (uchan == NULL) {
873 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
874 goto error;
875 }
876
877 ret = channel_ust_disable(usess, uchan);
878 if (ret != LTTNG_OK) {
879 goto error;
880 }
881 break;
882 }
883 #if 0
884 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
885 case LTTNG_DOMAIN_UST_EXEC_NAME:
886 case LTTNG_DOMAIN_UST_PID:
887 #endif
888 default:
889 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
890 goto error;
891 }
892
893 ret = LTTNG_OK;
894
895 error:
896 rcu_read_unlock();
897 return ret;
898 }
899
900 /*
901 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
902 *
903 * The wpipe arguments is used as a notifier for the kernel thread.
904 */
905 int cmd_enable_channel(struct ltt_session *session,
906 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
907 {
908 int ret;
909 struct ltt_ust_session *usess = session->ust_session;
910 struct lttng_ht *chan_ht;
911 size_t len;
912
913 assert(session);
914 assert(attr);
915 assert(domain);
916
917 len = strnlen(attr->name, sizeof(attr->name));
918
919 /* Validate channel name */
920 if (attr->name[0] == '.' ||
921 memchr(attr->name, '/', len) != NULL) {
922 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
923 goto end;
924 }
925
926 DBG("Enabling channel %s for session %s", attr->name, session->name);
927
928 rcu_read_lock();
929
930 /*
931 * Don't try to enable a channel if the session has been started at
932 * some point in time before. The tracer does not allow it.
933 */
934 if (session->has_been_started) {
935 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
936 goto error;
937 }
938
939 /*
940 * If the session is a live session, remove the switch timer, the
941 * live timer does the same thing but sends also synchronisation
942 * beacons for inactive streams.
943 */
944 if (session->live_timer > 0) {
945 attr->attr.live_timer_interval = session->live_timer;
946 attr->attr.switch_timer_interval = 0;
947 }
948
949 /*
950 * The ringbuffer (both in user space and kernel) behave badly in overwrite
951 * mode and with less than 2 subbuf so block it right away and send back an
952 * invalid attribute error.
953 */
954 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
955 ret = LTTNG_ERR_INVALID;
956 goto error;
957 }
958
959 switch (domain->type) {
960 case LTTNG_DOMAIN_KERNEL:
961 {
962 struct ltt_kernel_channel *kchan;
963
964 kchan = trace_kernel_get_channel_by_name(attr->name,
965 session->kernel_session);
966 if (kchan == NULL) {
967 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
968 if (attr->name[0] != '\0') {
969 session->kernel_session->has_non_default_channel = 1;
970 }
971 } else {
972 ret = channel_kernel_enable(session->kernel_session, kchan);
973 }
974
975 if (ret != LTTNG_OK) {
976 goto error;
977 }
978
979 kernel_wait_quiescent(kernel_tracer_fd);
980 break;
981 }
982 case LTTNG_DOMAIN_UST:
983 {
984 struct ltt_ust_channel *uchan;
985
986 chan_ht = usess->domain_global.channels;
987
988 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
989 if (uchan == NULL) {
990 ret = channel_ust_create(usess, attr, domain->buf_type);
991 if (attr->name[0] != '\0') {
992 usess->has_non_default_channel = 1;
993 }
994 } else {
995 ret = channel_ust_enable(usess, uchan);
996 }
997 break;
998 }
999 default:
1000 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1001 goto error;
1002 }
1003
1004 error:
1005 rcu_read_unlock();
1006 end:
1007 return ret;
1008 }
1009
1010
1011 /*
1012 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1013 */
1014 int cmd_disable_event(struct ltt_session *session, int domain,
1015 char *channel_name, char *event_name)
1016 {
1017 int ret;
1018
1019 rcu_read_lock();
1020
1021 switch (domain) {
1022 case LTTNG_DOMAIN_KERNEL:
1023 {
1024 struct ltt_kernel_channel *kchan;
1025 struct ltt_kernel_session *ksess;
1026
1027 ksess = session->kernel_session;
1028
1029 /*
1030 * If a non-default channel has been created in the
1031 * session, explicitely require that -c chan_name needs
1032 * to be provided.
1033 */
1034 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1035 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1036 goto error;
1037 }
1038
1039 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1040 if (kchan == NULL) {
1041 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1042 goto error;
1043 }
1044
1045 ret = event_kernel_disable_tracepoint(kchan, event_name);
1046 if (ret != LTTNG_OK) {
1047 goto error;
1048 }
1049
1050 kernel_wait_quiescent(kernel_tracer_fd);
1051 break;
1052 }
1053 case LTTNG_DOMAIN_UST:
1054 {
1055 struct ltt_ust_channel *uchan;
1056 struct ltt_ust_session *usess;
1057
1058 usess = session->ust_session;
1059
1060 /*
1061 * If a non-default channel has been created in the
1062 * session, explicitely require that -c chan_name needs
1063 * to be provided.
1064 */
1065 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1066 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1067 goto error;
1068 }
1069
1070 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1071 channel_name);
1072 if (uchan == NULL) {
1073 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1074 goto error;
1075 }
1076
1077 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
1078 if (ret != LTTNG_OK) {
1079 goto error;
1080 }
1081
1082 DBG3("Disable UST event %s in channel %s completed", event_name,
1083 channel_name);
1084 break;
1085 }
1086 case LTTNG_DOMAIN_JUL:
1087 {
1088 struct ltt_ust_session *usess = session->ust_session;
1089
1090 assert(usess);
1091
1092 ret = event_jul_disable(usess, event_name);
1093 if (ret != LTTNG_OK) {
1094 goto error;
1095 }
1096
1097 break;
1098 }
1099 #if 0
1100 case LTTNG_DOMAIN_UST_EXEC_NAME:
1101 case LTTNG_DOMAIN_UST_PID:
1102 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1103 #endif
1104 default:
1105 ret = LTTNG_ERR_UND;
1106 goto error;
1107 }
1108
1109 ret = LTTNG_OK;
1110
1111 error:
1112 rcu_read_unlock();
1113 return ret;
1114 }
1115
1116 /*
1117 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1118 */
1119 int cmd_disable_event_all(struct ltt_session *session, int domain,
1120 char *channel_name)
1121 {
1122 int ret;
1123
1124 rcu_read_lock();
1125
1126 switch (domain) {
1127 case LTTNG_DOMAIN_KERNEL:
1128 {
1129 struct ltt_kernel_session *ksess;
1130 struct ltt_kernel_channel *kchan;
1131
1132 ksess = session->kernel_session;
1133
1134 /*
1135 * If a non-default channel has been created in the
1136 * session, explicitely require that -c chan_name needs
1137 * to be provided.
1138 */
1139 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1140 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1141 goto error;
1142 }
1143
1144 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1145 if (kchan == NULL) {
1146 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1147 goto error;
1148 }
1149
1150 ret = event_kernel_disable_all(kchan);
1151 if (ret != LTTNG_OK) {
1152 goto error;
1153 }
1154
1155 kernel_wait_quiescent(kernel_tracer_fd);
1156 break;
1157 }
1158 case LTTNG_DOMAIN_UST:
1159 {
1160 struct ltt_ust_session *usess;
1161 struct ltt_ust_channel *uchan;
1162
1163 usess = session->ust_session;
1164
1165 /*
1166 * If a non-default channel has been created in the
1167 * session, explicitely require that -c chan_name needs
1168 * to be provided.
1169 */
1170 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1171 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1172 goto error;
1173 }
1174
1175 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1176 channel_name);
1177 if (uchan == NULL) {
1178 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1179 goto error;
1180 }
1181
1182 ret = event_ust_disable_all_tracepoints(usess, uchan);
1183 if (ret != 0) {
1184 goto error;
1185 }
1186
1187 DBG3("Disable all UST events in channel %s completed", channel_name);
1188
1189 break;
1190 }
1191 case LTTNG_DOMAIN_JUL:
1192 {
1193 struct ltt_ust_session *usess = session->ust_session;
1194
1195 assert(usess);
1196
1197 ret = event_jul_disable_all(usess);
1198 if (ret != LTTNG_OK) {
1199 goto error;
1200 }
1201
1202 break;
1203 }
1204 #if 0
1205 case LTTNG_DOMAIN_UST_EXEC_NAME:
1206 case LTTNG_DOMAIN_UST_PID:
1207 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1208 #endif
1209 default:
1210 ret = LTTNG_ERR_UND;
1211 goto error;
1212 }
1213
1214 ret = LTTNG_OK;
1215
1216 error:
1217 rcu_read_unlock();
1218 return ret;
1219 }
1220
1221 /*
1222 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1223 */
1224 int cmd_add_context(struct ltt_session *session, int domain,
1225 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
1226 {
1227 int ret, chan_kern_created = 0, chan_ust_created = 0;
1228
1229 switch (domain) {
1230 case LTTNG_DOMAIN_KERNEL:
1231 assert(session->kernel_session);
1232
1233 if (session->kernel_session->channel_count == 0) {
1234 /* Create default channel */
1235 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1236 if (ret != LTTNG_OK) {
1237 goto error;
1238 }
1239 chan_kern_created = 1;
1240 }
1241 /* Add kernel context to kernel tracer */
1242 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1243 if (ret != LTTNG_OK) {
1244 goto error;
1245 }
1246 break;
1247 case LTTNG_DOMAIN_UST:
1248 {
1249 struct ltt_ust_session *usess = session->ust_session;
1250 unsigned int chan_count;
1251
1252 assert(usess);
1253
1254 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1255 if (chan_count == 0) {
1256 struct lttng_channel *attr;
1257 /* Create default channel */
1258 attr = channel_new_default_attr(domain, usess->buffer_type);
1259 if (attr == NULL) {
1260 ret = LTTNG_ERR_FATAL;
1261 goto error;
1262 }
1263
1264 ret = channel_ust_create(usess, attr, usess->buffer_type);
1265 if (ret != LTTNG_OK) {
1266 free(attr);
1267 goto error;
1268 }
1269 free(attr);
1270 chan_ust_created = 1;
1271 }
1272
1273 ret = context_ust_add(usess, domain, ctx, channel_name);
1274 if (ret != LTTNG_OK) {
1275 goto error;
1276 }
1277 break;
1278 }
1279 #if 0
1280 case LTTNG_DOMAIN_UST_EXEC_NAME:
1281 case LTTNG_DOMAIN_UST_PID:
1282 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1283 #endif
1284 default:
1285 ret = LTTNG_ERR_UND;
1286 goto error;
1287 }
1288
1289 return LTTNG_OK;
1290
1291 error:
1292 if (chan_kern_created) {
1293 struct ltt_kernel_channel *kchan =
1294 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1295 session->kernel_session);
1296 /* Created previously, this should NOT fail. */
1297 assert(kchan);
1298 kernel_destroy_channel(kchan);
1299 }
1300
1301 if (chan_ust_created) {
1302 struct ltt_ust_channel *uchan =
1303 trace_ust_find_channel_by_name(
1304 session->ust_session->domain_global.channels,
1305 DEFAULT_CHANNEL_NAME);
1306 /* Created previously, this should NOT fail. */
1307 assert(uchan);
1308 /* Remove from the channel list of the session. */
1309 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1310 uchan);
1311 trace_ust_destroy_channel(uchan);
1312 }
1313 return ret;
1314 }
1315
1316 /*
1317 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1318 * We own filter, exclusion, and filter_expression.
1319 */
1320 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
1321 char *channel_name, struct lttng_event *event,
1322 char *filter_expression,
1323 struct lttng_filter_bytecode *filter,
1324 struct lttng_event_exclusion *exclusion,
1325 int wpipe)
1326 {
1327 int ret, channel_created = 0;
1328 struct lttng_channel *attr;
1329
1330 assert(session);
1331 assert(event);
1332 assert(channel_name);
1333
1334 rcu_read_lock();
1335
1336 switch (domain->type) {
1337 case LTTNG_DOMAIN_KERNEL:
1338 {
1339 struct ltt_kernel_channel *kchan;
1340
1341 /*
1342 * If a non-default channel has been created in the
1343 * session, explicitely require that -c chan_name needs
1344 * to be provided.
1345 */
1346 if (session->kernel_session->has_non_default_channel
1347 && channel_name[0] == '\0') {
1348 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1349 goto error;
1350 }
1351
1352 kchan = trace_kernel_get_channel_by_name(channel_name,
1353 session->kernel_session);
1354 if (kchan == NULL) {
1355 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1356 LTTNG_BUFFER_GLOBAL);
1357 if (attr == NULL) {
1358 ret = LTTNG_ERR_FATAL;
1359 goto error;
1360 }
1361 strncpy(attr->name, channel_name, sizeof(attr->name));
1362
1363 ret = cmd_enable_channel(session, domain, attr, wpipe);
1364 if (ret != LTTNG_OK) {
1365 free(attr);
1366 goto error;
1367 }
1368 free(attr);
1369
1370 channel_created = 1;
1371 }
1372
1373 /* Get the newly created kernel channel pointer */
1374 kchan = trace_kernel_get_channel_by_name(channel_name,
1375 session->kernel_session);
1376 if (kchan == NULL) {
1377 /* This sould not happen... */
1378 ret = LTTNG_ERR_FATAL;
1379 goto error;
1380 }
1381
1382 ret = event_kernel_enable_tracepoint(kchan, event);
1383 if (ret != LTTNG_OK) {
1384 if (channel_created) {
1385 /* Let's not leak a useless channel. */
1386 kernel_destroy_channel(kchan);
1387 }
1388 goto error;
1389 }
1390
1391 kernel_wait_quiescent(kernel_tracer_fd);
1392 break;
1393 }
1394 case LTTNG_DOMAIN_UST:
1395 {
1396 struct ltt_ust_channel *uchan;
1397 struct ltt_ust_session *usess = session->ust_session;
1398
1399 assert(usess);
1400
1401 /*
1402 * If a non-default channel has been created in the
1403 * session, explicitely require that -c chan_name needs
1404 * to be provided.
1405 */
1406 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1407 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1408 goto error;
1409 }
1410
1411 /* Get channel from global UST domain */
1412 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1413 channel_name);
1414 if (uchan == NULL) {
1415 /* Create default channel */
1416 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1417 usess->buffer_type);
1418 if (attr == NULL) {
1419 ret = LTTNG_ERR_FATAL;
1420 goto error;
1421 }
1422 strncpy(attr->name, channel_name, sizeof(attr->name));
1423
1424 ret = cmd_enable_channel(session, domain, attr, wpipe);
1425 if (ret != LTTNG_OK) {
1426 free(attr);
1427 goto error;
1428 }
1429 free(attr);
1430
1431 /* Get the newly created channel reference back */
1432 uchan = trace_ust_find_channel_by_name(
1433 usess->domain_global.channels, channel_name);
1434 assert(uchan);
1435 }
1436
1437 /* At this point, the session and channel exist on the tracer */
1438 ret = event_ust_enable_tracepoint(usess, uchan, event,
1439 filter_expression, filter, exclusion);
1440 /* We have passed ownership */
1441 filter_expression = NULL;
1442 filter = NULL;
1443 exclusion = NULL;
1444 if (ret != LTTNG_OK) {
1445 goto error;
1446 }
1447 break;
1448 }
1449 case LTTNG_DOMAIN_JUL:
1450 {
1451 struct lttng_event uevent;
1452 struct lttng_domain tmp_dom;
1453 struct ltt_ust_session *usess = session->ust_session;
1454
1455 assert(usess);
1456
1457 /* Create the default JUL tracepoint. */
1458 memset(&uevent, 0, sizeof(uevent));
1459 uevent.type = LTTNG_EVENT_TRACEPOINT;
1460 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1461 if (is_root) {
1462 strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME,
1463 sizeof(uevent.name));
1464 } else {
1465 strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME,
1466 sizeof(uevent.name));
1467 }
1468 uevent.name[sizeof(uevent.name) - 1] = '\0';
1469
1470 /*
1471 * The domain type is changed because we are about to enable the
1472 * default channel and event for the JUL domain that are hardcoded.
1473 * This happens in the UST domain.
1474 */
1475 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1476 tmp_dom.type = LTTNG_DOMAIN_UST;
1477
1478 ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
1479 &uevent, filter_expression, filter, NULL, wpipe);
1480 /* We have passed ownership */
1481 filter_expression = NULL;
1482 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1483 goto error;
1484 }
1485
1486 /* The wild card * means that everything should be enabled. */
1487 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
1488 ret = event_jul_enable_all(usess, event, filter);
1489 filter = NULL;
1490 } else {
1491 ret = event_jul_enable(usess, event, filter);
1492 filter = NULL;
1493 }
1494 if (ret != LTTNG_OK) {
1495 goto error;
1496 }
1497
1498 break;
1499 }
1500 #if 0
1501 case LTTNG_DOMAIN_UST_EXEC_NAME:
1502 case LTTNG_DOMAIN_UST_PID:
1503 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1504 #endif
1505 default:
1506 ret = LTTNG_ERR_UND;
1507 goto error;
1508 }
1509
1510 ret = LTTNG_OK;
1511
1512 error:
1513 free(filter_expression);
1514 free(filter);
1515 free(exclusion);
1516 rcu_read_unlock();
1517 return ret;
1518 }
1519
1520 /*
1521 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1522 */
1523 int cmd_enable_event_all(struct ltt_session *session,
1524 struct lttng_domain *domain, char *channel_name, int event_type,
1525 char *filter_expression,
1526 struct lttng_filter_bytecode *filter, int wpipe)
1527 {
1528 int ret;
1529 struct lttng_channel *attr;
1530
1531 assert(session);
1532 assert(channel_name);
1533
1534 rcu_read_lock();
1535
1536 switch (domain->type) {
1537 case LTTNG_DOMAIN_KERNEL:
1538 {
1539 struct ltt_kernel_channel *kchan;
1540
1541 assert(session->kernel_session);
1542
1543 /*
1544 * If a non-default channel has been created in the
1545 * session, explicitely require that -c chan_name needs
1546 * to be provided.
1547 */
1548 if (session->kernel_session->has_non_default_channel
1549 && channel_name[0] == '\0') {
1550 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1551 goto error;
1552 }
1553
1554 kchan = trace_kernel_get_channel_by_name(channel_name,
1555 session->kernel_session);
1556 if (kchan == NULL) {
1557 /* Create default channel */
1558 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1559 LTTNG_BUFFER_GLOBAL);
1560 if (attr == NULL) {
1561 ret = LTTNG_ERR_FATAL;
1562 goto error;
1563 }
1564 strncpy(attr->name, channel_name, sizeof(attr->name));
1565
1566 ret = cmd_enable_channel(session, domain, attr, wpipe);
1567 if (ret != LTTNG_OK) {
1568 free(attr);
1569 goto error;
1570 }
1571 free(attr);
1572
1573 /* Get the newly created kernel channel pointer */
1574 kchan = trace_kernel_get_channel_by_name(channel_name,
1575 session->kernel_session);
1576 assert(kchan);
1577 }
1578
1579 switch (event_type) {
1580 case LTTNG_EVENT_SYSCALL:
1581 ret = event_kernel_enable_all_syscalls(kchan, kernel_tracer_fd);
1582 break;
1583 case LTTNG_EVENT_TRACEPOINT:
1584 /*
1585 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1586 * events already registered to the channel.
1587 */
1588 ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
1589 break;
1590 case LTTNG_EVENT_ALL:
1591 /* Enable syscalls and tracepoints */
1592 ret = event_kernel_enable_all(kchan, kernel_tracer_fd);
1593 break;
1594 default:
1595 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
1596 goto error;
1597 }
1598
1599 /* Manage return value */
1600 if (ret != LTTNG_OK) {
1601 /*
1602 * On error, cmd_enable_channel call will take care of destroying
1603 * the created channel if it was needed.
1604 */
1605 goto error;
1606 }
1607
1608 kernel_wait_quiescent(kernel_tracer_fd);
1609 break;
1610 }
1611 case LTTNG_DOMAIN_UST:
1612 {
1613 struct ltt_ust_channel *uchan;
1614 struct ltt_ust_session *usess = session->ust_session;
1615
1616 assert(usess);
1617
1618 /*
1619 * If a non-default channel has been created in the
1620 * session, explicitely require that -c chan_name needs
1621 * to be provided.
1622 */
1623 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1624 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1625 goto error;
1626 }
1627
1628 /* Get channel from global UST domain */
1629 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1630 channel_name);
1631 if (uchan == NULL) {
1632 /* Create default channel */
1633 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1634 usess->buffer_type);
1635 if (attr == NULL) {
1636 ret = LTTNG_ERR_FATAL;
1637 goto error;
1638 }
1639 strncpy(attr->name, channel_name, sizeof(attr->name));
1640
1641 ret = cmd_enable_channel(session, domain, attr, wpipe);
1642 if (ret != LTTNG_OK) {
1643 free(attr);
1644 goto error;
1645 }
1646 free(attr);
1647
1648 /* Get the newly created channel reference back */
1649 uchan = trace_ust_find_channel_by_name(
1650 usess->domain_global.channels, channel_name);
1651 assert(uchan);
1652 }
1653
1654 /* At this point, the session and channel exist on the tracer */
1655
1656 switch (event_type) {
1657 case LTTNG_EVENT_ALL:
1658 case LTTNG_EVENT_TRACEPOINT:
1659 ret = event_ust_enable_all_tracepoints(usess, uchan,
1660 filter_expression, filter);
1661 if (ret != LTTNG_OK) {
1662 goto error;
1663 }
1664 break;
1665 default:
1666 ret = LTTNG_ERR_UST_ENABLE_FAIL;
1667 goto error;
1668 }
1669
1670 /* Manage return value */
1671 if (ret != LTTNG_OK) {
1672 goto error;
1673 }
1674
1675 break;
1676 }
1677 case LTTNG_DOMAIN_JUL:
1678 {
1679 struct lttng_event uevent, event;
1680 struct lttng_domain tmp_dom;
1681 struct ltt_ust_session *usess = session->ust_session;
1682
1683 assert(usess);
1684
1685 /* Create the default JUL tracepoint. */
1686 uevent.type = LTTNG_EVENT_TRACEPOINT;
1687 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1688 if (is_root) {
1689 strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME,
1690 sizeof(uevent.name));
1691 } else {
1692 strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME,
1693 sizeof(uevent.name));
1694 }
1695 uevent.name[sizeof(uevent.name) - 1] = '\0';
1696
1697 /*
1698 * The domain type is changed because we are about to enable the
1699 * default channel and event for the JUL domain that are hardcoded.
1700 * This happens in the UST domain.
1701 */
1702 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1703 tmp_dom.type = LTTNG_DOMAIN_UST;
1704
1705 ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
1706 &uevent, NULL, NULL, NULL, wpipe);
1707 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1708 goto error;
1709 }
1710
1711 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1712 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1713 strncpy(event.name, "*", sizeof(event.name));
1714 event.name[sizeof(event.name) - 1] = '\0';
1715
1716 ret = event_jul_enable_all(usess, &event, filter);
1717 if (ret != LTTNG_OK) {
1718 goto error;
1719 }
1720
1721 break;
1722 }
1723 #if 0
1724 case LTTNG_DOMAIN_UST_EXEC_NAME:
1725 case LTTNG_DOMAIN_UST_PID:
1726 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1727 #endif
1728 default:
1729 ret = LTTNG_ERR_UND;
1730 goto error;
1731 }
1732
1733 ret = LTTNG_OK;
1734
1735 error:
1736 rcu_read_unlock();
1737 return ret;
1738 }
1739
1740
1741 /*
1742 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1743 */
1744 ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1745 {
1746 int ret;
1747 ssize_t nb_events = 0;
1748
1749 switch (domain) {
1750 case LTTNG_DOMAIN_KERNEL:
1751 nb_events = kernel_list_events(kernel_tracer_fd, events);
1752 if (nb_events < 0) {
1753 ret = LTTNG_ERR_KERN_LIST_FAIL;
1754 goto error;
1755 }
1756 break;
1757 case LTTNG_DOMAIN_UST:
1758 nb_events = ust_app_list_events(events);
1759 if (nb_events < 0) {
1760 ret = LTTNG_ERR_UST_LIST_FAIL;
1761 goto error;
1762 }
1763 break;
1764 case LTTNG_DOMAIN_JUL:
1765 nb_events = jul_list_events(events);
1766 if (nb_events < 0) {
1767 ret = LTTNG_ERR_UST_LIST_FAIL;
1768 goto error;
1769 }
1770 break;
1771 default:
1772 ret = LTTNG_ERR_UND;
1773 goto error;
1774 }
1775
1776 return nb_events;
1777
1778 error:
1779 /* Return negative value to differentiate return code */
1780 return -ret;
1781 }
1782
1783 /*
1784 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1785 */
1786 ssize_t cmd_list_tracepoint_fields(int domain,
1787 struct lttng_event_field **fields)
1788 {
1789 int ret;
1790 ssize_t nb_fields = 0;
1791
1792 switch (domain) {
1793 case LTTNG_DOMAIN_UST:
1794 nb_fields = ust_app_list_event_fields(fields);
1795 if (nb_fields < 0) {
1796 ret = LTTNG_ERR_UST_LIST_FAIL;
1797 goto error;
1798 }
1799 break;
1800 case LTTNG_DOMAIN_KERNEL:
1801 default: /* fall-through */
1802 ret = LTTNG_ERR_UND;
1803 goto error;
1804 }
1805
1806 return nb_fields;
1807
1808 error:
1809 /* Return negative value to differentiate return code */
1810 return -ret;
1811 }
1812
1813 /*
1814 * Command LTTNG_START_TRACE processed by the client thread.
1815 */
1816 int cmd_start_trace(struct ltt_session *session)
1817 {
1818 int ret;
1819 unsigned long nb_chan = 0;
1820 struct ltt_kernel_session *ksession;
1821 struct ltt_ust_session *usess;
1822
1823 assert(session);
1824
1825 /* Ease our life a bit ;) */
1826 ksession = session->kernel_session;
1827 usess = session->ust_session;
1828
1829 /* Is the session already started? */
1830 if (session->active) {
1831 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1832 goto error;
1833 }
1834
1835 /*
1836 * Starting a session without channel is useless since after that it's not
1837 * possible to enable channel thus inform the client.
1838 */
1839 if (usess && usess->domain_global.channels) {
1840 rcu_read_lock();
1841 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
1842 rcu_read_unlock();
1843 }
1844 if (ksession) {
1845 nb_chan += ksession->channel_count;
1846 }
1847 if (!nb_chan) {
1848 ret = LTTNG_ERR_NO_CHANNEL;
1849 goto error;
1850 }
1851
1852 /* Kernel tracing */
1853 if (ksession != NULL) {
1854 ret = start_kernel_session(ksession, kernel_tracer_fd);
1855 if (ret != LTTNG_OK) {
1856 goto error;
1857 }
1858 }
1859
1860 /* Flag session that trace should start automatically */
1861 if (usess) {
1862 /*
1863 * Even though the start trace might fail, flag this session active so
1864 * other application coming in are started by default.
1865 */
1866 usess->active = 1;
1867
1868 ret = ust_app_start_trace_all(usess);
1869 if (ret < 0) {
1870 ret = LTTNG_ERR_UST_START_FAIL;
1871 goto error;
1872 }
1873 }
1874
1875 /* Flag this after a successful start. */
1876 session->has_been_started = 1;
1877 session->active = 1;
1878
1879 ret = LTTNG_OK;
1880
1881 error:
1882 return ret;
1883 }
1884
1885 /*
1886 * Command LTTNG_STOP_TRACE processed by the client thread.
1887 */
1888 int cmd_stop_trace(struct ltt_session *session)
1889 {
1890 int ret;
1891 struct ltt_kernel_channel *kchan;
1892 struct ltt_kernel_session *ksession;
1893 struct ltt_ust_session *usess;
1894
1895 assert(session);
1896
1897 /* Short cut */
1898 ksession = session->kernel_session;
1899 usess = session->ust_session;
1900
1901 /* Session is not active. Skip everythong and inform the client. */
1902 if (!session->active) {
1903 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
1904 goto error;
1905 }
1906
1907 /* Kernel tracer */
1908 if (ksession && ksession->active) {
1909 DBG("Stop kernel tracing");
1910
1911 /* Flush metadata if exist */
1912 if (ksession->metadata_stream_fd >= 0) {
1913 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1914 if (ret < 0) {
1915 ERR("Kernel metadata flush failed");
1916 }
1917 }
1918
1919 /* Flush all buffers before stopping */
1920 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1921 ret = kernel_flush_buffer(kchan);
1922 if (ret < 0) {
1923 ERR("Kernel flush buffer error");
1924 }
1925 }
1926
1927 ret = kernel_stop_session(ksession);
1928 if (ret < 0) {
1929 ret = LTTNG_ERR_KERN_STOP_FAIL;
1930 goto error;
1931 }
1932
1933 kernel_wait_quiescent(kernel_tracer_fd);
1934
1935 ksession->active = 0;
1936 }
1937
1938 if (usess && usess->active) {
1939 /*
1940 * Even though the stop trace might fail, flag this session inactive so
1941 * other application coming in are not started by default.
1942 */
1943 usess->active = 0;
1944
1945 ret = ust_app_stop_trace_all(usess);
1946 if (ret < 0) {
1947 ret = LTTNG_ERR_UST_STOP_FAIL;
1948 goto error;
1949 }
1950 }
1951
1952 /* Flag inactive after a successful stop. */
1953 session->active = 0;
1954 ret = LTTNG_OK;
1955
1956 error:
1957 return ret;
1958 }
1959
1960 /*
1961 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1962 */
1963 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
1964 struct lttng_uri *uris)
1965 {
1966 int ret, i;
1967 struct ltt_kernel_session *ksess = session->kernel_session;
1968 struct ltt_ust_session *usess = session->ust_session;
1969
1970 assert(session);
1971 assert(uris);
1972 assert(nb_uri > 0);
1973
1974 /* Can't set consumer URI if the session is active. */
1975 if (session->active) {
1976 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1977 goto error;
1978 }
1979
1980 /* Set the "global" consumer URIs */
1981 for (i = 0; i < nb_uri; i++) {
1982 ret = add_uri_to_consumer(session->consumer,
1983 &uris[i], 0, session->name);
1984 if (ret != LTTNG_OK) {
1985 goto error;
1986 }
1987 }
1988
1989 /* Set UST session URIs */
1990 if (session->ust_session) {
1991 for (i = 0; i < nb_uri; i++) {
1992 ret = add_uri_to_consumer(
1993 session->ust_session->consumer,
1994 &uris[i], LTTNG_DOMAIN_UST,
1995 session->name);
1996 if (ret != LTTNG_OK) {
1997 goto error;
1998 }
1999 }
2000 }
2001
2002 /* Set kernel session URIs */
2003 if (session->kernel_session) {
2004 for (i = 0; i < nb_uri; i++) {
2005 ret = add_uri_to_consumer(
2006 session->kernel_session->consumer,
2007 &uris[i], LTTNG_DOMAIN_KERNEL,
2008 session->name);
2009 if (ret != LTTNG_OK) {
2010 goto error;
2011 }
2012 }
2013 }
2014
2015 /*
2016 * Make sure to set the session in output mode after we set URI since a
2017 * session can be created without URL (thus flagged in no output mode).
2018 */
2019 session->output_traces = 1;
2020 if (ksess) {
2021 ksess->output_traces = 1;
2022 }
2023
2024 if (usess) {
2025 usess->output_traces = 1;
2026 }
2027
2028 /* All good! */
2029 ret = LTTNG_OK;
2030
2031 error:
2032 return ret;
2033 }
2034
2035 /*
2036 * Command LTTNG_CREATE_SESSION processed by the client thread.
2037 */
2038 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
2039 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2040 {
2041 int ret;
2042 struct ltt_session *session;
2043
2044 assert(name);
2045 assert(creds);
2046
2047 /*
2048 * Verify if the session already exist
2049 *
2050 * XXX: There is no need for the session lock list here since the caller
2051 * (process_client_msg) is holding it. We might want to change that so a
2052 * single command does not lock the entire session list.
2053 */
2054 session = session_find_by_name(name);
2055 if (session != NULL) {
2056 ret = LTTNG_ERR_EXIST_SESS;
2057 goto find_error;
2058 }
2059
2060 /* Create tracing session in the registry */
2061 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2062 LTTNG_SOCK_GET_GID_CRED(creds));
2063 if (ret != LTTNG_OK) {
2064 goto session_error;
2065 }
2066
2067 /*
2068 * Get the newly created session pointer back
2069 *
2070 * XXX: There is no need for the session lock list here since the caller
2071 * (process_client_msg) is holding it. We might want to change that so a
2072 * single command does not lock the entire session list.
2073 */
2074 session = session_find_by_name(name);
2075 assert(session);
2076
2077 session->live_timer = live_timer;
2078 /* Create default consumer output for the session not yet created. */
2079 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2080 if (session->consumer == NULL) {
2081 ret = LTTNG_ERR_FATAL;
2082 goto consumer_error;
2083 }
2084
2085 if (uris) {
2086 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2087 if (ret != LTTNG_OK) {
2088 goto consumer_error;
2089 }
2090 session->output_traces = 1;
2091 } else {
2092 session->output_traces = 0;
2093 DBG2("Session %s created with no output", session->name);
2094 }
2095
2096 session->consumer->enabled = 1;
2097
2098 return LTTNG_OK;
2099
2100 consumer_error:
2101 session_destroy(session);
2102 session_error:
2103 find_error:
2104 return ret;
2105 }
2106
2107 /*
2108 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2109 */
2110 int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2111 size_t nb_uri, lttng_sock_cred *creds)
2112 {
2113 int ret;
2114 struct ltt_session *session;
2115 struct snapshot_output *new_output = NULL;
2116
2117 assert(name);
2118 assert(creds);
2119
2120 /*
2121 * Create session in no output mode with URIs set to NULL. The uris we've
2122 * received are for a default snapshot output if one.
2123 */
2124 ret = cmd_create_session_uri(name, NULL, 0, creds, -1);
2125 if (ret != LTTNG_OK) {
2126 goto error;
2127 }
2128
2129 /* Get the newly created session pointer back. This should NEVER fail. */
2130 session = session_find_by_name(name);
2131 assert(session);
2132
2133 /* Flag session for snapshot mode. */
2134 session->snapshot_mode = 1;
2135
2136 /* Skip snapshot output creation if no URI is given. */
2137 if (nb_uri == 0) {
2138 goto end;
2139 }
2140
2141 new_output = snapshot_output_alloc();
2142 if (!new_output) {
2143 ret = LTTNG_ERR_NOMEM;
2144 goto error_snapshot_alloc;
2145 }
2146
2147 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2148 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2149 if (ret < 0) {
2150 if (ret == -ENOMEM) {
2151 ret = LTTNG_ERR_NOMEM;
2152 } else {
2153 ret = LTTNG_ERR_INVALID;
2154 }
2155 goto error_snapshot;
2156 }
2157
2158 rcu_read_lock();
2159 snapshot_add_output(&session->snapshot, new_output);
2160 rcu_read_unlock();
2161
2162 end:
2163 return LTTNG_OK;
2164
2165 error_snapshot:
2166 snapshot_output_destroy(new_output);
2167 error_snapshot_alloc:
2168 session_destroy(session);
2169 error:
2170 return ret;
2171 }
2172
2173 /*
2174 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2175 */
2176 int cmd_destroy_session(struct ltt_session *session, int wpipe)
2177 {
2178 int ret;
2179 struct ltt_ust_session *usess;
2180 struct ltt_kernel_session *ksess;
2181
2182 /* Safety net */
2183 assert(session);
2184
2185 usess = session->ust_session;
2186 ksess = session->kernel_session;
2187
2188 /* Clean kernel session teardown */
2189 kernel_destroy_session(ksess);
2190
2191 /* UST session teardown */
2192 if (usess) {
2193 /* Close any relayd session */
2194 consumer_output_send_destroy_relayd(usess->consumer);
2195
2196 /* Destroy every UST application related to this session. */
2197 ret = ust_app_destroy_trace_all(usess);
2198 if (ret) {
2199 ERR("Error in ust_app_destroy_trace_all");
2200 }
2201
2202 /* Clean up the rest. */
2203 trace_ust_destroy_session(usess);
2204 }
2205
2206 /*
2207 * Must notify the kernel thread here to update it's poll set in order to
2208 * remove the channel(s)' fd just destroyed.
2209 */
2210 ret = notify_thread_pipe(wpipe);
2211 if (ret < 0) {
2212 PERROR("write kernel poll pipe");
2213 }
2214
2215 ret = session_destroy(session);
2216
2217 return ret;
2218 }
2219
2220 /*
2221 * Command LTTNG_CALIBRATE processed by the client thread.
2222 */
2223 int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
2224 {
2225 int ret;
2226
2227 switch (domain) {
2228 case LTTNG_DOMAIN_KERNEL:
2229 {
2230 struct lttng_kernel_calibrate kcalibrate;
2231
2232 switch (calibrate->type) {
2233 case LTTNG_CALIBRATE_FUNCTION:
2234 default:
2235 /* Default and only possible calibrate option. */
2236 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2237 break;
2238 }
2239
2240 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2241 if (ret < 0) {
2242 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2243 goto error;
2244 }
2245 break;
2246 }
2247 case LTTNG_DOMAIN_UST:
2248 {
2249 struct lttng_ust_calibrate ucalibrate;
2250
2251 switch (calibrate->type) {
2252 case LTTNG_CALIBRATE_FUNCTION:
2253 default:
2254 /* Default and only possible calibrate option. */
2255 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2256 break;
2257 }
2258
2259 ret = ust_app_calibrate_glb(&ucalibrate);
2260 if (ret < 0) {
2261 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2262 goto error;
2263 }
2264 break;
2265 }
2266 default:
2267 ret = LTTNG_ERR_UND;
2268 goto error;
2269 }
2270
2271 ret = LTTNG_OK;
2272
2273 error:
2274 return ret;
2275 }
2276
2277 /*
2278 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2279 */
2280 int cmd_register_consumer(struct ltt_session *session, int domain,
2281 const char *sock_path, struct consumer_data *cdata)
2282 {
2283 int ret, sock;
2284 struct consumer_socket *socket = NULL;
2285
2286 assert(session);
2287 assert(cdata);
2288 assert(sock_path);
2289
2290 switch (domain) {
2291 case LTTNG_DOMAIN_KERNEL:
2292 {
2293 struct ltt_kernel_session *ksess = session->kernel_session;
2294
2295 assert(ksess);
2296
2297 /* Can't register a consumer if there is already one */
2298 if (ksess->consumer_fds_sent != 0) {
2299 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2300 goto error;
2301 }
2302
2303 sock = lttcomm_connect_unix_sock(sock_path);
2304 if (sock < 0) {
2305 ret = LTTNG_ERR_CONNECT_FAIL;
2306 goto error;
2307 }
2308 cdata->cmd_sock = sock;
2309
2310 socket = consumer_allocate_socket(&cdata->cmd_sock);
2311 if (socket == NULL) {
2312 ret = close(sock);
2313 if (ret < 0) {
2314 PERROR("close register consumer");
2315 }
2316 cdata->cmd_sock = -1;
2317 ret = LTTNG_ERR_FATAL;
2318 goto error;
2319 }
2320
2321 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2322 if (socket->lock == NULL) {
2323 PERROR("zmalloc pthread mutex");
2324 ret = LTTNG_ERR_FATAL;
2325 goto error;
2326 }
2327 pthread_mutex_init(socket->lock, NULL);
2328 socket->registered = 1;
2329
2330 rcu_read_lock();
2331 consumer_add_socket(socket, ksess->consumer);
2332 rcu_read_unlock();
2333
2334 pthread_mutex_lock(&cdata->pid_mutex);
2335 cdata->pid = -1;
2336 pthread_mutex_unlock(&cdata->pid_mutex);
2337
2338 break;
2339 }
2340 default:
2341 /* TODO: Userspace tracing */
2342 ret = LTTNG_ERR_UND;
2343 goto error;
2344 }
2345
2346 return LTTNG_OK;
2347
2348 error:
2349 if (socket) {
2350 consumer_destroy_socket(socket);
2351 }
2352 return ret;
2353 }
2354
2355 /*
2356 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2357 */
2358 ssize_t cmd_list_domains(struct ltt_session *session,
2359 struct lttng_domain **domains)
2360 {
2361 int ret, index = 0;
2362 ssize_t nb_dom = 0;
2363
2364 if (session->kernel_session != NULL) {
2365 DBG3("Listing domains found kernel domain");
2366 nb_dom++;
2367 }
2368
2369 if (session->ust_session != NULL) {
2370 DBG3("Listing domains found UST global domain");
2371 nb_dom++;
2372
2373 if (session->ust_session->domain_jul.being_used) {
2374 nb_dom++;
2375 }
2376 }
2377
2378 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2379 if (*domains == NULL) {
2380 ret = LTTNG_ERR_FATAL;
2381 goto error;
2382 }
2383
2384 if (session->kernel_session != NULL) {
2385 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2386 index++;
2387 }
2388
2389 if (session->ust_session != NULL) {
2390 (*domains)[index].type = LTTNG_DOMAIN_UST;
2391 (*domains)[index].buf_type = session->ust_session->buffer_type;
2392 index++;
2393
2394 if (session->ust_session->domain_jul.being_used) {
2395 (*domains)[index].type = LTTNG_DOMAIN_JUL;
2396 (*domains)[index].buf_type = session->ust_session->buffer_type;
2397 index++;
2398 }
2399 }
2400
2401 return nb_dom;
2402
2403 error:
2404 /* Return negative value to differentiate return code */
2405 return -ret;
2406 }
2407
2408
2409 /*
2410 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2411 */
2412 ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2413 struct lttng_channel **channels)
2414 {
2415 int ret;
2416 ssize_t nb_chan = 0;
2417
2418 switch (domain) {
2419 case LTTNG_DOMAIN_KERNEL:
2420 if (session->kernel_session != NULL) {
2421 nb_chan = session->kernel_session->channel_count;
2422 }
2423 DBG3("Number of kernel channels %zd", nb_chan);
2424 if (nb_chan <= 0) {
2425 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2426 }
2427 break;
2428 case LTTNG_DOMAIN_UST:
2429 if (session->ust_session != NULL) {
2430 rcu_read_lock();
2431 nb_chan = lttng_ht_get_count(
2432 session->ust_session->domain_global.channels);
2433 rcu_read_unlock();
2434 }
2435 DBG3("Number of UST global channels %zd", nb_chan);
2436 if (nb_chan < 0) {
2437 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2438 goto error;
2439 }
2440 break;
2441 default:
2442 ret = LTTNG_ERR_UND;
2443 goto error;
2444 }
2445
2446 if (nb_chan > 0) {
2447 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2448 if (*channels == NULL) {
2449 ret = LTTNG_ERR_FATAL;
2450 goto error;
2451 }
2452
2453 list_lttng_channels(domain, session, *channels);
2454 }
2455
2456 return nb_chan;
2457
2458 error:
2459 /* Return negative value to differentiate return code */
2460 return -ret;
2461 }
2462
2463 /*
2464 * Command LTTNG_LIST_EVENTS processed by the client thread.
2465 */
2466 ssize_t cmd_list_events(int domain, struct ltt_session *session,
2467 char *channel_name, struct lttng_event **events)
2468 {
2469 int ret = 0;
2470 ssize_t nb_event = 0;
2471
2472 switch (domain) {
2473 case LTTNG_DOMAIN_KERNEL:
2474 if (session->kernel_session != NULL) {
2475 nb_event = list_lttng_kernel_events(channel_name,
2476 session->kernel_session, events);
2477 }
2478 break;
2479 case LTTNG_DOMAIN_UST:
2480 {
2481 if (session->ust_session != NULL) {
2482 nb_event = list_lttng_ust_global_events(channel_name,
2483 &session->ust_session->domain_global, events);
2484 }
2485 break;
2486 }
2487 case LTTNG_DOMAIN_JUL:
2488 if (session->ust_session) {
2489 nb_event = list_lttng_jul_events(
2490 &session->ust_session->domain_jul, events);
2491 }
2492 break;
2493 default:
2494 ret = LTTNG_ERR_UND;
2495 goto error;
2496 }
2497
2498 return nb_event;
2499
2500 error:
2501 /* Return negative value to differentiate return code */
2502 return -ret;
2503 }
2504
2505 /*
2506 * Using the session list, filled a lttng_session array to send back to the
2507 * client for session listing.
2508 *
2509 * The session list lock MUST be acquired before calling this function. Use
2510 * session_lock_list() and session_unlock_list().
2511 */
2512 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2513 gid_t gid)
2514 {
2515 int ret;
2516 unsigned int i = 0;
2517 struct ltt_session *session;
2518 struct ltt_session_list *list = session_get_list();
2519
2520 DBG("Getting all available session for UID %d GID %d",
2521 uid, gid);
2522 /*
2523 * Iterate over session list and append data after the control struct in
2524 * the buffer.
2525 */
2526 cds_list_for_each_entry(session, &list->head, list) {
2527 /*
2528 * Only list the sessions the user can control.
2529 */
2530 if (!session_access_ok(session, uid, gid)) {
2531 continue;
2532 }
2533
2534 struct ltt_kernel_session *ksess = session->kernel_session;
2535 struct ltt_ust_session *usess = session->ust_session;
2536
2537 if (session->consumer->type == CONSUMER_DST_NET ||
2538 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2539 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2540 ret = build_network_session_path(sessions[i].path,
2541 sizeof(sessions[i].path), session);
2542 } else {
2543 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2544 session->consumer->dst.trace_path);
2545 }
2546 if (ret < 0) {
2547 PERROR("snprintf session path");
2548 continue;
2549 }
2550
2551 strncpy(sessions[i].name, session->name, NAME_MAX);
2552 sessions[i].name[NAME_MAX - 1] = '\0';
2553 sessions[i].enabled = session->active;
2554 sessions[i].snapshot_mode = session->snapshot_mode;
2555 sessions[i].live_timer_interval = session->live_timer;
2556 i++;
2557 }
2558 }
2559
2560 /*
2561 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2562 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2563 */
2564 int cmd_data_pending(struct ltt_session *session)
2565 {
2566 int ret;
2567 struct ltt_kernel_session *ksess = session->kernel_session;
2568 struct ltt_ust_session *usess = session->ust_session;
2569
2570 assert(session);
2571
2572 /* Session MUST be stopped to ask for data availability. */
2573 if (session->active) {
2574 ret = LTTNG_ERR_SESSION_STARTED;
2575 goto error;
2576 } else {
2577 /*
2578 * If stopped, just make sure we've started before else the above call
2579 * will always send that there is data pending.
2580 *
2581 * The consumer assumes that when the data pending command is received,
2582 * the trace has been started before or else no output data is written
2583 * by the streams which is a condition for data pending. So, this is
2584 * *VERY* important that we don't ask the consumer before a start
2585 * trace.
2586 */
2587 if (!session->has_been_started) {
2588 ret = 0;
2589 goto error;
2590 }
2591 }
2592
2593 if (ksess && ksess->consumer) {
2594 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2595 if (ret == 1) {
2596 /* Data is still being extracted for the kernel. */
2597 goto error;
2598 }
2599 }
2600
2601 if (usess && usess->consumer) {
2602 ret = consumer_is_data_pending(usess->id, usess->consumer);
2603 if (ret == 1) {
2604 /* Data is still being extracted for the kernel. */
2605 goto error;
2606 }
2607 }
2608
2609 /* Data is ready to be read by a viewer */
2610 ret = 0;
2611
2612 error:
2613 return ret;
2614 }
2615
2616 /*
2617 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2618 *
2619 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2620 */
2621 int cmd_snapshot_add_output(struct ltt_session *session,
2622 struct lttng_snapshot_output *output, uint32_t *id)
2623 {
2624 int ret;
2625 struct snapshot_output *new_output;
2626
2627 assert(session);
2628 assert(output);
2629
2630 DBG("Cmd snapshot add output for session %s", session->name);
2631
2632 /*
2633 * Permission denied to create an output if the session is not
2634 * set in no output mode.
2635 */
2636 if (session->output_traces) {
2637 ret = LTTNG_ERR_EPERM;
2638 goto error;
2639 }
2640
2641 /* Only one output is allowed until we have the "tee" feature. */
2642 if (session->snapshot.nb_output == 1) {
2643 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
2644 goto error;
2645 }
2646
2647 new_output = snapshot_output_alloc();
2648 if (!new_output) {
2649 ret = LTTNG_ERR_NOMEM;
2650 goto error;
2651 }
2652
2653 ret = snapshot_output_init(output->max_size, output->name,
2654 output->ctrl_url, output->data_url, session->consumer, new_output,
2655 &session->snapshot);
2656 if (ret < 0) {
2657 if (ret == -ENOMEM) {
2658 ret = LTTNG_ERR_NOMEM;
2659 } else {
2660 ret = LTTNG_ERR_INVALID;
2661 }
2662 goto free_error;
2663 }
2664
2665 rcu_read_lock();
2666 snapshot_add_output(&session->snapshot, new_output);
2667 if (id) {
2668 *id = new_output->id;
2669 }
2670 rcu_read_unlock();
2671
2672 return LTTNG_OK;
2673
2674 free_error:
2675 snapshot_output_destroy(new_output);
2676 error:
2677 return ret;
2678 }
2679
2680 /*
2681 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2682 *
2683 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2684 */
2685 int cmd_snapshot_del_output(struct ltt_session *session,
2686 struct lttng_snapshot_output *output)
2687 {
2688 int ret;
2689 struct snapshot_output *sout = NULL;
2690
2691 assert(session);
2692 assert(output);
2693
2694 rcu_read_lock();
2695
2696 /*
2697 * Permission denied to create an output if the session is not
2698 * set in no output mode.
2699 */
2700 if (session->output_traces) {
2701 ret = LTTNG_ERR_EPERM;
2702 goto error;
2703 }
2704
2705 if (output->id) {
2706 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
2707 session->name);
2708 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
2709 } else if (*output->name != '\0') {
2710 DBG("Cmd snapshot del output name %s for session %s", output->name,
2711 session->name);
2712 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
2713 }
2714 if (!sout) {
2715 ret = LTTNG_ERR_INVALID;
2716 goto error;
2717 }
2718
2719 snapshot_delete_output(&session->snapshot, sout);
2720 snapshot_output_destroy(sout);
2721 ret = LTTNG_OK;
2722
2723 error:
2724 rcu_read_unlock();
2725 return ret;
2726 }
2727
2728 /*
2729 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2730 *
2731 * If no output is available, outputs is untouched and 0 is returned.
2732 *
2733 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2734 */
2735 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
2736 struct lttng_snapshot_output **outputs)
2737 {
2738 int ret, idx = 0;
2739 struct lttng_snapshot_output *list = NULL;
2740 struct lttng_ht_iter iter;
2741 struct snapshot_output *output;
2742
2743 assert(session);
2744 assert(outputs);
2745
2746 DBG("Cmd snapshot list outputs for session %s", session->name);
2747
2748 /*
2749 * Permission denied to create an output if the session is not
2750 * set in no output mode.
2751 */
2752 if (session->output_traces) {
2753 ret = -LTTNG_ERR_EPERM;
2754 goto error;
2755 }
2756
2757 if (session->snapshot.nb_output == 0) {
2758 ret = 0;
2759 goto error;
2760 }
2761
2762 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
2763 if (!list) {
2764 ret = -LTTNG_ERR_NOMEM;
2765 goto error;
2766 }
2767
2768 /* Copy list from session to the new list object. */
2769 rcu_read_lock();
2770 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
2771 output, node.node) {
2772 assert(output->consumer);
2773 list[idx].id = output->id;
2774 list[idx].max_size = output->max_size;
2775 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
2776 if (output->consumer->type == CONSUMER_DST_LOCAL) {
2777 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
2778 sizeof(list[idx].ctrl_url));
2779 } else {
2780 /* Control URI. */
2781 ret = uri_to_str_url(&output->consumer->dst.net.control,
2782 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
2783 if (ret < 0) {
2784 ret = -LTTNG_ERR_NOMEM;
2785 goto error;
2786 }
2787
2788 /* Data URI. */
2789 ret = uri_to_str_url(&output->consumer->dst.net.data,
2790 list[idx].data_url, sizeof(list[idx].data_url));
2791 if (ret < 0) {
2792 ret = -LTTNG_ERR_NOMEM;
2793 goto error;
2794 }
2795 }
2796 idx++;
2797 }
2798
2799 *outputs = list;
2800 list = NULL;
2801 ret = session->snapshot.nb_output;
2802 error:
2803 free(list);
2804 rcu_read_unlock();
2805 return ret;
2806 }
2807
2808 /*
2809 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2810 * snapshot output is *not* set with a remote destination.
2811 *
2812 * Return 0 on success or a LTTNG_ERR code.
2813 */
2814 static int set_relayd_for_snapshot(struct consumer_output *consumer,
2815 struct snapshot_output *snap_output, struct ltt_session *session)
2816 {
2817 int ret = LTTNG_OK;
2818 struct lttng_ht_iter iter;
2819 struct consumer_socket *socket;
2820
2821 assert(consumer);
2822 assert(snap_output);
2823 assert(session);
2824
2825 DBG2("Set relayd object from snapshot output");
2826
2827 /* Ignore if snapshot consumer output is not network. */
2828 if (snap_output->consumer->type != CONSUMER_DST_NET) {
2829 goto error;
2830 }
2831
2832 /*
2833 * For each consumer socket, create and send the relayd object of the
2834 * snapshot output.
2835 */
2836 rcu_read_lock();
2837 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
2838 socket, node.node) {
2839 ret = send_consumer_relayd_sockets(0, session->id,
2840 snap_output->consumer, socket,
2841 session->name, session->hostname,
2842 session->live_timer);
2843 if (ret != LTTNG_OK) {
2844 rcu_read_unlock();
2845 goto error;
2846 }
2847 }
2848 rcu_read_unlock();
2849
2850 error:
2851 return ret;
2852 }
2853
2854 /*
2855 * Record a kernel snapshot.
2856 *
2857 * Return LTTNG_OK on success or a LTTNG_ERR code.
2858 */
2859 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
2860 struct snapshot_output *output, struct ltt_session *session,
2861 int wait, uint64_t nb_packets_per_stream)
2862 {
2863 int ret;
2864
2865 assert(ksess);
2866 assert(output);
2867 assert(session);
2868
2869 /* Get the datetime for the snapshot output directory. */
2870 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2871 sizeof(output->datetime));
2872 if (!ret) {
2873 ret = LTTNG_ERR_INVALID;
2874 goto error;
2875 }
2876
2877 /*
2878 * Copy kernel session sockets so we can communicate with the right
2879 * consumer for the snapshot record command.
2880 */
2881 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
2882 if (ret < 0) {
2883 ret = LTTNG_ERR_NOMEM;
2884 goto error;
2885 }
2886
2887 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
2888 if (ret != LTTNG_OK) {
2889 goto error_snapshot;
2890 }
2891
2892 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
2893 if (ret != LTTNG_OK) {
2894 goto error_snapshot;
2895 }
2896
2897 ret = LTTNG_OK;
2898 goto end;
2899
2900 error_snapshot:
2901 /* Clean up copied sockets so this output can use some other later on. */
2902 consumer_destroy_output_sockets(output->consumer);
2903 error:
2904 end:
2905 return ret;
2906 }
2907
2908 /*
2909 * Record a UST snapshot.
2910 *
2911 * Return 0 on success or a LTTNG_ERR error code.
2912 */
2913 static int record_ust_snapshot(struct ltt_ust_session *usess,
2914 struct snapshot_output *output, struct ltt_session *session,
2915 int wait, uint64_t nb_packets_per_stream)
2916 {
2917 int ret;
2918
2919 assert(usess);
2920 assert(output);
2921 assert(session);
2922
2923 /* Get the datetime for the snapshot output directory. */
2924 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2925 sizeof(output->datetime));
2926 if (!ret) {
2927 ret = LTTNG_ERR_INVALID;
2928 goto error;
2929 }
2930
2931 /*
2932 * Copy UST session sockets so we can communicate with the right
2933 * consumer for the snapshot record command.
2934 */
2935 ret = consumer_copy_sockets(output->consumer, usess->consumer);
2936 if (ret < 0) {
2937 ret = LTTNG_ERR_NOMEM;
2938 goto error;
2939 }
2940
2941 ret = set_relayd_for_snapshot(usess->consumer, output, session);
2942 if (ret != LTTNG_OK) {
2943 goto error_snapshot;
2944 }
2945
2946 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
2947 if (ret < 0) {
2948 switch (-ret) {
2949 case EINVAL:
2950 ret = LTTNG_ERR_INVALID;
2951 break;
2952 case ENODATA:
2953 ret = LTTNG_ERR_SNAPSHOT_NODATA;
2954 break;
2955 default:
2956 ret = LTTNG_ERR_SNAPSHOT_FAIL;
2957 break;
2958 }
2959 goto error_snapshot;
2960 }
2961
2962 ret = LTTNG_OK;
2963
2964 error_snapshot:
2965 /* Clean up copied sockets so this output can use some other later on. */
2966 consumer_destroy_output_sockets(output->consumer);
2967 error:
2968 return ret;
2969 }
2970
2971 static
2972 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
2973 uint64_t cur_nr_packets)
2974 {
2975 uint64_t tot_size = 0;
2976
2977 if (session->kernel_session) {
2978 struct ltt_kernel_channel *chan;
2979 struct ltt_kernel_session *ksess = session->kernel_session;
2980
2981 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
2982 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
2983 /*
2984 * Don't take channel into account if we
2985 * already grab all its packets.
2986 */
2987 continue;
2988 }
2989 tot_size += chan->channel->attr.subbuf_size
2990 * chan->stream_count;
2991 }
2992 }
2993
2994 if (session->ust_session) {
2995 struct ltt_ust_session *usess = session->ust_session;
2996
2997 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
2998 cur_nr_packets);
2999 }
3000
3001 return tot_size;
3002 }
3003
3004 /*
3005 * Calculate the number of packets we can grab from each stream that
3006 * fits within the overall snapshot max size.
3007 *
3008 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3009 * the number of packets per stream.
3010 *
3011 * TODO: this approach is not perfect: we consider the worse case
3012 * (packet filling the sub-buffers) as an upper bound, but we could do
3013 * better if we do this calculation while we actually grab the packet
3014 * content: we would know how much padding we don't actually store into
3015 * the file.
3016 *
3017 * This algorithm is currently bounded by the number of packets per
3018 * stream.
3019 *
3020 * Since we call this algorithm before actually grabbing the data, it's
3021 * an approximation: for instance, applications could appear/disappear
3022 * in between this call and actually grabbing data.
3023 */
3024 static
3025 int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
3026 {
3027 int64_t size_left;
3028 uint64_t cur_nb_packets = 0;
3029
3030 if (!max_size) {
3031 return 0; /* Infinite */
3032 }
3033
3034 size_left = max_size;
3035 for (;;) {
3036 uint64_t one_more_packet_tot_size;
3037
3038 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3039 cur_nb_packets);
3040 if (!one_more_packet_tot_size) {
3041 /* We are already grabbing all packets. */
3042 break;
3043 }
3044 size_left -= one_more_packet_tot_size;
3045 if (size_left < 0) {
3046 break;
3047 }
3048 cur_nb_packets++;
3049 }
3050 if (!cur_nb_packets) {
3051 /* Not enough room to grab one packet of each stream, error. */
3052 return -1;
3053 }
3054 return cur_nb_packets;
3055 }
3056
3057 /*
3058 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3059 *
3060 * The wait parameter is ignored so this call always wait for the snapshot to
3061 * complete before returning.
3062 *
3063 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3064 */
3065 int cmd_snapshot_record(struct ltt_session *session,
3066 struct lttng_snapshot_output *output, int wait)
3067 {
3068 int ret = LTTNG_OK;
3069 unsigned int use_tmp_output = 0;
3070 struct snapshot_output tmp_output;
3071 unsigned int snapshot_success = 0;
3072
3073 assert(session);
3074
3075 DBG("Cmd snapshot record for session %s", session->name);
3076
3077 /*
3078 * Permission denied to create an output if the session is not
3079 * set in no output mode.
3080 */
3081 if (session->output_traces) {
3082 ret = LTTNG_ERR_EPERM;
3083 goto error;
3084 }
3085
3086 /* The session needs to be started at least once. */
3087 if (!session->has_been_started) {
3088 ret = LTTNG_ERR_START_SESSION_ONCE;
3089 goto error;
3090 }
3091
3092 /* Use temporary output for the session. */
3093 if (output && *output->ctrl_url != '\0') {
3094 ret = snapshot_output_init(output->max_size, output->name,
3095 output->ctrl_url, output->data_url, session->consumer,
3096 &tmp_output, NULL);
3097 if (ret < 0) {
3098 if (ret == -ENOMEM) {
3099 ret = LTTNG_ERR_NOMEM;
3100 } else {
3101 ret = LTTNG_ERR_INVALID;
3102 }
3103 goto error;
3104 }
3105 /* Use the global session count for the temporary snapshot. */
3106 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3107 use_tmp_output = 1;
3108 }
3109
3110 if (session->kernel_session) {
3111 struct ltt_kernel_session *ksess = session->kernel_session;
3112
3113 if (use_tmp_output) {
3114 int64_t nb_packets_per_stream;
3115
3116 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3117 tmp_output.max_size);
3118 if (nb_packets_per_stream < 0) {
3119 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3120 goto error;
3121 }
3122 ret = record_kernel_snapshot(ksess, &tmp_output, session,
3123 wait, nb_packets_per_stream);
3124 if (ret != LTTNG_OK) {
3125 goto error;
3126 }
3127 snapshot_success = 1;
3128 } else {
3129 struct snapshot_output *sout;
3130 struct lttng_ht_iter iter;
3131
3132 rcu_read_lock();
3133 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3134 &iter.iter, sout, node.node) {
3135 int64_t nb_packets_per_stream;
3136
3137 /*
3138 * Make a local copy of the output and assign the possible
3139 * temporary value given by the caller.
3140 */
3141 memset(&tmp_output, 0, sizeof(tmp_output));
3142 memcpy(&tmp_output, sout, sizeof(tmp_output));
3143
3144 if (output->max_size != (uint64_t) -1ULL) {
3145 tmp_output.max_size = output->max_size;
3146 }
3147
3148 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3149 tmp_output.max_size);
3150 if (nb_packets_per_stream < 0) {
3151 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3152 goto error;
3153 }
3154
3155 /* Use temporary name. */
3156 if (*output->name != '\0') {
3157 strncpy(tmp_output.name, output->name,
3158 sizeof(tmp_output.name));
3159 }
3160
3161 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3162
3163 ret = record_kernel_snapshot(ksess, &tmp_output,
3164 session, wait, nb_packets_per_stream);
3165 if (ret != LTTNG_OK) {
3166 rcu_read_unlock();
3167 goto error;
3168 }
3169 snapshot_success = 1;
3170 }
3171 rcu_read_unlock();
3172 }
3173 }
3174
3175 if (session->ust_session) {
3176 struct ltt_ust_session *usess = session->ust_session;
3177
3178 if (use_tmp_output) {
3179 int64_t nb_packets_per_stream;
3180
3181 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3182 tmp_output.max_size);
3183 if (nb_packets_per_stream < 0) {
3184 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3185 goto error;
3186 }
3187 ret = record_ust_snapshot(usess, &tmp_output, session,
3188 wait, nb_packets_per_stream);
3189 if (ret != LTTNG_OK) {
3190 goto error;
3191 }
3192 snapshot_success = 1;
3193 } else {
3194 struct snapshot_output *sout;
3195 struct lttng_ht_iter iter;
3196
3197 rcu_read_lock();
3198 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3199 &iter.iter, sout, node.node) {
3200 int64_t nb_packets_per_stream;
3201
3202 /*
3203 * Make a local copy of the output and assign the possible
3204 * temporary value given by the caller.
3205 */
3206 memset(&tmp_output, 0, sizeof(tmp_output));
3207 memcpy(&tmp_output, sout, sizeof(tmp_output));
3208
3209 if (output->max_size != (uint64_t) -1ULL) {
3210 tmp_output.max_size = output->max_size;
3211 }
3212
3213 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3214 tmp_output.max_size);
3215 if (nb_packets_per_stream < 0) {
3216 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3217 rcu_read_unlock();
3218 goto error;
3219 }
3220
3221 /* Use temporary name. */
3222 if (*output->name != '\0') {
3223 strncpy(tmp_output.name, output->name,
3224 sizeof(tmp_output.name));
3225 }
3226
3227 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3228
3229 ret = record_ust_snapshot(usess, &tmp_output, session,
3230 wait, nb_packets_per_stream);
3231 if (ret != LTTNG_OK) {
3232 rcu_read_unlock();
3233 goto error;
3234 }
3235 snapshot_success = 1;
3236 }
3237 rcu_read_unlock();
3238 }
3239 }
3240
3241 if (snapshot_success) {
3242 session->snapshot.nb_snapshot++;
3243 } else {
3244 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3245 }
3246
3247 error:
3248 return ret;
3249 }
3250
3251 /*
3252 * Init command subsystem.
3253 */
3254 void cmd_init(void)
3255 {
3256 /*
3257 * Set network sequence index to 1 for streams to match a relayd
3258 * socket on the consumer side.
3259 */
3260 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3261 relayd_net_seq_idx = 1;
3262 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
3263
3264 DBG("Command subsystem initialized");
3265 }
This page took 0.096735 seconds and 4 git commands to generate.