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