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