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