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