Fix: ust-consumer: segfault on snapshot after regenerate metadata
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <assert.h>
12 #include <poll.h>
13 #include <pthread.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
19 #include <inttypes.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <stdint.h>
23
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/sessiond-comm/relayd.h>
29 #include <common/compat/fcntl.h>
30 #include <common/compat/endian.h>
31 #include <common/pipe.h>
32 #include <common/relayd/relayd.h>
33 #include <common/utils.h>
34 #include <common/consumer/consumer-stream.h>
35 #include <common/index/index.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/optional.h>
38 #include <common/buffer-view.h>
39 #include <common/consumer/consumer.h>
40 #include <common/consumer/metadata-bucket.h>
41
42 #include "kernel-consumer.h"
43
44 extern struct lttng_consumer_global_data consumer_data;
45 extern int consumer_poll_timeout;
46
47 /*
48 * Take a snapshot for a specific fd
49 *
50 * Returns 0 on success, < 0 on error
51 */
52 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
53 {
54 int ret = 0;
55 int infd = stream->wait_fd;
56
57 ret = kernctl_snapshot(infd);
58 /*
59 * -EAGAIN is not an error, it just means that there is no data to
60 * be read.
61 */
62 if (ret != 0 && ret != -EAGAIN) {
63 PERROR("Getting sub-buffer snapshot.");
64 }
65
66 return ret;
67 }
68
69 /*
70 * Sample consumed and produced positions for a specific fd.
71 *
72 * Returns 0 on success, < 0 on error.
73 */
74 int lttng_kconsumer_sample_snapshot_positions(
75 struct lttng_consumer_stream *stream)
76 {
77 assert(stream);
78
79 return kernctl_snapshot_sample_positions(stream->wait_fd);
80 }
81
82 /*
83 * Get the produced position
84 *
85 * Returns 0 on success, < 0 on error
86 */
87 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
88 unsigned long *pos)
89 {
90 int ret;
91 int infd = stream->wait_fd;
92
93 ret = kernctl_snapshot_get_produced(infd, pos);
94 if (ret != 0) {
95 PERROR("kernctl_snapshot_get_produced");
96 }
97
98 return ret;
99 }
100
101 /*
102 * Get the consumerd position
103 *
104 * Returns 0 on success, < 0 on error
105 */
106 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
107 unsigned long *pos)
108 {
109 int ret;
110 int infd = stream->wait_fd;
111
112 ret = kernctl_snapshot_get_consumed(infd, pos);
113 if (ret != 0) {
114 PERROR("kernctl_snapshot_get_consumed");
115 }
116
117 return ret;
118 }
119
120 static
121 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
122 const char **addr)
123 {
124 int ret;
125 unsigned long mmap_offset;
126 const char *mmap_base = stream->mmap_base;
127
128 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
129 if (ret < 0) {
130 PERROR("Failed to get mmap read offset");
131 goto error;
132 }
133
134 *addr = mmap_base + mmap_offset;
135 error:
136 return ret;
137 }
138
139 /*
140 * Take a snapshot of all the stream of a channel
141 * RCU read-side lock must be held across this function to ensure existence of
142 * channel.
143 *
144 * Returns 0 on success, < 0 on error
145 */
146 static int lttng_kconsumer_snapshot_channel(
147 struct lttng_consumer_channel *channel,
148 uint64_t key, char *path, uint64_t relayd_id,
149 uint64_t nb_packets_per_stream,
150 struct lttng_consumer_local_data *ctx)
151 {
152 int ret;
153 struct lttng_consumer_stream *stream;
154
155 DBG("Kernel consumer snapshot channel %" PRIu64, key);
156
157 /* Prevent channel modifications while we perform the snapshot.*/
158 pthread_mutex_lock(&channel->lock);
159
160 rcu_read_lock();
161
162 /* Splice is not supported yet for channel snapshot. */
163 if (channel->output != CONSUMER_CHANNEL_MMAP) {
164 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
165 channel->name);
166 ret = -1;
167 goto end;
168 }
169
170 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
171 unsigned long consumed_pos, produced_pos;
172
173 health_code_update();
174
175 /*
176 * Lock stream because we are about to change its state.
177 */
178 pthread_mutex_lock(&stream->lock);
179
180 assert(channel->trace_chunk);
181 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
182 /*
183 * Can't happen barring an internal error as the channel
184 * holds a reference to the trace chunk.
185 */
186 ERR("Failed to acquire reference to channel's trace chunk");
187 ret = -1;
188 goto end_unlock;
189 }
190 assert(!stream->trace_chunk);
191 stream->trace_chunk = channel->trace_chunk;
192
193 /*
194 * Assign the received relayd ID so we can use it for streaming. The streams
195 * are not visible to anyone so this is OK to change it.
196 */
197 stream->net_seq_idx = relayd_id;
198 channel->relayd_id = relayd_id;
199 if (relayd_id != (uint64_t) -1ULL) {
200 ret = consumer_send_relayd_stream(stream, path);
201 if (ret < 0) {
202 ERR("sending stream to relayd");
203 goto end_unlock;
204 }
205 } else {
206 ret = consumer_stream_create_output_files(stream,
207 false);
208 if (ret < 0) {
209 goto end_unlock;
210 }
211 DBG("Kernel consumer snapshot stream (%" PRIu64 ")",
212 stream->key);
213 }
214
215 ret = kernctl_buffer_flush_empty(stream->wait_fd);
216 if (ret < 0) {
217 /*
218 * Doing a buffer flush which does not take into
219 * account empty packets. This is not perfect
220 * for stream intersection, but required as a
221 * fall-back when "flush_empty" is not
222 * implemented by lttng-modules.
223 */
224 ret = kernctl_buffer_flush(stream->wait_fd);
225 if (ret < 0) {
226 ERR("Failed to flush kernel stream");
227 goto end_unlock;
228 }
229 goto end_unlock;
230 }
231
232 ret = lttng_kconsumer_take_snapshot(stream);
233 if (ret < 0) {
234 ERR("Taking kernel snapshot");
235 goto end_unlock;
236 }
237
238 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
239 if (ret < 0) {
240 ERR("Produced kernel snapshot position");
241 goto end_unlock;
242 }
243
244 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
245 if (ret < 0) {
246 ERR("Consumerd kernel snapshot position");
247 goto end_unlock;
248 }
249
250 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
251 produced_pos, nb_packets_per_stream,
252 stream->max_sb_size);
253
254 while ((long) (consumed_pos - produced_pos) < 0) {
255 ssize_t read_len;
256 unsigned long len, padded_len;
257 const char *subbuf_addr;
258 struct lttng_buffer_view subbuf_view;
259
260 health_code_update();
261 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
262
263 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
264 if (ret < 0) {
265 if (ret != -EAGAIN) {
266 PERROR("kernctl_get_subbuf snapshot");
267 goto end_unlock;
268 }
269 DBG("Kernel consumer get subbuf failed. Skipping it.");
270 consumed_pos += stream->max_sb_size;
271 stream->chan->lost_packets++;
272 continue;
273 }
274
275 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
276 if (ret < 0) {
277 ERR("Snapshot kernctl_get_subbuf_size");
278 goto error_put_subbuf;
279 }
280
281 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
282 if (ret < 0) {
283 ERR("Snapshot kernctl_get_padded_subbuf_size");
284 goto error_put_subbuf;
285 }
286
287 ret = get_current_subbuf_addr(stream, &subbuf_addr);
288 if (ret) {
289 goto error_put_subbuf;
290 }
291
292 subbuf_view = lttng_buffer_view_init(
293 subbuf_addr, 0, padded_len);
294 read_len = lttng_consumer_on_read_subbuffer_mmap(
295 stream, &subbuf_view,
296 padded_len - len);
297 /*
298 * We write the padded len in local tracefiles but the data len
299 * when using a relay. Display the error but continue processing
300 * to try to release the subbuffer.
301 */
302 if (relayd_id != (uint64_t) -1ULL) {
303 if (read_len != len) {
304 ERR("Error sending to the relay (ret: %zd != len: %lu)",
305 read_len, len);
306 }
307 } else {
308 if (read_len != padded_len) {
309 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
310 read_len, padded_len);
311 }
312 }
313
314 ret = kernctl_put_subbuf(stream->wait_fd);
315 if (ret < 0) {
316 ERR("Snapshot kernctl_put_subbuf");
317 goto end_unlock;
318 }
319 consumed_pos += stream->max_sb_size;
320 }
321
322 if (relayd_id == (uint64_t) -1ULL) {
323 if (stream->out_fd >= 0) {
324 ret = close(stream->out_fd);
325 if (ret < 0) {
326 PERROR("Kernel consumer snapshot close out_fd");
327 goto end_unlock;
328 }
329 stream->out_fd = -1;
330 }
331 } else {
332 close_relayd_stream(stream);
333 stream->net_seq_idx = (uint64_t) -1ULL;
334 }
335 lttng_trace_chunk_put(stream->trace_chunk);
336 stream->trace_chunk = NULL;
337 pthread_mutex_unlock(&stream->lock);
338 }
339
340 /* All good! */
341 ret = 0;
342 goto end;
343
344 error_put_subbuf:
345 ret = kernctl_put_subbuf(stream->wait_fd);
346 if (ret < 0) {
347 ERR("Snapshot kernctl_put_subbuf error path");
348 }
349 end_unlock:
350 pthread_mutex_unlock(&stream->lock);
351 end:
352 rcu_read_unlock();
353 pthread_mutex_unlock(&channel->lock);
354 return ret;
355 }
356
357 /*
358 * Read the whole metadata available for a snapshot.
359 * RCU read-side lock must be held across this function to ensure existence of
360 * metadata_channel.
361 *
362 * Returns 0 on success, < 0 on error
363 */
364 static int lttng_kconsumer_snapshot_metadata(
365 struct lttng_consumer_channel *metadata_channel,
366 uint64_t key, char *path, uint64_t relayd_id,
367 struct lttng_consumer_local_data *ctx)
368 {
369 int ret, use_relayd = 0;
370 ssize_t ret_read;
371 struct lttng_consumer_stream *metadata_stream;
372
373 assert(ctx);
374
375 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
376 key, path);
377
378 rcu_read_lock();
379
380 metadata_stream = metadata_channel->metadata_stream;
381 assert(metadata_stream);
382
383 /* Take all the appropriate locks hehehe.*/
384 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
385 assert(metadata_channel->trace_chunk);
386 assert(metadata_stream->trace_chunk);
387
388 /* Flag once that we have a valid relayd for the stream. */
389 if (relayd_id != (uint64_t) -1ULL) {
390 use_relayd = 1;
391 }
392
393 if (use_relayd) {
394 ret = consumer_send_relayd_stream(metadata_stream, path);
395 if (ret < 0) {
396 goto error_snapshot;
397 }
398 } else {
399 ret = consumer_stream_create_output_files(metadata_stream,
400 false);
401 if (ret < 0) {
402 goto error_snapshot;
403 }
404 }
405
406 do {
407 health_code_update();
408
409 ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
410 if (ret_read < 0) {
411 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
412 ret_read);
413 ret = ret_read;
414 goto error_snapshot;
415 }
416 } while (ret_read > 0);
417
418 if (use_relayd) {
419 close_relayd_stream(metadata_stream);
420 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
421 } else {
422 if (metadata_stream->out_fd >= 0) {
423 ret = close(metadata_stream->out_fd);
424 if (ret < 0) {
425 PERROR("Kernel consumer snapshot metadata close out_fd");
426 /*
427 * Don't go on error here since the snapshot was successful at this
428 * point but somehow the close failed.
429 */
430 }
431 metadata_stream->out_fd = -1;
432 lttng_trace_chunk_put(metadata_stream->trace_chunk);
433 metadata_stream->trace_chunk = NULL;
434 }
435 }
436
437 ret = 0;
438 error_snapshot:
439 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
440 cds_list_del(&metadata_stream->send_node);
441 consumer_stream_destroy(metadata_stream, NULL);
442 metadata_channel->metadata_stream = NULL;
443 rcu_read_unlock();
444 return ret;
445 }
446
447 /*
448 * Receive command from session daemon and process it.
449 *
450 * Return 1 on success else a negative value or 0.
451 */
452 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
453 int sock, struct pollfd *consumer_sockpoll)
454 {
455 ssize_t ret;
456 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
457 struct lttcomm_consumer_msg msg;
458
459 health_code_update();
460
461 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
462 if (ret != sizeof(msg)) {
463 if (ret > 0) {
464 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
465 ret = -1;
466 }
467 return ret;
468 }
469
470 health_code_update();
471
472 /* Deprecated command */
473 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
474
475 health_code_update();
476
477 /* relayd needs RCU read-side protection */
478 rcu_read_lock();
479
480 switch (msg.cmd_type) {
481 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
482 {
483 /* Session daemon status message are handled in the following call. */
484 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
485 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
486 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
487 msg.u.relayd_sock.relayd_session_id);
488 goto end_nosignal;
489 }
490 case LTTNG_CONSUMER_ADD_CHANNEL:
491 {
492 struct lttng_consumer_channel *new_channel;
493 int ret_recv;
494 const uint64_t chunk_id = msg.u.channel.chunk_id.value;
495
496 health_code_update();
497
498 /* First send a status message before receiving the fds. */
499 ret = consumer_send_status_msg(sock, ret_code);
500 if (ret < 0) {
501 /* Somehow, the session daemon is not responding anymore. */
502 goto error_fatal;
503 }
504
505 health_code_update();
506
507 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
508 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
509 msg.u.channel.session_id,
510 msg.u.channel.chunk_id.is_set ?
511 &chunk_id : NULL,
512 msg.u.channel.pathname,
513 msg.u.channel.name,
514 msg.u.channel.relayd_id, msg.u.channel.output,
515 msg.u.channel.tracefile_size,
516 msg.u.channel.tracefile_count, 0,
517 msg.u.channel.monitor,
518 msg.u.channel.live_timer_interval,
519 msg.u.channel.is_live,
520 NULL, NULL);
521 if (new_channel == NULL) {
522 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
523 goto end_nosignal;
524 }
525 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
526 switch (msg.u.channel.output) {
527 case LTTNG_EVENT_SPLICE:
528 new_channel->output = CONSUMER_CHANNEL_SPLICE;
529 break;
530 case LTTNG_EVENT_MMAP:
531 new_channel->output = CONSUMER_CHANNEL_MMAP;
532 break;
533 default:
534 ERR("Channel output unknown %d", msg.u.channel.output);
535 goto end_nosignal;
536 }
537
538 /* Translate and save channel type. */
539 switch (msg.u.channel.type) {
540 case CONSUMER_CHANNEL_TYPE_DATA:
541 case CONSUMER_CHANNEL_TYPE_METADATA:
542 new_channel->type = msg.u.channel.type;
543 break;
544 default:
545 assert(0);
546 goto end_nosignal;
547 };
548
549 health_code_update();
550
551 if (ctx->on_recv_channel != NULL) {
552 ret_recv = ctx->on_recv_channel(new_channel);
553 if (ret_recv == 0) {
554 ret = consumer_add_channel(new_channel, ctx);
555 } else if (ret_recv < 0) {
556 goto end_nosignal;
557 }
558 } else {
559 ret = consumer_add_channel(new_channel, ctx);
560 }
561 if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) {
562 int monitor_start_ret;
563
564 DBG("Consumer starting monitor timer");
565 consumer_timer_live_start(new_channel,
566 msg.u.channel.live_timer_interval);
567 monitor_start_ret = consumer_timer_monitor_start(
568 new_channel,
569 msg.u.channel.monitor_timer_interval);
570 if (monitor_start_ret < 0) {
571 ERR("Starting channel monitoring timer failed");
572 goto end_nosignal;
573 }
574
575 }
576
577 health_code_update();
578
579 /* If we received an error in add_channel, we need to report it. */
580 if (ret < 0) {
581 ret = consumer_send_status_msg(sock, ret);
582 if (ret < 0) {
583 goto error_fatal;
584 }
585 goto end_nosignal;
586 }
587
588 goto end_nosignal;
589 }
590 case LTTNG_CONSUMER_ADD_STREAM:
591 {
592 int fd;
593 struct lttng_pipe *stream_pipe;
594 struct lttng_consumer_stream *new_stream;
595 struct lttng_consumer_channel *channel;
596 int alloc_ret = 0;
597
598 /*
599 * Get stream's channel reference. Needed when adding the stream to the
600 * global hash table.
601 */
602 channel = consumer_find_channel(msg.u.stream.channel_key);
603 if (!channel) {
604 /*
605 * We could not find the channel. Can happen if cpu hotplug
606 * happens while tearing down.
607 */
608 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
609 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
610 }
611
612 health_code_update();
613
614 /* First send a status message before receiving the fds. */
615 ret = consumer_send_status_msg(sock, ret_code);
616 if (ret < 0) {
617 /* Somehow, the session daemon is not responding anymore. */
618 goto error_add_stream_fatal;
619 }
620
621 health_code_update();
622
623 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
624 /* Channel was not found. */
625 goto error_add_stream_nosignal;
626 }
627
628 /* Blocking call */
629 health_poll_entry();
630 ret = lttng_consumer_poll_socket(consumer_sockpoll);
631 health_poll_exit();
632 if (ret) {
633 goto error_add_stream_fatal;
634 }
635
636 health_code_update();
637
638 /* Get stream file descriptor from socket */
639 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
640 if (ret != sizeof(fd)) {
641 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
642 goto end;
643 }
644
645 health_code_update();
646
647 /*
648 * Send status code to session daemon only if the recv works. If the
649 * above recv() failed, the session daemon is notified through the
650 * error socket and the teardown is eventually done.
651 */
652 ret = consumer_send_status_msg(sock, ret_code);
653 if (ret < 0) {
654 /* Somehow, the session daemon is not responding anymore. */
655 goto error_add_stream_nosignal;
656 }
657
658 health_code_update();
659
660 pthread_mutex_lock(&channel->lock);
661 new_stream = consumer_stream_create(
662 channel,
663 channel->key,
664 fd,
665 channel->name,
666 channel->relayd_id,
667 channel->session_id,
668 channel->trace_chunk,
669 msg.u.stream.cpu,
670 &alloc_ret,
671 channel->type,
672 channel->monitor);
673 if (new_stream == NULL) {
674 switch (alloc_ret) {
675 case -ENOMEM:
676 case -EINVAL:
677 default:
678 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
679 break;
680 }
681 pthread_mutex_unlock(&channel->lock);
682 goto error_add_stream_nosignal;
683 }
684
685 new_stream->wait_fd = fd;
686 ret = kernctl_get_max_subbuf_size(new_stream->wait_fd,
687 &new_stream->max_sb_size);
688 if (ret < 0) {
689 pthread_mutex_unlock(&channel->lock);
690 ERR("Failed to get kernel maximal subbuffer size");
691 goto error_add_stream_nosignal;
692 }
693
694 consumer_stream_update_channel_attributes(new_stream,
695 channel);
696
697 /*
698 * We've just assigned the channel to the stream so increment the
699 * refcount right now. We don't need to increment the refcount for
700 * streams in no monitor because we handle manually the cleanup of
701 * those. It is very important to make sure there is NO prior
702 * consumer_del_stream() calls or else the refcount will be unbalanced.
703 */
704 if (channel->monitor) {
705 uatomic_inc(&new_stream->chan->refcount);
706 }
707
708 /*
709 * The buffer flush is done on the session daemon side for the kernel
710 * so no need for the stream "hangup_flush_done" variable to be
711 * tracked. This is important for a kernel stream since we don't rely
712 * on the flush state of the stream to read data. It's not the case for
713 * user space tracing.
714 */
715 new_stream->hangup_flush_done = 0;
716
717 health_code_update();
718
719 pthread_mutex_lock(&new_stream->lock);
720 if (ctx->on_recv_stream) {
721 ret = ctx->on_recv_stream(new_stream);
722 if (ret < 0) {
723 pthread_mutex_unlock(&new_stream->lock);
724 pthread_mutex_unlock(&channel->lock);
725 consumer_stream_free(new_stream);
726 goto error_add_stream_nosignal;
727 }
728 }
729 health_code_update();
730
731 if (new_stream->metadata_flag) {
732 channel->metadata_stream = new_stream;
733 }
734
735 /* Do not monitor this stream. */
736 if (!channel->monitor) {
737 DBG("Kernel consumer add stream %s in no monitor mode with "
738 "relayd id %" PRIu64, new_stream->name,
739 new_stream->net_seq_idx);
740 cds_list_add(&new_stream->send_node, &channel->streams.head);
741 pthread_mutex_unlock(&new_stream->lock);
742 pthread_mutex_unlock(&channel->lock);
743 goto end_add_stream;
744 }
745
746 /* Send stream to relayd if the stream has an ID. */
747 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
748 ret = consumer_send_relayd_stream(new_stream,
749 new_stream->chan->pathname);
750 if (ret < 0) {
751 pthread_mutex_unlock(&new_stream->lock);
752 pthread_mutex_unlock(&channel->lock);
753 consumer_stream_free(new_stream);
754 goto error_add_stream_nosignal;
755 }
756
757 /*
758 * If adding an extra stream to an already
759 * existing channel (e.g. cpu hotplug), we need
760 * to send the "streams_sent" command to relayd.
761 */
762 if (channel->streams_sent_to_relayd) {
763 ret = consumer_send_relayd_streams_sent(
764 new_stream->net_seq_idx);
765 if (ret < 0) {
766 pthread_mutex_unlock(&new_stream->lock);
767 pthread_mutex_unlock(&channel->lock);
768 goto error_add_stream_nosignal;
769 }
770 }
771 }
772 pthread_mutex_unlock(&new_stream->lock);
773 pthread_mutex_unlock(&channel->lock);
774
775 /* Get the right pipe where the stream will be sent. */
776 if (new_stream->metadata_flag) {
777 consumer_add_metadata_stream(new_stream);
778 stream_pipe = ctx->consumer_metadata_pipe;
779 } else {
780 consumer_add_data_stream(new_stream);
781 stream_pipe = ctx->consumer_data_pipe;
782 }
783
784 /* Visible to other threads */
785 new_stream->globally_visible = 1;
786
787 health_code_update();
788
789 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
790 if (ret < 0) {
791 ERR("Consumer write %s stream to pipe %d",
792 new_stream->metadata_flag ? "metadata" : "data",
793 lttng_pipe_get_writefd(stream_pipe));
794 if (new_stream->metadata_flag) {
795 consumer_del_stream_for_metadata(new_stream);
796 } else {
797 consumer_del_stream_for_data(new_stream);
798 }
799 goto error_add_stream_nosignal;
800 }
801
802 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64,
803 new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id);
804 end_add_stream:
805 break;
806 error_add_stream_nosignal:
807 goto end_nosignal;
808 error_add_stream_fatal:
809 goto error_fatal;
810 }
811 case LTTNG_CONSUMER_STREAMS_SENT:
812 {
813 struct lttng_consumer_channel *channel;
814
815 /*
816 * Get stream's channel reference. Needed when adding the stream to the
817 * global hash table.
818 */
819 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
820 if (!channel) {
821 /*
822 * We could not find the channel. Can happen if cpu hotplug
823 * happens while tearing down.
824 */
825 ERR("Unable to find channel key %" PRIu64,
826 msg.u.sent_streams.channel_key);
827 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
828 }
829
830 health_code_update();
831
832 /*
833 * Send status code to session daemon.
834 */
835 ret = consumer_send_status_msg(sock, ret_code);
836 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
837 /* Somehow, the session daemon is not responding anymore. */
838 goto error_streams_sent_nosignal;
839 }
840
841 health_code_update();
842
843 /*
844 * We should not send this message if we don't monitor the
845 * streams in this channel.
846 */
847 if (!channel->monitor) {
848 goto end_error_streams_sent;
849 }
850
851 health_code_update();
852 /* Send stream to relayd if the stream has an ID. */
853 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
854 ret = consumer_send_relayd_streams_sent(
855 msg.u.sent_streams.net_seq_idx);
856 if (ret < 0) {
857 goto error_streams_sent_nosignal;
858 }
859 channel->streams_sent_to_relayd = true;
860 }
861 end_error_streams_sent:
862 break;
863 error_streams_sent_nosignal:
864 goto end_nosignal;
865 }
866 case LTTNG_CONSUMER_UPDATE_STREAM:
867 {
868 rcu_read_unlock();
869 return -ENOSYS;
870 }
871 case LTTNG_CONSUMER_DESTROY_RELAYD:
872 {
873 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
874 struct consumer_relayd_sock_pair *relayd;
875
876 DBG("Kernel consumer destroying relayd %" PRIu64, index);
877
878 /* Get relayd reference if exists. */
879 relayd = consumer_find_relayd(index);
880 if (relayd == NULL) {
881 DBG("Unable to find relayd %" PRIu64, index);
882 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
883 }
884
885 /*
886 * Each relayd socket pair has a refcount of stream attached to it
887 * which tells if the relayd is still active or not depending on the
888 * refcount value.
889 *
890 * This will set the destroy flag of the relayd object and destroy it
891 * if the refcount reaches zero when called.
892 *
893 * The destroy can happen either here or when a stream fd hangs up.
894 */
895 if (relayd) {
896 consumer_flag_relayd_for_destroy(relayd);
897 }
898
899 health_code_update();
900
901 ret = consumer_send_status_msg(sock, ret_code);
902 if (ret < 0) {
903 /* Somehow, the session daemon is not responding anymore. */
904 goto error_fatal;
905 }
906
907 goto end_nosignal;
908 }
909 case LTTNG_CONSUMER_DATA_PENDING:
910 {
911 int32_t ret;
912 uint64_t id = msg.u.data_pending.session_id;
913
914 DBG("Kernel consumer data pending command for id %" PRIu64, id);
915
916 ret = consumer_data_pending(id);
917
918 health_code_update();
919
920 /* Send back returned value to session daemon */
921 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
922 if (ret < 0) {
923 PERROR("send data pending ret code");
924 goto error_fatal;
925 }
926
927 /*
928 * No need to send back a status message since the data pending
929 * returned value is the response.
930 */
931 break;
932 }
933 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
934 {
935 struct lttng_consumer_channel *channel;
936 uint64_t key = msg.u.snapshot_channel.key;
937
938 channel = consumer_find_channel(key);
939 if (!channel) {
940 ERR("Channel %" PRIu64 " not found", key);
941 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
942 } else {
943 if (msg.u.snapshot_channel.metadata == 1) {
944 ret = lttng_kconsumer_snapshot_metadata(channel, key,
945 msg.u.snapshot_channel.pathname,
946 msg.u.snapshot_channel.relayd_id, ctx);
947 if (ret < 0) {
948 ERR("Snapshot metadata failed");
949 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
950 }
951 } else {
952 ret = lttng_kconsumer_snapshot_channel(channel, key,
953 msg.u.snapshot_channel.pathname,
954 msg.u.snapshot_channel.relayd_id,
955 msg.u.snapshot_channel.nb_packets_per_stream,
956 ctx);
957 if (ret < 0) {
958 ERR("Snapshot channel failed");
959 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
960 }
961 }
962 }
963 health_code_update();
964
965 ret = consumer_send_status_msg(sock, ret_code);
966 if (ret < 0) {
967 /* Somehow, the session daemon is not responding anymore. */
968 goto end_nosignal;
969 }
970 break;
971 }
972 case LTTNG_CONSUMER_DESTROY_CHANNEL:
973 {
974 uint64_t key = msg.u.destroy_channel.key;
975 struct lttng_consumer_channel *channel;
976
977 channel = consumer_find_channel(key);
978 if (!channel) {
979 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
980 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
981 }
982
983 health_code_update();
984
985 ret = consumer_send_status_msg(sock, ret_code);
986 if (ret < 0) {
987 /* Somehow, the session daemon is not responding anymore. */
988 goto end_destroy_channel;
989 }
990
991 health_code_update();
992
993 /* Stop right now if no channel was found. */
994 if (!channel) {
995 goto end_destroy_channel;
996 }
997
998 /*
999 * This command should ONLY be issued for channel with streams set in
1000 * no monitor mode.
1001 */
1002 assert(!channel->monitor);
1003
1004 /*
1005 * The refcount should ALWAYS be 0 in the case of a channel in no
1006 * monitor mode.
1007 */
1008 assert(!uatomic_sub_return(&channel->refcount, 1));
1009
1010 consumer_del_channel(channel);
1011 end_destroy_channel:
1012 goto end_nosignal;
1013 }
1014 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1015 {
1016 ssize_t ret;
1017 uint64_t count;
1018 struct lttng_consumer_channel *channel;
1019 uint64_t id = msg.u.discarded_events.session_id;
1020 uint64_t key = msg.u.discarded_events.channel_key;
1021
1022 DBG("Kernel consumer discarded events command for session id %"
1023 PRIu64 ", channel key %" PRIu64, id, key);
1024
1025 channel = consumer_find_channel(key);
1026 if (!channel) {
1027 ERR("Kernel consumer discarded events channel %"
1028 PRIu64 " not found", key);
1029 count = 0;
1030 } else {
1031 count = channel->discarded_events;
1032 }
1033
1034 health_code_update();
1035
1036 /* Send back returned value to session daemon */
1037 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1038 if (ret < 0) {
1039 PERROR("send discarded events");
1040 goto error_fatal;
1041 }
1042
1043 break;
1044 }
1045 case LTTNG_CONSUMER_LOST_PACKETS:
1046 {
1047 ssize_t ret;
1048 uint64_t count;
1049 struct lttng_consumer_channel *channel;
1050 uint64_t id = msg.u.lost_packets.session_id;
1051 uint64_t key = msg.u.lost_packets.channel_key;
1052
1053 DBG("Kernel consumer lost packets command for session id %"
1054 PRIu64 ", channel key %" PRIu64, id, key);
1055
1056 channel = consumer_find_channel(key);
1057 if (!channel) {
1058 ERR("Kernel consumer lost packets channel %"
1059 PRIu64 " not found", key);
1060 count = 0;
1061 } else {
1062 count = channel->lost_packets;
1063 }
1064
1065 health_code_update();
1066
1067 /* Send back returned value to session daemon */
1068 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
1069 if (ret < 0) {
1070 PERROR("send lost packets");
1071 goto error_fatal;
1072 }
1073
1074 break;
1075 }
1076 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1077 {
1078 int channel_monitor_pipe;
1079
1080 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1081 /* Successfully received the command's type. */
1082 ret = consumer_send_status_msg(sock, ret_code);
1083 if (ret < 0) {
1084 goto error_fatal;
1085 }
1086
1087 ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe,
1088 1);
1089 if (ret != sizeof(channel_monitor_pipe)) {
1090 ERR("Failed to receive channel monitor pipe");
1091 goto error_fatal;
1092 }
1093
1094 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1095 ret = consumer_timer_thread_set_channel_monitor_pipe(
1096 channel_monitor_pipe);
1097 if (!ret) {
1098 int flags;
1099
1100 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1101 /* Set the pipe as non-blocking. */
1102 ret = fcntl(channel_monitor_pipe, F_GETFL, 0);
1103 if (ret == -1) {
1104 PERROR("fcntl get flags of the channel monitoring pipe");
1105 goto error_fatal;
1106 }
1107 flags = ret;
1108
1109 ret = fcntl(channel_monitor_pipe, F_SETFL,
1110 flags | O_NONBLOCK);
1111 if (ret == -1) {
1112 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1113 goto error_fatal;
1114 }
1115 DBG("Channel monitor pipe set as non-blocking");
1116 } else {
1117 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1118 }
1119 ret = consumer_send_status_msg(sock, ret_code);
1120 if (ret < 0) {
1121 goto error_fatal;
1122 }
1123 break;
1124 }
1125 case LTTNG_CONSUMER_ROTATE_CHANNEL:
1126 {
1127 struct lttng_consumer_channel *channel;
1128 uint64_t key = msg.u.rotate_channel.key;
1129
1130 DBG("Consumer rotate channel %" PRIu64, key);
1131
1132 channel = consumer_find_channel(key);
1133 if (!channel) {
1134 ERR("Channel %" PRIu64 " not found", key);
1135 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1136 } else {
1137 /*
1138 * Sample the rotate position of all the streams in this channel.
1139 */
1140 ret = lttng_consumer_rotate_channel(channel, key,
1141 msg.u.rotate_channel.relayd_id,
1142 msg.u.rotate_channel.metadata,
1143 ctx);
1144 if (ret < 0) {
1145 ERR("Rotate channel failed");
1146 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
1147 }
1148
1149 health_code_update();
1150 }
1151 ret = consumer_send_status_msg(sock, ret_code);
1152 if (ret < 0) {
1153 /* Somehow, the session daemon is not responding anymore. */
1154 goto error_rotate_channel;
1155 }
1156 if (channel) {
1157 /* Rotate the streams that are ready right now. */
1158 ret = lttng_consumer_rotate_ready_streams(
1159 channel, key, ctx);
1160 if (ret < 0) {
1161 ERR("Rotate ready streams failed");
1162 }
1163 }
1164 break;
1165 error_rotate_channel:
1166 goto end_nosignal;
1167 }
1168 case LTTNG_CONSUMER_CLEAR_CHANNEL:
1169 {
1170 struct lttng_consumer_channel *channel;
1171 uint64_t key = msg.u.clear_channel.key;
1172
1173 channel = consumer_find_channel(key);
1174 if (!channel) {
1175 DBG("Channel %" PRIu64 " not found", key);
1176 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1177 } else {
1178 ret = lttng_consumer_clear_channel(channel);
1179 if (ret) {
1180 ERR("Clear channel failed");
1181 ret_code = ret;
1182 }
1183
1184 health_code_update();
1185 }
1186 ret = consumer_send_status_msg(sock, ret_code);
1187 if (ret < 0) {
1188 /* Somehow, the session daemon is not responding anymore. */
1189 goto end_nosignal;
1190 }
1191
1192 break;
1193 }
1194 case LTTNG_CONSUMER_INIT:
1195 {
1196 ret_code = lttng_consumer_init_command(ctx,
1197 msg.u.init.sessiond_uuid);
1198 health_code_update();
1199 ret = consumer_send_status_msg(sock, ret_code);
1200 if (ret < 0) {
1201 /* Somehow, the session daemon is not responding anymore. */
1202 goto end_nosignal;
1203 }
1204 break;
1205 }
1206 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
1207 {
1208 const struct lttng_credentials credentials = {
1209 .uid = msg.u.create_trace_chunk.credentials.value.uid,
1210 .gid = msg.u.create_trace_chunk.credentials.value.gid,
1211 };
1212 const bool is_local_trace =
1213 !msg.u.create_trace_chunk.relayd_id.is_set;
1214 const uint64_t relayd_id =
1215 msg.u.create_trace_chunk.relayd_id.value;
1216 const char *chunk_override_name =
1217 *msg.u.create_trace_chunk.override_name ?
1218 msg.u.create_trace_chunk.override_name :
1219 NULL;
1220 struct lttng_directory_handle *chunk_directory_handle = NULL;
1221
1222 /*
1223 * The session daemon will only provide a chunk directory file
1224 * descriptor for local traces.
1225 */
1226 if (is_local_trace) {
1227 int chunk_dirfd;
1228
1229 /* Acnowledge the reception of the command. */
1230 ret = consumer_send_status_msg(sock,
1231 LTTCOMM_CONSUMERD_SUCCESS);
1232 if (ret < 0) {
1233 /* Somehow, the session daemon is not responding anymore. */
1234 goto end_nosignal;
1235 }
1236
1237 ret = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1);
1238 if (ret != sizeof(chunk_dirfd)) {
1239 ERR("Failed to receive trace chunk directory file descriptor");
1240 goto error_fatal;
1241 }
1242
1243 DBG("Received trace chunk directory fd (%d)",
1244 chunk_dirfd);
1245 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
1246 chunk_dirfd);
1247 if (!chunk_directory_handle) {
1248 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1249 if (close(chunk_dirfd)) {
1250 PERROR("Failed to close chunk directory file descriptor");
1251 }
1252 goto error_fatal;
1253 }
1254 }
1255
1256 ret_code = lttng_consumer_create_trace_chunk(
1257 !is_local_trace ? &relayd_id : NULL,
1258 msg.u.create_trace_chunk.session_id,
1259 msg.u.create_trace_chunk.chunk_id,
1260 (time_t) msg.u.create_trace_chunk
1261 .creation_timestamp,
1262 chunk_override_name,
1263 msg.u.create_trace_chunk.credentials.is_set ?
1264 &credentials :
1265 NULL,
1266 chunk_directory_handle);
1267 lttng_directory_handle_put(chunk_directory_handle);
1268 goto end_msg_sessiond;
1269 }
1270 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
1271 {
1272 enum lttng_trace_chunk_command_type close_command =
1273 msg.u.close_trace_chunk.close_command.value;
1274 const uint64_t relayd_id =
1275 msg.u.close_trace_chunk.relayd_id.value;
1276 struct lttcomm_consumer_close_trace_chunk_reply reply;
1277 char path[LTTNG_PATH_MAX];
1278
1279 ret_code = lttng_consumer_close_trace_chunk(
1280 msg.u.close_trace_chunk.relayd_id.is_set ?
1281 &relayd_id :
1282 NULL,
1283 msg.u.close_trace_chunk.session_id,
1284 msg.u.close_trace_chunk.chunk_id,
1285 (time_t) msg.u.close_trace_chunk.close_timestamp,
1286 msg.u.close_trace_chunk.close_command.is_set ?
1287 &close_command :
1288 NULL, path);
1289 reply.ret_code = ret_code;
1290 reply.path_length = strlen(path) + 1;
1291 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
1292 if (ret != sizeof(reply)) {
1293 goto error_fatal;
1294 }
1295 ret = lttcomm_send_unix_sock(sock, path, reply.path_length);
1296 if (ret != reply.path_length) {
1297 goto error_fatal;
1298 }
1299 goto end_nosignal;
1300 }
1301 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
1302 {
1303 const uint64_t relayd_id =
1304 msg.u.trace_chunk_exists.relayd_id.value;
1305
1306 ret_code = lttng_consumer_trace_chunk_exists(
1307 msg.u.trace_chunk_exists.relayd_id.is_set ?
1308 &relayd_id : NULL,
1309 msg.u.trace_chunk_exists.session_id,
1310 msg.u.trace_chunk_exists.chunk_id);
1311 goto end_msg_sessiond;
1312 }
1313 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
1314 {
1315 const uint64_t key = msg.u.open_channel_packets.key;
1316 struct lttng_consumer_channel *channel =
1317 consumer_find_channel(key);
1318
1319 if (channel) {
1320 pthread_mutex_lock(&channel->lock);
1321 ret_code = lttng_consumer_open_channel_packets(channel);
1322 pthread_mutex_unlock(&channel->lock);
1323 } else {
1324 WARN("Channel %" PRIu64 " not found", key);
1325 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1326 }
1327
1328 health_code_update();
1329 goto end_msg_sessiond;
1330 }
1331 default:
1332 goto end_nosignal;
1333 }
1334
1335 end_nosignal:
1336 /*
1337 * Return 1 to indicate success since the 0 value can be a socket
1338 * shutdown during the recv() or send() call.
1339 */
1340 ret = 1;
1341 goto end;
1342 error_fatal:
1343 /* This will issue a consumer stop. */
1344 ret = -1;
1345 goto end;
1346 end_msg_sessiond:
1347 /*
1348 * The returned value here is not useful since either way we'll return 1 to
1349 * the caller because the session daemon socket management is done
1350 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1351 */
1352 ret = consumer_send_status_msg(sock, ret_code);
1353 if (ret < 0) {
1354 goto error_fatal;
1355 }
1356 ret = 1;
1357 end:
1358 health_code_update();
1359 rcu_read_unlock();
1360 return ret;
1361 }
1362
1363 /*
1364 * Sync metadata meaning request them to the session daemon and snapshot to the
1365 * metadata thread can consumer them.
1366 *
1367 * Metadata stream lock MUST be acquired.
1368 */
1369 enum sync_metadata_status lttng_kconsumer_sync_metadata(
1370 struct lttng_consumer_stream *metadata)
1371 {
1372 int ret;
1373 enum sync_metadata_status status;
1374
1375 assert(metadata);
1376
1377 ret = kernctl_buffer_flush(metadata->wait_fd);
1378 if (ret < 0) {
1379 ERR("Failed to flush kernel stream");
1380 status = SYNC_METADATA_STATUS_ERROR;
1381 goto end;
1382 }
1383
1384 ret = kernctl_snapshot(metadata->wait_fd);
1385 if (ret < 0) {
1386 if (errno == EAGAIN) {
1387 /* No new metadata, exit. */
1388 DBG("Sync metadata, no new kernel metadata");
1389 status = SYNC_METADATA_STATUS_NO_DATA;
1390 } else {
1391 ERR("Sync metadata, taking kernel snapshot failed.");
1392 status = SYNC_METADATA_STATUS_ERROR;
1393 }
1394 } else {
1395 status = SYNC_METADATA_STATUS_NEW_DATA;
1396 }
1397
1398 end:
1399 return status;
1400 }
1401
1402 static
1403 int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
1404 struct stream_subbuffer *subbuf)
1405 {
1406 int ret;
1407
1408 ret = kernctl_get_subbuf_size(
1409 stream->wait_fd, &subbuf->info.data.subbuf_size);
1410 if (ret) {
1411 goto end;
1412 }
1413
1414 ret = kernctl_get_padded_subbuf_size(
1415 stream->wait_fd, &subbuf->info.data.padded_subbuf_size);
1416 if (ret) {
1417 goto end;
1418 }
1419
1420 end:
1421 return ret;
1422 }
1423
1424 static
1425 int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
1426 struct stream_subbuffer *subbuf)
1427 {
1428 int ret;
1429
1430 ret = extract_common_subbuffer_info(stream, subbuf);
1431 if (ret) {
1432 goto end;
1433 }
1434
1435 ret = kernctl_get_metadata_version(
1436 stream->wait_fd, &subbuf->info.metadata.version);
1437 if (ret) {
1438 goto end;
1439 }
1440
1441 end:
1442 return ret;
1443 }
1444
1445 static
1446 int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
1447 struct stream_subbuffer *subbuf)
1448 {
1449 int ret;
1450
1451 ret = extract_common_subbuffer_info(stream, subbuf);
1452 if (ret) {
1453 goto end;
1454 }
1455
1456 ret = kernctl_get_packet_size(
1457 stream->wait_fd, &subbuf->info.data.packet_size);
1458 if (ret < 0) {
1459 PERROR("Failed to get sub-buffer packet size");
1460 goto end;
1461 }
1462
1463 ret = kernctl_get_content_size(
1464 stream->wait_fd, &subbuf->info.data.content_size);
1465 if (ret < 0) {
1466 PERROR("Failed to get sub-buffer content size");
1467 goto end;
1468 }
1469
1470 ret = kernctl_get_timestamp_begin(
1471 stream->wait_fd, &subbuf->info.data.timestamp_begin);
1472 if (ret < 0) {
1473 PERROR("Failed to get sub-buffer begin timestamp");
1474 goto end;
1475 }
1476
1477 ret = kernctl_get_timestamp_end(
1478 stream->wait_fd, &subbuf->info.data.timestamp_end);
1479 if (ret < 0) {
1480 PERROR("Failed to get sub-buffer end timestamp");
1481 goto end;
1482 }
1483
1484 ret = kernctl_get_events_discarded(
1485 stream->wait_fd, &subbuf->info.data.events_discarded);
1486 if (ret) {
1487 PERROR("Failed to get sub-buffer events discarded count");
1488 goto end;
1489 }
1490
1491 ret = kernctl_get_sequence_number(stream->wait_fd,
1492 &subbuf->info.data.sequence_number.value);
1493 if (ret) {
1494 /* May not be supported by older LTTng-modules. */
1495 if (ret != -ENOTTY) {
1496 PERROR("Failed to get sub-buffer sequence number");
1497 goto end;
1498 }
1499 } else {
1500 subbuf->info.data.sequence_number.is_set = true;
1501 }
1502
1503 ret = kernctl_get_stream_id(
1504 stream->wait_fd, &subbuf->info.data.stream_id);
1505 if (ret < 0) {
1506 PERROR("Failed to get stream id");
1507 goto end;
1508 }
1509
1510 ret = kernctl_get_instance_id(stream->wait_fd,
1511 &subbuf->info.data.stream_instance_id.value);
1512 if (ret) {
1513 /* May not be supported by older LTTng-modules. */
1514 if (ret != -ENOTTY) {
1515 PERROR("Failed to get stream instance id");
1516 goto end;
1517 }
1518 } else {
1519 subbuf->info.data.stream_instance_id.is_set = true;
1520 }
1521 end:
1522 return ret;
1523 }
1524
1525 static
1526 int get_subbuffer_common(struct lttng_consumer_stream *stream,
1527 struct stream_subbuffer *subbuffer)
1528 {
1529 int ret;
1530
1531 ret = kernctl_get_next_subbuf(stream->wait_fd);
1532 if (ret) {
1533 /*
1534 * The caller only expects -ENODATA when there is no data to
1535 * read, but the kernel tracer returns -EAGAIN when there is
1536 * currently no data for a non-finalized stream, and -ENODATA
1537 * when there is no data for a finalized stream. Those can be
1538 * combined into a -ENODATA return value.
1539 */
1540 if (ret == -EAGAIN) {
1541 ret = -ENODATA;
1542 }
1543
1544 goto end;
1545 }
1546
1547 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
1548 stream, subbuffer);
1549 end:
1550 return ret;
1551 }
1552
1553 static
1554 int get_next_subbuffer_splice(struct lttng_consumer_stream *stream,
1555 struct stream_subbuffer *subbuffer)
1556 {
1557 int ret;
1558
1559 ret = get_subbuffer_common(stream, subbuffer);
1560 if (ret) {
1561 goto end;
1562 }
1563
1564 subbuffer->buffer.fd = stream->wait_fd;
1565 end:
1566 return ret;
1567 }
1568
1569 static
1570 int get_next_subbuffer_mmap(struct lttng_consumer_stream *stream,
1571 struct stream_subbuffer *subbuffer)
1572 {
1573 int ret;
1574 const char *addr;
1575
1576 ret = get_subbuffer_common(stream, subbuffer);
1577 if (ret) {
1578 goto end;
1579 }
1580
1581 ret = get_current_subbuf_addr(stream, &addr);
1582 if (ret) {
1583 goto end;
1584 }
1585
1586 subbuffer->buffer.buffer = lttng_buffer_view_init(
1587 addr, 0, subbuffer->info.data.padded_subbuf_size);
1588 end:
1589 return ret;
1590 }
1591
1592 static
1593 int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
1594 struct stream_subbuffer *subbuffer)
1595 {
1596 int ret;
1597 const char *addr;
1598 bool coherent;
1599
1600 ret = kernctl_get_next_subbuf_metadata_check(stream->wait_fd,
1601 &coherent);
1602 if (ret) {
1603 goto end;
1604 }
1605
1606 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
1607 stream, subbuffer);
1608 if (ret) {
1609 goto end;
1610 }
1611
1612 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
1613
1614 ret = get_current_subbuf_addr(stream, &addr);
1615 if (ret) {
1616 goto end;
1617 }
1618
1619 subbuffer->buffer.buffer = lttng_buffer_view_init(
1620 addr, 0, subbuffer->info.data.padded_subbuf_size);
1621 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1622 subbuffer->info.metadata.padded_subbuf_size,
1623 coherent ? "true" : "false");
1624 end:
1625 /*
1626 * The caller only expects -ENODATA when there is no data to read, but
1627 * the kernel tracer returns -EAGAIN when there is currently no data
1628 * for a non-finalized stream, and -ENODATA when there is no data for a
1629 * finalized stream. Those can be combined into a -ENODATA return value.
1630 */
1631 if (ret == -EAGAIN) {
1632 ret = -ENODATA;
1633 }
1634
1635 return ret;
1636 }
1637
1638 static
1639 int put_next_subbuffer(struct lttng_consumer_stream *stream,
1640 struct stream_subbuffer *subbuffer)
1641 {
1642 const int ret = kernctl_put_next_subbuf(stream->wait_fd);
1643
1644 if (ret) {
1645 if (ret == -EFAULT) {
1646 PERROR("Error in unreserving sub buffer");
1647 } else if (ret == -EIO) {
1648 /* Should never happen with newer LTTng versions */
1649 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1650 }
1651 }
1652
1653 return ret;
1654 }
1655
1656 static
1657 bool is_get_next_check_metadata_available(int tracer_fd)
1658 {
1659 const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL);
1660 const bool available = ret != -ENOTTY;
1661
1662 if (ret == 0) {
1663 /* get succeeded, make sure to put the subbuffer. */
1664 kernctl_put_subbuf(tracer_fd);
1665 }
1666
1667 return available;
1668 }
1669
1670 static
1671 int signal_metadata(struct lttng_consumer_stream *stream,
1672 struct lttng_consumer_local_data *ctx)
1673 {
1674 ASSERT_LOCKED(stream->metadata_rdv_lock);
1675 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
1676 }
1677
1678 static
1679 int lttng_kconsumer_set_stream_ops(
1680 struct lttng_consumer_stream *stream)
1681 {
1682 int ret = 0;
1683
1684 if (stream->metadata_flag && stream->chan->is_live) {
1685 DBG("Attempting to enable metadata bucketization for live consumers");
1686 if (is_get_next_check_metadata_available(stream->wait_fd)) {
1687 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1688 stream->read_subbuffer_ops.get_next_subbuffer =
1689 get_next_subbuffer_metadata_check;
1690 ret = consumer_stream_enable_metadata_bucketization(
1691 stream);
1692 if (ret) {
1693 goto end;
1694 }
1695 } else {
1696 /*
1697 * The kernel tracer version is too old to indicate
1698 * when the metadata stream has reached a "coherent"
1699 * (parseable) point.
1700 *
1701 * This means that a live viewer may see an incoherent
1702 * sequence of metadata and fail to parse it.
1703 */
1704 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1705 metadata_bucket_destroy(stream->metadata_bucket);
1706 stream->metadata_bucket = NULL;
1707 }
1708
1709 stream->read_subbuffer_ops.on_sleep = signal_metadata;
1710 }
1711
1712 if (!stream->read_subbuffer_ops.get_next_subbuffer) {
1713 if (stream->chan->output == CONSUMER_CHANNEL_MMAP) {
1714 stream->read_subbuffer_ops.get_next_subbuffer =
1715 get_next_subbuffer_mmap;
1716 } else {
1717 stream->read_subbuffer_ops.get_next_subbuffer =
1718 get_next_subbuffer_splice;
1719 }
1720 }
1721
1722 if (stream->metadata_flag) {
1723 stream->read_subbuffer_ops.extract_subbuffer_info =
1724 extract_metadata_subbuffer_info;
1725 } else {
1726 stream->read_subbuffer_ops.extract_subbuffer_info =
1727 extract_data_subbuffer_info;
1728 if (stream->chan->is_live) {
1729 stream->read_subbuffer_ops.send_live_beacon =
1730 consumer_flush_kernel_index;
1731 }
1732 }
1733
1734 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
1735 end:
1736 return ret;
1737 }
1738
1739 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1740 {
1741 int ret;
1742
1743 assert(stream);
1744
1745 /*
1746 * Don't create anything if this is set for streaming or if there is
1747 * no current trace chunk on the parent channel.
1748 */
1749 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
1750 stream->chan->trace_chunk) {
1751 ret = consumer_stream_create_output_files(stream, true);
1752 if (ret) {
1753 goto error;
1754 }
1755 }
1756
1757 if (stream->output == LTTNG_EVENT_MMAP) {
1758 /* get the len of the mmap region */
1759 unsigned long mmap_len;
1760
1761 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1762 if (ret != 0) {
1763 PERROR("kernctl_get_mmap_len");
1764 goto error_close_fd;
1765 }
1766 stream->mmap_len = (size_t) mmap_len;
1767
1768 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1769 MAP_PRIVATE, stream->wait_fd, 0);
1770 if (stream->mmap_base == MAP_FAILED) {
1771 PERROR("Error mmaping");
1772 ret = -1;
1773 goto error_close_fd;
1774 }
1775 }
1776
1777 ret = lttng_kconsumer_set_stream_ops(stream);
1778 if (ret) {
1779 goto error_close_fd;
1780 }
1781
1782 /* we return 0 to let the library handle the FD internally */
1783 return 0;
1784
1785 error_close_fd:
1786 if (stream->out_fd >= 0) {
1787 int err;
1788
1789 err = close(stream->out_fd);
1790 assert(!err);
1791 stream->out_fd = -1;
1792 }
1793 error:
1794 return ret;
1795 }
1796
1797 /*
1798 * Check if data is still being extracted from the buffers for a specific
1799 * stream. Consumer data lock MUST be acquired before calling this function
1800 * and the stream lock.
1801 *
1802 * Return 1 if the traced data are still getting read else 0 meaning that the
1803 * data is available for trace viewer reading.
1804 */
1805 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1806 {
1807 int ret;
1808
1809 assert(stream);
1810
1811 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1812 ret = 0;
1813 goto end;
1814 }
1815
1816 ret = kernctl_get_next_subbuf(stream->wait_fd);
1817 if (ret == 0) {
1818 /* There is still data so let's put back this subbuffer. */
1819 ret = kernctl_put_subbuf(stream->wait_fd);
1820 assert(ret == 0);
1821 ret = 1; /* Data is pending */
1822 goto end;
1823 }
1824
1825 /* Data is NOT pending and ready to be read. */
1826 ret = 0;
1827
1828 end:
1829 return ret;
1830 }
This page took 0.104085 seconds and 5 git commands to generate.