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