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