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