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