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