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