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