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