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