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