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