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