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