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