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