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 {
1142 struct agent *agt;
1143 struct ltt_ust_session *usess = session->ust_session;
1144
1145 assert(usess);
1146
1147 switch (event->type) {
1148 case LTTNG_EVENT_ALL:
1149 break;
1150 default:
1151 ret = LTTNG_ERR_UNK;
1152 goto error;
1153 }
1154
1155 agt = trace_ust_find_agent(usess, domain);
1156 if (!agt) {
1157 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1158 goto error;
1159 }
1160 /* The wild card * means that everything should be disabled. */
1161 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
1162 ret = event_agent_disable_all(usess, agt);
1163 } else {
1164 ret = event_agent_disable(usess, agt, event_name);
1165 }
1166 if (ret != LTTNG_OK) {
1167 goto error;
1168 }
1169
1170 break;
1171 }
1172 #if 0
1173 case LTTNG_DOMAIN_UST_EXEC_NAME:
1174 case LTTNG_DOMAIN_UST_PID:
1175 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1176 #endif
1177 default:
1178 ret = LTTNG_ERR_UND;
1179 goto error;
1180 }
1181
1182 ret = LTTNG_OK;
1183
1184 error:
1185 rcu_read_unlock();
1186 return ret;
1187 }
1188
1189 /*
1190 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1191 */
1192 static
1193 int disable_kevent_all(struct ltt_session *session, int domain,
1194 char *channel_name,
1195 struct lttng_event *event)
1196 {
1197 int ret;
1198
1199 rcu_read_lock();
1200
1201 switch (domain) {
1202 case LTTNG_DOMAIN_KERNEL:
1203 {
1204 struct ltt_kernel_session *ksess;
1205 struct ltt_kernel_channel *kchan;
1206
1207 ksess = session->kernel_session;
1208
1209 /*
1210 * If a non-default channel has been created in the
1211 * session, explicitely require that -c chan_name needs
1212 * to be provided.
1213 */
1214 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1215 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1216 goto error;
1217 }
1218
1219 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1220 if (kchan == NULL) {
1221 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1222 goto error;
1223 }
1224
1225 switch (event->type) {
1226 case LTTNG_EVENT_ALL:
1227 ret = event_kernel_disable_all(kchan);
1228 if (ret != LTTNG_OK) {
1229 goto error;
1230 }
1231 break;
1232 case LTTNG_EVENT_SYSCALL:
1233 ret = event_kernel_disable_syscall(kchan, "");
1234 if (ret != LTTNG_OK) {
1235 goto error;
1236 }
1237 break;
1238 default:
1239 ret = LTTNG_ERR_UNK;
1240 goto error;
1241 }
1242
1243 kernel_wait_quiescent(kernel_tracer_fd);
1244 break;
1245 }
1246 default:
1247 ret = LTTNG_ERR_UND;
1248 goto error;
1249 }
1250
1251 ret = LTTNG_OK;
1252
1253 error:
1254 rcu_read_unlock();
1255 return ret;
1256 }
1257
1258 /*
1259 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1260 */
1261 int cmd_add_context(struct ltt_session *session, int domain,
1262 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
1263 {
1264 int ret, chan_kern_created = 0, chan_ust_created = 0;
1265
1266 switch (domain) {
1267 case LTTNG_DOMAIN_KERNEL:
1268 assert(session->kernel_session);
1269
1270 if (session->kernel_session->channel_count == 0) {
1271 /* Create default channel */
1272 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1273 if (ret != LTTNG_OK) {
1274 goto error;
1275 }
1276 chan_kern_created = 1;
1277 }
1278 /* Add kernel context to kernel tracer */
1279 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1280 if (ret != LTTNG_OK) {
1281 goto error;
1282 }
1283 break;
1284 case LTTNG_DOMAIN_UST:
1285 {
1286 struct ltt_ust_session *usess = session->ust_session;
1287 unsigned int chan_count;
1288
1289 assert(usess);
1290
1291 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1292 if (chan_count == 0) {
1293 struct lttng_channel *attr;
1294 /* Create default channel */
1295 attr = channel_new_default_attr(domain, usess->buffer_type);
1296 if (attr == NULL) {
1297 ret = LTTNG_ERR_FATAL;
1298 goto error;
1299 }
1300
1301 ret = channel_ust_create(usess, attr, usess->buffer_type);
1302 if (ret != LTTNG_OK) {
1303 free(attr);
1304 goto error;
1305 }
1306 free(attr);
1307 chan_ust_created = 1;
1308 }
1309
1310 ret = context_ust_add(usess, domain, ctx, channel_name);
1311 if (ret != LTTNG_OK) {
1312 goto error;
1313 }
1314 break;
1315 }
1316 #if 0
1317 case LTTNG_DOMAIN_UST_EXEC_NAME:
1318 case LTTNG_DOMAIN_UST_PID:
1319 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1320 #endif
1321 default:
1322 ret = LTTNG_ERR_UND;
1323 goto error;
1324 }
1325
1326 return LTTNG_OK;
1327
1328 error:
1329 if (chan_kern_created) {
1330 struct ltt_kernel_channel *kchan =
1331 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1332 session->kernel_session);
1333 /* Created previously, this should NOT fail. */
1334 assert(kchan);
1335 kernel_destroy_channel(kchan);
1336 }
1337
1338 if (chan_ust_created) {
1339 struct ltt_ust_channel *uchan =
1340 trace_ust_find_channel_by_name(
1341 session->ust_session->domain_global.channels,
1342 DEFAULT_CHANNEL_NAME);
1343 /* Created previously, this should NOT fail. */
1344 assert(uchan);
1345 /* Remove from the channel list of the session. */
1346 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1347 uchan);
1348 trace_ust_destroy_channel(uchan);
1349 }
1350 return ret;
1351 }
1352
1353 static int validate_event_name(const char *name)
1354 {
1355 int ret = 0;
1356 const char *c = name;
1357 const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN;
1358
1359 /*
1360 * Make sure that unescaped wildcards are only used as the last
1361 * character of the event name.
1362 */
1363 while (c < event_name_end) {
1364 switch (*c) {
1365 case '\0':
1366 goto end;
1367 case '\\':
1368 c++;
1369 break;
1370 case '*':
1371 if ((c + 1) < event_name_end && *(c + 1)) {
1372 /* Wildcard is not the last character */
1373 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1374 goto end;
1375 }
1376 default:
1377 break;
1378 }
1379 c++;
1380 }
1381 end:
1382 return ret;
1383 }
1384
1385 /*
1386 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1387 */
1388 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
1389 char *channel_name, struct lttng_event *event,
1390 char *filter_expression,
1391 struct lttng_filter_bytecode *filter,
1392 struct lttng_event_exclusion *exclusion,
1393 int wpipe)
1394 {
1395 int ret, channel_created = 0;
1396 struct lttng_channel *attr;
1397
1398 assert(session);
1399 assert(event);
1400 assert(channel_name);
1401
1402 DBG("Enable event command for event \'%s\'", event->name);
1403
1404 /* Special handling for kernel domain all events. */
1405 if (domain->type == LTTNG_DOMAIN_KERNEL && !strcmp(event->name, "*")) {
1406 return enable_kevent_all(session, domain, channel_name, event,
1407 filter_expression, filter, wpipe);
1408 }
1409
1410 ret = validate_event_name(event->name);
1411 if (ret) {
1412 goto error;
1413 }
1414
1415 rcu_read_lock();
1416
1417 switch (domain->type) {
1418 case LTTNG_DOMAIN_KERNEL:
1419 {
1420 struct ltt_kernel_channel *kchan;
1421
1422 /*
1423 * If a non-default channel has been created in the
1424 * session, explicitely require that -c chan_name needs
1425 * to be provided.
1426 */
1427 if (session->kernel_session->has_non_default_channel
1428 && channel_name[0] == '\0') {
1429 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1430 goto error;
1431 }
1432
1433 kchan = trace_kernel_get_channel_by_name(channel_name,
1434 session->kernel_session);
1435 if (kchan == NULL) {
1436 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1437 LTTNG_BUFFER_GLOBAL);
1438 if (attr == NULL) {
1439 ret = LTTNG_ERR_FATAL;
1440 goto error;
1441 }
1442 strncpy(attr->name, channel_name, sizeof(attr->name));
1443
1444 ret = cmd_enable_channel(session, domain, attr, wpipe);
1445 if (ret != LTTNG_OK) {
1446 free(attr);
1447 goto error;
1448 }
1449 free(attr);
1450
1451 channel_created = 1;
1452 }
1453
1454 /* Get the newly created kernel channel pointer */
1455 kchan = trace_kernel_get_channel_by_name(channel_name,
1456 session->kernel_session);
1457 if (kchan == NULL) {
1458 /* This sould not happen... */
1459 ret = LTTNG_ERR_FATAL;
1460 goto error;
1461 }
1462
1463 switch (event->type) {
1464 case LTTNG_EVENT_ALL:
1465 case LTTNG_EVENT_PROBE:
1466 case LTTNG_EVENT_FUNCTION:
1467 case LTTNG_EVENT_FUNCTION_ENTRY:
1468 case LTTNG_EVENT_TRACEPOINT:
1469 ret = event_kernel_enable_tracepoint(kchan, event);
1470 if (ret != LTTNG_OK) {
1471 if (channel_created) {
1472 /* Let's not leak a useless channel. */
1473 kernel_destroy_channel(kchan);
1474 }
1475 goto error;
1476 }
1477 break;
1478 case LTTNG_EVENT_SYSCALL:
1479 ret = event_kernel_enable_syscall(kchan, event->name);
1480 if (ret != LTTNG_OK) {
1481 goto error;
1482 }
1483 break;
1484 default:
1485 ret = LTTNG_ERR_UNK;
1486 goto error;
1487 }
1488
1489 kernel_wait_quiescent(kernel_tracer_fd);
1490 break;
1491 }
1492 case LTTNG_DOMAIN_UST:
1493 {
1494 struct ltt_ust_channel *uchan;
1495 struct ltt_ust_session *usess = session->ust_session;
1496
1497 assert(usess);
1498
1499 /*
1500 * If a non-default channel has been created in the
1501 * session, explicitely require that -c chan_name needs
1502 * to be provided.
1503 */
1504 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1505 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1506 goto error;
1507 }
1508
1509 /* Get channel from global UST domain */
1510 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1511 channel_name);
1512 if (uchan == NULL) {
1513 /* Create default channel */
1514 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1515 usess->buffer_type);
1516 if (attr == NULL) {
1517 ret = LTTNG_ERR_FATAL;
1518 goto error;
1519 }
1520 strncpy(attr->name, channel_name, sizeof(attr->name));
1521
1522 ret = cmd_enable_channel(session, domain, attr, wpipe);
1523 if (ret != LTTNG_OK) {
1524 free(attr);
1525 goto error;
1526 }
1527 free(attr);
1528
1529 /* Get the newly created channel reference back */
1530 uchan = trace_ust_find_channel_by_name(
1531 usess->domain_global.channels, channel_name);
1532 assert(uchan);
1533 }
1534
1535 /* At this point, the session and channel exist on the tracer */
1536 ret = event_ust_enable_tracepoint(usess, uchan, event,
1537 filter_expression, filter, exclusion);
1538 if (ret != LTTNG_OK) {
1539 goto error;
1540 }
1541 break;
1542 }
1543 case LTTNG_DOMAIN_LOG4J:
1544 case LTTNG_DOMAIN_JUL:
1545 {
1546 const char *default_event_name, *default_chan_name;
1547 struct agent *agt;
1548 struct lttng_event uevent;
1549 struct lttng_domain tmp_dom;
1550 struct ltt_ust_session *usess = session->ust_session;
1551
1552 assert(usess);
1553
1554 agt = trace_ust_find_agent(usess, domain->type);
1555 if (!agt) {
1556 agt = agent_create(domain->type);
1557 if (!agt) {
1558 ret = -LTTNG_ERR_NOMEM;
1559 goto error;
1560 }
1561 agent_add(agt, usess->agents);
1562 }
1563
1564 /* Create the default tracepoint. */
1565 memset(&uevent, 0, sizeof(uevent));
1566 uevent.type = LTTNG_EVENT_TRACEPOINT;
1567 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1568 default_event_name = event_get_default_agent_ust_name(domain->type);
1569 if (!default_event_name) {
1570 ret = -LTTNG_ERR_FATAL;
1571 goto error;
1572 }
1573 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
1574 uevent.name[sizeof(uevent.name) - 1] = '\0';
1575
1576 /*
1577 * The domain type is changed because we are about to enable the
1578 * default channel and event for the JUL domain that are hardcoded.
1579 * This happens in the UST domain.
1580 */
1581 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1582 tmp_dom.type = LTTNG_DOMAIN_UST;
1583
1584 if (domain->type == LTTNG_DOMAIN_LOG4J) {
1585 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
1586 } else {
1587 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
1588 }
1589
1590 ret = cmd_enable_event(session, &tmp_dom, (char *) default_chan_name,
1591 &uevent, filter_expression, filter, NULL, wpipe);
1592 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1593 goto error;
1594 }
1595
1596 /* The wild card * means that everything should be enabled. */
1597 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
1598 ret = event_agent_enable_all(usess, agt, event, filter);
1599 } else {
1600 ret = event_agent_enable(usess, agt, event, filter);
1601 }
1602 if (ret != LTTNG_OK) {
1603 goto error;
1604 }
1605
1606 break;
1607 }
1608 #if 0
1609 case LTTNG_DOMAIN_UST_EXEC_NAME:
1610 case LTTNG_DOMAIN_UST_PID:
1611 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1612 #endif
1613 default:
1614 ret = LTTNG_ERR_UND;
1615 goto error;
1616 }
1617
1618 ret = LTTNG_OK;
1619
1620 error:
1621 rcu_read_unlock();
1622 return ret;
1623 }
1624
1625 /*
1626 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1627 */
1628 static
1629 int enable_kevent_all(struct ltt_session *session,
1630 struct lttng_domain *domain, char *channel_name,
1631 struct lttng_event *event,
1632 char *filter_expression,
1633 struct lttng_filter_bytecode *filter, int wpipe)
1634 {
1635 int ret;
1636 struct lttng_channel *attr;
1637
1638 assert(session);
1639 assert(channel_name);
1640
1641 rcu_read_lock();
1642
1643 switch (domain->type) {
1644 case LTTNG_DOMAIN_KERNEL:
1645 {
1646 struct ltt_kernel_channel *kchan;
1647
1648 assert(session->kernel_session);
1649
1650 /*
1651 * If a non-default channel has been created in the
1652 * session, explicitely require that -c chan_name needs
1653 * to be provided.
1654 */
1655 if (session->kernel_session->has_non_default_channel
1656 && channel_name[0] == '\0') {
1657 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1658 goto error;
1659 }
1660
1661 kchan = trace_kernel_get_channel_by_name(channel_name,
1662 session->kernel_session);
1663 if (kchan == NULL) {
1664 /* Create default channel */
1665 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1666 LTTNG_BUFFER_GLOBAL);
1667 if (attr == NULL) {
1668 ret = LTTNG_ERR_FATAL;
1669 goto error;
1670 }
1671 strncpy(attr->name, channel_name, sizeof(attr->name));
1672
1673 ret = cmd_enable_channel(session, domain, attr, wpipe);
1674 if (ret != LTTNG_OK) {
1675 free(attr);
1676 goto error;
1677 }
1678 free(attr);
1679
1680 /* Get the newly created kernel channel pointer */
1681 kchan = trace_kernel_get_channel_by_name(channel_name,
1682 session->kernel_session);
1683 assert(kchan);
1684 }
1685
1686 switch (event->type) {
1687 case LTTNG_EVENT_SYSCALL:
1688 ret = event_kernel_enable_syscall(kchan, "");
1689 if (ret != LTTNG_OK) {
1690 goto error;
1691 }
1692 break;
1693 case LTTNG_EVENT_TRACEPOINT:
1694 /*
1695 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1696 * events already registered to the channel.
1697 */
1698 ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
1699 break;
1700 case LTTNG_EVENT_ALL:
1701 /* Enable syscalls and tracepoints */
1702 ret = event_kernel_enable_all(kchan, kernel_tracer_fd);
1703 break;
1704 default:
1705 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
1706 goto error;
1707 }
1708
1709 /* Manage return value */
1710 if (ret != LTTNG_OK) {
1711 /*
1712 * On error, cmd_enable_channel call will take care of destroying
1713 * the created channel if it was needed.
1714 */
1715 goto error;
1716 }
1717
1718 kernel_wait_quiescent(kernel_tracer_fd);
1719 break;
1720 }
1721 default:
1722 ret = LTTNG_ERR_UND;
1723 goto error;
1724 }
1725
1726 ret = LTTNG_OK;
1727
1728 error:
1729 rcu_read_unlock();
1730 return ret;
1731 }
1732
1733
1734 /*
1735 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1736 */
1737 ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1738 {
1739 int ret;
1740 ssize_t nb_events = 0;
1741
1742 switch (domain) {
1743 case LTTNG_DOMAIN_KERNEL:
1744 nb_events = kernel_list_events(kernel_tracer_fd, events);
1745 if (nb_events < 0) {
1746 ret = LTTNG_ERR_KERN_LIST_FAIL;
1747 goto error;
1748 }
1749 break;
1750 case LTTNG_DOMAIN_UST:
1751 nb_events = ust_app_list_events(events);
1752 if (nb_events < 0) {
1753 ret = LTTNG_ERR_UST_LIST_FAIL;
1754 goto error;
1755 }
1756 break;
1757 case LTTNG_DOMAIN_LOG4J:
1758 case LTTNG_DOMAIN_JUL:
1759 nb_events = agent_list_events(events, domain);
1760 if (nb_events < 0) {
1761 ret = LTTNG_ERR_UST_LIST_FAIL;
1762 goto error;
1763 }
1764 break;
1765 default:
1766 ret = LTTNG_ERR_UND;
1767 goto error;
1768 }
1769
1770 return nb_events;
1771
1772 error:
1773 /* Return negative value to differentiate return code */
1774 return -ret;
1775 }
1776
1777 /*
1778 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1779 */
1780 ssize_t cmd_list_tracepoint_fields(int domain,
1781 struct lttng_event_field **fields)
1782 {
1783 int ret;
1784 ssize_t nb_fields = 0;
1785
1786 switch (domain) {
1787 case LTTNG_DOMAIN_UST:
1788 nb_fields = ust_app_list_event_fields(fields);
1789 if (nb_fields < 0) {
1790 ret = LTTNG_ERR_UST_LIST_FAIL;
1791 goto error;
1792 }
1793 break;
1794 case LTTNG_DOMAIN_KERNEL:
1795 default: /* fall-through */
1796 ret = LTTNG_ERR_UND;
1797 goto error;
1798 }
1799
1800 return nb_fields;
1801
1802 error:
1803 /* Return negative value to differentiate return code */
1804 return -ret;
1805 }
1806
1807 ssize_t cmd_list_syscalls(struct lttng_event **events)
1808 {
1809 return syscall_table_list(events);
1810 }
1811
1812 /*
1813 * Command LTTNG_START_TRACE processed by the client thread.
1814 */
1815 int cmd_start_trace(struct ltt_session *session)
1816 {
1817 int ret;
1818 unsigned long nb_chan = 0;
1819 struct ltt_kernel_session *ksession;
1820 struct ltt_ust_session *usess;
1821
1822 assert(session);
1823
1824 /* Ease our life a bit ;) */
1825 ksession = session->kernel_session;
1826 usess = session->ust_session;
1827
1828 /* Is the session already started? */
1829 if (session->active) {
1830 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1831 goto error;
1832 }
1833
1834 /*
1835 * Starting a session without channel is useless since after that it's not
1836 * possible to enable channel thus inform the client.
1837 */
1838 if (usess && usess->domain_global.channels) {
1839 rcu_read_lock();
1840 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
1841 rcu_read_unlock();
1842 }
1843 if (ksession) {
1844 nb_chan += ksession->channel_count;
1845 }
1846 if (!nb_chan) {
1847 ret = LTTNG_ERR_NO_CHANNEL;
1848 goto error;
1849 }
1850
1851 /* Kernel tracing */
1852 if (ksession != NULL) {
1853 ret = start_kernel_session(ksession, kernel_tracer_fd);
1854 if (ret != LTTNG_OK) {
1855 goto error;
1856 }
1857 }
1858
1859 /* Flag session that trace should start automatically */
1860 if (usess) {
1861 /*
1862 * Even though the start trace might fail, flag this session active so
1863 * other application coming in are started by default.
1864 */
1865 usess->active = 1;
1866
1867 ret = ust_app_start_trace_all(usess);
1868 if (ret < 0) {
1869 ret = LTTNG_ERR_UST_START_FAIL;
1870 goto error;
1871 }
1872 }
1873
1874 /* Flag this after a successful start. */
1875 session->has_been_started = 1;
1876 session->active = 1;
1877
1878 ret = LTTNG_OK;
1879
1880 error:
1881 return ret;
1882 }
1883
1884 /*
1885 * Command LTTNG_STOP_TRACE processed by the client thread.
1886 */
1887 int cmd_stop_trace(struct ltt_session *session)
1888 {
1889 int ret;
1890 struct ltt_kernel_channel *kchan;
1891 struct ltt_kernel_session *ksession;
1892 struct ltt_ust_session *usess;
1893
1894 assert(session);
1895
1896 /* Short cut */
1897 ksession = session->kernel_session;
1898 usess = session->ust_session;
1899
1900 /* Session is not active. Skip everythong and inform the client. */
1901 if (!session->active) {
1902 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
1903 goto error;
1904 }
1905
1906 /* Kernel tracer */
1907 if (ksession && ksession->active) {
1908 DBG("Stop kernel tracing");
1909
1910 /* Flush metadata if exist */
1911 if (ksession->metadata_stream_fd >= 0) {
1912 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1913 if (ret < 0) {
1914 ERR("Kernel metadata flush failed");
1915 }
1916 }
1917
1918 /* Flush all buffers before stopping */
1919 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1920 ret = kernel_flush_buffer(kchan);
1921 if (ret < 0) {
1922 ERR("Kernel flush buffer error");
1923 }
1924 }
1925
1926 ret = kernel_stop_session(ksession);
1927 if (ret < 0) {
1928 ret = LTTNG_ERR_KERN_STOP_FAIL;
1929 goto error;
1930 }
1931
1932 kernel_wait_quiescent(kernel_tracer_fd);
1933
1934 ksession->active = 0;
1935 }
1936
1937 if (usess && usess->active) {
1938 /*
1939 * Even though the stop trace might fail, flag this session inactive so
1940 * other application coming in are not started by default.
1941 */
1942 usess->active = 0;
1943
1944 ret = ust_app_stop_trace_all(usess);
1945 if (ret < 0) {
1946 ret = LTTNG_ERR_UST_STOP_FAIL;
1947 goto error;
1948 }
1949 }
1950
1951 /* Flag inactive after a successful stop. */
1952 session->active = 0;
1953 ret = LTTNG_OK;
1954
1955 error:
1956 return ret;
1957 }
1958
1959 /*
1960 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1961 */
1962 int cmd_set_consumer_uri(int domain, struct ltt_session *session,
1963 size_t nb_uri, struct lttng_uri *uris)
1964 {
1965 int ret, i;
1966 struct ltt_kernel_session *ksess = session->kernel_session;
1967 struct ltt_ust_session *usess = session->ust_session;
1968 struct consumer_output *consumer = NULL;
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 /*
1981 * This case switch makes sure the domain session has a temporary consumer
1982 * so the URL can be set.
1983 */
1984 switch (domain) {
1985 case 0:
1986 /* Code flow error. A session MUST always have a consumer object */
1987 assert(session->consumer);
1988 /*
1989 * The URL will be added to the tracing session consumer instead of a
1990 * specific domain consumer.
1991 */
1992 consumer = session->consumer;
1993 break;
1994 case LTTNG_DOMAIN_KERNEL:
1995 /* Code flow error if we don't have a kernel session here. */
1996 assert(ksess);
1997 assert(ksess->consumer);
1998 consumer = ksess->consumer;
1999 break;
2000 case LTTNG_DOMAIN_UST:
2001 /* Code flow error if we don't have a kernel session here. */
2002 assert(usess);
2003 assert(usess->consumer);
2004 consumer = usess->consumer;
2005 break;
2006 }
2007
2008 for (i = 0; i < nb_uri; i++) {
2009 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
2010 if (ret != LTTNG_OK) {
2011 goto error;
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 } else if (usess) {
2023 usess->output_traces = 1;
2024 }
2025
2026 /* All good! */
2027 ret = LTTNG_OK;
2028
2029 error:
2030 return ret;
2031 }
2032
2033 /*
2034 * Command LTTNG_CREATE_SESSION processed by the client thread.
2035 */
2036 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
2037 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2038 {
2039 int ret;
2040 struct ltt_session *session;
2041
2042 assert(name);
2043 assert(creds);
2044
2045 /*
2046 * Verify if the session already exist
2047 *
2048 * XXX: There is no need for the session lock list here since the caller
2049 * (process_client_msg) is holding it. We might want to change that so a
2050 * single command does not lock the entire session list.
2051 */
2052 session = session_find_by_name(name);
2053 if (session != NULL) {
2054 ret = LTTNG_ERR_EXIST_SESS;
2055 goto find_error;
2056 }
2057
2058 /* Create tracing session in the registry */
2059 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2060 LTTNG_SOCK_GET_GID_CRED(creds));
2061 if (ret != LTTNG_OK) {
2062 goto session_error;
2063 }
2064
2065 /*
2066 * Get the newly created session pointer back
2067 *
2068 * XXX: There is no need for the session lock list here since the caller
2069 * (process_client_msg) is holding it. We might want to change that so a
2070 * single command does not lock the entire session list.
2071 */
2072 session = session_find_by_name(name);
2073 assert(session);
2074
2075 session->live_timer = live_timer;
2076 /* Create default consumer output for the session not yet created. */
2077 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2078 if (session->consumer == NULL) {
2079 ret = LTTNG_ERR_FATAL;
2080 goto consumer_error;
2081 }
2082
2083 if (uris) {
2084 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
2085 if (ret != LTTNG_OK) {
2086 goto consumer_error;
2087 }
2088 session->output_traces = 1;
2089 } else {
2090 session->output_traces = 0;
2091 DBG2("Session %s created with no output", session->name);
2092 }
2093
2094 session->consumer->enabled = 1;
2095
2096 return LTTNG_OK;
2097
2098 consumer_error:
2099 session_destroy(session);
2100 session_error:
2101 find_error:
2102 return ret;
2103 }
2104
2105 /*
2106 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2107 */
2108 int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2109 size_t nb_uri, lttng_sock_cred *creds)
2110 {
2111 int ret;
2112 struct ltt_session *session;
2113 struct snapshot_output *new_output = NULL;
2114
2115 assert(name);
2116 assert(creds);
2117
2118 /*
2119 * Create session in no output mode with URIs set to NULL. The uris we've
2120 * received are for a default snapshot output if one.
2121 */
2122 ret = cmd_create_session_uri(name, NULL, 0, creds, -1);
2123 if (ret != LTTNG_OK) {
2124 goto error;
2125 }
2126
2127 /* Get the newly created session pointer back. This should NEVER fail. */
2128 session = session_find_by_name(name);
2129 assert(session);
2130
2131 /* Flag session for snapshot mode. */
2132 session->snapshot_mode = 1;
2133
2134 /* Skip snapshot output creation if no URI is given. */
2135 if (nb_uri == 0) {
2136 goto end;
2137 }
2138
2139 new_output = snapshot_output_alloc();
2140 if (!new_output) {
2141 ret = LTTNG_ERR_NOMEM;
2142 goto error_snapshot_alloc;
2143 }
2144
2145 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2146 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2147 if (ret < 0) {
2148 if (ret == -ENOMEM) {
2149 ret = LTTNG_ERR_NOMEM;
2150 } else {
2151 ret = LTTNG_ERR_INVALID;
2152 }
2153 goto error_snapshot;
2154 }
2155
2156 rcu_read_lock();
2157 snapshot_add_output(&session->snapshot, new_output);
2158 rcu_read_unlock();
2159
2160 end:
2161 return LTTNG_OK;
2162
2163 error_snapshot:
2164 snapshot_output_destroy(new_output);
2165 error_snapshot_alloc:
2166 session_destroy(session);
2167 error:
2168 return ret;
2169 }
2170
2171 /*
2172 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2173 */
2174 int cmd_destroy_session(struct ltt_session *session, int wpipe)
2175 {
2176 int ret;
2177 struct ltt_ust_session *usess;
2178 struct ltt_kernel_session *ksess;
2179
2180 /* Safety net */
2181 assert(session);
2182
2183 usess = session->ust_session;
2184 ksess = session->kernel_session;
2185
2186 /* Clean kernel session teardown */
2187 kernel_destroy_session(ksess);
2188
2189 /* UST session teardown */
2190 if (usess) {
2191 /* Close any relayd session */
2192 consumer_output_send_destroy_relayd(usess->consumer);
2193
2194 /* Destroy every UST application related to this session. */
2195 ret = ust_app_destroy_trace_all(usess);
2196 if (ret) {
2197 ERR("Error in ust_app_destroy_trace_all");
2198 }
2199
2200 /* Clean up the rest. */
2201 trace_ust_destroy_session(usess);
2202 }
2203
2204 /*
2205 * Must notify the kernel thread here to update it's poll set in order to
2206 * remove the channel(s)' fd just destroyed.
2207 */
2208 ret = notify_thread_pipe(wpipe);
2209 if (ret < 0) {
2210 PERROR("write kernel poll pipe");
2211 }
2212
2213 ret = session_destroy(session);
2214
2215 return ret;
2216 }
2217
2218 /*
2219 * Command LTTNG_CALIBRATE processed by the client thread.
2220 */
2221 int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
2222 {
2223 int ret;
2224
2225 switch (domain) {
2226 case LTTNG_DOMAIN_KERNEL:
2227 {
2228 struct lttng_kernel_calibrate kcalibrate;
2229
2230 switch (calibrate->type) {
2231 case LTTNG_CALIBRATE_FUNCTION:
2232 default:
2233 /* Default and only possible calibrate option. */
2234 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2235 break;
2236 }
2237
2238 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2239 if (ret < 0) {
2240 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2241 goto error;
2242 }
2243 break;
2244 }
2245 case LTTNG_DOMAIN_UST:
2246 {
2247 struct lttng_ust_calibrate ucalibrate;
2248
2249 switch (calibrate->type) {
2250 case LTTNG_CALIBRATE_FUNCTION:
2251 default:
2252 /* Default and only possible calibrate option. */
2253 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2254 break;
2255 }
2256
2257 ret = ust_app_calibrate_glb(&ucalibrate);
2258 if (ret < 0) {
2259 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2260 goto error;
2261 }
2262 break;
2263 }
2264 default:
2265 ret = LTTNG_ERR_UND;
2266 goto error;
2267 }
2268
2269 ret = LTTNG_OK;
2270
2271 error:
2272 return ret;
2273 }
2274
2275 /*
2276 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2277 */
2278 int cmd_register_consumer(struct ltt_session *session, int domain,
2279 const char *sock_path, struct consumer_data *cdata)
2280 {
2281 int ret, sock;
2282 struct consumer_socket *socket = NULL;
2283
2284 assert(session);
2285 assert(cdata);
2286 assert(sock_path);
2287
2288 switch (domain) {
2289 case LTTNG_DOMAIN_KERNEL:
2290 {
2291 struct ltt_kernel_session *ksess = session->kernel_session;
2292
2293 assert(ksess);
2294
2295 /* Can't register a consumer if there is already one */
2296 if (ksess->consumer_fds_sent != 0) {
2297 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2298 goto error;
2299 }
2300
2301 sock = lttcomm_connect_unix_sock(sock_path);
2302 if (sock < 0) {
2303 ret = LTTNG_ERR_CONNECT_FAIL;
2304 goto error;
2305 }
2306 cdata->cmd_sock = sock;
2307
2308 socket = consumer_allocate_socket(&cdata->cmd_sock);
2309 if (socket == NULL) {
2310 ret = close(sock);
2311 if (ret < 0) {
2312 PERROR("close register consumer");
2313 }
2314 cdata->cmd_sock = -1;
2315 ret = LTTNG_ERR_FATAL;
2316 goto error;
2317 }
2318
2319 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2320 if (socket->lock == NULL) {
2321 PERROR("zmalloc pthread mutex");
2322 ret = LTTNG_ERR_FATAL;
2323 goto error;
2324 }
2325 pthread_mutex_init(socket->lock, NULL);
2326 socket->registered = 1;
2327
2328 rcu_read_lock();
2329 consumer_add_socket(socket, ksess->consumer);
2330 rcu_read_unlock();
2331
2332 pthread_mutex_lock(&cdata->pid_mutex);
2333 cdata->pid = -1;
2334 pthread_mutex_unlock(&cdata->pid_mutex);
2335
2336 break;
2337 }
2338 default:
2339 /* TODO: Userspace tracing */
2340 ret = LTTNG_ERR_UND;
2341 goto error;
2342 }
2343
2344 return LTTNG_OK;
2345
2346 error:
2347 if (socket) {
2348 consumer_destroy_socket(socket);
2349 }
2350 return ret;
2351 }
2352
2353 /*
2354 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2355 */
2356 ssize_t cmd_list_domains(struct ltt_session *session,
2357 struct lttng_domain **domains)
2358 {
2359 int ret, index = 0;
2360 ssize_t nb_dom = 0;
2361 struct agent *agt;
2362 struct lttng_ht_iter iter;
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 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2374 agt, node.node) {
2375 if (agt->being_used) {
2376 nb_dom++;
2377 }
2378 }
2379 }
2380
2381 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2382 if (*domains == NULL) {
2383 ret = LTTNG_ERR_FATAL;
2384 goto error;
2385 }
2386
2387 if (session->kernel_session != NULL) {
2388 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2389 index++;
2390 }
2391
2392 if (session->ust_session != NULL) {
2393 (*domains)[index].type = LTTNG_DOMAIN_UST;
2394 (*domains)[index].buf_type = session->ust_session->buffer_type;
2395 index++;
2396
2397 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2398 agt, node.node) {
2399 if (agt->being_used) {
2400 (*domains)[index].type = agt->domain;
2401 (*domains)[index].buf_type = session->ust_session->buffer_type;
2402 index++;
2403 }
2404 }
2405 }
2406
2407 return nb_dom;
2408
2409 error:
2410 /* Return negative value to differentiate return code */
2411 return -ret;
2412 }
2413
2414
2415 /*
2416 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2417 */
2418 ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2419 struct lttng_channel **channels)
2420 {
2421 int ret;
2422 ssize_t nb_chan = 0;
2423
2424 switch (domain) {
2425 case LTTNG_DOMAIN_KERNEL:
2426 if (session->kernel_session != NULL) {
2427 nb_chan = session->kernel_session->channel_count;
2428 }
2429 DBG3("Number of kernel channels %zd", nb_chan);
2430 if (nb_chan <= 0) {
2431 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2432 }
2433 break;
2434 case LTTNG_DOMAIN_UST:
2435 if (session->ust_session != NULL) {
2436 nb_chan = lttng_ht_get_count(
2437 session->ust_session->domain_global.channels);
2438 }
2439 DBG3("Number of UST global channels %zd", nb_chan);
2440 if (nb_chan < 0) {
2441 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2442 goto error;
2443 }
2444 break;
2445 default:
2446 ret = LTTNG_ERR_UND;
2447 goto error;
2448 }
2449
2450 if (nb_chan > 0) {
2451 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2452 if (*channels == NULL) {
2453 ret = LTTNG_ERR_FATAL;
2454 goto error;
2455 }
2456
2457 list_lttng_channels(domain, session, *channels);
2458 }
2459
2460 return nb_chan;
2461
2462 error:
2463 /* Return negative value to differentiate return code */
2464 return -ret;
2465 }
2466
2467 /*
2468 * Command LTTNG_LIST_EVENTS processed by the client thread.
2469 */
2470 ssize_t cmd_list_events(int domain, struct ltt_session *session,
2471 char *channel_name, struct lttng_event **events)
2472 {
2473 int ret = 0;
2474 ssize_t nb_event = 0;
2475
2476 switch (domain) {
2477 case LTTNG_DOMAIN_KERNEL:
2478 if (session->kernel_session != NULL) {
2479 nb_event = list_lttng_kernel_events(channel_name,
2480 session->kernel_session, events);
2481 }
2482 break;
2483 case LTTNG_DOMAIN_UST:
2484 {
2485 if (session->ust_session != NULL) {
2486 nb_event = list_lttng_ust_global_events(channel_name,
2487 &session->ust_session->domain_global, events);
2488 }
2489 break;
2490 }
2491 case LTTNG_DOMAIN_LOG4J:
2492 case LTTNG_DOMAIN_JUL:
2493 if (session->ust_session) {
2494 struct lttng_ht_iter iter;
2495 struct agent *agt;
2496
2497 cds_lfht_for_each_entry(session->ust_session->agents->ht,
2498 &iter.iter, agt, node.node) {
2499 nb_event = list_lttng_agent_events(agt, events);
2500 }
2501 }
2502 break;
2503 default:
2504 ret = LTTNG_ERR_UND;
2505 goto error;
2506 }
2507
2508 return nb_event;
2509
2510 error:
2511 /* Return negative value to differentiate return code */
2512 return -ret;
2513 }
2514
2515 /*
2516 * Using the session list, filled a lttng_session array to send back to the
2517 * client for session listing.
2518 *
2519 * The session list lock MUST be acquired before calling this function. Use
2520 * session_lock_list() and session_unlock_list().
2521 */
2522 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2523 gid_t gid)
2524 {
2525 int ret;
2526 unsigned int i = 0;
2527 struct ltt_session *session;
2528 struct ltt_session_list *list = session_get_list();
2529
2530 DBG("Getting all available session for UID %d GID %d",
2531 uid, gid);
2532 /*
2533 * Iterate over session list and append data after the control struct in
2534 * the buffer.
2535 */
2536 cds_list_for_each_entry(session, &list->head, list) {
2537 /*
2538 * Only list the sessions the user can control.
2539 */
2540 if (!session_access_ok(session, uid, gid)) {
2541 continue;
2542 }
2543
2544 struct ltt_kernel_session *ksess = session->kernel_session;
2545 struct ltt_ust_session *usess = session->ust_session;
2546
2547 if (session->consumer->type == CONSUMER_DST_NET ||
2548 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2549 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2550 ret = build_network_session_path(sessions[i].path,
2551 sizeof(sessions[i].path), session);
2552 } else {
2553 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2554 session->consumer->dst.trace_path);
2555 }
2556 if (ret < 0) {
2557 PERROR("snprintf session path");
2558 continue;
2559 }
2560
2561 strncpy(sessions[i].name, session->name, NAME_MAX);
2562 sessions[i].name[NAME_MAX - 1] = '\0';
2563 sessions[i].enabled = session->active;
2564 sessions[i].snapshot_mode = session->snapshot_mode;
2565 sessions[i].live_timer_interval = session->live_timer;
2566 i++;
2567 }
2568 }
2569
2570 /*
2571 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2572 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2573 */
2574 int cmd_data_pending(struct ltt_session *session)
2575 {
2576 int ret;
2577 struct ltt_kernel_session *ksess = session->kernel_session;
2578 struct ltt_ust_session *usess = session->ust_session;
2579
2580 assert(session);
2581
2582 /* Session MUST be stopped to ask for data availability. */
2583 if (session->active) {
2584 ret = LTTNG_ERR_SESSION_STARTED;
2585 goto error;
2586 } else {
2587 /*
2588 * If stopped, just make sure we've started before else the above call
2589 * will always send that there is data pending.
2590 *
2591 * The consumer assumes that when the data pending command is received,
2592 * the trace has been started before or else no output data is written
2593 * by the streams which is a condition for data pending. So, this is
2594 * *VERY* important that we don't ask the consumer before a start
2595 * trace.
2596 */
2597 if (!session->has_been_started) {
2598 ret = 0;
2599 goto error;
2600 }
2601 }
2602
2603 if (ksess && ksess->consumer) {
2604 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2605 if (ret == 1) {
2606 /* Data is still being extracted for the kernel. */
2607 goto error;
2608 }
2609 }
2610
2611 if (usess && usess->consumer) {
2612 ret = consumer_is_data_pending(usess->id, usess->consumer);
2613 if (ret == 1) {
2614 /* Data is still being extracted for the kernel. */
2615 goto error;
2616 }
2617 }
2618
2619 /* Data is ready to be read by a viewer */
2620 ret = 0;
2621
2622 error:
2623 return ret;
2624 }
2625
2626 /*
2627 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2628 *
2629 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2630 */
2631 int cmd_snapshot_add_output(struct ltt_session *session,
2632 struct lttng_snapshot_output *output, uint32_t *id)
2633 {
2634 int ret;
2635 struct snapshot_output *new_output;
2636
2637 assert(session);
2638 assert(output);
2639
2640 DBG("Cmd snapshot add output for session %s", session->name);
2641
2642 /*
2643 * Permission denied to create an output if the session is not
2644 * set in no output mode.
2645 */
2646 if (session->output_traces) {
2647 ret = LTTNG_ERR_EPERM;
2648 goto error;
2649 }
2650
2651 /* Only one output is allowed until we have the "tee" feature. */
2652 if (session->snapshot.nb_output == 1) {
2653 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
2654 goto error;
2655 }
2656
2657 new_output = snapshot_output_alloc();
2658 if (!new_output) {
2659 ret = LTTNG_ERR_NOMEM;
2660 goto error;
2661 }
2662
2663 ret = snapshot_output_init(output->max_size, output->name,
2664 output->ctrl_url, output->data_url, session->consumer, new_output,
2665 &session->snapshot);
2666 if (ret < 0) {
2667 if (ret == -ENOMEM) {
2668 ret = LTTNG_ERR_NOMEM;
2669 } else {
2670 ret = LTTNG_ERR_INVALID;
2671 }
2672 goto free_error;
2673 }
2674
2675 rcu_read_lock();
2676 snapshot_add_output(&session->snapshot, new_output);
2677 if (id) {
2678 *id = new_output->id;
2679 }
2680 rcu_read_unlock();
2681
2682 return LTTNG_OK;
2683
2684 free_error:
2685 snapshot_output_destroy(new_output);
2686 error:
2687 return ret;
2688 }
2689
2690 /*
2691 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2692 *
2693 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2694 */
2695 int cmd_snapshot_del_output(struct ltt_session *session,
2696 struct lttng_snapshot_output *output)
2697 {
2698 int ret;
2699 struct snapshot_output *sout = NULL;
2700
2701 assert(session);
2702 assert(output);
2703
2704 rcu_read_lock();
2705
2706 /*
2707 * Permission denied to create an output if the session is not
2708 * set in no output mode.
2709 */
2710 if (session->output_traces) {
2711 ret = LTTNG_ERR_EPERM;
2712 goto error;
2713 }
2714
2715 if (output->id) {
2716 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
2717 session->name);
2718 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
2719 } else if (*output->name != '\0') {
2720 DBG("Cmd snapshot del output name %s for session %s", output->name,
2721 session->name);
2722 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
2723 }
2724 if (!sout) {
2725 ret = LTTNG_ERR_INVALID;
2726 goto error;
2727 }
2728
2729 snapshot_delete_output(&session->snapshot, sout);
2730 snapshot_output_destroy(sout);
2731 ret = LTTNG_OK;
2732
2733 error:
2734 rcu_read_unlock();
2735 return ret;
2736 }
2737
2738 /*
2739 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2740 *
2741 * If no output is available, outputs is untouched and 0 is returned.
2742 *
2743 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2744 */
2745 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
2746 struct lttng_snapshot_output **outputs)
2747 {
2748 int ret, idx = 0;
2749 struct lttng_snapshot_output *list;
2750 struct lttng_ht_iter iter;
2751 struct snapshot_output *output;
2752
2753 assert(session);
2754 assert(outputs);
2755
2756 DBG("Cmd snapshot list outputs for session %s", session->name);
2757
2758 /*
2759 * Permission denied to create an output if the session is not
2760 * set in no output mode.
2761 */
2762 if (session->output_traces) {
2763 ret = LTTNG_ERR_EPERM;
2764 goto error;
2765 }
2766
2767 if (session->snapshot.nb_output == 0) {
2768 ret = 0;
2769 goto error;
2770 }
2771
2772 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
2773 if (!list) {
2774 ret = LTTNG_ERR_NOMEM;
2775 goto error;
2776 }
2777
2778 /* Copy list from session to the new list object. */
2779 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
2780 output, node.node) {
2781 assert(output->consumer);
2782 list[idx].id = output->id;
2783 list[idx].max_size = output->max_size;
2784 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
2785 if (output->consumer->type == CONSUMER_DST_LOCAL) {
2786 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
2787 sizeof(list[idx].ctrl_url));
2788 } else {
2789 /* Control URI. */
2790 ret = uri_to_str_url(&output->consumer->dst.net.control,
2791 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
2792 if (ret < 0) {
2793 ret = LTTNG_ERR_NOMEM;
2794 goto free_error;
2795 }
2796
2797 /* Data URI. */
2798 ret = uri_to_str_url(&output->consumer->dst.net.data,
2799 list[idx].data_url, sizeof(list[idx].data_url));
2800 if (ret < 0) {
2801 ret = LTTNG_ERR_NOMEM;
2802 goto free_error;
2803 }
2804 }
2805 idx++;
2806 }
2807
2808 *outputs = list;
2809 return session->snapshot.nb_output;
2810
2811 free_error:
2812 free(list);
2813 error:
2814 return -ret;
2815 }
2816
2817 /*
2818 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2819 * snapshot output is *not* set with a remote destination.
2820 *
2821 * Return 0 on success or a LTTNG_ERR code.
2822 */
2823 static int set_relayd_for_snapshot(struct consumer_output *consumer,
2824 struct snapshot_output *snap_output, struct ltt_session *session)
2825 {
2826 int ret = LTTNG_OK;
2827 struct lttng_ht_iter iter;
2828 struct consumer_socket *socket;
2829
2830 assert(consumer);
2831 assert(snap_output);
2832 assert(session);
2833
2834 DBG2("Set relayd object from snapshot output");
2835
2836 /* Ignore if snapshot consumer output is not network. */
2837 if (snap_output->consumer->type != CONSUMER_DST_NET) {
2838 goto error;
2839 }
2840
2841 /*
2842 * For each consumer socket, create and send the relayd object of the
2843 * snapshot output.
2844 */
2845 rcu_read_lock();
2846 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
2847 socket, node.node) {
2848 ret = send_consumer_relayd_sockets(0, session->id,
2849 snap_output->consumer, socket,
2850 session->name, session->hostname,
2851 session->live_timer);
2852 if (ret != LTTNG_OK) {
2853 rcu_read_unlock();
2854 goto error;
2855 }
2856 }
2857 rcu_read_unlock();
2858
2859 error:
2860 return ret;
2861 }
2862
2863 /*
2864 * Record a kernel snapshot.
2865 *
2866 * Return LTTNG_OK on success or a LTTNG_ERR code.
2867 */
2868 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
2869 struct snapshot_output *output, struct ltt_session *session,
2870 int wait, uint64_t max_stream_size)
2871 {
2872 int ret;
2873
2874 assert(ksess);
2875 assert(output);
2876 assert(session);
2877
2878 /* Get the datetime for the snapshot output directory. */
2879 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2880 sizeof(output->datetime));
2881 if (!ret) {
2882 ret = LTTNG_ERR_INVALID;
2883 goto error;
2884 }
2885
2886 /*
2887 * Copy kernel session sockets so we can communicate with the right
2888 * consumer for the snapshot record command.
2889 */
2890 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
2891 if (ret < 0) {
2892 ret = LTTNG_ERR_NOMEM;
2893 goto error;
2894 }
2895
2896 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
2897 if (ret != LTTNG_OK) {
2898 goto error_snapshot;
2899 }
2900
2901 ret = kernel_snapshot_record(ksess, output, wait, max_stream_size);
2902 if (ret != LTTNG_OK) {
2903 goto error_snapshot;
2904 }
2905
2906 ret = LTTNG_OK;
2907
2908 error_snapshot:
2909 /* Clean up copied sockets so this output can use some other later on. */
2910 consumer_destroy_output_sockets(output->consumer);
2911 error:
2912 return ret;
2913 }
2914
2915 /*
2916 * Record a UST snapshot.
2917 *
2918 * Return 0 on success or a LTTNG_ERR error code.
2919 */
2920 static int record_ust_snapshot(struct ltt_ust_session *usess,
2921 struct snapshot_output *output, struct ltt_session *session,
2922 int wait, uint64_t max_stream_size)
2923 {
2924 int ret;
2925
2926 assert(usess);
2927 assert(output);
2928 assert(session);
2929
2930 /* Get the datetime for the snapshot output directory. */
2931 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2932 sizeof(output->datetime));
2933 if (!ret) {
2934 ret = LTTNG_ERR_INVALID;
2935 goto error;
2936 }
2937
2938 /*
2939 * Copy UST session sockets so we can communicate with the right
2940 * consumer for the snapshot record command.
2941 */
2942 ret = consumer_copy_sockets(output->consumer, usess->consumer);
2943 if (ret < 0) {
2944 ret = LTTNG_ERR_NOMEM;
2945 goto error;
2946 }
2947
2948 ret = set_relayd_for_snapshot(usess->consumer, output, session);
2949 if (ret != LTTNG_OK) {
2950 goto error_snapshot;
2951 }
2952
2953 ret = ust_app_snapshot_record(usess, output, wait, max_stream_size);
2954 if (ret < 0) {
2955 switch (-ret) {
2956 case EINVAL:
2957 ret = LTTNG_ERR_INVALID;
2958 break;
2959 case ENODATA:
2960 ret = LTTNG_ERR_SNAPSHOT_NODATA;
2961 break;
2962 default:
2963 ret = LTTNG_ERR_SNAPSHOT_FAIL;
2964 break;
2965 }
2966 goto error_snapshot;
2967 }
2968
2969 ret = LTTNG_OK;
2970
2971 error_snapshot:
2972 /* Clean up copied sockets so this output can use some other later on. */
2973 consumer_destroy_output_sockets(output->consumer);
2974 error:
2975 return ret;
2976 }
2977
2978 /*
2979 * Return the biggest subbuffer size of all channels in the given session.
2980 */
2981 static uint64_t get_session_max_subbuf_size(struct ltt_session *session)
2982 {
2983 uint64_t max_size = 0;
2984
2985 assert(session);
2986
2987 if (session->kernel_session) {
2988 struct ltt_kernel_channel *chan;
2989 struct ltt_kernel_session *ksess = session->kernel_session;
2990
2991 /*
2992 * For each channel, add to the max size the size of each subbuffer
2993 * multiplied by their sized.
2994 */
2995 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
2996 if (chan->channel->attr.subbuf_size > max_size) {
2997 max_size = chan->channel->attr.subbuf_size;
2998 }
2999 }
3000 }
3001
3002 if (session->ust_session) {
3003 struct lttng_ht_iter iter;
3004 struct ltt_ust_channel *uchan;
3005 struct ltt_ust_session *usess = session->ust_session;
3006
3007 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
3008 uchan, node.node) {
3009 if (uchan->attr.subbuf_size > max_size) {
3010 max_size = uchan->attr.subbuf_size;
3011 }
3012 }
3013 }
3014
3015 return max_size;
3016 }
3017
3018 /*
3019 * Returns the total number of streams for a session or a negative value
3020 * on error.
3021 */
3022 static unsigned int get_session_nb_streams(struct ltt_session *session)
3023 {
3024 unsigned int total_streams = 0;
3025
3026 if (session->kernel_session) {
3027 struct ltt_kernel_session *ksess = session->kernel_session;
3028
3029 total_streams += ksess->stream_count_global;
3030 }
3031
3032 if (session->ust_session) {
3033 struct ltt_ust_session *usess = session->ust_session;
3034
3035 total_streams += ust_app_get_nb_stream(usess);
3036 }
3037
3038 return total_streams;
3039 }
3040
3041 /*
3042 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3043 *
3044 * The wait parameter is ignored so this call always wait for the snapshot to
3045 * complete before returning.
3046 *
3047 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3048 */
3049 int cmd_snapshot_record(struct ltt_session *session,
3050 struct lttng_snapshot_output *output, int wait)
3051 {
3052 int ret = LTTNG_OK;
3053 unsigned int use_tmp_output = 0;
3054 struct snapshot_output tmp_output;
3055 unsigned int nb_streams, snapshot_success = 0;
3056 uint64_t session_max_size = 0, max_stream_size = 0;
3057
3058 assert(session);
3059 assert(output);
3060
3061 DBG("Cmd snapshot record for session %s", session->name);
3062
3063 /*
3064 * Permission denied to create an output if the session is not
3065 * set in no output mode.
3066 */
3067 if (session->output_traces) {
3068 ret = LTTNG_ERR_EPERM;
3069 goto error;
3070 }
3071
3072 /* The session needs to be started at least once. */
3073 if (!session->has_been_started) {
3074 ret = LTTNG_ERR_START_SESSION_ONCE;
3075 goto error;
3076 }
3077
3078 /* Use temporary output for the session. */
3079 if (*output->ctrl_url != '\0') {
3080 ret = snapshot_output_init(output->max_size, output->name,
3081 output->ctrl_url, output->data_url, session->consumer,
3082 &tmp_output, NULL);
3083 if (ret < 0) {
3084 if (ret == -ENOMEM) {
3085 ret = LTTNG_ERR_NOMEM;
3086 } else {
3087 ret = LTTNG_ERR_INVALID;
3088 }
3089 goto error;
3090 }
3091 /* Use the global session count for the temporary snapshot. */
3092 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3093 use_tmp_output = 1;
3094 }
3095
3096 /*
3097 * Get the session maximum size for a snapshot meaning it will compute the
3098 * size of all streams from all domain.
3099 */
3100 max_stream_size = get_session_max_subbuf_size(session);
3101
3102 nb_streams = get_session_nb_streams(session);
3103 if (nb_streams) {
3104 /*
3105 * The maximum size of the snapshot is the number of streams multiplied
3106 * by the biggest subbuf size of all channels in a session which is the
3107 * maximum stream size available for each stream. The session max size
3108 * is now checked against the snapshot max size value given by the user
3109 * and if lower, an error is returned.
3110 */
3111 session_max_size = max_stream_size * nb_streams;
3112 }
3113
3114 DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64,
3115 session_max_size, max_stream_size);
3116
3117 /*
3118 * If we use a temporary output, check right away if the max size fits else
3119 * for each output the max size will be checked.
3120 */
3121 if (use_tmp_output &&
3122 (tmp_output.max_size != 0 &&
3123 tmp_output.max_size < session_max_size)) {
3124 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3125 goto error;
3126 }
3127
3128 if (session->kernel_session) {
3129 struct ltt_kernel_session *ksess = session->kernel_session;
3130
3131 if (use_tmp_output) {
3132 ret = record_kernel_snapshot(ksess, &tmp_output, session,
3133 wait, max_stream_size);
3134 if (ret != LTTNG_OK) {
3135 goto error;
3136 }
3137 snapshot_success = 1;
3138 } else {
3139 struct snapshot_output *sout;
3140 struct lttng_ht_iter iter;
3141
3142 rcu_read_lock();
3143 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3144 &iter.iter, sout, node.node) {
3145 /*
3146 * Make a local copy of the output and assign the possible
3147 * temporary value given by the caller.
3148 */
3149 memset(&tmp_output, 0, sizeof(tmp_output));
3150 memcpy(&tmp_output, sout, sizeof(tmp_output));
3151
3152 /* Use temporary max size. */
3153 if (output->max_size != (uint64_t) -1ULL) {
3154 tmp_output.max_size = output->max_size;
3155 }
3156
3157 if (tmp_output.max_size != 0 &&
3158 tmp_output.max_size < session_max_size) {
3159 rcu_read_unlock();
3160 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3161 goto error;
3162 }
3163
3164 /* Use temporary name. */
3165 if (*output->name != '\0') {
3166 strncpy(tmp_output.name, output->name,
3167 sizeof(tmp_output.name));
3168 }
3169
3170 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3171
3172 ret = record_kernel_snapshot(ksess, &tmp_output,
3173 session, wait, max_stream_size);
3174 if (ret != LTTNG_OK) {
3175 rcu_read_unlock();
3176 goto error;
3177 }
3178 snapshot_success = 1;
3179 }
3180 rcu_read_unlock();
3181 }
3182 }
3183
3184 if (session->ust_session) {
3185 struct ltt_ust_session *usess = session->ust_session;
3186
3187 if (use_tmp_output) {
3188 ret = record_ust_snapshot(usess, &tmp_output, session,
3189 wait, max_stream_size);
3190 if (ret != LTTNG_OK) {
3191 goto error;
3192 }
3193 snapshot_success = 1;
3194 } else {
3195 struct snapshot_output *sout;
3196 struct lttng_ht_iter iter;
3197
3198 rcu_read_lock();
3199 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3200 &iter.iter, sout, node.node) {
3201 /*
3202 * Make a local copy of the output and assign the possible
3203 * temporary value given by the caller.
3204 */
3205 memset(&tmp_output, 0, sizeof(tmp_output));
3206 memcpy(&tmp_output, sout, sizeof(tmp_output));
3207
3208 /* Use temporary max size. */
3209 if (output->max_size != (uint64_t) -1ULL) {
3210 tmp_output.max_size = output->max_size;
3211 }
3212
3213 if (tmp_output.max_size != 0 &&
3214 tmp_output.max_size < session_max_size) {
3215 rcu_read_unlock();
3216 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3217 goto error;
3218 }
3219
3220 /* Use temporary name. */
3221 if (*output->name != '\0') {
3222 strncpy(tmp_output.name, output->name,
3223 sizeof(tmp_output.name));
3224 }
3225
3226 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3227
3228 ret = record_ust_snapshot(usess, &tmp_output, session,
3229 wait, max_stream_size);
3230 if (ret != LTTNG_OK) {
3231 rcu_read_unlock();
3232 goto error;
3233 }
3234 snapshot_success = 1;
3235 }
3236 rcu_read_unlock();
3237 }
3238 }
3239
3240 if (snapshot_success) {
3241 session->snapshot.nb_snapshot++;
3242 } else {
3243 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3244 }
3245
3246 error:
3247 return ret;
3248 }
3249
3250 /*
3251 * Init command subsystem.
3252 */
3253 void cmd_init(void)
3254 {
3255 /*
3256 * Set network sequence index to 1 for streams to match a relayd
3257 * socket on the consumer side.
3258 */
3259 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3260 relayd_net_seq_idx = 1;
3261 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
3262
3263 DBG("Command subsystem initialized");
3264 }
This page took 0.102976 seconds and 5 git commands to generate.