2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
9 #include "compat/memstream.h"
10 #include "cpp-common/bt2c/libc-up.hpp"
11 #include "cpp-common/bt2s/make-unique.hpp"
13 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
14 #include "../common/src/metadata/tsdl/decoder.hpp"
15 #include "lttng-live.hpp"
16 #include "metadata.hpp"
18 #define TSDL_MAGIC 0x75d11d57
25 uint32_t content_size
;
27 uint8_t compression_scheme
;
28 uint8_t encryption_scheme
;
29 uint8_t checksum_scheme
;
32 } __attribute__((__packed__
));
34 static bool stream_classes_all_have_default_clock_class(bt_trace_class
*tc
,
35 const bt2c::Logger
& logger
)
38 const bt_clock_class
*cc
= NULL
;
39 const bt_stream_class
*sc
;
42 sc_count
= bt_trace_class_get_stream_class_count(tc
);
43 for (i
= 0; i
< sc_count
; i
++) {
44 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
48 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
51 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
,
52 "Stream class doesn't have a default clock class: "
53 "sc-id={}, sc-name=\"{}\"",
54 bt_stream_class_get_id(sc
), bt_stream_class_get_name(sc
));
63 * Iterate over the stream classes and returns the first clock class
64 * encountered. This is useful to create message iterator inactivity message as
65 * we don't need a particular clock class.
67 static const bt_clock_class
*borrow_any_clock_class(bt_trace_class
*tc
)
70 const bt_clock_class
*cc
= NULL
;
71 const bt_stream_class
*sc
;
73 sc_count
= bt_trace_class_get_stream_class_count(tc
);
74 for (i
= 0; i
< sc_count
; i
++) {
75 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
78 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
88 enum lttng_live_iterator_status
lttng_live_metadata_update(struct lttng_live_trace
*trace
)
90 struct lttng_live_session
*session
= trace
->session
;
91 struct lttng_live_metadata
*metadata
= trace
->metadata
.get();
92 std::vector
<char> metadataBuf
;
95 enum ctf_metadata_decoder_status decoder_status
;
96 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
97 enum lttng_live_get_one_metadata_status metadata_status
;
99 BT_CPPLOGD_SPEC(metadata
->logger
, "Updating metadata for trace: session-id={}, trace-id={}",
100 session
->id
, trace
->id
);
102 /* No metadata stream yet. */
104 if (session
->closed
) {
106 * The session is closed AND we never received any
107 * metadata this indicates that we will never receive
110 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
111 } else if (session
->new_streams_needed
) {
112 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
114 session
->new_streams_needed
= true;
115 status
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
120 if (trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
124 keep_receiving
= true;
125 /* Grab all available metadata. */
126 while (keep_receiving
) {
128 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
129 * for new metadata. If new metadata is received, the function
130 * writes it to the provided file handle and updates the
131 * reply_len output parameter. We call this function in loop
132 * until it returns _END meaning that no new metadata is
134 * We may receive a _CLOSED status if the metadata stream we
135 * are requesting is no longer available on the relay.
136 * If we receive an _ERROR status, it means there was a
137 * networking, allocating, or some other unrecoverable error.
139 metadata_status
= lttng_live_get_one_metadata_packet(trace
, metadataBuf
);
141 switch (metadata_status
) {
142 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
144 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
145 keep_receiving
= false;
147 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
150 "Metadata stream was closed by the Relay, the trace is no longer active: "
151 "trace-id={}, metadata-stream-id={}",
152 trace
->id
, metadata
->stream_id
);
154 * The stream was closed and we received everything
155 * there was to receive for this metadata stream.
156 * We go on with the decoding of what we received. So
157 * that data stream can be decoded.
159 keep_receiving
= false;
160 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
;
162 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
163 BT_CPPLOGE_APPEND_CAUSE_SPEC(metadata
->logger
,
164 "Error getting one trace metadata packet: trace-id={}",
172 if (metadataBuf
.empty()) {
174 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
178 /* The relay sent zero bytes of metadata. */
179 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
184 * Open a new reading file handle on the `metadata_buf` and pass it to
185 * the metadata decoder.
187 fp
.reset(bt_fmemopen(metadataBuf
.data(), metadataBuf
.size(), "rb"));
189 if (errno
== EINTR
&& lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
190 session
->lttng_live_msg_iter
->was_interrupted
= true;
191 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
193 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(metadata
->logger
,
194 "Cannot memory-open metadata buffer", ".");
195 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
201 * The call to ctf_metadata_decoder_append_content() will append
202 * new metadata to our current trace class.
204 BT_CPPLOGD_SPEC(metadata
->logger
, "Appending new metadata to the ctf_trace class");
205 decoder_status
= ctf_metadata_decoder_append_content(metadata
->decoder
.get(), fp
.get());
206 switch (decoder_status
) {
207 case CTF_METADATA_DECODER_STATUS_OK
:
208 if (!trace
->trace_class
) {
209 struct ctf_trace_class
*tc
=
210 ctf_metadata_decoder_borrow_ctf_trace_class(metadata
->decoder
.get());
212 trace
->trace_class
= ctf_metadata_decoder_get_ir_trace_class(metadata
->decoder
.get());
213 trace
->trace
= trace
->trace_class
->instantiate();
215 BT_CPPLOGE_APPEND_CAUSE_SPEC(metadata
->logger
, "Failed to create bt_trace");
219 ctf_trace_class_configure_ir_trace(tc
, *trace
->trace
);
221 if (!stream_classes_all_have_default_clock_class(trace
->trace_class
->libObjPtr(),
223 /* Error logged in function. */
226 trace
->clock_class
= borrow_any_clock_class(trace
->trace_class
->libObjPtr());
229 /* The metadata was updated successfully. */
230 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
240 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
245 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
, uint64_t ctf_trace_id
,
248 struct lttng_live_trace
*trace
;
250 ctf_metadata_decoder_config cfg
{session
->logger
};
251 cfg
.self_comp
= session
->self_comp
;
252 cfg
.create_trace_class
= true;
254 auto metadata
= bt2s::make_unique
<lttng_live_metadata
>(session
->logger
);
255 metadata
->stream_id
= stream_id
;
257 metadata
->decoder
= ctf_metadata_decoder_create(&cfg
);
258 if (!metadata
->decoder
) {
259 BT_CPPLOGE_APPEND_CAUSE_SPEC(session
->logger
, "Failed to create CTF metadata decoder");
263 trace
= lttng_live_session_borrow_or_create_trace_by_id(session
, ctf_trace_id
);
265 BT_CPPLOGE_APPEND_CAUSE_SPEC(session
->logger
, "Failed to borrow trace");
269 trace
->metadata
= std::move(metadata
);