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